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 /* TODO: Something more elegant is needed but fine for now */
1110 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
1111 * for 32-bit vs 64-bit compat processes. */
1112 /* replicate in ust with version number */
1114 int reg
; /* 1:register, 0:unregister */
1119 DBG("[thread] Manage application registration started");
1121 ret
= lttcomm_listen_unix_sock(apps_sock
);
1126 /* First fd is always the quit pipe */
1127 pollfd
[0].fd
= thread_quit_pipe
[0];
1130 pollfd
[1].fd
= apps_sock
;
1131 pollfd
[1].events
= POLLIN
;
1133 /* Notify all applications to register */
1134 ret
= notify_ust_apps(1);
1136 ERR("Failed to notify applications or create the wait shared memory.\n"
1137 "Execution continues but there might be problem for already running\n"
1138 "applications that wishes to register.");
1142 DBG("Accepting application registration");
1144 tracepoint(sessiond_th_apps_poll
);
1146 /* Inifinite blocking call, waiting for transmission */
1147 ret
= poll(pollfd
, 2, -1);
1149 perror("poll register apps thread");
1153 /* Thread quit pipe has been closed. Killing thread. */
1154 if (pollfd
[0].revents
== POLLNVAL
) {
1156 } else if (pollfd
[1].revents
== POLLERR
) {
1157 ERR("Register apps socket poll error");
1161 sock
= lttcomm_accept_unix_sock(apps_sock
);
1166 /* Create UST registration command for enqueuing */
1167 ust_cmd
= malloc(sizeof(struct ust_command
));
1168 if (ust_cmd
== NULL
) {
1169 perror("ust command malloc");
1174 * Using message-based transmissions to ensure we don't have to deal
1175 * with partially received messages.
1177 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1178 sizeof(struct ust_register_msg
));
1179 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1181 perror("lttcomm_recv_unix_sock register apps");
1183 ERR("Wrong size received on apps register");
1190 ust_cmd
->sock
= sock
;
1192 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1193 " gid:%d sock:%d name:%s (version %d.%d)",
1194 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1195 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1196 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1197 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1199 * Lock free enqueue the registration request.
1200 * The red pill has been taken! This apps will be part of the *system*
1202 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1205 * Wake the registration queue futex.
1206 * Implicit memory barrier with the exchange in cds_wfq_enqueue.
1208 futex_nto1_wake(&ust_cmd_queue
.futex
);
1212 DBG("UST Registration thread dying");
1214 /* Notify that the registration thread is gone */
1220 unlink(apps_unix_sock_path
);
1226 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
1227 * exec or it will fails.
1229 static int spawn_kconsumerd_thread(void)
1233 /* Setup semaphore */
1234 sem_init(&kconsumerd_sem
, 0, 0);
1236 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
1238 perror("pthread_create kconsumerd");
1242 /* Wait for the kconsumerd thread to be ready */
1243 sem_wait(&kconsumerd_sem
);
1245 if (kconsumerd_pid
== 0) {
1246 ERR("Kconsumerd did not start");
1253 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1258 * Join kernel consumer thread
1260 static int join_kconsumerd_thread(void)
1265 if (kconsumerd_pid
!= 0) {
1266 ret
= kill(kconsumerd_pid
, SIGTERM
);
1268 ERR("Error killing kconsumerd");
1271 return pthread_join(kconsumerd_thread
, &status
);
1278 * Fork and exec a kernel consumer daemon (kconsumerd).
1280 * Return pid if successful else -1.
1282 static pid_t
spawn_kconsumerd(void)
1286 const char *verbosity
;
1288 DBG("Spawning kconsumerd");
1295 if (opt_verbose
> 1 || opt_verbose_kconsumerd
) {
1296 verbosity
= "--verbose";
1298 verbosity
= "--quiet";
1300 execl(INSTALL_BIN_PATH
"/ltt-kconsumerd", "ltt-kconsumerd", verbosity
, NULL
);
1302 perror("kernel start consumer exec");
1305 } else if (pid
> 0) {
1309 perror("kernel start consumer fork");
1319 * Spawn the kconsumerd daemon and session daemon thread.
1321 static int start_kconsumerd(void)
1325 pthread_mutex_lock(&kconsumerd_pid_mutex
);
1326 if (kconsumerd_pid
!= 0) {
1327 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1331 ret
= spawn_kconsumerd();
1333 ERR("Spawning kconsumerd failed");
1334 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1335 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1339 /* Setting up the global kconsumerd_pid */
1340 kconsumerd_pid
= ret
;
1341 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1343 DBG("Kconsumerd pid %d", ret
);
1345 DBG("Spawning kconsumerd thread");
1346 ret
= spawn_kconsumerd_thread();
1348 ERR("Fatal error spawning kconsumerd thread");
1360 * modprobe_kernel_modules
1362 static int modprobe_kernel_modules(void)
1367 for (i
= 0; i
< ARRAY_SIZE(kernel_modules_list
); i
++) {
1368 ret
= snprintf(modprobe
, sizeof(modprobe
),
1369 "/sbin/modprobe %s%s",
1370 kernel_modules_list
[i
].required
? "" : "--quiet ",
1371 kernel_modules_list
[i
].name
);
1373 perror("snprintf modprobe");
1376 modprobe
[sizeof(modprobe
) - 1] = '\0';
1377 ret
= system(modprobe
);
1379 ERR("Unable to launch modprobe for module %s",
1380 kernel_modules_list
[i
].name
);
1381 } else if (kernel_modules_list
[i
].required
1382 && WEXITSTATUS(ret
) != 0) {
1383 ERR("Unable to load module %s",
1384 kernel_modules_list
[i
].name
);
1386 DBG("Modprobe successfully %s",
1387 kernel_modules_list
[i
].name
);
1398 static int mount_debugfs(char *path
)
1401 char *type
= "debugfs";
1403 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, geteuid(), getegid());
1408 ret
= mount(type
, path
, type
, 0, NULL
);
1410 perror("mount debugfs");
1414 DBG("Mounted debugfs successfully at %s", path
);
1421 * Setup necessary data for kernel tracer action.
1423 static void init_kernel_tracer(void)
1426 char *proc_mounts
= "/proc/mounts";
1428 char *debugfs_path
= NULL
, *lttng_path
;
1431 /* Detect debugfs */
1432 fp
= fopen(proc_mounts
, "r");
1434 ERR("Unable to probe %s", proc_mounts
);
1438 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1439 if (strstr(line
, "debugfs") != NULL
) {
1440 /* Remove first string */
1442 /* Dup string here so we can reuse line later on */
1443 debugfs_path
= strdup(strtok(NULL
, " "));
1444 DBG("Got debugfs path : %s", debugfs_path
);
1451 /* Mount debugfs if needded */
1452 if (debugfs_path
== NULL
) {
1453 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1455 perror("asprintf debugfs path");
1458 ret
= mount_debugfs(debugfs_path
);
1464 /* Modprobe lttng kernel modules */
1465 ret
= modprobe_kernel_modules();
1470 /* Setup lttng kernel path */
1471 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1473 perror("asprintf lttng path");
1477 /* Open debugfs lttng */
1478 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1479 if (kernel_tracer_fd
< 0) {
1480 DBG("Failed to open %s", lttng_path
);
1486 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1496 WARN("No kernel tracer available");
1497 kernel_tracer_fd
= 0;
1502 * Start tracing by creating trace directory and sending FDs to the kernel
1505 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1509 if (session
->kconsumer_fds_sent
== 0) {
1511 * Assign default kernel consumer if no consumer assigned to the kernel
1512 * session. At this point, it's NOT suppose to be 0 but this is an extra
1515 if (session
->consumer_fd
== 0) {
1516 session
->consumer_fd
= kconsumerd_cmd_sock
;
1519 ret
= send_kconsumerd_fds(session
);
1521 ERR("Send kconsumerd fds failed");
1522 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1526 session
->kconsumer_fds_sent
= 1;
1534 * Notify kernel thread to update it's pollfd.
1536 static int notify_kernel_pollfd(void)
1540 /* Inform kernel thread of the new kernel channel */
1541 ret
= write(kernel_poll_pipe
[1], "!", 1);
1543 perror("write kernel poll pipe");
1550 * Allocate a channel structure and fill it.
1552 static struct lttng_channel
*init_default_channel(enum lttng_domain_type domain_type
,
1555 struct lttng_channel
*chan
;
1557 chan
= malloc(sizeof(struct lttng_channel
));
1559 perror("init channel malloc");
1563 if (snprintf(chan
->name
, NAME_MAX
, "%s", name
) < 0) {
1564 perror("snprintf channel name");
1568 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1569 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1570 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1572 switch (domain_type
) {
1573 case LTTNG_DOMAIN_KERNEL
:
1574 chan
->attr
.subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
1575 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1576 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1580 goto error
; /* Not implemented */
1591 * Create an UST session and add it to the session ust list.
1593 static int create_ust_session(pid_t pid
, struct ltt_session
*session
)
1596 struct ltt_ust_session
*lus
;
1598 DBG("Creating UST session");
1600 lus
= trace_ust_create_session(session
->path
, pid
);
1605 ret
= mkdir_recursive(lus
->path
, S_IRWXU
| S_IRWXG
,
1606 geteuid(), allowed_group());
1608 if (ret
!= -EEXIST
) {
1609 ERR("Trace directory creation error");
1614 /* Create session on the UST tracer */
1615 ret
= ustctl_create_session(lus
);
1628 * Create a kernel tracer session then create the default channel.
1630 static int create_kernel_session(struct ltt_session
*session
)
1634 DBG("Creating kernel session");
1636 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1638 ret
= LTTCOMM_KERN_SESS_FAIL
;
1642 /* Set kernel consumer socket fd */
1643 if (kconsumerd_cmd_sock
) {
1644 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
1647 ret
= mkdir_recursive(session
->kernel_session
->trace_path
,
1648 S_IRWXU
| S_IRWXG
, geteuid(), allowed_group());
1650 if (ret
!= -EEXIST
) {
1651 ERR("Trace directory creation error");
1661 * Using the session list, filled a lttng_session array to send back to the
1662 * client for session listing.
1664 * The session list lock MUST be acquired before calling this function. Use
1665 * lock_session_list() and unlock_session_list().
1667 static void list_lttng_sessions(struct lttng_session
*sessions
)
1670 struct ltt_session
*session
;
1672 DBG("Getting all available session");
1674 * Iterate over session list and append data after the control struct in
1677 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1678 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1679 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
1680 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1681 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
1687 * Fill lttng_channel array of all channels.
1689 static void list_lttng_channels(struct ltt_session
*session
,
1690 struct lttng_channel
*channels
)
1693 struct ltt_kernel_channel
*kchan
;
1695 DBG("Listing channels for session %s", session
->name
);
1697 /* Kernel channels */
1698 if (session
->kernel_session
!= NULL
) {
1699 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
1700 /* Copy lttng_channel struct to array */
1701 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1702 channels
[i
].enabled
= kchan
->enabled
;
1707 /* TODO: Missing UST listing */
1711 * Fill lttng_event array of all events in the channel.
1713 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1714 struct lttng_event
*events
)
1717 * TODO: This is ONLY kernel. Need UST support.
1720 struct ltt_kernel_event
*event
;
1722 DBG("Listing events for channel %s", kchan
->channel
->name
);
1724 /* Kernel channels */
1725 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1726 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1727 events
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1728 events
[i
].enabled
= event
->enabled
;
1729 switch (event
->event
->instrumentation
) {
1730 case LTTNG_KERNEL_TRACEPOINT
:
1731 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1733 case LTTNG_KERNEL_KPROBE
:
1734 case LTTNG_KERNEL_KRETPROBE
:
1735 events
[i
].type
= LTTNG_EVENT_PROBE
;
1736 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
1737 sizeof(struct lttng_kernel_kprobe
));
1739 case LTTNG_KERNEL_FUNCTION
:
1740 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
1741 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
1742 sizeof(struct lttng_kernel_function
));
1750 * Process the command requested by the lttng client within the command
1751 * context structure. This function make sure that the return structure (llm)
1752 * is set and ready for transmission before returning.
1754 * Return any error encountered or 0 for success.
1756 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1758 int ret
= LTTCOMM_OK
;
1760 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1763 * Commands that DO NOT need a session.
1765 switch (cmd_ctx
->lsm
->cmd_type
) {
1766 case LTTNG_CREATE_SESSION
:
1767 case LTTNG_LIST_SESSIONS
:
1768 case LTTNG_LIST_TRACEPOINTS
:
1769 case LTTNG_CALIBRATE
:
1772 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
1773 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session
.name
);
1774 if (cmd_ctx
->session
== NULL
) {
1775 /* If session name not found */
1776 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
1777 ret
= LTTCOMM_SESS_NOT_FOUND
;
1778 } else { /* If no session name specified */
1779 ret
= LTTCOMM_SELECT_SESS
;
1783 /* Acquire lock for the session */
1784 lock_session(cmd_ctx
->session
);
1790 * Check domain type for specific "pre-action".
1792 switch (cmd_ctx
->lsm
->domain
.type
) {
1793 case LTTNG_DOMAIN_KERNEL
:
1794 /* Kernel tracer check */
1795 if (kernel_tracer_fd
== 0) {
1796 init_kernel_tracer();
1797 if (kernel_tracer_fd
== 0) {
1798 ret
= LTTCOMM_KERN_NA
;
1802 /* Need a session for kernel command */
1803 switch (cmd_ctx
->lsm
->cmd_type
) {
1804 case LTTNG_CALIBRATE
:
1805 case LTTNG_CREATE_SESSION
:
1806 case LTTNG_LIST_SESSIONS
:
1807 case LTTNG_LIST_TRACEPOINTS
:
1810 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1811 ret
= create_kernel_session(cmd_ctx
->session
);
1813 ret
= LTTCOMM_KERN_SESS_FAIL
;
1816 /* Start the kernel consumer daemon */
1817 if (kconsumerd_pid
== 0 &&
1818 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1819 ret
= start_kconsumerd();
1827 case LTTNG_DOMAIN_UST_PID
:
1833 /* Process by command type */
1834 switch (cmd_ctx
->lsm
->cmd_type
) {
1835 case LTTNG_ADD_CONTEXT
:
1837 struct lttng_kernel_context kctx
;
1839 /* Setup lttng message with no payload */
1840 ret
= setup_lttng_msg(cmd_ctx
, 0);
1845 switch (cmd_ctx
->lsm
->domain
.type
) {
1846 case LTTNG_DOMAIN_KERNEL
:
1847 /* Create Kernel context */
1848 kctx
.ctx
= cmd_ctx
->lsm
->u
.context
.ctx
.ctx
;
1849 kctx
.u
.perf_counter
.type
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.type
;
1850 kctx
.u
.perf_counter
.config
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.config
;
1851 strncpy(kctx
.u
.perf_counter
.name
,
1852 cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.name
,
1853 LTTNG_SYMBOL_NAME_LEN
);
1854 kctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1856 /* Add kernel context to kernel tracer. See context.c */
1857 ret
= add_kernel_context(cmd_ctx
->session
->kernel_session
, &kctx
,
1858 cmd_ctx
->lsm
->u
.context
.event_name
,
1859 cmd_ctx
->lsm
->u
.context
.channel_name
);
1860 if (ret
!= LTTCOMM_OK
) {
1865 /* TODO: Userspace tracing */
1866 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1873 case LTTNG_DISABLE_CHANNEL
:
1875 struct ltt_kernel_channel
*kchan
;
1877 /* Setup lttng message with no payload */
1878 ret
= setup_lttng_msg(cmd_ctx
, 0);
1883 switch (cmd_ctx
->lsm
->domain
.type
) {
1884 case LTTNG_DOMAIN_KERNEL
:
1885 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1886 cmd_ctx
->session
->kernel_session
);
1887 if (kchan
== NULL
) {
1888 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1890 } else if (kchan
->enabled
== 1) {
1891 ret
= kernel_disable_channel(kchan
);
1893 if (ret
!= EEXIST
) {
1894 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1899 kernel_wait_quiescent(kernel_tracer_fd
);
1902 /* TODO: Userspace tracing */
1903 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1910 case LTTNG_DISABLE_EVENT
:
1912 struct ltt_kernel_channel
*kchan
;
1913 struct ltt_kernel_event
*kevent
;
1915 /* Setup lttng message with no payload */
1916 ret
= setup_lttng_msg(cmd_ctx
, 0);
1921 switch (cmd_ctx
->lsm
->domain
.type
) {
1922 case LTTNG_DOMAIN_KERNEL
:
1923 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1924 cmd_ctx
->session
->kernel_session
);
1925 if (kchan
== NULL
) {
1926 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1930 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, kchan
);
1931 if (kevent
!= NULL
) {
1932 DBG("Disabling kernel event %s for channel %s.", kevent
->event
->name
,
1933 kchan
->channel
->name
);
1934 ret
= kernel_disable_event(kevent
);
1936 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1941 kernel_wait_quiescent(kernel_tracer_fd
);
1944 /* TODO: Userspace tracing */
1945 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1952 case LTTNG_DISABLE_ALL_EVENT
:
1954 struct ltt_kernel_channel
*kchan
;
1955 struct ltt_kernel_event
*kevent
;
1957 /* Setup lttng message with no payload */
1958 ret
= setup_lttng_msg(cmd_ctx
, 0);
1963 switch (cmd_ctx
->lsm
->domain
.type
) {
1964 case LTTNG_DOMAIN_KERNEL
:
1965 DBG("Disabling all enabled kernel events");
1966 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1967 cmd_ctx
->session
->kernel_session
);
1968 if (kchan
== NULL
) {
1969 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1973 /* For each event in the kernel session */
1974 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1975 DBG("Disabling kernel event %s for channel %s.",
1976 kevent
->event
->name
, kchan
->channel
->name
);
1977 ret
= kernel_disable_event(kevent
);
1983 /* Quiescent wait after event disable */
1984 kernel_wait_quiescent(kernel_tracer_fd
);
1987 /* TODO: Userspace tracing */
1988 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1995 case LTTNG_ENABLE_CHANNEL
:
1997 struct ltt_kernel_channel
*kchan
;
1999 /* Setup lttng message with no payload */
2000 ret
= setup_lttng_msg(cmd_ctx
, 0);
2005 switch (cmd_ctx
->lsm
->domain
.type
) {
2006 case LTTNG_DOMAIN_KERNEL
:
2007 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
2008 cmd_ctx
->session
->kernel_session
);
2009 if (kchan
== NULL
) {
2010 /* Channel not found, creating it */
2011 DBG("Creating kernel channel");
2013 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2014 &cmd_ctx
->lsm
->u
.channel
.chan
,
2015 cmd_ctx
->session
->kernel_session
->trace_path
);
2017 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2021 /* Notify kernel thread that there is a new channel */
2022 ret
= notify_kernel_pollfd();
2024 ret
= LTTCOMM_FATAL
;
2027 } else if (kchan
->enabled
== 0) {
2028 ret
= kernel_enable_channel(kchan
);
2030 if (ret
!= EEXIST
) {
2031 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
2037 kernel_wait_quiescent(kernel_tracer_fd
);
2039 case LTTNG_DOMAIN_UST_PID
:
2043 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2050 case LTTNG_ENABLE_EVENT
:
2053 struct ltt_kernel_channel
*kchan
;
2054 struct ltt_kernel_event
*kevent
;
2055 struct lttng_channel
*chan
;
2057 /* Setup lttng message with no payload */
2058 ret
= setup_lttng_msg(cmd_ctx
, 0);
2063 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2065 switch (cmd_ctx
->lsm
->domain
.type
) {
2066 case LTTNG_DOMAIN_KERNEL
:
2067 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2068 cmd_ctx
->session
->kernel_session
);
2069 if (kchan
== NULL
) {
2070 DBG("Channel not found. Creating channel %s", channel_name
);
2072 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2074 ret
= LTTCOMM_FATAL
;
2078 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2079 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2081 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2084 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2085 cmd_ctx
->session
->kernel_session
);
2086 if (kchan
== NULL
) {
2087 ERR("Channel %s not found after creation. Internal error, giving up.",
2089 ret
= LTTCOMM_FATAL
;
2094 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, kchan
);
2095 if (kevent
== NULL
) {
2096 DBG("Creating kernel event %s for channel %s.",
2097 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
2098 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, kchan
);
2100 DBG("Enabling kernel event %s for channel %s.",
2101 kevent
->event
->name
, channel_name
);
2102 ret
= kernel_enable_event(kevent
);
2103 if (ret
== -EEXIST
) {
2104 ret
= LTTCOMM_KERN_EVENT_EXIST
;
2110 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2114 kernel_wait_quiescent(kernel_tracer_fd
);
2117 /* TODO: Userspace tracing */
2118 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2124 case LTTNG_ENABLE_ALL_EVENT
:
2128 struct ltt_kernel_channel
*kchan
;
2129 struct ltt_kernel_event
*kevent
;
2130 struct lttng_event
*event_list
;
2131 struct lttng_channel
*chan
;
2133 /* Setup lttng message with no payload */
2134 ret
= setup_lttng_msg(cmd_ctx
, 0);
2139 DBG("Enabling all kernel event");
2141 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2143 switch (cmd_ctx
->lsm
->domain
.type
) {
2144 case LTTNG_DOMAIN_KERNEL
:
2145 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2146 cmd_ctx
->session
->kernel_session
);
2147 if (kchan
== NULL
) {
2148 DBG("Channel not found. Creating channel %s", channel_name
);
2150 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2152 ret
= LTTCOMM_FATAL
;
2156 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2157 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2159 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2162 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2163 cmd_ctx
->session
->kernel_session
);
2164 if (kchan
== NULL
) {
2165 ERR("Channel %s not found after creation. Internal error, giving up.",
2167 ret
= LTTCOMM_FATAL
;
2172 /* For each event in the kernel session */
2173 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
2174 DBG("Enabling kernel event %s for channel %s.",
2175 kevent
->event
->name
, channel_name
);
2176 ret
= kernel_enable_event(kevent
);
2182 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
2184 ret
= LTTCOMM_KERN_LIST_FAIL
;
2188 for (i
= 0; i
< size
; i
++) {
2189 kevent
= trace_kernel_get_event_by_name(event_list
[i
].name
, kchan
);
2190 if (kevent
== NULL
) {
2191 /* Default event type for enable all */
2192 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2193 /* Enable each single tracepoint event */
2194 ret
= kernel_create_event(&event_list
[i
], kchan
);
2196 /* Ignore error here and continue */
2203 /* Quiescent wait after event enable */
2204 kernel_wait_quiescent(kernel_tracer_fd
);
2207 /* TODO: Userspace tracing */
2208 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2215 case LTTNG_LIST_TRACEPOINTS
:
2217 struct lttng_event
*events
;
2218 ssize_t nb_events
= 0;
2220 switch (cmd_ctx
->lsm
->domain
.type
) {
2221 case LTTNG_DOMAIN_KERNEL
:
2222 DBG("Listing kernel events");
2223 nb_events
= kernel_list_events(kernel_tracer_fd
, &events
);
2224 if (nb_events
< 0) {
2225 ret
= LTTCOMM_KERN_LIST_FAIL
;
2230 /* TODO: Userspace listing */
2231 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2236 * Setup lttng message with payload size set to the event list size in
2237 * bytes and then copy list into the llm payload.
2239 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2245 /* Copy event list into message payload */
2246 memcpy(cmd_ctx
->llm
->payload
, events
,
2247 sizeof(struct lttng_event
) * nb_events
);
2254 case LTTNG_START_TRACE
:
2256 struct ltt_kernel_channel
*chan
;
2258 /* Setup lttng message with no payload */
2259 ret
= setup_lttng_msg(cmd_ctx
, 0);
2264 /* Kernel tracing */
2265 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2266 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
2267 DBG("Open kernel metadata");
2268 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
2269 cmd_ctx
->session
->kernel_session
->trace_path
);
2271 ret
= LTTCOMM_KERN_META_FAIL
;
2276 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2277 DBG("Opening kernel metadata stream");
2278 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2279 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
2281 ERR("Kernel create metadata stream failed");
2282 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2288 /* For each channel */
2289 cds_list_for_each_entry(chan
,
2290 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2291 if (chan
->stream_count
== 0) {
2292 ret
= kernel_open_channel_stream(chan
);
2294 ERR("Kernel create channel stream failed");
2295 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2298 /* Update the stream global counter */
2299 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
2303 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
2305 ret
= LTTCOMM_KERN_START_FAIL
;
2309 DBG("Start kernel tracing");
2310 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
2312 ERR("Kernel start session failed");
2313 ret
= LTTCOMM_KERN_START_FAIL
;
2317 /* Quiescent wait after starting trace */
2318 kernel_wait_quiescent(kernel_tracer_fd
);
2321 /* TODO: Start all UST traces */
2326 case LTTNG_STOP_TRACE
:
2328 struct ltt_kernel_channel
*chan
;
2329 /* Setup lttng message with no payload */
2330 ret
= setup_lttng_msg(cmd_ctx
, 0);
2336 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2337 DBG("Stop kernel tracing");
2339 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
2341 ERR("Kernel metadata flush failed");
2344 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2345 ret
= kernel_flush_buffer(chan
);
2347 ERR("Kernel flush buffer error");
2351 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
2353 ERR("Kernel stop session failed");
2354 ret
= LTTCOMM_KERN_STOP_FAIL
;
2358 /* Quiescent wait after stopping trace */
2359 kernel_wait_quiescent(kernel_tracer_fd
);
2362 /* TODO : User-space tracer */
2367 case LTTNG_CREATE_SESSION
:
2369 /* Setup lttng message with no payload */
2370 ret
= setup_lttng_msg(cmd_ctx
, 0);
2375 tracepoint(create_session_start
);
2376 ret
= create_session(cmd_ctx
->lsm
->session
.name
, cmd_ctx
->lsm
->session
.path
);
2377 tracepoint(create_session_end
);
2379 if (ret
== -EEXIST
) {
2380 ret
= LTTCOMM_EXIST_SESS
;
2382 ret
= LTTCOMM_FATAL
;
2390 case LTTNG_DESTROY_SESSION
:
2392 /* Setup lttng message with no payload */
2393 ret
= setup_lttng_msg(cmd_ctx
, 0);
2398 /* Clean kernel session teardown */
2399 teardown_kernel_session(cmd_ctx
->session
);
2401 tracepoint(destroy_session_start
);
2402 ret
= destroy_session(cmd_ctx
->lsm
->session
.name
);
2403 tracepoint(destroy_session_end
);
2405 ret
= LTTCOMM_FATAL
;
2410 * Must notify the kernel thread here to update it's pollfd in order to
2411 * remove the channel(s)' fd just destroyed.
2413 ret
= notify_kernel_pollfd();
2415 ret
= LTTCOMM_FATAL
;
2422 case LTTNG_LIST_DOMAINS
:
2426 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2430 nb_dom
+= cmd_ctx
->session
->ust_session_list
.count
;
2432 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_domain
) * nb_dom
);
2437 ((struct lttng_domain
*)(cmd_ctx
->llm
->payload
))[0].type
=
2438 LTTNG_DOMAIN_KERNEL
;
2440 /* TODO: User-space tracer domain support */
2444 case LTTNG_LIST_CHANNELS
:
2447 * TODO: Only kernel channels are listed here. UST listing
2448 * is needed on lttng-ust 2.0 release.
2451 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2452 nb_chan
+= cmd_ctx
->session
->kernel_session
->channel_count
;
2455 ret
= setup_lttng_msg(cmd_ctx
,
2456 sizeof(struct lttng_channel
) * nb_chan
);
2461 list_lttng_channels(cmd_ctx
->session
,
2462 (struct lttng_channel
*)(cmd_ctx
->llm
->payload
));
2467 case LTTNG_LIST_EVENTS
:
2470 * TODO: Only kernel events are listed here. UST listing
2471 * is needed on lttng-ust 2.0 release.
2473 size_t nb_event
= 0;
2474 struct ltt_kernel_channel
*kchan
= NULL
;
2476 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2477 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.list
.channel_name
,
2478 cmd_ctx
->session
->kernel_session
);
2479 if (kchan
== NULL
) {
2480 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2483 nb_event
+= kchan
->event_count
;
2486 ret
= setup_lttng_msg(cmd_ctx
,
2487 sizeof(struct lttng_event
) * nb_event
);
2492 DBG("Listing events (%zu events)", nb_event
);
2494 list_lttng_events(kchan
,
2495 (struct lttng_event
*)(cmd_ctx
->llm
->payload
));
2500 case LTTNG_LIST_SESSIONS
:
2502 lock_session_list();
2504 if (session_list_ptr
->count
== 0) {
2505 ret
= LTTCOMM_NO_SESSION
;
2506 unlock_session_list();
2510 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
2511 session_list_ptr
->count
);
2513 unlock_session_list();
2517 /* Filled the session array */
2518 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
2520 unlock_session_list();
2525 case LTTNG_CALIBRATE
:
2527 /* Setup lttng message with no payload */
2528 ret
= setup_lttng_msg(cmd_ctx
, 0);
2533 switch (cmd_ctx
->lsm
->domain
.type
) {
2534 case LTTNG_DOMAIN_KERNEL
:
2536 struct lttng_kernel_calibrate kcalibrate
;
2538 kcalibrate
.type
= cmd_ctx
->lsm
->u
.calibrate
.type
;
2539 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2541 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2547 /* TODO: Userspace tracing */
2548 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2554 case LTTNG_REGISTER_CONSUMER
:
2558 /* Setup lttng message with no payload */
2559 ret
= setup_lttng_msg(cmd_ctx
, 0);
2564 switch (cmd_ctx
->lsm
->domain
.type
) {
2565 case LTTNG_DOMAIN_KERNEL
:
2567 /* Can't register a consumer if there is already one */
2568 if (cmd_ctx
->session
->kernel_session
->consumer_fd
!= 0) {
2569 ret
= LTTCOMM_CONNECT_FAIL
;
2573 sock
= lttcomm_connect_unix_sock(cmd_ctx
->lsm
->u
.reg
.path
);
2575 ret
= LTTCOMM_CONNECT_FAIL
;
2579 cmd_ctx
->session
->kernel_session
->consumer_fd
= sock
;
2583 /* TODO: Userspace tracing */
2584 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2593 /* Undefined command */
2594 ret
= setup_lttng_msg(cmd_ctx
, 0);
2603 /* Set return code */
2604 cmd_ctx
->llm
->ret_code
= ret
;
2606 if (cmd_ctx
->session
) {
2607 unlock_session(cmd_ctx
->session
);
2613 if (cmd_ctx
->llm
== NULL
) {
2614 DBG("Missing llm structure. Allocating one.");
2615 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2619 /* Notify client of error */
2620 cmd_ctx
->llm
->ret_code
= ret
;
2623 if (cmd_ctx
->session
) {
2624 unlock_session(cmd_ctx
->session
);
2630 * This thread manage all clients request using the unix client socket for
2633 static void *thread_manage_clients(void *data
)
2636 struct command_ctx
*cmd_ctx
= NULL
;
2637 struct pollfd pollfd
[2];
2639 tracepoint(sessiond_th_cli_start
);
2641 DBG("[thread] Manage client started");
2643 ret
= lttcomm_listen_unix_sock(client_sock
);
2648 /* First fd is always the quit pipe */
2649 pollfd
[0].fd
= thread_quit_pipe
[0];
2652 pollfd
[1].fd
= client_sock
;
2653 pollfd
[1].events
= POLLIN
;
2655 /* Notify parent pid that we are ready
2656 * to accept command for client side.
2658 if (opt_sig_parent
) {
2659 kill(ppid
, SIGCHLD
);
2663 DBG("Accepting client command ...");
2665 tracepoint(sessiond_th_cli_poll
);
2667 /* Inifinite blocking call, waiting for transmission */
2668 ret
= poll(pollfd
, 2, -1);
2670 perror("poll client thread");
2674 /* Thread quit pipe has been closed. Killing thread. */
2675 if (pollfd
[0].revents
== POLLNVAL
) {
2677 } else if (pollfd
[1].revents
== POLLERR
) {
2678 ERR("Client socket poll error");
2682 sock
= lttcomm_accept_unix_sock(client_sock
);
2687 /* Allocate context command to process the client request */
2688 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2690 /* Allocate data buffer for reception */
2691 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2692 cmd_ctx
->llm
= NULL
;
2693 cmd_ctx
->session
= NULL
;
2696 * Data is received from the lttng client. The struct
2697 * lttcomm_session_msg (lsm) contains the command and data request of
2700 DBG("Receiving data from client ...");
2701 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2706 // TODO: Validate cmd_ctx including sanity check for security purpose.
2709 * This function dispatch the work to the kernel or userspace tracer
2710 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2711 * informations for the client. The command context struct contains
2712 * everything this function may needs.
2714 ret
= process_client_msg(cmd_ctx
);
2716 /* TODO: Inform client somehow of the fatal error. At this point,
2717 * ret < 0 means that a malloc failed (ENOMEM). */
2718 /* Error detected but still accept command */
2719 clean_command_ctx(&cmd_ctx
);
2723 DBG("Sending response (size: %d, retcode: %d)",
2724 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2725 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2727 ERR("Failed to send data back to client");
2730 clean_command_ctx(&cmd_ctx
);
2732 /* End of transmission */
2737 DBG("Client thread dying");
2745 unlink(client_unix_sock_path
);
2747 clean_command_ctx(&cmd_ctx
);
2753 * usage function on stderr
2755 static void usage(void)
2757 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2758 fprintf(stderr
, " -h, --help Display this usage.\n");
2759 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2760 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2761 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2762 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2763 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2764 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2765 fprintf(stderr
, " -V, --version Show version number.\n");
2766 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2767 fprintf(stderr
, " -q, --quiet No output at all.\n");
2768 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2769 fprintf(stderr
, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2773 * daemon argument parsing
2775 static int parse_args(int argc
, char **argv
)
2779 static struct option long_options
[] = {
2780 { "client-sock", 1, 0, 'c' },
2781 { "apps-sock", 1, 0, 'a' },
2782 { "kconsumerd-cmd-sock", 1, 0, 0 },
2783 { "kconsumerd-err-sock", 1, 0, 0 },
2784 { "daemonize", 0, 0, 'd' },
2785 { "sig-parent", 0, 0, 'S' },
2786 { "help", 0, 0, 'h' },
2787 { "group", 1, 0, 'g' },
2788 { "version", 0, 0, 'V' },
2789 { "quiet", 0, 0, 'q' },
2790 { "verbose", 0, 0, 'v' },
2791 { "verbose-kconsumerd", 0, 0, 'Z' },
2796 int option_index
= 0;
2797 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:Z", long_options
, &option_index
);
2804 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2806 fprintf(stderr
, " with arg %s\n", optarg
);
2810 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2813 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2819 opt_tracing_group
= strdup(optarg
);
2825 fprintf(stdout
, "%s\n", VERSION
);
2831 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2834 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2840 /* Verbose level can increase using multiple -v */
2844 opt_verbose_kconsumerd
+= 1;
2847 /* Unknown option or other error.
2848 * Error is printed by getopt, just return */
2857 * Creates the two needed socket by the daemon.
2858 * apps_sock - The communication socket for all UST apps.
2859 * client_sock - The communication of the cli tool (lttng).
2861 static int init_daemon_socket(void)
2866 old_umask
= umask(0);
2868 /* Create client tool unix socket */
2869 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2870 if (client_sock
< 0) {
2871 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2876 /* File permission MUST be 660 */
2877 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2879 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2884 /* Create the application unix socket */
2885 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2886 if (apps_sock
< 0) {
2887 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2892 /* File permission MUST be 666 */
2893 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2895 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2906 * Check if the global socket is available, and if a daemon is answering
2907 * at the other side. If yes, error is returned.
2909 static int check_existing_daemon(void)
2911 if (access(client_unix_sock_path
, F_OK
) < 0 &&
2912 access(apps_unix_sock_path
, F_OK
) < 0) {
2915 /* Is there anybody out there ? */
2916 if (lttng_session_daemon_alive()) {
2924 * Set the tracing group gid onto the client socket.
2926 * Race window between mkdir and chown is OK because we are going from more
2927 * permissive (root.root) to les permissive (root.tracing).
2929 static int set_permissions(void)
2934 gid
= allowed_group();
2937 WARN("No tracing group detected");
2940 ERR("Missing tracing group. Aborting execution.");
2946 /* Set lttng run dir */
2947 ret
= chown(LTTNG_RUNDIR
, 0, gid
);
2949 ERR("Unable to set group on " LTTNG_RUNDIR
);
2953 /* lttng client socket path */
2954 ret
= chown(client_unix_sock_path
, 0, gid
);
2956 ERR("Unable to set group on %s", client_unix_sock_path
);
2960 /* kconsumerd error socket path */
2961 ret
= chown(kconsumerd_err_unix_sock_path
, 0, gid
);
2963 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2967 DBG("All permissions are set");
2974 * Create the pipe used to wake up the kernel thread.
2976 static int create_kernel_poll_pipe(void)
2978 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2982 * Create the application command pipe to wake thread_manage_apps.
2984 static int create_apps_cmd_pipe(void)
2986 return pipe2(apps_cmd_pipe
, O_CLOEXEC
);
2990 * Create the lttng run directory needed for all global sockets and pipe.
2992 static int create_lttng_rundir(void)
2996 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2998 if (errno
!= EEXIST
) {
2999 ERR("Unable to create " LTTNG_RUNDIR
);
3011 * Setup sockets and directory needed by the kconsumerd communication with the
3014 static int set_kconsumerd_sockets(void)
3018 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
3019 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
3022 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
3023 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
3026 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
3028 if (errno
!= EEXIST
) {
3029 ERR("Failed to create " KCONSUMERD_PATH
);
3035 /* Create the kconsumerd error unix socket */
3036 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
3037 if (kconsumerd_err_sock
< 0) {
3038 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
3043 /* File permission MUST be 660 */
3044 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3046 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
3056 * Signal handler for the daemon
3058 * Simply stop all worker threads, leaving main() return gracefully
3059 * after joining all threads and calling cleanup().
3061 static void sighandler(int sig
)
3065 DBG("SIGPIPE catched");
3068 DBG("SIGINT catched");
3072 DBG("SIGTERM catched");
3081 * Setup signal handler for :
3082 * SIGINT, SIGTERM, SIGPIPE
3084 static int set_signal_handler(void)
3087 struct sigaction sa
;
3090 if ((ret
= sigemptyset(&sigset
)) < 0) {
3091 perror("sigemptyset");
3095 sa
.sa_handler
= sighandler
;
3096 sa
.sa_mask
= sigset
;
3098 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3099 perror("sigaction");
3103 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3104 perror("sigaction");
3108 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3109 perror("sigaction");
3113 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3119 * Set open files limit to unlimited. This daemon can open a large number of
3120 * file descriptors in order to consumer multiple kernel traces.
3122 static void set_ulimit(void)
3127 /* The kernel does not allowed an infinite limit for open files */
3128 lim
.rlim_cur
= 65535;
3129 lim
.rlim_max
= 65535;
3131 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3133 perror("failed to set open files limit");
3140 int main(int argc
, char **argv
)
3144 const char *home_path
;
3146 tracepoint(sessiond_boot_start
);
3148 /* Create thread quit pipe */
3149 if ((ret
= init_thread_quit_pipe()) < 0) {
3153 /* Parse arguments */
3155 if ((ret
= parse_args(argc
, argv
) < 0)) {
3168 /* Check if daemon is UID = 0 */
3169 is_root
= !getuid();
3172 ret
= create_lttng_rundir();
3177 if (strlen(apps_unix_sock_path
) == 0) {
3178 snprintf(apps_unix_sock_path
, PATH_MAX
,
3179 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
3182 if (strlen(client_unix_sock_path
) == 0) {
3183 snprintf(client_unix_sock_path
, PATH_MAX
,
3184 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
3187 /* Set global SHM for ust */
3188 if (strlen(wait_shm_path
) == 0) {
3189 snprintf(wait_shm_path
, PATH_MAX
,
3190 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
3193 home_path
= get_home_dir();
3194 if (home_path
== NULL
) {
3195 /* TODO: Add --socket PATH option */
3196 ERR("Can't get HOME directory for sockets creation.");
3201 if (strlen(apps_unix_sock_path
) == 0) {
3202 snprintf(apps_unix_sock_path
, PATH_MAX
,
3203 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
3206 /* Set the cli tool unix socket path */
3207 if (strlen(client_unix_sock_path
) == 0) {
3208 snprintf(client_unix_sock_path
, PATH_MAX
,
3209 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
3212 /* Set global SHM for ust */
3213 if (strlen(wait_shm_path
) == 0) {
3214 snprintf(wait_shm_path
, PATH_MAX
,
3215 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, geteuid());
3219 DBG("Client socket path %s", client_unix_sock_path
);
3220 DBG("Application socket path %s", apps_unix_sock_path
);
3223 * See if daemon already exist.
3225 if ((ret
= check_existing_daemon()) < 0) {
3226 ERR("Already running daemon.\n");
3228 * We do not goto exit because we must not cleanup()
3229 * because a daemon is already running.
3234 /* After this point, we can safely call cleanup() so goto error is used */
3237 * These actions must be executed as root. We do that *after* setting up
3238 * the sockets path because we MUST make the check for another daemon using
3239 * those paths *before* trying to set the kernel consumer sockets and init
3243 ret
= set_kconsumerd_sockets();
3248 /* Setup kernel tracer */
3249 init_kernel_tracer();
3251 /* Set ulimit for open files */
3255 if ((ret
= set_signal_handler()) < 0) {
3259 /* Setup the needed unix socket */
3260 if ((ret
= init_daemon_socket()) < 0) {
3264 /* Set credentials to socket */
3265 if (is_root
&& ((ret
= set_permissions()) < 0)) {
3269 /* Get parent pid if -S, --sig-parent is specified. */
3270 if (opt_sig_parent
) {
3274 /* Setup the kernel pipe for waking up the kernel thread */
3275 if ((ret
= create_kernel_poll_pipe()) < 0) {
3279 /* Setup the thread apps communication pipe. */
3280 if ((ret
= create_apps_cmd_pipe()) < 0) {
3284 /* Init UST command queue. */
3285 cds_wfq_init(&ust_cmd_queue
.queue
);
3288 * Get session list pointer. This pointer MUST NOT be free().
3289 * This list is statically declared in session.c
3291 session_list_ptr
= get_session_list();
3293 /* Create thread to manage the client socket */
3294 ret
= pthread_create(&client_thread
, NULL
,
3295 thread_manage_clients
, (void *) NULL
);
3297 perror("pthread_create clients");
3301 /* Create thread to dispatch registration */
3302 ret
= pthread_create(&dispatch_thread
, NULL
,
3303 thread_dispatch_ust_registration
, (void *) NULL
);
3305 perror("pthread_create dispatch");
3309 /* Create thread to manage application registration. */
3310 ret
= pthread_create(®_apps_thread
, NULL
,
3311 thread_registration_apps
, (void *) NULL
);
3313 perror("pthread_create registration");
3317 /* Create thread to manage application socket */
3318 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
3320 perror("pthread_create apps");
3324 /* Create kernel thread to manage kernel event */
3325 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
3327 perror("pthread_create kernel");
3331 tracepoint(sessiond_boot_end
);
3333 ret
= pthread_join(kernel_thread
, &status
);
3335 perror("pthread_join");
3336 goto error
; /* join error, exit without cleanup */
3340 ret
= pthread_join(apps_thread
, &status
);
3342 perror("pthread_join");
3343 goto error
; /* join error, exit without cleanup */
3347 ret
= pthread_join(reg_apps_thread
, &status
);
3349 perror("pthread_join");
3350 goto error
; /* join error, exit without cleanup */
3354 ret
= pthread_join(dispatch_thread
, &status
);
3356 perror("pthread_join");
3357 goto error
; /* join error, exit without cleanup */
3361 ret
= pthread_join(client_thread
, &status
);
3363 perror("pthread_join");
3364 goto error
; /* join error, exit without cleanup */
3367 ret
= join_kconsumerd_thread();
3369 perror("join_kconsumerd");
3370 goto error
; /* join error, exit without cleanup */
3376 * cleanup() is called when no other thread is running.