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
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; only version 2
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <semaphore.h>
33 #include <sys/mount.h>
34 #include <sys/resource.h>
35 #include <sys/socket.h>
37 #include <sys/types.h>
39 #include <urcu/futex.h>
42 #include <ltt-kconsumerd.h>
43 #include <lttng-sessiond-comm.h>
44 #include <lttng/lttng-kconsumerd.h>
49 #include "kernel-ctl.h"
50 #include "ltt-sessiond.h"
52 #include "traceable-app.h"
57 #include "benchmark.h"
60 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
61 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
62 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
63 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
66 int opt_verbose
; /* Not static for lttngerr.h */
67 int opt_verbose_kconsumerd
; /* Not static for lttngerr.h */
68 int opt_quiet
; /* Not static for lttngerr.h */
71 const char *opt_tracing_group
;
72 static int opt_sig_parent
;
73 static int opt_daemon
;
74 static int is_root
; /* Set to 1 if the daemon is running as root */
75 static pid_t ppid
; /* Parent PID for --sig-parent option */
76 static pid_t kconsumerd_pid
;
77 static struct pollfd
*kernel_pollfd
;
78 static int dispatch_thread_exit
;
80 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
81 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
82 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
83 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
84 static char wait_shm_path
[PATH_MAX
]; /* global wait shm path for UST */
87 static int client_sock
;
89 static int kconsumerd_err_sock
;
90 static int kconsumerd_cmd_sock
;
91 static int kernel_tracer_fd
;
92 static int kernel_poll_pipe
[2];
95 * Quit pipe for all threads. This permits a single cancellation point
96 * for all threads when receiving an event on the pipe.
98 static int thread_quit_pipe
[2];
101 * This pipe is used to inform the thread managing application communication
102 * that a command is queued and ready to be processed.
104 static int apps_cmd_pipe
[2];
106 /* Pthread, Mutexes and Semaphores */
107 static pthread_t kconsumerd_thread
;
108 static pthread_t apps_thread
;
109 static pthread_t reg_apps_thread
;
110 static pthread_t client_thread
;
111 static pthread_t kernel_thread
;
112 static pthread_t dispatch_thread
;
113 static sem_t kconsumerd_sem
;
115 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
118 * UST registration command queue. This queue is tied with a futex and uses a N
119 * wakers / 1 waiter implemented and detailed in futex.c/.h
121 * The thread_manage_apps and thread_dispatch_ust_registration interact with
122 * this queue and the wait/wake scheme.
124 static struct ust_cmd_queue ust_cmd_queue
;
127 * Pointer initialized before thread creation.
129 * This points to the tracing session list containing the session count and a
130 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
131 * MUST NOT be taken if you call a public function in session.c.
133 * The lock is nested inside the structure: session_list_ptr->lock. Please use
134 * lock_session_list and unlock_session_list for lock acquisition.
136 static struct ltt_session_list
*session_list_ptr
;
139 * Remove modules in reverse load order.
141 static int modprobe_remove_kernel_modules(void)
146 for (i
= ARRAY_SIZE(kernel_modules_list
) - 1; i
>= 0; i
--) {
147 ret
= snprintf(modprobe
, sizeof(modprobe
),
148 "/sbin/modprobe --remove --quiet %s",
149 kernel_modules_list
[i
].name
);
151 perror("snprintf modprobe --remove");
154 modprobe
[sizeof(modprobe
) - 1] = '\0';
155 ret
= system(modprobe
);
157 ERR("Unable to launch modprobe --remove for module %s",
158 kernel_modules_list
[i
].name
);
159 } else if (kernel_modules_list
[i
].required
160 && WEXITSTATUS(ret
) != 0) {
161 ERR("Unable to remove module %s",
162 kernel_modules_list
[i
].name
);
164 DBG("Modprobe removal successful %s",
165 kernel_modules_list
[i
].name
);
174 * Return group ID of the tracing group or -1 if not found.
176 static gid_t
allowed_group(void)
180 if (opt_tracing_group
) {
181 grp
= getgrnam(opt_tracing_group
);
183 grp
= getgrnam(default_tracing_group
);
195 * Return -1 on error or 0 if all pipes are created.
197 static int init_thread_quit_pipe(void)
201 ret
= pipe2(thread_quit_pipe
, O_CLOEXEC
);
203 perror("thread quit pipe");
212 * Complete teardown of a kernel session. This free all data structure related
213 * to a kernel session and update counter.
215 static void teardown_kernel_session(struct ltt_session
*session
)
217 if (session
->kernel_session
!= NULL
) {
218 DBG("Tearing down kernel session");
221 * If a custom kernel consumer was registered, close the socket before
222 * tearing down the complete kernel session structure
224 if (session
->kernel_session
->consumer_fd
!= kconsumerd_cmd_sock
) {
225 lttcomm_close_unix_sock(session
->kernel_session
->consumer_fd
);
228 trace_kernel_destroy_session(session
->kernel_session
);
229 /* Extra precaution */
230 session
->kernel_session
= NULL
;
235 * Stop all threads by closing the thread quit pipe.
237 static void stop_threads(void)
239 /* Stopping all threads */
240 DBG("Terminating all threads");
241 close(thread_quit_pipe
[0]);
242 close(thread_quit_pipe
[1]);
243 /* Dispatch thread */
244 dispatch_thread_exit
= 1;
245 futex_nto1_wake(&ust_cmd_queue
.futex
);
251 static void cleanup(void)
255 struct ltt_session
*sess
, *stmp
;
260 MSG("%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
261 "Matthew, BEET driven development works!%c[%dm",
262 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
265 DBG("Removing %s directory", LTTNG_RUNDIR
);
266 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
268 ERR("asprintf failed. Something is really wrong!");
271 /* Remove lttng run directory */
274 ERR("Unable to clean " LTTNG_RUNDIR
);
277 DBG("Cleaning up all session");
279 /* Destroy session list mutex */
280 if (session_list_ptr
!= NULL
) {
281 pthread_mutex_destroy(&session_list_ptr
->lock
);
283 /* Cleanup ALL session */
284 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
285 teardown_kernel_session(sess
);
286 // TODO complete session cleanup (including UST)
290 DBG("Closing all UST sockets");
291 clean_traceable_apps_list();
293 pthread_mutex_destroy(&kconsumerd_pid_mutex
);
295 DBG("Closing kernel fd");
296 close(kernel_tracer_fd
);
299 DBG("Unloading kernel modules");
300 modprobe_remove_kernel_modules();
303 /* OUTPUT BENCHMARK RESULTS */
306 if (getenv("BENCH_UST_NOTIFY")) {
307 bench_print_ust_notification();
310 if (getenv("BENCH_UST_REGISTER")) {
311 bench_print_ust_register();
314 if (getenv("BENCH_BOOT_PROCESS")) {
315 bench_print_boot_process();
323 * Send data on a unix socket using the liblttsessiondcomm API.
325 * Return lttcomm error code.
327 static int send_unix_sock(int sock
, void *buf
, size_t len
)
329 /* Check valid length */
334 return lttcomm_send_unix_sock(sock
, buf
, len
);
338 * Free memory of a command context structure.
340 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
342 DBG("Clean command context structure");
344 if ((*cmd_ctx
)->llm
) {
345 free((*cmd_ctx
)->llm
);
347 if ((*cmd_ctx
)->lsm
) {
348 free((*cmd_ctx
)->lsm
);
356 * Send all stream fds of kernel channel to the consumer.
358 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
362 struct ltt_kernel_stream
*stream
;
363 struct lttcomm_kconsumerd_header lkh
;
364 struct lttcomm_kconsumerd_msg lkm
;
366 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
368 nb_fd
= channel
->stream_count
;
371 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
372 lkh
.cmd_type
= ADD_STREAM
;
374 DBG("Sending kconsumerd header");
376 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
378 perror("send kconsumerd header");
382 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
383 if (stream
->fd
!= 0) {
385 lkm
.state
= stream
->state
;
386 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
387 lkm
.output
= channel
->channel
->attr
.output
;
388 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
389 lkm
.path_name
[PATH_MAX
- 1] = '\0';
391 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
393 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
395 perror("send kconsumerd fd");
401 DBG("Kconsumerd channel fds sent");
410 * Send all stream fds of the kernel session to the consumer.
412 static int send_kconsumerd_fds(struct ltt_kernel_session
*session
)
415 struct ltt_kernel_channel
*chan
;
416 struct lttcomm_kconsumerd_header lkh
;
417 struct lttcomm_kconsumerd_msg lkm
;
420 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
421 lkh
.cmd_type
= ADD_STREAM
;
423 DBG("Sending kconsumerd header for metadata");
425 ret
= lttcomm_send_unix_sock(session
->consumer_fd
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
427 perror("send kconsumerd header");
431 DBG("Sending metadata stream fd");
433 /* Extra protection. It's NOT suppose to be set to 0 at this point */
434 if (session
->consumer_fd
== 0) {
435 session
->consumer_fd
= kconsumerd_cmd_sock
;
438 if (session
->metadata_stream_fd
!= 0) {
439 /* Send metadata stream fd first */
440 lkm
.fd
= session
->metadata_stream_fd
;
441 lkm
.state
= ACTIVE_FD
;
442 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
443 lkm
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
444 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
445 lkm
.path_name
[PATH_MAX
- 1] = '\0';
447 ret
= lttcomm_send_fds_unix_sock(session
->consumer_fd
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
449 perror("send kconsumerd fd");
454 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
455 ret
= send_kconsumerd_channel_fds(session
->consumer_fd
, chan
);
461 DBG("Kconsumerd fds (metadata and channel streams) sent");
470 * Notify UST applications using the shm mmap futex.
472 static int notify_ust_apps(int active
)
476 DBG("Notifying applications of session daemon state: %d", active
);
478 tracepoint(ust_notify_apps_start
);
480 /* See shm.c for this call implying mmap, shm and futex calls */
481 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
482 if (wait_shm_mmap
== NULL
) {
486 /* Wake waiting process */
487 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
489 tracepoint(ust_notify_apps_stop
);
491 /* Apps notified successfully */
499 * Setup the outgoing data buffer for the response (llm) by allocating the
500 * right amount of memory and copying the original information from the lsm
503 * Return total size of the buffer pointed by buf.
505 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
511 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
512 if (cmd_ctx
->llm
== NULL
) {
518 /* Copy common data */
519 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
520 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
522 cmd_ctx
->llm
->data_size
= size
;
523 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
532 * Update the kernel pollfd set of all channel fd available over all tracing
533 * session. Add the wakeup pipe at the end of the set.
535 static int update_kernel_pollfd(void)
539 * The wakup pipe and the quit pipe are needed so the number of fds starts
540 * at 2 for those pipes.
542 unsigned int nb_fd
= 2;
543 struct ltt_session
*session
;
544 struct ltt_kernel_channel
*channel
;
546 DBG("Updating kernel_pollfd");
548 /* Get the number of channel of all kernel session */
550 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
551 lock_session(session
);
552 if (session
->kernel_session
== NULL
) {
553 unlock_session(session
);
556 nb_fd
+= session
->kernel_session
->channel_count
;
557 unlock_session(session
);
560 DBG("Resizing kernel_pollfd to size %d", nb_fd
);
562 kernel_pollfd
= realloc(kernel_pollfd
, nb_fd
* sizeof(struct pollfd
));
563 if (kernel_pollfd
== NULL
) {
564 perror("malloc kernel_pollfd");
568 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
569 lock_session(session
);
570 if (session
->kernel_session
== NULL
) {
571 unlock_session(session
);
575 ERR("To much channel for kernel_pollfd size");
576 unlock_session(session
);
579 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
580 kernel_pollfd
[i
].fd
= channel
->fd
;
581 kernel_pollfd
[i
].events
= POLLIN
| POLLRDNORM
;
584 unlock_session(session
);
586 unlock_session_list();
588 /* Adding wake up pipe */
589 kernel_pollfd
[nb_fd
- 2].fd
= kernel_poll_pipe
[0];
590 kernel_pollfd
[nb_fd
- 2].events
= POLLIN
;
592 /* Adding the quit pipe */
593 kernel_pollfd
[nb_fd
- 1].fd
= thread_quit_pipe
[0];
598 unlock_session_list();
603 * Find the channel fd from 'fd' over all tracing session. When found, check
604 * for new channel stream and send those stream fds to the kernel consumer.
606 * Useful for CPU hotplug feature.
608 static int update_kernel_stream(int fd
)
611 struct ltt_session
*session
;
612 struct ltt_kernel_channel
*channel
;
614 DBG("Updating kernel streams for channel fd %d", fd
);
617 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
618 lock_session(session
);
619 if (session
->kernel_session
== NULL
) {
620 unlock_session(session
);
624 /* This is not suppose to be 0 but this is an extra security check */
625 if (session
->kernel_session
->consumer_fd
== 0) {
626 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
629 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
630 if (channel
->fd
== fd
) {
631 DBG("Channel found, updating kernel streams");
632 ret
= kernel_open_channel_stream(channel
);
638 * Have we already sent fds to the consumer? If yes, it means that
639 * tracing is started so it is safe to send our updated stream fds.
641 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
642 ret
= send_kconsumerd_channel_fds(session
->kernel_session
->consumer_fd
,
651 unlock_session(session
);
655 unlock_session_list();
657 unlock_session(session
);
663 * This thread manage event coming from the kernel.
665 * Features supported in this thread:
668 static void *thread_manage_kernel(void *data
)
670 int ret
, i
, nb_fd
= 0;
672 int update_poll_flag
= 1;
674 tracepoint(sessiond_th_kern_start
);
676 DBG("Thread manage kernel started");
679 if (update_poll_flag
== 1) {
680 nb_fd
= update_kernel_pollfd();
684 update_poll_flag
= 0;
687 DBG("Polling on %d fds", nb_fd
);
689 tracepoint(sessiond_th_kern_poll
);
691 /* Poll infinite value of time */
692 ret
= poll(kernel_pollfd
, nb_fd
, -1);
694 perror("poll kernel thread");
696 } else if (ret
== 0) {
697 /* Should not happen since timeout is infinite */
701 /* Thread quit pipe has been closed. Killing thread. */
702 if (kernel_pollfd
[nb_fd
- 1].revents
== POLLNVAL
) {
706 DBG("Kernel poll event triggered");
709 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
712 switch (kernel_pollfd
[nb_fd
- 2].revents
) {
714 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
715 update_poll_flag
= 1;
723 for (i
= 0; i
< nb_fd
; i
++) {
724 switch (kernel_pollfd
[i
].revents
) {
726 * New CPU detected by the kernel. Adding kernel stream to kernel
727 * session and updating the kernel consumer
729 case POLLIN
| POLLRDNORM
:
730 ret
= update_kernel_stream(kernel_pollfd
[i
].fd
);
740 DBG("Kernel thread dying");
745 close(kernel_poll_pipe
[0]);
746 close(kernel_poll_pipe
[1]);
751 * This thread manage the kconsumerd error sent back to the session daemon.
753 static void *thread_manage_kconsumerd(void *data
)
756 enum lttcomm_return_code code
;
757 struct pollfd pollfd
[2];
759 tracepoint(sessiond_th_kcon_start
);
761 DBG("[thread] Manage kconsumerd started");
763 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
768 /* First fd is always the quit pipe */
769 pollfd
[0].fd
= thread_quit_pipe
[0];
772 pollfd
[1].fd
= kconsumerd_err_sock
;
773 pollfd
[1].events
= POLLIN
;
775 tracepoint(sessiond_th_kcon_poll
);
777 /* Inifinite blocking call, waiting for transmission */
778 ret
= poll(pollfd
, 2, -1);
780 perror("poll kconsumerd thread");
784 /* Thread quit pipe has been closed. Killing thread. */
785 if (pollfd
[0].revents
== POLLNVAL
) {
787 } else if (pollfd
[1].revents
== POLLERR
) {
788 ERR("Kconsumerd err socket poll error");
792 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
797 /* Getting status code from kconsumerd */
798 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
803 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
804 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
805 if (kconsumerd_cmd_sock
< 0) {
806 sem_post(&kconsumerd_sem
);
807 perror("kconsumerd connect");
810 /* Signal condition to tell that the kconsumerd is ready */
811 sem_post(&kconsumerd_sem
);
812 DBG("Kconsumerd command socket ready");
814 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
815 lttcomm_get_readable_code(-code
));
819 /* Kconsumerd err socket */
821 pollfd
[1].events
= POLLIN
;
823 /* Inifinite blocking call, waiting for transmission */
824 ret
= poll(pollfd
, 2, -1);
826 perror("poll kconsumerd thread");
830 /* Thread quit pipe has been closed. Killing thread. */
831 if (pollfd
[0].revents
== POLLNVAL
) {
833 } else if (pollfd
[1].revents
== POLLERR
) {
834 ERR("Kconsumerd err socket second poll error");
838 /* Wait for any kconsumerd error */
839 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
841 ERR("Kconsumerd closed the command socket");
845 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
848 DBG("Kconsumerd thread dying");
849 if (kconsumerd_err_sock
) {
850 close(kconsumerd_err_sock
);
852 if (kconsumerd_cmd_sock
) {
853 close(kconsumerd_cmd_sock
);
859 unlink(kconsumerd_err_unix_sock_path
);
860 unlink(kconsumerd_cmd_unix_sock_path
);
867 * Reallocate the apps command pollfd structure of nb_fd size.
869 * The first two fds must be there at all time.
871 static int update_apps_cmd_pollfd(unsigned int nb_fd
, unsigned int old_nb_fd
,
872 struct pollfd
**pollfd
)
875 struct pollfd
*old_pollfd
= NULL
;
877 /* Can't accept pollfd less than 2 */
884 old_pollfd
= *pollfd
;
887 *pollfd
= malloc(nb_fd
* sizeof(struct pollfd
));
888 if (*pollfd
== NULL
) {
889 perror("malloc manage apps pollfd");
893 /* First fd is always the quit pipe */
894 (*pollfd
)[0].fd
= thread_quit_pipe
[0];
895 /* Apps command pipe */
896 (*pollfd
)[1].fd
= apps_cmd_pipe
[0];
897 (*pollfd
)[1].events
= POLLIN
;
899 /* Start count after the two pipes below */
901 for (i
= 2; i
< old_nb_fd
; i
++) {
902 /* Add to new pollfd */
903 if (old_pollfd
[i
].fd
!= -1) {
904 (*pollfd
)[count
].fd
= old_pollfd
[i
].fd
;
905 (*pollfd
)[count
].events
= POLLHUP
| POLLNVAL
| POLLERR
;
910 ERR("Updating poll fd wrong size");
915 /* Destroy old pollfd */
918 DBG("Apps cmd pollfd realloc of size %d", nb_fd
);
924 /* Destroy old pollfd */
930 * This thread manage application communication.
932 static void *thread_manage_apps(void *data
)
934 int i
, ret
, current_nb_fd
;
935 unsigned int nb_fd
= 2;
936 int update_poll_flag
= 1;
937 struct pollfd
*pollfd
= NULL
;
938 struct ust_command ust_cmd
;
940 tracepoint(sessiond_th_apps_start
);
942 DBG("[thread] Manage application started");
945 current_nb_fd
= nb_fd
;
948 /* See if we have a valid socket to add to pollfd */
949 if (ust_cmd
.sock
!= -1) {
951 update_poll_flag
= 1;
954 /* The pollfd struct must be updated */
955 if (update_poll_flag
) {
956 ret
= update_apps_cmd_pollfd(nb_fd
, current_nb_fd
, &pollfd
);
958 /* malloc failed so we quit */
962 if (ust_cmd
.sock
!= -1) {
963 /* Update pollfd with the new UST socket */
964 DBG("Adding sock %d to apps cmd pollfd", ust_cmd
.sock
);
965 pollfd
[nb_fd
- 1].fd
= ust_cmd
.sock
;
966 pollfd
[nb_fd
- 1].events
= POLLHUP
| POLLNVAL
| POLLERR
;
971 DBG("Apps thread polling on %d fds", nb_fd
);
973 tracepoint(sessiond_th_apps_poll
);
975 /* Inifinite blocking call, waiting for transmission */
976 ret
= poll(pollfd
, nb_fd
, -1);
978 perror("poll apps thread");
982 /* Thread quit pipe has been closed. Killing thread. */
983 if (pollfd
[0].revents
== POLLNVAL
) {
986 /* apps_cmd_pipe pipe events */
987 switch (pollfd
[1].revents
) {
989 ERR("Apps command pipe poll error");
992 tracepoint(ust_register_read_start
);
995 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
996 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
997 perror("read apps cmd pipe");
1001 tracepoint(ust_register_read_stop
);
1003 tracepoint(ust_register_add_start
);
1004 /* Register applicaton to the session daemon */
1005 ret
= register_traceable_app(&ust_cmd
.reg_msg
, ust_cmd
.sock
);
1007 /* Only critical ENOMEM error can be returned here */
1010 tracepoint(ust_register_add_stop
);
1012 tracepoint(ust_register_done_start
);
1013 ret
= ustctl_register_done(ust_cmd
.sock
);
1016 * If the registration is not possible, we simply unregister
1017 * the apps and continue
1019 unregister_traceable_app(ust_cmd
.sock
);
1022 tracepoint(ust_register_done_stop
);
1027 current_nb_fd
= nb_fd
;
1028 for (i
= 2; i
< current_nb_fd
; i
++) {
1029 /* Apps socket is closed/hungup */
1030 switch (pollfd
[i
].revents
) {
1035 unregister_traceable_app(pollfd
[i
].fd
);
1036 /* Indicate to remove this fd from the pollfd */
1043 if (nb_fd
!= current_nb_fd
) {
1044 update_poll_flag
= 1;
1049 DBG("Application communication apps dying");
1050 close(apps_cmd_pipe
[0]);
1051 close(apps_cmd_pipe
[1]);
1059 * Dispatch request from the registration threads to the application
1060 * communication thread.
1062 static void *thread_dispatch_ust_registration(void *data
)
1065 struct cds_wfq_node
*node
;
1066 struct ust_command
*ust_cmd
= NULL
;
1068 tracepoint(sessiond_th_dispatch_start
);
1070 DBG("[thread] Dispatch UST command started");
1072 while (!dispatch_thread_exit
) {
1073 /* Atomically prepare the queue futex */
1074 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1077 tracepoint(sessiond_th_dispatch_block
);
1079 /* Dequeue command for registration */
1080 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1082 DBG("Waked up but nothing in the UST command queue");
1083 /* Continue thread execution */
1087 tracepoint(ust_dispatch_register_start
);
1089 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1091 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1092 " gid:%d sock:%d name:%s (version %d.%d)",
1093 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1094 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1095 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1096 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1098 * Inform apps thread of the new application registration. This
1099 * call is blocking so we can be assured that the data will be read
1100 * at some point in time or wait to the end of the world :)
1102 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1103 sizeof(struct ust_command
));
1105 perror("write apps cmd pipe");
1106 if (errno
== EBADF
) {
1108 * We can't inform the application thread to process
1109 * registration. We will exit or else application
1110 * registration will not occur and tracing will never
1117 } while (node
!= NULL
);
1119 tracepoint(ust_dispatch_register_stop
);
1121 /* Futex wait on queue. Blocking call on futex() */
1122 futex_nto1_wait(&ust_cmd_queue
.futex
);
1126 DBG("Dispatch thread dying");
1131 * This thread manage application registration.
1133 static void *thread_registration_apps(void *data
)
1136 struct pollfd pollfd
[2];
1138 * Get allocated in this thread, enqueued to a global queue, dequeued and
1139 * freed in the manage apps thread.
1141 struct ust_command
*ust_cmd
= NULL
;
1143 tracepoint(sessiond_th_reg_start
);
1145 DBG("[thread] Manage application registration started");
1147 ret
= lttcomm_listen_unix_sock(apps_sock
);
1152 /* First fd is always the quit pipe */
1153 pollfd
[0].fd
= thread_quit_pipe
[0];
1156 pollfd
[1].fd
= apps_sock
;
1157 pollfd
[1].events
= POLLIN
;
1159 /* Notify all applications to register */
1160 ret
= notify_ust_apps(1);
1162 ERR("Failed to notify applications or create the wait shared memory.\n"
1163 "Execution continues but there might be problem for already running\n"
1164 "applications that wishes to register.");
1168 DBG("Accepting application registration");
1170 tracepoint(sessiond_th_reg_poll
);
1172 /* Inifinite blocking call, waiting for transmission */
1173 ret
= poll(pollfd
, 2, -1);
1175 perror("poll register apps thread");
1179 /* Thread quit pipe has been closed. Killing thread. */
1180 if (pollfd
[0].revents
== POLLNVAL
) {
1182 } else if (pollfd
[1].revents
== POLLERR
) {
1183 ERR("Register apps socket poll error");
1187 /* Registration starts here. Recording cycles */
1188 tracepoint(ust_register_start
);
1190 sock
= lttcomm_accept_unix_sock(apps_sock
);
1195 /* Create UST registration command for enqueuing */
1196 ust_cmd
= malloc(sizeof(struct ust_command
));
1197 if (ust_cmd
== NULL
) {
1198 perror("ust command malloc");
1203 * Using message-based transmissions to ensure we don't have to deal
1204 * with partially received messages.
1206 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1207 sizeof(struct ust_register_msg
));
1208 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1210 perror("lttcomm_recv_unix_sock register apps");
1212 ERR("Wrong size received on apps register");
1219 ust_cmd
->sock
= sock
;
1221 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1222 " gid:%d sock:%d name:%s (version %d.%d)",
1223 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1224 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1225 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1226 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1228 * Lock free enqueue the registration request.
1229 * The red pill has been taken! This apps will be part of the *system*
1231 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1234 * Wake the registration queue futex.
1235 * Implicit memory barrier with the exchange in cds_wfq_enqueue.
1237 futex_nto1_wake(&ust_cmd_queue
.futex
);
1239 tracepoint(ust_register_stop
);
1243 DBG("UST Registration thread dying");
1245 /* Notify that the registration thread is gone */
1251 unlink(apps_unix_sock_path
);
1257 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
1258 * exec or it will fails.
1260 static int spawn_kconsumerd_thread(void)
1264 /* Setup semaphore */
1265 sem_init(&kconsumerd_sem
, 0, 0);
1267 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
1269 perror("pthread_create kconsumerd");
1273 /* Wait for the kconsumerd thread to be ready */
1274 sem_wait(&kconsumerd_sem
);
1276 if (kconsumerd_pid
== 0) {
1277 ERR("Kconsumerd did not start");
1284 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1289 * Join kernel consumer thread
1291 static int join_kconsumerd_thread(void)
1296 if (kconsumerd_pid
!= 0) {
1297 ret
= kill(kconsumerd_pid
, SIGTERM
);
1299 ERR("Error killing kconsumerd");
1302 return pthread_join(kconsumerd_thread
, &status
);
1309 * Fork and exec a kernel consumer daemon (kconsumerd).
1311 * Return pid if successful else -1.
1313 static pid_t
spawn_kconsumerd(void)
1317 const char *verbosity
;
1319 DBG("Spawning kconsumerd");
1326 if (opt_verbose
> 1 || opt_verbose_kconsumerd
) {
1327 verbosity
= "--verbose";
1329 verbosity
= "--quiet";
1331 execl(INSTALL_BIN_PATH
"/ltt-kconsumerd", "ltt-kconsumerd", verbosity
, NULL
);
1333 perror("kernel start consumer exec");
1336 } else if (pid
> 0) {
1340 perror("kernel start consumer fork");
1350 * Spawn the kconsumerd daemon and session daemon thread.
1352 static int start_kconsumerd(void)
1356 pthread_mutex_lock(&kconsumerd_pid_mutex
);
1357 if (kconsumerd_pid
!= 0) {
1358 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1362 ret
= spawn_kconsumerd();
1364 ERR("Spawning kconsumerd failed");
1365 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1366 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1370 /* Setting up the global kconsumerd_pid */
1371 kconsumerd_pid
= ret
;
1372 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1374 DBG("Kconsumerd pid %d", ret
);
1376 DBG("Spawning kconsumerd thread");
1377 ret
= spawn_kconsumerd_thread();
1379 ERR("Fatal error spawning kconsumerd thread");
1391 * modprobe_kernel_modules
1393 static int modprobe_kernel_modules(void)
1398 for (i
= 0; i
< ARRAY_SIZE(kernel_modules_list
); i
++) {
1399 ret
= snprintf(modprobe
, sizeof(modprobe
),
1400 "/sbin/modprobe %s%s",
1401 kernel_modules_list
[i
].required
? "" : "--quiet ",
1402 kernel_modules_list
[i
].name
);
1404 perror("snprintf modprobe");
1407 modprobe
[sizeof(modprobe
) - 1] = '\0';
1408 ret
= system(modprobe
);
1410 ERR("Unable to launch modprobe for module %s",
1411 kernel_modules_list
[i
].name
);
1412 } else if (kernel_modules_list
[i
].required
1413 && WEXITSTATUS(ret
) != 0) {
1414 ERR("Unable to load module %s",
1415 kernel_modules_list
[i
].name
);
1417 DBG("Modprobe successfully %s",
1418 kernel_modules_list
[i
].name
);
1429 static int mount_debugfs(char *path
)
1432 char *type
= "debugfs";
1434 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, geteuid(), getegid());
1439 ret
= mount(type
, path
, type
, 0, NULL
);
1441 perror("mount debugfs");
1445 DBG("Mounted debugfs successfully at %s", path
);
1452 * Setup necessary data for kernel tracer action.
1454 static void init_kernel_tracer(void)
1457 char *proc_mounts
= "/proc/mounts";
1459 char *debugfs_path
= NULL
, *lttng_path
;
1462 /* Detect debugfs */
1463 fp
= fopen(proc_mounts
, "r");
1465 ERR("Unable to probe %s", proc_mounts
);
1469 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1470 if (strstr(line
, "debugfs") != NULL
) {
1471 /* Remove first string */
1473 /* Dup string here so we can reuse line later on */
1474 debugfs_path
= strdup(strtok(NULL
, " "));
1475 DBG("Got debugfs path : %s", debugfs_path
);
1482 /* Mount debugfs if needded */
1483 if (debugfs_path
== NULL
) {
1484 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1486 perror("asprintf debugfs path");
1489 ret
= mount_debugfs(debugfs_path
);
1495 /* Modprobe lttng kernel modules */
1496 ret
= modprobe_kernel_modules();
1501 /* Setup lttng kernel path */
1502 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1504 perror("asprintf lttng path");
1508 /* Open debugfs lttng */
1509 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1510 if (kernel_tracer_fd
< 0) {
1511 DBG("Failed to open %s", lttng_path
);
1517 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1527 WARN("No kernel tracer available");
1528 kernel_tracer_fd
= 0;
1533 * Start tracing by creating trace directory and sending FDs to the kernel
1536 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1540 if (session
->kconsumer_fds_sent
== 0) {
1542 * Assign default kernel consumer if no consumer assigned to the kernel
1543 * session. At this point, it's NOT suppose to be 0 but this is an extra
1546 if (session
->consumer_fd
== 0) {
1547 session
->consumer_fd
= kconsumerd_cmd_sock
;
1550 ret
= send_kconsumerd_fds(session
);
1552 ERR("Send kconsumerd fds failed");
1553 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1557 session
->kconsumer_fds_sent
= 1;
1565 * Notify kernel thread to update it's pollfd.
1567 static int notify_kernel_pollfd(void)
1571 /* Inform kernel thread of the new kernel channel */
1572 ret
= write(kernel_poll_pipe
[1], "!", 1);
1574 perror("write kernel poll pipe");
1581 * Allocate a channel structure and fill it.
1583 static struct lttng_channel
*init_default_channel(enum lttng_domain_type domain_type
,
1586 struct lttng_channel
*chan
;
1588 chan
= malloc(sizeof(struct lttng_channel
));
1590 perror("init channel malloc");
1594 if (snprintf(chan
->name
, NAME_MAX
, "%s", name
) < 0) {
1595 perror("snprintf channel name");
1599 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1600 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1601 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1603 switch (domain_type
) {
1604 case LTTNG_DOMAIN_KERNEL
:
1605 chan
->attr
.subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
1606 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1607 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1611 goto error
; /* Not implemented */
1622 * Create an UST session and add it to the session ust list.
1624 static int create_ust_session(pid_t pid
, struct ltt_session
*session
)
1627 struct ltt_ust_session
*lus
;
1629 DBG("Creating UST session");
1631 lus
= trace_ust_create_session(session
->path
, pid
);
1636 ret
= mkdir_recursive(lus
->path
, S_IRWXU
| S_IRWXG
,
1637 geteuid(), allowed_group());
1639 if (ret
!= -EEXIST
) {
1640 ERR("Trace directory creation error");
1645 /* Create session on the UST tracer */
1646 ret
= ustctl_create_session(lus
);
1659 * Create a kernel tracer session then create the default channel.
1661 static int create_kernel_session(struct ltt_session
*session
)
1665 DBG("Creating kernel session");
1667 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1669 ret
= LTTCOMM_KERN_SESS_FAIL
;
1673 /* Set kernel consumer socket fd */
1674 if (kconsumerd_cmd_sock
) {
1675 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
1678 ret
= mkdir_recursive(session
->kernel_session
->trace_path
,
1679 S_IRWXU
| S_IRWXG
, geteuid(), allowed_group());
1681 if (ret
!= -EEXIST
) {
1682 ERR("Trace directory creation error");
1692 * Using the session list, filled a lttng_session array to send back to the
1693 * client for session listing.
1695 * The session list lock MUST be acquired before calling this function. Use
1696 * lock_session_list() and unlock_session_list().
1698 static void list_lttng_sessions(struct lttng_session
*sessions
)
1701 struct ltt_session
*session
;
1703 DBG("Getting all available session");
1705 * Iterate over session list and append data after the control struct in
1708 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1709 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1710 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
1711 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1712 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
1718 * Fill lttng_channel array of all channels.
1720 static void list_lttng_channels(struct ltt_session
*session
,
1721 struct lttng_channel
*channels
)
1724 struct ltt_kernel_channel
*kchan
;
1726 DBG("Listing channels for session %s", session
->name
);
1728 /* Kernel channels */
1729 if (session
->kernel_session
!= NULL
) {
1730 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
1731 /* Copy lttng_channel struct to array */
1732 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1733 channels
[i
].enabled
= kchan
->enabled
;
1738 /* TODO: Missing UST listing */
1742 * Fill lttng_event array of all events in the channel.
1744 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1745 struct lttng_event
*events
)
1748 * TODO: This is ONLY kernel. Need UST support.
1751 struct ltt_kernel_event
*event
;
1753 DBG("Listing events for channel %s", kchan
->channel
->name
);
1755 /* Kernel channels */
1756 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1757 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1758 events
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1759 events
[i
].enabled
= event
->enabled
;
1760 switch (event
->event
->instrumentation
) {
1761 case LTTNG_KERNEL_TRACEPOINT
:
1762 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1764 case LTTNG_KERNEL_KPROBE
:
1765 case LTTNG_KERNEL_KRETPROBE
:
1766 events
[i
].type
= LTTNG_EVENT_PROBE
;
1767 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
1768 sizeof(struct lttng_kernel_kprobe
));
1770 case LTTNG_KERNEL_FUNCTION
:
1771 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
1772 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
1773 sizeof(struct lttng_kernel_function
));
1781 * Process the command requested by the lttng client within the command
1782 * context structure. This function make sure that the return structure (llm)
1783 * is set and ready for transmission before returning.
1785 * Return any error encountered or 0 for success.
1787 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1789 int ret
= LTTCOMM_OK
;
1791 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1794 * Commands that DO NOT need a session.
1796 switch (cmd_ctx
->lsm
->cmd_type
) {
1797 case LTTNG_CREATE_SESSION
:
1798 case LTTNG_LIST_SESSIONS
:
1799 case LTTNG_LIST_TRACEPOINTS
:
1800 case LTTNG_CALIBRATE
:
1803 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
1804 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session
.name
);
1805 if (cmd_ctx
->session
== NULL
) {
1806 /* If session name not found */
1807 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
1808 ret
= LTTCOMM_SESS_NOT_FOUND
;
1809 } else { /* If no session name specified */
1810 ret
= LTTCOMM_SELECT_SESS
;
1814 /* Acquire lock for the session */
1815 lock_session(cmd_ctx
->session
);
1821 * Check domain type for specific "pre-action".
1823 switch (cmd_ctx
->lsm
->domain
.type
) {
1824 case LTTNG_DOMAIN_KERNEL
:
1825 /* Kernel tracer check */
1826 if (kernel_tracer_fd
== 0) {
1827 init_kernel_tracer();
1828 if (kernel_tracer_fd
== 0) {
1829 ret
= LTTCOMM_KERN_NA
;
1833 /* Need a session for kernel command */
1834 switch (cmd_ctx
->lsm
->cmd_type
) {
1835 case LTTNG_CALIBRATE
:
1836 case LTTNG_CREATE_SESSION
:
1837 case LTTNG_LIST_SESSIONS
:
1838 case LTTNG_LIST_TRACEPOINTS
:
1841 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1842 ret
= create_kernel_session(cmd_ctx
->session
);
1844 ret
= LTTCOMM_KERN_SESS_FAIL
;
1847 /* Start the kernel consumer daemon */
1848 if (kconsumerd_pid
== 0 &&
1849 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1850 ret
= start_kconsumerd();
1858 case LTTNG_DOMAIN_UST_PID
:
1864 /* Process by command type */
1865 switch (cmd_ctx
->lsm
->cmd_type
) {
1866 case LTTNG_ADD_CONTEXT
:
1868 struct lttng_kernel_context kctx
;
1870 /* Setup lttng message with no payload */
1871 ret
= setup_lttng_msg(cmd_ctx
, 0);
1876 switch (cmd_ctx
->lsm
->domain
.type
) {
1877 case LTTNG_DOMAIN_KERNEL
:
1878 /* Create Kernel context */
1879 kctx
.ctx
= cmd_ctx
->lsm
->u
.context
.ctx
.ctx
;
1880 kctx
.u
.perf_counter
.type
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.type
;
1881 kctx
.u
.perf_counter
.config
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.config
;
1882 strncpy(kctx
.u
.perf_counter
.name
,
1883 cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.name
,
1884 LTTNG_SYMBOL_NAME_LEN
);
1885 kctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1887 /* Add kernel context to kernel tracer. See context.c */
1888 ret
= add_kernel_context(cmd_ctx
->session
->kernel_session
, &kctx
,
1889 cmd_ctx
->lsm
->u
.context
.event_name
,
1890 cmd_ctx
->lsm
->u
.context
.channel_name
);
1891 if (ret
!= LTTCOMM_OK
) {
1896 /* TODO: Userspace tracing */
1897 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1904 case LTTNG_DISABLE_CHANNEL
:
1906 struct ltt_kernel_channel
*kchan
;
1908 /* Setup lttng message with no payload */
1909 ret
= setup_lttng_msg(cmd_ctx
, 0);
1914 switch (cmd_ctx
->lsm
->domain
.type
) {
1915 case LTTNG_DOMAIN_KERNEL
:
1916 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1917 cmd_ctx
->session
->kernel_session
);
1918 if (kchan
== NULL
) {
1919 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1921 } else if (kchan
->enabled
== 1) {
1922 ret
= kernel_disable_channel(kchan
);
1924 if (ret
!= EEXIST
) {
1925 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1930 kernel_wait_quiescent(kernel_tracer_fd
);
1933 /* TODO: Userspace tracing */
1934 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1941 case LTTNG_DISABLE_EVENT
:
1943 struct ltt_kernel_channel
*kchan
;
1944 struct ltt_kernel_event
*kevent
;
1946 /* Setup lttng message with no payload */
1947 ret
= setup_lttng_msg(cmd_ctx
, 0);
1952 switch (cmd_ctx
->lsm
->domain
.type
) {
1953 case LTTNG_DOMAIN_KERNEL
:
1954 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1955 cmd_ctx
->session
->kernel_session
);
1956 if (kchan
== NULL
) {
1957 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1961 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, kchan
);
1962 if (kevent
!= NULL
) {
1963 DBG("Disabling kernel event %s for channel %s.", kevent
->event
->name
,
1964 kchan
->channel
->name
);
1965 ret
= kernel_disable_event(kevent
);
1967 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1972 kernel_wait_quiescent(kernel_tracer_fd
);
1975 /* TODO: Userspace tracing */
1976 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1983 case LTTNG_DISABLE_ALL_EVENT
:
1985 struct ltt_kernel_channel
*kchan
;
1986 struct ltt_kernel_event
*kevent
;
1988 /* Setup lttng message with no payload */
1989 ret
= setup_lttng_msg(cmd_ctx
, 0);
1994 switch (cmd_ctx
->lsm
->domain
.type
) {
1995 case LTTNG_DOMAIN_KERNEL
:
1996 DBG("Disabling all enabled kernel events");
1997 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1998 cmd_ctx
->session
->kernel_session
);
1999 if (kchan
== NULL
) {
2000 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2004 /* For each event in the kernel session */
2005 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
2006 DBG("Disabling kernel event %s for channel %s.",
2007 kevent
->event
->name
, kchan
->channel
->name
);
2008 ret
= kernel_disable_event(kevent
);
2014 /* Quiescent wait after event disable */
2015 kernel_wait_quiescent(kernel_tracer_fd
);
2018 /* TODO: Userspace tracing */
2019 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2026 case LTTNG_ENABLE_CHANNEL
:
2028 struct ltt_kernel_channel
*kchan
;
2030 /* Setup lttng message with no payload */
2031 ret
= setup_lttng_msg(cmd_ctx
, 0);
2036 switch (cmd_ctx
->lsm
->domain
.type
) {
2037 case LTTNG_DOMAIN_KERNEL
:
2038 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
2039 cmd_ctx
->session
->kernel_session
);
2040 if (kchan
== NULL
) {
2041 /* Channel not found, creating it */
2042 DBG("Creating kernel channel");
2044 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2045 &cmd_ctx
->lsm
->u
.channel
.chan
,
2046 cmd_ctx
->session
->kernel_session
->trace_path
);
2048 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2052 /* Notify kernel thread that there is a new channel */
2053 ret
= notify_kernel_pollfd();
2055 ret
= LTTCOMM_FATAL
;
2058 } else if (kchan
->enabled
== 0) {
2059 ret
= kernel_enable_channel(kchan
);
2061 if (ret
!= EEXIST
) {
2062 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
2068 kernel_wait_quiescent(kernel_tracer_fd
);
2070 case LTTNG_DOMAIN_UST_PID
:
2074 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2081 case LTTNG_ENABLE_EVENT
:
2084 struct ltt_kernel_channel
*kchan
;
2085 struct ltt_kernel_event
*kevent
;
2086 struct lttng_channel
*chan
;
2088 /* Setup lttng message with no payload */
2089 ret
= setup_lttng_msg(cmd_ctx
, 0);
2094 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2096 switch (cmd_ctx
->lsm
->domain
.type
) {
2097 case LTTNG_DOMAIN_KERNEL
:
2098 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2099 cmd_ctx
->session
->kernel_session
);
2100 if (kchan
== NULL
) {
2101 DBG("Channel not found. Creating channel %s", channel_name
);
2103 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2105 ret
= LTTCOMM_FATAL
;
2109 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2110 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2112 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2115 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2116 cmd_ctx
->session
->kernel_session
);
2117 if (kchan
== NULL
) {
2118 ERR("Channel %s not found after creation. Internal error, giving up.",
2120 ret
= LTTCOMM_FATAL
;
2125 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, kchan
);
2126 if (kevent
== NULL
) {
2127 DBG("Creating kernel event %s for channel %s.",
2128 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
2129 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, kchan
);
2131 DBG("Enabling kernel event %s for channel %s.",
2132 kevent
->event
->name
, channel_name
);
2133 ret
= kernel_enable_event(kevent
);
2134 if (ret
== -EEXIST
) {
2135 ret
= LTTCOMM_KERN_EVENT_EXIST
;
2141 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2145 kernel_wait_quiescent(kernel_tracer_fd
);
2148 /* TODO: Userspace tracing */
2149 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2155 case LTTNG_ENABLE_ALL_EVENT
:
2159 struct ltt_kernel_channel
*kchan
;
2160 struct ltt_kernel_event
*kevent
;
2161 struct lttng_event
*event_list
;
2162 struct lttng_channel
*chan
;
2164 /* Setup lttng message with no payload */
2165 ret
= setup_lttng_msg(cmd_ctx
, 0);
2170 DBG("Enabling all kernel event");
2172 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2174 switch (cmd_ctx
->lsm
->domain
.type
) {
2175 case LTTNG_DOMAIN_KERNEL
:
2176 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2177 cmd_ctx
->session
->kernel_session
);
2178 if (kchan
== NULL
) {
2179 DBG("Channel not found. Creating channel %s", channel_name
);
2181 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2183 ret
= LTTCOMM_FATAL
;
2187 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2188 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2190 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2193 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2194 cmd_ctx
->session
->kernel_session
);
2195 if (kchan
== NULL
) {
2196 ERR("Channel %s not found after creation. Internal error, giving up.",
2198 ret
= LTTCOMM_FATAL
;
2203 /* For each event in the kernel session */
2204 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
2205 DBG("Enabling kernel event %s for channel %s.",
2206 kevent
->event
->name
, channel_name
);
2207 ret
= kernel_enable_event(kevent
);
2213 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
2215 ret
= LTTCOMM_KERN_LIST_FAIL
;
2219 for (i
= 0; i
< size
; i
++) {
2220 kevent
= trace_kernel_get_event_by_name(event_list
[i
].name
, kchan
);
2221 if (kevent
== NULL
) {
2222 /* Default event type for enable all */
2223 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2224 /* Enable each single tracepoint event */
2225 ret
= kernel_create_event(&event_list
[i
], kchan
);
2227 /* Ignore error here and continue */
2234 /* Quiescent wait after event enable */
2235 kernel_wait_quiescent(kernel_tracer_fd
);
2238 /* TODO: Userspace tracing */
2239 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2246 case LTTNG_LIST_TRACEPOINTS
:
2248 struct lttng_event
*events
;
2249 ssize_t nb_events
= 0;
2251 switch (cmd_ctx
->lsm
->domain
.type
) {
2252 case LTTNG_DOMAIN_KERNEL
:
2253 DBG("Listing kernel events");
2254 nb_events
= kernel_list_events(kernel_tracer_fd
, &events
);
2255 if (nb_events
< 0) {
2256 ret
= LTTCOMM_KERN_LIST_FAIL
;
2261 /* TODO: Userspace listing */
2262 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2267 * Setup lttng message with payload size set to the event list size in
2268 * bytes and then copy list into the llm payload.
2270 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2276 /* Copy event list into message payload */
2277 memcpy(cmd_ctx
->llm
->payload
, events
,
2278 sizeof(struct lttng_event
) * nb_events
);
2285 case LTTNG_START_TRACE
:
2287 struct ltt_kernel_channel
*chan
;
2289 /* Setup lttng message with no payload */
2290 ret
= setup_lttng_msg(cmd_ctx
, 0);
2295 /* Kernel tracing */
2296 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2297 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
2298 DBG("Open kernel metadata");
2299 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
2300 cmd_ctx
->session
->kernel_session
->trace_path
);
2302 ret
= LTTCOMM_KERN_META_FAIL
;
2307 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2308 DBG("Opening kernel metadata stream");
2309 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2310 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
2312 ERR("Kernel create metadata stream failed");
2313 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2319 /* For each channel */
2320 cds_list_for_each_entry(chan
,
2321 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2322 if (chan
->stream_count
== 0) {
2323 ret
= kernel_open_channel_stream(chan
);
2325 ERR("Kernel create channel stream failed");
2326 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2329 /* Update the stream global counter */
2330 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
2334 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
2336 ret
= LTTCOMM_KERN_START_FAIL
;
2340 DBG("Start kernel tracing");
2341 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
2343 ERR("Kernel start session failed");
2344 ret
= LTTCOMM_KERN_START_FAIL
;
2348 /* Quiescent wait after starting trace */
2349 kernel_wait_quiescent(kernel_tracer_fd
);
2352 /* TODO: Start all UST traces */
2357 case LTTNG_STOP_TRACE
:
2359 struct ltt_kernel_channel
*chan
;
2360 /* Setup lttng message with no payload */
2361 ret
= setup_lttng_msg(cmd_ctx
, 0);
2367 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2368 DBG("Stop kernel tracing");
2370 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
2372 ERR("Kernel metadata flush failed");
2375 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2376 ret
= kernel_flush_buffer(chan
);
2378 ERR("Kernel flush buffer error");
2382 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
2384 ERR("Kernel stop session failed");
2385 ret
= LTTCOMM_KERN_STOP_FAIL
;
2389 /* Quiescent wait after stopping trace */
2390 kernel_wait_quiescent(kernel_tracer_fd
);
2393 /* TODO : User-space tracer */
2398 case LTTNG_CREATE_SESSION
:
2400 /* Setup lttng message with no payload */
2401 ret
= setup_lttng_msg(cmd_ctx
, 0);
2406 tracepoint(create_session_start
);
2407 ret
= create_session(cmd_ctx
->lsm
->session
.name
, cmd_ctx
->lsm
->session
.path
);
2408 tracepoint(create_session_end
);
2410 if (ret
== -EEXIST
) {
2411 ret
= LTTCOMM_EXIST_SESS
;
2413 ret
= LTTCOMM_FATAL
;
2421 case LTTNG_DESTROY_SESSION
:
2423 /* Setup lttng message with no payload */
2424 ret
= setup_lttng_msg(cmd_ctx
, 0);
2429 /* Clean kernel session teardown */
2430 teardown_kernel_session(cmd_ctx
->session
);
2432 tracepoint(destroy_session_start
);
2433 ret
= destroy_session(cmd_ctx
->lsm
->session
.name
);
2434 tracepoint(destroy_session_end
);
2436 ret
= LTTCOMM_FATAL
;
2441 * Must notify the kernel thread here to update it's pollfd in order to
2442 * remove the channel(s)' fd just destroyed.
2444 ret
= notify_kernel_pollfd();
2446 ret
= LTTCOMM_FATAL
;
2453 case LTTNG_LIST_DOMAINS
:
2457 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2461 nb_dom
+= cmd_ctx
->session
->ust_session_list
.count
;
2463 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_domain
) * nb_dom
);
2468 ((struct lttng_domain
*)(cmd_ctx
->llm
->payload
))[0].type
=
2469 LTTNG_DOMAIN_KERNEL
;
2471 /* TODO: User-space tracer domain support */
2475 case LTTNG_LIST_CHANNELS
:
2478 * TODO: Only kernel channels are listed here. UST listing
2479 * is needed on lttng-ust 2.0 release.
2482 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2483 nb_chan
+= cmd_ctx
->session
->kernel_session
->channel_count
;
2486 ret
= setup_lttng_msg(cmd_ctx
,
2487 sizeof(struct lttng_channel
) * nb_chan
);
2492 list_lttng_channels(cmd_ctx
->session
,
2493 (struct lttng_channel
*)(cmd_ctx
->llm
->payload
));
2498 case LTTNG_LIST_EVENTS
:
2501 * TODO: Only kernel events are listed here. UST listing
2502 * is needed on lttng-ust 2.0 release.
2504 size_t nb_event
= 0;
2505 struct ltt_kernel_channel
*kchan
= NULL
;
2507 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2508 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.list
.channel_name
,
2509 cmd_ctx
->session
->kernel_session
);
2510 if (kchan
== NULL
) {
2511 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2514 nb_event
+= kchan
->event_count
;
2517 ret
= setup_lttng_msg(cmd_ctx
,
2518 sizeof(struct lttng_event
) * nb_event
);
2523 DBG("Listing events (%zu events)", nb_event
);
2525 list_lttng_events(kchan
,
2526 (struct lttng_event
*)(cmd_ctx
->llm
->payload
));
2531 case LTTNG_LIST_SESSIONS
:
2533 lock_session_list();
2535 if (session_list_ptr
->count
== 0) {
2536 ret
= LTTCOMM_NO_SESSION
;
2537 unlock_session_list();
2541 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
2542 session_list_ptr
->count
);
2544 unlock_session_list();
2548 /* Filled the session array */
2549 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
2551 unlock_session_list();
2556 case LTTNG_CALIBRATE
:
2558 /* Setup lttng message with no payload */
2559 ret
= setup_lttng_msg(cmd_ctx
, 0);
2564 switch (cmd_ctx
->lsm
->domain
.type
) {
2565 case LTTNG_DOMAIN_KERNEL
:
2567 struct lttng_kernel_calibrate kcalibrate
;
2569 kcalibrate
.type
= cmd_ctx
->lsm
->u
.calibrate
.type
;
2570 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2572 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2578 /* TODO: Userspace tracing */
2579 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2585 case LTTNG_REGISTER_CONSUMER
:
2589 /* Setup lttng message with no payload */
2590 ret
= setup_lttng_msg(cmd_ctx
, 0);
2595 switch (cmd_ctx
->lsm
->domain
.type
) {
2596 case LTTNG_DOMAIN_KERNEL
:
2598 /* Can't register a consumer if there is already one */
2599 if (cmd_ctx
->session
->kernel_session
->consumer_fd
!= 0) {
2600 ret
= LTTCOMM_CONNECT_FAIL
;
2604 sock
= lttcomm_connect_unix_sock(cmd_ctx
->lsm
->u
.reg
.path
);
2606 ret
= LTTCOMM_CONNECT_FAIL
;
2610 cmd_ctx
->session
->kernel_session
->consumer_fd
= sock
;
2614 /* TODO: Userspace tracing */
2615 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2624 /* Undefined command */
2625 ret
= setup_lttng_msg(cmd_ctx
, 0);
2634 /* Set return code */
2635 cmd_ctx
->llm
->ret_code
= ret
;
2637 if (cmd_ctx
->session
) {
2638 unlock_session(cmd_ctx
->session
);
2644 if (cmd_ctx
->llm
== NULL
) {
2645 DBG("Missing llm structure. Allocating one.");
2646 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2650 /* Notify client of error */
2651 cmd_ctx
->llm
->ret_code
= ret
;
2654 if (cmd_ctx
->session
) {
2655 unlock_session(cmd_ctx
->session
);
2661 * This thread manage all clients request using the unix client socket for
2664 static void *thread_manage_clients(void *data
)
2667 struct command_ctx
*cmd_ctx
= NULL
;
2668 struct pollfd pollfd
[2];
2670 tracepoint(sessiond_th_cli_start
);
2672 DBG("[thread] Manage client started");
2674 ret
= lttcomm_listen_unix_sock(client_sock
);
2679 /* First fd is always the quit pipe */
2680 pollfd
[0].fd
= thread_quit_pipe
[0];
2683 pollfd
[1].fd
= client_sock
;
2684 pollfd
[1].events
= POLLIN
;
2686 /* Notify parent pid that we are ready
2687 * to accept command for client side.
2689 if (opt_sig_parent
) {
2690 kill(ppid
, SIGCHLD
);
2694 DBG("Accepting client command ...");
2696 tracepoint(sessiond_th_cli_poll
);
2698 /* Inifinite blocking call, waiting for transmission */
2699 ret
= poll(pollfd
, 2, -1);
2701 perror("poll client thread");
2705 /* Thread quit pipe has been closed. Killing thread. */
2706 if (pollfd
[0].revents
== POLLNVAL
) {
2708 } else if (pollfd
[1].revents
== POLLERR
) {
2709 ERR("Client socket poll error");
2713 sock
= lttcomm_accept_unix_sock(client_sock
);
2718 /* Allocate context command to process the client request */
2719 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2721 /* Allocate data buffer for reception */
2722 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2723 cmd_ctx
->llm
= NULL
;
2724 cmd_ctx
->session
= NULL
;
2727 * Data is received from the lttng client. The struct
2728 * lttcomm_session_msg (lsm) contains the command and data request of
2731 DBG("Receiving data from client ...");
2732 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2737 // TODO: Validate cmd_ctx including sanity check for security purpose.
2740 * This function dispatch the work to the kernel or userspace tracer
2741 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2742 * informations for the client. The command context struct contains
2743 * everything this function may needs.
2745 ret
= process_client_msg(cmd_ctx
);
2747 /* TODO: Inform client somehow of the fatal error. At this point,
2748 * ret < 0 means that a malloc failed (ENOMEM). */
2749 /* Error detected but still accept command */
2750 clean_command_ctx(&cmd_ctx
);
2754 DBG("Sending response (size: %d, retcode: %d)",
2755 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2756 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2758 ERR("Failed to send data back to client");
2761 clean_command_ctx(&cmd_ctx
);
2763 /* End of transmission */
2768 DBG("Client thread dying");
2776 unlink(client_unix_sock_path
);
2778 clean_command_ctx(&cmd_ctx
);
2784 * usage function on stderr
2786 static void usage(void)
2788 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2789 fprintf(stderr
, " -h, --help Display this usage.\n");
2790 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2791 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2792 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2793 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2794 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2795 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2796 fprintf(stderr
, " -V, --version Show version number.\n");
2797 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2798 fprintf(stderr
, " -q, --quiet No output at all.\n");
2799 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2800 fprintf(stderr
, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2804 * daemon argument parsing
2806 static int parse_args(int argc
, char **argv
)
2810 static struct option long_options
[] = {
2811 { "client-sock", 1, 0, 'c' },
2812 { "apps-sock", 1, 0, 'a' },
2813 { "kconsumerd-cmd-sock", 1, 0, 0 },
2814 { "kconsumerd-err-sock", 1, 0, 0 },
2815 { "daemonize", 0, 0, 'd' },
2816 { "sig-parent", 0, 0, 'S' },
2817 { "help", 0, 0, 'h' },
2818 { "group", 1, 0, 'g' },
2819 { "version", 0, 0, 'V' },
2820 { "quiet", 0, 0, 'q' },
2821 { "verbose", 0, 0, 'v' },
2822 { "verbose-kconsumerd", 0, 0, 'Z' },
2827 int option_index
= 0;
2828 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:Z", long_options
, &option_index
);
2835 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2837 fprintf(stderr
, " with arg %s\n", optarg
);
2841 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2844 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2850 opt_tracing_group
= strdup(optarg
);
2856 fprintf(stdout
, "%s\n", VERSION
);
2862 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2865 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2871 /* Verbose level can increase using multiple -v */
2875 opt_verbose_kconsumerd
+= 1;
2878 /* Unknown option or other error.
2879 * Error is printed by getopt, just return */
2888 * Creates the two needed socket by the daemon.
2889 * apps_sock - The communication socket for all UST apps.
2890 * client_sock - The communication of the cli tool (lttng).
2892 static int init_daemon_socket(void)
2897 old_umask
= umask(0);
2899 /* Create client tool unix socket */
2900 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2901 if (client_sock
< 0) {
2902 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2907 /* File permission MUST be 660 */
2908 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2910 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2915 /* Create the application unix socket */
2916 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2917 if (apps_sock
< 0) {
2918 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2923 /* File permission MUST be 666 */
2924 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2926 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2937 * Check if the global socket is available, and if a daemon is answering
2938 * at the other side. If yes, error is returned.
2940 static int check_existing_daemon(void)
2942 if (access(client_unix_sock_path
, F_OK
) < 0 &&
2943 access(apps_unix_sock_path
, F_OK
) < 0) {
2946 /* Is there anybody out there ? */
2947 if (lttng_session_daemon_alive()) {
2955 * Set the tracing group gid onto the client socket.
2957 * Race window between mkdir and chown is OK because we are going from more
2958 * permissive (root.root) to les permissive (root.tracing).
2960 static int set_permissions(void)
2965 gid
= allowed_group();
2968 WARN("No tracing group detected");
2971 ERR("Missing tracing group. Aborting execution.");
2977 /* Set lttng run dir */
2978 ret
= chown(LTTNG_RUNDIR
, 0, gid
);
2980 ERR("Unable to set group on " LTTNG_RUNDIR
);
2984 /* lttng client socket path */
2985 ret
= chown(client_unix_sock_path
, 0, gid
);
2987 ERR("Unable to set group on %s", client_unix_sock_path
);
2991 /* kconsumerd error socket path */
2992 ret
= chown(kconsumerd_err_unix_sock_path
, 0, gid
);
2994 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2998 DBG("All permissions are set");
3005 * Create the pipe used to wake up the kernel thread.
3007 static int create_kernel_poll_pipe(void)
3009 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
3013 * Create the application command pipe to wake thread_manage_apps.
3015 static int create_apps_cmd_pipe(void)
3017 return pipe2(apps_cmd_pipe
, O_CLOEXEC
);
3021 * Create the lttng run directory needed for all global sockets and pipe.
3023 static int create_lttng_rundir(void)
3027 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
3029 if (errno
!= EEXIST
) {
3030 ERR("Unable to create " LTTNG_RUNDIR
);
3042 * Setup sockets and directory needed by the kconsumerd communication with the
3045 static int set_kconsumerd_sockets(void)
3049 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
3050 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
3053 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
3054 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
3057 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
3059 if (errno
!= EEXIST
) {
3060 ERR("Failed to create " KCONSUMERD_PATH
);
3066 /* Create the kconsumerd error unix socket */
3067 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
3068 if (kconsumerd_err_sock
< 0) {
3069 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
3074 /* File permission MUST be 660 */
3075 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3077 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
3087 * Signal handler for the daemon
3089 * Simply stop all worker threads, leaving main() return gracefully
3090 * after joining all threads and calling cleanup().
3092 static void sighandler(int sig
)
3096 DBG("SIGPIPE catched");
3099 DBG("SIGINT catched");
3103 DBG("SIGTERM catched");
3112 * Setup signal handler for :
3113 * SIGINT, SIGTERM, SIGPIPE
3115 static int set_signal_handler(void)
3118 struct sigaction sa
;
3121 if ((ret
= sigemptyset(&sigset
)) < 0) {
3122 perror("sigemptyset");
3126 sa
.sa_handler
= sighandler
;
3127 sa
.sa_mask
= sigset
;
3129 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3130 perror("sigaction");
3134 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3135 perror("sigaction");
3139 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3140 perror("sigaction");
3144 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3150 * Set open files limit to unlimited. This daemon can open a large number of
3151 * file descriptors in order to consumer multiple kernel traces.
3153 static void set_ulimit(void)
3158 /* The kernel does not allowed an infinite limit for open files */
3159 lim
.rlim_cur
= 65535;
3160 lim
.rlim_max
= 65535;
3162 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3164 perror("failed to set open files limit");
3171 int main(int argc
, char **argv
)
3175 const char *home_path
;
3177 tracepoint(sessiond_boot_start
);
3179 /* Create thread quit pipe */
3180 if ((ret
= init_thread_quit_pipe()) < 0) {
3184 /* Parse arguments */
3186 if ((ret
= parse_args(argc
, argv
) < 0)) {
3199 /* Check if daemon is UID = 0 */
3200 is_root
= !getuid();
3203 ret
= create_lttng_rundir();
3208 if (strlen(apps_unix_sock_path
) == 0) {
3209 snprintf(apps_unix_sock_path
, PATH_MAX
,
3210 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
3213 if (strlen(client_unix_sock_path
) == 0) {
3214 snprintf(client_unix_sock_path
, PATH_MAX
,
3215 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
3218 /* Set global SHM for ust */
3219 if (strlen(wait_shm_path
) == 0) {
3220 snprintf(wait_shm_path
, PATH_MAX
,
3221 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
3224 home_path
= get_home_dir();
3225 if (home_path
== NULL
) {
3226 /* TODO: Add --socket PATH option */
3227 ERR("Can't get HOME directory for sockets creation.");
3232 if (strlen(apps_unix_sock_path
) == 0) {
3233 snprintf(apps_unix_sock_path
, PATH_MAX
,
3234 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
3237 /* Set the cli tool unix socket path */
3238 if (strlen(client_unix_sock_path
) == 0) {
3239 snprintf(client_unix_sock_path
, PATH_MAX
,
3240 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
3243 /* Set global SHM for ust */
3244 if (strlen(wait_shm_path
) == 0) {
3245 snprintf(wait_shm_path
, PATH_MAX
,
3246 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, geteuid());
3250 DBG("Client socket path %s", client_unix_sock_path
);
3251 DBG("Application socket path %s", apps_unix_sock_path
);
3254 * See if daemon already exist.
3256 if ((ret
= check_existing_daemon()) < 0) {
3257 ERR("Already running daemon.\n");
3259 * We do not goto exit because we must not cleanup()
3260 * because a daemon is already running.
3265 /* After this point, we can safely call cleanup() so goto error is used */
3268 * These actions must be executed as root. We do that *after* setting up
3269 * the sockets path because we MUST make the check for another daemon using
3270 * those paths *before* trying to set the kernel consumer sockets and init
3274 ret
= set_kconsumerd_sockets();
3279 /* Setup kernel tracer */
3280 init_kernel_tracer();
3282 /* Set ulimit for open files */
3286 if ((ret
= set_signal_handler()) < 0) {
3290 /* Setup the needed unix socket */
3291 if ((ret
= init_daemon_socket()) < 0) {
3295 /* Set credentials to socket */
3296 if (is_root
&& ((ret
= set_permissions()) < 0)) {
3300 /* Get parent pid if -S, --sig-parent is specified. */
3301 if (opt_sig_parent
) {
3305 /* Setup the kernel pipe for waking up the kernel thread */
3306 if ((ret
= create_kernel_poll_pipe()) < 0) {
3310 /* Setup the thread apps communication pipe. */
3311 if ((ret
= create_apps_cmd_pipe()) < 0) {
3315 /* Init UST command queue. */
3316 cds_wfq_init(&ust_cmd_queue
.queue
);
3319 * Get session list pointer. This pointer MUST NOT be free().
3320 * This list is statically declared in session.c
3322 session_list_ptr
= get_session_list();
3324 /* Create thread to manage the client socket */
3325 ret
= pthread_create(&client_thread
, NULL
,
3326 thread_manage_clients
, (void *) NULL
);
3328 perror("pthread_create clients");
3332 /* Create thread to dispatch registration */
3333 ret
= pthread_create(&dispatch_thread
, NULL
,
3334 thread_dispatch_ust_registration
, (void *) NULL
);
3336 perror("pthread_create dispatch");
3340 /* Create thread to manage application registration. */
3341 ret
= pthread_create(®_apps_thread
, NULL
,
3342 thread_registration_apps
, (void *) NULL
);
3344 perror("pthread_create registration");
3348 /* Create thread to manage application socket */
3349 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
3351 perror("pthread_create apps");
3355 /* Create kernel thread to manage kernel event */
3356 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
3358 perror("pthread_create kernel");
3362 tracepoint(sessiond_boot_end
);
3364 ret
= pthread_join(kernel_thread
, &status
);
3366 perror("pthread_join");
3367 goto error
; /* join error, exit without cleanup */
3371 ret
= pthread_join(apps_thread
, &status
);
3373 perror("pthread_join");
3374 goto error
; /* join error, exit without cleanup */
3378 ret
= pthread_join(reg_apps_thread
, &status
);
3380 perror("pthread_join");
3381 goto error
; /* join error, exit without cleanup */
3385 ret
= pthread_join(dispatch_thread
, &status
);
3387 perror("pthread_join");
3388 goto error
; /* join error, exit without cleanup */
3392 ret
= pthread_join(client_thread
, &status
);
3394 perror("pthread_join");
3395 goto error
; /* join error, exit without cleanup */
3398 ret
= join_kconsumerd_thread();
3400 perror("join_kconsumerd");
3401 goto error
; /* join error, exit without cleanup */
3407 * cleanup() is called when no other thread is running.