2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <semaphore.h>
30 #include <sys/mount.h>
31 #include <sys/resource.h>
32 #include <sys/socket.h>
34 #include <sys/types.h>
36 #include <urcu/uatomic.h>
40 #include <common/common.h>
41 #include <common/compat/poll.h>
42 #include <common/compat/socket.h>
43 #include <common/defaults.h>
44 #include <common/kernel-consumer/kernel-consumer.h>
45 #include <common/futex.h>
46 #include <common/relayd/relayd.h>
47 #include <common/utils.h>
49 #include "lttng-sessiond.h"
55 #include "kernel-consumer.h"
59 #include "ust-consumer.h"
65 #define CONSUMERD_FILE "lttng-consumerd"
68 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
69 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
70 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
71 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
74 const char *opt_tracing_group
;
75 static int opt_sig_parent
;
76 static int opt_verbose_consumer
;
77 static int opt_daemon
;
78 static int opt_no_kernel
;
79 static int is_root
; /* Set to 1 if the daemon is running as root */
80 static pid_t ppid
; /* Parent PID for --sig-parent option */
83 /* Consumer daemon specific control data */
84 static struct consumer_data kconsumer_data
= {
85 .type
= LTTNG_CONSUMER_KERNEL
,
86 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
87 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
90 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
91 .lock
= PTHREAD_MUTEX_INITIALIZER
,
93 static struct consumer_data ustconsumer64_data
= {
94 .type
= LTTNG_CONSUMER64_UST
,
95 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
96 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
99 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
100 .lock
= PTHREAD_MUTEX_INITIALIZER
,
102 static struct consumer_data ustconsumer32_data
= {
103 .type
= LTTNG_CONSUMER32_UST
,
104 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
105 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
108 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
109 .lock
= PTHREAD_MUTEX_INITIALIZER
,
112 /* Shared between threads */
113 static int dispatch_thread_exit
;
115 /* Global application Unix socket path */
116 static char apps_unix_sock_path
[PATH_MAX
];
117 /* Global client Unix socket path */
118 static char client_unix_sock_path
[PATH_MAX
];
119 /* global wait shm path for UST */
120 static char wait_shm_path
[PATH_MAX
];
121 /* Global health check unix path */
122 static char health_unix_sock_path
[PATH_MAX
];
124 /* Sockets and FDs */
125 static int client_sock
= -1;
126 static int apps_sock
= -1;
127 static int kernel_tracer_fd
= -1;
128 static int kernel_poll_pipe
[2] = { -1, -1 };
131 * Quit pipe for all threads. This permits a single cancellation point
132 * for all threads when receiving an event on the pipe.
134 static int thread_quit_pipe
[2] = { -1, -1 };
137 * This pipe is used to inform the thread managing application communication
138 * that a command is queued and ready to be processed.
140 static int apps_cmd_pipe
[2] = { -1, -1 };
142 /* Pthread, Mutexes and Semaphores */
143 static pthread_t apps_thread
;
144 static pthread_t reg_apps_thread
;
145 static pthread_t client_thread
;
146 static pthread_t kernel_thread
;
147 static pthread_t dispatch_thread
;
148 static pthread_t health_thread
;
151 * UST registration command queue. This queue is tied with a futex and uses a N
152 * wakers / 1 waiter implemented and detailed in futex.c/.h
154 * The thread_manage_apps and thread_dispatch_ust_registration interact with
155 * this queue and the wait/wake scheme.
157 static struct ust_cmd_queue ust_cmd_queue
;
160 * Pointer initialized before thread creation.
162 * This points to the tracing session list containing the session count and a
163 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
164 * MUST NOT be taken if you call a public function in session.c.
166 * The lock is nested inside the structure: session_list_ptr->lock. Please use
167 * session_lock_list and session_unlock_list for lock acquisition.
169 static struct ltt_session_list
*session_list_ptr
;
171 int ust_consumerd64_fd
= -1;
172 int ust_consumerd32_fd
= -1;
174 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
175 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
176 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
177 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
180 * Consumer daemon state which is changed when spawning it, killing it or in
181 * case of a fatal error.
183 enum consumerd_state
{
184 CONSUMER_STARTED
= 1,
185 CONSUMER_STOPPED
= 2,
190 * This consumer daemon state is used to validate if a client command will be
191 * able to reach the consumer. If not, the client is informed. For instance,
192 * doing a "lttng start" when the consumer state is set to ERROR will return an
193 * error to the client.
195 * The following example shows a possible race condition of this scheme:
197 * consumer thread error happens
199 * client cmd checks state -> still OK
200 * consumer thread exit, sets error
201 * client cmd try to talk to consumer
204 * However, since the consumer is a different daemon, we have no way of making
205 * sure the command will reach it safely even with this state flag. This is why
206 * we consider that up to the state validation during command processing, the
207 * command is safe. After that, we can not guarantee the correctness of the
208 * client request vis-a-vis the consumer.
210 static enum consumerd_state ust_consumerd_state
;
211 static enum consumerd_state kernel_consumerd_state
;
214 * Used to keep a unique index for each relayd socket created where this value
215 * is associated with streams on the consumer so it can match the right relayd
218 * This value should be incremented atomically for safety purposes and future
219 * possible concurrent access.
221 static unsigned int relayd_net_seq_idx
;
223 /* Used for the health monitoring of the session daemon. See health.h */
224 struct health_state health_thread_cmd
;
225 struct health_state health_thread_app_manage
;
226 struct health_state health_thread_app_reg
;
227 struct health_state health_thread_kernel
;
230 void setup_consumerd_path(void)
232 const char *bin
, *libdir
;
235 * Allow INSTALL_BIN_PATH to be used as a target path for the
236 * native architecture size consumer if CONFIG_CONSUMER*_PATH
237 * has not been defined.
239 #if (CAA_BITS_PER_LONG == 32)
240 if (!consumerd32_bin
[0]) {
241 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
243 if (!consumerd32_libdir
[0]) {
244 consumerd32_libdir
= INSTALL_LIB_PATH
;
246 #elif (CAA_BITS_PER_LONG == 64)
247 if (!consumerd64_bin
[0]) {
248 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
250 if (!consumerd64_libdir
[0]) {
251 consumerd64_libdir
= INSTALL_LIB_PATH
;
254 #error "Unknown bitness"
258 * runtime env. var. overrides the build default.
260 bin
= getenv("LTTNG_CONSUMERD32_BIN");
262 consumerd32_bin
= bin
;
264 bin
= getenv("LTTNG_CONSUMERD64_BIN");
266 consumerd64_bin
= bin
;
268 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
270 consumerd32_libdir
= libdir
;
272 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
274 consumerd64_libdir
= libdir
;
279 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
281 static int create_thread_poll_set(struct lttng_poll_event
*events
,
286 if (events
== NULL
|| size
== 0) {
291 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
297 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
309 * Check if the thread quit pipe was triggered.
311 * Return 1 if it was triggered else 0;
313 static int check_thread_quit_pipe(int fd
, uint32_t events
)
315 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
323 * Return group ID of the tracing group or -1 if not found.
325 static gid_t
allowed_group(void)
329 if (opt_tracing_group
) {
330 grp
= getgrnam(opt_tracing_group
);
332 grp
= getgrnam(default_tracing_group
);
342 * Init thread quit pipe.
344 * Return -1 on error or 0 if all pipes are created.
346 static int init_thread_quit_pipe(void)
350 ret
= pipe(thread_quit_pipe
);
352 PERROR("thread quit pipe");
356 for (i
= 0; i
< 2; i
++) {
357 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
369 * Complete teardown of a kernel session. This free all data structure related
370 * to a kernel session and update counter.
372 static void teardown_kernel_session(struct ltt_session
*session
)
375 struct lttng_ht_iter iter
;
376 struct ltt_kernel_session
*ksess
;
377 struct consumer_socket
*socket
;
379 if (!session
->kernel_session
) {
380 DBG3("No kernel session when tearing down session");
384 ksess
= session
->kernel_session
;
386 DBG("Tearing down kernel session");
389 * Destroy relayd associated with the session consumer. This action is
390 * valid since in order to destroy a session we must acquire the session
391 * lock. This means that there CAN NOT be stream(s) being sent to a
392 * consumer since this action also requires the session lock at any time.
394 * At this point, we are sure that not streams data will be lost after this
397 if (ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) {
398 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
,
400 ret
= consumer_send_destroy_relayd(socket
, ksess
->consumer
);
402 ERR("Unable to send destroy relayd command to consumer");
403 /* Continue since we MUST delete everything at this point. */
409 * If a custom kernel consumer was registered, close the socket before
410 * tearing down the complete kernel session structure
412 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
,
414 if (socket
->fd
!= kconsumer_data
.cmd_sock
) {
416 consumer_del_socket(socket
, ksess
->consumer
);
417 lttcomm_close_unix_sock(socket
->fd
);
418 consumer_destroy_socket(socket
);
423 trace_kernel_destroy_session(ksess
);
427 * Complete teardown of all UST sessions. This will free everything on his path
428 * and destroy the core essence of all ust sessions :)
430 static void teardown_ust_session(struct ltt_session
*session
)
433 struct lttng_ht_iter iter
;
434 struct ltt_ust_session
*usess
;
435 struct consumer_socket
*socket
;
437 if (!session
->ust_session
) {
438 DBG3("No UST session when tearing down session");
441 usess
= session
->ust_session
;
443 DBG("Tearing down UST session(s)");
446 * Destroy relayd associated with the session consumer. This action is
447 * valid since in order to destroy a session we must acquire the session
448 * lock. This means that there CAN NOT be stream(s) being sent to a
449 * consumer since this action also requires the session lock at any time.
451 * At this point, we are sure that no data will be lost after this command
454 if (usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
) {
455 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
, socket
,
457 ret
= consumer_send_destroy_relayd(socket
, usess
->consumer
);
459 ERR("Unable to send destroy relayd command to consumer");
460 /* Continue since we MUST delete everything at this point. */
465 ret
= ust_app_destroy_trace_all(usess
);
467 ERR("Error in ust_app_destroy_trace_all");
470 trace_ust_destroy_session(usess
);
474 * Stop all threads by closing the thread quit pipe.
476 static void stop_threads(void)
480 /* Stopping all threads */
481 DBG("Terminating all threads");
482 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
484 ERR("write error on thread quit pipe");
487 /* Dispatch thread */
488 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
489 futex_nto1_wake(&ust_cmd_queue
.futex
);
495 static void cleanup(void)
499 struct ltt_session
*sess
, *stmp
;
503 DBG("Removing %s directory", rundir
);
504 ret
= asprintf(&cmd
, "rm -rf %s", rundir
);
506 ERR("asprintf failed. Something is really wrong!");
509 /* Remove lttng run directory */
512 ERR("Unable to clean %s", rundir
);
516 DBG("Cleaning up all sessions");
518 /* Destroy session list mutex */
519 if (session_list_ptr
!= NULL
) {
520 pthread_mutex_destroy(&session_list_ptr
->lock
);
522 /* Cleanup ALL session */
523 cds_list_for_each_entry_safe(sess
, stmp
,
524 &session_list_ptr
->head
, list
) {
525 teardown_kernel_session(sess
);
526 teardown_ust_session(sess
);
531 DBG("Closing all UST sockets");
532 ust_app_clean_list();
534 if (is_root
&& !opt_no_kernel
) {
535 DBG2("Closing kernel fd");
536 if (kernel_tracer_fd
>= 0) {
537 ret
= close(kernel_tracer_fd
);
542 DBG("Unloading kernel modules");
543 modprobe_remove_lttng_all();
545 utils_close_pipe(kernel_poll_pipe
);
546 utils_close_pipe(thread_quit_pipe
);
547 utils_close_pipe(apps_cmd_pipe
);
550 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
551 "Matthew, BEET driven development works!%c[%dm",
552 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
557 * Send data on a unix socket using the liblttsessiondcomm API.
559 * Return lttcomm error code.
561 static int send_unix_sock(int sock
, void *buf
, size_t len
)
563 /* Check valid length */
568 return lttcomm_send_unix_sock(sock
, buf
, len
);
572 * Free memory of a command context structure.
574 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
576 DBG("Clean command context structure");
578 if ((*cmd_ctx
)->llm
) {
579 free((*cmd_ctx
)->llm
);
581 if ((*cmd_ctx
)->lsm
) {
582 free((*cmd_ctx
)->lsm
);
590 * Notify UST applications using the shm mmap futex.
592 static int notify_ust_apps(int active
)
596 DBG("Notifying applications of session daemon state: %d", active
);
598 /* See shm.c for this call implying mmap, shm and futex calls */
599 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
600 if (wait_shm_mmap
== NULL
) {
604 /* Wake waiting process */
605 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
607 /* Apps notified successfully */
615 * Setup the outgoing data buffer for the response (llm) by allocating the
616 * right amount of memory and copying the original information from the lsm
619 * Return total size of the buffer pointed by buf.
621 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
627 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
628 if (cmd_ctx
->llm
== NULL
) {
634 /* Copy common data */
635 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
636 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
638 cmd_ctx
->llm
->data_size
= size
;
639 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
648 * Update the kernel poll set of all channel fd available over all tracing
649 * session. Add the wakeup pipe at the end of the set.
651 static int update_kernel_poll(struct lttng_poll_event
*events
)
654 struct ltt_session
*session
;
655 struct ltt_kernel_channel
*channel
;
657 DBG("Updating kernel poll set");
660 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
661 session_lock(session
);
662 if (session
->kernel_session
== NULL
) {
663 session_unlock(session
);
667 cds_list_for_each_entry(channel
,
668 &session
->kernel_session
->channel_list
.head
, list
) {
669 /* Add channel fd to the kernel poll set */
670 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
672 session_unlock(session
);
675 DBG("Channel fd %d added to kernel set", channel
->fd
);
677 session_unlock(session
);
679 session_unlock_list();
684 session_unlock_list();
689 * Find the channel fd from 'fd' over all tracing session. When found, check
690 * for new channel stream and send those stream fds to the kernel consumer.
692 * Useful for CPU hotplug feature.
694 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
697 struct ltt_session
*session
;
698 struct ltt_kernel_session
*ksess
;
699 struct ltt_kernel_channel
*channel
;
701 DBG("Updating kernel streams for channel fd %d", fd
);
704 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
705 session_lock(session
);
706 if (session
->kernel_session
== NULL
) {
707 session_unlock(session
);
710 ksess
= session
->kernel_session
;
712 cds_list_for_each_entry(channel
, &ksess
->channel_list
.head
, list
) {
713 if (channel
->fd
== fd
) {
714 DBG("Channel found, updating kernel streams");
715 ret
= kernel_open_channel_stream(channel
);
721 * Have we already sent fds to the consumer? If yes, it means
722 * that tracing is started so it is safe to send our updated
725 if (ksess
->consumer_fds_sent
== 1 && ksess
->consumer
!= NULL
) {
726 struct lttng_ht_iter iter
;
727 struct consumer_socket
*socket
;
730 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
731 &iter
.iter
, socket
, node
.node
) {
732 /* Code flow error */
733 assert(socket
->fd
>= 0);
735 pthread_mutex_lock(socket
->lock
);
736 ret
= kernel_consumer_send_channel_stream(socket
->fd
,
738 pthread_mutex_unlock(socket
->lock
);
747 session_unlock(session
);
749 session_unlock_list();
753 session_unlock(session
);
754 session_unlock_list();
759 * For each tracing session, update newly registered apps.
761 static void update_ust_app(int app_sock
)
763 struct ltt_session
*sess
, *stmp
;
767 /* For all tracing session(s) */
768 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
770 if (sess
->ust_session
) {
771 ust_app_global_update(sess
->ust_session
, app_sock
);
773 session_unlock(sess
);
776 session_unlock_list();
780 * This thread manage event coming from the kernel.
782 * Features supported in this thread:
785 static void *thread_manage_kernel(void *data
)
787 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
788 uint32_t revents
, nb_fd
;
790 struct lttng_poll_event events
;
792 DBG("Thread manage kernel started");
794 health_code_update(&health_thread_kernel
);
796 ret
= create_thread_poll_set(&events
, 2);
798 goto error_poll_create
;
801 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
807 health_code_update(&health_thread_kernel
);
809 if (update_poll_flag
== 1) {
811 * Reset number of fd in the poll set. Always 2 since there is the thread
812 * quit pipe and the kernel pipe.
816 ret
= update_kernel_poll(&events
);
820 update_poll_flag
= 0;
823 nb_fd
= LTTNG_POLL_GETNB(&events
);
825 DBG("Thread kernel polling on %d fds", nb_fd
);
827 /* Zeroed the poll events */
828 lttng_poll_reset(&events
);
830 /* Poll infinite value of time */
832 health_poll_update(&health_thread_kernel
);
833 ret
= lttng_poll_wait(&events
, -1);
834 health_poll_update(&health_thread_kernel
);
837 * Restart interrupted system call.
839 if (errno
== EINTR
) {
843 } else if (ret
== 0) {
844 /* Should not happen since timeout is infinite */
845 ERR("Return value of poll is 0 with an infinite timeout.\n"
846 "This should not have happened! Continuing...");
850 for (i
= 0; i
< nb_fd
; i
++) {
851 /* Fetch once the poll data */
852 revents
= LTTNG_POLL_GETEV(&events
, i
);
853 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
855 health_code_update(&health_thread_kernel
);
857 /* Thread quit pipe has been closed. Killing thread. */
858 ret
= check_thread_quit_pipe(pollfd
, revents
);
864 /* Check for data on kernel pipe */
865 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
866 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
867 update_poll_flag
= 1;
871 * New CPU detected by the kernel. Adding kernel stream to
872 * kernel session and updating the kernel consumer
874 if (revents
& LPOLLIN
) {
875 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
881 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
882 * and unregister kernel stream at this point.
891 lttng_poll_clean(&events
);
894 health_error(&health_thread_kernel
);
895 ERR("Health error occurred in %s", __func__
);
897 health_exit(&health_thread_kernel
);
898 DBG("Kernel thread dying");
903 * This thread manage the consumer error sent back to the session daemon.
905 static void *thread_manage_consumer(void *data
)
907 int sock
= -1, i
, ret
, pollfd
, err
= -1;
908 uint32_t revents
, nb_fd
;
909 enum lttcomm_return_code code
;
910 struct lttng_poll_event events
;
911 struct consumer_data
*consumer_data
= data
;
913 DBG("[thread] Manage consumer started");
915 health_code_update(&consumer_data
->health
);
917 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
923 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
924 * Nothing more will be added to this poll set.
926 ret
= create_thread_poll_set(&events
, 2);
931 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
936 nb_fd
= LTTNG_POLL_GETNB(&events
);
938 health_code_update(&consumer_data
->health
);
940 /* Inifinite blocking call, waiting for transmission */
942 health_poll_update(&consumer_data
->health
);
943 ret
= lttng_poll_wait(&events
, -1);
944 health_poll_update(&consumer_data
->health
);
947 * Restart interrupted system call.
949 if (errno
== EINTR
) {
955 for (i
= 0; i
< nb_fd
; i
++) {
956 /* Fetch once the poll data */
957 revents
= LTTNG_POLL_GETEV(&events
, i
);
958 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
960 health_code_update(&consumer_data
->health
);
962 /* Thread quit pipe has been closed. Killing thread. */
963 ret
= check_thread_quit_pipe(pollfd
, revents
);
969 /* Event on the registration socket */
970 if (pollfd
== consumer_data
->err_sock
) {
971 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
972 ERR("consumer err socket poll error");
978 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
983 health_code_update(&consumer_data
->health
);
985 DBG2("Receiving code from consumer err_sock");
987 /* Getting status code from kconsumerd */
988 ret
= lttcomm_recv_unix_sock(sock
, &code
,
989 sizeof(enum lttcomm_return_code
));
994 health_code_update(&consumer_data
->health
);
996 if (code
== CONSUMERD_COMMAND_SOCK_READY
) {
997 consumer_data
->cmd_sock
=
998 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
999 if (consumer_data
->cmd_sock
< 0) {
1000 sem_post(&consumer_data
->sem
);
1001 PERROR("consumer connect");
1004 /* Signal condition to tell that the kconsumerd is ready */
1005 sem_post(&consumer_data
->sem
);
1006 DBG("consumer command socket ready");
1008 ERR("consumer error when waiting for SOCK_READY : %s",
1009 lttcomm_get_readable_code(-code
));
1013 /* Remove the kconsumerd error sock since we've established a connexion */
1014 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1019 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1024 health_code_update(&consumer_data
->health
);
1026 /* Update number of fd */
1027 nb_fd
= LTTNG_POLL_GETNB(&events
);
1029 /* Inifinite blocking call, waiting for transmission */
1031 health_poll_update(&consumer_data
->health
);
1032 ret
= lttng_poll_wait(&events
, -1);
1033 health_poll_update(&consumer_data
->health
);
1036 * Restart interrupted system call.
1038 if (errno
== EINTR
) {
1044 for (i
= 0; i
< nb_fd
; i
++) {
1045 /* Fetch once the poll data */
1046 revents
= LTTNG_POLL_GETEV(&events
, i
);
1047 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1049 health_code_update(&consumer_data
->health
);
1051 /* Thread quit pipe has been closed. Killing thread. */
1052 ret
= check_thread_quit_pipe(pollfd
, revents
);
1058 /* Event on the kconsumerd socket */
1059 if (pollfd
== sock
) {
1060 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1061 ERR("consumer err socket second poll error");
1067 health_code_update(&consumer_data
->health
);
1069 /* Wait for any kconsumerd error */
1070 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1071 sizeof(enum lttcomm_return_code
));
1073 ERR("consumer closed the command socket");
1077 ERR("consumer return code : %s", lttcomm_get_readable_code(-code
));
1081 /* Immediately set the consumerd state to stopped */
1082 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1083 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1084 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1085 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1086 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1088 /* Code flow error... */
1092 if (consumer_data
->err_sock
>= 0) {
1093 ret
= close(consumer_data
->err_sock
);
1098 if (consumer_data
->cmd_sock
>= 0) {
1099 ret
= close(consumer_data
->cmd_sock
);
1111 unlink(consumer_data
->err_unix_sock_path
);
1112 unlink(consumer_data
->cmd_unix_sock_path
);
1113 consumer_data
->pid
= 0;
1115 lttng_poll_clean(&events
);
1119 health_error(&consumer_data
->health
);
1120 ERR("Health error occurred in %s", __func__
);
1122 health_exit(&consumer_data
->health
);
1123 DBG("consumer thread cleanup completed");
1129 * This thread manage application communication.
1131 static void *thread_manage_apps(void *data
)
1133 int i
, ret
, pollfd
, err
= -1;
1134 uint32_t revents
, nb_fd
;
1135 struct ust_command ust_cmd
;
1136 struct lttng_poll_event events
;
1138 DBG("[thread] Manage application started");
1140 rcu_register_thread();
1141 rcu_thread_online();
1143 health_code_update(&health_thread_app_manage
);
1145 ret
= create_thread_poll_set(&events
, 2);
1147 goto error_poll_create
;
1150 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1155 health_code_update(&health_thread_app_manage
);
1158 /* Zeroed the events structure */
1159 lttng_poll_reset(&events
);
1161 nb_fd
= LTTNG_POLL_GETNB(&events
);
1163 DBG("Apps thread polling on %d fds", nb_fd
);
1165 /* Inifinite blocking call, waiting for transmission */
1167 health_poll_update(&health_thread_app_manage
);
1168 ret
= lttng_poll_wait(&events
, -1);
1169 health_poll_update(&health_thread_app_manage
);
1172 * Restart interrupted system call.
1174 if (errno
== EINTR
) {
1180 for (i
= 0; i
< nb_fd
; i
++) {
1181 /* Fetch once the poll data */
1182 revents
= LTTNG_POLL_GETEV(&events
, i
);
1183 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1185 health_code_update(&health_thread_app_manage
);
1187 /* Thread quit pipe has been closed. Killing thread. */
1188 ret
= check_thread_quit_pipe(pollfd
, revents
);
1194 /* Inspect the apps cmd pipe */
1195 if (pollfd
== apps_cmd_pipe
[0]) {
1196 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1197 ERR("Apps command pipe error");
1199 } else if (revents
& LPOLLIN
) {
1201 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
1202 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
1203 PERROR("read apps cmd pipe");
1207 health_code_update(&health_thread_app_manage
);
1209 /* Register applicaton to the session daemon */
1210 ret
= ust_app_register(&ust_cmd
.reg_msg
,
1212 if (ret
== -ENOMEM
) {
1214 } else if (ret
< 0) {
1218 health_code_update(&health_thread_app_manage
);
1221 * Validate UST version compatibility.
1223 ret
= ust_app_validate_version(ust_cmd
.sock
);
1226 * Add channel(s) and event(s) to newly registered apps
1227 * from lttng global UST domain.
1229 update_ust_app(ust_cmd
.sock
);
1232 health_code_update(&health_thread_app_manage
);
1234 ret
= ust_app_register_done(ust_cmd
.sock
);
1237 * If the registration is not possible, we simply
1238 * unregister the apps and continue
1240 ust_app_unregister(ust_cmd
.sock
);
1243 * We just need here to monitor the close of the UST
1244 * socket and poll set monitor those by default.
1245 * Listen on POLLIN (even if we never expect any
1246 * data) to ensure that hangup wakes us.
1248 ret
= lttng_poll_add(&events
, ust_cmd
.sock
, LPOLLIN
);
1253 DBG("Apps with sock %d added to poll set",
1257 health_code_update(&health_thread_app_manage
);
1263 * At this point, we know that a registered application made
1264 * the event at poll_wait.
1266 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1267 /* Removing from the poll set */
1268 ret
= lttng_poll_del(&events
, pollfd
);
1273 /* Socket closed on remote end. */
1274 ust_app_unregister(pollfd
);
1279 health_code_update(&health_thread_app_manage
);
1285 lttng_poll_clean(&events
);
1288 health_error(&health_thread_app_manage
);
1289 ERR("Health error occurred in %s", __func__
);
1291 health_exit(&health_thread_app_manage
);
1292 DBG("Application communication apps thread cleanup complete");
1293 rcu_thread_offline();
1294 rcu_unregister_thread();
1299 * Dispatch request from the registration threads to the application
1300 * communication thread.
1302 static void *thread_dispatch_ust_registration(void *data
)
1305 struct cds_wfq_node
*node
;
1306 struct ust_command
*ust_cmd
= NULL
;
1308 DBG("[thread] Dispatch UST command started");
1310 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1311 /* Atomically prepare the queue futex */
1312 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1315 /* Dequeue command for registration */
1316 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1318 DBG("Woken up but nothing in the UST command queue");
1319 /* Continue thread execution */
1323 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1325 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1326 " gid:%d sock:%d name:%s (version %d.%d)",
1327 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1328 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1329 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1330 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1332 * Inform apps thread of the new application registration. This
1333 * call is blocking so we can be assured that the data will be read
1334 * at some point in time or wait to the end of the world :)
1336 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1337 sizeof(struct ust_command
));
1339 PERROR("write apps cmd pipe");
1340 if (errno
== EBADF
) {
1342 * We can't inform the application thread to process
1343 * registration. We will exit or else application
1344 * registration will not occur and tracing will never
1351 } while (node
!= NULL
);
1353 /* Futex wait on queue. Blocking call on futex() */
1354 futex_nto1_wait(&ust_cmd_queue
.futex
);
1358 DBG("Dispatch thread dying");
1363 * This thread manage application registration.
1365 static void *thread_registration_apps(void *data
)
1367 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1368 uint32_t revents
, nb_fd
;
1369 struct lttng_poll_event events
;
1371 * Get allocated in this thread, enqueued to a global queue, dequeued and
1372 * freed in the manage apps thread.
1374 struct ust_command
*ust_cmd
= NULL
;
1376 DBG("[thread] Manage application registration started");
1378 ret
= lttcomm_listen_unix_sock(apps_sock
);
1384 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1385 * more will be added to this poll set.
1387 ret
= create_thread_poll_set(&events
, 2);
1389 goto error_create_poll
;
1392 /* Add the application registration socket */
1393 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1395 goto error_poll_add
;
1398 /* Notify all applications to register */
1399 ret
= notify_ust_apps(1);
1401 ERR("Failed to notify applications or create the wait shared memory.\n"
1402 "Execution continues but there might be problem for already\n"
1403 "running applications that wishes to register.");
1407 DBG("Accepting application registration");
1409 nb_fd
= LTTNG_POLL_GETNB(&events
);
1411 /* Inifinite blocking call, waiting for transmission */
1413 health_poll_update(&health_thread_app_reg
);
1414 ret
= lttng_poll_wait(&events
, -1);
1415 health_poll_update(&health_thread_app_reg
);
1418 * Restart interrupted system call.
1420 if (errno
== EINTR
) {
1426 for (i
= 0; i
< nb_fd
; i
++) {
1427 health_code_update(&health_thread_app_reg
);
1429 /* Fetch once the poll data */
1430 revents
= LTTNG_POLL_GETEV(&events
, i
);
1431 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1433 /* Thread quit pipe has been closed. Killing thread. */
1434 ret
= check_thread_quit_pipe(pollfd
, revents
);
1440 /* Event on the registration socket */
1441 if (pollfd
== apps_sock
) {
1442 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1443 ERR("Register apps socket poll error");
1445 } else if (revents
& LPOLLIN
) {
1446 sock
= lttcomm_accept_unix_sock(apps_sock
);
1451 /* Create UST registration command for enqueuing */
1452 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1453 if (ust_cmd
== NULL
) {
1454 PERROR("ust command zmalloc");
1459 * Using message-based transmissions to ensure we don't
1460 * have to deal with partially received messages.
1462 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1464 ERR("Exhausted file descriptors allowed for applications.");
1473 health_code_update(&health_thread_app_reg
);
1474 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1475 sizeof(struct ust_register_msg
));
1476 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1478 PERROR("lttcomm_recv_unix_sock register apps");
1480 ERR("Wrong size received on apps register");
1487 lttng_fd_put(LTTNG_FD_APPS
, 1);
1491 health_code_update(&health_thread_app_reg
);
1493 ust_cmd
->sock
= sock
;
1496 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1497 " gid:%d sock:%d name:%s (version %d.%d)",
1498 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1499 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1500 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1501 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1504 * Lock free enqueue the registration request. The red pill
1505 * has been taken! This apps will be part of the *system*.
1507 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1510 * Wake the registration queue futex. Implicit memory
1511 * barrier with the exchange in cds_wfq_enqueue.
1513 futex_nto1_wake(&ust_cmd_queue
.futex
);
1522 health_error(&health_thread_app_reg
);
1523 ERR("Health error occurred in %s", __func__
);
1525 health_exit(&health_thread_app_reg
);
1527 /* Notify that the registration thread is gone */
1530 if (apps_sock
>= 0) {
1531 ret
= close(apps_sock
);
1541 lttng_fd_put(LTTNG_FD_APPS
, 1);
1543 unlink(apps_unix_sock_path
);
1546 lttng_poll_clean(&events
);
1549 DBG("UST Registration thread cleanup complete");
1555 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1556 * exec or it will fails.
1558 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
1561 struct timespec timeout
;
1563 timeout
.tv_sec
= DEFAULT_SEM_WAIT_TIMEOUT
;
1564 timeout
.tv_nsec
= 0;
1566 /* Setup semaphore */
1567 ret
= sem_init(&consumer_data
->sem
, 0, 0);
1569 PERROR("sem_init consumer semaphore");
1573 ret
= pthread_create(&consumer_data
->thread
, NULL
,
1574 thread_manage_consumer
, consumer_data
);
1576 PERROR("pthread_create consumer");
1581 /* Get time for sem_timedwait absolute timeout */
1582 ret
= clock_gettime(CLOCK_REALTIME
, &timeout
);
1584 PERROR("clock_gettime spawn consumer");
1585 /* Infinite wait for the kconsumerd thread to be ready */
1586 ret
= sem_wait(&consumer_data
->sem
);
1588 /* Normal timeout if the gettime was successful */
1589 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
1590 ret
= sem_timedwait(&consumer_data
->sem
, &timeout
);
1594 if (errno
== ETIMEDOUT
) {
1596 * Call has timed out so we kill the kconsumerd_thread and return
1599 ERR("The consumer thread was never ready. Killing it");
1600 ret
= pthread_cancel(consumer_data
->thread
);
1602 PERROR("pthread_cancel consumer thread");
1605 PERROR("semaphore wait failed consumer thread");
1610 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1611 if (consumer_data
->pid
== 0) {
1612 ERR("Kconsumerd did not start");
1613 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1616 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1625 * Join consumer thread
1627 static int join_consumer_thread(struct consumer_data
*consumer_data
)
1632 /* Consumer pid must be a real one. */
1633 if (consumer_data
->pid
> 0) {
1634 ret
= kill(consumer_data
->pid
, SIGTERM
);
1636 ERR("Error killing consumer daemon");
1639 return pthread_join(consumer_data
->thread
, &status
);
1646 * Fork and exec a consumer daemon (consumerd).
1648 * Return pid if successful else -1.
1650 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
1654 const char *consumer_to_use
;
1655 const char *verbosity
;
1658 DBG("Spawning consumerd");
1665 if (opt_verbose_consumer
) {
1666 verbosity
= "--verbose";
1668 verbosity
= "--quiet";
1670 switch (consumer_data
->type
) {
1671 case LTTNG_CONSUMER_KERNEL
:
1673 * Find out which consumerd to execute. We will first try the
1674 * 64-bit path, then the sessiond's installation directory, and
1675 * fallback on the 32-bit one,
1677 DBG3("Looking for a kernel consumer at these locations:");
1678 DBG3(" 1) %s", consumerd64_bin
);
1679 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
1680 DBG3(" 3) %s", consumerd32_bin
);
1681 if (stat(consumerd64_bin
, &st
) == 0) {
1682 DBG3("Found location #1");
1683 consumer_to_use
= consumerd64_bin
;
1684 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
1685 DBG3("Found location #2");
1686 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
1687 } else if (stat(consumerd32_bin
, &st
) == 0) {
1688 DBG3("Found location #3");
1689 consumer_to_use
= consumerd32_bin
;
1691 DBG("Could not find any valid consumerd executable");
1694 DBG("Using kernel consumer at: %s", consumer_to_use
);
1695 execl(consumer_to_use
,
1696 "lttng-consumerd", verbosity
, "-k",
1697 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1698 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1701 case LTTNG_CONSUMER64_UST
:
1703 char *tmpnew
= NULL
;
1705 if (consumerd64_libdir
[0] != '\0') {
1709 tmp
= getenv("LD_LIBRARY_PATH");
1713 tmplen
= strlen("LD_LIBRARY_PATH=")
1714 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
1715 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1720 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1721 strcat(tmpnew
, consumerd64_libdir
);
1722 if (tmp
[0] != '\0') {
1723 strcat(tmpnew
, ":");
1724 strcat(tmpnew
, tmp
);
1726 ret
= putenv(tmpnew
);
1732 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
1733 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
1734 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1735 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1737 if (consumerd64_libdir
[0] != '\0') {
1745 case LTTNG_CONSUMER32_UST
:
1747 char *tmpnew
= NULL
;
1749 if (consumerd32_libdir
[0] != '\0') {
1753 tmp
= getenv("LD_LIBRARY_PATH");
1757 tmplen
= strlen("LD_LIBRARY_PATH=")
1758 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
1759 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1764 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1765 strcat(tmpnew
, consumerd32_libdir
);
1766 if (tmp
[0] != '\0') {
1767 strcat(tmpnew
, ":");
1768 strcat(tmpnew
, tmp
);
1770 ret
= putenv(tmpnew
);
1776 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
1777 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
1778 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1779 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1781 if (consumerd32_libdir
[0] != '\0') {
1790 PERROR("unknown consumer type");
1794 PERROR("kernel start consumer exec");
1797 } else if (pid
> 0) {
1800 PERROR("start consumer fork");
1808 * Spawn the consumerd daemon and session daemon thread.
1810 static int start_consumerd(struct consumer_data
*consumer_data
)
1814 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1815 if (consumer_data
->pid
!= 0) {
1816 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1820 ret
= spawn_consumerd(consumer_data
);
1822 ERR("Spawning consumerd failed");
1823 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1827 /* Setting up the consumer_data pid */
1828 consumer_data
->pid
= ret
;
1829 DBG2("Consumer pid %d", consumer_data
->pid
);
1830 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1832 DBG2("Spawning consumer control thread");
1833 ret
= spawn_consumer_thread(consumer_data
);
1835 ERR("Fatal error spawning consumer control thread");
1847 * Compute health status of each consumer. If one of them is zero (bad
1848 * state), we return 0.
1850 static int check_consumer_health(void)
1854 ret
= health_check_state(&kconsumer_data
.health
) &&
1855 health_check_state(&ustconsumer32_data
.health
) &&
1856 health_check_state(&ustconsumer64_data
.health
);
1858 DBG3("Health consumer check %d", ret
);
1864 * Check version of the lttng-modules.
1866 static int validate_lttng_modules_version(void)
1868 return kernel_validate_version(kernel_tracer_fd
);
1872 * Setup necessary data for kernel tracer action.
1874 static int init_kernel_tracer(void)
1878 /* Modprobe lttng kernel modules */
1879 ret
= modprobe_lttng_control();
1884 /* Open debugfs lttng */
1885 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1886 if (kernel_tracer_fd
< 0) {
1887 DBG("Failed to open %s", module_proc_lttng
);
1892 /* Validate kernel version */
1893 ret
= validate_lttng_modules_version();
1898 ret
= modprobe_lttng_data();
1903 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1907 modprobe_remove_lttng_control();
1908 ret
= close(kernel_tracer_fd
);
1912 kernel_tracer_fd
= -1;
1913 return LTTCOMM_KERN_VERSION
;
1916 ret
= close(kernel_tracer_fd
);
1922 modprobe_remove_lttng_control();
1925 WARN("No kernel tracer available");
1926 kernel_tracer_fd
= -1;
1928 return LTTCOMM_NEED_ROOT_SESSIOND
;
1930 return LTTCOMM_KERN_NA
;
1935 * Init tracing by creating trace directory and sending fds kernel consumer.
1937 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
1940 struct lttng_ht_iter iter
;
1941 struct consumer_socket
*socket
;
1945 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
1946 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
1947 socket
, node
.node
) {
1948 /* Code flow error */
1949 assert(socket
->fd
>= 0);
1951 pthread_mutex_lock(socket
->lock
);
1952 ret
= kernel_consumer_send_session(socket
->fd
, session
);
1953 pthread_mutex_unlock(socket
->lock
);
1955 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1966 * Create a socket to the relayd using the URI.
1968 * On success, the relayd_sock pointer is set to the created socket.
1969 * Else, it is untouched and an lttcomm error code is returned.
1971 static int create_connect_relayd(struct consumer_output
*output
,
1972 const char *session_name
, struct lttng_uri
*uri
,
1973 struct lttcomm_sock
**relayd_sock
)
1976 struct lttcomm_sock
*sock
;
1978 /* Create socket object from URI */
1979 sock
= lttcomm_alloc_sock_from_uri(uri
);
1981 ret
= LTTCOMM_FATAL
;
1985 ret
= lttcomm_create_sock(sock
);
1987 ret
= LTTCOMM_FATAL
;
1991 /* Connect to relayd so we can proceed with a session creation. */
1992 ret
= relayd_connect(sock
);
1994 ERR("Unable to reach lttng-relayd");
1995 ret
= LTTCOMM_RELAYD_SESSION_FAIL
;
1999 /* Create socket for control stream. */
2000 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
2001 DBG3("Creating relayd stream socket from URI");
2003 /* Check relayd version */
2004 ret
= relayd_version_check(sock
, LTTNG_UST_COMM_MAJOR
, 0);
2006 ret
= LTTCOMM_RELAYD_VERSION_FAIL
;
2009 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
2010 DBG3("Creating relayd data socket from URI");
2012 /* Command is not valid */
2013 ERR("Relayd invalid stream type: %d", uri
->stype
);
2014 ret
= LTTCOMM_INVALID
;
2018 *relayd_sock
= sock
;
2024 (void) relayd_close(sock
);
2028 lttcomm_destroy_sock(sock
);
2035 * Connect to the relayd using URI and send the socket to the right consumer.
2037 static int send_socket_relayd_consumer(int domain
, struct ltt_session
*session
,
2038 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
2042 struct lttcomm_sock
*sock
= NULL
;
2044 /* Set the network sequence index if not set. */
2045 if (consumer
->net_seq_index
== -1) {
2047 * Increment net_seq_idx because we are about to transfer the
2048 * new relayd socket to the consumer.
2050 uatomic_inc(&relayd_net_seq_idx
);
2051 /* Assign unique key so the consumer can match streams */
2052 consumer
->net_seq_index
= uatomic_read(&relayd_net_seq_idx
);
2055 /* Connect to relayd and make version check if uri is the control. */
2056 ret
= create_connect_relayd(consumer
, session
->name
, relayd_uri
, &sock
);
2057 if (ret
!= LTTCOMM_OK
) {
2061 /* If the control socket is connected, network session is ready */
2062 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
2063 session
->net_handle
= 1;
2066 /* Send relayd socket to consumer. */
2067 ret
= consumer_send_relayd_socket(consumer_fd
, sock
,
2068 consumer
, relayd_uri
->stype
);
2070 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
2077 * Close socket which was dup on the consumer side. The session daemon does
2078 * NOT keep track of the relayd socket(s) once transfer to the consumer.
2083 (void) relayd_close(sock
);
2084 lttcomm_destroy_sock(sock
);
2091 * Send both relayd sockets to a specific consumer and domain. This is a
2092 * helper function to facilitate sending the information to the consumer for a
2095 static int send_sockets_relayd_consumer(int domain
,
2096 struct ltt_session
*session
, struct consumer_output
*consumer
, int fd
)
2103 /* Don't resend the sockets to the consumer. */
2104 if (consumer
->dst
.net
.relayd_socks_sent
) {
2109 /* Sending control relayd socket. */
2110 ret
= send_socket_relayd_consumer(domain
, session
,
2111 &consumer
->dst
.net
.control
, consumer
, fd
);
2112 if (ret
!= LTTCOMM_OK
) {
2116 /* Sending data relayd socket. */
2117 ret
= send_socket_relayd_consumer(domain
, session
,
2118 &consumer
->dst
.net
.data
, consumer
, fd
);
2119 if (ret
!= LTTCOMM_OK
) {
2123 /* Flag that all relayd sockets were sent to the consumer. */
2124 consumer
->dst
.net
.relayd_socks_sent
= 1;
2131 * Setup relayd connections for a tracing session. First creates the socket to
2132 * the relayd and send them to the right domain consumer. Consumer type MUST be
2135 static int setup_relayd(struct ltt_session
*session
)
2137 int ret
= LTTCOMM_OK
;
2138 struct ltt_ust_session
*usess
;
2139 struct ltt_kernel_session
*ksess
;
2140 struct consumer_socket
*socket
;
2141 struct lttng_ht_iter iter
;
2145 usess
= session
->ust_session
;
2146 ksess
= session
->kernel_session
;
2148 DBG2("Setting relayd for session %s", session
->name
);
2150 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
2151 && usess
->consumer
->enabled
) {
2152 /* For each consumer socket, send relayd sockets */
2153 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
2154 socket
, node
.node
) {
2155 /* Code flow error */
2156 assert(socket
->fd
>= 0);
2158 pthread_mutex_lock(socket
->lock
);
2159 send_sockets_relayd_consumer(LTTNG_DOMAIN_UST
, session
,
2160 usess
->consumer
, socket
->fd
);
2161 pthread_mutex_unlock(socket
->lock
);
2162 if (ret
!= LTTCOMM_OK
) {
2168 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
2169 && ksess
->consumer
->enabled
) {
2170 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
2171 socket
, node
.node
) {
2172 /* Code flow error */
2173 assert(socket
->fd
>= 0);
2175 pthread_mutex_lock(socket
->lock
);
2176 send_sockets_relayd_consumer(LTTNG_DOMAIN_KERNEL
, session
,
2177 ksess
->consumer
, socket
->fd
);
2178 pthread_mutex_unlock(socket
->lock
);
2179 if (ret
!= LTTCOMM_OK
) {
2190 * Set consumer subdirectory using the session name and a generated datetime if
2191 * needed. This is appended to the current subdirectory.
2193 static int set_consumer_subdir(struct consumer_output
*consumer
,
2194 const char *session_name
)
2197 unsigned int have_default_name
= 0;
2198 char datetime
[16], tmp_path
[PATH_MAX
];
2200 struct tm
*timeinfo
;
2203 assert(session_name
);
2205 memset(tmp_path
, 0, sizeof(tmp_path
));
2207 /* Flag if we have a default session. */
2208 if (strncmp(session_name
, DEFAULT_SESSION_NAME
"-",
2209 strlen(DEFAULT_SESSION_NAME
) + 1) == 0) {
2210 have_default_name
= 1;
2212 /* Get date and time for session path */
2214 timeinfo
= localtime(&rawtime
);
2215 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2218 if (have_default_name
) {
2219 ret
= snprintf(tmp_path
, sizeof(tmp_path
),
2220 "%s/%s", consumer
->subdir
, session_name
);
2222 ret
= snprintf(tmp_path
, sizeof(tmp_path
),
2223 "%s/%s-%s/", consumer
->subdir
, session_name
, datetime
);
2226 PERROR("snprintf session name date");
2230 strncpy(consumer
->subdir
, tmp_path
, sizeof(consumer
->subdir
));
2231 DBG2("Consumer subdir set to %s", consumer
->subdir
);
2238 * Copy consumer output from the tracing session to the domain session. The
2239 * function also applies the right modification on a per domain basis for the
2240 * trace files destination directory.
2242 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2245 const char *dir_name
;
2246 struct consumer_output
*consumer
;
2249 assert(session
->consumer
);
2252 case LTTNG_DOMAIN_KERNEL
:
2253 DBG3("Copying tracing session consumer output in kernel session");
2254 session
->kernel_session
->consumer
=
2255 consumer_copy_output(session
->consumer
);
2256 /* Ease our life a bit for the next part */
2257 consumer
= session
->kernel_session
->consumer
;
2258 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2260 case LTTNG_DOMAIN_UST
:
2261 DBG3("Copying tracing session consumer output in UST session");
2262 session
->ust_session
->consumer
=
2263 consumer_copy_output(session
->consumer
);
2264 /* Ease our life a bit for the next part */
2265 consumer
= session
->ust_session
->consumer
;
2266 dir_name
= DEFAULT_UST_TRACE_DIR
;
2269 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2273 ret
= set_consumer_subdir(session
->consumer
, session
->name
);
2275 ret
= LTTCOMM_FATAL
;
2279 /* Append correct directory to subdir */
2280 strncat(consumer
->subdir
, dir_name
, sizeof(consumer
->subdir
));
2281 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2290 * Create an UST session and add it to the session ust list.
2292 static int create_ust_session(struct ltt_session
*session
,
2293 struct lttng_domain
*domain
)
2296 struct ltt_ust_session
*lus
= NULL
;
2300 assert(session
->consumer
);
2302 switch (domain
->type
) {
2303 case LTTNG_DOMAIN_UST
:
2306 ERR("Unknown UST domain on create session %d", domain
->type
);
2307 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2311 DBG("Creating UST session");
2313 lus
= trace_ust_create_session(session
->path
, session
->id
, domain
);
2315 ret
= LTTCOMM_UST_SESS_FAIL
;
2319 lus
->uid
= session
->uid
;
2320 lus
->gid
= session
->gid
;
2321 session
->ust_session
= lus
;
2323 /* Copy session output to the newly created UST session */
2324 ret
= copy_session_consumer(domain
->type
, session
);
2325 if (ret
!= LTTCOMM_OK
) {
2333 session
->ust_session
= NULL
;
2338 * Create a kernel tracer session then create the default channel.
2340 static int create_kernel_session(struct ltt_session
*session
)
2344 DBG("Creating kernel session");
2346 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2348 ret
= LTTCOMM_KERN_SESS_FAIL
;
2352 /* Code flow safety */
2353 assert(session
->kernel_session
);
2355 /* Copy session output to the newly created Kernel session */
2356 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2357 if (ret
!= LTTCOMM_OK
) {
2361 /* Create directory(ies) on local filesystem. */
2362 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2363 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2364 ret
= run_as_mkdir_recursive(
2365 session
->kernel_session
->consumer
->dst
.trace_path
,
2366 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2368 if (ret
!= -EEXIST
) {
2369 ERR("Trace directory creation error");
2375 session
->kernel_session
->uid
= session
->uid
;
2376 session
->kernel_session
->gid
= session
->gid
;
2381 trace_kernel_destroy_session(session
->kernel_session
);
2382 session
->kernel_session
= NULL
;
2387 * Check if the UID or GID match the session. Root user has access to all
2390 static int session_access_ok(struct ltt_session
*session
, uid_t uid
, gid_t gid
)
2392 if (uid
!= session
->uid
&& gid
!= session
->gid
&& uid
!= 0) {
2400 * Count number of session permitted by uid/gid.
2402 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2405 struct ltt_session
*session
;
2407 DBG("Counting number of available session for UID %d GID %d",
2409 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2411 * Only list the sessions the user can control.
2413 if (!session_access_ok(session
, uid
, gid
)) {
2422 * Using the session list, filled a lttng_session array to send back to the
2423 * client for session listing.
2425 * The session list lock MUST be acquired before calling this function. Use
2426 * session_lock_list() and session_unlock_list().
2428 static void list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2432 struct ltt_session
*session
;
2434 DBG("Getting all available session for UID %d GID %d",
2437 * Iterate over session list and append data after the control struct in
2440 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2442 * Only list the sessions the user can control.
2444 if (!session_access_ok(session
, uid
, gid
)) {
2447 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
2448 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
2449 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2450 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2451 sessions
[i
].enabled
= session
->enabled
;
2457 * Fill lttng_channel array of all channels.
2459 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
2460 struct lttng_channel
*channels
)
2463 struct ltt_kernel_channel
*kchan
;
2465 DBG("Listing channels for session %s", session
->name
);
2468 case LTTNG_DOMAIN_KERNEL
:
2469 /* Kernel channels */
2470 if (session
->kernel_session
!= NULL
) {
2471 cds_list_for_each_entry(kchan
,
2472 &session
->kernel_session
->channel_list
.head
, list
) {
2473 /* Copy lttng_channel struct to array */
2474 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
2475 channels
[i
].enabled
= kchan
->enabled
;
2480 case LTTNG_DOMAIN_UST
:
2482 struct lttng_ht_iter iter
;
2483 struct ltt_ust_channel
*uchan
;
2485 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
2486 &iter
.iter
, uchan
, node
.node
) {
2487 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
2488 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
2489 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2490 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2491 channels
[i
].attr
.switch_timer_interval
=
2492 uchan
->attr
.switch_timer_interval
;
2493 channels
[i
].attr
.read_timer_interval
=
2494 uchan
->attr
.read_timer_interval
;
2495 channels
[i
].enabled
= uchan
->enabled
;
2496 switch (uchan
->attr
.output
) {
2497 case LTTNG_UST_MMAP
:
2499 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
2512 * Create a list of ust global domain events.
2514 static int list_lttng_ust_global_events(char *channel_name
,
2515 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
2518 unsigned int nb_event
= 0;
2519 struct lttng_ht_iter iter
;
2520 struct lttng_ht_node_str
*node
;
2521 struct ltt_ust_channel
*uchan
;
2522 struct ltt_ust_event
*uevent
;
2523 struct lttng_event
*tmp
;
2525 DBG("Listing UST global events for channel %s", channel_name
);
2529 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
2530 node
= lttng_ht_iter_get_node_str(&iter
);
2532 ret
= -LTTCOMM_UST_CHAN_NOT_FOUND
;
2536 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
2538 nb_event
+= lttng_ht_get_count(uchan
->events
);
2540 if (nb_event
== 0) {
2545 DBG3("Listing UST global %d events", nb_event
);
2547 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
2549 ret
= -LTTCOMM_FATAL
;
2553 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
2554 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
2555 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
2556 tmp
[i
].enabled
= uevent
->enabled
;
2557 switch (uevent
->attr
.instrumentation
) {
2558 case LTTNG_UST_TRACEPOINT
:
2559 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2561 case LTTNG_UST_PROBE
:
2562 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
2564 case LTTNG_UST_FUNCTION
:
2565 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
2568 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
2569 switch (uevent
->attr
.loglevel_type
) {
2570 case LTTNG_UST_LOGLEVEL_ALL
:
2571 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2573 case LTTNG_UST_LOGLEVEL_RANGE
:
2574 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
2576 case LTTNG_UST_LOGLEVEL_SINGLE
:
2577 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
2580 if (uevent
->filter
) {
2595 * Fill lttng_event array of all kernel events in the channel.
2597 static int list_lttng_kernel_events(char *channel_name
,
2598 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
2601 unsigned int nb_event
;
2602 struct ltt_kernel_event
*event
;
2603 struct ltt_kernel_channel
*kchan
;
2605 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
2606 if (kchan
== NULL
) {
2607 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2611 nb_event
= kchan
->event_count
;
2613 DBG("Listing events for channel %s", kchan
->channel
->name
);
2615 if (nb_event
== 0) {
2620 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
2621 if (*events
== NULL
) {
2622 ret
= LTTCOMM_FATAL
;
2626 /* Kernel channels */
2627 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
2628 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
2629 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
2630 (*events
)[i
].enabled
= event
->enabled
;
2631 switch (event
->event
->instrumentation
) {
2632 case LTTNG_KERNEL_TRACEPOINT
:
2633 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2635 case LTTNG_KERNEL_KPROBE
:
2636 case LTTNG_KERNEL_KRETPROBE
:
2637 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
2638 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
2639 sizeof(struct lttng_kernel_kprobe
));
2641 case LTTNG_KERNEL_FUNCTION
:
2642 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
2643 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
2644 sizeof(struct lttng_kernel_function
));
2646 case LTTNG_KERNEL_NOOP
:
2647 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
2649 case LTTNG_KERNEL_SYSCALL
:
2650 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
2652 case LTTNG_KERNEL_ALL
:
2667 * Add URI so the consumer output object. Set the correct path depending on the
2668 * domain adding the default trace directory.
2670 static int add_uri_to_consumer(struct consumer_output
*consumer
,
2671 struct lttng_uri
*uri
, int domain
, const char *session_name
)
2673 int ret
= LTTCOMM_OK
;
2674 const char *default_trace_dir
;
2678 if (consumer
== NULL
) {
2679 DBG("No consumer detected. Don't add URI. Stopping.");
2680 ret
= LTTCOMM_NO_CONSUMER
;
2685 case LTTNG_DOMAIN_KERNEL
:
2686 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
2688 case LTTNG_DOMAIN_UST
:
2689 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
2693 * This case is possible is we try to add the URI to the global tracing
2694 * session consumer object which in this case there is no subdir.
2696 default_trace_dir
= "";
2699 switch (uri
->dtype
) {
2700 case LTTNG_DST_IPV4
:
2701 case LTTNG_DST_IPV6
:
2702 DBG2("Setting network URI to consumer");
2704 /* Set URI into consumer output object */
2705 ret
= consumer_set_network_uri(consumer
, uri
);
2707 ret
= LTTCOMM_FATAL
;
2711 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
2712 ret
= set_consumer_subdir(consumer
, session_name
);
2714 ret
= LTTCOMM_FATAL
;
2719 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
2720 /* On a new subdir, reappend the default trace dir. */
2721 strncat(consumer
->subdir
, default_trace_dir
, sizeof(consumer
->subdir
));
2722 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
2726 case LTTNG_DST_PATH
:
2727 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
2728 memset(consumer
->dst
.trace_path
, 0,
2729 sizeof(consumer
->dst
.trace_path
));
2730 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
2731 sizeof(consumer
->dst
.trace_path
));
2732 /* Append default trace dir */
2733 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
2734 sizeof(consumer
->dst
.trace_path
));
2735 /* Flag consumer as local. */
2736 consumer
->type
= CONSUMER_DST_LOCAL
;
2745 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
2747 static int cmd_disable_channel(struct ltt_session
*session
,
2748 int domain
, char *channel_name
)
2751 struct ltt_ust_session
*usess
;
2753 usess
= session
->ust_session
;
2756 case LTTNG_DOMAIN_KERNEL
:
2758 ret
= channel_kernel_disable(session
->kernel_session
,
2760 if (ret
!= LTTCOMM_OK
) {
2764 kernel_wait_quiescent(kernel_tracer_fd
);
2767 case LTTNG_DOMAIN_UST
:
2769 struct ltt_ust_channel
*uchan
;
2770 struct lttng_ht
*chan_ht
;
2772 chan_ht
= usess
->domain_global
.channels
;
2774 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
2775 if (uchan
== NULL
) {
2776 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2780 ret
= channel_ust_disable(usess
, domain
, uchan
);
2781 if (ret
!= LTTCOMM_OK
) {
2787 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2788 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2789 case LTTNG_DOMAIN_UST_PID
:
2792 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2803 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
2805 static int cmd_enable_channel(struct ltt_session
*session
,
2806 int domain
, struct lttng_channel
*attr
)
2809 struct ltt_ust_session
*usess
= session
->ust_session
;
2810 struct lttng_ht
*chan_ht
;
2812 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
2815 case LTTNG_DOMAIN_KERNEL
:
2817 struct ltt_kernel_channel
*kchan
;
2819 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
2820 session
->kernel_session
);
2821 if (kchan
== NULL
) {
2822 ret
= channel_kernel_create(session
->kernel_session
,
2823 attr
, kernel_poll_pipe
[1]);
2825 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
2828 if (ret
!= LTTCOMM_OK
) {
2832 kernel_wait_quiescent(kernel_tracer_fd
);
2835 case LTTNG_DOMAIN_UST
:
2837 struct ltt_ust_channel
*uchan
;
2839 chan_ht
= usess
->domain_global
.channels
;
2841 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
2842 if (uchan
== NULL
) {
2843 ret
= channel_ust_create(usess
, domain
, attr
);
2845 ret
= channel_ust_enable(usess
, domain
, uchan
);
2850 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2851 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2852 case LTTNG_DOMAIN_UST_PID
:
2855 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2864 * Command LTTNG_DISABLE_EVENT processed by the client thread.
2866 static int cmd_disable_event(struct ltt_session
*session
, int domain
,
2867 char *channel_name
, char *event_name
)
2872 case LTTNG_DOMAIN_KERNEL
:
2874 struct ltt_kernel_channel
*kchan
;
2875 struct ltt_kernel_session
*ksess
;
2877 ksess
= session
->kernel_session
;
2879 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
2880 if (kchan
== NULL
) {
2881 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2885 ret
= event_kernel_disable_tracepoint(ksess
, kchan
, event_name
);
2886 if (ret
!= LTTCOMM_OK
) {
2890 kernel_wait_quiescent(kernel_tracer_fd
);
2893 case LTTNG_DOMAIN_UST
:
2895 struct ltt_ust_channel
*uchan
;
2896 struct ltt_ust_session
*usess
;
2898 usess
= session
->ust_session
;
2900 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2902 if (uchan
== NULL
) {
2903 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2907 ret
= event_ust_disable_tracepoint(usess
, domain
, uchan
, event_name
);
2908 if (ret
!= LTTCOMM_OK
) {
2912 DBG3("Disable UST event %s in channel %s completed", event_name
,
2917 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2918 case LTTNG_DOMAIN_UST_PID
:
2919 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2933 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
2935 static int cmd_disable_event_all(struct ltt_session
*session
, int domain
,
2941 case LTTNG_DOMAIN_KERNEL
:
2943 struct ltt_kernel_session
*ksess
;
2944 struct ltt_kernel_channel
*kchan
;
2946 ksess
= session
->kernel_session
;
2948 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
2949 if (kchan
== NULL
) {
2950 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2954 ret
= event_kernel_disable_all(ksess
, kchan
);
2955 if (ret
!= LTTCOMM_OK
) {
2959 kernel_wait_quiescent(kernel_tracer_fd
);
2962 case LTTNG_DOMAIN_UST
:
2964 struct ltt_ust_session
*usess
;
2965 struct ltt_ust_channel
*uchan
;
2967 usess
= session
->ust_session
;
2969 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2971 if (uchan
== NULL
) {
2972 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2976 ret
= event_ust_disable_all_tracepoints(usess
, domain
, uchan
);
2981 DBG3("Disable all UST events in channel %s completed", channel_name
);
2986 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2987 case LTTNG_DOMAIN_UST_PID
:
2988 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
3002 * Command LTTNG_ADD_CONTEXT processed by the client thread.
3004 static int cmd_add_context(struct ltt_session
*session
, int domain
,
3005 char *channel_name
, char *event_name
, struct lttng_event_context
*ctx
)
3010 case LTTNG_DOMAIN_KERNEL
:
3011 /* Add kernel context to kernel tracer */
3012 ret
= context_kernel_add(session
->kernel_session
, ctx
,
3013 event_name
, channel_name
);
3014 if (ret
!= LTTCOMM_OK
) {
3018 case LTTNG_DOMAIN_UST
:
3020 struct ltt_ust_session
*usess
= session
->ust_session
;
3022 ret
= context_ust_add(usess
, domain
, ctx
, event_name
, channel_name
);
3023 if (ret
!= LTTCOMM_OK
) {
3029 case LTTNG_DOMAIN_UST_EXEC_NAME
:
3030 case LTTNG_DOMAIN_UST_PID
:
3031 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
3045 * Command LTTNG_SET_FILTER processed by the client thread.
3047 static int cmd_set_filter(struct ltt_session
*session
, int domain
,
3048 char *channel_name
, char *event_name
,
3049 struct lttng_filter_bytecode
*bytecode
)
3054 case LTTNG_DOMAIN_KERNEL
:
3055 ret
= LTTCOMM_FATAL
;
3057 case LTTNG_DOMAIN_UST
:
3059 struct ltt_ust_session
*usess
= session
->ust_session
;
3061 ret
= filter_ust_set(usess
, domain
, bytecode
, event_name
, channel_name
);
3062 if (ret
!= LTTCOMM_OK
) {
3068 case LTTNG_DOMAIN_UST_EXEC_NAME
:
3069 case LTTNG_DOMAIN_UST_PID
:
3070 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
3085 * Command LTTNG_ENABLE_EVENT processed by the client thread.
3087 static int cmd_enable_event(struct ltt_session
*session
, int domain
,
3088 char *channel_name
, struct lttng_event
*event
)
3091 struct lttng_channel
*attr
;
3092 struct ltt_ust_session
*usess
= session
->ust_session
;
3095 case LTTNG_DOMAIN_KERNEL
:
3097 struct ltt_kernel_channel
*kchan
;
3099 kchan
= trace_kernel_get_channel_by_name(channel_name
,
3100 session
->kernel_session
);
3101 if (kchan
== NULL
) {
3102 attr
= channel_new_default_attr(domain
);
3104 ret
= LTTCOMM_FATAL
;
3107 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
3109 /* This call will notify the kernel thread */
3110 ret
= channel_kernel_create(session
->kernel_session
,
3111 attr
, kernel_poll_pipe
[1]);
3112 if (ret
!= LTTCOMM_OK
) {
3119 /* Get the newly created kernel channel pointer */
3120 kchan
= trace_kernel_get_channel_by_name(channel_name
,
3121 session
->kernel_session
);
3122 if (kchan
== NULL
) {
3123 /* This sould not happen... */
3124 ret
= LTTCOMM_FATAL
;
3128 ret
= event_kernel_enable_tracepoint(session
->kernel_session
, kchan
,
3130 if (ret
!= LTTCOMM_OK
) {
3134 kernel_wait_quiescent(kernel_tracer_fd
);
3137 case LTTNG_DOMAIN_UST
:
3139 struct lttng_channel
*attr
;
3140 struct ltt_ust_channel
*uchan
;
3142 /* Get channel from global UST domain */
3143 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
3145 if (uchan
== NULL
) {
3146 /* Create default channel */
3147 attr
= channel_new_default_attr(domain
);
3149 ret
= LTTCOMM_FATAL
;
3152 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
3153 attr
->name
[NAME_MAX
- 1] = '\0';
3155 ret
= channel_ust_create(usess
, domain
, attr
);
3156 if (ret
!= LTTCOMM_OK
) {
3162 /* Get the newly created channel reference back */
3163 uchan
= trace_ust_find_channel_by_name(
3164 usess
->domain_global
.channels
, channel_name
);
3165 if (uchan
== NULL
) {
3166 /* Something is really wrong */
3167 ret
= LTTCOMM_FATAL
;
3172 /* At this point, the session and channel exist on the tracer */
3173 ret
= event_ust_enable_tracepoint(usess
, domain
, uchan
, event
);
3174 if (ret
!= LTTCOMM_OK
) {
3180 case LTTNG_DOMAIN_UST_EXEC_NAME
:
3181 case LTTNG_DOMAIN_UST_PID
:
3182 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
3196 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
3198 static int cmd_enable_event_all(struct ltt_session
*session
, int domain
,
3199 char *channel_name
, int event_type
)
3202 struct ltt_kernel_channel
*kchan
;
3205 case LTTNG_DOMAIN_KERNEL
:
3206 kchan
= trace_kernel_get_channel_by_name(channel_name
,
3207 session
->kernel_session
);
3208 if (kchan
== NULL
) {
3209 /* This call will notify the kernel thread */
3210 ret
= channel_kernel_create(session
->kernel_session
, NULL
,
3211 kernel_poll_pipe
[1]);
3212 if (ret
!= LTTCOMM_OK
) {
3216 /* Get the newly created kernel channel pointer */
3217 kchan
= trace_kernel_get_channel_by_name(channel_name
,
3218 session
->kernel_session
);
3219 if (kchan
== NULL
) {
3220 /* This sould not happen... */
3221 ret
= LTTCOMM_FATAL
;
3227 switch (event_type
) {
3228 case LTTNG_EVENT_SYSCALL
:
3229 ret
= event_kernel_enable_all_syscalls(session
->kernel_session
,
3230 kchan
, kernel_tracer_fd
);
3232 case LTTNG_EVENT_TRACEPOINT
:
3234 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
3235 * events already registered to the channel.
3237 ret
= event_kernel_enable_all_tracepoints(session
->kernel_session
,
3238 kchan
, kernel_tracer_fd
);
3240 case LTTNG_EVENT_ALL
:
3241 /* Enable syscalls and tracepoints */
3242 ret
= event_kernel_enable_all(session
->kernel_session
,
3243 kchan
, kernel_tracer_fd
);
3246 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
3250 /* Manage return value */
3251 if (ret
!= LTTCOMM_OK
) {
3255 kernel_wait_quiescent(kernel_tracer_fd
);
3257 case LTTNG_DOMAIN_UST
:
3259 struct lttng_channel
*attr
;
3260 struct ltt_ust_channel
*uchan
;
3261 struct ltt_ust_session
*usess
= session
->ust_session
;
3263 /* Get channel from global UST domain */
3264 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
3266 if (uchan
== NULL
) {
3267 /* Create default channel */
3268 attr
= channel_new_default_attr(domain
);
3270 ret
= LTTCOMM_FATAL
;
3273 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
3274 attr
->name
[NAME_MAX
- 1] = '\0';
3276 /* Use the internal command enable channel */
3277 ret
= channel_ust_create(usess
, domain
, attr
);
3278 if (ret
!= LTTCOMM_OK
) {
3284 /* Get the newly created channel reference back */
3285 uchan
= trace_ust_find_channel_by_name(
3286 usess
->domain_global
.channels
, channel_name
);
3287 if (uchan
== NULL
) {
3288 /* Something is really wrong */
3289 ret
= LTTCOMM_FATAL
;
3294 /* At this point, the session and channel exist on the tracer */
3296 switch (event_type
) {
3297 case LTTNG_EVENT_ALL
:
3298 case LTTNG_EVENT_TRACEPOINT
:
3299 ret
= event_ust_enable_all_tracepoints(usess
, domain
, uchan
);
3300 if (ret
!= LTTCOMM_OK
) {
3305 ret
= LTTCOMM_UST_ENABLE_FAIL
;
3309 /* Manage return value */
3310 if (ret
!= LTTCOMM_OK
) {
3317 case LTTNG_DOMAIN_UST_EXEC_NAME
:
3318 case LTTNG_DOMAIN_UST_PID
:
3319 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
3333 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
3335 static ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
3338 ssize_t nb_events
= 0;
3341 case LTTNG_DOMAIN_KERNEL
:
3342 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
3343 if (nb_events
< 0) {
3344 ret
= LTTCOMM_KERN_LIST_FAIL
;
3348 case LTTNG_DOMAIN_UST
:
3349 nb_events
= ust_app_list_events(events
);
3350 if (nb_events
< 0) {
3351 ret
= LTTCOMM_UST_LIST_FAIL
;
3363 /* Return negative value to differentiate return code */
3368 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
3370 static ssize_t
cmd_list_tracepoint_fields(int domain
,
3371 struct lttng_event_field
**fields
)
3374 ssize_t nb_fields
= 0;
3377 case LTTNG_DOMAIN_UST
:
3378 nb_fields
= ust_app_list_event_fields(fields
);
3379 if (nb_fields
< 0) {
3380 ret
= LTTCOMM_UST_LIST_FAIL
;
3384 case LTTNG_DOMAIN_KERNEL
:
3385 default: /* fall-through */
3393 /* Return negative value to differentiate return code */
3398 * Command LTTNG_START_TRACE processed by the client thread.
3400 static int cmd_start_trace(struct ltt_session
*session
)
3403 struct ltt_kernel_session
*ksession
;
3404 struct ltt_ust_session
*usess
;
3405 struct ltt_kernel_channel
*kchan
;
3407 /* Ease our life a bit ;) */
3408 ksession
= session
->kernel_session
;
3409 usess
= session
->ust_session
;
3411 if (session
->enabled
) {
3412 /* Already started. */
3413 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
3417 session
->enabled
= 1;
3419 ret
= setup_relayd(session
);
3420 if (ret
!= LTTCOMM_OK
) {
3421 ERR("Error setting up relayd for session %s", session
->name
);
3425 /* Kernel tracing */
3426 if (ksession
!= NULL
) {
3427 /* Open kernel metadata */
3428 if (ksession
->metadata
== NULL
) {
3429 ret
= kernel_open_metadata(ksession
);
3431 ret
= LTTCOMM_KERN_META_FAIL
;
3436 /* Open kernel metadata stream */
3437 if (ksession
->metadata_stream_fd
< 0) {
3438 ret
= kernel_open_metadata_stream(ksession
);
3440 ERR("Kernel create metadata stream failed");
3441 ret
= LTTCOMM_KERN_STREAM_FAIL
;
3446 /* For each channel */
3447 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
3448 if (kchan
->stream_count
== 0) {
3449 ret
= kernel_open_channel_stream(kchan
);
3451 ret
= LTTCOMM_KERN_STREAM_FAIL
;
3454 /* Update the stream global counter */
3455 ksession
->stream_count_global
+= ret
;
3459 /* Setup kernel consumer socket and send fds to it */
3460 ret
= init_kernel_tracing(ksession
);
3462 ret
= LTTCOMM_KERN_START_FAIL
;
3466 /* This start the kernel tracing */
3467 ret
= kernel_start_session(ksession
);
3469 ret
= LTTCOMM_KERN_START_FAIL
;
3473 /* Quiescent wait after starting trace */
3474 kernel_wait_quiescent(kernel_tracer_fd
);
3477 /* Flag session that trace should start automatically */
3479 usess
->start_trace
= 1;
3481 ret
= ust_app_start_trace_all(usess
);
3483 ret
= LTTCOMM_UST_START_FAIL
;
3495 * Command LTTNG_STOP_TRACE processed by the client thread.
3497 static int cmd_stop_trace(struct ltt_session
*session
)
3500 struct ltt_kernel_channel
*kchan
;
3501 struct ltt_kernel_session
*ksession
;
3502 struct ltt_ust_session
*usess
;
3505 ksession
= session
->kernel_session
;
3506 usess
= session
->ust_session
;
3508 if (!session
->enabled
) {
3509 ret
= LTTCOMM_TRACE_ALREADY_STOPPED
;
3513 session
->enabled
= 0;
3516 if (ksession
!= NULL
) {
3517 DBG("Stop kernel tracing");
3519 /* Flush metadata if exist */
3520 if (ksession
->metadata_stream_fd
>= 0) {
3521 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
3523 ERR("Kernel metadata flush failed");
3527 /* Flush all buffers before stopping */
3528 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
3529 ret
= kernel_flush_buffer(kchan
);
3531 ERR("Kernel flush buffer error");
3535 ret
= kernel_stop_session(ksession
);
3537 ret
= LTTCOMM_KERN_STOP_FAIL
;
3541 kernel_wait_quiescent(kernel_tracer_fd
);
3545 usess
->start_trace
= 0;
3547 ret
= ust_app_stop_trace_all(usess
);
3549 ret
= LTTCOMM_UST_STOP_FAIL
;
3561 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
3563 static int cmd_set_consumer_uri(int domain
, struct ltt_session
*session
,
3564 size_t nb_uri
, struct lttng_uri
*uris
)
3567 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3568 struct ltt_ust_session
*usess
= session
->ust_session
;
3569 struct consumer_output
*consumer
= NULL
;
3575 /* Can't enable consumer after session started. */
3576 if (session
->enabled
) {
3577 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
3581 if (!session
->start_consumer
) {
3582 ret
= LTTCOMM_NO_CONSUMER
;
3587 * This case switch makes sure the domain session has a temporary consumer
3588 * so the URL can be set.
3592 /* Code flow error. A session MUST always have a consumer object */
3593 assert(session
->consumer
);
3595 * The URL will be added to the tracing session consumer instead of a
3596 * specific domain consumer.
3598 consumer
= session
->consumer
;
3600 case LTTNG_DOMAIN_KERNEL
:
3601 /* Code flow error if we don't have a kernel session here. */
3604 /* Create consumer output if none exists */
3605 consumer
= ksess
->tmp_consumer
;
3606 if (consumer
== NULL
) {
3607 consumer
= consumer_copy_output(ksess
->consumer
);
3608 if (consumer
== NULL
) {
3609 ret
= LTTCOMM_FATAL
;
3612 /* Trash the consumer subdir, we are about to set a new one. */
3613 memset(consumer
->subdir
, 0, sizeof(consumer
->subdir
));
3614 ksess
->tmp_consumer
= consumer
;
3618 case LTTNG_DOMAIN_UST
:
3619 /* Code flow error if we don't have a kernel session here. */
3622 /* Create consumer output if none exists */
3623 consumer
= usess
->tmp_consumer
;
3624 if (consumer
== NULL
) {
3625 consumer
= consumer_copy_output(usess
->consumer
);
3626 if (consumer
== NULL
) {
3627 ret
= LTTCOMM_FATAL
;
3630 /* Trash the consumer subdir, we are about to set a new one. */
3631 memset(consumer
->subdir
, 0, sizeof(consumer
->subdir
));
3632 usess
->tmp_consumer
= consumer
;
3638 for (i
= 0; i
< nb_uri
; i
++) {
3639 struct consumer_socket
*socket
;
3640 struct lttng_ht_iter iter
;
3642 ret
= add_uri_to_consumer(consumer
, &uris
[i
], domain
, session
->name
);
3648 * Don't send relayd socket if URI is NOT remote or if the relayd
3649 * sockets for the session are already sent.
3651 if (uris
[i
].dtype
== LTTNG_DST_PATH
||
3652 consumer
->dst
.net
.relayd_socks_sent
) {
3656 /* Try to send relayd URI to the consumer if exist. */
3657 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
,
3658 socket
, node
.node
) {
3660 /* A socket in the HT should never have a negative fd */
3661 assert(socket
->fd
>= 0);
3663 pthread_mutex_lock(socket
->lock
);
3664 ret
= send_socket_relayd_consumer(domain
, session
, &uris
[i
],
3665 consumer
, socket
->fd
);
3666 pthread_mutex_unlock(socket
->lock
);
3667 if (ret
!= LTTCOMM_OK
) {
3682 * Command LTTNG_CREATE_SESSION processed by the client thread.
3684 static int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
3685 size_t nb_uri
, lttng_sock_cred
*creds
)
3689 struct ltt_session
*session
;
3690 //struct consumer_output *consumer = NULL;
3691 //struct lttng_uri *ctrl_uri, *data_uri = NULL;
3696 * Verify if the session already exist
3698 * XXX: There is no need for the session lock list here since the caller
3699 * (process_client_msg) is holding it. We might want to change that so a
3700 * single command does not lock the entire session list.
3702 session
= session_find_by_name(name
);
3703 if (session
!= NULL
) {
3704 ret
= LTTCOMM_EXIST_SESS
;
3708 /* Create tracing session in the registry */
3709 ret
= session_create(name
, path
, LTTNG_SOCK_GET_UID_CRED(creds
),
3710 LTTNG_SOCK_GET_GID_CRED(creds
));
3711 if (ret
!= LTTCOMM_OK
) {
3716 * Get the newly created session pointer back
3718 * XXX: There is no need for the session lock list here since the caller
3719 * (process_client_msg) is holding it. We might want to change that so a
3720 * single command does not lock the entire session list.
3722 session
= session_find_by_name(name
);
3725 /* Create default consumer output for the session not yet created. */
3726 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
3727 if (session
->consumer
== NULL
) {
3728 ret
= LTTCOMM_FATAL
;
3729 goto consumer_error
;
3733 * This means that the lttng_create_session call was called with the _path_
3734 * argument set to NULL.
3738 * At this point, we'll skip the consumer URI setup and create a
3739 * session with a NULL path which will flag the session to NOT spawn a
3742 DBG("Create session %s with NO uri, skipping consumer setup", name
);
3746 session
->start_consumer
= 1;
3748 ret
= cmd_set_consumer_uri(0, session
, nb_uri
, uris
);
3749 if (ret
!= LTTCOMM_OK
) {
3750 goto consumer_error
;
3753 session
->consumer
->enabled
= 1;
3759 session_destroy(session
);
3766 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3768 static int cmd_destroy_session(struct ltt_session
*session
, char *name
)
3775 /* Clean kernel session teardown */
3776 teardown_kernel_session(session
);
3777 /* UST session teardown */
3778 teardown_ust_session(session
);
3781 * Must notify the kernel thread here to update it's poll setin order
3782 * to remove the channel(s)' fd just destroyed.
3784 ret
= notify_thread_pipe(kernel_poll_pipe
[1]);
3786 PERROR("write kernel poll pipe");
3789 ret
= session_destroy(session
);
3795 * Command LTTNG_CALIBRATE processed by the client thread.
3797 static int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
3802 case LTTNG_DOMAIN_KERNEL
:
3804 struct lttng_kernel_calibrate kcalibrate
;
3806 kcalibrate
.type
= calibrate
->type
;
3807 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
3809 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
3814 case LTTNG_DOMAIN_UST
:
3816 struct lttng_ust_calibrate ucalibrate
;
3818 ucalibrate
.type
= calibrate
->type
;
3819 ret
= ust_app_calibrate_glb(&ucalibrate
);
3821 ret
= LTTCOMM_UST_CALIBRATE_FAIL
;
3838 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3840 static int cmd_register_consumer(struct ltt_session
*session
, int domain
,
3844 struct consumer_socket
*socket
;
3847 case LTTNG_DOMAIN_KERNEL
:
3848 /* Can't register a consumer if there is already one */
3849 if (session
->kernel_session
->consumer_fds_sent
!= 0) {
3850 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
3854 sock
= lttcomm_connect_unix_sock(sock_path
);
3856 ret
= LTTCOMM_CONNECT_FAIL
;
3860 socket
= consumer_allocate_socket(sock
);
3861 if (socket
== NULL
) {
3862 ret
= LTTCOMM_FATAL
;
3867 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3868 if (socket
->lock
== NULL
) {
3869 PERROR("zmalloc pthread mutex");
3870 ret
= LTTCOMM_FATAL
;
3873 pthread_mutex_init(socket
->lock
, NULL
);
3876 consumer_add_socket(socket
, session
->kernel_session
->consumer
);
3879 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3880 kconsumer_data
.pid
= -1;
3881 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3885 /* TODO: Userspace tracing */
3897 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3899 static ssize_t
cmd_list_domains(struct ltt_session
*session
,
3900 struct lttng_domain
**domains
)
3905 if (session
->kernel_session
!= NULL
) {
3906 DBG3("Listing domains found kernel domain");
3910 if (session
->ust_session
!= NULL
) {
3911 DBG3("Listing domains found UST global domain");
3915 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3916 if (*domains
== NULL
) {
3917 ret
= -LTTCOMM_FATAL
;
3921 if (session
->kernel_session
!= NULL
) {
3922 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3926 if (session
->ust_session
!= NULL
) {
3927 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3938 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3940 static ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
3941 struct lttng_channel
**channels
)
3944 ssize_t nb_chan
= 0;
3947 case LTTNG_DOMAIN_KERNEL
:
3948 if (session
->kernel_session
!= NULL
) {
3949 nb_chan
= session
->kernel_session
->channel_count
;
3951 DBG3("Number of kernel channels %zd", nb_chan
);
3953 case LTTNG_DOMAIN_UST
:
3954 if (session
->ust_session
!= NULL
) {
3955 nb_chan
= lttng_ht_get_count(
3956 session
->ust_session
->domain_global
.channels
);
3958 DBG3("Number of UST global channels %zd", nb_chan
);
3967 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
3968 if (*channels
== NULL
) {
3969 ret
= -LTTCOMM_FATAL
;
3973 list_lttng_channels(domain
, session
, *channels
);
3985 * Command LTTNG_LIST_EVENTS processed by the client thread.
3987 static ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
3988 char *channel_name
, struct lttng_event
**events
)
3991 ssize_t nb_event
= 0;
3994 case LTTNG_DOMAIN_KERNEL
:
3995 if (session
->kernel_session
!= NULL
) {
3996 nb_event
= list_lttng_kernel_events(channel_name
,
3997 session
->kernel_session
, events
);
4000 case LTTNG_DOMAIN_UST
:
4002 if (session
->ust_session
!= NULL
) {
4003 nb_event
= list_lttng_ust_global_events(channel_name
,
4004 &session
->ust_session
->domain_global
, events
);
4020 * Command LTTNG_DISABLE_CONSUMER processed by the client thread.
4022 static int cmd_disable_consumer(int domain
, struct ltt_session
*session
)
4025 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4026 struct ltt_ust_session
*usess
= session
->ust_session
;
4027 struct consumer_output
*consumer
;
4031 if (session
->enabled
) {
4032 /* Can't disable consumer on an already started session */
4033 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
4037 if (!session
->start_consumer
) {
4038 ret
= LTTCOMM_NO_CONSUMER
;
4044 DBG("Disable tracing session %s consumer", session
->name
);
4045 consumer
= session
->consumer
;
4047 case LTTNG_DOMAIN_KERNEL
:
4048 /* Code flow error if we don't have a kernel session here. */
4051 DBG("Disabling kernel consumer");
4052 consumer
= ksess
->consumer
;
4055 case LTTNG_DOMAIN_UST
:
4056 /* Code flow error if we don't have a UST session here. */
4059 DBG("Disabling UST consumer");
4060 consumer
= usess
->consumer
;
4064 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
4069 consumer
->enabled
= 0;
4070 /* Success at this point */
4073 ret
= LTTCOMM_NO_CONSUMER
;
4081 * Command LTTNG_ENABLE_CONSUMER processed by the client thread.
4083 static int cmd_enable_consumer(int domain
, struct ltt_session
*session
)
4086 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4087 struct ltt_ust_session
*usess
= session
->ust_session
;
4088 struct consumer_output
*consumer
= NULL
;
4092 /* Can't enable consumer after session started. */
4093 if (session
->enabled
) {
4094 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
4098 if (!session
->start_consumer
) {
4099 ret
= LTTCOMM_NO_CONSUMER
;
4105 assert(session
->consumer
);
4106 consumer
= session
->consumer
;
4108 case LTTNG_DOMAIN_KERNEL
:
4109 /* Code flow error if we don't have a kernel session here. */
4113 * Check if we have already sent fds to the consumer. In that case,
4114 * the enable-consumer command can't be used because a start trace
4115 * had previously occured.
4117 if (ksess
->consumer_fds_sent
) {
4118 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
4122 consumer
= ksess
->tmp_consumer
;
4123 if (consumer
== NULL
) {
4125 /* No temp. consumer output exists. Using the current one. */
4126 DBG3("No temporary consumer. Using default");
4127 consumer
= ksess
->consumer
;
4131 switch (consumer
->type
) {
4132 case CONSUMER_DST_LOCAL
:
4133 DBG2("Consumer output is local. Creating directory(ies)");
4135 /* Create directory(ies) */
4136 ret
= run_as_mkdir_recursive(consumer
->dst
.trace_path
,
4137 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
4139 if (ret
!= -EEXIST
) {
4140 ERR("Trace directory creation error");
4141 ret
= LTTCOMM_FATAL
;
4146 case CONSUMER_DST_NET
:
4147 DBG2("Consumer output is network. Validating URIs");
4148 /* Validate if we have both control and data path set. */
4149 if (!consumer
->dst
.net
.control_isset
) {
4150 ret
= LTTCOMM_URL_CTRL_MISS
;
4154 if (!consumer
->dst
.net
.data_isset
) {
4155 ret
= LTTCOMM_URL_DATA_MISS
;
4159 /* Check established network session state */
4160 if (session
->net_handle
== 0) {
4161 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
4162 ERR("Session network handle is not set on enable-consumer");
4169 /* Append default kernel trace dir to subdir */
4170 strncat(ksess
->consumer
->subdir
, DEFAULT_KERNEL_TRACE_DIR
,
4171 sizeof(ksess
->consumer
->subdir
));
4175 * This is race free for now since the session lock is acquired before
4176 * ending up in this function. No other threads can access this kernel
4177 * session without this lock hence freeing the consumer output object
4181 consumer_destroy_output(ksess
->consumer
);
4183 ksess
->consumer
= consumer
;
4184 ksess
->tmp_consumer
= NULL
;
4187 case LTTNG_DOMAIN_UST
:
4188 /* Code flow error if we don't have a UST session here. */
4192 * Check if we have already sent fds to the consumer. In that case,
4193 * the enable-consumer command can't be used because a start trace
4194 * had previously occured.
4196 if (usess
->start_trace
) {
4197 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
4201 consumer
= usess
->tmp_consumer
;
4202 if (consumer
== NULL
) {
4204 /* No temp. consumer output exists. Using the current one. */
4205 DBG3("No temporary consumer. Using default");
4206 consumer
= usess
->consumer
;
4210 switch (consumer
->type
) {
4211 case CONSUMER_DST_LOCAL
:
4212 DBG2("Consumer output is local. Creating directory(ies)");
4214 /* Create directory(ies) */
4215 ret
= run_as_mkdir_recursive(consumer
->dst
.trace_path
,
4216 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
4218 if (ret
!= -EEXIST
) {
4219 ERR("Trace directory creation error");
4220 ret
= LTTCOMM_FATAL
;
4225 case CONSUMER_DST_NET
:
4226 DBG2("Consumer output is network. Validating URIs");
4227 /* Validate if we have both control and data path set. */
4228 if (!consumer
->dst
.net
.control_isset
) {
4229 ret
= LTTCOMM_URL_CTRL_MISS
;
4233 if (!consumer
->dst
.net
.data_isset
) {
4234 ret
= LTTCOMM_URL_DATA_MISS
;
4238 /* Check established network session state */
4239 if (session
->net_handle
== 0) {
4240 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
4241 DBG2("Session network handle is not set on enable-consumer");
4245 if (consumer
->net_seq_index
== -1) {
4246 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
4247 DBG2("Network index is not set on the consumer");
4254 /* Append default kernel trace dir to subdir */
4255 strncat(usess
->consumer
->subdir
, DEFAULT_UST_TRACE_DIR
,
4256 sizeof(usess
->consumer
->subdir
));
4260 * This is race free for now since the session lock is acquired before
4261 * ending up in this function. No other threads can access this kernel
4262 * session without this lock hence freeing the consumer output object
4266 consumer_destroy_output(usess
->consumer
);
4268 usess
->consumer
= consumer
;
4269 usess
->tmp_consumer
= NULL
;
4276 consumer
->enabled
= 1;
4277 /* Success at this point */
4280 /* Should not really happend... */
4281 ret
= LTTCOMM_NO_CONSUMER
;
4289 * Process the command requested by the lttng client within the command
4290 * context structure. This function make sure that the return structure (llm)
4291 * is set and ready for transmission before returning.
4293 * Return any error encountered or 0 for success.
4295 * "sock" is only used for special-case var. len data.
4297 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
4300 int ret
= LTTCOMM_OK
;
4301 int need_tracing_session
= 1;
4304 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
4308 switch (cmd_ctx
->lsm
->cmd_type
) {
4309 case LTTNG_CREATE_SESSION
:
4310 case LTTNG_DESTROY_SESSION
:
4311 case LTTNG_LIST_SESSIONS
:
4312 case LTTNG_LIST_DOMAINS
:
4313 case LTTNG_START_TRACE
:
4314 case LTTNG_STOP_TRACE
:
4321 if (opt_no_kernel
&& need_domain
4322 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
4324 ret
= LTTCOMM_NEED_ROOT_SESSIOND
;
4326 ret
= LTTCOMM_KERN_NA
;
4331 /* Deny register consumer if we already have a spawned consumer. */
4332 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
4333 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
4334 if (kconsumer_data
.pid
> 0) {
4335 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
4338 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
4342 * Check for command that don't needs to allocate a returned payload. We do
4343 * this here so we don't have to make the call for no payload at each
4346 switch(cmd_ctx
->lsm
->cmd_type
) {
4347 case LTTNG_LIST_SESSIONS
:
4348 case LTTNG_LIST_TRACEPOINTS
:
4349 case LTTNG_LIST_TRACEPOINT_FIELDS
:
4350 case LTTNG_LIST_DOMAINS
:
4351 case LTTNG_LIST_CHANNELS
:
4352 case LTTNG_LIST_EVENTS
:
4355 /* Setup lttng message with no payload */
4356 ret
= setup_lttng_msg(cmd_ctx
, 0);
4358 /* This label does not try to unlock the session */
4359 goto init_setup_error
;
4363 /* Commands that DO NOT need a session. */
4364 switch (cmd_ctx
->lsm
->cmd_type
) {
4365 case LTTNG_CREATE_SESSION
:
4366 case LTTNG_CALIBRATE
:
4367 case LTTNG_LIST_SESSIONS
:
4368 case LTTNG_LIST_TRACEPOINTS
:
4369 case LTTNG_LIST_TRACEPOINT_FIELDS
:
4370 need_tracing_session
= 0;
4373 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
4375 * We keep the session list lock across _all_ commands
4376 * for now, because the per-session lock does not
4377 * handle teardown properly.
4379 session_lock_list();
4380 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
4381 if (cmd_ctx
->session
== NULL
) {
4382 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
4383 ret
= LTTCOMM_SESS_NOT_FOUND
;
4385 /* If no session name specified */
4386 ret
= LTTCOMM_SELECT_SESS
;
4390 /* Acquire lock for the session */
4391 session_lock(cmd_ctx
->session
);
4401 * Check domain type for specific "pre-action".
4403 switch (cmd_ctx
->lsm
->domain
.type
) {
4404 case LTTNG_DOMAIN_KERNEL
:
4406 ret
= LTTCOMM_NEED_ROOT_SESSIOND
;
4410 /* Kernel tracer check */
4411 if (kernel_tracer_fd
== -1) {
4412 /* Basically, load kernel tracer modules */
4413 ret
= init_kernel_tracer();
4419 /* Consumer is in an ERROR state. Report back to client */
4420 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
4421 ret
= LTTCOMM_NO_KERNCONSUMERD
;
4425 /* Need a session for kernel command */
4426 if (need_tracing_session
) {
4427 if (cmd_ctx
->session
->kernel_session
== NULL
) {
4428 ret
= create_kernel_session(cmd_ctx
->session
);
4430 ret
= LTTCOMM_KERN_SESS_FAIL
;
4435 /* Start the kernel consumer daemon */
4436 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
4437 if (kconsumer_data
.pid
== 0 &&
4438 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
&&
4439 cmd_ctx
->session
->start_consumer
) {
4440 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
4441 ret
= start_consumerd(&kconsumer_data
);
4443 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
4446 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
4448 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
4452 * The consumer was just spawned so we need to add the socket to
4453 * the consumer output of the session if exist.
4455 ret
= consumer_create_socket(&kconsumer_data
,
4456 cmd_ctx
->session
->kernel_session
->consumer
);
4463 case LTTNG_DOMAIN_UST
:
4465 /* Consumer is in an ERROR state. Report back to client */
4466 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
4467 ret
= LTTCOMM_NO_USTCONSUMERD
;
4471 if (need_tracing_session
) {
4472 /* Create UST session if none exist. */
4473 if (cmd_ctx
->session
->ust_session
== NULL
) {
4474 ret
= create_ust_session(cmd_ctx
->session
,
4475 &cmd_ctx
->lsm
->domain
);
4476 if (ret
!= LTTCOMM_OK
) {
4481 /* Start the UST consumer daemons */
4483 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
4484 if (consumerd64_bin
[0] != '\0' &&
4485 ustconsumer64_data
.pid
== 0 &&
4486 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
&&
4487 cmd_ctx
->session
->start_consumer
) {
4488 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
4489 ret
= start_consumerd(&ustconsumer64_data
);
4491 ret
= LTTCOMM_UST_CONSUMER64_FAIL
;
4492 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
4496 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
4497 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
4499 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
4503 * Setup socket for consumer 64 bit. No need for atomic access
4504 * since it was set above and can ONLY be set in this thread.
4506 ret
= consumer_create_socket(&ustconsumer64_data
,
4507 cmd_ctx
->session
->ust_session
->consumer
);
4513 if (consumerd32_bin
[0] != '\0' &&
4514 ustconsumer32_data
.pid
== 0 &&
4515 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
&&
4516 cmd_ctx
->session
->start_consumer
) {
4517 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
4518 ret
= start_consumerd(&ustconsumer32_data
);
4520 ret
= LTTCOMM_UST_CONSUMER32_FAIL
;
4521 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
4525 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
4526 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
4528 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
4532 * Setup socket for consumer 64 bit. No need for atomic access
4533 * since it was set above and can ONLY be set in this thread.
4535 ret
= consumer_create_socket(&ustconsumer32_data
,
4536 cmd_ctx
->session
->ust_session
->consumer
);
4548 /* Validate consumer daemon state when start/stop trace command */
4549 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
4550 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
4551 switch (cmd_ctx
->lsm
->domain
.type
) {
4552 case LTTNG_DOMAIN_UST
:
4553 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
4554 ret
= LTTCOMM_NO_USTCONSUMERD
;
4558 case LTTNG_DOMAIN_KERNEL
:
4559 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
4560 ret
= LTTCOMM_NO_KERNCONSUMERD
;
4568 * Check that the UID or GID match that of the tracing session.
4569 * The root user can interact with all sessions.
4571 if (need_tracing_session
) {
4572 if (!session_access_ok(cmd_ctx
->session
,
4573 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4574 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
4575 ret
= LTTCOMM_EPERM
;
4580 /* Process by command type */
4581 switch (cmd_ctx
->lsm
->cmd_type
) {
4582 case LTTNG_ADD_CONTEXT
:
4584 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4585 cmd_ctx
->lsm
->u
.context
.channel_name
,
4586 cmd_ctx
->lsm
->u
.context
.event_name
,
4587 &cmd_ctx
->lsm
->u
.context
.ctx
);
4590 case LTTNG_DISABLE_CHANNEL
:
4592 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4593 cmd_ctx
->lsm
->u
.disable
.channel_name
);
4596 case LTTNG_DISABLE_EVENT
:
4598 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4599 cmd_ctx
->lsm
->u
.disable
.channel_name
,
4600 cmd_ctx
->lsm
->u
.disable
.name
);
4603 case LTTNG_DISABLE_ALL_EVENT
:
4605 DBG("Disabling all events");
4607 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4608 cmd_ctx
->lsm
->u
.disable
.channel_name
);
4611 case LTTNG_DISABLE_CONSUMER
:
4613 ret
= cmd_disable_consumer(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
);
4616 case LTTNG_ENABLE_CHANNEL
:
4618 ret
= cmd_enable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4619 &cmd_ctx
->lsm
->u
.channel
.chan
);
4622 case LTTNG_ENABLE_CONSUMER
:
4625 * XXX: 0 means that this URI should be applied on the session. Should
4626 * be a DOMAIN enuam.
4628 ret
= cmd_enable_consumer(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
);
4629 if (ret
!= LTTCOMM_OK
) {
4633 if (cmd_ctx
->lsm
->domain
.type
== 0) {
4634 /* Add the URI for the UST session if a consumer is present. */
4635 if (cmd_ctx
->session
->ust_session
&&
4636 cmd_ctx
->session
->ust_session
->consumer
) {
4637 ret
= cmd_enable_consumer(LTTNG_DOMAIN_UST
, cmd_ctx
->session
);
4638 } else if (cmd_ctx
->session
->kernel_session
&&
4639 cmd_ctx
->session
->kernel_session
->consumer
) {
4640 ret
= cmd_enable_consumer(LTTNG_DOMAIN_KERNEL
,
4646 case LTTNG_ENABLE_EVENT
:
4648 ret
= cmd_enable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4649 cmd_ctx
->lsm
->u
.enable
.channel_name
,
4650 &cmd_ctx
->lsm
->u
.enable
.event
);
4653 case LTTNG_ENABLE_ALL_EVENT
:
4655 DBG("Enabling all events");
4657 ret
= cmd_enable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4658 cmd_ctx
->lsm
->u
.enable
.channel_name
,
4659 cmd_ctx
->lsm
->u
.enable
.event
.type
);
4662 case LTTNG_LIST_TRACEPOINTS
:
4664 struct lttng_event
*events
;
4667 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
4668 if (nb_events
< 0) {
4674 * Setup lttng message with payload size set to the event list size in
4675 * bytes and then copy list into the llm payload.
4677 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
4683 /* Copy event list into message payload */
4684 memcpy(cmd_ctx
->llm
->payload
, events
,
4685 sizeof(struct lttng_event
) * nb_events
);
4692 case LTTNG_LIST_TRACEPOINT_FIELDS
:
4694 struct lttng_event_field
*fields
;
4697 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
4699 if (nb_fields
< 0) {
4705 * Setup lttng message with payload size set to the event list size in
4706 * bytes and then copy list into the llm payload.
4708 ret
= setup_lttng_msg(cmd_ctx
,
4709 sizeof(struct lttng_event_field
) * nb_fields
);
4715 /* Copy event list into message payload */
4716 memcpy(cmd_ctx
->llm
->payload
, fields
,
4717 sizeof(struct lttng_event_field
) * nb_fields
);
4724 case LTTNG_SET_CONSUMER_URI
:
4727 struct lttng_uri
*uris
;
4729 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4730 len
= nb_uri
* sizeof(struct lttng_uri
);
4733 ret
= LTTCOMM_INVALID
;
4737 uris
= zmalloc(len
);
4739 ret
= LTTCOMM_FATAL
;
4743 /* Receive variable len data */
4744 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
4745 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4747 DBG("No URIs received from client... continuing");
4749 ret
= LTTCOMM_SESSION_FAIL
;
4753 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
4755 if (ret
!= LTTCOMM_OK
) {
4760 * XXX: 0 means that this URI should be applied on the session. Should
4761 * be a DOMAIN enuam.
4763 if (cmd_ctx
->lsm
->domain
.type
== 0) {
4764 /* Add the URI for the UST session if a consumer is present. */
4765 if (cmd_ctx
->session
->ust_session
&&
4766 cmd_ctx
->session
->ust_session
->consumer
) {
4767 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_UST
, cmd_ctx
->session
,
4769 } else if (cmd_ctx
->session
->kernel_session
&&
4770 cmd_ctx
->session
->kernel_session
->consumer
) {
4771 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL
,
4772 cmd_ctx
->session
, nb_uri
, uris
);
4778 case LTTNG_START_TRACE
:
4780 ret
= cmd_start_trace(cmd_ctx
->session
);
4783 case LTTNG_STOP_TRACE
:
4785 ret
= cmd_stop_trace(cmd_ctx
->session
);
4788 case LTTNG_CREATE_SESSION
:
4791 struct lttng_uri
*uris
= NULL
;
4793 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4794 len
= nb_uri
* sizeof(struct lttng_uri
);
4797 uris
= zmalloc(len
);
4799 ret
= LTTCOMM_FATAL
;
4803 /* Receive variable len data */
4804 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4805 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4807 DBG("No URIs received from client... continuing");
4809 ret
= LTTCOMM_SESSION_FAIL
;
4813 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4814 DBG("Creating session with ONE network URI is a bad call");
4815 ret
= LTTCOMM_SESSION_FAIL
;
4820 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
4825 case LTTNG_DESTROY_SESSION
:
4827 ret
= cmd_destroy_session(cmd_ctx
->session
,
4828 cmd_ctx
->lsm
->session
.name
);
4830 /* Set session to NULL so we do not unlock it after free. */
4831 cmd_ctx
->session
= NULL
;
4834 case LTTNG_LIST_DOMAINS
:
4837 struct lttng_domain
*domains
;
4839 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
4845 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
4850 /* Copy event list into message payload */
4851 memcpy(cmd_ctx
->llm
->payload
, domains
,
4852 nb_dom
* sizeof(struct lttng_domain
));
4859 case LTTNG_LIST_CHANNELS
:
4862 struct lttng_channel
*channels
;
4864 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
4865 cmd_ctx
->session
, &channels
);
4871 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
4876 /* Copy event list into message payload */
4877 memcpy(cmd_ctx
->llm
->payload
, channels
,
4878 nb_chan
* sizeof(struct lttng_channel
));
4885 case LTTNG_LIST_EVENTS
:
4888 struct lttng_event
*events
= NULL
;
4890 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
4891 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
4897 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
4902 /* Copy event list into message payload */
4903 memcpy(cmd_ctx
->llm
->payload
, events
,
4904 nb_event
* sizeof(struct lttng_event
));
4911 case LTTNG_LIST_SESSIONS
:
4913 unsigned int nr_sessions
;
4915 session_lock_list();
4916 nr_sessions
= lttng_sessions_count(
4917 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4918 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4920 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
4922 session_unlock_list();
4926 /* Filled the session array */
4927 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
4928 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4929 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4931 session_unlock_list();
4936 case LTTNG_CALIBRATE
:
4938 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
4939 &cmd_ctx
->lsm
->u
.calibrate
);
4942 case LTTNG_REGISTER_CONSUMER
:
4944 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4945 cmd_ctx
->lsm
->u
.reg
.path
);
4948 case LTTNG_SET_FILTER
:
4950 struct lttng_filter_bytecode
*bytecode
;
4952 if (cmd_ctx
->lsm
->u
.filter
.bytecode_len
> 65336) {
4953 ret
= LTTCOMM_FILTER_INVAL
;
4956 bytecode
= zmalloc(cmd_ctx
->lsm
->u
.filter
.bytecode_len
);
4958 ret
= LTTCOMM_FILTER_NOMEM
;
4961 /* Receive var. len. data */
4962 DBG("Receiving var len data from client ...");
4963 ret
= lttcomm_recv_unix_sock(sock
, bytecode
,
4964 cmd_ctx
->lsm
->u
.filter
.bytecode_len
);
4966 DBG("Nothing recv() from client var len data... continuing");
4968 ret
= LTTCOMM_FILTER_INVAL
;
4972 if (bytecode
->len
+ sizeof(*bytecode
)
4973 != cmd_ctx
->lsm
->u
.filter
.bytecode_len
) {
4975 ret
= LTTCOMM_FILTER_INVAL
;
4979 ret
= cmd_set_filter(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4980 cmd_ctx
->lsm
->u
.filter
.channel_name
,
4981 cmd_ctx
->lsm
->u
.filter
.event_name
,
4991 if (cmd_ctx
->llm
== NULL
) {
4992 DBG("Missing llm structure. Allocating one.");
4993 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
4997 /* Set return code */
4998 cmd_ctx
->llm
->ret_code
= ret
;
5000 if (cmd_ctx
->session
) {
5001 session_unlock(cmd_ctx
->session
);
5003 if (need_tracing_session
) {
5004 session_unlock_list();
5011 * Thread managing health check socket.
5013 static void *thread_manage_health(void *data
)
5015 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
5016 uint32_t revents
, nb_fd
;
5017 struct lttng_poll_event events
;
5018 struct lttcomm_health_msg msg
;
5019 struct lttcomm_health_data reply
;
5021 DBG("[thread] Manage health check started");
5023 rcu_register_thread();
5025 /* Create unix socket */
5026 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
5028 ERR("Unable to create health check Unix socket");
5033 ret
= lttcomm_listen_unix_sock(sock
);
5039 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
5040 * more will be added to this poll set.
5042 ret
= create_thread_poll_set(&events
, 2);
5047 /* Add the application registration socket */
5048 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
5054 DBG("Health check ready");
5056 nb_fd
= LTTNG_POLL_GETNB(&events
);
5058 /* Inifinite blocking call, waiting for transmission */
5060 ret
= lttng_poll_wait(&events
, -1);
5063 * Restart interrupted system call.
5065 if (errno
== EINTR
) {
5071 for (i
= 0; i
< nb_fd
; i
++) {
5072 /* Fetch once the poll data */
5073 revents
= LTTNG_POLL_GETEV(&events
, i
);
5074 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
5076 /* Thread quit pipe has been closed. Killing thread. */
5077 ret
= check_thread_quit_pipe(pollfd
, revents
);
5083 /* Event on the registration socket */
5084 if (pollfd
== sock
) {
5085 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
5086 ERR("Health socket poll error");
5092 new_sock
= lttcomm_accept_unix_sock(sock
);
5097 DBG("Receiving data from client for health...");
5098 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
5100 DBG("Nothing recv() from client... continuing");
5101 ret
= close(new_sock
);
5109 rcu_thread_online();
5111 switch (msg
.component
) {
5112 case LTTNG_HEALTH_CMD
:
5113 reply
.ret_code
= health_check_state(&health_thread_cmd
);
5115 case LTTNG_HEALTH_APP_MANAGE
:
5116 reply
.ret_code
= health_check_state(&health_thread_app_manage
);
5118 case LTTNG_HEALTH_APP_REG
:
5119 reply
.ret_code
= health_check_state(&health_thread_app_reg
);
5121 case LTTNG_HEALTH_KERNEL
:
5122 reply
.ret_code
= health_check_state(&health_thread_kernel
);
5124 case LTTNG_HEALTH_CONSUMER
:
5125 reply
.ret_code
= check_consumer_health();
5127 case LTTNG_HEALTH_ALL
:
5129 health_check_state(&health_thread_app_manage
) &&
5130 health_check_state(&health_thread_app_reg
) &&
5131 health_check_state(&health_thread_cmd
) &&
5132 health_check_state(&health_thread_kernel
) &&
5133 check_consumer_health();
5136 reply
.ret_code
= LTTCOMM_UND
;
5141 * Flip ret value since 0 is a success and 1 indicates a bad health for
5142 * the client where in the sessiond it is the opposite. Again, this is
5143 * just to make things easier for us poor developer which enjoy a lot
5146 if (reply
.ret_code
== 0 || reply
.ret_code
== 1) {
5147 reply
.ret_code
= !reply
.ret_code
;
5150 DBG2("Health check return value %d", reply
.ret_code
);
5152 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
5154 ERR("Failed to send health data back to client");
5157 /* End of transmission */
5158 ret
= close(new_sock
);
5168 ERR("Health error occurred in %s", __func__
);
5170 DBG("Health check thread dying");
5171 unlink(health_unix_sock_path
);
5178 if (new_sock
>= 0) {
5179 ret
= close(new_sock
);
5185 lttng_poll_clean(&events
);
5187 rcu_unregister_thread();
5192 * This thread manage all clients request using the unix client socket for
5195 static void *thread_manage_clients(void *data
)
5197 int sock
= -1, ret
, i
, pollfd
, err
= -1;
5199 uint32_t revents
, nb_fd
;
5200 struct command_ctx
*cmd_ctx
= NULL
;
5201 struct lttng_poll_event events
;
5203 DBG("[thread] Manage client started");
5205 rcu_register_thread();
5207 health_code_update(&health_thread_cmd
);
5209 ret
= lttcomm_listen_unix_sock(client_sock
);
5215 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
5216 * more will be added to this poll set.
5218 ret
= create_thread_poll_set(&events
, 2);
5223 /* Add the application registration socket */
5224 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
5230 * Notify parent pid that we are ready to accept command for client side.
5232 if (opt_sig_parent
) {
5233 kill(ppid
, SIGUSR1
);
5236 health_code_update(&health_thread_cmd
);
5239 DBG("Accepting client command ...");
5241 nb_fd
= LTTNG_POLL_GETNB(&events
);
5243 /* Inifinite blocking call, waiting for transmission */
5245 health_poll_update(&health_thread_cmd
);
5246 ret
= lttng_poll_wait(&events
, -1);
5247 health_poll_update(&health_thread_cmd
);
5250 * Restart interrupted system call.
5252 if (errno
== EINTR
) {
5258 for (i
= 0; i
< nb_fd
; i
++) {
5259 /* Fetch once the poll data */
5260 revents
= LTTNG_POLL_GETEV(&events
, i
);
5261 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
5263 health_code_update(&health_thread_cmd
);
5265 /* Thread quit pipe has been closed. Killing thread. */
5266 ret
= check_thread_quit_pipe(pollfd
, revents
);
5272 /* Event on the registration socket */
5273 if (pollfd
== client_sock
) {
5274 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
5275 ERR("Client socket poll error");
5281 DBG("Wait for client response");
5283 health_code_update(&health_thread_cmd
);
5285 sock
= lttcomm_accept_unix_sock(client_sock
);
5290 /* Set socket option for credentials retrieval */
5291 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
5296 /* Allocate context command to process the client request */
5297 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
5298 if (cmd_ctx
== NULL
) {
5299 PERROR("zmalloc cmd_ctx");
5303 /* Allocate data buffer for reception */
5304 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
5305 if (cmd_ctx
->lsm
== NULL
) {
5306 PERROR("zmalloc cmd_ctx->lsm");
5310 cmd_ctx
->llm
= NULL
;
5311 cmd_ctx
->session
= NULL
;
5313 health_code_update(&health_thread_cmd
);
5316 * Data is received from the lttng client. The struct
5317 * lttcomm_session_msg (lsm) contains the command and data request of
5320 DBG("Receiving data from client ...");
5321 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
5322 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
5324 DBG("Nothing recv() from client... continuing");
5330 clean_command_ctx(&cmd_ctx
);
5334 health_code_update(&health_thread_cmd
);
5336 // TODO: Validate cmd_ctx including sanity check for
5337 // security purpose.
5339 rcu_thread_online();
5341 * This function dispatch the work to the kernel or userspace tracer
5342 * libs and fill the lttcomm_lttng_msg data structure of all the needed
5343 * informations for the client. The command context struct contains
5344 * everything this function may needs.
5346 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
5347 rcu_thread_offline();
5357 * TODO: Inform client somehow of the fatal error. At
5358 * this point, ret < 0 means that a zmalloc failed
5359 * (ENOMEM). Error detected but still accept
5360 * command, unless a socket error has been
5363 clean_command_ctx(&cmd_ctx
);
5367 health_code_update(&health_thread_cmd
);
5369 DBG("Sending response (size: %d, retcode: %s)",
5370 cmd_ctx
->lttng_msg_size
,
5371 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
5372 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
5374 ERR("Failed to send data back to client");
5377 /* End of transmission */
5384 clean_command_ctx(&cmd_ctx
);
5386 health_code_update(&health_thread_cmd
);
5392 health_error(&health_thread_cmd
);
5393 ERR("Health error occurred in %s", __func__
);
5395 health_exit(&health_thread_cmd
);
5397 DBG("Client thread dying");
5398 unlink(client_unix_sock_path
);
5399 if (client_sock
>= 0) {
5400 ret
= close(client_sock
);
5412 lttng_poll_clean(&events
);
5413 clean_command_ctx(&cmd_ctx
);
5415 rcu_unregister_thread();
5421 * usage function on stderr
5423 static void usage(void)
5425 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
5426 fprintf(stderr
, " -h, --help Display this usage.\n");
5427 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
5428 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
5429 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
5430 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
5431 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
5432 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
5433 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
5434 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
5435 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
5436 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
5437 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
5438 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
5439 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
5440 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
5441 fprintf(stderr
, " -V, --version Show version number.\n");
5442 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
5443 fprintf(stderr
, " -q, --quiet No output at all.\n");
5444 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
5445 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
5446 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
5450 * daemon argument parsing
5452 static int parse_args(int argc
, char **argv
)
5456 static struct option long_options
[] = {
5457 { "client-sock", 1, 0, 'c' },
5458 { "apps-sock", 1, 0, 'a' },
5459 { "kconsumerd-cmd-sock", 1, 0, 'C' },
5460 { "kconsumerd-err-sock", 1, 0, 'E' },
5461 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
5462 { "ustconsumerd32-err-sock", 1, 0, 'H' },
5463 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
5464 { "ustconsumerd64-err-sock", 1, 0, 'F' },
5465 { "consumerd32-path", 1, 0, 'u' },
5466 { "consumerd32-libdir", 1, 0, 'U' },
5467 { "consumerd64-path", 1, 0, 't' },
5468 { "consumerd64-libdir", 1, 0, 'T' },
5469 { "daemonize", 0, 0, 'd' },
5470 { "sig-parent", 0, 0, 'S' },
5471 { "help", 0, 0, 'h' },
5472 { "group", 1, 0, 'g' },
5473 { "version", 0, 0, 'V' },
5474 { "quiet", 0, 0, 'q' },
5475 { "verbose", 0, 0, 'v' },
5476 { "verbose-consumer", 0, 0, 'Z' },
5477 { "no-kernel", 0, 0, 'N' },
5482 int option_index
= 0;
5483 c
= getopt_long(argc
, argv
, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t",
5484 long_options
, &option_index
);
5491 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
5493 fprintf(stderr
, " with arg %s\n", optarg
);
5497 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5500 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5506 opt_tracing_group
= optarg
;
5512 fprintf(stdout
, "%s\n", VERSION
);
5518 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5521 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5524 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5527 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5530 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5533 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
5539 lttng_opt_quiet
= 1;
5542 /* Verbose level can increase using multiple -v */
5543 lttng_opt_verbose
+= 1;
5546 opt_verbose_consumer
+= 1;
5549 consumerd32_bin
= optarg
;
5552 consumerd32_libdir
= optarg
;
5555 consumerd64_bin
= optarg
;
5558 consumerd64_libdir
= optarg
;
5561 /* Unknown option or other error.
5562 * Error is printed by getopt, just return */
5571 * Creates the two needed socket by the daemon.
5572 * apps_sock - The communication socket for all UST apps.
5573 * client_sock - The communication of the cli tool (lttng).
5575 static int init_daemon_socket(void)
5580 old_umask
= umask(0);
5582 /* Create client tool unix socket */
5583 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
5584 if (client_sock
< 0) {
5585 ERR("Create unix sock failed: %s", client_unix_sock_path
);
5590 /* File permission MUST be 660 */
5591 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5593 ERR("Set file permissions failed: %s", client_unix_sock_path
);
5598 /* Create the application unix socket */
5599 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
5600 if (apps_sock
< 0) {
5601 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
5606 /* File permission MUST be 666 */
5607 ret
= chmod(apps_unix_sock_path
,
5608 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5610 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
5621 * Check if the global socket is available, and if a daemon is answering at the
5622 * other side. If yes, error is returned.
5624 static int check_existing_daemon(void)
5626 /* Is there anybody out there ? */
5627 if (lttng_session_daemon_alive()) {
5635 * Set the tracing group gid onto the client socket.
5637 * Race window between mkdir and chown is OK because we are going from more
5638 * permissive (root.root) to less permissive (root.tracing).
5640 static int set_permissions(char *rundir
)
5645 ret
= allowed_group();
5647 WARN("No tracing group detected");
5654 /* Set lttng run dir */
5655 ret
= chown(rundir
, 0, gid
);
5657 ERR("Unable to set group on %s", rundir
);
5661 /* Ensure tracing group can search the run dir */
5662 ret
= chmod(rundir
, S_IRWXU
| S_IXGRP
| S_IXOTH
);
5664 ERR("Unable to set permissions on %s", rundir
);
5668 /* lttng client socket path */
5669 ret
= chown(client_unix_sock_path
, 0, gid
);
5671 ERR("Unable to set group on %s", client_unix_sock_path
);
5675 /* kconsumer error socket path */
5676 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, gid
);
5678 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5682 /* 64-bit ustconsumer error socket path */
5683 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, gid
);
5685 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5689 /* 32-bit ustconsumer compat32 error socket path */
5690 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, gid
);
5692 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5696 DBG("All permissions are set");
5703 * Create the lttng run directory needed for all global sockets and pipe.
5705 static int create_lttng_rundir(const char *rundir
)
5709 DBG3("Creating LTTng run directory: %s", rundir
);
5711 ret
= mkdir(rundir
, S_IRWXU
);
5713 if (errno
!= EEXIST
) {
5714 ERR("Unable to create %s", rundir
);
5726 * Setup sockets and directory needed by the kconsumerd communication with the
5729 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
5733 char path
[PATH_MAX
];
5735 switch (consumer_data
->type
) {
5736 case LTTNG_CONSUMER_KERNEL
:
5737 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
5739 case LTTNG_CONSUMER64_UST
:
5740 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
5742 case LTTNG_CONSUMER32_UST
:
5743 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
5746 ERR("Consumer type unknown");
5751 DBG2("Creating consumer directory: %s", path
);
5753 ret
= mkdir(path
, S_IRWXU
);
5755 if (errno
!= EEXIST
) {
5757 ERR("Failed to create %s", path
);
5763 /* Create the kconsumerd error unix socket */
5764 consumer_data
->err_sock
=
5765 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5766 if (consumer_data
->err_sock
< 0) {
5767 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5772 /* File permission MUST be 660 */
5773 ret
= chmod(consumer_data
->err_unix_sock_path
,
5774 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5776 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5786 * Signal handler for the daemon
5788 * Simply stop all worker threads, leaving main() return gracefully after
5789 * joining all threads and calling cleanup().
5791 static void sighandler(int sig
)
5795 DBG("SIGPIPE caught");
5798 DBG("SIGINT caught");
5802 DBG("SIGTERM caught");
5811 * Setup signal handler for :
5812 * SIGINT, SIGTERM, SIGPIPE
5814 static int set_signal_handler(void)
5817 struct sigaction sa
;
5820 if ((ret
= sigemptyset(&sigset
)) < 0) {
5821 PERROR("sigemptyset");
5825 sa
.sa_handler
= sighandler
;
5826 sa
.sa_mask
= sigset
;
5828 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5829 PERROR("sigaction");
5833 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5834 PERROR("sigaction");
5838 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5839 PERROR("sigaction");
5843 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
5849 * Set open files limit to unlimited. This daemon can open a large number of
5850 * file descriptors in order to consumer multiple kernel traces.
5852 static void set_ulimit(void)
5857 /* The kernel does not allowed an infinite limit for open files */
5858 lim
.rlim_cur
= 65535;
5859 lim
.rlim_max
= 65535;
5861 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5863 PERROR("failed to set open files limit");
5870 int main(int argc
, char **argv
)
5874 const char *home_path
;
5876 init_kernel_workarounds();
5878 rcu_register_thread();
5880 setup_consumerd_path();
5882 /* Parse arguments */
5884 if ((ret
= parse_args(argc
, argv
) < 0)) {
5894 * child: setsid, close FD 0, 1, 2, chdir /
5895 * parent: exit (if fork is successful)
5903 * We are in the child. Make sure all other file
5904 * descriptors are closed, in case we are called with
5905 * more opened file descriptors than the standard ones.
5907 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5912 /* Create thread quit pipe */
5913 if ((ret
= init_thread_quit_pipe()) < 0) {
5917 /* Check if daemon is UID = 0 */
5918 is_root
= !getuid();
5921 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
5923 /* Create global run dir with root access */
5924 ret
= create_lttng_rundir(rundir
);
5929 if (strlen(apps_unix_sock_path
) == 0) {
5930 snprintf(apps_unix_sock_path
, PATH_MAX
,
5931 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
5934 if (strlen(client_unix_sock_path
) == 0) {
5935 snprintf(client_unix_sock_path
, PATH_MAX
,
5936 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
5939 /* Set global SHM for ust */
5940 if (strlen(wait_shm_path
) == 0) {
5941 snprintf(wait_shm_path
, PATH_MAX
,
5942 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
5945 if (strlen(health_unix_sock_path
) == 0) {
5946 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
5947 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
5950 /* Setup kernel consumerd path */
5951 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
5952 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
5953 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
5954 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
5956 DBG2("Kernel consumer err path: %s",
5957 kconsumer_data
.err_unix_sock_path
);
5958 DBG2("Kernel consumer cmd path: %s",
5959 kconsumer_data
.cmd_unix_sock_path
);
5961 home_path
= get_home_dir();
5962 if (home_path
== NULL
) {
5963 /* TODO: Add --socket PATH option */
5964 ERR("Can't get HOME directory for sockets creation.");
5970 * Create rundir from home path. This will create something like
5973 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
5979 ret
= create_lttng_rundir(rundir
);
5984 if (strlen(apps_unix_sock_path
) == 0) {
5985 snprintf(apps_unix_sock_path
, PATH_MAX
,
5986 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
5989 /* Set the cli tool unix socket path */
5990 if (strlen(client_unix_sock_path
) == 0) {
5991 snprintf(client_unix_sock_path
, PATH_MAX
,
5992 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
5995 /* Set global SHM for ust */
5996 if (strlen(wait_shm_path
) == 0) {
5997 snprintf(wait_shm_path
, PATH_MAX
,
5998 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, geteuid());
6001 /* Set health check Unix path */
6002 if (strlen(health_unix_sock_path
) == 0) {
6003 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
6004 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
6008 /* Set consumer initial state */
6009 kernel_consumerd_state
= CONSUMER_STOPPED
;
6010 ust_consumerd_state
= CONSUMER_STOPPED
;
6012 DBG("Client socket path %s", client_unix_sock_path
);
6013 DBG("Application socket path %s", apps_unix_sock_path
);
6014 DBG("LTTng run directory path: %s", rundir
);
6016 /* 32 bits consumerd path setup */
6017 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
6018 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
6019 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
6020 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
6022 DBG2("UST consumer 32 bits err path: %s",
6023 ustconsumer32_data
.err_unix_sock_path
);
6024 DBG2("UST consumer 32 bits cmd path: %s",
6025 ustconsumer32_data
.cmd_unix_sock_path
);
6027 /* 64 bits consumerd path setup */
6028 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
6029 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
6030 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
6031 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
6033 DBG2("UST consumer 64 bits err path: %s",
6034 ustconsumer64_data
.err_unix_sock_path
);
6035 DBG2("UST consumer 64 bits cmd path: %s",
6036 ustconsumer64_data
.cmd_unix_sock_path
);
6039 * See if daemon already exist.
6041 if ((ret
= check_existing_daemon()) < 0) {
6042 ERR("Already running daemon.\n");
6044 * We do not goto exit because we must not cleanup()
6045 * because a daemon is already running.
6051 * Init UST app hash table. Alloc hash table before this point since
6052 * cleanup() can get called after that point.
6056 /* After this point, we can safely call cleanup() with "goto exit" */
6059 * These actions must be executed as root. We do that *after* setting up
6060 * the sockets path because we MUST make the check for another daemon using
6061 * those paths *before* trying to set the kernel consumer sockets and init
6065 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
6070 /* Setup kernel tracer */
6071 if (!opt_no_kernel
) {
6072 init_kernel_tracer();
6075 /* Set ulimit for open files */
6078 /* init lttng_fd tracking must be done after set_ulimit. */
6081 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
6086 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
6091 if ((ret
= set_signal_handler()) < 0) {
6095 /* Setup the needed unix socket */
6096 if ((ret
= init_daemon_socket()) < 0) {
6100 /* Set credentials to socket */
6101 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
6105 /* Get parent pid if -S, --sig-parent is specified. */
6106 if (opt_sig_parent
) {
6110 /* Setup the kernel pipe for waking up the kernel thread */
6111 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
6115 /* Setup the thread apps communication pipe. */
6116 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
6120 /* Init UST command queue. */
6121 cds_wfq_init(&ust_cmd_queue
.queue
);
6124 * Get session list pointer. This pointer MUST NOT be free(). This list is
6125 * statically declared in session.c
6127 session_list_ptr
= session_get_list();
6129 /* Set up max poll set size */
6130 lttng_poll_set_max_size();
6133 * Set network sequence index to 1 for streams to match a relayd socket on
6134 * the consumer side.
6136 uatomic_set(&relayd_net_seq_idx
, 1);
6138 /* Init all health thread counters. */
6139 health_init(&health_thread_cmd
);
6140 health_init(&health_thread_kernel
);
6141 health_init(&health_thread_app_manage
);
6142 health_init(&health_thread_app_reg
);
6145 * Init health counters of the consumer thread. We do a quick hack here to
6146 * the state of the consumer health is fine even if the thread is not
6147 * started. This is simply to ease our life and has no cost what so ever.
6149 health_init(&kconsumer_data
.health
);
6150 health_poll_update(&kconsumer_data
.health
);
6151 health_init(&ustconsumer32_data
.health
);
6152 health_poll_update(&ustconsumer32_data
.health
);
6153 health_init(&ustconsumer64_data
.health
);
6154 health_poll_update(&ustconsumer64_data
.health
);
6156 /* Create thread to manage the client socket */
6157 ret
= pthread_create(&health_thread
, NULL
,
6158 thread_manage_health
, (void *) NULL
);
6160 PERROR("pthread_create health");
6164 /* Create thread to manage the client socket */
6165 ret
= pthread_create(&client_thread
, NULL
,
6166 thread_manage_clients
, (void *) NULL
);
6168 PERROR("pthread_create clients");
6172 /* Create thread to dispatch registration */
6173 ret
= pthread_create(&dispatch_thread
, NULL
,
6174 thread_dispatch_ust_registration
, (void *) NULL
);
6176 PERROR("pthread_create dispatch");
6180 /* Create thread to manage application registration. */
6181 ret
= pthread_create(®_apps_thread
, NULL
,
6182 thread_registration_apps
, (void *) NULL
);
6184 PERROR("pthread_create registration");
6188 /* Create thread to manage application socket */
6189 ret
= pthread_create(&apps_thread
, NULL
,
6190 thread_manage_apps
, (void *) NULL
);
6192 PERROR("pthread_create apps");
6196 /* Create kernel thread to manage kernel event */
6197 ret
= pthread_create(&kernel_thread
, NULL
,
6198 thread_manage_kernel
, (void *) NULL
);
6200 PERROR("pthread_create kernel");
6204 ret
= pthread_join(kernel_thread
, &status
);
6206 PERROR("pthread_join");
6207 goto error
; /* join error, exit without cleanup */
6211 ret
= pthread_join(apps_thread
, &status
);
6213 PERROR("pthread_join");
6214 goto error
; /* join error, exit without cleanup */
6218 ret
= pthread_join(reg_apps_thread
, &status
);
6220 PERROR("pthread_join");
6221 goto error
; /* join error, exit without cleanup */
6225 ret
= pthread_join(dispatch_thread
, &status
);
6227 PERROR("pthread_join");
6228 goto error
; /* join error, exit without cleanup */
6232 ret
= pthread_join(client_thread
, &status
);
6234 PERROR("pthread_join");
6235 goto error
; /* join error, exit without cleanup */
6238 ret
= join_consumer_thread(&kconsumer_data
);
6240 PERROR("join_consumer");
6241 goto error
; /* join error, exit without cleanup */
6248 * cleanup() is called when no other thread is running.
6250 rcu_thread_online();
6252 rcu_thread_offline();
6253 rcu_unregister_thread();