2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <urcu/uatomic.h>
42 #include <common/common.h>
43 #include <common/compat/socket.h>
44 #include <common/compat/getenv.h>
45 #include <common/defaults.h>
46 #include <common/kernel-consumer/kernel-consumer.h>
47 #include <common/futex.h>
48 #include <common/relayd/relayd.h>
49 #include <common/utils.h>
50 #include <common/daemonize.h>
51 #include <common/config/session-config.h>
53 #include "lttng-sessiond.h"
54 #include "buffer-registry.h"
61 #include "kernel-consumer.h"
65 #include "ust-consumer.h"
68 #include "health-sessiond.h"
69 #include "testpoint.h"
70 #include "ust-thread.h"
71 #include "agent-thread.h"
73 #include "load-session-thread.h"
76 #include "ht-cleanup.h"
78 #define CONSUMERD_FILE "lttng-consumerd"
81 static const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
82 static int tracing_group_name_override
;
83 static char *opt_pidfile
;
84 static int opt_sig_parent
;
85 static int opt_verbose_consumer
;
86 static int opt_daemon
, opt_background
;
87 static int opt_no_kernel
;
88 static char *opt_load_session_path
;
89 static pid_t ppid
; /* Parent PID for --sig-parent option */
90 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
92 static int lockfile_fd
= -1;
94 /* Set to 1 when a SIGUSR1 signal is received. */
95 static int recv_child_signal
;
98 * Consumer daemon specific control data. Every value not initialized here is
99 * set to 0 by the static definition.
101 static struct consumer_data kconsumer_data
= {
102 .type
= LTTNG_CONSUMER_KERNEL
,
103 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
104 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
107 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
108 .lock
= PTHREAD_MUTEX_INITIALIZER
,
109 .cond
= PTHREAD_COND_INITIALIZER
,
110 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
112 static struct consumer_data ustconsumer64_data
= {
113 .type
= LTTNG_CONSUMER64_UST
,
114 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
115 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
118 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
119 .lock
= PTHREAD_MUTEX_INITIALIZER
,
120 .cond
= PTHREAD_COND_INITIALIZER
,
121 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
123 static struct consumer_data ustconsumer32_data
= {
124 .type
= LTTNG_CONSUMER32_UST
,
125 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
126 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
129 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
130 .lock
= PTHREAD_MUTEX_INITIALIZER
,
131 .cond
= PTHREAD_COND_INITIALIZER
,
132 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
135 /* Command line options */
136 static const struct option long_options
[] = {
137 { "client-sock", required_argument
, 0, 'c' },
138 { "apps-sock", required_argument
, 0, 'a' },
139 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
140 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
141 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
142 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
143 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
144 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
145 { "consumerd32-path", required_argument
, 0, '\0' },
146 { "consumerd32-libdir", required_argument
, 0, '\0' },
147 { "consumerd64-path", required_argument
, 0, '\0' },
148 { "consumerd64-libdir", required_argument
, 0, '\0' },
149 { "daemonize", no_argument
, 0, 'd' },
150 { "background", no_argument
, 0, 'b' },
151 { "sig-parent", no_argument
, 0, 'S' },
152 { "help", no_argument
, 0, 'h' },
153 { "group", required_argument
, 0, 'g' },
154 { "version", no_argument
, 0, 'V' },
155 { "quiet", no_argument
, 0, 'q' },
156 { "verbose", no_argument
, 0, 'v' },
157 { "verbose-consumer", no_argument
, 0, '\0' },
158 { "no-kernel", no_argument
, 0, '\0' },
159 { "pidfile", required_argument
, 0, 'p' },
160 { "agent-tcp-port", required_argument
, 0, '\0' },
161 { "config", required_argument
, 0, 'f' },
162 { "load", required_argument
, 0, 'l' },
163 { "kmod-probes", required_argument
, 0, '\0' },
164 { "extra-kmod-probes", required_argument
, 0, '\0' },
168 /* Command line options to ignore from configuration file */
169 static const char *config_ignore_options
[] = { "help", "version", "config" };
171 /* Shared between threads */
172 static int dispatch_thread_exit
;
174 /* Global application Unix socket path */
175 static char apps_unix_sock_path
[PATH_MAX
];
176 /* Global client Unix socket path */
177 static char client_unix_sock_path
[PATH_MAX
];
178 /* global wait shm path for UST */
179 static char wait_shm_path
[PATH_MAX
];
180 /* Global health check unix path */
181 static char health_unix_sock_path
[PATH_MAX
];
183 /* Sockets and FDs */
184 static int client_sock
= -1;
185 static int apps_sock
= -1;
186 int kernel_tracer_fd
= -1;
187 static int kernel_poll_pipe
[2] = { -1, -1 };
190 * Quit pipe for all threads. This permits a single cancellation point
191 * for all threads when receiving an event on the pipe.
193 static int thread_quit_pipe
[2] = { -1, -1 };
196 * This pipe is used to inform the thread managing application communication
197 * that a command is queued and ready to be processed.
199 static int apps_cmd_pipe
[2] = { -1, -1 };
201 int apps_cmd_notify_pipe
[2] = { -1, -1 };
203 /* Pthread, Mutexes and Semaphores */
204 static pthread_t apps_thread
;
205 static pthread_t apps_notify_thread
;
206 static pthread_t reg_apps_thread
;
207 static pthread_t client_thread
;
208 static pthread_t kernel_thread
;
209 static pthread_t dispatch_thread
;
210 static pthread_t health_thread
;
211 static pthread_t ht_cleanup_thread
;
212 static pthread_t agent_reg_thread
;
213 static pthread_t load_session_thread
;
216 * UST registration command queue. This queue is tied with a futex and uses a N
217 * wakers / 1 waiter implemented and detailed in futex.c/.h
219 * The thread_registration_apps and thread_dispatch_ust_registration uses this
220 * queue along with the wait/wake scheme. The thread_manage_apps receives down
221 * the line new application socket and monitors it for any I/O error or clean
222 * close that triggers an unregistration of the application.
224 static struct ust_cmd_queue ust_cmd_queue
;
227 * Pointer initialized before thread creation.
229 * This points to the tracing session list containing the session count and a
230 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
231 * MUST NOT be taken if you call a public function in session.c.
233 * The lock is nested inside the structure: session_list_ptr->lock. Please use
234 * session_lock_list and session_unlock_list for lock acquisition.
236 static struct ltt_session_list
*session_list_ptr
;
238 int ust_consumerd64_fd
= -1;
239 int ust_consumerd32_fd
= -1;
241 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
242 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
243 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
244 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
245 static int consumerd32_bin_override
;
246 static int consumerd64_bin_override
;
247 static int consumerd32_libdir_override
;
248 static int consumerd64_libdir_override
;
250 static const char *module_proc_lttng
= "/proc/lttng";
253 * Consumer daemon state which is changed when spawning it, killing it or in
254 * case of a fatal error.
256 enum consumerd_state
{
257 CONSUMER_STARTED
= 1,
258 CONSUMER_STOPPED
= 2,
263 * This consumer daemon state is used to validate if a client command will be
264 * able to reach the consumer. If not, the client is informed. For instance,
265 * doing a "lttng start" when the consumer state is set to ERROR will return an
266 * error to the client.
268 * The following example shows a possible race condition of this scheme:
270 * consumer thread error happens
272 * client cmd checks state -> still OK
273 * consumer thread exit, sets error
274 * client cmd try to talk to consumer
277 * However, since the consumer is a different daemon, we have no way of making
278 * sure the command will reach it safely even with this state flag. This is why
279 * we consider that up to the state validation during command processing, the
280 * command is safe. After that, we can not guarantee the correctness of the
281 * client request vis-a-vis the consumer.
283 static enum consumerd_state ust_consumerd_state
;
284 static enum consumerd_state kernel_consumerd_state
;
287 * Socket timeout for receiving and sending in seconds.
289 static int app_socket_timeout
;
291 /* Set in main() with the current page size. */
294 /* Application health monitoring */
295 struct health_app
*health_sessiond
;
297 /* Agent TCP port for registration. Used by the agent thread. */
298 unsigned int agent_tcp_port
= DEFAULT_AGENT_TCP_PORT
;
300 /* Am I root or not. */
301 int is_root
; /* Set to 1 if the daemon is running as root */
303 const char * const config_section_name
= "sessiond";
305 /* Load session thread information to operate. */
306 struct load_session_thread_data
*load_info
;
308 /* Global hash tables */
309 struct lttng_ht
*agent_apps_ht_by_sock
= NULL
;
312 * Whether sessiond is ready for commands/health check requests.
313 * NR_LTTNG_SESSIOND_READY must match the number of calls to
314 * sessiond_notify_ready().
316 #define NR_LTTNG_SESSIOND_READY 3
317 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_READY
;
319 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
321 return (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) ? 1 : 0;
324 /* Notify parents that we are ready for cmd and health check */
326 void sessiond_notify_ready(void)
328 if (uatomic_sub_return(<tng_sessiond_ready
, 1) == 0) {
330 * Notify parent pid that we are ready to accept command
331 * for client side. This ppid is the one from the
332 * external process that spawned us.
334 if (opt_sig_parent
) {
339 * Notify the parent of the fork() process that we are
342 if (opt_daemon
|| opt_background
) {
343 kill(child_ppid
, SIGUSR1
);
349 void setup_consumerd_path(void)
351 const char *bin
, *libdir
;
354 * Allow INSTALL_BIN_PATH to be used as a target path for the
355 * native architecture size consumer if CONFIG_CONSUMER*_PATH
356 * has not been defined.
358 #if (CAA_BITS_PER_LONG == 32)
359 if (!consumerd32_bin
[0]) {
360 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
362 if (!consumerd32_libdir
[0]) {
363 consumerd32_libdir
= INSTALL_LIB_PATH
;
365 #elif (CAA_BITS_PER_LONG == 64)
366 if (!consumerd64_bin
[0]) {
367 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
369 if (!consumerd64_libdir
[0]) {
370 consumerd64_libdir
= INSTALL_LIB_PATH
;
373 #error "Unknown bitness"
377 * runtime env. var. overrides the build default.
379 bin
= lttng_secure_getenv("LTTNG_CONSUMERD32_BIN");
381 consumerd32_bin
= bin
;
383 bin
= lttng_secure_getenv("LTTNG_CONSUMERD64_BIN");
385 consumerd64_bin
= bin
;
387 libdir
= lttng_secure_getenv("LTTNG_CONSUMERD32_LIBDIR");
389 consumerd32_libdir
= libdir
;
391 libdir
= lttng_secure_getenv("LTTNG_CONSUMERD64_LIBDIR");
393 consumerd64_libdir
= libdir
;
398 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
405 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
411 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
423 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
425 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
427 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);
431 * Init thread quit pipe.
433 * Return -1 on error or 0 if all pipes are created.
435 static int __init_thread_quit_pipe(int *a_pipe
)
441 PERROR("thread quit pipe");
445 for (i
= 0; i
< 2; i
++) {
446 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
457 static int init_thread_quit_pipe(void)
459 return __init_thread_quit_pipe(thread_quit_pipe
);
463 * Stop all threads by closing the thread quit pipe.
465 static void stop_threads(void)
469 /* Stopping all threads */
470 DBG("Terminating all threads");
471 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
473 ERR("write error on thread quit pipe");
476 /* Dispatch thread */
477 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
478 futex_nto1_wake(&ust_cmd_queue
.futex
);
482 * Close every consumer sockets.
484 static void close_consumer_sockets(void)
488 if (kconsumer_data
.err_sock
>= 0) {
489 ret
= close(kconsumer_data
.err_sock
);
491 PERROR("kernel consumer err_sock close");
494 if (ustconsumer32_data
.err_sock
>= 0) {
495 ret
= close(ustconsumer32_data
.err_sock
);
497 PERROR("UST consumerd32 err_sock close");
500 if (ustconsumer64_data
.err_sock
>= 0) {
501 ret
= close(ustconsumer64_data
.err_sock
);
503 PERROR("UST consumerd64 err_sock close");
506 if (kconsumer_data
.cmd_sock
>= 0) {
507 ret
= close(kconsumer_data
.cmd_sock
);
509 PERROR("kernel consumer cmd_sock close");
512 if (ustconsumer32_data
.cmd_sock
>= 0) {
513 ret
= close(ustconsumer32_data
.cmd_sock
);
515 PERROR("UST consumerd32 cmd_sock close");
518 if (ustconsumer64_data
.cmd_sock
>= 0) {
519 ret
= close(ustconsumer64_data
.cmd_sock
);
521 PERROR("UST consumerd64 cmd_sock close");
527 * Generate the full lock file path using the rundir.
529 * Return the snprintf() return value thus a negative value is an error.
531 static int generate_lock_file_path(char *path
, size_t len
)
538 /* Build lockfile path from rundir. */
539 ret
= snprintf(path
, len
, "%s/" DEFAULT_LTTNG_SESSIOND_LOCKFILE
, rundir
);
541 PERROR("snprintf lockfile path");
548 * Wait on consumer process termination.
550 * Need to be called with the consumer data lock held or from a context
551 * ensuring no concurrent access to data (e.g: cleanup).
553 static void wait_consumer(struct consumer_data
*consumer_data
)
558 if (consumer_data
->pid
<= 0) {
562 DBG("Waiting for complete teardown of consumerd (PID: %d)",
564 ret
= waitpid(consumer_data
->pid
, &status
, 0);
566 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
567 } else if (!WIFEXITED(status
)) {
568 ERR("consumerd termination with error: %d",
571 consumer_data
->pid
= 0;
575 * Cleanup the session daemon's data structures.
577 static void sessiond_cleanup(void)
580 struct ltt_session
*sess
, *stmp
;
583 DBG("Cleanup sessiond");
586 * Close the thread quit pipe. It has already done its job,
587 * since we are now called.
589 utils_close_pipe(thread_quit_pipe
);
592 * If opt_pidfile is undefined, the default file will be wiped when
593 * removing the rundir.
596 ret
= remove(opt_pidfile
);
598 PERROR("remove pidfile %s", opt_pidfile
);
602 DBG("Removing sessiond and consumerd content of directory %s", rundir
);
605 snprintf(path
, PATH_MAX
,
607 rundir
, DEFAULT_LTTNG_SESSIOND_PIDFILE
);
608 DBG("Removing %s", path
);
611 snprintf(path
, PATH_MAX
, "%s/%s", rundir
,
612 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
);
613 DBG("Removing %s", path
);
617 snprintf(path
, PATH_MAX
,
618 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
620 DBG("Removing %s", path
);
623 snprintf(path
, PATH_MAX
,
624 DEFAULT_KCONSUMERD_PATH
,
626 DBG("Removing directory %s", path
);
629 /* ust consumerd 32 */
630 snprintf(path
, PATH_MAX
,
631 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
633 DBG("Removing %s", path
);
636 snprintf(path
, PATH_MAX
,
637 DEFAULT_USTCONSUMERD32_PATH
,
639 DBG("Removing directory %s", path
);
642 /* ust consumerd 64 */
643 snprintf(path
, PATH_MAX
,
644 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
646 DBG("Removing %s", path
);
649 snprintf(path
, PATH_MAX
,
650 DEFAULT_USTCONSUMERD64_PATH
,
652 DBG("Removing directory %s", path
);
655 DBG("Cleaning up all sessions");
657 /* Destroy session list mutex */
658 if (session_list_ptr
!= NULL
) {
659 pthread_mutex_destroy(&session_list_ptr
->lock
);
661 /* Cleanup ALL session */
662 cds_list_for_each_entry_safe(sess
, stmp
,
663 &session_list_ptr
->head
, list
) {
664 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
668 wait_consumer(&kconsumer_data
);
669 wait_consumer(&ustconsumer64_data
);
670 wait_consumer(&ustconsumer32_data
);
672 DBG("Cleaning up all agent apps");
673 agent_app_ht_clean();
675 DBG("Closing all UST sockets");
676 ust_app_clean_list();
677 buffer_reg_destroy_registries();
679 if (is_root
&& !opt_no_kernel
) {
680 DBG2("Closing kernel fd");
681 if (kernel_tracer_fd
>= 0) {
682 ret
= close(kernel_tracer_fd
);
687 DBG("Unloading kernel modules");
688 modprobe_remove_lttng_all();
692 close_consumer_sockets();
695 load_session_destroy_data(load_info
);
700 * Cleanup lock file by deleting it and finaly closing it which will
701 * release the file system lock.
703 if (lockfile_fd
>= 0) {
704 char lockfile_path
[PATH_MAX
];
706 ret
= generate_lock_file_path(lockfile_path
,
707 sizeof(lockfile_path
));
709 ret
= remove(lockfile_path
);
711 PERROR("remove lock file");
713 ret
= close(lockfile_fd
);
715 PERROR("close lock file");
721 * We do NOT rmdir rundir because there are other processes
722 * using it, for instance lttng-relayd, which can start in
723 * parallel with this teardown.
730 * Cleanup the daemon's option data structures.
732 static void sessiond_cleanup_options(void)
734 DBG("Cleaning up options");
737 * If the override option is set, the pointer points to a *non* const
738 * thus freeing it even though the variable type is set to const.
740 if (tracing_group_name_override
) {
741 free((void *) tracing_group_name
);
743 if (consumerd32_bin_override
) {
744 free((void *) consumerd32_bin
);
746 if (consumerd64_bin_override
) {
747 free((void *) consumerd64_bin
);
749 if (consumerd32_libdir_override
) {
750 free((void *) consumerd32_libdir
);
752 if (consumerd64_libdir_override
) {
753 free((void *) consumerd64_libdir
);
757 free(opt_load_session_path
);
758 free(kmod_probes_list
);
759 free(kmod_extra_probes_list
);
761 run_as_destroy_worker();
765 * Send data on a unix socket using the liblttsessiondcomm API.
767 * Return lttcomm error code.
769 static int send_unix_sock(int sock
, void *buf
, size_t len
)
771 /* Check valid length */
776 return lttcomm_send_unix_sock(sock
, buf
, len
);
780 * Free memory of a command context structure.
782 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
784 DBG("Clean command context structure");
786 if ((*cmd_ctx
)->llm
) {
787 free((*cmd_ctx
)->llm
);
789 if ((*cmd_ctx
)->lsm
) {
790 free((*cmd_ctx
)->lsm
);
798 * Notify UST applications using the shm mmap futex.
800 static int notify_ust_apps(int active
)
804 DBG("Notifying applications of session daemon state: %d", active
);
806 /* See shm.c for this call implying mmap, shm and futex calls */
807 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
808 if (wait_shm_mmap
== NULL
) {
812 /* Wake waiting process */
813 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
815 /* Apps notified successfully */
823 * Setup the outgoing data buffer for the response (llm) by allocating the
824 * right amount of memory and copying the original information from the lsm
827 * Return 0 on success, negative value on error.
829 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
830 const void *payload_buf
, size_t payload_len
,
831 const void *cmd_header_buf
, size_t cmd_header_len
)
834 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
835 const size_t cmd_header_offset
= header_len
;
836 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
837 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
839 cmd_ctx
->llm
= zmalloc(total_msg_size
);
841 if (cmd_ctx
->llm
== NULL
) {
847 /* Copy common data */
848 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
849 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
850 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
851 cmd_ctx
->llm
->data_size
= payload_len
;
852 cmd_ctx
->lttng_msg_size
= total_msg_size
;
854 /* Copy command header */
855 if (cmd_header_len
) {
856 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
862 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
871 * Version of setup_lttng_msg() without command header.
873 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
874 void *payload_buf
, size_t payload_len
)
876 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
879 * Update the kernel poll set of all channel fd available over all tracing
880 * session. Add the wakeup pipe at the end of the set.
882 static int update_kernel_poll(struct lttng_poll_event
*events
)
885 struct ltt_session
*session
;
886 struct ltt_kernel_channel
*channel
;
888 DBG("Updating kernel poll set");
891 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
892 session_lock(session
);
893 if (session
->kernel_session
== NULL
) {
894 session_unlock(session
);
898 cds_list_for_each_entry(channel
,
899 &session
->kernel_session
->channel_list
.head
, list
) {
900 /* Add channel fd to the kernel poll set */
901 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
903 session_unlock(session
);
906 DBG("Channel fd %d added to kernel set", channel
->fd
);
908 session_unlock(session
);
910 session_unlock_list();
915 session_unlock_list();
920 * Find the channel fd from 'fd' over all tracing session. When found, check
921 * for new channel stream and send those stream fds to the kernel consumer.
923 * Useful for CPU hotplug feature.
925 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
928 struct ltt_session
*session
;
929 struct ltt_kernel_session
*ksess
;
930 struct ltt_kernel_channel
*channel
;
932 DBG("Updating kernel streams for channel fd %d", fd
);
935 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
936 session_lock(session
);
937 if (session
->kernel_session
== NULL
) {
938 session_unlock(session
);
941 ksess
= session
->kernel_session
;
943 cds_list_for_each_entry(channel
,
944 &ksess
->channel_list
.head
, list
) {
945 struct lttng_ht_iter iter
;
946 struct consumer_socket
*socket
;
948 if (channel
->fd
!= fd
) {
951 DBG("Channel found, updating kernel streams");
952 ret
= kernel_open_channel_stream(channel
);
956 /* Update the stream global counter */
957 ksess
->stream_count_global
+= ret
;
960 * Have we already sent fds to the consumer? If yes, it
961 * means that tracing is started so it is safe to send
962 * our updated stream fds.
964 if (ksess
->consumer_fds_sent
!= 1
965 || ksess
->consumer
== NULL
) {
971 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
972 &iter
.iter
, socket
, node
.node
) {
973 pthread_mutex_lock(socket
->lock
);
974 ret
= kernel_consumer_send_channel_stream(socket
,
976 session
->output_traces
? 1 : 0);
977 pthread_mutex_unlock(socket
->lock
);
985 session_unlock(session
);
987 session_unlock_list();
991 session_unlock(session
);
992 session_unlock_list();
997 * For each tracing session, update newly registered apps. The session list
998 * lock MUST be acquired before calling this.
1000 static void update_ust_app(int app_sock
)
1002 struct ltt_session
*sess
, *stmp
;
1004 /* Consumer is in an ERROR state. Stop any application update. */
1005 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
1006 /* Stop the update process since the consumer is dead. */
1010 /* For all tracing session(s) */
1011 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
1012 struct ust_app
*app
;
1015 if (!sess
->ust_session
) {
1016 goto unlock_session
;
1020 assert(app_sock
>= 0);
1021 app
= ust_app_find_by_sock(app_sock
);
1024 * Application can be unregistered before so
1025 * this is possible hence simply stopping the
1028 DBG3("UST app update failed to find app sock %d",
1032 ust_app_global_update(sess
->ust_session
, app
);
1036 session_unlock(sess
);
1041 * This thread manage event coming from the kernel.
1043 * Features supported in this thread:
1046 static void *thread_manage_kernel(void *data
)
1048 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
1049 uint32_t revents
, nb_fd
;
1051 struct lttng_poll_event events
;
1053 DBG("[thread] Thread manage kernel started");
1055 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
1058 * This first step of the while is to clean this structure which could free
1059 * non NULL pointers so initialize it before the loop.
1061 lttng_poll_init(&events
);
1063 if (testpoint(sessiond_thread_manage_kernel
)) {
1064 goto error_testpoint
;
1067 health_code_update();
1069 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
1070 goto error_testpoint
;
1074 health_code_update();
1076 if (update_poll_flag
== 1) {
1077 /* Clean events object. We are about to populate it again. */
1078 lttng_poll_clean(&events
);
1080 ret
= sessiond_set_thread_pollset(&events
, 2);
1082 goto error_poll_create
;
1085 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
1090 /* This will add the available kernel channel if any. */
1091 ret
= update_kernel_poll(&events
);
1095 update_poll_flag
= 0;
1098 DBG("Thread kernel polling");
1100 /* Poll infinite value of time */
1102 health_poll_entry();
1103 ret
= lttng_poll_wait(&events
, -1);
1104 DBG("Thread kernel return from poll on %d fds",
1105 LTTNG_POLL_GETNB(&events
));
1109 * Restart interrupted system call.
1111 if (errno
== EINTR
) {
1115 } else if (ret
== 0) {
1116 /* Should not happen since timeout is infinite */
1117 ERR("Return value of poll is 0 with an infinite timeout.\n"
1118 "This should not have happened! Continuing...");
1124 for (i
= 0; i
< nb_fd
; i
++) {
1125 /* Fetch once the poll data */
1126 revents
= LTTNG_POLL_GETEV(&events
, i
);
1127 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1129 health_code_update();
1132 /* No activity for this FD (poll implementation). */
1136 /* Thread quit pipe has been closed. Killing thread. */
1137 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1143 /* Check for data on kernel pipe */
1144 if (revents
& LPOLLIN
) {
1145 if (pollfd
== kernel_poll_pipe
[0]) {
1146 (void) lttng_read(kernel_poll_pipe
[0],
1149 * Ret value is useless here, if this pipe gets any actions an
1150 * update is required anyway.
1152 update_poll_flag
= 1;
1156 * New CPU detected by the kernel. Adding kernel stream to
1157 * kernel session and updating the kernel consumer
1159 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1165 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1166 update_poll_flag
= 1;
1169 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1177 lttng_poll_clean(&events
);
1180 utils_close_pipe(kernel_poll_pipe
);
1181 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1184 ERR("Health error occurred in %s", __func__
);
1185 WARN("Kernel thread died unexpectedly. "
1186 "Kernel tracing can continue but CPU hotplug is disabled.");
1188 health_unregister(health_sessiond
);
1189 DBG("Kernel thread dying");
1194 * Signal pthread condition of the consumer data that the thread.
1196 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1198 pthread_mutex_lock(&data
->cond_mutex
);
1201 * The state is set before signaling. It can be any value, it's the waiter
1202 * job to correctly interpret this condition variable associated to the
1203 * consumer pthread_cond.
1205 * A value of 0 means that the corresponding thread of the consumer data
1206 * was not started. 1 indicates that the thread has started and is ready
1207 * for action. A negative value means that there was an error during the
1210 data
->consumer_thread_is_ready
= state
;
1211 (void) pthread_cond_signal(&data
->cond
);
1213 pthread_mutex_unlock(&data
->cond_mutex
);
1217 * This thread manage the consumer error sent back to the session daemon.
1219 static void *thread_manage_consumer(void *data
)
1221 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1222 uint32_t revents
, nb_fd
;
1223 enum lttcomm_return_code code
;
1224 struct lttng_poll_event events
;
1225 struct consumer_data
*consumer_data
= data
;
1227 DBG("[thread] Manage consumer started");
1229 rcu_register_thread();
1230 rcu_thread_online();
1232 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1234 health_code_update();
1237 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1238 * metadata_sock. Nothing more will be added to this poll set.
1240 ret
= sessiond_set_thread_pollset(&events
, 3);
1246 * The error socket here is already in a listening state which was done
1247 * just before spawning this thread to avoid a race between the consumer
1248 * daemon exec trying to connect and the listen() call.
1250 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1255 health_code_update();
1257 /* Infinite blocking call, waiting for transmission */
1259 health_poll_entry();
1261 if (testpoint(sessiond_thread_manage_consumer
)) {
1265 ret
= lttng_poll_wait(&events
, -1);
1269 * Restart interrupted system call.
1271 if (errno
== EINTR
) {
1279 for (i
= 0; i
< nb_fd
; i
++) {
1280 /* Fetch once the poll data */
1281 revents
= LTTNG_POLL_GETEV(&events
, i
);
1282 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1284 health_code_update();
1287 /* No activity for this FD (poll implementation). */
1291 /* Thread quit pipe has been closed. Killing thread. */
1292 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1298 /* Event on the registration socket */
1299 if (pollfd
== consumer_data
->err_sock
) {
1300 if (revents
& LPOLLIN
) {
1302 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1303 ERR("consumer err socket poll error");
1306 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1312 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1318 * Set the CLOEXEC flag. Return code is useless because either way, the
1321 (void) utils_set_fd_cloexec(sock
);
1323 health_code_update();
1325 DBG2("Receiving code from consumer err_sock");
1327 /* Getting status code from kconsumerd */
1328 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1329 sizeof(enum lttcomm_return_code
));
1334 health_code_update();
1335 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1336 /* Connect both socket, command and metadata. */
1337 consumer_data
->cmd_sock
=
1338 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1339 consumer_data
->metadata_fd
=
1340 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1341 if (consumer_data
->cmd_sock
< 0
1342 || consumer_data
->metadata_fd
< 0) {
1343 PERROR("consumer connect cmd socket");
1344 /* On error, signal condition and quit. */
1345 signal_consumer_condition(consumer_data
, -1);
1348 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1349 /* Create metadata socket lock. */
1350 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1351 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1352 PERROR("zmalloc pthread mutex");
1355 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1357 signal_consumer_condition(consumer_data
, 1);
1358 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1359 DBG("Consumer metadata socket ready (fd: %d)",
1360 consumer_data
->metadata_fd
);
1362 ERR("consumer error when waiting for SOCK_READY : %s",
1363 lttcomm_get_readable_code(-code
));
1367 /* Remove the consumerd error sock since we've established a connexion */
1368 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1373 /* Add new accepted error socket. */
1374 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1379 /* Add metadata socket that is successfully connected. */
1380 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1381 LPOLLIN
| LPOLLRDHUP
);
1386 health_code_update();
1388 /* Infinite blocking call, waiting for transmission */
1391 health_code_update();
1393 /* Exit the thread because the thread quit pipe has been triggered. */
1395 /* Not a health error. */
1400 health_poll_entry();
1401 ret
= lttng_poll_wait(&events
, -1);
1405 * Restart interrupted system call.
1407 if (errno
== EINTR
) {
1415 for (i
= 0; i
< nb_fd
; i
++) {
1416 /* Fetch once the poll data */
1417 revents
= LTTNG_POLL_GETEV(&events
, i
);
1418 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1420 health_code_update();
1423 /* No activity for this FD (poll implementation). */
1428 * Thread quit pipe has been triggered, flag that we should stop
1429 * but continue the current loop to handle potential data from
1432 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1434 if (pollfd
== sock
) {
1435 /* Event on the consumerd socket */
1436 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1437 && !(revents
& LPOLLIN
)) {
1438 ERR("consumer err socket second poll error");
1441 health_code_update();
1442 /* Wait for any kconsumerd error */
1443 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1444 sizeof(enum lttcomm_return_code
));
1446 ERR("consumer closed the command socket");
1450 ERR("consumer return code : %s",
1451 lttcomm_get_readable_code(-code
));
1454 } else if (pollfd
== consumer_data
->metadata_fd
) {
1455 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1456 && !(revents
& LPOLLIN
)) {
1457 ERR("consumer err metadata socket second poll error");
1460 /* UST metadata requests */
1461 ret
= ust_consumer_metadata_request(
1462 &consumer_data
->metadata_sock
);
1464 ERR("Handling metadata request");
1468 /* No need for an else branch all FDs are tested prior. */
1470 health_code_update();
1476 * We lock here because we are about to close the sockets and some other
1477 * thread might be using them so get exclusive access which will abort all
1478 * other consumer command by other threads.
1480 pthread_mutex_lock(&consumer_data
->lock
);
1482 /* Immediately set the consumerd state to stopped */
1483 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1484 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1485 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1486 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1487 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1489 /* Code flow error... */
1493 if (consumer_data
->err_sock
>= 0) {
1494 ret
= close(consumer_data
->err_sock
);
1498 consumer_data
->err_sock
= -1;
1500 if (consumer_data
->cmd_sock
>= 0) {
1501 ret
= close(consumer_data
->cmd_sock
);
1505 consumer_data
->cmd_sock
= -1;
1507 if (consumer_data
->metadata_sock
.fd_ptr
&&
1508 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1509 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1521 unlink(consumer_data
->err_unix_sock_path
);
1522 unlink(consumer_data
->cmd_unix_sock_path
);
1523 pthread_mutex_unlock(&consumer_data
->lock
);
1525 /* Cleanup metadata socket mutex. */
1526 if (consumer_data
->metadata_sock
.lock
) {
1527 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1528 free(consumer_data
->metadata_sock
.lock
);
1530 lttng_poll_clean(&events
);
1534 ERR("Health error occurred in %s", __func__
);
1536 health_unregister(health_sessiond
);
1537 DBG("consumer thread cleanup completed");
1539 rcu_thread_offline();
1540 rcu_unregister_thread();
1546 * This thread manage application communication.
1548 static void *thread_manage_apps(void *data
)
1550 int i
, ret
, pollfd
, err
= -1;
1552 uint32_t revents
, nb_fd
;
1553 struct lttng_poll_event events
;
1555 DBG("[thread] Manage application started");
1557 rcu_register_thread();
1558 rcu_thread_online();
1560 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1562 if (testpoint(sessiond_thread_manage_apps
)) {
1563 goto error_testpoint
;
1566 health_code_update();
1568 ret
= sessiond_set_thread_pollset(&events
, 2);
1570 goto error_poll_create
;
1573 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1578 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1582 health_code_update();
1585 DBG("Apps thread polling");
1587 /* Inifinite blocking call, waiting for transmission */
1589 health_poll_entry();
1590 ret
= lttng_poll_wait(&events
, -1);
1591 DBG("Apps thread return from poll on %d fds",
1592 LTTNG_POLL_GETNB(&events
));
1596 * Restart interrupted system call.
1598 if (errno
== EINTR
) {
1606 for (i
= 0; i
< nb_fd
; i
++) {
1607 /* Fetch once the poll data */
1608 revents
= LTTNG_POLL_GETEV(&events
, i
);
1609 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1611 health_code_update();
1614 /* No activity for this FD (poll implementation). */
1618 /* Thread quit pipe has been closed. Killing thread. */
1619 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1625 /* Inspect the apps cmd pipe */
1626 if (pollfd
== apps_cmd_pipe
[0]) {
1627 if (revents
& LPOLLIN
) {
1631 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1632 if (size_ret
< sizeof(sock
)) {
1633 PERROR("read apps cmd pipe");
1637 health_code_update();
1640 * Since this is a command socket (write then read),
1641 * we only monitor the error events of the socket.
1643 ret
= lttng_poll_add(&events
, sock
,
1644 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1649 DBG("Apps with sock %d added to poll set", sock
);
1650 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1651 ERR("Apps command pipe error");
1654 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1659 * At this point, we know that a registered application made
1660 * the event at poll_wait.
1662 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1663 /* Removing from the poll set */
1664 ret
= lttng_poll_del(&events
, pollfd
);
1669 /* Socket closed on remote end. */
1670 ust_app_unregister(pollfd
);
1672 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1677 health_code_update();
1683 lttng_poll_clean(&events
);
1686 utils_close_pipe(apps_cmd_pipe
);
1687 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1690 * We don't clean the UST app hash table here since already registered
1691 * applications can still be controlled so let them be until the session
1692 * daemon dies or the applications stop.
1697 ERR("Health error occurred in %s", __func__
);
1699 health_unregister(health_sessiond
);
1700 DBG("Application communication apps thread cleanup complete");
1701 rcu_thread_offline();
1702 rcu_unregister_thread();
1707 * Send a socket to a thread This is called from the dispatch UST registration
1708 * thread once all sockets are set for the application.
1710 * The sock value can be invalid, we don't really care, the thread will handle
1711 * it and make the necessary cleanup if so.
1713 * On success, return 0 else a negative value being the errno message of the
1716 static int send_socket_to_thread(int fd
, int sock
)
1721 * It's possible that the FD is set as invalid with -1 concurrently just
1722 * before calling this function being a shutdown state of the thread.
1729 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1730 if (ret
< sizeof(sock
)) {
1731 PERROR("write apps pipe %d", fd
);
1738 /* All good. Don't send back the write positive ret value. */
1745 * Sanitize the wait queue of the dispatch registration thread meaning removing
1746 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1747 * notify socket is never received.
1749 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1751 int ret
, nb_fd
= 0, i
;
1752 unsigned int fd_added
= 0;
1753 struct lttng_poll_event events
;
1754 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1758 lttng_poll_init(&events
);
1760 /* Just skip everything for an empty queue. */
1761 if (!wait_queue
->count
) {
1765 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1770 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1771 &wait_queue
->head
, head
) {
1772 assert(wait_node
->app
);
1773 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1774 LPOLLHUP
| LPOLLERR
);
1787 * Poll but don't block so we can quickly identify the faulty events and
1788 * clean them afterwards from the wait queue.
1790 ret
= lttng_poll_wait(&events
, 0);
1796 for (i
= 0; i
< nb_fd
; i
++) {
1797 /* Get faulty FD. */
1798 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1799 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1802 /* No activity for this FD (poll implementation). */
1806 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1807 &wait_queue
->head
, head
) {
1808 if (pollfd
== wait_node
->app
->sock
&&
1809 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1810 cds_list_del(&wait_node
->head
);
1811 wait_queue
->count
--;
1812 ust_app_destroy(wait_node
->app
);
1815 * Silence warning of use-after-free in
1816 * cds_list_for_each_entry_safe which uses
1817 * __typeof__(*wait_node).
1822 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1829 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1833 lttng_poll_clean(&events
);
1837 lttng_poll_clean(&events
);
1839 ERR("Unable to sanitize wait queue");
1844 * Dispatch request from the registration threads to the application
1845 * communication thread.
1847 static void *thread_dispatch_ust_registration(void *data
)
1850 struct cds_wfcq_node
*node
;
1851 struct ust_command
*ust_cmd
= NULL
;
1852 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1853 struct ust_reg_wait_queue wait_queue
= {
1857 rcu_register_thread();
1859 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1861 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1862 goto error_testpoint
;
1865 health_code_update();
1867 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1869 DBG("[thread] Dispatch UST command started");
1872 health_code_update();
1874 /* Atomically prepare the queue futex */
1875 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1877 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1882 struct ust_app
*app
= NULL
;
1886 * Make sure we don't have node(s) that have hung up before receiving
1887 * the notify socket. This is to clean the list in order to avoid
1888 * memory leaks from notify socket that are never seen.
1890 sanitize_wait_queue(&wait_queue
);
1892 health_code_update();
1893 /* Dequeue command for registration */
1894 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1896 DBG("Woken up but nothing in the UST command queue");
1897 /* Continue thread execution */
1901 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1903 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1904 " gid:%d sock:%d name:%s (version %d.%d)",
1905 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1906 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1907 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1908 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1910 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1911 wait_node
= zmalloc(sizeof(*wait_node
));
1913 PERROR("zmalloc wait_node dispatch");
1914 ret
= close(ust_cmd
->sock
);
1916 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1918 lttng_fd_put(LTTNG_FD_APPS
, 1);
1922 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1924 /* Create application object if socket is CMD. */
1925 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1927 if (!wait_node
->app
) {
1928 ret
= close(ust_cmd
->sock
);
1930 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1932 lttng_fd_put(LTTNG_FD_APPS
, 1);
1938 * Add application to the wait queue so we can set the notify
1939 * socket before putting this object in the global ht.
1941 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1946 * We have to continue here since we don't have the notify
1947 * socket and the application MUST be added to the hash table
1948 * only at that moment.
1953 * Look for the application in the local wait queue and set the
1954 * notify socket if found.
1956 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1957 &wait_queue
.head
, head
) {
1958 health_code_update();
1959 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1960 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1961 cds_list_del(&wait_node
->head
);
1963 app
= wait_node
->app
;
1965 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1971 * With no application at this stage the received socket is
1972 * basically useless so close it before we free the cmd data
1973 * structure for good.
1976 ret
= close(ust_cmd
->sock
);
1978 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1980 lttng_fd_put(LTTNG_FD_APPS
, 1);
1987 * @session_lock_list
1989 * Lock the global session list so from the register up to the
1990 * registration done message, no thread can see the application
1991 * and change its state.
1993 session_lock_list();
1997 * Add application to the global hash table. This needs to be
1998 * done before the update to the UST registry can locate the
2003 /* Set app version. This call will print an error if needed. */
2004 (void) ust_app_version(app
);
2006 /* Send notify socket through the notify pipe. */
2007 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
2011 session_unlock_list();
2013 * No notify thread, stop the UST tracing. However, this is
2014 * not an internal error of the this thread thus setting
2015 * the health error code to a normal exit.
2022 * Update newly registered application with the tracing
2023 * registry info already enabled information.
2025 update_ust_app(app
->sock
);
2028 * Don't care about return value. Let the manage apps threads
2029 * handle app unregistration upon socket close.
2031 (void) ust_app_register_done(app
);
2034 * Even if the application socket has been closed, send the app
2035 * to the thread and unregistration will take place at that
2038 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
2041 session_unlock_list();
2043 * No apps. thread, stop the UST tracing. However, this is
2044 * not an internal error of the this thread thus setting
2045 * the health error code to a normal exit.
2052 session_unlock_list();
2054 } while (node
!= NULL
);
2056 health_poll_entry();
2057 /* Futex wait on queue. Blocking call on futex() */
2058 futex_nto1_wait(&ust_cmd_queue
.futex
);
2061 /* Normal exit, no error */
2065 /* Clean up wait queue. */
2066 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2067 &wait_queue
.head
, head
) {
2068 cds_list_del(&wait_node
->head
);
2073 /* Empty command queue. */
2075 /* Dequeue command for registration */
2076 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2080 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2081 ret
= close(ust_cmd
->sock
);
2083 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2085 lttng_fd_put(LTTNG_FD_APPS
, 1);
2090 DBG("Dispatch thread dying");
2093 ERR("Health error occurred in %s", __func__
);
2095 health_unregister(health_sessiond
);
2096 rcu_unregister_thread();
2101 * This thread manage application registration.
2103 static void *thread_registration_apps(void *data
)
2105 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2106 uint32_t revents
, nb_fd
;
2107 struct lttng_poll_event events
;
2109 * Get allocated in this thread, enqueued to a global queue, dequeued and
2110 * freed in the manage apps thread.
2112 struct ust_command
*ust_cmd
= NULL
;
2114 DBG("[thread] Manage application registration started");
2116 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2118 if (testpoint(sessiond_thread_registration_apps
)) {
2119 goto error_testpoint
;
2122 ret
= lttcomm_listen_unix_sock(apps_sock
);
2128 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2129 * more will be added to this poll set.
2131 ret
= sessiond_set_thread_pollset(&events
, 2);
2133 goto error_create_poll
;
2136 /* Add the application registration socket */
2137 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2139 goto error_poll_add
;
2142 /* Notify all applications to register */
2143 ret
= notify_ust_apps(1);
2145 ERR("Failed to notify applications or create the wait shared memory.\n"
2146 "Execution continues but there might be problem for already\n"
2147 "running applications that wishes to register.");
2151 DBG("Accepting application registration");
2153 /* Inifinite blocking call, waiting for transmission */
2155 health_poll_entry();
2156 ret
= lttng_poll_wait(&events
, -1);
2160 * Restart interrupted system call.
2162 if (errno
== EINTR
) {
2170 for (i
= 0; i
< nb_fd
; i
++) {
2171 health_code_update();
2173 /* Fetch once the poll data */
2174 revents
= LTTNG_POLL_GETEV(&events
, i
);
2175 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2178 /* No activity for this FD (poll implementation). */
2182 /* Thread quit pipe has been closed. Killing thread. */
2183 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2189 /* Event on the registration socket */
2190 if (pollfd
== apps_sock
) {
2191 if (revents
& LPOLLIN
) {
2192 sock
= lttcomm_accept_unix_sock(apps_sock
);
2198 * Set socket timeout for both receiving and ending.
2199 * app_socket_timeout is in seconds, whereas
2200 * lttcomm_setsockopt_rcv_timeout and
2201 * lttcomm_setsockopt_snd_timeout expect msec as
2204 if (app_socket_timeout
>= 0) {
2205 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2206 app_socket_timeout
* 1000);
2207 (void) lttcomm_setsockopt_snd_timeout(sock
,
2208 app_socket_timeout
* 1000);
2212 * Set the CLOEXEC flag. Return code is useless because
2213 * either way, the show must go on.
2215 (void) utils_set_fd_cloexec(sock
);
2217 /* Create UST registration command for enqueuing */
2218 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2219 if (ust_cmd
== NULL
) {
2220 PERROR("ust command zmalloc");
2229 * Using message-based transmissions to ensure we don't
2230 * have to deal with partially received messages.
2232 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2234 ERR("Exhausted file descriptors allowed for applications.");
2244 health_code_update();
2245 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2248 /* Close socket of the application. */
2253 lttng_fd_put(LTTNG_FD_APPS
, 1);
2257 health_code_update();
2259 ust_cmd
->sock
= sock
;
2262 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2263 " gid:%d sock:%d name:%s (version %d.%d)",
2264 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2265 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2266 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2267 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2270 * Lock free enqueue the registration request. The red pill
2271 * has been taken! This apps will be part of the *system*.
2273 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2276 * Wake the registration queue futex. Implicit memory
2277 * barrier with the exchange in cds_wfcq_enqueue.
2279 futex_nto1_wake(&ust_cmd_queue
.futex
);
2280 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2281 ERR("Register apps socket poll error");
2284 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2293 /* Notify that the registration thread is gone */
2296 if (apps_sock
>= 0) {
2297 ret
= close(apps_sock
);
2307 lttng_fd_put(LTTNG_FD_APPS
, 1);
2309 unlink(apps_unix_sock_path
);
2312 lttng_poll_clean(&events
);
2316 DBG("UST Registration thread cleanup complete");
2319 ERR("Health error occurred in %s", __func__
);
2321 health_unregister(health_sessiond
);
2327 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2328 * exec or it will fails.
2330 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2333 struct timespec timeout
;
2336 * Make sure we set the readiness flag to 0 because we are NOT ready.
2337 * This access to consumer_thread_is_ready does not need to be
2338 * protected by consumer_data.cond_mutex (yet) since the consumer
2339 * management thread has not been started at this point.
2341 consumer_data
->consumer_thread_is_ready
= 0;
2343 /* Setup pthread condition */
2344 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2347 PERROR("pthread_condattr_init consumer data");
2352 * Set the monotonic clock in order to make sure we DO NOT jump in time
2353 * between the clock_gettime() call and the timedwait call. See bug #324
2354 * for a more details and how we noticed it.
2356 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2359 PERROR("pthread_condattr_setclock consumer data");
2363 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2366 PERROR("pthread_cond_init consumer data");
2370 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2371 thread_manage_consumer
, consumer_data
);
2374 PERROR("pthread_create consumer");
2379 /* We are about to wait on a pthread condition */
2380 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2382 /* Get time for sem_timedwait absolute timeout */
2383 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2385 * Set the timeout for the condition timed wait even if the clock gettime
2386 * call fails since we might loop on that call and we want to avoid to
2387 * increment the timeout too many times.
2389 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2392 * The following loop COULD be skipped in some conditions so this is why we
2393 * set ret to 0 in order to make sure at least one round of the loop is
2399 * Loop until the condition is reached or when a timeout is reached. Note
2400 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2401 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2402 * possible. This loop does not take any chances and works with both of
2405 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2406 if (clock_ret
< 0) {
2407 PERROR("clock_gettime spawn consumer");
2408 /* Infinite wait for the consumerd thread to be ready */
2409 ret
= pthread_cond_wait(&consumer_data
->cond
,
2410 &consumer_data
->cond_mutex
);
2412 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2413 &consumer_data
->cond_mutex
, &timeout
);
2417 /* Release the pthread condition */
2418 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2422 if (ret
== ETIMEDOUT
) {
2426 * Call has timed out so we kill the kconsumerd_thread and return
2429 ERR("Condition timed out. The consumer thread was never ready."
2431 pth_ret
= pthread_cancel(consumer_data
->thread
);
2433 PERROR("pthread_cancel consumer thread");
2436 PERROR("pthread_cond_wait failed consumer thread");
2438 /* Caller is expecting a negative value on failure. */
2443 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2444 if (consumer_data
->pid
== 0) {
2445 ERR("Consumerd did not start");
2446 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2449 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2458 * Join consumer thread
2460 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2464 /* Consumer pid must be a real one. */
2465 if (consumer_data
->pid
> 0) {
2467 ret
= kill(consumer_data
->pid
, SIGTERM
);
2469 PERROR("Error killing consumer daemon");
2472 return pthread_join(consumer_data
->thread
, &status
);
2479 * Fork and exec a consumer daemon (consumerd).
2481 * Return pid if successful else -1.
2483 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2487 const char *consumer_to_use
;
2488 const char *verbosity
;
2491 DBG("Spawning consumerd");
2498 if (opt_verbose_consumer
) {
2499 verbosity
= "--verbose";
2500 } else if (lttng_opt_quiet
) {
2501 verbosity
= "--quiet";
2506 switch (consumer_data
->type
) {
2507 case LTTNG_CONSUMER_KERNEL
:
2509 * Find out which consumerd to execute. We will first try the
2510 * 64-bit path, then the sessiond's installation directory, and
2511 * fallback on the 32-bit one,
2513 DBG3("Looking for a kernel consumer at these locations:");
2514 DBG3(" 1) %s", consumerd64_bin
);
2515 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2516 DBG3(" 3) %s", consumerd32_bin
);
2517 if (stat(consumerd64_bin
, &st
) == 0) {
2518 DBG3("Found location #1");
2519 consumer_to_use
= consumerd64_bin
;
2520 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2521 DBG3("Found location #2");
2522 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2523 } else if (stat(consumerd32_bin
, &st
) == 0) {
2524 DBG3("Found location #3");
2525 consumer_to_use
= consumerd32_bin
;
2527 DBG("Could not find any valid consumerd executable");
2531 DBG("Using kernel consumer at: %s", consumer_to_use
);
2532 ret
= execl(consumer_to_use
,
2533 "lttng-consumerd", verbosity
, "-k",
2534 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2535 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2536 "--group", tracing_group_name
,
2539 case LTTNG_CONSUMER64_UST
:
2541 char *tmpnew
= NULL
;
2543 if (consumerd64_libdir
[0] != '\0') {
2547 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2551 tmplen
= strlen("LD_LIBRARY_PATH=")
2552 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2553 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2558 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2559 strcat(tmpnew
, consumerd64_libdir
);
2560 if (tmp
[0] != '\0') {
2561 strcat(tmpnew
, ":");
2562 strcat(tmpnew
, tmp
);
2564 ret
= putenv(tmpnew
);
2571 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2572 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2573 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2574 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2575 "--group", tracing_group_name
,
2577 if (consumerd64_libdir
[0] != '\0') {
2582 case LTTNG_CONSUMER32_UST
:
2584 char *tmpnew
= NULL
;
2586 if (consumerd32_libdir
[0] != '\0') {
2590 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2594 tmplen
= strlen("LD_LIBRARY_PATH=")
2595 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2596 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2601 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2602 strcat(tmpnew
, consumerd32_libdir
);
2603 if (tmp
[0] != '\0') {
2604 strcat(tmpnew
, ":");
2605 strcat(tmpnew
, tmp
);
2607 ret
= putenv(tmpnew
);
2614 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2615 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2616 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2617 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2618 "--group", tracing_group_name
,
2620 if (consumerd32_libdir
[0] != '\0') {
2626 PERROR("unknown consumer type");
2630 PERROR("Consumer execl()");
2632 /* Reaching this point, we got a failure on our execl(). */
2634 } else if (pid
> 0) {
2637 PERROR("start consumer fork");
2645 * Spawn the consumerd daemon and session daemon thread.
2647 static int start_consumerd(struct consumer_data
*consumer_data
)
2652 * Set the listen() state on the socket since there is a possible race
2653 * between the exec() of the consumer daemon and this call if place in the
2654 * consumer thread. See bug #366 for more details.
2656 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2661 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2662 if (consumer_data
->pid
!= 0) {
2663 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2667 ret
= spawn_consumerd(consumer_data
);
2669 ERR("Spawning consumerd failed");
2670 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2674 /* Setting up the consumer_data pid */
2675 consumer_data
->pid
= ret
;
2676 DBG2("Consumer pid %d", consumer_data
->pid
);
2677 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2679 DBG2("Spawning consumer control thread");
2680 ret
= spawn_consumer_thread(consumer_data
);
2682 ERR("Fatal error spawning consumer control thread");
2690 /* Cleanup already created sockets on error. */
2691 if (consumer_data
->err_sock
>= 0) {
2694 err
= close(consumer_data
->err_sock
);
2696 PERROR("close consumer data error socket");
2703 * Setup necessary data for kernel tracer action.
2705 static int init_kernel_tracer(void)
2709 /* Modprobe lttng kernel modules */
2710 ret
= modprobe_lttng_control();
2715 /* Open debugfs lttng */
2716 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2717 if (kernel_tracer_fd
< 0) {
2718 DBG("Failed to open %s", module_proc_lttng
);
2722 /* Validate kernel version */
2723 ret
= kernel_validate_version(kernel_tracer_fd
);
2728 ret
= modprobe_lttng_data();
2733 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2737 modprobe_remove_lttng_control();
2738 ret
= close(kernel_tracer_fd
);
2742 kernel_tracer_fd
= -1;
2743 return LTTNG_ERR_KERN_VERSION
;
2746 ret
= close(kernel_tracer_fd
);
2752 modprobe_remove_lttng_control();
2755 WARN("No kernel tracer available");
2756 kernel_tracer_fd
= -1;
2758 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2760 return LTTNG_ERR_KERN_NA
;
2766 * Copy consumer output from the tracing session to the domain session. The
2767 * function also applies the right modification on a per domain basis for the
2768 * trace files destination directory.
2770 * Should *NOT* be called with RCU read-side lock held.
2772 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2775 const char *dir_name
;
2776 struct consumer_output
*consumer
;
2779 assert(session
->consumer
);
2782 case LTTNG_DOMAIN_KERNEL
:
2783 DBG3("Copying tracing session consumer output in kernel session");
2785 * XXX: We should audit the session creation and what this function
2786 * does "extra" in order to avoid a destroy since this function is used
2787 * in the domain session creation (kernel and ust) only. Same for UST
2790 if (session
->kernel_session
->consumer
) {
2791 consumer_output_put(session
->kernel_session
->consumer
);
2793 session
->kernel_session
->consumer
=
2794 consumer_copy_output(session
->consumer
);
2795 /* Ease our life a bit for the next part */
2796 consumer
= session
->kernel_session
->consumer
;
2797 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2799 case LTTNG_DOMAIN_JUL
:
2800 case LTTNG_DOMAIN_LOG4J
:
2801 case LTTNG_DOMAIN_PYTHON
:
2802 case LTTNG_DOMAIN_UST
:
2803 DBG3("Copying tracing session consumer output in UST session");
2804 if (session
->ust_session
->consumer
) {
2805 consumer_output_put(session
->ust_session
->consumer
);
2807 session
->ust_session
->consumer
=
2808 consumer_copy_output(session
->consumer
);
2809 /* Ease our life a bit for the next part */
2810 consumer
= session
->ust_session
->consumer
;
2811 dir_name
= DEFAULT_UST_TRACE_DIR
;
2814 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2818 /* Append correct directory to subdir */
2819 strncat(consumer
->subdir
, dir_name
,
2820 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2821 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2830 * Create an UST session and add it to the session ust list.
2832 * Should *NOT* be called with RCU read-side lock held.
2834 static int create_ust_session(struct ltt_session
*session
,
2835 struct lttng_domain
*domain
)
2838 struct ltt_ust_session
*lus
= NULL
;
2842 assert(session
->consumer
);
2844 switch (domain
->type
) {
2845 case LTTNG_DOMAIN_JUL
:
2846 case LTTNG_DOMAIN_LOG4J
:
2847 case LTTNG_DOMAIN_PYTHON
:
2848 case LTTNG_DOMAIN_UST
:
2851 ERR("Unknown UST domain on create session %d", domain
->type
);
2852 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2856 DBG("Creating UST session");
2858 lus
= trace_ust_create_session(session
->id
);
2860 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2864 lus
->uid
= session
->uid
;
2865 lus
->gid
= session
->gid
;
2866 lus
->output_traces
= session
->output_traces
;
2867 lus
->snapshot_mode
= session
->snapshot_mode
;
2868 lus
->live_timer_interval
= session
->live_timer
;
2869 session
->ust_session
= lus
;
2870 if (session
->shm_path
[0]) {
2871 strncpy(lus
->root_shm_path
, session
->shm_path
,
2872 sizeof(lus
->root_shm_path
));
2873 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2874 strncpy(lus
->shm_path
, session
->shm_path
,
2875 sizeof(lus
->shm_path
));
2876 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2877 strncat(lus
->shm_path
, "/ust",
2878 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2880 /* Copy session output to the newly created UST session */
2881 ret
= copy_session_consumer(domain
->type
, session
);
2882 if (ret
!= LTTNG_OK
) {
2890 session
->ust_session
= NULL
;
2895 * Create a kernel tracer session then create the default channel.
2897 static int create_kernel_session(struct ltt_session
*session
)
2901 DBG("Creating kernel session");
2903 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2905 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2909 /* Code flow safety */
2910 assert(session
->kernel_session
);
2912 /* Copy session output to the newly created Kernel session */
2913 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2914 if (ret
!= LTTNG_OK
) {
2918 /* Create directory(ies) on local filesystem. */
2919 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2920 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2921 ret
= run_as_mkdir_recursive(
2922 session
->kernel_session
->consumer
->dst
.trace_path
,
2923 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2925 if (errno
!= EEXIST
) {
2926 ERR("Trace directory creation error");
2932 session
->kernel_session
->uid
= session
->uid
;
2933 session
->kernel_session
->gid
= session
->gid
;
2934 session
->kernel_session
->output_traces
= session
->output_traces
;
2935 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2940 trace_kernel_destroy_session(session
->kernel_session
);
2941 session
->kernel_session
= NULL
;
2946 * Count number of session permitted by uid/gid.
2948 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2951 struct ltt_session
*session
;
2953 DBG("Counting number of available session for UID %d GID %d",
2955 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2957 * Only list the sessions the user can control.
2959 if (!session_access_ok(session
, uid
, gid
)) {
2968 * Process the command requested by the lttng client within the command
2969 * context structure. This function make sure that the return structure (llm)
2970 * is set and ready for transmission before returning.
2972 * Return any error encountered or 0 for success.
2974 * "sock" is only used for special-case var. len data.
2976 * Should *NOT* be called with RCU read-side lock held.
2978 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2982 int need_tracing_session
= 1;
2985 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2987 assert(!rcu_read_ongoing());
2991 switch (cmd_ctx
->lsm
->cmd_type
) {
2992 case LTTNG_CREATE_SESSION
:
2993 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2994 case LTTNG_CREATE_SESSION_LIVE
:
2995 case LTTNG_DESTROY_SESSION
:
2996 case LTTNG_LIST_SESSIONS
:
2997 case LTTNG_LIST_DOMAINS
:
2998 case LTTNG_START_TRACE
:
2999 case LTTNG_STOP_TRACE
:
3000 case LTTNG_DATA_PENDING
:
3001 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3002 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3003 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3004 case LTTNG_SNAPSHOT_RECORD
:
3005 case LTTNG_SAVE_SESSION
:
3006 case LTTNG_SET_SESSION_SHM_PATH
:
3007 case LTTNG_REGENERATE_METADATA
:
3008 case LTTNG_REGENERATE_STATEDUMP
:
3015 if (opt_no_kernel
&& need_domain
3016 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
3018 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3020 ret
= LTTNG_ERR_KERN_NA
;
3025 /* Deny register consumer if we already have a spawned consumer. */
3026 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
3027 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3028 if (kconsumer_data
.pid
> 0) {
3029 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3030 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3033 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3037 * Check for command that don't needs to allocate a returned payload. We do
3038 * this here so we don't have to make the call for no payload at each
3041 switch(cmd_ctx
->lsm
->cmd_type
) {
3042 case LTTNG_LIST_SESSIONS
:
3043 case LTTNG_LIST_TRACEPOINTS
:
3044 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3045 case LTTNG_LIST_DOMAINS
:
3046 case LTTNG_LIST_CHANNELS
:
3047 case LTTNG_LIST_EVENTS
:
3048 case LTTNG_LIST_SYSCALLS
:
3049 case LTTNG_LIST_TRACKER_PIDS
:
3050 case LTTNG_DATA_PENDING
:
3053 /* Setup lttng message with no payload */
3054 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
3056 /* This label does not try to unlock the session */
3057 goto init_setup_error
;
3061 /* Commands that DO NOT need a session. */
3062 switch (cmd_ctx
->lsm
->cmd_type
) {
3063 case LTTNG_CREATE_SESSION
:
3064 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3065 case LTTNG_CREATE_SESSION_LIVE
:
3066 case LTTNG_LIST_SESSIONS
:
3067 case LTTNG_LIST_TRACEPOINTS
:
3068 case LTTNG_LIST_SYSCALLS
:
3069 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3070 case LTTNG_SAVE_SESSION
:
3071 need_tracing_session
= 0;
3074 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3076 * We keep the session list lock across _all_ commands
3077 * for now, because the per-session lock does not
3078 * handle teardown properly.
3080 session_lock_list();
3081 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3082 if (cmd_ctx
->session
== NULL
) {
3083 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3086 /* Acquire lock for the session */
3087 session_lock(cmd_ctx
->session
);
3093 * Commands that need a valid session but should NOT create one if none
3094 * exists. Instead of creating one and destroying it when the command is
3095 * handled, process that right before so we save some round trip in useless
3098 switch (cmd_ctx
->lsm
->cmd_type
) {
3099 case LTTNG_DISABLE_CHANNEL
:
3100 case LTTNG_DISABLE_EVENT
:
3101 switch (cmd_ctx
->lsm
->domain
.type
) {
3102 case LTTNG_DOMAIN_KERNEL
:
3103 if (!cmd_ctx
->session
->kernel_session
) {
3104 ret
= LTTNG_ERR_NO_CHANNEL
;
3108 case LTTNG_DOMAIN_JUL
:
3109 case LTTNG_DOMAIN_LOG4J
:
3110 case LTTNG_DOMAIN_PYTHON
:
3111 case LTTNG_DOMAIN_UST
:
3112 if (!cmd_ctx
->session
->ust_session
) {
3113 ret
= LTTNG_ERR_NO_CHANNEL
;
3118 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3130 * Check domain type for specific "pre-action".
3132 switch (cmd_ctx
->lsm
->domain
.type
) {
3133 case LTTNG_DOMAIN_KERNEL
:
3135 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3139 /* Kernel tracer check */
3140 if (kernel_tracer_fd
== -1) {
3141 /* Basically, load kernel tracer modules */
3142 ret
= init_kernel_tracer();
3148 /* Consumer is in an ERROR state. Report back to client */
3149 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3150 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3154 /* Need a session for kernel command */
3155 if (need_tracing_session
) {
3156 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3157 ret
= create_kernel_session(cmd_ctx
->session
);
3159 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3164 /* Start the kernel consumer daemon */
3165 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3166 if (kconsumer_data
.pid
== 0 &&
3167 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3168 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3169 ret
= start_consumerd(&kconsumer_data
);
3171 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3174 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3176 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3180 * The consumer was just spawned so we need to add the socket to
3181 * the consumer output of the session if exist.
3183 ret
= consumer_create_socket(&kconsumer_data
,
3184 cmd_ctx
->session
->kernel_session
->consumer
);
3191 case LTTNG_DOMAIN_JUL
:
3192 case LTTNG_DOMAIN_LOG4J
:
3193 case LTTNG_DOMAIN_PYTHON
:
3194 case LTTNG_DOMAIN_UST
:
3196 if (!ust_app_supported()) {
3197 ret
= LTTNG_ERR_NO_UST
;
3200 /* Consumer is in an ERROR state. Report back to client */
3201 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3202 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3206 if (need_tracing_session
) {
3207 /* Create UST session if none exist. */
3208 if (cmd_ctx
->session
->ust_session
== NULL
) {
3209 ret
= create_ust_session(cmd_ctx
->session
,
3210 &cmd_ctx
->lsm
->domain
);
3211 if (ret
!= LTTNG_OK
) {
3216 /* Start the UST consumer daemons */
3218 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3219 if (consumerd64_bin
[0] != '\0' &&
3220 ustconsumer64_data
.pid
== 0 &&
3221 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3222 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3223 ret
= start_consumerd(&ustconsumer64_data
);
3225 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3226 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3230 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3231 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3233 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3237 * Setup socket for consumer 64 bit. No need for atomic access
3238 * since it was set above and can ONLY be set in this thread.
3240 ret
= consumer_create_socket(&ustconsumer64_data
,
3241 cmd_ctx
->session
->ust_session
->consumer
);
3247 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3248 if (consumerd32_bin
[0] != '\0' &&
3249 ustconsumer32_data
.pid
== 0 &&
3250 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3251 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3252 ret
= start_consumerd(&ustconsumer32_data
);
3254 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3255 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3259 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3260 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3262 pthread_mutex_unlock(&ustconsumer32_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(&ustconsumer32_data
,
3270 cmd_ctx
->session
->ust_session
->consumer
);
3282 /* Validate consumer daemon state when start/stop trace command */
3283 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3284 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3285 switch (cmd_ctx
->lsm
->domain
.type
) {
3286 case LTTNG_DOMAIN_NONE
:
3288 case LTTNG_DOMAIN_JUL
:
3289 case LTTNG_DOMAIN_LOG4J
:
3290 case LTTNG_DOMAIN_PYTHON
:
3291 case LTTNG_DOMAIN_UST
:
3292 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3293 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3297 case LTTNG_DOMAIN_KERNEL
:
3298 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3299 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3304 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3310 * Check that the UID or GID match that of the tracing session.
3311 * The root user can interact with all sessions.
3313 if (need_tracing_session
) {
3314 if (!session_access_ok(cmd_ctx
->session
,
3315 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3316 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3317 ret
= LTTNG_ERR_EPERM
;
3323 * Send relayd information to consumer as soon as we have a domain and a
3326 if (cmd_ctx
->session
&& need_domain
) {
3328 * Setup relayd if not done yet. If the relayd information was already
3329 * sent to the consumer, this call will gracefully return.
3331 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3332 if (ret
!= LTTNG_OK
) {
3337 /* Process by command type */
3338 switch (cmd_ctx
->lsm
->cmd_type
) {
3339 case LTTNG_ADD_CONTEXT
:
3342 * An LTTNG_ADD_CONTEXT command might have a supplementary
3343 * payload if the context being added is an application context.
3345 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3346 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3347 char *provider_name
= NULL
, *context_name
= NULL
;
3348 size_t provider_name_len
=
3349 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3350 size_t context_name_len
=
3351 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3353 if (provider_name_len
== 0 || context_name_len
== 0) {
3355 * Application provider and context names MUST
3358 ret
= -LTTNG_ERR_INVALID
;
3362 provider_name
= zmalloc(provider_name_len
+ 1);
3363 if (!provider_name
) {
3364 ret
= -LTTNG_ERR_NOMEM
;
3367 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3370 context_name
= zmalloc(context_name_len
+ 1);
3371 if (!context_name
) {
3372 ret
= -LTTNG_ERR_NOMEM
;
3373 goto error_add_context
;
3375 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3378 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3381 goto error_add_context
;
3384 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3387 goto error_add_context
;
3392 * cmd_add_context assumes ownership of the provider and context
3395 ret
= cmd_add_context(cmd_ctx
->session
,
3396 cmd_ctx
->lsm
->domain
.type
,
3397 cmd_ctx
->lsm
->u
.context
.channel_name
,
3398 &cmd_ctx
->lsm
->u
.context
.ctx
,
3399 kernel_poll_pipe
[1]);
3401 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3402 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3404 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3405 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3411 case LTTNG_DISABLE_CHANNEL
:
3413 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3414 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3417 case LTTNG_DISABLE_EVENT
:
3421 * FIXME: handle filter; for now we just receive the filter's
3422 * bytecode along with the filter expression which are sent by
3423 * liblttng-ctl and discard them.
3425 * This fixes an issue where the client may block while sending
3426 * the filter payload and encounter an error because the session
3427 * daemon closes the socket without ever handling this data.
3429 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3430 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3433 char data
[LTTNG_FILTER_MAX_LEN
];
3435 DBG("Discarding disable event command payload of size %zu", count
);
3437 ret
= lttcomm_recv_unix_sock(sock
, data
,
3438 count
> sizeof(data
) ? sizeof(data
) : count
);
3443 count
-= (size_t) ret
;
3446 /* FIXME: passing packed structure to non-packed pointer */
3447 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3448 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3449 &cmd_ctx
->lsm
->u
.disable
.event
);
3452 case LTTNG_ENABLE_CHANNEL
:
3454 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3455 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
3458 case LTTNG_TRACK_PID
:
3460 ret
= cmd_track_pid(cmd_ctx
->session
,
3461 cmd_ctx
->lsm
->domain
.type
,
3462 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3465 case LTTNG_UNTRACK_PID
:
3467 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3468 cmd_ctx
->lsm
->domain
.type
,
3469 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3472 case LTTNG_ENABLE_EVENT
:
3474 struct lttng_event_exclusion
*exclusion
= NULL
;
3475 struct lttng_filter_bytecode
*bytecode
= NULL
;
3476 char *filter_expression
= NULL
;
3478 /* Handle exclusion events and receive it from the client. */
3479 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3480 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3482 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3483 (count
* LTTNG_SYMBOL_NAME_LEN
));
3485 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3489 DBG("Receiving var len exclusion event list from client ...");
3490 exclusion
->count
= count
;
3491 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3492 count
* LTTNG_SYMBOL_NAME_LEN
);
3494 DBG("Nothing recv() from client var len data... continuing");
3497 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3502 /* Get filter expression from client. */
3503 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3504 size_t expression_len
=
3505 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3507 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3508 ret
= LTTNG_ERR_FILTER_INVAL
;
3513 filter_expression
= zmalloc(expression_len
);
3514 if (!filter_expression
) {
3516 ret
= LTTNG_ERR_FILTER_NOMEM
;
3520 /* Receive var. len. data */
3521 DBG("Receiving var len filter's expression from client ...");
3522 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3525 DBG("Nothing recv() from client car len data... continuing");
3527 free(filter_expression
);
3529 ret
= LTTNG_ERR_FILTER_INVAL
;
3534 /* Handle filter and get bytecode from client. */
3535 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3536 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3538 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3539 ret
= LTTNG_ERR_FILTER_INVAL
;
3540 free(filter_expression
);
3545 bytecode
= zmalloc(bytecode_len
);
3547 free(filter_expression
);
3549 ret
= LTTNG_ERR_FILTER_NOMEM
;
3553 /* Receive var. len. data */
3554 DBG("Receiving var len filter's bytecode from client ...");
3555 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3557 DBG("Nothing recv() from client car len data... continuing");
3559 free(filter_expression
);
3562 ret
= LTTNG_ERR_FILTER_INVAL
;
3566 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3567 free(filter_expression
);
3570 ret
= LTTNG_ERR_FILTER_INVAL
;
3575 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3576 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3577 &cmd_ctx
->lsm
->u
.enable
.event
,
3578 filter_expression
, bytecode
, exclusion
,
3579 kernel_poll_pipe
[1]);
3582 case LTTNG_LIST_TRACEPOINTS
:
3584 struct lttng_event
*events
;
3587 session_lock_list();
3588 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3589 session_unlock_list();
3590 if (nb_events
< 0) {
3591 /* Return value is a negative lttng_error_code. */
3597 * Setup lttng message with payload size set to the event list size in
3598 * bytes and then copy list into the llm payload.
3600 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3601 sizeof(struct lttng_event
) * nb_events
);
3611 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3613 struct lttng_event_field
*fields
;
3616 session_lock_list();
3617 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3619 session_unlock_list();
3620 if (nb_fields
< 0) {
3621 /* Return value is a negative lttng_error_code. */
3627 * Setup lttng message with payload size set to the event list size in
3628 * bytes and then copy list into the llm payload.
3630 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3631 sizeof(struct lttng_event_field
) * nb_fields
);
3641 case LTTNG_LIST_SYSCALLS
:
3643 struct lttng_event
*events
;
3646 nb_events
= cmd_list_syscalls(&events
);
3647 if (nb_events
< 0) {
3648 /* Return value is a negative lttng_error_code. */
3654 * Setup lttng message with payload size set to the event list size in
3655 * bytes and then copy list into the llm payload.
3657 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3658 sizeof(struct lttng_event
) * nb_events
);
3668 case LTTNG_LIST_TRACKER_PIDS
:
3670 int32_t *pids
= NULL
;
3673 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3674 cmd_ctx
->lsm
->domain
.type
, &pids
);
3676 /* Return value is a negative lttng_error_code. */
3682 * Setup lttng message with payload size set to the event list size in
3683 * bytes and then copy list into the llm payload.
3685 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3686 sizeof(int32_t) * nr_pids
);
3696 case LTTNG_SET_CONSUMER_URI
:
3699 struct lttng_uri
*uris
;
3701 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3702 len
= nb_uri
* sizeof(struct lttng_uri
);
3705 ret
= LTTNG_ERR_INVALID
;
3709 uris
= zmalloc(len
);
3711 ret
= LTTNG_ERR_FATAL
;
3715 /* Receive variable len data */
3716 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3717 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3719 DBG("No URIs received from client... continuing");
3721 ret
= LTTNG_ERR_SESSION_FAIL
;
3726 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3728 if (ret
!= LTTNG_OK
) {
3735 case LTTNG_START_TRACE
:
3737 ret
= cmd_start_trace(cmd_ctx
->session
);
3740 case LTTNG_STOP_TRACE
:
3742 ret
= cmd_stop_trace(cmd_ctx
->session
);
3745 case LTTNG_CREATE_SESSION
:
3748 struct lttng_uri
*uris
= NULL
;
3750 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3751 len
= nb_uri
* sizeof(struct lttng_uri
);
3754 uris
= zmalloc(len
);
3756 ret
= LTTNG_ERR_FATAL
;
3760 /* Receive variable len data */
3761 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3762 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3764 DBG("No URIs received from client... continuing");
3766 ret
= LTTNG_ERR_SESSION_FAIL
;
3771 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3772 DBG("Creating session with ONE network URI is a bad call");
3773 ret
= LTTNG_ERR_SESSION_FAIL
;
3779 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3780 &cmd_ctx
->creds
, 0);
3786 case LTTNG_DESTROY_SESSION
:
3788 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3790 /* Set session to NULL so we do not unlock it after free. */
3791 cmd_ctx
->session
= NULL
;
3794 case LTTNG_LIST_DOMAINS
:
3797 struct lttng_domain
*domains
= NULL
;
3799 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3801 /* Return value is a negative lttng_error_code. */
3806 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3807 nb_dom
* sizeof(struct lttng_domain
));
3817 case LTTNG_LIST_CHANNELS
:
3819 ssize_t payload_size
;
3820 struct lttng_channel
*channels
= NULL
;
3822 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3823 cmd_ctx
->session
, &channels
);
3824 if (payload_size
< 0) {
3825 /* Return value is a negative lttng_error_code. */
3826 ret
= -payload_size
;
3830 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3841 case LTTNG_LIST_EVENTS
:
3844 struct lttng_event
*events
= NULL
;
3845 struct lttcomm_event_command_header cmd_header
;
3848 memset(&cmd_header
, 0, sizeof(cmd_header
));
3849 /* Extended infos are included at the end of events */
3850 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3851 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3852 &events
, &total_size
);
3855 /* Return value is a negative lttng_error_code. */
3860 cmd_header
.nb_events
= nb_event
;
3861 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3862 &cmd_header
, sizeof(cmd_header
));
3872 case LTTNG_LIST_SESSIONS
:
3874 unsigned int nr_sessions
;
3875 void *sessions_payload
;
3878 session_lock_list();
3879 nr_sessions
= lttng_sessions_count(
3880 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3881 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3882 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
3883 sessions_payload
= zmalloc(payload_len
);
3885 if (!sessions_payload
) {
3886 session_unlock_list();
3891 cmd_list_lttng_sessions(sessions_payload
,
3892 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3893 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3894 session_unlock_list();
3896 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
3898 free(sessions_payload
);
3907 case LTTNG_REGISTER_CONSUMER
:
3909 struct consumer_data
*cdata
;
3911 switch (cmd_ctx
->lsm
->domain
.type
) {
3912 case LTTNG_DOMAIN_KERNEL
:
3913 cdata
= &kconsumer_data
;
3916 ret
= LTTNG_ERR_UND
;
3920 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3921 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3924 case LTTNG_DATA_PENDING
:
3927 uint8_t pending_ret_byte
;
3929 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
3934 * This function may returns 0 or 1 to indicate whether or not
3935 * there is data pending. In case of error, it should return an
3936 * LTTNG_ERR code. However, some code paths may still return
3937 * a nondescript error code, which we handle by returning an
3940 if (pending_ret
== 0 || pending_ret
== 1) {
3942 * ret will be set to LTTNG_OK at the end of
3945 } else if (pending_ret
< 0) {
3946 ret
= LTTNG_ERR_UNK
;
3953 pending_ret_byte
= (uint8_t) pending_ret
;
3955 /* 1 byte to return whether or not data is pending */
3956 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
3957 &pending_ret_byte
, 1);
3966 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3968 struct lttcomm_lttng_output_id reply
;
3970 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3971 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3972 if (ret
!= LTTNG_OK
) {
3976 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
3982 /* Copy output list into message payload */
3986 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3988 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3989 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3992 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3995 struct lttng_snapshot_output
*outputs
= NULL
;
3997 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3998 if (nb_output
< 0) {
4003 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
4004 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
4005 nb_output
* sizeof(struct lttng_snapshot_output
));
4015 case LTTNG_SNAPSHOT_RECORD
:
4017 ret
= cmd_snapshot_record(cmd_ctx
->session
,
4018 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
4019 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
4022 case LTTNG_CREATE_SESSION_SNAPSHOT
:
4025 struct lttng_uri
*uris
= NULL
;
4027 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4028 len
= nb_uri
* sizeof(struct lttng_uri
);
4031 uris
= zmalloc(len
);
4033 ret
= LTTNG_ERR_FATAL
;
4037 /* Receive variable len data */
4038 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4039 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4041 DBG("No URIs received from client... continuing");
4043 ret
= LTTNG_ERR_SESSION_FAIL
;
4048 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4049 DBG("Creating session with ONE network URI is a bad call");
4050 ret
= LTTNG_ERR_SESSION_FAIL
;
4056 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
4057 nb_uri
, &cmd_ctx
->creds
);
4061 case LTTNG_CREATE_SESSION_LIVE
:
4064 struct lttng_uri
*uris
= NULL
;
4066 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4067 len
= nb_uri
* sizeof(struct lttng_uri
);
4070 uris
= zmalloc(len
);
4072 ret
= LTTNG_ERR_FATAL
;
4076 /* Receive variable len data */
4077 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4078 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4080 DBG("No URIs received from client... continuing");
4082 ret
= LTTNG_ERR_SESSION_FAIL
;
4087 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4088 DBG("Creating session with ONE network URI is a bad call");
4089 ret
= LTTNG_ERR_SESSION_FAIL
;
4095 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4096 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4100 case LTTNG_SAVE_SESSION
:
4102 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4106 case LTTNG_SET_SESSION_SHM_PATH
:
4108 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4109 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4112 case LTTNG_REGENERATE_METADATA
:
4114 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4117 case LTTNG_REGENERATE_STATEDUMP
:
4119 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4123 ret
= LTTNG_ERR_UND
;
4128 if (cmd_ctx
->llm
== NULL
) {
4129 DBG("Missing llm structure. Allocating one.");
4130 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4134 /* Set return code */
4135 cmd_ctx
->llm
->ret_code
= ret
;
4137 if (cmd_ctx
->session
) {
4138 session_unlock(cmd_ctx
->session
);
4140 if (need_tracing_session
) {
4141 session_unlock_list();
4144 assert(!rcu_read_ongoing());
4149 * Thread managing health check socket.
4151 static void *thread_manage_health(void *data
)
4153 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4154 uint32_t revents
, nb_fd
;
4155 struct lttng_poll_event events
;
4156 struct health_comm_msg msg
;
4157 struct health_comm_reply reply
;
4159 DBG("[thread] Manage health check started");
4161 rcu_register_thread();
4163 /* We might hit an error path before this is created. */
4164 lttng_poll_init(&events
);
4166 /* Create unix socket */
4167 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
4169 ERR("Unable to create health check Unix socket");
4174 /* lttng health client socket path permissions */
4175 ret
= chown(health_unix_sock_path
, 0,
4176 utils_get_group_id(tracing_group_name
));
4178 ERR("Unable to set group on %s", health_unix_sock_path
);
4183 ret
= chmod(health_unix_sock_path
,
4184 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4186 ERR("Unable to set permissions on %s", health_unix_sock_path
);
4193 * Set the CLOEXEC flag. Return code is useless because either way, the
4196 (void) utils_set_fd_cloexec(sock
);
4198 ret
= lttcomm_listen_unix_sock(sock
);
4204 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4205 * more will be added to this poll set.
4207 ret
= sessiond_set_thread_pollset(&events
, 2);
4212 /* Add the application registration socket */
4213 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4218 sessiond_notify_ready();
4221 DBG("Health check ready");
4223 /* Inifinite blocking call, waiting for transmission */
4225 ret
= lttng_poll_wait(&events
, -1);
4228 * Restart interrupted system call.
4230 if (errno
== EINTR
) {
4238 for (i
= 0; i
< nb_fd
; i
++) {
4239 /* Fetch once the poll data */
4240 revents
= LTTNG_POLL_GETEV(&events
, i
);
4241 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4244 /* No activity for this FD (poll implementation). */
4248 /* Thread quit pipe has been closed. Killing thread. */
4249 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4255 /* Event on the registration socket */
4256 if (pollfd
== sock
) {
4257 if (revents
& LPOLLIN
) {
4259 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4260 ERR("Health socket poll error");
4263 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4269 new_sock
= lttcomm_accept_unix_sock(sock
);
4275 * Set the CLOEXEC flag. Return code is useless because either way, the
4278 (void) utils_set_fd_cloexec(new_sock
);
4280 DBG("Receiving data from client for health...");
4281 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4283 DBG("Nothing recv() from client... continuing");
4284 ret
= close(new_sock
);
4291 rcu_thread_online();
4293 memset(&reply
, 0, sizeof(reply
));
4294 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4296 * health_check_state returns 0 if health is
4299 if (!health_check_state(health_sessiond
, i
)) {
4300 reply
.ret_code
|= 1ULL << i
;
4304 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4306 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4308 ERR("Failed to send health data back to client");
4311 /* End of transmission */
4312 ret
= close(new_sock
);
4321 ERR("Health error occurred in %s", __func__
);
4323 DBG("Health check thread dying");
4324 unlink(health_unix_sock_path
);
4332 lttng_poll_clean(&events
);
4334 rcu_unregister_thread();
4339 * This thread manage all clients request using the unix client socket for
4342 static void *thread_manage_clients(void *data
)
4344 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4346 uint32_t revents
, nb_fd
;
4347 struct command_ctx
*cmd_ctx
= NULL
;
4348 struct lttng_poll_event events
;
4350 DBG("[thread] Manage client started");
4352 rcu_register_thread();
4354 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4356 health_code_update();
4358 ret
= lttcomm_listen_unix_sock(client_sock
);
4364 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4365 * more will be added to this poll set.
4367 ret
= sessiond_set_thread_pollset(&events
, 2);
4369 goto error_create_poll
;
4372 /* Add the application registration socket */
4373 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4378 sessiond_notify_ready();
4379 ret
= sem_post(&load_info
->message_thread_ready
);
4381 PERROR("sem_post message_thread_ready");
4385 /* This testpoint is after we signal readiness to the parent. */
4386 if (testpoint(sessiond_thread_manage_clients
)) {
4390 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4394 health_code_update();
4397 DBG("Accepting client command ...");
4399 /* Inifinite blocking call, waiting for transmission */
4401 health_poll_entry();
4402 ret
= lttng_poll_wait(&events
, -1);
4406 * Restart interrupted system call.
4408 if (errno
== EINTR
) {
4416 for (i
= 0; i
< nb_fd
; i
++) {
4417 /* Fetch once the poll data */
4418 revents
= LTTNG_POLL_GETEV(&events
, i
);
4419 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4421 health_code_update();
4424 /* No activity for this FD (poll implementation). */
4428 /* Thread quit pipe has been closed. Killing thread. */
4429 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4435 /* Event on the registration socket */
4436 if (pollfd
== client_sock
) {
4437 if (revents
& LPOLLIN
) {
4439 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4440 ERR("Client socket poll error");
4443 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4449 DBG("Wait for client response");
4451 health_code_update();
4453 sock
= lttcomm_accept_unix_sock(client_sock
);
4459 * Set the CLOEXEC flag. Return code is useless because either way, the
4462 (void) utils_set_fd_cloexec(sock
);
4464 /* Set socket option for credentials retrieval */
4465 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4470 /* Allocate context command to process the client request */
4471 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4472 if (cmd_ctx
== NULL
) {
4473 PERROR("zmalloc cmd_ctx");
4477 /* Allocate data buffer for reception */
4478 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4479 if (cmd_ctx
->lsm
== NULL
) {
4480 PERROR("zmalloc cmd_ctx->lsm");
4484 cmd_ctx
->llm
= NULL
;
4485 cmd_ctx
->session
= NULL
;
4487 health_code_update();
4490 * Data is received from the lttng client. The struct
4491 * lttcomm_session_msg (lsm) contains the command and data request of
4494 DBG("Receiving data from client ...");
4495 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4496 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4498 DBG("Nothing recv() from client... continuing");
4504 clean_command_ctx(&cmd_ctx
);
4508 health_code_update();
4510 // TODO: Validate cmd_ctx including sanity check for
4511 // security purpose.
4513 rcu_thread_online();
4515 * This function dispatch the work to the kernel or userspace tracer
4516 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4517 * informations for the client. The command context struct contains
4518 * everything this function may needs.
4520 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4521 rcu_thread_offline();
4529 * TODO: Inform client somehow of the fatal error. At
4530 * this point, ret < 0 means that a zmalloc failed
4531 * (ENOMEM). Error detected but still accept
4532 * command, unless a socket error has been
4535 clean_command_ctx(&cmd_ctx
);
4539 health_code_update();
4541 DBG("Sending response (size: %d, retcode: %s (%d))",
4542 cmd_ctx
->lttng_msg_size
,
4543 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4544 cmd_ctx
->llm
->ret_code
);
4545 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4547 ERR("Failed to send data back to client");
4550 /* End of transmission */
4557 clean_command_ctx(&cmd_ctx
);
4559 health_code_update();
4571 lttng_poll_clean(&events
);
4572 clean_command_ctx(&cmd_ctx
);
4576 unlink(client_unix_sock_path
);
4577 if (client_sock
>= 0) {
4578 ret
= close(client_sock
);
4586 ERR("Health error occurred in %s", __func__
);
4589 health_unregister(health_sessiond
);
4591 DBG("Client thread dying");
4593 rcu_unregister_thread();
4596 * Since we are creating the consumer threads, we own them, so we need
4597 * to join them before our thread exits.
4599 ret
= join_consumer_thread(&kconsumer_data
);
4602 PERROR("join_consumer");
4605 ret
= join_consumer_thread(&ustconsumer32_data
);
4608 PERROR("join_consumer ust32");
4611 ret
= join_consumer_thread(&ustconsumer64_data
);
4614 PERROR("join_consumer ust64");
4619 static int string_match(const char *str1
, const char *str2
)
4621 return (str1
&& str2
) && !strcmp(str1
, str2
);
4625 * Take an option from the getopt output and set it in the right variable to be
4628 * Return 0 on success else a negative value.
4630 static int set_option(int opt
, const char *arg
, const char *optname
)
4634 if (string_match(optname
, "client-sock") || opt
== 'c') {
4635 if (!arg
|| *arg
== '\0') {
4639 if (lttng_is_setuid_setgid()) {
4640 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4641 "-c, --client-sock");
4643 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", arg
);
4645 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4646 if (!arg
|| *arg
== '\0') {
4650 if (lttng_is_setuid_setgid()) {
4651 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4654 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", arg
);
4656 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4658 } else if (string_match(optname
, "background") || opt
== 'b') {
4660 } else if (string_match(optname
, "group") || opt
== 'g') {
4661 if (!arg
|| *arg
== '\0') {
4665 if (lttng_is_setuid_setgid()) {
4666 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4670 * If the override option is set, the pointer points to a
4671 * *non* const thus freeing it even though the variable type is
4674 if (tracing_group_name_override
) {
4675 free((void *) tracing_group_name
);
4677 tracing_group_name
= strdup(arg
);
4678 if (!tracing_group_name
) {
4682 tracing_group_name_override
= 1;
4684 } else if (string_match(optname
, "help") || opt
== 'h') {
4685 ret
= utils_show_man_page(8, "lttng-sessiond");
4687 ERR("Cannot view man page lttng-sessiond(8)");
4690 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
4691 } else if (string_match(optname
, "version") || opt
== 'V') {
4692 fprintf(stdout
, "%s\n", VERSION
);
4694 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4696 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4697 if (!arg
|| *arg
== '\0') {
4701 if (lttng_is_setuid_setgid()) {
4702 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4703 "--kconsumerd-err-sock");
4705 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4707 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4708 if (!arg
|| *arg
== '\0') {
4712 if (lttng_is_setuid_setgid()) {
4713 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4714 "--kconsumerd-cmd-sock");
4716 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4718 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4719 if (!arg
|| *arg
== '\0') {
4723 if (lttng_is_setuid_setgid()) {
4724 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4725 "--ustconsumerd64-err-sock");
4727 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4729 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4730 if (!arg
|| *arg
== '\0') {
4734 if (lttng_is_setuid_setgid()) {
4735 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4736 "--ustconsumerd64-cmd-sock");
4738 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4740 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4741 if (!arg
|| *arg
== '\0') {
4745 if (lttng_is_setuid_setgid()) {
4746 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4747 "--ustconsumerd32-err-sock");
4749 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4751 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4752 if (!arg
|| *arg
== '\0') {
4756 if (lttng_is_setuid_setgid()) {
4757 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4758 "--ustconsumerd32-cmd-sock");
4760 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4762 } else if (string_match(optname
, "no-kernel")) {
4764 } else if (string_match(optname
, "quiet") || opt
== 'q') {
4765 lttng_opt_quiet
= 1;
4766 } else if (string_match(optname
, "verbose") || opt
== 'v') {
4767 /* Verbose level can increase using multiple -v */
4769 /* Value obtained from config file */
4770 lttng_opt_verbose
= config_parse_value(arg
);
4772 /* -v used on command line */
4773 lttng_opt_verbose
++;
4775 /* Clamp value to [0, 3] */
4776 lttng_opt_verbose
= lttng_opt_verbose
< 0 ? 0 :
4777 (lttng_opt_verbose
<= 3 ? lttng_opt_verbose
: 3);
4778 } else if (string_match(optname
, "verbose-consumer")) {
4780 opt_verbose_consumer
= config_parse_value(arg
);
4782 opt_verbose_consumer
++;
4784 } else if (string_match(optname
, "consumerd32-path")) {
4785 if (!arg
|| *arg
== '\0') {
4789 if (lttng_is_setuid_setgid()) {
4790 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4791 "--consumerd32-path");
4793 if (consumerd32_bin_override
) {
4794 free((void *) consumerd32_bin
);
4796 consumerd32_bin
= strdup(arg
);
4797 if (!consumerd32_bin
) {
4801 consumerd32_bin_override
= 1;
4803 } else if (string_match(optname
, "consumerd32-libdir")) {
4804 if (!arg
|| *arg
== '\0') {
4808 if (lttng_is_setuid_setgid()) {
4809 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4810 "--consumerd32-libdir");
4812 if (consumerd32_libdir_override
) {
4813 free((void *) consumerd32_libdir
);
4815 consumerd32_libdir
= strdup(arg
);
4816 if (!consumerd32_libdir
) {
4820 consumerd32_libdir_override
= 1;
4822 } else if (string_match(optname
, "consumerd64-path")) {
4823 if (!arg
|| *arg
== '\0') {
4827 if (lttng_is_setuid_setgid()) {
4828 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4829 "--consumerd64-path");
4831 if (consumerd64_bin_override
) {
4832 free((void *) consumerd64_bin
);
4834 consumerd64_bin
= strdup(arg
);
4835 if (!consumerd64_bin
) {
4839 consumerd64_bin_override
= 1;
4841 } else if (string_match(optname
, "consumerd64-libdir")) {
4842 if (!arg
|| *arg
== '\0') {
4846 if (lttng_is_setuid_setgid()) {
4847 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4848 "--consumerd64-libdir");
4850 if (consumerd64_libdir_override
) {
4851 free((void *) consumerd64_libdir
);
4853 consumerd64_libdir
= strdup(arg
);
4854 if (!consumerd64_libdir
) {
4858 consumerd64_libdir_override
= 1;
4860 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
4861 if (!arg
|| *arg
== '\0') {
4865 if (lttng_is_setuid_setgid()) {
4866 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4870 opt_pidfile
= strdup(arg
);
4876 } else if (string_match(optname
, "agent-tcp-port")) {
4877 if (!arg
|| *arg
== '\0') {
4881 if (lttng_is_setuid_setgid()) {
4882 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4883 "--agent-tcp-port");
4888 v
= strtoul(arg
, NULL
, 0);
4889 if (errno
!= 0 || !isdigit(arg
[0])) {
4890 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
4893 if (v
== 0 || v
>= 65535) {
4894 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
4897 agent_tcp_port
= (uint32_t) v
;
4898 DBG3("Agent TCP port set to non default: %u", agent_tcp_port
);
4900 } else if (string_match(optname
, "load") || opt
== 'l') {
4901 if (!arg
|| *arg
== '\0') {
4905 if (lttng_is_setuid_setgid()) {
4906 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4909 free(opt_load_session_path
);
4910 opt_load_session_path
= strdup(arg
);
4911 if (!opt_load_session_path
) {
4916 } else if (string_match(optname
, "kmod-probes")) {
4917 if (!arg
|| *arg
== '\0') {
4921 if (lttng_is_setuid_setgid()) {
4922 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4925 free(kmod_probes_list
);
4926 kmod_probes_list
= strdup(arg
);
4927 if (!kmod_probes_list
) {
4932 } else if (string_match(optname
, "extra-kmod-probes")) {
4933 if (!arg
|| *arg
== '\0') {
4937 if (lttng_is_setuid_setgid()) {
4938 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4939 "--extra-kmod-probes");
4941 free(kmod_extra_probes_list
);
4942 kmod_extra_probes_list
= strdup(arg
);
4943 if (!kmod_extra_probes_list
) {
4948 } else if (string_match(optname
, "config") || opt
== 'f') {
4949 /* This is handled in set_options() thus silent skip. */
4952 /* Unknown option or other error.
4953 * Error is printed by getopt, just return */
4958 if (ret
== -EINVAL
) {
4959 const char *opt_name
= "unknown";
4962 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
4964 if (opt
== long_options
[i
].val
) {
4965 opt_name
= long_options
[i
].name
;
4970 WARN("Invalid argument provided for option \"%s\", using default value.",
4978 * config_entry_handler_cb used to handle options read from a config file.
4979 * See config_entry_handler_cb comment in common/config/session-config.h for the
4980 * return value conventions.
4982 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
4986 if (!entry
|| !entry
->name
|| !entry
->value
) {
4991 /* Check if the option is to be ignored */
4992 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
4993 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
4998 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
5001 /* Ignore if not fully matched. */
5002 if (strcmp(entry
->name
, long_options
[i
].name
)) {
5007 * If the option takes no argument on the command line, we have to
5008 * check if the value is "true". We support non-zero numeric values,
5011 if (!long_options
[i
].has_arg
) {
5012 ret
= config_parse_value(entry
->value
);
5015 WARN("Invalid configuration value \"%s\" for option %s",
5016 entry
->value
, entry
->name
);
5018 /* False, skip boolean config option. */
5023 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
5027 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
5034 * daemon configuration loading and argument parsing
5036 static int set_options(int argc
, char **argv
)
5038 int ret
= 0, c
= 0, option_index
= 0;
5039 int orig_optopt
= optopt
, orig_optind
= optind
;
5041 const char *config_path
= NULL
;
5043 optstring
= utils_generate_optstring(long_options
,
5044 sizeof(long_options
) / sizeof(struct option
));
5050 /* Check for the --config option */
5051 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
5052 &option_index
)) != -1) {
5056 } else if (c
!= 'f') {
5057 /* if not equal to --config option. */
5061 if (lttng_is_setuid_setgid()) {
5062 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5065 config_path
= utils_expand_path(optarg
);
5067 ERR("Failed to resolve path: %s", optarg
);
5072 ret
= config_get_section_entries(config_path
, config_section_name
,
5073 config_entry_handler
, NULL
);
5076 ERR("Invalid configuration option at line %i", ret
);
5082 /* Reset getopt's global state */
5083 optopt
= orig_optopt
;
5084 optind
= orig_optind
;
5088 * getopt_long() will not set option_index if it encounters a
5091 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5098 * Pass NULL as the long option name if popt left the index
5101 ret
= set_option(c
, optarg
,
5102 option_index
< 0 ? NULL
:
5103 long_options
[option_index
].name
);
5115 * Creates the two needed socket by the daemon.
5116 * apps_sock - The communication socket for all UST apps.
5117 * client_sock - The communication of the cli tool (lttng).
5119 static int init_daemon_socket(void)
5124 old_umask
= umask(0);
5126 /* Create client tool unix socket */
5127 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
5128 if (client_sock
< 0) {
5129 ERR("Create unix sock failed: %s", client_unix_sock_path
);
5134 /* Set the cloexec flag */
5135 ret
= utils_set_fd_cloexec(client_sock
);
5137 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5138 "Continuing but note that the consumer daemon will have a "
5139 "reference to this socket on exec()", client_sock
);
5142 /* File permission MUST be 660 */
5143 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5145 ERR("Set file permissions failed: %s", client_unix_sock_path
);
5150 /* Create the application unix socket */
5151 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
5152 if (apps_sock
< 0) {
5153 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
5158 /* Set the cloexec flag */
5159 ret
= utils_set_fd_cloexec(apps_sock
);
5161 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5162 "Continuing but note that the consumer daemon will have a "
5163 "reference to this socket on exec()", apps_sock
);
5166 /* File permission MUST be 666 */
5167 ret
= chmod(apps_unix_sock_path
,
5168 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5170 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
5175 DBG3("Session daemon client socket %d and application socket %d created",
5176 client_sock
, apps_sock
);
5184 * Check if the global socket is available, and if a daemon is answering at the
5185 * other side. If yes, error is returned.
5187 static int check_existing_daemon(void)
5189 /* Is there anybody out there ? */
5190 if (lttng_session_daemon_alive()) {
5198 * Set the tracing group gid onto the client socket.
5200 * Race window between mkdir and chown is OK because we are going from more
5201 * permissive (root.root) to less permissive (root.tracing).
5203 static int set_permissions(char *rundir
)
5208 gid
= utils_get_group_id(tracing_group_name
);
5210 /* Set lttng run dir */
5211 ret
= chown(rundir
, 0, gid
);
5213 ERR("Unable to set group on %s", rundir
);
5218 * Ensure all applications and tracing group can search the run
5219 * dir. Allow everyone to read the directory, since it does not
5220 * buy us anything to hide its content.
5222 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5224 ERR("Unable to set permissions on %s", rundir
);
5228 /* lttng client socket path */
5229 ret
= chown(client_unix_sock_path
, 0, gid
);
5231 ERR("Unable to set group on %s", client_unix_sock_path
);
5235 /* kconsumer error socket path */
5236 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5238 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5242 /* 64-bit ustconsumer error socket path */
5243 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5245 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5249 /* 32-bit ustconsumer compat32 error socket path */
5250 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5252 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5256 DBG("All permissions are set");
5262 * Create the lttng run directory needed for all global sockets and pipe.
5264 static int create_lttng_rundir(const char *rundir
)
5268 DBG3("Creating LTTng run directory: %s", rundir
);
5270 ret
= mkdir(rundir
, S_IRWXU
);
5272 if (errno
!= EEXIST
) {
5273 ERR("Unable to create %s", rundir
);
5285 * Setup sockets and directory needed by the kconsumerd communication with the
5288 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
5292 char path
[PATH_MAX
];
5294 switch (consumer_data
->type
) {
5295 case LTTNG_CONSUMER_KERNEL
:
5296 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
5298 case LTTNG_CONSUMER64_UST
:
5299 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
5301 case LTTNG_CONSUMER32_UST
:
5302 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
5305 ERR("Consumer type unknown");
5310 DBG2("Creating consumer directory: %s", path
);
5312 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5314 if (errno
!= EEXIST
) {
5316 ERR("Failed to create %s", path
);
5322 ret
= chown(path
, 0, utils_get_group_id(tracing_group_name
));
5324 ERR("Unable to set group on %s", path
);
5330 /* Create the kconsumerd error unix socket */
5331 consumer_data
->err_sock
=
5332 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5333 if (consumer_data
->err_sock
< 0) {
5334 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5340 * Set the CLOEXEC flag. Return code is useless because either way, the
5343 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5345 PERROR("utils_set_fd_cloexec");
5346 /* continue anyway */
5349 /* File permission MUST be 660 */
5350 ret
= chmod(consumer_data
->err_unix_sock_path
,
5351 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5353 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5363 * Signal handler for the daemon
5365 * Simply stop all worker threads, leaving main() return gracefully after
5366 * joining all threads and calling cleanup().
5368 static void sighandler(int sig
)
5372 DBG("SIGINT caught");
5376 DBG("SIGTERM caught");
5380 CMM_STORE_SHARED(recv_child_signal
, 1);
5388 * Setup signal handler for :
5389 * SIGINT, SIGTERM, SIGPIPE
5391 static int set_signal_handler(void)
5394 struct sigaction sa
;
5397 if ((ret
= sigemptyset(&sigset
)) < 0) {
5398 PERROR("sigemptyset");
5402 sa
.sa_mask
= sigset
;
5405 sa
.sa_handler
= sighandler
;
5406 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5407 PERROR("sigaction");
5411 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5412 PERROR("sigaction");
5416 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5417 PERROR("sigaction");
5421 sa
.sa_handler
= SIG_IGN
;
5422 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5423 PERROR("sigaction");
5427 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5433 * Set open files limit to unlimited. This daemon can open a large number of
5434 * file descriptors in order to consume multiple kernel traces.
5436 static void set_ulimit(void)
5441 /* The kernel does not allow an infinite limit for open files */
5442 lim
.rlim_cur
= 65535;
5443 lim
.rlim_max
= 65535;
5445 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5447 PERROR("failed to set open files limit");
5452 * Write pidfile using the rundir and opt_pidfile.
5454 static int write_pidfile(void)
5457 char pidfile_path
[PATH_MAX
];
5462 if (lttng_strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
))) {
5467 /* Build pidfile path from rundir and opt_pidfile. */
5468 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
5469 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
5471 PERROR("snprintf pidfile path");
5477 * Create pid file in rundir.
5479 ret
= utils_create_pid_file(getpid(), pidfile_path
);
5485 * Create lockfile using the rundir and return its fd.
5487 static int create_lockfile(void)
5490 char lockfile_path
[PATH_MAX
];
5492 ret
= generate_lock_file_path(lockfile_path
, sizeof(lockfile_path
));
5497 ret
= utils_create_lock_file(lockfile_path
);
5503 * Write agent TCP port using the rundir.
5505 static int write_agent_port(void)
5508 char path
[PATH_MAX
];
5512 ret
= snprintf(path
, sizeof(path
), "%s/"
5513 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
, rundir
);
5515 PERROR("snprintf agent port path");
5520 * Create TCP agent port file in rundir.
5522 ret
= utils_create_pid_file(agent_tcp_port
, path
);
5528 static int set_clock_plugin_env(void)
5531 const char *original_env_value
;
5532 char *full_path
= NULL
;
5533 char *new_env_value
= NULL
;
5535 original_env_value
= getenv("LTTNG_UST_CLOCK_PLUGIN");
5536 if (!original_env_value
) {
5540 full_path
= utils_expand_path(original_env_value
);
5542 ERR("Failed to expand LTTNG_UST_CLOCK_PLUGIN path \"%s\"",
5543 original_env_value
);
5547 ret
= asprintf(&new_env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
5555 DBG("Updating environment: %s", new_env_value
);
5556 ret
= putenv(new_env_value
);
5558 free(new_env_value
);
5559 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5569 int main(int argc
, char **argv
)
5571 int ret
= 0, retval
= 0;
5573 const char *home_path
, *env_app_timeout
;
5575 init_kernel_workarounds();
5577 rcu_register_thread();
5579 if (set_signal_handler()) {
5581 goto exit_set_signal_handler
;
5584 setup_consumerd_path();
5586 page_size
= sysconf(_SC_PAGESIZE
);
5587 if (page_size
< 0) {
5588 PERROR("sysconf _SC_PAGESIZE");
5589 page_size
= LONG_MAX
;
5590 WARN("Fallback page size to %ld", page_size
);
5594 * Parse arguments and load the daemon configuration file.
5596 * We have an exit_options exit path to free memory reserved by
5597 * set_options. This is needed because the rest of sessiond_cleanup()
5598 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5599 * depends on set_options.
5602 if (set_options(argc
, argv
)) {
5607 ret
= set_clock_plugin_env();
5614 if (opt_daemon
|| opt_background
) {
5617 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5625 * We are in the child. Make sure all other file descriptors are
5626 * closed, in case we are called with more opened file
5627 * descriptors than the standard ones.
5629 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5634 if (run_as_create_worker(argv
[0]) < 0) {
5635 goto exit_create_run_as_worker_cleanup
;
5639 * Starting from here, we can create threads. This needs to be after
5640 * lttng_daemonize due to RCU.
5644 * Initialize the health check subsystem. This call should set the
5645 * appropriate time values.
5647 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5648 if (!health_sessiond
) {
5649 PERROR("health_app_create error");
5651 goto exit_health_sessiond_cleanup
;
5654 /* Create thread to clean up RCU hash tables */
5655 if (init_ht_cleanup_thread(&ht_cleanup_thread
)) {
5657 goto exit_ht_cleanup
;
5660 /* Create thread quit pipe */
5661 if (init_thread_quit_pipe()) {
5663 goto exit_init_data
;
5666 /* Check if daemon is UID = 0 */
5667 is_root
= !getuid();
5670 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
5673 goto exit_init_data
;
5676 /* Create global run dir with root access */
5677 if (create_lttng_rundir(rundir
)) {
5679 goto exit_init_data
;
5682 if (strlen(apps_unix_sock_path
) == 0) {
5683 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5684 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
5687 goto exit_init_data
;
5691 if (strlen(client_unix_sock_path
) == 0) {
5692 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5693 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
5696 goto exit_init_data
;
5700 /* Set global SHM for ust */
5701 if (strlen(wait_shm_path
) == 0) {
5702 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5703 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
5706 goto exit_init_data
;
5710 if (strlen(health_unix_sock_path
) == 0) {
5711 ret
= snprintf(health_unix_sock_path
,
5712 sizeof(health_unix_sock_path
),
5713 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
5716 goto exit_init_data
;
5720 /* Setup kernel consumerd path */
5721 ret
= snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
5722 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
5725 goto exit_init_data
;
5727 ret
= snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
5728 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
5731 goto exit_init_data
;
5734 DBG2("Kernel consumer err path: %s",
5735 kconsumer_data
.err_unix_sock_path
);
5736 DBG2("Kernel consumer cmd path: %s",
5737 kconsumer_data
.cmd_unix_sock_path
);
5739 home_path
= utils_get_home_dir();
5740 if (home_path
== NULL
) {
5741 /* TODO: Add --socket PATH option */
5742 ERR("Can't get HOME directory for sockets creation.");
5744 goto exit_init_data
;
5748 * Create rundir from home path. This will create something like
5751 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
5754 goto exit_init_data
;
5757 if (create_lttng_rundir(rundir
)) {
5759 goto exit_init_data
;
5762 if (strlen(apps_unix_sock_path
) == 0) {
5763 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5764 DEFAULT_HOME_APPS_UNIX_SOCK
,
5768 goto exit_init_data
;
5772 /* Set the cli tool unix socket path */
5773 if (strlen(client_unix_sock_path
) == 0) {
5774 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5775 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
5779 goto exit_init_data
;
5783 /* Set global SHM for ust */
5784 if (strlen(wait_shm_path
) == 0) {
5785 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5786 DEFAULT_HOME_APPS_WAIT_SHM_PATH
,
5790 goto exit_init_data
;
5794 /* Set health check Unix path */
5795 if (strlen(health_unix_sock_path
) == 0) {
5796 ret
= snprintf(health_unix_sock_path
,
5797 sizeof(health_unix_sock_path
),
5798 DEFAULT_HOME_HEALTH_UNIX_SOCK
,
5802 goto exit_init_data
;
5807 lockfile_fd
= create_lockfile();
5808 if (lockfile_fd
< 0) {
5810 goto exit_init_data
;
5813 /* Set consumer initial state */
5814 kernel_consumerd_state
= CONSUMER_STOPPED
;
5815 ust_consumerd_state
= CONSUMER_STOPPED
;
5817 DBG("Client socket path %s", client_unix_sock_path
);
5818 DBG("Application socket path %s", apps_unix_sock_path
);
5819 DBG("Application wait path %s", wait_shm_path
);
5820 DBG("LTTng run directory path: %s", rundir
);
5822 /* 32 bits consumerd path setup */
5823 ret
= snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
5824 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
5826 PERROR("snprintf 32-bit consumer error socket path");
5828 goto exit_init_data
;
5830 ret
= snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
5831 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
5833 PERROR("snprintf 32-bit consumer command socket path");
5835 goto exit_init_data
;
5838 DBG2("UST consumer 32 bits err path: %s",
5839 ustconsumer32_data
.err_unix_sock_path
);
5840 DBG2("UST consumer 32 bits cmd path: %s",
5841 ustconsumer32_data
.cmd_unix_sock_path
);
5843 /* 64 bits consumerd path setup */
5844 ret
= snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
5845 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
5847 PERROR("snprintf 64-bit consumer error socket path");
5849 goto exit_init_data
;
5851 ret
= snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
5852 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
5854 PERROR("snprintf 64-bit consumer command socket path");
5856 goto exit_init_data
;
5859 DBG2("UST consumer 64 bits err path: %s",
5860 ustconsumer64_data
.err_unix_sock_path
);
5861 DBG2("UST consumer 64 bits cmd path: %s",
5862 ustconsumer64_data
.cmd_unix_sock_path
);
5865 * See if daemon already exist.
5867 if (check_existing_daemon()) {
5868 ERR("Already running daemon.\n");
5870 * We do not goto exit because we must not cleanup()
5871 * because a daemon is already running.
5874 goto exit_init_data
;
5878 * Init UST app hash table. Alloc hash table before this point since
5879 * cleanup() can get called after that point.
5881 if (ust_app_ht_alloc()) {
5882 ERR("Failed to allocate UST app hash table");
5884 goto exit_init_data
;
5888 * Initialize agent app hash table. We allocate the hash table here
5889 * since cleanup() can get called after this point.
5891 if (agent_app_ht_alloc()) {
5892 ERR("Failed to allocate Agent app hash table");
5894 goto exit_init_data
;
5898 * These actions must be executed as root. We do that *after* setting up
5899 * the sockets path because we MUST make the check for another daemon using
5900 * those paths *before* trying to set the kernel consumer sockets and init
5904 if (set_consumer_sockets(&kconsumer_data
, rundir
)) {
5906 goto exit_init_data
;
5909 /* Setup kernel tracer */
5910 if (!opt_no_kernel
) {
5911 init_kernel_tracer();
5912 if (kernel_tracer_fd
>= 0) {
5913 ret
= syscall_init_table();
5915 ERR("Unable to populate syscall table. "
5916 "Syscall tracing won't work "
5917 "for this session daemon.");
5922 /* Set ulimit for open files */
5925 /* init lttng_fd tracking must be done after set_ulimit. */
5928 if (set_consumer_sockets(&ustconsumer64_data
, rundir
)) {
5930 goto exit_init_data
;
5933 if (set_consumer_sockets(&ustconsumer32_data
, rundir
)) {
5935 goto exit_init_data
;
5938 /* Setup the needed unix socket */
5939 if (init_daemon_socket()) {
5941 goto exit_init_data
;
5944 /* Set credentials to socket */
5945 if (is_root
&& set_permissions(rundir
)) {
5947 goto exit_init_data
;
5950 /* Get parent pid if -S, --sig-parent is specified. */
5951 if (opt_sig_parent
) {
5955 /* Setup the kernel pipe for waking up the kernel thread */
5956 if (is_root
&& !opt_no_kernel
) {
5957 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
5959 goto exit_init_data
;
5963 /* Setup the thread apps communication pipe. */
5964 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
5966 goto exit_init_data
;
5969 /* Setup the thread apps notify communication pipe. */
5970 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
5972 goto exit_init_data
;
5975 /* Initialize global buffer per UID and PID registry. */
5976 buffer_reg_init_uid_registry();
5977 buffer_reg_init_pid_registry();
5979 /* Init UST command queue. */
5980 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
5983 * Get session list pointer. This pointer MUST NOT be free'd. This list
5984 * is statically declared in session.c
5986 session_list_ptr
= session_get_list();
5990 /* Check for the application socket timeout env variable. */
5991 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
5992 if (env_app_timeout
) {
5993 app_socket_timeout
= atoi(env_app_timeout
);
5995 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
5998 ret
= write_pidfile();
6000 ERR("Error in write_pidfile");
6002 goto exit_init_data
;
6004 ret
= write_agent_port();
6006 ERR("Error in write_agent_port");
6008 goto exit_init_data
;
6011 /* Initialize communication library */
6013 /* Initialize TCP timeout values */
6014 lttcomm_inet_init();
6016 if (load_session_init_data(&load_info
) < 0) {
6018 goto exit_init_data
;
6020 load_info
->path
= opt_load_session_path
;
6022 /* Create health-check thread */
6023 ret
= pthread_create(&health_thread
, default_pthread_attr(),
6024 thread_manage_health
, (void *) NULL
);
6027 PERROR("pthread_create health");
6032 /* Create thread to manage the client socket */
6033 ret
= pthread_create(&client_thread
, default_pthread_attr(),
6034 thread_manage_clients
, (void *) NULL
);
6037 PERROR("pthread_create clients");
6042 /* Create thread to dispatch registration */
6043 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
6044 thread_dispatch_ust_registration
, (void *) NULL
);
6047 PERROR("pthread_create dispatch");
6052 /* Create thread to manage application registration. */
6053 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
6054 thread_registration_apps
, (void *) NULL
);
6057 PERROR("pthread_create registration");
6062 /* Create thread to manage application socket */
6063 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
6064 thread_manage_apps
, (void *) NULL
);
6067 PERROR("pthread_create apps");
6072 /* Create thread to manage application notify socket */
6073 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
6074 ust_thread_manage_notify
, (void *) NULL
);
6077 PERROR("pthread_create notify");
6079 goto exit_apps_notify
;
6082 /* Create agent registration thread. */
6083 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
6084 agent_thread_manage_registration
, (void *) NULL
);
6087 PERROR("pthread_create agent");
6089 goto exit_agent_reg
;
6092 /* Don't start this thread if kernel tracing is not requested nor root */
6093 if (is_root
&& !opt_no_kernel
) {
6094 /* Create kernel thread to manage kernel event */
6095 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
6096 thread_manage_kernel
, (void *) NULL
);
6099 PERROR("pthread_create kernel");
6105 /* Create session loading thread. */
6106 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
6107 thread_load_session
, load_info
);
6110 PERROR("pthread_create load_session_thread");
6112 goto exit_load_session
;
6116 * This is where we start awaiting program completion (e.g. through
6117 * signal that asks threads to teardown).
6120 ret
= pthread_join(load_session_thread
, &status
);
6123 PERROR("pthread_join load_session_thread");
6128 if (is_root
&& !opt_no_kernel
) {
6129 ret
= pthread_join(kernel_thread
, &status
);
6132 PERROR("pthread_join");
6138 ret
= pthread_join(agent_reg_thread
, &status
);
6141 PERROR("pthread_join agent");
6146 ret
= pthread_join(apps_notify_thread
, &status
);
6149 PERROR("pthread_join apps notify");
6154 ret
= pthread_join(apps_thread
, &status
);
6157 PERROR("pthread_join apps");
6162 ret
= pthread_join(reg_apps_thread
, &status
);
6165 PERROR("pthread_join");
6171 * Join dispatch thread after joining reg_apps_thread to ensure
6172 * we don't leak applications in the queue.
6174 ret
= pthread_join(dispatch_thread
, &status
);
6177 PERROR("pthread_join");
6182 ret
= pthread_join(client_thread
, &status
);
6185 PERROR("pthread_join");
6190 ret
= pthread_join(health_thread
, &status
);
6193 PERROR("pthread_join health thread");
6200 * Wait for all pending call_rcu work to complete before tearing
6201 * down data structures. call_rcu worker may be trying to
6202 * perform lookups in those structures.
6206 * sessiond_cleanup() is called when no other thread is running, except
6207 * the ht_cleanup thread, which is needed to destroy the hash tables.
6209 rcu_thread_online();
6211 rcu_thread_offline();
6212 rcu_unregister_thread();
6215 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6216 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6217 * the queue is empty before shutting down the clean-up thread.
6221 ret
= fini_ht_cleanup_thread(&ht_cleanup_thread
);
6227 health_app_destroy(health_sessiond
);
6228 exit_health_sessiond_cleanup
:
6229 exit_create_run_as_worker_cleanup
:
6232 sessiond_cleanup_options();
6234 exit_set_signal_handler
: