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 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
906 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
914 * Version of setup_lttng_msg() without command header.
916 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
917 void *payload_buf
, size_t payload_len
)
919 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
922 * Update the kernel poll set of all channel fd available over all tracing
923 * session. Add the wakeup pipe at the end of the set.
925 static int update_kernel_poll(struct lttng_poll_event
*events
)
928 struct ltt_session
*session
;
929 struct ltt_kernel_channel
*channel
;
931 DBG("Updating kernel poll set");
934 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
935 session_lock(session
);
936 if (session
->kernel_session
== NULL
) {
937 session_unlock(session
);
941 cds_list_for_each_entry(channel
,
942 &session
->kernel_session
->channel_list
.head
, list
) {
943 /* Add channel fd to the kernel poll set */
944 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
946 session_unlock(session
);
949 DBG("Channel fd %d added to kernel set", channel
->fd
);
951 session_unlock(session
);
953 session_unlock_list();
958 session_unlock_list();
963 * Find the channel fd from 'fd' over all tracing session. When found, check
964 * for new channel stream and send those stream fds to the kernel consumer.
966 * Useful for CPU hotplug feature.
968 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
971 struct ltt_session
*session
;
972 struct ltt_kernel_session
*ksess
;
973 struct ltt_kernel_channel
*channel
;
975 DBG("Updating kernel streams for channel fd %d", fd
);
978 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
979 session_lock(session
);
980 if (session
->kernel_session
== NULL
) {
981 session_unlock(session
);
984 ksess
= session
->kernel_session
;
986 cds_list_for_each_entry(channel
,
987 &ksess
->channel_list
.head
, list
) {
988 struct lttng_ht_iter iter
;
989 struct consumer_socket
*socket
;
991 if (channel
->fd
!= fd
) {
994 DBG("Channel found, updating kernel streams");
995 ret
= kernel_open_channel_stream(channel
);
999 /* Update the stream global counter */
1000 ksess
->stream_count_global
+= ret
;
1003 * Have we already sent fds to the consumer? If yes, it
1004 * means that tracing is started so it is safe to send
1005 * our updated stream fds.
1007 if (ksess
->consumer_fds_sent
!= 1
1008 || ksess
->consumer
== NULL
) {
1014 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
1015 &iter
.iter
, socket
, node
.node
) {
1016 pthread_mutex_lock(socket
->lock
);
1017 ret
= kernel_consumer_send_channel_stream(socket
,
1019 session
->output_traces
? 1 : 0);
1020 pthread_mutex_unlock(socket
->lock
);
1028 session_unlock(session
);
1030 session_unlock_list();
1034 session_unlock(session
);
1035 session_unlock_list();
1040 * For each tracing session, update newly registered apps. The session list
1041 * lock MUST be acquired before calling this.
1043 static void update_ust_app(int app_sock
)
1045 struct ltt_session
*sess
, *stmp
;
1047 /* Consumer is in an ERROR state. Stop any application update. */
1048 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
1049 /* Stop the update process since the consumer is dead. */
1053 /* For all tracing session(s) */
1054 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
1055 struct ust_app
*app
;
1058 if (!sess
->ust_session
) {
1059 goto unlock_session
;
1063 assert(app_sock
>= 0);
1064 app
= ust_app_find_by_sock(app_sock
);
1067 * Application can be unregistered before so
1068 * this is possible hence simply stopping the
1071 DBG3("UST app update failed to find app sock %d",
1075 ust_app_global_update(sess
->ust_session
, app
);
1079 session_unlock(sess
);
1084 * This thread manage event coming from the kernel.
1086 * Features supported in this thread:
1089 static void *thread_manage_kernel(void *data
)
1091 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
1092 uint32_t revents
, nb_fd
;
1094 struct lttng_poll_event events
;
1096 DBG("[thread] Thread manage kernel started");
1098 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
1101 * This first step of the while is to clean this structure which could free
1102 * non NULL pointers so initialize it before the loop.
1104 lttng_poll_init(&events
);
1106 if (testpoint(sessiond_thread_manage_kernel
)) {
1107 goto error_testpoint
;
1110 health_code_update();
1112 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
1113 goto error_testpoint
;
1117 health_code_update();
1119 if (update_poll_flag
== 1) {
1120 /* Clean events object. We are about to populate it again. */
1121 lttng_poll_clean(&events
);
1123 ret
= sessiond_set_thread_pollset(&events
, 2);
1125 goto error_poll_create
;
1128 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
1133 /* This will add the available kernel channel if any. */
1134 ret
= update_kernel_poll(&events
);
1138 update_poll_flag
= 0;
1141 DBG("Thread kernel polling");
1143 /* Poll infinite value of time */
1145 health_poll_entry();
1146 ret
= lttng_poll_wait(&events
, -1);
1147 DBG("Thread kernel return from poll on %d fds",
1148 LTTNG_POLL_GETNB(&events
));
1152 * Restart interrupted system call.
1154 if (errno
== EINTR
) {
1158 } else if (ret
== 0) {
1159 /* Should not happen since timeout is infinite */
1160 ERR("Return value of poll is 0 with an infinite timeout.\n"
1161 "This should not have happened! Continuing...");
1167 for (i
= 0; i
< nb_fd
; i
++) {
1168 /* Fetch once the poll data */
1169 revents
= LTTNG_POLL_GETEV(&events
, i
);
1170 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1172 health_code_update();
1175 /* No activity for this FD (poll implementation). */
1179 /* Thread quit pipe has been closed. Killing thread. */
1180 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1186 /* Check for data on kernel pipe */
1187 if (revents
& LPOLLIN
) {
1188 if (pollfd
== kernel_poll_pipe
[0]) {
1189 (void) lttng_read(kernel_poll_pipe
[0],
1192 * Ret value is useless here, if this pipe gets any actions an
1193 * update is required anyway.
1195 update_poll_flag
= 1;
1199 * New CPU detected by the kernel. Adding kernel stream to
1200 * kernel session and updating the kernel consumer
1202 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1208 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1209 update_poll_flag
= 1;
1212 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1220 lttng_poll_clean(&events
);
1223 utils_close_pipe(kernel_poll_pipe
);
1224 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1227 ERR("Health error occurred in %s", __func__
);
1228 WARN("Kernel thread died unexpectedly. "
1229 "Kernel tracing can continue but CPU hotplug is disabled.");
1231 health_unregister(health_sessiond
);
1232 DBG("Kernel thread dying");
1237 * Signal pthread condition of the consumer data that the thread.
1239 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1241 pthread_mutex_lock(&data
->cond_mutex
);
1244 * The state is set before signaling. It can be any value, it's the waiter
1245 * job to correctly interpret this condition variable associated to the
1246 * consumer pthread_cond.
1248 * A value of 0 means that the corresponding thread of the consumer data
1249 * was not started. 1 indicates that the thread has started and is ready
1250 * for action. A negative value means that there was an error during the
1253 data
->consumer_thread_is_ready
= state
;
1254 (void) pthread_cond_signal(&data
->cond
);
1256 pthread_mutex_unlock(&data
->cond_mutex
);
1260 * This thread manage the consumer error sent back to the session daemon.
1262 static void *thread_manage_consumer(void *data
)
1264 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1265 uint32_t revents
, nb_fd
;
1266 enum lttcomm_return_code code
;
1267 struct lttng_poll_event events
;
1268 struct consumer_data
*consumer_data
= data
;
1270 DBG("[thread] Manage consumer started");
1272 rcu_register_thread();
1273 rcu_thread_online();
1275 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1277 health_code_update();
1280 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1281 * metadata_sock. Nothing more will be added to this poll set.
1283 ret
= sessiond_set_thread_pollset(&events
, 3);
1289 * The error socket here is already in a listening state which was done
1290 * just before spawning this thread to avoid a race between the consumer
1291 * daemon exec trying to connect and the listen() call.
1293 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1298 health_code_update();
1300 /* Infinite blocking call, waiting for transmission */
1302 health_poll_entry();
1304 if (testpoint(sessiond_thread_manage_consumer
)) {
1308 ret
= lttng_poll_wait(&events
, -1);
1312 * Restart interrupted system call.
1314 if (errno
== EINTR
) {
1322 for (i
= 0; i
< nb_fd
; i
++) {
1323 /* Fetch once the poll data */
1324 revents
= LTTNG_POLL_GETEV(&events
, i
);
1325 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1327 health_code_update();
1330 /* No activity for this FD (poll implementation). */
1334 /* Thread quit pipe has been closed. Killing thread. */
1335 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1341 /* Event on the registration socket */
1342 if (pollfd
== consumer_data
->err_sock
) {
1343 if (revents
& LPOLLIN
) {
1345 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1346 ERR("consumer err socket poll error");
1349 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1355 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1361 * Set the CLOEXEC flag. Return code is useless because either way, the
1364 (void) utils_set_fd_cloexec(sock
);
1366 health_code_update();
1368 DBG2("Receiving code from consumer err_sock");
1370 /* Getting status code from kconsumerd */
1371 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1372 sizeof(enum lttcomm_return_code
));
1377 health_code_update();
1378 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1379 /* Connect both socket, command and metadata. */
1380 consumer_data
->cmd_sock
=
1381 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1382 consumer_data
->metadata_fd
=
1383 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1384 if (consumer_data
->cmd_sock
< 0
1385 || consumer_data
->metadata_fd
< 0) {
1386 PERROR("consumer connect cmd socket");
1387 /* On error, signal condition and quit. */
1388 signal_consumer_condition(consumer_data
, -1);
1391 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1392 /* Create metadata socket lock. */
1393 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1394 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1395 PERROR("zmalloc pthread mutex");
1399 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1401 signal_consumer_condition(consumer_data
, 1);
1402 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1403 DBG("Consumer metadata socket ready (fd: %d)",
1404 consumer_data
->metadata_fd
);
1406 ERR("consumer error when waiting for SOCK_READY : %s",
1407 lttcomm_get_readable_code(-code
));
1411 /* Remove the consumerd error sock since we've established a connexion */
1412 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1417 /* Add new accepted error socket. */
1418 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1423 /* Add metadata socket that is successfully connected. */
1424 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1425 LPOLLIN
| LPOLLRDHUP
);
1430 health_code_update();
1432 /* Infinite blocking call, waiting for transmission */
1435 health_code_update();
1437 /* Exit the thread because the thread quit pipe has been triggered. */
1439 /* Not a health error. */
1444 health_poll_entry();
1445 ret
= lttng_poll_wait(&events
, -1);
1449 * Restart interrupted system call.
1451 if (errno
== EINTR
) {
1459 for (i
= 0; i
< nb_fd
; i
++) {
1460 /* Fetch once the poll data */
1461 revents
= LTTNG_POLL_GETEV(&events
, i
);
1462 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1464 health_code_update();
1467 /* No activity for this FD (poll implementation). */
1472 * Thread quit pipe has been triggered, flag that we should stop
1473 * but continue the current loop to handle potential data from
1476 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1478 if (pollfd
== sock
) {
1479 /* Event on the consumerd socket */
1480 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1481 && !(revents
& LPOLLIN
)) {
1482 ERR("consumer err socket second poll error");
1485 health_code_update();
1486 /* Wait for any kconsumerd error */
1487 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1488 sizeof(enum lttcomm_return_code
));
1490 ERR("consumer closed the command socket");
1494 ERR("consumer return code : %s",
1495 lttcomm_get_readable_code(-code
));
1498 } else if (pollfd
== consumer_data
->metadata_fd
) {
1499 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1500 && !(revents
& LPOLLIN
)) {
1501 ERR("consumer err metadata socket second poll error");
1504 /* UST metadata requests */
1505 ret
= ust_consumer_metadata_request(
1506 &consumer_data
->metadata_sock
);
1508 ERR("Handling metadata request");
1512 /* No need for an else branch all FDs are tested prior. */
1514 health_code_update();
1520 * We lock here because we are about to close the sockets and some other
1521 * thread might be using them so get exclusive access which will abort all
1522 * other consumer command by other threads.
1524 pthread_mutex_lock(&consumer_data
->lock
);
1526 /* Immediately set the consumerd state to stopped */
1527 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1528 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1529 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1530 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1531 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1533 /* Code flow error... */
1537 if (consumer_data
->err_sock
>= 0) {
1538 ret
= close(consumer_data
->err_sock
);
1542 consumer_data
->err_sock
= -1;
1544 if (consumer_data
->cmd_sock
>= 0) {
1545 ret
= close(consumer_data
->cmd_sock
);
1549 consumer_data
->cmd_sock
= -1;
1551 if (consumer_data
->metadata_sock
.fd_ptr
&&
1552 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1553 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1565 unlink(consumer_data
->err_unix_sock_path
);
1566 unlink(consumer_data
->cmd_unix_sock_path
);
1567 pthread_mutex_unlock(&consumer_data
->lock
);
1569 /* Cleanup metadata socket mutex. */
1570 if (consumer_data
->metadata_sock
.lock
) {
1571 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1572 free(consumer_data
->metadata_sock
.lock
);
1574 lttng_poll_clean(&events
);
1578 ERR("Health error occurred in %s", __func__
);
1580 health_unregister(health_sessiond
);
1581 DBG("consumer thread cleanup completed");
1583 rcu_thread_offline();
1584 rcu_unregister_thread();
1590 * This thread manage application communication.
1592 static void *thread_manage_apps(void *data
)
1594 int i
, ret
, pollfd
, err
= -1;
1596 uint32_t revents
, nb_fd
;
1597 struct lttng_poll_event events
;
1599 DBG("[thread] Manage application started");
1601 rcu_register_thread();
1602 rcu_thread_online();
1604 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1606 if (testpoint(sessiond_thread_manage_apps
)) {
1607 goto error_testpoint
;
1610 health_code_update();
1612 ret
= sessiond_set_thread_pollset(&events
, 2);
1614 goto error_poll_create
;
1617 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1622 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1626 health_code_update();
1629 DBG("Apps thread polling");
1631 /* Inifinite blocking call, waiting for transmission */
1633 health_poll_entry();
1634 ret
= lttng_poll_wait(&events
, -1);
1635 DBG("Apps thread return from poll on %d fds",
1636 LTTNG_POLL_GETNB(&events
));
1640 * Restart interrupted system call.
1642 if (errno
== EINTR
) {
1650 for (i
= 0; i
< nb_fd
; i
++) {
1651 /* Fetch once the poll data */
1652 revents
= LTTNG_POLL_GETEV(&events
, i
);
1653 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1655 health_code_update();
1658 /* No activity for this FD (poll implementation). */
1662 /* Thread quit pipe has been closed. Killing thread. */
1663 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1669 /* Inspect the apps cmd pipe */
1670 if (pollfd
== apps_cmd_pipe
[0]) {
1671 if (revents
& LPOLLIN
) {
1675 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1676 if (size_ret
< sizeof(sock
)) {
1677 PERROR("read apps cmd pipe");
1681 health_code_update();
1684 * Since this is a command socket (write then read),
1685 * we only monitor the error events of the socket.
1687 ret
= lttng_poll_add(&events
, sock
,
1688 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1693 DBG("Apps with sock %d added to poll set", sock
);
1694 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1695 ERR("Apps command pipe error");
1698 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1703 * At this point, we know that a registered application made
1704 * the event at poll_wait.
1706 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1707 /* Removing from the poll set */
1708 ret
= lttng_poll_del(&events
, pollfd
);
1713 /* Socket closed on remote end. */
1714 ust_app_unregister(pollfd
);
1716 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1721 health_code_update();
1727 lttng_poll_clean(&events
);
1730 utils_close_pipe(apps_cmd_pipe
);
1731 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1734 * We don't clean the UST app hash table here since already registered
1735 * applications can still be controlled so let them be until the session
1736 * daemon dies or the applications stop.
1741 ERR("Health error occurred in %s", __func__
);
1743 health_unregister(health_sessiond
);
1744 DBG("Application communication apps thread cleanup complete");
1745 rcu_thread_offline();
1746 rcu_unregister_thread();
1751 * Send a socket to a thread This is called from the dispatch UST registration
1752 * thread once all sockets are set for the application.
1754 * The sock value can be invalid, we don't really care, the thread will handle
1755 * it and make the necessary cleanup if so.
1757 * On success, return 0 else a negative value being the errno message of the
1760 static int send_socket_to_thread(int fd
, int sock
)
1765 * It's possible that the FD is set as invalid with -1 concurrently just
1766 * before calling this function being a shutdown state of the thread.
1773 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1774 if (ret
< sizeof(sock
)) {
1775 PERROR("write apps pipe %d", fd
);
1782 /* All good. Don't send back the write positive ret value. */
1789 * Sanitize the wait queue of the dispatch registration thread meaning removing
1790 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1791 * notify socket is never received.
1793 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1795 int ret
, nb_fd
= 0, i
;
1796 unsigned int fd_added
= 0;
1797 struct lttng_poll_event events
;
1798 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1802 lttng_poll_init(&events
);
1804 /* Just skip everything for an empty queue. */
1805 if (!wait_queue
->count
) {
1809 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1814 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1815 &wait_queue
->head
, head
) {
1816 assert(wait_node
->app
);
1817 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1818 LPOLLHUP
| LPOLLERR
);
1831 * Poll but don't block so we can quickly identify the faulty events and
1832 * clean them afterwards from the wait queue.
1834 ret
= lttng_poll_wait(&events
, 0);
1840 for (i
= 0; i
< nb_fd
; i
++) {
1841 /* Get faulty FD. */
1842 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1843 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1846 /* No activity for this FD (poll implementation). */
1850 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1851 &wait_queue
->head
, head
) {
1852 if (pollfd
== wait_node
->app
->sock
&&
1853 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1854 cds_list_del(&wait_node
->head
);
1855 wait_queue
->count
--;
1856 ust_app_destroy(wait_node
->app
);
1859 * Silence warning of use-after-free in
1860 * cds_list_for_each_entry_safe which uses
1861 * __typeof__(*wait_node).
1866 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1873 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1877 lttng_poll_clean(&events
);
1881 lttng_poll_clean(&events
);
1883 ERR("Unable to sanitize wait queue");
1888 * Dispatch request from the registration threads to the application
1889 * communication thread.
1891 static void *thread_dispatch_ust_registration(void *data
)
1894 struct cds_wfcq_node
*node
;
1895 struct ust_command
*ust_cmd
= NULL
;
1896 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1897 struct ust_reg_wait_queue wait_queue
= {
1901 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1903 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1904 goto error_testpoint
;
1907 health_code_update();
1909 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1911 DBG("[thread] Dispatch UST command started");
1913 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1914 health_code_update();
1916 /* Atomically prepare the queue futex */
1917 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1920 struct ust_app
*app
= NULL
;
1924 * Make sure we don't have node(s) that have hung up before receiving
1925 * the notify socket. This is to clean the list in order to avoid
1926 * memory leaks from notify socket that are never seen.
1928 sanitize_wait_queue(&wait_queue
);
1930 health_code_update();
1931 /* Dequeue command for registration */
1932 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1934 DBG("Woken up but nothing in the UST command queue");
1935 /* Continue thread execution */
1939 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1941 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1942 " gid:%d sock:%d name:%s (version %d.%d)",
1943 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1944 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1945 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1946 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1948 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1949 wait_node
= zmalloc(sizeof(*wait_node
));
1951 PERROR("zmalloc wait_node dispatch");
1952 ret
= close(ust_cmd
->sock
);
1954 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1956 lttng_fd_put(LTTNG_FD_APPS
, 1);
1960 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1962 /* Create application object if socket is CMD. */
1963 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1965 if (!wait_node
->app
) {
1966 ret
= close(ust_cmd
->sock
);
1968 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1970 lttng_fd_put(LTTNG_FD_APPS
, 1);
1976 * Add application to the wait queue so we can set the notify
1977 * socket before putting this object in the global ht.
1979 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1984 * We have to continue here since we don't have the notify
1985 * socket and the application MUST be added to the hash table
1986 * only at that moment.
1991 * Look for the application in the local wait queue and set the
1992 * notify socket if found.
1994 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1995 &wait_queue
.head
, head
) {
1996 health_code_update();
1997 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1998 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1999 cds_list_del(&wait_node
->head
);
2001 app
= wait_node
->app
;
2003 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
2009 * With no application at this stage the received socket is
2010 * basically useless so close it before we free the cmd data
2011 * structure for good.
2014 ret
= close(ust_cmd
->sock
);
2016 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
2018 lttng_fd_put(LTTNG_FD_APPS
, 1);
2025 * @session_lock_list
2027 * Lock the global session list so from the register up to the
2028 * registration done message, no thread can see the application
2029 * and change its state.
2031 session_lock_list();
2035 * Add application to the global hash table. This needs to be
2036 * done before the update to the UST registry can locate the
2041 /* Set app version. This call will print an error if needed. */
2042 (void) ust_app_version(app
);
2044 /* Send notify socket through the notify pipe. */
2045 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
2049 session_unlock_list();
2051 * No notify thread, stop the UST tracing. However, this is
2052 * not an internal error of the this thread thus setting
2053 * the health error code to a normal exit.
2060 * Update newly registered application with the tracing
2061 * registry info already enabled information.
2063 update_ust_app(app
->sock
);
2066 * Don't care about return value. Let the manage apps threads
2067 * handle app unregistration upon socket close.
2069 (void) ust_app_register_done(app
);
2072 * Even if the application socket has been closed, send the app
2073 * to the thread and unregistration will take place at that
2076 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
2079 session_unlock_list();
2081 * No apps. thread, stop the UST tracing. However, this is
2082 * not an internal error of the this thread thus setting
2083 * the health error code to a normal exit.
2090 session_unlock_list();
2092 } while (node
!= NULL
);
2094 health_poll_entry();
2095 /* Futex wait on queue. Blocking call on futex() */
2096 futex_nto1_wait(&ust_cmd_queue
.futex
);
2099 /* Normal exit, no error */
2103 /* Clean up wait queue. */
2104 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2105 &wait_queue
.head
, head
) {
2106 cds_list_del(&wait_node
->head
);
2111 /* Empty command queue. */
2113 /* Dequeue command for registration */
2114 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2118 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2119 ret
= close(ust_cmd
->sock
);
2121 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2123 lttng_fd_put(LTTNG_FD_APPS
, 1);
2128 DBG("Dispatch thread dying");
2131 ERR("Health error occurred in %s", __func__
);
2133 health_unregister(health_sessiond
);
2138 * This thread manage application registration.
2140 static void *thread_registration_apps(void *data
)
2142 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2143 uint32_t revents
, nb_fd
;
2144 struct lttng_poll_event events
;
2146 * Get allocated in this thread, enqueued to a global queue, dequeued and
2147 * freed in the manage apps thread.
2149 struct ust_command
*ust_cmd
= NULL
;
2151 DBG("[thread] Manage application registration started");
2153 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2155 if (testpoint(sessiond_thread_registration_apps
)) {
2156 goto error_testpoint
;
2159 ret
= lttcomm_listen_unix_sock(apps_sock
);
2165 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2166 * more will be added to this poll set.
2168 ret
= sessiond_set_thread_pollset(&events
, 2);
2170 goto error_create_poll
;
2173 /* Add the application registration socket */
2174 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2176 goto error_poll_add
;
2179 /* Notify all applications to register */
2180 ret
= notify_ust_apps(1);
2182 ERR("Failed to notify applications or create the wait shared memory.\n"
2183 "Execution continues but there might be problem for already\n"
2184 "running applications that wishes to register.");
2188 DBG("Accepting application registration");
2190 /* Inifinite blocking call, waiting for transmission */
2192 health_poll_entry();
2193 ret
= lttng_poll_wait(&events
, -1);
2197 * Restart interrupted system call.
2199 if (errno
== EINTR
) {
2207 for (i
= 0; i
< nb_fd
; i
++) {
2208 health_code_update();
2210 /* Fetch once the poll data */
2211 revents
= LTTNG_POLL_GETEV(&events
, i
);
2212 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2215 /* No activity for this FD (poll implementation). */
2219 /* Thread quit pipe has been closed. Killing thread. */
2220 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2226 /* Event on the registration socket */
2227 if (pollfd
== apps_sock
) {
2228 if (revents
& LPOLLIN
) {
2229 sock
= lttcomm_accept_unix_sock(apps_sock
);
2235 * Set socket timeout for both receiving and ending.
2236 * app_socket_timeout is in seconds, whereas
2237 * lttcomm_setsockopt_rcv_timeout and
2238 * lttcomm_setsockopt_snd_timeout expect msec as
2241 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2242 app_socket_timeout
* 1000);
2243 (void) lttcomm_setsockopt_snd_timeout(sock
,
2244 app_socket_timeout
* 1000);
2247 * Set the CLOEXEC flag. Return code is useless because
2248 * either way, the show must go on.
2250 (void) utils_set_fd_cloexec(sock
);
2252 /* Create UST registration command for enqueuing */
2253 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2254 if (ust_cmd
== NULL
) {
2255 PERROR("ust command zmalloc");
2264 * Using message-based transmissions to ensure we don't
2265 * have to deal with partially received messages.
2267 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2269 ERR("Exhausted file descriptors allowed for applications.");
2279 health_code_update();
2280 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2283 /* Close socket of the application. */
2288 lttng_fd_put(LTTNG_FD_APPS
, 1);
2292 health_code_update();
2294 ust_cmd
->sock
= sock
;
2297 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2298 " gid:%d sock:%d name:%s (version %d.%d)",
2299 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2300 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2301 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2302 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2305 * Lock free enqueue the registration request. The red pill
2306 * has been taken! This apps will be part of the *system*.
2308 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2311 * Wake the registration queue futex. Implicit memory
2312 * barrier with the exchange in cds_wfcq_enqueue.
2314 futex_nto1_wake(&ust_cmd_queue
.futex
);
2315 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2316 ERR("Register apps socket poll error");
2319 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2328 /* Notify that the registration thread is gone */
2331 if (apps_sock
>= 0) {
2332 ret
= close(apps_sock
);
2342 lttng_fd_put(LTTNG_FD_APPS
, 1);
2344 unlink(apps_unix_sock_path
);
2347 lttng_poll_clean(&events
);
2351 DBG("UST Registration thread cleanup complete");
2354 ERR("Health error occurred in %s", __func__
);
2356 health_unregister(health_sessiond
);
2362 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2363 * exec or it will fails.
2365 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2368 struct timespec timeout
;
2370 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2371 consumer_data
->consumer_thread_is_ready
= 0;
2373 /* Setup pthread condition */
2374 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2377 PERROR("pthread_condattr_init consumer data");
2382 * Set the monotonic clock in order to make sure we DO NOT jump in time
2383 * between the clock_gettime() call and the timedwait call. See bug #324
2384 * for a more details and how we noticed it.
2386 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2389 PERROR("pthread_condattr_setclock consumer data");
2393 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2396 PERROR("pthread_cond_init consumer data");
2400 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
2404 PERROR("pthread_create consumer");
2409 /* We are about to wait on a pthread condition */
2410 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2412 /* Get time for sem_timedwait absolute timeout */
2413 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2415 * Set the timeout for the condition timed wait even if the clock gettime
2416 * call fails since we might loop on that call and we want to avoid to
2417 * increment the timeout too many times.
2419 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2422 * The following loop COULD be skipped in some conditions so this is why we
2423 * set ret to 0 in order to make sure at least one round of the loop is
2429 * Loop until the condition is reached or when a timeout is reached. Note
2430 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2431 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2432 * possible. This loop does not take any chances and works with both of
2435 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2436 if (clock_ret
< 0) {
2437 PERROR("clock_gettime spawn consumer");
2438 /* Infinite wait for the consumerd thread to be ready */
2439 ret
= pthread_cond_wait(&consumer_data
->cond
,
2440 &consumer_data
->cond_mutex
);
2442 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2443 &consumer_data
->cond_mutex
, &timeout
);
2447 /* Release the pthread condition */
2448 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2452 if (ret
== ETIMEDOUT
) {
2456 * Call has timed out so we kill the kconsumerd_thread and return
2459 ERR("Condition timed out. The consumer thread was never ready."
2461 pth_ret
= pthread_cancel(consumer_data
->thread
);
2463 PERROR("pthread_cancel consumer thread");
2466 PERROR("pthread_cond_wait failed consumer thread");
2468 /* Caller is expecting a negative value on failure. */
2473 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2474 if (consumer_data
->pid
== 0) {
2475 ERR("Consumerd did not start");
2476 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2479 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2488 * Join consumer thread
2490 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2494 /* Consumer pid must be a real one. */
2495 if (consumer_data
->pid
> 0) {
2497 ret
= kill(consumer_data
->pid
, SIGTERM
);
2499 PERROR("Error killing consumer daemon");
2502 return pthread_join(consumer_data
->thread
, &status
);
2509 * Fork and exec a consumer daemon (consumerd).
2511 * Return pid if successful else -1.
2513 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2517 const char *consumer_to_use
;
2518 const char *verbosity
;
2521 DBG("Spawning consumerd");
2528 if (opt_verbose_consumer
) {
2529 verbosity
= "--verbose";
2530 } else if (lttng_opt_quiet
) {
2531 verbosity
= "--quiet";
2536 switch (consumer_data
->type
) {
2537 case LTTNG_CONSUMER_KERNEL
:
2539 * Find out which consumerd to execute. We will first try the
2540 * 64-bit path, then the sessiond's installation directory, and
2541 * fallback on the 32-bit one,
2543 DBG3("Looking for a kernel consumer at these locations:");
2544 DBG3(" 1) %s", consumerd64_bin
);
2545 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2546 DBG3(" 3) %s", consumerd32_bin
);
2547 if (stat(consumerd64_bin
, &st
) == 0) {
2548 DBG3("Found location #1");
2549 consumer_to_use
= consumerd64_bin
;
2550 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2551 DBG3("Found location #2");
2552 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2553 } else if (stat(consumerd32_bin
, &st
) == 0) {
2554 DBG3("Found location #3");
2555 consumer_to_use
= consumerd32_bin
;
2557 DBG("Could not find any valid consumerd executable");
2561 DBG("Using kernel consumer at: %s", consumer_to_use
);
2562 ret
= execl(consumer_to_use
,
2563 "lttng-consumerd", verbosity
, "-k",
2564 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2565 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2566 "--group", tracing_group_name
,
2569 case LTTNG_CONSUMER64_UST
:
2571 char *tmpnew
= NULL
;
2573 if (consumerd64_libdir
[0] != '\0') {
2577 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2581 tmplen
= strlen("LD_LIBRARY_PATH=")
2582 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2583 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2588 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2589 strcat(tmpnew
, consumerd64_libdir
);
2590 if (tmp
[0] != '\0') {
2591 strcat(tmpnew
, ":");
2592 strcat(tmpnew
, tmp
);
2594 ret
= putenv(tmpnew
);
2601 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2602 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2603 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2604 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2605 "--group", tracing_group_name
,
2607 if (consumerd64_libdir
[0] != '\0') {
2612 case LTTNG_CONSUMER32_UST
:
2614 char *tmpnew
= NULL
;
2616 if (consumerd32_libdir
[0] != '\0') {
2620 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2624 tmplen
= strlen("LD_LIBRARY_PATH=")
2625 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2626 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2631 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2632 strcat(tmpnew
, consumerd32_libdir
);
2633 if (tmp
[0] != '\0') {
2634 strcat(tmpnew
, ":");
2635 strcat(tmpnew
, tmp
);
2637 ret
= putenv(tmpnew
);
2644 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2645 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2646 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2647 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2648 "--group", tracing_group_name
,
2650 if (consumerd32_libdir
[0] != '\0') {
2656 PERROR("unknown consumer type");
2660 PERROR("Consumer execl()");
2662 /* Reaching this point, we got a failure on our execl(). */
2664 } else if (pid
> 0) {
2667 PERROR("start consumer fork");
2675 * Spawn the consumerd daemon and session daemon thread.
2677 static int start_consumerd(struct consumer_data
*consumer_data
)
2682 * Set the listen() state on the socket since there is a possible race
2683 * between the exec() of the consumer daemon and this call if place in the
2684 * consumer thread. See bug #366 for more details.
2686 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2691 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2692 if (consumer_data
->pid
!= 0) {
2693 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2697 ret
= spawn_consumerd(consumer_data
);
2699 ERR("Spawning consumerd failed");
2700 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2704 /* Setting up the consumer_data pid */
2705 consumer_data
->pid
= ret
;
2706 DBG2("Consumer pid %d", consumer_data
->pid
);
2707 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2709 DBG2("Spawning consumer control thread");
2710 ret
= spawn_consumer_thread(consumer_data
);
2712 ERR("Fatal error spawning consumer control thread");
2720 /* Cleanup already created sockets on error. */
2721 if (consumer_data
->err_sock
>= 0) {
2724 err
= close(consumer_data
->err_sock
);
2726 PERROR("close consumer data error socket");
2733 * Setup necessary data for kernel tracer action.
2735 static int init_kernel_tracer(void)
2739 /* Modprobe lttng kernel modules */
2740 ret
= modprobe_lttng_control();
2745 /* Open debugfs lttng */
2746 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2747 if (kernel_tracer_fd
< 0) {
2748 DBG("Failed to open %s", module_proc_lttng
);
2753 /* Validate kernel version */
2754 ret
= kernel_validate_version(kernel_tracer_fd
);
2759 ret
= modprobe_lttng_data();
2764 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2768 modprobe_remove_lttng_control();
2769 ret
= close(kernel_tracer_fd
);
2773 kernel_tracer_fd
= -1;
2774 return LTTNG_ERR_KERN_VERSION
;
2777 ret
= close(kernel_tracer_fd
);
2783 modprobe_remove_lttng_control();
2786 WARN("No kernel tracer available");
2787 kernel_tracer_fd
= -1;
2789 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2791 return LTTNG_ERR_KERN_NA
;
2797 * Copy consumer output from the tracing session to the domain session. The
2798 * function also applies the right modification on a per domain basis for the
2799 * trace files destination directory.
2801 * Should *NOT* be called with RCU read-side lock held.
2803 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2806 const char *dir_name
;
2807 struct consumer_output
*consumer
;
2810 assert(session
->consumer
);
2813 case LTTNG_DOMAIN_KERNEL
:
2814 DBG3("Copying tracing session consumer output in kernel session");
2816 * XXX: We should audit the session creation and what this function
2817 * does "extra" in order to avoid a destroy since this function is used
2818 * in the domain session creation (kernel and ust) only. Same for UST
2821 if (session
->kernel_session
->consumer
) {
2822 consumer_output_put(session
->kernel_session
->consumer
);
2824 session
->kernel_session
->consumer
=
2825 consumer_copy_output(session
->consumer
);
2826 /* Ease our life a bit for the next part */
2827 consumer
= session
->kernel_session
->consumer
;
2828 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2830 case LTTNG_DOMAIN_JUL
:
2831 case LTTNG_DOMAIN_LOG4J
:
2832 case LTTNG_DOMAIN_PYTHON
:
2833 case LTTNG_DOMAIN_UST
:
2834 DBG3("Copying tracing session consumer output in UST session");
2835 if (session
->ust_session
->consumer
) {
2836 consumer_output_put(session
->ust_session
->consumer
);
2838 session
->ust_session
->consumer
=
2839 consumer_copy_output(session
->consumer
);
2840 /* Ease our life a bit for the next part */
2841 consumer
= session
->ust_session
->consumer
;
2842 dir_name
= DEFAULT_UST_TRACE_DIR
;
2845 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2849 /* Append correct directory to subdir */
2850 strncat(consumer
->subdir
, dir_name
,
2851 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2852 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2861 * Create an UST session and add it to the session ust list.
2863 * Should *NOT* be called with RCU read-side lock held.
2865 static int create_ust_session(struct ltt_session
*session
,
2866 struct lttng_domain
*domain
)
2869 struct ltt_ust_session
*lus
= NULL
;
2873 assert(session
->consumer
);
2875 switch (domain
->type
) {
2876 case LTTNG_DOMAIN_JUL
:
2877 case LTTNG_DOMAIN_LOG4J
:
2878 case LTTNG_DOMAIN_PYTHON
:
2879 case LTTNG_DOMAIN_UST
:
2882 ERR("Unknown UST domain on create session %d", domain
->type
);
2883 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2887 DBG("Creating UST session");
2889 lus
= trace_ust_create_session(session
->id
);
2891 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2895 lus
->uid
= session
->uid
;
2896 lus
->gid
= session
->gid
;
2897 lus
->output_traces
= session
->output_traces
;
2898 lus
->snapshot_mode
= session
->snapshot_mode
;
2899 lus
->live_timer_interval
= session
->live_timer
;
2900 session
->ust_session
= lus
;
2901 if (session
->shm_path
[0]) {
2902 strncpy(lus
->root_shm_path
, session
->shm_path
,
2903 sizeof(lus
->root_shm_path
));
2904 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2905 strncpy(lus
->shm_path
, session
->shm_path
,
2906 sizeof(lus
->shm_path
));
2907 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2908 strncat(lus
->shm_path
, "/ust",
2909 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2911 /* Copy session output to the newly created UST session */
2912 ret
= copy_session_consumer(domain
->type
, session
);
2913 if (ret
!= LTTNG_OK
) {
2921 session
->ust_session
= NULL
;
2926 * Create a kernel tracer session then create the default channel.
2928 static int create_kernel_session(struct ltt_session
*session
)
2932 DBG("Creating kernel session");
2934 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2936 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2940 /* Code flow safety */
2941 assert(session
->kernel_session
);
2943 /* Copy session output to the newly created Kernel session */
2944 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2945 if (ret
!= LTTNG_OK
) {
2949 /* Create directory(ies) on local filesystem. */
2950 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2951 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2952 ret
= run_as_mkdir_recursive(
2953 session
->kernel_session
->consumer
->dst
.trace_path
,
2954 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2956 if (errno
!= EEXIST
) {
2957 ERR("Trace directory creation error");
2963 session
->kernel_session
->uid
= session
->uid
;
2964 session
->kernel_session
->gid
= session
->gid
;
2965 session
->kernel_session
->output_traces
= session
->output_traces
;
2966 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2971 trace_kernel_destroy_session(session
->kernel_session
);
2972 session
->kernel_session
= NULL
;
2977 * Count number of session permitted by uid/gid.
2979 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2982 struct ltt_session
*session
;
2984 DBG("Counting number of available session for UID %d GID %d",
2986 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2988 * Only list the sessions the user can control.
2990 if (!session_access_ok(session
, uid
, gid
)) {
2999 * Process the command requested by the lttng client within the command
3000 * context structure. This function make sure that the return structure (llm)
3001 * is set and ready for transmission before returning.
3003 * Return any error encountered or 0 for success.
3005 * "sock" is only used for special-case var. len data.
3007 * Should *NOT* be called with RCU read-side lock held.
3009 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
3013 int need_tracing_session
= 1;
3016 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
3018 assert(!rcu_read_ongoing());
3022 switch (cmd_ctx
->lsm
->cmd_type
) {
3023 case LTTNG_CREATE_SESSION
:
3024 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3025 case LTTNG_CREATE_SESSION_LIVE
:
3026 case LTTNG_DESTROY_SESSION
:
3027 case LTTNG_LIST_SESSIONS
:
3028 case LTTNG_LIST_DOMAINS
:
3029 case LTTNG_START_TRACE
:
3030 case LTTNG_STOP_TRACE
:
3031 case LTTNG_DATA_PENDING
:
3032 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3033 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3034 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3035 case LTTNG_SNAPSHOT_RECORD
:
3036 case LTTNG_SAVE_SESSION
:
3037 case LTTNG_SET_SESSION_SHM_PATH
:
3044 if (opt_no_kernel
&& need_domain
3045 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
3047 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3049 ret
= LTTNG_ERR_KERN_NA
;
3054 /* Deny register consumer if we already have a spawned consumer. */
3055 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
3056 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3057 if (kconsumer_data
.pid
> 0) {
3058 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3059 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3062 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3066 * Check for command that don't needs to allocate a returned payload. We do
3067 * this here so we don't have to make the call for no payload at each
3070 switch(cmd_ctx
->lsm
->cmd_type
) {
3071 case LTTNG_LIST_SESSIONS
:
3072 case LTTNG_LIST_TRACEPOINTS
:
3073 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3074 case LTTNG_LIST_DOMAINS
:
3075 case LTTNG_LIST_CHANNELS
:
3076 case LTTNG_LIST_EVENTS
:
3077 case LTTNG_LIST_SYSCALLS
:
3078 case LTTNG_LIST_TRACKER_PIDS
:
3081 /* Setup lttng message with no payload */
3082 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
3084 /* This label does not try to unlock the session */
3085 goto init_setup_error
;
3089 /* Commands that DO NOT need a session. */
3090 switch (cmd_ctx
->lsm
->cmd_type
) {
3091 case LTTNG_CREATE_SESSION
:
3092 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3093 case LTTNG_CREATE_SESSION_LIVE
:
3094 case LTTNG_CALIBRATE
:
3095 case LTTNG_LIST_SESSIONS
:
3096 case LTTNG_LIST_TRACEPOINTS
:
3097 case LTTNG_LIST_SYSCALLS
:
3098 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3099 case LTTNG_SAVE_SESSION
:
3100 need_tracing_session
= 0;
3103 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3105 * We keep the session list lock across _all_ commands
3106 * for now, because the per-session lock does not
3107 * handle teardown properly.
3109 session_lock_list();
3110 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3111 if (cmd_ctx
->session
== NULL
) {
3112 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3115 /* Acquire lock for the session */
3116 session_lock(cmd_ctx
->session
);
3122 * Commands that need a valid session but should NOT create one if none
3123 * exists. Instead of creating one and destroying it when the command is
3124 * handled, process that right before so we save some round trip in useless
3127 switch (cmd_ctx
->lsm
->cmd_type
) {
3128 case LTTNG_DISABLE_CHANNEL
:
3129 case LTTNG_DISABLE_EVENT
:
3130 switch (cmd_ctx
->lsm
->domain
.type
) {
3131 case LTTNG_DOMAIN_KERNEL
:
3132 if (!cmd_ctx
->session
->kernel_session
) {
3133 ret
= LTTNG_ERR_NO_CHANNEL
;
3137 case LTTNG_DOMAIN_JUL
:
3138 case LTTNG_DOMAIN_LOG4J
:
3139 case LTTNG_DOMAIN_PYTHON
:
3140 case LTTNG_DOMAIN_UST
:
3141 if (!cmd_ctx
->session
->ust_session
) {
3142 ret
= LTTNG_ERR_NO_CHANNEL
;
3147 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3159 * Check domain type for specific "pre-action".
3161 switch (cmd_ctx
->lsm
->domain
.type
) {
3162 case LTTNG_DOMAIN_KERNEL
:
3164 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3168 /* Kernel tracer check */
3169 if (kernel_tracer_fd
== -1) {
3170 /* Basically, load kernel tracer modules */
3171 ret
= init_kernel_tracer();
3177 /* Consumer is in an ERROR state. Report back to client */
3178 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3179 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3183 /* Need a session for kernel command */
3184 if (need_tracing_session
) {
3185 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3186 ret
= create_kernel_session(cmd_ctx
->session
);
3188 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3193 /* Start the kernel consumer daemon */
3194 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3195 if (kconsumer_data
.pid
== 0 &&
3196 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3197 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3198 ret
= start_consumerd(&kconsumer_data
);
3200 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3203 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3205 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3209 * The consumer was just spawned so we need to add the socket to
3210 * the consumer output of the session if exist.
3212 ret
= consumer_create_socket(&kconsumer_data
,
3213 cmd_ctx
->session
->kernel_session
->consumer
);
3220 case LTTNG_DOMAIN_JUL
:
3221 case LTTNG_DOMAIN_LOG4J
:
3222 case LTTNG_DOMAIN_PYTHON
:
3223 case LTTNG_DOMAIN_UST
:
3225 if (!ust_app_supported()) {
3226 ret
= LTTNG_ERR_NO_UST
;
3229 /* Consumer is in an ERROR state. Report back to client */
3230 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3231 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3235 if (need_tracing_session
) {
3236 /* Create UST session if none exist. */
3237 if (cmd_ctx
->session
->ust_session
== NULL
) {
3238 ret
= create_ust_session(cmd_ctx
->session
,
3239 &cmd_ctx
->lsm
->domain
);
3240 if (ret
!= LTTNG_OK
) {
3245 /* Start the UST consumer daemons */
3247 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3248 if (consumerd64_bin
[0] != '\0' &&
3249 ustconsumer64_data
.pid
== 0 &&
3250 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3251 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3252 ret
= start_consumerd(&ustconsumer64_data
);
3254 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3255 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3259 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3260 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3262 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3266 * Setup socket for consumer 64 bit. No need for atomic access
3267 * since it was set above and can ONLY be set in this thread.
3269 ret
= consumer_create_socket(&ustconsumer64_data
,
3270 cmd_ctx
->session
->ust_session
->consumer
);
3276 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3277 if (consumerd32_bin
[0] != '\0' &&
3278 ustconsumer32_data
.pid
== 0 &&
3279 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3280 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3281 ret
= start_consumerd(&ustconsumer32_data
);
3283 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3284 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3288 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3289 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3291 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3295 * Setup socket for consumer 64 bit. No need for atomic access
3296 * since it was set above and can ONLY be set in this thread.
3298 ret
= consumer_create_socket(&ustconsumer32_data
,
3299 cmd_ctx
->session
->ust_session
->consumer
);
3311 /* Validate consumer daemon state when start/stop trace command */
3312 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3313 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3314 switch (cmd_ctx
->lsm
->domain
.type
) {
3315 case LTTNG_DOMAIN_JUL
:
3316 case LTTNG_DOMAIN_LOG4J
:
3317 case LTTNG_DOMAIN_PYTHON
:
3318 case LTTNG_DOMAIN_UST
:
3319 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3320 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3324 case LTTNG_DOMAIN_KERNEL
:
3325 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3326 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3334 * Check that the UID or GID match that of the tracing session.
3335 * The root user can interact with all sessions.
3337 if (need_tracing_session
) {
3338 if (!session_access_ok(cmd_ctx
->session
,
3339 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3340 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3341 ret
= LTTNG_ERR_EPERM
;
3347 * Send relayd information to consumer as soon as we have a domain and a
3350 if (cmd_ctx
->session
&& need_domain
) {
3352 * Setup relayd if not done yet. If the relayd information was already
3353 * sent to the consumer, this call will gracefully return.
3355 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3356 if (ret
!= LTTNG_OK
) {
3361 /* Process by command type */
3362 switch (cmd_ctx
->lsm
->cmd_type
) {
3363 case LTTNG_ADD_CONTEXT
:
3366 * An LTTNG_ADD_CONTEXT command might have a supplementary
3367 * payload if the context being added is an application context.
3369 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3370 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3371 char *provider_name
= NULL
, *context_name
= NULL
;
3372 size_t provider_name_len
=
3373 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3374 size_t context_name_len
=
3375 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3377 if (provider_name_len
== 0 || context_name_len
== 0) {
3379 * Application provider and context names MUST
3382 ret
= -LTTNG_ERR_INVALID
;
3386 provider_name
= zmalloc(provider_name_len
+ 1);
3387 if (!provider_name
) {
3388 ret
= -LTTNG_ERR_NOMEM
;
3391 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3394 context_name
= zmalloc(context_name_len
+ 1);
3395 if (!context_name
) {
3396 ret
= -LTTNG_ERR_NOMEM
;
3397 goto error_add_context
;
3399 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3402 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3405 goto error_add_context
;
3408 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3411 goto error_add_context
;
3416 * cmd_add_context assumes ownership of the provider and context
3419 ret
= cmd_add_context(cmd_ctx
->session
,
3420 cmd_ctx
->lsm
->domain
.type
,
3421 cmd_ctx
->lsm
->u
.context
.channel_name
,
3422 &cmd_ctx
->lsm
->u
.context
.ctx
,
3423 kernel_poll_pipe
[1]);
3425 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3426 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3428 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3429 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3435 case LTTNG_DISABLE_CHANNEL
:
3437 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3438 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3441 case LTTNG_DISABLE_EVENT
:
3445 * FIXME: handle filter; for now we just receive the filter's
3446 * bytecode along with the filter expression which are sent by
3447 * liblttng-ctl and discard them.
3449 * This fixes an issue where the client may block while sending
3450 * the filter payload and encounter an error because the session
3451 * daemon closes the socket without ever handling this data.
3453 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3454 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3457 char data
[LTTNG_FILTER_MAX_LEN
];
3459 DBG("Discarding disable event command payload of size %zu", count
);
3461 ret
= lttcomm_recv_unix_sock(sock
, data
,
3462 count
> sizeof(data
) ? sizeof(data
) : count
);
3467 count
-= (size_t) ret
;
3470 /* FIXME: passing packed structure to non-packed pointer */
3471 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3472 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3473 &cmd_ctx
->lsm
->u
.disable
.event
);
3476 case LTTNG_ENABLE_CHANNEL
:
3478 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3479 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
3482 case LTTNG_TRACK_PID
:
3484 ret
= cmd_track_pid(cmd_ctx
->session
,
3485 cmd_ctx
->lsm
->domain
.type
,
3486 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3489 case LTTNG_UNTRACK_PID
:
3491 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3492 cmd_ctx
->lsm
->domain
.type
,
3493 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3496 case LTTNG_ENABLE_EVENT
:
3498 struct lttng_event_exclusion
*exclusion
= NULL
;
3499 struct lttng_filter_bytecode
*bytecode
= NULL
;
3500 char *filter_expression
= NULL
;
3502 /* Handle exclusion events and receive it from the client. */
3503 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3504 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3506 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3507 (count
* LTTNG_SYMBOL_NAME_LEN
));
3509 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3513 DBG("Receiving var len exclusion event list from client ...");
3514 exclusion
->count
= count
;
3515 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3516 count
* LTTNG_SYMBOL_NAME_LEN
);
3518 DBG("Nothing recv() from client var len data... continuing");
3521 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3526 /* Get filter expression from client. */
3527 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3528 size_t expression_len
=
3529 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3531 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3532 ret
= LTTNG_ERR_FILTER_INVAL
;
3537 filter_expression
= zmalloc(expression_len
);
3538 if (!filter_expression
) {
3540 ret
= LTTNG_ERR_FILTER_NOMEM
;
3544 /* Receive var. len. data */
3545 DBG("Receiving var len filter's expression from client ...");
3546 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3549 DBG("Nothing recv() from client car len data... continuing");
3551 free(filter_expression
);
3553 ret
= LTTNG_ERR_FILTER_INVAL
;
3558 /* Handle filter and get bytecode from client. */
3559 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3560 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3562 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3563 ret
= LTTNG_ERR_FILTER_INVAL
;
3564 free(filter_expression
);
3569 bytecode
= zmalloc(bytecode_len
);
3571 free(filter_expression
);
3573 ret
= LTTNG_ERR_FILTER_NOMEM
;
3577 /* Receive var. len. data */
3578 DBG("Receiving var len filter's bytecode from client ...");
3579 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3581 DBG("Nothing recv() from client car len data... continuing");
3583 free(filter_expression
);
3586 ret
= LTTNG_ERR_FILTER_INVAL
;
3590 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3591 free(filter_expression
);
3594 ret
= LTTNG_ERR_FILTER_INVAL
;
3599 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3600 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3601 &cmd_ctx
->lsm
->u
.enable
.event
,
3602 filter_expression
, bytecode
, exclusion
,
3603 kernel_poll_pipe
[1]);
3606 case LTTNG_LIST_TRACEPOINTS
:
3608 struct lttng_event
*events
;
3611 session_lock_list();
3612 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3613 session_unlock_list();
3614 if (nb_events
< 0) {
3615 /* Return value is a negative lttng_error_code. */
3621 * Setup lttng message with payload size set to the event list size in
3622 * bytes and then copy list into the llm payload.
3624 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3625 sizeof(struct lttng_event
) * nb_events
);
3635 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3637 struct lttng_event_field
*fields
;
3640 session_lock_list();
3641 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3643 session_unlock_list();
3644 if (nb_fields
< 0) {
3645 /* Return value is a negative lttng_error_code. */
3651 * Setup lttng message with payload size set to the event list size in
3652 * bytes and then copy list into the llm payload.
3654 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3655 sizeof(struct lttng_event_field
) * nb_fields
);
3665 case LTTNG_LIST_SYSCALLS
:
3667 struct lttng_event
*events
;
3670 nb_events
= cmd_list_syscalls(&events
);
3671 if (nb_events
< 0) {
3672 /* Return value is a negative lttng_error_code. */
3678 * Setup lttng message with payload size set to the event list size in
3679 * bytes and then copy list into the llm payload.
3681 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3682 sizeof(struct lttng_event
) * nb_events
);
3692 case LTTNG_LIST_TRACKER_PIDS
:
3694 int32_t *pids
= NULL
;
3697 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3698 cmd_ctx
->lsm
->domain
.type
, &pids
);
3700 /* Return value is a negative lttng_error_code. */
3706 * Setup lttng message with payload size set to the event list size in
3707 * bytes and then copy list into the llm payload.
3709 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3710 sizeof(int32_t) * nr_pids
);
3720 case LTTNG_SET_CONSUMER_URI
:
3723 struct lttng_uri
*uris
;
3725 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3726 len
= nb_uri
* sizeof(struct lttng_uri
);
3729 ret
= LTTNG_ERR_INVALID
;
3733 uris
= zmalloc(len
);
3735 ret
= LTTNG_ERR_FATAL
;
3739 /* Receive variable len data */
3740 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3741 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3743 DBG("No URIs received from client... continuing");
3745 ret
= LTTNG_ERR_SESSION_FAIL
;
3750 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3752 if (ret
!= LTTNG_OK
) {
3759 case LTTNG_START_TRACE
:
3761 ret
= cmd_start_trace(cmd_ctx
->session
);
3764 case LTTNG_STOP_TRACE
:
3766 ret
= cmd_stop_trace(cmd_ctx
->session
);
3769 case LTTNG_CREATE_SESSION
:
3772 struct lttng_uri
*uris
= NULL
;
3774 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3775 len
= nb_uri
* sizeof(struct lttng_uri
);
3778 uris
= zmalloc(len
);
3780 ret
= LTTNG_ERR_FATAL
;
3784 /* Receive variable len data */
3785 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3786 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3788 DBG("No URIs received from client... continuing");
3790 ret
= LTTNG_ERR_SESSION_FAIL
;
3795 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3796 DBG("Creating session with ONE network URI is a bad call");
3797 ret
= LTTNG_ERR_SESSION_FAIL
;
3803 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3804 &cmd_ctx
->creds
, 0);
3810 case LTTNG_DESTROY_SESSION
:
3812 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3814 /* Set session to NULL so we do not unlock it after free. */
3815 cmd_ctx
->session
= NULL
;
3818 case LTTNG_LIST_DOMAINS
:
3821 struct lttng_domain
*domains
= NULL
;
3823 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3825 /* Return value is a negative lttng_error_code. */
3830 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3831 nb_dom
* sizeof(struct lttng_domain
));
3841 case LTTNG_LIST_CHANNELS
:
3844 struct lttng_channel
*channels
= NULL
;
3846 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3847 cmd_ctx
->session
, &channels
);
3849 /* Return value is a negative lttng_error_code. */
3854 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3855 nb_chan
* sizeof(struct lttng_channel
));
3865 case LTTNG_LIST_EVENTS
:
3868 struct lttng_event
*events
= NULL
;
3870 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3871 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
3873 /* Return value is a negative lttng_error_code. */
3878 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3879 nb_event
* sizeof(struct lttng_event
));
3889 case LTTNG_LIST_SESSIONS
:
3891 unsigned int nr_sessions
;
3892 void *sessions_payload
;
3895 session_lock_list();
3896 nr_sessions
= lttng_sessions_count(
3897 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3898 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3899 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
3900 sessions_payload
= zmalloc(payload_len
);
3902 if (!sessions_payload
) {
3903 session_unlock_list();
3908 cmd_list_lttng_sessions(sessions_payload
,
3909 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3910 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3911 session_unlock_list();
3913 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
3915 free(sessions_payload
);
3924 case LTTNG_CALIBRATE
:
3926 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3927 &cmd_ctx
->lsm
->u
.calibrate
);
3930 case LTTNG_REGISTER_CONSUMER
:
3932 struct consumer_data
*cdata
;
3934 switch (cmd_ctx
->lsm
->domain
.type
) {
3935 case LTTNG_DOMAIN_KERNEL
:
3936 cdata
= &kconsumer_data
;
3939 ret
= LTTNG_ERR_UND
;
3943 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3944 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3947 case LTTNG_DATA_PENDING
:
3950 uint8_t pending_ret_byte
;
3952 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
3957 * This function may returns 0 or 1 to indicate whether or not
3958 * there is data pending. In case of error, it should return an
3959 * LTTNG_ERR code. However, some code paths may still return
3960 * a nondescript error code, which we handle by returning an
3963 if (pending_ret
== 0 || pending_ret
== 1) {
3965 * ret will be set to LTTNG_OK at the end of
3968 } else if (pending_ret
< 0) {
3969 ret
= LTTNG_ERR_UNK
;
3976 pending_ret_byte
= (uint8_t) pending_ret
;
3978 /* 1 byte to return whether or not data is pending */
3979 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
3980 &pending_ret_byte
, 1);
3989 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3991 struct lttcomm_lttng_output_id reply
;
3993 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3994 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3995 if (ret
!= LTTNG_OK
) {
3999 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
4005 /* Copy output list into message payload */
4009 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
4011 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
4012 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
4015 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
4018 struct lttng_snapshot_output
*outputs
= NULL
;
4020 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
4021 if (nb_output
< 0) {
4026 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
4027 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
4028 nb_output
* sizeof(struct lttng_snapshot_output
));
4038 case LTTNG_SNAPSHOT_RECORD
:
4040 ret
= cmd_snapshot_record(cmd_ctx
->session
,
4041 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
4042 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
4045 case LTTNG_CREATE_SESSION_SNAPSHOT
:
4048 struct lttng_uri
*uris
= NULL
;
4050 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4051 len
= nb_uri
* sizeof(struct lttng_uri
);
4054 uris
= zmalloc(len
);
4056 ret
= LTTNG_ERR_FATAL
;
4060 /* Receive variable len data */
4061 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4062 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4064 DBG("No URIs received from client... continuing");
4066 ret
= LTTNG_ERR_SESSION_FAIL
;
4071 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4072 DBG("Creating session with ONE network URI is a bad call");
4073 ret
= LTTNG_ERR_SESSION_FAIL
;
4079 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
4080 nb_uri
, &cmd_ctx
->creds
);
4084 case LTTNG_CREATE_SESSION_LIVE
:
4087 struct lttng_uri
*uris
= NULL
;
4089 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4090 len
= nb_uri
* sizeof(struct lttng_uri
);
4093 uris
= zmalloc(len
);
4095 ret
= LTTNG_ERR_FATAL
;
4099 /* Receive variable len data */
4100 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4101 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4103 DBG("No URIs received from client... continuing");
4105 ret
= LTTNG_ERR_SESSION_FAIL
;
4110 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4111 DBG("Creating session with ONE network URI is a bad call");
4112 ret
= LTTNG_ERR_SESSION_FAIL
;
4118 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4119 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4123 case LTTNG_SAVE_SESSION
:
4125 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4129 case LTTNG_SET_SESSION_SHM_PATH
:
4131 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4132 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4136 ret
= LTTNG_ERR_UND
;
4141 if (cmd_ctx
->llm
== NULL
) {
4142 DBG("Missing llm structure. Allocating one.");
4143 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4147 /* Set return code */
4148 cmd_ctx
->llm
->ret_code
= ret
;
4150 if (cmd_ctx
->session
) {
4151 session_unlock(cmd_ctx
->session
);
4153 if (need_tracing_session
) {
4154 session_unlock_list();
4157 assert(!rcu_read_ongoing());
4162 * Thread managing health check socket.
4164 static void *thread_manage_health(void *data
)
4166 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4167 uint32_t revents
, nb_fd
;
4168 struct lttng_poll_event events
;
4169 struct health_comm_msg msg
;
4170 struct health_comm_reply reply
;
4172 DBG("[thread] Manage health check started");
4174 rcu_register_thread();
4176 /* We might hit an error path before this is created. */
4177 lttng_poll_init(&events
);
4179 /* Create unix socket */
4180 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
4182 ERR("Unable to create health check Unix socket");
4188 /* lttng health client socket path permissions */
4189 ret
= chown(health_unix_sock_path
, 0,
4190 utils_get_group_id(tracing_group_name
));
4192 ERR("Unable to set group on %s", health_unix_sock_path
);
4198 ret
= chmod(health_unix_sock_path
,
4199 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4201 ERR("Unable to set permissions on %s", health_unix_sock_path
);
4209 * Set the CLOEXEC flag. Return code is useless because either way, the
4212 (void) utils_set_fd_cloexec(sock
);
4214 ret
= lttcomm_listen_unix_sock(sock
);
4220 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4221 * more will be added to this poll set.
4223 ret
= sessiond_set_thread_pollset(&events
, 2);
4228 /* Add the application registration socket */
4229 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4234 sessiond_notify_ready();
4237 DBG("Health check ready");
4239 /* Inifinite blocking call, waiting for transmission */
4241 ret
= lttng_poll_wait(&events
, -1);
4244 * Restart interrupted system call.
4246 if (errno
== EINTR
) {
4254 for (i
= 0; i
< nb_fd
; i
++) {
4255 /* Fetch once the poll data */
4256 revents
= LTTNG_POLL_GETEV(&events
, i
);
4257 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4260 /* No activity for this FD (poll implementation). */
4264 /* Thread quit pipe has been closed. Killing thread. */
4265 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4271 /* Event on the registration socket */
4272 if (pollfd
== sock
) {
4273 if (revents
& LPOLLIN
) {
4275 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4276 ERR("Health socket poll error");
4279 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4285 new_sock
= lttcomm_accept_unix_sock(sock
);
4291 * Set the CLOEXEC flag. Return code is useless because either way, the
4294 (void) utils_set_fd_cloexec(new_sock
);
4296 DBG("Receiving data from client for health...");
4297 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4299 DBG("Nothing recv() from client... continuing");
4300 ret
= close(new_sock
);
4308 rcu_thread_online();
4310 memset(&reply
, 0, sizeof(reply
));
4311 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4313 * health_check_state returns 0 if health is
4316 if (!health_check_state(health_sessiond
, i
)) {
4317 reply
.ret_code
|= 1ULL << i
;
4321 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4323 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4325 ERR("Failed to send health data back to client");
4328 /* End of transmission */
4329 ret
= close(new_sock
);
4339 ERR("Health error occurred in %s", __func__
);
4341 DBG("Health check thread dying");
4342 unlink(health_unix_sock_path
);
4350 lttng_poll_clean(&events
);
4352 rcu_unregister_thread();
4357 * This thread manage all clients request using the unix client socket for
4360 static void *thread_manage_clients(void *data
)
4362 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4364 uint32_t revents
, nb_fd
;
4365 struct command_ctx
*cmd_ctx
= NULL
;
4366 struct lttng_poll_event events
;
4368 DBG("[thread] Manage client started");
4370 rcu_register_thread();
4372 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4374 health_code_update();
4376 ret
= lttcomm_listen_unix_sock(client_sock
);
4382 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4383 * more will be added to this poll set.
4385 ret
= sessiond_set_thread_pollset(&events
, 2);
4387 goto error_create_poll
;
4390 /* Add the application registration socket */
4391 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4396 sessiond_notify_ready();
4397 ret
= sem_post(&load_info
->message_thread_ready
);
4399 PERROR("sem_post message_thread_ready");
4403 /* This testpoint is after we signal readiness to the parent. */
4404 if (testpoint(sessiond_thread_manage_clients
)) {
4408 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4412 health_code_update();
4415 DBG("Accepting client command ...");
4417 /* Inifinite blocking call, waiting for transmission */
4419 health_poll_entry();
4420 ret
= lttng_poll_wait(&events
, -1);
4424 * Restart interrupted system call.
4426 if (errno
== EINTR
) {
4434 for (i
= 0; i
< nb_fd
; i
++) {
4435 /* Fetch once the poll data */
4436 revents
= LTTNG_POLL_GETEV(&events
, i
);
4437 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4439 health_code_update();
4442 /* No activity for this FD (poll implementation). */
4446 /* Thread quit pipe has been closed. Killing thread. */
4447 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4453 /* Event on the registration socket */
4454 if (pollfd
== client_sock
) {
4455 if (revents
& LPOLLIN
) {
4457 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4458 ERR("Client socket poll error");
4461 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4467 DBG("Wait for client response");
4469 health_code_update();
4471 sock
= lttcomm_accept_unix_sock(client_sock
);
4477 * Set the CLOEXEC flag. Return code is useless because either way, the
4480 (void) utils_set_fd_cloexec(sock
);
4482 /* Set socket option for credentials retrieval */
4483 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4488 /* Allocate context command to process the client request */
4489 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4490 if (cmd_ctx
== NULL
) {
4491 PERROR("zmalloc cmd_ctx");
4495 /* Allocate data buffer for reception */
4496 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4497 if (cmd_ctx
->lsm
== NULL
) {
4498 PERROR("zmalloc cmd_ctx->lsm");
4502 cmd_ctx
->llm
= NULL
;
4503 cmd_ctx
->session
= NULL
;
4505 health_code_update();
4508 * Data is received from the lttng client. The struct
4509 * lttcomm_session_msg (lsm) contains the command and data request of
4512 DBG("Receiving data from client ...");
4513 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4514 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4516 DBG("Nothing recv() from client... continuing");
4522 clean_command_ctx(&cmd_ctx
);
4526 health_code_update();
4528 // TODO: Validate cmd_ctx including sanity check for
4529 // security purpose.
4531 rcu_thread_online();
4533 * This function dispatch the work to the kernel or userspace tracer
4534 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4535 * informations for the client. The command context struct contains
4536 * everything this function may needs.
4538 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4539 rcu_thread_offline();
4547 * TODO: Inform client somehow of the fatal error. At
4548 * this point, ret < 0 means that a zmalloc failed
4549 * (ENOMEM). Error detected but still accept
4550 * command, unless a socket error has been
4553 clean_command_ctx(&cmd_ctx
);
4557 health_code_update();
4559 DBG("Sending response (size: %d, retcode: %s (%d))",
4560 cmd_ctx
->lttng_msg_size
,
4561 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4562 cmd_ctx
->llm
->ret_code
);
4563 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4565 ERR("Failed to send data back to client");
4568 /* End of transmission */
4575 clean_command_ctx(&cmd_ctx
);
4577 health_code_update();
4589 lttng_poll_clean(&events
);
4590 clean_command_ctx(&cmd_ctx
);
4594 unlink(client_unix_sock_path
);
4595 if (client_sock
>= 0) {
4596 ret
= close(client_sock
);
4604 ERR("Health error occurred in %s", __func__
);
4607 health_unregister(health_sessiond
);
4609 DBG("Client thread dying");
4611 rcu_unregister_thread();
4614 * Since we are creating the consumer threads, we own them, so we need
4615 * to join them before our thread exits.
4617 ret
= join_consumer_thread(&kconsumer_data
);
4620 PERROR("join_consumer");
4623 ret
= join_consumer_thread(&ustconsumer32_data
);
4626 PERROR("join_consumer ust32");
4629 ret
= join_consumer_thread(&ustconsumer64_data
);
4632 PERROR("join_consumer ust64");
4639 * usage function on stderr
4641 static void usage(void)
4643 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
4644 fprintf(stderr
, " -h, --help Display this usage.\n");
4645 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
4646 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
4647 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
4648 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
4649 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
4650 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
4651 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
4652 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
4653 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
4654 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
4655 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
4656 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
4657 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
4658 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
4659 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
4660 fprintf(stderr
, " -V, --version Show version number.\n");
4661 fprintf(stderr
, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n");
4662 fprintf(stderr
, " -q, --quiet No output at all.\n");
4663 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
4664 fprintf(stderr
, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
4665 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4666 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
4667 fprintf(stderr
, " --agent-tcp-port Agent registration TCP port\n");
4668 fprintf(stderr
, " -f --config PATH Load daemon configuration file\n");
4669 fprintf(stderr
, " -l --load PATH Load session configuration\n");
4670 fprintf(stderr
, " --kmod-probes Specify kernel module probes to load\n");
4671 fprintf(stderr
, " --extra-kmod-probes Specify extra kernel module probes to load\n");
4674 static int string_match(const char *str1
, const char *str2
)
4676 return (str1
&& str2
) && !strcmp(str1
, str2
);
4680 * Take an option from the getopt output and set it in the right variable to be
4683 * Return 0 on success else a negative value.
4685 static int set_option(int opt
, const char *arg
, const char *optname
)
4689 if (arg
&& arg
[0] == '\0') {
4691 * This only happens if the value is read from daemon config
4692 * file. This means the option requires an argument and the
4693 * configuration file contains a line such as:
4700 if (string_match(optname
, "client-sock") || opt
== 'c') {
4701 if (lttng_is_setuid_setgid()) {
4702 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4703 "-c, --client-sock");
4705 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", arg
);
4707 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4708 if (lttng_is_setuid_setgid()) {
4709 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4712 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", arg
);
4714 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4716 } else if (string_match(optname
, "background") || opt
== 'b') {
4718 } else if (string_match(optname
, "group") || opt
== 'g') {
4719 if (lttng_is_setuid_setgid()) {
4720 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4724 * If the override option is set, the pointer points to a
4725 * *non* const thus freeing it even though the variable type is
4728 if (tracing_group_name_override
) {
4729 free((void *) tracing_group_name
);
4731 tracing_group_name
= strdup(arg
);
4732 if (!tracing_group_name
) {
4736 tracing_group_name_override
= 1;
4738 } else if (string_match(optname
, "help") || opt
== 'h') {
4741 } else if (string_match(optname
, "version") || opt
== 'V') {
4742 fprintf(stdout
, "%s\n", VERSION
);
4744 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4746 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4747 if (lttng_is_setuid_setgid()) {
4748 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4749 "--kconsumerd-err-sock");
4751 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4753 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4754 if (lttng_is_setuid_setgid()) {
4755 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4756 "--kconsumerd-cmd-sock");
4758 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4760 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4761 if (lttng_is_setuid_setgid()) {
4762 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4763 "--ustconsumerd64-err-sock");
4765 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4767 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4768 if (lttng_is_setuid_setgid()) {
4769 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4770 "--ustconsumerd64-cmd-sock");
4772 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4774 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4775 if (lttng_is_setuid_setgid()) {
4776 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4777 "--ustconsumerd32-err-sock");
4779 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4781 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4782 if (lttng_is_setuid_setgid()) {
4783 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4784 "--ustconsumerd32-cmd-sock");
4786 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4788 } else if (string_match(optname
, "no-kernel")) {
4790 } else if (string_match(optname
, "quiet") || opt
== 'q') {
4791 lttng_opt_quiet
= 1;
4792 } else if (string_match(optname
, "verbose") || opt
== 'v') {
4793 /* Verbose level can increase using multiple -v */
4795 /* Value obtained from config file */
4796 lttng_opt_verbose
= config_parse_value(arg
);
4798 /* -v used on command line */
4799 lttng_opt_verbose
++;
4801 /* Clamp value to [0, 3] */
4802 lttng_opt_verbose
= lttng_opt_verbose
< 0 ? 0 :
4803 (lttng_opt_verbose
<= 3 ? lttng_opt_verbose
: 3);
4804 } else if (string_match(optname
, "verbose-consumer")) {
4806 opt_verbose_consumer
= config_parse_value(arg
);
4808 opt_verbose_consumer
+= 1;
4810 } else if (string_match(optname
, "consumerd32-path")) {
4811 if (lttng_is_setuid_setgid()) {
4812 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4813 "--consumerd32-path");
4815 if (consumerd32_bin_override
) {
4816 free((void *) consumerd32_bin
);
4818 consumerd32_bin
= strdup(arg
);
4819 if (!consumerd32_bin
) {
4823 consumerd32_bin_override
= 1;
4825 } else if (string_match(optname
, "consumerd32-libdir")) {
4826 if (lttng_is_setuid_setgid()) {
4827 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4828 "--consumerd32-libdir");
4830 if (consumerd32_libdir_override
) {
4831 free((void *) consumerd32_libdir
);
4833 consumerd32_libdir
= strdup(arg
);
4834 if (!consumerd32_libdir
) {
4838 consumerd32_libdir_override
= 1;
4840 } else if (string_match(optname
, "consumerd64-path")) {
4841 if (lttng_is_setuid_setgid()) {
4842 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4843 "--consumerd64-path");
4845 if (consumerd64_bin_override
) {
4846 free((void *) consumerd64_bin
);
4848 consumerd64_bin
= strdup(arg
);
4849 if (!consumerd64_bin
) {
4853 consumerd64_bin_override
= 1;
4855 } else if (string_match(optname
, "consumerd64-libdir")) {
4856 if (lttng_is_setuid_setgid()) {
4857 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4858 "--consumerd64-libdir");
4860 if (consumerd64_libdir_override
) {
4861 free((void *) consumerd64_libdir
);
4863 consumerd64_libdir
= strdup(arg
);
4864 if (!consumerd64_libdir
) {
4868 consumerd64_libdir_override
= 1;
4870 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
4871 if (lttng_is_setuid_setgid()) {
4872 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4876 opt_pidfile
= strdup(arg
);
4882 } else if (string_match(optname
, "agent-tcp-port")) {
4883 if (lttng_is_setuid_setgid()) {
4884 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4885 "--agent-tcp-port");
4894 v
= strtoul(arg
, NULL
, 0);
4895 if (errno
!= 0 || !isdigit(arg
[0])) {
4896 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
4899 if (v
== 0 || v
>= 65535) {
4900 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
4903 agent_tcp_port
= (uint32_t) v
;
4904 DBG3("Agent TCP port set to non default: %u", agent_tcp_port
);
4906 } else if (string_match(optname
, "load") || opt
== 'l') {
4907 if (lttng_is_setuid_setgid()) {
4908 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4911 free(opt_load_session_path
);
4912 opt_load_session_path
= strdup(arg
);
4913 if (!opt_load_session_path
) {
4918 } else if (string_match(optname
, "kmod-probes")) {
4919 if (lttng_is_setuid_setgid()) {
4920 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4923 free(kmod_probes_list
);
4924 kmod_probes_list
= strdup(arg
);
4925 if (!kmod_probes_list
) {
4930 } else if (string_match(optname
, "extra-kmod-probes")) {
4931 if (lttng_is_setuid_setgid()) {
4932 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4933 "--extra-kmod-probes");
4935 free(kmod_extra_probes_list
);
4936 kmod_extra_probes_list
= strdup(arg
);
4937 if (!kmod_extra_probes_list
) {
4942 } else if (string_match(optname
, "config") || opt
== 'f') {
4943 /* This is handled in set_options() thus silent skip. */
4946 /* Unknown option or other error.
4947 * Error is printed by getopt, just return */
4952 if (ret
== -EINVAL
) {
4953 const char *opt_name
= "unknown";
4956 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
4958 if (opt
== long_options
[i
].val
) {
4959 opt_name
= long_options
[i
].name
;
4964 WARN("Invalid argument provided for option \"%s\", using default value.",
4972 * config_entry_handler_cb used to handle options read from a config file.
4973 * See config_entry_handler_cb comment in common/config/session-config.h for the
4974 * return value conventions.
4976 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
4980 if (!entry
|| !entry
->name
|| !entry
->value
) {
4985 /* Check if the option is to be ignored */
4986 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
4987 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
4992 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
4995 /* Ignore if not fully matched. */
4996 if (strcmp(entry
->name
, long_options
[i
].name
)) {
5001 * If the option takes no argument on the command line, we have to
5002 * check if the value is "true". We support non-zero numeric values,
5005 if (!long_options
[i
].has_arg
) {
5006 ret
= config_parse_value(entry
->value
);
5009 WARN("Invalid configuration value \"%s\" for option %s",
5010 entry
->value
, entry
->name
);
5012 /* False, skip boolean config option. */
5017 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
5021 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
5028 * daemon configuration loading and argument parsing
5030 static int set_options(int argc
, char **argv
)
5032 int ret
= 0, c
= 0, option_index
= 0;
5033 int orig_optopt
= optopt
, orig_optind
= optind
;
5035 const char *config_path
= NULL
;
5037 optstring
= utils_generate_optstring(long_options
,
5038 sizeof(long_options
) / sizeof(struct option
));
5044 /* Check for the --config option */
5045 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
5046 &option_index
)) != -1) {
5050 } else if (c
!= 'f') {
5051 /* if not equal to --config option. */
5055 if (lttng_is_setuid_setgid()) {
5056 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5059 config_path
= utils_expand_path(optarg
);
5061 ERR("Failed to resolve path: %s", optarg
);
5066 ret
= config_get_section_entries(config_path
, config_section_name
,
5067 config_entry_handler
, NULL
);
5070 ERR("Invalid configuration option at line %i", ret
);
5076 /* Reset getopt's global state */
5077 optopt
= orig_optopt
;
5078 optind
= orig_optind
;
5082 * getopt_long() will not set option_index if it encounters a
5085 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5092 * Pass NULL as the long option name if popt left the index
5095 ret
= set_option(c
, optarg
,
5096 option_index
< 0 ? NULL
:
5097 long_options
[option_index
].name
);
5109 * Creates the two needed socket by the daemon.
5110 * apps_sock - The communication socket for all UST apps.
5111 * client_sock - The communication of the cli tool (lttng).
5113 static int init_daemon_socket(void)
5118 old_umask
= umask(0);
5120 /* Create client tool unix socket */
5121 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
5122 if (client_sock
< 0) {
5123 ERR("Create unix sock failed: %s", client_unix_sock_path
);
5128 /* Set the cloexec flag */
5129 ret
= utils_set_fd_cloexec(client_sock
);
5131 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5132 "Continuing but note that the consumer daemon will have a "
5133 "reference to this socket on exec()", client_sock
);
5136 /* File permission MUST be 660 */
5137 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5139 ERR("Set file permissions failed: %s", client_unix_sock_path
);
5144 /* Create the application unix socket */
5145 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
5146 if (apps_sock
< 0) {
5147 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
5152 /* Set the cloexec flag */
5153 ret
= utils_set_fd_cloexec(apps_sock
);
5155 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5156 "Continuing but note that the consumer daemon will have a "
5157 "reference to this socket on exec()", apps_sock
);
5160 /* File permission MUST be 666 */
5161 ret
= chmod(apps_unix_sock_path
,
5162 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5164 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
5169 DBG3("Session daemon client socket %d and application socket %d created",
5170 client_sock
, apps_sock
);
5178 * Check if the global socket is available, and if a daemon is answering at the
5179 * other side. If yes, error is returned.
5181 static int check_existing_daemon(void)
5183 /* Is there anybody out there ? */
5184 if (lttng_session_daemon_alive()) {
5192 * Set the tracing group gid onto the client socket.
5194 * Race window between mkdir and chown is OK because we are going from more
5195 * permissive (root.root) to less permissive (root.tracing).
5197 static int set_permissions(char *rundir
)
5202 gid
= utils_get_group_id(tracing_group_name
);
5204 /* Set lttng run dir */
5205 ret
= chown(rundir
, 0, gid
);
5207 ERR("Unable to set group on %s", rundir
);
5212 * Ensure all applications and tracing group can search the run
5213 * dir. Allow everyone to read the directory, since it does not
5214 * buy us anything to hide its content.
5216 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5218 ERR("Unable to set permissions on %s", rundir
);
5222 /* lttng client socket path */
5223 ret
= chown(client_unix_sock_path
, 0, gid
);
5225 ERR("Unable to set group on %s", client_unix_sock_path
);
5229 /* kconsumer error socket path */
5230 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5232 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5236 /* 64-bit ustconsumer error socket path */
5237 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5239 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5243 /* 32-bit ustconsumer compat32 error socket path */
5244 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5246 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5250 DBG("All permissions are set");
5256 * Create the lttng run directory needed for all global sockets and pipe.
5258 static int create_lttng_rundir(const char *rundir
)
5262 DBG3("Creating LTTng run directory: %s", rundir
);
5264 ret
= mkdir(rundir
, S_IRWXU
);
5266 if (errno
!= EEXIST
) {
5267 ERR("Unable to create %s", rundir
);
5279 * Setup sockets and directory needed by the kconsumerd communication with the
5282 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
5286 char path
[PATH_MAX
];
5288 switch (consumer_data
->type
) {
5289 case LTTNG_CONSUMER_KERNEL
:
5290 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
5292 case LTTNG_CONSUMER64_UST
:
5293 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
5295 case LTTNG_CONSUMER32_UST
:
5296 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
5299 ERR("Consumer type unknown");
5304 DBG2("Creating consumer directory: %s", path
);
5306 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5308 if (errno
!= EEXIST
) {
5310 ERR("Failed to create %s", path
);
5316 ret
= chown(path
, 0, utils_get_group_id(tracing_group_name
));
5318 ERR("Unable to set group on %s", path
);
5324 /* Create the kconsumerd error unix socket */
5325 consumer_data
->err_sock
=
5326 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5327 if (consumer_data
->err_sock
< 0) {
5328 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5334 * Set the CLOEXEC flag. Return code is useless because either way, the
5337 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5339 PERROR("utils_set_fd_cloexec");
5340 /* continue anyway */
5343 /* File permission MUST be 660 */
5344 ret
= chmod(consumer_data
->err_unix_sock_path
,
5345 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5347 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5357 * Signal handler for the daemon
5359 * Simply stop all worker threads, leaving main() return gracefully after
5360 * joining all threads and calling cleanup().
5362 static void sighandler(int sig
)
5366 DBG("SIGPIPE caught");
5369 DBG("SIGINT caught");
5373 DBG("SIGTERM caught");
5377 CMM_STORE_SHARED(recv_child_signal
, 1);
5385 * Setup signal handler for :
5386 * SIGINT, SIGTERM, SIGPIPE
5388 static int set_signal_handler(void)
5391 struct sigaction sa
;
5394 if ((ret
= sigemptyset(&sigset
)) < 0) {
5395 PERROR("sigemptyset");
5399 sa
.sa_handler
= sighandler
;
5400 sa
.sa_mask
= sigset
;
5402 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5403 PERROR("sigaction");
5407 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5408 PERROR("sigaction");
5412 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5413 PERROR("sigaction");
5417 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5418 PERROR("sigaction");
5422 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5428 * Set open files limit to unlimited. This daemon can open a large number of
5429 * file descriptors in order to consumer multiple kernel traces.
5431 static void set_ulimit(void)
5436 /* The kernel does not allowed an infinite limit for open files */
5437 lim
.rlim_cur
= 65535;
5438 lim
.rlim_max
= 65535;
5440 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5442 PERROR("failed to set open files limit");
5447 * Write pidfile using the rundir and opt_pidfile.
5449 static int write_pidfile(void)
5452 char pidfile_path
[PATH_MAX
];
5457 strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
));
5459 /* Build pidfile path from rundir and opt_pidfile. */
5460 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
5461 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
5463 PERROR("snprintf pidfile path");
5469 * Create pid file in rundir.
5471 ret
= utils_create_pid_file(getpid(), pidfile_path
);
5477 * Create lockfile using the rundir and return its fd.
5479 static int create_lockfile(void)
5482 char lockfile_path
[PATH_MAX
];
5484 ret
= generate_lock_file_path(lockfile_path
, sizeof(lockfile_path
));
5489 ret
= utils_create_lock_file(lockfile_path
);
5495 * Write agent TCP port using the rundir.
5497 static int write_agent_port(void)
5500 char path
[PATH_MAX
];
5504 ret
= snprintf(path
, sizeof(path
), "%s/"
5505 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
, rundir
);
5507 PERROR("snprintf agent port path");
5512 * Create TCP agent port file in rundir.
5514 ret
= utils_create_pid_file(agent_tcp_port
, path
);
5523 int main(int argc
, char **argv
)
5525 int ret
= 0, retval
= 0;
5527 const char *home_path
, *env_app_timeout
;
5529 init_kernel_workarounds();
5531 rcu_register_thread();
5533 if (set_signal_handler()) {
5535 goto exit_set_signal_handler
;
5538 setup_consumerd_path();
5540 page_size
= sysconf(_SC_PAGESIZE
);
5541 if (page_size
< 0) {
5542 PERROR("sysconf _SC_PAGESIZE");
5543 page_size
= LONG_MAX
;
5544 WARN("Fallback page size to %ld", page_size
);
5548 * Parse arguments and load the daemon configuration file.
5550 * We have an exit_options exit path to free memory reserved by
5551 * set_options. This is needed because the rest of sessiond_cleanup()
5552 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5553 * depends on set_options.
5556 if (set_options(argc
, argv
)) {
5562 if (opt_daemon
|| opt_background
) {
5565 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5573 * We are in the child. Make sure all other file descriptors are
5574 * closed, in case we are called with more opened file
5575 * descriptors than the standard ones.
5577 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5582 if (run_as_create_worker(argv
[0]) < 0) {
5583 goto exit_create_run_as_worker_cleanup
;
5587 * Starting from here, we can create threads. This needs to be after
5588 * lttng_daemonize due to RCU.
5592 * Initialize the health check subsystem. This call should set the
5593 * appropriate time values.
5595 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5596 if (!health_sessiond
) {
5597 PERROR("health_app_create error");
5599 goto exit_health_sessiond_cleanup
;
5602 if (init_ht_cleanup_quit_pipe()) {
5604 goto exit_ht_cleanup_quit_pipe
;
5607 /* Setup the thread ht_cleanup communication pipe. */
5608 if (utils_create_pipe_cloexec(ht_cleanup_pipe
)) {
5610 goto exit_ht_cleanup_pipe
;
5613 /* Set up max poll set size */
5614 if (lttng_poll_set_max_size()) {
5616 goto exit_set_max_size
;
5619 /* Create thread to clean up RCU hash tables */
5620 ret
= pthread_create(&ht_cleanup_thread
, NULL
,
5621 thread_ht_cleanup
, (void *) NULL
);
5624 PERROR("pthread_create ht_cleanup");
5626 goto exit_ht_cleanup
;
5629 /* Create thread quit pipe */
5630 if (init_thread_quit_pipe()) {
5632 goto exit_init_data
;
5635 /* Check if daemon is UID = 0 */
5636 is_root
= !getuid();
5639 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
5642 goto exit_init_data
;
5645 /* Create global run dir with root access */
5646 if (create_lttng_rundir(rundir
)) {
5648 goto exit_init_data
;
5651 if (strlen(apps_unix_sock_path
) == 0) {
5652 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5653 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
5656 goto exit_init_data
;
5660 if (strlen(client_unix_sock_path
) == 0) {
5661 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5662 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
5665 goto exit_init_data
;
5669 /* Set global SHM for ust */
5670 if (strlen(wait_shm_path
) == 0) {
5671 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5672 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
5675 goto exit_init_data
;
5679 if (strlen(health_unix_sock_path
) == 0) {
5680 ret
= snprintf(health_unix_sock_path
,
5681 sizeof(health_unix_sock_path
),
5682 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
5685 goto exit_init_data
;
5689 /* Setup kernel consumerd path */
5690 ret
= snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
5691 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
5694 goto exit_init_data
;
5696 ret
= snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
5697 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
5700 goto exit_init_data
;
5703 DBG2("Kernel consumer err path: %s",
5704 kconsumer_data
.err_unix_sock_path
);
5705 DBG2("Kernel consumer cmd path: %s",
5706 kconsumer_data
.cmd_unix_sock_path
);
5708 home_path
= utils_get_home_dir();
5709 if (home_path
== NULL
) {
5710 /* TODO: Add --socket PATH option */
5711 ERR("Can't get HOME directory for sockets creation.");
5713 goto exit_init_data
;
5717 * Create rundir from home path. This will create something like
5720 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
5723 goto exit_init_data
;
5726 if (create_lttng_rundir(rundir
)) {
5728 goto exit_init_data
;
5731 if (strlen(apps_unix_sock_path
) == 0) {
5732 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5733 DEFAULT_HOME_APPS_UNIX_SOCK
,
5737 goto exit_init_data
;
5741 /* Set the cli tool unix socket path */
5742 if (strlen(client_unix_sock_path
) == 0) {
5743 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5744 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
5748 goto exit_init_data
;
5752 /* Set global SHM for ust */
5753 if (strlen(wait_shm_path
) == 0) {
5754 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5755 DEFAULT_HOME_APPS_WAIT_SHM_PATH
,
5759 goto exit_init_data
;
5763 /* Set health check Unix path */
5764 if (strlen(health_unix_sock_path
) == 0) {
5765 ret
= snprintf(health_unix_sock_path
,
5766 sizeof(health_unix_sock_path
),
5767 DEFAULT_HOME_HEALTH_UNIX_SOCK
,
5771 goto exit_init_data
;
5776 lockfile_fd
= create_lockfile();
5777 if (lockfile_fd
< 0) {
5779 goto exit_init_data
;
5782 /* Set consumer initial state */
5783 kernel_consumerd_state
= CONSUMER_STOPPED
;
5784 ust_consumerd_state
= CONSUMER_STOPPED
;
5786 DBG("Client socket path %s", client_unix_sock_path
);
5787 DBG("Application socket path %s", apps_unix_sock_path
);
5788 DBG("Application wait path %s", wait_shm_path
);
5789 DBG("LTTng run directory path: %s", rundir
);
5791 /* 32 bits consumerd path setup */
5792 ret
= snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
5793 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
5795 PERROR("snprintf 32-bit consumer error socket path");
5797 goto exit_init_data
;
5799 ret
= snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
5800 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
5802 PERROR("snprintf 32-bit consumer command socket path");
5804 goto exit_init_data
;
5807 DBG2("UST consumer 32 bits err path: %s",
5808 ustconsumer32_data
.err_unix_sock_path
);
5809 DBG2("UST consumer 32 bits cmd path: %s",
5810 ustconsumer32_data
.cmd_unix_sock_path
);
5812 /* 64 bits consumerd path setup */
5813 ret
= snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
5814 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
5816 PERROR("snprintf 64-bit consumer error socket path");
5818 goto exit_init_data
;
5820 ret
= snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
5821 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
5823 PERROR("snprintf 64-bit consumer command socket path");
5825 goto exit_init_data
;
5828 DBG2("UST consumer 64 bits err path: %s",
5829 ustconsumer64_data
.err_unix_sock_path
);
5830 DBG2("UST consumer 64 bits cmd path: %s",
5831 ustconsumer64_data
.cmd_unix_sock_path
);
5834 * See if daemon already exist.
5836 if (check_existing_daemon()) {
5837 ERR("Already running daemon.\n");
5839 * We do not goto exit because we must not cleanup()
5840 * because a daemon is already running.
5843 goto exit_init_data
;
5847 * Init UST app hash table. Alloc hash table before this point since
5848 * cleanup() can get called after that point.
5850 if (ust_app_ht_alloc()) {
5851 ERR("Failed to allocate UST app hash table");
5853 goto exit_init_data
;
5857 * Initialize agent app hash table. We allocate the hash table here
5858 * since cleanup() can get called after this point.
5860 if (agent_app_ht_alloc()) {
5861 ERR("Failed to allocate Agent app hash table");
5863 goto exit_init_data
;
5867 * These actions must be executed as root. We do that *after* setting up
5868 * the sockets path because we MUST make the check for another daemon using
5869 * those paths *before* trying to set the kernel consumer sockets and init
5873 if (set_consumer_sockets(&kconsumer_data
, rundir
)) {
5875 goto exit_init_data
;
5878 /* Setup kernel tracer */
5879 if (!opt_no_kernel
) {
5880 init_kernel_tracer();
5881 if (kernel_tracer_fd
>= 0) {
5882 ret
= syscall_init_table();
5884 ERR("Unable to populate syscall table. "
5885 "Syscall tracing won't work "
5886 "for this session daemon.");
5891 /* Set ulimit for open files */
5894 /* init lttng_fd tracking must be done after set_ulimit. */
5897 if (set_consumer_sockets(&ustconsumer64_data
, rundir
)) {
5899 goto exit_init_data
;
5902 if (set_consumer_sockets(&ustconsumer32_data
, rundir
)) {
5904 goto exit_init_data
;
5907 /* Setup the needed unix socket */
5908 if (init_daemon_socket()) {
5910 goto exit_init_data
;
5913 /* Set credentials to socket */
5914 if (is_root
&& set_permissions(rundir
)) {
5916 goto exit_init_data
;
5919 /* Get parent pid if -S, --sig-parent is specified. */
5920 if (opt_sig_parent
) {
5924 /* Setup the kernel pipe for waking up the kernel thread */
5925 if (is_root
&& !opt_no_kernel
) {
5926 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
5928 goto exit_init_data
;
5932 /* Setup the thread apps communication pipe. */
5933 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
5935 goto exit_init_data
;
5938 /* Setup the thread apps notify communication pipe. */
5939 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
5941 goto exit_init_data
;
5944 /* Initialize global buffer per UID and PID registry. */
5945 buffer_reg_init_uid_registry();
5946 buffer_reg_init_pid_registry();
5948 /* Init UST command queue. */
5949 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
5952 * Get session list pointer. This pointer MUST NOT be free'd. This list
5953 * is statically declared in session.c
5955 session_list_ptr
= session_get_list();
5959 /* Check for the application socket timeout env variable. */
5960 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
5961 if (env_app_timeout
) {
5962 app_socket_timeout
= atoi(env_app_timeout
);
5964 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
5967 ret
= write_pidfile();
5969 ERR("Error in write_pidfile");
5971 goto exit_init_data
;
5973 ret
= write_agent_port();
5975 ERR("Error in write_agent_port");
5977 goto exit_init_data
;
5980 /* Initialize communication library */
5982 /* Initialize TCP timeout values */
5983 lttcomm_inet_init();
5985 if (load_session_init_data(&load_info
) < 0) {
5987 goto exit_init_data
;
5989 load_info
->path
= opt_load_session_path
;
5991 /* Create health-check thread */
5992 ret
= pthread_create(&health_thread
, NULL
,
5993 thread_manage_health
, (void *) NULL
);
5996 PERROR("pthread_create health");
6001 /* Create thread to manage the client socket */
6002 ret
= pthread_create(&client_thread
, NULL
,
6003 thread_manage_clients
, (void *) NULL
);
6006 PERROR("pthread_create clients");
6011 /* Create thread to dispatch registration */
6012 ret
= pthread_create(&dispatch_thread
, NULL
,
6013 thread_dispatch_ust_registration
, (void *) NULL
);
6016 PERROR("pthread_create dispatch");
6021 /* Create thread to manage application registration. */
6022 ret
= pthread_create(®_apps_thread
, NULL
,
6023 thread_registration_apps
, (void *) NULL
);
6026 PERROR("pthread_create registration");
6031 /* Create thread to manage application socket */
6032 ret
= pthread_create(&apps_thread
, NULL
,
6033 thread_manage_apps
, (void *) NULL
);
6036 PERROR("pthread_create apps");
6041 /* Create thread to manage application notify socket */
6042 ret
= pthread_create(&apps_notify_thread
, NULL
,
6043 ust_thread_manage_notify
, (void *) NULL
);
6046 PERROR("pthread_create notify");
6048 goto exit_apps_notify
;
6051 /* Create agent registration thread. */
6052 ret
= pthread_create(&agent_reg_thread
, NULL
,
6053 agent_thread_manage_registration
, (void *) NULL
);
6056 PERROR("pthread_create agent");
6058 goto exit_agent_reg
;
6061 /* Don't start this thread if kernel tracing is not requested nor root */
6062 if (is_root
&& !opt_no_kernel
) {
6063 /* Create kernel thread to manage kernel event */
6064 ret
= pthread_create(&kernel_thread
, NULL
,
6065 thread_manage_kernel
, (void *) NULL
);
6068 PERROR("pthread_create kernel");
6074 /* Create session loading thread. */
6075 ret
= pthread_create(&load_session_thread
, NULL
, thread_load_session
,
6079 PERROR("pthread_create load_session_thread");
6081 goto exit_load_session
;
6085 * This is where we start awaiting program completion (e.g. through
6086 * signal that asks threads to teardown).
6089 ret
= pthread_join(load_session_thread
, &status
);
6092 PERROR("pthread_join load_session_thread");
6097 if (is_root
&& !opt_no_kernel
) {
6098 ret
= pthread_join(kernel_thread
, &status
);
6101 PERROR("pthread_join");
6107 ret
= pthread_join(agent_reg_thread
, &status
);
6110 PERROR("pthread_join agent");
6115 ret
= pthread_join(apps_notify_thread
, &status
);
6118 PERROR("pthread_join apps notify");
6123 ret
= pthread_join(apps_thread
, &status
);
6126 PERROR("pthread_join apps");
6131 ret
= pthread_join(reg_apps_thread
, &status
);
6134 PERROR("pthread_join");
6140 * Join dispatch thread after joining reg_apps_thread to ensure
6141 * we don't leak applications in the queue.
6143 ret
= pthread_join(dispatch_thread
, &status
);
6146 PERROR("pthread_join");
6151 ret
= pthread_join(client_thread
, &status
);
6154 PERROR("pthread_join");
6159 ret
= pthread_join(health_thread
, &status
);
6162 PERROR("pthread_join health thread");
6169 * sessiond_cleanup() is called when no other thread is running, except
6170 * the ht_cleanup thread, which is needed to destroy the hash tables.
6172 rcu_thread_online();
6174 rcu_thread_offline();
6175 rcu_unregister_thread();
6177 ret
= notify_thread_pipe(ht_cleanup_quit_pipe
[1]);
6179 ERR("write error on ht_cleanup quit pipe");
6183 ret
= pthread_join(ht_cleanup_thread
, &status
);
6186 PERROR("pthread_join ht cleanup thread");
6192 utils_close_pipe(ht_cleanup_pipe
);
6193 exit_ht_cleanup_pipe
:
6196 * Close the ht_cleanup quit pipe.
6198 utils_close_pipe(ht_cleanup_quit_pipe
);
6199 exit_ht_cleanup_quit_pipe
:
6201 health_app_destroy(health_sessiond
);
6202 exit_health_sessiond_cleanup
:
6203 exit_create_run_as_worker_cleanup
:
6206 /* Ensure all prior call_rcu are done. */
6209 sessiond_cleanup_options();
6211 exit_set_signal_handler
: