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 DBG("[thread] Manage application started");
945 /* See if we have a valid socket to add to pollfd */
946 if (ust_cmd
.sock
!= -1) {
948 update_poll_flag
= 1;
951 /* The pollfd struct must be updated */
952 if (update_poll_flag
) {
953 ret
= update_apps_cmd_pollfd(nb_fd
, ARRAY_SIZE(pollfd
), &pollfd
);
955 /* malloc failed so we quit */
959 if (ust_cmd
.sock
!= -1) {
960 /* Update pollfd with the new UST socket */
961 DBG("Adding sock %d to apps cmd pollfd", ust_cmd
.sock
);
962 pollfd
[nb_fd
- 1].fd
= ust_cmd
.sock
;
963 pollfd
[nb_fd
- 1].events
= POLLHUP
| POLLNVAL
| POLLERR
;
968 DBG("Apps thread polling on %d fds", nb_fd
);
970 /* Inifinite blocking call, waiting for transmission */
971 ret
= poll(pollfd
, nb_fd
, -1);
973 perror("poll apps thread");
977 /* Thread quit pipe has been closed. Killing thread. */
978 if (pollfd
[0].revents
== POLLNVAL
) {
981 /* apps_cmd_pipe pipe events */
982 switch (pollfd
[1].revents
) {
984 ERR("Apps command pipe poll error");
987 tracepoint(ust_manage_register_start
);
990 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
991 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
992 perror("read apps cmd pipe");
996 /* Register applicaton to the session daemon */
997 ret
= register_traceable_app(&ust_cmd
.reg_msg
, ust_cmd
.sock
);
999 /* Only critical ENOMEM error can be returned here */
1003 ret
= ustctl_register_done(ust_cmd
.sock
);
1006 * If the registration is not possible, we simply unregister
1007 * the apps and continue
1009 unregister_traceable_app(ust_cmd
.sock
);
1012 tracepoint(ust_manage_register_stop
);
1018 for (i
= 2; i
< count
; i
++) {
1019 /* Apps socket is closed/hungup */
1020 switch (pollfd
[i
].revents
) {
1025 unregister_traceable_app(pollfd
[i
].fd
);
1026 /* Indicate to remove this fd from the pollfd */
1033 if (nb_fd
!= count
) {
1034 update_poll_flag
= 1;
1039 DBG("Application communication apps dying");
1040 close(apps_cmd_pipe
[0]);
1041 close(apps_cmd_pipe
[1]);
1049 * Dispatch request from the registration threads to the application
1050 * communication thread.
1052 static void *thread_dispatch_ust_registration(void *data
)
1055 struct cds_wfq_node
*node
;
1056 struct ust_command
*ust_cmd
= NULL
;
1058 DBG("[thread] Dispatch UST command started");
1060 while (!dispatch_thread_exit
) {
1061 /* Atomically prepare the queue futex */
1062 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1065 /* Dequeue command for registration */
1066 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1068 DBG("Waked up but nothing in the UST command queue");
1069 /* Continue thread execution */
1073 tracepoint(ust_dispatch_register_start
);
1075 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1077 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1078 " gid:%d sock:%d name:%s (version %d.%d)",
1079 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1080 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1081 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1082 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1084 * Inform apps thread of the new application registration. This
1085 * call is blocking so we can be assured that the data will be read
1086 * at some point in time or wait to the end of the world :)
1088 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1089 sizeof(struct ust_command
));
1091 perror("write apps cmd pipe");
1092 if (errno
== EBADF
) {
1094 * We can't inform the application thread to process
1095 * registration. We will exit or else application
1096 * registration will not occur and tracing will never
1103 } while (node
!= NULL
);
1105 tracepoint(ust_dispatch_register_stop
);
1107 /* Futex wait on queue. Blocking call on futex() */
1108 futex_nto1_wait(&ust_cmd_queue
.futex
);
1112 DBG("Dispatch thread dying");
1117 * This thread manage application registration.
1119 static void *thread_registration_apps(void *data
)
1122 struct pollfd pollfd
[2];
1124 * Get allocated in this thread, enqueued to a global queue, dequeued and
1125 * freed in the manage apps thread.
1127 struct ust_command
*ust_cmd
= NULL
;
1129 tracepoint(sessiond_th_reg_start
);
1131 DBG("[thread] Manage application registration started");
1133 ret
= lttcomm_listen_unix_sock(apps_sock
);
1138 /* First fd is always the quit pipe */
1139 pollfd
[0].fd
= thread_quit_pipe
[0];
1142 pollfd
[1].fd
= apps_sock
;
1143 pollfd
[1].events
= POLLIN
;
1145 /* Notify all applications to register */
1146 ret
= notify_ust_apps(1);
1148 ERR("Failed to notify applications or create the wait shared memory.\n"
1149 "Execution continues but there might be problem for already running\n"
1150 "applications that wishes to register.");
1154 DBG("Accepting application registration");
1156 tracepoint(sessiond_th_reg_poll
);
1158 /* Inifinite blocking call, waiting for transmission */
1159 ret
= poll(pollfd
, 2, -1);
1161 perror("poll register apps thread");
1165 /* Thread quit pipe has been closed. Killing thread. */
1166 if (pollfd
[0].revents
== POLLNVAL
) {
1168 } else if (pollfd
[1].revents
== POLLERR
) {
1169 ERR("Register apps socket poll error");
1173 /* Registration starts here. Recording cycles */
1174 tracepoint(ust_register_start
);
1176 sock
= lttcomm_accept_unix_sock(apps_sock
);
1181 /* Create UST registration command for enqueuing */
1182 ust_cmd
= malloc(sizeof(struct ust_command
));
1183 if (ust_cmd
== NULL
) {
1184 perror("ust command malloc");
1189 * Using message-based transmissions to ensure we don't have to deal
1190 * with partially received messages.
1192 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1193 sizeof(struct ust_register_msg
));
1194 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1196 perror("lttcomm_recv_unix_sock register apps");
1198 ERR("Wrong size received on apps register");
1205 ust_cmd
->sock
= sock
;
1207 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1208 " gid:%d sock:%d name:%s (version %d.%d)",
1209 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1210 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1211 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1212 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1214 * Lock free enqueue the registration request.
1215 * The red pill has been taken! This apps will be part of the *system*
1217 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1220 * Wake the registration queue futex.
1221 * Implicit memory barrier with the exchange in cds_wfq_enqueue.
1223 futex_nto1_wake(&ust_cmd_queue
.futex
);
1225 tracepoint(ust_register_stop
);
1229 DBG("UST Registration thread dying");
1231 /* Notify that the registration thread is gone */
1237 unlink(apps_unix_sock_path
);
1243 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
1244 * exec or it will fails.
1246 static int spawn_kconsumerd_thread(void)
1250 /* Setup semaphore */
1251 sem_init(&kconsumerd_sem
, 0, 0);
1253 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
1255 perror("pthread_create kconsumerd");
1259 /* Wait for the kconsumerd thread to be ready */
1260 sem_wait(&kconsumerd_sem
);
1262 if (kconsumerd_pid
== 0) {
1263 ERR("Kconsumerd did not start");
1270 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1275 * Join kernel consumer thread
1277 static int join_kconsumerd_thread(void)
1282 if (kconsumerd_pid
!= 0) {
1283 ret
= kill(kconsumerd_pid
, SIGTERM
);
1285 ERR("Error killing kconsumerd");
1288 return pthread_join(kconsumerd_thread
, &status
);
1295 * Fork and exec a kernel consumer daemon (kconsumerd).
1297 * Return pid if successful else -1.
1299 static pid_t
spawn_kconsumerd(void)
1303 const char *verbosity
;
1305 DBG("Spawning kconsumerd");
1312 if (opt_verbose
> 1 || opt_verbose_kconsumerd
) {
1313 verbosity
= "--verbose";
1315 verbosity
= "--quiet";
1317 execl(INSTALL_BIN_PATH
"/ltt-kconsumerd", "ltt-kconsumerd", verbosity
, NULL
);
1319 perror("kernel start consumer exec");
1322 } else if (pid
> 0) {
1326 perror("kernel start consumer fork");
1336 * Spawn the kconsumerd daemon and session daemon thread.
1338 static int start_kconsumerd(void)
1342 pthread_mutex_lock(&kconsumerd_pid_mutex
);
1343 if (kconsumerd_pid
!= 0) {
1344 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1348 ret
= spawn_kconsumerd();
1350 ERR("Spawning kconsumerd failed");
1351 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1352 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1356 /* Setting up the global kconsumerd_pid */
1357 kconsumerd_pid
= ret
;
1358 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1360 DBG("Kconsumerd pid %d", ret
);
1362 DBG("Spawning kconsumerd thread");
1363 ret
= spawn_kconsumerd_thread();
1365 ERR("Fatal error spawning kconsumerd thread");
1377 * modprobe_kernel_modules
1379 static int modprobe_kernel_modules(void)
1384 for (i
= 0; i
< ARRAY_SIZE(kernel_modules_list
); i
++) {
1385 ret
= snprintf(modprobe
, sizeof(modprobe
),
1386 "/sbin/modprobe %s%s",
1387 kernel_modules_list
[i
].required
? "" : "--quiet ",
1388 kernel_modules_list
[i
].name
);
1390 perror("snprintf modprobe");
1393 modprobe
[sizeof(modprobe
) - 1] = '\0';
1394 ret
= system(modprobe
);
1396 ERR("Unable to launch modprobe for module %s",
1397 kernel_modules_list
[i
].name
);
1398 } else if (kernel_modules_list
[i
].required
1399 && WEXITSTATUS(ret
) != 0) {
1400 ERR("Unable to load module %s",
1401 kernel_modules_list
[i
].name
);
1403 DBG("Modprobe successfully %s",
1404 kernel_modules_list
[i
].name
);
1415 static int mount_debugfs(char *path
)
1418 char *type
= "debugfs";
1420 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, geteuid(), getegid());
1425 ret
= mount(type
, path
, type
, 0, NULL
);
1427 perror("mount debugfs");
1431 DBG("Mounted debugfs successfully at %s", path
);
1438 * Setup necessary data for kernel tracer action.
1440 static void init_kernel_tracer(void)
1443 char *proc_mounts
= "/proc/mounts";
1445 char *debugfs_path
= NULL
, *lttng_path
;
1448 /* Detect debugfs */
1449 fp
= fopen(proc_mounts
, "r");
1451 ERR("Unable to probe %s", proc_mounts
);
1455 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1456 if (strstr(line
, "debugfs") != NULL
) {
1457 /* Remove first string */
1459 /* Dup string here so we can reuse line later on */
1460 debugfs_path
= strdup(strtok(NULL
, " "));
1461 DBG("Got debugfs path : %s", debugfs_path
);
1468 /* Mount debugfs if needded */
1469 if (debugfs_path
== NULL
) {
1470 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1472 perror("asprintf debugfs path");
1475 ret
= mount_debugfs(debugfs_path
);
1481 /* Modprobe lttng kernel modules */
1482 ret
= modprobe_kernel_modules();
1487 /* Setup lttng kernel path */
1488 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1490 perror("asprintf lttng path");
1494 /* Open debugfs lttng */
1495 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1496 if (kernel_tracer_fd
< 0) {
1497 DBG("Failed to open %s", lttng_path
);
1503 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1513 WARN("No kernel tracer available");
1514 kernel_tracer_fd
= 0;
1519 * Start tracing by creating trace directory and sending FDs to the kernel
1522 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1526 if (session
->kconsumer_fds_sent
== 0) {
1528 * Assign default kernel consumer if no consumer assigned to the kernel
1529 * session. At this point, it's NOT suppose to be 0 but this is an extra
1532 if (session
->consumer_fd
== 0) {
1533 session
->consumer_fd
= kconsumerd_cmd_sock
;
1536 ret
= send_kconsumerd_fds(session
);
1538 ERR("Send kconsumerd fds failed");
1539 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1543 session
->kconsumer_fds_sent
= 1;
1551 * Notify kernel thread to update it's pollfd.
1553 static int notify_kernel_pollfd(void)
1557 /* Inform kernel thread of the new kernel channel */
1558 ret
= write(kernel_poll_pipe
[1], "!", 1);
1560 perror("write kernel poll pipe");
1567 * Allocate a channel structure and fill it.
1569 static struct lttng_channel
*init_default_channel(enum lttng_domain_type domain_type
,
1572 struct lttng_channel
*chan
;
1574 chan
= malloc(sizeof(struct lttng_channel
));
1576 perror("init channel malloc");
1580 if (snprintf(chan
->name
, NAME_MAX
, "%s", name
) < 0) {
1581 perror("snprintf channel name");
1585 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1586 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1587 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1589 switch (domain_type
) {
1590 case LTTNG_DOMAIN_KERNEL
:
1591 chan
->attr
.subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
1592 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1593 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1597 goto error
; /* Not implemented */
1608 * Create an UST session and add it to the session ust list.
1610 static int create_ust_session(pid_t pid
, struct ltt_session
*session
)
1613 struct ltt_ust_session
*lus
;
1615 DBG("Creating UST session");
1617 lus
= trace_ust_create_session(session
->path
, pid
);
1622 ret
= mkdir_recursive(lus
->path
, S_IRWXU
| S_IRWXG
,
1623 geteuid(), allowed_group());
1625 if (ret
!= -EEXIST
) {
1626 ERR("Trace directory creation error");
1631 /* Create session on the UST tracer */
1632 ret
= ustctl_create_session(lus
);
1645 * Create a kernel tracer session then create the default channel.
1647 static int create_kernel_session(struct ltt_session
*session
)
1651 DBG("Creating kernel session");
1653 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1655 ret
= LTTCOMM_KERN_SESS_FAIL
;
1659 /* Set kernel consumer socket fd */
1660 if (kconsumerd_cmd_sock
) {
1661 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
1664 ret
= mkdir_recursive(session
->kernel_session
->trace_path
,
1665 S_IRWXU
| S_IRWXG
, geteuid(), allowed_group());
1667 if (ret
!= -EEXIST
) {
1668 ERR("Trace directory creation error");
1678 * Using the session list, filled a lttng_session array to send back to the
1679 * client for session listing.
1681 * The session list lock MUST be acquired before calling this function. Use
1682 * lock_session_list() and unlock_session_list().
1684 static void list_lttng_sessions(struct lttng_session
*sessions
)
1687 struct ltt_session
*session
;
1689 DBG("Getting all available session");
1691 * Iterate over session list and append data after the control struct in
1694 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1695 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1696 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
1697 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1698 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
1704 * Fill lttng_channel array of all channels.
1706 static void list_lttng_channels(struct ltt_session
*session
,
1707 struct lttng_channel
*channels
)
1710 struct ltt_kernel_channel
*kchan
;
1712 DBG("Listing channels for session %s", session
->name
);
1714 /* Kernel channels */
1715 if (session
->kernel_session
!= NULL
) {
1716 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
1717 /* Copy lttng_channel struct to array */
1718 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1719 channels
[i
].enabled
= kchan
->enabled
;
1724 /* TODO: Missing UST listing */
1728 * Fill lttng_event array of all events in the channel.
1730 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1731 struct lttng_event
*events
)
1734 * TODO: This is ONLY kernel. Need UST support.
1737 struct ltt_kernel_event
*event
;
1739 DBG("Listing events for channel %s", kchan
->channel
->name
);
1741 /* Kernel channels */
1742 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1743 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1744 events
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1745 events
[i
].enabled
= event
->enabled
;
1746 switch (event
->event
->instrumentation
) {
1747 case LTTNG_KERNEL_TRACEPOINT
:
1748 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1750 case LTTNG_KERNEL_KPROBE
:
1751 case LTTNG_KERNEL_KRETPROBE
:
1752 events
[i
].type
= LTTNG_EVENT_PROBE
;
1753 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
1754 sizeof(struct lttng_kernel_kprobe
));
1756 case LTTNG_KERNEL_FUNCTION
:
1757 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
1758 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
1759 sizeof(struct lttng_kernel_function
));
1767 * Process the command requested by the lttng client within the command
1768 * context structure. This function make sure that the return structure (llm)
1769 * is set and ready for transmission before returning.
1771 * Return any error encountered or 0 for success.
1773 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1775 int ret
= LTTCOMM_OK
;
1777 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1780 * Commands that DO NOT need a session.
1782 switch (cmd_ctx
->lsm
->cmd_type
) {
1783 case LTTNG_CREATE_SESSION
:
1784 case LTTNG_LIST_SESSIONS
:
1785 case LTTNG_LIST_TRACEPOINTS
:
1786 case LTTNG_CALIBRATE
:
1789 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
1790 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session
.name
);
1791 if (cmd_ctx
->session
== NULL
) {
1792 /* If session name not found */
1793 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
1794 ret
= LTTCOMM_SESS_NOT_FOUND
;
1795 } else { /* If no session name specified */
1796 ret
= LTTCOMM_SELECT_SESS
;
1800 /* Acquire lock for the session */
1801 lock_session(cmd_ctx
->session
);
1807 * Check domain type for specific "pre-action".
1809 switch (cmd_ctx
->lsm
->domain
.type
) {
1810 case LTTNG_DOMAIN_KERNEL
:
1811 /* Kernel tracer check */
1812 if (kernel_tracer_fd
== 0) {
1813 init_kernel_tracer();
1814 if (kernel_tracer_fd
== 0) {
1815 ret
= LTTCOMM_KERN_NA
;
1819 /* Need a session for kernel command */
1820 switch (cmd_ctx
->lsm
->cmd_type
) {
1821 case LTTNG_CALIBRATE
:
1822 case LTTNG_CREATE_SESSION
:
1823 case LTTNG_LIST_SESSIONS
:
1824 case LTTNG_LIST_TRACEPOINTS
:
1827 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1828 ret
= create_kernel_session(cmd_ctx
->session
);
1830 ret
= LTTCOMM_KERN_SESS_FAIL
;
1833 /* Start the kernel consumer daemon */
1834 if (kconsumerd_pid
== 0 &&
1835 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1836 ret
= start_kconsumerd();
1844 case LTTNG_DOMAIN_UST_PID
:
1850 /* Process by command type */
1851 switch (cmd_ctx
->lsm
->cmd_type
) {
1852 case LTTNG_ADD_CONTEXT
:
1854 struct lttng_kernel_context kctx
;
1856 /* Setup lttng message with no payload */
1857 ret
= setup_lttng_msg(cmd_ctx
, 0);
1862 switch (cmd_ctx
->lsm
->domain
.type
) {
1863 case LTTNG_DOMAIN_KERNEL
:
1864 /* Create Kernel context */
1865 kctx
.ctx
= cmd_ctx
->lsm
->u
.context
.ctx
.ctx
;
1866 kctx
.u
.perf_counter
.type
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.type
;
1867 kctx
.u
.perf_counter
.config
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.config
;
1868 strncpy(kctx
.u
.perf_counter
.name
,
1869 cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.name
,
1870 LTTNG_SYMBOL_NAME_LEN
);
1871 kctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1873 /* Add kernel context to kernel tracer. See context.c */
1874 ret
= add_kernel_context(cmd_ctx
->session
->kernel_session
, &kctx
,
1875 cmd_ctx
->lsm
->u
.context
.event_name
,
1876 cmd_ctx
->lsm
->u
.context
.channel_name
);
1877 if (ret
!= LTTCOMM_OK
) {
1882 /* TODO: Userspace tracing */
1883 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1890 case LTTNG_DISABLE_CHANNEL
:
1892 struct ltt_kernel_channel
*kchan
;
1894 /* Setup lttng message with no payload */
1895 ret
= setup_lttng_msg(cmd_ctx
, 0);
1900 switch (cmd_ctx
->lsm
->domain
.type
) {
1901 case LTTNG_DOMAIN_KERNEL
:
1902 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1903 cmd_ctx
->session
->kernel_session
);
1904 if (kchan
== NULL
) {
1905 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1907 } else if (kchan
->enabled
== 1) {
1908 ret
= kernel_disable_channel(kchan
);
1910 if (ret
!= EEXIST
) {
1911 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1916 kernel_wait_quiescent(kernel_tracer_fd
);
1919 /* TODO: Userspace tracing */
1920 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1927 case LTTNG_DISABLE_EVENT
:
1929 struct ltt_kernel_channel
*kchan
;
1930 struct ltt_kernel_event
*kevent
;
1932 /* Setup lttng message with no payload */
1933 ret
= setup_lttng_msg(cmd_ctx
, 0);
1938 switch (cmd_ctx
->lsm
->domain
.type
) {
1939 case LTTNG_DOMAIN_KERNEL
:
1940 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1941 cmd_ctx
->session
->kernel_session
);
1942 if (kchan
== NULL
) {
1943 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1947 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, kchan
);
1948 if (kevent
!= NULL
) {
1949 DBG("Disabling kernel event %s for channel %s.", kevent
->event
->name
,
1950 kchan
->channel
->name
);
1951 ret
= kernel_disable_event(kevent
);
1953 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1958 kernel_wait_quiescent(kernel_tracer_fd
);
1961 /* TODO: Userspace tracing */
1962 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1969 case LTTNG_DISABLE_ALL_EVENT
:
1971 struct ltt_kernel_channel
*kchan
;
1972 struct ltt_kernel_event
*kevent
;
1974 /* Setup lttng message with no payload */
1975 ret
= setup_lttng_msg(cmd_ctx
, 0);
1980 switch (cmd_ctx
->lsm
->domain
.type
) {
1981 case LTTNG_DOMAIN_KERNEL
:
1982 DBG("Disabling all enabled kernel events");
1983 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1984 cmd_ctx
->session
->kernel_session
);
1985 if (kchan
== NULL
) {
1986 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1990 /* For each event in the kernel session */
1991 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1992 DBG("Disabling kernel event %s for channel %s.",
1993 kevent
->event
->name
, kchan
->channel
->name
);
1994 ret
= kernel_disable_event(kevent
);
2000 /* Quiescent wait after event disable */
2001 kernel_wait_quiescent(kernel_tracer_fd
);
2004 /* TODO: Userspace tracing */
2005 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2012 case LTTNG_ENABLE_CHANNEL
:
2014 struct ltt_kernel_channel
*kchan
;
2016 /* Setup lttng message with no payload */
2017 ret
= setup_lttng_msg(cmd_ctx
, 0);
2022 switch (cmd_ctx
->lsm
->domain
.type
) {
2023 case LTTNG_DOMAIN_KERNEL
:
2024 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
2025 cmd_ctx
->session
->kernel_session
);
2026 if (kchan
== NULL
) {
2027 /* Channel not found, creating it */
2028 DBG("Creating kernel channel");
2030 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2031 &cmd_ctx
->lsm
->u
.channel
.chan
,
2032 cmd_ctx
->session
->kernel_session
->trace_path
);
2034 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2038 /* Notify kernel thread that there is a new channel */
2039 ret
= notify_kernel_pollfd();
2041 ret
= LTTCOMM_FATAL
;
2044 } else if (kchan
->enabled
== 0) {
2045 ret
= kernel_enable_channel(kchan
);
2047 if (ret
!= EEXIST
) {
2048 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
2054 kernel_wait_quiescent(kernel_tracer_fd
);
2056 case LTTNG_DOMAIN_UST_PID
:
2060 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2067 case LTTNG_ENABLE_EVENT
:
2070 struct ltt_kernel_channel
*kchan
;
2071 struct ltt_kernel_event
*kevent
;
2072 struct lttng_channel
*chan
;
2074 /* Setup lttng message with no payload */
2075 ret
= setup_lttng_msg(cmd_ctx
, 0);
2080 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2082 switch (cmd_ctx
->lsm
->domain
.type
) {
2083 case LTTNG_DOMAIN_KERNEL
:
2084 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2085 cmd_ctx
->session
->kernel_session
);
2086 if (kchan
== NULL
) {
2087 DBG("Channel not found. Creating channel %s", channel_name
);
2089 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2091 ret
= LTTCOMM_FATAL
;
2095 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2096 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2098 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2101 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2102 cmd_ctx
->session
->kernel_session
);
2103 if (kchan
== NULL
) {
2104 ERR("Channel %s not found after creation. Internal error, giving up.",
2106 ret
= LTTCOMM_FATAL
;
2111 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, kchan
);
2112 if (kevent
== NULL
) {
2113 DBG("Creating kernel event %s for channel %s.",
2114 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
2115 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, kchan
);
2117 DBG("Enabling kernel event %s for channel %s.",
2118 kevent
->event
->name
, channel_name
);
2119 ret
= kernel_enable_event(kevent
);
2120 if (ret
== -EEXIST
) {
2121 ret
= LTTCOMM_KERN_EVENT_EXIST
;
2127 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2131 kernel_wait_quiescent(kernel_tracer_fd
);
2134 /* TODO: Userspace tracing */
2135 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2141 case LTTNG_ENABLE_ALL_EVENT
:
2145 struct ltt_kernel_channel
*kchan
;
2146 struct ltt_kernel_event
*kevent
;
2147 struct lttng_event
*event_list
;
2148 struct lttng_channel
*chan
;
2150 /* Setup lttng message with no payload */
2151 ret
= setup_lttng_msg(cmd_ctx
, 0);
2156 DBG("Enabling all kernel event");
2158 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2160 switch (cmd_ctx
->lsm
->domain
.type
) {
2161 case LTTNG_DOMAIN_KERNEL
:
2162 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2163 cmd_ctx
->session
->kernel_session
);
2164 if (kchan
== NULL
) {
2165 DBG("Channel not found. Creating channel %s", channel_name
);
2167 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2169 ret
= LTTCOMM_FATAL
;
2173 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2174 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2176 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2179 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2180 cmd_ctx
->session
->kernel_session
);
2181 if (kchan
== NULL
) {
2182 ERR("Channel %s not found after creation. Internal error, giving up.",
2184 ret
= LTTCOMM_FATAL
;
2189 /* For each event in the kernel session */
2190 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
2191 DBG("Enabling kernel event %s for channel %s.",
2192 kevent
->event
->name
, channel_name
);
2193 ret
= kernel_enable_event(kevent
);
2199 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
2201 ret
= LTTCOMM_KERN_LIST_FAIL
;
2205 for (i
= 0; i
< size
; i
++) {
2206 kevent
= trace_kernel_get_event_by_name(event_list
[i
].name
, kchan
);
2207 if (kevent
== NULL
) {
2208 /* Default event type for enable all */
2209 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2210 /* Enable each single tracepoint event */
2211 ret
= kernel_create_event(&event_list
[i
], kchan
);
2213 /* Ignore error here and continue */
2220 /* Quiescent wait after event enable */
2221 kernel_wait_quiescent(kernel_tracer_fd
);
2224 /* TODO: Userspace tracing */
2225 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2232 case LTTNG_LIST_TRACEPOINTS
:
2234 struct lttng_event
*events
;
2235 ssize_t nb_events
= 0;
2237 switch (cmd_ctx
->lsm
->domain
.type
) {
2238 case LTTNG_DOMAIN_KERNEL
:
2239 DBG("Listing kernel events");
2240 nb_events
= kernel_list_events(kernel_tracer_fd
, &events
);
2241 if (nb_events
< 0) {
2242 ret
= LTTCOMM_KERN_LIST_FAIL
;
2247 /* TODO: Userspace listing */
2248 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2253 * Setup lttng message with payload size set to the event list size in
2254 * bytes and then copy list into the llm payload.
2256 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2262 /* Copy event list into message payload */
2263 memcpy(cmd_ctx
->llm
->payload
, events
,
2264 sizeof(struct lttng_event
) * nb_events
);
2271 case LTTNG_START_TRACE
:
2273 struct ltt_kernel_channel
*chan
;
2275 /* Setup lttng message with no payload */
2276 ret
= setup_lttng_msg(cmd_ctx
, 0);
2281 /* Kernel tracing */
2282 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2283 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
2284 DBG("Open kernel metadata");
2285 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
2286 cmd_ctx
->session
->kernel_session
->trace_path
);
2288 ret
= LTTCOMM_KERN_META_FAIL
;
2293 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2294 DBG("Opening kernel metadata stream");
2295 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2296 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
2298 ERR("Kernel create metadata stream failed");
2299 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2305 /* For each channel */
2306 cds_list_for_each_entry(chan
,
2307 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2308 if (chan
->stream_count
== 0) {
2309 ret
= kernel_open_channel_stream(chan
);
2311 ERR("Kernel create channel stream failed");
2312 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2315 /* Update the stream global counter */
2316 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
2320 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
2322 ret
= LTTCOMM_KERN_START_FAIL
;
2326 DBG("Start kernel tracing");
2327 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
2329 ERR("Kernel start session failed");
2330 ret
= LTTCOMM_KERN_START_FAIL
;
2334 /* Quiescent wait after starting trace */
2335 kernel_wait_quiescent(kernel_tracer_fd
);
2338 /* TODO: Start all UST traces */
2343 case LTTNG_STOP_TRACE
:
2345 struct ltt_kernel_channel
*chan
;
2346 /* Setup lttng message with no payload */
2347 ret
= setup_lttng_msg(cmd_ctx
, 0);
2353 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2354 DBG("Stop kernel tracing");
2356 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
2358 ERR("Kernel metadata flush failed");
2361 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2362 ret
= kernel_flush_buffer(chan
);
2364 ERR("Kernel flush buffer error");
2368 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
2370 ERR("Kernel stop session failed");
2371 ret
= LTTCOMM_KERN_STOP_FAIL
;
2375 /* Quiescent wait after stopping trace */
2376 kernel_wait_quiescent(kernel_tracer_fd
);
2379 /* TODO : User-space tracer */
2384 case LTTNG_CREATE_SESSION
:
2386 /* Setup lttng message with no payload */
2387 ret
= setup_lttng_msg(cmd_ctx
, 0);
2392 tracepoint(create_session_start
);
2393 ret
= create_session(cmd_ctx
->lsm
->session
.name
, cmd_ctx
->lsm
->session
.path
);
2394 tracepoint(create_session_end
);
2396 if (ret
== -EEXIST
) {
2397 ret
= LTTCOMM_EXIST_SESS
;
2399 ret
= LTTCOMM_FATAL
;
2407 case LTTNG_DESTROY_SESSION
:
2409 /* Setup lttng message with no payload */
2410 ret
= setup_lttng_msg(cmd_ctx
, 0);
2415 /* Clean kernel session teardown */
2416 teardown_kernel_session(cmd_ctx
->session
);
2418 tracepoint(destroy_session_start
);
2419 ret
= destroy_session(cmd_ctx
->lsm
->session
.name
);
2420 tracepoint(destroy_session_end
);
2422 ret
= LTTCOMM_FATAL
;
2427 * Must notify the kernel thread here to update it's pollfd in order to
2428 * remove the channel(s)' fd just destroyed.
2430 ret
= notify_kernel_pollfd();
2432 ret
= LTTCOMM_FATAL
;
2439 case LTTNG_LIST_DOMAINS
:
2443 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2447 nb_dom
+= cmd_ctx
->session
->ust_session_list
.count
;
2449 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_domain
) * nb_dom
);
2454 ((struct lttng_domain
*)(cmd_ctx
->llm
->payload
))[0].type
=
2455 LTTNG_DOMAIN_KERNEL
;
2457 /* TODO: User-space tracer domain support */
2461 case LTTNG_LIST_CHANNELS
:
2464 * TODO: Only kernel channels are listed here. UST listing
2465 * is needed on lttng-ust 2.0 release.
2468 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2469 nb_chan
+= cmd_ctx
->session
->kernel_session
->channel_count
;
2472 ret
= setup_lttng_msg(cmd_ctx
,
2473 sizeof(struct lttng_channel
) * nb_chan
);
2478 list_lttng_channels(cmd_ctx
->session
,
2479 (struct lttng_channel
*)(cmd_ctx
->llm
->payload
));
2484 case LTTNG_LIST_EVENTS
:
2487 * TODO: Only kernel events are listed here. UST listing
2488 * is needed on lttng-ust 2.0 release.
2490 size_t nb_event
= 0;
2491 struct ltt_kernel_channel
*kchan
= NULL
;
2493 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2494 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.list
.channel_name
,
2495 cmd_ctx
->session
->kernel_session
);
2496 if (kchan
== NULL
) {
2497 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2500 nb_event
+= kchan
->event_count
;
2503 ret
= setup_lttng_msg(cmd_ctx
,
2504 sizeof(struct lttng_event
) * nb_event
);
2509 DBG("Listing events (%zu events)", nb_event
);
2511 list_lttng_events(kchan
,
2512 (struct lttng_event
*)(cmd_ctx
->llm
->payload
));
2517 case LTTNG_LIST_SESSIONS
:
2519 lock_session_list();
2521 if (session_list_ptr
->count
== 0) {
2522 ret
= LTTCOMM_NO_SESSION
;
2523 unlock_session_list();
2527 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
2528 session_list_ptr
->count
);
2530 unlock_session_list();
2534 /* Filled the session array */
2535 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
2537 unlock_session_list();
2542 case LTTNG_CALIBRATE
:
2544 /* Setup lttng message with no payload */
2545 ret
= setup_lttng_msg(cmd_ctx
, 0);
2550 switch (cmd_ctx
->lsm
->domain
.type
) {
2551 case LTTNG_DOMAIN_KERNEL
:
2553 struct lttng_kernel_calibrate kcalibrate
;
2555 kcalibrate
.type
= cmd_ctx
->lsm
->u
.calibrate
.type
;
2556 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2558 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2564 /* TODO: Userspace tracing */
2565 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2571 case LTTNG_REGISTER_CONSUMER
:
2575 /* Setup lttng message with no payload */
2576 ret
= setup_lttng_msg(cmd_ctx
, 0);
2581 switch (cmd_ctx
->lsm
->domain
.type
) {
2582 case LTTNG_DOMAIN_KERNEL
:
2584 /* Can't register a consumer if there is already one */
2585 if (cmd_ctx
->session
->kernel_session
->consumer_fd
!= 0) {
2586 ret
= LTTCOMM_CONNECT_FAIL
;
2590 sock
= lttcomm_connect_unix_sock(cmd_ctx
->lsm
->u
.reg
.path
);
2592 ret
= LTTCOMM_CONNECT_FAIL
;
2596 cmd_ctx
->session
->kernel_session
->consumer_fd
= sock
;
2600 /* TODO: Userspace tracing */
2601 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2610 /* Undefined command */
2611 ret
= setup_lttng_msg(cmd_ctx
, 0);
2620 /* Set return code */
2621 cmd_ctx
->llm
->ret_code
= ret
;
2623 if (cmd_ctx
->session
) {
2624 unlock_session(cmd_ctx
->session
);
2630 if (cmd_ctx
->llm
== NULL
) {
2631 DBG("Missing llm structure. Allocating one.");
2632 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2636 /* Notify client of error */
2637 cmd_ctx
->llm
->ret_code
= ret
;
2640 if (cmd_ctx
->session
) {
2641 unlock_session(cmd_ctx
->session
);
2647 * This thread manage all clients request using the unix client socket for
2650 static void *thread_manage_clients(void *data
)
2653 struct command_ctx
*cmd_ctx
= NULL
;
2654 struct pollfd pollfd
[2];
2656 tracepoint(sessiond_th_cli_start
);
2658 DBG("[thread] Manage client started");
2660 ret
= lttcomm_listen_unix_sock(client_sock
);
2665 /* First fd is always the quit pipe */
2666 pollfd
[0].fd
= thread_quit_pipe
[0];
2669 pollfd
[1].fd
= client_sock
;
2670 pollfd
[1].events
= POLLIN
;
2672 /* Notify parent pid that we are ready
2673 * to accept command for client side.
2675 if (opt_sig_parent
) {
2676 kill(ppid
, SIGCHLD
);
2680 DBG("Accepting client command ...");
2682 tracepoint(sessiond_th_cli_poll
);
2684 /* Inifinite blocking call, waiting for transmission */
2685 ret
= poll(pollfd
, 2, -1);
2687 perror("poll client thread");
2691 /* Thread quit pipe has been closed. Killing thread. */
2692 if (pollfd
[0].revents
== POLLNVAL
) {
2694 } else if (pollfd
[1].revents
== POLLERR
) {
2695 ERR("Client socket poll error");
2699 sock
= lttcomm_accept_unix_sock(client_sock
);
2704 /* Allocate context command to process the client request */
2705 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2707 /* Allocate data buffer for reception */
2708 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2709 cmd_ctx
->llm
= NULL
;
2710 cmd_ctx
->session
= NULL
;
2713 * Data is received from the lttng client. The struct
2714 * lttcomm_session_msg (lsm) contains the command and data request of
2717 DBG("Receiving data from client ...");
2718 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2723 // TODO: Validate cmd_ctx including sanity check for security purpose.
2726 * This function dispatch the work to the kernel or userspace tracer
2727 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2728 * informations for the client. The command context struct contains
2729 * everything this function may needs.
2731 ret
= process_client_msg(cmd_ctx
);
2733 /* TODO: Inform client somehow of the fatal error. At this point,
2734 * ret < 0 means that a malloc failed (ENOMEM). */
2735 /* Error detected but still accept command */
2736 clean_command_ctx(&cmd_ctx
);
2740 DBG("Sending response (size: %d, retcode: %d)",
2741 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2742 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2744 ERR("Failed to send data back to client");
2747 clean_command_ctx(&cmd_ctx
);
2749 /* End of transmission */
2754 DBG("Client thread dying");
2762 unlink(client_unix_sock_path
);
2764 clean_command_ctx(&cmd_ctx
);
2770 * usage function on stderr
2772 static void usage(void)
2774 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2775 fprintf(stderr
, " -h, --help Display this usage.\n");
2776 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2777 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2778 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2779 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2780 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2781 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2782 fprintf(stderr
, " -V, --version Show version number.\n");
2783 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2784 fprintf(stderr
, " -q, --quiet No output at all.\n");
2785 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2786 fprintf(stderr
, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2790 * daemon argument parsing
2792 static int parse_args(int argc
, char **argv
)
2796 static struct option long_options
[] = {
2797 { "client-sock", 1, 0, 'c' },
2798 { "apps-sock", 1, 0, 'a' },
2799 { "kconsumerd-cmd-sock", 1, 0, 0 },
2800 { "kconsumerd-err-sock", 1, 0, 0 },
2801 { "daemonize", 0, 0, 'd' },
2802 { "sig-parent", 0, 0, 'S' },
2803 { "help", 0, 0, 'h' },
2804 { "group", 1, 0, 'g' },
2805 { "version", 0, 0, 'V' },
2806 { "quiet", 0, 0, 'q' },
2807 { "verbose", 0, 0, 'v' },
2808 { "verbose-kconsumerd", 0, 0, 'Z' },
2813 int option_index
= 0;
2814 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:Z", long_options
, &option_index
);
2821 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2823 fprintf(stderr
, " with arg %s\n", optarg
);
2827 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2830 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2836 opt_tracing_group
= strdup(optarg
);
2842 fprintf(stdout
, "%s\n", VERSION
);
2848 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2851 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2857 /* Verbose level can increase using multiple -v */
2861 opt_verbose_kconsumerd
+= 1;
2864 /* Unknown option or other error.
2865 * Error is printed by getopt, just return */
2874 * Creates the two needed socket by the daemon.
2875 * apps_sock - The communication socket for all UST apps.
2876 * client_sock - The communication of the cli tool (lttng).
2878 static int init_daemon_socket(void)
2883 old_umask
= umask(0);
2885 /* Create client tool unix socket */
2886 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2887 if (client_sock
< 0) {
2888 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2893 /* File permission MUST be 660 */
2894 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2896 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2901 /* Create the application unix socket */
2902 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2903 if (apps_sock
< 0) {
2904 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2909 /* File permission MUST be 666 */
2910 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2912 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2923 * Check if the global socket is available, and if a daemon is answering
2924 * at the other side. If yes, error is returned.
2926 static int check_existing_daemon(void)
2928 if (access(client_unix_sock_path
, F_OK
) < 0 &&
2929 access(apps_unix_sock_path
, F_OK
) < 0) {
2932 /* Is there anybody out there ? */
2933 if (lttng_session_daemon_alive()) {
2941 * Set the tracing group gid onto the client socket.
2943 * Race window between mkdir and chown is OK because we are going from more
2944 * permissive (root.root) to les permissive (root.tracing).
2946 static int set_permissions(void)
2951 gid
= allowed_group();
2954 WARN("No tracing group detected");
2957 ERR("Missing tracing group. Aborting execution.");
2963 /* Set lttng run dir */
2964 ret
= chown(LTTNG_RUNDIR
, 0, gid
);
2966 ERR("Unable to set group on " LTTNG_RUNDIR
);
2970 /* lttng client socket path */
2971 ret
= chown(client_unix_sock_path
, 0, gid
);
2973 ERR("Unable to set group on %s", client_unix_sock_path
);
2977 /* kconsumerd error socket path */
2978 ret
= chown(kconsumerd_err_unix_sock_path
, 0, gid
);
2980 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2984 DBG("All permissions are set");
2991 * Create the pipe used to wake up the kernel thread.
2993 static int create_kernel_poll_pipe(void)
2995 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2999 * Create the application command pipe to wake thread_manage_apps.
3001 static int create_apps_cmd_pipe(void)
3003 return pipe2(apps_cmd_pipe
, O_CLOEXEC
);
3007 * Create the lttng run directory needed for all global sockets and pipe.
3009 static int create_lttng_rundir(void)
3013 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
3015 if (errno
!= EEXIST
) {
3016 ERR("Unable to create " LTTNG_RUNDIR
);
3028 * Setup sockets and directory needed by the kconsumerd communication with the
3031 static int set_kconsumerd_sockets(void)
3035 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
3036 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
3039 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
3040 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
3043 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
3045 if (errno
!= EEXIST
) {
3046 ERR("Failed to create " KCONSUMERD_PATH
);
3052 /* Create the kconsumerd error unix socket */
3053 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
3054 if (kconsumerd_err_sock
< 0) {
3055 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
3060 /* File permission MUST be 660 */
3061 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3063 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
3073 * Signal handler for the daemon
3075 * Simply stop all worker threads, leaving main() return gracefully
3076 * after joining all threads and calling cleanup().
3078 static void sighandler(int sig
)
3082 DBG("SIGPIPE catched");
3085 DBG("SIGINT catched");
3089 DBG("SIGTERM catched");
3098 * Setup signal handler for :
3099 * SIGINT, SIGTERM, SIGPIPE
3101 static int set_signal_handler(void)
3104 struct sigaction sa
;
3107 if ((ret
= sigemptyset(&sigset
)) < 0) {
3108 perror("sigemptyset");
3112 sa
.sa_handler
= sighandler
;
3113 sa
.sa_mask
= sigset
;
3115 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3116 perror("sigaction");
3120 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3121 perror("sigaction");
3125 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3126 perror("sigaction");
3130 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3136 * Set open files limit to unlimited. This daemon can open a large number of
3137 * file descriptors in order to consumer multiple kernel traces.
3139 static void set_ulimit(void)
3144 /* The kernel does not allowed an infinite limit for open files */
3145 lim
.rlim_cur
= 65535;
3146 lim
.rlim_max
= 65535;
3148 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3150 perror("failed to set open files limit");
3157 int main(int argc
, char **argv
)
3161 const char *home_path
;
3163 tracepoint(sessiond_boot_start
);
3165 /* Create thread quit pipe */
3166 if ((ret
= init_thread_quit_pipe()) < 0) {
3170 /* Parse arguments */
3172 if ((ret
= parse_args(argc
, argv
) < 0)) {
3185 /* Check if daemon is UID = 0 */
3186 is_root
= !getuid();
3189 ret
= create_lttng_rundir();
3194 if (strlen(apps_unix_sock_path
) == 0) {
3195 snprintf(apps_unix_sock_path
, PATH_MAX
,
3196 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
3199 if (strlen(client_unix_sock_path
) == 0) {
3200 snprintf(client_unix_sock_path
, PATH_MAX
,
3201 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
3204 /* Set global SHM for ust */
3205 if (strlen(wait_shm_path
) == 0) {
3206 snprintf(wait_shm_path
, PATH_MAX
,
3207 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
3210 home_path
= get_home_dir();
3211 if (home_path
== NULL
) {
3212 /* TODO: Add --socket PATH option */
3213 ERR("Can't get HOME directory for sockets creation.");
3218 if (strlen(apps_unix_sock_path
) == 0) {
3219 snprintf(apps_unix_sock_path
, PATH_MAX
,
3220 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
3223 /* Set the cli tool unix socket path */
3224 if (strlen(client_unix_sock_path
) == 0) {
3225 snprintf(client_unix_sock_path
, PATH_MAX
,
3226 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
3229 /* Set global SHM for ust */
3230 if (strlen(wait_shm_path
) == 0) {
3231 snprintf(wait_shm_path
, PATH_MAX
,
3232 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, geteuid());
3236 DBG("Client socket path %s", client_unix_sock_path
);
3237 DBG("Application socket path %s", apps_unix_sock_path
);
3240 * See if daemon already exist.
3242 if ((ret
= check_existing_daemon()) < 0) {
3243 ERR("Already running daemon.\n");
3245 * We do not goto exit because we must not cleanup()
3246 * because a daemon is already running.
3251 /* After this point, we can safely call cleanup() so goto error is used */
3254 * These actions must be executed as root. We do that *after* setting up
3255 * the sockets path because we MUST make the check for another daemon using
3256 * those paths *before* trying to set the kernel consumer sockets and init
3260 ret
= set_kconsumerd_sockets();
3265 /* Setup kernel tracer */
3266 init_kernel_tracer();
3268 /* Set ulimit for open files */
3272 if ((ret
= set_signal_handler()) < 0) {
3276 /* Setup the needed unix socket */
3277 if ((ret
= init_daemon_socket()) < 0) {
3281 /* Set credentials to socket */
3282 if (is_root
&& ((ret
= set_permissions()) < 0)) {
3286 /* Get parent pid if -S, --sig-parent is specified. */
3287 if (opt_sig_parent
) {
3291 /* Setup the kernel pipe for waking up the kernel thread */
3292 if ((ret
= create_kernel_poll_pipe()) < 0) {
3296 /* Setup the thread apps communication pipe. */
3297 if ((ret
= create_apps_cmd_pipe()) < 0) {
3301 /* Init UST command queue. */
3302 cds_wfq_init(&ust_cmd_queue
.queue
);
3305 * Get session list pointer. This pointer MUST NOT be free().
3306 * This list is statically declared in session.c
3308 session_list_ptr
= get_session_list();
3310 /* Create thread to manage the client socket */
3311 ret
= pthread_create(&client_thread
, NULL
,
3312 thread_manage_clients
, (void *) NULL
);
3314 perror("pthread_create clients");
3318 /* Create thread to dispatch registration */
3319 ret
= pthread_create(&dispatch_thread
, NULL
,
3320 thread_dispatch_ust_registration
, (void *) NULL
);
3322 perror("pthread_create dispatch");
3326 /* Create thread to manage application registration. */
3327 ret
= pthread_create(®_apps_thread
, NULL
,
3328 thread_registration_apps
, (void *) NULL
);
3330 perror("pthread_create registration");
3334 /* Create thread to manage application socket */
3335 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
3337 perror("pthread_create apps");
3341 /* Create kernel thread to manage kernel event */
3342 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
3344 perror("pthread_create kernel");
3348 tracepoint(sessiond_boot_end
);
3350 ret
= pthread_join(kernel_thread
, &status
);
3352 perror("pthread_join");
3353 goto error
; /* join error, exit without cleanup */
3357 ret
= pthread_join(apps_thread
, &status
);
3359 perror("pthread_join");
3360 goto error
; /* join error, exit without cleanup */
3364 ret
= pthread_join(reg_apps_thread
, &status
);
3366 perror("pthread_join");
3367 goto error
; /* join error, exit without cleanup */
3371 ret
= pthread_join(dispatch_thread
, &status
);
3373 perror("pthread_join");
3374 goto error
; /* join error, exit without cleanup */
3378 ret
= pthread_join(client_thread
, &status
);
3380 perror("pthread_join");
3381 goto error
; /* join error, exit without cleanup */
3384 ret
= join_kconsumerd_thread();
3386 perror("join_kconsumerd");
3387 goto error
; /* join error, exit without cleanup */
3393 * cleanup() is called when no other thread is running.