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.
26 #include <semaphore.h>
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <urcu/futex.h>
41 #include <ltt-kconsumerd.h>
42 #include <lttng-sessiond-comm.h>
43 #include <lttng/lttng-kconsumerd.h>
46 #include "compat/poll.h"
49 #include "kernel-ctl.h"
50 #include "ltt-sessiond.h"
52 #include "traceable-app.h"
58 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
59 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
60 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
61 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
64 int opt_verbose
; /* Not static for lttngerr.h */
65 int opt_verbose_kconsumerd
; /* Not static for lttngerr.h */
66 int opt_quiet
; /* Not static for lttngerr.h */
69 const char *opt_tracing_group
;
70 static int opt_sig_parent
;
71 static int opt_daemon
;
72 static int is_root
; /* Set to 1 if the daemon is running as root */
73 static pid_t ppid
; /* Parent PID for --sig-parent option */
74 static pid_t kconsumerd_pid
;
75 static int dispatch_thread_exit
;
77 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
78 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
79 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
80 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
81 static char wait_shm_path
[PATH_MAX
]; /* global wait shm path for UST */
84 static int client_sock
;
86 static int kconsumerd_err_sock
;
87 static int kconsumerd_cmd_sock
;
88 static int kernel_tracer_fd
;
89 static int kernel_poll_pipe
[2];
92 * Quit pipe for all threads. This permits a single cancellation point
93 * for all threads when receiving an event on the pipe.
95 static int thread_quit_pipe
[2];
98 * This pipe is used to inform the thread managing application communication
99 * that a command is queued and ready to be processed.
101 static int apps_cmd_pipe
[2];
103 /* Pthread, Mutexes and Semaphores */
104 static pthread_t kconsumerd_thread
;
105 static pthread_t apps_thread
;
106 static pthread_t reg_apps_thread
;
107 static pthread_t client_thread
;
108 static pthread_t kernel_thread
;
109 static pthread_t dispatch_thread
;
110 static sem_t kconsumerd_sem
;
113 /* Mutex to control kconsumerd pid assignation */
114 static pthread_mutex_t kconsumerd_pid_mutex
;
117 * UST registration command queue. This queue is tied with a futex and uses a N
118 * wakers / 1 waiter implemented and detailed in futex.c/.h
120 * The thread_manage_apps and thread_dispatch_ust_registration interact with
121 * this queue and the wait/wake scheme.
123 static struct ust_cmd_queue ust_cmd_queue
;
126 * Pointer initialized before thread creation.
128 * This points to the tracing session list containing the session count and a
129 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
130 * MUST NOT be taken if you call a public function in session.c.
132 * The lock is nested inside the structure: session_list_ptr->lock. Please use
133 * lock_session_list and unlock_session_list for lock acquisition.
135 static struct ltt_session_list
*session_list_ptr
;
138 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
140 static int create_thread_poll_set(struct lttng_poll_event
*events
,
145 if (events
== NULL
|| size
== 0) {
150 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
156 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
168 * Check if the thread quit pipe was triggered.
170 * Return 1 if it was triggered else 0;
172 static int check_thread_quit_pipe(int fd
, uint32_t events
)
174 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
182 * Remove modules in reverse load order.
184 static int modprobe_remove_kernel_modules(void)
189 for (i
= ARRAY_SIZE(kernel_modules_list
) - 1; i
>= 0; i
--) {
190 ret
= snprintf(modprobe
, sizeof(modprobe
),
191 "/sbin/modprobe --remove --quiet %s",
192 kernel_modules_list
[i
].name
);
194 perror("snprintf modprobe --remove");
197 modprobe
[sizeof(modprobe
) - 1] = '\0';
198 ret
= system(modprobe
);
200 ERR("Unable to launch modprobe --remove for module %s",
201 kernel_modules_list
[i
].name
);
202 } else if (kernel_modules_list
[i
].required
203 && WEXITSTATUS(ret
) != 0) {
204 ERR("Unable to remove module %s",
205 kernel_modules_list
[i
].name
);
207 DBG("Modprobe removal successful %s",
208 kernel_modules_list
[i
].name
);
217 * Return group ID of the tracing group or -1 if not found.
219 static gid_t
allowed_group(void)
223 if (opt_tracing_group
) {
224 grp
= getgrnam(opt_tracing_group
);
226 grp
= getgrnam(default_tracing_group
);
236 * Init thread quit pipe.
238 * Return -1 on error or 0 if all pipes are created.
240 static int init_thread_quit_pipe(void)
244 ret
= pipe2(thread_quit_pipe
, O_CLOEXEC
);
246 perror("thread quit pipe");
255 * Complete teardown of a kernel session. This free all data structure related
256 * to a kernel session and update counter.
258 static void teardown_kernel_session(struct ltt_session
*session
)
260 if (session
->kernel_session
!= NULL
) {
261 DBG("Tearing down kernel session");
264 * If a custom kernel consumer was registered, close the socket before
265 * tearing down the complete kernel session structure
267 if (session
->kernel_session
->consumer_fd
!= kconsumerd_cmd_sock
) {
268 lttcomm_close_unix_sock(session
->kernel_session
->consumer_fd
);
271 trace_kernel_destroy_session(session
->kernel_session
);
272 /* Extra precaution */
273 session
->kernel_session
= NULL
;
278 * Stop all threads by closing the thread quit pipe.
280 static void stop_threads(void)
284 /* Stopping all threads */
285 DBG("Terminating all threads");
286 ret
= write(thread_quit_pipe
[1], "!", 1);
288 ERR("write error on thread quit pipe");
291 /* Dispatch thread */
292 dispatch_thread_exit
= 1;
293 futex_nto1_wake(&ust_cmd_queue
.futex
);
299 static void cleanup(void)
303 struct ltt_session
*sess
, *stmp
;
308 MSG("%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
309 "Matthew, BEET driven development works!%c[%dm",
310 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
314 DBG("Removing %s directory", LTTNG_RUNDIR
);
315 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
317 ERR("asprintf failed. Something is really wrong!");
320 /* Remove lttng run directory */
323 ERR("Unable to clean " LTTNG_RUNDIR
);
327 DBG("Cleaning up all session");
329 /* Destroy session list mutex */
330 if (session_list_ptr
!= NULL
) {
331 pthread_mutex_destroy(&session_list_ptr
->lock
);
333 /* Cleanup ALL session */
334 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
335 teardown_kernel_session(sess
);
336 // TODO complete session cleanup (including UST)
340 DBG("Closing all UST sockets");
341 clean_traceable_apps_list();
343 pthread_mutex_destroy(&kconsumerd_pid_mutex
);
345 DBG("Closing kernel fd");
346 close(kernel_tracer_fd
);
349 DBG("Unloading kernel modules");
350 modprobe_remove_kernel_modules();
353 close(thread_quit_pipe
[0]);
354 close(thread_quit_pipe
[1]);
358 * Send data on a unix socket using the liblttsessiondcomm API.
360 * Return lttcomm error code.
362 static int send_unix_sock(int sock
, void *buf
, size_t len
)
364 /* Check valid length */
369 return lttcomm_send_unix_sock(sock
, buf
, len
);
373 * Free memory of a command context structure.
375 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
377 DBG("Clean command context structure");
379 if ((*cmd_ctx
)->llm
) {
380 free((*cmd_ctx
)->llm
);
382 if ((*cmd_ctx
)->lsm
) {
383 free((*cmd_ctx
)->lsm
);
391 * Send all stream fds of kernel channel to the consumer.
393 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
397 struct ltt_kernel_stream
*stream
;
398 struct lttcomm_kconsumerd_header lkh
;
399 struct lttcomm_kconsumerd_msg lkm
;
401 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
403 nb_fd
= channel
->stream_count
;
406 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
407 lkh
.cmd_type
= ADD_STREAM
;
409 DBG("Sending kconsumerd header");
411 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
413 perror("send kconsumerd header");
417 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
418 if (stream
->fd
!= 0) {
420 lkm
.state
= stream
->state
;
421 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
422 lkm
.output
= channel
->channel
->attr
.output
;
423 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
424 lkm
.path_name
[PATH_MAX
- 1] = '\0';
426 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
428 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
430 perror("send kconsumerd fd");
436 DBG("Kconsumerd channel fds sent");
445 * Send all stream fds of the kernel session to the consumer.
447 static int send_kconsumerd_fds(struct ltt_kernel_session
*session
)
450 struct ltt_kernel_channel
*chan
;
451 struct lttcomm_kconsumerd_header lkh
;
452 struct lttcomm_kconsumerd_msg lkm
;
455 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
456 lkh
.cmd_type
= ADD_STREAM
;
458 DBG("Sending kconsumerd header for metadata");
460 ret
= lttcomm_send_unix_sock(session
->consumer_fd
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
462 perror("send kconsumerd header");
466 DBG("Sending metadata stream fd");
468 /* Extra protection. It's NOT suppose to be set to 0 at this point */
469 if (session
->consumer_fd
== 0) {
470 session
->consumer_fd
= kconsumerd_cmd_sock
;
473 if (session
->metadata_stream_fd
!= 0) {
474 /* Send metadata stream fd first */
475 lkm
.fd
= session
->metadata_stream_fd
;
476 lkm
.state
= ACTIVE_FD
;
477 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
478 lkm
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
479 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
480 lkm
.path_name
[PATH_MAX
- 1] = '\0';
482 ret
= lttcomm_send_fds_unix_sock(session
->consumer_fd
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
484 perror("send kconsumerd fd");
489 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
490 ret
= send_kconsumerd_channel_fds(session
->consumer_fd
, chan
);
496 DBG("Kconsumerd fds (metadata and channel streams) sent");
505 * Notify UST applications using the shm mmap futex.
507 static int notify_ust_apps(int active
)
511 DBG("Notifying applications of session daemon state: %d", active
);
513 /* See shm.c for this call implying mmap, shm and futex calls */
514 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
515 if (wait_shm_mmap
== NULL
) {
519 /* Wake waiting process */
520 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
522 /* Apps notified successfully */
530 * Setup the outgoing data buffer for the response (llm) by allocating the
531 * right amount of memory and copying the original information from the lsm
534 * Return total size of the buffer pointed by buf.
536 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
542 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
543 if (cmd_ctx
->llm
== NULL
) {
549 /* Copy common data */
550 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
551 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
553 cmd_ctx
->llm
->data_size
= size
;
554 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
563 * Update the kernel poll set of all channel fd available over all tracing
564 * session. Add the wakeup pipe at the end of the set.
566 static int update_kernel_poll(struct lttng_poll_event
*events
)
569 struct ltt_session
*session
;
570 struct ltt_kernel_channel
*channel
;
572 DBG("Updating kernel poll set");
575 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
576 lock_session(session
);
577 if (session
->kernel_session
== NULL
) {
578 unlock_session(session
);
582 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
583 /* Add channel fd to the kernel poll set */
584 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
586 unlock_session(session
);
589 DBG("Channel fd %d added to kernel set", channel
->fd
);
591 unlock_session(session
);
593 unlock_session_list();
598 unlock_session_list();
603 * Find the channel fd from 'fd' over all tracing session. When found, check
604 * for new channel stream and send those stream fds to the kernel consumer.
606 * Useful for CPU hotplug feature.
608 static int update_kernel_stream(int fd
)
611 struct ltt_session
*session
;
612 struct ltt_kernel_channel
*channel
;
614 DBG("Updating kernel streams for channel fd %d", fd
);
617 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
618 lock_session(session
);
619 if (session
->kernel_session
== NULL
) {
620 unlock_session(session
);
624 /* This is not suppose to be 0 but this is an extra security check */
625 if (session
->kernel_session
->consumer_fd
== 0) {
626 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
629 cds_list_for_each_entry(channel
,
630 &session
->kernel_session
->channel_list
.head
, list
) {
631 if (channel
->fd
== fd
) {
632 DBG("Channel found, updating kernel streams");
633 ret
= kernel_open_channel_stream(channel
);
639 * Have we already sent fds to the consumer? If yes, it means
640 * that tracing is started so it is safe to send our updated
643 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
644 ret
= send_kconsumerd_channel_fds(
645 session
->kernel_session
->consumer_fd
, channel
);
653 unlock_session(session
);
655 unlock_session_list();
659 unlock_session(session
);
660 unlock_session_list();
665 * This thread manage event coming from the kernel.
667 * Features supported in this thread:
670 static void *thread_manage_kernel(void *data
)
672 int ret
, i
, pollfd
, update_poll_flag
= 1;
673 uint32_t revents
, nb_fd
;
675 struct lttng_poll_event events
;
677 DBG("Thread manage kernel started");
679 ret
= create_thread_poll_set(&events
, 2);
684 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
690 if (update_poll_flag
== 1) {
691 ret
= update_kernel_poll(&events
);
695 update_poll_flag
= 0;
698 nb_fd
= LTTNG_POLL_GETNB(&events
);
700 DBG("Thread kernel polling on %d fds", nb_fd
);
702 /* Zeroed the poll events */
703 lttng_poll_reset(&events
);
705 /* Poll infinite value of time */
706 ret
= lttng_poll_wait(&events
, -1);
709 } else if (ret
== 0) {
710 /* Should not happen since timeout is infinite */
711 ERR("Return value of poll is 0 with an infinite timeout.\n"
712 "This should not have happened! Continuing...");
716 for (i
= 0; i
< nb_fd
; i
++) {
717 /* Fetch once the poll data */
718 revents
= LTTNG_POLL_GETEV(&events
, i
);
719 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
721 /* Thread quit pipe has been closed. Killing thread. */
722 ret
= check_thread_quit_pipe(pollfd
, revents
);
727 /* Check for data on kernel pipe */
728 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
729 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
730 update_poll_flag
= 1;
734 * New CPU detected by the kernel. Adding kernel stream to
735 * kernel session and updating the kernel consumer
737 if (revents
& LPOLLIN
) {
738 ret
= update_kernel_stream(pollfd
);
744 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
745 * and unregister kernel stream at this point.
753 DBG("Kernel thread dying");
754 close(kernel_poll_pipe
[0]);
755 close(kernel_poll_pipe
[1]);
757 lttng_poll_clean(&events
);
763 * This thread manage the kconsumerd error sent back to the session daemon.
765 static void *thread_manage_kconsumerd(void *data
)
767 int sock
= 0, i
, ret
, pollfd
;
768 uint32_t revents
, nb_fd
;
769 enum lttcomm_return_code code
;
770 struct lttng_poll_event events
;
772 DBG("[thread] Manage kconsumerd started");
774 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
780 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
781 * Nothing more will be added to this poll set.
783 ret
= create_thread_poll_set(&events
, 2);
788 ret
= lttng_poll_add(&events
, kconsumerd_err_sock
, LPOLLIN
| LPOLLRDHUP
);
793 nb_fd
= LTTNG_POLL_GETNB(&events
);
795 /* Inifinite blocking call, waiting for transmission */
796 ret
= lttng_poll_wait(&events
, -1);
801 for (i
= 0; i
< nb_fd
; i
++) {
802 /* Fetch once the poll data */
803 revents
= LTTNG_POLL_GETEV(&events
, i
);
804 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
806 /* Thread quit pipe has been closed. Killing thread. */
807 ret
= check_thread_quit_pipe(pollfd
, revents
);
812 /* Event on the registration socket */
813 if (pollfd
== kconsumerd_err_sock
) {
814 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
815 ERR("Kconsumerd err socket poll error");
821 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
826 /* Getting status code from kconsumerd */
827 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
832 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
833 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
834 if (kconsumerd_cmd_sock
< 0) {
835 sem_post(&kconsumerd_sem
);
836 perror("kconsumerd connect");
839 /* Signal condition to tell that the kconsumerd is ready */
840 sem_post(&kconsumerd_sem
);
841 DBG("Kconsumerd command socket ready");
843 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
844 lttcomm_get_readable_code(-code
));
848 /* Remove the kconsumerd error socket since we have established a connexion */
849 ret
= lttng_poll_del(&events
, kconsumerd_err_sock
);
854 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
859 /* Update number of fd */
860 nb_fd
= LTTNG_POLL_GETNB(&events
);
862 /* Inifinite blocking call, waiting for transmission */
863 ret
= lttng_poll_wait(&events
, -1);
868 for (i
= 0; i
< nb_fd
; i
++) {
869 /* Fetch once the poll data */
870 revents
= LTTNG_POLL_GETEV(&events
, i
);
871 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
873 /* Thread quit pipe has been closed. Killing thread. */
874 ret
= check_thread_quit_pipe(pollfd
, revents
);
879 /* Event on the kconsumerd socket */
880 if (pollfd
== sock
) {
881 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
882 ERR("Kconsumerd err socket second poll error");
888 /* Wait for any kconsumerd error */
889 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
891 ERR("Kconsumerd closed the command socket");
895 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
898 DBG("Kconsumerd thread dying");
899 close(kconsumerd_err_sock
);
900 close(kconsumerd_cmd_sock
);
903 unlink(kconsumerd_err_unix_sock_path
);
904 unlink(kconsumerd_cmd_unix_sock_path
);
907 lttng_poll_clean(&events
);
913 * This thread manage application communication.
915 static void *thread_manage_apps(void *data
)
918 uint32_t revents
, nb_fd
;
919 struct ust_command ust_cmd
;
920 struct lttng_poll_event events
;
922 DBG("[thread] Manage application started");
924 ret
= create_thread_poll_set(&events
, 2);
929 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
935 /* Zeroed the events structure */
936 lttng_poll_reset(&events
);
938 nb_fd
= LTTNG_POLL_GETNB(&events
);
940 DBG("Apps thread polling on %d fds", nb_fd
);
942 /* Inifinite blocking call, waiting for transmission */
943 ret
= lttng_poll_wait(&events
, -1);
948 for (i
= 0; i
< nb_fd
; i
++) {
949 /* Fetch once the poll data */
950 revents
= LTTNG_POLL_GETEV(&events
, i
);
951 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
953 /* Thread quit pipe has been closed. Killing thread. */
954 ret
= check_thread_quit_pipe(pollfd
, revents
);
959 /* Inspect the apps cmd pipe */
960 if (pollfd
== apps_cmd_pipe
[0]) {
961 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
962 ERR("Apps command pipe error");
964 } else if (revents
& LPOLLIN
) {
966 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
967 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
968 perror("read apps cmd pipe");
972 /* Register applicaton to the session daemon */
973 ret
= register_traceable_app(&ust_cmd
.reg_msg
, ust_cmd
.sock
);
975 /* Only critical ENOMEM error can be returned here */
979 ret
= ustctl_register_done(ust_cmd
.sock
);
982 * If the registration is not possible, we simply
983 * unregister the apps and continue
985 unregister_traceable_app(ust_cmd
.sock
);
988 * We just need here to monitor the close of the UST
989 * socket and poll set monitor those by default.
991 ret
= lttng_poll_add(&events
, ust_cmd
.sock
, 0);
996 DBG("Apps with sock %d added to poll set", ust_cmd
.sock
);
1002 * At this point, we know that a registered application made the
1003 * event at poll_wait.
1005 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1006 /* Removing from the poll set */
1007 ret
= lttng_poll_del(&events
, pollfd
);
1013 unregister_traceable_app(pollfd
);
1021 DBG("Application communication apps dying");
1022 close(apps_cmd_pipe
[0]);
1023 close(apps_cmd_pipe
[1]);
1025 lttng_poll_clean(&events
);
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
)
1099 int sock
= 0, i
, ret
, pollfd
;
1100 uint32_t revents
, nb_fd
;
1101 struct lttng_poll_event events
;
1103 * Get allocated in this thread, enqueued to a global queue, dequeued and
1104 * freed in the manage apps thread.
1106 struct ust_command
*ust_cmd
= NULL
;
1108 DBG("[thread] Manage application registration started");
1110 ret
= lttcomm_listen_unix_sock(apps_sock
);
1116 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1117 * more will be added to this poll set.
1119 ret
= create_thread_poll_set(&events
, 2);
1124 /* Add the application registration socket */
1125 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1130 /* Notify all applications to register */
1131 ret
= notify_ust_apps(1);
1133 ERR("Failed to notify applications or create the wait shared memory.\n"
1134 "Execution continues but there might be problem for already running\n"
1135 "applications that wishes to register.");
1139 DBG("Accepting application registration");
1141 nb_fd
= LTTNG_POLL_GETNB(&events
);
1143 /* Inifinite blocking call, waiting for transmission */
1144 ret
= lttng_poll_wait(&events
, -1);
1149 for (i
= 0; i
< nb_fd
; i
++) {
1150 /* Fetch once the poll data */
1151 revents
= LTTNG_POLL_GETEV(&events
, i
);
1152 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1154 /* Thread quit pipe has been closed. Killing thread. */
1155 ret
= check_thread_quit_pipe(pollfd
, revents
);
1160 /* Event on the registration socket */
1161 if (pollfd
== apps_sock
) {
1162 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1163 ERR("Register apps socket poll error");
1165 } else if (revents
& LPOLLIN
) {
1166 sock
= lttcomm_accept_unix_sock(apps_sock
);
1171 /* Create UST registration command for enqueuing */
1172 ust_cmd
= malloc(sizeof(struct ust_command
));
1173 if (ust_cmd
== NULL
) {
1174 perror("ust command malloc");
1179 * Using message-based transmissions to ensure we don't
1180 * have to deal with partially received messages.
1182 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1183 sizeof(struct ust_register_msg
));
1184 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1186 perror("lttcomm_recv_unix_sock register apps");
1188 ERR("Wrong size received on apps register");
1195 ust_cmd
->sock
= sock
;
1197 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1198 " gid:%d sock:%d name:%s (version %d.%d)",
1199 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1200 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1201 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1202 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1204 * Lock free enqueue the registration request. The red pill
1205 * has been taken! This apps will be part of the *system* :)
1207 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1210 * Wake the registration queue futex. Implicit memory
1211 * barrier with the exchange in cds_wfq_enqueue.
1213 futex_nto1_wake(&ust_cmd_queue
.futex
);
1220 DBG("UST Registration thread dying");
1222 /* Notify that the registration thread is gone */
1227 unlink(apps_unix_sock_path
);
1229 lttng_poll_clean(&events
);
1235 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
1236 * exec or it will fails.
1238 static int spawn_kconsumerd_thread(void)
1242 /* Setup semaphore */
1243 sem_init(&kconsumerd_sem
, 0, 0);
1245 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
1247 perror("pthread_create kconsumerd");
1251 /* Wait for the kconsumerd thread to be ready */
1252 sem_wait(&kconsumerd_sem
);
1254 if (kconsumerd_pid
== 0) {
1255 ERR("Kconsumerd did not start");
1262 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1267 * Join kernel consumer thread
1269 static int join_kconsumerd_thread(void)
1274 if (kconsumerd_pid
!= 0) {
1275 ret
= kill(kconsumerd_pid
, SIGTERM
);
1277 ERR("Error killing kconsumerd");
1280 return pthread_join(kconsumerd_thread
, &status
);
1287 * Fork and exec a kernel consumer daemon (kconsumerd).
1289 * Return pid if successful else -1.
1291 static pid_t
spawn_kconsumerd(void)
1295 const char *verbosity
;
1297 DBG("Spawning kconsumerd");
1304 if (opt_verbose
> 1 || opt_verbose_kconsumerd
) {
1305 verbosity
= "--verbose";
1307 verbosity
= "--quiet";
1309 execl(INSTALL_BIN_PATH
"/ltt-kconsumerd", "ltt-kconsumerd", verbosity
, NULL
);
1311 perror("kernel start consumer exec");
1314 } else if (pid
> 0) {
1318 perror("kernel start consumer fork");
1328 * Spawn the kconsumerd daemon and session daemon thread.
1330 static int start_kconsumerd(void)
1334 pthread_mutex_lock(&kconsumerd_pid_mutex
);
1335 if (kconsumerd_pid
!= 0) {
1336 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1340 ret
= spawn_kconsumerd();
1342 ERR("Spawning kconsumerd failed");
1343 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1344 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1348 /* Setting up the global kconsumerd_pid */
1349 kconsumerd_pid
= ret
;
1350 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1352 DBG("Kconsumerd pid %d", ret
);
1354 DBG("Spawning kconsumerd thread");
1355 ret
= spawn_kconsumerd_thread();
1357 ERR("Fatal error spawning kconsumerd thread");
1369 * modprobe_kernel_modules
1371 static int modprobe_kernel_modules(void)
1376 for (i
= 0; i
< ARRAY_SIZE(kernel_modules_list
); i
++) {
1377 ret
= snprintf(modprobe
, sizeof(modprobe
),
1378 "/sbin/modprobe %s%s",
1379 kernel_modules_list
[i
].required
? "" : "--quiet ",
1380 kernel_modules_list
[i
].name
);
1382 perror("snprintf modprobe");
1385 modprobe
[sizeof(modprobe
) - 1] = '\0';
1386 ret
= system(modprobe
);
1388 ERR("Unable to launch modprobe for module %s",
1389 kernel_modules_list
[i
].name
);
1390 } else if (kernel_modules_list
[i
].required
1391 && WEXITSTATUS(ret
) != 0) {
1392 ERR("Unable to load module %s",
1393 kernel_modules_list
[i
].name
);
1395 DBG("Modprobe successfully %s",
1396 kernel_modules_list
[i
].name
);
1407 static int mount_debugfs(char *path
)
1410 char *type
= "debugfs";
1412 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, geteuid(), getegid());
1417 ret
= mount(type
, path
, type
, 0, NULL
);
1419 perror("mount debugfs");
1423 DBG("Mounted debugfs successfully at %s", path
);
1430 * Setup necessary data for kernel tracer action.
1432 static void init_kernel_tracer(void)
1435 char *proc_mounts
= "/proc/mounts";
1437 char *debugfs_path
= NULL
, *lttng_path
;
1440 /* Detect debugfs */
1441 fp
= fopen(proc_mounts
, "r");
1443 ERR("Unable to probe %s", proc_mounts
);
1447 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1448 if (strstr(line
, "debugfs") != NULL
) {
1449 /* Remove first string */
1451 /* Dup string here so we can reuse line later on */
1452 debugfs_path
= strdup(strtok(NULL
, " "));
1453 DBG("Got debugfs path : %s", debugfs_path
);
1460 /* Mount debugfs if needded */
1461 if (debugfs_path
== NULL
) {
1462 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1464 perror("asprintf debugfs path");
1467 ret
= mount_debugfs(debugfs_path
);
1473 /* Modprobe lttng kernel modules */
1474 ret
= modprobe_kernel_modules();
1479 /* Setup lttng kernel path */
1480 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1482 perror("asprintf lttng path");
1486 /* Open debugfs lttng */
1487 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1488 if (kernel_tracer_fd
< 0) {
1489 DBG("Failed to open %s", lttng_path
);
1495 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1505 WARN("No kernel tracer available");
1506 kernel_tracer_fd
= 0;
1511 * Start tracing by creating trace directory and sending FDs to the kernel
1514 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1518 if (session
->kconsumer_fds_sent
== 0) {
1520 * Assign default kernel consumer if no consumer assigned to the kernel
1521 * session. At this point, it's NOT suppose to be 0 but this is an extra
1524 if (session
->consumer_fd
== 0) {
1525 session
->consumer_fd
= kconsumerd_cmd_sock
;
1528 ret
= send_kconsumerd_fds(session
);
1530 ERR("Send kconsumerd fds failed");
1531 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1535 session
->kconsumer_fds_sent
= 1;
1543 * Notify kernel thread to update it's poll set.
1545 static int notify_kernel_channels_update(void)
1549 /* Inform kernel thread of the new kernel channel */
1550 ret
= write(kernel_poll_pipe
[1], "!", 1);
1552 perror("write kernel poll pipe");
1559 * Allocate a channel structure and fill it.
1561 static struct lttng_channel
*init_default_channel(enum lttng_domain_type domain_type
,
1564 struct lttng_channel
*chan
;
1566 chan
= malloc(sizeof(struct lttng_channel
));
1568 perror("init channel malloc");
1572 if (snprintf(chan
->name
, NAME_MAX
, "%s", name
) < 0) {
1573 perror("snprintf channel name");
1577 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1578 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1579 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1581 switch (domain_type
) {
1582 case LTTNG_DOMAIN_KERNEL
:
1583 chan
->attr
.subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
1584 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1585 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1589 goto error
; /* Not implemented */
1600 * Create an UST session and add it to the session ust list.
1602 static int create_ust_session(pid_t pid
, struct ltt_session
*session
)
1605 struct ltt_ust_session
*lus
;
1607 DBG("Creating UST session");
1609 lus
= trace_ust_create_session(session
->path
, pid
);
1614 ret
= mkdir_recursive(lus
->path
, S_IRWXU
| S_IRWXG
,
1615 geteuid(), allowed_group());
1617 if (ret
!= -EEXIST
) {
1618 ERR("Trace directory creation error");
1623 /* Create session on the UST tracer */
1624 ret
= ustctl_create_session(lus
);
1637 * Create a kernel tracer session then create the default channel.
1639 static int create_kernel_session(struct ltt_session
*session
)
1643 DBG("Creating kernel session");
1645 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1647 ret
= LTTCOMM_KERN_SESS_FAIL
;
1651 /* Set kernel consumer socket fd */
1652 if (kconsumerd_cmd_sock
) {
1653 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
1656 ret
= mkdir_recursive(session
->kernel_session
->trace_path
,
1657 S_IRWXU
| S_IRWXG
, geteuid(), allowed_group());
1659 if (ret
!= -EEXIST
) {
1660 ERR("Trace directory creation error");
1670 * Using the session list, filled a lttng_session array to send back to the
1671 * client for session listing.
1673 * The session list lock MUST be acquired before calling this function. Use
1674 * lock_session_list() and unlock_session_list().
1676 static void list_lttng_sessions(struct lttng_session
*sessions
)
1679 struct ltt_session
*session
;
1681 DBG("Getting all available session");
1683 * Iterate over session list and append data after the control struct in
1686 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1687 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1688 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
1689 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1690 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
1696 * Fill lttng_channel array of all channels.
1698 static void list_lttng_channels(struct ltt_session
*session
,
1699 struct lttng_channel
*channels
)
1702 struct ltt_kernel_channel
*kchan
;
1704 DBG("Listing channels for session %s", session
->name
);
1706 /* Kernel channels */
1707 if (session
->kernel_session
!= NULL
) {
1708 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
1709 /* Copy lttng_channel struct to array */
1710 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1711 channels
[i
].enabled
= kchan
->enabled
;
1716 /* TODO: Missing UST listing */
1720 * Fill lttng_event array of all events in the channel.
1722 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1723 struct lttng_event
*events
)
1726 * TODO: This is ONLY kernel. Need UST support.
1729 struct ltt_kernel_event
*event
;
1731 DBG("Listing events for channel %s", kchan
->channel
->name
);
1733 /* Kernel channels */
1734 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1735 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1736 events
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1737 events
[i
].enabled
= event
->enabled
;
1738 switch (event
->event
->instrumentation
) {
1739 case LTTNG_KERNEL_TRACEPOINT
:
1740 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1742 case LTTNG_KERNEL_KPROBE
:
1743 case LTTNG_KERNEL_KRETPROBE
:
1744 events
[i
].type
= LTTNG_EVENT_PROBE
;
1745 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
1746 sizeof(struct lttng_kernel_kprobe
));
1748 case LTTNG_KERNEL_FUNCTION
:
1749 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
1750 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
1751 sizeof(struct lttng_kernel_function
));
1759 * Process the command requested by the lttng client within the command
1760 * context structure. This function make sure that the return structure (llm)
1761 * is set and ready for transmission before returning.
1763 * Return any error encountered or 0 for success.
1765 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1767 int ret
= LTTCOMM_OK
;
1769 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1772 * Commands that DO NOT need a session.
1774 switch (cmd_ctx
->lsm
->cmd_type
) {
1775 case LTTNG_CREATE_SESSION
:
1776 case LTTNG_LIST_SESSIONS
:
1777 case LTTNG_LIST_TRACEPOINTS
:
1778 case LTTNG_CALIBRATE
:
1781 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
1782 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session
.name
);
1783 if (cmd_ctx
->session
== NULL
) {
1784 /* If session name not found */
1785 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
1786 ret
= LTTCOMM_SESS_NOT_FOUND
;
1787 } else { /* If no session name specified */
1788 ret
= LTTCOMM_SELECT_SESS
;
1792 /* Acquire lock for the session */
1793 lock_session(cmd_ctx
->session
);
1799 * Check domain type for specific "pre-action".
1801 switch (cmd_ctx
->lsm
->domain
.type
) {
1802 case LTTNG_DOMAIN_KERNEL
:
1803 /* Kernel tracer check */
1804 if (kernel_tracer_fd
== 0) {
1805 init_kernel_tracer();
1806 if (kernel_tracer_fd
== 0) {
1807 ret
= LTTCOMM_KERN_NA
;
1811 /* Need a session for kernel command */
1812 switch (cmd_ctx
->lsm
->cmd_type
) {
1813 case LTTNG_CALIBRATE
:
1814 case LTTNG_CREATE_SESSION
:
1815 case LTTNG_LIST_SESSIONS
:
1816 case LTTNG_LIST_TRACEPOINTS
:
1819 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1820 ret
= create_kernel_session(cmd_ctx
->session
);
1822 ret
= LTTCOMM_KERN_SESS_FAIL
;
1825 /* Start the kernel consumer daemon */
1826 if (kconsumerd_pid
== 0 &&
1827 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1828 ret
= start_kconsumerd();
1836 case LTTNG_DOMAIN_UST_PID
:
1842 /* Process by command type */
1843 switch (cmd_ctx
->lsm
->cmd_type
) {
1844 case LTTNG_ADD_CONTEXT
:
1846 struct lttng_kernel_context kctx
;
1848 /* Setup lttng message with no payload */
1849 ret
= setup_lttng_msg(cmd_ctx
, 0);
1854 switch (cmd_ctx
->lsm
->domain
.type
) {
1855 case LTTNG_DOMAIN_KERNEL
:
1856 /* Create Kernel context */
1857 kctx
.ctx
= cmd_ctx
->lsm
->u
.context
.ctx
.ctx
;
1858 kctx
.u
.perf_counter
.type
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.type
;
1859 kctx
.u
.perf_counter
.config
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.config
;
1860 strncpy(kctx
.u
.perf_counter
.name
,
1861 cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.name
,
1862 LTTNG_SYMBOL_NAME_LEN
);
1863 kctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1865 /* Add kernel context to kernel tracer. See context.c */
1866 ret
= add_kernel_context(cmd_ctx
->session
->kernel_session
, &kctx
,
1867 cmd_ctx
->lsm
->u
.context
.event_name
,
1868 cmd_ctx
->lsm
->u
.context
.channel_name
);
1869 if (ret
!= LTTCOMM_OK
) {
1874 /* TODO: Userspace tracing */
1875 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1882 case LTTNG_DISABLE_CHANNEL
:
1884 struct ltt_kernel_channel
*kchan
;
1886 /* Setup lttng message with no payload */
1887 ret
= setup_lttng_msg(cmd_ctx
, 0);
1892 switch (cmd_ctx
->lsm
->domain
.type
) {
1893 case LTTNG_DOMAIN_KERNEL
:
1894 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1895 cmd_ctx
->session
->kernel_session
);
1896 if (kchan
== NULL
) {
1897 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1899 } else if (kchan
->enabled
== 1) {
1900 ret
= kernel_disable_channel(kchan
);
1902 if (ret
!= EEXIST
) {
1903 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1908 kernel_wait_quiescent(kernel_tracer_fd
);
1911 /* TODO: Userspace tracing */
1912 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1919 case LTTNG_DISABLE_EVENT
:
1921 struct ltt_kernel_channel
*kchan
;
1922 struct ltt_kernel_event
*kevent
;
1924 /* Setup lttng message with no payload */
1925 ret
= setup_lttng_msg(cmd_ctx
, 0);
1930 switch (cmd_ctx
->lsm
->domain
.type
) {
1931 case LTTNG_DOMAIN_KERNEL
:
1932 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1933 cmd_ctx
->session
->kernel_session
);
1934 if (kchan
== NULL
) {
1935 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1939 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, kchan
);
1940 if (kevent
!= NULL
) {
1941 DBG("Disabling kernel event %s for channel %s.", kevent
->event
->name
,
1942 kchan
->channel
->name
);
1943 ret
= kernel_disable_event(kevent
);
1945 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1950 kernel_wait_quiescent(kernel_tracer_fd
);
1953 /* TODO: Userspace tracing */
1954 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1961 case LTTNG_DISABLE_ALL_EVENT
:
1963 struct ltt_kernel_channel
*kchan
;
1964 struct ltt_kernel_event
*kevent
;
1966 /* Setup lttng message with no payload */
1967 ret
= setup_lttng_msg(cmd_ctx
, 0);
1972 switch (cmd_ctx
->lsm
->domain
.type
) {
1973 case LTTNG_DOMAIN_KERNEL
:
1974 DBG("Disabling all enabled kernel events");
1975 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1976 cmd_ctx
->session
->kernel_session
);
1977 if (kchan
== NULL
) {
1978 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1982 /* For each event in the kernel session */
1983 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1984 DBG("Disabling kernel event %s for channel %s.",
1985 kevent
->event
->name
, kchan
->channel
->name
);
1986 ret
= kernel_disable_event(kevent
);
1992 /* Quiescent wait after event disable */
1993 kernel_wait_quiescent(kernel_tracer_fd
);
1996 /* TODO: Userspace tracing */
1997 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2004 case LTTNG_ENABLE_CHANNEL
:
2006 struct ltt_kernel_channel
*kchan
;
2008 /* Setup lttng message with no payload */
2009 ret
= setup_lttng_msg(cmd_ctx
, 0);
2014 switch (cmd_ctx
->lsm
->domain
.type
) {
2015 case LTTNG_DOMAIN_KERNEL
:
2016 kchan
= trace_kernel_get_channel_by_name(
2017 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2018 cmd_ctx
->session
->kernel_session
);
2019 if (kchan
== NULL
) {
2020 /* Channel not found, creating it */
2021 DBG("Creating kernel channel %s",
2022 cmd_ctx
->lsm
->u
.enable
.channel_name
);
2024 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2025 &cmd_ctx
->lsm
->u
.channel
.chan
,
2026 cmd_ctx
->session
->kernel_session
->trace_path
);
2028 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2032 /* Notify kernel thread that there is a new channel */
2033 ret
= notify_kernel_channels_update();
2035 ret
= LTTCOMM_FATAL
;
2038 } else if (kchan
->enabled
== 0) {
2039 ret
= kernel_enable_channel(kchan
);
2041 if (ret
!= EEXIST
) {
2042 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
2048 kernel_wait_quiescent(kernel_tracer_fd
);
2050 case LTTNG_DOMAIN_UST_PID
:
2054 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2061 case LTTNG_ENABLE_EVENT
:
2064 struct ltt_kernel_channel
*kchan
;
2065 struct ltt_kernel_event
*kevent
;
2066 struct lttng_channel
*chan
;
2068 /* Setup lttng message with no payload */
2069 ret
= setup_lttng_msg(cmd_ctx
, 0);
2074 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2076 switch (cmd_ctx
->lsm
->domain
.type
) {
2077 case LTTNG_DOMAIN_KERNEL
:
2078 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2079 cmd_ctx
->session
->kernel_session
);
2080 if (kchan
== NULL
) {
2081 DBG("Channel not found. Creating channel %s", channel_name
);
2083 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2085 ret
= LTTCOMM_FATAL
;
2089 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2090 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2092 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2095 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2096 cmd_ctx
->session
->kernel_session
);
2097 if (kchan
== NULL
) {
2098 ERR("Channel %s not found after creation. Internal error, giving up.",
2100 ret
= LTTCOMM_FATAL
;
2104 ret
= notify_kernel_channels_update();
2106 ret
= LTTCOMM_FATAL
;
2111 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, kchan
);
2112 if (kevent
== NULL
) {
2113 DBG("Creating kernel event %s for channel %s.",
2114 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
2115 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, kchan
);
2117 DBG("Enabling kernel event %s for channel %s.",
2118 kevent
->event
->name
, channel_name
);
2119 ret
= kernel_enable_event(kevent
);
2120 if (ret
== -EEXIST
) {
2121 ret
= LTTCOMM_KERN_EVENT_EXIST
;
2127 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2131 kernel_wait_quiescent(kernel_tracer_fd
);
2134 /* TODO: Userspace tracing */
2135 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2141 case LTTNG_ENABLE_ALL_EVENT
:
2145 struct ltt_kernel_channel
*kchan
;
2146 struct ltt_kernel_event
*kevent
;
2147 struct lttng_event
*event_list
;
2148 struct lttng_channel
*chan
;
2150 /* Setup lttng message with no payload */
2151 ret
= setup_lttng_msg(cmd_ctx
, 0);
2156 DBG("Enabling all kernel event");
2158 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2160 switch (cmd_ctx
->lsm
->domain
.type
) {
2161 case LTTNG_DOMAIN_KERNEL
:
2162 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2163 cmd_ctx
->session
->kernel_session
);
2164 if (kchan
== NULL
) {
2165 DBG("Channel not found. Creating channel %s", channel_name
);
2167 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2169 ret
= LTTCOMM_FATAL
;
2173 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2174 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2176 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2179 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2180 cmd_ctx
->session
->kernel_session
);
2181 if (kchan
== NULL
) {
2182 ERR("Channel %s not found after creation. Internal error, giving up.",
2184 ret
= LTTCOMM_FATAL
;
2188 ret
= notify_kernel_channels_update();
2190 ret
= LTTCOMM_FATAL
;
2195 /* For each event in the kernel session */
2196 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
2197 DBG("Enabling kernel event %s for channel %s.",
2198 kevent
->event
->name
, channel_name
);
2199 ret
= kernel_enable_event(kevent
);
2205 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
2207 ret
= LTTCOMM_KERN_LIST_FAIL
;
2211 for (i
= 0; i
< size
; i
++) {
2212 kevent
= trace_kernel_get_event_by_name(event_list
[i
].name
, kchan
);
2213 if (kevent
== NULL
) {
2214 /* Default event type for enable all */
2215 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2216 /* Enable each single tracepoint event */
2217 ret
= kernel_create_event(&event_list
[i
], kchan
);
2219 /* Ignore error here and continue */
2226 /* Quiescent wait after event enable */
2227 kernel_wait_quiescent(kernel_tracer_fd
);
2230 /* TODO: Userspace tracing */
2231 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2238 case LTTNG_LIST_TRACEPOINTS
:
2240 struct lttng_event
*events
;
2241 ssize_t nb_events
= 0;
2243 switch (cmd_ctx
->lsm
->domain
.type
) {
2244 case LTTNG_DOMAIN_KERNEL
:
2245 DBG("Listing kernel events");
2246 nb_events
= kernel_list_events(kernel_tracer_fd
, &events
);
2247 if (nb_events
< 0) {
2248 ret
= LTTCOMM_KERN_LIST_FAIL
;
2253 /* TODO: Userspace listing */
2254 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2259 * Setup lttng message with payload size set to the event list size in
2260 * bytes and then copy list into the llm payload.
2262 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2268 /* Copy event list into message payload */
2269 memcpy(cmd_ctx
->llm
->payload
, events
,
2270 sizeof(struct lttng_event
) * nb_events
);
2277 case LTTNG_START_TRACE
:
2279 struct ltt_kernel_channel
*chan
;
2281 /* Setup lttng message with no payload */
2282 ret
= setup_lttng_msg(cmd_ctx
, 0);
2287 /* Kernel tracing */
2288 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2289 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
2290 DBG("Open kernel metadata");
2291 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
2292 cmd_ctx
->session
->kernel_session
->trace_path
);
2294 ret
= LTTCOMM_KERN_META_FAIL
;
2299 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2300 DBG("Opening kernel metadata stream");
2301 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2302 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
2304 ERR("Kernel create metadata stream failed");
2305 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2311 /* For each channel */
2312 cds_list_for_each_entry(chan
,
2313 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2314 if (chan
->stream_count
== 0) {
2315 ret
= kernel_open_channel_stream(chan
);
2317 ERR("Kernel create channel stream failed");
2318 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2321 /* Update the stream global counter */
2322 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
2326 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
2328 ret
= LTTCOMM_KERN_START_FAIL
;
2332 DBG("Start kernel tracing");
2333 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
2335 ERR("Kernel start session failed");
2336 ret
= LTTCOMM_KERN_START_FAIL
;
2340 /* Quiescent wait after starting trace */
2341 kernel_wait_quiescent(kernel_tracer_fd
);
2344 /* TODO: Start all UST traces */
2349 case LTTNG_STOP_TRACE
:
2351 struct ltt_kernel_channel
*chan
;
2352 /* Setup lttng message with no payload */
2353 ret
= setup_lttng_msg(cmd_ctx
, 0);
2359 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2360 DBG("Stop kernel tracing");
2362 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
2364 ERR("Kernel metadata flush failed");
2367 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2368 ret
= kernel_flush_buffer(chan
);
2370 ERR("Kernel flush buffer error");
2374 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
2376 ERR("Kernel stop session failed");
2377 ret
= LTTCOMM_KERN_STOP_FAIL
;
2381 /* Quiescent wait after stopping trace */
2382 kernel_wait_quiescent(kernel_tracer_fd
);
2385 /* TODO : User-space tracer */
2390 case LTTNG_CREATE_SESSION
:
2392 /* Setup lttng message with no payload */
2393 ret
= setup_lttng_msg(cmd_ctx
, 0);
2398 ret
= create_session(cmd_ctx
->lsm
->session
.name
, cmd_ctx
->lsm
->session
.path
);
2400 if (ret
== -EEXIST
) {
2401 ret
= LTTCOMM_EXIST_SESS
;
2403 ret
= LTTCOMM_FATAL
;
2411 case LTTNG_DESTROY_SESSION
:
2413 /* Setup lttng message with no payload */
2414 ret
= setup_lttng_msg(cmd_ctx
, 0);
2419 /* Clean kernel session teardown */
2420 teardown_kernel_session(cmd_ctx
->session
);
2422 ret
= destroy_session(cmd_ctx
->lsm
->session
.name
);
2424 ret
= LTTCOMM_FATAL
;
2429 * Must notify the kernel thread here to update it's poll setin order
2430 * to remove the channel(s)' fd just destroyed.
2432 ret
= notify_kernel_channels_update();
2434 ret
= LTTCOMM_FATAL
;
2441 case LTTNG_LIST_DOMAINS
:
2445 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2449 nb_dom
+= cmd_ctx
->session
->ust_session_list
.count
;
2451 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_domain
) * nb_dom
);
2456 ((struct lttng_domain
*)(cmd_ctx
->llm
->payload
))[0].type
=
2457 LTTNG_DOMAIN_KERNEL
;
2459 /* TODO: User-space tracer domain support */
2463 case LTTNG_LIST_CHANNELS
:
2466 * TODO: Only kernel channels are listed here. UST listing
2467 * is needed on lttng-ust 2.0 release.
2470 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2471 nb_chan
+= cmd_ctx
->session
->kernel_session
->channel_count
;
2474 ret
= setup_lttng_msg(cmd_ctx
,
2475 sizeof(struct lttng_channel
) * nb_chan
);
2480 list_lttng_channels(cmd_ctx
->session
,
2481 (struct lttng_channel
*)(cmd_ctx
->llm
->payload
));
2486 case LTTNG_LIST_EVENTS
:
2489 * TODO: Only kernel events are listed here. UST listing
2490 * is needed on lttng-ust 2.0 release.
2492 size_t nb_event
= 0;
2493 struct ltt_kernel_channel
*kchan
= NULL
;
2495 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2496 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.list
.channel_name
,
2497 cmd_ctx
->session
->kernel_session
);
2498 if (kchan
== NULL
) {
2499 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2502 nb_event
+= kchan
->event_count
;
2505 ret
= setup_lttng_msg(cmd_ctx
,
2506 sizeof(struct lttng_event
) * nb_event
);
2511 DBG("Listing events (%zu events)", nb_event
);
2513 list_lttng_events(kchan
,
2514 (struct lttng_event
*)(cmd_ctx
->llm
->payload
));
2519 case LTTNG_LIST_SESSIONS
:
2521 lock_session_list();
2523 if (session_list_ptr
->count
== 0) {
2524 ret
= LTTCOMM_NO_SESSION
;
2525 unlock_session_list();
2529 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
2530 session_list_ptr
->count
);
2532 unlock_session_list();
2536 /* Filled the session array */
2537 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
2539 unlock_session_list();
2544 case LTTNG_CALIBRATE
:
2546 /* Setup lttng message with no payload */
2547 ret
= setup_lttng_msg(cmd_ctx
, 0);
2552 switch (cmd_ctx
->lsm
->domain
.type
) {
2553 case LTTNG_DOMAIN_KERNEL
:
2555 struct lttng_kernel_calibrate kcalibrate
;
2557 kcalibrate
.type
= cmd_ctx
->lsm
->u
.calibrate
.type
;
2558 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2560 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2566 /* TODO: Userspace tracing */
2567 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2573 case LTTNG_REGISTER_CONSUMER
:
2577 /* Setup lttng message with no payload */
2578 ret
= setup_lttng_msg(cmd_ctx
, 0);
2583 switch (cmd_ctx
->lsm
->domain
.type
) {
2584 case LTTNG_DOMAIN_KERNEL
:
2586 /* Can't register a consumer if there is already one */
2587 if (cmd_ctx
->session
->kernel_session
->consumer_fd
!= 0) {
2588 ret
= LTTCOMM_CONNECT_FAIL
;
2592 sock
= lttcomm_connect_unix_sock(cmd_ctx
->lsm
->u
.reg
.path
);
2594 ret
= LTTCOMM_CONNECT_FAIL
;
2598 cmd_ctx
->session
->kernel_session
->consumer_fd
= sock
;
2602 /* TODO: Userspace tracing */
2603 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2612 /* Undefined command */
2613 ret
= setup_lttng_msg(cmd_ctx
, 0);
2622 /* Set return code */
2623 cmd_ctx
->llm
->ret_code
= ret
;
2625 if (cmd_ctx
->session
) {
2626 unlock_session(cmd_ctx
->session
);
2632 if (cmd_ctx
->llm
== NULL
) {
2633 DBG("Missing llm structure. Allocating one.");
2634 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2638 /* Notify client of error */
2639 cmd_ctx
->llm
->ret_code
= ret
;
2642 if (cmd_ctx
->session
) {
2643 unlock_session(cmd_ctx
->session
);
2649 * This thread manage all clients request using the unix client socket for
2652 static void *thread_manage_clients(void *data
)
2654 int sock
= 0, ret
, i
, pollfd
;
2655 uint32_t revents
, nb_fd
;
2656 struct command_ctx
*cmd_ctx
= NULL
;
2657 struct lttng_poll_event events
;
2659 DBG("[thread] Manage client started");
2661 ret
= lttcomm_listen_unix_sock(client_sock
);
2667 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2668 * more will be added to this poll set.
2670 ret
= create_thread_poll_set(&events
, 2);
2675 /* Add the application registration socket */
2676 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2682 * Notify parent pid that we are ready to accept command for client side.
2684 if (opt_sig_parent
) {
2685 kill(ppid
, SIGCHLD
);
2689 DBG("Accepting client command ...");
2691 nb_fd
= LTTNG_POLL_GETNB(&events
);
2693 /* Inifinite blocking call, waiting for transmission */
2694 ret
= lttng_poll_wait(&events
, -1);
2699 for (i
= 0; i
< nb_fd
; i
++) {
2700 /* Fetch once the poll data */
2701 revents
= LTTNG_POLL_GETEV(&events
, i
);
2702 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2704 /* Thread quit pipe has been closed. Killing thread. */
2705 ret
= check_thread_quit_pipe(pollfd
, revents
);
2710 /* Event on the registration socket */
2711 if (pollfd
== client_sock
) {
2712 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2713 ERR("Client socket poll error");
2719 DBG("Wait for client response");
2721 sock
= lttcomm_accept_unix_sock(client_sock
);
2726 /* Allocate context command to process the client request */
2727 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2728 if (cmd_ctx
== NULL
) {
2729 perror("malloc cmd_ctx");
2733 /* Allocate data buffer for reception */
2734 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2735 if (cmd_ctx
->lsm
== NULL
) {
2736 perror("malloc cmd_ctx->lsm");
2740 cmd_ctx
->llm
= NULL
;
2741 cmd_ctx
->session
= NULL
;
2744 * Data is received from the lttng client. The struct
2745 * lttcomm_session_msg (lsm) contains the command and data request of
2748 DBG("Receiving data from client ...");
2749 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
,
2750 sizeof(struct lttcomm_session_msg
));
2752 DBG("Nothing recv() from client... continuing");
2758 // TODO: Validate cmd_ctx including sanity check for
2759 // security purpose.
2762 * This function dispatch the work to the kernel or userspace tracer
2763 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2764 * informations for the client. The command context struct contains
2765 * everything this function may needs.
2767 ret
= process_client_msg(cmd_ctx
);
2770 * TODO: Inform client somehow of the fatal error. At
2771 * this point, ret < 0 means that a malloc failed
2772 * (ENOMEM). Error detected but still accept command.
2774 clean_command_ctx(&cmd_ctx
);
2778 DBG("Sending response (size: %d, retcode: %d)",
2779 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2780 ret
= send_unix_sock(sock
, cmd_ctx
->llm
,
2781 cmd_ctx
->lttng_msg_size
);
2783 ERR("Failed to send data back to client");
2786 clean_command_ctx(&cmd_ctx
);
2788 /* End of transmission */
2793 DBG("Client thread dying");
2794 unlink(client_unix_sock_path
);
2798 lttng_poll_clean(&events
);
2799 clean_command_ctx(&cmd_ctx
);
2805 * usage function on stderr
2807 static void usage(void)
2809 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2810 fprintf(stderr
, " -h, --help Display this usage.\n");
2811 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2812 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2813 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2814 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2815 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2816 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2817 fprintf(stderr
, " -V, --version Show version number.\n");
2818 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2819 fprintf(stderr
, " -q, --quiet No output at all.\n");
2820 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2821 fprintf(stderr
, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2825 * daemon argument parsing
2827 static int parse_args(int argc
, char **argv
)
2831 static struct option long_options
[] = {
2832 { "client-sock", 1, 0, 'c' },
2833 { "apps-sock", 1, 0, 'a' },
2834 { "kconsumerd-cmd-sock", 1, 0, 0 },
2835 { "kconsumerd-err-sock", 1, 0, 0 },
2836 { "daemonize", 0, 0, 'd' },
2837 { "sig-parent", 0, 0, 'S' },
2838 { "help", 0, 0, 'h' },
2839 { "group", 1, 0, 'g' },
2840 { "version", 0, 0, 'V' },
2841 { "quiet", 0, 0, 'q' },
2842 { "verbose", 0, 0, 'v' },
2843 { "verbose-kconsumerd", 0, 0, 'Z' },
2848 int option_index
= 0;
2849 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:Z", long_options
, &option_index
);
2856 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2858 fprintf(stderr
, " with arg %s\n", optarg
);
2862 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2865 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2871 opt_tracing_group
= strdup(optarg
);
2877 fprintf(stdout
, "%s\n", VERSION
);
2883 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2886 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2892 /* Verbose level can increase using multiple -v */
2896 opt_verbose_kconsumerd
+= 1;
2899 /* Unknown option or other error.
2900 * Error is printed by getopt, just return */
2909 * Creates the two needed socket by the daemon.
2910 * apps_sock - The communication socket for all UST apps.
2911 * client_sock - The communication of the cli tool (lttng).
2913 static int init_daemon_socket(void)
2918 old_umask
= umask(0);
2920 /* Create client tool unix socket */
2921 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2922 if (client_sock
< 0) {
2923 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2928 /* File permission MUST be 660 */
2929 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2931 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2936 /* Create the application unix socket */
2937 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2938 if (apps_sock
< 0) {
2939 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2944 /* File permission MUST be 666 */
2945 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2947 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2958 * Check if the global socket is available, and if a daemon is answering
2959 * at the other side. If yes, error is returned.
2961 static int check_existing_daemon(void)
2963 if (access(client_unix_sock_path
, F_OK
) < 0 &&
2964 access(apps_unix_sock_path
, F_OK
) < 0) {
2967 /* Is there anybody out there ? */
2968 if (lttng_session_daemon_alive()) {
2976 * Set the tracing group gid onto the client socket.
2978 * Race window between mkdir and chown is OK because we are going from more
2979 * permissive (root.root) to les permissive (root.tracing).
2981 static int set_permissions(void)
2986 gid
= allowed_group();
2989 WARN("No tracing group detected");
2992 ERR("Missing tracing group. Aborting execution.");
2998 /* Set lttng run dir */
2999 ret
= chown(LTTNG_RUNDIR
, 0, gid
);
3001 ERR("Unable to set group on " LTTNG_RUNDIR
);
3005 /* lttng client socket path */
3006 ret
= chown(client_unix_sock_path
, 0, gid
);
3008 ERR("Unable to set group on %s", client_unix_sock_path
);
3012 /* kconsumerd error socket path */
3013 ret
= chown(kconsumerd_err_unix_sock_path
, 0, gid
);
3015 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
3019 DBG("All permissions are set");
3026 * Create the pipe used to wake up the kernel thread.
3028 static int create_kernel_poll_pipe(void)
3030 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
3034 * Create the application command pipe to wake thread_manage_apps.
3036 static int create_apps_cmd_pipe(void)
3038 return pipe2(apps_cmd_pipe
, O_CLOEXEC
);
3042 * Create the lttng run directory needed for all global sockets and pipe.
3044 static int create_lttng_rundir(void)
3048 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
3050 if (errno
!= EEXIST
) {
3051 ERR("Unable to create " LTTNG_RUNDIR
);
3063 * Setup sockets and directory needed by the kconsumerd communication with the
3066 static int set_kconsumerd_sockets(void)
3070 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
3071 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
3074 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
3075 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
3078 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
3080 if (errno
!= EEXIST
) {
3081 ERR("Failed to create " KCONSUMERD_PATH
);
3087 /* Create the kconsumerd error unix socket */
3088 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
3089 if (kconsumerd_err_sock
< 0) {
3090 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
3095 /* File permission MUST be 660 */
3096 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3098 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
3108 * Signal handler for the daemon
3110 * Simply stop all worker threads, leaving main() return gracefully
3111 * after joining all threads and calling cleanup().
3113 static void sighandler(int sig
)
3117 DBG("SIGPIPE catched");
3120 DBG("SIGINT catched");
3124 DBG("SIGTERM catched");
3133 * Setup signal handler for :
3134 * SIGINT, SIGTERM, SIGPIPE
3136 static int set_signal_handler(void)
3139 struct sigaction sa
;
3142 if ((ret
= sigemptyset(&sigset
)) < 0) {
3143 perror("sigemptyset");
3147 sa
.sa_handler
= sighandler
;
3148 sa
.sa_mask
= sigset
;
3150 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3151 perror("sigaction");
3155 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3156 perror("sigaction");
3160 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3161 perror("sigaction");
3165 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3171 * Set open files limit to unlimited. This daemon can open a large number of
3172 * file descriptors in order to consumer multiple kernel traces.
3174 static void set_ulimit(void)
3179 /* The kernel does not allowed an infinite limit for open files */
3180 lim
.rlim_cur
= 65535;
3181 lim
.rlim_max
= 65535;
3183 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3185 perror("failed to set open files limit");
3192 int main(int argc
, char **argv
)
3196 const char *home_path
;
3198 /* Create thread quit pipe */
3199 if ((ret
= init_thread_quit_pipe()) < 0) {
3203 /* Parse arguments */
3205 if ((ret
= parse_args(argc
, argv
) < 0)) {
3218 /* Check if daemon is UID = 0 */
3219 is_root
= !getuid();
3222 ret
= create_lttng_rundir();
3227 if (strlen(apps_unix_sock_path
) == 0) {
3228 snprintf(apps_unix_sock_path
, PATH_MAX
,
3229 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
3232 if (strlen(client_unix_sock_path
) == 0) {
3233 snprintf(client_unix_sock_path
, PATH_MAX
,
3234 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
3237 /* Set global SHM for ust */
3238 if (strlen(wait_shm_path
) == 0) {
3239 snprintf(wait_shm_path
, PATH_MAX
,
3240 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
3243 home_path
= get_home_dir();
3244 if (home_path
== NULL
) {
3245 /* TODO: Add --socket PATH option */
3246 ERR("Can't get HOME directory for sockets creation.");
3251 if (strlen(apps_unix_sock_path
) == 0) {
3252 snprintf(apps_unix_sock_path
, PATH_MAX
,
3253 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
3256 /* Set the cli tool unix socket path */
3257 if (strlen(client_unix_sock_path
) == 0) {
3258 snprintf(client_unix_sock_path
, PATH_MAX
,
3259 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
3262 /* Set global SHM for ust */
3263 if (strlen(wait_shm_path
) == 0) {
3264 snprintf(wait_shm_path
, PATH_MAX
,
3265 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, geteuid());
3269 DBG("Client socket path %s", client_unix_sock_path
);
3270 DBG("Application socket path %s", apps_unix_sock_path
);
3273 * See if daemon already exist.
3275 if ((ret
= check_existing_daemon()) < 0) {
3276 ERR("Already running daemon.\n");
3278 * We do not goto exit because we must not cleanup()
3279 * because a daemon is already running.
3284 /* After this point, we can safely call cleanup() so goto error is used */
3287 * These actions must be executed as root. We do that *after* setting up
3288 * the sockets path because we MUST make the check for another daemon using
3289 * those paths *before* trying to set the kernel consumer sockets and init
3293 ret
= set_kconsumerd_sockets();
3298 /* Setup kernel tracer */
3299 init_kernel_tracer();
3301 /* Set ulimit for open files */
3305 if ((ret
= set_signal_handler()) < 0) {
3309 /* Setup the needed unix socket */
3310 if ((ret
= init_daemon_socket()) < 0) {
3314 /* Set credentials to socket */
3315 if (is_root
&& ((ret
= set_permissions()) < 0)) {
3319 /* Get parent pid if -S, --sig-parent is specified. */
3320 if (opt_sig_parent
) {
3324 /* Setup the kernel pipe for waking up the kernel thread */
3325 if ((ret
= create_kernel_poll_pipe()) < 0) {
3329 /* Setup the thread apps communication pipe. */
3330 if ((ret
= create_apps_cmd_pipe()) < 0) {
3334 /* Init UST command queue. */
3335 cds_wfq_init(&ust_cmd_queue
.queue
);
3338 * Get session list pointer. This pointer MUST NOT be free().
3339 * This list is statically declared in session.c
3341 session_list_ptr
= get_session_list();
3343 /* Set up max poll set size */
3344 lttng_poll_set_max_size();
3346 /* Create thread to manage the client socket */
3347 ret
= pthread_create(&client_thread
, NULL
,
3348 thread_manage_clients
, (void *) NULL
);
3350 perror("pthread_create clients");
3354 /* Create thread to dispatch registration */
3355 ret
= pthread_create(&dispatch_thread
, NULL
,
3356 thread_dispatch_ust_registration
, (void *) NULL
);
3358 perror("pthread_create dispatch");
3362 /* Create thread to manage application registration. */
3363 ret
= pthread_create(®_apps_thread
, NULL
,
3364 thread_registration_apps
, (void *) NULL
);
3366 perror("pthread_create registration");
3370 /* Create thread to manage application socket */
3371 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
3373 perror("pthread_create apps");
3377 /* Create kernel thread to manage kernel event */
3378 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
3380 perror("pthread_create kernel");
3384 ret
= pthread_join(kernel_thread
, &status
);
3386 perror("pthread_join");
3387 goto error
; /* join error, exit without cleanup */
3391 ret
= pthread_join(apps_thread
, &status
);
3393 perror("pthread_join");
3394 goto error
; /* join error, exit without cleanup */
3398 ret
= pthread_join(reg_apps_thread
, &status
);
3400 perror("pthread_join");
3401 goto error
; /* join error, exit without cleanup */
3405 ret
= pthread_join(dispatch_thread
, &status
);
3407 perror("pthread_join");
3408 goto error
; /* join error, exit without cleanup */
3412 ret
= pthread_join(client_thread
, &status
);
3414 perror("pthread_join");
3415 goto error
; /* join error, exit without cleanup */
3418 ret
= join_kconsumerd_thread();
3420 perror("join_kconsumerd");
3421 goto error
; /* join error, exit without cleanup */
3427 * cleanup() is called when no other thread is running.