2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <urcu/uatomic.h>
42 #include <common/common.h>
43 #include <common/compat/socket.h>
44 #include <common/defaults.h>
45 #include <common/kernel-consumer/kernel-consumer.h>
46 #include <common/futex.h>
47 #include <common/relayd/relayd.h>
48 #include <common/utils.h>
49 #include <common/daemonize.h>
50 #include <common/config/config.h>
52 #include "lttng-sessiond.h"
53 #include "buffer-registry.h"
60 #include "kernel-consumer.h"
64 #include "ust-consumer.h"
67 #include "health-sessiond.h"
68 #include "testpoint.h"
69 #include "ust-thread.h"
70 #include "jul-thread.h"
73 #define CONSUMERD_FILE "lttng-consumerd"
76 static const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
77 static int tracing_group_name_override
;
78 static char *opt_pidfile
;
79 static int opt_sig_parent
;
80 static int opt_verbose_consumer
;
81 static int opt_daemon
, opt_background
;
82 static int opt_no_kernel
;
83 static pid_t ppid
; /* Parent PID for --sig-parent option */
84 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
87 /* Set to 1 when a SIGUSR1 signal is received. */
88 static int recv_child_signal
;
91 * Consumer daemon specific control data. Every value not initialized here is
92 * set to 0 by the static definition.
94 static struct consumer_data kconsumer_data
= {
95 .type
= LTTNG_CONSUMER_KERNEL
,
96 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
97 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
100 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
101 .lock
= PTHREAD_MUTEX_INITIALIZER
,
102 .cond
= PTHREAD_COND_INITIALIZER
,
103 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
105 static struct consumer_data ustconsumer64_data
= {
106 .type
= LTTNG_CONSUMER64_UST
,
107 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
108 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
111 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
112 .lock
= PTHREAD_MUTEX_INITIALIZER
,
113 .cond
= PTHREAD_COND_INITIALIZER
,
114 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
116 static struct consumer_data ustconsumer32_data
= {
117 .type
= LTTNG_CONSUMER32_UST
,
118 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
119 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
122 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
123 .lock
= PTHREAD_MUTEX_INITIALIZER
,
124 .cond
= PTHREAD_COND_INITIALIZER
,
125 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
128 /* Command line options */
129 static const struct option long_options
[] = {
130 { "client-sock", 1, 0, 'c' },
131 { "apps-sock", 1, 0, 'a' },
132 { "kconsumerd-cmd-sock", 1, 0, 'C' },
133 { "kconsumerd-err-sock", 1, 0, 'E' },
134 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
135 { "ustconsumerd32-err-sock", 1, 0, 'H' },
136 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
137 { "ustconsumerd64-err-sock", 1, 0, 'F' },
138 { "consumerd32-path", 1, 0, 'u' },
139 { "consumerd32-libdir", 1, 0, 'U' },
140 { "consumerd64-path", 1, 0, 't' },
141 { "consumerd64-libdir", 1, 0, 'T' },
142 { "daemonize", 0, 0, 'd' },
143 { "background", 0, 0, 'b' },
144 { "sig-parent", 0, 0, 'S' },
145 { "help", 0, 0, 'h' },
146 { "group", 1, 0, 'g' },
147 { "version", 0, 0, 'V' },
148 { "quiet", 0, 0, 'q' },
149 { "verbose", 0, 0, 'v' },
150 { "verbose-consumer", 0, 0, 'Z' },
151 { "no-kernel", 0, 0, 'N' },
152 { "pidfile", 1, 0, 'p' },
153 { "jul-tcp-port", 1, 0, 'J' },
154 { "config", 1, 0, 'f' },
158 /* Command line options to ignore from configuration file */
159 static const char *config_ignore_options
[] = { "help", "version", "config" };
161 /* Shared between threads */
162 static int dispatch_thread_exit
;
164 /* Global application Unix socket path */
165 static char apps_unix_sock_path
[PATH_MAX
];
166 /* Global client Unix socket path */
167 static char client_unix_sock_path
[PATH_MAX
];
168 /* global wait shm path for UST */
169 static char wait_shm_path
[PATH_MAX
];
170 /* Global health check unix path */
171 static char health_unix_sock_path
[PATH_MAX
];
173 /* Sockets and FDs */
174 static int client_sock
= -1;
175 static int apps_sock
= -1;
176 int kernel_tracer_fd
= -1;
177 static int kernel_poll_pipe
[2] = { -1, -1 };
180 * Quit pipe for all threads. This permits a single cancellation point
181 * for all threads when receiving an event on the pipe.
183 static int thread_quit_pipe
[2] = { -1, -1 };
186 * This pipe is used to inform the thread managing application communication
187 * that a command is queued and ready to be processed.
189 static int apps_cmd_pipe
[2] = { -1, -1 };
191 int apps_cmd_notify_pipe
[2] = { -1, -1 };
193 /* Pthread, Mutexes and Semaphores */
194 static pthread_t apps_thread
;
195 static pthread_t apps_notify_thread
;
196 static pthread_t reg_apps_thread
;
197 static pthread_t client_thread
;
198 static pthread_t kernel_thread
;
199 static pthread_t dispatch_thread
;
200 static pthread_t health_thread
;
201 static pthread_t ht_cleanup_thread
;
202 static pthread_t jul_reg_thread
;
205 * UST registration command queue. This queue is tied with a futex and uses a N
206 * wakers / 1 waiter implemented and detailed in futex.c/.h
208 * The thread_registration_apps and thread_dispatch_ust_registration uses this
209 * queue along with the wait/wake scheme. The thread_manage_apps receives down
210 * the line new application socket and monitors it for any I/O error or clean
211 * close that triggers an unregistration of the application.
213 static struct ust_cmd_queue ust_cmd_queue
;
216 * Pointer initialized before thread creation.
218 * This points to the tracing session list containing the session count and a
219 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
220 * MUST NOT be taken if you call a public function in session.c.
222 * The lock is nested inside the structure: session_list_ptr->lock. Please use
223 * session_lock_list and session_unlock_list for lock acquisition.
225 static struct ltt_session_list
*session_list_ptr
;
227 int ust_consumerd64_fd
= -1;
228 int ust_consumerd32_fd
= -1;
230 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
231 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
232 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
233 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
234 static int consumerd32_bin_override
;
235 static int consumerd64_bin_override
;
236 static int consumerd32_libdir_override
;
237 static int consumerd64_libdir_override
;
239 static const char *module_proc_lttng
= "/proc/lttng";
242 * Consumer daemon state which is changed when spawning it, killing it or in
243 * case of a fatal error.
245 enum consumerd_state
{
246 CONSUMER_STARTED
= 1,
247 CONSUMER_STOPPED
= 2,
252 * This consumer daemon state is used to validate if a client command will be
253 * able to reach the consumer. If not, the client is informed. For instance,
254 * doing a "lttng start" when the consumer state is set to ERROR will return an
255 * error to the client.
257 * The following example shows a possible race condition of this scheme:
259 * consumer thread error happens
261 * client cmd checks state -> still OK
262 * consumer thread exit, sets error
263 * client cmd try to talk to consumer
266 * However, since the consumer is a different daemon, we have no way of making
267 * sure the command will reach it safely even with this state flag. This is why
268 * we consider that up to the state validation during command processing, the
269 * command is safe. After that, we can not guarantee the correctness of the
270 * client request vis-a-vis the consumer.
272 static enum consumerd_state ust_consumerd_state
;
273 static enum consumerd_state kernel_consumerd_state
;
276 * Socket timeout for receiving and sending in seconds.
278 static int app_socket_timeout
;
280 /* Set in main() with the current page size. */
283 /* Application health monitoring */
284 struct health_app
*health_sessiond
;
286 /* JUL TCP port for registration. Used by the JUL thread. */
287 unsigned int jul_tcp_port
= DEFAULT_JUL_TCP_PORT
;
289 /* Am I root or not. */
290 int is_root
; /* Set to 1 if the daemon is running as root */
292 const char * const config_section_name
= "sessiond";
295 * Whether sessiond is ready for commands/health check requests.
296 * NR_LTTNG_SESSIOND_READY must match the number of calls to
297 * lttng_sessiond_notify_ready().
299 #define NR_LTTNG_SESSIOND_READY 2
300 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_READY
;
302 /* Notify parents that we are ready for cmd and health check */
304 void lttng_sessiond_notify_ready(void)
306 if (uatomic_sub_return(<tng_sessiond_ready
, 1) == 0) {
308 * Notify parent pid that we are ready to accept command
309 * for client side. This ppid is the one from the
310 * external process that spawned us.
312 if (opt_sig_parent
) {
317 * Notify the parent of the fork() process that we are
320 if (opt_daemon
|| opt_background
) {
321 kill(child_ppid
, SIGUSR1
);
327 void setup_consumerd_path(void)
329 const char *bin
, *libdir
;
332 * Allow INSTALL_BIN_PATH to be used as a target path for the
333 * native architecture size consumer if CONFIG_CONSUMER*_PATH
334 * has not been defined.
336 #if (CAA_BITS_PER_LONG == 32)
337 if (!consumerd32_bin
[0]) {
338 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
340 if (!consumerd32_libdir
[0]) {
341 consumerd32_libdir
= INSTALL_LIB_PATH
;
343 #elif (CAA_BITS_PER_LONG == 64)
344 if (!consumerd64_bin
[0]) {
345 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
347 if (!consumerd64_libdir
[0]) {
348 consumerd64_libdir
= INSTALL_LIB_PATH
;
351 #error "Unknown bitness"
355 * runtime env. var. overrides the build default.
357 bin
= getenv("LTTNG_CONSUMERD32_BIN");
359 consumerd32_bin
= bin
;
361 bin
= getenv("LTTNG_CONSUMERD64_BIN");
363 consumerd64_bin
= bin
;
365 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
367 consumerd32_libdir
= libdir
;
369 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
371 consumerd64_libdir
= libdir
;
376 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
378 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
384 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
390 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
402 * Check if the thread quit pipe was triggered.
404 * Return 1 if it was triggered else 0;
406 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
408 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
416 * Init thread quit pipe.
418 * Return -1 on error or 0 if all pipes are created.
420 static int init_thread_quit_pipe(void)
424 ret
= pipe(thread_quit_pipe
);
426 PERROR("thread quit pipe");
430 for (i
= 0; i
< 2; i
++) {
431 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
443 * Stop all threads by closing the thread quit pipe.
445 static void stop_threads(void)
449 /* Stopping all threads */
450 DBG("Terminating all threads");
451 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
453 ERR("write error on thread quit pipe");
456 /* Dispatch thread */
457 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
458 futex_nto1_wake(&ust_cmd_queue
.futex
);
462 * Close every consumer sockets.
464 static void close_consumer_sockets(void)
468 if (kconsumer_data
.err_sock
>= 0) {
469 ret
= close(kconsumer_data
.err_sock
);
471 PERROR("kernel consumer err_sock close");
474 if (ustconsumer32_data
.err_sock
>= 0) {
475 ret
= close(ustconsumer32_data
.err_sock
);
477 PERROR("UST consumerd32 err_sock close");
480 if (ustconsumer64_data
.err_sock
>= 0) {
481 ret
= close(ustconsumer64_data
.err_sock
);
483 PERROR("UST consumerd64 err_sock close");
486 if (kconsumer_data
.cmd_sock
>= 0) {
487 ret
= close(kconsumer_data
.cmd_sock
);
489 PERROR("kernel consumer cmd_sock close");
492 if (ustconsumer32_data
.cmd_sock
>= 0) {
493 ret
= close(ustconsumer32_data
.cmd_sock
);
495 PERROR("UST consumerd32 cmd_sock close");
498 if (ustconsumer64_data
.cmd_sock
>= 0) {
499 ret
= close(ustconsumer64_data
.cmd_sock
);
501 PERROR("UST consumerd64 cmd_sock close");
509 static void cleanup(void)
512 struct ltt_session
*sess
, *stmp
;
518 * Close the thread quit pipe. It has already done its job,
519 * since we are now called.
521 utils_close_pipe(thread_quit_pipe
);
524 * If opt_pidfile is undefined, the default file will be wiped when
525 * removing the rundir.
528 ret
= remove(opt_pidfile
);
530 PERROR("remove pidfile %s", opt_pidfile
);
534 DBG("Removing sessiond and consumerd content of directory %s", rundir
);
537 snprintf(path
, PATH_MAX
,
539 rundir
, DEFAULT_LTTNG_SESSIOND_PIDFILE
);
540 DBG("Removing %s", path
);
543 snprintf(path
, PATH_MAX
, "%s/%s", rundir
,
544 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE
);
545 DBG("Removing %s", path
);
549 snprintf(path
, PATH_MAX
,
550 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
552 DBG("Removing %s", path
);
555 snprintf(path
, PATH_MAX
,
556 DEFAULT_KCONSUMERD_PATH
,
558 DBG("Removing directory %s", path
);
561 /* ust consumerd 32 */
562 snprintf(path
, PATH_MAX
,
563 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
565 DBG("Removing %s", path
);
568 snprintf(path
, PATH_MAX
,
569 DEFAULT_USTCONSUMERD32_PATH
,
571 DBG("Removing directory %s", path
);
574 /* ust consumerd 64 */
575 snprintf(path
, PATH_MAX
,
576 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
578 DBG("Removing %s", path
);
581 snprintf(path
, PATH_MAX
,
582 DEFAULT_USTCONSUMERD64_PATH
,
584 DBG("Removing directory %s", path
);
588 * We do NOT rmdir rundir because there are other processes
589 * using it, for instance lttng-relayd, which can start in
590 * parallel with this teardown.
595 DBG("Cleaning up all sessions");
597 /* Destroy session list mutex */
598 if (session_list_ptr
!= NULL
) {
599 pthread_mutex_destroy(&session_list_ptr
->lock
);
601 /* Cleanup ALL session */
602 cds_list_for_each_entry_safe(sess
, stmp
,
603 &session_list_ptr
->head
, list
) {
604 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
608 DBG("Closing all UST sockets");
609 ust_app_clean_list();
610 buffer_reg_destroy_registries();
612 if (is_root
&& !opt_no_kernel
) {
613 DBG2("Closing kernel fd");
614 if (kernel_tracer_fd
>= 0) {
615 ret
= close(kernel_tracer_fd
);
620 DBG("Unloading kernel modules");
621 modprobe_remove_lttng_all();
624 close_consumer_sockets();
627 * If the override option is set, the pointer points to a *non* const thus
628 * freeing it even though the variable type is set to const.
630 if (tracing_group_name_override
) {
631 free((void *) tracing_group_name
);
633 if (consumerd32_bin_override
) {
634 free((void *) consumerd32_bin
);
636 if (consumerd64_bin_override
) {
637 free((void *) consumerd64_bin
);
639 if (consumerd32_libdir_override
) {
640 free((void *) consumerd32_libdir
);
642 if (consumerd64_libdir_override
) {
643 free((void *) consumerd64_libdir
);
651 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
652 "Matthew, BEET driven development works!%c[%dm",
653 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
658 * Send data on a unix socket using the liblttsessiondcomm API.
660 * Return lttcomm error code.
662 static int send_unix_sock(int sock
, void *buf
, size_t len
)
664 /* Check valid length */
669 return lttcomm_send_unix_sock(sock
, buf
, len
);
673 * Free memory of a command context structure.
675 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
677 DBG("Clean command context structure");
679 if ((*cmd_ctx
)->llm
) {
680 free((*cmd_ctx
)->llm
);
682 if ((*cmd_ctx
)->lsm
) {
683 free((*cmd_ctx
)->lsm
);
691 * Notify UST applications using the shm mmap futex.
693 static int notify_ust_apps(int active
)
697 DBG("Notifying applications of session daemon state: %d", active
);
699 /* See shm.c for this call implying mmap, shm and futex calls */
700 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
701 if (wait_shm_mmap
== NULL
) {
705 /* Wake waiting process */
706 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
708 /* Apps notified successfully */
716 * Setup the outgoing data buffer for the response (llm) by allocating the
717 * right amount of memory and copying the original information from the lsm
720 * Return total size of the buffer pointed by buf.
722 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
728 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
729 if (cmd_ctx
->llm
== NULL
) {
735 /* Copy common data */
736 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
737 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
739 cmd_ctx
->llm
->data_size
= size
;
740 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
749 * Update the kernel poll set of all channel fd available over all tracing
750 * session. Add the wakeup pipe at the end of the set.
752 static int update_kernel_poll(struct lttng_poll_event
*events
)
755 struct ltt_session
*session
;
756 struct ltt_kernel_channel
*channel
;
758 DBG("Updating kernel poll set");
761 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
762 session_lock(session
);
763 if (session
->kernel_session
== NULL
) {
764 session_unlock(session
);
768 cds_list_for_each_entry(channel
,
769 &session
->kernel_session
->channel_list
.head
, list
) {
770 /* Add channel fd to the kernel poll set */
771 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
773 session_unlock(session
);
776 DBG("Channel fd %d added to kernel set", channel
->fd
);
778 session_unlock(session
);
780 session_unlock_list();
785 session_unlock_list();
790 * Find the channel fd from 'fd' over all tracing session. When found, check
791 * for new channel stream and send those stream fds to the kernel consumer.
793 * Useful for CPU hotplug feature.
795 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
798 struct ltt_session
*session
;
799 struct ltt_kernel_session
*ksess
;
800 struct ltt_kernel_channel
*channel
;
802 DBG("Updating kernel streams for channel fd %d", fd
);
805 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
806 session_lock(session
);
807 if (session
->kernel_session
== NULL
) {
808 session_unlock(session
);
811 ksess
= session
->kernel_session
;
813 cds_list_for_each_entry(channel
, &ksess
->channel_list
.head
, list
) {
814 if (channel
->fd
== fd
) {
815 DBG("Channel found, updating kernel streams");
816 ret
= kernel_open_channel_stream(channel
);
820 /* Update the stream global counter */
821 ksess
->stream_count_global
+= ret
;
824 * Have we already sent fds to the consumer? If yes, it means
825 * that tracing is started so it is safe to send our updated
828 if (ksess
->consumer_fds_sent
== 1 && ksess
->consumer
!= NULL
) {
829 struct lttng_ht_iter iter
;
830 struct consumer_socket
*socket
;
833 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
834 &iter
.iter
, socket
, node
.node
) {
835 pthread_mutex_lock(socket
->lock
);
836 ret
= kernel_consumer_send_channel_stream(socket
,
838 session
->output_traces
? 1 : 0);
839 pthread_mutex_unlock(socket
->lock
);
850 session_unlock(session
);
852 session_unlock_list();
856 session_unlock(session
);
857 session_unlock_list();
862 * For each tracing session, update newly registered apps. The session list
863 * lock MUST be acquired before calling this.
865 static void update_ust_app(int app_sock
)
867 struct ltt_session
*sess
, *stmp
;
869 /* Consumer is in an ERROR state. Stop any application update. */
870 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
871 /* Stop the update process since the consumer is dead. */
875 /* For all tracing session(s) */
876 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
878 if (sess
->ust_session
) {
879 ust_app_global_update(sess
->ust_session
, app_sock
);
881 session_unlock(sess
);
886 * This thread manage event coming from the kernel.
888 * Features supported in this thread:
891 static void *thread_manage_kernel(void *data
)
893 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
894 uint32_t revents
, nb_fd
;
896 struct lttng_poll_event events
;
898 DBG("[thread] Thread manage kernel started");
900 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
903 * This first step of the while is to clean this structure which could free
904 * non NULL pointers so initialize it before the loop.
906 lttng_poll_init(&events
);
908 if (testpoint(sessiond_thread_manage_kernel
)) {
909 goto error_testpoint
;
912 health_code_update();
914 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
915 goto error_testpoint
;
919 health_code_update();
921 if (update_poll_flag
== 1) {
922 /* Clean events object. We are about to populate it again. */
923 lttng_poll_clean(&events
);
925 ret
= sessiond_set_thread_pollset(&events
, 2);
927 goto error_poll_create
;
930 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
935 /* This will add the available kernel channel if any. */
936 ret
= update_kernel_poll(&events
);
940 update_poll_flag
= 0;
943 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events
));
945 /* Poll infinite value of time */
948 ret
= lttng_poll_wait(&events
, -1);
952 * Restart interrupted system call.
954 if (errno
== EINTR
) {
958 } else if (ret
== 0) {
959 /* Should not happen since timeout is infinite */
960 ERR("Return value of poll is 0 with an infinite timeout.\n"
961 "This should not have happened! Continuing...");
967 for (i
= 0; i
< nb_fd
; i
++) {
968 /* Fetch once the poll data */
969 revents
= LTTNG_POLL_GETEV(&events
, i
);
970 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
972 health_code_update();
974 /* Thread quit pipe has been closed. Killing thread. */
975 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
981 /* Check for data on kernel pipe */
982 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
983 (void) lttng_read(kernel_poll_pipe
[0],
986 * Ret value is useless here, if this pipe gets any actions an
987 * update is required anyway.
989 update_poll_flag
= 1;
993 * New CPU detected by the kernel. Adding kernel stream to
994 * kernel session and updating the kernel consumer
996 if (revents
& LPOLLIN
) {
997 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1003 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
1004 * and unregister kernel stream at this point.
1013 lttng_poll_clean(&events
);
1016 utils_close_pipe(kernel_poll_pipe
);
1017 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1020 ERR("Health error occurred in %s", __func__
);
1021 WARN("Kernel thread died unexpectedly. "
1022 "Kernel tracing can continue but CPU hotplug is disabled.");
1024 health_unregister(health_sessiond
);
1025 DBG("Kernel thread dying");
1030 * Signal pthread condition of the consumer data that the thread.
1032 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1034 pthread_mutex_lock(&data
->cond_mutex
);
1037 * The state is set before signaling. It can be any value, it's the waiter
1038 * job to correctly interpret this condition variable associated to the
1039 * consumer pthread_cond.
1041 * A value of 0 means that the corresponding thread of the consumer data
1042 * was not started. 1 indicates that the thread has started and is ready
1043 * for action. A negative value means that there was an error during the
1046 data
->consumer_thread_is_ready
= state
;
1047 (void) pthread_cond_signal(&data
->cond
);
1049 pthread_mutex_unlock(&data
->cond_mutex
);
1053 * This thread manage the consumer error sent back to the session daemon.
1055 static void *thread_manage_consumer(void *data
)
1057 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1058 uint32_t revents
, nb_fd
;
1059 enum lttcomm_return_code code
;
1060 struct lttng_poll_event events
;
1061 struct consumer_data
*consumer_data
= data
;
1063 DBG("[thread] Manage consumer started");
1065 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1067 health_code_update();
1070 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1071 * metadata_sock. Nothing more will be added to this poll set.
1073 ret
= sessiond_set_thread_pollset(&events
, 3);
1079 * The error socket here is already in a listening state which was done
1080 * just before spawning this thread to avoid a race between the consumer
1081 * daemon exec trying to connect and the listen() call.
1083 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1088 health_code_update();
1090 /* Infinite blocking call, waiting for transmission */
1092 health_poll_entry();
1094 if (testpoint(sessiond_thread_manage_consumer
)) {
1098 ret
= lttng_poll_wait(&events
, -1);
1102 * Restart interrupted system call.
1104 if (errno
== EINTR
) {
1112 for (i
= 0; i
< nb_fd
; i
++) {
1113 /* Fetch once the poll data */
1114 revents
= LTTNG_POLL_GETEV(&events
, i
);
1115 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1117 health_code_update();
1119 /* Thread quit pipe has been closed. Killing thread. */
1120 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1126 /* Event on the registration socket */
1127 if (pollfd
== consumer_data
->err_sock
) {
1128 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1129 ERR("consumer err socket poll error");
1135 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1141 * Set the CLOEXEC flag. Return code is useless because either way, the
1144 (void) utils_set_fd_cloexec(sock
);
1146 health_code_update();
1148 DBG2("Receiving code from consumer err_sock");
1150 /* Getting status code from kconsumerd */
1151 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1152 sizeof(enum lttcomm_return_code
));
1157 health_code_update();
1158 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1159 /* Connect both socket, command and metadata. */
1160 consumer_data
->cmd_sock
=
1161 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1162 consumer_data
->metadata_fd
=
1163 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1164 if (consumer_data
->cmd_sock
< 0
1165 || consumer_data
->metadata_fd
< 0) {
1166 PERROR("consumer connect cmd socket");
1167 /* On error, signal condition and quit. */
1168 signal_consumer_condition(consumer_data
, -1);
1171 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1172 /* Create metadata socket lock. */
1173 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1174 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1175 PERROR("zmalloc pthread mutex");
1179 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1181 signal_consumer_condition(consumer_data
, 1);
1182 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1183 DBG("Consumer metadata socket ready (fd: %d)",
1184 consumer_data
->metadata_fd
);
1186 ERR("consumer error when waiting for SOCK_READY : %s",
1187 lttcomm_get_readable_code(-code
));
1191 /* Remove the consumerd error sock since we've established a connexion */
1192 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1197 /* Add new accepted error socket. */
1198 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1203 /* Add metadata socket that is successfully connected. */
1204 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1205 LPOLLIN
| LPOLLRDHUP
);
1210 health_code_update();
1212 /* Infinite blocking call, waiting for transmission */
1215 health_poll_entry();
1216 ret
= lttng_poll_wait(&events
, -1);
1220 * Restart interrupted system call.
1222 if (errno
== EINTR
) {
1230 for (i
= 0; i
< nb_fd
; i
++) {
1231 /* Fetch once the poll data */
1232 revents
= LTTNG_POLL_GETEV(&events
, i
);
1233 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1235 health_code_update();
1237 /* Thread quit pipe has been closed. Killing thread. */
1238 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1244 if (pollfd
== sock
) {
1245 /* Event on the consumerd socket */
1246 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1247 ERR("consumer err socket second poll error");
1250 health_code_update();
1251 /* Wait for any kconsumerd error */
1252 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1253 sizeof(enum lttcomm_return_code
));
1255 ERR("consumer closed the command socket");
1259 ERR("consumer return code : %s",
1260 lttcomm_get_readable_code(-code
));
1263 } else if (pollfd
== consumer_data
->metadata_fd
) {
1264 /* UST metadata requests */
1265 ret
= ust_consumer_metadata_request(
1266 &consumer_data
->metadata_sock
);
1268 ERR("Handling metadata request");
1273 ERR("Unknown pollfd");
1277 health_code_update();
1283 * We lock here because we are about to close the sockets and some other
1284 * thread might be using them so get exclusive access which will abort all
1285 * other consumer command by other threads.
1287 pthread_mutex_lock(&consumer_data
->lock
);
1289 /* Immediately set the consumerd state to stopped */
1290 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1291 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1292 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1293 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1294 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1296 /* Code flow error... */
1300 if (consumer_data
->err_sock
>= 0) {
1301 ret
= close(consumer_data
->err_sock
);
1305 consumer_data
->err_sock
= -1;
1307 if (consumer_data
->cmd_sock
>= 0) {
1308 ret
= close(consumer_data
->cmd_sock
);
1312 consumer_data
->cmd_sock
= -1;
1314 if (consumer_data
->metadata_sock
.fd_ptr
&&
1315 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1316 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1328 unlink(consumer_data
->err_unix_sock_path
);
1329 unlink(consumer_data
->cmd_unix_sock_path
);
1330 consumer_data
->pid
= 0;
1331 pthread_mutex_unlock(&consumer_data
->lock
);
1333 /* Cleanup metadata socket mutex. */
1334 if (consumer_data
->metadata_sock
.lock
) {
1335 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1336 free(consumer_data
->metadata_sock
.lock
);
1338 lttng_poll_clean(&events
);
1342 ERR("Health error occurred in %s", __func__
);
1344 health_unregister(health_sessiond
);
1345 DBG("consumer thread cleanup completed");
1351 * This thread manage application communication.
1353 static void *thread_manage_apps(void *data
)
1355 int i
, ret
, pollfd
, err
= -1;
1357 uint32_t revents
, nb_fd
;
1358 struct lttng_poll_event events
;
1360 DBG("[thread] Manage application started");
1362 rcu_register_thread();
1363 rcu_thread_online();
1365 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1367 if (testpoint(sessiond_thread_manage_apps
)) {
1368 goto error_testpoint
;
1371 health_code_update();
1373 ret
= sessiond_set_thread_pollset(&events
, 2);
1375 goto error_poll_create
;
1378 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1383 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1387 health_code_update();
1390 DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events
));
1392 /* Inifinite blocking call, waiting for transmission */
1394 health_poll_entry();
1395 ret
= lttng_poll_wait(&events
, -1);
1399 * Restart interrupted system call.
1401 if (errno
== EINTR
) {
1409 for (i
= 0; i
< nb_fd
; i
++) {
1410 /* Fetch once the poll data */
1411 revents
= LTTNG_POLL_GETEV(&events
, i
);
1412 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1414 health_code_update();
1416 /* Thread quit pipe has been closed. Killing thread. */
1417 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1423 /* Inspect the apps cmd pipe */
1424 if (pollfd
== apps_cmd_pipe
[0]) {
1425 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1426 ERR("Apps command pipe error");
1428 } else if (revents
& LPOLLIN
) {
1432 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1433 if (size_ret
< sizeof(sock
)) {
1434 PERROR("read apps cmd pipe");
1438 health_code_update();
1441 * We only monitor the error events of the socket. This
1442 * thread does not handle any incoming data from UST
1445 ret
= lttng_poll_add(&events
, sock
,
1446 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1451 DBG("Apps with sock %d added to poll set", sock
);
1455 * At this point, we know that a registered application made
1456 * the event at poll_wait.
1458 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1459 /* Removing from the poll set */
1460 ret
= lttng_poll_del(&events
, pollfd
);
1465 /* Socket closed on remote end. */
1466 ust_app_unregister(pollfd
);
1470 health_code_update();
1476 lttng_poll_clean(&events
);
1479 utils_close_pipe(apps_cmd_pipe
);
1480 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1483 * We don't clean the UST app hash table here since already registered
1484 * applications can still be controlled so let them be until the session
1485 * daemon dies or the applications stop.
1490 ERR("Health error occurred in %s", __func__
);
1492 health_unregister(health_sessiond
);
1493 DBG("Application communication apps thread cleanup complete");
1494 rcu_thread_offline();
1495 rcu_unregister_thread();
1500 * Send a socket to a thread This is called from the dispatch UST registration
1501 * thread once all sockets are set for the application.
1503 * The sock value can be invalid, we don't really care, the thread will handle
1504 * it and make the necessary cleanup if so.
1506 * On success, return 0 else a negative value being the errno message of the
1509 static int send_socket_to_thread(int fd
, int sock
)
1514 * It's possible that the FD is set as invalid with -1 concurrently just
1515 * before calling this function being a shutdown state of the thread.
1522 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1523 if (ret
< sizeof(sock
)) {
1524 PERROR("write apps pipe %d", fd
);
1531 /* All good. Don't send back the write positive ret value. */
1538 * Sanitize the wait queue of the dispatch registration thread meaning removing
1539 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1540 * notify socket is never received.
1542 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1544 int ret
, nb_fd
= 0, i
;
1545 unsigned int fd_added
= 0;
1546 struct lttng_poll_event events
;
1547 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1551 lttng_poll_init(&events
);
1553 /* Just skip everything for an empty queue. */
1554 if (!wait_queue
->count
) {
1558 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1563 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1564 &wait_queue
->head
, head
) {
1565 assert(wait_node
->app
);
1566 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1567 LPOLLHUP
| LPOLLERR
);
1580 * Poll but don't block so we can quickly identify the faulty events and
1581 * clean them afterwards from the wait queue.
1583 ret
= lttng_poll_wait(&events
, 0);
1589 for (i
= 0; i
< nb_fd
; i
++) {
1590 /* Get faulty FD. */
1591 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1592 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1594 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1595 &wait_queue
->head
, head
) {
1596 if (pollfd
== wait_node
->app
->sock
&&
1597 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1598 cds_list_del(&wait_node
->head
);
1599 wait_queue
->count
--;
1600 ust_app_destroy(wait_node
->app
);
1608 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1612 lttng_poll_clean(&events
);
1616 lttng_poll_clean(&events
);
1618 ERR("Unable to sanitize wait queue");
1623 * Dispatch request from the registration threads to the application
1624 * communication thread.
1626 static void *thread_dispatch_ust_registration(void *data
)
1629 struct cds_wfq_node
*node
;
1630 struct ust_command
*ust_cmd
= NULL
;
1631 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1632 struct ust_reg_wait_queue wait_queue
= {
1636 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1638 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1639 goto error_testpoint
;
1642 health_code_update();
1644 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1646 DBG("[thread] Dispatch UST command started");
1648 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1649 health_code_update();
1651 /* Atomically prepare the queue futex */
1652 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1655 struct ust_app
*app
= NULL
;
1659 * Make sure we don't have node(s) that have hung up before receiving
1660 * the notify socket. This is to clean the list in order to avoid
1661 * memory leaks from notify socket that are never seen.
1663 sanitize_wait_queue(&wait_queue
);
1665 health_code_update();
1666 /* Dequeue command for registration */
1667 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1669 DBG("Woken up but nothing in the UST command queue");
1670 /* Continue thread execution */
1674 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1676 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1677 " gid:%d sock:%d name:%s (version %d.%d)",
1678 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1679 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1680 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1681 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1683 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1684 wait_node
= zmalloc(sizeof(*wait_node
));
1686 PERROR("zmalloc wait_node dispatch");
1687 ret
= close(ust_cmd
->sock
);
1689 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1691 lttng_fd_put(LTTNG_FD_APPS
, 1);
1695 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1697 /* Create application object if socket is CMD. */
1698 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1700 if (!wait_node
->app
) {
1701 ret
= close(ust_cmd
->sock
);
1703 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1705 lttng_fd_put(LTTNG_FD_APPS
, 1);
1711 * Add application to the wait queue so we can set the notify
1712 * socket before putting this object in the global ht.
1714 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1719 * We have to continue here since we don't have the notify
1720 * socket and the application MUST be added to the hash table
1721 * only at that moment.
1726 * Look for the application in the local wait queue and set the
1727 * notify socket if found.
1729 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1730 &wait_queue
.head
, head
) {
1731 health_code_update();
1732 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1733 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1734 cds_list_del(&wait_node
->head
);
1736 app
= wait_node
->app
;
1738 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1744 * With no application at this stage the received socket is
1745 * basically useless so close it before we free the cmd data
1746 * structure for good.
1749 ret
= close(ust_cmd
->sock
);
1751 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1753 lttng_fd_put(LTTNG_FD_APPS
, 1);
1760 * @session_lock_list
1762 * Lock the global session list so from the register up to the
1763 * registration done message, no thread can see the application
1764 * and change its state.
1766 session_lock_list();
1770 * Add application to the global hash table. This needs to be
1771 * done before the update to the UST registry can locate the
1776 /* Set app version. This call will print an error if needed. */
1777 (void) ust_app_version(app
);
1779 /* Send notify socket through the notify pipe. */
1780 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1784 session_unlock_list();
1786 * No notify thread, stop the UST tracing. However, this is
1787 * not an internal error of the this thread thus setting
1788 * the health error code to a normal exit.
1795 * Update newly registered application with the tracing
1796 * registry info already enabled information.
1798 update_ust_app(app
->sock
);
1801 * Don't care about return value. Let the manage apps threads
1802 * handle app unregistration upon socket close.
1804 (void) ust_app_register_done(app
->sock
);
1807 * Even if the application socket has been closed, send the app
1808 * to the thread and unregistration will take place at that
1811 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1814 session_unlock_list();
1816 * No apps. thread, stop the UST tracing. However, this is
1817 * not an internal error of the this thread thus setting
1818 * the health error code to a normal exit.
1825 session_unlock_list();
1827 } while (node
!= NULL
);
1829 health_poll_entry();
1830 /* Futex wait on queue. Blocking call on futex() */
1831 futex_nto1_wait(&ust_cmd_queue
.futex
);
1834 /* Normal exit, no error */
1838 /* Clean up wait queue. */
1839 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1840 &wait_queue
.head
, head
) {
1841 cds_list_del(&wait_node
->head
);
1847 DBG("Dispatch thread dying");
1850 ERR("Health error occurred in %s", __func__
);
1852 health_unregister(health_sessiond
);
1857 * This thread manage application registration.
1859 static void *thread_registration_apps(void *data
)
1861 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1862 uint32_t revents
, nb_fd
;
1863 struct lttng_poll_event events
;
1865 * Get allocated in this thread, enqueued to a global queue, dequeued and
1866 * freed in the manage apps thread.
1868 struct ust_command
*ust_cmd
= NULL
;
1870 DBG("[thread] Manage application registration started");
1872 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
1874 if (testpoint(sessiond_thread_registration_apps
)) {
1875 goto error_testpoint
;
1878 ret
= lttcomm_listen_unix_sock(apps_sock
);
1884 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1885 * more will be added to this poll set.
1887 ret
= sessiond_set_thread_pollset(&events
, 2);
1889 goto error_create_poll
;
1892 /* Add the application registration socket */
1893 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1895 goto error_poll_add
;
1898 /* Notify all applications to register */
1899 ret
= notify_ust_apps(1);
1901 ERR("Failed to notify applications or create the wait shared memory.\n"
1902 "Execution continues but there might be problem for already\n"
1903 "running applications that wishes to register.");
1907 DBG("Accepting application registration");
1909 /* Inifinite blocking call, waiting for transmission */
1911 health_poll_entry();
1912 ret
= lttng_poll_wait(&events
, -1);
1916 * Restart interrupted system call.
1918 if (errno
== EINTR
) {
1926 for (i
= 0; i
< nb_fd
; i
++) {
1927 health_code_update();
1929 /* Fetch once the poll data */
1930 revents
= LTTNG_POLL_GETEV(&events
, i
);
1931 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1933 /* Thread quit pipe has been closed. Killing thread. */
1934 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1940 /* Event on the registration socket */
1941 if (pollfd
== apps_sock
) {
1942 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1943 ERR("Register apps socket poll error");
1945 } else if (revents
& LPOLLIN
) {
1946 sock
= lttcomm_accept_unix_sock(apps_sock
);
1952 * Set socket timeout for both receiving and ending.
1953 * app_socket_timeout is in seconds, whereas
1954 * lttcomm_setsockopt_rcv_timeout and
1955 * lttcomm_setsockopt_snd_timeout expect msec as
1958 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1959 app_socket_timeout
* 1000);
1960 (void) lttcomm_setsockopt_snd_timeout(sock
,
1961 app_socket_timeout
* 1000);
1964 * Set the CLOEXEC flag. Return code is useless because
1965 * either way, the show must go on.
1967 (void) utils_set_fd_cloexec(sock
);
1969 /* Create UST registration command for enqueuing */
1970 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1971 if (ust_cmd
== NULL
) {
1972 PERROR("ust command zmalloc");
1977 * Using message-based transmissions to ensure we don't
1978 * have to deal with partially received messages.
1980 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1982 ERR("Exhausted file descriptors allowed for applications.");
1992 health_code_update();
1993 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
1996 /* Close socket of the application. */
2001 lttng_fd_put(LTTNG_FD_APPS
, 1);
2005 health_code_update();
2007 ust_cmd
->sock
= sock
;
2010 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2011 " gid:%d sock:%d name:%s (version %d.%d)",
2012 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2013 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2014 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2015 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2018 * Lock free enqueue the registration request. The red pill
2019 * has been taken! This apps will be part of the *system*.
2021 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
2024 * Wake the registration queue futex. Implicit memory
2025 * barrier with the exchange in cds_wfq_enqueue.
2027 futex_nto1_wake(&ust_cmd_queue
.futex
);
2035 /* Notify that the registration thread is gone */
2038 if (apps_sock
>= 0) {
2039 ret
= close(apps_sock
);
2049 lttng_fd_put(LTTNG_FD_APPS
, 1);
2051 unlink(apps_unix_sock_path
);
2054 lttng_poll_clean(&events
);
2058 DBG("UST Registration thread cleanup complete");
2061 ERR("Health error occurred in %s", __func__
);
2063 health_unregister(health_sessiond
);
2069 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2070 * exec or it will fails.
2072 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2075 struct timespec timeout
;
2077 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2078 consumer_data
->consumer_thread_is_ready
= 0;
2080 /* Setup pthread condition */
2081 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2084 PERROR("pthread_condattr_init consumer data");
2089 * Set the monotonic clock in order to make sure we DO NOT jump in time
2090 * between the clock_gettime() call and the timedwait call. See bug #324
2091 * for a more details and how we noticed it.
2093 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2096 PERROR("pthread_condattr_setclock consumer data");
2100 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2103 PERROR("pthread_cond_init consumer data");
2107 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
2110 PERROR("pthread_create consumer");
2115 /* We are about to wait on a pthread condition */
2116 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2118 /* Get time for sem_timedwait absolute timeout */
2119 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2121 * Set the timeout for the condition timed wait even if the clock gettime
2122 * call fails since we might loop on that call and we want to avoid to
2123 * increment the timeout too many times.
2125 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2128 * The following loop COULD be skipped in some conditions so this is why we
2129 * set ret to 0 in order to make sure at least one round of the loop is
2135 * Loop until the condition is reached or when a timeout is reached. Note
2136 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2137 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2138 * possible. This loop does not take any chances and works with both of
2141 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2142 if (clock_ret
< 0) {
2143 PERROR("clock_gettime spawn consumer");
2144 /* Infinite wait for the consumerd thread to be ready */
2145 ret
= pthread_cond_wait(&consumer_data
->cond
,
2146 &consumer_data
->cond_mutex
);
2148 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2149 &consumer_data
->cond_mutex
, &timeout
);
2153 /* Release the pthread condition */
2154 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2158 if (ret
== ETIMEDOUT
) {
2162 * Call has timed out so we kill the kconsumerd_thread and return
2165 ERR("Condition timed out. The consumer thread was never ready."
2167 pth_ret
= pthread_cancel(consumer_data
->thread
);
2169 PERROR("pthread_cancel consumer thread");
2172 PERROR("pthread_cond_wait failed consumer thread");
2174 /* Caller is expecting a negative value on failure. */
2179 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2180 if (consumer_data
->pid
== 0) {
2181 ERR("Consumerd did not start");
2182 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2185 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2194 * Join consumer thread
2196 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2200 /* Consumer pid must be a real one. */
2201 if (consumer_data
->pid
> 0) {
2203 ret
= kill(consumer_data
->pid
, SIGTERM
);
2205 ERR("Error killing consumer daemon");
2208 return pthread_join(consumer_data
->thread
, &status
);
2215 * Fork and exec a consumer daemon (consumerd).
2217 * Return pid if successful else -1.
2219 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2223 const char *consumer_to_use
;
2224 const char *verbosity
;
2227 DBG("Spawning consumerd");
2234 if (opt_verbose_consumer
) {
2235 verbosity
= "--verbose";
2236 } else if (lttng_opt_quiet
) {
2237 verbosity
= "--quiet";
2242 switch (consumer_data
->type
) {
2243 case LTTNG_CONSUMER_KERNEL
:
2245 * Find out which consumerd to execute. We will first try the
2246 * 64-bit path, then the sessiond's installation directory, and
2247 * fallback on the 32-bit one,
2249 DBG3("Looking for a kernel consumer at these locations:");
2250 DBG3(" 1) %s", consumerd64_bin
);
2251 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2252 DBG3(" 3) %s", consumerd32_bin
);
2253 if (stat(consumerd64_bin
, &st
) == 0) {
2254 DBG3("Found location #1");
2255 consumer_to_use
= consumerd64_bin
;
2256 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2257 DBG3("Found location #2");
2258 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2259 } else if (stat(consumerd32_bin
, &st
) == 0) {
2260 DBG3("Found location #3");
2261 consumer_to_use
= consumerd32_bin
;
2263 DBG("Could not find any valid consumerd executable");
2267 DBG("Using kernel consumer at: %s", consumer_to_use
);
2268 ret
= execl(consumer_to_use
,
2269 "lttng-consumerd", verbosity
, "-k",
2270 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2271 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2272 "--group", tracing_group_name
,
2275 case LTTNG_CONSUMER64_UST
:
2277 char *tmpnew
= NULL
;
2279 if (consumerd64_libdir
[0] != '\0') {
2283 tmp
= getenv("LD_LIBRARY_PATH");
2287 tmplen
= strlen("LD_LIBRARY_PATH=")
2288 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2289 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2294 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2295 strcat(tmpnew
, consumerd64_libdir
);
2296 if (tmp
[0] != '\0') {
2297 strcat(tmpnew
, ":");
2298 strcat(tmpnew
, tmp
);
2300 ret
= putenv(tmpnew
);
2307 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2308 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2309 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2310 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2311 "--group", tracing_group_name
,
2313 if (consumerd64_libdir
[0] != '\0') {
2318 case LTTNG_CONSUMER32_UST
:
2320 char *tmpnew
= NULL
;
2322 if (consumerd32_libdir
[0] != '\0') {
2326 tmp
= getenv("LD_LIBRARY_PATH");
2330 tmplen
= strlen("LD_LIBRARY_PATH=")
2331 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2332 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2337 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2338 strcat(tmpnew
, consumerd32_libdir
);
2339 if (tmp
[0] != '\0') {
2340 strcat(tmpnew
, ":");
2341 strcat(tmpnew
, tmp
);
2343 ret
= putenv(tmpnew
);
2350 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2351 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2352 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2353 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2354 "--group", tracing_group_name
,
2356 if (consumerd32_libdir
[0] != '\0') {
2362 PERROR("unknown consumer type");
2366 PERROR("Consumer execl()");
2368 /* Reaching this point, we got a failure on our execl(). */
2370 } else if (pid
> 0) {
2373 PERROR("start consumer fork");
2381 * Spawn the consumerd daemon and session daemon thread.
2383 static int start_consumerd(struct consumer_data
*consumer_data
)
2388 * Set the listen() state on the socket since there is a possible race
2389 * between the exec() of the consumer daemon and this call if place in the
2390 * consumer thread. See bug #366 for more details.
2392 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2397 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2398 if (consumer_data
->pid
!= 0) {
2399 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2403 ret
= spawn_consumerd(consumer_data
);
2405 ERR("Spawning consumerd failed");
2406 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2410 /* Setting up the consumer_data pid */
2411 consumer_data
->pid
= ret
;
2412 DBG2("Consumer pid %d", consumer_data
->pid
);
2413 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2415 DBG2("Spawning consumer control thread");
2416 ret
= spawn_consumer_thread(consumer_data
);
2418 ERR("Fatal error spawning consumer control thread");
2426 /* Cleanup already created sockets on error. */
2427 if (consumer_data
->err_sock
>= 0) {
2430 err
= close(consumer_data
->err_sock
);
2432 PERROR("close consumer data error socket");
2439 * Setup necessary data for kernel tracer action.
2441 static int init_kernel_tracer(void)
2445 /* Modprobe lttng kernel modules */
2446 ret
= modprobe_lttng_control();
2451 /* Open debugfs lttng */
2452 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2453 if (kernel_tracer_fd
< 0) {
2454 DBG("Failed to open %s", module_proc_lttng
);
2459 /* Validate kernel version */
2460 ret
= kernel_validate_version(kernel_tracer_fd
);
2465 ret
= modprobe_lttng_data();
2470 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2474 modprobe_remove_lttng_control();
2475 ret
= close(kernel_tracer_fd
);
2479 kernel_tracer_fd
= -1;
2480 return LTTNG_ERR_KERN_VERSION
;
2483 ret
= close(kernel_tracer_fd
);
2489 modprobe_remove_lttng_control();
2492 WARN("No kernel tracer available");
2493 kernel_tracer_fd
= -1;
2495 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2497 return LTTNG_ERR_KERN_NA
;
2503 * Copy consumer output from the tracing session to the domain session. The
2504 * function also applies the right modification on a per domain basis for the
2505 * trace files destination directory.
2507 * Should *NOT* be called with RCU read-side lock held.
2509 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2512 const char *dir_name
;
2513 struct consumer_output
*consumer
;
2516 assert(session
->consumer
);
2519 case LTTNG_DOMAIN_KERNEL
:
2520 DBG3("Copying tracing session consumer output in kernel session");
2522 * XXX: We should audit the session creation and what this function
2523 * does "extra" in order to avoid a destroy since this function is used
2524 * in the domain session creation (kernel and ust) only. Same for UST
2527 if (session
->kernel_session
->consumer
) {
2528 consumer_destroy_output(session
->kernel_session
->consumer
);
2530 session
->kernel_session
->consumer
=
2531 consumer_copy_output(session
->consumer
);
2532 /* Ease our life a bit for the next part */
2533 consumer
= session
->kernel_session
->consumer
;
2534 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2536 case LTTNG_DOMAIN_JUL
:
2537 case LTTNG_DOMAIN_UST
:
2538 DBG3("Copying tracing session consumer output in UST session");
2539 if (session
->ust_session
->consumer
) {
2540 consumer_destroy_output(session
->ust_session
->consumer
);
2542 session
->ust_session
->consumer
=
2543 consumer_copy_output(session
->consumer
);
2544 /* Ease our life a bit for the next part */
2545 consumer
= session
->ust_session
->consumer
;
2546 dir_name
= DEFAULT_UST_TRACE_DIR
;
2549 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2553 /* Append correct directory to subdir */
2554 strncat(consumer
->subdir
, dir_name
,
2555 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2556 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2565 * Create an UST session and add it to the session ust list.
2567 * Should *NOT* be called with RCU read-side lock held.
2569 static int create_ust_session(struct ltt_session
*session
,
2570 struct lttng_domain
*domain
)
2573 struct ltt_ust_session
*lus
= NULL
;
2577 assert(session
->consumer
);
2579 switch (domain
->type
) {
2580 case LTTNG_DOMAIN_JUL
:
2581 case LTTNG_DOMAIN_UST
:
2584 ERR("Unknown UST domain on create session %d", domain
->type
);
2585 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2589 DBG("Creating UST session");
2591 lus
= trace_ust_create_session(session
->id
);
2593 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2597 lus
->uid
= session
->uid
;
2598 lus
->gid
= session
->gid
;
2599 lus
->output_traces
= session
->output_traces
;
2600 lus
->snapshot_mode
= session
->snapshot_mode
;
2601 lus
->live_timer_interval
= session
->live_timer
;
2602 session
->ust_session
= lus
;
2604 /* Copy session output to the newly created UST session */
2605 ret
= copy_session_consumer(domain
->type
, session
);
2606 if (ret
!= LTTNG_OK
) {
2614 session
->ust_session
= NULL
;
2619 * Create a kernel tracer session then create the default channel.
2621 static int create_kernel_session(struct ltt_session
*session
)
2625 DBG("Creating kernel session");
2627 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2629 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2633 /* Code flow safety */
2634 assert(session
->kernel_session
);
2636 /* Copy session output to the newly created Kernel session */
2637 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2638 if (ret
!= LTTNG_OK
) {
2642 /* Create directory(ies) on local filesystem. */
2643 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2644 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2645 ret
= run_as_mkdir_recursive(
2646 session
->kernel_session
->consumer
->dst
.trace_path
,
2647 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2649 if (ret
!= -EEXIST
) {
2650 ERR("Trace directory creation error");
2656 session
->kernel_session
->uid
= session
->uid
;
2657 session
->kernel_session
->gid
= session
->gid
;
2658 session
->kernel_session
->output_traces
= session
->output_traces
;
2659 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2664 trace_kernel_destroy_session(session
->kernel_session
);
2665 session
->kernel_session
= NULL
;
2670 * Count number of session permitted by uid/gid.
2672 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2675 struct ltt_session
*session
;
2677 DBG("Counting number of available session for UID %d GID %d",
2679 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2681 * Only list the sessions the user can control.
2683 if (!session_access_ok(session
, uid
, gid
)) {
2692 * Process the command requested by the lttng client within the command
2693 * context structure. This function make sure that the return structure (llm)
2694 * is set and ready for transmission before returning.
2696 * Return any error encountered or 0 for success.
2698 * "sock" is only used for special-case var. len data.
2700 * Should *NOT* be called with RCU read-side lock held.
2702 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2706 int need_tracing_session
= 1;
2709 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2713 switch (cmd_ctx
->lsm
->cmd_type
) {
2714 case LTTNG_CREATE_SESSION
:
2715 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2716 case LTTNG_CREATE_SESSION_LIVE
:
2717 case LTTNG_DESTROY_SESSION
:
2718 case LTTNG_LIST_SESSIONS
:
2719 case LTTNG_LIST_DOMAINS
:
2720 case LTTNG_START_TRACE
:
2721 case LTTNG_STOP_TRACE
:
2722 case LTTNG_DATA_PENDING
:
2723 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2724 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2725 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2726 case LTTNG_SNAPSHOT_RECORD
:
2727 case LTTNG_SAVE_SESSION
:
2734 if (opt_no_kernel
&& need_domain
2735 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2737 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2739 ret
= LTTNG_ERR_KERN_NA
;
2744 /* Deny register consumer if we already have a spawned consumer. */
2745 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2746 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2747 if (kconsumer_data
.pid
> 0) {
2748 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2749 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2752 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2756 * Check for command that don't needs to allocate a returned payload. We do
2757 * this here so we don't have to make the call for no payload at each
2760 switch(cmd_ctx
->lsm
->cmd_type
) {
2761 case LTTNG_LIST_SESSIONS
:
2762 case LTTNG_LIST_TRACEPOINTS
:
2763 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2764 case LTTNG_LIST_DOMAINS
:
2765 case LTTNG_LIST_CHANNELS
:
2766 case LTTNG_LIST_EVENTS
:
2769 /* Setup lttng message with no payload */
2770 ret
= setup_lttng_msg(cmd_ctx
, 0);
2772 /* This label does not try to unlock the session */
2773 goto init_setup_error
;
2777 /* Commands that DO NOT need a session. */
2778 switch (cmd_ctx
->lsm
->cmd_type
) {
2779 case LTTNG_CREATE_SESSION
:
2780 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2781 case LTTNG_CREATE_SESSION_LIVE
:
2782 case LTTNG_CALIBRATE
:
2783 case LTTNG_LIST_SESSIONS
:
2784 case LTTNG_LIST_TRACEPOINTS
:
2785 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2786 case LTTNG_SAVE_SESSION
:
2787 need_tracing_session
= 0;
2790 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2792 * We keep the session list lock across _all_ commands
2793 * for now, because the per-session lock does not
2794 * handle teardown properly.
2796 session_lock_list();
2797 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2798 if (cmd_ctx
->session
== NULL
) {
2799 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2802 /* Acquire lock for the session */
2803 session_lock(cmd_ctx
->session
);
2813 * Check domain type for specific "pre-action".
2815 switch (cmd_ctx
->lsm
->domain
.type
) {
2816 case LTTNG_DOMAIN_KERNEL
:
2818 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2822 /* Kernel tracer check */
2823 if (kernel_tracer_fd
== -1) {
2824 /* Basically, load kernel tracer modules */
2825 ret
= init_kernel_tracer();
2831 /* Consumer is in an ERROR state. Report back to client */
2832 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
2833 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2837 /* Need a session for kernel command */
2838 if (need_tracing_session
) {
2839 if (cmd_ctx
->session
->kernel_session
== NULL
) {
2840 ret
= create_kernel_session(cmd_ctx
->session
);
2842 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2847 /* Start the kernel consumer daemon */
2848 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2849 if (kconsumer_data
.pid
== 0 &&
2850 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2851 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2852 ret
= start_consumerd(&kconsumer_data
);
2854 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2857 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
2859 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2863 * The consumer was just spawned so we need to add the socket to
2864 * the consumer output of the session if exist.
2866 ret
= consumer_create_socket(&kconsumer_data
,
2867 cmd_ctx
->session
->kernel_session
->consumer
);
2874 case LTTNG_DOMAIN_JUL
:
2875 case LTTNG_DOMAIN_UST
:
2877 if (!ust_app_supported()) {
2878 ret
= LTTNG_ERR_NO_UST
;
2881 /* Consumer is in an ERROR state. Report back to client */
2882 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
2883 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2887 if (need_tracing_session
) {
2888 /* Create UST session if none exist. */
2889 if (cmd_ctx
->session
->ust_session
== NULL
) {
2890 ret
= create_ust_session(cmd_ctx
->session
,
2891 &cmd_ctx
->lsm
->domain
);
2892 if (ret
!= LTTNG_OK
) {
2897 /* Start the UST consumer daemons */
2899 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
2900 if (consumerd64_bin
[0] != '\0' &&
2901 ustconsumer64_data
.pid
== 0 &&
2902 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2903 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2904 ret
= start_consumerd(&ustconsumer64_data
);
2906 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
2907 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
2911 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
2912 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2914 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2918 * Setup socket for consumer 64 bit. No need for atomic access
2919 * since it was set above and can ONLY be set in this thread.
2921 ret
= consumer_create_socket(&ustconsumer64_data
,
2922 cmd_ctx
->session
->ust_session
->consumer
);
2928 if (consumerd32_bin
[0] != '\0' &&
2929 ustconsumer32_data
.pid
== 0 &&
2930 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2931 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2932 ret
= start_consumerd(&ustconsumer32_data
);
2934 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
2935 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
2939 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
2940 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2942 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2946 * Setup socket for consumer 64 bit. No need for atomic access
2947 * since it was set above and can ONLY be set in this thread.
2949 ret
= consumer_create_socket(&ustconsumer32_data
,
2950 cmd_ctx
->session
->ust_session
->consumer
);
2962 /* Validate consumer daemon state when start/stop trace command */
2963 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
2964 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
2965 switch (cmd_ctx
->lsm
->domain
.type
) {
2966 case LTTNG_DOMAIN_JUL
:
2967 case LTTNG_DOMAIN_UST
:
2968 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
2969 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2973 case LTTNG_DOMAIN_KERNEL
:
2974 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
2975 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2983 * Check that the UID or GID match that of the tracing session.
2984 * The root user can interact with all sessions.
2986 if (need_tracing_session
) {
2987 if (!session_access_ok(cmd_ctx
->session
,
2988 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2989 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
2990 ret
= LTTNG_ERR_EPERM
;
2996 * Send relayd information to consumer as soon as we have a domain and a
2999 if (cmd_ctx
->session
&& need_domain
) {
3001 * Setup relayd if not done yet. If the relayd information was already
3002 * sent to the consumer, this call will gracefully return.
3004 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3005 if (ret
!= LTTNG_OK
) {
3010 /* Process by command type */
3011 switch (cmd_ctx
->lsm
->cmd_type
) {
3012 case LTTNG_ADD_CONTEXT
:
3014 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3015 cmd_ctx
->lsm
->u
.context
.channel_name
,
3016 &cmd_ctx
->lsm
->u
.context
.ctx
, kernel_poll_pipe
[1]);
3019 case LTTNG_DISABLE_CHANNEL
:
3021 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3022 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3025 case LTTNG_DISABLE_EVENT
:
3027 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3028 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3029 cmd_ctx
->lsm
->u
.disable
.name
);
3032 case LTTNG_DISABLE_ALL_EVENT
:
3034 DBG("Disabling all events");
3036 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3037 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3040 case LTTNG_ENABLE_CHANNEL
:
3042 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3043 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
3046 case LTTNG_ENABLE_EVENT
:
3048 struct lttng_event_exclusion
*exclusion
= NULL
;
3049 struct lttng_filter_bytecode
*bytecode
= NULL
;
3050 char *filter_expression
= NULL
;
3052 /* Handle exclusion events and receive it from the client. */
3053 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3054 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3056 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3057 (count
* LTTNG_SYMBOL_NAME_LEN
));
3059 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3063 DBG("Receiving var len exclusion event list from client ...");
3064 exclusion
->count
= count
;
3065 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3066 count
* LTTNG_SYMBOL_NAME_LEN
);
3068 DBG("Nothing recv() from client var len data... continuing");
3071 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3076 /* Get filter expression from client. */
3077 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3078 size_t expression_len
=
3079 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3081 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3082 ret
= LTTNG_ERR_FILTER_INVAL
;
3087 filter_expression
= zmalloc(expression_len
);
3088 if (!filter_expression
) {
3090 ret
= LTTNG_ERR_FILTER_NOMEM
;
3094 /* Receive var. len. data */
3095 DBG("Receiving var len filter's expression from client ...");
3096 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3099 DBG("Nothing recv() from client car len data... continuing");
3101 free(filter_expression
);
3103 ret
= LTTNG_ERR_FILTER_INVAL
;
3108 /* Handle filter and get bytecode from client. */
3109 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3110 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3112 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3113 ret
= LTTNG_ERR_FILTER_INVAL
;
3118 bytecode
= zmalloc(bytecode_len
);
3121 ret
= LTTNG_ERR_FILTER_NOMEM
;
3125 /* Receive var. len. data */
3126 DBG("Receiving var len filter's bytecode from client ...");
3127 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3129 DBG("Nothing recv() from client car len data... continuing");
3133 ret
= LTTNG_ERR_FILTER_INVAL
;
3137 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3140 ret
= LTTNG_ERR_FILTER_INVAL
;
3145 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3146 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3147 &cmd_ctx
->lsm
->u
.enable
.event
,
3148 filter_expression
, bytecode
, exclusion
,
3149 kernel_poll_pipe
[1]);
3152 case LTTNG_ENABLE_ALL_EVENT
:
3154 DBG("Enabling all events");
3156 ret
= cmd_enable_event_all(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3157 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3158 cmd_ctx
->lsm
->u
.enable
.event
.type
, NULL
, NULL
,
3159 kernel_poll_pipe
[1]);
3162 case LTTNG_LIST_TRACEPOINTS
:
3164 struct lttng_event
*events
;
3167 session_lock_list();
3168 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3169 session_unlock_list();
3170 if (nb_events
< 0) {
3171 /* Return value is a negative lttng_error_code. */
3177 * Setup lttng message with payload size set to the event list size in
3178 * bytes and then copy list into the llm payload.
3180 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
3186 /* Copy event list into message payload */
3187 memcpy(cmd_ctx
->llm
->payload
, events
,
3188 sizeof(struct lttng_event
) * nb_events
);
3195 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3197 struct lttng_event_field
*fields
;
3200 session_lock_list();
3201 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3203 session_unlock_list();
3204 if (nb_fields
< 0) {
3205 /* Return value is a negative lttng_error_code. */
3211 * Setup lttng message with payload size set to the event list size in
3212 * bytes and then copy list into the llm payload.
3214 ret
= setup_lttng_msg(cmd_ctx
,
3215 sizeof(struct lttng_event_field
) * nb_fields
);
3221 /* Copy event list into message payload */
3222 memcpy(cmd_ctx
->llm
->payload
, fields
,
3223 sizeof(struct lttng_event_field
) * nb_fields
);
3230 case LTTNG_SET_CONSUMER_URI
:
3233 struct lttng_uri
*uris
;
3235 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3236 len
= nb_uri
* sizeof(struct lttng_uri
);
3239 ret
= LTTNG_ERR_INVALID
;
3243 uris
= zmalloc(len
);
3245 ret
= LTTNG_ERR_FATAL
;
3249 /* Receive variable len data */
3250 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3251 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3253 DBG("No URIs received from client... continuing");
3255 ret
= LTTNG_ERR_SESSION_FAIL
;
3260 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3262 if (ret
!= LTTNG_OK
) {
3268 * XXX: 0 means that this URI should be applied on the session. Should
3269 * be a DOMAIN enuam.
3271 if (cmd_ctx
->lsm
->domain
.type
== 0) {
3272 /* Add the URI for the UST session if a consumer is present. */
3273 if (cmd_ctx
->session
->ust_session
&&
3274 cmd_ctx
->session
->ust_session
->consumer
) {
3275 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_UST
, cmd_ctx
->session
,
3277 } else if (cmd_ctx
->session
->kernel_session
&&
3278 cmd_ctx
->session
->kernel_session
->consumer
) {
3279 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL
,
3280 cmd_ctx
->session
, nb_uri
, uris
);
3288 case LTTNG_START_TRACE
:
3290 ret
= cmd_start_trace(cmd_ctx
->session
);
3293 case LTTNG_STOP_TRACE
:
3295 ret
= cmd_stop_trace(cmd_ctx
->session
);
3298 case LTTNG_CREATE_SESSION
:
3301 struct lttng_uri
*uris
= NULL
;
3303 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3304 len
= nb_uri
* sizeof(struct lttng_uri
);
3307 uris
= zmalloc(len
);
3309 ret
= LTTNG_ERR_FATAL
;
3313 /* Receive variable len data */
3314 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3315 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3317 DBG("No URIs received from client... continuing");
3319 ret
= LTTNG_ERR_SESSION_FAIL
;
3324 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3325 DBG("Creating session with ONE network URI is a bad call");
3326 ret
= LTTNG_ERR_SESSION_FAIL
;
3332 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3333 &cmd_ctx
->creds
, 0);
3339 case LTTNG_DESTROY_SESSION
:
3341 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3343 /* Set session to NULL so we do not unlock it after free. */
3344 cmd_ctx
->session
= NULL
;
3347 case LTTNG_LIST_DOMAINS
:
3350 struct lttng_domain
*domains
;
3352 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3354 /* Return value is a negative lttng_error_code. */
3359 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
3365 /* Copy event list into message payload */
3366 memcpy(cmd_ctx
->llm
->payload
, domains
,
3367 nb_dom
* sizeof(struct lttng_domain
));
3374 case LTTNG_LIST_CHANNELS
:
3377 struct lttng_channel
*channels
;
3379 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3380 cmd_ctx
->session
, &channels
);
3382 /* Return value is a negative lttng_error_code. */
3387 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
3393 /* Copy event list into message payload */
3394 memcpy(cmd_ctx
->llm
->payload
, channels
,
3395 nb_chan
* sizeof(struct lttng_channel
));
3402 case LTTNG_LIST_EVENTS
:
3405 struct lttng_event
*events
= NULL
;
3407 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3408 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
3410 /* Return value is a negative lttng_error_code. */
3415 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
3421 /* Copy event list into message payload */
3422 memcpy(cmd_ctx
->llm
->payload
, events
,
3423 nb_event
* sizeof(struct lttng_event
));
3430 case LTTNG_LIST_SESSIONS
:
3432 unsigned int nr_sessions
;
3434 session_lock_list();
3435 nr_sessions
= lttng_sessions_count(
3436 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3437 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3439 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
3441 session_unlock_list();
3445 /* Filled the session array */
3446 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
3447 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3448 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3450 session_unlock_list();
3455 case LTTNG_CALIBRATE
:
3457 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3458 &cmd_ctx
->lsm
->u
.calibrate
);
3461 case LTTNG_REGISTER_CONSUMER
:
3463 struct consumer_data
*cdata
;
3465 switch (cmd_ctx
->lsm
->domain
.type
) {
3466 case LTTNG_DOMAIN_KERNEL
:
3467 cdata
= &kconsumer_data
;
3470 ret
= LTTNG_ERR_UND
;
3474 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3475 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3478 case LTTNG_DATA_PENDING
:
3480 ret
= cmd_data_pending(cmd_ctx
->session
);
3483 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3485 struct lttcomm_lttng_output_id reply
;
3487 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3488 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3489 if (ret
!= LTTNG_OK
) {
3493 ret
= setup_lttng_msg(cmd_ctx
, sizeof(reply
));
3498 /* Copy output list into message payload */
3499 memcpy(cmd_ctx
->llm
->payload
, &reply
, sizeof(reply
));
3503 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3505 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3506 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3509 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3512 struct lttng_snapshot_output
*outputs
= NULL
;
3514 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3515 if (nb_output
< 0) {
3520 ret
= setup_lttng_msg(cmd_ctx
,
3521 nb_output
* sizeof(struct lttng_snapshot_output
));
3528 /* Copy output list into message payload */
3529 memcpy(cmd_ctx
->llm
->payload
, outputs
,
3530 nb_output
* sizeof(struct lttng_snapshot_output
));
3537 case LTTNG_SNAPSHOT_RECORD
:
3539 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3540 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3541 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3544 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3547 struct lttng_uri
*uris
= NULL
;
3549 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3550 len
= nb_uri
* sizeof(struct lttng_uri
);
3553 uris
= zmalloc(len
);
3555 ret
= LTTNG_ERR_FATAL
;
3559 /* Receive variable len data */
3560 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3561 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3563 DBG("No URIs received from client... continuing");
3565 ret
= LTTNG_ERR_SESSION_FAIL
;
3570 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3571 DBG("Creating session with ONE network URI is a bad call");
3572 ret
= LTTNG_ERR_SESSION_FAIL
;
3578 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
3579 nb_uri
, &cmd_ctx
->creds
);
3583 case LTTNG_CREATE_SESSION_LIVE
:
3586 struct lttng_uri
*uris
= NULL
;
3588 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3589 len
= nb_uri
* sizeof(struct lttng_uri
);
3592 uris
= zmalloc(len
);
3594 ret
= LTTNG_ERR_FATAL
;
3598 /* Receive variable len data */
3599 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3600 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3602 DBG("No URIs received from client... continuing");
3604 ret
= LTTNG_ERR_SESSION_FAIL
;
3609 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3610 DBG("Creating session with ONE network URI is a bad call");
3611 ret
= LTTNG_ERR_SESSION_FAIL
;
3617 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
3618 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
3622 case LTTNG_SAVE_SESSION
:
3624 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
3629 ret
= LTTNG_ERR_UND
;
3634 if (cmd_ctx
->llm
== NULL
) {
3635 DBG("Missing llm structure. Allocating one.");
3636 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
3640 /* Set return code */
3641 cmd_ctx
->llm
->ret_code
= ret
;
3643 if (cmd_ctx
->session
) {
3644 session_unlock(cmd_ctx
->session
);
3646 if (need_tracing_session
) {
3647 session_unlock_list();
3654 * Thread managing health check socket.
3656 static void *thread_manage_health(void *data
)
3658 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
3659 uint32_t revents
, nb_fd
;
3660 struct lttng_poll_event events
;
3661 struct health_comm_msg msg
;
3662 struct health_comm_reply reply
;
3664 DBG("[thread] Manage health check started");
3666 rcu_register_thread();
3668 /* We might hit an error path before this is created. */
3669 lttng_poll_init(&events
);
3671 /* Create unix socket */
3672 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
3674 ERR("Unable to create health check Unix socket");
3680 /* lttng health client socket path permissions */
3681 ret
= chown(health_unix_sock_path
, 0,
3682 utils_get_group_id(tracing_group_name
));
3684 ERR("Unable to set group on %s", health_unix_sock_path
);
3690 ret
= chmod(health_unix_sock_path
,
3691 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3693 ERR("Unable to set permissions on %s", health_unix_sock_path
);
3701 * Set the CLOEXEC flag. Return code is useless because either way, the
3704 (void) utils_set_fd_cloexec(sock
);
3706 ret
= lttcomm_listen_unix_sock(sock
);
3712 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3713 * more will be added to this poll set.
3715 ret
= sessiond_set_thread_pollset(&events
, 2);
3720 /* Add the application registration socket */
3721 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
3726 lttng_sessiond_notify_ready();
3729 DBG("Health check ready");
3731 /* Inifinite blocking call, waiting for transmission */
3733 ret
= lttng_poll_wait(&events
, -1);
3736 * Restart interrupted system call.
3738 if (errno
== EINTR
) {
3746 for (i
= 0; i
< nb_fd
; i
++) {
3747 /* Fetch once the poll data */
3748 revents
= LTTNG_POLL_GETEV(&events
, i
);
3749 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3751 /* Thread quit pipe has been closed. Killing thread. */
3752 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3758 /* Event on the registration socket */
3759 if (pollfd
== sock
) {
3760 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3761 ERR("Health socket poll error");
3767 new_sock
= lttcomm_accept_unix_sock(sock
);
3773 * Set the CLOEXEC flag. Return code is useless because either way, the
3776 (void) utils_set_fd_cloexec(new_sock
);
3778 DBG("Receiving data from client for health...");
3779 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
3781 DBG("Nothing recv() from client... continuing");
3782 ret
= close(new_sock
);
3790 rcu_thread_online();
3792 memset(&reply
, 0, sizeof(reply
));
3793 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
3795 * health_check_state returns 0 if health is
3798 if (!health_check_state(health_sessiond
, i
)) {
3799 reply
.ret_code
|= 1ULL << i
;
3803 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
3805 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
3807 ERR("Failed to send health data back to client");
3810 /* End of transmission */
3811 ret
= close(new_sock
);
3821 ERR("Health error occurred in %s", __func__
);
3823 DBG("Health check thread dying");
3824 unlink(health_unix_sock_path
);
3832 lttng_poll_clean(&events
);
3834 rcu_unregister_thread();
3839 * This thread manage all clients request using the unix client socket for
3842 static void *thread_manage_clients(void *data
)
3844 int sock
= -1, ret
, i
, pollfd
, err
= -1;
3846 uint32_t revents
, nb_fd
;
3847 struct command_ctx
*cmd_ctx
= NULL
;
3848 struct lttng_poll_event events
;
3850 DBG("[thread] Manage client started");
3852 rcu_register_thread();
3854 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
3856 health_code_update();
3858 ret
= lttcomm_listen_unix_sock(client_sock
);
3864 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3865 * more will be added to this poll set.
3867 ret
= sessiond_set_thread_pollset(&events
, 2);
3869 goto error_create_poll
;
3872 /* Add the application registration socket */
3873 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
3878 lttng_sessiond_notify_ready();
3880 /* This testpoint is after we signal readiness to the parent. */
3881 if (testpoint(sessiond_thread_manage_clients
)) {
3885 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
3889 health_code_update();
3892 DBG("Accepting client command ...");
3894 /* Inifinite blocking call, waiting for transmission */
3896 health_poll_entry();
3897 ret
= lttng_poll_wait(&events
, -1);
3901 * Restart interrupted system call.
3903 if (errno
== EINTR
) {
3911 for (i
= 0; i
< nb_fd
; i
++) {
3912 /* Fetch once the poll data */
3913 revents
= LTTNG_POLL_GETEV(&events
, i
);
3914 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3916 health_code_update();
3918 /* Thread quit pipe has been closed. Killing thread. */
3919 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3925 /* Event on the registration socket */
3926 if (pollfd
== client_sock
) {
3927 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3928 ERR("Client socket poll error");
3934 DBG("Wait for client response");
3936 health_code_update();
3938 sock
= lttcomm_accept_unix_sock(client_sock
);
3944 * Set the CLOEXEC flag. Return code is useless because either way, the
3947 (void) utils_set_fd_cloexec(sock
);
3949 /* Set socket option for credentials retrieval */
3950 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
3955 /* Allocate context command to process the client request */
3956 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
3957 if (cmd_ctx
== NULL
) {
3958 PERROR("zmalloc cmd_ctx");
3962 /* Allocate data buffer for reception */
3963 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
3964 if (cmd_ctx
->lsm
== NULL
) {
3965 PERROR("zmalloc cmd_ctx->lsm");
3969 cmd_ctx
->llm
= NULL
;
3970 cmd_ctx
->session
= NULL
;
3972 health_code_update();
3975 * Data is received from the lttng client. The struct
3976 * lttcomm_session_msg (lsm) contains the command and data request of
3979 DBG("Receiving data from client ...");
3980 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
3981 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
3983 DBG("Nothing recv() from client... continuing");
3989 clean_command_ctx(&cmd_ctx
);
3993 health_code_update();
3995 // TODO: Validate cmd_ctx including sanity check for
3996 // security purpose.
3998 rcu_thread_online();
4000 * This function dispatch the work to the kernel or userspace tracer
4001 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4002 * informations for the client. The command context struct contains
4003 * everything this function may needs.
4005 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4006 rcu_thread_offline();
4014 * TODO: Inform client somehow of the fatal error. At
4015 * this point, ret < 0 means that a zmalloc failed
4016 * (ENOMEM). Error detected but still accept
4017 * command, unless a socket error has been
4020 clean_command_ctx(&cmd_ctx
);
4024 health_code_update();
4026 DBG("Sending response (size: %d, retcode: %s)",
4027 cmd_ctx
->lttng_msg_size
,
4028 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
4029 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4031 ERR("Failed to send data back to client");
4034 /* End of transmission */
4041 clean_command_ctx(&cmd_ctx
);
4043 health_code_update();
4055 lttng_poll_clean(&events
);
4056 clean_command_ctx(&cmd_ctx
);
4060 unlink(client_unix_sock_path
);
4061 if (client_sock
>= 0) {
4062 ret
= close(client_sock
);
4070 ERR("Health error occurred in %s", __func__
);
4073 health_unregister(health_sessiond
);
4075 DBG("Client thread dying");
4077 rcu_unregister_thread();
4083 * usage function on stderr
4085 static void usage(void)
4087 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
4088 fprintf(stderr
, " -h, --help Display this usage.\n");
4089 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
4090 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
4091 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
4092 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
4093 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
4094 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
4095 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
4096 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
4097 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
4098 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
4099 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
4100 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
4101 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
4102 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
4103 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
4104 fprintf(stderr
, " -V, --version Show version number.\n");
4105 fprintf(stderr
, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n");
4106 fprintf(stderr
, " -q, --quiet No output at all.\n");
4107 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
4108 fprintf(stderr
, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
4109 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4110 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
4111 fprintf(stderr
, " --jul-tcp-port JUL application registration TCP port\n");
4112 fprintf(stderr
, " -f --config Load daemon configuration file\n");
4116 * Take an option from the getopt output and set it in the right variable to be
4119 * Return 0 on success else a negative value.
4121 static int set_option(int opt
, const char *arg
, const char *optname
)
4127 fprintf(stderr
, "option %s", optname
);
4129 fprintf(stderr
, " with arg %s\n", arg
);
4133 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", arg
);
4136 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", arg
);
4145 tracing_group_name
= strdup(arg
);
4151 fprintf(stdout
, "%s\n", VERSION
);
4157 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4160 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4163 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4166 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4169 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4172 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4178 lttng_opt_quiet
= 1;
4181 /* Verbose level can increase using multiple -v */
4183 lttng_opt_verbose
= config_parse_value(arg
);
4185 lttng_opt_verbose
+= 1;
4190 opt_verbose_consumer
= config_parse_value(arg
);
4192 opt_verbose_consumer
+= 1;
4196 consumerd32_bin
= strdup(arg
);
4197 consumerd32_bin_override
= 1;
4200 consumerd32_libdir
= strdup(arg
);
4201 consumerd32_libdir_override
= 1;
4204 consumerd64_bin
= strdup(arg
);
4205 consumerd64_bin_override
= 1;
4208 consumerd64_libdir
= strdup(arg
);
4209 consumerd64_libdir_override
= 1;
4212 opt_pidfile
= strdup(arg
);
4214 case 'J': /* JUL TCP port. */
4219 v
= strtoul(arg
, NULL
, 0);
4220 if (errno
!= 0 || !isdigit(arg
[0])) {
4221 ERR("Wrong value in --jul-tcp-port parameter: %s", arg
);
4224 if (v
== 0 || v
>= 65535) {
4225 ERR("Port overflow in --jul-tcp-port parameter: %s", arg
);
4228 jul_tcp_port
= (uint32_t) v
;
4229 DBG3("JUL TCP port set to non default: %u", jul_tcp_port
);
4233 /* Unknown option or other error.
4234 * Error is printed by getopt, just return */
4242 * config_entry_handler_cb used to handle options read from a config file.
4243 * See config_entry_handler_cb comment in common/config/config.h for the
4244 * return value conventions.
4246 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
4250 if (!entry
|| !entry
->name
|| !entry
->value
) {
4255 /* Check if the option is to be ignored */
4256 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
4257 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
4262 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
4265 /* Ignore if not fully matched. */
4266 if (strcmp(entry
->name
, long_options
[i
].name
)) {
4271 * If the option takes no argument on the command line, we have to
4272 * check if the value is "true". We support non-zero numeric values,
4275 if (!long_options
[i
].has_arg
) {
4276 ret
= config_parse_value(entry
->value
);
4279 WARN("Invalid configuration value \"%s\" for option %s",
4280 entry
->value
, entry
->name
);
4282 /* False, skip boolean config option. */
4287 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
4291 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
4298 * daemon configuration loading and argument parsing
4300 static int set_options(int argc
, char **argv
)
4302 int ret
= 0, c
= 0, option_index
= 0;
4303 int orig_optopt
= optopt
, orig_optind
= optind
;
4305 const char *config_path
= NULL
;
4307 optstring
= utils_generate_optstring(long_options
,
4308 sizeof(long_options
) / sizeof(struct option
));
4314 /* Check for the --config option */
4315 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
4316 &option_index
)) != -1) {
4320 } else if (c
!= 'f') {
4321 /* if not equal to --config option. */
4325 config_path
= utils_expand_path(optarg
);
4327 ERR("Failed to resolve path: %s", optarg
);
4331 ret
= config_get_section_entries(config_path
, config_section_name
,
4332 config_entry_handler
, NULL
);
4335 ERR("Invalid configuration option at line %i", ret
);
4341 /* Reset getopt's global state */
4342 optopt
= orig_optopt
;
4343 optind
= orig_optind
;
4345 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
4350 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
4362 * Creates the two needed socket by the daemon.
4363 * apps_sock - The communication socket for all UST apps.
4364 * client_sock - The communication of the cli tool (lttng).
4366 static int init_daemon_socket(void)
4371 old_umask
= umask(0);
4373 /* Create client tool unix socket */
4374 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
4375 if (client_sock
< 0) {
4376 ERR("Create unix sock failed: %s", client_unix_sock_path
);
4381 /* Set the cloexec flag */
4382 ret
= utils_set_fd_cloexec(client_sock
);
4384 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4385 "Continuing but note that the consumer daemon will have a "
4386 "reference to this socket on exec()", client_sock
);
4389 /* File permission MUST be 660 */
4390 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4392 ERR("Set file permissions failed: %s", client_unix_sock_path
);
4397 /* Create the application unix socket */
4398 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
4399 if (apps_sock
< 0) {
4400 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
4405 /* Set the cloexec flag */
4406 ret
= utils_set_fd_cloexec(apps_sock
);
4408 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4409 "Continuing but note that the consumer daemon will have a "
4410 "reference to this socket on exec()", apps_sock
);
4413 /* File permission MUST be 666 */
4414 ret
= chmod(apps_unix_sock_path
,
4415 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
4417 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
4422 DBG3("Session daemon client socket %d and application socket %d created",
4423 client_sock
, apps_sock
);
4431 * Check if the global socket is available, and if a daemon is answering at the
4432 * other side. If yes, error is returned.
4434 static int check_existing_daemon(void)
4436 /* Is there anybody out there ? */
4437 if (lttng_session_daemon_alive()) {
4445 * Set the tracing group gid onto the client socket.
4447 * Race window between mkdir and chown is OK because we are going from more
4448 * permissive (root.root) to less permissive (root.tracing).
4450 static int set_permissions(char *rundir
)
4455 gid
= utils_get_group_id(tracing_group_name
);
4457 /* Set lttng run dir */
4458 ret
= chown(rundir
, 0, gid
);
4460 ERR("Unable to set group on %s", rundir
);
4465 * Ensure all applications and tracing group can search the run
4466 * dir. Allow everyone to read the directory, since it does not
4467 * buy us anything to hide its content.
4469 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
4471 ERR("Unable to set permissions on %s", rundir
);
4475 /* lttng client socket path */
4476 ret
= chown(client_unix_sock_path
, 0, gid
);
4478 ERR("Unable to set group on %s", client_unix_sock_path
);
4482 /* kconsumer error socket path */
4483 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
4485 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
4489 /* 64-bit ustconsumer error socket path */
4490 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
4492 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
4496 /* 32-bit ustconsumer compat32 error socket path */
4497 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
4499 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
4503 DBG("All permissions are set");
4509 * Create the lttng run directory needed for all global sockets and pipe.
4511 static int create_lttng_rundir(const char *rundir
)
4515 DBG3("Creating LTTng run directory: %s", rundir
);
4517 ret
= mkdir(rundir
, S_IRWXU
);
4519 if (errno
!= EEXIST
) {
4520 ERR("Unable to create %s", rundir
);
4532 * Setup sockets and directory needed by the kconsumerd communication with the
4535 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
4539 char path
[PATH_MAX
];
4541 switch (consumer_data
->type
) {
4542 case LTTNG_CONSUMER_KERNEL
:
4543 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
4545 case LTTNG_CONSUMER64_UST
:
4546 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
4548 case LTTNG_CONSUMER32_UST
:
4549 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
4552 ERR("Consumer type unknown");
4557 DBG2("Creating consumer directory: %s", path
);
4559 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
4561 if (errno
!= EEXIST
) {
4563 ERR("Failed to create %s", path
);
4569 ret
= chown(path
, 0, utils_get_group_id(tracing_group_name
));
4571 ERR("Unable to set group on %s", path
);
4577 /* Create the kconsumerd error unix socket */
4578 consumer_data
->err_sock
=
4579 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
4580 if (consumer_data
->err_sock
< 0) {
4581 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
4587 * Set the CLOEXEC flag. Return code is useless because either way, the
4590 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
4592 PERROR("utils_set_fd_cloexec");
4593 /* continue anyway */
4596 /* File permission MUST be 660 */
4597 ret
= chmod(consumer_data
->err_unix_sock_path
,
4598 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4600 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
4610 * Signal handler for the daemon
4612 * Simply stop all worker threads, leaving main() return gracefully after
4613 * joining all threads and calling cleanup().
4615 static void sighandler(int sig
)
4619 DBG("SIGPIPE caught");
4622 DBG("SIGINT caught");
4626 DBG("SIGTERM caught");
4630 CMM_STORE_SHARED(recv_child_signal
, 1);
4638 * Setup signal handler for :
4639 * SIGINT, SIGTERM, SIGPIPE
4641 static int set_signal_handler(void)
4644 struct sigaction sa
;
4647 if ((ret
= sigemptyset(&sigset
)) < 0) {
4648 PERROR("sigemptyset");
4652 sa
.sa_handler
= sighandler
;
4653 sa
.sa_mask
= sigset
;
4655 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
4656 PERROR("sigaction");
4660 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
4661 PERROR("sigaction");
4665 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
4666 PERROR("sigaction");
4670 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
4671 PERROR("sigaction");
4675 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
4681 * Set open files limit to unlimited. This daemon can open a large number of
4682 * file descriptors in order to consumer multiple kernel traces.
4684 static void set_ulimit(void)
4689 /* The kernel does not allowed an infinite limit for open files */
4690 lim
.rlim_cur
= 65535;
4691 lim
.rlim_max
= 65535;
4693 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
4695 PERROR("failed to set open files limit");
4700 * Write pidfile using the rundir and opt_pidfile.
4702 static void write_pidfile(void)
4705 char pidfile_path
[PATH_MAX
];
4710 strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
));
4712 /* Build pidfile path from rundir and opt_pidfile. */
4713 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
4714 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
4716 PERROR("snprintf pidfile path");
4722 * Create pid file in rundir. Return value is of no importance. The
4723 * execution will continue even though we are not able to write the file.
4725 (void) utils_create_pid_file(getpid(), pidfile_path
);
4732 * Write JUL TCP port using the rundir.
4734 static void write_julport(void)
4737 char path
[PATH_MAX
];
4741 ret
= snprintf(path
, sizeof(path
), "%s/"
4742 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE
, rundir
);
4744 PERROR("snprintf julport path");
4749 * Create TCP JUL port file in rundir. Return value is of no importance.
4750 * The execution will continue even though we are not able to write the
4753 (void) utils_create_pid_file(jul_tcp_port
, path
);
4762 int main(int argc
, char **argv
)
4766 const char *home_path
, *env_app_timeout
;
4768 init_kernel_workarounds();
4770 rcu_register_thread();
4772 if ((ret
= set_signal_handler()) < 0) {
4776 setup_consumerd_path();
4778 page_size
= sysconf(_SC_PAGESIZE
);
4779 if (page_size
< 0) {
4780 PERROR("sysconf _SC_PAGESIZE");
4781 page_size
= LONG_MAX
;
4782 WARN("Fallback page size to %ld", page_size
);
4785 /* Parse arguments and load the daemon configuration file */
4787 if ((ret
= set_options(argc
, argv
)) < 0) {
4792 if (opt_daemon
|| opt_background
) {
4795 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
4802 * We are in the child. Make sure all other file descriptors are
4803 * closed, in case we are called with more opened file descriptors than
4804 * the standard ones.
4806 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
4811 /* Create thread quit pipe */
4812 if ((ret
= init_thread_quit_pipe()) < 0) {
4816 /* Check if daemon is UID = 0 */
4817 is_root
= !getuid();
4820 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
4822 /* Create global run dir with root access */
4823 ret
= create_lttng_rundir(rundir
);
4828 if (strlen(apps_unix_sock_path
) == 0) {
4829 snprintf(apps_unix_sock_path
, PATH_MAX
,
4830 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
4833 if (strlen(client_unix_sock_path
) == 0) {
4834 snprintf(client_unix_sock_path
, PATH_MAX
,
4835 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
4838 /* Set global SHM for ust */
4839 if (strlen(wait_shm_path
) == 0) {
4840 snprintf(wait_shm_path
, PATH_MAX
,
4841 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
4844 if (strlen(health_unix_sock_path
) == 0) {
4845 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4846 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
4849 /* Setup kernel consumerd path */
4850 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
4851 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
4852 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
4853 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
4855 DBG2("Kernel consumer err path: %s",
4856 kconsumer_data
.err_unix_sock_path
);
4857 DBG2("Kernel consumer cmd path: %s",
4858 kconsumer_data
.cmd_unix_sock_path
);
4860 home_path
= utils_get_home_dir();
4861 if (home_path
== NULL
) {
4862 /* TODO: Add --socket PATH option */
4863 ERR("Can't get HOME directory for sockets creation.");
4869 * Create rundir from home path. This will create something like
4872 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
4878 ret
= create_lttng_rundir(rundir
);
4883 if (strlen(apps_unix_sock_path
) == 0) {
4884 snprintf(apps_unix_sock_path
, PATH_MAX
,
4885 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
4888 /* Set the cli tool unix socket path */
4889 if (strlen(client_unix_sock_path
) == 0) {
4890 snprintf(client_unix_sock_path
, PATH_MAX
,
4891 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
4894 /* Set global SHM for ust */
4895 if (strlen(wait_shm_path
) == 0) {
4896 snprintf(wait_shm_path
, PATH_MAX
,
4897 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
4900 /* Set health check Unix path */
4901 if (strlen(health_unix_sock_path
) == 0) {
4902 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4903 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
4907 /* Set consumer initial state */
4908 kernel_consumerd_state
= CONSUMER_STOPPED
;
4909 ust_consumerd_state
= CONSUMER_STOPPED
;
4911 DBG("Client socket path %s", client_unix_sock_path
);
4912 DBG("Application socket path %s", apps_unix_sock_path
);
4913 DBG("Application wait path %s", wait_shm_path
);
4914 DBG("LTTng run directory path: %s", rundir
);
4916 /* 32 bits consumerd path setup */
4917 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
4918 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
4919 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
4920 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
4922 DBG2("UST consumer 32 bits err path: %s",
4923 ustconsumer32_data
.err_unix_sock_path
);
4924 DBG2("UST consumer 32 bits cmd path: %s",
4925 ustconsumer32_data
.cmd_unix_sock_path
);
4927 /* 64 bits consumerd path setup */
4928 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
4929 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
4930 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
4931 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
4933 DBG2("UST consumer 64 bits err path: %s",
4934 ustconsumer64_data
.err_unix_sock_path
);
4935 DBG2("UST consumer 64 bits cmd path: %s",
4936 ustconsumer64_data
.cmd_unix_sock_path
);
4939 * See if daemon already exist.
4941 if ((ret
= check_existing_daemon()) < 0) {
4942 ERR("Already running daemon.\n");
4944 * We do not goto exit because we must not cleanup()
4945 * because a daemon is already running.
4951 * Init UST app hash table. Alloc hash table before this point since
4952 * cleanup() can get called after that point.
4956 /* Initialize JUL domain subsystem. */
4957 if ((ret
= jul_init()) < 0) {
4958 /* ENOMEM at this point. */
4962 /* After this point, we can safely call cleanup() with "goto exit" */
4965 * These actions must be executed as root. We do that *after* setting up
4966 * the sockets path because we MUST make the check for another daemon using
4967 * those paths *before* trying to set the kernel consumer sockets and init
4971 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
4976 /* Setup kernel tracer */
4977 if (!opt_no_kernel
) {
4978 init_kernel_tracer();
4981 /* Set ulimit for open files */
4984 /* init lttng_fd tracking must be done after set_ulimit. */
4987 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
4992 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
4997 /* Setup the needed unix socket */
4998 if ((ret
= init_daemon_socket()) < 0) {
5002 /* Set credentials to socket */
5003 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
5007 /* Get parent pid if -S, --sig-parent is specified. */
5008 if (opt_sig_parent
) {
5012 /* Setup the kernel pipe for waking up the kernel thread */
5013 if (is_root
&& !opt_no_kernel
) {
5014 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
5019 /* Setup the thread ht_cleanup communication pipe. */
5020 if (utils_create_pipe_cloexec(ht_cleanup_pipe
) < 0) {
5024 /* Setup the thread apps communication pipe. */
5025 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
5029 /* Setup the thread apps notify communication pipe. */
5030 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
) < 0) {
5034 /* Initialize global buffer per UID and PID registry. */
5035 buffer_reg_init_uid_registry();
5036 buffer_reg_init_pid_registry();
5038 /* Init UST command queue. */
5039 cds_wfq_init(&ust_cmd_queue
.queue
);
5042 * Get session list pointer. This pointer MUST NOT be free(). This list is
5043 * statically declared in session.c
5045 session_list_ptr
= session_get_list();
5047 /* Set up max poll set size */
5048 lttng_poll_set_max_size();
5052 /* Check for the application socket timeout env variable. */
5053 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
5054 if (env_app_timeout
) {
5055 app_socket_timeout
= atoi(env_app_timeout
);
5057 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
5063 /* Initialize communication library */
5065 /* This is to get the TCP timeout value. */
5066 lttcomm_inet_init();
5069 * Initialize the health check subsystem. This call should set the
5070 * appropriate time values.
5072 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5073 if (!health_sessiond
) {
5074 PERROR("health_app_create error");
5075 goto exit_health_sessiond_cleanup
;
5078 /* Create thread to clean up RCU hash tables */
5079 ret
= pthread_create(&ht_cleanup_thread
, NULL
,
5080 thread_ht_cleanup
, (void *) NULL
);
5082 PERROR("pthread_create ht_cleanup");
5083 goto exit_ht_cleanup
;
5086 /* Create health-check thread */
5087 ret
= pthread_create(&health_thread
, NULL
,
5088 thread_manage_health
, (void *) NULL
);
5090 PERROR("pthread_create health");
5094 /* Create thread to manage the client socket */
5095 ret
= pthread_create(&client_thread
, NULL
,
5096 thread_manage_clients
, (void *) NULL
);
5098 PERROR("pthread_create clients");
5102 /* Create thread to dispatch registration */
5103 ret
= pthread_create(&dispatch_thread
, NULL
,
5104 thread_dispatch_ust_registration
, (void *) NULL
);
5106 PERROR("pthread_create dispatch");
5110 /* Create thread to manage application registration. */
5111 ret
= pthread_create(®_apps_thread
, NULL
,
5112 thread_registration_apps
, (void *) NULL
);
5114 PERROR("pthread_create registration");
5118 /* Create thread to manage application socket */
5119 ret
= pthread_create(&apps_thread
, NULL
,
5120 thread_manage_apps
, (void *) NULL
);
5122 PERROR("pthread_create apps");
5126 /* Create thread to manage application notify socket */
5127 ret
= pthread_create(&apps_notify_thread
, NULL
,
5128 ust_thread_manage_notify
, (void *) NULL
);
5130 PERROR("pthread_create apps");
5131 goto exit_apps_notify
;
5134 /* Create JUL registration thread. */
5135 ret
= pthread_create(&jul_reg_thread
, NULL
,
5136 jul_thread_manage_registration
, (void *) NULL
);
5138 PERROR("pthread_create apps");
5142 /* Don't start this thread if kernel tracing is not requested nor root */
5143 if (is_root
&& !opt_no_kernel
) {
5144 /* Create kernel thread to manage kernel event */
5145 ret
= pthread_create(&kernel_thread
, NULL
,
5146 thread_manage_kernel
, (void *) NULL
);
5148 PERROR("pthread_create kernel");
5152 ret
= pthread_join(kernel_thread
, &status
);
5154 PERROR("pthread_join");
5155 goto error
; /* join error, exit without cleanup */
5160 ret
= pthread_join(jul_reg_thread
, &status
);
5162 PERROR("pthread_join JUL");
5163 goto error
; /* join error, exit without cleanup */
5167 ret
= pthread_join(apps_notify_thread
, &status
);
5169 PERROR("pthread_join apps notify");
5170 goto error
; /* join error, exit without cleanup */
5174 ret
= pthread_join(apps_thread
, &status
);
5176 PERROR("pthread_join apps");
5177 goto error
; /* join error, exit without cleanup */
5182 ret
= pthread_join(reg_apps_thread
, &status
);
5184 PERROR("pthread_join");
5185 goto error
; /* join error, exit without cleanup */
5189 ret
= pthread_join(dispatch_thread
, &status
);
5191 PERROR("pthread_join");
5192 goto error
; /* join error, exit without cleanup */
5196 ret
= pthread_join(client_thread
, &status
);
5198 PERROR("pthread_join");
5199 goto error
; /* join error, exit without cleanup */
5202 ret
= join_consumer_thread(&kconsumer_data
);
5204 PERROR("join_consumer");
5205 goto error
; /* join error, exit without cleanup */
5208 ret
= join_consumer_thread(&ustconsumer32_data
);
5210 PERROR("join_consumer ust32");
5211 goto error
; /* join error, exit without cleanup */
5214 ret
= join_consumer_thread(&ustconsumer64_data
);
5216 PERROR("join_consumer ust64");
5217 goto error
; /* join error, exit without cleanup */
5221 ret
= pthread_join(health_thread
, &status
);
5223 PERROR("pthread_join health thread");
5224 goto error
; /* join error, exit without cleanup */
5228 ret
= pthread_join(ht_cleanup_thread
, &status
);
5230 PERROR("pthread_join ht cleanup thread");
5231 goto error
; /* join error, exit without cleanup */
5234 health_app_destroy(health_sessiond
);
5235 exit_health_sessiond_cleanup
:
5238 * cleanup() is called when no other thread is running.
5240 rcu_thread_online();
5242 rcu_thread_offline();
5243 rcu_unregister_thread();