2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <urcu/uatomic.h>
41 #include <common/common.h>
42 #include <common/compat/socket.h>
43 #include <common/compat/getenv.h>
44 #include <common/defaults.h>
45 #include <common/kernel-consumer/kernel-consumer.h>
46 #include <common/futex.h>
47 #include <common/relayd/relayd.h>
48 #include <common/utils.h>
49 #include <common/daemonize.h>
50 #include <common/config/session-config.h>
52 #include "lttng-sessiond.h"
53 #include "buffer-registry.h"
60 #include "kernel-consumer.h"
64 #include "ust-consumer.h"
67 #include "health-sessiond.h"
68 #include "testpoint.h"
69 #include "ust-thread.h"
70 #include "agent-thread.h"
72 #include "load-session-thread.h"
76 #define CONSUMERD_FILE "lttng-consumerd"
79 static const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
80 static int tracing_group_name_override
;
81 static char *opt_pidfile
;
82 static int opt_sig_parent
;
83 static int opt_verbose_consumer
;
84 static int opt_daemon
, opt_background
;
85 static int opt_no_kernel
;
86 static char *opt_load_session_path
;
87 static pid_t ppid
; /* Parent PID for --sig-parent option */
88 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
90 static int lockfile_fd
= -1;
92 /* Set to 1 when a SIGUSR1 signal is received. */
93 static int recv_child_signal
;
96 * Consumer daemon specific control data. Every value not initialized here is
97 * set to 0 by the static definition.
99 static struct consumer_data kconsumer_data
= {
100 .type
= LTTNG_CONSUMER_KERNEL
,
101 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
102 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
105 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
106 .lock
= PTHREAD_MUTEX_INITIALIZER
,
107 .cond
= PTHREAD_COND_INITIALIZER
,
108 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
110 static struct consumer_data ustconsumer64_data
= {
111 .type
= LTTNG_CONSUMER64_UST
,
112 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
113 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
116 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
117 .lock
= PTHREAD_MUTEX_INITIALIZER
,
118 .cond
= PTHREAD_COND_INITIALIZER
,
119 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
121 static struct consumer_data ustconsumer32_data
= {
122 .type
= LTTNG_CONSUMER32_UST
,
123 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
124 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
127 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
128 .lock
= PTHREAD_MUTEX_INITIALIZER
,
129 .cond
= PTHREAD_COND_INITIALIZER
,
130 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
133 /* Command line options */
134 static const struct option long_options
[] = {
135 { "client-sock", required_argument
, 0, 'c' },
136 { "apps-sock", required_argument
, 0, 'a' },
137 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
138 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
139 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
140 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
141 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
142 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
143 { "consumerd32-path", required_argument
, 0, '\0' },
144 { "consumerd32-libdir", required_argument
, 0, '\0' },
145 { "consumerd64-path", required_argument
, 0, '\0' },
146 { "consumerd64-libdir", required_argument
, 0, '\0' },
147 { "daemonize", no_argument
, 0, 'd' },
148 { "background", no_argument
, 0, 'b' },
149 { "sig-parent", no_argument
, 0, 'S' },
150 { "help", no_argument
, 0, 'h' },
151 { "group", required_argument
, 0, 'g' },
152 { "version", no_argument
, 0, 'V' },
153 { "quiet", no_argument
, 0, 'q' },
154 { "verbose", no_argument
, 0, 'v' },
155 { "verbose-consumer", no_argument
, 0, '\0' },
156 { "no-kernel", no_argument
, 0, '\0' },
157 { "pidfile", required_argument
, 0, 'p' },
158 { "agent-tcp-port", required_argument
, 0, '\0' },
159 { "config", required_argument
, 0, 'f' },
160 { "load", required_argument
, 0, 'l' },
161 { "kmod-probes", required_argument
, 0, '\0' },
162 { "extra-kmod-probes", required_argument
, 0, '\0' },
166 /* Command line options to ignore from configuration file */
167 static const char *config_ignore_options
[] = { "help", "version", "config" };
169 /* Shared between threads */
170 static int dispatch_thread_exit
;
172 /* Global application Unix socket path */
173 static char apps_unix_sock_path
[PATH_MAX
];
174 /* Global client Unix socket path */
175 static char client_unix_sock_path
[PATH_MAX
];
176 /* global wait shm path for UST */
177 static char wait_shm_path
[PATH_MAX
];
178 /* Global health check unix path */
179 static char health_unix_sock_path
[PATH_MAX
];
181 /* Sockets and FDs */
182 static int client_sock
= -1;
183 static int apps_sock
= -1;
184 int kernel_tracer_fd
= -1;
185 static int kernel_poll_pipe
[2] = { -1, -1 };
188 * Quit pipe for all threads. This permits a single cancellation point
189 * for all threads when receiving an event on the pipe.
191 static int thread_quit_pipe
[2] = { -1, -1 };
192 static int ht_cleanup_quit_pipe
[2] = { -1, -1 };
195 * This pipe is used to inform the thread managing application communication
196 * that a command is queued and ready to be processed.
198 static int apps_cmd_pipe
[2] = { -1, -1 };
200 int apps_cmd_notify_pipe
[2] = { -1, -1 };
202 /* Pthread, Mutexes and Semaphores */
203 static pthread_t apps_thread
;
204 static pthread_t apps_notify_thread
;
205 static pthread_t reg_apps_thread
;
206 static pthread_t client_thread
;
207 static pthread_t kernel_thread
;
208 static pthread_t dispatch_thread
;
209 static pthread_t health_thread
;
210 static pthread_t ht_cleanup_thread
;
211 static pthread_t agent_reg_thread
;
212 static pthread_t load_session_thread
;
215 * UST registration command queue. This queue is tied with a futex and uses a N
216 * wakers / 1 waiter implemented and detailed in futex.c/.h
218 * The thread_registration_apps and thread_dispatch_ust_registration uses this
219 * queue along with the wait/wake scheme. The thread_manage_apps receives down
220 * the line new application socket and monitors it for any I/O error or clean
221 * close that triggers an unregistration of the application.
223 static struct ust_cmd_queue ust_cmd_queue
;
226 * Pointer initialized before thread creation.
228 * This points to the tracing session list containing the session count and a
229 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
230 * MUST NOT be taken if you call a public function in session.c.
232 * The lock is nested inside the structure: session_list_ptr->lock. Please use
233 * session_lock_list and session_unlock_list for lock acquisition.
235 static struct ltt_session_list
*session_list_ptr
;
237 int ust_consumerd64_fd
= -1;
238 int ust_consumerd32_fd
= -1;
240 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
241 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
242 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
243 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
244 static int consumerd32_bin_override
;
245 static int consumerd64_bin_override
;
246 static int consumerd32_libdir_override
;
247 static int consumerd64_libdir_override
;
249 static const char *module_proc_lttng
= "/proc/lttng";
252 * Consumer daemon state which is changed when spawning it, killing it or in
253 * case of a fatal error.
255 enum consumerd_state
{
256 CONSUMER_STARTED
= 1,
257 CONSUMER_STOPPED
= 2,
262 * This consumer daemon state is used to validate if a client command will be
263 * able to reach the consumer. If not, the client is informed. For instance,
264 * doing a "lttng start" when the consumer state is set to ERROR will return an
265 * error to the client.
267 * The following example shows a possible race condition of this scheme:
269 * consumer thread error happens
271 * client cmd checks state -> still OK
272 * consumer thread exit, sets error
273 * client cmd try to talk to consumer
276 * However, since the consumer is a different daemon, we have no way of making
277 * sure the command will reach it safely even with this state flag. This is why
278 * we consider that up to the state validation during command processing, the
279 * command is safe. After that, we can not guarantee the correctness of the
280 * client request vis-a-vis the consumer.
282 static enum consumerd_state ust_consumerd_state
;
283 static enum consumerd_state kernel_consumerd_state
;
286 * Socket timeout for receiving and sending in seconds.
288 static int app_socket_timeout
;
290 /* Set in main() with the current page size. */
293 /* Application health monitoring */
294 struct health_app
*health_sessiond
;
296 /* Agent TCP port for registration. Used by the agent thread. */
297 unsigned int agent_tcp_port
= DEFAULT_AGENT_TCP_PORT
;
299 /* Am I root or not. */
300 int is_root
; /* Set to 1 if the daemon is running as root */
302 const char * const config_section_name
= "sessiond";
304 /* Load session thread information to operate. */
305 struct load_session_thread_data
*load_info
;
307 /* Global hash tables */
308 struct lttng_ht
*agent_apps_ht_by_sock
= NULL
;
311 * Whether sessiond is ready for commands/health check requests.
312 * NR_LTTNG_SESSIOND_READY must match the number of calls to
313 * sessiond_notify_ready().
315 #define NR_LTTNG_SESSIOND_READY 3
316 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_READY
;
318 /* Notify parents that we are ready for cmd and health check */
320 void sessiond_notify_ready(void)
322 if (uatomic_sub_return(<tng_sessiond_ready
, 1) == 0) {
324 * Notify parent pid that we are ready to accept command
325 * for client side. This ppid is the one from the
326 * external process that spawned us.
328 if (opt_sig_parent
) {
333 * Notify the parent of the fork() process that we are
336 if (opt_daemon
|| opt_background
) {
337 kill(child_ppid
, SIGUSR1
);
343 void setup_consumerd_path(void)
345 const char *bin
, *libdir
;
348 * Allow INSTALL_BIN_PATH to be used as a target path for the
349 * native architecture size consumer if CONFIG_CONSUMER*_PATH
350 * has not been defined.
352 #if (CAA_BITS_PER_LONG == 32)
353 if (!consumerd32_bin
[0]) {
354 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
356 if (!consumerd32_libdir
[0]) {
357 consumerd32_libdir
= INSTALL_LIB_PATH
;
359 #elif (CAA_BITS_PER_LONG == 64)
360 if (!consumerd64_bin
[0]) {
361 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
363 if (!consumerd64_libdir
[0]) {
364 consumerd64_libdir
= INSTALL_LIB_PATH
;
367 #error "Unknown bitness"
371 * runtime env. var. overrides the build default.
373 bin
= lttng_secure_getenv("LTTNG_CONSUMERD32_BIN");
375 consumerd32_bin
= bin
;
377 bin
= lttng_secure_getenv("LTTNG_CONSUMERD64_BIN");
379 consumerd64_bin
= bin
;
381 libdir
= lttng_secure_getenv("LTTNG_CONSUMERD32_LIBDIR");
383 consumerd32_libdir
= libdir
;
385 libdir
= lttng_secure_getenv("LTTNG_CONSUMERD64_LIBDIR");
387 consumerd64_libdir
= libdir
;
392 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
399 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
405 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
417 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
419 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
421 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);
425 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
427 int sessiond_set_ht_cleanup_thread_pollset(struct lttng_poll_event
*events
,
430 return __sessiond_set_thread_pollset(events
, size
,
431 ht_cleanup_quit_pipe
);
435 int __sessiond_check_thread_quit_pipe(int fd
, uint32_t events
, int a_pipe
)
437 if (fd
== a_pipe
&& (events
& LPOLLIN
)) {
444 * Check if the thread quit pipe was triggered.
446 * Return 1 if it was triggered else 0;
448 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
450 return __sessiond_check_thread_quit_pipe(fd
, events
,
451 thread_quit_pipe
[0]);
455 * Check if the ht_cleanup thread quit pipe was triggered.
457 * Return 1 if it was triggered else 0;
459 int sessiond_check_ht_cleanup_quit(int fd
, uint32_t events
)
461 return __sessiond_check_thread_quit_pipe(fd
, events
,
462 ht_cleanup_quit_pipe
[0]);
466 * Init thread quit pipe.
468 * Return -1 on error or 0 if all pipes are created.
470 static int __init_thread_quit_pipe(int *a_pipe
)
476 PERROR("thread quit pipe");
480 for (i
= 0; i
< 2; i
++) {
481 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
492 static int init_thread_quit_pipe(void)
494 return __init_thread_quit_pipe(thread_quit_pipe
);
497 static int init_ht_cleanup_quit_pipe(void)
499 return __init_thread_quit_pipe(ht_cleanup_quit_pipe
);
503 * Stop all threads by closing the thread quit pipe.
505 static void stop_threads(void)
509 /* Stopping all threads */
510 DBG("Terminating all threads");
511 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
513 ERR("write error on thread quit pipe");
516 /* Dispatch thread */
517 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
518 futex_nto1_wake(&ust_cmd_queue
.futex
);
522 * Close every consumer sockets.
524 static void close_consumer_sockets(void)
528 if (kconsumer_data
.err_sock
>= 0) {
529 ret
= close(kconsumer_data
.err_sock
);
531 PERROR("kernel consumer err_sock close");
534 if (ustconsumer32_data
.err_sock
>= 0) {
535 ret
= close(ustconsumer32_data
.err_sock
);
537 PERROR("UST consumerd32 err_sock close");
540 if (ustconsumer64_data
.err_sock
>= 0) {
541 ret
= close(ustconsumer64_data
.err_sock
);
543 PERROR("UST consumerd64 err_sock close");
546 if (kconsumer_data
.cmd_sock
>= 0) {
547 ret
= close(kconsumer_data
.cmd_sock
);
549 PERROR("kernel consumer cmd_sock close");
552 if (ustconsumer32_data
.cmd_sock
>= 0) {
553 ret
= close(ustconsumer32_data
.cmd_sock
);
555 PERROR("UST consumerd32 cmd_sock close");
558 if (ustconsumer64_data
.cmd_sock
>= 0) {
559 ret
= close(ustconsumer64_data
.cmd_sock
);
561 PERROR("UST consumerd64 cmd_sock close");
567 * Generate the full lock file path using the rundir.
569 * Return the snprintf() return value thus a negative value is an error.
571 static int generate_lock_file_path(char *path
, size_t len
)
578 /* Build lockfile path from rundir. */
579 ret
= snprintf(path
, len
, "%s/" DEFAULT_LTTNG_SESSIOND_LOCKFILE
, rundir
);
581 PERROR("snprintf lockfile path");
588 * Wait on consumer process termination.
590 * Need to be called with the consumer data lock held or from a context
591 * ensuring no concurrent access to data (e.g: cleanup).
593 static void wait_consumer(struct consumer_data
*consumer_data
)
598 if (consumer_data
->pid
<= 0) {
602 DBG("Waiting for complete teardown of consumerd (PID: %d)",
604 ret
= waitpid(consumer_data
->pid
, &status
, 0);
606 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
608 if (!WIFEXITED(status
)) {
609 ERR("consumerd termination with error: %d",
612 consumer_data
->pid
= 0;
616 * Cleanup the session daemon's data structures.
618 static void sessiond_cleanup(void)
621 struct ltt_session
*sess
, *stmp
;
624 DBG("Cleanup sessiond");
627 * Close the thread quit pipe. It has already done its job,
628 * since we are now called.
630 utils_close_pipe(thread_quit_pipe
);
633 * If opt_pidfile is undefined, the default file will be wiped when
634 * removing the rundir.
637 ret
= remove(opt_pidfile
);
639 PERROR("remove pidfile %s", opt_pidfile
);
643 DBG("Removing sessiond and consumerd content of directory %s", rundir
);
646 snprintf(path
, PATH_MAX
,
648 rundir
, DEFAULT_LTTNG_SESSIOND_PIDFILE
);
649 DBG("Removing %s", path
);
652 snprintf(path
, PATH_MAX
, "%s/%s", rundir
,
653 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
);
654 DBG("Removing %s", path
);
658 snprintf(path
, PATH_MAX
,
659 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
661 DBG("Removing %s", path
);
664 snprintf(path
, PATH_MAX
,
665 DEFAULT_KCONSUMERD_PATH
,
667 DBG("Removing directory %s", path
);
670 /* ust consumerd 32 */
671 snprintf(path
, PATH_MAX
,
672 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
674 DBG("Removing %s", path
);
677 snprintf(path
, PATH_MAX
,
678 DEFAULT_USTCONSUMERD32_PATH
,
680 DBG("Removing directory %s", path
);
683 /* ust consumerd 64 */
684 snprintf(path
, PATH_MAX
,
685 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
687 DBG("Removing %s", path
);
690 snprintf(path
, PATH_MAX
,
691 DEFAULT_USTCONSUMERD64_PATH
,
693 DBG("Removing directory %s", path
);
696 DBG("Cleaning up all sessions");
698 /* Destroy session list mutex */
699 if (session_list_ptr
!= NULL
) {
700 pthread_mutex_destroy(&session_list_ptr
->lock
);
702 /* Cleanup ALL session */
703 cds_list_for_each_entry_safe(sess
, stmp
,
704 &session_list_ptr
->head
, list
) {
705 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
709 wait_consumer(&kconsumer_data
);
710 wait_consumer(&ustconsumer64_data
);
711 wait_consumer(&ustconsumer32_data
);
713 DBG("Cleaning up all agent apps");
714 agent_app_ht_clean();
716 DBG("Closing all UST sockets");
717 ust_app_clean_list();
718 buffer_reg_destroy_registries();
720 if (is_root
&& !opt_no_kernel
) {
721 DBG2("Closing kernel fd");
722 if (kernel_tracer_fd
>= 0) {
723 ret
= close(kernel_tracer_fd
);
728 DBG("Unloading kernel modules");
729 modprobe_remove_lttng_all();
733 close_consumer_sockets();
736 load_session_destroy_data(load_info
);
741 * Cleanup lock file by deleting it and finaly closing it which will
742 * release the file system lock.
744 if (lockfile_fd
>= 0) {
745 char lockfile_path
[PATH_MAX
];
747 ret
= generate_lock_file_path(lockfile_path
,
748 sizeof(lockfile_path
));
750 ret
= remove(lockfile_path
);
752 PERROR("remove lock file");
754 ret
= close(lockfile_fd
);
756 PERROR("close lock file");
762 * We do NOT rmdir rundir because there are other processes
763 * using it, for instance lttng-relayd, which can start in
764 * parallel with this teardown.
771 * Cleanup the daemon's option data structures.
773 static void sessiond_cleanup_options(void)
775 DBG("Cleaning up options");
778 * If the override option is set, the pointer points to a *non* const
779 * thus freeing it even though the variable type is set to const.
781 if (tracing_group_name_override
) {
782 free((void *) tracing_group_name
);
784 if (consumerd32_bin_override
) {
785 free((void *) consumerd32_bin
);
787 if (consumerd64_bin_override
) {
788 free((void *) consumerd64_bin
);
790 if (consumerd32_libdir_override
) {
791 free((void *) consumerd32_libdir
);
793 if (consumerd64_libdir_override
) {
794 free((void *) consumerd64_libdir
);
798 free(opt_load_session_path
);
799 free(kmod_probes_list
);
800 free(kmod_extra_probes_list
);
802 run_as_destroy_worker();
805 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
806 "Matthew, BEET driven development works!%c[%dm",
807 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
812 * Send data on a unix socket using the liblttsessiondcomm API.
814 * Return lttcomm error code.
816 static int send_unix_sock(int sock
, void *buf
, size_t len
)
818 /* Check valid length */
823 return lttcomm_send_unix_sock(sock
, buf
, len
);
827 * Free memory of a command context structure.
829 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
831 DBG("Clean command context structure");
833 if ((*cmd_ctx
)->llm
) {
834 free((*cmd_ctx
)->llm
);
836 if ((*cmd_ctx
)->lsm
) {
837 free((*cmd_ctx
)->lsm
);
845 * Notify UST applications using the shm mmap futex.
847 static int notify_ust_apps(int active
)
851 DBG("Notifying applications of session daemon state: %d", active
);
853 /* See shm.c for this call implying mmap, shm and futex calls */
854 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
855 if (wait_shm_mmap
== NULL
) {
859 /* Wake waiting process */
860 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
862 /* Apps notified successfully */
870 * Setup the outgoing data buffer for the response (llm) by allocating the
871 * right amount of memory and copying the original information from the lsm
874 * Return 0 on success, negative value on error.
876 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
877 const void *payload_buf
, size_t payload_len
,
878 const void *cmd_header_buf
, size_t cmd_header_len
)
881 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
882 const size_t cmd_header_offset
= header_len
;
883 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
884 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
886 cmd_ctx
->llm
= zmalloc(total_msg_size
);
888 if (cmd_ctx
->llm
== NULL
) {
894 /* Copy common data */
895 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
896 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
897 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
898 cmd_ctx
->llm
->data_size
= payload_len
;
899 cmd_ctx
->lttng_msg_size
= total_msg_size
;
901 /* Copy command header */
902 if (cmd_header_len
) {
903 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
909 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
918 * Version of setup_lttng_msg() without command header.
920 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
921 void *payload_buf
, size_t payload_len
)
923 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
926 * Update the kernel poll set of all channel fd available over all tracing
927 * session. Add the wakeup pipe at the end of the set.
929 static int update_kernel_poll(struct lttng_poll_event
*events
)
932 struct ltt_session
*session
;
933 struct ltt_kernel_channel
*channel
;
935 DBG("Updating kernel poll set");
938 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
939 session_lock(session
);
940 if (session
->kernel_session
== NULL
) {
941 session_unlock(session
);
945 cds_list_for_each_entry(channel
,
946 &session
->kernel_session
->channel_list
.head
, list
) {
947 /* Add channel fd to the kernel poll set */
948 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
950 session_unlock(session
);
953 DBG("Channel fd %d added to kernel set", channel
->fd
);
955 session_unlock(session
);
957 session_unlock_list();
962 session_unlock_list();
967 * Find the channel fd from 'fd' over all tracing session. When found, check
968 * for new channel stream and send those stream fds to the kernel consumer.
970 * Useful for CPU hotplug feature.
972 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
975 struct ltt_session
*session
;
976 struct ltt_kernel_session
*ksess
;
977 struct ltt_kernel_channel
*channel
;
979 DBG("Updating kernel streams for channel fd %d", fd
);
982 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
983 session_lock(session
);
984 if (session
->kernel_session
== NULL
) {
985 session_unlock(session
);
988 ksess
= session
->kernel_session
;
990 cds_list_for_each_entry(channel
,
991 &ksess
->channel_list
.head
, list
) {
992 struct lttng_ht_iter iter
;
993 struct consumer_socket
*socket
;
995 if (channel
->fd
!= fd
) {
998 DBG("Channel found, updating kernel streams");
999 ret
= kernel_open_channel_stream(channel
);
1003 /* Update the stream global counter */
1004 ksess
->stream_count_global
+= ret
;
1007 * Have we already sent fds to the consumer? If yes, it
1008 * means that tracing is started so it is safe to send
1009 * our updated stream fds.
1011 if (ksess
->consumer_fds_sent
!= 1
1012 || ksess
->consumer
== NULL
) {
1018 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
1019 &iter
.iter
, socket
, node
.node
) {
1020 pthread_mutex_lock(socket
->lock
);
1021 ret
= kernel_consumer_send_channel_stream(socket
,
1023 session
->output_traces
? 1 : 0);
1024 pthread_mutex_unlock(socket
->lock
);
1032 session_unlock(session
);
1034 session_unlock_list();
1038 session_unlock(session
);
1039 session_unlock_list();
1044 * For each tracing session, update newly registered apps. The session list
1045 * lock MUST be acquired before calling this.
1047 static void update_ust_app(int app_sock
)
1049 struct ltt_session
*sess
, *stmp
;
1051 /* Consumer is in an ERROR state. Stop any application update. */
1052 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
1053 /* Stop the update process since the consumer is dead. */
1057 /* For all tracing session(s) */
1058 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
1059 struct ust_app
*app
;
1062 if (!sess
->ust_session
) {
1063 goto unlock_session
;
1067 assert(app_sock
>= 0);
1068 app
= ust_app_find_by_sock(app_sock
);
1071 * Application can be unregistered before so
1072 * this is possible hence simply stopping the
1075 DBG3("UST app update failed to find app sock %d",
1079 ust_app_global_update(sess
->ust_session
, app
);
1083 session_unlock(sess
);
1088 * This thread manage event coming from the kernel.
1090 * Features supported in this thread:
1093 static void *thread_manage_kernel(void *data
)
1095 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
1096 uint32_t revents
, nb_fd
;
1098 struct lttng_poll_event events
;
1100 DBG("[thread] Thread manage kernel started");
1102 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
1105 * This first step of the while is to clean this structure which could free
1106 * non NULL pointers so initialize it before the loop.
1108 lttng_poll_init(&events
);
1110 if (testpoint(sessiond_thread_manage_kernel
)) {
1111 goto error_testpoint
;
1114 health_code_update();
1116 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
1117 goto error_testpoint
;
1121 health_code_update();
1123 if (update_poll_flag
== 1) {
1124 /* Clean events object. We are about to populate it again. */
1125 lttng_poll_clean(&events
);
1127 ret
= sessiond_set_thread_pollset(&events
, 2);
1129 goto error_poll_create
;
1132 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
1137 /* This will add the available kernel channel if any. */
1138 ret
= update_kernel_poll(&events
);
1142 update_poll_flag
= 0;
1145 DBG("Thread kernel polling");
1147 /* Poll infinite value of time */
1149 health_poll_entry();
1150 ret
= lttng_poll_wait(&events
, -1);
1151 DBG("Thread kernel return from poll on %d fds",
1152 LTTNG_POLL_GETNB(&events
));
1156 * Restart interrupted system call.
1158 if (errno
== EINTR
) {
1162 } else if (ret
== 0) {
1163 /* Should not happen since timeout is infinite */
1164 ERR("Return value of poll is 0 with an infinite timeout.\n"
1165 "This should not have happened! Continuing...");
1171 for (i
= 0; i
< nb_fd
; i
++) {
1172 /* Fetch once the poll data */
1173 revents
= LTTNG_POLL_GETEV(&events
, i
);
1174 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1176 health_code_update();
1179 /* No activity for this FD (poll implementation). */
1183 /* Thread quit pipe has been closed. Killing thread. */
1184 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1190 /* Check for data on kernel pipe */
1191 if (revents
& LPOLLIN
) {
1192 if (pollfd
== kernel_poll_pipe
[0]) {
1193 (void) lttng_read(kernel_poll_pipe
[0],
1196 * Ret value is useless here, if this pipe gets any actions an
1197 * update is required anyway.
1199 update_poll_flag
= 1;
1203 * New CPU detected by the kernel. Adding kernel stream to
1204 * kernel session and updating the kernel consumer
1206 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1212 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1213 update_poll_flag
= 1;
1216 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1224 lttng_poll_clean(&events
);
1227 utils_close_pipe(kernel_poll_pipe
);
1228 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1231 ERR("Health error occurred in %s", __func__
);
1232 WARN("Kernel thread died unexpectedly. "
1233 "Kernel tracing can continue but CPU hotplug is disabled.");
1235 health_unregister(health_sessiond
);
1236 DBG("Kernel thread dying");
1241 * Signal pthread condition of the consumer data that the thread.
1243 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1245 pthread_mutex_lock(&data
->cond_mutex
);
1248 * The state is set before signaling. It can be any value, it's the waiter
1249 * job to correctly interpret this condition variable associated to the
1250 * consumer pthread_cond.
1252 * A value of 0 means that the corresponding thread of the consumer data
1253 * was not started. 1 indicates that the thread has started and is ready
1254 * for action. A negative value means that there was an error during the
1257 data
->consumer_thread_is_ready
= state
;
1258 (void) pthread_cond_signal(&data
->cond
);
1260 pthread_mutex_unlock(&data
->cond_mutex
);
1264 * This thread manage the consumer error sent back to the session daemon.
1266 static void *thread_manage_consumer(void *data
)
1268 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1269 uint32_t revents
, nb_fd
;
1270 enum lttcomm_return_code code
;
1271 struct lttng_poll_event events
;
1272 struct consumer_data
*consumer_data
= data
;
1274 DBG("[thread] Manage consumer started");
1276 rcu_register_thread();
1277 rcu_thread_online();
1279 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1281 health_code_update();
1284 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1285 * metadata_sock. Nothing more will be added to this poll set.
1287 ret
= sessiond_set_thread_pollset(&events
, 3);
1293 * The error socket here is already in a listening state which was done
1294 * just before spawning this thread to avoid a race between the consumer
1295 * daemon exec trying to connect and the listen() call.
1297 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1302 health_code_update();
1304 /* Infinite blocking call, waiting for transmission */
1306 health_poll_entry();
1308 if (testpoint(sessiond_thread_manage_consumer
)) {
1312 ret
= lttng_poll_wait(&events
, -1);
1316 * Restart interrupted system call.
1318 if (errno
== EINTR
) {
1326 for (i
= 0; i
< nb_fd
; i
++) {
1327 /* Fetch once the poll data */
1328 revents
= LTTNG_POLL_GETEV(&events
, i
);
1329 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1331 health_code_update();
1334 /* No activity for this FD (poll implementation). */
1338 /* Thread quit pipe has been closed. Killing thread. */
1339 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1345 /* Event on the registration socket */
1346 if (pollfd
== consumer_data
->err_sock
) {
1347 if (revents
& LPOLLIN
) {
1349 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1350 ERR("consumer err socket poll error");
1353 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1359 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1365 * Set the CLOEXEC flag. Return code is useless because either way, the
1368 (void) utils_set_fd_cloexec(sock
);
1370 health_code_update();
1372 DBG2("Receiving code from consumer err_sock");
1374 /* Getting status code from kconsumerd */
1375 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1376 sizeof(enum lttcomm_return_code
));
1381 health_code_update();
1382 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1383 /* Connect both socket, command and metadata. */
1384 consumer_data
->cmd_sock
=
1385 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1386 consumer_data
->metadata_fd
=
1387 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1388 if (consumer_data
->cmd_sock
< 0
1389 || consumer_data
->metadata_fd
< 0) {
1390 PERROR("consumer connect cmd socket");
1391 /* On error, signal condition and quit. */
1392 signal_consumer_condition(consumer_data
, -1);
1395 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1396 /* Create metadata socket lock. */
1397 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1398 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1399 PERROR("zmalloc pthread mutex");
1403 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1405 signal_consumer_condition(consumer_data
, 1);
1406 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1407 DBG("Consumer metadata socket ready (fd: %d)",
1408 consumer_data
->metadata_fd
);
1410 ERR("consumer error when waiting for SOCK_READY : %s",
1411 lttcomm_get_readable_code(-code
));
1415 /* Remove the consumerd error sock since we've established a connexion */
1416 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1421 /* Add new accepted error socket. */
1422 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1427 /* Add metadata socket that is successfully connected. */
1428 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1429 LPOLLIN
| LPOLLRDHUP
);
1434 health_code_update();
1436 /* Infinite blocking call, waiting for transmission */
1439 health_code_update();
1441 /* Exit the thread because the thread quit pipe has been triggered. */
1443 /* Not a health error. */
1448 health_poll_entry();
1449 ret
= lttng_poll_wait(&events
, -1);
1453 * Restart interrupted system call.
1455 if (errno
== EINTR
) {
1463 for (i
= 0; i
< nb_fd
; i
++) {
1464 /* Fetch once the poll data */
1465 revents
= LTTNG_POLL_GETEV(&events
, i
);
1466 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1468 health_code_update();
1471 /* No activity for this FD (poll implementation). */
1476 * Thread quit pipe has been triggered, flag that we should stop
1477 * but continue the current loop to handle potential data from
1480 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1482 if (pollfd
== sock
) {
1483 /* Event on the consumerd socket */
1484 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1485 && !(revents
& LPOLLIN
)) {
1486 ERR("consumer err socket second poll error");
1489 health_code_update();
1490 /* Wait for any kconsumerd error */
1491 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1492 sizeof(enum lttcomm_return_code
));
1494 ERR("consumer closed the command socket");
1498 ERR("consumer return code : %s",
1499 lttcomm_get_readable_code(-code
));
1502 } else if (pollfd
== consumer_data
->metadata_fd
) {
1503 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1504 && !(revents
& LPOLLIN
)) {
1505 ERR("consumer err metadata socket second poll error");
1508 /* UST metadata requests */
1509 ret
= ust_consumer_metadata_request(
1510 &consumer_data
->metadata_sock
);
1512 ERR("Handling metadata request");
1516 /* No need for an else branch all FDs are tested prior. */
1518 health_code_update();
1524 * We lock here because we are about to close the sockets and some other
1525 * thread might be using them so get exclusive access which will abort all
1526 * other consumer command by other threads.
1528 pthread_mutex_lock(&consumer_data
->lock
);
1530 /* Immediately set the consumerd state to stopped */
1531 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1532 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1533 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1534 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1535 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1537 /* Code flow error... */
1541 if (consumer_data
->err_sock
>= 0) {
1542 ret
= close(consumer_data
->err_sock
);
1546 consumer_data
->err_sock
= -1;
1548 if (consumer_data
->cmd_sock
>= 0) {
1549 ret
= close(consumer_data
->cmd_sock
);
1553 consumer_data
->cmd_sock
= -1;
1555 if (consumer_data
->metadata_sock
.fd_ptr
&&
1556 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1557 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1569 unlink(consumer_data
->err_unix_sock_path
);
1570 unlink(consumer_data
->cmd_unix_sock_path
);
1571 pthread_mutex_unlock(&consumer_data
->lock
);
1573 /* Cleanup metadata socket mutex. */
1574 if (consumer_data
->metadata_sock
.lock
) {
1575 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1576 free(consumer_data
->metadata_sock
.lock
);
1578 lttng_poll_clean(&events
);
1582 ERR("Health error occurred in %s", __func__
);
1584 health_unregister(health_sessiond
);
1585 DBG("consumer thread cleanup completed");
1587 rcu_thread_offline();
1588 rcu_unregister_thread();
1594 * This thread manage application communication.
1596 static void *thread_manage_apps(void *data
)
1598 int i
, ret
, pollfd
, err
= -1;
1600 uint32_t revents
, nb_fd
;
1601 struct lttng_poll_event events
;
1603 DBG("[thread] Manage application started");
1605 rcu_register_thread();
1606 rcu_thread_online();
1608 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1610 if (testpoint(sessiond_thread_manage_apps
)) {
1611 goto error_testpoint
;
1614 health_code_update();
1616 ret
= sessiond_set_thread_pollset(&events
, 2);
1618 goto error_poll_create
;
1621 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1626 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1630 health_code_update();
1633 DBG("Apps thread polling");
1635 /* Inifinite blocking call, waiting for transmission */
1637 health_poll_entry();
1638 ret
= lttng_poll_wait(&events
, -1);
1639 DBG("Apps thread return from poll on %d fds",
1640 LTTNG_POLL_GETNB(&events
));
1644 * Restart interrupted system call.
1646 if (errno
== EINTR
) {
1654 for (i
= 0; i
< nb_fd
; i
++) {
1655 /* Fetch once the poll data */
1656 revents
= LTTNG_POLL_GETEV(&events
, i
);
1657 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1659 health_code_update();
1662 /* No activity for this FD (poll implementation). */
1666 /* Thread quit pipe has been closed. Killing thread. */
1667 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1673 /* Inspect the apps cmd pipe */
1674 if (pollfd
== apps_cmd_pipe
[0]) {
1675 if (revents
& LPOLLIN
) {
1679 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1680 if (size_ret
< sizeof(sock
)) {
1681 PERROR("read apps cmd pipe");
1685 health_code_update();
1688 * Since this is a command socket (write then read),
1689 * we only monitor the error events of the socket.
1691 ret
= lttng_poll_add(&events
, sock
,
1692 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1697 DBG("Apps with sock %d added to poll set", sock
);
1698 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1699 ERR("Apps command pipe error");
1702 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1707 * At this point, we know that a registered application made
1708 * the event at poll_wait.
1710 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1711 /* Removing from the poll set */
1712 ret
= lttng_poll_del(&events
, pollfd
);
1717 /* Socket closed on remote end. */
1718 ust_app_unregister(pollfd
);
1720 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1725 health_code_update();
1731 lttng_poll_clean(&events
);
1734 utils_close_pipe(apps_cmd_pipe
);
1735 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1738 * We don't clean the UST app hash table here since already registered
1739 * applications can still be controlled so let them be until the session
1740 * daemon dies or the applications stop.
1745 ERR("Health error occurred in %s", __func__
);
1747 health_unregister(health_sessiond
);
1748 DBG("Application communication apps thread cleanup complete");
1749 rcu_thread_offline();
1750 rcu_unregister_thread();
1755 * Send a socket to a thread This is called from the dispatch UST registration
1756 * thread once all sockets are set for the application.
1758 * The sock value can be invalid, we don't really care, the thread will handle
1759 * it and make the necessary cleanup if so.
1761 * On success, return 0 else a negative value being the errno message of the
1764 static int send_socket_to_thread(int fd
, int sock
)
1769 * It's possible that the FD is set as invalid with -1 concurrently just
1770 * before calling this function being a shutdown state of the thread.
1777 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1778 if (ret
< sizeof(sock
)) {
1779 PERROR("write apps pipe %d", fd
);
1786 /* All good. Don't send back the write positive ret value. */
1793 * Sanitize the wait queue of the dispatch registration thread meaning removing
1794 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1795 * notify socket is never received.
1797 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1799 int ret
, nb_fd
= 0, i
;
1800 unsigned int fd_added
= 0;
1801 struct lttng_poll_event events
;
1802 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1806 lttng_poll_init(&events
);
1808 /* Just skip everything for an empty queue. */
1809 if (!wait_queue
->count
) {
1813 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1818 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1819 &wait_queue
->head
, head
) {
1820 assert(wait_node
->app
);
1821 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1822 LPOLLHUP
| LPOLLERR
);
1835 * Poll but don't block so we can quickly identify the faulty events and
1836 * clean them afterwards from the wait queue.
1838 ret
= lttng_poll_wait(&events
, 0);
1844 for (i
= 0; i
< nb_fd
; i
++) {
1845 /* Get faulty FD. */
1846 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1847 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1850 /* No activity for this FD (poll implementation). */
1854 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1855 &wait_queue
->head
, head
) {
1856 if (pollfd
== wait_node
->app
->sock
&&
1857 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1858 cds_list_del(&wait_node
->head
);
1859 wait_queue
->count
--;
1860 ust_app_destroy(wait_node
->app
);
1863 * Silence warning of use-after-free in
1864 * cds_list_for_each_entry_safe which uses
1865 * __typeof__(*wait_node).
1870 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1877 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1881 lttng_poll_clean(&events
);
1885 lttng_poll_clean(&events
);
1887 ERR("Unable to sanitize wait queue");
1892 * Dispatch request from the registration threads to the application
1893 * communication thread.
1895 static void *thread_dispatch_ust_registration(void *data
)
1898 struct cds_wfcq_node
*node
;
1899 struct ust_command
*ust_cmd
= NULL
;
1900 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1901 struct ust_reg_wait_queue wait_queue
= {
1905 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1907 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1908 goto error_testpoint
;
1911 health_code_update();
1913 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1915 DBG("[thread] Dispatch UST command started");
1917 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1918 health_code_update();
1920 /* Atomically prepare the queue futex */
1921 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1924 struct ust_app
*app
= NULL
;
1928 * Make sure we don't have node(s) that have hung up before receiving
1929 * the notify socket. This is to clean the list in order to avoid
1930 * memory leaks from notify socket that are never seen.
1932 sanitize_wait_queue(&wait_queue
);
1934 health_code_update();
1935 /* Dequeue command for registration */
1936 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1938 DBG("Woken up but nothing in the UST command queue");
1939 /* Continue thread execution */
1943 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1945 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1946 " gid:%d sock:%d name:%s (version %d.%d)",
1947 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1948 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1949 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1950 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1952 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1953 wait_node
= zmalloc(sizeof(*wait_node
));
1955 PERROR("zmalloc wait_node dispatch");
1956 ret
= close(ust_cmd
->sock
);
1958 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1960 lttng_fd_put(LTTNG_FD_APPS
, 1);
1964 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1966 /* Create application object if socket is CMD. */
1967 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1969 if (!wait_node
->app
) {
1970 ret
= close(ust_cmd
->sock
);
1972 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1974 lttng_fd_put(LTTNG_FD_APPS
, 1);
1980 * Add application to the wait queue so we can set the notify
1981 * socket before putting this object in the global ht.
1983 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1988 * We have to continue here since we don't have the notify
1989 * socket and the application MUST be added to the hash table
1990 * only at that moment.
1995 * Look for the application in the local wait queue and set the
1996 * notify socket if found.
1998 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1999 &wait_queue
.head
, head
) {
2000 health_code_update();
2001 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
2002 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
2003 cds_list_del(&wait_node
->head
);
2005 app
= wait_node
->app
;
2007 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
2013 * With no application at this stage the received socket is
2014 * basically useless so close it before we free the cmd data
2015 * structure for good.
2018 ret
= close(ust_cmd
->sock
);
2020 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
2022 lttng_fd_put(LTTNG_FD_APPS
, 1);
2029 * @session_lock_list
2031 * Lock the global session list so from the register up to the
2032 * registration done message, no thread can see the application
2033 * and change its state.
2035 session_lock_list();
2039 * Add application to the global hash table. This needs to be
2040 * done before the update to the UST registry can locate the
2045 /* Set app version. This call will print an error if needed. */
2046 (void) ust_app_version(app
);
2048 /* Send notify socket through the notify pipe. */
2049 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
2053 session_unlock_list();
2055 * No notify thread, stop the UST tracing. However, this is
2056 * not an internal error of the this thread thus setting
2057 * the health error code to a normal exit.
2064 * Update newly registered application with the tracing
2065 * registry info already enabled information.
2067 update_ust_app(app
->sock
);
2070 * Don't care about return value. Let the manage apps threads
2071 * handle app unregistration upon socket close.
2073 (void) ust_app_register_done(app
);
2076 * Even if the application socket has been closed, send the app
2077 * to the thread and unregistration will take place at that
2080 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
2083 session_unlock_list();
2085 * No apps. thread, stop the UST tracing. However, this is
2086 * not an internal error of the this thread thus setting
2087 * the health error code to a normal exit.
2094 session_unlock_list();
2096 } while (node
!= NULL
);
2098 health_poll_entry();
2099 /* Futex wait on queue. Blocking call on futex() */
2100 futex_nto1_wait(&ust_cmd_queue
.futex
);
2103 /* Normal exit, no error */
2107 /* Clean up wait queue. */
2108 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2109 &wait_queue
.head
, head
) {
2110 cds_list_del(&wait_node
->head
);
2115 /* Empty command queue. */
2117 /* Dequeue command for registration */
2118 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2122 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2123 ret
= close(ust_cmd
->sock
);
2125 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2127 lttng_fd_put(LTTNG_FD_APPS
, 1);
2132 DBG("Dispatch thread dying");
2135 ERR("Health error occurred in %s", __func__
);
2137 health_unregister(health_sessiond
);
2142 * This thread manage application registration.
2144 static void *thread_registration_apps(void *data
)
2146 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2147 uint32_t revents
, nb_fd
;
2148 struct lttng_poll_event events
;
2150 * Get allocated in this thread, enqueued to a global queue, dequeued and
2151 * freed in the manage apps thread.
2153 struct ust_command
*ust_cmd
= NULL
;
2155 DBG("[thread] Manage application registration started");
2157 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2159 if (testpoint(sessiond_thread_registration_apps
)) {
2160 goto error_testpoint
;
2163 ret
= lttcomm_listen_unix_sock(apps_sock
);
2169 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2170 * more will be added to this poll set.
2172 ret
= sessiond_set_thread_pollset(&events
, 2);
2174 goto error_create_poll
;
2177 /* Add the application registration socket */
2178 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2180 goto error_poll_add
;
2183 /* Notify all applications to register */
2184 ret
= notify_ust_apps(1);
2186 ERR("Failed to notify applications or create the wait shared memory.\n"
2187 "Execution continues but there might be problem for already\n"
2188 "running applications that wishes to register.");
2192 DBG("Accepting application registration");
2194 /* Inifinite blocking call, waiting for transmission */
2196 health_poll_entry();
2197 ret
= lttng_poll_wait(&events
, -1);
2201 * Restart interrupted system call.
2203 if (errno
== EINTR
) {
2211 for (i
= 0; i
< nb_fd
; i
++) {
2212 health_code_update();
2214 /* Fetch once the poll data */
2215 revents
= LTTNG_POLL_GETEV(&events
, i
);
2216 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2219 /* No activity for this FD (poll implementation). */
2223 /* Thread quit pipe has been closed. Killing thread. */
2224 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2230 /* Event on the registration socket */
2231 if (pollfd
== apps_sock
) {
2232 if (revents
& LPOLLIN
) {
2233 sock
= lttcomm_accept_unix_sock(apps_sock
);
2239 * Set socket timeout for both receiving and ending.
2240 * app_socket_timeout is in seconds, whereas
2241 * lttcomm_setsockopt_rcv_timeout and
2242 * lttcomm_setsockopt_snd_timeout expect msec as
2245 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2246 app_socket_timeout
* 1000);
2247 (void) lttcomm_setsockopt_snd_timeout(sock
,
2248 app_socket_timeout
* 1000);
2251 * Set the CLOEXEC flag. Return code is useless because
2252 * either way, the show must go on.
2254 (void) utils_set_fd_cloexec(sock
);
2256 /* Create UST registration command for enqueuing */
2257 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2258 if (ust_cmd
== NULL
) {
2259 PERROR("ust command zmalloc");
2268 * Using message-based transmissions to ensure we don't
2269 * have to deal with partially received messages.
2271 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2273 ERR("Exhausted file descriptors allowed for applications.");
2283 health_code_update();
2284 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2287 /* Close socket of the application. */
2292 lttng_fd_put(LTTNG_FD_APPS
, 1);
2296 health_code_update();
2298 ust_cmd
->sock
= sock
;
2301 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2302 " gid:%d sock:%d name:%s (version %d.%d)",
2303 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2304 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2305 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2306 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2309 * Lock free enqueue the registration request. The red pill
2310 * has been taken! This apps will be part of the *system*.
2312 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2315 * Wake the registration queue futex. Implicit memory
2316 * barrier with the exchange in cds_wfcq_enqueue.
2318 futex_nto1_wake(&ust_cmd_queue
.futex
);
2319 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2320 ERR("Register apps socket poll error");
2323 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2332 /* Notify that the registration thread is gone */
2335 if (apps_sock
>= 0) {
2336 ret
= close(apps_sock
);
2346 lttng_fd_put(LTTNG_FD_APPS
, 1);
2348 unlink(apps_unix_sock_path
);
2351 lttng_poll_clean(&events
);
2355 DBG("UST Registration thread cleanup complete");
2358 ERR("Health error occurred in %s", __func__
);
2360 health_unregister(health_sessiond
);
2366 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2367 * exec or it will fails.
2369 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2372 struct timespec timeout
;
2374 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2375 consumer_data
->consumer_thread_is_ready
= 0;
2377 /* Setup pthread condition */
2378 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2381 PERROR("pthread_condattr_init consumer data");
2386 * Set the monotonic clock in order to make sure we DO NOT jump in time
2387 * between the clock_gettime() call and the timedwait call. See bug #324
2388 * for a more details and how we noticed it.
2390 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2393 PERROR("pthread_condattr_setclock consumer data");
2397 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2400 PERROR("pthread_cond_init consumer data");
2404 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
2408 PERROR("pthread_create consumer");
2413 /* We are about to wait on a pthread condition */
2414 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2416 /* Get time for sem_timedwait absolute timeout */
2417 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2419 * Set the timeout for the condition timed wait even if the clock gettime
2420 * call fails since we might loop on that call and we want to avoid to
2421 * increment the timeout too many times.
2423 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2426 * The following loop COULD be skipped in some conditions so this is why we
2427 * set ret to 0 in order to make sure at least one round of the loop is
2433 * Loop until the condition is reached or when a timeout is reached. Note
2434 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2435 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2436 * possible. This loop does not take any chances and works with both of
2439 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2440 if (clock_ret
< 0) {
2441 PERROR("clock_gettime spawn consumer");
2442 /* Infinite wait for the consumerd thread to be ready */
2443 ret
= pthread_cond_wait(&consumer_data
->cond
,
2444 &consumer_data
->cond_mutex
);
2446 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2447 &consumer_data
->cond_mutex
, &timeout
);
2451 /* Release the pthread condition */
2452 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2456 if (ret
== ETIMEDOUT
) {
2460 * Call has timed out so we kill the kconsumerd_thread and return
2463 ERR("Condition timed out. The consumer thread was never ready."
2465 pth_ret
= pthread_cancel(consumer_data
->thread
);
2467 PERROR("pthread_cancel consumer thread");
2470 PERROR("pthread_cond_wait failed consumer thread");
2472 /* Caller is expecting a negative value on failure. */
2477 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2478 if (consumer_data
->pid
== 0) {
2479 ERR("Consumerd did not start");
2480 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2483 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2492 * Join consumer thread
2494 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2498 /* Consumer pid must be a real one. */
2499 if (consumer_data
->pid
> 0) {
2501 ret
= kill(consumer_data
->pid
, SIGTERM
);
2503 PERROR("Error killing consumer daemon");
2506 return pthread_join(consumer_data
->thread
, &status
);
2513 * Fork and exec a consumer daemon (consumerd).
2515 * Return pid if successful else -1.
2517 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2521 const char *consumer_to_use
;
2522 const char *verbosity
;
2525 DBG("Spawning consumerd");
2532 if (opt_verbose_consumer
) {
2533 verbosity
= "--verbose";
2534 } else if (lttng_opt_quiet
) {
2535 verbosity
= "--quiet";
2540 switch (consumer_data
->type
) {
2541 case LTTNG_CONSUMER_KERNEL
:
2543 * Find out which consumerd to execute. We will first try the
2544 * 64-bit path, then the sessiond's installation directory, and
2545 * fallback on the 32-bit one,
2547 DBG3("Looking for a kernel consumer at these locations:");
2548 DBG3(" 1) %s", consumerd64_bin
);
2549 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2550 DBG3(" 3) %s", consumerd32_bin
);
2551 if (stat(consumerd64_bin
, &st
) == 0) {
2552 DBG3("Found location #1");
2553 consumer_to_use
= consumerd64_bin
;
2554 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2555 DBG3("Found location #2");
2556 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2557 } else if (stat(consumerd32_bin
, &st
) == 0) {
2558 DBG3("Found location #3");
2559 consumer_to_use
= consumerd32_bin
;
2561 DBG("Could not find any valid consumerd executable");
2565 DBG("Using kernel consumer at: %s", consumer_to_use
);
2566 ret
= execl(consumer_to_use
,
2567 "lttng-consumerd", verbosity
, "-k",
2568 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2569 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2570 "--group", tracing_group_name
,
2573 case LTTNG_CONSUMER64_UST
:
2575 char *tmpnew
= NULL
;
2577 if (consumerd64_libdir
[0] != '\0') {
2581 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2585 tmplen
= strlen("LD_LIBRARY_PATH=")
2586 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2587 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2592 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2593 strcat(tmpnew
, consumerd64_libdir
);
2594 if (tmp
[0] != '\0') {
2595 strcat(tmpnew
, ":");
2596 strcat(tmpnew
, tmp
);
2598 ret
= putenv(tmpnew
);
2605 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2606 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2607 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2608 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2609 "--group", tracing_group_name
,
2611 if (consumerd64_libdir
[0] != '\0') {
2616 case LTTNG_CONSUMER32_UST
:
2618 char *tmpnew
= NULL
;
2620 if (consumerd32_libdir
[0] != '\0') {
2624 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2628 tmplen
= strlen("LD_LIBRARY_PATH=")
2629 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2630 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2635 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2636 strcat(tmpnew
, consumerd32_libdir
);
2637 if (tmp
[0] != '\0') {
2638 strcat(tmpnew
, ":");
2639 strcat(tmpnew
, tmp
);
2641 ret
= putenv(tmpnew
);
2648 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2649 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2650 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2651 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2652 "--group", tracing_group_name
,
2654 if (consumerd32_libdir
[0] != '\0') {
2660 PERROR("unknown consumer type");
2664 PERROR("Consumer execl()");
2666 /* Reaching this point, we got a failure on our execl(). */
2668 } else if (pid
> 0) {
2671 PERROR("start consumer fork");
2679 * Spawn the consumerd daemon and session daemon thread.
2681 static int start_consumerd(struct consumer_data
*consumer_data
)
2686 * Set the listen() state on the socket since there is a possible race
2687 * between the exec() of the consumer daemon and this call if place in the
2688 * consumer thread. See bug #366 for more details.
2690 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2695 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2696 if (consumer_data
->pid
!= 0) {
2697 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2701 ret
= spawn_consumerd(consumer_data
);
2703 ERR("Spawning consumerd failed");
2704 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2708 /* Setting up the consumer_data pid */
2709 consumer_data
->pid
= ret
;
2710 DBG2("Consumer pid %d", consumer_data
->pid
);
2711 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2713 DBG2("Spawning consumer control thread");
2714 ret
= spawn_consumer_thread(consumer_data
);
2716 ERR("Fatal error spawning consumer control thread");
2724 /* Cleanup already created sockets on error. */
2725 if (consumer_data
->err_sock
>= 0) {
2728 err
= close(consumer_data
->err_sock
);
2730 PERROR("close consumer data error socket");
2737 * Setup necessary data for kernel tracer action.
2739 static int init_kernel_tracer(void)
2743 /* Modprobe lttng kernel modules */
2744 ret
= modprobe_lttng_control();
2749 /* Open debugfs lttng */
2750 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2751 if (kernel_tracer_fd
< 0) {
2752 DBG("Failed to open %s", module_proc_lttng
);
2757 /* Validate kernel version */
2758 ret
= kernel_validate_version(kernel_tracer_fd
);
2763 ret
= modprobe_lttng_data();
2768 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2772 modprobe_remove_lttng_control();
2773 ret
= close(kernel_tracer_fd
);
2777 kernel_tracer_fd
= -1;
2778 return LTTNG_ERR_KERN_VERSION
;
2781 ret
= close(kernel_tracer_fd
);
2787 modprobe_remove_lttng_control();
2790 WARN("No kernel tracer available");
2791 kernel_tracer_fd
= -1;
2793 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2795 return LTTNG_ERR_KERN_NA
;
2801 * Copy consumer output from the tracing session to the domain session. The
2802 * function also applies the right modification on a per domain basis for the
2803 * trace files destination directory.
2805 * Should *NOT* be called with RCU read-side lock held.
2807 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2810 const char *dir_name
;
2811 struct consumer_output
*consumer
;
2814 assert(session
->consumer
);
2817 case LTTNG_DOMAIN_KERNEL
:
2818 DBG3("Copying tracing session consumer output in kernel session");
2820 * XXX: We should audit the session creation and what this function
2821 * does "extra" in order to avoid a destroy since this function is used
2822 * in the domain session creation (kernel and ust) only. Same for UST
2825 if (session
->kernel_session
->consumer
) {
2826 consumer_output_put(session
->kernel_session
->consumer
);
2828 session
->kernel_session
->consumer
=
2829 consumer_copy_output(session
->consumer
);
2830 /* Ease our life a bit for the next part */
2831 consumer
= session
->kernel_session
->consumer
;
2832 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2834 case LTTNG_DOMAIN_JUL
:
2835 case LTTNG_DOMAIN_LOG4J
:
2836 case LTTNG_DOMAIN_PYTHON
:
2837 case LTTNG_DOMAIN_UST
:
2838 DBG3("Copying tracing session consumer output in UST session");
2839 if (session
->ust_session
->consumer
) {
2840 consumer_output_put(session
->ust_session
->consumer
);
2842 session
->ust_session
->consumer
=
2843 consumer_copy_output(session
->consumer
);
2844 /* Ease our life a bit for the next part */
2845 consumer
= session
->ust_session
->consumer
;
2846 dir_name
= DEFAULT_UST_TRACE_DIR
;
2849 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2853 /* Append correct directory to subdir */
2854 strncat(consumer
->subdir
, dir_name
,
2855 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2856 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2865 * Create an UST session and add it to the session ust list.
2867 * Should *NOT* be called with RCU read-side lock held.
2869 static int create_ust_session(struct ltt_session
*session
,
2870 struct lttng_domain
*domain
)
2873 struct ltt_ust_session
*lus
= NULL
;
2877 assert(session
->consumer
);
2879 switch (domain
->type
) {
2880 case LTTNG_DOMAIN_JUL
:
2881 case LTTNG_DOMAIN_LOG4J
:
2882 case LTTNG_DOMAIN_PYTHON
:
2883 case LTTNG_DOMAIN_UST
:
2886 ERR("Unknown UST domain on create session %d", domain
->type
);
2887 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2891 DBG("Creating UST session");
2893 lus
= trace_ust_create_session(session
->id
);
2895 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2899 lus
->uid
= session
->uid
;
2900 lus
->gid
= session
->gid
;
2901 lus
->output_traces
= session
->output_traces
;
2902 lus
->snapshot_mode
= session
->snapshot_mode
;
2903 lus
->live_timer_interval
= session
->live_timer
;
2904 session
->ust_session
= lus
;
2905 if (session
->shm_path
[0]) {
2906 strncpy(lus
->root_shm_path
, session
->shm_path
,
2907 sizeof(lus
->root_shm_path
));
2908 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2909 strncpy(lus
->shm_path
, session
->shm_path
,
2910 sizeof(lus
->shm_path
));
2911 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2912 strncat(lus
->shm_path
, "/ust",
2913 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2915 /* Copy session output to the newly created UST session */
2916 ret
= copy_session_consumer(domain
->type
, session
);
2917 if (ret
!= LTTNG_OK
) {
2925 session
->ust_session
= NULL
;
2930 * Create a kernel tracer session then create the default channel.
2932 static int create_kernel_session(struct ltt_session
*session
)
2936 DBG("Creating kernel session");
2938 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2940 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2944 /* Code flow safety */
2945 assert(session
->kernel_session
);
2947 /* Copy session output to the newly created Kernel session */
2948 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2949 if (ret
!= LTTNG_OK
) {
2953 /* Create directory(ies) on local filesystem. */
2954 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2955 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2956 ret
= run_as_mkdir_recursive(
2957 session
->kernel_session
->consumer
->dst
.trace_path
,
2958 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2960 if (errno
!= EEXIST
) {
2961 ERR("Trace directory creation error");
2967 session
->kernel_session
->uid
= session
->uid
;
2968 session
->kernel_session
->gid
= session
->gid
;
2969 session
->kernel_session
->output_traces
= session
->output_traces
;
2970 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2975 trace_kernel_destroy_session(session
->kernel_session
);
2976 session
->kernel_session
= NULL
;
2981 * Count number of session permitted by uid/gid.
2983 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2986 struct ltt_session
*session
;
2988 DBG("Counting number of available session for UID %d GID %d",
2990 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2992 * Only list the sessions the user can control.
2994 if (!session_access_ok(session
, uid
, gid
)) {
3003 * Process the command requested by the lttng client within the command
3004 * context structure. This function make sure that the return structure (llm)
3005 * is set and ready for transmission before returning.
3007 * Return any error encountered or 0 for success.
3009 * "sock" is only used for special-case var. len data.
3011 * Should *NOT* be called with RCU read-side lock held.
3013 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
3017 int need_tracing_session
= 1;
3020 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
3022 assert(!rcu_read_ongoing());
3026 switch (cmd_ctx
->lsm
->cmd_type
) {
3027 case LTTNG_CREATE_SESSION
:
3028 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3029 case LTTNG_CREATE_SESSION_LIVE
:
3030 case LTTNG_DESTROY_SESSION
:
3031 case LTTNG_LIST_SESSIONS
:
3032 case LTTNG_LIST_DOMAINS
:
3033 case LTTNG_START_TRACE
:
3034 case LTTNG_STOP_TRACE
:
3035 case LTTNG_DATA_PENDING
:
3036 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3037 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3038 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3039 case LTTNG_SNAPSHOT_RECORD
:
3040 case LTTNG_SAVE_SESSION
:
3041 case LTTNG_SET_SESSION_SHM_PATH
:
3042 case LTTNG_METADATA_REGENERATE
:
3049 if (opt_no_kernel
&& need_domain
3050 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
3052 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3054 ret
= LTTNG_ERR_KERN_NA
;
3059 /* Deny register consumer if we already have a spawned consumer. */
3060 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
3061 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3062 if (kconsumer_data
.pid
> 0) {
3063 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3064 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3067 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3071 * Check for command that don't needs to allocate a returned payload. We do
3072 * this here so we don't have to make the call for no payload at each
3075 switch(cmd_ctx
->lsm
->cmd_type
) {
3076 case LTTNG_LIST_SESSIONS
:
3077 case LTTNG_LIST_TRACEPOINTS
:
3078 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3079 case LTTNG_LIST_DOMAINS
:
3080 case LTTNG_LIST_CHANNELS
:
3081 case LTTNG_LIST_EVENTS
:
3082 case LTTNG_LIST_SYSCALLS
:
3083 case LTTNG_LIST_TRACKER_PIDS
:
3086 /* Setup lttng message with no payload */
3087 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
3089 /* This label does not try to unlock the session */
3090 goto init_setup_error
;
3094 /* Commands that DO NOT need a session. */
3095 switch (cmd_ctx
->lsm
->cmd_type
) {
3096 case LTTNG_CREATE_SESSION
:
3097 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3098 case LTTNG_CREATE_SESSION_LIVE
:
3099 case LTTNG_CALIBRATE
:
3100 case LTTNG_LIST_SESSIONS
:
3101 case LTTNG_LIST_TRACEPOINTS
:
3102 case LTTNG_LIST_SYSCALLS
:
3103 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3104 case LTTNG_SAVE_SESSION
:
3105 need_tracing_session
= 0;
3108 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3110 * We keep the session list lock across _all_ commands
3111 * for now, because the per-session lock does not
3112 * handle teardown properly.
3114 session_lock_list();
3115 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3116 if (cmd_ctx
->session
== NULL
) {
3117 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3120 /* Acquire lock for the session */
3121 session_lock(cmd_ctx
->session
);
3127 * Commands that need a valid session but should NOT create one if none
3128 * exists. Instead of creating one and destroying it when the command is
3129 * handled, process that right before so we save some round trip in useless
3132 switch (cmd_ctx
->lsm
->cmd_type
) {
3133 case LTTNG_DISABLE_CHANNEL
:
3134 case LTTNG_DISABLE_EVENT
:
3135 switch (cmd_ctx
->lsm
->domain
.type
) {
3136 case LTTNG_DOMAIN_KERNEL
:
3137 if (!cmd_ctx
->session
->kernel_session
) {
3138 ret
= LTTNG_ERR_NO_CHANNEL
;
3142 case LTTNG_DOMAIN_JUL
:
3143 case LTTNG_DOMAIN_LOG4J
:
3144 case LTTNG_DOMAIN_PYTHON
:
3145 case LTTNG_DOMAIN_UST
:
3146 if (!cmd_ctx
->session
->ust_session
) {
3147 ret
= LTTNG_ERR_NO_CHANNEL
;
3152 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3164 * Check domain type for specific "pre-action".
3166 switch (cmd_ctx
->lsm
->domain
.type
) {
3167 case LTTNG_DOMAIN_KERNEL
:
3169 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3173 /* Kernel tracer check */
3174 if (kernel_tracer_fd
== -1) {
3175 /* Basically, load kernel tracer modules */
3176 ret
= init_kernel_tracer();
3182 /* Consumer is in an ERROR state. Report back to client */
3183 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3184 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3188 /* Need a session for kernel command */
3189 if (need_tracing_session
) {
3190 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3191 ret
= create_kernel_session(cmd_ctx
->session
);
3193 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3198 /* Start the kernel consumer daemon */
3199 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3200 if (kconsumer_data
.pid
== 0 &&
3201 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3202 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3203 ret
= start_consumerd(&kconsumer_data
);
3205 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3208 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3210 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3214 * The consumer was just spawned so we need to add the socket to
3215 * the consumer output of the session if exist.
3217 ret
= consumer_create_socket(&kconsumer_data
,
3218 cmd_ctx
->session
->kernel_session
->consumer
);
3225 case LTTNG_DOMAIN_JUL
:
3226 case LTTNG_DOMAIN_LOG4J
:
3227 case LTTNG_DOMAIN_PYTHON
:
3228 case LTTNG_DOMAIN_UST
:
3230 if (!ust_app_supported()) {
3231 ret
= LTTNG_ERR_NO_UST
;
3234 /* Consumer is in an ERROR state. Report back to client */
3235 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3236 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3240 if (need_tracing_session
) {
3241 /* Create UST session if none exist. */
3242 if (cmd_ctx
->session
->ust_session
== NULL
) {
3243 ret
= create_ust_session(cmd_ctx
->session
,
3244 &cmd_ctx
->lsm
->domain
);
3245 if (ret
!= LTTNG_OK
) {
3250 /* Start the UST consumer daemons */
3252 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3253 if (consumerd64_bin
[0] != '\0' &&
3254 ustconsumer64_data
.pid
== 0 &&
3255 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3256 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3257 ret
= start_consumerd(&ustconsumer64_data
);
3259 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3260 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3264 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3265 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3267 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3271 * Setup socket for consumer 64 bit. No need for atomic access
3272 * since it was set above and can ONLY be set in this thread.
3274 ret
= consumer_create_socket(&ustconsumer64_data
,
3275 cmd_ctx
->session
->ust_session
->consumer
);
3281 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3282 if (consumerd32_bin
[0] != '\0' &&
3283 ustconsumer32_data
.pid
== 0 &&
3284 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3285 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3286 ret
= start_consumerd(&ustconsumer32_data
);
3288 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3289 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3293 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3294 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3296 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3300 * Setup socket for consumer 64 bit. No need for atomic access
3301 * since it was set above and can ONLY be set in this thread.
3303 ret
= consumer_create_socket(&ustconsumer32_data
,
3304 cmd_ctx
->session
->ust_session
->consumer
);
3316 /* Validate consumer daemon state when start/stop trace command */
3317 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3318 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3319 switch (cmd_ctx
->lsm
->domain
.type
) {
3320 case LTTNG_DOMAIN_NONE
:
3322 case LTTNG_DOMAIN_JUL
:
3323 case LTTNG_DOMAIN_LOG4J
:
3324 case LTTNG_DOMAIN_PYTHON
:
3325 case LTTNG_DOMAIN_UST
:
3326 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3327 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3331 case LTTNG_DOMAIN_KERNEL
:
3332 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3333 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3338 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3344 * Check that the UID or GID match that of the tracing session.
3345 * The root user can interact with all sessions.
3347 if (need_tracing_session
) {
3348 if (!session_access_ok(cmd_ctx
->session
,
3349 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3350 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3351 ret
= LTTNG_ERR_EPERM
;
3357 * Send relayd information to consumer as soon as we have a domain and a
3360 if (cmd_ctx
->session
&& need_domain
) {
3362 * Setup relayd if not done yet. If the relayd information was already
3363 * sent to the consumer, this call will gracefully return.
3365 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3366 if (ret
!= LTTNG_OK
) {
3371 /* Process by command type */
3372 switch (cmd_ctx
->lsm
->cmd_type
) {
3373 case LTTNG_ADD_CONTEXT
:
3376 * An LTTNG_ADD_CONTEXT command might have a supplementary
3377 * payload if the context being added is an application context.
3379 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3380 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3381 char *provider_name
= NULL
, *context_name
= NULL
;
3382 size_t provider_name_len
=
3383 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3384 size_t context_name_len
=
3385 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3387 if (provider_name_len
== 0 || context_name_len
== 0) {
3389 * Application provider and context names MUST
3392 ret
= -LTTNG_ERR_INVALID
;
3396 provider_name
= zmalloc(provider_name_len
+ 1);
3397 if (!provider_name
) {
3398 ret
= -LTTNG_ERR_NOMEM
;
3401 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3404 context_name
= zmalloc(context_name_len
+ 1);
3405 if (!context_name
) {
3406 ret
= -LTTNG_ERR_NOMEM
;
3407 goto error_add_context
;
3409 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3412 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3415 goto error_add_context
;
3418 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3421 goto error_add_context
;
3426 * cmd_add_context assumes ownership of the provider and context
3429 ret
= cmd_add_context(cmd_ctx
->session
,
3430 cmd_ctx
->lsm
->domain
.type
,
3431 cmd_ctx
->lsm
->u
.context
.channel_name
,
3432 &cmd_ctx
->lsm
->u
.context
.ctx
,
3433 kernel_poll_pipe
[1]);
3435 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3436 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3438 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3439 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3445 case LTTNG_DISABLE_CHANNEL
:
3447 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3448 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3451 case LTTNG_DISABLE_EVENT
:
3455 * FIXME: handle filter; for now we just receive the filter's
3456 * bytecode along with the filter expression which are sent by
3457 * liblttng-ctl and discard them.
3459 * This fixes an issue where the client may block while sending
3460 * the filter payload and encounter an error because the session
3461 * daemon closes the socket without ever handling this data.
3463 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3464 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3467 char data
[LTTNG_FILTER_MAX_LEN
];
3469 DBG("Discarding disable event command payload of size %zu", count
);
3471 ret
= lttcomm_recv_unix_sock(sock
, data
,
3472 count
> sizeof(data
) ? sizeof(data
) : count
);
3477 count
-= (size_t) ret
;
3480 /* FIXME: passing packed structure to non-packed pointer */
3481 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3482 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3483 &cmd_ctx
->lsm
->u
.disable
.event
);
3486 case LTTNG_ENABLE_CHANNEL
:
3488 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3489 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
3492 case LTTNG_TRACK_PID
:
3494 ret
= cmd_track_pid(cmd_ctx
->session
,
3495 cmd_ctx
->lsm
->domain
.type
,
3496 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3499 case LTTNG_UNTRACK_PID
:
3501 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3502 cmd_ctx
->lsm
->domain
.type
,
3503 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3506 case LTTNG_ENABLE_EVENT
:
3508 struct lttng_event_exclusion
*exclusion
= NULL
;
3509 struct lttng_filter_bytecode
*bytecode
= NULL
;
3510 char *filter_expression
= NULL
;
3512 /* Handle exclusion events and receive it from the client. */
3513 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3514 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3516 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3517 (count
* LTTNG_SYMBOL_NAME_LEN
));
3519 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3523 DBG("Receiving var len exclusion event list from client ...");
3524 exclusion
->count
= count
;
3525 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3526 count
* LTTNG_SYMBOL_NAME_LEN
);
3528 DBG("Nothing recv() from client var len data... continuing");
3531 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3536 /* Get filter expression from client. */
3537 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3538 size_t expression_len
=
3539 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3541 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3542 ret
= LTTNG_ERR_FILTER_INVAL
;
3547 filter_expression
= zmalloc(expression_len
);
3548 if (!filter_expression
) {
3550 ret
= LTTNG_ERR_FILTER_NOMEM
;
3554 /* Receive var. len. data */
3555 DBG("Receiving var len filter's expression from client ...");
3556 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3559 DBG("Nothing recv() from client car len data... continuing");
3561 free(filter_expression
);
3563 ret
= LTTNG_ERR_FILTER_INVAL
;
3568 /* Handle filter and get bytecode from client. */
3569 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3570 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3572 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3573 ret
= LTTNG_ERR_FILTER_INVAL
;
3574 free(filter_expression
);
3579 bytecode
= zmalloc(bytecode_len
);
3581 free(filter_expression
);
3583 ret
= LTTNG_ERR_FILTER_NOMEM
;
3587 /* Receive var. len. data */
3588 DBG("Receiving var len filter's bytecode from client ...");
3589 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3591 DBG("Nothing recv() from client car len data... continuing");
3593 free(filter_expression
);
3596 ret
= LTTNG_ERR_FILTER_INVAL
;
3600 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3601 free(filter_expression
);
3604 ret
= LTTNG_ERR_FILTER_INVAL
;
3609 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3610 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3611 &cmd_ctx
->lsm
->u
.enable
.event
,
3612 filter_expression
, bytecode
, exclusion
,
3613 kernel_poll_pipe
[1]);
3616 case LTTNG_LIST_TRACEPOINTS
:
3618 struct lttng_event
*events
;
3621 session_lock_list();
3622 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3623 session_unlock_list();
3624 if (nb_events
< 0) {
3625 /* Return value is a negative lttng_error_code. */
3631 * Setup lttng message with payload size set to the event list size in
3632 * bytes and then copy list into the llm payload.
3634 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3635 sizeof(struct lttng_event
) * nb_events
);
3645 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3647 struct lttng_event_field
*fields
;
3650 session_lock_list();
3651 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3653 session_unlock_list();
3654 if (nb_fields
< 0) {
3655 /* Return value is a negative lttng_error_code. */
3661 * Setup lttng message with payload size set to the event list size in
3662 * bytes and then copy list into the llm payload.
3664 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3665 sizeof(struct lttng_event_field
) * nb_fields
);
3675 case LTTNG_LIST_SYSCALLS
:
3677 struct lttng_event
*events
;
3680 nb_events
= cmd_list_syscalls(&events
);
3681 if (nb_events
< 0) {
3682 /* Return value is a negative lttng_error_code. */
3688 * Setup lttng message with payload size set to the event list size in
3689 * bytes and then copy list into the llm payload.
3691 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3692 sizeof(struct lttng_event
) * nb_events
);
3702 case LTTNG_LIST_TRACKER_PIDS
:
3704 int32_t *pids
= NULL
;
3707 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3708 cmd_ctx
->lsm
->domain
.type
, &pids
);
3710 /* Return value is a negative lttng_error_code. */
3716 * Setup lttng message with payload size set to the event list size in
3717 * bytes and then copy list into the llm payload.
3719 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3720 sizeof(int32_t) * nr_pids
);
3730 case LTTNG_SET_CONSUMER_URI
:
3733 struct lttng_uri
*uris
;
3735 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3736 len
= nb_uri
* sizeof(struct lttng_uri
);
3739 ret
= LTTNG_ERR_INVALID
;
3743 uris
= zmalloc(len
);
3745 ret
= LTTNG_ERR_FATAL
;
3749 /* Receive variable len data */
3750 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3751 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3753 DBG("No URIs received from client... continuing");
3755 ret
= LTTNG_ERR_SESSION_FAIL
;
3760 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3762 if (ret
!= LTTNG_OK
) {
3769 case LTTNG_START_TRACE
:
3771 ret
= cmd_start_trace(cmd_ctx
->session
);
3774 case LTTNG_STOP_TRACE
:
3776 ret
= cmd_stop_trace(cmd_ctx
->session
);
3779 case LTTNG_CREATE_SESSION
:
3782 struct lttng_uri
*uris
= NULL
;
3784 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3785 len
= nb_uri
* sizeof(struct lttng_uri
);
3788 uris
= zmalloc(len
);
3790 ret
= LTTNG_ERR_FATAL
;
3794 /* Receive variable len data */
3795 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3796 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3798 DBG("No URIs received from client... continuing");
3800 ret
= LTTNG_ERR_SESSION_FAIL
;
3805 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3806 DBG("Creating session with ONE network URI is a bad call");
3807 ret
= LTTNG_ERR_SESSION_FAIL
;
3813 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3814 &cmd_ctx
->creds
, 0);
3820 case LTTNG_DESTROY_SESSION
:
3822 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3824 /* Set session to NULL so we do not unlock it after free. */
3825 cmd_ctx
->session
= NULL
;
3828 case LTTNG_LIST_DOMAINS
:
3831 struct lttng_domain
*domains
= NULL
;
3833 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3835 /* Return value is a negative lttng_error_code. */
3840 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3841 nb_dom
* sizeof(struct lttng_domain
));
3851 case LTTNG_LIST_CHANNELS
:
3853 ssize_t payload_size
;
3854 struct lttng_channel
*channels
= NULL
;
3856 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3857 cmd_ctx
->session
, &channels
);
3858 if (payload_size
< 0) {
3859 /* Return value is a negative lttng_error_code. */
3860 ret
= -payload_size
;
3864 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3875 case LTTNG_LIST_EVENTS
:
3878 struct lttng_event
*events
= NULL
;
3879 struct lttcomm_event_command_header cmd_header
;
3882 memset(&cmd_header
, 0, sizeof(cmd_header
));
3883 /* Extended infos are included at the end of events */
3884 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3885 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3886 &events
, &total_size
);
3889 /* Return value is a negative lttng_error_code. */
3894 cmd_header
.nb_events
= nb_event
;
3895 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3896 &cmd_header
, sizeof(cmd_header
));
3906 case LTTNG_LIST_SESSIONS
:
3908 unsigned int nr_sessions
;
3909 void *sessions_payload
;
3912 session_lock_list();
3913 nr_sessions
= lttng_sessions_count(
3914 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3915 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3916 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
3917 sessions_payload
= zmalloc(payload_len
);
3919 if (!sessions_payload
) {
3920 session_unlock_list();
3925 cmd_list_lttng_sessions(sessions_payload
,
3926 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3927 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3928 session_unlock_list();
3930 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
3932 free(sessions_payload
);
3941 case LTTNG_CALIBRATE
:
3943 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3944 &cmd_ctx
->lsm
->u
.calibrate
);
3947 case LTTNG_REGISTER_CONSUMER
:
3949 struct consumer_data
*cdata
;
3951 switch (cmd_ctx
->lsm
->domain
.type
) {
3952 case LTTNG_DOMAIN_KERNEL
:
3953 cdata
= &kconsumer_data
;
3956 ret
= LTTNG_ERR_UND
;
3960 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3961 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3964 case LTTNG_DATA_PENDING
:
3967 uint8_t pending_ret_byte
;
3969 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
3974 * This function may returns 0 or 1 to indicate whether or not
3975 * there is data pending. In case of error, it should return an
3976 * LTTNG_ERR code. However, some code paths may still return
3977 * a nondescript error code, which we handle by returning an
3980 if (pending_ret
== 0 || pending_ret
== 1) {
3982 * ret will be set to LTTNG_OK at the end of
3985 } else if (pending_ret
< 0) {
3986 ret
= LTTNG_ERR_UNK
;
3993 pending_ret_byte
= (uint8_t) pending_ret
;
3995 /* 1 byte to return whether or not data is pending */
3996 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
3997 &pending_ret_byte
, 1);
4006 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
4008 struct lttcomm_lttng_output_id reply
;
4010 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
4011 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
4012 if (ret
!= LTTNG_OK
) {
4016 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
4022 /* Copy output list into message payload */
4026 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
4028 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
4029 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
4032 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
4035 struct lttng_snapshot_output
*outputs
= NULL
;
4037 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
4038 if (nb_output
< 0) {
4043 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
4044 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
4045 nb_output
* sizeof(struct lttng_snapshot_output
));
4055 case LTTNG_SNAPSHOT_RECORD
:
4057 ret
= cmd_snapshot_record(cmd_ctx
->session
,
4058 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
4059 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
4062 case LTTNG_CREATE_SESSION_SNAPSHOT
:
4065 struct lttng_uri
*uris
= NULL
;
4067 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4068 len
= nb_uri
* sizeof(struct lttng_uri
);
4071 uris
= zmalloc(len
);
4073 ret
= LTTNG_ERR_FATAL
;
4077 /* Receive variable len data */
4078 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4079 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4081 DBG("No URIs received from client... continuing");
4083 ret
= LTTNG_ERR_SESSION_FAIL
;
4088 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4089 DBG("Creating session with ONE network URI is a bad call");
4090 ret
= LTTNG_ERR_SESSION_FAIL
;
4096 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
4097 nb_uri
, &cmd_ctx
->creds
);
4101 case LTTNG_CREATE_SESSION_LIVE
:
4104 struct lttng_uri
*uris
= NULL
;
4106 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4107 len
= nb_uri
* sizeof(struct lttng_uri
);
4110 uris
= zmalloc(len
);
4112 ret
= LTTNG_ERR_FATAL
;
4116 /* Receive variable len data */
4117 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4118 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4120 DBG("No URIs received from client... continuing");
4122 ret
= LTTNG_ERR_SESSION_FAIL
;
4127 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4128 DBG("Creating session with ONE network URI is a bad call");
4129 ret
= LTTNG_ERR_SESSION_FAIL
;
4135 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4136 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4140 case LTTNG_SAVE_SESSION
:
4142 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4146 case LTTNG_SET_SESSION_SHM_PATH
:
4148 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4149 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4152 case LTTNG_METADATA_REGENERATE
:
4154 ret
= cmd_metadata_regenerate(cmd_ctx
->session
);
4158 ret
= LTTNG_ERR_UND
;
4163 if (cmd_ctx
->llm
== NULL
) {
4164 DBG("Missing llm structure. Allocating one.");
4165 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4169 /* Set return code */
4170 cmd_ctx
->llm
->ret_code
= ret
;
4172 if (cmd_ctx
->session
) {
4173 session_unlock(cmd_ctx
->session
);
4175 if (need_tracing_session
) {
4176 session_unlock_list();
4179 assert(!rcu_read_ongoing());
4184 * Thread managing health check socket.
4186 static void *thread_manage_health(void *data
)
4188 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4189 uint32_t revents
, nb_fd
;
4190 struct lttng_poll_event events
;
4191 struct health_comm_msg msg
;
4192 struct health_comm_reply reply
;
4194 DBG("[thread] Manage health check started");
4196 rcu_register_thread();
4198 /* We might hit an error path before this is created. */
4199 lttng_poll_init(&events
);
4201 /* Create unix socket */
4202 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
4204 ERR("Unable to create health check Unix socket");
4210 /* lttng health client socket path permissions */
4211 ret
= chown(health_unix_sock_path
, 0,
4212 utils_get_group_id(tracing_group_name
));
4214 ERR("Unable to set group on %s", health_unix_sock_path
);
4220 ret
= chmod(health_unix_sock_path
,
4221 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4223 ERR("Unable to set permissions on %s", health_unix_sock_path
);
4231 * Set the CLOEXEC flag. Return code is useless because either way, the
4234 (void) utils_set_fd_cloexec(sock
);
4236 ret
= lttcomm_listen_unix_sock(sock
);
4242 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4243 * more will be added to this poll set.
4245 ret
= sessiond_set_thread_pollset(&events
, 2);
4250 /* Add the application registration socket */
4251 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4256 sessiond_notify_ready();
4259 DBG("Health check ready");
4261 /* Inifinite blocking call, waiting for transmission */
4263 ret
= lttng_poll_wait(&events
, -1);
4266 * Restart interrupted system call.
4268 if (errno
== EINTR
) {
4276 for (i
= 0; i
< nb_fd
; i
++) {
4277 /* Fetch once the poll data */
4278 revents
= LTTNG_POLL_GETEV(&events
, i
);
4279 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4282 /* No activity for this FD (poll implementation). */
4286 /* Thread quit pipe has been closed. Killing thread. */
4287 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4293 /* Event on the registration socket */
4294 if (pollfd
== sock
) {
4295 if (revents
& LPOLLIN
) {
4297 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4298 ERR("Health socket poll error");
4301 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4307 new_sock
= lttcomm_accept_unix_sock(sock
);
4313 * Set the CLOEXEC flag. Return code is useless because either way, the
4316 (void) utils_set_fd_cloexec(new_sock
);
4318 DBG("Receiving data from client for health...");
4319 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4321 DBG("Nothing recv() from client... continuing");
4322 ret
= close(new_sock
);
4330 rcu_thread_online();
4332 memset(&reply
, 0, sizeof(reply
));
4333 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4335 * health_check_state returns 0 if health is
4338 if (!health_check_state(health_sessiond
, i
)) {
4339 reply
.ret_code
|= 1ULL << i
;
4343 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4345 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4347 ERR("Failed to send health data back to client");
4350 /* End of transmission */
4351 ret
= close(new_sock
);
4361 ERR("Health error occurred in %s", __func__
);
4363 DBG("Health check thread dying");
4364 unlink(health_unix_sock_path
);
4372 lttng_poll_clean(&events
);
4374 rcu_unregister_thread();
4379 * This thread manage all clients request using the unix client socket for
4382 static void *thread_manage_clients(void *data
)
4384 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4386 uint32_t revents
, nb_fd
;
4387 struct command_ctx
*cmd_ctx
= NULL
;
4388 struct lttng_poll_event events
;
4390 DBG("[thread] Manage client started");
4392 rcu_register_thread();
4394 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4396 health_code_update();
4398 ret
= lttcomm_listen_unix_sock(client_sock
);
4404 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4405 * more will be added to this poll set.
4407 ret
= sessiond_set_thread_pollset(&events
, 2);
4409 goto error_create_poll
;
4412 /* Add the application registration socket */
4413 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4418 sessiond_notify_ready();
4419 ret
= sem_post(&load_info
->message_thread_ready
);
4421 PERROR("sem_post message_thread_ready");
4425 /* This testpoint is after we signal readiness to the parent. */
4426 if (testpoint(sessiond_thread_manage_clients
)) {
4430 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4434 health_code_update();
4437 DBG("Accepting client command ...");
4439 /* Inifinite blocking call, waiting for transmission */
4441 health_poll_entry();
4442 ret
= lttng_poll_wait(&events
, -1);
4446 * Restart interrupted system call.
4448 if (errno
== EINTR
) {
4456 for (i
= 0; i
< nb_fd
; i
++) {
4457 /* Fetch once the poll data */
4458 revents
= LTTNG_POLL_GETEV(&events
, i
);
4459 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4461 health_code_update();
4464 /* No activity for this FD (poll implementation). */
4468 /* Thread quit pipe has been closed. Killing thread. */
4469 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4475 /* Event on the registration socket */
4476 if (pollfd
== client_sock
) {
4477 if (revents
& LPOLLIN
) {
4479 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4480 ERR("Client socket poll error");
4483 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4489 DBG("Wait for client response");
4491 health_code_update();
4493 sock
= lttcomm_accept_unix_sock(client_sock
);
4499 * Set the CLOEXEC flag. Return code is useless because either way, the
4502 (void) utils_set_fd_cloexec(sock
);
4504 /* Set socket option for credentials retrieval */
4505 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4510 /* Allocate context command to process the client request */
4511 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4512 if (cmd_ctx
== NULL
) {
4513 PERROR("zmalloc cmd_ctx");
4517 /* Allocate data buffer for reception */
4518 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4519 if (cmd_ctx
->lsm
== NULL
) {
4520 PERROR("zmalloc cmd_ctx->lsm");
4524 cmd_ctx
->llm
= NULL
;
4525 cmd_ctx
->session
= NULL
;
4527 health_code_update();
4530 * Data is received from the lttng client. The struct
4531 * lttcomm_session_msg (lsm) contains the command and data request of
4534 DBG("Receiving data from client ...");
4535 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4536 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4538 DBG("Nothing recv() from client... continuing");
4544 clean_command_ctx(&cmd_ctx
);
4548 health_code_update();
4550 // TODO: Validate cmd_ctx including sanity check for
4551 // security purpose.
4553 rcu_thread_online();
4555 * This function dispatch the work to the kernel or userspace tracer
4556 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4557 * informations for the client. The command context struct contains
4558 * everything this function may needs.
4560 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4561 rcu_thread_offline();
4569 * TODO: Inform client somehow of the fatal error. At
4570 * this point, ret < 0 means that a zmalloc failed
4571 * (ENOMEM). Error detected but still accept
4572 * command, unless a socket error has been
4575 clean_command_ctx(&cmd_ctx
);
4579 health_code_update();
4581 DBG("Sending response (size: %d, retcode: %s (%d))",
4582 cmd_ctx
->lttng_msg_size
,
4583 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4584 cmd_ctx
->llm
->ret_code
);
4585 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4587 ERR("Failed to send data back to client");
4590 /* End of transmission */
4597 clean_command_ctx(&cmd_ctx
);
4599 health_code_update();
4611 lttng_poll_clean(&events
);
4612 clean_command_ctx(&cmd_ctx
);
4616 unlink(client_unix_sock_path
);
4617 if (client_sock
>= 0) {
4618 ret
= close(client_sock
);
4626 ERR("Health error occurred in %s", __func__
);
4629 health_unregister(health_sessiond
);
4631 DBG("Client thread dying");
4633 rcu_unregister_thread();
4636 * Since we are creating the consumer threads, we own them, so we need
4637 * to join them before our thread exits.
4639 ret
= join_consumer_thread(&kconsumer_data
);
4642 PERROR("join_consumer");
4645 ret
= join_consumer_thread(&ustconsumer32_data
);
4648 PERROR("join_consumer ust32");
4651 ret
= join_consumer_thread(&ustconsumer64_data
);
4654 PERROR("join_consumer ust64");
4659 static int string_match(const char *str1
, const char *str2
)
4661 return (str1
&& str2
) && !strcmp(str1
, str2
);
4665 * Take an option from the getopt output and set it in the right variable to be
4668 * Return 0 on success else a negative value.
4670 static int set_option(int opt
, const char *arg
, const char *optname
)
4674 if (string_match(optname
, "client-sock") || opt
== 'c') {
4675 if (!arg
|| *arg
== '\0') {
4679 if (lttng_is_setuid_setgid()) {
4680 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4681 "-c, --client-sock");
4683 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", arg
);
4685 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4686 if (!arg
|| *arg
== '\0') {
4690 if (lttng_is_setuid_setgid()) {
4691 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4694 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", arg
);
4696 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4698 } else if (string_match(optname
, "background") || opt
== 'b') {
4700 } else if (string_match(optname
, "group") || opt
== 'g') {
4701 if (!arg
|| *arg
== '\0') {
4705 if (lttng_is_setuid_setgid()) {
4706 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4710 * If the override option is set, the pointer points to a
4711 * *non* const thus freeing it even though the variable type is
4714 if (tracing_group_name_override
) {
4715 free((void *) tracing_group_name
);
4717 tracing_group_name
= strdup(arg
);
4718 if (!tracing_group_name
) {
4722 tracing_group_name_override
= 1;
4724 } else if (string_match(optname
, "help") || opt
== 'h') {
4725 ret
= utils_show_man_page(8, "lttng-sessiond");
4727 ERR("Cannot view man page lttng-sessiond(8)");
4730 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
4731 } else if (string_match(optname
, "version") || opt
== 'V') {
4732 fprintf(stdout
, "%s\n", VERSION
);
4734 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4736 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4737 if (!arg
|| *arg
== '\0') {
4741 if (lttng_is_setuid_setgid()) {
4742 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4743 "--kconsumerd-err-sock");
4745 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4747 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4748 if (!arg
|| *arg
== '\0') {
4752 if (lttng_is_setuid_setgid()) {
4753 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4754 "--kconsumerd-cmd-sock");
4756 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4758 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4759 if (!arg
|| *arg
== '\0') {
4763 if (lttng_is_setuid_setgid()) {
4764 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4765 "--ustconsumerd64-err-sock");
4767 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4769 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4770 if (!arg
|| *arg
== '\0') {
4774 if (lttng_is_setuid_setgid()) {
4775 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4776 "--ustconsumerd64-cmd-sock");
4778 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4780 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4781 if (!arg
|| *arg
== '\0') {
4785 if (lttng_is_setuid_setgid()) {
4786 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4787 "--ustconsumerd32-err-sock");
4789 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4791 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4792 if (!arg
|| *arg
== '\0') {
4796 if (lttng_is_setuid_setgid()) {
4797 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4798 "--ustconsumerd32-cmd-sock");
4800 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4802 } else if (string_match(optname
, "no-kernel")) {
4804 } else if (string_match(optname
, "quiet") || opt
== 'q') {
4805 lttng_opt_quiet
= 1;
4806 } else if (string_match(optname
, "verbose") || opt
== 'v') {
4807 /* Verbose level can increase using multiple -v */
4809 /* Value obtained from config file */
4810 lttng_opt_verbose
= config_parse_value(arg
);
4812 /* -v used on command line */
4813 lttng_opt_verbose
++;
4815 /* Clamp value to [0, 3] */
4816 lttng_opt_verbose
= lttng_opt_verbose
< 0 ? 0 :
4817 (lttng_opt_verbose
<= 3 ? lttng_opt_verbose
: 3);
4818 } else if (string_match(optname
, "verbose-consumer")) {
4820 opt_verbose_consumer
= config_parse_value(arg
);
4822 opt_verbose_consumer
++;
4824 } else if (string_match(optname
, "consumerd32-path")) {
4825 if (!arg
|| *arg
== '\0') {
4829 if (lttng_is_setuid_setgid()) {
4830 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4831 "--consumerd32-path");
4833 if (consumerd32_bin_override
) {
4834 free((void *) consumerd32_bin
);
4836 consumerd32_bin
= strdup(arg
);
4837 if (!consumerd32_bin
) {
4841 consumerd32_bin_override
= 1;
4843 } else if (string_match(optname
, "consumerd32-libdir")) {
4844 if (!arg
|| *arg
== '\0') {
4848 if (lttng_is_setuid_setgid()) {
4849 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4850 "--consumerd32-libdir");
4852 if (consumerd32_libdir_override
) {
4853 free((void *) consumerd32_libdir
);
4855 consumerd32_libdir
= strdup(arg
);
4856 if (!consumerd32_libdir
) {
4860 consumerd32_libdir_override
= 1;
4862 } else if (string_match(optname
, "consumerd64-path")) {
4863 if (!arg
|| *arg
== '\0') {
4867 if (lttng_is_setuid_setgid()) {
4868 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4869 "--consumerd64-path");
4871 if (consumerd64_bin_override
) {
4872 free((void *) consumerd64_bin
);
4874 consumerd64_bin
= strdup(arg
);
4875 if (!consumerd64_bin
) {
4879 consumerd64_bin_override
= 1;
4881 } else if (string_match(optname
, "consumerd64-libdir")) {
4882 if (!arg
|| *arg
== '\0') {
4886 if (lttng_is_setuid_setgid()) {
4887 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4888 "--consumerd64-libdir");
4890 if (consumerd64_libdir_override
) {
4891 free((void *) consumerd64_libdir
);
4893 consumerd64_libdir
= strdup(arg
);
4894 if (!consumerd64_libdir
) {
4898 consumerd64_libdir_override
= 1;
4900 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
4901 if (!arg
|| *arg
== '\0') {
4905 if (lttng_is_setuid_setgid()) {
4906 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4910 opt_pidfile
= strdup(arg
);
4916 } else if (string_match(optname
, "agent-tcp-port")) {
4917 if (!arg
|| *arg
== '\0') {
4921 if (lttng_is_setuid_setgid()) {
4922 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4923 "--agent-tcp-port");
4932 v
= strtoul(arg
, NULL
, 0);
4933 if (errno
!= 0 || !isdigit(arg
[0])) {
4934 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
4937 if (v
== 0 || v
>= 65535) {
4938 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
4941 agent_tcp_port
= (uint32_t) v
;
4942 DBG3("Agent TCP port set to non default: %u", agent_tcp_port
);
4944 } else if (string_match(optname
, "load") || opt
== 'l') {
4945 if (!arg
|| *arg
== '\0') {
4949 if (lttng_is_setuid_setgid()) {
4950 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4953 free(opt_load_session_path
);
4954 opt_load_session_path
= strdup(arg
);
4955 if (!opt_load_session_path
) {
4960 } else if (string_match(optname
, "kmod-probes")) {
4961 if (!arg
|| *arg
== '\0') {
4965 if (lttng_is_setuid_setgid()) {
4966 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4969 free(kmod_probes_list
);
4970 kmod_probes_list
= strdup(arg
);
4971 if (!kmod_probes_list
) {
4976 } else if (string_match(optname
, "extra-kmod-probes")) {
4977 if (!arg
|| *arg
== '\0') {
4981 if (lttng_is_setuid_setgid()) {
4982 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4983 "--extra-kmod-probes");
4985 free(kmod_extra_probes_list
);
4986 kmod_extra_probes_list
= strdup(arg
);
4987 if (!kmod_extra_probes_list
) {
4992 } else if (string_match(optname
, "config") || opt
== 'f') {
4993 /* This is handled in set_options() thus silent skip. */
4996 /* Unknown option or other error.
4997 * Error is printed by getopt, just return */
5002 if (ret
== -EINVAL
) {
5003 const char *opt_name
= "unknown";
5006 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
5008 if (opt
== long_options
[i
].val
) {
5009 opt_name
= long_options
[i
].name
;
5014 WARN("Invalid argument provided for option \"%s\", using default value.",
5022 * config_entry_handler_cb used to handle options read from a config file.
5023 * See config_entry_handler_cb comment in common/config/session-config.h for the
5024 * return value conventions.
5026 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
5030 if (!entry
|| !entry
->name
|| !entry
->value
) {
5035 /* Check if the option is to be ignored */
5036 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
5037 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
5042 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
5045 /* Ignore if not fully matched. */
5046 if (strcmp(entry
->name
, long_options
[i
].name
)) {
5051 * If the option takes no argument on the command line, we have to
5052 * check if the value is "true". We support non-zero numeric values,
5055 if (!long_options
[i
].has_arg
) {
5056 ret
= config_parse_value(entry
->value
);
5059 WARN("Invalid configuration value \"%s\" for option %s",
5060 entry
->value
, entry
->name
);
5062 /* False, skip boolean config option. */
5067 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
5071 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
5078 * daemon configuration loading and argument parsing
5080 static int set_options(int argc
, char **argv
)
5082 int ret
= 0, c
= 0, option_index
= 0;
5083 int orig_optopt
= optopt
, orig_optind
= optind
;
5085 const char *config_path
= NULL
;
5087 optstring
= utils_generate_optstring(long_options
,
5088 sizeof(long_options
) / sizeof(struct option
));
5094 /* Check for the --config option */
5095 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
5096 &option_index
)) != -1) {
5100 } else if (c
!= 'f') {
5101 /* if not equal to --config option. */
5105 if (lttng_is_setuid_setgid()) {
5106 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5109 config_path
= utils_expand_path(optarg
);
5111 ERR("Failed to resolve path: %s", optarg
);
5116 ret
= config_get_section_entries(config_path
, config_section_name
,
5117 config_entry_handler
, NULL
);
5120 ERR("Invalid configuration option at line %i", ret
);
5126 /* Reset getopt's global state */
5127 optopt
= orig_optopt
;
5128 optind
= orig_optind
;
5132 * getopt_long() will not set option_index if it encounters a
5135 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5142 * Pass NULL as the long option name if popt left the index
5145 ret
= set_option(c
, optarg
,
5146 option_index
< 0 ? NULL
:
5147 long_options
[option_index
].name
);
5159 * Creates the two needed socket by the daemon.
5160 * apps_sock - The communication socket for all UST apps.
5161 * client_sock - The communication of the cli tool (lttng).
5163 static int init_daemon_socket(void)
5168 old_umask
= umask(0);
5170 /* Create client tool unix socket */
5171 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
5172 if (client_sock
< 0) {
5173 ERR("Create unix sock failed: %s", client_unix_sock_path
);
5178 /* Set the cloexec flag */
5179 ret
= utils_set_fd_cloexec(client_sock
);
5181 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5182 "Continuing but note that the consumer daemon will have a "
5183 "reference to this socket on exec()", client_sock
);
5186 /* File permission MUST be 660 */
5187 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5189 ERR("Set file permissions failed: %s", client_unix_sock_path
);
5194 /* Create the application unix socket */
5195 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
5196 if (apps_sock
< 0) {
5197 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
5202 /* Set the cloexec flag */
5203 ret
= utils_set_fd_cloexec(apps_sock
);
5205 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5206 "Continuing but note that the consumer daemon will have a "
5207 "reference to this socket on exec()", apps_sock
);
5210 /* File permission MUST be 666 */
5211 ret
= chmod(apps_unix_sock_path
,
5212 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5214 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
5219 DBG3("Session daemon client socket %d and application socket %d created",
5220 client_sock
, apps_sock
);
5228 * Check if the global socket is available, and if a daemon is answering at the
5229 * other side. If yes, error is returned.
5231 static int check_existing_daemon(void)
5233 /* Is there anybody out there ? */
5234 if (lttng_session_daemon_alive()) {
5242 * Set the tracing group gid onto the client socket.
5244 * Race window between mkdir and chown is OK because we are going from more
5245 * permissive (root.root) to less permissive (root.tracing).
5247 static int set_permissions(char *rundir
)
5252 gid
= utils_get_group_id(tracing_group_name
);
5254 /* Set lttng run dir */
5255 ret
= chown(rundir
, 0, gid
);
5257 ERR("Unable to set group on %s", rundir
);
5262 * Ensure all applications and tracing group can search the run
5263 * dir. Allow everyone to read the directory, since it does not
5264 * buy us anything to hide its content.
5266 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5268 ERR("Unable to set permissions on %s", rundir
);
5272 /* lttng client socket path */
5273 ret
= chown(client_unix_sock_path
, 0, gid
);
5275 ERR("Unable to set group on %s", client_unix_sock_path
);
5279 /* kconsumer error socket path */
5280 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5282 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5286 /* 64-bit ustconsumer error socket path */
5287 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5289 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5293 /* 32-bit ustconsumer compat32 error socket path */
5294 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5296 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5300 DBG("All permissions are set");
5306 * Create the lttng run directory needed for all global sockets and pipe.
5308 static int create_lttng_rundir(const char *rundir
)
5312 DBG3("Creating LTTng run directory: %s", rundir
);
5314 ret
= mkdir(rundir
, S_IRWXU
);
5316 if (errno
!= EEXIST
) {
5317 ERR("Unable to create %s", rundir
);
5329 * Setup sockets and directory needed by the kconsumerd communication with the
5332 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
5336 char path
[PATH_MAX
];
5338 switch (consumer_data
->type
) {
5339 case LTTNG_CONSUMER_KERNEL
:
5340 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
5342 case LTTNG_CONSUMER64_UST
:
5343 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
5345 case LTTNG_CONSUMER32_UST
:
5346 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
5349 ERR("Consumer type unknown");
5354 DBG2("Creating consumer directory: %s", path
);
5356 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5358 if (errno
!= EEXIST
) {
5360 ERR("Failed to create %s", path
);
5366 ret
= chown(path
, 0, utils_get_group_id(tracing_group_name
));
5368 ERR("Unable to set group on %s", path
);
5374 /* Create the kconsumerd error unix socket */
5375 consumer_data
->err_sock
=
5376 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5377 if (consumer_data
->err_sock
< 0) {
5378 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5384 * Set the CLOEXEC flag. Return code is useless because either way, the
5387 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5389 PERROR("utils_set_fd_cloexec");
5390 /* continue anyway */
5393 /* File permission MUST be 660 */
5394 ret
= chmod(consumer_data
->err_unix_sock_path
,
5395 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5397 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5407 * Signal handler for the daemon
5409 * Simply stop all worker threads, leaving main() return gracefully after
5410 * joining all threads and calling cleanup().
5412 static void sighandler(int sig
)
5416 DBG("SIGPIPE caught");
5419 DBG("SIGINT caught");
5423 DBG("SIGTERM caught");
5427 CMM_STORE_SHARED(recv_child_signal
, 1);
5435 * Setup signal handler for :
5436 * SIGINT, SIGTERM, SIGPIPE
5438 static int set_signal_handler(void)
5441 struct sigaction sa
;
5444 if ((ret
= sigemptyset(&sigset
)) < 0) {
5445 PERROR("sigemptyset");
5449 sa
.sa_handler
= sighandler
;
5450 sa
.sa_mask
= sigset
;
5452 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5453 PERROR("sigaction");
5457 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5458 PERROR("sigaction");
5462 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5463 PERROR("sigaction");
5467 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5468 PERROR("sigaction");
5472 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5478 * Set open files limit to unlimited. This daemon can open a large number of
5479 * file descriptors in order to consumer multiple kernel traces.
5481 static void set_ulimit(void)
5486 /* The kernel does not allowed an infinite limit for open files */
5487 lim
.rlim_cur
= 65535;
5488 lim
.rlim_max
= 65535;
5490 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5492 PERROR("failed to set open files limit");
5497 * Write pidfile using the rundir and opt_pidfile.
5499 static int write_pidfile(void)
5502 char pidfile_path
[PATH_MAX
];
5507 if (lttng_strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
))) {
5512 /* Build pidfile path from rundir and opt_pidfile. */
5513 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
5514 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
5516 PERROR("snprintf pidfile path");
5522 * Create pid file in rundir.
5524 ret
= utils_create_pid_file(getpid(), pidfile_path
);
5530 * Create lockfile using the rundir and return its fd.
5532 static int create_lockfile(void)
5535 char lockfile_path
[PATH_MAX
];
5537 ret
= generate_lock_file_path(lockfile_path
, sizeof(lockfile_path
));
5542 ret
= utils_create_lock_file(lockfile_path
);
5548 * Write agent TCP port using the rundir.
5550 static int write_agent_port(void)
5553 char path
[PATH_MAX
];
5557 ret
= snprintf(path
, sizeof(path
), "%s/"
5558 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
, rundir
);
5560 PERROR("snprintf agent port path");
5565 * Create TCP agent port file in rundir.
5567 ret
= utils_create_pid_file(agent_tcp_port
, path
);
5576 int main(int argc
, char **argv
)
5578 int ret
= 0, retval
= 0;
5580 const char *home_path
, *env_app_timeout
;
5582 init_kernel_workarounds();
5584 rcu_register_thread();
5586 if (set_signal_handler()) {
5588 goto exit_set_signal_handler
;
5591 setup_consumerd_path();
5593 page_size
= sysconf(_SC_PAGESIZE
);
5594 if (page_size
< 0) {
5595 PERROR("sysconf _SC_PAGESIZE");
5596 page_size
= LONG_MAX
;
5597 WARN("Fallback page size to %ld", page_size
);
5601 * Parse arguments and load the daemon configuration file.
5603 * We have an exit_options exit path to free memory reserved by
5604 * set_options. This is needed because the rest of sessiond_cleanup()
5605 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5606 * depends on set_options.
5609 if (set_options(argc
, argv
)) {
5615 if (opt_daemon
|| opt_background
) {
5618 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5626 * We are in the child. Make sure all other file descriptors are
5627 * closed, in case we are called with more opened file
5628 * descriptors than the standard ones.
5630 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5635 if (run_as_create_worker(argv
[0]) < 0) {
5636 goto exit_create_run_as_worker_cleanup
;
5640 * Starting from here, we can create threads. This needs to be after
5641 * lttng_daemonize due to RCU.
5645 * Initialize the health check subsystem. This call should set the
5646 * appropriate time values.
5648 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5649 if (!health_sessiond
) {
5650 PERROR("health_app_create error");
5652 goto exit_health_sessiond_cleanup
;
5655 if (init_ht_cleanup_quit_pipe()) {
5657 goto exit_ht_cleanup_quit_pipe
;
5660 /* Setup the thread ht_cleanup communication pipe. */
5661 if (utils_create_pipe_cloexec(ht_cleanup_pipe
)) {
5663 goto exit_ht_cleanup_pipe
;
5666 /* Set up max poll set size */
5667 if (lttng_poll_set_max_size()) {
5669 goto exit_set_max_size
;
5672 /* Create thread to clean up RCU hash tables */
5673 ret
= pthread_create(&ht_cleanup_thread
, NULL
,
5674 thread_ht_cleanup
, (void *) NULL
);
5677 PERROR("pthread_create ht_cleanup");
5679 goto exit_ht_cleanup
;
5682 /* Create thread quit pipe */
5683 if (init_thread_quit_pipe()) {
5685 goto exit_init_data
;
5688 /* Check if daemon is UID = 0 */
5689 is_root
= !getuid();
5692 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
5695 goto exit_init_data
;
5698 /* Create global run dir with root access */
5699 if (create_lttng_rundir(rundir
)) {
5701 goto exit_init_data
;
5704 if (strlen(apps_unix_sock_path
) == 0) {
5705 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5706 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
5709 goto exit_init_data
;
5713 if (strlen(client_unix_sock_path
) == 0) {
5714 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5715 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
5718 goto exit_init_data
;
5722 /* Set global SHM for ust */
5723 if (strlen(wait_shm_path
) == 0) {
5724 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5725 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
5728 goto exit_init_data
;
5732 if (strlen(health_unix_sock_path
) == 0) {
5733 ret
= snprintf(health_unix_sock_path
,
5734 sizeof(health_unix_sock_path
),
5735 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
5738 goto exit_init_data
;
5742 /* Setup kernel consumerd path */
5743 ret
= snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
5744 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
5747 goto exit_init_data
;
5749 ret
= snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
5750 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
5753 goto exit_init_data
;
5756 DBG2("Kernel consumer err path: %s",
5757 kconsumer_data
.err_unix_sock_path
);
5758 DBG2("Kernel consumer cmd path: %s",
5759 kconsumer_data
.cmd_unix_sock_path
);
5761 home_path
= utils_get_home_dir();
5762 if (home_path
== NULL
) {
5763 /* TODO: Add --socket PATH option */
5764 ERR("Can't get HOME directory for sockets creation.");
5766 goto exit_init_data
;
5770 * Create rundir from home path. This will create something like
5773 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
5776 goto exit_init_data
;
5779 if (create_lttng_rundir(rundir
)) {
5781 goto exit_init_data
;
5784 if (strlen(apps_unix_sock_path
) == 0) {
5785 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5786 DEFAULT_HOME_APPS_UNIX_SOCK
,
5790 goto exit_init_data
;
5794 /* Set the cli tool unix socket path */
5795 if (strlen(client_unix_sock_path
) == 0) {
5796 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5797 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
5801 goto exit_init_data
;
5805 /* Set global SHM for ust */
5806 if (strlen(wait_shm_path
) == 0) {
5807 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5808 DEFAULT_HOME_APPS_WAIT_SHM_PATH
,
5812 goto exit_init_data
;
5816 /* Set health check Unix path */
5817 if (strlen(health_unix_sock_path
) == 0) {
5818 ret
= snprintf(health_unix_sock_path
,
5819 sizeof(health_unix_sock_path
),
5820 DEFAULT_HOME_HEALTH_UNIX_SOCK
,
5824 goto exit_init_data
;
5829 lockfile_fd
= create_lockfile();
5830 if (lockfile_fd
< 0) {
5832 goto exit_init_data
;
5835 /* Set consumer initial state */
5836 kernel_consumerd_state
= CONSUMER_STOPPED
;
5837 ust_consumerd_state
= CONSUMER_STOPPED
;
5839 DBG("Client socket path %s", client_unix_sock_path
);
5840 DBG("Application socket path %s", apps_unix_sock_path
);
5841 DBG("Application wait path %s", wait_shm_path
);
5842 DBG("LTTng run directory path: %s", rundir
);
5844 /* 32 bits consumerd path setup */
5845 ret
= snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
5846 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
5848 PERROR("snprintf 32-bit consumer error socket path");
5850 goto exit_init_data
;
5852 ret
= snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
5853 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
5855 PERROR("snprintf 32-bit consumer command socket path");
5857 goto exit_init_data
;
5860 DBG2("UST consumer 32 bits err path: %s",
5861 ustconsumer32_data
.err_unix_sock_path
);
5862 DBG2("UST consumer 32 bits cmd path: %s",
5863 ustconsumer32_data
.cmd_unix_sock_path
);
5865 /* 64 bits consumerd path setup */
5866 ret
= snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
5867 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
5869 PERROR("snprintf 64-bit consumer error socket path");
5871 goto exit_init_data
;
5873 ret
= snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
5874 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
5876 PERROR("snprintf 64-bit consumer command socket path");
5878 goto exit_init_data
;
5881 DBG2("UST consumer 64 bits err path: %s",
5882 ustconsumer64_data
.err_unix_sock_path
);
5883 DBG2("UST consumer 64 bits cmd path: %s",
5884 ustconsumer64_data
.cmd_unix_sock_path
);
5887 * See if daemon already exist.
5889 if (check_existing_daemon()) {
5890 ERR("Already running daemon.\n");
5892 * We do not goto exit because we must not cleanup()
5893 * because a daemon is already running.
5896 goto exit_init_data
;
5900 * Init UST app hash table. Alloc hash table before this point since
5901 * cleanup() can get called after that point.
5903 if (ust_app_ht_alloc()) {
5904 ERR("Failed to allocate UST app hash table");
5906 goto exit_init_data
;
5910 * Initialize agent app hash table. We allocate the hash table here
5911 * since cleanup() can get called after this point.
5913 if (agent_app_ht_alloc()) {
5914 ERR("Failed to allocate Agent app hash table");
5916 goto exit_init_data
;
5920 * These actions must be executed as root. We do that *after* setting up
5921 * the sockets path because we MUST make the check for another daemon using
5922 * those paths *before* trying to set the kernel consumer sockets and init
5926 if (set_consumer_sockets(&kconsumer_data
, rundir
)) {
5928 goto exit_init_data
;
5931 /* Setup kernel tracer */
5932 if (!opt_no_kernel
) {
5933 init_kernel_tracer();
5934 if (kernel_tracer_fd
>= 0) {
5935 ret
= syscall_init_table();
5937 ERR("Unable to populate syscall table. "
5938 "Syscall tracing won't work "
5939 "for this session daemon.");
5944 /* Set ulimit for open files */
5947 /* init lttng_fd tracking must be done after set_ulimit. */
5950 if (set_consumer_sockets(&ustconsumer64_data
, rundir
)) {
5952 goto exit_init_data
;
5955 if (set_consumer_sockets(&ustconsumer32_data
, rundir
)) {
5957 goto exit_init_data
;
5960 /* Setup the needed unix socket */
5961 if (init_daemon_socket()) {
5963 goto exit_init_data
;
5966 /* Set credentials to socket */
5967 if (is_root
&& set_permissions(rundir
)) {
5969 goto exit_init_data
;
5972 /* Get parent pid if -S, --sig-parent is specified. */
5973 if (opt_sig_parent
) {
5977 /* Setup the kernel pipe for waking up the kernel thread */
5978 if (is_root
&& !opt_no_kernel
) {
5979 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
5981 goto exit_init_data
;
5985 /* Setup the thread apps communication pipe. */
5986 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
5988 goto exit_init_data
;
5991 /* Setup the thread apps notify communication pipe. */
5992 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
5994 goto exit_init_data
;
5997 /* Initialize global buffer per UID and PID registry. */
5998 buffer_reg_init_uid_registry();
5999 buffer_reg_init_pid_registry();
6001 /* Init UST command queue. */
6002 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
6005 * Get session list pointer. This pointer MUST NOT be free'd. This list
6006 * is statically declared in session.c
6008 session_list_ptr
= session_get_list();
6012 /* Check for the application socket timeout env variable. */
6013 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
6014 if (env_app_timeout
) {
6015 app_socket_timeout
= atoi(env_app_timeout
);
6017 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
6020 ret
= write_pidfile();
6022 ERR("Error in write_pidfile");
6024 goto exit_init_data
;
6026 ret
= write_agent_port();
6028 ERR("Error in write_agent_port");
6030 goto exit_init_data
;
6033 /* Initialize communication library */
6035 /* Initialize TCP timeout values */
6036 lttcomm_inet_init();
6038 if (load_session_init_data(&load_info
) < 0) {
6040 goto exit_init_data
;
6042 load_info
->path
= opt_load_session_path
;
6044 /* Create health-check thread */
6045 ret
= pthread_create(&health_thread
, NULL
,
6046 thread_manage_health
, (void *) NULL
);
6049 PERROR("pthread_create health");
6054 /* Create thread to manage the client socket */
6055 ret
= pthread_create(&client_thread
, NULL
,
6056 thread_manage_clients
, (void *) NULL
);
6059 PERROR("pthread_create clients");
6064 /* Create thread to dispatch registration */
6065 ret
= pthread_create(&dispatch_thread
, NULL
,
6066 thread_dispatch_ust_registration
, (void *) NULL
);
6069 PERROR("pthread_create dispatch");
6074 /* Create thread to manage application registration. */
6075 ret
= pthread_create(®_apps_thread
, NULL
,
6076 thread_registration_apps
, (void *) NULL
);
6079 PERROR("pthread_create registration");
6084 /* Create thread to manage application socket */
6085 ret
= pthread_create(&apps_thread
, NULL
,
6086 thread_manage_apps
, (void *) NULL
);
6089 PERROR("pthread_create apps");
6094 /* Create thread to manage application notify socket */
6095 ret
= pthread_create(&apps_notify_thread
, NULL
,
6096 ust_thread_manage_notify
, (void *) NULL
);
6099 PERROR("pthread_create notify");
6101 goto exit_apps_notify
;
6104 /* Create agent registration thread. */
6105 ret
= pthread_create(&agent_reg_thread
, NULL
,
6106 agent_thread_manage_registration
, (void *) NULL
);
6109 PERROR("pthread_create agent");
6111 goto exit_agent_reg
;
6114 /* Don't start this thread if kernel tracing is not requested nor root */
6115 if (is_root
&& !opt_no_kernel
) {
6116 /* Create kernel thread to manage kernel event */
6117 ret
= pthread_create(&kernel_thread
, NULL
,
6118 thread_manage_kernel
, (void *) NULL
);
6121 PERROR("pthread_create kernel");
6127 /* Create session loading thread. */
6128 ret
= pthread_create(&load_session_thread
, NULL
, thread_load_session
,
6132 PERROR("pthread_create load_session_thread");
6134 goto exit_load_session
;
6138 * This is where we start awaiting program completion (e.g. through
6139 * signal that asks threads to teardown).
6142 ret
= pthread_join(load_session_thread
, &status
);
6145 PERROR("pthread_join load_session_thread");
6150 if (is_root
&& !opt_no_kernel
) {
6151 ret
= pthread_join(kernel_thread
, &status
);
6154 PERROR("pthread_join");
6160 ret
= pthread_join(agent_reg_thread
, &status
);
6163 PERROR("pthread_join agent");
6168 ret
= pthread_join(apps_notify_thread
, &status
);
6171 PERROR("pthread_join apps notify");
6176 ret
= pthread_join(apps_thread
, &status
);
6179 PERROR("pthread_join apps");
6184 ret
= pthread_join(reg_apps_thread
, &status
);
6187 PERROR("pthread_join");
6193 * Join dispatch thread after joining reg_apps_thread to ensure
6194 * we don't leak applications in the queue.
6196 ret
= pthread_join(dispatch_thread
, &status
);
6199 PERROR("pthread_join");
6204 ret
= pthread_join(client_thread
, &status
);
6207 PERROR("pthread_join");
6212 ret
= pthread_join(health_thread
, &status
);
6215 PERROR("pthread_join health thread");
6222 * sessiond_cleanup() is called when no other thread is running, except
6223 * the ht_cleanup thread, which is needed to destroy the hash tables.
6225 rcu_thread_online();
6227 rcu_thread_offline();
6228 rcu_unregister_thread();
6230 ret
= notify_thread_pipe(ht_cleanup_quit_pipe
[1]);
6232 ERR("write error on ht_cleanup quit pipe");
6236 ret
= pthread_join(ht_cleanup_thread
, &status
);
6239 PERROR("pthread_join ht cleanup thread");
6245 utils_close_pipe(ht_cleanup_pipe
);
6246 exit_ht_cleanup_pipe
:
6249 * Close the ht_cleanup quit pipe.
6251 utils_close_pipe(ht_cleanup_quit_pipe
);
6252 exit_ht_cleanup_quit_pipe
:
6254 health_app_destroy(health_sessiond
);
6255 exit_health_sessiond_cleanup
:
6256 exit_create_run_as_worker_cleanup
:
6259 /* Ensure all prior call_rcu are done. */
6262 sessiond_cleanup_options();
6264 exit_set_signal_handler
: