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"
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 */
83 /* Consumer daemon specific control data */
84 static struct consumer_data kconsumer_data
= {
85 .type
= LTTNG_CONSUMER_KERNEL
,
86 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
87 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
91 static struct consumer_data ustconsumer64_data
= {
92 .type
= LTTNG_CONSUMER64_UST
,
93 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
94 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
98 static struct consumer_data ustconsumer32_data
= {
99 .type
= LTTNG_CONSUMER32_UST
,
100 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
101 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
106 /* Shared between threads */
107 static int dispatch_thread_exit
;
109 /* Global application Unix socket path */
110 static char apps_unix_sock_path
[PATH_MAX
];
111 /* Global client Unix socket path */
112 static char client_unix_sock_path
[PATH_MAX
];
113 /* global wait shm path for UST */
114 static char wait_shm_path
[PATH_MAX
];
115 /* Global health check unix path */
116 static char health_unix_sock_path
[PATH_MAX
];
118 /* Sockets and FDs */
119 static int client_sock
= -1;
120 static int apps_sock
= -1;
121 static int kernel_tracer_fd
= -1;
122 static int kernel_poll_pipe
[2] = { -1, -1 };
125 * Quit pipe for all threads. This permits a single cancellation point
126 * for all threads when receiving an event on the pipe.
128 static int thread_quit_pipe
[2] = { -1, -1 };
131 * This pipe is used to inform the thread managing application communication
132 * that a command is queued and ready to be processed.
134 static int apps_cmd_pipe
[2] = { -1, -1 };
136 /* Pthread, Mutexes and Semaphores */
137 static pthread_t apps_thread
;
138 static pthread_t reg_apps_thread
;
139 static pthread_t client_thread
;
140 static pthread_t kernel_thread
;
141 static pthread_t dispatch_thread
;
142 static pthread_t health_thread
;
145 * UST registration command queue. This queue is tied with a futex and uses a N
146 * wakers / 1 waiter implemented and detailed in futex.c/.h
148 * The thread_manage_apps and thread_dispatch_ust_registration interact with
149 * this queue and the wait/wake scheme.
151 static struct ust_cmd_queue ust_cmd_queue
;
154 * Pointer initialized before thread creation.
156 * This points to the tracing session list containing the session count and a
157 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
158 * MUST NOT be taken if you call a public function in session.c.
160 * The lock is nested inside the structure: session_list_ptr->lock. Please use
161 * session_lock_list and session_unlock_list for lock acquisition.
163 static struct ltt_session_list
*session_list_ptr
;
165 int ust_consumerd64_fd
= -1;
166 int ust_consumerd32_fd
= -1;
168 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
169 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
170 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
171 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
174 * Consumer daemon state which is changed when spawning it, killing it or in
175 * case of a fatal error.
177 enum consumerd_state
{
178 CONSUMER_STARTED
= 1,
179 CONSUMER_STOPPED
= 2,
184 * This consumer daemon state is used to validate if a client command will be
185 * able to reach the consumer. If not, the client is informed. For instance,
186 * doing a "lttng start" when the consumer state is set to ERROR will return an
187 * error to the client.
189 * The following example shows a possible race condition of this scheme:
191 * consumer thread error happens
193 * client cmd checks state -> still OK
194 * consumer thread exit, sets error
195 * client cmd try to talk to consumer
198 * However, since the consumer is a different daemon, we have no way of making
199 * sure the command will reach it safely even with this state flag. This is why
200 * we consider that up to the state validation during command processing, the
201 * command is safe. After that, we can not guarantee the correctness of the
202 * client request vis-a-vis the consumer.
204 static enum consumerd_state ust_consumerd_state
;
205 static enum consumerd_state kernel_consumerd_state
;
208 * Used to keep a unique index for each relayd socket created where this value
209 * is associated with streams on the consumer so it can match the right relayd
212 * This value should be incremented atomically for safety purposes and future
213 * possible concurrent access.
215 static unsigned int relayd_net_seq_idx
;
217 /* Used for the health monitoring of the session daemon. See health.h */
218 struct health_state health_thread_cmd
;
219 struct health_state health_thread_app_manage
;
220 struct health_state health_thread_app_reg
;
221 struct health_state health_thread_kernel
;
224 void setup_consumerd_path(void)
226 const char *bin
, *libdir
;
229 * Allow INSTALL_BIN_PATH to be used as a target path for the
230 * native architecture size consumer if CONFIG_CONSUMER*_PATH
231 * has not been defined.
233 #if (CAA_BITS_PER_LONG == 32)
234 if (!consumerd32_bin
[0]) {
235 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
237 if (!consumerd32_libdir
[0]) {
238 consumerd32_libdir
= INSTALL_LIB_PATH
;
240 #elif (CAA_BITS_PER_LONG == 64)
241 if (!consumerd64_bin
[0]) {
242 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
244 if (!consumerd64_libdir
[0]) {
245 consumerd64_libdir
= INSTALL_LIB_PATH
;
248 #error "Unknown bitness"
252 * runtime env. var. overrides the build default.
254 bin
= getenv("LTTNG_CONSUMERD32_BIN");
256 consumerd32_bin
= bin
;
258 bin
= getenv("LTTNG_CONSUMERD64_BIN");
260 consumerd64_bin
= bin
;
262 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
264 consumerd32_libdir
= libdir
;
266 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
268 consumerd64_libdir
= libdir
;
273 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
275 static int create_thread_poll_set(struct lttng_poll_event
*events
,
280 if (events
== NULL
|| size
== 0) {
285 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
291 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
303 * Check if the thread quit pipe was triggered.
305 * Return 1 if it was triggered else 0;
307 static int check_thread_quit_pipe(int fd
, uint32_t events
)
309 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
317 * Return group ID of the tracing group or -1 if not found.
319 static gid_t
allowed_group(void)
323 if (opt_tracing_group
) {
324 grp
= getgrnam(opt_tracing_group
);
326 grp
= getgrnam(default_tracing_group
);
336 * Init thread quit pipe.
338 * Return -1 on error or 0 if all pipes are created.
340 static int init_thread_quit_pipe(void)
344 ret
= pipe(thread_quit_pipe
);
346 PERROR("thread quit pipe");
350 for (i
= 0; i
< 2; i
++) {
351 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
363 * Complete teardown of a kernel session. This free all data structure related
364 * to a kernel session and update counter.
366 static void teardown_kernel_session(struct ltt_session
*session
)
368 if (!session
->kernel_session
) {
369 DBG3("No kernel session when tearing down session");
373 DBG("Tearing down kernel session");
376 * If a custom kernel consumer was registered, close the socket before
377 * tearing down the complete kernel session structure
379 if (kconsumer_data
.cmd_sock
>= 0 &&
380 session
->kernel_session
->consumer_fd
!= kconsumer_data
.cmd_sock
) {
381 lttcomm_close_unix_sock(session
->kernel_session
->consumer_fd
);
384 trace_kernel_destroy_session(session
->kernel_session
);
388 * Complete teardown of all UST sessions. This will free everything on his path
389 * and destroy the core essence of all ust sessions :)
391 static void teardown_ust_session(struct ltt_session
*session
)
395 if (!session
->ust_session
) {
396 DBG3("No UST session when tearing down session");
400 DBG("Tearing down UST session(s)");
402 ret
= ust_app_destroy_trace_all(session
->ust_session
);
404 ERR("Error in ust_app_destroy_trace_all");
407 trace_ust_destroy_session(session
->ust_session
);
411 * Stop all threads by closing the thread quit pipe.
413 static void stop_threads(void)
417 /* Stopping all threads */
418 DBG("Terminating all threads");
419 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
421 ERR("write error on thread quit pipe");
424 /* Dispatch thread */
425 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
426 futex_nto1_wake(&ust_cmd_queue
.futex
);
432 static void cleanup(void)
436 struct ltt_session
*sess
, *stmp
;
440 DBG("Removing %s directory", rundir
);
441 ret
= asprintf(&cmd
, "rm -rf %s", rundir
);
443 ERR("asprintf failed. Something is really wrong!");
446 /* Remove lttng run directory */
449 ERR("Unable to clean %s", rundir
);
453 DBG("Cleaning up all sessions");
455 /* Destroy session list mutex */
456 if (session_list_ptr
!= NULL
) {
457 pthread_mutex_destroy(&session_list_ptr
->lock
);
459 /* Cleanup ALL session */
460 cds_list_for_each_entry_safe(sess
, stmp
,
461 &session_list_ptr
->head
, list
) {
462 teardown_kernel_session(sess
);
463 teardown_ust_session(sess
);
468 DBG("Closing all UST sockets");
469 ust_app_clean_list();
471 pthread_mutex_destroy(&kconsumer_data
.pid_mutex
);
473 if (is_root
&& !opt_no_kernel
) {
474 DBG2("Closing kernel fd");
475 if (kernel_tracer_fd
>= 0) {
476 ret
= close(kernel_tracer_fd
);
481 DBG("Unloading kernel modules");
482 modprobe_remove_lttng_all();
484 utils_close_pipe(kernel_poll_pipe
);
485 utils_close_pipe(thread_quit_pipe
);
486 utils_close_pipe(apps_cmd_pipe
);
489 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
490 "Matthew, BEET driven development works!%c[%dm",
491 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
496 * Send data on a unix socket using the liblttsessiondcomm API.
498 * Return lttcomm error code.
500 static int send_unix_sock(int sock
, void *buf
, size_t len
)
502 /* Check valid length */
507 return lttcomm_send_unix_sock(sock
, buf
, len
);
511 * Free memory of a command context structure.
513 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
515 DBG("Clean command context structure");
517 if ((*cmd_ctx
)->llm
) {
518 free((*cmd_ctx
)->llm
);
520 if ((*cmd_ctx
)->lsm
) {
521 free((*cmd_ctx
)->lsm
);
529 * Notify UST applications using the shm mmap futex.
531 static int notify_ust_apps(int active
)
535 DBG("Notifying applications of session daemon state: %d", active
);
537 /* See shm.c for this call implying mmap, shm and futex calls */
538 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
539 if (wait_shm_mmap
== NULL
) {
543 /* Wake waiting process */
544 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
546 /* Apps notified successfully */
554 * Setup the outgoing data buffer for the response (llm) by allocating the
555 * right amount of memory and copying the original information from the lsm
558 * Return total size of the buffer pointed by buf.
560 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
566 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
567 if (cmd_ctx
->llm
== NULL
) {
573 /* Copy common data */
574 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
575 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
577 cmd_ctx
->llm
->data_size
= size
;
578 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
587 * Update the kernel poll set of all channel fd available over all tracing
588 * session. Add the wakeup pipe at the end of the set.
590 static int update_kernel_poll(struct lttng_poll_event
*events
)
593 struct ltt_session
*session
;
594 struct ltt_kernel_channel
*channel
;
596 DBG("Updating kernel poll set");
599 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
600 session_lock(session
);
601 if (session
->kernel_session
== NULL
) {
602 session_unlock(session
);
606 cds_list_for_each_entry(channel
,
607 &session
->kernel_session
->channel_list
.head
, list
) {
608 /* Add channel fd to the kernel poll set */
609 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
611 session_unlock(session
);
614 DBG("Channel fd %d added to kernel set", channel
->fd
);
616 session_unlock(session
);
618 session_unlock_list();
623 session_unlock_list();
628 * Find the channel fd from 'fd' over all tracing session. When found, check
629 * for new channel stream and send those stream fds to the kernel consumer.
631 * Useful for CPU hotplug feature.
633 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
636 struct ltt_session
*session
;
637 struct ltt_kernel_channel
*channel
;
639 DBG("Updating kernel streams for channel fd %d", fd
);
642 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
643 session_lock(session
);
644 if (session
->kernel_session
== NULL
) {
645 session_unlock(session
);
649 /* This is not suppose to be -1 but this is an extra security check */
650 if (session
->kernel_session
->consumer_fd
< 0) {
651 session
->kernel_session
->consumer_fd
= consumer_data
->cmd_sock
;
654 cds_list_for_each_entry(channel
,
655 &session
->kernel_session
->channel_list
.head
, list
) {
656 if (channel
->fd
== fd
) {
657 DBG("Channel found, updating kernel streams");
658 ret
= kernel_open_channel_stream(channel
);
664 * Have we already sent fds to the consumer? If yes, it means
665 * that tracing is started so it is safe to send our updated
668 if (session
->kernel_session
->consumer_fds_sent
== 1 &&
669 session
->kernel_session
->consumer
!= NULL
) {
670 ret
= kernel_consumer_send_channel_stream(
671 session
->kernel_session
->consumer_fd
, channel
,
672 session
->kernel_session
);
680 session_unlock(session
);
682 session_unlock_list();
686 session_unlock(session
);
687 session_unlock_list();
692 * For each tracing session, update newly registered apps.
694 static void update_ust_app(int app_sock
)
696 struct ltt_session
*sess
, *stmp
;
700 /* For all tracing session(s) */
701 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
703 if (sess
->ust_session
) {
704 ust_app_global_update(sess
->ust_session
, app_sock
);
706 session_unlock(sess
);
709 session_unlock_list();
713 * This thread manage event coming from the kernel.
715 * Features supported in this thread:
718 static void *thread_manage_kernel(void *data
)
720 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
721 uint32_t revents
, nb_fd
;
723 struct lttng_poll_event events
;
725 DBG("Thread manage kernel started");
727 health_code_update(&health_thread_kernel
);
729 ret
= create_thread_poll_set(&events
, 2);
731 goto error_poll_create
;
734 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
740 health_code_update(&health_thread_kernel
);
742 if (update_poll_flag
== 1) {
744 * Reset number of fd in the poll set. Always 2 since there is the thread
745 * quit pipe and the kernel pipe.
749 ret
= update_kernel_poll(&events
);
753 update_poll_flag
= 0;
756 nb_fd
= LTTNG_POLL_GETNB(&events
);
758 DBG("Thread kernel polling on %d fds", nb_fd
);
760 /* Zeroed the poll events */
761 lttng_poll_reset(&events
);
763 /* Poll infinite value of time */
765 health_poll_update(&health_thread_kernel
);
766 ret
= lttng_poll_wait(&events
, -1);
767 health_poll_update(&health_thread_kernel
);
770 * Restart interrupted system call.
772 if (errno
== EINTR
) {
776 } else if (ret
== 0) {
777 /* Should not happen since timeout is infinite */
778 ERR("Return value of poll is 0 with an infinite timeout.\n"
779 "This should not have happened! Continuing...");
783 for (i
= 0; i
< nb_fd
; i
++) {
784 /* Fetch once the poll data */
785 revents
= LTTNG_POLL_GETEV(&events
, i
);
786 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
788 health_code_update(&health_thread_kernel
);
790 /* Thread quit pipe has been closed. Killing thread. */
791 ret
= check_thread_quit_pipe(pollfd
, revents
);
797 /* Check for data on kernel pipe */
798 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
799 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
800 update_poll_flag
= 1;
804 * New CPU detected by the kernel. Adding kernel stream to
805 * kernel session and updating the kernel consumer
807 if (revents
& LPOLLIN
) {
808 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
814 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
815 * and unregister kernel stream at this point.
824 lttng_poll_clean(&events
);
827 health_error(&health_thread_kernel
);
828 ERR("Health error occurred in %s", __func__
);
830 health_exit(&health_thread_kernel
);
831 DBG("Kernel thread dying");
836 * This thread manage the consumer error sent back to the session daemon.
838 static void *thread_manage_consumer(void *data
)
840 int sock
= -1, i
, ret
, pollfd
, err
= -1;
841 uint32_t revents
, nb_fd
;
842 enum lttcomm_return_code code
;
843 struct lttng_poll_event events
;
844 struct consumer_data
*consumer_data
= data
;
846 DBG("[thread] Manage consumer started");
848 health_code_update(&consumer_data
->health
);
850 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
856 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
857 * Nothing more will be added to this poll set.
859 ret
= create_thread_poll_set(&events
, 2);
864 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
869 nb_fd
= LTTNG_POLL_GETNB(&events
);
871 health_code_update(&consumer_data
->health
);
873 /* Inifinite blocking call, waiting for transmission */
875 health_poll_update(&consumer_data
->health
);
876 ret
= lttng_poll_wait(&events
, -1);
877 health_poll_update(&consumer_data
->health
);
880 * Restart interrupted system call.
882 if (errno
== EINTR
) {
888 for (i
= 0; i
< nb_fd
; i
++) {
889 /* Fetch once the poll data */
890 revents
= LTTNG_POLL_GETEV(&events
, i
);
891 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
893 health_code_update(&consumer_data
->health
);
895 /* Thread quit pipe has been closed. Killing thread. */
896 ret
= check_thread_quit_pipe(pollfd
, revents
);
902 /* Event on the registration socket */
903 if (pollfd
== consumer_data
->err_sock
) {
904 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
905 ERR("consumer err socket poll error");
911 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
916 health_code_update(&consumer_data
->health
);
918 DBG2("Receiving code from consumer err_sock");
920 /* Getting status code from kconsumerd */
921 ret
= lttcomm_recv_unix_sock(sock
, &code
,
922 sizeof(enum lttcomm_return_code
));
927 health_code_update(&consumer_data
->health
);
929 if (code
== CONSUMERD_COMMAND_SOCK_READY
) {
930 consumer_data
->cmd_sock
=
931 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
932 if (consumer_data
->cmd_sock
< 0) {
933 sem_post(&consumer_data
->sem
);
934 PERROR("consumer connect");
937 /* Signal condition to tell that the kconsumerd is ready */
938 sem_post(&consumer_data
->sem
);
939 DBG("consumer command socket ready");
941 ERR("consumer error when waiting for SOCK_READY : %s",
942 lttcomm_get_readable_code(-code
));
946 /* Remove the kconsumerd error sock since we've established a connexion */
947 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
952 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
957 health_code_update(&consumer_data
->health
);
959 /* Update number of fd */
960 nb_fd
= LTTNG_POLL_GETNB(&events
);
962 /* Inifinite blocking call, waiting for transmission */
964 health_poll_update(&consumer_data
->health
);
965 ret
= lttng_poll_wait(&events
, -1);
966 health_poll_update(&consumer_data
->health
);
969 * Restart interrupted system call.
971 if (errno
== EINTR
) {
977 for (i
= 0; i
< nb_fd
; i
++) {
978 /* Fetch once the poll data */
979 revents
= LTTNG_POLL_GETEV(&events
, i
);
980 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
982 health_code_update(&consumer_data
->health
);
984 /* Thread quit pipe has been closed. Killing thread. */
985 ret
= check_thread_quit_pipe(pollfd
, revents
);
991 /* Event on the kconsumerd socket */
992 if (pollfd
== sock
) {
993 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
994 ERR("consumer err socket second poll error");
1000 health_code_update(&consumer_data
->health
);
1002 /* Wait for any kconsumerd error */
1003 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1004 sizeof(enum lttcomm_return_code
));
1006 ERR("consumer closed the command socket");
1010 ERR("consumer return code : %s", lttcomm_get_readable_code(-code
));
1014 /* Immediately set the consumerd state to stopped */
1015 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1016 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1017 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1018 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1019 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1021 /* Code flow error... */
1025 if (consumer_data
->err_sock
>= 0) {
1026 ret
= close(consumer_data
->err_sock
);
1031 if (consumer_data
->cmd_sock
>= 0) {
1032 ret
= close(consumer_data
->cmd_sock
);
1044 unlink(consumer_data
->err_unix_sock_path
);
1045 unlink(consumer_data
->cmd_unix_sock_path
);
1046 consumer_data
->pid
= 0;
1048 lttng_poll_clean(&events
);
1052 health_error(&consumer_data
->health
);
1053 ERR("Health error occurred in %s", __func__
);
1055 health_exit(&consumer_data
->health
);
1056 DBG("consumer thread cleanup completed");
1062 * This thread manage application communication.
1064 static void *thread_manage_apps(void *data
)
1066 int i
, ret
, pollfd
, err
= -1;
1067 uint32_t revents
, nb_fd
;
1068 struct ust_command ust_cmd
;
1069 struct lttng_poll_event events
;
1071 DBG("[thread] Manage application started");
1073 rcu_register_thread();
1074 rcu_thread_online();
1076 health_code_update(&health_thread_app_manage
);
1078 ret
= create_thread_poll_set(&events
, 2);
1080 goto error_poll_create
;
1083 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1088 health_code_update(&health_thread_app_manage
);
1091 /* Zeroed the events structure */
1092 lttng_poll_reset(&events
);
1094 nb_fd
= LTTNG_POLL_GETNB(&events
);
1096 DBG("Apps thread polling on %d fds", nb_fd
);
1098 /* Inifinite blocking call, waiting for transmission */
1100 health_poll_update(&health_thread_app_manage
);
1101 ret
= lttng_poll_wait(&events
, -1);
1102 health_poll_update(&health_thread_app_manage
);
1105 * Restart interrupted system call.
1107 if (errno
== EINTR
) {
1113 for (i
= 0; i
< nb_fd
; i
++) {
1114 /* Fetch once the poll data */
1115 revents
= LTTNG_POLL_GETEV(&events
, i
);
1116 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1118 health_code_update(&health_thread_app_manage
);
1120 /* Thread quit pipe has been closed. Killing thread. */
1121 ret
= check_thread_quit_pipe(pollfd
, revents
);
1127 /* Inspect the apps cmd pipe */
1128 if (pollfd
== apps_cmd_pipe
[0]) {
1129 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1130 ERR("Apps command pipe error");
1132 } else if (revents
& LPOLLIN
) {
1134 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
1135 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
1136 PERROR("read apps cmd pipe");
1140 health_code_update(&health_thread_app_manage
);
1142 /* Register applicaton to the session daemon */
1143 ret
= ust_app_register(&ust_cmd
.reg_msg
,
1145 if (ret
== -ENOMEM
) {
1147 } else if (ret
< 0) {
1151 health_code_update(&health_thread_app_manage
);
1154 * Validate UST version compatibility.
1156 ret
= ust_app_validate_version(ust_cmd
.sock
);
1159 * Add channel(s) and event(s) to newly registered apps
1160 * from lttng global UST domain.
1162 update_ust_app(ust_cmd
.sock
);
1165 health_code_update(&health_thread_app_manage
);
1167 ret
= ust_app_register_done(ust_cmd
.sock
);
1170 * If the registration is not possible, we simply
1171 * unregister the apps and continue
1173 ust_app_unregister(ust_cmd
.sock
);
1176 * We just need here to monitor the close of the UST
1177 * socket and poll set monitor those by default.
1178 * Listen on POLLIN (even if we never expect any
1179 * data) to ensure that hangup wakes us.
1181 ret
= lttng_poll_add(&events
, ust_cmd
.sock
, LPOLLIN
);
1186 DBG("Apps with sock %d added to poll set",
1190 health_code_update(&health_thread_app_manage
);
1196 * At this point, we know that a registered application made
1197 * the event at poll_wait.
1199 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1200 /* Removing from the poll set */
1201 ret
= lttng_poll_del(&events
, pollfd
);
1206 /* Socket closed on remote end. */
1207 ust_app_unregister(pollfd
);
1212 health_code_update(&health_thread_app_manage
);
1218 lttng_poll_clean(&events
);
1221 health_error(&health_thread_app_manage
);
1222 ERR("Health error occurred in %s", __func__
);
1224 health_exit(&health_thread_app_manage
);
1225 DBG("Application communication apps thread cleanup complete");
1226 rcu_thread_offline();
1227 rcu_unregister_thread();
1232 * Dispatch request from the registration threads to the application
1233 * communication thread.
1235 static void *thread_dispatch_ust_registration(void *data
)
1238 struct cds_wfq_node
*node
;
1239 struct ust_command
*ust_cmd
= NULL
;
1241 DBG("[thread] Dispatch UST command started");
1243 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1244 /* Atomically prepare the queue futex */
1245 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1248 /* Dequeue command for registration */
1249 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1251 DBG("Woken up but nothing in the UST command queue");
1252 /* Continue thread execution */
1256 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1258 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1259 " gid:%d sock:%d name:%s (version %d.%d)",
1260 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1261 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1262 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1263 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1265 * Inform apps thread of the new application registration. This
1266 * call is blocking so we can be assured that the data will be read
1267 * at some point in time or wait to the end of the world :)
1269 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1270 sizeof(struct ust_command
));
1272 PERROR("write apps cmd pipe");
1273 if (errno
== EBADF
) {
1275 * We can't inform the application thread to process
1276 * registration. We will exit or else application
1277 * registration will not occur and tracing will never
1284 } while (node
!= NULL
);
1286 /* Futex wait on queue. Blocking call on futex() */
1287 futex_nto1_wait(&ust_cmd_queue
.futex
);
1291 DBG("Dispatch thread dying");
1296 * This thread manage application registration.
1298 static void *thread_registration_apps(void *data
)
1300 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1301 uint32_t revents
, nb_fd
;
1302 struct lttng_poll_event events
;
1304 * Get allocated in this thread, enqueued to a global queue, dequeued and
1305 * freed in the manage apps thread.
1307 struct ust_command
*ust_cmd
= NULL
;
1309 DBG("[thread] Manage application registration started");
1311 ret
= lttcomm_listen_unix_sock(apps_sock
);
1317 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1318 * more will be added to this poll set.
1320 ret
= create_thread_poll_set(&events
, 2);
1322 goto error_create_poll
;
1325 /* Add the application registration socket */
1326 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1328 goto error_poll_add
;
1331 /* Notify all applications to register */
1332 ret
= notify_ust_apps(1);
1334 ERR("Failed to notify applications or create the wait shared memory.\n"
1335 "Execution continues but there might be problem for already\n"
1336 "running applications that wishes to register.");
1340 DBG("Accepting application registration");
1342 nb_fd
= LTTNG_POLL_GETNB(&events
);
1344 /* Inifinite blocking call, waiting for transmission */
1346 health_poll_update(&health_thread_app_reg
);
1347 ret
= lttng_poll_wait(&events
, -1);
1348 health_poll_update(&health_thread_app_reg
);
1351 * Restart interrupted system call.
1353 if (errno
== EINTR
) {
1359 for (i
= 0; i
< nb_fd
; i
++) {
1360 health_code_update(&health_thread_app_reg
);
1362 /* Fetch once the poll data */
1363 revents
= LTTNG_POLL_GETEV(&events
, i
);
1364 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1366 /* Thread quit pipe has been closed. Killing thread. */
1367 ret
= check_thread_quit_pipe(pollfd
, revents
);
1373 /* Event on the registration socket */
1374 if (pollfd
== apps_sock
) {
1375 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1376 ERR("Register apps socket poll error");
1378 } else if (revents
& LPOLLIN
) {
1379 sock
= lttcomm_accept_unix_sock(apps_sock
);
1384 /* Create UST registration command for enqueuing */
1385 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1386 if (ust_cmd
== NULL
) {
1387 PERROR("ust command zmalloc");
1392 * Using message-based transmissions to ensure we don't
1393 * have to deal with partially received messages.
1395 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1397 ERR("Exhausted file descriptors allowed for applications.");
1406 health_code_update(&health_thread_app_reg
);
1407 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1408 sizeof(struct ust_register_msg
));
1409 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1411 PERROR("lttcomm_recv_unix_sock register apps");
1413 ERR("Wrong size received on apps register");
1420 lttng_fd_put(LTTNG_FD_APPS
, 1);
1424 health_code_update(&health_thread_app_reg
);
1426 ust_cmd
->sock
= sock
;
1429 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1430 " gid:%d sock:%d name:%s (version %d.%d)",
1431 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1432 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1433 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1434 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1437 * Lock free enqueue the registration request. The red pill
1438 * has been taken! This apps will be part of the *system*.
1440 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1443 * Wake the registration queue futex. Implicit memory
1444 * barrier with the exchange in cds_wfq_enqueue.
1446 futex_nto1_wake(&ust_cmd_queue
.futex
);
1455 health_error(&health_thread_app_reg
);
1456 ERR("Health error occurred in %s", __func__
);
1458 health_exit(&health_thread_app_reg
);
1460 /* Notify that the registration thread is gone */
1463 if (apps_sock
>= 0) {
1464 ret
= close(apps_sock
);
1474 lttng_fd_put(LTTNG_FD_APPS
, 1);
1476 unlink(apps_unix_sock_path
);
1479 lttng_poll_clean(&events
);
1482 DBG("UST Registration thread cleanup complete");
1488 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1489 * exec or it will fails.
1491 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
1494 struct timespec timeout
;
1496 timeout
.tv_sec
= DEFAULT_SEM_WAIT_TIMEOUT
;
1497 timeout
.tv_nsec
= 0;
1499 /* Setup semaphore */
1500 ret
= sem_init(&consumer_data
->sem
, 0, 0);
1502 PERROR("sem_init consumer semaphore");
1506 ret
= pthread_create(&consumer_data
->thread
, NULL
,
1507 thread_manage_consumer
, consumer_data
);
1509 PERROR("pthread_create consumer");
1514 /* Get time for sem_timedwait absolute timeout */
1515 ret
= clock_gettime(CLOCK_REALTIME
, &timeout
);
1517 PERROR("clock_gettime spawn consumer");
1518 /* Infinite wait for the kconsumerd thread to be ready */
1519 ret
= sem_wait(&consumer_data
->sem
);
1521 /* Normal timeout if the gettime was successful */
1522 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
1523 ret
= sem_timedwait(&consumer_data
->sem
, &timeout
);
1527 if (errno
== ETIMEDOUT
) {
1529 * Call has timed out so we kill the kconsumerd_thread and return
1532 ERR("The consumer thread was never ready. Killing it");
1533 ret
= pthread_cancel(consumer_data
->thread
);
1535 PERROR("pthread_cancel consumer thread");
1538 PERROR("semaphore wait failed consumer thread");
1543 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1544 if (consumer_data
->pid
== 0) {
1545 ERR("Kconsumerd did not start");
1546 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1549 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1558 * Join consumer thread
1560 static int join_consumer_thread(struct consumer_data
*consumer_data
)
1565 if (consumer_data
->pid
!= 0) {
1566 ret
= kill(consumer_data
->pid
, SIGTERM
);
1568 ERR("Error killing consumer daemon");
1571 return pthread_join(consumer_data
->thread
, &status
);
1578 * Fork and exec a consumer daemon (consumerd).
1580 * Return pid if successful else -1.
1582 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
1586 const char *consumer_to_use
;
1587 const char *verbosity
;
1590 DBG("Spawning consumerd");
1597 if (opt_verbose_consumer
) {
1598 verbosity
= "--verbose";
1600 verbosity
= "--quiet";
1602 switch (consumer_data
->type
) {
1603 case LTTNG_CONSUMER_KERNEL
:
1605 * Find out which consumerd to execute. We will first try the
1606 * 64-bit path, then the sessiond's installation directory, and
1607 * fallback on the 32-bit one,
1609 DBG3("Looking for a kernel consumer at these locations:");
1610 DBG3(" 1) %s", consumerd64_bin
);
1611 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
1612 DBG3(" 3) %s", consumerd32_bin
);
1613 if (stat(consumerd64_bin
, &st
) == 0) {
1614 DBG3("Found location #1");
1615 consumer_to_use
= consumerd64_bin
;
1616 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
1617 DBG3("Found location #2");
1618 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
1619 } else if (stat(consumerd32_bin
, &st
) == 0) {
1620 DBG3("Found location #3");
1621 consumer_to_use
= consumerd32_bin
;
1623 DBG("Could not find any valid consumerd executable");
1626 DBG("Using kernel consumer at: %s", consumer_to_use
);
1627 execl(consumer_to_use
,
1628 "lttng-consumerd", verbosity
, "-k",
1629 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1630 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1633 case LTTNG_CONSUMER64_UST
:
1635 char *tmpnew
= NULL
;
1637 if (consumerd64_libdir
[0] != '\0') {
1641 tmp
= getenv("LD_LIBRARY_PATH");
1645 tmplen
= strlen("LD_LIBRARY_PATH=")
1646 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
1647 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1652 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1653 strcat(tmpnew
, consumerd64_libdir
);
1654 if (tmp
[0] != '\0') {
1655 strcat(tmpnew
, ":");
1656 strcat(tmpnew
, tmp
);
1658 ret
= putenv(tmpnew
);
1664 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
1665 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
1666 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1667 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1669 if (consumerd64_libdir
[0] != '\0') {
1677 case LTTNG_CONSUMER32_UST
:
1679 char *tmpnew
= NULL
;
1681 if (consumerd32_libdir
[0] != '\0') {
1685 tmp
= getenv("LD_LIBRARY_PATH");
1689 tmplen
= strlen("LD_LIBRARY_PATH=")
1690 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
1691 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1696 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1697 strcat(tmpnew
, consumerd32_libdir
);
1698 if (tmp
[0] != '\0') {
1699 strcat(tmpnew
, ":");
1700 strcat(tmpnew
, tmp
);
1702 ret
= putenv(tmpnew
);
1708 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
1709 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
1710 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1711 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1713 if (consumerd32_libdir
[0] != '\0') {
1722 PERROR("unknown consumer type");
1726 PERROR("kernel start consumer exec");
1729 } else if (pid
> 0) {
1732 PERROR("start consumer fork");
1740 * Spawn the consumerd daemon and session daemon thread.
1742 static int start_consumerd(struct consumer_data
*consumer_data
)
1746 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1747 if (consumer_data
->pid
!= 0) {
1748 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1752 ret
= spawn_consumerd(consumer_data
);
1754 ERR("Spawning consumerd failed");
1755 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1759 /* Setting up the consumer_data pid */
1760 consumer_data
->pid
= ret
;
1761 DBG2("Consumer pid %d", consumer_data
->pid
);
1762 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1764 DBG2("Spawning consumer control thread");
1765 ret
= spawn_consumer_thread(consumer_data
);
1767 ERR("Fatal error spawning consumer control thread");
1779 * Compute health status of each consumer. If one of them is zero (bad
1780 * state), we return 0.
1782 static int check_consumer_health(void)
1786 ret
= health_check_state(&kconsumer_data
.health
) &&
1787 health_check_state(&ustconsumer32_data
.health
) &&
1788 health_check_state(&ustconsumer64_data
.health
);
1790 DBG3("Health consumer check %d", ret
);
1796 * Check version of the lttng-modules.
1798 static int validate_lttng_modules_version(void)
1800 return kernel_validate_version(kernel_tracer_fd
);
1804 * Setup necessary data for kernel tracer action.
1806 static int init_kernel_tracer(void)
1810 /* Modprobe lttng kernel modules */
1811 ret
= modprobe_lttng_control();
1816 /* Open debugfs lttng */
1817 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1818 if (kernel_tracer_fd
< 0) {
1819 DBG("Failed to open %s", module_proc_lttng
);
1824 /* Validate kernel version */
1825 ret
= validate_lttng_modules_version();
1830 ret
= modprobe_lttng_data();
1835 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1839 modprobe_remove_lttng_control();
1840 ret
= close(kernel_tracer_fd
);
1844 kernel_tracer_fd
= -1;
1845 return LTTCOMM_KERN_VERSION
;
1848 ret
= close(kernel_tracer_fd
);
1854 modprobe_remove_lttng_control();
1857 WARN("No kernel tracer available");
1858 kernel_tracer_fd
= -1;
1860 return LTTCOMM_NEED_ROOT_SESSIOND
;
1862 return LTTCOMM_KERN_NA
;
1867 * Init tracing by creating trace directory and sending fds kernel consumer.
1869 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
1873 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
1875 * Assign default kernel consumer socket if no consumer assigned to the
1876 * kernel session. At this point, it's NOT supposed to be -1 but this is
1877 * an extra security check.
1879 if (session
->consumer_fd
< 0) {
1880 session
->consumer_fd
= kconsumer_data
.cmd_sock
;
1883 ret
= kernel_consumer_send_session(session
->consumer_fd
, session
);
1885 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1895 * Create a socket to the relayd using the URI.
1897 * On success, the relayd_sock pointer is set to the created socket.
1898 * Else, it is untouched and an lttcomm error code is returned.
1900 static int create_connect_relayd(struct consumer_output
*output
,
1901 const char *session_name
, struct lttng_uri
*uri
,
1902 struct lttcomm_sock
**relayd_sock
)
1905 struct lttcomm_sock
*sock
;
1907 /* Create socket object from URI */
1908 sock
= lttcomm_alloc_sock_from_uri(uri
);
1910 ret
= LTTCOMM_FATAL
;
1914 ret
= lttcomm_create_sock(sock
);
1916 ret
= LTTCOMM_FATAL
;
1920 /* Connect to relayd so we can proceed with a session creation. */
1921 ret
= relayd_connect(sock
);
1923 ERR("Unable to reach lttng-relayd");
1924 ret
= LTTCOMM_RELAYD_SESSION_FAIL
;
1928 /* Create socket for control stream. */
1929 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
1930 DBG3("Creating relayd stream socket from URI");
1932 /* Check relayd version */
1933 ret
= relayd_version_check(sock
, LTTNG_UST_COMM_MAJOR
, 0);
1935 ret
= LTTCOMM_RELAYD_VERSION_FAIL
;
1938 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
1939 DBG3("Creating relayd data socket from URI");
1941 /* Command is not valid */
1942 ERR("Relayd invalid stream type: %d", uri
->stype
);
1943 ret
= LTTCOMM_INVALID
;
1947 *relayd_sock
= sock
;
1953 (void) relayd_close(sock
);
1957 lttcomm_destroy_sock(sock
);
1964 * Connect to the relayd using URI and send the socket to the right consumer.
1966 static int send_socket_relayd_consumer(int domain
, struct ltt_session
*session
,
1967 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
1971 struct lttcomm_sock
*sock
= NULL
;
1973 /* Set the network sequence index if not set. */
1974 if (consumer
->net_seq_index
== -1) {
1976 * Increment net_seq_idx because we are about to transfer the
1977 * new relayd socket to the consumer.
1979 uatomic_inc(&relayd_net_seq_idx
);
1980 /* Assign unique key so the consumer can match streams */
1981 consumer
->net_seq_index
= uatomic_read(&relayd_net_seq_idx
);
1984 /* Connect to relayd and make version check if uri is the control. */
1985 ret
= create_connect_relayd(consumer
, session
->name
, relayd_uri
, &sock
);
1986 if (ret
!= LTTCOMM_OK
) {
1990 /* If the control socket is connected, network session is ready */
1991 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1992 session
->net_handle
= 1;
1995 /* Send relayd socket to consumer. */
1996 ret
= consumer_send_relayd_socket(consumer_fd
, sock
,
1997 consumer
, relayd_uri
->stype
);
1999 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
2006 * Close socket which was dup on the consumer side. The session daemon does
2007 * NOT keep track of the relayd socket(s) once transfer to the consumer.
2012 (void) relayd_close(sock
);
2013 lttcomm_destroy_sock(sock
);
2020 * Send both relayd sockets to a specific consumer and domain. This is a
2021 * helper function to facilitate sending the information to the consumer for a
2024 static int send_sockets_relayd_consumer(int domain
,
2025 struct ltt_session
*session
, struct consumer_output
*consumer
, int fd
)
2029 /* Sending control relayd socket. */
2030 ret
= send_socket_relayd_consumer(domain
, session
,
2031 &consumer
->dst
.net
.control
, consumer
, fd
);
2032 if (ret
!= LTTCOMM_OK
) {
2036 /* Sending data relayd socket. */
2037 ret
= send_socket_relayd_consumer(domain
, session
,
2038 &consumer
->dst
.net
.data
, consumer
, fd
);
2039 if (ret
!= LTTCOMM_OK
) {
2048 * Setup relayd connections for a tracing session. First creates the socket to
2049 * the relayd and send them to the right domain consumer. Consumer type MUST be
2052 static int setup_relayd(struct ltt_session
*session
)
2054 int ret
= LTTCOMM_OK
;
2055 struct ltt_ust_session
*usess
;
2056 struct ltt_kernel_session
*ksess
;
2060 usess
= session
->ust_session
;
2061 ksess
= session
->kernel_session
;
2063 DBG2("Setting relayd for session %s", session
->name
);
2065 if (usess
&& usess
->consumer
->sock
== -1 &&
2066 usess
->consumer
->type
== CONSUMER_DST_NET
&&
2067 usess
->consumer
->enabled
) {
2068 /* Setup relayd for 64 bits consumer */
2069 if (ust_consumerd64_fd
>= 0) {
2070 send_sockets_relayd_consumer(LTTNG_DOMAIN_UST
, session
,
2071 usess
->consumer
, ust_consumerd64_fd
);
2072 if (ret
!= LTTCOMM_OK
) {
2077 /* Setup relayd for 32 bits consumer */
2078 if (ust_consumerd32_fd
>= 0) {
2079 send_sockets_relayd_consumer(LTTNG_DOMAIN_UST
, session
,
2080 usess
->consumer
, ust_consumerd32_fd
);
2081 if (ret
!= LTTCOMM_OK
) {
2085 } else if (ksess
&& ksess
->consumer
->sock
== -1 &&
2086 ksess
->consumer
->type
== CONSUMER_DST_NET
&&
2087 ksess
->consumer
->enabled
) {
2088 send_sockets_relayd_consumer(LTTNG_DOMAIN_KERNEL
, session
,
2089 ksess
->consumer
, ksess
->consumer_fd
);
2090 if (ret
!= LTTCOMM_OK
) {
2100 * Copy consumer output from the tracing session to the domain session. The
2101 * function also applies the right modification on a per domain basis for the
2102 * trace files destination directory.
2104 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2107 const char *dir_name
;
2108 struct consumer_output
*consumer
;
2111 case LTTNG_DOMAIN_KERNEL
:
2112 DBG3("Copying tracing session consumer output in kernel session");
2113 session
->kernel_session
->consumer
=
2114 consumer_copy_output(session
->consumer
);
2115 /* Ease our life a bit for the next part */
2116 consumer
= session
->kernel_session
->consumer
;
2117 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2119 case LTTNG_DOMAIN_UST
:
2120 DBG3("Copying tracing session consumer output in UST session");
2121 session
->ust_session
->consumer
=
2122 consumer_copy_output(session
->consumer
);
2123 /* Ease our life a bit for the next part */
2124 consumer
= session
->ust_session
->consumer
;
2125 dir_name
= DEFAULT_UST_TRACE_DIR
;
2128 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2132 /* Append correct directory to subdir */
2133 strncat(consumer
->subdir
, dir_name
, sizeof(consumer
->subdir
));
2134 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2136 /* Add default trace directory name */
2137 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
2138 strncat(consumer
->dst
.trace_path
, dir_name
,
2139 sizeof(consumer
->dst
.trace_path
));
2149 * Create an UST session and add it to the session ust list.
2151 static int create_ust_session(struct ltt_session
*session
,
2152 struct lttng_domain
*domain
)
2155 struct ltt_ust_session
*lus
= NULL
;
2158 assert(session
->consumer
);
2160 switch (domain
->type
) {
2161 case LTTNG_DOMAIN_UST
:
2164 ERR("Unknown UST domain on create session %d", domain
->type
);
2165 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2169 DBG("Creating UST session");
2171 lus
= trace_ust_create_session(session
->path
, session
->id
, domain
);
2173 ret
= LTTCOMM_UST_SESS_FAIL
;
2177 if (session
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2178 ret
= run_as_mkdir_recursive(lus
->pathname
, S_IRWXU
| S_IRWXG
,
2179 session
->uid
, session
->gid
);
2181 if (ret
!= -EEXIST
) {
2182 ERR("Trace directory creation error");
2183 ret
= LTTCOMM_UST_SESS_FAIL
;
2189 lus
->uid
= session
->uid
;
2190 lus
->gid
= session
->gid
;
2191 session
->ust_session
= lus
;
2193 /* Copy session output to the newly created UST session */
2194 ret
= copy_session_consumer(domain
->type
, session
);
2195 if (ret
!= LTTCOMM_OK
) {
2203 session
->ust_session
= NULL
;
2208 * Create a kernel tracer session then create the default channel.
2210 static int create_kernel_session(struct ltt_session
*session
)
2214 DBG("Creating kernel session");
2216 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2218 ret
= LTTCOMM_KERN_SESS_FAIL
;
2222 /* Set kernel consumer socket fd */
2223 if (kconsumer_data
.cmd_sock
>= 0) {
2224 session
->kernel_session
->consumer_fd
= kconsumer_data
.cmd_sock
;
2227 /* Copy session output to the newly created Kernel session */
2228 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2229 if (ret
!= LTTCOMM_OK
) {
2233 /* Create directory(ies) on local filesystem. */
2234 if (session
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2235 ret
= run_as_mkdir_recursive(
2236 session
->kernel_session
->consumer
->dst
.trace_path
,
2237 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2239 if (ret
!= -EEXIST
) {
2240 ERR("Trace directory creation error");
2246 session
->kernel_session
->uid
= session
->uid
;
2247 session
->kernel_session
->gid
= session
->gid
;
2252 trace_kernel_destroy_session(session
->kernel_session
);
2253 session
->kernel_session
= NULL
;
2258 * Check if the UID or GID match the session. Root user has access to all
2261 static int session_access_ok(struct ltt_session
*session
, uid_t uid
, gid_t gid
)
2263 if (uid
!= session
->uid
&& gid
!= session
->gid
&& uid
!= 0) {
2271 * Count number of session permitted by uid/gid.
2273 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2276 struct ltt_session
*session
;
2278 DBG("Counting number of available session for UID %d GID %d",
2280 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2282 * Only list the sessions the user can control.
2284 if (!session_access_ok(session
, uid
, gid
)) {
2293 * Using the session list, filled a lttng_session array to send back to the
2294 * client for session listing.
2296 * The session list lock MUST be acquired before calling this function. Use
2297 * session_lock_list() and session_unlock_list().
2299 static void list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2303 struct ltt_session
*session
;
2305 DBG("Getting all available session for UID %d GID %d",
2308 * Iterate over session list and append data after the control struct in
2311 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2313 * Only list the sessions the user can control.
2315 if (!session_access_ok(session
, uid
, gid
)) {
2318 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
2319 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
2320 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2321 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2322 sessions
[i
].enabled
= session
->enabled
;
2328 * Fill lttng_channel array of all channels.
2330 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
2331 struct lttng_channel
*channels
)
2334 struct ltt_kernel_channel
*kchan
;
2336 DBG("Listing channels for session %s", session
->name
);
2339 case LTTNG_DOMAIN_KERNEL
:
2340 /* Kernel channels */
2341 if (session
->kernel_session
!= NULL
) {
2342 cds_list_for_each_entry(kchan
,
2343 &session
->kernel_session
->channel_list
.head
, list
) {
2344 /* Copy lttng_channel struct to array */
2345 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
2346 channels
[i
].enabled
= kchan
->enabled
;
2351 case LTTNG_DOMAIN_UST
:
2353 struct lttng_ht_iter iter
;
2354 struct ltt_ust_channel
*uchan
;
2356 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
2357 &iter
.iter
, uchan
, node
.node
) {
2358 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
2359 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
2360 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2361 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2362 channels
[i
].attr
.switch_timer_interval
=
2363 uchan
->attr
.switch_timer_interval
;
2364 channels
[i
].attr
.read_timer_interval
=
2365 uchan
->attr
.read_timer_interval
;
2366 channels
[i
].enabled
= uchan
->enabled
;
2367 switch (uchan
->attr
.output
) {
2368 case LTTNG_UST_MMAP
:
2370 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
2383 * Create a list of ust global domain events.
2385 static int list_lttng_ust_global_events(char *channel_name
,
2386 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
2389 unsigned int nb_event
= 0;
2390 struct lttng_ht_iter iter
;
2391 struct lttng_ht_node_str
*node
;
2392 struct ltt_ust_channel
*uchan
;
2393 struct ltt_ust_event
*uevent
;
2394 struct lttng_event
*tmp
;
2396 DBG("Listing UST global events for channel %s", channel_name
);
2400 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
2401 node
= lttng_ht_iter_get_node_str(&iter
);
2403 ret
= -LTTCOMM_UST_CHAN_NOT_FOUND
;
2407 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
2409 nb_event
+= lttng_ht_get_count(uchan
->events
);
2411 if (nb_event
== 0) {
2416 DBG3("Listing UST global %d events", nb_event
);
2418 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
2420 ret
= -LTTCOMM_FATAL
;
2424 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
2425 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
2426 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
2427 tmp
[i
].enabled
= uevent
->enabled
;
2428 switch (uevent
->attr
.instrumentation
) {
2429 case LTTNG_UST_TRACEPOINT
:
2430 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2432 case LTTNG_UST_PROBE
:
2433 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
2435 case LTTNG_UST_FUNCTION
:
2436 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
2439 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
2440 switch (uevent
->attr
.loglevel_type
) {
2441 case LTTNG_UST_LOGLEVEL_ALL
:
2442 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2444 case LTTNG_UST_LOGLEVEL_RANGE
:
2445 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
2447 case LTTNG_UST_LOGLEVEL_SINGLE
:
2448 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
2451 if (uevent
->filter
) {
2466 * Fill lttng_event array of all kernel events in the channel.
2468 static int list_lttng_kernel_events(char *channel_name
,
2469 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
2472 unsigned int nb_event
;
2473 struct ltt_kernel_event
*event
;
2474 struct ltt_kernel_channel
*kchan
;
2476 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
2477 if (kchan
== NULL
) {
2478 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2482 nb_event
= kchan
->event_count
;
2484 DBG("Listing events for channel %s", kchan
->channel
->name
);
2486 if (nb_event
== 0) {
2491 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
2492 if (*events
== NULL
) {
2493 ret
= LTTCOMM_FATAL
;
2497 /* Kernel channels */
2498 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
2499 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
2500 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
2501 (*events
)[i
].enabled
= event
->enabled
;
2502 switch (event
->event
->instrumentation
) {
2503 case LTTNG_KERNEL_TRACEPOINT
:
2504 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2506 case LTTNG_KERNEL_KPROBE
:
2507 case LTTNG_KERNEL_KRETPROBE
:
2508 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
2509 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
2510 sizeof(struct lttng_kernel_kprobe
));
2512 case LTTNG_KERNEL_FUNCTION
:
2513 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
2514 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
2515 sizeof(struct lttng_kernel_function
));
2517 case LTTNG_KERNEL_NOOP
:
2518 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
2520 case LTTNG_KERNEL_SYSCALL
:
2521 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
2523 case LTTNG_KERNEL_ALL
:
2537 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
2539 static int cmd_disable_channel(struct ltt_session
*session
,
2540 int domain
, char *channel_name
)
2543 struct ltt_ust_session
*usess
;
2545 usess
= session
->ust_session
;
2548 case LTTNG_DOMAIN_KERNEL
:
2550 ret
= channel_kernel_disable(session
->kernel_session
,
2552 if (ret
!= LTTCOMM_OK
) {
2556 kernel_wait_quiescent(kernel_tracer_fd
);
2559 case LTTNG_DOMAIN_UST
:
2561 struct ltt_ust_channel
*uchan
;
2562 struct lttng_ht
*chan_ht
;
2564 chan_ht
= usess
->domain_global
.channels
;
2566 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
2567 if (uchan
== NULL
) {
2568 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2572 ret
= channel_ust_disable(usess
, domain
, uchan
);
2573 if (ret
!= LTTCOMM_OK
) {
2579 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2580 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2581 case LTTNG_DOMAIN_UST_PID
:
2584 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2595 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
2597 static int cmd_enable_channel(struct ltt_session
*session
,
2598 int domain
, struct lttng_channel
*attr
)
2601 struct ltt_ust_session
*usess
= session
->ust_session
;
2602 struct lttng_ht
*chan_ht
;
2604 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
2607 case LTTNG_DOMAIN_KERNEL
:
2609 struct ltt_kernel_channel
*kchan
;
2611 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
2612 session
->kernel_session
);
2613 if (kchan
== NULL
) {
2614 ret
= channel_kernel_create(session
->kernel_session
,
2615 attr
, kernel_poll_pipe
[1]);
2617 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
2620 if (ret
!= LTTCOMM_OK
) {
2624 kernel_wait_quiescent(kernel_tracer_fd
);
2627 case LTTNG_DOMAIN_UST
:
2629 struct ltt_ust_channel
*uchan
;
2631 chan_ht
= usess
->domain_global
.channels
;
2633 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
2634 if (uchan
== NULL
) {
2635 ret
= channel_ust_create(usess
, domain
, attr
);
2637 ret
= channel_ust_enable(usess
, domain
, uchan
);
2642 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2643 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2644 case LTTNG_DOMAIN_UST_PID
:
2647 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2656 * Command LTTNG_DISABLE_EVENT processed by the client thread.
2658 static int cmd_disable_event(struct ltt_session
*session
, int domain
,
2659 char *channel_name
, char *event_name
)
2664 case LTTNG_DOMAIN_KERNEL
:
2666 struct ltt_kernel_channel
*kchan
;
2667 struct ltt_kernel_session
*ksess
;
2669 ksess
= session
->kernel_session
;
2671 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
2672 if (kchan
== NULL
) {
2673 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2677 ret
= event_kernel_disable_tracepoint(ksess
, kchan
, event_name
);
2678 if (ret
!= LTTCOMM_OK
) {
2682 kernel_wait_quiescent(kernel_tracer_fd
);
2685 case LTTNG_DOMAIN_UST
:
2687 struct ltt_ust_channel
*uchan
;
2688 struct ltt_ust_session
*usess
;
2690 usess
= session
->ust_session
;
2692 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2694 if (uchan
== NULL
) {
2695 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2699 ret
= event_ust_disable_tracepoint(usess
, domain
, uchan
, event_name
);
2700 if (ret
!= LTTCOMM_OK
) {
2704 DBG3("Disable UST event %s in channel %s completed", event_name
,
2709 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2710 case LTTNG_DOMAIN_UST_PID
:
2711 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2725 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
2727 static int cmd_disable_event_all(struct ltt_session
*session
, int domain
,
2733 case LTTNG_DOMAIN_KERNEL
:
2735 struct ltt_kernel_session
*ksess
;
2736 struct ltt_kernel_channel
*kchan
;
2738 ksess
= session
->kernel_session
;
2740 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
2741 if (kchan
== NULL
) {
2742 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2746 ret
= event_kernel_disable_all(ksess
, kchan
);
2747 if (ret
!= LTTCOMM_OK
) {
2751 kernel_wait_quiescent(kernel_tracer_fd
);
2754 case LTTNG_DOMAIN_UST
:
2756 struct ltt_ust_session
*usess
;
2757 struct ltt_ust_channel
*uchan
;
2759 usess
= session
->ust_session
;
2761 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2763 if (uchan
== NULL
) {
2764 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2768 ret
= event_ust_disable_all_tracepoints(usess
, domain
, uchan
);
2773 DBG3("Disable all UST events in channel %s completed", channel_name
);
2778 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2779 case LTTNG_DOMAIN_UST_PID
:
2780 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2794 * Command LTTNG_ADD_CONTEXT processed by the client thread.
2796 static int cmd_add_context(struct ltt_session
*session
, int domain
,
2797 char *channel_name
, char *event_name
, struct lttng_event_context
*ctx
)
2802 case LTTNG_DOMAIN_KERNEL
:
2803 /* Add kernel context to kernel tracer */
2804 ret
= context_kernel_add(session
->kernel_session
, ctx
,
2805 event_name
, channel_name
);
2806 if (ret
!= LTTCOMM_OK
) {
2810 case LTTNG_DOMAIN_UST
:
2812 struct ltt_ust_session
*usess
= session
->ust_session
;
2814 ret
= context_ust_add(usess
, domain
, ctx
, event_name
, channel_name
);
2815 if (ret
!= LTTCOMM_OK
) {
2821 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2822 case LTTNG_DOMAIN_UST_PID
:
2823 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2837 * Command LTTNG_SET_FILTER processed by the client thread.
2839 static int cmd_set_filter(struct ltt_session
*session
, int domain
,
2840 char *channel_name
, char *event_name
,
2841 struct lttng_filter_bytecode
*bytecode
)
2846 case LTTNG_DOMAIN_KERNEL
:
2847 ret
= LTTCOMM_FATAL
;
2849 case LTTNG_DOMAIN_UST
:
2851 struct ltt_ust_session
*usess
= session
->ust_session
;
2853 ret
= filter_ust_set(usess
, domain
, bytecode
, event_name
, channel_name
);
2854 if (ret
!= LTTCOMM_OK
) {
2860 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2861 case LTTNG_DOMAIN_UST_PID
:
2862 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2877 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2879 static int cmd_enable_event(struct ltt_session
*session
, int domain
,
2880 char *channel_name
, struct lttng_event
*event
)
2883 struct lttng_channel
*attr
;
2884 struct ltt_ust_session
*usess
= session
->ust_session
;
2887 case LTTNG_DOMAIN_KERNEL
:
2889 struct ltt_kernel_channel
*kchan
;
2891 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2892 session
->kernel_session
);
2893 if (kchan
== NULL
) {
2894 attr
= channel_new_default_attr(domain
);
2896 ret
= LTTCOMM_FATAL
;
2899 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
2901 /* This call will notify the kernel thread */
2902 ret
= channel_kernel_create(session
->kernel_session
,
2903 attr
, kernel_poll_pipe
[1]);
2904 if (ret
!= LTTCOMM_OK
) {
2911 /* Get the newly created kernel channel pointer */
2912 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2913 session
->kernel_session
);
2914 if (kchan
== NULL
) {
2915 /* This sould not happen... */
2916 ret
= LTTCOMM_FATAL
;
2920 ret
= event_kernel_enable_tracepoint(session
->kernel_session
, kchan
,
2922 if (ret
!= LTTCOMM_OK
) {
2926 kernel_wait_quiescent(kernel_tracer_fd
);
2929 case LTTNG_DOMAIN_UST
:
2931 struct lttng_channel
*attr
;
2932 struct ltt_ust_channel
*uchan
;
2934 /* Get channel from global UST domain */
2935 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2937 if (uchan
== NULL
) {
2938 /* Create default channel */
2939 attr
= channel_new_default_attr(domain
);
2941 ret
= LTTCOMM_FATAL
;
2944 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
2945 attr
->name
[NAME_MAX
- 1] = '\0';
2947 ret
= channel_ust_create(usess
, domain
, attr
);
2948 if (ret
!= LTTCOMM_OK
) {
2954 /* Get the newly created channel reference back */
2955 uchan
= trace_ust_find_channel_by_name(
2956 usess
->domain_global
.channels
, channel_name
);
2957 if (uchan
== NULL
) {
2958 /* Something is really wrong */
2959 ret
= LTTCOMM_FATAL
;
2964 /* At this point, the session and channel exist on the tracer */
2965 ret
= event_ust_enable_tracepoint(usess
, domain
, uchan
, event
);
2966 if (ret
!= LTTCOMM_OK
) {
2972 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2973 case LTTNG_DOMAIN_UST_PID
:
2974 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2988 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
2990 static int cmd_enable_event_all(struct ltt_session
*session
, int domain
,
2991 char *channel_name
, int event_type
)
2994 struct ltt_kernel_channel
*kchan
;
2997 case LTTNG_DOMAIN_KERNEL
:
2998 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2999 session
->kernel_session
);
3000 if (kchan
== NULL
) {
3001 /* This call will notify the kernel thread */
3002 ret
= channel_kernel_create(session
->kernel_session
, NULL
,
3003 kernel_poll_pipe
[1]);
3004 if (ret
!= LTTCOMM_OK
) {
3008 /* Get the newly created kernel channel pointer */
3009 kchan
= trace_kernel_get_channel_by_name(channel_name
,
3010 session
->kernel_session
);
3011 if (kchan
== NULL
) {
3012 /* This sould not happen... */
3013 ret
= LTTCOMM_FATAL
;
3019 switch (event_type
) {
3020 case LTTNG_EVENT_SYSCALL
:
3021 ret
= event_kernel_enable_all_syscalls(session
->kernel_session
,
3022 kchan
, kernel_tracer_fd
);
3024 case LTTNG_EVENT_TRACEPOINT
:
3026 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
3027 * events already registered to the channel.
3029 ret
= event_kernel_enable_all_tracepoints(session
->kernel_session
,
3030 kchan
, kernel_tracer_fd
);
3032 case LTTNG_EVENT_ALL
:
3033 /* Enable syscalls and tracepoints */
3034 ret
= event_kernel_enable_all(session
->kernel_session
,
3035 kchan
, kernel_tracer_fd
);
3038 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
3042 /* Manage return value */
3043 if (ret
!= LTTCOMM_OK
) {
3047 kernel_wait_quiescent(kernel_tracer_fd
);
3049 case LTTNG_DOMAIN_UST
:
3051 struct lttng_channel
*attr
;
3052 struct ltt_ust_channel
*uchan
;
3053 struct ltt_ust_session
*usess
= session
->ust_session
;
3055 /* Get channel from global UST domain */
3056 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
3058 if (uchan
== NULL
) {
3059 /* Create default channel */
3060 attr
= channel_new_default_attr(domain
);
3062 ret
= LTTCOMM_FATAL
;
3065 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
3066 attr
->name
[NAME_MAX
- 1] = '\0';
3068 /* Use the internal command enable channel */
3069 ret
= channel_ust_create(usess
, domain
, attr
);
3070 if (ret
!= LTTCOMM_OK
) {
3076 /* Get the newly created channel reference back */
3077 uchan
= trace_ust_find_channel_by_name(
3078 usess
->domain_global
.channels
, channel_name
);
3079 if (uchan
== NULL
) {
3080 /* Something is really wrong */
3081 ret
= LTTCOMM_FATAL
;
3086 /* At this point, the session and channel exist on the tracer */
3088 switch (event_type
) {
3089 case LTTNG_EVENT_ALL
:
3090 case LTTNG_EVENT_TRACEPOINT
:
3091 ret
= event_ust_enable_all_tracepoints(usess
, domain
, uchan
);
3092 if (ret
!= LTTCOMM_OK
) {
3097 ret
= LTTCOMM_UST_ENABLE_FAIL
;
3101 /* Manage return value */
3102 if (ret
!= LTTCOMM_OK
) {
3109 case LTTNG_DOMAIN_UST_EXEC_NAME
:
3110 case LTTNG_DOMAIN_UST_PID
:
3111 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
3125 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
3127 static ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
3130 ssize_t nb_events
= 0;
3133 case LTTNG_DOMAIN_KERNEL
:
3134 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
3135 if (nb_events
< 0) {
3136 ret
= LTTCOMM_KERN_LIST_FAIL
;
3140 case LTTNG_DOMAIN_UST
:
3141 nb_events
= ust_app_list_events(events
);
3142 if (nb_events
< 0) {
3143 ret
= LTTCOMM_UST_LIST_FAIL
;
3155 /* Return negative value to differentiate return code */
3160 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
3162 static ssize_t
cmd_list_tracepoint_fields(int domain
,
3163 struct lttng_event_field
**fields
)
3166 ssize_t nb_fields
= 0;
3169 case LTTNG_DOMAIN_UST
:
3170 nb_fields
= ust_app_list_event_fields(fields
);
3171 if (nb_fields
< 0) {
3172 ret
= LTTCOMM_UST_LIST_FAIL
;
3176 case LTTNG_DOMAIN_KERNEL
:
3177 default: /* fall-through */
3185 /* Return negative value to differentiate return code */
3190 * Command LTTNG_START_TRACE processed by the client thread.
3192 static int cmd_start_trace(struct ltt_session
*session
)
3195 struct ltt_kernel_session
*ksession
;
3196 struct ltt_ust_session
*usess
;
3197 struct ltt_kernel_channel
*kchan
;
3199 /* Ease our life a bit ;) */
3200 ksession
= session
->kernel_session
;
3201 usess
= session
->ust_session
;
3203 if (session
->enabled
) {
3204 /* Already started. */
3205 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
3209 session
->enabled
= 1;
3211 ret
= setup_relayd(session
);
3212 if (ret
!= LTTCOMM_OK
) {
3213 ERR("Error setting up relayd for session %s", session
->name
);
3217 /* Kernel tracing */
3218 if (ksession
!= NULL
) {
3219 /* Open kernel metadata */
3220 if (ksession
->metadata
== NULL
) {
3221 ret
= kernel_open_metadata(ksession
,
3222 ksession
->consumer
->dst
.trace_path
);
3224 ret
= LTTCOMM_KERN_META_FAIL
;
3229 /* Open kernel metadata stream */
3230 if (ksession
->metadata_stream_fd
< 0) {
3231 ret
= kernel_open_metadata_stream(ksession
);
3233 ERR("Kernel create metadata stream failed");
3234 ret
= LTTCOMM_KERN_STREAM_FAIL
;
3239 /* For each channel */
3240 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
3241 if (kchan
->stream_count
== 0) {
3242 ret
= kernel_open_channel_stream(kchan
);
3244 ret
= LTTCOMM_KERN_STREAM_FAIL
;
3247 /* Update the stream global counter */
3248 ksession
->stream_count_global
+= ret
;
3252 /* Setup kernel consumer socket and send fds to it */
3253 ret
= init_kernel_tracing(ksession
);
3255 ret
= LTTCOMM_KERN_START_FAIL
;
3259 /* This start the kernel tracing */
3260 ret
= kernel_start_session(ksession
);
3262 ret
= LTTCOMM_KERN_START_FAIL
;
3266 /* Quiescent wait after starting trace */
3267 kernel_wait_quiescent(kernel_tracer_fd
);
3270 /* Flag session that trace should start automatically */
3272 usess
->start_trace
= 1;
3274 ret
= ust_app_start_trace_all(usess
);
3276 ret
= LTTCOMM_UST_START_FAIL
;
3288 * Command LTTNG_STOP_TRACE processed by the client thread.
3290 static int cmd_stop_trace(struct ltt_session
*session
)
3293 struct ltt_kernel_channel
*kchan
;
3294 struct ltt_kernel_session
*ksession
;
3295 struct ltt_ust_session
*usess
;
3298 ksession
= session
->kernel_session
;
3299 usess
= session
->ust_session
;
3301 if (!session
->enabled
) {
3302 ret
= LTTCOMM_TRACE_ALREADY_STOPPED
;
3306 session
->enabled
= 0;
3309 if (ksession
!= NULL
) {
3310 DBG("Stop kernel tracing");
3312 /* Flush metadata if exist */
3313 if (ksession
->metadata_stream_fd
>= 0) {
3314 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
3316 ERR("Kernel metadata flush failed");
3320 /* Flush all buffers before stopping */
3321 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
3322 ret
= kernel_flush_buffer(kchan
);
3324 ERR("Kernel flush buffer error");
3328 ret
= kernel_stop_session(ksession
);
3330 ret
= LTTCOMM_KERN_STOP_FAIL
;
3334 kernel_wait_quiescent(kernel_tracer_fd
);
3338 usess
->start_trace
= 0;
3340 ret
= ust_app_stop_trace_all(usess
);
3342 ret
= LTTCOMM_UST_STOP_FAIL
;
3354 * Command LTTNG_CREATE_SESSION_URI processed by the client thread.
3356 static int cmd_create_session_uri(char *name
, struct lttng_uri
*ctrl_uri
,
3357 struct lttng_uri
*data_uri
, unsigned int enable_consumer
,
3358 lttng_sock_cred
*creds
)
3362 struct ltt_session
*session
;
3363 struct consumer_output
*consumer
;
3365 /* Verify if the session already exist */
3366 session
= session_find_by_name(name
);
3367 if (session
!= NULL
) {
3368 ret
= LTTCOMM_EXIST_SESS
;
3372 /* TODO: validate URIs */
3374 /* Create default consumer output */
3375 consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
3376 if (consumer
== NULL
) {
3377 ret
= LTTCOMM_FATAL
;
3380 strncpy(consumer
->subdir
, ctrl_uri
->subdir
, sizeof(consumer
->subdir
));
3381 DBG2("Consumer subdir set to %s", consumer
->subdir
);
3383 switch (ctrl_uri
->dtype
) {
3384 case LTTNG_DST_IPV4
:
3385 case LTTNG_DST_IPV6
:
3386 /* Set control URI into consumer output object */
3387 ret
= consumer_set_network_uri(consumer
, ctrl_uri
);
3389 ret
= LTTCOMM_FATAL
;
3393 /* Set data URI into consumer output object */
3394 ret
= consumer_set_network_uri(consumer
, data_uri
);
3396 ret
= LTTCOMM_FATAL
;
3400 /* Empty path since the session is network */
3403 case LTTNG_DST_PATH
:
3404 /* Very volatile pointer. Only used for the create session. */
3405 path
= ctrl_uri
->dst
.path
;
3406 strncpy(consumer
->dst
.trace_path
, path
,
3407 sizeof(consumer
->dst
.trace_path
));
3411 /* Set if the consumer is enabled or not */
3412 consumer
->enabled
= enable_consumer
;
3414 ret
= session_create(name
, path
, LTTNG_SOCK_GET_UID_CRED(creds
),
3415 LTTNG_SOCK_GET_GID_CRED(creds
));
3416 if (ret
!= LTTCOMM_OK
) {
3417 goto consumer_error
;
3420 /* Get the newly created session pointer back */
3421 session
= session_find_by_name(name
);
3424 /* Assign consumer to session */
3425 session
->consumer
= consumer
;
3430 consumer_destroy_output(consumer
);
3436 * Command LTTNG_CREATE_SESSION processed by the client thread.
3438 static int cmd_create_session(char *name
, char *path
, lttng_sock_cred
*creds
)
3441 struct lttng_uri uri
;
3443 /* Zeroed temporary URI */
3444 memset(&uri
, 0, sizeof(uri
));
3446 uri
.dtype
= LTTNG_DST_PATH
;
3447 uri
.utype
= LTTNG_URI_DST
;
3448 strncpy(uri
.dst
.path
, path
, sizeof(uri
.dst
.path
));
3450 /* TODO: Strip date-time from path and put it in uri's subdir */
3452 ret
= cmd_create_session_uri(name
, &uri
, NULL
, 1, creds
);
3453 if (ret
!= LTTCOMM_OK
) {
3462 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3464 static int cmd_destroy_session(struct ltt_session
*session
, char *name
)
3468 /* Clean kernel session teardown */
3469 teardown_kernel_session(session
);
3470 /* UST session teardown */
3471 teardown_ust_session(session
);
3474 * Must notify the kernel thread here to update it's poll setin order
3475 * to remove the channel(s)' fd just destroyed.
3477 ret
= notify_thread_pipe(kernel_poll_pipe
[1]);
3479 PERROR("write kernel poll pipe");
3482 ret
= session_destroy(session
);
3488 * Command LTTNG_CALIBRATE processed by the client thread.
3490 static int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
3495 case LTTNG_DOMAIN_KERNEL
:
3497 struct lttng_kernel_calibrate kcalibrate
;
3499 kcalibrate
.type
= calibrate
->type
;
3500 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
3502 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
3507 case LTTNG_DOMAIN_UST
:
3509 struct lttng_ust_calibrate ucalibrate
;
3511 ucalibrate
.type
= calibrate
->type
;
3512 ret
= ust_app_calibrate_glb(&ucalibrate
);
3514 ret
= LTTCOMM_UST_CALIBRATE_FAIL
;
3531 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3533 static int cmd_register_consumer(struct ltt_session
*session
, int domain
,
3539 case LTTNG_DOMAIN_KERNEL
:
3540 /* Can't register a consumer if there is already one */
3541 if (session
->kernel_session
->consumer_fds_sent
!= 0) {
3542 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
3546 sock
= lttcomm_connect_unix_sock(sock_path
);
3548 ret
= LTTCOMM_CONNECT_FAIL
;
3552 session
->kernel_session
->consumer_fd
= sock
;
3555 /* TODO: Userspace tracing */
3567 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3569 static ssize_t
cmd_list_domains(struct ltt_session
*session
,
3570 struct lttng_domain
**domains
)
3575 if (session
->kernel_session
!= NULL
) {
3576 DBG3("Listing domains found kernel domain");
3580 if (session
->ust_session
!= NULL
) {
3581 DBG3("Listing domains found UST global domain");
3585 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3586 if (*domains
== NULL
) {
3587 ret
= -LTTCOMM_FATAL
;
3591 if (session
->kernel_session
!= NULL
) {
3592 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3596 if (session
->ust_session
!= NULL
) {
3597 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3608 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3610 static ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
3611 struct lttng_channel
**channels
)
3614 ssize_t nb_chan
= 0;
3617 case LTTNG_DOMAIN_KERNEL
:
3618 if (session
->kernel_session
!= NULL
) {
3619 nb_chan
= session
->kernel_session
->channel_count
;
3621 DBG3("Number of kernel channels %zd", nb_chan
);
3623 case LTTNG_DOMAIN_UST
:
3624 if (session
->ust_session
!= NULL
) {
3625 nb_chan
= lttng_ht_get_count(
3626 session
->ust_session
->domain_global
.channels
);
3628 DBG3("Number of UST global channels %zd", nb_chan
);
3637 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
3638 if (*channels
== NULL
) {
3639 ret
= -LTTCOMM_FATAL
;
3643 list_lttng_channels(domain
, session
, *channels
);
3655 * Command LTTNG_LIST_EVENTS processed by the client thread.
3657 static ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
3658 char *channel_name
, struct lttng_event
**events
)
3661 ssize_t nb_event
= 0;
3664 case LTTNG_DOMAIN_KERNEL
:
3665 if (session
->kernel_session
!= NULL
) {
3666 nb_event
= list_lttng_kernel_events(channel_name
,
3667 session
->kernel_session
, events
);
3670 case LTTNG_DOMAIN_UST
:
3672 if (session
->ust_session
!= NULL
) {
3673 nb_event
= list_lttng_ust_global_events(channel_name
,
3674 &session
->ust_session
->domain_global
, events
);
3690 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
3692 static int cmd_set_consumer_uri(int domain
, struct ltt_session
*session
,
3693 struct lttng_uri
*uri
)
3696 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3697 struct ltt_ust_session
*usess
= session
->ust_session
;
3698 struct consumer_output
*consumer
;
3700 /* Can't enable consumer after session started. */
3701 if (session
->enabled
) {
3702 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
3707 case LTTNG_DOMAIN_KERNEL
:
3708 /* Code flow error if we don't have a kernel session here. */
3711 /* Create consumer output if none exists */
3712 consumer
= ksess
->tmp_consumer
;
3713 if (consumer
== NULL
) {
3714 consumer
= consumer_copy_output(ksess
->consumer
);
3715 if (consumer
== NULL
) {
3716 ret
= LTTCOMM_FATAL
;
3719 /* Reassign new pointer */
3720 ksess
->tmp_consumer
= consumer
;
3723 switch (uri
->dtype
) {
3724 case LTTNG_DST_IPV4
:
3725 case LTTNG_DST_IPV6
:
3726 DBG2("Setting network URI for kernel session %s", session
->name
);
3728 /* Set URI into consumer output object */
3729 ret
= consumer_set_network_uri(consumer
, uri
);
3731 ret
= LTTCOMM_FATAL
;
3735 /* On a new subdir, reappend the default trace dir. */
3736 if (strlen(uri
->subdir
) != 0) {
3737 strncat(consumer
->subdir
, DEFAULT_KERNEL_TRACE_DIR
,
3738 sizeof(consumer
->subdir
));
3741 ret
= send_socket_relayd_consumer(domain
, session
, uri
, consumer
,
3742 ksess
->consumer_fd
);
3743 if (ret
!= LTTCOMM_OK
) {
3747 case LTTNG_DST_PATH
:
3748 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
3749 memset(consumer
->dst
.trace_path
, 0,
3750 sizeof(consumer
->dst
.trace_path
));
3751 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
3752 sizeof(consumer
->dst
.trace_path
));
3753 /* Append default kernel trace dir */
3754 strncat(consumer
->dst
.trace_path
, DEFAULT_KERNEL_TRACE_DIR
,
3755 sizeof(consumer
->dst
.trace_path
));
3761 case LTTNG_DOMAIN_UST
:
3762 /* Code flow error if we don't have a kernel session here. */
3765 /* Create consumer output if none exists */
3766 consumer
= usess
->tmp_consumer
;
3767 if (consumer
== NULL
) {
3768 consumer
= consumer_copy_output(usess
->consumer
);
3769 if (consumer
== NULL
) {
3770 ret
= LTTCOMM_FATAL
;
3773 /* Reassign new pointer */
3774 usess
->tmp_consumer
= consumer
;
3777 switch (uri
->dtype
) {
3778 case LTTNG_DST_IPV4
:
3779 case LTTNG_DST_IPV6
:
3781 DBG2("Setting network URI for UST session %s", session
->name
);
3783 /* Set URI into consumer object */
3784 ret
= consumer_set_network_uri(consumer
, uri
);
3786 ret
= LTTCOMM_FATAL
;
3790 /* On a new subdir, reappend the default trace dir. */
3791 if (strlen(uri
->subdir
) != 0) {
3792 strncat(consumer
->subdir
, DEFAULT_UST_TRACE_DIR
,
3793 sizeof(consumer
->subdir
));
3796 if (ust_consumerd64_fd
>= 0) {
3797 ret
= send_socket_relayd_consumer(domain
, session
, uri
,
3798 consumer
, ust_consumerd64_fd
);
3799 if (ret
!= LTTCOMM_OK
) {
3804 if (ust_consumerd32_fd
>= 0) {
3805 ret
= send_socket_relayd_consumer(domain
, session
, uri
,
3806 consumer
, ust_consumerd32_fd
);
3807 if (ret
!= LTTCOMM_OK
) {
3814 case LTTNG_DST_PATH
:
3815 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
3816 memset(consumer
->dst
.trace_path
, 0,
3817 sizeof(consumer
->dst
.trace_path
));
3818 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
3819 sizeof(consumer
->dst
.trace_path
));
3820 /* Append default UST trace dir */
3821 strncat(consumer
->dst
.trace_path
, DEFAULT_UST_TRACE_DIR
,
3822 sizeof(consumer
->dst
.trace_path
));
3836 * Command LTTNG_DISABLE_CONSUMER processed by the client thread.
3838 static int cmd_disable_consumer(int domain
, struct ltt_session
*session
)
3841 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3842 struct ltt_ust_session
*usess
= session
->ust_session
;
3843 struct consumer_output
*consumer
;
3845 if (session
->enabled
) {
3846 /* Can't disable consumer on an already started session */
3847 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
3852 case LTTNG_DOMAIN_KERNEL
:
3853 /* Code flow error if we don't have a kernel session here. */
3856 DBG("Disabling kernel consumer");
3857 consumer
= ksess
->consumer
;
3860 case LTTNG_DOMAIN_UST
:
3861 /* Code flow error if we don't have a UST session here. */
3864 DBG("Disabling UST consumer");
3865 consumer
= usess
->consumer
;
3869 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
3874 consumer
->enabled
= 0;
3876 /* Success at this point */
3884 * Command LTTNG_ENABLE_CONSUMER processed by the client thread.
3886 static int cmd_enable_consumer(int domain
, struct ltt_session
*session
)
3889 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3890 struct ltt_ust_session
*usess
= session
->ust_session
;
3891 struct consumer_output
*tmp_out
;
3893 /* Can't enable consumer after session started. */
3894 if (session
->enabled
) {
3895 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
3900 case LTTNG_DOMAIN_KERNEL
:
3901 /* Code flow error if we don't have a kernel session here. */
3905 * Check if we have already sent fds to the consumer. In that case,
3906 * the enable-consumer command can't be used because a start trace
3907 * had previously occured.
3909 if (ksess
->consumer_fds_sent
) {
3910 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
3914 tmp_out
= ksess
->tmp_consumer
;
3915 if (tmp_out
== NULL
) {
3916 /* No temp. consumer output exists. Using the current one. */
3917 DBG3("No temporary consumer. Using default");
3922 switch (tmp_out
->type
) {
3923 case CONSUMER_DST_LOCAL
:
3924 DBG2("Consumer output is local. Creating directory(ies)");
3926 /* Create directory(ies) */
3927 ret
= run_as_mkdir_recursive(tmp_out
->dst
.trace_path
,
3928 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
3930 if (ret
!= -EEXIST
) {
3931 ERR("Trace directory creation error");
3932 ret
= LTTCOMM_FATAL
;
3937 case CONSUMER_DST_NET
:
3938 DBG2("Consumer output is network. Validating URIs");
3939 /* Validate if we have both control and data path set. */
3940 if (!tmp_out
->dst
.net
.control_isset
) {
3941 ret
= LTTCOMM_URI_CTRL_MISS
;
3945 if (!tmp_out
->dst
.net
.data_isset
) {
3946 ret
= LTTCOMM_URI_DATA_MISS
;
3950 /* Check established network session state */
3951 if (session
->net_handle
== 0) {
3952 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
3953 ERR("Session network handle is not set on enable-consumer");
3957 /* Append default kernel trace dir to subdir */
3958 strncat(ksess
->consumer
->subdir
, DEFAULT_KERNEL_TRACE_DIR
,
3959 sizeof(ksess
->consumer
->subdir
));
3966 * This is race free for now since the session lock is acquired before
3967 * ending up in this function. No other threads can access this kernel
3968 * session without this lock hence freeing the consumer output object
3971 consumer_destroy_output(ksess
->consumer
);
3972 ksess
->consumer
= tmp_out
;
3973 ksess
->tmp_consumer
= NULL
;
3976 case LTTNG_DOMAIN_UST
:
3977 /* Code flow error if we don't have a UST session here. */
3981 * Check if we have already sent fds to the consumer. In that case,
3982 * the enable-consumer command can't be used because a start trace
3983 * had previously occured.
3985 if (usess
->start_trace
) {
3986 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
3990 tmp_out
= usess
->tmp_consumer
;
3991 if (tmp_out
== NULL
) {
3992 /* No temp. consumer output exists. Using the current one. */
3993 DBG3("No temporary consumer. Using default");
3998 switch (tmp_out
->type
) {
3999 case CONSUMER_DST_LOCAL
:
4000 DBG2("Consumer output is local. Creating directory(ies)");
4002 /* Create directory(ies) */
4003 ret
= run_as_mkdir_recursive(tmp_out
->dst
.trace_path
,
4004 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
4006 if (ret
!= -EEXIST
) {
4007 ERR("Trace directory creation error");
4008 ret
= LTTCOMM_FATAL
;
4013 case CONSUMER_DST_NET
:
4014 DBG2("Consumer output is network. Validating URIs");
4015 /* Validate if we have both control and data path set. */
4016 if (!tmp_out
->dst
.net
.control_isset
) {
4017 ret
= LTTCOMM_URI_CTRL_MISS
;
4021 if (!tmp_out
->dst
.net
.data_isset
) {
4022 ret
= LTTCOMM_URI_DATA_MISS
;
4026 /* Check established network session state */
4027 if (session
->net_handle
== 0) {
4028 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
4029 DBG2("Session network handle is not set on enable-consumer");
4033 if (tmp_out
->net_seq_index
== -1) {
4034 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
4035 DBG2("Network index is not set on the consumer");
4039 /* Append default kernel trace dir to subdir */
4040 strncat(usess
->consumer
->subdir
, DEFAULT_UST_TRACE_DIR
,
4041 sizeof(usess
->consumer
->subdir
));
4048 * This is race free for now since the session lock is acquired before
4049 * ending up in this function. No other threads can access this kernel
4050 * session without this lock hence freeing the consumer output object
4053 consumer_destroy_output(usess
->consumer
);
4054 usess
->consumer
= tmp_out
;
4055 usess
->tmp_consumer
= NULL
;
4060 /* Success at this point */
4068 * Process the command requested by the lttng client within the command
4069 * context structure. This function make sure that the return structure (llm)
4070 * is set and ready for transmission before returning.
4072 * Return any error encountered or 0 for success.
4074 * "sock" is only used for special-case var. len data.
4076 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
4079 int ret
= LTTCOMM_OK
;
4080 int need_tracing_session
= 1;
4083 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
4087 switch (cmd_ctx
->lsm
->cmd_type
) {
4088 case LTTNG_CREATE_SESSION
:
4089 case LTTNG_CREATE_SESSION_URI
:
4090 case LTTNG_DESTROY_SESSION
:
4091 case LTTNG_LIST_SESSIONS
:
4092 case LTTNG_LIST_DOMAINS
:
4093 case LTTNG_START_TRACE
:
4094 case LTTNG_STOP_TRACE
:
4101 if (opt_no_kernel
&& need_domain
4102 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
4104 ret
= LTTCOMM_NEED_ROOT_SESSIOND
;
4106 ret
= LTTCOMM_KERN_NA
;
4112 * Check for command that don't needs to allocate a returned payload. We do
4113 * this here so we don't have to make the call for no payload at each
4116 switch(cmd_ctx
->lsm
->cmd_type
) {
4117 case LTTNG_LIST_SESSIONS
:
4118 case LTTNG_LIST_TRACEPOINTS
:
4119 case LTTNG_LIST_TRACEPOINT_FIELDS
:
4120 case LTTNG_LIST_DOMAINS
:
4121 case LTTNG_LIST_CHANNELS
:
4122 case LTTNG_LIST_EVENTS
:
4125 /* Setup lttng message with no payload */
4126 ret
= setup_lttng_msg(cmd_ctx
, 0);
4128 /* This label does not try to unlock the session */
4129 goto init_setup_error
;
4133 /* Commands that DO NOT need a session. */
4134 switch (cmd_ctx
->lsm
->cmd_type
) {
4135 case LTTNG_CREATE_SESSION
:
4136 case LTTNG_CREATE_SESSION_URI
:
4137 case LTTNG_CALIBRATE
:
4138 case LTTNG_LIST_SESSIONS
:
4139 case LTTNG_LIST_TRACEPOINTS
:
4140 case LTTNG_LIST_TRACEPOINT_FIELDS
:
4141 need_tracing_session
= 0;
4144 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
4146 * We keep the session list lock across _all_ commands
4147 * for now, because the per-session lock does not
4148 * handle teardown properly.
4150 session_lock_list();
4151 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
4152 if (cmd_ctx
->session
== NULL
) {
4153 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
4154 ret
= LTTCOMM_SESS_NOT_FOUND
;
4156 /* If no session name specified */
4157 ret
= LTTCOMM_SELECT_SESS
;
4161 /* Acquire lock for the session */
4162 session_lock(cmd_ctx
->session
);
4171 * Check domain type for specific "pre-action".
4173 switch (cmd_ctx
->lsm
->domain
.type
) {
4174 case LTTNG_DOMAIN_KERNEL
:
4176 ret
= LTTCOMM_NEED_ROOT_SESSIOND
;
4180 /* Kernel tracer check */
4181 if (kernel_tracer_fd
== -1) {
4182 /* Basically, load kernel tracer modules */
4183 ret
= init_kernel_tracer();
4189 /* Consumer is in an ERROR state. Report back to client */
4190 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
4191 ret
= LTTCOMM_NO_KERNCONSUMERD
;
4195 /* Need a session for kernel command */
4196 if (need_tracing_session
) {
4197 if (cmd_ctx
->session
->kernel_session
== NULL
) {
4198 ret
= create_kernel_session(cmd_ctx
->session
);
4200 ret
= LTTCOMM_KERN_SESS_FAIL
;
4205 /* Start the kernel consumer daemon */
4206 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
4207 if (kconsumer_data
.pid
== 0 &&
4208 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
4209 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
4210 ret
= start_consumerd(&kconsumer_data
);
4212 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
4215 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
4217 /* Set consumer fd of the session */
4218 cmd_ctx
->session
->kernel_session
->consumer_fd
=
4219 kconsumer_data
.cmd_sock
;
4221 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
4226 case LTTNG_DOMAIN_UST
:
4228 /* Consumer is in an ERROR state. Report back to client */
4229 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
4230 ret
= LTTCOMM_NO_USTCONSUMERD
;
4234 if (need_tracing_session
) {
4235 if (cmd_ctx
->session
->ust_session
== NULL
) {
4236 ret
= create_ust_session(cmd_ctx
->session
,
4237 &cmd_ctx
->lsm
->domain
);
4238 if (ret
!= LTTCOMM_OK
) {
4243 /* Start the UST consumer daemons */
4245 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
4246 if (consumerd64_bin
[0] != '\0' &&
4247 ustconsumer64_data
.pid
== 0 &&
4248 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
4249 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
4250 ret
= start_consumerd(&ustconsumer64_data
);
4252 ret
= LTTCOMM_UST_CONSUMER64_FAIL
;
4253 ust_consumerd64_fd
= -EINVAL
;
4257 ust_consumerd64_fd
= ustconsumer64_data
.cmd_sock
;
4258 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
4260 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
4263 if (consumerd32_bin
[0] != '\0' &&
4264 ustconsumer32_data
.pid
== 0 &&
4265 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
4266 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
4267 ret
= start_consumerd(&ustconsumer32_data
);
4269 ret
= LTTCOMM_UST_CONSUMER32_FAIL
;
4270 ust_consumerd32_fd
= -EINVAL
;
4274 ust_consumerd32_fd
= ustconsumer32_data
.cmd_sock
;
4275 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
4277 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
4287 /* Validate consumer daemon state when start/stop trace command */
4288 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
4289 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
4290 switch (cmd_ctx
->lsm
->domain
.type
) {
4291 case LTTNG_DOMAIN_UST
:
4292 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
4293 ret
= LTTCOMM_NO_USTCONSUMERD
;
4297 case LTTNG_DOMAIN_KERNEL
:
4298 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
4299 ret
= LTTCOMM_NO_KERNCONSUMERD
;
4307 * Check that the UID or GID match that of the tracing session.
4308 * The root user can interact with all sessions.
4310 if (need_tracing_session
) {
4311 if (!session_access_ok(cmd_ctx
->session
,
4312 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4313 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
4314 ret
= LTTCOMM_EPERM
;
4319 /* Process by command type */
4320 switch (cmd_ctx
->lsm
->cmd_type
) {
4321 case LTTNG_ADD_CONTEXT
:
4323 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4324 cmd_ctx
->lsm
->u
.context
.channel_name
,
4325 cmd_ctx
->lsm
->u
.context
.event_name
,
4326 &cmd_ctx
->lsm
->u
.context
.ctx
);
4329 case LTTNG_DISABLE_CHANNEL
:
4331 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4332 cmd_ctx
->lsm
->u
.disable
.channel_name
);
4335 case LTTNG_DISABLE_EVENT
:
4337 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4338 cmd_ctx
->lsm
->u
.disable
.channel_name
,
4339 cmd_ctx
->lsm
->u
.disable
.name
);
4342 case LTTNG_DISABLE_ALL_EVENT
:
4344 DBG("Disabling all events");
4346 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4347 cmd_ctx
->lsm
->u
.disable
.channel_name
);
4350 case LTTNG_DISABLE_CONSUMER
:
4352 ret
= cmd_disable_consumer(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
);
4355 case LTTNG_ENABLE_CHANNEL
:
4357 ret
= cmd_enable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4358 &cmd_ctx
->lsm
->u
.channel
.chan
);
4361 case LTTNG_ENABLE_CONSUMER
:
4363 ret
= cmd_enable_consumer(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
);
4366 case LTTNG_ENABLE_EVENT
:
4368 ret
= cmd_enable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4369 cmd_ctx
->lsm
->u
.enable
.channel_name
,
4370 &cmd_ctx
->lsm
->u
.enable
.event
);
4373 case LTTNG_ENABLE_ALL_EVENT
:
4375 DBG("Enabling all events");
4377 ret
= cmd_enable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4378 cmd_ctx
->lsm
->u
.enable
.channel_name
,
4379 cmd_ctx
->lsm
->u
.enable
.event
.type
);
4382 case LTTNG_LIST_TRACEPOINTS
:
4384 struct lttng_event
*events
;
4387 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
4388 if (nb_events
< 0) {
4394 * Setup lttng message with payload size set to the event list size in
4395 * bytes and then copy list into the llm payload.
4397 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
4403 /* Copy event list into message payload */
4404 memcpy(cmd_ctx
->llm
->payload
, events
,
4405 sizeof(struct lttng_event
) * nb_events
);
4412 case LTTNG_LIST_TRACEPOINT_FIELDS
:
4414 struct lttng_event_field
*fields
;
4417 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
, &fields
);
4418 if (nb_fields
< 0) {
4424 * Setup lttng message with payload size set to the event list size in
4425 * bytes and then copy list into the llm payload.
4427 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event_field
) * nb_fields
);
4433 /* Copy event list into message payload */
4434 memcpy(cmd_ctx
->llm
->payload
, fields
,
4435 sizeof(struct lttng_event_field
) * nb_fields
);
4442 case LTTNG_SET_CONSUMER_URI
:
4444 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
4445 &cmd_ctx
->lsm
->u
.uri
);
4448 case LTTNG_START_TRACE
:
4450 ret
= cmd_start_trace(cmd_ctx
->session
);
4453 case LTTNG_STOP_TRACE
:
4455 ret
= cmd_stop_trace(cmd_ctx
->session
);
4458 case LTTNG_CREATE_SESSION
:
4460 ret
= cmd_create_session(cmd_ctx
->lsm
->session
.name
,
4461 cmd_ctx
->lsm
->session
.path
, &cmd_ctx
->creds
);
4464 case LTTNG_CREATE_SESSION_URI
:
4466 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
,
4467 &cmd_ctx
->lsm
->u
.create_uri
.ctrl_uri
,
4468 &cmd_ctx
->lsm
->u
.create_uri
.data_uri
,
4469 cmd_ctx
->lsm
->u
.create_uri
.enable_consumer
, &cmd_ctx
->creds
);
4472 case LTTNG_DESTROY_SESSION
:
4474 ret
= cmd_destroy_session(cmd_ctx
->session
,
4475 cmd_ctx
->lsm
->session
.name
);
4477 * Set session to NULL so we do not unlock it after
4480 cmd_ctx
->session
= NULL
;
4483 case LTTNG_LIST_DOMAINS
:
4486 struct lttng_domain
*domains
;
4488 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
4494 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
4499 /* Copy event list into message payload */
4500 memcpy(cmd_ctx
->llm
->payload
, domains
,
4501 nb_dom
* sizeof(struct lttng_domain
));
4508 case LTTNG_LIST_CHANNELS
:
4511 struct lttng_channel
*channels
;
4513 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
4514 cmd_ctx
->session
, &channels
);
4520 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
4525 /* Copy event list into message payload */
4526 memcpy(cmd_ctx
->llm
->payload
, channels
,
4527 nb_chan
* sizeof(struct lttng_channel
));
4534 case LTTNG_LIST_EVENTS
:
4537 struct lttng_event
*events
= NULL
;
4539 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
4540 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
4546 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
4551 /* Copy event list into message payload */
4552 memcpy(cmd_ctx
->llm
->payload
, events
,
4553 nb_event
* sizeof(struct lttng_event
));
4560 case LTTNG_LIST_SESSIONS
:
4562 unsigned int nr_sessions
;
4564 session_lock_list();
4565 nr_sessions
= lttng_sessions_count(
4566 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4567 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4569 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
4571 session_unlock_list();
4575 /* Filled the session array */
4576 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
4577 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4578 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4580 session_unlock_list();
4585 case LTTNG_CALIBRATE
:
4587 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
4588 &cmd_ctx
->lsm
->u
.calibrate
);
4591 case LTTNG_REGISTER_CONSUMER
:
4593 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4594 cmd_ctx
->lsm
->u
.reg
.path
);
4597 case LTTNG_SET_FILTER
:
4599 struct lttng_filter_bytecode
*bytecode
;
4601 if (cmd_ctx
->lsm
->u
.filter
.bytecode_len
> 65336) {
4602 ret
= LTTCOMM_FILTER_INVAL
;
4605 bytecode
= zmalloc(cmd_ctx
->lsm
->u
.filter
.bytecode_len
);
4607 ret
= LTTCOMM_FILTER_NOMEM
;
4610 /* Receive var. len. data */
4611 DBG("Receiving var len data from client ...");
4612 ret
= lttcomm_recv_unix_sock(sock
, bytecode
,
4613 cmd_ctx
->lsm
->u
.filter
.bytecode_len
);
4615 DBG("Nothing recv() from client var len data... continuing");
4617 ret
= LTTCOMM_FILTER_INVAL
;
4621 if (bytecode
->len
+ sizeof(*bytecode
)
4622 != cmd_ctx
->lsm
->u
.filter
.bytecode_len
) {
4624 ret
= LTTCOMM_FILTER_INVAL
;
4628 ret
= cmd_set_filter(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4629 cmd_ctx
->lsm
->u
.filter
.channel_name
,
4630 cmd_ctx
->lsm
->u
.filter
.event_name
,
4640 if (cmd_ctx
->llm
== NULL
) {
4641 DBG("Missing llm structure. Allocating one.");
4642 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
4646 /* Set return code */
4647 cmd_ctx
->llm
->ret_code
= ret
;
4649 if (cmd_ctx
->session
) {
4650 session_unlock(cmd_ctx
->session
);
4652 if (need_tracing_session
) {
4653 session_unlock_list();
4660 * Thread managing health check socket.
4662 static void *thread_manage_health(void *data
)
4664 int sock
= -1, new_sock
, ret
, i
, pollfd
, err
= -1;
4665 uint32_t revents
, nb_fd
;
4666 struct lttng_poll_event events
;
4667 struct lttcomm_health_msg msg
;
4668 struct lttcomm_health_data reply
;
4670 DBG("[thread] Manage health check started");
4672 rcu_register_thread();
4674 /* Create unix socket */
4675 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
4677 ERR("Unable to create health check Unix socket");
4682 ret
= lttcomm_listen_unix_sock(sock
);
4688 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4689 * more will be added to this poll set.
4691 ret
= create_thread_poll_set(&events
, 2);
4696 /* Add the application registration socket */
4697 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4703 DBG("Health check ready");
4705 nb_fd
= LTTNG_POLL_GETNB(&events
);
4707 /* Inifinite blocking call, waiting for transmission */
4709 ret
= lttng_poll_wait(&events
, -1);
4712 * Restart interrupted system call.
4714 if (errno
== EINTR
) {
4720 for (i
= 0; i
< nb_fd
; i
++) {
4721 /* Fetch once the poll data */
4722 revents
= LTTNG_POLL_GETEV(&events
, i
);
4723 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4725 /* Thread quit pipe has been closed. Killing thread. */
4726 ret
= check_thread_quit_pipe(pollfd
, revents
);
4732 /* Event on the registration socket */
4733 if (pollfd
== sock
) {
4734 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4735 ERR("Health socket poll error");
4741 new_sock
= lttcomm_accept_unix_sock(sock
);
4746 DBG("Receiving data from client for health...");
4747 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4749 DBG("Nothing recv() from client... continuing");
4750 ret
= close(new_sock
);
4758 rcu_thread_online();
4760 switch (msg
.component
) {
4761 case LTTNG_HEALTH_CMD
:
4762 reply
.ret_code
= health_check_state(&health_thread_cmd
);
4764 case LTTNG_HEALTH_APP_MANAGE
:
4765 reply
.ret_code
= health_check_state(&health_thread_app_manage
);
4767 case LTTNG_HEALTH_APP_REG
:
4768 reply
.ret_code
= health_check_state(&health_thread_app_reg
);
4770 case LTTNG_HEALTH_KERNEL
:
4771 reply
.ret_code
= health_check_state(&health_thread_kernel
);
4773 case LTTNG_HEALTH_CONSUMER
:
4774 reply
.ret_code
= check_consumer_health();
4776 case LTTNG_HEALTH_ALL
:
4778 health_check_state(&health_thread_app_manage
) &&
4779 health_check_state(&health_thread_app_reg
) &&
4780 health_check_state(&health_thread_cmd
) &&
4781 health_check_state(&health_thread_kernel
) &&
4782 check_consumer_health();
4785 reply
.ret_code
= LTTCOMM_UND
;
4790 * Flip ret value since 0 is a success and 1 indicates a bad health for
4791 * the client where in the sessiond it is the opposite. Again, this is
4792 * just to make things easier for us poor developer which enjoy a lot
4795 if (reply
.ret_code
== 0 || reply
.ret_code
== 1) {
4796 reply
.ret_code
= !reply
.ret_code
;
4799 DBG2("Health check return value %d", reply
.ret_code
);
4801 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4803 ERR("Failed to send health data back to client");
4806 /* End of transmission */
4807 ret
= close(new_sock
);
4817 ERR("Health error occurred in %s", __func__
);
4819 DBG("Health check thread dying");
4820 unlink(health_unix_sock_path
);
4827 if (new_sock
>= 0) {
4828 ret
= close(new_sock
);
4834 lttng_poll_clean(&events
);
4836 rcu_unregister_thread();
4841 * This thread manage all clients request using the unix client socket for
4844 static void *thread_manage_clients(void *data
)
4846 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4848 uint32_t revents
, nb_fd
;
4849 struct command_ctx
*cmd_ctx
= NULL
;
4850 struct lttng_poll_event events
;
4852 DBG("[thread] Manage client started");
4854 rcu_register_thread();
4856 health_code_update(&health_thread_cmd
);
4858 ret
= lttcomm_listen_unix_sock(client_sock
);
4864 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4865 * more will be added to this poll set.
4867 ret
= create_thread_poll_set(&events
, 2);
4872 /* Add the application registration socket */
4873 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4879 * Notify parent pid that we are ready to accept command for client side.
4881 if (opt_sig_parent
) {
4882 kill(ppid
, SIGUSR1
);
4885 health_code_update(&health_thread_cmd
);
4888 DBG("Accepting client command ...");
4890 nb_fd
= LTTNG_POLL_GETNB(&events
);
4892 /* Inifinite blocking call, waiting for transmission */
4894 health_poll_update(&health_thread_cmd
);
4895 ret
= lttng_poll_wait(&events
, -1);
4896 health_poll_update(&health_thread_cmd
);
4899 * Restart interrupted system call.
4901 if (errno
== EINTR
) {
4907 for (i
= 0; i
< nb_fd
; i
++) {
4908 /* Fetch once the poll data */
4909 revents
= LTTNG_POLL_GETEV(&events
, i
);
4910 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4912 health_code_update(&health_thread_cmd
);
4914 /* Thread quit pipe has been closed. Killing thread. */
4915 ret
= check_thread_quit_pipe(pollfd
, revents
);
4921 /* Event on the registration socket */
4922 if (pollfd
== client_sock
) {
4923 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4924 ERR("Client socket poll error");
4930 DBG("Wait for client response");
4932 health_code_update(&health_thread_cmd
);
4934 sock
= lttcomm_accept_unix_sock(client_sock
);
4939 /* Set socket option for credentials retrieval */
4940 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4945 /* Allocate context command to process the client request */
4946 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4947 if (cmd_ctx
== NULL
) {
4948 PERROR("zmalloc cmd_ctx");
4952 /* Allocate data buffer for reception */
4953 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4954 if (cmd_ctx
->lsm
== NULL
) {
4955 PERROR("zmalloc cmd_ctx->lsm");
4959 cmd_ctx
->llm
= NULL
;
4960 cmd_ctx
->session
= NULL
;
4962 health_code_update(&health_thread_cmd
);
4965 * Data is received from the lttng client. The struct
4966 * lttcomm_session_msg (lsm) contains the command and data request of
4969 DBG("Receiving data from client ...");
4970 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4971 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4973 DBG("Nothing recv() from client... continuing");
4979 clean_command_ctx(&cmd_ctx
);
4983 health_code_update(&health_thread_cmd
);
4985 // TODO: Validate cmd_ctx including sanity check for
4986 // security purpose.
4988 rcu_thread_online();
4990 * This function dispatch the work to the kernel or userspace tracer
4991 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4992 * informations for the client. The command context struct contains
4993 * everything this function may needs.
4995 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4996 rcu_thread_offline();
5006 * TODO: Inform client somehow of the fatal error. At
5007 * this point, ret < 0 means that a zmalloc failed
5008 * (ENOMEM). Error detected but still accept
5009 * command, unless a socket error has been
5012 clean_command_ctx(&cmd_ctx
);
5016 health_code_update(&health_thread_cmd
);
5018 DBG("Sending response (size: %d, retcode: %s)",
5019 cmd_ctx
->lttng_msg_size
,
5020 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
5021 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
5023 ERR("Failed to send data back to client");
5026 /* End of transmission */
5033 clean_command_ctx(&cmd_ctx
);
5035 health_code_update(&health_thread_cmd
);
5041 health_error(&health_thread_cmd
);
5042 ERR("Health error occurred in %s", __func__
);
5044 health_exit(&health_thread_cmd
);
5046 DBG("Client thread dying");
5047 unlink(client_unix_sock_path
);
5048 if (client_sock
>= 0) {
5049 ret
= close(client_sock
);
5061 lttng_poll_clean(&events
);
5062 clean_command_ctx(&cmd_ctx
);
5064 rcu_unregister_thread();
5070 * usage function on stderr
5072 static void usage(void)
5074 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
5075 fprintf(stderr
, " -h, --help Display this usage.\n");
5076 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
5077 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
5078 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
5079 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
5080 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
5081 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
5082 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
5083 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
5084 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
5085 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
5086 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
5087 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
5088 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
5089 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
5090 fprintf(stderr
, " -V, --version Show version number.\n");
5091 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
5092 fprintf(stderr
, " -q, --quiet No output at all.\n");
5093 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
5094 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
5095 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
5099 * daemon argument parsing
5101 static int parse_args(int argc
, char **argv
)
5105 static struct option long_options
[] = {
5106 { "client-sock", 1, 0, 'c' },
5107 { "apps-sock", 1, 0, 'a' },
5108 { "kconsumerd-cmd-sock", 1, 0, 'C' },
5109 { "kconsumerd-err-sock", 1, 0, 'E' },
5110 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
5111 { "ustconsumerd32-err-sock", 1, 0, 'H' },
5112 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
5113 { "ustconsumerd64-err-sock", 1, 0, 'F' },
5114 { "consumerd32-path", 1, 0, 'u' },
5115 { "consumerd32-libdir", 1, 0, 'U' },
5116 { "consumerd64-path", 1, 0, 't' },
5117 { "consumerd64-libdir", 1, 0, 'T' },
5118 { "daemonize", 0, 0, 'd' },
5119 { "sig-parent", 0, 0, 'S' },
5120 { "help", 0, 0, 'h' },
5121 { "group", 1, 0, 'g' },
5122 { "version", 0, 0, 'V' },
5123 { "quiet", 0, 0, 'q' },
5124 { "verbose", 0, 0, 'v' },
5125 { "verbose-consumer", 0, 0, 'Z' },
5126 { "no-kernel", 0, 0, 'N' },
5131 int option_index
= 0;
5132 c
= getopt_long(argc
, argv
, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t",
5133 long_options
, &option_index
);
5140 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
5142 fprintf(stderr
, " with arg %s\n", optarg
);
5146 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5149 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5155 opt_tracing_group
= optarg
;
5161 fprintf(stdout
, "%s\n", VERSION
);
5167 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5170 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5173 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5176 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5179 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5182 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5188 lttng_opt_quiet
= 1;
5191 /* Verbose level can increase using multiple -v */
5192 lttng_opt_verbose
+= 1;
5195 opt_verbose_consumer
+= 1;
5198 consumerd32_bin
= optarg
;
5201 consumerd32_libdir
= optarg
;
5204 consumerd64_bin
= optarg
;
5207 consumerd64_libdir
= optarg
;
5210 /* Unknown option or other error.
5211 * Error is printed by getopt, just return */
5220 * Creates the two needed socket by the daemon.
5221 * apps_sock - The communication socket for all UST apps.
5222 * client_sock - The communication of the cli tool (lttng).
5224 static int init_daemon_socket(void)
5229 old_umask
= umask(0);
5231 /* Create client tool unix socket */
5232 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
5233 if (client_sock
< 0) {
5234 ERR("Create unix sock failed: %s", client_unix_sock_path
);
5239 /* File permission MUST be 660 */
5240 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5242 ERR("Set file permissions failed: %s", client_unix_sock_path
);
5247 /* Create the application unix socket */
5248 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
5249 if (apps_sock
< 0) {
5250 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
5255 /* File permission MUST be 666 */
5256 ret
= chmod(apps_unix_sock_path
,
5257 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5259 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
5270 * Check if the global socket is available, and if a daemon is answering at the
5271 * other side. If yes, error is returned.
5273 static int check_existing_daemon(void)
5275 /* Is there anybody out there ? */
5276 if (lttng_session_daemon_alive()) {
5284 * Set the tracing group gid onto the client socket.
5286 * Race window between mkdir and chown is OK because we are going from more
5287 * permissive (root.root) to less permissive (root.tracing).
5289 static int set_permissions(char *rundir
)
5294 ret
= allowed_group();
5296 WARN("No tracing group detected");
5303 /* Set lttng run dir */
5304 ret
= chown(rundir
, 0, gid
);
5306 ERR("Unable to set group on %s", rundir
);
5310 /* Ensure tracing group can search the run dir */
5311 ret
= chmod(rundir
, S_IRWXU
| S_IXGRP
| S_IXOTH
);
5313 ERR("Unable to set permissions on %s", rundir
);
5317 /* lttng client socket path */
5318 ret
= chown(client_unix_sock_path
, 0, gid
);
5320 ERR("Unable to set group on %s", client_unix_sock_path
);
5324 /* kconsumer error socket path */
5325 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, gid
);
5327 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5331 /* 64-bit ustconsumer error socket path */
5332 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, gid
);
5334 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5338 /* 32-bit ustconsumer compat32 error socket path */
5339 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, gid
);
5341 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5345 DBG("All permissions are set");
5352 * Create the lttng run directory needed for all global sockets and pipe.
5354 static int create_lttng_rundir(const char *rundir
)
5358 DBG3("Creating LTTng run directory: %s", rundir
);
5360 ret
= mkdir(rundir
, S_IRWXU
);
5362 if (errno
!= EEXIST
) {
5363 ERR("Unable to create %s", rundir
);
5375 * Setup sockets and directory needed by the kconsumerd communication with the
5378 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
5382 char path
[PATH_MAX
];
5384 switch (consumer_data
->type
) {
5385 case LTTNG_CONSUMER_KERNEL
:
5386 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
5388 case LTTNG_CONSUMER64_UST
:
5389 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
5391 case LTTNG_CONSUMER32_UST
:
5392 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
5395 ERR("Consumer type unknown");
5400 DBG2("Creating consumer directory: %s", path
);
5402 ret
= mkdir(path
, S_IRWXU
);
5404 if (errno
!= EEXIST
) {
5406 ERR("Failed to create %s", path
);
5412 /* Create the kconsumerd error unix socket */
5413 consumer_data
->err_sock
=
5414 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5415 if (consumer_data
->err_sock
< 0) {
5416 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5421 /* File permission MUST be 660 */
5422 ret
= chmod(consumer_data
->err_unix_sock_path
,
5423 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5425 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5435 * Signal handler for the daemon
5437 * Simply stop all worker threads, leaving main() return gracefully after
5438 * joining all threads and calling cleanup().
5440 static void sighandler(int sig
)
5444 DBG("SIGPIPE caught");
5447 DBG("SIGINT caught");
5451 DBG("SIGTERM caught");
5460 * Setup signal handler for :
5461 * SIGINT, SIGTERM, SIGPIPE
5463 static int set_signal_handler(void)
5466 struct sigaction sa
;
5469 if ((ret
= sigemptyset(&sigset
)) < 0) {
5470 PERROR("sigemptyset");
5474 sa
.sa_handler
= sighandler
;
5475 sa
.sa_mask
= sigset
;
5477 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5478 PERROR("sigaction");
5482 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5483 PERROR("sigaction");
5487 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5488 PERROR("sigaction");
5492 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
5498 * Set open files limit to unlimited. This daemon can open a large number of
5499 * file descriptors in order to consumer multiple kernel traces.
5501 static void set_ulimit(void)
5506 /* The kernel does not allowed an infinite limit for open files */
5507 lim
.rlim_cur
= 65535;
5508 lim
.rlim_max
= 65535;
5510 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5512 PERROR("failed to set open files limit");
5519 int main(int argc
, char **argv
)
5523 const char *home_path
;
5525 init_kernel_workarounds();
5527 rcu_register_thread();
5529 setup_consumerd_path();
5531 /* Parse arguments */
5533 if ((ret
= parse_args(argc
, argv
) < 0)) {
5543 * child: setsid, close FD 0, 1, 2, chdir /
5544 * parent: exit (if fork is successful)
5552 * We are in the child. Make sure all other file
5553 * descriptors are closed, in case we are called with
5554 * more opened file descriptors than the standard ones.
5556 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5561 /* Create thread quit pipe */
5562 if ((ret
= init_thread_quit_pipe()) < 0) {
5566 /* Check if daemon is UID = 0 */
5567 is_root
= !getuid();
5570 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
5572 /* Create global run dir with root access */
5573 ret
= create_lttng_rundir(rundir
);
5578 if (strlen(apps_unix_sock_path
) == 0) {
5579 snprintf(apps_unix_sock_path
, PATH_MAX
,
5580 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
5583 if (strlen(client_unix_sock_path
) == 0) {
5584 snprintf(client_unix_sock_path
, PATH_MAX
,
5585 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
5588 /* Set global SHM for ust */
5589 if (strlen(wait_shm_path
) == 0) {
5590 snprintf(wait_shm_path
, PATH_MAX
,
5591 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
5594 if (strlen(health_unix_sock_path
) == 0) {
5595 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
5596 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
5599 /* Setup kernel consumerd path */
5600 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
5601 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
5602 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
5603 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
5605 DBG2("Kernel consumer err path: %s",
5606 kconsumer_data
.err_unix_sock_path
);
5607 DBG2("Kernel consumer cmd path: %s",
5608 kconsumer_data
.cmd_unix_sock_path
);
5610 home_path
= get_home_dir();
5611 if (home_path
== NULL
) {
5612 /* TODO: Add --socket PATH option */
5613 ERR("Can't get HOME directory for sockets creation.");
5619 * Create rundir from home path. This will create something like
5622 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
5628 ret
= create_lttng_rundir(rundir
);
5633 if (strlen(apps_unix_sock_path
) == 0) {
5634 snprintf(apps_unix_sock_path
, PATH_MAX
,
5635 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
5638 /* Set the cli tool unix socket path */
5639 if (strlen(client_unix_sock_path
) == 0) {
5640 snprintf(client_unix_sock_path
, PATH_MAX
,
5641 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
5644 /* Set global SHM for ust */
5645 if (strlen(wait_shm_path
) == 0) {
5646 snprintf(wait_shm_path
, PATH_MAX
,
5647 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, geteuid());
5650 /* Set health check Unix path */
5651 if (strlen(health_unix_sock_path
) == 0) {
5652 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
5653 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
5657 /* Set consumer initial state */
5658 kernel_consumerd_state
= CONSUMER_STOPPED
;
5659 ust_consumerd_state
= CONSUMER_STOPPED
;
5661 DBG("Client socket path %s", client_unix_sock_path
);
5662 DBG("Application socket path %s", apps_unix_sock_path
);
5663 DBG("LTTng run directory path: %s", rundir
);
5665 /* 32 bits consumerd path setup */
5666 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
5667 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
5668 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
5669 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
5671 DBG2("UST consumer 32 bits err path: %s",
5672 ustconsumer32_data
.err_unix_sock_path
);
5673 DBG2("UST consumer 32 bits cmd path: %s",
5674 ustconsumer32_data
.cmd_unix_sock_path
);
5676 /* 64 bits consumerd path setup */
5677 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
5678 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
5679 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
5680 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
5682 DBG2("UST consumer 64 bits err path: %s",
5683 ustconsumer64_data
.err_unix_sock_path
);
5684 DBG2("UST consumer 64 bits cmd path: %s",
5685 ustconsumer64_data
.cmd_unix_sock_path
);
5688 * See if daemon already exist.
5690 if ((ret
= check_existing_daemon()) < 0) {
5691 ERR("Already running daemon.\n");
5693 * We do not goto exit because we must not cleanup()
5694 * because a daemon is already running.
5700 * Init UST app hash table. Alloc hash table before this point since
5701 * cleanup() can get called after that point.
5705 /* After this point, we can safely call cleanup() with "goto exit" */
5708 * These actions must be executed as root. We do that *after* setting up
5709 * the sockets path because we MUST make the check for another daemon using
5710 * those paths *before* trying to set the kernel consumer sockets and init
5714 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
5719 /* Setup kernel tracer */
5720 if (!opt_no_kernel
) {
5721 init_kernel_tracer();
5724 /* Set ulimit for open files */
5727 /* init lttng_fd tracking must be done after set_ulimit. */
5730 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
5735 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
5740 if ((ret
= set_signal_handler()) < 0) {
5744 /* Setup the needed unix socket */
5745 if ((ret
= init_daemon_socket()) < 0) {
5749 /* Set credentials to socket */
5750 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
5754 /* Get parent pid if -S, --sig-parent is specified. */
5755 if (opt_sig_parent
) {
5759 /* Setup the kernel pipe for waking up the kernel thread */
5760 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
5764 /* Setup the thread apps communication pipe. */
5765 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
5769 /* Init UST command queue. */
5770 cds_wfq_init(&ust_cmd_queue
.queue
);
5773 * Get session list pointer. This pointer MUST NOT be free(). This list is
5774 * statically declared in session.c
5776 session_list_ptr
= session_get_list();
5778 /* Set up max poll set size */
5779 lttng_poll_set_max_size();
5782 * Set network sequence index to 1 for streams to match a relayd socket on
5783 * the consumer side.
5785 uatomic_set(&relayd_net_seq_idx
, 1);
5787 /* Init all health thread counters. */
5788 health_init(&health_thread_cmd
);
5789 health_init(&health_thread_kernel
);
5790 health_init(&health_thread_app_manage
);
5791 health_init(&health_thread_app_reg
);
5794 * Init health counters of the consumer thread. We do a quick hack here to
5795 * the state of the consumer health is fine even if the thread is not
5796 * started. This is simply to ease our life and has no cost what so ever.
5798 health_init(&kconsumer_data
.health
);
5799 health_poll_update(&kconsumer_data
.health
);
5800 health_init(&ustconsumer32_data
.health
);
5801 health_poll_update(&ustconsumer32_data
.health
);
5802 health_init(&ustconsumer64_data
.health
);
5803 health_poll_update(&ustconsumer64_data
.health
);
5805 /* Create thread to manage the client socket */
5806 ret
= pthread_create(&health_thread
, NULL
,
5807 thread_manage_health
, (void *) NULL
);
5809 PERROR("pthread_create health");
5813 /* Create thread to manage the client socket */
5814 ret
= pthread_create(&client_thread
, NULL
,
5815 thread_manage_clients
, (void *) NULL
);
5817 PERROR("pthread_create clients");
5821 /* Create thread to dispatch registration */
5822 ret
= pthread_create(&dispatch_thread
, NULL
,
5823 thread_dispatch_ust_registration
, (void *) NULL
);
5825 PERROR("pthread_create dispatch");
5829 /* Create thread to manage application registration. */
5830 ret
= pthread_create(®_apps_thread
, NULL
,
5831 thread_registration_apps
, (void *) NULL
);
5833 PERROR("pthread_create registration");
5837 /* Create thread to manage application socket */
5838 ret
= pthread_create(&apps_thread
, NULL
,
5839 thread_manage_apps
, (void *) NULL
);
5841 PERROR("pthread_create apps");
5845 /* Create kernel thread to manage kernel event */
5846 ret
= pthread_create(&kernel_thread
, NULL
,
5847 thread_manage_kernel
, (void *) NULL
);
5849 PERROR("pthread_create kernel");
5853 ret
= pthread_join(kernel_thread
, &status
);
5855 PERROR("pthread_join");
5856 goto error
; /* join error, exit without cleanup */
5860 ret
= pthread_join(apps_thread
, &status
);
5862 PERROR("pthread_join");
5863 goto error
; /* join error, exit without cleanup */
5867 ret
= pthread_join(reg_apps_thread
, &status
);
5869 PERROR("pthread_join");
5870 goto error
; /* join error, exit without cleanup */
5874 ret
= pthread_join(dispatch_thread
, &status
);
5876 PERROR("pthread_join");
5877 goto error
; /* join error, exit without cleanup */
5881 ret
= pthread_join(client_thread
, &status
);
5883 PERROR("pthread_join");
5884 goto error
; /* join error, exit without cleanup */
5887 ret
= join_consumer_thread(&kconsumer_data
);
5889 PERROR("join_consumer");
5890 goto error
; /* join error, exit without cleanup */
5897 * cleanup() is called when no other thread is running.
5899 rcu_thread_online();
5901 rcu_thread_offline();
5902 rcu_unregister_thread();