2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <sys/mount.h>
30 #include <sys/resource.h>
31 #include <sys/socket.h>
33 #include <sys/types.h>
35 #include <urcu/uatomic.h>
39 #include <common/common.h>
40 #include <common/compat/poll.h>
41 #include <common/compat/socket.h>
42 #include <common/defaults.h>
43 #include <common/kernel-consumer/kernel-consumer.h>
44 #include <common/futex.h>
45 #include <common/relayd/relayd.h>
46 #include <common/utils.h>
48 #include "lttng-sessiond.h"
55 #include "kernel-consumer.h"
59 #include "ust-consumer.h"
65 #define CONSUMERD_FILE "lttng-consumerd"
68 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
69 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
70 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
71 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
74 const char *opt_tracing_group
;
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 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
94 .lock
= PTHREAD_MUTEX_INITIALIZER
,
95 .cond
= PTHREAD_COND_INITIALIZER
,
96 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
98 static struct consumer_data ustconsumer64_data
= {
99 .type
= LTTNG_CONSUMER64_UST
,
100 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
101 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
104 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
105 .lock
= PTHREAD_MUTEX_INITIALIZER
,
106 .cond
= PTHREAD_COND_INITIALIZER
,
107 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
109 static struct consumer_data ustconsumer32_data
= {
110 .type
= LTTNG_CONSUMER32_UST
,
111 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
112 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
115 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
116 .lock
= PTHREAD_MUTEX_INITIALIZER
,
117 .cond
= PTHREAD_COND_INITIALIZER
,
118 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
121 /* Shared between threads */
122 static int dispatch_thread_exit
;
124 /* Global application Unix socket path */
125 static char apps_unix_sock_path
[PATH_MAX
];
126 /* Global client Unix socket path */
127 static char client_unix_sock_path
[PATH_MAX
];
128 /* global wait shm path for UST */
129 static char wait_shm_path
[PATH_MAX
];
130 /* Global health check unix path */
131 static char health_unix_sock_path
[PATH_MAX
];
133 /* Sockets and FDs */
134 static int client_sock
= -1;
135 static int apps_sock
= -1;
136 int kernel_tracer_fd
= -1;
137 static int kernel_poll_pipe
[2] = { -1, -1 };
140 * Quit pipe for all threads. This permits a single cancellation point
141 * for all threads when receiving an event on the pipe.
143 static int thread_quit_pipe
[2] = { -1, -1 };
146 * This pipe is used to inform the thread managing application communication
147 * that a command is queued and ready to be processed.
149 static int apps_cmd_pipe
[2] = { -1, -1 };
151 /* Pthread, Mutexes and Semaphores */
152 static pthread_t apps_thread
;
153 static pthread_t reg_apps_thread
;
154 static pthread_t client_thread
;
155 static pthread_t kernel_thread
;
156 static pthread_t dispatch_thread
;
157 static pthread_t health_thread
;
160 * UST registration command queue. This queue is tied with a futex and uses a N
161 * wakers / 1 waiter implemented and detailed in futex.c/.h
163 * The thread_manage_apps and thread_dispatch_ust_registration interact with
164 * this queue and the wait/wake scheme.
166 static struct ust_cmd_queue ust_cmd_queue
;
169 * Pointer initialized before thread creation.
171 * This points to the tracing session list containing the session count and a
172 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
173 * MUST NOT be taken if you call a public function in session.c.
175 * The lock is nested inside the structure: session_list_ptr->lock. Please use
176 * session_lock_list and session_unlock_list for lock acquisition.
178 static struct ltt_session_list
*session_list_ptr
;
180 int ust_consumerd64_fd
= -1;
181 int ust_consumerd32_fd
= -1;
183 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
184 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
185 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
186 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
188 static const char *module_proc_lttng
= "/proc/lttng";
191 * Consumer daemon state which is changed when spawning it, killing it or in
192 * case of a fatal error.
194 enum consumerd_state
{
195 CONSUMER_STARTED
= 1,
196 CONSUMER_STOPPED
= 2,
201 * This consumer daemon state is used to validate if a client command will be
202 * able to reach the consumer. If not, the client is informed. For instance,
203 * doing a "lttng start" when the consumer state is set to ERROR will return an
204 * error to the client.
206 * The following example shows a possible race condition of this scheme:
208 * consumer thread error happens
210 * client cmd checks state -> still OK
211 * consumer thread exit, sets error
212 * client cmd try to talk to consumer
215 * However, since the consumer is a different daemon, we have no way of making
216 * sure the command will reach it safely even with this state flag. This is why
217 * we consider that up to the state validation during command processing, the
218 * command is safe. After that, we can not guarantee the correctness of the
219 * client request vis-a-vis the consumer.
221 static enum consumerd_state ust_consumerd_state
;
222 static enum consumerd_state kernel_consumerd_state
;
224 /* Used for the health monitoring of the session daemon. See health.h */
225 struct health_state health_thread_cmd
;
226 struct health_state health_thread_app_manage
;
227 struct health_state health_thread_app_reg
;
228 struct health_state health_thread_kernel
;
231 void setup_consumerd_path(void)
233 const char *bin
, *libdir
;
236 * Allow INSTALL_BIN_PATH to be used as a target path for the
237 * native architecture size consumer if CONFIG_CONSUMER*_PATH
238 * has not been defined.
240 #if (CAA_BITS_PER_LONG == 32)
241 if (!consumerd32_bin
[0]) {
242 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
244 if (!consumerd32_libdir
[0]) {
245 consumerd32_libdir
= INSTALL_LIB_PATH
;
247 #elif (CAA_BITS_PER_LONG == 64)
248 if (!consumerd64_bin
[0]) {
249 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
251 if (!consumerd64_libdir
[0]) {
252 consumerd64_libdir
= INSTALL_LIB_PATH
;
255 #error "Unknown bitness"
259 * runtime env. var. overrides the build default.
261 bin
= getenv("LTTNG_CONSUMERD32_BIN");
263 consumerd32_bin
= bin
;
265 bin
= getenv("LTTNG_CONSUMERD64_BIN");
267 consumerd64_bin
= bin
;
269 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
271 consumerd32_libdir
= libdir
;
273 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
275 consumerd64_libdir
= libdir
;
280 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
282 static int create_thread_poll_set(struct lttng_poll_event
*events
,
287 if (events
== NULL
|| size
== 0) {
292 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
298 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
310 * Check if the thread quit pipe was triggered.
312 * Return 1 if it was triggered else 0;
314 static int check_thread_quit_pipe(int fd
, uint32_t events
)
316 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
324 * Return group ID of the tracing group or -1 if not found.
326 static gid_t
allowed_group(void)
330 if (opt_tracing_group
) {
331 grp
= getgrnam(opt_tracing_group
);
333 grp
= getgrnam(default_tracing_group
);
343 * Init thread quit pipe.
345 * Return -1 on error or 0 if all pipes are created.
347 static int init_thread_quit_pipe(void)
351 ret
= pipe(thread_quit_pipe
);
353 PERROR("thread quit pipe");
357 for (i
= 0; i
< 2; i
++) {
358 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
370 * Stop all threads by closing the thread quit pipe.
372 static void stop_threads(void)
376 /* Stopping all threads */
377 DBG("Terminating all threads");
378 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
380 ERR("write error on thread quit pipe");
383 /* Dispatch thread */
384 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
385 futex_nto1_wake(&ust_cmd_queue
.futex
);
391 static void cleanup(void)
395 struct ltt_session
*sess
, *stmp
;
399 /* First thing first, stop all threads */
400 utils_close_pipe(thread_quit_pipe
);
402 DBG("Removing %s directory", rundir
);
403 ret
= asprintf(&cmd
, "rm -rf %s", rundir
);
405 ERR("asprintf failed. Something is really wrong!");
408 /* Remove lttng run directory */
411 ERR("Unable to clean %s", rundir
);
415 DBG("Cleaning up all sessions");
417 /* Destroy session list mutex */
418 if (session_list_ptr
!= NULL
) {
419 pthread_mutex_destroy(&session_list_ptr
->lock
);
421 /* Cleanup ALL session */
422 cds_list_for_each_entry_safe(sess
, stmp
,
423 &session_list_ptr
->head
, list
) {
424 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
428 DBG("Closing all UST sockets");
429 ust_app_clean_list();
431 if (is_root
&& !opt_no_kernel
) {
432 DBG2("Closing kernel fd");
433 if (kernel_tracer_fd
>= 0) {
434 ret
= close(kernel_tracer_fd
);
439 DBG("Unloading kernel modules");
440 modprobe_remove_lttng_all();
443 utils_close_pipe(kernel_poll_pipe
);
444 utils_close_pipe(apps_cmd_pipe
);
447 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
448 "Matthew, BEET driven development works!%c[%dm",
449 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
454 * Send data on a unix socket using the liblttsessiondcomm API.
456 * Return lttcomm error code.
458 static int send_unix_sock(int sock
, void *buf
, size_t len
)
460 /* Check valid length */
465 return lttcomm_send_unix_sock(sock
, buf
, len
);
469 * Free memory of a command context structure.
471 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
473 DBG("Clean command context structure");
475 if ((*cmd_ctx
)->llm
) {
476 free((*cmd_ctx
)->llm
);
478 if ((*cmd_ctx
)->lsm
) {
479 free((*cmd_ctx
)->lsm
);
487 * Notify UST applications using the shm mmap futex.
489 static int notify_ust_apps(int active
)
493 DBG("Notifying applications of session daemon state: %d", active
);
495 /* See shm.c for this call implying mmap, shm and futex calls */
496 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
497 if (wait_shm_mmap
== NULL
) {
501 /* Wake waiting process */
502 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
504 /* Apps notified successfully */
512 * Setup the outgoing data buffer for the response (llm) by allocating the
513 * right amount of memory and copying the original information from the lsm
516 * Return total size of the buffer pointed by buf.
518 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
524 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
525 if (cmd_ctx
->llm
== NULL
) {
531 /* Copy common data */
532 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
533 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
535 cmd_ctx
->llm
->data_size
= size
;
536 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
545 * Update the kernel poll set of all channel fd available over all tracing
546 * session. Add the wakeup pipe at the end of the set.
548 static int update_kernel_poll(struct lttng_poll_event
*events
)
551 struct ltt_session
*session
;
552 struct ltt_kernel_channel
*channel
;
554 DBG("Updating kernel poll set");
557 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
558 session_lock(session
);
559 if (session
->kernel_session
== NULL
) {
560 session_unlock(session
);
564 cds_list_for_each_entry(channel
,
565 &session
->kernel_session
->channel_list
.head
, list
) {
566 /* Add channel fd to the kernel poll set */
567 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
569 session_unlock(session
);
572 DBG("Channel fd %d added to kernel set", channel
->fd
);
574 session_unlock(session
);
576 session_unlock_list();
581 session_unlock_list();
586 * Find the channel fd from 'fd' over all tracing session. When found, check
587 * for new channel stream and send those stream fds to the kernel consumer.
589 * Useful for CPU hotplug feature.
591 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
594 struct ltt_session
*session
;
595 struct ltt_kernel_session
*ksess
;
596 struct ltt_kernel_channel
*channel
;
598 DBG("Updating kernel streams for channel fd %d", fd
);
601 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
602 session_lock(session
);
603 if (session
->kernel_session
== NULL
) {
604 session_unlock(session
);
607 ksess
= session
->kernel_session
;
609 cds_list_for_each_entry(channel
, &ksess
->channel_list
.head
, list
) {
610 if (channel
->fd
== fd
) {
611 DBG("Channel found, updating kernel streams");
612 ret
= kernel_open_channel_stream(channel
);
618 * Have we already sent fds to the consumer? If yes, it means
619 * that tracing is started so it is safe to send our updated
622 if (ksess
->consumer_fds_sent
== 1 && ksess
->consumer
!= NULL
) {
623 struct lttng_ht_iter iter
;
624 struct consumer_socket
*socket
;
627 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
628 &iter
.iter
, socket
, node
.node
) {
629 /* Code flow error */
630 assert(socket
->fd
>= 0);
632 pthread_mutex_lock(socket
->lock
);
633 ret
= kernel_consumer_send_channel_stream(socket
->fd
,
635 pthread_mutex_unlock(socket
->lock
);
644 session_unlock(session
);
646 session_unlock_list();
650 session_unlock(session
);
651 session_unlock_list();
656 * For each tracing session, update newly registered apps.
658 static void update_ust_app(int app_sock
)
660 struct ltt_session
*sess
, *stmp
;
664 /* For all tracing session(s) */
665 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
667 if (sess
->ust_session
) {
668 ust_app_global_update(sess
->ust_session
, app_sock
);
670 session_unlock(sess
);
673 session_unlock_list();
677 * This thread manage event coming from the kernel.
679 * Features supported in this thread:
682 static void *thread_manage_kernel(void *data
)
684 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
685 uint32_t revents
, nb_fd
;
687 struct lttng_poll_event events
;
689 DBG("Thread manage kernel started");
691 health_code_update(&health_thread_kernel
);
693 ret
= create_thread_poll_set(&events
, 2);
695 goto error_poll_create
;
698 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
704 health_code_update(&health_thread_kernel
);
706 if (update_poll_flag
== 1) {
708 * Reset number of fd in the poll set. Always 2 since there is the thread
709 * quit pipe and the kernel pipe.
713 ret
= update_kernel_poll(&events
);
717 update_poll_flag
= 0;
720 nb_fd
= LTTNG_POLL_GETNB(&events
);
722 DBG("Thread kernel polling on %d fds", nb_fd
);
724 /* Zeroed the poll events */
725 lttng_poll_reset(&events
);
727 /* Poll infinite value of time */
729 health_poll_update(&health_thread_kernel
);
730 ret
= lttng_poll_wait(&events
, -1);
731 health_poll_update(&health_thread_kernel
);
734 * Restart interrupted system call.
736 if (errno
== EINTR
) {
740 } else if (ret
== 0) {
741 /* Should not happen since timeout is infinite */
742 ERR("Return value of poll is 0 with an infinite timeout.\n"
743 "This should not have happened! Continuing...");
747 for (i
= 0; i
< nb_fd
; i
++) {
748 /* Fetch once the poll data */
749 revents
= LTTNG_POLL_GETEV(&events
, i
);
750 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
752 health_code_update(&health_thread_kernel
);
754 /* Thread quit pipe has been closed. Killing thread. */
755 ret
= check_thread_quit_pipe(pollfd
, revents
);
761 /* Check for data on kernel pipe */
762 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
763 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
764 update_poll_flag
= 1;
768 * New CPU detected by the kernel. Adding kernel stream to
769 * kernel session and updating the kernel consumer
771 if (revents
& LPOLLIN
) {
772 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
778 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
779 * and unregister kernel stream at this point.
788 lttng_poll_clean(&events
);
791 health_error(&health_thread_kernel
);
792 ERR("Health error occurred in %s", __func__
);
794 health_exit(&health_thread_kernel
);
795 DBG("Kernel thread dying");
800 * Signal pthread condition of the consumer data that the thread.
802 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
804 pthread_mutex_lock(&data
->cond_mutex
);
807 * The state is set before signaling. It can be any value, it's the waiter
808 * job to correctly interpret this condition variable associated to the
809 * consumer pthread_cond.
811 * A value of 0 means that the corresponding thread of the consumer data
812 * was not started. 1 indicates that the thread has started and is ready
813 * for action. A negative value means that there was an error during the
816 data
->consumer_thread_is_ready
= state
;
817 (void) pthread_cond_signal(&data
->cond
);
819 pthread_mutex_unlock(&data
->cond_mutex
);
823 * This thread manage the consumer error sent back to the session daemon.
825 static void *thread_manage_consumer(void *data
)
827 int sock
= -1, i
, ret
, pollfd
, err
= -1;
828 uint32_t revents
, nb_fd
;
829 enum lttcomm_return_code code
;
830 struct lttng_poll_event events
;
831 struct consumer_data
*consumer_data
= data
;
833 DBG("[thread] Manage consumer started");
835 health_code_update(&consumer_data
->health
);
837 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
843 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
844 * Nothing more will be added to this poll set.
846 ret
= create_thread_poll_set(&events
, 2);
851 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
856 nb_fd
= LTTNG_POLL_GETNB(&events
);
858 health_code_update(&consumer_data
->health
);
860 /* Inifinite blocking call, waiting for transmission */
862 health_poll_update(&consumer_data
->health
);
863 ret
= lttng_poll_wait(&events
, -1);
864 health_poll_update(&consumer_data
->health
);
867 * Restart interrupted system call.
869 if (errno
== EINTR
) {
875 for (i
= 0; i
< nb_fd
; i
++) {
876 /* Fetch once the poll data */
877 revents
= LTTNG_POLL_GETEV(&events
, i
);
878 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
880 health_code_update(&consumer_data
->health
);
882 /* Thread quit pipe has been closed. Killing thread. */
883 ret
= check_thread_quit_pipe(pollfd
, revents
);
889 /* Event on the registration socket */
890 if (pollfd
== consumer_data
->err_sock
) {
891 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
892 ERR("consumer err socket poll error");
898 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
903 health_code_update(&consumer_data
->health
);
905 DBG2("Receiving code from consumer err_sock");
907 /* Getting status code from kconsumerd */
908 ret
= lttcomm_recv_unix_sock(sock
, &code
,
909 sizeof(enum lttcomm_return_code
));
914 health_code_update(&consumer_data
->health
);
916 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
917 consumer_data
->cmd_sock
=
918 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
919 if (consumer_data
->cmd_sock
< 0) {
920 /* On error, signal condition and quit. */
921 signal_consumer_condition(consumer_data
, -1);
922 PERROR("consumer connect");
925 signal_consumer_condition(consumer_data
, 1);
926 DBG("Consumer command socket ready");
928 ERR("consumer error when waiting for SOCK_READY : %s",
929 lttcomm_get_readable_code(-code
));
933 /* Remove the kconsumerd error sock since we've established a connexion */
934 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
939 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
944 health_code_update(&consumer_data
->health
);
946 /* Update number of fd */
947 nb_fd
= LTTNG_POLL_GETNB(&events
);
949 /* Inifinite blocking call, waiting for transmission */
951 health_poll_update(&consumer_data
->health
);
952 ret
= lttng_poll_wait(&events
, -1);
953 health_poll_update(&consumer_data
->health
);
956 * Restart interrupted system call.
958 if (errno
== EINTR
) {
964 for (i
= 0; i
< nb_fd
; i
++) {
965 /* Fetch once the poll data */
966 revents
= LTTNG_POLL_GETEV(&events
, i
);
967 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
969 health_code_update(&consumer_data
->health
);
971 /* Thread quit pipe has been closed. Killing thread. */
972 ret
= check_thread_quit_pipe(pollfd
, revents
);
978 /* Event on the kconsumerd socket */
979 if (pollfd
== sock
) {
980 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
981 ERR("consumer err socket second poll error");
987 health_code_update(&consumer_data
->health
);
989 /* Wait for any kconsumerd error */
990 ret
= lttcomm_recv_unix_sock(sock
, &code
,
991 sizeof(enum lttcomm_return_code
));
993 ERR("consumer closed the command socket");
997 ERR("consumer return code : %s", lttcomm_get_readable_code(-code
));
1001 /* Immediately set the consumerd state to stopped */
1002 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1003 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1004 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1005 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1006 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1008 /* Code flow error... */
1012 if (consumer_data
->err_sock
>= 0) {
1013 ret
= close(consumer_data
->err_sock
);
1018 if (consumer_data
->cmd_sock
>= 0) {
1019 ret
= close(consumer_data
->cmd_sock
);
1031 unlink(consumer_data
->err_unix_sock_path
);
1032 unlink(consumer_data
->cmd_unix_sock_path
);
1033 consumer_data
->pid
= 0;
1035 lttng_poll_clean(&events
);
1039 health_error(&consumer_data
->health
);
1040 ERR("Health error occurred in %s", __func__
);
1042 health_exit(&consumer_data
->health
);
1043 DBG("consumer thread cleanup completed");
1049 * This thread manage application communication.
1051 static void *thread_manage_apps(void *data
)
1053 int i
, ret
, pollfd
, err
= -1;
1054 uint32_t revents
, nb_fd
;
1055 struct ust_command ust_cmd
;
1056 struct lttng_poll_event events
;
1058 DBG("[thread] Manage application started");
1060 rcu_register_thread();
1061 rcu_thread_online();
1063 health_code_update(&health_thread_app_manage
);
1065 ret
= create_thread_poll_set(&events
, 2);
1067 goto error_poll_create
;
1070 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1075 health_code_update(&health_thread_app_manage
);
1078 /* Zeroed the events structure */
1079 lttng_poll_reset(&events
);
1081 nb_fd
= LTTNG_POLL_GETNB(&events
);
1083 DBG("Apps thread polling on %d fds", nb_fd
);
1085 /* Inifinite blocking call, waiting for transmission */
1087 health_poll_update(&health_thread_app_manage
);
1088 ret
= lttng_poll_wait(&events
, -1);
1089 health_poll_update(&health_thread_app_manage
);
1092 * Restart interrupted system call.
1094 if (errno
== EINTR
) {
1100 for (i
= 0; i
< nb_fd
; i
++) {
1101 /* Fetch once the poll data */
1102 revents
= LTTNG_POLL_GETEV(&events
, i
);
1103 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1105 health_code_update(&health_thread_app_manage
);
1107 /* Thread quit pipe has been closed. Killing thread. */
1108 ret
= check_thread_quit_pipe(pollfd
, revents
);
1114 /* Inspect the apps cmd pipe */
1115 if (pollfd
== apps_cmd_pipe
[0]) {
1116 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1117 ERR("Apps command pipe error");
1119 } else if (revents
& LPOLLIN
) {
1121 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
1122 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
1123 PERROR("read apps cmd pipe");
1127 health_code_update(&health_thread_app_manage
);
1129 /* Register applicaton to the session daemon */
1130 ret
= ust_app_register(&ust_cmd
.reg_msg
,
1132 if (ret
== -ENOMEM
) {
1134 } else if (ret
< 0) {
1138 health_code_update(&health_thread_app_manage
);
1141 * Validate UST version compatibility.
1143 ret
= ust_app_validate_version(ust_cmd
.sock
);
1146 * Add channel(s) and event(s) to newly registered apps
1147 * from lttng global UST domain.
1149 update_ust_app(ust_cmd
.sock
);
1152 health_code_update(&health_thread_app_manage
);
1154 ret
= ust_app_register_done(ust_cmd
.sock
);
1157 * If the registration is not possible, we simply
1158 * unregister the apps and continue
1160 ust_app_unregister(ust_cmd
.sock
);
1163 * We just need here to monitor the close of the UST
1164 * socket and poll set monitor those by default.
1165 * Listen on POLLIN (even if we never expect any
1166 * data) to ensure that hangup wakes us.
1168 ret
= lttng_poll_add(&events
, ust_cmd
.sock
, LPOLLIN
);
1173 DBG("Apps with sock %d added to poll set",
1177 health_code_update(&health_thread_app_manage
);
1183 * At this point, we know that a registered application made
1184 * the event at poll_wait.
1186 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1187 /* Removing from the poll set */
1188 ret
= lttng_poll_del(&events
, pollfd
);
1193 /* Socket closed on remote end. */
1194 ust_app_unregister(pollfd
);
1199 health_code_update(&health_thread_app_manage
);
1205 lttng_poll_clean(&events
);
1208 health_error(&health_thread_app_manage
);
1209 ERR("Health error occurred in %s", __func__
);
1211 health_exit(&health_thread_app_manage
);
1212 DBG("Application communication apps thread cleanup complete");
1213 rcu_thread_offline();
1214 rcu_unregister_thread();
1219 * Dispatch request from the registration threads to the application
1220 * communication thread.
1222 static void *thread_dispatch_ust_registration(void *data
)
1225 struct cds_wfq_node
*node
;
1226 struct ust_command
*ust_cmd
= NULL
;
1228 DBG("[thread] Dispatch UST command started");
1230 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1231 /* Atomically prepare the queue futex */
1232 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1235 /* Dequeue command for registration */
1236 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1238 DBG("Woken up but nothing in the UST command queue");
1239 /* Continue thread execution */
1243 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1245 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1246 " gid:%d sock:%d name:%s (version %d.%d)",
1247 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1248 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1249 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1250 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1252 * Inform apps thread of the new application registration. This
1253 * call is blocking so we can be assured that the data will be read
1254 * at some point in time or wait to the end of the world :)
1256 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1257 sizeof(struct ust_command
));
1259 PERROR("write apps cmd pipe");
1260 if (errno
== EBADF
) {
1262 * We can't inform the application thread to process
1263 * registration. We will exit or else application
1264 * registration will not occur and tracing will never
1271 } while (node
!= NULL
);
1273 /* Futex wait on queue. Blocking call on futex() */
1274 futex_nto1_wait(&ust_cmd_queue
.futex
);
1278 DBG("Dispatch thread dying");
1283 * This thread manage application registration.
1285 static void *thread_registration_apps(void *data
)
1287 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1288 uint32_t revents
, nb_fd
;
1289 struct lttng_poll_event events
;
1291 * Get allocated in this thread, enqueued to a global queue, dequeued and
1292 * freed in the manage apps thread.
1294 struct ust_command
*ust_cmd
= NULL
;
1296 DBG("[thread] Manage application registration started");
1298 ret
= lttcomm_listen_unix_sock(apps_sock
);
1304 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1305 * more will be added to this poll set.
1307 ret
= create_thread_poll_set(&events
, 2);
1309 goto error_create_poll
;
1312 /* Add the application registration socket */
1313 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1315 goto error_poll_add
;
1318 /* Notify all applications to register */
1319 ret
= notify_ust_apps(1);
1321 ERR("Failed to notify applications or create the wait shared memory.\n"
1322 "Execution continues but there might be problem for already\n"
1323 "running applications that wishes to register.");
1327 DBG("Accepting application registration");
1329 nb_fd
= LTTNG_POLL_GETNB(&events
);
1331 /* Inifinite blocking call, waiting for transmission */
1333 health_poll_update(&health_thread_app_reg
);
1334 ret
= lttng_poll_wait(&events
, -1);
1335 health_poll_update(&health_thread_app_reg
);
1338 * Restart interrupted system call.
1340 if (errno
== EINTR
) {
1346 for (i
= 0; i
< nb_fd
; i
++) {
1347 health_code_update(&health_thread_app_reg
);
1349 /* Fetch once the poll data */
1350 revents
= LTTNG_POLL_GETEV(&events
, i
);
1351 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1353 /* Thread quit pipe has been closed. Killing thread. */
1354 ret
= check_thread_quit_pipe(pollfd
, revents
);
1360 /* Event on the registration socket */
1361 if (pollfd
== apps_sock
) {
1362 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1363 ERR("Register apps socket poll error");
1365 } else if (revents
& LPOLLIN
) {
1366 sock
= lttcomm_accept_unix_sock(apps_sock
);
1371 /* Create UST registration command for enqueuing */
1372 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1373 if (ust_cmd
== NULL
) {
1374 PERROR("ust command zmalloc");
1379 * Using message-based transmissions to ensure we don't
1380 * have to deal with partially received messages.
1382 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1384 ERR("Exhausted file descriptors allowed for applications.");
1393 health_code_update(&health_thread_app_reg
);
1394 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1395 sizeof(struct ust_register_msg
));
1396 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1398 PERROR("lttcomm_recv_unix_sock register apps");
1400 ERR("Wrong size received on apps register");
1407 lttng_fd_put(LTTNG_FD_APPS
, 1);
1411 health_code_update(&health_thread_app_reg
);
1413 ust_cmd
->sock
= sock
;
1416 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1417 " gid:%d sock:%d name:%s (version %d.%d)",
1418 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1419 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1420 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1421 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1424 * Lock free enqueue the registration request. The red pill
1425 * has been taken! This apps will be part of the *system*.
1427 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1430 * Wake the registration queue futex. Implicit memory
1431 * barrier with the exchange in cds_wfq_enqueue.
1433 futex_nto1_wake(&ust_cmd_queue
.futex
);
1442 health_error(&health_thread_app_reg
);
1443 ERR("Health error occurred in %s", __func__
);
1445 health_exit(&health_thread_app_reg
);
1447 /* Notify that the registration thread is gone */
1450 if (apps_sock
>= 0) {
1451 ret
= close(apps_sock
);
1461 lttng_fd_put(LTTNG_FD_APPS
, 1);
1463 unlink(apps_unix_sock_path
);
1466 lttng_poll_clean(&events
);
1469 DBG("UST Registration thread cleanup complete");
1475 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1476 * exec or it will fails.
1478 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
1481 struct timespec timeout
;
1483 /* Make sure we set the readiness flag to 0 because we are NOT ready */
1484 consumer_data
->consumer_thread_is_ready
= 0;
1486 /* Setup pthread condition */
1487 ret
= pthread_condattr_init(&consumer_data
->condattr
);
1490 PERROR("pthread_condattr_init consumer data");
1495 * Set the monotonic clock in order to make sure we DO NOT jump in time
1496 * between the clock_gettime() call and the timedwait call. See bug #324
1497 * for a more details and how we noticed it.
1499 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
1502 PERROR("pthread_condattr_setclock consumer data");
1506 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
1509 PERROR("pthread_cond_init consumer data");
1513 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
1516 PERROR("pthread_create consumer");
1521 /* We are about to wait on a pthread condition */
1522 pthread_mutex_lock(&consumer_data
->cond_mutex
);
1524 /* Get time for sem_timedwait absolute timeout */
1525 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
1527 * Set the timeout for the condition timed wait even if the clock gettime
1528 * call fails since we might loop on that call and we want to avoid to
1529 * increment the timeout too many times.
1531 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
1534 * The following loop COULD be skipped in some conditions so this is why we
1535 * set ret to 0 in order to make sure at least one round of the loop is
1541 * Loop until the condition is reached or when a timeout is reached. Note
1542 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
1543 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
1544 * possible. This loop does not take any chances and works with both of
1547 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
1548 if (clock_ret
< 0) {
1549 PERROR("clock_gettime spawn consumer");
1550 /* Infinite wait for the consumerd thread to be ready */
1551 ret
= pthread_cond_wait(&consumer_data
->cond
,
1552 &consumer_data
->cond_mutex
);
1554 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
1555 &consumer_data
->cond_mutex
, &timeout
);
1559 /* Release the pthread condition */
1560 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
1564 if (ret
== ETIMEDOUT
) {
1566 * Call has timed out so we kill the kconsumerd_thread and return
1569 ERR("Condition timed out. The consumer thread was never ready."
1571 ret
= pthread_cancel(consumer_data
->thread
);
1573 PERROR("pthread_cancel consumer thread");
1576 PERROR("pthread_cond_wait failed consumer thread");
1581 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1582 if (consumer_data
->pid
== 0) {
1583 ERR("Consumerd did not start");
1584 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1587 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1596 * Join consumer thread
1598 static int join_consumer_thread(struct consumer_data
*consumer_data
)
1603 /* Consumer pid must be a real one. */
1604 if (consumer_data
->pid
> 0) {
1605 ret
= kill(consumer_data
->pid
, SIGTERM
);
1607 ERR("Error killing consumer daemon");
1610 return pthread_join(consumer_data
->thread
, &status
);
1617 * Fork and exec a consumer daemon (consumerd).
1619 * Return pid if successful else -1.
1621 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
1625 const char *consumer_to_use
;
1626 const char *verbosity
;
1629 DBG("Spawning consumerd");
1636 if (opt_verbose_consumer
) {
1637 verbosity
= "--verbose";
1639 verbosity
= "--quiet";
1641 switch (consumer_data
->type
) {
1642 case LTTNG_CONSUMER_KERNEL
:
1644 * Find out which consumerd to execute. We will first try the
1645 * 64-bit path, then the sessiond's installation directory, and
1646 * fallback on the 32-bit one,
1648 DBG3("Looking for a kernel consumer at these locations:");
1649 DBG3(" 1) %s", consumerd64_bin
);
1650 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
1651 DBG3(" 3) %s", consumerd32_bin
);
1652 if (stat(consumerd64_bin
, &st
) == 0) {
1653 DBG3("Found location #1");
1654 consumer_to_use
= consumerd64_bin
;
1655 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
1656 DBG3("Found location #2");
1657 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
1658 } else if (stat(consumerd32_bin
, &st
) == 0) {
1659 DBG3("Found location #3");
1660 consumer_to_use
= consumerd32_bin
;
1662 DBG("Could not find any valid consumerd executable");
1665 DBG("Using kernel consumer at: %s", consumer_to_use
);
1666 execl(consumer_to_use
,
1667 "lttng-consumerd", verbosity
, "-k",
1668 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1669 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1672 case LTTNG_CONSUMER64_UST
:
1674 char *tmpnew
= NULL
;
1676 if (consumerd64_libdir
[0] != '\0') {
1680 tmp
= getenv("LD_LIBRARY_PATH");
1684 tmplen
= strlen("LD_LIBRARY_PATH=")
1685 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
1686 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1691 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1692 strcat(tmpnew
, consumerd64_libdir
);
1693 if (tmp
[0] != '\0') {
1694 strcat(tmpnew
, ":");
1695 strcat(tmpnew
, tmp
);
1697 ret
= putenv(tmpnew
);
1703 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
1704 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
1705 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1706 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1708 if (consumerd64_libdir
[0] != '\0') {
1716 case LTTNG_CONSUMER32_UST
:
1718 char *tmpnew
= NULL
;
1720 if (consumerd32_libdir
[0] != '\0') {
1724 tmp
= getenv("LD_LIBRARY_PATH");
1728 tmplen
= strlen("LD_LIBRARY_PATH=")
1729 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
1730 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1735 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1736 strcat(tmpnew
, consumerd32_libdir
);
1737 if (tmp
[0] != '\0') {
1738 strcat(tmpnew
, ":");
1739 strcat(tmpnew
, tmp
);
1741 ret
= putenv(tmpnew
);
1747 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
1748 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
1749 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1750 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1752 if (consumerd32_libdir
[0] != '\0') {
1761 PERROR("unknown consumer type");
1765 PERROR("kernel start consumer exec");
1768 } else if (pid
> 0) {
1771 PERROR("start consumer fork");
1779 * Spawn the consumerd daemon and session daemon thread.
1781 static int start_consumerd(struct consumer_data
*consumer_data
)
1785 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1786 if (consumer_data
->pid
!= 0) {
1787 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1791 ret
= spawn_consumerd(consumer_data
);
1793 ERR("Spawning consumerd failed");
1794 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1798 /* Setting up the consumer_data pid */
1799 consumer_data
->pid
= ret
;
1800 DBG2("Consumer pid %d", consumer_data
->pid
);
1801 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1803 DBG2("Spawning consumer control thread");
1804 ret
= spawn_consumer_thread(consumer_data
);
1806 ERR("Fatal error spawning consumer control thread");
1818 * Compute health status of each consumer. If one of them is zero (bad
1819 * state), we return 0.
1821 static int check_consumer_health(void)
1825 ret
= health_check_state(&kconsumer_data
.health
) &&
1826 health_check_state(&ustconsumer32_data
.health
) &&
1827 health_check_state(&ustconsumer64_data
.health
);
1829 DBG3("Health consumer check %d", ret
);
1835 * Setup necessary data for kernel tracer action.
1837 static int init_kernel_tracer(void)
1841 /* Modprobe lttng kernel modules */
1842 ret
= modprobe_lttng_control();
1847 /* Open debugfs lttng */
1848 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1849 if (kernel_tracer_fd
< 0) {
1850 DBG("Failed to open %s", module_proc_lttng
);
1855 /* Validate kernel version */
1856 ret
= kernel_validate_version(kernel_tracer_fd
);
1861 ret
= modprobe_lttng_data();
1866 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1870 modprobe_remove_lttng_control();
1871 ret
= close(kernel_tracer_fd
);
1875 kernel_tracer_fd
= -1;
1876 return LTTNG_ERR_KERN_VERSION
;
1879 ret
= close(kernel_tracer_fd
);
1885 modprobe_remove_lttng_control();
1888 WARN("No kernel tracer available");
1889 kernel_tracer_fd
= -1;
1891 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
1893 return LTTNG_ERR_KERN_NA
;
1899 * Copy consumer output from the tracing session to the domain session. The
1900 * function also applies the right modification on a per domain basis for the
1901 * trace files destination directory.
1903 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
1906 const char *dir_name
;
1907 struct consumer_output
*consumer
;
1910 assert(session
->consumer
);
1913 case LTTNG_DOMAIN_KERNEL
:
1914 DBG3("Copying tracing session consumer output in kernel session");
1915 session
->kernel_session
->consumer
=
1916 consumer_copy_output(session
->consumer
);
1917 /* Ease our life a bit for the next part */
1918 consumer
= session
->kernel_session
->consumer
;
1919 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
1921 case LTTNG_DOMAIN_UST
:
1922 DBG3("Copying tracing session consumer output in UST session");
1923 session
->ust_session
->consumer
=
1924 consumer_copy_output(session
->consumer
);
1925 /* Ease our life a bit for the next part */
1926 consumer
= session
->ust_session
->consumer
;
1927 dir_name
= DEFAULT_UST_TRACE_DIR
;
1930 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1934 /* Append correct directory to subdir */
1935 strncat(consumer
->subdir
, dir_name
,
1936 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
1937 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
1946 * Create an UST session and add it to the session ust list.
1948 static int create_ust_session(struct ltt_session
*session
,
1949 struct lttng_domain
*domain
)
1952 struct ltt_ust_session
*lus
= NULL
;
1956 assert(session
->consumer
);
1958 switch (domain
->type
) {
1959 case LTTNG_DOMAIN_UST
:
1962 ERR("Unknown UST domain on create session %d", domain
->type
);
1963 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1967 DBG("Creating UST session");
1969 lus
= trace_ust_create_session(session
->path
, session
->id
, domain
);
1971 ret
= LTTNG_ERR_UST_SESS_FAIL
;
1975 lus
->uid
= session
->uid
;
1976 lus
->gid
= session
->gid
;
1977 session
->ust_session
= lus
;
1979 /* Copy session output to the newly created UST session */
1980 ret
= copy_session_consumer(domain
->type
, session
);
1981 if (ret
!= LTTNG_OK
) {
1989 session
->ust_session
= NULL
;
1994 * Create a kernel tracer session then create the default channel.
1996 static int create_kernel_session(struct ltt_session
*session
)
2000 DBG("Creating kernel session");
2002 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2004 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2008 /* Code flow safety */
2009 assert(session
->kernel_session
);
2011 /* Copy session output to the newly created Kernel session */
2012 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2013 if (ret
!= LTTNG_OK
) {
2017 /* Create directory(ies) on local filesystem. */
2018 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2019 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2020 ret
= run_as_mkdir_recursive(
2021 session
->kernel_session
->consumer
->dst
.trace_path
,
2022 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2024 if (ret
!= -EEXIST
) {
2025 ERR("Trace directory creation error");
2031 session
->kernel_session
->uid
= session
->uid
;
2032 session
->kernel_session
->gid
= session
->gid
;
2037 trace_kernel_destroy_session(session
->kernel_session
);
2038 session
->kernel_session
= NULL
;
2043 * Count number of session permitted by uid/gid.
2045 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2048 struct ltt_session
*session
;
2050 DBG("Counting number of available session for UID %d GID %d",
2052 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2054 * Only list the sessions the user can control.
2056 if (!session_access_ok(session
, uid
, gid
)) {
2065 * Process the command requested by the lttng client within the command
2066 * context structure. This function make sure that the return structure (llm)
2067 * is set and ready for transmission before returning.
2069 * Return any error encountered or 0 for success.
2071 * "sock" is only used for special-case var. len data.
2073 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2077 int need_tracing_session
= 1;
2080 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2084 switch (cmd_ctx
->lsm
->cmd_type
) {
2085 case LTTNG_CREATE_SESSION
:
2086 case LTTNG_DESTROY_SESSION
:
2087 case LTTNG_LIST_SESSIONS
:
2088 case LTTNG_LIST_DOMAINS
:
2089 case LTTNG_START_TRACE
:
2090 case LTTNG_STOP_TRACE
:
2097 if (opt_no_kernel
&& need_domain
2098 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2100 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2102 ret
= LTTNG_ERR_KERN_NA
;
2107 /* Deny register consumer if we already have a spawned consumer. */
2108 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2109 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2110 if (kconsumer_data
.pid
> 0) {
2111 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2112 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2115 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2119 * Check for command that don't needs to allocate a returned payload. We do
2120 * this here so we don't have to make the call for no payload at each
2123 switch(cmd_ctx
->lsm
->cmd_type
) {
2124 case LTTNG_LIST_SESSIONS
:
2125 case LTTNG_LIST_TRACEPOINTS
:
2126 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2127 case LTTNG_LIST_DOMAINS
:
2128 case LTTNG_LIST_CHANNELS
:
2129 case LTTNG_LIST_EVENTS
:
2132 /* Setup lttng message with no payload */
2133 ret
= setup_lttng_msg(cmd_ctx
, 0);
2135 /* This label does not try to unlock the session */
2136 goto init_setup_error
;
2140 /* Commands that DO NOT need a session. */
2141 switch (cmd_ctx
->lsm
->cmd_type
) {
2142 case LTTNG_CREATE_SESSION
:
2143 case LTTNG_CALIBRATE
:
2144 case LTTNG_LIST_SESSIONS
:
2145 case LTTNG_LIST_TRACEPOINTS
:
2146 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2147 need_tracing_session
= 0;
2150 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2152 * We keep the session list lock across _all_ commands
2153 * for now, because the per-session lock does not
2154 * handle teardown properly.
2156 session_lock_list();
2157 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2158 if (cmd_ctx
->session
== NULL
) {
2159 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
2160 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2162 /* If no session name specified */
2163 ret
= LTTNG_ERR_SELECT_SESS
;
2167 /* Acquire lock for the session */
2168 session_lock(cmd_ctx
->session
);
2178 * Check domain type for specific "pre-action".
2180 switch (cmd_ctx
->lsm
->domain
.type
) {
2181 case LTTNG_DOMAIN_KERNEL
:
2183 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2187 /* Kernel tracer check */
2188 if (kernel_tracer_fd
== -1) {
2189 /* Basically, load kernel tracer modules */
2190 ret
= init_kernel_tracer();
2196 /* Consumer is in an ERROR state. Report back to client */
2197 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
2198 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2202 /* Need a session for kernel command */
2203 if (need_tracing_session
) {
2204 if (cmd_ctx
->session
->kernel_session
== NULL
) {
2205 ret
= create_kernel_session(cmd_ctx
->session
);
2207 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2212 /* Start the kernel consumer daemon */
2213 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2214 if (kconsumer_data
.pid
== 0 &&
2215 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
&&
2216 cmd_ctx
->session
->start_consumer
) {
2217 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2218 ret
= start_consumerd(&kconsumer_data
);
2220 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2223 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
2225 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2229 * The consumer was just spawned so we need to add the socket to
2230 * the consumer output of the session if exist.
2232 ret
= consumer_create_socket(&kconsumer_data
,
2233 cmd_ctx
->session
->kernel_session
->consumer
);
2240 case LTTNG_DOMAIN_UST
:
2242 /* Consumer is in an ERROR state. Report back to client */
2243 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
2244 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2248 if (need_tracing_session
) {
2249 /* Create UST session if none exist. */
2250 if (cmd_ctx
->session
->ust_session
== NULL
) {
2251 ret
= create_ust_session(cmd_ctx
->session
,
2252 &cmd_ctx
->lsm
->domain
);
2253 if (ret
!= LTTNG_OK
) {
2258 /* Start the UST consumer daemons */
2260 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
2261 if (consumerd64_bin
[0] != '\0' &&
2262 ustconsumer64_data
.pid
== 0 &&
2263 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
&&
2264 cmd_ctx
->session
->start_consumer
) {
2265 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2266 ret
= start_consumerd(&ustconsumer64_data
);
2268 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
2269 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
2273 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
2274 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2276 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2280 * Setup socket for consumer 64 bit. No need for atomic access
2281 * since it was set above and can ONLY be set in this thread.
2283 ret
= consumer_create_socket(&ustconsumer64_data
,
2284 cmd_ctx
->session
->ust_session
->consumer
);
2290 if (consumerd32_bin
[0] != '\0' &&
2291 ustconsumer32_data
.pid
== 0 &&
2292 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
&&
2293 cmd_ctx
->session
->start_consumer
) {
2294 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2295 ret
= start_consumerd(&ustconsumer32_data
);
2297 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
2298 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
2302 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
2303 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2305 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2309 * Setup socket for consumer 64 bit. No need for atomic access
2310 * since it was set above and can ONLY be set in this thread.
2312 ret
= consumer_create_socket(&ustconsumer32_data
,
2313 cmd_ctx
->session
->ust_session
->consumer
);
2325 /* Validate consumer daemon state when start/stop trace command */
2326 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
2327 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
2328 switch (cmd_ctx
->lsm
->domain
.type
) {
2329 case LTTNG_DOMAIN_UST
:
2330 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
2331 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2335 case LTTNG_DOMAIN_KERNEL
:
2336 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
2337 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2345 * Check that the UID or GID match that of the tracing session.
2346 * The root user can interact with all sessions.
2348 if (need_tracing_session
) {
2349 if (!session_access_ok(cmd_ctx
->session
,
2350 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2351 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
2352 ret
= LTTNG_ERR_EPERM
;
2357 /* Process by command type */
2358 switch (cmd_ctx
->lsm
->cmd_type
) {
2359 case LTTNG_ADD_CONTEXT
:
2361 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2362 cmd_ctx
->lsm
->u
.context
.channel_name
,
2363 cmd_ctx
->lsm
->u
.context
.event_name
,
2364 &cmd_ctx
->lsm
->u
.context
.ctx
);
2367 case LTTNG_DISABLE_CHANNEL
:
2369 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2370 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2373 case LTTNG_DISABLE_EVENT
:
2375 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2376 cmd_ctx
->lsm
->u
.disable
.channel_name
,
2377 cmd_ctx
->lsm
->u
.disable
.name
);
2380 case LTTNG_DISABLE_ALL_EVENT
:
2382 DBG("Disabling all events");
2384 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2385 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2388 case LTTNG_DISABLE_CONSUMER
:
2390 ret
= cmd_disable_consumer(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
);
2393 case LTTNG_ENABLE_CHANNEL
:
2395 ret
= cmd_enable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2396 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
2399 case LTTNG_ENABLE_CONSUMER
:
2402 * XXX: 0 means that this URI should be applied on the session. Should
2403 * be a DOMAIN enuam.
2405 ret
= cmd_enable_consumer(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
);
2406 if (ret
!= LTTNG_OK
) {
2410 if (cmd_ctx
->lsm
->domain
.type
== 0) {
2411 /* Add the URI for the UST session if a consumer is present. */
2412 if (cmd_ctx
->session
->ust_session
&&
2413 cmd_ctx
->session
->ust_session
->consumer
) {
2414 ret
= cmd_enable_consumer(LTTNG_DOMAIN_UST
, cmd_ctx
->session
);
2415 } else if (cmd_ctx
->session
->kernel_session
&&
2416 cmd_ctx
->session
->kernel_session
->consumer
) {
2417 ret
= cmd_enable_consumer(LTTNG_DOMAIN_KERNEL
,
2423 case LTTNG_ENABLE_EVENT
:
2425 ret
= cmd_enable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2426 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2427 &cmd_ctx
->lsm
->u
.enable
.event
, kernel_poll_pipe
[1]);
2430 case LTTNG_ENABLE_ALL_EVENT
:
2432 DBG("Enabling all events");
2434 ret
= cmd_enable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2435 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2436 cmd_ctx
->lsm
->u
.enable
.event
.type
, kernel_poll_pipe
[1]);
2439 case LTTNG_LIST_TRACEPOINTS
:
2441 struct lttng_event
*events
;
2444 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
2445 if (nb_events
< 0) {
2446 /* Return value is a negative lttng_error_code. */
2452 * Setup lttng message with payload size set to the event list size in
2453 * bytes and then copy list into the llm payload.
2455 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2461 /* Copy event list into message payload */
2462 memcpy(cmd_ctx
->llm
->payload
, events
,
2463 sizeof(struct lttng_event
) * nb_events
);
2470 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2472 struct lttng_event_field
*fields
;
2475 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
2477 if (nb_fields
< 0) {
2478 /* Return value is a negative lttng_error_code. */
2484 * Setup lttng message with payload size set to the event list size in
2485 * bytes and then copy list into the llm payload.
2487 ret
= setup_lttng_msg(cmd_ctx
,
2488 sizeof(struct lttng_event_field
) * nb_fields
);
2494 /* Copy event list into message payload */
2495 memcpy(cmd_ctx
->llm
->payload
, fields
,
2496 sizeof(struct lttng_event_field
) * nb_fields
);
2503 case LTTNG_SET_CONSUMER_URI
:
2506 struct lttng_uri
*uris
;
2508 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
2509 len
= nb_uri
* sizeof(struct lttng_uri
);
2512 ret
= LTTNG_ERR_INVALID
;
2516 uris
= zmalloc(len
);
2518 ret
= LTTNG_ERR_FATAL
;
2522 /* Receive variable len data */
2523 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
2524 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
2526 DBG("No URIs received from client... continuing");
2528 ret
= LTTNG_ERR_SESSION_FAIL
;
2532 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
2534 if (ret
!= LTTNG_OK
) {
2539 * XXX: 0 means that this URI should be applied on the session. Should
2540 * be a DOMAIN enuam.
2542 if (cmd_ctx
->lsm
->domain
.type
== 0) {
2543 /* Add the URI for the UST session if a consumer is present. */
2544 if (cmd_ctx
->session
->ust_session
&&
2545 cmd_ctx
->session
->ust_session
->consumer
) {
2546 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_UST
, cmd_ctx
->session
,
2548 } else if (cmd_ctx
->session
->kernel_session
&&
2549 cmd_ctx
->session
->kernel_session
->consumer
) {
2550 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL
,
2551 cmd_ctx
->session
, nb_uri
, uris
);
2557 case LTTNG_START_TRACE
:
2559 ret
= cmd_start_trace(cmd_ctx
->session
);
2562 case LTTNG_STOP_TRACE
:
2564 ret
= cmd_stop_trace(cmd_ctx
->session
);
2567 case LTTNG_CREATE_SESSION
:
2570 struct lttng_uri
*uris
= NULL
;
2572 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
2573 len
= nb_uri
* sizeof(struct lttng_uri
);
2576 uris
= zmalloc(len
);
2578 ret
= LTTNG_ERR_FATAL
;
2582 /* Receive variable len data */
2583 DBG("Waiting for %zu URIs from client ...", nb_uri
);
2584 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
2586 DBG("No URIs received from client... continuing");
2588 ret
= LTTNG_ERR_SESSION_FAIL
;
2592 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
2593 DBG("Creating session with ONE network URI is a bad call");
2594 ret
= LTTNG_ERR_SESSION_FAIL
;
2599 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
2604 case LTTNG_DESTROY_SESSION
:
2606 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
2608 /* Set session to NULL so we do not unlock it after free. */
2609 cmd_ctx
->session
= NULL
;
2612 case LTTNG_LIST_DOMAINS
:
2615 struct lttng_domain
*domains
;
2617 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
2619 /* Return value is a negative lttng_error_code. */
2624 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
2629 /* Copy event list into message payload */
2630 memcpy(cmd_ctx
->llm
->payload
, domains
,
2631 nb_dom
* sizeof(struct lttng_domain
));
2638 case LTTNG_LIST_CHANNELS
:
2641 struct lttng_channel
*channels
;
2643 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
2644 cmd_ctx
->session
, &channels
);
2646 /* Return value is a negative lttng_error_code. */
2651 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
2656 /* Copy event list into message payload */
2657 memcpy(cmd_ctx
->llm
->payload
, channels
,
2658 nb_chan
* sizeof(struct lttng_channel
));
2665 case LTTNG_LIST_EVENTS
:
2668 struct lttng_event
*events
= NULL
;
2670 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
2671 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
2673 /* Return value is a negative lttng_error_code. */
2678 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
2683 /* Copy event list into message payload */
2684 memcpy(cmd_ctx
->llm
->payload
, events
,
2685 nb_event
* sizeof(struct lttng_event
));
2692 case LTTNG_LIST_SESSIONS
:
2694 unsigned int nr_sessions
;
2696 session_lock_list();
2697 nr_sessions
= lttng_sessions_count(
2698 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2699 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
2701 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
2703 session_unlock_list();
2707 /* Filled the session array */
2708 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
2709 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2710 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
2712 session_unlock_list();
2717 case LTTNG_CALIBRATE
:
2719 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
2720 &cmd_ctx
->lsm
->u
.calibrate
);
2723 case LTTNG_REGISTER_CONSUMER
:
2725 struct consumer_data
*cdata
;
2727 switch (cmd_ctx
->lsm
->domain
.type
) {
2728 case LTTNG_DOMAIN_KERNEL
:
2729 cdata
= &kconsumer_data
;
2732 ret
= LTTNG_ERR_UND
;
2736 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2737 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
2740 case LTTNG_SET_FILTER
:
2742 struct lttng_filter_bytecode
*bytecode
;
2744 if (cmd_ctx
->lsm
->u
.filter
.bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
2745 ret
= LTTNG_ERR_FILTER_INVAL
;
2748 bytecode
= zmalloc(cmd_ctx
->lsm
->u
.filter
.bytecode_len
);
2750 ret
= LTTNG_ERR_FILTER_NOMEM
;
2753 /* Receive var. len. data */
2754 DBG("Receiving var len data from client ...");
2755 ret
= lttcomm_recv_unix_sock(sock
, bytecode
,
2756 cmd_ctx
->lsm
->u
.filter
.bytecode_len
);
2758 DBG("Nothing recv() from client var len data... continuing");
2760 ret
= LTTNG_ERR_FILTER_INVAL
;
2764 if (bytecode
->len
+ sizeof(*bytecode
)
2765 != cmd_ctx
->lsm
->u
.filter
.bytecode_len
) {
2767 ret
= LTTNG_ERR_FILTER_INVAL
;
2771 ret
= cmd_set_filter(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2772 cmd_ctx
->lsm
->u
.filter
.channel_name
,
2773 cmd_ctx
->lsm
->u
.filter
.event_name
,
2778 ret
= LTTNG_ERR_UND
;
2783 if (cmd_ctx
->llm
== NULL
) {
2784 DBG("Missing llm structure. Allocating one.");
2785 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2789 /* Set return code */
2790 cmd_ctx
->llm
->ret_code
= ret
;
2792 if (cmd_ctx
->session
) {
2793 session_unlock(cmd_ctx
->session
);
2795 if (need_tracing_session
) {
2796 session_unlock_list();
2803 * Thread managing health check socket.
2805 static void *thread_manage_health(void *data
)
2807 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
2808 uint32_t revents
, nb_fd
;
2809 struct lttng_poll_event events
;
2810 struct lttcomm_health_msg msg
;
2811 struct lttcomm_health_data reply
;
2813 DBG("[thread] Manage health check started");
2815 rcu_register_thread();
2817 /* Create unix socket */
2818 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
2820 ERR("Unable to create health check Unix socket");
2825 ret
= lttcomm_listen_unix_sock(sock
);
2831 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2832 * more will be added to this poll set.
2834 ret
= create_thread_poll_set(&events
, 2);
2839 /* Add the application registration socket */
2840 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
2846 DBG("Health check ready");
2848 nb_fd
= LTTNG_POLL_GETNB(&events
);
2850 /* Inifinite blocking call, waiting for transmission */
2852 ret
= lttng_poll_wait(&events
, -1);
2855 * Restart interrupted system call.
2857 if (errno
== EINTR
) {
2863 for (i
= 0; i
< nb_fd
; i
++) {
2864 /* Fetch once the poll data */
2865 revents
= LTTNG_POLL_GETEV(&events
, i
);
2866 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2868 /* Thread quit pipe has been closed. Killing thread. */
2869 ret
= check_thread_quit_pipe(pollfd
, revents
);
2875 /* Event on the registration socket */
2876 if (pollfd
== sock
) {
2877 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2878 ERR("Health socket poll error");
2884 new_sock
= lttcomm_accept_unix_sock(sock
);
2889 DBG("Receiving data from client for health...");
2890 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
2892 DBG("Nothing recv() from client... continuing");
2893 ret
= close(new_sock
);
2901 rcu_thread_online();
2903 switch (msg
.component
) {
2904 case LTTNG_HEALTH_CMD
:
2905 reply
.ret_code
= health_check_state(&health_thread_cmd
);
2907 case LTTNG_HEALTH_APP_MANAGE
:
2908 reply
.ret_code
= health_check_state(&health_thread_app_manage
);
2910 case LTTNG_HEALTH_APP_REG
:
2911 reply
.ret_code
= health_check_state(&health_thread_app_reg
);
2913 case LTTNG_HEALTH_KERNEL
:
2914 reply
.ret_code
= health_check_state(&health_thread_kernel
);
2916 case LTTNG_HEALTH_CONSUMER
:
2917 reply
.ret_code
= check_consumer_health();
2919 case LTTNG_HEALTH_ALL
:
2921 health_check_state(&health_thread_app_manage
) &&
2922 health_check_state(&health_thread_app_reg
) &&
2923 health_check_state(&health_thread_cmd
) &&
2924 health_check_state(&health_thread_kernel
) &&
2925 check_consumer_health();
2928 reply
.ret_code
= LTTNG_ERR_UND
;
2933 * Flip ret value since 0 is a success and 1 indicates a bad health for
2934 * the client where in the sessiond it is the opposite. Again, this is
2935 * just to make things easier for us poor developer which enjoy a lot
2938 if (reply
.ret_code
== 0 || reply
.ret_code
== 1) {
2939 reply
.ret_code
= !reply
.ret_code
;
2942 DBG2("Health check return value %d", reply
.ret_code
);
2944 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
2946 ERR("Failed to send health data back to client");
2949 /* End of transmission */
2950 ret
= close(new_sock
);
2960 ERR("Health error occurred in %s", __func__
);
2962 DBG("Health check thread dying");
2963 unlink(health_unix_sock_path
);
2970 if (new_sock
>= 0) {
2971 ret
= close(new_sock
);
2977 lttng_poll_clean(&events
);
2979 rcu_unregister_thread();
2984 * This thread manage all clients request using the unix client socket for
2987 static void *thread_manage_clients(void *data
)
2989 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2991 uint32_t revents
, nb_fd
;
2992 struct command_ctx
*cmd_ctx
= NULL
;
2993 struct lttng_poll_event events
;
2995 DBG("[thread] Manage client started");
2997 rcu_register_thread();
2999 health_code_update(&health_thread_cmd
);
3001 ret
= lttcomm_listen_unix_sock(client_sock
);
3007 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3008 * more will be added to this poll set.
3010 ret
= create_thread_poll_set(&events
, 2);
3015 /* Add the application registration socket */
3016 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
3022 * Notify parent pid that we are ready to accept command for client side.
3024 if (opt_sig_parent
) {
3025 kill(ppid
, SIGUSR1
);
3028 health_code_update(&health_thread_cmd
);
3031 DBG("Accepting client command ...");
3033 nb_fd
= LTTNG_POLL_GETNB(&events
);
3035 /* Inifinite blocking call, waiting for transmission */
3037 health_poll_update(&health_thread_cmd
);
3038 ret
= lttng_poll_wait(&events
, -1);
3039 health_poll_update(&health_thread_cmd
);
3042 * Restart interrupted system call.
3044 if (errno
== EINTR
) {
3050 for (i
= 0; i
< nb_fd
; i
++) {
3051 /* Fetch once the poll data */
3052 revents
= LTTNG_POLL_GETEV(&events
, i
);
3053 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3055 health_code_update(&health_thread_cmd
);
3057 /* Thread quit pipe has been closed. Killing thread. */
3058 ret
= check_thread_quit_pipe(pollfd
, revents
);
3064 /* Event on the registration socket */
3065 if (pollfd
== client_sock
) {
3066 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3067 ERR("Client socket poll error");
3073 DBG("Wait for client response");
3075 health_code_update(&health_thread_cmd
);
3077 sock
= lttcomm_accept_unix_sock(client_sock
);
3082 /* Set socket option for credentials retrieval */
3083 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
3088 /* Allocate context command to process the client request */
3089 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
3090 if (cmd_ctx
== NULL
) {
3091 PERROR("zmalloc cmd_ctx");
3095 /* Allocate data buffer for reception */
3096 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
3097 if (cmd_ctx
->lsm
== NULL
) {
3098 PERROR("zmalloc cmd_ctx->lsm");
3102 cmd_ctx
->llm
= NULL
;
3103 cmd_ctx
->session
= NULL
;
3105 health_code_update(&health_thread_cmd
);
3108 * Data is received from the lttng client. The struct
3109 * lttcomm_session_msg (lsm) contains the command and data request of
3112 DBG("Receiving data from client ...");
3113 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
3114 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
3116 DBG("Nothing recv() from client... continuing");
3122 clean_command_ctx(&cmd_ctx
);
3126 health_code_update(&health_thread_cmd
);
3128 // TODO: Validate cmd_ctx including sanity check for
3129 // security purpose.
3131 rcu_thread_online();
3133 * This function dispatch the work to the kernel or userspace tracer
3134 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3135 * informations for the client. The command context struct contains
3136 * everything this function may needs.
3138 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
3139 rcu_thread_offline();
3149 * TODO: Inform client somehow of the fatal error. At
3150 * this point, ret < 0 means that a zmalloc failed
3151 * (ENOMEM). Error detected but still accept
3152 * command, unless a socket error has been
3155 clean_command_ctx(&cmd_ctx
);
3159 health_code_update(&health_thread_cmd
);
3161 DBG("Sending response (size: %d, retcode: %s)",
3162 cmd_ctx
->lttng_msg_size
,
3163 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
3164 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
3166 ERR("Failed to send data back to client");
3169 /* End of transmission */
3176 clean_command_ctx(&cmd_ctx
);
3178 health_code_update(&health_thread_cmd
);
3184 health_error(&health_thread_cmd
);
3185 ERR("Health error occurred in %s", __func__
);
3187 health_exit(&health_thread_cmd
);
3189 DBG("Client thread dying");
3190 unlink(client_unix_sock_path
);
3191 if (client_sock
>= 0) {
3192 ret
= close(client_sock
);
3204 lttng_poll_clean(&events
);
3205 clean_command_ctx(&cmd_ctx
);
3207 rcu_unregister_thread();
3213 * usage function on stderr
3215 static void usage(void)
3217 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
3218 fprintf(stderr
, " -h, --help Display this usage.\n");
3219 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
3220 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
3221 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
3222 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
3223 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
3224 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
3225 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
3226 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
3227 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
3228 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
3229 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
3230 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
3231 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
3232 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
3233 fprintf(stderr
, " -V, --version Show version number.\n");
3234 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
3235 fprintf(stderr
, " -q, --quiet No output at all.\n");
3236 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3237 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
3238 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
3242 * daemon argument parsing
3244 static int parse_args(int argc
, char **argv
)
3248 static struct option long_options
[] = {
3249 { "client-sock", 1, 0, 'c' },
3250 { "apps-sock", 1, 0, 'a' },
3251 { "kconsumerd-cmd-sock", 1, 0, 'C' },
3252 { "kconsumerd-err-sock", 1, 0, 'E' },
3253 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
3254 { "ustconsumerd32-err-sock", 1, 0, 'H' },
3255 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
3256 { "ustconsumerd64-err-sock", 1, 0, 'F' },
3257 { "consumerd32-path", 1, 0, 'u' },
3258 { "consumerd32-libdir", 1, 0, 'U' },
3259 { "consumerd64-path", 1, 0, 't' },
3260 { "consumerd64-libdir", 1, 0, 'T' },
3261 { "daemonize", 0, 0, 'd' },
3262 { "sig-parent", 0, 0, 'S' },
3263 { "help", 0, 0, 'h' },
3264 { "group", 1, 0, 'g' },
3265 { "version", 0, 0, 'V' },
3266 { "quiet", 0, 0, 'q' },
3267 { "verbose", 0, 0, 'v' },
3268 { "verbose-consumer", 0, 0, 'Z' },
3269 { "no-kernel", 0, 0, 'N' },
3274 int option_index
= 0;
3275 c
= getopt_long(argc
, argv
, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t",
3276 long_options
, &option_index
);
3283 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
3285 fprintf(stderr
, " with arg %s\n", optarg
);
3289 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3292 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3298 opt_tracing_group
= optarg
;
3304 fprintf(stdout
, "%s\n", VERSION
);
3310 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3313 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3316 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3319 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3322 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3325 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3331 lttng_opt_quiet
= 1;
3334 /* Verbose level can increase using multiple -v */
3335 lttng_opt_verbose
+= 1;
3338 opt_verbose_consumer
+= 1;
3341 consumerd32_bin
= optarg
;
3344 consumerd32_libdir
= optarg
;
3347 consumerd64_bin
= optarg
;
3350 consumerd64_libdir
= optarg
;
3353 /* Unknown option or other error.
3354 * Error is printed by getopt, just return */
3363 * Creates the two needed socket by the daemon.
3364 * apps_sock - The communication socket for all UST apps.
3365 * client_sock - The communication of the cli tool (lttng).
3367 static int init_daemon_socket(void)
3372 old_umask
= umask(0);
3374 /* Create client tool unix socket */
3375 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
3376 if (client_sock
< 0) {
3377 ERR("Create unix sock failed: %s", client_unix_sock_path
);
3382 /* File permission MUST be 660 */
3383 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3385 ERR("Set file permissions failed: %s", client_unix_sock_path
);
3390 /* Create the application unix socket */
3391 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
3392 if (apps_sock
< 0) {
3393 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
3398 /* File permission MUST be 666 */
3399 ret
= chmod(apps_unix_sock_path
,
3400 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
3402 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
3413 * Check if the global socket is available, and if a daemon is answering at the
3414 * other side. If yes, error is returned.
3416 static int check_existing_daemon(void)
3418 /* Is there anybody out there ? */
3419 if (lttng_session_daemon_alive()) {
3427 * Set the tracing group gid onto the client socket.
3429 * Race window between mkdir and chown is OK because we are going from more
3430 * permissive (root.root) to less permissive (root.tracing).
3432 static int set_permissions(char *rundir
)
3437 ret
= allowed_group();
3439 WARN("No tracing group detected");
3446 /* Set lttng run dir */
3447 ret
= chown(rundir
, 0, gid
);
3449 ERR("Unable to set group on %s", rundir
);
3453 /* Ensure tracing group can search the run dir */
3454 ret
= chmod(rundir
, S_IRWXU
| S_IXGRP
| S_IXOTH
);
3456 ERR("Unable to set permissions on %s", rundir
);
3460 /* lttng client socket path */
3461 ret
= chown(client_unix_sock_path
, 0, gid
);
3463 ERR("Unable to set group on %s", client_unix_sock_path
);
3467 /* kconsumer error socket path */
3468 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, gid
);
3470 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
3474 /* 64-bit ustconsumer error socket path */
3475 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, gid
);
3477 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
3481 /* 32-bit ustconsumer compat32 error socket path */
3482 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, gid
);
3484 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
3488 DBG("All permissions are set");
3495 * Create the lttng run directory needed for all global sockets and pipe.
3497 static int create_lttng_rundir(const char *rundir
)
3501 DBG3("Creating LTTng run directory: %s", rundir
);
3503 ret
= mkdir(rundir
, S_IRWXU
);
3505 if (errno
!= EEXIST
) {
3506 ERR("Unable to create %s", rundir
);
3518 * Setup sockets and directory needed by the kconsumerd communication with the
3521 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
3525 char path
[PATH_MAX
];
3527 switch (consumer_data
->type
) {
3528 case LTTNG_CONSUMER_KERNEL
:
3529 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
3531 case LTTNG_CONSUMER64_UST
:
3532 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
3534 case LTTNG_CONSUMER32_UST
:
3535 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
3538 ERR("Consumer type unknown");
3543 DBG2("Creating consumer directory: %s", path
);
3545 ret
= mkdir(path
, S_IRWXU
);
3547 if (errno
!= EEXIST
) {
3549 ERR("Failed to create %s", path
);
3555 /* Create the kconsumerd error unix socket */
3556 consumer_data
->err_sock
=
3557 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
3558 if (consumer_data
->err_sock
< 0) {
3559 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
3564 /* File permission MUST be 660 */
3565 ret
= chmod(consumer_data
->err_unix_sock_path
,
3566 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3568 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
3578 * Signal handler for the daemon
3580 * Simply stop all worker threads, leaving main() return gracefully after
3581 * joining all threads and calling cleanup().
3583 static void sighandler(int sig
)
3587 DBG("SIGPIPE caught");
3590 DBG("SIGINT caught");
3594 DBG("SIGTERM caught");
3603 * Setup signal handler for :
3604 * SIGINT, SIGTERM, SIGPIPE
3606 static int set_signal_handler(void)
3609 struct sigaction sa
;
3612 if ((ret
= sigemptyset(&sigset
)) < 0) {
3613 PERROR("sigemptyset");
3617 sa
.sa_handler
= sighandler
;
3618 sa
.sa_mask
= sigset
;
3620 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3621 PERROR("sigaction");
3625 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3626 PERROR("sigaction");
3630 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3631 PERROR("sigaction");
3635 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3641 * Set open files limit to unlimited. This daemon can open a large number of
3642 * file descriptors in order to consumer multiple kernel traces.
3644 static void set_ulimit(void)
3649 /* The kernel does not allowed an infinite limit for open files */
3650 lim
.rlim_cur
= 65535;
3651 lim
.rlim_max
= 65535;
3653 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3655 PERROR("failed to set open files limit");
3662 int main(int argc
, char **argv
)
3666 const char *home_path
;
3668 init_kernel_workarounds();
3670 rcu_register_thread();
3672 setup_consumerd_path();
3674 /* Parse arguments */
3676 if ((ret
= parse_args(argc
, argv
) < 0)) {
3686 * child: setsid, close FD 0, 1, 2, chdir /
3687 * parent: exit (if fork is successful)
3695 * We are in the child. Make sure all other file
3696 * descriptors are closed, in case we are called with
3697 * more opened file descriptors than the standard ones.
3699 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
3704 /* Create thread quit pipe */
3705 if ((ret
= init_thread_quit_pipe()) < 0) {
3709 /* Check if daemon is UID = 0 */
3710 is_root
= !getuid();
3713 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
3715 /* Create global run dir with root access */
3716 ret
= create_lttng_rundir(rundir
);
3721 if (strlen(apps_unix_sock_path
) == 0) {
3722 snprintf(apps_unix_sock_path
, PATH_MAX
,
3723 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
3726 if (strlen(client_unix_sock_path
) == 0) {
3727 snprintf(client_unix_sock_path
, PATH_MAX
,
3728 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
3731 /* Set global SHM for ust */
3732 if (strlen(wait_shm_path
) == 0) {
3733 snprintf(wait_shm_path
, PATH_MAX
,
3734 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
3737 if (strlen(health_unix_sock_path
) == 0) {
3738 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
3739 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
3742 /* Setup kernel consumerd path */
3743 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
3744 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
3745 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
3746 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
3748 DBG2("Kernel consumer err path: %s",
3749 kconsumer_data
.err_unix_sock_path
);
3750 DBG2("Kernel consumer cmd path: %s",
3751 kconsumer_data
.cmd_unix_sock_path
);
3753 home_path
= get_home_dir();
3754 if (home_path
== NULL
) {
3755 /* TODO: Add --socket PATH option */
3756 ERR("Can't get HOME directory for sockets creation.");
3762 * Create rundir from home path. This will create something like
3765 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
3771 ret
= create_lttng_rundir(rundir
);
3776 if (strlen(apps_unix_sock_path
) == 0) {
3777 snprintf(apps_unix_sock_path
, PATH_MAX
,
3778 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
3781 /* Set the cli tool unix socket path */
3782 if (strlen(client_unix_sock_path
) == 0) {
3783 snprintf(client_unix_sock_path
, PATH_MAX
,
3784 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
3787 /* Set global SHM for ust */
3788 if (strlen(wait_shm_path
) == 0) {
3789 snprintf(wait_shm_path
, PATH_MAX
,
3790 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, geteuid());
3793 /* Set health check Unix path */
3794 if (strlen(health_unix_sock_path
) == 0) {
3795 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
3796 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
3800 /* Set consumer initial state */
3801 kernel_consumerd_state
= CONSUMER_STOPPED
;
3802 ust_consumerd_state
= CONSUMER_STOPPED
;
3804 DBG("Client socket path %s", client_unix_sock_path
);
3805 DBG("Application socket path %s", apps_unix_sock_path
);
3806 DBG("LTTng run directory path: %s", rundir
);
3808 /* 32 bits consumerd path setup */
3809 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
3810 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
3811 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
3812 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
3814 DBG2("UST consumer 32 bits err path: %s",
3815 ustconsumer32_data
.err_unix_sock_path
);
3816 DBG2("UST consumer 32 bits cmd path: %s",
3817 ustconsumer32_data
.cmd_unix_sock_path
);
3819 /* 64 bits consumerd path setup */
3820 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
3821 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
3822 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
3823 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
3825 DBG2("UST consumer 64 bits err path: %s",
3826 ustconsumer64_data
.err_unix_sock_path
);
3827 DBG2("UST consumer 64 bits cmd path: %s",
3828 ustconsumer64_data
.cmd_unix_sock_path
);
3831 * See if daemon already exist.
3833 if ((ret
= check_existing_daemon()) < 0) {
3834 ERR("Already running daemon.\n");
3836 * We do not goto exit because we must not cleanup()
3837 * because a daemon is already running.
3843 * Init UST app hash table. Alloc hash table before this point since
3844 * cleanup() can get called after that point.
3848 /* After this point, we can safely call cleanup() with "goto exit" */
3851 * These actions must be executed as root. We do that *after* setting up
3852 * the sockets path because we MUST make the check for another daemon using
3853 * those paths *before* trying to set the kernel consumer sockets and init
3857 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
3862 /* Setup kernel tracer */
3863 if (!opt_no_kernel
) {
3864 init_kernel_tracer();
3867 /* Set ulimit for open files */
3870 /* init lttng_fd tracking must be done after set_ulimit. */
3873 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
3878 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
3883 if ((ret
= set_signal_handler()) < 0) {
3887 /* Setup the needed unix socket */
3888 if ((ret
= init_daemon_socket()) < 0) {
3892 /* Set credentials to socket */
3893 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
3897 /* Get parent pid if -S, --sig-parent is specified. */
3898 if (opt_sig_parent
) {
3902 /* Setup the kernel pipe for waking up the kernel thread */
3903 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
3907 /* Setup the thread apps communication pipe. */
3908 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
3912 /* Init UST command queue. */
3913 cds_wfq_init(&ust_cmd_queue
.queue
);
3916 * Get session list pointer. This pointer MUST NOT be free(). This list is
3917 * statically declared in session.c
3919 session_list_ptr
= session_get_list();
3921 /* Set up max poll set size */
3922 lttng_poll_set_max_size();
3926 /* Init all health thread counters. */
3927 health_init(&health_thread_cmd
);
3928 health_init(&health_thread_kernel
);
3929 health_init(&health_thread_app_manage
);
3930 health_init(&health_thread_app_reg
);
3933 * Init health counters of the consumer thread. We do a quick hack here to
3934 * the state of the consumer health is fine even if the thread is not
3935 * started. This is simply to ease our life and has no cost what so ever.
3937 health_init(&kconsumer_data
.health
);
3938 health_poll_update(&kconsumer_data
.health
);
3939 health_init(&ustconsumer32_data
.health
);
3940 health_poll_update(&ustconsumer32_data
.health
);
3941 health_init(&ustconsumer64_data
.health
);
3942 health_poll_update(&ustconsumer64_data
.health
);
3944 /* Create thread to manage the client socket */
3945 ret
= pthread_create(&health_thread
, NULL
,
3946 thread_manage_health
, (void *) NULL
);
3948 PERROR("pthread_create health");
3952 /* Create thread to manage the client socket */
3953 ret
= pthread_create(&client_thread
, NULL
,
3954 thread_manage_clients
, (void *) NULL
);
3956 PERROR("pthread_create clients");
3960 /* Create thread to dispatch registration */
3961 ret
= pthread_create(&dispatch_thread
, NULL
,
3962 thread_dispatch_ust_registration
, (void *) NULL
);
3964 PERROR("pthread_create dispatch");
3968 /* Create thread to manage application registration. */
3969 ret
= pthread_create(®_apps_thread
, NULL
,
3970 thread_registration_apps
, (void *) NULL
);
3972 PERROR("pthread_create registration");
3976 /* Create thread to manage application socket */
3977 ret
= pthread_create(&apps_thread
, NULL
,
3978 thread_manage_apps
, (void *) NULL
);
3980 PERROR("pthread_create apps");
3984 /* Create kernel thread to manage kernel event */
3985 ret
= pthread_create(&kernel_thread
, NULL
,
3986 thread_manage_kernel
, (void *) NULL
);
3988 PERROR("pthread_create kernel");
3992 ret
= pthread_join(kernel_thread
, &status
);
3994 PERROR("pthread_join");
3995 goto error
; /* join error, exit without cleanup */
3999 ret
= pthread_join(apps_thread
, &status
);
4001 PERROR("pthread_join");
4002 goto error
; /* join error, exit without cleanup */
4006 ret
= pthread_join(reg_apps_thread
, &status
);
4008 PERROR("pthread_join");
4009 goto error
; /* join error, exit without cleanup */
4013 ret
= pthread_join(dispatch_thread
, &status
);
4015 PERROR("pthread_join");
4016 goto error
; /* join error, exit without cleanup */
4020 ret
= pthread_join(client_thread
, &status
);
4022 PERROR("pthread_join");
4023 goto error
; /* join error, exit without cleanup */
4026 ret
= join_consumer_thread(&kconsumer_data
);
4028 PERROR("join_consumer");
4029 goto error
; /* join error, exit without cleanup */
4036 * cleanup() is called when no other thread is running.
4038 rcu_thread_online();
4040 rcu_thread_offline();
4041 rcu_unregister_thread();