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
)
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");
947 /* See if we have a valid socket to add to pollfd */
948 if (ust_cmd
.sock
!= -1) {
950 update_poll_flag
= 1;
953 /* The pollfd struct must be updated */
954 if (update_poll_flag
) {
955 ret
= update_apps_cmd_pollfd(nb_fd
, ARRAY_SIZE(pollfd
), &pollfd
);
957 /* malloc failed so we quit */
961 if (ust_cmd
.sock
!= -1) {
962 /* Update pollfd with the new UST socket */
963 DBG("Adding sock %d to apps cmd pollfd", ust_cmd
.sock
);
964 pollfd
[nb_fd
- 1].fd
= ust_cmd
.sock
;
965 pollfd
[nb_fd
- 1].events
= POLLHUP
| POLLNVAL
| POLLERR
;
970 DBG("Apps thread polling on %d fds", nb_fd
);
972 tracepoint(sessiond_th_apps_poll
);
974 /* Inifinite blocking call, waiting for transmission */
975 ret
= poll(pollfd
, nb_fd
, -1);
977 perror("poll apps thread");
981 /* Thread quit pipe has been closed. Killing thread. */
982 if (pollfd
[0].revents
== POLLNVAL
) {
985 /* apps_cmd_pipe pipe events */
986 switch (pollfd
[1].revents
) {
988 ERR("Apps command pipe poll error");
991 tracepoint(ust_register_read_start
);
994 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
995 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
996 perror("read apps cmd pipe");
1000 tracepoint(ust_register_read_stop
);
1002 tracepoint(ust_register_add_start
);
1003 /* Register applicaton to the session daemon */
1004 ret
= register_traceable_app(&ust_cmd
.reg_msg
, ust_cmd
.sock
);
1006 /* Only critical ENOMEM error can be returned here */
1009 tracepoint(ust_register_add_stop
);
1011 tracepoint(ust_register_done_start
);
1012 ret
= ustctl_register_done(ust_cmd
.sock
);
1015 * If the registration is not possible, we simply unregister
1016 * the apps and continue
1018 unregister_traceable_app(ust_cmd
.sock
);
1021 tracepoint(ust_register_done_stop
);
1027 for (i
= 2; i
< count
; i
++) {
1028 /* Apps socket is closed/hungup */
1029 switch (pollfd
[i
].revents
) {
1034 unregister_traceable_app(pollfd
[i
].fd
);
1035 /* Indicate to remove this fd from the pollfd */
1042 if (nb_fd
!= count
) {
1043 update_poll_flag
= 1;
1048 DBG("Application communication apps dying");
1049 close(apps_cmd_pipe
[0]);
1050 close(apps_cmd_pipe
[1]);
1058 * Dispatch request from the registration threads to the application
1059 * communication thread.
1061 static void *thread_dispatch_ust_registration(void *data
)
1064 struct cds_wfq_node
*node
;
1065 struct ust_command
*ust_cmd
= NULL
;
1067 tracepoint(sessiond_th_dispatch_start
);
1069 DBG("[thread] Dispatch UST command started");
1071 while (!dispatch_thread_exit
) {
1072 /* Atomically prepare the queue futex */
1073 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1076 tracepoint(sessiond_th_dispatch_block
);
1078 /* Dequeue command for registration */
1079 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1081 DBG("Waked up but nothing in the UST command queue");
1082 /* Continue thread execution */
1086 tracepoint(ust_dispatch_register_start
);
1088 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1090 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1091 " gid:%d sock:%d name:%s (version %d.%d)",
1092 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1093 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1094 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1095 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1097 * Inform apps thread of the new application registration. This
1098 * call is blocking so we can be assured that the data will be read
1099 * at some point in time or wait to the end of the world :)
1101 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1102 sizeof(struct ust_command
));
1104 perror("write apps cmd pipe");
1105 if (errno
== EBADF
) {
1107 * We can't inform the application thread to process
1108 * registration. We will exit or else application
1109 * registration will not occur and tracing will never
1116 } while (node
!= NULL
);
1118 tracepoint(ust_dispatch_register_stop
);
1120 /* Futex wait on queue. Blocking call on futex() */
1121 futex_nto1_wait(&ust_cmd_queue
.futex
);
1125 DBG("Dispatch thread dying");
1130 * This thread manage application registration.
1132 static void *thread_registration_apps(void *data
)
1135 struct pollfd pollfd
[2];
1137 * Get allocated in this thread, enqueued to a global queue, dequeued and
1138 * freed in the manage apps thread.
1140 struct ust_command
*ust_cmd
= NULL
;
1142 tracepoint(sessiond_th_reg_start
);
1144 DBG("[thread] Manage application registration started");
1146 ret
= lttcomm_listen_unix_sock(apps_sock
);
1151 /* First fd is always the quit pipe */
1152 pollfd
[0].fd
= thread_quit_pipe
[0];
1155 pollfd
[1].fd
= apps_sock
;
1156 pollfd
[1].events
= POLLIN
;
1158 /* Notify all applications to register */
1159 ret
= notify_ust_apps(1);
1161 ERR("Failed to notify applications or create the wait shared memory.\n"
1162 "Execution continues but there might be problem for already running\n"
1163 "applications that wishes to register.");
1167 DBG("Accepting application registration");
1169 tracepoint(sessiond_th_reg_poll
);
1171 /* Inifinite blocking call, waiting for transmission */
1172 ret
= poll(pollfd
, 2, -1);
1174 perror("poll register apps thread");
1178 /* Thread quit pipe has been closed. Killing thread. */
1179 if (pollfd
[0].revents
== POLLNVAL
) {
1181 } else if (pollfd
[1].revents
== POLLERR
) {
1182 ERR("Register apps socket poll error");
1186 /* Registration starts here. Recording cycles */
1187 tracepoint(ust_register_start
);
1189 sock
= lttcomm_accept_unix_sock(apps_sock
);
1194 /* Create UST registration command for enqueuing */
1195 ust_cmd
= malloc(sizeof(struct ust_command
));
1196 if (ust_cmd
== NULL
) {
1197 perror("ust command malloc");
1202 * Using message-based transmissions to ensure we don't have to deal
1203 * with partially received messages.
1205 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1206 sizeof(struct ust_register_msg
));
1207 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1209 perror("lttcomm_recv_unix_sock register apps");
1211 ERR("Wrong size received on apps register");
1218 ust_cmd
->sock
= sock
;
1220 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1221 " gid:%d sock:%d name:%s (version %d.%d)",
1222 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1223 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1224 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1225 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1227 * Lock free enqueue the registration request.
1228 * The red pill has been taken! This apps will be part of the *system*
1230 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1233 * Wake the registration queue futex.
1234 * Implicit memory barrier with the exchange in cds_wfq_enqueue.
1236 futex_nto1_wake(&ust_cmd_queue
.futex
);
1238 tracepoint(ust_register_stop
);
1242 DBG("UST Registration thread dying");
1244 /* Notify that the registration thread is gone */
1250 unlink(apps_unix_sock_path
);
1256 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
1257 * exec or it will fails.
1259 static int spawn_kconsumerd_thread(void)
1263 /* Setup semaphore */
1264 sem_init(&kconsumerd_sem
, 0, 0);
1266 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
1268 perror("pthread_create kconsumerd");
1272 /* Wait for the kconsumerd thread to be ready */
1273 sem_wait(&kconsumerd_sem
);
1275 if (kconsumerd_pid
== 0) {
1276 ERR("Kconsumerd did not start");
1283 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1288 * Join kernel consumer thread
1290 static int join_kconsumerd_thread(void)
1295 if (kconsumerd_pid
!= 0) {
1296 ret
= kill(kconsumerd_pid
, SIGTERM
);
1298 ERR("Error killing kconsumerd");
1301 return pthread_join(kconsumerd_thread
, &status
);
1308 * Fork and exec a kernel consumer daemon (kconsumerd).
1310 * Return pid if successful else -1.
1312 static pid_t
spawn_kconsumerd(void)
1316 const char *verbosity
;
1318 DBG("Spawning kconsumerd");
1325 if (opt_verbose
> 1 || opt_verbose_kconsumerd
) {
1326 verbosity
= "--verbose";
1328 verbosity
= "--quiet";
1330 execl(INSTALL_BIN_PATH
"/ltt-kconsumerd", "ltt-kconsumerd", verbosity
, NULL
);
1332 perror("kernel start consumer exec");
1335 } else if (pid
> 0) {
1339 perror("kernel start consumer fork");
1349 * Spawn the kconsumerd daemon and session daemon thread.
1351 static int start_kconsumerd(void)
1355 pthread_mutex_lock(&kconsumerd_pid_mutex
);
1356 if (kconsumerd_pid
!= 0) {
1357 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1361 ret
= spawn_kconsumerd();
1363 ERR("Spawning kconsumerd failed");
1364 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1365 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1369 /* Setting up the global kconsumerd_pid */
1370 kconsumerd_pid
= ret
;
1371 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1373 DBG("Kconsumerd pid %d", ret
);
1375 DBG("Spawning kconsumerd thread");
1376 ret
= spawn_kconsumerd_thread();
1378 ERR("Fatal error spawning kconsumerd thread");
1390 * modprobe_kernel_modules
1392 static int modprobe_kernel_modules(void)
1397 for (i
= 0; i
< ARRAY_SIZE(kernel_modules_list
); i
++) {
1398 ret
= snprintf(modprobe
, sizeof(modprobe
),
1399 "/sbin/modprobe %s%s",
1400 kernel_modules_list
[i
].required
? "" : "--quiet ",
1401 kernel_modules_list
[i
].name
);
1403 perror("snprintf modprobe");
1406 modprobe
[sizeof(modprobe
) - 1] = '\0';
1407 ret
= system(modprobe
);
1409 ERR("Unable to launch modprobe for module %s",
1410 kernel_modules_list
[i
].name
);
1411 } else if (kernel_modules_list
[i
].required
1412 && WEXITSTATUS(ret
) != 0) {
1413 ERR("Unable to load module %s",
1414 kernel_modules_list
[i
].name
);
1416 DBG("Modprobe successfully %s",
1417 kernel_modules_list
[i
].name
);
1428 static int mount_debugfs(char *path
)
1431 char *type
= "debugfs";
1433 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, geteuid(), getegid());
1438 ret
= mount(type
, path
, type
, 0, NULL
);
1440 perror("mount debugfs");
1444 DBG("Mounted debugfs successfully at %s", path
);
1451 * Setup necessary data for kernel tracer action.
1453 static void init_kernel_tracer(void)
1456 char *proc_mounts
= "/proc/mounts";
1458 char *debugfs_path
= NULL
, *lttng_path
;
1461 /* Detect debugfs */
1462 fp
= fopen(proc_mounts
, "r");
1464 ERR("Unable to probe %s", proc_mounts
);
1468 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1469 if (strstr(line
, "debugfs") != NULL
) {
1470 /* Remove first string */
1472 /* Dup string here so we can reuse line later on */
1473 debugfs_path
= strdup(strtok(NULL
, " "));
1474 DBG("Got debugfs path : %s", debugfs_path
);
1481 /* Mount debugfs if needded */
1482 if (debugfs_path
== NULL
) {
1483 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1485 perror("asprintf debugfs path");
1488 ret
= mount_debugfs(debugfs_path
);
1494 /* Modprobe lttng kernel modules */
1495 ret
= modprobe_kernel_modules();
1500 /* Setup lttng kernel path */
1501 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1503 perror("asprintf lttng path");
1507 /* Open debugfs lttng */
1508 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1509 if (kernel_tracer_fd
< 0) {
1510 DBG("Failed to open %s", lttng_path
);
1516 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1526 WARN("No kernel tracer available");
1527 kernel_tracer_fd
= 0;
1532 * Start tracing by creating trace directory and sending FDs to the kernel
1535 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1539 if (session
->kconsumer_fds_sent
== 0) {
1541 * Assign default kernel consumer if no consumer assigned to the kernel
1542 * session. At this point, it's NOT suppose to be 0 but this is an extra
1545 if (session
->consumer_fd
== 0) {
1546 session
->consumer_fd
= kconsumerd_cmd_sock
;
1549 ret
= send_kconsumerd_fds(session
);
1551 ERR("Send kconsumerd fds failed");
1552 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1556 session
->kconsumer_fds_sent
= 1;
1564 * Notify kernel thread to update it's pollfd.
1566 static int notify_kernel_pollfd(void)
1570 /* Inform kernel thread of the new kernel channel */
1571 ret
= write(kernel_poll_pipe
[1], "!", 1);
1573 perror("write kernel poll pipe");
1580 * Allocate a channel structure and fill it.
1582 static struct lttng_channel
*init_default_channel(enum lttng_domain_type domain_type
,
1585 struct lttng_channel
*chan
;
1587 chan
= malloc(sizeof(struct lttng_channel
));
1589 perror("init channel malloc");
1593 if (snprintf(chan
->name
, NAME_MAX
, "%s", name
) < 0) {
1594 perror("snprintf channel name");
1598 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1599 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1600 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1602 switch (domain_type
) {
1603 case LTTNG_DOMAIN_KERNEL
:
1604 chan
->attr
.subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
1605 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1606 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1610 goto error
; /* Not implemented */
1621 * Create an UST session and add it to the session ust list.
1623 static int create_ust_session(pid_t pid
, struct ltt_session
*session
)
1626 struct ltt_ust_session
*lus
;
1628 DBG("Creating UST session");
1630 lus
= trace_ust_create_session(session
->path
, pid
);
1635 ret
= mkdir_recursive(lus
->path
, S_IRWXU
| S_IRWXG
,
1636 geteuid(), allowed_group());
1638 if (ret
!= -EEXIST
) {
1639 ERR("Trace directory creation error");
1644 /* Create session on the UST tracer */
1645 ret
= ustctl_create_session(lus
);
1658 * Create a kernel tracer session then create the default channel.
1660 static int create_kernel_session(struct ltt_session
*session
)
1664 DBG("Creating kernel session");
1666 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1668 ret
= LTTCOMM_KERN_SESS_FAIL
;
1672 /* Set kernel consumer socket fd */
1673 if (kconsumerd_cmd_sock
) {
1674 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
1677 ret
= mkdir_recursive(session
->kernel_session
->trace_path
,
1678 S_IRWXU
| S_IRWXG
, geteuid(), allowed_group());
1680 if (ret
!= -EEXIST
) {
1681 ERR("Trace directory creation error");
1691 * Using the session list, filled a lttng_session array to send back to the
1692 * client for session listing.
1694 * The session list lock MUST be acquired before calling this function. Use
1695 * lock_session_list() and unlock_session_list().
1697 static void list_lttng_sessions(struct lttng_session
*sessions
)
1700 struct ltt_session
*session
;
1702 DBG("Getting all available session");
1704 * Iterate over session list and append data after the control struct in
1707 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1708 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1709 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
1710 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1711 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
1717 * Fill lttng_channel array of all channels.
1719 static void list_lttng_channels(struct ltt_session
*session
,
1720 struct lttng_channel
*channels
)
1723 struct ltt_kernel_channel
*kchan
;
1725 DBG("Listing channels for session %s", session
->name
);
1727 /* Kernel channels */
1728 if (session
->kernel_session
!= NULL
) {
1729 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
1730 /* Copy lttng_channel struct to array */
1731 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1732 channels
[i
].enabled
= kchan
->enabled
;
1737 /* TODO: Missing UST listing */
1741 * Fill lttng_event array of all events in the channel.
1743 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1744 struct lttng_event
*events
)
1747 * TODO: This is ONLY kernel. Need UST support.
1750 struct ltt_kernel_event
*event
;
1752 DBG("Listing events for channel %s", kchan
->channel
->name
);
1754 /* Kernel channels */
1755 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1756 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1757 events
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1758 events
[i
].enabled
= event
->enabled
;
1759 switch (event
->event
->instrumentation
) {
1760 case LTTNG_KERNEL_TRACEPOINT
:
1761 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1763 case LTTNG_KERNEL_KPROBE
:
1764 case LTTNG_KERNEL_KRETPROBE
:
1765 events
[i
].type
= LTTNG_EVENT_PROBE
;
1766 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
1767 sizeof(struct lttng_kernel_kprobe
));
1769 case LTTNG_KERNEL_FUNCTION
:
1770 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
1771 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
1772 sizeof(struct lttng_kernel_function
));
1780 * Process the command requested by the lttng client within the command
1781 * context structure. This function make sure that the return structure (llm)
1782 * is set and ready for transmission before returning.
1784 * Return any error encountered or 0 for success.
1786 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1788 int ret
= LTTCOMM_OK
;
1790 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1793 * Commands that DO NOT need a session.
1795 switch (cmd_ctx
->lsm
->cmd_type
) {
1796 case LTTNG_CREATE_SESSION
:
1797 case LTTNG_LIST_SESSIONS
:
1798 case LTTNG_LIST_TRACEPOINTS
:
1799 case LTTNG_CALIBRATE
:
1802 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
1803 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session
.name
);
1804 if (cmd_ctx
->session
== NULL
) {
1805 /* If session name not found */
1806 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
1807 ret
= LTTCOMM_SESS_NOT_FOUND
;
1808 } else { /* If no session name specified */
1809 ret
= LTTCOMM_SELECT_SESS
;
1813 /* Acquire lock for the session */
1814 lock_session(cmd_ctx
->session
);
1820 * Check domain type for specific "pre-action".
1822 switch (cmd_ctx
->lsm
->domain
.type
) {
1823 case LTTNG_DOMAIN_KERNEL
:
1824 /* Kernel tracer check */
1825 if (kernel_tracer_fd
== 0) {
1826 init_kernel_tracer();
1827 if (kernel_tracer_fd
== 0) {
1828 ret
= LTTCOMM_KERN_NA
;
1832 /* Need a session for kernel command */
1833 switch (cmd_ctx
->lsm
->cmd_type
) {
1834 case LTTNG_CALIBRATE
:
1835 case LTTNG_CREATE_SESSION
:
1836 case LTTNG_LIST_SESSIONS
:
1837 case LTTNG_LIST_TRACEPOINTS
:
1840 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1841 ret
= create_kernel_session(cmd_ctx
->session
);
1843 ret
= LTTCOMM_KERN_SESS_FAIL
;
1846 /* Start the kernel consumer daemon */
1847 if (kconsumerd_pid
== 0 &&
1848 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1849 ret
= start_kconsumerd();
1857 case LTTNG_DOMAIN_UST_PID
:
1863 /* Process by command type */
1864 switch (cmd_ctx
->lsm
->cmd_type
) {
1865 case LTTNG_ADD_CONTEXT
:
1867 struct lttng_kernel_context kctx
;
1869 /* Setup lttng message with no payload */
1870 ret
= setup_lttng_msg(cmd_ctx
, 0);
1875 switch (cmd_ctx
->lsm
->domain
.type
) {
1876 case LTTNG_DOMAIN_KERNEL
:
1877 /* Create Kernel context */
1878 kctx
.ctx
= cmd_ctx
->lsm
->u
.context
.ctx
.ctx
;
1879 kctx
.u
.perf_counter
.type
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.type
;
1880 kctx
.u
.perf_counter
.config
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.config
;
1881 strncpy(kctx
.u
.perf_counter
.name
,
1882 cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.name
,
1883 LTTNG_SYMBOL_NAME_LEN
);
1884 kctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1886 /* Add kernel context to kernel tracer. See context.c */
1887 ret
= add_kernel_context(cmd_ctx
->session
->kernel_session
, &kctx
,
1888 cmd_ctx
->lsm
->u
.context
.event_name
,
1889 cmd_ctx
->lsm
->u
.context
.channel_name
);
1890 if (ret
!= LTTCOMM_OK
) {
1895 /* TODO: Userspace tracing */
1896 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1903 case LTTNG_DISABLE_CHANNEL
:
1905 struct ltt_kernel_channel
*kchan
;
1907 /* Setup lttng message with no payload */
1908 ret
= setup_lttng_msg(cmd_ctx
, 0);
1913 switch (cmd_ctx
->lsm
->domain
.type
) {
1914 case LTTNG_DOMAIN_KERNEL
:
1915 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1916 cmd_ctx
->session
->kernel_session
);
1917 if (kchan
== NULL
) {
1918 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1920 } else if (kchan
->enabled
== 1) {
1921 ret
= kernel_disable_channel(kchan
);
1923 if (ret
!= EEXIST
) {
1924 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1929 kernel_wait_quiescent(kernel_tracer_fd
);
1932 /* TODO: Userspace tracing */
1933 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1940 case LTTNG_DISABLE_EVENT
:
1942 struct ltt_kernel_channel
*kchan
;
1943 struct ltt_kernel_event
*kevent
;
1945 /* Setup lttng message with no payload */
1946 ret
= setup_lttng_msg(cmd_ctx
, 0);
1951 switch (cmd_ctx
->lsm
->domain
.type
) {
1952 case LTTNG_DOMAIN_KERNEL
:
1953 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1954 cmd_ctx
->session
->kernel_session
);
1955 if (kchan
== NULL
) {
1956 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1960 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, kchan
);
1961 if (kevent
!= NULL
) {
1962 DBG("Disabling kernel event %s for channel %s.", kevent
->event
->name
,
1963 kchan
->channel
->name
);
1964 ret
= kernel_disable_event(kevent
);
1966 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1971 kernel_wait_quiescent(kernel_tracer_fd
);
1974 /* TODO: Userspace tracing */
1975 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1982 case LTTNG_DISABLE_ALL_EVENT
:
1984 struct ltt_kernel_channel
*kchan
;
1985 struct ltt_kernel_event
*kevent
;
1987 /* Setup lttng message with no payload */
1988 ret
= setup_lttng_msg(cmd_ctx
, 0);
1993 switch (cmd_ctx
->lsm
->domain
.type
) {
1994 case LTTNG_DOMAIN_KERNEL
:
1995 DBG("Disabling all enabled kernel events");
1996 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1997 cmd_ctx
->session
->kernel_session
);
1998 if (kchan
== NULL
) {
1999 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2003 /* For each event in the kernel session */
2004 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
2005 DBG("Disabling kernel event %s for channel %s.",
2006 kevent
->event
->name
, kchan
->channel
->name
);
2007 ret
= kernel_disable_event(kevent
);
2013 /* Quiescent wait after event disable */
2014 kernel_wait_quiescent(kernel_tracer_fd
);
2017 /* TODO: Userspace tracing */
2018 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2025 case LTTNG_ENABLE_CHANNEL
:
2027 struct ltt_kernel_channel
*kchan
;
2029 /* Setup lttng message with no payload */
2030 ret
= setup_lttng_msg(cmd_ctx
, 0);
2035 switch (cmd_ctx
->lsm
->domain
.type
) {
2036 case LTTNG_DOMAIN_KERNEL
:
2037 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
2038 cmd_ctx
->session
->kernel_session
);
2039 if (kchan
== NULL
) {
2040 /* Channel not found, creating it */
2041 DBG("Creating kernel channel");
2043 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2044 &cmd_ctx
->lsm
->u
.channel
.chan
,
2045 cmd_ctx
->session
->kernel_session
->trace_path
);
2047 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2051 /* Notify kernel thread that there is a new channel */
2052 ret
= notify_kernel_pollfd();
2054 ret
= LTTCOMM_FATAL
;
2057 } else if (kchan
->enabled
== 0) {
2058 ret
= kernel_enable_channel(kchan
);
2060 if (ret
!= EEXIST
) {
2061 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
2067 kernel_wait_quiescent(kernel_tracer_fd
);
2069 case LTTNG_DOMAIN_UST_PID
:
2073 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2080 case LTTNG_ENABLE_EVENT
:
2083 struct ltt_kernel_channel
*kchan
;
2084 struct ltt_kernel_event
*kevent
;
2085 struct lttng_channel
*chan
;
2087 /* Setup lttng message with no payload */
2088 ret
= setup_lttng_msg(cmd_ctx
, 0);
2093 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2095 switch (cmd_ctx
->lsm
->domain
.type
) {
2096 case LTTNG_DOMAIN_KERNEL
:
2097 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2098 cmd_ctx
->session
->kernel_session
);
2099 if (kchan
== NULL
) {
2100 DBG("Channel not found. Creating channel %s", channel_name
);
2102 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2104 ret
= LTTCOMM_FATAL
;
2108 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2109 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2111 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2114 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2115 cmd_ctx
->session
->kernel_session
);
2116 if (kchan
== NULL
) {
2117 ERR("Channel %s not found after creation. Internal error, giving up.",
2119 ret
= LTTCOMM_FATAL
;
2124 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, kchan
);
2125 if (kevent
== NULL
) {
2126 DBG("Creating kernel event %s for channel %s.",
2127 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
2128 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, kchan
);
2130 DBG("Enabling kernel event %s for channel %s.",
2131 kevent
->event
->name
, channel_name
);
2132 ret
= kernel_enable_event(kevent
);
2133 if (ret
== -EEXIST
) {
2134 ret
= LTTCOMM_KERN_EVENT_EXIST
;
2140 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2144 kernel_wait_quiescent(kernel_tracer_fd
);
2147 /* TODO: Userspace tracing */
2148 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2154 case LTTNG_ENABLE_ALL_EVENT
:
2158 struct ltt_kernel_channel
*kchan
;
2159 struct ltt_kernel_event
*kevent
;
2160 struct lttng_event
*event_list
;
2161 struct lttng_channel
*chan
;
2163 /* Setup lttng message with no payload */
2164 ret
= setup_lttng_msg(cmd_ctx
, 0);
2169 DBG("Enabling all kernel event");
2171 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2173 switch (cmd_ctx
->lsm
->domain
.type
) {
2174 case LTTNG_DOMAIN_KERNEL
:
2175 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2176 cmd_ctx
->session
->kernel_session
);
2177 if (kchan
== NULL
) {
2178 DBG("Channel not found. Creating channel %s", channel_name
);
2180 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2182 ret
= LTTCOMM_FATAL
;
2186 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2187 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2189 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2192 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2193 cmd_ctx
->session
->kernel_session
);
2194 if (kchan
== NULL
) {
2195 ERR("Channel %s not found after creation. Internal error, giving up.",
2197 ret
= LTTCOMM_FATAL
;
2202 /* For each event in the kernel session */
2203 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
2204 DBG("Enabling kernel event %s for channel %s.",
2205 kevent
->event
->name
, channel_name
);
2206 ret
= kernel_enable_event(kevent
);
2212 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
2214 ret
= LTTCOMM_KERN_LIST_FAIL
;
2218 for (i
= 0; i
< size
; i
++) {
2219 kevent
= trace_kernel_get_event_by_name(event_list
[i
].name
, kchan
);
2220 if (kevent
== NULL
) {
2221 /* Default event type for enable all */
2222 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2223 /* Enable each single tracepoint event */
2224 ret
= kernel_create_event(&event_list
[i
], kchan
);
2226 /* Ignore error here and continue */
2233 /* Quiescent wait after event enable */
2234 kernel_wait_quiescent(kernel_tracer_fd
);
2237 /* TODO: Userspace tracing */
2238 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2245 case LTTNG_LIST_TRACEPOINTS
:
2247 struct lttng_event
*events
;
2248 ssize_t nb_events
= 0;
2250 switch (cmd_ctx
->lsm
->domain
.type
) {
2251 case LTTNG_DOMAIN_KERNEL
:
2252 DBG("Listing kernel events");
2253 nb_events
= kernel_list_events(kernel_tracer_fd
, &events
);
2254 if (nb_events
< 0) {
2255 ret
= LTTCOMM_KERN_LIST_FAIL
;
2260 /* TODO: Userspace listing */
2261 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2266 * Setup lttng message with payload size set to the event list size in
2267 * bytes and then copy list into the llm payload.
2269 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2275 /* Copy event list into message payload */
2276 memcpy(cmd_ctx
->llm
->payload
, events
,
2277 sizeof(struct lttng_event
) * nb_events
);
2284 case LTTNG_START_TRACE
:
2286 struct ltt_kernel_channel
*chan
;
2288 /* Setup lttng message with no payload */
2289 ret
= setup_lttng_msg(cmd_ctx
, 0);
2294 /* Kernel tracing */
2295 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2296 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
2297 DBG("Open kernel metadata");
2298 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
2299 cmd_ctx
->session
->kernel_session
->trace_path
);
2301 ret
= LTTCOMM_KERN_META_FAIL
;
2306 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2307 DBG("Opening kernel metadata stream");
2308 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2309 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
2311 ERR("Kernel create metadata stream failed");
2312 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2318 /* For each channel */
2319 cds_list_for_each_entry(chan
,
2320 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2321 if (chan
->stream_count
== 0) {
2322 ret
= kernel_open_channel_stream(chan
);
2324 ERR("Kernel create channel stream failed");
2325 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2328 /* Update the stream global counter */
2329 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
2333 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
2335 ret
= LTTCOMM_KERN_START_FAIL
;
2339 DBG("Start kernel tracing");
2340 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
2342 ERR("Kernel start session failed");
2343 ret
= LTTCOMM_KERN_START_FAIL
;
2347 /* Quiescent wait after starting trace */
2348 kernel_wait_quiescent(kernel_tracer_fd
);
2351 /* TODO: Start all UST traces */
2356 case LTTNG_STOP_TRACE
:
2358 struct ltt_kernel_channel
*chan
;
2359 /* Setup lttng message with no payload */
2360 ret
= setup_lttng_msg(cmd_ctx
, 0);
2366 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2367 DBG("Stop kernel tracing");
2369 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
2371 ERR("Kernel metadata flush failed");
2374 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2375 ret
= kernel_flush_buffer(chan
);
2377 ERR("Kernel flush buffer error");
2381 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
2383 ERR("Kernel stop session failed");
2384 ret
= LTTCOMM_KERN_STOP_FAIL
;
2388 /* Quiescent wait after stopping trace */
2389 kernel_wait_quiescent(kernel_tracer_fd
);
2392 /* TODO : User-space tracer */
2397 case LTTNG_CREATE_SESSION
:
2399 /* Setup lttng message with no payload */
2400 ret
= setup_lttng_msg(cmd_ctx
, 0);
2405 tracepoint(create_session_start
);
2406 ret
= create_session(cmd_ctx
->lsm
->session
.name
, cmd_ctx
->lsm
->session
.path
);
2407 tracepoint(create_session_end
);
2409 if (ret
== -EEXIST
) {
2410 ret
= LTTCOMM_EXIST_SESS
;
2412 ret
= LTTCOMM_FATAL
;
2420 case LTTNG_DESTROY_SESSION
:
2422 /* Setup lttng message with no payload */
2423 ret
= setup_lttng_msg(cmd_ctx
, 0);
2428 /* Clean kernel session teardown */
2429 teardown_kernel_session(cmd_ctx
->session
);
2431 tracepoint(destroy_session_start
);
2432 ret
= destroy_session(cmd_ctx
->lsm
->session
.name
);
2433 tracepoint(destroy_session_end
);
2435 ret
= LTTCOMM_FATAL
;
2440 * Must notify the kernel thread here to update it's pollfd in order to
2441 * remove the channel(s)' fd just destroyed.
2443 ret
= notify_kernel_pollfd();
2445 ret
= LTTCOMM_FATAL
;
2452 case LTTNG_LIST_DOMAINS
:
2456 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2460 nb_dom
+= cmd_ctx
->session
->ust_session_list
.count
;
2462 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_domain
) * nb_dom
);
2467 ((struct lttng_domain
*)(cmd_ctx
->llm
->payload
))[0].type
=
2468 LTTNG_DOMAIN_KERNEL
;
2470 /* TODO: User-space tracer domain support */
2474 case LTTNG_LIST_CHANNELS
:
2477 * TODO: Only kernel channels are listed here. UST listing
2478 * is needed on lttng-ust 2.0 release.
2481 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2482 nb_chan
+= cmd_ctx
->session
->kernel_session
->channel_count
;
2485 ret
= setup_lttng_msg(cmd_ctx
,
2486 sizeof(struct lttng_channel
) * nb_chan
);
2491 list_lttng_channels(cmd_ctx
->session
,
2492 (struct lttng_channel
*)(cmd_ctx
->llm
->payload
));
2497 case LTTNG_LIST_EVENTS
:
2500 * TODO: Only kernel events are listed here. UST listing
2501 * is needed on lttng-ust 2.0 release.
2503 size_t nb_event
= 0;
2504 struct ltt_kernel_channel
*kchan
= NULL
;
2506 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2507 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.list
.channel_name
,
2508 cmd_ctx
->session
->kernel_session
);
2509 if (kchan
== NULL
) {
2510 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2513 nb_event
+= kchan
->event_count
;
2516 ret
= setup_lttng_msg(cmd_ctx
,
2517 sizeof(struct lttng_event
) * nb_event
);
2522 DBG("Listing events (%zu events)", nb_event
);
2524 list_lttng_events(kchan
,
2525 (struct lttng_event
*)(cmd_ctx
->llm
->payload
));
2530 case LTTNG_LIST_SESSIONS
:
2532 lock_session_list();
2534 if (session_list_ptr
->count
== 0) {
2535 ret
= LTTCOMM_NO_SESSION
;
2536 unlock_session_list();
2540 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
2541 session_list_ptr
->count
);
2543 unlock_session_list();
2547 /* Filled the session array */
2548 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
2550 unlock_session_list();
2555 case LTTNG_CALIBRATE
:
2557 /* Setup lttng message with no payload */
2558 ret
= setup_lttng_msg(cmd_ctx
, 0);
2563 switch (cmd_ctx
->lsm
->domain
.type
) {
2564 case LTTNG_DOMAIN_KERNEL
:
2566 struct lttng_kernel_calibrate kcalibrate
;
2568 kcalibrate
.type
= cmd_ctx
->lsm
->u
.calibrate
.type
;
2569 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2571 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2577 /* TODO: Userspace tracing */
2578 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2584 case LTTNG_REGISTER_CONSUMER
:
2588 /* Setup lttng message with no payload */
2589 ret
= setup_lttng_msg(cmd_ctx
, 0);
2594 switch (cmd_ctx
->lsm
->domain
.type
) {
2595 case LTTNG_DOMAIN_KERNEL
:
2597 /* Can't register a consumer if there is already one */
2598 if (cmd_ctx
->session
->kernel_session
->consumer_fd
!= 0) {
2599 ret
= LTTCOMM_CONNECT_FAIL
;
2603 sock
= lttcomm_connect_unix_sock(cmd_ctx
->lsm
->u
.reg
.path
);
2605 ret
= LTTCOMM_CONNECT_FAIL
;
2609 cmd_ctx
->session
->kernel_session
->consumer_fd
= sock
;
2613 /* TODO: Userspace tracing */
2614 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2623 /* Undefined command */
2624 ret
= setup_lttng_msg(cmd_ctx
, 0);
2633 /* Set return code */
2634 cmd_ctx
->llm
->ret_code
= ret
;
2636 if (cmd_ctx
->session
) {
2637 unlock_session(cmd_ctx
->session
);
2643 if (cmd_ctx
->llm
== NULL
) {
2644 DBG("Missing llm structure. Allocating one.");
2645 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2649 /* Notify client of error */
2650 cmd_ctx
->llm
->ret_code
= ret
;
2653 if (cmd_ctx
->session
) {
2654 unlock_session(cmd_ctx
->session
);
2660 * This thread manage all clients request using the unix client socket for
2663 static void *thread_manage_clients(void *data
)
2666 struct command_ctx
*cmd_ctx
= NULL
;
2667 struct pollfd pollfd
[2];
2669 tracepoint(sessiond_th_cli_start
);
2671 DBG("[thread] Manage client started");
2673 ret
= lttcomm_listen_unix_sock(client_sock
);
2678 /* First fd is always the quit pipe */
2679 pollfd
[0].fd
= thread_quit_pipe
[0];
2682 pollfd
[1].fd
= client_sock
;
2683 pollfd
[1].events
= POLLIN
;
2685 /* Notify parent pid that we are ready
2686 * to accept command for client side.
2688 if (opt_sig_parent
) {
2689 kill(ppid
, SIGCHLD
);
2693 DBG("Accepting client command ...");
2695 tracepoint(sessiond_th_cli_poll
);
2697 /* Inifinite blocking call, waiting for transmission */
2698 ret
= poll(pollfd
, 2, -1);
2700 perror("poll client thread");
2704 /* Thread quit pipe has been closed. Killing thread. */
2705 if (pollfd
[0].revents
== POLLNVAL
) {
2707 } else if (pollfd
[1].revents
== POLLERR
) {
2708 ERR("Client socket poll error");
2712 sock
= lttcomm_accept_unix_sock(client_sock
);
2717 /* Allocate context command to process the client request */
2718 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2720 /* Allocate data buffer for reception */
2721 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2722 cmd_ctx
->llm
= NULL
;
2723 cmd_ctx
->session
= NULL
;
2726 * Data is received from the lttng client. The struct
2727 * lttcomm_session_msg (lsm) contains the command and data request of
2730 DBG("Receiving data from client ...");
2731 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2736 // TODO: Validate cmd_ctx including sanity check for security purpose.
2739 * This function dispatch the work to the kernel or userspace tracer
2740 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2741 * informations for the client. The command context struct contains
2742 * everything this function may needs.
2744 ret
= process_client_msg(cmd_ctx
);
2746 /* TODO: Inform client somehow of the fatal error. At this point,
2747 * ret < 0 means that a malloc failed (ENOMEM). */
2748 /* Error detected but still accept command */
2749 clean_command_ctx(&cmd_ctx
);
2753 DBG("Sending response (size: %d, retcode: %d)",
2754 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2755 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2757 ERR("Failed to send data back to client");
2760 clean_command_ctx(&cmd_ctx
);
2762 /* End of transmission */
2767 DBG("Client thread dying");
2775 unlink(client_unix_sock_path
);
2777 clean_command_ctx(&cmd_ctx
);
2783 * usage function on stderr
2785 static void usage(void)
2787 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2788 fprintf(stderr
, " -h, --help Display this usage.\n");
2789 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2790 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2791 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2792 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2793 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2794 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2795 fprintf(stderr
, " -V, --version Show version number.\n");
2796 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2797 fprintf(stderr
, " -q, --quiet No output at all.\n");
2798 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2799 fprintf(stderr
, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2803 * daemon argument parsing
2805 static int parse_args(int argc
, char **argv
)
2809 static struct option long_options
[] = {
2810 { "client-sock", 1, 0, 'c' },
2811 { "apps-sock", 1, 0, 'a' },
2812 { "kconsumerd-cmd-sock", 1, 0, 0 },
2813 { "kconsumerd-err-sock", 1, 0, 0 },
2814 { "daemonize", 0, 0, 'd' },
2815 { "sig-parent", 0, 0, 'S' },
2816 { "help", 0, 0, 'h' },
2817 { "group", 1, 0, 'g' },
2818 { "version", 0, 0, 'V' },
2819 { "quiet", 0, 0, 'q' },
2820 { "verbose", 0, 0, 'v' },
2821 { "verbose-kconsumerd", 0, 0, 'Z' },
2826 int option_index
= 0;
2827 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:Z", long_options
, &option_index
);
2834 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2836 fprintf(stderr
, " with arg %s\n", optarg
);
2840 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2843 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2849 opt_tracing_group
= strdup(optarg
);
2855 fprintf(stdout
, "%s\n", VERSION
);
2861 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2864 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2870 /* Verbose level can increase using multiple -v */
2874 opt_verbose_kconsumerd
+= 1;
2877 /* Unknown option or other error.
2878 * Error is printed by getopt, just return */
2887 * Creates the two needed socket by the daemon.
2888 * apps_sock - The communication socket for all UST apps.
2889 * client_sock - The communication of the cli tool (lttng).
2891 static int init_daemon_socket(void)
2896 old_umask
= umask(0);
2898 /* Create client tool unix socket */
2899 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2900 if (client_sock
< 0) {
2901 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2906 /* File permission MUST be 660 */
2907 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2909 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2914 /* Create the application unix socket */
2915 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2916 if (apps_sock
< 0) {
2917 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2922 /* File permission MUST be 666 */
2923 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2925 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2936 * Check if the global socket is available, and if a daemon is answering
2937 * at the other side. If yes, error is returned.
2939 static int check_existing_daemon(void)
2941 if (access(client_unix_sock_path
, F_OK
) < 0 &&
2942 access(apps_unix_sock_path
, F_OK
) < 0) {
2945 /* Is there anybody out there ? */
2946 if (lttng_session_daemon_alive()) {
2954 * Set the tracing group gid onto the client socket.
2956 * Race window between mkdir and chown is OK because we are going from more
2957 * permissive (root.root) to les permissive (root.tracing).
2959 static int set_permissions(void)
2964 gid
= allowed_group();
2967 WARN("No tracing group detected");
2970 ERR("Missing tracing group. Aborting execution.");
2976 /* Set lttng run dir */
2977 ret
= chown(LTTNG_RUNDIR
, 0, gid
);
2979 ERR("Unable to set group on " LTTNG_RUNDIR
);
2983 /* lttng client socket path */
2984 ret
= chown(client_unix_sock_path
, 0, gid
);
2986 ERR("Unable to set group on %s", client_unix_sock_path
);
2990 /* kconsumerd error socket path */
2991 ret
= chown(kconsumerd_err_unix_sock_path
, 0, gid
);
2993 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2997 DBG("All permissions are set");
3004 * Create the pipe used to wake up the kernel thread.
3006 static int create_kernel_poll_pipe(void)
3008 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
3012 * Create the application command pipe to wake thread_manage_apps.
3014 static int create_apps_cmd_pipe(void)
3016 return pipe2(apps_cmd_pipe
, O_CLOEXEC
);
3020 * Create the lttng run directory needed for all global sockets and pipe.
3022 static int create_lttng_rundir(void)
3026 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
3028 if (errno
!= EEXIST
) {
3029 ERR("Unable to create " LTTNG_RUNDIR
);
3041 * Setup sockets and directory needed by the kconsumerd communication with the
3044 static int set_kconsumerd_sockets(void)
3048 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
3049 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
3052 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
3053 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
3056 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
3058 if (errno
!= EEXIST
) {
3059 ERR("Failed to create " KCONSUMERD_PATH
);
3065 /* Create the kconsumerd error unix socket */
3066 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
3067 if (kconsumerd_err_sock
< 0) {
3068 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
3073 /* File permission MUST be 660 */
3074 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3076 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
3086 * Signal handler for the daemon
3088 * Simply stop all worker threads, leaving main() return gracefully
3089 * after joining all threads and calling cleanup().
3091 static void sighandler(int sig
)
3095 DBG("SIGPIPE catched");
3098 DBG("SIGINT catched");
3102 DBG("SIGTERM catched");
3111 * Setup signal handler for :
3112 * SIGINT, SIGTERM, SIGPIPE
3114 static int set_signal_handler(void)
3117 struct sigaction sa
;
3120 if ((ret
= sigemptyset(&sigset
)) < 0) {
3121 perror("sigemptyset");
3125 sa
.sa_handler
= sighandler
;
3126 sa
.sa_mask
= sigset
;
3128 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3129 perror("sigaction");
3133 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3134 perror("sigaction");
3138 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3139 perror("sigaction");
3143 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3149 * Set open files limit to unlimited. This daemon can open a large number of
3150 * file descriptors in order to consumer multiple kernel traces.
3152 static void set_ulimit(void)
3157 /* The kernel does not allowed an infinite limit for open files */
3158 lim
.rlim_cur
= 65535;
3159 lim
.rlim_max
= 65535;
3161 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3163 perror("failed to set open files limit");
3170 int main(int argc
, char **argv
)
3174 const char *home_path
;
3176 tracepoint(sessiond_boot_start
);
3178 /* Create thread quit pipe */
3179 if ((ret
= init_thread_quit_pipe()) < 0) {
3183 /* Parse arguments */
3185 if ((ret
= parse_args(argc
, argv
) < 0)) {
3198 /* Check if daemon is UID = 0 */
3199 is_root
= !getuid();
3202 ret
= create_lttng_rundir();
3207 if (strlen(apps_unix_sock_path
) == 0) {
3208 snprintf(apps_unix_sock_path
, PATH_MAX
,
3209 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
3212 if (strlen(client_unix_sock_path
) == 0) {
3213 snprintf(client_unix_sock_path
, PATH_MAX
,
3214 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
3217 /* Set global SHM for ust */
3218 if (strlen(wait_shm_path
) == 0) {
3219 snprintf(wait_shm_path
, PATH_MAX
,
3220 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
3223 home_path
= get_home_dir();
3224 if (home_path
== NULL
) {
3225 /* TODO: Add --socket PATH option */
3226 ERR("Can't get HOME directory for sockets creation.");
3231 if (strlen(apps_unix_sock_path
) == 0) {
3232 snprintf(apps_unix_sock_path
, PATH_MAX
,
3233 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
3236 /* Set the cli tool unix socket path */
3237 if (strlen(client_unix_sock_path
) == 0) {
3238 snprintf(client_unix_sock_path
, PATH_MAX
,
3239 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
3242 /* Set global SHM for ust */
3243 if (strlen(wait_shm_path
) == 0) {
3244 snprintf(wait_shm_path
, PATH_MAX
,
3245 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, geteuid());
3249 DBG("Client socket path %s", client_unix_sock_path
);
3250 DBG("Application socket path %s", apps_unix_sock_path
);
3253 * See if daemon already exist.
3255 if ((ret
= check_existing_daemon()) < 0) {
3256 ERR("Already running daemon.\n");
3258 * We do not goto exit because we must not cleanup()
3259 * because a daemon is already running.
3264 /* After this point, we can safely call cleanup() so goto error is used */
3267 * These actions must be executed as root. We do that *after* setting up
3268 * the sockets path because we MUST make the check for another daemon using
3269 * those paths *before* trying to set the kernel consumer sockets and init
3273 ret
= set_kconsumerd_sockets();
3278 /* Setup kernel tracer */
3279 init_kernel_tracer();
3281 /* Set ulimit for open files */
3285 if ((ret
= set_signal_handler()) < 0) {
3289 /* Setup the needed unix socket */
3290 if ((ret
= init_daemon_socket()) < 0) {
3294 /* Set credentials to socket */
3295 if (is_root
&& ((ret
= set_permissions()) < 0)) {
3299 /* Get parent pid if -S, --sig-parent is specified. */
3300 if (opt_sig_parent
) {
3304 /* Setup the kernel pipe for waking up the kernel thread */
3305 if ((ret
= create_kernel_poll_pipe()) < 0) {
3309 /* Setup the thread apps communication pipe. */
3310 if ((ret
= create_apps_cmd_pipe()) < 0) {
3314 /* Init UST command queue. */
3315 cds_wfq_init(&ust_cmd_queue
.queue
);
3318 * Get session list pointer. This pointer MUST NOT be free().
3319 * This list is statically declared in session.c
3321 session_list_ptr
= get_session_list();
3323 /* Create thread to manage the client socket */
3324 ret
= pthread_create(&client_thread
, NULL
,
3325 thread_manage_clients
, (void *) NULL
);
3327 perror("pthread_create clients");
3331 /* Create thread to dispatch registration */
3332 ret
= pthread_create(&dispatch_thread
, NULL
,
3333 thread_dispatch_ust_registration
, (void *) NULL
);
3335 perror("pthread_create dispatch");
3339 /* Create thread to manage application registration. */
3340 ret
= pthread_create(®_apps_thread
, NULL
,
3341 thread_registration_apps
, (void *) NULL
);
3343 perror("pthread_create registration");
3347 /* Create thread to manage application socket */
3348 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
3350 perror("pthread_create apps");
3354 /* Create kernel thread to manage kernel event */
3355 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
3357 perror("pthread_create kernel");
3361 tracepoint(sessiond_boot_end
);
3363 ret
= pthread_join(kernel_thread
, &status
);
3365 perror("pthread_join");
3366 goto error
; /* join error, exit without cleanup */
3370 ret
= pthread_join(apps_thread
, &status
);
3372 perror("pthread_join");
3373 goto error
; /* join error, exit without cleanup */
3377 ret
= pthread_join(reg_apps_thread
, &status
);
3379 perror("pthread_join");
3380 goto error
; /* join error, exit without cleanup */
3384 ret
= pthread_join(dispatch_thread
, &status
);
3386 perror("pthread_join");
3387 goto error
; /* join error, exit without cleanup */
3391 ret
= pthread_join(client_thread
, &status
);
3393 perror("pthread_join");
3394 goto error
; /* join error, exit without cleanup */
3397 ret
= join_kconsumerd_thread();
3399 perror("join_kconsumerd");
3400 goto error
; /* join error, exit without cleanup */
3406 * cleanup() is called when no other thread is running.