2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <semaphore.h>
30 #include <sys/mount.h>
31 #include <sys/resource.h>
32 #include <sys/socket.h>
34 #include <sys/types.h>
36 #include <urcu/uatomic.h>
40 #include <common/common.h>
41 #include <common/compat/poll.h>
42 #include <common/compat/socket.h>
43 #include <common/defaults.h>
44 #include <common/kernel-consumer/kernel-consumer.h>
45 #include <common/futex.h>
46 #include <common/relayd/relayd.h>
47 #include <common/utils.h>
49 #include "lttng-sessiond.h"
55 #include "kernel-consumer.h"
59 #include "ust-consumer.h"
65 #define CONSUMERD_FILE "lttng-consumerd"
68 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
69 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
70 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
71 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
74 const char *opt_tracing_group
;
75 static int opt_sig_parent
;
76 static int opt_verbose_consumer
;
77 static int opt_daemon
;
78 static int opt_no_kernel
;
79 static int is_root
; /* Set to 1 if the daemon is running as root */
80 static pid_t ppid
; /* Parent PID for --sig-parent option */
83 /* Consumer daemon specific control data */
84 static struct consumer_data kconsumer_data
= {
85 .type
= LTTNG_CONSUMER_KERNEL
,
86 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
87 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
91 static struct consumer_data ustconsumer64_data
= {
92 .type
= LTTNG_CONSUMER64_UST
,
93 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
94 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
98 static struct consumer_data ustconsumer32_data
= {
99 .type
= LTTNG_CONSUMER32_UST
,
100 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
101 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
106 /* Shared between threads */
107 static int dispatch_thread_exit
;
109 /* Global application Unix socket path */
110 static char apps_unix_sock_path
[PATH_MAX
];
111 /* Global client Unix socket path */
112 static char client_unix_sock_path
[PATH_MAX
];
113 /* global wait shm path for UST */
114 static char wait_shm_path
[PATH_MAX
];
115 /* Global health check unix path */
116 static char health_unix_sock_path
[PATH_MAX
];
118 /* Sockets and FDs */
119 static int client_sock
= -1;
120 static int apps_sock
= -1;
121 static int kernel_tracer_fd
= -1;
122 static int kernel_poll_pipe
[2] = { -1, -1 };
125 * Quit pipe for all threads. This permits a single cancellation point
126 * for all threads when receiving an event on the pipe.
128 static int thread_quit_pipe
[2] = { -1, -1 };
131 * This pipe is used to inform the thread managing application communication
132 * that a command is queued and ready to be processed.
134 static int apps_cmd_pipe
[2] = { -1, -1 };
136 /* Pthread, Mutexes and Semaphores */
137 static pthread_t apps_thread
;
138 static pthread_t reg_apps_thread
;
139 static pthread_t client_thread
;
140 static pthread_t kernel_thread
;
141 static pthread_t dispatch_thread
;
142 static pthread_t health_thread
;
145 * UST registration command queue. This queue is tied with a futex and uses a N
146 * wakers / 1 waiter implemented and detailed in futex.c/.h
148 * The thread_manage_apps and thread_dispatch_ust_registration interact with
149 * this queue and the wait/wake scheme.
151 static struct ust_cmd_queue ust_cmd_queue
;
154 * Pointer initialized before thread creation.
156 * This points to the tracing session list containing the session count and a
157 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
158 * MUST NOT be taken if you call a public function in session.c.
160 * The lock is nested inside the structure: session_list_ptr->lock. Please use
161 * session_lock_list and session_unlock_list for lock acquisition.
163 static struct ltt_session_list
*session_list_ptr
;
165 int ust_consumerd64_fd
= -1;
166 int ust_consumerd32_fd
= -1;
168 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
169 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
170 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
171 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
174 * Consumer daemon state which is changed when spawning it, killing it or in
175 * case of a fatal error.
177 enum consumerd_state
{
178 CONSUMER_STARTED
= 1,
179 CONSUMER_STOPPED
= 2,
184 * This consumer daemon state is used to validate if a client command will be
185 * able to reach the consumer. If not, the client is informed. For instance,
186 * doing a "lttng start" when the consumer state is set to ERROR will return an
187 * error to the client.
189 * The following example shows a possible race condition of this scheme:
191 * consumer thread error happens
193 * client cmd checks state -> still OK
194 * consumer thread exit, sets error
195 * client cmd try to talk to consumer
198 * However, since the consumer is a different daemon, we have no way of making
199 * sure the command will reach it safely even with this state flag. This is why
200 * we consider that up to the state validation during command processing, the
201 * command is safe. After that, we can not guarantee the correctness of the
202 * client request vis-a-vis the consumer.
204 static enum consumerd_state ust_consumerd_state
;
205 static enum consumerd_state kernel_consumerd_state
;
208 * Used to keep a unique index for each relayd socket created where this value
209 * is associated with streams on the consumer so it can match the right relayd
212 * This value should be incremented atomically for safety purposes and future
213 * possible concurrent access.
215 static unsigned int relayd_net_seq_idx
;
217 /* Used for the health monitoring of the session daemon. See health.h */
218 struct health_state health_thread_cmd
;
219 struct health_state health_thread_app_reg
;
220 struct health_state health_thread_kernel
;
223 void setup_consumerd_path(void)
225 const char *bin
, *libdir
;
228 * Allow INSTALL_BIN_PATH to be used as a target path for the
229 * native architecture size consumer if CONFIG_CONSUMER*_PATH
230 * has not been defined.
232 #if (CAA_BITS_PER_LONG == 32)
233 if (!consumerd32_bin
[0]) {
234 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
236 if (!consumerd32_libdir
[0]) {
237 consumerd32_libdir
= INSTALL_LIB_PATH
;
239 #elif (CAA_BITS_PER_LONG == 64)
240 if (!consumerd64_bin
[0]) {
241 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
243 if (!consumerd64_libdir
[0]) {
244 consumerd64_libdir
= INSTALL_LIB_PATH
;
247 #error "Unknown bitness"
251 * runtime env. var. overrides the build default.
253 bin
= getenv("LTTNG_CONSUMERD32_BIN");
255 consumerd32_bin
= bin
;
257 bin
= getenv("LTTNG_CONSUMERD64_BIN");
259 consumerd64_bin
= bin
;
261 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
263 consumerd32_libdir
= libdir
;
265 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
267 consumerd64_libdir
= libdir
;
272 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
274 static int create_thread_poll_set(struct lttng_poll_event
*events
,
279 if (events
== NULL
|| size
== 0) {
284 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
290 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
302 * Check if the thread quit pipe was triggered.
304 * Return 1 if it was triggered else 0;
306 static int check_thread_quit_pipe(int fd
, uint32_t events
)
308 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
316 * Return group ID of the tracing group or -1 if not found.
318 static gid_t
allowed_group(void)
322 if (opt_tracing_group
) {
323 grp
= getgrnam(opt_tracing_group
);
325 grp
= getgrnam(default_tracing_group
);
335 * Init thread quit pipe.
337 * Return -1 on error or 0 if all pipes are created.
339 static int init_thread_quit_pipe(void)
343 ret
= pipe(thread_quit_pipe
);
345 PERROR("thread quit pipe");
349 for (i
= 0; i
< 2; i
++) {
350 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
362 * Complete teardown of a kernel session. This free all data structure related
363 * to a kernel session and update counter.
365 static void teardown_kernel_session(struct ltt_session
*session
)
367 if (!session
->kernel_session
) {
368 DBG3("No kernel session when tearing down session");
372 DBG("Tearing down kernel session");
375 * If a custom kernel consumer was registered, close the socket before
376 * tearing down the complete kernel session structure
378 if (kconsumer_data
.cmd_sock
>= 0 &&
379 session
->kernel_session
->consumer_fd
!= kconsumer_data
.cmd_sock
) {
380 lttcomm_close_unix_sock(session
->kernel_session
->consumer_fd
);
383 trace_kernel_destroy_session(session
->kernel_session
);
387 * Complete teardown of all UST sessions. This will free everything on his path
388 * and destroy the core essence of all ust sessions :)
390 static void teardown_ust_session(struct ltt_session
*session
)
394 if (!session
->ust_session
) {
395 DBG3("No UST session when tearing down session");
399 DBG("Tearing down UST session(s)");
401 ret
= ust_app_destroy_trace_all(session
->ust_session
);
403 ERR("Error in ust_app_destroy_trace_all");
406 trace_ust_destroy_session(session
->ust_session
);
410 * Stop all threads by closing the thread quit pipe.
412 static void stop_threads(void)
416 /* Stopping all threads */
417 DBG("Terminating all threads");
418 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
420 ERR("write error on thread quit pipe");
423 /* Dispatch thread */
424 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
425 futex_nto1_wake(&ust_cmd_queue
.futex
);
431 static void cleanup(void)
435 struct ltt_session
*sess
, *stmp
;
439 DBG("Removing %s directory", rundir
);
440 ret
= asprintf(&cmd
, "rm -rf %s", rundir
);
442 ERR("asprintf failed. Something is really wrong!");
445 /* Remove lttng run directory */
448 ERR("Unable to clean %s", rundir
);
452 DBG("Cleaning up all sessions");
454 /* Destroy session list mutex */
455 if (session_list_ptr
!= NULL
) {
456 pthread_mutex_destroy(&session_list_ptr
->lock
);
458 /* Cleanup ALL session */
459 cds_list_for_each_entry_safe(sess
, stmp
,
460 &session_list_ptr
->head
, list
) {
461 teardown_kernel_session(sess
);
462 teardown_ust_session(sess
);
467 DBG("Closing all UST sockets");
468 ust_app_clean_list();
470 pthread_mutex_destroy(&kconsumer_data
.pid_mutex
);
472 if (is_root
&& !opt_no_kernel
) {
473 DBG2("Closing kernel fd");
474 if (kernel_tracer_fd
>= 0) {
475 ret
= close(kernel_tracer_fd
);
480 DBG("Unloading kernel modules");
481 modprobe_remove_lttng_all();
483 utils_close_pipe(kernel_poll_pipe
);
484 utils_close_pipe(thread_quit_pipe
);
485 utils_close_pipe(apps_cmd_pipe
);
488 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
489 "Matthew, BEET driven development works!%c[%dm",
490 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
495 * Send data on a unix socket using the liblttsessiondcomm API.
497 * Return lttcomm error code.
499 static int send_unix_sock(int sock
, void *buf
, size_t len
)
501 /* Check valid length */
506 return lttcomm_send_unix_sock(sock
, buf
, len
);
510 * Free memory of a command context structure.
512 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
514 DBG("Clean command context structure");
516 if ((*cmd_ctx
)->llm
) {
517 free((*cmd_ctx
)->llm
);
519 if ((*cmd_ctx
)->lsm
) {
520 free((*cmd_ctx
)->lsm
);
528 * Notify UST applications using the shm mmap futex.
530 static int notify_ust_apps(int active
)
534 DBG("Notifying applications of session daemon state: %d", active
);
536 /* See shm.c for this call implying mmap, shm and futex calls */
537 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
538 if (wait_shm_mmap
== NULL
) {
542 /* Wake waiting process */
543 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
545 /* Apps notified successfully */
553 * Setup the outgoing data buffer for the response (llm) by allocating the
554 * right amount of memory and copying the original information from the lsm
557 * Return total size of the buffer pointed by buf.
559 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
565 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
566 if (cmd_ctx
->llm
== NULL
) {
572 /* Copy common data */
573 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
574 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
576 cmd_ctx
->llm
->data_size
= size
;
577 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
586 * Update the kernel poll set of all channel fd available over all tracing
587 * session. Add the wakeup pipe at the end of the set.
589 static int update_kernel_poll(struct lttng_poll_event
*events
)
592 struct ltt_session
*session
;
593 struct ltt_kernel_channel
*channel
;
595 DBG("Updating kernel poll set");
598 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
599 session_lock(session
);
600 if (session
->kernel_session
== NULL
) {
601 session_unlock(session
);
605 cds_list_for_each_entry(channel
,
606 &session
->kernel_session
->channel_list
.head
, list
) {
607 /* Add channel fd to the kernel poll set */
608 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
610 session_unlock(session
);
613 DBG("Channel fd %d added to kernel set", channel
->fd
);
615 session_unlock(session
);
617 session_unlock_list();
622 session_unlock_list();
627 * Find the channel fd from 'fd' over all tracing session. When found, check
628 * for new channel stream and send those stream fds to the kernel consumer.
630 * Useful for CPU hotplug feature.
632 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
635 struct ltt_session
*session
;
636 struct ltt_kernel_channel
*channel
;
638 DBG("Updating kernel streams for channel fd %d", fd
);
641 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
642 session_lock(session
);
643 if (session
->kernel_session
== NULL
) {
644 session_unlock(session
);
648 /* This is not suppose to be -1 but this is an extra security check */
649 if (session
->kernel_session
->consumer_fd
< 0) {
650 session
->kernel_session
->consumer_fd
= consumer_data
->cmd_sock
;
653 cds_list_for_each_entry(channel
,
654 &session
->kernel_session
->channel_list
.head
, list
) {
655 if (channel
->fd
== fd
) {
656 DBG("Channel found, updating kernel streams");
657 ret
= kernel_open_channel_stream(channel
);
663 * Have we already sent fds to the consumer? If yes, it means
664 * that tracing is started so it is safe to send our updated
667 if (session
->kernel_session
->consumer_fds_sent
== 1 &&
668 session
->kernel_session
->consumer
!= NULL
) {
669 ret
= kernel_consumer_send_channel_stream(
670 session
->kernel_session
->consumer_fd
, channel
,
671 session
->kernel_session
);
679 session_unlock(session
);
681 session_unlock_list();
685 session_unlock(session
);
686 session_unlock_list();
691 * For each tracing session, update newly registered apps.
693 static void update_ust_app(int app_sock
)
695 struct ltt_session
*sess
, *stmp
;
699 /* For all tracing session(s) */
700 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
702 if (sess
->ust_session
) {
703 ust_app_global_update(sess
->ust_session
, app_sock
);
705 session_unlock(sess
);
708 session_unlock_list();
712 * This thread manage event coming from the kernel.
714 * Features supported in this thread:
717 static void *thread_manage_kernel(void *data
)
719 int ret
, i
, pollfd
, update_poll_flag
= 1;
720 uint32_t revents
, nb_fd
;
722 struct lttng_poll_event events
;
724 DBG("Thread manage kernel started");
726 health_code_update(&health_thread_kernel
);
728 ret
= create_thread_poll_set(&events
, 2);
730 goto error_poll_create
;
733 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
739 health_code_update(&health_thread_kernel
);
741 if (update_poll_flag
== 1) {
743 * Reset number of fd in the poll set. Always 2 since there is the thread
744 * quit pipe and the kernel pipe.
748 ret
= update_kernel_poll(&events
);
752 update_poll_flag
= 0;
755 nb_fd
= LTTNG_POLL_GETNB(&events
);
757 DBG("Thread kernel polling on %d fds", nb_fd
);
759 /* Zeroed the poll events */
760 lttng_poll_reset(&events
);
762 /* Poll infinite value of time */
764 health_poll_update(&health_thread_kernel
);
765 ret
= lttng_poll_wait(&events
, -1);
766 health_poll_update(&health_thread_kernel
);
769 * Restart interrupted system call.
771 if (errno
== EINTR
) {
775 } else if (ret
== 0) {
776 /* Should not happen since timeout is infinite */
777 ERR("Return value of poll is 0 with an infinite timeout.\n"
778 "This should not have happened! Continuing...");
782 for (i
= 0; i
< nb_fd
; i
++) {
783 /* Fetch once the poll data */
784 revents
= LTTNG_POLL_GETEV(&events
, i
);
785 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
787 health_code_update(&health_thread_kernel
);
789 /* Thread quit pipe has been closed. Killing thread. */
790 ret
= check_thread_quit_pipe(pollfd
, revents
);
795 /* Check for data on kernel pipe */
796 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
797 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
798 update_poll_flag
= 1;
802 * New CPU detected by the kernel. Adding kernel stream to
803 * kernel session and updating the kernel consumer
805 if (revents
& LPOLLIN
) {
806 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
812 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
813 * and unregister kernel stream at this point.
821 lttng_poll_clean(&events
);
823 health_reset(&health_thread_kernel
);
824 DBG("Kernel thread dying");
829 * This thread manage the consumer error sent back to the session daemon.
831 static void *thread_manage_consumer(void *data
)
833 int sock
= -1, i
, ret
, pollfd
;
834 uint32_t revents
, nb_fd
;
835 enum lttcomm_return_code code
;
836 struct lttng_poll_event events
;
837 struct consumer_data
*consumer_data
= data
;
839 DBG("[thread] Manage consumer started");
841 health_code_update(&consumer_data
->health
);
843 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
849 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
850 * Nothing more will be added to this poll set.
852 ret
= create_thread_poll_set(&events
, 2);
857 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
862 nb_fd
= LTTNG_POLL_GETNB(&events
);
864 health_code_update(&consumer_data
->health
);
866 /* Inifinite blocking call, waiting for transmission */
868 health_poll_update(&consumer_data
->health
);
869 ret
= lttng_poll_wait(&events
, -1);
870 health_poll_update(&consumer_data
->health
);
873 * Restart interrupted system call.
875 if (errno
== EINTR
) {
881 for (i
= 0; i
< nb_fd
; i
++) {
882 /* Fetch once the poll data */
883 revents
= LTTNG_POLL_GETEV(&events
, i
);
884 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
886 health_code_update(&consumer_data
->health
);
888 /* Thread quit pipe has been closed. Killing thread. */
889 ret
= check_thread_quit_pipe(pollfd
, revents
);
894 /* Event on the registration socket */
895 if (pollfd
== consumer_data
->err_sock
) {
896 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
897 ERR("consumer err socket poll error");
903 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
908 health_code_update(&consumer_data
->health
);
910 DBG2("Receiving code from consumer err_sock");
912 /* Getting status code from kconsumerd */
913 ret
= lttcomm_recv_unix_sock(sock
, &code
,
914 sizeof(enum lttcomm_return_code
));
919 health_code_update(&consumer_data
->health
);
921 if (code
== CONSUMERD_COMMAND_SOCK_READY
) {
922 consumer_data
->cmd_sock
=
923 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
924 if (consumer_data
->cmd_sock
< 0) {
925 sem_post(&consumer_data
->sem
);
926 PERROR("consumer connect");
929 /* Signal condition to tell that the kconsumerd is ready */
930 sem_post(&consumer_data
->sem
);
931 DBG("consumer command socket ready");
933 ERR("consumer error when waiting for SOCK_READY : %s",
934 lttcomm_get_readable_code(-code
));
938 /* Remove the kconsumerd error sock since we've established a connexion */
939 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
944 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
949 health_code_update(&consumer_data
->health
);
951 /* Update number of fd */
952 nb_fd
= LTTNG_POLL_GETNB(&events
);
954 /* Inifinite blocking call, waiting for transmission */
956 health_poll_update(&consumer_data
->health
);
957 ret
= lttng_poll_wait(&events
, -1);
958 health_poll_update(&consumer_data
->health
);
961 * Restart interrupted system call.
963 if (errno
== EINTR
) {
969 for (i
= 0; i
< nb_fd
; i
++) {
970 /* Fetch once the poll data */
971 revents
= LTTNG_POLL_GETEV(&events
, i
);
972 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
974 health_code_update(&consumer_data
->health
);
976 /* Thread quit pipe has been closed. Killing thread. */
977 ret
= check_thread_quit_pipe(pollfd
, revents
);
982 /* Event on the kconsumerd socket */
983 if (pollfd
== sock
) {
984 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
985 ERR("consumer err socket second poll error");
991 health_code_update(&consumer_data
->health
);
993 /* Wait for any kconsumerd error */
994 ret
= lttcomm_recv_unix_sock(sock
, &code
,
995 sizeof(enum lttcomm_return_code
));
997 ERR("consumer closed the command socket");
1001 ERR("consumer return code : %s", lttcomm_get_readable_code(-code
));
1004 /* Immediately set the consumerd state to stopped */
1005 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1006 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1007 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1008 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1009 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1011 /* Code flow error... */
1015 if (consumer_data
->err_sock
>= 0) {
1016 ret
= close(consumer_data
->err_sock
);
1021 if (consumer_data
->cmd_sock
>= 0) {
1022 ret
= close(consumer_data
->cmd_sock
);
1034 unlink(consumer_data
->err_unix_sock_path
);
1035 unlink(consumer_data
->cmd_unix_sock_path
);
1036 consumer_data
->pid
= 0;
1038 lttng_poll_clean(&events
);
1041 health_reset(&consumer_data
->health
);
1042 DBG("consumer thread cleanup completed");
1048 * This thread manage application communication.
1050 static void *thread_manage_apps(void *data
)
1053 uint32_t revents
, nb_fd
;
1054 struct ust_command ust_cmd
;
1055 struct lttng_poll_event events
;
1057 DBG("[thread] Manage application started");
1059 rcu_register_thread();
1060 rcu_thread_online();
1062 health_code_update(&health_thread_app_reg
);
1064 ret
= create_thread_poll_set(&events
, 2);
1066 goto error_poll_create
;
1069 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1074 health_code_update(&health_thread_app_reg
);
1077 /* Zeroed the events structure */
1078 lttng_poll_reset(&events
);
1080 nb_fd
= LTTNG_POLL_GETNB(&events
);
1082 DBG("Apps thread polling on %d fds", nb_fd
);
1084 /* Inifinite blocking call, waiting for transmission */
1086 health_poll_update(&health_thread_app_reg
);
1087 ret
= lttng_poll_wait(&events
, -1);
1088 health_poll_update(&health_thread_app_reg
);
1091 * Restart interrupted system call.
1093 if (errno
== EINTR
) {
1099 for (i
= 0; i
< nb_fd
; i
++) {
1100 /* Fetch once the poll data */
1101 revents
= LTTNG_POLL_GETEV(&events
, i
);
1102 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1104 health_code_update(&health_thread_app_reg
);
1106 /* Thread quit pipe has been closed. Killing thread. */
1107 ret
= check_thread_quit_pipe(pollfd
, revents
);
1112 /* Inspect the apps cmd pipe */
1113 if (pollfd
== apps_cmd_pipe
[0]) {
1114 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1115 ERR("Apps command pipe error");
1117 } else if (revents
& LPOLLIN
) {
1119 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
1120 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
1121 PERROR("read apps cmd pipe");
1125 health_code_update(&health_thread_app_reg
);
1127 /* Register applicaton to the session daemon */
1128 ret
= ust_app_register(&ust_cmd
.reg_msg
,
1130 if (ret
== -ENOMEM
) {
1132 } else if (ret
< 0) {
1136 health_code_update(&health_thread_app_reg
);
1139 * Validate UST version compatibility.
1141 ret
= ust_app_validate_version(ust_cmd
.sock
);
1144 * Add channel(s) and event(s) to newly registered apps
1145 * from lttng global UST domain.
1147 update_ust_app(ust_cmd
.sock
);
1150 health_code_update(&health_thread_app_reg
);
1152 ret
= ust_app_register_done(ust_cmd
.sock
);
1155 * If the registration is not possible, we simply
1156 * unregister the apps and continue
1158 ust_app_unregister(ust_cmd
.sock
);
1161 * We just need here to monitor the close of the UST
1162 * socket and poll set monitor those by default.
1163 * Listen on POLLIN (even if we never expect any
1164 * data) to ensure that hangup wakes us.
1166 ret
= lttng_poll_add(&events
, ust_cmd
.sock
, LPOLLIN
);
1171 DBG("Apps with sock %d added to poll set",
1175 health_code_update(&health_thread_app_reg
);
1181 * At this point, we know that a registered application made
1182 * the event at poll_wait.
1184 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1185 /* Removing from the poll set */
1186 ret
= lttng_poll_del(&events
, pollfd
);
1191 /* Socket closed on remote end. */
1192 ust_app_unregister(pollfd
);
1197 health_code_update(&health_thread_app_reg
);
1202 lttng_poll_clean(&events
);
1204 health_reset(&health_thread_app_reg
);
1205 DBG("Application communication apps thread cleanup complete");
1206 rcu_thread_offline();
1207 rcu_unregister_thread();
1212 * Dispatch request from the registration threads to the application
1213 * communication thread.
1215 static void *thread_dispatch_ust_registration(void *data
)
1218 struct cds_wfq_node
*node
;
1219 struct ust_command
*ust_cmd
= NULL
;
1221 DBG("[thread] Dispatch UST command started");
1223 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1224 /* Atomically prepare the queue futex */
1225 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1228 /* Dequeue command for registration */
1229 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1231 DBG("Woken up but nothing in the UST command queue");
1232 /* Continue thread execution */
1236 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1238 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1239 " gid:%d sock:%d name:%s (version %d.%d)",
1240 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1241 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1242 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1243 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1245 * Inform apps thread of the new application registration. This
1246 * call is blocking so we can be assured that the data will be read
1247 * at some point in time or wait to the end of the world :)
1249 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1250 sizeof(struct ust_command
));
1252 PERROR("write apps cmd pipe");
1253 if (errno
== EBADF
) {
1255 * We can't inform the application thread to process
1256 * registration. We will exit or else application
1257 * registration will not occur and tracing will never
1264 } while (node
!= NULL
);
1266 /* Futex wait on queue. Blocking call on futex() */
1267 futex_nto1_wait(&ust_cmd_queue
.futex
);
1271 DBG("Dispatch thread dying");
1276 * This thread manage application registration.
1278 static void *thread_registration_apps(void *data
)
1280 int sock
= -1, i
, ret
, pollfd
;
1281 uint32_t revents
, nb_fd
;
1282 struct lttng_poll_event events
;
1284 * Get allocated in this thread, enqueued to a global queue, dequeued and
1285 * freed in the manage apps thread.
1287 struct ust_command
*ust_cmd
= NULL
;
1289 DBG("[thread] Manage application registration started");
1291 ret
= lttcomm_listen_unix_sock(apps_sock
);
1297 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1298 * more will be added to this poll set.
1300 ret
= create_thread_poll_set(&events
, 2);
1302 goto error_create_poll
;
1305 /* Add the application registration socket */
1306 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1308 goto error_poll_add
;
1311 /* Notify all applications to register */
1312 ret
= notify_ust_apps(1);
1314 ERR("Failed to notify applications or create the wait shared memory.\n"
1315 "Execution continues but there might be problem for already\n"
1316 "running applications that wishes to register.");
1320 DBG("Accepting application registration");
1322 nb_fd
= LTTNG_POLL_GETNB(&events
);
1324 /* Inifinite blocking call, waiting for transmission */
1326 ret
= lttng_poll_wait(&events
, -1);
1329 * Restart interrupted system call.
1331 if (errno
== EINTR
) {
1337 for (i
= 0; i
< nb_fd
; i
++) {
1338 /* Fetch once the poll data */
1339 revents
= LTTNG_POLL_GETEV(&events
, i
);
1340 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1342 /* Thread quit pipe has been closed. Killing thread. */
1343 ret
= check_thread_quit_pipe(pollfd
, revents
);
1348 /* Event on the registration socket */
1349 if (pollfd
== apps_sock
) {
1350 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1351 ERR("Register apps socket poll error");
1353 } else if (revents
& LPOLLIN
) {
1354 sock
= lttcomm_accept_unix_sock(apps_sock
);
1359 /* Create UST registration command for enqueuing */
1360 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1361 if (ust_cmd
== NULL
) {
1362 PERROR("ust command zmalloc");
1367 * Using message-based transmissions to ensure we don't
1368 * have to deal with partially received messages.
1370 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1372 ERR("Exhausted file descriptors allowed for applications.");
1381 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1382 sizeof(struct ust_register_msg
));
1383 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1385 PERROR("lttcomm_recv_unix_sock register apps");
1387 ERR("Wrong size received on apps register");
1394 lttng_fd_put(LTTNG_FD_APPS
, 1);
1399 ust_cmd
->sock
= sock
;
1402 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1403 " gid:%d sock:%d name:%s (version %d.%d)",
1404 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1405 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1406 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1407 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1410 * Lock free enqueue the registration request. The red pill
1411 * has been taken! This apps will be part of the *system*.
1413 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1416 * Wake the registration queue futex. Implicit memory
1417 * barrier with the exchange in cds_wfq_enqueue.
1419 futex_nto1_wake(&ust_cmd_queue
.futex
);
1426 /* Notify that the registration thread is gone */
1429 if (apps_sock
>= 0) {
1430 ret
= close(apps_sock
);
1440 lttng_fd_put(LTTNG_FD_APPS
, 1);
1442 unlink(apps_unix_sock_path
);
1445 lttng_poll_clean(&events
);
1448 DBG("UST Registration thread cleanup complete");
1454 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1455 * exec or it will fails.
1457 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
1460 struct timespec timeout
;
1462 timeout
.tv_sec
= DEFAULT_SEM_WAIT_TIMEOUT
;
1463 timeout
.tv_nsec
= 0;
1465 /* Setup semaphore */
1466 ret
= sem_init(&consumer_data
->sem
, 0, 0);
1468 PERROR("sem_init consumer semaphore");
1472 ret
= pthread_create(&consumer_data
->thread
, NULL
,
1473 thread_manage_consumer
, consumer_data
);
1475 PERROR("pthread_create consumer");
1480 /* Get time for sem_timedwait absolute timeout */
1481 ret
= clock_gettime(CLOCK_REALTIME
, &timeout
);
1483 PERROR("clock_gettime spawn consumer");
1484 /* Infinite wait for the kconsumerd thread to be ready */
1485 ret
= sem_wait(&consumer_data
->sem
);
1487 /* Normal timeout if the gettime was successful */
1488 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
1489 ret
= sem_timedwait(&consumer_data
->sem
, &timeout
);
1493 if (errno
== ETIMEDOUT
) {
1495 * Call has timed out so we kill the kconsumerd_thread and return
1498 ERR("The consumer thread was never ready. Killing it");
1499 ret
= pthread_cancel(consumer_data
->thread
);
1501 PERROR("pthread_cancel consumer thread");
1504 PERROR("semaphore wait failed consumer thread");
1509 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1510 if (consumer_data
->pid
== 0) {
1511 ERR("Kconsumerd did not start");
1512 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1515 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1524 * Join consumer thread
1526 static int join_consumer_thread(struct consumer_data
*consumer_data
)
1531 if (consumer_data
->pid
!= 0) {
1532 ret
= kill(consumer_data
->pid
, SIGTERM
);
1534 ERR("Error killing consumer daemon");
1537 return pthread_join(consumer_data
->thread
, &status
);
1544 * Fork and exec a consumer daemon (consumerd).
1546 * Return pid if successful else -1.
1548 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
1552 const char *consumer_to_use
;
1553 const char *verbosity
;
1556 DBG("Spawning consumerd");
1563 if (opt_verbose_consumer
) {
1564 verbosity
= "--verbose";
1566 verbosity
= "--quiet";
1568 switch (consumer_data
->type
) {
1569 case LTTNG_CONSUMER_KERNEL
:
1571 * Find out which consumerd to execute. We will first try the
1572 * 64-bit path, then the sessiond's installation directory, and
1573 * fallback on the 32-bit one,
1575 DBG3("Looking for a kernel consumer at these locations:");
1576 DBG3(" 1) %s", consumerd64_bin
);
1577 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
1578 DBG3(" 3) %s", consumerd32_bin
);
1579 if (stat(consumerd64_bin
, &st
) == 0) {
1580 DBG3("Found location #1");
1581 consumer_to_use
= consumerd64_bin
;
1582 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
1583 DBG3("Found location #2");
1584 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
1585 } else if (stat(consumerd32_bin
, &st
) == 0) {
1586 DBG3("Found location #3");
1587 consumer_to_use
= consumerd32_bin
;
1589 DBG("Could not find any valid consumerd executable");
1592 DBG("Using kernel consumer at: %s", consumer_to_use
);
1593 execl(consumer_to_use
,
1594 "lttng-consumerd", verbosity
, "-k",
1595 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1596 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1599 case LTTNG_CONSUMER64_UST
:
1601 char *tmpnew
= NULL
;
1603 if (consumerd64_libdir
[0] != '\0') {
1607 tmp
= getenv("LD_LIBRARY_PATH");
1611 tmplen
= strlen("LD_LIBRARY_PATH=")
1612 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
1613 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1618 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1619 strcat(tmpnew
, consumerd64_libdir
);
1620 if (tmp
[0] != '\0') {
1621 strcat(tmpnew
, ":");
1622 strcat(tmpnew
, tmp
);
1624 ret
= putenv(tmpnew
);
1630 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
1631 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
1632 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1633 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1635 if (consumerd64_libdir
[0] != '\0') {
1643 case LTTNG_CONSUMER32_UST
:
1645 char *tmpnew
= NULL
;
1647 if (consumerd32_libdir
[0] != '\0') {
1651 tmp
= getenv("LD_LIBRARY_PATH");
1655 tmplen
= strlen("LD_LIBRARY_PATH=")
1656 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
1657 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1662 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1663 strcat(tmpnew
, consumerd32_libdir
);
1664 if (tmp
[0] != '\0') {
1665 strcat(tmpnew
, ":");
1666 strcat(tmpnew
, tmp
);
1668 ret
= putenv(tmpnew
);
1674 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
1675 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
1676 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1677 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1679 if (consumerd32_libdir
[0] != '\0') {
1688 PERROR("unknown consumer type");
1692 PERROR("kernel start consumer exec");
1695 } else if (pid
> 0) {
1698 PERROR("start consumer fork");
1706 * Spawn the consumerd daemon and session daemon thread.
1708 static int start_consumerd(struct consumer_data
*consumer_data
)
1712 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1713 if (consumer_data
->pid
!= 0) {
1714 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1718 ret
= spawn_consumerd(consumer_data
);
1720 ERR("Spawning consumerd failed");
1721 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1725 /* Setting up the consumer_data pid */
1726 consumer_data
->pid
= ret
;
1727 DBG2("Consumer pid %d", consumer_data
->pid
);
1728 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1730 DBG2("Spawning consumer control thread");
1731 ret
= spawn_consumer_thread(consumer_data
);
1733 ERR("Fatal error spawning consumer control thread");
1745 * Compute health status of each consumer.
1747 static int check_consumer_health(void)
1752 health_check_state(&kconsumer_data
.health
) &
1753 health_check_state(&ustconsumer32_data
.health
) &
1754 health_check_state(&ustconsumer64_data
.health
);
1756 DBG3("Health consumer check %d", ret
);
1762 * Check version of the lttng-modules.
1764 static int validate_lttng_modules_version(void)
1766 return kernel_validate_version(kernel_tracer_fd
);
1770 * Setup necessary data for kernel tracer action.
1772 static int init_kernel_tracer(void)
1776 /* Modprobe lttng kernel modules */
1777 ret
= modprobe_lttng_control();
1782 /* Open debugfs lttng */
1783 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1784 if (kernel_tracer_fd
< 0) {
1785 DBG("Failed to open %s", module_proc_lttng
);
1790 /* Validate kernel version */
1791 ret
= validate_lttng_modules_version();
1796 ret
= modprobe_lttng_data();
1801 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1805 modprobe_remove_lttng_control();
1806 ret
= close(kernel_tracer_fd
);
1810 kernel_tracer_fd
= -1;
1811 return LTTCOMM_KERN_VERSION
;
1814 ret
= close(kernel_tracer_fd
);
1820 modprobe_remove_lttng_control();
1823 WARN("No kernel tracer available");
1824 kernel_tracer_fd
= -1;
1826 return LTTCOMM_NEED_ROOT_SESSIOND
;
1828 return LTTCOMM_KERN_NA
;
1833 * Init tracing by creating trace directory and sending fds kernel consumer.
1835 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
1839 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
1841 * Assign default kernel consumer socket if no consumer assigned to the
1842 * kernel session. At this point, it's NOT supposed to be -1 but this is
1843 * an extra security check.
1845 if (session
->consumer_fd
< 0) {
1846 session
->consumer_fd
= kconsumer_data
.cmd_sock
;
1849 ret
= kernel_consumer_send_session(session
->consumer_fd
, session
);
1851 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1861 * Create a socket to the relayd using the URI.
1863 * On success, the relayd_sock pointer is set to the created socket.
1864 * Else, it is untouched and an lttcomm error code is returned.
1866 static int create_connect_relayd(struct consumer_output
*output
,
1867 const char *session_name
, struct lttng_uri
*uri
,
1868 struct lttcomm_sock
**relayd_sock
)
1871 struct lttcomm_sock
*sock
;
1873 /* Create socket object from URI */
1874 sock
= lttcomm_alloc_sock_from_uri(uri
);
1876 ret
= LTTCOMM_FATAL
;
1880 ret
= lttcomm_create_sock(sock
);
1882 ret
= LTTCOMM_FATAL
;
1886 /* Connect to relayd so we can proceed with a session creation. */
1887 ret
= relayd_connect(sock
);
1889 ERR("Unable to reach lttng-relayd");
1890 ret
= LTTCOMM_RELAYD_SESSION_FAIL
;
1894 /* Create socket for control stream. */
1895 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
1896 DBG3("Creating relayd stream socket from URI");
1898 /* Check relayd version */
1899 ret
= relayd_version_check(sock
, LTTNG_UST_COMM_MAJOR
, 0);
1901 ret
= LTTCOMM_RELAYD_VERSION_FAIL
;
1904 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
1905 DBG3("Creating relayd data socket from URI");
1907 /* Command is not valid */
1908 ERR("Relayd invalid stream type: %d", uri
->stype
);
1909 ret
= LTTCOMM_INVALID
;
1913 *relayd_sock
= sock
;
1919 (void) relayd_close(sock
);
1923 lttcomm_destroy_sock(sock
);
1930 * Connect to the relayd using URI and send the socket to the right consumer.
1932 static int send_socket_relayd_consumer(int domain
, struct ltt_session
*session
,
1933 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
1937 struct lttcomm_sock
*sock
= NULL
;
1939 /* Set the network sequence index if not set. */
1940 if (consumer
->net_seq_index
== -1) {
1942 * Increment net_seq_idx because we are about to transfer the
1943 * new relayd socket to the consumer.
1945 uatomic_inc(&relayd_net_seq_idx
);
1946 /* Assign unique key so the consumer can match streams */
1947 consumer
->net_seq_index
= uatomic_read(&relayd_net_seq_idx
);
1950 /* Connect to relayd and make version check if uri is the control. */
1951 ret
= create_connect_relayd(consumer
, session
->name
, relayd_uri
, &sock
);
1952 if (ret
!= LTTCOMM_OK
) {
1956 /* If the control socket is connected, network session is ready */
1957 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1958 session
->net_handle
= 1;
1961 /* Send relayd socket to consumer. */
1962 ret
= consumer_send_relayd_socket(consumer_fd
, sock
,
1963 consumer
, relayd_uri
->stype
);
1965 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
1972 * Close socket which was dup on the consumer side. The session daemon does
1973 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1978 (void) relayd_close(sock
);
1979 lttcomm_destroy_sock(sock
);
1986 * Send both relayd sockets to a specific consumer and domain. This is a
1987 * helper function to facilitate sending the information to the consumer for a
1990 static int send_sockets_relayd_consumer(int domain
,
1991 struct ltt_session
*session
, struct consumer_output
*consumer
, int fd
)
1995 /* Sending control relayd socket. */
1996 ret
= send_socket_relayd_consumer(domain
, session
,
1997 &consumer
->dst
.net
.control
, consumer
, fd
);
1998 if (ret
!= LTTCOMM_OK
) {
2002 /* Sending data relayd socket. */
2003 ret
= send_socket_relayd_consumer(domain
, session
,
2004 &consumer
->dst
.net
.data
, consumer
, fd
);
2005 if (ret
!= LTTCOMM_OK
) {
2014 * Setup relayd connections for a tracing session. First creates the socket to
2015 * the relayd and send them to the right domain consumer. Consumer type MUST be
2018 static int setup_relayd(struct ltt_session
*session
)
2020 int ret
= LTTCOMM_OK
;
2021 struct ltt_ust_session
*usess
;
2022 struct ltt_kernel_session
*ksess
;
2026 usess
= session
->ust_session
;
2027 ksess
= session
->kernel_session
;
2029 DBG2("Setting relayd for session %s", session
->name
);
2031 if (usess
&& usess
->consumer
->sock
== -1 &&
2032 usess
->consumer
->type
== CONSUMER_DST_NET
&&
2033 usess
->consumer
->enabled
) {
2034 /* Setup relayd for 64 bits consumer */
2035 if (ust_consumerd64_fd
>= 0) {
2036 send_sockets_relayd_consumer(LTTNG_DOMAIN_UST
, session
,
2037 usess
->consumer
, ust_consumerd64_fd
);
2038 if (ret
!= LTTCOMM_OK
) {
2043 /* Setup relayd for 32 bits consumer */
2044 if (ust_consumerd32_fd
>= 0) {
2045 send_sockets_relayd_consumer(LTTNG_DOMAIN_UST
, session
,
2046 usess
->consumer
, ust_consumerd32_fd
);
2047 if (ret
!= LTTCOMM_OK
) {
2051 } else if (ksess
&& ksess
->consumer
->sock
== -1 &&
2052 ksess
->consumer
->type
== CONSUMER_DST_NET
&&
2053 ksess
->consumer
->enabled
) {
2054 send_sockets_relayd_consumer(LTTNG_DOMAIN_KERNEL
, session
,
2055 ksess
->consumer
, ksess
->consumer_fd
);
2056 if (ret
!= LTTCOMM_OK
) {
2066 * Copy consumer output from the tracing session to the domain session. The
2067 * function also applies the right modification on a per domain basis for the
2068 * trace files destination directory.
2070 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2073 const char *dir_name
;
2074 struct consumer_output
*consumer
;
2077 case LTTNG_DOMAIN_KERNEL
:
2078 DBG3("Copying tracing session consumer output in kernel session");
2079 session
->kernel_session
->consumer
=
2080 consumer_copy_output(session
->consumer
);
2081 /* Ease our life a bit for the next part */
2082 consumer
= session
->kernel_session
->consumer
;
2083 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2085 case LTTNG_DOMAIN_UST
:
2086 DBG3("Copying tracing session consumer output in UST session");
2087 session
->ust_session
->consumer
=
2088 consumer_copy_output(session
->consumer
);
2089 /* Ease our life a bit for the next part */
2090 consumer
= session
->ust_session
->consumer
;
2091 dir_name
= DEFAULT_UST_TRACE_DIR
;
2094 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2098 /* Append correct directory to subdir */
2099 strncat(consumer
->subdir
, dir_name
, sizeof(consumer
->subdir
));
2100 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2102 /* Add default trace directory name */
2103 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
2104 strncat(consumer
->dst
.trace_path
, dir_name
,
2105 sizeof(consumer
->dst
.trace_path
));
2115 * Create an UST session and add it to the session ust list.
2117 static int create_ust_session(struct ltt_session
*session
,
2118 struct lttng_domain
*domain
)
2121 struct ltt_ust_session
*lus
= NULL
;
2124 assert(session
->consumer
);
2126 switch (domain
->type
) {
2127 case LTTNG_DOMAIN_UST
:
2130 ERR("Unknown UST domain on create session %d", domain
->type
);
2131 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2135 DBG("Creating UST session");
2137 lus
= trace_ust_create_session(session
->path
, session
->id
, domain
);
2139 ret
= LTTCOMM_UST_SESS_FAIL
;
2143 if (session
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2144 ret
= run_as_mkdir_recursive(lus
->pathname
, S_IRWXU
| S_IRWXG
,
2145 session
->uid
, session
->gid
);
2147 if (ret
!= -EEXIST
) {
2148 ERR("Trace directory creation error");
2149 ret
= LTTCOMM_UST_SESS_FAIL
;
2155 lus
->uid
= session
->uid
;
2156 lus
->gid
= session
->gid
;
2157 session
->ust_session
= lus
;
2159 /* Copy session output to the newly created UST session */
2160 ret
= copy_session_consumer(domain
->type
, session
);
2161 if (ret
!= LTTCOMM_OK
) {
2169 session
->ust_session
= NULL
;
2174 * Create a kernel tracer session then create the default channel.
2176 static int create_kernel_session(struct ltt_session
*session
)
2180 DBG("Creating kernel session");
2182 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2184 ret
= LTTCOMM_KERN_SESS_FAIL
;
2188 /* Set kernel consumer socket fd */
2189 if (kconsumer_data
.cmd_sock
>= 0) {
2190 session
->kernel_session
->consumer_fd
= kconsumer_data
.cmd_sock
;
2193 /* Copy session output to the newly created Kernel session */
2194 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2195 if (ret
!= LTTCOMM_OK
) {
2199 /* Create directory(ies) on local filesystem. */
2200 if (session
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2201 ret
= run_as_mkdir_recursive(
2202 session
->kernel_session
->consumer
->dst
.trace_path
,
2203 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2205 if (ret
!= -EEXIST
) {
2206 ERR("Trace directory creation error");
2212 session
->kernel_session
->uid
= session
->uid
;
2213 session
->kernel_session
->gid
= session
->gid
;
2218 trace_kernel_destroy_session(session
->kernel_session
);
2219 session
->kernel_session
= NULL
;
2224 * Check if the UID or GID match the session. Root user has access to all
2227 static int session_access_ok(struct ltt_session
*session
, uid_t uid
, gid_t gid
)
2229 if (uid
!= session
->uid
&& gid
!= session
->gid
&& uid
!= 0) {
2237 * Count number of session permitted by uid/gid.
2239 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2242 struct ltt_session
*session
;
2244 DBG("Counting number of available session for UID %d GID %d",
2246 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2248 * Only list the sessions the user can control.
2250 if (!session_access_ok(session
, uid
, gid
)) {
2259 * Using the session list, filled a lttng_session array to send back to the
2260 * client for session listing.
2262 * The session list lock MUST be acquired before calling this function. Use
2263 * session_lock_list() and session_unlock_list().
2265 static void list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2269 struct ltt_session
*session
;
2271 DBG("Getting all available session for UID %d GID %d",
2274 * Iterate over session list and append data after the control struct in
2277 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2279 * Only list the sessions the user can control.
2281 if (!session_access_ok(session
, uid
, gid
)) {
2284 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
2285 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
2286 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2287 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2288 sessions
[i
].enabled
= session
->enabled
;
2294 * Fill lttng_channel array of all channels.
2296 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
2297 struct lttng_channel
*channels
)
2300 struct ltt_kernel_channel
*kchan
;
2302 DBG("Listing channels for session %s", session
->name
);
2305 case LTTNG_DOMAIN_KERNEL
:
2306 /* Kernel channels */
2307 if (session
->kernel_session
!= NULL
) {
2308 cds_list_for_each_entry(kchan
,
2309 &session
->kernel_session
->channel_list
.head
, list
) {
2310 /* Copy lttng_channel struct to array */
2311 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
2312 channels
[i
].enabled
= kchan
->enabled
;
2317 case LTTNG_DOMAIN_UST
:
2319 struct lttng_ht_iter iter
;
2320 struct ltt_ust_channel
*uchan
;
2322 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
2323 &iter
.iter
, uchan
, node
.node
) {
2324 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
2325 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
2326 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2327 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2328 channels
[i
].attr
.switch_timer_interval
=
2329 uchan
->attr
.switch_timer_interval
;
2330 channels
[i
].attr
.read_timer_interval
=
2331 uchan
->attr
.read_timer_interval
;
2332 channels
[i
].enabled
= uchan
->enabled
;
2333 switch (uchan
->attr
.output
) {
2334 case LTTNG_UST_MMAP
:
2336 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
2349 * Create a list of ust global domain events.
2351 static int list_lttng_ust_global_events(char *channel_name
,
2352 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
2355 unsigned int nb_event
= 0;
2356 struct lttng_ht_iter iter
;
2357 struct lttng_ht_node_str
*node
;
2358 struct ltt_ust_channel
*uchan
;
2359 struct ltt_ust_event
*uevent
;
2360 struct lttng_event
*tmp
;
2362 DBG("Listing UST global events for channel %s", channel_name
);
2366 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
2367 node
= lttng_ht_iter_get_node_str(&iter
);
2369 ret
= -LTTCOMM_UST_CHAN_NOT_FOUND
;
2373 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
2375 nb_event
+= lttng_ht_get_count(uchan
->events
);
2377 if (nb_event
== 0) {
2382 DBG3("Listing UST global %d events", nb_event
);
2384 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
2386 ret
= -LTTCOMM_FATAL
;
2390 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
2391 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
2392 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
2393 tmp
[i
].enabled
= uevent
->enabled
;
2394 switch (uevent
->attr
.instrumentation
) {
2395 case LTTNG_UST_TRACEPOINT
:
2396 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2398 case LTTNG_UST_PROBE
:
2399 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
2401 case LTTNG_UST_FUNCTION
:
2402 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
2405 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
2406 switch (uevent
->attr
.loglevel_type
) {
2407 case LTTNG_UST_LOGLEVEL_ALL
:
2408 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2410 case LTTNG_UST_LOGLEVEL_RANGE
:
2411 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
2413 case LTTNG_UST_LOGLEVEL_SINGLE
:
2414 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
2417 if (uevent
->filter
) {
2432 * Fill lttng_event array of all kernel events in the channel.
2434 static int list_lttng_kernel_events(char *channel_name
,
2435 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
2438 unsigned int nb_event
;
2439 struct ltt_kernel_event
*event
;
2440 struct ltt_kernel_channel
*kchan
;
2442 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
2443 if (kchan
== NULL
) {
2444 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2448 nb_event
= kchan
->event_count
;
2450 DBG("Listing events for channel %s", kchan
->channel
->name
);
2452 if (nb_event
== 0) {
2457 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
2458 if (*events
== NULL
) {
2459 ret
= LTTCOMM_FATAL
;
2463 /* Kernel channels */
2464 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
2465 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
2466 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
2467 (*events
)[i
].enabled
= event
->enabled
;
2468 switch (event
->event
->instrumentation
) {
2469 case LTTNG_KERNEL_TRACEPOINT
:
2470 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2472 case LTTNG_KERNEL_KPROBE
:
2473 case LTTNG_KERNEL_KRETPROBE
:
2474 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
2475 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
2476 sizeof(struct lttng_kernel_kprobe
));
2478 case LTTNG_KERNEL_FUNCTION
:
2479 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
2480 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
2481 sizeof(struct lttng_kernel_function
));
2483 case LTTNG_KERNEL_NOOP
:
2484 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
2486 case LTTNG_KERNEL_SYSCALL
:
2487 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
2489 case LTTNG_KERNEL_ALL
:
2503 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
2505 static int cmd_disable_channel(struct ltt_session
*session
,
2506 int domain
, char *channel_name
)
2509 struct ltt_ust_session
*usess
;
2511 usess
= session
->ust_session
;
2514 case LTTNG_DOMAIN_KERNEL
:
2516 ret
= channel_kernel_disable(session
->kernel_session
,
2518 if (ret
!= LTTCOMM_OK
) {
2522 kernel_wait_quiescent(kernel_tracer_fd
);
2525 case LTTNG_DOMAIN_UST
:
2527 struct ltt_ust_channel
*uchan
;
2528 struct lttng_ht
*chan_ht
;
2530 chan_ht
= usess
->domain_global
.channels
;
2532 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
2533 if (uchan
== NULL
) {
2534 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2538 ret
= channel_ust_disable(usess
, domain
, uchan
);
2539 if (ret
!= LTTCOMM_OK
) {
2545 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2546 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2547 case LTTNG_DOMAIN_UST_PID
:
2550 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2561 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
2563 static int cmd_enable_channel(struct ltt_session
*session
,
2564 int domain
, struct lttng_channel
*attr
)
2567 struct ltt_ust_session
*usess
= session
->ust_session
;
2568 struct lttng_ht
*chan_ht
;
2570 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
2573 case LTTNG_DOMAIN_KERNEL
:
2575 struct ltt_kernel_channel
*kchan
;
2577 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
2578 session
->kernel_session
);
2579 if (kchan
== NULL
) {
2580 ret
= channel_kernel_create(session
->kernel_session
,
2581 attr
, kernel_poll_pipe
[1]);
2583 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
2586 if (ret
!= LTTCOMM_OK
) {
2590 kernel_wait_quiescent(kernel_tracer_fd
);
2593 case LTTNG_DOMAIN_UST
:
2595 struct ltt_ust_channel
*uchan
;
2597 chan_ht
= usess
->domain_global
.channels
;
2599 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
2600 if (uchan
== NULL
) {
2601 ret
= channel_ust_create(usess
, domain
, attr
);
2603 ret
= channel_ust_enable(usess
, domain
, uchan
);
2608 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2609 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2610 case LTTNG_DOMAIN_UST_PID
:
2613 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2622 * Command LTTNG_DISABLE_EVENT processed by the client thread.
2624 static int cmd_disable_event(struct ltt_session
*session
, int domain
,
2625 char *channel_name
, char *event_name
)
2630 case LTTNG_DOMAIN_KERNEL
:
2632 struct ltt_kernel_channel
*kchan
;
2633 struct ltt_kernel_session
*ksess
;
2635 ksess
= session
->kernel_session
;
2637 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
2638 if (kchan
== NULL
) {
2639 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2643 ret
= event_kernel_disable_tracepoint(ksess
, kchan
, event_name
);
2644 if (ret
!= LTTCOMM_OK
) {
2648 kernel_wait_quiescent(kernel_tracer_fd
);
2651 case LTTNG_DOMAIN_UST
:
2653 struct ltt_ust_channel
*uchan
;
2654 struct ltt_ust_session
*usess
;
2656 usess
= session
->ust_session
;
2658 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2660 if (uchan
== NULL
) {
2661 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2665 ret
= event_ust_disable_tracepoint(usess
, domain
, uchan
, event_name
);
2666 if (ret
!= LTTCOMM_OK
) {
2670 DBG3("Disable UST event %s in channel %s completed", event_name
,
2675 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2676 case LTTNG_DOMAIN_UST_PID
:
2677 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2691 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
2693 static int cmd_disable_event_all(struct ltt_session
*session
, int domain
,
2699 case LTTNG_DOMAIN_KERNEL
:
2701 struct ltt_kernel_session
*ksess
;
2702 struct ltt_kernel_channel
*kchan
;
2704 ksess
= session
->kernel_session
;
2706 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
2707 if (kchan
== NULL
) {
2708 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2712 ret
= event_kernel_disable_all(ksess
, kchan
);
2713 if (ret
!= LTTCOMM_OK
) {
2717 kernel_wait_quiescent(kernel_tracer_fd
);
2720 case LTTNG_DOMAIN_UST
:
2722 struct ltt_ust_session
*usess
;
2723 struct ltt_ust_channel
*uchan
;
2725 usess
= session
->ust_session
;
2727 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2729 if (uchan
== NULL
) {
2730 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2734 ret
= event_ust_disable_all_tracepoints(usess
, domain
, uchan
);
2739 DBG3("Disable all UST events in channel %s completed", channel_name
);
2744 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2745 case LTTNG_DOMAIN_UST_PID
:
2746 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2760 * Command LTTNG_ADD_CONTEXT processed by the client thread.
2762 static int cmd_add_context(struct ltt_session
*session
, int domain
,
2763 char *channel_name
, char *event_name
, struct lttng_event_context
*ctx
)
2768 case LTTNG_DOMAIN_KERNEL
:
2769 /* Add kernel context to kernel tracer */
2770 ret
= context_kernel_add(session
->kernel_session
, ctx
,
2771 event_name
, channel_name
);
2772 if (ret
!= LTTCOMM_OK
) {
2776 case LTTNG_DOMAIN_UST
:
2778 struct ltt_ust_session
*usess
= session
->ust_session
;
2780 ret
= context_ust_add(usess
, domain
, ctx
, event_name
, channel_name
);
2781 if (ret
!= LTTCOMM_OK
) {
2787 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2788 case LTTNG_DOMAIN_UST_PID
:
2789 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2803 * Command LTTNG_SET_FILTER processed by the client thread.
2805 static int cmd_set_filter(struct ltt_session
*session
, int domain
,
2806 char *channel_name
, char *event_name
,
2807 struct lttng_filter_bytecode
*bytecode
)
2812 case LTTNG_DOMAIN_KERNEL
:
2813 ret
= LTTCOMM_FATAL
;
2815 case LTTNG_DOMAIN_UST
:
2817 struct ltt_ust_session
*usess
= session
->ust_session
;
2819 ret
= filter_ust_set(usess
, domain
, bytecode
, event_name
, channel_name
);
2820 if (ret
!= LTTCOMM_OK
) {
2826 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2827 case LTTNG_DOMAIN_UST_PID
:
2828 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2843 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2845 static int cmd_enable_event(struct ltt_session
*session
, int domain
,
2846 char *channel_name
, struct lttng_event
*event
)
2849 struct lttng_channel
*attr
;
2850 struct ltt_ust_session
*usess
= session
->ust_session
;
2853 case LTTNG_DOMAIN_KERNEL
:
2855 struct ltt_kernel_channel
*kchan
;
2857 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2858 session
->kernel_session
);
2859 if (kchan
== NULL
) {
2860 attr
= channel_new_default_attr(domain
);
2862 ret
= LTTCOMM_FATAL
;
2865 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
2867 /* This call will notify the kernel thread */
2868 ret
= channel_kernel_create(session
->kernel_session
,
2869 attr
, kernel_poll_pipe
[1]);
2870 if (ret
!= LTTCOMM_OK
) {
2877 /* Get the newly created kernel channel pointer */
2878 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2879 session
->kernel_session
);
2880 if (kchan
== NULL
) {
2881 /* This sould not happen... */
2882 ret
= LTTCOMM_FATAL
;
2886 ret
= event_kernel_enable_tracepoint(session
->kernel_session
, kchan
,
2888 if (ret
!= LTTCOMM_OK
) {
2892 kernel_wait_quiescent(kernel_tracer_fd
);
2895 case LTTNG_DOMAIN_UST
:
2897 struct lttng_channel
*attr
;
2898 struct ltt_ust_channel
*uchan
;
2900 /* Get channel from global UST domain */
2901 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2903 if (uchan
== NULL
) {
2904 /* Create default channel */
2905 attr
= channel_new_default_attr(domain
);
2907 ret
= LTTCOMM_FATAL
;
2910 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
2911 attr
->name
[NAME_MAX
- 1] = '\0';
2913 ret
= channel_ust_create(usess
, domain
, attr
);
2914 if (ret
!= LTTCOMM_OK
) {
2920 /* Get the newly created channel reference back */
2921 uchan
= trace_ust_find_channel_by_name(
2922 usess
->domain_global
.channels
, channel_name
);
2923 if (uchan
== NULL
) {
2924 /* Something is really wrong */
2925 ret
= LTTCOMM_FATAL
;
2930 /* At this point, the session and channel exist on the tracer */
2931 ret
= event_ust_enable_tracepoint(usess
, domain
, uchan
, event
);
2932 if (ret
!= LTTCOMM_OK
) {
2938 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2939 case LTTNG_DOMAIN_UST_PID
:
2940 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2954 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
2956 static int cmd_enable_event_all(struct ltt_session
*session
, int domain
,
2957 char *channel_name
, int event_type
)
2960 struct ltt_kernel_channel
*kchan
;
2963 case LTTNG_DOMAIN_KERNEL
:
2964 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2965 session
->kernel_session
);
2966 if (kchan
== NULL
) {
2967 /* This call will notify the kernel thread */
2968 ret
= channel_kernel_create(session
->kernel_session
, NULL
,
2969 kernel_poll_pipe
[1]);
2970 if (ret
!= LTTCOMM_OK
) {
2974 /* Get the newly created kernel channel pointer */
2975 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2976 session
->kernel_session
);
2977 if (kchan
== NULL
) {
2978 /* This sould not happen... */
2979 ret
= LTTCOMM_FATAL
;
2985 switch (event_type
) {
2986 case LTTNG_EVENT_SYSCALL
:
2987 ret
= event_kernel_enable_all_syscalls(session
->kernel_session
,
2988 kchan
, kernel_tracer_fd
);
2990 case LTTNG_EVENT_TRACEPOINT
:
2992 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
2993 * events already registered to the channel.
2995 ret
= event_kernel_enable_all_tracepoints(session
->kernel_session
,
2996 kchan
, kernel_tracer_fd
);
2998 case LTTNG_EVENT_ALL
:
2999 /* Enable syscalls and tracepoints */
3000 ret
= event_kernel_enable_all(session
->kernel_session
,
3001 kchan
, kernel_tracer_fd
);
3004 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
3008 /* Manage return value */
3009 if (ret
!= LTTCOMM_OK
) {
3013 kernel_wait_quiescent(kernel_tracer_fd
);
3015 case LTTNG_DOMAIN_UST
:
3017 struct lttng_channel
*attr
;
3018 struct ltt_ust_channel
*uchan
;
3019 struct ltt_ust_session
*usess
= session
->ust_session
;
3021 /* Get channel from global UST domain */
3022 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
3024 if (uchan
== NULL
) {
3025 /* Create default channel */
3026 attr
= channel_new_default_attr(domain
);
3028 ret
= LTTCOMM_FATAL
;
3031 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
3032 attr
->name
[NAME_MAX
- 1] = '\0';
3034 /* Use the internal command enable channel */
3035 ret
= channel_ust_create(usess
, domain
, attr
);
3036 if (ret
!= LTTCOMM_OK
) {
3042 /* Get the newly created channel reference back */
3043 uchan
= trace_ust_find_channel_by_name(
3044 usess
->domain_global
.channels
, channel_name
);
3045 if (uchan
== NULL
) {
3046 /* Something is really wrong */
3047 ret
= LTTCOMM_FATAL
;
3052 /* At this point, the session and channel exist on the tracer */
3054 switch (event_type
) {
3055 case LTTNG_EVENT_ALL
:
3056 case LTTNG_EVENT_TRACEPOINT
:
3057 ret
= event_ust_enable_all_tracepoints(usess
, domain
, uchan
);
3058 if (ret
!= LTTCOMM_OK
) {
3063 ret
= LTTCOMM_UST_ENABLE_FAIL
;
3067 /* Manage return value */
3068 if (ret
!= LTTCOMM_OK
) {
3075 case LTTNG_DOMAIN_UST_EXEC_NAME
:
3076 case LTTNG_DOMAIN_UST_PID
:
3077 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
3091 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
3093 static ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
3096 ssize_t nb_events
= 0;
3099 case LTTNG_DOMAIN_KERNEL
:
3100 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
3101 if (nb_events
< 0) {
3102 ret
= LTTCOMM_KERN_LIST_FAIL
;
3106 case LTTNG_DOMAIN_UST
:
3107 nb_events
= ust_app_list_events(events
);
3108 if (nb_events
< 0) {
3109 ret
= LTTCOMM_UST_LIST_FAIL
;
3121 /* Return negative value to differentiate return code */
3126 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
3128 static ssize_t
cmd_list_tracepoint_fields(int domain
,
3129 struct lttng_event_field
**fields
)
3132 ssize_t nb_fields
= 0;
3135 case LTTNG_DOMAIN_UST
:
3136 nb_fields
= ust_app_list_event_fields(fields
);
3137 if (nb_fields
< 0) {
3138 ret
= LTTCOMM_UST_LIST_FAIL
;
3142 case LTTNG_DOMAIN_KERNEL
:
3143 default: /* fall-through */
3151 /* Return negative value to differentiate return code */
3156 * Command LTTNG_START_TRACE processed by the client thread.
3158 static int cmd_start_trace(struct ltt_session
*session
)
3161 struct ltt_kernel_session
*ksession
;
3162 struct ltt_ust_session
*usess
;
3163 struct ltt_kernel_channel
*kchan
;
3165 /* Ease our life a bit ;) */
3166 ksession
= session
->kernel_session
;
3167 usess
= session
->ust_session
;
3169 if (session
->enabled
) {
3170 /* Already started. */
3171 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
3175 session
->enabled
= 1;
3177 ret
= setup_relayd(session
);
3178 if (ret
!= LTTCOMM_OK
) {
3179 ERR("Error setting up relayd for session %s", session
->name
);
3183 /* Kernel tracing */
3184 if (ksession
!= NULL
) {
3185 /* Open kernel metadata */
3186 if (ksession
->metadata
== NULL
) {
3187 ret
= kernel_open_metadata(ksession
,
3188 ksession
->consumer
->dst
.trace_path
);
3190 ret
= LTTCOMM_KERN_META_FAIL
;
3195 /* Open kernel metadata stream */
3196 if (ksession
->metadata_stream_fd
< 0) {
3197 ret
= kernel_open_metadata_stream(ksession
);
3199 ERR("Kernel create metadata stream failed");
3200 ret
= LTTCOMM_KERN_STREAM_FAIL
;
3205 /* For each channel */
3206 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
3207 if (kchan
->stream_count
== 0) {
3208 ret
= kernel_open_channel_stream(kchan
);
3210 ret
= LTTCOMM_KERN_STREAM_FAIL
;
3213 /* Update the stream global counter */
3214 ksession
->stream_count_global
+= ret
;
3218 /* Setup kernel consumer socket and send fds to it */
3219 ret
= init_kernel_tracing(ksession
);
3221 ret
= LTTCOMM_KERN_START_FAIL
;
3225 /* This start the kernel tracing */
3226 ret
= kernel_start_session(ksession
);
3228 ret
= LTTCOMM_KERN_START_FAIL
;
3232 /* Quiescent wait after starting trace */
3233 kernel_wait_quiescent(kernel_tracer_fd
);
3236 /* Flag session that trace should start automatically */
3238 usess
->start_trace
= 1;
3240 ret
= ust_app_start_trace_all(usess
);
3242 ret
= LTTCOMM_UST_START_FAIL
;
3254 * Command LTTNG_STOP_TRACE processed by the client thread.
3256 static int cmd_stop_trace(struct ltt_session
*session
)
3259 struct ltt_kernel_channel
*kchan
;
3260 struct ltt_kernel_session
*ksession
;
3261 struct ltt_ust_session
*usess
;
3264 ksession
= session
->kernel_session
;
3265 usess
= session
->ust_session
;
3267 if (!session
->enabled
) {
3268 ret
= LTTCOMM_TRACE_ALREADY_STOPPED
;
3272 session
->enabled
= 0;
3275 if (ksession
!= NULL
) {
3276 DBG("Stop kernel tracing");
3278 /* Flush metadata if exist */
3279 if (ksession
->metadata_stream_fd
>= 0) {
3280 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
3282 ERR("Kernel metadata flush failed");
3286 /* Flush all buffers before stopping */
3287 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
3288 ret
= kernel_flush_buffer(kchan
);
3290 ERR("Kernel flush buffer error");
3294 ret
= kernel_stop_session(ksession
);
3296 ret
= LTTCOMM_KERN_STOP_FAIL
;
3300 kernel_wait_quiescent(kernel_tracer_fd
);
3304 usess
->start_trace
= 0;
3306 ret
= ust_app_stop_trace_all(usess
);
3308 ret
= LTTCOMM_UST_STOP_FAIL
;
3320 * Command LTTNG_CREATE_SESSION_URI processed by the client thread.
3322 static int cmd_create_session_uri(char *name
, struct lttng_uri
*ctrl_uri
,
3323 struct lttng_uri
*data_uri
, unsigned int enable_consumer
,
3324 lttng_sock_cred
*creds
)
3328 struct ltt_session
*session
;
3329 struct consumer_output
*consumer
;
3331 /* Verify if the session already exist */
3332 session
= session_find_by_name(name
);
3333 if (session
!= NULL
) {
3334 ret
= LTTCOMM_EXIST_SESS
;
3338 /* TODO: validate URIs */
3340 /* Create default consumer output */
3341 consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
3342 if (consumer
== NULL
) {
3343 ret
= LTTCOMM_FATAL
;
3346 strncpy(consumer
->subdir
, ctrl_uri
->subdir
, sizeof(consumer
->subdir
));
3347 DBG2("Consumer subdir set to %s", consumer
->subdir
);
3349 switch (ctrl_uri
->dtype
) {
3350 case LTTNG_DST_IPV4
:
3351 case LTTNG_DST_IPV6
:
3352 /* Set control URI into consumer output object */
3353 ret
= consumer_set_network_uri(consumer
, ctrl_uri
);
3355 ret
= LTTCOMM_FATAL
;
3359 /* Set data URI into consumer output object */
3360 ret
= consumer_set_network_uri(consumer
, data_uri
);
3362 ret
= LTTCOMM_FATAL
;
3366 /* Empty path since the session is network */
3369 case LTTNG_DST_PATH
:
3370 /* Very volatile pointer. Only used for the create session. */
3371 path
= ctrl_uri
->dst
.path
;
3372 strncpy(consumer
->dst
.trace_path
, path
,
3373 sizeof(consumer
->dst
.trace_path
));
3377 /* Set if the consumer is enabled or not */
3378 consumer
->enabled
= enable_consumer
;
3380 ret
= session_create(name
, path
, LTTNG_SOCK_GET_UID_CRED(creds
),
3381 LTTNG_SOCK_GET_GID_CRED(creds
));
3382 if (ret
!= LTTCOMM_OK
) {
3383 goto consumer_error
;
3386 /* Get the newly created session pointer back */
3387 session
= session_find_by_name(name
);
3390 /* Assign consumer to session */
3391 session
->consumer
= consumer
;
3396 consumer_destroy_output(consumer
);
3402 * Command LTTNG_CREATE_SESSION processed by the client thread.
3404 static int cmd_create_session(char *name
, char *path
, lttng_sock_cred
*creds
)
3407 struct lttng_uri uri
;
3409 /* Zeroed temporary URI */
3410 memset(&uri
, 0, sizeof(uri
));
3412 uri
.dtype
= LTTNG_DST_PATH
;
3413 uri
.utype
= LTTNG_URI_DST
;
3414 strncpy(uri
.dst
.path
, path
, sizeof(uri
.dst
.path
));
3416 /* TODO: Strip date-time from path and put it in uri's subdir */
3418 ret
= cmd_create_session_uri(name
, &uri
, NULL
, 1, creds
);
3419 if (ret
!= LTTCOMM_OK
) {
3428 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3430 static int cmd_destroy_session(struct ltt_session
*session
, char *name
)
3434 /* Clean kernel session teardown */
3435 teardown_kernel_session(session
);
3436 /* UST session teardown */
3437 teardown_ust_session(session
);
3440 * Must notify the kernel thread here to update it's poll setin order
3441 * to remove the channel(s)' fd just destroyed.
3443 ret
= notify_thread_pipe(kernel_poll_pipe
[1]);
3445 PERROR("write kernel poll pipe");
3448 ret
= session_destroy(session
);
3454 * Command LTTNG_CALIBRATE processed by the client thread.
3456 static int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
3461 case LTTNG_DOMAIN_KERNEL
:
3463 struct lttng_kernel_calibrate kcalibrate
;
3465 kcalibrate
.type
= calibrate
->type
;
3466 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
3468 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
3473 case LTTNG_DOMAIN_UST
:
3475 struct lttng_ust_calibrate ucalibrate
;
3477 ucalibrate
.type
= calibrate
->type
;
3478 ret
= ust_app_calibrate_glb(&ucalibrate
);
3480 ret
= LTTCOMM_UST_CALIBRATE_FAIL
;
3497 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3499 static int cmd_register_consumer(struct ltt_session
*session
, int domain
,
3505 case LTTNG_DOMAIN_KERNEL
:
3506 /* Can't register a consumer if there is already one */
3507 if (session
->kernel_session
->consumer_fds_sent
!= 0) {
3508 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
3512 sock
= lttcomm_connect_unix_sock(sock_path
);
3514 ret
= LTTCOMM_CONNECT_FAIL
;
3518 session
->kernel_session
->consumer_fd
= sock
;
3521 /* TODO: Userspace tracing */
3533 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3535 static ssize_t
cmd_list_domains(struct ltt_session
*session
,
3536 struct lttng_domain
**domains
)
3541 if (session
->kernel_session
!= NULL
) {
3542 DBG3("Listing domains found kernel domain");
3546 if (session
->ust_session
!= NULL
) {
3547 DBG3("Listing domains found UST global domain");
3551 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3552 if (*domains
== NULL
) {
3553 ret
= -LTTCOMM_FATAL
;
3557 if (session
->kernel_session
!= NULL
) {
3558 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3562 if (session
->ust_session
!= NULL
) {
3563 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3574 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3576 static ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
3577 struct lttng_channel
**channels
)
3580 ssize_t nb_chan
= 0;
3583 case LTTNG_DOMAIN_KERNEL
:
3584 if (session
->kernel_session
!= NULL
) {
3585 nb_chan
= session
->kernel_session
->channel_count
;
3587 DBG3("Number of kernel channels %zd", nb_chan
);
3589 case LTTNG_DOMAIN_UST
:
3590 if (session
->ust_session
!= NULL
) {
3591 nb_chan
= lttng_ht_get_count(
3592 session
->ust_session
->domain_global
.channels
);
3594 DBG3("Number of UST global channels %zd", nb_chan
);
3603 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
3604 if (*channels
== NULL
) {
3605 ret
= -LTTCOMM_FATAL
;
3609 list_lttng_channels(domain
, session
, *channels
);
3621 * Command LTTNG_LIST_EVENTS processed by the client thread.
3623 static ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
3624 char *channel_name
, struct lttng_event
**events
)
3627 ssize_t nb_event
= 0;
3630 case LTTNG_DOMAIN_KERNEL
:
3631 if (session
->kernel_session
!= NULL
) {
3632 nb_event
= list_lttng_kernel_events(channel_name
,
3633 session
->kernel_session
, events
);
3636 case LTTNG_DOMAIN_UST
:
3638 if (session
->ust_session
!= NULL
) {
3639 nb_event
= list_lttng_ust_global_events(channel_name
,
3640 &session
->ust_session
->domain_global
, events
);
3656 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
3658 static int cmd_set_consumer_uri(int domain
, struct ltt_session
*session
,
3659 struct lttng_uri
*uri
)
3662 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3663 struct ltt_ust_session
*usess
= session
->ust_session
;
3664 struct consumer_output
*consumer
;
3666 /* Can't enable consumer after session started. */
3667 if (session
->enabled
) {
3668 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
3673 case LTTNG_DOMAIN_KERNEL
:
3674 /* Code flow error if we don't have a kernel session here. */
3677 /* Create consumer output if none exists */
3678 consumer
= ksess
->tmp_consumer
;
3679 if (consumer
== NULL
) {
3680 consumer
= consumer_copy_output(ksess
->consumer
);
3681 if (consumer
== NULL
) {
3682 ret
= LTTCOMM_FATAL
;
3685 /* Reassign new pointer */
3686 ksess
->tmp_consumer
= consumer
;
3689 switch (uri
->dtype
) {
3690 case LTTNG_DST_IPV4
:
3691 case LTTNG_DST_IPV6
:
3692 DBG2("Setting network URI for kernel session %s", session
->name
);
3694 /* Set URI into consumer output object */
3695 ret
= consumer_set_network_uri(consumer
, uri
);
3697 ret
= LTTCOMM_FATAL
;
3701 /* On a new subdir, reappend the default trace dir. */
3702 if (strlen(uri
->subdir
) != 0) {
3703 strncat(consumer
->subdir
, DEFAULT_KERNEL_TRACE_DIR
,
3704 sizeof(consumer
->subdir
));
3707 ret
= send_socket_relayd_consumer(domain
, session
, uri
, consumer
,
3708 ksess
->consumer_fd
);
3709 if (ret
!= LTTCOMM_OK
) {
3713 case LTTNG_DST_PATH
:
3714 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
3715 memset(consumer
->dst
.trace_path
, 0,
3716 sizeof(consumer
->dst
.trace_path
));
3717 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
3718 sizeof(consumer
->dst
.trace_path
));
3719 /* Append default kernel trace dir */
3720 strncat(consumer
->dst
.trace_path
, DEFAULT_KERNEL_TRACE_DIR
,
3721 sizeof(consumer
->dst
.trace_path
));
3727 case LTTNG_DOMAIN_UST
:
3728 /* Code flow error if we don't have a kernel session here. */
3731 /* Create consumer output if none exists */
3732 consumer
= usess
->tmp_consumer
;
3733 if (consumer
== NULL
) {
3734 consumer
= consumer_copy_output(usess
->consumer
);
3735 if (consumer
== NULL
) {
3736 ret
= LTTCOMM_FATAL
;
3739 /* Reassign new pointer */
3740 usess
->tmp_consumer
= consumer
;
3743 switch (uri
->dtype
) {
3744 case LTTNG_DST_IPV4
:
3745 case LTTNG_DST_IPV6
:
3747 DBG2("Setting network URI for UST session %s", session
->name
);
3749 /* Set URI into consumer object */
3750 ret
= consumer_set_network_uri(consumer
, uri
);
3752 ret
= LTTCOMM_FATAL
;
3756 /* On a new subdir, reappend the default trace dir. */
3757 if (strlen(uri
->subdir
) != 0) {
3758 strncat(consumer
->subdir
, DEFAULT_UST_TRACE_DIR
,
3759 sizeof(consumer
->subdir
));
3762 if (ust_consumerd64_fd
>= 0) {
3763 ret
= send_socket_relayd_consumer(domain
, session
, uri
,
3764 consumer
, ust_consumerd64_fd
);
3765 if (ret
!= LTTCOMM_OK
) {
3770 if (ust_consumerd32_fd
>= 0) {
3771 ret
= send_socket_relayd_consumer(domain
, session
, uri
,
3772 consumer
, ust_consumerd32_fd
);
3773 if (ret
!= LTTCOMM_OK
) {
3780 case LTTNG_DST_PATH
:
3781 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
3782 memset(consumer
->dst
.trace_path
, 0,
3783 sizeof(consumer
->dst
.trace_path
));
3784 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
3785 sizeof(consumer
->dst
.trace_path
));
3786 /* Append default UST trace dir */
3787 strncat(consumer
->dst
.trace_path
, DEFAULT_UST_TRACE_DIR
,
3788 sizeof(consumer
->dst
.trace_path
));
3802 * Command LTTNG_DISABLE_CONSUMER processed by the client thread.
3804 static int cmd_disable_consumer(int domain
, struct ltt_session
*session
)
3807 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3808 struct ltt_ust_session
*usess
= session
->ust_session
;
3809 struct consumer_output
*consumer
;
3811 if (session
->enabled
) {
3812 /* Can't disable consumer on an already started session */
3813 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
3818 case LTTNG_DOMAIN_KERNEL
:
3819 /* Code flow error if we don't have a kernel session here. */
3822 DBG("Disabling kernel consumer");
3823 consumer
= ksess
->consumer
;
3826 case LTTNG_DOMAIN_UST
:
3827 /* Code flow error if we don't have a UST session here. */
3830 DBG("Disabling UST consumer");
3831 consumer
= usess
->consumer
;
3835 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
3840 consumer
->enabled
= 0;
3842 /* Success at this point */
3850 * Command LTTNG_ENABLE_CONSUMER processed by the client thread.
3852 static int cmd_enable_consumer(int domain
, struct ltt_session
*session
)
3855 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3856 struct ltt_ust_session
*usess
= session
->ust_session
;
3857 struct consumer_output
*tmp_out
;
3859 /* Can't enable consumer after session started. */
3860 if (session
->enabled
) {
3861 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
3866 case LTTNG_DOMAIN_KERNEL
:
3867 /* Code flow error if we don't have a kernel session here. */
3871 * Check if we have already sent fds to the consumer. In that case,
3872 * the enable-consumer command can't be used because a start trace
3873 * had previously occured.
3875 if (ksess
->consumer_fds_sent
) {
3876 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
3880 tmp_out
= ksess
->tmp_consumer
;
3881 if (tmp_out
== NULL
) {
3882 /* No temp. consumer output exists. Using the current one. */
3883 DBG3("No temporary consumer. Using default");
3888 switch (tmp_out
->type
) {
3889 case CONSUMER_DST_LOCAL
:
3890 DBG2("Consumer output is local. Creating directory(ies)");
3892 /* Create directory(ies) */
3893 ret
= run_as_mkdir_recursive(tmp_out
->dst
.trace_path
,
3894 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
3896 if (ret
!= -EEXIST
) {
3897 ERR("Trace directory creation error");
3898 ret
= LTTCOMM_FATAL
;
3903 case CONSUMER_DST_NET
:
3904 DBG2("Consumer output is network. Validating URIs");
3905 /* Validate if we have both control and data path set. */
3906 if (!tmp_out
->dst
.net
.control_isset
) {
3907 ret
= LTTCOMM_URI_CTRL_MISS
;
3911 if (!tmp_out
->dst
.net
.data_isset
) {
3912 ret
= LTTCOMM_URI_DATA_MISS
;
3916 /* Check established network session state */
3917 if (session
->net_handle
== 0) {
3918 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
3919 ERR("Session network handle is not set on enable-consumer");
3923 /* Append default kernel trace dir to subdir */
3924 strncat(ksess
->consumer
->subdir
, DEFAULT_KERNEL_TRACE_DIR
,
3925 sizeof(ksess
->consumer
->subdir
));
3932 * This is race free for now since the session lock is acquired before
3933 * ending up in this function. No other threads can access this kernel
3934 * session without this lock hence freeing the consumer output object
3937 consumer_destroy_output(ksess
->consumer
);
3938 ksess
->consumer
= tmp_out
;
3939 ksess
->tmp_consumer
= NULL
;
3942 case LTTNG_DOMAIN_UST
:
3943 /* Code flow error if we don't have a UST session here. */
3947 * Check if we have already sent fds to the consumer. In that case,
3948 * the enable-consumer command can't be used because a start trace
3949 * had previously occured.
3951 if (usess
->start_trace
) {
3952 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
3956 tmp_out
= usess
->tmp_consumer
;
3957 if (tmp_out
== NULL
) {
3958 /* No temp. consumer output exists. Using the current one. */
3959 DBG3("No temporary consumer. Using default");
3964 switch (tmp_out
->type
) {
3965 case CONSUMER_DST_LOCAL
:
3966 DBG2("Consumer output is local. Creating directory(ies)");
3968 /* Create directory(ies) */
3969 ret
= run_as_mkdir_recursive(tmp_out
->dst
.trace_path
,
3970 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
3972 if (ret
!= -EEXIST
) {
3973 ERR("Trace directory creation error");
3974 ret
= LTTCOMM_FATAL
;
3979 case CONSUMER_DST_NET
:
3980 DBG2("Consumer output is network. Validating URIs");
3981 /* Validate if we have both control and data path set. */
3982 if (!tmp_out
->dst
.net
.control_isset
) {
3983 ret
= LTTCOMM_URI_CTRL_MISS
;
3987 if (!tmp_out
->dst
.net
.data_isset
) {
3988 ret
= LTTCOMM_URI_DATA_MISS
;
3992 /* Check established network session state */
3993 if (session
->net_handle
== 0) {
3994 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
3995 DBG2("Session network handle is not set on enable-consumer");
3999 if (tmp_out
->net_seq_index
== -1) {
4000 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
4001 DBG2("Network index is not set on the consumer");
4005 /* Append default kernel trace dir to subdir */
4006 strncat(usess
->consumer
->subdir
, DEFAULT_UST_TRACE_DIR
,
4007 sizeof(usess
->consumer
->subdir
));
4014 * This is race free for now since the session lock is acquired before
4015 * ending up in this function. No other threads can access this kernel
4016 * session without this lock hence freeing the consumer output object
4019 consumer_destroy_output(usess
->consumer
);
4020 usess
->consumer
= tmp_out
;
4021 usess
->tmp_consumer
= NULL
;
4026 /* Success at this point */
4034 * Process the command requested by the lttng client within the command
4035 * context structure. This function make sure that the return structure (llm)
4036 * is set and ready for transmission before returning.
4038 * Return any error encountered or 0 for success.
4040 * "sock" is only used for special-case var. len data.
4042 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
4045 int ret
= LTTCOMM_OK
;
4046 int need_tracing_session
= 1;
4049 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
4053 switch (cmd_ctx
->lsm
->cmd_type
) {
4054 case LTTNG_CREATE_SESSION
:
4055 case LTTNG_CREATE_SESSION_URI
:
4056 case LTTNG_DESTROY_SESSION
:
4057 case LTTNG_LIST_SESSIONS
:
4058 case LTTNG_LIST_DOMAINS
:
4059 case LTTNG_START_TRACE
:
4060 case LTTNG_STOP_TRACE
:
4067 if (opt_no_kernel
&& need_domain
4068 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
4070 ret
= LTTCOMM_NEED_ROOT_SESSIOND
;
4072 ret
= LTTCOMM_KERN_NA
;
4078 * Check for command that don't needs to allocate a returned payload. We do
4079 * this here so we don't have to make the call for no payload at each
4082 switch(cmd_ctx
->lsm
->cmd_type
) {
4083 case LTTNG_LIST_SESSIONS
:
4084 case LTTNG_LIST_TRACEPOINTS
:
4085 case LTTNG_LIST_TRACEPOINT_FIELDS
:
4086 case LTTNG_LIST_DOMAINS
:
4087 case LTTNG_LIST_CHANNELS
:
4088 case LTTNG_LIST_EVENTS
:
4091 /* Setup lttng message with no payload */
4092 ret
= setup_lttng_msg(cmd_ctx
, 0);
4094 /* This label does not try to unlock the session */
4095 goto init_setup_error
;
4099 /* Commands that DO NOT need a session. */
4100 switch (cmd_ctx
->lsm
->cmd_type
) {
4101 case LTTNG_CREATE_SESSION
:
4102 case LTTNG_CREATE_SESSION_URI
:
4103 case LTTNG_CALIBRATE
:
4104 case LTTNG_LIST_SESSIONS
:
4105 case LTTNG_LIST_TRACEPOINTS
:
4106 case LTTNG_LIST_TRACEPOINT_FIELDS
:
4107 need_tracing_session
= 0;
4110 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
4112 * We keep the session list lock across _all_ commands
4113 * for now, because the per-session lock does not
4114 * handle teardown properly.
4116 session_lock_list();
4117 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
4118 if (cmd_ctx
->session
== NULL
) {
4119 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
4120 ret
= LTTCOMM_SESS_NOT_FOUND
;
4122 /* If no session name specified */
4123 ret
= LTTCOMM_SELECT_SESS
;
4127 /* Acquire lock for the session */
4128 session_lock(cmd_ctx
->session
);
4137 * Check domain type for specific "pre-action".
4139 switch (cmd_ctx
->lsm
->domain
.type
) {
4140 case LTTNG_DOMAIN_KERNEL
:
4142 ret
= LTTCOMM_NEED_ROOT_SESSIOND
;
4146 /* Kernel tracer check */
4147 if (kernel_tracer_fd
== -1) {
4148 /* Basically, load kernel tracer modules */
4149 ret
= init_kernel_tracer();
4155 /* Consumer is in an ERROR state. Report back to client */
4156 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
4157 ret
= LTTCOMM_NO_KERNCONSUMERD
;
4161 /* Need a session for kernel command */
4162 if (need_tracing_session
) {
4163 if (cmd_ctx
->session
->kernel_session
== NULL
) {
4164 ret
= create_kernel_session(cmd_ctx
->session
);
4166 ret
= LTTCOMM_KERN_SESS_FAIL
;
4171 /* Start the kernel consumer daemon */
4172 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
4173 if (kconsumer_data
.pid
== 0 &&
4174 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
4175 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
4176 ret
= start_consumerd(&kconsumer_data
);
4178 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
4181 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
4183 /* Set consumer fd of the session */
4184 cmd_ctx
->session
->kernel_session
->consumer_fd
=
4185 kconsumer_data
.cmd_sock
;
4187 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
4192 case LTTNG_DOMAIN_UST
:
4194 /* Consumer is in an ERROR state. Report back to client */
4195 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
4196 ret
= LTTCOMM_NO_USTCONSUMERD
;
4200 if (need_tracing_session
) {
4201 if (cmd_ctx
->session
->ust_session
== NULL
) {
4202 ret
= create_ust_session(cmd_ctx
->session
,
4203 &cmd_ctx
->lsm
->domain
);
4204 if (ret
!= LTTCOMM_OK
) {
4209 /* Start the UST consumer daemons */
4211 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
4212 if (consumerd64_bin
[0] != '\0' &&
4213 ustconsumer64_data
.pid
== 0 &&
4214 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
4215 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
4216 ret
= start_consumerd(&ustconsumer64_data
);
4218 ret
= LTTCOMM_UST_CONSUMER64_FAIL
;
4219 ust_consumerd64_fd
= -EINVAL
;
4223 ust_consumerd64_fd
= ustconsumer64_data
.cmd_sock
;
4224 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
4226 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
4229 if (consumerd32_bin
[0] != '\0' &&
4230 ustconsumer32_data
.pid
== 0 &&
4231 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
4232 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
4233 ret
= start_consumerd(&ustconsumer32_data
);
4235 ret
= LTTCOMM_UST_CONSUMER32_FAIL
;
4236 ust_consumerd32_fd
= -EINVAL
;
4240 ust_consumerd32_fd
= ustconsumer32_data
.cmd_sock
;
4241 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
4243 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
4253 /* Validate consumer daemon state when start/stop trace command */
4254 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
4255 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
4256 switch (cmd_ctx
->lsm
->domain
.type
) {
4257 case LTTNG_DOMAIN_UST
:
4258 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
4259 ret
= LTTCOMM_NO_USTCONSUMERD
;
4263 case LTTNG_DOMAIN_KERNEL
:
4264 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
4265 ret
= LTTCOMM_NO_KERNCONSUMERD
;
4273 * Check that the UID or GID match that of the tracing session.
4274 * The root user can interact with all sessions.
4276 if (need_tracing_session
) {
4277 if (!session_access_ok(cmd_ctx
->session
,
4278 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4279 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
4280 ret
= LTTCOMM_EPERM
;
4285 /* Process by command type */
4286 switch (cmd_ctx
->lsm
->cmd_type
) {
4287 case LTTNG_ADD_CONTEXT
:
4289 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4290 cmd_ctx
->lsm
->u
.context
.channel_name
,
4291 cmd_ctx
->lsm
->u
.context
.event_name
,
4292 &cmd_ctx
->lsm
->u
.context
.ctx
);
4295 case LTTNG_DISABLE_CHANNEL
:
4297 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4298 cmd_ctx
->lsm
->u
.disable
.channel_name
);
4301 case LTTNG_DISABLE_EVENT
:
4303 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4304 cmd_ctx
->lsm
->u
.disable
.channel_name
,
4305 cmd_ctx
->lsm
->u
.disable
.name
);
4308 case LTTNG_DISABLE_ALL_EVENT
:
4310 DBG("Disabling all events");
4312 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4313 cmd_ctx
->lsm
->u
.disable
.channel_name
);
4316 case LTTNG_DISABLE_CONSUMER
:
4318 ret
= cmd_disable_consumer(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
);
4321 case LTTNG_ENABLE_CHANNEL
:
4323 ret
= cmd_enable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4324 &cmd_ctx
->lsm
->u
.channel
.chan
);
4327 case LTTNG_ENABLE_CONSUMER
:
4329 ret
= cmd_enable_consumer(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
);
4332 case LTTNG_ENABLE_EVENT
:
4334 ret
= cmd_enable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4335 cmd_ctx
->lsm
->u
.enable
.channel_name
,
4336 &cmd_ctx
->lsm
->u
.enable
.event
);
4339 case LTTNG_ENABLE_ALL_EVENT
:
4341 DBG("Enabling all events");
4343 ret
= cmd_enable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4344 cmd_ctx
->lsm
->u
.enable
.channel_name
,
4345 cmd_ctx
->lsm
->u
.enable
.event
.type
);
4348 case LTTNG_LIST_TRACEPOINTS
:
4350 struct lttng_event
*events
;
4353 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
4354 if (nb_events
< 0) {
4360 * Setup lttng message with payload size set to the event list size in
4361 * bytes and then copy list into the llm payload.
4363 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
4369 /* Copy event list into message payload */
4370 memcpy(cmd_ctx
->llm
->payload
, events
,
4371 sizeof(struct lttng_event
) * nb_events
);
4378 case LTTNG_LIST_TRACEPOINT_FIELDS
:
4380 struct lttng_event_field
*fields
;
4383 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
, &fields
);
4384 if (nb_fields
< 0) {
4390 * Setup lttng message with payload size set to the event list size in
4391 * bytes and then copy list into the llm payload.
4393 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event_field
) * nb_fields
);
4399 /* Copy event list into message payload */
4400 memcpy(cmd_ctx
->llm
->payload
, fields
,
4401 sizeof(struct lttng_event_field
) * nb_fields
);
4408 case LTTNG_SET_CONSUMER_URI
:
4410 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
4411 &cmd_ctx
->lsm
->u
.uri
);
4414 case LTTNG_START_TRACE
:
4416 ret
= cmd_start_trace(cmd_ctx
->session
);
4419 case LTTNG_STOP_TRACE
:
4421 ret
= cmd_stop_trace(cmd_ctx
->session
);
4424 case LTTNG_CREATE_SESSION
:
4426 ret
= cmd_create_session(cmd_ctx
->lsm
->session
.name
,
4427 cmd_ctx
->lsm
->session
.path
, &cmd_ctx
->creds
);
4430 case LTTNG_CREATE_SESSION_URI
:
4432 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
,
4433 &cmd_ctx
->lsm
->u
.create_uri
.ctrl_uri
,
4434 &cmd_ctx
->lsm
->u
.create_uri
.data_uri
,
4435 cmd_ctx
->lsm
->u
.create_uri
.enable_consumer
, &cmd_ctx
->creds
);
4438 case LTTNG_DESTROY_SESSION
:
4440 ret
= cmd_destroy_session(cmd_ctx
->session
,
4441 cmd_ctx
->lsm
->session
.name
);
4443 * Set session to NULL so we do not unlock it after
4446 cmd_ctx
->session
= NULL
;
4449 case LTTNG_LIST_DOMAINS
:
4452 struct lttng_domain
*domains
;
4454 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
4460 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
4465 /* Copy event list into message payload */
4466 memcpy(cmd_ctx
->llm
->payload
, domains
,
4467 nb_dom
* sizeof(struct lttng_domain
));
4474 case LTTNG_LIST_CHANNELS
:
4477 struct lttng_channel
*channels
;
4479 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
4480 cmd_ctx
->session
, &channels
);
4486 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
4491 /* Copy event list into message payload */
4492 memcpy(cmd_ctx
->llm
->payload
, channels
,
4493 nb_chan
* sizeof(struct lttng_channel
));
4500 case LTTNG_LIST_EVENTS
:
4503 struct lttng_event
*events
= NULL
;
4505 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
4506 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
4512 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
4517 /* Copy event list into message payload */
4518 memcpy(cmd_ctx
->llm
->payload
, events
,
4519 nb_event
* sizeof(struct lttng_event
));
4526 case LTTNG_LIST_SESSIONS
:
4528 unsigned int nr_sessions
;
4530 session_lock_list();
4531 nr_sessions
= lttng_sessions_count(
4532 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4533 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4535 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
4537 session_unlock_list();
4541 /* Filled the session array */
4542 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
4543 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4544 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4546 session_unlock_list();
4551 case LTTNG_CALIBRATE
:
4553 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
4554 &cmd_ctx
->lsm
->u
.calibrate
);
4557 case LTTNG_REGISTER_CONSUMER
:
4559 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4560 cmd_ctx
->lsm
->u
.reg
.path
);
4563 case LTTNG_SET_FILTER
:
4565 struct lttng_filter_bytecode
*bytecode
;
4567 if (cmd_ctx
->lsm
->u
.filter
.bytecode_len
> 65336) {
4568 ret
= LTTCOMM_FILTER_INVAL
;
4571 bytecode
= zmalloc(cmd_ctx
->lsm
->u
.filter
.bytecode_len
);
4573 ret
= LTTCOMM_FILTER_NOMEM
;
4576 /* Receive var. len. data */
4577 DBG("Receiving var len data from client ...");
4578 ret
= lttcomm_recv_unix_sock(sock
, bytecode
,
4579 cmd_ctx
->lsm
->u
.filter
.bytecode_len
);
4581 DBG("Nothing recv() from client var len data... continuing");
4583 ret
= LTTCOMM_FILTER_INVAL
;
4587 if (bytecode
->len
+ sizeof(*bytecode
)
4588 != cmd_ctx
->lsm
->u
.filter
.bytecode_len
) {
4590 ret
= LTTCOMM_FILTER_INVAL
;
4594 ret
= cmd_set_filter(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4595 cmd_ctx
->lsm
->u
.filter
.channel_name
,
4596 cmd_ctx
->lsm
->u
.filter
.event_name
,
4606 if (cmd_ctx
->llm
== NULL
) {
4607 DBG("Missing llm structure. Allocating one.");
4608 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
4612 /* Set return code */
4613 cmd_ctx
->llm
->ret_code
= ret
;
4615 if (cmd_ctx
->session
) {
4616 session_unlock(cmd_ctx
->session
);
4618 if (need_tracing_session
) {
4619 session_unlock_list();
4626 * Thread managing health check socket.
4628 static void *thread_manage_health(void *data
)
4630 int sock
= -1, new_sock
, ret
, i
, pollfd
;
4631 uint32_t revents
, nb_fd
;
4632 struct lttng_poll_event events
;
4633 struct lttcomm_health_msg msg
;
4634 struct lttcomm_health_data reply
;
4636 DBG("[thread] Manage health check started");
4638 rcu_register_thread();
4640 /* Create unix socket */
4641 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
4643 ERR("Unable to create health check Unix socket");
4648 ret
= lttcomm_listen_unix_sock(sock
);
4654 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4655 * more will be added to this poll set.
4657 ret
= create_thread_poll_set(&events
, 2);
4662 /* Add the application registration socket */
4663 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4669 DBG("Health check ready");
4671 nb_fd
= LTTNG_POLL_GETNB(&events
);
4673 /* Inifinite blocking call, waiting for transmission */
4675 ret
= lttng_poll_wait(&events
, -1);
4678 * Restart interrupted system call.
4680 if (errno
== EINTR
) {
4686 for (i
= 0; i
< nb_fd
; i
++) {
4687 /* Fetch once the poll data */
4688 revents
= LTTNG_POLL_GETEV(&events
, i
);
4689 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4691 /* Thread quit pipe has been closed. Killing thread. */
4692 ret
= check_thread_quit_pipe(pollfd
, revents
);
4697 /* Event on the registration socket */
4698 if (pollfd
== sock
) {
4699 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4700 ERR("Health socket poll error");
4706 new_sock
= lttcomm_accept_unix_sock(sock
);
4711 DBG("Receiving data from client for health...");
4712 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4714 DBG("Nothing recv() from client... continuing");
4715 ret
= close(new_sock
);
4723 rcu_thread_online();
4725 switch (msg
.component
) {
4726 case LTTNG_HEALTH_CMD
:
4727 reply
.ret_code
= health_check_state(&health_thread_cmd
);
4729 case LTTNG_HEALTH_APP_REG
:
4730 reply
.ret_code
= health_check_state(&health_thread_app_reg
);
4732 case LTTNG_HEALTH_KERNEL
:
4733 reply
.ret_code
= health_check_state(&health_thread_kernel
);
4735 case LTTNG_HEALTH_CONSUMER
:
4736 reply
.ret_code
= check_consumer_health();
4738 case LTTNG_HEALTH_ALL
:
4739 ret
= check_consumer_health();
4742 health_check_state(&health_thread_app_reg
) &
4743 health_check_state(&health_thread_cmd
) &
4744 health_check_state(&health_thread_kernel
) &
4748 reply
.ret_code
= LTTCOMM_UND
;
4753 * Flip ret value since 0 is a success and 1 indicates a bad health for
4754 * the client where in the sessiond it is the opposite. Again, this is
4755 * just to make things easier for us poor developer which enjoy a lot
4758 if (reply
.ret_code
== 0 || reply
.ret_code
== 1) {
4759 reply
.ret_code
= !reply
.ret_code
;
4762 DBG2("Health check return value %d", reply
.ret_code
);
4764 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4766 ERR("Failed to send health data back to client");
4769 /* End of transmission */
4770 ret
= close(new_sock
);
4778 DBG("Health check thread dying");
4779 unlink(health_unix_sock_path
);
4786 if (new_sock
>= 0) {
4787 ret
= close(new_sock
);
4793 lttng_poll_clean(&events
);
4795 rcu_unregister_thread();
4800 * This thread manage all clients request using the unix client socket for
4803 static void *thread_manage_clients(void *data
)
4805 int sock
= -1, ret
, i
, pollfd
;
4807 uint32_t revents
, nb_fd
;
4808 struct command_ctx
*cmd_ctx
= NULL
;
4809 struct lttng_poll_event events
;
4811 DBG("[thread] Manage client started");
4813 rcu_register_thread();
4815 health_code_update(&health_thread_cmd
);
4817 ret
= lttcomm_listen_unix_sock(client_sock
);
4823 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4824 * more will be added to this poll set.
4826 ret
= create_thread_poll_set(&events
, 2);
4831 /* Add the application registration socket */
4832 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4838 * Notify parent pid that we are ready to accept command for client side.
4840 if (opt_sig_parent
) {
4841 kill(ppid
, SIGUSR1
);
4844 health_code_update(&health_thread_cmd
);
4847 DBG("Accepting client command ...");
4849 nb_fd
= LTTNG_POLL_GETNB(&events
);
4851 /* Inifinite blocking call, waiting for transmission */
4853 health_poll_update(&health_thread_cmd
);
4854 ret
= lttng_poll_wait(&events
, -1);
4855 health_poll_update(&health_thread_cmd
);
4858 * Restart interrupted system call.
4860 if (errno
== EINTR
) {
4866 for (i
= 0; i
< nb_fd
; i
++) {
4867 /* Fetch once the poll data */
4868 revents
= LTTNG_POLL_GETEV(&events
, i
);
4869 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4871 health_code_update(&health_thread_cmd
);
4873 /* Thread quit pipe has been closed. Killing thread. */
4874 ret
= check_thread_quit_pipe(pollfd
, revents
);
4879 /* Event on the registration socket */
4880 if (pollfd
== client_sock
) {
4881 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4882 ERR("Client socket poll error");
4888 DBG("Wait for client response");
4890 health_code_update(&health_thread_cmd
);
4892 sock
= lttcomm_accept_unix_sock(client_sock
);
4897 /* Set socket option for credentials retrieval */
4898 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4903 /* Allocate context command to process the client request */
4904 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4905 if (cmd_ctx
== NULL
) {
4906 PERROR("zmalloc cmd_ctx");
4910 /* Allocate data buffer for reception */
4911 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4912 if (cmd_ctx
->lsm
== NULL
) {
4913 PERROR("zmalloc cmd_ctx->lsm");
4917 cmd_ctx
->llm
= NULL
;
4918 cmd_ctx
->session
= NULL
;
4920 health_code_update(&health_thread_cmd
);
4923 * Data is received from the lttng client. The struct
4924 * lttcomm_session_msg (lsm) contains the command and data request of
4927 DBG("Receiving data from client ...");
4928 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4929 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4931 DBG("Nothing recv() from client... continuing");
4937 clean_command_ctx(&cmd_ctx
);
4941 health_code_update(&health_thread_cmd
);
4943 // TODO: Validate cmd_ctx including sanity check for
4944 // security purpose.
4946 rcu_thread_online();
4948 * This function dispatch the work to the kernel or userspace tracer
4949 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4950 * informations for the client. The command context struct contains
4951 * everything this function may needs.
4953 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4954 rcu_thread_offline();
4964 * TODO: Inform client somehow of the fatal error. At
4965 * this point, ret < 0 means that a zmalloc failed
4966 * (ENOMEM). Error detected but still accept
4967 * command, unless a socket error has been
4970 clean_command_ctx(&cmd_ctx
);
4974 health_code_update(&health_thread_cmd
);
4976 DBG("Sending response (size: %d, retcode: %s)",
4977 cmd_ctx
->lttng_msg_size
,
4978 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
4979 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4981 ERR("Failed to send data back to client");
4984 /* End of transmission */
4991 clean_command_ctx(&cmd_ctx
);
4993 health_code_update(&health_thread_cmd
);
4997 health_reset(&health_thread_cmd
);
4999 DBG("Client thread dying");
5000 unlink(client_unix_sock_path
);
5001 if (client_sock
>= 0) {
5002 ret
= close(client_sock
);
5014 lttng_poll_clean(&events
);
5015 clean_command_ctx(&cmd_ctx
);
5017 rcu_unregister_thread();
5023 * usage function on stderr
5025 static void usage(void)
5027 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
5028 fprintf(stderr
, " -h, --help Display this usage.\n");
5029 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
5030 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
5031 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
5032 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
5033 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
5034 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
5035 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
5036 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
5037 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
5038 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
5039 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
5040 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
5041 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
5042 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
5043 fprintf(stderr
, " -V, --version Show version number.\n");
5044 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
5045 fprintf(stderr
, " -q, --quiet No output at all.\n");
5046 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
5047 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
5048 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
5052 * daemon argument parsing
5054 static int parse_args(int argc
, char **argv
)
5058 static struct option long_options
[] = {
5059 { "client-sock", 1, 0, 'c' },
5060 { "apps-sock", 1, 0, 'a' },
5061 { "kconsumerd-cmd-sock", 1, 0, 'C' },
5062 { "kconsumerd-err-sock", 1, 0, 'E' },
5063 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
5064 { "ustconsumerd32-err-sock", 1, 0, 'H' },
5065 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
5066 { "ustconsumerd64-err-sock", 1, 0, 'F' },
5067 { "consumerd32-path", 1, 0, 'u' },
5068 { "consumerd32-libdir", 1, 0, 'U' },
5069 { "consumerd64-path", 1, 0, 't' },
5070 { "consumerd64-libdir", 1, 0, 'T' },
5071 { "daemonize", 0, 0, 'd' },
5072 { "sig-parent", 0, 0, 'S' },
5073 { "help", 0, 0, 'h' },
5074 { "group", 1, 0, 'g' },
5075 { "version", 0, 0, 'V' },
5076 { "quiet", 0, 0, 'q' },
5077 { "verbose", 0, 0, 'v' },
5078 { "verbose-consumer", 0, 0, 'Z' },
5079 { "no-kernel", 0, 0, 'N' },
5084 int option_index
= 0;
5085 c
= getopt_long(argc
, argv
, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t",
5086 long_options
, &option_index
);
5093 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
5095 fprintf(stderr
, " with arg %s\n", optarg
);
5099 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5102 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5108 opt_tracing_group
= optarg
;
5114 fprintf(stdout
, "%s\n", VERSION
);
5120 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5123 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5126 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5129 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5132 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5135 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5141 lttng_opt_quiet
= 1;
5144 /* Verbose level can increase using multiple -v */
5145 lttng_opt_verbose
+= 1;
5148 opt_verbose_consumer
+= 1;
5151 consumerd32_bin
= optarg
;
5154 consumerd32_libdir
= optarg
;
5157 consumerd64_bin
= optarg
;
5160 consumerd64_libdir
= optarg
;
5163 /* Unknown option or other error.
5164 * Error is printed by getopt, just return */
5173 * Creates the two needed socket by the daemon.
5174 * apps_sock - The communication socket for all UST apps.
5175 * client_sock - The communication of the cli tool (lttng).
5177 static int init_daemon_socket(void)
5182 old_umask
= umask(0);
5184 /* Create client tool unix socket */
5185 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
5186 if (client_sock
< 0) {
5187 ERR("Create unix sock failed: %s", client_unix_sock_path
);
5192 /* File permission MUST be 660 */
5193 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5195 ERR("Set file permissions failed: %s", client_unix_sock_path
);
5200 /* Create the application unix socket */
5201 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
5202 if (apps_sock
< 0) {
5203 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
5208 /* File permission MUST be 666 */
5209 ret
= chmod(apps_unix_sock_path
,
5210 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5212 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
5223 * Check if the global socket is available, and if a daemon is answering at the
5224 * other side. If yes, error is returned.
5226 static int check_existing_daemon(void)
5228 /* Is there anybody out there ? */
5229 if (lttng_session_daemon_alive()) {
5237 * Set the tracing group gid onto the client socket.
5239 * Race window between mkdir and chown is OK because we are going from more
5240 * permissive (root.root) to less permissive (root.tracing).
5242 static int set_permissions(char *rundir
)
5247 ret
= allowed_group();
5249 WARN("No tracing group detected");
5256 /* Set lttng run dir */
5257 ret
= chown(rundir
, 0, gid
);
5259 ERR("Unable to set group on %s", rundir
);
5263 /* Ensure tracing group can search the run dir */
5264 ret
= chmod(rundir
, S_IRWXU
| S_IXGRP
| S_IXOTH
);
5266 ERR("Unable to set permissions on %s", rundir
);
5270 /* lttng client socket path */
5271 ret
= chown(client_unix_sock_path
, 0, gid
);
5273 ERR("Unable to set group on %s", client_unix_sock_path
);
5277 /* kconsumer error socket path */
5278 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, gid
);
5280 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5284 /* 64-bit ustconsumer error socket path */
5285 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, gid
);
5287 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5291 /* 32-bit ustconsumer compat32 error socket path */
5292 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, gid
);
5294 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5298 DBG("All permissions are set");
5305 * Create the lttng run directory needed for all global sockets and pipe.
5307 static int create_lttng_rundir(const char *rundir
)
5311 DBG3("Creating LTTng run directory: %s", rundir
);
5313 ret
= mkdir(rundir
, S_IRWXU
);
5315 if (errno
!= EEXIST
) {
5316 ERR("Unable to create %s", rundir
);
5328 * Setup sockets and directory needed by the kconsumerd communication with the
5331 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
5335 char path
[PATH_MAX
];
5337 switch (consumer_data
->type
) {
5338 case LTTNG_CONSUMER_KERNEL
:
5339 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
5341 case LTTNG_CONSUMER64_UST
:
5342 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
5344 case LTTNG_CONSUMER32_UST
:
5345 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
5348 ERR("Consumer type unknown");
5353 DBG2("Creating consumer directory: %s", path
);
5355 ret
= mkdir(path
, S_IRWXU
);
5357 if (errno
!= EEXIST
) {
5359 ERR("Failed to create %s", path
);
5365 /* Create the kconsumerd error unix socket */
5366 consumer_data
->err_sock
=
5367 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5368 if (consumer_data
->err_sock
< 0) {
5369 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5374 /* File permission MUST be 660 */
5375 ret
= chmod(consumer_data
->err_unix_sock_path
,
5376 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5378 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5388 * Signal handler for the daemon
5390 * Simply stop all worker threads, leaving main() return gracefully after
5391 * joining all threads and calling cleanup().
5393 static void sighandler(int sig
)
5397 DBG("SIGPIPE caught");
5400 DBG("SIGINT caught");
5404 DBG("SIGTERM caught");
5413 * Setup signal handler for :
5414 * SIGINT, SIGTERM, SIGPIPE
5416 static int set_signal_handler(void)
5419 struct sigaction sa
;
5422 if ((ret
= sigemptyset(&sigset
)) < 0) {
5423 PERROR("sigemptyset");
5427 sa
.sa_handler
= sighandler
;
5428 sa
.sa_mask
= sigset
;
5430 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5431 PERROR("sigaction");
5435 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5436 PERROR("sigaction");
5440 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5441 PERROR("sigaction");
5445 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
5451 * Set open files limit to unlimited. This daemon can open a large number of
5452 * file descriptors in order to consumer multiple kernel traces.
5454 static void set_ulimit(void)
5459 /* The kernel does not allowed an infinite limit for open files */
5460 lim
.rlim_cur
= 65535;
5461 lim
.rlim_max
= 65535;
5463 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5465 PERROR("failed to set open files limit");
5472 int main(int argc
, char **argv
)
5476 const char *home_path
;
5478 init_kernel_workarounds();
5480 rcu_register_thread();
5482 setup_consumerd_path();
5484 /* Parse arguments */
5486 if ((ret
= parse_args(argc
, argv
) < 0)) {
5496 * child: setsid, close FD 0, 1, 2, chdir /
5497 * parent: exit (if fork is successful)
5505 * We are in the child. Make sure all other file
5506 * descriptors are closed, in case we are called with
5507 * more opened file descriptors than the standard ones.
5509 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5514 /* Create thread quit pipe */
5515 if ((ret
= init_thread_quit_pipe()) < 0) {
5519 /* Check if daemon is UID = 0 */
5520 is_root
= !getuid();
5523 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
5525 /* Create global run dir with root access */
5526 ret
= create_lttng_rundir(rundir
);
5531 if (strlen(apps_unix_sock_path
) == 0) {
5532 snprintf(apps_unix_sock_path
, PATH_MAX
,
5533 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
5536 if (strlen(client_unix_sock_path
) == 0) {
5537 snprintf(client_unix_sock_path
, PATH_MAX
,
5538 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
5541 /* Set global SHM for ust */
5542 if (strlen(wait_shm_path
) == 0) {
5543 snprintf(wait_shm_path
, PATH_MAX
,
5544 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
5547 if (strlen(health_unix_sock_path
) == 0) {
5548 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
5549 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
5552 /* Setup kernel consumerd path */
5553 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
5554 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
5555 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
5556 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
5558 DBG2("Kernel consumer err path: %s",
5559 kconsumer_data
.err_unix_sock_path
);
5560 DBG2("Kernel consumer cmd path: %s",
5561 kconsumer_data
.cmd_unix_sock_path
);
5563 home_path
= get_home_dir();
5564 if (home_path
== NULL
) {
5565 /* TODO: Add --socket PATH option */
5566 ERR("Can't get HOME directory for sockets creation.");
5572 * Create rundir from home path. This will create something like
5575 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
5581 ret
= create_lttng_rundir(rundir
);
5586 if (strlen(apps_unix_sock_path
) == 0) {
5587 snprintf(apps_unix_sock_path
, PATH_MAX
,
5588 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
5591 /* Set the cli tool unix socket path */
5592 if (strlen(client_unix_sock_path
) == 0) {
5593 snprintf(client_unix_sock_path
, PATH_MAX
,
5594 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
5597 /* Set global SHM for ust */
5598 if (strlen(wait_shm_path
) == 0) {
5599 snprintf(wait_shm_path
, PATH_MAX
,
5600 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, geteuid());
5603 /* Set health check Unix path */
5604 if (strlen(health_unix_sock_path
) == 0) {
5605 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
5606 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
5610 /* Set consumer initial state */
5611 kernel_consumerd_state
= CONSUMER_STOPPED
;
5612 ust_consumerd_state
= CONSUMER_STOPPED
;
5614 DBG("Client socket path %s", client_unix_sock_path
);
5615 DBG("Application socket path %s", apps_unix_sock_path
);
5616 DBG("LTTng run directory path: %s", rundir
);
5618 /* 32 bits consumerd path setup */
5619 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
5620 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
5621 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
5622 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
5624 DBG2("UST consumer 32 bits err path: %s",
5625 ustconsumer32_data
.err_unix_sock_path
);
5626 DBG2("UST consumer 32 bits cmd path: %s",
5627 ustconsumer32_data
.cmd_unix_sock_path
);
5629 /* 64 bits consumerd path setup */
5630 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
5631 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
5632 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
5633 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
5635 DBG2("UST consumer 64 bits err path: %s",
5636 ustconsumer64_data
.err_unix_sock_path
);
5637 DBG2("UST consumer 64 bits cmd path: %s",
5638 ustconsumer64_data
.cmd_unix_sock_path
);
5641 * See if daemon already exist.
5643 if ((ret
= check_existing_daemon()) < 0) {
5644 ERR("Already running daemon.\n");
5646 * We do not goto exit because we must not cleanup()
5647 * because a daemon is already running.
5653 * Init UST app hash table. Alloc hash table before this point since
5654 * cleanup() can get called after that point.
5658 /* After this point, we can safely call cleanup() with "goto exit" */
5661 * These actions must be executed as root. We do that *after* setting up
5662 * the sockets path because we MUST make the check for another daemon using
5663 * those paths *before* trying to set the kernel consumer sockets and init
5667 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
5672 /* Setup kernel tracer */
5673 if (!opt_no_kernel
) {
5674 init_kernel_tracer();
5677 /* Set ulimit for open files */
5680 /* init lttng_fd tracking must be done after set_ulimit. */
5683 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
5688 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
5693 if ((ret
= set_signal_handler()) < 0) {
5697 /* Setup the needed unix socket */
5698 if ((ret
= init_daemon_socket()) < 0) {
5702 /* Set credentials to socket */
5703 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
5707 /* Get parent pid if -S, --sig-parent is specified. */
5708 if (opt_sig_parent
) {
5712 /* Setup the kernel pipe for waking up the kernel thread */
5713 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
5717 /* Setup the thread apps communication pipe. */
5718 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
5722 /* Init UST command queue. */
5723 cds_wfq_init(&ust_cmd_queue
.queue
);
5726 * Get session list pointer. This pointer MUST NOT be free(). This list is
5727 * statically declared in session.c
5729 session_list_ptr
= session_get_list();
5731 /* Set up max poll set size */
5732 lttng_poll_set_max_size();
5735 * Set network sequence index to 1 for streams to match a relayd socket on
5736 * the consumer side.
5738 uatomic_set(&relayd_net_seq_idx
, 1);
5740 /* Init all health thread counters. */
5741 health_init(&health_thread_cmd
);
5742 health_init(&health_thread_kernel
);
5743 health_init(&health_thread_app_reg
);
5746 * Init health counters of the consumer thread. We do a quick hack here to
5747 * the state of the consumer health is fine even if the thread is not
5748 * started. This is simply to ease our life and has no cost what so ever.
5750 health_init(&kconsumer_data
.health
);
5751 health_poll_update(&kconsumer_data
.health
);
5752 health_init(&ustconsumer32_data
.health
);
5753 health_poll_update(&ustconsumer32_data
.health
);
5754 health_init(&ustconsumer64_data
.health
);
5755 health_poll_update(&ustconsumer64_data
.health
);
5757 /* Create thread to manage the client socket */
5758 ret
= pthread_create(&health_thread
, NULL
,
5759 thread_manage_health
, (void *) NULL
);
5761 PERROR("pthread_create health");
5765 /* Create thread to manage the client socket */
5766 ret
= pthread_create(&client_thread
, NULL
,
5767 thread_manage_clients
, (void *) NULL
);
5769 PERROR("pthread_create clients");
5773 /* Create thread to dispatch registration */
5774 ret
= pthread_create(&dispatch_thread
, NULL
,
5775 thread_dispatch_ust_registration
, (void *) NULL
);
5777 PERROR("pthread_create dispatch");
5781 /* Create thread to manage application registration. */
5782 ret
= pthread_create(®_apps_thread
, NULL
,
5783 thread_registration_apps
, (void *) NULL
);
5785 PERROR("pthread_create registration");
5789 /* Create thread to manage application socket */
5790 ret
= pthread_create(&apps_thread
, NULL
,
5791 thread_manage_apps
, (void *) NULL
);
5793 PERROR("pthread_create apps");
5797 /* Create kernel thread to manage kernel event */
5798 ret
= pthread_create(&kernel_thread
, NULL
,
5799 thread_manage_kernel
, (void *) NULL
);
5801 PERROR("pthread_create kernel");
5805 ret
= pthread_join(kernel_thread
, &status
);
5807 PERROR("pthread_join");
5808 goto error
; /* join error, exit without cleanup */
5812 ret
= pthread_join(apps_thread
, &status
);
5814 PERROR("pthread_join");
5815 goto error
; /* join error, exit without cleanup */
5819 ret
= pthread_join(reg_apps_thread
, &status
);
5821 PERROR("pthread_join");
5822 goto error
; /* join error, exit without cleanup */
5826 ret
= pthread_join(dispatch_thread
, &status
);
5828 PERROR("pthread_join");
5829 goto error
; /* join error, exit without cleanup */
5833 ret
= pthread_join(client_thread
, &status
);
5835 PERROR("pthread_join");
5836 goto error
; /* join error, exit without cleanup */
5839 ret
= join_consumer_thread(&kconsumer_data
);
5841 PERROR("join_consumer");
5842 goto error
; /* join error, exit without cleanup */
5849 * cleanup() is called when no other thread is running.
5851 rcu_thread_online();
5853 rcu_thread_offline();
5854 rcu_unregister_thread();