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
)
568 if (!WIFEXITED(status
)) {
569 ERR("consumerd termination with error: %d",
572 consumer_data
->pid
= 0;
576 * Cleanup the session daemon's data structures.
578 static void sessiond_cleanup(void)
581 struct ltt_session
*sess
, *stmp
;
584 DBG("Cleanup sessiond");
587 * Close the thread quit pipe. It has already done its job,
588 * since we are now called.
590 utils_close_pipe(thread_quit_pipe
);
593 * If opt_pidfile is undefined, the default file will be wiped when
594 * removing the rundir.
597 ret
= remove(opt_pidfile
);
599 PERROR("remove pidfile %s", opt_pidfile
);
603 DBG("Removing sessiond and consumerd content of directory %s", rundir
);
606 snprintf(path
, PATH_MAX
,
608 rundir
, DEFAULT_LTTNG_SESSIOND_PIDFILE
);
609 DBG("Removing %s", path
);
612 snprintf(path
, PATH_MAX
, "%s/%s", rundir
,
613 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
);
614 DBG("Removing %s", path
);
618 snprintf(path
, PATH_MAX
,
619 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
621 DBG("Removing %s", path
);
624 snprintf(path
, PATH_MAX
,
625 DEFAULT_KCONSUMERD_PATH
,
627 DBG("Removing directory %s", path
);
630 /* ust consumerd 32 */
631 snprintf(path
, PATH_MAX
,
632 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
634 DBG("Removing %s", path
);
637 snprintf(path
, PATH_MAX
,
638 DEFAULT_USTCONSUMERD32_PATH
,
640 DBG("Removing directory %s", path
);
643 /* ust consumerd 64 */
644 snprintf(path
, PATH_MAX
,
645 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
647 DBG("Removing %s", path
);
650 snprintf(path
, PATH_MAX
,
651 DEFAULT_USTCONSUMERD64_PATH
,
653 DBG("Removing directory %s", path
);
656 DBG("Cleaning up all sessions");
658 /* Destroy session list mutex */
659 if (session_list_ptr
!= NULL
) {
660 pthread_mutex_destroy(&session_list_ptr
->lock
);
662 /* Cleanup ALL session */
663 cds_list_for_each_entry_safe(sess
, stmp
,
664 &session_list_ptr
->head
, list
) {
665 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
669 wait_consumer(&kconsumer_data
);
670 wait_consumer(&ustconsumer64_data
);
671 wait_consumer(&ustconsumer32_data
);
673 DBG("Cleaning up all agent apps");
674 agent_app_ht_clean();
676 DBG("Closing all UST sockets");
677 ust_app_clean_list();
678 buffer_reg_destroy_registries();
680 if (is_root
&& !opt_no_kernel
) {
681 DBG2("Closing kernel fd");
682 if (kernel_tracer_fd
>= 0) {
683 ret
= close(kernel_tracer_fd
);
688 DBG("Unloading kernel modules");
689 modprobe_remove_lttng_all();
693 close_consumer_sockets();
696 load_session_destroy_data(load_info
);
701 * Cleanup lock file by deleting it and finaly closing it which will
702 * release the file system lock.
704 if (lockfile_fd
>= 0) {
705 char lockfile_path
[PATH_MAX
];
707 ret
= generate_lock_file_path(lockfile_path
,
708 sizeof(lockfile_path
));
710 ret
= remove(lockfile_path
);
712 PERROR("remove lock file");
714 ret
= close(lockfile_fd
);
716 PERROR("close lock file");
722 * We do NOT rmdir rundir because there are other processes
723 * using it, for instance lttng-relayd, which can start in
724 * parallel with this teardown.
731 * Cleanup the daemon's option data structures.
733 static void sessiond_cleanup_options(void)
735 DBG("Cleaning up options");
738 * If the override option is set, the pointer points to a *non* const
739 * thus freeing it even though the variable type is set to const.
741 if (tracing_group_name_override
) {
742 free((void *) tracing_group_name
);
744 if (consumerd32_bin_override
) {
745 free((void *) consumerd32_bin
);
747 if (consumerd64_bin_override
) {
748 free((void *) consumerd64_bin
);
750 if (consumerd32_libdir_override
) {
751 free((void *) consumerd32_libdir
);
753 if (consumerd64_libdir_override
) {
754 free((void *) consumerd64_libdir
);
758 free(opt_load_session_path
);
759 free(kmod_probes_list
);
760 free(kmod_extra_probes_list
);
762 run_as_destroy_worker();
766 * Send data on a unix socket using the liblttsessiondcomm API.
768 * Return lttcomm error code.
770 static int send_unix_sock(int sock
, void *buf
, size_t len
)
772 /* Check valid length */
777 return lttcomm_send_unix_sock(sock
, buf
, len
);
781 * Free memory of a command context structure.
783 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
785 DBG("Clean command context structure");
787 if ((*cmd_ctx
)->llm
) {
788 free((*cmd_ctx
)->llm
);
790 if ((*cmd_ctx
)->lsm
) {
791 free((*cmd_ctx
)->lsm
);
799 * Notify UST applications using the shm mmap futex.
801 static int notify_ust_apps(int active
)
805 DBG("Notifying applications of session daemon state: %d", active
);
807 /* See shm.c for this call implying mmap, shm and futex calls */
808 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
809 if (wait_shm_mmap
== NULL
) {
813 /* Wake waiting process */
814 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
816 /* Apps notified successfully */
824 * Setup the outgoing data buffer for the response (llm) by allocating the
825 * right amount of memory and copying the original information from the lsm
828 * Return 0 on success, negative value on error.
830 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
831 const void *payload_buf
, size_t payload_len
,
832 const void *cmd_header_buf
, size_t cmd_header_len
)
835 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
836 const size_t cmd_header_offset
= header_len
;
837 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
838 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
840 cmd_ctx
->llm
= zmalloc(total_msg_size
);
842 if (cmd_ctx
->llm
== NULL
) {
848 /* Copy common data */
849 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
850 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
851 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
852 cmd_ctx
->llm
->data_size
= payload_len
;
853 cmd_ctx
->lttng_msg_size
= total_msg_size
;
855 /* Copy command header */
856 if (cmd_header_len
) {
857 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
863 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
872 * Version of setup_lttng_msg() without command header.
874 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
875 void *payload_buf
, size_t payload_len
)
877 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
880 * Update the kernel poll set of all channel fd available over all tracing
881 * session. Add the wakeup pipe at the end of the set.
883 static int update_kernel_poll(struct lttng_poll_event
*events
)
886 struct ltt_session
*session
;
887 struct ltt_kernel_channel
*channel
;
889 DBG("Updating kernel poll set");
892 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
893 session_lock(session
);
894 if (session
->kernel_session
== NULL
) {
895 session_unlock(session
);
899 cds_list_for_each_entry(channel
,
900 &session
->kernel_session
->channel_list
.head
, list
) {
901 /* Add channel fd to the kernel poll set */
902 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
904 session_unlock(session
);
907 DBG("Channel fd %d added to kernel set", channel
->fd
);
909 session_unlock(session
);
911 session_unlock_list();
916 session_unlock_list();
921 * Find the channel fd from 'fd' over all tracing session. When found, check
922 * for new channel stream and send those stream fds to the kernel consumer.
924 * Useful for CPU hotplug feature.
926 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
929 struct ltt_session
*session
;
930 struct ltt_kernel_session
*ksess
;
931 struct ltt_kernel_channel
*channel
;
933 DBG("Updating kernel streams for channel fd %d", fd
);
936 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
937 session_lock(session
);
938 if (session
->kernel_session
== NULL
) {
939 session_unlock(session
);
942 ksess
= session
->kernel_session
;
944 cds_list_for_each_entry(channel
,
945 &ksess
->channel_list
.head
, list
) {
946 struct lttng_ht_iter iter
;
947 struct consumer_socket
*socket
;
949 if (channel
->fd
!= fd
) {
952 DBG("Channel found, updating kernel streams");
953 ret
= kernel_open_channel_stream(channel
);
957 /* Update the stream global counter */
958 ksess
->stream_count_global
+= ret
;
961 * Have we already sent fds to the consumer? If yes, it
962 * means that tracing is started so it is safe to send
963 * our updated stream fds.
965 if (ksess
->consumer_fds_sent
!= 1
966 || ksess
->consumer
== NULL
) {
972 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
973 &iter
.iter
, socket
, node
.node
) {
974 pthread_mutex_lock(socket
->lock
);
975 ret
= kernel_consumer_send_channel_stream(socket
,
977 session
->output_traces
? 1 : 0);
978 pthread_mutex_unlock(socket
->lock
);
986 session_unlock(session
);
988 session_unlock_list();
992 session_unlock(session
);
993 session_unlock_list();
998 * For each tracing session, update newly registered apps. The session list
999 * lock MUST be acquired before calling this.
1001 static void update_ust_app(int app_sock
)
1003 struct ltt_session
*sess
, *stmp
;
1005 /* Consumer is in an ERROR state. Stop any application update. */
1006 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
1007 /* Stop the update process since the consumer is dead. */
1011 /* For all tracing session(s) */
1012 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
1013 struct ust_app
*app
;
1016 if (!sess
->ust_session
) {
1017 goto unlock_session
;
1021 assert(app_sock
>= 0);
1022 app
= ust_app_find_by_sock(app_sock
);
1025 * Application can be unregistered before so
1026 * this is possible hence simply stopping the
1029 DBG3("UST app update failed to find app sock %d",
1033 ust_app_global_update(sess
->ust_session
, app
);
1037 session_unlock(sess
);
1042 * This thread manage event coming from the kernel.
1044 * Features supported in this thread:
1047 static void *thread_manage_kernel(void *data
)
1049 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
1050 uint32_t revents
, nb_fd
;
1052 struct lttng_poll_event events
;
1054 DBG("[thread] Thread manage kernel started");
1056 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
1059 * This first step of the while is to clean this structure which could free
1060 * non NULL pointers so initialize it before the loop.
1062 lttng_poll_init(&events
);
1064 if (testpoint(sessiond_thread_manage_kernel
)) {
1065 goto error_testpoint
;
1068 health_code_update();
1070 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
1071 goto error_testpoint
;
1075 health_code_update();
1077 if (update_poll_flag
== 1) {
1078 /* Clean events object. We are about to populate it again. */
1079 lttng_poll_clean(&events
);
1081 ret
= sessiond_set_thread_pollset(&events
, 2);
1083 goto error_poll_create
;
1086 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
1091 /* This will add the available kernel channel if any. */
1092 ret
= update_kernel_poll(&events
);
1096 update_poll_flag
= 0;
1099 DBG("Thread kernel polling");
1101 /* Poll infinite value of time */
1103 health_poll_entry();
1104 ret
= lttng_poll_wait(&events
, -1);
1105 DBG("Thread kernel return from poll on %d fds",
1106 LTTNG_POLL_GETNB(&events
));
1110 * Restart interrupted system call.
1112 if (errno
== EINTR
) {
1116 } else if (ret
== 0) {
1117 /* Should not happen since timeout is infinite */
1118 ERR("Return value of poll is 0 with an infinite timeout.\n"
1119 "This should not have happened! Continuing...");
1125 for (i
= 0; i
< nb_fd
; i
++) {
1126 /* Fetch once the poll data */
1127 revents
= LTTNG_POLL_GETEV(&events
, i
);
1128 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1130 health_code_update();
1133 /* No activity for this FD (poll implementation). */
1137 /* Thread quit pipe has been closed. Killing thread. */
1138 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1144 /* Check for data on kernel pipe */
1145 if (revents
& LPOLLIN
) {
1146 if (pollfd
== kernel_poll_pipe
[0]) {
1147 (void) lttng_read(kernel_poll_pipe
[0],
1150 * Ret value is useless here, if this pipe gets any actions an
1151 * update is required anyway.
1153 update_poll_flag
= 1;
1157 * New CPU detected by the kernel. Adding kernel stream to
1158 * kernel session and updating the kernel consumer
1160 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1166 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1167 update_poll_flag
= 1;
1170 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1178 lttng_poll_clean(&events
);
1181 utils_close_pipe(kernel_poll_pipe
);
1182 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1185 ERR("Health error occurred in %s", __func__
);
1186 WARN("Kernel thread died unexpectedly. "
1187 "Kernel tracing can continue but CPU hotplug is disabled.");
1189 health_unregister(health_sessiond
);
1190 DBG("Kernel thread dying");
1195 * Signal pthread condition of the consumer data that the thread.
1197 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1199 pthread_mutex_lock(&data
->cond_mutex
);
1202 * The state is set before signaling. It can be any value, it's the waiter
1203 * job to correctly interpret this condition variable associated to the
1204 * consumer pthread_cond.
1206 * A value of 0 means that the corresponding thread of the consumer data
1207 * was not started. 1 indicates that the thread has started and is ready
1208 * for action. A negative value means that there was an error during the
1211 data
->consumer_thread_is_ready
= state
;
1212 (void) pthread_cond_signal(&data
->cond
);
1214 pthread_mutex_unlock(&data
->cond_mutex
);
1218 * This thread manage the consumer error sent back to the session daemon.
1220 static void *thread_manage_consumer(void *data
)
1222 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1223 uint32_t revents
, nb_fd
;
1224 enum lttcomm_return_code code
;
1225 struct lttng_poll_event events
;
1226 struct consumer_data
*consumer_data
= data
;
1228 DBG("[thread] Manage consumer started");
1230 rcu_register_thread();
1231 rcu_thread_online();
1233 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1235 health_code_update();
1238 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1239 * metadata_sock. Nothing more will be added to this poll set.
1241 ret
= sessiond_set_thread_pollset(&events
, 3);
1247 * The error socket here is already in a listening state which was done
1248 * just before spawning this thread to avoid a race between the consumer
1249 * daemon exec trying to connect and the listen() call.
1251 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1256 health_code_update();
1258 /* Infinite blocking call, waiting for transmission */
1260 health_poll_entry();
1262 if (testpoint(sessiond_thread_manage_consumer
)) {
1266 ret
= lttng_poll_wait(&events
, -1);
1270 * Restart interrupted system call.
1272 if (errno
== EINTR
) {
1280 for (i
= 0; i
< nb_fd
; i
++) {
1281 /* Fetch once the poll data */
1282 revents
= LTTNG_POLL_GETEV(&events
, i
);
1283 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1285 health_code_update();
1288 /* No activity for this FD (poll implementation). */
1292 /* Thread quit pipe has been closed. Killing thread. */
1293 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1299 /* Event on the registration socket */
1300 if (pollfd
== consumer_data
->err_sock
) {
1301 if (revents
& LPOLLIN
) {
1303 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1304 ERR("consumer err socket poll error");
1307 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1313 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1319 * Set the CLOEXEC flag. Return code is useless because either way, the
1322 (void) utils_set_fd_cloexec(sock
);
1324 health_code_update();
1326 DBG2("Receiving code from consumer err_sock");
1328 /* Getting status code from kconsumerd */
1329 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1330 sizeof(enum lttcomm_return_code
));
1335 health_code_update();
1336 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1337 /* Connect both socket, command and metadata. */
1338 consumer_data
->cmd_sock
=
1339 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1340 consumer_data
->metadata_fd
=
1341 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1342 if (consumer_data
->cmd_sock
< 0
1343 || consumer_data
->metadata_fd
< 0) {
1344 PERROR("consumer connect cmd socket");
1345 /* On error, signal condition and quit. */
1346 signal_consumer_condition(consumer_data
, -1);
1349 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1350 /* Create metadata socket lock. */
1351 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1352 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1353 PERROR("zmalloc pthread mutex");
1356 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1358 signal_consumer_condition(consumer_data
, 1);
1359 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1360 DBG("Consumer metadata socket ready (fd: %d)",
1361 consumer_data
->metadata_fd
);
1363 ERR("consumer error when waiting for SOCK_READY : %s",
1364 lttcomm_get_readable_code(-code
));
1368 /* Remove the consumerd error sock since we've established a connexion */
1369 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1374 /* Add new accepted error socket. */
1375 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1380 /* Add metadata socket that is successfully connected. */
1381 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1382 LPOLLIN
| LPOLLRDHUP
);
1387 health_code_update();
1389 /* Infinite blocking call, waiting for transmission */
1392 health_code_update();
1394 /* Exit the thread because the thread quit pipe has been triggered. */
1396 /* Not a health error. */
1401 health_poll_entry();
1402 ret
= lttng_poll_wait(&events
, -1);
1406 * Restart interrupted system call.
1408 if (errno
== EINTR
) {
1416 for (i
= 0; i
< nb_fd
; i
++) {
1417 /* Fetch once the poll data */
1418 revents
= LTTNG_POLL_GETEV(&events
, i
);
1419 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1421 health_code_update();
1424 /* No activity for this FD (poll implementation). */
1429 * Thread quit pipe has been triggered, flag that we should stop
1430 * but continue the current loop to handle potential data from
1433 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1435 if (pollfd
== sock
) {
1436 /* Event on the consumerd socket */
1437 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1438 && !(revents
& LPOLLIN
)) {
1439 ERR("consumer err socket second poll error");
1442 health_code_update();
1443 /* Wait for any kconsumerd error */
1444 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1445 sizeof(enum lttcomm_return_code
));
1447 ERR("consumer closed the command socket");
1451 ERR("consumer return code : %s",
1452 lttcomm_get_readable_code(-code
));
1455 } else if (pollfd
== consumer_data
->metadata_fd
) {
1456 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1457 && !(revents
& LPOLLIN
)) {
1458 ERR("consumer err metadata socket second poll error");
1461 /* UST metadata requests */
1462 ret
= ust_consumer_metadata_request(
1463 &consumer_data
->metadata_sock
);
1465 ERR("Handling metadata request");
1469 /* No need for an else branch all FDs are tested prior. */
1471 health_code_update();
1477 * We lock here because we are about to close the sockets and some other
1478 * thread might be using them so get exclusive access which will abort all
1479 * other consumer command by other threads.
1481 pthread_mutex_lock(&consumer_data
->lock
);
1483 /* Immediately set the consumerd state to stopped */
1484 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1485 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1486 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1487 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1488 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1490 /* Code flow error... */
1494 if (consumer_data
->err_sock
>= 0) {
1495 ret
= close(consumer_data
->err_sock
);
1499 consumer_data
->err_sock
= -1;
1501 if (consumer_data
->cmd_sock
>= 0) {
1502 ret
= close(consumer_data
->cmd_sock
);
1506 consumer_data
->cmd_sock
= -1;
1508 if (consumer_data
->metadata_sock
.fd_ptr
&&
1509 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1510 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1522 unlink(consumer_data
->err_unix_sock_path
);
1523 unlink(consumer_data
->cmd_unix_sock_path
);
1524 pthread_mutex_unlock(&consumer_data
->lock
);
1526 /* Cleanup metadata socket mutex. */
1527 if (consumer_data
->metadata_sock
.lock
) {
1528 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1529 free(consumer_data
->metadata_sock
.lock
);
1531 lttng_poll_clean(&events
);
1535 ERR("Health error occurred in %s", __func__
);
1537 health_unregister(health_sessiond
);
1538 DBG("consumer thread cleanup completed");
1540 rcu_thread_offline();
1541 rcu_unregister_thread();
1547 * This thread manage application communication.
1549 static void *thread_manage_apps(void *data
)
1551 int i
, ret
, pollfd
, err
= -1;
1553 uint32_t revents
, nb_fd
;
1554 struct lttng_poll_event events
;
1556 DBG("[thread] Manage application started");
1558 rcu_register_thread();
1559 rcu_thread_online();
1561 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1563 if (testpoint(sessiond_thread_manage_apps
)) {
1564 goto error_testpoint
;
1567 health_code_update();
1569 ret
= sessiond_set_thread_pollset(&events
, 2);
1571 goto error_poll_create
;
1574 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1579 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1583 health_code_update();
1586 DBG("Apps thread polling");
1588 /* Inifinite blocking call, waiting for transmission */
1590 health_poll_entry();
1591 ret
= lttng_poll_wait(&events
, -1);
1592 DBG("Apps thread return from poll on %d fds",
1593 LTTNG_POLL_GETNB(&events
));
1597 * Restart interrupted system call.
1599 if (errno
== EINTR
) {
1607 for (i
= 0; i
< nb_fd
; i
++) {
1608 /* Fetch once the poll data */
1609 revents
= LTTNG_POLL_GETEV(&events
, i
);
1610 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1612 health_code_update();
1615 /* No activity for this FD (poll implementation). */
1619 /* Thread quit pipe has been closed. Killing thread. */
1620 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1626 /* Inspect the apps cmd pipe */
1627 if (pollfd
== apps_cmd_pipe
[0]) {
1628 if (revents
& LPOLLIN
) {
1632 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1633 if (size_ret
< sizeof(sock
)) {
1634 PERROR("read apps cmd pipe");
1638 health_code_update();
1641 * Since this is a command socket (write then read),
1642 * we only monitor the error events of the socket.
1644 ret
= lttng_poll_add(&events
, sock
,
1645 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1650 DBG("Apps with sock %d added to poll set", sock
);
1651 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1652 ERR("Apps command pipe error");
1655 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1660 * At this point, we know that a registered application made
1661 * the event at poll_wait.
1663 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1664 /* Removing from the poll set */
1665 ret
= lttng_poll_del(&events
, pollfd
);
1670 /* Socket closed on remote end. */
1671 ust_app_unregister(pollfd
);
1673 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1678 health_code_update();
1684 lttng_poll_clean(&events
);
1687 utils_close_pipe(apps_cmd_pipe
);
1688 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1691 * We don't clean the UST app hash table here since already registered
1692 * applications can still be controlled so let them be until the session
1693 * daemon dies or the applications stop.
1698 ERR("Health error occurred in %s", __func__
);
1700 health_unregister(health_sessiond
);
1701 DBG("Application communication apps thread cleanup complete");
1702 rcu_thread_offline();
1703 rcu_unregister_thread();
1708 * Send a socket to a thread This is called from the dispatch UST registration
1709 * thread once all sockets are set for the application.
1711 * The sock value can be invalid, we don't really care, the thread will handle
1712 * it and make the necessary cleanup if so.
1714 * On success, return 0 else a negative value being the errno message of the
1717 static int send_socket_to_thread(int fd
, int sock
)
1722 * It's possible that the FD is set as invalid with -1 concurrently just
1723 * before calling this function being a shutdown state of the thread.
1730 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1731 if (ret
< sizeof(sock
)) {
1732 PERROR("write apps pipe %d", fd
);
1739 /* All good. Don't send back the write positive ret value. */
1746 * Sanitize the wait queue of the dispatch registration thread meaning removing
1747 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1748 * notify socket is never received.
1750 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1752 int ret
, nb_fd
= 0, i
;
1753 unsigned int fd_added
= 0;
1754 struct lttng_poll_event events
;
1755 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1759 lttng_poll_init(&events
);
1761 /* Just skip everything for an empty queue. */
1762 if (!wait_queue
->count
) {
1766 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1771 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1772 &wait_queue
->head
, head
) {
1773 assert(wait_node
->app
);
1774 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1775 LPOLLHUP
| LPOLLERR
);
1788 * Poll but don't block so we can quickly identify the faulty events and
1789 * clean them afterwards from the wait queue.
1791 ret
= lttng_poll_wait(&events
, 0);
1797 for (i
= 0; i
< nb_fd
; i
++) {
1798 /* Get faulty FD. */
1799 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1800 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1803 /* No activity for this FD (poll implementation). */
1807 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1808 &wait_queue
->head
, head
) {
1809 if (pollfd
== wait_node
->app
->sock
&&
1810 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1811 cds_list_del(&wait_node
->head
);
1812 wait_queue
->count
--;
1813 ust_app_destroy(wait_node
->app
);
1816 * Silence warning of use-after-free in
1817 * cds_list_for_each_entry_safe which uses
1818 * __typeof__(*wait_node).
1823 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1830 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1834 lttng_poll_clean(&events
);
1838 lttng_poll_clean(&events
);
1840 ERR("Unable to sanitize wait queue");
1845 * Dispatch request from the registration threads to the application
1846 * communication thread.
1848 static void *thread_dispatch_ust_registration(void *data
)
1851 struct cds_wfcq_node
*node
;
1852 struct ust_command
*ust_cmd
= NULL
;
1853 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1854 struct ust_reg_wait_queue wait_queue
= {
1858 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1860 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1861 goto error_testpoint
;
1864 health_code_update();
1866 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1868 DBG("[thread] Dispatch UST command started");
1870 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1871 health_code_update();
1873 /* Atomically prepare the queue futex */
1874 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1877 struct ust_app
*app
= NULL
;
1881 * Make sure we don't have node(s) that have hung up before receiving
1882 * the notify socket. This is to clean the list in order to avoid
1883 * memory leaks from notify socket that are never seen.
1885 sanitize_wait_queue(&wait_queue
);
1887 health_code_update();
1888 /* Dequeue command for registration */
1889 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1891 DBG("Woken up but nothing in the UST command queue");
1892 /* Continue thread execution */
1896 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1898 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1899 " gid:%d sock:%d name:%s (version %d.%d)",
1900 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1901 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1902 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1903 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1905 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1906 wait_node
= zmalloc(sizeof(*wait_node
));
1908 PERROR("zmalloc wait_node dispatch");
1909 ret
= close(ust_cmd
->sock
);
1911 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1913 lttng_fd_put(LTTNG_FD_APPS
, 1);
1917 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1919 /* Create application object if socket is CMD. */
1920 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1922 if (!wait_node
->app
) {
1923 ret
= close(ust_cmd
->sock
);
1925 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1927 lttng_fd_put(LTTNG_FD_APPS
, 1);
1933 * Add application to the wait queue so we can set the notify
1934 * socket before putting this object in the global ht.
1936 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1941 * We have to continue here since we don't have the notify
1942 * socket and the application MUST be added to the hash table
1943 * only at that moment.
1948 * Look for the application in the local wait queue and set the
1949 * notify socket if found.
1951 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1952 &wait_queue
.head
, head
) {
1953 health_code_update();
1954 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1955 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1956 cds_list_del(&wait_node
->head
);
1958 app
= wait_node
->app
;
1960 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1966 * With no application at this stage the received socket is
1967 * basically useless so close it before we free the cmd data
1968 * structure for good.
1971 ret
= close(ust_cmd
->sock
);
1973 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1975 lttng_fd_put(LTTNG_FD_APPS
, 1);
1982 * @session_lock_list
1984 * Lock the global session list so from the register up to the
1985 * registration done message, no thread can see the application
1986 * and change its state.
1988 session_lock_list();
1992 * Add application to the global hash table. This needs to be
1993 * done before the update to the UST registry can locate the
1998 /* Set app version. This call will print an error if needed. */
1999 (void) ust_app_version(app
);
2001 /* Send notify socket through the notify pipe. */
2002 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
2006 session_unlock_list();
2008 * No notify thread, stop the UST tracing. However, this is
2009 * not an internal error of the this thread thus setting
2010 * the health error code to a normal exit.
2017 * Update newly registered application with the tracing
2018 * registry info already enabled information.
2020 update_ust_app(app
->sock
);
2023 * Don't care about return value. Let the manage apps threads
2024 * handle app unregistration upon socket close.
2026 (void) ust_app_register_done(app
);
2029 * Even if the application socket has been closed, send the app
2030 * to the thread and unregistration will take place at that
2033 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
2036 session_unlock_list();
2038 * No apps. thread, stop the UST tracing. However, this is
2039 * not an internal error of the this thread thus setting
2040 * the health error code to a normal exit.
2047 session_unlock_list();
2049 } while (node
!= NULL
);
2051 health_poll_entry();
2052 /* Futex wait on queue. Blocking call on futex() */
2053 futex_nto1_wait(&ust_cmd_queue
.futex
);
2056 /* Normal exit, no error */
2060 /* Clean up wait queue. */
2061 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2062 &wait_queue
.head
, head
) {
2063 cds_list_del(&wait_node
->head
);
2068 /* Empty command queue. */
2070 /* Dequeue command for registration */
2071 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2075 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2076 ret
= close(ust_cmd
->sock
);
2078 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2080 lttng_fd_put(LTTNG_FD_APPS
, 1);
2085 DBG("Dispatch thread dying");
2088 ERR("Health error occurred in %s", __func__
);
2090 health_unregister(health_sessiond
);
2095 * This thread manage application registration.
2097 static void *thread_registration_apps(void *data
)
2099 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2100 uint32_t revents
, nb_fd
;
2101 struct lttng_poll_event events
;
2103 * Get allocated in this thread, enqueued to a global queue, dequeued and
2104 * freed in the manage apps thread.
2106 struct ust_command
*ust_cmd
= NULL
;
2108 DBG("[thread] Manage application registration started");
2110 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2112 if (testpoint(sessiond_thread_registration_apps
)) {
2113 goto error_testpoint
;
2116 ret
= lttcomm_listen_unix_sock(apps_sock
);
2122 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2123 * more will be added to this poll set.
2125 ret
= sessiond_set_thread_pollset(&events
, 2);
2127 goto error_create_poll
;
2130 /* Add the application registration socket */
2131 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2133 goto error_poll_add
;
2136 /* Notify all applications to register */
2137 ret
= notify_ust_apps(1);
2139 ERR("Failed to notify applications or create the wait shared memory.\n"
2140 "Execution continues but there might be problem for already\n"
2141 "running applications that wishes to register.");
2145 DBG("Accepting application registration");
2147 /* Inifinite blocking call, waiting for transmission */
2149 health_poll_entry();
2150 ret
= lttng_poll_wait(&events
, -1);
2154 * Restart interrupted system call.
2156 if (errno
== EINTR
) {
2164 for (i
= 0; i
< nb_fd
; i
++) {
2165 health_code_update();
2167 /* Fetch once the poll data */
2168 revents
= LTTNG_POLL_GETEV(&events
, i
);
2169 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2172 /* No activity for this FD (poll implementation). */
2176 /* Thread quit pipe has been closed. Killing thread. */
2177 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2183 /* Event on the registration socket */
2184 if (pollfd
== apps_sock
) {
2185 if (revents
& LPOLLIN
) {
2186 sock
= lttcomm_accept_unix_sock(apps_sock
);
2192 * Set socket timeout for both receiving and ending.
2193 * app_socket_timeout is in seconds, whereas
2194 * lttcomm_setsockopt_rcv_timeout and
2195 * lttcomm_setsockopt_snd_timeout expect msec as
2198 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2199 app_socket_timeout
* 1000);
2200 (void) lttcomm_setsockopt_snd_timeout(sock
,
2201 app_socket_timeout
* 1000);
2204 * Set the CLOEXEC flag. Return code is useless because
2205 * either way, the show must go on.
2207 (void) utils_set_fd_cloexec(sock
);
2209 /* Create UST registration command for enqueuing */
2210 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2211 if (ust_cmd
== NULL
) {
2212 PERROR("ust command zmalloc");
2221 * Using message-based transmissions to ensure we don't
2222 * have to deal with partially received messages.
2224 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2226 ERR("Exhausted file descriptors allowed for applications.");
2236 health_code_update();
2237 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2240 /* Close socket of the application. */
2245 lttng_fd_put(LTTNG_FD_APPS
, 1);
2249 health_code_update();
2251 ust_cmd
->sock
= sock
;
2254 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2255 " gid:%d sock:%d name:%s (version %d.%d)",
2256 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2257 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2258 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2259 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2262 * Lock free enqueue the registration request. The red pill
2263 * has been taken! This apps will be part of the *system*.
2265 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2268 * Wake the registration queue futex. Implicit memory
2269 * barrier with the exchange in cds_wfcq_enqueue.
2271 futex_nto1_wake(&ust_cmd_queue
.futex
);
2272 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2273 ERR("Register apps socket poll error");
2276 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2285 /* Notify that the registration thread is gone */
2288 if (apps_sock
>= 0) {
2289 ret
= close(apps_sock
);
2299 lttng_fd_put(LTTNG_FD_APPS
, 1);
2301 unlink(apps_unix_sock_path
);
2304 lttng_poll_clean(&events
);
2308 DBG("UST Registration thread cleanup complete");
2311 ERR("Health error occurred in %s", __func__
);
2313 health_unregister(health_sessiond
);
2319 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2320 * exec or it will fails.
2322 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2325 struct timespec timeout
;
2328 * Make sure we set the readiness flag to 0 because we are NOT ready.
2329 * This access to consumer_thread_is_ready does not need to be
2330 * protected by consumer_data.cond_mutex (yet) since the consumer
2331 * management thread has not been started at this point.
2333 consumer_data
->consumer_thread_is_ready
= 0;
2335 /* Setup pthread condition */
2336 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2339 PERROR("pthread_condattr_init consumer data");
2344 * Set the monotonic clock in order to make sure we DO NOT jump in time
2345 * between the clock_gettime() call and the timedwait call. See bug #324
2346 * for a more details and how we noticed it.
2348 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2351 PERROR("pthread_condattr_setclock consumer data");
2355 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2358 PERROR("pthread_cond_init consumer data");
2362 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2363 thread_manage_consumer
, consumer_data
);
2366 PERROR("pthread_create consumer");
2371 /* We are about to wait on a pthread condition */
2372 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2374 /* Get time for sem_timedwait absolute timeout */
2375 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2377 * Set the timeout for the condition timed wait even if the clock gettime
2378 * call fails since we might loop on that call and we want to avoid to
2379 * increment the timeout too many times.
2381 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2384 * The following loop COULD be skipped in some conditions so this is why we
2385 * set ret to 0 in order to make sure at least one round of the loop is
2391 * Loop until the condition is reached or when a timeout is reached. Note
2392 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2393 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2394 * possible. This loop does not take any chances and works with both of
2397 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2398 if (clock_ret
< 0) {
2399 PERROR("clock_gettime spawn consumer");
2400 /* Infinite wait for the consumerd thread to be ready */
2401 ret
= pthread_cond_wait(&consumer_data
->cond
,
2402 &consumer_data
->cond_mutex
);
2404 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2405 &consumer_data
->cond_mutex
, &timeout
);
2409 /* Release the pthread condition */
2410 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2414 if (ret
== ETIMEDOUT
) {
2418 * Call has timed out so we kill the kconsumerd_thread and return
2421 ERR("Condition timed out. The consumer thread was never ready."
2423 pth_ret
= pthread_cancel(consumer_data
->thread
);
2425 PERROR("pthread_cancel consumer thread");
2428 PERROR("pthread_cond_wait failed consumer thread");
2430 /* Caller is expecting a negative value on failure. */
2435 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2436 if (consumer_data
->pid
== 0) {
2437 ERR("Consumerd did not start");
2438 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2441 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2450 * Join consumer thread
2452 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2456 /* Consumer pid must be a real one. */
2457 if (consumer_data
->pid
> 0) {
2459 ret
= kill(consumer_data
->pid
, SIGTERM
);
2461 PERROR("Error killing consumer daemon");
2464 return pthread_join(consumer_data
->thread
, &status
);
2471 * Fork and exec a consumer daemon (consumerd).
2473 * Return pid if successful else -1.
2475 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2479 const char *consumer_to_use
;
2480 const char *verbosity
;
2483 DBG("Spawning consumerd");
2490 if (opt_verbose_consumer
) {
2491 verbosity
= "--verbose";
2492 } else if (lttng_opt_quiet
) {
2493 verbosity
= "--quiet";
2498 switch (consumer_data
->type
) {
2499 case LTTNG_CONSUMER_KERNEL
:
2501 * Find out which consumerd to execute. We will first try the
2502 * 64-bit path, then the sessiond's installation directory, and
2503 * fallback on the 32-bit one,
2505 DBG3("Looking for a kernel consumer at these locations:");
2506 DBG3(" 1) %s", consumerd64_bin
);
2507 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2508 DBG3(" 3) %s", consumerd32_bin
);
2509 if (stat(consumerd64_bin
, &st
) == 0) {
2510 DBG3("Found location #1");
2511 consumer_to_use
= consumerd64_bin
;
2512 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2513 DBG3("Found location #2");
2514 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2515 } else if (stat(consumerd32_bin
, &st
) == 0) {
2516 DBG3("Found location #3");
2517 consumer_to_use
= consumerd32_bin
;
2519 DBG("Could not find any valid consumerd executable");
2523 DBG("Using kernel consumer at: %s", consumer_to_use
);
2524 ret
= execl(consumer_to_use
,
2525 "lttng-consumerd", verbosity
, "-k",
2526 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2527 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2528 "--group", tracing_group_name
,
2531 case LTTNG_CONSUMER64_UST
:
2533 char *tmpnew
= NULL
;
2535 if (consumerd64_libdir
[0] != '\0') {
2539 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2543 tmplen
= strlen("LD_LIBRARY_PATH=")
2544 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2545 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2550 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2551 strcat(tmpnew
, consumerd64_libdir
);
2552 if (tmp
[0] != '\0') {
2553 strcat(tmpnew
, ":");
2554 strcat(tmpnew
, tmp
);
2556 ret
= putenv(tmpnew
);
2563 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2564 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2565 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2566 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2567 "--group", tracing_group_name
,
2569 if (consumerd64_libdir
[0] != '\0') {
2574 case LTTNG_CONSUMER32_UST
:
2576 char *tmpnew
= NULL
;
2578 if (consumerd32_libdir
[0] != '\0') {
2582 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2586 tmplen
= strlen("LD_LIBRARY_PATH=")
2587 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2588 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2593 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2594 strcat(tmpnew
, consumerd32_libdir
);
2595 if (tmp
[0] != '\0') {
2596 strcat(tmpnew
, ":");
2597 strcat(tmpnew
, tmp
);
2599 ret
= putenv(tmpnew
);
2606 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2607 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2608 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2609 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2610 "--group", tracing_group_name
,
2612 if (consumerd32_libdir
[0] != '\0') {
2618 PERROR("unknown consumer type");
2622 PERROR("Consumer execl()");
2624 /* Reaching this point, we got a failure on our execl(). */
2626 } else if (pid
> 0) {
2629 PERROR("start consumer fork");
2637 * Spawn the consumerd daemon and session daemon thread.
2639 static int start_consumerd(struct consumer_data
*consumer_data
)
2644 * Set the listen() state on the socket since there is a possible race
2645 * between the exec() of the consumer daemon and this call if place in the
2646 * consumer thread. See bug #366 for more details.
2648 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2653 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2654 if (consumer_data
->pid
!= 0) {
2655 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2659 ret
= spawn_consumerd(consumer_data
);
2661 ERR("Spawning consumerd failed");
2662 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2666 /* Setting up the consumer_data pid */
2667 consumer_data
->pid
= ret
;
2668 DBG2("Consumer pid %d", consumer_data
->pid
);
2669 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2671 DBG2("Spawning consumer control thread");
2672 ret
= spawn_consumer_thread(consumer_data
);
2674 ERR("Fatal error spawning consumer control thread");
2682 /* Cleanup already created sockets on error. */
2683 if (consumer_data
->err_sock
>= 0) {
2686 err
= close(consumer_data
->err_sock
);
2688 PERROR("close consumer data error socket");
2695 * Setup necessary data for kernel tracer action.
2697 static int init_kernel_tracer(void)
2701 /* Modprobe lttng kernel modules */
2702 ret
= modprobe_lttng_control();
2707 /* Open debugfs lttng */
2708 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2709 if (kernel_tracer_fd
< 0) {
2710 DBG("Failed to open %s", module_proc_lttng
);
2715 /* Validate kernel version */
2716 ret
= kernel_validate_version(kernel_tracer_fd
);
2721 ret
= modprobe_lttng_data();
2726 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2730 modprobe_remove_lttng_control();
2731 ret
= close(kernel_tracer_fd
);
2735 kernel_tracer_fd
= -1;
2736 return LTTNG_ERR_KERN_VERSION
;
2739 ret
= close(kernel_tracer_fd
);
2745 modprobe_remove_lttng_control();
2748 WARN("No kernel tracer available");
2749 kernel_tracer_fd
= -1;
2751 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2753 return LTTNG_ERR_KERN_NA
;
2759 * Copy consumer output from the tracing session to the domain session. The
2760 * function also applies the right modification on a per domain basis for the
2761 * trace files destination directory.
2763 * Should *NOT* be called with RCU read-side lock held.
2765 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2768 const char *dir_name
;
2769 struct consumer_output
*consumer
;
2772 assert(session
->consumer
);
2775 case LTTNG_DOMAIN_KERNEL
:
2776 DBG3("Copying tracing session consumer output in kernel session");
2778 * XXX: We should audit the session creation and what this function
2779 * does "extra" in order to avoid a destroy since this function is used
2780 * in the domain session creation (kernel and ust) only. Same for UST
2783 if (session
->kernel_session
->consumer
) {
2784 consumer_output_put(session
->kernel_session
->consumer
);
2786 session
->kernel_session
->consumer
=
2787 consumer_copy_output(session
->consumer
);
2788 /* Ease our life a bit for the next part */
2789 consumer
= session
->kernel_session
->consumer
;
2790 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2792 case LTTNG_DOMAIN_JUL
:
2793 case LTTNG_DOMAIN_LOG4J
:
2794 case LTTNG_DOMAIN_PYTHON
:
2795 case LTTNG_DOMAIN_UST
:
2796 DBG3("Copying tracing session consumer output in UST session");
2797 if (session
->ust_session
->consumer
) {
2798 consumer_output_put(session
->ust_session
->consumer
);
2800 session
->ust_session
->consumer
=
2801 consumer_copy_output(session
->consumer
);
2802 /* Ease our life a bit for the next part */
2803 consumer
= session
->ust_session
->consumer
;
2804 dir_name
= DEFAULT_UST_TRACE_DIR
;
2807 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2811 /* Append correct directory to subdir */
2812 strncat(consumer
->subdir
, dir_name
,
2813 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2814 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2823 * Create an UST session and add it to the session ust list.
2825 * Should *NOT* be called with RCU read-side lock held.
2827 static int create_ust_session(struct ltt_session
*session
,
2828 struct lttng_domain
*domain
)
2831 struct ltt_ust_session
*lus
= NULL
;
2835 assert(session
->consumer
);
2837 switch (domain
->type
) {
2838 case LTTNG_DOMAIN_JUL
:
2839 case LTTNG_DOMAIN_LOG4J
:
2840 case LTTNG_DOMAIN_PYTHON
:
2841 case LTTNG_DOMAIN_UST
:
2844 ERR("Unknown UST domain on create session %d", domain
->type
);
2845 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2849 DBG("Creating UST session");
2851 lus
= trace_ust_create_session(session
->id
);
2853 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2857 lus
->uid
= session
->uid
;
2858 lus
->gid
= session
->gid
;
2859 lus
->output_traces
= session
->output_traces
;
2860 lus
->snapshot_mode
= session
->snapshot_mode
;
2861 lus
->live_timer_interval
= session
->live_timer
;
2862 session
->ust_session
= lus
;
2863 if (session
->shm_path
[0]) {
2864 strncpy(lus
->root_shm_path
, session
->shm_path
,
2865 sizeof(lus
->root_shm_path
));
2866 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2867 strncpy(lus
->shm_path
, session
->shm_path
,
2868 sizeof(lus
->shm_path
));
2869 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2870 strncat(lus
->shm_path
, "/ust",
2871 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2873 /* Copy session output to the newly created UST session */
2874 ret
= copy_session_consumer(domain
->type
, session
);
2875 if (ret
!= LTTNG_OK
) {
2883 session
->ust_session
= NULL
;
2888 * Create a kernel tracer session then create the default channel.
2890 static int create_kernel_session(struct ltt_session
*session
)
2894 DBG("Creating kernel session");
2896 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2898 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2902 /* Code flow safety */
2903 assert(session
->kernel_session
);
2905 /* Copy session output to the newly created Kernel session */
2906 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2907 if (ret
!= LTTNG_OK
) {
2911 /* Create directory(ies) on local filesystem. */
2912 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2913 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2914 ret
= run_as_mkdir_recursive(
2915 session
->kernel_session
->consumer
->dst
.trace_path
,
2916 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2918 if (errno
!= EEXIST
) {
2919 ERR("Trace directory creation error");
2925 session
->kernel_session
->uid
= session
->uid
;
2926 session
->kernel_session
->gid
= session
->gid
;
2927 session
->kernel_session
->output_traces
= session
->output_traces
;
2928 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2933 trace_kernel_destroy_session(session
->kernel_session
);
2934 session
->kernel_session
= NULL
;
2939 * Count number of session permitted by uid/gid.
2941 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2944 struct ltt_session
*session
;
2946 DBG("Counting number of available session for UID %d GID %d",
2948 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2950 * Only list the sessions the user can control.
2952 if (!session_access_ok(session
, uid
, gid
)) {
2961 * Process the command requested by the lttng client within the command
2962 * context structure. This function make sure that the return structure (llm)
2963 * is set and ready for transmission before returning.
2965 * Return any error encountered or 0 for success.
2967 * "sock" is only used for special-case var. len data.
2969 * Should *NOT* be called with RCU read-side lock held.
2971 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2975 int need_tracing_session
= 1;
2978 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2980 assert(!rcu_read_ongoing());
2984 switch (cmd_ctx
->lsm
->cmd_type
) {
2985 case LTTNG_CREATE_SESSION
:
2986 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2987 case LTTNG_CREATE_SESSION_LIVE
:
2988 case LTTNG_DESTROY_SESSION
:
2989 case LTTNG_LIST_SESSIONS
:
2990 case LTTNG_LIST_DOMAINS
:
2991 case LTTNG_START_TRACE
:
2992 case LTTNG_STOP_TRACE
:
2993 case LTTNG_DATA_PENDING
:
2994 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2995 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2996 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2997 case LTTNG_SNAPSHOT_RECORD
:
2998 case LTTNG_SAVE_SESSION
:
2999 case LTTNG_SET_SESSION_SHM_PATH
:
3000 case LTTNG_REGENERATE_METADATA
:
3001 case LTTNG_REGENERATE_STATEDUMP
:
3008 if (opt_no_kernel
&& need_domain
3009 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
3011 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3013 ret
= LTTNG_ERR_KERN_NA
;
3018 /* Deny register consumer if we already have a spawned consumer. */
3019 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
3020 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3021 if (kconsumer_data
.pid
> 0) {
3022 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3023 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3026 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3030 * Check for command that don't needs to allocate a returned payload. We do
3031 * this here so we don't have to make the call for no payload at each
3034 switch(cmd_ctx
->lsm
->cmd_type
) {
3035 case LTTNG_LIST_SESSIONS
:
3036 case LTTNG_LIST_TRACEPOINTS
:
3037 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3038 case LTTNG_LIST_DOMAINS
:
3039 case LTTNG_LIST_CHANNELS
:
3040 case LTTNG_LIST_EVENTS
:
3041 case LTTNG_LIST_SYSCALLS
:
3042 case LTTNG_LIST_TRACKER_PIDS
:
3043 case LTTNG_DATA_PENDING
:
3046 /* Setup lttng message with no payload */
3047 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
3049 /* This label does not try to unlock the session */
3050 goto init_setup_error
;
3054 /* Commands that DO NOT need a session. */
3055 switch (cmd_ctx
->lsm
->cmd_type
) {
3056 case LTTNG_CREATE_SESSION
:
3057 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3058 case LTTNG_CREATE_SESSION_LIVE
:
3059 case LTTNG_CALIBRATE
:
3060 case LTTNG_LIST_SESSIONS
:
3061 case LTTNG_LIST_TRACEPOINTS
:
3062 case LTTNG_LIST_SYSCALLS
:
3063 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3064 case LTTNG_SAVE_SESSION
:
3065 need_tracing_session
= 0;
3068 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3070 * We keep the session list lock across _all_ commands
3071 * for now, because the per-session lock does not
3072 * handle teardown properly.
3074 session_lock_list();
3075 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3076 if (cmd_ctx
->session
== NULL
) {
3077 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3080 /* Acquire lock for the session */
3081 session_lock(cmd_ctx
->session
);
3087 * Commands that need a valid session but should NOT create one if none
3088 * exists. Instead of creating one and destroying it when the command is
3089 * handled, process that right before so we save some round trip in useless
3092 switch (cmd_ctx
->lsm
->cmd_type
) {
3093 case LTTNG_DISABLE_CHANNEL
:
3094 case LTTNG_DISABLE_EVENT
:
3095 switch (cmd_ctx
->lsm
->domain
.type
) {
3096 case LTTNG_DOMAIN_KERNEL
:
3097 if (!cmd_ctx
->session
->kernel_session
) {
3098 ret
= LTTNG_ERR_NO_CHANNEL
;
3102 case LTTNG_DOMAIN_JUL
:
3103 case LTTNG_DOMAIN_LOG4J
:
3104 case LTTNG_DOMAIN_PYTHON
:
3105 case LTTNG_DOMAIN_UST
:
3106 if (!cmd_ctx
->session
->ust_session
) {
3107 ret
= LTTNG_ERR_NO_CHANNEL
;
3112 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3124 * Check domain type for specific "pre-action".
3126 switch (cmd_ctx
->lsm
->domain
.type
) {
3127 case LTTNG_DOMAIN_KERNEL
:
3129 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3133 /* Kernel tracer check */
3134 if (kernel_tracer_fd
== -1) {
3135 /* Basically, load kernel tracer modules */
3136 ret
= init_kernel_tracer();
3142 /* Consumer is in an ERROR state. Report back to client */
3143 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3144 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3148 /* Need a session for kernel command */
3149 if (need_tracing_session
) {
3150 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3151 ret
= create_kernel_session(cmd_ctx
->session
);
3153 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3158 /* Start the kernel consumer daemon */
3159 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3160 if (kconsumer_data
.pid
== 0 &&
3161 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3162 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3163 ret
= start_consumerd(&kconsumer_data
);
3165 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3168 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3170 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3174 * The consumer was just spawned so we need to add the socket to
3175 * the consumer output of the session if exist.
3177 ret
= consumer_create_socket(&kconsumer_data
,
3178 cmd_ctx
->session
->kernel_session
->consumer
);
3185 case LTTNG_DOMAIN_JUL
:
3186 case LTTNG_DOMAIN_LOG4J
:
3187 case LTTNG_DOMAIN_PYTHON
:
3188 case LTTNG_DOMAIN_UST
:
3190 if (!ust_app_supported()) {
3191 ret
= LTTNG_ERR_NO_UST
;
3194 /* Consumer is in an ERROR state. Report back to client */
3195 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3196 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3200 if (need_tracing_session
) {
3201 /* Create UST session if none exist. */
3202 if (cmd_ctx
->session
->ust_session
== NULL
) {
3203 ret
= create_ust_session(cmd_ctx
->session
,
3204 &cmd_ctx
->lsm
->domain
);
3205 if (ret
!= LTTNG_OK
) {
3210 /* Start the UST consumer daemons */
3212 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3213 if (consumerd64_bin
[0] != '\0' &&
3214 ustconsumer64_data
.pid
== 0 &&
3215 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3216 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3217 ret
= start_consumerd(&ustconsumer64_data
);
3219 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3220 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3224 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3225 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3227 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3231 * Setup socket for consumer 64 bit. No need for atomic