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.
30 #include <sys/mount.h>
31 #include <sys/resource.h>
32 #include <sys/socket.h>
34 #include <sys/types.h>
36 #include <urcu/uatomic.h>
40 #include <common/common.h>
41 #include <common/compat/socket.h>
42 #include <common/defaults.h>
43 #include <common/kernel-consumer/kernel-consumer.h>
44 #include <common/futex.h>
45 #include <common/relayd/relayd.h>
46 #include <common/utils.h>
48 #include "lttng-sessiond.h"
49 #include "buffer-registry.h"
56 #include "kernel-consumer.h"
60 #include "ust-consumer.h"
63 #include "health-sessiond.h"
64 #include "testpoint.h"
65 #include "ust-thread.h"
66 #include "jul-thread.h"
68 #define CONSUMERD_FILE "lttng-consumerd"
71 static const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
72 static const char *opt_pidfile
;
73 static int opt_sig_parent
;
74 static int opt_verbose_consumer
;
75 static int opt_daemon
;
76 static int opt_no_kernel
;
77 static int is_root
; /* Set to 1 if the daemon is running as root */
78 static pid_t ppid
; /* Parent PID for --sig-parent option */
82 * Consumer daemon specific control data. Every value not initialized here is
83 * set to 0 by the static definition.
85 static struct consumer_data kconsumer_data
= {
86 .type
= LTTNG_CONSUMER_KERNEL
,
87 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
88 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
91 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
92 .lock
= PTHREAD_MUTEX_INITIALIZER
,
93 .cond
= PTHREAD_COND_INITIALIZER
,
94 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
96 static struct consumer_data ustconsumer64_data
= {
97 .type
= LTTNG_CONSUMER64_UST
,
98 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
99 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
102 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
103 .lock
= PTHREAD_MUTEX_INITIALIZER
,
104 .cond
= PTHREAD_COND_INITIALIZER
,
105 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
107 static struct consumer_data ustconsumer32_data
= {
108 .type
= LTTNG_CONSUMER32_UST
,
109 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
110 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
113 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
114 .lock
= PTHREAD_MUTEX_INITIALIZER
,
115 .cond
= PTHREAD_COND_INITIALIZER
,
116 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
119 /* Shared between threads */
120 static int dispatch_thread_exit
;
122 /* Global application Unix socket path */
123 static char apps_unix_sock_path
[PATH_MAX
];
124 /* Global client Unix socket path */
125 static char client_unix_sock_path
[PATH_MAX
];
126 /* global wait shm path for UST */
127 static char wait_shm_path
[PATH_MAX
];
128 /* Global health check unix path */
129 static char health_unix_sock_path
[PATH_MAX
];
131 /* Sockets and FDs */
132 static int client_sock
= -1;
133 static int apps_sock
= -1;
134 int kernel_tracer_fd
= -1;
135 static int kernel_poll_pipe
[2] = { -1, -1 };
138 * Quit pipe for all threads. This permits a single cancellation point
139 * for all threads when receiving an event on the pipe.
141 static int thread_quit_pipe
[2] = { -1, -1 };
144 * This pipe is used to inform the thread managing application communication
145 * that a command is queued and ready to be processed.
147 static int apps_cmd_pipe
[2] = { -1, -1 };
149 int apps_cmd_notify_pipe
[2] = { -1, -1 };
151 /* Pthread, Mutexes and Semaphores */
152 static pthread_t apps_thread
;
153 static pthread_t apps_notify_thread
;
154 static pthread_t reg_apps_thread
;
155 static pthread_t client_thread
;
156 static pthread_t kernel_thread
;
157 static pthread_t dispatch_thread
;
158 static pthread_t health_thread
;
159 static pthread_t ht_cleanup_thread
;
160 static pthread_t jul_reg_thread
;
163 * UST registration command queue. This queue is tied with a futex and uses a N
164 * wakers / 1 waiter implemented and detailed in futex.c/.h
166 * The thread_manage_apps and thread_dispatch_ust_registration interact with
167 * this queue and the wait/wake scheme.
169 static struct ust_cmd_queue ust_cmd_queue
;
172 * Pointer initialized before thread creation.
174 * This points to the tracing session list containing the session count and a
175 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
176 * MUST NOT be taken if you call a public function in session.c.
178 * The lock is nested inside the structure: session_list_ptr->lock. Please use
179 * session_lock_list and session_unlock_list for lock acquisition.
181 static struct ltt_session_list
*session_list_ptr
;
183 int ust_consumerd64_fd
= -1;
184 int ust_consumerd32_fd
= -1;
186 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
187 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
188 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
189 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
191 static const char *module_proc_lttng
= "/proc/lttng";
194 * Consumer daemon state which is changed when spawning it, killing it or in
195 * case of a fatal error.
197 enum consumerd_state
{
198 CONSUMER_STARTED
= 1,
199 CONSUMER_STOPPED
= 2,
204 * This consumer daemon state is used to validate if a client command will be
205 * able to reach the consumer. If not, the client is informed. For instance,
206 * doing a "lttng start" when the consumer state is set to ERROR will return an
207 * error to the client.
209 * The following example shows a possible race condition of this scheme:
211 * consumer thread error happens
213 * client cmd checks state -> still OK
214 * consumer thread exit, sets error
215 * client cmd try to talk to consumer
218 * However, since the consumer is a different daemon, we have no way of making
219 * sure the command will reach it safely even with this state flag. This is why
220 * we consider that up to the state validation during command processing, the
221 * command is safe. After that, we can not guarantee the correctness of the
222 * client request vis-a-vis the consumer.
224 static enum consumerd_state ust_consumerd_state
;
225 static enum consumerd_state kernel_consumerd_state
;
228 * Socket timeout for receiving and sending in seconds.
230 static int app_socket_timeout
;
232 /* Set in main() with the current page size. */
235 /* Application health monitoring */
236 struct health_app
*health_sessiond
;
238 /* JUL TCP port for registration. Used by the JUL thread. */
239 unsigned int jul_tcp_port
= DEFAULT_JUL_TCP_PORT
;
242 void setup_consumerd_path(void)
244 const char *bin
, *libdir
;
247 * Allow INSTALL_BIN_PATH to be used as a target path for the
248 * native architecture size consumer if CONFIG_CONSUMER*_PATH
249 * has not been defined.
251 #if (CAA_BITS_PER_LONG == 32)
252 if (!consumerd32_bin
[0]) {
253 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
255 if (!consumerd32_libdir
[0]) {
256 consumerd32_libdir
= INSTALL_LIB_PATH
;
258 #elif (CAA_BITS_PER_LONG == 64)
259 if (!consumerd64_bin
[0]) {
260 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
262 if (!consumerd64_libdir
[0]) {
263 consumerd64_libdir
= INSTALL_LIB_PATH
;
266 #error "Unknown bitness"
270 * runtime env. var. overrides the build default.
272 bin
= getenv("LTTNG_CONSUMERD32_BIN");
274 consumerd32_bin
= bin
;
276 bin
= getenv("LTTNG_CONSUMERD64_BIN");
278 consumerd64_bin
= bin
;
280 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
282 consumerd32_libdir
= libdir
;
284 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
286 consumerd64_libdir
= libdir
;
291 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
293 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
299 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
305 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
317 * Check if the thread quit pipe was triggered.
319 * Return 1 if it was triggered else 0;
321 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
323 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
331 * Init thread quit pipe.
333 * Return -1 on error or 0 if all pipes are created.
335 static int init_thread_quit_pipe(void)
339 ret
= pipe(thread_quit_pipe
);
341 PERROR("thread quit pipe");
345 for (i
= 0; i
< 2; i
++) {
346 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
358 * Stop all threads by closing the thread quit pipe.
360 static void stop_threads(void)
364 /* Stopping all threads */
365 DBG("Terminating all threads");
366 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
368 ERR("write error on thread quit pipe");
371 /* Dispatch thread */
372 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
373 futex_nto1_wake(&ust_cmd_queue
.futex
);
377 * Close every consumer sockets.
379 static void close_consumer_sockets(void)
383 if (kconsumer_data
.err_sock
>= 0) {
384 ret
= close(kconsumer_data
.err_sock
);
386 PERROR("kernel consumer err_sock close");
389 if (ustconsumer32_data
.err_sock
>= 0) {
390 ret
= close(ustconsumer32_data
.err_sock
);
392 PERROR("UST consumerd32 err_sock close");
395 if (ustconsumer64_data
.err_sock
>= 0) {
396 ret
= close(ustconsumer64_data
.err_sock
);
398 PERROR("UST consumerd64 err_sock close");
401 if (kconsumer_data
.cmd_sock
>= 0) {
402 ret
= close(kconsumer_data
.cmd_sock
);
404 PERROR("kernel consumer cmd_sock close");
407 if (ustconsumer32_data
.cmd_sock
>= 0) {
408 ret
= close(ustconsumer32_data
.cmd_sock
);
410 PERROR("UST consumerd32 cmd_sock close");
413 if (ustconsumer64_data
.cmd_sock
>= 0) {
414 ret
= close(ustconsumer64_data
.cmd_sock
);
416 PERROR("UST consumerd64 cmd_sock close");
424 static void cleanup(void)
427 struct ltt_session
*sess
, *stmp
;
433 * Close the thread quit pipe. It has already done its job,
434 * since we are now called.
436 utils_close_pipe(thread_quit_pipe
);
439 * If opt_pidfile is undefined, the default file will be wiped when
440 * removing the rundir.
443 ret
= remove(opt_pidfile
);
445 PERROR("remove pidfile %s", opt_pidfile
);
449 DBG("Removing sessiond and consumerd content of directory %s", rundir
);
452 snprintf(path
, PATH_MAX
,
454 rundir
, DEFAULT_LTTNG_SESSIOND_PIDFILE
);
455 DBG("Removing %s", path
);
458 snprintf(path
, PATH_MAX
, "%s/%s", rundir
,
459 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE
);
460 DBG("Removing %s", path
);
464 snprintf(path
, PATH_MAX
,
465 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
467 DBG("Removing %s", path
);
470 snprintf(path
, PATH_MAX
,
471 DEFAULT_KCONSUMERD_PATH
,
473 DBG("Removing directory %s", path
);
476 /* ust consumerd 32 */
477 snprintf(path
, PATH_MAX
,
478 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
480 DBG("Removing %s", path
);
483 snprintf(path
, PATH_MAX
,
484 DEFAULT_USTCONSUMERD32_PATH
,
486 DBG("Removing directory %s", path
);
489 /* ust consumerd 64 */
490 snprintf(path
, PATH_MAX
,
491 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
493 DBG("Removing %s", path
);
496 snprintf(path
, PATH_MAX
,
497 DEFAULT_USTCONSUMERD64_PATH
,
499 DBG("Removing directory %s", path
);
503 * We do NOT rmdir rundir because there are other processes
504 * using it, for instance lttng-relayd, which can start in
505 * parallel with this teardown.
510 DBG("Cleaning up all sessions");
512 /* Destroy session list mutex */
513 if (session_list_ptr
!= NULL
) {
514 pthread_mutex_destroy(&session_list_ptr
->lock
);
516 /* Cleanup ALL session */
517 cds_list_for_each_entry_safe(sess
, stmp
,
518 &session_list_ptr
->head
, list
) {
519 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
523 DBG("Closing all UST sockets");
524 ust_app_clean_list();
525 buffer_reg_destroy_registries();
527 if (is_root
&& !opt_no_kernel
) {
528 DBG2("Closing kernel fd");
529 if (kernel_tracer_fd
>= 0) {
530 ret
= close(kernel_tracer_fd
);
535 DBG("Unloading kernel modules");
536 modprobe_remove_lttng_all();
539 close_consumer_sockets();
542 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
543 "Matthew, BEET driven development works!%c[%dm",
544 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
549 * Send data on a unix socket using the liblttsessiondcomm API.
551 * Return lttcomm error code.
553 static int send_unix_sock(int sock
, void *buf
, size_t len
)
555 /* Check valid length */
560 return lttcomm_send_unix_sock(sock
, buf
, len
);
564 * Free memory of a command context structure.
566 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
568 DBG("Clean command context structure");
570 if ((*cmd_ctx
)->llm
) {
571 free((*cmd_ctx
)->llm
);
573 if ((*cmd_ctx
)->lsm
) {
574 free((*cmd_ctx
)->lsm
);
582 * Notify UST applications using the shm mmap futex.
584 static int notify_ust_apps(int active
)
588 DBG("Notifying applications of session daemon state: %d", active
);
590 /* See shm.c for this call implying mmap, shm and futex calls */
591 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
592 if (wait_shm_mmap
== NULL
) {
596 /* Wake waiting process */
597 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
599 /* Apps notified successfully */
607 * Setup the outgoing data buffer for the response (llm) by allocating the
608 * right amount of memory and copying the original information from the lsm
611 * Return total size of the buffer pointed by buf.
613 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
619 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
620 if (cmd_ctx
->llm
== NULL
) {
626 /* Copy common data */
627 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
628 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
630 cmd_ctx
->llm
->data_size
= size
;
631 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
640 * Update the kernel poll set of all channel fd available over all tracing
641 * session. Add the wakeup pipe at the end of the set.
643 static int update_kernel_poll(struct lttng_poll_event
*events
)
646 struct ltt_session
*session
;
647 struct ltt_kernel_channel
*channel
;
649 DBG("Updating kernel poll set");
652 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
653 session_lock(session
);
654 if (session
->kernel_session
== NULL
) {
655 session_unlock(session
);
659 cds_list_for_each_entry(channel
,
660 &session
->kernel_session
->channel_list
.head
, list
) {
661 /* Add channel fd to the kernel poll set */
662 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
664 session_unlock(session
);
667 DBG("Channel fd %d added to kernel set", channel
->fd
);
669 session_unlock(session
);
671 session_unlock_list();
676 session_unlock_list();
681 * Find the channel fd from 'fd' over all tracing session. When found, check
682 * for new channel stream and send those stream fds to the kernel consumer.
684 * Useful for CPU hotplug feature.
686 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
689 struct ltt_session
*session
;
690 struct ltt_kernel_session
*ksess
;
691 struct ltt_kernel_channel
*channel
;
693 DBG("Updating kernel streams for channel fd %d", fd
);
696 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
697 session_lock(session
);
698 if (session
->kernel_session
== NULL
) {
699 session_unlock(session
);
702 ksess
= session
->kernel_session
;
704 cds_list_for_each_entry(channel
, &ksess
->channel_list
.head
, list
) {
705 if (channel
->fd
== fd
) {
706 DBG("Channel found, updating kernel streams");
707 ret
= kernel_open_channel_stream(channel
);
711 /* Update the stream global counter */
712 ksess
->stream_count_global
+= ret
;
715 * Have we already sent fds to the consumer? If yes, it means
716 * that tracing is started so it is safe to send our updated
719 if (ksess
->consumer_fds_sent
== 1 && ksess
->consumer
!= NULL
) {
720 struct lttng_ht_iter iter
;
721 struct consumer_socket
*socket
;
724 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
725 &iter
.iter
, socket
, node
.node
) {
726 pthread_mutex_lock(socket
->lock
);
727 ret
= kernel_consumer_send_channel_stream(socket
,
729 session
->output_traces
? 1 : 0);
730 pthread_mutex_unlock(socket
->lock
);
741 session_unlock(session
);
743 session_unlock_list();
747 session_unlock(session
);
748 session_unlock_list();
753 * For each tracing session, update newly registered apps. The session list
754 * lock MUST be acquired before calling this.
756 static void update_ust_app(int app_sock
)
758 struct ltt_session
*sess
, *stmp
;
760 /* Consumer is in an ERROR state. Stop any application update. */
761 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
762 /* Stop the update process since the consumer is dead. */
766 /* For all tracing session(s) */
767 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
769 if (sess
->ust_session
) {
770 ust_app_global_update(sess
->ust_session
, app_sock
);
772 session_unlock(sess
);
777 * This thread manage event coming from the kernel.
779 * Features supported in this thread:
782 static void *thread_manage_kernel(void *data
)
784 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
786 uint32_t revents
, nb_fd
;
788 struct lttng_poll_event events
;
790 DBG("[thread] Thread manage kernel started");
792 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
795 * This first step of the while is to clean this structure which could free
796 * non NULL pointers so initialize it before the loop.
798 lttng_poll_init(&events
);
800 if (testpoint(thread_manage_kernel
)) {
801 goto error_testpoint
;
804 health_code_update();
806 if (testpoint(thread_manage_kernel_before_loop
)) {
807 goto error_testpoint
;
811 health_code_update();
813 if (update_poll_flag
== 1) {
814 /* Clean events object. We are about to populate it again. */
815 lttng_poll_clean(&events
);
817 ret
= sessiond_set_thread_pollset(&events
, 2);
819 goto error_poll_create
;
822 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
827 /* This will add the available kernel channel if any. */
828 ret
= update_kernel_poll(&events
);
832 update_poll_flag
= 0;
835 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events
));
837 /* Poll infinite value of time */
840 ret
= lttng_poll_wait(&events
, -1);
844 * Restart interrupted system call.
846 if (errno
== EINTR
) {
850 } else if (ret
== 0) {
851 /* Should not happen since timeout is infinite */
852 ERR("Return value of poll is 0 with an infinite timeout.\n"
853 "This should not have happened! Continuing...");
859 for (i
= 0; i
< nb_fd
; i
++) {
860 /* Fetch once the poll data */
861 revents
= LTTNG_POLL_GETEV(&events
, i
);
862 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
864 health_code_update();
866 /* Thread quit pipe has been closed. Killing thread. */
867 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
873 /* Check for data on kernel pipe */
874 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
875 (void) lttng_read(kernel_poll_pipe
[0],
878 * Ret value is useless here, if this pipe gets any actions an
879 * update is required anyway.
881 update_poll_flag
= 1;
885 * New CPU detected by the kernel. Adding kernel stream to
886 * kernel session and updating the kernel consumer
888 if (revents
& LPOLLIN
) {
889 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
895 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
896 * and unregister kernel stream at this point.
905 lttng_poll_clean(&events
);
908 utils_close_pipe(kernel_poll_pipe
);
909 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
912 ERR("Health error occurred in %s", __func__
);
913 WARN("Kernel thread died unexpectedly. "
914 "Kernel tracing can continue but CPU hotplug is disabled.");
916 health_unregister(health_sessiond
);
917 DBG("Kernel thread dying");
922 * Signal pthread condition of the consumer data that the thread.
924 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
926 pthread_mutex_lock(&data
->cond_mutex
);
929 * The state is set before signaling. It can be any value, it's the waiter
930 * job to correctly interpret this condition variable associated to the
931 * consumer pthread_cond.
933 * A value of 0 means that the corresponding thread of the consumer data
934 * was not started. 1 indicates that the thread has started and is ready
935 * for action. A negative value means that there was an error during the
938 data
->consumer_thread_is_ready
= state
;
939 (void) pthread_cond_signal(&data
->cond
);
941 pthread_mutex_unlock(&data
->cond_mutex
);
945 * This thread manage the consumer error sent back to the session daemon.
947 static void *thread_manage_consumer(void *data
)
949 int sock
= -1, i
, ret
, pollfd
, err
= -1;
950 uint32_t revents
, nb_fd
;
951 enum lttcomm_return_code code
;
952 struct lttng_poll_event events
;
953 struct consumer_data
*consumer_data
= data
;
955 DBG("[thread] Manage consumer started");
957 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
959 health_code_update();
962 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
963 * metadata_sock. Nothing more will be added to this poll set.
965 ret
= sessiond_set_thread_pollset(&events
, 3);
971 * The error socket here is already in a listening state which was done
972 * just before spawning this thread to avoid a race between the consumer
973 * daemon exec trying to connect and the listen() call.
975 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
980 health_code_update();
982 /* Infinite blocking call, waiting for transmission */
986 if (testpoint(thread_manage_consumer
)) {
990 ret
= lttng_poll_wait(&events
, -1);
994 * Restart interrupted system call.
996 if (errno
== EINTR
) {
1004 for (i
= 0; i
< nb_fd
; i
++) {
1005 /* Fetch once the poll data */
1006 revents
= LTTNG_POLL_GETEV(&events
, i
);
1007 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1009 health_code_update();
1011 /* Thread quit pipe has been closed. Killing thread. */
1012 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1018 /* Event on the registration socket */
1019 if (pollfd
== consumer_data
->err_sock
) {
1020 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1021 ERR("consumer err socket poll error");
1027 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1033 * Set the CLOEXEC flag. Return code is useless because either way, the
1036 (void) utils_set_fd_cloexec(sock
);
1038 health_code_update();
1040 DBG2("Receiving code from consumer err_sock");
1042 /* Getting status code from kconsumerd */
1043 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1044 sizeof(enum lttcomm_return_code
));
1049 health_code_update();
1051 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1052 /* Connect both socket, command and metadata. */
1053 consumer_data
->cmd_sock
=
1054 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1055 consumer_data
->metadata_fd
=
1056 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1057 if (consumer_data
->cmd_sock
< 0
1058 || consumer_data
->metadata_fd
< 0) {
1059 PERROR("consumer connect cmd socket");
1060 /* On error, signal condition and quit. */
1061 signal_consumer_condition(consumer_data
, -1);
1064 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1065 /* Create metadata socket lock. */
1066 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1067 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1068 PERROR("zmalloc pthread mutex");
1072 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1074 signal_consumer_condition(consumer_data
, 1);
1075 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1076 DBG("Consumer metadata socket ready (fd: %d)",
1077 consumer_data
->metadata_fd
);
1079 ERR("consumer error when waiting for SOCK_READY : %s",
1080 lttcomm_get_readable_code(-code
));
1084 /* Remove the consumerd error sock since we've established a connexion */
1085 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1090 /* Add new accepted error socket. */
1091 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1096 /* Add metadata socket that is successfully connected. */
1097 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1098 LPOLLIN
| LPOLLRDHUP
);
1103 health_code_update();
1105 /* Infinite blocking call, waiting for transmission */
1108 health_poll_entry();
1109 ret
= lttng_poll_wait(&events
, -1);
1113 * Restart interrupted system call.
1115 if (errno
== EINTR
) {
1123 for (i
= 0; i
< nb_fd
; i
++) {
1124 /* Fetch once the poll data */
1125 revents
= LTTNG_POLL_GETEV(&events
, i
);
1126 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1128 health_code_update();
1130 /* Thread quit pipe has been closed. Killing thread. */
1131 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1137 if (pollfd
== sock
) {
1138 /* Event on the consumerd socket */
1139 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1140 ERR("consumer err socket second poll error");
1143 health_code_update();
1144 /* Wait for any kconsumerd error */
1145 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1146 sizeof(enum lttcomm_return_code
));
1148 ERR("consumer closed the command socket");
1152 ERR("consumer return code : %s",
1153 lttcomm_get_readable_code(-code
));
1156 } else if (pollfd
== consumer_data
->metadata_fd
) {
1157 /* UST metadata requests */
1158 ret
= ust_consumer_metadata_request(
1159 &consumer_data
->metadata_sock
);
1161 ERR("Handling metadata request");
1166 ERR("Unknown pollfd");
1170 health_code_update();
1176 * We lock here because we are about to close the sockets and some other
1177 * thread might be using them so get exclusive access which will abort all
1178 * other consumer command by other threads.
1180 pthread_mutex_lock(&consumer_data
->lock
);
1182 /* Immediately set the consumerd state to stopped */
1183 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1184 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1185 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1186 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1187 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1189 /* Code flow error... */
1193 if (consumer_data
->err_sock
>= 0) {
1194 ret
= close(consumer_data
->err_sock
);
1198 consumer_data
->err_sock
= -1;
1200 if (consumer_data
->cmd_sock
>= 0) {
1201 ret
= close(consumer_data
->cmd_sock
);
1205 consumer_data
->cmd_sock
= -1;
1207 if (*consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1208 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1221 unlink(consumer_data
->err_unix_sock_path
);
1222 unlink(consumer_data
->cmd_unix_sock_path
);
1223 consumer_data
->pid
= 0;
1224 pthread_mutex_unlock(&consumer_data
->lock
);
1226 /* Cleanup metadata socket mutex. */
1227 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1228 free(consumer_data
->metadata_sock
.lock
);
1230 lttng_poll_clean(&events
);
1234 ERR("Health error occurred in %s", __func__
);
1236 health_unregister(health_sessiond
);
1237 DBG("consumer thread cleanup completed");
1243 * This thread manage application communication.
1245 static void *thread_manage_apps(void *data
)
1247 int i
, ret
, pollfd
, err
= -1;
1249 uint32_t revents
, nb_fd
;
1250 struct lttng_poll_event events
;
1252 DBG("[thread] Manage application started");
1254 rcu_register_thread();
1255 rcu_thread_online();
1257 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1259 if (testpoint(thread_manage_apps
)) {
1260 goto error_testpoint
;
1263 health_code_update();
1265 ret
= sessiond_set_thread_pollset(&events
, 2);
1267 goto error_poll_create
;
1270 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1275 if (testpoint(thread_manage_apps_before_loop
)) {
1279 health_code_update();
1282 DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events
));
1284 /* Inifinite blocking call, waiting for transmission */
1286 health_poll_entry();
1287 ret
= lttng_poll_wait(&events
, -1);
1291 * Restart interrupted system call.
1293 if (errno
== EINTR
) {
1301 for (i
= 0; i
< nb_fd
; i
++) {
1302 /* Fetch once the poll data */
1303 revents
= LTTNG_POLL_GETEV(&events
, i
);
1304 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1306 health_code_update();
1308 /* Thread quit pipe has been closed. Killing thread. */
1309 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1315 /* Inspect the apps cmd pipe */
1316 if (pollfd
== apps_cmd_pipe
[0]) {
1317 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1318 ERR("Apps command pipe error");
1320 } else if (revents
& LPOLLIN
) {
1324 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1325 if (size_ret
< sizeof(sock
)) {
1326 PERROR("read apps cmd pipe");
1330 health_code_update();
1333 * We only monitor the error events of the socket. This
1334 * thread does not handle any incoming data from UST
1337 ret
= lttng_poll_add(&events
, sock
,
1338 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1343 DBG("Apps with sock %d added to poll set", sock
);
1345 health_code_update();
1351 * At this point, we know that a registered application made
1352 * the event at poll_wait.
1354 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1355 /* Removing from the poll set */
1356 ret
= lttng_poll_del(&events
, pollfd
);
1361 /* Socket closed on remote end. */
1362 ust_app_unregister(pollfd
);
1367 health_code_update();
1373 lttng_poll_clean(&events
);
1376 utils_close_pipe(apps_cmd_pipe
);
1377 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1380 * We don't clean the UST app hash table here since already registered
1381 * applications can still be controlled so let them be until the session
1382 * daemon dies or the applications stop.
1387 ERR("Health error occurred in %s", __func__
);
1389 health_unregister(health_sessiond
);
1390 DBG("Application communication apps thread cleanup complete");
1391 rcu_thread_offline();
1392 rcu_unregister_thread();
1397 * Send a socket to a thread This is called from the dispatch UST registration
1398 * thread once all sockets are set for the application.
1400 * The sock value can be invalid, we don't really care, the thread will handle
1401 * it and make the necessary cleanup if so.
1403 * On success, return 0 else a negative value being the errno message of the
1406 static int send_socket_to_thread(int fd
, int sock
)
1411 * It's possible that the FD is set as invalid with -1 concurrently just
1412 * before calling this function being a shutdown state of the thread.
1419 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1420 if (ret
< sizeof(sock
)) {
1421 PERROR("write apps pipe %d", fd
);
1428 /* All good. Don't send back the write positive ret value. */
1435 * Sanitize the wait queue of the dispatch registration thread meaning removing
1436 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1437 * notify socket is never received.
1439 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1441 int ret
, nb_fd
= 0, i
;
1442 unsigned int fd_added
= 0;
1443 struct lttng_poll_event events
;
1444 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1448 lttng_poll_init(&events
);
1450 /* Just skip everything for an empty queue. */
1451 if (!wait_queue
->count
) {
1455 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1460 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1461 &wait_queue
->head
, head
) {
1462 assert(wait_node
->app
);
1463 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1464 LPOLLHUP
| LPOLLERR
);
1477 * Poll but don't block so we can quickly identify the faulty events and
1478 * clean them afterwards from the wait queue.
1480 ret
= lttng_poll_wait(&events
, 0);
1486 for (i
= 0; i
< nb_fd
; i
++) {
1487 /* Get faulty FD. */
1488 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1489 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1491 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1492 &wait_queue
->head
, head
) {
1493 if (pollfd
== wait_node
->app
->sock
&&
1494 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1495 cds_list_del(&wait_node
->head
);
1496 wait_queue
->count
--;
1497 ust_app_destroy(wait_node
->app
);
1505 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1509 lttng_poll_clean(&events
);
1513 lttng_poll_clean(&events
);
1515 ERR("Unable to sanitize wait queue");
1520 * Dispatch request from the registration threads to the application
1521 * communication thread.
1523 static void *thread_dispatch_ust_registration(void *data
)
1526 struct cds_wfq_node
*node
;
1527 struct ust_command
*ust_cmd
= NULL
;
1528 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1529 struct ust_reg_wait_queue wait_queue
= {
1533 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1535 health_code_update();
1537 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1539 DBG("[thread] Dispatch UST command started");
1541 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1542 health_code_update();
1544 /* Atomically prepare the queue futex */
1545 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1548 struct ust_app
*app
= NULL
;
1552 * Make sure we don't have node(s) that have hung up before receiving
1553 * the notify socket. This is to clean the list in order to avoid
1554 * memory leaks from notify socket that are never seen.
1556 sanitize_wait_queue(&wait_queue
);
1558 health_code_update();
1559 /* Dequeue command for registration */
1560 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1562 DBG("Woken up but nothing in the UST command queue");
1563 /* Continue thread execution */
1567 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1569 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1570 " gid:%d sock:%d name:%s (version %d.%d)",
1571 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1572 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1573 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1574 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1576 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1577 wait_node
= zmalloc(sizeof(*wait_node
));
1579 PERROR("zmalloc wait_node dispatch");
1580 ret
= close(ust_cmd
->sock
);
1582 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1584 lttng_fd_put(LTTNG_FD_APPS
, 1);
1588 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1590 /* Create application object if socket is CMD. */
1591 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1593 if (!wait_node
->app
) {
1594 ret
= close(ust_cmd
->sock
);
1596 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1598 lttng_fd_put(LTTNG_FD_APPS
, 1);
1604 * Add application to the wait queue so we can set the notify
1605 * socket before putting this object in the global ht.
1607 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1612 * We have to continue here since we don't have the notify
1613 * socket and the application MUST be added to the hash table
1614 * only at that moment.
1619 * Look for the application in the local wait queue and set the
1620 * notify socket if found.
1622 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1623 &wait_queue
.head
, head
) {
1624 health_code_update();
1625 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1626 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1627 cds_list_del(&wait_node
->head
);
1629 app
= wait_node
->app
;
1631 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1637 * With no application at this stage the received socket is
1638 * basically useless so close it before we free the cmd data
1639 * structure for good.
1642 ret
= close(ust_cmd
->sock
);
1644 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1646 lttng_fd_put(LTTNG_FD_APPS
, 1);
1653 * @session_lock_list
1655 * Lock the global session list so from the register up to the
1656 * registration done message, no thread can see the application
1657 * and change its state.
1659 session_lock_list();
1663 * Add application to the global hash table. This needs to be
1664 * done before the update to the UST registry can locate the
1669 /* Set app version. This call will print an error if needed. */
1670 (void) ust_app_version(app
);
1672 /* Send notify socket through the notify pipe. */
1673 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1677 session_unlock_list();
1679 * No notify thread, stop the UST tracing. However, this is
1680 * not an internal error of the this thread thus setting
1681 * the health error code to a normal exit.
1688 * Update newly registered application with the tracing
1689 * registry info already enabled information.
1691 update_ust_app(app
->sock
);
1694 * Don't care about return value. Let the manage apps threads
1695 * handle app unregistration upon socket close.
1697 (void) ust_app_register_done(app
->sock
);
1700 * Even if the application socket has been closed, send the app
1701 * to the thread and unregistration will take place at that
1704 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1707 session_unlock_list();
1709 * No apps. thread, stop the UST tracing. However, this is
1710 * not an internal error of the this thread thus setting
1711 * the health error code to a normal exit.
1718 session_unlock_list();
1720 } while (node
!= NULL
);
1722 health_poll_entry();
1723 /* Futex wait on queue. Blocking call on futex() */
1724 futex_nto1_wait(&ust_cmd_queue
.futex
);
1727 /* Normal exit, no error */
1731 /* Clean up wait queue. */
1732 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1733 &wait_queue
.head
, head
) {
1734 cds_list_del(&wait_node
->head
);
1739 DBG("Dispatch thread dying");
1742 ERR("Health error occurred in %s", __func__
);
1744 health_unregister(health_sessiond
);
1749 * This thread manage application registration.
1751 static void *thread_registration_apps(void *data
)
1753 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1754 uint32_t revents
, nb_fd
;
1755 struct lttng_poll_event events
;
1757 * Get allocated in this thread, enqueued to a global queue, dequeued and
1758 * freed in the manage apps thread.
1760 struct ust_command
*ust_cmd
= NULL
;
1762 DBG("[thread] Manage application registration started");
1764 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
1766 if (testpoint(thread_registration_apps
)) {
1767 goto error_testpoint
;
1770 ret
= lttcomm_listen_unix_sock(apps_sock
);
1776 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1777 * more will be added to this poll set.
1779 ret
= sessiond_set_thread_pollset(&events
, 2);
1781 goto error_create_poll
;
1784 /* Add the application registration socket */
1785 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1787 goto error_poll_add
;
1790 /* Notify all applications to register */
1791 ret
= notify_ust_apps(1);
1793 ERR("Failed to notify applications or create the wait shared memory.\n"
1794 "Execution continues but there might be problem for already\n"
1795 "running applications that wishes to register.");
1799 DBG("Accepting application registration");
1801 /* Inifinite blocking call, waiting for transmission */
1803 health_poll_entry();
1804 ret
= lttng_poll_wait(&events
, -1);
1808 * Restart interrupted system call.
1810 if (errno
== EINTR
) {
1818 for (i
= 0; i
< nb_fd
; i
++) {
1819 health_code_update();
1821 /* Fetch once the poll data */
1822 revents
= LTTNG_POLL_GETEV(&events
, i
);
1823 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1825 /* Thread quit pipe has been closed. Killing thread. */
1826 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1832 /* Event on the registration socket */
1833 if (pollfd
== apps_sock
) {
1834 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1835 ERR("Register apps socket poll error");
1837 } else if (revents
& LPOLLIN
) {
1838 sock
= lttcomm_accept_unix_sock(apps_sock
);
1844 * Set socket timeout for both receiving and ending.
1845 * app_socket_timeout is in seconds, whereas
1846 * lttcomm_setsockopt_rcv_timeout and
1847 * lttcomm_setsockopt_snd_timeout expect msec as
1850 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1851 app_socket_timeout
* 1000);
1852 (void) lttcomm_setsockopt_snd_timeout(sock
,
1853 app_socket_timeout
* 1000);
1856 * Set the CLOEXEC flag. Return code is useless because
1857 * either way, the show must go on.
1859 (void) utils_set_fd_cloexec(sock
);
1861 /* Create UST registration command for enqueuing */
1862 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1863 if (ust_cmd
== NULL
) {
1864 PERROR("ust command zmalloc");
1869 * Using message-based transmissions to ensure we don't
1870 * have to deal with partially received messages.
1872 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1874 ERR("Exhausted file descriptors allowed for applications.");
1884 health_code_update();
1885 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
1888 /* Close socket of the application. */
1893 lttng_fd_put(LTTNG_FD_APPS
, 1);
1897 health_code_update();
1899 ust_cmd
->sock
= sock
;
1902 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1903 " gid:%d sock:%d name:%s (version %d.%d)",
1904 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1905 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1906 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1907 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1910 * Lock free enqueue the registration request. The red pill
1911 * has been taken! This apps will be part of the *system*.
1913 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1916 * Wake the registration queue futex. Implicit memory
1917 * barrier with the exchange in cds_wfq_enqueue.
1919 futex_nto1_wake(&ust_cmd_queue
.futex
);
1929 ERR("Health error occurred in %s", __func__
);
1932 /* Notify that the registration thread is gone */
1935 if (apps_sock
>= 0) {
1936 ret
= close(apps_sock
);
1946 lttng_fd_put(LTTNG_FD_APPS
, 1);
1948 unlink(apps_unix_sock_path
);
1951 lttng_poll_clean(&events
);
1955 DBG("UST Registration thread cleanup complete");
1956 health_unregister(health_sessiond
);
1962 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1963 * exec or it will fails.
1965 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
1968 struct timespec timeout
;
1970 /* Make sure we set the readiness flag to 0 because we are NOT ready */
1971 consumer_data
->consumer_thread_is_ready
= 0;
1973 /* Setup pthread condition */
1974 ret
= pthread_condattr_init(&consumer_data
->condattr
);
1977 PERROR("pthread_condattr_init consumer data");
1982 * Set the monotonic clock in order to make sure we DO NOT jump in time
1983 * between the clock_gettime() call and the timedwait call. See bug #324
1984 * for a more details and how we noticed it.
1986 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
1989 PERROR("pthread_condattr_setclock consumer data");
1993 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
1996 PERROR("pthread_cond_init consumer data");
2000 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
2003 PERROR("pthread_create consumer");
2008 /* We are about to wait on a pthread condition */
2009 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2011 /* Get time for sem_timedwait absolute timeout */
2012 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2014 * Set the timeout for the condition timed wait even if the clock gettime
2015 * call fails since we might loop on that call and we want to avoid to
2016 * increment the timeout too many times.
2018 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2021 * The following loop COULD be skipped in some conditions so this is why we
2022 * set ret to 0 in order to make sure at least one round of the loop is
2028 * Loop until the condition is reached or when a timeout is reached. Note
2029 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2030 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2031 * possible. This loop does not take any chances and works with both of
2034 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2035 if (clock_ret
< 0) {
2036 PERROR("clock_gettime spawn consumer");
2037 /* Infinite wait for the consumerd thread to be ready */
2038 ret
= pthread_cond_wait(&consumer_data
->cond
,
2039 &consumer_data
->cond_mutex
);
2041 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2042 &consumer_data
->cond_mutex
, &timeout
);
2046 /* Release the pthread condition */
2047 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2051 if (ret
== ETIMEDOUT
) {
2053 * Call has timed out so we kill the kconsumerd_thread and return
2056 ERR("Condition timed out. The consumer thread was never ready."
2058 ret
= pthread_cancel(consumer_data
->thread
);
2060 PERROR("pthread_cancel consumer thread");
2063 PERROR("pthread_cond_wait failed consumer thread");
2068 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2069 if (consumer_data
->pid
== 0) {
2070 ERR("Consumerd did not start");
2071 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2074 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2083 * Join consumer thread
2085 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2089 /* Consumer pid must be a real one. */
2090 if (consumer_data
->pid
> 0) {
2092 ret
= kill(consumer_data
->pid
, SIGTERM
);
2094 ERR("Error killing consumer daemon");
2097 return pthread_join(consumer_data
->thread
, &status
);
2104 * Fork and exec a consumer daemon (consumerd).
2106 * Return pid if successful else -1.
2108 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2112 const char *consumer_to_use
;
2113 const char *verbosity
;
2116 DBG("Spawning consumerd");
2123 if (opt_verbose_consumer
) {
2124 verbosity
= "--verbose";
2126 verbosity
= "--quiet";
2128 switch (consumer_data
->type
) {
2129 case LTTNG_CONSUMER_KERNEL
:
2131 * Find out which consumerd to execute. We will first try the
2132 * 64-bit path, then the sessiond's installation directory, and
2133 * fallback on the 32-bit one,
2135 DBG3("Looking for a kernel consumer at these locations:");
2136 DBG3(" 1) %s", consumerd64_bin
);
2137 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2138 DBG3(" 3) %s", consumerd32_bin
);
2139 if (stat(consumerd64_bin
, &st
) == 0) {
2140 DBG3("Found location #1");
2141 consumer_to_use
= consumerd64_bin
;
2142 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2143 DBG3("Found location #2");
2144 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2145 } else if (stat(consumerd32_bin
, &st
) == 0) {
2146 DBG3("Found location #3");
2147 consumer_to_use
= consumerd32_bin
;
2149 DBG("Could not find any valid consumerd executable");
2152 DBG("Using kernel consumer at: %s", consumer_to_use
);
2153 execl(consumer_to_use
,
2154 "lttng-consumerd", verbosity
, "-k",
2155 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2156 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2157 "--group", tracing_group_name
,
2160 case LTTNG_CONSUMER64_UST
:
2162 char *tmpnew
= NULL
;
2164 if (consumerd64_libdir
[0] != '\0') {
2168 tmp
= getenv("LD_LIBRARY_PATH");
2172 tmplen
= strlen("LD_LIBRARY_PATH=")
2173 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2174 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2179 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2180 strcat(tmpnew
, consumerd64_libdir
);
2181 if (tmp
[0] != '\0') {
2182 strcat(tmpnew
, ":");
2183 strcat(tmpnew
, tmp
);
2185 ret
= putenv(tmpnew
);
2192 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2193 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2194 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2195 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2196 "--group", tracing_group_name
,
2198 if (consumerd64_libdir
[0] != '\0') {
2206 case LTTNG_CONSUMER32_UST
:
2208 char *tmpnew
= NULL
;
2210 if (consumerd32_libdir
[0] != '\0') {
2214 tmp
= getenv("LD_LIBRARY_PATH");
2218 tmplen
= strlen("LD_LIBRARY_PATH=")
2219 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2220 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2225 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2226 strcat(tmpnew
, consumerd32_libdir
);
2227 if (tmp
[0] != '\0') {
2228 strcat(tmpnew
, ":");
2229 strcat(tmpnew
, tmp
);
2231 ret
= putenv(tmpnew
);
2238 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2239 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2240 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2241 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2242 "--group", tracing_group_name
,
2244 if (consumerd32_libdir
[0] != '\0') {
2253 PERROR("unknown consumer type");
2257 PERROR("kernel start consumer exec");
2260 } else if (pid
> 0) {
2263 PERROR("start consumer fork");
2271 * Spawn the consumerd daemon and session daemon thread.
2273 static int start_consumerd(struct consumer_data
*consumer_data
)
2278 * Set the listen() state on the socket since there is a possible race
2279 * between the exec() of the consumer daemon and this call if place in the
2280 * consumer thread. See bug #366 for more details.
2282 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2287 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2288 if (consumer_data
->pid
!= 0) {
2289 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2293 ret
= spawn_consumerd(consumer_data
);
2295 ERR("Spawning consumerd failed");
2296 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2300 /* Setting up the consumer_data pid */
2301 consumer_data
->pid
= ret
;
2302 DBG2("Consumer pid %d", consumer_data
->pid
);
2303 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2305 DBG2("Spawning consumer control thread");
2306 ret
= spawn_consumer_thread(consumer_data
);
2308 ERR("Fatal error spawning consumer control thread");
2316 /* Cleanup already created sockets on error. */
2317 if (consumer_data
->err_sock
>= 0) {
2320 err
= close(consumer_data
->err_sock
);
2322 PERROR("close consumer data error socket");
2329 * Setup necessary data for kernel tracer action.
2331 static int init_kernel_tracer(void)
2335 /* Modprobe lttng kernel modules */
2336 ret
= modprobe_lttng_control();
2341 /* Open debugfs lttng */
2342 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2343 if (kernel_tracer_fd
< 0) {
2344 DBG("Failed to open %s", module_proc_lttng
);
2349 /* Validate kernel version */
2350 ret
= kernel_validate_version(kernel_tracer_fd
);
2355 ret
= modprobe_lttng_data();
2360 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2364 modprobe_remove_lttng_control();
2365 ret
= close(kernel_tracer_fd
);
2369 kernel_tracer_fd
= -1;
2370 return LTTNG_ERR_KERN_VERSION
;
2373 ret
= close(kernel_tracer_fd
);
2379 modprobe_remove_lttng_control();
2382 WARN("No kernel tracer available");
2383 kernel_tracer_fd
= -1;
2385 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2387 return LTTNG_ERR_KERN_NA
;
2393 * Copy consumer output from the tracing session to the domain session. The
2394 * function also applies the right modification on a per domain basis for the
2395 * trace files destination directory.
2397 * Should *NOT* be called with RCU read-side lock held.
2399 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2402 const char *dir_name
;
2403 struct consumer_output
*consumer
;
2406 assert(session
->consumer
);
2409 case LTTNG_DOMAIN_KERNEL
:
2410 DBG3("Copying tracing session consumer output in kernel session");
2412 * XXX: We should audit the session creation and what this function
2413 * does "extra" in order to avoid a destroy since this function is used
2414 * in the domain session creation (kernel and ust) only. Same for UST
2417 if (session
->kernel_session
->consumer
) {
2418 consumer_destroy_output(session
->kernel_session
->consumer
);
2420 session
->kernel_session
->consumer
=
2421 consumer_copy_output(session
->consumer
);
2422 /* Ease our life a bit for the next part */
2423 consumer
= session
->kernel_session
->consumer
;
2424 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2426 case LTTNG_DOMAIN_JUL
:
2427 case LTTNG_DOMAIN_UST
:
2428 DBG3("Copying tracing session consumer output in UST session");
2429 if (session
->ust_session
->consumer
) {
2430 consumer_destroy_output(session
->ust_session
->consumer
);
2432 session
->ust_session
->consumer
=
2433 consumer_copy_output(session
->consumer
);
2434 /* Ease our life a bit for the next part */
2435 consumer
= session
->ust_session
->consumer
;
2436 dir_name
= DEFAULT_UST_TRACE_DIR
;
2439 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2443 /* Append correct directory to subdir */
2444 strncat(consumer
->subdir
, dir_name
,
2445 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2446 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2455 * Create an UST session and add it to the session ust list.
2457 * Should *NOT* be called with RCU read-side lock held.
2459 static int create_ust_session(struct ltt_session
*session
,
2460 struct lttng_domain
*domain
)
2463 struct ltt_ust_session
*lus
= NULL
;
2467 assert(session
->consumer
);
2469 switch (domain
->type
) {
2470 case LTTNG_DOMAIN_JUL
:
2471 case LTTNG_DOMAIN_UST
:
2474 ERR("Unknown UST domain on create session %d", domain
->type
);
2475 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2479 DBG("Creating UST session");
2481 lus
= trace_ust_create_session(session
->id
);
2483 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2487 lus
->uid
= session
->uid
;
2488 lus
->gid
= session
->gid
;
2489 lus
->output_traces
= session
->output_traces
;
2490 lus
->snapshot_mode
= session
->snapshot_mode
;
2491 lus
->live_timer_interval
= session
->live_timer
;
2492 session
->ust_session
= lus
;
2494 /* Copy session output to the newly created UST session */
2495 ret
= copy_session_consumer(domain
->type
, session
);
2496 if (ret
!= LTTNG_OK
) {
2504 session
->ust_session
= NULL
;
2509 * Create a kernel tracer session then create the default channel.
2511 static int create_kernel_session(struct ltt_session
*session
)
2515 DBG("Creating kernel session");
2517 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2519 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2523 /* Code flow safety */
2524 assert(session
->kernel_session
);
2526 /* Copy session output to the newly created Kernel session */
2527 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2528 if (ret
!= LTTNG_OK
) {
2532 /* Create directory(ies) on local filesystem. */
2533 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2534 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2535 ret
= run_as_mkdir_recursive(
2536 session
->kernel_session
->consumer
->dst
.trace_path
,
2537 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2539 if (ret
!= -EEXIST
) {
2540 ERR("Trace directory creation error");
2546 session
->kernel_session
->uid
= session
->uid
;
2547 session
->kernel_session
->gid
= session
->gid
;
2548 session
->kernel_session
->output_traces
= session
->output_traces
;
2549 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2554 trace_kernel_destroy_session(session
->kernel_session
);
2555 session
->kernel_session
= NULL
;
2560 * Count number of session permitted by uid/gid.
2562 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2565 struct ltt_session
*session
;
2567 DBG("Counting number of available session for UID %d GID %d",
2569 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2571 * Only list the sessions the user can control.
2573 if (!session_access_ok(session
, uid
, gid
)) {
2582 * Process the command requested by the lttng client within the command
2583 * context structure. This function make sure that the return structure (llm)
2584 * is set and ready for transmission before returning.
2586 * Return any error encountered or 0 for success.
2588 * "sock" is only used for special-case var. len data.
2590 * Should *NOT* be called with RCU read-side lock held.
2592 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2596 int need_tracing_session
= 1;
2599 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2603 switch (cmd_ctx
->lsm
->cmd_type
) {
2604 case LTTNG_CREATE_SESSION
:
2605 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2606 case LTTNG_CREATE_SESSION_LIVE
:
2607 case LTTNG_DESTROY_SESSION
:
2608 case LTTNG_LIST_SESSIONS
:
2609 case LTTNG_LIST_DOMAINS
:
2610 case LTTNG_START_TRACE
:
2611 case LTTNG_STOP_TRACE
:
2612 case LTTNG_DATA_PENDING
:
2613 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2614 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2615 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2616 case LTTNG_SNAPSHOT_RECORD
:
2623 if (opt_no_kernel
&& need_domain
2624 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2626 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2628 ret
= LTTNG_ERR_KERN_NA
;
2633 /* Deny register consumer if we already have a spawned consumer. */
2634 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2635 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2636 if (kconsumer_data
.pid
> 0) {
2637 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2638 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2641 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2645 * Check for command that don't needs to allocate a returned payload. We do
2646 * this here so we don't have to make the call for no payload at each
2649 switch(cmd_ctx
->lsm
->cmd_type
) {
2650 case LTTNG_LIST_SESSIONS
:
2651 case LTTNG_LIST_TRACEPOINTS
:
2652 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2653 case LTTNG_LIST_DOMAINS
:
2654 case LTTNG_LIST_CHANNELS
:
2655 case LTTNG_LIST_EVENTS
:
2658 /* Setup lttng message with no payload */
2659 ret
= setup_lttng_msg(cmd_ctx
, 0);
2661 /* This label does not try to unlock the session */
2662 goto init_setup_error
;
2666 /* Commands that DO NOT need a session. */
2667 switch (cmd_ctx
->lsm
->cmd_type
) {
2668 case LTTNG_CREATE_SESSION
:
2669 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2670 case LTTNG_CREATE_SESSION_LIVE
:
2671 case LTTNG_CALIBRATE
:
2672 case LTTNG_LIST_SESSIONS
:
2673 case LTTNG_LIST_TRACEPOINTS
:
2674 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2675 need_tracing_session
= 0;
2678 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2680 * We keep the session list lock across _all_ commands
2681 * for now, because the per-session lock does not
2682 * handle teardown properly.
2684 session_lock_list();
2685 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2686 if (cmd_ctx
->session
== NULL
) {
2687 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2690 /* Acquire lock for the session */
2691 session_lock(cmd_ctx
->session
);
2701 * Check domain type for specific "pre-action".
2703 switch (cmd_ctx
->lsm
->domain
.type
) {
2704 case LTTNG_DOMAIN_KERNEL
:
2706 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2710 /* Kernel tracer check */
2711 if (kernel_tracer_fd
== -1) {
2712 /* Basically, load kernel tracer modules */
2713 ret
= init_kernel_tracer();
2719 /* Consumer is in an ERROR state. Report back to client */
2720 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
2721 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2725 /* Need a session for kernel command */
2726 if (need_tracing_session
) {
2727 if (cmd_ctx
->session
->kernel_session
== NULL
) {
2728 ret
= create_kernel_session(cmd_ctx
->session
);
2730 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2735 /* Start the kernel consumer daemon */
2736 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2737 if (kconsumer_data
.pid
== 0 &&
2738 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2739 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2740 ret
= start_consumerd(&kconsumer_data
);
2742 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2745 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
2747 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2751 * The consumer was just spawned so we need to add the socket to
2752 * the consumer output of the session if exist.
2754 ret
= consumer_create_socket(&kconsumer_data
,
2755 cmd_ctx
->session
->kernel_session
->consumer
);
2762 case LTTNG_DOMAIN_JUL
:
2763 case LTTNG_DOMAIN_UST
:
2765 if (!ust_app_supported()) {
2766 ret
= LTTNG_ERR_NO_UST
;
2769 /* Consumer is in an ERROR state. Report back to client */
2770 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
2771 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2775 if (need_tracing_session
) {
2776 /* Create UST session if none exist. */
2777 if (cmd_ctx
->session
->ust_session
== NULL
) {
2778 ret
= create_ust_session(cmd_ctx
->session
,
2779 &cmd_ctx
->lsm
->domain
);
2780 if (ret
!= LTTNG_OK
) {
2785 /* Start the UST consumer daemons */
2787 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
2788 if (consumerd64_bin
[0] != '\0' &&
2789 ustconsumer64_data
.pid
== 0 &&
2790 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2791 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2792 ret
= start_consumerd(&ustconsumer64_data
);
2794 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
2795 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
2799 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
2800 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2802 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2806 * Setup socket for consumer 64 bit. No need for atomic access
2807 * since it was set above and can ONLY be set in this thread.
2809 ret
= consumer_create_socket(&ustconsumer64_data
,
2810 cmd_ctx
->session
->ust_session
->consumer
);
2816 if (consumerd32_bin
[0] != '\0' &&
2817 ustconsumer32_data
.pid
== 0 &&
2818 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2819 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2820 ret
= start_consumerd(&ustconsumer32_data
);
2822 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
2823 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
2827 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
2828 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2830 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2834 * Setup socket for consumer 64 bit. No need for atomic access
2835 * since it was set above and can ONLY be set in this thread.
2837 ret
= consumer_create_socket(&ustconsumer32_data
,
2838 cmd_ctx
->session
->ust_session
->consumer
);
2850 /* Validate consumer daemon state when start/stop trace command */
2851 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
2852 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
2853 switch (cmd_ctx
->lsm
->domain
.type
) {
2854 case LTTNG_DOMAIN_JUL
:
2855 case LTTNG_DOMAIN_UST
:
2856 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
2857 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2861 case LTTNG_DOMAIN_KERNEL
:
2862 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
2863 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2871 * Check that the UID or GID match that of the tracing session.
2872 * The root user can interact with all sessions.
2874 if (need_tracing_session
) {
2875 if (!session_access_ok(cmd_ctx
->session
,
2876 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2877 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
2878 ret
= LTTNG_ERR_EPERM
;
2884 * Send relayd information to consumer as soon as we have a domain and a
2887 if (cmd_ctx
->session
&& need_domain
) {
2889 * Setup relayd if not done yet. If the relayd information was already
2890 * sent to the consumer, this call will gracefully return.
2892 ret
= cmd_setup_relayd(cmd_ctx
->session
);
2893 if (ret
!= LTTNG_OK
) {
2898 /* Process by command type */
2899 switch (cmd_ctx
->lsm
->cmd_type
) {
2900 case LTTNG_ADD_CONTEXT
:
2902 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2903 cmd_ctx
->lsm
->u
.context
.channel_name
,
2904 &cmd_ctx
->lsm
->u
.context
.ctx
, kernel_poll_pipe
[1]);
2907 case LTTNG_DISABLE_CHANNEL
:
2909 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2910 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2913 case LTTNG_DISABLE_EVENT
:
2915 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2916 cmd_ctx
->lsm
->u
.disable
.channel_name
,
2917 cmd_ctx
->lsm
->u
.disable
.name
);
2920 case LTTNG_DISABLE_ALL_EVENT
:
2922 DBG("Disabling all events");
2924 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2925 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2928 case LTTNG_ENABLE_CHANNEL
:
2930 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
2931 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
2934 case LTTNG_ENABLE_EVENT
:
2936 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
2937 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2938 &cmd_ctx
->lsm
->u
.enable
.event
, NULL
, kernel_poll_pipe
[1]);
2941 case LTTNG_ENABLE_ALL_EVENT
:
2943 DBG("Enabling all events");
2945 ret
= cmd_enable_event_all(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
2946 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2947 cmd_ctx
->lsm
->u
.enable
.event
.type
, NULL
, kernel_poll_pipe
[1]);
2950 case LTTNG_LIST_TRACEPOINTS
:
2952 struct lttng_event
*events
;
2955 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
2956 if (nb_events
< 0) {
2957 /* Return value is a negative lttng_error_code. */
2963 * Setup lttng message with payload size set to the event list size in
2964 * bytes and then copy list into the llm payload.
2966 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2972 /* Copy event list into message payload */
2973 memcpy(cmd_ctx
->llm
->payload
, events
,
2974 sizeof(struct lttng_event
) * nb_events
);
2981 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2983 struct lttng_event_field
*fields
;
2986 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
2988 if (nb_fields
< 0) {
2989 /* Return value is a negative lttng_error_code. */
2995 * Setup lttng message with payload size set to the event list size in
2996 * bytes and then copy list into the llm payload.
2998 ret
= setup_lttng_msg(cmd_ctx
,
2999 sizeof(struct lttng_event_field
) * nb_fields
);
3005 /* Copy event list into message payload */
3006 memcpy(cmd_ctx
->llm
->payload
, fields
,
3007 sizeof(struct lttng_event_field
) * nb_fields
);
3014 case LTTNG_SET_CONSUMER_URI
:
3017 struct lttng_uri
*uris
;
3019 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3020 len
= nb_uri
* sizeof(struct lttng_uri
);
3023 ret
= LTTNG_ERR_INVALID
;
3027 uris
= zmalloc(len
);
3029 ret
= LTTNG_ERR_FATAL
;
3033 /* Receive variable len data */
3034 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3035 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3037 DBG("No URIs received from client... continuing");
3039 ret
= LTTNG_ERR_SESSION_FAIL
;
3044 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3046 if (ret
!= LTTNG_OK
) {
3052 * XXX: 0 means that this URI should be applied on the session. Should
3053 * be a DOMAIN enuam.
3055 if (cmd_ctx
->lsm
->domain
.type
== 0) {
3056 /* Add the URI for the UST session if a consumer is present. */
3057 if (cmd_ctx
->session
->ust_session
&&
3058 cmd_ctx
->session
->ust_session
->consumer
) {
3059 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_UST
, cmd_ctx
->session
,
3061 } else if (cmd_ctx
->session
->kernel_session
&&
3062 cmd_ctx
->session
->kernel_session
->consumer
) {
3063 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL
,
3064 cmd_ctx
->session
, nb_uri
, uris
);
3072 case LTTNG_START_TRACE
:
3074 ret
= cmd_start_trace(cmd_ctx
->session
);
3077 case LTTNG_STOP_TRACE
:
3079 ret
= cmd_stop_trace(cmd_ctx
->session
);
3082 case LTTNG_CREATE_SESSION
:
3085 struct lttng_uri
*uris
= NULL
;
3087 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3088 len
= nb_uri
* sizeof(struct lttng_uri
);
3091 uris
= zmalloc(len
);
3093 ret
= LTTNG_ERR_FATAL
;
3097 /* Receive variable len data */
3098 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3099 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3101 DBG("No URIs received from client... continuing");
3103 ret
= LTTNG_ERR_SESSION_FAIL
;
3108 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3109 DBG("Creating session with ONE network URI is a bad call");
3110 ret
= LTTNG_ERR_SESSION_FAIL
;
3116 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3117 &cmd_ctx
->creds
, 0);
3123 case LTTNG_DESTROY_SESSION
:
3125 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3127 /* Set session to NULL so we do not unlock it after free. */
3128 cmd_ctx
->session
= NULL
;
3131 case LTTNG_LIST_DOMAINS
:
3134 struct lttng_domain
*domains
;
3136 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3138 /* Return value is a negative lttng_error_code. */
3143 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
3149 /* Copy event list into message payload */
3150 memcpy(cmd_ctx
->llm
->payload
, domains
,
3151 nb_dom
* sizeof(struct lttng_domain
));
3158 case LTTNG_LIST_CHANNELS
:
3161 struct lttng_channel
*channels
;
3163 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3164 cmd_ctx
->session
, &channels
);
3166 /* Return value is a negative lttng_error_code. */
3171 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
3177 /* Copy event list into message payload */
3178 memcpy(cmd_ctx
->llm
->payload
, channels
,
3179 nb_chan
* sizeof(struct lttng_channel
));
3186 case LTTNG_LIST_EVENTS
:
3189 struct lttng_event
*events
= NULL
;
3191 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3192 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
3194 /* Return value is a negative lttng_error_code. */
3199 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
3205 /* Copy event list into message payload */
3206 memcpy(cmd_ctx
->llm
->payload
, events
,
3207 nb_event
* sizeof(struct lttng_event
));
3214 case LTTNG_LIST_SESSIONS
:
3216 unsigned int nr_sessions
;
3218 session_lock_list();
3219 nr_sessions
= lttng_sessions_count(
3220 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3221 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3223 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
3225 session_unlock_list();
3229 /* Filled the session array */
3230 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
3231 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3232 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3234 session_unlock_list();
3239 case LTTNG_CALIBRATE
:
3241 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3242 &cmd_ctx
->lsm
->u
.calibrate
);
3245 case LTTNG_REGISTER_CONSUMER
:
3247 struct consumer_data
*cdata
;
3249 switch (cmd_ctx
->lsm
->domain
.type
) {
3250 case LTTNG_DOMAIN_KERNEL
:
3251 cdata
= &kconsumer_data
;
3254 ret
= LTTNG_ERR_UND
;
3258 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3259 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3262 case LTTNG_ENABLE_EVENT_WITH_FILTER
:
3264 struct lttng_filter_bytecode
*bytecode
;
3266 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3267 ret
= LTTNG_ERR_FILTER_INVAL
;
3270 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
== 0) {
3271 ret
= LTTNG_ERR_FILTER_INVAL
;
3274 bytecode
= zmalloc(cmd_ctx
->lsm
->u
.enable
.bytecode_len
);
3276 ret
= LTTNG_ERR_FILTER_NOMEM
;
3279 /* Receive var. len. data */
3280 DBG("Receiving var len data from client ...");
3281 ret
= lttcomm_recv_unix_sock(sock
, bytecode
,
3282 cmd_ctx
->lsm
->u
.enable
.bytecode_len
);
3284 DBG("Nothing recv() from client var len data... continuing");
3286 ret
= LTTNG_ERR_FILTER_INVAL
;
3290 if (bytecode
->len
+ sizeof(*bytecode
)
3291 != cmd_ctx
->lsm
->u
.enable
.bytecode_len
) {
3293 ret
= LTTNG_ERR_FILTER_INVAL
;
3297 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3298 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3299 &cmd_ctx
->lsm
->u
.enable
.event
, bytecode
, kernel_poll_pipe
[1]);
3302 case LTTNG_DATA_PENDING
:
3304 ret
= cmd_data_pending(cmd_ctx
->session
);
3307 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3309 struct lttcomm_lttng_output_id reply
;
3311 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3312 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3313 if (ret
!= LTTNG_OK
) {
3317 ret
= setup_lttng_msg(cmd_ctx
, sizeof(reply
));
3322 /* Copy output list into message payload */
3323 memcpy(cmd_ctx
->llm
->payload
, &reply
, sizeof(reply
));
3327 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3329 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3330 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3333 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3336 struct lttng_snapshot_output
*outputs
= NULL
;
3338 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3339 if (nb_output
< 0) {
3344 ret
= setup_lttng_msg(cmd_ctx
,
3345 nb_output
* sizeof(struct lttng_snapshot_output
));
3352 /* Copy output list into message payload */
3353 memcpy(cmd_ctx
->llm
->payload
, outputs
,
3354 nb_output
* sizeof(struct lttng_snapshot_output
));
3361 case LTTNG_SNAPSHOT_RECORD
:
3363 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3364 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3365 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3368 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3371 struct lttng_uri
*uris
= NULL
;
3373 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3374 len
= nb_uri
* sizeof(struct lttng_uri
);
3377 uris
= zmalloc(len
);
3379 ret
= LTTNG_ERR_FATAL
;
3383 /* Receive variable len data */
3384 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3385 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3387 DBG("No URIs received from client... continuing");
3389 ret
= LTTNG_ERR_SESSION_FAIL
;
3394 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3395 DBG("Creating session with ONE network URI is a bad call");
3396 ret
= LTTNG_ERR_SESSION_FAIL
;
3402 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
3403 nb_uri
, &cmd_ctx
->creds
);
3407 case LTTNG_CREATE_SESSION_LIVE
:
3410 struct lttng_uri
*uris
= NULL
;
3412 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3413 len
= nb_uri
* sizeof(struct lttng_uri
);
3416 uris
= zmalloc(len
);
3418 ret
= LTTNG_ERR_FATAL
;
3422 /* Receive variable len data */
3423 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3424 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3426 DBG("No URIs received from client... continuing");
3428 ret
= LTTNG_ERR_SESSION_FAIL
;
3433 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3434 DBG("Creating session with ONE network URI is a bad call");
3435 ret
= LTTNG_ERR_SESSION_FAIL
;
3441 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
3442 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
3447 ret
= LTTNG_ERR_UND
;
3452 if (cmd_ctx
->llm
== NULL
) {
3453 DBG("Missing llm structure. Allocating one.");
3454 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
3458 /* Set return code */
3459 cmd_ctx
->llm
->ret_code
= ret
;
3461 if (cmd_ctx
->session
) {
3462 session_unlock(cmd_ctx
->session
);
3464 if (need_tracing_session
) {
3465 session_unlock_list();
3472 * Thread managing health check socket.
3474 static void *thread_manage_health(void *data
)
3476 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
3477 uint32_t revents
, nb_fd
;
3478 struct lttng_poll_event events
;
3479 struct health_comm_msg msg
;
3480 struct health_comm_reply reply
;
3482 DBG("[thread] Manage health check started");
3484 rcu_register_thread();
3486 /* We might hit an error path before this is created. */
3487 lttng_poll_init(&events
);
3489 /* Create unix socket */
3490 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
3492 ERR("Unable to create health check Unix socket");
3498 /* lttng health client socket path permissions */
3499 ret
= chown(health_unix_sock_path
, 0,
3500 utils_get_group_id(tracing_group_name
));
3502 ERR("Unable to set group on %s", health_unix_sock_path
);
3508 ret
= chmod(health_unix_sock_path
,
3509 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3511 ERR("Unable to set permissions on %s", health_unix_sock_path
);
3519 * Set the CLOEXEC flag. Return code is useless because either way, the
3522 (void) utils_set_fd_cloexec(sock
);
3524 ret
= lttcomm_listen_unix_sock(sock
);
3530 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3531 * more will be added to this poll set.
3533 ret
= sessiond_set_thread_pollset(&events
, 2);
3538 /* Add the application registration socket */
3539 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
3545 DBG("Health check ready");
3547 /* Inifinite blocking call, waiting for transmission */
3549 ret
= lttng_poll_wait(&events
, -1);
3552 * Restart interrupted system call.
3554 if (errno
== EINTR
) {
3562 for (i
= 0; i
< nb_fd
; i
++) {
3563 /* Fetch once the poll data */
3564 revents
= LTTNG_POLL_GETEV(&events
, i
);
3565 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3567 /* Thread quit pipe has been closed. Killing thread. */
3568 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3574 /* Event on the registration socket */
3575 if (pollfd
== sock
) {
3576 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3577 ERR("Health socket poll error");
3583 new_sock
= lttcomm_accept_unix_sock(sock
);
3589 * Set the CLOEXEC flag. Return code is useless because either way, the
3592 (void) utils_set_fd_cloexec(new_sock
);
3594 DBG("Receiving data from client for health...");
3595 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
3597 DBG("Nothing recv() from client... continuing");
3598 ret
= close(new_sock
);
3606 rcu_thread_online();
3609 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
3611 * health_check_state returns 0 if health is
3614 if (!health_check_state(health_sessiond
, i
)) {
3615 reply
.ret_code
|= 1ULL << i
;
3619 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
3621 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
3623 ERR("Failed to send health data back to client");
3626 /* End of transmission */
3627 ret
= close(new_sock
);
3637 ERR("Health error occurred in %s", __func__
);
3639 DBG("Health check thread dying");
3640 unlink(health_unix_sock_path
);
3648 lttng_poll_clean(&events
);
3650 rcu_unregister_thread();
3655 * This thread manage all clients request using the unix client socket for
3658 static void *thread_manage_clients(void *data
)
3660 int sock
= -1, ret
, i
, pollfd
, err
= -1;
3662 uint32_t revents
, nb_fd
;
3663 struct command_ctx
*cmd_ctx
= NULL
;
3664 struct lttng_poll_event events
;
3666 DBG("[thread] Manage client started");
3668 rcu_register_thread();
3670 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
3672 if (testpoint(thread_manage_clients
)) {
3673 goto error_testpoint
;
3676 health_code_update();
3678 ret
= lttcomm_listen_unix_sock(client_sock
);
3684 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3685 * more will be added to this poll set.
3687 ret
= sessiond_set_thread_pollset(&events
, 2);
3689 goto error_create_poll
;
3692 /* Add the application registration socket */
3693 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
3699 * Notify parent pid that we are ready to accept command for client side.
3701 if (opt_sig_parent
) {
3702 kill(ppid
, SIGUSR1
);
3705 if (testpoint(thread_manage_clients_before_loop
)) {
3709 health_code_update();
3712 DBG("Accepting client command ...");
3714 /* Inifinite blocking call, waiting for transmission */
3716 health_poll_entry();
3717 ret
= lttng_poll_wait(&events
, -1);
3721 * Restart interrupted system call.
3723 if (errno
== EINTR
) {
3731 for (i
= 0; i
< nb_fd
; i
++) {
3732 /* Fetch once the poll data */
3733 revents
= LTTNG_POLL_GETEV(&events
, i
);
3734 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3736 health_code_update();
3738 /* Thread quit pipe has been closed. Killing thread. */
3739 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3745 /* Event on the registration socket */
3746 if (pollfd
== client_sock
) {
3747 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3748 ERR("Client socket poll error");
3754 DBG("Wait for client response");
3756 health_code_update();
3758 sock
= lttcomm_accept_unix_sock(client_sock
);
3764 * Set the CLOEXEC flag. Return code is useless because either way, the
3767 (void) utils_set_fd_cloexec(sock
);
3769 /* Set socket option for credentials retrieval */
3770 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
3775 /* Allocate context command to process the client request */
3776 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
3777 if (cmd_ctx
== NULL
) {
3778 PERROR("zmalloc cmd_ctx");
3782 /* Allocate data buffer for reception */
3783 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
3784 if (cmd_ctx
->lsm
== NULL
) {
3785 PERROR("zmalloc cmd_ctx->lsm");
3789 cmd_ctx
->llm
= NULL
;
3790 cmd_ctx
->session
= NULL
;
3792 health_code_update();
3795 * Data is received from the lttng client. The struct
3796 * lttcomm_session_msg (lsm) contains the command and data request of
3799 DBG("Receiving data from client ...");
3800 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
3801 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
3803 DBG("Nothing recv() from client... continuing");
3809 clean_command_ctx(&cmd_ctx
);
3813 health_code_update();
3815 // TODO: Validate cmd_ctx including sanity check for
3816 // security purpose.
3818 rcu_thread_online();
3820 * This function dispatch the work to the kernel or userspace tracer
3821 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3822 * informations for the client. The command context struct contains
3823 * everything this function may needs.
3825 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
3826 rcu_thread_offline();
3834 * TODO: Inform client somehow of the fatal error. At
3835 * this point, ret < 0 means that a zmalloc failed
3836 * (ENOMEM). Error detected but still accept
3837 * command, unless a socket error has been
3840 clean_command_ctx(&cmd_ctx
);
3844 health_code_update();
3846 DBG("Sending response (size: %d, retcode: %s)",
3847 cmd_ctx
->lttng_msg_size
,
3848 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
3849 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
3851 ERR("Failed to send data back to client");
3854 /* End of transmission */
3861 clean_command_ctx(&cmd_ctx
);
3863 health_code_update();
3875 lttng_poll_clean(&events
);
3876 clean_command_ctx(&cmd_ctx
);
3881 unlink(client_unix_sock_path
);
3882 if (client_sock
>= 0) {
3883 ret
= close(client_sock
);
3891 ERR("Health error occurred in %s", __func__
);
3894 health_unregister(health_sessiond
);
3896 DBG("Client thread dying");
3898 rcu_unregister_thread();
3904 * usage function on stderr
3906 static void usage(void)
3908 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
3909 fprintf(stderr
, " -h, --help Display this usage.\n");
3910 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
3911 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
3912 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
3913 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
3914 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
3915 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
3916 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
3917 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
3918 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
3919 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
3920 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
3921 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
3922 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
3923 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
3924 fprintf(stderr
, " -V, --version Show version number.\n");
3925 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
3926 fprintf(stderr
, " -q, --quiet No output at all.\n");
3927 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3928 fprintf(stderr
, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
3929 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
3930 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
3931 fprintf(stderr
, " --jul-tcp-port JUL application registration TCP port\n");
3935 * daemon argument parsing
3937 static int parse_args(int argc
, char **argv
)
3941 static struct option long_options
[] = {
3942 { "client-sock", 1, 0, 'c' },
3943 { "apps-sock", 1, 0, 'a' },
3944 { "kconsumerd-cmd-sock", 1, 0, 'C' },
3945 { "kconsumerd-err-sock", 1, 0, 'E' },
3946 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
3947 { "ustconsumerd32-err-sock", 1, 0, 'H' },
3948 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
3949 { "ustconsumerd64-err-sock", 1, 0, 'F' },
3950 { "consumerd32-path", 1, 0, 'u' },
3951 { "consumerd32-libdir", 1, 0, 'U' },
3952 { "consumerd64-path", 1, 0, 't' },
3953 { "consumerd64-libdir", 1, 0, 'T' },
3954 { "daemonize", 0, 0, 'd' },
3955 { "sig-parent", 0, 0, 'S' },
3956 { "help", 0, 0, 'h' },
3957 { "group", 1, 0, 'g' },
3958 { "version", 0, 0, 'V' },
3959 { "quiet", 0, 0, 'q' },
3960 { "verbose", 0, 0, 'v' },
3961 { "verbose-consumer", 0, 0, 'Z' },
3962 { "no-kernel", 0, 0, 'N' },
3963 { "pidfile", 1, 0, 'p' },
3964 { "jul-tcp-port", 1, 0, 'J' },
3969 int option_index
= 0;
3970 c
= getopt_long(argc
, argv
, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t:p:J:",
3971 long_options
, &option_index
);
3978 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
3980 fprintf(stderr
, " with arg %s\n", optarg
);
3984 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3987 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3993 tracing_group_name
= optarg
;
3999 fprintf(stdout
, "%s\n", VERSION
);
4005 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4008 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4011 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4014 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4017 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4020 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4026 lttng_opt_quiet
= 1;
4029 /* Verbose level can increase using multiple -v */
4030 lttng_opt_verbose
+= 1;
4033 opt_verbose_consumer
+= 1;
4036 consumerd32_bin
= optarg
;
4039 consumerd32_libdir
= optarg
;
4042 consumerd64_bin
= optarg
;
4045 consumerd64_libdir
= optarg
;
4048 opt_pidfile
= optarg
;
4050 case 'J': /* JUL TCP port. */
4055 v
= strtoul(optarg
, NULL
, 0);
4056 if (errno
!= 0 || !isdigit(optarg
[0])) {
4057 ERR("Wrong value in --jul-tcp-port parameter: %s", optarg
);
4060 if (v
== 0 || v
>= 65535) {
4061 ERR("Port overflow in --jul-tcp-port parameter: %s", optarg
);
4064 jul_tcp_port
= (uint32_t) v
;
4065 DBG3("JUL TCP port set to non default: %u", jul_tcp_port
);
4069 /* Unknown option or other error.
4070 * Error is printed by getopt, just return */
4079 * Creates the two needed socket by the daemon.
4080 * apps_sock - The communication socket for all UST apps.
4081 * client_sock - The communication of the cli tool (lttng).
4083 static int init_daemon_socket(void)
4088 old_umask
= umask(0);
4090 /* Create client tool unix socket */
4091 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
4092 if (client_sock
< 0) {
4093 ERR("Create unix sock failed: %s", client_unix_sock_path
);
4098 /* Set the cloexec flag */
4099 ret
= utils_set_fd_cloexec(client_sock
);
4101 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4102 "Continuing but note that the consumer daemon will have a "
4103 "reference to this socket on exec()", client_sock
);
4106 /* File permission MUST be 660 */
4107 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4109 ERR("Set file permissions failed: %s", client_unix_sock_path
);
4114 /* Create the application unix socket */
4115 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
4116 if (apps_sock
< 0) {
4117 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
4122 /* Set the cloexec flag */
4123 ret
= utils_set_fd_cloexec(apps_sock
);
4125 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4126 "Continuing but note that the consumer daemon will have a "
4127 "reference to this socket on exec()", apps_sock
);
4130 /* File permission MUST be 666 */
4131 ret
= chmod(apps_unix_sock_path
,
4132 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
4134 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
4139 DBG3("Session daemon client socket %d and application socket %d created",
4140 client_sock
, apps_sock
);
4148 * Check if the global socket is available, and if a daemon is answering at the
4149 * other side. If yes, error is returned.
4151 static int check_existing_daemon(void)
4153 /* Is there anybody out there ? */
4154 if (lttng_session_daemon_alive()) {
4162 * Set the tracing group gid onto the client socket.
4164 * Race window between mkdir and chown is OK because we are going from more
4165 * permissive (root.root) to less permissive (root.tracing).
4167 static int set_permissions(char *rundir
)
4172 gid
= utils_get_group_id(tracing_group_name
);
4174 /* Set lttng run dir */
4175 ret
= chown(rundir
, 0, gid
);
4177 ERR("Unable to set group on %s", rundir
);
4182 * Ensure all applications and tracing group can search the run
4183 * dir. Allow everyone to read the directory, since it does not
4184 * buy us anything to hide its content.
4186 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
4188 ERR("Unable to set permissions on %s", rundir
);
4192 /* lttng client socket path */
4193 ret
= chown(client_unix_sock_path
, 0, gid
);
4195 ERR("Unable to set group on %s", client_unix_sock_path
);
4199 /* kconsumer error socket path */
4200 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
4202 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
4206 /* 64-bit ustconsumer error socket path */
4207 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
4209 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
4213 /* 32-bit ustconsumer compat32 error socket path */
4214 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
4216 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
4220 DBG("All permissions are set");
4226 * Create the lttng run directory needed for all global sockets and pipe.
4228 static int create_lttng_rundir(const char *rundir
)
4232 DBG3("Creating LTTng run directory: %s", rundir
);
4234 ret
= mkdir(rundir
, S_IRWXU
);
4236 if (errno
!= EEXIST
) {
4237 ERR("Unable to create %s", rundir
);
4249 * Setup sockets and directory needed by the kconsumerd communication with the
4252 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
4256 char path
[PATH_MAX
];
4258 switch (consumer_data
->type
) {
4259 case LTTNG_CONSUMER_KERNEL
:
4260 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
4262 case LTTNG_CONSUMER64_UST
:
4263 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
4265 case LTTNG_CONSUMER32_UST
:
4266 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
4269 ERR("Consumer type unknown");
4274 DBG2("Creating consumer directory: %s", path
);
4276 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
4278 if (errno
!= EEXIST
) {
4280 ERR("Failed to create %s", path
);
4286 ret
= chown(path
, 0, utils_get_group_id(tracing_group_name
));
4288 ERR("Unable to set group on %s", path
);
4294 /* Create the kconsumerd error unix socket */
4295 consumer_data
->err_sock
=
4296 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
4297 if (consumer_data
->err_sock
< 0) {
4298 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
4304 * Set the CLOEXEC flag. Return code is useless because either way, the
4307 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
4309 PERROR("utils_set_fd_cloexec");
4310 /* continue anyway */
4313 /* File permission MUST be 660 */
4314 ret
= chmod(consumer_data
->err_unix_sock_path
,
4315 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4317 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
4327 * Signal handler for the daemon
4329 * Simply stop all worker threads, leaving main() return gracefully after
4330 * joining all threads and calling cleanup().
4332 static void sighandler(int sig
)
4336 DBG("SIGPIPE caught");
4339 DBG("SIGINT caught");
4343 DBG("SIGTERM caught");
4352 * Setup signal handler for :
4353 * SIGINT, SIGTERM, SIGPIPE
4355 static int set_signal_handler(void)
4358 struct sigaction sa
;
4361 if ((ret
= sigemptyset(&sigset
)) < 0) {
4362 PERROR("sigemptyset");
4366 sa
.sa_handler
= sighandler
;
4367 sa
.sa_mask
= sigset
;
4369 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
4370 PERROR("sigaction");
4374 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
4375 PERROR("sigaction");
4379 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
4380 PERROR("sigaction");
4384 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
4390 * Set open files limit to unlimited. This daemon can open a large number of
4391 * file descriptors in order to consumer multiple kernel traces.
4393 static void set_ulimit(void)
4398 /* The kernel does not allowed an infinite limit for open files */
4399 lim
.rlim_cur
= 65535;
4400 lim
.rlim_max
= 65535;
4402 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
4404 PERROR("failed to set open files limit");
4409 * Write pidfile using the rundir and opt_pidfile.
4411 static void write_pidfile(void)
4414 char pidfile_path
[PATH_MAX
];
4419 strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
));
4421 /* Build pidfile path from rundir and opt_pidfile. */
4422 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
4423 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
4425 PERROR("snprintf pidfile path");
4431 * Create pid file in rundir. Return value is of no importance. The
4432 * execution will continue even though we are not able to write the file.
4434 (void) utils_create_pid_file(getpid(), pidfile_path
);
4441 * Write JUL TCP port using the rundir.
4443 static void write_julport(void)
4446 char path
[PATH_MAX
];
4450 ret
= snprintf(path
, sizeof(path
), "%s/"
4451 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE
, rundir
);
4453 PERROR("snprintf julport path");
4458 * Create TCP JUL port file in rundir. Return value is of no importance.
4459 * The execution will continue even though we are not able to write the
4462 (void) utils_create_pid_file(jul_tcp_port
, path
);
4471 int main(int argc
, char **argv
)
4475 const char *home_path
, *env_app_timeout
;
4477 init_kernel_workarounds();
4479 rcu_register_thread();
4481 setup_consumerd_path();
4483 page_size
= sysconf(_SC_PAGESIZE
);
4484 if (page_size
< 0) {
4485 PERROR("sysconf _SC_PAGESIZE");
4486 page_size
= LONG_MAX
;
4487 WARN("Fallback page size to %ld", page_size
);
4490 /* Parse arguments */
4492 if ((ret
= parse_args(argc
, argv
)) < 0) {
4502 * child: setsid, close FD 0, 1, 2, chdir /
4503 * parent: exit (if fork is successful)
4511 * We are in the child. Make sure all other file
4512 * descriptors are closed, in case we are called with
4513 * more opened file descriptors than the standard ones.
4515 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
4520 /* Create thread quit pipe */
4521 if ((ret
= init_thread_quit_pipe()) < 0) {
4525 /* Check if daemon is UID = 0 */
4526 is_root
= !getuid();
4529 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
4531 /* Create global run dir with root access */
4532 ret
= create_lttng_rundir(rundir
);
4537 if (strlen(apps_unix_sock_path
) == 0) {
4538 snprintf(apps_unix_sock_path
, PATH_MAX
,
4539 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
4542 if (strlen(client_unix_sock_path
) == 0) {
4543 snprintf(client_unix_sock_path
, PATH_MAX
,
4544 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
4547 /* Set global SHM for ust */
4548 if (strlen(wait_shm_path
) == 0) {
4549 snprintf(wait_shm_path
, PATH_MAX
,
4550 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
4553 if (strlen(health_unix_sock_path
) == 0) {
4554 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4555 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
4558 /* Setup kernel consumerd path */
4559 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
4560 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
4561 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
4562 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
4564 DBG2("Kernel consumer err path: %s",
4565 kconsumer_data
.err_unix_sock_path
);
4566 DBG2("Kernel consumer cmd path: %s",
4567 kconsumer_data
.cmd_unix_sock_path
);
4569 home_path
= utils_get_home_dir();
4570 if (home_path
== NULL
) {
4571 /* TODO: Add --socket PATH option */
4572 ERR("Can't get HOME directory for sockets creation.");
4578 * Create rundir from home path. This will create something like
4581 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
4587 ret
= create_lttng_rundir(rundir
);
4592 if (strlen(apps_unix_sock_path
) == 0) {
4593 snprintf(apps_unix_sock_path
, PATH_MAX
,
4594 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
4597 /* Set the cli tool unix socket path */
4598 if (strlen(client_unix_sock_path
) == 0) {
4599 snprintf(client_unix_sock_path
, PATH_MAX
,
4600 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
4603 /* Set global SHM for ust */
4604 if (strlen(wait_shm_path
) == 0) {
4605 snprintf(wait_shm_path
, PATH_MAX
,
4606 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
4609 /* Set health check Unix path */
4610 if (strlen(health_unix_sock_path
) == 0) {
4611 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4612 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
4616 /* Set consumer initial state */
4617 kernel_consumerd_state
= CONSUMER_STOPPED
;
4618 ust_consumerd_state
= CONSUMER_STOPPED
;
4620 DBG("Client socket path %s", client_unix_sock_path
);
4621 DBG("Application socket path %s", apps_unix_sock_path
);
4622 DBG("Application wait path %s", wait_shm_path
);
4623 DBG("LTTng run directory path: %s", rundir
);
4625 /* 32 bits consumerd path setup */
4626 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
4627 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
4628 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
4629 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
4631 DBG2("UST consumer 32 bits err path: %s",
4632 ustconsumer32_data
.err_unix_sock_path
);
4633 DBG2("UST consumer 32 bits cmd path: %s",
4634 ustconsumer32_data
.cmd_unix_sock_path
);
4636 /* 64 bits consumerd path setup */
4637 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
4638 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
4639 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
4640 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
4642 DBG2("UST consumer 64 bits err path: %s",
4643 ustconsumer64_data
.err_unix_sock_path
);
4644 DBG2("UST consumer 64 bits cmd path: %s",
4645 ustconsumer64_data
.cmd_unix_sock_path
);
4648 * See if daemon already exist.
4650 if ((ret
= check_existing_daemon()) < 0) {
4651 ERR("Already running daemon.\n");
4653 * We do not goto exit because we must not cleanup()
4654 * because a daemon is already running.
4660 * Init UST app hash table. Alloc hash table before this point since
4661 * cleanup() can get called after that point.
4665 /* Initialize JUL domain subsystem. */
4666 if ((ret
= jul_init()) < 0) {
4667 /* ENOMEM at this point. */
4671 /* After this point, we can safely call cleanup() with "goto exit" */
4674 * These actions must be executed as root. We do that *after* setting up
4675 * the sockets path because we MUST make the check for another daemon using
4676 * those paths *before* trying to set the kernel consumer sockets and init
4680 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
4685 /* Setup kernel tracer */
4686 if (!opt_no_kernel
) {
4687 init_kernel_tracer();
4690 /* Set ulimit for open files */
4693 /* init lttng_fd tracking must be done after set_ulimit. */
4696 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
4701 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
4706 if ((ret
= set_signal_handler()) < 0) {
4710 /* Setup the needed unix socket */
4711 if ((ret
= init_daemon_socket()) < 0) {
4715 /* Set credentials to socket */
4716 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
4720 /* Get parent pid if -S, --sig-parent is specified. */
4721 if (opt_sig_parent
) {
4725 /* Setup the kernel pipe for waking up the kernel thread */
4726 if (is_root
&& !opt_no_kernel
) {
4727 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
4732 /* Setup the thread ht_cleanup communication pipe. */
4733 if (utils_create_pipe_cloexec(ht_cleanup_pipe
) < 0) {
4737 /* Setup the thread apps communication pipe. */
4738 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
4742 /* Setup the thread apps notify communication pipe. */
4743 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
) < 0) {
4747 /* Initialize global buffer per UID and PID registry. */
4748 buffer_reg_init_uid_registry();
4749 buffer_reg_init_pid_registry();
4751 /* Init UST command queue. */
4752 cds_wfq_init(&ust_cmd_queue
.queue
);
4755 * Get session list pointer. This pointer MUST NOT be free(). This list is
4756 * statically declared in session.c
4758 session_list_ptr
= session_get_list();
4760 /* Set up max poll set size */
4761 lttng_poll_set_max_size();
4765 /* Check for the application socket timeout env variable. */
4766 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
4767 if (env_app_timeout
) {
4768 app_socket_timeout
= atoi(env_app_timeout
);
4770 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
4776 /* Initialize communication library */
4778 /* This is to get the TCP timeout value. */
4779 lttcomm_inet_init();
4782 * Initialize the health check subsystem. This call should set the
4783 * appropriate time values.
4785 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
4786 if (!health_sessiond
) {
4787 PERROR("health_app_create error");
4788 goto exit_health_sessiond_cleanup
;
4791 /* Create thread to manage the client socket */
4792 ret
= pthread_create(&ht_cleanup_thread
, NULL
,
4793 thread_ht_cleanup
, (void *) NULL
);
4795 PERROR("pthread_create ht_cleanup");
4796 goto exit_ht_cleanup
;
4799 /* Create thread to manage the client socket */
4800 ret
= pthread_create(&health_thread
, NULL
,
4801 thread_manage_health
, (void *) NULL
);
4803 PERROR("pthread_create health");
4807 /* Create thread to manage the client socket */
4808 ret
= pthread_create(&client_thread
, NULL
,
4809 thread_manage_clients
, (void *) NULL
);
4811 PERROR("pthread_create clients");
4815 /* Create thread to dispatch registration */
4816 ret
= pthread_create(&dispatch_thread
, NULL
,
4817 thread_dispatch_ust_registration
, (void *) NULL
);
4819 PERROR("pthread_create dispatch");
4823 /* Create thread to manage application registration. */
4824 ret
= pthread_create(®_apps_thread
, NULL
,
4825 thread_registration_apps
, (void *) NULL
);
4827 PERROR("pthread_create registration");
4831 /* Create thread to manage application socket */
4832 ret
= pthread_create(&apps_thread
, NULL
,
4833 thread_manage_apps
, (void *) NULL
);
4835 PERROR("pthread_create apps");
4839 /* Create thread to manage application notify socket */
4840 ret
= pthread_create(&apps_notify_thread
, NULL
,
4841 ust_thread_manage_notify
, (void *) NULL
);
4843 PERROR("pthread_create apps");
4844 goto exit_apps_notify
;
4847 /* Create JUL registration thread. */
4848 ret
= pthread_create(&jul_reg_thread
, NULL
,
4849 jul_thread_manage_registration
, (void *) NULL
);
4851 PERROR("pthread_create apps");
4855 /* Don't start this thread if kernel tracing is not requested nor root */
4856 if (is_root
&& !opt_no_kernel
) {
4857 /* Create kernel thread to manage kernel event */
4858 ret
= pthread_create(&kernel_thread
, NULL
,
4859 thread_manage_kernel
, (void *) NULL
);
4861 PERROR("pthread_create kernel");
4865 ret
= pthread_join(kernel_thread
, &status
);
4867 PERROR("pthread_join");
4868 goto error
; /* join error, exit without cleanup */
4873 ret
= pthread_join(jul_reg_thread
, &status
);
4875 PERROR("pthread_join JUL");
4876 goto error
; /* join error, exit without cleanup */
4880 ret
= pthread_join(apps_notify_thread
, &status
);
4882 PERROR("pthread_join apps notify");
4883 goto error
; /* join error, exit without cleanup */
4887 ret
= pthread_join(apps_thread
, &status
);
4889 PERROR("pthread_join apps");
4890 goto error
; /* join error, exit without cleanup */
4895 ret
= pthread_join(reg_apps_thread
, &status
);
4897 PERROR("pthread_join");
4898 goto error
; /* join error, exit without cleanup */
4902 ret
= pthread_join(dispatch_thread
, &status
);
4904 PERROR("pthread_join");
4905 goto error
; /* join error, exit without cleanup */
4909 ret
= pthread_join(client_thread
, &status
);
4911 PERROR("pthread_join");
4912 goto error
; /* join error, exit without cleanup */
4915 ret
= join_consumer_thread(&kconsumer_data
);
4917 PERROR("join_consumer");
4918 goto error
; /* join error, exit without cleanup */
4921 ret
= join_consumer_thread(&ustconsumer32_data
);
4923 PERROR("join_consumer ust32");
4924 goto error
; /* join error, exit without cleanup */
4927 ret
= join_consumer_thread(&ustconsumer64_data
);
4929 PERROR("join_consumer ust64");
4930 goto error
; /* join error, exit without cleanup */
4934 ret
= pthread_join(health_thread
, &status
);
4936 PERROR("pthread_join health thread");
4937 goto error
; /* join error, exit without cleanup */
4941 ret
= pthread_join(ht_cleanup_thread
, &status
);
4943 PERROR("pthread_join ht cleanup thread");
4944 goto error
; /* join error, exit without cleanup */
4947 health_app_destroy(health_sessiond
);
4948 exit_health_sessiond_cleanup
:
4951 * cleanup() is called when no other thread is running.
4953 rcu_thread_online();
4955 rcu_thread_offline();
4956 rcu_unregister_thread();