2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <sys/mount.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
35 #include <sys/types.h>
37 #include <urcu/uatomic.h>
41 #include <common/common.h>
42 #include <common/compat/socket.h>
43 #include <common/defaults.h>
44 #include <common/kernel-consumer/kernel-consumer.h>
45 #include <common/futex.h>
46 #include <common/relayd/relayd.h>
47 #include <common/utils.h>
48 #include <common/daemonize.h>
50 #include "lttng-sessiond.h"
51 #include "buffer-registry.h"
58 #include "kernel-consumer.h"
62 #include "ust-consumer.h"
65 #include "health-sessiond.h"
66 #include "testpoint.h"
67 #include "ust-thread.h"
68 #include "jul-thread.h"
70 #define CONSUMERD_FILE "lttng-consumerd"
73 static const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
74 static const char *opt_pidfile
;
75 static int opt_sig_parent
;
76 static int opt_verbose_consumer
;
77 static int opt_daemon
, opt_background
;
78 static int opt_no_kernel
;
79 static pid_t ppid
; /* Parent PID for --sig-parent option */
80 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
83 /* Set to 1 when a SIGUSR1 signal is received. */
84 static int recv_child_signal
;
87 * Consumer daemon specific control data. Every value not initialized here is
88 * set to 0 by the static definition.
90 static struct consumer_data kconsumer_data
= {
91 .type
= LTTNG_CONSUMER_KERNEL
,
92 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
93 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
96 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
97 .lock
= PTHREAD_MUTEX_INITIALIZER
,
98 .cond
= PTHREAD_COND_INITIALIZER
,
99 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
101 static struct consumer_data ustconsumer64_data
= {
102 .type
= LTTNG_CONSUMER64_UST
,
103 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
104 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
107 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
108 .lock
= PTHREAD_MUTEX_INITIALIZER
,
109 .cond
= PTHREAD_COND_INITIALIZER
,
110 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
112 static struct consumer_data ustconsumer32_data
= {
113 .type
= LTTNG_CONSUMER32_UST
,
114 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
115 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
118 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
119 .lock
= PTHREAD_MUTEX_INITIALIZER
,
120 .cond
= PTHREAD_COND_INITIALIZER
,
121 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
124 /* Shared between threads */
125 static int dispatch_thread_exit
;
127 /* Global application Unix socket path */
128 static char apps_unix_sock_path
[PATH_MAX
];
129 /* Global client Unix socket path */
130 static char client_unix_sock_path
[PATH_MAX
];
131 /* global wait shm path for UST */
132 static char wait_shm_path
[PATH_MAX
];
133 /* Global health check unix path */
134 static char health_unix_sock_path
[PATH_MAX
];
136 /* Sockets and FDs */
137 static int client_sock
= -1;
138 static int apps_sock
= -1;
139 int kernel_tracer_fd
= -1;
140 static int kernel_poll_pipe
[2] = { -1, -1 };
143 * Quit pipe for all threads. This permits a single cancellation point
144 * for all threads when receiving an event on the pipe.
146 static int thread_quit_pipe
[2] = { -1, -1 };
149 * This pipe is used to inform the thread managing application communication
150 * that a command is queued and ready to be processed.
152 static int apps_cmd_pipe
[2] = { -1, -1 };
154 int apps_cmd_notify_pipe
[2] = { -1, -1 };
156 /* Pthread, Mutexes and Semaphores */
157 static pthread_t apps_thread
;
158 static pthread_t apps_notify_thread
;
159 static pthread_t reg_apps_thread
;
160 static pthread_t client_thread
;
161 static pthread_t kernel_thread
;
162 static pthread_t dispatch_thread
;
163 static pthread_t health_thread
;
164 static pthread_t ht_cleanup_thread
;
165 static pthread_t jul_reg_thread
;
168 * UST registration command queue. This queue is tied with a futex and uses a N
169 * wakers / 1 waiter implemented and detailed in futex.c/.h
171 * The thread_manage_apps and thread_dispatch_ust_registration interact with
172 * this queue and the wait/wake scheme.
174 static struct ust_cmd_queue ust_cmd_queue
;
177 * Pointer initialized before thread creation.
179 * This points to the tracing session list containing the session count and a
180 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
181 * MUST NOT be taken if you call a public function in session.c.
183 * The lock is nested inside the structure: session_list_ptr->lock. Please use
184 * session_lock_list and session_unlock_list for lock acquisition.
186 static struct ltt_session_list
*session_list_ptr
;
188 int ust_consumerd64_fd
= -1;
189 int ust_consumerd32_fd
= -1;
191 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
192 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
193 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
194 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
196 static const char *module_proc_lttng
= "/proc/lttng";
199 * Consumer daemon state which is changed when spawning it, killing it or in
200 * case of a fatal error.
202 enum consumerd_state
{
203 CONSUMER_STARTED
= 1,
204 CONSUMER_STOPPED
= 2,
209 * This consumer daemon state is used to validate if a client command will be
210 * able to reach the consumer. If not, the client is informed. For instance,
211 * doing a "lttng start" when the consumer state is set to ERROR will return an
212 * error to the client.
214 * The following example shows a possible race condition of this scheme:
216 * consumer thread error happens
218 * client cmd checks state -> still OK
219 * consumer thread exit, sets error
220 * client cmd try to talk to consumer
223 * However, since the consumer is a different daemon, we have no way of making
224 * sure the command will reach it safely even with this state flag. This is why
225 * we consider that up to the state validation during command processing, the
226 * command is safe. After that, we can not guarantee the correctness of the
227 * client request vis-a-vis the consumer.
229 static enum consumerd_state ust_consumerd_state
;
230 static enum consumerd_state kernel_consumerd_state
;
233 * Socket timeout for receiving and sending in seconds.
235 static int app_socket_timeout
;
237 /* Set in main() with the current page size. */
240 /* Application health monitoring */
241 struct health_app
*health_sessiond
;
243 /* JUL TCP port for registration. Used by the JUL thread. */
244 unsigned int jul_tcp_port
= DEFAULT_JUL_TCP_PORT
;
246 /* Am I root or not. */
247 int is_root
; /* Set to 1 if the daemon is running as root */
250 * Whether sessiond is ready for commands/health check requests.
251 * NR_LTTNG_SESSIOND_READY must match the number of calls to
252 * lttng_sessiond_notify_ready().
254 #define NR_LTTNG_SESSIOND_READY 2
255 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_READY
;
257 /* Notify parents that we are ready for cmd and health check */
259 void lttng_sessiond_notify_ready(void)
261 if (uatomic_sub_return(<tng_sessiond_ready
, 1) == 0) {
263 * Notify parent pid that we are ready to accept command
264 * for client side. This ppid is the one from the
265 * external process that spawned us.
267 if (opt_sig_parent
) {
272 * Notify the parent of the fork() process that we are
275 if (opt_daemon
|| opt_background
) {
276 kill(child_ppid
, SIGUSR1
);
282 void setup_consumerd_path(void)
284 const char *bin
, *libdir
;
287 * Allow INSTALL_BIN_PATH to be used as a target path for the
288 * native architecture size consumer if CONFIG_CONSUMER*_PATH
289 * has not been defined.
291 #if (CAA_BITS_PER_LONG == 32)
292 if (!consumerd32_bin
[0]) {
293 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
295 if (!consumerd32_libdir
[0]) {
296 consumerd32_libdir
= INSTALL_LIB_PATH
;
298 #elif (CAA_BITS_PER_LONG == 64)
299 if (!consumerd64_bin
[0]) {
300 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
302 if (!consumerd64_libdir
[0]) {
303 consumerd64_libdir
= INSTALL_LIB_PATH
;
306 #error "Unknown bitness"
310 * runtime env. var. overrides the build default.
312 bin
= getenv("LTTNG_CONSUMERD32_BIN");
314 consumerd32_bin
= bin
;
316 bin
= getenv("LTTNG_CONSUMERD64_BIN");
318 consumerd64_bin
= bin
;
320 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
322 consumerd32_libdir
= libdir
;
324 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
326 consumerd64_libdir
= libdir
;
331 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
333 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
339 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
345 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
357 * Check if the thread quit pipe was triggered.
359 * Return 1 if it was triggered else 0;
361 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
363 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
371 * Init thread quit pipe.
373 * Return -1 on error or 0 if all pipes are created.
375 static int init_thread_quit_pipe(void)
379 ret
= pipe(thread_quit_pipe
);
381 PERROR("thread quit pipe");
385 for (i
= 0; i
< 2; i
++) {
386 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
398 * Stop all threads by closing the thread quit pipe.
400 static void stop_threads(void)
404 /* Stopping all threads */
405 DBG("Terminating all threads");
406 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
408 ERR("write error on thread quit pipe");
411 /* Dispatch thread */
412 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
413 futex_nto1_wake(&ust_cmd_queue
.futex
);
417 * Close every consumer sockets.
419 static void close_consumer_sockets(void)
423 if (kconsumer_data
.err_sock
>= 0) {
424 ret
= close(kconsumer_data
.err_sock
);
426 PERROR("kernel consumer err_sock close");
429 if (ustconsumer32_data
.err_sock
>= 0) {
430 ret
= close(ustconsumer32_data
.err_sock
);
432 PERROR("UST consumerd32 err_sock close");
435 if (ustconsumer64_data
.err_sock
>= 0) {
436 ret
= close(ustconsumer64_data
.err_sock
);
438 PERROR("UST consumerd64 err_sock close");
441 if (kconsumer_data
.cmd_sock
>= 0) {
442 ret
= close(kconsumer_data
.cmd_sock
);
444 PERROR("kernel consumer cmd_sock close");
447 if (ustconsumer32_data
.cmd_sock
>= 0) {
448 ret
= close(ustconsumer32_data
.cmd_sock
);
450 PERROR("UST consumerd32 cmd_sock close");
453 if (ustconsumer64_data
.cmd_sock
>= 0) {
454 ret
= close(ustconsumer64_data
.cmd_sock
);
456 PERROR("UST consumerd64 cmd_sock close");
464 static void cleanup(void)
467 struct ltt_session
*sess
, *stmp
;
473 * Close the thread quit pipe. It has already done its job,
474 * since we are now called.
476 utils_close_pipe(thread_quit_pipe
);
479 * If opt_pidfile is undefined, the default file will be wiped when
480 * removing the rundir.
483 ret
= remove(opt_pidfile
);
485 PERROR("remove pidfile %s", opt_pidfile
);
489 DBG("Removing sessiond and consumerd content of directory %s", rundir
);
492 snprintf(path
, PATH_MAX
,
494 rundir
, DEFAULT_LTTNG_SESSIOND_PIDFILE
);
495 DBG("Removing %s", path
);
498 snprintf(path
, PATH_MAX
, "%s/%s", rundir
,
499 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE
);
500 DBG("Removing %s", path
);
504 snprintf(path
, PATH_MAX
,
505 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
507 DBG("Removing %s", path
);
510 snprintf(path
, PATH_MAX
,
511 DEFAULT_KCONSUMERD_PATH
,
513 DBG("Removing directory %s", path
);
516 /* ust consumerd 32 */
517 snprintf(path
, PATH_MAX
,
518 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
520 DBG("Removing %s", path
);
523 snprintf(path
, PATH_MAX
,
524 DEFAULT_USTCONSUMERD32_PATH
,
526 DBG("Removing directory %s", path
);
529 /* ust consumerd 64 */
530 snprintf(path
, PATH_MAX
,
531 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
533 DBG("Removing %s", path
);
536 snprintf(path
, PATH_MAX
,
537 DEFAULT_USTCONSUMERD64_PATH
,
539 DBG("Removing directory %s", path
);
543 * We do NOT rmdir rundir because there are other processes
544 * using it, for instance lttng-relayd, which can start in
545 * parallel with this teardown.
550 DBG("Cleaning up all sessions");
552 /* Destroy session list mutex */
553 if (session_list_ptr
!= NULL
) {
554 pthread_mutex_destroy(&session_list_ptr
->lock
);
556 /* Cleanup ALL session */
557 cds_list_for_each_entry_safe(sess
, stmp
,
558 &session_list_ptr
->head
, list
) {
559 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
563 DBG("Closing all UST sockets");
564 ust_app_clean_list();
565 buffer_reg_destroy_registries();
567 if (is_root
&& !opt_no_kernel
) {
568 DBG2("Closing kernel fd");
569 if (kernel_tracer_fd
>= 0) {
570 ret
= close(kernel_tracer_fd
);
575 DBG("Unloading kernel modules");
576 modprobe_remove_lttng_all();
579 close_consumer_sockets();
582 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
583 "Matthew, BEET driven development works!%c[%dm",
584 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
589 * Send data on a unix socket using the liblttsessiondcomm API.
591 * Return lttcomm error code.
593 static int send_unix_sock(int sock
, void *buf
, size_t len
)
595 /* Check valid length */
600 return lttcomm_send_unix_sock(sock
, buf
, len
);
604 * Free memory of a command context structure.
606 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
608 DBG("Clean command context structure");
610 if ((*cmd_ctx
)->llm
) {
611 free((*cmd_ctx
)->llm
);
613 if ((*cmd_ctx
)->lsm
) {
614 free((*cmd_ctx
)->lsm
);
622 * Notify UST applications using the shm mmap futex.
624 static int notify_ust_apps(int active
)
628 DBG("Notifying applications of session daemon state: %d", active
);
630 /* See shm.c for this call implying mmap, shm and futex calls */
631 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
632 if (wait_shm_mmap
== NULL
) {
636 /* Wake waiting process */
637 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
639 /* Apps notified successfully */
647 * Setup the outgoing data buffer for the response (llm) by allocating the
648 * right amount of memory and copying the original information from the lsm
651 * Return total size of the buffer pointed by buf.
653 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
659 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
660 if (cmd_ctx
->llm
== NULL
) {
666 /* Copy common data */
667 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
668 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
670 cmd_ctx
->llm
->data_size
= size
;
671 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
680 * Update the kernel poll set of all channel fd available over all tracing
681 * session. Add the wakeup pipe at the end of the set.
683 static int update_kernel_poll(struct lttng_poll_event
*events
)
686 struct ltt_session
*session
;
687 struct ltt_kernel_channel
*channel
;
689 DBG("Updating kernel poll set");
692 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
693 session_lock(session
);
694 if (session
->kernel_session
== NULL
) {
695 session_unlock(session
);
699 cds_list_for_each_entry(channel
,
700 &session
->kernel_session
->channel_list
.head
, list
) {
701 /* Add channel fd to the kernel poll set */
702 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
704 session_unlock(session
);
707 DBG("Channel fd %d added to kernel set", channel
->fd
);
709 session_unlock(session
);
711 session_unlock_list();
716 session_unlock_list();
721 * Find the channel fd from 'fd' over all tracing session. When found, check
722 * for new channel stream and send those stream fds to the kernel consumer.
724 * Useful for CPU hotplug feature.
726 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
729 struct ltt_session
*session
;
730 struct ltt_kernel_session
*ksess
;
731 struct ltt_kernel_channel
*channel
;
733 DBG("Updating kernel streams for channel fd %d", fd
);
736 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
737 session_lock(session
);
738 if (session
->kernel_session
== NULL
) {
739 session_unlock(session
);
742 ksess
= session
->kernel_session
;
744 cds_list_for_each_entry(channel
, &ksess
->channel_list
.head
, list
) {
745 if (channel
->fd
== fd
) {
746 DBG("Channel found, updating kernel streams");
747 ret
= kernel_open_channel_stream(channel
);
751 /* Update the stream global counter */
752 ksess
->stream_count_global
+= ret
;
755 * Have we already sent fds to the consumer? If yes, it means
756 * that tracing is started so it is safe to send our updated
759 if (ksess
->consumer_fds_sent
== 1 && ksess
->consumer
!= NULL
) {
760 struct lttng_ht_iter iter
;
761 struct consumer_socket
*socket
;
764 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
765 &iter
.iter
, socket
, node
.node
) {
766 pthread_mutex_lock(socket
->lock
);
767 ret
= kernel_consumer_send_channel_stream(socket
,
769 session
->output_traces
? 1 : 0);
770 pthread_mutex_unlock(socket
->lock
);
781 session_unlock(session
);
783 session_unlock_list();
787 session_unlock(session
);
788 session_unlock_list();
793 * For each tracing session, update newly registered apps. The session list
794 * lock MUST be acquired before calling this.
796 static void update_ust_app(int app_sock
)
798 struct ltt_session
*sess
, *stmp
;
800 /* Consumer is in an ERROR state. Stop any application update. */
801 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
802 /* Stop the update process since the consumer is dead. */
806 /* For all tracing session(s) */
807 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
809 if (sess
->ust_session
) {
810 ust_app_global_update(sess
->ust_session
, app_sock
);
812 session_unlock(sess
);
817 * This thread manage event coming from the kernel.
819 * Features supported in this thread:
822 static void *thread_manage_kernel(void *data
)
824 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
825 uint32_t revents
, nb_fd
;
827 struct lttng_poll_event events
;
829 DBG("[thread] Thread manage kernel started");
831 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
834 * This first step of the while is to clean this structure which could free
835 * non NULL pointers so initialize it before the loop.
837 lttng_poll_init(&events
);
839 if (testpoint(sessiond_thread_manage_kernel
)) {
840 goto error_testpoint
;
843 health_code_update();
845 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
846 goto error_testpoint
;
850 health_code_update();
852 if (update_poll_flag
== 1) {
853 /* Clean events object. We are about to populate it again. */
854 lttng_poll_clean(&events
);
856 ret
= sessiond_set_thread_pollset(&events
, 2);
858 goto error_poll_create
;
861 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
866 /* This will add the available kernel channel if any. */
867 ret
= update_kernel_poll(&events
);
871 update_poll_flag
= 0;
874 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events
));
876 /* Poll infinite value of time */
879 ret
= lttng_poll_wait(&events
, -1);
883 * Restart interrupted system call.
885 if (errno
== EINTR
) {
889 } else if (ret
== 0) {
890 /* Should not happen since timeout is infinite */
891 ERR("Return value of poll is 0 with an infinite timeout.\n"
892 "This should not have happened! Continuing...");
898 for (i
= 0; i
< nb_fd
; i
++) {
899 /* Fetch once the poll data */
900 revents
= LTTNG_POLL_GETEV(&events
, i
);
901 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
903 health_code_update();
905 /* Thread quit pipe has been closed. Killing thread. */
906 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
912 /* Check for data on kernel pipe */
913 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
914 (void) lttng_read(kernel_poll_pipe
[0],
917 * Ret value is useless here, if this pipe gets any actions an
918 * update is required anyway.
920 update_poll_flag
= 1;
924 * New CPU detected by the kernel. Adding kernel stream to
925 * kernel session and updating the kernel consumer
927 if (revents
& LPOLLIN
) {
928 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
934 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
935 * and unregister kernel stream at this point.
944 lttng_poll_clean(&events
);
947 utils_close_pipe(kernel_poll_pipe
);
948 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
951 ERR("Health error occurred in %s", __func__
);
952 WARN("Kernel thread died unexpectedly. "
953 "Kernel tracing can continue but CPU hotplug is disabled.");
955 health_unregister(health_sessiond
);
956 DBG("Kernel thread dying");
961 * Signal pthread condition of the consumer data that the thread.
963 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
965 pthread_mutex_lock(&data
->cond_mutex
);
968 * The state is set before signaling. It can be any value, it's the waiter
969 * job to correctly interpret this condition variable associated to the
970 * consumer pthread_cond.
972 * A value of 0 means that the corresponding thread of the consumer data
973 * was not started. 1 indicates that the thread has started and is ready
974 * for action. A negative value means that there was an error during the
977 data
->consumer_thread_is_ready
= state
;
978 (void) pthread_cond_signal(&data
->cond
);
980 pthread_mutex_unlock(&data
->cond_mutex
);
984 * This thread manage the consumer error sent back to the session daemon.
986 static void *thread_manage_consumer(void *data
)
988 int sock
= -1, i
, ret
, pollfd
, err
= -1;
989 uint32_t revents
, nb_fd
;
990 enum lttcomm_return_code code
;
991 struct lttng_poll_event events
;
992 struct consumer_data
*consumer_data
= data
;
994 DBG("[thread] Manage consumer started");
996 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
998 health_code_update();
1001 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1002 * metadata_sock. Nothing more will be added to this poll set.
1004 ret
= sessiond_set_thread_pollset(&events
, 3);
1010 * The error socket here is already in a listening state which was done
1011 * just before spawning this thread to avoid a race between the consumer
1012 * daemon exec trying to connect and the listen() call.
1014 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1019 health_code_update();
1021 /* Infinite blocking call, waiting for transmission */
1023 health_poll_entry();
1025 if (testpoint(sessiond_thread_manage_consumer
)) {
1029 ret
= lttng_poll_wait(&events
, -1);
1033 * Restart interrupted system call.
1035 if (errno
== EINTR
) {
1043 for (i
= 0; i
< nb_fd
; i
++) {
1044 /* Fetch once the poll data */
1045 revents
= LTTNG_POLL_GETEV(&events
, i
);
1046 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1048 health_code_update();
1050 /* Thread quit pipe has been closed. Killing thread. */
1051 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1057 /* Event on the registration socket */
1058 if (pollfd
== consumer_data
->err_sock
) {
1059 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1060 ERR("consumer err socket poll error");
1066 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1072 * Set the CLOEXEC flag. Return code is useless because either way, the
1075 (void) utils_set_fd_cloexec(sock
);
1077 health_code_update();
1079 DBG2("Receiving code from consumer err_sock");
1081 /* Getting status code from kconsumerd */
1082 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1083 sizeof(enum lttcomm_return_code
));
1088 health_code_update();
1089 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1090 /* Connect both socket, command and metadata. */
1091 consumer_data
->cmd_sock
=
1092 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1093 consumer_data
->metadata_fd
=
1094 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1095 if (consumer_data
->cmd_sock
< 0
1096 || consumer_data
->metadata_fd
< 0) {
1097 PERROR("consumer connect cmd socket");
1098 /* On error, signal condition and quit. */
1099 signal_consumer_condition(consumer_data
, -1);
1102 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1103 /* Create metadata socket lock. */
1104 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1105 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1106 PERROR("zmalloc pthread mutex");
1110 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1112 signal_consumer_condition(consumer_data
, 1);
1113 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1114 DBG("Consumer metadata socket ready (fd: %d)",
1115 consumer_data
->metadata_fd
);
1117 ERR("consumer error when waiting for SOCK_READY : %s",
1118 lttcomm_get_readable_code(-code
));
1122 /* Remove the consumerd error sock since we've established a connexion */
1123 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1128 /* Add new accepted error socket. */
1129 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1134 /* Add metadata socket that is successfully connected. */
1135 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1136 LPOLLIN
| LPOLLRDHUP
);
1141 health_code_update();
1143 /* Infinite blocking call, waiting for transmission */
1146 health_poll_entry();
1147 ret
= lttng_poll_wait(&events
, -1);
1151 * Restart interrupted system call.
1153 if (errno
== EINTR
) {
1161 for (i
= 0; i
< nb_fd
; i
++) {
1162 /* Fetch once the poll data */
1163 revents
= LTTNG_POLL_GETEV(&events
, i
);
1164 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1166 health_code_update();
1168 /* Thread quit pipe has been closed. Killing thread. */
1169 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1175 if (pollfd
== sock
) {
1176 /* Event on the consumerd socket */
1177 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1178 ERR("consumer err socket second poll error");
1181 health_code_update();
1182 /* Wait for any kconsumerd error */
1183 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1184 sizeof(enum lttcomm_return_code
));
1186 ERR("consumer closed the command socket");
1190 ERR("consumer return code : %s",
1191 lttcomm_get_readable_code(-code
));
1194 } else if (pollfd
== consumer_data
->metadata_fd
) {
1195 /* UST metadata requests */
1196 ret
= ust_consumer_metadata_request(
1197 &consumer_data
->metadata_sock
);
1199 ERR("Handling metadata request");
1204 ERR("Unknown pollfd");
1208 health_code_update();
1214 * We lock here because we are about to close the sockets and some other
1215 * thread might be using them so get exclusive access which will abort all
1216 * other consumer command by other threads.
1218 pthread_mutex_lock(&consumer_data
->lock
);
1220 /* Immediately set the consumerd state to stopped */
1221 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1222 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1223 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1224 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1225 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1227 /* Code flow error... */
1231 if (consumer_data
->err_sock
>= 0) {
1232 ret
= close(consumer_data
->err_sock
);
1236 consumer_data
->err_sock
= -1;
1238 if (consumer_data
->cmd_sock
>= 0) {
1239 ret
= close(consumer_data
->cmd_sock
);
1243 consumer_data
->cmd_sock
= -1;
1245 if (consumer_data
->metadata_sock
.fd_ptr
&&
1246 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1247 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1259 unlink(consumer_data
->err_unix_sock_path
);
1260 unlink(consumer_data
->cmd_unix_sock_path
);
1261 consumer_data
->pid
= 0;
1262 pthread_mutex_unlock(&consumer_data
->lock
);
1264 /* Cleanup metadata socket mutex. */
1265 if (consumer_data
->metadata_sock
.lock
) {
1266 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1267 free(consumer_data
->metadata_sock
.lock
);
1269 lttng_poll_clean(&events
);
1273 ERR("Health error occurred in %s", __func__
);
1275 health_unregister(health_sessiond
);
1276 DBG("consumer thread cleanup completed");
1282 * This thread manage application communication.
1284 static void *thread_manage_apps(void *data
)
1286 int i
, ret
, pollfd
, err
= -1;
1288 uint32_t revents
, nb_fd
;
1289 struct lttng_poll_event events
;
1291 DBG("[thread] Manage application started");
1293 rcu_register_thread();
1294 rcu_thread_online();
1296 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1298 if (testpoint(sessiond_thread_manage_apps
)) {
1299 goto error_testpoint
;
1302 health_code_update();
1304 ret
= sessiond_set_thread_pollset(&events
, 2);
1306 goto error_poll_create
;
1309 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1314 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1318 health_code_update();
1321 DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events
));
1323 /* Inifinite blocking call, waiting for transmission */
1325 health_poll_entry();
1326 ret
= lttng_poll_wait(&events
, -1);
1330 * Restart interrupted system call.
1332 if (errno
== EINTR
) {
1340 for (i
= 0; i
< nb_fd
; i
++) {
1341 /* Fetch once the poll data */
1342 revents
= LTTNG_POLL_GETEV(&events
, i
);
1343 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1345 health_code_update();
1347 /* Thread quit pipe has been closed. Killing thread. */
1348 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1354 /* Inspect the apps cmd pipe */
1355 if (pollfd
== apps_cmd_pipe
[0]) {
1356 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1357 ERR("Apps command pipe error");
1359 } else if (revents
& LPOLLIN
) {
1363 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1364 if (size_ret
< sizeof(sock
)) {
1365 PERROR("read apps cmd pipe");
1369 health_code_update();
1372 * We only monitor the error events of the socket. This
1373 * thread does not handle any incoming data from UST
1376 ret
= lttng_poll_add(&events
, sock
,
1377 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1382 DBG("Apps with sock %d added to poll set", sock
);
1386 * At this point, we know that a registered application made
1387 * the event at poll_wait.
1389 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1390 /* Removing from the poll set */
1391 ret
= lttng_poll_del(&events
, pollfd
);
1396 /* Socket closed on remote end. */
1397 ust_app_unregister(pollfd
);
1401 health_code_update();
1407 lttng_poll_clean(&events
);
1410 utils_close_pipe(apps_cmd_pipe
);
1411 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1414 * We don't clean the UST app hash table here since already registered
1415 * applications can still be controlled so let them be until the session
1416 * daemon dies or the applications stop.
1421 ERR("Health error occurred in %s", __func__
);
1423 health_unregister(health_sessiond
);
1424 DBG("Application communication apps thread cleanup complete");
1425 rcu_thread_offline();
1426 rcu_unregister_thread();
1431 * Send a socket to a thread This is called from the dispatch UST registration
1432 * thread once all sockets are set for the application.
1434 * The sock value can be invalid, we don't really care, the thread will handle
1435 * it and make the necessary cleanup if so.
1437 * On success, return 0 else a negative value being the errno message of the
1440 static int send_socket_to_thread(int fd
, int sock
)
1445 * It's possible that the FD is set as invalid with -1 concurrently just
1446 * before calling this function being a shutdown state of the thread.
1453 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1454 if (ret
< sizeof(sock
)) {
1455 PERROR("write apps pipe %d", fd
);
1462 /* All good. Don't send back the write positive ret value. */
1469 * Sanitize the wait queue of the dispatch registration thread meaning removing
1470 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1471 * notify socket is never received.
1473 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1475 int ret
, nb_fd
= 0, i
;
1476 unsigned int fd_added
= 0;
1477 struct lttng_poll_event events
;
1478 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1482 lttng_poll_init(&events
);
1484 /* Just skip everything for an empty queue. */
1485 if (!wait_queue
->count
) {
1489 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1494 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1495 &wait_queue
->head
, head
) {
1496 assert(wait_node
->app
);
1497 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1498 LPOLLHUP
| LPOLLERR
);
1511 * Poll but don't block so we can quickly identify the faulty events and
1512 * clean them afterwards from the wait queue.
1514 ret
= lttng_poll_wait(&events
, 0);
1520 for (i
= 0; i
< nb_fd
; i
++) {
1521 /* Get faulty FD. */
1522 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1523 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1525 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1526 &wait_queue
->head
, head
) {
1527 if (pollfd
== wait_node
->app
->sock
&&
1528 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1529 cds_list_del(&wait_node
->head
);
1530 wait_queue
->count
--;
1531 ust_app_destroy(wait_node
->app
);
1539 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1543 lttng_poll_clean(&events
);
1547 lttng_poll_clean(&events
);
1549 ERR("Unable to sanitize wait queue");
1554 * Dispatch request from the registration threads to the application
1555 * communication thread.
1557 static void *thread_dispatch_ust_registration(void *data
)
1560 struct cds_wfq_node
*node
;
1561 struct ust_command
*ust_cmd
= NULL
;
1562 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1563 struct ust_reg_wait_queue wait_queue
= {
1567 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1569 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1570 goto error_testpoint
;
1573 health_code_update();
1575 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1577 DBG("[thread] Dispatch UST command started");
1579 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1580 health_code_update();
1582 /* Atomically prepare the queue futex */
1583 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1586 struct ust_app
*app
= NULL
;
1590 * Make sure we don't have node(s) that have hung up before receiving
1591 * the notify socket. This is to clean the list in order to avoid
1592 * memory leaks from notify socket that are never seen.
1594 sanitize_wait_queue(&wait_queue
);
1596 health_code_update();
1597 /* Dequeue command for registration */
1598 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1600 DBG("Woken up but nothing in the UST command queue");
1601 /* Continue thread execution */
1605 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1607 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1608 " gid:%d sock:%d name:%s (version %d.%d)",
1609 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1610 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1611 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1612 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1614 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1615 wait_node
= zmalloc(sizeof(*wait_node
));
1617 PERROR("zmalloc wait_node dispatch");
1618 ret
= close(ust_cmd
->sock
);
1620 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1622 lttng_fd_put(LTTNG_FD_APPS
, 1);
1626 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1628 /* Create application object if socket is CMD. */
1629 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1631 if (!wait_node
->app
) {
1632 ret
= close(ust_cmd
->sock
);
1634 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1636 lttng_fd_put(LTTNG_FD_APPS
, 1);
1642 * Add application to the wait queue so we can set the notify
1643 * socket before putting this object in the global ht.
1645 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1650 * We have to continue here since we don't have the notify
1651 * socket and the application MUST be added to the hash table
1652 * only at that moment.
1657 * Look for the application in the local wait queue and set the
1658 * notify socket if found.
1660 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1661 &wait_queue
.head
, head
) {
1662 health_code_update();
1663 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1664 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1665 cds_list_del(&wait_node
->head
);
1667 app
= wait_node
->app
;
1669 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1675 * With no application at this stage the received socket is
1676 * basically useless so close it before we free the cmd data
1677 * structure for good.
1680 ret
= close(ust_cmd
->sock
);
1682 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1684 lttng_fd_put(LTTNG_FD_APPS
, 1);
1691 * @session_lock_list
1693 * Lock the global session list so from the register up to the
1694 * registration done message, no thread can see the application
1695 * and change its state.
1697 session_lock_list();
1701 * Add application to the global hash table. This needs to be
1702 * done before the update to the UST registry can locate the
1707 /* Set app version. This call will print an error if needed. */
1708 (void) ust_app_version(app
);
1710 /* Send notify socket through the notify pipe. */
1711 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1715 session_unlock_list();
1717 * No notify thread, stop the UST tracing. However, this is
1718 * not an internal error of the this thread thus setting
1719 * the health error code to a normal exit.
1726 * Update newly registered application with the tracing
1727 * registry info already enabled information.
1729 update_ust_app(app
->sock
);
1732 * Don't care about return value. Let the manage apps threads
1733 * handle app unregistration upon socket close.
1735 (void) ust_app_register_done(app
->sock
);
1738 * Even if the application socket has been closed, send the app
1739 * to the thread and unregistration will take place at that
1742 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1745 session_unlock_list();
1747 * No apps. thread, stop the UST tracing. However, this is
1748 * not an internal error of the this thread thus setting
1749 * the health error code to a normal exit.
1756 session_unlock_list();
1758 } while (node
!= NULL
);
1760 health_poll_entry();
1761 /* Futex wait on queue. Blocking call on futex() */
1762 futex_nto1_wait(&ust_cmd_queue
.futex
);
1765 /* Normal exit, no error */
1769 /* Clean up wait queue. */
1770 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1771 &wait_queue
.head
, head
) {
1772 cds_list_del(&wait_node
->head
);
1778 DBG("Dispatch thread dying");
1781 ERR("Health error occurred in %s", __func__
);
1783 health_unregister(health_sessiond
);
1788 * This thread manage application registration.
1790 static void *thread_registration_apps(void *data
)
1792 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1793 uint32_t revents
, nb_fd
;
1794 struct lttng_poll_event events
;
1796 * Get allocated in this thread, enqueued to a global queue, dequeued and
1797 * freed in the manage apps thread.
1799 struct ust_command
*ust_cmd
= NULL
;
1801 DBG("[thread] Manage application registration started");
1803 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
1805 if (testpoint(sessiond_thread_registration_apps
)) {
1806 goto error_testpoint
;
1809 ret
= lttcomm_listen_unix_sock(apps_sock
);
1815 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1816 * more will be added to this poll set.
1818 ret
= sessiond_set_thread_pollset(&events
, 2);
1820 goto error_create_poll
;
1823 /* Add the application registration socket */
1824 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1826 goto error_poll_add
;
1829 /* Notify all applications to register */
1830 ret
= notify_ust_apps(1);
1832 ERR("Failed to notify applications or create the wait shared memory.\n"
1833 "Execution continues but there might be problem for already\n"
1834 "running applications that wishes to register.");
1838 DBG("Accepting application registration");
1840 /* Inifinite blocking call, waiting for transmission */
1842 health_poll_entry();
1843 ret
= lttng_poll_wait(&events
, -1);
1847 * Restart interrupted system call.
1849 if (errno
== EINTR
) {
1857 for (i
= 0; i
< nb_fd
; i
++) {
1858 health_code_update();
1860 /* Fetch once the poll data */
1861 revents
= LTTNG_POLL_GETEV(&events
, i
);
1862 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1864 /* Thread quit pipe has been closed. Killing thread. */
1865 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1871 /* Event on the registration socket */
1872 if (pollfd
== apps_sock
) {
1873 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1874 ERR("Register apps socket poll error");
1876 } else if (revents
& LPOLLIN
) {
1877 sock
= lttcomm_accept_unix_sock(apps_sock
);
1883 * Set socket timeout for both receiving and ending.
1884 * app_socket_timeout is in seconds, whereas
1885 * lttcomm_setsockopt_rcv_timeout and
1886 * lttcomm_setsockopt_snd_timeout expect msec as
1889 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1890 app_socket_timeout
* 1000);
1891 (void) lttcomm_setsockopt_snd_timeout(sock
,
1892 app_socket_timeout
* 1000);
1895 * Set the CLOEXEC flag. Return code is useless because
1896 * either way, the show must go on.
1898 (void) utils_set_fd_cloexec(sock
);
1900 /* Create UST registration command for enqueuing */
1901 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1902 if (ust_cmd
== NULL
) {
1903 PERROR("ust command zmalloc");
1908 * Using message-based transmissions to ensure we don't
1909 * have to deal with partially received messages.
1911 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1913 ERR("Exhausted file descriptors allowed for applications.");
1923 health_code_update();
1924 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
1927 /* Close socket of the application. */
1932 lttng_fd_put(LTTNG_FD_APPS
, 1);
1936 health_code_update();
1938 ust_cmd
->sock
= sock
;
1941 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1942 " gid:%d sock:%d name:%s (version %d.%d)",
1943 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1944 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1945 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1946 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1949 * Lock free enqueue the registration request. The red pill
1950 * has been taken! This apps will be part of the *system*.
1952 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1955 * Wake the registration queue futex. Implicit memory
1956 * barrier with the exchange in cds_wfq_enqueue.
1958 futex_nto1_wake(&ust_cmd_queue
.futex
);
1966 /* Notify that the registration thread is gone */
1969 if (apps_sock
>= 0) {
1970 ret
= close(apps_sock
);
1980 lttng_fd_put(LTTNG_FD_APPS
, 1);
1982 unlink(apps_unix_sock_path
);
1985 lttng_poll_clean(&events
);
1989 DBG("UST Registration thread cleanup complete");
1992 ERR("Health error occurred in %s", __func__
);
1994 health_unregister(health_sessiond
);
2000 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2001 * exec or it will fails.
2003 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2006 struct timespec timeout
;
2008 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2009 consumer_data
->consumer_thread_is_ready
= 0;
2011 /* Setup pthread condition */
2012 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2015 PERROR("pthread_condattr_init consumer data");
2020 * Set the monotonic clock in order to make sure we DO NOT jump in time
2021 * between the clock_gettime() call and the timedwait call. See bug #324
2022 * for a more details and how we noticed it.
2024 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2027 PERROR("pthread_condattr_setclock consumer data");
2031 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2034 PERROR("pthread_cond_init consumer data");
2038 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
2041 PERROR("pthread_create consumer");
2046 /* We are about to wait on a pthread condition */
2047 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2049 /* Get time for sem_timedwait absolute timeout */
2050 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2052 * Set the timeout for the condition timed wait even if the clock gettime
2053 * call fails since we might loop on that call and we want to avoid to
2054 * increment the timeout too many times.
2056 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2059 * The following loop COULD be skipped in some conditions so this is why we
2060 * set ret to 0 in order to make sure at least one round of the loop is
2066 * Loop until the condition is reached or when a timeout is reached. Note
2067 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2068 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2069 * possible. This loop does not take any chances and works with both of
2072 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2073 if (clock_ret
< 0) {
2074 PERROR("clock_gettime spawn consumer");
2075 /* Infinite wait for the consumerd thread to be ready */
2076 ret
= pthread_cond_wait(&consumer_data
->cond
,
2077 &consumer_data
->cond_mutex
);
2079 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2080 &consumer_data
->cond_mutex
, &timeout
);
2084 /* Release the pthread condition */
2085 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2089 if (ret
== ETIMEDOUT
) {
2093 * Call has timed out so we kill the kconsumerd_thread and return
2096 ERR("Condition timed out. The consumer thread was never ready."
2098 pth_ret
= pthread_cancel(consumer_data
->thread
);
2100 PERROR("pthread_cancel consumer thread");
2103 PERROR("pthread_cond_wait failed consumer thread");
2105 /* Caller is expecting a negative value on failure. */
2110 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2111 if (consumer_data
->pid
== 0) {
2112 ERR("Consumerd did not start");
2113 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2116 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2125 * Join consumer thread
2127 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2131 /* Consumer pid must be a real one. */
2132 if (consumer_data
->pid
> 0) {
2134 ret
= kill(consumer_data
->pid
, SIGTERM
);
2136 ERR("Error killing consumer daemon");
2139 return pthread_join(consumer_data
->thread
, &status
);
2146 * Fork and exec a consumer daemon (consumerd).
2148 * Return pid if successful else -1.
2150 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2154 const char *consumer_to_use
;
2155 const char *verbosity
;
2158 DBG("Spawning consumerd");
2165 if (opt_verbose_consumer
) {
2166 verbosity
= "--verbose";
2168 verbosity
= "--quiet";
2170 switch (consumer_data
->type
) {
2171 case LTTNG_CONSUMER_KERNEL
:
2173 * Find out which consumerd to execute. We will first try the
2174 * 64-bit path, then the sessiond's installation directory, and
2175 * fallback on the 32-bit one,
2177 DBG3("Looking for a kernel consumer at these locations:");
2178 DBG3(" 1) %s", consumerd64_bin
);
2179 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2180 DBG3(" 3) %s", consumerd32_bin
);
2181 if (stat(consumerd64_bin
, &st
) == 0) {
2182 DBG3("Found location #1");
2183 consumer_to_use
= consumerd64_bin
;
2184 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2185 DBG3("Found location #2");
2186 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2187 } else if (stat(consumerd32_bin
, &st
) == 0) {
2188 DBG3("Found location #3");
2189 consumer_to_use
= consumerd32_bin
;
2191 DBG("Could not find any valid consumerd executable");
2195 DBG("Using kernel consumer at: %s", consumer_to_use
);
2196 ret
= execl(consumer_to_use
,
2197 "lttng-consumerd", verbosity
, "-k",
2198 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2199 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2200 "--group", tracing_group_name
,
2203 case LTTNG_CONSUMER64_UST
:
2205 char *tmpnew
= NULL
;
2207 if (consumerd64_libdir
[0] != '\0') {
2211 tmp
= getenv("LD_LIBRARY_PATH");
2215 tmplen
= strlen("LD_LIBRARY_PATH=")
2216 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2217 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2222 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2223 strcat(tmpnew
, consumerd64_libdir
);
2224 if (tmp
[0] != '\0') {
2225 strcat(tmpnew
, ":");
2226 strcat(tmpnew
, tmp
);
2228 ret
= putenv(tmpnew
);
2235 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2236 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2237 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2238 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2239 "--group", tracing_group_name
,
2241 if (consumerd64_libdir
[0] != '\0') {
2246 case LTTNG_CONSUMER32_UST
:
2248 char *tmpnew
= NULL
;
2250 if (consumerd32_libdir
[0] != '\0') {
2254 tmp
= getenv("LD_LIBRARY_PATH");
2258 tmplen
= strlen("LD_LIBRARY_PATH=")
2259 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2260 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2265 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2266 strcat(tmpnew
, consumerd32_libdir
);
2267 if (tmp
[0] != '\0') {
2268 strcat(tmpnew
, ":");
2269 strcat(tmpnew
, tmp
);
2271 ret
= putenv(tmpnew
);
2278 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2279 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2280 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2281 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2282 "--group", tracing_group_name
,
2284 if (consumerd32_libdir
[0] != '\0') {
2290 PERROR("unknown consumer type");
2294 PERROR("Consumer execl()");
2296 /* Reaching this point, we got a failure on our execl(). */
2298 } else if (pid
> 0) {
2301 PERROR("start consumer fork");
2309 * Spawn the consumerd daemon and session daemon thread.
2311 static int start_consumerd(struct consumer_data
*consumer_data
)
2316 * Set the listen() state on the socket since there is a possible race
2317 * between the exec() of the consumer daemon and this call if place in the
2318 * consumer thread. See bug #366 for more details.
2320 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2325 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2326 if (consumer_data
->pid
!= 0) {
2327 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2331 ret
= spawn_consumerd(consumer_data
);
2333 ERR("Spawning consumerd failed");
2334 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2338 /* Setting up the consumer_data pid */
2339 consumer_data
->pid
= ret
;
2340 DBG2("Consumer pid %d", consumer_data
->pid
);
2341 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2343 DBG2("Spawning consumer control thread");
2344 ret
= spawn_consumer_thread(consumer_data
);
2346 ERR("Fatal error spawning consumer control thread");
2354 /* Cleanup already created sockets on error. */
2355 if (consumer_data
->err_sock
>= 0) {
2358 err
= close(consumer_data
->err_sock
);
2360 PERROR("close consumer data error socket");
2367 * Setup necessary data for kernel tracer action.
2369 static int init_kernel_tracer(void)
2373 /* Modprobe lttng kernel modules */
2374 ret
= modprobe_lttng_control();
2379 /* Open debugfs lttng */
2380 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2381 if (kernel_tracer_fd
< 0) {
2382 DBG("Failed to open %s", module_proc_lttng
);
2387 /* Validate kernel version */
2388 ret
= kernel_validate_version(kernel_tracer_fd
);
2393 ret
= modprobe_lttng_data();
2398 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2402 modprobe_remove_lttng_control();
2403 ret
= close(kernel_tracer_fd
);
2407 kernel_tracer_fd
= -1;
2408 return LTTNG_ERR_KERN_VERSION
;
2411 ret
= close(kernel_tracer_fd
);
2417 modprobe_remove_lttng_control();
2420 WARN("No kernel tracer available");
2421 kernel_tracer_fd
= -1;
2423 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2425 return LTTNG_ERR_KERN_NA
;
2431 * Copy consumer output from the tracing session to the domain session. The
2432 * function also applies the right modification on a per domain basis for the
2433 * trace files destination directory.
2435 * Should *NOT* be called with RCU read-side lock held.
2437 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2440 const char *dir_name
;
2441 struct consumer_output
*consumer
;
2444 assert(session
->consumer
);
2447 case LTTNG_DOMAIN_KERNEL
:
2448 DBG3("Copying tracing session consumer output in kernel session");
2450 * XXX: We should audit the session creation and what this function
2451 * does "extra" in order to avoid a destroy since this function is used
2452 * in the domain session creation (kernel and ust) only. Same for UST
2455 if (session
->kernel_session
->consumer
) {
2456 consumer_destroy_output(session
->kernel_session
->consumer
);
2458 session
->kernel_session
->consumer
=
2459 consumer_copy_output(session
->consumer
);
2460 /* Ease our life a bit for the next part */
2461 consumer
= session
->kernel_session
->consumer
;
2462 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2464 case LTTNG_DOMAIN_JUL
:
2465 case LTTNG_DOMAIN_UST
:
2466 DBG3("Copying tracing session consumer output in UST session");
2467 if (session
->ust_session
->consumer
) {
2468 consumer_destroy_output(session
->ust_session
->consumer
);
2470 session
->ust_session
->consumer
=
2471 consumer_copy_output(session
->consumer
);
2472 /* Ease our life a bit for the next part */
2473 consumer
= session
->ust_session
->consumer
;
2474 dir_name
= DEFAULT_UST_TRACE_DIR
;
2477 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2481 /* Append correct directory to subdir */
2482 strncat(consumer
->subdir
, dir_name
,
2483 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2484 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2493 * Create an UST session and add it to the session ust list.
2495 * Should *NOT* be called with RCU read-side lock held.
2497 static int create_ust_session(struct ltt_session
*session
,
2498 struct lttng_domain
*domain
)
2501 struct ltt_ust_session
*lus
= NULL
;
2505 assert(session
->consumer
);
2507 switch (domain
->type
) {
2508 case LTTNG_DOMAIN_JUL
:
2509 case LTTNG_DOMAIN_UST
:
2512 ERR("Unknown UST domain on create session %d", domain
->type
);
2513 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2517 DBG("Creating UST session");
2519 lus
= trace_ust_create_session(session
->id
);
2521 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2525 lus
->uid
= session
->uid
;
2526 lus
->gid
= session
->gid
;
2527 lus
->output_traces
= session
->output_traces
;
2528 lus
->snapshot_mode
= session
->snapshot_mode
;
2529 lus
->live_timer_interval
= session
->live_timer
;
2530 session
->ust_session
= lus
;
2532 /* Copy session output to the newly created UST session */
2533 ret
= copy_session_consumer(domain
->type
, session
);
2534 if (ret
!= LTTNG_OK
) {
2542 session
->ust_session
= NULL
;
2547 * Create a kernel tracer session then create the default channel.
2549 static int create_kernel_session(struct ltt_session
*session
)
2553 DBG("Creating kernel session");
2555 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2557 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2561 /* Code flow safety */
2562 assert(session
->kernel_session
);
2564 /* Copy session output to the newly created Kernel session */
2565 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2566 if (ret
!= LTTNG_OK
) {
2570 /* Create directory(ies) on local filesystem. */
2571 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2572 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2573 ret
= run_as_mkdir_recursive(
2574 session
->kernel_session
->consumer
->dst
.trace_path
,
2575 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2577 if (ret
!= -EEXIST
) {
2578 ERR("Trace directory creation error");
2584 session
->kernel_session
->uid
= session
->uid
;
2585 session
->kernel_session
->gid
= session
->gid
;
2586 session
->kernel_session
->output_traces
= session
->output_traces
;
2587 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2592 trace_kernel_destroy_session(session
->kernel_session
);
2593 session
->kernel_session
= NULL
;
2598 * Count number of session permitted by uid/gid.
2600 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2603 struct ltt_session
*session
;
2605 DBG("Counting number of available session for UID %d GID %d",
2607 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2609 * Only list the sessions the user can control.
2611 if (!session_access_ok(session
, uid
, gid
)) {
2620 * Process the command requested by the lttng client within the command
2621 * context structure. This function make sure that the return structure (llm)
2622 * is set and ready for transmission before returning.
2624 * Return any error encountered or 0 for success.
2626 * "sock" is only used for special-case var. len data.
2628 * Should *NOT* be called with RCU read-side lock held.
2630 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2634 int need_tracing_session
= 1;
2637 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2641 switch (cmd_ctx
->lsm
->cmd_type
) {
2642 case LTTNG_CREATE_SESSION
:
2643 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2644 case LTTNG_CREATE_SESSION_LIVE
:
2645 case LTTNG_DESTROY_SESSION
:
2646 case LTTNG_LIST_SESSIONS
:
2647 case LTTNG_LIST_DOMAINS
:
2648 case LTTNG_START_TRACE
:
2649 case LTTNG_STOP_TRACE
:
2650 case LTTNG_DATA_PENDING
:
2651 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2652 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2653 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2654 case LTTNG_SNAPSHOT_RECORD
:
2661 if (opt_no_kernel
&& need_domain
2662 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2664 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2666 ret
= LTTNG_ERR_KERN_NA
;
2671 /* Deny register consumer if we already have a spawned consumer. */
2672 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2673 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2674 if (kconsumer_data
.pid
> 0) {
2675 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2676 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2679 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2683 * Check for command that don't needs to allocate a returned payload. We do
2684 * this here so we don't have to make the call for no payload at each
2687 switch(cmd_ctx
->lsm
->cmd_type
) {
2688 case LTTNG_LIST_SESSIONS
:
2689 case LTTNG_LIST_TRACEPOINTS
:
2690 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2691 case LTTNG_LIST_DOMAINS
:
2692 case LTTNG_LIST_CHANNELS
:
2693 case LTTNG_LIST_EVENTS
:
2696 /* Setup lttng message with no payload */
2697 ret
= setup_lttng_msg(cmd_ctx
, 0);
2699 /* This label does not try to unlock the session */
2700 goto init_setup_error
;
2704 /* Commands that DO NOT need a session. */
2705 switch (cmd_ctx
->lsm
->cmd_type
) {
2706 case LTTNG_CREATE_SESSION
:
2707 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2708 case LTTNG_CREATE_SESSION_LIVE
:
2709 case LTTNG_CALIBRATE
:
2710 case LTTNG_LIST_SESSIONS
:
2711 case LTTNG_LIST_TRACEPOINTS
:
2712 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2713 need_tracing_session
= 0;
2716 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2718 * We keep the session list lock across _all_ commands
2719 * for now, because the per-session lock does not
2720 * handle teardown properly.
2722 session_lock_list();
2723 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2724 if (cmd_ctx
->session
== NULL
) {
2725 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2728 /* Acquire lock for the session */
2729 session_lock(cmd_ctx
->session
);
2739 * Check domain type for specific "pre-action".
2741 switch (cmd_ctx
->lsm
->domain
.type
) {
2742 case LTTNG_DOMAIN_KERNEL
:
2744 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2748 /* Kernel tracer check */
2749 if (kernel_tracer_fd
== -1) {
2750 /* Basically, load kernel tracer modules */
2751 ret
= init_kernel_tracer();
2757 /* Consumer is in an ERROR state. Report back to client */
2758 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
2759 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2763 /* Need a session for kernel command */
2764 if (need_tracing_session
) {
2765 if (cmd_ctx
->session
->kernel_session
== NULL
) {
2766 ret
= create_kernel_session(cmd_ctx
->session
);
2768 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2773 /* Start the kernel consumer daemon */
2774 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2775 if (kconsumer_data
.pid
== 0 &&
2776 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2777 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2778 ret
= start_consumerd(&kconsumer_data
);
2780 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2783 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
2785 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2789 * The consumer was just spawned so we need to add the socket to
2790 * the consumer output of the session if exist.
2792 ret
= consumer_create_socket(&kconsumer_data
,
2793 cmd_ctx
->session
->kernel_session
->consumer
);
2800 case LTTNG_DOMAIN_JUL
:
2801 case LTTNG_DOMAIN_UST
:
2803 if (!ust_app_supported()) {
2804 ret
= LTTNG_ERR_NO_UST
;
2807 /* Consumer is in an ERROR state. Report back to client */
2808 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
2809 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2813 if (need_tracing_session
) {
2814 /* Create UST session if none exist. */
2815 if (cmd_ctx
->session
->ust_session
== NULL
) {
2816 ret
= create_ust_session(cmd_ctx
->session
,
2817 &cmd_ctx
->lsm
->domain
);
2818 if (ret
!= LTTNG_OK
) {
2823 /* Start the UST consumer daemons */
2825 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
2826 if (consumerd64_bin
[0] != '\0' &&
2827 ustconsumer64_data
.pid
== 0 &&
2828 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2829 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2830 ret
= start_consumerd(&ustconsumer64_data
);
2832 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
2833 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
2837 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
2838 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2840 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2844 * Setup socket for consumer 64 bit. No need for atomic access
2845 * since it was set above and can ONLY be set in this thread.
2847 ret
= consumer_create_socket(&ustconsumer64_data
,
2848 cmd_ctx
->session
->ust_session
->consumer
);
2854 if (consumerd32_bin
[0] != '\0' &&
2855 ustconsumer32_data
.pid
== 0 &&
2856 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2857 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2858 ret
= start_consumerd(&ustconsumer32_data
);
2860 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
2861 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
2865 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
2866 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2868 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2872 * Setup socket for consumer 64 bit. No need for atomic access
2873 * since it was set above and can ONLY be set in this thread.
2875 ret
= consumer_create_socket(&ustconsumer32_data
,
2876 cmd_ctx
->session
->ust_session
->consumer
);
2888 /* Validate consumer daemon state when start/stop trace command */
2889 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
2890 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
2891 switch (cmd_ctx
->lsm
->domain
.type
) {
2892 case LTTNG_DOMAIN_JUL
:
2893 case LTTNG_DOMAIN_UST
:
2894 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
2895 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2899 case LTTNG_DOMAIN_KERNEL
:
2900 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
2901 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2909 * Check that the UID or GID match that of the tracing session.
2910 * The root user can interact with all sessions.
2912 if (need_tracing_session
) {
2913 if (!session_access_ok(cmd_ctx
->session
,
2914 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2915 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
2916 ret
= LTTNG_ERR_EPERM
;
2922 * Send relayd information to consumer as soon as we have a domain and a
2925 if (cmd_ctx
->session
&& need_domain
) {
2927 * Setup relayd if not done yet. If the relayd information was already
2928 * sent to the consumer, this call will gracefully return.
2930 ret
= cmd_setup_relayd(cmd_ctx
->session
);
2931 if (ret
!= LTTNG_OK
) {
2936 /* Process by command type */
2937 switch (cmd_ctx
->lsm
->cmd_type
) {
2938 case LTTNG_ADD_CONTEXT
:
2940 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2941 cmd_ctx
->lsm
->u
.context
.channel_name
,
2942 &cmd_ctx
->lsm
->u
.context
.ctx
, kernel_poll_pipe
[1]);
2945 case LTTNG_DISABLE_CHANNEL
:
2947 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2948 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2951 case LTTNG_DISABLE_EVENT
:
2953 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2954 cmd_ctx
->lsm
->u
.disable
.channel_name
,
2955 cmd_ctx
->lsm
->u
.disable
.name
);
2958 case LTTNG_DISABLE_ALL_EVENT
:
2960 DBG("Disabling all events");
2962 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2963 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2966 case LTTNG_ENABLE_CHANNEL
:
2968 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
2969 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
2972 case LTTNG_ENABLE_EVENT
:
2974 struct lttng_event_exclusion
*exclusion
= NULL
;
2975 struct lttng_filter_bytecode
*bytecode
= NULL
;
2977 /* Handle exclusion events and receive it from the client. */
2978 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
2979 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
2981 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
2982 (count
* LTTNG_SYMBOL_NAME_LEN
));
2984 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
2988 DBG("Receiving var len exclusion event list from client ...");
2989 exclusion
->count
= count
;
2990 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
2991 count
* LTTNG_SYMBOL_NAME_LEN
);
2993 DBG("Nothing recv() from client var len data... continuing");
2996 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3001 /* Handle filter and get bytecode from client. */
3002 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3003 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3005 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3006 ret
= LTTNG_ERR_FILTER_INVAL
;
3011 bytecode
= zmalloc(bytecode_len
);
3014 ret
= LTTNG_ERR_FILTER_NOMEM
;
3018 /* Receive var. len. data */
3019 DBG("Receiving var len filter's bytecode from client ...");
3020 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3022 DBG("Nothing recv() from client car len data... continuing");
3026 ret
= LTTNG_ERR_FILTER_INVAL
;
3030 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3033 ret
= LTTNG_ERR_FILTER_INVAL
;
3038 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3039 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3040 &cmd_ctx
->lsm
->u
.enable
.event
, bytecode
, exclusion
,
3041 kernel_poll_pipe
[1]);
3044 case LTTNG_ENABLE_ALL_EVENT
:
3046 DBG("Enabling all events");
3048 ret
= cmd_enable_event_all(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3049 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3050 cmd_ctx
->lsm
->u
.enable
.event
.type
, NULL
, kernel_poll_pipe
[1]);
3053 case LTTNG_LIST_TRACEPOINTS
:
3055 struct lttng_event
*events
;
3058 session_lock_list();
3059 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3060 session_unlock_list();
3061 if (nb_events
< 0) {
3062 /* Return value is a negative lttng_error_code. */
3068 * Setup lttng message with payload size set to the event list size in
3069 * bytes and then copy list into the llm payload.
3071 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
3077 /* Copy event list into message payload */
3078 memcpy(cmd_ctx
->llm
->payload
, events
,
3079 sizeof(struct lttng_event
) * nb_events
);
3086 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3088 struct lttng_event_field
*fields
;
3091 session_lock_list();
3092 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3094 session_unlock_list();
3095 if (nb_fields
< 0) {
3096 /* Return value is a negative lttng_error_code. */
3102 * Setup lttng message with payload size set to the event list size in
3103 * bytes and then copy list into the llm payload.
3105 ret
= setup_lttng_msg(cmd_ctx
,
3106 sizeof(struct lttng_event_field
) * nb_fields
);
3112 /* Copy event list into message payload */
3113 memcpy(cmd_ctx
->llm
->payload
, fields
,
3114 sizeof(struct lttng_event_field
) * nb_fields
);
3121 case LTTNG_SET_CONSUMER_URI
:
3124 struct lttng_uri
*uris
;
3126 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3127 len
= nb_uri
* sizeof(struct lttng_uri
);
3130 ret
= LTTNG_ERR_INVALID
;
3134 uris
= zmalloc(len
);
3136 ret
= LTTNG_ERR_FATAL
;
3140 /* Receive variable len data */
3141 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3142 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3144 DBG("No URIs received from client... continuing");
3146 ret
= LTTNG_ERR_SESSION_FAIL
;
3151 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3153 if (ret
!= LTTNG_OK
) {
3159 * XXX: 0 means that this URI should be applied on the session. Should
3160 * be a DOMAIN enuam.
3162 if (cmd_ctx
->lsm
->domain
.type
== 0) {
3163 /* Add the URI for the UST session if a consumer is present. */
3164 if (cmd_ctx
->session
->ust_session
&&
3165 cmd_ctx
->session
->ust_session
->consumer
) {
3166 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_UST
, cmd_ctx
->session
,
3168 } else if (cmd_ctx
->session
->kernel_session
&&
3169 cmd_ctx
->session
->kernel_session
->consumer
) {
3170 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL
,
3171 cmd_ctx
->session
, nb_uri
, uris
);
3179 case LTTNG_START_TRACE
:
3181 ret
= cmd_start_trace(cmd_ctx
->session
);
3184 case LTTNG_STOP_TRACE
:
3186 ret
= cmd_stop_trace(cmd_ctx
->session
);
3189 case LTTNG_CREATE_SESSION
:
3192 struct lttng_uri
*uris
= NULL
;
3194 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3195 len
= nb_uri
* sizeof(struct lttng_uri
);
3198 uris
= zmalloc(len
);
3200 ret
= LTTNG_ERR_FATAL
;
3204 /* Receive variable len data */
3205 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3206 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3208 DBG("No URIs received from client... continuing");
3210 ret
= LTTNG_ERR_SESSION_FAIL
;
3215 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3216 DBG("Creating session with ONE network URI is a bad call");
3217 ret
= LTTNG_ERR_SESSION_FAIL
;
3223 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3224 &cmd_ctx
->creds
, 0);
3230 case LTTNG_DESTROY_SESSION
:
3232 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3234 /* Set session to NULL so we do not unlock it after free. */
3235 cmd_ctx
->session
= NULL
;
3238 case LTTNG_LIST_DOMAINS
:
3241 struct lttng_domain
*domains
;
3243 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3245 /* Return value is a negative lttng_error_code. */
3250 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
3256 /* Copy event list into message payload */
3257 memcpy(cmd_ctx
->llm
->payload
, domains
,
3258 nb_dom
* sizeof(struct lttng_domain
));
3265 case LTTNG_LIST_CHANNELS
:
3268 struct lttng_channel
*channels
;
3270 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3271 cmd_ctx
->session
, &channels
);
3273 /* Return value is a negative lttng_error_code. */
3278 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
3284 /* Copy event list into message payload */
3285 memcpy(cmd_ctx
->llm
->payload
, channels
,
3286 nb_chan
* sizeof(struct lttng_channel
));
3293 case LTTNG_LIST_EVENTS
:
3296 struct lttng_event
*events
= NULL
;
3298 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3299 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
3301 /* Return value is a negative lttng_error_code. */
3306 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
3312 /* Copy event list into message payload */
3313 memcpy(cmd_ctx
->llm
->payload
, events
,
3314 nb_event
* sizeof(struct lttng_event
));
3321 case LTTNG_LIST_SESSIONS
:
3323 unsigned int nr_sessions
;
3325 session_lock_list();
3326 nr_sessions
= lttng_sessions_count(
3327 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3328 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3330 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
3332 session_unlock_list();
3336 /* Filled the session array */
3337 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
3338 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3339 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3341 session_unlock_list();
3346 case LTTNG_CALIBRATE
:
3348 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3349 &cmd_ctx
->lsm
->u
.calibrate
);
3352 case LTTNG_REGISTER_CONSUMER
:
3354 struct consumer_data
*cdata
;
3356 switch (cmd_ctx
->lsm
->domain
.type
) {
3357 case LTTNG_DOMAIN_KERNEL
:
3358 cdata
= &kconsumer_data
;
3361 ret
= LTTNG_ERR_UND
;
3365 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3366 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3369 case LTTNG_DATA_PENDING
:
3371 ret
= cmd_data_pending(cmd_ctx
->session
);
3374 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3376 struct lttcomm_lttng_output_id reply
;
3378 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3379 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3380 if (ret
!= LTTNG_OK
) {
3384 ret
= setup_lttng_msg(cmd_ctx
, sizeof(reply
));
3389 /* Copy output list into message payload */
3390 memcpy(cmd_ctx
->llm
->payload
, &reply
, sizeof(reply
));
3394 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3396 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3397 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3400 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3403 struct lttng_snapshot_output
*outputs
= NULL
;
3405 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3406 if (nb_output
< 0) {
3411 ret
= setup_lttng_msg(cmd_ctx
,
3412 nb_output
* sizeof(struct lttng_snapshot_output
));
3419 /* Copy output list into message payload */
3420 memcpy(cmd_ctx
->llm
->payload
, outputs
,
3421 nb_output
* sizeof(struct lttng_snapshot_output
));
3428 case LTTNG_SNAPSHOT_RECORD
:
3430 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3431 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3432 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3435 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3438 struct lttng_uri
*uris
= NULL
;
3440 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3441 len
= nb_uri
* sizeof(struct lttng_uri
);
3444 uris
= zmalloc(len
);
3446 ret
= LTTNG_ERR_FATAL
;
3450 /* Receive variable len data */
3451 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3452 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3454 DBG("No URIs received from client... continuing");
3456 ret
= LTTNG_ERR_SESSION_FAIL
;
3461 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3462 DBG("Creating session with ONE network URI is a bad call");
3463 ret
= LTTNG_ERR_SESSION_FAIL
;
3469 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
3470 nb_uri
, &cmd_ctx
->creds
);
3474 case LTTNG_CREATE_SESSION_LIVE
:
3477 struct lttng_uri
*uris
= NULL
;
3479 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3480 len
= nb_uri
* sizeof(struct lttng_uri
);
3483 uris
= zmalloc(len
);
3485 ret
= LTTNG_ERR_FATAL
;
3489 /* Receive variable len data */
3490 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3491 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3493 DBG("No URIs received from client... continuing");
3495 ret
= LTTNG_ERR_SESSION_FAIL
;
3500 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3501 DBG("Creating session with ONE network URI is a bad call");
3502 ret
= LTTNG_ERR_SESSION_FAIL
;
3508 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
3509 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
3514 ret
= LTTNG_ERR_UND
;
3519 if (cmd_ctx
->llm
== NULL
) {
3520 DBG("Missing llm structure. Allocating one.");
3521 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
3525 /* Set return code */
3526 cmd_ctx
->llm
->ret_code
= ret
;
3528 if (cmd_ctx
->session
) {
3529 session_unlock(cmd_ctx
->session
);
3531 if (need_tracing_session
) {
3532 session_unlock_list();
3539 * Thread managing health check socket.
3541 static void *thread_manage_health(void *data
)
3543 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
3544 uint32_t revents
, nb_fd
;
3545 struct lttng_poll_event events
;
3546 struct health_comm_msg msg
;
3547 struct health_comm_reply reply
;
3549 DBG("[thread] Manage health check started");
3551 rcu_register_thread();
3553 /* We might hit an error path before this is created. */
3554 lttng_poll_init(&events
);
3556 /* Create unix socket */
3557 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
3559 ERR("Unable to create health check Unix socket");
3565 /* lttng health client socket path permissions */
3566 ret
= chown(health_unix_sock_path
, 0,
3567 utils_get_group_id(tracing_group_name
));
3569 ERR("Unable to set group on %s", health_unix_sock_path
);
3575 ret
= chmod(health_unix_sock_path
,
3576 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3578 ERR("Unable to set permissions on %s", health_unix_sock_path
);
3586 * Set the CLOEXEC flag. Return code is useless because either way, the
3589 (void) utils_set_fd_cloexec(sock
);
3591 ret
= lttcomm_listen_unix_sock(sock
);
3597 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3598 * more will be added to this poll set.
3600 ret
= sessiond_set_thread_pollset(&events
, 2);
3605 /* Add the application registration socket */
3606 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
3611 lttng_sessiond_notify_ready();
3614 DBG("Health check ready");
3616 /* Inifinite blocking call, waiting for transmission */
3618 ret
= lttng_poll_wait(&events
, -1);
3621 * Restart interrupted system call.
3623 if (errno
== EINTR
) {
3631 for (i
= 0; i
< nb_fd
; i
++) {
3632 /* Fetch once the poll data */
3633 revents
= LTTNG_POLL_GETEV(&events
, i
);
3634 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3636 /* Thread quit pipe has been closed. Killing thread. */
3637 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3643 /* Event on the registration socket */
3644 if (pollfd
== sock
) {
3645 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3646 ERR("Health socket poll error");
3652 new_sock
= lttcomm_accept_unix_sock(sock
);
3658 * Set the CLOEXEC flag. Return code is useless because either way, the
3661 (void) utils_set_fd_cloexec(new_sock
);
3663 DBG("Receiving data from client for health...");
3664 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
3666 DBG("Nothing recv() from client... continuing");
3667 ret
= close(new_sock
);
3675 rcu_thread_online();
3677 memset(&reply
, 0, sizeof(reply
));
3678 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
3680 * health_check_state returns 0 if health is
3683 if (!health_check_state(health_sessiond
, i
)) {
3684 reply
.ret_code
|= 1ULL << i
;
3688 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
3690 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
3692 ERR("Failed to send health data back to client");
3695 /* End of transmission */
3696 ret
= close(new_sock
);
3706 ERR("Health error occurred in %s", __func__
);
3708 DBG("Health check thread dying");
3709 unlink(health_unix_sock_path
);
3717 lttng_poll_clean(&events
);
3719 rcu_unregister_thread();
3724 * This thread manage all clients request using the unix client socket for
3727 static void *thread_manage_clients(void *data
)
3729 int sock
= -1, ret
, i
, pollfd
, err
= -1;
3731 uint32_t revents
, nb_fd
;
3732 struct command_ctx
*cmd_ctx
= NULL
;
3733 struct lttng_poll_event events
;
3735 DBG("[thread] Manage client started");
3737 rcu_register_thread();
3739 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
3741 health_code_update();
3743 ret
= lttcomm_listen_unix_sock(client_sock
);
3749 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3750 * more will be added to this poll set.
3752 ret
= sessiond_set_thread_pollset(&events
, 2);
3754 goto error_create_poll
;
3757 /* Add the application registration socket */
3758 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
3763 lttng_sessiond_notify_ready();
3765 /* This testpoint is after we signal readiness to the parent. */
3766 if (testpoint(sessiond_thread_manage_clients
)) {
3770 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
3774 health_code_update();
3777 DBG("Accepting client command ...");
3779 /* Inifinite blocking call, waiting for transmission */
3781 health_poll_entry();
3782 ret
= lttng_poll_wait(&events
, -1);
3786 * Restart interrupted system call.
3788 if (errno
== EINTR
) {
3796 for (i
= 0; i
< nb_fd
; i
++) {
3797 /* Fetch once the poll data */
3798 revents
= LTTNG_POLL_GETEV(&events
, i
);
3799 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3801 health_code_update();
3803 /* Thread quit pipe has been closed. Killing thread. */
3804 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3810 /* Event on the registration socket */
3811 if (pollfd
== client_sock
) {
3812 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3813 ERR("Client socket poll error");
3819 DBG("Wait for client response");
3821 health_code_update();
3823 sock
= lttcomm_accept_unix_sock(client_sock
);
3829 * Set the CLOEXEC flag. Return code is useless because either way, the
3832 (void) utils_set_fd_cloexec(sock
);
3834 /* Set socket option for credentials retrieval */
3835 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
3840 /* Allocate context command to process the client request */
3841 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
3842 if (cmd_ctx
== NULL
) {
3843 PERROR("zmalloc cmd_ctx");
3847 /* Allocate data buffer for reception */
3848 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
3849 if (cmd_ctx
->lsm
== NULL
) {
3850 PERROR("zmalloc cmd_ctx->lsm");
3854 cmd_ctx
->llm
= NULL
;
3855 cmd_ctx
->session
= NULL
;
3857 health_code_update();
3860 * Data is received from the lttng client. The struct
3861 * lttcomm_session_msg (lsm) contains the command and data request of
3864 DBG("Receiving data from client ...");
3865 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
3866 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
3868 DBG("Nothing recv() from client... continuing");
3874 clean_command_ctx(&cmd_ctx
);
3878 health_code_update();
3880 // TODO: Validate cmd_ctx including sanity check for
3881 // security purpose.
3883 rcu_thread_online();
3885 * This function dispatch the work to the kernel or userspace tracer
3886 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3887 * informations for the client. The command context struct contains
3888 * everything this function may needs.
3890 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
3891 rcu_thread_offline();
3899 * TODO: Inform client somehow of the fatal error. At
3900 * this point, ret < 0 means that a zmalloc failed
3901 * (ENOMEM). Error detected but still accept
3902 * command, unless a socket error has been
3905 clean_command_ctx(&cmd_ctx
);
3909 health_code_update();
3911 DBG("Sending response (size: %d, retcode: %s)",
3912 cmd_ctx
->lttng_msg_size
,
3913 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
3914 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
3916 ERR("Failed to send data back to client");
3919 /* End of transmission */
3926 clean_command_ctx(&cmd_ctx
);
3928 health_code_update();
3940 lttng_poll_clean(&events
);
3941 clean_command_ctx(&cmd_ctx
);
3945 unlink(client_unix_sock_path
);
3946 if (client_sock
>= 0) {
3947 ret
= close(client_sock
);
3955 ERR("Health error occurred in %s", __func__
);
3958 health_unregister(health_sessiond
);
3960 DBG("Client thread dying");
3962 rcu_unregister_thread();
3968 * usage function on stderr
3970 static void usage(void)
3972 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
3973 fprintf(stderr
, " -h, --help Display this usage.\n");
3974 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
3975 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
3976 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
3977 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
3978 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
3979 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
3980 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
3981 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
3982 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
3983 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
3984 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
3985 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
3986 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
3987 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
3988 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
3989 fprintf(stderr
, " -V, --version Show version number.\n");
3990 fprintf(stderr
, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n");
3991 fprintf(stderr
, " -q, --quiet No output at all.\n");
3992 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3993 fprintf(stderr
, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
3994 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
3995 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
3996 fprintf(stderr
, " --jul-tcp-port JUL application registration TCP port\n");
4000 * daemon argument parsing
4002 static int parse_args(int argc
, char **argv
)
4006 static struct option long_options
[] = {
4007 { "client-sock", 1, 0, 'c' },
4008 { "apps-sock", 1, 0, 'a' },
4009 { "kconsumerd-cmd-sock", 1, 0, 'C' },
4010 { "kconsumerd-err-sock", 1, 0, 'E' },
4011 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
4012 { "ustconsumerd32-err-sock", 1, 0, 'H' },
4013 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
4014 { "ustconsumerd64-err-sock", 1, 0, 'F' },
4015 { "consumerd32-path", 1, 0, 'u' },
4016 { "consumerd32-libdir", 1, 0, 'U' },
4017 { "consumerd64-path", 1, 0, 't' },
4018 { "consumerd64-libdir", 1, 0, 'T' },
4019 { "daemonize", 0, 0, 'd' },
4020 { "sig-parent", 0, 0, 'S' },
4021 { "help", 0, 0, 'h' },
4022 { "group", 1, 0, 'g' },
4023 { "version", 0, 0, 'V' },
4024 { "quiet", 0, 0, 'q' },
4025 { "verbose", 0, 0, 'v' },
4026 { "verbose-consumer", 0, 0, 'Z' },
4027 { "no-kernel", 0, 0, 'N' },
4028 { "pidfile", 1, 0, 'p' },
4029 { "jul-tcp-port", 1, 0, 'J' },
4030 { "background", 0, 0, 'b' },
4035 int option_index
= 0;
4036 c
= getopt_long(argc
, argv
, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t:p:J:b",
4037 long_options
, &option_index
);
4044 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
4046 fprintf(stderr
, " with arg %s\n", optarg
);
4050 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4053 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4062 tracing_group_name
= optarg
;
4068 fprintf(stdout
, "%s\n", VERSION
);
4074 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4077 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4080 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4083 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4086 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4089 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4095 lttng_opt_quiet
= 1;
4098 /* Verbose level can increase using multiple -v */
4099 lttng_opt_verbose
+= 1;
4102 opt_verbose_consumer
+= 1;
4105 consumerd32_bin
= optarg
;
4108 consumerd32_libdir
= optarg
;
4111 consumerd64_bin
= optarg
;
4114 consumerd64_libdir
= optarg
;
4117 opt_pidfile
= optarg
;
4119 case 'J': /* JUL TCP port. */
4124 v
= strtoul(optarg
, NULL
, 0);
4125 if (errno
!= 0 || !isdigit(optarg
[0])) {
4126 ERR("Wrong value in --jul-tcp-port parameter: %s", optarg
);
4129 if (v
== 0 || v
>= 65535) {
4130 ERR("Port overflow in --jul-tcp-port parameter: %s", optarg
);
4133 jul_tcp_port
= (uint32_t) v
;
4134 DBG3("JUL TCP port set to non default: %u", jul_tcp_port
);
4138 /* Unknown option or other error.
4139 * Error is printed by getopt, just return */
4148 * Creates the two needed socket by the daemon.
4149 * apps_sock - The communication socket for all UST apps.
4150 * client_sock - The communication of the cli tool (lttng).
4152 static int init_daemon_socket(void)
4157 old_umask
= umask(0);
4159 /* Create client tool unix socket */
4160 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
4161 if (client_sock
< 0) {
4162 ERR("Create unix sock failed: %s", client_unix_sock_path
);
4167 /* Set the cloexec flag */
4168 ret
= utils_set_fd_cloexec(client_sock
);
4170 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4171 "Continuing but note that the consumer daemon will have a "
4172 "reference to this socket on exec()", client_sock
);
4175 /* File permission MUST be 660 */
4176 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4178 ERR("Set file permissions failed: %s", client_unix_sock_path
);
4183 /* Create the application unix socket */
4184 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
4185 if (apps_sock
< 0) {
4186 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
4191 /* Set the cloexec flag */
4192 ret
= utils_set_fd_cloexec(apps_sock
);
4194 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4195 "Continuing but note that the consumer daemon will have a "
4196 "reference to this socket on exec()", apps_sock
);
4199 /* File permission MUST be 666 */
4200 ret
= chmod(apps_unix_sock_path
,
4201 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
4203 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
4208 DBG3("Session daemon client socket %d and application socket %d created",
4209 client_sock
, apps_sock
);
4217 * Check if the global socket is available, and if a daemon is answering at the
4218 * other side. If yes, error is returned.
4220 static int check_existing_daemon(void)
4222 /* Is there anybody out there ? */
4223 if (lttng_session_daemon_alive()) {
4231 * Set the tracing group gid onto the client socket.
4233 * Race window between mkdir and chown is OK because we are going from more
4234 * permissive (root.root) to less permissive (root.tracing).
4236 static int set_permissions(char *rundir
)
4241 gid
= utils_get_group_id(tracing_group_name
);
4243 /* Set lttng run dir */
4244 ret
= chown(rundir
, 0, gid
);
4246 ERR("Unable to set group on %s", rundir
);
4251 * Ensure all applications and tracing group can search the run
4252 * dir. Allow everyone to read the directory, since it does not
4253 * buy us anything to hide its content.
4255 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
4257 ERR("Unable to set permissions on %s", rundir
);
4261 /* lttng client socket path */
4262 ret
= chown(client_unix_sock_path
, 0, gid
);
4264 ERR("Unable to set group on %s", client_unix_sock_path
);
4268 /* kconsumer error socket path */
4269 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
4271 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
4275 /* 64-bit ustconsumer error socket path */
4276 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
4278 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
4282 /* 32-bit ustconsumer compat32 error socket path */
4283 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
4285 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
4289 DBG("All permissions are set");
4295 * Create the lttng run directory needed for all global sockets and pipe.
4297 static int create_lttng_rundir(const char *rundir
)
4301 DBG3("Creating LTTng run directory: %s", rundir
);
4303 ret
= mkdir(rundir
, S_IRWXU
);
4305 if (errno
!= EEXIST
) {
4306 ERR("Unable to create %s", rundir
);
4318 * Setup sockets and directory needed by the kconsumerd communication with the
4321 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
4325 char path
[PATH_MAX
];
4327 switch (consumer_data
->type
) {
4328 case LTTNG_CONSUMER_KERNEL
:
4329 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
4331 case LTTNG_CONSUMER64_UST
:
4332 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
4334 case LTTNG_CONSUMER32_UST
:
4335 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
4338 ERR("Consumer type unknown");
4343 DBG2("Creating consumer directory: %s", path
);
4345 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
4347 if (errno
!= EEXIST
) {
4349 ERR("Failed to create %s", path
);
4355 ret
= chown(path
, 0, utils_get_group_id(tracing_group_name
));
4357 ERR("Unable to set group on %s", path
);
4363 /* Create the kconsumerd error unix socket */
4364 consumer_data
->err_sock
=
4365 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
4366 if (consumer_data
->err_sock
< 0) {
4367 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
4373 * Set the CLOEXEC flag. Return code is useless because either way, the
4376 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
4378 PERROR("utils_set_fd_cloexec");
4379 /* continue anyway */
4382 /* File permission MUST be 660 */
4383 ret
= chmod(consumer_data
->err_unix_sock_path
,
4384 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4386 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
4396 * Signal handler for the daemon
4398 * Simply stop all worker threads, leaving main() return gracefully after
4399 * joining all threads and calling cleanup().
4401 static void sighandler(int sig
)
4405 DBG("SIGPIPE caught");
4408 DBG("SIGINT caught");
4412 DBG("SIGTERM caught");
4416 CMM_STORE_SHARED(recv_child_signal
, 1);
4424 * Setup signal handler for :
4425 * SIGINT, SIGTERM, SIGPIPE
4427 static int set_signal_handler(void)
4430 struct sigaction sa
;
4433 if ((ret
= sigemptyset(&sigset
)) < 0) {
4434 PERROR("sigemptyset");
4438 sa
.sa_handler
= sighandler
;
4439 sa
.sa_mask
= sigset
;
4441 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
4442 PERROR("sigaction");
4446 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
4447 PERROR("sigaction");
4451 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
4452 PERROR("sigaction");
4456 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
4457 PERROR("sigaction");
4461 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
4467 * Set open files limit to unlimited. This daemon can open a large number of
4468 * file descriptors in order to consumer multiple kernel traces.
4470 static void set_ulimit(void)
4475 /* The kernel does not allowed an infinite limit for open files */
4476 lim
.rlim_cur
= 65535;
4477 lim
.rlim_max
= 65535;
4479 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
4481 PERROR("failed to set open files limit");
4486 * Write pidfile using the rundir and opt_pidfile.
4488 static void write_pidfile(void)
4491 char pidfile_path
[PATH_MAX
];
4496 strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
));
4498 /* Build pidfile path from rundir and opt_pidfile. */
4499 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
4500 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
4502 PERROR("snprintf pidfile path");
4508 * Create pid file in rundir. Return value is of no importance. The
4509 * execution will continue even though we are not able to write the file.
4511 (void) utils_create_pid_file(getpid(), pidfile_path
);
4518 * Write JUL TCP port using the rundir.
4520 static void write_julport(void)
4523 char path
[PATH_MAX
];
4527 ret
= snprintf(path
, sizeof(path
), "%s/"
4528 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE
, rundir
);
4530 PERROR("snprintf julport path");
4535 * Create TCP JUL port file in rundir. Return value is of no importance.
4536 * The execution will continue even though we are not able to write the
4539 (void) utils_create_pid_file(jul_tcp_port
, path
);
4548 int main(int argc
, char **argv
)
4552 const char *home_path
, *env_app_timeout
;
4554 init_kernel_workarounds();
4556 rcu_register_thread();
4558 if ((ret
= set_signal_handler()) < 0) {
4562 setup_consumerd_path();
4564 page_size
= sysconf(_SC_PAGESIZE
);
4565 if (page_size
< 0) {
4566 PERROR("sysconf _SC_PAGESIZE");
4567 page_size
= LONG_MAX
;
4568 WARN("Fallback page size to %ld", page_size
);
4571 /* Parse arguments */
4573 if ((ret
= parse_args(argc
, argv
)) < 0) {
4578 if (opt_daemon
|| opt_background
) {
4581 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
4588 * We are in the child. Make sure all other file descriptors are
4589 * closed, in case we are called with more opened file descriptors than
4590 * the standard ones.
4592 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
4597 /* Create thread quit pipe */
4598 if ((ret
= init_thread_quit_pipe()) < 0) {
4602 /* Check if daemon is UID = 0 */
4603 is_root
= !getuid();
4606 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
4608 /* Create global run dir with root access */
4609 ret
= create_lttng_rundir(rundir
);
4614 if (strlen(apps_unix_sock_path
) == 0) {
4615 snprintf(apps_unix_sock_path
, PATH_MAX
,
4616 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
4619 if (strlen(client_unix_sock_path
) == 0) {
4620 snprintf(client_unix_sock_path
, PATH_MAX
,
4621 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
4624 /* Set global SHM for ust */
4625 if (strlen(wait_shm_path
) == 0) {
4626 snprintf(wait_shm_path
, PATH_MAX
,
4627 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
4630 if (strlen(health_unix_sock_path
) == 0) {
4631 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4632 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
4635 /* Setup kernel consumerd path */
4636 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
4637 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
4638 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
4639 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
4641 DBG2("Kernel consumer err path: %s",
4642 kconsumer_data
.err_unix_sock_path
);
4643 DBG2("Kernel consumer cmd path: %s",
4644 kconsumer_data
.cmd_unix_sock_path
);
4646 home_path
= utils_get_home_dir();
4647 if (home_path
== NULL
) {
4648 /* TODO: Add --socket PATH option */
4649 ERR("Can't get HOME directory for sockets creation.");
4655 * Create rundir from home path. This will create something like
4658 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
4664 ret
= create_lttng_rundir(rundir
);
4669 if (strlen(apps_unix_sock_path
) == 0) {
4670 snprintf(apps_unix_sock_path
, PATH_MAX
,
4671 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
4674 /* Set the cli tool unix socket path */
4675 if (strlen(client_unix_sock_path
) == 0) {
4676 snprintf(client_unix_sock_path
, PATH_MAX
,
4677 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
4680 /* Set global SHM for ust */
4681 if (strlen(wait_shm_path
) == 0) {
4682 snprintf(wait_shm_path
, PATH_MAX
,
4683 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
4686 /* Set health check Unix path */
4687 if (strlen(health_unix_sock_path
) == 0) {
4688 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4689 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
4693 /* Set consumer initial state */
4694 kernel_consumerd_state
= CONSUMER_STOPPED
;
4695 ust_consumerd_state
= CONSUMER_STOPPED
;
4697 DBG("Client socket path %s", client_unix_sock_path
);
4698 DBG("Application socket path %s", apps_unix_sock_path
);
4699 DBG("Application wait path %s", wait_shm_path
);
4700 DBG("LTTng run directory path: %s", rundir
);
4702 /* 32 bits consumerd path setup */
4703 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
4704 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
4705 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
4706 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
4708 DBG2("UST consumer 32 bits err path: %s",
4709 ustconsumer32_data
.err_unix_sock_path
);
4710 DBG2("UST consumer 32 bits cmd path: %s",
4711 ustconsumer32_data
.cmd_unix_sock_path
);
4713 /* 64 bits consumerd path setup */
4714 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
4715 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
4716 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
4717 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
4719 DBG2("UST consumer 64 bits err path: %s",
4720 ustconsumer64_data
.err_unix_sock_path
);
4721 DBG2("UST consumer 64 bits cmd path: %s",
4722 ustconsumer64_data
.cmd_unix_sock_path
);
4725 * See if daemon already exist.
4727 if ((ret
= check_existing_daemon()) < 0) {
4728 ERR("Already running daemon.\n");
4730 * We do not goto exit because we must not cleanup()
4731 * because a daemon is already running.
4737 * Init UST app hash table. Alloc hash table before this point since
4738 * cleanup() can get called after that point.
4742 /* Initialize JUL domain subsystem. */
4743 if ((ret
= jul_init()) < 0) {
4744 /* ENOMEM at this point. */
4748 /* After this point, we can safely call cleanup() with "goto exit" */
4751 * These actions must be executed as root. We do that *after* setting up
4752 * the sockets path because we MUST make the check for another daemon using
4753 * those paths *before* trying to set the kernel consumer sockets and init
4757 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
4762 /* Setup kernel tracer */
4763 if (!opt_no_kernel
) {
4764 init_kernel_tracer();
4767 /* Set ulimit for open files */
4770 /* init lttng_fd tracking must be done after set_ulimit. */
4773 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
4778 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
4783 /* Setup the needed unix socket */
4784 if ((ret
= init_daemon_socket()) < 0) {
4788 /* Set credentials to socket */
4789 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
4793 /* Get parent pid if -S, --sig-parent is specified. */
4794 if (opt_sig_parent
) {
4798 /* Setup the kernel pipe for waking up the kernel thread */
4799 if (is_root
&& !opt_no_kernel
) {
4800 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
4805 /* Setup the thread ht_cleanup communication pipe. */
4806 if (utils_create_pipe_cloexec(ht_cleanup_pipe
) < 0) {
4810 /* Setup the thread apps communication pipe. */
4811 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
4815 /* Setup the thread apps notify communication pipe. */
4816 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
) < 0) {
4820 /* Initialize global buffer per UID and PID registry. */
4821 buffer_reg_init_uid_registry();
4822 buffer_reg_init_pid_registry();
4824 /* Init UST command queue. */
4825 cds_wfq_init(&ust_cmd_queue
.queue
);
4828 * Get session list pointer. This pointer MUST NOT be free(). This list is
4829 * statically declared in session.c
4831 session_list_ptr
= session_get_list();
4833 /* Set up max poll set size */
4834 lttng_poll_set_max_size();
4838 /* Check for the application socket timeout env variable. */
4839 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
4840 if (env_app_timeout
) {
4841 app_socket_timeout
= atoi(env_app_timeout
);
4843 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
4849 /* Initialize communication library */
4851 /* This is to get the TCP timeout value. */
4852 lttcomm_inet_init();
4855 * Initialize the health check subsystem. This call should set the
4856 * appropriate time values.
4858 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
4859 if (!health_sessiond
) {
4860 PERROR("health_app_create error");
4861 goto exit_health_sessiond_cleanup
;
4864 /* Create thread to clean up RCU hash tables */
4865 ret
= pthread_create(&ht_cleanup_thread
, NULL
,
4866 thread_ht_cleanup
, (void *) NULL
);
4868 PERROR("pthread_create ht_cleanup");
4869 goto exit_ht_cleanup
;
4872 /* Create health-check thread */
4873 ret
= pthread_create(&health_thread
, NULL
,
4874 thread_manage_health
, (void *) NULL
);
4876 PERROR("pthread_create health");
4880 /* Create thread to manage the client socket */
4881 ret
= pthread_create(&client_thread
, NULL
,
4882 thread_manage_clients
, (void *) NULL
);
4884 PERROR("pthread_create clients");
4888 /* Create thread to dispatch registration */
4889 ret
= pthread_create(&dispatch_thread
, NULL
,
4890 thread_dispatch_ust_registration
, (void *) NULL
);
4892 PERROR("pthread_create dispatch");
4896 /* Create thread to manage application registration. */
4897 ret
= pthread_create(®_apps_thread
, NULL
,
4898 thread_registration_apps
, (void *) NULL
);
4900 PERROR("pthread_create registration");
4904 /* Create thread to manage application socket */
4905 ret
= pthread_create(&apps_thread
, NULL
,
4906 thread_manage_apps
, (void *) NULL
);
4908 PERROR("pthread_create apps");
4912 /* Create thread to manage application notify socket */
4913 ret
= pthread_create(&apps_notify_thread
, NULL
,
4914 ust_thread_manage_notify
, (void *) NULL
);
4916 PERROR("pthread_create notify");
4917 goto exit_apps_notify
;
4920 /* Create JUL registration thread. */
4921 ret
= pthread_create(&jul_reg_thread
, NULL
,
4922 jul_thread_manage_registration
, (void *) NULL
);
4924 PERROR("pthread_create JUL");
4928 /* Don't start this thread if kernel tracing is not requested nor root */
4929 if (is_root
&& !opt_no_kernel
) {
4930 /* Create kernel thread to manage kernel event */
4931 ret
= pthread_create(&kernel_thread
, NULL
,
4932 thread_manage_kernel
, (void *) NULL
);
4934 PERROR("pthread_create kernel");
4938 ret
= pthread_join(kernel_thread
, &status
);
4940 PERROR("pthread_join");
4941 goto error
; /* join error, exit without cleanup */
4946 ret
= pthread_join(jul_reg_thread
, &status
);
4948 PERROR("pthread_join JUL");
4949 goto error
; /* join error, exit without cleanup */
4953 ret
= pthread_join(apps_notify_thread
, &status
);
4955 PERROR("pthread_join apps notify");
4956 goto error
; /* join error, exit without cleanup */
4960 ret
= pthread_join(apps_thread
, &status
);
4962 PERROR("pthread_join apps");
4963 goto error
; /* join error, exit without cleanup */
4968 ret
= pthread_join(reg_apps_thread
, &status
);
4970 PERROR("pthread_join");
4971 goto error
; /* join error, exit without cleanup */
4975 ret
= pthread_join(dispatch_thread
, &status
);
4977 PERROR("pthread_join");
4978 goto error
; /* join error, exit without cleanup */
4982 ret
= pthread_join(client_thread
, &status
);
4984 PERROR("pthread_join");
4985 goto error
; /* join error, exit without cleanup */
4988 ret
= join_consumer_thread(&kconsumer_data
);
4990 PERROR("join_consumer");
4991 goto error
; /* join error, exit without cleanup */
4994 ret
= join_consumer_thread(&ustconsumer32_data
);
4996 PERROR("join_consumer ust32");
4997 goto error
; /* join error, exit without cleanup */
5000 ret
= join_consumer_thread(&ustconsumer64_data
);
5002 PERROR("join_consumer ust64");
5003 goto error
; /* join error, exit without cleanup */
5007 ret
= pthread_join(health_thread
, &status
);
5009 PERROR("pthread_join health thread");
5010 goto error
; /* join error, exit without cleanup */
5014 ret
= pthread_join(ht_cleanup_thread
, &status
);
5016 PERROR("pthread_join ht cleanup thread");
5017 goto error
; /* join error, exit without cleanup */
5020 health_app_destroy(health_sessiond
);
5021 exit_health_sessiond_cleanup
:
5024 * cleanup() is called when no other thread is running.
5026 rcu_thread_online();
5028 rcu_thread_offline();
5029 rcu_unregister_thread();