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.
24 #include <semaphore.h>
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/poll.h>
42 #include <common/compat/socket.h>
43 #include <common/defaults.h>
44 #include <common/kernel-consumer/kernel-consumer.h>
45 #include <common/futex.h>
46 #include <common/relayd/relayd.h>
47 #include <common/utils.h>
49 #include "lttng-sessiond.h"
56 #include "kernel-consumer.h"
60 #include "ust-consumer.h"
66 #define CONSUMERD_FILE "lttng-consumerd"
69 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
70 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
71 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
72 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
75 const char *opt_tracing_group
;
76 static int opt_sig_parent
;
77 static int opt_verbose_consumer
;
78 static int opt_daemon
;
79 static int opt_no_kernel
;
80 static int is_root
; /* Set to 1 if the daemon is running as root */
81 static pid_t ppid
; /* Parent PID for --sig-parent option */
84 /* Consumer daemon specific control data */
85 static struct consumer_data kconsumer_data
= {
86 .type
= LTTNG_CONSUMER_KERNEL
,
87 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
88 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
91 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
92 .lock
= PTHREAD_MUTEX_INITIALIZER
,
94 static struct consumer_data ustconsumer64_data
= {
95 .type
= LTTNG_CONSUMER64_UST
,
96 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
97 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
100 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
101 .lock
= PTHREAD_MUTEX_INITIALIZER
,
103 static struct consumer_data ustconsumer32_data
= {
104 .type
= LTTNG_CONSUMER32_UST
,
105 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
106 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
109 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
110 .lock
= PTHREAD_MUTEX_INITIALIZER
,
113 /* Shared between threads */
114 static int dispatch_thread_exit
;
116 /* Global application Unix socket path */
117 static char apps_unix_sock_path
[PATH_MAX
];
118 /* Global client Unix socket path */
119 static char client_unix_sock_path
[PATH_MAX
];
120 /* global wait shm path for UST */
121 static char wait_shm_path
[PATH_MAX
];
122 /* Global health check unix path */
123 static char health_unix_sock_path
[PATH_MAX
];
125 /* Sockets and FDs */
126 static int client_sock
= -1;
127 static int apps_sock
= -1;
128 int kernel_tracer_fd
= -1;
129 static int kernel_poll_pipe
[2] = { -1, -1 };
132 * Quit pipe for all threads. This permits a single cancellation point
133 * for all threads when receiving an event on the pipe.
135 static int thread_quit_pipe
[2] = { -1, -1 };
138 * This pipe is used to inform the thread managing application communication
139 * that a command is queued and ready to be processed.
141 static int apps_cmd_pipe
[2] = { -1, -1 };
143 /* Pthread, Mutexes and Semaphores */
144 static pthread_t apps_thread
;
145 static pthread_t reg_apps_thread
;
146 static pthread_t client_thread
;
147 static pthread_t kernel_thread
;
148 static pthread_t dispatch_thread
;
149 static pthread_t health_thread
;
152 * UST registration command queue. This queue is tied with a futex and uses a N
153 * wakers / 1 waiter implemented and detailed in futex.c/.h
155 * The thread_manage_apps and thread_dispatch_ust_registration interact with
156 * this queue and the wait/wake scheme.
158 static struct ust_cmd_queue ust_cmd_queue
;
161 * Pointer initialized before thread creation.
163 * This points to the tracing session list containing the session count and a
164 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
165 * MUST NOT be taken if you call a public function in session.c.
167 * The lock is nested inside the structure: session_list_ptr->lock. Please use
168 * session_lock_list and session_unlock_list for lock acquisition.
170 static struct ltt_session_list
*session_list_ptr
;
172 int ust_consumerd64_fd
= -1;
173 int ust_consumerd32_fd
= -1;
175 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
176 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
177 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
178 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
180 static const char *module_proc_lttng
= "/proc/lttng";
183 * Consumer daemon state which is changed when spawning it, killing it or in
184 * case of a fatal error.
186 enum consumerd_state
{
187 CONSUMER_STARTED
= 1,
188 CONSUMER_STOPPED
= 2,
193 * This consumer daemon state is used to validate if a client command will be
194 * able to reach the consumer. If not, the client is informed. For instance,
195 * doing a "lttng start" when the consumer state is set to ERROR will return an
196 * error to the client.
198 * The following example shows a possible race condition of this scheme:
200 * consumer thread error happens
202 * client cmd checks state -> still OK
203 * consumer thread exit, sets error
204 * client cmd try to talk to consumer
207 * However, since the consumer is a different daemon, we have no way of making
208 * sure the command will reach it safely even with this state flag. This is why
209 * we consider that up to the state validation during command processing, the
210 * command is safe. After that, we can not guarantee the correctness of the
211 * client request vis-a-vis the consumer.
213 static enum consumerd_state ust_consumerd_state
;
214 static enum consumerd_state kernel_consumerd_state
;
216 /* Used for the health monitoring of the session daemon. See health.h */
217 struct health_state health_thread_cmd
;
218 struct health_state health_thread_app_manage
;
219 struct health_state health_thread_app_reg
;
220 struct health_state health_thread_kernel
;
223 void setup_consumerd_path(void)
225 const char *bin
, *libdir
;
228 * Allow INSTALL_BIN_PATH to be used as a target path for the
229 * native architecture size consumer if CONFIG_CONSUMER*_PATH
230 * has not been defined.
232 #if (CAA_BITS_PER_LONG == 32)
233 if (!consumerd32_bin
[0]) {
234 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
236 if (!consumerd32_libdir
[0]) {
237 consumerd32_libdir
= INSTALL_LIB_PATH
;
239 #elif (CAA_BITS_PER_LONG == 64)
240 if (!consumerd64_bin
[0]) {
241 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
243 if (!consumerd64_libdir
[0]) {
244 consumerd64_libdir
= INSTALL_LIB_PATH
;
247 #error "Unknown bitness"
251 * runtime env. var. overrides the build default.
253 bin
= getenv("LTTNG_CONSUMERD32_BIN");
255 consumerd32_bin
= bin
;
257 bin
= getenv("LTTNG_CONSUMERD64_BIN");
259 consumerd64_bin
= bin
;
261 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
263 consumerd32_libdir
= libdir
;
265 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
267 consumerd64_libdir
= libdir
;
272 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
274 static int create_thread_poll_set(struct lttng_poll_event
*events
,
279 if (events
== NULL
|| size
== 0) {
284 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
290 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
302 * Check if the thread quit pipe was triggered.
304 * Return 1 if it was triggered else 0;
306 static int check_thread_quit_pipe(int fd
, uint32_t events
)
308 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
316 * Return group ID of the tracing group or -1 if not found.
318 static gid_t
allowed_group(void)
322 if (opt_tracing_group
) {
323 grp
= getgrnam(opt_tracing_group
);
325 grp
= getgrnam(default_tracing_group
);
335 * Init thread quit pipe.
337 * Return -1 on error or 0 if all pipes are created.
339 static int init_thread_quit_pipe(void)
343 ret
= pipe(thread_quit_pipe
);
345 PERROR("thread quit pipe");
349 for (i
= 0; i
< 2; i
++) {
350 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
362 * Stop all threads by closing the thread quit pipe.
364 static void stop_threads(void)
368 /* Stopping all threads */
369 DBG("Terminating all threads");
370 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
372 ERR("write error on thread quit pipe");
375 /* Dispatch thread */
376 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
377 futex_nto1_wake(&ust_cmd_queue
.futex
);
383 static void cleanup(void)
387 struct ltt_session
*sess
, *stmp
;
391 /* First thing first, stop all threads */
392 utils_close_pipe(thread_quit_pipe
);
394 DBG("Removing %s directory", rundir
);
395 ret
= asprintf(&cmd
, "rm -rf %s", rundir
);
397 ERR("asprintf failed. Something is really wrong!");
400 /* Remove lttng run directory */
403 ERR("Unable to clean %s", rundir
);
407 DBG("Cleaning up all sessions");
409 /* Destroy session list mutex */
410 if (session_list_ptr
!= NULL
) {
411 pthread_mutex_destroy(&session_list_ptr
->lock
);
413 /* Cleanup ALL session */
414 cds_list_for_each_entry_safe(sess
, stmp
,
415 &session_list_ptr
->head
, list
) {
416 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
420 DBG("Closing all UST sockets");
421 ust_app_clean_list();
423 if (is_root
&& !opt_no_kernel
) {
424 DBG2("Closing kernel fd");
425 if (kernel_tracer_fd
>= 0) {
426 ret
= close(kernel_tracer_fd
);
431 DBG("Unloading kernel modules");
432 modprobe_remove_lttng_all();
435 utils_close_pipe(kernel_poll_pipe
);
436 utils_close_pipe(apps_cmd_pipe
);
439 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
440 "Matthew, BEET driven development works!%c[%dm",
441 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
446 * Send data on a unix socket using the liblttsessiondcomm API.
448 * Return lttcomm error code.
450 static int send_unix_sock(int sock
, void *buf
, size_t len
)
452 /* Check valid length */
457 return lttcomm_send_unix_sock(sock
, buf
, len
);
461 * Free memory of a command context structure.
463 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
465 DBG("Clean command context structure");
467 if ((*cmd_ctx
)->llm
) {
468 free((*cmd_ctx
)->llm
);
470 if ((*cmd_ctx
)->lsm
) {
471 free((*cmd_ctx
)->lsm
);
479 * Notify UST applications using the shm mmap futex.
481 static int notify_ust_apps(int active
)
485 DBG("Notifying applications of session daemon state: %d", active
);
487 /* See shm.c for this call implying mmap, shm and futex calls */
488 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
489 if (wait_shm_mmap
== NULL
) {
493 /* Wake waiting process */
494 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
496 /* Apps notified successfully */
504 * Setup the outgoing data buffer for the response (llm) by allocating the
505 * right amount of memory and copying the original information from the lsm
508 * Return total size of the buffer pointed by buf.
510 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
516 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
517 if (cmd_ctx
->llm
== NULL
) {
523 /* Copy common data */
524 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
525 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
527 cmd_ctx
->llm
->data_size
= size
;
528 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
537 * Update the kernel poll set of all channel fd available over all tracing
538 * session. Add the wakeup pipe at the end of the set.
540 static int update_kernel_poll(struct lttng_poll_event
*events
)
543 struct ltt_session
*session
;
544 struct ltt_kernel_channel
*channel
;
546 DBG("Updating kernel poll set");
549 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
550 session_lock(session
);
551 if (session
->kernel_session
== NULL
) {
552 session_unlock(session
);
556 cds_list_for_each_entry(channel
,
557 &session
->kernel_session
->channel_list
.head
, list
) {
558 /* Add channel fd to the kernel poll set */
559 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
561 session_unlock(session
);
564 DBG("Channel fd %d added to kernel set", channel
->fd
);
566 session_unlock(session
);
568 session_unlock_list();
573 session_unlock_list();
578 * Find the channel fd from 'fd' over all tracing session. When found, check
579 * for new channel stream and send those stream fds to the kernel consumer.
581 * Useful for CPU hotplug feature.
583 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
586 struct ltt_session
*session
;
587 struct ltt_kernel_session
*ksess
;
588 struct ltt_kernel_channel
*channel
;
590 DBG("Updating kernel streams for channel fd %d", fd
);
593 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
594 session_lock(session
);
595 if (session
->kernel_session
== NULL
) {
596 session_unlock(session
);
599 ksess
= session
->kernel_session
;
601 cds_list_for_each_entry(channel
, &ksess
->channel_list
.head
, list
) {
602 if (channel
->fd
== fd
) {
603 DBG("Channel found, updating kernel streams");
604 ret
= kernel_open_channel_stream(channel
);
610 * Have we already sent fds to the consumer? If yes, it means
611 * that tracing is started so it is safe to send our updated
614 if (ksess
->consumer_fds_sent
== 1 && ksess
->consumer
!= NULL
) {
615 struct lttng_ht_iter iter
;
616 struct consumer_socket
*socket
;
619 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
620 &iter
.iter
, socket
, node
.node
) {
621 /* Code flow error */
622 assert(socket
->fd
>= 0);
624 pthread_mutex_lock(socket
->lock
);
625 ret
= kernel_consumer_send_channel_stream(socket
->fd
,
627 pthread_mutex_unlock(socket
->lock
);
636 session_unlock(session
);
638 session_unlock_list();
642 session_unlock(session
);
643 session_unlock_list();
648 * For each tracing session, update newly registered apps.
650 static void update_ust_app(int app_sock
)
652 struct ltt_session
*sess
, *stmp
;
656 /* For all tracing session(s) */
657 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
659 if (sess
->ust_session
) {
660 ust_app_global_update(sess
->ust_session
, app_sock
);
662 session_unlock(sess
);
665 session_unlock_list();
669 * This thread manage event coming from the kernel.
671 * Features supported in this thread:
674 static void *thread_manage_kernel(void *data
)
676 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
677 uint32_t revents
, nb_fd
;
679 struct lttng_poll_event events
;
681 DBG("Thread manage kernel started");
683 health_code_update(&health_thread_kernel
);
685 ret
= create_thread_poll_set(&events
, 2);
687 goto error_poll_create
;
690 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
696 health_code_update(&health_thread_kernel
);
698 if (update_poll_flag
== 1) {
700 * Reset number of fd in the poll set. Always 2 since there is the thread
701 * quit pipe and the kernel pipe.
705 ret
= update_kernel_poll(&events
);
709 update_poll_flag
= 0;
712 nb_fd
= LTTNG_POLL_GETNB(&events
);
714 DBG("Thread kernel polling on %d fds", nb_fd
);
716 /* Zeroed the poll events */
717 lttng_poll_reset(&events
);
719 /* Poll infinite value of time */
721 health_poll_update(&health_thread_kernel
);
722 ret
= lttng_poll_wait(&events
, -1);
723 health_poll_update(&health_thread_kernel
);
726 * Restart interrupted system call.
728 if (errno
== EINTR
) {
732 } else if (ret
== 0) {
733 /* Should not happen since timeout is infinite */
734 ERR("Return value of poll is 0 with an infinite timeout.\n"
735 "This should not have happened! Continuing...");
739 for (i
= 0; i
< nb_fd
; i
++) {
740 /* Fetch once the poll data */
741 revents
= LTTNG_POLL_GETEV(&events
, i
);
742 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
744 health_code_update(&health_thread_kernel
);
746 /* Thread quit pipe has been closed. Killing thread. */
747 ret
= check_thread_quit_pipe(pollfd
, revents
);
753 /* Check for data on kernel pipe */
754 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
755 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
756 update_poll_flag
= 1;
760 * New CPU detected by the kernel. Adding kernel stream to
761 * kernel session and updating the kernel consumer
763 if (revents
& LPOLLIN
) {
764 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
770 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
771 * and unregister kernel stream at this point.
780 lttng_poll_clean(&events
);
783 health_error(&health_thread_kernel
);
784 ERR("Health error occurred in %s", __func__
);
786 health_exit(&health_thread_kernel
);
787 DBG("Kernel thread dying");
792 * This thread manage the consumer error sent back to the session daemon.
794 static void *thread_manage_consumer(void *data
)
796 int sock
= -1, i
, ret
, pollfd
, err
= -1;
797 uint32_t revents
, nb_fd
;
798 enum lttcomm_return_code code
;
799 struct lttng_poll_event events
;
800 struct consumer_data
*consumer_data
= data
;
802 DBG("[thread] Manage consumer started");
804 health_code_update(&consumer_data
->health
);
806 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
812 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
813 * Nothing more will be added to this poll set.
815 ret
= create_thread_poll_set(&events
, 2);
820 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
825 nb_fd
= LTTNG_POLL_GETNB(&events
);
827 health_code_update(&consumer_data
->health
);
829 /* Inifinite blocking call, waiting for transmission */
831 health_poll_update(&consumer_data
->health
);
832 ret
= lttng_poll_wait(&events
, -1);
833 health_poll_update(&consumer_data
->health
);
836 * Restart interrupted system call.
838 if (errno
== EINTR
) {
844 for (i
= 0; i
< nb_fd
; i
++) {
845 /* Fetch once the poll data */
846 revents
= LTTNG_POLL_GETEV(&events
, i
);
847 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
849 health_code_update(&consumer_data
->health
);
851 /* Thread quit pipe has been closed. Killing thread. */
852 ret
= check_thread_quit_pipe(pollfd
, revents
);
858 /* Event on the registration socket */
859 if (pollfd
== consumer_data
->err_sock
) {
860 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
861 ERR("consumer err socket poll error");
867 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
872 health_code_update(&consumer_data
->health
);
874 DBG2("Receiving code from consumer err_sock");
876 /* Getting status code from kconsumerd */
877 ret
= lttcomm_recv_unix_sock(sock
, &code
,
878 sizeof(enum lttcomm_return_code
));
883 health_code_update(&consumer_data
->health
);
885 if (code
== CONSUMERD_COMMAND_SOCK_READY
) {
886 consumer_data
->cmd_sock
=
887 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
888 if (consumer_data
->cmd_sock
< 0) {
889 sem_post(&consumer_data
->sem
);
890 PERROR("consumer connect");
893 /* Signal condition to tell that the kconsumerd is ready */
894 sem_post(&consumer_data
->sem
);
895 DBG("consumer command socket ready");
897 ERR("consumer error when waiting for SOCK_READY : %s",
898 lttcomm_get_readable_code(-code
));
902 /* Remove the kconsumerd error sock since we've established a connexion */
903 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
908 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
913 health_code_update(&consumer_data
->health
);
915 /* Update number of fd */
916 nb_fd
= LTTNG_POLL_GETNB(&events
);
918 /* Inifinite blocking call, waiting for transmission */
920 health_poll_update(&consumer_data
->health
);
921 ret
= lttng_poll_wait(&events
, -1);
922 health_poll_update(&consumer_data
->health
);
925 * Restart interrupted system call.
927 if (errno
== EINTR
) {
933 for (i
= 0; i
< nb_fd
; i
++) {
934 /* Fetch once the poll data */
935 revents
= LTTNG_POLL_GETEV(&events
, i
);
936 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
938 health_code_update(&consumer_data
->health
);
940 /* Thread quit pipe has been closed. Killing thread. */
941 ret
= check_thread_quit_pipe(pollfd
, revents
);
947 /* Event on the kconsumerd socket */
948 if (pollfd
== sock
) {
949 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
950 ERR("consumer err socket second poll error");
956 health_code_update(&consumer_data
->health
);
958 /* Wait for any kconsumerd error */
959 ret
= lttcomm_recv_unix_sock(sock
, &code
,
960 sizeof(enum lttcomm_return_code
));
962 ERR("consumer closed the command socket");
966 ERR("consumer return code : %s", lttcomm_get_readable_code(-code
));
970 /* Immediately set the consumerd state to stopped */
971 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
972 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
973 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
974 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
975 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
977 /* Code flow error... */
981 if (consumer_data
->err_sock
>= 0) {
982 ret
= close(consumer_data
->err_sock
);
987 if (consumer_data
->cmd_sock
>= 0) {
988 ret
= close(consumer_data
->cmd_sock
);
1000 unlink(consumer_data
->err_unix_sock_path
);
1001 unlink(consumer_data
->cmd_unix_sock_path
);
1002 consumer_data
->pid
= 0;
1004 lttng_poll_clean(&events
);
1008 health_error(&consumer_data
->health
);
1009 ERR("Health error occurred in %s", __func__
);
1011 health_exit(&consumer_data
->health
);
1012 DBG("consumer thread cleanup completed");
1018 * This thread manage application communication.
1020 static void *thread_manage_apps(void *data
)
1022 int i
, ret
, pollfd
, err
= -1;
1023 uint32_t revents
, nb_fd
;
1024 struct ust_command ust_cmd
;
1025 struct lttng_poll_event events
;
1027 DBG("[thread] Manage application started");
1029 rcu_register_thread();
1030 rcu_thread_online();
1032 health_code_update(&health_thread_app_manage
);
1034 ret
= create_thread_poll_set(&events
, 2);
1036 goto error_poll_create
;
1039 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1044 health_code_update(&health_thread_app_manage
);
1047 /* Zeroed the events structure */
1048 lttng_poll_reset(&events
);
1050 nb_fd
= LTTNG_POLL_GETNB(&events
);
1052 DBG("Apps thread polling on %d fds", nb_fd
);
1054 /* Inifinite blocking call, waiting for transmission */
1056 health_poll_update(&health_thread_app_manage
);
1057 ret
= lttng_poll_wait(&events
, -1);
1058 health_poll_update(&health_thread_app_manage
);
1061 * Restart interrupted system call.
1063 if (errno
== EINTR
) {
1069 for (i
= 0; i
< nb_fd
; i
++) {
1070 /* Fetch once the poll data */
1071 revents
= LTTNG_POLL_GETEV(&events
, i
);
1072 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1074 health_code_update(&health_thread_app_manage
);
1076 /* Thread quit pipe has been closed. Killing thread. */
1077 ret
= check_thread_quit_pipe(pollfd
, revents
);
1083 /* Inspect the apps cmd pipe */
1084 if (pollfd
== apps_cmd_pipe
[0]) {
1085 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1086 ERR("Apps command pipe error");
1088 } else if (revents
& LPOLLIN
) {
1090 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
1091 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
1092 PERROR("read apps cmd pipe");
1096 health_code_update(&health_thread_app_manage
);
1098 /* Register applicaton to the session daemon */
1099 ret
= ust_app_register(&ust_cmd
.reg_msg
,
1101 if (ret
== -ENOMEM
) {
1103 } else if (ret
< 0) {
1107 health_code_update(&health_thread_app_manage
);
1110 * Validate UST version compatibility.
1112 ret
= ust_app_validate_version(ust_cmd
.sock
);
1115 * Add channel(s) and event(s) to newly registered apps
1116 * from lttng global UST domain.
1118 update_ust_app(ust_cmd
.sock
);
1121 health_code_update(&health_thread_app_manage
);
1123 ret
= ust_app_register_done(ust_cmd
.sock
);
1126 * If the registration is not possible, we simply
1127 * unregister the apps and continue
1129 ust_app_unregister(ust_cmd
.sock
);
1132 * We just need here to monitor the close of the UST
1133 * socket and poll set monitor those by default.
1134 * Listen on POLLIN (even if we never expect any
1135 * data) to ensure that hangup wakes us.
1137 ret
= lttng_poll_add(&events
, ust_cmd
.sock
, LPOLLIN
);
1142 DBG("Apps with sock %d added to poll set",
1146 health_code_update(&health_thread_app_manage
);
1152 * At this point, we know that a registered application made
1153 * the event at poll_wait.
1155 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1156 /* Removing from the poll set */
1157 ret
= lttng_poll_del(&events
, pollfd
);
1162 /* Socket closed on remote end. */
1163 ust_app_unregister(pollfd
);
1168 health_code_update(&health_thread_app_manage
);
1174 lttng_poll_clean(&events
);
1177 health_error(&health_thread_app_manage
);
1178 ERR("Health error occurred in %s", __func__
);
1180 health_exit(&health_thread_app_manage
);
1181 DBG("Application communication apps thread cleanup complete");
1182 rcu_thread_offline();
1183 rcu_unregister_thread();
1188 * Dispatch request from the registration threads to the application
1189 * communication thread.
1191 static void *thread_dispatch_ust_registration(void *data
)
1194 struct cds_wfq_node
*node
;
1195 struct ust_command
*ust_cmd
= NULL
;
1197 DBG("[thread] Dispatch UST command started");
1199 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1200 /* Atomically prepare the queue futex */
1201 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1204 /* Dequeue command for registration */
1205 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1207 DBG("Woken up but nothing in the UST command queue");
1208 /* Continue thread execution */
1212 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1214 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1215 " gid:%d sock:%d name:%s (version %d.%d)",
1216 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1217 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1218 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1219 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1221 * Inform apps thread of the new application registration. This
1222 * call is blocking so we can be assured that the data will be read
1223 * at some point in time or wait to the end of the world :)
1225 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1226 sizeof(struct ust_command
));
1228 PERROR("write apps cmd pipe");
1229 if (errno
== EBADF
) {
1231 * We can't inform the application thread to process
1232 * registration. We will exit or else application
1233 * registration will not occur and tracing will never
1240 } while (node
!= NULL
);
1242 /* Futex wait on queue. Blocking call on futex() */
1243 futex_nto1_wait(&ust_cmd_queue
.futex
);
1247 DBG("Dispatch thread dying");
1252 * This thread manage application registration.
1254 static void *thread_registration_apps(void *data
)
1256 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1257 uint32_t revents
, nb_fd
;
1258 struct lttng_poll_event events
;
1260 * Get allocated in this thread, enqueued to a global queue, dequeued and
1261 * freed in the manage apps thread.
1263 struct ust_command
*ust_cmd
= NULL
;
1265 DBG("[thread] Manage application registration started");
1267 ret
= lttcomm_listen_unix_sock(apps_sock
);
1273 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1274 * more will be added to this poll set.
1276 ret
= create_thread_poll_set(&events
, 2);
1278 goto error_create_poll
;
1281 /* Add the application registration socket */
1282 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1284 goto error_poll_add
;
1287 /* Notify all applications to register */
1288 ret
= notify_ust_apps(1);
1290 ERR("Failed to notify applications or create the wait shared memory.\n"
1291 "Execution continues but there might be problem for already\n"
1292 "running applications that wishes to register.");
1296 DBG("Accepting application registration");
1298 nb_fd
= LTTNG_POLL_GETNB(&events
);
1300 /* Inifinite blocking call, waiting for transmission */
1302 health_poll_update(&health_thread_app_reg
);
1303 ret
= lttng_poll_wait(&events
, -1);
1304 health_poll_update(&health_thread_app_reg
);
1307 * Restart interrupted system call.
1309 if (errno
== EINTR
) {
1315 for (i
= 0; i
< nb_fd
; i
++) {
1316 health_code_update(&health_thread_app_reg
);
1318 /* Fetch once the poll data */
1319 revents
= LTTNG_POLL_GETEV(&events
, i
);
1320 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1322 /* Thread quit pipe has been closed. Killing thread. */
1323 ret
= check_thread_quit_pipe(pollfd
, revents
);
1329 /* Event on the registration socket */
1330 if (pollfd
== apps_sock
) {
1331 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1332 ERR("Register apps socket poll error");
1334 } else if (revents
& LPOLLIN
) {
1335 sock
= lttcomm_accept_unix_sock(apps_sock
);
1340 /* Create UST registration command for enqueuing */
1341 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1342 if (ust_cmd
== NULL
) {
1343 PERROR("ust command zmalloc");
1348 * Using message-based transmissions to ensure we don't
1349 * have to deal with partially received messages.
1351 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1353 ERR("Exhausted file descriptors allowed for applications.");
1362 health_code_update(&health_thread_app_reg
);
1363 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1364 sizeof(struct ust_register_msg
));
1365 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1367 PERROR("lttcomm_recv_unix_sock register apps");
1369 ERR("Wrong size received on apps register");
1376 lttng_fd_put(LTTNG_FD_APPS
, 1);
1380 health_code_update(&health_thread_app_reg
);
1382 ust_cmd
->sock
= sock
;
1385 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1386 " gid:%d sock:%d name:%s (version %d.%d)",
1387 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1388 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1389 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1390 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1393 * Lock free enqueue the registration request. The red pill
1394 * has been taken! This apps will be part of the *system*.
1396 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1399 * Wake the registration queue futex. Implicit memory
1400 * barrier with the exchange in cds_wfq_enqueue.
1402 futex_nto1_wake(&ust_cmd_queue
.futex
);
1411 health_error(&health_thread_app_reg
);
1412 ERR("Health error occurred in %s", __func__
);
1414 health_exit(&health_thread_app_reg
);
1416 /* Notify that the registration thread is gone */
1419 if (apps_sock
>= 0) {
1420 ret
= close(apps_sock
);
1430 lttng_fd_put(LTTNG_FD_APPS
, 1);
1432 unlink(apps_unix_sock_path
);
1435 lttng_poll_clean(&events
);
1438 DBG("UST Registration thread cleanup complete");
1444 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1445 * exec or it will fails.
1447 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
1450 struct timespec timeout
;
1452 timeout
.tv_sec
= DEFAULT_SEM_WAIT_TIMEOUT
;
1453 timeout
.tv_nsec
= 0;
1455 /* Setup semaphore */
1456 ret
= sem_init(&consumer_data
->sem
, 0, 0);
1458 PERROR("sem_init consumer semaphore");
1462 ret
= pthread_create(&consumer_data
->thread
, NULL
,
1463 thread_manage_consumer
, consumer_data
);
1465 PERROR("pthread_create consumer");
1470 /* Get time for sem_timedwait absolute timeout */
1471 ret
= clock_gettime(CLOCK_REALTIME
, &timeout
);
1473 PERROR("clock_gettime spawn consumer");
1474 /* Infinite wait for the kconsumerd thread to be ready */
1475 ret
= sem_wait(&consumer_data
->sem
);
1477 /* Normal timeout if the gettime was successful */
1478 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
1479 ret
= sem_timedwait(&consumer_data
->sem
, &timeout
);
1483 if (errno
== ETIMEDOUT
) {
1485 * Call has timed out so we kill the kconsumerd_thread and return
1488 ERR("The consumer thread was never ready. Killing it");
1489 ret
= pthread_cancel(consumer_data
->thread
);
1491 PERROR("pthread_cancel consumer thread");
1494 PERROR("semaphore wait failed consumer thread");
1499 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1500 if (consumer_data
->pid
== 0) {
1501 ERR("Kconsumerd did not start");
1502 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1505 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1514 * Join consumer thread
1516 static int join_consumer_thread(struct consumer_data
*consumer_data
)
1521 /* Consumer pid must be a real one. */
1522 if (consumer_data
->pid
> 0) {
1523 ret
= kill(consumer_data
->pid
, SIGTERM
);
1525 ERR("Error killing consumer daemon");
1528 return pthread_join(consumer_data
->thread
, &status
);
1535 * Fork and exec a consumer daemon (consumerd).
1537 * Return pid if successful else -1.
1539 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
1543 const char *consumer_to_use
;
1544 const char *verbosity
;
1547 DBG("Spawning consumerd");
1554 if (opt_verbose_consumer
) {
1555 verbosity
= "--verbose";
1557 verbosity
= "--quiet";
1559 switch (consumer_data
->type
) {
1560 case LTTNG_CONSUMER_KERNEL
:
1562 * Find out which consumerd to execute. We will first try the
1563 * 64-bit path, then the sessiond's installation directory, and
1564 * fallback on the 32-bit one,
1566 DBG3("Looking for a kernel consumer at these locations:");
1567 DBG3(" 1) %s", consumerd64_bin
);
1568 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
1569 DBG3(" 3) %s", consumerd32_bin
);
1570 if (stat(consumerd64_bin
, &st
) == 0) {
1571 DBG3("Found location #1");
1572 consumer_to_use
= consumerd64_bin
;
1573 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
1574 DBG3("Found location #2");
1575 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
1576 } else if (stat(consumerd32_bin
, &st
) == 0) {
1577 DBG3("Found location #3");
1578 consumer_to_use
= consumerd32_bin
;
1580 DBG("Could not find any valid consumerd executable");
1583 DBG("Using kernel consumer at: %s", consumer_to_use
);
1584 execl(consumer_to_use
,
1585 "lttng-consumerd", verbosity
, "-k",
1586 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1587 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1590 case LTTNG_CONSUMER64_UST
:
1592 char *tmpnew
= NULL
;
1594 if (consumerd64_libdir
[0] != '\0') {
1598 tmp
= getenv("LD_LIBRARY_PATH");
1602 tmplen
= strlen("LD_LIBRARY_PATH=")
1603 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
1604 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1609 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1610 strcat(tmpnew
, consumerd64_libdir
);
1611 if (tmp
[0] != '\0') {
1612 strcat(tmpnew
, ":");
1613 strcat(tmpnew
, tmp
);
1615 ret
= putenv(tmpnew
);
1621 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
1622 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
1623 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1624 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1626 if (consumerd64_libdir
[0] != '\0') {
1634 case LTTNG_CONSUMER32_UST
:
1636 char *tmpnew
= NULL
;
1638 if (consumerd32_libdir
[0] != '\0') {
1642 tmp
= getenv("LD_LIBRARY_PATH");
1646 tmplen
= strlen("LD_LIBRARY_PATH=")
1647 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
1648 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1653 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1654 strcat(tmpnew
, consumerd32_libdir
);
1655 if (tmp
[0] != '\0') {
1656 strcat(tmpnew
, ":");
1657 strcat(tmpnew
, tmp
);
1659 ret
= putenv(tmpnew
);
1665 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
1666 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
1667 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1668 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1670 if (consumerd32_libdir
[0] != '\0') {
1679 PERROR("unknown consumer type");
1683 PERROR("kernel start consumer exec");
1686 } else if (pid
> 0) {
1689 PERROR("start consumer fork");
1697 * Spawn the consumerd daemon and session daemon thread.
1699 static int start_consumerd(struct consumer_data
*consumer_data
)
1703 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1704 if (consumer_data
->pid
!= 0) {
1705 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1709 ret
= spawn_consumerd(consumer_data
);
1711 ERR("Spawning consumerd failed");
1712 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1716 /* Setting up the consumer_data pid */
1717 consumer_data
->pid
= ret
;
1718 DBG2("Consumer pid %d", consumer_data
->pid
);
1719 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1721 DBG2("Spawning consumer control thread");
1722 ret
= spawn_consumer_thread(consumer_data
);
1724 ERR("Fatal error spawning consumer control thread");
1736 * Compute health status of each consumer. If one of them is zero (bad
1737 * state), we return 0.
1739 static int check_consumer_health(void)
1743 ret
= health_check_state(&kconsumer_data
.health
) &&
1744 health_check_state(&ustconsumer32_data
.health
) &&
1745 health_check_state(&ustconsumer64_data
.health
);
1747 DBG3("Health consumer check %d", ret
);
1753 * Setup necessary data for kernel tracer action.
1755 static int init_kernel_tracer(void)
1759 /* Modprobe lttng kernel modules */
1760 ret
= modprobe_lttng_control();
1765 /* Open debugfs lttng */
1766 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1767 if (kernel_tracer_fd
< 0) {
1768 DBG("Failed to open %s", module_proc_lttng
);
1773 /* Validate kernel version */
1774 ret
= kernel_validate_version(kernel_tracer_fd
);
1779 ret
= modprobe_lttng_data();
1784 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1788 modprobe_remove_lttng_control();
1789 ret
= close(kernel_tracer_fd
);
1793 kernel_tracer_fd
= -1;
1794 return LTTCOMM_KERN_VERSION
;
1797 ret
= close(kernel_tracer_fd
);
1803 modprobe_remove_lttng_control();
1806 WARN("No kernel tracer available");
1807 kernel_tracer_fd
= -1;
1809 return LTTCOMM_NEED_ROOT_SESSIOND
;
1811 return LTTCOMM_KERN_NA
;
1817 * Copy consumer output from the tracing session to the domain session. The
1818 * function also applies the right modification on a per domain basis for the
1819 * trace files destination directory.
1821 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
1824 const char *dir_name
;
1825 struct consumer_output
*consumer
;
1828 assert(session
->consumer
);
1831 case LTTNG_DOMAIN_KERNEL
:
1832 DBG3("Copying tracing session consumer output in kernel session");
1833 session
->kernel_session
->consumer
=
1834 consumer_copy_output(session
->consumer
);
1835 /* Ease our life a bit for the next part */
1836 consumer
= session
->kernel_session
->consumer
;
1837 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
1839 case LTTNG_DOMAIN_UST
:
1840 DBG3("Copying tracing session consumer output in UST session");
1841 session
->ust_session
->consumer
=
1842 consumer_copy_output(session
->consumer
);
1843 /* Ease our life a bit for the next part */
1844 consumer
= session
->ust_session
->consumer
;
1845 dir_name
= DEFAULT_UST_TRACE_DIR
;
1848 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
1852 /* Append correct directory to subdir */
1853 strncat(consumer
->subdir
, dir_name
,
1854 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
1855 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
1864 * Create an UST session and add it to the session ust list.
1866 static int create_ust_session(struct ltt_session
*session
,
1867 struct lttng_domain
*domain
)
1870 struct ltt_ust_session
*lus
= NULL
;
1874 assert(session
->consumer
);
1876 switch (domain
->type
) {
1877 case LTTNG_DOMAIN_UST
:
1880 ERR("Unknown UST domain on create session %d", domain
->type
);
1881 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
1885 DBG("Creating UST session");
1887 lus
= trace_ust_create_session(session
->path
, session
->id
, domain
);
1889 ret
= LTTCOMM_UST_SESS_FAIL
;
1893 lus
->uid
= session
->uid
;
1894 lus
->gid
= session
->gid
;
1895 session
->ust_session
= lus
;
1897 /* Copy session output to the newly created UST session */
1898 ret
= copy_session_consumer(domain
->type
, session
);
1899 if (ret
!= LTTCOMM_OK
) {
1907 session
->ust_session
= NULL
;
1912 * Create a kernel tracer session then create the default channel.
1914 static int create_kernel_session(struct ltt_session
*session
)
1918 DBG("Creating kernel session");
1920 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1922 ret
= LTTCOMM_KERN_SESS_FAIL
;
1926 /* Code flow safety */
1927 assert(session
->kernel_session
);
1929 /* Copy session output to the newly created Kernel session */
1930 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
1931 if (ret
!= LTTCOMM_OK
) {
1935 /* Create directory(ies) on local filesystem. */
1936 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
1937 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
1938 ret
= run_as_mkdir_recursive(
1939 session
->kernel_session
->consumer
->dst
.trace_path
,
1940 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
1942 if (ret
!= -EEXIST
) {
1943 ERR("Trace directory creation error");
1949 session
->kernel_session
->uid
= session
->uid
;
1950 session
->kernel_session
->gid
= session
->gid
;
1955 trace_kernel_destroy_session(session
->kernel_session
);
1956 session
->kernel_session
= NULL
;
1961 * Count number of session permitted by uid/gid.
1963 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
1966 struct ltt_session
*session
;
1968 DBG("Counting number of available session for UID %d GID %d",
1970 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1972 * Only list the sessions the user can control.
1974 if (!session_access_ok(session
, uid
, gid
)) {
1983 * Process the command requested by the lttng client within the command
1984 * context structure. This function make sure that the return structure (llm)
1985 * is set and ready for transmission before returning.
1987 * Return any error encountered or 0 for success.
1989 * "sock" is only used for special-case var. len data.
1991 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
1994 int ret
= LTTCOMM_OK
;
1995 int need_tracing_session
= 1;
1998 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2002 switch (cmd_ctx
->lsm
->cmd_type
) {
2003 case LTTNG_CREATE_SESSION
:
2004 case LTTNG_DESTROY_SESSION
:
2005 case LTTNG_LIST_SESSIONS
:
2006 case LTTNG_LIST_DOMAINS
:
2007 case LTTNG_START_TRACE
:
2008 case LTTNG_STOP_TRACE
:
2015 if (opt_no_kernel
&& need_domain
2016 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2018 ret
= LTTCOMM_NEED_ROOT_SESSIOND
;
2020 ret
= LTTCOMM_KERN_NA
;
2025 /* Deny register consumer if we already have a spawned consumer. */
2026 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2027 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2028 if (kconsumer_data
.pid
> 0) {
2029 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
2030 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2033 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2037 * Check for command that don't needs to allocate a returned payload. We do
2038 * this here so we don't have to make the call for no payload at each
2041 switch(cmd_ctx
->lsm
->cmd_type
) {
2042 case LTTNG_LIST_SESSIONS
:
2043 case LTTNG_LIST_TRACEPOINTS
:
2044 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2045 case LTTNG_LIST_DOMAINS
:
2046 case LTTNG_LIST_CHANNELS
:
2047 case LTTNG_LIST_EVENTS
:
2050 /* Setup lttng message with no payload */
2051 ret
= setup_lttng_msg(cmd_ctx
, 0);
2053 /* This label does not try to unlock the session */
2054 goto init_setup_error
;
2058 /* Commands that DO NOT need a session. */
2059 switch (cmd_ctx
->lsm
->cmd_type
) {
2060 case LTTNG_CREATE_SESSION
:
2061 case LTTNG_CALIBRATE
:
2062 case LTTNG_LIST_SESSIONS
:
2063 case LTTNG_LIST_TRACEPOINTS
:
2064 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2065 need_tracing_session
= 0;
2068 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2070 * We keep the session list lock across _all_ commands
2071 * for now, because the per-session lock does not
2072 * handle teardown properly.
2074 session_lock_list();
2075 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2076 if (cmd_ctx
->session
== NULL
) {
2077 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
2078 ret
= LTTCOMM_SESS_NOT_FOUND
;
2080 /* If no session name specified */
2081 ret
= LTTCOMM_SELECT_SESS
;
2085 /* Acquire lock for the session */
2086 session_lock(cmd_ctx
->session
);
2096 * Check domain type for specific "pre-action".
2098 switch (cmd_ctx
->lsm
->domain
.type
) {
2099 case LTTNG_DOMAIN_KERNEL
:
2101 ret
= LTTCOMM_NEED_ROOT_SESSIOND
;
2105 /* Kernel tracer check */
2106 if (kernel_tracer_fd
== -1) {
2107 /* Basically, load kernel tracer modules */
2108 ret
= init_kernel_tracer();
2114 /* Consumer is in an ERROR state. Report back to client */
2115 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
2116 ret
= LTTCOMM_NO_KERNCONSUMERD
;
2120 /* Need a session for kernel command */
2121 if (need_tracing_session
) {
2122 if (cmd_ctx
->session
->kernel_session
== NULL
) {
2123 ret
= create_kernel_session(cmd_ctx
->session
);
2125 ret
= LTTCOMM_KERN_SESS_FAIL
;
2130 /* Start the kernel consumer daemon */
2131 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2132 if (kconsumer_data
.pid
== 0 &&
2133 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
&&
2134 cmd_ctx
->session
->start_consumer
) {
2135 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2136 ret
= start_consumerd(&kconsumer_data
);
2138 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
2141 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
2143 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2147 * The consumer was just spawned so we need to add the socket to
2148 * the consumer output of the session if exist.
2150 ret
= consumer_create_socket(&kconsumer_data
,
2151 cmd_ctx
->session
->kernel_session
->consumer
);
2158 case LTTNG_DOMAIN_UST
:
2160 /* Consumer is in an ERROR state. Report back to client */
2161 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
2162 ret
= LTTCOMM_NO_USTCONSUMERD
;
2166 if (need_tracing_session
) {
2167 /* Create UST session if none exist. */
2168 if (cmd_ctx
->session
->ust_session
== NULL
) {
2169 ret
= create_ust_session(cmd_ctx
->session
,
2170 &cmd_ctx
->lsm
->domain
);
2171 if (ret
!= LTTCOMM_OK
) {
2176 /* Start the UST consumer daemons */
2178 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
2179 if (consumerd64_bin
[0] != '\0' &&
2180 ustconsumer64_data
.pid
== 0 &&
2181 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
&&
2182 cmd_ctx
->session
->start_consumer
) {
2183 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2184 ret
= start_consumerd(&ustconsumer64_data
);
2186 ret
= LTTCOMM_UST_CONSUMER64_FAIL
;
2187 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
2191 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
2192 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2194 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2198 * Setup socket for consumer 64 bit. No need for atomic access
2199 * since it was set above and can ONLY be set in this thread.
2201 ret
= consumer_create_socket(&ustconsumer64_data
,
2202 cmd_ctx
->session
->ust_session
->consumer
);
2208 if (consumerd32_bin
[0] != '\0' &&
2209 ustconsumer32_data
.pid
== 0 &&
2210 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
&&
2211 cmd_ctx
->session
->start_consumer
) {
2212 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2213 ret
= start_consumerd(&ustconsumer32_data
);
2215 ret
= LTTCOMM_UST_CONSUMER32_FAIL
;
2216 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
2220 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
2221 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2223 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2227 * Setup socket for consumer 64 bit. No need for atomic access
2228 * since it was set above and can ONLY be set in this thread.
2230 ret
= consumer_create_socket(&ustconsumer32_data
,
2231 cmd_ctx
->session
->ust_session
->consumer
);
2243 /* Validate consumer daemon state when start/stop trace command */
2244 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
2245 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
2246 switch (cmd_ctx
->lsm
->domain
.type
) {
2247 case LTTNG_DOMAIN_UST
:
2248 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
2249 ret
= LTTCOMM_NO_USTCONSUMERD
;
2253 case LTTNG_DOMAIN_KERNEL
:
2254 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
2255 ret
= LTTCOMM_NO_KERNCONSUMERD
;
2263 * Check that the UID or GID match that of the tracing session.
2264 * The root user can interact with all sessions.
2266 if (need_tracing_session
) {
2267 if (!session_access_ok(cmd_ctx
->session
,
2268 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2269 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
2270 ret
= LTTCOMM_EPERM
;
2275 /* Process by command type */
2276 switch (cmd_ctx
->lsm
->cmd_type
) {
2277 case LTTNG_ADD_CONTEXT
:
2279 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2280 cmd_ctx
->lsm
->u
.context
.channel_name
,
2281 cmd_ctx
->lsm
->u
.context
.event_name
,
2282 &cmd_ctx
->lsm
->u
.context
.ctx
);
2285 case LTTNG_DISABLE_CHANNEL
:
2287 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2288 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2291 case LTTNG_DISABLE_EVENT
:
2293 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2294 cmd_ctx
->lsm
->u
.disable
.channel_name
,
2295 cmd_ctx
->lsm
->u
.disable
.name
);
2298 case LTTNG_DISABLE_ALL_EVENT
:
2300 DBG("Disabling all events");
2302 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2303 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2306 case LTTNG_DISABLE_CONSUMER
:
2308 ret
= cmd_disable_consumer(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
);
2311 case LTTNG_ENABLE_CHANNEL
:
2313 ret
= cmd_enable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2314 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
2317 case LTTNG_ENABLE_CONSUMER
:
2320 * XXX: 0 means that this URI should be applied on the session. Should
2321 * be a DOMAIN enuam.
2323 ret
= cmd_enable_consumer(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
);
2324 if (ret
!= LTTCOMM_OK
) {
2328 if (cmd_ctx
->lsm
->domain
.type
== 0) {
2329 /* Add the URI for the UST session if a consumer is present. */
2330 if (cmd_ctx
->session
->ust_session
&&
2331 cmd_ctx
->session
->ust_session
->consumer
) {
2332 ret
= cmd_enable_consumer(LTTNG_DOMAIN_UST
, cmd_ctx
->session
);
2333 } else if (cmd_ctx
->session
->kernel_session
&&
2334 cmd_ctx
->session
->kernel_session
->consumer
) {
2335 ret
= cmd_enable_consumer(LTTNG_DOMAIN_KERNEL
,
2341 case LTTNG_ENABLE_EVENT
:
2343 ret
= cmd_enable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2344 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2345 &cmd_ctx
->lsm
->u
.enable
.event
, kernel_poll_pipe
[1]);
2348 case LTTNG_ENABLE_ALL_EVENT
:
2350 DBG("Enabling all events");
2352 ret
= cmd_enable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2353 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2354 cmd_ctx
->lsm
->u
.enable
.event
.type
, kernel_poll_pipe
[1]);
2357 case LTTNG_LIST_TRACEPOINTS
:
2359 struct lttng_event
*events
;
2362 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
2363 if (nb_events
< 0) {
2369 * Setup lttng message with payload size set to the event list size in
2370 * bytes and then copy list into the llm payload.
2372 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2378 /* Copy event list into message payload */
2379 memcpy(cmd_ctx
->llm
->payload
, events
,
2380 sizeof(struct lttng_event
) * nb_events
);
2387 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2389 struct lttng_event_field
*fields
;
2392 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
2394 if (nb_fields
< 0) {
2400 * Setup lttng message with payload size set to the event list size in
2401 * bytes and then copy list into the llm payload.
2403 ret
= setup_lttng_msg(cmd_ctx
,
2404 sizeof(struct lttng_event_field
) * nb_fields
);
2410 /* Copy event list into message payload */
2411 memcpy(cmd_ctx
->llm
->payload
, fields
,
2412 sizeof(struct lttng_event_field
) * nb_fields
);
2419 case LTTNG_SET_CONSUMER_URI
:
2422 struct lttng_uri
*uris
;
2424 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
2425 len
= nb_uri
* sizeof(struct lttng_uri
);
2428 ret
= LTTCOMM_INVALID
;
2432 uris
= zmalloc(len
);
2434 ret
= LTTCOMM_FATAL
;
2438 /* Receive variable len data */
2439 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
2440 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
2442 DBG("No URIs received from client... continuing");
2444 ret
= LTTCOMM_SESSION_FAIL
;
2448 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
2450 if (ret
!= LTTCOMM_OK
) {
2455 * XXX: 0 means that this URI should be applied on the session. Should
2456 * be a DOMAIN enuam.
2458 if (cmd_ctx
->lsm
->domain
.type
== 0) {
2459 /* Add the URI for the UST session if a consumer is present. */
2460 if (cmd_ctx
->session
->ust_session
&&
2461 cmd_ctx
->session
->ust_session
->consumer
) {
2462 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_UST
, cmd_ctx
->session
,
2464 } else if (cmd_ctx
->session
->kernel_session
&&
2465 cmd_ctx
->session
->kernel_session
->consumer
) {
2466 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL
,
2467 cmd_ctx
->session
, nb_uri
, uris
);
2473 case LTTNG_START_TRACE
:
2475 ret
= cmd_start_trace(cmd_ctx
->session
);
2478 case LTTNG_STOP_TRACE
:
2480 ret
= cmd_stop_trace(cmd_ctx
->session
);
2483 case LTTNG_CREATE_SESSION
:
2486 struct lttng_uri
*uris
= NULL
;
2488 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
2489 len
= nb_uri
* sizeof(struct lttng_uri
);
2492 uris
= zmalloc(len
);
2494 ret
= LTTCOMM_FATAL
;
2498 /* Receive variable len data */
2499 DBG("Waiting for %zu URIs from client ...", nb_uri
);
2500 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
2502 DBG("No URIs received from client... continuing");
2504 ret
= LTTCOMM_SESSION_FAIL
;
2508 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
2509 DBG("Creating session with ONE network URI is a bad call");
2510 ret
= LTTCOMM_SESSION_FAIL
;
2515 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
2520 case LTTNG_DESTROY_SESSION
:
2522 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
2524 /* Set session to NULL so we do not unlock it after free. */
2525 cmd_ctx
->session
= NULL
;
2528 case LTTNG_LIST_DOMAINS
:
2531 struct lttng_domain
*domains
;
2533 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
2539 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
2544 /* Copy event list into message payload */
2545 memcpy(cmd_ctx
->llm
->payload
, domains
,
2546 nb_dom
* sizeof(struct lttng_domain
));
2553 case LTTNG_LIST_CHANNELS
:
2556 struct lttng_channel
*channels
;
2558 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
2559 cmd_ctx
->session
, &channels
);
2565 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
2570 /* Copy event list into message payload */
2571 memcpy(cmd_ctx
->llm
->payload
, channels
,
2572 nb_chan
* sizeof(struct lttng_channel
));
2579 case LTTNG_LIST_EVENTS
:
2582 struct lttng_event
*events
= NULL
;
2584 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
2585 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
2591 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
2596 /* Copy event list into message payload */
2597 memcpy(cmd_ctx
->llm
->payload
, events
,
2598 nb_event
* sizeof(struct lttng_event
));
2605 case LTTNG_LIST_SESSIONS
:
2607 unsigned int nr_sessions
;
2609 session_lock_list();
2610 nr_sessions
= lttng_sessions_count(
2611 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2612 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
2614 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
2616 session_unlock_list();
2620 /* Filled the session array */
2621 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
2622 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2623 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
2625 session_unlock_list();
2630 case LTTNG_CALIBRATE
:
2632 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
2633 &cmd_ctx
->lsm
->u
.calibrate
);
2636 case LTTNG_REGISTER_CONSUMER
:
2638 struct consumer_data
*cdata
;
2640 switch (cmd_ctx
->lsm
->domain
.type
) {
2641 case LTTNG_DOMAIN_KERNEL
:
2642 cdata
= &kconsumer_data
;
2649 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2650 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
2653 case LTTNG_SET_FILTER
:
2655 struct lttng_filter_bytecode
*bytecode
;
2657 if (cmd_ctx
->lsm
->u
.filter
.bytecode_len
> 65336) {
2658 ret
= LTTCOMM_FILTER_INVAL
;
2661 bytecode
= zmalloc(cmd_ctx
->lsm
->u
.filter
.bytecode_len
);
2663 ret
= LTTCOMM_FILTER_NOMEM
;
2666 /* Receive var. len. data */
2667 DBG("Receiving var len data from client ...");
2668 ret
= lttcomm_recv_unix_sock(sock
, bytecode
,
2669 cmd_ctx
->lsm
->u
.filter
.bytecode_len
);
2671 DBG("Nothing recv() from client var len data... continuing");
2673 ret
= LTTCOMM_FILTER_INVAL
;
2677 if (bytecode
->len
+ sizeof(*bytecode
)
2678 != cmd_ctx
->lsm
->u
.filter
.bytecode_len
) {
2680 ret
= LTTCOMM_FILTER_INVAL
;
2684 ret
= cmd_set_filter(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2685 cmd_ctx
->lsm
->u
.filter
.channel_name
,
2686 cmd_ctx
->lsm
->u
.filter
.event_name
,
2696 if (cmd_ctx
->llm
== NULL
) {
2697 DBG("Missing llm structure. Allocating one.");
2698 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2702 /* Set return code */
2703 cmd_ctx
->llm
->ret_code
= ret
;
2705 if (cmd_ctx
->session
) {
2706 session_unlock(cmd_ctx
->session
);
2708 if (need_tracing_session
) {
2709 session_unlock_list();
2716 * Thread managing health check socket.
2718 static void *thread_manage_health(void *data
)
2720 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
2721 uint32_t revents
, nb_fd
;
2722 struct lttng_poll_event events
;
2723 struct lttcomm_health_msg msg
;
2724 struct lttcomm_health_data reply
;
2726 DBG("[thread] Manage health check started");
2728 rcu_register_thread();
2730 /* Create unix socket */
2731 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
2733 ERR("Unable to create health check Unix socket");
2738 ret
= lttcomm_listen_unix_sock(sock
);
2744 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2745 * more will be added to this poll set.
2747 ret
= create_thread_poll_set(&events
, 2);
2752 /* Add the application registration socket */
2753 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
2759 DBG("Health check ready");
2761 nb_fd
= LTTNG_POLL_GETNB(&events
);
2763 /* Inifinite blocking call, waiting for transmission */
2765 ret
= lttng_poll_wait(&events
, -1);
2768 * Restart interrupted system call.
2770 if (errno
== EINTR
) {
2776 for (i
= 0; i
< nb_fd
; i
++) {
2777 /* Fetch once the poll data */
2778 revents
= LTTNG_POLL_GETEV(&events
, i
);
2779 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2781 /* Thread quit pipe has been closed. Killing thread. */
2782 ret
= check_thread_quit_pipe(pollfd
, revents
);
2788 /* Event on the registration socket */
2789 if (pollfd
== sock
) {
2790 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2791 ERR("Health socket poll error");
2797 new_sock
= lttcomm_accept_unix_sock(sock
);
2802 DBG("Receiving data from client for health...");
2803 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
2805 DBG("Nothing recv() from client... continuing");
2806 ret
= close(new_sock
);
2814 rcu_thread_online();
2816 switch (msg
.component
) {
2817 case LTTNG_HEALTH_CMD
:
2818 reply
.ret_code
= health_check_state(&health_thread_cmd
);
2820 case LTTNG_HEALTH_APP_MANAGE
:
2821 reply
.ret_code
= health_check_state(&health_thread_app_manage
);
2823 case LTTNG_HEALTH_APP_REG
:
2824 reply
.ret_code
= health_check_state(&health_thread_app_reg
);
2826 case LTTNG_HEALTH_KERNEL
:
2827 reply
.ret_code
= health_check_state(&health_thread_kernel
);
2829 case LTTNG_HEALTH_CONSUMER
:
2830 reply
.ret_code
= check_consumer_health();
2832 case LTTNG_HEALTH_ALL
:
2834 health_check_state(&health_thread_app_manage
) &&
2835 health_check_state(&health_thread_app_reg
) &&
2836 health_check_state(&health_thread_cmd
) &&
2837 health_check_state(&health_thread_kernel
) &&
2838 check_consumer_health();
2841 reply
.ret_code
= LTTCOMM_UND
;
2846 * Flip ret value since 0 is a success and 1 indicates a bad health for
2847 * the client where in the sessiond it is the opposite. Again, this is
2848 * just to make things easier for us poor developer which enjoy a lot
2851 if (reply
.ret_code
== 0 || reply
.ret_code
== 1) {
2852 reply
.ret_code
= !reply
.ret_code
;
2855 DBG2("Health check return value %d", reply
.ret_code
);
2857 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
2859 ERR("Failed to send health data back to client");
2862 /* End of transmission */
2863 ret
= close(new_sock
);
2873 ERR("Health error occurred in %s", __func__
);
2875 DBG("Health check thread dying");
2876 unlink(health_unix_sock_path
);
2883 if (new_sock
>= 0) {
2884 ret
= close(new_sock
);
2890 lttng_poll_clean(&events
);
2892 rcu_unregister_thread();
2897 * This thread manage all clients request using the unix client socket for
2900 static void *thread_manage_clients(void *data
)
2902 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2904 uint32_t revents
, nb_fd
;
2905 struct command_ctx
*cmd_ctx
= NULL
;
2906 struct lttng_poll_event events
;
2908 DBG("[thread] Manage client started");
2910 rcu_register_thread();
2912 health_code_update(&health_thread_cmd
);
2914 ret
= lttcomm_listen_unix_sock(client_sock
);
2920 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2921 * more will be added to this poll set.
2923 ret
= create_thread_poll_set(&events
, 2);
2928 /* Add the application registration socket */
2929 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2935 * Notify parent pid that we are ready to accept command for client side.
2937 if (opt_sig_parent
) {
2938 kill(ppid
, SIGUSR1
);
2941 health_code_update(&health_thread_cmd
);
2944 DBG("Accepting client command ...");
2946 nb_fd
= LTTNG_POLL_GETNB(&events
);
2948 /* Inifinite blocking call, waiting for transmission */
2950 health_poll_update(&health_thread_cmd
);
2951 ret
= lttng_poll_wait(&events
, -1);
2952 health_poll_update(&health_thread_cmd
);
2955 * Restart interrupted system call.
2957 if (errno
== EINTR
) {
2963 for (i
= 0; i
< nb_fd
; i
++) {
2964 /* Fetch once the poll data */
2965 revents
= LTTNG_POLL_GETEV(&events
, i
);
2966 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2968 health_code_update(&health_thread_cmd
);
2970 /* Thread quit pipe has been closed. Killing thread. */
2971 ret
= check_thread_quit_pipe(pollfd
, revents
);
2977 /* Event on the registration socket */
2978 if (pollfd
== client_sock
) {
2979 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2980 ERR("Client socket poll error");
2986 DBG("Wait for client response");
2988 health_code_update(&health_thread_cmd
);
2990 sock
= lttcomm_accept_unix_sock(client_sock
);
2995 /* Set socket option for credentials retrieval */
2996 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
3001 /* Allocate context command to process the client request */
3002 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
3003 if (cmd_ctx
== NULL
) {
3004 PERROR("zmalloc cmd_ctx");
3008 /* Allocate data buffer for reception */
3009 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
3010 if (cmd_ctx
->lsm
== NULL
) {
3011 PERROR("zmalloc cmd_ctx->lsm");
3015 cmd_ctx
->llm
= NULL
;
3016 cmd_ctx
->session
= NULL
;
3018 health_code_update(&health_thread_cmd
);
3021 * Data is received from the lttng client. The struct
3022 * lttcomm_session_msg (lsm) contains the command and data request of
3025 DBG("Receiving data from client ...");
3026 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
3027 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
3029 DBG("Nothing recv() from client... continuing");
3035 clean_command_ctx(&cmd_ctx
);
3039 health_code_update(&health_thread_cmd
);
3041 // TODO: Validate cmd_ctx including sanity check for
3042 // security purpose.
3044 rcu_thread_online();
3046 * This function dispatch the work to the kernel or userspace tracer
3047 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3048 * informations for the client. The command context struct contains
3049 * everything this function may needs.
3051 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
3052 rcu_thread_offline();
3062 * TODO: Inform client somehow of the fatal error. At
3063 * this point, ret < 0 means that a zmalloc failed
3064 * (ENOMEM). Error detected but still accept
3065 * command, unless a socket error has been
3068 clean_command_ctx(&cmd_ctx
);
3072 health_code_update(&health_thread_cmd
);
3074 DBG("Sending response (size: %d, retcode: %s)",
3075 cmd_ctx
->lttng_msg_size
,
3076 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
3077 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
3079 ERR("Failed to send data back to client");
3082 /* End of transmission */
3089 clean_command_ctx(&cmd_ctx
);
3091 health_code_update(&health_thread_cmd
);
3097 health_error(&health_thread_cmd
);
3098 ERR("Health error occurred in %s", __func__
);
3100 health_exit(&health_thread_cmd
);
3102 DBG("Client thread dying");
3103 unlink(client_unix_sock_path
);
3104 if (client_sock
>= 0) {
3105 ret
= close(client_sock
);
3117 lttng_poll_clean(&events
);
3118 clean_command_ctx(&cmd_ctx
);
3120 rcu_unregister_thread();
3126 * usage function on stderr
3128 static void usage(void)
3130 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
3131 fprintf(stderr
, " -h, --help Display this usage.\n");
3132 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
3133 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
3134 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
3135 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
3136 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
3137 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
3138 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
3139 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
3140 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
3141 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
3142 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
3143 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
3144 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
3145 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
3146 fprintf(stderr
, " -V, --version Show version number.\n");
3147 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
3148 fprintf(stderr
, " -q, --quiet No output at all.\n");
3149 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3150 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
3151 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
3155 * daemon argument parsing
3157 static int parse_args(int argc
, char **argv
)
3161 static struct option long_options
[] = {
3162 { "client-sock", 1, 0, 'c' },
3163 { "apps-sock", 1, 0, 'a' },
3164 { "kconsumerd-cmd-sock", 1, 0, 'C' },
3165 { "kconsumerd-err-sock", 1, 0, 'E' },
3166 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
3167 { "ustconsumerd32-err-sock", 1, 0, 'H' },
3168 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
3169 { "ustconsumerd64-err-sock", 1, 0, 'F' },
3170 { "consumerd32-path", 1, 0, 'u' },
3171 { "consumerd32-libdir", 1, 0, 'U' },
3172 { "consumerd64-path", 1, 0, 't' },
3173 { "consumerd64-libdir", 1, 0, 'T' },
3174 { "daemonize", 0, 0, 'd' },
3175 { "sig-parent", 0, 0, 'S' },
3176 { "help", 0, 0, 'h' },
3177 { "group", 1, 0, 'g' },
3178 { "version", 0, 0, 'V' },
3179 { "quiet", 0, 0, 'q' },
3180 { "verbose", 0, 0, 'v' },
3181 { "verbose-consumer", 0, 0, 'Z' },
3182 { "no-kernel", 0, 0, 'N' },
3187 int option_index
= 0;
3188 c
= getopt_long(argc
, argv
, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t",
3189 long_options
, &option_index
);
3196 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
3198 fprintf(stderr
, " with arg %s\n", optarg
);
3202 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3205 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3211 opt_tracing_group
= optarg
;
3217 fprintf(stdout
, "%s\n", VERSION
);
3223 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3226 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3229 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3232 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3235 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3238 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3244 lttng_opt_quiet
= 1;
3247 /* Verbose level can increase using multiple -v */
3248 lttng_opt_verbose
+= 1;
3251 opt_verbose_consumer
+= 1;
3254 consumerd32_bin
= optarg
;
3257 consumerd32_libdir
= optarg
;
3260 consumerd64_bin
= optarg
;
3263 consumerd64_libdir
= optarg
;
3266 /* Unknown option or other error.
3267 * Error is printed by getopt, just return */
3276 * Creates the two needed socket by the daemon.
3277 * apps_sock - The communication socket for all UST apps.
3278 * client_sock - The communication of the cli tool (lttng).
3280 static int init_daemon_socket(void)
3285 old_umask
= umask(0);
3287 /* Create client tool unix socket */
3288 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
3289 if (client_sock
< 0) {
3290 ERR("Create unix sock failed: %s", client_unix_sock_path
);
3295 /* File permission MUST be 660 */
3296 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3298 ERR("Set file permissions failed: %s", client_unix_sock_path
);
3303 /* Create the application unix socket */
3304 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
3305 if (apps_sock
< 0) {
3306 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
3311 /* File permission MUST be 666 */
3312 ret
= chmod(apps_unix_sock_path
,
3313 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
3315 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
3326 * Check if the global socket is available, and if a daemon is answering at the
3327 * other side. If yes, error is returned.
3329 static int check_existing_daemon(void)
3331 /* Is there anybody out there ? */
3332 if (lttng_session_daemon_alive()) {
3340 * Set the tracing group gid onto the client socket.
3342 * Race window between mkdir and chown is OK because we are going from more
3343 * permissive (root.root) to less permissive (root.tracing).
3345 static int set_permissions(char *rundir
)
3350 ret
= allowed_group();
3352 WARN("No tracing group detected");
3359 /* Set lttng run dir */
3360 ret
= chown(rundir
, 0, gid
);
3362 ERR("Unable to set group on %s", rundir
);
3366 /* Ensure tracing group can search the run dir */
3367 ret
= chmod(rundir
, S_IRWXU
| S_IXGRP
| S_IXOTH
);
3369 ERR("Unable to set permissions on %s", rundir
);
3373 /* lttng client socket path */
3374 ret
= chown(client_unix_sock_path
, 0, gid
);
3376 ERR("Unable to set group on %s", client_unix_sock_path
);
3380 /* kconsumer error socket path */
3381 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, gid
);
3383 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
3387 /* 64-bit ustconsumer error socket path */
3388 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, gid
);
3390 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
3394 /* 32-bit ustconsumer compat32 error socket path */
3395 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, gid
);
3397 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
3401 DBG("All permissions are set");
3408 * Create the lttng run directory needed for all global sockets and pipe.
3410 static int create_lttng_rundir(const char *rundir
)
3414 DBG3("Creating LTTng run directory: %s", rundir
);
3416 ret
= mkdir(rundir
, S_IRWXU
);
3418 if (errno
!= EEXIST
) {
3419 ERR("Unable to create %s", rundir
);
3431 * Setup sockets and directory needed by the kconsumerd communication with the
3434 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
3438 char path
[PATH_MAX
];
3440 switch (consumer_data
->type
) {
3441 case LTTNG_CONSUMER_KERNEL
:
3442 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
3444 case LTTNG_CONSUMER64_UST
:
3445 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
3447 case LTTNG_CONSUMER32_UST
:
3448 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
3451 ERR("Consumer type unknown");
3456 DBG2("Creating consumer directory: %s", path
);
3458 ret
= mkdir(path
, S_IRWXU
);
3460 if (errno
!= EEXIST
) {
3462 ERR("Failed to create %s", path
);
3468 /* Create the kconsumerd error unix socket */
3469 consumer_data
->err_sock
=
3470 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
3471 if (consumer_data
->err_sock
< 0) {
3472 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
3477 /* File permission MUST be 660 */
3478 ret
= chmod(consumer_data
->err_unix_sock_path
,
3479 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3481 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
3491 * Signal handler for the daemon
3493 * Simply stop all worker threads, leaving main() return gracefully after
3494 * joining all threads and calling cleanup().
3496 static void sighandler(int sig
)
3500 DBG("SIGPIPE caught");
3503 DBG("SIGINT caught");
3507 DBG("SIGTERM caught");
3516 * Setup signal handler for :
3517 * SIGINT, SIGTERM, SIGPIPE
3519 static int set_signal_handler(void)
3522 struct sigaction sa
;
3525 if ((ret
= sigemptyset(&sigset
)) < 0) {
3526 PERROR("sigemptyset");
3530 sa
.sa_handler
= sighandler
;
3531 sa
.sa_mask
= sigset
;
3533 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3534 PERROR("sigaction");
3538 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3539 PERROR("sigaction");
3543 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3544 PERROR("sigaction");
3548 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3554 * Set open files limit to unlimited. This daemon can open a large number of
3555 * file descriptors in order to consumer multiple kernel traces.
3557 static void set_ulimit(void)
3562 /* The kernel does not allowed an infinite limit for open files */
3563 lim
.rlim_cur
= 65535;
3564 lim
.rlim_max
= 65535;
3566 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3568 PERROR("failed to set open files limit");
3575 int main(int argc
, char **argv
)
3579 const char *home_path
;
3581 init_kernel_workarounds();
3583 rcu_register_thread();
3585 setup_consumerd_path();
3587 /* Parse arguments */
3589 if ((ret
= parse_args(argc
, argv
) < 0)) {
3599 * child: setsid, close FD 0, 1, 2, chdir /
3600 * parent: exit (if fork is successful)
3608 * We are in the child. Make sure all other file
3609 * descriptors are closed, in case we are called with
3610 * more opened file descriptors than the standard ones.
3612 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
3617 /* Create thread quit pipe */
3618 if ((ret
= init_thread_quit_pipe()) < 0) {
3622 /* Check if daemon is UID = 0 */
3623 is_root
= !getuid();
3626 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
3628 /* Create global run dir with root access */
3629 ret
= create_lttng_rundir(rundir
);
3634 if (strlen(apps_unix_sock_path
) == 0) {
3635 snprintf(apps_unix_sock_path
, PATH_MAX
,
3636 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
3639 if (strlen(client_unix_sock_path
) == 0) {
3640 snprintf(client_unix_sock_path
, PATH_MAX
,
3641 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
3644 /* Set global SHM for ust */
3645 if (strlen(wait_shm_path
) == 0) {
3646 snprintf(wait_shm_path
, PATH_MAX
,
3647 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
3650 if (strlen(health_unix_sock_path
) == 0) {
3651 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
3652 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
3655 /* Setup kernel consumerd path */
3656 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
3657 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
3658 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
3659 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
3661 DBG2("Kernel consumer err path: %s",
3662 kconsumer_data
.err_unix_sock_path
);
3663 DBG2("Kernel consumer cmd path: %s",
3664 kconsumer_data
.cmd_unix_sock_path
);
3666 home_path
= get_home_dir();
3667 if (home_path
== NULL
) {
3668 /* TODO: Add --socket PATH option */
3669 ERR("Can't get HOME directory for sockets creation.");
3675 * Create rundir from home path. This will create something like
3678 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
3684 ret
= create_lttng_rundir(rundir
);
3689 if (strlen(apps_unix_sock_path
) == 0) {
3690 snprintf(apps_unix_sock_path
, PATH_MAX
,
3691 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
3694 /* Set the cli tool unix socket path */
3695 if (strlen(client_unix_sock_path
) == 0) {
3696 snprintf(client_unix_sock_path
, PATH_MAX
,
3697 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
3700 /* Set global SHM for ust */
3701 if (strlen(wait_shm_path
) == 0) {
3702 snprintf(wait_shm_path
, PATH_MAX
,
3703 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, geteuid());
3706 /* Set health check Unix path */
3707 if (strlen(health_unix_sock_path
) == 0) {
3708 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
3709 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
3713 /* Set consumer initial state */
3714 kernel_consumerd_state
= CONSUMER_STOPPED
;
3715 ust_consumerd_state
= CONSUMER_STOPPED
;
3717 DBG("Client socket path %s", client_unix_sock_path
);
3718 DBG("Application socket path %s", apps_unix_sock_path
);
3719 DBG("LTTng run directory path: %s", rundir
);
3721 /* 32 bits consumerd path setup */
3722 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
3723 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
3724 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
3725 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
3727 DBG2("UST consumer 32 bits err path: %s",
3728 ustconsumer32_data
.err_unix_sock_path
);
3729 DBG2("UST consumer 32 bits cmd path: %s",
3730 ustconsumer32_data
.cmd_unix_sock_path
);
3732 /* 64 bits consumerd path setup */
3733 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
3734 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
3735 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
3736 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
3738 DBG2("UST consumer 64 bits err path: %s",
3739 ustconsumer64_data
.err_unix_sock_path
);
3740 DBG2("UST consumer 64 bits cmd path: %s",
3741 ustconsumer64_data
.cmd_unix_sock_path
);
3744 * See if daemon already exist.
3746 if ((ret
= check_existing_daemon()) < 0) {
3747 ERR("Already running daemon.\n");
3749 * We do not goto exit because we must not cleanup()
3750 * because a daemon is already running.
3756 * Init UST app hash table. Alloc hash table before this point since
3757 * cleanup() can get called after that point.
3761 /* After this point, we can safely call cleanup() with "goto exit" */
3764 * These actions must be executed as root. We do that *after* setting up
3765 * the sockets path because we MUST make the check for another daemon using
3766 * those paths *before* trying to set the kernel consumer sockets and init
3770 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
3775 /* Setup kernel tracer */
3776 if (!opt_no_kernel
) {
3777 init_kernel_tracer();
3780 /* Set ulimit for open files */
3783 /* init lttng_fd tracking must be done after set_ulimit. */
3786 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
3791 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
3796 if ((ret
= set_signal_handler()) < 0) {
3800 /* Setup the needed unix socket */
3801 if ((ret
= init_daemon_socket()) < 0) {
3805 /* Set credentials to socket */
3806 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
3810 /* Get parent pid if -S, --sig-parent is specified. */
3811 if (opt_sig_parent
) {
3815 /* Setup the kernel pipe for waking up the kernel thread */
3816 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
3820 /* Setup the thread apps communication pipe. */
3821 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
3825 /* Init UST command queue. */
3826 cds_wfq_init(&ust_cmd_queue
.queue
);
3829 * Get session list pointer. This pointer MUST NOT be free(). This list is
3830 * statically declared in session.c
3832 session_list_ptr
= session_get_list();
3834 /* Set up max poll set size */
3835 lttng_poll_set_max_size();
3839 /* Init all health thread counters. */
3840 health_init(&health_thread_cmd
);
3841 health_init(&health_thread_kernel
);
3842 health_init(&health_thread_app_manage
);
3843 health_init(&health_thread_app_reg
);
3846 * Init health counters of the consumer thread. We do a quick hack here to
3847 * the state of the consumer health is fine even if the thread is not
3848 * started. This is simply to ease our life and has no cost what so ever.
3850 health_init(&kconsumer_data
.health
);
3851 health_poll_update(&kconsumer_data
.health
);
3852 health_init(&ustconsumer32_data
.health
);
3853 health_poll_update(&ustconsumer32_data
.health
);
3854 health_init(&ustconsumer64_data
.health
);
3855 health_poll_update(&ustconsumer64_data
.health
);
3857 /* Create thread to manage the client socket */
3858 ret
= pthread_create(&health_thread
, NULL
,
3859 thread_manage_health
, (void *) NULL
);
3861 PERROR("pthread_create health");
3865 /* Create thread to manage the client socket */
3866 ret
= pthread_create(&client_thread
, NULL
,
3867 thread_manage_clients
, (void *) NULL
);
3869 PERROR("pthread_create clients");
3873 /* Create thread to dispatch registration */
3874 ret
= pthread_create(&dispatch_thread
, NULL
,
3875 thread_dispatch_ust_registration
, (void *) NULL
);
3877 PERROR("pthread_create dispatch");
3881 /* Create thread to manage application registration. */
3882 ret
= pthread_create(®_apps_thread
, NULL
,
3883 thread_registration_apps
, (void *) NULL
);
3885 PERROR("pthread_create registration");
3889 /* Create thread to manage application socket */
3890 ret
= pthread_create(&apps_thread
, NULL
,
3891 thread_manage_apps
, (void *) NULL
);
3893 PERROR("pthread_create apps");
3897 /* Create kernel thread to manage kernel event */
3898 ret
= pthread_create(&kernel_thread
, NULL
,
3899 thread_manage_kernel
, (void *) NULL
);
3901 PERROR("pthread_create kernel");
3905 ret
= pthread_join(kernel_thread
, &status
);
3907 PERROR("pthread_join");
3908 goto error
; /* join error, exit without cleanup */
3912 ret
= pthread_join(apps_thread
, &status
);
3914 PERROR("pthread_join");
3915 goto error
; /* join error, exit without cleanup */
3919 ret
= pthread_join(reg_apps_thread
, &status
);
3921 PERROR("pthread_join");
3922 goto error
; /* join error, exit without cleanup */
3926 ret
= pthread_join(dispatch_thread
, &status
);
3928 PERROR("pthread_join");
3929 goto error
; /* join error, exit without cleanup */
3933 ret
= pthread_join(client_thread
, &status
);
3935 PERROR("pthread_join");
3936 goto error
; /* join error, exit without cleanup */
3939 ret
= join_consumer_thread(&kconsumer_data
);
3941 PERROR("join_consumer");
3942 goto error
; /* join error, exit without cleanup */
3949 * cleanup() is called when no other thread is running.
3951 rcu_thread_online();
3953 rcu_thread_offline();
3954 rcu_unregister_thread();