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.
29 #include <sys/mount.h>
30 #include <sys/resource.h>
31 #include <sys/socket.h>
33 #include <sys/types.h>
35 #include <urcu/uatomic.h>
39 #include <common/common.h>
40 #include <common/compat/poll.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"
55 #include "kernel-consumer.h"
59 #include "ust-consumer.h"
64 #include "testpoint.h"
66 #define CONSUMERD_FILE "lttng-consumerd"
69 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
70 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
71 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
72 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
75 const char *opt_tracing_group
;
76 static int opt_sig_parent
;
77 static int opt_verbose_consumer
;
78 static int opt_daemon
;
79 static int opt_no_kernel
;
80 static int is_root
; /* Set to 1 if the daemon is running as root */
81 static pid_t ppid
; /* Parent PID for --sig-parent option */
85 * Consumer daemon specific control data. Every value not initialized here is
86 * set to 0 by the static definition.
88 static struct consumer_data kconsumer_data
= {
89 .type
= LTTNG_CONSUMER_KERNEL
,
90 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
91 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
94 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
95 .lock
= PTHREAD_MUTEX_INITIALIZER
,
96 .cond
= PTHREAD_COND_INITIALIZER
,
97 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
99 static struct consumer_data ustconsumer64_data
= {
100 .type
= LTTNG_CONSUMER64_UST
,
101 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
102 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
105 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
106 .lock
= PTHREAD_MUTEX_INITIALIZER
,
107 .cond
= PTHREAD_COND_INITIALIZER
,
108 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
110 static struct consumer_data ustconsumer32_data
= {
111 .type
= LTTNG_CONSUMER32_UST
,
112 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
113 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
116 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
117 .lock
= PTHREAD_MUTEX_INITIALIZER
,
118 .cond
= PTHREAD_COND_INITIALIZER
,
119 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
122 /* Shared between threads */
123 static int dispatch_thread_exit
;
125 /* Global application Unix socket path */
126 static char apps_unix_sock_path
[PATH_MAX
];
127 /* Global client Unix socket path */
128 static char client_unix_sock_path
[PATH_MAX
];
129 /* global wait shm path for UST */
130 static char wait_shm_path
[PATH_MAX
];
131 /* Global health check unix path */
132 static char health_unix_sock_path
[PATH_MAX
];
134 /* Sockets and FDs */
135 static int client_sock
= -1;
136 static int apps_sock
= -1;
137 int kernel_tracer_fd
= -1;
138 static int kernel_poll_pipe
[2] = { -1, -1 };
141 * Quit pipe for all threads. This permits a single cancellation point
142 * for all threads when receiving an event on the pipe.
144 static int thread_quit_pipe
[2] = { -1, -1 };
147 * This pipe is used to inform the thread managing application communication
148 * that a command is queued and ready to be processed.
150 static int apps_cmd_pipe
[2] = { -1, -1 };
152 /* Pthread, Mutexes and Semaphores */
153 static pthread_t apps_thread
;
154 static pthread_t reg_apps_thread
;
155 static pthread_t client_thread
;
156 static pthread_t kernel_thread
;
157 static pthread_t dispatch_thread
;
158 static pthread_t health_thread
;
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
;
225 /* Used for the health monitoring of the session daemon. See health.h */
226 struct health_state health_thread_cmd
;
227 struct health_state health_thread_app_manage
;
228 struct health_state health_thread_app_reg
;
229 struct health_state health_thread_kernel
;
232 void setup_consumerd_path(void)
234 const char *bin
, *libdir
;
237 * Allow INSTALL_BIN_PATH to be used as a target path for the
238 * native architecture size consumer if CONFIG_CONSUMER*_PATH
239 * has not been defined.
241 #if (CAA_BITS_PER_LONG == 32)
242 if (!consumerd32_bin
[0]) {
243 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
245 if (!consumerd32_libdir
[0]) {
246 consumerd32_libdir
= INSTALL_LIB_PATH
;
248 #elif (CAA_BITS_PER_LONG == 64)
249 if (!consumerd64_bin
[0]) {
250 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
252 if (!consumerd64_libdir
[0]) {
253 consumerd64_libdir
= INSTALL_LIB_PATH
;
256 #error "Unknown bitness"
260 * runtime env. var. overrides the build default.
262 bin
= getenv("LTTNG_CONSUMERD32_BIN");
264 consumerd32_bin
= bin
;
266 bin
= getenv("LTTNG_CONSUMERD64_BIN");
268 consumerd64_bin
= bin
;
270 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
272 consumerd32_libdir
= libdir
;
274 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
276 consumerd64_libdir
= libdir
;
281 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
283 static int create_thread_poll_set(struct lttng_poll_event
*events
,
288 if (events
== NULL
|| size
== 0) {
293 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
299 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
311 * Check if the thread quit pipe was triggered.
313 * Return 1 if it was triggered else 0;
315 static int check_thread_quit_pipe(int fd
, uint32_t events
)
317 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
325 * Return group ID of the tracing group or -1 if not found.
327 static gid_t
allowed_group(void)
331 if (opt_tracing_group
) {
332 grp
= getgrnam(opt_tracing_group
);
334 grp
= getgrnam(default_tracing_group
);
344 * Init thread quit pipe.
346 * Return -1 on error or 0 if all pipes are created.
348 static int init_thread_quit_pipe(void)
352 ret
= pipe(thread_quit_pipe
);
354 PERROR("thread quit pipe");
358 for (i
= 0; i
< 2; i
++) {
359 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
371 * Stop all threads by closing the thread quit pipe.
373 static void stop_threads(void)
377 /* Stopping all threads */
378 DBG("Terminating all threads");
379 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
381 ERR("write error on thread quit pipe");
384 /* Dispatch thread */
385 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
386 futex_nto1_wake(&ust_cmd_queue
.futex
);
392 static void cleanup(void)
396 struct ltt_session
*sess
, *stmp
;
400 /* First thing first, stop all threads */
401 utils_close_pipe(thread_quit_pipe
);
403 DBG("Removing %s directory", rundir
);
404 ret
= asprintf(&cmd
, "rm -rf %s", rundir
);
406 ERR("asprintf failed. Something is really wrong!");
409 /* Remove lttng run directory */
412 ERR("Unable to clean %s", rundir
);
416 DBG("Cleaning up all sessions");
418 /* Destroy session list mutex */
419 if (session_list_ptr
!= NULL
) {
420 pthread_mutex_destroy(&session_list_ptr
->lock
);
422 /* Cleanup ALL session */
423 cds_list_for_each_entry_safe(sess
, stmp
,
424 &session_list_ptr
->head
, list
) {
425 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
429 DBG("Closing all UST sockets");
430 ust_app_clean_list();
432 if (is_root
&& !opt_no_kernel
) {
433 DBG2("Closing kernel fd");
434 if (kernel_tracer_fd
>= 0) {
435 ret
= close(kernel_tracer_fd
);
440 DBG("Unloading kernel modules");
441 modprobe_remove_lttng_all();
444 utils_close_pipe(kernel_poll_pipe
);
445 utils_close_pipe(apps_cmd_pipe
);
448 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
449 "Matthew, BEET driven development works!%c[%dm",
450 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
455 * Send data on a unix socket using the liblttsessiondcomm API.
457 * Return lttcomm error code.
459 static int send_unix_sock(int sock
, void *buf
, size_t len
)
461 /* Check valid length */
466 return lttcomm_send_unix_sock(sock
, buf
, len
);
470 * Free memory of a command context structure.
472 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
474 DBG("Clean command context structure");
476 if ((*cmd_ctx
)->llm
) {
477 free((*cmd_ctx
)->llm
);
479 if ((*cmd_ctx
)->lsm
) {
480 free((*cmd_ctx
)->lsm
);
488 * Notify UST applications using the shm mmap futex.
490 static int notify_ust_apps(int active
)
494 DBG("Notifying applications of session daemon state: %d", active
);
496 /* See shm.c for this call implying mmap, shm and futex calls */
497 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
498 if (wait_shm_mmap
== NULL
) {
502 /* Wake waiting process */
503 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
505 /* Apps notified successfully */
513 * Setup the outgoing data buffer for the response (llm) by allocating the
514 * right amount of memory and copying the original information from the lsm
517 * Return total size of the buffer pointed by buf.
519 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
525 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
526 if (cmd_ctx
->llm
== NULL
) {
532 /* Copy common data */
533 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
534 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
536 cmd_ctx
->llm
->data_size
= size
;
537 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
546 * Update the kernel poll set of all channel fd available over all tracing
547 * session. Add the wakeup pipe at the end of the set.
549 static int update_kernel_poll(struct lttng_poll_event
*events
)
552 struct ltt_session
*session
;
553 struct ltt_kernel_channel
*channel
;
555 DBG("Updating kernel poll set");
558 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
559 session_lock(session
);
560 if (session
->kernel_session
== NULL
) {
561 session_unlock(session
);
565 cds_list_for_each_entry(channel
,
566 &session
->kernel_session
->channel_list
.head
, list
) {
567 /* Add channel fd to the kernel poll set */
568 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
570 session_unlock(session
);
573 DBG("Channel fd %d added to kernel set", channel
->fd
);
575 session_unlock(session
);
577 session_unlock_list();
582 session_unlock_list();
587 * Find the channel fd from 'fd' over all tracing session. When found, check
588 * for new channel stream and send those stream fds to the kernel consumer.
590 * Useful for CPU hotplug feature.
592 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
595 struct ltt_session
*session
;
596 struct ltt_kernel_session
*ksess
;
597 struct ltt_kernel_channel
*channel
;
599 DBG("Updating kernel streams for channel fd %d", fd
);
602 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
603 session_lock(session
);
604 if (session
->kernel_session
== NULL
) {
605 session_unlock(session
);
608 ksess
= session
->kernel_session
;
610 cds_list_for_each_entry(channel
, &ksess
->channel_list
.head
, list
) {
611 if (channel
->fd
== fd
) {
612 DBG("Channel found, updating kernel streams");
613 ret
= kernel_open_channel_stream(channel
);
619 * Have we already sent fds to the consumer? If yes, it means
620 * that tracing is started so it is safe to send our updated
623 if (ksess
->consumer_fds_sent
== 1 && ksess
->consumer
!= NULL
) {
624 struct lttng_ht_iter iter
;
625 struct consumer_socket
*socket
;
628 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
629 &iter
.iter
, socket
, node
.node
) {
630 /* Code flow error */
631 assert(socket
->fd
>= 0);
633 pthread_mutex_lock(socket
->lock
);
634 ret
= kernel_consumer_send_channel_stream(socket
->fd
,
636 pthread_mutex_unlock(socket
->lock
);
645 session_unlock(session
);
647 session_unlock_list();
651 session_unlock(session
);
652 session_unlock_list();
657 * For each tracing session, update newly registered apps.
659 static void update_ust_app(int app_sock
)
661 struct ltt_session
*sess
, *stmp
;
665 /* For all tracing session(s) */
666 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
668 if (sess
->ust_session
) {
669 ust_app_global_update(sess
->ust_session
, app_sock
);
671 session_unlock(sess
);
674 session_unlock_list();
678 * This thread manage event coming from the kernel.
680 * Features supported in this thread:
683 static void *thread_manage_kernel(void *data
)
685 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
686 uint32_t revents
, nb_fd
;
688 struct lttng_poll_event events
;
690 DBG("Thread manage kernel started");
692 testpoint(thread_manage_kernel
);
694 health_code_update(&health_thread_kernel
);
696 testpoint(thread_manage_kernel_before_loop
);
698 ret
= create_thread_poll_set(&events
, 2);
700 goto error_poll_create
;
703 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
709 health_code_update(&health_thread_kernel
);
711 if (update_poll_flag
== 1) {
713 * Reset number of fd in the poll set. Always 2 since there is the thread
714 * quit pipe and the kernel pipe.
718 ret
= update_kernel_poll(&events
);
722 update_poll_flag
= 0;
725 nb_fd
= LTTNG_POLL_GETNB(&events
);
727 DBG("Thread kernel polling on %d fds", nb_fd
);
729 /* Zeroed the poll events */
730 lttng_poll_reset(&events
);
732 /* Poll infinite value of time */
734 health_poll_update(&health_thread_kernel
);
735 ret
= lttng_poll_wait(&events
, -1);
736 health_poll_update(&health_thread_kernel
);
739 * Restart interrupted system call.
741 if (errno
== EINTR
) {
745 } else if (ret
== 0) {
746 /* Should not happen since timeout is infinite */
747 ERR("Return value of poll is 0 with an infinite timeout.\n"
748 "This should not have happened! Continuing...");
752 for (i
= 0; i
< nb_fd
; i
++) {
753 /* Fetch once the poll data */
754 revents
= LTTNG_POLL_GETEV(&events
, i
);
755 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
757 health_code_update(&health_thread_kernel
);
759 /* Thread quit pipe has been closed. Killing thread. */
760 ret
= check_thread_quit_pipe(pollfd
, revents
);
766 /* Check for data on kernel pipe */
767 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
768 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
769 update_poll_flag
= 1;
773 * New CPU detected by the kernel. Adding kernel stream to
774 * kernel session and updating the kernel consumer
776 if (revents
& LPOLLIN
) {
777 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
783 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
784 * and unregister kernel stream at this point.
793 lttng_poll_clean(&events
);
796 health_error(&health_thread_kernel
);
797 ERR("Health error occurred in %s", __func__
);
799 health_exit(&health_thread_kernel
);
800 DBG("Kernel thread dying");
805 * Signal pthread condition of the consumer data that the thread.
807 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
809 pthread_mutex_lock(&data
->cond_mutex
);
812 * The state is set before signaling. It can be any value, it's the waiter
813 * job to correctly interpret this condition variable associated to the
814 * consumer pthread_cond.
816 * A value of 0 means that the corresponding thread of the consumer data
817 * was not started. 1 indicates that the thread has started and is ready
818 * for action. A negative value means that there was an error during the
821 data
->consumer_thread_is_ready
= state
;
822 (void) pthread_cond_signal(&data
->cond
);
824 pthread_mutex_unlock(&data
->cond_mutex
);
828 * This thread manage the consumer error sent back to the session daemon.
830 static void *thread_manage_consumer(void *data
)
832 int sock
= -1, i
, ret
, pollfd
, err
= -1;
833 uint32_t revents
, nb_fd
;
834 enum lttcomm_return_code code
;
835 struct lttng_poll_event events
;
836 struct consumer_data
*consumer_data
= data
;
838 DBG("[thread] Manage consumer started");
840 health_code_update(&consumer_data
->health
);
842 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
848 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
849 * Nothing more will be added to this poll set.
851 ret
= create_thread_poll_set(&events
, 2);
856 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
861 nb_fd
= LTTNG_POLL_GETNB(&events
);
863 health_code_update(&consumer_data
->health
);
865 /* Inifinite blocking call, waiting for transmission */
867 health_poll_update(&consumer_data
->health
);
869 testpoint(thread_manage_consumer
);
871 ret
= lttng_poll_wait(&events
, -1);
872 health_poll_update(&consumer_data
->health
);
875 * Restart interrupted system call.
877 if (errno
== EINTR
) {
883 for (i
= 0; i
< nb_fd
; i
++) {
884 /* Fetch once the poll data */
885 revents
= LTTNG_POLL_GETEV(&events
, i
);
886 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
888 health_code_update(&consumer_data
->health
);
890 /* Thread quit pipe has been closed. Killing thread. */
891 ret
= check_thread_quit_pipe(pollfd
, revents
);
897 /* Event on the registration socket */
898 if (pollfd
== consumer_data
->err_sock
) {
899 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
900 ERR("consumer err socket poll error");
906 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
911 health_code_update(&consumer_data
->health
);
913 DBG2("Receiving code from consumer err_sock");
915 /* Getting status code from kconsumerd */
916 ret
= lttcomm_recv_unix_sock(sock
, &code
,
917 sizeof(enum lttcomm_return_code
));
922 health_code_update(&consumer_data
->health
);
924 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
925 consumer_data
->cmd_sock
=
926 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
927 if (consumer_data
->cmd_sock
< 0) {
928 /* On error, signal condition and quit. */
929 signal_consumer_condition(consumer_data
, -1);
930 PERROR("consumer connect");
933 signal_consumer_condition(consumer_data
, 1);
934 DBG("Consumer command socket ready");
936 ERR("consumer error when waiting for SOCK_READY : %s",
937 lttcomm_get_readable_code(-code
));
941 /* Remove the kconsumerd error sock since we've established a connexion */
942 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
947 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
952 health_code_update(&consumer_data
->health
);
954 /* Update number of fd */
955 nb_fd
= LTTNG_POLL_GETNB(&events
);
957 /* Inifinite blocking call, waiting for transmission */
959 health_poll_update(&consumer_data
->health
);
960 ret
= lttng_poll_wait(&events
, -1);
961 health_poll_update(&consumer_data
->health
);
964 * Restart interrupted system call.
966 if (errno
== EINTR
) {
972 for (i
= 0; i
< nb_fd
; i
++) {
973 /* Fetch once the poll data */
974 revents
= LTTNG_POLL_GETEV(&events
, i
);
975 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
977 health_code_update(&consumer_data
->health
);
979 /* Thread quit pipe has been closed. Killing thread. */
980 ret
= check_thread_quit_pipe(pollfd
, revents
);
986 /* Event on the kconsumerd socket */
987 if (pollfd
== sock
) {
988 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
989 ERR("consumer err socket second poll error");
995 health_code_update(&consumer_data
->health
);
997 /* Wait for any kconsumerd error */
998 ret
= lttcomm_recv_unix_sock(sock
, &code
,
999 sizeof(enum lttcomm_return_code
));
1001 ERR("consumer closed the command socket");
1005 ERR("consumer return code : %s", lttcomm_get_readable_code(-code
));
1009 /* Immediately set the consumerd state to stopped */
1010 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1011 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1012 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1013 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1014 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1016 /* Code flow error... */
1020 if (consumer_data
->err_sock
>= 0) {
1021 ret
= close(consumer_data
->err_sock
);
1026 if (consumer_data
->cmd_sock
>= 0) {
1027 ret
= close(consumer_data
->cmd_sock
);
1039 unlink(consumer_data
->err_unix_sock_path
);
1040 unlink(consumer_data
->cmd_unix_sock_path
);
1041 consumer_data
->pid
= 0;
1043 lttng_poll_clean(&events
);
1047 health_error(&consumer_data
->health
);
1048 ERR("Health error occurred in %s", __func__
);
1050 health_exit(&consumer_data
->health
);
1051 DBG("consumer thread cleanup completed");
1057 * This thread manage application communication.
1059 static void *thread_manage_apps(void *data
)
1061 int i
, ret
, pollfd
, err
= -1;
1062 uint32_t revents
, nb_fd
;
1063 struct ust_command ust_cmd
;
1064 struct lttng_poll_event events
;
1066 DBG("[thread] Manage application started");
1068 testpoint(thread_manage_apps
);
1070 rcu_register_thread();
1071 rcu_thread_online();
1073 health_code_update(&health_thread_app_manage
);
1075 ret
= create_thread_poll_set(&events
, 2);
1077 goto error_poll_create
;
1080 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1085 testpoint(thread_manage_apps_before_loop
);
1087 health_code_update(&health_thread_app_manage
);
1090 /* Zeroed the events structure */
1091 lttng_poll_reset(&events
);
1093 nb_fd
= LTTNG_POLL_GETNB(&events
);
1095 DBG("Apps thread polling on %d fds", nb_fd
);
1097 /* Inifinite blocking call, waiting for transmission */
1099 health_poll_update(&health_thread_app_manage
);
1100 ret
= lttng_poll_wait(&events
, -1);
1101 health_poll_update(&health_thread_app_manage
);
1104 * Restart interrupted system call.
1106 if (errno
== EINTR
) {
1112 for (i
= 0; i
< nb_fd
; i
++) {
1113 /* Fetch once the poll data */
1114 revents
= LTTNG_POLL_GETEV(&events
, i
);
1115 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1117 health_code_update(&health_thread_app_manage
);
1119 /* Thread quit pipe has been closed. Killing thread. */
1120 ret
= check_thread_quit_pipe(pollfd
, revents
);
1126 /* Inspect the apps cmd pipe */
1127 if (pollfd
== apps_cmd_pipe
[0]) {
1128 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1129 ERR("Apps command pipe error");
1131 } else if (revents
& LPOLLIN
) {
1133 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
1134 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
1135 PERROR("read apps cmd pipe");
1139 health_code_update(&health_thread_app_manage
);
1141 /* Register applicaton to the session daemon */
1142 ret
= ust_app_register(&ust_cmd
.reg_msg
,
1144 if (ret
== -ENOMEM
) {
1146 } else if (ret
< 0) {
1150 health_code_update(&health_thread_app_manage
);
1153 * Validate UST version compatibility.
1155 ret
= ust_app_validate_version(ust_cmd
.sock
);
1158 * Add channel(s) and event(s) to newly registered apps
1159 * from lttng global UST domain.
1161 update_ust_app(ust_cmd
.sock
);
1164 health_code_update(&health_thread_app_manage
);
1166 ret
= ust_app_register_done(ust_cmd
.sock
);
1169 * If the registration is not possible, we simply
1170 * unregister the apps and continue
1172 ust_app_unregister(ust_cmd
.sock
);
1175 * We just need here to monitor the close of the UST
1176 * socket and poll set monitor those by default.
1177 * Listen on POLLIN (even if we never expect any
1178 * data) to ensure that hangup wakes us.
1180 ret
= lttng_poll_add(&events
, ust_cmd
.sock
, LPOLLIN
);
1185 DBG("Apps with sock %d added to poll set",
1189 health_code_update(&health_thread_app_manage
);
1195 * At this point, we know that a registered application made
1196 * the event at poll_wait.
1198 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1199 /* Removing from the poll set */
1200 ret
= lttng_poll_del(&events
, pollfd
);
1205 /* Socket closed on remote end. */
1206 ust_app_unregister(pollfd
);
1211 health_code_update(&health_thread_app_manage
);
1217 lttng_poll_clean(&events
);
1220 health_error(&health_thread_app_manage
);
1221 ERR("Health error occurred in %s", __func__
);
1223 health_exit(&health_thread_app_manage
);
1224 DBG("Application communication apps thread cleanup complete");
1225 rcu_thread_offline();
1226 rcu_unregister_thread();
1231 * Dispatch request from the registration threads to the application
1232 * communication thread.
1234 static void *thread_dispatch_ust_registration(void *data
)
1237 struct cds_wfq_node
*node
;
1238 struct ust_command
*ust_cmd
= NULL
;
1240 DBG("[thread] Dispatch UST command started");
1242 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1243 /* Atomically prepare the queue futex */
1244 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1247 /* Dequeue command for registration */
1248 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1250 DBG("Woken up but nothing in the UST command queue");
1251 /* Continue thread execution */
1255 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1257 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1258 " gid:%d sock:%d name:%s (version %d.%d)",
1259 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1260 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1261 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1262 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1264 * Inform apps thread of the new application registration. This
1265 * call is blocking so we can be assured that the data will be read
1266 * at some point in time or wait to the end of the world :)
1268 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1269 sizeof(struct ust_command
));
1271 PERROR("write apps cmd pipe");
1272 if (errno
== EBADF
) {
1274 * We can't inform the application thread to process
1275 * registration. We will exit or else application
1276 * registration will not occur and tracing will never
1283 } while (node
!= NULL
);
1285 /* Futex wait on queue. Blocking call on futex() */
1286 futex_nto1_wait(&ust_cmd_queue
.futex
);
1290 DBG("Dispatch thread dying");
1295 * This thread manage application registration.
1297 static void *thread_registration_apps(void *data
)
1299 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1300 uint32_t revents
, nb_fd
;
1301 struct lttng_poll_event events
;
1303 * Get allocated in this thread, enqueued to a global queue, dequeued and
1304 * freed in the manage apps thread.
1306 struct ust_command
*ust_cmd
= NULL
;
1308 DBG("[thread] Manage application registration started");
1310 testpoint(thread_registration_apps
);
1312 ret
= lttcomm_listen_unix_sock(apps_sock
);
1318 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1319 * more will be added to this poll set.
1321 ret
= create_thread_poll_set(&events
, 2);
1323 goto error_create_poll
;
1326 /* Add the application registration socket */
1327 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1329 goto error_poll_add
;
1332 /* Notify all applications to register */
1333 ret
= notify_ust_apps(1);
1335 ERR("Failed to notify applications or create the wait shared memory.\n"
1336 "Execution continues but there might be problem for already\n"
1337 "running applications that wishes to register.");
1341 DBG("Accepting application registration");
1343 nb_fd
= LTTNG_POLL_GETNB(&events
);
1345 /* Inifinite blocking call, waiting for transmission */
1347 health_poll_update(&health_thread_app_reg
);
1348 ret
= lttng_poll_wait(&events
, -1);
1349 health_poll_update(&health_thread_app_reg
);
1352 * Restart interrupted system call.
1354 if (errno
== EINTR
) {
1360 for (i
= 0; i
< nb_fd
; i
++) {
1361 health_code_update(&health_thread_app_reg
);
1363 /* Fetch once the poll data */
1364 revents
= LTTNG_POLL_GETEV(&events
, i
);
1365 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1367 /* Thread quit pipe has been closed. Killing thread. */
1368 ret
= check_thread_quit_pipe(pollfd
, revents
);
1374 /* Event on the registration socket */
1375 if (pollfd
== apps_sock
) {
1376 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1377 ERR("Register apps socket poll error");
1379 } else if (revents
& LPOLLIN
) {
1380 sock
= lttcomm_accept_unix_sock(apps_sock
);
1385 /* Create UST registration command for enqueuing */
1386 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1387 if (ust_cmd
== NULL
) {
1388 PERROR("ust command zmalloc");
1393 * Using message-based transmissions to ensure we don't
1394 * have to deal with partially received messages.
1396 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1398 ERR("Exhausted file descriptors allowed for applications.");
1407 health_code_update(&health_thread_app_reg
);
1408 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1409 sizeof(struct ust_register_msg
));
1410 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1412 PERROR("lttcomm_recv_unix_sock register apps");
1414 ERR("Wrong size received on apps register");
1421 lttng_fd_put(LTTNG_FD_APPS
, 1);
1425 health_code_update(&health_thread_app_reg
);
1427 ust_cmd
->sock
= sock
;
1430 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1431 " gid:%d sock:%d name:%s (version %d.%d)",
1432 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1433 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1434 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1435 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1438 * Lock free enqueue the registration request. The red pill
1439 * has been taken! This apps will be part of the *system*.
1441 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1444 * Wake the registration queue futex. Implicit memory
1445 * barrier with the exchange in cds_wfq_enqueue.
1447 futex_nto1_wake(&ust_cmd_queue
.futex
);
1456 health_error(&health_thread_app_reg
);
1457 ERR("Health error occurred in %s", __func__
);
1459 health_exit(&health_thread_app_reg
);
1461 /* Notify that the registration thread is gone */
1464 if (apps_sock
>= 0) {
1465 ret
= close(apps_sock
);
1475 lttng_fd_put(LTTNG_FD_APPS
, 1);
1477 unlink(apps_unix_sock_path
);
1480 lttng_poll_clean(&events
);
1483 DBG("UST Registration thread cleanup complete");
1489 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1490 * exec or it will fails.
1492 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
1495 struct timespec timeout
;
1497 /* Make sure we set the readiness flag to 0 because we are NOT ready */
1498 consumer_data
->consumer_thread_is_ready
= 0;
1500 /* Setup pthread condition */
1501 ret
= pthread_condattr_init(&consumer_data
->condattr
);
1504 PERROR("pthread_condattr_init consumer data");
1509 * Set the monotonic clock in order to make sure we DO NOT jump in time
1510 * between the clock_gettime() call and the timedwait call. See bug #324
1511 * for a more details and how we noticed it.
1513 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
1516 PERROR("pthread_condattr_setclock consumer data");
1520 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
1523 PERROR("pthread_cond_init consumer data");
1527 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
1530 PERROR("pthread_create consumer");
1535 /* We are about to wait on a pthread condition */
1536 pthread_mutex_lock(&consumer_data
->cond_mutex
);
1538 /* Get time for sem_timedwait absolute timeout */
1539 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
1541 * Set the timeout for the condition timed wait even if the clock gettime
1542 * call fails since we might loop on that call and we want to avoid to
1543 * increment the timeout too many times.
1545 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
1548 * The following loop COULD be skipped in some conditions so this is why we
1549 * set ret to 0 in order to make sure at least one round of the loop is
1555 * Loop until the condition is reached or when a timeout is reached. Note
1556 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
1557 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
1558 * possible. This loop does not take any chances and works with both of
1561 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
1562 if (clock_ret
< 0) {
1563 PERROR("clock_gettime spawn consumer");
1564 /* Infinite wait for the consumerd thread to be ready */
1565 ret
= pthread_cond_wait(&consumer_data
->cond
,
1566 &consumer_data
->cond_mutex
);
1568 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
1569 &consumer_data
->cond_mutex
, &timeout
);
1573 /* Release the pthread condition */
1574 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
1578 if (ret
== ETIMEDOUT
) {
1580 * Call has timed out so we kill the kconsumerd_thread and return
1583 ERR("Condition timed out. The consumer thread was never ready."
1585 ret
= pthread_cancel(consumer_data
->thread
);
1587 PERROR("pthread_cancel consumer thread");
1590 PERROR("pthread_cond_wait failed consumer thread");
1595 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1596 if (consumer_data
->pid
== 0) {
1597 ERR("Consumerd did not start");
1598 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1601 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1610 * Join consumer thread
1612 static int join_consumer_thread(struct consumer_data
*consumer_data
)
1617 /* Consumer pid must be a real one. */
1618 if (consumer_data
->pid
> 0) {
1619 ret
= kill(consumer_data
->pid
, SIGTERM
);
1621 ERR("Error killing consumer daemon");
1624 return pthread_join(consumer_data
->thread
, &status
);
1631 * Fork and exec a consumer daemon (consumerd).
1633 * Return pid if successful else -1.
1635 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
1639 const char *consumer_to_use
;
1640 const char *verbosity
;
1643 DBG("Spawning consumerd");
1650 if (opt_verbose_consumer
) {
1651 verbosity
= "--verbose";
1653 verbosity
= "--quiet";
1655 switch (consumer_data
->type
) {
1656 case LTTNG_CONSUMER_KERNEL
:
1658 * Find out which consumerd to execute. We will first try the
1659 * 64-bit path, then the sessiond's installation directory, and
1660 * fallback on the 32-bit one,
1662 DBG3("Looking for a kernel consumer at these locations:");
1663 DBG3(" 1) %s", consumerd64_bin
);
1664 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
1665 DBG3(" 3) %s", consumerd32_bin
);
1666 if (stat(consumerd64_bin
, &st
) == 0) {
1667 DBG3("Found location #1");
1668 consumer_to_use
= consumerd64_bin
;
1669 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
1670 DBG3("Found location #2");
1671 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
1672 } else if (stat(consumerd32_bin
, &st
) == 0) {
1673 DBG3("Found location #3");
1674 consumer_to_use
= consumerd32_bin
;
1676 DBG("Could not find any valid consumerd executable");
1679 DBG("Using kernel consumer at: %s", consumer_to_use
);
1680 execl(consumer_to_use
,
1681 "lttng-consumerd", verbosity
, "-k",
1682 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1683 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1686 case LTTNG_CONSUMER64_UST
:
1688 char *tmpnew
= NULL
;
1690 if (consumerd64_libdir
[0] != '\0') {
1694 tmp
= getenv("LD_LIBRARY_PATH");
1698 tmplen
= strlen("LD_LIBRARY_PATH=")
1699 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
1700 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1705 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1706 strcat(tmpnew
, consumerd64_libdir
);
1707 if (tmp
[0] != '\0') {
1708 strcat(tmpnew
, ":");
1709 strcat(tmpnew
, tmp
);
1711 ret
= putenv(tmpnew
);
1717 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
1718 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
1719 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1720 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1722 if (consumerd64_libdir
[0] != '\0') {
1730 case LTTNG_CONSUMER32_UST
:
1732 char *tmpnew
= NULL
;
1734 if (consumerd32_libdir
[0] != '\0') {
1738 tmp
= getenv("LD_LIBRARY_PATH");
1742 tmplen
= strlen("LD_LIBRARY_PATH=")
1743 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
1744 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1749 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1750 strcat(tmpnew
, consumerd32_libdir
);
1751 if (tmp
[0] != '\0') {
1752 strcat(tmpnew
, ":");
1753 strcat(tmpnew
, tmp
);
1755 ret
= putenv(tmpnew
);
1761 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
1762 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
1763 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1764 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1766 if (consumerd32_libdir
[0] != '\0') {
1775 PERROR("unknown consumer type");
1779 PERROR("kernel start consumer exec");
1782 } else if (pid
> 0) {
1785 PERROR("start consumer fork");
1793 * Spawn the consumerd daemon and session daemon thread.
1795 static int start_consumerd(struct consumer_data
*consumer_data
)
1799 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1800 if (consumer_data
->pid
!= 0) {
1801 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1805 ret
= spawn_consumerd(consumer_data
);
1807 ERR("Spawning consumerd failed");
1808 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1812 /* Setting up the consumer_data pid */
1813 consumer_data
->pid
= ret
;
1814 DBG2("Consumer pid %d", consumer_data
->pid
);
1815 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1817 DBG2("Spawning consumer control thread");
1818 ret
= spawn_consumer_thread(consumer_data
);
1820 ERR("Fatal error spawning consumer control thread");
1832 * Compute health status of each consumer. If one of them is zero (bad
1833 * state), we return 0.
1835 static int check_consumer_health(void)
1839 ret
= health_check_state(&kconsumer_data
.health
) &&
1840 health_check_state(&ustconsumer32_data
.health
) &&
1841 health_check_state(&ustconsumer64_data
.health
);
1843 DBG3("Health consumer check %d", ret
);
1849 * Setup necessary data for kernel tracer action.
1851 static int init_kernel_tracer(void)
1855 /* Modprobe lttng kernel modules */
1856 ret
= modprobe_lttng_control();
1861 /* Open debugfs lttng */
1862 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1863 if (kernel_tracer_fd
< 0) {
1864 DBG("Failed to open %s", module_proc_lttng
);
1869 /* Validate kernel version */
1870 ret
= kernel_validate_version(kernel_tracer_fd
);
1875 ret
= modprobe_lttng_data();
1880 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1884 modprobe_remove_lttng_control();
1885 ret
= close(kernel_tracer_fd
);
1889 kernel_tracer_fd
= -1;
1890 return LTTNG_ERR_KERN_VERSION
;
1893 ret
= close(kernel_tracer_fd
);
1899 modprobe_remove_lttng_control();
1902 WARN("No kernel tracer available");
1903 kernel_tracer_fd
= -1;
1905 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
1907 return LTTNG_ERR_KERN_NA
;
1913 * Copy consumer output from the tracing session to the domain session. The
1914 * function also applies the right modification on a per domain basis for the
1915 * trace files destination directory.
1917 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
1920 const char *dir_name
;
1921 struct consumer_output
*consumer
;
1924 assert(session
->consumer
);
1927 case LTTNG_DOMAIN_KERNEL
:
1928 DBG3("Copying tracing session consumer output in kernel session");
1929 session
->kernel_session
->consumer
=
1930 consumer_copy_output(session
->consumer
);
1931 /* Ease our life a bit for the next part */
1932 consumer
= session
->kernel_session
->consumer
;
1933 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
1935 case LTTNG_DOMAIN_UST
:
1936 DBG3("Copying tracing session consumer output in UST session");
1937 session
->ust_session
->consumer
=
1938 consumer_copy_output(session
->consumer
);
1939 /* Ease our life a bit for the next part */
1940 consumer
= session
->ust_session
->consumer
;
1941 dir_name
= DEFAULT_UST_TRACE_DIR
;
1944 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1948 /* Append correct directory to subdir */
1949 strncat(consumer
->subdir
, dir_name
,
1950 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
1951 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
1960 * Create an UST session and add it to the session ust list.
1962 static int create_ust_session(struct ltt_session
*session
,
1963 struct lttng_domain
*domain
)
1966 struct ltt_ust_session
*lus
= NULL
;
1970 assert(session
->consumer
);
1972 switch (domain
->type
) {
1973 case LTTNG_DOMAIN_UST
:
1976 ERR("Unknown UST domain on create session %d", domain
->type
);
1977 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1981 DBG("Creating UST session");
1983 lus
= trace_ust_create_session(session
->path
, session
->id
, domain
);
1985 ret
= LTTNG_ERR_UST_SESS_FAIL
;
1989 lus
->uid
= session
->uid
;
1990 lus
->gid
= session
->gid
;
1991 session
->ust_session
= lus
;
1993 /* Copy session output to the newly created UST session */
1994 ret
= copy_session_consumer(domain
->type
, session
);
1995 if (ret
!= LTTNG_OK
) {
2003 session
->ust_session
= NULL
;
2008 * Create a kernel tracer session then create the default channel.
2010 static int create_kernel_session(struct ltt_session
*session
)
2014 DBG("Creating kernel session");
2016 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2018 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2022 /* Code flow safety */
2023 assert(session
->kernel_session
);
2025 /* Copy session output to the newly created Kernel session */
2026 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2027 if (ret
!= LTTNG_OK
) {
2031 /* Create directory(ies) on local filesystem. */
2032 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2033 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2034 ret
= run_as_mkdir_recursive(
2035 session
->kernel_session
->consumer
->dst
.trace_path
,
2036 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2038 if (ret
!= -EEXIST
) {
2039 ERR("Trace directory creation error");
2045 session
->kernel_session
->uid
= session
->uid
;
2046 session
->kernel_session
->gid
= session
->gid
;
2051 trace_kernel_destroy_session(session
->kernel_session
);
2052 session
->kernel_session
= NULL
;
2057 * Count number of session permitted by uid/gid.
2059 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2062 struct ltt_session
*session
;
2064 DBG("Counting number of available session for UID %d GID %d",
2066 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2068 * Only list the sessions the user can control.
2070 if (!session_access_ok(session
, uid
, gid
)) {
2079 * Process the command requested by the lttng client within the command
2080 * context structure. This function make sure that the return structure (llm)
2081 * is set and ready for transmission before returning.
2083 * Return any error encountered or 0 for success.
2085 * "sock" is only used for special-case var. len data.
2087 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2091 int need_tracing_session
= 1;
2094 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2098 switch (cmd_ctx
->lsm
->cmd_type
) {
2099 case LTTNG_CREATE_SESSION
:
2100 case LTTNG_DESTROY_SESSION
:
2101 case LTTNG_LIST_SESSIONS
:
2102 case LTTNG_LIST_DOMAINS
:
2103 case LTTNG_START_TRACE
:
2104 case LTTNG_STOP_TRACE
:
2105 case LTTNG_DATA_AVAILABLE
:
2112 if (opt_no_kernel
&& need_domain
2113 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2115 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2117 ret
= LTTNG_ERR_KERN_NA
;
2122 /* Deny register consumer if we already have a spawned consumer. */
2123 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2124 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2125 if (kconsumer_data
.pid
> 0) {
2126 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2127 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2130 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2134 * Check for command that don't needs to allocate a returned payload. We do
2135 * this here so we don't have to make the call for no payload at each
2138 switch(cmd_ctx
->lsm
->cmd_type
) {
2139 case LTTNG_LIST_SESSIONS
:
2140 case LTTNG_LIST_TRACEPOINTS
:
2141 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2142 case LTTNG_LIST_DOMAINS
:
2143 case LTTNG_LIST_CHANNELS
:
2144 case LTTNG_LIST_EVENTS
:
2147 /* Setup lttng message with no payload */
2148 ret
= setup_lttng_msg(cmd_ctx
, 0);
2150 /* This label does not try to unlock the session */
2151 goto init_setup_error
;
2155 /* Commands that DO NOT need a session. */
2156 switch (cmd_ctx
->lsm
->cmd_type
) {
2157 case LTTNG_CREATE_SESSION
:
2158 case LTTNG_CALIBRATE
:
2159 case LTTNG_LIST_SESSIONS
:
2160 case LTTNG_LIST_TRACEPOINTS
:
2161 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2162 need_tracing_session
= 0;
2165 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2167 * We keep the session list lock across _all_ commands
2168 * for now, because the per-session lock does not
2169 * handle teardown properly.
2171 session_lock_list();
2172 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2173 if (cmd_ctx
->session
== NULL
) {
2174 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
2175 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2177 /* If no session name specified */
2178 ret
= LTTNG_ERR_SELECT_SESS
;
2182 /* Acquire lock for the session */
2183 session_lock(cmd_ctx
->session
);
2193 * Check domain type for specific "pre-action".
2195 switch (cmd_ctx
->lsm
->domain
.type
) {
2196 case LTTNG_DOMAIN_KERNEL
:
2198 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2202 /* Kernel tracer check */
2203 if (kernel_tracer_fd
== -1) {
2204 /* Basically, load kernel tracer modules */
2205 ret
= init_kernel_tracer();
2211 /* Consumer is in an ERROR state. Report back to client */
2212 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
2213 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2217 /* Need a session for kernel command */
2218 if (need_tracing_session
) {
2219 if (cmd_ctx
->session
->kernel_session
== NULL
) {
2220 ret
= create_kernel_session(cmd_ctx
->session
);
2222 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2227 /* Start the kernel consumer daemon */
2228 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2229 if (kconsumer_data
.pid
== 0 &&
2230 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
&&
2231 cmd_ctx
->session
->start_consumer
) {
2232 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2233 ret
= start_consumerd(&kconsumer_data
);
2235 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2238 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
2240 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2244 * The consumer was just spawned so we need to add the socket to
2245 * the consumer output of the session if exist.
2247 ret
= consumer_create_socket(&kconsumer_data
,
2248 cmd_ctx
->session
->kernel_session
->consumer
);
2255 case LTTNG_DOMAIN_UST
:
2257 /* Consumer is in an ERROR state. Report back to client */
2258 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
2259 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2263 if (need_tracing_session
) {
2264 /* Create UST session if none exist. */
2265 if (cmd_ctx
->session
->ust_session
== NULL
) {
2266 ret
= create_ust_session(cmd_ctx
->session
,
2267 &cmd_ctx
->lsm
->domain
);
2268 if (ret
!= LTTNG_OK
) {
2273 /* Start the UST consumer daemons */
2275 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
2276 if (consumerd64_bin
[0] != '\0' &&
2277 ustconsumer64_data
.pid
== 0 &&
2278 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
&&
2279 cmd_ctx
->session
->start_consumer
) {
2280 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2281 ret
= start_consumerd(&ustconsumer64_data
);
2283 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
2284 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
2288 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
2289 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2291 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2295 * Setup socket for consumer 64 bit. No need for atomic access
2296 * since it was set above and can ONLY be set in this thread.
2298 ret
= consumer_create_socket(&ustconsumer64_data
,
2299 cmd_ctx
->session
->ust_session
->consumer
);
2305 if (consumerd32_bin
[0] != '\0' &&
2306 ustconsumer32_data
.pid
== 0 &&
2307 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
&&
2308 cmd_ctx
->session
->start_consumer
) {
2309 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2310 ret
= start_consumerd(&ustconsumer32_data
);
2312 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
2313 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
2317 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
2318 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2320 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2324 * Setup socket for consumer 64 bit. No need for atomic access
2325 * since it was set above and can ONLY be set in this thread.
2327 ret
= consumer_create_socket(&ustconsumer32_data
,
2328 cmd_ctx
->session
->ust_session
->consumer
);
2340 /* Validate consumer daemon state when start/stop trace command */
2341 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
2342 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
2343 switch (cmd_ctx
->lsm
->domain
.type
) {
2344 case LTTNG_DOMAIN_UST
:
2345 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
2346 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2350 case LTTNG_DOMAIN_KERNEL
:
2351 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
2352 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2360 * Check that the UID or GID match that of the tracing session.
2361 * The root user can interact with all sessions.
2363 if (need_tracing_session
) {
2364 if (!session_access_ok(cmd_ctx
->session
,
2365 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2366 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
2367 ret
= LTTNG_ERR_EPERM
;
2372 /* Process by command type */
2373 switch (cmd_ctx
->lsm
->cmd_type
) {
2374 case LTTNG_ADD_CONTEXT
:
2376 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2377 cmd_ctx
->lsm
->u
.context
.channel_name
,
2378 cmd_ctx
->lsm
->u
.context
.event_name
,
2379 &cmd_ctx
->lsm
->u
.context
.ctx
);
2382 case LTTNG_DISABLE_CHANNEL
:
2384 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2385 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2388 case LTTNG_DISABLE_EVENT
:
2390 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2391 cmd_ctx
->lsm
->u
.disable
.channel_name
,
2392 cmd_ctx
->lsm
->u
.disable
.name
);
2395 case LTTNG_DISABLE_ALL_EVENT
:
2397 DBG("Disabling all events");
2399 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2400 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2403 case LTTNG_DISABLE_CONSUMER
:
2405 ret
= cmd_disable_consumer(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
);
2408 case LTTNG_ENABLE_CHANNEL
:
2410 ret
= cmd_enable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2411 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
2414 case LTTNG_ENABLE_CONSUMER
:
2417 * XXX: 0 means that this URI should be applied on the session. Should
2418 * be a DOMAIN enuam.
2420 ret
= cmd_enable_consumer(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
);
2421 if (ret
!= LTTNG_OK
) {
2425 if (cmd_ctx
->lsm
->domain
.type
== 0) {
2426 /* Add the URI for the UST session if a consumer is present. */
2427 if (cmd_ctx
->session
->ust_session
&&
2428 cmd_ctx
->session
->ust_session
->consumer
) {
2429 ret
= cmd_enable_consumer(LTTNG_DOMAIN_UST
, cmd_ctx
->session
);
2430 } else if (cmd_ctx
->session
->kernel_session
&&
2431 cmd_ctx
->session
->kernel_session
->consumer
) {
2432 ret
= cmd_enable_consumer(LTTNG_DOMAIN_KERNEL
,
2438 case LTTNG_ENABLE_EVENT
:
2440 ret
= cmd_enable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2441 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2442 &cmd_ctx
->lsm
->u
.enable
.event
, kernel_poll_pipe
[1]);
2445 case LTTNG_ENABLE_ALL_EVENT
:
2447 DBG("Enabling all events");
2449 ret
= cmd_enable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2450 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2451 cmd_ctx
->lsm
->u
.enable
.event
.type
, kernel_poll_pipe
[1]);
2454 case LTTNG_LIST_TRACEPOINTS
:
2456 struct lttng_event
*events
;
2459 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
2460 if (nb_events
< 0) {
2461 /* Return value is a negative lttng_error_code. */
2467 * Setup lttng message with payload size set to the event list size in
2468 * bytes and then copy list into the llm payload.
2470 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2476 /* Copy event list into message payload */
2477 memcpy(cmd_ctx
->llm
->payload
, events
,
2478 sizeof(struct lttng_event
) * nb_events
);
2485 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2487 struct lttng_event_field
*fields
;
2490 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
2492 if (nb_fields
< 0) {
2493 /* Return value is a negative lttng_error_code. */
2499 * Setup lttng message with payload size set to the event list size in
2500 * bytes and then copy list into the llm payload.
2502 ret
= setup_lttng_msg(cmd_ctx
,
2503 sizeof(struct lttng_event_field
) * nb_fields
);
2509 /* Copy event list into message payload */
2510 memcpy(cmd_ctx
->llm
->payload
, fields
,
2511 sizeof(struct lttng_event_field
) * nb_fields
);
2518 case LTTNG_SET_CONSUMER_URI
:
2521 struct lttng_uri
*uris
;
2523 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
2524 len
= nb_uri
* sizeof(struct lttng_uri
);
2527 ret
= LTTNG_ERR_INVALID
;
2531 uris
= zmalloc(len
);
2533 ret
= LTTNG_ERR_FATAL
;
2537 /* Receive variable len data */
2538 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
2539 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
2541 DBG("No URIs received from client... continuing");
2543 ret
= LTTNG_ERR_SESSION_FAIL
;
2547 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
2549 if (ret
!= LTTNG_OK
) {
2554 * XXX: 0 means that this URI should be applied on the session. Should
2555 * be a DOMAIN enuam.
2557 if (cmd_ctx
->lsm
->domain
.type
== 0) {
2558 /* Add the URI for the UST session if a consumer is present. */
2559 if (cmd_ctx
->session
->ust_session
&&
2560 cmd_ctx
->session
->ust_session
->consumer
) {
2561 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_UST
, cmd_ctx
->session
,
2563 } else if (cmd_ctx
->session
->kernel_session
&&
2564 cmd_ctx
->session
->kernel_session
->consumer
) {
2565 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL
,
2566 cmd_ctx
->session
, nb_uri
, uris
);
2572 case LTTNG_START_TRACE
:
2574 ret
= cmd_start_trace(cmd_ctx
->session
);
2577 case LTTNG_STOP_TRACE
:
2579 ret
= cmd_stop_trace(cmd_ctx
->session
);
2582 case LTTNG_CREATE_SESSION
:
2585 struct lttng_uri
*uris
= NULL
;
2587 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
2588 len
= nb_uri
* sizeof(struct lttng_uri
);
2591 uris
= zmalloc(len
);
2593 ret
= LTTNG_ERR_FATAL
;
2597 /* Receive variable len data */
2598 DBG("Waiting for %zu URIs from client ...", nb_uri
);
2599 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
2601 DBG("No URIs received from client... continuing");
2603 ret
= LTTNG_ERR_SESSION_FAIL
;
2607 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
2608 DBG("Creating session with ONE network URI is a bad call");
2609 ret
= LTTNG_ERR_SESSION_FAIL
;
2614 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
2619 case LTTNG_DESTROY_SESSION
:
2621 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
2623 /* Set session to NULL so we do not unlock it after free. */
2624 cmd_ctx
->session
= NULL
;
2627 case LTTNG_LIST_DOMAINS
:
2630 struct lttng_domain
*domains
;
2632 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
2634 /* Return value is a negative lttng_error_code. */
2639 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
2644 /* Copy event list into message payload */
2645 memcpy(cmd_ctx
->llm
->payload
, domains
,
2646 nb_dom
* sizeof(struct lttng_domain
));
2653 case LTTNG_LIST_CHANNELS
:
2656 struct lttng_channel
*channels
;
2658 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
2659 cmd_ctx
->session
, &channels
);
2661 /* Return value is a negative lttng_error_code. */
2666 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
2671 /* Copy event list into message payload */
2672 memcpy(cmd_ctx
->llm
->payload
, channels
,
2673 nb_chan
* sizeof(struct lttng_channel
));
2680 case LTTNG_LIST_EVENTS
:
2683 struct lttng_event
*events
= NULL
;
2685 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
2686 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
2688 /* Return value is a negative lttng_error_code. */
2693 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
2698 /* Copy event list into message payload */
2699 memcpy(cmd_ctx
->llm
->payload
, events
,
2700 nb_event
* sizeof(struct lttng_event
));
2707 case LTTNG_LIST_SESSIONS
:
2709 unsigned int nr_sessions
;
2711 session_lock_list();
2712 nr_sessions
= lttng_sessions_count(
2713 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2714 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
2716 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
2718 session_unlock_list();
2722 /* Filled the session array */
2723 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
2724 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2725 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
2727 session_unlock_list();
2732 case LTTNG_CALIBRATE
:
2734 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
2735 &cmd_ctx
->lsm
->u
.calibrate
);
2738 case LTTNG_REGISTER_CONSUMER
:
2740 struct consumer_data
*cdata
;
2742 switch (cmd_ctx
->lsm
->domain
.type
) {
2743 case LTTNG_DOMAIN_KERNEL
:
2744 cdata
= &kconsumer_data
;
2747 ret
= LTTNG_ERR_UND
;
2751 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2752 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
2755 case LTTNG_SET_FILTER
:
2757 struct lttng_filter_bytecode
*bytecode
;
2759 if (cmd_ctx
->lsm
->u
.filter
.bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
2760 ret
= LTTNG_ERR_FILTER_INVAL
;
2763 bytecode
= zmalloc(cmd_ctx
->lsm
->u
.filter
.bytecode_len
);
2765 ret
= LTTNG_ERR_FILTER_NOMEM
;
2768 /* Receive var. len. data */
2769 DBG("Receiving var len data from client ...");
2770 ret
= lttcomm_recv_unix_sock(sock
, bytecode
,
2771 cmd_ctx
->lsm
->u
.filter
.bytecode_len
);
2773 DBG("Nothing recv() from client var len data... continuing");
2775 ret
= LTTNG_ERR_FILTER_INVAL
;
2779 if (bytecode
->len
+ sizeof(*bytecode
)
2780 != cmd_ctx
->lsm
->u
.filter
.bytecode_len
) {
2782 ret
= LTTNG_ERR_FILTER_INVAL
;
2786 ret
= cmd_set_filter(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2787 cmd_ctx
->lsm
->u
.filter
.channel_name
,
2788 cmd_ctx
->lsm
->u
.filter
.event_name
,
2792 case LTTNG_DATA_AVAILABLE
:
2794 ret
= cmd_data_available(cmd_ctx
->session
);
2798 ret
= LTTNG_ERR_UND
;
2803 if (cmd_ctx
->llm
== NULL
) {
2804 DBG("Missing llm structure. Allocating one.");
2805 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2809 /* Set return code */
2810 cmd_ctx
->llm
->ret_code
= ret
;
2812 if (cmd_ctx
->session
) {
2813 session_unlock(cmd_ctx
->session
);
2815 if (need_tracing_session
) {
2816 session_unlock_list();
2823 * Thread managing health check socket.
2825 static void *thread_manage_health(void *data
)
2827 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
2828 uint32_t revents
, nb_fd
;
2829 struct lttng_poll_event events
;
2830 struct lttcomm_health_msg msg
;
2831 struct lttcomm_health_data reply
;
2833 DBG("[thread] Manage health check started");
2835 rcu_register_thread();
2837 /* Create unix socket */
2838 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
2840 ERR("Unable to create health check Unix socket");
2845 ret
= lttcomm_listen_unix_sock(sock
);
2851 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2852 * more will be added to this poll set.
2854 ret
= create_thread_poll_set(&events
, 2);
2859 /* Add the application registration socket */
2860 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
2866 DBG("Health check ready");
2868 nb_fd
= LTTNG_POLL_GETNB(&events
);
2870 /* Inifinite blocking call, waiting for transmission */
2872 ret
= lttng_poll_wait(&events
, -1);
2875 * Restart interrupted system call.
2877 if (errno
== EINTR
) {
2883 for (i
= 0; i
< nb_fd
; i
++) {
2884 /* Fetch once the poll data */
2885 revents
= LTTNG_POLL_GETEV(&events
, i
);
2886 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2888 /* Thread quit pipe has been closed. Killing thread. */
2889 ret
= check_thread_quit_pipe(pollfd
, revents
);
2895 /* Event on the registration socket */
2896 if (pollfd
== sock
) {
2897 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2898 ERR("Health socket poll error");
2904 new_sock
= lttcomm_accept_unix_sock(sock
);
2909 DBG("Receiving data from client for health...");
2910 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
2912 DBG("Nothing recv() from client... continuing");
2913 ret
= close(new_sock
);
2921 rcu_thread_online();
2923 switch (msg
.component
) {
2924 case LTTNG_HEALTH_CMD
:
2925 reply
.ret_code
= health_check_state(&health_thread_cmd
);
2927 case LTTNG_HEALTH_APP_MANAGE
:
2928 reply
.ret_code
= health_check_state(&health_thread_app_manage
);
2930 case LTTNG_HEALTH_APP_REG
:
2931 reply
.ret_code
= health_check_state(&health_thread_app_reg
);
2933 case LTTNG_HEALTH_KERNEL
:
2934 reply
.ret_code
= health_check_state(&health_thread_kernel
);
2936 case LTTNG_HEALTH_CONSUMER
:
2937 reply
.ret_code
= check_consumer_health();
2939 case LTTNG_HEALTH_ALL
:
2941 health_check_state(&health_thread_app_manage
) &&
2942 health_check_state(&health_thread_app_reg
) &&
2943 health_check_state(&health_thread_cmd
) &&
2944 health_check_state(&health_thread_kernel
) &&
2945 check_consumer_health();
2948 reply
.ret_code
= LTTNG_ERR_UND
;
2953 * Flip ret value since 0 is a success and 1 indicates a bad health for
2954 * the client where in the sessiond it is the opposite. Again, this is
2955 * just to make things easier for us poor developer which enjoy a lot
2958 if (reply
.ret_code
== 0 || reply
.ret_code
== 1) {
2959 reply
.ret_code
= !reply
.ret_code
;
2962 DBG2("Health check return value %d", reply
.ret_code
);
2964 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
2966 ERR("Failed to send health data back to client");
2969 /* End of transmission */
2970 ret
= close(new_sock
);
2980 ERR("Health error occurred in %s", __func__
);
2982 DBG("Health check thread dying");
2983 unlink(health_unix_sock_path
);
2990 if (new_sock
>= 0) {
2991 ret
= close(new_sock
);
2997 lttng_poll_clean(&events
);
2999 rcu_unregister_thread();
3004 * This thread manage all clients request using the unix client socket for
3007 static void *thread_manage_clients(void *data
)
3009 int sock
= -1, ret
, i
, pollfd
, err
= -1;
3011 uint32_t revents
, nb_fd
;
3012 struct command_ctx
*cmd_ctx
= NULL
;
3013 struct lttng_poll_event events
;
3015 DBG("[thread] Manage client started");
3017 testpoint(thread_manage_clients
);
3019 rcu_register_thread();
3021 health_code_update(&health_thread_cmd
);
3023 ret
= lttcomm_listen_unix_sock(client_sock
);
3029 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3030 * more will be added to this poll set.
3032 ret
= create_thread_poll_set(&events
, 2);
3037 /* Add the application registration socket */
3038 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
3044 * Notify parent pid that we are ready to accept command for client side.
3046 if (opt_sig_parent
) {
3047 kill(ppid
, SIGUSR1
);
3050 testpoint(thread_manage_clients_before_loop
);
3052 health_code_update(&health_thread_cmd
);
3055 DBG("Accepting client command ...");
3057 nb_fd
= LTTNG_POLL_GETNB(&events
);
3059 /* Inifinite blocking call, waiting for transmission */
3061 health_poll_update(&health_thread_cmd
);
3062 ret
= lttng_poll_wait(&events
, -1);
3063 health_poll_update(&health_thread_cmd
);
3066 * Restart interrupted system call.
3068 if (errno
== EINTR
) {
3074 for (i
= 0; i
< nb_fd
; i
++) {
3075 /* Fetch once the poll data */
3076 revents
= LTTNG_POLL_GETEV(&events
, i
);
3077 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3079 health_code_update(&health_thread_cmd
);
3081 /* Thread quit pipe has been closed. Killing thread. */
3082 ret
= check_thread_quit_pipe(pollfd
, revents
);
3088 /* Event on the registration socket */
3089 if (pollfd
== client_sock
) {
3090 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3091 ERR("Client socket poll error");
3097 DBG("Wait for client response");
3099 health_code_update(&health_thread_cmd
);
3101 sock
= lttcomm_accept_unix_sock(client_sock
);
3106 /* Set socket option for credentials retrieval */
3107 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
3112 /* Allocate context command to process the client request */
3113 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
3114 if (cmd_ctx
== NULL
) {
3115 PERROR("zmalloc cmd_ctx");
3119 /* Allocate data buffer for reception */
3120 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
3121 if (cmd_ctx
->lsm
== NULL
) {
3122 PERROR("zmalloc cmd_ctx->lsm");
3126 cmd_ctx
->llm
= NULL
;
3127 cmd_ctx
->session
= NULL
;
3129 health_code_update(&health_thread_cmd
);
3132 * Data is received from the lttng client. The struct
3133 * lttcomm_session_msg (lsm) contains the command and data request of
3136 DBG("Receiving data from client ...");
3137 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
3138 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
3140 DBG("Nothing recv() from client... continuing");
3146 clean_command_ctx(&cmd_ctx
);
3150 health_code_update(&health_thread_cmd
);
3152 // TODO: Validate cmd_ctx including sanity check for
3153 // security purpose.
3155 rcu_thread_online();
3157 * This function dispatch the work to the kernel or userspace tracer
3158 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3159 * informations for the client. The command context struct contains
3160 * everything this function may needs.
3162 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
3163 rcu_thread_offline();
3173 * TODO: Inform client somehow of the fatal error. At
3174 * this point, ret < 0 means that a zmalloc failed
3175 * (ENOMEM). Error detected but still accept
3176 * command, unless a socket error has been
3179 clean_command_ctx(&cmd_ctx
);
3183 health_code_update(&health_thread_cmd
);
3185 DBG("Sending response (size: %d, retcode: %s)",
3186 cmd_ctx
->lttng_msg_size
,
3187 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
3188 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
3190 ERR("Failed to send data back to client");
3193 /* End of transmission */
3200 clean_command_ctx(&cmd_ctx
);
3202 health_code_update(&health_thread_cmd
);
3208 health_error(&health_thread_cmd
);
3209 ERR("Health error occurred in %s", __func__
);
3211 health_exit(&health_thread_cmd
);
3213 DBG("Client thread dying");
3214 unlink(client_unix_sock_path
);
3215 if (client_sock
>= 0) {
3216 ret
= close(client_sock
);
3228 lttng_poll_clean(&events
);
3229 clean_command_ctx(&cmd_ctx
);
3231 rcu_unregister_thread();
3237 * usage function on stderr
3239 static void usage(void)
3241 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
3242 fprintf(stderr
, " -h, --help Display this usage.\n");
3243 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
3244 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
3245 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
3246 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
3247 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
3248 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
3249 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
3250 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
3251 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
3252 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
3253 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
3254 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
3255 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
3256 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
3257 fprintf(stderr
, " -V, --version Show version number.\n");
3258 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
3259 fprintf(stderr
, " -q, --quiet No output at all.\n");
3260 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3261 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
3262 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
3266 * daemon argument parsing
3268 static int parse_args(int argc
, char **argv
)
3272 static struct option long_options
[] = {
3273 { "client-sock", 1, 0, 'c' },
3274 { "apps-sock", 1, 0, 'a' },
3275 { "kconsumerd-cmd-sock", 1, 0, 'C' },
3276 { "kconsumerd-err-sock", 1, 0, 'E' },
3277 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
3278 { "ustconsumerd32-err-sock", 1, 0, 'H' },
3279 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
3280 { "ustconsumerd64-err-sock", 1, 0, 'F' },
3281 { "consumerd32-path", 1, 0, 'u' },
3282 { "consumerd32-libdir", 1, 0, 'U' },
3283 { "consumerd64-path", 1, 0, 't' },
3284 { "consumerd64-libdir", 1, 0, 'T' },
3285 { "daemonize", 0, 0, 'd' },
3286 { "sig-parent", 0, 0, 'S' },
3287 { "help", 0, 0, 'h' },
3288 { "group", 1, 0, 'g' },
3289 { "version", 0, 0, 'V' },
3290 { "quiet", 0, 0, 'q' },
3291 { "verbose", 0, 0, 'v' },
3292 { "verbose-consumer", 0, 0, 'Z' },
3293 { "no-kernel", 0, 0, 'N' },
3298 int option_index
= 0;
3299 c
= getopt_long(argc
, argv
, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t",
3300 long_options
, &option_index
);
3307 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
3309 fprintf(stderr
, " with arg %s\n", optarg
);
3313 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3316 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3322 opt_tracing_group
= optarg
;
3328 fprintf(stdout
, "%s\n", VERSION
);
3334 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3337 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3340 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3343 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3346 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3349 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3355 lttng_opt_quiet
= 1;
3358 /* Verbose level can increase using multiple -v */
3359 lttng_opt_verbose
+= 1;
3362 opt_verbose_consumer
+= 1;
3365 consumerd32_bin
= optarg
;
3368 consumerd32_libdir
= optarg
;
3371 consumerd64_bin
= optarg
;
3374 consumerd64_libdir
= optarg
;
3377 /* Unknown option or other error.
3378 * Error is printed by getopt, just return */
3387 * Creates the two needed socket by the daemon.
3388 * apps_sock - The communication socket for all UST apps.
3389 * client_sock - The communication of the cli tool (lttng).
3391 static int init_daemon_socket(void)
3396 old_umask
= umask(0);
3398 /* Create client tool unix socket */
3399 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
3400 if (client_sock
< 0) {
3401 ERR("Create unix sock failed: %s", client_unix_sock_path
);
3406 /* File permission MUST be 660 */
3407 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3409 ERR("Set file permissions failed: %s", client_unix_sock_path
);
3414 /* Create the application unix socket */
3415 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
3416 if (apps_sock
< 0) {
3417 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
3422 /* File permission MUST be 666 */
3423 ret
= chmod(apps_unix_sock_path
,
3424 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
3426 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
3437 * Check if the global socket is available, and if a daemon is answering at the
3438 * other side. If yes, error is returned.
3440 static int check_existing_daemon(void)
3442 /* Is there anybody out there ? */
3443 if (lttng_session_daemon_alive()) {
3451 * Set the tracing group gid onto the client socket.
3453 * Race window between mkdir and chown is OK because we are going from more
3454 * permissive (root.root) to less permissive (root.tracing).
3456 static int set_permissions(char *rundir
)
3461 ret
= allowed_group();
3463 WARN("No tracing group detected");
3470 /* Set lttng run dir */
3471 ret
= chown(rundir
, 0, gid
);
3473 ERR("Unable to set group on %s", rundir
);
3477 /* Ensure tracing group can search the run dir */
3478 ret
= chmod(rundir
, S_IRWXU
| S_IXGRP
| S_IXOTH
);
3480 ERR("Unable to set permissions on %s", rundir
);
3484 /* lttng client socket path */
3485 ret
= chown(client_unix_sock_path
, 0, gid
);
3487 ERR("Unable to set group on %s", client_unix_sock_path
);
3491 /* kconsumer error socket path */
3492 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, gid
);
3494 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
3498 /* 64-bit ustconsumer error socket path */
3499 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, gid
);
3501 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
3505 /* 32-bit ustconsumer compat32 error socket path */
3506 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, gid
);
3508 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
3512 DBG("All permissions are set");
3519 * Create the lttng run directory needed for all global sockets and pipe.
3521 static int create_lttng_rundir(const char *rundir
)
3525 DBG3("Creating LTTng run directory: %s", rundir
);
3527 ret
= mkdir(rundir
, S_IRWXU
);
3529 if (errno
!= EEXIST
) {
3530 ERR("Unable to create %s", rundir
);
3542 * Setup sockets and directory needed by the kconsumerd communication with the
3545 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
3549 char path
[PATH_MAX
];
3551 switch (consumer_data
->type
) {
3552 case LTTNG_CONSUMER_KERNEL
:
3553 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
3555 case LTTNG_CONSUMER64_UST
:
3556 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
3558 case LTTNG_CONSUMER32_UST
:
3559 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
3562 ERR("Consumer type unknown");
3567 DBG2("Creating consumer directory: %s", path
);
3569 ret
= mkdir(path
, S_IRWXU
);
3571 if (errno
!= EEXIST
) {
3573 ERR("Failed to create %s", path
);
3579 /* Create the kconsumerd error unix socket */
3580 consumer_data
->err_sock
=
3581 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
3582 if (consumer_data
->err_sock
< 0) {
3583 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
3588 /* File permission MUST be 660 */
3589 ret
= chmod(consumer_data
->err_unix_sock_path
,
3590 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3592 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
3602 * Signal handler for the daemon
3604 * Simply stop all worker threads, leaving main() return gracefully after
3605 * joining all threads and calling cleanup().
3607 static void sighandler(int sig
)
3611 DBG("SIGPIPE caught");
3614 DBG("SIGINT caught");
3618 DBG("SIGTERM caught");
3627 * Setup signal handler for :
3628 * SIGINT, SIGTERM, SIGPIPE
3630 static int set_signal_handler(void)
3633 struct sigaction sa
;
3636 if ((ret
= sigemptyset(&sigset
)) < 0) {
3637 PERROR("sigemptyset");
3641 sa
.sa_handler
= sighandler
;
3642 sa
.sa_mask
= sigset
;
3644 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3645 PERROR("sigaction");
3649 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3650 PERROR("sigaction");
3654 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3655 PERROR("sigaction");
3659 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3665 * Set open files limit to unlimited. This daemon can open a large number of
3666 * file descriptors in order to consumer multiple kernel traces.
3668 static void set_ulimit(void)
3673 /* The kernel does not allowed an infinite limit for open files */
3674 lim
.rlim_cur
= 65535;
3675 lim
.rlim_max
= 65535;
3677 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3679 PERROR("failed to set open files limit");
3686 int main(int argc
, char **argv
)
3690 const char *home_path
;
3692 init_kernel_workarounds();
3694 rcu_register_thread();
3696 setup_consumerd_path();
3698 /* Parse arguments */
3700 if ((ret
= parse_args(argc
, argv
) < 0)) {
3710 * child: setsid, close FD 0, 1, 2, chdir /
3711 * parent: exit (if fork is successful)
3719 * We are in the child. Make sure all other file
3720 * descriptors are closed, in case we are called with
3721 * more opened file descriptors than the standard ones.
3723 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
3728 /* Create thread quit pipe */
3729 if ((ret
= init_thread_quit_pipe()) < 0) {
3733 /* Check if daemon is UID = 0 */
3734 is_root
= !getuid();
3737 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
3739 /* Create global run dir with root access */
3740 ret
= create_lttng_rundir(rundir
);
3745 if (strlen(apps_unix_sock_path
) == 0) {
3746 snprintf(apps_unix_sock_path
, PATH_MAX
,
3747 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
3750 if (strlen(client_unix_sock_path
) == 0) {
3751 snprintf(client_unix_sock_path
, PATH_MAX
,
3752 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
3755 /* Set global SHM for ust */
3756 if (strlen(wait_shm_path
) == 0) {
3757 snprintf(wait_shm_path
, PATH_MAX
,
3758 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
3761 if (strlen(health_unix_sock_path
) == 0) {
3762 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
3763 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
3766 /* Setup kernel consumerd path */
3767 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
3768 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
3769 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
3770 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
3772 DBG2("Kernel consumer err path: %s",
3773 kconsumer_data
.err_unix_sock_path
);
3774 DBG2("Kernel consumer cmd path: %s",
3775 kconsumer_data
.cmd_unix_sock_path
);
3777 home_path
= get_home_dir();
3778 if (home_path
== NULL
) {
3779 /* TODO: Add --socket PATH option */
3780 ERR("Can't get HOME directory for sockets creation.");
3786 * Create rundir from home path. This will create something like
3789 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
3795 ret
= create_lttng_rundir(rundir
);
3800 if (strlen(apps_unix_sock_path
) == 0) {
3801 snprintf(apps_unix_sock_path
, PATH_MAX
,
3802 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
3805 /* Set the cli tool unix socket path */
3806 if (strlen(client_unix_sock_path
) == 0) {
3807 snprintf(client_unix_sock_path
, PATH_MAX
,
3808 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
3811 /* Set global SHM for ust */
3812 if (strlen(wait_shm_path
) == 0) {
3813 snprintf(wait_shm_path
, PATH_MAX
,
3814 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, geteuid());
3817 /* Set health check Unix path */
3818 if (strlen(health_unix_sock_path
) == 0) {
3819 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
3820 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
3824 /* Set consumer initial state */
3825 kernel_consumerd_state
= CONSUMER_STOPPED
;
3826 ust_consumerd_state
= CONSUMER_STOPPED
;
3828 DBG("Client socket path %s", client_unix_sock_path
);
3829 DBG("Application socket path %s", apps_unix_sock_path
);
3830 DBG("LTTng run directory path: %s", rundir
);
3832 /* 32 bits consumerd path setup */
3833 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
3834 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
3835 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
3836 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
3838 DBG2("UST consumer 32 bits err path: %s",
3839 ustconsumer32_data
.err_unix_sock_path
);
3840 DBG2("UST consumer 32 bits cmd path: %s",
3841 ustconsumer32_data
.cmd_unix_sock_path
);
3843 /* 64 bits consumerd path setup */
3844 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
3845 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
3846 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
3847 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
3849 DBG2("UST consumer 64 bits err path: %s",
3850 ustconsumer64_data
.err_unix_sock_path
);
3851 DBG2("UST consumer 64 bits cmd path: %s",
3852 ustconsumer64_data
.cmd_unix_sock_path
);
3855 * See if daemon already exist.
3857 if ((ret
= check_existing_daemon()) < 0) {
3858 ERR("Already running daemon.\n");
3860 * We do not goto exit because we must not cleanup()
3861 * because a daemon is already running.
3867 * Init UST app hash table. Alloc hash table before this point since
3868 * cleanup() can get called after that point.
3872 /* After this point, we can safely call cleanup() with "goto exit" */
3875 * These actions must be executed as root. We do that *after* setting up
3876 * the sockets path because we MUST make the check for another daemon using
3877 * those paths *before* trying to set the kernel consumer sockets and init
3881 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
3886 /* Setup kernel tracer */
3887 if (!opt_no_kernel
) {
3888 init_kernel_tracer();
3891 /* Set ulimit for open files */
3894 /* init lttng_fd tracking must be done after set_ulimit. */
3897 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
3902 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
3907 if ((ret
= set_signal_handler()) < 0) {
3911 /* Setup the needed unix socket */
3912 if ((ret
= init_daemon_socket()) < 0) {
3916 /* Set credentials to socket */
3917 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
3921 /* Get parent pid if -S, --sig-parent is specified. */
3922 if (opt_sig_parent
) {
3926 /* Setup the kernel pipe for waking up the kernel thread */
3927 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
3931 /* Setup the thread apps communication pipe. */
3932 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
3936 /* Init UST command queue. */
3937 cds_wfq_init(&ust_cmd_queue
.queue
);
3940 * Get session list pointer. This pointer MUST NOT be free(). This list is
3941 * statically declared in session.c
3943 session_list_ptr
= session_get_list();
3945 /* Set up max poll set size */
3946 lttng_poll_set_max_size();
3950 /* Init all health thread counters. */
3951 health_init(&health_thread_cmd
);
3952 health_init(&health_thread_kernel
);
3953 health_init(&health_thread_app_manage
);
3954 health_init(&health_thread_app_reg
);
3957 * Init health counters of the consumer thread. We do a quick hack here to
3958 * the state of the consumer health is fine even if the thread is not
3959 * started. This is simply to ease our life and has no cost what so ever.
3961 health_init(&kconsumer_data
.health
);
3962 health_poll_update(&kconsumer_data
.health
);
3963 health_init(&ustconsumer32_data
.health
);
3964 health_poll_update(&ustconsumer32_data
.health
);
3965 health_init(&ustconsumer64_data
.health
);
3966 health_poll_update(&ustconsumer64_data
.health
);
3968 /* Create thread to manage the client socket */
3969 ret
= pthread_create(&health_thread
, NULL
,
3970 thread_manage_health
, (void *) NULL
);
3972 PERROR("pthread_create health");
3976 /* Create thread to manage the client socket */
3977 ret
= pthread_create(&client_thread
, NULL
,
3978 thread_manage_clients
, (void *) NULL
);
3980 PERROR("pthread_create clients");
3984 /* Create thread to dispatch registration */
3985 ret
= pthread_create(&dispatch_thread
, NULL
,
3986 thread_dispatch_ust_registration
, (void *) NULL
);
3988 PERROR("pthread_create dispatch");
3992 /* Create thread to manage application registration. */
3993 ret
= pthread_create(®_apps_thread
, NULL
,
3994 thread_registration_apps
, (void *) NULL
);
3996 PERROR("pthread_create registration");
4000 /* Create thread to manage application socket */
4001 ret
= pthread_create(&apps_thread
, NULL
,
4002 thread_manage_apps
, (void *) NULL
);
4004 PERROR("pthread_create apps");
4008 /* Create kernel thread to manage kernel event */
4009 ret
= pthread_create(&kernel_thread
, NULL
,
4010 thread_manage_kernel
, (void *) NULL
);
4012 PERROR("pthread_create kernel");
4016 ret
= pthread_join(kernel_thread
, &status
);
4018 PERROR("pthread_join");
4019 goto error
; /* join error, exit without cleanup */
4023 ret
= pthread_join(apps_thread
, &status
);
4025 PERROR("pthread_join");
4026 goto error
; /* join error, exit without cleanup */
4030 ret
= pthread_join(reg_apps_thread
, &status
);
4032 PERROR("pthread_join");
4033 goto error
; /* join error, exit without cleanup */
4037 ret
= pthread_join(dispatch_thread
, &status
);
4039 PERROR("pthread_join");
4040 goto error
; /* join error, exit without cleanup */
4044 ret
= pthread_join(client_thread
, &status
);
4046 PERROR("pthread_join");
4047 goto error
; /* join error, exit without cleanup */
4050 ret
= join_consumer_thread(&kconsumer_data
);
4052 PERROR("join_consumer");
4053 goto error
; /* join error, exit without cleanup */
4060 * cleanup() is called when no other thread is running.
4062 rcu_thread_online();
4064 rcu_thread_offline();
4065 rcu_unregister_thread();