2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <semaphore.h>
30 #include <sys/mount.h>
31 #include <sys/resource.h>
32 #include <sys/socket.h>
34 #include <sys/types.h>
36 #include <urcu/uatomic.h>
40 #include <common/common.h>
41 #include <common/compat/poll.h>
42 #include <common/compat/socket.h>
43 #include <common/defaults.h>
44 #include <common/kernel-consumer/kernel-consumer.h>
45 #include <common/futex.h>
46 #include <common/relayd/relayd.h>
48 #include "lttng-sessiond.h"
54 #include "kernel-consumer.h"
58 #include "ust-consumer.h"
64 #define CONSUMERD_FILE "lttng-consumerd"
67 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
68 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
69 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
70 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
73 const char *opt_tracing_group
;
74 static int opt_sig_parent
;
75 static int opt_verbose_consumer
;
76 static int opt_daemon
;
77 static int opt_no_kernel
;
78 static int is_root
; /* Set to 1 if the daemon is running as root */
79 static pid_t ppid
; /* Parent PID for --sig-parent option */
82 /* Consumer daemon specific control data */
83 static struct consumer_data kconsumer_data
= {
84 .type
= LTTNG_CONSUMER_KERNEL
,
85 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
86 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
90 static struct consumer_data ustconsumer64_data
= {
91 .type
= LTTNG_CONSUMER64_UST
,
92 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
93 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
97 static struct consumer_data ustconsumer32_data
= {
98 .type
= LTTNG_CONSUMER32_UST
,
99 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
100 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
105 static int dispatch_thread_exit
;
107 /* Global application Unix socket path */
108 static char apps_unix_sock_path
[PATH_MAX
];
109 /* Global client Unix socket path */
110 static char client_unix_sock_path
[PATH_MAX
];
111 /* global wait shm path for UST */
112 static char wait_shm_path
[PATH_MAX
];
113 /* Global health check unix path */
114 static char health_unix_sock_path
[PATH_MAX
];
116 /* Sockets and FDs */
117 static int client_sock
= -1;
118 static int apps_sock
= -1;
119 static int kernel_tracer_fd
= -1;
120 static int kernel_poll_pipe
[2] = { -1, -1 };
123 * Quit pipe for all threads. This permits a single cancellation point
124 * for all threads when receiving an event on the pipe.
126 static int thread_quit_pipe
[2] = { -1, -1 };
129 * This pipe is used to inform the thread managing application communication
130 * that a command is queued and ready to be processed.
132 static int apps_cmd_pipe
[2] = { -1, -1 };
134 /* Pthread, Mutexes and Semaphores */
135 static pthread_t apps_thread
;
136 static pthread_t reg_apps_thread
;
137 static pthread_t client_thread
;
138 static pthread_t kernel_thread
;
139 static pthread_t dispatch_thread
;
140 static pthread_t health_thread
;
143 * UST registration command queue. This queue is tied with a futex and uses a N
144 * wakers / 1 waiter implemented and detailed in futex.c/.h
146 * The thread_manage_apps and thread_dispatch_ust_registration interact with
147 * this queue and the wait/wake scheme.
149 static struct ust_cmd_queue ust_cmd_queue
;
152 * Pointer initialized before thread creation.
154 * This points to the tracing session list containing the session count and a
155 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
156 * MUST NOT be taken if you call a public function in session.c.
158 * The lock is nested inside the structure: session_list_ptr->lock. Please use
159 * session_lock_list and session_unlock_list for lock acquisition.
161 static struct ltt_session_list
*session_list_ptr
;
163 int ust_consumerd64_fd
= -1;
164 int ust_consumerd32_fd
= -1;
166 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
167 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
168 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
169 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
172 * Consumer daemon state which is changed when spawning it, killing it or in
173 * case of a fatal error.
175 enum consumerd_state
{
176 CONSUMER_STARTED
= 1,
177 CONSUMER_STOPPED
= 2,
182 * This consumer daemon state is used to validate if a client command will be
183 * able to reach the consumer. If not, the client is informed. For instance,
184 * doing a "lttng start" when the consumer state is set to ERROR will return an
185 * error to the client.
187 * The following example shows a possible race condition of this scheme:
189 * consumer thread error happens
191 * client cmd checks state -> still OK
192 * consumer thread exit, sets error
193 * client cmd try to talk to consumer
196 * However, since the consumer is a different daemon, we have no way of making
197 * sure the command will reach it safely even with this state flag. This is why
198 * we consider that up to the state validation during command processing, the
199 * command is safe. After that, we can not guarantee the correctness of the
200 * client request vis-a-vis the consumer.
202 static enum consumerd_state ust_consumerd_state
;
203 static enum consumerd_state kernel_consumerd_state
;
206 * Used to keep a unique index for each relayd socket created where this value
207 * is associated with streams on the consumer so it can match the right relayd
210 * This value should be incremented atomically for safety purposes and future
211 * possible concurrent access.
213 static unsigned int relayd_net_seq_idx
;
215 /* Used for the health monitoring of the session daemon. See health.h */
216 struct health_state health_thread_cmd
;
217 struct health_state health_thread_app_reg
;
218 struct health_state health_thread_kernel
;
221 void setup_consumerd_path(void)
223 const char *bin
, *libdir
;
226 * Allow INSTALL_BIN_PATH to be used as a target path for the
227 * native architecture size consumer if CONFIG_CONSUMER*_PATH
228 * has not been defined.
230 #if (CAA_BITS_PER_LONG == 32)
231 if (!consumerd32_bin
[0]) {
232 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
234 if (!consumerd32_libdir
[0]) {
235 consumerd32_libdir
= INSTALL_LIB_PATH
;
237 #elif (CAA_BITS_PER_LONG == 64)
238 if (!consumerd64_bin
[0]) {
239 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
241 if (!consumerd64_libdir
[0]) {
242 consumerd64_libdir
= INSTALL_LIB_PATH
;
245 #error "Unknown bitness"
249 * runtime env. var. overrides the build default.
251 bin
= getenv("LTTNG_CONSUMERD32_BIN");
253 consumerd32_bin
= bin
;
255 bin
= getenv("LTTNG_CONSUMERD64_BIN");
257 consumerd64_bin
= bin
;
259 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
261 consumerd32_libdir
= libdir
;
263 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
265 consumerd64_libdir
= libdir
;
270 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
272 static int create_thread_poll_set(struct lttng_poll_event
*events
,
277 if (events
== NULL
|| size
== 0) {
282 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
288 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
300 * Check if the thread quit pipe was triggered.
302 * Return 1 if it was triggered else 0;
304 static int check_thread_quit_pipe(int fd
, uint32_t events
)
306 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
314 * Return group ID of the tracing group or -1 if not found.
316 static gid_t
allowed_group(void)
320 if (opt_tracing_group
) {
321 grp
= getgrnam(opt_tracing_group
);
323 grp
= getgrnam(default_tracing_group
);
333 * Init thread quit pipe.
335 * Return -1 on error or 0 if all pipes are created.
337 static int init_thread_quit_pipe(void)
341 ret
= pipe(thread_quit_pipe
);
343 PERROR("thread quit pipe");
347 for (i
= 0; i
< 2; i
++) {
348 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
360 * Complete teardown of a kernel session. This free all data structure related
361 * to a kernel session and update counter.
363 static void teardown_kernel_session(struct ltt_session
*session
)
365 if (!session
->kernel_session
) {
366 DBG3("No kernel session when tearing down session");
370 DBG("Tearing down kernel session");
373 * If a custom kernel consumer was registered, close the socket before
374 * tearing down the complete kernel session structure
376 if (kconsumer_data
.cmd_sock
>= 0 &&
377 session
->kernel_session
->consumer_fd
!= kconsumer_data
.cmd_sock
) {
378 lttcomm_close_unix_sock(session
->kernel_session
->consumer_fd
);
381 trace_kernel_destroy_session(session
->kernel_session
);
385 * Complete teardown of all UST sessions. This will free everything on his path
386 * and destroy the core essence of all ust sessions :)
388 static void teardown_ust_session(struct ltt_session
*session
)
392 if (!session
->ust_session
) {
393 DBG3("No UST session when tearing down session");
397 DBG("Tearing down UST session(s)");
399 ret
= ust_app_destroy_trace_all(session
->ust_session
);
401 ERR("Error in ust_app_destroy_trace_all");
404 trace_ust_destroy_session(session
->ust_session
);
408 * Stop all threads by closing the thread quit pipe.
410 static void stop_threads(void)
414 /* Stopping all threads */
415 DBG("Terminating all threads");
416 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
418 ERR("write error on thread quit pipe");
421 /* Dispatch thread */
422 dispatch_thread_exit
= 1;
423 futex_nto1_wake(&ust_cmd_queue
.futex
);
429 static void cleanup(void)
433 struct ltt_session
*sess
, *stmp
;
437 DBG("Removing %s directory", rundir
);
438 ret
= asprintf(&cmd
, "rm -rf %s", rundir
);
440 ERR("asprintf failed. Something is really wrong!");
443 /* Remove lttng run directory */
446 ERR("Unable to clean %s", rundir
);
450 DBG("Cleaning up all sessions");
452 /* Destroy session list mutex */
453 if (session_list_ptr
!= NULL
) {
454 pthread_mutex_destroy(&session_list_ptr
->lock
);
456 /* Cleanup ALL session */
457 cds_list_for_each_entry_safe(sess
, stmp
,
458 &session_list_ptr
->head
, list
) {
459 teardown_kernel_session(sess
);
460 teardown_ust_session(sess
);
465 DBG("Closing all UST sockets");
466 ust_app_clean_list();
468 pthread_mutex_destroy(&kconsumer_data
.pid_mutex
);
470 if (is_root
&& !opt_no_kernel
) {
471 DBG2("Closing kernel fd");
472 if (kernel_tracer_fd
>= 0) {
473 ret
= close(kernel_tracer_fd
);
478 DBG("Unloading kernel modules");
479 modprobe_remove_lttng_all();
481 utils_close_pipe(kernel_poll_pipe
);
482 utils_close_pipe(thread_quit_pipe
);
483 utils_close_pipe(apps_cmd_pipe
);
486 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
487 "Matthew, BEET driven development works!%c[%dm",
488 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
493 * Send data on a unix socket using the liblttsessiondcomm API.
495 * Return lttcomm error code.
497 static int send_unix_sock(int sock
, void *buf
, size_t len
)
499 /* Check valid length */
504 return lttcomm_send_unix_sock(sock
, buf
, len
);
508 * Free memory of a command context structure.
510 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
512 DBG("Clean command context structure");
514 if ((*cmd_ctx
)->llm
) {
515 free((*cmd_ctx
)->llm
);
517 if ((*cmd_ctx
)->lsm
) {
518 free((*cmd_ctx
)->lsm
);
526 * Notify UST applications using the shm mmap futex.
528 static int notify_ust_apps(int active
)
532 DBG("Notifying applications of session daemon state: %d", active
);
534 /* See shm.c for this call implying mmap, shm and futex calls */
535 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
536 if (wait_shm_mmap
== NULL
) {
540 /* Wake waiting process */
541 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
543 /* Apps notified successfully */
551 * Setup the outgoing data buffer for the response (llm) by allocating the
552 * right amount of memory and copying the original information from the lsm
555 * Return total size of the buffer pointed by buf.
557 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
563 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
564 if (cmd_ctx
->llm
== NULL
) {
570 /* Copy common data */
571 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
572 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
574 cmd_ctx
->llm
->data_size
= size
;
575 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
584 * Update the kernel poll set of all channel fd available over all tracing
585 * session. Add the wakeup pipe at the end of the set.
587 static int update_kernel_poll(struct lttng_poll_event
*events
)
590 struct ltt_session
*session
;
591 struct ltt_kernel_channel
*channel
;
593 DBG("Updating kernel poll set");
596 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
597 session_lock(session
);
598 if (session
->kernel_session
== NULL
) {
599 session_unlock(session
);
603 cds_list_for_each_entry(channel
,
604 &session
->kernel_session
->channel_list
.head
, list
) {
605 /* Add channel fd to the kernel poll set */
606 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
608 session_unlock(session
);
611 DBG("Channel fd %d added to kernel set", channel
->fd
);
613 session_unlock(session
);
615 session_unlock_list();
620 session_unlock_list();
625 * Find the channel fd from 'fd' over all tracing session. When found, check
626 * for new channel stream and send those stream fds to the kernel consumer.
628 * Useful for CPU hotplug feature.
630 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
633 struct ltt_session
*session
;
634 struct ltt_kernel_channel
*channel
;
636 DBG("Updating kernel streams for channel fd %d", fd
);
639 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
640 session_lock(session
);
641 if (session
->kernel_session
== NULL
) {
642 session_unlock(session
);
646 /* This is not suppose to be -1 but this is an extra security check */
647 if (session
->kernel_session
->consumer_fd
< 0) {
648 session
->kernel_session
->consumer_fd
= consumer_data
->cmd_sock
;
651 cds_list_for_each_entry(channel
,
652 &session
->kernel_session
->channel_list
.head
, list
) {
653 if (channel
->fd
== fd
) {
654 DBG("Channel found, updating kernel streams");
655 ret
= kernel_open_channel_stream(channel
);
661 * Have we already sent fds to the consumer? If yes, it means
662 * that tracing is started so it is safe to send our updated
665 if (session
->kernel_session
->consumer_fds_sent
== 1 &&
666 session
->kernel_session
->consumer
!= NULL
) {
667 ret
= kernel_consumer_send_channel_stream(
668 session
->kernel_session
->consumer_fd
, channel
,
669 session
->kernel_session
);
677 session_unlock(session
);
679 session_unlock_list();
683 session_unlock(session
);
684 session_unlock_list();
689 * For each tracing session, update newly registered apps.
691 static void update_ust_app(int app_sock
)
693 struct ltt_session
*sess
, *stmp
;
697 /* For all tracing session(s) */
698 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
700 if (sess
->ust_session
) {
701 ust_app_global_update(sess
->ust_session
, app_sock
);
703 session_unlock(sess
);
706 session_unlock_list();
710 * This thread manage event coming from the kernel.
712 * Features supported in this thread:
715 static void *thread_manage_kernel(void *data
)
717 int ret
, i
, pollfd
, update_poll_flag
= 1;
718 uint32_t revents
, nb_fd
;
720 struct lttng_poll_event events
;
722 DBG("Thread manage kernel started");
724 health_code_update(&health_thread_kernel
);
726 ret
= create_thread_poll_set(&events
, 2);
728 goto error_poll_create
;
731 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
737 health_code_update(&health_thread_kernel
);
739 if (update_poll_flag
== 1) {
741 * Reset number of fd in the poll set. Always 2 since there is the thread
742 * quit pipe and the kernel pipe.
746 ret
= update_kernel_poll(&events
);
750 update_poll_flag
= 0;
753 nb_fd
= LTTNG_POLL_GETNB(&events
);
755 DBG("Thread kernel polling on %d fds", nb_fd
);
757 /* Zeroed the poll events */
758 lttng_poll_reset(&events
);
760 /* Poll infinite value of time */
762 health_poll_update(&health_thread_kernel
);
763 ret
= lttng_poll_wait(&events
, -1);
764 health_poll_update(&health_thread_kernel
);
767 * Restart interrupted system call.
769 if (errno
== EINTR
) {
773 } else if (ret
== 0) {
774 /* Should not happen since timeout is infinite */
775 ERR("Return value of poll is 0 with an infinite timeout.\n"
776 "This should not have happened! Continuing...");
780 for (i
= 0; i
< nb_fd
; i
++) {
781 /* Fetch once the poll data */
782 revents
= LTTNG_POLL_GETEV(&events
, i
);
783 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
785 health_code_update(&health_thread_kernel
);
787 /* Thread quit pipe has been closed. Killing thread. */
788 ret
= check_thread_quit_pipe(pollfd
, revents
);
793 /* Check for data on kernel pipe */
794 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
795 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
796 update_poll_flag
= 1;
800 * New CPU detected by the kernel. Adding kernel stream to
801 * kernel session and updating the kernel consumer
803 if (revents
& LPOLLIN
) {
804 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
810 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
811 * and unregister kernel stream at this point.
819 lttng_poll_clean(&events
);
821 health_reset(&health_thread_kernel
);
822 DBG("Kernel thread dying");
827 * This thread manage the consumer error sent back to the session daemon.
829 static void *thread_manage_consumer(void *data
)
831 int sock
= -1, i
, ret
, pollfd
;
832 uint32_t revents
, nb_fd
;
833 enum lttcomm_return_code code
;
834 struct lttng_poll_event events
;
835 struct consumer_data
*consumer_data
= data
;
837 DBG("[thread] Manage consumer started");
839 health_code_update(&consumer_data
->health
);
841 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
847 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
848 * Nothing more will be added to this poll set.
850 ret
= create_thread_poll_set(&events
, 2);
855 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
860 nb_fd
= LTTNG_POLL_GETNB(&events
);
862 health_code_update(&consumer_data
->health
);
864 /* Inifinite blocking call, waiting for transmission */
866 health_poll_update(&consumer_data
->health
);
867 ret
= lttng_poll_wait(&events
, -1);
868 health_poll_update(&consumer_data
->health
);
871 * Restart interrupted system call.
873 if (errno
== EINTR
) {
879 for (i
= 0; i
< nb_fd
; i
++) {
880 /* Fetch once the poll data */
881 revents
= LTTNG_POLL_GETEV(&events
, i
);
882 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
884 health_code_update(&consumer_data
->health
);
886 /* Thread quit pipe has been closed. Killing thread. */
887 ret
= check_thread_quit_pipe(pollfd
, revents
);
892 /* Event on the registration socket */
893 if (pollfd
== consumer_data
->err_sock
) {
894 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
895 ERR("consumer err socket poll error");
901 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
906 health_code_update(&consumer_data
->health
);
908 DBG2("Receiving code from consumer err_sock");
910 /* Getting status code from kconsumerd */
911 ret
= lttcomm_recv_unix_sock(sock
, &code
,
912 sizeof(enum lttcomm_return_code
));
917 health_code_update(&consumer_data
->health
);
919 if (code
== CONSUMERD_COMMAND_SOCK_READY
) {
920 consumer_data
->cmd_sock
=
921 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
922 if (consumer_data
->cmd_sock
< 0) {
923 sem_post(&consumer_data
->sem
);
924 PERROR("consumer connect");
927 /* Signal condition to tell that the kconsumerd is ready */
928 sem_post(&consumer_data
->sem
);
929 DBG("consumer command socket ready");
931 ERR("consumer error when waiting for SOCK_READY : %s",
932 lttcomm_get_readable_code(-code
));
936 /* Remove the kconsumerd error sock since we've established a connexion */
937 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
942 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
947 health_code_update(&consumer_data
->health
);
949 /* Update number of fd */
950 nb_fd
= LTTNG_POLL_GETNB(&events
);
952 /* Inifinite blocking call, waiting for transmission */
954 health_poll_update(&consumer_data
->health
);
955 ret
= lttng_poll_wait(&events
, -1);
956 health_poll_update(&consumer_data
->health
);
959 * Restart interrupted system call.
961 if (errno
== EINTR
) {
967 for (i
= 0; i
< nb_fd
; i
++) {
968 /* Fetch once the poll data */
969 revents
= LTTNG_POLL_GETEV(&events
, i
);
970 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
972 health_code_update(&consumer_data
->health
);
974 /* Thread quit pipe has been closed. Killing thread. */
975 ret
= check_thread_quit_pipe(pollfd
, revents
);
980 /* Event on the kconsumerd socket */
981 if (pollfd
== sock
) {
982 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
983 ERR("consumer err socket second poll error");
989 health_code_update(&consumer_data
->health
);
991 /* Wait for any kconsumerd error */
992 ret
= lttcomm_recv_unix_sock(sock
, &code
,
993 sizeof(enum lttcomm_return_code
));
995 ERR("consumer closed the command socket");
999 ERR("consumer return code : %s", lttcomm_get_readable_code(-code
));
1002 /* Immediately set the consumerd state to stopped */
1003 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1004 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1005 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1006 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1007 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1009 /* Code flow error... */
1013 if (consumer_data
->err_sock
>= 0) {
1014 ret
= close(consumer_data
->err_sock
);
1019 if (consumer_data
->cmd_sock
>= 0) {
1020 ret
= close(consumer_data
->cmd_sock
);
1032 unlink(consumer_data
->err_unix_sock_path
);
1033 unlink(consumer_data
->cmd_unix_sock_path
);
1034 consumer_data
->pid
= 0;
1036 lttng_poll_clean(&events
);
1039 health_reset(&consumer_data
->health
);
1040 DBG("consumer thread cleanup completed");
1046 * This thread manage application communication.
1048 static void *thread_manage_apps(void *data
)
1051 uint32_t revents
, nb_fd
;
1052 struct ust_command ust_cmd
;
1053 struct lttng_poll_event events
;
1055 DBG("[thread] Manage application started");
1057 rcu_register_thread();
1058 rcu_thread_online();
1060 health_code_update(&health_thread_app_reg
);
1062 ret
= create_thread_poll_set(&events
, 2);
1064 goto error_poll_create
;
1067 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1072 health_code_update(&health_thread_app_reg
);
1075 /* Zeroed the events structure */
1076 lttng_poll_reset(&events
);
1078 nb_fd
= LTTNG_POLL_GETNB(&events
);
1080 DBG("Apps thread polling on %d fds", nb_fd
);
1082 /* Inifinite blocking call, waiting for transmission */
1084 health_poll_update(&health_thread_app_reg
);
1085 ret
= lttng_poll_wait(&events
, -1);
1086 health_poll_update(&health_thread_app_reg
);
1089 * Restart interrupted system call.
1091 if (errno
== EINTR
) {
1097 for (i
= 0; i
< nb_fd
; i
++) {
1098 /* Fetch once the poll data */
1099 revents
= LTTNG_POLL_GETEV(&events
, i
);
1100 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1102 health_code_update(&health_thread_app_reg
);
1104 /* Thread quit pipe has been closed. Killing thread. */
1105 ret
= check_thread_quit_pipe(pollfd
, revents
);
1110 /* Inspect the apps cmd pipe */
1111 if (pollfd
== apps_cmd_pipe
[0]) {
1112 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1113 ERR("Apps command pipe error");
1115 } else if (revents
& LPOLLIN
) {
1117 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
1118 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
1119 PERROR("read apps cmd pipe");
1123 health_code_update(&health_thread_app_reg
);
1125 /* Register applicaton to the session daemon */
1126 ret
= ust_app_register(&ust_cmd
.reg_msg
,
1128 if (ret
== -ENOMEM
) {
1130 } else if (ret
< 0) {
1134 health_code_update(&health_thread_app_reg
);
1137 * Validate UST version compatibility.
1139 ret
= ust_app_validate_version(ust_cmd
.sock
);
1142 * Add channel(s) and event(s) to newly registered apps
1143 * from lttng global UST domain.
1145 update_ust_app(ust_cmd
.sock
);
1148 health_code_update(&health_thread_app_reg
);
1150 ret
= ust_app_register_done(ust_cmd
.sock
);
1153 * If the registration is not possible, we simply
1154 * unregister the apps and continue
1156 ust_app_unregister(ust_cmd
.sock
);
1159 * We just need here to monitor the close of the UST
1160 * socket and poll set monitor those by default.
1161 * Listen on POLLIN (even if we never expect any
1162 * data) to ensure that hangup wakes us.
1164 ret
= lttng_poll_add(&events
, ust_cmd
.sock
, LPOLLIN
);
1169 DBG("Apps with sock %d added to poll set",
1173 health_code_update(&health_thread_app_reg
);
1179 * At this point, we know that a registered application made
1180 * the event at poll_wait.
1182 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1183 /* Removing from the poll set */
1184 ret
= lttng_poll_del(&events
, pollfd
);
1189 /* Socket closed on remote end. */
1190 ust_app_unregister(pollfd
);
1195 health_code_update(&health_thread_app_reg
);
1200 lttng_poll_clean(&events
);
1202 health_reset(&health_thread_app_reg
);
1203 DBG("Application communication apps thread cleanup complete");
1204 rcu_thread_offline();
1205 rcu_unregister_thread();
1210 * Dispatch request from the registration threads to the application
1211 * communication thread.
1213 static void *thread_dispatch_ust_registration(void *data
)
1216 struct cds_wfq_node
*node
;
1217 struct ust_command
*ust_cmd
= NULL
;
1219 DBG("[thread] Dispatch UST command started");
1221 while (!dispatch_thread_exit
) {
1222 /* Atomically prepare the queue futex */
1223 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1226 /* Dequeue command for registration */
1227 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1229 DBG("Woken up but nothing in the UST command queue");
1230 /* Continue thread execution */
1234 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1236 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1237 " gid:%d sock:%d name:%s (version %d.%d)",
1238 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1239 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1240 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1241 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1243 * Inform apps thread of the new application registration. This
1244 * call is blocking so we can be assured that the data will be read
1245 * at some point in time or wait to the end of the world :)
1247 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1248 sizeof(struct ust_command
));
1250 PERROR("write apps cmd pipe");
1251 if (errno
== EBADF
) {
1253 * We can't inform the application thread to process
1254 * registration. We will exit or else application
1255 * registration will not occur and tracing will never
1262 } while (node
!= NULL
);
1264 /* Futex wait on queue. Blocking call on futex() */
1265 futex_nto1_wait(&ust_cmd_queue
.futex
);
1269 DBG("Dispatch thread dying");
1274 * This thread manage application registration.
1276 static void *thread_registration_apps(void *data
)
1278 int sock
= -1, i
, ret
, pollfd
;
1279 uint32_t revents
, nb_fd
;
1280 struct lttng_poll_event events
;
1282 * Get allocated in this thread, enqueued to a global queue, dequeued and
1283 * freed in the manage apps thread.
1285 struct ust_command
*ust_cmd
= NULL
;
1287 DBG("[thread] Manage application registration started");
1289 ret
= lttcomm_listen_unix_sock(apps_sock
);
1295 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1296 * more will be added to this poll set.
1298 ret
= create_thread_poll_set(&events
, 2);
1300 goto error_create_poll
;
1303 /* Add the application registration socket */
1304 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1306 goto error_poll_add
;
1309 /* Notify all applications to register */
1310 ret
= notify_ust_apps(1);
1312 ERR("Failed to notify applications or create the wait shared memory.\n"
1313 "Execution continues but there might be problem for already\n"
1314 "running applications that wishes to register.");
1318 DBG("Accepting application registration");
1320 nb_fd
= LTTNG_POLL_GETNB(&events
);
1322 /* Inifinite blocking call, waiting for transmission */
1324 ret
= lttng_poll_wait(&events
, -1);
1327 * Restart interrupted system call.
1329 if (errno
== EINTR
) {
1335 for (i
= 0; i
< nb_fd
; i
++) {
1336 /* Fetch once the poll data */
1337 revents
= LTTNG_POLL_GETEV(&events
, i
);
1338 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1340 /* Thread quit pipe has been closed. Killing thread. */
1341 ret
= check_thread_quit_pipe(pollfd
, revents
);
1346 /* Event on the registration socket */
1347 if (pollfd
== apps_sock
) {
1348 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1349 ERR("Register apps socket poll error");
1351 } else if (revents
& LPOLLIN
) {
1352 sock
= lttcomm_accept_unix_sock(apps_sock
);
1357 /* Create UST registration command for enqueuing */
1358 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1359 if (ust_cmd
== NULL
) {
1360 PERROR("ust command zmalloc");
1365 * Using message-based transmissions to ensure we don't
1366 * have to deal with partially received messages.
1368 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1370 ERR("Exhausted file descriptors allowed for applications.");
1379 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1380 sizeof(struct ust_register_msg
));
1381 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1383 PERROR("lttcomm_recv_unix_sock register apps");
1385 ERR("Wrong size received on apps register");
1392 lttng_fd_put(LTTNG_FD_APPS
, 1);
1397 ust_cmd
->sock
= sock
;
1400 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1401 " gid:%d sock:%d name:%s (version %d.%d)",
1402 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1403 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1404 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1405 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1408 * Lock free enqueue the registration request. The red pill
1409 * has been taken! This apps will be part of the *system*.
1411 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1414 * Wake the registration queue futex. Implicit memory
1415 * barrier with the exchange in cds_wfq_enqueue.
1417 futex_nto1_wake(&ust_cmd_queue
.futex
);
1424 /* Notify that the registration thread is gone */
1427 if (apps_sock
>= 0) {
1428 ret
= close(apps_sock
);
1438 lttng_fd_put(LTTNG_FD_APPS
, 1);
1440 unlink(apps_unix_sock_path
);
1443 lttng_poll_clean(&events
);
1446 DBG("UST Registration thread cleanup complete");
1452 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1453 * exec or it will fails.
1455 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
1458 struct timespec timeout
;
1460 timeout
.tv_sec
= DEFAULT_SEM_WAIT_TIMEOUT
;
1461 timeout
.tv_nsec
= 0;
1463 /* Setup semaphore */
1464 ret
= sem_init(&consumer_data
->sem
, 0, 0);
1466 PERROR("sem_init consumer semaphore");
1470 ret
= pthread_create(&consumer_data
->thread
, NULL
,
1471 thread_manage_consumer
, consumer_data
);
1473 PERROR("pthread_create consumer");
1478 /* Get time for sem_timedwait absolute timeout */
1479 ret
= clock_gettime(CLOCK_REALTIME
, &timeout
);
1481 PERROR("clock_gettime spawn consumer");
1482 /* Infinite wait for the kconsumerd thread to be ready */
1483 ret
= sem_wait(&consumer_data
->sem
);
1485 /* Normal timeout if the gettime was successful */
1486 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
1487 ret
= sem_timedwait(&consumer_data
->sem
, &timeout
);
1491 if (errno
== ETIMEDOUT
) {
1493 * Call has timed out so we kill the kconsumerd_thread and return
1496 ERR("The consumer thread was never ready. Killing it");
1497 ret
= pthread_cancel(consumer_data
->thread
);
1499 PERROR("pthread_cancel consumer thread");
1502 PERROR("semaphore wait failed consumer thread");
1507 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1508 if (consumer_data
->pid
== 0) {
1509 ERR("Kconsumerd did not start");
1510 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1513 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1522 * Join consumer thread
1524 static int join_consumer_thread(struct consumer_data
*consumer_data
)
1529 if (consumer_data
->pid
!= 0) {
1530 ret
= kill(consumer_data
->pid
, SIGTERM
);
1532 ERR("Error killing consumer daemon");
1535 return pthread_join(consumer_data
->thread
, &status
);
1542 * Fork and exec a consumer daemon (consumerd).
1544 * Return pid if successful else -1.
1546 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
1550 const char *consumer_to_use
;
1551 const char *verbosity
;
1554 DBG("Spawning consumerd");
1561 if (opt_verbose_consumer
) {
1562 verbosity
= "--verbose";
1564 verbosity
= "--quiet";
1566 switch (consumer_data
->type
) {
1567 case LTTNG_CONSUMER_KERNEL
:
1569 * Find out which consumerd to execute. We will first try the
1570 * 64-bit path, then the sessiond's installation directory, and
1571 * fallback on the 32-bit one,
1573 DBG3("Looking for a kernel consumer at these locations:");
1574 DBG3(" 1) %s", consumerd64_bin
);
1575 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
1576 DBG3(" 3) %s", consumerd32_bin
);
1577 if (stat(consumerd64_bin
, &st
) == 0) {
1578 DBG3("Found location #1");
1579 consumer_to_use
= consumerd64_bin
;
1580 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
1581 DBG3("Found location #2");
1582 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
1583 } else if (stat(consumerd32_bin
, &st
) == 0) {
1584 DBG3("Found location #3");
1585 consumer_to_use
= consumerd32_bin
;
1587 DBG("Could not find any valid consumerd executable");
1590 DBG("Using kernel consumer at: %s", consumer_to_use
);
1591 execl(consumer_to_use
,
1592 "lttng-consumerd", verbosity
, "-k",
1593 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1594 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1597 case LTTNG_CONSUMER64_UST
:
1599 char *tmpnew
= NULL
;
1601 if (consumerd64_libdir
[0] != '\0') {
1605 tmp
= getenv("LD_LIBRARY_PATH");
1609 tmplen
= strlen("LD_LIBRARY_PATH=")
1610 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
1611 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1616 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1617 strcat(tmpnew
, consumerd64_libdir
);
1618 if (tmp
[0] != '\0') {
1619 strcat(tmpnew
, ":");
1620 strcat(tmpnew
, tmp
);
1622 ret
= putenv(tmpnew
);
1628 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
1629 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
1630 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1631 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1633 if (consumerd64_libdir
[0] != '\0') {
1641 case LTTNG_CONSUMER32_UST
:
1643 char *tmpnew
= NULL
;
1645 if (consumerd32_libdir
[0] != '\0') {
1649 tmp
= getenv("LD_LIBRARY_PATH");
1653 tmplen
= strlen("LD_LIBRARY_PATH=")
1654 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
1655 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1660 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1661 strcat(tmpnew
, consumerd32_libdir
);
1662 if (tmp
[0] != '\0') {
1663 strcat(tmpnew
, ":");
1664 strcat(tmpnew
, tmp
);
1666 ret
= putenv(tmpnew
);
1672 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
1673 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
1674 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1675 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1677 if (consumerd32_libdir
[0] != '\0') {
1686 PERROR("unknown consumer type");
1690 PERROR("kernel start consumer exec");
1693 } else if (pid
> 0) {
1696 PERROR("start consumer fork");
1704 * Spawn the consumerd daemon and session daemon thread.
1706 static int start_consumerd(struct consumer_data
*consumer_data
)
1710 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1711 if (consumer_data
->pid
!= 0) {
1712 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1716 ret
= spawn_consumerd(consumer_data
);
1718 ERR("Spawning consumerd failed");
1719 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1723 /* Setting up the consumer_data pid */
1724 consumer_data
->pid
= ret
;
1725 DBG2("Consumer pid %d", consumer_data
->pid
);
1726 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1728 DBG2("Spawning consumer control thread");
1729 ret
= spawn_consumer_thread(consumer_data
);
1731 ERR("Fatal error spawning consumer control thread");
1743 * Compute health status of each consumer.
1745 static int check_consumer_health(void)
1750 health_check_state(&kconsumer_data
.health
) &
1751 health_check_state(&ustconsumer32_data
.health
) &
1752 health_check_state(&ustconsumer64_data
.health
);
1754 DBG3("Health consumer check %d", ret
);
1760 * Check version of the lttng-modules.
1762 static int validate_lttng_modules_version(void)
1764 return kernel_validate_version(kernel_tracer_fd
);
1768 * Setup necessary data for kernel tracer action.
1770 static int init_kernel_tracer(void)
1774 /* Modprobe lttng kernel modules */
1775 ret
= modprobe_lttng_control();
1780 /* Open debugfs lttng */
1781 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1782 if (kernel_tracer_fd
< 0) {
1783 DBG("Failed to open %s", module_proc_lttng
);
1788 /* Validate kernel version */
1789 ret
= validate_lttng_modules_version();
1794 ret
= modprobe_lttng_data();
1799 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1803 modprobe_remove_lttng_control();
1804 ret
= close(kernel_tracer_fd
);
1808 kernel_tracer_fd
= -1;
1809 return LTTCOMM_KERN_VERSION
;
1812 ret
= close(kernel_tracer_fd
);
1818 modprobe_remove_lttng_control();
1821 WARN("No kernel tracer available");
1822 kernel_tracer_fd
= -1;
1824 return LTTCOMM_NEED_ROOT_SESSIOND
;
1826 return LTTCOMM_KERN_NA
;
1831 * Init tracing by creating trace directory and sending fds kernel consumer.
1833 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
1837 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
1839 * Assign default kernel consumer socket if no consumer assigned to the
1840 * kernel session. At this point, it's NOT supposed to be -1 but this is
1841 * an extra security check.
1843 if (session
->consumer_fd
< 0) {
1844 session
->consumer_fd
= kconsumer_data
.cmd_sock
;
1847 ret
= kernel_consumer_send_session(session
->consumer_fd
, session
);
1849 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1859 * Create a socket to the relayd using the URI.
1861 * On success, the relayd_sock pointer is set to the created socket.
1862 * Else, it is untouched and an lttcomm error code is returned.
1864 static int create_connect_relayd(struct consumer_output
*output
,
1865 const char *session_name
, struct lttng_uri
*uri
,
1866 struct lttcomm_sock
**relayd_sock
)
1869 struct lttcomm_sock
*sock
;
1871 /* Create socket object from URI */
1872 sock
= lttcomm_alloc_sock_from_uri(uri
);
1874 ret
= LTTCOMM_FATAL
;
1878 ret
= lttcomm_create_sock(sock
);
1880 ret
= LTTCOMM_FATAL
;
1884 /* Connect to relayd so we can proceed with a session creation. */
1885 ret
= relayd_connect(sock
);
1887 ERR("Unable to reach lttng-relayd");
1888 ret
= LTTCOMM_RELAYD_SESSION_FAIL
;
1892 /* Create socket for control stream. */
1893 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
1894 DBG3("Creating relayd stream socket from URI");
1896 /* Check relayd version */
1897 ret
= relayd_version_check(sock
, LTTNG_UST_COMM_MAJOR
, 0);
1899 ret
= LTTCOMM_RELAYD_VERSION_FAIL
;
1902 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
1903 DBG3("Creating relayd data socket from URI");
1905 /* Command is not valid */
1906 ERR("Relayd invalid stream type: %d", uri
->stype
);
1907 ret
= LTTCOMM_INVALID
;
1911 *relayd_sock
= sock
;
1917 (void) relayd_close(sock
);
1921 lttcomm_destroy_sock(sock
);
1928 * Connect to the relayd using URI and send the socket to the right consumer.
1930 static int send_socket_relayd_consumer(int domain
, struct ltt_session
*session
,
1931 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
1935 struct lttcomm_sock
*sock
= NULL
;
1937 /* Set the network sequence index if not set. */
1938 if (consumer
->net_seq_index
== -1) {
1940 * Increment net_seq_idx because we are about to transfer the
1941 * new relayd socket to the consumer.
1943 uatomic_inc(&relayd_net_seq_idx
);
1944 /* Assign unique key so the consumer can match streams */
1945 consumer
->net_seq_index
= uatomic_read(&relayd_net_seq_idx
);
1948 /* Connect to relayd and make version check if uri is the control. */
1949 ret
= create_connect_relayd(consumer
, session
->name
, relayd_uri
, &sock
);
1950 if (ret
!= LTTCOMM_OK
) {
1954 /* If the control socket is connected, network session is ready */
1955 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1956 session
->net_handle
= 1;
1960 case LTTNG_DOMAIN_KERNEL
:
1961 /* Send relayd socket to consumer. */
1962 ret
= kernel_consumer_send_relayd_socket(consumer_fd
, sock
,
1963 consumer
, relayd_uri
->stype
);
1965 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
1969 case LTTNG_DOMAIN_UST
:
1970 /* Send relayd socket to consumer. */
1971 ret
= ust_consumer_send_relayd_socket(consumer_fd
, sock
,
1972 consumer
, relayd_uri
->stype
);
1974 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
1983 * Close socket which was dup on the consumer side. The session daemon does
1984 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1989 (void) relayd_close(sock
);
1990 lttcomm_destroy_sock(sock
);
1997 * Send both relayd sockets to a specific consumer and domain. This is a
1998 * helper function to facilitate sending the information to the consumer for a
2001 static int send_sockets_relayd_consumer(int domain
,
2002 struct ltt_session
*session
, struct consumer_output
*consumer
, int fd
)
2006 /* Sending control relayd socket. */
2007 ret
= send_socket_relayd_consumer(domain
, session
,
2008 &consumer
->dst
.net
.control
, consumer
, fd
);
2009 if (ret
!= LTTCOMM_OK
) {
2013 /* Sending data relayd socket. */
2014 ret
= send_socket_relayd_consumer(domain
, session
,
2015 &consumer
->dst
.net
.data
, consumer
, fd
);
2016 if (ret
!= LTTCOMM_OK
) {
2025 * Setup relayd connections for a tracing session. First creates the socket to
2026 * the relayd and send them to the right domain consumer. Consumer type MUST be
2029 static int setup_relayd(struct ltt_session
*session
)
2031 int ret
= LTTCOMM_OK
;
2032 struct ltt_ust_session
*usess
;
2033 struct ltt_kernel_session
*ksess
;
2037 usess
= session
->ust_session
;
2038 ksess
= session
->kernel_session
;
2040 DBG2("Setting relayd for session %s", session
->name
);
2042 if (usess
&& usess
->consumer
->sock
== -1 &&
2043 usess
->consumer
->type
== CONSUMER_DST_NET
&&
2044 usess
->consumer
->enabled
) {
2045 /* Setup relayd for 64 bits consumer */
2046 if (ust_consumerd64_fd
>= 0) {
2047 send_sockets_relayd_consumer(LTTNG_DOMAIN_UST
, session
,
2048 usess
->consumer
, ust_consumerd64_fd
);
2049 if (ret
!= LTTCOMM_OK
) {
2054 /* Setup relayd for 32 bits consumer */
2055 if (ust_consumerd32_fd
>= 0) {
2056 send_sockets_relayd_consumer(LTTNG_DOMAIN_UST
, session
,
2057 usess
->consumer
, ust_consumerd32_fd
);
2058 if (ret
!= LTTCOMM_OK
) {
2062 } else if (ksess
&& ksess
->consumer
->sock
== -1 &&
2063 ksess
->consumer
->type
== CONSUMER_DST_NET
&&
2064 ksess
->consumer
->enabled
) {
2065 send_sockets_relayd_consumer(LTTNG_DOMAIN_KERNEL
, session
,
2066 ksess
->consumer
, ksess
->consumer_fd
);
2067 if (ret
!= LTTCOMM_OK
) {
2077 * Copy consumer output from the tracing session to the domain session. The
2078 * function also applies the right modification on a per domain basis for the
2079 * trace files destination directory.
2081 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2084 const char *dir_name
;
2085 struct consumer_output
*consumer
;
2088 case LTTNG_DOMAIN_KERNEL
:
2089 DBG3("Copying tracing session consumer output in kernel session");
2090 session
->kernel_session
->consumer
=
2091 consumer_copy_output(session
->consumer
);
2092 /* Ease our life a bit for the next part */
2093 consumer
= session
->kernel_session
->consumer
;
2094 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2096 case LTTNG_DOMAIN_UST
:
2097 DBG3("Copying tracing session consumer output in UST session");
2098 session
->ust_session
->consumer
=
2099 consumer_copy_output(session
->consumer
);
2100 /* Ease our life a bit for the next part */
2101 consumer
= session
->ust_session
->consumer
;
2102 dir_name
= DEFAULT_UST_TRACE_DIR
;
2105 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2109 /* Append correct directory to subdir */
2110 strncat(consumer
->subdir
, dir_name
, sizeof(consumer
->subdir
));
2111 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2113 /* Add default trace directory name */
2114 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
2115 strncat(consumer
->dst
.trace_path
, dir_name
,
2116 sizeof(consumer
->dst
.trace_path
));
2126 * Create an UST session and add it to the session ust list.
2128 static int create_ust_session(struct ltt_session
*session
,
2129 struct lttng_domain
*domain
)
2132 struct ltt_ust_session
*lus
= NULL
;
2135 assert(session
->consumer
);
2137 switch (domain
->type
) {
2138 case LTTNG_DOMAIN_UST
:
2141 ERR("Unknown UST domain on create session %d", domain
->type
);
2142 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2146 DBG("Creating UST session");
2148 lus
= trace_ust_create_session(session
->path
, session
->id
, domain
);
2150 ret
= LTTCOMM_UST_SESS_FAIL
;
2154 if (session
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2155 ret
= run_as_mkdir_recursive(lus
->pathname
, S_IRWXU
| S_IRWXG
,
2156 session
->uid
, session
->gid
);
2158 if (ret
!= -EEXIST
) {
2159 ERR("Trace directory creation error");
2160 ret
= LTTCOMM_UST_SESS_FAIL
;
2166 lus
->uid
= session
->uid
;
2167 lus
->gid
= session
->gid
;
2168 session
->ust_session
= lus
;
2170 /* Copy session output to the newly created UST session */
2171 ret
= copy_session_consumer(domain
->type
, session
);
2172 if (ret
!= LTTCOMM_OK
) {
2180 session
->ust_session
= NULL
;
2185 * Create a kernel tracer session then create the default channel.
2187 static int create_kernel_session(struct ltt_session
*session
)
2191 DBG("Creating kernel session");
2193 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2195 ret
= LTTCOMM_KERN_SESS_FAIL
;
2199 /* Set kernel consumer socket fd */
2200 if (kconsumer_data
.cmd_sock
>= 0) {
2201 session
->kernel_session
->consumer_fd
= kconsumer_data
.cmd_sock
;
2204 /* Copy session output to the newly created Kernel session */
2205 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2206 if (ret
!= LTTCOMM_OK
) {
2210 /* Create directory(ies) on local filesystem. */
2211 if (session
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2212 ret
= run_as_mkdir_recursive(
2213 session
->kernel_session
->consumer
->dst
.trace_path
,
2214 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2216 if (ret
!= -EEXIST
) {
2217 ERR("Trace directory creation error");
2223 session
->kernel_session
->uid
= session
->uid
;
2224 session
->kernel_session
->gid
= session
->gid
;
2229 trace_kernel_destroy_session(session
->kernel_session
);
2230 session
->kernel_session
= NULL
;
2235 * Check if the UID or GID match the session. Root user has access to all
2238 static int session_access_ok(struct ltt_session
*session
, uid_t uid
, gid_t gid
)
2240 if (uid
!= session
->uid
&& gid
!= session
->gid
&& uid
!= 0) {
2248 * Count number of session permitted by uid/gid.
2250 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2253 struct ltt_session
*session
;
2255 DBG("Counting number of available session for UID %d GID %d",
2257 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2259 * Only list the sessions the user can control.
2261 if (!session_access_ok(session
, uid
, gid
)) {
2270 * Using the session list, filled a lttng_session array to send back to the
2271 * client for session listing.
2273 * The session list lock MUST be acquired before calling this function. Use
2274 * session_lock_list() and session_unlock_list().
2276 static void list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2280 struct ltt_session
*session
;
2282 DBG("Getting all available session for UID %d GID %d",
2285 * Iterate over session list and append data after the control struct in
2288 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2290 * Only list the sessions the user can control.
2292 if (!session_access_ok(session
, uid
, gid
)) {
2295 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
2296 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
2297 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2298 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2299 sessions
[i
].enabled
= session
->enabled
;
2305 * Fill lttng_channel array of all channels.
2307 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
2308 struct lttng_channel
*channels
)
2311 struct ltt_kernel_channel
*kchan
;
2313 DBG("Listing channels for session %s", session
->name
);
2316 case LTTNG_DOMAIN_KERNEL
:
2317 /* Kernel channels */
2318 if (session
->kernel_session
!= NULL
) {
2319 cds_list_for_each_entry(kchan
,
2320 &session
->kernel_session
->channel_list
.head
, list
) {
2321 /* Copy lttng_channel struct to array */
2322 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
2323 channels
[i
].enabled
= kchan
->enabled
;
2328 case LTTNG_DOMAIN_UST
:
2330 struct lttng_ht_iter iter
;
2331 struct ltt_ust_channel
*uchan
;
2333 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
2334 &iter
.iter
, uchan
, node
.node
) {
2335 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
2336 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
2337 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2338 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2339 channels
[i
].attr
.switch_timer_interval
=
2340 uchan
->attr
.switch_timer_interval
;
2341 channels
[i
].attr
.read_timer_interval
=
2342 uchan
->attr
.read_timer_interval
;
2343 channels
[i
].enabled
= uchan
->enabled
;
2344 switch (uchan
->attr
.output
) {
2345 case LTTNG_UST_MMAP
:
2347 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
2360 * Create a list of ust global domain events.
2362 static int list_lttng_ust_global_events(char *channel_name
,
2363 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
2366 unsigned int nb_event
= 0;
2367 struct lttng_ht_iter iter
;
2368 struct lttng_ht_node_str
*node
;
2369 struct ltt_ust_channel
*uchan
;
2370 struct ltt_ust_event
*uevent
;
2371 struct lttng_event
*tmp
;
2373 DBG("Listing UST global events for channel %s", channel_name
);
2377 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
2378 node
= lttng_ht_iter_get_node_str(&iter
);
2380 ret
= -LTTCOMM_UST_CHAN_NOT_FOUND
;
2384 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
2386 nb_event
+= lttng_ht_get_count(uchan
->events
);
2388 if (nb_event
== 0) {
2393 DBG3("Listing UST global %d events", nb_event
);
2395 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
2397 ret
= -LTTCOMM_FATAL
;
2401 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
2402 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
2403 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
2404 tmp
[i
].enabled
= uevent
->enabled
;
2405 switch (uevent
->attr
.instrumentation
) {
2406 case LTTNG_UST_TRACEPOINT
:
2407 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2409 case LTTNG_UST_PROBE
:
2410 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
2412 case LTTNG_UST_FUNCTION
:
2413 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
2416 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
2417 switch (uevent
->attr
.loglevel_type
) {
2418 case LTTNG_UST_LOGLEVEL_ALL
:
2419 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2421 case LTTNG_UST_LOGLEVEL_RANGE
:
2422 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
2424 case LTTNG_UST_LOGLEVEL_SINGLE
:
2425 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
2428 if (uevent
->filter
) {
2443 * Fill lttng_event array of all kernel events in the channel.
2445 static int list_lttng_kernel_events(char *channel_name
,
2446 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
2449 unsigned int nb_event
;
2450 struct ltt_kernel_event
*event
;
2451 struct ltt_kernel_channel
*kchan
;
2453 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
2454 if (kchan
== NULL
) {
2455 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2459 nb_event
= kchan
->event_count
;
2461 DBG("Listing events for channel %s", kchan
->channel
->name
);
2463 if (nb_event
== 0) {
2468 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
2469 if (*events
== NULL
) {
2470 ret
= LTTCOMM_FATAL
;
2474 /* Kernel channels */
2475 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
2476 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
2477 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
2478 (*events
)[i
].enabled
= event
->enabled
;
2479 switch (event
->event
->instrumentation
) {
2480 case LTTNG_KERNEL_TRACEPOINT
:
2481 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2483 case LTTNG_KERNEL_KPROBE
:
2484 case LTTNG_KERNEL_KRETPROBE
:
2485 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
2486 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
2487 sizeof(struct lttng_kernel_kprobe
));
2489 case LTTNG_KERNEL_FUNCTION
:
2490 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
2491 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
2492 sizeof(struct lttng_kernel_function
));
2494 case LTTNG_KERNEL_NOOP
:
2495 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
2497 case LTTNG_KERNEL_SYSCALL
:
2498 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
2500 case LTTNG_KERNEL_ALL
:
2514 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
2516 static int cmd_disable_channel(struct ltt_session
*session
,
2517 int domain
, char *channel_name
)
2520 struct ltt_ust_session
*usess
;
2522 usess
= session
->ust_session
;
2525 case LTTNG_DOMAIN_KERNEL
:
2527 ret
= channel_kernel_disable(session
->kernel_session
,
2529 if (ret
!= LTTCOMM_OK
) {
2533 kernel_wait_quiescent(kernel_tracer_fd
);
2536 case LTTNG_DOMAIN_UST
:
2538 struct ltt_ust_channel
*uchan
;
2539 struct lttng_ht
*chan_ht
;
2541 chan_ht
= usess
->domain_global
.channels
;
2543 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
2544 if (uchan
== NULL
) {
2545 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2549 ret
= channel_ust_disable(usess
, domain
, uchan
);
2550 if (ret
!= LTTCOMM_OK
) {
2556 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2557 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2558 case LTTNG_DOMAIN_UST_PID
:
2561 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2572 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
2574 static int cmd_enable_channel(struct ltt_session
*session
,
2575 int domain
, struct lttng_channel
*attr
)
2578 struct ltt_ust_session
*usess
= session
->ust_session
;
2579 struct lttng_ht
*chan_ht
;
2581 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
2584 case LTTNG_DOMAIN_KERNEL
:
2586 struct ltt_kernel_channel
*kchan
;
2588 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
2589 session
->kernel_session
);
2590 if (kchan
== NULL
) {
2591 ret
= channel_kernel_create(session
->kernel_session
,
2592 attr
, kernel_poll_pipe
[1]);
2594 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
2597 if (ret
!= LTTCOMM_OK
) {
2601 kernel_wait_quiescent(kernel_tracer_fd
);
2604 case LTTNG_DOMAIN_UST
:
2606 struct ltt_ust_channel
*uchan
;
2608 chan_ht
= usess
->domain_global
.channels
;
2610 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
2611 if (uchan
== NULL
) {
2612 ret
= channel_ust_create(usess
, domain
, attr
);
2614 ret
= channel_ust_enable(usess
, domain
, uchan
);
2619 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2620 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2621 case LTTNG_DOMAIN_UST_PID
:
2624 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2633 * Command LTTNG_DISABLE_EVENT processed by the client thread.
2635 static int cmd_disable_event(struct ltt_session
*session
, int domain
,
2636 char *channel_name
, char *event_name
)
2641 case LTTNG_DOMAIN_KERNEL
:
2643 struct ltt_kernel_channel
*kchan
;
2644 struct ltt_kernel_session
*ksess
;
2646 ksess
= session
->kernel_session
;
2648 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
2649 if (kchan
== NULL
) {
2650 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2654 ret
= event_kernel_disable_tracepoint(ksess
, kchan
, event_name
);
2655 if (ret
!= LTTCOMM_OK
) {
2659 kernel_wait_quiescent(kernel_tracer_fd
);
2662 case LTTNG_DOMAIN_UST
:
2664 struct ltt_ust_channel
*uchan
;
2665 struct ltt_ust_session
*usess
;
2667 usess
= session
->ust_session
;
2669 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2671 if (uchan
== NULL
) {
2672 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2676 ret
= event_ust_disable_tracepoint(usess
, domain
, uchan
, event_name
);
2677 if (ret
!= LTTCOMM_OK
) {
2681 DBG3("Disable UST event %s in channel %s completed", event_name
,
2686 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2687 case LTTNG_DOMAIN_UST_PID
:
2688 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2702 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
2704 static int cmd_disable_event_all(struct ltt_session
*session
, int domain
,
2710 case LTTNG_DOMAIN_KERNEL
:
2712 struct ltt_kernel_session
*ksess
;
2713 struct ltt_kernel_channel
*kchan
;
2715 ksess
= session
->kernel_session
;
2717 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
2718 if (kchan
== NULL
) {
2719 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2723 ret
= event_kernel_disable_all(ksess
, kchan
);
2724 if (ret
!= LTTCOMM_OK
) {
2728 kernel_wait_quiescent(kernel_tracer_fd
);
2731 case LTTNG_DOMAIN_UST
:
2733 struct ltt_ust_session
*usess
;
2734 struct ltt_ust_channel
*uchan
;
2736 usess
= session
->ust_session
;
2738 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2740 if (uchan
== NULL
) {
2741 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2745 ret
= event_ust_disable_all_tracepoints(usess
, domain
, uchan
);
2750 DBG3("Disable all UST events in channel %s completed", channel_name
);
2755 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2756 case LTTNG_DOMAIN_UST_PID
:
2757 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2771 * Command LTTNG_ADD_CONTEXT processed by the client thread.
2773 static int cmd_add_context(struct ltt_session
*session
, int domain
,
2774 char *channel_name
, char *event_name
, struct lttng_event_context
*ctx
)
2779 case LTTNG_DOMAIN_KERNEL
:
2780 /* Add kernel context to kernel tracer */
2781 ret
= context_kernel_add(session
->kernel_session
, ctx
,
2782 event_name
, channel_name
);
2783 if (ret
!= LTTCOMM_OK
) {
2787 case LTTNG_DOMAIN_UST
:
2789 struct ltt_ust_session
*usess
= session
->ust_session
;
2791 ret
= context_ust_add(usess
, domain
, ctx
, event_name
, channel_name
);
2792 if (ret
!= LTTCOMM_OK
) {
2798 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2799 case LTTNG_DOMAIN_UST_PID
:
2800 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2814 * Command LTTNG_SET_FILTER processed by the client thread.
2816 static int cmd_set_filter(struct ltt_session
*session
, int domain
,
2817 char *channel_name
, char *event_name
,
2818 struct lttng_filter_bytecode
*bytecode
)
2823 case LTTNG_DOMAIN_KERNEL
:
2824 ret
= LTTCOMM_FATAL
;
2826 case LTTNG_DOMAIN_UST
:
2828 struct ltt_ust_session
*usess
= session
->ust_session
;
2830 ret
= filter_ust_set(usess
, domain
, bytecode
, event_name
, channel_name
);
2831 if (ret
!= LTTCOMM_OK
) {
2837 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2838 case LTTNG_DOMAIN_UST_PID
:
2839 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2854 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2856 static int cmd_enable_event(struct ltt_session
*session
, int domain
,
2857 char *channel_name
, struct lttng_event
*event
)
2860 struct lttng_channel
*attr
;
2861 struct ltt_ust_session
*usess
= session
->ust_session
;
2864 case LTTNG_DOMAIN_KERNEL
:
2866 struct ltt_kernel_channel
*kchan
;
2868 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2869 session
->kernel_session
);
2870 if (kchan
== NULL
) {
2871 attr
= channel_new_default_attr(domain
);
2873 ret
= LTTCOMM_FATAL
;
2876 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
2878 /* This call will notify the kernel thread */
2879 ret
= channel_kernel_create(session
->kernel_session
,
2880 attr
, kernel_poll_pipe
[1]);
2881 if (ret
!= LTTCOMM_OK
) {
2888 /* Get the newly created kernel channel pointer */
2889 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2890 session
->kernel_session
);
2891 if (kchan
== NULL
) {
2892 /* This sould not happen... */
2893 ret
= LTTCOMM_FATAL
;
2897 ret
= event_kernel_enable_tracepoint(session
->kernel_session
, kchan
,
2899 if (ret
!= LTTCOMM_OK
) {
2903 kernel_wait_quiescent(kernel_tracer_fd
);
2906 case LTTNG_DOMAIN_UST
:
2908 struct lttng_channel
*attr
;
2909 struct ltt_ust_channel
*uchan
;
2911 /* Get channel from global UST domain */
2912 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2914 if (uchan
== NULL
) {
2915 /* Create default channel */
2916 attr
= channel_new_default_attr(domain
);
2918 ret
= LTTCOMM_FATAL
;
2921 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
2922 attr
->name
[NAME_MAX
- 1] = '\0';
2924 ret
= channel_ust_create(usess
, domain
, attr
);
2925 if (ret
!= LTTCOMM_OK
) {
2931 /* Get the newly created channel reference back */
2932 uchan
= trace_ust_find_channel_by_name(
2933 usess
->domain_global
.channels
, channel_name
);
2934 if (uchan
== NULL
) {
2935 /* Something is really wrong */
2936 ret
= LTTCOMM_FATAL
;
2941 /* At this point, the session and channel exist on the tracer */
2942 ret
= event_ust_enable_tracepoint(usess
, domain
, uchan
, event
);
2943 if (ret
!= LTTCOMM_OK
) {
2949 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2950 case LTTNG_DOMAIN_UST_PID
:
2951 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2965 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
2967 static int cmd_enable_event_all(struct ltt_session
*session
, int domain
,
2968 char *channel_name
, int event_type
)
2971 struct ltt_kernel_channel
*kchan
;
2974 case LTTNG_DOMAIN_KERNEL
:
2975 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2976 session
->kernel_session
);
2977 if (kchan
== NULL
) {
2978 /* This call will notify the kernel thread */
2979 ret
= channel_kernel_create(session
->kernel_session
, NULL
,
2980 kernel_poll_pipe
[1]);
2981 if (ret
!= LTTCOMM_OK
) {
2985 /* Get the newly created kernel channel pointer */
2986 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2987 session
->kernel_session
);
2988 if (kchan
== NULL
) {
2989 /* This sould not happen... */
2990 ret
= LTTCOMM_FATAL
;
2996 switch (event_type
) {
2997 case LTTNG_EVENT_SYSCALL
:
2998 ret
= event_kernel_enable_all_syscalls(session
->kernel_session
,
2999 kchan
, kernel_tracer_fd
);
3001 case LTTNG_EVENT_TRACEPOINT
:
3003 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
3004 * events already registered to the channel.
3006 ret
= event_kernel_enable_all_tracepoints(session
->kernel_session
,
3007 kchan
, kernel_tracer_fd
);
3009 case LTTNG_EVENT_ALL
:
3010 /* Enable syscalls and tracepoints */
3011 ret
= event_kernel_enable_all(session
->kernel_session
,
3012 kchan
, kernel_tracer_fd
);
3015 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
3019 /* Manage return value */
3020 if (ret
!= LTTCOMM_OK
) {
3024 kernel_wait_quiescent(kernel_tracer_fd
);
3026 case LTTNG_DOMAIN_UST
:
3028 struct lttng_channel
*attr
;
3029 struct ltt_ust_channel
*uchan
;
3030 struct ltt_ust_session
*usess
= session
->ust_session
;
3032 /* Get channel from global UST domain */
3033 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
3035 if (uchan
== NULL
) {
3036 /* Create default channel */
3037 attr
= channel_new_default_attr(domain
);
3039 ret
= LTTCOMM_FATAL
;
3042 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
3043 attr
->name
[NAME_MAX
- 1] = '\0';
3045 /* Use the internal command enable channel */
3046 ret
= channel_ust_create(usess
, domain
, attr
);
3047 if (ret
!= LTTCOMM_OK
) {
3053 /* Get the newly created channel reference back */
3054 uchan
= trace_ust_find_channel_by_name(
3055 usess
->domain_global
.channels
, channel_name
);
3056 if (uchan
== NULL
) {
3057 /* Something is really wrong */
3058 ret
= LTTCOMM_FATAL
;
3063 /* At this point, the session and channel exist on the tracer */
3065 switch (event_type
) {
3066 case LTTNG_EVENT_ALL
:
3067 case LTTNG_EVENT_TRACEPOINT
:
3068 ret
= event_ust_enable_all_tracepoints(usess
, domain
, uchan
);
3069 if (ret
!= LTTCOMM_OK
) {
3074 ret
= LTTCOMM_UST_ENABLE_FAIL
;
3078 /* Manage return value */
3079 if (ret
!= LTTCOMM_OK
) {
3086 case LTTNG_DOMAIN_UST_EXEC_NAME
:
3087 case LTTNG_DOMAIN_UST_PID
:
3088 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
3102 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
3104 static ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
3107 ssize_t nb_events
= 0;
3110 case LTTNG_DOMAIN_KERNEL
:
3111 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
3112 if (nb_events
< 0) {
3113 ret
= LTTCOMM_KERN_LIST_FAIL
;
3117 case LTTNG_DOMAIN_UST
:
3118 nb_events
= ust_app_list_events(events
);
3119 if (nb_events
< 0) {
3120 ret
= LTTCOMM_UST_LIST_FAIL
;
3132 /* Return negative value to differentiate return code */
3137 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
3139 static ssize_t
cmd_list_tracepoint_fields(int domain
,
3140 struct lttng_event_field
**fields
)
3143 ssize_t nb_fields
= 0;
3146 case LTTNG_DOMAIN_UST
:
3147 nb_fields
= ust_app_list_event_fields(fields
);
3148 if (nb_fields
< 0) {
3149 ret
= LTTCOMM_UST_LIST_FAIL
;
3153 case LTTNG_DOMAIN_KERNEL
:
3154 default: /* fall-through */
3162 /* Return negative value to differentiate return code */
3167 * Command LTTNG_START_TRACE processed by the client thread.
3169 static int cmd_start_trace(struct ltt_session
*session
)
3172 struct ltt_kernel_session
*ksession
;
3173 struct ltt_ust_session
*usess
;
3174 struct ltt_kernel_channel
*kchan
;
3176 /* Ease our life a bit ;) */
3177 ksession
= session
->kernel_session
;
3178 usess
= session
->ust_session
;
3180 if (session
->enabled
) {
3181 /* Already started. */
3182 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
3186 session
->enabled
= 1;
3188 ret
= setup_relayd(session
);
3189 if (ret
!= LTTCOMM_OK
) {
3190 ERR("Error setting up relayd for session %s", session
->name
);
3194 /* Kernel tracing */
3195 if (ksession
!= NULL
) {
3196 /* Open kernel metadata */
3197 if (ksession
->metadata
== NULL
) {
3198 ret
= kernel_open_metadata(ksession
,
3199 ksession
->consumer
->dst
.trace_path
);
3201 ret
= LTTCOMM_KERN_META_FAIL
;
3206 /* Open kernel metadata stream */
3207 if (ksession
->metadata_stream_fd
< 0) {
3208 ret
= kernel_open_metadata_stream(ksession
);
3210 ERR("Kernel create metadata stream failed");
3211 ret
= LTTCOMM_KERN_STREAM_FAIL
;
3216 /* For each channel */
3217 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
3218 if (kchan
->stream_count
== 0) {
3219 ret
= kernel_open_channel_stream(kchan
);
3221 ret
= LTTCOMM_KERN_STREAM_FAIL
;
3224 /* Update the stream global counter */
3225 ksession
->stream_count_global
+= ret
;
3229 /* Setup kernel consumer socket and send fds to it */
3230 ret
= init_kernel_tracing(ksession
);
3232 ret
= LTTCOMM_KERN_START_FAIL
;
3236 /* This start the kernel tracing */
3237 ret
= kernel_start_session(ksession
);
3239 ret
= LTTCOMM_KERN_START_FAIL
;
3243 /* Quiescent wait after starting trace */
3244 kernel_wait_quiescent(kernel_tracer_fd
);
3247 /* Flag session that trace should start automatically */
3249 usess
->start_trace
= 1;
3251 ret
= ust_app_start_trace_all(usess
);
3253 ret
= LTTCOMM_UST_START_FAIL
;
3265 * Command LTTNG_STOP_TRACE processed by the client thread.
3267 static int cmd_stop_trace(struct ltt_session
*session
)
3270 struct ltt_kernel_channel
*kchan
;
3271 struct ltt_kernel_session
*ksession
;
3272 struct ltt_ust_session
*usess
;
3275 ksession
= session
->kernel_session
;
3276 usess
= session
->ust_session
;
3278 if (!session
->enabled
) {
3279 ret
= LTTCOMM_TRACE_ALREADY_STOPPED
;
3283 session
->enabled
= 0;
3286 if (ksession
!= NULL
) {
3287 DBG("Stop kernel tracing");
3289 /* Flush metadata if exist */
3290 if (ksession
->metadata_stream_fd
>= 0) {
3291 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
3293 ERR("Kernel metadata flush failed");
3297 /* Flush all buffers before stopping */
3298 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
3299 ret
= kernel_flush_buffer(kchan
);
3301 ERR("Kernel flush buffer error");
3305 ret
= kernel_stop_session(ksession
);
3307 ret
= LTTCOMM_KERN_STOP_FAIL
;
3311 kernel_wait_quiescent(kernel_tracer_fd
);
3315 usess
->start_trace
= 0;
3317 ret
= ust_app_stop_trace_all(usess
);
3319 ret
= LTTCOMM_UST_STOP_FAIL
;
3331 * Command LTTNG_CREATE_SESSION_URI processed by the client thread.
3333 static int cmd_create_session_uri(char *name
, struct lttng_uri
*ctrl_uri
,
3334 struct lttng_uri
*data_uri
, unsigned int enable_consumer
,
3335 lttng_sock_cred
*creds
)
3339 struct ltt_session
*session
;
3340 struct consumer_output
*consumer
;
3342 /* Verify if the session already exist */
3343 session
= session_find_by_name(name
);
3344 if (session
!= NULL
) {
3345 ret
= LTTCOMM_EXIST_SESS
;
3349 /* TODO: validate URIs */
3351 /* Create default consumer output */
3352 consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
3353 if (consumer
== NULL
) {
3354 ret
= LTTCOMM_FATAL
;
3357 strncpy(consumer
->subdir
, ctrl_uri
->subdir
, sizeof(consumer
->subdir
));
3358 DBG2("Consumer subdir set to %s", consumer
->subdir
);
3360 switch (ctrl_uri
->dtype
) {
3361 case LTTNG_DST_IPV4
:
3362 case LTTNG_DST_IPV6
:
3363 /* Set control URI into consumer output object */
3364 ret
= consumer_set_network_uri(consumer
, ctrl_uri
);
3366 ret
= LTTCOMM_FATAL
;
3370 /* Set data URI into consumer output object */
3371 ret
= consumer_set_network_uri(consumer
, data_uri
);
3373 ret
= LTTCOMM_FATAL
;
3377 /* Empty path since the session is network */
3380 case LTTNG_DST_PATH
:
3381 /* Very volatile pointer. Only used for the create session. */
3382 path
= ctrl_uri
->dst
.path
;
3383 strncpy(consumer
->dst
.trace_path
, path
,
3384 sizeof(consumer
->dst
.trace_path
));
3388 /* Set if the consumer is enabled or not */
3389 consumer
->enabled
= enable_consumer
;
3391 ret
= session_create(name
, path
, LTTNG_SOCK_GET_UID_CRED(creds
),
3392 LTTNG_SOCK_GET_GID_CRED(creds
));
3393 if (ret
!= LTTCOMM_OK
) {
3394 goto consumer_error
;
3397 /* Get the newly created session pointer back */
3398 session
= session_find_by_name(name
);
3401 /* Assign consumer to session */
3402 session
->consumer
= consumer
;
3407 consumer_destroy_output(consumer
);
3413 * Command LTTNG_CREATE_SESSION processed by the client thread.
3415 static int cmd_create_session(char *name
, char *path
, lttng_sock_cred
*creds
)
3418 struct lttng_uri uri
;
3420 /* Zeroed temporary URI */
3421 memset(&uri
, 0, sizeof(uri
));
3423 uri
.dtype
= LTTNG_DST_PATH
;
3424 uri
.utype
= LTTNG_URI_DST
;
3425 strncpy(uri
.dst
.path
, path
, sizeof(uri
.dst
.path
));
3427 /* TODO: Strip date-time from path and put it in uri's subdir */
3429 ret
= cmd_create_session_uri(name
, &uri
, NULL
, 1, creds
);
3430 if (ret
!= LTTCOMM_OK
) {
3439 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3441 static int cmd_destroy_session(struct ltt_session
*session
, char *name
)
3445 /* Clean kernel session teardown */
3446 teardown_kernel_session(session
);
3447 /* UST session teardown */
3448 teardown_ust_session(session
);
3451 * Must notify the kernel thread here to update it's poll setin order
3452 * to remove the channel(s)' fd just destroyed.
3454 ret
= notify_thread_pipe(kernel_poll_pipe
[1]);
3456 PERROR("write kernel poll pipe");
3459 ret
= session_destroy(session
);
3465 * Command LTTNG_CALIBRATE processed by the client thread.
3467 static int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
3472 case LTTNG_DOMAIN_KERNEL
:
3474 struct lttng_kernel_calibrate kcalibrate
;
3476 kcalibrate
.type
= calibrate
->type
;
3477 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
3479 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
3484 case LTTNG_DOMAIN_UST
:
3486 struct lttng_ust_calibrate ucalibrate
;
3488 ucalibrate
.type
= calibrate
->type
;
3489 ret
= ust_app_calibrate_glb(&ucalibrate
);
3491 ret
= LTTCOMM_UST_CALIBRATE_FAIL
;
3508 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3510 static int cmd_register_consumer(struct ltt_session
*session
, int domain
,
3516 case LTTNG_DOMAIN_KERNEL
:
3517 /* Can't register a consumer if there is already one */
3518 if (session
->kernel_session
->consumer_fds_sent
!= 0) {
3519 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
3523 sock
= lttcomm_connect_unix_sock(sock_path
);
3525 ret
= LTTCOMM_CONNECT_FAIL
;
3529 session
->kernel_session
->consumer_fd
= sock
;
3532 /* TODO: Userspace tracing */
3544 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3546 static ssize_t
cmd_list_domains(struct ltt_session
*session
,
3547 struct lttng_domain
**domains
)
3552 if (session
->kernel_session
!= NULL
) {
3553 DBG3("Listing domains found kernel domain");
3557 if (session
->ust_session
!= NULL
) {
3558 DBG3("Listing domains found UST global domain");
3562 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3563 if (*domains
== NULL
) {
3564 ret
= -LTTCOMM_FATAL
;
3568 if (session
->kernel_session
!= NULL
) {
3569 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3573 if (session
->ust_session
!= NULL
) {
3574 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3585 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3587 static ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
3588 struct lttng_channel
**channels
)
3591 ssize_t nb_chan
= 0;
3594 case LTTNG_DOMAIN_KERNEL
:
3595 if (session
->kernel_session
!= NULL
) {
3596 nb_chan
= session
->kernel_session
->channel_count
;
3598 DBG3("Number of kernel channels %zd", nb_chan
);
3600 case LTTNG_DOMAIN_UST
:
3601 if (session
->ust_session
!= NULL
) {
3602 nb_chan
= lttng_ht_get_count(
3603 session
->ust_session
->domain_global
.channels
);
3605 DBG3("Number of UST global channels %zd", nb_chan
);
3614 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
3615 if (*channels
== NULL
) {
3616 ret
= -LTTCOMM_FATAL
;
3620 list_lttng_channels(domain
, session
, *channels
);
3632 * Command LTTNG_LIST_EVENTS processed by the client thread.
3634 static ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
3635 char *channel_name
, struct lttng_event
**events
)
3638 ssize_t nb_event
= 0;
3641 case LTTNG_DOMAIN_KERNEL
:
3642 if (session
->kernel_session
!= NULL
) {
3643 nb_event
= list_lttng_kernel_events(channel_name
,
3644 session
->kernel_session
, events
);
3647 case LTTNG_DOMAIN_UST
:
3649 if (session
->ust_session
!= NULL
) {
3650 nb_event
= list_lttng_ust_global_events(channel_name
,
3651 &session
->ust_session
->domain_global
, events
);
3667 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
3669 static int cmd_set_consumer_uri(int domain
, struct ltt_session
*session
,
3670 struct lttng_uri
*uri
)
3673 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3674 struct ltt_ust_session
*usess
= session
->ust_session
;
3675 struct consumer_output
*consumer
;
3677 /* Can't enable consumer after session started. */
3678 if (session
->enabled
) {
3679 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
3684 case LTTNG_DOMAIN_KERNEL
:
3685 /* Code flow error if we don't have a kernel session here. */
3688 /* Create consumer output if none exists */
3689 consumer
= ksess
->tmp_consumer
;
3690 if (consumer
== NULL
) {
3691 consumer
= consumer_copy_output(ksess
->consumer
);
3692 if (consumer
== NULL
) {
3693 ret
= LTTCOMM_FATAL
;
3696 /* Reassign new pointer */
3697 ksess
->tmp_consumer
= consumer
;
3700 switch (uri
->dtype
) {
3701 case LTTNG_DST_IPV4
:
3702 case LTTNG_DST_IPV6
:
3703 DBG2("Setting network URI for kernel session %s", session
->name
);
3705 /* Set URI into consumer output object */
3706 ret
= consumer_set_network_uri(consumer
, uri
);
3708 ret
= LTTCOMM_FATAL
;
3712 /* On a new subdir, reappend the default trace dir. */
3713 if (strlen(uri
->subdir
) != 0) {
3714 strncat(consumer
->subdir
, DEFAULT_KERNEL_TRACE_DIR
,
3715 sizeof(consumer
->subdir
));
3718 ret
= send_socket_relayd_consumer(domain
, session
, uri
, consumer
,
3719 ksess
->consumer_fd
);
3720 if (ret
!= LTTCOMM_OK
) {
3724 case LTTNG_DST_PATH
:
3725 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
3726 memset(consumer
->dst
.trace_path
, 0,
3727 sizeof(consumer
->dst
.trace_path
));
3728 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
3729 sizeof(consumer
->dst
.trace_path
));
3730 /* Append default kernel trace dir */
3731 strncat(consumer
->dst
.trace_path
, DEFAULT_KERNEL_TRACE_DIR
,
3732 sizeof(consumer
->dst
.trace_path
));
3738 case LTTNG_DOMAIN_UST
:
3739 /* Code flow error if we don't have a kernel session here. */
3742 /* Create consumer output if none exists */
3743 consumer
= usess
->tmp_consumer
;
3744 if (consumer
== NULL
) {
3745 consumer
= consumer_copy_output(usess
->consumer
);
3746 if (consumer
== NULL
) {
3747 ret
= LTTCOMM_FATAL
;
3750 /* Reassign new pointer */
3751 usess
->tmp_consumer
= consumer
;
3754 switch (uri
->dtype
) {
3755 case LTTNG_DST_IPV4
:
3756 case LTTNG_DST_IPV6
:
3758 DBG2("Setting network URI for UST session %s", session
->name
);
3760 /* Set URI into consumer object */
3761 ret
= consumer_set_network_uri(consumer
, uri
);
3763 ret
= LTTCOMM_FATAL
;
3767 /* On a new subdir, reappend the default trace dir. */
3768 if (strlen(uri
->subdir
) != 0) {
3769 strncat(consumer
->subdir
, DEFAULT_UST_TRACE_DIR
,
3770 sizeof(consumer
->subdir
));
3773 if (ust_consumerd64_fd
>= 0) {
3774 ret
= send_socket_relayd_consumer(domain
, session
, uri
,
3775 consumer
, ust_consumerd64_fd
);
3776 if (ret
!= LTTCOMM_OK
) {
3781 if (ust_consumerd32_fd
>= 0) {
3782 ret
= send_socket_relayd_consumer(domain
, session
, uri
,
3783 consumer
, ust_consumerd32_fd
);
3784 if (ret
!= LTTCOMM_OK
) {
3791 case LTTNG_DST_PATH
:
3792 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
3793 memset(consumer
->dst
.trace_path
, 0,
3794 sizeof(consumer
->dst
.trace_path
));
3795 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
3796 sizeof(consumer
->dst
.trace_path
));
3797 /* Append default UST trace dir */
3798 strncat(consumer
->dst
.trace_path
, DEFAULT_UST_TRACE_DIR
,
3799 sizeof(consumer
->dst
.trace_path
));
3813 * Command LTTNG_DISABLE_CONSUMER processed by the client thread.
3815 static int cmd_disable_consumer(int domain
, struct ltt_session
*session
)
3818 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3819 struct ltt_ust_session
*usess
= session
->ust_session
;
3820 struct consumer_output
*consumer
;
3822 if (session
->enabled
) {
3823 /* Can't disable consumer on an already started session */
3824 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
3829 case LTTNG_DOMAIN_KERNEL
:
3830 /* Code flow error if we don't have a kernel session here. */
3833 DBG("Disabling kernel consumer");
3834 consumer
= ksess
->consumer
;
3837 case LTTNG_DOMAIN_UST
:
3838 /* Code flow error if we don't have a UST session here. */
3841 DBG("Disabling UST consumer");
3842 consumer
= usess
->consumer
;
3846 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
3851 consumer
->enabled
= 0;
3853 /* Success at this point */
3861 * Command LTTNG_ENABLE_CONSUMER processed by the client thread.
3863 static int cmd_enable_consumer(int domain
, struct ltt_session
*session
)
3866 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3867 struct ltt_ust_session
*usess
= session
->ust_session
;
3868 struct consumer_output
*tmp_out
;
3870 /* Can't enable consumer after session started. */
3871 if (session
->enabled
) {
3872 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
3877 case LTTNG_DOMAIN_KERNEL
:
3878 /* Code flow error if we don't have a kernel session here. */
3882 * Check if we have already sent fds to the consumer. In that case,
3883 * the enable-consumer command can't be used because a start trace
3884 * had previously occured.
3886 if (ksess
->consumer_fds_sent
) {
3887 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
3891 tmp_out
= ksess
->tmp_consumer
;
3892 if (tmp_out
== NULL
) {
3893 /* No temp. consumer output exists. Using the current one. */
3894 DBG3("No temporary consumer. Using default");
3899 switch (tmp_out
->type
) {
3900 case CONSUMER_DST_LOCAL
:
3901 DBG2("Consumer output is local. Creating directory(ies)");
3903 /* Create directory(ies) */
3904 ret
= run_as_mkdir_recursive(tmp_out
->dst
.trace_path
,
3905 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
3907 if (ret
!= -EEXIST
) {
3908 ERR("Trace directory creation error");
3909 ret
= LTTCOMM_FATAL
;
3914 case CONSUMER_DST_NET
:
3915 DBG2("Consumer output is network. Validating URIs");
3916 /* Validate if we have both control and data path set. */
3917 if (!tmp_out
->dst
.net
.control_isset
) {
3918 ret
= LTTCOMM_URI_CTRL_MISS
;
3922 if (!tmp_out
->dst
.net
.data_isset
) {
3923 ret
= LTTCOMM_URI_DATA_MISS
;
3927 /* Check established network session state */
3928 if (session
->net_handle
== 0) {
3929 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
3930 ERR("Session network handle is not set on enable-consumer");
3934 /* Append default kernel trace dir to subdir */
3935 strncat(ksess
->consumer
->subdir
, DEFAULT_KERNEL_TRACE_DIR
,
3936 sizeof(ksess
->consumer
->subdir
));
3943 * This is race free for now since the session lock is acquired before
3944 * ending up in this function. No other threads can access this kernel
3945 * session without this lock hence freeing the consumer output object
3948 consumer_destroy_output(ksess
->consumer
);
3949 ksess
->consumer
= tmp_out
;
3950 ksess
->tmp_consumer
= NULL
;
3953 case LTTNG_DOMAIN_UST
:
3954 /* Code flow error if we don't have a UST session here. */
3958 * Check if we have already sent fds to the consumer. In that case,
3959 * the enable-consumer command can't be used because a start trace
3960 * had previously occured.
3962 if (usess
->start_trace
) {
3963 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
3967 tmp_out
= usess
->tmp_consumer
;
3968 if (tmp_out
== NULL
) {
3969 /* No temp. consumer output exists. Using the current one. */
3970 DBG3("No temporary consumer. Using default");
3975 switch (tmp_out
->type
) {
3976 case CONSUMER_DST_LOCAL
:
3977 DBG2("Consumer output is local. Creating directory(ies)");
3979 /* Create directory(ies) */
3980 ret
= run_as_mkdir_recursive(tmp_out
->dst
.trace_path
,
3981 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
3983 if (ret
!= -EEXIST
) {
3984 ERR("Trace directory creation error");
3985 ret
= LTTCOMM_FATAL
;
3990 case CONSUMER_DST_NET
:
3991 DBG2("Consumer output is network. Validating URIs");
3992 /* Validate if we have both control and data path set. */
3993 if (!tmp_out
->dst
.net
.control_isset
) {
3994 ret
= LTTCOMM_URI_CTRL_MISS
;
3998 if (!tmp_out
->dst
.net
.data_isset
) {
3999 ret
= LTTCOMM_URI_DATA_MISS
;
4003 /* Check established network session state */
4004 if (session
->net_handle
== 0) {
4005 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
4006 DBG2("Session network handle is not set on enable-consumer");
4010 if (tmp_out
->net_seq_index
== -1) {
4011 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
4012 DBG2("Network index is not set on the consumer");
4016 /* Append default kernel trace dir to subdir */
4017 strncat(usess
->consumer
->subdir
, DEFAULT_UST_TRACE_DIR
,
4018 sizeof(usess
->consumer
->subdir
));
4025 * This is race free for now since the session lock is acquired before
4026 * ending up in this function. No other threads can access this kernel
4027 * session without this lock hence freeing the consumer output object
4030 consumer_destroy_output(usess
->consumer
);
4031 usess
->consumer
= tmp_out
;
4032 usess
->tmp_consumer
= NULL
;
4037 /* Success at this point */
4045 * Process the command requested by the lttng client within the command
4046 * context structure. This function make sure that the return structure (llm)
4047 * is set and ready for transmission before returning.
4049 * Return any error encountered or 0 for success.
4051 * "sock" is only used for special-case var. len data.
4053 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
4056 int ret
= LTTCOMM_OK
;
4057 int need_tracing_session
= 1;
4060 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
4064 switch (cmd_ctx
->lsm
->cmd_type
) {
4065 case LTTNG_CREATE_SESSION
:
4066 case LTTNG_CREATE_SESSION_URI
:
4067 case LTTNG_DESTROY_SESSION
:
4068 case LTTNG_LIST_SESSIONS
:
4069 case LTTNG_LIST_DOMAINS
:
4070 case LTTNG_START_TRACE
:
4071 case LTTNG_STOP_TRACE
:
4078 if (opt_no_kernel
&& need_domain
4079 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
4081 ret
= LTTCOMM_NEED_ROOT_SESSIOND
;
4083 ret
= LTTCOMM_KERN_NA
;
4089 * Check for command that don't needs to allocate a returned payload. We do
4090 * this here so we don't have to make the call for no payload at each
4093 switch(cmd_ctx
->lsm
->cmd_type
) {
4094 case LTTNG_LIST_SESSIONS
:
4095 case LTTNG_LIST_TRACEPOINTS
:
4096 case LTTNG_LIST_TRACEPOINT_FIELDS
:
4097 case LTTNG_LIST_DOMAINS
:
4098 case LTTNG_LIST_CHANNELS
:
4099 case LTTNG_LIST_EVENTS
:
4102 /* Setup lttng message with no payload */
4103 ret
= setup_lttng_msg(cmd_ctx
, 0);
4105 /* This label does not try to unlock the session */
4106 goto init_setup_error
;
4110 /* Commands that DO NOT need a session. */
4111 switch (cmd_ctx
->lsm
->cmd_type
) {
4112 case LTTNG_CREATE_SESSION
:
4113 case LTTNG_CREATE_SESSION_URI
:
4114 case LTTNG_CALIBRATE
:
4115 case LTTNG_LIST_SESSIONS
:
4116 case LTTNG_LIST_TRACEPOINTS
:
4117 case LTTNG_LIST_TRACEPOINT_FIELDS
:
4118 need_tracing_session
= 0;
4121 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
4123 * We keep the session list lock across _all_ commands
4124 * for now, because the per-session lock does not
4125 * handle teardown properly.
4127 session_lock_list();
4128 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
4129 if (cmd_ctx
->session
== NULL
) {
4130 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
4131 ret
= LTTCOMM_SESS_NOT_FOUND
;
4133 /* If no session name specified */
4134 ret
= LTTCOMM_SELECT_SESS
;
4138 /* Acquire lock for the session */
4139 session_lock(cmd_ctx
->session
);
4148 * Check domain type for specific "pre-action".
4150 switch (cmd_ctx
->lsm
->domain
.type
) {
4151 case LTTNG_DOMAIN_KERNEL
:
4153 ret
= LTTCOMM_NEED_ROOT_SESSIOND
;
4157 /* Kernel tracer check */
4158 if (kernel_tracer_fd
== -1) {
4159 /* Basically, load kernel tracer modules */
4160 ret
= init_kernel_tracer();
4166 /* Consumer is in an ERROR state. Report back to client */
4167 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
4168 ret
= LTTCOMM_NO_KERNCONSUMERD
;
4172 /* Need a session for kernel command */
4173 if (need_tracing_session
) {
4174 if (cmd_ctx
->session
->kernel_session
== NULL
) {
4175 ret
= create_kernel_session(cmd_ctx
->session
);
4177 ret
= LTTCOMM_KERN_SESS_FAIL
;
4182 /* Start the kernel consumer daemon */
4183 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
4184 if (kconsumer_data
.pid
== 0 &&
4185 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
4186 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
4187 ret
= start_consumerd(&kconsumer_data
);
4189 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
4192 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
4194 /* Set consumer fd of the session */
4195 cmd_ctx
->session
->kernel_session
->consumer_fd
=
4196 kconsumer_data
.cmd_sock
;
4198 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
4203 case LTTNG_DOMAIN_UST
:
4205 /* Consumer is in an ERROR state. Report back to client */
4206 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
4207 ret
= LTTCOMM_NO_USTCONSUMERD
;
4211 if (need_tracing_session
) {
4212 if (cmd_ctx
->session
->ust_session
== NULL
) {
4213 ret
= create_ust_session(cmd_ctx
->session
,
4214 &cmd_ctx
->lsm
->domain
);
4215 if (ret
!= LTTCOMM_OK
) {
4220 /* Start the UST consumer daemons */
4222 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
4223 if (consumerd64_bin
[0] != '\0' &&
4224 ustconsumer64_data
.pid
== 0 &&
4225 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
4226 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
4227 ret
= start_consumerd(&ustconsumer64_data
);
4229 ret
= LTTCOMM_UST_CONSUMER64_FAIL
;
4230 ust_consumerd64_fd
= -EINVAL
;
4234 ust_consumerd64_fd
= ustconsumer64_data
.cmd_sock
;
4235 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
4237 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
4240 if (consumerd32_bin
[0] != '\0' &&
4241 ustconsumer32_data
.pid
== 0 &&
4242 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
4243 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
4244 ret
= start_consumerd(&ustconsumer32_data
);
4246 ret
= LTTCOMM_UST_CONSUMER32_FAIL
;
4247 ust_consumerd32_fd
= -EINVAL
;
4251 ust_consumerd32_fd
= ustconsumer32_data
.cmd_sock
;
4252 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
4254 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
4264 /* Validate consumer daemon state when start/stop trace command */
4265 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
4266 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
4267 switch (cmd_ctx
->lsm
->domain
.type
) {
4268 case LTTNG_DOMAIN_UST
:
4269 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
4270 ret
= LTTCOMM_NO_USTCONSUMERD
;
4274 case LTTNG_DOMAIN_KERNEL
:
4275 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
4276 ret
= LTTCOMM_NO_KERNCONSUMERD
;
4284 * Check that the UID or GID match that of the tracing session.
4285 * The root user can interact with all sessions.
4287 if (need_tracing_session
) {
4288 if (!session_access_ok(cmd_ctx
->session
,
4289 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4290 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
4291 ret
= LTTCOMM_EPERM
;
4296 /* Process by command type */
4297 switch (cmd_ctx
->lsm
->cmd_type
) {
4298 case LTTNG_ADD_CONTEXT
:
4300 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4301 cmd_ctx
->lsm
->u
.context
.channel_name
,
4302 cmd_ctx
->lsm
->u
.context
.event_name
,
4303 &cmd_ctx
->lsm
->u
.context
.ctx
);
4306 case LTTNG_DISABLE_CHANNEL
:
4308 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4309 cmd_ctx
->lsm
->u
.disable
.channel_name
);
4312 case LTTNG_DISABLE_EVENT
:
4314 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4315 cmd_ctx
->lsm
->u
.disable
.channel_name
,
4316 cmd_ctx
->lsm
->u
.disable
.name
);
4319 case LTTNG_DISABLE_ALL_EVENT
:
4321 DBG("Disabling all events");
4323 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4324 cmd_ctx
->lsm
->u
.disable
.channel_name
);
4327 case LTTNG_DISABLE_CONSUMER
:
4329 ret
= cmd_disable_consumer(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
);
4332 case LTTNG_ENABLE_CHANNEL
:
4334 ret
= cmd_enable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4335 &cmd_ctx
->lsm
->u
.channel
.chan
);
4338 case LTTNG_ENABLE_CONSUMER
:
4340 ret
= cmd_enable_consumer(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
);
4343 case LTTNG_ENABLE_EVENT
:
4345 ret
= cmd_enable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4346 cmd_ctx
->lsm
->u
.enable
.channel_name
,
4347 &cmd_ctx
->lsm
->u
.enable
.event
);
4350 case LTTNG_ENABLE_ALL_EVENT
:
4352 DBG("Enabling all events");
4354 ret
= cmd_enable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4355 cmd_ctx
->lsm
->u
.enable
.channel_name
,
4356 cmd_ctx
->lsm
->u
.enable
.event
.type
);
4359 case LTTNG_LIST_TRACEPOINTS
:
4361 struct lttng_event
*events
;
4364 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
4365 if (nb_events
< 0) {
4371 * Setup lttng message with payload size set to the event list size in
4372 * bytes and then copy list into the llm payload.
4374 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
4380 /* Copy event list into message payload */
4381 memcpy(cmd_ctx
->llm
->payload
, events
,
4382 sizeof(struct lttng_event
) * nb_events
);
4389 case LTTNG_LIST_TRACEPOINT_FIELDS
:
4391 struct lttng_event_field
*fields
;
4394 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
, &fields
);
4395 if (nb_fields
< 0) {
4401 * Setup lttng message with payload size set to the event list size in
4402 * bytes and then copy list into the llm payload.
4404 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event_field
) * nb_fields
);
4410 /* Copy event list into message payload */
4411 memcpy(cmd_ctx
->llm
->payload
, fields
,
4412 sizeof(struct lttng_event_field
) * nb_fields
);
4419 case LTTNG_SET_CONSUMER_URI
:
4421 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
4422 &cmd_ctx
->lsm
->u
.uri
);
4425 case LTTNG_START_TRACE
:
4427 ret
= cmd_start_trace(cmd_ctx
->session
);
4430 case LTTNG_STOP_TRACE
:
4432 ret
= cmd_stop_trace(cmd_ctx
->session
);
4435 case LTTNG_CREATE_SESSION
:
4437 ret
= cmd_create_session(cmd_ctx
->lsm
->session
.name
,
4438 cmd_ctx
->lsm
->session
.path
, &cmd_ctx
->creds
);
4441 case LTTNG_CREATE_SESSION_URI
:
4443 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
,
4444 &cmd_ctx
->lsm
->u
.create_uri
.ctrl_uri
,
4445 &cmd_ctx
->lsm
->u
.create_uri
.data_uri
,
4446 cmd_ctx
->lsm
->u
.create_uri
.enable_consumer
, &cmd_ctx
->creds
);
4449 case LTTNG_DESTROY_SESSION
:
4451 ret
= cmd_destroy_session(cmd_ctx
->session
,
4452 cmd_ctx
->lsm
->session
.name
);
4454 * Set session to NULL so we do not unlock it after
4457 cmd_ctx
->session
= NULL
;
4460 case LTTNG_LIST_DOMAINS
:
4463 struct lttng_domain
*domains
;
4465 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
4471 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
4476 /* Copy event list into message payload */
4477 memcpy(cmd_ctx
->llm
->payload
, domains
,
4478 nb_dom
* sizeof(struct lttng_domain
));
4485 case LTTNG_LIST_CHANNELS
:
4488 struct lttng_channel
*channels
;
4490 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
4491 cmd_ctx
->session
, &channels
);
4497 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
4502 /* Copy event list into message payload */
4503 memcpy(cmd_ctx
->llm
->payload
, channels
,
4504 nb_chan
* sizeof(struct lttng_channel
));
4511 case LTTNG_LIST_EVENTS
:
4514 struct lttng_event
*events
= NULL
;
4516 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
4517 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
4523 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
4528 /* Copy event list into message payload */
4529 memcpy(cmd_ctx
->llm
->payload
, events
,
4530 nb_event
* sizeof(struct lttng_event
));
4537 case LTTNG_LIST_SESSIONS
:
4539 unsigned int nr_sessions
;
4541 session_lock_list();
4542 nr_sessions
= lttng_sessions_count(
4543 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4544 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4546 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
4548 session_unlock_list();
4552 /* Filled the session array */
4553 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
4554 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4555 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4557 session_unlock_list();
4562 case LTTNG_CALIBRATE
:
4564 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
4565 &cmd_ctx
->lsm
->u
.calibrate
);
4568 case LTTNG_REGISTER_CONSUMER
:
4570 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4571 cmd_ctx
->lsm
->u
.reg
.path
);
4574 case LTTNG_SET_FILTER
:
4576 struct lttng_filter_bytecode
*bytecode
;
4578 if (cmd_ctx
->lsm
->u
.filter
.bytecode_len
> 65336) {
4579 ret
= LTTCOMM_FILTER_INVAL
;
4582 bytecode
= zmalloc(cmd_ctx
->lsm
->u
.filter
.bytecode_len
);
4584 ret
= LTTCOMM_FILTER_NOMEM
;
4587 /* Receive var. len. data */
4588 DBG("Receiving var len data from client ...");
4589 ret
= lttcomm_recv_unix_sock(sock
, bytecode
,
4590 cmd_ctx
->lsm
->u
.filter
.bytecode_len
);
4592 DBG("Nothing recv() from client var len data... continuing");
4594 ret
= LTTCOMM_FILTER_INVAL
;
4598 if (bytecode
->len
+ sizeof(*bytecode
)
4599 != cmd_ctx
->lsm
->u
.filter
.bytecode_len
) {
4601 ret
= LTTCOMM_FILTER_INVAL
;
4605 ret
= cmd_set_filter(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4606 cmd_ctx
->lsm
->u
.filter
.channel_name
,
4607 cmd_ctx
->lsm
->u
.filter
.event_name
,
4617 if (cmd_ctx
->llm
== NULL
) {
4618 DBG("Missing llm structure. Allocating one.");
4619 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
4623 /* Set return code */
4624 cmd_ctx
->llm
->ret_code
= ret
;
4626 if (cmd_ctx
->session
) {
4627 session_unlock(cmd_ctx
->session
);
4629 if (need_tracing_session
) {
4630 session_unlock_list();
4637 * Thread managing health check socket.
4639 static void *thread_manage_health(void *data
)
4641 int sock
= -1, new_sock
, ret
, i
, pollfd
;
4642 uint32_t revents
, nb_fd
;
4643 struct lttng_poll_event events
;
4644 struct lttcomm_health_msg msg
;
4645 struct lttcomm_health_data reply
;
4647 DBG("[thread] Manage health check started");
4649 rcu_register_thread();
4651 /* Create unix socket */
4652 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
4654 ERR("Unable to create health check Unix socket");
4659 ret
= lttcomm_listen_unix_sock(sock
);
4665 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4666 * more will be added to this poll set.
4668 ret
= create_thread_poll_set(&events
, 2);
4673 /* Add the application registration socket */
4674 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4680 DBG("Health check ready");
4682 nb_fd
= LTTNG_POLL_GETNB(&events
);
4684 /* Inifinite blocking call, waiting for transmission */
4686 ret
= lttng_poll_wait(&events
, -1);
4689 * Restart interrupted system call.
4691 if (errno
== EINTR
) {
4697 for (i
= 0; i
< nb_fd
; i
++) {
4698 /* Fetch once the poll data */
4699 revents
= LTTNG_POLL_GETEV(&events
, i
);
4700 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4702 /* Thread quit pipe has been closed. Killing thread. */
4703 ret
= check_thread_quit_pipe(pollfd
, revents
);
4708 /* Event on the registration socket */
4709 if (pollfd
== sock
) {
4710 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4711 ERR("Health socket poll error");
4717 new_sock
= lttcomm_accept_unix_sock(sock
);
4722 DBG("Receiving data from client for health...");
4723 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4725 DBG("Nothing recv() from client... continuing");
4726 ret
= close(new_sock
);
4734 rcu_thread_online();
4736 switch (msg
.component
) {
4737 case LTTNG_HEALTH_CMD
:
4738 reply
.ret_code
= health_check_state(&health_thread_cmd
);
4740 case LTTNG_HEALTH_APP_REG
:
4741 reply
.ret_code
= health_check_state(&health_thread_app_reg
);
4743 case LTTNG_HEALTH_KERNEL
:
4744 reply
.ret_code
= health_check_state(&health_thread_kernel
);
4746 case LTTNG_HEALTH_CONSUMER
:
4747 reply
.ret_code
= check_consumer_health();
4749 case LTTNG_HEALTH_ALL
:
4750 ret
= check_consumer_health();
4753 health_check_state(&health_thread_app_reg
) &
4754 health_check_state(&health_thread_cmd
) &
4755 health_check_state(&health_thread_kernel
) &
4759 reply
.ret_code
= LTTCOMM_UND
;
4764 * Flip ret value since 0 is a success and 1 indicates a bad health for
4765 * the client where in the sessiond it is the opposite. Again, this is
4766 * just to make things easier for us poor developer which enjoy a lot
4769 if (reply
.ret_code
== 0 || reply
.ret_code
== 1) {
4770 reply
.ret_code
= !reply
.ret_code
;
4773 DBG2("Health check return value %d", reply
.ret_code
);
4775 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4777 ERR("Failed to send health data back to client");
4780 /* End of transmission */
4781 ret
= close(new_sock
);
4789 DBG("Health check thread dying");
4790 unlink(health_unix_sock_path
);
4797 if (new_sock
>= 0) {
4798 ret
= close(new_sock
);
4804 lttng_poll_clean(&events
);
4806 rcu_unregister_thread();
4811 * This thread manage all clients request using the unix client socket for
4814 static void *thread_manage_clients(void *data
)
4816 int sock
= -1, ret
, i
, pollfd
;
4818 uint32_t revents
, nb_fd
;
4819 struct command_ctx
*cmd_ctx
= NULL
;
4820 struct lttng_poll_event events
;
4822 DBG("[thread] Manage client started");
4824 rcu_register_thread();
4826 health_code_update(&health_thread_cmd
);
4828 ret
= lttcomm_listen_unix_sock(client_sock
);
4834 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4835 * more will be added to this poll set.
4837 ret
= create_thread_poll_set(&events
, 2);
4842 /* Add the application registration socket */
4843 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4849 * Notify parent pid that we are ready to accept command for client side.
4851 if (opt_sig_parent
) {
4852 kill(ppid
, SIGUSR1
);
4855 health_code_update(&health_thread_cmd
);
4858 DBG("Accepting client command ...");
4860 nb_fd
= LTTNG_POLL_GETNB(&events
);
4862 /* Inifinite blocking call, waiting for transmission */
4864 health_poll_update(&health_thread_cmd
);
4865 ret
= lttng_poll_wait(&events
, -1);
4866 health_poll_update(&health_thread_cmd
);
4869 * Restart interrupted system call.
4871 if (errno
== EINTR
) {
4877 for (i
= 0; i
< nb_fd
; i
++) {
4878 /* Fetch once the poll data */
4879 revents
= LTTNG_POLL_GETEV(&events
, i
);
4880 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4882 health_code_update(&health_thread_cmd
);
4884 /* Thread quit pipe has been closed. Killing thread. */
4885 ret
= check_thread_quit_pipe(pollfd
, revents
);
4890 /* Event on the registration socket */
4891 if (pollfd
== client_sock
) {
4892 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4893 ERR("Client socket poll error");
4899 DBG("Wait for client response");
4901 health_code_update(&health_thread_cmd
);
4903 sock
= lttcomm_accept_unix_sock(client_sock
);
4908 /* Set socket option for credentials retrieval */
4909 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4914 /* Allocate context command to process the client request */
4915 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4916 if (cmd_ctx
== NULL
) {
4917 PERROR("zmalloc cmd_ctx");
4921 /* Allocate data buffer for reception */
4922 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4923 if (cmd_ctx
->lsm
== NULL
) {
4924 PERROR("zmalloc cmd_ctx->lsm");
4928 cmd_ctx
->llm
= NULL
;
4929 cmd_ctx
->session
= NULL
;
4931 health_code_update(&health_thread_cmd
);
4934 * Data is received from the lttng client. The struct
4935 * lttcomm_session_msg (lsm) contains the command and data request of
4938 DBG("Receiving data from client ...");
4939 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4940 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4942 DBG("Nothing recv() from client... continuing");
4948 clean_command_ctx(&cmd_ctx
);
4952 health_code_update(&health_thread_cmd
);
4954 // TODO: Validate cmd_ctx including sanity check for
4955 // security purpose.
4957 rcu_thread_online();
4959 * This function dispatch the work to the kernel or userspace tracer
4960 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4961 * informations for the client. The command context struct contains
4962 * everything this function may needs.
4964 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4965 rcu_thread_offline();
4975 * TODO: Inform client somehow of the fatal error. At
4976 * this point, ret < 0 means that a zmalloc failed
4977 * (ENOMEM). Error detected but still accept
4978 * command, unless a socket error has been
4981 clean_command_ctx(&cmd_ctx
);
4985 health_code_update(&health_thread_cmd
);
4987 DBG("Sending response (size: %d, retcode: %s)",
4988 cmd_ctx
->lttng_msg_size
,
4989 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
4990 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4992 ERR("Failed to send data back to client");
4995 /* End of transmission */
5002 clean_command_ctx(&cmd_ctx
);
5004 health_code_update(&health_thread_cmd
);
5008 health_reset(&health_thread_cmd
);
5010 DBG("Client thread dying");
5011 unlink(client_unix_sock_path
);
5012 if (client_sock
>= 0) {
5013 ret
= close(client_sock
);
5025 lttng_poll_clean(&events
);
5026 clean_command_ctx(&cmd_ctx
);
5028 rcu_unregister_thread();
5034 * usage function on stderr
5036 static void usage(void)
5038 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
5039 fprintf(stderr
, " -h, --help Display this usage.\n");
5040 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
5041 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
5042 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
5043 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
5044 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
5045 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
5046 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
5047 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
5048 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
5049 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
5050 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
5051 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
5052 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
5053 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
5054 fprintf(stderr
, " -V, --version Show version number.\n");
5055 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
5056 fprintf(stderr
, " -q, --quiet No output at all.\n");
5057 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
5058 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
5059 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
5063 * daemon argument parsing
5065 static int parse_args(int argc
, char **argv
)
5069 static struct option long_options
[] = {
5070 { "client-sock", 1, 0, 'c' },
5071 { "apps-sock", 1, 0, 'a' },
5072 { "kconsumerd-cmd-sock", 1, 0, 'C' },
5073 { "kconsumerd-err-sock", 1, 0, 'E' },
5074 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
5075 { "ustconsumerd32-err-sock", 1, 0, 'H' },
5076 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
5077 { "ustconsumerd64-err-sock", 1, 0, 'F' },
5078 { "consumerd32-path", 1, 0, 'u' },
5079 { "consumerd32-libdir", 1, 0, 'U' },
5080 { "consumerd64-path", 1, 0, 't' },
5081 { "consumerd64-libdir", 1, 0, 'T' },
5082 { "daemonize", 0, 0, 'd' },
5083 { "sig-parent", 0, 0, 'S' },
5084 { "help", 0, 0, 'h' },
5085 { "group", 1, 0, 'g' },
5086 { "version", 0, 0, 'V' },
5087 { "quiet", 0, 0, 'q' },
5088 { "verbose", 0, 0, 'v' },
5089 { "verbose-consumer", 0, 0, 'Z' },
5090 { "no-kernel", 0, 0, 'N' },
5095 int option_index
= 0;
5096 c
= getopt_long(argc
, argv
, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t",
5097 long_options
, &option_index
);
5104 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
5106 fprintf(stderr
, " with arg %s\n", optarg
);
5110 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5113 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5119 opt_tracing_group
= optarg
;
5125 fprintf(stdout
, "%s\n", VERSION
);
5131 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5134 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5137 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5140 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5143 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5146 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5152 lttng_opt_quiet
= 1;
5155 /* Verbose level can increase using multiple -v */
5156 lttng_opt_verbose
+= 1;
5159 opt_verbose_consumer
+= 1;
5162 consumerd32_bin
= optarg
;
5165 consumerd32_libdir
= optarg
;
5168 consumerd64_bin
= optarg
;
5171 consumerd64_libdir
= optarg
;
5174 /* Unknown option or other error.
5175 * Error is printed by getopt, just return */
5184 * Creates the two needed socket by the daemon.
5185 * apps_sock - The communication socket for all UST apps.
5186 * client_sock - The communication of the cli tool (lttng).
5188 static int init_daemon_socket(void)
5193 old_umask
= umask(0);
5195 /* Create client tool unix socket */
5196 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
5197 if (client_sock
< 0) {
5198 ERR("Create unix sock failed: %s", client_unix_sock_path
);
5203 /* File permission MUST be 660 */
5204 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5206 ERR("Set file permissions failed: %s", client_unix_sock_path
);
5211 /* Create the application unix socket */
5212 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
5213 if (apps_sock
< 0) {
5214 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
5219 /* File permission MUST be 666 */
5220 ret
= chmod(apps_unix_sock_path
,
5221 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5223 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
5234 * Check if the global socket is available, and if a daemon is answering at the
5235 * other side. If yes, error is returned.
5237 static int check_existing_daemon(void)
5239 /* Is there anybody out there ? */
5240 if (lttng_session_daemon_alive()) {
5248 * Set the tracing group gid onto the client socket.
5250 * Race window between mkdir and chown is OK because we are going from more
5251 * permissive (root.root) to less permissive (root.tracing).
5253 static int set_permissions(char *rundir
)
5258 ret
= allowed_group();
5260 WARN("No tracing group detected");
5267 /* Set lttng run dir */
5268 ret
= chown(rundir
, 0, gid
);
5270 ERR("Unable to set group on %s", rundir
);
5274 /* Ensure tracing group can search the run dir */
5275 ret
= chmod(rundir
, S_IRWXU
| S_IXGRP
| S_IXOTH
);
5277 ERR("Unable to set permissions on %s", rundir
);
5281 /* lttng client socket path */
5282 ret
= chown(client_unix_sock_path
, 0, gid
);
5284 ERR("Unable to set group on %s", client_unix_sock_path
);
5288 /* kconsumer error socket path */
5289 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, gid
);
5291 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5295 /* 64-bit ustconsumer error socket path */
5296 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, gid
);
5298 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5302 /* 32-bit ustconsumer compat32 error socket path */
5303 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, gid
);
5305 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5309 DBG("All permissions are set");
5316 * Create the lttng run directory needed for all global sockets and pipe.
5318 static int create_lttng_rundir(const char *rundir
)
5322 DBG3("Creating LTTng run directory: %s", rundir
);
5324 ret
= mkdir(rundir
, S_IRWXU
);
5326 if (errno
!= EEXIST
) {
5327 ERR("Unable to create %s", rundir
);
5339 * Setup sockets and directory needed by the kconsumerd communication with the
5342 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
5346 char path
[PATH_MAX
];
5348 switch (consumer_data
->type
) {
5349 case LTTNG_CONSUMER_KERNEL
:
5350 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
5352 case LTTNG_CONSUMER64_UST
:
5353 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
5355 case LTTNG_CONSUMER32_UST
:
5356 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
5359 ERR("Consumer type unknown");
5364 DBG2("Creating consumer directory: %s", path
);
5366 ret
= mkdir(path
, S_IRWXU
);
5368 if (errno
!= EEXIST
) {
5370 ERR("Failed to create %s", path
);
5376 /* Create the kconsumerd error unix socket */
5377 consumer_data
->err_sock
=
5378 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5379 if (consumer_data
->err_sock
< 0) {
5380 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5385 /* File permission MUST be 660 */
5386 ret
= chmod(consumer_data
->err_unix_sock_path
,
5387 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5389 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5399 * Signal handler for the daemon
5401 * Simply stop all worker threads, leaving main() return gracefully after
5402 * joining all threads and calling cleanup().
5404 static void sighandler(int sig
)
5408 DBG("SIGPIPE caught");
5411 DBG("SIGINT caught");
5415 DBG("SIGTERM caught");
5424 * Setup signal handler for :
5425 * SIGINT, SIGTERM, SIGPIPE
5427 static int set_signal_handler(void)
5430 struct sigaction sa
;
5433 if ((ret
= sigemptyset(&sigset
)) < 0) {
5434 PERROR("sigemptyset");
5438 sa
.sa_handler
= sighandler
;
5439 sa
.sa_mask
= sigset
;
5441 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5442 PERROR("sigaction");
5446 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5447 PERROR("sigaction");
5451 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5452 PERROR("sigaction");
5456 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
5462 * Set open files limit to unlimited. This daemon can open a large number of
5463 * file descriptors in order to consumer multiple kernel traces.
5465 static void set_ulimit(void)
5470 /* The kernel does not allowed an infinite limit for open files */
5471 lim
.rlim_cur
= 65535;
5472 lim
.rlim_max
= 65535;
5474 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5476 PERROR("failed to set open files limit");
5483 int main(int argc
, char **argv
)
5487 const char *home_path
;
5489 init_kernel_workarounds();
5491 rcu_register_thread();
5493 setup_consumerd_path();
5495 /* Parse arguments */
5497 if ((ret
= parse_args(argc
, argv
) < 0)) {
5507 * child: setsid, close FD 0, 1, 2, chdir /
5508 * parent: exit (if fork is successful)
5516 * We are in the child. Make sure all other file
5517 * descriptors are closed, in case we are called with
5518 * more opened file descriptors than the standard ones.
5520 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5525 /* Create thread quit pipe */
5526 if ((ret
= init_thread_quit_pipe()) < 0) {
5530 /* Check if daemon is UID = 0 */
5531 is_root
= !getuid();
5534 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
5536 /* Create global run dir with root access */
5537 ret
= create_lttng_rundir(rundir
);
5542 if (strlen(apps_unix_sock_path
) == 0) {
5543 snprintf(apps_unix_sock_path
, PATH_MAX
,
5544 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
5547 if (strlen(client_unix_sock_path
) == 0) {
5548 snprintf(client_unix_sock_path
, PATH_MAX
,
5549 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
5552 /* Set global SHM for ust */
5553 if (strlen(wait_shm_path
) == 0) {
5554 snprintf(wait_shm_path
, PATH_MAX
,
5555 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
5558 if (strlen(health_unix_sock_path
) == 0) {
5559 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
5560 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
5563 /* Setup kernel consumerd path */
5564 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
5565 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
5566 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
5567 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
5569 DBG2("Kernel consumer err path: %s",
5570 kconsumer_data
.err_unix_sock_path
);
5571 DBG2("Kernel consumer cmd path: %s",
5572 kconsumer_data
.cmd_unix_sock_path
);
5574 home_path
= get_home_dir();
5575 if (home_path
== NULL
) {
5576 /* TODO: Add --socket PATH option */
5577 ERR("Can't get HOME directory for sockets creation.");
5583 * Create rundir from home path. This will create something like
5586 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
5592 ret
= create_lttng_rundir(rundir
);
5597 if (strlen(apps_unix_sock_path
) == 0) {
5598 snprintf(apps_unix_sock_path
, PATH_MAX
,
5599 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
5602 /* Set the cli tool unix socket path */
5603 if (strlen(client_unix_sock_path
) == 0) {
5604 snprintf(client_unix_sock_path
, PATH_MAX
,
5605 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
5608 /* Set global SHM for ust */
5609 if (strlen(wait_shm_path
) == 0) {
5610 snprintf(wait_shm_path
, PATH_MAX
,
5611 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, geteuid());
5614 /* Set health check Unix path */
5615 if (strlen(health_unix_sock_path
) == 0) {
5616 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
5617 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
5621 /* Set consumer initial state */
5622 kernel_consumerd_state
= CONSUMER_STOPPED
;
5623 ust_consumerd_state
= CONSUMER_STOPPED
;
5625 DBG("Client socket path %s", client_unix_sock_path
);
5626 DBG("Application socket path %s", apps_unix_sock_path
);
5627 DBG("LTTng run directory path: %s", rundir
);
5629 /* 32 bits consumerd path setup */
5630 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
5631 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
5632 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
5633 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
5635 DBG2("UST consumer 32 bits err path: %s",
5636 ustconsumer32_data
.err_unix_sock_path
);
5637 DBG2("UST consumer 32 bits cmd path: %s",
5638 ustconsumer32_data
.cmd_unix_sock_path
);
5640 /* 64 bits consumerd path setup */
5641 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
5642 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
5643 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
5644 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
5646 DBG2("UST consumer 64 bits err path: %s",
5647 ustconsumer64_data
.err_unix_sock_path
);
5648 DBG2("UST consumer 64 bits cmd path: %s",
5649 ustconsumer64_data
.cmd_unix_sock_path
);
5652 * See if daemon already exist.
5654 if ((ret
= check_existing_daemon()) < 0) {
5655 ERR("Already running daemon.\n");
5657 * We do not goto exit because we must not cleanup()
5658 * because a daemon is already running.
5664 * Init UST app hash table. Alloc hash table before this point since
5665 * cleanup() can get called after that point.
5669 /* After this point, we can safely call cleanup() with "goto exit" */
5672 * These actions must be executed as root. We do that *after* setting up
5673 * the sockets path because we MUST make the check for another daemon using
5674 * those paths *before* trying to set the kernel consumer sockets and init
5678 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
5683 /* Setup kernel tracer */
5684 if (!opt_no_kernel
) {
5685 init_kernel_tracer();
5688 /* Set ulimit for open files */
5691 /* init lttng_fd tracking must be done after set_ulimit. */
5694 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
5699 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
5704 if ((ret
= set_signal_handler()) < 0) {
5708 /* Setup the needed unix socket */
5709 if ((ret
= init_daemon_socket()) < 0) {
5713 /* Set credentials to socket */
5714 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
5718 /* Get parent pid if -S, --sig-parent is specified. */
5719 if (opt_sig_parent
) {
5723 /* Setup the kernel pipe for waking up the kernel thread */
5724 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
5728 /* Setup the thread apps communication pipe. */
5729 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
5733 /* Init UST command queue. */
5734 cds_wfq_init(&ust_cmd_queue
.queue
);
5737 * Get session list pointer. This pointer MUST NOT be free(). This list is
5738 * statically declared in session.c
5740 session_list_ptr
= session_get_list();
5742 /* Set up max poll set size */
5743 lttng_poll_set_max_size();
5746 * Set network sequence index to 1 for streams to match a relayd socket on
5747 * the consumer side.
5749 uatomic_set(&relayd_net_seq_idx
, 1);
5751 /* Init all health thread counters. */
5752 health_init(&health_thread_cmd
);
5753 health_init(&health_thread_kernel
);
5754 health_init(&health_thread_app_reg
);
5757 * Init health counters of the consumer thread. We do a quick hack here to
5758 * the state of the consumer health is fine even if the thread is not
5759 * started. This is simply to ease our life and has no cost what so ever.
5761 health_init(&kconsumer_data
.health
);
5762 health_poll_update(&kconsumer_data
.health
);
5763 health_init(&ustconsumer32_data
.health
);
5764 health_poll_update(&ustconsumer32_data
.health
);
5765 health_init(&ustconsumer64_data
.health
);
5766 health_poll_update(&ustconsumer64_data
.health
);
5768 /* Create thread to manage the client socket */
5769 ret
= pthread_create(&health_thread
, NULL
,
5770 thread_manage_health
, (void *) NULL
);
5772 PERROR("pthread_create health");
5776 /* Create thread to manage the client socket */
5777 ret
= pthread_create(&client_thread
, NULL
,
5778 thread_manage_clients
, (void *) NULL
);
5780 PERROR("pthread_create clients");
5784 /* Create thread to dispatch registration */
5785 ret
= pthread_create(&dispatch_thread
, NULL
,
5786 thread_dispatch_ust_registration
, (void *) NULL
);
5788 PERROR("pthread_create dispatch");
5792 /* Create thread to manage application registration. */
5793 ret
= pthread_create(®_apps_thread
, NULL
,
5794 thread_registration_apps
, (void *) NULL
);
5796 PERROR("pthread_create registration");
5800 /* Create thread to manage application socket */
5801 ret
= pthread_create(&apps_thread
, NULL
,
5802 thread_manage_apps
, (void *) NULL
);
5804 PERROR("pthread_create apps");
5808 /* Create kernel thread to manage kernel event */
5809 ret
= pthread_create(&kernel_thread
, NULL
,
5810 thread_manage_kernel
, (void *) NULL
);
5812 PERROR("pthread_create kernel");
5816 ret
= pthread_join(kernel_thread
, &status
);
5818 PERROR("pthread_join");
5819 goto error
; /* join error, exit without cleanup */
5823 ret
= pthread_join(apps_thread
, &status
);
5825 PERROR("pthread_join");
5826 goto error
; /* join error, exit without cleanup */
5830 ret
= pthread_join(reg_apps_thread
, &status
);
5832 PERROR("pthread_join");
5833 goto error
; /* join error, exit without cleanup */
5837 ret
= pthread_join(dispatch_thread
, &status
);
5839 PERROR("pthread_join");
5840 goto error
; /* join error, exit without cleanup */
5844 ret
= pthread_join(client_thread
, &status
);
5846 PERROR("pthread_join");
5847 goto error
; /* join error, exit without cleanup */
5850 ret
= join_consumer_thread(&kconsumer_data
);
5852 PERROR("join_consumer");
5853 goto error
; /* join error, exit without cleanup */
5860 * cleanup() is called when no other thread is running.
5862 rcu_thread_online();
5864 rcu_thread_offline();
5865 rcu_unregister_thread();