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/socket.h>
41 #include <common/defaults.h>
42 #include <common/kernel-consumer/kernel-consumer.h>
43 #include <common/futex.h>
44 #include <common/relayd/relayd.h>
45 #include <common/utils.h>
47 #include "lttng-sessiond.h"
48 #include "buffer-registry.h"
55 #include "kernel-consumer.h"
59 #include "ust-consumer.h"
63 #include "testpoint.h"
64 #include "ust-thread.h"
66 #define CONSUMERD_FILE "lttng-consumerd"
69 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
72 const char *opt_tracing_group
;
73 static const char *opt_pidfile
;
74 static int opt_sig_parent
;
75 static int opt_verbose_consumer
;
76 static int opt_daemon
;
77 static int opt_no_kernel
;
78 static int is_root
; /* Set to 1 if the daemon is running as root */
79 static pid_t ppid
; /* Parent PID for --sig-parent option */
83 * Consumer daemon specific control data. Every value not initialized here is
84 * set to 0 by the static definition.
86 static struct consumer_data kconsumer_data
= {
87 .type
= LTTNG_CONSUMER_KERNEL
,
88 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
89 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
92 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
93 .lock
= PTHREAD_MUTEX_INITIALIZER
,
94 .cond
= PTHREAD_COND_INITIALIZER
,
95 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
97 static struct consumer_data ustconsumer64_data
= {
98 .type
= LTTNG_CONSUMER64_UST
,
99 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
100 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
103 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
104 .lock
= PTHREAD_MUTEX_INITIALIZER
,
105 .cond
= PTHREAD_COND_INITIALIZER
,
106 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
108 static struct consumer_data ustconsumer32_data
= {
109 .type
= LTTNG_CONSUMER32_UST
,
110 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
111 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
114 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
115 .lock
= PTHREAD_MUTEX_INITIALIZER
,
116 .cond
= PTHREAD_COND_INITIALIZER
,
117 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
120 /* Shared between threads */
121 static int dispatch_thread_exit
;
123 /* Global application Unix socket path */
124 static char apps_unix_sock_path
[PATH_MAX
];
125 /* Global client Unix socket path */
126 static char client_unix_sock_path
[PATH_MAX
];
127 /* global wait shm path for UST */
128 static char wait_shm_path
[PATH_MAX
];
129 /* Global health check unix path */
130 static char health_unix_sock_path
[PATH_MAX
];
132 /* Sockets and FDs */
133 static int client_sock
= -1;
134 static int apps_sock
= -1;
135 int kernel_tracer_fd
= -1;
136 static int kernel_poll_pipe
[2] = { -1, -1 };
139 * Quit pipe for all threads. This permits a single cancellation point
140 * for all threads when receiving an event on the pipe.
142 static int thread_quit_pipe
[2] = { -1, -1 };
145 * This pipe is used to inform the thread managing application communication
146 * that a command is queued and ready to be processed.
148 static int apps_cmd_pipe
[2] = { -1, -1 };
150 int apps_cmd_notify_pipe
[2] = { -1, -1 };
152 /* Pthread, Mutexes and Semaphores */
153 static pthread_t apps_thread
;
154 static pthread_t apps_notify_thread
;
155 static pthread_t reg_apps_thread
;
156 static pthread_t client_thread
;
157 static pthread_t kernel_thread
;
158 static pthread_t dispatch_thread
;
159 static pthread_t health_thread
;
162 * UST registration command queue. This queue is tied with a futex and uses a N
163 * wakers / 1 waiter implemented and detailed in futex.c/.h
165 * The thread_manage_apps and thread_dispatch_ust_registration interact with
166 * this queue and the wait/wake scheme.
168 static struct ust_cmd_queue ust_cmd_queue
;
171 * Pointer initialized before thread creation.
173 * This points to the tracing session list containing the session count and a
174 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
175 * MUST NOT be taken if you call a public function in session.c.
177 * The lock is nested inside the structure: session_list_ptr->lock. Please use
178 * session_lock_list and session_unlock_list for lock acquisition.
180 static struct ltt_session_list
*session_list_ptr
;
182 int ust_consumerd64_fd
= -1;
183 int ust_consumerd32_fd
= -1;
185 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
186 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
187 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
188 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
190 static const char *module_proc_lttng
= "/proc/lttng";
193 * Consumer daemon state which is changed when spawning it, killing it or in
194 * case of a fatal error.
196 enum consumerd_state
{
197 CONSUMER_STARTED
= 1,
198 CONSUMER_STOPPED
= 2,
203 * This consumer daemon state is used to validate if a client command will be
204 * able to reach the consumer. If not, the client is informed. For instance,
205 * doing a "lttng start" when the consumer state is set to ERROR will return an
206 * error to the client.
208 * The following example shows a possible race condition of this scheme:
210 * consumer thread error happens
212 * client cmd checks state -> still OK
213 * consumer thread exit, sets error
214 * client cmd try to talk to consumer
217 * However, since the consumer is a different daemon, we have no way of making
218 * sure the command will reach it safely even with this state flag. This is why
219 * we consider that up to the state validation during command processing, the
220 * command is safe. After that, we can not guarantee the correctness of the
221 * client request vis-a-vis the consumer.
223 static enum consumerd_state ust_consumerd_state
;
224 static enum consumerd_state kernel_consumerd_state
;
227 * Socket timeout for receiving and sending in seconds.
229 static int app_socket_timeout
;
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 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
289 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
295 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
307 * Check if the thread quit pipe was triggered.
309 * Return 1 if it was triggered else 0;
311 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
313 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
321 * Return group ID of the tracing group or -1 if not found.
323 static gid_t
allowed_group(void)
327 if (opt_tracing_group
) {
328 grp
= getgrnam(opt_tracing_group
);
330 grp
= getgrnam(default_tracing_group
);
340 * Init thread quit pipe.
342 * Return -1 on error or 0 if all pipes are created.
344 static int init_thread_quit_pipe(void)
348 ret
= pipe(thread_quit_pipe
);
350 PERROR("thread quit pipe");
354 for (i
= 0; i
< 2; i
++) {
355 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
367 * Stop all threads by closing the thread quit pipe.
369 static void stop_threads(void)
373 /* Stopping all threads */
374 DBG("Terminating all threads");
375 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
377 ERR("write error on thread quit pipe");
380 /* Dispatch thread */
381 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
382 futex_nto1_wake(&ust_cmd_queue
.futex
);
388 static void cleanup(void)
392 struct ltt_session
*sess
, *stmp
;
396 /* First thing first, stop all threads */
397 utils_close_pipe(thread_quit_pipe
);
400 * If opt_pidfile is undefined, the default file will be wiped when
401 * removing the rundir.
404 ret
= remove(opt_pidfile
);
406 PERROR("remove pidfile %s", opt_pidfile
);
410 DBG("Removing %s directory", rundir
);
411 ret
= asprintf(&cmd
, "rm -rf %s", rundir
);
413 ERR("asprintf failed. Something is really wrong!");
416 /* Remove lttng run directory */
419 ERR("Unable to clean %s", rundir
);
424 DBG("Cleaning up all sessions");
426 /* Destroy session list mutex */
427 if (session_list_ptr
!= NULL
) {
428 pthread_mutex_destroy(&session_list_ptr
->lock
);
430 /* Cleanup ALL session */
431 cds_list_for_each_entry_safe(sess
, stmp
,
432 &session_list_ptr
->head
, list
) {
433 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
437 DBG("Closing all UST sockets");
438 ust_app_clean_list();
439 buffer_reg_destroy_registries();
441 if (is_root
&& !opt_no_kernel
) {
442 DBG2("Closing kernel fd");
443 if (kernel_tracer_fd
>= 0) {
444 ret
= close(kernel_tracer_fd
);
449 DBG("Unloading kernel modules");
450 modprobe_remove_lttng_all();
454 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
455 "Matthew, BEET driven development works!%c[%dm",
456 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
461 * Send data on a unix socket using the liblttsessiondcomm API.
463 * Return lttcomm error code.
465 static int send_unix_sock(int sock
, void *buf
, size_t len
)
467 /* Check valid length */
472 return lttcomm_send_unix_sock(sock
, buf
, len
);
476 * Free memory of a command context structure.
478 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
480 DBG("Clean command context structure");
482 if ((*cmd_ctx
)->llm
) {
483 free((*cmd_ctx
)->llm
);
485 if ((*cmd_ctx
)->lsm
) {
486 free((*cmd_ctx
)->lsm
);
494 * Notify UST applications using the shm mmap futex.
496 static int notify_ust_apps(int active
)
500 DBG("Notifying applications of session daemon state: %d", active
);
502 /* See shm.c for this call implying mmap, shm and futex calls */
503 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
504 if (wait_shm_mmap
== NULL
) {
508 /* Wake waiting process */
509 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
511 /* Apps notified successfully */
519 * Setup the outgoing data buffer for the response (llm) by allocating the
520 * right amount of memory and copying the original information from the lsm
523 * Return total size of the buffer pointed by buf.
525 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
531 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
532 if (cmd_ctx
->llm
== NULL
) {
538 /* Copy common data */
539 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
540 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
542 cmd_ctx
->llm
->data_size
= size
;
543 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
552 * Update the kernel poll set of all channel fd available over all tracing
553 * session. Add the wakeup pipe at the end of the set.
555 static int update_kernel_poll(struct lttng_poll_event
*events
)
558 struct ltt_session
*session
;
559 struct ltt_kernel_channel
*channel
;
561 DBG("Updating kernel poll set");
564 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
565 session_lock(session
);
566 if (session
->kernel_session
== NULL
) {
567 session_unlock(session
);
571 cds_list_for_each_entry(channel
,
572 &session
->kernel_session
->channel_list
.head
, list
) {
573 /* Add channel fd to the kernel poll set */
574 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
576 session_unlock(session
);
579 DBG("Channel fd %d added to kernel set", channel
->fd
);
581 session_unlock(session
);
583 session_unlock_list();
588 session_unlock_list();
593 * Find the channel fd from 'fd' over all tracing session. When found, check
594 * for new channel stream and send those stream fds to the kernel consumer.
596 * Useful for CPU hotplug feature.
598 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
601 struct ltt_session
*session
;
602 struct ltt_kernel_session
*ksess
;
603 struct ltt_kernel_channel
*channel
;
605 DBG("Updating kernel streams for channel fd %d", fd
);
608 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
609 session_lock(session
);
610 if (session
->kernel_session
== NULL
) {
611 session_unlock(session
);
614 ksess
= session
->kernel_session
;
616 cds_list_for_each_entry(channel
, &ksess
->channel_list
.head
, list
) {
617 if (channel
->fd
== fd
) {
618 DBG("Channel found, updating kernel streams");
619 ret
= kernel_open_channel_stream(channel
);
625 * Have we already sent fds to the consumer? If yes, it means
626 * that tracing is started so it is safe to send our updated
629 if (ksess
->consumer_fds_sent
== 1 && ksess
->consumer
!= NULL
) {
630 struct lttng_ht_iter iter
;
631 struct consumer_socket
*socket
;
634 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
635 &iter
.iter
, socket
, node
.node
) {
636 /* Code flow error */
637 assert(socket
->fd
>= 0);
639 pthread_mutex_lock(socket
->lock
);
640 ret
= kernel_consumer_send_channel_stream(socket
,
642 pthread_mutex_unlock(socket
->lock
);
653 session_unlock(session
);
655 session_unlock_list();
659 session_unlock(session
);
660 session_unlock_list();
665 * For each tracing session, update newly registered apps. The session list
666 * lock MUST be acquired before calling this.
668 static void update_ust_app(int app_sock
)
670 struct ltt_session
*sess
, *stmp
;
672 /* For all tracing session(s) */
673 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
675 if (sess
->ust_session
) {
676 ust_app_global_update(sess
->ust_session
, app_sock
);
678 session_unlock(sess
);
683 * This thread manage event coming from the kernel.
685 * Features supported in this thread:
688 static void *thread_manage_kernel(void *data
)
690 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
691 uint32_t revents
, nb_fd
;
693 struct lttng_poll_event events
;
695 DBG("[thread] Thread manage kernel started");
697 health_register(HEALTH_TYPE_KERNEL
);
700 * This first step of the while is to clean this structure which could free
701 * non NULL pointers so zero it before the loop.
703 memset(&events
, 0, sizeof(events
));
705 if (testpoint(thread_manage_kernel
)) {
706 goto error_testpoint
;
709 health_code_update();
711 if (testpoint(thread_manage_kernel_before_loop
)) {
712 goto error_testpoint
;
716 health_code_update();
718 if (update_poll_flag
== 1) {
719 /* Clean events object. We are about to populate it again. */
720 lttng_poll_clean(&events
);
722 ret
= sessiond_set_thread_pollset(&events
, 2);
724 goto error_poll_create
;
727 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
732 /* This will add the available kernel channel if any. */
733 ret
= update_kernel_poll(&events
);
737 update_poll_flag
= 0;
740 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events
));
742 /* Poll infinite value of time */
745 ret
= lttng_poll_wait(&events
, -1);
749 * Restart interrupted system call.
751 if (errno
== EINTR
) {
755 } else if (ret
== 0) {
756 /* Should not happen since timeout is infinite */
757 ERR("Return value of poll is 0 with an infinite timeout.\n"
758 "This should not have happened! Continuing...");
764 for (i
= 0; i
< nb_fd
; i
++) {
765 /* Fetch once the poll data */
766 revents
= LTTNG_POLL_GETEV(&events
, i
);
767 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
769 health_code_update();
771 /* Thread quit pipe has been closed. Killing thread. */
772 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
778 /* Check for data on kernel pipe */
779 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
781 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
782 } while (ret
< 0 && errno
== EINTR
);
784 * Ret value is useless here, if this pipe gets any actions an
785 * update is required anyway.
787 update_poll_flag
= 1;
791 * New CPU detected by the kernel. Adding kernel stream to
792 * kernel session and updating the kernel consumer
794 if (revents
& LPOLLIN
) {
795 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
801 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
802 * and unregister kernel stream at this point.
811 lttng_poll_clean(&events
);
814 utils_close_pipe(kernel_poll_pipe
);
815 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
818 ERR("Health error occurred in %s", __func__
);
819 WARN("Kernel thread died unexpectedly. "
820 "Kernel tracing can continue but CPU hotplug is disabled.");
823 DBG("Kernel thread dying");
828 * Signal pthread condition of the consumer data that the thread.
830 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
832 pthread_mutex_lock(&data
->cond_mutex
);
835 * The state is set before signaling. It can be any value, it's the waiter
836 * job to correctly interpret this condition variable associated to the
837 * consumer pthread_cond.
839 * A value of 0 means that the corresponding thread of the consumer data
840 * was not started. 1 indicates that the thread has started and is ready
841 * for action. A negative value means that there was an error during the
844 data
->consumer_thread_is_ready
= state
;
845 (void) pthread_cond_signal(&data
->cond
);
847 pthread_mutex_unlock(&data
->cond_mutex
);
851 * This thread manage the consumer error sent back to the session daemon.
853 static void *thread_manage_consumer(void *data
)
855 int sock
= -1, i
, ret
, pollfd
, err
= -1;
856 uint32_t revents
, nb_fd
;
857 enum lttcomm_return_code code
;
858 struct lttng_poll_event events
;
859 struct consumer_data
*consumer_data
= data
;
861 DBG("[thread] Manage consumer started");
863 health_register(HEALTH_TYPE_CONSUMER
);
865 health_code_update();
868 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
869 * Nothing more will be added to this poll set.
871 ret
= sessiond_set_thread_pollset(&events
, 2);
877 * The error socket here is already in a listening state which was done
878 * just before spawning this thread to avoid a race between the consumer
879 * daemon exec trying to connect and the listen() call.
881 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
886 health_code_update();
888 /* Inifinite blocking call, waiting for transmission */
892 if (testpoint(thread_manage_consumer
)) {
896 ret
= lttng_poll_wait(&events
, -1);
900 * Restart interrupted system call.
902 if (errno
== EINTR
) {
910 for (i
= 0; i
< nb_fd
; i
++) {
911 /* Fetch once the poll data */
912 revents
= LTTNG_POLL_GETEV(&events
, i
);
913 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
915 health_code_update();
917 /* Thread quit pipe has been closed. Killing thread. */
918 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
924 /* Event on the registration socket */
925 if (pollfd
== consumer_data
->err_sock
) {
926 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
927 ERR("consumer err socket poll error");
933 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
939 * Set the CLOEXEC flag. Return code is useless because either way, the
942 (void) utils_set_fd_cloexec(sock
);
944 health_code_update();
946 DBG2("Receiving code from consumer err_sock");
948 /* Getting status code from kconsumerd */
949 ret
= lttcomm_recv_unix_sock(sock
, &code
,
950 sizeof(enum lttcomm_return_code
));
955 health_code_update();
957 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
958 consumer_data
->cmd_sock
=
959 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
960 if (consumer_data
->cmd_sock
< 0) {
961 /* On error, signal condition and quit. */
962 signal_consumer_condition(consumer_data
, -1);
963 PERROR("consumer connect");
966 signal_consumer_condition(consumer_data
, 1);
967 DBG("Consumer command socket ready");
969 ERR("consumer error when waiting for SOCK_READY : %s",
970 lttcomm_get_readable_code(-code
));
974 /* Remove the kconsumerd error sock since we've established a connexion */
975 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
980 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
985 health_code_update();
987 /* Inifinite blocking call, waiting for transmission */
990 ret
= lttng_poll_wait(&events
, -1);
994 * Restart interrupted system call.
996 if (errno
== EINTR
) {
1004 for (i
= 0; i
< nb_fd
; i
++) {
1005 /* Fetch once the poll data */
1006 revents
= LTTNG_POLL_GETEV(&events
, i
);
1007 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1009 health_code_update();
1011 /* Thread quit pipe has been closed. Killing thread. */
1012 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1018 /* Event on the kconsumerd socket */
1019 if (pollfd
== sock
) {
1020 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1021 ERR("consumer err socket second poll error");
1027 health_code_update();
1029 /* Wait for any kconsumerd error */
1030 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1031 sizeof(enum lttcomm_return_code
));
1033 ERR("consumer closed the command socket");
1037 ERR("consumer return code : %s", lttcomm_get_readable_code(-code
));
1041 /* Immediately set the consumerd state to stopped */
1042 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1043 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1044 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1045 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1046 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1048 /* Code flow error... */
1052 if (consumer_data
->err_sock
>= 0) {
1053 ret
= close(consumer_data
->err_sock
);
1058 if (consumer_data
->cmd_sock
>= 0) {
1059 ret
= close(consumer_data
->cmd_sock
);
1071 unlink(consumer_data
->err_unix_sock_path
);
1072 unlink(consumer_data
->cmd_unix_sock_path
);
1073 consumer_data
->pid
= 0;
1075 lttng_poll_clean(&events
);
1079 ERR("Health error occurred in %s", __func__
);
1081 health_unregister();
1082 DBG("consumer thread cleanup completed");
1088 * This thread manage application communication.
1090 static void *thread_manage_apps(void *data
)
1092 int i
, ret
, pollfd
, err
= -1;
1093 uint32_t revents
, nb_fd
;
1094 struct lttng_poll_event events
;
1096 DBG("[thread] Manage application started");
1098 rcu_register_thread();
1099 rcu_thread_online();
1101 health_register(HEALTH_TYPE_APP_MANAGE
);
1103 if (testpoint(thread_manage_apps
)) {
1104 goto error_testpoint
;
1107 health_code_update();
1109 ret
= sessiond_set_thread_pollset(&events
, 2);
1111 goto error_poll_create
;
1114 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1119 if (testpoint(thread_manage_apps_before_loop
)) {
1123 health_code_update();
1126 DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events
));
1128 /* Inifinite blocking call, waiting for transmission */
1130 health_poll_entry();
1131 ret
= lttng_poll_wait(&events
, -1);
1135 * Restart interrupted system call.
1137 if (errno
== EINTR
) {
1145 for (i
= 0; i
< nb_fd
; i
++) {
1146 /* Fetch once the poll data */
1147 revents
= LTTNG_POLL_GETEV(&events
, i
);
1148 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1150 health_code_update();
1152 /* Thread quit pipe has been closed. Killing thread. */
1153 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1159 /* Inspect the apps cmd pipe */
1160 if (pollfd
== apps_cmd_pipe
[0]) {
1161 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1162 ERR("Apps command pipe error");
1164 } else if (revents
& LPOLLIN
) {
1169 ret
= read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1170 } while (ret
< 0 && errno
== EINTR
);
1171 if (ret
< 0 || ret
< sizeof(sock
)) {
1172 PERROR("read apps cmd pipe");
1176 health_code_update();
1179 * We only monitor the error events of the socket. This
1180 * thread does not handle any incoming data from UST
1183 ret
= lttng_poll_add(&events
, sock
,
1184 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1189 /* Set socket timeout for both receiving and ending */
1190 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1191 app_socket_timeout
);
1192 (void) lttcomm_setsockopt_snd_timeout(sock
,
1193 app_socket_timeout
);
1195 DBG("Apps with sock %d added to poll set", sock
);
1197 health_code_update();
1203 * At this point, we know that a registered application made
1204 * the event at poll_wait.
1206 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1207 /* Removing from the poll set */
1208 ret
= lttng_poll_del(&events
, pollfd
);
1213 /* Socket closed on remote end. */
1214 ust_app_unregister(pollfd
);
1219 health_code_update();
1225 lttng_poll_clean(&events
);
1228 utils_close_pipe(apps_cmd_pipe
);
1229 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1232 * We don't clean the UST app hash table here since already registered
1233 * applications can still be controlled so let them be until the session
1234 * daemon dies or the applications stop.
1239 ERR("Health error occurred in %s", __func__
);
1241 health_unregister();
1242 DBG("Application communication apps thread cleanup complete");
1243 rcu_thread_offline();
1244 rcu_unregister_thread();
1249 * Send a socket to a thread This is called from the dispatch UST registration
1250 * thread once all sockets are set for the application.
1252 * On success, return 0 else a negative value being the errno message of the
1255 static int send_socket_to_thread(int fd
, int sock
)
1259 /* Sockets MUST be set or else this should not have been called. */
1264 ret
= write(fd
, &sock
, sizeof(sock
));
1265 } while (ret
< 0 && errno
== EINTR
);
1266 if (ret
< 0 || ret
!= sizeof(sock
)) {
1267 PERROR("write apps pipe %d", fd
);
1274 /* All good. Don't send back the write positive ret value. */
1281 * Dispatch request from the registration threads to the application
1282 * communication thread.
1284 static void *thread_dispatch_ust_registration(void *data
)
1287 struct cds_wfq_node
*node
;
1288 struct ust_command
*ust_cmd
= NULL
;
1290 struct ust_app
*app
;
1291 struct cds_list_head head
;
1292 } *wait_node
= NULL
, *tmp_wait_node
;
1294 CDS_LIST_HEAD(wait_queue
);
1296 DBG("[thread] Dispatch UST command started");
1298 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1299 /* Atomically prepare the queue futex */
1300 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1303 struct ust_app
*app
= NULL
;
1306 /* Dequeue command for registration */
1307 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1309 DBG("Woken up but nothing in the UST command queue");
1310 /* Continue thread execution */
1314 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1316 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1317 " gid:%d sock:%d name:%s (version %d.%d)",
1318 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1319 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1320 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1321 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1323 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1324 wait_node
= zmalloc(sizeof(*wait_node
));
1326 PERROR("zmalloc wait_node dispatch");
1330 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1332 /* Create application object if socket is CMD. */
1333 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1335 if (!wait_node
->app
) {
1336 ret
= close(ust_cmd
->sock
);
1338 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1340 lttng_fd_put(1, LTTNG_FD_APPS
);
1346 * Add application to the wait queue so we can set the notify
1347 * socket before putting this object in the global ht.
1349 cds_list_add(&wait_node
->head
, &wait_queue
);
1353 * We have to continue here since we don't have the notify
1354 * socket and the application MUST be added to the hash table
1355 * only at that moment.
1360 * Look for the application in the local wait queue and set the
1361 * notify socket if found.
1363 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1364 &wait_queue
, head
) {
1365 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1366 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1367 cds_list_del(&wait_node
->head
);
1368 app
= wait_node
->app
;
1370 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1379 * @session_lock_list
1381 * Lock the global session list so from the register up to the
1382 * registration done message, no thread can see the application
1383 * and change its state.
1385 session_lock_list();
1389 * Add application to the global hash table. This needs to be
1390 * done before the update to the UST registry can locate the
1395 /* Set app version. This call will print an error if needed. */
1396 (void) ust_app_version(app
);
1398 /* Send notify socket through the notify pipe. */
1399 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1403 session_unlock_list();
1404 /* No notify thread, stop the UST tracing. */
1409 * Update newly registered application with the tracing
1410 * registry info already enabled information.
1412 update_ust_app(app
->sock
);
1415 * Don't care about return value. Let the manage apps threads
1416 * handle app unregistration upon socket close.
1418 (void) ust_app_register_done(app
->sock
);
1421 * Even if the application socket has been closed, send the app
1422 * to the thread and unregistration will take place at that
1425 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1428 session_unlock_list();
1429 /* No apps. thread, stop the UST tracing. */
1434 session_unlock_list();
1436 /* Application manager threads are not available. */
1437 ret
= close(ust_cmd
->sock
);
1439 PERROR("close ust_cmd sock");
1441 lttng_fd_put(1, LTTNG_FD_APPS
);
1443 } while (node
!= NULL
);
1445 /* Futex wait on queue. Blocking call on futex() */
1446 futex_nto1_wait(&ust_cmd_queue
.futex
);
1450 /* Clean up wait queue. */
1451 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1452 &wait_queue
, head
) {
1453 cds_list_del(&wait_node
->head
);
1457 DBG("Dispatch thread dying");
1462 * This thread manage application registration.
1464 static void *thread_registration_apps(void *data
)
1466 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1467 uint32_t revents
, nb_fd
;
1468 struct lttng_poll_event events
;
1470 * Get allocated in this thread, enqueued to a global queue, dequeued and
1471 * freed in the manage apps thread.
1473 struct ust_command
*ust_cmd
= NULL
;
1475 DBG("[thread] Manage application registration started");
1477 health_register(HEALTH_TYPE_APP_REG
);
1479 if (testpoint(thread_registration_apps
)) {
1480 goto error_testpoint
;
1483 ret
= lttcomm_listen_unix_sock(apps_sock
);
1489 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1490 * more will be added to this poll set.
1492 ret
= sessiond_set_thread_pollset(&events
, 2);
1494 goto error_create_poll
;
1497 /* Add the application registration socket */
1498 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1500 goto error_poll_add
;
1503 /* Notify all applications to register */
1504 ret
= notify_ust_apps(1);
1506 ERR("Failed to notify applications or create the wait shared memory.\n"
1507 "Execution continues but there might be problem for already\n"
1508 "running applications that wishes to register.");
1512 DBG("Accepting application registration");
1514 /* Inifinite blocking call, waiting for transmission */
1516 health_poll_entry();
1517 ret
= lttng_poll_wait(&events
, -1);
1521 * Restart interrupted system call.
1523 if (errno
== EINTR
) {
1531 for (i
= 0; i
< nb_fd
; i
++) {
1532 health_code_update();
1534 /* Fetch once the poll data */
1535 revents
= LTTNG_POLL_GETEV(&events
, i
);
1536 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1538 /* Thread quit pipe has been closed. Killing thread. */
1539 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1545 /* Event on the registration socket */
1546 if (pollfd
== apps_sock
) {
1547 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1548 ERR("Register apps socket poll error");
1550 } else if (revents
& LPOLLIN
) {
1551 sock
= lttcomm_accept_unix_sock(apps_sock
);
1557 * Set the CLOEXEC flag. Return code is useless because
1558 * either way, the show must go on.
1560 (void) utils_set_fd_cloexec(sock
);
1562 /* Create UST registration command for enqueuing */
1563 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1564 if (ust_cmd
== NULL
) {
1565 PERROR("ust command zmalloc");
1570 * Using message-based transmissions to ensure we don't
1571 * have to deal with partially received messages.
1573 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1575 ERR("Exhausted file descriptors allowed for applications.");
1585 health_code_update();
1586 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
1589 /* Close socket of the application. */
1594 lttng_fd_put(LTTNG_FD_APPS
, 1);
1598 health_code_update();
1600 ust_cmd
->sock
= sock
;
1603 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1604 " gid:%d sock:%d name:%s (version %d.%d)",
1605 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1606 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1607 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1608 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1611 * Lock free enqueue the registration request. The red pill
1612 * has been taken! This apps will be part of the *system*.
1614 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1617 * Wake the registration queue futex. Implicit memory
1618 * barrier with the exchange in cds_wfq_enqueue.
1620 futex_nto1_wake(&ust_cmd_queue
.futex
);
1630 ERR("Health error occurred in %s", __func__
);
1633 /* Notify that the registration thread is gone */
1636 if (apps_sock
>= 0) {
1637 ret
= close(apps_sock
);
1647 lttng_fd_put(LTTNG_FD_APPS
, 1);
1649 unlink(apps_unix_sock_path
);
1652 lttng_poll_clean(&events
);
1656 DBG("UST Registration thread cleanup complete");
1657 health_unregister();
1663 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1664 * exec or it will fails.
1666 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
1669 struct timespec timeout
;
1671 /* Make sure we set the readiness flag to 0 because we are NOT ready */
1672 consumer_data
->consumer_thread_is_ready
= 0;
1674 /* Setup pthread condition */
1675 ret
= pthread_condattr_init(&consumer_data
->condattr
);
1678 PERROR("pthread_condattr_init consumer data");
1683 * Set the monotonic clock in order to make sure we DO NOT jump in time
1684 * between the clock_gettime() call and the timedwait call. See bug #324
1685 * for a more details and how we noticed it.
1687 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
1690 PERROR("pthread_condattr_setclock consumer data");
1694 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
1697 PERROR("pthread_cond_init consumer data");
1701 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
1704 PERROR("pthread_create consumer");
1709 /* We are about to wait on a pthread condition */
1710 pthread_mutex_lock(&consumer_data
->cond_mutex
);
1712 /* Get time for sem_timedwait absolute timeout */
1713 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
1715 * Set the timeout for the condition timed wait even if the clock gettime
1716 * call fails since we might loop on that call and we want to avoid to
1717 * increment the timeout too many times.
1719 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
1722 * The following loop COULD be skipped in some conditions so this is why we
1723 * set ret to 0 in order to make sure at least one round of the loop is
1729 * Loop until the condition is reached or when a timeout is reached. Note
1730 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
1731 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
1732 * possible. This loop does not take any chances and works with both of
1735 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
1736 if (clock_ret
< 0) {
1737 PERROR("clock_gettime spawn consumer");
1738 /* Infinite wait for the consumerd thread to be ready */
1739 ret
= pthread_cond_wait(&consumer_data
->cond
,
1740 &consumer_data
->cond_mutex
);
1742 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
1743 &consumer_data
->cond_mutex
, &timeout
);
1747 /* Release the pthread condition */
1748 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
1752 if (ret
== ETIMEDOUT
) {
1754 * Call has timed out so we kill the kconsumerd_thread and return
1757 ERR("Condition timed out. The consumer thread was never ready."
1759 ret
= pthread_cancel(consumer_data
->thread
);
1761 PERROR("pthread_cancel consumer thread");
1764 PERROR("pthread_cond_wait failed consumer thread");
1769 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1770 if (consumer_data
->pid
== 0) {
1771 ERR("Consumerd did not start");
1772 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1775 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1784 * Join consumer thread
1786 static int join_consumer_thread(struct consumer_data
*consumer_data
)
1790 /* Consumer pid must be a real one. */
1791 if (consumer_data
->pid
> 0) {
1793 ret
= kill(consumer_data
->pid
, SIGTERM
);
1795 ERR("Error killing consumer daemon");
1798 return pthread_join(consumer_data
->thread
, &status
);
1805 * Fork and exec a consumer daemon (consumerd).
1807 * Return pid if successful else -1.
1809 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
1813 const char *consumer_to_use
;
1814 const char *verbosity
;
1817 DBG("Spawning consumerd");
1824 if (opt_verbose_consumer
) {
1825 verbosity
= "--verbose";
1827 verbosity
= "--quiet";
1829 switch (consumer_data
->type
) {
1830 case LTTNG_CONSUMER_KERNEL
:
1832 * Find out which consumerd to execute. We will first try the
1833 * 64-bit path, then the sessiond's installation directory, and
1834 * fallback on the 32-bit one,
1836 DBG3("Looking for a kernel consumer at these locations:");
1837 DBG3(" 1) %s", consumerd64_bin
);
1838 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
1839 DBG3(" 3) %s", consumerd32_bin
);
1840 if (stat(consumerd64_bin
, &st
) == 0) {
1841 DBG3("Found location #1");
1842 consumer_to_use
= consumerd64_bin
;
1843 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
1844 DBG3("Found location #2");
1845 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
1846 } else if (stat(consumerd32_bin
, &st
) == 0) {
1847 DBG3("Found location #3");
1848 consumer_to_use
= consumerd32_bin
;
1850 DBG("Could not find any valid consumerd executable");
1853 DBG("Using kernel consumer at: %s", consumer_to_use
);
1854 execl(consumer_to_use
,
1855 "lttng-consumerd", verbosity
, "-k",
1856 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1857 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1860 case LTTNG_CONSUMER64_UST
:
1862 char *tmpnew
= NULL
;
1864 if (consumerd64_libdir
[0] != '\0') {
1868 tmp
= getenv("LD_LIBRARY_PATH");
1872 tmplen
= strlen("LD_LIBRARY_PATH=")
1873 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
1874 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1879 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1880 strcat(tmpnew
, consumerd64_libdir
);
1881 if (tmp
[0] != '\0') {
1882 strcat(tmpnew
, ":");
1883 strcat(tmpnew
, tmp
);
1885 ret
= putenv(tmpnew
);
1892 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
1893 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
1894 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1895 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1897 if (consumerd64_libdir
[0] != '\0') {
1905 case LTTNG_CONSUMER32_UST
:
1907 char *tmpnew
= NULL
;
1909 if (consumerd32_libdir
[0] != '\0') {
1913 tmp
= getenv("LD_LIBRARY_PATH");
1917 tmplen
= strlen("LD_LIBRARY_PATH=")
1918 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
1919 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1924 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1925 strcat(tmpnew
, consumerd32_libdir
);
1926 if (tmp
[0] != '\0') {
1927 strcat(tmpnew
, ":");
1928 strcat(tmpnew
, tmp
);
1930 ret
= putenv(tmpnew
);
1937 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
1938 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
1939 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1940 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1942 if (consumerd32_libdir
[0] != '\0') {
1951 PERROR("unknown consumer type");
1955 PERROR("kernel start consumer exec");
1958 } else if (pid
> 0) {
1961 PERROR("start consumer fork");
1969 * Spawn the consumerd daemon and session daemon thread.
1971 static int start_consumerd(struct consumer_data
*consumer_data
)
1976 * Set the listen() state on the socket since there is a possible race
1977 * between the exec() of the consumer daemon and this call if place in the
1978 * consumer thread. See bug #366 for more details.
1980 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
1985 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1986 if (consumer_data
->pid
!= 0) {
1987 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1991 ret
= spawn_consumerd(consumer_data
);
1993 ERR("Spawning consumerd failed");
1994 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1998 /* Setting up the consumer_data pid */
1999 consumer_data
->pid
= ret
;
2000 DBG2("Consumer pid %d", consumer_data
->pid
);
2001 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2003 DBG2("Spawning consumer control thread");
2004 ret
= spawn_consumer_thread(consumer_data
);
2006 ERR("Fatal error spawning consumer control thread");
2014 /* Cleanup already created socket on error. */
2015 if (consumer_data
->err_sock
>= 0) {
2018 err
= close(consumer_data
->err_sock
);
2020 PERROR("close consumer data error socket");
2027 * Compute health status of each consumer. If one of them is zero (bad
2028 * state), we return 0.
2030 static int check_consumer_health(void)
2034 ret
= health_check_state(HEALTH_TYPE_CONSUMER
);
2036 DBG3("Health consumer check %d", ret
);
2042 * Setup necessary data for kernel tracer action.
2044 static int init_kernel_tracer(void)
2048 /* Modprobe lttng kernel modules */
2049 ret
= modprobe_lttng_control();
2054 /* Open debugfs lttng */
2055 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2056 if (kernel_tracer_fd
< 0) {
2057 DBG("Failed to open %s", module_proc_lttng
);
2062 /* Validate kernel version */
2063 ret
= kernel_validate_version(kernel_tracer_fd
);
2068 ret
= modprobe_lttng_data();
2073 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2077 modprobe_remove_lttng_control();
2078 ret
= close(kernel_tracer_fd
);
2082 kernel_tracer_fd
= -1;
2083 return LTTNG_ERR_KERN_VERSION
;
2086 ret
= close(kernel_tracer_fd
);
2092 modprobe_remove_lttng_control();
2095 WARN("No kernel tracer available");
2096 kernel_tracer_fd
= -1;
2098 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2100 return LTTNG_ERR_KERN_NA
;
2106 * Copy consumer output from the tracing session to the domain session. The
2107 * function also applies the right modification on a per domain basis for the
2108 * trace files destination directory.
2110 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2113 const char *dir_name
;
2114 struct consumer_output
*consumer
;
2117 assert(session
->consumer
);
2120 case LTTNG_DOMAIN_KERNEL
:
2121 DBG3("Copying tracing session consumer output in kernel session");
2123 * XXX: We should audit the session creation and what this function
2124 * does "extra" in order to avoid a destroy since this function is used
2125 * in the domain session creation (kernel and ust) only. Same for UST
2128 if (session
->kernel_session
->consumer
) {
2129 consumer_destroy_output(session
->kernel_session
->consumer
);
2131 session
->kernel_session
->consumer
=
2132 consumer_copy_output(session
->consumer
);
2133 /* Ease our life a bit for the next part */
2134 consumer
= session
->kernel_session
->consumer
;
2135 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2137 case LTTNG_DOMAIN_UST
:
2138 DBG3("Copying tracing session consumer output in UST session");
2139 if (session
->ust_session
->consumer
) {
2140 consumer_destroy_output(session
->ust_session
->consumer
);
2142 session
->ust_session
->consumer
=
2143 consumer_copy_output(session
->consumer
);
2144 /* Ease our life a bit for the next part */
2145 consumer
= session
->ust_session
->consumer
;
2146 dir_name
= DEFAULT_UST_TRACE_DIR
;
2149 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2153 /* Append correct directory to subdir */
2154 strncat(consumer
->subdir
, dir_name
,
2155 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2156 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2165 * Create an UST session and add it to the session ust list.
2167 static int create_ust_session(struct ltt_session
*session
,
2168 struct lttng_domain
*domain
)
2171 struct ltt_ust_session
*lus
= NULL
;
2175 assert(session
->consumer
);
2177 switch (domain
->type
) {
2178 case LTTNG_DOMAIN_UST
:
2181 ERR("Unknown UST domain on create session %d", domain
->type
);
2182 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2186 DBG("Creating UST session");
2188 lus
= trace_ust_create_session(session
->path
, session
->id
);
2190 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2194 lus
->uid
= session
->uid
;
2195 lus
->gid
= session
->gid
;
2196 session
->ust_session
= lus
;
2198 /* Copy session output to the newly created UST session */
2199 ret
= copy_session_consumer(domain
->type
, session
);
2200 if (ret
!= LTTNG_OK
) {
2208 session
->ust_session
= NULL
;
2213 * Create a kernel tracer session then create the default channel.
2215 static int create_kernel_session(struct ltt_session
*session
)
2219 DBG("Creating kernel session");
2221 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2223 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2227 /* Code flow safety */
2228 assert(session
->kernel_session
);
2230 /* Copy session output to the newly created Kernel session */
2231 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2232 if (ret
!= LTTNG_OK
) {
2236 /* Create directory(ies) on local filesystem. */
2237 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2238 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2239 ret
= run_as_mkdir_recursive(
2240 session
->kernel_session
->consumer
->dst
.trace_path
,
2241 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2243 if (ret
!= -EEXIST
) {
2244 ERR("Trace directory creation error");
2250 session
->kernel_session
->uid
= session
->uid
;
2251 session
->kernel_session
->gid
= session
->gid
;
2256 trace_kernel_destroy_session(session
->kernel_session
);
2257 session
->kernel_session
= NULL
;
2262 * Count number of session permitted by uid/gid.
2264 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2267 struct ltt_session
*session
;
2269 DBG("Counting number of available session for UID %d GID %d",
2271 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2273 * Only list the sessions the user can control.
2275 if (!session_access_ok(session
, uid
, gid
)) {
2284 * Process the command requested by the lttng client within the command
2285 * context structure. This function make sure that the return structure (llm)
2286 * is set and ready for transmission before returning.
2288 * Return any error encountered or 0 for success.
2290 * "sock" is only used for special-case var. len data.
2292 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2296 int need_tracing_session
= 1;
2299 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2303 switch (cmd_ctx
->lsm
->cmd_type
) {
2304 case LTTNG_CREATE_SESSION
:
2305 case LTTNG_DESTROY_SESSION
:
2306 case LTTNG_LIST_SESSIONS
:
2307 case LTTNG_LIST_DOMAINS
:
2308 case LTTNG_START_TRACE
:
2309 case LTTNG_STOP_TRACE
:
2310 case LTTNG_DATA_PENDING
:
2317 if (opt_no_kernel
&& need_domain
2318 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2320 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2322 ret
= LTTNG_ERR_KERN_NA
;
2327 /* Deny register consumer if we already have a spawned consumer. */
2328 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2329 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2330 if (kconsumer_data
.pid
> 0) {
2331 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2332 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2335 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2339 * Check for command that don't needs to allocate a returned payload. We do
2340 * this here so we don't have to make the call for no payload at each
2343 switch(cmd_ctx
->lsm
->cmd_type
) {
2344 case LTTNG_LIST_SESSIONS
:
2345 case LTTNG_LIST_TRACEPOINTS
:
2346 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2347 case LTTNG_LIST_DOMAINS
:
2348 case LTTNG_LIST_CHANNELS
:
2349 case LTTNG_LIST_EVENTS
:
2352 /* Setup lttng message with no payload */
2353 ret
= setup_lttng_msg(cmd_ctx
, 0);
2355 /* This label does not try to unlock the session */
2356 goto init_setup_error
;
2360 /* Commands that DO NOT need a session. */
2361 switch (cmd_ctx
->lsm
->cmd_type
) {
2362 case LTTNG_CREATE_SESSION
:
2363 case LTTNG_CALIBRATE
:
2364 case LTTNG_LIST_SESSIONS
:
2365 case LTTNG_LIST_TRACEPOINTS
:
2366 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2367 need_tracing_session
= 0;
2370 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2372 * We keep the session list lock across _all_ commands
2373 * for now, because the per-session lock does not
2374 * handle teardown properly.
2376 session_lock_list();
2377 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2378 if (cmd_ctx
->session
== NULL
) {
2379 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
2380 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2382 /* If no session name specified */
2383 ret
= LTTNG_ERR_SELECT_SESS
;
2387 /* Acquire lock for the session */
2388 session_lock(cmd_ctx
->session
);
2398 * Check domain type for specific "pre-action".
2400 switch (cmd_ctx
->lsm
->domain
.type
) {
2401 case LTTNG_DOMAIN_KERNEL
:
2403 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2407 /* Kernel tracer check */
2408 if (kernel_tracer_fd
== -1) {
2409 /* Basically, load kernel tracer modules */
2410 ret
= init_kernel_tracer();
2416 /* Consumer is in an ERROR state. Report back to client */
2417 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
2418 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2422 /* Need a session for kernel command */
2423 if (need_tracing_session
) {
2424 if (cmd_ctx
->session
->kernel_session
== NULL
) {
2425 ret
= create_kernel_session(cmd_ctx
->session
);
2427 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2432 /* Start the kernel consumer daemon */
2433 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2434 if (kconsumer_data
.pid
== 0 &&
2435 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2436 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2437 ret
= start_consumerd(&kconsumer_data
);
2439 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2442 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
2444 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2448 * The consumer was just spawned so we need to add the socket to
2449 * the consumer output of the session if exist.
2451 ret
= consumer_create_socket(&kconsumer_data
,
2452 cmd_ctx
->session
->kernel_session
->consumer
);
2459 case LTTNG_DOMAIN_UST
:
2461 /* Consumer is in an ERROR state. Report back to client */
2462 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
2463 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2467 if (need_tracing_session
) {
2468 /* Create UST session if none exist. */
2469 if (cmd_ctx
->session
->ust_session
== NULL
) {
2470 ret
= create_ust_session(cmd_ctx
->session
,
2471 &cmd_ctx
->lsm
->domain
);
2472 if (ret
!= LTTNG_OK
) {
2477 /* Start the UST consumer daemons */
2479 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
2480 if (consumerd64_bin
[0] != '\0' &&
2481 ustconsumer64_data
.pid
== 0 &&
2482 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2483 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2484 ret
= start_consumerd(&ustconsumer64_data
);
2486 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
2487 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
2491 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
2492 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2494 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2498 * Setup socket for consumer 64 bit. No need for atomic access
2499 * since it was set above and can ONLY be set in this thread.
2501 ret
= consumer_create_socket(&ustconsumer64_data
,
2502 cmd_ctx
->session
->ust_session
->consumer
);
2508 if (consumerd32_bin
[0] != '\0' &&
2509 ustconsumer32_data
.pid
== 0 &&
2510 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2511 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2512 ret
= start_consumerd(&ustconsumer32_data
);
2514 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
2515 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
2519 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
2520 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2522 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2526 * Setup socket for consumer 64 bit. No need for atomic access
2527 * since it was set above and can ONLY be set in this thread.
2529 ret
= consumer_create_socket(&ustconsumer32_data
,
2530 cmd_ctx
->session
->ust_session
->consumer
);
2542 /* Validate consumer daemon state when start/stop trace command */
2543 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
2544 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
2545 switch (cmd_ctx
->lsm
->domain
.type
) {
2546 case LTTNG_DOMAIN_UST
:
2547 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
2548 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2552 case LTTNG_DOMAIN_KERNEL
:
2553 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
2554 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2562 * Check that the UID or GID match that of the tracing session.
2563 * The root user can interact with all sessions.
2565 if (need_tracing_session
) {
2566 if (!session_access_ok(cmd_ctx
->session
,
2567 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2568 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
2569 ret
= LTTNG_ERR_EPERM
;
2575 * Send relayd information to consumer as soon as we have a domain and a
2578 if (cmd_ctx
->session
&& need_domain
) {
2580 * Setup relayd if not done yet. If the relayd information was already
2581 * sent to the consumer, this call will gracefully return.
2583 ret
= cmd_setup_relayd(cmd_ctx
->session
);
2584 if (ret
!= LTTNG_OK
) {
2589 /* Process by command type */
2590 switch (cmd_ctx
->lsm
->cmd_type
) {
2591 case LTTNG_ADD_CONTEXT
:
2593 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2594 cmd_ctx
->lsm
->u
.context
.channel_name
,
2595 &cmd_ctx
->lsm
->u
.context
.ctx
, kernel_poll_pipe
[1]);
2598 case LTTNG_DISABLE_CHANNEL
:
2600 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2601 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2604 case LTTNG_DISABLE_EVENT
:
2606 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2607 cmd_ctx
->lsm
->u
.disable
.channel_name
,
2608 cmd_ctx
->lsm
->u
.disable
.name
);
2611 case LTTNG_DISABLE_ALL_EVENT
:
2613 DBG("Disabling all events");
2615 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2616 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2619 case LTTNG_ENABLE_CHANNEL
:
2621 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
2622 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
2625 case LTTNG_ENABLE_EVENT
:
2627 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
2628 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2629 &cmd_ctx
->lsm
->u
.enable
.event
, NULL
, kernel_poll_pipe
[1]);
2632 case LTTNG_ENABLE_ALL_EVENT
:
2634 DBG("Enabling all events");
2636 ret
= cmd_enable_event_all(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
2637 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2638 cmd_ctx
->lsm
->u
.enable
.event
.type
, NULL
, kernel_poll_pipe
[1]);
2641 case LTTNG_LIST_TRACEPOINTS
:
2643 struct lttng_event
*events
;
2646 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
2647 if (nb_events
< 0) {
2648 /* Return value is a negative lttng_error_code. */
2654 * Setup lttng message with payload size set to the event list size in
2655 * bytes and then copy list into the llm payload.
2657 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2663 /* Copy event list into message payload */
2664 memcpy(cmd_ctx
->llm
->payload
, events
,
2665 sizeof(struct lttng_event
) * nb_events
);
2672 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2674 struct lttng_event_field
*fields
;
2677 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
2679 if (nb_fields
< 0) {
2680 /* Return value is a negative lttng_error_code. */
2686 * Setup lttng message with payload size set to the event list size in
2687 * bytes and then copy list into the llm payload.
2689 ret
= setup_lttng_msg(cmd_ctx
,
2690 sizeof(struct lttng_event_field
) * nb_fields
);
2696 /* Copy event list into message payload */
2697 memcpy(cmd_ctx
->llm
->payload
, fields
,
2698 sizeof(struct lttng_event_field
) * nb_fields
);
2705 case LTTNG_SET_CONSUMER_URI
:
2708 struct lttng_uri
*uris
;
2710 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
2711 len
= nb_uri
* sizeof(struct lttng_uri
);
2714 ret
= LTTNG_ERR_INVALID
;
2718 uris
= zmalloc(len
);
2720 ret
= LTTNG_ERR_FATAL
;
2724 /* Receive variable len data */
2725 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
2726 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
2728 DBG("No URIs received from client... continuing");
2730 ret
= LTTNG_ERR_SESSION_FAIL
;
2735 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
2737 if (ret
!= LTTNG_OK
) {
2743 * XXX: 0 means that this URI should be applied on the session. Should
2744 * be a DOMAIN enuam.
2746 if (cmd_ctx
->lsm
->domain
.type
== 0) {
2747 /* Add the URI for the UST session if a consumer is present. */
2748 if (cmd_ctx
->session
->ust_session
&&
2749 cmd_ctx
->session
->ust_session
->consumer
) {
2750 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_UST
, cmd_ctx
->session
,
2752 } else if (cmd_ctx
->session
->kernel_session
&&
2753 cmd_ctx
->session
->kernel_session
->consumer
) {
2754 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL
,
2755 cmd_ctx
->session
, nb_uri
, uris
);
2763 case LTTNG_START_TRACE
:
2765 ret
= cmd_start_trace(cmd_ctx
->session
);
2768 case LTTNG_STOP_TRACE
:
2770 ret
= cmd_stop_trace(cmd_ctx
->session
);
2773 case LTTNG_CREATE_SESSION
:
2776 struct lttng_uri
*uris
= NULL
;
2778 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
2779 len
= nb_uri
* sizeof(struct lttng_uri
);
2782 uris
= zmalloc(len
);
2784 ret
= LTTNG_ERR_FATAL
;
2788 /* Receive variable len data */
2789 DBG("Waiting for %zu URIs from client ...", nb_uri
);
2790 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
2792 DBG("No URIs received from client... continuing");
2794 ret
= LTTNG_ERR_SESSION_FAIL
;
2799 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
2800 DBG("Creating session with ONE network URI is a bad call");
2801 ret
= LTTNG_ERR_SESSION_FAIL
;
2807 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
2814 case LTTNG_DESTROY_SESSION
:
2816 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
2818 /* Set session to NULL so we do not unlock it after free. */
2819 cmd_ctx
->session
= NULL
;
2822 case LTTNG_LIST_DOMAINS
:
2825 struct lttng_domain
*domains
;
2827 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
2829 /* Return value is a negative lttng_error_code. */
2834 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
2839 /* Copy event list into message payload */
2840 memcpy(cmd_ctx
->llm
->payload
, domains
,
2841 nb_dom
* sizeof(struct lttng_domain
));
2848 case LTTNG_LIST_CHANNELS
:
2851 struct lttng_channel
*channels
;
2853 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
2854 cmd_ctx
->session
, &channels
);
2856 /* Return value is a negative lttng_error_code. */
2861 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
2866 /* Copy event list into message payload */
2867 memcpy(cmd_ctx
->llm
->payload
, channels
,
2868 nb_chan
* sizeof(struct lttng_channel
));
2875 case LTTNG_LIST_EVENTS
:
2878 struct lttng_event
*events
= NULL
;
2880 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
2881 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
2883 /* Return value is a negative lttng_error_code. */
2888 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
2893 /* Copy event list into message payload */
2894 memcpy(cmd_ctx
->llm
->payload
, events
,
2895 nb_event
* sizeof(struct lttng_event
));
2902 case LTTNG_LIST_SESSIONS
:
2904 unsigned int nr_sessions
;
2906 session_lock_list();
2907 nr_sessions
= lttng_sessions_count(
2908 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2909 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
2911 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
2913 session_unlock_list();
2917 /* Filled the session array */
2918 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
2919 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2920 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
2922 session_unlock_list();
2927 case LTTNG_CALIBRATE
:
2929 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
2930 &cmd_ctx
->lsm
->u
.calibrate
);
2933 case LTTNG_REGISTER_CONSUMER
:
2935 struct consumer_data
*cdata
;
2937 switch (cmd_ctx
->lsm
->domain
.type
) {
2938 case LTTNG_DOMAIN_KERNEL
:
2939 cdata
= &kconsumer_data
;
2942 ret
= LTTNG_ERR_UND
;
2946 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2947 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
2950 case LTTNG_ENABLE_EVENT_WITH_FILTER
:
2952 struct lttng_filter_bytecode
*bytecode
;
2954 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
2955 ret
= LTTNG_ERR_FILTER_INVAL
;
2958 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
== 0) {
2959 ret
= LTTNG_ERR_FILTER_INVAL
;
2962 bytecode
= zmalloc(cmd_ctx
->lsm
->u
.enable
.bytecode_len
);
2964 ret
= LTTNG_ERR_FILTER_NOMEM
;
2967 /* Receive var. len. data */
2968 DBG("Receiving var len data from client ...");
2969 ret
= lttcomm_recv_unix_sock(sock
, bytecode
,
2970 cmd_ctx
->lsm
->u
.enable
.bytecode_len
);
2972 DBG("Nothing recv() from client var len data... continuing");
2974 ret
= LTTNG_ERR_FILTER_INVAL
;
2978 if (bytecode
->len
+ sizeof(*bytecode
)
2979 != cmd_ctx
->lsm
->u
.enable
.bytecode_len
) {
2981 ret
= LTTNG_ERR_FILTER_INVAL
;
2985 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
2986 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2987 &cmd_ctx
->lsm
->u
.enable
.event
, bytecode
, kernel_poll_pipe
[1]);
2990 case LTTNG_DATA_PENDING
:
2992 ret
= cmd_data_pending(cmd_ctx
->session
);
2996 ret
= LTTNG_ERR_UND
;
3001 if (cmd_ctx
->llm
== NULL
) {
3002 DBG("Missing llm structure. Allocating one.");
3003 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
3007 /* Set return code */
3008 cmd_ctx
->llm
->ret_code
= ret
;
3010 if (cmd_ctx
->session
) {
3011 session_unlock(cmd_ctx
->session
);
3013 if (need_tracing_session
) {
3014 session_unlock_list();
3021 * Thread managing health check socket.
3023 static void *thread_manage_health(void *data
)
3025 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
3026 uint32_t revents
, nb_fd
;
3027 struct lttng_poll_event events
;
3028 struct lttcomm_health_msg msg
;
3029 struct lttcomm_health_data reply
;
3031 DBG("[thread] Manage health check started");
3033 rcu_register_thread();
3035 /* Create unix socket */
3036 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
3038 ERR("Unable to create health check Unix socket");
3044 * Set the CLOEXEC flag. Return code is useless because either way, the
3047 (void) utils_set_fd_cloexec(sock
);
3049 ret
= lttcomm_listen_unix_sock(sock
);
3055 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3056 * more will be added to this poll set.
3058 ret
= sessiond_set_thread_pollset(&events
, 2);
3063 /* Add the application registration socket */
3064 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
3070 DBG("Health check ready");
3072 /* Inifinite blocking call, waiting for transmission */
3074 ret
= lttng_poll_wait(&events
, -1);
3077 * Restart interrupted system call.
3079 if (errno
== EINTR
) {
3087 for (i
= 0; i
< nb_fd
; i
++) {
3088 /* Fetch once the poll data */
3089 revents
= LTTNG_POLL_GETEV(&events
, i
);
3090 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3092 /* Thread quit pipe has been closed. Killing thread. */
3093 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3099 /* Event on the registration socket */
3100 if (pollfd
== sock
) {
3101 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3102 ERR("Health socket poll error");
3108 new_sock
= lttcomm_accept_unix_sock(sock
);
3114 * Set the CLOEXEC flag. Return code is useless because either way, the
3117 (void) utils_set_fd_cloexec(new_sock
);
3119 DBG("Receiving data from client for health...");
3120 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
3122 DBG("Nothing recv() from client... continuing");
3123 ret
= close(new_sock
);
3131 rcu_thread_online();
3133 switch (msg
.component
) {
3134 case LTTNG_HEALTH_CMD
:
3135 reply
.ret_code
= health_check_state(HEALTH_TYPE_CMD
);
3137 case LTTNG_HEALTH_APP_MANAGE
:
3138 reply
.ret_code
= health_check_state(HEALTH_TYPE_APP_MANAGE
);
3140 case LTTNG_HEALTH_APP_REG
:
3141 reply
.ret_code
= health_check_state(HEALTH_TYPE_APP_REG
);
3143 case LTTNG_HEALTH_KERNEL
:
3144 reply
.ret_code
= health_check_state(HEALTH_TYPE_KERNEL
);
3146 case LTTNG_HEALTH_CONSUMER
:
3147 reply
.ret_code
= check_consumer_health();
3149 case LTTNG_HEALTH_ALL
:
3151 health_check_state(HEALTH_TYPE_APP_MANAGE
) &&
3152 health_check_state(HEALTH_TYPE_APP_REG
) &&
3153 health_check_state(HEALTH_TYPE_CMD
) &&
3154 health_check_state(HEALTH_TYPE_KERNEL
) &&
3155 check_consumer_health();
3158 reply
.ret_code
= LTTNG_ERR_UND
;
3163 * Flip ret value since 0 is a success and 1 indicates a bad health for
3164 * the client where in the sessiond it is the opposite. Again, this is
3165 * just to make things easier for us poor developer which enjoy a lot
3168 if (reply
.ret_code
== 0 || reply
.ret_code
== 1) {
3169 reply
.ret_code
= !reply
.ret_code
;
3172 DBG2("Health check return value %d", reply
.ret_code
);
3174 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
3176 ERR("Failed to send health data back to client");
3179 /* End of transmission */
3180 ret
= close(new_sock
);
3190 ERR("Health error occurred in %s", __func__
);
3192 DBG("Health check thread dying");
3193 unlink(health_unix_sock_path
);
3200 if (new_sock
>= 0) {
3201 ret
= close(new_sock
);
3207 lttng_poll_clean(&events
);
3209 rcu_unregister_thread();
3214 * This thread manage all clients request using the unix client socket for
3217 static void *thread_manage_clients(void *data
)
3219 int sock
= -1, ret
, i
, pollfd
, err
= -1;
3221 uint32_t revents
, nb_fd
;
3222 struct command_ctx
*cmd_ctx
= NULL
;
3223 struct lttng_poll_event events
;
3225 DBG("[thread] Manage client started");
3227 rcu_register_thread();
3229 health_register(HEALTH_TYPE_CMD
);
3231 if (testpoint(thread_manage_clients
)) {
3232 goto error_testpoint
;
3235 health_code_update();
3237 ret
= lttcomm_listen_unix_sock(client_sock
);
3243 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3244 * more will be added to this poll set.
3246 ret
= sessiond_set_thread_pollset(&events
, 2);
3248 goto error_create_poll
;
3251 /* Add the application registration socket */
3252 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
3258 * Notify parent pid that we are ready to accept command for client side.
3260 if (opt_sig_parent
) {
3261 kill(ppid
, SIGUSR1
);
3264 if (testpoint(thread_manage_clients_before_loop
)) {
3268 health_code_update();
3271 DBG("Accepting client command ...");
3273 /* Inifinite blocking call, waiting for transmission */
3275 health_poll_entry();
3276 ret
= lttng_poll_wait(&events
, -1);
3280 * Restart interrupted system call.
3282 if (errno
== EINTR
) {
3290 for (i
= 0; i
< nb_fd
; i
++) {
3291 /* Fetch once the poll data */
3292 revents
= LTTNG_POLL_GETEV(&events
, i
);
3293 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3295 health_code_update();
3297 /* Thread quit pipe has been closed. Killing thread. */
3298 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3304 /* Event on the registration socket */
3305 if (pollfd
== client_sock
) {
3306 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3307 ERR("Client socket poll error");
3313 DBG("Wait for client response");
3315 health_code_update();
3317 sock
= lttcomm_accept_unix_sock(client_sock
);
3323 * Set the CLOEXEC flag. Return code is useless because either way, the
3326 (void) utils_set_fd_cloexec(sock
);
3328 /* Set socket option for credentials retrieval */
3329 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
3334 /* Allocate context command to process the client request */
3335 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
3336 if (cmd_ctx
== NULL
) {
3337 PERROR("zmalloc cmd_ctx");
3341 /* Allocate data buffer for reception */
3342 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
3343 if (cmd_ctx
->lsm
== NULL
) {
3344 PERROR("zmalloc cmd_ctx->lsm");
3348 cmd_ctx
->llm
= NULL
;
3349 cmd_ctx
->session
= NULL
;
3351 health_code_update();
3354 * Data is received from the lttng client. The struct
3355 * lttcomm_session_msg (lsm) contains the command and data request of
3358 DBG("Receiving data from client ...");
3359 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
3360 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
3362 DBG("Nothing recv() from client... continuing");
3368 clean_command_ctx(&cmd_ctx
);
3372 health_code_update();
3374 // TODO: Validate cmd_ctx including sanity check for
3375 // security purpose.
3377 rcu_thread_online();
3379 * This function dispatch the work to the kernel or userspace tracer
3380 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3381 * informations for the client. The command context struct contains
3382 * everything this function may needs.
3384 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
3385 rcu_thread_offline();
3395 * TODO: Inform client somehow of the fatal error. At
3396 * this point, ret < 0 means that a zmalloc failed
3397 * (ENOMEM). Error detected but still accept
3398 * command, unless a socket error has been
3401 clean_command_ctx(&cmd_ctx
);
3405 health_code_update();
3407 DBG("Sending response (size: %d, retcode: %s)",
3408 cmd_ctx
->lttng_msg_size
,
3409 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
3410 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
3412 ERR("Failed to send data back to client");
3415 /* End of transmission */
3422 clean_command_ctx(&cmd_ctx
);
3424 health_code_update();
3436 lttng_poll_clean(&events
);
3437 clean_command_ctx(&cmd_ctx
);
3442 unlink(client_unix_sock_path
);
3443 if (client_sock
>= 0) {
3444 ret
= close(client_sock
);
3452 ERR("Health error occurred in %s", __func__
);
3455 health_unregister();
3457 DBG("Client thread dying");
3459 rcu_unregister_thread();
3465 * usage function on stderr
3467 static void usage(void)
3469 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
3470 fprintf(stderr
, " -h, --help Display this usage.\n");
3471 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
3472 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
3473 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
3474 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
3475 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
3476 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
3477 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
3478 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
3479 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
3480 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
3481 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
3482 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
3483 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
3484 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
3485 fprintf(stderr
, " -V, --version Show version number.\n");
3486 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
3487 fprintf(stderr
, " -q, --quiet No output at all.\n");
3488 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3489 fprintf(stderr
, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
3490 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
3491 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
3495 * daemon argument parsing
3497 static int parse_args(int argc
, char **argv
)
3501 static struct option long_options
[] = {
3502 { "client-sock", 1, 0, 'c' },
3503 { "apps-sock", 1, 0, 'a' },
3504 { "kconsumerd-cmd-sock", 1, 0, 'C' },
3505 { "kconsumerd-err-sock", 1, 0, 'E' },
3506 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
3507 { "ustconsumerd32-err-sock", 1, 0, 'H' },
3508 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
3509 { "ustconsumerd64-err-sock", 1, 0, 'F' },
3510 { "consumerd32-path", 1, 0, 'u' },
3511 { "consumerd32-libdir", 1, 0, 'U' },
3512 { "consumerd64-path", 1, 0, 't' },
3513 { "consumerd64-libdir", 1, 0, 'T' },
3514 { "daemonize", 0, 0, 'd' },
3515 { "sig-parent", 0, 0, 'S' },
3516 { "help", 0, 0, 'h' },
3517 { "group", 1, 0, 'g' },
3518 { "version", 0, 0, 'V' },
3519 { "quiet", 0, 0, 'q' },
3520 { "verbose", 0, 0, 'v' },
3521 { "verbose-consumer", 0, 0, 'Z' },
3522 { "no-kernel", 0, 0, 'N' },
3523 { "pidfile", 1, 0, 'p' },
3528 int option_index
= 0;
3529 c
= getopt_long(argc
, argv
, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t:p:",
3530 long_options
, &option_index
);
3537 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
3539 fprintf(stderr
, " with arg %s\n", optarg
);
3543 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3546 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3552 opt_tracing_group
= optarg
;
3558 fprintf(stdout
, "%s\n", VERSION
);
3564 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3567 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3570 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3573 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3576 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3579 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3585 lttng_opt_quiet
= 1;
3588 /* Verbose level can increase using multiple -v */
3589 lttng_opt_verbose
+= 1;
3592 opt_verbose_consumer
+= 1;
3595 consumerd32_bin
= optarg
;
3598 consumerd32_libdir
= optarg
;
3601 consumerd64_bin
= optarg
;
3604 consumerd64_libdir
= optarg
;
3607 opt_pidfile
= optarg
;
3610 /* Unknown option or other error.
3611 * Error is printed by getopt, just return */
3620 * Creates the two needed socket by the daemon.
3621 * apps_sock - The communication socket for all UST apps.
3622 * client_sock - The communication of the cli tool (lttng).
3624 static int init_daemon_socket(void)
3629 old_umask
= umask(0);
3631 /* Create client tool unix socket */
3632 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
3633 if (client_sock
< 0) {
3634 ERR("Create unix sock failed: %s", client_unix_sock_path
);
3639 /* Set the cloexec flag */
3640 ret
= utils_set_fd_cloexec(client_sock
);
3642 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
3643 "Continuing but note that the consumer daemon will have a "
3644 "reference to this socket on exec()", client_sock
);
3647 /* File permission MUST be 660 */
3648 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3650 ERR("Set file permissions failed: %s", client_unix_sock_path
);
3655 /* Create the application unix socket */
3656 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
3657 if (apps_sock
< 0) {
3658 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
3663 /* Set the cloexec flag */
3664 ret
= utils_set_fd_cloexec(apps_sock
);
3666 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
3667 "Continuing but note that the consumer daemon will have a "
3668 "reference to this socket on exec()", apps_sock
);
3671 /* File permission MUST be 666 */
3672 ret
= chmod(apps_unix_sock_path
,
3673 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
3675 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
3680 DBG3("Session daemon client socket %d and application socket %d created",
3681 client_sock
, apps_sock
);
3689 * Check if the global socket is available, and if a daemon is answering at the
3690 * other side. If yes, error is returned.
3692 static int check_existing_daemon(void)
3694 /* Is there anybody out there ? */
3695 if (lttng_session_daemon_alive()) {
3703 * Set the tracing group gid onto the client socket.
3705 * Race window between mkdir and chown is OK because we are going from more
3706 * permissive (root.root) to less permissive (root.tracing).
3708 static int set_permissions(char *rundir
)
3713 ret
= allowed_group();
3715 WARN("No tracing group detected");
3722 /* Set lttng run dir */
3723 ret
= chown(rundir
, 0, gid
);
3725 ERR("Unable to set group on %s", rundir
);
3729 /* Ensure tracing group can search the run dir */
3730 ret
= chmod(rundir
, S_IRWXU
| S_IXGRP
| S_IXOTH
);
3732 ERR("Unable to set permissions on %s", rundir
);
3736 /* lttng client socket path */
3737 ret
= chown(client_unix_sock_path
, 0, gid
);
3739 ERR("Unable to set group on %s", client_unix_sock_path
);
3743 /* kconsumer error socket path */
3744 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, gid
);
3746 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
3750 /* 64-bit ustconsumer error socket path */
3751 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, gid
);
3753 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
3757 /* 32-bit ustconsumer compat32 error socket path */
3758 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, gid
);
3760 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
3764 DBG("All permissions are set");
3771 * Create the lttng run directory needed for all global sockets and pipe.
3773 static int create_lttng_rundir(const char *rundir
)
3777 DBG3("Creating LTTng run directory: %s", rundir
);
3779 ret
= mkdir(rundir
, S_IRWXU
);
3781 if (errno
!= EEXIST
) {
3782 ERR("Unable to create %s", rundir
);
3794 * Setup sockets and directory needed by the kconsumerd communication with the
3797 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
3801 char path
[PATH_MAX
];
3803 switch (consumer_data
->type
) {
3804 case LTTNG_CONSUMER_KERNEL
:
3805 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
3807 case LTTNG_CONSUMER64_UST
:
3808 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
3810 case LTTNG_CONSUMER32_UST
:
3811 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
3814 ERR("Consumer type unknown");
3819 DBG2("Creating consumer directory: %s", path
);
3821 ret
= mkdir(path
, S_IRWXU
);
3823 if (errno
!= EEXIST
) {
3825 ERR("Failed to create %s", path
);
3831 /* Create the kconsumerd error unix socket */
3832 consumer_data
->err_sock
=
3833 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
3834 if (consumer_data
->err_sock
< 0) {
3835 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
3840 /* File permission MUST be 660 */
3841 ret
= chmod(consumer_data
->err_unix_sock_path
,
3842 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3844 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
3854 * Signal handler for the daemon
3856 * Simply stop all worker threads, leaving main() return gracefully after
3857 * joining all threads and calling cleanup().
3859 static void sighandler(int sig
)
3863 DBG("SIGPIPE caught");
3866 DBG("SIGINT caught");
3870 DBG("SIGTERM caught");
3879 * Setup signal handler for :
3880 * SIGINT, SIGTERM, SIGPIPE
3882 static int set_signal_handler(void)
3885 struct sigaction sa
;
3888 if ((ret
= sigemptyset(&sigset
)) < 0) {
3889 PERROR("sigemptyset");
3893 sa
.sa_handler
= sighandler
;
3894 sa
.sa_mask
= sigset
;
3896 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3897 PERROR("sigaction");
3901 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3902 PERROR("sigaction");
3906 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3907 PERROR("sigaction");
3911 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3917 * Set open files limit to unlimited. This daemon can open a large number of
3918 * file descriptors in order to consumer multiple kernel traces.
3920 static void set_ulimit(void)
3925 /* The kernel does not allowed an infinite limit for open files */
3926 lim
.rlim_cur
= 65535;
3927 lim
.rlim_max
= 65535;
3929 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3931 PERROR("failed to set open files limit");
3936 * Write pidfile using the rundir and opt_pidfile.
3938 static void write_pidfile(void)
3941 char pidfile_path
[PATH_MAX
];
3946 strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
));
3948 /* Build pidfile path from rundir and opt_pidfile. */
3949 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
3950 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
3952 PERROR("snprintf pidfile path");
3958 * Create pid file in rundir. Return value is of no importance. The
3959 * execution will continue even though we are not able to write the file.
3961 (void) utils_create_pid_file(getpid(), pidfile_path
);
3970 int main(int argc
, char **argv
)
3974 const char *home_path
, *env_app_timeout
;
3976 init_kernel_workarounds();
3978 rcu_register_thread();
3980 setup_consumerd_path();
3982 /* Parse arguments */
3984 if ((ret
= parse_args(argc
, argv
)) < 0) {
3994 * child: setsid, close FD 0, 1, 2, chdir /
3995 * parent: exit (if fork is successful)
4003 * We are in the child. Make sure all other file
4004 * descriptors are closed, in case we are called with
4005 * more opened file descriptors than the standard ones.
4007 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
4012 /* Create thread quit pipe */
4013 if ((ret
= init_thread_quit_pipe()) < 0) {
4017 /* Check if daemon is UID = 0 */
4018 is_root
= !getuid();
4021 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
4023 /* Create global run dir with root access */
4024 ret
= create_lttng_rundir(rundir
);
4029 if (strlen(apps_unix_sock_path
) == 0) {
4030 snprintf(apps_unix_sock_path
, PATH_MAX
,
4031 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
4034 if (strlen(client_unix_sock_path
) == 0) {
4035 snprintf(client_unix_sock_path
, PATH_MAX
,
4036 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
4039 /* Set global SHM for ust */
4040 if (strlen(wait_shm_path
) == 0) {
4041 snprintf(wait_shm_path
, PATH_MAX
,
4042 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
4045 if (strlen(health_unix_sock_path
) == 0) {
4046 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4047 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
4050 /* Setup kernel consumerd path */
4051 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
4052 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
4053 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
4054 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
4056 DBG2("Kernel consumer err path: %s",
4057 kconsumer_data
.err_unix_sock_path
);
4058 DBG2("Kernel consumer cmd path: %s",
4059 kconsumer_data
.cmd_unix_sock_path
);
4061 home_path
= get_home_dir();
4062 if (home_path
== NULL
) {
4063 /* TODO: Add --socket PATH option */
4064 ERR("Can't get HOME directory for sockets creation.");
4070 * Create rundir from home path. This will create something like
4073 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
4079 ret
= create_lttng_rundir(rundir
);
4084 if (strlen(apps_unix_sock_path
) == 0) {
4085 snprintf(apps_unix_sock_path
, PATH_MAX
,
4086 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
4089 /* Set the cli tool unix socket path */
4090 if (strlen(client_unix_sock_path
) == 0) {
4091 snprintf(client_unix_sock_path
, PATH_MAX
,
4092 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
4095 /* Set global SHM for ust */
4096 if (strlen(wait_shm_path
) == 0) {
4097 snprintf(wait_shm_path
, PATH_MAX
,
4098 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
4101 /* Set health check Unix path */
4102 if (strlen(health_unix_sock_path
) == 0) {
4103 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4104 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
4108 /* Set consumer initial state */
4109 kernel_consumerd_state
= CONSUMER_STOPPED
;
4110 ust_consumerd_state
= CONSUMER_STOPPED
;
4112 DBG("Client socket path %s", client_unix_sock_path
);
4113 DBG("Application socket path %s", apps_unix_sock_path
);
4114 DBG("Application wait path %s", wait_shm_path
);
4115 DBG("LTTng run directory path: %s", rundir
);
4117 /* 32 bits consumerd path setup */
4118 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
4119 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
4120 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
4121 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
4123 DBG2("UST consumer 32 bits err path: %s",
4124 ustconsumer32_data
.err_unix_sock_path
);
4125 DBG2("UST consumer 32 bits cmd path: %s",
4126 ustconsumer32_data
.cmd_unix_sock_path
);
4128 /* 64 bits consumerd path setup */
4129 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
4130 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
4131 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
4132 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
4134 DBG2("UST consumer 64 bits err path: %s",
4135 ustconsumer64_data
.err_unix_sock_path
);
4136 DBG2("UST consumer 64 bits cmd path: %s",
4137 ustconsumer64_data
.cmd_unix_sock_path
);
4140 * See if daemon already exist.
4142 if ((ret
= check_existing_daemon()) < 0) {
4143 ERR("Already running daemon.\n");
4145 * We do not goto exit because we must not cleanup()
4146 * because a daemon is already running.
4152 * Init UST app hash table. Alloc hash table before this point since
4153 * cleanup() can get called after that point.
4157 /* After this point, we can safely call cleanup() with "goto exit" */
4160 * These actions must be executed as root. We do that *after* setting up
4161 * the sockets path because we MUST make the check for another daemon using
4162 * those paths *before* trying to set the kernel consumer sockets and init
4166 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
4171 /* Setup kernel tracer */
4172 if (!opt_no_kernel
) {
4173 init_kernel_tracer();
4176 /* Set ulimit for open files */
4179 /* init lttng_fd tracking must be done after set_ulimit. */
4182 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
4187 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
4192 if ((ret
= set_signal_handler()) < 0) {
4196 /* Setup the needed unix socket */
4197 if ((ret
= init_daemon_socket()) < 0) {
4201 /* Set credentials to socket */
4202 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
4206 /* Get parent pid if -S, --sig-parent is specified. */
4207 if (opt_sig_parent
) {
4211 /* Setup the kernel pipe for waking up the kernel thread */
4212 if (is_root
&& !opt_no_kernel
) {
4213 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
4218 /* Setup the thread apps communication pipe. */
4219 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
4223 /* Setup the thread apps notify communication pipe. */
4224 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
) < 0) {
4228 /* Initialize global buffer per UID and PID registry. */
4229 buffer_reg_init_uid_registry();
4230 buffer_reg_init_pid_registry();
4232 /* Init UST command queue. */
4233 cds_wfq_init(&ust_cmd_queue
.queue
);
4236 * Get session list pointer. This pointer MUST NOT be free(). This list is
4237 * statically declared in session.c
4239 session_list_ptr
= session_get_list();
4241 /* Set up max poll set size */
4242 lttng_poll_set_max_size();
4246 /* Check for the application socket timeout env variable. */
4247 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
4248 if (env_app_timeout
) {
4249 app_socket_timeout
= atoi(env_app_timeout
);
4251 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
4256 /* Create thread to manage the client socket */
4257 ret
= pthread_create(&health_thread
, NULL
,
4258 thread_manage_health
, (void *) NULL
);
4260 PERROR("pthread_create health");
4264 /* Create thread to manage the client socket */
4265 ret
= pthread_create(&client_thread
, NULL
,
4266 thread_manage_clients
, (void *) NULL
);
4268 PERROR("pthread_create clients");
4272 /* Create thread to dispatch registration */
4273 ret
= pthread_create(&dispatch_thread
, NULL
,
4274 thread_dispatch_ust_registration
, (void *) NULL
);
4276 PERROR("pthread_create dispatch");
4280 /* Create thread to manage application registration. */
4281 ret
= pthread_create(®_apps_thread
, NULL
,
4282 thread_registration_apps
, (void *) NULL
);
4284 PERROR("pthread_create registration");
4288 /* Create thread to manage application socket */
4289 ret
= pthread_create(&apps_thread
, NULL
,
4290 thread_manage_apps
, (void *) NULL
);
4292 PERROR("pthread_create apps");
4296 /* Create thread to manage application notify socket */
4297 ret
= pthread_create(&apps_notify_thread
, NULL
,
4298 ust_thread_manage_notify
, (void *) NULL
);
4300 PERROR("pthread_create apps");
4304 /* Don't start this thread if kernel tracing is not requested nor root */
4305 if (is_root
&& !opt_no_kernel
) {
4306 /* Create kernel thread to manage kernel event */
4307 ret
= pthread_create(&kernel_thread
, NULL
,
4308 thread_manage_kernel
, (void *) NULL
);
4310 PERROR("pthread_create kernel");
4314 ret
= pthread_join(kernel_thread
, &status
);
4316 PERROR("pthread_join");
4317 goto error
; /* join error, exit without cleanup */
4322 ret
= pthread_join(apps_thread
, &status
);
4324 PERROR("pthread_join");
4325 goto error
; /* join error, exit without cleanup */
4329 ret
= pthread_join(reg_apps_thread
, &status
);
4331 PERROR("pthread_join");
4332 goto error
; /* join error, exit without cleanup */
4336 ret
= pthread_join(dispatch_thread
, &status
);
4338 PERROR("pthread_join");
4339 goto error
; /* join error, exit without cleanup */
4343 ret
= pthread_join(client_thread
, &status
);
4345 PERROR("pthread_join");
4346 goto error
; /* join error, exit without cleanup */
4349 ret
= join_consumer_thread(&kconsumer_data
);
4351 PERROR("join_consumer");
4352 goto error
; /* join error, exit without cleanup */
4355 ret
= join_consumer_thread(&ustconsumer32_data
);
4357 PERROR("join_consumer ust32");
4358 goto error
; /* join error, exit without cleanup */
4361 ret
= join_consumer_thread(&ustconsumer64_data
);
4363 PERROR("join_consumer ust64");
4364 goto error
; /* join error, exit without cleanup */
4368 ret
= pthread_join(health_thread
, &status
);
4370 PERROR("pthread_join health thread");
4371 goto error
; /* join error, exit without cleanup */
4377 * cleanup() is called when no other thread is running.
4379 rcu_thread_online();
4381 rcu_thread_offline();
4382 rcu_unregister_thread();