2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 #include <sys/mount.h>
34 #include <sys/resource.h>
35 #include <sys/socket.h>
37 #include <sys/types.h>
39 #include <urcu/uatomic.h>
43 #include <common/common.h>
44 #include <common/compat/socket.h>
45 #include <common/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/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 #define CONSUMERD_FILE "lttng-consumerd"
79 static const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
80 static int tracing_group_name_override
;
81 static char *opt_pidfile
;
82 static int opt_sig_parent
;
83 static int opt_verbose_consumer
;
84 static int opt_daemon
, opt_background
;
85 static int opt_no_kernel
;
86 static char *opt_load_session_path
;
87 static pid_t ppid
; /* Parent PID for --sig-parent option */
88 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
90 static int lockfile_fd
= -1;
92 /* Set to 1 when a SIGUSR1 signal is received. */
93 static int recv_child_signal
;
96 * Consumer daemon specific control data. Every value not initialized here is
97 * set to 0 by the static definition.
99 static struct consumer_data kconsumer_data
= {
100 .type
= LTTNG_CONSUMER_KERNEL
,
101 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
102 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
105 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
106 .lock
= PTHREAD_MUTEX_INITIALIZER
,
107 .cond
= PTHREAD_COND_INITIALIZER
,
108 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
110 static struct consumer_data ustconsumer64_data
= {
111 .type
= LTTNG_CONSUMER64_UST
,
112 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
113 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
116 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
117 .lock
= PTHREAD_MUTEX_INITIALIZER
,
118 .cond
= PTHREAD_COND_INITIALIZER
,
119 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
121 static struct consumer_data ustconsumer32_data
= {
122 .type
= LTTNG_CONSUMER32_UST
,
123 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
124 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
127 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
128 .lock
= PTHREAD_MUTEX_INITIALIZER
,
129 .cond
= PTHREAD_COND_INITIALIZER
,
130 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
133 /* Command line options */
134 static const struct option long_options
[] = {
135 { "client-sock", 1, 0, 'c' },
136 { "apps-sock", 1, 0, 'a' },
137 { "kconsumerd-cmd-sock", 1, 0, 'C' },
138 { "kconsumerd-err-sock", 1, 0, 'E' },
139 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
140 { "ustconsumerd32-err-sock", 1, 0, 'H' },
141 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
142 { "ustconsumerd64-err-sock", 1, 0, 'F' },
143 { "consumerd32-path", 1, 0, 'u' },
144 { "consumerd32-libdir", 1, 0, 'U' },
145 { "consumerd64-path", 1, 0, 't' },
146 { "consumerd64-libdir", 1, 0, 'T' },
147 { "daemonize", 0, 0, 'd' },
148 { "background", 0, 0, 'b' },
149 { "sig-parent", 0, 0, 'S' },
150 { "help", 0, 0, 'h' },
151 { "group", 1, 0, 'g' },
152 { "version", 0, 0, 'V' },
153 { "quiet", 0, 0, 'q' },
154 { "verbose", 0, 0, 'v' },
155 { "verbose-consumer", 0, 0, 'Z' },
156 { "no-kernel", 0, 0, 'N' },
157 { "pidfile", 1, 0, 'p' },
158 { "agent-tcp-port", 1, 0, 'J' },
159 { "config", 1, 0, 'f' },
160 { "load", 1, 0, 'l' },
161 { "kmod-probes", 1, 0, 'P' },
162 { "extra-kmod-probes", 1, 0, 'e' },
166 /* Command line options to ignore from configuration file */
167 static const char *config_ignore_options
[] = { "help", "version", "config" };
169 /* Shared between threads */
170 static int dispatch_thread_exit
;
172 /* Global application Unix socket path */
173 static char apps_unix_sock_path
[PATH_MAX
];
174 /* Global client Unix socket path */
175 static char client_unix_sock_path
[PATH_MAX
];
176 /* global wait shm path for UST */
177 static char wait_shm_path
[PATH_MAX
];
178 /* Global health check unix path */
179 static char health_unix_sock_path
[PATH_MAX
];
181 /* Sockets and FDs */
182 static int client_sock
= -1;
183 static int apps_sock
= -1;
184 int kernel_tracer_fd
= -1;
185 static int kernel_poll_pipe
[2] = { -1, -1 };
188 * Quit pipe for all threads. This permits a single cancellation point
189 * for all threads when receiving an event on the pipe.
191 static int thread_quit_pipe
[2] = { -1, -1 };
194 * This pipe is used to inform the thread managing application communication
195 * that a command is queued and ready to be processed.
197 static int apps_cmd_pipe
[2] = { -1, -1 };
199 int apps_cmd_notify_pipe
[2] = { -1, -1 };
201 /* Pthread, Mutexes and Semaphores */
202 static pthread_t apps_thread
;
203 static pthread_t apps_notify_thread
;
204 static pthread_t reg_apps_thread
;
205 static pthread_t client_thread
;
206 static pthread_t kernel_thread
;
207 static pthread_t dispatch_thread
;
208 static pthread_t health_thread
;
209 static pthread_t ht_cleanup_thread
;
210 static pthread_t agent_reg_thread
;
211 static pthread_t load_session_thread
;
214 * UST registration command queue. This queue is tied with a futex and uses a N
215 * wakers / 1 waiter implemented and detailed in futex.c/.h
217 * The thread_registration_apps and thread_dispatch_ust_registration uses this
218 * queue along with the wait/wake scheme. The thread_manage_apps receives down
219 * the line new application socket and monitors it for any I/O error or clean
220 * close that triggers an unregistration of the application.
222 static struct ust_cmd_queue ust_cmd_queue
;
225 * Pointer initialized before thread creation.
227 * This points to the tracing session list containing the session count and a
228 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
229 * MUST NOT be taken if you call a public function in session.c.
231 * The lock is nested inside the structure: session_list_ptr->lock. Please use
232 * session_lock_list and session_unlock_list for lock acquisition.
234 static struct ltt_session_list
*session_list_ptr
;
236 int ust_consumerd64_fd
= -1;
237 int ust_consumerd32_fd
= -1;
239 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
240 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
241 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
242 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
243 static int consumerd32_bin_override
;
244 static int consumerd64_bin_override
;
245 static int consumerd32_libdir_override
;
246 static int consumerd64_libdir_override
;
248 static const char *module_proc_lttng
= "/proc/lttng";
251 * Consumer daemon state which is changed when spawning it, killing it or in
252 * case of a fatal error.
254 enum consumerd_state
{
255 CONSUMER_STARTED
= 1,
256 CONSUMER_STOPPED
= 2,
261 * This consumer daemon state is used to validate if a client command will be
262 * able to reach the consumer. If not, the client is informed. For instance,
263 * doing a "lttng start" when the consumer state is set to ERROR will return an
264 * error to the client.
266 * The following example shows a possible race condition of this scheme:
268 * consumer thread error happens
270 * client cmd checks state -> still OK
271 * consumer thread exit, sets error
272 * client cmd try to talk to consumer
275 * However, since the consumer is a different daemon, we have no way of making
276 * sure the command will reach it safely even with this state flag. This is why
277 * we consider that up to the state validation during command processing, the
278 * command is safe. After that, we can not guarantee the correctness of the
279 * client request vis-a-vis the consumer.
281 static enum consumerd_state ust_consumerd_state
;
282 static enum consumerd_state kernel_consumerd_state
;
285 * Socket timeout for receiving and sending in seconds.
287 static int app_socket_timeout
;
289 /* Set in main() with the current page size. */
292 /* Application health monitoring */
293 struct health_app
*health_sessiond
;
295 /* Agent TCP port for registration. Used by the agent thread. */
296 unsigned int agent_tcp_port
= DEFAULT_AGENT_TCP_PORT
;
298 /* Am I root or not. */
299 int is_root
; /* Set to 1 if the daemon is running as root */
301 const char * const config_section_name
= "sessiond";
303 /* Load session thread information to operate. */
304 struct load_session_thread_data
*load_info
;
307 * Whether sessiond is ready for commands/health check requests.
308 * NR_LTTNG_SESSIOND_READY must match the number of calls to
309 * sessiond_notify_ready().
311 #define NR_LTTNG_SESSIOND_READY 3
312 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_READY
;
314 /* Notify parents that we are ready for cmd and health check */
316 void sessiond_notify_ready(void)
318 if (uatomic_sub_return(<tng_sessiond_ready
, 1) == 0) {
320 * Notify parent pid that we are ready to accept command
321 * for client side. This ppid is the one from the
322 * external process that spawned us.
324 if (opt_sig_parent
) {
329 * Notify the parent of the fork() process that we are
332 if (opt_daemon
|| opt_background
) {
333 kill(child_ppid
, SIGUSR1
);
339 void setup_consumerd_path(void)
341 const char *bin
, *libdir
;
344 * Allow INSTALL_BIN_PATH to be used as a target path for the
345 * native architecture size consumer if CONFIG_CONSUMER*_PATH
346 * has not been defined.
348 #if (CAA_BITS_PER_LONG == 32)
349 if (!consumerd32_bin
[0]) {
350 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
352 if (!consumerd32_libdir
[0]) {
353 consumerd32_libdir
= INSTALL_LIB_PATH
;
355 #elif (CAA_BITS_PER_LONG == 64)
356 if (!consumerd64_bin
[0]) {
357 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
359 if (!consumerd64_libdir
[0]) {
360 consumerd64_libdir
= INSTALL_LIB_PATH
;
363 #error "Unknown bitness"
367 * runtime env. var. overrides the build default.
369 bin
= getenv("LTTNG_CONSUMERD32_BIN");
371 consumerd32_bin
= bin
;
373 bin
= getenv("LTTNG_CONSUMERD64_BIN");
375 consumerd64_bin
= bin
;
377 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
379 consumerd32_libdir
= libdir
;
381 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
383 consumerd64_libdir
= libdir
;
388 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
390 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
396 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
402 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
414 * Check if the thread quit pipe was triggered.
416 * Return 1 if it was triggered else 0;
418 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
420 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
428 * Init thread quit pipe.
430 * Return -1 on error or 0 if all pipes are created.
432 static int init_thread_quit_pipe(void)
436 ret
= pipe(thread_quit_pipe
);
438 PERROR("thread quit pipe");
442 for (i
= 0; i
< 2; i
++) {
443 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
455 * Stop all threads by closing the thread quit pipe.
457 static void stop_threads(void)
461 /* Stopping all threads */
462 DBG("Terminating all threads");
463 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
465 ERR("write error on thread quit pipe");
468 /* Dispatch thread */
469 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
470 futex_nto1_wake(&ust_cmd_queue
.futex
);
474 * Close every consumer sockets.
476 static void close_consumer_sockets(void)
480 if (kconsumer_data
.err_sock
>= 0) {
481 ret
= close(kconsumer_data
.err_sock
);
483 PERROR("kernel consumer err_sock close");
486 if (ustconsumer32_data
.err_sock
>= 0) {
487 ret
= close(ustconsumer32_data
.err_sock
);
489 PERROR("UST consumerd32 err_sock close");
492 if (ustconsumer64_data
.err_sock
>= 0) {
493 ret
= close(ustconsumer64_data
.err_sock
);
495 PERROR("UST consumerd64 err_sock close");
498 if (kconsumer_data
.cmd_sock
>= 0) {
499 ret
= close(kconsumer_data
.cmd_sock
);
501 PERROR("kernel consumer cmd_sock close");
504 if (ustconsumer32_data
.cmd_sock
>= 0) {
505 ret
= close(ustconsumer32_data
.cmd_sock
);
507 PERROR("UST consumerd32 cmd_sock close");
510 if (ustconsumer64_data
.cmd_sock
>= 0) {
511 ret
= close(ustconsumer64_data
.cmd_sock
);
513 PERROR("UST consumerd64 cmd_sock close");
519 * Generate the full lock file path using the rundir.
521 * Return the snprintf() return value thus a negative value is an error.
523 static int generate_lock_file_path(char *path
, size_t len
)
530 /* Build lockfile path from rundir. */
531 ret
= snprintf(path
, len
, "%s/" DEFAULT_LTTNG_SESSIOND_LOCKFILE
, rundir
);
533 PERROR("snprintf lockfile path");
542 static void cleanup(void)
545 struct ltt_session
*sess
, *stmp
;
551 * Close the thread quit pipe. It has already done its job,
552 * since we are now called.
554 utils_close_pipe(thread_quit_pipe
);
557 * If opt_pidfile is undefined, the default file will be wiped when
558 * removing the rundir.
561 ret
= remove(opt_pidfile
);
563 PERROR("remove pidfile %s", opt_pidfile
);
567 DBG("Removing sessiond and consumerd content of directory %s", rundir
);
570 snprintf(path
, PATH_MAX
,
572 rundir
, DEFAULT_LTTNG_SESSIOND_PIDFILE
);
573 DBG("Removing %s", path
);
576 snprintf(path
, PATH_MAX
, "%s/%s", rundir
,
577 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
);
578 DBG("Removing %s", path
);
582 snprintf(path
, PATH_MAX
,
583 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
585 DBG("Removing %s", path
);
588 snprintf(path
, PATH_MAX
,
589 DEFAULT_KCONSUMERD_PATH
,
591 DBG("Removing directory %s", path
);
594 /* ust consumerd 32 */
595 snprintf(path
, PATH_MAX
,
596 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
598 DBG("Removing %s", path
);
601 snprintf(path
, PATH_MAX
,
602 DEFAULT_USTCONSUMERD32_PATH
,
604 DBG("Removing directory %s", path
);
607 /* ust consumerd 64 */
608 snprintf(path
, PATH_MAX
,
609 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
611 DBG("Removing %s", path
);
614 snprintf(path
, PATH_MAX
,
615 DEFAULT_USTCONSUMERD64_PATH
,
617 DBG("Removing directory %s", path
);
620 DBG("Cleaning up all sessions");
622 /* Destroy session list mutex */
623 if (session_list_ptr
!= NULL
) {
624 pthread_mutex_destroy(&session_list_ptr
->lock
);
626 /* Cleanup ALL session */
627 cds_list_for_each_entry_safe(sess
, stmp
,
628 &session_list_ptr
->head
, list
) {
629 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
633 DBG("Closing all UST sockets");
634 ust_app_clean_list();
635 buffer_reg_destroy_registries();
637 if (is_root
&& !opt_no_kernel
) {
638 DBG2("Closing kernel fd");
639 if (kernel_tracer_fd
>= 0) {
640 ret
= close(kernel_tracer_fd
);
645 DBG("Unloading kernel modules");
646 modprobe_remove_lttng_all();
650 close_consumer_sockets();
653 * If the override option is set, the pointer points to a *non* const thus
654 * freeing it even though the variable type is set to const.
656 if (tracing_group_name_override
) {
657 free((void *) tracing_group_name
);
659 if (consumerd32_bin_override
) {
660 free((void *) consumerd32_bin
);
662 if (consumerd64_bin_override
) {
663 free((void *) consumerd64_bin
);
665 if (consumerd32_libdir_override
) {
666 free((void *) consumerd32_libdir
);
668 if (consumerd64_libdir_override
) {
669 free((void *) consumerd64_libdir
);
676 if (opt_load_session_path
) {
677 free(opt_load_session_path
);
681 load_session_destroy_data(load_info
);
686 * Cleanup lock file by deleting it and finaly closing it which will
687 * release the file system lock.
689 if (lockfile_fd
>= 0) {
690 char lockfile_path
[PATH_MAX
];
692 ret
= generate_lock_file_path(lockfile_path
, sizeof(lockfile_path
));
694 ret
= remove(lockfile_path
);
696 PERROR("remove lock file");
698 ret
= close(lockfile_fd
);
700 PERROR("close lock file");
706 * We do NOT rmdir rundir because there are other processes
707 * using it, for instance lttng-relayd, which can start in
708 * parallel with this teardown.
714 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
715 "Matthew, BEET driven development works!%c[%dm",
716 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
721 * Send data on a unix socket using the liblttsessiondcomm API.
723 * Return lttcomm error code.
725 static int send_unix_sock(int sock
, void *buf
, size_t len
)
727 /* Check valid length */
732 return lttcomm_send_unix_sock(sock
, buf
, len
);
736 * Free memory of a command context structure.
738 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
740 DBG("Clean command context structure");
742 if ((*cmd_ctx
)->llm
) {
743 free((*cmd_ctx
)->llm
);
745 if ((*cmd_ctx
)->lsm
) {
746 free((*cmd_ctx
)->lsm
);
754 * Notify UST applications using the shm mmap futex.
756 static int notify_ust_apps(int active
)
760 DBG("Notifying applications of session daemon state: %d", active
);
762 /* See shm.c for this call implying mmap, shm and futex calls */
763 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
764 if (wait_shm_mmap
== NULL
) {
768 /* Wake waiting process */
769 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
771 /* Apps notified successfully */
779 * Setup the outgoing data buffer for the response (llm) by allocating the
780 * right amount of memory and copying the original information from the lsm
783 * Return total size of the buffer pointed by buf.
785 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
791 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
792 if (cmd_ctx
->llm
== NULL
) {
798 /* Copy common data */
799 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
800 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
802 cmd_ctx
->llm
->data_size
= size
;
803 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
812 * Update the kernel poll set of all channel fd available over all tracing
813 * session. Add the wakeup pipe at the end of the set.
815 static int update_kernel_poll(struct lttng_poll_event
*events
)
818 struct ltt_session
*session
;
819 struct ltt_kernel_channel
*channel
;
821 DBG("Updating kernel poll set");
824 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
825 session_lock(session
);
826 if (session
->kernel_session
== NULL
) {
827 session_unlock(session
);
831 cds_list_for_each_entry(channel
,
832 &session
->kernel_session
->channel_list
.head
, list
) {
833 /* Add channel fd to the kernel poll set */
834 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
836 session_unlock(session
);
839 DBG("Channel fd %d added to kernel set", channel
->fd
);
841 session_unlock(session
);
843 session_unlock_list();
848 session_unlock_list();
853 * Find the channel fd from 'fd' over all tracing session. When found, check
854 * for new channel stream and send those stream fds to the kernel consumer.
856 * Useful for CPU hotplug feature.
858 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
861 struct ltt_session
*session
;
862 struct ltt_kernel_session
*ksess
;
863 struct ltt_kernel_channel
*channel
;
865 DBG("Updating kernel streams for channel fd %d", fd
);
868 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
869 session_lock(session
);
870 if (session
->kernel_session
== NULL
) {
871 session_unlock(session
);
874 ksess
= session
->kernel_session
;
876 cds_list_for_each_entry(channel
, &ksess
->channel_list
.head
, list
) {
877 if (channel
->fd
== fd
) {
878 DBG("Channel found, updating kernel streams");
879 ret
= kernel_open_channel_stream(channel
);
883 /* Update the stream global counter */
884 ksess
->stream_count_global
+= ret
;
887 * Have we already sent fds to the consumer? If yes, it means
888 * that tracing is started so it is safe to send our updated
891 if (ksess
->consumer_fds_sent
== 1 && ksess
->consumer
!= NULL
) {
892 struct lttng_ht_iter iter
;
893 struct consumer_socket
*socket
;
896 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
897 &iter
.iter
, socket
, node
.node
) {
898 pthread_mutex_lock(socket
->lock
);
899 ret
= kernel_consumer_send_channel_stream(socket
,
901 session
->output_traces
? 1 : 0);
902 pthread_mutex_unlock(socket
->lock
);
913 session_unlock(session
);
915 session_unlock_list();
919 session_unlock(session
);
920 session_unlock_list();
925 * For each tracing session, update newly registered apps. The session list
926 * lock MUST be acquired before calling this.
928 static void update_ust_app(int app_sock
)
930 struct ltt_session
*sess
, *stmp
;
932 /* Consumer is in an ERROR state. Stop any application update. */
933 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
934 /* Stop the update process since the consumer is dead. */
938 /* For all tracing session(s) */
939 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
941 if (sess
->ust_session
) {
942 ust_app_global_update(sess
->ust_session
, app_sock
);
944 session_unlock(sess
);
949 * This thread manage event coming from the kernel.
951 * Features supported in this thread:
954 static void *thread_manage_kernel(void *data
)
956 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
957 uint32_t revents
, nb_fd
;
959 struct lttng_poll_event events
;
961 DBG("[thread] Thread manage kernel started");
963 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
966 * This first step of the while is to clean this structure which could free
967 * non NULL pointers so initialize it before the loop.
969 lttng_poll_init(&events
);
971 if (testpoint(sessiond_thread_manage_kernel
)) {
972 goto error_testpoint
;
975 health_code_update();
977 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
978 goto error_testpoint
;
982 health_code_update();
984 if (update_poll_flag
== 1) {
985 /* Clean events object. We are about to populate it again. */
986 lttng_poll_clean(&events
);
988 ret
= sessiond_set_thread_pollset(&events
, 2);
990 goto error_poll_create
;
993 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
998 /* This will add the available kernel channel if any. */
999 ret
= update_kernel_poll(&events
);
1003 update_poll_flag
= 0;
1006 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events
));
1008 /* Poll infinite value of time */
1010 health_poll_entry();
1011 ret
= lttng_poll_wait(&events
, -1);
1015 * Restart interrupted system call.
1017 if (errno
== EINTR
) {
1021 } else if (ret
== 0) {
1022 /* Should not happen since timeout is infinite */
1023 ERR("Return value of poll is 0 with an infinite timeout.\n"
1024 "This should not have happened! Continuing...");
1030 for (i
= 0; i
< nb_fd
; i
++) {
1031 /* Fetch once the poll data */
1032 revents
= LTTNG_POLL_GETEV(&events
, i
);
1033 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1035 health_code_update();
1037 /* Thread quit pipe has been closed. Killing thread. */
1038 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1044 /* Check for data on kernel pipe */
1045 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
1046 (void) lttng_read(kernel_poll_pipe
[0],
1049 * Ret value is useless here, if this pipe gets any actions an
1050 * update is required anyway.
1052 update_poll_flag
= 1;
1056 * New CPU detected by the kernel. Adding kernel stream to
1057 * kernel session and updating the kernel consumer
1059 if (revents
& LPOLLIN
) {
1060 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1066 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
1067 * and unregister kernel stream at this point.
1076 lttng_poll_clean(&events
);
1079 utils_close_pipe(kernel_poll_pipe
);
1080 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1083 ERR("Health error occurred in %s", __func__
);
1084 WARN("Kernel thread died unexpectedly. "
1085 "Kernel tracing can continue but CPU hotplug is disabled.");
1087 health_unregister(health_sessiond
);
1088 DBG("Kernel thread dying");
1093 * Signal pthread condition of the consumer data that the thread.
1095 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1097 pthread_mutex_lock(&data
->cond_mutex
);
1100 * The state is set before signaling. It can be any value, it's the waiter
1101 * job to correctly interpret this condition variable associated to the
1102 * consumer pthread_cond.
1104 * A value of 0 means that the corresponding thread of the consumer data
1105 * was not started. 1 indicates that the thread has started and is ready
1106 * for action. A negative value means that there was an error during the
1109 data
->consumer_thread_is_ready
= state
;
1110 (void) pthread_cond_signal(&data
->cond
);
1112 pthread_mutex_unlock(&data
->cond_mutex
);
1116 * This thread manage the consumer error sent back to the session daemon.
1118 static void *thread_manage_consumer(void *data
)
1120 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1121 uint32_t revents
, nb_fd
;
1122 enum lttcomm_return_code code
;
1123 struct lttng_poll_event events
;
1124 struct consumer_data
*consumer_data
= data
;
1126 DBG("[thread] Manage consumer started");
1128 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1130 health_code_update();
1133 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1134 * metadata_sock. Nothing more will be added to this poll set.
1136 ret
= sessiond_set_thread_pollset(&events
, 3);
1142 * The error socket here is already in a listening state which was done
1143 * just before spawning this thread to avoid a race between the consumer
1144 * daemon exec trying to connect and the listen() call.
1146 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1151 health_code_update();
1153 /* Infinite blocking call, waiting for transmission */
1155 health_poll_entry();
1157 if (testpoint(sessiond_thread_manage_consumer
)) {
1161 ret
= lttng_poll_wait(&events
, -1);
1165 * Restart interrupted system call.
1167 if (errno
== EINTR
) {
1175 for (i
= 0; i
< nb_fd
; i
++) {
1176 /* Fetch once the poll data */
1177 revents
= LTTNG_POLL_GETEV(&events
, i
);
1178 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1180 health_code_update();
1182 /* Thread quit pipe has been closed. Killing thread. */
1183 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1189 /* Event on the registration socket */
1190 if (pollfd
== consumer_data
->err_sock
) {
1191 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1192 ERR("consumer err socket poll error");
1198 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1204 * Set the CLOEXEC flag. Return code is useless because either way, the
1207 (void) utils_set_fd_cloexec(sock
);
1209 health_code_update();
1211 DBG2("Receiving code from consumer err_sock");
1213 /* Getting status code from kconsumerd */
1214 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1215 sizeof(enum lttcomm_return_code
));
1220 health_code_update();
1221 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1222 /* Connect both socket, command and metadata. */
1223 consumer_data
->cmd_sock
=
1224 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1225 consumer_data
->metadata_fd
=
1226 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1227 if (consumer_data
->cmd_sock
< 0
1228 || consumer_data
->metadata_fd
< 0) {
1229 PERROR("consumer connect cmd socket");
1230 /* On error, signal condition and quit. */
1231 signal_consumer_condition(consumer_data
, -1);
1234 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1235 /* Create metadata socket lock. */
1236 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1237 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1238 PERROR("zmalloc pthread mutex");
1242 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1244 signal_consumer_condition(consumer_data
, 1);
1245 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1246 DBG("Consumer metadata socket ready (fd: %d)",
1247 consumer_data
->metadata_fd
);
1249 ERR("consumer error when waiting for SOCK_READY : %s",
1250 lttcomm_get_readable_code(-code
));
1254 /* Remove the consumerd error sock since we've established a connexion */
1255 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1260 /* Add new accepted error socket. */
1261 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1266 /* Add metadata socket that is successfully connected. */
1267 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1268 LPOLLIN
| LPOLLRDHUP
);
1273 health_code_update();
1275 /* Infinite blocking call, waiting for transmission */
1278 health_code_update();
1280 /* Exit the thread because the thread quit pipe has been triggered. */
1282 /* Not a health error. */
1287 health_poll_entry();
1288 ret
= lttng_poll_wait(&events
, -1);
1292 * Restart interrupted system call.
1294 if (errno
== EINTR
) {
1302 for (i
= 0; i
< nb_fd
; i
++) {
1303 /* Fetch once the poll data */
1304 revents
= LTTNG_POLL_GETEV(&events
, i
);
1305 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1307 health_code_update();
1310 * Thread quit pipe has been triggered, flag that we should stop
1311 * but continue the current loop to handle potential data from
1314 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1316 if (pollfd
== sock
) {
1317 /* Event on the consumerd socket */
1318 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1319 ERR("consumer err socket second poll error");
1322 health_code_update();
1323 /* Wait for any kconsumerd error */
1324 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1325 sizeof(enum lttcomm_return_code
));
1327 ERR("consumer closed the command socket");
1331 ERR("consumer return code : %s",
1332 lttcomm_get_readable_code(-code
));
1335 } else if (pollfd
== consumer_data
->metadata_fd
) {
1336 /* UST metadata requests */
1337 ret
= ust_consumer_metadata_request(
1338 &consumer_data
->metadata_sock
);
1340 ERR("Handling metadata request");
1344 /* No need for an else branch all FDs are tested prior. */
1346 health_code_update();
1352 * We lock here because we are about to close the sockets and some other
1353 * thread might be using them so get exclusive access which will abort all
1354 * other consumer command by other threads.
1356 pthread_mutex_lock(&consumer_data
->lock
);
1358 /* Immediately set the consumerd state to stopped */
1359 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1360 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1361 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1362 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1363 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1365 /* Code flow error... */
1369 if (consumer_data
->err_sock
>= 0) {
1370 ret
= close(consumer_data
->err_sock
);
1374 consumer_data
->err_sock
= -1;
1376 if (consumer_data
->cmd_sock
>= 0) {
1377 ret
= close(consumer_data
->cmd_sock
);
1381 consumer_data
->cmd_sock
= -1;
1383 if (consumer_data
->metadata_sock
.fd_ptr
&&
1384 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1385 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1397 unlink(consumer_data
->err_unix_sock_path
);
1398 unlink(consumer_data
->cmd_unix_sock_path
);
1399 consumer_data
->pid
= 0;
1400 pthread_mutex_unlock(&consumer_data
->lock
);
1402 /* Cleanup metadata socket mutex. */
1403 if (consumer_data
->metadata_sock
.lock
) {
1404 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1405 free(consumer_data
->metadata_sock
.lock
);
1407 lttng_poll_clean(&events
);
1411 ERR("Health error occurred in %s", __func__
);
1413 health_unregister(health_sessiond
);
1414 DBG("consumer thread cleanup completed");
1420 * This thread manage application communication.
1422 static void *thread_manage_apps(void *data
)
1424 int i
, ret
, pollfd
, err
= -1;
1426 uint32_t revents
, nb_fd
;
1427 struct lttng_poll_event events
;
1429 DBG("[thread] Manage application started");
1431 rcu_register_thread();
1432 rcu_thread_online();
1434 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1436 if (testpoint(sessiond_thread_manage_apps
)) {
1437 goto error_testpoint
;
1440 health_code_update();
1442 ret
= sessiond_set_thread_pollset(&events
, 2);
1444 goto error_poll_create
;
1447 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1452 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1456 health_code_update();
1459 DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events
));
1461 /* Inifinite blocking call, waiting for transmission */
1463 health_poll_entry();
1464 ret
= lttng_poll_wait(&events
, -1);
1468 * Restart interrupted system call.
1470 if (errno
== EINTR
) {
1478 for (i
= 0; i
< nb_fd
; i
++) {
1479 /* Fetch once the poll data */
1480 revents
= LTTNG_POLL_GETEV(&events
, i
);
1481 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1483 health_code_update();
1485 /* Thread quit pipe has been closed. Killing thread. */
1486 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1492 /* Inspect the apps cmd pipe */
1493 if (pollfd
== apps_cmd_pipe
[0]) {
1494 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1495 ERR("Apps command pipe error");
1497 } else if (revents
& LPOLLIN
) {
1501 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1502 if (size_ret
< sizeof(sock
)) {
1503 PERROR("read apps cmd pipe");
1507 health_code_update();
1510 * We only monitor the error events of the socket. This
1511 * thread does not handle any incoming data from UST
1514 ret
= lttng_poll_add(&events
, sock
,
1515 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1520 DBG("Apps with sock %d added to poll set", sock
);
1524 * At this point, we know that a registered application made
1525 * the event at poll_wait.
1527 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1528 /* Removing from the poll set */
1529 ret
= lttng_poll_del(&events
, pollfd
);
1534 /* Socket closed on remote end. */
1535 ust_app_unregister(pollfd
);
1539 health_code_update();
1545 lttng_poll_clean(&events
);
1548 utils_close_pipe(apps_cmd_pipe
);
1549 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1552 * We don't clean the UST app hash table here since already registered
1553 * applications can still be controlled so let them be until the session
1554 * daemon dies or the applications stop.
1559 ERR("Health error occurred in %s", __func__
);
1561 health_unregister(health_sessiond
);
1562 DBG("Application communication apps thread cleanup complete");
1563 rcu_thread_offline();
1564 rcu_unregister_thread();
1569 * Send a socket to a thread This is called from the dispatch UST registration
1570 * thread once all sockets are set for the application.
1572 * The sock value can be invalid, we don't really care, the thread will handle
1573 * it and make the necessary cleanup if so.
1575 * On success, return 0 else a negative value being the errno message of the
1578 static int send_socket_to_thread(int fd
, int sock
)
1583 * It's possible that the FD is set as invalid with -1 concurrently just
1584 * before calling this function being a shutdown state of the thread.
1591 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1592 if (ret
< sizeof(sock
)) {
1593 PERROR("write apps pipe %d", fd
);
1600 /* All good. Don't send back the write positive ret value. */
1607 * Sanitize the wait queue of the dispatch registration thread meaning removing
1608 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1609 * notify socket is never received.
1611 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1613 int ret
, nb_fd
= 0, i
;
1614 unsigned int fd_added
= 0;
1615 struct lttng_poll_event events
;
1616 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1620 lttng_poll_init(&events
);
1622 /* Just skip everything for an empty queue. */
1623 if (!wait_queue
->count
) {
1627 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1632 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1633 &wait_queue
->head
, head
) {
1634 assert(wait_node
->app
);
1635 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1636 LPOLLHUP
| LPOLLERR
);
1649 * Poll but don't block so we can quickly identify the faulty events and
1650 * clean them afterwards from the wait queue.
1652 ret
= lttng_poll_wait(&events
, 0);
1658 for (i
= 0; i
< nb_fd
; i
++) {
1659 /* Get faulty FD. */
1660 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1661 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1663 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1664 &wait_queue
->head
, head
) {
1665 if (pollfd
== wait_node
->app
->sock
&&
1666 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1667 cds_list_del(&wait_node
->head
);
1668 wait_queue
->count
--;
1669 ust_app_destroy(wait_node
->app
);
1677 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1681 lttng_poll_clean(&events
);
1685 lttng_poll_clean(&events
);
1687 ERR("Unable to sanitize wait queue");
1692 * Dispatch request from the registration threads to the application
1693 * communication thread.
1695 static void *thread_dispatch_ust_registration(void *data
)
1698 struct cds_wfcq_node
*node
;
1699 struct ust_command
*ust_cmd
= NULL
;
1700 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1701 struct ust_reg_wait_queue wait_queue
= {
1705 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1707 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1708 goto error_testpoint
;
1711 health_code_update();
1713 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1715 DBG("[thread] Dispatch UST command started");
1717 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1718 health_code_update();
1720 /* Atomically prepare the queue futex */
1721 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1724 struct ust_app
*app
= NULL
;
1728 * Make sure we don't have node(s) that have hung up before receiving
1729 * the notify socket. This is to clean the list in order to avoid
1730 * memory leaks from notify socket that are never seen.
1732 sanitize_wait_queue(&wait_queue
);
1734 health_code_update();
1735 /* Dequeue command for registration */
1736 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1738 DBG("Woken up but nothing in the UST command queue");
1739 /* Continue thread execution */
1743 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1745 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1746 " gid:%d sock:%d name:%s (version %d.%d)",
1747 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1748 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1749 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1750 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1752 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1753 wait_node
= zmalloc(sizeof(*wait_node
));
1755 PERROR("zmalloc wait_node dispatch");
1756 ret
= close(ust_cmd
->sock
);
1758 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1760 lttng_fd_put(LTTNG_FD_APPS
, 1);
1764 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1766 /* Create application object if socket is CMD. */
1767 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1769 if (!wait_node
->app
) {
1770 ret
= close(ust_cmd
->sock
);
1772 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1774 lttng_fd_put(LTTNG_FD_APPS
, 1);
1780 * Add application to the wait queue so we can set the notify
1781 * socket before putting this object in the global ht.
1783 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1788 * We have to continue here since we don't have the notify
1789 * socket and the application MUST be added to the hash table
1790 * only at that moment.
1795 * Look for the application in the local wait queue and set the
1796 * notify socket if found.
1798 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1799 &wait_queue
.head
, head
) {
1800 health_code_update();
1801 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1802 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1803 cds_list_del(&wait_node
->head
);
1805 app
= wait_node
->app
;
1807 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1813 * With no application at this stage the received socket is
1814 * basically useless so close it before we free the cmd data
1815 * structure for good.
1818 ret
= close(ust_cmd
->sock
);
1820 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1822 lttng_fd_put(LTTNG_FD_APPS
, 1);
1829 * @session_lock_list
1831 * Lock the global session list so from the register up to the
1832 * registration done message, no thread can see the application
1833 * and change its state.
1835 session_lock_list();
1839 * Add application to the global hash table. This needs to be
1840 * done before the update to the UST registry can locate the
1845 /* Set app version. This call will print an error if needed. */
1846 (void) ust_app_version(app
);
1848 /* Send notify socket through the notify pipe. */
1849 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1853 session_unlock_list();
1855 * No notify thread, stop the UST tracing. However, this is
1856 * not an internal error of the this thread thus setting
1857 * the health error code to a normal exit.
1864 * Update newly registered application with the tracing
1865 * registry info already enabled information.
1867 update_ust_app(app
->sock
);
1870 * Don't care about return value. Let the manage apps threads
1871 * handle app unregistration upon socket close.
1873 (void) ust_app_register_done(app
->sock
);
1876 * Even if the application socket has been closed, send the app
1877 * to the thread and unregistration will take place at that
1880 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1883 session_unlock_list();
1885 * No apps. thread, stop the UST tracing. However, this is
1886 * not an internal error of the this thread thus setting
1887 * the health error code to a normal exit.
1894 session_unlock_list();
1896 } while (node
!= NULL
);
1898 health_poll_entry();
1899 /* Futex wait on queue. Blocking call on futex() */
1900 futex_nto1_wait(&ust_cmd_queue
.futex
);
1903 /* Normal exit, no error */
1907 /* Clean up wait queue. */
1908 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1909 &wait_queue
.head
, head
) {
1910 cds_list_del(&wait_node
->head
);
1916 DBG("Dispatch thread dying");
1919 ERR("Health error occurred in %s", __func__
);
1921 health_unregister(health_sessiond
);
1926 * This thread manage application registration.
1928 static void *thread_registration_apps(void *data
)
1930 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1931 uint32_t revents
, nb_fd
;
1932 struct lttng_poll_event events
;
1934 * Get allocated in this thread, enqueued to a global queue, dequeued and
1935 * freed in the manage apps thread.
1937 struct ust_command
*ust_cmd
= NULL
;
1939 DBG("[thread] Manage application registration started");
1941 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
1943 if (testpoint(sessiond_thread_registration_apps
)) {
1944 goto error_testpoint
;
1947 ret
= lttcomm_listen_unix_sock(apps_sock
);
1953 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1954 * more will be added to this poll set.
1956 ret
= sessiond_set_thread_pollset(&events
, 2);
1958 goto error_create_poll
;
1961 /* Add the application registration socket */
1962 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1964 goto error_poll_add
;
1967 /* Notify all applications to register */
1968 ret
= notify_ust_apps(1);
1970 ERR("Failed to notify applications or create the wait shared memory.\n"
1971 "Execution continues but there might be problem for already\n"
1972 "running applications that wishes to register.");
1976 DBG("Accepting application registration");
1978 /* Inifinite blocking call, waiting for transmission */
1980 health_poll_entry();
1981 ret
= lttng_poll_wait(&events
, -1);
1985 * Restart interrupted system call.
1987 if (errno
== EINTR
) {
1995 for (i
= 0; i
< nb_fd
; i
++) {
1996 health_code_update();
1998 /* Fetch once the poll data */
1999 revents
= LTTNG_POLL_GETEV(&events
, i
);
2000 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2002 /* Thread quit pipe has been closed. Killing thread. */
2003 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2009 /* Event on the registration socket */
2010 if (pollfd
== apps_sock
) {
2011 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2012 ERR("Register apps socket poll error");
2014 } else if (revents
& LPOLLIN
) {
2015 sock
= lttcomm_accept_unix_sock(apps_sock
);
2021 * Set socket timeout for both receiving and ending.
2022 * app_socket_timeout is in seconds, whereas
2023 * lttcomm_setsockopt_rcv_timeout and
2024 * lttcomm_setsockopt_snd_timeout expect msec as
2027 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2028 app_socket_timeout
* 1000);
2029 (void) lttcomm_setsockopt_snd_timeout(sock
,
2030 app_socket_timeout
* 1000);
2033 * Set the CLOEXEC flag. Return code is useless because
2034 * either way, the show must go on.
2036 (void) utils_set_fd_cloexec(sock
);
2038 /* Create UST registration command for enqueuing */
2039 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2040 if (ust_cmd
== NULL
) {
2041 PERROR("ust command zmalloc");
2046 * Using message-based transmissions to ensure we don't
2047 * have to deal with partially received messages.
2049 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2051 ERR("Exhausted file descriptors allowed for applications.");
2061 health_code_update();
2062 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2065 /* Close socket of the application. */
2070 lttng_fd_put(LTTNG_FD_APPS
, 1);
2074 health_code_update();
2076 ust_cmd
->sock
= sock
;
2079 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2080 " gid:%d sock:%d name:%s (version %d.%d)",
2081 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2082 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2083 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2084 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2087 * Lock free enqueue the registration request. The red pill
2088 * has been taken! This apps will be part of the *system*.
2090 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2093 * Wake the registration queue futex. Implicit memory
2094 * barrier with the exchange in cds_wfcq_enqueue.
2096 futex_nto1_wake(&ust_cmd_queue
.futex
);
2104 /* Notify that the registration thread is gone */
2107 if (apps_sock
>= 0) {
2108 ret
= close(apps_sock
);
2118 lttng_fd_put(LTTNG_FD_APPS
, 1);
2120 unlink(apps_unix_sock_path
);
2123 lttng_poll_clean(&events
);
2127 DBG("UST Registration thread cleanup complete");
2130 ERR("Health error occurred in %s", __func__
);
2132 health_unregister(health_sessiond
);
2138 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2139 * exec or it will fails.
2141 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2144 struct timespec timeout
;
2146 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2147 consumer_data
->consumer_thread_is_ready
= 0;
2149 /* Setup pthread condition */
2150 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2153 PERROR("pthread_condattr_init consumer data");
2158 * Set the monotonic clock in order to make sure we DO NOT jump in time
2159 * between the clock_gettime() call and the timedwait call. See bug #324
2160 * for a more details and how we noticed it.
2162 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2165 PERROR("pthread_condattr_setclock consumer data");
2169 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2172 PERROR("pthread_cond_init consumer data");
2176 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
2179 PERROR("pthread_create consumer");
2184 /* We are about to wait on a pthread condition */
2185 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2187 /* Get time for sem_timedwait absolute timeout */
2188 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2190 * Set the timeout for the condition timed wait even if the clock gettime
2191 * call fails since we might loop on that call and we want to avoid to
2192 * increment the timeout too many times.
2194 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2197 * The following loop COULD be skipped in some conditions so this is why we
2198 * set ret to 0 in order to make sure at least one round of the loop is
2204 * Loop until the condition is reached or when a timeout is reached. Note
2205 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2206 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2207 * possible. This loop does not take any chances and works with both of
2210 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2211 if (clock_ret
< 0) {
2212 PERROR("clock_gettime spawn consumer");
2213 /* Infinite wait for the consumerd thread to be ready */
2214 ret
= pthread_cond_wait(&consumer_data
->cond
,
2215 &consumer_data
->cond_mutex
);
2217 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2218 &consumer_data
->cond_mutex
, &timeout
);
2222 /* Release the pthread condition */
2223 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2227 if (ret
== ETIMEDOUT
) {
2231 * Call has timed out so we kill the kconsumerd_thread and return
2234 ERR("Condition timed out. The consumer thread was never ready."
2236 pth_ret
= pthread_cancel(consumer_data
->thread
);
2238 PERROR("pthread_cancel consumer thread");
2241 PERROR("pthread_cond_wait failed consumer thread");
2243 /* Caller is expecting a negative value on failure. */
2248 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2249 if (consumer_data
->pid
== 0) {
2250 ERR("Consumerd did not start");
2251 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2254 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2263 * Join consumer thread
2265 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2269 /* Consumer pid must be a real one. */
2270 if (consumer_data
->pid
> 0) {
2272 ret
= kill(consumer_data
->pid
, SIGTERM
);
2274 ERR("Error killing consumer daemon");
2277 return pthread_join(consumer_data
->thread
, &status
);
2284 * Fork and exec a consumer daemon (consumerd).
2286 * Return pid if successful else -1.
2288 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2292 const char *consumer_to_use
;
2293 const char *verbosity
;
2296 DBG("Spawning consumerd");
2303 if (opt_verbose_consumer
) {
2304 verbosity
= "--verbose";
2305 } else if (lttng_opt_quiet
) {
2306 verbosity
= "--quiet";
2311 switch (consumer_data
->type
) {
2312 case LTTNG_CONSUMER_KERNEL
:
2314 * Find out which consumerd to execute. We will first try the
2315 * 64-bit path, then the sessiond's installation directory, and
2316 * fallback on the 32-bit one,
2318 DBG3("Looking for a kernel consumer at these locations:");
2319 DBG3(" 1) %s", consumerd64_bin
);
2320 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2321 DBG3(" 3) %s", consumerd32_bin
);
2322 if (stat(consumerd64_bin
, &st
) == 0) {
2323 DBG3("Found location #1");
2324 consumer_to_use
= consumerd64_bin
;
2325 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2326 DBG3("Found location #2");
2327 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2328 } else if (stat(consumerd32_bin
, &st
) == 0) {
2329 DBG3("Found location #3");
2330 consumer_to_use
= consumerd32_bin
;
2332 DBG("Could not find any valid consumerd executable");
2336 DBG("Using kernel consumer at: %s", consumer_to_use
);
2337 ret
= execl(consumer_to_use
,
2338 "lttng-consumerd", verbosity
, "-k",
2339 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2340 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2341 "--group", tracing_group_name
,
2344 case LTTNG_CONSUMER64_UST
:
2346 char *tmpnew
= NULL
;
2348 if (consumerd64_libdir
[0] != '\0') {
2352 tmp
= getenv("LD_LIBRARY_PATH");
2356 tmplen
= strlen("LD_LIBRARY_PATH=")
2357 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2358 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2363 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2364 strcat(tmpnew
, consumerd64_libdir
);
2365 if (tmp
[0] != '\0') {
2366 strcat(tmpnew
, ":");
2367 strcat(tmpnew
, tmp
);
2369 ret
= putenv(tmpnew
);
2376 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2377 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2378 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2379 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2380 "--group", tracing_group_name
,
2382 if (consumerd64_libdir
[0] != '\0') {
2387 case LTTNG_CONSUMER32_UST
:
2389 char *tmpnew
= NULL
;
2391 if (consumerd32_libdir
[0] != '\0') {
2395 tmp
= getenv("LD_LIBRARY_PATH");
2399 tmplen
= strlen("LD_LIBRARY_PATH=")
2400 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2401 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2406 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2407 strcat(tmpnew
, consumerd32_libdir
);
2408 if (tmp
[0] != '\0') {
2409 strcat(tmpnew
, ":");
2410 strcat(tmpnew
, tmp
);
2412 ret
= putenv(tmpnew
);
2419 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2420 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2421 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2422 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2423 "--group", tracing_group_name
,
2425 if (consumerd32_libdir
[0] != '\0') {
2431 PERROR("unknown consumer type");
2435 PERROR("Consumer execl()");
2437 /* Reaching this point, we got a failure on our execl(). */
2439 } else if (pid
> 0) {
2442 PERROR("start consumer fork");
2450 * Spawn the consumerd daemon and session daemon thread.
2452 static int start_consumerd(struct consumer_data
*consumer_data
)
2457 * Set the listen() state on the socket since there is a possible race
2458 * between the exec() of the consumer daemon and this call if place in the
2459 * consumer thread. See bug #366 for more details.
2461 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2466 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2467 if (consumer_data
->pid
!= 0) {
2468 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2472 ret
= spawn_consumerd(consumer_data
);
2474 ERR("Spawning consumerd failed");
2475 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2479 /* Setting up the consumer_data pid */
2480 consumer_data
->pid
= ret
;
2481 DBG2("Consumer pid %d", consumer_data
->pid
);
2482 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2484 DBG2("Spawning consumer control thread");
2485 ret
= spawn_consumer_thread(consumer_data
);
2487 ERR("Fatal error spawning consumer control thread");
2495 /* Cleanup already created sockets on error. */
2496 if (consumer_data
->err_sock
>= 0) {
2499 err
= close(consumer_data
->err_sock
);
2501 PERROR("close consumer data error socket");
2508 * Setup necessary data for kernel tracer action.
2510 static int init_kernel_tracer(void)
2514 /* Modprobe lttng kernel modules */
2515 ret
= modprobe_lttng_control();
2520 /* Open debugfs lttng */
2521 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2522 if (kernel_tracer_fd
< 0) {
2523 DBG("Failed to open %s", module_proc_lttng
);
2528 /* Validate kernel version */
2529 ret
= kernel_validate_version(kernel_tracer_fd
);
2534 ret
= modprobe_lttng_data();
2539 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2543 modprobe_remove_lttng_control();
2544 ret
= close(kernel_tracer_fd
);
2548 kernel_tracer_fd
= -1;
2549 return LTTNG_ERR_KERN_VERSION
;
2552 ret
= close(kernel_tracer_fd
);
2558 modprobe_remove_lttng_control();
2561 WARN("No kernel tracer available");
2562 kernel_tracer_fd
= -1;
2564 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2566 return LTTNG_ERR_KERN_NA
;
2572 * Copy consumer output from the tracing session to the domain session. The
2573 * function also applies the right modification on a per domain basis for the
2574 * trace files destination directory.
2576 * Should *NOT* be called with RCU read-side lock held.
2578 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2581 const char *dir_name
;
2582 struct consumer_output
*consumer
;
2585 assert(session
->consumer
);
2588 case LTTNG_DOMAIN_KERNEL
:
2589 DBG3("Copying tracing session consumer output in kernel session");
2591 * XXX: We should audit the session creation and what this function
2592 * does "extra" in order to avoid a destroy since this function is used
2593 * in the domain session creation (kernel and ust) only. Same for UST
2596 if (session
->kernel_session
->consumer
) {
2597 consumer_destroy_output(session
->kernel_session
->consumer
);
2599 session
->kernel_session
->consumer
=
2600 consumer_copy_output(session
->consumer
);
2601 /* Ease our life a bit for the next part */
2602 consumer
= session
->kernel_session
->consumer
;
2603 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2605 case LTTNG_DOMAIN_JUL
:
2606 case LTTNG_DOMAIN_LOG4J
:
2607 case LTTNG_DOMAIN_PYTHON
:
2608 case LTTNG_DOMAIN_UST
:
2609 DBG3("Copying tracing session consumer output in UST session");
2610 if (session
->ust_session
->consumer
) {
2611 consumer_destroy_output(session
->ust_session
->consumer
);
2613 session
->ust_session
->consumer
=
2614 consumer_copy_output(session
->consumer
);
2615 /* Ease our life a bit for the next part */
2616 consumer
= session
->ust_session
->consumer
;
2617 dir_name
= DEFAULT_UST_TRACE_DIR
;
2620 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2624 /* Append correct directory to subdir */
2625 strncat(consumer
->subdir
, dir_name
,
2626 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2627 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2636 * Create an UST session and add it to the session ust list.
2638 * Should *NOT* be called with RCU read-side lock held.
2640 static int create_ust_session(struct ltt_session
*session
,
2641 struct lttng_domain
*domain
)
2644 struct ltt_ust_session
*lus
= NULL
;
2648 assert(session
->consumer
);
2650 switch (domain
->type
) {
2651 case LTTNG_DOMAIN_JUL
:
2652 case LTTNG_DOMAIN_LOG4J
:
2653 case LTTNG_DOMAIN_PYTHON
:
2654 case LTTNG_DOMAIN_UST
:
2657 ERR("Unknown UST domain on create session %d", domain
->type
);
2658 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2662 DBG("Creating UST session");
2664 lus
= trace_ust_create_session(session
->id
);
2666 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2670 lus
->uid
= session
->uid
;
2671 lus
->gid
= session
->gid
;
2672 lus
->output_traces
= session
->output_traces
;
2673 lus
->snapshot_mode
= session
->snapshot_mode
;
2674 lus
->live_timer_interval
= session
->live_timer
;
2675 session
->ust_session
= lus
;
2677 /* Copy session output to the newly created UST session */
2678 ret
= copy_session_consumer(domain
->type
, session
);
2679 if (ret
!= LTTNG_OK
) {
2687 session
->ust_session
= NULL
;
2692 * Create a kernel tracer session then create the default channel.
2694 static int create_kernel_session(struct ltt_session
*session
)
2698 DBG("Creating kernel session");
2700 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2702 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2706 /* Code flow safety */
2707 assert(session
->kernel_session
);
2709 /* Copy session output to the newly created Kernel session */
2710 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2711 if (ret
!= LTTNG_OK
) {
2715 /* Create directory(ies) on local filesystem. */
2716 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2717 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2718 ret
= run_as_mkdir_recursive(
2719 session
->kernel_session
->consumer
->dst
.trace_path
,
2720 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2722 if (ret
!= -EEXIST
) {
2723 ERR("Trace directory creation error");
2729 session
->kernel_session
->uid
= session
->uid
;
2730 session
->kernel_session
->gid
= session
->gid
;
2731 session
->kernel_session
->output_traces
= session
->output_traces
;
2732 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2737 trace_kernel_destroy_session(session
->kernel_session
);
2738 session
->kernel_session
= NULL
;
2743 * Count number of session permitted by uid/gid.
2745 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2748 struct ltt_session
*session
;
2750 DBG("Counting number of available session for UID %d GID %d",
2752 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2754 * Only list the sessions the user can control.
2756 if (!session_access_ok(session
, uid
, gid
)) {
2765 * Process the command requested by the lttng client within the command
2766 * context structure. This function make sure that the return structure (llm)
2767 * is set and ready for transmission before returning.
2769 * Return any error encountered or 0 for success.
2771 * "sock" is only used for special-case var. len data.
2773 * Should *NOT* be called with RCU read-side lock held.
2775 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2779 int need_tracing_session
= 1;
2782 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2786 switch (cmd_ctx
->lsm
->cmd_type
) {
2787 case LTTNG_CREATE_SESSION
:
2788 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2789 case LTTNG_CREATE_SESSION_LIVE
:
2790 case LTTNG_DESTROY_SESSION
:
2791 case LTTNG_LIST_SESSIONS
:
2792 case LTTNG_LIST_DOMAINS
:
2793 case LTTNG_START_TRACE
:
2794 case LTTNG_STOP_TRACE
:
2795 case LTTNG_DATA_PENDING
:
2796 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2797 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2798 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2799 case LTTNG_SNAPSHOT_RECORD
:
2800 case LTTNG_SAVE_SESSION
:
2807 if (opt_no_kernel
&& need_domain
2808 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2810 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2812 ret
= LTTNG_ERR_KERN_NA
;
2817 /* Deny register consumer if we already have a spawned consumer. */
2818 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2819 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2820 if (kconsumer_data
.pid
> 0) {
2821 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2822 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2825 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2829 * Check for command that don't needs to allocate a returned payload. We do
2830 * this here so we don't have to make the call for no payload at each
2833 switch(cmd_ctx
->lsm
->cmd_type
) {
2834 case LTTNG_LIST_SESSIONS
:
2835 case LTTNG_LIST_TRACEPOINTS
:
2836 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2837 case LTTNG_LIST_DOMAINS
:
2838 case LTTNG_LIST_CHANNELS
:
2839 case LTTNG_LIST_EVENTS
:
2840 case LTTNG_LIST_SYSCALLS
:
2843 /* Setup lttng message with no payload */
2844 ret
= setup_lttng_msg(cmd_ctx
, 0);
2846 /* This label does not try to unlock the session */
2847 goto init_setup_error
;
2851 /* Commands that DO NOT need a session. */
2852 switch (cmd_ctx
->lsm
->cmd_type
) {
2853 case LTTNG_CREATE_SESSION
:
2854 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2855 case LTTNG_CREATE_SESSION_LIVE
:
2856 case LTTNG_CALIBRATE
:
2857 case LTTNG_LIST_SESSIONS
:
2858 case LTTNG_LIST_TRACEPOINTS
:
2859 case LTTNG_LIST_SYSCALLS
:
2860 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2861 case LTTNG_SAVE_SESSION
:
2862 need_tracing_session
= 0;
2865 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2867 * We keep the session list lock across _all_ commands
2868 * for now, because the per-session lock does not
2869 * handle teardown properly.
2871 session_lock_list();
2872 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2873 if (cmd_ctx
->session
== NULL
) {
2874 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2877 /* Acquire lock for the session */
2878 session_lock(cmd_ctx
->session
);
2884 * Commands that need a valid session but should NOT create one if none
2885 * exists. Instead of creating one and destroying it when the command is
2886 * handled, process that right before so we save some round trip in useless
2889 switch (cmd_ctx
->lsm
->cmd_type
) {
2890 case LTTNG_DISABLE_CHANNEL
:
2891 case LTTNG_DISABLE_EVENT
:
2892 switch (cmd_ctx
->lsm
->domain
.type
) {
2893 case LTTNG_DOMAIN_KERNEL
:
2894 if (!cmd_ctx
->session
->kernel_session
) {
2895 ret
= LTTNG_ERR_NO_CHANNEL
;
2899 case LTTNG_DOMAIN_JUL
:
2900 case LTTNG_DOMAIN_LOG4J
:
2901 case LTTNG_DOMAIN_PYTHON
:
2902 case LTTNG_DOMAIN_UST
:
2903 if (!cmd_ctx
->session
->ust_session
) {
2904 ret
= LTTNG_ERR_NO_CHANNEL
;
2909 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2921 * Check domain type for specific "pre-action".
2923 switch (cmd_ctx
->lsm
->domain
.type
) {
2924 case LTTNG_DOMAIN_KERNEL
:
2926 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2930 /* Kernel tracer check */
2931 if (kernel_tracer_fd
== -1) {
2932 /* Basically, load kernel tracer modules */
2933 ret
= init_kernel_tracer();
2939 /* Consumer is in an ERROR state. Report back to client */
2940 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
2941 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2945 /* Need a session for kernel command */
2946 if (need_tracing_session
) {
2947 if (cmd_ctx
->session
->kernel_session
== NULL
) {
2948 ret
= create_kernel_session(cmd_ctx
->session
);
2950 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2955 /* Start the kernel consumer daemon */
2956 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2957 if (kconsumer_data
.pid
== 0 &&
2958 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2959 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2960 ret
= start_consumerd(&kconsumer_data
);
2962 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2965 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
2967 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2971 * The consumer was just spawned so we need to add the socket to
2972 * the consumer output of the session if exist.
2974 ret
= consumer_create_socket(&kconsumer_data
,
2975 cmd_ctx
->session
->kernel_session
->consumer
);
2982 case LTTNG_DOMAIN_JUL
:
2983 case LTTNG_DOMAIN_LOG4J
:
2984 case LTTNG_DOMAIN_PYTHON
:
2985 case LTTNG_DOMAIN_UST
:
2987 if (!ust_app_supported()) {
2988 ret
= LTTNG_ERR_NO_UST
;
2991 /* Consumer is in an ERROR state. Report back to client */
2992 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
2993 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2997 if (need_tracing_session
) {
2998 /* Create UST session if none exist. */
2999 if (cmd_ctx
->session
->ust_session
== NULL
) {
3000 ret
= create_ust_session(cmd_ctx
->session
,
3001 &cmd_ctx
->lsm
->domain
);
3002 if (ret
!= LTTNG_OK
) {
3007 /* Start the UST consumer daemons */
3009 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3010 if (consumerd64_bin
[0] != '\0' &&
3011 ustconsumer64_data
.pid
== 0 &&
3012 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3013 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3014 ret
= start_consumerd(&ustconsumer64_data
);
3016 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3017 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3021 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3022 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3024 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3028 * Setup socket for consumer 64 bit. No need for atomic access
3029 * since it was set above and can ONLY be set in this thread.
3031 ret
= consumer_create_socket(&ustconsumer64_data
,
3032 cmd_ctx
->session
->ust_session
->consumer
);
3038 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3039 if (consumerd32_bin
[0] != '\0' &&
3040 ustconsumer32_data
.pid
== 0 &&
3041 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3042 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3043 ret
= start_consumerd(&ustconsumer32_data
);
3045 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3046 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3050 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3051 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3053 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3057 * Setup socket for consumer 64 bit. No need for atomic access
3058 * since it was set above and can ONLY be set in this thread.
3060 ret
= consumer_create_socket(&ustconsumer32_data
,
3061 cmd_ctx
->session
->ust_session
->consumer
);
3073 /* Validate consumer daemon state when start/stop trace command */
3074 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3075 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3076 switch (cmd_ctx
->lsm
->domain
.type
) {
3077 case LTTNG_DOMAIN_JUL
:
3078 case LTTNG_DOMAIN_LOG4J
:
3079 case LTTNG_DOMAIN_PYTHON
:
3080 case LTTNG_DOMAIN_UST
:
3081 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3082 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3086 case LTTNG_DOMAIN_KERNEL
:
3087 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3088 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3096 * Check that the UID or GID match that of the tracing session.
3097 * The root user can interact with all sessions.
3099 if (need_tracing_session
) {
3100 if (!session_access_ok(cmd_ctx
->session
,
3101 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3102 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3103 ret
= LTTNG_ERR_EPERM
;
3109 * Send relayd information to consumer as soon as we have a domain and a
3112 if (cmd_ctx
->session
&& need_domain
) {
3114 * Setup relayd if not done yet. If the relayd information was already
3115 * sent to the consumer, this call will gracefully return.
3117 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3118 if (ret
!= LTTNG_OK
) {
3123 /* Process by command type */
3124 switch (cmd_ctx
->lsm
->cmd_type
) {
3125 case LTTNG_ADD_CONTEXT
:
3127 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3128 cmd_ctx
->lsm
->u
.context
.channel_name
,
3129 &cmd_ctx
->lsm
->u
.context
.ctx
, kernel_poll_pipe
[1]);
3132 case LTTNG_DISABLE_CHANNEL
:
3134 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3135 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3138 case LTTNG_DISABLE_EVENT
:
3140 /* FIXME: passing packed structure to non-packed pointer */
3141 /* TODO: handle filter */
3142 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3143 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3144 &cmd_ctx
->lsm
->u
.disable
.event
);
3147 case LTTNG_ENABLE_CHANNEL
:
3149 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3150 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
3153 case LTTNG_ENABLE_EVENT
:
3155 struct lttng_event_exclusion
*exclusion
= NULL
;
3156 struct lttng_filter_bytecode
*bytecode
= NULL
;
3157 char *filter_expression
= NULL
;
3159 /* Handle exclusion events and receive it from the client. */
3160 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3161 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3163 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3164 (count
* LTTNG_SYMBOL_NAME_LEN
));
3166 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3170 DBG("Receiving var len exclusion event list from client ...");
3171 exclusion
->count
= count
;
3172 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3173 count
* LTTNG_SYMBOL_NAME_LEN
);
3175 DBG("Nothing recv() from client var len data... continuing");
3178 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3183 /* Get filter expression from client. */
3184 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3185 size_t expression_len
=
3186 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3188 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3189 ret
= LTTNG_ERR_FILTER_INVAL
;
3194 filter_expression
= zmalloc(expression_len
);
3195 if (!filter_expression
) {
3197 ret
= LTTNG_ERR_FILTER_NOMEM
;
3201 /* Receive var. len. data */
3202 DBG("Receiving var len filter's expression from client ...");
3203 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3206 DBG("Nothing recv() from client car len data... continuing");
3208 free(filter_expression
);
3210 ret
= LTTNG_ERR_FILTER_INVAL
;
3215 /* Handle filter and get bytecode from client. */
3216 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3217 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3219 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3220 ret
= LTTNG_ERR_FILTER_INVAL
;
3221 free(filter_expression
);
3226 bytecode
= zmalloc(bytecode_len
);
3228 free(filter_expression
);
3230 ret
= LTTNG_ERR_FILTER_NOMEM
;
3234 /* Receive var. len. data */
3235 DBG("Receiving var len filter's bytecode from client ...");
3236 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3238 DBG("Nothing recv() from client car len data... continuing");
3240 free(filter_expression
);
3243 ret
= LTTNG_ERR_FILTER_INVAL
;
3247 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3248 free(filter_expression
);
3251 ret
= LTTNG_ERR_FILTER_INVAL
;
3256 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3257 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3258 &cmd_ctx
->lsm
->u
.enable
.event
,
3259 filter_expression
, bytecode
, exclusion
,
3260 kernel_poll_pipe
[1]);
3263 case LTTNG_LIST_TRACEPOINTS
:
3265 struct lttng_event
*events
;
3268 session_lock_list();
3269 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3270 session_unlock_list();
3271 if (nb_events
< 0) {
3272 /* Return value is a negative lttng_error_code. */
3278 * Setup lttng message with payload size set to the event list size in
3279 * bytes and then copy list into the llm payload.
3281 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
3287 /* Copy event list into message payload */
3288 memcpy(cmd_ctx
->llm
->payload
, events
,
3289 sizeof(struct lttng_event
) * nb_events
);
3296 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3298 struct lttng_event_field
*fields
;
3301 session_lock_list();
3302 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3304 session_unlock_list();
3305 if (nb_fields
< 0) {
3306 /* Return value is a negative lttng_error_code. */
3312 * Setup lttng message with payload size set to the event list size in
3313 * bytes and then copy list into the llm payload.
3315 ret
= setup_lttng_msg(cmd_ctx
,
3316 sizeof(struct lttng_event_field
) * nb_fields
);
3322 /* Copy event list into message payload */
3323 memcpy(cmd_ctx
->llm
->payload
, fields
,
3324 sizeof(struct lttng_event_field
) * nb_fields
);
3331 case LTTNG_LIST_SYSCALLS
:
3333 struct lttng_event
*events
;
3336 nb_events
= cmd_list_syscalls(&events
);
3337 if (nb_events
< 0) {
3338 /* Return value is a negative lttng_error_code. */
3344 * Setup lttng message with payload size set to the event list size in
3345 * bytes and then copy list into the llm payload.
3347 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
3353 /* Copy event list into message payload */
3354 memcpy(cmd_ctx
->llm
->payload
, events
,
3355 sizeof(struct lttng_event
) * nb_events
);
3362 case LTTNG_SET_CONSUMER_URI
:
3365 struct lttng_uri
*uris
;
3367 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3368 len
= nb_uri
* sizeof(struct lttng_uri
);
3371 ret
= LTTNG_ERR_INVALID
;
3375 uris
= zmalloc(len
);
3377 ret
= LTTNG_ERR_FATAL
;
3381 /* Receive variable len data */
3382 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3383 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3385 DBG("No URIs received from client... continuing");
3387 ret
= LTTNG_ERR_SESSION_FAIL
;
3392 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3394 if (ret
!= LTTNG_OK
) {
3401 case LTTNG_START_TRACE
:
3403 ret
= cmd_start_trace(cmd_ctx
->session
);
3406 case LTTNG_STOP_TRACE
:
3408 ret
= cmd_stop_trace(cmd_ctx
->session
);
3411 case LTTNG_CREATE_SESSION
:
3414 struct lttng_uri
*uris
= NULL
;
3416 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3417 len
= nb_uri
* sizeof(struct lttng_uri
);
3420 uris
= zmalloc(len
);
3422 ret
= LTTNG_ERR_FATAL
;
3426 /* Receive variable len data */
3427 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3428 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3430 DBG("No URIs received from client... continuing");
3432 ret
= LTTNG_ERR_SESSION_FAIL
;
3437 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3438 DBG("Creating session with ONE network URI is a bad call");
3439 ret
= LTTNG_ERR_SESSION_FAIL
;
3445 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3446 &cmd_ctx
->creds
, 0);
3452 case LTTNG_DESTROY_SESSION
:
3454 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3456 /* Set session to NULL so we do not unlock it after free. */
3457 cmd_ctx
->session
= NULL
;
3460 case LTTNG_LIST_DOMAINS
:
3463 struct lttng_domain
*domains
= NULL
;
3465 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3467 /* Return value is a negative lttng_error_code. */
3472 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
3478 /* Copy event list into message payload */
3479 memcpy(cmd_ctx
->llm
->payload
, domains
,
3480 nb_dom
* sizeof(struct lttng_domain
));
3487 case LTTNG_LIST_CHANNELS
:
3490 struct lttng_channel
*channels
= NULL
;
3492 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3493 cmd_ctx
->session
, &channels
);
3495 /* Return value is a negative lttng_error_code. */
3500 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
3506 /* Copy event list into message payload */
3507 memcpy(cmd_ctx
->llm
->payload
, channels
,
3508 nb_chan
* sizeof(struct lttng_channel
));
3515 case LTTNG_LIST_EVENTS
:
3518 struct lttng_event
*events
= NULL
;
3520 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3521 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
3523 /* Return value is a negative lttng_error_code. */
3528 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
3534 /* Copy event list into message payload */
3535 memcpy(cmd_ctx
->llm
->payload
, events
,
3536 nb_event
* sizeof(struct lttng_event
));
3543 case LTTNG_LIST_SESSIONS
:
3545 unsigned int nr_sessions
;
3547 session_lock_list();
3548 nr_sessions
= lttng_sessions_count(
3549 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3550 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3552 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
3554 session_unlock_list();
3558 /* Filled the session array */
3559 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
3560 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3561 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3563 session_unlock_list();
3568 case LTTNG_CALIBRATE
:
3570 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3571 &cmd_ctx
->lsm
->u
.calibrate
);
3574 case LTTNG_REGISTER_CONSUMER
:
3576 struct consumer_data
*cdata
;
3578 switch (cmd_ctx
->lsm
->domain
.type
) {
3579 case LTTNG_DOMAIN_KERNEL
:
3580 cdata
= &kconsumer_data
;
3583 ret
= LTTNG_ERR_UND
;
3587 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3588 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3591 case LTTNG_DATA_PENDING
:
3593 ret
= cmd_data_pending(cmd_ctx
->session
);
3596 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3598 struct lttcomm_lttng_output_id reply
;
3600 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3601 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3602 if (ret
!= LTTNG_OK
) {
3606 ret
= setup_lttng_msg(cmd_ctx
, sizeof(reply
));
3611 /* Copy output list into message payload */
3612 memcpy(cmd_ctx
->llm
->payload
, &reply
, sizeof(reply
));
3616 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3618 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3619 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3622 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3625 struct lttng_snapshot_output
*outputs
= NULL
;
3627 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3628 if (nb_output
< 0) {
3633 ret
= setup_lttng_msg(cmd_ctx
,
3634 nb_output
* sizeof(struct lttng_snapshot_output
));
3641 /* Copy output list into message payload */
3642 memcpy(cmd_ctx
->llm
->payload
, outputs
,
3643 nb_output
* sizeof(struct lttng_snapshot_output
));
3650 case LTTNG_SNAPSHOT_RECORD
:
3652 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3653 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3654 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3657 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3660 struct lttng_uri
*uris
= NULL
;
3662 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3663 len
= nb_uri
* sizeof(struct lttng_uri
);
3666 uris
= zmalloc(len
);
3668 ret
= LTTNG_ERR_FATAL
;
3672 /* Receive variable len data */
3673 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3674 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3676 DBG("No URIs received from client... continuing");
3678 ret
= LTTNG_ERR_SESSION_FAIL
;
3683 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3684 DBG("Creating session with ONE network URI is a bad call");
3685 ret
= LTTNG_ERR_SESSION_FAIL
;
3691 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
3692 nb_uri
, &cmd_ctx
->creds
);
3696 case LTTNG_CREATE_SESSION_LIVE
:
3699 struct lttng_uri
*uris
= NULL
;
3701 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3702 len
= nb_uri
* sizeof(struct lttng_uri
);
3705 uris
= zmalloc(len
);
3707 ret
= LTTNG_ERR_FATAL
;
3711 /* Receive variable len data */
3712 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3713 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3715 DBG("No URIs received from client... continuing");
3717 ret
= LTTNG_ERR_SESSION_FAIL
;
3722 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3723 DBG("Creating session with ONE network URI is a bad call");
3724 ret
= LTTNG_ERR_SESSION_FAIL
;
3730 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
3731 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
3735 case LTTNG_SAVE_SESSION
:
3737 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
3742 ret
= LTTNG_ERR_UND
;
3747 if (cmd_ctx
->llm
== NULL
) {
3748 DBG("Missing llm structure. Allocating one.");
3749 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
3753 /* Set return code */
3754 cmd_ctx
->llm
->ret_code
= ret
;
3756 if (cmd_ctx
->session
) {
3757 session_unlock(cmd_ctx
->session
);
3759 if (need_tracing_session
) {
3760 session_unlock_list();
3767 * Thread managing health check socket.
3769 static void *thread_manage_health(void *data
)
3771 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
3772 uint32_t revents
, nb_fd
;
3773 struct lttng_poll_event events
;
3774 struct health_comm_msg msg
;
3775 struct health_comm_reply reply
;
3777 DBG("[thread] Manage health check started");
3779 rcu_register_thread();
3781 /* We might hit an error path before this is created. */
3782 lttng_poll_init(&events
);
3784 /* Create unix socket */
3785 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
3787 ERR("Unable to create health check Unix socket");
3793 /* lttng health client socket path permissions */
3794 ret
= chown(health_unix_sock_path
, 0,
3795 utils_get_group_id(tracing_group_name
));
3797 ERR("Unable to set group on %s", health_unix_sock_path
);
3803 ret
= chmod(health_unix_sock_path
,
3804 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3806 ERR("Unable to set permissions on %s", health_unix_sock_path
);
3814 * Set the CLOEXEC flag. Return code is useless because either way, the
3817 (void) utils_set_fd_cloexec(sock
);
3819 ret
= lttcomm_listen_unix_sock(sock
);
3825 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3826 * more will be added to this poll set.
3828 ret
= sessiond_set_thread_pollset(&events
, 2);
3833 /* Add the application registration socket */
3834 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
3839 sessiond_notify_ready();
3842 DBG("Health check ready");
3844 /* Inifinite blocking call, waiting for transmission */
3846 ret
= lttng_poll_wait(&events
, -1);
3849 * Restart interrupted system call.
3851 if (errno
== EINTR
) {
3859 for (i
= 0; i
< nb_fd
; i
++) {
3860 /* Fetch once the poll data */
3861 revents
= LTTNG_POLL_GETEV(&events
, i
);
3862 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3864 /* Thread quit pipe has been closed. Killing thread. */
3865 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3871 /* Event on the registration socket */
3872 if (pollfd
== sock
) {
3873 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3874 ERR("Health socket poll error");
3880 new_sock
= lttcomm_accept_unix_sock(sock
);
3886 * Set the CLOEXEC flag. Return code is useless because either way, the
3889 (void) utils_set_fd_cloexec(new_sock
);
3891 DBG("Receiving data from client for health...");
3892 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
3894 DBG("Nothing recv() from client... continuing");
3895 ret
= close(new_sock
);
3903 rcu_thread_online();
3905 memset(&reply
, 0, sizeof(reply
));
3906 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
3908 * health_check_state returns 0 if health is
3911 if (!health_check_state(health_sessiond
, i
)) {
3912 reply
.ret_code
|= 1ULL << i
;
3916 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
3918 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
3920 ERR("Failed to send health data back to client");
3923 /* End of transmission */
3924 ret
= close(new_sock
);
3934 ERR("Health error occurred in %s", __func__
);
3936 DBG("Health check thread dying");
3937 unlink(health_unix_sock_path
);
3945 lttng_poll_clean(&events
);
3947 rcu_unregister_thread();
3952 * This thread manage all clients request using the unix client socket for
3955 static void *thread_manage_clients(void *data
)
3957 int sock
= -1, ret
, i
, pollfd
, err
= -1;
3959 uint32_t revents
, nb_fd
;
3960 struct command_ctx
*cmd_ctx
= NULL
;
3961 struct lttng_poll_event events
;
3963 DBG("[thread] Manage client started");
3965 rcu_register_thread();
3967 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
3969 health_code_update();
3971 ret
= lttcomm_listen_unix_sock(client_sock
);
3977 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3978 * more will be added to this poll set.
3980 ret
= sessiond_set_thread_pollset(&events
, 2);
3982 goto error_create_poll
;
3985 /* Add the application registration socket */
3986 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
3991 sessiond_notify_ready();
3992 ret
= sem_post(&load_info
->message_thread_ready
);
3994 PERROR("sem_post message_thread_ready");
3998 /* This testpoint is after we signal readiness to the parent. */
3999 if (testpoint(sessiond_thread_manage_clients
)) {
4003 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4007 health_code_update();
4010 DBG("Accepting client command ...");
4012 /* Inifinite blocking call, waiting for transmission */
4014 health_poll_entry();
4015 ret
= lttng_poll_wait(&events
, -1);
4019 * Restart interrupted system call.
4021 if (errno
== EINTR
) {
4029 for (i
= 0; i
< nb_fd
; i
++) {
4030 /* Fetch once the poll data */
4031 revents
= LTTNG_POLL_GETEV(&events
, i
);
4032 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4034 health_code_update();
4036 /* Thread quit pipe has been closed. Killing thread. */
4037 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4043 /* Event on the registration socket */
4044 if (pollfd
== client_sock
) {
4045 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4046 ERR("Client socket poll error");
4052 DBG("Wait for client response");
4054 health_code_update();
4056 sock
= lttcomm_accept_unix_sock(client_sock
);
4062 * Set the CLOEXEC flag. Return code is useless because either way, the
4065 (void) utils_set_fd_cloexec(sock
);
4067 /* Set socket option for credentials retrieval */
4068 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4073 /* Allocate context command to process the client request */
4074 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4075 if (cmd_ctx
== NULL
) {
4076 PERROR("zmalloc cmd_ctx");
4080 /* Allocate data buffer for reception */
4081 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4082 if (cmd_ctx
->lsm
== NULL
) {
4083 PERROR("zmalloc cmd_ctx->lsm");
4087 cmd_ctx
->llm
= NULL
;
4088 cmd_ctx
->session
= NULL
;
4090 health_code_update();
4093 * Data is received from the lttng client. The struct
4094 * lttcomm_session_msg (lsm) contains the command and data request of
4097 DBG("Receiving data from client ...");
4098 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4099 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4101 DBG("Nothing recv() from client... continuing");
4107 clean_command_ctx(&cmd_ctx
);
4111 health_code_update();
4113 // TODO: Validate cmd_ctx including sanity check for
4114 // security purpose.
4116 rcu_thread_online();
4118 * This function dispatch the work to the kernel or userspace tracer
4119 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4120 * informations for the client. The command context struct contains
4121 * everything this function may needs.
4123 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4124 rcu_thread_offline();
4132 * TODO: Inform client somehow of the fatal error. At
4133 * this point, ret < 0 means that a zmalloc failed
4134 * (ENOMEM). Error detected but still accept
4135 * command, unless a socket error has been
4138 clean_command_ctx(&cmd_ctx
);
4142 health_code_update();
4144 DBG("Sending response (size: %d, retcode: %s)",
4145 cmd_ctx
->lttng_msg_size
,
4146 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
4147 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4149 ERR("Failed to send data back to client");
4152 /* End of transmission */
4159 clean_command_ctx(&cmd_ctx
);
4161 health_code_update();
4173 lttng_poll_clean(&events
);
4174 clean_command_ctx(&cmd_ctx
);
4178 unlink(client_unix_sock_path
);
4179 if (client_sock
>= 0) {
4180 ret
= close(client_sock
);
4188 ERR("Health error occurred in %s", __func__
);
4191 health_unregister(health_sessiond
);
4193 DBG("Client thread dying");
4195 rcu_unregister_thread();
4201 * usage function on stderr
4203 static void usage(void)
4205 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
4206 fprintf(stderr
, " -h, --help Display this usage.\n");
4207 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
4208 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
4209 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
4210 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
4211 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
4212 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
4213 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
4214 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
4215 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
4216 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
4217 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
4218 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
4219 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
4220 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
4221 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
4222 fprintf(stderr
, " -V, --version Show version number.\n");
4223 fprintf(stderr
, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n");
4224 fprintf(stderr
, " -q, --quiet No output at all.\n");
4225 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
4226 fprintf(stderr
, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
4227 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4228 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
4229 fprintf(stderr
, " --agent-tcp-port Agent registration TCP port\n");
4230 fprintf(stderr
, " -f --config PATH Load daemon configuration file\n");
4231 fprintf(stderr
, " -l --load PATH Load session configuration\n");
4232 fprintf(stderr
, " --kmod-probes Specify kernel module probes to load\n");
4233 fprintf(stderr
, " --extra-kmod-probes Specify extra kernel module probes to load\n");
4237 * Take an option from the getopt output and set it in the right variable to be
4240 * Return 0 on success else a negative value.
4242 static int set_option(int opt
, const char *arg
, const char *optname
)
4246 if (arg
&& arg
[0] == '\0') {
4248 * This only happens if the value is read from daemon config
4249 * file. This means the option requires an argument and the
4250 * configuration file contains a line such as:
4259 fprintf(stderr
, "option %s", optname
);
4261 fprintf(stderr
, " with arg %s\n", arg
);
4265 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", arg
);
4268 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", arg
);
4278 * If the override option is set, the pointer points to a
4279 * *non* const thus freeing it even though the variable type is
4282 if (tracing_group_name_override
) {
4283 free((void *) tracing_group_name
);
4285 tracing_group_name
= strdup(arg
);
4286 if (!tracing_group_name
) {
4290 tracing_group_name_override
= 1;
4296 fprintf(stdout
, "%s\n", VERSION
);
4302 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4305 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4308 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4311 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4314 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4317 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4323 lttng_opt_quiet
= 1;
4326 /* Verbose level can increase using multiple -v */
4328 /* Value obtained from config file */
4329 lttng_opt_verbose
= config_parse_value(arg
);
4331 /* -v used on command line */
4332 lttng_opt_verbose
++;
4334 /* Clamp value to [0, 3] */
4335 lttng_opt_verbose
= lttng_opt_verbose
< 0 ? 0 :
4336 (lttng_opt_verbose
<= 3 ? lttng_opt_verbose
: 3);
4340 opt_verbose_consumer
= config_parse_value(arg
);
4342 opt_verbose_consumer
+= 1;
4346 if (consumerd32_bin_override
) {
4347 free((void *) consumerd32_bin
);
4349 consumerd32_bin
= strdup(arg
);
4350 if (!consumerd32_bin
) {
4354 consumerd32_bin_override
= 1;
4357 if (consumerd32_libdir_override
) {
4358 free((void *) consumerd32_libdir
);
4360 consumerd32_libdir
= strdup(arg
);
4361 if (!consumerd32_libdir
) {
4365 consumerd32_libdir_override
= 1;
4368 if (consumerd64_bin_override
) {
4369 free((void *) consumerd64_bin
);
4371 consumerd64_bin
= strdup(arg
);
4372 if (!consumerd64_bin
) {
4376 consumerd64_bin_override
= 1;
4379 if (consumerd64_libdir_override
) {
4380 free((void *) consumerd64_libdir
);
4382 consumerd64_libdir
= strdup(arg
);
4383 if (!consumerd64_libdir
) {
4387 consumerd64_libdir_override
= 1;
4391 opt_pidfile
= strdup(arg
);
4397 case 'J': /* Agent TCP port. */
4402 v
= strtoul(arg
, NULL
, 0);
4403 if (errno
!= 0 || !isdigit(arg
[0])) {
4404 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
4407 if (v
== 0 || v
>= 65535) {
4408 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
4411 agent_tcp_port
= (uint32_t) v
;
4412 DBG3("Agent TCP port set to non default: %u", agent_tcp_port
);
4416 free(opt_load_session_path
);
4417 opt_load_session_path
= strdup(arg
);
4418 if (!opt_load_session_path
) {
4423 case 'P': /* probe modules list */
4424 free(kmod_probes_list
);
4425 kmod_probes_list
= strdup(arg
);
4426 if (!kmod_probes_list
) {
4432 free(kmod_extra_probes_list
);
4433 kmod_extra_probes_list
= strdup(arg
);
4434 if (!kmod_extra_probes_list
) {
4440 /* This is handled in set_options() thus silent break. */
4443 /* Unknown option or other error.
4444 * Error is printed by getopt, just return */
4449 if (ret
== -EINVAL
) {
4450 const char *opt_name
= "unknown";
4453 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
4455 if (opt
== long_options
[i
].val
) {
4456 opt_name
= long_options
[i
].name
;
4461 WARN("Invalid argument provided for option \"%s\", using default value.",
4469 * config_entry_handler_cb used to handle options read from a config file.
4470 * See config_entry_handler_cb comment in common/config/config.h for the
4471 * return value conventions.
4473 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
4477 if (!entry
|| !entry
->name
|| !entry
->value
) {
4482 /* Check if the option is to be ignored */
4483 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
4484 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
4489 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
4492 /* Ignore if not fully matched. */
4493 if (strcmp(entry
->name
, long_options
[i
].name
)) {
4498 * If the option takes no argument on the command line, we have to
4499 * check if the value is "true". We support non-zero numeric values,
4502 if (!long_options
[i
].has_arg
) {
4503 ret
= config_parse_value(entry
->value
);
4506 WARN("Invalid configuration value \"%s\" for option %s",
4507 entry
->value
, entry
->name
);
4509 /* False, skip boolean config option. */
4514 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
4518 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
4525 * daemon configuration loading and argument parsing
4527 static int set_options(int argc
, char **argv
)
4529 int ret
= 0, c
= 0, option_index
= 0;
4530 int orig_optopt
= optopt
, orig_optind
= optind
;
4532 const char *config_path
= NULL
;
4534 optstring
= utils_generate_optstring(long_options
,
4535 sizeof(long_options
) / sizeof(struct option
));
4541 /* Check for the --config option */
4542 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
4543 &option_index
)) != -1) {
4547 } else if (c
!= 'f') {
4548 /* if not equal to --config option. */
4552 config_path
= utils_expand_path(optarg
);
4554 ERR("Failed to resolve path: %s", optarg
);
4558 ret
= config_get_section_entries(config_path
, config_section_name
,
4559 config_entry_handler
, NULL
);
4562 ERR("Invalid configuration option at line %i", ret
);
4568 /* Reset getopt's global state */
4569 optopt
= orig_optopt
;
4570 optind
= orig_optind
;
4572 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
4577 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
4589 * Creates the two needed socket by the daemon.
4590 * apps_sock - The communication socket for all UST apps.
4591 * client_sock - The communication of the cli tool (lttng).
4593 static int init_daemon_socket(void)
4598 old_umask
= umask(0);
4600 /* Create client tool unix socket */
4601 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
4602 if (client_sock
< 0) {
4603 ERR("Create unix sock failed: %s", client_unix_sock_path
);
4608 /* Set the cloexec flag */
4609 ret
= utils_set_fd_cloexec(client_sock
);
4611 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4612 "Continuing but note that the consumer daemon will have a "
4613 "reference to this socket on exec()", client_sock
);
4616 /* File permission MUST be 660 */
4617 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4619 ERR("Set file permissions failed: %s", client_unix_sock_path
);
4624 /* Create the application unix socket */
4625 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
4626 if (apps_sock
< 0) {
4627 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
4632 /* Set the cloexec flag */
4633 ret
= utils_set_fd_cloexec(apps_sock
);
4635 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4636 "Continuing but note that the consumer daemon will have a "
4637 "reference to this socket on exec()", apps_sock
);
4640 /* File permission MUST be 666 */
4641 ret
= chmod(apps_unix_sock_path
,
4642 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
4644 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
4649 DBG3("Session daemon client socket %d and application socket %d created",
4650 client_sock
, apps_sock
);
4658 * Check if the global socket is available, and if a daemon is answering at the
4659 * other side. If yes, error is returned.
4661 static int check_existing_daemon(void)
4663 /* Is there anybody out there ? */
4664 if (lttng_session_daemon_alive()) {
4672 * Set the tracing group gid onto the client socket.
4674 * Race window between mkdir and chown is OK because we are going from more
4675 * permissive (root.root) to less permissive (root.tracing).
4677 static int set_permissions(char *rundir
)
4682 gid
= utils_get_group_id(tracing_group_name
);
4684 /* Set lttng run dir */
4685 ret
= chown(rundir
, 0, gid
);
4687 ERR("Unable to set group on %s", rundir
);
4692 * Ensure all applications and tracing group can search the run
4693 * dir. Allow everyone to read the directory, since it does not
4694 * buy us anything to hide its content.
4696 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
4698 ERR("Unable to set permissions on %s", rundir
);
4702 /* lttng client socket path */
4703 ret
= chown(client_unix_sock_path
, 0, gid
);
4705 ERR("Unable to set group on %s", client_unix_sock_path
);
4709 /* kconsumer error socket path */
4710 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
4712 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
4716 /* 64-bit ustconsumer error socket path */
4717 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
4719 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
4723 /* 32-bit ustconsumer compat32 error socket path */
4724 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
4726 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
4730 DBG("All permissions are set");
4736 * Create the lttng run directory needed for all global sockets and pipe.
4738 static int create_lttng_rundir(const char *rundir
)
4742 DBG3("Creating LTTng run directory: %s", rundir
);
4744 ret
= mkdir(rundir
, S_IRWXU
);
4746 if (errno
!= EEXIST
) {
4747 ERR("Unable to create %s", rundir
);
4759 * Setup sockets and directory needed by the kconsumerd communication with the
4762 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
4766 char path
[PATH_MAX
];
4768 switch (consumer_data
->type
) {
4769 case LTTNG_CONSUMER_KERNEL
:
4770 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
4772 case LTTNG_CONSUMER64_UST
:
4773 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
4775 case LTTNG_CONSUMER32_UST
:
4776 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
4779 ERR("Consumer type unknown");
4784 DBG2("Creating consumer directory: %s", path
);
4786 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
4788 if (errno
!= EEXIST
) {
4790 ERR("Failed to create %s", path
);
4796 ret
= chown(path
, 0, utils_get_group_id(tracing_group_name
));
4798 ERR("Unable to set group on %s", path
);
4804 /* Create the kconsumerd error unix socket */
4805 consumer_data
->err_sock
=
4806 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
4807 if (consumer_data
->err_sock
< 0) {
4808 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
4814 * Set the CLOEXEC flag. Return code is useless because either way, the
4817 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
4819 PERROR("utils_set_fd_cloexec");
4820 /* continue anyway */
4823 /* File permission MUST be 660 */
4824 ret
= chmod(consumer_data
->err_unix_sock_path
,
4825 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4827 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
4837 * Signal handler for the daemon
4839 * Simply stop all worker threads, leaving main() return gracefully after
4840 * joining all threads and calling cleanup().
4842 static void sighandler(int sig
)
4846 DBG("SIGPIPE caught");
4849 DBG("SIGINT caught");
4853 DBG("SIGTERM caught");
4857 CMM_STORE_SHARED(recv_child_signal
, 1);
4865 * Setup signal handler for :
4866 * SIGINT, SIGTERM, SIGPIPE
4868 static int set_signal_handler(void)
4871 struct sigaction sa
;
4874 if ((ret
= sigemptyset(&sigset
)) < 0) {
4875 PERROR("sigemptyset");
4879 sa
.sa_handler
= sighandler
;
4880 sa
.sa_mask
= sigset
;
4882 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
4883 PERROR("sigaction");
4887 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
4888 PERROR("sigaction");
4892 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
4893 PERROR("sigaction");
4897 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
4898 PERROR("sigaction");
4902 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
4908 * Set open files limit to unlimited. This daemon can open a large number of
4909 * file descriptors in order to consumer multiple kernel traces.
4911 static void set_ulimit(void)
4916 /* The kernel does not allowed an infinite limit for open files */
4917 lim
.rlim_cur
= 65535;
4918 lim
.rlim_max
= 65535;
4920 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
4922 PERROR("failed to set open files limit");
4927 * Write pidfile using the rundir and opt_pidfile.
4929 static void write_pidfile(void)
4932 char pidfile_path
[PATH_MAX
];
4937 strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
));
4939 /* Build pidfile path from rundir and opt_pidfile. */
4940 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
4941 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
4943 PERROR("snprintf pidfile path");
4949 * Create pid file in rundir. Return value is of no importance. The
4950 * execution will continue even though we are not able to write the file.
4952 (void) utils_create_pid_file(getpid(), pidfile_path
);
4959 * Create lockfile using the rundir and return its fd.
4961 static int create_lockfile(void)
4964 char lockfile_path
[PATH_MAX
];
4966 ret
= generate_lock_file_path(lockfile_path
, sizeof(lockfile_path
));
4971 ret
= utils_create_lock_file(lockfile_path
);
4977 * Write agent TCP port using the rundir.
4979 static void write_agent_port(void)
4982 char path
[PATH_MAX
];
4986 ret
= snprintf(path
, sizeof(path
), "%s/"
4987 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
, rundir
);
4989 PERROR("snprintf agent port path");
4994 * Create TCP agent port file in rundir. Return value is of no importance.
4995 * The execution will continue even though we are not able to write the
4998 (void) utils_create_pid_file(agent_tcp_port
, path
);
5005 * Start the load session thread and dettach from it so the main thread can
5006 * continue. This does not return a value since whatever the outcome, the main
5007 * thread will continue.
5009 static void start_load_session_thread(void)
5013 /* Create session loading thread. */
5014 ret
= pthread_create(&load_session_thread
, NULL
, thread_load_session
,
5017 PERROR("pthread_create load_session_thread");
5021 ret
= pthread_detach(load_session_thread
);
5023 PERROR("pthread_detach load_session_thread");
5026 /* Everything went well so don't cleanup anything. */
5029 /* The cleanup() function will destroy the load_info data. */
5036 int main(int argc
, char **argv
)
5040 const char *home_path
, *env_app_timeout
;
5042 init_kernel_workarounds();
5044 rcu_register_thread();
5046 if ((ret
= set_signal_handler()) < 0) {
5050 setup_consumerd_path();
5052 page_size
= sysconf(_SC_PAGESIZE
);
5053 if (page_size
< 0) {
5054 PERROR("sysconf _SC_PAGESIZE");
5055 page_size
= LONG_MAX
;
5056 WARN("Fallback page size to %ld", page_size
);
5059 /* Parse arguments and load the daemon configuration file */
5061 if ((ret
= set_options(argc
, argv
)) < 0) {
5066 if (opt_daemon
|| opt_background
) {
5069 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5076 * We are in the child. Make sure all other file descriptors are
5077 * closed, in case we are called with more opened file descriptors than
5078 * the standard ones.
5080 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5085 /* Create thread quit pipe */
5086 if ((ret
= init_thread_quit_pipe()) < 0) {
5090 /* Check if daemon is UID = 0 */
5091 is_root
= !getuid();
5094 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
5100 /* Create global run dir with root access */
5101 ret
= create_lttng_rundir(rundir
);
5106 if (strlen(apps_unix_sock_path
) == 0) {
5107 snprintf(apps_unix_sock_path
, PATH_MAX
,
5108 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
5111 if (strlen(client_unix_sock_path
) == 0) {
5112 snprintf(client_unix_sock_path
, PATH_MAX
,
5113 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
5116 /* Set global SHM for ust */
5117 if (strlen(wait_shm_path
) == 0) {
5118 snprintf(wait_shm_path
, PATH_MAX
,
5119 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
5122 if (strlen(health_unix_sock_path
) == 0) {
5123 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
5124 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
5127 /* Setup kernel consumerd path */
5128 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
5129 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
5130 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
5131 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
5133 DBG2("Kernel consumer err path: %s",
5134 kconsumer_data
.err_unix_sock_path
);
5135 DBG2("Kernel consumer cmd path: %s",
5136 kconsumer_data
.cmd_unix_sock_path
);
5138 home_path
= utils_get_home_dir();
5139 if (home_path
== NULL
) {
5140 /* TODO: Add --socket PATH option */
5141 ERR("Can't get HOME directory for sockets creation.");
5147 * Create rundir from home path. This will create something like
5150 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
5156 ret
= create_lttng_rundir(rundir
);
5161 if (strlen(apps_unix_sock_path
) == 0) {
5162 snprintf(apps_unix_sock_path
, PATH_MAX
,
5163 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
5166 /* Set the cli tool unix socket path */
5167 if (strlen(client_unix_sock_path
) == 0) {
5168 snprintf(client_unix_sock_path
, PATH_MAX
,
5169 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
5172 /* Set global SHM for ust */
5173 if (strlen(wait_shm_path
) == 0) {
5174 snprintf(wait_shm_path
, PATH_MAX
,
5175 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
5178 /* Set health check Unix path */
5179 if (strlen(health_unix_sock_path
) == 0) {
5180 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
5181 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
5185 lockfile_fd
= create_lockfile();
5186 if (lockfile_fd
< 0) {
5190 /* Set consumer initial state */
5191 kernel_consumerd_state
= CONSUMER_STOPPED
;
5192 ust_consumerd_state
= CONSUMER_STOPPED
;
5194 DBG("Client socket path %s", client_unix_sock_path
);
5195 DBG("Application socket path %s", apps_unix_sock_path
);
5196 DBG("Application wait path %s", wait_shm_path
);
5197 DBG("LTTng run directory path: %s", rundir
);
5199 /* 32 bits consumerd path setup */
5200 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
5201 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
5202 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
5203 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
5205 DBG2("UST consumer 32 bits err path: %s",
5206 ustconsumer32_data
.err_unix_sock_path
);
5207 DBG2("UST consumer 32 bits cmd path: %s",
5208 ustconsumer32_data
.cmd_unix_sock_path
);
5210 /* 64 bits consumerd path setup */
5211 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
5212 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
5213 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
5214 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
5216 DBG2("UST consumer 64 bits err path: %s",
5217 ustconsumer64_data
.err_unix_sock_path
);
5218 DBG2("UST consumer 64 bits cmd path: %s",
5219 ustconsumer64_data
.cmd_unix_sock_path
);
5222 * See if daemon already exist.
5224 if ((ret
= check_existing_daemon()) < 0) {
5225 ERR("Already running daemon.\n");
5227 * We do not goto exit because we must not cleanup()
5228 * because a daemon is already running.
5234 * Init UST app hash table. Alloc hash table before this point since
5235 * cleanup() can get called after that point.
5239 /* Initialize agent domain subsystem. */
5240 if ((ret
= agent_setup()) < 0) {
5241 /* ENOMEM at this point. */
5245 /* After this point, we can safely call cleanup() with "goto exit" */
5248 * These actions must be executed as root. We do that *after* setting up
5249 * the sockets path because we MUST make the check for another daemon using
5250 * those paths *before* trying to set the kernel consumer sockets and init
5254 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
5259 /* Setup kernel tracer */
5260 if (!opt_no_kernel
) {
5261 init_kernel_tracer();
5262 if (kernel_tracer_fd
>= 0) {
5263 ret
= syscall_init_table();
5265 ERR("Unable to populate syscall table. Syscall tracing"
5266 " won't work for this session daemon.");
5271 /* Set ulimit for open files */
5274 /* init lttng_fd tracking must be done after set_ulimit. */
5277 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
5282 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
5287 /* Setup the needed unix socket */
5288 if ((ret
= init_daemon_socket()) < 0) {
5292 /* Set credentials to socket */
5293 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
5297 /* Get parent pid if -S, --sig-parent is specified. */
5298 if (opt_sig_parent
) {
5302 /* Setup the kernel pipe for waking up the kernel thread */
5303 if (is_root
&& !opt_no_kernel
) {
5304 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
5309 /* Setup the thread ht_cleanup communication pipe. */
5310 if (utils_create_pipe_cloexec(ht_cleanup_pipe
) < 0) {
5314 /* Setup the thread apps communication pipe. */
5315 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
5319 /* Setup the thread apps notify communication pipe. */
5320 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
) < 0) {
5324 /* Initialize global buffer per UID and PID registry. */
5325 buffer_reg_init_uid_registry();
5326 buffer_reg_init_pid_registry();
5328 /* Init UST command queue. */
5329 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
5332 * Get session list pointer. This pointer MUST NOT be free(). This list is
5333 * statically declared in session.c
5335 session_list_ptr
= session_get_list();
5337 /* Set up max poll set size */
5338 lttng_poll_set_max_size();
5342 /* Check for the application socket timeout env variable. */
5343 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
5344 if (env_app_timeout
) {
5345 app_socket_timeout
= atoi(env_app_timeout
);
5347 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
5353 /* Initialize communication library */
5355 /* This is to get the TCP timeout value. */
5356 lttcomm_inet_init();
5358 if (load_session_init_data(&load_info
) < 0) {
5361 load_info
->path
= opt_load_session_path
;
5364 * Initialize the health check subsystem. This call should set the
5365 * appropriate time values.
5367 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5368 if (!health_sessiond
) {
5369 PERROR("health_app_create error");
5370 goto exit_health_sessiond_cleanup
;
5373 /* Create thread to clean up RCU hash tables */
5374 ret
= pthread_create(&ht_cleanup_thread
, NULL
,
5375 thread_ht_cleanup
, (void *) NULL
);
5377 PERROR("pthread_create ht_cleanup");
5378 goto exit_ht_cleanup
;
5381 /* Create health-check thread */
5382 ret
= pthread_create(&health_thread
, NULL
,
5383 thread_manage_health
, (void *) NULL
);
5385 PERROR("pthread_create health");
5389 /* Create thread to manage the client socket */
5390 ret
= pthread_create(&client_thread
, NULL
,
5391 thread_manage_clients
, (void *) NULL
);
5393 PERROR("pthread_create clients");
5397 /* Create thread to dispatch registration */
5398 ret
= pthread_create(&dispatch_thread
, NULL
,
5399 thread_dispatch_ust_registration
, (void *) NULL
);
5401 PERROR("pthread_create dispatch");
5405 /* Create thread to manage application registration. */
5406 ret
= pthread_create(®_apps_thread
, NULL
,
5407 thread_registration_apps
, (void *) NULL
);
5409 PERROR("pthread_create registration");
5413 /* Create thread to manage application socket */
5414 ret
= pthread_create(&apps_thread
, NULL
,
5415 thread_manage_apps
, (void *) NULL
);
5417 PERROR("pthread_create apps");
5421 /* Create thread to manage application notify socket */
5422 ret
= pthread_create(&apps_notify_thread
, NULL
,
5423 ust_thread_manage_notify
, (void *) NULL
);
5425 PERROR("pthread_create notify");
5426 goto exit_apps_notify
;
5429 /* Create agent registration thread. */
5430 ret
= pthread_create(&agent_reg_thread
, NULL
,
5431 agent_thread_manage_registration
, (void *) NULL
);
5433 PERROR("pthread_create agent");
5434 goto exit_agent_reg
;
5437 /* Don't start this thread if kernel tracing is not requested nor root */
5438 if (is_root
&& !opt_no_kernel
) {
5439 /* Create kernel thread to manage kernel event */
5440 ret
= pthread_create(&kernel_thread
, NULL
,
5441 thread_manage_kernel
, (void *) NULL
);
5443 PERROR("pthread_create kernel");
5448 /* Load possible session(s). */
5449 start_load_session_thread();
5451 if (is_root
&& !opt_no_kernel
) {
5452 ret
= pthread_join(kernel_thread
, &status
);
5454 PERROR("pthread_join");
5455 goto error
; /* join error, exit without cleanup */
5460 ret
= pthread_join(agent_reg_thread
, &status
);
5462 PERROR("pthread_join agent");
5463 goto error
; /* join error, exit without cleanup */
5467 ret
= pthread_join(apps_notify_thread
, &status
);
5469 PERROR("pthread_join apps notify");
5470 goto error
; /* join error, exit without cleanup */
5474 ret
= pthread_join(apps_thread
, &status
);
5476 PERROR("pthread_join apps");
5477 goto error
; /* join error, exit without cleanup */
5482 ret
= pthread_join(reg_apps_thread
, &status
);
5484 PERROR("pthread_join");
5485 goto error
; /* join error, exit without cleanup */
5489 ret
= pthread_join(dispatch_thread
, &status
);
5491 PERROR("pthread_join");
5492 goto error
; /* join error, exit without cleanup */
5496 ret
= pthread_join(client_thread
, &status
);
5498 PERROR("pthread_join");
5499 goto error
; /* join error, exit without cleanup */
5502 ret
= join_consumer_thread(&kconsumer_data
);
5504 PERROR("join_consumer");
5505 goto error
; /* join error, exit without cleanup */
5508 ret
= join_consumer_thread(&ustconsumer32_data
);
5510 PERROR("join_consumer ust32");
5511 goto error
; /* join error, exit without cleanup */
5514 ret
= join_consumer_thread(&ustconsumer64_data
);
5516 PERROR("join_consumer ust64");
5517 goto error
; /* join error, exit without cleanup */
5521 ret
= pthread_join(health_thread
, &status
);
5523 PERROR("pthread_join health thread");
5524 goto error
; /* join error, exit without cleanup */
5528 ret
= pthread_join(ht_cleanup_thread
, &status
);
5530 PERROR("pthread_join ht cleanup thread");
5531 goto error
; /* join error, exit without cleanup */
5534 health_app_destroy(health_sessiond
);
5535 exit_health_sessiond_cleanup
:
5538 * cleanup() is called when no other thread is running.
5540 rcu_thread_online();
5542 rcu_thread_offline();
5543 rcu_unregister_thread();