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"
67 #define CONSUMERD_FILE "lttng-consumerd"
70 static const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
71 static const char *opt_pidfile
;
72 static int opt_sig_parent
;
73 static int opt_verbose_consumer
;
74 static int opt_daemon
;
75 static int opt_no_kernel
;
76 static int is_root
; /* Set to 1 if the daemon is running as root */
77 static pid_t ppid
; /* Parent PID for --sig-parent option */
81 * Consumer daemon specific control data. Every value not initialized here is
82 * set to 0 by the static definition.
84 static struct consumer_data kconsumer_data
= {
85 .type
= LTTNG_CONSUMER_KERNEL
,
86 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
87 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
90 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
91 .lock
= PTHREAD_MUTEX_INITIALIZER
,
92 .cond
= PTHREAD_COND_INITIALIZER
,
93 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
95 static struct consumer_data ustconsumer64_data
= {
96 .type
= LTTNG_CONSUMER64_UST
,
97 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
98 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
101 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
102 .lock
= PTHREAD_MUTEX_INITIALIZER
,
103 .cond
= PTHREAD_COND_INITIALIZER
,
104 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
106 static struct consumer_data ustconsumer32_data
= {
107 .type
= LTTNG_CONSUMER32_UST
,
108 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
109 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
112 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
113 .lock
= PTHREAD_MUTEX_INITIALIZER
,
114 .cond
= PTHREAD_COND_INITIALIZER
,
115 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
118 /* Shared between threads */
119 static int dispatch_thread_exit
;
121 /* Global application Unix socket path */
122 static char apps_unix_sock_path
[PATH_MAX
];
123 /* Global client Unix socket path */
124 static char client_unix_sock_path
[PATH_MAX
];
125 /* global wait shm path for UST */
126 static char wait_shm_path
[PATH_MAX
];
127 /* Global health check unix path */
128 static char health_unix_sock_path
[PATH_MAX
];
130 /* Sockets and FDs */
131 static int client_sock
= -1;
132 static int apps_sock
= -1;
133 int kernel_tracer_fd
= -1;
134 static int kernel_poll_pipe
[2] = { -1, -1 };
137 * Quit pipe for all threads. This permits a single cancellation point
138 * for all threads when receiving an event on the pipe.
140 static int thread_quit_pipe
[2] = { -1, -1 };
143 * This pipe is used to inform the thread managing application communication
144 * that a command is queued and ready to be processed.
146 static int apps_cmd_pipe
[2] = { -1, -1 };
148 int apps_cmd_notify_pipe
[2] = { -1, -1 };
150 /* Pthread, Mutexes and Semaphores */
151 static pthread_t apps_thread
;
152 static pthread_t apps_notify_thread
;
153 static pthread_t reg_apps_thread
;
154 static pthread_t client_thread
;
155 static pthread_t kernel_thread
;
156 static pthread_t dispatch_thread
;
157 static pthread_t health_thread
;
158 static pthread_t ht_cleanup_thread
;
161 * UST registration command queue. This queue is tied with a futex and uses a N
162 * wakers / 1 waiter implemented and detailed in futex.c/.h
164 * The thread_manage_apps and thread_dispatch_ust_registration interact with
165 * this queue and the wait/wake scheme.
167 static struct ust_cmd_queue ust_cmd_queue
;
170 * Pointer initialized before thread creation.
172 * This points to the tracing session list containing the session count and a
173 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
174 * MUST NOT be taken if you call a public function in session.c.
176 * The lock is nested inside the structure: session_list_ptr->lock. Please use
177 * session_lock_list and session_unlock_list for lock acquisition.
179 static struct ltt_session_list
*session_list_ptr
;
181 int ust_consumerd64_fd
= -1;
182 int ust_consumerd32_fd
= -1;
184 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
185 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
186 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
187 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
189 static const char *module_proc_lttng
= "/proc/lttng";
192 * Consumer daemon state which is changed when spawning it, killing it or in
193 * case of a fatal error.
195 enum consumerd_state
{
196 CONSUMER_STARTED
= 1,
197 CONSUMER_STOPPED
= 2,
202 * This consumer daemon state is used to validate if a client command will be
203 * able to reach the consumer. If not, the client is informed. For instance,
204 * doing a "lttng start" when the consumer state is set to ERROR will return an
205 * error to the client.
207 * The following example shows a possible race condition of this scheme:
209 * consumer thread error happens
211 * client cmd checks state -> still OK
212 * consumer thread exit, sets error
213 * client cmd try to talk to consumer
216 * However, since the consumer is a different daemon, we have no way of making
217 * sure the command will reach it safely even with this state flag. This is why
218 * we consider that up to the state validation during command processing, the
219 * command is safe. After that, we can not guarantee the correctness of the
220 * client request vis-a-vis the consumer.
222 static enum consumerd_state ust_consumerd_state
;
223 static enum consumerd_state kernel_consumerd_state
;
226 * Socket timeout for receiving and sending in seconds.
228 static int app_socket_timeout
;
230 /* Set in main() with the current page size. */
233 /* Application health monitoring */
234 struct health_app
*health_sessiond
;
237 void setup_consumerd_path(void)
239 const char *bin
, *libdir
;
242 * Allow INSTALL_BIN_PATH to be used as a target path for the
243 * native architecture size consumer if CONFIG_CONSUMER*_PATH
244 * has not been defined.
246 #if (CAA_BITS_PER_LONG == 32)
247 if (!consumerd32_bin
[0]) {
248 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
250 if (!consumerd32_libdir
[0]) {
251 consumerd32_libdir
= INSTALL_LIB_PATH
;
253 #elif (CAA_BITS_PER_LONG == 64)
254 if (!consumerd64_bin
[0]) {
255 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
257 if (!consumerd64_libdir
[0]) {
258 consumerd64_libdir
= INSTALL_LIB_PATH
;
261 #error "Unknown bitness"
265 * runtime env. var. overrides the build default.
267 bin
= getenv("LTTNG_CONSUMERD32_BIN");
269 consumerd32_bin
= bin
;
271 bin
= getenv("LTTNG_CONSUMERD64_BIN");
273 consumerd64_bin
= bin
;
275 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
277 consumerd32_libdir
= libdir
;
279 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
281 consumerd64_libdir
= libdir
;
286 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
288 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
294 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
300 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
312 * Check if the thread quit pipe was triggered.
314 * Return 1 if it was triggered else 0;
316 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
318 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
326 * Init thread quit pipe.
328 * Return -1 on error or 0 if all pipes are created.
330 static int init_thread_quit_pipe(void)
334 ret
= pipe(thread_quit_pipe
);
336 PERROR("thread quit pipe");
340 for (i
= 0; i
< 2; i
++) {
341 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
353 * Stop all threads by closing the thread quit pipe.
355 static void stop_threads(void)
359 /* Stopping all threads */
360 DBG("Terminating all threads");
361 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
363 ERR("write error on thread quit pipe");
366 /* Dispatch thread */
367 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
368 futex_nto1_wake(&ust_cmd_queue
.futex
);
372 * Close every consumer sockets.
374 static void close_consumer_sockets(void)
378 if (kconsumer_data
.err_sock
>= 0) {
379 ret
= close(kconsumer_data
.err_sock
);
381 PERROR("kernel consumer err_sock close");
384 if (ustconsumer32_data
.err_sock
>= 0) {
385 ret
= close(ustconsumer32_data
.err_sock
);
387 PERROR("UST consumerd32 err_sock close");
390 if (ustconsumer64_data
.err_sock
>= 0) {
391 ret
= close(ustconsumer64_data
.err_sock
);
393 PERROR("UST consumerd64 err_sock close");
396 if (kconsumer_data
.cmd_sock
>= 0) {
397 ret
= close(kconsumer_data
.cmd_sock
);
399 PERROR("kernel consumer cmd_sock close");
402 if (ustconsumer32_data
.cmd_sock
>= 0) {
403 ret
= close(ustconsumer32_data
.cmd_sock
);
405 PERROR("UST consumerd32 cmd_sock close");
408 if (ustconsumer64_data
.cmd_sock
>= 0) {
409 ret
= close(ustconsumer64_data
.cmd_sock
);
411 PERROR("UST consumerd64 cmd_sock close");
419 static void cleanup(void)
423 struct ltt_session
*sess
, *stmp
;
428 * Close the thread quit pipe. It has already done its job,
429 * since we are now called.
431 utils_close_pipe(thread_quit_pipe
);
434 * If opt_pidfile is undefined, the default file will be wiped when
435 * removing the rundir.
438 ret
= remove(opt_pidfile
);
440 PERROR("remove pidfile %s", opt_pidfile
);
444 DBG("Removing %s directory", rundir
);
445 ret
= asprintf(&cmd
, "rm -rf %s", rundir
);
447 ERR("asprintf failed. Something is really wrong!");
450 /* Remove lttng run directory */
453 ERR("Unable to clean %s", rundir
);
458 DBG("Cleaning up all sessions");
460 /* Destroy session list mutex */
461 if (session_list_ptr
!= NULL
) {
462 pthread_mutex_destroy(&session_list_ptr
->lock
);
464 /* Cleanup ALL session */
465 cds_list_for_each_entry_safe(sess
, stmp
,
466 &session_list_ptr
->head
, list
) {
467 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
471 DBG("Closing all UST sockets");
472 ust_app_clean_list();
473 buffer_reg_destroy_registries();
475 if (is_root
&& !opt_no_kernel
) {
476 DBG2("Closing kernel fd");
477 if (kernel_tracer_fd
>= 0) {
478 ret
= close(kernel_tracer_fd
);
483 DBG("Unloading kernel modules");
484 modprobe_remove_lttng_all();
487 close_consumer_sockets();
490 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
491 "Matthew, BEET driven development works!%c[%dm",
492 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
497 * Send data on a unix socket using the liblttsessiondcomm API.
499 * Return lttcomm error code.
501 static int send_unix_sock(int sock
, void *buf
, size_t len
)
503 /* Check valid length */
508 return lttcomm_send_unix_sock(sock
, buf
, len
);
512 * Free memory of a command context structure.
514 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
516 DBG("Clean command context structure");
518 if ((*cmd_ctx
)->llm
) {
519 free((*cmd_ctx
)->llm
);
521 if ((*cmd_ctx
)->lsm
) {
522 free((*cmd_ctx
)->lsm
);
530 * Notify UST applications using the shm mmap futex.
532 static int notify_ust_apps(int active
)
536 DBG("Notifying applications of session daemon state: %d", active
);
538 /* See shm.c for this call implying mmap, shm and futex calls */
539 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
540 if (wait_shm_mmap
== NULL
) {
544 /* Wake waiting process */
545 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
547 /* Apps notified successfully */
555 * Setup the outgoing data buffer for the response (llm) by allocating the
556 * right amount of memory and copying the original information from the lsm
559 * Return total size of the buffer pointed by buf.
561 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
567 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
568 if (cmd_ctx
->llm
== NULL
) {
574 /* Copy common data */
575 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
576 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
578 cmd_ctx
->llm
->data_size
= size
;
579 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
588 * Update the kernel poll set of all channel fd available over all tracing
589 * session. Add the wakeup pipe at the end of the set.
591 static int update_kernel_poll(struct lttng_poll_event
*events
)
594 struct ltt_session
*session
;
595 struct ltt_kernel_channel
*channel
;
597 DBG("Updating kernel poll set");
600 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
601 session_lock(session
);
602 if (session
->kernel_session
== NULL
) {
603 session_unlock(session
);
607 cds_list_for_each_entry(channel
,
608 &session
->kernel_session
->channel_list
.head
, list
) {
609 /* Add channel fd to the kernel poll set */
610 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
612 session_unlock(session
);
615 DBG("Channel fd %d added to kernel set", channel
->fd
);
617 session_unlock(session
);
619 session_unlock_list();
624 session_unlock_list();
629 * Find the channel fd from 'fd' over all tracing session. When found, check
630 * for new channel stream and send those stream fds to the kernel consumer.
632 * Useful for CPU hotplug feature.
634 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
637 struct ltt_session
*session
;
638 struct ltt_kernel_session
*ksess
;
639 struct ltt_kernel_channel
*channel
;
641 DBG("Updating kernel streams for channel fd %d", fd
);
644 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
645 session_lock(session
);
646 if (session
->kernel_session
== NULL
) {
647 session_unlock(session
);
650 ksess
= session
->kernel_session
;
652 cds_list_for_each_entry(channel
, &ksess
->channel_list
.head
, list
) {
653 if (channel
->fd
== fd
) {
654 DBG("Channel found, updating kernel streams");
655 ret
= kernel_open_channel_stream(channel
);
659 /* Update the stream global counter */
660 ksess
->stream_count_global
+= ret
;
663 * Have we already sent fds to the consumer? If yes, it means
664 * that tracing is started so it is safe to send our updated
667 if (ksess
->consumer_fds_sent
== 1 && ksess
->consumer
!= NULL
) {
668 struct lttng_ht_iter iter
;
669 struct consumer_socket
*socket
;
672 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
673 &iter
.iter
, socket
, node
.node
) {
674 pthread_mutex_lock(socket
->lock
);
675 ret
= kernel_consumer_send_channel_stream(socket
,
677 session
->output_traces
? 1 : 0);
678 pthread_mutex_unlock(socket
->lock
);
689 session_unlock(session
);
691 session_unlock_list();
695 session_unlock(session
);
696 session_unlock_list();
701 * For each tracing session, update newly registered apps. The session list
702 * lock MUST be acquired before calling this.
704 static void update_ust_app(int app_sock
)
706 struct ltt_session
*sess
, *stmp
;
708 /* Consumer is in an ERROR state. Stop any application update. */
709 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
710 /* Stop the update process since the consumer is dead. */
714 /* For all tracing session(s) */
715 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
717 if (sess
->ust_session
) {
718 ust_app_global_update(sess
->ust_session
, app_sock
);
720 session_unlock(sess
);
725 * This thread manage event coming from the kernel.
727 * Features supported in this thread:
730 static void *thread_manage_kernel(void *data
)
732 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
733 uint32_t revents
, nb_fd
;
735 struct lttng_poll_event events
;
737 DBG("[thread] Thread manage kernel started");
739 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
742 * This first step of the while is to clean this structure which could free
743 * non NULL pointers so initialize it before the loop.
745 lttng_poll_init(&events
);
747 if (testpoint(thread_manage_kernel
)) {
748 goto error_testpoint
;
751 health_code_update();
753 if (testpoint(thread_manage_kernel_before_loop
)) {
754 goto error_testpoint
;
758 health_code_update();
760 if (update_poll_flag
== 1) {
761 /* Clean events object. We are about to populate it again. */
762 lttng_poll_clean(&events
);
764 ret
= sessiond_set_thread_pollset(&events
, 2);
766 goto error_poll_create
;
769 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
774 /* This will add the available kernel channel if any. */
775 ret
= update_kernel_poll(&events
);
779 update_poll_flag
= 0;
782 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events
));
784 /* Poll infinite value of time */
787 ret
= lttng_poll_wait(&events
, -1);
791 * Restart interrupted system call.
793 if (errno
== EINTR
) {
797 } else if (ret
== 0) {
798 /* Should not happen since timeout is infinite */
799 ERR("Return value of poll is 0 with an infinite timeout.\n"
800 "This should not have happened! Continuing...");
806 for (i
= 0; i
< nb_fd
; i
++) {
807 /* Fetch once the poll data */
808 revents
= LTTNG_POLL_GETEV(&events
, i
);
809 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
811 health_code_update();
813 /* Thread quit pipe has been closed. Killing thread. */
814 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
820 /* Check for data on kernel pipe */
821 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
823 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
824 } while (ret
< 0 && errno
== EINTR
);
826 * Ret value is useless here, if this pipe gets any actions an
827 * update is required anyway.
829 update_poll_flag
= 1;
833 * New CPU detected by the kernel. Adding kernel stream to
834 * kernel session and updating the kernel consumer
836 if (revents
& LPOLLIN
) {
837 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
843 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
844 * and unregister kernel stream at this point.
853 lttng_poll_clean(&events
);
856 utils_close_pipe(kernel_poll_pipe
);
857 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
860 ERR("Health error occurred in %s", __func__
);
861 WARN("Kernel thread died unexpectedly. "
862 "Kernel tracing can continue but CPU hotplug is disabled.");
864 health_unregister(health_sessiond
);
865 DBG("Kernel thread dying");
870 * Signal pthread condition of the consumer data that the thread.
872 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
874 pthread_mutex_lock(&data
->cond_mutex
);
877 * The state is set before signaling. It can be any value, it's the waiter
878 * job to correctly interpret this condition variable associated to the
879 * consumer pthread_cond.
881 * A value of 0 means that the corresponding thread of the consumer data
882 * was not started. 1 indicates that the thread has started and is ready
883 * for action. A negative value means that there was an error during the
886 data
->consumer_thread_is_ready
= state
;
887 (void) pthread_cond_signal(&data
->cond
);
889 pthread_mutex_unlock(&data
->cond_mutex
);
893 * This thread manage the consumer error sent back to the session daemon.
895 static void *thread_manage_consumer(void *data
)
897 int sock
= -1, i
, ret
, pollfd
, err
= -1;
898 uint32_t revents
, nb_fd
;
899 enum lttcomm_return_code code
;
900 struct lttng_poll_event events
;
901 struct consumer_data
*consumer_data
= data
;
903 DBG("[thread] Manage consumer started");
905 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
907 health_code_update();
910 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
911 * metadata_sock. Nothing more will be added to this poll set.
913 ret
= sessiond_set_thread_pollset(&events
, 3);
919 * The error socket here is already in a listening state which was done
920 * just before spawning this thread to avoid a race between the consumer
921 * daemon exec trying to connect and the listen() call.
923 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
928 health_code_update();
930 /* Infinite blocking call, waiting for transmission */
934 if (testpoint(thread_manage_consumer
)) {
938 ret
= lttng_poll_wait(&events
, -1);
942 * Restart interrupted system call.
944 if (errno
== EINTR
) {
952 for (i
= 0; i
< nb_fd
; i
++) {
953 /* Fetch once the poll data */
954 revents
= LTTNG_POLL_GETEV(&events
, i
);
955 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
957 health_code_update();
959 /* Thread quit pipe has been closed. Killing thread. */
960 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
966 /* Event on the registration socket */
967 if (pollfd
== consumer_data
->err_sock
) {
968 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
969 ERR("consumer err socket poll error");
975 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
981 * Set the CLOEXEC flag. Return code is useless because either way, the
984 (void) utils_set_fd_cloexec(sock
);
986 health_code_update();
988 DBG2("Receiving code from consumer err_sock");
990 /* Getting status code from kconsumerd */
991 ret
= lttcomm_recv_unix_sock(sock
, &code
,
992 sizeof(enum lttcomm_return_code
));
997 health_code_update();
999 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1000 /* Connect both socket, command and metadata. */
1001 consumer_data
->cmd_sock
=
1002 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1003 consumer_data
->metadata_fd
=
1004 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1005 if (consumer_data
->cmd_sock
< 0
1006 || consumer_data
->metadata_fd
< 0) {
1007 PERROR("consumer connect cmd socket");
1008 /* On error, signal condition and quit. */
1009 signal_consumer_condition(consumer_data
, -1);
1012 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1013 /* Create metadata socket lock. */
1014 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1015 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1016 PERROR("zmalloc pthread mutex");
1020 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1022 signal_consumer_condition(consumer_data
, 1);
1023 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1024 DBG("Consumer metadata socket ready (fd: %d)",
1025 consumer_data
->metadata_fd
);
1027 ERR("consumer error when waiting for SOCK_READY : %s",
1028 lttcomm_get_readable_code(-code
));
1032 /* Remove the consumerd error sock since we've established a connexion */
1033 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1038 /* Add new accepted error socket. */
1039 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1044 /* Add metadata socket that is successfully connected. */
1045 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1046 LPOLLIN
| LPOLLRDHUP
);
1051 health_code_update();
1053 /* Infinite blocking call, waiting for transmission */
1056 health_poll_entry();
1057 ret
= lttng_poll_wait(&events
, -1);
1061 * Restart interrupted system call.
1063 if (errno
== EINTR
) {
1071 for (i
= 0; i
< nb_fd
; i
++) {
1072 /* Fetch once the poll data */
1073 revents
= LTTNG_POLL_GETEV(&events
, i
);
1074 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1076 health_code_update();
1078 /* Thread quit pipe has been closed. Killing thread. */
1079 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1085 if (pollfd
== sock
) {
1086 /* Event on the consumerd socket */
1087 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1088 ERR("consumer err socket second poll error");
1091 health_code_update();
1092 /* Wait for any kconsumerd error */
1093 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1094 sizeof(enum lttcomm_return_code
));
1096 ERR("consumer closed the command socket");
1100 ERR("consumer return code : %s",
1101 lttcomm_get_readable_code(-code
));
1104 } else if (pollfd
== consumer_data
->metadata_fd
) {
1105 /* UST metadata requests */
1106 ret
= ust_consumer_metadata_request(
1107 &consumer_data
->metadata_sock
);
1109 ERR("Handling metadata request");
1114 ERR("Unknown pollfd");
1118 health_code_update();
1124 * We lock here because we are about to close the sockets and some other
1125 * thread might be using them so get exclusive access which will abort all
1126 * other consumer command by other threads.
1128 pthread_mutex_lock(&consumer_data
->lock
);
1130 /* Immediately set the consumerd state to stopped */
1131 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1132 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1133 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1134 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1135 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1137 /* Code flow error... */
1141 if (consumer_data
->err_sock
>= 0) {
1142 ret
= close(consumer_data
->err_sock
);
1146 consumer_data
->err_sock
= -1;
1148 if (consumer_data
->cmd_sock
>= 0) {
1149 ret
= close(consumer_data
->cmd_sock
);
1153 consumer_data
->cmd_sock
= -1;
1155 if (*consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1156 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1169 unlink(consumer_data
->err_unix_sock_path
);
1170 unlink(consumer_data
->cmd_unix_sock_path
);
1171 consumer_data
->pid
= 0;
1172 pthread_mutex_unlock(&consumer_data
->lock
);
1174 /* Cleanup metadata socket mutex. */
1175 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1176 free(consumer_data
->metadata_sock
.lock
);
1178 lttng_poll_clean(&events
);
1182 ERR("Health error occurred in %s", __func__
);
1184 health_unregister(health_sessiond
);
1185 DBG("consumer thread cleanup completed");
1191 * This thread manage application communication.
1193 static void *thread_manage_apps(void *data
)
1195 int i
, ret
, pollfd
, err
= -1;
1196 uint32_t revents
, nb_fd
;
1197 struct lttng_poll_event events
;
1199 DBG("[thread] Manage application started");
1201 rcu_register_thread();
1202 rcu_thread_online();
1204 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1206 if (testpoint(thread_manage_apps
)) {
1207 goto error_testpoint
;
1210 health_code_update();
1212 ret
= sessiond_set_thread_pollset(&events
, 2);
1214 goto error_poll_create
;
1217 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1222 if (testpoint(thread_manage_apps_before_loop
)) {
1226 health_code_update();
1229 DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events
));
1231 /* Inifinite blocking call, waiting for transmission */
1233 health_poll_entry();
1234 ret
= lttng_poll_wait(&events
, -1);
1238 * Restart interrupted system call.
1240 if (errno
== EINTR
) {
1248 for (i
= 0; i
< nb_fd
; i
++) {
1249 /* Fetch once the poll data */
1250 revents
= LTTNG_POLL_GETEV(&events
, i
);
1251 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1253 health_code_update();
1255 /* Thread quit pipe has been closed. Killing thread. */
1256 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1262 /* Inspect the apps cmd pipe */
1263 if (pollfd
== apps_cmd_pipe
[0]) {
1264 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1265 ERR("Apps command pipe error");
1267 } else if (revents
& LPOLLIN
) {
1272 ret
= read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1273 } while (ret
< 0 && errno
== EINTR
);
1274 if (ret
< 0 || ret
< sizeof(sock
)) {
1275 PERROR("read apps cmd pipe");
1279 health_code_update();
1282 * We only monitor the error events of the socket. This
1283 * thread does not handle any incoming data from UST
1286 ret
= lttng_poll_add(&events
, sock
,
1287 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1293 * Set socket timeout for both receiving and ending.
1294 * app_socket_timeout is in seconds, whereas
1295 * lttcomm_setsockopt_rcv_timeout and
1296 * lttcomm_setsockopt_snd_timeout expect msec as
1299 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1300 app_socket_timeout
* 1000);
1301 (void) lttcomm_setsockopt_snd_timeout(sock
,
1302 app_socket_timeout
* 1000);
1304 DBG("Apps with sock %d added to poll set", sock
);
1306 health_code_update();
1312 * At this point, we know that a registered application made
1313 * the event at poll_wait.
1315 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1316 /* Removing from the poll set */
1317 ret
= lttng_poll_del(&events
, pollfd
);
1322 /* Socket closed on remote end. */
1323 ust_app_unregister(pollfd
);
1328 health_code_update();
1334 lttng_poll_clean(&events
);
1337 utils_close_pipe(apps_cmd_pipe
);
1338 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1341 * We don't clean the UST app hash table here since already registered
1342 * applications can still be controlled so let them be until the session
1343 * daemon dies or the applications stop.
1348 ERR("Health error occurred in %s", __func__
);
1350 health_unregister(health_sessiond
);
1351 DBG("Application communication apps thread cleanup complete");
1352 rcu_thread_offline();
1353 rcu_unregister_thread();
1358 * Send a socket to a thread This is called from the dispatch UST registration
1359 * thread once all sockets are set for the application.
1361 * The sock value can be invalid, we don't really care, the thread will handle
1362 * it and make the necessary cleanup if so.
1364 * On success, return 0 else a negative value being the errno message of the
1367 static int send_socket_to_thread(int fd
, int sock
)
1372 * It's possible that the FD is set as invalid with -1 concurrently just
1373 * before calling this function being a shutdown state of the thread.
1381 ret
= write(fd
, &sock
, sizeof(sock
));
1382 } while (ret
< 0 && errno
== EINTR
);
1383 if (ret
< 0 || ret
!= sizeof(sock
)) {
1384 PERROR("write apps pipe %d", fd
);
1391 /* All good. Don't send back the write positive ret value. */
1398 * Sanitize the wait queue of the dispatch registration thread meaning removing
1399 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1400 * notify socket is never received.
1402 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1404 int ret
, nb_fd
= 0, i
;
1405 unsigned int fd_added
= 0;
1406 struct lttng_poll_event events
;
1407 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1411 lttng_poll_init(&events
);
1413 /* Just skip everything for an empty queue. */
1414 if (!wait_queue
->count
) {
1418 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1423 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1424 &wait_queue
->head
, head
) {
1425 assert(wait_node
->app
);
1426 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1427 LPOLLHUP
| LPOLLERR
);
1440 * Poll but don't block so we can quickly identify the faulty events and
1441 * clean them afterwards from the wait queue.
1443 ret
= lttng_poll_wait(&events
, 0);
1449 for (i
= 0; i
< nb_fd
; i
++) {
1450 /* Get faulty FD. */
1451 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1452 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1454 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1455 &wait_queue
->head
, head
) {
1456 if (pollfd
== wait_node
->app
->sock
&&
1457 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1458 cds_list_del(&wait_node
->head
);
1459 wait_queue
->count
--;
1460 ust_app_destroy(wait_node
->app
);
1468 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1472 lttng_poll_clean(&events
);
1476 lttng_poll_clean(&events
);
1478 ERR("Unable to sanitize wait queue");
1483 * Dispatch request from the registration threads to the application
1484 * communication thread.
1486 static void *thread_dispatch_ust_registration(void *data
)
1489 struct cds_wfq_node
*node
;
1490 struct ust_command
*ust_cmd
= NULL
;
1491 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1492 struct ust_reg_wait_queue wait_queue
= {
1496 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1498 health_code_update();
1500 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1502 DBG("[thread] Dispatch UST command started");
1504 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1505 health_code_update();
1507 /* Atomically prepare the queue futex */
1508 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1511 struct ust_app
*app
= NULL
;
1515 * Make sure we don't have node(s) that have hung up before receiving
1516 * the notify socket. This is to clean the list in order to avoid
1517 * memory leaks from notify socket that are never seen.
1519 sanitize_wait_queue(&wait_queue
);
1521 health_code_update();
1522 /* Dequeue command for registration */
1523 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1525 DBG("Woken up but nothing in the UST command queue");
1526 /* Continue thread execution */
1530 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1532 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1533 " gid:%d sock:%d name:%s (version %d.%d)",
1534 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1535 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1536 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1537 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1539 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1540 wait_node
= zmalloc(sizeof(*wait_node
));
1542 PERROR("zmalloc wait_node dispatch");
1543 ret
= close(ust_cmd
->sock
);
1545 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1547 lttng_fd_put(1, LTTNG_FD_APPS
);
1551 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1553 /* Create application object if socket is CMD. */
1554 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1556 if (!wait_node
->app
) {
1557 ret
= close(ust_cmd
->sock
);
1559 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1561 lttng_fd_put(1, LTTNG_FD_APPS
);
1567 * Add application to the wait queue so we can set the notify
1568 * socket before putting this object in the global ht.
1570 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1575 * We have to continue here since we don't have the notify
1576 * socket and the application MUST be added to the hash table
1577 * only at that moment.
1582 * Look for the application in the local wait queue and set the
1583 * notify socket if found.
1585 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1586 &wait_queue
.head
, head
) {
1587 health_code_update();
1588 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1589 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1590 cds_list_del(&wait_node
->head
);
1592 app
= wait_node
->app
;
1594 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1600 * With no application at this stage the received socket is
1601 * basically useless so close it before we free the cmd data
1602 * structure for good.
1605 ret
= close(ust_cmd
->sock
);
1607 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1609 lttng_fd_put(1, LTTNG_FD_APPS
);
1616 * @session_lock_list
1618 * Lock the global session list so from the register up to the
1619 * registration done message, no thread can see the application
1620 * and change its state.
1622 session_lock_list();
1626 * Add application to the global hash table. This needs to be
1627 * done before the update to the UST registry can locate the
1632 /* Set app version. This call will print an error if needed. */
1633 (void) ust_app_version(app
);
1635 /* Send notify socket through the notify pipe. */
1636 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1640 session_unlock_list();
1642 * No notify thread, stop the UST tracing. However, this is
1643 * not an internal error of the this thread thus setting
1644 * the health error code to a normal exit.
1651 * Update newly registered application with the tracing
1652 * registry info already enabled information.
1654 update_ust_app(app
->sock
);
1657 * Don't care about return value. Let the manage apps threads
1658 * handle app unregistration upon socket close.
1660 (void) ust_app_register_done(app
->sock
);
1663 * Even if the application socket has been closed, send the app
1664 * to the thread and unregistration will take place at that
1667 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1670 session_unlock_list();
1672 * No apps. thread, stop the UST tracing. However, this is
1673 * not an internal error of the this thread thus setting
1674 * the health error code to a normal exit.
1681 session_unlock_list();
1683 } while (node
!= NULL
);
1685 health_poll_entry();
1686 /* Futex wait on queue. Blocking call on futex() */
1687 futex_nto1_wait(&ust_cmd_queue
.futex
);
1690 /* Normal exit, no error */
1694 /* Clean up wait queue. */
1695 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1696 &wait_queue
.head
, head
) {
1697 cds_list_del(&wait_node
->head
);
1702 DBG("Dispatch thread dying");
1705 ERR("Health error occurred in %s", __func__
);
1707 health_unregister(health_sessiond
);
1712 * This thread manage application registration.
1714 static void *thread_registration_apps(void *data
)
1716 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1717 uint32_t revents
, nb_fd
;
1718 struct lttng_poll_event events
;
1720 * Get allocated in this thread, enqueued to a global queue, dequeued and
1721 * freed in the manage apps thread.
1723 struct ust_command
*ust_cmd
= NULL
;
1725 DBG("[thread] Manage application registration started");
1727 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
1729 if (testpoint(thread_registration_apps
)) {
1730 goto error_testpoint
;
1733 ret
= lttcomm_listen_unix_sock(apps_sock
);
1739 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1740 * more will be added to this poll set.
1742 ret
= sessiond_set_thread_pollset(&events
, 2);
1744 goto error_create_poll
;
1747 /* Add the application registration socket */
1748 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1750 goto error_poll_add
;
1753 /* Notify all applications to register */
1754 ret
= notify_ust_apps(1);
1756 ERR("Failed to notify applications or create the wait shared memory.\n"
1757 "Execution continues but there might be problem for already\n"
1758 "running applications that wishes to register.");
1762 DBG("Accepting application registration");
1764 /* Inifinite blocking call, waiting for transmission */
1766 health_poll_entry();
1767 ret
= lttng_poll_wait(&events
, -1);
1771 * Restart interrupted system call.
1773 if (errno
== EINTR
) {
1781 for (i
= 0; i
< nb_fd
; i
++) {
1782 health_code_update();
1784 /* Fetch once the poll data */
1785 revents
= LTTNG_POLL_GETEV(&events
, i
);
1786 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1788 /* Thread quit pipe has been closed. Killing thread. */
1789 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1795 /* Event on the registration socket */
1796 if (pollfd
== apps_sock
) {
1797 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1798 ERR("Register apps socket poll error");
1800 } else if (revents
& LPOLLIN
) {
1801 sock
= lttcomm_accept_unix_sock(apps_sock
);
1807 * Set the CLOEXEC flag. Return code is useless because
1808 * either way, the show must go on.
1810 (void) utils_set_fd_cloexec(sock
);
1812 /* Create UST registration command for enqueuing */
1813 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1814 if (ust_cmd
== NULL
) {
1815 PERROR("ust command zmalloc");
1820 * Using message-based transmissions to ensure we don't
1821 * have to deal with partially received messages.
1823 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1825 ERR("Exhausted file descriptors allowed for applications.");
1835 health_code_update();
1836 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
1839 /* Close socket of the application. */
1844 lttng_fd_put(LTTNG_FD_APPS
, 1);
1848 health_code_update();
1850 ust_cmd
->sock
= sock
;
1853 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1854 " gid:%d sock:%d name:%s (version %d.%d)",
1855 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1856 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1857 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1858 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1861 * Lock free enqueue the registration request. The red pill
1862 * has been taken! This apps will be part of the *system*.
1864 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1867 * Wake the registration queue futex. Implicit memory
1868 * barrier with the exchange in cds_wfq_enqueue.
1870 futex_nto1_wake(&ust_cmd_queue
.futex
);
1880 ERR("Health error occurred in %s", __func__
);
1883 /* Notify that the registration thread is gone */
1886 if (apps_sock
>= 0) {
1887 ret
= close(apps_sock
);
1897 lttng_fd_put(LTTNG_FD_APPS
, 1);
1899 unlink(apps_unix_sock_path
);
1902 lttng_poll_clean(&events
);
1906 DBG("UST Registration thread cleanup complete");
1907 health_unregister(health_sessiond
);
1913 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1914 * exec or it will fails.
1916 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
1919 struct timespec timeout
;
1921 /* Make sure we set the readiness flag to 0 because we are NOT ready */
1922 consumer_data
->consumer_thread_is_ready
= 0;
1924 /* Setup pthread condition */
1925 ret
= pthread_condattr_init(&consumer_data
->condattr
);
1928 PERROR("pthread_condattr_init consumer data");
1933 * Set the monotonic clock in order to make sure we DO NOT jump in time
1934 * between the clock_gettime() call and the timedwait call. See bug #324
1935 * for a more details and how we noticed it.
1937 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
1940 PERROR("pthread_condattr_setclock consumer data");
1944 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
1947 PERROR("pthread_cond_init consumer data");
1951 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
1954 PERROR("pthread_create consumer");
1959 /* We are about to wait on a pthread condition */
1960 pthread_mutex_lock(&consumer_data
->cond_mutex
);
1962 /* Get time for sem_timedwait absolute timeout */
1963 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
1965 * Set the timeout for the condition timed wait even if the clock gettime
1966 * call fails since we might loop on that call and we want to avoid to
1967 * increment the timeout too many times.
1969 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
1972 * The following loop COULD be skipped in some conditions so this is why we
1973 * set ret to 0 in order to make sure at least one round of the loop is
1979 * Loop until the condition is reached or when a timeout is reached. Note
1980 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
1981 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
1982 * possible. This loop does not take any chances and works with both of
1985 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
1986 if (clock_ret
< 0) {
1987 PERROR("clock_gettime spawn consumer");
1988 /* Infinite wait for the consumerd thread to be ready */
1989 ret
= pthread_cond_wait(&consumer_data
->cond
,
1990 &consumer_data
->cond_mutex
);
1992 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
1993 &consumer_data
->cond_mutex
, &timeout
);
1997 /* Release the pthread condition */
1998 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2002 if (ret
== ETIMEDOUT
) {
2004 * Call has timed out so we kill the kconsumerd_thread and return
2007 ERR("Condition timed out. The consumer thread was never ready."
2009 ret
= pthread_cancel(consumer_data
->thread
);
2011 PERROR("pthread_cancel consumer thread");
2014 PERROR("pthread_cond_wait failed consumer thread");
2019 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2020 if (consumer_data
->pid
== 0) {
2021 ERR("Consumerd did not start");
2022 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2025 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2034 * Join consumer thread
2036 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2040 /* Consumer pid must be a real one. */
2041 if (consumer_data
->pid
> 0) {
2043 ret
= kill(consumer_data
->pid
, SIGTERM
);
2045 ERR("Error killing consumer daemon");
2048 return pthread_join(consumer_data
->thread
, &status
);
2055 * Fork and exec a consumer daemon (consumerd).
2057 * Return pid if successful else -1.
2059 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2063 const char *consumer_to_use
;
2064 const char *verbosity
;
2067 DBG("Spawning consumerd");
2074 if (opt_verbose_consumer
) {
2075 verbosity
= "--verbose";
2077 verbosity
= "--quiet";
2079 switch (consumer_data
->type
) {
2080 case LTTNG_CONSUMER_KERNEL
:
2082 * Find out which consumerd to execute. We will first try the
2083 * 64-bit path, then the sessiond's installation directory, and
2084 * fallback on the 32-bit one,
2086 DBG3("Looking for a kernel consumer at these locations:");
2087 DBG3(" 1) %s", consumerd64_bin
);
2088 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2089 DBG3(" 3) %s", consumerd32_bin
);
2090 if (stat(consumerd64_bin
, &st
) == 0) {
2091 DBG3("Found location #1");
2092 consumer_to_use
= consumerd64_bin
;
2093 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2094 DBG3("Found location #2");
2095 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2096 } else if (stat(consumerd32_bin
, &st
) == 0) {
2097 DBG3("Found location #3");
2098 consumer_to_use
= consumerd32_bin
;
2100 DBG("Could not find any valid consumerd executable");
2103 DBG("Using kernel consumer at: %s", consumer_to_use
);
2104 execl(consumer_to_use
,
2105 "lttng-consumerd", verbosity
, "-k",
2106 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2107 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2108 "--group", tracing_group_name
,
2111 case LTTNG_CONSUMER64_UST
:
2113 char *tmpnew
= NULL
;
2115 if (consumerd64_libdir
[0] != '\0') {
2119 tmp
= getenv("LD_LIBRARY_PATH");
2123 tmplen
= strlen("LD_LIBRARY_PATH=")
2124 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2125 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2130 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2131 strcat(tmpnew
, consumerd64_libdir
);
2132 if (tmp
[0] != '\0') {
2133 strcat(tmpnew
, ":");
2134 strcat(tmpnew
, tmp
);
2136 ret
= putenv(tmpnew
);
2143 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2144 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2145 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2146 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2147 "--group", tracing_group_name
,
2149 if (consumerd64_libdir
[0] != '\0') {
2157 case LTTNG_CONSUMER32_UST
:
2159 char *tmpnew
= NULL
;
2161 if (consumerd32_libdir
[0] != '\0') {
2165 tmp
= getenv("LD_LIBRARY_PATH");
2169 tmplen
= strlen("LD_LIBRARY_PATH=")
2170 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2171 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2176 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2177 strcat(tmpnew
, consumerd32_libdir
);
2178 if (tmp
[0] != '\0') {
2179 strcat(tmpnew
, ":");
2180 strcat(tmpnew
, tmp
);
2182 ret
= putenv(tmpnew
);
2189 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2190 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2191 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2192 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2193 "--group", tracing_group_name
,
2195 if (consumerd32_libdir
[0] != '\0') {
2204 PERROR("unknown consumer type");
2208 PERROR("kernel start consumer exec");
2211 } else if (pid
> 0) {
2214 PERROR("start consumer fork");
2222 * Spawn the consumerd daemon and session daemon thread.
2224 static int start_consumerd(struct consumer_data
*consumer_data
)
2229 * Set the listen() state on the socket since there is a possible race
2230 * between the exec() of the consumer daemon and this call if place in the
2231 * consumer thread. See bug #366 for more details.
2233 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2238 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2239 if (consumer_data
->pid
!= 0) {
2240 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2244 ret
= spawn_consumerd(consumer_data
);
2246 ERR("Spawning consumerd failed");
2247 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2251 /* Setting up the consumer_data pid */
2252 consumer_data
->pid
= ret
;
2253 DBG2("Consumer pid %d", consumer_data
->pid
);
2254 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2256 DBG2("Spawning consumer control thread");
2257 ret
= spawn_consumer_thread(consumer_data
);
2259 ERR("Fatal error spawning consumer control thread");
2267 /* Cleanup already created sockets on error. */
2268 if (consumer_data
->err_sock
>= 0) {
2271 err
= close(consumer_data
->err_sock
);
2273 PERROR("close consumer data error socket");
2280 * Setup necessary data for kernel tracer action.
2282 static int init_kernel_tracer(void)
2286 /* Modprobe lttng kernel modules */
2287 ret
= modprobe_lttng_control();
2292 /* Open debugfs lttng */
2293 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2294 if (kernel_tracer_fd
< 0) {
2295 DBG("Failed to open %s", module_proc_lttng
);
2300 /* Validate kernel version */
2301 ret
= kernel_validate_version(kernel_tracer_fd
);
2306 ret
= modprobe_lttng_data();
2311 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2315 modprobe_remove_lttng_control();
2316 ret
= close(kernel_tracer_fd
);
2320 kernel_tracer_fd
= -1;
2321 return LTTNG_ERR_KERN_VERSION
;
2324 ret
= close(kernel_tracer_fd
);
2330 modprobe_remove_lttng_control();
2333 WARN("No kernel tracer available");
2334 kernel_tracer_fd
= -1;
2336 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2338 return LTTNG_ERR_KERN_NA
;
2344 * Copy consumer output from the tracing session to the domain session. The
2345 * function also applies the right modification on a per domain basis for the
2346 * trace files destination directory.
2348 * Should *NOT* be called with RCU read-side lock held.
2350 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2353 const char *dir_name
;
2354 struct consumer_output
*consumer
;
2357 assert(session
->consumer
);
2360 case LTTNG_DOMAIN_KERNEL
:
2361 DBG3("Copying tracing session consumer output in kernel session");
2363 * XXX: We should audit the session creation and what this function
2364 * does "extra" in order to avoid a destroy since this function is used
2365 * in the domain session creation (kernel and ust) only. Same for UST
2368 if (session
->kernel_session
->consumer
) {
2369 consumer_destroy_output(session
->kernel_session
->consumer
);
2371 session
->kernel_session
->consumer
=
2372 consumer_copy_output(session
->consumer
);
2373 /* Ease our life a bit for the next part */
2374 consumer
= session
->kernel_session
->consumer
;
2375 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2377 case LTTNG_DOMAIN_UST
:
2378 DBG3("Copying tracing session consumer output in UST session");
2379 if (session
->ust_session
->consumer
) {
2380 consumer_destroy_output(session
->ust_session
->consumer
);
2382 session
->ust_session
->consumer
=
2383 consumer_copy_output(session
->consumer
);
2384 /* Ease our life a bit for the next part */
2385 consumer
= session
->ust_session
->consumer
;
2386 dir_name
= DEFAULT_UST_TRACE_DIR
;
2389 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2393 /* Append correct directory to subdir */
2394 strncat(consumer
->subdir
, dir_name
,
2395 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2396 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2405 * Create an UST session and add it to the session ust list.
2407 * Should *NOT* be called with RCU read-side lock held.
2409 static int create_ust_session(struct ltt_session
*session
,
2410 struct lttng_domain
*domain
)
2413 struct ltt_ust_session
*lus
= NULL
;
2417 assert(session
->consumer
);
2419 switch (domain
->type
) {
2420 case LTTNG_DOMAIN_UST
:
2423 ERR("Unknown UST domain on create session %d", domain
->type
);
2424 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2428 DBG("Creating UST session");
2430 lus
= trace_ust_create_session(session
->id
);
2432 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2436 lus
->uid
= session
->uid
;
2437 lus
->gid
= session
->gid
;
2438 lus
->output_traces
= session
->output_traces
;
2439 lus
->snapshot_mode
= session
->snapshot_mode
;
2440 lus
->live_timer_interval
= session
->live_timer
;
2441 session
->ust_session
= lus
;
2443 /* Copy session output to the newly created UST session */
2444 ret
= copy_session_consumer(domain
->type
, session
);
2445 if (ret
!= LTTNG_OK
) {
2453 session
->ust_session
= NULL
;
2458 * Create a kernel tracer session then create the default channel.
2460 static int create_kernel_session(struct ltt_session
*session
)
2464 DBG("Creating kernel session");
2466 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2468 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2472 /* Code flow safety */
2473 assert(session
->kernel_session
);
2475 /* Copy session output to the newly created Kernel session */
2476 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2477 if (ret
!= LTTNG_OK
) {
2481 /* Create directory(ies) on local filesystem. */
2482 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2483 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2484 ret
= run_as_mkdir_recursive(
2485 session
->kernel_session
->consumer
->dst
.trace_path
,
2486 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2488 if (ret
!= -EEXIST
) {
2489 ERR("Trace directory creation error");
2495 session
->kernel_session
->uid
= session
->uid
;
2496 session
->kernel_session
->gid
= session
->gid
;
2497 session
->kernel_session
->output_traces
= session
->output_traces
;
2498 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2503 trace_kernel_destroy_session(session
->kernel_session
);
2504 session
->kernel_session
= NULL
;
2509 * Count number of session permitted by uid/gid.
2511 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2514 struct ltt_session
*session
;
2516 DBG("Counting number of available session for UID %d GID %d",
2518 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2520 * Only list the sessions the user can control.
2522 if (!session_access_ok(session
, uid
, gid
)) {
2531 * Process the command requested by the lttng client within the command
2532 * context structure. This function make sure that the return structure (llm)
2533 * is set and ready for transmission before returning.
2535 * Return any error encountered or 0 for success.
2537 * "sock" is only used for special-case var. len data.
2539 * Should *NOT* be called with RCU read-side lock held.
2541 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2545 int need_tracing_session
= 1;
2548 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2552 switch (cmd_ctx
->lsm
->cmd_type
) {
2553 case LTTNG_CREATE_SESSION
:
2554 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2555 case LTTNG_CREATE_SESSION_LIVE
:
2556 case LTTNG_DESTROY_SESSION
:
2557 case LTTNG_LIST_SESSIONS
:
2558 case LTTNG_LIST_DOMAINS
:
2559 case LTTNG_START_TRACE
:
2560 case LTTNG_STOP_TRACE
:
2561 case LTTNG_DATA_PENDING
:
2562 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2563 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2564 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2565 case LTTNG_SNAPSHOT_RECORD
:
2572 if (opt_no_kernel
&& need_domain
2573 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2575 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2577 ret
= LTTNG_ERR_KERN_NA
;
2582 /* Deny register consumer if we already have a spawned consumer. */
2583 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2584 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2585 if (kconsumer_data
.pid
> 0) {
2586 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2587 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2590 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2594 * Check for command that don't needs to allocate a returned payload. We do
2595 * this here so we don't have to make the call for no payload at each
2598 switch(cmd_ctx
->lsm
->cmd_type
) {
2599 case LTTNG_LIST_SESSIONS
:
2600 case LTTNG_LIST_TRACEPOINTS
:
2601 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2602 case LTTNG_LIST_DOMAINS
:
2603 case LTTNG_LIST_CHANNELS
:
2604 case LTTNG_LIST_EVENTS
:
2607 /* Setup lttng message with no payload */
2608 ret
= setup_lttng_msg(cmd_ctx
, 0);
2610 /* This label does not try to unlock the session */
2611 goto init_setup_error
;
2615 /* Commands that DO NOT need a session. */
2616 switch (cmd_ctx
->lsm
->cmd_type
) {
2617 case LTTNG_CREATE_SESSION
:
2618 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2619 case LTTNG_CREATE_SESSION_LIVE
:
2620 case LTTNG_CALIBRATE
:
2621 case LTTNG_LIST_SESSIONS
:
2622 case LTTNG_LIST_TRACEPOINTS
:
2623 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2624 need_tracing_session
= 0;
2627 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2629 * We keep the session list lock across _all_ commands
2630 * for now, because the per-session lock does not
2631 * handle teardown properly.
2633 session_lock_list();
2634 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2635 if (cmd_ctx
->session
== NULL
) {
2636 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2639 /* Acquire lock for the session */
2640 session_lock(cmd_ctx
->session
);
2650 * Check domain type for specific "pre-action".
2652 switch (cmd_ctx
->lsm
->domain
.type
) {
2653 case LTTNG_DOMAIN_KERNEL
:
2655 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2659 /* Kernel tracer check */
2660 if (kernel_tracer_fd
== -1) {
2661 /* Basically, load kernel tracer modules */
2662 ret
= init_kernel_tracer();
2668 /* Consumer is in an ERROR state. Report back to client */
2669 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
2670 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2674 /* Need a session for kernel command */
2675 if (need_tracing_session
) {
2676 if (cmd_ctx
->session
->kernel_session
== NULL
) {
2677 ret
= create_kernel_session(cmd_ctx
->session
);
2679 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2684 /* Start the kernel consumer daemon */
2685 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2686 if (kconsumer_data
.pid
== 0 &&
2687 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2688 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2689 ret
= start_consumerd(&kconsumer_data
);
2691 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2694 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
2696 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2700 * The consumer was just spawned so we need to add the socket to
2701 * the consumer output of the session if exist.
2703 ret
= consumer_create_socket(&kconsumer_data
,
2704 cmd_ctx
->session
->kernel_session
->consumer
);
2711 case LTTNG_DOMAIN_UST
:
2713 if (!ust_app_supported()) {
2714 ret
= LTTNG_ERR_NO_UST
;
2717 /* Consumer is in an ERROR state. Report back to client */
2718 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
2719 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2723 if (need_tracing_session
) {
2724 /* Create UST session if none exist. */
2725 if (cmd_ctx
->session
->ust_session
== NULL
) {
2726 ret
= create_ust_session(cmd_ctx
->session
,
2727 &cmd_ctx
->lsm
->domain
);
2728 if (ret
!= LTTNG_OK
) {
2733 /* Start the UST consumer daemons */
2735 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
2736 if (consumerd64_bin
[0] != '\0' &&
2737 ustconsumer64_data
.pid
== 0 &&
2738 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2739 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2740 ret
= start_consumerd(&ustconsumer64_data
);
2742 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
2743 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
2747 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
2748 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2750 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2754 * Setup socket for consumer 64 bit. No need for atomic access
2755 * since it was set above and can ONLY be set in this thread.
2757 ret
= consumer_create_socket(&ustconsumer64_data
,
2758 cmd_ctx
->session
->ust_session
->consumer
);
2764 if (consumerd32_bin
[0] != '\0' &&
2765 ustconsumer32_data
.pid
== 0 &&
2766 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2767 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2768 ret
= start_consumerd(&ustconsumer32_data
);
2770 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
2771 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
2775 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
2776 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2778 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2782 * Setup socket for consumer 64 bit. No need for atomic access
2783 * since it was set above and can ONLY be set in this thread.
2785 ret
= consumer_create_socket(&ustconsumer32_data
,
2786 cmd_ctx
->session
->ust_session
->consumer
);
2798 /* Validate consumer daemon state when start/stop trace command */
2799 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
2800 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
2801 switch (cmd_ctx
->lsm
->domain
.type
) {
2802 case LTTNG_DOMAIN_UST
:
2803 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
2804 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2808 case LTTNG_DOMAIN_KERNEL
:
2809 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
2810 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2818 * Check that the UID or GID match that of the tracing session.
2819 * The root user can interact with all sessions.
2821 if (need_tracing_session
) {
2822 if (!session_access_ok(cmd_ctx
->session
,
2823 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2824 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
2825 ret
= LTTNG_ERR_EPERM
;
2831 * Send relayd information to consumer as soon as we have a domain and a
2834 if (cmd_ctx
->session
&& need_domain
) {
2836 * Setup relayd if not done yet. If the relayd information was already
2837 * sent to the consumer, this call will gracefully return.
2839 ret
= cmd_setup_relayd(cmd_ctx
->session
);
2840 if (ret
!= LTTNG_OK
) {
2845 /* Process by command type */
2846 switch (cmd_ctx
->lsm
->cmd_type
) {
2847 case LTTNG_ADD_CONTEXT
:
2849 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2850 cmd_ctx
->lsm
->u
.context
.channel_name
,
2851 &cmd_ctx
->lsm
->u
.context
.ctx
, kernel_poll_pipe
[1]);
2854 case LTTNG_DISABLE_CHANNEL
:
2856 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2857 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2860 case LTTNG_DISABLE_EVENT
:
2862 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2863 cmd_ctx
->lsm
->u
.disable
.channel_name
,
2864 cmd_ctx
->lsm
->u
.disable
.name
);
2867 case LTTNG_DISABLE_ALL_EVENT
:
2869 DBG("Disabling all events");
2871 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2872 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2875 case LTTNG_ENABLE_CHANNEL
:
2877 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
2878 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
2881 case LTTNG_ENABLE_EVENT
:
2883 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
2884 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2885 &cmd_ctx
->lsm
->u
.enable
.event
, NULL
, kernel_poll_pipe
[1]);
2888 case LTTNG_ENABLE_ALL_EVENT
:
2890 DBG("Enabling all events");
2892 ret
= cmd_enable_event_all(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
2893 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2894 cmd_ctx
->lsm
->u
.enable
.event
.type
, NULL
, kernel_poll_pipe
[1]);
2897 case LTTNG_LIST_TRACEPOINTS
:
2899 struct lttng_event
*events
;
2902 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
2903 if (nb_events
< 0) {
2904 /* Return value is a negative lttng_error_code. */
2910 * Setup lttng message with payload size set to the event list size in
2911 * bytes and then copy list into the llm payload.
2913 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2919 /* Copy event list into message payload */
2920 memcpy(cmd_ctx
->llm
->payload
, events
,
2921 sizeof(struct lttng_event
) * nb_events
);
2928 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2930 struct lttng_event_field
*fields
;
2933 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
2935 if (nb_fields
< 0) {
2936 /* Return value is a negative lttng_error_code. */
2942 * Setup lttng message with payload size set to the event list size in
2943 * bytes and then copy list into the llm payload.
2945 ret
= setup_lttng_msg(cmd_ctx
,
2946 sizeof(struct lttng_event_field
) * nb_fields
);
2952 /* Copy event list into message payload */
2953 memcpy(cmd_ctx
->llm
->payload
, fields
,
2954 sizeof(struct lttng_event_field
) * nb_fields
);
2961 case LTTNG_SET_CONSUMER_URI
:
2964 struct lttng_uri
*uris
;
2966 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
2967 len
= nb_uri
* sizeof(struct lttng_uri
);
2970 ret
= LTTNG_ERR_INVALID
;
2974 uris
= zmalloc(len
);
2976 ret
= LTTNG_ERR_FATAL
;
2980 /* Receive variable len data */
2981 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
2982 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
2984 DBG("No URIs received from client... continuing");
2986 ret
= LTTNG_ERR_SESSION_FAIL
;
2991 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
2993 if (ret
!= LTTNG_OK
) {
2999 * XXX: 0 means that this URI should be applied on the session. Should
3000 * be a DOMAIN enuam.
3002 if (cmd_ctx
->lsm
->domain
.type
== 0) {
3003 /* Add the URI for the UST session if a consumer is present. */
3004 if (cmd_ctx
->session
->ust_session
&&
3005 cmd_ctx
->session
->ust_session
->consumer
) {
3006 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_UST
, cmd_ctx
->session
,
3008 } else if (cmd_ctx
->session
->kernel_session
&&
3009 cmd_ctx
->session
->kernel_session
->consumer
) {
3010 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL
,
3011 cmd_ctx
->session
, nb_uri
, uris
);
3019 case LTTNG_START_TRACE
:
3021 ret
= cmd_start_trace(cmd_ctx
->session
);
3024 case LTTNG_STOP_TRACE
:
3026 ret
= cmd_stop_trace(cmd_ctx
->session
);
3029 case LTTNG_CREATE_SESSION
:
3032 struct lttng_uri
*uris
= NULL
;
3034 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3035 len
= nb_uri
* sizeof(struct lttng_uri
);
3038 uris
= zmalloc(len
);
3040 ret
= LTTNG_ERR_FATAL
;
3044 /* Receive variable len data */
3045 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3046 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3048 DBG("No URIs received from client... continuing");
3050 ret
= LTTNG_ERR_SESSION_FAIL
;
3055 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3056 DBG("Creating session with ONE network URI is a bad call");
3057 ret
= LTTNG_ERR_SESSION_FAIL
;
3063 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3064 &cmd_ctx
->creds
, 0);
3070 case LTTNG_DESTROY_SESSION
:
3072 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3074 /* Set session to NULL so we do not unlock it after free. */
3075 cmd_ctx
->session
= NULL
;
3078 case LTTNG_LIST_DOMAINS
:
3081 struct lttng_domain
*domains
;
3083 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3085 /* Return value is a negative lttng_error_code. */
3090 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
3096 /* Copy event list into message payload */
3097 memcpy(cmd_ctx
->llm
->payload
, domains
,
3098 nb_dom
* sizeof(struct lttng_domain
));
3105 case LTTNG_LIST_CHANNELS
:
3108 struct lttng_channel
*channels
;
3110 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3111 cmd_ctx
->session
, &channels
);
3113 /* Return value is a negative lttng_error_code. */
3118 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
3124 /* Copy event list into message payload */
3125 memcpy(cmd_ctx
->llm
->payload
, channels
,
3126 nb_chan
* sizeof(struct lttng_channel
));
3133 case LTTNG_LIST_EVENTS
:
3136 struct lttng_event
*events
= NULL
;
3138 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3139 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
3141 /* Return value is a negative lttng_error_code. */
3146 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
3152 /* Copy event list into message payload */
3153 memcpy(cmd_ctx
->llm
->payload
, events
,
3154 nb_event
* sizeof(struct lttng_event
));
3161 case LTTNG_LIST_SESSIONS
:
3163 unsigned int nr_sessions
;
3165 session_lock_list();
3166 nr_sessions
= lttng_sessions_count(
3167 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3168 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3170 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
3172 session_unlock_list();
3176 /* Filled the session array */
3177 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
3178 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3179 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3181 session_unlock_list();
3186 case LTTNG_CALIBRATE
:
3188 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3189 &cmd_ctx
->lsm
->u
.calibrate
);
3192 case LTTNG_REGISTER_CONSUMER
:
3194 struct consumer_data
*cdata
;
3196 switch (cmd_ctx
->lsm
->domain
.type
) {
3197 case LTTNG_DOMAIN_KERNEL
:
3198 cdata
= &kconsumer_data
;
3201 ret
= LTTNG_ERR_UND
;
3205 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3206 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3209 case LTTNG_ENABLE_EVENT_WITH_FILTER
:
3211 struct lttng_filter_bytecode
*bytecode
;
3213 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3214 ret
= LTTNG_ERR_FILTER_INVAL
;
3217 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
== 0) {
3218 ret
= LTTNG_ERR_FILTER_INVAL
;
3221 bytecode
= zmalloc(cmd_ctx
->lsm
->u
.enable
.bytecode_len
);
3223 ret
= LTTNG_ERR_FILTER_NOMEM
;
3226 /* Receive var. len. data */
3227 DBG("Receiving var len data from client ...");
3228 ret
= lttcomm_recv_unix_sock(sock
, bytecode
,
3229 cmd_ctx
->lsm
->u
.enable
.bytecode_len
);
3231 DBG("Nothing recv() from client var len data... continuing");
3233 ret
= LTTNG_ERR_FILTER_INVAL
;
3237 if (bytecode
->len
+ sizeof(*bytecode
)
3238 != cmd_ctx
->lsm
->u
.enable
.bytecode_len
) {
3240 ret
= LTTNG_ERR_FILTER_INVAL
;
3244 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3245 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3246 &cmd_ctx
->lsm
->u
.enable
.event
, bytecode
, kernel_poll_pipe
[1]);
3249 case LTTNG_DATA_PENDING
:
3251 ret
= cmd_data_pending(cmd_ctx
->session
);
3254 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3256 struct lttcomm_lttng_output_id reply
;
3258 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3259 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3260 if (ret
!= LTTNG_OK
) {
3264 ret
= setup_lttng_msg(cmd_ctx
, sizeof(reply
));
3269 /* Copy output list into message payload */
3270 memcpy(cmd_ctx
->llm
->payload
, &reply
, sizeof(reply
));
3274 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3276 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3277 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3280 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3283 struct lttng_snapshot_output
*outputs
= NULL
;
3285 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3286 if (nb_output
< 0) {
3291 ret
= setup_lttng_msg(cmd_ctx
,
3292 nb_output
* sizeof(struct lttng_snapshot_output
));
3299 /* Copy output list into message payload */
3300 memcpy(cmd_ctx
->llm
->payload
, outputs
,
3301 nb_output
* sizeof(struct lttng_snapshot_output
));
3308 case LTTNG_SNAPSHOT_RECORD
:
3310 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3311 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3312 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3315 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3318 struct lttng_uri
*uris
= NULL
;
3320 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3321 len
= nb_uri
* sizeof(struct lttng_uri
);
3324 uris
= zmalloc(len
);
3326 ret
= LTTNG_ERR_FATAL
;
3330 /* Receive variable len data */
3331 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3332 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3334 DBG("No URIs received from client... continuing");
3336 ret
= LTTNG_ERR_SESSION_FAIL
;
3341 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3342 DBG("Creating session with ONE network URI is a bad call");
3343 ret
= LTTNG_ERR_SESSION_FAIL
;
3349 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
3350 nb_uri
, &cmd_ctx
->creds
);
3354 case LTTNG_CREATE_SESSION_LIVE
:
3357 struct lttng_uri
*uris
= NULL
;
3359 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3360 len
= nb_uri
* sizeof(struct lttng_uri
);
3363 uris
= zmalloc(len
);
3365 ret
= LTTNG_ERR_FATAL
;
3369 /* Receive variable len data */
3370 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3371 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3373 DBG("No URIs received from client... continuing");
3375 ret
= LTTNG_ERR_SESSION_FAIL
;
3380 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3381 DBG("Creating session with ONE network URI is a bad call");
3382 ret
= LTTNG_ERR_SESSION_FAIL
;
3388 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
3389 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
3394 ret
= LTTNG_ERR_UND
;
3399 if (cmd_ctx
->llm
== NULL
) {
3400 DBG("Missing llm structure. Allocating one.");
3401 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
3405 /* Set return code */
3406 cmd_ctx
->llm
->ret_code
= ret
;
3408 if (cmd_ctx
->session
) {
3409 session_unlock(cmd_ctx
->session
);
3411 if (need_tracing_session
) {
3412 session_unlock_list();
3419 * Thread managing health check socket.
3421 static void *thread_manage_health(void *data
)
3423 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
3424 uint32_t revents
, nb_fd
;
3425 struct lttng_poll_event events
;
3426 struct health_comm_msg msg
;
3427 struct health_comm_reply reply
;
3429 DBG("[thread] Manage health check started");
3431 rcu_register_thread();
3433 /* We might hit an error path before this is created. */
3434 lttng_poll_init(&events
);
3436 /* Create unix socket */
3437 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
3439 ERR("Unable to create health check Unix socket");
3445 /* lttng health client socket path permissions */
3446 ret
= chown(health_unix_sock_path
, 0,
3447 utils_get_group_id(tracing_group_name
));
3449 ERR("Unable to set group on %s", health_unix_sock_path
);
3455 ret
= chmod(health_unix_sock_path
,
3456 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3458 ERR("Unable to set permissions on %s", health_unix_sock_path
);
3466 * Set the CLOEXEC flag. Return code is useless because either way, the
3469 (void) utils_set_fd_cloexec(sock
);
3471 ret
= lttcomm_listen_unix_sock(sock
);
3477 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3478 * more will be added to this poll set.
3480 ret
= sessiond_set_thread_pollset(&events
, 2);
3485 /* Add the application registration socket */
3486 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
3492 DBG("Health check ready");
3494 /* Inifinite blocking call, waiting for transmission */
3496 ret
= lttng_poll_wait(&events
, -1);
3499 * Restart interrupted system call.
3501 if (errno
== EINTR
) {
3509 for (i
= 0; i
< nb_fd
; i
++) {
3510 /* Fetch once the poll data */
3511 revents
= LTTNG_POLL_GETEV(&events
, i
);
3512 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3514 /* Thread quit pipe has been closed. Killing thread. */
3515 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3521 /* Event on the registration socket */
3522 if (pollfd
== sock
) {
3523 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3524 ERR("Health socket poll error");
3530 new_sock
= lttcomm_accept_unix_sock(sock
);
3536 * Set the CLOEXEC flag. Return code is useless because either way, the
3539 (void) utils_set_fd_cloexec(new_sock
);
3541 DBG("Receiving data from client for health...");
3542 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
3544 DBG("Nothing recv() from client... continuing");
3545 ret
= close(new_sock
);
3553 rcu_thread_online();
3556 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
3558 * health_check_state returns 0 if health is
3561 if (!health_check_state(health_sessiond
, i
)) {
3562 reply
.ret_code
|= 1ULL << i
;
3566 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
3568 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
3570 ERR("Failed to send health data back to client");
3573 /* End of transmission */
3574 ret
= close(new_sock
);
3584 ERR("Health error occurred in %s", __func__
);
3586 DBG("Health check thread dying");
3587 unlink(health_unix_sock_path
);
3595 lttng_poll_clean(&events
);
3597 rcu_unregister_thread();
3602 * This thread manage all clients request using the unix client socket for
3605 static void *thread_manage_clients(void *data
)
3607 int sock
= -1, ret
, i
, pollfd
, err
= -1;
3609 uint32_t revents
, nb_fd
;
3610 struct command_ctx
*cmd_ctx
= NULL
;
3611 struct lttng_poll_event events
;
3613 DBG("[thread] Manage client started");
3615 rcu_register_thread();
3617 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
3619 if (testpoint(thread_manage_clients
)) {
3620 goto error_testpoint
;
3623 health_code_update();
3625 ret
= lttcomm_listen_unix_sock(client_sock
);
3631 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3632 * more will be added to this poll set.
3634 ret
= sessiond_set_thread_pollset(&events
, 2);
3636 goto error_create_poll
;
3639 /* Add the application registration socket */
3640 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
3646 * Notify parent pid that we are ready to accept command for client side.
3648 if (opt_sig_parent
) {
3649 kill(ppid
, SIGUSR1
);
3652 if (testpoint(thread_manage_clients_before_loop
)) {
3656 health_code_update();
3659 DBG("Accepting client command ...");
3661 /* Inifinite blocking call, waiting for transmission */
3663 health_poll_entry();
3664 ret
= lttng_poll_wait(&events
, -1);
3668 * Restart interrupted system call.
3670 if (errno
== EINTR
) {
3678 for (i
= 0; i
< nb_fd
; i
++) {
3679 /* Fetch once the poll data */
3680 revents
= LTTNG_POLL_GETEV(&events
, i
);
3681 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3683 health_code_update();
3685 /* Thread quit pipe has been closed. Killing thread. */
3686 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3692 /* Event on the registration socket */
3693 if (pollfd
== client_sock
) {
3694 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3695 ERR("Client socket poll error");
3701 DBG("Wait for client response");
3703 health_code_update();
3705 sock
= lttcomm_accept_unix_sock(client_sock
);
3711 * Set the CLOEXEC flag. Return code is useless because either way, the
3714 (void) utils_set_fd_cloexec(sock
);
3716 /* Set socket option for credentials retrieval */
3717 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
3722 /* Allocate context command to process the client request */
3723 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
3724 if (cmd_ctx
== NULL
) {
3725 PERROR("zmalloc cmd_ctx");
3729 /* Allocate data buffer for reception */
3730 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
3731 if (cmd_ctx
->lsm
== NULL
) {
3732 PERROR("zmalloc cmd_ctx->lsm");
3736 cmd_ctx
->llm
= NULL
;
3737 cmd_ctx
->session
= NULL
;
3739 health_code_update();
3742 * Data is received from the lttng client. The struct
3743 * lttcomm_session_msg (lsm) contains the command and data request of
3746 DBG("Receiving data from client ...");
3747 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
3748 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
3750 DBG("Nothing recv() from client... continuing");
3756 clean_command_ctx(&cmd_ctx
);
3760 health_code_update();
3762 // TODO: Validate cmd_ctx including sanity check for
3763 // security purpose.
3765 rcu_thread_online();
3767 * This function dispatch the work to the kernel or userspace tracer
3768 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3769 * informations for the client. The command context struct contains
3770 * everything this function may needs.
3772 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
3773 rcu_thread_offline();
3781 * TODO: Inform client somehow of the fatal error. At
3782 * this point, ret < 0 means that a zmalloc failed
3783 * (ENOMEM). Error detected but still accept
3784 * command, unless a socket error has been
3787 clean_command_ctx(&cmd_ctx
);
3791 health_code_update();
3793 DBG("Sending response (size: %d, retcode: %s)",
3794 cmd_ctx
->lttng_msg_size
,
3795 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
3796 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
3798 ERR("Failed to send data back to client");
3801 /* End of transmission */
3808 clean_command_ctx(&cmd_ctx
);
3810 health_code_update();
3822 lttng_poll_clean(&events
);
3823 clean_command_ctx(&cmd_ctx
);
3828 unlink(client_unix_sock_path
);
3829 if (client_sock
>= 0) {
3830 ret
= close(client_sock
);
3838 ERR("Health error occurred in %s", __func__
);
3841 health_unregister(health_sessiond
);
3843 DBG("Client thread dying");
3845 rcu_unregister_thread();
3851 * usage function on stderr
3853 static void usage(void)
3855 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
3856 fprintf(stderr
, " -h, --help Display this usage.\n");
3857 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
3858 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
3859 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
3860 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
3861 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
3862 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
3863 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
3864 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
3865 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
3866 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
3867 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
3868 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
3869 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
3870 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
3871 fprintf(stderr
, " -V, --version Show version number.\n");
3872 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
3873 fprintf(stderr
, " -q, --quiet No output at all.\n");
3874 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3875 fprintf(stderr
, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
3876 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
3877 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
3881 * daemon argument parsing
3883 static int parse_args(int argc
, char **argv
)
3887 static struct option long_options
[] = {
3888 { "client-sock", 1, 0, 'c' },
3889 { "apps-sock", 1, 0, 'a' },
3890 { "kconsumerd-cmd-sock", 1, 0, 'C' },
3891 { "kconsumerd-err-sock", 1, 0, 'E' },
3892 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
3893 { "ustconsumerd32-err-sock", 1, 0, 'H' },
3894 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
3895 { "ustconsumerd64-err-sock", 1, 0, 'F' },
3896 { "consumerd32-path", 1, 0, 'u' },
3897 { "consumerd32-libdir", 1, 0, 'U' },
3898 { "consumerd64-path", 1, 0, 't' },
3899 { "consumerd64-libdir", 1, 0, 'T' },
3900 { "daemonize", 0, 0, 'd' },
3901 { "sig-parent", 0, 0, 'S' },
3902 { "help", 0, 0, 'h' },
3903 { "group", 1, 0, 'g' },
3904 { "version", 0, 0, 'V' },
3905 { "quiet", 0, 0, 'q' },
3906 { "verbose", 0, 0, 'v' },
3907 { "verbose-consumer", 0, 0, 'Z' },
3908 { "no-kernel", 0, 0, 'N' },
3909 { "pidfile", 1, 0, 'p' },
3914 int option_index
= 0;
3915 c
= getopt_long(argc
, argv
, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t:p:",
3916 long_options
, &option_index
);
3923 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
3925 fprintf(stderr
, " with arg %s\n", optarg
);
3929 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3932 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3938 tracing_group_name
= optarg
;
3944 fprintf(stdout
, "%s\n", VERSION
);
3950 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3953 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3956 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3959 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3962 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3965 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3971 lttng_opt_quiet
= 1;
3974 /* Verbose level can increase using multiple -v */
3975 lttng_opt_verbose
+= 1;
3978 opt_verbose_consumer
+= 1;
3981 consumerd32_bin
= optarg
;
3984 consumerd32_libdir
= optarg
;
3987 consumerd64_bin
= optarg
;
3990 consumerd64_libdir
= optarg
;
3993 opt_pidfile
= optarg
;
3996 /* Unknown option or other error.
3997 * Error is printed by getopt, just return */
4006 * Creates the two needed socket by the daemon.
4007 * apps_sock - The communication socket for all UST apps.
4008 * client_sock - The communication of the cli tool (lttng).
4010 static int init_daemon_socket(void)
4015 old_umask
= umask(0);
4017 /* Create client tool unix socket */
4018 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
4019 if (client_sock
< 0) {
4020 ERR("Create unix sock failed: %s", client_unix_sock_path
);
4025 /* Set the cloexec flag */
4026 ret
= utils_set_fd_cloexec(client_sock
);
4028 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4029 "Continuing but note that the consumer daemon will have a "
4030 "reference to this socket on exec()", client_sock
);
4033 /* File permission MUST be 660 */
4034 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4036 ERR("Set file permissions failed: %s", client_unix_sock_path
);
4041 /* Create the application unix socket */
4042 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
4043 if (apps_sock
< 0) {
4044 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
4049 /* Set the cloexec flag */
4050 ret
= utils_set_fd_cloexec(apps_sock
);
4052 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4053 "Continuing but note that the consumer daemon will have a "
4054 "reference to this socket on exec()", apps_sock
);
4057 /* File permission MUST be 666 */
4058 ret
= chmod(apps_unix_sock_path
,
4059 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
4061 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
4066 DBG3("Session daemon client socket %d and application socket %d created",
4067 client_sock
, apps_sock
);
4075 * Check if the global socket is available, and if a daemon is answering at the
4076 * other side. If yes, error is returned.
4078 static int check_existing_daemon(void)
4080 /* Is there anybody out there ? */
4081 if (lttng_session_daemon_alive()) {
4089 * Set the tracing group gid onto the client socket.
4091 * Race window between mkdir and chown is OK because we are going from more
4092 * permissive (root.root) to less permissive (root.tracing).
4094 static int set_permissions(char *rundir
)
4099 gid
= utils_get_group_id(tracing_group_name
);
4101 /* Set lttng run dir */
4102 ret
= chown(rundir
, 0, gid
);
4104 ERR("Unable to set group on %s", rundir
);
4109 * Ensure all applications and tracing group can search the run
4110 * dir. Allow everyone to read the directory, since it does not
4111 * buy us anything to hide its content.
4113 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
4115 ERR("Unable to set permissions on %s", rundir
);
4119 /* lttng client socket path */
4120 ret
= chown(client_unix_sock_path
, 0, gid
);
4122 ERR("Unable to set group on %s", client_unix_sock_path
);
4126 /* kconsumer error socket path */
4127 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
4129 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
4133 /* 64-bit ustconsumer error socket path */
4134 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
4136 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
4140 /* 32-bit ustconsumer compat32 error socket path */
4141 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
4143 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
4147 DBG("All permissions are set");
4153 * Create the lttng run directory needed for all global sockets and pipe.
4155 static int create_lttng_rundir(const char *rundir
)
4159 DBG3("Creating LTTng run directory: %s", rundir
);
4161 ret
= mkdir(rundir
, S_IRWXU
);
4163 if (errno
!= EEXIST
) {
4164 ERR("Unable to create %s", rundir
);
4176 * Setup sockets and directory needed by the kconsumerd communication with the
4179 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
4183 char path
[PATH_MAX
];
4185 switch (consumer_data
->type
) {
4186 case LTTNG_CONSUMER_KERNEL
:
4187 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
4189 case LTTNG_CONSUMER64_UST
:
4190 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
4192 case LTTNG_CONSUMER32_UST
:
4193 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
4196 ERR("Consumer type unknown");
4201 DBG2("Creating consumer directory: %s", path
);
4203 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
4205 if (errno
!= EEXIST
) {
4207 ERR("Failed to create %s", path
);
4213 ret
= chown(path
, 0, utils_get_group_id(tracing_group_name
));
4215 ERR("Unable to set group on %s", path
);
4221 /* Create the kconsumerd error unix socket */
4222 consumer_data
->err_sock
=
4223 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
4224 if (consumer_data
->err_sock
< 0) {
4225 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
4231 * Set the CLOEXEC flag. Return code is useless because either way, the
4234 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
4236 PERROR("utils_set_fd_cloexec");
4237 /* continue anyway */
4240 /* File permission MUST be 660 */
4241 ret
= chmod(consumer_data
->err_unix_sock_path
,
4242 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4244 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
4254 * Signal handler for the daemon
4256 * Simply stop all worker threads, leaving main() return gracefully after
4257 * joining all threads and calling cleanup().
4259 static void sighandler(int sig
)
4263 DBG("SIGPIPE caught");
4266 DBG("SIGINT caught");
4270 DBG("SIGTERM caught");
4279 * Setup signal handler for :
4280 * SIGINT, SIGTERM, SIGPIPE
4282 static int set_signal_handler(void)
4285 struct sigaction sa
;
4288 if ((ret
= sigemptyset(&sigset
)) < 0) {
4289 PERROR("sigemptyset");
4293 sa
.sa_handler
= sighandler
;
4294 sa
.sa_mask
= sigset
;
4296 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
4297 PERROR("sigaction");
4301 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
4302 PERROR("sigaction");
4306 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
4307 PERROR("sigaction");
4311 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
4317 * Set open files limit to unlimited. This daemon can open a large number of
4318 * file descriptors in order to consumer multiple kernel traces.
4320 static void set_ulimit(void)
4325 /* The kernel does not allowed an infinite limit for open files */
4326 lim
.rlim_cur
= 65535;
4327 lim
.rlim_max
= 65535;
4329 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
4331 PERROR("failed to set open files limit");
4336 * Write pidfile using the rundir and opt_pidfile.
4338 static void write_pidfile(void)
4341 char pidfile_path
[PATH_MAX
];
4346 strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
));
4348 /* Build pidfile path from rundir and opt_pidfile. */
4349 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
4350 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
4352 PERROR("snprintf pidfile path");
4358 * Create pid file in rundir. Return value is of no importance. The
4359 * execution will continue even though we are not able to write the file.
4361 (void) utils_create_pid_file(getpid(), pidfile_path
);
4370 int main(int argc
, char **argv
)
4374 const char *home_path
, *env_app_timeout
;
4376 init_kernel_workarounds();
4378 rcu_register_thread();
4380 setup_consumerd_path();
4382 page_size
= sysconf(_SC_PAGESIZE
);
4383 if (page_size
< 0) {
4384 PERROR("sysconf _SC_PAGESIZE");
4385 page_size
= LONG_MAX
;
4386 WARN("Fallback page size to %ld", page_size
);
4389 /* Parse arguments */
4391 if ((ret
= parse_args(argc
, argv
)) < 0) {
4401 * child: setsid, close FD 0, 1, 2, chdir /
4402 * parent: exit (if fork is successful)
4410 * We are in the child. Make sure all other file
4411 * descriptors are closed, in case we are called with
4412 * more opened file descriptors than the standard ones.
4414 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
4419 /* Create thread quit pipe */
4420 if ((ret
= init_thread_quit_pipe()) < 0) {
4424 /* Check if daemon is UID = 0 */
4425 is_root
= !getuid();
4428 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
4430 /* Create global run dir with root access */
4431 ret
= create_lttng_rundir(rundir
);
4436 if (strlen(apps_unix_sock_path
) == 0) {
4437 snprintf(apps_unix_sock_path
, PATH_MAX
,
4438 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
4441 if (strlen(client_unix_sock_path
) == 0) {
4442 snprintf(client_unix_sock_path
, PATH_MAX
,
4443 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
4446 /* Set global SHM for ust */
4447 if (strlen(wait_shm_path
) == 0) {
4448 snprintf(wait_shm_path
, PATH_MAX
,
4449 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
4452 if (strlen(health_unix_sock_path
) == 0) {
4453 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4454 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
4457 /* Setup kernel consumerd path */
4458 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
4459 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
4460 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
4461 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
4463 DBG2("Kernel consumer err path: %s",
4464 kconsumer_data
.err_unix_sock_path
);
4465 DBG2("Kernel consumer cmd path: %s",
4466 kconsumer_data
.cmd_unix_sock_path
);
4468 home_path
= utils_get_home_dir();
4469 if (home_path
== NULL
) {
4470 /* TODO: Add --socket PATH option */
4471 ERR("Can't get HOME directory for sockets creation.");
4477 * Create rundir from home path. This will create something like
4480 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
4486 ret
= create_lttng_rundir(rundir
);
4491 if (strlen(apps_unix_sock_path
) == 0) {
4492 snprintf(apps_unix_sock_path
, PATH_MAX
,
4493 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
4496 /* Set the cli tool unix socket path */
4497 if (strlen(client_unix_sock_path
) == 0) {
4498 snprintf(client_unix_sock_path
, PATH_MAX
,
4499 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
4502 /* Set global SHM for ust */
4503 if (strlen(wait_shm_path
) == 0) {
4504 snprintf(wait_shm_path
, PATH_MAX
,
4505 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
4508 /* Set health check Unix path */
4509 if (strlen(health_unix_sock_path
) == 0) {
4510 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4511 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
4515 /* Set consumer initial state */
4516 kernel_consumerd_state
= CONSUMER_STOPPED
;
4517 ust_consumerd_state
= CONSUMER_STOPPED
;
4519 DBG("Client socket path %s", client_unix_sock_path
);
4520 DBG("Application socket path %s", apps_unix_sock_path
);
4521 DBG("Application wait path %s", wait_shm_path
);
4522 DBG("LTTng run directory path: %s", rundir
);
4524 /* 32 bits consumerd path setup */
4525 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
4526 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
4527 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
4528 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
4530 DBG2("UST consumer 32 bits err path: %s",
4531 ustconsumer32_data
.err_unix_sock_path
);
4532 DBG2("UST consumer 32 bits cmd path: %s",
4533 ustconsumer32_data
.cmd_unix_sock_path
);
4535 /* 64 bits consumerd path setup */
4536 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
4537 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
4538 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
4539 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
4541 DBG2("UST consumer 64 bits err path: %s",
4542 ustconsumer64_data
.err_unix_sock_path
);
4543 DBG2("UST consumer 64 bits cmd path: %s",
4544 ustconsumer64_data
.cmd_unix_sock_path
);
4547 * See if daemon already exist.
4549 if ((ret
= check_existing_daemon()) < 0) {
4550 ERR("Already running daemon.\n");
4552 * We do not goto exit because we must not cleanup()
4553 * because a daemon is already running.
4559 * Init UST app hash table. Alloc hash table before this point since
4560 * cleanup() can get called after that point.
4564 /* After this point, we can safely call cleanup() with "goto exit" */
4567 * These actions must be executed as root. We do that *after* setting up
4568 * the sockets path because we MUST make the check for another daemon using
4569 * those paths *before* trying to set the kernel consumer sockets and init
4573 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
4578 /* Setup kernel tracer */
4579 if (!opt_no_kernel
) {
4580 init_kernel_tracer();
4583 /* Set ulimit for open files */
4586 /* init lttng_fd tracking must be done after set_ulimit. */
4589 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
4594 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
4599 if ((ret
= set_signal_handler()) < 0) {
4603 /* Setup the needed unix socket */
4604 if ((ret
= init_daemon_socket()) < 0) {
4608 /* Set credentials to socket */
4609 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
4613 /* Get parent pid if -S, --sig-parent is specified. */
4614 if (opt_sig_parent
) {
4618 /* Setup the kernel pipe for waking up the kernel thread */
4619 if (is_root
&& !opt_no_kernel
) {
4620 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
4625 /* Setup the thread ht_cleanup communication pipe. */
4626 if (utils_create_pipe_cloexec(ht_cleanup_pipe
) < 0) {
4630 /* Setup the thread apps communication pipe. */
4631 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
4635 /* Setup the thread apps notify communication pipe. */
4636 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
) < 0) {
4640 /* Initialize global buffer per UID and PID registry. */
4641 buffer_reg_init_uid_registry();
4642 buffer_reg_init_pid_registry();
4644 /* Init UST command queue. */
4645 cds_wfq_init(&ust_cmd_queue
.queue
);
4648 * Get session list pointer. This pointer MUST NOT be free(). This list is
4649 * statically declared in session.c
4651 session_list_ptr
= session_get_list();
4653 /* Set up max poll set size */
4654 lttng_poll_set_max_size();
4658 /* Check for the application socket timeout env variable. */
4659 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
4660 if (env_app_timeout
) {
4661 app_socket_timeout
= atoi(env_app_timeout
);
4663 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
4668 /* Initialize communication library */
4670 /* This is to get the TCP timeout value. */
4671 lttcomm_inet_init();
4674 * Initialize the health check subsystem. This call should set the
4675 * appropriate time values.
4677 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
4678 if (!health_sessiond
) {
4679 PERROR("health_app_create error");
4680 goto exit_health_sessiond_cleanup
;
4683 /* Create thread to manage the client socket */
4684 ret
= pthread_create(&ht_cleanup_thread
, NULL
,
4685 thread_ht_cleanup
, (void *) NULL
);
4687 PERROR("pthread_create ht_cleanup");
4688 goto exit_ht_cleanup
;
4691 /* Create thread to manage the client socket */
4692 ret
= pthread_create(&health_thread
, NULL
,
4693 thread_manage_health
, (void *) NULL
);
4695 PERROR("pthread_create health");
4699 /* Create thread to manage the client socket */
4700 ret
= pthread_create(&client_thread
, NULL
,
4701 thread_manage_clients
, (void *) NULL
);
4703 PERROR("pthread_create clients");
4707 /* Create thread to dispatch registration */
4708 ret
= pthread_create(&dispatch_thread
, NULL
,
4709 thread_dispatch_ust_registration
, (void *) NULL
);
4711 PERROR("pthread_create dispatch");
4715 /* Create thread to manage application registration. */
4716 ret
= pthread_create(®_apps_thread
, NULL
,
4717 thread_registration_apps
, (void *) NULL
);
4719 PERROR("pthread_create registration");
4723 /* Create thread to manage application socket */
4724 ret
= pthread_create(&apps_thread
, NULL
,
4725 thread_manage_apps
, (void *) NULL
);
4727 PERROR("pthread_create apps");
4731 /* Create thread to manage application notify socket */
4732 ret
= pthread_create(&apps_notify_thread
, NULL
,
4733 ust_thread_manage_notify
, (void *) NULL
);
4735 PERROR("pthread_create apps");
4736 goto exit_apps_notify
;
4739 /* Don't start this thread if kernel tracing is not requested nor root */
4740 if (is_root
&& !opt_no_kernel
) {
4741 /* Create kernel thread to manage kernel event */
4742 ret
= pthread_create(&kernel_thread
, NULL
,
4743 thread_manage_kernel
, (void *) NULL
);
4745 PERROR("pthread_create kernel");
4749 ret
= pthread_join(kernel_thread
, &status
);
4751 PERROR("pthread_join");
4752 goto error
; /* join error, exit without cleanup */
4757 ret
= pthread_join(apps_notify_thread
, &status
);
4759 PERROR("pthread_join apps notify");
4760 goto error
; /* join error, exit without cleanup */
4764 ret
= pthread_join(apps_thread
, &status
);
4766 PERROR("pthread_join apps");
4767 goto error
; /* join error, exit without cleanup */
4772 ret
= pthread_join(reg_apps_thread
, &status
);
4774 PERROR("pthread_join");
4775 goto error
; /* join error, exit without cleanup */
4779 ret
= pthread_join(dispatch_thread
, &status
);
4781 PERROR("pthread_join");
4782 goto error
; /* join error, exit without cleanup */
4786 ret
= pthread_join(client_thread
, &status
);
4788 PERROR("pthread_join");
4789 goto error
; /* join error, exit without cleanup */
4792 ret
= join_consumer_thread(&kconsumer_data
);
4794 PERROR("join_consumer");
4795 goto error
; /* join error, exit without cleanup */
4798 ret
= join_consumer_thread(&ustconsumer32_data
);
4800 PERROR("join_consumer ust32");
4801 goto error
; /* join error, exit without cleanup */
4804 ret
= join_consumer_thread(&ustconsumer64_data
);
4806 PERROR("join_consumer ust64");
4807 goto error
; /* join error, exit without cleanup */
4811 ret
= pthread_join(health_thread
, &status
);
4813 PERROR("pthread_join health thread");
4814 goto error
; /* join error, exit without cleanup */
4818 ret
= pthread_join(ht_cleanup_thread
, &status
);
4820 PERROR("pthread_join ht cleanup thread");
4821 goto error
; /* join error, exit without cleanup */
4824 health_app_destroy(health_sessiond
);
4825 exit_health_sessiond_cleanup
:
4828 * cleanup() is called when no other thread is running.
4830 rcu_thread_online();
4832 rcu_thread_offline();
4833 rcu_unregister_thread();