2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 - 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>
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
39 #include <ltt-kconsumerd.h>
40 #include <lttng-sessiond-comm.h>
41 #include <lttng/lttng-kconsumerd.h>
46 #include "kernel-ctl.h"
47 #include "ltt-sessiond.h"
48 #include "traceable-app.h"
54 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
55 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
56 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
57 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
60 int opt_verbose
; /* Not static for lttngerr.h */
61 int opt_verbose_kconsumerd
; /* Not static for lttngerr.h */
62 int opt_quiet
; /* Not static for lttngerr.h */
65 const char *opt_tracing_group
;
66 static int opt_sig_parent
;
67 static int opt_daemon
;
68 static int is_root
; /* Set to 1 if the daemon is running as root */
69 static pid_t ppid
; /* Parent PID for --sig-parent option */
70 static pid_t kconsumerd_pid
;
71 static struct pollfd
*kernel_pollfd
;
72 static int dispatch_thread_exit
;
74 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
75 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
76 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
77 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
80 static int client_sock
;
82 static int kconsumerd_err_sock
;
83 static int kconsumerd_cmd_sock
;
84 static int kernel_tracer_fd
;
85 static int kernel_poll_pipe
[2];
88 * Quit pipe for all threads. This permits a single cancellation point
89 * for all threads when receiving an event on the pipe.
91 static int thread_quit_pipe
[2];
94 * This pipe is used to inform the thread managing application communication
95 * that a command is queued and ready to be processed.
97 static int apps_cmd_pipe
[2];
99 /* Pthread, Mutexes and Semaphores */
100 static pthread_t kconsumerd_thread
;
101 static pthread_t apps_thread
;
102 static pthread_t reg_apps_thread
;
103 static pthread_t client_thread
;
104 static pthread_t kernel_thread
;
105 static pthread_t dispatch_thread
;
106 static sem_t kconsumerd_sem
;
108 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
110 static int modprobe_remove_kernel_modules(void);
113 * UST registration command queue. This queue is tied with a futex and uses a N
114 * wakers / 1 waiter implemented and detailed in futex.c/.h
116 * The thread_manage_apps and thread_dispatch_ust_registration interact with
117 * this queue and the wait/wake scheme.
119 static struct ust_cmd_queue ust_cmd_queue
;
122 * Pointer initialized before thread creation.
124 * This points to the tracing session list containing the session count and a
125 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
126 * MUST NOT be taken if you call a public function in session.c.
128 * The lock is nested inside the structure: session_list_ptr->lock. Please use
129 * lock_session_list and unlock_session_list for lock acquisition.
131 static struct ltt_session_list
*session_list_ptr
;
133 static gid_t
allowed_group(void)
137 if (opt_tracing_group
) {
138 grp
= getgrnam(opt_tracing_group
);
140 grp
= getgrnam(default_tracing_group
);
152 * Return -1 on error or 0 if all pipes are created.
154 static int init_thread_quit_pipe(void)
158 ret
= pipe2(thread_quit_pipe
, O_CLOEXEC
);
160 perror("thread quit pipe");
169 * Complete teardown of a kernel session. This free all data structure related
170 * to a kernel session and update counter.
172 static void teardown_kernel_session(struct ltt_session
*session
)
174 if (session
->kernel_session
!= NULL
) {
175 DBG("Tearing down kernel session");
178 * If a custom kernel consumer was registered, close the socket before
179 * tearing down the complete kernel session structure
181 if (session
->kernel_session
->consumer_fd
!= kconsumerd_cmd_sock
) {
182 lttcomm_close_unix_sock(session
->kernel_session
->consumer_fd
);
185 trace_kernel_destroy_session(session
->kernel_session
);
186 /* Extra precaution */
187 session
->kernel_session
= NULL
;
192 * Stop all threads by closing the thread quit pipe.
194 static void stop_threads(void)
196 /* Stopping all threads */
197 DBG("Terminating all threads");
198 close(thread_quit_pipe
[0]);
199 close(thread_quit_pipe
[1]);
200 /* Dispatch thread */
201 dispatch_thread_exit
= 1;
202 futex_nto1_wake(&ust_cmd_queue
.futex
);
208 static void cleanup(void)
212 struct ltt_session
*sess
, *stmp
;
217 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
218 "Matthew, BEET driven development works!%c[%dm",
219 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
222 DBG("Removing %s directory", LTTNG_RUNDIR
);
223 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
225 ERR("asprintf failed. Something is really wrong!");
228 /* Remove lttng run directory */
231 ERR("Unable to clean " LTTNG_RUNDIR
);
234 DBG("Cleaning up all session");
236 /* Destroy session list mutex */
237 if (session_list_ptr
!= NULL
) {
238 pthread_mutex_destroy(&session_list_ptr
->lock
);
240 /* Cleanup ALL session */
241 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
242 teardown_kernel_session(sess
);
243 // TODO complete session cleanup (including UST)
247 DBG("Closing all UST sockets");
248 clean_traceable_apps_list();
250 pthread_mutex_destroy(&kconsumerd_pid_mutex
);
252 DBG("Closing kernel fd");
253 close(kernel_tracer_fd
);
255 DBG("Unloading kernel modules");
256 modprobe_remove_kernel_modules();
260 * Send data on a unix socket using the liblttsessiondcomm API.
262 * Return lttcomm error code.
264 static int send_unix_sock(int sock
, void *buf
, size_t len
)
266 /* Check valid length */
271 return lttcomm_send_unix_sock(sock
, buf
, len
);
275 * Free memory of a command context structure.
277 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
279 DBG("Clean command context structure");
281 if ((*cmd_ctx
)->llm
) {
282 free((*cmd_ctx
)->llm
);
284 if ((*cmd_ctx
)->lsm
) {
285 free((*cmd_ctx
)->lsm
);
293 * Send all stream fds of kernel channel to the consumer.
295 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
299 struct ltt_kernel_stream
*stream
;
300 struct lttcomm_kconsumerd_header lkh
;
301 struct lttcomm_kconsumerd_msg lkm
;
303 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
305 nb_fd
= channel
->stream_count
;
308 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
309 lkh
.cmd_type
= ADD_STREAM
;
311 DBG("Sending kconsumerd header");
313 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
315 perror("send kconsumerd header");
319 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
320 if (stream
->fd
!= 0) {
322 lkm
.state
= stream
->state
;
323 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
324 lkm
.output
= channel
->channel
->attr
.output
;
325 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
326 lkm
.path_name
[PATH_MAX
- 1] = '\0';
328 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
330 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
332 perror("send kconsumerd fd");
338 DBG("Kconsumerd channel fds sent");
347 * Send all stream fds of the kernel session to the consumer.
349 static int send_kconsumerd_fds(struct ltt_kernel_session
*session
)
352 struct ltt_kernel_channel
*chan
;
353 struct lttcomm_kconsumerd_header lkh
;
354 struct lttcomm_kconsumerd_msg lkm
;
357 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
358 lkh
.cmd_type
= ADD_STREAM
;
360 DBG("Sending kconsumerd header for metadata");
362 ret
= lttcomm_send_unix_sock(session
->consumer_fd
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
364 perror("send kconsumerd header");
368 DBG("Sending metadata stream fd");
370 /* Extra protection. It's NOT suppose to be set to 0 at this point */
371 if (session
->consumer_fd
== 0) {
372 session
->consumer_fd
= kconsumerd_cmd_sock
;
375 if (session
->metadata_stream_fd
!= 0) {
376 /* Send metadata stream fd first */
377 lkm
.fd
= session
->metadata_stream_fd
;
378 lkm
.state
= ACTIVE_FD
;
379 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
380 lkm
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
381 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
382 lkm
.path_name
[PATH_MAX
- 1] = '\0';
384 ret
= lttcomm_send_fds_unix_sock(session
->consumer_fd
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
386 perror("send kconsumerd fd");
391 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
392 ret
= send_kconsumerd_channel_fds(session
->consumer_fd
, chan
);
398 DBG("Kconsumerd fds (metadata and channel streams) sent");
408 * Return a socket connected to the libust communication socket of the
409 * application identified by the pid.
411 * If the pid is not found in the traceable list, return -1 to indicate error.
413 static int ust_connect_app(pid_t pid
)
416 struct ltt_traceable_app
*lta
;
418 DBG("Connect to application pid %d", pid
);
420 lta
= find_app_by_pid(pid
);
423 DBG("Application pid %d not found", pid
);
427 sock
= ustctl_connect_pid(lta
->pid
);
429 ERR("Fail connecting to the PID %d", pid
);
436 * Notify apps by writing 42 to a named pipe using name. Every applications
437 * waiting for a ltt-sessiond will be notified and re-register automatically to
438 * the session daemon.
440 * Return open or write error value.
442 static int notify_apps(const char *name
)
447 DBG("Notify the global application pipe");
449 /* Try opening the global pipe */
450 fd
= open(name
, O_WRONLY
);
455 /* Notify by writing on the pipe */
456 ret
= write(fd
, "42", 2);
464 #endif /* DISABLED */
467 * Setup the outgoing data buffer for the response (llm) by allocating the
468 * right amount of memory and copying the original information from the lsm
471 * Return total size of the buffer pointed by buf.
473 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
479 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
480 if (cmd_ctx
->llm
== NULL
) {
486 /* Copy common data */
487 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
488 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
490 cmd_ctx
->llm
->data_size
= size
;
491 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
500 * Update the kernel pollfd set of all channel fd available over all tracing
501 * session. Add the wakeup pipe at the end of the set.
503 static int update_kernel_pollfd(void)
507 * The wakup pipe and the quit pipe are needed so the number of fds starts
508 * at 2 for those pipes.
510 unsigned int nb_fd
= 2;
511 struct ltt_session
*session
;
512 struct ltt_kernel_channel
*channel
;
514 DBG("Updating kernel_pollfd");
516 /* Get the number of channel of all kernel session */
518 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
519 lock_session(session
);
520 if (session
->kernel_session
== NULL
) {
521 unlock_session(session
);
524 nb_fd
+= session
->kernel_session
->channel_count
;
525 unlock_session(session
);
528 DBG("Resizing kernel_pollfd to size %d", nb_fd
);
530 kernel_pollfd
= realloc(kernel_pollfd
, nb_fd
* sizeof(struct pollfd
));
531 if (kernel_pollfd
== NULL
) {
532 perror("malloc kernel_pollfd");
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
);
543 ERR("To much channel for kernel_pollfd size");
544 unlock_session(session
);
547 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
548 kernel_pollfd
[i
].fd
= channel
->fd
;
549 kernel_pollfd
[i
].events
= POLLIN
| POLLRDNORM
;
552 unlock_session(session
);
554 unlock_session_list();
556 /* Adding wake up pipe */
557 kernel_pollfd
[nb_fd
- 2].fd
= kernel_poll_pipe
[0];
558 kernel_pollfd
[nb_fd
- 2].events
= POLLIN
;
560 /* Adding the quit pipe */
561 kernel_pollfd
[nb_fd
- 1].fd
= thread_quit_pipe
[0];
566 unlock_session_list();
571 * Find the channel fd from 'fd' over all tracing session. When found, check
572 * for new channel stream and send those stream fds to the kernel consumer.
574 * Useful for CPU hotplug feature.
576 static int update_kernel_stream(int fd
)
579 struct ltt_session
*session
;
580 struct ltt_kernel_channel
*channel
;
582 DBG("Updating kernel streams for channel fd %d", fd
);
585 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
586 lock_session(session
);
587 if (session
->kernel_session
== NULL
) {
588 unlock_session(session
);
592 /* This is not suppose to be 0 but this is an extra security check */
593 if (session
->kernel_session
->consumer_fd
== 0) {
594 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
597 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
598 if (channel
->fd
== fd
) {
599 DBG("Channel found, updating kernel streams");
600 ret
= kernel_open_channel_stream(channel
);
606 * Have we already sent fds to the consumer? If yes, it means that
607 * tracing is started so it is safe to send our updated stream fds.
609 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
610 ret
= send_kconsumerd_channel_fds(session
->kernel_session
->consumer_fd
,
619 unlock_session(session
);
623 unlock_session_list();
625 unlock_session(session
);
631 * This thread manage event coming from the kernel.
633 * Features supported in this thread:
636 static void *thread_manage_kernel(void *data
)
638 int ret
, i
, nb_fd
= 0;
640 int update_poll_flag
= 1;
642 DBG("Thread manage kernel started");
645 if (update_poll_flag
== 1) {
646 nb_fd
= update_kernel_pollfd();
650 update_poll_flag
= 0;
653 DBG("Polling on %d fds", nb_fd
);
655 /* Poll infinite value of time */
656 ret
= poll(kernel_pollfd
, nb_fd
, -1);
658 perror("poll kernel thread");
660 } else if (ret
== 0) {
661 /* Should not happen since timeout is infinite */
665 /* Thread quit pipe has been closed. Killing thread. */
666 if (kernel_pollfd
[nb_fd
- 1].revents
== POLLNVAL
) {
670 DBG("Kernel poll event triggered");
673 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
676 switch (kernel_pollfd
[nb_fd
- 2].revents
) {
678 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
679 update_poll_flag
= 1;
687 for (i
= 0; i
< nb_fd
; i
++) {
688 switch (kernel_pollfd
[i
].revents
) {
690 * New CPU detected by the kernel. Adding kernel stream to kernel
691 * session and updating the kernel consumer
693 case POLLIN
| POLLRDNORM
:
694 ret
= update_kernel_stream(kernel_pollfd
[i
].fd
);
704 DBG("Kernel thread dying");
709 close(kernel_poll_pipe
[0]);
710 close(kernel_poll_pipe
[1]);
715 * This thread manage the kconsumerd error sent back to the session daemon.
717 static void *thread_manage_kconsumerd(void *data
)
720 enum lttcomm_return_code code
;
721 struct pollfd pollfd
[2];
723 DBG("[thread] Manage kconsumerd started");
725 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
730 /* First fd is always the quit pipe */
731 pollfd
[0].fd
= thread_quit_pipe
[0];
734 pollfd
[1].fd
= kconsumerd_err_sock
;
735 pollfd
[1].events
= POLLIN
;
737 /* Inifinite blocking call, waiting for transmission */
738 ret
= poll(pollfd
, 2, -1);
740 perror("poll kconsumerd thread");
744 /* Thread quit pipe has been closed. Killing thread. */
745 if (pollfd
[0].revents
== POLLNVAL
) {
747 } else if (pollfd
[1].revents
== POLLERR
) {
748 ERR("Kconsumerd err socket poll error");
752 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
757 /* Getting status code from kconsumerd */
758 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
763 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
764 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
765 if (kconsumerd_cmd_sock
< 0) {
766 sem_post(&kconsumerd_sem
);
767 perror("kconsumerd connect");
770 /* Signal condition to tell that the kconsumerd is ready */
771 sem_post(&kconsumerd_sem
);
772 DBG("Kconsumerd command socket ready");
774 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
775 lttcomm_get_readable_code(-code
));
779 /* Kconsumerd err socket */
781 pollfd
[1].events
= POLLIN
;
783 /* Inifinite blocking call, waiting for transmission */
784 ret
= poll(pollfd
, 2, -1);
786 perror("poll kconsumerd thread");
790 /* Thread quit pipe has been closed. Killing thread. */
791 if (pollfd
[0].revents
== POLLNVAL
) {
793 } else if (pollfd
[1].revents
== POLLERR
) {
794 ERR("Kconsumerd err socket second poll error");
798 /* Wait for any kconsumerd error */
799 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
801 ERR("Kconsumerd closed the command socket");
805 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
808 DBG("Kconsumerd thread dying");
809 if (kconsumerd_err_sock
) {
810 close(kconsumerd_err_sock
);
812 if (kconsumerd_cmd_sock
) {
813 close(kconsumerd_cmd_sock
);
819 unlink(kconsumerd_err_unix_sock_path
);
820 unlink(kconsumerd_cmd_unix_sock_path
);
827 * Reallocate the apps command pollfd structure of nb_fd size.
829 * The first two fds must be there at all time.
831 static int update_apps_cmd_pollfd(unsigned int nb_fd
, struct pollfd
**pollfd
)
833 /* Can't accept pollfd less than 2 */
838 *pollfd
= realloc(*pollfd
, nb_fd
* sizeof(struct pollfd
));
839 if (*pollfd
== NULL
) {
840 perror("realloc manage apps pollfd");
844 /* First fd is always the quit pipe */
845 (*pollfd
)[0].fd
= thread_quit_pipe
[0];
846 /* Apps command pipe */
847 (*pollfd
)[1].fd
= apps_cmd_pipe
[0];
848 (*pollfd
)[1].events
= POLLIN
;
850 DBG("Apps cmd pollfd realloc of size %d", nb_fd
);
860 * Send registration done packet to the application.
862 static int send_ust_register_done(int sock
)
864 struct lttcomm_ust_msg lum
;
866 DBG("Sending register done command to %d", sock
);
868 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
869 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
871 return ustcomm_send_command(sock
, &lum
);
875 * This thread manage application communication.
877 static void *thread_manage_apps(void *data
)
880 unsigned int nb_fd
= 2;
881 int update_poll_flag
= 1;
882 struct pollfd
*pollfd
= NULL
;
883 struct ust_command ust_cmd
;
885 DBG("[thread] Manage application started");
890 /* See if we have a valid socket to add to pollfd */
891 if (ust_cmd
.sock
!= -1) {
893 update_poll_flag
= 1;
896 /* The pollfd struct must be updated */
897 if (update_poll_flag
) {
898 ret
= update_apps_cmd_pollfd(nb_fd
, &pollfd
);
900 /* malloc failed so we quit */
903 if (ust_cmd
.sock
!= -1) {
904 /* Update pollfd with the new UST socket */
905 DBG("Adding sock %d to apps cmd pollfd", ust_cmd
.sock
);
906 pollfd
[nb_fd
- 1].fd
= ust_cmd
.sock
;
907 pollfd
[nb_fd
- 1].events
= POLLHUP
| POLLNVAL
;
912 DBG("Apps thread polling on %d fds", nb_fd
);
914 /* Inifinite blocking call, waiting for transmission */
915 ret
= poll(pollfd
, nb_fd
, -1);
917 perror("poll apps thread");
921 /* Thread quit pipe has been closed. Killing thread. */
922 if (pollfd
[0].revents
== POLLNVAL
) {
924 } else if (pollfd
[1].revents
== POLLERR
) {
925 ERR("Apps command pipe poll error");
927 } else if (pollfd
[1].revents
== POLLIN
) {
929 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
930 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
931 perror("read apps cmd pipe");
935 /* Register applicaton to the session daemon */
936 ret
= register_traceable_app(&ust_cmd
.reg_msg
, ust_cmd
.sock
);
938 /* Only critical ENOMEM error can be returned here */
942 ret
= send_ust_register_done(ust_cmd
.sock
);
945 * If the registration is not possible, we simply unregister
946 * the apps and continue
948 unregister_traceable_app(ust_cmd
.sock
);
953 for (i
= 2; i
< count
; i
++) {
954 /* Apps socket is closed/hungup */
955 switch (pollfd
[i
].revents
) {
959 unregister_traceable_app(pollfd
[i
].fd
);
964 if (nb_fd
!= count
) {
965 update_poll_flag
= 1;
970 DBG("Application communication apps dying");
971 close(apps_cmd_pipe
[0]);
972 close(apps_cmd_pipe
[1]);
980 * Dispatch request from the registration threads to the application
981 * communication thread.
983 static void *thread_dispatch_ust_registration(void *data
)
986 struct cds_wfq_node
*node
;
987 struct ust_command
*ust_cmd
= NULL
;
989 DBG("[thread] Dispatch UST command started");
991 while (!dispatch_thread_exit
) {
992 /* Atomically prepare the queue futex */
993 futex_nto1_prepare(&ust_cmd_queue
.futex
);
996 /* Dequeue command for registration */
997 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
999 DBG("Waked up but nothing in the UST command queue");
1000 /* Continue thread execution */
1004 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1006 DBG("Dispatching UST registration pid:%d sock:%d",
1007 ust_cmd
->reg_msg
.pid
, ust_cmd
->sock
);
1009 * Inform apps thread of the new application registration. This
1010 * call is blocking so we can be assured that the data will be read
1011 * at some point in time or wait to the end of the world :)
1013 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1014 sizeof(struct ust_command
));
1016 perror("write apps cmd pipe");
1017 if (errno
== EBADF
) {
1019 * We can't inform the application thread to process
1020 * registration. We will exit or else application
1021 * registration will not occur and tracing will never
1028 } while (node
!= NULL
);
1030 /* Futex wait on queue. Blocking call on futex() */
1031 futex_nto1_wait(&ust_cmd_queue
.futex
);
1035 DBG("Dispatch thread dying");
1040 * This thread manage application registration.
1042 static void *thread_registration_apps(void *data
)
1045 struct pollfd pollfd
[2];
1047 * Get allocated in this thread, enqueued to a global queue, dequeued and
1048 * freed in the manage apps thread.
1050 struct ust_command
*ust_cmd
= NULL
;
1052 DBG("[thread] Manage application registration started");
1054 ret
= lttcomm_listen_unix_sock(apps_sock
);
1059 /* First fd is always the quit pipe */
1060 pollfd
[0].fd
= thread_quit_pipe
[0];
1063 pollfd
[1].fd
= apps_sock
;
1064 pollfd
[1].events
= POLLIN
;
1066 /* Notify all applications to register */
1067 //notify_apps(default_global_apps_pipe);
1070 DBG("Accepting application registration");
1072 /* Inifinite blocking call, waiting for transmission */
1073 ret
= poll(pollfd
, 2, -1);
1075 perror("poll register apps thread");
1079 /* Thread quit pipe has been closed. Killing thread. */
1080 if (pollfd
[0].revents
== POLLNVAL
) {
1082 } else if (pollfd
[1].revents
== POLLERR
) {
1083 ERR("Register apps socket poll error");
1087 sock
= lttcomm_accept_unix_sock(apps_sock
);
1092 /* Create UST registration command for enqueuing */
1093 ust_cmd
= malloc(sizeof(struct ust_command
));
1094 if (ust_cmd
== NULL
) {
1095 perror("ust command malloc");
1100 * Using message-based transmissions to ensure we don't have to deal
1101 * with partially received messages.
1103 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1104 sizeof(struct ust_register_msg
));
1105 if (ret
< 0 || ret
!= sizeof(struct ust_register_msg
)) {
1106 perror("lttcomm_recv_unix_sock register apps");
1112 ust_cmd
->sock
= sock
;
1115 * Lock free enqueue the registration request.
1116 * The red pill has been taken! This apps will be part of the *system*
1118 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1121 * Wake the registration queue futex.
1122 * Implicit memory barrier with the exchange in cds_wfq_enqueue.
1124 futex_nto1_wake(&ust_cmd_queue
.futex
);
1126 DBG("Thread manage apps informed of queued node with sock:%d pid:%d",
1127 sock
, ust_cmd
->reg_msg
.pid
);
1131 DBG("Register apps thread dying");
1139 unlink(apps_unix_sock_path
);
1144 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
1145 * exec or it will fails.
1147 static int spawn_kconsumerd_thread(void)
1151 /* Setup semaphore */
1152 sem_init(&kconsumerd_sem
, 0, 0);
1154 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
1156 perror("pthread_create kconsumerd");
1160 /* Wait for the kconsumerd thread to be ready */
1161 sem_wait(&kconsumerd_sem
);
1163 if (kconsumerd_pid
== 0) {
1164 ERR("Kconsumerd did not start");
1171 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1176 * Join kernel consumer thread
1178 static int join_kconsumerd_thread(void)
1183 if (kconsumerd_pid
!= 0) {
1184 ret
= kill(kconsumerd_pid
, SIGTERM
);
1186 ERR("Error killing kconsumerd");
1189 return pthread_join(kconsumerd_thread
, &status
);
1196 * Fork and exec a kernel consumer daemon (kconsumerd).
1198 * Return pid if successful else -1.
1200 static pid_t
spawn_kconsumerd(void)
1204 const char *verbosity
;
1206 DBG("Spawning kconsumerd");
1213 if (opt_verbose
> 1 || opt_verbose_kconsumerd
) {
1214 verbosity
= "--verbose";
1216 verbosity
= "--quiet";
1218 execl(INSTALL_BIN_PATH
"/ltt-kconsumerd", "ltt-kconsumerd", verbosity
, NULL
);
1220 perror("kernel start consumer exec");
1223 } else if (pid
> 0) {
1227 perror("kernel start consumer fork");
1237 * Spawn the kconsumerd daemon and session daemon thread.
1239 static int start_kconsumerd(void)
1243 pthread_mutex_lock(&kconsumerd_pid_mutex
);
1244 if (kconsumerd_pid
!= 0) {
1245 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1249 ret
= spawn_kconsumerd();
1251 ERR("Spawning kconsumerd failed");
1252 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1253 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1257 /* Setting up the global kconsumerd_pid */
1258 kconsumerd_pid
= ret
;
1259 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1261 DBG("Kconsumerd pid %d", ret
);
1263 DBG("Spawning kconsumerd thread");
1264 ret
= spawn_kconsumerd_thread();
1266 ERR("Fatal error spawning kconsumerd thread");
1278 * modprobe_kernel_modules
1280 static int modprobe_kernel_modules(void)
1285 for (i
= 0; i
< ARRAY_SIZE(kernel_modules_list
); i
++) {
1286 ret
= snprintf(modprobe
, sizeof(modprobe
),
1287 "/sbin/modprobe %s%s",
1288 kernel_modules_list
[i
].required
? "" : "--quiet ",
1289 kernel_modules_list
[i
].name
);
1291 perror("snprintf modprobe");
1294 modprobe
[sizeof(modprobe
) - 1] = '\0';
1295 ret
= system(modprobe
);
1297 ERR("Unable to launch modprobe for module %s",
1298 kernel_modules_list
[i
].name
);
1299 } else if (kernel_modules_list
[i
].required
1300 && WEXITSTATUS(ret
) != 0) {
1301 ERR("Unable to load module %s",
1302 kernel_modules_list
[i
].name
);
1304 DBG("Modprobe successfully %s",
1305 kernel_modules_list
[i
].name
);
1314 * modprobe_remove_kernel_modules
1315 * Remove modules in reverse load order.
1317 static int modprobe_remove_kernel_modules(void)
1322 for (i
= ARRAY_SIZE(kernel_modules_list
) - 1; i
>= 0; i
--) {
1323 ret
= snprintf(modprobe
, sizeof(modprobe
),
1324 "/sbin/modprobe --remove --quiet %s",
1325 kernel_modules_list
[i
].name
);
1327 perror("snprintf modprobe --remove");
1330 modprobe
[sizeof(modprobe
) - 1] = '\0';
1331 ret
= system(modprobe
);
1333 ERR("Unable to launch modprobe --remove for module %s",
1334 kernel_modules_list
[i
].name
);
1335 } else if (kernel_modules_list
[i
].required
1336 && WEXITSTATUS(ret
) != 0) {
1337 ERR("Unable to remove module %s",
1338 kernel_modules_list
[i
].name
);
1340 DBG("Modprobe removal successful %s",
1341 kernel_modules_list
[i
].name
);
1352 static int mount_debugfs(char *path
)
1355 char *type
= "debugfs";
1357 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, geteuid(), getegid());
1362 ret
= mount(type
, path
, type
, 0, NULL
);
1364 perror("mount debugfs");
1368 DBG("Mounted debugfs successfully at %s", path
);
1375 * Setup necessary data for kernel tracer action.
1377 static void init_kernel_tracer(void)
1380 char *proc_mounts
= "/proc/mounts";
1382 char *debugfs_path
= NULL
, *lttng_path
;
1385 /* Detect debugfs */
1386 fp
= fopen(proc_mounts
, "r");
1388 ERR("Unable to probe %s", proc_mounts
);
1392 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1393 if (strstr(line
, "debugfs") != NULL
) {
1394 /* Remove first string */
1396 /* Dup string here so we can reuse line later on */
1397 debugfs_path
= strdup(strtok(NULL
, " "));
1398 DBG("Got debugfs path : %s", debugfs_path
);
1405 /* Mount debugfs if needded */
1406 if (debugfs_path
== NULL
) {
1407 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1409 perror("asprintf debugfs path");
1412 ret
= mount_debugfs(debugfs_path
);
1418 /* Modprobe lttng kernel modules */
1419 ret
= modprobe_kernel_modules();
1424 /* Setup lttng kernel path */
1425 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1427 perror("asprintf lttng path");
1431 /* Open debugfs lttng */
1432 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1433 if (kernel_tracer_fd
< 0) {
1434 DBG("Failed to open %s", lttng_path
);
1440 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1450 WARN("No kernel tracer available");
1451 kernel_tracer_fd
= 0;
1456 * Start tracing by creating trace directory and sending FDs to the kernel
1459 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1463 if (session
->kconsumer_fds_sent
== 0) {
1465 * Assign default kernel consumer if no consumer assigned to the kernel
1466 * session. At this point, it's NOT suppose to be 0 but this is an extra
1469 if (session
->consumer_fd
== 0) {
1470 session
->consumer_fd
= kconsumerd_cmd_sock
;
1473 ret
= send_kconsumerd_fds(session
);
1475 ERR("Send kconsumerd fds failed");
1476 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1480 session
->kconsumer_fds_sent
= 1;
1488 * Notify kernel thread to update it's pollfd.
1490 static int notify_kernel_pollfd(void)
1494 /* Inform kernel thread of the new kernel channel */
1495 ret
= write(kernel_poll_pipe
[1], "!", 1);
1497 perror("write kernel poll pipe");
1504 * Allocate a channel structure and fill it.
1506 static struct lttng_channel
*init_default_channel(enum lttng_domain_type domain_type
,
1509 struct lttng_channel
*chan
;
1511 chan
= malloc(sizeof(struct lttng_channel
));
1513 perror("init channel malloc");
1517 if (snprintf(chan
->name
, NAME_MAX
, "%s", name
) < 0) {
1518 perror("snprintf channel name");
1522 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1523 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1524 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1526 switch (domain_type
) {
1527 case LTTNG_DOMAIN_KERNEL
:
1528 chan
->attr
.subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
1529 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1530 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1534 goto error
; /* Not implemented */
1545 * Create a kernel tracer session then create the default channel.
1547 static int create_kernel_session(struct ltt_session
*session
)
1551 DBG("Creating kernel session");
1553 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1555 ret
= LTTCOMM_KERN_SESS_FAIL
;
1559 /* Set kernel consumer socket fd */
1560 if (kconsumerd_cmd_sock
) {
1561 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
1564 ret
= asprintf(&session
->kernel_session
->trace_path
, "%s/kernel",
1567 perror("asprintf kernel traces path");
1571 ret
= mkdir_recursive(session
->kernel_session
->trace_path
,
1572 S_IRWXU
| S_IRWXG
, geteuid(), allowed_group());
1574 if (ret
!= -EEXIST
) {
1575 ERR("Trace directory creation error");
1585 * Using the session list, filled a lttng_session array to send back to the
1586 * client for session listing.
1588 * The session list lock MUST be acquired before calling this function. Use
1589 * lock_session_list() and unlock_session_list().
1591 static void list_lttng_sessions(struct lttng_session
*sessions
)
1594 struct ltt_session
*session
;
1596 DBG("Getting all available session");
1598 * Iterate over session list and append data after the control struct in
1601 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1602 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1603 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
1604 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1605 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
1611 * Fill lttng_channel array of all channels.
1613 static void list_lttng_channels(struct ltt_session
*session
,
1614 struct lttng_channel
*channels
)
1617 struct ltt_kernel_channel
*kchan
;
1619 DBG("Listing channels for session %s", session
->name
);
1621 /* Kernel channels */
1622 if (session
->kernel_session
!= NULL
) {
1623 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
1624 /* Copy lttng_channel struct to array */
1625 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1626 channels
[i
].enabled
= kchan
->enabled
;
1631 /* TODO: Missing UST listing */
1635 * Fill lttng_event array of all events in the channel.
1637 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1638 struct lttng_event
*events
)
1641 * TODO: This is ONLY kernel. Need UST support.
1644 struct ltt_kernel_event
*event
;
1646 DBG("Listing events for channel %s", kchan
->channel
->name
);
1648 /* Kernel channels */
1649 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1650 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1651 events
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1652 events
[i
].enabled
= event
->enabled
;
1653 switch (event
->event
->instrumentation
) {
1654 case LTTNG_KERNEL_TRACEPOINT
:
1655 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1657 case LTTNG_KERNEL_KPROBE
:
1658 case LTTNG_KERNEL_KRETPROBE
:
1659 events
[i
].type
= LTTNG_EVENT_PROBE
;
1660 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
1661 sizeof(struct lttng_kernel_kprobe
));
1663 case LTTNG_KERNEL_FUNCTION
:
1664 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
1665 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
1666 sizeof(struct lttng_kernel_function
));
1674 * Process the command requested by the lttng client within the command
1675 * context structure. This function make sure that the return structure (llm)
1676 * is set and ready for transmission before returning.
1678 * Return any error encountered or 0 for success.
1680 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1682 int ret
= LTTCOMM_OK
;
1684 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1687 * Commands that DO NOT need a session.
1689 switch (cmd_ctx
->lsm
->cmd_type
) {
1690 case LTTNG_CREATE_SESSION
:
1691 case LTTNG_LIST_SESSIONS
:
1692 case LTTNG_LIST_TRACEPOINTS
:
1693 case LTTNG_CALIBRATE
:
1696 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
1697 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session
.name
);
1698 if (cmd_ctx
->session
== NULL
) {
1699 /* If session name not found */
1700 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
1701 ret
= LTTCOMM_SESS_NOT_FOUND
;
1702 } else { /* If no session name specified */
1703 ret
= LTTCOMM_SELECT_SESS
;
1707 /* Acquire lock for the session */
1708 lock_session(cmd_ctx
->session
);
1714 * Check domain type for specific "pre-action".
1716 switch (cmd_ctx
->lsm
->domain
.type
) {
1717 case LTTNG_DOMAIN_KERNEL
:
1718 /* Kernel tracer check */
1719 if (kernel_tracer_fd
== 0) {
1720 init_kernel_tracer();
1721 if (kernel_tracer_fd
== 0) {
1722 ret
= LTTCOMM_KERN_NA
;
1727 /* Need a session for kernel command */
1728 switch (cmd_ctx
->lsm
->cmd_type
) {
1729 case LTTNG_CALIBRATE
:
1730 case LTTNG_CREATE_SESSION
:
1731 case LTTNG_LIST_SESSIONS
:
1732 case LTTNG_LIST_TRACEPOINTS
:
1735 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1736 ret
= create_kernel_session(cmd_ctx
->session
);
1738 ret
= LTTCOMM_KERN_SESS_FAIL
;
1742 /* Start the kernel consumer daemon */
1744 if (kconsumerd_pid
== 0 &&
1745 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1746 ret
= start_kconsumerd();
1758 /* Process by command type */
1759 switch (cmd_ctx
->lsm
->cmd_type
) {
1760 case LTTNG_ADD_CONTEXT
:
1762 struct lttng_kernel_context kctx
;
1764 /* Setup lttng message with no payload */
1765 ret
= setup_lttng_msg(cmd_ctx
, 0);
1770 switch (cmd_ctx
->lsm
->domain
.type
) {
1771 case LTTNG_DOMAIN_KERNEL
:
1772 /* Create Kernel context */
1773 kctx
.ctx
= cmd_ctx
->lsm
->u
.context
.ctx
.ctx
;
1774 kctx
.u
.perf_counter
.type
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.type
;
1775 kctx
.u
.perf_counter
.config
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.config
;
1776 strncpy(kctx
.u
.perf_counter
.name
,
1777 cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.name
,
1778 LTTNG_SYMBOL_NAME_LEN
);
1779 kctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1781 /* Add kernel context to kernel tracer. See context.c */
1782 ret
= add_kernel_context(cmd_ctx
->session
->kernel_session
, &kctx
,
1783 cmd_ctx
->lsm
->u
.context
.event_name
,
1784 cmd_ctx
->lsm
->u
.context
.channel_name
);
1785 if (ret
!= LTTCOMM_OK
) {
1790 /* TODO: Userspace tracing */
1791 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1798 case LTTNG_DISABLE_CHANNEL
:
1800 struct ltt_kernel_channel
*kchan
;
1802 /* Setup lttng message with no payload */
1803 ret
= setup_lttng_msg(cmd_ctx
, 0);
1808 switch (cmd_ctx
->lsm
->domain
.type
) {
1809 case LTTNG_DOMAIN_KERNEL
:
1810 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1811 cmd_ctx
->session
->kernel_session
);
1812 if (kchan
== NULL
) {
1813 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1815 } else if (kchan
->enabled
== 1) {
1816 ret
= kernel_disable_channel(kchan
);
1818 if (ret
!= EEXIST
) {
1819 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1824 kernel_wait_quiescent(kernel_tracer_fd
);
1827 /* TODO: Userspace tracing */
1828 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1835 case LTTNG_DISABLE_EVENT
:
1837 struct ltt_kernel_channel
*kchan
;
1838 struct ltt_kernel_event
*kevent
;
1840 /* Setup lttng message with no payload */
1841 ret
= setup_lttng_msg(cmd_ctx
, 0);
1846 switch (cmd_ctx
->lsm
->domain
.type
) {
1847 case LTTNG_DOMAIN_KERNEL
:
1848 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1849 cmd_ctx
->session
->kernel_session
);
1850 if (kchan
== NULL
) {
1851 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1855 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, kchan
);
1856 if (kevent
!= NULL
) {
1857 DBG("Disabling kernel event %s for channel %s.", kevent
->event
->name
,
1858 kchan
->channel
->name
);
1859 ret
= kernel_disable_event(kevent
);
1861 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1866 kernel_wait_quiescent(kernel_tracer_fd
);
1869 /* TODO: Userspace tracing */
1870 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1877 case LTTNG_DISABLE_ALL_EVENT
:
1879 struct ltt_kernel_channel
*kchan
;
1880 struct ltt_kernel_event
*kevent
;
1882 /* Setup lttng message with no payload */
1883 ret
= setup_lttng_msg(cmd_ctx
, 0);
1888 switch (cmd_ctx
->lsm
->domain
.type
) {
1889 case LTTNG_DOMAIN_KERNEL
:
1890 DBG("Disabling all enabled kernel events");
1891 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1892 cmd_ctx
->session
->kernel_session
);
1893 if (kchan
== NULL
) {
1894 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1898 /* For each event in the kernel session */
1899 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1900 DBG("Disabling kernel event %s for channel %s.",
1901 kevent
->event
->name
, kchan
->channel
->name
);
1902 ret
= kernel_disable_event(kevent
);
1908 /* Quiescent wait after event disable */
1909 kernel_wait_quiescent(kernel_tracer_fd
);
1912 /* TODO: Userspace tracing */
1913 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1920 case LTTNG_ENABLE_CHANNEL
:
1922 struct ltt_kernel_channel
*kchan
;
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
.enable
.channel_name
,
1933 cmd_ctx
->session
->kernel_session
);
1934 if (kchan
== NULL
) {
1935 /* Channel not found, creating it */
1936 DBG("Creating kernel channel");
1938 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1939 &cmd_ctx
->lsm
->u
.channel
.chan
,
1940 cmd_ctx
->session
->kernel_session
->trace_path
);
1942 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1946 /* Notify kernel thread that there is a new channel */
1947 ret
= notify_kernel_pollfd();
1949 ret
= LTTCOMM_FATAL
;
1952 } else if (kchan
->enabled
== 0) {
1953 ret
= kernel_enable_channel(kchan
);
1955 if (ret
!= EEXIST
) {
1956 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1962 kernel_wait_quiescent(kernel_tracer_fd
);
1965 /* TODO: Userspace tracing */
1966 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1973 case LTTNG_ENABLE_EVENT
:
1976 struct ltt_kernel_channel
*kchan
;
1977 struct ltt_kernel_event
*kevent
;
1978 struct lttng_channel
*chan
;
1980 /* Setup lttng message with no payload */
1981 ret
= setup_lttng_msg(cmd_ctx
, 0);
1986 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
1988 switch (cmd_ctx
->lsm
->domain
.type
) {
1989 case LTTNG_DOMAIN_KERNEL
:
1990 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1991 cmd_ctx
->session
->kernel_session
);
1992 if (kchan
== NULL
) {
1993 DBG("Channel not found. Creating channel %s", channel_name
);
1995 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
1997 ret
= LTTCOMM_FATAL
;
2001 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2002 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2004 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2007 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2008 cmd_ctx
->session
->kernel_session
);
2009 if (kchan
== NULL
) {
2010 ERR("Channel %s not found after creation. Internal error, giving up.",
2012 ret
= LTTCOMM_FATAL
;
2017 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, kchan
);
2018 if (kevent
== NULL
) {
2019 DBG("Creating kernel event %s for channel %s.",
2020 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
2021 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, kchan
);
2023 DBG("Enabling kernel event %s for channel %s.",
2024 kevent
->event
->name
, channel_name
);
2025 ret
= kernel_enable_event(kevent
);
2026 if (ret
== -EEXIST
) {
2027 ret
= LTTCOMM_KERN_EVENT_EXIST
;
2033 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2037 kernel_wait_quiescent(kernel_tracer_fd
);
2040 /* TODO: Userspace tracing */
2041 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2047 case LTTNG_ENABLE_ALL_EVENT
:
2051 struct ltt_kernel_channel
*kchan
;
2052 struct ltt_kernel_event
*kevent
;
2053 struct lttng_event
*event_list
;
2054 struct lttng_channel
*chan
;
2056 /* Setup lttng message with no payload */
2057 ret
= setup_lttng_msg(cmd_ctx
, 0);
2062 DBG("Enabling all kernel event");
2064 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2066 switch (cmd_ctx
->lsm
->domain
.type
) {
2067 case LTTNG_DOMAIN_KERNEL
:
2068 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2069 cmd_ctx
->session
->kernel_session
);
2070 if (kchan
== NULL
) {
2071 DBG("Channel not found. Creating channel %s", channel_name
);
2073 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2075 ret
= LTTCOMM_FATAL
;
2079 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2080 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2082 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2085 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2086 cmd_ctx
->session
->kernel_session
);
2087 if (kchan
== NULL
) {
2088 ERR("Channel %s not found after creation. Internal error, giving up.",
2090 ret
= LTTCOMM_FATAL
;
2095 /* For each event in the kernel session */
2096 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
2097 DBG("Enabling kernel event %s for channel %s.",
2098 kevent
->event
->name
, channel_name
);
2099 ret
= kernel_enable_event(kevent
);
2105 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
2107 ret
= LTTCOMM_KERN_LIST_FAIL
;
2111 for (i
= 0; i
< size
; i
++) {
2112 kevent
= trace_kernel_get_event_by_name(event_list
[i
].name
, kchan
);
2113 if (kevent
== NULL
) {
2114 /* Default event type for enable all */
2115 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2116 /* Enable each single tracepoint event */
2117 ret
= kernel_create_event(&event_list
[i
], kchan
);
2119 /* Ignore error here and continue */
2126 /* Quiescent wait after event enable */
2127 kernel_wait_quiescent(kernel_tracer_fd
);
2130 /* TODO: Userspace tracing */
2131 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2138 case LTTNG_LIST_TRACEPOINTS
:
2140 struct lttng_event
*events
;
2141 ssize_t nb_events
= 0;
2143 switch (cmd_ctx
->lsm
->domain
.type
) {
2144 case LTTNG_DOMAIN_KERNEL
:
2145 DBG("Listing kernel events");
2146 nb_events
= kernel_list_events(kernel_tracer_fd
, &events
);
2147 if (nb_events
< 0) {
2148 ret
= LTTCOMM_KERN_LIST_FAIL
;
2153 /* TODO: Userspace listing */
2154 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2159 * Setup lttng message with payload size set to the event list size in
2160 * bytes and then copy list into the llm payload.
2162 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2168 /* Copy event list into message payload */
2169 memcpy(cmd_ctx
->llm
->payload
, events
,
2170 sizeof(struct lttng_event
) * nb_events
);
2177 case LTTNG_START_TRACE
:
2179 struct ltt_kernel_channel
*chan
;
2181 /* Setup lttng message with no payload */
2182 ret
= setup_lttng_msg(cmd_ctx
, 0);
2187 /* Kernel tracing */
2188 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2189 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
2190 DBG("Open kernel metadata");
2191 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
2192 cmd_ctx
->session
->kernel_session
->trace_path
);
2194 ret
= LTTCOMM_KERN_META_FAIL
;
2199 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2200 DBG("Opening kernel metadata stream");
2201 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2202 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
2204 ERR("Kernel create metadata stream failed");
2205 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2211 /* For each channel */
2212 cds_list_for_each_entry(chan
,
2213 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2214 if (chan
->stream_count
== 0) {
2215 ret
= kernel_open_channel_stream(chan
);
2217 ERR("Kernel create channel stream failed");
2218 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2221 /* Update the stream global counter */
2222 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
2226 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
2228 ret
= LTTCOMM_KERN_START_FAIL
;
2232 DBG("Start kernel tracing");
2233 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
2235 ERR("Kernel start session failed");
2236 ret
= LTTCOMM_KERN_START_FAIL
;
2240 /* Quiescent wait after starting trace */
2241 kernel_wait_quiescent(kernel_tracer_fd
);
2244 /* TODO: Start all UST traces */
2249 case LTTNG_STOP_TRACE
:
2251 struct ltt_kernel_channel
*chan
;
2252 /* Setup lttng message with no payload */
2253 ret
= setup_lttng_msg(cmd_ctx
, 0);
2259 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2260 DBG("Stop kernel tracing");
2262 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
2264 ERR("Kernel metadata flush failed");
2267 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2268 ret
= kernel_flush_buffer(chan
);
2270 ERR("Kernel flush buffer error");
2274 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
2276 ERR("Kernel stop session failed");
2277 ret
= LTTCOMM_KERN_STOP_FAIL
;
2281 /* Quiescent wait after stopping trace */
2282 kernel_wait_quiescent(kernel_tracer_fd
);
2285 /* TODO : User-space tracer */
2290 case LTTNG_CREATE_SESSION
:
2292 /* Setup lttng message with no payload */
2293 ret
= setup_lttng_msg(cmd_ctx
, 0);
2298 ret
= create_session(cmd_ctx
->lsm
->session
.name
, cmd_ctx
->lsm
->session
.path
);
2300 if (ret
== -EEXIST
) {
2301 ret
= LTTCOMM_EXIST_SESS
;
2303 ret
= LTTCOMM_FATAL
;
2311 case LTTNG_DESTROY_SESSION
:
2313 /* Setup lttng message with no payload */
2314 ret
= setup_lttng_msg(cmd_ctx
, 0);
2319 /* Clean kernel session teardown */
2320 teardown_kernel_session(cmd_ctx
->session
);
2322 ret
= destroy_session(cmd_ctx
->lsm
->session
.name
);
2324 ret
= LTTCOMM_FATAL
;
2329 * Must notify the kernel thread here to update it's pollfd in order to
2330 * remove the channel(s)' fd just destroyed.
2332 ret
= notify_kernel_pollfd();
2334 ret
= LTTCOMM_FATAL
;
2341 case LTTNG_LIST_DOMAINS
:
2345 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2349 nb_dom
+= cmd_ctx
->session
->ust_trace_count
;
2351 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_domain
) * nb_dom
);
2356 ((struct lttng_domain
*)(cmd_ctx
->llm
->payload
))[0].type
=
2357 LTTNG_DOMAIN_KERNEL
;
2359 /* TODO: User-space tracer domain support */
2363 case LTTNG_LIST_CHANNELS
:
2366 * TODO: Only kernel channels are listed here. UST listing
2367 * is needed on lttng-ust 2.0 release.
2370 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2371 nb_chan
+= cmd_ctx
->session
->kernel_session
->channel_count
;
2374 ret
= setup_lttng_msg(cmd_ctx
,
2375 sizeof(struct lttng_channel
) * nb_chan
);
2380 list_lttng_channels(cmd_ctx
->session
,
2381 (struct lttng_channel
*)(cmd_ctx
->llm
->payload
));
2386 case LTTNG_LIST_EVENTS
:
2389 * TODO: Only kernel events are listed here. UST listing
2390 * is needed on lttng-ust 2.0 release.
2392 size_t nb_event
= 0;
2393 struct ltt_kernel_channel
*kchan
= NULL
;
2395 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2396 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.list
.channel_name
,
2397 cmd_ctx
->session
->kernel_session
);
2398 if (kchan
== NULL
) {
2399 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2402 nb_event
+= kchan
->event_count
;
2405 ret
= setup_lttng_msg(cmd_ctx
,
2406 sizeof(struct lttng_event
) * nb_event
);
2411 DBG("Listing events (%zu events)", nb_event
);
2413 list_lttng_events(kchan
,
2414 (struct lttng_event
*)(cmd_ctx
->llm
->payload
));
2419 case LTTNG_LIST_SESSIONS
:
2421 lock_session_list();
2423 if (session_list_ptr
->count
== 0) {
2424 ret
= LTTCOMM_NO_SESSION
;
2425 unlock_session_list();
2429 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
2430 session_list_ptr
->count
);
2432 unlock_session_list();
2436 /* Filled the session array */
2437 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
2439 unlock_session_list();
2444 case LTTNG_CALIBRATE
:
2446 /* Setup lttng message with no payload */
2447 ret
= setup_lttng_msg(cmd_ctx
, 0);
2452 switch (cmd_ctx
->lsm
->domain
.type
) {
2453 case LTTNG_DOMAIN_KERNEL
:
2455 struct lttng_kernel_calibrate kcalibrate
;
2457 kcalibrate
.type
= cmd_ctx
->lsm
->u
.calibrate
.type
;
2458 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2460 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2466 /* TODO: Userspace tracing */
2467 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2473 case LTTNG_REGISTER_CONSUMER
:
2477 /* Setup lttng message with no payload */
2478 ret
= setup_lttng_msg(cmd_ctx
, 0);
2483 switch (cmd_ctx
->lsm
->domain
.type
) {
2484 case LTTNG_DOMAIN_KERNEL
:
2486 /* Can't register a consumer if there is already one */
2487 if (cmd_ctx
->session
->kernel_session
->consumer_fd
!= 0) {
2488 ret
= LTTCOMM_CONNECT_FAIL
;
2492 sock
= lttcomm_connect_unix_sock(cmd_ctx
->lsm
->u
.reg
.path
);
2494 ret
= LTTCOMM_CONNECT_FAIL
;
2498 cmd_ctx
->session
->kernel_session
->consumer_fd
= sock
;
2502 /* TODO: Userspace tracing */
2503 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2512 /* Undefined command */
2513 ret
= setup_lttng_msg(cmd_ctx
, 0);
2522 /* Set return code */
2523 cmd_ctx
->llm
->ret_code
= ret
;
2525 if (cmd_ctx
->session
) {
2526 unlock_session(cmd_ctx
->session
);
2532 if (cmd_ctx
->llm
== NULL
) {
2533 DBG("Missing llm structure. Allocating one.");
2534 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2538 /* Notify client of error */
2539 cmd_ctx
->llm
->ret_code
= ret
;
2542 if (cmd_ctx
->session
) {
2543 unlock_session(cmd_ctx
->session
);
2549 * This thread manage all clients request using the unix client socket for
2552 static void *thread_manage_clients(void *data
)
2555 struct command_ctx
*cmd_ctx
= NULL
;
2556 struct pollfd pollfd
[2];
2558 DBG("[thread] Manage client started");
2560 ret
= lttcomm_listen_unix_sock(client_sock
);
2565 /* First fd is always the quit pipe */
2566 pollfd
[0].fd
= thread_quit_pipe
[0];
2569 pollfd
[1].fd
= client_sock
;
2570 pollfd
[1].events
= POLLIN
;
2572 /* Notify parent pid that we are ready
2573 * to accept command for client side.
2575 if (opt_sig_parent
) {
2576 kill(ppid
, SIGCHLD
);
2580 DBG("Accepting client command ...");
2582 /* Inifinite blocking call, waiting for transmission */
2583 ret
= poll(pollfd
, 2, -1);
2585 perror("poll client thread");
2589 /* Thread quit pipe has been closed. Killing thread. */
2590 if (pollfd
[0].revents
== POLLNVAL
) {
2592 } else if (pollfd
[1].revents
== POLLERR
) {
2593 ERR("Client socket poll error");
2597 sock
= lttcomm_accept_unix_sock(client_sock
);
2602 /* Allocate context command to process the client request */
2603 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2605 /* Allocate data buffer for reception */
2606 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2607 cmd_ctx
->llm
= NULL
;
2608 cmd_ctx
->session
= NULL
;
2611 * Data is received from the lttng client. The struct
2612 * lttcomm_session_msg (lsm) contains the command and data request of
2615 DBG("Receiving data from client ...");
2616 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2621 // TODO: Validate cmd_ctx including sanity check for security purpose.
2624 * This function dispatch the work to the kernel or userspace tracer
2625 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2626 * informations for the client. The command context struct contains
2627 * everything this function may needs.
2629 ret
= process_client_msg(cmd_ctx
);
2631 /* TODO: Inform client somehow of the fatal error. At this point,
2632 * ret < 0 means that a malloc failed (ENOMEM). */
2633 /* Error detected but still accept command */
2634 clean_command_ctx(&cmd_ctx
);
2638 DBG("Sending response (size: %d, retcode: %d)",
2639 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2640 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2642 ERR("Failed to send data back to client");
2645 clean_command_ctx(&cmd_ctx
);
2647 /* End of transmission */
2652 DBG("Client thread dying");
2660 unlink(client_unix_sock_path
);
2662 clean_command_ctx(&cmd_ctx
);
2668 * usage function on stderr
2670 static void usage(void)
2672 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2673 fprintf(stderr
, " -h, --help Display this usage.\n");
2674 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2675 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2676 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2677 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2678 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2679 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2680 fprintf(stderr
, " -V, --version Show version number.\n");
2681 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2682 fprintf(stderr
, " -q, --quiet No output at all.\n");
2683 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2684 fprintf(stderr
, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2688 * daemon argument parsing
2690 static int parse_args(int argc
, char **argv
)
2694 static struct option long_options
[] = {
2695 { "client-sock", 1, 0, 'c' },
2696 { "apps-sock", 1, 0, 'a' },
2697 { "kconsumerd-cmd-sock", 1, 0, 0 },
2698 { "kconsumerd-err-sock", 1, 0, 0 },
2699 { "daemonize", 0, 0, 'd' },
2700 { "sig-parent", 0, 0, 'S' },
2701 { "help", 0, 0, 'h' },
2702 { "group", 1, 0, 'g' },
2703 { "version", 0, 0, 'V' },
2704 { "quiet", 0, 0, 'q' },
2705 { "verbose", 0, 0, 'v' },
2706 { "verbose-kconsumerd", 0, 0, 'Z' },
2711 int option_index
= 0;
2712 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:Z", long_options
, &option_index
);
2719 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2721 fprintf(stderr
, " with arg %s\n", optarg
);
2725 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2728 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2734 opt_tracing_group
= strdup(optarg
);
2740 fprintf(stdout
, "%s\n", VERSION
);
2746 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2749 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2755 /* Verbose level can increase using multiple -v */
2759 opt_verbose_kconsumerd
+= 1;
2762 /* Unknown option or other error.
2763 * Error is printed by getopt, just return */
2772 * Creates the two needed socket by the daemon.
2773 * apps_sock - The communication socket for all UST apps.
2774 * client_sock - The communication of the cli tool (lttng).
2776 static int init_daemon_socket(void)
2781 old_umask
= umask(0);
2783 /* Create client tool unix socket */
2784 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2785 if (client_sock
< 0) {
2786 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2791 /* File permission MUST be 660 */
2792 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2794 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2799 /* Create the application unix socket */
2800 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2801 if (apps_sock
< 0) {
2802 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2807 /* File permission MUST be 666 */
2808 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2810 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2821 * Check if the global socket is available, and if a daemon is answering
2822 * at the other side. If yes, error is returned.
2824 static int check_existing_daemon(void)
2826 if (access(client_unix_sock_path
, F_OK
) < 0 &&
2827 access(apps_unix_sock_path
, F_OK
) < 0) {
2830 /* Is there anybody out there ? */
2831 if (lttng_session_daemon_alive()) {
2839 * Set the tracing group gid onto the client socket.
2841 * Race window between mkdir and chown is OK because we are going from more
2842 * permissive (root.root) to les permissive (root.tracing).
2844 static int set_permissions(void)
2849 gid
= allowed_group();
2852 WARN("No tracing group detected");
2855 ERR("Missing tracing group. Aborting execution.");
2861 /* Set lttng run dir */
2862 ret
= chown(LTTNG_RUNDIR
, 0, gid
);
2864 ERR("Unable to set group on " LTTNG_RUNDIR
);
2868 /* lttng client socket path */
2869 ret
= chown(client_unix_sock_path
, 0, gid
);
2871 ERR("Unable to set group on %s", client_unix_sock_path
);
2875 /* kconsumerd error socket path */
2876 ret
= chown(kconsumerd_err_unix_sock_path
, 0, gid
);
2878 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2882 DBG("All permissions are set");
2889 * Create the pipe used to wake up the kernel thread.
2891 static int create_kernel_poll_pipe(void)
2893 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2897 * Create the application command pipe to wake thread_manage_apps.
2899 static int create_apps_cmd_pipe(void)
2901 return pipe2(apps_cmd_pipe
, O_CLOEXEC
);
2905 * Create the lttng run directory needed for all global sockets and pipe.
2907 static int create_lttng_rundir(void)
2911 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2913 if (errno
!= EEXIST
) {
2914 ERR("Unable to create " LTTNG_RUNDIR
);
2926 * Setup sockets and directory needed by the kconsumerd communication with the
2929 static int set_kconsumerd_sockets(void)
2933 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
2934 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
2937 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
2938 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
2941 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
2943 if (errno
!= EEXIST
) {
2944 ERR("Failed to create " KCONSUMERD_PATH
);
2950 /* Create the kconsumerd error unix socket */
2951 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
2952 if (kconsumerd_err_sock
< 0) {
2953 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
2958 /* File permission MUST be 660 */
2959 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2961 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
2971 * Signal handler for the daemon
2973 * Simply stop all worker threads, leaving main() return gracefully
2974 * after joining all threads and calling cleanup().
2976 static void sighandler(int sig
)
2980 DBG("SIGPIPE catched");
2983 DBG("SIGINT catched");
2987 DBG("SIGTERM catched");
2996 * Setup signal handler for :
2997 * SIGINT, SIGTERM, SIGPIPE
2999 static int set_signal_handler(void)
3002 struct sigaction sa
;
3005 if ((ret
= sigemptyset(&sigset
)) < 0) {
3006 perror("sigemptyset");
3010 sa
.sa_handler
= sighandler
;
3011 sa
.sa_mask
= sigset
;
3013 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3014 perror("sigaction");
3018 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3019 perror("sigaction");
3023 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3024 perror("sigaction");
3028 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3034 * Set open files limit to unlimited. This daemon can open a large number of
3035 * file descriptors in order to consumer multiple kernel traces.
3037 static void set_ulimit(void)
3042 /* The kernel does not allowed an infinite limit for open files */
3043 lim
.rlim_cur
= 65535;
3044 lim
.rlim_max
= 65535;
3046 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3048 perror("failed to set open files limit");
3055 int main(int argc
, char **argv
)
3059 const char *home_path
;
3061 /* Create thread quit pipe */
3062 if ((ret
= init_thread_quit_pipe()) < 0) {
3066 /* Parse arguments */
3068 if ((ret
= parse_args(argc
, argv
) < 0)) {
3081 /* Check if daemon is UID = 0 */
3082 is_root
= !getuid();
3085 ret
= create_lttng_rundir();
3090 if (strlen(apps_unix_sock_path
) == 0) {
3091 snprintf(apps_unix_sock_path
, PATH_MAX
,
3092 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
3095 if (strlen(client_unix_sock_path
) == 0) {
3096 snprintf(client_unix_sock_path
, PATH_MAX
,
3097 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
3100 home_path
= get_home_dir();
3101 if (home_path
== NULL
) {
3102 /* TODO: Add --socket PATH option */
3103 ERR("Can't get HOME directory for sockets creation.");
3108 if (strlen(apps_unix_sock_path
) == 0) {
3109 snprintf(apps_unix_sock_path
, PATH_MAX
,
3110 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
3113 /* Set the cli tool unix socket path */
3114 if (strlen(client_unix_sock_path
) == 0) {
3115 snprintf(client_unix_sock_path
, PATH_MAX
,
3116 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
3120 DBG("Client socket path %s", client_unix_sock_path
);
3121 DBG("Application socket path %s", apps_unix_sock_path
);
3124 * See if daemon already exist.
3126 if ((ret
= check_existing_daemon()) < 0) {
3127 ERR("Already running daemon.\n");
3129 * We do not goto exit because we must not cleanup()
3130 * because a daemon is already running.
3135 /* After this point, we can safely call cleanup() so goto error is used */
3138 * These actions must be executed as root. We do that *after* setting up
3139 * the sockets path because we MUST make the check for another daemon using
3140 * those paths *before* trying to set the kernel consumer sockets and init
3144 ret
= set_kconsumerd_sockets();
3149 /* Setup kernel tracer */
3150 init_kernel_tracer();
3152 /* Set ulimit for open files */
3156 if ((ret
= set_signal_handler()) < 0) {
3160 /* Setup the needed unix socket */
3161 if ((ret
= init_daemon_socket()) < 0) {
3165 /* Set credentials to socket */
3166 if (is_root
&& ((ret
= set_permissions()) < 0)) {
3170 /* Get parent pid if -S, --sig-parent is specified. */
3171 if (opt_sig_parent
) {
3175 /* Setup the kernel pipe for waking up the kernel thread */
3176 if ((ret
= create_kernel_poll_pipe()) < 0) {
3180 /* Setup the thread apps communication pipe. */
3181 if ((ret
= create_apps_cmd_pipe()) < 0) {
3185 /* Init UST command queue. */
3186 cds_wfq_init(&ust_cmd_queue
.queue
);
3189 * Get session list pointer. This pointer MUST NOT be free().
3190 * This list is statically declared in session.c
3192 session_list_ptr
= get_session_list();
3194 /* Create thread to manage the client socket */
3195 ret
= pthread_create(&client_thread
, NULL
,
3196 thread_manage_clients
, (void *) NULL
);
3198 perror("pthread_create clients");
3202 /* Create thread to dispatch registration */
3203 ret
= pthread_create(&dispatch_thread
, NULL
,
3204 thread_dispatch_ust_registration
, (void *) NULL
);
3206 perror("pthread_create dispatch");
3210 /* Create thread to manage application registration. */
3211 ret
= pthread_create(®_apps_thread
, NULL
,
3212 thread_registration_apps
, (void *) NULL
);
3214 perror("pthread_create registration");
3218 /* Create thread to manage application socket */
3219 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
3221 perror("pthread_create apps");
3225 /* Create kernel thread to manage kernel event */
3226 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
3228 perror("pthread_create kernel");
3232 ret
= pthread_join(kernel_thread
, &status
);
3234 perror("pthread_join");
3235 goto error
; /* join error, exit without cleanup */
3239 ret
= pthread_join(apps_thread
, &status
);
3241 perror("pthread_join");
3242 goto error
; /* join error, exit without cleanup */
3246 ret
= pthread_join(reg_apps_thread
, &status
);
3248 perror("pthread_join");
3249 goto error
; /* join error, exit without cleanup */
3253 ret
= pthread_join(dispatch_thread
, &status
);
3255 perror("pthread_join");
3256 goto error
; /* join error, exit without cleanup */
3260 ret
= pthread_join(client_thread
, &status
);
3262 perror("pthread_join");
3263 goto error
; /* join error, exit without cleanup */
3266 ret
= join_kconsumerd_thread();
3268 perror("join_kconsumerd");
3269 goto error
; /* join error, exit without cleanup */
3275 * cleanup() is called when no other thread is running.