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
;
236 void setup_consumerd_path(void)
238 const char *bin
, *libdir
;
241 * Allow INSTALL_BIN_PATH to be used as a target path for the
242 * native architecture size consumer if CONFIG_CONSUMER*_PATH
243 * has not been defined.
245 #if (CAA_BITS_PER_LONG == 32)
246 if (!consumerd32_bin
[0]) {
247 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
249 if (!consumerd32_libdir
[0]) {
250 consumerd32_libdir
= INSTALL_LIB_PATH
;
252 #elif (CAA_BITS_PER_LONG == 64)
253 if (!consumerd64_bin
[0]) {
254 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
256 if (!consumerd64_libdir
[0]) {
257 consumerd64_libdir
= INSTALL_LIB_PATH
;
260 #error "Unknown bitness"
264 * runtime env. var. overrides the build default.
266 bin
= getenv("LTTNG_CONSUMERD32_BIN");
268 consumerd32_bin
= bin
;
270 bin
= getenv("LTTNG_CONSUMERD64_BIN");
272 consumerd64_bin
= bin
;
274 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
276 consumerd32_libdir
= libdir
;
278 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
280 consumerd64_libdir
= libdir
;
285 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
287 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
293 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
299 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
311 * Check if the thread quit pipe was triggered.
313 * Return 1 if it was triggered else 0;
315 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
317 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
325 * Return group ID of the tracing group or -1 if not found.
327 static gid_t
allowed_group(void)
331 if (opt_tracing_group
) {
332 grp
= getgrnam(opt_tracing_group
);
334 grp
= getgrnam(default_tracing_group
);
344 * Init thread quit pipe.
346 * Return -1 on error or 0 if all pipes are created.
348 static int init_thread_quit_pipe(void)
352 ret
= pipe(thread_quit_pipe
);
354 PERROR("thread quit pipe");
358 for (i
= 0; i
< 2; i
++) {
359 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
371 * Stop all threads by closing the thread quit pipe.
373 static void stop_threads(void)
377 /* Stopping all threads */
378 DBG("Terminating all threads");
379 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
381 ERR("write error on thread quit pipe");
384 /* Dispatch thread */
385 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
386 futex_nto1_wake(&ust_cmd_queue
.futex
);
392 static void cleanup(void)
396 struct ltt_session
*sess
, *stmp
;
400 /* First thing first, stop all threads */
401 utils_close_pipe(thread_quit_pipe
);
404 * If opt_pidfile is undefined, the default file will be wiped when
405 * removing the rundir.
408 ret
= remove(opt_pidfile
);
410 PERROR("remove pidfile %s", opt_pidfile
);
414 DBG("Removing %s directory", rundir
);
415 ret
= asprintf(&cmd
, "rm -rf %s", rundir
);
417 ERR("asprintf failed. Something is really wrong!");
420 /* Remove lttng run directory */
423 ERR("Unable to clean %s", rundir
);
428 DBG("Cleaning up all sessions");
430 /* Destroy session list mutex */
431 if (session_list_ptr
!= NULL
) {
432 pthread_mutex_destroy(&session_list_ptr
->lock
);
434 /* Cleanup ALL session */
435 cds_list_for_each_entry_safe(sess
, stmp
,
436 &session_list_ptr
->head
, list
) {
437 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
441 DBG("Closing all UST sockets");
442 ust_app_clean_list();
443 buffer_reg_destroy_registries();
445 if (is_root
&& !opt_no_kernel
) {
446 DBG2("Closing kernel fd");
447 if (kernel_tracer_fd
>= 0) {
448 ret
= close(kernel_tracer_fd
);
453 DBG("Unloading kernel modules");
454 modprobe_remove_lttng_all();
458 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
459 "Matthew, BEET driven development works!%c[%dm",
460 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
465 * Send data on a unix socket using the liblttsessiondcomm API.
467 * Return lttcomm error code.
469 static int send_unix_sock(int sock
, void *buf
, size_t len
)
471 /* Check valid length */
476 return lttcomm_send_unix_sock(sock
, buf
, len
);
480 * Free memory of a command context structure.
482 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
484 DBG("Clean command context structure");
486 if ((*cmd_ctx
)->llm
) {
487 free((*cmd_ctx
)->llm
);
489 if ((*cmd_ctx
)->lsm
) {
490 free((*cmd_ctx
)->lsm
);
498 * Notify UST applications using the shm mmap futex.
500 static int notify_ust_apps(int active
)
504 DBG("Notifying applications of session daemon state: %d", active
);
506 /* See shm.c for this call implying mmap, shm and futex calls */
507 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
508 if (wait_shm_mmap
== NULL
) {
512 /* Wake waiting process */
513 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
515 /* Apps notified successfully */
523 * Setup the outgoing data buffer for the response (llm) by allocating the
524 * right amount of memory and copying the original information from the lsm
527 * Return total size of the buffer pointed by buf.
529 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
535 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
536 if (cmd_ctx
->llm
== NULL
) {
542 /* Copy common data */
543 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
544 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
546 cmd_ctx
->llm
->data_size
= size
;
547 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
556 * Update the kernel poll set of all channel fd available over all tracing
557 * session. Add the wakeup pipe at the end of the set.
559 static int update_kernel_poll(struct lttng_poll_event
*events
)
562 struct ltt_session
*session
;
563 struct ltt_kernel_channel
*channel
;
565 DBG("Updating kernel poll set");
568 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
569 session_lock(session
);
570 if (session
->kernel_session
== NULL
) {
571 session_unlock(session
);
575 cds_list_for_each_entry(channel
,
576 &session
->kernel_session
->channel_list
.head
, list
) {
577 /* Add channel fd to the kernel poll set */
578 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
580 session_unlock(session
);
583 DBG("Channel fd %d added to kernel set", channel
->fd
);
585 session_unlock(session
);
587 session_unlock_list();
592 session_unlock_list();
597 * Find the channel fd from 'fd' over all tracing session. When found, check
598 * for new channel stream and send those stream fds to the kernel consumer.
600 * Useful for CPU hotplug feature.
602 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
605 struct ltt_session
*session
;
606 struct ltt_kernel_session
*ksess
;
607 struct ltt_kernel_channel
*channel
;
609 DBG("Updating kernel streams for channel fd %d", fd
);
612 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
613 session_lock(session
);
614 if (session
->kernel_session
== NULL
) {
615 session_unlock(session
);
618 ksess
= session
->kernel_session
;
620 cds_list_for_each_entry(channel
, &ksess
->channel_list
.head
, list
) {
621 if (channel
->fd
== fd
) {
622 DBG("Channel found, updating kernel streams");
623 ret
= kernel_open_channel_stream(channel
);
629 * Have we already sent fds to the consumer? If yes, it means
630 * that tracing is started so it is safe to send our updated
633 if (ksess
->consumer_fds_sent
== 1 && ksess
->consumer
!= NULL
) {
634 struct lttng_ht_iter iter
;
635 struct consumer_socket
*socket
;
638 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
639 &iter
.iter
, socket
, node
.node
) {
640 /* Code flow error */
641 assert(socket
->fd
>= 0);
643 pthread_mutex_lock(socket
->lock
);
644 ret
= kernel_consumer_send_channel_stream(socket
,
646 pthread_mutex_unlock(socket
->lock
);
657 session_unlock(session
);
659 session_unlock_list();
663 session_unlock(session
);
664 session_unlock_list();
669 * For each tracing session, update newly registered apps. The session list
670 * lock MUST be acquired before calling this.
672 static void update_ust_app(int app_sock
)
674 struct ltt_session
*sess
, *stmp
;
676 /* For all tracing session(s) */
677 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
679 if (sess
->ust_session
) {
680 ust_app_global_update(sess
->ust_session
, app_sock
);
682 session_unlock(sess
);
687 * This thread manage event coming from the kernel.
689 * Features supported in this thread:
692 static void *thread_manage_kernel(void *data
)
694 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
695 uint32_t revents
, nb_fd
;
697 struct lttng_poll_event events
;
699 DBG("[thread] Thread manage kernel started");
701 health_register(HEALTH_TYPE_KERNEL
);
704 * This first step of the while is to clean this structure which could free
705 * non NULL pointers so zero it before the loop.
707 memset(&events
, 0, sizeof(events
));
709 if (testpoint(thread_manage_kernel
)) {
710 goto error_testpoint
;
713 health_code_update();
715 if (testpoint(thread_manage_kernel_before_loop
)) {
716 goto error_testpoint
;
720 health_code_update();
722 if (update_poll_flag
== 1) {
723 /* Clean events object. We are about to populate it again. */
724 lttng_poll_clean(&events
);
726 ret
= sessiond_set_thread_pollset(&events
, 2);
728 goto error_poll_create
;
731 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
736 /* This will add the available kernel channel if any. */
737 ret
= update_kernel_poll(&events
);
741 update_poll_flag
= 0;
744 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events
));
746 /* Poll infinite value of time */
749 ret
= lttng_poll_wait(&events
, -1);
753 * Restart interrupted system call.
755 if (errno
== EINTR
) {
759 } else if (ret
== 0) {
760 /* Should not happen since timeout is infinite */
761 ERR("Return value of poll is 0 with an infinite timeout.\n"
762 "This should not have happened! Continuing...");
768 for (i
= 0; i
< nb_fd
; i
++) {
769 /* Fetch once the poll data */
770 revents
= LTTNG_POLL_GETEV(&events
, i
);
771 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
773 health_code_update();
775 /* Thread quit pipe has been closed. Killing thread. */
776 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
782 /* Check for data on kernel pipe */
783 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
785 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
786 } while (ret
< 0 && errno
== EINTR
);
788 * Ret value is useless here, if this pipe gets any actions an
789 * update is required anyway.
791 update_poll_flag
= 1;
795 * New CPU detected by the kernel. Adding kernel stream to
796 * kernel session and updating the kernel consumer
798 if (revents
& LPOLLIN
) {
799 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
805 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
806 * and unregister kernel stream at this point.
815 lttng_poll_clean(&events
);
818 utils_close_pipe(kernel_poll_pipe
);
819 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
822 ERR("Health error occurred in %s", __func__
);
823 WARN("Kernel thread died unexpectedly. "
824 "Kernel tracing can continue but CPU hotplug is disabled.");
827 DBG("Kernel thread dying");
832 * Signal pthread condition of the consumer data that the thread.
834 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
836 pthread_mutex_lock(&data
->cond_mutex
);
839 * The state is set before signaling. It can be any value, it's the waiter
840 * job to correctly interpret this condition variable associated to the
841 * consumer pthread_cond.
843 * A value of 0 means that the corresponding thread of the consumer data
844 * was not started. 1 indicates that the thread has started and is ready
845 * for action. A negative value means that there was an error during the
848 data
->consumer_thread_is_ready
= state
;
849 (void) pthread_cond_signal(&data
->cond
);
851 pthread_mutex_unlock(&data
->cond_mutex
);
855 * This thread manage the consumer error sent back to the session daemon.
857 static void *thread_manage_consumer(void *data
)
859 int sock
= -1, i
, ret
, pollfd
, err
= -1;
860 uint32_t revents
, nb_fd
;
861 enum lttcomm_return_code code
;
862 struct lttng_poll_event events
;
863 struct consumer_data
*consumer_data
= data
;
865 DBG("[thread] Manage consumer started");
867 health_register(HEALTH_TYPE_CONSUMER
);
869 health_code_update();
872 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
873 * metadata_sock. Nothing more will be added to this poll set.
875 ret
= sessiond_set_thread_pollset(&events
, 3);
881 * The error socket here is already in a listening state which was done
882 * just before spawning this thread to avoid a race between the consumer
883 * daemon exec trying to connect and the listen() call.
885 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
890 health_code_update();
892 /* Infinite blocking call, waiting for transmission */
896 if (testpoint(thread_manage_consumer
)) {
900 ret
= lttng_poll_wait(&events
, -1);
904 * Restart interrupted system call.
906 if (errno
== EINTR
) {
914 for (i
= 0; i
< nb_fd
; i
++) {
915 /* Fetch once the poll data */
916 revents
= LTTNG_POLL_GETEV(&events
, i
);
917 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
919 health_code_update();
921 /* Thread quit pipe has been closed. Killing thread. */
922 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
928 /* Event on the registration socket */
929 if (pollfd
== consumer_data
->err_sock
) {
930 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
931 ERR("consumer err socket poll error");
937 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
943 * Set the CLOEXEC flag. Return code is useless because either way, the
946 (void) utils_set_fd_cloexec(sock
);
948 health_code_update();
950 DBG2("Receiving code from consumer err_sock");
952 /* Getting status code from kconsumerd */
953 ret
= lttcomm_recv_unix_sock(sock
, &code
,
954 sizeof(enum lttcomm_return_code
));
959 health_code_update();
961 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
962 /* Connect both socket, command and metadata. */
963 consumer_data
->cmd_sock
=
964 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
965 consumer_data
->metadata_sock
.fd
=
966 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
967 if (consumer_data
->cmd_sock
< 0 ||
968 consumer_data
->metadata_sock
.fd
< 0) {
969 PERROR("consumer connect cmd socket");
970 /* On error, signal condition and quit. */
971 signal_consumer_condition(consumer_data
, -1);
974 /* Create metadata socket lock. */
975 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
976 if (consumer_data
->metadata_sock
.lock
== NULL
) {
977 PERROR("zmalloc pthread mutex");
981 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
983 signal_consumer_condition(consumer_data
, 1);
984 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
985 DBG("Consumer metadata socket ready (fd: %d)",
986 consumer_data
->metadata_sock
.fd
);
988 ERR("consumer error when waiting for SOCK_READY : %s",
989 lttcomm_get_readable_code(-code
));
993 /* Remove the consumerd error sock since we've established a connexion */
994 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
999 /* Add new accepted error socket. */
1000 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1005 /* Add metadata socket that is successfully connected. */
1006 ret
= lttng_poll_add(&events
, consumer_data
->metadata_sock
.fd
,
1007 LPOLLIN
| LPOLLRDHUP
);
1012 health_code_update();
1014 /* Infinite blocking call, waiting for transmission */
1017 health_poll_entry();
1018 ret
= lttng_poll_wait(&events
, -1);
1022 * Restart interrupted system call.
1024 if (errno
== EINTR
) {
1032 for (i
= 0; i
< nb_fd
; i
++) {
1033 /* Fetch once the poll data */
1034 revents
= LTTNG_POLL_GETEV(&events
, i
);
1035 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1037 health_code_update();
1039 /* Thread quit pipe has been closed. Killing thread. */
1040 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1046 if (pollfd
== sock
) {
1047 /* Event on the consumerd socket */
1048 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1049 ERR("consumer err socket second poll error");
1052 health_code_update();
1053 /* Wait for any kconsumerd error */
1054 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1055 sizeof(enum lttcomm_return_code
));
1057 ERR("consumer closed the command socket");
1061 ERR("consumer return code : %s",
1062 lttcomm_get_readable_code(-code
));
1065 } else if (pollfd
== consumer_data
->metadata_sock
.fd
) {
1066 /* UST metadata requests */
1067 ret
= ust_consumer_metadata_request(
1068 &consumer_data
->metadata_sock
);
1070 ERR("Handling metadata request");
1075 ERR("Unknown pollfd");
1079 health_code_update();
1084 /* Immediately set the consumerd state to stopped */
1085 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1086 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1087 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1088 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1089 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1091 /* Code flow error... */
1095 if (consumer_data
->err_sock
>= 0) {
1096 ret
= close(consumer_data
->err_sock
);
1101 if (consumer_data
->cmd_sock
>= 0) {
1102 ret
= close(consumer_data
->cmd_sock
);
1107 if (consumer_data
->metadata_sock
.fd
>= 0) {
1108 ret
= close(consumer_data
->metadata_sock
.fd
);
1113 /* Cleanup metadata socket mutex. */
1114 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1115 free(consumer_data
->metadata_sock
.lock
);
1124 unlink(consumer_data
->err_unix_sock_path
);
1125 unlink(consumer_data
->cmd_unix_sock_path
);
1126 consumer_data
->pid
= 0;
1128 lttng_poll_clean(&events
);
1132 ERR("Health error occurred in %s", __func__
);
1134 health_unregister();
1135 DBG("consumer thread cleanup completed");
1141 * This thread manage application communication.
1143 static void *thread_manage_apps(void *data
)
1145 int i
, ret
, pollfd
, err
= -1;
1146 uint32_t revents
, nb_fd
;
1147 struct lttng_poll_event events
;
1149 DBG("[thread] Manage application started");
1151 rcu_register_thread();
1152 rcu_thread_online();
1154 health_register(HEALTH_TYPE_APP_MANAGE
);
1156 if (testpoint(thread_manage_apps
)) {
1157 goto error_testpoint
;
1160 health_code_update();
1162 ret
= sessiond_set_thread_pollset(&events
, 2);
1164 goto error_poll_create
;
1167 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1172 if (testpoint(thread_manage_apps_before_loop
)) {
1176 health_code_update();
1179 DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events
));
1181 /* Inifinite blocking call, waiting for transmission */
1183 health_poll_entry();
1184 ret
= lttng_poll_wait(&events
, -1);
1188 * Restart interrupted system call.
1190 if (errno
== EINTR
) {
1198 for (i
= 0; i
< nb_fd
; i
++) {
1199 /* Fetch once the poll data */
1200 revents
= LTTNG_POLL_GETEV(&events
, i
);
1201 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1203 health_code_update();
1205 /* Thread quit pipe has been closed. Killing thread. */
1206 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1212 /* Inspect the apps cmd pipe */
1213 if (pollfd
== apps_cmd_pipe
[0]) {
1214 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1215 ERR("Apps command pipe error");
1217 } else if (revents
& LPOLLIN
) {
1222 ret
= read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1223 } while (ret
< 0 && errno
== EINTR
);
1224 if (ret
< 0 || ret
< sizeof(sock
)) {
1225 PERROR("read apps cmd pipe");
1229 health_code_update();
1232 * We only monitor the error events of the socket. This
1233 * thread does not handle any incoming data from UST
1236 ret
= lttng_poll_add(&events
, sock
,
1237 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1242 /* Set socket timeout for both receiving and ending */
1243 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1244 app_socket_timeout
);
1245 (void) lttcomm_setsockopt_snd_timeout(sock
,
1246 app_socket_timeout
);
1248 DBG("Apps with sock %d added to poll set", sock
);
1250 health_code_update();
1256 * At this point, we know that a registered application made
1257 * the event at poll_wait.
1259 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1260 /* Removing from the poll set */
1261 ret
= lttng_poll_del(&events
, pollfd
);
1266 /* Socket closed on remote end. */
1267 ust_app_unregister(pollfd
);
1272 health_code_update();
1278 lttng_poll_clean(&events
);
1281 utils_close_pipe(apps_cmd_pipe
);
1282 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1285 * We don't clean the UST app hash table here since already registered
1286 * applications can still be controlled so let them be until the session
1287 * daemon dies or the applications stop.
1292 ERR("Health error occurred in %s", __func__
);
1294 health_unregister();
1295 DBG("Application communication apps thread cleanup complete");
1296 rcu_thread_offline();
1297 rcu_unregister_thread();
1302 * Send a socket to a thread This is called from the dispatch UST registration
1303 * thread once all sockets are set for the application.
1305 * On success, return 0 else a negative value being the errno message of the
1308 static int send_socket_to_thread(int fd
, int sock
)
1312 /* Sockets MUST be set or else this should not have been called. */
1317 ret
= write(fd
, &sock
, sizeof(sock
));
1318 } while (ret
< 0 && errno
== EINTR
);
1319 if (ret
< 0 || ret
!= sizeof(sock
)) {
1320 PERROR("write apps pipe %d", fd
);
1327 /* All good. Don't send back the write positive ret value. */
1334 * Dispatch request from the registration threads to the application
1335 * communication thread.
1337 static void *thread_dispatch_ust_registration(void *data
)
1340 struct cds_wfq_node
*node
;
1341 struct ust_command
*ust_cmd
= NULL
;
1343 struct ust_app
*app
;
1344 struct cds_list_head head
;
1345 } *wait_node
= NULL
, *tmp_wait_node
;
1347 CDS_LIST_HEAD(wait_queue
);
1349 DBG("[thread] Dispatch UST command started");
1351 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1352 /* Atomically prepare the queue futex */
1353 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1356 struct ust_app
*app
= NULL
;
1359 /* Dequeue command for registration */
1360 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1362 DBG("Woken up but nothing in the UST command queue");
1363 /* Continue thread execution */
1367 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1369 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1370 " gid:%d sock:%d name:%s (version %d.%d)",
1371 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1372 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1373 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1374 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1376 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1377 wait_node
= zmalloc(sizeof(*wait_node
));
1379 PERROR("zmalloc wait_node dispatch");
1383 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1385 /* Create application object if socket is CMD. */
1386 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1388 if (!wait_node
->app
) {
1389 ret
= close(ust_cmd
->sock
);
1391 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1393 lttng_fd_put(1, LTTNG_FD_APPS
);
1399 * Add application to the wait queue so we can set the notify
1400 * socket before putting this object in the global ht.
1402 cds_list_add(&wait_node
->head
, &wait_queue
);
1406 * We have to continue here since we don't have the notify
1407 * socket and the application MUST be added to the hash table
1408 * only at that moment.
1413 * Look for the application in the local wait queue and set the
1414 * notify socket if found.
1416 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1417 &wait_queue
, head
) {
1418 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1419 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1420 cds_list_del(&wait_node
->head
);
1421 app
= wait_node
->app
;
1423 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1432 * @session_lock_list
1434 * Lock the global session list so from the register up to the
1435 * registration done message, no thread can see the application
1436 * and change its state.
1438 session_lock_list();
1442 * Add application to the global hash table. This needs to be
1443 * done before the update to the UST registry can locate the
1448 /* Set app version. This call will print an error if needed. */
1449 (void) ust_app_version(app
);
1451 /* Send notify socket through the notify pipe. */
1452 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1456 session_unlock_list();
1457 /* No notify thread, stop the UST tracing. */
1462 * Update newly registered application with the tracing
1463 * registry info already enabled information.
1465 update_ust_app(app
->sock
);
1468 * Don't care about return value. Let the manage apps threads
1469 * handle app unregistration upon socket close.
1471 (void) ust_app_register_done(app
->sock
);
1474 * Even if the application socket has been closed, send the app
1475 * to the thread and unregistration will take place at that
1478 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1481 session_unlock_list();
1482 /* No apps. thread, stop the UST tracing. */
1487 session_unlock_list();
1489 /* Application manager threads are not available. */
1490 ret
= close(ust_cmd
->sock
);
1492 PERROR("close ust_cmd sock");
1494 lttng_fd_put(1, LTTNG_FD_APPS
);
1496 } while (node
!= NULL
);
1498 /* Futex wait on queue. Blocking call on futex() */
1499 futex_nto1_wait(&ust_cmd_queue
.futex
);
1503 /* Clean up wait queue. */
1504 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1505 &wait_queue
, head
) {
1506 cds_list_del(&wait_node
->head
);
1510 DBG("Dispatch thread dying");
1515 * This thread manage application registration.
1517 static void *thread_registration_apps(void *data
)
1519 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1520 uint32_t revents
, nb_fd
;
1521 struct lttng_poll_event events
;
1523 * Get allocated in this thread, enqueued to a global queue, dequeued and
1524 * freed in the manage apps thread.
1526 struct ust_command
*ust_cmd
= NULL
;
1528 DBG("[thread] Manage application registration started");
1530 health_register(HEALTH_TYPE_APP_REG
);
1532 if (testpoint(thread_registration_apps
)) {
1533 goto error_testpoint
;
1536 ret
= lttcomm_listen_unix_sock(apps_sock
);
1542 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1543 * more will be added to this poll set.
1545 ret
= sessiond_set_thread_pollset(&events
, 2);
1547 goto error_create_poll
;
1550 /* Add the application registration socket */
1551 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1553 goto error_poll_add
;
1556 /* Notify all applications to register */
1557 ret
= notify_ust_apps(1);
1559 ERR("Failed to notify applications or create the wait shared memory.\n"
1560 "Execution continues but there might be problem for already\n"
1561 "running applications that wishes to register.");
1565 DBG("Accepting application registration");
1567 /* Inifinite blocking call, waiting for transmission */
1569 health_poll_entry();
1570 ret
= lttng_poll_wait(&events
, -1);
1574 * Restart interrupted system call.
1576 if (errno
== EINTR
) {
1584 for (i
= 0; i
< nb_fd
; i
++) {
1585 health_code_update();
1587 /* Fetch once the poll data */
1588 revents
= LTTNG_POLL_GETEV(&events
, i
);
1589 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1591 /* Thread quit pipe has been closed. Killing thread. */
1592 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1598 /* Event on the registration socket */
1599 if (pollfd
== apps_sock
) {
1600 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1601 ERR("Register apps socket poll error");
1603 } else if (revents
& LPOLLIN
) {
1604 sock
= lttcomm_accept_unix_sock(apps_sock
);
1610 * Set the CLOEXEC flag. Return code is useless because
1611 * either way, the show must go on.
1613 (void) utils_set_fd_cloexec(sock
);
1615 /* Create UST registration command for enqueuing */
1616 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1617 if (ust_cmd
== NULL
) {
1618 PERROR("ust command zmalloc");
1623 * Using message-based transmissions to ensure we don't
1624 * have to deal with partially received messages.
1626 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1628 ERR("Exhausted file descriptors allowed for applications.");
1638 health_code_update();
1639 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
1642 /* Close socket of the application. */
1647 lttng_fd_put(LTTNG_FD_APPS
, 1);
1651 health_code_update();
1653 ust_cmd
->sock
= sock
;
1656 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1657 " gid:%d sock:%d name:%s (version %d.%d)",
1658 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1659 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1660 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1661 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1664 * Lock free enqueue the registration request. The red pill
1665 * has been taken! This apps will be part of the *system*.
1667 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1670 * Wake the registration queue futex. Implicit memory
1671 * barrier with the exchange in cds_wfq_enqueue.
1673 futex_nto1_wake(&ust_cmd_queue
.futex
);
1683 ERR("Health error occurred in %s", __func__
);
1686 /* Notify that the registration thread is gone */
1689 if (apps_sock
>= 0) {
1690 ret
= close(apps_sock
);
1700 lttng_fd_put(LTTNG_FD_APPS
, 1);
1702 unlink(apps_unix_sock_path
);
1705 lttng_poll_clean(&events
);
1709 DBG("UST Registration thread cleanup complete");
1710 health_unregister();
1716 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1717 * exec or it will fails.
1719 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
1722 struct timespec timeout
;
1724 /* Make sure we set the readiness flag to 0 because we are NOT ready */
1725 consumer_data
->consumer_thread_is_ready
= 0;
1727 /* Setup pthread condition */
1728 ret
= pthread_condattr_init(&consumer_data
->condattr
);
1731 PERROR("pthread_condattr_init consumer data");
1736 * Set the monotonic clock in order to make sure we DO NOT jump in time
1737 * between the clock_gettime() call and the timedwait call. See bug #324
1738 * for a more details and how we noticed it.
1740 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
1743 PERROR("pthread_condattr_setclock consumer data");
1747 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
1750 PERROR("pthread_cond_init consumer data");
1754 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
1757 PERROR("pthread_create consumer");
1762 /* We are about to wait on a pthread condition */
1763 pthread_mutex_lock(&consumer_data
->cond_mutex
);
1765 /* Get time for sem_timedwait absolute timeout */
1766 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
1768 * Set the timeout for the condition timed wait even if the clock gettime
1769 * call fails since we might loop on that call and we want to avoid to
1770 * increment the timeout too many times.
1772 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
1775 * The following loop COULD be skipped in some conditions so this is why we
1776 * set ret to 0 in order to make sure at least one round of the loop is
1782 * Loop until the condition is reached or when a timeout is reached. Note
1783 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
1784 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
1785 * possible. This loop does not take any chances and works with both of
1788 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
1789 if (clock_ret
< 0) {
1790 PERROR("clock_gettime spawn consumer");
1791 /* Infinite wait for the consumerd thread to be ready */
1792 ret
= pthread_cond_wait(&consumer_data
->cond
,
1793 &consumer_data
->cond_mutex
);
1795 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
1796 &consumer_data
->cond_mutex
, &timeout
);
1800 /* Release the pthread condition */
1801 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
1805 if (ret
== ETIMEDOUT
) {
1807 * Call has timed out so we kill the kconsumerd_thread and return
1810 ERR("Condition timed out. The consumer thread was never ready."
1812 ret
= pthread_cancel(consumer_data
->thread
);
1814 PERROR("pthread_cancel consumer thread");
1817 PERROR("pthread_cond_wait failed consumer thread");
1822 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1823 if (consumer_data
->pid
== 0) {
1824 ERR("Consumerd did not start");
1825 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1828 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1837 * Join consumer thread
1839 static int join_consumer_thread(struct consumer_data
*consumer_data
)
1843 /* Consumer pid must be a real one. */
1844 if (consumer_data
->pid
> 0) {
1846 ret
= kill(consumer_data
->pid
, SIGTERM
);
1848 ERR("Error killing consumer daemon");
1851 return pthread_join(consumer_data
->thread
, &status
);
1858 * Fork and exec a consumer daemon (consumerd).
1860 * Return pid if successful else -1.
1862 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
1866 const char *consumer_to_use
;
1867 const char *verbosity
;
1870 DBG("Spawning consumerd");
1877 if (opt_verbose_consumer
) {
1878 verbosity
= "--verbose";
1880 verbosity
= "--quiet";
1882 switch (consumer_data
->type
) {
1883 case LTTNG_CONSUMER_KERNEL
:
1885 * Find out which consumerd to execute. We will first try the
1886 * 64-bit path, then the sessiond's installation directory, and
1887 * fallback on the 32-bit one,
1889 DBG3("Looking for a kernel consumer at these locations:");
1890 DBG3(" 1) %s", consumerd64_bin
);
1891 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
1892 DBG3(" 3) %s", consumerd32_bin
);
1893 if (stat(consumerd64_bin
, &st
) == 0) {
1894 DBG3("Found location #1");
1895 consumer_to_use
= consumerd64_bin
;
1896 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
1897 DBG3("Found location #2");
1898 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
1899 } else if (stat(consumerd32_bin
, &st
) == 0) {
1900 DBG3("Found location #3");
1901 consumer_to_use
= consumerd32_bin
;
1903 DBG("Could not find any valid consumerd executable");
1906 DBG("Using kernel consumer at: %s", consumer_to_use
);
1907 execl(consumer_to_use
,
1908 "lttng-consumerd", verbosity
, "-k",
1909 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1910 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1913 case LTTNG_CONSUMER64_UST
:
1915 char *tmpnew
= NULL
;
1917 if (consumerd64_libdir
[0] != '\0') {
1921 tmp
= getenv("LD_LIBRARY_PATH");
1925 tmplen
= strlen("LD_LIBRARY_PATH=")
1926 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
1927 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1932 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1933 strcat(tmpnew
, consumerd64_libdir
);
1934 if (tmp
[0] != '\0') {
1935 strcat(tmpnew
, ":");
1936 strcat(tmpnew
, tmp
);
1938 ret
= putenv(tmpnew
);
1945 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
1946 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
1947 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1948 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1950 if (consumerd64_libdir
[0] != '\0') {
1958 case LTTNG_CONSUMER32_UST
:
1960 char *tmpnew
= NULL
;
1962 if (consumerd32_libdir
[0] != '\0') {
1966 tmp
= getenv("LD_LIBRARY_PATH");
1970 tmplen
= strlen("LD_LIBRARY_PATH=")
1971 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
1972 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1977 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1978 strcat(tmpnew
, consumerd32_libdir
);
1979 if (tmp
[0] != '\0') {
1980 strcat(tmpnew
, ":");
1981 strcat(tmpnew
, tmp
);
1983 ret
= putenv(tmpnew
);
1990 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
1991 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
1992 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1993 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1995 if (consumerd32_libdir
[0] != '\0') {
2004 PERROR("unknown consumer type");
2008 PERROR("kernel start consumer exec");
2011 } else if (pid
> 0) {
2014 PERROR("start consumer fork");
2022 * Spawn the consumerd daemon and session daemon thread.
2024 static int start_consumerd(struct consumer_data
*consumer_data
)
2029 * Set the listen() state on the socket since there is a possible race
2030 * between the exec() of the consumer daemon and this call if place in the
2031 * consumer thread. See bug #366 for more details.
2033 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2038 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2039 if (consumer_data
->pid
!= 0) {
2040 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2044 ret
= spawn_consumerd(consumer_data
);
2046 ERR("Spawning consumerd failed");
2047 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2051 /* Setting up the consumer_data pid */
2052 consumer_data
->pid
= ret
;
2053 DBG2("Consumer pid %d", consumer_data
->pid
);
2054 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2056 DBG2("Spawning consumer control thread");
2057 ret
= spawn_consumer_thread(consumer_data
);
2059 ERR("Fatal error spawning consumer control thread");
2067 /* Cleanup already created sockets on error. */
2068 if (consumer_data
->err_sock
>= 0) {
2071 err
= close(consumer_data
->err_sock
);
2073 PERROR("close consumer data error socket");
2080 * Compute health status of each consumer. If one of them is zero (bad
2081 * state), we return 0.
2083 static int check_consumer_health(void)
2087 ret
= health_check_state(HEALTH_TYPE_CONSUMER
);
2089 DBG3("Health consumer check %d", ret
);
2095 * Setup necessary data for kernel tracer action.
2097 static int init_kernel_tracer(void)
2101 /* Modprobe lttng kernel modules */
2102 ret
= modprobe_lttng_control();
2107 /* Open debugfs lttng */
2108 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2109 if (kernel_tracer_fd
< 0) {
2110 DBG("Failed to open %s", module_proc_lttng
);
2115 /* Validate kernel version */
2116 ret
= kernel_validate_version(kernel_tracer_fd
);
2121 ret
= modprobe_lttng_data();
2126 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2130 modprobe_remove_lttng_control();
2131 ret
= close(kernel_tracer_fd
);
2135 kernel_tracer_fd
= -1;
2136 return LTTNG_ERR_KERN_VERSION
;
2139 ret
= close(kernel_tracer_fd
);
2145 modprobe_remove_lttng_control();
2148 WARN("No kernel tracer available");
2149 kernel_tracer_fd
= -1;
2151 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2153 return LTTNG_ERR_KERN_NA
;
2159 * Copy consumer output from the tracing session to the domain session. The
2160 * function also applies the right modification on a per domain basis for the
2161 * trace files destination directory.
2163 * Should *NOT* be called with RCU read-side lock held.
2165 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2168 const char *dir_name
;
2169 struct consumer_output
*consumer
;
2172 assert(session
->consumer
);
2175 case LTTNG_DOMAIN_KERNEL
:
2176 DBG3("Copying tracing session consumer output in kernel session");
2178 * XXX: We should audit the session creation and what this function
2179 * does "extra" in order to avoid a destroy since this function is used
2180 * in the domain session creation (kernel and ust) only. Same for UST
2183 if (session
->kernel_session
->consumer
) {
2184 consumer_destroy_output(session
->kernel_session
->consumer
);
2186 session
->kernel_session
->consumer
=
2187 consumer_copy_output(session
->consumer
);
2188 /* Ease our life a bit for the next part */
2189 consumer
= session
->kernel_session
->consumer
;
2190 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2192 case LTTNG_DOMAIN_UST
:
2193 DBG3("Copying tracing session consumer output in UST session");
2194 if (session
->ust_session
->consumer
) {
2195 consumer_destroy_output(session
->ust_session
->consumer
);
2197 session
->ust_session
->consumer
=
2198 consumer_copy_output(session
->consumer
);
2199 /* Ease our life a bit for the next part */
2200 consumer
= session
->ust_session
->consumer
;
2201 dir_name
= DEFAULT_UST_TRACE_DIR
;
2204 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2208 /* Append correct directory to subdir */
2209 strncat(consumer
->subdir
, dir_name
,
2210 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2211 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2220 * Create an UST session and add it to the session ust list.
2222 * Should *NOT* be called with RCU read-side lock held.
2224 static int create_ust_session(struct ltt_session
*session
,
2225 struct lttng_domain
*domain
)
2228 struct ltt_ust_session
*lus
= NULL
;
2232 assert(session
->consumer
);
2234 switch (domain
->type
) {
2235 case LTTNG_DOMAIN_UST
:
2238 ERR("Unknown UST domain on create session %d", domain
->type
);
2239 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2243 DBG("Creating UST session");
2245 lus
= trace_ust_create_session(session
->id
);
2247 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2251 lus
->uid
= session
->uid
;
2252 lus
->gid
= session
->gid
;
2253 session
->ust_session
= lus
;
2255 /* Copy session output to the newly created UST session */
2256 ret
= copy_session_consumer(domain
->type
, session
);
2257 if (ret
!= LTTNG_OK
) {
2265 session
->ust_session
= NULL
;
2270 * Create a kernel tracer session then create the default channel.
2272 static int create_kernel_session(struct ltt_session
*session
)
2276 DBG("Creating kernel session");
2278 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2280 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2284 /* Code flow safety */
2285 assert(session
->kernel_session
);
2287 /* Copy session output to the newly created Kernel session */
2288 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2289 if (ret
!= LTTNG_OK
) {
2293 /* Create directory(ies) on local filesystem. */
2294 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2295 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2296 ret
= run_as_mkdir_recursive(
2297 session
->kernel_session
->consumer
->dst
.trace_path
,
2298 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2300 if (ret
!= -EEXIST
) {
2301 ERR("Trace directory creation error");
2307 session
->kernel_session
->uid
= session
->uid
;
2308 session
->kernel_session
->gid
= session
->gid
;
2313 trace_kernel_destroy_session(session
->kernel_session
);
2314 session
->kernel_session
= NULL
;
2319 * Count number of session permitted by uid/gid.
2321 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2324 struct ltt_session
*session
;
2326 DBG("Counting number of available session for UID %d GID %d",
2328 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2330 * Only list the sessions the user can control.
2332 if (!session_access_ok(session
, uid
, gid
)) {
2341 * Process the command requested by the lttng client within the command
2342 * context structure. This function make sure that the return structure (llm)
2343 * is set and ready for transmission before returning.
2345 * Return any error encountered or 0 for success.
2347 * "sock" is only used for special-case var. len data.
2349 * Should *NOT* be called with RCU read-side lock held.
2351 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2355 int need_tracing_session
= 1;
2358 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2362 switch (cmd_ctx
->lsm
->cmd_type
) {
2363 case LTTNG_CREATE_SESSION
:
2364 case LTTNG_DESTROY_SESSION
:
2365 case LTTNG_LIST_SESSIONS
:
2366 case LTTNG_LIST_DOMAINS
:
2367 case LTTNG_START_TRACE
:
2368 case LTTNG_STOP_TRACE
:
2369 case LTTNG_DATA_PENDING
:
2376 if (opt_no_kernel
&& need_domain
2377 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2379 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2381 ret
= LTTNG_ERR_KERN_NA
;
2386 /* Deny register consumer if we already have a spawned consumer. */
2387 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2388 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2389 if (kconsumer_data
.pid
> 0) {
2390 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2391 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2394 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2398 * Check for command that don't needs to allocate a returned payload. We do
2399 * this here so we don't have to make the call for no payload at each
2402 switch(cmd_ctx
->lsm
->cmd_type
) {
2403 case LTTNG_LIST_SESSIONS
:
2404 case LTTNG_LIST_TRACEPOINTS
:
2405 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2406 case LTTNG_LIST_DOMAINS
:
2407 case LTTNG_LIST_CHANNELS
:
2408 case LTTNG_LIST_EVENTS
:
2411 /* Setup lttng message with no payload */
2412 ret
= setup_lttng_msg(cmd_ctx
, 0);
2414 /* This label does not try to unlock the session */
2415 goto init_setup_error
;
2419 /* Commands that DO NOT need a session. */
2420 switch (cmd_ctx
->lsm
->cmd_type
) {
2421 case LTTNG_CREATE_SESSION
:
2422 case LTTNG_CALIBRATE
:
2423 case LTTNG_LIST_SESSIONS
:
2424 case LTTNG_LIST_TRACEPOINTS
:
2425 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2426 need_tracing_session
= 0;
2429 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2431 * We keep the session list lock across _all_ commands
2432 * for now, because the per-session lock does not
2433 * handle teardown properly.
2435 session_lock_list();
2436 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2437 if (cmd_ctx
->session
== NULL
) {
2438 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
2439 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2441 /* If no session name specified */
2442 ret
= LTTNG_ERR_SELECT_SESS
;
2446 /* Acquire lock for the session */
2447 session_lock(cmd_ctx
->session
);
2457 * Check domain type for specific "pre-action".
2459 switch (cmd_ctx
->lsm
->domain
.type
) {
2460 case LTTNG_DOMAIN_KERNEL
:
2462 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2466 /* Kernel tracer check */
2467 if (kernel_tracer_fd
== -1) {
2468 /* Basically, load kernel tracer modules */
2469 ret
= init_kernel_tracer();
2475 /* Consumer is in an ERROR state. Report back to client */
2476 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
2477 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2481 /* Need a session for kernel command */
2482 if (need_tracing_session
) {
2483 if (cmd_ctx
->session
->kernel_session
== NULL
) {
2484 ret
= create_kernel_session(cmd_ctx
->session
);
2486 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2491 /* Start the kernel consumer daemon */
2492 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2493 if (kconsumer_data
.pid
== 0 &&
2494 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2495 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2496 ret
= start_consumerd(&kconsumer_data
);
2498 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2501 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
2503 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2507 * The consumer was just spawned so we need to add the socket to
2508 * the consumer output of the session if exist.
2510 ret
= consumer_create_socket(&kconsumer_data
,
2511 cmd_ctx
->session
->kernel_session
->consumer
);
2518 case LTTNG_DOMAIN_UST
:
2520 /* Consumer is in an ERROR state. Report back to client */
2521 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
2522 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2526 if (need_tracing_session
) {
2527 /* Create UST session if none exist. */
2528 if (cmd_ctx
->session
->ust_session
== NULL
) {
2529 ret
= create_ust_session(cmd_ctx
->session
,
2530 &cmd_ctx
->lsm
->domain
);
2531 if (ret
!= LTTNG_OK
) {
2536 /* Start the UST consumer daemons */
2538 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
2539 if (consumerd64_bin
[0] != '\0' &&
2540 ustconsumer64_data
.pid
== 0 &&
2541 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2542 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2543 ret
= start_consumerd(&ustconsumer64_data
);
2545 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
2546 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
2550 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
2551 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2553 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2557 * Setup socket for consumer 64 bit. No need for atomic access
2558 * since it was set above and can ONLY be set in this thread.
2560 ret
= consumer_create_socket(&ustconsumer64_data
,
2561 cmd_ctx
->session
->ust_session
->consumer
);
2567 if (consumerd32_bin
[0] != '\0' &&
2568 ustconsumer32_data
.pid
== 0 &&
2569 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2570 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2571 ret
= start_consumerd(&ustconsumer32_data
);
2573 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
2574 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
2578 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
2579 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2581 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2585 * Setup socket for consumer 64 bit. No need for atomic access
2586 * since it was set above and can ONLY be set in this thread.
2588 ret
= consumer_create_socket(&ustconsumer32_data
,
2589 cmd_ctx
->session
->ust_session
->consumer
);
2601 /* Validate consumer daemon state when start/stop trace command */
2602 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
2603 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
2604 switch (cmd_ctx
->lsm
->domain
.type
) {
2605 case LTTNG_DOMAIN_UST
:
2606 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
2607 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2611 case LTTNG_DOMAIN_KERNEL
:
2612 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
2613 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2621 * Check that the UID or GID match that of the tracing session.
2622 * The root user can interact with all sessions.
2624 if (need_tracing_session
) {
2625 if (!session_access_ok(cmd_ctx
->session
,
2626 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2627 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
2628 ret
= LTTNG_ERR_EPERM
;
2634 * Send relayd information to consumer as soon as we have a domain and a
2637 if (cmd_ctx
->session
&& need_domain
) {
2639 * Setup relayd if not done yet. If the relayd information was already
2640 * sent to the consumer, this call will gracefully return.
2642 ret
= cmd_setup_relayd(cmd_ctx
->session
);
2643 if (ret
!= LTTNG_OK
) {
2648 /* Process by command type */
2649 switch (cmd_ctx
->lsm
->cmd_type
) {
2650 case LTTNG_ADD_CONTEXT
:
2652 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2653 cmd_ctx
->lsm
->u
.context
.channel_name
,
2654 &cmd_ctx
->lsm
->u
.context
.ctx
, kernel_poll_pipe
[1]);
2657 case LTTNG_DISABLE_CHANNEL
:
2659 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2660 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2663 case LTTNG_DISABLE_EVENT
:
2665 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2666 cmd_ctx
->lsm
->u
.disable
.channel_name
,
2667 cmd_ctx
->lsm
->u
.disable
.name
);
2670 case LTTNG_DISABLE_ALL_EVENT
:
2672 DBG("Disabling all events");
2674 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2675 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2678 case LTTNG_ENABLE_CHANNEL
:
2680 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
2681 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
2684 case LTTNG_ENABLE_EVENT
:
2686 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
2687 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2688 &cmd_ctx
->lsm
->u
.enable
.event
, NULL
, kernel_poll_pipe
[1]);
2691 case LTTNG_ENABLE_ALL_EVENT
:
2693 DBG("Enabling all events");
2695 ret
= cmd_enable_event_all(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
2696 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2697 cmd_ctx
->lsm
->u
.enable
.event
.type
, NULL
, kernel_poll_pipe
[1]);
2700 case LTTNG_LIST_TRACEPOINTS
:
2702 struct lttng_event
*events
;
2705 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
2706 if (nb_events
< 0) {
2707 /* Return value is a negative lttng_error_code. */
2713 * Setup lttng message with payload size set to the event list size in
2714 * bytes and then copy list into the llm payload.
2716 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2722 /* Copy event list into message payload */
2723 memcpy(cmd_ctx
->llm
->payload
, events
,
2724 sizeof(struct lttng_event
) * nb_events
);
2731 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2733 struct lttng_event_field
*fields
;
2736 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
2738 if (nb_fields
< 0) {
2739 /* Return value is a negative lttng_error_code. */
2745 * Setup lttng message with payload size set to the event list size in
2746 * bytes and then copy list into the llm payload.
2748 ret
= setup_lttng_msg(cmd_ctx
,
2749 sizeof(struct lttng_event_field
) * nb_fields
);
2755 /* Copy event list into message payload */
2756 memcpy(cmd_ctx
->llm
->payload
, fields
,
2757 sizeof(struct lttng_event_field
) * nb_fields
);
2764 case LTTNG_SET_CONSUMER_URI
:
2767 struct lttng_uri
*uris
;
2769 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
2770 len
= nb_uri
* sizeof(struct lttng_uri
);
2773 ret
= LTTNG_ERR_INVALID
;
2777 uris
= zmalloc(len
);
2779 ret
= LTTNG_ERR_FATAL
;
2783 /* Receive variable len data */
2784 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
2785 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
2787 DBG("No URIs received from client... continuing");
2789 ret
= LTTNG_ERR_SESSION_FAIL
;
2794 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
2796 if (ret
!= LTTNG_OK
) {
2802 * XXX: 0 means that this URI should be applied on the session. Should
2803 * be a DOMAIN enuam.
2805 if (cmd_ctx
->lsm
->domain
.type
== 0) {
2806 /* Add the URI for the UST session if a consumer is present. */
2807 if (cmd_ctx
->session
->ust_session
&&
2808 cmd_ctx
->session
->ust_session
->consumer
) {
2809 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_UST
, cmd_ctx
->session
,
2811 } else if (cmd_ctx
->session
->kernel_session
&&
2812 cmd_ctx
->session
->kernel_session
->consumer
) {
2813 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL
,
2814 cmd_ctx
->session
, nb_uri
, uris
);
2822 case LTTNG_START_TRACE
:
2824 ret
= cmd_start_trace(cmd_ctx
->session
);
2827 case LTTNG_STOP_TRACE
:
2829 ret
= cmd_stop_trace(cmd_ctx
->session
);
2832 case LTTNG_CREATE_SESSION
:
2835 struct lttng_uri
*uris
= NULL
;
2837 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
2838 len
= nb_uri
* sizeof(struct lttng_uri
);
2841 uris
= zmalloc(len
);
2843 ret
= LTTNG_ERR_FATAL
;
2847 /* Receive variable len data */
2848 DBG("Waiting for %zu URIs from client ...", nb_uri
);
2849 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
2851 DBG("No URIs received from client... continuing");
2853 ret
= LTTNG_ERR_SESSION_FAIL
;
2858 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
2859 DBG("Creating session with ONE network URI is a bad call");
2860 ret
= LTTNG_ERR_SESSION_FAIL
;
2866 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
2873 case LTTNG_DESTROY_SESSION
:
2875 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
2877 /* Set session to NULL so we do not unlock it after free. */
2878 cmd_ctx
->session
= NULL
;
2881 case LTTNG_LIST_DOMAINS
:
2884 struct lttng_domain
*domains
;
2886 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
2888 /* Return value is a negative lttng_error_code. */
2893 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
2898 /* Copy event list into message payload */
2899 memcpy(cmd_ctx
->llm
->payload
, domains
,
2900 nb_dom
* sizeof(struct lttng_domain
));
2907 case LTTNG_LIST_CHANNELS
:
2910 struct lttng_channel
*channels
;
2912 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
2913 cmd_ctx
->session
, &channels
);
2915 /* Return value is a negative lttng_error_code. */
2920 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
2925 /* Copy event list into message payload */
2926 memcpy(cmd_ctx
->llm
->payload
, channels
,
2927 nb_chan
* sizeof(struct lttng_channel
));
2934 case LTTNG_LIST_EVENTS
:
2937 struct lttng_event
*events
= NULL
;
2939 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
2940 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
2942 /* Return value is a negative lttng_error_code. */
2947 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
2952 /* Copy event list into message payload */
2953 memcpy(cmd_ctx
->llm
->payload
, events
,
2954 nb_event
* sizeof(struct lttng_event
));
2961 case LTTNG_LIST_SESSIONS
:
2963 unsigned int nr_sessions
;
2965 session_lock_list();
2966 nr_sessions
= lttng_sessions_count(
2967 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2968 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
2970 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
2972 session_unlock_list();
2976 /* Filled the session array */
2977 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
2978 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2979 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
2981 session_unlock_list();
2986 case LTTNG_CALIBRATE
:
2988 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
2989 &cmd_ctx
->lsm
->u
.calibrate
);
2992 case LTTNG_REGISTER_CONSUMER
:
2994 struct consumer_data
*cdata
;
2996 switch (cmd_ctx
->lsm
->domain
.type
) {
2997 case LTTNG_DOMAIN_KERNEL
:
2998 cdata
= &kconsumer_data
;
3001 ret
= LTTNG_ERR_UND
;
3005 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3006 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3009 case LTTNG_ENABLE_EVENT_WITH_FILTER
:
3011 struct lttng_filter_bytecode
*bytecode
;
3013 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3014 ret
= LTTNG_ERR_FILTER_INVAL
;
3017 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
== 0) {
3018 ret
= LTTNG_ERR_FILTER_INVAL
;
3021 bytecode
= zmalloc(cmd_ctx
->lsm
->u
.enable
.bytecode_len
);
3023 ret
= LTTNG_ERR_FILTER_NOMEM
;
3026 /* Receive var. len. data */
3027 DBG("Receiving var len data from client ...");
3028 ret
= lttcomm_recv_unix_sock(sock
, bytecode
,
3029 cmd_ctx
->lsm
->u
.enable
.bytecode_len
);
3031 DBG("Nothing recv() from client var len data... continuing");
3033 ret
= LTTNG_ERR_FILTER_INVAL
;
3037 if (bytecode
->len
+ sizeof(*bytecode
)
3038 != cmd_ctx
->lsm
->u
.enable
.bytecode_len
) {
3040 ret
= LTTNG_ERR_FILTER_INVAL
;
3044 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3045 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3046 &cmd_ctx
->lsm
->u
.enable
.event
, bytecode
, kernel_poll_pipe
[1]);
3049 case LTTNG_DATA_PENDING
:
3051 ret
= cmd_data_pending(cmd_ctx
->session
);
3055 ret
= LTTNG_ERR_UND
;
3060 if (cmd_ctx
->llm
== NULL
) {
3061 DBG("Missing llm structure. Allocating one.");
3062 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
3066 /* Set return code */
3067 cmd_ctx
->llm
->ret_code
= ret
;
3069 if (cmd_ctx
->session
) {
3070 session_unlock(cmd_ctx
->session
);
3072 if (need_tracing_session
) {
3073 session_unlock_list();
3080 * Thread managing health check socket.
3082 static void *thread_manage_health(void *data
)
3084 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
3085 uint32_t revents
, nb_fd
;
3086 struct lttng_poll_event events
;
3087 struct lttcomm_health_msg msg
;
3088 struct lttcomm_health_data reply
;
3090 DBG("[thread] Manage health check started");
3092 rcu_register_thread();
3094 /* Create unix socket */
3095 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
3097 ERR("Unable to create health check Unix socket");
3103 * Set the CLOEXEC flag. Return code is useless because either way, the
3106 (void) utils_set_fd_cloexec(sock
);
3108 ret
= lttcomm_listen_unix_sock(sock
);
3114 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3115 * more will be added to this poll set.
3117 ret
= sessiond_set_thread_pollset(&events
, 2);
3122 /* Add the application registration socket */
3123 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
3129 DBG("Health check ready");
3131 /* Inifinite blocking call, waiting for transmission */
3133 ret
= lttng_poll_wait(&events
, -1);
3136 * Restart interrupted system call.
3138 if (errno
== EINTR
) {
3146 for (i
= 0; i
< nb_fd
; i
++) {
3147 /* Fetch once the poll data */
3148 revents
= LTTNG_POLL_GETEV(&events
, i
);
3149 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3151 /* Thread quit pipe has been closed. Killing thread. */
3152 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3158 /* Event on the registration socket */
3159 if (pollfd
== sock
) {
3160 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3161 ERR("Health socket poll error");
3167 new_sock
= lttcomm_accept_unix_sock(sock
);
3173 * Set the CLOEXEC flag. Return code is useless because either way, the
3176 (void) utils_set_fd_cloexec(new_sock
);
3178 DBG("Receiving data from client for health...");
3179 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
3181 DBG("Nothing recv() from client... continuing");
3182 ret
= close(new_sock
);
3190 rcu_thread_online();
3192 switch (msg
.component
) {
3193 case LTTNG_HEALTH_CMD
:
3194 reply
.ret_code
= health_check_state(HEALTH_TYPE_CMD
);
3196 case LTTNG_HEALTH_APP_MANAGE
:
3197 reply
.ret_code
= health_check_state(HEALTH_TYPE_APP_MANAGE
);
3199 case LTTNG_HEALTH_APP_REG
:
3200 reply
.ret_code
= health_check_state(HEALTH_TYPE_APP_REG
);
3202 case LTTNG_HEALTH_KERNEL
:
3203 reply
.ret_code
= health_check_state(HEALTH_TYPE_KERNEL
);
3205 case LTTNG_HEALTH_CONSUMER
:
3206 reply
.ret_code
= check_consumer_health();
3208 case LTTNG_HEALTH_ALL
:
3210 health_check_state(HEALTH_TYPE_APP_MANAGE
) &&
3211 health_check_state(HEALTH_TYPE_APP_REG
) &&
3212 health_check_state(HEALTH_TYPE_CMD
) &&
3213 health_check_state(HEALTH_TYPE_KERNEL
) &&
3214 check_consumer_health();
3217 reply
.ret_code
= LTTNG_ERR_UND
;
3222 * Flip ret value since 0 is a success and 1 indicates a bad health for
3223 * the client where in the sessiond it is the opposite. Again, this is
3224 * just to make things easier for us poor developer which enjoy a lot
3227 if (reply
.ret_code
== 0 || reply
.ret_code
== 1) {
3228 reply
.ret_code
= !reply
.ret_code
;
3231 DBG2("Health check return value %d", reply
.ret_code
);
3233 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
3235 ERR("Failed to send health data back to client");
3238 /* End of transmission */
3239 ret
= close(new_sock
);
3249 ERR("Health error occurred in %s", __func__
);
3251 DBG("Health check thread dying");
3252 unlink(health_unix_sock_path
);
3259 if (new_sock
>= 0) {
3260 ret
= close(new_sock
);
3266 lttng_poll_clean(&events
);
3268 rcu_unregister_thread();
3273 * This thread manage all clients request using the unix client socket for
3276 static void *thread_manage_clients(void *data
)
3278 int sock
= -1, ret
, i
, pollfd
, err
= -1;
3280 uint32_t revents
, nb_fd
;
3281 struct command_ctx
*cmd_ctx
= NULL
;
3282 struct lttng_poll_event events
;
3284 DBG("[thread] Manage client started");
3286 rcu_register_thread();
3288 health_register(HEALTH_TYPE_CMD
);
3290 if (testpoint(thread_manage_clients
)) {
3291 goto error_testpoint
;
3294 health_code_update();
3296 ret
= lttcomm_listen_unix_sock(client_sock
);
3302 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3303 * more will be added to this poll set.
3305 ret
= sessiond_set_thread_pollset(&events
, 2);
3307 goto error_create_poll
;
3310 /* Add the application registration socket */
3311 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
3317 * Notify parent pid that we are ready to accept command for client side.
3319 if (opt_sig_parent
) {
3320 kill(ppid
, SIGUSR1
);
3323 if (testpoint(thread_manage_clients_before_loop
)) {
3327 health_code_update();
3330 DBG("Accepting client command ...");
3332 /* Inifinite blocking call, waiting for transmission */
3334 health_poll_entry();
3335 ret
= lttng_poll_wait(&events
, -1);
3339 * Restart interrupted system call.
3341 if (errno
== EINTR
) {
3349 for (i
= 0; i
< nb_fd
; i
++) {
3350 /* Fetch once the poll data */
3351 revents
= LTTNG_POLL_GETEV(&events
, i
);
3352 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3354 health_code_update();
3356 /* Thread quit pipe has been closed. Killing thread. */
3357 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3363 /* Event on the registration socket */
3364 if (pollfd
== client_sock
) {
3365 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3366 ERR("Client socket poll error");
3372 DBG("Wait for client response");
3374 health_code_update();
3376 sock
= lttcomm_accept_unix_sock(client_sock
);
3382 * Set the CLOEXEC flag. Return code is useless because either way, the
3385 (void) utils_set_fd_cloexec(sock
);
3387 /* Set socket option for credentials retrieval */
3388 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
3393 /* Allocate context command to process the client request */
3394 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
3395 if (cmd_ctx
== NULL
) {
3396 PERROR("zmalloc cmd_ctx");
3400 /* Allocate data buffer for reception */
3401 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
3402 if (cmd_ctx
->lsm
== NULL
) {
3403 PERROR("zmalloc cmd_ctx->lsm");
3407 cmd_ctx
->llm
= NULL
;
3408 cmd_ctx
->session
= NULL
;
3410 health_code_update();
3413 * Data is received from the lttng client. The struct
3414 * lttcomm_session_msg (lsm) contains the command and data request of
3417 DBG("Receiving data from client ...");
3418 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
3419 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
3421 DBG("Nothing recv() from client... continuing");
3427 clean_command_ctx(&cmd_ctx
);
3431 health_code_update();
3433 // TODO: Validate cmd_ctx including sanity check for
3434 // security purpose.
3436 rcu_thread_online();
3438 * This function dispatch the work to the kernel or userspace tracer
3439 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3440 * informations for the client. The command context struct contains
3441 * everything this function may needs.
3443 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
3444 rcu_thread_offline();
3454 * TODO: Inform client somehow of the fatal error. At
3455 * this point, ret < 0 means that a zmalloc failed
3456 * (ENOMEM). Error detected but still accept
3457 * command, unless a socket error has been
3460 clean_command_ctx(&cmd_ctx
);
3464 health_code_update();
3466 DBG("Sending response (size: %d, retcode: %s)",
3467 cmd_ctx
->lttng_msg_size
,
3468 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
3469 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
3471 ERR("Failed to send data back to client");
3474 /* End of transmission */
3481 clean_command_ctx(&cmd_ctx
);
3483 health_code_update();
3495 lttng_poll_clean(&events
);
3496 clean_command_ctx(&cmd_ctx
);
3501 unlink(client_unix_sock_path
);
3502 if (client_sock
>= 0) {
3503 ret
= close(client_sock
);
3511 ERR("Health error occurred in %s", __func__
);
3514 health_unregister();
3516 DBG("Client thread dying");
3518 rcu_unregister_thread();
3524 * usage function on stderr
3526 static void usage(void)
3528 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
3529 fprintf(stderr
, " -h, --help Display this usage.\n");
3530 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
3531 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
3532 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
3533 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
3534 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
3535 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
3536 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
3537 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
3538 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
3539 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
3540 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
3541 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
3542 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
3543 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
3544 fprintf(stderr
, " -V, --version Show version number.\n");
3545 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
3546 fprintf(stderr
, " -q, --quiet No output at all.\n");
3547 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3548 fprintf(stderr
, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
3549 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
3550 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
3554 * daemon argument parsing
3556 static int parse_args(int argc
, char **argv
)
3560 static struct option long_options
[] = {
3561 { "client-sock", 1, 0, 'c' },
3562 { "apps-sock", 1, 0, 'a' },
3563 { "kconsumerd-cmd-sock", 1, 0, 'C' },
3564 { "kconsumerd-err-sock", 1, 0, 'E' },
3565 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
3566 { "ustconsumerd32-err-sock", 1, 0, 'H' },
3567 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
3568 { "ustconsumerd64-err-sock", 1, 0, 'F' },
3569 { "consumerd32-path", 1, 0, 'u' },
3570 { "consumerd32-libdir", 1, 0, 'U' },
3571 { "consumerd64-path", 1, 0, 't' },
3572 { "consumerd64-libdir", 1, 0, 'T' },
3573 { "daemonize", 0, 0, 'd' },
3574 { "sig-parent", 0, 0, 'S' },
3575 { "help", 0, 0, 'h' },
3576 { "group", 1, 0, 'g' },
3577 { "version", 0, 0, 'V' },
3578 { "quiet", 0, 0, 'q' },
3579 { "verbose", 0, 0, 'v' },
3580 { "verbose-consumer", 0, 0, 'Z' },
3581 { "no-kernel", 0, 0, 'N' },
3582 { "pidfile", 1, 0, 'p' },
3587 int option_index
= 0;
3588 c
= getopt_long(argc
, argv
, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t:p:",
3589 long_options
, &option_index
);
3596 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
3598 fprintf(stderr
, " with arg %s\n", optarg
);
3602 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3605 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3611 opt_tracing_group
= optarg
;
3617 fprintf(stdout
, "%s\n", VERSION
);
3623 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3626 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3629 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3632 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3635 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3638 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3644 lttng_opt_quiet
= 1;
3647 /* Verbose level can increase using multiple -v */
3648 lttng_opt_verbose
+= 1;
3651 opt_verbose_consumer
+= 1;
3654 consumerd32_bin
= optarg
;
3657 consumerd32_libdir
= optarg
;
3660 consumerd64_bin
= optarg
;
3663 consumerd64_libdir
= optarg
;
3666 opt_pidfile
= optarg
;
3669 /* Unknown option or other error.
3670 * Error is printed by getopt, just return */
3679 * Creates the two needed socket by the daemon.
3680 * apps_sock - The communication socket for all UST apps.
3681 * client_sock - The communication of the cli tool (lttng).
3683 static int init_daemon_socket(void)
3688 old_umask
= umask(0);
3690 /* Create client tool unix socket */
3691 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
3692 if (client_sock
< 0) {
3693 ERR("Create unix sock failed: %s", client_unix_sock_path
);
3698 /* Set the cloexec flag */
3699 ret
= utils_set_fd_cloexec(client_sock
);
3701 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
3702 "Continuing but note that the consumer daemon will have a "
3703 "reference to this socket on exec()", client_sock
);
3706 /* File permission MUST be 660 */
3707 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3709 ERR("Set file permissions failed: %s", client_unix_sock_path
);
3714 /* Create the application unix socket */
3715 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
3716 if (apps_sock
< 0) {
3717 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
3722 /* Set the cloexec flag */
3723 ret
= utils_set_fd_cloexec(apps_sock
);
3725 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
3726 "Continuing but note that the consumer daemon will have a "
3727 "reference to this socket on exec()", apps_sock
);
3730 /* File permission MUST be 666 */
3731 ret
= chmod(apps_unix_sock_path
,
3732 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
3734 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
3739 DBG3("Session daemon client socket %d and application socket %d created",
3740 client_sock
, apps_sock
);
3748 * Check if the global socket is available, and if a daemon is answering at the
3749 * other side. If yes, error is returned.
3751 static int check_existing_daemon(void)
3753 /* Is there anybody out there ? */
3754 if (lttng_session_daemon_alive()) {
3762 * Set the tracing group gid onto the client socket.
3764 * Race window between mkdir and chown is OK because we are going from more
3765 * permissive (root.root) to less permissive (root.tracing).
3767 static int set_permissions(char *rundir
)
3772 ret
= allowed_group();
3774 WARN("No tracing group detected");
3781 /* Set lttng run dir */
3782 ret
= chown(rundir
, 0, gid
);
3784 ERR("Unable to set group on %s", rundir
);
3788 /* Ensure tracing group can search the run dir */
3789 ret
= chmod(rundir
, S_IRWXU
| S_IXGRP
| S_IXOTH
);
3791 ERR("Unable to set permissions on %s", rundir
);
3795 /* lttng client socket path */
3796 ret
= chown(client_unix_sock_path
, 0, gid
);
3798 ERR("Unable to set group on %s", client_unix_sock_path
);
3802 /* kconsumer error socket path */
3803 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, gid
);
3805 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
3809 /* 64-bit ustconsumer error socket path */
3810 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, gid
);
3812 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
3816 /* 32-bit ustconsumer compat32 error socket path */
3817 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, gid
);
3819 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
3823 DBG("All permissions are set");
3830 * Create the lttng run directory needed for all global sockets and pipe.
3832 static int create_lttng_rundir(const char *rundir
)
3836 DBG3("Creating LTTng run directory: %s", rundir
);
3838 ret
= mkdir(rundir
, S_IRWXU
);
3840 if (errno
!= EEXIST
) {
3841 ERR("Unable to create %s", rundir
);
3853 * Setup sockets and directory needed by the kconsumerd communication with the
3856 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
3860 char path
[PATH_MAX
];
3862 switch (consumer_data
->type
) {
3863 case LTTNG_CONSUMER_KERNEL
:
3864 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
3866 case LTTNG_CONSUMER64_UST
:
3867 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
3869 case LTTNG_CONSUMER32_UST
:
3870 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
3873 ERR("Consumer type unknown");
3878 DBG2("Creating consumer directory: %s", path
);
3880 ret
= mkdir(path
, S_IRWXU
);
3882 if (errno
!= EEXIST
) {
3884 ERR("Failed to create %s", path
);
3890 /* Create the kconsumerd error unix socket */
3891 consumer_data
->err_sock
=
3892 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
3893 if (consumer_data
->err_sock
< 0) {
3894 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
3899 /* File permission MUST be 660 */
3900 ret
= chmod(consumer_data
->err_unix_sock_path
,
3901 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3903 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
3913 * Signal handler for the daemon
3915 * Simply stop all worker threads, leaving main() return gracefully after
3916 * joining all threads and calling cleanup().
3918 static void sighandler(int sig
)
3922 DBG("SIGPIPE caught");
3925 DBG("SIGINT caught");
3929 DBG("SIGTERM caught");
3938 * Setup signal handler for :
3939 * SIGINT, SIGTERM, SIGPIPE
3941 static int set_signal_handler(void)
3944 struct sigaction sa
;
3947 if ((ret
= sigemptyset(&sigset
)) < 0) {
3948 PERROR("sigemptyset");
3952 sa
.sa_handler
= sighandler
;
3953 sa
.sa_mask
= sigset
;
3955 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3956 PERROR("sigaction");
3960 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3961 PERROR("sigaction");
3965 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3966 PERROR("sigaction");
3970 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3976 * Set open files limit to unlimited. This daemon can open a large number of
3977 * file descriptors in order to consumer multiple kernel traces.
3979 static void set_ulimit(void)
3984 /* The kernel does not allowed an infinite limit for open files */
3985 lim
.rlim_cur
= 65535;
3986 lim
.rlim_max
= 65535;
3988 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3990 PERROR("failed to set open files limit");
3995 * Write pidfile using the rundir and opt_pidfile.
3997 static void write_pidfile(void)
4000 char pidfile_path
[PATH_MAX
];
4005 strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
));
4007 /* Build pidfile path from rundir and opt_pidfile. */
4008 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
4009 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
4011 PERROR("snprintf pidfile path");
4017 * Create pid file in rundir. Return value is of no importance. The
4018 * execution will continue even though we are not able to write the file.
4020 (void) utils_create_pid_file(getpid(), pidfile_path
);
4029 int main(int argc
, char **argv
)
4033 const char *home_path
, *env_app_timeout
;
4035 init_kernel_workarounds();
4037 rcu_register_thread();
4039 setup_consumerd_path();
4041 /* Parse arguments */
4043 if ((ret
= parse_args(argc
, argv
)) < 0) {
4053 * child: setsid, close FD 0, 1, 2, chdir /
4054 * parent: exit (if fork is successful)
4062 * We are in the child. Make sure all other file
4063 * descriptors are closed, in case we are called with
4064 * more opened file descriptors than the standard ones.
4066 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
4071 /* Create thread quit pipe */
4072 if ((ret
= init_thread_quit_pipe()) < 0) {
4076 /* Check if daemon is UID = 0 */
4077 is_root
= !getuid();
4080 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
4082 /* Create global run dir with root access */
4083 ret
= create_lttng_rundir(rundir
);
4088 if (strlen(apps_unix_sock_path
) == 0) {
4089 snprintf(apps_unix_sock_path
, PATH_MAX
,
4090 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
4093 if (strlen(client_unix_sock_path
) == 0) {
4094 snprintf(client_unix_sock_path
, PATH_MAX
,
4095 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
4098 /* Set global SHM for ust */
4099 if (strlen(wait_shm_path
) == 0) {
4100 snprintf(wait_shm_path
, PATH_MAX
,
4101 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
4104 if (strlen(health_unix_sock_path
) == 0) {
4105 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4106 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
4109 /* Setup kernel consumerd path */
4110 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
4111 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
4112 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
4113 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
4115 DBG2("Kernel consumer err path: %s",
4116 kconsumer_data
.err_unix_sock_path
);
4117 DBG2("Kernel consumer cmd path: %s",
4118 kconsumer_data
.cmd_unix_sock_path
);
4120 home_path
= get_home_dir();
4121 if (home_path
== NULL
) {
4122 /* TODO: Add --socket PATH option */
4123 ERR("Can't get HOME directory for sockets creation.");
4129 * Create rundir from home path. This will create something like
4132 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
4138 ret
= create_lttng_rundir(rundir
);
4143 if (strlen(apps_unix_sock_path
) == 0) {
4144 snprintf(apps_unix_sock_path
, PATH_MAX
,
4145 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
4148 /* Set the cli tool unix socket path */
4149 if (strlen(client_unix_sock_path
) == 0) {
4150 snprintf(client_unix_sock_path
, PATH_MAX
,
4151 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
4154 /* Set global SHM for ust */
4155 if (strlen(wait_shm_path
) == 0) {
4156 snprintf(wait_shm_path
, PATH_MAX
,
4157 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
4160 /* Set health check Unix path */
4161 if (strlen(health_unix_sock_path
) == 0) {
4162 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4163 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
4167 /* Set consumer initial state */
4168 kernel_consumerd_state
= CONSUMER_STOPPED
;
4169 ust_consumerd_state
= CONSUMER_STOPPED
;
4171 DBG("Client socket path %s", client_unix_sock_path
);
4172 DBG("Application socket path %s", apps_unix_sock_path
);
4173 DBG("Application wait path %s", wait_shm_path
);
4174 DBG("LTTng run directory path: %s", rundir
);
4176 /* 32 bits consumerd path setup */
4177 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
4178 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
4179 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
4180 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
4182 DBG2("UST consumer 32 bits err path: %s",
4183 ustconsumer32_data
.err_unix_sock_path
);
4184 DBG2("UST consumer 32 bits cmd path: %s",
4185 ustconsumer32_data
.cmd_unix_sock_path
);
4187 /* 64 bits consumerd path setup */
4188 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
4189 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
4190 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
4191 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
4193 DBG2("UST consumer 64 bits err path: %s",
4194 ustconsumer64_data
.err_unix_sock_path
);
4195 DBG2("UST consumer 64 bits cmd path: %s",
4196 ustconsumer64_data
.cmd_unix_sock_path
);
4199 * See if daemon already exist.
4201 if ((ret
= check_existing_daemon()) < 0) {
4202 ERR("Already running daemon.\n");
4204 * We do not goto exit because we must not cleanup()
4205 * because a daemon is already running.
4211 * Init UST app hash table. Alloc hash table before this point since
4212 * cleanup() can get called after that point.
4216 /* After this point, we can safely call cleanup() with "goto exit" */
4219 * These actions must be executed as root. We do that *after* setting up
4220 * the sockets path because we MUST make the check for another daemon using
4221 * those paths *before* trying to set the kernel consumer sockets and init
4225 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
4230 /* Setup kernel tracer */
4231 if (!opt_no_kernel
) {
4232 init_kernel_tracer();
4235 /* Set ulimit for open files */
4238 /* init lttng_fd tracking must be done after set_ulimit. */
4241 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
4246 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
4251 if ((ret
= set_signal_handler()) < 0) {
4255 /* Setup the needed unix socket */
4256 if ((ret
= init_daemon_socket()) < 0) {
4260 /* Set credentials to socket */
4261 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
4265 /* Get parent pid if -S, --sig-parent is specified. */
4266 if (opt_sig_parent
) {
4270 /* Setup the kernel pipe for waking up the kernel thread */
4271 if (is_root
&& !opt_no_kernel
) {
4272 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
4277 /* Setup the thread apps communication pipe. */
4278 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
4282 /* Setup the thread apps notify communication pipe. */
4283 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
) < 0) {
4287 /* Initialize global buffer per UID and PID registry. */
4288 buffer_reg_init_uid_registry();
4289 buffer_reg_init_pid_registry();
4291 /* Init UST command queue. */
4292 cds_wfq_init(&ust_cmd_queue
.queue
);
4295 * Get session list pointer. This pointer MUST NOT be free(). This list is
4296 * statically declared in session.c
4298 session_list_ptr
= session_get_list();
4300 /* Set up max poll set size */
4301 lttng_poll_set_max_size();
4305 /* Check for the application socket timeout env variable. */
4306 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
4307 if (env_app_timeout
) {
4308 app_socket_timeout
= atoi(env_app_timeout
);
4310 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
4315 /* Create thread to manage the client socket */
4316 ret
= pthread_create(&health_thread
, NULL
,
4317 thread_manage_health
, (void *) NULL
);
4319 PERROR("pthread_create health");
4323 /* Create thread to manage the client socket */
4324 ret
= pthread_create(&client_thread
, NULL
,
4325 thread_manage_clients
, (void *) NULL
);
4327 PERROR("pthread_create clients");
4331 /* Create thread to dispatch registration */
4332 ret
= pthread_create(&dispatch_thread
, NULL
,
4333 thread_dispatch_ust_registration
, (void *) NULL
);
4335 PERROR("pthread_create dispatch");
4339 /* Create thread to manage application registration. */
4340 ret
= pthread_create(®_apps_thread
, NULL
,
4341 thread_registration_apps
, (void *) NULL
);
4343 PERROR("pthread_create registration");
4347 /* Create thread to manage application socket */
4348 ret
= pthread_create(&apps_thread
, NULL
,
4349 thread_manage_apps
, (void *) NULL
);
4351 PERROR("pthread_create apps");
4355 /* Create thread to manage application notify socket */
4356 ret
= pthread_create(&apps_notify_thread
, NULL
,
4357 ust_thread_manage_notify
, (void *) NULL
);
4359 PERROR("pthread_create apps");
4363 /* Don't start this thread if kernel tracing is not requested nor root */
4364 if (is_root
&& !opt_no_kernel
) {
4365 /* Create kernel thread to manage kernel event */
4366 ret
= pthread_create(&kernel_thread
, NULL
,
4367 thread_manage_kernel
, (void *) NULL
);
4369 PERROR("pthread_create kernel");
4373 ret
= pthread_join(kernel_thread
, &status
);
4375 PERROR("pthread_join");
4376 goto error
; /* join error, exit without cleanup */
4381 ret
= pthread_join(apps_thread
, &status
);
4383 PERROR("pthread_join");
4384 goto error
; /* join error, exit without cleanup */
4388 ret
= pthread_join(reg_apps_thread
, &status
);
4390 PERROR("pthread_join");
4391 goto error
; /* join error, exit without cleanup */
4395 ret
= pthread_join(dispatch_thread
, &status
);
4397 PERROR("pthread_join");
4398 goto error
; /* join error, exit without cleanup */
4402 ret
= pthread_join(client_thread
, &status
);
4404 PERROR("pthread_join");
4405 goto error
; /* join error, exit without cleanup */
4408 ret
= join_consumer_thread(&kconsumer_data
);
4410 PERROR("join_consumer");
4411 goto error
; /* join error, exit without cleanup */
4414 ret
= join_consumer_thread(&ustconsumer32_data
);
4416 PERROR("join_consumer ust32");
4417 goto error
; /* join error, exit without cleanup */
4420 ret
= join_consumer_thread(&ustconsumer64_data
);
4422 PERROR("join_consumer ust64");
4423 goto error
; /* join error, exit without cleanup */
4427 ret
= pthread_join(health_thread
, &status
);
4429 PERROR("pthread_join health thread");
4430 goto error
; /* join error, exit without cleanup */
4436 * cleanup() is called when no other thread is running.
4438 rcu_thread_online();
4440 rcu_thread_offline();
4441 rcu_unregister_thread();