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
);
417 DBG("Cleaning up all sessions");
419 /* Destroy session list mutex */
420 if (session_list_ptr
!= NULL
) {
421 pthread_mutex_destroy(&session_list_ptr
->lock
);
423 /* Cleanup ALL session */
424 cds_list_for_each_entry_safe(sess
, stmp
,
425 &session_list_ptr
->head
, list
) {
426 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
430 DBG("Closing all UST sockets");
431 ust_app_clean_list();
433 if (is_root
&& !opt_no_kernel
) {
434 DBG2("Closing kernel fd");
435 if (kernel_tracer_fd
>= 0) {
436 ret
= close(kernel_tracer_fd
);
441 DBG("Unloading kernel modules");
442 modprobe_remove_lttng_all();
445 utils_close_pipe(kernel_poll_pipe
);
446 utils_close_pipe(apps_cmd_pipe
);
449 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
450 "Matthew, BEET driven development works!%c[%dm",
451 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
456 * Send data on a unix socket using the liblttsessiondcomm API.
458 * Return lttcomm error code.
460 static int send_unix_sock(int sock
, void *buf
, size_t len
)
462 /* Check valid length */
467 return lttcomm_send_unix_sock(sock
, buf
, len
);
471 * Free memory of a command context structure.
473 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
475 DBG("Clean command context structure");
477 if ((*cmd_ctx
)->llm
) {
478 free((*cmd_ctx
)->llm
);
480 if ((*cmd_ctx
)->lsm
) {
481 free((*cmd_ctx
)->lsm
);
489 * Notify UST applications using the shm mmap futex.
491 static int notify_ust_apps(int active
)
495 DBG("Notifying applications of session daemon state: %d", active
);
497 /* See shm.c for this call implying mmap, shm and futex calls */
498 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
499 if (wait_shm_mmap
== NULL
) {
503 /* Wake waiting process */
504 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
506 /* Apps notified successfully */
514 * Setup the outgoing data buffer for the response (llm) by allocating the
515 * right amount of memory and copying the original information from the lsm
518 * Return total size of the buffer pointed by buf.
520 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
526 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
527 if (cmd_ctx
->llm
== NULL
) {
533 /* Copy common data */
534 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
535 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
537 cmd_ctx
->llm
->data_size
= size
;
538 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
547 * Update the kernel poll set of all channel fd available over all tracing
548 * session. Add the wakeup pipe at the end of the set.
550 static int update_kernel_poll(struct lttng_poll_event
*events
)
553 struct ltt_session
*session
;
554 struct ltt_kernel_channel
*channel
;
556 DBG("Updating kernel poll set");
559 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
560 session_lock(session
);
561 if (session
->kernel_session
== NULL
) {
562 session_unlock(session
);
566 cds_list_for_each_entry(channel
,
567 &session
->kernel_session
->channel_list
.head
, list
) {
568 /* Add channel fd to the kernel poll set */
569 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
571 session_unlock(session
);
574 DBG("Channel fd %d added to kernel set", channel
->fd
);
576 session_unlock(session
);
578 session_unlock_list();
583 session_unlock_list();
588 * Find the channel fd from 'fd' over all tracing session. When found, check
589 * for new channel stream and send those stream fds to the kernel consumer.
591 * Useful for CPU hotplug feature.
593 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
596 struct ltt_session
*session
;
597 struct ltt_kernel_session
*ksess
;
598 struct ltt_kernel_channel
*channel
;
600 DBG("Updating kernel streams for channel fd %d", fd
);
603 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
604 session_lock(session
);
605 if (session
->kernel_session
== NULL
) {
606 session_unlock(session
);
609 ksess
= session
->kernel_session
;
611 cds_list_for_each_entry(channel
, &ksess
->channel_list
.head
, list
) {
612 if (channel
->fd
== fd
) {
613 DBG("Channel found, updating kernel streams");
614 ret
= kernel_open_channel_stream(channel
);
620 * Have we already sent fds to the consumer? If yes, it means
621 * that tracing is started so it is safe to send our updated
624 if (ksess
->consumer_fds_sent
== 1 && ksess
->consumer
!= NULL
) {
625 struct lttng_ht_iter iter
;
626 struct consumer_socket
*socket
;
629 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
630 &iter
.iter
, socket
, node
.node
) {
631 /* Code flow error */
632 assert(socket
->fd
>= 0);
634 pthread_mutex_lock(socket
->lock
);
635 ret
= kernel_consumer_send_channel_stream(socket
->fd
,
637 pthread_mutex_unlock(socket
->lock
);
646 session_unlock(session
);
648 session_unlock_list();
652 session_unlock(session
);
653 session_unlock_list();
658 * For each tracing session, update newly registered apps.
660 static void update_ust_app(int app_sock
)
662 struct ltt_session
*sess
, *stmp
;
666 /* For all tracing session(s) */
667 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
669 if (sess
->ust_session
) {
670 ust_app_global_update(sess
->ust_session
, app_sock
);
672 session_unlock(sess
);
675 session_unlock_list();
679 * This thread manage event coming from the kernel.
681 * Features supported in this thread:
684 static void *thread_manage_kernel(void *data
)
686 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
687 uint32_t revents
, nb_fd
;
689 struct lttng_poll_event events
;
691 DBG("Thread manage kernel started");
693 testpoint(thread_manage_kernel
);
695 health_code_update(&health_thread_kernel
);
697 testpoint(thread_manage_kernel_before_loop
);
699 ret
= create_thread_poll_set(&events
, 2);
701 goto error_poll_create
;
704 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
710 health_code_update(&health_thread_kernel
);
712 if (update_poll_flag
== 1) {
714 * Reset number of fd in the poll set. Always 2 since there is the thread
715 * quit pipe and the kernel pipe.
719 ret
= update_kernel_poll(&events
);
723 update_poll_flag
= 0;
726 nb_fd
= LTTNG_POLL_GETNB(&events
);
728 DBG("Thread kernel polling on %d fds", nb_fd
);
730 /* Zeroed the poll events */
731 lttng_poll_reset(&events
);
733 /* Poll infinite value of time */
735 health_poll_update(&health_thread_kernel
);
736 ret
= lttng_poll_wait(&events
, -1);
737 health_poll_update(&health_thread_kernel
);
740 * Restart interrupted system call.
742 if (errno
== EINTR
) {
746 } else if (ret
== 0) {
747 /* Should not happen since timeout is infinite */
748 ERR("Return value of poll is 0 with an infinite timeout.\n"
749 "This should not have happened! Continuing...");
753 for (i
= 0; i
< nb_fd
; i
++) {
754 /* Fetch once the poll data */
755 revents
= LTTNG_POLL_GETEV(&events
, i
);
756 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
758 health_code_update(&health_thread_kernel
);
760 /* Thread quit pipe has been closed. Killing thread. */
761 ret
= check_thread_quit_pipe(pollfd
, revents
);
767 /* Check for data on kernel pipe */
768 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
769 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
770 update_poll_flag
= 1;
774 * New CPU detected by the kernel. Adding kernel stream to
775 * kernel session and updating the kernel consumer
777 if (revents
& LPOLLIN
) {
778 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
784 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
785 * and unregister kernel stream at this point.
794 lttng_poll_clean(&events
);
797 health_error(&health_thread_kernel
);
798 ERR("Health error occurred in %s", __func__
);
800 health_exit(&health_thread_kernel
);
801 DBG("Kernel thread dying");
806 * Signal pthread condition of the consumer data that the thread.
808 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
810 pthread_mutex_lock(&data
->cond_mutex
);
813 * The state is set before signaling. It can be any value, it's the waiter
814 * job to correctly interpret this condition variable associated to the
815 * consumer pthread_cond.
817 * A value of 0 means that the corresponding thread of the consumer data
818 * was not started. 1 indicates that the thread has started and is ready
819 * for action. A negative value means that there was an error during the
822 data
->consumer_thread_is_ready
= state
;
823 (void) pthread_cond_signal(&data
->cond
);
825 pthread_mutex_unlock(&data
->cond_mutex
);
829 * This thread manage the consumer error sent back to the session daemon.
831 static void *thread_manage_consumer(void *data
)
833 int sock
= -1, i
, ret
, pollfd
, err
= -1;
834 uint32_t revents
, nb_fd
;
835 enum lttcomm_return_code code
;
836 struct lttng_poll_event events
;
837 struct consumer_data
*consumer_data
= data
;
839 DBG("[thread] Manage consumer started");
841 health_code_update(&consumer_data
->health
);
843 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
849 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
850 * Nothing more will be added to this poll set.
852 ret
= create_thread_poll_set(&events
, 2);
857 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
862 nb_fd
= LTTNG_POLL_GETNB(&events
);
864 health_code_update(&consumer_data
->health
);
866 /* Inifinite blocking call, waiting for transmission */
868 health_poll_update(&consumer_data
->health
);
870 testpoint(thread_manage_consumer
);
872 ret
= lttng_poll_wait(&events
, -1);
873 health_poll_update(&consumer_data
->health
);
876 * Restart interrupted system call.
878 if (errno
== EINTR
) {
884 for (i
= 0; i
< nb_fd
; i
++) {
885 /* Fetch once the poll data */
886 revents
= LTTNG_POLL_GETEV(&events
, i
);
887 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
889 health_code_update(&consumer_data
->health
);
891 /* Thread quit pipe has been closed. Killing thread. */
892 ret
= check_thread_quit_pipe(pollfd
, revents
);
898 /* Event on the registration socket */
899 if (pollfd
== consumer_data
->err_sock
) {
900 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
901 ERR("consumer err socket poll error");
907 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
912 health_code_update(&consumer_data
->health
);
914 DBG2("Receiving code from consumer err_sock");
916 /* Getting status code from kconsumerd */
917 ret
= lttcomm_recv_unix_sock(sock
, &code
,
918 sizeof(enum lttcomm_return_code
));
923 health_code_update(&consumer_data
->health
);
925 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
926 consumer_data
->cmd_sock
=
927 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
928 if (consumer_data
->cmd_sock
< 0) {
929 /* On error, signal condition and quit. */
930 signal_consumer_condition(consumer_data
, -1);
931 PERROR("consumer connect");
934 signal_consumer_condition(consumer_data
, 1);
935 DBG("Consumer command socket ready");
937 ERR("consumer error when waiting for SOCK_READY : %s",
938 lttcomm_get_readable_code(-code
));
942 /* Remove the kconsumerd error sock since we've established a connexion */
943 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
948 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
953 health_code_update(&consumer_data
->health
);
955 /* Update number of fd */
956 nb_fd
= LTTNG_POLL_GETNB(&events
);
958 /* Inifinite blocking call, waiting for transmission */
960 health_poll_update(&consumer_data
->health
);
961 ret
= lttng_poll_wait(&events
, -1);
962 health_poll_update(&consumer_data
->health
);
965 * Restart interrupted system call.
967 if (errno
== EINTR
) {
973 for (i
= 0; i
< nb_fd
; i
++) {
974 /* Fetch once the poll data */
975 revents
= LTTNG_POLL_GETEV(&events
, i
);
976 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
978 health_code_update(&consumer_data
->health
);
980 /* Thread quit pipe has been closed. Killing thread. */
981 ret
= check_thread_quit_pipe(pollfd
, revents
);
987 /* Event on the kconsumerd socket */
988 if (pollfd
== sock
) {
989 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
990 ERR("consumer err socket second poll error");
996 health_code_update(&consumer_data
->health
);
998 /* Wait for any kconsumerd error */
999 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1000 sizeof(enum lttcomm_return_code
));
1002 ERR("consumer closed the command socket");
1006 ERR("consumer return code : %s", lttcomm_get_readable_code(-code
));
1010 /* Immediately set the consumerd state to stopped */
1011 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1012 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1013 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1014 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1015 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1017 /* Code flow error... */
1021 if (consumer_data
->err_sock
>= 0) {
1022 ret
= close(consumer_data
->err_sock
);
1027 if (consumer_data
->cmd_sock
>= 0) {
1028 ret
= close(consumer_data
->cmd_sock
);
1040 unlink(consumer_data
->err_unix_sock_path
);
1041 unlink(consumer_data
->cmd_unix_sock_path
);
1042 consumer_data
->pid
= 0;
1044 lttng_poll_clean(&events
);
1048 health_error(&consumer_data
->health
);
1049 ERR("Health error occurred in %s", __func__
);
1051 health_exit(&consumer_data
->health
);
1052 DBG("consumer thread cleanup completed");
1058 * This thread manage application communication.
1060 static void *thread_manage_apps(void *data
)
1062 int i
, ret
, pollfd
, err
= -1;
1063 uint32_t revents
, nb_fd
;
1064 struct ust_command ust_cmd
;
1065 struct lttng_poll_event events
;
1067 DBG("[thread] Manage application started");
1069 testpoint(thread_manage_apps
);
1071 rcu_register_thread();
1072 rcu_thread_online();
1074 health_code_update(&health_thread_app_manage
);
1076 ret
= create_thread_poll_set(&events
, 2);
1078 goto error_poll_create
;
1081 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1086 testpoint(thread_manage_apps_before_loop
);
1088 health_code_update(&health_thread_app_manage
);
1091 /* Zeroed the events structure */
1092 lttng_poll_reset(&events
);
1094 nb_fd
= LTTNG_POLL_GETNB(&events
);
1096 DBG("Apps thread polling on %d fds", nb_fd
);
1098 /* Inifinite blocking call, waiting for transmission */
1100 health_poll_update(&health_thread_app_manage
);
1101 ret
= lttng_poll_wait(&events
, -1);
1102 health_poll_update(&health_thread_app_manage
);
1105 * Restart interrupted system call.
1107 if (errno
== EINTR
) {
1113 for (i
= 0; i
< nb_fd
; i
++) {
1114 /* Fetch once the poll data */
1115 revents
= LTTNG_POLL_GETEV(&events
, i
);
1116 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1118 health_code_update(&health_thread_app_manage
);
1120 /* Thread quit pipe has been closed. Killing thread. */
1121 ret
= check_thread_quit_pipe(pollfd
, revents
);
1127 /* Inspect the apps cmd pipe */
1128 if (pollfd
== apps_cmd_pipe
[0]) {
1129 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1130 ERR("Apps command pipe error");
1132 } else if (revents
& LPOLLIN
) {
1134 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
1135 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
1136 PERROR("read apps cmd pipe");
1140 health_code_update(&health_thread_app_manage
);
1142 /* Register applicaton to the session daemon */
1143 ret
= ust_app_register(&ust_cmd
.reg_msg
,
1145 if (ret
== -ENOMEM
) {
1147 } else if (ret
< 0) {
1151 health_code_update(&health_thread_app_manage
);
1154 * Validate UST version compatibility.
1156 ret
= ust_app_validate_version(ust_cmd
.sock
);
1159 * Add channel(s) and event(s) to newly registered apps
1160 * from lttng global UST domain.
1162 update_ust_app(ust_cmd
.sock
);
1165 health_code_update(&health_thread_app_manage
);
1167 ret
= ust_app_register_done(ust_cmd
.sock
);
1170 * If the registration is not possible, we simply
1171 * unregister the apps and continue
1173 ust_app_unregister(ust_cmd
.sock
);
1176 * We just need here to monitor the close of the UST
1177 * socket and poll set monitor those by default.
1178 * Listen on POLLIN (even if we never expect any
1179 * data) to ensure that hangup wakes us.
1181 ret
= lttng_poll_add(&events
, ust_cmd
.sock
, LPOLLIN
);
1186 DBG("Apps with sock %d added to poll set",
1190 health_code_update(&health_thread_app_manage
);
1196 * At this point, we know that a registered application made
1197 * the event at poll_wait.
1199 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1200 /* Removing from the poll set */
1201 ret
= lttng_poll_del(&events
, pollfd
);
1206 /* Socket closed on remote end. */
1207 ust_app_unregister(pollfd
);
1212 health_code_update(&health_thread_app_manage
);
1218 lttng_poll_clean(&events
);
1221 health_error(&health_thread_app_manage
);
1222 ERR("Health error occurred in %s", __func__
);
1224 health_exit(&health_thread_app_manage
);
1225 DBG("Application communication apps thread cleanup complete");
1226 rcu_thread_offline();
1227 rcu_unregister_thread();
1232 * Dispatch request from the registration threads to the application
1233 * communication thread.
1235 static void *thread_dispatch_ust_registration(void *data
)
1238 struct cds_wfq_node
*node
;
1239 struct ust_command
*ust_cmd
= NULL
;
1241 DBG("[thread] Dispatch UST command started");
1243 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1244 /* Atomically prepare the queue futex */
1245 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1248 /* Dequeue command for registration */
1249 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1251 DBG("Woken up but nothing in the UST command queue");
1252 /* Continue thread execution */
1256 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1258 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1259 " gid:%d sock:%d name:%s (version %d.%d)",
1260 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1261 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1262 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1263 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1265 * Inform apps thread of the new application registration. This
1266 * call is blocking so we can be assured that the data will be read
1267 * at some point in time or wait to the end of the world :)
1269 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1270 sizeof(struct ust_command
));
1272 PERROR("write apps cmd pipe");
1273 if (errno
== EBADF
) {
1275 * We can't inform the application thread to process
1276 * registration. We will exit or else application
1277 * registration will not occur and tracing will never
1284 } while (node
!= NULL
);
1286 /* Futex wait on queue. Blocking call on futex() */
1287 futex_nto1_wait(&ust_cmd_queue
.futex
);
1291 DBG("Dispatch thread dying");
1296 * This thread manage application registration.
1298 static void *thread_registration_apps(void *data
)
1300 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1301 uint32_t revents
, nb_fd
;
1302 struct lttng_poll_event events
;
1304 * Get allocated in this thread, enqueued to a global queue, dequeued and
1305 * freed in the manage apps thread.
1307 struct ust_command
*ust_cmd
= NULL
;
1309 DBG("[thread] Manage application registration started");
1311 testpoint(thread_registration_apps
);
1313 ret
= lttcomm_listen_unix_sock(apps_sock
);
1319 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1320 * more will be added to this poll set.
1322 ret
= create_thread_poll_set(&events
, 2);
1324 goto error_create_poll
;
1327 /* Add the application registration socket */
1328 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1330 goto error_poll_add
;
1333 /* Notify all applications to register */
1334 ret
= notify_ust_apps(1);
1336 ERR("Failed to notify applications or create the wait shared memory.\n"
1337 "Execution continues but there might be problem for already\n"
1338 "running applications that wishes to register.");
1342 DBG("Accepting application registration");
1344 nb_fd
= LTTNG_POLL_GETNB(&events
);
1346 /* Inifinite blocking call, waiting for transmission */
1348 health_poll_update(&health_thread_app_reg
);
1349 ret
= lttng_poll_wait(&events
, -1);
1350 health_poll_update(&health_thread_app_reg
);
1353 * Restart interrupted system call.
1355 if (errno
== EINTR
) {
1361 for (i
= 0; i
< nb_fd
; i
++) {
1362 health_code_update(&health_thread_app_reg
);
1364 /* Fetch once the poll data */
1365 revents
= LTTNG_POLL_GETEV(&events
, i
);
1366 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1368 /* Thread quit pipe has been closed. Killing thread. */
1369 ret
= check_thread_quit_pipe(pollfd
, revents
);
1375 /* Event on the registration socket */
1376 if (pollfd
== apps_sock
) {
1377 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1378 ERR("Register apps socket poll error");
1380 } else if (revents
& LPOLLIN
) {
1381 sock
= lttcomm_accept_unix_sock(apps_sock
);
1386 /* Create UST registration command for enqueuing */
1387 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1388 if (ust_cmd
== NULL
) {
1389 PERROR("ust command zmalloc");
1394 * Using message-based transmissions to ensure we don't
1395 * have to deal with partially received messages.
1397 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1399 ERR("Exhausted file descriptors allowed for applications.");
1408 health_code_update(&health_thread_app_reg
);
1409 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1410 sizeof(struct ust_register_msg
));
1411 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1413 PERROR("lttcomm_recv_unix_sock register apps");
1415 ERR("Wrong size received on apps register");
1422 lttng_fd_put(LTTNG_FD_APPS
, 1);
1426 health_code_update(&health_thread_app_reg
);
1428 ust_cmd
->sock
= sock
;
1431 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1432 " gid:%d sock:%d name:%s (version %d.%d)",
1433 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1434 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1435 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1436 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1439 * Lock free enqueue the registration request. The red pill
1440 * has been taken! This apps will be part of the *system*.
1442 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1445 * Wake the registration queue futex. Implicit memory
1446 * barrier with the exchange in cds_wfq_enqueue.
1448 futex_nto1_wake(&ust_cmd_queue
.futex
);
1457 health_error(&health_thread_app_reg
);
1458 ERR("Health error occurred in %s", __func__
);
1460 health_exit(&health_thread_app_reg
);
1462 /* Notify that the registration thread is gone */
1465 if (apps_sock
>= 0) {
1466 ret
= close(apps_sock
);
1476 lttng_fd_put(LTTNG_FD_APPS
, 1);
1478 unlink(apps_unix_sock_path
);
1481 lttng_poll_clean(&events
);
1484 DBG("UST Registration thread cleanup complete");
1490 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1491 * exec or it will fails.
1493 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
1496 struct timespec timeout
;
1498 /* Make sure we set the readiness flag to 0 because we are NOT ready */
1499 consumer_data
->consumer_thread_is_ready
= 0;
1501 /* Setup pthread condition */
1502 ret
= pthread_condattr_init(&consumer_data
->condattr
);
1505 PERROR("pthread_condattr_init consumer data");
1510 * Set the monotonic clock in order to make sure we DO NOT jump in time
1511 * between the clock_gettime() call and the timedwait call. See bug #324
1512 * for a more details and how we noticed it.
1514 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
1517 PERROR("pthread_condattr_setclock consumer data");
1521 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
1524 PERROR("pthread_cond_init consumer data");
1528 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
1531 PERROR("pthread_create consumer");
1536 /* We are about to wait on a pthread condition */
1537 pthread_mutex_lock(&consumer_data
->cond_mutex
);
1539 /* Get time for sem_timedwait absolute timeout */
1540 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
1542 * Set the timeout for the condition timed wait even if the clock gettime
1543 * call fails since we might loop on that call and we want to avoid to
1544 * increment the timeout too many times.
1546 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
1549 * The following loop COULD be skipped in some conditions so this is why we
1550 * set ret to 0 in order to make sure at least one round of the loop is
1556 * Loop until the condition is reached or when a timeout is reached. Note
1557 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
1558 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
1559 * possible. This loop does not take any chances and works with both of
1562 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
1563 if (clock_ret
< 0) {
1564 PERROR("clock_gettime spawn consumer");
1565 /* Infinite wait for the consumerd thread to be ready */
1566 ret
= pthread_cond_wait(&consumer_data
->cond
,
1567 &consumer_data
->cond_mutex
);
1569 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
1570 &consumer_data
->cond_mutex
, &timeout
);
1574 /* Release the pthread condition */
1575 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
1579 if (ret
== ETIMEDOUT
) {
1581 * Call has timed out so we kill the kconsumerd_thread and return
1584 ERR("Condition timed out. The consumer thread was never ready."
1586 ret
= pthread_cancel(consumer_data
->thread
);
1588 PERROR("pthread_cancel consumer thread");
1591 PERROR("pthread_cond_wait failed consumer thread");
1596 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1597 if (consumer_data
->pid
== 0) {
1598 ERR("Consumerd did not start");
1599 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1602 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1611 * Join consumer thread
1613 static int join_consumer_thread(struct consumer_data
*consumer_data
)
1618 /* Consumer pid must be a real one. */
1619 if (consumer_data
->pid
> 0) {
1620 ret
= kill(consumer_data
->pid
, SIGTERM
);
1622 ERR("Error killing consumer daemon");
1625 return pthread_join(consumer_data
->thread
, &status
);
1632 * Fork and exec a consumer daemon (consumerd).
1634 * Return pid if successful else -1.
1636 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
1640 const char *consumer_to_use
;
1641 const char *verbosity
;
1644 DBG("Spawning consumerd");
1651 if (opt_verbose_consumer
) {
1652 verbosity
= "--verbose";
1654 verbosity
= "--quiet";
1656 switch (consumer_data
->type
) {
1657 case LTTNG_CONSUMER_KERNEL
:
1659 * Find out which consumerd to execute. We will first try the
1660 * 64-bit path, then the sessiond's installation directory, and
1661 * fallback on the 32-bit one,
1663 DBG3("Looking for a kernel consumer at these locations:");
1664 DBG3(" 1) %s", consumerd64_bin
);
1665 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
1666 DBG3(" 3) %s", consumerd32_bin
);
1667 if (stat(consumerd64_bin
, &st
) == 0) {
1668 DBG3("Found location #1");
1669 consumer_to_use
= consumerd64_bin
;
1670 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
1671 DBG3("Found location #2");
1672 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
1673 } else if (stat(consumerd32_bin
, &st
) == 0) {
1674 DBG3("Found location #3");
1675 consumer_to_use
= consumerd32_bin
;
1677 DBG("Could not find any valid consumerd executable");
1680 DBG("Using kernel consumer at: %s", consumer_to_use
);
1681 execl(consumer_to_use
,
1682 "lttng-consumerd", verbosity
, "-k",
1683 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1684 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1687 case LTTNG_CONSUMER64_UST
:
1689 char *tmpnew
= NULL
;
1691 if (consumerd64_libdir
[0] != '\0') {
1695 tmp
= getenv("LD_LIBRARY_PATH");
1699 tmplen
= strlen("LD_LIBRARY_PATH=")
1700 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
1701 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1706 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1707 strcat(tmpnew
, consumerd64_libdir
);
1708 if (tmp
[0] != '\0') {
1709 strcat(tmpnew
, ":");
1710 strcat(tmpnew
, tmp
);
1712 ret
= putenv(tmpnew
);
1718 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
1719 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
1720 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1721 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1723 if (consumerd64_libdir
[0] != '\0') {
1731 case LTTNG_CONSUMER32_UST
:
1733 char *tmpnew
= NULL
;
1735 if (consumerd32_libdir
[0] != '\0') {
1739 tmp
= getenv("LD_LIBRARY_PATH");
1743 tmplen
= strlen("LD_LIBRARY_PATH=")
1744 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
1745 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1750 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1751 strcat(tmpnew
, consumerd32_libdir
);
1752 if (tmp
[0] != '\0') {
1753 strcat(tmpnew
, ":");
1754 strcat(tmpnew
, tmp
);
1756 ret
= putenv(tmpnew
);
1762 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
1763 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
1764 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1765 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1767 if (consumerd32_libdir
[0] != '\0') {
1776 PERROR("unknown consumer type");
1780 PERROR("kernel start consumer exec");
1783 } else if (pid
> 0) {
1786 PERROR("start consumer fork");
1794 * Spawn the consumerd daemon and session daemon thread.
1796 static int start_consumerd(struct consumer_data
*consumer_data
)
1800 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1801 if (consumer_data
->pid
!= 0) {
1802 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1806 ret
= spawn_consumerd(consumer_data
);
1808 ERR("Spawning consumerd failed");
1809 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1813 /* Setting up the consumer_data pid */
1814 consumer_data
->pid
= ret
;
1815 DBG2("Consumer pid %d", consumer_data
->pid
);
1816 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1818 DBG2("Spawning consumer control thread");
1819 ret
= spawn_consumer_thread(consumer_data
);
1821 ERR("Fatal error spawning consumer control thread");
1833 * Compute health status of each consumer. If one of them is zero (bad
1834 * state), we return 0.
1836 static int check_consumer_health(void)
1840 ret
= health_check_state(&kconsumer_data
.health
) &&
1841 health_check_state(&ustconsumer32_data
.health
) &&
1842 health_check_state(&ustconsumer64_data
.health
);
1844 DBG3("Health consumer check %d", ret
);
1850 * Setup necessary data for kernel tracer action.
1852 static int init_kernel_tracer(void)
1856 /* Modprobe lttng kernel modules */
1857 ret
= modprobe_lttng_control();
1862 /* Open debugfs lttng */
1863 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1864 if (kernel_tracer_fd
< 0) {
1865 DBG("Failed to open %s", module_proc_lttng
);
1870 /* Validate kernel version */
1871 ret
= kernel_validate_version(kernel_tracer_fd
);
1876 ret
= modprobe_lttng_data();
1881 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1885 modprobe_remove_lttng_control();
1886 ret
= close(kernel_tracer_fd
);
1890 kernel_tracer_fd
= -1;
1891 return LTTNG_ERR_KERN_VERSION
;
1894 ret
= close(kernel_tracer_fd
);
1900 modprobe_remove_lttng_control();
1903 WARN("No kernel tracer available");
1904 kernel_tracer_fd
= -1;
1906 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
1908 return LTTNG_ERR_KERN_NA
;
1914 * Copy consumer output from the tracing session to the domain session. The
1915 * function also applies the right modification on a per domain basis for the
1916 * trace files destination directory.
1918 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
1921 const char *dir_name
;
1922 struct consumer_output
*consumer
;
1925 assert(session
->consumer
);
1928 case LTTNG_DOMAIN_KERNEL
:
1929 DBG3("Copying tracing session consumer output in kernel session");
1931 * XXX: We should audit the session creation and what this function
1932 * does "extra" in order to avoid a destroy since this function is used
1933 * in the domain session creation (kernel and ust) only. Same for UST
1936 if (session
->kernel_session
->consumer
) {
1937 consumer_destroy_output(session
->kernel_session
->consumer
);
1939 session
->kernel_session
->consumer
=
1940 consumer_copy_output(session
->consumer
);
1941 /* Ease our life a bit for the next part */
1942 consumer
= session
->kernel_session
->consumer
;
1943 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
1945 case LTTNG_DOMAIN_UST
:
1946 DBG3("Copying tracing session consumer output in UST session");
1947 if (session
->ust_session
->consumer
) {
1948 consumer_destroy_output(session
->ust_session
->consumer
);
1950 session
->ust_session
->consumer
=
1951 consumer_copy_output(session
->consumer
);
1952 /* Ease our life a bit for the next part */
1953 consumer
= session
->ust_session
->consumer
;
1954 dir_name
= DEFAULT_UST_TRACE_DIR
;
1957 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1961 /* Append correct directory to subdir */
1962 strncat(consumer
->subdir
, dir_name
,
1963 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
1964 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
1973 * Create an UST session and add it to the session ust list.
1975 static int create_ust_session(struct ltt_session
*session
,
1976 struct lttng_domain
*domain
)
1979 struct ltt_ust_session
*lus
= NULL
;
1983 assert(session
->consumer
);
1985 switch (domain
->type
) {
1986 case LTTNG_DOMAIN_UST
:
1989 ERR("Unknown UST domain on create session %d", domain
->type
);
1990 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1994 DBG("Creating UST session");
1996 lus
= trace_ust_create_session(session
->path
, session
->id
, domain
);
1998 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2002 lus
->uid
= session
->uid
;
2003 lus
->gid
= session
->gid
;
2004 session
->ust_session
= lus
;
2006 /* Copy session output to the newly created UST session */
2007 ret
= copy_session_consumer(domain
->type
, session
);
2008 if (ret
!= LTTNG_OK
) {
2016 session
->ust_session
= NULL
;
2021 * Create a kernel tracer session then create the default channel.
2023 static int create_kernel_session(struct ltt_session
*session
)
2027 DBG("Creating kernel session");
2029 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2031 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2035 /* Code flow safety */
2036 assert(session
->kernel_session
);
2038 /* Copy session output to the newly created Kernel session */
2039 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2040 if (ret
!= LTTNG_OK
) {
2044 /* Create directory(ies) on local filesystem. */
2045 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2046 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2047 ret
= run_as_mkdir_recursive(
2048 session
->kernel_session
->consumer
->dst
.trace_path
,
2049 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2051 if (ret
!= -EEXIST
) {
2052 ERR("Trace directory creation error");
2058 session
->kernel_session
->uid
= session
->uid
;
2059 session
->kernel_session
->gid
= session
->gid
;
2064 trace_kernel_destroy_session(session
->kernel_session
);
2065 session
->kernel_session
= NULL
;
2070 * Count number of session permitted by uid/gid.
2072 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2075 struct ltt_session
*session
;
2077 DBG("Counting number of available session for UID %d GID %d",
2079 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2081 * Only list the sessions the user can control.
2083 if (!session_access_ok(session
, uid
, gid
)) {
2092 * Process the command requested by the lttng client within the command
2093 * context structure. This function make sure that the return structure (llm)
2094 * is set and ready for transmission before returning.
2096 * Return any error encountered or 0 for success.
2098 * "sock" is only used for special-case var. len data.
2100 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2104 int need_tracing_session
= 1;
2107 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2111 switch (cmd_ctx
->lsm
->cmd_type
) {
2112 case LTTNG_CREATE_SESSION
:
2113 case LTTNG_DESTROY_SESSION
:
2114 case LTTNG_LIST_SESSIONS
:
2115 case LTTNG_LIST_DOMAINS
:
2116 case LTTNG_START_TRACE
:
2117 case LTTNG_STOP_TRACE
:
2118 case LTTNG_DATA_AVAILABLE
:
2125 if (opt_no_kernel
&& need_domain
2126 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2128 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2130 ret
= LTTNG_ERR_KERN_NA
;
2135 /* Deny register consumer if we already have a spawned consumer. */
2136 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2137 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2138 if (kconsumer_data
.pid
> 0) {
2139 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2140 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2143 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2147 * Check for command that don't needs to allocate a returned payload. We do
2148 * this here so we don't have to make the call for no payload at each
2151 switch(cmd_ctx
->lsm
->cmd_type
) {
2152 case LTTNG_LIST_SESSIONS
:
2153 case LTTNG_LIST_TRACEPOINTS
:
2154 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2155 case LTTNG_LIST_DOMAINS
:
2156 case LTTNG_LIST_CHANNELS
:
2157 case LTTNG_LIST_EVENTS
:
2160 /* Setup lttng message with no payload */
2161 ret
= setup_lttng_msg(cmd_ctx
, 0);
2163 /* This label does not try to unlock the session */
2164 goto init_setup_error
;
2168 /* Commands that DO NOT need a session. */
2169 switch (cmd_ctx
->lsm
->cmd_type
) {
2170 case LTTNG_CREATE_SESSION
:
2171 case LTTNG_CALIBRATE
:
2172 case LTTNG_LIST_SESSIONS
:
2173 case LTTNG_LIST_TRACEPOINTS
:
2174 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2175 need_tracing_session
= 0;
2178 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2180 * We keep the session list lock across _all_ commands
2181 * for now, because the per-session lock does not
2182 * handle teardown properly.
2184 session_lock_list();
2185 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2186 if (cmd_ctx
->session
== NULL
) {
2187 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
2188 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2190 /* If no session name specified */
2191 ret
= LTTNG_ERR_SELECT_SESS
;
2195 /* Acquire lock for the session */
2196 session_lock(cmd_ctx
->session
);
2206 * Check domain type for specific "pre-action".
2208 switch (cmd_ctx
->lsm
->domain
.type
) {
2209 case LTTNG_DOMAIN_KERNEL
:
2211 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2215 /* Kernel tracer check */
2216 if (kernel_tracer_fd
== -1) {
2217 /* Basically, load kernel tracer modules */
2218 ret
= init_kernel_tracer();
2224 /* Consumer is in an ERROR state. Report back to client */
2225 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
2226 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2230 /* Need a session for kernel command */
2231 if (need_tracing_session
) {
2232 if (cmd_ctx
->session
->kernel_session
== NULL
) {
2233 ret
= create_kernel_session(cmd_ctx
->session
);
2235 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2240 /* Start the kernel consumer daemon */
2241 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2242 if (kconsumer_data
.pid
== 0 &&
2243 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
&&
2244 cmd_ctx
->session
->start_consumer
) {
2245 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2246 ret
= start_consumerd(&kconsumer_data
);
2248 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2251 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
2253 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2257 * The consumer was just spawned so we need to add the socket to
2258 * the consumer output of the session if exist.
2260 ret
= consumer_create_socket(&kconsumer_data
,
2261 cmd_ctx
->session
->kernel_session
->consumer
);
2268 case LTTNG_DOMAIN_UST
:
2270 /* Consumer is in an ERROR state. Report back to client */
2271 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
2272 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2276 if (need_tracing_session
) {
2277 /* Create UST session if none exist. */
2278 if (cmd_ctx
->session
->ust_session
== NULL
) {
2279 ret
= create_ust_session(cmd_ctx
->session
,
2280 &cmd_ctx
->lsm
->domain
);
2281 if (ret
!= LTTNG_OK
) {
2286 /* Start the UST consumer daemons */
2288 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
2289 if (consumerd64_bin
[0] != '\0' &&
2290 ustconsumer64_data
.pid
== 0 &&
2291 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
&&
2292 cmd_ctx
->session
->start_consumer
) {
2293 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2294 ret
= start_consumerd(&ustconsumer64_data
);
2296 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
2297 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
2301 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
2302 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2304 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2308 * Setup socket for consumer 64 bit. No need for atomic access
2309 * since it was set above and can ONLY be set in this thread.
2311 ret
= consumer_create_socket(&ustconsumer64_data
,
2312 cmd_ctx
->session
->ust_session
->consumer
);
2318 if (consumerd32_bin
[0] != '\0' &&
2319 ustconsumer32_data
.pid
== 0 &&
2320 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
&&
2321 cmd_ctx
->session
->start_consumer
) {
2322 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2323 ret
= start_consumerd(&ustconsumer32_data
);
2325 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
2326 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
2330 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
2331 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2333 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2337 * Setup socket for consumer 64 bit. No need for atomic access
2338 * since it was set above and can ONLY be set in this thread.
2340 ret
= consumer_create_socket(&ustconsumer32_data
,
2341 cmd_ctx
->session
->ust_session
->consumer
);
2353 /* Validate consumer daemon state when start/stop trace command */
2354 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
2355 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
2356 switch (cmd_ctx
->lsm
->domain
.type
) {
2357 case LTTNG_DOMAIN_UST
:
2358 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
2359 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2363 case LTTNG_DOMAIN_KERNEL
:
2364 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
2365 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2373 * Check that the UID or GID match that of the tracing session.
2374 * The root user can interact with all sessions.
2376 if (need_tracing_session
) {
2377 if (!session_access_ok(cmd_ctx
->session
,
2378 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2379 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
2380 ret
= LTTNG_ERR_EPERM
;
2385 /* Process by command type */
2386 switch (cmd_ctx
->lsm
->cmd_type
) {
2387 case LTTNG_ADD_CONTEXT
:
2389 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2390 cmd_ctx
->lsm
->u
.context
.channel_name
,
2391 cmd_ctx
->lsm
->u
.context
.event_name
,
2392 &cmd_ctx
->lsm
->u
.context
.ctx
);
2395 case LTTNG_DISABLE_CHANNEL
:
2397 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2398 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2401 case LTTNG_DISABLE_EVENT
:
2403 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2404 cmd_ctx
->lsm
->u
.disable
.channel_name
,
2405 cmd_ctx
->lsm
->u
.disable
.name
);
2408 case LTTNG_DISABLE_ALL_EVENT
:
2410 DBG("Disabling all events");
2412 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2413 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2416 case LTTNG_DISABLE_CONSUMER
:
2418 ret
= cmd_disable_consumer(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
);
2421 case LTTNG_ENABLE_CHANNEL
:
2423 ret
= cmd_enable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2424 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
2427 case LTTNG_ENABLE_CONSUMER
:
2430 * XXX: 0 means that this URI should be applied on the session. Should
2431 * be a DOMAIN enuam.
2433 ret
= cmd_enable_consumer(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
);
2434 if (ret
!= LTTNG_OK
) {
2438 if (cmd_ctx
->lsm
->domain
.type
== 0) {
2439 /* Add the URI for the UST session if a consumer is present. */
2440 if (cmd_ctx
->session
->ust_session
&&
2441 cmd_ctx
->session
->ust_session
->consumer
) {
2442 ret
= cmd_enable_consumer(LTTNG_DOMAIN_UST
, cmd_ctx
->session
);
2443 } else if (cmd_ctx
->session
->kernel_session
&&
2444 cmd_ctx
->session
->kernel_session
->consumer
) {
2445 ret
= cmd_enable_consumer(LTTNG_DOMAIN_KERNEL
,
2451 case LTTNG_ENABLE_EVENT
:
2453 ret
= cmd_enable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2454 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2455 &cmd_ctx
->lsm
->u
.enable
.event
, kernel_poll_pipe
[1]);
2458 case LTTNG_ENABLE_ALL_EVENT
:
2460 DBG("Enabling all events");
2462 ret
= cmd_enable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2463 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2464 cmd_ctx
->lsm
->u
.enable
.event
.type
, kernel_poll_pipe
[1]);
2467 case LTTNG_LIST_TRACEPOINTS
:
2469 struct lttng_event
*events
;
2472 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
2473 if (nb_events
< 0) {
2474 /* Return value is a negative lttng_error_code. */
2480 * Setup lttng message with payload size set to the event list size in
2481 * bytes and then copy list into the llm payload.
2483 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2489 /* Copy event list into message payload */
2490 memcpy(cmd_ctx
->llm
->payload
, events
,
2491 sizeof(struct lttng_event
) * nb_events
);
2498 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2500 struct lttng_event_field
*fields
;
2503 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
2505 if (nb_fields
< 0) {
2506 /* Return value is a negative lttng_error_code. */
2512 * Setup lttng message with payload size set to the event list size in
2513 * bytes and then copy list into the llm payload.
2515 ret
= setup_lttng_msg(cmd_ctx
,
2516 sizeof(struct lttng_event_field
) * nb_fields
);
2522 /* Copy event list into message payload */
2523 memcpy(cmd_ctx
->llm
->payload
, fields
,
2524 sizeof(struct lttng_event_field
) * nb_fields
);
2531 case LTTNG_SET_CONSUMER_URI
:
2534 struct lttng_uri
*uris
;
2536 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
2537 len
= nb_uri
* sizeof(struct lttng_uri
);
2540 ret
= LTTNG_ERR_INVALID
;
2544 uris
= zmalloc(len
);
2546 ret
= LTTNG_ERR_FATAL
;
2550 /* Receive variable len data */
2551 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
2552 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
2554 DBG("No URIs received from client... continuing");
2556 ret
= LTTNG_ERR_SESSION_FAIL
;
2561 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
2563 if (ret
!= LTTNG_OK
) {
2569 * XXX: 0 means that this URI should be applied on the session. Should
2570 * be a DOMAIN enuam.
2572 if (cmd_ctx
->lsm
->domain
.type
== 0) {
2573 /* Add the URI for the UST session if a consumer is present. */
2574 if (cmd_ctx
->session
->ust_session
&&
2575 cmd_ctx
->session
->ust_session
->consumer
) {
2576 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_UST
, cmd_ctx
->session
,
2578 } else if (cmd_ctx
->session
->kernel_session
&&
2579 cmd_ctx
->session
->kernel_session
->consumer
) {
2580 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL
,
2581 cmd_ctx
->session
, nb_uri
, uris
);
2589 case LTTNG_START_TRACE
:
2591 ret
= cmd_start_trace(cmd_ctx
->session
);
2594 case LTTNG_STOP_TRACE
:
2596 ret
= cmd_stop_trace(cmd_ctx
->session
);
2599 case LTTNG_CREATE_SESSION
:
2602 struct lttng_uri
*uris
= NULL
;
2604 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
2605 len
= nb_uri
* sizeof(struct lttng_uri
);
2608 uris
= zmalloc(len
);
2610 ret
= LTTNG_ERR_FATAL
;
2614 /* Receive variable len data */
2615 DBG("Waiting for %zu URIs from client ...", nb_uri
);
2616 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
2618 DBG("No URIs received from client... continuing");
2620 ret
= LTTNG_ERR_SESSION_FAIL
;
2625 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
2626 DBG("Creating session with ONE network URI is a bad call");
2627 ret
= LTTNG_ERR_SESSION_FAIL
;
2633 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
2640 case LTTNG_DESTROY_SESSION
:
2642 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
2644 /* Set session to NULL so we do not unlock it after free. */
2645 cmd_ctx
->session
= NULL
;
2648 case LTTNG_LIST_DOMAINS
:
2651 struct lttng_domain
*domains
;
2653 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
2655 /* Return value is a negative lttng_error_code. */
2660 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
2665 /* Copy event list into message payload */
2666 memcpy(cmd_ctx
->llm
->payload
, domains
,
2667 nb_dom
* sizeof(struct lttng_domain
));
2674 case LTTNG_LIST_CHANNELS
:
2677 struct lttng_channel
*channels
;
2679 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
2680 cmd_ctx
->session
, &channels
);
2682 /* Return value is a negative lttng_error_code. */
2687 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
2692 /* Copy event list into message payload */
2693 memcpy(cmd_ctx
->llm
->payload
, channels
,
2694 nb_chan
* sizeof(struct lttng_channel
));
2701 case LTTNG_LIST_EVENTS
:
2704 struct lttng_event
*events
= NULL
;
2706 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
2707 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
2709 /* Return value is a negative lttng_error_code. */
2714 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
2719 /* Copy event list into message payload */
2720 memcpy(cmd_ctx
->llm
->payload
, events
,
2721 nb_event
* sizeof(struct lttng_event
));
2728 case LTTNG_LIST_SESSIONS
:
2730 unsigned int nr_sessions
;
2732 session_lock_list();
2733 nr_sessions
= lttng_sessions_count(
2734 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2735 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
2737 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
2739 session_unlock_list();
2743 /* Filled the session array */
2744 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
2745 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2746 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
2748 session_unlock_list();
2753 case LTTNG_CALIBRATE
:
2755 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
2756 &cmd_ctx
->lsm
->u
.calibrate
);
2759 case LTTNG_REGISTER_CONSUMER
:
2761 struct consumer_data
*cdata
;
2763 switch (cmd_ctx
->lsm
->domain
.type
) {
2764 case LTTNG_DOMAIN_KERNEL
:
2765 cdata
= &kconsumer_data
;
2768 ret
= LTTNG_ERR_UND
;
2772 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2773 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
2776 case LTTNG_SET_FILTER
:
2778 struct lttng_filter_bytecode
*bytecode
;
2780 if (cmd_ctx
->lsm
->u
.filter
.bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
2781 ret
= LTTNG_ERR_FILTER_INVAL
;
2784 bytecode
= zmalloc(cmd_ctx
->lsm
->u
.filter
.bytecode_len
);
2786 ret
= LTTNG_ERR_FILTER_NOMEM
;
2789 /* Receive var. len. data */
2790 DBG("Receiving var len data from client ...");
2791 ret
= lttcomm_recv_unix_sock(sock
, bytecode
,
2792 cmd_ctx
->lsm
->u
.filter
.bytecode_len
);
2794 DBG("Nothing recv() from client var len data... continuing");
2796 ret
= LTTNG_ERR_FILTER_INVAL
;
2800 if (bytecode
->len
+ sizeof(*bytecode
)
2801 != cmd_ctx
->lsm
->u
.filter
.bytecode_len
) {
2803 ret
= LTTNG_ERR_FILTER_INVAL
;
2807 ret
= cmd_set_filter(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2808 cmd_ctx
->lsm
->u
.filter
.channel_name
,
2809 cmd_ctx
->lsm
->u
.filter
.event_name
,
2813 case LTTNG_DATA_AVAILABLE
:
2815 ret
= cmd_data_available(cmd_ctx
->session
);
2819 ret
= LTTNG_ERR_UND
;
2824 if (cmd_ctx
->llm
== NULL
) {
2825 DBG("Missing llm structure. Allocating one.");
2826 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2830 /* Set return code */
2831 cmd_ctx
->llm
->ret_code
= ret
;
2833 if (cmd_ctx
->session
) {
2834 session_unlock(cmd_ctx
->session
);
2836 if (need_tracing_session
) {
2837 session_unlock_list();
2844 * Thread managing health check socket.
2846 static void *thread_manage_health(void *data
)
2848 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
2849 uint32_t revents
, nb_fd
;
2850 struct lttng_poll_event events
;
2851 struct lttcomm_health_msg msg
;
2852 struct lttcomm_health_data reply
;
2854 DBG("[thread] Manage health check started");
2856 rcu_register_thread();
2858 /* Create unix socket */
2859 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
2861 ERR("Unable to create health check Unix socket");
2866 ret
= lttcomm_listen_unix_sock(sock
);
2872 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2873 * more will be added to this poll set.
2875 ret
= create_thread_poll_set(&events
, 2);
2880 /* Add the application registration socket */
2881 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
2887 DBG("Health check ready");
2889 nb_fd
= LTTNG_POLL_GETNB(&events
);
2891 /* Inifinite blocking call, waiting for transmission */
2893 ret
= lttng_poll_wait(&events
, -1);
2896 * Restart interrupted system call.
2898 if (errno
== EINTR
) {
2904 for (i
= 0; i
< nb_fd
; i
++) {
2905 /* Fetch once the poll data */
2906 revents
= LTTNG_POLL_GETEV(&events
, i
);
2907 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2909 /* Thread quit pipe has been closed. Killing thread. */
2910 ret
= check_thread_quit_pipe(pollfd
, revents
);
2916 /* Event on the registration socket */
2917 if (pollfd
== sock
) {
2918 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2919 ERR("Health socket poll error");
2925 new_sock
= lttcomm_accept_unix_sock(sock
);
2930 DBG("Receiving data from client for health...");
2931 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
2933 DBG("Nothing recv() from client... continuing");
2934 ret
= close(new_sock
);
2942 rcu_thread_online();
2944 switch (msg
.component
) {
2945 case LTTNG_HEALTH_CMD
:
2946 reply
.ret_code
= health_check_state(&health_thread_cmd
);
2948 case LTTNG_HEALTH_APP_MANAGE
:
2949 reply
.ret_code
= health_check_state(&health_thread_app_manage
);
2951 case LTTNG_HEALTH_APP_REG
:
2952 reply
.ret_code
= health_check_state(&health_thread_app_reg
);
2954 case LTTNG_HEALTH_KERNEL
:
2955 reply
.ret_code
= health_check_state(&health_thread_kernel
);
2957 case LTTNG_HEALTH_CONSUMER
:
2958 reply
.ret_code
= check_consumer_health();
2960 case LTTNG_HEALTH_ALL
:
2962 health_check_state(&health_thread_app_manage
) &&
2963 health_check_state(&health_thread_app_reg
) &&
2964 health_check_state(&health_thread_cmd
) &&
2965 health_check_state(&health_thread_kernel
) &&
2966 check_consumer_health();
2969 reply
.ret_code
= LTTNG_ERR_UND
;
2974 * Flip ret value since 0 is a success and 1 indicates a bad health for
2975 * the client where in the sessiond it is the opposite. Again, this is
2976 * just to make things easier for us poor developer which enjoy a lot
2979 if (reply
.ret_code
== 0 || reply
.ret_code
== 1) {
2980 reply
.ret_code
= !reply
.ret_code
;
2983 DBG2("Health check return value %d", reply
.ret_code
);
2985 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
2987 ERR("Failed to send health data back to client");
2990 /* End of transmission */
2991 ret
= close(new_sock
);
3001 ERR("Health error occurred in %s", __func__
);
3003 DBG("Health check thread dying");
3004 unlink(health_unix_sock_path
);
3011 if (new_sock
>= 0) {
3012 ret
= close(new_sock
);
3018 lttng_poll_clean(&events
);
3020 rcu_unregister_thread();
3025 * This thread manage all clients request using the unix client socket for
3028 static void *thread_manage_clients(void *data
)
3030 int sock
= -1, ret
, i
, pollfd
, err
= -1;
3032 uint32_t revents
, nb_fd
;
3033 struct command_ctx
*cmd_ctx
= NULL
;
3034 struct lttng_poll_event events
;
3036 DBG("[thread] Manage client started");
3038 testpoint(thread_manage_clients
);
3040 rcu_register_thread();
3042 health_code_update(&health_thread_cmd
);
3044 ret
= lttcomm_listen_unix_sock(client_sock
);
3050 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3051 * more will be added to this poll set.
3053 ret
= create_thread_poll_set(&events
, 2);
3058 /* Add the application registration socket */
3059 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
3065 * Notify parent pid that we are ready to accept command for client side.
3067 if (opt_sig_parent
) {
3068 kill(ppid
, SIGUSR1
);
3071 testpoint(thread_manage_clients_before_loop
);
3073 health_code_update(&health_thread_cmd
);
3076 DBG("Accepting client command ...");
3078 nb_fd
= LTTNG_POLL_GETNB(&events
);
3080 /* Inifinite blocking call, waiting for transmission */
3082 health_poll_update(&health_thread_cmd
);
3083 ret
= lttng_poll_wait(&events
, -1);
3084 health_poll_update(&health_thread_cmd
);
3087 * Restart interrupted system call.
3089 if (errno
== EINTR
) {
3095 for (i
= 0; i
< nb_fd
; i
++) {
3096 /* Fetch once the poll data */
3097 revents
= LTTNG_POLL_GETEV(&events
, i
);
3098 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3100 health_code_update(&health_thread_cmd
);
3102 /* Thread quit pipe has been closed. Killing thread. */
3103 ret
= check_thread_quit_pipe(pollfd
, revents
);
3109 /* Event on the registration socket */
3110 if (pollfd
== client_sock
) {
3111 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3112 ERR("Client socket poll error");
3118 DBG("Wait for client response");
3120 health_code_update(&health_thread_cmd
);
3122 sock
= lttcomm_accept_unix_sock(client_sock
);
3127 /* Set socket option for credentials retrieval */
3128 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
3133 /* Allocate context command to process the client request */
3134 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
3135 if (cmd_ctx
== NULL
) {
3136 PERROR("zmalloc cmd_ctx");
3140 /* Allocate data buffer for reception */
3141 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
3142 if (cmd_ctx
->lsm
== NULL
) {
3143 PERROR("zmalloc cmd_ctx->lsm");
3147 cmd_ctx
->llm
= NULL
;
3148 cmd_ctx
->session
= NULL
;
3150 health_code_update(&health_thread_cmd
);
3153 * Data is received from the lttng client. The struct
3154 * lttcomm_session_msg (lsm) contains the command and data request of
3157 DBG("Receiving data from client ...");
3158 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
3159 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
3161 DBG("Nothing recv() from client... continuing");
3167 clean_command_ctx(&cmd_ctx
);
3171 health_code_update(&health_thread_cmd
);
3173 // TODO: Validate cmd_ctx including sanity check for
3174 // security purpose.
3176 rcu_thread_online();
3178 * This function dispatch the work to the kernel or userspace tracer
3179 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3180 * informations for the client. The command context struct contains
3181 * everything this function may needs.
3183 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
3184 rcu_thread_offline();
3194 * TODO: Inform client somehow of the fatal error. At
3195 * this point, ret < 0 means that a zmalloc failed
3196 * (ENOMEM). Error detected but still accept
3197 * command, unless a socket error has been
3200 clean_command_ctx(&cmd_ctx
);
3204 health_code_update(&health_thread_cmd
);
3206 DBG("Sending response (size: %d, retcode: %s)",
3207 cmd_ctx
->lttng_msg_size
,
3208 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
3209 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
3211 ERR("Failed to send data back to client");
3214 /* End of transmission */
3221 clean_command_ctx(&cmd_ctx
);
3223 health_code_update(&health_thread_cmd
);
3229 health_error(&health_thread_cmd
);
3230 ERR("Health error occurred in %s", __func__
);
3232 health_exit(&health_thread_cmd
);
3234 DBG("Client thread dying");
3235 unlink(client_unix_sock_path
);
3236 if (client_sock
>= 0) {
3237 ret
= close(client_sock
);
3249 lttng_poll_clean(&events
);
3250 clean_command_ctx(&cmd_ctx
);
3252 rcu_unregister_thread();
3258 * usage function on stderr
3260 static void usage(void)
3262 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
3263 fprintf(stderr
, " -h, --help Display this usage.\n");
3264 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
3265 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
3266 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
3267 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
3268 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
3269 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
3270 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
3271 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
3272 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
3273 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
3274 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
3275 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
3276 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
3277 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
3278 fprintf(stderr
, " -V, --version Show version number.\n");
3279 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
3280 fprintf(stderr
, " -q, --quiet No output at all.\n");
3281 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3282 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
3283 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
3287 * daemon argument parsing
3289 static int parse_args(int argc
, char **argv
)
3293 static struct option long_options
[] = {
3294 { "client-sock", 1, 0, 'c' },
3295 { "apps-sock", 1, 0, 'a' },
3296 { "kconsumerd-cmd-sock", 1, 0, 'C' },
3297 { "kconsumerd-err-sock", 1, 0, 'E' },
3298 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
3299 { "ustconsumerd32-err-sock", 1, 0, 'H' },
3300 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
3301 { "ustconsumerd64-err-sock", 1, 0, 'F' },
3302 { "consumerd32-path", 1, 0, 'u' },
3303 { "consumerd32-libdir", 1, 0, 'U' },
3304 { "consumerd64-path", 1, 0, 't' },
3305 { "consumerd64-libdir", 1, 0, 'T' },
3306 { "daemonize", 0, 0, 'd' },
3307 { "sig-parent", 0, 0, 'S' },
3308 { "help", 0, 0, 'h' },
3309 { "group", 1, 0, 'g' },
3310 { "version", 0, 0, 'V' },
3311 { "quiet", 0, 0, 'q' },
3312 { "verbose", 0, 0, 'v' },
3313 { "verbose-consumer", 0, 0, 'Z' },
3314 { "no-kernel", 0, 0, 'N' },
3319 int option_index
= 0;
3320 c
= getopt_long(argc
, argv
, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t",
3321 long_options
, &option_index
);
3328 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
3330 fprintf(stderr
, " with arg %s\n", optarg
);
3334 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3337 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3343 opt_tracing_group
= optarg
;
3349 fprintf(stdout
, "%s\n", VERSION
);
3355 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3358 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3361 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3364 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3367 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3370 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3376 lttng_opt_quiet
= 1;
3379 /* Verbose level can increase using multiple -v */
3380 lttng_opt_verbose
+= 1;
3383 opt_verbose_consumer
+= 1;
3386 consumerd32_bin
= optarg
;
3389 consumerd32_libdir
= optarg
;
3392 consumerd64_bin
= optarg
;
3395 consumerd64_libdir
= optarg
;
3398 /* Unknown option or other error.
3399 * Error is printed by getopt, just return */
3408 * Creates the two needed socket by the daemon.
3409 * apps_sock - The communication socket for all UST apps.
3410 * client_sock - The communication of the cli tool (lttng).
3412 static int init_daemon_socket(void)
3417 old_umask
= umask(0);
3419 /* Create client tool unix socket */
3420 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
3421 if (client_sock
< 0) {
3422 ERR("Create unix sock failed: %s", client_unix_sock_path
);
3427 /* File permission MUST be 660 */
3428 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3430 ERR("Set file permissions failed: %s", client_unix_sock_path
);
3435 /* Create the application unix socket */
3436 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
3437 if (apps_sock
< 0) {
3438 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
3443 /* File permission MUST be 666 */
3444 ret
= chmod(apps_unix_sock_path
,
3445 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
3447 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
3458 * Check if the global socket is available, and if a daemon is answering at the
3459 * other side. If yes, error is returned.
3461 static int check_existing_daemon(void)
3463 /* Is there anybody out there ? */
3464 if (lttng_session_daemon_alive()) {
3472 * Set the tracing group gid onto the client socket.
3474 * Race window between mkdir and chown is OK because we are going from more
3475 * permissive (root.root) to less permissive (root.tracing).
3477 static int set_permissions(char *rundir
)
3482 ret
= allowed_group();
3484 WARN("No tracing group detected");
3491 /* Set lttng run dir */
3492 ret
= chown(rundir
, 0, gid
);
3494 ERR("Unable to set group on %s", rundir
);
3498 /* Ensure tracing group can search the run dir */
3499 ret
= chmod(rundir
, S_IRWXU
| S_IXGRP
| S_IXOTH
);
3501 ERR("Unable to set permissions on %s", rundir
);
3505 /* lttng client socket path */
3506 ret
= chown(client_unix_sock_path
, 0, gid
);
3508 ERR("Unable to set group on %s", client_unix_sock_path
);
3512 /* kconsumer error socket path */
3513 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, gid
);
3515 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
3519 /* 64-bit ustconsumer error socket path */
3520 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, gid
);
3522 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
3526 /* 32-bit ustconsumer compat32 error socket path */
3527 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, gid
);
3529 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
3533 DBG("All permissions are set");
3540 * Create the lttng run directory needed for all global sockets and pipe.
3542 static int create_lttng_rundir(const char *rundir
)
3546 DBG3("Creating LTTng run directory: %s", rundir
);
3548 ret
= mkdir(rundir
, S_IRWXU
);
3550 if (errno
!= EEXIST
) {
3551 ERR("Unable to create %s", rundir
);
3563 * Setup sockets and directory needed by the kconsumerd communication with the
3566 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
3570 char path
[PATH_MAX
];
3572 switch (consumer_data
->type
) {
3573 case LTTNG_CONSUMER_KERNEL
:
3574 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
3576 case LTTNG_CONSUMER64_UST
:
3577 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
3579 case LTTNG_CONSUMER32_UST
:
3580 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
3583 ERR("Consumer type unknown");
3588 DBG2("Creating consumer directory: %s", path
);
3590 ret
= mkdir(path
, S_IRWXU
);
3592 if (errno
!= EEXIST
) {
3594 ERR("Failed to create %s", path
);
3600 /* Create the kconsumerd error unix socket */
3601 consumer_data
->err_sock
=
3602 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
3603 if (consumer_data
->err_sock
< 0) {
3604 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
3609 /* File permission MUST be 660 */
3610 ret
= chmod(consumer_data
->err_unix_sock_path
,
3611 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3613 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
3623 * Signal handler for the daemon
3625 * Simply stop all worker threads, leaving main() return gracefully after
3626 * joining all threads and calling cleanup().
3628 static void sighandler(int sig
)
3632 DBG("SIGPIPE caught");
3635 DBG("SIGINT caught");
3639 DBG("SIGTERM caught");
3648 * Setup signal handler for :
3649 * SIGINT, SIGTERM, SIGPIPE
3651 static int set_signal_handler(void)
3654 struct sigaction sa
;
3657 if ((ret
= sigemptyset(&sigset
)) < 0) {
3658 PERROR("sigemptyset");
3662 sa
.sa_handler
= sighandler
;
3663 sa
.sa_mask
= sigset
;
3665 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3666 PERROR("sigaction");
3670 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3671 PERROR("sigaction");
3675 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3676 PERROR("sigaction");
3680 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3686 * Set open files limit to unlimited. This daemon can open a large number of
3687 * file descriptors in order to consumer multiple kernel traces.
3689 static void set_ulimit(void)
3694 /* The kernel does not allowed an infinite limit for open files */
3695 lim
.rlim_cur
= 65535;
3696 lim
.rlim_max
= 65535;
3698 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3700 PERROR("failed to set open files limit");
3707 int main(int argc
, char **argv
)
3711 const char *home_path
;
3713 init_kernel_workarounds();
3715 rcu_register_thread();
3717 setup_consumerd_path();
3719 /* Parse arguments */
3721 if ((ret
= parse_args(argc
, argv
) < 0)) {
3731 * child: setsid, close FD 0, 1, 2, chdir /
3732 * parent: exit (if fork is successful)
3740 * We are in the child. Make sure all other file
3741 * descriptors are closed, in case we are called with
3742 * more opened file descriptors than the standard ones.
3744 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
3749 /* Create thread quit pipe */
3750 if ((ret
= init_thread_quit_pipe()) < 0) {
3754 /* Check if daemon is UID = 0 */
3755 is_root
= !getuid();
3758 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
3760 /* Create global run dir with root access */
3761 ret
= create_lttng_rundir(rundir
);
3766 if (strlen(apps_unix_sock_path
) == 0) {
3767 snprintf(apps_unix_sock_path
, PATH_MAX
,
3768 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
3771 if (strlen(client_unix_sock_path
) == 0) {
3772 snprintf(client_unix_sock_path
, PATH_MAX
,
3773 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
3776 /* Set global SHM for ust */
3777 if (strlen(wait_shm_path
) == 0) {
3778 snprintf(wait_shm_path
, PATH_MAX
,
3779 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
3782 if (strlen(health_unix_sock_path
) == 0) {
3783 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
3784 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
3787 /* Setup kernel consumerd path */
3788 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
3789 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
3790 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
3791 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
3793 DBG2("Kernel consumer err path: %s",
3794 kconsumer_data
.err_unix_sock_path
);
3795 DBG2("Kernel consumer cmd path: %s",
3796 kconsumer_data
.cmd_unix_sock_path
);
3798 home_path
= get_home_dir();
3799 if (home_path
== NULL
) {
3800 /* TODO: Add --socket PATH option */
3801 ERR("Can't get HOME directory for sockets creation.");
3807 * Create rundir from home path. This will create something like
3810 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
3816 ret
= create_lttng_rundir(rundir
);
3821 if (strlen(apps_unix_sock_path
) == 0) {
3822 snprintf(apps_unix_sock_path
, PATH_MAX
,
3823 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
3826 /* Set the cli tool unix socket path */
3827 if (strlen(client_unix_sock_path
) == 0) {
3828 snprintf(client_unix_sock_path
, PATH_MAX
,
3829 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
3832 /* Set global SHM for ust */
3833 if (strlen(wait_shm_path
) == 0) {
3834 snprintf(wait_shm_path
, PATH_MAX
,
3835 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, geteuid());
3838 /* Set health check Unix path */
3839 if (strlen(health_unix_sock_path
) == 0) {
3840 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
3841 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
3845 /* Set consumer initial state */
3846 kernel_consumerd_state
= CONSUMER_STOPPED
;
3847 ust_consumerd_state
= CONSUMER_STOPPED
;
3849 DBG("Client socket path %s", client_unix_sock_path
);
3850 DBG("Application socket path %s", apps_unix_sock_path
);
3851 DBG("LTTng run directory path: %s", rundir
);
3853 /* 32 bits consumerd path setup */
3854 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
3855 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
3856 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
3857 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
3859 DBG2("UST consumer 32 bits err path: %s",
3860 ustconsumer32_data
.err_unix_sock_path
);
3861 DBG2("UST consumer 32 bits cmd path: %s",
3862 ustconsumer32_data
.cmd_unix_sock_path
);
3864 /* 64 bits consumerd path setup */
3865 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
3866 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
3867 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
3868 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
3870 DBG2("UST consumer 64 bits err path: %s",
3871 ustconsumer64_data
.err_unix_sock_path
);
3872 DBG2("UST consumer 64 bits cmd path: %s",
3873 ustconsumer64_data
.cmd_unix_sock_path
);
3876 * See if daemon already exist.
3878 if ((ret
= check_existing_daemon()) < 0) {
3879 ERR("Already running daemon.\n");
3881 * We do not goto exit because we must not cleanup()
3882 * because a daemon is already running.
3888 * Init UST app hash table. Alloc hash table before this point since
3889 * cleanup() can get called after that point.
3893 /* After this point, we can safely call cleanup() with "goto exit" */
3896 * These actions must be executed as root. We do that *after* setting up
3897 * the sockets path because we MUST make the check for another daemon using
3898 * those paths *before* trying to set the kernel consumer sockets and init
3902 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
3907 /* Setup kernel tracer */
3908 if (!opt_no_kernel
) {
3909 init_kernel_tracer();
3912 /* Set ulimit for open files */
3915 /* init lttng_fd tracking must be done after set_ulimit. */
3918 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
3923 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
3928 if ((ret
= set_signal_handler()) < 0) {
3932 /* Setup the needed unix socket */
3933 if ((ret
= init_daemon_socket()) < 0) {
3937 /* Set credentials to socket */
3938 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
3942 /* Get parent pid if -S, --sig-parent is specified. */
3943 if (opt_sig_parent
) {
3947 /* Setup the kernel pipe for waking up the kernel thread */
3948 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
3952 /* Setup the thread apps communication pipe. */
3953 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
3957 /* Init UST command queue. */
3958 cds_wfq_init(&ust_cmd_queue
.queue
);
3961 * Get session list pointer. This pointer MUST NOT be free(). This list is
3962 * statically declared in session.c
3964 session_list_ptr
= session_get_list();
3966 /* Set up max poll set size */
3967 lttng_poll_set_max_size();
3971 /* Init all health thread counters. */
3972 health_init(&health_thread_cmd
);
3973 health_init(&health_thread_kernel
);
3974 health_init(&health_thread_app_manage
);
3975 health_init(&health_thread_app_reg
);
3978 * Init health counters of the consumer thread. We do a quick hack here to
3979 * the state of the consumer health is fine even if the thread is not
3980 * started. This is simply to ease our life and has no cost what so ever.
3982 health_init(&kconsumer_data
.health
);
3983 health_poll_update(&kconsumer_data
.health
);
3984 health_init(&ustconsumer32_data
.health
);
3985 health_poll_update(&ustconsumer32_data
.health
);
3986 health_init(&ustconsumer64_data
.health
);
3987 health_poll_update(&ustconsumer64_data
.health
);
3989 /* Create thread to manage the client socket */
3990 ret
= pthread_create(&health_thread
, NULL
,
3991 thread_manage_health
, (void *) NULL
);
3993 PERROR("pthread_create health");
3997 /* Create thread to manage the client socket */
3998 ret
= pthread_create(&client_thread
, NULL
,
3999 thread_manage_clients
, (void *) NULL
);
4001 PERROR("pthread_create clients");
4005 /* Create thread to dispatch registration */
4006 ret
= pthread_create(&dispatch_thread
, NULL
,
4007 thread_dispatch_ust_registration
, (void *) NULL
);
4009 PERROR("pthread_create dispatch");
4013 /* Create thread to manage application registration. */
4014 ret
= pthread_create(®_apps_thread
, NULL
,
4015 thread_registration_apps
, (void *) NULL
);
4017 PERROR("pthread_create registration");
4021 /* Create thread to manage application socket */
4022 ret
= pthread_create(&apps_thread
, NULL
,
4023 thread_manage_apps
, (void *) NULL
);
4025 PERROR("pthread_create apps");
4029 /* Create kernel thread to manage kernel event */
4030 ret
= pthread_create(&kernel_thread
, NULL
,
4031 thread_manage_kernel
, (void *) NULL
);
4033 PERROR("pthread_create kernel");
4037 ret
= pthread_join(kernel_thread
, &status
);
4039 PERROR("pthread_join");
4040 goto error
; /* join error, exit without cleanup */
4044 ret
= pthread_join(apps_thread
, &status
);
4046 PERROR("pthread_join");
4047 goto error
; /* join error, exit without cleanup */
4051 ret
= pthread_join(reg_apps_thread
, &status
);
4053 PERROR("pthread_join");
4054 goto error
; /* join error, exit without cleanup */
4058 ret
= pthread_join(dispatch_thread
, &status
);
4060 PERROR("pthread_join");
4061 goto error
; /* join error, exit without cleanup */
4065 ret
= pthread_join(client_thread
, &status
);
4067 PERROR("pthread_join");
4068 goto error
; /* join error, exit without cleanup */
4071 ret
= join_consumer_thread(&kconsumer_data
);
4073 PERROR("join_consumer");
4074 goto error
; /* join error, exit without cleanup */
4077 ret
= join_consumer_thread(&ustconsumer32_data
);
4079 PERROR("join_consumer ust32");
4080 goto error
; /* join error, exit without cleanup */
4083 ret
= join_consumer_thread(&ustconsumer64_data
);
4085 PERROR("join_consumer ust64");
4086 goto error
; /* join error, exit without cleanup */
4090 ret
= pthread_join(health_thread
, &status
);
4092 PERROR("pthread_join health thread");
4093 goto error
; /* join error, exit without cleanup */
4099 * cleanup() is called when no other thread is running.
4101 rcu_thread_online();
4103 rcu_thread_offline();
4104 rcu_unregister_thread();