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 bench_print_boot_process();
313 * Send data on a unix socket using the liblttsessiondcomm API.
315 * Return lttcomm error code.
317 static int send_unix_sock(int sock
, void *buf
, size_t len
)
319 /* Check valid length */
324 return lttcomm_send_unix_sock(sock
, buf
, len
);
328 * Free memory of a command context structure.
330 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
332 DBG("Clean command context structure");
334 if ((*cmd_ctx
)->llm
) {
335 free((*cmd_ctx
)->llm
);
337 if ((*cmd_ctx
)->lsm
) {
338 free((*cmd_ctx
)->lsm
);
346 * Send all stream fds of kernel channel to the consumer.
348 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
352 struct ltt_kernel_stream
*stream
;
353 struct lttcomm_kconsumerd_header lkh
;
354 struct lttcomm_kconsumerd_msg lkm
;
356 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
358 nb_fd
= channel
->stream_count
;
361 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
362 lkh
.cmd_type
= ADD_STREAM
;
364 DBG("Sending kconsumerd header");
366 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
368 perror("send kconsumerd header");
372 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
373 if (stream
->fd
!= 0) {
375 lkm
.state
= stream
->state
;
376 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
377 lkm
.output
= channel
->channel
->attr
.output
;
378 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
379 lkm
.path_name
[PATH_MAX
- 1] = '\0';
381 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
383 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
385 perror("send kconsumerd fd");
391 DBG("Kconsumerd channel fds sent");
400 * Send all stream fds of the kernel session to the consumer.
402 static int send_kconsumerd_fds(struct ltt_kernel_session
*session
)
405 struct ltt_kernel_channel
*chan
;
406 struct lttcomm_kconsumerd_header lkh
;
407 struct lttcomm_kconsumerd_msg lkm
;
410 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
411 lkh
.cmd_type
= ADD_STREAM
;
413 DBG("Sending kconsumerd header for metadata");
415 ret
= lttcomm_send_unix_sock(session
->consumer_fd
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
417 perror("send kconsumerd header");
421 DBG("Sending metadata stream fd");
423 /* Extra protection. It's NOT suppose to be set to 0 at this point */
424 if (session
->consumer_fd
== 0) {
425 session
->consumer_fd
= kconsumerd_cmd_sock
;
428 if (session
->metadata_stream_fd
!= 0) {
429 /* Send metadata stream fd first */
430 lkm
.fd
= session
->metadata_stream_fd
;
431 lkm
.state
= ACTIVE_FD
;
432 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
433 lkm
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
434 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
435 lkm
.path_name
[PATH_MAX
- 1] = '\0';
437 ret
= lttcomm_send_fds_unix_sock(session
->consumer_fd
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
439 perror("send kconsumerd fd");
444 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
445 ret
= send_kconsumerd_channel_fds(session
->consumer_fd
, chan
);
451 DBG("Kconsumerd fds (metadata and channel streams) sent");
460 * Notify UST applications using the shm mmap futex.
462 static int notify_ust_apps(int active
)
466 DBG("Notifying applications of session daemon state: %d", active
);
468 /* See shm.c for this call implying mmap, shm and futex calls */
469 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
470 if (wait_shm_mmap
== NULL
) {
474 /* Wake waiting process */
475 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
477 /* Apps notified successfully */
485 * Setup the outgoing data buffer for the response (llm) by allocating the
486 * right amount of memory and copying the original information from the lsm
489 * Return total size of the buffer pointed by buf.
491 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
497 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
498 if (cmd_ctx
->llm
== NULL
) {
504 /* Copy common data */
505 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
506 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
508 cmd_ctx
->llm
->data_size
= size
;
509 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
518 * Update the kernel pollfd set of all channel fd available over all tracing
519 * session. Add the wakeup pipe at the end of the set.
521 static int update_kernel_pollfd(void)
525 * The wakup pipe and the quit pipe are needed so the number of fds starts
526 * at 2 for those pipes.
528 unsigned int nb_fd
= 2;
529 struct ltt_session
*session
;
530 struct ltt_kernel_channel
*channel
;
532 DBG("Updating kernel_pollfd");
534 /* Get the number of channel of all kernel session */
536 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
537 lock_session(session
);
538 if (session
->kernel_session
== NULL
) {
539 unlock_session(session
);
542 nb_fd
+= session
->kernel_session
->channel_count
;
543 unlock_session(session
);
546 DBG("Resizing kernel_pollfd to size %d", nb_fd
);
548 kernel_pollfd
= realloc(kernel_pollfd
, nb_fd
* sizeof(struct pollfd
));
549 if (kernel_pollfd
== NULL
) {
550 perror("malloc kernel_pollfd");
554 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
555 lock_session(session
);
556 if (session
->kernel_session
== NULL
) {
557 unlock_session(session
);
561 ERR("To much channel for kernel_pollfd size");
562 unlock_session(session
);
565 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
566 kernel_pollfd
[i
].fd
= channel
->fd
;
567 kernel_pollfd
[i
].events
= POLLIN
| POLLRDNORM
;
570 unlock_session(session
);
572 unlock_session_list();
574 /* Adding wake up pipe */
575 kernel_pollfd
[nb_fd
- 2].fd
= kernel_poll_pipe
[0];
576 kernel_pollfd
[nb_fd
- 2].events
= POLLIN
;
578 /* Adding the quit pipe */
579 kernel_pollfd
[nb_fd
- 1].fd
= thread_quit_pipe
[0];
584 unlock_session_list();
589 * Find the channel fd from 'fd' over all tracing session. When found, check
590 * for new channel stream and send those stream fds to the kernel consumer.
592 * Useful for CPU hotplug feature.
594 static int update_kernel_stream(int fd
)
597 struct ltt_session
*session
;
598 struct ltt_kernel_channel
*channel
;
600 DBG("Updating kernel streams for channel fd %d", fd
);
603 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
604 lock_session(session
);
605 if (session
->kernel_session
== NULL
) {
606 unlock_session(session
);
610 /* This is not suppose to be 0 but this is an extra security check */
611 if (session
->kernel_session
->consumer_fd
== 0) {
612 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
615 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
616 if (channel
->fd
== fd
) {
617 DBG("Channel found, updating kernel streams");
618 ret
= kernel_open_channel_stream(channel
);
624 * Have we already sent fds to the consumer? If yes, it means that
625 * tracing is started so it is safe to send our updated stream fds.
627 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
628 ret
= send_kconsumerd_channel_fds(session
->kernel_session
->consumer_fd
,
637 unlock_session(session
);
641 unlock_session_list();
643 unlock_session(session
);
649 * This thread manage event coming from the kernel.
651 * Features supported in this thread:
654 static void *thread_manage_kernel(void *data
)
656 int ret
, i
, nb_fd
= 0;
658 int update_poll_flag
= 1;
660 tracepoint(sessiond_th_kern_start
);
662 DBG("Thread manage kernel started");
665 if (update_poll_flag
== 1) {
666 nb_fd
= update_kernel_pollfd();
670 update_poll_flag
= 0;
673 DBG("Polling on %d fds", nb_fd
);
675 tracepoint(sessiond_th_kern_poll
);
677 /* Poll infinite value of time */
678 ret
= poll(kernel_pollfd
, nb_fd
, -1);
680 perror("poll kernel thread");
682 } else if (ret
== 0) {
683 /* Should not happen since timeout is infinite */
687 /* Thread quit pipe has been closed. Killing thread. */
688 if (kernel_pollfd
[nb_fd
- 1].revents
== POLLNVAL
) {
692 DBG("Kernel poll event triggered");
695 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
698 switch (kernel_pollfd
[nb_fd
- 2].revents
) {
700 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
701 update_poll_flag
= 1;
709 for (i
= 0; i
< nb_fd
; i
++) {
710 switch (kernel_pollfd
[i
].revents
) {
712 * New CPU detected by the kernel. Adding kernel stream to kernel
713 * session and updating the kernel consumer
715 case POLLIN
| POLLRDNORM
:
716 ret
= update_kernel_stream(kernel_pollfd
[i
].fd
);
726 DBG("Kernel thread dying");
731 close(kernel_poll_pipe
[0]);
732 close(kernel_poll_pipe
[1]);
737 * This thread manage the kconsumerd error sent back to the session daemon.
739 static void *thread_manage_kconsumerd(void *data
)
742 enum lttcomm_return_code code
;
743 struct pollfd pollfd
[2];
745 tracepoint(sessiond_th_kcon_start
);
747 DBG("[thread] Manage kconsumerd started");
749 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
754 /* First fd is always the quit pipe */
755 pollfd
[0].fd
= thread_quit_pipe
[0];
758 pollfd
[1].fd
= kconsumerd_err_sock
;
759 pollfd
[1].events
= POLLIN
;
761 tracepoint(sessiond_th_kcon_poll
);
763 /* Inifinite blocking call, waiting for transmission */
764 ret
= poll(pollfd
, 2, -1);
766 perror("poll kconsumerd thread");
770 /* Thread quit pipe has been closed. Killing thread. */
771 if (pollfd
[0].revents
== POLLNVAL
) {
773 } else if (pollfd
[1].revents
== POLLERR
) {
774 ERR("Kconsumerd err socket poll error");
778 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
783 /* Getting status code from kconsumerd */
784 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
789 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
790 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
791 if (kconsumerd_cmd_sock
< 0) {
792 sem_post(&kconsumerd_sem
);
793 perror("kconsumerd connect");
796 /* Signal condition to tell that the kconsumerd is ready */
797 sem_post(&kconsumerd_sem
);
798 DBG("Kconsumerd command socket ready");
800 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
801 lttcomm_get_readable_code(-code
));
805 /* Kconsumerd err socket */
807 pollfd
[1].events
= POLLIN
;
809 /* Inifinite blocking call, waiting for transmission */
810 ret
= poll(pollfd
, 2, -1);
812 perror("poll kconsumerd thread");
816 /* Thread quit pipe has been closed. Killing thread. */
817 if (pollfd
[0].revents
== POLLNVAL
) {
819 } else if (pollfd
[1].revents
== POLLERR
) {
820 ERR("Kconsumerd err socket second poll error");
824 /* Wait for any kconsumerd error */
825 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
827 ERR("Kconsumerd closed the command socket");
831 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
834 DBG("Kconsumerd thread dying");
835 if (kconsumerd_err_sock
) {
836 close(kconsumerd_err_sock
);
838 if (kconsumerd_cmd_sock
) {
839 close(kconsumerd_cmd_sock
);
845 unlink(kconsumerd_err_unix_sock_path
);
846 unlink(kconsumerd_cmd_unix_sock_path
);
853 * Reallocate the apps command pollfd structure of nb_fd size.
855 * The first two fds must be there at all time.
857 static int update_apps_cmd_pollfd(unsigned int nb_fd
, unsigned int old_nb_fd
,
858 struct pollfd
**pollfd
)
861 struct pollfd
*old_pollfd
= NULL
;
863 /* Can't accept pollfd less than 2 */
870 old_pollfd
= *pollfd
;
873 *pollfd
= malloc(nb_fd
* sizeof(struct pollfd
));
874 if (*pollfd
== NULL
) {
875 perror("malloc manage apps pollfd");
879 /* First fd is always the quit pipe */
880 (*pollfd
)[0].fd
= thread_quit_pipe
[0];
881 /* Apps command pipe */
882 (*pollfd
)[1].fd
= apps_cmd_pipe
[0];
883 (*pollfd
)[1].events
= POLLIN
;
885 /* Start count after the two pipes below */
887 for (i
= 2; i
< old_nb_fd
; i
++) {
888 /* Add to new pollfd */
889 if (old_pollfd
[i
].fd
!= -1) {
890 (*pollfd
)[count
].fd
= old_pollfd
[i
].fd
;
891 (*pollfd
)[count
].events
= POLLHUP
| POLLNVAL
| POLLERR
;
896 ERR("Updating poll fd wrong size");
901 /* Destroy old pollfd */
904 DBG("Apps cmd pollfd realloc of size %d", nb_fd
);
910 /* Destroy old pollfd */
916 * This thread manage application communication.
918 static void *thread_manage_apps(void *data
)
921 unsigned int nb_fd
= 2;
922 int update_poll_flag
= 1;
923 struct pollfd
*pollfd
= NULL
;
924 struct ust_command ust_cmd
;
926 DBG("[thread] Manage application started");
931 /* See if we have a valid socket to add to pollfd */
932 if (ust_cmd
.sock
!= -1) {
934 update_poll_flag
= 1;
937 /* The pollfd struct must be updated */
938 if (update_poll_flag
) {
939 ret
= update_apps_cmd_pollfd(nb_fd
, ARRAY_SIZE(pollfd
), &pollfd
);
941 /* malloc failed so we quit */
945 if (ust_cmd
.sock
!= -1) {
946 /* Update pollfd with the new UST socket */
947 DBG("Adding sock %d to apps cmd pollfd", ust_cmd
.sock
);
948 pollfd
[nb_fd
- 1].fd
= ust_cmd
.sock
;
949 pollfd
[nb_fd
- 1].events
= POLLHUP
| POLLNVAL
| POLLERR
;
954 DBG("Apps thread polling on %d fds", nb_fd
);
956 /* Inifinite blocking call, waiting for transmission */
957 ret
= poll(pollfd
, nb_fd
, -1);
959 perror("poll apps thread");
963 /* Thread quit pipe has been closed. Killing thread. */
964 if (pollfd
[0].revents
== POLLNVAL
) {
967 /* apps_cmd_pipe pipe events */
968 switch (pollfd
[1].revents
) {
970 ERR("Apps command pipe poll error");
974 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
975 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
976 perror("read apps cmd pipe");
980 /* Register applicaton to the session daemon */
981 ret
= register_traceable_app(&ust_cmd
.reg_msg
, ust_cmd
.sock
);
983 /* Only critical ENOMEM error can be returned here */
987 ret
= ustctl_register_done(ust_cmd
.sock
);
990 * If the registration is not possible, we simply unregister
991 * the apps and continue
993 unregister_traceable_app(ust_cmd
.sock
);
1000 for (i
= 2; i
< count
; i
++) {
1001 /* Apps socket is closed/hungup */
1002 switch (pollfd
[i
].revents
) {
1007 unregister_traceable_app(pollfd
[i
].fd
);
1008 /* Indicate to remove this fd from the pollfd */
1015 if (nb_fd
!= count
) {
1016 update_poll_flag
= 1;
1021 DBG("Application communication apps dying");
1022 close(apps_cmd_pipe
[0]);
1023 close(apps_cmd_pipe
[1]);
1031 * Dispatch request from the registration threads to the application
1032 * communication thread.
1034 static void *thread_dispatch_ust_registration(void *data
)
1037 struct cds_wfq_node
*node
;
1038 struct ust_command
*ust_cmd
= NULL
;
1040 DBG("[thread] Dispatch UST command started");
1042 while (!dispatch_thread_exit
) {
1043 /* Atomically prepare the queue futex */
1044 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1047 /* Dequeue command for registration */
1048 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1050 DBG("Waked up but nothing in the UST command queue");
1051 /* Continue thread execution */
1055 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1057 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1058 " gid:%d sock:%d name:%s (version %d.%d)",
1059 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1060 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1061 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1062 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1064 * Inform apps thread of the new application registration. This
1065 * call is blocking so we can be assured that the data will be read
1066 * at some point in time or wait to the end of the world :)
1068 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1069 sizeof(struct ust_command
));
1071 perror("write apps cmd pipe");
1072 if (errno
== EBADF
) {
1074 * We can't inform the application thread to process
1075 * registration. We will exit or else application
1076 * registration will not occur and tracing will never
1083 } while (node
!= NULL
);
1085 /* Futex wait on queue. Blocking call on futex() */
1086 futex_nto1_wait(&ust_cmd_queue
.futex
);
1090 DBG("Dispatch thread dying");
1095 * This thread manage application registration.
1097 static void *thread_registration_apps(void *data
)
1100 struct pollfd pollfd
[2];
1102 * Get allocated in this thread, enqueued to a global queue, dequeued and
1103 * freed in the manage apps thread.
1105 struct ust_command
*ust_cmd
= NULL
;
1107 tracepoint(sessiond_th_apps_start
);
1109 DBG("[thread] Manage application registration started");
1111 ret
= lttcomm_listen_unix_sock(apps_sock
);
1116 /* First fd is always the quit pipe */
1117 pollfd
[0].fd
= thread_quit_pipe
[0];
1120 pollfd
[1].fd
= apps_sock
;
1121 pollfd
[1].events
= POLLIN
;
1123 /* Notify all applications to register */
1124 ret
= notify_ust_apps(1);
1126 ERR("Failed to notify applications or create the wait shared memory.\n"
1127 "Execution continues but there might be problem for already running\n"
1128 "applications that wishes to register.");
1132 DBG("Accepting application registration");
1134 tracepoint(sessiond_th_apps_poll
);
1136 /* Inifinite blocking call, waiting for transmission */
1137 ret
= poll(pollfd
, 2, -1);
1139 perror("poll register apps thread");
1143 /* Thread quit pipe has been closed. Killing thread. */
1144 if (pollfd
[0].revents
== POLLNVAL
) {
1146 } else if (pollfd
[1].revents
== POLLERR
) {
1147 ERR("Register apps socket poll error");
1151 sock
= lttcomm_accept_unix_sock(apps_sock
);
1156 /* Create UST registration command for enqueuing */
1157 ust_cmd
= malloc(sizeof(struct ust_command
));
1158 if (ust_cmd
== NULL
) {
1159 perror("ust command malloc");
1164 * Using message-based transmissions to ensure we don't have to deal
1165 * with partially received messages.
1167 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1168 sizeof(struct ust_register_msg
));
1169 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1171 perror("lttcomm_recv_unix_sock register apps");
1173 ERR("Wrong size received on apps register");
1180 ust_cmd
->sock
= sock
;
1182 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1183 " gid:%d sock:%d name:%s (version %d.%d)",
1184 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1185 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1186 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1187 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1189 * Lock free enqueue the registration request.
1190 * The red pill has been taken! This apps will be part of the *system*
1192 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1195 * Wake the registration queue futex.
1196 * Implicit memory barrier with the exchange in cds_wfq_enqueue.
1198 futex_nto1_wake(&ust_cmd_queue
.futex
);
1202 DBG("UST Registration thread dying");
1204 /* Notify that the registration thread is gone */
1210 unlink(apps_unix_sock_path
);
1216 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
1217 * exec or it will fails.
1219 static int spawn_kconsumerd_thread(void)
1223 /* Setup semaphore */
1224 sem_init(&kconsumerd_sem
, 0, 0);
1226 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
1228 perror("pthread_create kconsumerd");
1232 /* Wait for the kconsumerd thread to be ready */
1233 sem_wait(&kconsumerd_sem
);
1235 if (kconsumerd_pid
== 0) {
1236 ERR("Kconsumerd did not start");
1243 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1248 * Join kernel consumer thread
1250 static int join_kconsumerd_thread(void)
1255 if (kconsumerd_pid
!= 0) {
1256 ret
= kill(kconsumerd_pid
, SIGTERM
);
1258 ERR("Error killing kconsumerd");
1261 return pthread_join(kconsumerd_thread
, &status
);
1268 * Fork and exec a kernel consumer daemon (kconsumerd).
1270 * Return pid if successful else -1.
1272 static pid_t
spawn_kconsumerd(void)
1276 const char *verbosity
;
1278 DBG("Spawning kconsumerd");
1285 if (opt_verbose
> 1 || opt_verbose_kconsumerd
) {
1286 verbosity
= "--verbose";
1288 verbosity
= "--quiet";
1290 execl(INSTALL_BIN_PATH
"/ltt-kconsumerd", "ltt-kconsumerd", verbosity
, NULL
);
1292 perror("kernel start consumer exec");
1295 } else if (pid
> 0) {
1299 perror("kernel start consumer fork");
1309 * Spawn the kconsumerd daemon and session daemon thread.
1311 static int start_kconsumerd(void)
1315 pthread_mutex_lock(&kconsumerd_pid_mutex
);
1316 if (kconsumerd_pid
!= 0) {
1317 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1321 ret
= spawn_kconsumerd();
1323 ERR("Spawning kconsumerd failed");
1324 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1325 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1329 /* Setting up the global kconsumerd_pid */
1330 kconsumerd_pid
= ret
;
1331 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1333 DBG("Kconsumerd pid %d", ret
);
1335 DBG("Spawning kconsumerd thread");
1336 ret
= spawn_kconsumerd_thread();
1338 ERR("Fatal error spawning kconsumerd thread");
1350 * modprobe_kernel_modules
1352 static int modprobe_kernel_modules(void)
1357 for (i
= 0; i
< ARRAY_SIZE(kernel_modules_list
); i
++) {
1358 ret
= snprintf(modprobe
, sizeof(modprobe
),
1359 "/sbin/modprobe %s%s",
1360 kernel_modules_list
[i
].required
? "" : "--quiet ",
1361 kernel_modules_list
[i
].name
);
1363 perror("snprintf modprobe");
1366 modprobe
[sizeof(modprobe
) - 1] = '\0';
1367 ret
= system(modprobe
);
1369 ERR("Unable to launch modprobe for module %s",
1370 kernel_modules_list
[i
].name
);
1371 } else if (kernel_modules_list
[i
].required
1372 && WEXITSTATUS(ret
) != 0) {
1373 ERR("Unable to load module %s",
1374 kernel_modules_list
[i
].name
);
1376 DBG("Modprobe successfully %s",
1377 kernel_modules_list
[i
].name
);
1388 static int mount_debugfs(char *path
)
1391 char *type
= "debugfs";
1393 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, geteuid(), getegid());
1398 ret
= mount(type
, path
, type
, 0, NULL
);
1400 perror("mount debugfs");
1404 DBG("Mounted debugfs successfully at %s", path
);
1411 * Setup necessary data for kernel tracer action.
1413 static void init_kernel_tracer(void)
1416 char *proc_mounts
= "/proc/mounts";
1418 char *debugfs_path
= NULL
, *lttng_path
;
1421 /* Detect debugfs */
1422 fp
= fopen(proc_mounts
, "r");
1424 ERR("Unable to probe %s", proc_mounts
);
1428 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1429 if (strstr(line
, "debugfs") != NULL
) {
1430 /* Remove first string */
1432 /* Dup string here so we can reuse line later on */
1433 debugfs_path
= strdup(strtok(NULL
, " "));
1434 DBG("Got debugfs path : %s", debugfs_path
);
1441 /* Mount debugfs if needded */
1442 if (debugfs_path
== NULL
) {
1443 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1445 perror("asprintf debugfs path");
1448 ret
= mount_debugfs(debugfs_path
);
1454 /* Modprobe lttng kernel modules */
1455 ret
= modprobe_kernel_modules();
1460 /* Setup lttng kernel path */
1461 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1463 perror("asprintf lttng path");
1467 /* Open debugfs lttng */
1468 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1469 if (kernel_tracer_fd
< 0) {
1470 DBG("Failed to open %s", lttng_path
);
1476 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1486 WARN("No kernel tracer available");
1487 kernel_tracer_fd
= 0;
1492 * Start tracing by creating trace directory and sending FDs to the kernel
1495 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1499 if (session
->kconsumer_fds_sent
== 0) {
1501 * Assign default kernel consumer if no consumer assigned to the kernel
1502 * session. At this point, it's NOT suppose to be 0 but this is an extra
1505 if (session
->consumer_fd
== 0) {
1506 session
->consumer_fd
= kconsumerd_cmd_sock
;
1509 ret
= send_kconsumerd_fds(session
);
1511 ERR("Send kconsumerd fds failed");
1512 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1516 session
->kconsumer_fds_sent
= 1;
1524 * Notify kernel thread to update it's pollfd.
1526 static int notify_kernel_pollfd(void)
1530 /* Inform kernel thread of the new kernel channel */
1531 ret
= write(kernel_poll_pipe
[1], "!", 1);
1533 perror("write kernel poll pipe");
1540 * Allocate a channel structure and fill it.
1542 static struct lttng_channel
*init_default_channel(enum lttng_domain_type domain_type
,
1545 struct lttng_channel
*chan
;
1547 chan
= malloc(sizeof(struct lttng_channel
));
1549 perror("init channel malloc");
1553 if (snprintf(chan
->name
, NAME_MAX
, "%s", name
) < 0) {
1554 perror("snprintf channel name");
1558 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1559 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1560 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1562 switch (domain_type
) {
1563 case LTTNG_DOMAIN_KERNEL
:
1564 chan
->attr
.subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
1565 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1566 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1570 goto error
; /* Not implemented */
1581 * Create an UST session and add it to the session ust list.
1583 static int create_ust_session(pid_t pid
, struct ltt_session
*session
)
1586 struct ltt_ust_session
*lus
;
1588 DBG("Creating UST session");
1590 lus
= trace_ust_create_session(session
->path
, pid
);
1595 ret
= mkdir_recursive(lus
->path
, S_IRWXU
| S_IRWXG
,
1596 geteuid(), allowed_group());
1598 if (ret
!= -EEXIST
) {
1599 ERR("Trace directory creation error");
1604 /* Create session on the UST tracer */
1605 ret
= ustctl_create_session(lus
);
1618 * Create a kernel tracer session then create the default channel.
1620 static int create_kernel_session(struct ltt_session
*session
)
1624 DBG("Creating kernel session");
1626 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1628 ret
= LTTCOMM_KERN_SESS_FAIL
;
1632 /* Set kernel consumer socket fd */
1633 if (kconsumerd_cmd_sock
) {
1634 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
1637 ret
= mkdir_recursive(session
->kernel_session
->trace_path
,
1638 S_IRWXU
| S_IRWXG
, geteuid(), allowed_group());
1640 if (ret
!= -EEXIST
) {
1641 ERR("Trace directory creation error");
1651 * Using the session list, filled a lttng_session array to send back to the
1652 * client for session listing.
1654 * The session list lock MUST be acquired before calling this function. Use
1655 * lock_session_list() and unlock_session_list().
1657 static void list_lttng_sessions(struct lttng_session
*sessions
)
1660 struct ltt_session
*session
;
1662 DBG("Getting all available session");
1664 * Iterate over session list and append data after the control struct in
1667 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1668 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1669 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
1670 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1671 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
1677 * Fill lttng_channel array of all channels.
1679 static void list_lttng_channels(struct ltt_session
*session
,
1680 struct lttng_channel
*channels
)
1683 struct ltt_kernel_channel
*kchan
;
1685 DBG("Listing channels for session %s", session
->name
);
1687 /* Kernel channels */
1688 if (session
->kernel_session
!= NULL
) {
1689 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
1690 /* Copy lttng_channel struct to array */
1691 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1692 channels
[i
].enabled
= kchan
->enabled
;
1697 /* TODO: Missing UST listing */
1701 * Fill lttng_event array of all events in the channel.
1703 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1704 struct lttng_event
*events
)
1707 * TODO: This is ONLY kernel. Need UST support.
1710 struct ltt_kernel_event
*event
;
1712 DBG("Listing events for channel %s", kchan
->channel
->name
);
1714 /* Kernel channels */
1715 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1716 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1717 events
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1718 events
[i
].enabled
= event
->enabled
;
1719 switch (event
->event
->instrumentation
) {
1720 case LTTNG_KERNEL_TRACEPOINT
:
1721 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1723 case LTTNG_KERNEL_KPROBE
:
1724 case LTTNG_KERNEL_KRETPROBE
:
1725 events
[i
].type
= LTTNG_EVENT_PROBE
;
1726 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
1727 sizeof(struct lttng_kernel_kprobe
));
1729 case LTTNG_KERNEL_FUNCTION
:
1730 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
1731 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
1732 sizeof(struct lttng_kernel_function
));
1740 * Process the command requested by the lttng client within the command
1741 * context structure. This function make sure that the return structure (llm)
1742 * is set and ready for transmission before returning.
1744 * Return any error encountered or 0 for success.
1746 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1748 int ret
= LTTCOMM_OK
;
1750 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1753 * Commands that DO NOT need a session.
1755 switch (cmd_ctx
->lsm
->cmd_type
) {
1756 case LTTNG_CREATE_SESSION
:
1757 case LTTNG_LIST_SESSIONS
:
1758 case LTTNG_LIST_TRACEPOINTS
:
1759 case LTTNG_CALIBRATE
:
1762 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
1763 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session
.name
);
1764 if (cmd_ctx
->session
== NULL
) {
1765 /* If session name not found */
1766 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
1767 ret
= LTTCOMM_SESS_NOT_FOUND
;
1768 } else { /* If no session name specified */
1769 ret
= LTTCOMM_SELECT_SESS
;
1773 /* Acquire lock for the session */
1774 lock_session(cmd_ctx
->session
);
1780 * Check domain type for specific "pre-action".
1782 switch (cmd_ctx
->lsm
->domain
.type
) {
1783 case LTTNG_DOMAIN_KERNEL
:
1784 /* Kernel tracer check */
1785 if (kernel_tracer_fd
== 0) {
1786 init_kernel_tracer();
1787 if (kernel_tracer_fd
== 0) {
1788 ret
= LTTCOMM_KERN_NA
;
1792 /* Need a session for kernel command */
1793 switch (cmd_ctx
->lsm
->cmd_type
) {
1794 case LTTNG_CALIBRATE
:
1795 case LTTNG_CREATE_SESSION
:
1796 case LTTNG_LIST_SESSIONS
:
1797 case LTTNG_LIST_TRACEPOINTS
:
1800 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1801 ret
= create_kernel_session(cmd_ctx
->session
);
1803 ret
= LTTCOMM_KERN_SESS_FAIL
;
1806 /* Start the kernel consumer daemon */
1807 if (kconsumerd_pid
== 0 &&
1808 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1809 ret
= start_kconsumerd();
1817 case LTTNG_DOMAIN_UST_PID
:
1823 /* Process by command type */
1824 switch (cmd_ctx
->lsm
->cmd_type
) {
1825 case LTTNG_ADD_CONTEXT
:
1827 struct lttng_kernel_context kctx
;
1829 /* Setup lttng message with no payload */
1830 ret
= setup_lttng_msg(cmd_ctx
, 0);
1835 switch (cmd_ctx
->lsm
->domain
.type
) {
1836 case LTTNG_DOMAIN_KERNEL
:
1837 /* Create Kernel context */
1838 kctx
.ctx
= cmd_ctx
->lsm
->u
.context
.ctx
.ctx
;
1839 kctx
.u
.perf_counter
.type
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.type
;
1840 kctx
.u
.perf_counter
.config
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.config
;
1841 strncpy(kctx
.u
.perf_counter
.name
,
1842 cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.name
,
1843 LTTNG_SYMBOL_NAME_LEN
);
1844 kctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1846 /* Add kernel context to kernel tracer. See context.c */
1847 ret
= add_kernel_context(cmd_ctx
->session
->kernel_session
, &kctx
,
1848 cmd_ctx
->lsm
->u
.context
.event_name
,
1849 cmd_ctx
->lsm
->u
.context
.channel_name
);
1850 if (ret
!= LTTCOMM_OK
) {
1855 /* TODO: Userspace tracing */
1856 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1863 case LTTNG_DISABLE_CHANNEL
:
1865 struct ltt_kernel_channel
*kchan
;
1867 /* Setup lttng message with no payload */
1868 ret
= setup_lttng_msg(cmd_ctx
, 0);
1873 switch (cmd_ctx
->lsm
->domain
.type
) {
1874 case LTTNG_DOMAIN_KERNEL
:
1875 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1876 cmd_ctx
->session
->kernel_session
);
1877 if (kchan
== NULL
) {
1878 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1880 } else if (kchan
->enabled
== 1) {
1881 ret
= kernel_disable_channel(kchan
);
1883 if (ret
!= EEXIST
) {
1884 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1889 kernel_wait_quiescent(kernel_tracer_fd
);
1892 /* TODO: Userspace tracing */
1893 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1900 case LTTNG_DISABLE_EVENT
:
1902 struct ltt_kernel_channel
*kchan
;
1903 struct ltt_kernel_event
*kevent
;
1905 /* Setup lttng message with no payload */
1906 ret
= setup_lttng_msg(cmd_ctx
, 0);
1911 switch (cmd_ctx
->lsm
->domain
.type
) {
1912 case LTTNG_DOMAIN_KERNEL
:
1913 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1914 cmd_ctx
->session
->kernel_session
);
1915 if (kchan
== NULL
) {
1916 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1920 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, kchan
);
1921 if (kevent
!= NULL
) {
1922 DBG("Disabling kernel event %s for channel %s.", kevent
->event
->name
,
1923 kchan
->channel
->name
);
1924 ret
= kernel_disable_event(kevent
);
1926 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1931 kernel_wait_quiescent(kernel_tracer_fd
);
1934 /* TODO: Userspace tracing */
1935 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1942 case LTTNG_DISABLE_ALL_EVENT
:
1944 struct ltt_kernel_channel
*kchan
;
1945 struct ltt_kernel_event
*kevent
;
1947 /* Setup lttng message with no payload */
1948 ret
= setup_lttng_msg(cmd_ctx
, 0);
1953 switch (cmd_ctx
->lsm
->domain
.type
) {
1954 case LTTNG_DOMAIN_KERNEL
:
1955 DBG("Disabling all enabled kernel events");
1956 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1957 cmd_ctx
->session
->kernel_session
);
1958 if (kchan
== NULL
) {
1959 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1963 /* For each event in the kernel session */
1964 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1965 DBG("Disabling kernel event %s for channel %s.",
1966 kevent
->event
->name
, kchan
->channel
->name
);
1967 ret
= kernel_disable_event(kevent
);
1973 /* Quiescent wait after event disable */
1974 kernel_wait_quiescent(kernel_tracer_fd
);
1977 /* TODO: Userspace tracing */
1978 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1985 case LTTNG_ENABLE_CHANNEL
:
1987 struct ltt_kernel_channel
*kchan
;
1989 /* Setup lttng message with no payload */
1990 ret
= setup_lttng_msg(cmd_ctx
, 0);
1995 switch (cmd_ctx
->lsm
->domain
.type
) {
1996 case LTTNG_DOMAIN_KERNEL
:
1997 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1998 cmd_ctx
->session
->kernel_session
);
1999 if (kchan
== NULL
) {
2000 /* Channel not found, creating it */
2001 DBG("Creating kernel channel");
2003 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2004 &cmd_ctx
->lsm
->u
.channel
.chan
,
2005 cmd_ctx
->session
->kernel_session
->trace_path
);
2007 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2011 /* Notify kernel thread that there is a new channel */
2012 ret
= notify_kernel_pollfd();
2014 ret
= LTTCOMM_FATAL
;
2017 } else if (kchan
->enabled
== 0) {
2018 ret
= kernel_enable_channel(kchan
);
2020 if (ret
!= EEXIST
) {
2021 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
2027 kernel_wait_quiescent(kernel_tracer_fd
);
2029 case LTTNG_DOMAIN_UST_PID
:
2033 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2040 case LTTNG_ENABLE_EVENT
:
2043 struct ltt_kernel_channel
*kchan
;
2044 struct ltt_kernel_event
*kevent
;
2045 struct lttng_channel
*chan
;
2047 /* Setup lttng message with no payload */
2048 ret
= setup_lttng_msg(cmd_ctx
, 0);
2053 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2055 switch (cmd_ctx
->lsm
->domain
.type
) {
2056 case LTTNG_DOMAIN_KERNEL
:
2057 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2058 cmd_ctx
->session
->kernel_session
);
2059 if (kchan
== NULL
) {
2060 DBG("Channel not found. Creating channel %s", channel_name
);
2062 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2064 ret
= LTTCOMM_FATAL
;
2068 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2069 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2071 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2074 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2075 cmd_ctx
->session
->kernel_session
);
2076 if (kchan
== NULL
) {
2077 ERR("Channel %s not found after creation. Internal error, giving up.",
2079 ret
= LTTCOMM_FATAL
;
2084 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, kchan
);
2085 if (kevent
== NULL
) {
2086 DBG("Creating kernel event %s for channel %s.",
2087 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
2088 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, kchan
);
2090 DBG("Enabling kernel event %s for channel %s.",
2091 kevent
->event
->name
, channel_name
);
2092 ret
= kernel_enable_event(kevent
);
2093 if (ret
== -EEXIST
) {
2094 ret
= LTTCOMM_KERN_EVENT_EXIST
;
2100 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2104 kernel_wait_quiescent(kernel_tracer_fd
);
2107 /* TODO: Userspace tracing */
2108 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2114 case LTTNG_ENABLE_ALL_EVENT
:
2118 struct ltt_kernel_channel
*kchan
;
2119 struct ltt_kernel_event
*kevent
;
2120 struct lttng_event
*event_list
;
2121 struct lttng_channel
*chan
;
2123 /* Setup lttng message with no payload */
2124 ret
= setup_lttng_msg(cmd_ctx
, 0);
2129 DBG("Enabling all kernel event");
2131 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2133 switch (cmd_ctx
->lsm
->domain
.type
) {
2134 case LTTNG_DOMAIN_KERNEL
:
2135 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2136 cmd_ctx
->session
->kernel_session
);
2137 if (kchan
== NULL
) {
2138 DBG("Channel not found. Creating channel %s", channel_name
);
2140 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2142 ret
= LTTCOMM_FATAL
;
2146 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2147 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2149 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2152 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2153 cmd_ctx
->session
->kernel_session
);
2154 if (kchan
== NULL
) {
2155 ERR("Channel %s not found after creation. Internal error, giving up.",
2157 ret
= LTTCOMM_FATAL
;
2162 /* For each event in the kernel session */
2163 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
2164 DBG("Enabling kernel event %s for channel %s.",
2165 kevent
->event
->name
, channel_name
);
2166 ret
= kernel_enable_event(kevent
);
2172 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
2174 ret
= LTTCOMM_KERN_LIST_FAIL
;
2178 for (i
= 0; i
< size
; i
++) {
2179 kevent
= trace_kernel_get_event_by_name(event_list
[i
].name
, kchan
);
2180 if (kevent
== NULL
) {
2181 /* Default event type for enable all */
2182 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2183 /* Enable each single tracepoint event */
2184 ret
= kernel_create_event(&event_list
[i
], kchan
);
2186 /* Ignore error here and continue */
2193 /* Quiescent wait after event enable */
2194 kernel_wait_quiescent(kernel_tracer_fd
);
2197 /* TODO: Userspace tracing */
2198 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2205 case LTTNG_LIST_TRACEPOINTS
:
2207 struct lttng_event
*events
;
2208 ssize_t nb_events
= 0;
2210 switch (cmd_ctx
->lsm
->domain
.type
) {
2211 case LTTNG_DOMAIN_KERNEL
:
2212 DBG("Listing kernel events");
2213 nb_events
= kernel_list_events(kernel_tracer_fd
, &events
);
2214 if (nb_events
< 0) {
2215 ret
= LTTCOMM_KERN_LIST_FAIL
;
2220 /* TODO: Userspace listing */
2221 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2226 * Setup lttng message with payload size set to the event list size in
2227 * bytes and then copy list into the llm payload.
2229 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2235 /* Copy event list into message payload */
2236 memcpy(cmd_ctx
->llm
->payload
, events
,
2237 sizeof(struct lttng_event
) * nb_events
);
2244 case LTTNG_START_TRACE
:
2246 struct ltt_kernel_channel
*chan
;
2248 /* Setup lttng message with no payload */
2249 ret
= setup_lttng_msg(cmd_ctx
, 0);
2254 /* Kernel tracing */
2255 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2256 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
2257 DBG("Open kernel metadata");
2258 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
2259 cmd_ctx
->session
->kernel_session
->trace_path
);
2261 ret
= LTTCOMM_KERN_META_FAIL
;
2266 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2267 DBG("Opening kernel metadata stream");
2268 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2269 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
2271 ERR("Kernel create metadata stream failed");
2272 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2278 /* For each channel */
2279 cds_list_for_each_entry(chan
,
2280 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2281 if (chan
->stream_count
== 0) {
2282 ret
= kernel_open_channel_stream(chan
);
2284 ERR("Kernel create channel stream failed");
2285 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2288 /* Update the stream global counter */
2289 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
2293 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
2295 ret
= LTTCOMM_KERN_START_FAIL
;
2299 DBG("Start kernel tracing");
2300 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
2302 ERR("Kernel start session failed");
2303 ret
= LTTCOMM_KERN_START_FAIL
;
2307 /* Quiescent wait after starting trace */
2308 kernel_wait_quiescent(kernel_tracer_fd
);
2311 /* TODO: Start all UST traces */
2316 case LTTNG_STOP_TRACE
:
2318 struct ltt_kernel_channel
*chan
;
2319 /* Setup lttng message with no payload */
2320 ret
= setup_lttng_msg(cmd_ctx
, 0);
2326 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2327 DBG("Stop kernel tracing");
2329 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
2331 ERR("Kernel metadata flush failed");
2334 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2335 ret
= kernel_flush_buffer(chan
);
2337 ERR("Kernel flush buffer error");
2341 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
2343 ERR("Kernel stop session failed");
2344 ret
= LTTCOMM_KERN_STOP_FAIL
;
2348 /* Quiescent wait after stopping trace */
2349 kernel_wait_quiescent(kernel_tracer_fd
);
2352 /* TODO : User-space tracer */
2357 case LTTNG_CREATE_SESSION
:
2359 /* Setup lttng message with no payload */
2360 ret
= setup_lttng_msg(cmd_ctx
, 0);
2365 tracepoint(create_session_start
);
2366 ret
= create_session(cmd_ctx
->lsm
->session
.name
, cmd_ctx
->lsm
->session
.path
);
2367 tracepoint(create_session_end
);
2369 if (ret
== -EEXIST
) {
2370 ret
= LTTCOMM_EXIST_SESS
;
2372 ret
= LTTCOMM_FATAL
;
2380 case LTTNG_DESTROY_SESSION
:
2382 /* Setup lttng message with no payload */
2383 ret
= setup_lttng_msg(cmd_ctx
, 0);
2388 /* Clean kernel session teardown */
2389 teardown_kernel_session(cmd_ctx
->session
);
2391 tracepoint(destroy_session_start
);
2392 ret
= destroy_session(cmd_ctx
->lsm
->session
.name
);
2393 tracepoint(destroy_session_end
);
2395 ret
= LTTCOMM_FATAL
;
2400 * Must notify the kernel thread here to update it's pollfd in order to
2401 * remove the channel(s)' fd just destroyed.
2403 ret
= notify_kernel_pollfd();
2405 ret
= LTTCOMM_FATAL
;
2412 case LTTNG_LIST_DOMAINS
:
2416 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2420 nb_dom
+= cmd_ctx
->session
->ust_session_list
.count
;
2422 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_domain
) * nb_dom
);
2427 ((struct lttng_domain
*)(cmd_ctx
->llm
->payload
))[0].type
=
2428 LTTNG_DOMAIN_KERNEL
;
2430 /* TODO: User-space tracer domain support */
2434 case LTTNG_LIST_CHANNELS
:
2437 * TODO: Only kernel channels are listed here. UST listing
2438 * is needed on lttng-ust 2.0 release.
2441 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2442 nb_chan
+= cmd_ctx
->session
->kernel_session
->channel_count
;
2445 ret
= setup_lttng_msg(cmd_ctx
,
2446 sizeof(struct lttng_channel
) * nb_chan
);
2451 list_lttng_channels(cmd_ctx
->session
,
2452 (struct lttng_channel
*)(cmd_ctx
->llm
->payload
));
2457 case LTTNG_LIST_EVENTS
:
2460 * TODO: Only kernel events are listed here. UST listing
2461 * is needed on lttng-ust 2.0 release.
2463 size_t nb_event
= 0;
2464 struct ltt_kernel_channel
*kchan
= NULL
;
2466 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2467 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.list
.channel_name
,
2468 cmd_ctx
->session
->kernel_session
);
2469 if (kchan
== NULL
) {
2470 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2473 nb_event
+= kchan
->event_count
;
2476 ret
= setup_lttng_msg(cmd_ctx
,
2477 sizeof(struct lttng_event
) * nb_event
);
2482 DBG("Listing events (%zu events)", nb_event
);
2484 list_lttng_events(kchan
,
2485 (struct lttng_event
*)(cmd_ctx
->llm
->payload
));
2490 case LTTNG_LIST_SESSIONS
:
2492 lock_session_list();
2494 if (session_list_ptr
->count
== 0) {
2495 ret
= LTTCOMM_NO_SESSION
;
2496 unlock_session_list();
2500 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
2501 session_list_ptr
->count
);
2503 unlock_session_list();
2507 /* Filled the session array */
2508 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
2510 unlock_session_list();
2515 case LTTNG_CALIBRATE
:
2517 /* Setup lttng message with no payload */
2518 ret
= setup_lttng_msg(cmd_ctx
, 0);
2523 switch (cmd_ctx
->lsm
->domain
.type
) {
2524 case LTTNG_DOMAIN_KERNEL
:
2526 struct lttng_kernel_calibrate kcalibrate
;
2528 kcalibrate
.type
= cmd_ctx
->lsm
->u
.calibrate
.type
;
2529 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2531 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2537 /* TODO: Userspace tracing */
2538 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2544 case LTTNG_REGISTER_CONSUMER
:
2548 /* Setup lttng message with no payload */
2549 ret
= setup_lttng_msg(cmd_ctx
, 0);
2554 switch (cmd_ctx
->lsm
->domain
.type
) {
2555 case LTTNG_DOMAIN_KERNEL
:
2557 /* Can't register a consumer if there is already one */
2558 if (cmd_ctx
->session
->kernel_session
->consumer_fd
!= 0) {
2559 ret
= LTTCOMM_CONNECT_FAIL
;
2563 sock
= lttcomm_connect_unix_sock(cmd_ctx
->lsm
->u
.reg
.path
);
2565 ret
= LTTCOMM_CONNECT_FAIL
;
2569 cmd_ctx
->session
->kernel_session
->consumer_fd
= sock
;
2573 /* TODO: Userspace tracing */
2574 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2583 /* Undefined command */
2584 ret
= setup_lttng_msg(cmd_ctx
, 0);
2593 /* Set return code */
2594 cmd_ctx
->llm
->ret_code
= ret
;
2596 if (cmd_ctx
->session
) {
2597 unlock_session(cmd_ctx
->session
);
2603 if (cmd_ctx
->llm
== NULL
) {
2604 DBG("Missing llm structure. Allocating one.");
2605 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2609 /* Notify client of error */
2610 cmd_ctx
->llm
->ret_code
= ret
;
2613 if (cmd_ctx
->session
) {
2614 unlock_session(cmd_ctx
->session
);
2620 * This thread manage all clients request using the unix client socket for
2623 static void *thread_manage_clients(void *data
)
2626 struct command_ctx
*cmd_ctx
= NULL
;
2627 struct pollfd pollfd
[2];
2629 tracepoint(sessiond_th_cli_start
);
2631 DBG("[thread] Manage client started");
2633 ret
= lttcomm_listen_unix_sock(client_sock
);
2638 /* First fd is always the quit pipe */
2639 pollfd
[0].fd
= thread_quit_pipe
[0];
2642 pollfd
[1].fd
= client_sock
;
2643 pollfd
[1].events
= POLLIN
;
2645 /* Notify parent pid that we are ready
2646 * to accept command for client side.
2648 if (opt_sig_parent
) {
2649 kill(ppid
, SIGCHLD
);
2653 DBG("Accepting client command ...");
2655 tracepoint(sessiond_th_cli_poll
);
2657 /* Inifinite blocking call, waiting for transmission */
2658 ret
= poll(pollfd
, 2, -1);
2660 perror("poll client thread");
2664 /* Thread quit pipe has been closed. Killing thread. */
2665 if (pollfd
[0].revents
== POLLNVAL
) {
2667 } else if (pollfd
[1].revents
== POLLERR
) {
2668 ERR("Client socket poll error");
2672 sock
= lttcomm_accept_unix_sock(client_sock
);
2677 /* Allocate context command to process the client request */
2678 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2680 /* Allocate data buffer for reception */
2681 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2682 cmd_ctx
->llm
= NULL
;
2683 cmd_ctx
->session
= NULL
;
2686 * Data is received from the lttng client. The struct
2687 * lttcomm_session_msg (lsm) contains the command and data request of
2690 DBG("Receiving data from client ...");
2691 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2696 // TODO: Validate cmd_ctx including sanity check for security purpose.
2699 * This function dispatch the work to the kernel or userspace tracer
2700 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2701 * informations for the client. The command context struct contains
2702 * everything this function may needs.
2704 ret
= process_client_msg(cmd_ctx
);
2706 /* TODO: Inform client somehow of the fatal error. At this point,
2707 * ret < 0 means that a malloc failed (ENOMEM). */
2708 /* Error detected but still accept command */
2709 clean_command_ctx(&cmd_ctx
);
2713 DBG("Sending response (size: %d, retcode: %d)",
2714 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2715 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2717 ERR("Failed to send data back to client");
2720 clean_command_ctx(&cmd_ctx
);
2722 /* End of transmission */
2727 DBG("Client thread dying");
2735 unlink(client_unix_sock_path
);
2737 clean_command_ctx(&cmd_ctx
);
2743 * usage function on stderr
2745 static void usage(void)
2747 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2748 fprintf(stderr
, " -h, --help Display this usage.\n");
2749 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2750 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2751 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2752 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2753 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2754 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2755 fprintf(stderr
, " -V, --version Show version number.\n");
2756 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2757 fprintf(stderr
, " -q, --quiet No output at all.\n");
2758 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2759 fprintf(stderr
, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2763 * daemon argument parsing
2765 static int parse_args(int argc
, char **argv
)
2769 static struct option long_options
[] = {
2770 { "client-sock", 1, 0, 'c' },
2771 { "apps-sock", 1, 0, 'a' },
2772 { "kconsumerd-cmd-sock", 1, 0, 0 },
2773 { "kconsumerd-err-sock", 1, 0, 0 },
2774 { "daemonize", 0, 0, 'd' },
2775 { "sig-parent", 0, 0, 'S' },
2776 { "help", 0, 0, 'h' },
2777 { "group", 1, 0, 'g' },
2778 { "version", 0, 0, 'V' },
2779 { "quiet", 0, 0, 'q' },
2780 { "verbose", 0, 0, 'v' },
2781 { "verbose-kconsumerd", 0, 0, 'Z' },
2786 int option_index
= 0;
2787 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:Z", long_options
, &option_index
);
2794 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2796 fprintf(stderr
, " with arg %s\n", optarg
);
2800 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2803 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2809 opt_tracing_group
= strdup(optarg
);
2815 fprintf(stdout
, "%s\n", VERSION
);
2821 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2824 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2830 /* Verbose level can increase using multiple -v */
2834 opt_verbose_kconsumerd
+= 1;
2837 /* Unknown option or other error.
2838 * Error is printed by getopt, just return */
2847 * Creates the two needed socket by the daemon.
2848 * apps_sock - The communication socket for all UST apps.
2849 * client_sock - The communication of the cli tool (lttng).
2851 static int init_daemon_socket(void)
2856 old_umask
= umask(0);
2858 /* Create client tool unix socket */
2859 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2860 if (client_sock
< 0) {
2861 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2866 /* File permission MUST be 660 */
2867 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2869 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2874 /* Create the application unix socket */
2875 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2876 if (apps_sock
< 0) {
2877 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2882 /* File permission MUST be 666 */
2883 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2885 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2896 * Check if the global socket is available, and if a daemon is answering
2897 * at the other side. If yes, error is returned.
2899 static int check_existing_daemon(void)
2901 if (access(client_unix_sock_path
, F_OK
) < 0 &&
2902 access(apps_unix_sock_path
, F_OK
) < 0) {
2905 /* Is there anybody out there ? */
2906 if (lttng_session_daemon_alive()) {
2914 * Set the tracing group gid onto the client socket.
2916 * Race window between mkdir and chown is OK because we are going from more
2917 * permissive (root.root) to les permissive (root.tracing).
2919 static int set_permissions(void)
2924 gid
= allowed_group();
2927 WARN("No tracing group detected");
2930 ERR("Missing tracing group. Aborting execution.");
2936 /* Set lttng run dir */
2937 ret
= chown(LTTNG_RUNDIR
, 0, gid
);
2939 ERR("Unable to set group on " LTTNG_RUNDIR
);
2943 /* lttng client socket path */
2944 ret
= chown(client_unix_sock_path
, 0, gid
);
2946 ERR("Unable to set group on %s", client_unix_sock_path
);
2950 /* kconsumerd error socket path */
2951 ret
= chown(kconsumerd_err_unix_sock_path
, 0, gid
);
2953 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2957 DBG("All permissions are set");
2964 * Create the pipe used to wake up the kernel thread.
2966 static int create_kernel_poll_pipe(void)
2968 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2972 * Create the application command pipe to wake thread_manage_apps.
2974 static int create_apps_cmd_pipe(void)
2976 return pipe2(apps_cmd_pipe
, O_CLOEXEC
);
2980 * Create the lttng run directory needed for all global sockets and pipe.
2982 static int create_lttng_rundir(void)
2986 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2988 if (errno
!= EEXIST
) {
2989 ERR("Unable to create " LTTNG_RUNDIR
);
3001 * Setup sockets and directory needed by the kconsumerd communication with the
3004 static int set_kconsumerd_sockets(void)
3008 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
3009 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
3012 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
3013 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
3016 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
3018 if (errno
!= EEXIST
) {
3019 ERR("Failed to create " KCONSUMERD_PATH
);
3025 /* Create the kconsumerd error unix socket */
3026 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
3027 if (kconsumerd_err_sock
< 0) {
3028 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
3033 /* File permission MUST be 660 */
3034 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3036 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
3046 * Signal handler for the daemon
3048 * Simply stop all worker threads, leaving main() return gracefully
3049 * after joining all threads and calling cleanup().
3051 static void sighandler(int sig
)
3055 DBG("SIGPIPE catched");
3058 DBG("SIGINT catched");
3062 DBG("SIGTERM catched");
3071 * Setup signal handler for :
3072 * SIGINT, SIGTERM, SIGPIPE
3074 static int set_signal_handler(void)
3077 struct sigaction sa
;
3080 if ((ret
= sigemptyset(&sigset
)) < 0) {
3081 perror("sigemptyset");
3085 sa
.sa_handler
= sighandler
;
3086 sa
.sa_mask
= sigset
;
3088 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3089 perror("sigaction");
3093 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3094 perror("sigaction");
3098 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3099 perror("sigaction");
3103 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3109 * Set open files limit to unlimited. This daemon can open a large number of
3110 * file descriptors in order to consumer multiple kernel traces.
3112 static void set_ulimit(void)
3117 /* The kernel does not allowed an infinite limit for open files */
3118 lim
.rlim_cur
= 65535;
3119 lim
.rlim_max
= 65535;
3121 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3123 perror("failed to set open files limit");
3130 int main(int argc
, char **argv
)
3134 const char *home_path
;
3136 tracepoint(sessiond_boot_start
);
3138 /* Create thread quit pipe */
3139 if ((ret
= init_thread_quit_pipe()) < 0) {
3143 /* Parse arguments */
3145 if ((ret
= parse_args(argc
, argv
) < 0)) {
3158 /* Check if daemon is UID = 0 */
3159 is_root
= !getuid();
3162 ret
= create_lttng_rundir();
3167 if (strlen(apps_unix_sock_path
) == 0) {
3168 snprintf(apps_unix_sock_path
, PATH_MAX
,
3169 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
3172 if (strlen(client_unix_sock_path
) == 0) {
3173 snprintf(client_unix_sock_path
, PATH_MAX
,
3174 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
3177 /* Set global SHM for ust */
3178 if (strlen(wait_shm_path
) == 0) {
3179 snprintf(wait_shm_path
, PATH_MAX
,
3180 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
3183 home_path
= get_home_dir();
3184 if (home_path
== NULL
) {
3185 /* TODO: Add --socket PATH option */
3186 ERR("Can't get HOME directory for sockets creation.");
3191 if (strlen(apps_unix_sock_path
) == 0) {
3192 snprintf(apps_unix_sock_path
, PATH_MAX
,
3193 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
3196 /* Set the cli tool unix socket path */
3197 if (strlen(client_unix_sock_path
) == 0) {
3198 snprintf(client_unix_sock_path
, PATH_MAX
,
3199 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
3202 /* Set global SHM for ust */
3203 if (strlen(wait_shm_path
) == 0) {
3204 snprintf(wait_shm_path
, PATH_MAX
,
3205 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, geteuid());
3209 DBG("Client socket path %s", client_unix_sock_path
);
3210 DBG("Application socket path %s", apps_unix_sock_path
);
3213 * See if daemon already exist.
3215 if ((ret
= check_existing_daemon()) < 0) {
3216 ERR("Already running daemon.\n");
3218 * We do not goto exit because we must not cleanup()
3219 * because a daemon is already running.
3224 /* After this point, we can safely call cleanup() so goto error is used */
3227 * These actions must be executed as root. We do that *after* setting up
3228 * the sockets path because we MUST make the check for another daemon using
3229 * those paths *before* trying to set the kernel consumer sockets and init
3233 ret
= set_kconsumerd_sockets();
3238 /* Setup kernel tracer */
3239 init_kernel_tracer();
3241 /* Set ulimit for open files */
3245 if ((ret
= set_signal_handler()) < 0) {
3249 /* Setup the needed unix socket */
3250 if ((ret
= init_daemon_socket()) < 0) {
3254 /* Set credentials to socket */
3255 if (is_root
&& ((ret
= set_permissions()) < 0)) {
3259 /* Get parent pid if -S, --sig-parent is specified. */
3260 if (opt_sig_parent
) {
3264 /* Setup the kernel pipe for waking up the kernel thread */
3265 if ((ret
= create_kernel_poll_pipe()) < 0) {
3269 /* Setup the thread apps communication pipe. */
3270 if ((ret
= create_apps_cmd_pipe()) < 0) {
3274 /* Init UST command queue. */
3275 cds_wfq_init(&ust_cmd_queue
.queue
);
3278 * Get session list pointer. This pointer MUST NOT be free().
3279 * This list is statically declared in session.c
3281 session_list_ptr
= get_session_list();
3283 /* Create thread to manage the client socket */
3284 ret
= pthread_create(&client_thread
, NULL
,
3285 thread_manage_clients
, (void *) NULL
);
3287 perror("pthread_create clients");
3291 /* Create thread to dispatch registration */
3292 ret
= pthread_create(&dispatch_thread
, NULL
,
3293 thread_dispatch_ust_registration
, (void *) NULL
);
3295 perror("pthread_create dispatch");
3299 /* Create thread to manage application registration. */
3300 ret
= pthread_create(®_apps_thread
, NULL
,
3301 thread_registration_apps
, (void *) NULL
);
3303 perror("pthread_create registration");
3307 /* Create thread to manage application socket */
3308 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
3310 perror("pthread_create apps");
3314 /* Create kernel thread to manage kernel event */
3315 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
3317 perror("pthread_create kernel");
3321 tracepoint(sessiond_boot_end
);
3323 ret
= pthread_join(kernel_thread
, &status
);
3325 perror("pthread_join");
3326 goto error
; /* join error, exit without cleanup */
3330 ret
= pthread_join(apps_thread
, &status
);
3332 perror("pthread_join");
3333 goto error
; /* join error, exit without cleanup */
3337 ret
= pthread_join(reg_apps_thread
, &status
);
3339 perror("pthread_join");
3340 goto error
; /* join error, exit without cleanup */
3344 ret
= pthread_join(dispatch_thread
, &status
);
3346 perror("pthread_join");
3347 goto error
; /* join error, exit without cleanup */
3351 ret
= pthread_join(client_thread
, &status
);
3353 perror("pthread_join");
3354 goto error
; /* join error, exit without cleanup */
3357 ret
= join_kconsumerd_thread();
3359 perror("join_kconsumerd");
3360 goto error
; /* join error, exit without cleanup */
3366 * cleanup() is called when no other thread is running.