2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 #include <sys/mount.h>
34 #include <sys/resource.h>
35 #include <sys/socket.h>
37 #include <sys/types.h>
39 #include <urcu/uatomic.h>
43 #include <common/common.h>
44 #include <common/compat/socket.h>
45 #include <common/compat/getenv.h>
46 #include <common/defaults.h>
47 #include <common/kernel-consumer/kernel-consumer.h>
48 #include <common/futex.h>
49 #include <common/relayd/relayd.h>
50 #include <common/utils.h>
51 #include <common/daemonize.h>
52 #include <common/config/config.h>
54 #include "lttng-sessiond.h"
55 #include "buffer-registry.h"
62 #include "kernel-consumer.h"
66 #include "ust-consumer.h"
69 #include "health-sessiond.h"
70 #include "testpoint.h"
71 #include "ust-thread.h"
72 #include "agent-thread.h"
74 #include "load-session-thread.h"
78 #define CONSUMERD_FILE "lttng-consumerd"
81 static const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
82 static int tracing_group_name_override
;
83 static char *opt_pidfile
;
84 static int opt_sig_parent
;
85 static int opt_verbose_consumer
;
86 static int opt_daemon
, opt_background
;
87 static int opt_no_kernel
;
88 static char *opt_load_session_path
;
89 static pid_t ppid
; /* Parent PID for --sig-parent option */
90 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
92 static int lockfile_fd
= -1;
94 /* Set to 1 when a SIGUSR1 signal is received. */
95 static int recv_child_signal
;
98 * Consumer daemon specific control data. Every value not initialized here is
99 * set to 0 by the static definition.
101 static struct consumer_data kconsumer_data
= {
102 .type
= LTTNG_CONSUMER_KERNEL
,
103 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
104 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
107 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
108 .lock
= PTHREAD_MUTEX_INITIALIZER
,
109 .cond
= PTHREAD_COND_INITIALIZER
,
110 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
112 static struct consumer_data ustconsumer64_data
= {
113 .type
= LTTNG_CONSUMER64_UST
,
114 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
115 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
118 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
119 .lock
= PTHREAD_MUTEX_INITIALIZER
,
120 .cond
= PTHREAD_COND_INITIALIZER
,
121 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
123 static struct consumer_data ustconsumer32_data
= {
124 .type
= LTTNG_CONSUMER32_UST
,
125 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
126 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
129 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
130 .lock
= PTHREAD_MUTEX_INITIALIZER
,
131 .cond
= PTHREAD_COND_INITIALIZER
,
132 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
135 /* Command line options */
136 static const struct option long_options
[] = {
137 { "client-sock", required_argument
, 0, 'c' },
138 { "apps-sock", required_argument
, 0, 'a' },
139 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
140 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
141 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
142 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
143 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
144 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
145 { "consumerd32-path", required_argument
, 0, '\0' },
146 { "consumerd32-libdir", required_argument
, 0, '\0' },
147 { "consumerd64-path", required_argument
, 0, '\0' },
148 { "consumerd64-libdir", required_argument
, 0, '\0' },
149 { "daemonize", no_argument
, 0, 'd' },
150 { "background", no_argument
, 0, 'b' },
151 { "sig-parent", no_argument
, 0, 'S' },
152 { "help", no_argument
, 0, 'h' },
153 { "group", required_argument
, 0, 'g' },
154 { "version", no_argument
, 0, 'V' },
155 { "quiet", no_argument
, 0, 'q' },
156 { "verbose", no_argument
, 0, 'v' },
157 { "verbose-consumer", no_argument
, 0, '\0' },
158 { "no-kernel", no_argument
, 0, '\0' },
159 { "pidfile", required_argument
, 0, 'p' },
160 { "agent-tcp-port", required_argument
, 0, '\0' },
161 { "config", required_argument
, 0, 'f' },
162 { "load", required_argument
, 0, 'l' },
163 { "kmod-probes", required_argument
, 0, '\0' },
164 { "extra-kmod-probes", required_argument
, 0, '\0' },
168 /* Command line options to ignore from configuration file */
169 static const char *config_ignore_options
[] = { "help", "version", "config" };
171 /* Shared between threads */
172 static int dispatch_thread_exit
;
174 /* Global application Unix socket path */
175 static char apps_unix_sock_path
[PATH_MAX
];
176 /* Global client Unix socket path */
177 static char client_unix_sock_path
[PATH_MAX
];
178 /* global wait shm path for UST */
179 static char wait_shm_path
[PATH_MAX
];
180 /* Global health check unix path */
181 static char health_unix_sock_path
[PATH_MAX
];
183 /* Sockets and FDs */
184 static int client_sock
= -1;
185 static int apps_sock
= -1;
186 int kernel_tracer_fd
= -1;
187 static int kernel_poll_pipe
[2] = { -1, -1 };
190 * Quit pipe for all threads. This permits a single cancellation point
191 * for all threads when receiving an event on the pipe.
193 static int thread_quit_pipe
[2] = { -1, -1 };
194 static int ht_cleanup_quit_pipe
[2] = { -1, -1 };
197 * This pipe is used to inform the thread managing application communication
198 * that a command is queued and ready to be processed.
200 static int apps_cmd_pipe
[2] = { -1, -1 };
202 int apps_cmd_notify_pipe
[2] = { -1, -1 };
204 /* Pthread, Mutexes and Semaphores */
205 static pthread_t apps_thread
;
206 static pthread_t apps_notify_thread
;
207 static pthread_t reg_apps_thread
;
208 static pthread_t client_thread
;
209 static pthread_t kernel_thread
;
210 static pthread_t dispatch_thread
;
211 static pthread_t health_thread
;
212 static pthread_t ht_cleanup_thread
;
213 static pthread_t agent_reg_thread
;
214 static pthread_t load_session_thread
;
217 * UST registration command queue. This queue is tied with a futex and uses a N
218 * wakers / 1 waiter implemented and detailed in futex.c/.h
220 * The thread_registration_apps and thread_dispatch_ust_registration uses this
221 * queue along with the wait/wake scheme. The thread_manage_apps receives down
222 * the line new application socket and monitors it for any I/O error or clean
223 * close that triggers an unregistration of the application.
225 static struct ust_cmd_queue ust_cmd_queue
;
228 * Pointer initialized before thread creation.
230 * This points to the tracing session list containing the session count and a
231 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
232 * MUST NOT be taken if you call a public function in session.c.
234 * The lock is nested inside the structure: session_list_ptr->lock. Please use
235 * session_lock_list and session_unlock_list for lock acquisition.
237 static struct ltt_session_list
*session_list_ptr
;
239 int ust_consumerd64_fd
= -1;
240 int ust_consumerd32_fd
= -1;
242 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
243 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
244 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
245 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
246 static int consumerd32_bin_override
;
247 static int consumerd64_bin_override
;
248 static int consumerd32_libdir_override
;
249 static int consumerd64_libdir_override
;
251 static const char *module_proc_lttng
= "/proc/lttng";
254 * Consumer daemon state which is changed when spawning it, killing it or in
255 * case of a fatal error.
257 enum consumerd_state
{
258 CONSUMER_STARTED
= 1,
259 CONSUMER_STOPPED
= 2,
264 * This consumer daemon state is used to validate if a client command will be
265 * able to reach the consumer. If not, the client is informed. For instance,
266 * doing a "lttng start" when the consumer state is set to ERROR will return an
267 * error to the client.
269 * The following example shows a possible race condition of this scheme:
271 * consumer thread error happens
273 * client cmd checks state -> still OK
274 * consumer thread exit, sets error
275 * client cmd try to talk to consumer
278 * However, since the consumer is a different daemon, we have no way of making
279 * sure the command will reach it safely even with this state flag. This is why
280 * we consider that up to the state validation during command processing, the
281 * command is safe. After that, we can not guarantee the correctness of the
282 * client request vis-a-vis the consumer.
284 static enum consumerd_state ust_consumerd_state
;
285 static enum consumerd_state kernel_consumerd_state
;
288 * Socket timeout for receiving and sending in seconds.
290 static int app_socket_timeout
;
292 /* Set in main() with the current page size. */
295 /* Application health monitoring */
296 struct health_app
*health_sessiond
;
298 /* Agent TCP port for registration. Used by the agent thread. */
299 unsigned int agent_tcp_port
= DEFAULT_AGENT_TCP_PORT
;
301 /* Am I root or not. */
302 int is_root
; /* Set to 1 if the daemon is running as root */
304 const char * const config_section_name
= "sessiond";
306 /* Load session thread information to operate. */
307 struct load_session_thread_data
*load_info
;
309 /* Global hash tables */
310 struct lttng_ht
*agent_apps_ht_by_sock
= NULL
;
313 * Whether sessiond is ready for commands/health check requests.
314 * NR_LTTNG_SESSIOND_READY must match the number of calls to
315 * sessiond_notify_ready().
317 #define NR_LTTNG_SESSIOND_READY 3
318 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_READY
;
320 /* Notify parents that we are ready for cmd and health check */
322 void sessiond_notify_ready(void)
324 if (uatomic_sub_return(<tng_sessiond_ready
, 1) == 0) {
326 * Notify parent pid that we are ready to accept command
327 * for client side. This ppid is the one from the
328 * external process that spawned us.
330 if (opt_sig_parent
) {
335 * Notify the parent of the fork() process that we are
338 if (opt_daemon
|| opt_background
) {
339 kill(child_ppid
, SIGUSR1
);
345 void setup_consumerd_path(void)
347 const char *bin
, *libdir
;
350 * Allow INSTALL_BIN_PATH to be used as a target path for the
351 * native architecture size consumer if CONFIG_CONSUMER*_PATH
352 * has not been defined.
354 #if (CAA_BITS_PER_LONG == 32)
355 if (!consumerd32_bin
[0]) {
356 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
358 if (!consumerd32_libdir
[0]) {
359 consumerd32_libdir
= INSTALL_LIB_PATH
;
361 #elif (CAA_BITS_PER_LONG == 64)
362 if (!consumerd64_bin
[0]) {
363 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
365 if (!consumerd64_libdir
[0]) {
366 consumerd64_libdir
= INSTALL_LIB_PATH
;
369 #error "Unknown bitness"
373 * runtime env. var. overrides the build default.
375 bin
= lttng_secure_getenv("LTTNG_CONSUMERD32_BIN");
377 consumerd32_bin
= bin
;
379 bin
= lttng_secure_getenv("LTTNG_CONSUMERD64_BIN");
381 consumerd64_bin
= bin
;
383 libdir
= lttng_secure_getenv("LTTNG_CONSUMERD32_LIBDIR");
385 consumerd32_libdir
= libdir
;
387 libdir
= lttng_secure_getenv("LTTNG_CONSUMERD64_LIBDIR");
389 consumerd64_libdir
= libdir
;
394 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
401 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
407 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
419 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
421 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
423 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);
427 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
429 int sessiond_set_ht_cleanup_thread_pollset(struct lttng_poll_event
*events
,
432 return __sessiond_set_thread_pollset(events
, size
,
433 ht_cleanup_quit_pipe
);
437 int __sessiond_check_thread_quit_pipe(int fd
, uint32_t events
, int a_pipe
)
439 if (fd
== a_pipe
&& (events
& LPOLLIN
)) {
446 * Check if the thread quit pipe was triggered.
448 * Return 1 if it was triggered else 0;
450 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
452 return __sessiond_check_thread_quit_pipe(fd
, events
,
453 thread_quit_pipe
[0]);
457 * Check if the ht_cleanup thread quit pipe was triggered.
459 * Return 1 if it was triggered else 0;
461 int sessiond_check_ht_cleanup_quit(int fd
, uint32_t events
)
463 return __sessiond_check_thread_quit_pipe(fd
, events
,
464 ht_cleanup_quit_pipe
[0]);
468 * Init thread quit pipe.
470 * Return -1 on error or 0 if all pipes are created.
472 static int __init_thread_quit_pipe(int *a_pipe
)
478 PERROR("thread quit pipe");
482 for (i
= 0; i
< 2; i
++) {
483 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
494 static int init_thread_quit_pipe(void)
496 return __init_thread_quit_pipe(thread_quit_pipe
);
499 static int init_ht_cleanup_quit_pipe(void)
501 return __init_thread_quit_pipe(ht_cleanup_quit_pipe
);
505 * Stop all threads by closing the thread quit pipe.
507 static void stop_threads(void)
511 /* Stopping all threads */
512 DBG("Terminating all threads");
513 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
515 ERR("write error on thread quit pipe");
518 /* Dispatch thread */
519 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
520 futex_nto1_wake(&ust_cmd_queue
.futex
);
524 * Close every consumer sockets.
526 static void close_consumer_sockets(void)
530 if (kconsumer_data
.err_sock
>= 0) {
531 ret
= close(kconsumer_data
.err_sock
);
533 PERROR("kernel consumer err_sock close");
536 if (ustconsumer32_data
.err_sock
>= 0) {
537 ret
= close(ustconsumer32_data
.err_sock
);
539 PERROR("UST consumerd32 err_sock close");
542 if (ustconsumer64_data
.err_sock
>= 0) {
543 ret
= close(ustconsumer64_data
.err_sock
);
545 PERROR("UST consumerd64 err_sock close");
548 if (kconsumer_data
.cmd_sock
>= 0) {
549 ret
= close(kconsumer_data
.cmd_sock
);
551 PERROR("kernel consumer cmd_sock close");
554 if (ustconsumer32_data
.cmd_sock
>= 0) {
555 ret
= close(ustconsumer32_data
.cmd_sock
);
557 PERROR("UST consumerd32 cmd_sock close");
560 if (ustconsumer64_data
.cmd_sock
>= 0) {
561 ret
= close(ustconsumer64_data
.cmd_sock
);
563 PERROR("UST consumerd64 cmd_sock close");
569 * Generate the full lock file path using the rundir.
571 * Return the snprintf() return value thus a negative value is an error.
573 static int generate_lock_file_path(char *path
, size_t len
)
580 /* Build lockfile path from rundir. */
581 ret
= snprintf(path
, len
, "%s/" DEFAULT_LTTNG_SESSIOND_LOCKFILE
, rundir
);
583 PERROR("snprintf lockfile path");
590 * Wait on consumer process termination.
592 * Need to be called with the consumer data lock held or from a context
593 * ensuring no concurrent access to data (e.g: cleanup).
595 static void wait_consumer(struct consumer_data
*consumer_data
)
600 if (consumer_data
->pid
<= 0) {
604 DBG("Waiting for complete teardown of consumerd (PID: %d)",
606 ret
= waitpid(consumer_data
->pid
, &status
, 0);
608 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
610 if (!WIFEXITED(status
)) {
611 ERR("consumerd termination with error: %d",
614 consumer_data
->pid
= 0;
618 * Cleanup the session daemon's data structures.
620 static void sessiond_cleanup(void)
623 struct ltt_session
*sess
, *stmp
;
626 DBG("Cleanup sessiond");
629 * Close the thread quit pipe. It has already done its job,
630 * since we are now called.
632 utils_close_pipe(thread_quit_pipe
);
635 * If opt_pidfile is undefined, the default file will be wiped when
636 * removing the rundir.
639 ret
= remove(opt_pidfile
);
641 PERROR("remove pidfile %s", opt_pidfile
);
645 DBG("Removing sessiond and consumerd content of directory %s", rundir
);
648 snprintf(path
, PATH_MAX
,
650 rundir
, DEFAULT_LTTNG_SESSIOND_PIDFILE
);
651 DBG("Removing %s", path
);
654 snprintf(path
, PATH_MAX
, "%s/%s", rundir
,
655 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
);
656 DBG("Removing %s", path
);
660 snprintf(path
, PATH_MAX
,
661 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
663 DBG("Removing %s", path
);
666 snprintf(path
, PATH_MAX
,
667 DEFAULT_KCONSUMERD_PATH
,
669 DBG("Removing directory %s", path
);
672 /* ust consumerd 32 */
673 snprintf(path
, PATH_MAX
,
674 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
676 DBG("Removing %s", path
);
679 snprintf(path
, PATH_MAX
,
680 DEFAULT_USTCONSUMERD32_PATH
,
682 DBG("Removing directory %s", path
);
685 /* ust consumerd 64 */
686 snprintf(path
, PATH_MAX
,
687 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
689 DBG("Removing %s", path
);
692 snprintf(path
, PATH_MAX
,
693 DEFAULT_USTCONSUMERD64_PATH
,
695 DBG("Removing directory %s", path
);
698 DBG("Cleaning up all sessions");
700 /* Destroy session list mutex */
701 if (session_list_ptr
!= NULL
) {
702 pthread_mutex_destroy(&session_list_ptr
->lock
);
704 /* Cleanup ALL session */
705 cds_list_for_each_entry_safe(sess
, stmp
,
706 &session_list_ptr
->head
, list
) {
707 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
711 wait_consumer(&kconsumer_data
);
712 wait_consumer(&ustconsumer64_data
);
713 wait_consumer(&ustconsumer32_data
);
715 DBG("Cleaning up all agent apps");
716 agent_app_ht_clean();
718 DBG("Closing all UST sockets");
719 ust_app_clean_list();
720 buffer_reg_destroy_registries();
722 if (is_root
&& !opt_no_kernel
) {
723 DBG2("Closing kernel fd");
724 if (kernel_tracer_fd
>= 0) {
725 ret
= close(kernel_tracer_fd
);
730 DBG("Unloading kernel modules");
731 modprobe_remove_lttng_all();
735 close_consumer_sockets();
738 load_session_destroy_data(load_info
);
743 * Cleanup lock file by deleting it and finaly closing it which will
744 * release the file system lock.
746 if (lockfile_fd
>= 0) {
747 char lockfile_path
[PATH_MAX
];
749 ret
= generate_lock_file_path(lockfile_path
,
750 sizeof(lockfile_path
));
752 ret
= remove(lockfile_path
);
754 PERROR("remove lock file");
756 ret
= close(lockfile_fd
);
758 PERROR("close lock file");
764 * We do NOT rmdir rundir because there are other processes
765 * using it, for instance lttng-relayd, which can start in
766 * parallel with this teardown.
773 * Cleanup the daemon's option data structures.
775 static void sessiond_cleanup_options(void)
777 DBG("Cleaning up options");
780 * If the override option is set, the pointer points to a *non* const
781 * thus freeing it even though the variable type is set to const.
783 if (tracing_group_name_override
) {
784 free((void *) tracing_group_name
);
786 if (consumerd32_bin_override
) {
787 free((void *) consumerd32_bin
);
789 if (consumerd64_bin_override
) {
790 free((void *) consumerd64_bin
);
792 if (consumerd32_libdir_override
) {
793 free((void *) consumerd32_libdir
);
795 if (consumerd64_libdir_override
) {
796 free((void *) consumerd64_libdir
);
800 free(opt_load_session_path
);
801 free(kmod_probes_list
);
802 free(kmod_extra_probes_list
);
804 run_as_destroy_worker();
807 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
808 "Matthew, BEET driven development works!%c[%dm",
809 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
814 * Send data on a unix socket using the liblttsessiondcomm API.
816 * Return lttcomm error code.
818 static int send_unix_sock(int sock
, void *buf
, size_t len
)
820 /* Check valid length */
825 return lttcomm_send_unix_sock(sock
, buf
, len
);
829 * Free memory of a command context structure.
831 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
833 DBG("Clean command context structure");
835 if ((*cmd_ctx
)->llm
) {
836 free((*cmd_ctx
)->llm
);
838 if ((*cmd_ctx
)->lsm
) {
839 free((*cmd_ctx
)->lsm
);
847 * Notify UST applications using the shm mmap futex.
849 static int notify_ust_apps(int active
)
853 DBG("Notifying applications of session daemon state: %d", active
);
855 /* See shm.c for this call implying mmap, shm and futex calls */
856 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
857 if (wait_shm_mmap
== NULL
) {
861 /* Wake waiting process */
862 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
864 /* Apps notified successfully */
872 * Setup the outgoing data buffer for the response (llm) by allocating the
873 * right amount of memory and copying the original information from the lsm
876 * Return total size of the buffer pointed by buf.
878 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
884 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
885 if (cmd_ctx
->llm
== NULL
) {
891 /* Copy common data */
892 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
893 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
895 cmd_ctx
->llm
->data_size
= size
;
896 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
905 * Update the kernel poll set of all channel fd available over all tracing
906 * session. Add the wakeup pipe at the end of the set.
908 static int update_kernel_poll(struct lttng_poll_event
*events
)
911 struct ltt_session
*session
;
912 struct ltt_kernel_channel
*channel
;
914 DBG("Updating kernel poll set");
917 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
918 session_lock(session
);
919 if (session
->kernel_session
== NULL
) {
920 session_unlock(session
);
924 cds_list_for_each_entry(channel
,
925 &session
->kernel_session
->channel_list
.head
, list
) {
926 /* Add channel fd to the kernel poll set */
927 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
929 session_unlock(session
);
932 DBG("Channel fd %d added to kernel set", channel
->fd
);
934 session_unlock(session
);
936 session_unlock_list();
941 session_unlock_list();
946 * Find the channel fd from 'fd' over all tracing session. When found, check
947 * for new channel stream and send those stream fds to the kernel consumer.
949 * Useful for CPU hotplug feature.
951 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
954 struct ltt_session
*session
;
955 struct ltt_kernel_session
*ksess
;
956 struct ltt_kernel_channel
*channel
;
958 DBG("Updating kernel streams for channel fd %d", fd
);
961 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
962 session_lock(session
);
963 if (session
->kernel_session
== NULL
) {
964 session_unlock(session
);
967 ksess
= session
->kernel_session
;
969 cds_list_for_each_entry(channel
,
970 &ksess
->channel_list
.head
, list
) {
971 struct lttng_ht_iter iter
;
972 struct consumer_socket
*socket
;
974 if (channel
->fd
!= fd
) {
977 DBG("Channel found, updating kernel streams");
978 ret
= kernel_open_channel_stream(channel
);
982 /* Update the stream global counter */
983 ksess
->stream_count_global
+= ret
;
986 * Have we already sent fds to the consumer? If yes, it
987 * means that tracing is started so it is safe to send
988 * our updated stream fds.
990 if (ksess
->consumer_fds_sent
!= 1
991 || ksess
->consumer
== NULL
) {
997 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
998 &iter
.iter
, socket
, node
.node
) {
999 pthread_mutex_lock(socket
->lock
);
1000 ret
= kernel_consumer_send_channel_stream(socket
,
1002 session
->output_traces
? 1 : 0);
1003 pthread_mutex_unlock(socket
->lock
);
1011 session_unlock(session
);
1013 session_unlock_list();
1017 session_unlock(session
);
1018 session_unlock_list();
1023 * For each tracing session, update newly registered apps. The session list
1024 * lock MUST be acquired before calling this.
1026 static void update_ust_app(int app_sock
)
1028 struct ltt_session
*sess
, *stmp
;
1030 /* Consumer is in an ERROR state. Stop any application update. */
1031 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
1032 /* Stop the update process since the consumer is dead. */
1036 /* For all tracing session(s) */
1037 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
1038 struct ust_app
*app
;
1041 if (!sess
->ust_session
) {
1042 goto unlock_session
;
1046 assert(app_sock
>= 0);
1047 app
= ust_app_find_by_sock(app_sock
);
1050 * Application can be unregistered before so
1051 * this is possible hence simply stopping the
1054 DBG3("UST app update failed to find app sock %d",
1058 ust_app_global_update(sess
->ust_session
, app
);
1062 session_unlock(sess
);
1067 * This thread manage event coming from the kernel.
1069 * Features supported in this thread:
1072 static void *thread_manage_kernel(void *data
)
1074 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
1075 uint32_t revents
, nb_fd
;
1077 struct lttng_poll_event events
;
1079 DBG("[thread] Thread manage kernel started");
1081 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
1084 * This first step of the while is to clean this structure which could free
1085 * non NULL pointers so initialize it before the loop.
1087 lttng_poll_init(&events
);
1089 if (testpoint(sessiond_thread_manage_kernel
)) {
1090 goto error_testpoint
;
1093 health_code_update();
1095 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
1096 goto error_testpoint
;
1100 health_code_update();
1102 if (update_poll_flag
== 1) {
1103 /* Clean events object. We are about to populate it again. */
1104 lttng_poll_clean(&events
);
1106 ret
= sessiond_set_thread_pollset(&events
, 2);
1108 goto error_poll_create
;
1111 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
1116 /* This will add the available kernel channel if any. */
1117 ret
= update_kernel_poll(&events
);
1121 update_poll_flag
= 0;
1124 DBG("Thread kernel polling");
1126 /* Poll infinite value of time */
1128 health_poll_entry();
1129 ret
= lttng_poll_wait(&events
, -1);
1130 DBG("Thread kernel return from poll on %d fds",
1131 LTTNG_POLL_GETNB(&events
));
1135 * Restart interrupted system call.
1137 if (errno
== EINTR
) {
1141 } else if (ret
== 0) {
1142 /* Should not happen since timeout is infinite */
1143 ERR("Return value of poll is 0 with an infinite timeout.\n"
1144 "This should not have happened! Continuing...");
1150 for (i
= 0; i
< nb_fd
; i
++) {
1151 /* Fetch once the poll data */
1152 revents
= LTTNG_POLL_GETEV(&events
, i
);
1153 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1155 health_code_update();
1158 /* No activity for this FD (poll implementation). */
1162 /* Thread quit pipe has been closed. Killing thread. */
1163 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1169 /* Check for data on kernel pipe */
1170 if (revents
& LPOLLIN
) {
1171 if (pollfd
== kernel_poll_pipe
[0]) {
1172 (void) lttng_read(kernel_poll_pipe
[0],
1175 * Ret value is useless here, if this pipe gets any actions an
1176 * update is required anyway.
1178 update_poll_flag
= 1;
1182 * New CPU detected by the kernel. Adding kernel stream to
1183 * kernel session and updating the kernel consumer
1185 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1191 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1192 update_poll_flag
= 1;
1195 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1203 lttng_poll_clean(&events
);
1206 utils_close_pipe(kernel_poll_pipe
);
1207 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1210 ERR("Health error occurred in %s", __func__
);
1211 WARN("Kernel thread died unexpectedly. "
1212 "Kernel tracing can continue but CPU hotplug is disabled.");
1214 health_unregister(health_sessiond
);
1215 DBG("Kernel thread dying");
1220 * Signal pthread condition of the consumer data that the thread.
1222 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1224 pthread_mutex_lock(&data
->cond_mutex
);
1227 * The state is set before signaling. It can be any value, it's the waiter
1228 * job to correctly interpret this condition variable associated to the
1229 * consumer pthread_cond.
1231 * A value of 0 means that the corresponding thread of the consumer data
1232 * was not started. 1 indicates that the thread has started and is ready
1233 * for action. A negative value means that there was an error during the
1236 data
->consumer_thread_is_ready
= state
;
1237 (void) pthread_cond_signal(&data
->cond
);
1239 pthread_mutex_unlock(&data
->cond_mutex
);
1243 * This thread manage the consumer error sent back to the session daemon.
1245 static void *thread_manage_consumer(void *data
)
1247 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1248 uint32_t revents
, nb_fd
;
1249 enum lttcomm_return_code code
;
1250 struct lttng_poll_event events
;
1251 struct consumer_data
*consumer_data
= data
;
1253 DBG("[thread] Manage consumer started");
1255 rcu_register_thread();
1256 rcu_thread_online();
1258 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1260 health_code_update();
1263 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1264 * metadata_sock. Nothing more will be added to this poll set.
1266 ret
= sessiond_set_thread_pollset(&events
, 3);
1272 * The error socket here is already in a listening state which was done
1273 * just before spawning this thread to avoid a race between the consumer
1274 * daemon exec trying to connect and the listen() call.
1276 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1281 health_code_update();
1283 /* Infinite blocking call, waiting for transmission */
1285 health_poll_entry();
1287 if (testpoint(sessiond_thread_manage_consumer
)) {
1291 ret
= lttng_poll_wait(&events
, -1);
1295 * Restart interrupted system call.
1297 if (errno
== EINTR
) {
1305 for (i
= 0; i
< nb_fd
; i
++) {
1306 /* Fetch once the poll data */
1307 revents
= LTTNG_POLL_GETEV(&events
, i
);
1308 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1310 health_code_update();
1313 /* No activity for this FD (poll implementation). */
1317 /* Thread quit pipe has been closed. Killing thread. */
1318 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1324 /* Event on the registration socket */
1325 if (pollfd
== consumer_data
->err_sock
) {
1326 if (revents
& LPOLLIN
) {
1328 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1329 ERR("consumer err socket poll error");
1332 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1338 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1344 * Set the CLOEXEC flag. Return code is useless because either way, the
1347 (void) utils_set_fd_cloexec(sock
);
1349 health_code_update();
1351 DBG2("Receiving code from consumer err_sock");
1353 /* Getting status code from kconsumerd */
1354 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1355 sizeof(enum lttcomm_return_code
));
1360 health_code_update();
1361 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1362 /* Connect both socket, command and metadata. */
1363 consumer_data
->cmd_sock
=
1364 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1365 consumer_data
->metadata_fd
=
1366 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1367 if (consumer_data
->cmd_sock
< 0
1368 || consumer_data
->metadata_fd
< 0) {
1369 PERROR("consumer connect cmd socket");
1370 /* On error, signal condition and quit. */
1371 signal_consumer_condition(consumer_data
, -1);
1374 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1375 /* Create metadata socket lock. */
1376 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1377 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1378 PERROR("zmalloc pthread mutex");
1382 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1384 signal_consumer_condition(consumer_data
, 1);
1385 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1386 DBG("Consumer metadata socket ready (fd: %d)",
1387 consumer_data
->metadata_fd
);
1389 ERR("consumer error when waiting for SOCK_READY : %s",
1390 lttcomm_get_readable_code(-code
));
1394 /* Remove the consumerd error sock since we've established a connexion */
1395 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1400 /* Add new accepted error socket. */
1401 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1406 /* Add metadata socket that is successfully connected. */
1407 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1408 LPOLLIN
| LPOLLRDHUP
);
1413 health_code_update();
1415 /* Infinite blocking call, waiting for transmission */
1418 health_code_update();
1420 /* Exit the thread because the thread quit pipe has been triggered. */
1422 /* Not a health error. */
1427 health_poll_entry();
1428 ret
= lttng_poll_wait(&events
, -1);
1432 * Restart interrupted system call.
1434 if (errno
== EINTR
) {
1442 for (i
= 0; i
< nb_fd
; i
++) {
1443 /* Fetch once the poll data */
1444 revents
= LTTNG_POLL_GETEV(&events
, i
);
1445 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1447 health_code_update();
1450 /* No activity for this FD (poll implementation). */
1455 * Thread quit pipe has been triggered, flag that we should stop
1456 * but continue the current loop to handle potential data from
1459 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1461 if (pollfd
== sock
) {
1462 /* Event on the consumerd socket */
1463 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1464 && !(revents
& LPOLLIN
)) {
1465 ERR("consumer err socket second poll error");
1468 health_code_update();
1469 /* Wait for any kconsumerd error */
1470 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1471 sizeof(enum lttcomm_return_code
));
1473 ERR("consumer closed the command socket");
1477 ERR("consumer return code : %s",
1478 lttcomm_get_readable_code(-code
));
1481 } else if (pollfd
== consumer_data
->metadata_fd
) {
1482 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1483 && !(revents
& LPOLLIN
)) {
1484 ERR("consumer err metadata socket second poll error");
1487 /* UST metadata requests */
1488 ret
= ust_consumer_metadata_request(
1489 &consumer_data
->metadata_sock
);
1491 ERR("Handling metadata request");
1495 /* No need for an else branch all FDs are tested prior. */
1497 health_code_update();
1503 * We lock here because we are about to close the sockets and some other
1504 * thread might be using them so get exclusive access which will abort all
1505 * other consumer command by other threads.
1507 pthread_mutex_lock(&consumer_data
->lock
);
1509 /* Immediately set the consumerd state to stopped */
1510 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1511 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1512 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1513 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1514 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1516 /* Code flow error... */
1520 if (consumer_data
->err_sock
>= 0) {
1521 ret
= close(consumer_data
->err_sock
);
1525 consumer_data
->err_sock
= -1;
1527 if (consumer_data
->cmd_sock
>= 0) {
1528 ret
= close(consumer_data
->cmd_sock
);
1532 consumer_data
->cmd_sock
= -1;
1534 if (consumer_data
->metadata_sock
.fd_ptr
&&
1535 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1536 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1548 unlink(consumer_data
->err_unix_sock_path
);
1549 unlink(consumer_data
->cmd_unix_sock_path
);
1550 pthread_mutex_unlock(&consumer_data
->lock
);
1552 /* Cleanup metadata socket mutex. */
1553 if (consumer_data
->metadata_sock
.lock
) {
1554 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1555 free(consumer_data
->metadata_sock
.lock
);
1557 lttng_poll_clean(&events
);
1561 ERR("Health error occurred in %s", __func__
);
1563 health_unregister(health_sessiond
);
1564 DBG("consumer thread cleanup completed");
1566 rcu_thread_offline();
1567 rcu_unregister_thread();
1573 * This thread manage application communication.
1575 static void *thread_manage_apps(void *data
)
1577 int i
, ret
, pollfd
, err
= -1;
1579 uint32_t revents
, nb_fd
;
1580 struct lttng_poll_event events
;
1582 DBG("[thread] Manage application started");
1584 rcu_register_thread();
1585 rcu_thread_online();
1587 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1589 if (testpoint(sessiond_thread_manage_apps
)) {
1590 goto error_testpoint
;
1593 health_code_update();
1595 ret
= sessiond_set_thread_pollset(&events
, 2);
1597 goto error_poll_create
;
1600 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1605 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1609 health_code_update();
1612 DBG("Apps thread polling");
1614 /* Inifinite blocking call, waiting for transmission */
1616 health_poll_entry();
1617 ret
= lttng_poll_wait(&events
, -1);
1618 DBG("Apps thread return from poll on %d fds",
1619 LTTNG_POLL_GETNB(&events
));
1623 * Restart interrupted system call.
1625 if (errno
== EINTR
) {
1633 for (i
= 0; i
< nb_fd
; i
++) {
1634 /* Fetch once the poll data */
1635 revents
= LTTNG_POLL_GETEV(&events
, i
);
1636 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1638 health_code_update();
1641 /* No activity for this FD (poll implementation). */
1645 /* Thread quit pipe has been closed. Killing thread. */
1646 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1652 /* Inspect the apps cmd pipe */
1653 if (pollfd
== apps_cmd_pipe
[0]) {
1654 if (revents
& LPOLLIN
) {
1658 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1659 if (size_ret
< sizeof(sock
)) {
1660 PERROR("read apps cmd pipe");
1664 health_code_update();
1667 * Since this is a command socket (write then read),
1668 * we only monitor the error events of the socket.
1670 ret
= lttng_poll_add(&events
, sock
,
1671 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1676 DBG("Apps with sock %d added to poll set", sock
);
1677 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1678 ERR("Apps command pipe error");
1681 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1686 * At this point, we know that a registered application made
1687 * the event at poll_wait.
1689 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1690 /* Removing from the poll set */
1691 ret
= lttng_poll_del(&events
, pollfd
);
1696 /* Socket closed on remote end. */
1697 ust_app_unregister(pollfd
);
1699 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1704 health_code_update();
1710 lttng_poll_clean(&events
);
1713 utils_close_pipe(apps_cmd_pipe
);
1714 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1717 * We don't clean the UST app hash table here since already registered
1718 * applications can still be controlled so let them be until the session
1719 * daemon dies or the applications stop.
1724 ERR("Health error occurred in %s", __func__
);
1726 health_unregister(health_sessiond
);
1727 DBG("Application communication apps thread cleanup complete");
1728 rcu_thread_offline();
1729 rcu_unregister_thread();
1734 * Send a socket to a thread This is called from the dispatch UST registration
1735 * thread once all sockets are set for the application.
1737 * The sock value can be invalid, we don't really care, the thread will handle
1738 * it and make the necessary cleanup if so.
1740 * On success, return 0 else a negative value being the errno message of the
1743 static int send_socket_to_thread(int fd
, int sock
)
1748 * It's possible that the FD is set as invalid with -1 concurrently just
1749 * before calling this function being a shutdown state of the thread.
1756 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1757 if (ret
< sizeof(sock
)) {
1758 PERROR("write apps pipe %d", fd
);
1765 /* All good. Don't send back the write positive ret value. */
1772 * Sanitize the wait queue of the dispatch registration thread meaning removing
1773 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1774 * notify socket is never received.
1776 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1778 int ret
, nb_fd
= 0, i
;
1779 unsigned int fd_added
= 0;
1780 struct lttng_poll_event events
;
1781 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1785 lttng_poll_init(&events
);
1787 /* Just skip everything for an empty queue. */
1788 if (!wait_queue
->count
) {
1792 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1797 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1798 &wait_queue
->head
, head
) {
1799 assert(wait_node
->app
);
1800 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1801 LPOLLHUP
| LPOLLERR
);
1814 * Poll but don't block so we can quickly identify the faulty events and
1815 * clean them afterwards from the wait queue.
1817 ret
= lttng_poll_wait(&events
, 0);
1823 for (i
= 0; i
< nb_fd
; i
++) {
1824 /* Get faulty FD. */
1825 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1826 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1829 /* No activity for this FD (poll implementation). */
1833 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1834 &wait_queue
->head
, head
) {
1835 if (pollfd
== wait_node
->app
->sock
&&
1836 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1837 cds_list_del(&wait_node
->head
);
1838 wait_queue
->count
--;
1839 ust_app_destroy(wait_node
->app
);
1842 * Silence warning of use-after-free in
1843 * cds_list_for_each_entry_safe which uses
1844 * __typeof__(*wait_node).
1849 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1856 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1860 lttng_poll_clean(&events
);
1864 lttng_poll_clean(&events
);
1866 ERR("Unable to sanitize wait queue");
1871 * Dispatch request from the registration threads to the application
1872 * communication thread.
1874 static void *thread_dispatch_ust_registration(void *data
)
1877 struct cds_wfcq_node
*node
;
1878 struct ust_command
*ust_cmd
= NULL
;
1879 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1880 struct ust_reg_wait_queue wait_queue
= {
1884 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1886 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1887 goto error_testpoint
;
1890 health_code_update();
1892 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1894 DBG("[thread] Dispatch UST command started");
1896 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1897 health_code_update();
1899 /* Atomically prepare the queue futex */
1900 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1903 struct ust_app
*app
= NULL
;
1907 * Make sure we don't have node(s) that have hung up before receiving
1908 * the notify socket. This is to clean the list in order to avoid
1909 * memory leaks from notify socket that are never seen.
1911 sanitize_wait_queue(&wait_queue
);
1913 health_code_update();
1914 /* Dequeue command for registration */
1915 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1917 DBG("Woken up but nothing in the UST command queue");
1918 /* Continue thread execution */
1922 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1924 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1925 " gid:%d sock:%d name:%s (version %d.%d)",
1926 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1927 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1928 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1929 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1931 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1932 wait_node
= zmalloc(sizeof(*wait_node
));
1934 PERROR("zmalloc wait_node dispatch");
1935 ret
= close(ust_cmd
->sock
);
1937 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1939 lttng_fd_put(LTTNG_FD_APPS
, 1);
1943 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1945 /* Create application object if socket is CMD. */
1946 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1948 if (!wait_node
->app
) {
1949 ret
= close(ust_cmd
->sock
);
1951 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1953 lttng_fd_put(LTTNG_FD_APPS
, 1);
1959 * Add application to the wait queue so we can set the notify
1960 * socket before putting this object in the global ht.
1962 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1967 * We have to continue here since we don't have the notify
1968 * socket and the application MUST be added to the hash table
1969 * only at that moment.
1974 * Look for the application in the local wait queue and set the
1975 * notify socket if found.
1977 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1978 &wait_queue
.head
, head
) {
1979 health_code_update();
1980 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1981 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1982 cds_list_del(&wait_node
->head
);
1984 app
= wait_node
->app
;
1986 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1992 * With no application at this stage the received socket is
1993 * basically useless so close it before we free the cmd data
1994 * structure for good.
1997 ret
= close(ust_cmd
->sock
);
1999 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
2001 lttng_fd_put(LTTNG_FD_APPS
, 1);
2008 * @session_lock_list
2010 * Lock the global session list so from the register up to the
2011 * registration done message, no thread can see the application
2012 * and change its state.
2014 session_lock_list();
2018 * Add application to the global hash table. This needs to be
2019 * done before the update to the UST registry can locate the
2024 /* Set app version. This call will print an error if needed. */
2025 (void) ust_app_version(app
);
2027 /* Send notify socket through the notify pipe. */
2028 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
2032 session_unlock_list();
2034 * No notify thread, stop the UST tracing. However, this is
2035 * not an internal error of the this thread thus setting
2036 * the health error code to a normal exit.
2043 * Update newly registered application with the tracing
2044 * registry info already enabled information.
2046 update_ust_app(app
->sock
);
2049 * Don't care about return value. Let the manage apps threads
2050 * handle app unregistration upon socket close.
2052 (void) ust_app_register_done(app
);
2055 * Even if the application socket has been closed, send the app
2056 * to the thread and unregistration will take place at that
2059 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
2062 session_unlock_list();
2064 * No apps. thread, stop the UST tracing. However, this is
2065 * not an internal error of the this thread thus setting
2066 * the health error code to a normal exit.
2073 session_unlock_list();
2075 } while (node
!= NULL
);
2077 health_poll_entry();
2078 /* Futex wait on queue. Blocking call on futex() */
2079 futex_nto1_wait(&ust_cmd_queue
.futex
);
2082 /* Normal exit, no error */
2086 /* Clean up wait queue. */
2087 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2088 &wait_queue
.head
, head
) {
2089 cds_list_del(&wait_node
->head
);
2094 /* Empty command queue. */
2096 /* Dequeue command for registration */
2097 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2101 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2102 ret
= close(ust_cmd
->sock
);
2104 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2106 lttng_fd_put(LTTNG_FD_APPS
, 1);
2111 DBG("Dispatch thread dying");
2114 ERR("Health error occurred in %s", __func__
);
2116 health_unregister(health_sessiond
);
2121 * This thread manage application registration.
2123 static void *thread_registration_apps(void *data
)
2125 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2126 uint32_t revents
, nb_fd
;
2127 struct lttng_poll_event events
;
2129 * Get allocated in this thread, enqueued to a global queue, dequeued and
2130 * freed in the manage apps thread.
2132 struct ust_command
*ust_cmd
= NULL
;
2134 DBG("[thread] Manage application registration started");
2136 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2138 if (testpoint(sessiond_thread_registration_apps
)) {
2139 goto error_testpoint
;
2142 ret
= lttcomm_listen_unix_sock(apps_sock
);
2148 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2149 * more will be added to this poll set.
2151 ret
= sessiond_set_thread_pollset(&events
, 2);
2153 goto error_create_poll
;
2156 /* Add the application registration socket */
2157 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2159 goto error_poll_add
;
2162 /* Notify all applications to register */
2163 ret
= notify_ust_apps(1);
2165 ERR("Failed to notify applications or create the wait shared memory.\n"
2166 "Execution continues but there might be problem for already\n"
2167 "running applications that wishes to register.");
2171 DBG("Accepting application registration");
2173 /* Inifinite blocking call, waiting for transmission */
2175 health_poll_entry();
2176 ret
= lttng_poll_wait(&events
, -1);
2180 * Restart interrupted system call.
2182 if (errno
== EINTR
) {
2190 for (i
= 0; i
< nb_fd
; i
++) {
2191 health_code_update();
2193 /* Fetch once the poll data */
2194 revents
= LTTNG_POLL_GETEV(&events
, i
);
2195 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2198 /* No activity for this FD (poll implementation). */
2202 /* Thread quit pipe has been closed. Killing thread. */
2203 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2209 /* Event on the registration socket */
2210 if (pollfd
== apps_sock
) {
2211 if (revents
& LPOLLIN
) {
2212 sock
= lttcomm_accept_unix_sock(apps_sock
);
2218 * Set socket timeout for both receiving and ending.
2219 * app_socket_timeout is in seconds, whereas
2220 * lttcomm_setsockopt_rcv_timeout and
2221 * lttcomm_setsockopt_snd_timeout expect msec as
2224 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2225 app_socket_timeout
* 1000);
2226 (void) lttcomm_setsockopt_snd_timeout(sock
,
2227 app_socket_timeout
* 1000);
2230 * Set the CLOEXEC flag. Return code is useless because
2231 * either way, the show must go on.
2233 (void) utils_set_fd_cloexec(sock
);
2235 /* Create UST registration command for enqueuing */
2236 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2237 if (ust_cmd
== NULL
) {
2238 PERROR("ust command zmalloc");
2247 * Using message-based transmissions to ensure we don't
2248 * have to deal with partially received messages.
2250 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2252 ERR("Exhausted file descriptors allowed for applications.");
2262 health_code_update();
2263 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2266 /* Close socket of the application. */
2271 lttng_fd_put(LTTNG_FD_APPS
, 1);
2275 health_code_update();
2277 ust_cmd
->sock
= sock
;
2280 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2281 " gid:%d sock:%d name:%s (version %d.%d)",
2282 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2283 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2284 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2285 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2288 * Lock free enqueue the registration request. The red pill
2289 * has been taken! This apps will be part of the *system*.
2291 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2294 * Wake the registration queue futex. Implicit memory
2295 * barrier with the exchange in cds_wfcq_enqueue.
2297 futex_nto1_wake(&ust_cmd_queue
.futex
);
2298 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2299 ERR("Register apps socket poll error");
2302 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2311 /* Notify that the registration thread is gone */
2314 if (apps_sock
>= 0) {
2315 ret
= close(apps_sock
);
2325 lttng_fd_put(LTTNG_FD_APPS
, 1);
2327 unlink(apps_unix_sock_path
);
2330 lttng_poll_clean(&events
);
2334 DBG("UST Registration thread cleanup complete");
2337 ERR("Health error occurred in %s", __func__
);
2339 health_unregister(health_sessiond
);
2345 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2346 * exec or it will fails.
2348 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2351 struct timespec timeout
;
2353 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2354 consumer_data
->consumer_thread_is_ready
= 0;
2356 /* Setup pthread condition */
2357 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2360 PERROR("pthread_condattr_init consumer data");
2365 * Set the monotonic clock in order to make sure we DO NOT jump in time
2366 * between the clock_gettime() call and the timedwait call. See bug #324
2367 * for a more details and how we noticed it.
2369 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2372 PERROR("pthread_condattr_setclock consumer data");
2376 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2379 PERROR("pthread_cond_init consumer data");
2383 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
2387 PERROR("pthread_create consumer");
2392 /* We are about to wait on a pthread condition */
2393 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2395 /* Get time for sem_timedwait absolute timeout */
2396 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2398 * Set the timeout for the condition timed wait even if the clock gettime
2399 * call fails since we might loop on that call and we want to avoid to
2400 * increment the timeout too many times.
2402 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2405 * The following loop COULD be skipped in some conditions so this is why we
2406 * set ret to 0 in order to make sure at least one round of the loop is
2412 * Loop until the condition is reached or when a timeout is reached. Note
2413 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2414 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2415 * possible. This loop does not take any chances and works with both of
2418 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2419 if (clock_ret
< 0) {
2420 PERROR("clock_gettime spawn consumer");
2421 /* Infinite wait for the consumerd thread to be ready */
2422 ret
= pthread_cond_wait(&consumer_data
->cond
,
2423 &consumer_data
->cond_mutex
);
2425 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2426 &consumer_data
->cond_mutex
, &timeout
);
2430 /* Release the pthread condition */
2431 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2435 if (ret
== ETIMEDOUT
) {
2439 * Call has timed out so we kill the kconsumerd_thread and return
2442 ERR("Condition timed out. The consumer thread was never ready."
2444 pth_ret
= pthread_cancel(consumer_data
->thread
);
2446 PERROR("pthread_cancel consumer thread");
2449 PERROR("pthread_cond_wait failed consumer thread");
2451 /* Caller is expecting a negative value on failure. */
2456 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2457 if (consumer_data
->pid
== 0) {
2458 ERR("Consumerd did not start");
2459 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2462 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2471 * Join consumer thread
2473 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2477 /* Consumer pid must be a real one. */
2478 if (consumer_data
->pid
> 0) {
2480 ret
= kill(consumer_data
->pid
, SIGTERM
);
2482 PERROR("Error killing consumer daemon");
2485 return pthread_join(consumer_data
->thread
, &status
);
2492 * Fork and exec a consumer daemon (consumerd).
2494 * Return pid if successful else -1.
2496 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2500 const char *consumer_to_use
;
2501 const char *verbosity
;
2504 DBG("Spawning consumerd");
2511 if (opt_verbose_consumer
) {
2512 verbosity
= "--verbose";
2513 } else if (lttng_opt_quiet
) {
2514 verbosity
= "--quiet";
2519 switch (consumer_data
->type
) {
2520 case LTTNG_CONSUMER_KERNEL
:
2522 * Find out which consumerd to execute. We will first try the
2523 * 64-bit path, then the sessiond's installation directory, and
2524 * fallback on the 32-bit one,
2526 DBG3("Looking for a kernel consumer at these locations:");
2527 DBG3(" 1) %s", consumerd64_bin
);
2528 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2529 DBG3(" 3) %s", consumerd32_bin
);
2530 if (stat(consumerd64_bin
, &st
) == 0) {
2531 DBG3("Found location #1");
2532 consumer_to_use
= consumerd64_bin
;
2533 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2534 DBG3("Found location #2");
2535 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2536 } else if (stat(consumerd32_bin
, &st
) == 0) {
2537 DBG3("Found location #3");
2538 consumer_to_use
= consumerd32_bin
;
2540 DBG("Could not find any valid consumerd executable");
2544 DBG("Using kernel consumer at: %s", consumer_to_use
);
2545 ret
= execl(consumer_to_use
,
2546 "lttng-consumerd", verbosity
, "-k",
2547 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2548 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2549 "--group", tracing_group_name
,
2552 case LTTNG_CONSUMER64_UST
:
2554 char *tmpnew
= NULL
;
2556 if (consumerd64_libdir
[0] != '\0') {
2560 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2564 tmplen
= strlen("LD_LIBRARY_PATH=")
2565 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2566 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2571 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2572 strcat(tmpnew
, consumerd64_libdir
);
2573 if (tmp
[0] != '\0') {
2574 strcat(tmpnew
, ":");
2575 strcat(tmpnew
, tmp
);
2577 ret
= putenv(tmpnew
);
2584 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2585 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2586 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2587 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2588 "--group", tracing_group_name
,
2590 if (consumerd64_libdir
[0] != '\0') {
2595 case LTTNG_CONSUMER32_UST
:
2597 char *tmpnew
= NULL
;
2599 if (consumerd32_libdir
[0] != '\0') {
2603 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2607 tmplen
= strlen("LD_LIBRARY_PATH=")
2608 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2609 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2614 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2615 strcat(tmpnew
, consumerd32_libdir
);
2616 if (tmp
[0] != '\0') {
2617 strcat(tmpnew
, ":");
2618 strcat(tmpnew
, tmp
);
2620 ret
= putenv(tmpnew
);
2627 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2628 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2629 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2630 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2631 "--group", tracing_group_name
,
2633 if (consumerd32_libdir
[0] != '\0') {
2639 PERROR("unknown consumer type");
2643 PERROR("Consumer execl()");
2645 /* Reaching this point, we got a failure on our execl(). */
2647 } else if (pid
> 0) {
2650 PERROR("start consumer fork");
2658 * Spawn the consumerd daemon and session daemon thread.
2660 static int start_consumerd(struct consumer_data
*consumer_data
)
2665 * Set the listen() state on the socket since there is a possible race
2666 * between the exec() of the consumer daemon and this call if place in the
2667 * consumer thread. See bug #366 for more details.
2669 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2674 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2675 if (consumer_data
->pid
!= 0) {
2676 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2680 ret
= spawn_consumerd(consumer_data
);
2682 ERR("Spawning consumerd failed");
2683 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2687 /* Setting up the consumer_data pid */
2688 consumer_data
->pid
= ret
;
2689 DBG2("Consumer pid %d", consumer_data
->pid
);
2690 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2692 DBG2("Spawning consumer control thread");
2693 ret
= spawn_consumer_thread(consumer_data
);
2695 ERR("Fatal error spawning consumer control thread");
2703 /* Cleanup already created sockets on error. */
2704 if (consumer_data
->err_sock
>= 0) {
2707 err
= close(consumer_data
->err_sock
);
2709 PERROR("close consumer data error socket");
2716 * Setup necessary data for kernel tracer action.
2718 static int init_kernel_tracer(void)
2722 /* Modprobe lttng kernel modules */
2723 ret
= modprobe_lttng_control();
2728 /* Open debugfs lttng */
2729 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2730 if (kernel_tracer_fd
< 0) {
2731 DBG("Failed to open %s", module_proc_lttng
);
2736 /* Validate kernel version */
2737 ret
= kernel_validate_version(kernel_tracer_fd
);
2742 ret
= modprobe_lttng_data();
2747 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2751 modprobe_remove_lttng_control();
2752 ret
= close(kernel_tracer_fd
);
2756 kernel_tracer_fd
= -1;
2757 return LTTNG_ERR_KERN_VERSION
;
2760 ret
= close(kernel_tracer_fd
);
2766 modprobe_remove_lttng_control();
2769 WARN("No kernel tracer available");
2770 kernel_tracer_fd
= -1;
2772 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2774 return LTTNG_ERR_KERN_NA
;
2780 * Copy consumer output from the tracing session to the domain session. The
2781 * function also applies the right modification on a per domain basis for the
2782 * trace files destination directory.
2784 * Should *NOT* be called with RCU read-side lock held.
2786 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2789 const char *dir_name
;
2790 struct consumer_output
*consumer
;
2793 assert(session
->consumer
);
2796 case LTTNG_DOMAIN_KERNEL
:
2797 DBG3("Copying tracing session consumer output in kernel session");
2799 * XXX: We should audit the session creation and what this function
2800 * does "extra" in order to avoid a destroy since this function is used
2801 * in the domain session creation (kernel and ust) only. Same for UST
2804 if (session
->kernel_session
->consumer
) {
2805 consumer_output_put(session
->kernel_session
->consumer
);
2807 session
->kernel_session
->consumer
=
2808 consumer_copy_output(session
->consumer
);
2809 /* Ease our life a bit for the next part */
2810 consumer
= session
->kernel_session
->consumer
;
2811 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2813 case LTTNG_DOMAIN_JUL
:
2814 case LTTNG_DOMAIN_LOG4J
:
2815 case LTTNG_DOMAIN_PYTHON
:
2816 case LTTNG_DOMAIN_UST
:
2817 DBG3("Copying tracing session consumer output in UST session");
2818 if (session
->ust_session
->consumer
) {
2819 consumer_output_put(session
->ust_session
->consumer
);
2821 session
->ust_session
->consumer
=
2822 consumer_copy_output(session
->consumer
);
2823 /* Ease our life a bit for the next part */
2824 consumer
= session
->ust_session
->consumer
;
2825 dir_name
= DEFAULT_UST_TRACE_DIR
;
2828 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2832 /* Append correct directory to subdir */
2833 strncat(consumer
->subdir
, dir_name
,
2834 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2835 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2844 * Create an UST session and add it to the session ust list.
2846 * Should *NOT* be called with RCU read-side lock held.
2848 static int create_ust_session(struct ltt_session
*session
,
2849 struct lttng_domain
*domain
)
2852 struct ltt_ust_session
*lus
= NULL
;
2856 assert(session
->consumer
);
2858 switch (domain
->type
) {
2859 case LTTNG_DOMAIN_JUL
:
2860 case LTTNG_DOMAIN_LOG4J
:
2861 case LTTNG_DOMAIN_PYTHON
:
2862 case LTTNG_DOMAIN_UST
:
2865 ERR("Unknown UST domain on create session %d", domain
->type
);
2866 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2870 DBG("Creating UST session");
2872 lus
= trace_ust_create_session(session
->id
);
2874 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2878 lus
->uid
= session
->uid
;
2879 lus
->gid
= session
->gid
;
2880 lus
->output_traces
= session
->output_traces
;
2881 lus
->snapshot_mode
= session
->snapshot_mode
;
2882 lus
->live_timer_interval
= session
->live_timer
;
2883 session
->ust_session
= lus
;
2884 if (session
->shm_path
[0]) {
2885 strncpy(lus
->root_shm_path
, session
->shm_path
,
2886 sizeof(lus
->root_shm_path
));
2887 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2888 strncpy(lus
->shm_path
, session
->shm_path
,
2889 sizeof(lus
->shm_path
));
2890 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2891 strncat(lus
->shm_path
, "/ust",
2892 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2894 /* Copy session output to the newly created UST session */
2895 ret
= copy_session_consumer(domain
->type
, session
);
2896 if (ret
!= LTTNG_OK
) {
2904 session
->ust_session
= NULL
;
2909 * Create a kernel tracer session then create the default channel.
2911 static int create_kernel_session(struct ltt_session
*session
)
2915 DBG("Creating kernel session");
2917 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2919 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2923 /* Code flow safety */
2924 assert(session
->kernel_session
);
2926 /* Copy session output to the newly created Kernel session */
2927 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2928 if (ret
!= LTTNG_OK
) {
2932 /* Create directory(ies) on local filesystem. */
2933 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2934 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2935 ret
= run_as_mkdir_recursive(
2936 session
->kernel_session
->consumer
->dst
.trace_path
,
2937 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2939 if (errno
!= EEXIST
) {
2940 ERR("Trace directory creation error");
2946 session
->kernel_session
->uid
= session
->uid
;
2947 session
->kernel_session
->gid
= session
->gid
;
2948 session
->kernel_session
->output_traces
= session
->output_traces
;
2949 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2954 trace_kernel_destroy_session(session
->kernel_session
);
2955 session
->kernel_session
= NULL
;
2960 * Count number of session permitted by uid/gid.
2962 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2965 struct ltt_session
*session
;
2967 DBG("Counting number of available session for UID %d GID %d",
2969 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2971 * Only list the sessions the user can control.
2973 if (!session_access_ok(session
, uid
, gid
)) {
2982 * Process the command requested by the lttng client within the command
2983 * context structure. This function make sure that the return structure (llm)
2984 * is set and ready for transmission before returning.
2986 * Return any error encountered or 0 for success.
2988 * "sock" is only used for special-case var. len data.
2990 * Should *NOT* be called with RCU read-side lock held.
2992 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2996 int need_tracing_session
= 1;
2999 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
3001 assert(!rcu_read_ongoing());
3005 switch (cmd_ctx
->lsm
->cmd_type
) {
3006 case LTTNG_CREATE_SESSION
:
3007 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3008 case LTTNG_CREATE_SESSION_LIVE
:
3009 case LTTNG_DESTROY_SESSION
:
3010 case LTTNG_LIST_SESSIONS
:
3011 case LTTNG_LIST_DOMAINS
:
3012 case LTTNG_START_TRACE
:
3013 case LTTNG_STOP_TRACE
:
3014 case LTTNG_DATA_PENDING
:
3015 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3016 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3017 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3018 case LTTNG_SNAPSHOT_RECORD
:
3019 case LTTNG_SAVE_SESSION
:
3020 case LTTNG_SET_SESSION_SHM_PATH
:
3027 if (opt_no_kernel
&& need_domain
3028 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
3030 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3032 ret
= LTTNG_ERR_KERN_NA
;
3037 /* Deny register consumer if we already have a spawned consumer. */
3038 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
3039 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3040 if (kconsumer_data
.pid
> 0) {
3041 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3042 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3045 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3049 * Check for command that don't needs to allocate a returned payload. We do
3050 * this here so we don't have to make the call for no payload at each
3053 switch(cmd_ctx
->lsm
->cmd_type
) {
3054 case LTTNG_LIST_SESSIONS
:
3055 case LTTNG_LIST_TRACEPOINTS
:
3056 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3057 case LTTNG_LIST_DOMAINS
:
3058 case LTTNG_LIST_CHANNELS
:
3059 case LTTNG_LIST_EVENTS
:
3060 case LTTNG_LIST_SYSCALLS
:
3061 case LTTNG_LIST_TRACKER_PIDS
:
3064 /* Setup lttng message with no payload */
3065 ret
= setup_lttng_msg(cmd_ctx
, 0);
3067 /* This label does not try to unlock the session */
3068 goto init_setup_error
;
3072 /* Commands that DO NOT need a session. */
3073 switch (cmd_ctx
->lsm
->cmd_type
) {
3074 case LTTNG_CREATE_SESSION
:
3075 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3076 case LTTNG_CREATE_SESSION_LIVE
:
3077 case LTTNG_CALIBRATE
:
3078 case LTTNG_LIST_SESSIONS
:
3079 case LTTNG_LIST_TRACEPOINTS
:
3080 case LTTNG_LIST_SYSCALLS
:
3081 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3082 case LTTNG_SAVE_SESSION
:
3083 need_tracing_session
= 0;
3086 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3088 * We keep the session list lock across _all_ commands
3089 * for now, because the per-session lock does not
3090 * handle teardown properly.
3092 session_lock_list();
3093 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3094 if (cmd_ctx
->session
== NULL
) {
3095 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3098 /* Acquire lock for the session */
3099 session_lock(cmd_ctx
->session
);
3105 * Commands that need a valid session but should NOT create one if none
3106 * exists. Instead of creating one and destroying it when the command is
3107 * handled, process that right before so we save some round trip in useless
3110 switch (cmd_ctx
->lsm
->cmd_type
) {
3111 case LTTNG_DISABLE_CHANNEL
:
3112 case LTTNG_DISABLE_EVENT
:
3113 switch (cmd_ctx
->lsm
->domain
.type
) {
3114 case LTTNG_DOMAIN_KERNEL
:
3115 if (!cmd_ctx
->session
->kernel_session
) {
3116 ret
= LTTNG_ERR_NO_CHANNEL
;
3120 case LTTNG_DOMAIN_JUL
:
3121 case LTTNG_DOMAIN_LOG4J
:
3122 case LTTNG_DOMAIN_PYTHON
:
3123 case LTTNG_DOMAIN_UST
:
3124 if (!cmd_ctx
->session
->ust_session
) {
3125 ret
= LTTNG_ERR_NO_CHANNEL
;
3130 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3142 * Check domain type for specific "pre-action".
3144 switch (cmd_ctx
->lsm
->domain
.type
) {
3145 case LTTNG_DOMAIN_KERNEL
:
3147 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3151 /* Kernel tracer check */
3152 if (kernel_tracer_fd
== -1) {
3153 /* Basically, load kernel tracer modules */
3154 ret
= init_kernel_tracer();
3160 /* Consumer is in an ERROR state. Report back to client */
3161 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3162 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3166 /* Need a session for kernel command */
3167 if (need_tracing_session
) {
3168 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3169 ret
= create_kernel_session(cmd_ctx
->session
);
3171 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3176 /* Start the kernel consumer daemon */
3177 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3178 if (kconsumer_data
.pid
== 0 &&
3179 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3180 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3181 ret
= start_consumerd(&kconsumer_data
);
3183 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3186 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3188 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3192 * The consumer was just spawned so we need to add the socket to
3193 * the consumer output of the session if exist.
3195 ret
= consumer_create_socket(&kconsumer_data
,
3196 cmd_ctx
->session
->kernel_session
->consumer
);
3203 case LTTNG_DOMAIN_JUL
:
3204 case LTTNG_DOMAIN_LOG4J
:
3205 case LTTNG_DOMAIN_PYTHON
:
3206 case LTTNG_DOMAIN_UST
:
3208 if (!ust_app_supported()) {
3209 ret
= LTTNG_ERR_NO_UST
;
3212 /* Consumer is in an ERROR state. Report back to client */
3213 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3214 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3218 if (need_tracing_session
) {
3219 /* Create UST session if none exist. */
3220 if (cmd_ctx
->session
->ust_session
== NULL
) {
3221 ret
= create_ust_session(cmd_ctx
->session
,
3222 &cmd_ctx
->lsm
->domain
);
3223 if (ret
!= LTTNG_OK
) {
3228 /* Start the UST consumer daemons */
3230 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3231 if (consumerd64_bin
[0] != '\0' &&
3232 ustconsumer64_data
.pid
== 0 &&
3233 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3234 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3235 ret
= start_consumerd(&ustconsumer64_data
);
3237 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3238 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3242 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3243 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3245 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3249 * Setup socket for consumer 64 bit. No need for atomic access
3250 * since it was set above and can ONLY be set in this thread.
3252 ret
= consumer_create_socket(&ustconsumer64_data
,
3253 cmd_ctx
->session
->ust_session
->consumer
);
3259 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3260 if (consumerd32_bin
[0] != '\0' &&
3261 ustconsumer32_data
.pid
== 0 &&
3262 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3263 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3264 ret
= start_consumerd(&ustconsumer32_data
);
3266 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3267 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3271 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3272 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3274 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3278 * Setup socket for consumer 64 bit. No need for atomic access
3279 * since it was set above and can ONLY be set in this thread.
3281 ret
= consumer_create_socket(&ustconsumer32_data
,
3282 cmd_ctx
->session
->ust_session
->consumer
);
3294 /* Validate consumer daemon state when start/stop trace command */
3295 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3296 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3297 switch (cmd_ctx
->lsm
->domain
.type
) {
3298 case LTTNG_DOMAIN_JUL
:
3299 case LTTNG_DOMAIN_LOG4J
:
3300 case LTTNG_DOMAIN_PYTHON
:
3301 case LTTNG_DOMAIN_UST
:
3302 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3303 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3307 case LTTNG_DOMAIN_KERNEL
:
3308 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3309 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3317 * Check that the UID or GID match that of the tracing session.
3318 * The root user can interact with all sessions.
3320 if (need_tracing_session
) {
3321 if (!session_access_ok(cmd_ctx
->session
,
3322 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3323 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3324 ret
= LTTNG_ERR_EPERM
;
3330 * Send relayd information to consumer as soon as we have a domain and a
3333 if (cmd_ctx
->session
&& need_domain
) {
3335 * Setup relayd if not done yet. If the relayd information was already
3336 * sent to the consumer, this call will gracefully return.
3338 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3339 if (ret
!= LTTNG_OK
) {
3344 /* Process by command type */
3345 switch (cmd_ctx
->lsm
->cmd_type
) {
3346 case LTTNG_ADD_CONTEXT
:
3348 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3349 cmd_ctx
->lsm
->u
.context
.channel_name
,
3350 &cmd_ctx
->lsm
->u
.context
.ctx
, kernel_poll_pipe
[1]);
3353 case LTTNG_DISABLE_CHANNEL
:
3355 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3356 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3359 case LTTNG_DISABLE_EVENT
:
3363 * FIXME: handle filter; for now we just receive the filter's
3364 * bytecode along with the filter expression which are sent by
3365 * liblttng-ctl and discard them.
3367 * This fixes an issue where the client may block while sending
3368 * the filter payload and encounter an error because the session
3369 * daemon closes the socket without ever handling this data.
3371 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3372 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3375 char data
[LTTNG_FILTER_MAX_LEN
];
3377 DBG("Discarding disable event command payload of size %zu", count
);
3379 ret
= lttcomm_recv_unix_sock(sock
, data
,
3380 count
> sizeof(data
) ? sizeof(data
) : count
);
3385 count
-= (size_t) ret
;
3388 /* FIXME: passing packed structure to non-packed pointer */
3389 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3390 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3391 &cmd_ctx
->lsm
->u
.disable
.event
);
3394 case LTTNG_ENABLE_CHANNEL
:
3396 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3397 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
3400 case LTTNG_TRACK_PID
:
3402 ret
= cmd_track_pid(cmd_ctx
->session
,
3403 cmd_ctx
->lsm
->domain
.type
,
3404 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3407 case LTTNG_UNTRACK_PID
:
3409 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3410 cmd_ctx
->lsm
->domain
.type
,
3411 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3414 case LTTNG_ENABLE_EVENT
:
3416 struct lttng_event_exclusion
*exclusion
= NULL
;
3417 struct lttng_filter_bytecode
*bytecode
= NULL
;
3418 char *filter_expression
= NULL
;
3420 /* Handle exclusion events and receive it from the client. */
3421 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3422 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3424 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3425 (count
* LTTNG_SYMBOL_NAME_LEN
));
3427 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3431 DBG("Receiving var len exclusion event list from client ...");
3432 exclusion
->count
= count
;
3433 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3434 count
* LTTNG_SYMBOL_NAME_LEN
);
3436 DBG("Nothing recv() from client var len data... continuing");
3439 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3444 /* Get filter expression from client. */
3445 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3446 size_t expression_len
=
3447 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3449 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3450 ret
= LTTNG_ERR_FILTER_INVAL
;
3455 filter_expression
= zmalloc(expression_len
);
3456 if (!filter_expression
) {
3458 ret
= LTTNG_ERR_FILTER_NOMEM
;
3462 /* Receive var. len. data */
3463 DBG("Receiving var len filter's expression from client ...");
3464 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3467 DBG("Nothing recv() from client car len data... continuing");
3469 free(filter_expression
);
3471 ret
= LTTNG_ERR_FILTER_INVAL
;
3476 /* Handle filter and get bytecode from client. */
3477 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3478 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3480 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3481 ret
= LTTNG_ERR_FILTER_INVAL
;
3482 free(filter_expression
);
3487 bytecode
= zmalloc(bytecode_len
);
3489 free(filter_expression
);
3491 ret
= LTTNG_ERR_FILTER_NOMEM
;
3495 /* Receive var. len. data */
3496 DBG("Receiving var len filter's bytecode from client ...");
3497 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3499 DBG("Nothing recv() from client car len data... continuing");
3501 free(filter_expression
);
3504 ret
= LTTNG_ERR_FILTER_INVAL
;
3508 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3509 free(filter_expression
);
3512 ret
= LTTNG_ERR_FILTER_INVAL
;
3517 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3518 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3519 &cmd_ctx
->lsm
->u
.enable
.event
,
3520 filter_expression
, bytecode
, exclusion
,
3521 kernel_poll_pipe
[1]);
3524 case LTTNG_LIST_TRACEPOINTS
:
3526 struct lttng_event
*events
;
3529 session_lock_list();
3530 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3531 session_unlock_list();
3532 if (nb_events
< 0) {
3533 /* Return value is a negative lttng_error_code. */
3539 * Setup lttng message with payload size set to the event list size in
3540 * bytes and then copy list into the llm payload.
3542 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
3548 /* Copy event list into message payload */
3549 memcpy(cmd_ctx
->llm
->payload
, events
,
3550 sizeof(struct lttng_event
) * nb_events
);
3557 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3559 struct lttng_event_field
*fields
;
3562 session_lock_list();
3563 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3565 session_unlock_list();
3566 if (nb_fields
< 0) {
3567 /* Return value is a negative lttng_error_code. */
3573 * Setup lttng message with payload size set to the event list size in
3574 * bytes and then copy list into the llm payload.
3576 ret
= setup_lttng_msg(cmd_ctx
,
3577 sizeof(struct lttng_event_field
) * nb_fields
);
3583 /* Copy event list into message payload */
3584 memcpy(cmd_ctx
->llm
->payload
, fields
,
3585 sizeof(struct lttng_event_field
) * nb_fields
);
3592 case LTTNG_LIST_SYSCALLS
:
3594 struct lttng_event
*events
;
3597 nb_events
= cmd_list_syscalls(&events
);
3598 if (nb_events
< 0) {
3599 /* Return value is a negative lttng_error_code. */
3605 * Setup lttng message with payload size set to the event list size in
3606 * bytes and then copy list into the llm payload.
3608 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
3614 /* Copy event list into message payload */
3615 memcpy(cmd_ctx
->llm
->payload
, events
,
3616 sizeof(struct lttng_event
) * nb_events
);
3623 case LTTNG_LIST_TRACKER_PIDS
:
3625 int32_t *pids
= NULL
;
3628 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3629 cmd_ctx
->lsm
->domain
.type
, &pids
);
3631 /* Return value is a negative lttng_error_code. */
3637 * Setup lttng message with payload size set to the event list size in
3638 * bytes and then copy list into the llm payload.
3640 ret
= setup_lttng_msg(cmd_ctx
, sizeof(int32_t) * nr_pids
);
3646 /* Copy event list into message payload */
3647 memcpy(cmd_ctx
->llm
->payload
, pids
,
3648 sizeof(int) * nr_pids
);
3655 case LTTNG_SET_CONSUMER_URI
:
3658 struct lttng_uri
*uris
;
3660 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3661 len
= nb_uri
* sizeof(struct lttng_uri
);
3664 ret
= LTTNG_ERR_INVALID
;
3668 uris
= zmalloc(len
);
3670 ret
= LTTNG_ERR_FATAL
;
3674 /* Receive variable len data */
3675 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3676 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3678 DBG("No URIs received from client... continuing");
3680 ret
= LTTNG_ERR_SESSION_FAIL
;
3685 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3687 if (ret
!= LTTNG_OK
) {
3694 case LTTNG_START_TRACE
:
3696 ret
= cmd_start_trace(cmd_ctx
->session
);
3699 case LTTNG_STOP_TRACE
:
3701 ret
= cmd_stop_trace(cmd_ctx
->session
);
3704 case LTTNG_CREATE_SESSION
:
3707 struct lttng_uri
*uris
= NULL
;
3709 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3710 len
= nb_uri
* sizeof(struct lttng_uri
);
3713 uris
= zmalloc(len
);
3715 ret
= LTTNG_ERR_FATAL
;
3719 /* Receive variable len data */
3720 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3721 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3723 DBG("No URIs received from client... continuing");
3725 ret
= LTTNG_ERR_SESSION_FAIL
;
3730 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3731 DBG("Creating session with ONE network URI is a bad call");
3732 ret
= LTTNG_ERR_SESSION_FAIL
;
3738 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3739 &cmd_ctx
->creds
, 0);
3745 case LTTNG_DESTROY_SESSION
:
3747 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3749 /* Set session to NULL so we do not unlock it after free. */
3750 cmd_ctx
->session
= NULL
;
3753 case LTTNG_LIST_DOMAINS
:
3756 struct lttng_domain
*domains
= NULL
;
3758 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3760 /* Return value is a negative lttng_error_code. */
3765 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
3771 /* Copy event list into message payload */
3772 memcpy(cmd_ctx
->llm
->payload
, domains
,
3773 nb_dom
* sizeof(struct lttng_domain
));
3780 case LTTNG_LIST_CHANNELS
:
3783 struct lttng_channel
*channels
= NULL
;
3785 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3786 cmd_ctx
->session
, &channels
);
3788 /* Return value is a negative lttng_error_code. */
3793 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
3799 /* Copy event list into message payload */
3800 memcpy(cmd_ctx
->llm
->payload
, channels
,
3801 nb_chan
* sizeof(struct lttng_channel
));
3808 case LTTNG_LIST_EVENTS
:
3811 struct lttng_event
*events
= NULL
;
3813 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3814 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
3816 /* Return value is a negative lttng_error_code. */
3821 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
3827 /* Copy event list into message payload */
3828 memcpy(cmd_ctx
->llm
->payload
, events
,
3829 nb_event
* sizeof(struct lttng_event
));
3836 case LTTNG_LIST_SESSIONS
:
3838 unsigned int nr_sessions
;
3840 session_lock_list();
3841 nr_sessions
= lttng_sessions_count(
3842 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3843 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3845 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
3847 session_unlock_list();
3851 /* Filled the session array */
3852 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
3853 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3854 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3856 session_unlock_list();
3861 case LTTNG_CALIBRATE
:
3863 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3864 &cmd_ctx
->lsm
->u
.calibrate
);
3867 case LTTNG_REGISTER_CONSUMER
:
3869 struct consumer_data
*cdata
;
3871 switch (cmd_ctx
->lsm
->domain
.type
) {
3872 case LTTNG_DOMAIN_KERNEL
:
3873 cdata
= &kconsumer_data
;
3876 ret
= LTTNG_ERR_UND
;
3880 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3881 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3884 case LTTNG_DATA_PENDING
:
3888 /* 1 byte to return whether or not data is pending */
3889 ret
= setup_lttng_msg(cmd_ctx
, 1);
3894 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
3898 * This function may returns 0 or 1 to indicate whether or not
3899 * there is data pending. In case of error, it should return an
3900 * LTTNG_ERR code. However, some code paths may still return
3901 * a nondescript error code, which we handle by returning an
3904 if (pending_ret
== 0 || pending_ret
== 1) {
3906 } else if (pending_ret
< 0) {
3907 ret
= LTTNG_ERR_UNK
;
3914 *cmd_ctx
->llm
->payload
= (uint8_t) pending_ret
;
3917 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3919 struct lttcomm_lttng_output_id reply
;
3921 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3922 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3923 if (ret
!= LTTNG_OK
) {
3927 ret
= setup_lttng_msg(cmd_ctx
, sizeof(reply
));
3932 /* Copy output list into message payload */
3933 memcpy(cmd_ctx
->llm
->payload
, &reply
, sizeof(reply
));
3937 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3939 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3940 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3943 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3946 struct lttng_snapshot_output
*outputs
= NULL
;
3948 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3949 if (nb_output
< 0) {
3954 ret
= setup_lttng_msg(cmd_ctx
,
3955 nb_output
* sizeof(struct lttng_snapshot_output
));
3962 /* Copy output list into message payload */
3963 memcpy(cmd_ctx
->llm
->payload
, outputs
,
3964 nb_output
* sizeof(struct lttng_snapshot_output
));
3971 case LTTNG_SNAPSHOT_RECORD
:
3973 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3974 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3975 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3978 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3981 struct lttng_uri
*uris
= NULL
;
3983 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3984 len
= nb_uri
* sizeof(struct lttng_uri
);
3987 uris
= zmalloc(len
);
3989 ret
= LTTNG_ERR_FATAL
;
3993 /* Receive variable len data */
3994 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3995 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3997 DBG("No URIs received from client... continuing");
3999 ret
= LTTNG_ERR_SESSION_FAIL
;
4004 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4005 DBG("Creating session with ONE network URI is a bad call");
4006 ret
= LTTNG_ERR_SESSION_FAIL
;
4012 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
4013 nb_uri
, &cmd_ctx
->creds
);
4017 case LTTNG_CREATE_SESSION_LIVE
:
4020 struct lttng_uri
*uris
= NULL
;
4022 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4023 len
= nb_uri
* sizeof(struct lttng_uri
);
4026 uris
= zmalloc(len
);
4028 ret
= LTTNG_ERR_FATAL
;
4032 /* Receive variable len data */
4033 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4034 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4036 DBG("No URIs received from client... continuing");
4038 ret
= LTTNG_ERR_SESSION_FAIL
;
4043 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4044 DBG("Creating session with ONE network URI is a bad call");
4045 ret
= LTTNG_ERR_SESSION_FAIL
;
4051 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4052 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4056 case LTTNG_SAVE_SESSION
:
4058 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4062 case LTTNG_SET_SESSION_SHM_PATH
:
4064 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4065 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4069 ret
= LTTNG_ERR_UND
;
4074 if (cmd_ctx
->llm
== NULL
) {
4075 DBG("Missing llm structure. Allocating one.");
4076 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
4080 /* Set return code */
4081 cmd_ctx
->llm
->ret_code
= ret
;
4083 if (cmd_ctx
->session
) {
4084 session_unlock(cmd_ctx
->session
);
4086 if (need_tracing_session
) {
4087 session_unlock_list();
4090 assert(!rcu_read_ongoing());
4095 * Thread managing health check socket.
4097 static void *thread_manage_health(void *data
)
4099 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4100 uint32_t revents
, nb_fd
;
4101 struct lttng_poll_event events
;
4102 struct health_comm_msg msg
;
4103 struct health_comm_reply reply
;
4105 DBG("[thread] Manage health check started");
4107 rcu_register_thread();
4109 /* We might hit an error path before this is created. */
4110 lttng_poll_init(&events
);
4112 /* Create unix socket */
4113 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
4115 ERR("Unable to create health check Unix socket");
4121 /* lttng health client socket path permissions */
4122 ret
= chown(health_unix_sock_path
, 0,
4123 utils_get_group_id(tracing_group_name
));
4125 ERR("Unable to set group on %s", health_unix_sock_path
);
4131 ret
= chmod(health_unix_sock_path
,
4132 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4134 ERR("Unable to set permissions on %s", health_unix_sock_path
);
4142 * Set the CLOEXEC flag. Return code is useless because either way, the
4145 (void) utils_set_fd_cloexec(sock
);
4147 ret
= lttcomm_listen_unix_sock(sock
);
4153 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4154 * more will be added to this poll set.
4156 ret
= sessiond_set_thread_pollset(&events
, 2);
4161 /* Add the application registration socket */
4162 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4167 sessiond_notify_ready();
4170 DBG("Health check ready");
4172 /* Inifinite blocking call, waiting for transmission */
4174 ret
= lttng_poll_wait(&events
, -1);
4177 * Restart interrupted system call.
4179 if (errno
== EINTR
) {
4187 for (i
= 0; i
< nb_fd
; i
++) {
4188 /* Fetch once the poll data */
4189 revents
= LTTNG_POLL_GETEV(&events
, i
);
4190 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4193 /* No activity for this FD (poll implementation). */
4197 /* Thread quit pipe has been closed. Killing thread. */
4198 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4204 /* Event on the registration socket */
4205 if (pollfd
== sock
) {
4206 if (revents
& LPOLLIN
) {
4208 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4209 ERR("Health socket poll error");
4212 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4218 new_sock
= lttcomm_accept_unix_sock(sock
);
4224 * Set the CLOEXEC flag. Return code is useless because either way, the
4227 (void) utils_set_fd_cloexec(new_sock
);
4229 DBG("Receiving data from client for health...");
4230 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4232 DBG("Nothing recv() from client... continuing");
4233 ret
= close(new_sock
);
4241 rcu_thread_online();
4243 memset(&reply
, 0, sizeof(reply
));
4244 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4246 * health_check_state returns 0 if health is
4249 if (!health_check_state(health_sessiond
, i
)) {
4250 reply
.ret_code
|= 1ULL << i
;
4254 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4256 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4258 ERR("Failed to send health data back to client");
4261 /* End of transmission */
4262 ret
= close(new_sock
);
4272 ERR("Health error occurred in %s", __func__
);
4274 DBG("Health check thread dying");
4275 unlink(health_unix_sock_path
);
4283 lttng_poll_clean(&events
);
4285 rcu_unregister_thread();
4290 * This thread manage all clients request using the unix client socket for
4293 static void *thread_manage_clients(void *data
)
4295 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4297 uint32_t revents
, nb_fd
;
4298 struct command_ctx
*cmd_ctx
= NULL
;
4299 struct lttng_poll_event events
;
4301 DBG("[thread] Manage client started");
4303 rcu_register_thread();
4305 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4307 health_code_update();
4309 ret
= lttcomm_listen_unix_sock(client_sock
);
4315 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4316 * more will be added to this poll set.
4318 ret
= sessiond_set_thread_pollset(&events
, 2);
4320 goto error_create_poll
;
4323 /* Add the application registration socket */
4324 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4329 sessiond_notify_ready();
4330 ret
= sem_post(&load_info
->message_thread_ready
);
4332 PERROR("sem_post message_thread_ready");
4336 /* This testpoint is after we signal readiness to the parent. */
4337 if (testpoint(sessiond_thread_manage_clients
)) {
4341 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4345 health_code_update();
4348 DBG("Accepting client command ...");
4350 /* Inifinite blocking call, waiting for transmission */
4352 health_poll_entry();
4353 ret
= lttng_poll_wait(&events
, -1);
4357 * Restart interrupted system call.
4359 if (errno
== EINTR
) {
4367 for (i
= 0; i
< nb_fd
; i
++) {
4368 /* Fetch once the poll data */
4369 revents
= LTTNG_POLL_GETEV(&events
, i
);
4370 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4372 health_code_update();
4375 /* No activity for this FD (poll implementation). */
4379 /* Thread quit pipe has been closed. Killing thread. */
4380 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4386 /* Event on the registration socket */
4387 if (pollfd
== client_sock
) {
4388 if (revents
& LPOLLIN
) {
4390 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4391 ERR("Client socket poll error");
4394 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4400 DBG("Wait for client response");
4402 health_code_update();
4404 sock
= lttcomm_accept_unix_sock(client_sock
);
4410 * Set the CLOEXEC flag. Return code is useless because either way, the
4413 (void) utils_set_fd_cloexec(sock
);
4415 /* Set socket option for credentials retrieval */
4416 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4421 /* Allocate context command to process the client request */
4422 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4423 if (cmd_ctx
== NULL
) {
4424 PERROR("zmalloc cmd_ctx");
4428 /* Allocate data buffer for reception */
4429 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4430 if (cmd_ctx
->lsm
== NULL
) {
4431 PERROR("zmalloc cmd_ctx->lsm");
4435 cmd_ctx
->llm
= NULL
;
4436 cmd_ctx
->session
= NULL
;
4438 health_code_update();
4441 * Data is received from the lttng client. The struct
4442 * lttcomm_session_msg (lsm) contains the command and data request of
4445 DBG("Receiving data from client ...");
4446 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4447 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4449 DBG("Nothing recv() from client... continuing");
4455 clean_command_ctx(&cmd_ctx
);
4459 health_code_update();
4461 // TODO: Validate cmd_ctx including sanity check for
4462 // security purpose.
4464 rcu_thread_online();
4466 * This function dispatch the work to the kernel or userspace tracer
4467 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4468 * informations for the client. The command context struct contains
4469 * everything this function may needs.
4471 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4472 rcu_thread_offline();
4480 * TODO: Inform client somehow of the fatal error. At
4481 * this point, ret < 0 means that a zmalloc failed
4482 * (ENOMEM). Error detected but still accept
4483 * command, unless a socket error has been
4486 clean_command_ctx(&cmd_ctx
);
4490 health_code_update();
4492 DBG("Sending response (size: %d, retcode: %s (%d))",
4493 cmd_ctx
->lttng_msg_size
,
4494 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4495 cmd_ctx
->llm
->ret_code
);
4496 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4498 ERR("Failed to send data back to client");
4501 /* End of transmission */
4508 clean_command_ctx(&cmd_ctx
);
4510 health_code_update();
4522 lttng_poll_clean(&events
);
4523 clean_command_ctx(&cmd_ctx
);
4527 unlink(client_unix_sock_path
);
4528 if (client_sock
>= 0) {
4529 ret
= close(client_sock
);
4537 ERR("Health error occurred in %s", __func__
);
4540 health_unregister(health_sessiond
);
4542 DBG("Client thread dying");
4544 rcu_unregister_thread();
4547 * Since we are creating the consumer threads, we own them, so we need
4548 * to join them before our thread exits.
4550 ret
= join_consumer_thread(&kconsumer_data
);
4553 PERROR("join_consumer");
4556 ret
= join_consumer_thread(&ustconsumer32_data
);
4559 PERROR("join_consumer ust32");
4562 ret
= join_consumer_thread(&ustconsumer64_data
);
4565 PERROR("join_consumer ust64");
4572 * usage function on stderr
4574 static void usage(void)
4576 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
4577 fprintf(stderr
, " -h, --help Display this usage.\n");
4578 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
4579 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
4580 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
4581 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
4582 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
4583 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
4584 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
4585 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
4586 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
4587 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
4588 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
4589 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
4590 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
4591 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
4592 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
4593 fprintf(stderr
, " -V, --version Show version number.\n");
4594 fprintf(stderr
, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n");
4595 fprintf(stderr
, " -q, --quiet No output at all.\n");
4596 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
4597 fprintf(stderr
, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
4598 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4599 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
4600 fprintf(stderr
, " --agent-tcp-port Agent registration TCP port\n");
4601 fprintf(stderr
, " -f --config PATH Load daemon configuration file\n");
4602 fprintf(stderr
, " -l --load PATH Load session configuration\n");
4603 fprintf(stderr
, " --kmod-probes Specify kernel module probes to load\n");
4604 fprintf(stderr
, " --extra-kmod-probes Specify extra kernel module probes to load\n");
4607 static int string_match(const char *str1
, const char *str2
)
4609 return (str1
&& str2
) && !strcmp(str1
, str2
);
4613 * Take an option from the getopt output and set it in the right variable to be
4616 * Return 0 on success else a negative value.
4618 static int set_option(int opt
, const char *arg
, const char *optname
)
4622 if (arg
&& arg
[0] == '\0') {
4624 * This only happens if the value is read from daemon config
4625 * file. This means the option requires an argument and the
4626 * configuration file contains a line such as:
4633 if (string_match(optname
, "client-sock") || opt
== 'c') {
4634 if (lttng_is_setuid_setgid()) {
4635 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4636 "-c, --client-sock");
4638 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", arg
);
4640 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4641 if (lttng_is_setuid_setgid()) {
4642 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4645 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", arg
);
4647 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4649 } else if (string_match(optname
, "background") || opt
== 'b') {
4651 } else if (string_match(optname
, "group") || opt
== 'g') {
4652 if (lttng_is_setuid_setgid()) {
4653 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4657 * If the override option is set, the pointer points to a
4658 * *non* const thus freeing it even though the variable type is
4661 if (tracing_group_name_override
) {
4662 free((void *) tracing_group_name
);
4664 tracing_group_name
= strdup(arg
);
4665 if (!tracing_group_name
) {
4669 tracing_group_name_override
= 1;
4671 } else if (string_match(optname
, "help") || opt
== 'h') {
4674 } else if (string_match(optname
, "version") || opt
== 'V') {
4675 fprintf(stdout
, "%s\n", VERSION
);
4677 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4679 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4680 if (lttng_is_setuid_setgid()) {
4681 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4682 "--kconsumerd-err-sock");
4684 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4686 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4687 if (lttng_is_setuid_setgid()) {
4688 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4689 "--kconsumerd-cmd-sock");
4691 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4693 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4694 if (lttng_is_setuid_setgid()) {
4695 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4696 "--ustconsumerd64-err-sock");
4698 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4700 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4701 if (lttng_is_setuid_setgid()) {
4702 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4703 "--ustconsumerd64-cmd-sock");
4705 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4707 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4708 if (lttng_is_setuid_setgid()) {
4709 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4710 "--ustconsumerd32-err-sock");
4712 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4714 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4715 if (lttng_is_setuid_setgid()) {
4716 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4717 "--ustconsumerd32-cmd-sock");
4719 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4721 } else if (string_match(optname
, "no-kernel")) {
4723 } else if (string_match(optname
, "quiet") || opt
== 'q') {
4724 lttng_opt_quiet
= 1;
4725 } else if (string_match(optname
, "verbose") || opt
== 'v') {
4726 /* Verbose level can increase using multiple -v */
4728 /* Value obtained from config file */
4729 lttng_opt_verbose
= config_parse_value(arg
);
4731 /* -v used on command line */
4732 lttng_opt_verbose
++;
4734 /* Clamp value to [0, 3] */
4735 lttng_opt_verbose
= lttng_opt_verbose
< 0 ? 0 :
4736 (lttng_opt_verbose
<= 3 ? lttng_opt_verbose
: 3);
4737 } else if (string_match(optname
, "verbose-consumer")) {
4739 opt_verbose_consumer
= config_parse_value(arg
);
4741 opt_verbose_consumer
+= 1;
4743 } else if (string_match(optname
, "consumerd32-path")) {
4744 if (lttng_is_setuid_setgid()) {
4745 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4746 "--consumerd32-path");
4748 if (consumerd32_bin_override
) {
4749 free((void *) consumerd32_bin
);
4751 consumerd32_bin
= strdup(arg
);
4752 if (!consumerd32_bin
) {
4756 consumerd32_bin_override
= 1;
4758 } else if (string_match(optname
, "consumerd32-libdir")) {
4759 if (lttng_is_setuid_setgid()) {
4760 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4761 "--consumerd32-libdir");
4763 if (consumerd32_libdir_override
) {
4764 free((void *) consumerd32_libdir
);
4766 consumerd32_libdir
= strdup(arg
);
4767 if (!consumerd32_libdir
) {
4771 consumerd32_libdir_override
= 1;
4773 } else if (string_match(optname
, "consumerd64-path")) {
4774 if (lttng_is_setuid_setgid()) {
4775 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4776 "--consumerd64-path");
4778 if (consumerd64_bin_override
) {
4779 free((void *) consumerd64_bin
);
4781 consumerd64_bin
= strdup(arg
);
4782 if (!consumerd64_bin
) {
4786 consumerd64_bin_override
= 1;
4788 } else if (string_match(optname
, "consumerd64-libdir")) {
4789 if (lttng_is_setuid_setgid()) {
4790 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4791 "--consumerd64-libdir");
4793 if (consumerd64_libdir_override
) {
4794 free((void *) consumerd64_libdir
);
4796 consumerd64_libdir
= strdup(arg
);
4797 if (!consumerd64_libdir
) {
4801 consumerd64_libdir_override
= 1;
4803 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
4804 if (lttng_is_setuid_setgid()) {
4805 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4809 opt_pidfile
= strdup(arg
);
4815 } else if (string_match(optname
, "agent-tcp-port")) {
4816 if (lttng_is_setuid_setgid()) {
4817 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4818 "--agent-tcp-port");
4827 v
= strtoul(arg
, NULL
, 0);
4828 if (errno
!= 0 || !isdigit(arg
[0])) {
4829 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
4832 if (v
== 0 || v
>= 65535) {
4833 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
4836 agent_tcp_port
= (uint32_t) v
;
4837 DBG3("Agent TCP port set to non default: %u", agent_tcp_port
);
4839 } else if (string_match(optname
, "load") || opt
== 'l') {
4840 if (lttng_is_setuid_setgid()) {
4841 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4844 free(opt_load_session_path
);
4845 opt_load_session_path
= strdup(arg
);
4846 if (!opt_load_session_path
) {
4851 } else if (string_match(optname
, "kmod-probes")) {
4852 if (lttng_is_setuid_setgid()) {
4853 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4856 free(kmod_probes_list
);
4857 kmod_probes_list
= strdup(arg
);
4858 if (!kmod_probes_list
) {
4863 } else if (string_match(optname
, "extra-kmod-probes")) {
4864 if (lttng_is_setuid_setgid()) {
4865 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4866 "--extra-kmod-probes");
4868 free(kmod_extra_probes_list
);
4869 kmod_extra_probes_list
= strdup(arg
);
4870 if (!kmod_extra_probes_list
) {
4875 } else if (string_match(optname
, "config") || opt
== 'f') {
4876 /* This is handled in set_options() thus silent skip. */
4879 /* Unknown option or other error.
4880 * Error is printed by getopt, just return */
4885 if (ret
== -EINVAL
) {
4886 const char *opt_name
= "unknown";
4889 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
4891 if (opt
== long_options
[i
].val
) {
4892 opt_name
= long_options
[i
].name
;
4897 WARN("Invalid argument provided for option \"%s\", using default value.",
4905 * config_entry_handler_cb used to handle options read from a config file.
4906 * See config_entry_handler_cb comment in common/config/config.h for the
4907 * return value conventions.
4909 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
4913 if (!entry
|| !entry
->name
|| !entry
->value
) {
4918 /* Check if the option is to be ignored */
4919 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
4920 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
4925 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
4928 /* Ignore if not fully matched. */
4929 if (strcmp(entry
->name
, long_options
[i
].name
)) {
4934 * If the option takes no argument on the command line, we have to
4935 * check if the value is "true". We support non-zero numeric values,
4938 if (!long_options
[i
].has_arg
) {
4939 ret
= config_parse_value(entry
->value
);
4942 WARN("Invalid configuration value \"%s\" for option %s",
4943 entry
->value
, entry
->name
);
4945 /* False, skip boolean config option. */
4950 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
4954 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
4961 * daemon configuration loading and argument parsing
4963 static int set_options(int argc
, char **argv
)
4965 int ret
= 0, c
= 0, option_index
= 0;
4966 int orig_optopt
= optopt
, orig_optind
= optind
;
4968 const char *config_path
= NULL
;
4970 optstring
= utils_generate_optstring(long_options
,
4971 sizeof(long_options
) / sizeof(struct option
));
4977 /* Check for the --config option */
4978 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
4979 &option_index
)) != -1) {
4983 } else if (c
!= 'f') {
4984 /* if not equal to --config option. */
4988 if (lttng_is_setuid_setgid()) {
4989 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4992 config_path
= utils_expand_path(optarg
);
4994 ERR("Failed to resolve path: %s", optarg
);
4999 ret
= config_get_section_entries(config_path
, config_section_name
,
5000 config_entry_handler
, NULL
);
5003 ERR("Invalid configuration option at line %i", ret
);
5009 /* Reset getopt's global state */
5010 optopt
= orig_optopt
;
5011 optind
= orig_optind
;
5015 * getopt_long() will not set option_index if it encounters a
5018 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5025 * Pass NULL as the long option name if popt left the index
5028 ret
= set_option(c
, optarg
,
5029 option_index
< 0 ? NULL
:
5030 long_options
[option_index
].name
);
5042 * Creates the two needed socket by the daemon.
5043 * apps_sock - The communication socket for all UST apps.
5044 * client_sock - The communication of the cli tool (lttng).
5046 static int init_daemon_socket(void)
5051 old_umask
= umask(0);
5053 /* Create client tool unix socket */
5054 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
5055 if (client_sock
< 0) {
5056 ERR("Create unix sock failed: %s", client_unix_sock_path
);
5061 /* Set the cloexec flag */
5062 ret
= utils_set_fd_cloexec(client_sock
);
5064 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5065 "Continuing but note that the consumer daemon will have a "
5066 "reference to this socket on exec()", client_sock
);
5069 /* File permission MUST be 660 */
5070 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5072 ERR("Set file permissions failed: %s", client_unix_sock_path
);
5077 /* Create the application unix socket */
5078 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
5079 if (apps_sock
< 0) {
5080 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
5085 /* Set the cloexec flag */
5086 ret
= utils_set_fd_cloexec(apps_sock
);
5088 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5089 "Continuing but note that the consumer daemon will have a "
5090 "reference to this socket on exec()", apps_sock
);
5093 /* File permission MUST be 666 */
5094 ret
= chmod(apps_unix_sock_path
,
5095 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5097 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
5102 DBG3("Session daemon client socket %d and application socket %d created",
5103 client_sock
, apps_sock
);
5111 * Check if the global socket is available, and if a daemon is answering at the
5112 * other side. If yes, error is returned.
5114 static int check_existing_daemon(void)
5116 /* Is there anybody out there ? */
5117 if (lttng_session_daemon_alive()) {
5125 * Set the tracing group gid onto the client socket.
5127 * Race window between mkdir and chown is OK because we are going from more
5128 * permissive (root.root) to less permissive (root.tracing).
5130 static int set_permissions(char *rundir
)
5135 gid
= utils_get_group_id(tracing_group_name
);
5137 /* Set lttng run dir */
5138 ret
= chown(rundir
, 0, gid
);
5140 ERR("Unable to set group on %s", rundir
);
5145 * Ensure all applications and tracing group can search the run
5146 * dir. Allow everyone to read the directory, since it does not
5147 * buy us anything to hide its content.
5149 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5151 ERR("Unable to set permissions on %s", rundir
);
5155 /* lttng client socket path */
5156 ret
= chown(client_unix_sock_path
, 0, gid
);
5158 ERR("Unable to set group on %s", client_unix_sock_path
);
5162 /* kconsumer error socket path */
5163 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5165 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5169 /* 64-bit ustconsumer error socket path */
5170 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5172 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5176 /* 32-bit ustconsumer compat32 error socket path */
5177 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5179 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5183 DBG("All permissions are set");
5189 * Create the lttng run directory needed for all global sockets and pipe.
5191 static int create_lttng_rundir(const char *rundir
)
5195 DBG3("Creating LTTng run directory: %s", rundir
);
5197 ret
= mkdir(rundir
, S_IRWXU
);
5199 if (errno
!= EEXIST
) {
5200 ERR("Unable to create %s", rundir
);
5212 * Setup sockets and directory needed by the kconsumerd communication with the
5215 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
5219 char path
[PATH_MAX
];
5221 switch (consumer_data
->type
) {
5222 case LTTNG_CONSUMER_KERNEL
:
5223 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
5225 case LTTNG_CONSUMER64_UST
:
5226 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
5228 case LTTNG_CONSUMER32_UST
:
5229 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
5232 ERR("Consumer type unknown");
5237 DBG2("Creating consumer directory: %s", path
);
5239 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5241 if (errno
!= EEXIST
) {
5243 ERR("Failed to create %s", path
);
5249 ret
= chown(path
, 0, utils_get_group_id(tracing_group_name
));
5251 ERR("Unable to set group on %s", path
);
5257 /* Create the kconsumerd error unix socket */
5258 consumer_data
->err_sock
=
5259 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5260 if (consumer_data
->err_sock
< 0) {
5261 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5267 * Set the CLOEXEC flag. Return code is useless because either way, the
5270 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5272 PERROR("utils_set_fd_cloexec");
5273 /* continue anyway */
5276 /* File permission MUST be 660 */
5277 ret
= chmod(consumer_data
->err_unix_sock_path
,
5278 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5280 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5290 * Signal handler for the daemon
5292 * Simply stop all worker threads, leaving main() return gracefully after
5293 * joining all threads and calling cleanup().
5295 static void sighandler(int sig
)
5299 DBG("SIGPIPE caught");
5302 DBG("SIGINT caught");
5306 DBG("SIGTERM caught");
5310 CMM_STORE_SHARED(recv_child_signal
, 1);
5318 * Setup signal handler for :
5319 * SIGINT, SIGTERM, SIGPIPE
5321 static int set_signal_handler(void)
5324 struct sigaction sa
;
5327 if ((ret
= sigemptyset(&sigset
)) < 0) {
5328 PERROR("sigemptyset");
5332 sa
.sa_handler
= sighandler
;
5333 sa
.sa_mask
= sigset
;
5335 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5336 PERROR("sigaction");
5340 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5341 PERROR("sigaction");
5345 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5346 PERROR("sigaction");
5350 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5351 PERROR("sigaction");
5355 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5361 * Set open files limit to unlimited. This daemon can open a large number of
5362 * file descriptors in order to consumer multiple kernel traces.
5364 static void set_ulimit(void)
5369 /* The kernel does not allowed an infinite limit for open files */
5370 lim
.rlim_cur
= 65535;
5371 lim
.rlim_max
= 65535;
5373 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5375 PERROR("failed to set open files limit");
5380 * Write pidfile using the rundir and opt_pidfile.
5382 static int write_pidfile(void)
5385 char pidfile_path
[PATH_MAX
];
5390 strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
));
5392 /* Build pidfile path from rundir and opt_pidfile. */
5393 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
5394 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
5396 PERROR("snprintf pidfile path");
5402 * Create pid file in rundir.
5404 ret
= utils_create_pid_file(getpid(), pidfile_path
);
5410 * Create lockfile using the rundir and return its fd.
5412 static int create_lockfile(void)
5415 char lockfile_path
[PATH_MAX
];
5417 ret
= generate_lock_file_path(lockfile_path
, sizeof(lockfile_path
));
5422 ret
= utils_create_lock_file(lockfile_path
);
5428 * Write agent TCP port using the rundir.
5430 static int write_agent_port(void)
5433 char path
[PATH_MAX
];
5437 ret
= snprintf(path
, sizeof(path
), "%s/"
5438 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
, rundir
);
5440 PERROR("snprintf agent port path");
5445 * Create TCP agent port file in rundir.
5447 ret
= utils_create_pid_file(agent_tcp_port
, path
);
5456 int main(int argc
, char **argv
)
5458 int ret
= 0, retval
= 0;
5460 const char *home_path
, *env_app_timeout
;
5462 init_kernel_workarounds();
5464 rcu_register_thread();
5466 if (set_signal_handler()) {
5468 goto exit_set_signal_handler
;
5471 setup_consumerd_path();
5473 page_size
= sysconf(_SC_PAGESIZE
);
5474 if (page_size
< 0) {
5475 PERROR("sysconf _SC_PAGESIZE");
5476 page_size
= LONG_MAX
;
5477 WARN("Fallback page size to %ld", page_size
);
5481 * Parse arguments and load the daemon configuration file.
5483 * We have an exit_options exit path to free memory reserved by
5484 * set_options. This is needed because the rest of sessiond_cleanup()
5485 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5486 * depends on set_options.
5489 if (set_options(argc
, argv
)) {
5495 if (opt_daemon
|| opt_background
) {
5498 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5506 * We are in the child. Make sure all other file descriptors are
5507 * closed, in case we are called with more opened file
5508 * descriptors than the standard ones.
5510 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5515 if (run_as_create_worker(argv
[0]) < 0) {
5516 goto exit_create_run_as_worker_cleanup
;
5520 * Starting from here, we can create threads. This needs to be after
5521 * lttng_daemonize due to RCU.
5525 * Initialize the health check subsystem. This call should set the
5526 * appropriate time values.
5528 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5529 if (!health_sessiond
) {
5530 PERROR("health_app_create error");
5532 goto exit_health_sessiond_cleanup
;
5535 if (init_ht_cleanup_quit_pipe()) {
5537 goto exit_ht_cleanup_quit_pipe
;
5540 /* Setup the thread ht_cleanup communication pipe. */
5541 if (utils_create_pipe_cloexec(ht_cleanup_pipe
)) {
5543 goto exit_ht_cleanup_pipe
;
5546 /* Set up max poll set size */
5547 if (lttng_poll_set_max_size()) {
5549 goto exit_set_max_size
;
5552 /* Create thread to clean up RCU hash tables */
5553 ret
= pthread_create(&ht_cleanup_thread
, NULL
,
5554 thread_ht_cleanup
, (void *) NULL
);
5557 PERROR("pthread_create ht_cleanup");
5559 goto exit_ht_cleanup
;
5562 /* Create thread quit pipe */
5563 if (init_thread_quit_pipe()) {
5565 goto exit_init_data
;
5568 /* Check if daemon is UID = 0 */
5569 is_root
= !getuid();
5572 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
5575 goto exit_init_data
;
5578 /* Create global run dir with root access */
5579 if (create_lttng_rundir(rundir
)) {
5581 goto exit_init_data
;
5584 if (strlen(apps_unix_sock_path
) == 0) {
5585 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5586 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
5589 goto exit_init_data
;
5593 if (strlen(client_unix_sock_path
) == 0) {
5594 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5595 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
5598 goto exit_init_data
;
5602 /* Set global SHM for ust */
5603 if (strlen(wait_shm_path
) == 0) {
5604 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5605 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
5608 goto exit_init_data
;
5612 if (strlen(health_unix_sock_path
) == 0) {
5613 ret
= snprintf(health_unix_sock_path
,
5614 sizeof(health_unix_sock_path
),
5615 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
5618 goto exit_init_data
;
5622 /* Setup kernel consumerd path */
5623 ret
= snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
5624 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
5627 goto exit_init_data
;
5629 ret
= snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
5630 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
5633 goto exit_init_data
;
5636 DBG2("Kernel consumer err path: %s",
5637 kconsumer_data
.err_unix_sock_path
);
5638 DBG2("Kernel consumer cmd path: %s",
5639 kconsumer_data
.cmd_unix_sock_path
);
5641 home_path
= utils_get_home_dir();
5642 if (home_path
== NULL
) {
5643 /* TODO: Add --socket PATH option */
5644 ERR("Can't get HOME directory for sockets creation.");
5646 goto exit_init_data
;
5650 * Create rundir from home path. This will create something like
5653 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
5656 goto exit_init_data
;
5659 if (create_lttng_rundir(rundir
)) {
5661 goto exit_init_data
;
5664 if (strlen(apps_unix_sock_path
) == 0) {
5665 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5666 DEFAULT_HOME_APPS_UNIX_SOCK
,
5670 goto exit_init_data
;
5674 /* Set the cli tool unix socket path */
5675 if (strlen(client_unix_sock_path
) == 0) {
5676 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5677 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
5681 goto exit_init_data
;
5685 /* Set global SHM for ust */
5686 if (strlen(wait_shm_path
) == 0) {
5687 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5688 DEFAULT_HOME_APPS_WAIT_SHM_PATH
,
5692 goto exit_init_data
;
5696 /* Set health check Unix path */
5697 if (strlen(health_unix_sock_path
) == 0) {
5698 ret
= snprintf(health_unix_sock_path
,
5699 sizeof(health_unix_sock_path
),
5700 DEFAULT_HOME_HEALTH_UNIX_SOCK
,
5704 goto exit_init_data
;
5709 lockfile_fd
= create_lockfile();
5710 if (lockfile_fd
< 0) {
5712 goto exit_init_data
;
5715 /* Set consumer initial state */
5716 kernel_consumerd_state
= CONSUMER_STOPPED
;
5717 ust_consumerd_state
= CONSUMER_STOPPED
;
5719 DBG("Client socket path %s", client_unix_sock_path
);
5720 DBG("Application socket path %s", apps_unix_sock_path
);
5721 DBG("Application wait path %s", wait_shm_path
);
5722 DBG("LTTng run directory path: %s", rundir
);
5724 /* 32 bits consumerd path setup */
5725 ret
= snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
5726 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
5728 PERROR("snprintf 32-bit consumer error socket path");
5730 goto exit_init_data
;
5732 ret
= snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
5733 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
5735 PERROR("snprintf 32-bit consumer command socket path");
5737 goto exit_init_data
;
5740 DBG2("UST consumer 32 bits err path: %s",
5741 ustconsumer32_data
.err_unix_sock_path
);
5742 DBG2("UST consumer 32 bits cmd path: %s",
5743 ustconsumer32_data
.cmd_unix_sock_path
);
5745 /* 64 bits consumerd path setup */
5746 ret
= snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
5747 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
5749 PERROR("snprintf 64-bit consumer error socket path");
5751 goto exit_init_data
;
5753 ret
= snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
5754 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
5756 PERROR("snprintf 64-bit consumer command socket path");
5758 goto exit_init_data
;
5761 DBG2("UST consumer 64 bits err path: %s",
5762 ustconsumer64_data
.err_unix_sock_path
);
5763 DBG2("UST consumer 64 bits cmd path: %s",
5764 ustconsumer64_data
.cmd_unix_sock_path
);
5767 * See if daemon already exist.
5769 if (check_existing_daemon()) {
5770 ERR("Already running daemon.\n");
5772 * We do not goto exit because we must not cleanup()
5773 * because a daemon is already running.
5776 goto exit_init_data
;
5780 * Init UST app hash table. Alloc hash table before this point since
5781 * cleanup() can get called after that point.
5783 if (ust_app_ht_alloc()) {
5784 ERR("Failed to allocate UST app hash table");
5786 goto exit_init_data
;
5790 * Initialize agent app hash table. We allocate the hash table here
5791 * since cleanup() can get called after this point.
5793 if (agent_app_ht_alloc()) {
5794 ERR("Failed to allocate Agent app hash table");
5796 goto exit_init_data
;
5800 * These actions must be executed as root. We do that *after* setting up
5801 * the sockets path because we MUST make the check for another daemon using
5802 * those paths *before* trying to set the kernel consumer sockets and init
5806 if (set_consumer_sockets(&kconsumer_data
, rundir
)) {
5808 goto exit_init_data
;
5811 /* Setup kernel tracer */
5812 if (!opt_no_kernel
) {
5813 init_kernel_tracer();
5814 if (kernel_tracer_fd
>= 0) {
5815 ret
= syscall_init_table();
5817 ERR("Unable to populate syscall table. "
5818 "Syscall tracing won't work "
5819 "for this session daemon.");
5824 /* Set ulimit for open files */
5827 /* init lttng_fd tracking must be done after set_ulimit. */
5830 if (set_consumer_sockets(&ustconsumer64_data
, rundir
)) {
5832 goto exit_init_data
;
5835 if (set_consumer_sockets(&ustconsumer32_data
, rundir
)) {
5837 goto exit_init_data
;
5840 /* Setup the needed unix socket */
5841 if (init_daemon_socket()) {
5843 goto exit_init_data
;
5846 /* Set credentials to socket */
5847 if (is_root
&& set_permissions(rundir
)) {
5849 goto exit_init_data
;
5852 /* Get parent pid if -S, --sig-parent is specified. */
5853 if (opt_sig_parent
) {
5857 /* Setup the kernel pipe for waking up the kernel thread */
5858 if (is_root
&& !opt_no_kernel
) {
5859 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
5861 goto exit_init_data
;
5865 /* Setup the thread apps communication pipe. */
5866 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
5868 goto exit_init_data
;
5871 /* Setup the thread apps notify communication pipe. */
5872 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
5874 goto exit_init_data
;
5877 /* Initialize global buffer per UID and PID registry. */
5878 buffer_reg_init_uid_registry();
5879 buffer_reg_init_pid_registry();
5881 /* Init UST command queue. */
5882 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
5885 * Get session list pointer. This pointer MUST NOT be free'd. This list
5886 * is statically declared in session.c
5888 session_list_ptr
= session_get_list();
5892 /* Check for the application socket timeout env variable. */
5893 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
5894 if (env_app_timeout
) {
5895 app_socket_timeout
= atoi(env_app_timeout
);
5897 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
5900 ret
= write_pidfile();
5902 ERR("Error in write_pidfile");
5904 goto exit_init_data
;
5906 ret
= write_agent_port();
5908 ERR("Error in write_agent_port");
5910 goto exit_init_data
;
5913 /* Initialize communication library */
5915 /* Initialize TCP timeout values */
5916 lttcomm_inet_init();
5918 if (load_session_init_data(&load_info
) < 0) {
5920 goto exit_init_data
;
5922 load_info
->path
= opt_load_session_path
;
5924 /* Create health-check thread */
5925 ret
= pthread_create(&health_thread
, NULL
,
5926 thread_manage_health
, (void *) NULL
);
5929 PERROR("pthread_create health");
5934 /* Create thread to manage the client socket */
5935 ret
= pthread_create(&client_thread
, NULL
,
5936 thread_manage_clients
, (void *) NULL
);
5939 PERROR("pthread_create clients");
5944 /* Create thread to dispatch registration */
5945 ret
= pthread_create(&dispatch_thread
, NULL
,
5946 thread_dispatch_ust_registration
, (void *) NULL
);
5949 PERROR("pthread_create dispatch");
5954 /* Create thread to manage application registration. */
5955 ret
= pthread_create(®_apps_thread
, NULL
,
5956 thread_registration_apps
, (void *) NULL
);
5959 PERROR("pthread_create registration");
5964 /* Create thread to manage application socket */
5965 ret
= pthread_create(&apps_thread
, NULL
,
5966 thread_manage_apps
, (void *) NULL
);
5969 PERROR("pthread_create apps");
5974 /* Create thread to manage application notify socket */
5975 ret
= pthread_create(&apps_notify_thread
, NULL
,
5976 ust_thread_manage_notify
, (void *) NULL
);
5979 PERROR("pthread_create notify");
5981 goto exit_apps_notify
;
5984 /* Create agent registration thread. */
5985 ret
= pthread_create(&agent_reg_thread
, NULL
,
5986 agent_thread_manage_registration
, (void *) NULL
);
5989 PERROR("pthread_create agent");
5991 goto exit_agent_reg
;
5994 /* Don't start this thread if kernel tracing is not requested nor root */
5995 if (is_root
&& !opt_no_kernel
) {
5996 /* Create kernel thread to manage kernel event */
5997 ret
= pthread_create(&kernel_thread
, NULL
,
5998 thread_manage_kernel
, (void *) NULL
);
6001 PERROR("pthread_create kernel");
6007 /* Create session loading thread. */
6008 ret
= pthread_create(&load_session_thread
, NULL
, thread_load_session
,
6012 PERROR("pthread_create load_session_thread");
6014 goto exit_load_session
;
6018 * This is where we start awaiting program completion (e.g. through
6019 * signal that asks threads to teardown).
6022 ret
= pthread_join(load_session_thread
, &status
);
6025 PERROR("pthread_join load_session_thread");
6030 if (is_root
&& !opt_no_kernel
) {
6031 ret
= pthread_join(kernel_thread
, &status
);
6034 PERROR("pthread_join");
6040 ret
= pthread_join(agent_reg_thread
, &status
);
6043 PERROR("pthread_join agent");
6048 ret
= pthread_join(apps_notify_thread
, &status
);
6051 PERROR("pthread_join apps notify");
6056 ret
= pthread_join(apps_thread
, &status
);
6059 PERROR("pthread_join apps");
6064 ret
= pthread_join(reg_apps_thread
, &status
);
6067 PERROR("pthread_join");
6073 * Join dispatch thread after joining reg_apps_thread to ensure
6074 * we don't leak applications in the queue.
6076 ret
= pthread_join(dispatch_thread
, &status
);
6079 PERROR("pthread_join");
6084 ret
= pthread_join(client_thread
, &status
);
6087 PERROR("pthread_join");
6092 ret
= pthread_join(health_thread
, &status
);
6095 PERROR("pthread_join health thread");
6102 * sessiond_cleanup() is called when no other thread is running, except
6103 * the ht_cleanup thread, which is needed to destroy the hash tables.
6105 rcu_thread_online();
6107 rcu_thread_offline();
6108 rcu_unregister_thread();
6110 ret
= notify_thread_pipe(ht_cleanup_quit_pipe
[1]);
6112 ERR("write error on ht_cleanup quit pipe");
6116 ret
= pthread_join(ht_cleanup_thread
, &status
);
6119 PERROR("pthread_join ht cleanup thread");
6125 utils_close_pipe(ht_cleanup_pipe
);
6126 exit_ht_cleanup_pipe
:
6129 * Close the ht_cleanup quit pipe.
6131 utils_close_pipe(ht_cleanup_quit_pipe
);
6132 exit_ht_cleanup_quit_pipe
:
6134 health_app_destroy(health_sessiond
);
6135 exit_health_sessiond_cleanup
:
6136 exit_create_run_as_worker_cleanup
:
6139 /* Ensure all prior call_rcu are done. */
6142 sessiond_cleanup_options();
6144 exit_set_signal_handler
: