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>
48 #include "lttng-sessiond.h"
54 #include "kernel-consumer.h"
58 #include "ust-consumer.h"
62 #define CONSUMERD_FILE "lttng-consumerd"
65 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
66 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
67 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
68 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
71 const char *opt_tracing_group
;
72 static int opt_sig_parent
;
73 static int opt_verbose_consumer
;
74 static int opt_daemon
;
75 static int opt_no_kernel
;
76 static int is_root
; /* Set to 1 if the daemon is running as root */
77 static pid_t ppid
; /* Parent PID for --sig-parent option */
80 /* Consumer daemon specific control data */
81 static struct consumer_data kconsumer_data
= {
82 .type
= LTTNG_CONSUMER_KERNEL
,
83 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
84 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
88 static struct consumer_data ustconsumer64_data
= {
89 .type
= LTTNG_CONSUMER64_UST
,
90 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
91 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
95 static struct consumer_data ustconsumer32_data
= {
96 .type
= LTTNG_CONSUMER32_UST
,
97 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
98 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
103 static int dispatch_thread_exit
;
105 /* Global application Unix socket path */
106 static char apps_unix_sock_path
[PATH_MAX
];
107 /* Global client Unix socket path */
108 static char client_unix_sock_path
[PATH_MAX
];
109 /* global wait shm path for UST */
110 static char wait_shm_path
[PATH_MAX
];
112 /* Sockets and FDs */
113 static int client_sock
= -1;
114 static int apps_sock
= -1;
115 static int kernel_tracer_fd
= -1;
116 static int kernel_poll_pipe
[2] = { -1, -1 };
119 * Quit pipe for all threads. This permits a single cancellation point
120 * for all threads when receiving an event on the pipe.
122 static int thread_quit_pipe
[2] = { -1, -1 };
125 * This pipe is used to inform the thread managing application communication
126 * that a command is queued and ready to be processed.
128 static int apps_cmd_pipe
[2] = { -1, -1 };
130 /* Pthread, Mutexes and Semaphores */
131 static pthread_t apps_thread
;
132 static pthread_t reg_apps_thread
;
133 static pthread_t client_thread
;
134 static pthread_t kernel_thread
;
135 static pthread_t dispatch_thread
;
138 * UST registration command queue. This queue is tied with a futex and uses a N
139 * wakers / 1 waiter implemented and detailed in futex.c/.h
141 * The thread_manage_apps and thread_dispatch_ust_registration interact with
142 * this queue and the wait/wake scheme.
144 static struct ust_cmd_queue ust_cmd_queue
;
147 * Pointer initialized before thread creation.
149 * This points to the tracing session list containing the session count and a
150 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
151 * MUST NOT be taken if you call a public function in session.c.
153 * The lock is nested inside the structure: session_list_ptr->lock. Please use
154 * session_lock_list and session_unlock_list for lock acquisition.
156 static struct ltt_session_list
*session_list_ptr
;
158 int ust_consumerd64_fd
= -1;
159 int ust_consumerd32_fd
= -1;
161 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
162 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
163 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
164 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
167 * Consumer daemon state which is changed when spawning it, killing it or in
168 * case of a fatal error.
170 enum consumerd_state
{
171 CONSUMER_STARTED
= 1,
172 CONSUMER_STOPPED
= 2,
177 * This consumer daemon state is used to validate if a client command will be
178 * able to reach the consumer. If not, the client is informed. For instance,
179 * doing a "lttng start" when the consumer state is set to ERROR will return an
180 * error to the client.
182 * The following example shows a possible race condition of this scheme:
184 * consumer thread error happens
186 * client cmd checks state -> still OK
187 * consumer thread exit, sets error
188 * client cmd try to talk to consumer
191 * However, since the consumer is a different daemon, we have no way of making
192 * sure the command will reach it safely even with this state flag. This is why
193 * we consider that up to the state validation during command processing, the
194 * command is safe. After that, we can not guarantee the correctness of the
195 * client request vis-a-vis the consumer.
197 static enum consumerd_state ust_consumerd_state
;
198 static enum consumerd_state kernel_consumerd_state
;
201 * Used to keep a unique index for each relayd socket created where this value
202 * is associated with streams on the consumer so it can match the right relayd
205 * This value should be incremented atomically for safety purposes and future
206 * possible concurrent access.
208 static unsigned int relayd_net_seq_idx
;
211 void setup_consumerd_path(void)
213 const char *bin
, *libdir
;
216 * Allow INSTALL_BIN_PATH to be used as a target path for the
217 * native architecture size consumer if CONFIG_CONSUMER*_PATH
218 * has not been defined.
220 #if (CAA_BITS_PER_LONG == 32)
221 if (!consumerd32_bin
[0]) {
222 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
224 if (!consumerd32_libdir
[0]) {
225 consumerd32_libdir
= INSTALL_LIB_PATH
;
227 #elif (CAA_BITS_PER_LONG == 64)
228 if (!consumerd64_bin
[0]) {
229 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
231 if (!consumerd64_libdir
[0]) {
232 consumerd64_libdir
= INSTALL_LIB_PATH
;
235 #error "Unknown bitness"
239 * runtime env. var. overrides the build default.
241 bin
= getenv("LTTNG_CONSUMERD32_BIN");
243 consumerd32_bin
= bin
;
245 bin
= getenv("LTTNG_CONSUMERD64_BIN");
247 consumerd64_bin
= bin
;
249 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
251 consumerd32_libdir
= libdir
;
253 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
255 consumerd64_libdir
= libdir
;
260 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
262 static int create_thread_poll_set(struct lttng_poll_event
*events
,
267 if (events
== NULL
|| size
== 0) {
272 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
278 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
290 * Check if the thread quit pipe was triggered.
292 * Return 1 if it was triggered else 0;
294 static int check_thread_quit_pipe(int fd
, uint32_t events
)
296 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
304 * Return group ID of the tracing group or -1 if not found.
306 static gid_t
allowed_group(void)
310 if (opt_tracing_group
) {
311 grp
= getgrnam(opt_tracing_group
);
313 grp
= getgrnam(default_tracing_group
);
323 * Init thread quit pipe.
325 * Return -1 on error or 0 if all pipes are created.
327 static int init_thread_quit_pipe(void)
331 ret
= pipe(thread_quit_pipe
);
333 PERROR("thread quit pipe");
337 for (i
= 0; i
< 2; i
++) {
338 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
350 * Complete teardown of a kernel session. This free all data structure related
351 * to a kernel session and update counter.
353 static void teardown_kernel_session(struct ltt_session
*session
)
355 if (!session
->kernel_session
) {
356 DBG3("No kernel session when tearing down session");
360 DBG("Tearing down kernel session");
363 * If a custom kernel consumer was registered, close the socket before
364 * tearing down the complete kernel session structure
366 if (kconsumer_data
.cmd_sock
>= 0 &&
367 session
->kernel_session
->consumer_fd
!= kconsumer_data
.cmd_sock
) {
368 lttcomm_close_unix_sock(session
->kernel_session
->consumer_fd
);
371 trace_kernel_destroy_session(session
->kernel_session
);
375 * Complete teardown of all UST sessions. This will free everything on his path
376 * and destroy the core essence of all ust sessions :)
378 static void teardown_ust_session(struct ltt_session
*session
)
382 if (!session
->ust_session
) {
383 DBG3("No UST session when tearing down session");
387 DBG("Tearing down UST session(s)");
389 ret
= ust_app_destroy_trace_all(session
->ust_session
);
391 ERR("Error in ust_app_destroy_trace_all");
394 trace_ust_destroy_session(session
->ust_session
);
398 * Stop all threads by closing the thread quit pipe.
400 static void stop_threads(void)
404 /* Stopping all threads */
405 DBG("Terminating all threads");
406 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
408 ERR("write error on thread quit pipe");
411 /* Dispatch thread */
412 dispatch_thread_exit
= 1;
413 futex_nto1_wake(&ust_cmd_queue
.futex
);
419 static void cleanup(void)
423 struct ltt_session
*sess
, *stmp
;
427 DBG("Removing %s directory", rundir
);
428 ret
= asprintf(&cmd
, "rm -rf %s", rundir
);
430 ERR("asprintf failed. Something is really wrong!");
433 /* Remove lttng run directory */
436 ERR("Unable to clean %s", rundir
);
440 DBG("Cleaning up all sessions");
442 /* Destroy session list mutex */
443 if (session_list_ptr
!= NULL
) {
444 pthread_mutex_destroy(&session_list_ptr
->lock
);
446 /* Cleanup ALL session */
447 cds_list_for_each_entry_safe(sess
, stmp
,
448 &session_list_ptr
->head
, list
) {
449 teardown_kernel_session(sess
);
450 teardown_ust_session(sess
);
455 DBG("Closing all UST sockets");
456 ust_app_clean_list();
458 pthread_mutex_destroy(&kconsumer_data
.pid_mutex
);
460 if (is_root
&& !opt_no_kernel
) {
461 DBG2("Closing kernel fd");
462 if (kernel_tracer_fd
>= 0) {
463 ret
= close(kernel_tracer_fd
);
468 DBG("Unloading kernel modules");
469 modprobe_remove_lttng_all();
471 utils_close_pipe(kernel_poll_pipe
);
472 utils_close_pipe(thread_quit_pipe
);
473 utils_close_pipe(apps_cmd_pipe
);
476 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
477 "Matthew, BEET driven development works!%c[%dm",
478 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
483 * Send data on a unix socket using the liblttsessiondcomm API.
485 * Return lttcomm error code.
487 static int send_unix_sock(int sock
, void *buf
, size_t len
)
489 /* Check valid length */
494 return lttcomm_send_unix_sock(sock
, buf
, len
);
498 * Free memory of a command context structure.
500 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
502 DBG("Clean command context structure");
504 if ((*cmd_ctx
)->llm
) {
505 free((*cmd_ctx
)->llm
);
507 if ((*cmd_ctx
)->lsm
) {
508 free((*cmd_ctx
)->lsm
);
516 * Notify UST applications using the shm mmap futex.
518 static int notify_ust_apps(int active
)
522 DBG("Notifying applications of session daemon state: %d", active
);
524 /* See shm.c for this call implying mmap, shm and futex calls */
525 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
526 if (wait_shm_mmap
== NULL
) {
530 /* Wake waiting process */
531 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
533 /* Apps notified successfully */
541 * Setup the outgoing data buffer for the response (llm) by allocating the
542 * right amount of memory and copying the original information from the lsm
545 * Return total size of the buffer pointed by buf.
547 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
553 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
554 if (cmd_ctx
->llm
== NULL
) {
560 /* Copy common data */
561 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
562 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
564 cmd_ctx
->llm
->data_size
= size
;
565 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
574 * Update the kernel poll set of all channel fd available over all tracing
575 * session. Add the wakeup pipe at the end of the set.
577 static int update_kernel_poll(struct lttng_poll_event
*events
)
580 struct ltt_session
*session
;
581 struct ltt_kernel_channel
*channel
;
583 DBG("Updating kernel poll set");
586 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
587 session_lock(session
);
588 if (session
->kernel_session
== NULL
) {
589 session_unlock(session
);
593 cds_list_for_each_entry(channel
,
594 &session
->kernel_session
->channel_list
.head
, list
) {
595 /* Add channel fd to the kernel poll set */
596 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
598 session_unlock(session
);
601 DBG("Channel fd %d added to kernel set", channel
->fd
);
603 session_unlock(session
);
605 session_unlock_list();
610 session_unlock_list();
615 * Find the channel fd from 'fd' over all tracing session. When found, check
616 * for new channel stream and send those stream fds to the kernel consumer.
618 * Useful for CPU hotplug feature.
620 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
623 struct ltt_session
*session
;
624 struct ltt_kernel_channel
*channel
;
626 DBG("Updating kernel streams for channel fd %d", fd
);
629 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
630 session_lock(session
);
631 if (session
->kernel_session
== NULL
) {
632 session_unlock(session
);
636 /* This is not suppose to be -1 but this is an extra security check */
637 if (session
->kernel_session
->consumer_fd
< 0) {
638 session
->kernel_session
->consumer_fd
= consumer_data
->cmd_sock
;
641 cds_list_for_each_entry(channel
,
642 &session
->kernel_session
->channel_list
.head
, list
) {
643 if (channel
->fd
== fd
) {
644 DBG("Channel found, updating kernel streams");
645 ret
= kernel_open_channel_stream(channel
);
651 * Have we already sent fds to the consumer? If yes, it means
652 * that tracing is started so it is safe to send our updated
655 if (session
->kernel_session
->consumer_fds_sent
== 1 &&
656 session
->kernel_session
->consumer
!= NULL
) {
657 ret
= kernel_consumer_send_channel_stream(
658 session
->kernel_session
->consumer_fd
, channel
,
659 session
->kernel_session
);
667 session_unlock(session
);
669 session_unlock_list();
673 session_unlock(session
);
674 session_unlock_list();
679 * For each tracing session, update newly registered apps.
681 static void update_ust_app(int app_sock
)
683 struct ltt_session
*sess
, *stmp
;
687 /* For all tracing session(s) */
688 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
690 if (sess
->ust_session
) {
691 ust_app_global_update(sess
->ust_session
, app_sock
);
693 session_unlock(sess
);
696 session_unlock_list();
700 * This thread manage event coming from the kernel.
702 * Features supported in this thread:
705 static void *thread_manage_kernel(void *data
)
707 int ret
, i
, pollfd
, update_poll_flag
= 1;
708 uint32_t revents
, nb_fd
;
710 struct lttng_poll_event events
;
712 DBG("Thread manage kernel started");
714 ret
= create_thread_poll_set(&events
, 2);
716 goto error_poll_create
;
719 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
725 if (update_poll_flag
== 1) {
727 * Reset number of fd in the poll set. Always 2 since there is the thread
728 * quit pipe and the kernel pipe.
732 ret
= update_kernel_poll(&events
);
736 update_poll_flag
= 0;
739 nb_fd
= LTTNG_POLL_GETNB(&events
);
741 DBG("Thread kernel polling on %d fds", nb_fd
);
743 /* Zeroed the poll events */
744 lttng_poll_reset(&events
);
746 /* Poll infinite value of time */
748 ret
= lttng_poll_wait(&events
, -1);
751 * Restart interrupted system call.
753 if (errno
== EINTR
) {
757 } else if (ret
== 0) {
758 /* Should not happen since timeout is infinite */
759 ERR("Return value of poll is 0 with an infinite timeout.\n"
760 "This should not have happened! Continuing...");
764 for (i
= 0; i
< nb_fd
; i
++) {
765 /* Fetch once the poll data */
766 revents
= LTTNG_POLL_GETEV(&events
, i
);
767 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
769 /* Thread quit pipe has been closed. Killing thread. */
770 ret
= check_thread_quit_pipe(pollfd
, revents
);
775 /* Check for data on kernel pipe */
776 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
777 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
778 update_poll_flag
= 1;
782 * New CPU detected by the kernel. Adding kernel stream to
783 * kernel session and updating the kernel consumer
785 if (revents
& LPOLLIN
) {
786 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
792 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
793 * and unregister kernel stream at this point.
801 lttng_poll_clean(&events
);
803 DBG("Kernel thread dying");
808 * This thread manage the consumer error sent back to the session daemon.
810 static void *thread_manage_consumer(void *data
)
812 int sock
= -1, i
, ret
, pollfd
;
813 uint32_t revents
, nb_fd
;
814 enum lttcomm_return_code code
;
815 struct lttng_poll_event events
;
816 struct consumer_data
*consumer_data
= data
;
818 DBG("[thread] Manage consumer started");
820 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
826 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
827 * Nothing more will be added to this poll set.
829 ret
= create_thread_poll_set(&events
, 2);
834 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
839 nb_fd
= LTTNG_POLL_GETNB(&events
);
841 /* Inifinite blocking call, waiting for transmission */
843 ret
= lttng_poll_wait(&events
, -1);
846 * Restart interrupted system call.
848 if (errno
== EINTR
) {
854 for (i
= 0; i
< nb_fd
; i
++) {
855 /* Fetch once the poll data */
856 revents
= LTTNG_POLL_GETEV(&events
, i
);
857 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
859 /* Thread quit pipe has been closed. Killing thread. */
860 ret
= check_thread_quit_pipe(pollfd
, revents
);
865 /* Event on the registration socket */
866 if (pollfd
== consumer_data
->err_sock
) {
867 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
868 ERR("consumer err socket poll error");
874 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
879 DBG2("Receiving code from consumer err_sock");
881 /* Getting status code from kconsumerd */
882 ret
= lttcomm_recv_unix_sock(sock
, &code
,
883 sizeof(enum lttcomm_return_code
));
888 if (code
== CONSUMERD_COMMAND_SOCK_READY
) {
889 consumer_data
->cmd_sock
=
890 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
891 if (consumer_data
->cmd_sock
< 0) {
892 sem_post(&consumer_data
->sem
);
893 PERROR("consumer connect");
896 /* Signal condition to tell that the kconsumerd is ready */
897 sem_post(&consumer_data
->sem
);
898 DBG("consumer command socket ready");
900 ERR("consumer error when waiting for SOCK_READY : %s",
901 lttcomm_get_readable_code(-code
));
905 /* Remove the kconsumerd error sock since we've established a connexion */
906 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
911 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
916 /* Update number of fd */
917 nb_fd
= LTTNG_POLL_GETNB(&events
);
919 /* Inifinite blocking call, waiting for transmission */
921 ret
= lttng_poll_wait(&events
, -1);
924 * Restart interrupted system call.
926 if (errno
== EINTR
) {
932 for (i
= 0; i
< nb_fd
; i
++) {
933 /* Fetch once the poll data */
934 revents
= LTTNG_POLL_GETEV(&events
, i
);
935 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
937 /* Thread quit pipe has been closed. Killing thread. */
938 ret
= check_thread_quit_pipe(pollfd
, revents
);
943 /* Event on the kconsumerd socket */
944 if (pollfd
== sock
) {
945 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
946 ERR("consumer err socket second poll error");
952 /* Wait for any kconsumerd error */
953 ret
= lttcomm_recv_unix_sock(sock
, &code
,
954 sizeof(enum lttcomm_return_code
));
956 ERR("consumer closed the command socket");
960 ERR("consumer return code : %s", lttcomm_get_readable_code(-code
));
963 /* Immediately set the consumerd state to stopped */
964 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
965 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
966 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
967 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
968 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
970 /* Code flow error... */
974 if (consumer_data
->err_sock
>= 0) {
975 ret
= close(consumer_data
->err_sock
);
980 if (consumer_data
->cmd_sock
>= 0) {
981 ret
= close(consumer_data
->cmd_sock
);
993 unlink(consumer_data
->err_unix_sock_path
);
994 unlink(consumer_data
->cmd_unix_sock_path
);
995 consumer_data
->pid
= 0;
997 lttng_poll_clean(&events
);
1000 DBG("consumer thread cleanup completed");
1006 * This thread manage application communication.
1008 static void *thread_manage_apps(void *data
)
1011 uint32_t revents
, nb_fd
;
1012 struct ust_command ust_cmd
;
1013 struct lttng_poll_event events
;
1015 DBG("[thread] Manage application started");
1017 rcu_register_thread();
1018 rcu_thread_online();
1020 ret
= create_thread_poll_set(&events
, 2);
1022 goto error_poll_create
;
1025 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1031 /* Zeroed the events structure */
1032 lttng_poll_reset(&events
);
1034 nb_fd
= LTTNG_POLL_GETNB(&events
);
1036 DBG("Apps thread polling on %d fds", nb_fd
);
1038 /* Inifinite blocking call, waiting for transmission */
1040 ret
= lttng_poll_wait(&events
, -1);
1043 * Restart interrupted system call.
1045 if (errno
== EINTR
) {
1051 for (i
= 0; i
< nb_fd
; i
++) {
1052 /* Fetch once the poll data */
1053 revents
= LTTNG_POLL_GETEV(&events
, i
);
1054 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1056 /* Thread quit pipe has been closed. Killing thread. */
1057 ret
= check_thread_quit_pipe(pollfd
, revents
);
1062 /* Inspect the apps cmd pipe */
1063 if (pollfd
== apps_cmd_pipe
[0]) {
1064 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1065 ERR("Apps command pipe error");
1067 } else if (revents
& LPOLLIN
) {
1069 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
1070 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
1071 PERROR("read apps cmd pipe");
1075 /* Register applicaton to the session daemon */
1076 ret
= ust_app_register(&ust_cmd
.reg_msg
,
1078 if (ret
== -ENOMEM
) {
1080 } else if (ret
< 0) {
1085 * Validate UST version compatibility.
1087 ret
= ust_app_validate_version(ust_cmd
.sock
);
1090 * Add channel(s) and event(s) to newly registered apps
1091 * from lttng global UST domain.
1093 update_ust_app(ust_cmd
.sock
);
1096 ret
= ust_app_register_done(ust_cmd
.sock
);
1099 * If the registration is not possible, we simply
1100 * unregister the apps and continue
1102 ust_app_unregister(ust_cmd
.sock
);
1105 * We just need here to monitor the close of the UST
1106 * socket and poll set monitor those by default.
1107 * Listen on POLLIN (even if we never expect any
1108 * data) to ensure that hangup wakes us.
1110 ret
= lttng_poll_add(&events
, ust_cmd
.sock
, LPOLLIN
);
1115 DBG("Apps with sock %d added to poll set",
1123 * At this point, we know that a registered application made
1124 * the event at poll_wait.
1126 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1127 /* Removing from the poll set */
1128 ret
= lttng_poll_del(&events
, pollfd
);
1133 /* Socket closed on remote end. */
1134 ust_app_unregister(pollfd
);
1142 lttng_poll_clean(&events
);
1144 DBG("Application communication apps thread cleanup complete");
1145 rcu_thread_offline();
1146 rcu_unregister_thread();
1151 * Dispatch request from the registration threads to the application
1152 * communication thread.
1154 static void *thread_dispatch_ust_registration(void *data
)
1157 struct cds_wfq_node
*node
;
1158 struct ust_command
*ust_cmd
= NULL
;
1160 DBG("[thread] Dispatch UST command started");
1162 while (!dispatch_thread_exit
) {
1163 /* Atomically prepare the queue futex */
1164 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1167 /* Dequeue command for registration */
1168 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1170 DBG("Woken up but nothing in the UST command queue");
1171 /* Continue thread execution */
1175 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1177 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1178 " gid:%d sock:%d name:%s (version %d.%d)",
1179 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1180 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1181 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1182 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1184 * Inform apps thread of the new application registration. This
1185 * call is blocking so we can be assured that the data will be read
1186 * at some point in time or wait to the end of the world :)
1188 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1189 sizeof(struct ust_command
));
1191 PERROR("write apps cmd pipe");
1192 if (errno
== EBADF
) {
1194 * We can't inform the application thread to process
1195 * registration. We will exit or else application
1196 * registration will not occur and tracing will never
1203 } while (node
!= NULL
);
1205 /* Futex wait on queue. Blocking call on futex() */
1206 futex_nto1_wait(&ust_cmd_queue
.futex
);
1210 DBG("Dispatch thread dying");
1215 * This thread manage application registration.
1217 static void *thread_registration_apps(void *data
)
1219 int sock
= -1, i
, ret
, pollfd
;
1220 uint32_t revents
, nb_fd
;
1221 struct lttng_poll_event events
;
1223 * Get allocated in this thread, enqueued to a global queue, dequeued and
1224 * freed in the manage apps thread.
1226 struct ust_command
*ust_cmd
= NULL
;
1228 DBG("[thread] Manage application registration started");
1230 ret
= lttcomm_listen_unix_sock(apps_sock
);
1236 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1237 * more will be added to this poll set.
1239 ret
= create_thread_poll_set(&events
, 2);
1241 goto error_create_poll
;
1244 /* Add the application registration socket */
1245 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1247 goto error_poll_add
;
1250 /* Notify all applications to register */
1251 ret
= notify_ust_apps(1);
1253 ERR("Failed to notify applications or create the wait shared memory.\n"
1254 "Execution continues but there might be problem for already\n"
1255 "running applications that wishes to register.");
1259 DBG("Accepting application registration");
1261 nb_fd
= LTTNG_POLL_GETNB(&events
);
1263 /* Inifinite blocking call, waiting for transmission */
1265 ret
= lttng_poll_wait(&events
, -1);
1268 * Restart interrupted system call.
1270 if (errno
== EINTR
) {
1276 for (i
= 0; i
< nb_fd
; i
++) {
1277 /* Fetch once the poll data */
1278 revents
= LTTNG_POLL_GETEV(&events
, i
);
1279 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1281 /* Thread quit pipe has been closed. Killing thread. */
1282 ret
= check_thread_quit_pipe(pollfd
, revents
);
1287 /* Event on the registration socket */
1288 if (pollfd
== apps_sock
) {
1289 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1290 ERR("Register apps socket poll error");
1292 } else if (revents
& LPOLLIN
) {
1293 sock
= lttcomm_accept_unix_sock(apps_sock
);
1298 /* Create UST registration command for enqueuing */
1299 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1300 if (ust_cmd
== NULL
) {
1301 PERROR("ust command zmalloc");
1306 * Using message-based transmissions to ensure we don't
1307 * have to deal with partially received messages.
1309 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1311 ERR("Exhausted file descriptors allowed for applications.");
1320 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1321 sizeof(struct ust_register_msg
));
1322 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1324 PERROR("lttcomm_recv_unix_sock register apps");
1326 ERR("Wrong size received on apps register");
1333 lttng_fd_put(LTTNG_FD_APPS
, 1);
1338 ust_cmd
->sock
= sock
;
1341 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1342 " gid:%d sock:%d name:%s (version %d.%d)",
1343 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1344 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1345 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1346 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1349 * Lock free enqueue the registration request. The red pill
1350 * has been taken! This apps will be part of the *system*.
1352 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1355 * Wake the registration queue futex. Implicit memory
1356 * barrier with the exchange in cds_wfq_enqueue.
1358 futex_nto1_wake(&ust_cmd_queue
.futex
);
1365 /* Notify that the registration thread is gone */
1368 if (apps_sock
>= 0) {
1369 ret
= close(apps_sock
);
1379 lttng_fd_put(LTTNG_FD_APPS
, 1);
1381 unlink(apps_unix_sock_path
);
1384 lttng_poll_clean(&events
);
1387 DBG("UST Registration thread cleanup complete");
1393 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1394 * exec or it will fails.
1396 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
1399 struct timespec timeout
;
1401 timeout
.tv_sec
= DEFAULT_SEM_WAIT_TIMEOUT
;
1402 timeout
.tv_nsec
= 0;
1404 /* Setup semaphore */
1405 ret
= sem_init(&consumer_data
->sem
, 0, 0);
1407 PERROR("sem_init consumer semaphore");
1411 ret
= pthread_create(&consumer_data
->thread
, NULL
,
1412 thread_manage_consumer
, consumer_data
);
1414 PERROR("pthread_create consumer");
1419 /* Get time for sem_timedwait absolute timeout */
1420 ret
= clock_gettime(CLOCK_REALTIME
, &timeout
);
1422 PERROR("clock_gettime spawn consumer");
1423 /* Infinite wait for the kconsumerd thread to be ready */
1424 ret
= sem_wait(&consumer_data
->sem
);
1426 /* Normal timeout if the gettime was successful */
1427 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
1428 ret
= sem_timedwait(&consumer_data
->sem
, &timeout
);
1432 if (errno
== ETIMEDOUT
) {
1434 * Call has timed out so we kill the kconsumerd_thread and return
1437 ERR("The consumer thread was never ready. Killing it");
1438 ret
= pthread_cancel(consumer_data
->thread
);
1440 PERROR("pthread_cancel consumer thread");
1443 PERROR("semaphore wait failed consumer thread");
1448 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1449 if (consumer_data
->pid
== 0) {
1450 ERR("Kconsumerd did not start");
1451 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1454 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1463 * Join consumer thread
1465 static int join_consumer_thread(struct consumer_data
*consumer_data
)
1470 if (consumer_data
->pid
!= 0) {
1471 ret
= kill(consumer_data
->pid
, SIGTERM
);
1473 ERR("Error killing consumer daemon");
1476 return pthread_join(consumer_data
->thread
, &status
);
1483 * Fork and exec a consumer daemon (consumerd).
1485 * Return pid if successful else -1.
1487 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
1491 const char *consumer_to_use
;
1492 const char *verbosity
;
1495 DBG("Spawning consumerd");
1502 if (opt_verbose_consumer
) {
1503 verbosity
= "--verbose";
1505 verbosity
= "--quiet";
1507 switch (consumer_data
->type
) {
1508 case LTTNG_CONSUMER_KERNEL
:
1510 * Find out which consumerd to execute. We will first try the
1511 * 64-bit path, then the sessiond's installation directory, and
1512 * fallback on the 32-bit one,
1514 DBG3("Looking for a kernel consumer at these locations:");
1515 DBG3(" 1) %s", consumerd64_bin
);
1516 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
1517 DBG3(" 3) %s", consumerd32_bin
);
1518 if (stat(consumerd64_bin
, &st
) == 0) {
1519 DBG3("Found location #1");
1520 consumer_to_use
= consumerd64_bin
;
1521 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
1522 DBG3("Found location #2");
1523 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
1524 } else if (stat(consumerd32_bin
, &st
) == 0) {
1525 DBG3("Found location #3");
1526 consumer_to_use
= consumerd32_bin
;
1528 DBG("Could not find any valid consumerd executable");
1531 DBG("Using kernel consumer at: %s", consumer_to_use
);
1532 execl(consumer_to_use
,
1533 "lttng-consumerd", verbosity
, "-k",
1534 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1535 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1538 case LTTNG_CONSUMER64_UST
:
1540 char *tmpnew
= NULL
;
1542 if (consumerd64_libdir
[0] != '\0') {
1546 tmp
= getenv("LD_LIBRARY_PATH");
1550 tmplen
= strlen("LD_LIBRARY_PATH=")
1551 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
1552 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1557 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1558 strcat(tmpnew
, consumerd64_libdir
);
1559 if (tmp
[0] != '\0') {
1560 strcat(tmpnew
, ":");
1561 strcat(tmpnew
, tmp
);
1563 ret
= putenv(tmpnew
);
1569 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
1570 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
1571 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1572 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1574 if (consumerd64_libdir
[0] != '\0') {
1582 case LTTNG_CONSUMER32_UST
:
1584 char *tmpnew
= NULL
;
1586 if (consumerd32_libdir
[0] != '\0') {
1590 tmp
= getenv("LD_LIBRARY_PATH");
1594 tmplen
= strlen("LD_LIBRARY_PATH=")
1595 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
1596 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1601 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1602 strcat(tmpnew
, consumerd32_libdir
);
1603 if (tmp
[0] != '\0') {
1604 strcat(tmpnew
, ":");
1605 strcat(tmpnew
, tmp
);
1607 ret
= putenv(tmpnew
);
1613 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
1614 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
1615 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1616 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1618 if (consumerd32_libdir
[0] != '\0') {
1627 PERROR("unknown consumer type");
1631 PERROR("kernel start consumer exec");
1634 } else if (pid
> 0) {
1637 PERROR("start consumer fork");
1645 * Spawn the consumerd daemon and session daemon thread.
1647 static int start_consumerd(struct consumer_data
*consumer_data
)
1651 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1652 if (consumer_data
->pid
!= 0) {
1653 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1657 ret
= spawn_consumerd(consumer_data
);
1659 ERR("Spawning consumerd failed");
1660 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1664 /* Setting up the consumer_data pid */
1665 consumer_data
->pid
= ret
;
1666 DBG2("Consumer pid %d", consumer_data
->pid
);
1667 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1669 DBG2("Spawning consumer control thread");
1670 ret
= spawn_consumer_thread(consumer_data
);
1672 ERR("Fatal error spawning consumer control thread");
1684 * Check version of the lttng-modules.
1686 static int validate_lttng_modules_version(void)
1688 return kernel_validate_version(kernel_tracer_fd
);
1692 * Setup necessary data for kernel tracer action.
1694 static int init_kernel_tracer(void)
1698 /* Modprobe lttng kernel modules */
1699 ret
= modprobe_lttng_control();
1704 /* Open debugfs lttng */
1705 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1706 if (kernel_tracer_fd
< 0) {
1707 DBG("Failed to open %s", module_proc_lttng
);
1712 /* Validate kernel version */
1713 ret
= validate_lttng_modules_version();
1718 ret
= modprobe_lttng_data();
1723 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1727 modprobe_remove_lttng_control();
1728 ret
= close(kernel_tracer_fd
);
1732 kernel_tracer_fd
= -1;
1733 return LTTCOMM_KERN_VERSION
;
1736 ret
= close(kernel_tracer_fd
);
1742 modprobe_remove_lttng_control();
1745 WARN("No kernel tracer available");
1746 kernel_tracer_fd
= -1;
1748 return LTTCOMM_NEED_ROOT_SESSIOND
;
1750 return LTTCOMM_KERN_NA
;
1755 * Init tracing by creating trace directory and sending fds kernel consumer.
1757 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
1761 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
1763 * Assign default kernel consumer socket if no consumer assigned to the
1764 * kernel session. At this point, it's NOT supposed to be -1 but this is
1765 * an extra security check.
1767 if (session
->consumer_fd
< 0) {
1768 session
->consumer_fd
= kconsumer_data
.cmd_sock
;
1771 ret
= kernel_consumer_send_session(session
->consumer_fd
, session
);
1773 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1783 * Create a socket to the relayd using the URI.
1785 * On success, the relayd_sock pointer is set to the created socket.
1786 * Else, it is untouched and an lttcomm error code is returned.
1788 static int create_connect_relayd(struct consumer_output
*output
,
1789 const char *session_name
, struct lttng_uri
*uri
,
1790 struct lttcomm_sock
**relayd_sock
)
1793 struct lttcomm_sock
*sock
;
1795 /* Create socket object from URI */
1796 sock
= lttcomm_alloc_sock_from_uri(uri
);
1798 ret
= LTTCOMM_FATAL
;
1802 ret
= lttcomm_create_sock(sock
);
1804 ret
= LTTCOMM_FATAL
;
1808 /* Connect to relayd so we can proceed with a session creation. */
1809 ret
= relayd_connect(sock
);
1811 ERR("Unable to reach lttng-relayd");
1812 ret
= LTTCOMM_RELAYD_SESSION_FAIL
;
1816 /* Create socket for control stream. */
1817 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
1818 DBG3("Creating relayd stream socket from URI");
1820 /* Check relayd version */
1821 ret
= relayd_version_check(sock
, LTTNG_UST_COMM_MAJOR
, 0);
1823 ret
= LTTCOMM_RELAYD_VERSION_FAIL
;
1826 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
1827 DBG3("Creating relayd data socket from URI");
1829 /* Command is not valid */
1830 ERR("Relayd invalid stream type: %d", uri
->stype
);
1831 ret
= LTTCOMM_INVALID
;
1835 *relayd_sock
= sock
;
1841 (void) relayd_close(sock
);
1845 lttcomm_destroy_sock(sock
);
1852 * Connect to the relayd using URI and send the socket to the right consumer.
1854 static int send_socket_relayd_consumer(int domain
, struct ltt_session
*session
,
1855 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
1859 struct lttcomm_sock
*sock
= NULL
;
1861 /* Set the network sequence index if not set. */
1862 if (consumer
->net_seq_index
== -1) {
1864 * Increment net_seq_idx because we are about to transfer the
1865 * new relayd socket to the consumer.
1867 uatomic_inc(&relayd_net_seq_idx
);
1868 /* Assign unique key so the consumer can match streams */
1869 consumer
->net_seq_index
= uatomic_read(&relayd_net_seq_idx
);
1872 /* Connect to relayd and make version check if uri is the control. */
1873 ret
= create_connect_relayd(consumer
, session
->name
, relayd_uri
, &sock
);
1874 if (ret
!= LTTCOMM_OK
) {
1878 /* If the control socket is connected, network session is ready */
1879 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1880 session
->net_handle
= 1;
1884 case LTTNG_DOMAIN_KERNEL
:
1885 /* Send relayd socket to consumer. */
1886 ret
= kernel_consumer_send_relayd_socket(consumer_fd
, sock
,
1887 consumer
, relayd_uri
->stype
);
1889 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
1893 case LTTNG_DOMAIN_UST
:
1894 /* Send relayd socket to consumer. */
1895 ret
= ust_consumer_send_relayd_socket(consumer_fd
, sock
,
1896 consumer
, relayd_uri
->stype
);
1898 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
1907 * Close socket which was dup on the consumer side. The session daemon does
1908 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1913 (void) relayd_close(sock
);
1914 lttcomm_destroy_sock(sock
);
1921 * Send both relayd sockets to a specific consumer and domain. This is a
1922 * helper function to facilitate sending the information to the consumer for a
1925 static int send_sockets_relayd_consumer(int domain
,
1926 struct ltt_session
*session
, struct consumer_output
*consumer
, int fd
)
1930 /* Sending control relayd socket. */
1931 ret
= send_socket_relayd_consumer(domain
, session
,
1932 &consumer
->dst
.net
.control
, consumer
, fd
);
1933 if (ret
!= LTTCOMM_OK
) {
1937 /* Sending data relayd socket. */
1938 ret
= send_socket_relayd_consumer(domain
, session
,
1939 &consumer
->dst
.net
.data
, consumer
, fd
);
1940 if (ret
!= LTTCOMM_OK
) {
1949 * Setup relayd connections for a tracing session. First creates the socket to
1950 * the relayd and send them to the right domain consumer. Consumer type MUST be
1953 static int setup_relayd(struct ltt_session
*session
)
1955 int ret
= LTTCOMM_OK
;
1956 struct ltt_ust_session
*usess
;
1957 struct ltt_kernel_session
*ksess
;
1961 usess
= session
->ust_session
;
1962 ksess
= session
->kernel_session
;
1964 DBG2("Setting relayd for session %s", session
->name
);
1966 if (usess
&& usess
->consumer
->sock
== -1 &&
1967 usess
->consumer
->type
== CONSUMER_DST_NET
&&
1968 usess
->consumer
->enabled
) {
1969 /* Setup relayd for 64 bits consumer */
1970 if (ust_consumerd64_fd
>= 0) {
1971 send_sockets_relayd_consumer(LTTNG_DOMAIN_UST
, session
,
1972 usess
->consumer
, ust_consumerd64_fd
);
1973 if (ret
!= LTTCOMM_OK
) {
1978 /* Setup relayd for 32 bits consumer */
1979 if (ust_consumerd32_fd
>= 0) {
1980 send_sockets_relayd_consumer(LTTNG_DOMAIN_UST
, session
,
1981 usess
->consumer
, ust_consumerd32_fd
);
1982 if (ret
!= LTTCOMM_OK
) {
1986 } else if (ksess
&& ksess
->consumer
->sock
== -1 &&
1987 ksess
->consumer
->type
== CONSUMER_DST_NET
&&
1988 ksess
->consumer
->enabled
) {
1989 send_sockets_relayd_consumer(LTTNG_DOMAIN_KERNEL
, session
,
1990 ksess
->consumer
, ksess
->consumer_fd
);
1991 if (ret
!= LTTCOMM_OK
) {
2001 * Copy consumer output from the tracing session to the domain session. The
2002 * function also applies the right modification on a per domain basis for the
2003 * trace files destination directory.
2005 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2008 const char *dir_name
;
2009 struct consumer_output
*consumer
;
2012 case LTTNG_DOMAIN_KERNEL
:
2013 DBG3("Copying tracing session consumer output in kernel session");
2014 session
->kernel_session
->consumer
=
2015 consumer_copy_output(session
->consumer
);
2016 /* Ease our life a bit for the next part */
2017 consumer
= session
->kernel_session
->consumer
;
2018 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2020 case LTTNG_DOMAIN_UST
:
2021 DBG3("Copying tracing session consumer output in UST session");
2022 session
->ust_session
->consumer
=
2023 consumer_copy_output(session
->consumer
);
2024 /* Ease our life a bit for the next part */
2025 consumer
= session
->ust_session
->consumer
;
2026 dir_name
= DEFAULT_UST_TRACE_DIR
;
2029 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2033 /* Append correct directory to subdir */
2034 strncat(consumer
->subdir
, dir_name
, sizeof(consumer
->subdir
));
2035 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2037 /* Add default trace directory name */
2038 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
2039 strncat(consumer
->dst
.trace_path
, dir_name
,
2040 sizeof(consumer
->dst
.trace_path
));
2050 * Create an UST session and add it to the session ust list.
2052 static int create_ust_session(struct ltt_session
*session
,
2053 struct lttng_domain
*domain
)
2056 struct ltt_ust_session
*lus
= NULL
;
2059 assert(session
->consumer
);
2061 switch (domain
->type
) {
2062 case LTTNG_DOMAIN_UST
:
2065 ERR("Unknown UST domain on create session %d", domain
->type
);
2066 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2070 DBG("Creating UST session");
2072 lus
= trace_ust_create_session(session
->path
, session
->id
, domain
);
2074 ret
= LTTCOMM_UST_SESS_FAIL
;
2078 if (session
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2079 ret
= run_as_mkdir_recursive(lus
->pathname
, S_IRWXU
| S_IRWXG
,
2080 session
->uid
, session
->gid
);
2082 if (ret
!= -EEXIST
) {
2083 ERR("Trace directory creation error");
2084 ret
= LTTCOMM_UST_SESS_FAIL
;
2090 lus
->uid
= session
->uid
;
2091 lus
->gid
= session
->gid
;
2092 session
->ust_session
= lus
;
2094 /* Copy session output to the newly created UST session */
2095 ret
= copy_session_consumer(domain
->type
, session
);
2096 if (ret
!= LTTCOMM_OK
) {
2104 session
->ust_session
= NULL
;
2109 * Create a kernel tracer session then create the default channel.
2111 static int create_kernel_session(struct ltt_session
*session
)
2115 DBG("Creating kernel session");
2117 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2119 ret
= LTTCOMM_KERN_SESS_FAIL
;
2123 /* Set kernel consumer socket fd */
2124 if (kconsumer_data
.cmd_sock
>= 0) {
2125 session
->kernel_session
->consumer_fd
= kconsumer_data
.cmd_sock
;
2128 /* Copy session output to the newly created Kernel session */
2129 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2130 if (ret
!= LTTCOMM_OK
) {
2134 /* Create directory(ies) on local filesystem. */
2135 if (session
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2136 ret
= run_as_mkdir_recursive(
2137 session
->kernel_session
->consumer
->dst
.trace_path
,
2138 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2140 if (ret
!= -EEXIST
) {
2141 ERR("Trace directory creation error");
2147 session
->kernel_session
->uid
= session
->uid
;
2148 session
->kernel_session
->gid
= session
->gid
;
2153 trace_kernel_destroy_session(session
->kernel_session
);
2154 session
->kernel_session
= NULL
;
2159 * Check if the UID or GID match the session. Root user has access to all
2162 static int session_access_ok(struct ltt_session
*session
, uid_t uid
, gid_t gid
)
2164 if (uid
!= session
->uid
&& gid
!= session
->gid
&& uid
!= 0) {
2172 * Count number of session permitted by uid/gid.
2174 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2177 struct ltt_session
*session
;
2179 DBG("Counting number of available session for UID %d GID %d",
2181 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2183 * Only list the sessions the user can control.
2185 if (!session_access_ok(session
, uid
, gid
)) {
2194 * Using the session list, filled a lttng_session array to send back to the
2195 * client for session listing.
2197 * The session list lock MUST be acquired before calling this function. Use
2198 * session_lock_list() and session_unlock_list().
2200 static void list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2204 struct ltt_session
*session
;
2206 DBG("Getting all available session for UID %d GID %d",
2209 * Iterate over session list and append data after the control struct in
2212 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2214 * Only list the sessions the user can control.
2216 if (!session_access_ok(session
, uid
, gid
)) {
2219 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
2220 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
2221 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2222 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2223 sessions
[i
].enabled
= session
->enabled
;
2229 * Fill lttng_channel array of all channels.
2231 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
2232 struct lttng_channel
*channels
)
2235 struct ltt_kernel_channel
*kchan
;
2237 DBG("Listing channels for session %s", session
->name
);
2240 case LTTNG_DOMAIN_KERNEL
:
2241 /* Kernel channels */
2242 if (session
->kernel_session
!= NULL
) {
2243 cds_list_for_each_entry(kchan
,
2244 &session
->kernel_session
->channel_list
.head
, list
) {
2245 /* Copy lttng_channel struct to array */
2246 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
2247 channels
[i
].enabled
= kchan
->enabled
;
2252 case LTTNG_DOMAIN_UST
:
2254 struct lttng_ht_iter iter
;
2255 struct ltt_ust_channel
*uchan
;
2257 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
2258 &iter
.iter
, uchan
, node
.node
) {
2259 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
2260 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
2261 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2262 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2263 channels
[i
].attr
.switch_timer_interval
=
2264 uchan
->attr
.switch_timer_interval
;
2265 channels
[i
].attr
.read_timer_interval
=
2266 uchan
->attr
.read_timer_interval
;
2267 channels
[i
].enabled
= uchan
->enabled
;
2268 switch (uchan
->attr
.output
) {
2269 case LTTNG_UST_MMAP
:
2271 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
2284 * Create a list of ust global domain events.
2286 static int list_lttng_ust_global_events(char *channel_name
,
2287 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
2290 unsigned int nb_event
= 0;
2291 struct lttng_ht_iter iter
;
2292 struct lttng_ht_node_str
*node
;
2293 struct ltt_ust_channel
*uchan
;
2294 struct ltt_ust_event
*uevent
;
2295 struct lttng_event
*tmp
;
2297 DBG("Listing UST global events for channel %s", channel_name
);
2301 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
2302 node
= lttng_ht_iter_get_node_str(&iter
);
2304 ret
= -LTTCOMM_UST_CHAN_NOT_FOUND
;
2308 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
2310 nb_event
+= lttng_ht_get_count(uchan
->events
);
2312 if (nb_event
== 0) {
2317 DBG3("Listing UST global %d events", nb_event
);
2319 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
2321 ret
= -LTTCOMM_FATAL
;
2325 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
2326 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
2327 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
2328 tmp
[i
].enabled
= uevent
->enabled
;
2329 switch (uevent
->attr
.instrumentation
) {
2330 case LTTNG_UST_TRACEPOINT
:
2331 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2333 case LTTNG_UST_PROBE
:
2334 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
2336 case LTTNG_UST_FUNCTION
:
2337 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
2340 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
2341 switch (uevent
->attr
.loglevel_type
) {
2342 case LTTNG_UST_LOGLEVEL_ALL
:
2343 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2345 case LTTNG_UST_LOGLEVEL_RANGE
:
2346 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
2348 case LTTNG_UST_LOGLEVEL_SINGLE
:
2349 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
2364 * Fill lttng_event array of all kernel events in the channel.
2366 static int list_lttng_kernel_events(char *channel_name
,
2367 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
2370 unsigned int nb_event
;
2371 struct ltt_kernel_event
*event
;
2372 struct ltt_kernel_channel
*kchan
;
2374 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
2375 if (kchan
== NULL
) {
2376 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2380 nb_event
= kchan
->event_count
;
2382 DBG("Listing events for channel %s", kchan
->channel
->name
);
2384 if (nb_event
== 0) {
2389 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
2390 if (*events
== NULL
) {
2391 ret
= LTTCOMM_FATAL
;
2395 /* Kernel channels */
2396 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
2397 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
2398 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
2399 (*events
)[i
].enabled
= event
->enabled
;
2400 switch (event
->event
->instrumentation
) {
2401 case LTTNG_KERNEL_TRACEPOINT
:
2402 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2404 case LTTNG_KERNEL_KPROBE
:
2405 case LTTNG_KERNEL_KRETPROBE
:
2406 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
2407 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
2408 sizeof(struct lttng_kernel_kprobe
));
2410 case LTTNG_KERNEL_FUNCTION
:
2411 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
2412 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
2413 sizeof(struct lttng_kernel_function
));
2415 case LTTNG_KERNEL_NOOP
:
2416 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
2418 case LTTNG_KERNEL_SYSCALL
:
2419 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
2421 case LTTNG_KERNEL_ALL
:
2435 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
2437 static int cmd_disable_channel(struct ltt_session
*session
,
2438 int domain
, char *channel_name
)
2441 struct ltt_ust_session
*usess
;
2443 usess
= session
->ust_session
;
2446 case LTTNG_DOMAIN_KERNEL
:
2448 ret
= channel_kernel_disable(session
->kernel_session
,
2450 if (ret
!= LTTCOMM_OK
) {
2454 kernel_wait_quiescent(kernel_tracer_fd
);
2457 case LTTNG_DOMAIN_UST
:
2459 struct ltt_ust_channel
*uchan
;
2460 struct lttng_ht
*chan_ht
;
2462 chan_ht
= usess
->domain_global
.channels
;
2464 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
2465 if (uchan
== NULL
) {
2466 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2470 ret
= channel_ust_disable(usess
, domain
, uchan
);
2471 if (ret
!= LTTCOMM_OK
) {
2477 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2478 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2479 case LTTNG_DOMAIN_UST_PID
:
2482 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2493 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
2495 static int cmd_enable_channel(struct ltt_session
*session
,
2496 int domain
, struct lttng_channel
*attr
)
2499 struct ltt_ust_session
*usess
= session
->ust_session
;
2500 struct lttng_ht
*chan_ht
;
2502 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
2505 case LTTNG_DOMAIN_KERNEL
:
2507 struct ltt_kernel_channel
*kchan
;
2509 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
2510 session
->kernel_session
);
2511 if (kchan
== NULL
) {
2512 ret
= channel_kernel_create(session
->kernel_session
,
2513 attr
, kernel_poll_pipe
[1]);
2515 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
2518 if (ret
!= LTTCOMM_OK
) {
2522 kernel_wait_quiescent(kernel_tracer_fd
);
2525 case LTTNG_DOMAIN_UST
:
2527 struct ltt_ust_channel
*uchan
;
2529 chan_ht
= usess
->domain_global
.channels
;
2531 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
2532 if (uchan
== NULL
) {
2533 ret
= channel_ust_create(usess
, domain
, attr
);
2535 ret
= channel_ust_enable(usess
, domain
, uchan
);
2540 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2541 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2542 case LTTNG_DOMAIN_UST_PID
:
2545 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2554 * Command LTTNG_DISABLE_EVENT processed by the client thread.
2556 static int cmd_disable_event(struct ltt_session
*session
, int domain
,
2557 char *channel_name
, char *event_name
)
2562 case LTTNG_DOMAIN_KERNEL
:
2564 struct ltt_kernel_channel
*kchan
;
2565 struct ltt_kernel_session
*ksess
;
2567 ksess
= session
->kernel_session
;
2569 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
2570 if (kchan
== NULL
) {
2571 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2575 ret
= event_kernel_disable_tracepoint(ksess
, kchan
, event_name
);
2576 if (ret
!= LTTCOMM_OK
) {
2580 kernel_wait_quiescent(kernel_tracer_fd
);
2583 case LTTNG_DOMAIN_UST
:
2585 struct ltt_ust_channel
*uchan
;
2586 struct ltt_ust_session
*usess
;
2588 usess
= session
->ust_session
;
2590 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2592 if (uchan
== NULL
) {
2593 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2597 ret
= event_ust_disable_tracepoint(usess
, domain
, uchan
, event_name
);
2598 if (ret
!= LTTCOMM_OK
) {
2602 DBG3("Disable UST event %s in channel %s completed", event_name
,
2607 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2608 case LTTNG_DOMAIN_UST_PID
:
2609 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2623 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
2625 static int cmd_disable_event_all(struct ltt_session
*session
, int domain
,
2631 case LTTNG_DOMAIN_KERNEL
:
2633 struct ltt_kernel_session
*ksess
;
2634 struct ltt_kernel_channel
*kchan
;
2636 ksess
= session
->kernel_session
;
2638 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
2639 if (kchan
== NULL
) {
2640 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2644 ret
= event_kernel_disable_all(ksess
, kchan
);
2645 if (ret
!= LTTCOMM_OK
) {
2649 kernel_wait_quiescent(kernel_tracer_fd
);
2652 case LTTNG_DOMAIN_UST
:
2654 struct ltt_ust_session
*usess
;
2655 struct ltt_ust_channel
*uchan
;
2657 usess
= session
->ust_session
;
2659 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2661 if (uchan
== NULL
) {
2662 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2666 ret
= event_ust_disable_all_tracepoints(usess
, domain
, uchan
);
2671 DBG3("Disable all UST events in channel %s completed", channel_name
);
2676 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2677 case LTTNG_DOMAIN_UST_PID
:
2678 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2692 * Command LTTNG_ADD_CONTEXT processed by the client thread.
2694 static int cmd_add_context(struct ltt_session
*session
, int domain
,
2695 char *channel_name
, char *event_name
, struct lttng_event_context
*ctx
)
2700 case LTTNG_DOMAIN_KERNEL
:
2701 /* Add kernel context to kernel tracer */
2702 ret
= context_kernel_add(session
->kernel_session
, ctx
,
2703 event_name
, channel_name
);
2704 if (ret
!= LTTCOMM_OK
) {
2708 case LTTNG_DOMAIN_UST
:
2710 struct ltt_ust_session
*usess
= session
->ust_session
;
2712 ret
= context_ust_add(usess
, domain
, ctx
, event_name
, channel_name
);
2713 if (ret
!= LTTCOMM_OK
) {
2719 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2720 case LTTNG_DOMAIN_UST_PID
:
2721 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2735 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2737 static int cmd_enable_event(struct ltt_session
*session
, int domain
,
2738 char *channel_name
, struct lttng_event
*event
)
2741 struct lttng_channel
*attr
;
2742 struct ltt_ust_session
*usess
= session
->ust_session
;
2745 case LTTNG_DOMAIN_KERNEL
:
2747 struct ltt_kernel_channel
*kchan
;
2749 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2750 session
->kernel_session
);
2751 if (kchan
== NULL
) {
2752 attr
= channel_new_default_attr(domain
);
2754 ret
= LTTCOMM_FATAL
;
2757 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
2759 /* This call will notify the kernel thread */
2760 ret
= channel_kernel_create(session
->kernel_session
,
2761 attr
, kernel_poll_pipe
[1]);
2762 if (ret
!= LTTCOMM_OK
) {
2769 /* Get the newly created kernel channel pointer */
2770 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2771 session
->kernel_session
);
2772 if (kchan
== NULL
) {
2773 /* This sould not happen... */
2774 ret
= LTTCOMM_FATAL
;
2778 ret
= event_kernel_enable_tracepoint(session
->kernel_session
, kchan
,
2780 if (ret
!= LTTCOMM_OK
) {
2784 kernel_wait_quiescent(kernel_tracer_fd
);
2787 case LTTNG_DOMAIN_UST
:
2789 struct lttng_channel
*attr
;
2790 struct ltt_ust_channel
*uchan
;
2792 /* Get channel from global UST domain */
2793 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2795 if (uchan
== NULL
) {
2796 /* Create default channel */
2797 attr
= channel_new_default_attr(domain
);
2799 ret
= LTTCOMM_FATAL
;
2802 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
2803 attr
->name
[NAME_MAX
- 1] = '\0';
2805 ret
= channel_ust_create(usess
, domain
, attr
);
2806 if (ret
!= LTTCOMM_OK
) {
2812 /* Get the newly created channel reference back */
2813 uchan
= trace_ust_find_channel_by_name(
2814 usess
->domain_global
.channels
, channel_name
);
2815 if (uchan
== NULL
) {
2816 /* Something is really wrong */
2817 ret
= LTTCOMM_FATAL
;
2822 /* At this point, the session and channel exist on the tracer */
2823 ret
= event_ust_enable_tracepoint(usess
, domain
, uchan
, event
);
2824 if (ret
!= LTTCOMM_OK
) {
2830 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2831 case LTTNG_DOMAIN_UST_PID
:
2832 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2846 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
2848 static int cmd_enable_event_all(struct ltt_session
*session
, int domain
,
2849 char *channel_name
, int event_type
)
2852 struct ltt_kernel_channel
*kchan
;
2855 case LTTNG_DOMAIN_KERNEL
:
2856 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2857 session
->kernel_session
);
2858 if (kchan
== NULL
) {
2859 /* This call will notify the kernel thread */
2860 ret
= channel_kernel_create(session
->kernel_session
, NULL
,
2861 kernel_poll_pipe
[1]);
2862 if (ret
!= LTTCOMM_OK
) {
2866 /* Get the newly created kernel channel pointer */
2867 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2868 session
->kernel_session
);
2869 if (kchan
== NULL
) {
2870 /* This sould not happen... */
2871 ret
= LTTCOMM_FATAL
;
2877 switch (event_type
) {
2878 case LTTNG_EVENT_SYSCALL
:
2879 ret
= event_kernel_enable_all_syscalls(session
->kernel_session
,
2880 kchan
, kernel_tracer_fd
);
2882 case LTTNG_EVENT_TRACEPOINT
:
2884 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
2885 * events already registered to the channel.
2887 ret
= event_kernel_enable_all_tracepoints(session
->kernel_session
,
2888 kchan
, kernel_tracer_fd
);
2890 case LTTNG_EVENT_ALL
:
2891 /* Enable syscalls and tracepoints */
2892 ret
= event_kernel_enable_all(session
->kernel_session
,
2893 kchan
, kernel_tracer_fd
);
2896 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2900 /* Manage return value */
2901 if (ret
!= LTTCOMM_OK
) {
2905 kernel_wait_quiescent(kernel_tracer_fd
);
2907 case LTTNG_DOMAIN_UST
:
2909 struct lttng_channel
*attr
;
2910 struct ltt_ust_channel
*uchan
;
2911 struct ltt_ust_session
*usess
= session
->ust_session
;
2913 /* Get channel from global UST domain */
2914 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2916 if (uchan
== NULL
) {
2917 /* Create default channel */
2918 attr
= channel_new_default_attr(domain
);
2920 ret
= LTTCOMM_FATAL
;
2923 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
2924 attr
->name
[NAME_MAX
- 1] = '\0';
2926 /* Use the internal command enable channel */
2927 ret
= channel_ust_create(usess
, domain
, attr
);
2928 if (ret
!= LTTCOMM_OK
) {
2934 /* Get the newly created channel reference back */
2935 uchan
= trace_ust_find_channel_by_name(
2936 usess
->domain_global
.channels
, channel_name
);
2937 if (uchan
== NULL
) {
2938 /* Something is really wrong */
2939 ret
= LTTCOMM_FATAL
;
2944 /* At this point, the session and channel exist on the tracer */
2946 switch (event_type
) {
2947 case LTTNG_EVENT_ALL
:
2948 case LTTNG_EVENT_TRACEPOINT
:
2949 ret
= event_ust_enable_all_tracepoints(usess
, domain
, uchan
);
2950 if (ret
!= LTTCOMM_OK
) {
2955 ret
= LTTCOMM_UST_ENABLE_FAIL
;
2959 /* Manage return value */
2960 if (ret
!= LTTCOMM_OK
) {
2967 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2968 case LTTNG_DOMAIN_UST_PID
:
2969 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2983 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2985 static ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
2988 ssize_t nb_events
= 0;
2991 case LTTNG_DOMAIN_KERNEL
:
2992 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2993 if (nb_events
< 0) {
2994 ret
= LTTCOMM_KERN_LIST_FAIL
;
2998 case LTTNG_DOMAIN_UST
:
2999 nb_events
= ust_app_list_events(events
);
3000 if (nb_events
< 0) {
3001 ret
= LTTCOMM_UST_LIST_FAIL
;
3013 /* Return negative value to differentiate return code */
3018 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
3020 static ssize_t
cmd_list_tracepoint_fields(int domain
,
3021 struct lttng_event_field
**fields
)
3024 ssize_t nb_fields
= 0;
3027 case LTTNG_DOMAIN_UST
:
3028 nb_fields
= ust_app_list_event_fields(fields
);
3029 if (nb_fields
< 0) {
3030 ret
= LTTCOMM_UST_LIST_FAIL
;
3034 case LTTNG_DOMAIN_KERNEL
:
3035 default: /* fall-through */
3043 /* Return negative value to differentiate return code */
3048 * Command LTTNG_START_TRACE processed by the client thread.
3050 static int cmd_start_trace(struct ltt_session
*session
)
3053 struct ltt_kernel_session
*ksession
;
3054 struct ltt_ust_session
*usess
;
3055 struct ltt_kernel_channel
*kchan
;
3057 /* Ease our life a bit ;) */
3058 ksession
= session
->kernel_session
;
3059 usess
= session
->ust_session
;
3061 if (session
->enabled
) {
3062 /* Already started. */
3063 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
3067 session
->enabled
= 1;
3069 ret
= setup_relayd(session
);
3070 if (ret
!= LTTCOMM_OK
) {
3071 ERR("Error setting up relayd for session %s", session
->name
);
3075 /* Kernel tracing */
3076 if (ksession
!= NULL
) {
3077 /* Open kernel metadata */
3078 if (ksession
->metadata
== NULL
) {
3079 ret
= kernel_open_metadata(ksession
,
3080 ksession
->consumer
->dst
.trace_path
);
3082 ret
= LTTCOMM_KERN_META_FAIL
;
3087 /* Open kernel metadata stream */
3088 if (ksession
->metadata_stream_fd
< 0) {
3089 ret
= kernel_open_metadata_stream(ksession
);
3091 ERR("Kernel create metadata stream failed");
3092 ret
= LTTCOMM_KERN_STREAM_FAIL
;
3097 /* For each channel */
3098 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
3099 if (kchan
->stream_count
== 0) {
3100 ret
= kernel_open_channel_stream(kchan
);
3102 ret
= LTTCOMM_KERN_STREAM_FAIL
;
3105 /* Update the stream global counter */
3106 ksession
->stream_count_global
+= ret
;
3110 /* Setup kernel consumer socket and send fds to it */
3111 ret
= init_kernel_tracing(ksession
);
3113 ret
= LTTCOMM_KERN_START_FAIL
;
3117 /* This start the kernel tracing */
3118 ret
= kernel_start_session(ksession
);
3120 ret
= LTTCOMM_KERN_START_FAIL
;
3124 /* Quiescent wait after starting trace */
3125 kernel_wait_quiescent(kernel_tracer_fd
);
3128 /* Flag session that trace should start automatically */
3130 usess
->start_trace
= 1;
3132 ret
= ust_app_start_trace_all(usess
);
3134 ret
= LTTCOMM_UST_START_FAIL
;
3146 * Command LTTNG_STOP_TRACE processed by the client thread.
3148 static int cmd_stop_trace(struct ltt_session
*session
)
3151 struct ltt_kernel_channel
*kchan
;
3152 struct ltt_kernel_session
*ksession
;
3153 struct ltt_ust_session
*usess
;
3156 ksession
= session
->kernel_session
;
3157 usess
= session
->ust_session
;
3159 if (!session
->enabled
) {
3160 ret
= LTTCOMM_TRACE_ALREADY_STOPPED
;
3164 session
->enabled
= 0;
3167 if (ksession
!= NULL
) {
3168 DBG("Stop kernel tracing");
3170 /* Flush metadata if exist */
3171 if (ksession
->metadata_stream_fd
>= 0) {
3172 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
3174 ERR("Kernel metadata flush failed");
3178 /* Flush all buffers before stopping */
3179 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
3180 ret
= kernel_flush_buffer(kchan
);
3182 ERR("Kernel flush buffer error");
3186 ret
= kernel_stop_session(ksession
);
3188 ret
= LTTCOMM_KERN_STOP_FAIL
;
3192 kernel_wait_quiescent(kernel_tracer_fd
);
3196 usess
->start_trace
= 0;
3198 ret
= ust_app_stop_trace_all(usess
);
3200 ret
= LTTCOMM_UST_STOP_FAIL
;
3212 * Command LTTNG_CREATE_SESSION_URI processed by the client thread.
3214 static int cmd_create_session_uri(char *name
, struct lttng_uri
*ctrl_uri
,
3215 struct lttng_uri
*data_uri
, unsigned int enable_consumer
,
3216 lttng_sock_cred
*creds
)
3220 struct ltt_session
*session
;
3221 struct consumer_output
*consumer
;
3223 /* Verify if the session already exist */
3224 session
= session_find_by_name(name
);
3225 if (session
!= NULL
) {
3226 ret
= LTTCOMM_EXIST_SESS
;
3230 /* TODO: validate URIs */
3232 /* Create default consumer output */
3233 consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
3234 if (consumer
== NULL
) {
3235 ret
= LTTCOMM_FATAL
;
3238 strncpy(consumer
->subdir
, ctrl_uri
->subdir
, sizeof(consumer
->subdir
));
3239 DBG2("Consumer subdir set to %s", consumer
->subdir
);
3241 switch (ctrl_uri
->dtype
) {
3242 case LTTNG_DST_IPV4
:
3243 case LTTNG_DST_IPV6
:
3244 /* Set control URI into consumer output object */
3245 ret
= consumer_set_network_uri(consumer
, ctrl_uri
);
3247 ret
= LTTCOMM_FATAL
;
3251 /* Set data URI into consumer output object */
3252 ret
= consumer_set_network_uri(consumer
, data_uri
);
3254 ret
= LTTCOMM_FATAL
;
3258 /* Empty path since the session is network */
3261 case LTTNG_DST_PATH
:
3262 /* Very volatile pointer. Only used for the create session. */
3263 path
= ctrl_uri
->dst
.path
;
3264 strncpy(consumer
->dst
.trace_path
, path
,
3265 sizeof(consumer
->dst
.trace_path
));
3269 /* Set if the consumer is enabled or not */
3270 consumer
->enabled
= enable_consumer
;
3272 ret
= session_create(name
, path
, LTTNG_SOCK_GET_UID_CRED(creds
),
3273 LTTNG_SOCK_GET_GID_CRED(creds
));
3274 if (ret
!= LTTCOMM_OK
) {
3275 goto consumer_error
;
3278 /* Get the newly created session pointer back */
3279 session
= session_find_by_name(name
);
3282 /* Assign consumer to session */
3283 session
->consumer
= consumer
;
3288 consumer_destroy_output(consumer
);
3294 * Command LTTNG_CREATE_SESSION processed by the client thread.
3296 static int cmd_create_session(char *name
, char *path
, lttng_sock_cred
*creds
)
3299 struct lttng_uri uri
;
3301 /* Zeroed temporary URI */
3302 memset(&uri
, 0, sizeof(uri
));
3304 uri
.dtype
= LTTNG_DST_PATH
;
3305 uri
.utype
= LTTNG_URI_DST
;
3306 strncpy(uri
.dst
.path
, path
, sizeof(uri
.dst
.path
));
3308 /* TODO: Strip date-time from path and put it in uri's subdir */
3310 ret
= cmd_create_session_uri(name
, &uri
, NULL
, 1, creds
);
3311 if (ret
!= LTTCOMM_OK
) {
3320 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3322 static int cmd_destroy_session(struct ltt_session
*session
, char *name
)
3326 /* Clean kernel session teardown */
3327 teardown_kernel_session(session
);
3328 /* UST session teardown */
3329 teardown_ust_session(session
);
3332 * Must notify the kernel thread here to update it's poll setin order
3333 * to remove the channel(s)' fd just destroyed.
3335 ret
= notify_thread_pipe(kernel_poll_pipe
[1]);
3337 PERROR("write kernel poll pipe");
3340 ret
= session_destroy(session
);
3346 * Command LTTNG_CALIBRATE processed by the client thread.
3348 static int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
3353 case LTTNG_DOMAIN_KERNEL
:
3355 struct lttng_kernel_calibrate kcalibrate
;
3357 kcalibrate
.type
= calibrate
->type
;
3358 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
3360 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
3365 case LTTNG_DOMAIN_UST
:
3367 struct lttng_ust_calibrate ucalibrate
;
3369 ucalibrate
.type
= calibrate
->type
;
3370 ret
= ust_app_calibrate_glb(&ucalibrate
);
3372 ret
= LTTCOMM_UST_CALIBRATE_FAIL
;
3389 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3391 static int cmd_register_consumer(struct ltt_session
*session
, int domain
,
3397 case LTTNG_DOMAIN_KERNEL
:
3398 /* Can't register a consumer if there is already one */
3399 if (session
->kernel_session
->consumer_fds_sent
!= 0) {
3400 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
3404 sock
= lttcomm_connect_unix_sock(sock_path
);
3406 ret
= LTTCOMM_CONNECT_FAIL
;
3410 session
->kernel_session
->consumer_fd
= sock
;
3413 /* TODO: Userspace tracing */
3425 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3427 static ssize_t
cmd_list_domains(struct ltt_session
*session
,
3428 struct lttng_domain
**domains
)
3433 if (session
->kernel_session
!= NULL
) {
3434 DBG3("Listing domains found kernel domain");
3438 if (session
->ust_session
!= NULL
) {
3439 DBG3("Listing domains found UST global domain");
3443 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3444 if (*domains
== NULL
) {
3445 ret
= -LTTCOMM_FATAL
;
3449 if (session
->kernel_session
!= NULL
) {
3450 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3454 if (session
->ust_session
!= NULL
) {
3455 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3466 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3468 static ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
3469 struct lttng_channel
**channels
)
3472 ssize_t nb_chan
= 0;
3475 case LTTNG_DOMAIN_KERNEL
:
3476 if (session
->kernel_session
!= NULL
) {
3477 nb_chan
= session
->kernel_session
->channel_count
;
3479 DBG3("Number of kernel channels %zd", nb_chan
);
3481 case LTTNG_DOMAIN_UST
:
3482 if (session
->ust_session
!= NULL
) {
3483 nb_chan
= lttng_ht_get_count(
3484 session
->ust_session
->domain_global
.channels
);
3486 DBG3("Number of UST global channels %zd", nb_chan
);
3495 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
3496 if (*channels
== NULL
) {
3497 ret
= -LTTCOMM_FATAL
;
3501 list_lttng_channels(domain
, session
, *channels
);
3513 * Command LTTNG_LIST_EVENTS processed by the client thread.
3515 static ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
3516 char *channel_name
, struct lttng_event
**events
)
3519 ssize_t nb_event
= 0;
3522 case LTTNG_DOMAIN_KERNEL
:
3523 if (session
->kernel_session
!= NULL
) {
3524 nb_event
= list_lttng_kernel_events(channel_name
,
3525 session
->kernel_session
, events
);
3528 case LTTNG_DOMAIN_UST
:
3530 if (session
->ust_session
!= NULL
) {
3531 nb_event
= list_lttng_ust_global_events(channel_name
,
3532 &session
->ust_session
->domain_global
, events
);
3548 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
3550 static int cmd_set_consumer_uri(int domain
, struct ltt_session
*session
,
3551 struct lttng_uri
*uri
)
3554 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3555 struct ltt_ust_session
*usess
= session
->ust_session
;
3556 struct consumer_output
*consumer
;
3558 /* Can't enable consumer after session started. */
3559 if (session
->enabled
) {
3560 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
3565 case LTTNG_DOMAIN_KERNEL
:
3566 /* Code flow error if we don't have a kernel session here. */
3569 /* Create consumer output if none exists */
3570 consumer
= ksess
->tmp_consumer
;
3571 if (consumer
== NULL
) {
3572 consumer
= consumer_copy_output(ksess
->consumer
);
3573 if (consumer
== NULL
) {
3574 ret
= LTTCOMM_FATAL
;
3577 /* Reassign new pointer */
3578 ksess
->tmp_consumer
= consumer
;
3581 switch (uri
->dtype
) {
3582 case LTTNG_DST_IPV4
:
3583 case LTTNG_DST_IPV6
:
3584 DBG2("Setting network URI for kernel session %s", session
->name
);
3586 /* Set URI into consumer output object */
3587 ret
= consumer_set_network_uri(consumer
, uri
);
3589 ret
= LTTCOMM_FATAL
;
3593 /* On a new subdir, reappend the default trace dir. */
3594 if (strlen(uri
->subdir
) != 0) {
3595 strncat(consumer
->subdir
, DEFAULT_KERNEL_TRACE_DIR
,
3596 sizeof(consumer
->subdir
));
3599 ret
= send_socket_relayd_consumer(domain
, session
, uri
, consumer
,
3600 ksess
->consumer_fd
);
3601 if (ret
!= LTTCOMM_OK
) {
3605 case LTTNG_DST_PATH
:
3606 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
3607 memset(consumer
->dst
.trace_path
, 0,
3608 sizeof(consumer
->dst
.trace_path
));
3609 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
3610 sizeof(consumer
->dst
.trace_path
));
3611 /* Append default kernel trace dir */
3612 strncat(consumer
->dst
.trace_path
, DEFAULT_KERNEL_TRACE_DIR
,
3613 sizeof(consumer
->dst
.trace_path
));
3619 case LTTNG_DOMAIN_UST
:
3620 /* Code flow error if we don't have a kernel session here. */
3623 /* Create consumer output if none exists */
3624 consumer
= usess
->tmp_consumer
;
3625 if (consumer
== NULL
) {
3626 consumer
= consumer_copy_output(usess
->consumer
);
3627 if (consumer
== NULL
) {
3628 ret
= LTTCOMM_FATAL
;
3631 /* Reassign new pointer */
3632 usess
->tmp_consumer
= consumer
;
3635 switch (uri
->dtype
) {
3636 case LTTNG_DST_IPV4
:
3637 case LTTNG_DST_IPV6
:
3639 DBG2("Setting network URI for UST session %s", session
->name
);
3641 /* Set URI into consumer object */
3642 ret
= consumer_set_network_uri(consumer
, uri
);
3644 ret
= LTTCOMM_FATAL
;
3648 /* On a new subdir, reappend the default trace dir. */
3649 if (strlen(uri
->subdir
) != 0) {
3650 strncat(consumer
->subdir
, DEFAULT_UST_TRACE_DIR
,
3651 sizeof(consumer
->subdir
));
3654 if (ust_consumerd64_fd
>= 0) {
3655 ret
= send_socket_relayd_consumer(domain
, session
, uri
,
3656 consumer
, ust_consumerd64_fd
);
3657 if (ret
!= LTTCOMM_OK
) {
3662 if (ust_consumerd32_fd
>= 0) {
3663 ret
= send_socket_relayd_consumer(domain
, session
, uri
,
3664 consumer
, ust_consumerd32_fd
);
3665 if (ret
!= LTTCOMM_OK
) {
3672 case LTTNG_DST_PATH
:
3673 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
3674 memset(consumer
->dst
.trace_path
, 0,
3675 sizeof(consumer
->dst
.trace_path
));
3676 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
3677 sizeof(consumer
->dst
.trace_path
));
3678 /* Append default UST trace dir */
3679 strncat(consumer
->dst
.trace_path
, DEFAULT_UST_TRACE_DIR
,
3680 sizeof(consumer
->dst
.trace_path
));
3694 * Command LTTNG_DISABLE_CONSUMER processed by the client thread.
3696 static int cmd_disable_consumer(int domain
, struct ltt_session
*session
)
3699 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3700 struct ltt_ust_session
*usess
= session
->ust_session
;
3701 struct consumer_output
*consumer
;
3703 if (session
->enabled
) {
3704 /* Can't disable consumer on an already started session */
3705 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
3710 case LTTNG_DOMAIN_KERNEL
:
3711 /* Code flow error if we don't have a kernel session here. */
3714 DBG("Disabling kernel consumer");
3715 consumer
= ksess
->consumer
;
3718 case LTTNG_DOMAIN_UST
:
3719 /* Code flow error if we don't have a UST session here. */
3722 DBG("Disabling UST consumer");
3723 consumer
= usess
->consumer
;
3727 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
3732 consumer
->enabled
= 0;
3734 /* Success at this point */
3742 * Command LTTNG_ENABLE_CONSUMER processed by the client thread.
3744 static int cmd_enable_consumer(int domain
, struct ltt_session
*session
)
3747 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3748 struct ltt_ust_session
*usess
= session
->ust_session
;
3749 struct consumer_output
*tmp_out
;
3751 /* Can't enable consumer after session started. */
3752 if (session
->enabled
) {
3753 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
3758 case LTTNG_DOMAIN_KERNEL
:
3759 /* Code flow error if we don't have a kernel session here. */
3763 * Check if we have already sent fds to the consumer. In that case,
3764 * the enable-consumer command can't be used because a start trace
3765 * had previously occured.
3767 if (ksess
->consumer_fds_sent
) {
3768 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
3772 tmp_out
= ksess
->tmp_consumer
;
3773 if (tmp_out
== NULL
) {
3774 /* No temp. consumer output exists. Using the current one. */
3775 DBG3("No temporary consumer. Using default");
3780 switch (tmp_out
->type
) {
3781 case CONSUMER_DST_LOCAL
:
3782 DBG2("Consumer output is local. Creating directory(ies)");
3784 /* Create directory(ies) */
3785 ret
= run_as_mkdir_recursive(tmp_out
->dst
.trace_path
,
3786 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
3788 if (ret
!= -EEXIST
) {
3789 ERR("Trace directory creation error");
3790 ret
= LTTCOMM_FATAL
;
3795 case CONSUMER_DST_NET
:
3796 DBG2("Consumer output is network. Validating URIs");
3797 /* Validate if we have both control and data path set. */
3798 if (!tmp_out
->dst
.net
.control_isset
) {
3799 ret
= LTTCOMM_URI_CTRL_MISS
;
3803 if (!tmp_out
->dst
.net
.data_isset
) {
3804 ret
= LTTCOMM_URI_DATA_MISS
;
3808 /* Check established network session state */
3809 if (session
->net_handle
== 0) {
3810 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
3811 ERR("Session network handle is not set on enable-consumer");
3815 /* Append default kernel trace dir to subdir */
3816 strncat(ksess
->consumer
->subdir
, DEFAULT_KERNEL_TRACE_DIR
,
3817 sizeof(ksess
->consumer
->subdir
));
3824 * This is race free for now since the session lock is acquired before
3825 * ending up in this function. No other threads can access this kernel
3826 * session without this lock hence freeing the consumer output object
3829 consumer_destroy_output(ksess
->consumer
);
3830 ksess
->consumer
= tmp_out
;
3831 ksess
->tmp_consumer
= NULL
;
3834 case LTTNG_DOMAIN_UST
:
3835 /* Code flow error if we don't have a UST session here. */
3839 * Check if we have already sent fds to the consumer. In that case,
3840 * the enable-consumer command can't be used because a start trace
3841 * had previously occured.
3843 if (usess
->start_trace
) {
3844 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
3848 tmp_out
= usess
->tmp_consumer
;
3849 if (tmp_out
== NULL
) {
3850 /* No temp. consumer output exists. Using the current one. */
3851 DBG3("No temporary consumer. Using default");
3856 switch (tmp_out
->type
) {
3857 case CONSUMER_DST_LOCAL
:
3858 DBG2("Consumer output is local. Creating directory(ies)");
3860 /* Create directory(ies) */
3861 ret
= run_as_mkdir_recursive(tmp_out
->dst
.trace_path
,
3862 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
3864 if (ret
!= -EEXIST
) {
3865 ERR("Trace directory creation error");
3866 ret
= LTTCOMM_FATAL
;
3871 case CONSUMER_DST_NET
:
3872 DBG2("Consumer output is network. Validating URIs");
3873 /* Validate if we have both control and data path set. */
3874 if (!tmp_out
->dst
.net
.control_isset
) {
3875 ret
= LTTCOMM_URI_CTRL_MISS
;
3879 if (!tmp_out
->dst
.net
.data_isset
) {
3880 ret
= LTTCOMM_URI_DATA_MISS
;
3884 /* Check established network session state */
3885 if (session
->net_handle
== 0) {
3886 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
3887 DBG2("Session network handle is not set on enable-consumer");
3891 if (tmp_out
->net_seq_index
== -1) {
3892 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
3893 DBG2("Network index is not set on the consumer");
3897 /* Append default kernel trace dir to subdir */
3898 strncat(usess
->consumer
->subdir
, DEFAULT_UST_TRACE_DIR
,
3899 sizeof(usess
->consumer
->subdir
));
3906 * This is race free for now since the session lock is acquired before
3907 * ending up in this function. No other threads can access this kernel
3908 * session without this lock hence freeing the consumer output object
3911 consumer_destroy_output(usess
->consumer
);
3912 usess
->consumer
= tmp_out
;
3913 usess
->tmp_consumer
= NULL
;
3918 /* Success at this point */
3926 * Process the command requested by the lttng client within the command
3927 * context structure. This function make sure that the return structure (llm)
3928 * is set and ready for transmission before returning.
3930 * Return any error encountered or 0 for success.
3932 static int process_client_msg(struct command_ctx
*cmd_ctx
)
3934 int ret
= LTTCOMM_OK
;
3935 int need_tracing_session
= 1;
3938 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
3940 switch (cmd_ctx
->lsm
->cmd_type
) {
3941 case LTTNG_CREATE_SESSION
:
3942 case LTTNG_CREATE_SESSION_URI
:
3943 case LTTNG_DESTROY_SESSION
:
3944 case LTTNG_LIST_SESSIONS
:
3945 case LTTNG_LIST_DOMAINS
:
3946 case LTTNG_START_TRACE
:
3947 case LTTNG_STOP_TRACE
:
3954 if (opt_no_kernel
&& need_domain
3955 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
3957 ret
= LTTCOMM_NEED_ROOT_SESSIOND
;
3959 ret
= LTTCOMM_KERN_NA
;
3965 * Check for command that don't needs to allocate a returned payload. We do
3966 * this here so we don't have to make the call for no payload at each
3969 switch(cmd_ctx
->lsm
->cmd_type
) {
3970 case LTTNG_LIST_SESSIONS
:
3971 case LTTNG_LIST_TRACEPOINTS
:
3972 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3973 case LTTNG_LIST_DOMAINS
:
3974 case LTTNG_LIST_CHANNELS
:
3975 case LTTNG_LIST_EVENTS
:
3978 /* Setup lttng message with no payload */
3979 ret
= setup_lttng_msg(cmd_ctx
, 0);
3981 /* This label does not try to unlock the session */
3982 goto init_setup_error
;
3986 /* Commands that DO NOT need a session. */
3987 switch (cmd_ctx
->lsm
->cmd_type
) {
3988 case LTTNG_CREATE_SESSION
:
3989 case LTTNG_CREATE_SESSION_URI
:
3990 case LTTNG_CALIBRATE
:
3991 case LTTNG_LIST_SESSIONS
:
3992 case LTTNG_LIST_TRACEPOINTS
:
3993 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3994 need_tracing_session
= 0;
3997 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3999 * We keep the session list lock across _all_ commands
4000 * for now, because the per-session lock does not
4001 * handle teardown properly.
4003 session_lock_list();
4004 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
4005 if (cmd_ctx
->session
== NULL
) {
4006 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
4007 ret
= LTTCOMM_SESS_NOT_FOUND
;
4009 /* If no session name specified */
4010 ret
= LTTCOMM_SELECT_SESS
;
4014 /* Acquire lock for the session */
4015 session_lock(cmd_ctx
->session
);
4024 * Check domain type for specific "pre-action".
4026 switch (cmd_ctx
->lsm
->domain
.type
) {
4027 case LTTNG_DOMAIN_KERNEL
:
4029 ret
= LTTCOMM_NEED_ROOT_SESSIOND
;
4033 /* Kernel tracer check */
4034 if (kernel_tracer_fd
== -1) {
4035 /* Basically, load kernel tracer modules */
4036 ret
= init_kernel_tracer();
4042 /* Consumer is in an ERROR state. Report back to client */
4043 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
4044 ret
= LTTCOMM_NO_KERNCONSUMERD
;
4048 /* Need a session for kernel command */
4049 if (need_tracing_session
) {
4050 if (cmd_ctx
->session
->kernel_session
== NULL
) {
4051 ret
= create_kernel_session(cmd_ctx
->session
);
4053 ret
= LTTCOMM_KERN_SESS_FAIL
;
4058 /* Start the kernel consumer daemon */
4059 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
4060 if (kconsumer_data
.pid
== 0 &&
4061 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
4062 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
4063 ret
= start_consumerd(&kconsumer_data
);
4065 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
4068 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
4070 /* Set consumer fd of the session */
4071 cmd_ctx
->session
->kernel_session
->consumer_fd
=
4072 kconsumer_data
.cmd_sock
;
4074 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
4079 case LTTNG_DOMAIN_UST
:
4081 /* Consumer is in an ERROR state. Report back to client */
4082 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
4083 ret
= LTTCOMM_NO_USTCONSUMERD
;
4087 if (need_tracing_session
) {
4088 if (cmd_ctx
->session
->ust_session
== NULL
) {
4089 ret
= create_ust_session(cmd_ctx
->session
,
4090 &cmd_ctx
->lsm
->domain
);
4091 if (ret
!= LTTCOMM_OK
) {
4096 /* Start the UST consumer daemons */
4098 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
4099 if (consumerd64_bin
[0] != '\0' &&
4100 ustconsumer64_data
.pid
== 0 &&
4101 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
4102 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
4103 ret
= start_consumerd(&ustconsumer64_data
);
4105 ret
= LTTCOMM_UST_CONSUMER64_FAIL
;
4106 ust_consumerd64_fd
= -EINVAL
;
4110 ust_consumerd64_fd
= ustconsumer64_data
.cmd_sock
;
4111 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
4113 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
4116 if (consumerd32_bin
[0] != '\0' &&
4117 ustconsumer32_data
.pid
== 0 &&
4118 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
4119 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
4120 ret
= start_consumerd(&ustconsumer32_data
);
4122 ret
= LTTCOMM_UST_CONSUMER32_FAIL
;
4123 ust_consumerd32_fd
= -EINVAL
;
4127 ust_consumerd32_fd
= ustconsumer32_data
.cmd_sock
;
4128 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
4130 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
4140 /* Validate consumer daemon state when start/stop trace command */
4141 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
4142 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
4143 switch (cmd_ctx
->lsm
->domain
.type
) {
4144 case LTTNG_DOMAIN_UST
:
4145 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
4146 ret
= LTTCOMM_NO_USTCONSUMERD
;
4150 case LTTNG_DOMAIN_KERNEL
:
4151 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
4152 ret
= LTTCOMM_NO_KERNCONSUMERD
;
4160 * Check that the UID or GID match that of the tracing session.
4161 * The root user can interact with all sessions.
4163 if (need_tracing_session
) {
4164 if (!session_access_ok(cmd_ctx
->session
,
4165 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4166 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
4167 ret
= LTTCOMM_EPERM
;
4172 /* Process by command type */
4173 switch (cmd_ctx
->lsm
->cmd_type
) {
4174 case LTTNG_ADD_CONTEXT
:
4176 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4177 cmd_ctx
->lsm
->u
.context
.channel_name
,
4178 cmd_ctx
->lsm
->u
.context
.event_name
,
4179 &cmd_ctx
->lsm
->u
.context
.ctx
);
4182 case LTTNG_DISABLE_CHANNEL
:
4184 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4185 cmd_ctx
->lsm
->u
.disable
.channel_name
);
4188 case LTTNG_DISABLE_EVENT
:
4190 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4191 cmd_ctx
->lsm
->u
.disable
.channel_name
,
4192 cmd_ctx
->lsm
->u
.disable
.name
);
4195 case LTTNG_DISABLE_ALL_EVENT
:
4197 DBG("Disabling all events");
4199 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4200 cmd_ctx
->lsm
->u
.disable
.channel_name
);
4203 case LTTNG_DISABLE_CONSUMER
:
4205 ret
= cmd_disable_consumer(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
);
4208 case LTTNG_ENABLE_CHANNEL
:
4210 ret
= cmd_enable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4211 &cmd_ctx
->lsm
->u
.channel
.chan
);
4214 case LTTNG_ENABLE_CONSUMER
:
4216 ret
= cmd_enable_consumer(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
);
4219 case LTTNG_ENABLE_EVENT
:
4221 ret
= cmd_enable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4222 cmd_ctx
->lsm
->u
.enable
.channel_name
,
4223 &cmd_ctx
->lsm
->u
.enable
.event
);
4226 case LTTNG_ENABLE_ALL_EVENT
:
4228 DBG("Enabling all events");
4230 ret
= cmd_enable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4231 cmd_ctx
->lsm
->u
.enable
.channel_name
,
4232 cmd_ctx
->lsm
->u
.enable
.event
.type
);
4235 case LTTNG_LIST_TRACEPOINTS
:
4237 struct lttng_event
*events
;
4240 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
4241 if (nb_events
< 0) {
4247 * Setup lttng message with payload size set to the event list size in
4248 * bytes and then copy list into the llm payload.
4250 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
4256 /* Copy event list into message payload */
4257 memcpy(cmd_ctx
->llm
->payload
, events
,
4258 sizeof(struct lttng_event
) * nb_events
);
4265 case LTTNG_LIST_TRACEPOINT_FIELDS
:
4267 struct lttng_event_field
*fields
;
4270 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
, &fields
);
4271 if (nb_fields
< 0) {
4277 * Setup lttng message with payload size set to the event list size in
4278 * bytes and then copy list into the llm payload.
4280 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event_field
) * nb_fields
);
4286 /* Copy event list into message payload */
4287 memcpy(cmd_ctx
->llm
->payload
, fields
,
4288 sizeof(struct lttng_event_field
) * nb_fields
);
4295 case LTTNG_SET_CONSUMER_URI
:
4297 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
4298 &cmd_ctx
->lsm
->u
.uri
);
4301 case LTTNG_START_TRACE
:
4303 ret
= cmd_start_trace(cmd_ctx
->session
);
4306 case LTTNG_STOP_TRACE
:
4308 ret
= cmd_stop_trace(cmd_ctx
->session
);
4311 case LTTNG_CREATE_SESSION
:
4313 ret
= cmd_create_session(cmd_ctx
->lsm
->session
.name
,
4314 cmd_ctx
->lsm
->session
.path
, &cmd_ctx
->creds
);
4317 case LTTNG_CREATE_SESSION_URI
:
4319 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
,
4320 &cmd_ctx
->lsm
->u
.create_uri
.ctrl_uri
,
4321 &cmd_ctx
->lsm
->u
.create_uri
.data_uri
,
4322 cmd_ctx
->lsm
->u
.create_uri
.enable_consumer
, &cmd_ctx
->creds
);
4325 case LTTNG_DESTROY_SESSION
:
4327 ret
= cmd_destroy_session(cmd_ctx
->session
,
4328 cmd_ctx
->lsm
->session
.name
);
4330 * Set session to NULL so we do not unlock it after
4333 cmd_ctx
->session
= NULL
;
4336 case LTTNG_LIST_DOMAINS
:
4339 struct lttng_domain
*domains
;
4341 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
4347 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
4352 /* Copy event list into message payload */
4353 memcpy(cmd_ctx
->llm
->payload
, domains
,
4354 nb_dom
* sizeof(struct lttng_domain
));
4361 case LTTNG_LIST_CHANNELS
:
4364 struct lttng_channel
*channels
;
4366 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
4367 cmd_ctx
->session
, &channels
);
4373 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
4378 /* Copy event list into message payload */
4379 memcpy(cmd_ctx
->llm
->payload
, channels
,
4380 nb_chan
* sizeof(struct lttng_channel
));
4387 case LTTNG_LIST_EVENTS
:
4390 struct lttng_event
*events
= NULL
;
4392 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
4393 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
4399 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
4404 /* Copy event list into message payload */
4405 memcpy(cmd_ctx
->llm
->payload
, events
,
4406 nb_event
* sizeof(struct lttng_event
));
4413 case LTTNG_LIST_SESSIONS
:
4415 unsigned int nr_sessions
;
4417 session_lock_list();
4418 nr_sessions
= lttng_sessions_count(
4419 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4420 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4422 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
4424 session_unlock_list();
4428 /* Filled the session array */
4429 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
4430 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4431 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4433 session_unlock_list();
4438 case LTTNG_CALIBRATE
:
4440 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
4441 &cmd_ctx
->lsm
->u
.calibrate
);
4444 case LTTNG_REGISTER_CONSUMER
:
4446 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4447 cmd_ctx
->lsm
->u
.reg
.path
);
4456 if (cmd_ctx
->llm
== NULL
) {
4457 DBG("Missing llm structure. Allocating one.");
4458 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
4462 /* Set return code */
4463 cmd_ctx
->llm
->ret_code
= ret
;
4465 if (cmd_ctx
->session
) {
4466 session_unlock(cmd_ctx
->session
);
4468 if (need_tracing_session
) {
4469 session_unlock_list();
4476 * This thread manage all clients request using the unix client socket for
4479 static void *thread_manage_clients(void *data
)
4481 int sock
= -1, ret
, i
, pollfd
;
4482 uint32_t revents
, nb_fd
;
4483 struct command_ctx
*cmd_ctx
= NULL
;
4484 struct lttng_poll_event events
;
4486 DBG("[thread] Manage client started");
4488 rcu_register_thread();
4490 ret
= lttcomm_listen_unix_sock(client_sock
);
4496 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4497 * more will be added to this poll set.
4499 ret
= create_thread_poll_set(&events
, 2);
4504 /* Add the application registration socket */
4505 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4511 * Notify parent pid that we are ready to accept command for client side.
4513 if (opt_sig_parent
) {
4514 kill(ppid
, SIGUSR1
);
4518 DBG("Accepting client command ...");
4520 nb_fd
= LTTNG_POLL_GETNB(&events
);
4522 /* Inifinite blocking call, waiting for transmission */
4524 ret
= lttng_poll_wait(&events
, -1);
4527 * Restart interrupted system call.
4529 if (errno
== EINTR
) {
4535 for (i
= 0; i
< nb_fd
; i
++) {
4536 /* Fetch once the poll data */
4537 revents
= LTTNG_POLL_GETEV(&events
, i
);
4538 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4540 /* Thread quit pipe has been closed. Killing thread. */
4541 ret
= check_thread_quit_pipe(pollfd
, revents
);
4546 /* Event on the registration socket */
4547 if (pollfd
== client_sock
) {
4548 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4549 ERR("Client socket poll error");
4555 DBG("Wait for client response");
4557 sock
= lttcomm_accept_unix_sock(client_sock
);
4562 /* Set socket option for credentials retrieval */
4563 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4568 /* Allocate context command to process the client request */
4569 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4570 if (cmd_ctx
== NULL
) {
4571 PERROR("zmalloc cmd_ctx");
4575 /* Allocate data buffer for reception */
4576 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4577 if (cmd_ctx
->lsm
== NULL
) {
4578 PERROR("zmalloc cmd_ctx->lsm");
4582 cmd_ctx
->llm
= NULL
;
4583 cmd_ctx
->session
= NULL
;
4586 * Data is received from the lttng client. The struct
4587 * lttcomm_session_msg (lsm) contains the command and data request of
4590 DBG("Receiving data from client ...");
4591 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4592 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4594 DBG("Nothing recv() from client... continuing");
4600 clean_command_ctx(&cmd_ctx
);
4604 // TODO: Validate cmd_ctx including sanity check for
4605 // security purpose.
4607 rcu_thread_online();
4609 * This function dispatch the work to the kernel or userspace tracer
4610 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4611 * informations for the client. The command context struct contains
4612 * everything this function may needs.
4614 ret
= process_client_msg(cmd_ctx
);
4615 rcu_thread_offline();
4618 * TODO: Inform client somehow of the fatal error. At
4619 * this point, ret < 0 means that a zmalloc failed
4620 * (ENOMEM). Error detected but still accept command.
4622 clean_command_ctx(&cmd_ctx
);
4626 DBG("Sending response (size: %d, retcode: %s)",
4627 cmd_ctx
->lttng_msg_size
,
4628 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
4629 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4631 ERR("Failed to send data back to client");
4634 /* End of transmission */
4641 clean_command_ctx(&cmd_ctx
);
4645 DBG("Client thread dying");
4646 unlink(client_unix_sock_path
);
4647 if (client_sock
>= 0) {
4648 ret
= close(client_sock
);
4660 lttng_poll_clean(&events
);
4661 clean_command_ctx(&cmd_ctx
);
4663 rcu_unregister_thread();
4669 * usage function on stderr
4671 static void usage(void)
4673 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
4674 fprintf(stderr
, " -h, --help Display this usage.\n");
4675 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
4676 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
4677 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
4678 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
4679 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
4680 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
4681 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
4682 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
4683 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
4684 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
4685 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
4686 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
4687 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
4688 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
4689 fprintf(stderr
, " -V, --version Show version number.\n");
4690 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
4691 fprintf(stderr
, " -q, --quiet No output at all.\n");
4692 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
4693 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4694 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
4698 * daemon argument parsing
4700 static int parse_args(int argc
, char **argv
)
4704 static struct option long_options
[] = {
4705 { "client-sock", 1, 0, 'c' },
4706 { "apps-sock", 1, 0, 'a' },
4707 { "kconsumerd-cmd-sock", 1, 0, 'C' },
4708 { "kconsumerd-err-sock", 1, 0, 'E' },
4709 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
4710 { "ustconsumerd32-err-sock", 1, 0, 'H' },
4711 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
4712 { "ustconsumerd64-err-sock", 1, 0, 'F' },
4713 { "consumerd32-path", 1, 0, 'u' },
4714 { "consumerd32-libdir", 1, 0, 'U' },
4715 { "consumerd64-path", 1, 0, 't' },
4716 { "consumerd64-libdir", 1, 0, 'T' },
4717 { "daemonize", 0, 0, 'd' },
4718 { "sig-parent", 0, 0, 'S' },
4719 { "help", 0, 0, 'h' },
4720 { "group", 1, 0, 'g' },
4721 { "version", 0, 0, 'V' },
4722 { "quiet", 0, 0, 'q' },
4723 { "verbose", 0, 0, 'v' },
4724 { "verbose-consumer", 0, 0, 'Z' },
4725 { "no-kernel", 0, 0, 'N' },
4730 int option_index
= 0;
4731 c
= getopt_long(argc
, argv
, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t",
4732 long_options
, &option_index
);
4739 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
4741 fprintf(stderr
, " with arg %s\n", optarg
);
4745 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4748 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4754 opt_tracing_group
= optarg
;
4760 fprintf(stdout
, "%s\n", VERSION
);
4766 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4769 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4772 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4775 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4778 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4781 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4787 lttng_opt_quiet
= 1;
4790 /* Verbose level can increase using multiple -v */
4791 lttng_opt_verbose
+= 1;
4794 opt_verbose_consumer
+= 1;
4797 consumerd32_bin
= optarg
;
4800 consumerd32_libdir
= optarg
;
4803 consumerd64_bin
= optarg
;
4806 consumerd64_libdir
= optarg
;
4809 /* Unknown option or other error.
4810 * Error is printed by getopt, just return */
4819 * Creates the two needed socket by the daemon.
4820 * apps_sock - The communication socket for all UST apps.
4821 * client_sock - The communication of the cli tool (lttng).
4823 static int init_daemon_socket(void)
4828 old_umask
= umask(0);
4830 /* Create client tool unix socket */
4831 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
4832 if (client_sock
< 0) {
4833 ERR("Create unix sock failed: %s", client_unix_sock_path
);
4838 /* File permission MUST be 660 */
4839 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4841 ERR("Set file permissions failed: %s", client_unix_sock_path
);
4846 /* Create the application unix socket */
4847 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
4848 if (apps_sock
< 0) {
4849 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
4854 /* File permission MUST be 666 */
4855 ret
= chmod(apps_unix_sock_path
,
4856 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
4858 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
4869 * Check if the global socket is available, and if a daemon is answering at the
4870 * other side. If yes, error is returned.
4872 static int check_existing_daemon(void)
4874 /* Is there anybody out there ? */
4875 if (lttng_session_daemon_alive()) {
4883 * Set the tracing group gid onto the client socket.
4885 * Race window between mkdir and chown is OK because we are going from more
4886 * permissive (root.root) to less permissive (root.tracing).
4888 static int set_permissions(char *rundir
)
4893 ret
= allowed_group();
4895 WARN("No tracing group detected");
4902 /* Set lttng run dir */
4903 ret
= chown(rundir
, 0, gid
);
4905 ERR("Unable to set group on %s", rundir
);
4909 /* Ensure tracing group can search the run dir */
4910 ret
= chmod(rundir
, S_IRWXU
| S_IXGRP
| S_IXOTH
);
4912 ERR("Unable to set permissions on %s", rundir
);
4916 /* lttng client socket path */
4917 ret
= chown(client_unix_sock_path
, 0, gid
);
4919 ERR("Unable to set group on %s", client_unix_sock_path
);
4923 /* kconsumer error socket path */
4924 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, gid
);
4926 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
4930 /* 64-bit ustconsumer error socket path */
4931 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, gid
);
4933 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
4937 /* 32-bit ustconsumer compat32 error socket path */
4938 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, gid
);
4940 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
4944 DBG("All permissions are set");
4951 * Create the lttng run directory needed for all global sockets and pipe.
4953 static int create_lttng_rundir(const char *rundir
)
4957 DBG3("Creating LTTng run directory: %s", rundir
);
4959 ret
= mkdir(rundir
, S_IRWXU
);
4961 if (errno
!= EEXIST
) {
4962 ERR("Unable to create %s", rundir
);
4974 * Setup sockets and directory needed by the kconsumerd communication with the
4977 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
4981 char path
[PATH_MAX
];
4983 switch (consumer_data
->type
) {
4984 case LTTNG_CONSUMER_KERNEL
:
4985 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
4987 case LTTNG_CONSUMER64_UST
:
4988 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
4990 case LTTNG_CONSUMER32_UST
:
4991 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
4994 ERR("Consumer type unknown");
4999 DBG2("Creating consumer directory: %s", path
);
5001 ret
= mkdir(path
, S_IRWXU
);
5003 if (errno
!= EEXIST
) {
5005 ERR("Failed to create %s", path
);
5011 /* Create the kconsumerd error unix socket */
5012 consumer_data
->err_sock
=
5013 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5014 if (consumer_data
->err_sock
< 0) {
5015 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5020 /* File permission MUST be 660 */
5021 ret
= chmod(consumer_data
->err_unix_sock_path
,
5022 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5024 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5034 * Signal handler for the daemon
5036 * Simply stop all worker threads, leaving main() return gracefully after
5037 * joining all threads and calling cleanup().
5039 static void sighandler(int sig
)
5043 DBG("SIGPIPE caught");
5046 DBG("SIGINT caught");
5050 DBG("SIGTERM caught");
5059 * Setup signal handler for :
5060 * SIGINT, SIGTERM, SIGPIPE
5062 static int set_signal_handler(void)
5065 struct sigaction sa
;
5068 if ((ret
= sigemptyset(&sigset
)) < 0) {
5069 PERROR("sigemptyset");
5073 sa
.sa_handler
= sighandler
;
5074 sa
.sa_mask
= sigset
;
5076 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5077 PERROR("sigaction");
5081 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5082 PERROR("sigaction");
5086 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5087 PERROR("sigaction");
5091 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
5097 * Set open files limit to unlimited. This daemon can open a large number of
5098 * file descriptors in order to consumer multiple kernel traces.
5100 static void set_ulimit(void)
5105 /* The kernel does not allowed an infinite limit for open files */
5106 lim
.rlim_cur
= 65535;
5107 lim
.rlim_max
= 65535;
5109 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5111 PERROR("failed to set open files limit");
5118 int main(int argc
, char **argv
)
5122 const char *home_path
;
5124 init_kernel_workarounds();
5126 rcu_register_thread();
5128 setup_consumerd_path();
5130 /* Parse arguments */
5132 if ((ret
= parse_args(argc
, argv
) < 0)) {
5142 * child: setsid, close FD 0, 1, 2, chdir /
5143 * parent: exit (if fork is successful)
5151 * We are in the child. Make sure all other file
5152 * descriptors are closed, in case we are called with
5153 * more opened file descriptors than the standard ones.
5155 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5160 /* Create thread quit pipe */
5161 if ((ret
= init_thread_quit_pipe()) < 0) {
5165 /* Check if daemon is UID = 0 */
5166 is_root
= !getuid();
5169 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
5171 /* Create global run dir with root access */
5172 ret
= create_lttng_rundir(rundir
);
5177 if (strlen(apps_unix_sock_path
) == 0) {
5178 snprintf(apps_unix_sock_path
, PATH_MAX
,
5179 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
5182 if (strlen(client_unix_sock_path
) == 0) {
5183 snprintf(client_unix_sock_path
, PATH_MAX
,
5184 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
5187 /* Set global SHM for ust */
5188 if (strlen(wait_shm_path
) == 0) {
5189 snprintf(wait_shm_path
, PATH_MAX
,
5190 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
5193 /* Setup kernel consumerd path */
5194 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
5195 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
5196 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
5197 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
5199 DBG2("Kernel consumer err path: %s",
5200 kconsumer_data
.err_unix_sock_path
);
5201 DBG2("Kernel consumer cmd path: %s",
5202 kconsumer_data
.cmd_unix_sock_path
);
5204 home_path
= get_home_dir();
5205 if (home_path
== NULL
) {
5206 /* TODO: Add --socket PATH option */
5207 ERR("Can't get HOME directory for sockets creation.");
5213 * Create rundir from home path. This will create something like
5216 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
5222 ret
= create_lttng_rundir(rundir
);
5227 if (strlen(apps_unix_sock_path
) == 0) {
5228 snprintf(apps_unix_sock_path
, PATH_MAX
,
5229 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
5232 /* Set the cli tool unix socket path */
5233 if (strlen(client_unix_sock_path
) == 0) {
5234 snprintf(client_unix_sock_path
, PATH_MAX
,
5235 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
5238 /* Set global SHM for ust */
5239 if (strlen(wait_shm_path
) == 0) {
5240 snprintf(wait_shm_path
, PATH_MAX
,
5241 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, geteuid());
5245 /* Set consumer initial state */
5246 kernel_consumerd_state
= CONSUMER_STOPPED
;
5247 ust_consumerd_state
= CONSUMER_STOPPED
;
5249 DBG("Client socket path %s", client_unix_sock_path
);
5250 DBG("Application socket path %s", apps_unix_sock_path
);
5251 DBG("LTTng run directory path: %s", rundir
);
5253 /* 32 bits consumerd path setup */
5254 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
5255 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
5256 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
5257 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
5259 DBG2("UST consumer 32 bits err path: %s",
5260 ustconsumer32_data
.err_unix_sock_path
);
5261 DBG2("UST consumer 32 bits cmd path: %s",
5262 ustconsumer32_data
.cmd_unix_sock_path
);
5264 /* 64 bits consumerd path setup */
5265 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
5266 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
5267 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
5268 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
5270 DBG2("UST consumer 64 bits err path: %s",
5271 ustconsumer64_data
.err_unix_sock_path
);
5272 DBG2("UST consumer 64 bits cmd path: %s",
5273 ustconsumer64_data
.cmd_unix_sock_path
);
5276 * See if daemon already exist.
5278 if ((ret
= check_existing_daemon()) < 0) {
5279 ERR("Already running daemon.\n");
5281 * We do not goto exit because we must not cleanup()
5282 * because a daemon is already running.
5288 * Init UST app hash table. Alloc hash table before this point since
5289 * cleanup() can get called after that point.
5293 /* After this point, we can safely call cleanup() with "goto exit" */
5296 * These actions must be executed as root. We do that *after* setting up
5297 * the sockets path because we MUST make the check for another daemon using
5298 * those paths *before* trying to set the kernel consumer sockets and init
5302 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
5307 /* Setup kernel tracer */
5308 if (!opt_no_kernel
) {
5309 init_kernel_tracer();
5312 /* Set ulimit for open files */
5315 /* init lttng_fd tracking must be done after set_ulimit. */
5318 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
5323 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
5328 if ((ret
= set_signal_handler()) < 0) {
5332 /* Setup the needed unix socket */
5333 if ((ret
= init_daemon_socket()) < 0) {
5337 /* Set credentials to socket */
5338 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
5342 /* Get parent pid if -S, --sig-parent is specified. */
5343 if (opt_sig_parent
) {
5347 /* Setup the kernel pipe for waking up the kernel thread */
5348 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
5352 /* Setup the thread apps communication pipe. */
5353 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
5357 /* Init UST command queue. */
5358 cds_wfq_init(&ust_cmd_queue
.queue
);
5361 * Get session list pointer. This pointer MUST NOT be free(). This list is
5362 * statically declared in session.c
5364 session_list_ptr
= session_get_list();
5366 /* Set up max poll set size */
5367 lttng_poll_set_max_size();
5370 * Set network sequence index to 1 for streams to match a relayd socket on
5371 * the consumer side.
5373 uatomic_set(&relayd_net_seq_idx
, 1);
5375 /* Create thread to manage the client socket */
5376 ret
= pthread_create(&client_thread
, NULL
,
5377 thread_manage_clients
, (void *) NULL
);
5379 PERROR("pthread_create clients");
5383 /* Create thread to dispatch registration */
5384 ret
= pthread_create(&dispatch_thread
, NULL
,
5385 thread_dispatch_ust_registration
, (void *) NULL
);
5387 PERROR("pthread_create dispatch");
5391 /* Create thread to manage application registration. */
5392 ret
= pthread_create(®_apps_thread
, NULL
,
5393 thread_registration_apps
, (void *) NULL
);
5395 PERROR("pthread_create registration");
5399 /* Create thread to manage application socket */
5400 ret
= pthread_create(&apps_thread
, NULL
,
5401 thread_manage_apps
, (void *) NULL
);
5403 PERROR("pthread_create apps");
5407 /* Create kernel thread to manage kernel event */
5408 ret
= pthread_create(&kernel_thread
, NULL
,
5409 thread_manage_kernel
, (void *) NULL
);
5411 PERROR("pthread_create kernel");
5415 ret
= pthread_join(kernel_thread
, &status
);
5417 PERROR("pthread_join");
5418 goto error
; /* join error, exit without cleanup */
5422 ret
= pthread_join(apps_thread
, &status
);
5424 PERROR("pthread_join");
5425 goto error
; /* join error, exit without cleanup */
5429 ret
= pthread_join(reg_apps_thread
, &status
);
5431 PERROR("pthread_join");
5432 goto error
; /* join error, exit without cleanup */
5436 ret
= pthread_join(dispatch_thread
, &status
);
5438 PERROR("pthread_join");
5439 goto error
; /* join error, exit without cleanup */
5443 ret
= pthread_join(client_thread
, &status
);
5445 PERROR("pthread_join");
5446 goto error
; /* join error, exit without cleanup */
5449 ret
= join_consumer_thread(&kconsumer_data
);
5451 PERROR("join_consumer");
5452 goto error
; /* join error, exit without cleanup */
5458 * cleanup() is called when no other thread is running.
5460 rcu_thread_online();
5462 rcu_thread_offline();
5463 rcu_unregister_thread();