2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/mount.h>
31 #include <sys/resource.h>
32 #include <sys/socket.h>
34 #include <sys/types.h>
36 #include <urcu/uatomic.h>
40 #include <common/common.h>
41 #include <common/compat/socket.h>
42 #include <common/defaults.h>
43 #include <common/kernel-consumer/kernel-consumer.h>
44 #include <common/futex.h>
45 #include <common/relayd/relayd.h>
46 #include <common/utils.h>
48 #include "lttng-sessiond.h"
49 #include "buffer-registry.h"
56 #include "kernel-consumer.h"
60 #include "ust-consumer.h"
64 #include "testpoint.h"
65 #include "ust-thread.h"
67 #define CONSUMERD_FILE "lttng-consumerd"
70 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
73 const char *opt_tracing_group
;
74 static const char *opt_pidfile
;
75 static int opt_sig_parent
;
76 static int opt_verbose_consumer
;
77 static int opt_daemon
;
78 static int opt_no_kernel
;
79 static int is_root
; /* Set to 1 if the daemon is running as root */
80 static pid_t ppid
; /* Parent PID for --sig-parent option */
84 * Consumer daemon specific control data. Every value not initialized here is
85 * set to 0 by the static definition.
87 static struct consumer_data kconsumer_data
= {
88 .type
= LTTNG_CONSUMER_KERNEL
,
89 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
90 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
93 .metadata_sock
.fd
= -1,
94 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
95 .lock
= PTHREAD_MUTEX_INITIALIZER
,
96 .cond
= PTHREAD_COND_INITIALIZER
,
97 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
99 static struct consumer_data ustconsumer64_data
= {
100 .type
= LTTNG_CONSUMER64_UST
,
101 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
102 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
105 .metadata_sock
.fd
= -1,
106 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
107 .lock
= PTHREAD_MUTEX_INITIALIZER
,
108 .cond
= PTHREAD_COND_INITIALIZER
,
109 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
111 static struct consumer_data ustconsumer32_data
= {
112 .type
= LTTNG_CONSUMER32_UST
,
113 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
114 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
117 .metadata_sock
.fd
= -1,
118 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
119 .lock
= PTHREAD_MUTEX_INITIALIZER
,
120 .cond
= PTHREAD_COND_INITIALIZER
,
121 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
124 /* Shared between threads */
125 static int dispatch_thread_exit
;
127 /* Global application Unix socket path */
128 static char apps_unix_sock_path
[PATH_MAX
];
129 /* Global client Unix socket path */
130 static char client_unix_sock_path
[PATH_MAX
];
131 /* global wait shm path for UST */
132 static char wait_shm_path
[PATH_MAX
];
133 /* Global health check unix path */
134 static char health_unix_sock_path
[PATH_MAX
];
136 /* Sockets and FDs */
137 static int client_sock
= -1;
138 static int apps_sock
= -1;
139 int kernel_tracer_fd
= -1;
140 static int kernel_poll_pipe
[2] = { -1, -1 };
143 * Quit pipe for all threads. This permits a single cancellation point
144 * for all threads when receiving an event on the pipe.
146 static int thread_quit_pipe
[2] = { -1, -1 };
149 * This pipe is used to inform the thread managing application communication
150 * that a command is queued and ready to be processed.
152 static int apps_cmd_pipe
[2] = { -1, -1 };
154 int apps_cmd_notify_pipe
[2] = { -1, -1 };
156 /* Pthread, Mutexes and Semaphores */
157 static pthread_t apps_thread
;
158 static pthread_t apps_notify_thread
;
159 static pthread_t reg_apps_thread
;
160 static pthread_t client_thread
;
161 static pthread_t kernel_thread
;
162 static pthread_t dispatch_thread
;
163 static pthread_t health_thread
;
166 * UST registration command queue. This queue is tied with a futex and uses a N
167 * wakers / 1 waiter implemented and detailed in futex.c/.h
169 * The thread_manage_apps and thread_dispatch_ust_registration interact with
170 * this queue and the wait/wake scheme.
172 static struct ust_cmd_queue ust_cmd_queue
;
175 * Pointer initialized before thread creation.
177 * This points to the tracing session list containing the session count and a
178 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
179 * MUST NOT be taken if you call a public function in session.c.
181 * The lock is nested inside the structure: session_list_ptr->lock. Please use
182 * session_lock_list and session_unlock_list for lock acquisition.
184 static struct ltt_session_list
*session_list_ptr
;
186 int ust_consumerd64_fd
= -1;
187 int ust_consumerd32_fd
= -1;
189 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
190 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
191 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
192 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
194 static const char *module_proc_lttng
= "/proc/lttng";
197 * Consumer daemon state which is changed when spawning it, killing it or in
198 * case of a fatal error.
200 enum consumerd_state
{
201 CONSUMER_STARTED
= 1,
202 CONSUMER_STOPPED
= 2,
207 * This consumer daemon state is used to validate if a client command will be
208 * able to reach the consumer. If not, the client is informed. For instance,
209 * doing a "lttng start" when the consumer state is set to ERROR will return an
210 * error to the client.
212 * The following example shows a possible race condition of this scheme:
214 * consumer thread error happens
216 * client cmd checks state -> still OK
217 * consumer thread exit, sets error
218 * client cmd try to talk to consumer
221 * However, since the consumer is a different daemon, we have no way of making
222 * sure the command will reach it safely even with this state flag. This is why
223 * we consider that up to the state validation during command processing, the
224 * command is safe. After that, we can not guarantee the correctness of the
225 * client request vis-a-vis the consumer.
227 static enum consumerd_state ust_consumerd_state
;
228 static enum consumerd_state kernel_consumerd_state
;
231 * Socket timeout for receiving and sending in seconds.
233 static int app_socket_timeout
;
235 /* Set in main() with the current page size. */
239 void setup_consumerd_path(void)
241 const char *bin
, *libdir
;
244 * Allow INSTALL_BIN_PATH to be used as a target path for the
245 * native architecture size consumer if CONFIG_CONSUMER*_PATH
246 * has not been defined.
248 #if (CAA_BITS_PER_LONG == 32)
249 if (!consumerd32_bin
[0]) {
250 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
252 if (!consumerd32_libdir
[0]) {
253 consumerd32_libdir
= INSTALL_LIB_PATH
;
255 #elif (CAA_BITS_PER_LONG == 64)
256 if (!consumerd64_bin
[0]) {
257 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
259 if (!consumerd64_libdir
[0]) {
260 consumerd64_libdir
= INSTALL_LIB_PATH
;
263 #error "Unknown bitness"
267 * runtime env. var. overrides the build default.
269 bin
= getenv("LTTNG_CONSUMERD32_BIN");
271 consumerd32_bin
= bin
;
273 bin
= getenv("LTTNG_CONSUMERD64_BIN");
275 consumerd64_bin
= bin
;
277 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
279 consumerd32_libdir
= libdir
;
281 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
283 consumerd64_libdir
= libdir
;
288 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
290 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
296 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
302 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
314 * Check if the thread quit pipe was triggered.
316 * Return 1 if it was triggered else 0;
318 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
320 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
328 * Return group ID of the tracing group or -1 if not found.
330 static gid_t
allowed_group(void)
334 if (opt_tracing_group
) {
335 grp
= getgrnam(opt_tracing_group
);
337 grp
= getgrnam(default_tracing_group
);
347 * Init thread quit pipe.
349 * Return -1 on error or 0 if all pipes are created.
351 static int init_thread_quit_pipe(void)
355 ret
= pipe(thread_quit_pipe
);
357 PERROR("thread quit pipe");
361 for (i
= 0; i
< 2; i
++) {
362 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
374 * Stop all threads by closing the thread quit pipe.
376 static void stop_threads(void)
380 /* Stopping all threads */
381 DBG("Terminating all threads");
382 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
384 ERR("write error on thread quit pipe");
387 /* Dispatch thread */
388 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
389 futex_nto1_wake(&ust_cmd_queue
.futex
);
395 static void cleanup(void)
399 struct ltt_session
*sess
, *stmp
;
403 /* First thing first, stop all threads */
404 utils_close_pipe(thread_quit_pipe
);
407 * If opt_pidfile is undefined, the default file will be wiped when
408 * removing the rundir.
411 ret
= remove(opt_pidfile
);
413 PERROR("remove pidfile %s", opt_pidfile
);
417 DBG("Removing %s directory", rundir
);
418 ret
= asprintf(&cmd
, "rm -rf %s", rundir
);
420 ERR("asprintf failed. Something is really wrong!");
423 /* Remove lttng run directory */
426 ERR("Unable to clean %s", rundir
);
431 DBG("Cleaning up all sessions");
433 /* Destroy session list mutex */
434 if (session_list_ptr
!= NULL
) {
435 pthread_mutex_destroy(&session_list_ptr
->lock
);
437 /* Cleanup ALL session */
438 cds_list_for_each_entry_safe(sess
, stmp
,
439 &session_list_ptr
->head
, list
) {
440 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
444 DBG("Closing all UST sockets");
445 ust_app_clean_list();
446 buffer_reg_destroy_registries();
448 if (is_root
&& !opt_no_kernel
) {
449 DBG2("Closing kernel fd");
450 if (kernel_tracer_fd
>= 0) {
451 ret
= close(kernel_tracer_fd
);
456 DBG("Unloading kernel modules");
457 modprobe_remove_lttng_all();
461 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
462 "Matthew, BEET driven development works!%c[%dm",
463 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
468 * Send data on a unix socket using the liblttsessiondcomm API.
470 * Return lttcomm error code.
472 static int send_unix_sock(int sock
, void *buf
, size_t len
)
474 /* Check valid length */
479 return lttcomm_send_unix_sock(sock
, buf
, len
);
483 * Free memory of a command context structure.
485 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
487 DBG("Clean command context structure");
489 if ((*cmd_ctx
)->llm
) {
490 free((*cmd_ctx
)->llm
);
492 if ((*cmd_ctx
)->lsm
) {
493 free((*cmd_ctx
)->lsm
);
501 * Notify UST applications using the shm mmap futex.
503 static int notify_ust_apps(int active
)
507 DBG("Notifying applications of session daemon state: %d", active
);
509 /* See shm.c for this call implying mmap, shm and futex calls */
510 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
511 if (wait_shm_mmap
== NULL
) {
515 /* Wake waiting process */
516 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
518 /* Apps notified successfully */
526 * Setup the outgoing data buffer for the response (llm) by allocating the
527 * right amount of memory and copying the original information from the lsm
530 * Return total size of the buffer pointed by buf.
532 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
538 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
539 if (cmd_ctx
->llm
== NULL
) {
545 /* Copy common data */
546 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
547 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
549 cmd_ctx
->llm
->data_size
= size
;
550 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
559 * Update the kernel poll set of all channel fd available over all tracing
560 * session. Add the wakeup pipe at the end of the set.
562 static int update_kernel_poll(struct lttng_poll_event
*events
)
565 struct ltt_session
*session
;
566 struct ltt_kernel_channel
*channel
;
568 DBG("Updating kernel poll set");
571 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
572 session_lock(session
);
573 if (session
->kernel_session
== NULL
) {
574 session_unlock(session
);
578 cds_list_for_each_entry(channel
,
579 &session
->kernel_session
->channel_list
.head
, list
) {
580 /* Add channel fd to the kernel poll set */
581 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
583 session_unlock(session
);
586 DBG("Channel fd %d added to kernel set", channel
->fd
);
588 session_unlock(session
);
590 session_unlock_list();
595 session_unlock_list();
600 * Find the channel fd from 'fd' over all tracing session. When found, check
601 * for new channel stream and send those stream fds to the kernel consumer.
603 * Useful for CPU hotplug feature.
605 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
608 struct ltt_session
*session
;
609 struct ltt_kernel_session
*ksess
;
610 struct ltt_kernel_channel
*channel
;
612 DBG("Updating kernel streams for channel fd %d", fd
);
615 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
616 session_lock(session
);
617 if (session
->kernel_session
== NULL
) {
618 session_unlock(session
);
621 ksess
= session
->kernel_session
;
623 cds_list_for_each_entry(channel
, &ksess
->channel_list
.head
, list
) {
624 if (channel
->fd
== fd
) {
625 DBG("Channel found, updating kernel streams");
626 ret
= kernel_open_channel_stream(channel
);
632 * Have we already sent fds to the consumer? If yes, it means
633 * that tracing is started so it is safe to send our updated
636 if (ksess
->consumer_fds_sent
== 1 && ksess
->consumer
!= NULL
) {
637 struct lttng_ht_iter iter
;
638 struct consumer_socket
*socket
;
641 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
642 &iter
.iter
, socket
, node
.node
) {
643 /* Code flow error */
644 assert(socket
->fd
>= 0);
646 pthread_mutex_lock(socket
->lock
);
647 ret
= kernel_consumer_send_channel_stream(socket
,
649 pthread_mutex_unlock(socket
->lock
);
660 session_unlock(session
);
662 session_unlock_list();
666 session_unlock(session
);
667 session_unlock_list();
672 * For each tracing session, update newly registered apps. The session list
673 * lock MUST be acquired before calling this.
675 static void update_ust_app(int app_sock
)
677 struct ltt_session
*sess
, *stmp
;
679 /* For all tracing session(s) */
680 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
682 if (sess
->ust_session
) {
683 ust_app_global_update(sess
->ust_session
, app_sock
);
685 session_unlock(sess
);
690 * This thread manage event coming from the kernel.
692 * Features supported in this thread:
695 static void *thread_manage_kernel(void *data
)
697 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
698 uint32_t revents
, nb_fd
;
700 struct lttng_poll_event events
;
702 DBG("[thread] Thread manage kernel started");
704 health_register(HEALTH_TYPE_KERNEL
);
707 * This first step of the while is to clean this structure which could free
708 * non NULL pointers so zero it before the loop.
710 memset(&events
, 0, sizeof(events
));
712 if (testpoint(thread_manage_kernel
)) {
713 goto error_testpoint
;
716 health_code_update();
718 if (testpoint(thread_manage_kernel_before_loop
)) {
719 goto error_testpoint
;
723 health_code_update();
725 if (update_poll_flag
== 1) {
726 /* Clean events object. We are about to populate it again. */
727 lttng_poll_clean(&events
);
729 ret
= sessiond_set_thread_pollset(&events
, 2);
731 goto error_poll_create
;
734 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
739 /* This will add the available kernel channel if any. */
740 ret
= update_kernel_poll(&events
);
744 update_poll_flag
= 0;
747 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events
));
749 /* Poll infinite value of time */
752 ret
= lttng_poll_wait(&events
, -1);
756 * Restart interrupted system call.
758 if (errno
== EINTR
) {
762 } else if (ret
== 0) {
763 /* Should not happen since timeout is infinite */
764 ERR("Return value of poll is 0 with an infinite timeout.\n"
765 "This should not have happened! Continuing...");
771 for (i
= 0; i
< nb_fd
; i
++) {
772 /* Fetch once the poll data */
773 revents
= LTTNG_POLL_GETEV(&events
, i
);
774 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
776 health_code_update();
778 /* Thread quit pipe has been closed. Killing thread. */
779 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
785 /* Check for data on kernel pipe */
786 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
788 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
789 } while (ret
< 0 && errno
== EINTR
);
791 * Ret value is useless here, if this pipe gets any actions an
792 * update is required anyway.
794 update_poll_flag
= 1;
798 * New CPU detected by the kernel. Adding kernel stream to
799 * kernel session and updating the kernel consumer
801 if (revents
& LPOLLIN
) {
802 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
808 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
809 * and unregister kernel stream at this point.
818 lttng_poll_clean(&events
);
821 utils_close_pipe(kernel_poll_pipe
);
822 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
825 ERR("Health error occurred in %s", __func__
);
826 WARN("Kernel thread died unexpectedly. "
827 "Kernel tracing can continue but CPU hotplug is disabled.");
830 DBG("Kernel thread dying");
835 * Signal pthread condition of the consumer data that the thread.
837 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
839 pthread_mutex_lock(&data
->cond_mutex
);
842 * The state is set before signaling. It can be any value, it's the waiter
843 * job to correctly interpret this condition variable associated to the
844 * consumer pthread_cond.
846 * A value of 0 means that the corresponding thread of the consumer data
847 * was not started. 1 indicates that the thread has started and is ready
848 * for action. A negative value means that there was an error during the
851 data
->consumer_thread_is_ready
= state
;
852 (void) pthread_cond_signal(&data
->cond
);
854 pthread_mutex_unlock(&data
->cond_mutex
);
858 * This thread manage the consumer error sent back to the session daemon.
860 static void *thread_manage_consumer(void *data
)
862 int sock
= -1, i
, ret
, pollfd
, err
= -1;
863 uint32_t revents
, nb_fd
;
864 enum lttcomm_return_code code
;
865 struct lttng_poll_event events
;
866 struct consumer_data
*consumer_data
= data
;
868 DBG("[thread] Manage consumer started");
870 health_register(HEALTH_TYPE_CONSUMER
);
872 health_code_update();
875 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
876 * metadata_sock. Nothing more will be added to this poll set.
878 ret
= sessiond_set_thread_pollset(&events
, 3);
884 * The error socket here is already in a listening state which was done
885 * just before spawning this thread to avoid a race between the consumer
886 * daemon exec trying to connect and the listen() call.
888 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
893 health_code_update();
895 /* Infinite blocking call, waiting for transmission */
899 if (testpoint(thread_manage_consumer
)) {
903 ret
= lttng_poll_wait(&events
, -1);
907 * Restart interrupted system call.
909 if (errno
== EINTR
) {
917 for (i
= 0; i
< nb_fd
; i
++) {
918 /* Fetch once the poll data */
919 revents
= LTTNG_POLL_GETEV(&events
, i
);
920 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
922 health_code_update();
924 /* Thread quit pipe has been closed. Killing thread. */
925 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
931 /* Event on the registration socket */
932 if (pollfd
== consumer_data
->err_sock
) {
933 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
934 ERR("consumer err socket poll error");
940 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
946 * Set the CLOEXEC flag. Return code is useless because either way, the
949 (void) utils_set_fd_cloexec(sock
);
951 health_code_update();
953 DBG2("Receiving code from consumer err_sock");
955 /* Getting status code from kconsumerd */
956 ret
= lttcomm_recv_unix_sock(sock
, &code
,
957 sizeof(enum lttcomm_return_code
));
962 health_code_update();
964 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
965 /* Connect both socket, command and metadata. */
966 consumer_data
->cmd_sock
=
967 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
968 consumer_data
->metadata_sock
.fd
=
969 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
970 if (consumer_data
->cmd_sock
< 0 ||
971 consumer_data
->metadata_sock
.fd
< 0) {
972 PERROR("consumer connect cmd socket");
973 /* On error, signal condition and quit. */
974 signal_consumer_condition(consumer_data
, -1);
977 /* Create metadata socket lock. */
978 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
979 if (consumer_data
->metadata_sock
.lock
== NULL
) {
980 PERROR("zmalloc pthread mutex");
984 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
986 signal_consumer_condition(consumer_data
, 1);
987 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
988 DBG("Consumer metadata socket ready (fd: %d)",
989 consumer_data
->metadata_sock
.fd
);
991 ERR("consumer error when waiting for SOCK_READY : %s",
992 lttcomm_get_readable_code(-code
));
996 /* Remove the consumerd error sock since we've established a connexion */
997 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1002 /* Add new accepted error socket. */
1003 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1008 /* Add metadata socket that is successfully connected. */
1009 ret
= lttng_poll_add(&events
, consumer_data
->metadata_sock
.fd
,
1010 LPOLLIN
| LPOLLRDHUP
);
1015 health_code_update();
1017 /* Infinite blocking call, waiting for transmission */
1020 health_poll_entry();
1021 ret
= lttng_poll_wait(&events
, -1);
1025 * Restart interrupted system call.
1027 if (errno
== EINTR
) {
1035 for (i
= 0; i
< nb_fd
; i
++) {
1036 /* Fetch once the poll data */
1037 revents
= LTTNG_POLL_GETEV(&events
, i
);
1038 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1040 health_code_update();
1042 /* Thread quit pipe has been closed. Killing thread. */
1043 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1049 if (pollfd
== sock
) {
1050 /* Event on the consumerd socket */
1051 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1052 ERR("consumer err socket second poll error");
1055 health_code_update();
1056 /* Wait for any kconsumerd error */
1057 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1058 sizeof(enum lttcomm_return_code
));
1060 ERR("consumer closed the command socket");
1064 ERR("consumer return code : %s",
1065 lttcomm_get_readable_code(-code
));
1068 } else if (pollfd
== consumer_data
->metadata_sock
.fd
) {
1069 /* UST metadata requests */
1070 ret
= ust_consumer_metadata_request(
1071 &consumer_data
->metadata_sock
);
1073 ERR("Handling metadata request");
1078 ERR("Unknown pollfd");
1082 health_code_update();
1087 /* Immediately set the consumerd state to stopped */
1088 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1089 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1090 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1091 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1092 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1094 /* Code flow error... */
1098 if (consumer_data
->err_sock
>= 0) {
1099 ret
= close(consumer_data
->err_sock
);
1104 if (consumer_data
->cmd_sock
>= 0) {
1105 ret
= close(consumer_data
->cmd_sock
);
1110 if (consumer_data
->metadata_sock
.fd
>= 0) {
1111 ret
= close(consumer_data
->metadata_sock
.fd
);
1116 /* Cleanup metadata socket mutex. */
1117 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1118 free(consumer_data
->metadata_sock
.lock
);
1127 unlink(consumer_data
->err_unix_sock_path
);
1128 unlink(consumer_data
->cmd_unix_sock_path
);
1129 consumer_data
->pid
= 0;
1131 lttng_poll_clean(&events
);
1135 ERR("Health error occurred in %s", __func__
);
1137 health_unregister();
1138 DBG("consumer thread cleanup completed");
1144 * This thread manage application communication.
1146 static void *thread_manage_apps(void *data
)
1148 int i
, ret
, pollfd
, err
= -1;
1149 uint32_t revents
, nb_fd
;
1150 struct lttng_poll_event events
;
1152 DBG("[thread] Manage application started");
1154 rcu_register_thread();
1155 rcu_thread_online();
1157 health_register(HEALTH_TYPE_APP_MANAGE
);
1159 if (testpoint(thread_manage_apps
)) {
1160 goto error_testpoint
;
1163 health_code_update();
1165 ret
= sessiond_set_thread_pollset(&events
, 2);
1167 goto error_poll_create
;
1170 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1175 if (testpoint(thread_manage_apps_before_loop
)) {
1179 health_code_update();
1182 DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events
));
1184 /* Inifinite blocking call, waiting for transmission */
1186 health_poll_entry();
1187 ret
= lttng_poll_wait(&events
, -1);
1191 * Restart interrupted system call.
1193 if (errno
== EINTR
) {
1201 for (i
= 0; i
< nb_fd
; i
++) {
1202 /* Fetch once the poll data */
1203 revents
= LTTNG_POLL_GETEV(&events
, i
);
1204 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1206 health_code_update();
1208 /* Thread quit pipe has been closed. Killing thread. */
1209 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1215 /* Inspect the apps cmd pipe */
1216 if (pollfd
== apps_cmd_pipe
[0]) {
1217 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1218 ERR("Apps command pipe error");
1220 } else if (revents
& LPOLLIN
) {
1225 ret
= read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1226 } while (ret
< 0 && errno
== EINTR
);
1227 if (ret
< 0 || ret
< sizeof(sock
)) {
1228 PERROR("read apps cmd pipe");
1232 health_code_update();
1235 * We only monitor the error events of the socket. This
1236 * thread does not handle any incoming data from UST
1239 ret
= lttng_poll_add(&events
, sock
,
1240 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1245 /* Set socket timeout for both receiving and ending */
1246 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1247 app_socket_timeout
);
1248 (void) lttcomm_setsockopt_snd_timeout(sock
,
1249 app_socket_timeout
);
1251 DBG("Apps with sock %d added to poll set", sock
);
1253 health_code_update();
1259 * At this point, we know that a registered application made
1260 * the event at poll_wait.
1262 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1263 /* Removing from the poll set */
1264 ret
= lttng_poll_del(&events
, pollfd
);
1269 /* Socket closed on remote end. */
1270 ust_app_unregister(pollfd
);
1275 health_code_update();
1281 lttng_poll_clean(&events
);
1284 utils_close_pipe(apps_cmd_pipe
);
1285 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1288 * We don't clean the UST app hash table here since already registered
1289 * applications can still be controlled so let them be until the session
1290 * daemon dies or the applications stop.
1295 ERR("Health error occurred in %s", __func__
);
1297 health_unregister();
1298 DBG("Application communication apps thread cleanup complete");
1299 rcu_thread_offline();
1300 rcu_unregister_thread();
1305 * Send a socket to a thread This is called from the dispatch UST registration
1306 * thread once all sockets are set for the application.
1308 * On success, return 0 else a negative value being the errno message of the
1311 static int send_socket_to_thread(int fd
, int sock
)
1315 /* Sockets MUST be set or else this should not have been called. */
1320 ret
= write(fd
, &sock
, sizeof(sock
));
1321 } while (ret
< 0 && errno
== EINTR
);
1322 if (ret
< 0 || ret
!= sizeof(sock
)) {
1323 PERROR("write apps pipe %d", fd
);
1330 /* All good. Don't send back the write positive ret value. */
1337 * Dispatch request from the registration threads to the application
1338 * communication thread.
1340 static void *thread_dispatch_ust_registration(void *data
)
1343 struct cds_wfq_node
*node
;
1344 struct ust_command
*ust_cmd
= NULL
;
1346 struct ust_app
*app
;
1347 struct cds_list_head head
;
1348 } *wait_node
= NULL
, *tmp_wait_node
;
1350 CDS_LIST_HEAD(wait_queue
);
1352 DBG("[thread] Dispatch UST command started");
1354 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1355 /* Atomically prepare the queue futex */
1356 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1359 struct ust_app
*app
= NULL
;
1362 /* Dequeue command for registration */
1363 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1365 DBG("Woken up but nothing in the UST command queue");
1366 /* Continue thread execution */
1370 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1372 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1373 " gid:%d sock:%d name:%s (version %d.%d)",
1374 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1375 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1376 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1377 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1379 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1380 wait_node
= zmalloc(sizeof(*wait_node
));
1382 PERROR("zmalloc wait_node dispatch");
1383 ret
= close(ust_cmd
->sock
);
1385 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1387 lttng_fd_put(1, LTTNG_FD_APPS
);
1391 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1393 /* Create application object if socket is CMD. */
1394 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1396 if (!wait_node
->app
) {
1397 ret
= close(ust_cmd
->sock
);
1399 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1401 lttng_fd_put(1, LTTNG_FD_APPS
);
1407 * Add application to the wait queue so we can set the notify
1408 * socket before putting this object in the global ht.
1410 cds_list_add(&wait_node
->head
, &wait_queue
);
1414 * We have to continue here since we don't have the notify
1415 * socket and the application MUST be added to the hash table
1416 * only at that moment.
1421 * Look for the application in the local wait queue and set the
1422 * notify socket if found.
1424 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1425 &wait_queue
, head
) {
1426 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1427 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1428 cds_list_del(&wait_node
->head
);
1429 app
= wait_node
->app
;
1431 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1437 * With no application at this stage the received socket is
1438 * basically useless so close it before we free the cmd data
1439 * structure for good.
1442 ret
= close(ust_cmd
->sock
);
1444 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1446 lttng_fd_put(1, LTTNG_FD_APPS
);
1453 * @session_lock_list
1455 * Lock the global session list so from the register up to the
1456 * registration done message, no thread can see the application
1457 * and change its state.
1459 session_lock_list();
1463 * Add application to the global hash table. This needs to be
1464 * done before the update to the UST registry can locate the
1469 /* Set app version. This call will print an error if needed. */
1470 (void) ust_app_version(app
);
1472 /* Send notify socket through the notify pipe. */
1473 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1477 session_unlock_list();
1478 /* No notify thread, stop the UST tracing. */
1483 * Update newly registered application with the tracing
1484 * registry info already enabled information.
1486 update_ust_app(app
->sock
);
1489 * Don't care about return value. Let the manage apps threads
1490 * handle app unregistration upon socket close.
1492 (void) ust_app_register_done(app
->sock
);
1495 * Even if the application socket has been closed, send the app
1496 * to the thread and unregistration will take place at that
1499 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1502 session_unlock_list();
1503 /* No apps. thread, stop the UST tracing. */
1508 session_unlock_list();
1510 } while (node
!= NULL
);
1512 /* Futex wait on queue. Blocking call on futex() */
1513 futex_nto1_wait(&ust_cmd_queue
.futex
);
1517 /* Clean up wait queue. */
1518 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1519 &wait_queue
, head
) {
1520 cds_list_del(&wait_node
->head
);
1524 DBG("Dispatch thread dying");
1529 * This thread manage application registration.
1531 static void *thread_registration_apps(void *data
)
1533 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1534 uint32_t revents
, nb_fd
;
1535 struct lttng_poll_event events
;
1537 * Get allocated in this thread, enqueued to a global queue, dequeued and
1538 * freed in the manage apps thread.
1540 struct ust_command
*ust_cmd
= NULL
;
1542 DBG("[thread] Manage application registration started");
1544 health_register(HEALTH_TYPE_APP_REG
);
1546 if (testpoint(thread_registration_apps
)) {
1547 goto error_testpoint
;
1550 ret
= lttcomm_listen_unix_sock(apps_sock
);
1556 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1557 * more will be added to this poll set.
1559 ret
= sessiond_set_thread_pollset(&events
, 2);
1561 goto error_create_poll
;
1564 /* Add the application registration socket */
1565 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1567 goto error_poll_add
;
1570 /* Notify all applications to register */
1571 ret
= notify_ust_apps(1);
1573 ERR("Failed to notify applications or create the wait shared memory.\n"
1574 "Execution continues but there might be problem for already\n"
1575 "running applications that wishes to register.");
1579 DBG("Accepting application registration");
1581 /* Inifinite blocking call, waiting for transmission */
1583 health_poll_entry();
1584 ret
= lttng_poll_wait(&events
, -1);
1588 * Restart interrupted system call.
1590 if (errno
== EINTR
) {
1598 for (i
= 0; i
< nb_fd
; i
++) {
1599 health_code_update();
1601 /* Fetch once the poll data */
1602 revents
= LTTNG_POLL_GETEV(&events
, i
);
1603 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1605 /* Thread quit pipe has been closed. Killing thread. */
1606 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1612 /* Event on the registration socket */
1613 if (pollfd
== apps_sock
) {
1614 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1615 ERR("Register apps socket poll error");
1617 } else if (revents
& LPOLLIN
) {
1618 sock
= lttcomm_accept_unix_sock(apps_sock
);
1624 * Set the CLOEXEC flag. Return code is useless because
1625 * either way, the show must go on.
1627 (void) utils_set_fd_cloexec(sock
);
1629 /* Create UST registration command for enqueuing */
1630 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1631 if (ust_cmd
== NULL
) {
1632 PERROR("ust command zmalloc");
1637 * Using message-based transmissions to ensure we don't
1638 * have to deal with partially received messages.
1640 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1642 ERR("Exhausted file descriptors allowed for applications.");
1652 health_code_update();
1653 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
1656 /* Close socket of the application. */
1661 lttng_fd_put(LTTNG_FD_APPS
, 1);
1665 health_code_update();
1667 ust_cmd
->sock
= sock
;
1670 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1671 " gid:%d sock:%d name:%s (version %d.%d)",
1672 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1673 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1674 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1675 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1678 * Lock free enqueue the registration request. The red pill
1679 * has been taken! This apps will be part of the *system*.
1681 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1684 * Wake the registration queue futex. Implicit memory
1685 * barrier with the exchange in cds_wfq_enqueue.
1687 futex_nto1_wake(&ust_cmd_queue
.futex
);
1697 ERR("Health error occurred in %s", __func__
);
1700 /* Notify that the registration thread is gone */
1703 if (apps_sock
>= 0) {
1704 ret
= close(apps_sock
);
1714 lttng_fd_put(LTTNG_FD_APPS
, 1);
1716 unlink(apps_unix_sock_path
);
1719 lttng_poll_clean(&events
);
1723 DBG("UST Registration thread cleanup complete");
1724 health_unregister();
1730 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1731 * exec or it will fails.
1733 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
1736 struct timespec timeout
;
1738 /* Make sure we set the readiness flag to 0 because we are NOT ready */
1739 consumer_data
->consumer_thread_is_ready
= 0;
1741 /* Setup pthread condition */
1742 ret
= pthread_condattr_init(&consumer_data
->condattr
);
1745 PERROR("pthread_condattr_init consumer data");
1750 * Set the monotonic clock in order to make sure we DO NOT jump in time
1751 * between the clock_gettime() call and the timedwait call. See bug #324
1752 * for a more details and how we noticed it.
1754 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
1757 PERROR("pthread_condattr_setclock consumer data");
1761 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
1764 PERROR("pthread_cond_init consumer data");
1768 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
1771 PERROR("pthread_create consumer");
1776 /* We are about to wait on a pthread condition */
1777 pthread_mutex_lock(&consumer_data
->cond_mutex
);
1779 /* Get time for sem_timedwait absolute timeout */
1780 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
1782 * Set the timeout for the condition timed wait even if the clock gettime
1783 * call fails since we might loop on that call and we want to avoid to
1784 * increment the timeout too many times.
1786 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
1789 * The following loop COULD be skipped in some conditions so this is why we
1790 * set ret to 0 in order to make sure at least one round of the loop is
1796 * Loop until the condition is reached or when a timeout is reached. Note
1797 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
1798 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
1799 * possible. This loop does not take any chances and works with both of
1802 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
1803 if (clock_ret
< 0) {
1804 PERROR("clock_gettime spawn consumer");
1805 /* Infinite wait for the consumerd thread to be ready */
1806 ret
= pthread_cond_wait(&consumer_data
->cond
,
1807 &consumer_data
->cond_mutex
);
1809 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
1810 &consumer_data
->cond_mutex
, &timeout
);
1814 /* Release the pthread condition */
1815 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
1819 if (ret
== ETIMEDOUT
) {
1821 * Call has timed out so we kill the kconsumerd_thread and return
1824 ERR("Condition timed out. The consumer thread was never ready."
1826 ret
= pthread_cancel(consumer_data
->thread
);
1828 PERROR("pthread_cancel consumer thread");
1831 PERROR("pthread_cond_wait failed consumer thread");
1836 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1837 if (consumer_data
->pid
== 0) {
1838 ERR("Consumerd did not start");
1839 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1842 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1851 * Join consumer thread
1853 static int join_consumer_thread(struct consumer_data
*consumer_data
)
1857 /* Consumer pid must be a real one. */
1858 if (consumer_data
->pid
> 0) {
1860 ret
= kill(consumer_data
->pid
, SIGTERM
);
1862 ERR("Error killing consumer daemon");
1865 return pthread_join(consumer_data
->thread
, &status
);
1872 * Fork and exec a consumer daemon (consumerd).
1874 * Return pid if successful else -1.
1876 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
1880 const char *consumer_to_use
;
1881 const char *verbosity
;
1884 DBG("Spawning consumerd");
1891 if (opt_verbose_consumer
) {
1892 verbosity
= "--verbose";
1894 verbosity
= "--quiet";
1896 switch (consumer_data
->type
) {
1897 case LTTNG_CONSUMER_KERNEL
:
1899 * Find out which consumerd to execute. We will first try the
1900 * 64-bit path, then the sessiond's installation directory, and
1901 * fallback on the 32-bit one,
1903 DBG3("Looking for a kernel consumer at these locations:");
1904 DBG3(" 1) %s", consumerd64_bin
);
1905 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
1906 DBG3(" 3) %s", consumerd32_bin
);
1907 if (stat(consumerd64_bin
, &st
) == 0) {
1908 DBG3("Found location #1");
1909 consumer_to_use
= consumerd64_bin
;
1910 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
1911 DBG3("Found location #2");
1912 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
1913 } else if (stat(consumerd32_bin
, &st
) == 0) {
1914 DBG3("Found location #3");
1915 consumer_to_use
= consumerd32_bin
;
1917 DBG("Could not find any valid consumerd executable");
1920 DBG("Using kernel consumer at: %s", consumer_to_use
);
1921 execl(consumer_to_use
,
1922 "lttng-consumerd", verbosity
, "-k",
1923 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1924 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1927 case LTTNG_CONSUMER64_UST
:
1929 char *tmpnew
= NULL
;
1931 if (consumerd64_libdir
[0] != '\0') {
1935 tmp
= getenv("LD_LIBRARY_PATH");
1939 tmplen
= strlen("LD_LIBRARY_PATH=")
1940 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
1941 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1946 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1947 strcat(tmpnew
, consumerd64_libdir
);
1948 if (tmp
[0] != '\0') {
1949 strcat(tmpnew
, ":");
1950 strcat(tmpnew
, tmp
);
1952 ret
= putenv(tmpnew
);
1959 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
1960 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
1961 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1962 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1964 if (consumerd64_libdir
[0] != '\0') {
1972 case LTTNG_CONSUMER32_UST
:
1974 char *tmpnew
= NULL
;
1976 if (consumerd32_libdir
[0] != '\0') {
1980 tmp
= getenv("LD_LIBRARY_PATH");
1984 tmplen
= strlen("LD_LIBRARY_PATH=")
1985 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
1986 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1991 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1992 strcat(tmpnew
, consumerd32_libdir
);
1993 if (tmp
[0] != '\0') {
1994 strcat(tmpnew
, ":");
1995 strcat(tmpnew
, tmp
);
1997 ret
= putenv(tmpnew
);
2004 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2005 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2006 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2007 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2009 if (consumerd32_libdir
[0] != '\0') {
2018 PERROR("unknown consumer type");
2022 PERROR("kernel start consumer exec");
2025 } else if (pid
> 0) {
2028 PERROR("start consumer fork");
2036 * Spawn the consumerd daemon and session daemon thread.
2038 static int start_consumerd(struct consumer_data
*consumer_data
)
2043 * Set the listen() state on the socket since there is a possible race
2044 * between the exec() of the consumer daemon and this call if place in the
2045 * consumer thread. See bug #366 for more details.
2047 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2052 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2053 if (consumer_data
->pid
!= 0) {
2054 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2058 ret
= spawn_consumerd(consumer_data
);
2060 ERR("Spawning consumerd failed");
2061 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2065 /* Setting up the consumer_data pid */
2066 consumer_data
->pid
= ret
;
2067 DBG2("Consumer pid %d", consumer_data
->pid
);
2068 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2070 DBG2("Spawning consumer control thread");
2071 ret
= spawn_consumer_thread(consumer_data
);
2073 ERR("Fatal error spawning consumer control thread");
2081 /* Cleanup already created sockets on error. */
2082 if (consumer_data
->err_sock
>= 0) {
2085 err
= close(consumer_data
->err_sock
);
2087 PERROR("close consumer data error socket");
2094 * Compute health status of each consumer. If one of them is zero (bad
2095 * state), we return 0.
2097 static int check_consumer_health(void)
2101 ret
= health_check_state(HEALTH_TYPE_CONSUMER
);
2103 DBG3("Health consumer check %d", ret
);
2109 * Setup necessary data for kernel tracer action.
2111 static int init_kernel_tracer(void)
2115 /* Modprobe lttng kernel modules */
2116 ret
= modprobe_lttng_control();
2121 /* Open debugfs lttng */
2122 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2123 if (kernel_tracer_fd
< 0) {
2124 DBG("Failed to open %s", module_proc_lttng
);
2129 /* Validate kernel version */
2130 ret
= kernel_validate_version(kernel_tracer_fd
);
2135 ret
= modprobe_lttng_data();
2140 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2144 modprobe_remove_lttng_control();
2145 ret
= close(kernel_tracer_fd
);
2149 kernel_tracer_fd
= -1;
2150 return LTTNG_ERR_KERN_VERSION
;
2153 ret
= close(kernel_tracer_fd
);
2159 modprobe_remove_lttng_control();
2162 WARN("No kernel tracer available");
2163 kernel_tracer_fd
= -1;
2165 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2167 return LTTNG_ERR_KERN_NA
;
2173 * Copy consumer output from the tracing session to the domain session. The
2174 * function also applies the right modification on a per domain basis for the
2175 * trace files destination directory.
2177 * Should *NOT* be called with RCU read-side lock held.
2179 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2182 const char *dir_name
;
2183 struct consumer_output
*consumer
;
2186 assert(session
->consumer
);
2189 case LTTNG_DOMAIN_KERNEL
:
2190 DBG3("Copying tracing session consumer output in kernel session");
2192 * XXX: We should audit the session creation and what this function
2193 * does "extra" in order to avoid a destroy since this function is used
2194 * in the domain session creation (kernel and ust) only. Same for UST
2197 if (session
->kernel_session
->consumer
) {
2198 consumer_destroy_output(session
->kernel_session
->consumer
);
2200 session
->kernel_session
->consumer
=
2201 consumer_copy_output(session
->consumer
);
2202 /* Ease our life a bit for the next part */
2203 consumer
= session
->kernel_session
->consumer
;
2204 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2206 case LTTNG_DOMAIN_UST
:
2207 DBG3("Copying tracing session consumer output in UST session");
2208 if (session
->ust_session
->consumer
) {
2209 consumer_destroy_output(session
->ust_session
->consumer
);
2211 session
->ust_session
->consumer
=
2212 consumer_copy_output(session
->consumer
);
2213 /* Ease our life a bit for the next part */
2214 consumer
= session
->ust_session
->consumer
;
2215 dir_name
= DEFAULT_UST_TRACE_DIR
;
2218 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2222 /* Append correct directory to subdir */
2223 strncat(consumer
->subdir
, dir_name
,
2224 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2225 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2234 * Create an UST session and add it to the session ust list.
2236 * Should *NOT* be called with RCU read-side lock held.
2238 static int create_ust_session(struct ltt_session
*session
,
2239 struct lttng_domain
*domain
)
2242 struct ltt_ust_session
*lus
= NULL
;
2246 assert(session
->consumer
);
2248 switch (domain
->type
) {
2249 case LTTNG_DOMAIN_UST
:
2252 ERR("Unknown UST domain on create session %d", domain
->type
);
2253 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2257 DBG("Creating UST session");
2259 lus
= trace_ust_create_session(session
->id
);
2261 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2265 lus
->uid
= session
->uid
;
2266 lus
->gid
= session
->gid
;
2267 session
->ust_session
= lus
;
2269 /* Copy session output to the newly created UST session */
2270 ret
= copy_session_consumer(domain
->type
, session
);
2271 if (ret
!= LTTNG_OK
) {
2279 session
->ust_session
= NULL
;
2284 * Create a kernel tracer session then create the default channel.
2286 static int create_kernel_session(struct ltt_session
*session
)
2290 DBG("Creating kernel session");
2292 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2294 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2298 /* Code flow safety */
2299 assert(session
->kernel_session
);
2301 /* Copy session output to the newly created Kernel session */
2302 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2303 if (ret
!= LTTNG_OK
) {
2307 /* Create directory(ies) on local filesystem. */
2308 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2309 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2310 ret
= run_as_mkdir_recursive(
2311 session
->kernel_session
->consumer
->dst
.trace_path
,
2312 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2314 if (ret
!= -EEXIST
) {
2315 ERR("Trace directory creation error");
2321 session
->kernel_session
->uid
= session
->uid
;
2322 session
->kernel_session
->gid
= session
->gid
;
2327 trace_kernel_destroy_session(session
->kernel_session
);
2328 session
->kernel_session
= NULL
;
2333 * Count number of session permitted by uid/gid.
2335 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2338 struct ltt_session
*session
;
2340 DBG("Counting number of available session for UID %d GID %d",
2342 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2344 * Only list the sessions the user can control.
2346 if (!session_access_ok(session
, uid
, gid
)) {
2355 * Process the command requested by the lttng client within the command
2356 * context structure. This function make sure that the return structure (llm)
2357 * is set and ready for transmission before returning.
2359 * Return any error encountered or 0 for success.
2361 * "sock" is only used for special-case var. len data.
2363 * Should *NOT* be called with RCU read-side lock held.
2365 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2369 int need_tracing_session
= 1;
2372 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2376 switch (cmd_ctx
->lsm
->cmd_type
) {
2377 case LTTNG_CREATE_SESSION
:
2378 case LTTNG_DESTROY_SESSION
:
2379 case LTTNG_LIST_SESSIONS
:
2380 case LTTNG_LIST_DOMAINS
:
2381 case LTTNG_START_TRACE
:
2382 case LTTNG_STOP_TRACE
:
2383 case LTTNG_DATA_PENDING
:
2390 if (opt_no_kernel
&& need_domain
2391 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2393 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2395 ret
= LTTNG_ERR_KERN_NA
;
2400 /* Deny register consumer if we already have a spawned consumer. */
2401 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2402 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2403 if (kconsumer_data
.pid
> 0) {
2404 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2405 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2408 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2412 * Check for command that don't needs to allocate a returned payload. We do
2413 * this here so we don't have to make the call for no payload at each
2416 switch(cmd_ctx
->lsm
->cmd_type
) {
2417 case LTTNG_LIST_SESSIONS
:
2418 case LTTNG_LIST_TRACEPOINTS
:
2419 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2420 case LTTNG_LIST_DOMAINS
:
2421 case LTTNG_LIST_CHANNELS
:
2422 case LTTNG_LIST_EVENTS
:
2425 /* Setup lttng message with no payload */
2426 ret
= setup_lttng_msg(cmd_ctx
, 0);
2428 /* This label does not try to unlock the session */
2429 goto init_setup_error
;
2433 /* Commands that DO NOT need a session. */
2434 switch (cmd_ctx
->lsm
->cmd_type
) {
2435 case LTTNG_CREATE_SESSION
:
2436 case LTTNG_CALIBRATE
:
2437 case LTTNG_LIST_SESSIONS
:
2438 case LTTNG_LIST_TRACEPOINTS
:
2439 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2440 need_tracing_session
= 0;
2443 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2445 * We keep the session list lock across _all_ commands
2446 * for now, because the per-session lock does not
2447 * handle teardown properly.
2449 session_lock_list();
2450 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2451 if (cmd_ctx
->session
== NULL
) {
2452 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
2453 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2455 /* If no session name specified */
2456 ret
= LTTNG_ERR_SELECT_SESS
;
2460 /* Acquire lock for the session */
2461 session_lock(cmd_ctx
->session
);
2471 * Check domain type for specific "pre-action".
2473 switch (cmd_ctx
->lsm
->domain
.type
) {
2474 case LTTNG_DOMAIN_KERNEL
:
2476 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2480 /* Kernel tracer check */
2481 if (kernel_tracer_fd
== -1) {
2482 /* Basically, load kernel tracer modules */
2483 ret
= init_kernel_tracer();
2489 /* Consumer is in an ERROR state. Report back to client */
2490 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
2491 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2495 /* Need a session for kernel command */
2496 if (need_tracing_session
) {
2497 if (cmd_ctx
->session
->kernel_session
== NULL
) {
2498 ret
= create_kernel_session(cmd_ctx
->session
);
2500 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2505 /* Start the kernel consumer daemon */
2506 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2507 if (kconsumer_data
.pid
== 0 &&
2508 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2509 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2510 ret
= start_consumerd(&kconsumer_data
);
2512 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2515 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
2517 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2521 * The consumer was just spawned so we need to add the socket to
2522 * the consumer output of the session if exist.
2524 ret
= consumer_create_socket(&kconsumer_data
,
2525 cmd_ctx
->session
->kernel_session
->consumer
);
2532 case LTTNG_DOMAIN_UST
:
2534 /* Consumer is in an ERROR state. Report back to client */
2535 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
2536 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2540 if (need_tracing_session
) {
2541 /* Create UST session if none exist. */
2542 if (cmd_ctx
->session
->ust_session
== NULL
) {
2543 ret
= create_ust_session(cmd_ctx
->session
,
2544 &cmd_ctx
->lsm
->domain
);
2545 if (ret
!= LTTNG_OK
) {
2550 /* Start the UST consumer daemons */
2552 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
2553 if (consumerd64_bin
[0] != '\0' &&
2554 ustconsumer64_data
.pid
== 0 &&
2555 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2556 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2557 ret
= start_consumerd(&ustconsumer64_data
);
2559 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
2560 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
2564 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
2565 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2567 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2571 * Setup socket for consumer 64 bit. No need for atomic access
2572 * since it was set above and can ONLY be set in this thread.
2574 ret
= consumer_create_socket(&ustconsumer64_data
,
2575 cmd_ctx
->session
->ust_session
->consumer
);
2581 if (consumerd32_bin
[0] != '\0' &&
2582 ustconsumer32_data
.pid
== 0 &&
2583 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2584 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2585 ret
= start_consumerd(&ustconsumer32_data
);
2587 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
2588 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
2592 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
2593 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2595 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2599 * Setup socket for consumer 64 bit. No need for atomic access
2600 * since it was set above and can ONLY be set in this thread.
2602 ret
= consumer_create_socket(&ustconsumer32_data
,
2603 cmd_ctx
->session
->ust_session
->consumer
);
2615 /* Validate consumer daemon state when start/stop trace command */
2616 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
2617 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
2618 switch (cmd_ctx
->lsm
->domain
.type
) {
2619 case LTTNG_DOMAIN_UST
:
2620 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
2621 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2625 case LTTNG_DOMAIN_KERNEL
:
2626 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
2627 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2635 * Check that the UID or GID match that of the tracing session.
2636 * The root user can interact with all sessions.
2638 if (need_tracing_session
) {
2639 if (!session_access_ok(cmd_ctx
->session
,
2640 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2641 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
2642 ret
= LTTNG_ERR_EPERM
;
2648 * Send relayd information to consumer as soon as we have a domain and a
2651 if (cmd_ctx
->session
&& need_domain
) {
2653 * Setup relayd if not done yet. If the relayd information was already
2654 * sent to the consumer, this call will gracefully return.
2656 ret
= cmd_setup_relayd(cmd_ctx
->session
);
2657 if (ret
!= LTTNG_OK
) {
2662 /* Process by command type */
2663 switch (cmd_ctx
->lsm
->cmd_type
) {
2664 case LTTNG_ADD_CONTEXT
:
2666 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2667 cmd_ctx
->lsm
->u
.context
.channel_name
,
2668 &cmd_ctx
->lsm
->u
.context
.ctx
, kernel_poll_pipe
[1]);
2671 case LTTNG_DISABLE_CHANNEL
:
2673 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2674 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2677 case LTTNG_DISABLE_EVENT
:
2679 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2680 cmd_ctx
->lsm
->u
.disable
.channel_name
,
2681 cmd_ctx
->lsm
->u
.disable
.name
);
2684 case LTTNG_DISABLE_ALL_EVENT
:
2686 DBG("Disabling all events");
2688 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2689 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2692 case LTTNG_ENABLE_CHANNEL
:
2694 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
2695 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
2698 case LTTNG_ENABLE_EVENT
:
2700 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
2701 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2702 &cmd_ctx
->lsm
->u
.enable
.event
, NULL
, kernel_poll_pipe
[1]);
2705 case LTTNG_ENABLE_ALL_EVENT
:
2707 DBG("Enabling all events");
2709 ret
= cmd_enable_event_all(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
2710 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2711 cmd_ctx
->lsm
->u
.enable
.event
.type
, NULL
, kernel_poll_pipe
[1]);
2714 case LTTNG_LIST_TRACEPOINTS
:
2716 struct lttng_event
*events
;
2719 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
2720 if (nb_events
< 0) {
2721 /* Return value is a negative lttng_error_code. */
2727 * Setup lttng message with payload size set to the event list size in
2728 * bytes and then copy list into the llm payload.
2730 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2736 /* Copy event list into message payload */
2737 memcpy(cmd_ctx
->llm
->payload
, events
,
2738 sizeof(struct lttng_event
) * nb_events
);
2745 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2747 struct lttng_event_field
*fields
;
2750 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
2752 if (nb_fields
< 0) {
2753 /* Return value is a negative lttng_error_code. */
2759 * Setup lttng message with payload size set to the event list size in
2760 * bytes and then copy list into the llm payload.
2762 ret
= setup_lttng_msg(cmd_ctx
,
2763 sizeof(struct lttng_event_field
) * nb_fields
);
2769 /* Copy event list into message payload */
2770 memcpy(cmd_ctx
->llm
->payload
, fields
,
2771 sizeof(struct lttng_event_field
) * nb_fields
);
2778 case LTTNG_SET_CONSUMER_URI
:
2781 struct lttng_uri
*uris
;
2783 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
2784 len
= nb_uri
* sizeof(struct lttng_uri
);
2787 ret
= LTTNG_ERR_INVALID
;
2791 uris
= zmalloc(len
);
2793 ret
= LTTNG_ERR_FATAL
;
2797 /* Receive variable len data */
2798 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
2799 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
2801 DBG("No URIs received from client... continuing");
2803 ret
= LTTNG_ERR_SESSION_FAIL
;
2808 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
2810 if (ret
!= LTTNG_OK
) {
2816 * XXX: 0 means that this URI should be applied on the session. Should
2817 * be a DOMAIN enuam.
2819 if (cmd_ctx
->lsm
->domain
.type
== 0) {
2820 /* Add the URI for the UST session if a consumer is present. */
2821 if (cmd_ctx
->session
->ust_session
&&
2822 cmd_ctx
->session
->ust_session
->consumer
) {
2823 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_UST
, cmd_ctx
->session
,
2825 } else if (cmd_ctx
->session
->kernel_session
&&
2826 cmd_ctx
->session
->kernel_session
->consumer
) {
2827 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL
,
2828 cmd_ctx
->session
, nb_uri
, uris
);
2836 case LTTNG_START_TRACE
:
2838 ret
= cmd_start_trace(cmd_ctx
->session
);
2841 case LTTNG_STOP_TRACE
:
2843 ret
= cmd_stop_trace(cmd_ctx
->session
);
2846 case LTTNG_CREATE_SESSION
:
2849 struct lttng_uri
*uris
= NULL
;
2851 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
2852 len
= nb_uri
* sizeof(struct lttng_uri
);
2855 uris
= zmalloc(len
);
2857 ret
= LTTNG_ERR_FATAL
;
2861 /* Receive variable len data */
2862 DBG("Waiting for %zu URIs from client ...", nb_uri
);
2863 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
2865 DBG("No URIs received from client... continuing");
2867 ret
= LTTNG_ERR_SESSION_FAIL
;
2872 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
2873 DBG("Creating session with ONE network URI is a bad call");
2874 ret
= LTTNG_ERR_SESSION_FAIL
;
2880 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
2887 case LTTNG_DESTROY_SESSION
:
2889 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
2891 /* Set session to NULL so we do not unlock it after free. */
2892 cmd_ctx
->session
= NULL
;
2895 case LTTNG_LIST_DOMAINS
:
2898 struct lttng_domain
*domains
;
2900 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
2902 /* Return value is a negative lttng_error_code. */
2907 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
2913 /* Copy event list into message payload */
2914 memcpy(cmd_ctx
->llm
->payload
, domains
,
2915 nb_dom
* sizeof(struct lttng_domain
));
2922 case LTTNG_LIST_CHANNELS
:
2925 struct lttng_channel
*channels
;
2927 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
2928 cmd_ctx
->session
, &channels
);
2930 /* Return value is a negative lttng_error_code. */
2935 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
2941 /* Copy event list into message payload */
2942 memcpy(cmd_ctx
->llm
->payload
, channels
,
2943 nb_chan
* sizeof(struct lttng_channel
));
2950 case LTTNG_LIST_EVENTS
:
2953 struct lttng_event
*events
= NULL
;
2955 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
2956 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
2958 /* Return value is a negative lttng_error_code. */
2963 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
2969 /* Copy event list into message payload */
2970 memcpy(cmd_ctx
->llm
->payload
, events
,
2971 nb_event
* sizeof(struct lttng_event
));
2978 case LTTNG_LIST_SESSIONS
:
2980 unsigned int nr_sessions
;
2982 session_lock_list();
2983 nr_sessions
= lttng_sessions_count(
2984 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2985 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
2987 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
2989 session_unlock_list();
2993 /* Filled the session array */
2994 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
2995 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2996 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
2998 session_unlock_list();
3003 case LTTNG_CALIBRATE
:
3005 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3006 &cmd_ctx
->lsm
->u
.calibrate
);
3009 case LTTNG_REGISTER_CONSUMER
:
3011 struct consumer_data
*cdata
;
3013 switch (cmd_ctx
->lsm
->domain
.type
) {
3014 case LTTNG_DOMAIN_KERNEL
:
3015 cdata
= &kconsumer_data
;
3018 ret
= LTTNG_ERR_UND
;
3022 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3023 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3026 case LTTNG_ENABLE_EVENT_WITH_FILTER
:
3028 struct lttng_filter_bytecode
*bytecode
;
3030 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3031 ret
= LTTNG_ERR_FILTER_INVAL
;
3034 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
== 0) {
3035 ret
= LTTNG_ERR_FILTER_INVAL
;
3038 bytecode
= zmalloc(cmd_ctx
->lsm
->u
.enable
.bytecode_len
);
3040 ret
= LTTNG_ERR_FILTER_NOMEM
;
3043 /* Receive var. len. data */
3044 DBG("Receiving var len data from client ...");
3045 ret
= lttcomm_recv_unix_sock(sock
, bytecode
,
3046 cmd_ctx
->lsm
->u
.enable
.bytecode_len
);
3048 DBG("Nothing recv() from client var len data... continuing");
3050 ret
= LTTNG_ERR_FILTER_INVAL
;
3054 if (bytecode
->len
+ sizeof(*bytecode
)
3055 != cmd_ctx
->lsm
->u
.enable
.bytecode_len
) {
3057 ret
= LTTNG_ERR_FILTER_INVAL
;
3061 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3062 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3063 &cmd_ctx
->lsm
->u
.enable
.event
, bytecode
, kernel_poll_pipe
[1]);
3066 case LTTNG_DATA_PENDING
:
3068 ret
= cmd_data_pending(cmd_ctx
->session
);
3072 ret
= LTTNG_ERR_UND
;
3077 if (cmd_ctx
->llm
== NULL
) {
3078 DBG("Missing llm structure. Allocating one.");
3079 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
3083 /* Set return code */
3084 cmd_ctx
->llm
->ret_code
= ret
;
3086 if (cmd_ctx
->session
) {
3087 session_unlock(cmd_ctx
->session
);
3089 if (need_tracing_session
) {
3090 session_unlock_list();
3097 * Thread managing health check socket.
3099 static void *thread_manage_health(void *data
)
3101 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
3102 uint32_t revents
, nb_fd
;
3103 struct lttng_poll_event events
;
3104 struct lttcomm_health_msg msg
;
3105 struct lttcomm_health_data reply
;
3107 DBG("[thread] Manage health check started");
3109 rcu_register_thread();
3111 /* We might hit an error path before this is set once. */
3112 memset(&events
, 0, sizeof(events
));
3115 /* Create unix socket */
3116 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
3118 ERR("Unable to create health check Unix socket");
3124 * Set the CLOEXEC flag. Return code is useless because either way, the
3127 (void) utils_set_fd_cloexec(sock
);
3129 ret
= lttcomm_listen_unix_sock(sock
);
3135 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3136 * more will be added to this poll set.
3138 ret
= sessiond_set_thread_pollset(&events
, 2);
3143 /* Add the application registration socket */
3144 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
3150 DBG("Health check ready");
3152 /* Inifinite blocking call, waiting for transmission */
3154 ret
= lttng_poll_wait(&events
, -1);
3157 * Restart interrupted system call.
3159 if (errno
== EINTR
) {
3167 for (i
= 0; i
< nb_fd
; i
++) {
3168 /* Fetch once the poll data */
3169 revents
= LTTNG_POLL_GETEV(&events
, i
);
3170 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3172 /* Thread quit pipe has been closed. Killing thread. */
3173 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3179 /* Event on the registration socket */
3180 if (pollfd
== sock
) {
3181 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3182 ERR("Health socket poll error");
3188 new_sock
= lttcomm_accept_unix_sock(sock
);
3194 * Set the CLOEXEC flag. Return code is useless because either way, the
3197 (void) utils_set_fd_cloexec(new_sock
);
3199 DBG("Receiving data from client for health...");
3200 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
3202 DBG("Nothing recv() from client... continuing");
3203 ret
= close(new_sock
);
3211 rcu_thread_online();
3213 switch (msg
.component
) {
3214 case LTTNG_HEALTH_CMD
:
3215 reply
.ret_code
= health_check_state(HEALTH_TYPE_CMD
);
3217 case LTTNG_HEALTH_APP_MANAGE
:
3218 reply
.ret_code
= health_check_state(HEALTH_TYPE_APP_MANAGE
);
3220 case LTTNG_HEALTH_APP_REG
:
3221 reply
.ret_code
= health_check_state(HEALTH_TYPE_APP_REG
);
3223 case LTTNG_HEALTH_KERNEL
:
3224 reply
.ret_code
= health_check_state(HEALTH_TYPE_KERNEL
);
3226 case LTTNG_HEALTH_CONSUMER
:
3227 reply
.ret_code
= check_consumer_health();
3229 case LTTNG_HEALTH_ALL
:
3231 health_check_state(HEALTH_TYPE_APP_MANAGE
) &&
3232 health_check_state(HEALTH_TYPE_APP_REG
) &&
3233 health_check_state(HEALTH_TYPE_CMD
) &&
3234 health_check_state(HEALTH_TYPE_KERNEL
) &&
3235 check_consumer_health();
3238 reply
.ret_code
= LTTNG_ERR_UND
;
3243 * Flip ret value since 0 is a success and 1 indicates a bad health for
3244 * the client where in the sessiond it is the opposite. Again, this is
3245 * just to make things easier for us poor developer which enjoy a lot
3248 if (reply
.ret_code
== 0 || reply
.ret_code
== 1) {
3249 reply
.ret_code
= !reply
.ret_code
;
3252 DBG2("Health check return value %d", reply
.ret_code
);
3254 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
3256 ERR("Failed to send health data back to client");
3259 /* End of transmission */
3260 ret
= close(new_sock
);
3270 ERR("Health error occurred in %s", __func__
);
3272 DBG("Health check thread dying");
3273 unlink(health_unix_sock_path
);
3280 if (new_sock
>= 0) {
3281 ret
= close(new_sock
);
3287 lttng_poll_clean(&events
);
3289 rcu_unregister_thread();
3294 * This thread manage all clients request using the unix client socket for
3297 static void *thread_manage_clients(void *data
)
3299 int sock
= -1, ret
, i
, pollfd
, err
= -1;
3301 uint32_t revents
, nb_fd
;
3302 struct command_ctx
*cmd_ctx
= NULL
;
3303 struct lttng_poll_event events
;
3305 DBG("[thread] Manage client started");
3307 rcu_register_thread();
3309 health_register(HEALTH_TYPE_CMD
);
3311 if (testpoint(thread_manage_clients
)) {
3312 goto error_testpoint
;
3315 health_code_update();
3317 ret
= lttcomm_listen_unix_sock(client_sock
);
3323 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3324 * more will be added to this poll set.
3326 ret
= sessiond_set_thread_pollset(&events
, 2);
3328 goto error_create_poll
;
3331 /* Add the application registration socket */
3332 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
3338 * Notify parent pid that we are ready to accept command for client side.
3340 if (opt_sig_parent
) {
3341 kill(ppid
, SIGUSR1
);
3344 if (testpoint(thread_manage_clients_before_loop
)) {
3348 health_code_update();
3351 DBG("Accepting client command ...");
3353 /* Inifinite blocking call, waiting for transmission */
3355 health_poll_entry();
3356 ret
= lttng_poll_wait(&events
, -1);
3360 * Restart interrupted system call.
3362 if (errno
== EINTR
) {
3370 for (i
= 0; i
< nb_fd
; i
++) {
3371 /* Fetch once the poll data */
3372 revents
= LTTNG_POLL_GETEV(&events
, i
);
3373 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3375 health_code_update();
3377 /* Thread quit pipe has been closed. Killing thread. */
3378 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3384 /* Event on the registration socket */
3385 if (pollfd
== client_sock
) {
3386 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3387 ERR("Client socket poll error");
3393 DBG("Wait for client response");
3395 health_code_update();
3397 sock
= lttcomm_accept_unix_sock(client_sock
);
3403 * Set the CLOEXEC flag. Return code is useless because either way, the
3406 (void) utils_set_fd_cloexec(sock
);
3408 /* Set socket option for credentials retrieval */
3409 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
3414 /* Allocate context command to process the client request */
3415 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
3416 if (cmd_ctx
== NULL
) {
3417 PERROR("zmalloc cmd_ctx");
3421 /* Allocate data buffer for reception */
3422 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
3423 if (cmd_ctx
->lsm
== NULL
) {
3424 PERROR("zmalloc cmd_ctx->lsm");
3428 cmd_ctx
->llm
= NULL
;
3429 cmd_ctx
->session
= NULL
;
3431 health_code_update();
3434 * Data is received from the lttng client. The struct
3435 * lttcomm_session_msg (lsm) contains the command and data request of
3438 DBG("Receiving data from client ...");
3439 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
3440 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
3442 DBG("Nothing recv() from client... continuing");
3448 clean_command_ctx(&cmd_ctx
);
3452 health_code_update();
3454 // TODO: Validate cmd_ctx including sanity check for
3455 // security purpose.
3457 rcu_thread_online();
3459 * This function dispatch the work to the kernel or userspace tracer
3460 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3461 * informations for the client. The command context struct contains
3462 * everything this function may needs.
3464 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
3465 rcu_thread_offline();
3473 * TODO: Inform client somehow of the fatal error. At
3474 * this point, ret < 0 means that a zmalloc failed
3475 * (ENOMEM). Error detected but still accept
3476 * command, unless a socket error has been
3479 clean_command_ctx(&cmd_ctx
);
3483 health_code_update();
3485 DBG("Sending response (size: %d, retcode: %s)",
3486 cmd_ctx
->lttng_msg_size
,
3487 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
3488 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
3490 ERR("Failed to send data back to client");
3493 /* End of transmission */
3500 clean_command_ctx(&cmd_ctx
);
3502 health_code_update();
3514 lttng_poll_clean(&events
);
3515 clean_command_ctx(&cmd_ctx
);
3520 unlink(client_unix_sock_path
);
3521 if (client_sock
>= 0) {
3522 ret
= close(client_sock
);
3530 ERR("Health error occurred in %s", __func__
);
3533 health_unregister();
3535 DBG("Client thread dying");
3537 rcu_unregister_thread();
3543 * usage function on stderr
3545 static void usage(void)
3547 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
3548 fprintf(stderr
, " -h, --help Display this usage.\n");
3549 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
3550 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
3551 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
3552 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
3553 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
3554 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
3555 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
3556 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
3557 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
3558 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
3559 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
3560 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
3561 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
3562 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
3563 fprintf(stderr
, " -V, --version Show version number.\n");
3564 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
3565 fprintf(stderr
, " -q, --quiet No output at all.\n");
3566 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3567 fprintf(stderr
, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
3568 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
3569 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
3573 * daemon argument parsing
3575 static int parse_args(int argc
, char **argv
)
3579 static struct option long_options
[] = {
3580 { "client-sock", 1, 0, 'c' },
3581 { "apps-sock", 1, 0, 'a' },
3582 { "kconsumerd-cmd-sock", 1, 0, 'C' },
3583 { "kconsumerd-err-sock", 1, 0, 'E' },
3584 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
3585 { "ustconsumerd32-err-sock", 1, 0, 'H' },
3586 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
3587 { "ustconsumerd64-err-sock", 1, 0, 'F' },
3588 { "consumerd32-path", 1, 0, 'u' },
3589 { "consumerd32-libdir", 1, 0, 'U' },
3590 { "consumerd64-path", 1, 0, 't' },
3591 { "consumerd64-libdir", 1, 0, 'T' },
3592 { "daemonize", 0, 0, 'd' },
3593 { "sig-parent", 0, 0, 'S' },
3594 { "help", 0, 0, 'h' },
3595 { "group", 1, 0, 'g' },
3596 { "version", 0, 0, 'V' },
3597 { "quiet", 0, 0, 'q' },
3598 { "verbose", 0, 0, 'v' },
3599 { "verbose-consumer", 0, 0, 'Z' },
3600 { "no-kernel", 0, 0, 'N' },
3601 { "pidfile", 1, 0, 'p' },
3606 int option_index
= 0;
3607 c
= getopt_long(argc
, argv
, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t:p:",
3608 long_options
, &option_index
);
3615 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
3617 fprintf(stderr
, " with arg %s\n", optarg
);
3621 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3624 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3630 opt_tracing_group
= optarg
;
3636 fprintf(stdout
, "%s\n", VERSION
);
3642 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3645 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3648 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3651 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3654 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3657 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3663 lttng_opt_quiet
= 1;
3666 /* Verbose level can increase using multiple -v */
3667 lttng_opt_verbose
+= 1;
3670 opt_verbose_consumer
+= 1;
3673 consumerd32_bin
= optarg
;
3676 consumerd32_libdir
= optarg
;
3679 consumerd64_bin
= optarg
;
3682 consumerd64_libdir
= optarg
;
3685 opt_pidfile
= optarg
;
3688 /* Unknown option or other error.
3689 * Error is printed by getopt, just return */
3698 * Creates the two needed socket by the daemon.
3699 * apps_sock - The communication socket for all UST apps.
3700 * client_sock - The communication of the cli tool (lttng).
3702 static int init_daemon_socket(void)
3707 old_umask
= umask(0);
3709 /* Create client tool unix socket */
3710 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
3711 if (client_sock
< 0) {
3712 ERR("Create unix sock failed: %s", client_unix_sock_path
);
3717 /* Set the cloexec flag */
3718 ret
= utils_set_fd_cloexec(client_sock
);
3720 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
3721 "Continuing but note that the consumer daemon will have a "
3722 "reference to this socket on exec()", client_sock
);
3725 /* File permission MUST be 660 */
3726 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3728 ERR("Set file permissions failed: %s", client_unix_sock_path
);
3733 /* Create the application unix socket */
3734 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
3735 if (apps_sock
< 0) {
3736 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
3741 /* Set the cloexec flag */
3742 ret
= utils_set_fd_cloexec(apps_sock
);
3744 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
3745 "Continuing but note that the consumer daemon will have a "
3746 "reference to this socket on exec()", apps_sock
);
3749 /* File permission MUST be 666 */
3750 ret
= chmod(apps_unix_sock_path
,
3751 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
3753 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
3758 DBG3("Session daemon client socket %d and application socket %d created",
3759 client_sock
, apps_sock
);
3767 * Check if the global socket is available, and if a daemon is answering at the
3768 * other side. If yes, error is returned.
3770 static int check_existing_daemon(void)
3772 /* Is there anybody out there ? */
3773 if (lttng_session_daemon_alive()) {
3781 * Set the tracing group gid onto the client socket.
3783 * Race window between mkdir and chown is OK because we are going from more
3784 * permissive (root.root) to less permissive (root.tracing).
3786 static int set_permissions(char *rundir
)
3791 ret
= allowed_group();
3793 WARN("No tracing group detected");
3800 /* Set lttng run dir */
3801 ret
= chown(rundir
, 0, gid
);
3803 ERR("Unable to set group on %s", rundir
);
3807 /* Ensure tracing group can search the run dir */
3808 ret
= chmod(rundir
, S_IRWXU
| S_IXGRP
| S_IXOTH
);
3810 ERR("Unable to set permissions on %s", rundir
);
3814 /* lttng client socket path */
3815 ret
= chown(client_unix_sock_path
, 0, gid
);
3817 ERR("Unable to set group on %s", client_unix_sock_path
);
3821 /* kconsumer error socket path */
3822 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, gid
);
3824 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
3828 /* 64-bit ustconsumer error socket path */
3829 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, gid
);
3831 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
3835 /* 32-bit ustconsumer compat32 error socket path */
3836 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, gid
);
3838 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
3842 DBG("All permissions are set");
3849 * Create the lttng run directory needed for all global sockets and pipe.
3851 static int create_lttng_rundir(const char *rundir
)
3855 DBG3("Creating LTTng run directory: %s", rundir
);
3857 ret
= mkdir(rundir
, S_IRWXU
);
3859 if (errno
!= EEXIST
) {
3860 ERR("Unable to create %s", rundir
);
3872 * Setup sockets and directory needed by the kconsumerd communication with the
3875 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
3879 char path
[PATH_MAX
];
3881 switch (consumer_data
->type
) {
3882 case LTTNG_CONSUMER_KERNEL
:
3883 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
3885 case LTTNG_CONSUMER64_UST
:
3886 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
3888 case LTTNG_CONSUMER32_UST
:
3889 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
3892 ERR("Consumer type unknown");
3897 DBG2("Creating consumer directory: %s", path
);
3899 ret
= mkdir(path
, S_IRWXU
);
3901 if (errno
!= EEXIST
) {
3903 ERR("Failed to create %s", path
);
3909 /* Create the kconsumerd error unix socket */
3910 consumer_data
->err_sock
=
3911 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
3912 if (consumer_data
->err_sock
< 0) {
3913 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
3918 /* File permission MUST be 660 */
3919 ret
= chmod(consumer_data
->err_unix_sock_path
,
3920 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3922 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
3932 * Signal handler for the daemon
3934 * Simply stop all worker threads, leaving main() return gracefully after
3935 * joining all threads and calling cleanup().
3937 static void sighandler(int sig
)
3941 DBG("SIGPIPE caught");
3944 DBG("SIGINT caught");
3948 DBG("SIGTERM caught");
3957 * Setup signal handler for :
3958 * SIGINT, SIGTERM, SIGPIPE
3960 static int set_signal_handler(void)
3963 struct sigaction sa
;
3966 if ((ret
= sigemptyset(&sigset
)) < 0) {
3967 PERROR("sigemptyset");
3971 sa
.sa_handler
= sighandler
;
3972 sa
.sa_mask
= sigset
;
3974 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3975 PERROR("sigaction");
3979 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3980 PERROR("sigaction");
3984 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3985 PERROR("sigaction");
3989 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3995 * Set open files limit to unlimited. This daemon can open a large number of
3996 * file descriptors in order to consumer multiple kernel traces.
3998 static void set_ulimit(void)
4003 /* The kernel does not allowed an infinite limit for open files */
4004 lim
.rlim_cur
= 65535;
4005 lim
.rlim_max
= 65535;
4007 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
4009 PERROR("failed to set open files limit");
4014 * Write pidfile using the rundir and opt_pidfile.
4016 static void write_pidfile(void)
4019 char pidfile_path
[PATH_MAX
];
4024 strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
));
4026 /* Build pidfile path from rundir and opt_pidfile. */
4027 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
4028 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
4030 PERROR("snprintf pidfile path");
4036 * Create pid file in rundir. Return value is of no importance. The
4037 * execution will continue even though we are not able to write the file.
4039 (void) utils_create_pid_file(getpid(), pidfile_path
);
4048 int main(int argc
, char **argv
)
4052 const char *home_path
, *env_app_timeout
;
4054 init_kernel_workarounds();
4056 rcu_register_thread();
4058 setup_consumerd_path();
4060 page_size
= sysconf(_SC_PAGESIZE
);
4061 if (page_size
< 0) {
4062 PERROR("sysconf _SC_PAGESIZE");
4063 page_size
= LONG_MAX
;
4064 WARN("Fallback page size to %ld", page_size
);
4067 /* Parse arguments */
4069 if ((ret
= parse_args(argc
, argv
)) < 0) {
4079 * child: setsid, close FD 0, 1, 2, chdir /
4080 * parent: exit (if fork is successful)
4088 * We are in the child. Make sure all other file
4089 * descriptors are closed, in case we are called with
4090 * more opened file descriptors than the standard ones.
4092 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
4097 /* Create thread quit pipe */
4098 if ((ret
= init_thread_quit_pipe()) < 0) {
4102 /* Check if daemon is UID = 0 */
4103 is_root
= !getuid();
4106 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
4108 /* Create global run dir with root access */
4109 ret
= create_lttng_rundir(rundir
);
4114 if (strlen(apps_unix_sock_path
) == 0) {
4115 snprintf(apps_unix_sock_path
, PATH_MAX
,
4116 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
4119 if (strlen(client_unix_sock_path
) == 0) {
4120 snprintf(client_unix_sock_path
, PATH_MAX
,
4121 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
4124 /* Set global SHM for ust */
4125 if (strlen(wait_shm_path
) == 0) {
4126 snprintf(wait_shm_path
, PATH_MAX
,
4127 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
4130 if (strlen(health_unix_sock_path
) == 0) {
4131 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4132 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
4135 /* Setup kernel consumerd path */
4136 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
4137 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
4138 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
4139 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
4141 DBG2("Kernel consumer err path: %s",
4142 kconsumer_data
.err_unix_sock_path
);
4143 DBG2("Kernel consumer cmd path: %s",
4144 kconsumer_data
.cmd_unix_sock_path
);
4146 home_path
= get_home_dir();
4147 if (home_path
== NULL
) {
4148 /* TODO: Add --socket PATH option */
4149 ERR("Can't get HOME directory for sockets creation.");
4155 * Create rundir from home path. This will create something like
4158 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
4164 ret
= create_lttng_rundir(rundir
);
4169 if (strlen(apps_unix_sock_path
) == 0) {
4170 snprintf(apps_unix_sock_path
, PATH_MAX
,
4171 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
4174 /* Set the cli tool unix socket path */
4175 if (strlen(client_unix_sock_path
) == 0) {
4176 snprintf(client_unix_sock_path
, PATH_MAX
,
4177 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
4180 /* Set global SHM for ust */
4181 if (strlen(wait_shm_path
) == 0) {
4182 snprintf(wait_shm_path
, PATH_MAX
,
4183 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
4186 /* Set health check Unix path */
4187 if (strlen(health_unix_sock_path
) == 0) {
4188 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4189 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
4193 /* Set consumer initial state */
4194 kernel_consumerd_state
= CONSUMER_STOPPED
;
4195 ust_consumerd_state
= CONSUMER_STOPPED
;
4197 DBG("Client socket path %s", client_unix_sock_path
);
4198 DBG("Application socket path %s", apps_unix_sock_path
);
4199 DBG("Application wait path %s", wait_shm_path
);
4200 DBG("LTTng run directory path: %s", rundir
);
4202 /* 32 bits consumerd path setup */
4203 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
4204 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
4205 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
4206 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
4208 DBG2("UST consumer 32 bits err path: %s",
4209 ustconsumer32_data
.err_unix_sock_path
);
4210 DBG2("UST consumer 32 bits cmd path: %s",
4211 ustconsumer32_data
.cmd_unix_sock_path
);
4213 /* 64 bits consumerd path setup */
4214 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
4215 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
4216 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
4217 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
4219 DBG2("UST consumer 64 bits err path: %s",
4220 ustconsumer64_data
.err_unix_sock_path
);
4221 DBG2("UST consumer 64 bits cmd path: %s",
4222 ustconsumer64_data
.cmd_unix_sock_path
);
4225 * See if daemon already exist.
4227 if ((ret
= check_existing_daemon()) < 0) {
4228 ERR("Already running daemon.\n");
4230 * We do not goto exit because we must not cleanup()
4231 * because a daemon is already running.
4237 * Init UST app hash table. Alloc hash table before this point since
4238 * cleanup() can get called after that point.
4242 /* After this point, we can safely call cleanup() with "goto exit" */
4245 * These actions must be executed as root. We do that *after* setting up
4246 * the sockets path because we MUST make the check for another daemon using
4247 * those paths *before* trying to set the kernel consumer sockets and init
4251 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
4256 /* Setup kernel tracer */
4257 if (!opt_no_kernel
) {
4258 init_kernel_tracer();
4261 /* Set ulimit for open files */
4264 /* init lttng_fd tracking must be done after set_ulimit. */
4267 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
4272 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
4277 if ((ret
= set_signal_handler()) < 0) {
4281 /* Setup the needed unix socket */
4282 if ((ret
= init_daemon_socket()) < 0) {
4286 /* Set credentials to socket */
4287 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
4291 /* Get parent pid if -S, --sig-parent is specified. */
4292 if (opt_sig_parent
) {
4296 /* Setup the kernel pipe for waking up the kernel thread */
4297 if (is_root
&& !opt_no_kernel
) {
4298 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
4303 /* Setup the thread apps communication pipe. */
4304 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
4308 /* Setup the thread apps notify communication pipe. */
4309 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
) < 0) {
4313 /* Initialize global buffer per UID and PID registry. */
4314 buffer_reg_init_uid_registry();
4315 buffer_reg_init_pid_registry();
4317 /* Init UST command queue. */
4318 cds_wfq_init(&ust_cmd_queue
.queue
);
4321 * Get session list pointer. This pointer MUST NOT be free(). This list is
4322 * statically declared in session.c
4324 session_list_ptr
= session_get_list();
4326 /* Set up max poll set size */
4327 lttng_poll_set_max_size();
4331 /* Check for the application socket timeout env variable. */
4332 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
4333 if (env_app_timeout
) {
4334 app_socket_timeout
= atoi(env_app_timeout
);
4336 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
4341 /* Create thread to manage the client socket */
4342 ret
= pthread_create(&health_thread
, NULL
,
4343 thread_manage_health
, (void *) NULL
);
4345 PERROR("pthread_create health");
4349 /* Create thread to manage the client socket */
4350 ret
= pthread_create(&client_thread
, NULL
,
4351 thread_manage_clients
, (void *) NULL
);
4353 PERROR("pthread_create clients");
4357 /* Create thread to dispatch registration */
4358 ret
= pthread_create(&dispatch_thread
, NULL
,
4359 thread_dispatch_ust_registration
, (void *) NULL
);
4361 PERROR("pthread_create dispatch");
4365 /* Create thread to manage application registration. */
4366 ret
= pthread_create(®_apps_thread
, NULL
,
4367 thread_registration_apps
, (void *) NULL
);
4369 PERROR("pthread_create registration");
4373 /* Create thread to manage application socket */
4374 ret
= pthread_create(&apps_thread
, NULL
,
4375 thread_manage_apps
, (void *) NULL
);
4377 PERROR("pthread_create apps");
4381 /* Create thread to manage application notify socket */
4382 ret
= pthread_create(&apps_notify_thread
, NULL
,
4383 ust_thread_manage_notify
, (void *) NULL
);
4385 PERROR("pthread_create apps");
4389 /* Don't start this thread if kernel tracing is not requested nor root */
4390 if (is_root
&& !opt_no_kernel
) {
4391 /* Create kernel thread to manage kernel event */
4392 ret
= pthread_create(&kernel_thread
, NULL
,
4393 thread_manage_kernel
, (void *) NULL
);
4395 PERROR("pthread_create kernel");
4399 ret
= pthread_join(kernel_thread
, &status
);
4401 PERROR("pthread_join");
4402 goto error
; /* join error, exit without cleanup */
4407 ret
= pthread_join(apps_thread
, &status
);
4409 PERROR("pthread_join");
4410 goto error
; /* join error, exit without cleanup */
4414 ret
= pthread_join(reg_apps_thread
, &status
);
4416 PERROR("pthread_join");
4417 goto error
; /* join error, exit without cleanup */
4421 ret
= pthread_join(dispatch_thread
, &status
);
4423 PERROR("pthread_join");
4424 goto error
; /* join error, exit without cleanup */
4428 ret
= pthread_join(client_thread
, &status
);
4430 PERROR("pthread_join");
4431 goto error
; /* join error, exit without cleanup */
4434 ret
= join_consumer_thread(&kconsumer_data
);
4436 PERROR("join_consumer");
4437 goto error
; /* join error, exit without cleanup */
4440 ret
= join_consumer_thread(&ustconsumer32_data
);
4442 PERROR("join_consumer ust32");
4443 goto error
; /* join error, exit without cleanup */
4446 ret
= join_consumer_thread(&ustconsumer64_data
);
4448 PERROR("join_consumer ust64");
4449 goto error
; /* join error, exit without cleanup */
4453 ret
= pthread_join(health_thread
, &status
);
4455 PERROR("pthread_join health thread");
4456 goto error
; /* join error, exit without cleanup */
4462 * cleanup() is called when no other thread is running.
4464 rcu_thread_online();
4466 rcu_thread_offline();
4467 rcu_unregister_thread();