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("%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
);
256 DBG("Unloading kernel modules");
257 modprobe_remove_kernel_modules();
262 * Send data on a unix socket using the liblttsessiondcomm API.
264 * Return lttcomm error code.
266 static int send_unix_sock(int sock
, void *buf
, size_t len
)
268 /* Check valid length */
273 return lttcomm_send_unix_sock(sock
, buf
, len
);
277 * Free memory of a command context structure.
279 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
281 DBG("Clean command context structure");
283 if ((*cmd_ctx
)->llm
) {
284 free((*cmd_ctx
)->llm
);
286 if ((*cmd_ctx
)->lsm
) {
287 free((*cmd_ctx
)->lsm
);
295 * Send all stream fds of kernel channel to the consumer.
297 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
301 struct ltt_kernel_stream
*stream
;
302 struct lttcomm_kconsumerd_header lkh
;
303 struct lttcomm_kconsumerd_msg lkm
;
305 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
307 nb_fd
= channel
->stream_count
;
310 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
311 lkh
.cmd_type
= ADD_STREAM
;
313 DBG("Sending kconsumerd header");
315 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
317 perror("send kconsumerd header");
321 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
322 if (stream
->fd
!= 0) {
324 lkm
.state
= stream
->state
;
325 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
326 lkm
.output
= channel
->channel
->attr
.output
;
327 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
328 lkm
.path_name
[PATH_MAX
- 1] = '\0';
330 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
332 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
334 perror("send kconsumerd fd");
340 DBG("Kconsumerd channel fds sent");
349 * Send all stream fds of the kernel session to the consumer.
351 static int send_kconsumerd_fds(struct ltt_kernel_session
*session
)
354 struct ltt_kernel_channel
*chan
;
355 struct lttcomm_kconsumerd_header lkh
;
356 struct lttcomm_kconsumerd_msg lkm
;
359 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
360 lkh
.cmd_type
= ADD_STREAM
;
362 DBG("Sending kconsumerd header for metadata");
364 ret
= lttcomm_send_unix_sock(session
->consumer_fd
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
366 perror("send kconsumerd header");
370 DBG("Sending metadata stream fd");
372 /* Extra protection. It's NOT suppose to be set to 0 at this point */
373 if (session
->consumer_fd
== 0) {
374 session
->consumer_fd
= kconsumerd_cmd_sock
;
377 if (session
->metadata_stream_fd
!= 0) {
378 /* Send metadata stream fd first */
379 lkm
.fd
= session
->metadata_stream_fd
;
380 lkm
.state
= ACTIVE_FD
;
381 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
382 lkm
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
383 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
384 lkm
.path_name
[PATH_MAX
- 1] = '\0';
386 ret
= lttcomm_send_fds_unix_sock(session
->consumer_fd
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
388 perror("send kconsumerd fd");
393 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
394 ret
= send_kconsumerd_channel_fds(session
->consumer_fd
, chan
);
400 DBG("Kconsumerd fds (metadata and channel streams) sent");
410 * Notify apps by writing 42 to a named pipe using name. Every applications
411 * waiting for a ltt-sessiond will be notified and re-register automatically to
412 * the session daemon.
414 * Return open or write error value.
416 static int notify_apps(const char *name
)
421 DBG("Notify the global application pipe");
423 /* Try opening the global pipe */
424 fd
= open(name
, O_WRONLY
);
429 /* Notify by writing on the pipe */
430 ret
= write(fd
, "42", 2);
438 #endif /* DISABLED */
441 * Setup the outgoing data buffer for the response (llm) by allocating the
442 * right amount of memory and copying the original information from the lsm
445 * Return total size of the buffer pointed by buf.
447 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
453 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
454 if (cmd_ctx
->llm
== NULL
) {
460 /* Copy common data */
461 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
462 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
464 cmd_ctx
->llm
->data_size
= size
;
465 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
474 * Update the kernel pollfd set of all channel fd available over all tracing
475 * session. Add the wakeup pipe at the end of the set.
477 static int update_kernel_pollfd(void)
481 * The wakup pipe and the quit pipe are needed so the number of fds starts
482 * at 2 for those pipes.
484 unsigned int nb_fd
= 2;
485 struct ltt_session
*session
;
486 struct ltt_kernel_channel
*channel
;
488 DBG("Updating kernel_pollfd");
490 /* Get the number of channel of all kernel session */
492 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
493 lock_session(session
);
494 if (session
->kernel_session
== NULL
) {
495 unlock_session(session
);
498 nb_fd
+= session
->kernel_session
->channel_count
;
499 unlock_session(session
);
502 DBG("Resizing kernel_pollfd to size %d", nb_fd
);
504 kernel_pollfd
= realloc(kernel_pollfd
, nb_fd
* sizeof(struct pollfd
));
505 if (kernel_pollfd
== NULL
) {
506 perror("malloc kernel_pollfd");
510 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
511 lock_session(session
);
512 if (session
->kernel_session
== NULL
) {
513 unlock_session(session
);
517 ERR("To much channel for kernel_pollfd size");
518 unlock_session(session
);
521 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
522 kernel_pollfd
[i
].fd
= channel
->fd
;
523 kernel_pollfd
[i
].events
= POLLIN
| POLLRDNORM
;
526 unlock_session(session
);
528 unlock_session_list();
530 /* Adding wake up pipe */
531 kernel_pollfd
[nb_fd
- 2].fd
= kernel_poll_pipe
[0];
532 kernel_pollfd
[nb_fd
- 2].events
= POLLIN
;
534 /* Adding the quit pipe */
535 kernel_pollfd
[nb_fd
- 1].fd
= thread_quit_pipe
[0];
540 unlock_session_list();
545 * Find the channel fd from 'fd' over all tracing session. When found, check
546 * for new channel stream and send those stream fds to the kernel consumer.
548 * Useful for CPU hotplug feature.
550 static int update_kernel_stream(int fd
)
553 struct ltt_session
*session
;
554 struct ltt_kernel_channel
*channel
;
556 DBG("Updating kernel streams for channel fd %d", fd
);
559 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
560 lock_session(session
);
561 if (session
->kernel_session
== NULL
) {
562 unlock_session(session
);
566 /* This is not suppose to be 0 but this is an extra security check */
567 if (session
->kernel_session
->consumer_fd
== 0) {
568 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
571 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
572 if (channel
->fd
== fd
) {
573 DBG("Channel found, updating kernel streams");
574 ret
= kernel_open_channel_stream(channel
);
580 * Have we already sent fds to the consumer? If yes, it means that
581 * tracing is started so it is safe to send our updated stream fds.
583 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
584 ret
= send_kconsumerd_channel_fds(session
->kernel_session
->consumer_fd
,
593 unlock_session(session
);
597 unlock_session_list();
599 unlock_session(session
);
605 * This thread manage event coming from the kernel.
607 * Features supported in this thread:
610 static void *thread_manage_kernel(void *data
)
612 int ret
, i
, nb_fd
= 0;
614 int update_poll_flag
= 1;
616 DBG("Thread manage kernel started");
619 if (update_poll_flag
== 1) {
620 nb_fd
= update_kernel_pollfd();
624 update_poll_flag
= 0;
627 DBG("Polling on %d fds", nb_fd
);
629 /* Poll infinite value of time */
630 ret
= poll(kernel_pollfd
, nb_fd
, -1);
632 perror("poll kernel thread");
634 } else if (ret
== 0) {
635 /* Should not happen since timeout is infinite */
639 /* Thread quit pipe has been closed. Killing thread. */
640 if (kernel_pollfd
[nb_fd
- 1].revents
== POLLNVAL
) {
644 DBG("Kernel poll event triggered");
647 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
650 switch (kernel_pollfd
[nb_fd
- 2].revents
) {
652 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
653 update_poll_flag
= 1;
661 for (i
= 0; i
< nb_fd
; i
++) {
662 switch (kernel_pollfd
[i
].revents
) {
664 * New CPU detected by the kernel. Adding kernel stream to kernel
665 * session and updating the kernel consumer
667 case POLLIN
| POLLRDNORM
:
668 ret
= update_kernel_stream(kernel_pollfd
[i
].fd
);
678 DBG("Kernel thread dying");
683 close(kernel_poll_pipe
[0]);
684 close(kernel_poll_pipe
[1]);
689 * This thread manage the kconsumerd error sent back to the session daemon.
691 static void *thread_manage_kconsumerd(void *data
)
694 enum lttcomm_return_code code
;
695 struct pollfd pollfd
[2];
697 DBG("[thread] Manage kconsumerd started");
699 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
704 /* First fd is always the quit pipe */
705 pollfd
[0].fd
= thread_quit_pipe
[0];
708 pollfd
[1].fd
= kconsumerd_err_sock
;
709 pollfd
[1].events
= POLLIN
;
711 /* Inifinite blocking call, waiting for transmission */
712 ret
= poll(pollfd
, 2, -1);
714 perror("poll kconsumerd thread");
718 /* Thread quit pipe has been closed. Killing thread. */
719 if (pollfd
[0].revents
== POLLNVAL
) {
721 } else if (pollfd
[1].revents
== POLLERR
) {
722 ERR("Kconsumerd err socket poll error");
726 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
731 /* Getting status code from kconsumerd */
732 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
737 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
738 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
739 if (kconsumerd_cmd_sock
< 0) {
740 sem_post(&kconsumerd_sem
);
741 perror("kconsumerd connect");
744 /* Signal condition to tell that the kconsumerd is ready */
745 sem_post(&kconsumerd_sem
);
746 DBG("Kconsumerd command socket ready");
748 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
749 lttcomm_get_readable_code(-code
));
753 /* Kconsumerd err socket */
755 pollfd
[1].events
= POLLIN
;
757 /* Inifinite blocking call, waiting for transmission */
758 ret
= poll(pollfd
, 2, -1);
760 perror("poll kconsumerd thread");
764 /* Thread quit pipe has been closed. Killing thread. */
765 if (pollfd
[0].revents
== POLLNVAL
) {
767 } else if (pollfd
[1].revents
== POLLERR
) {
768 ERR("Kconsumerd err socket second poll error");
772 /* Wait for any kconsumerd error */
773 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
775 ERR("Kconsumerd closed the command socket");
779 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
782 DBG("Kconsumerd thread dying");
783 if (kconsumerd_err_sock
) {
784 close(kconsumerd_err_sock
);
786 if (kconsumerd_cmd_sock
) {
787 close(kconsumerd_cmd_sock
);
793 unlink(kconsumerd_err_unix_sock_path
);
794 unlink(kconsumerd_cmd_unix_sock_path
);
801 * Reallocate the apps command pollfd structure of nb_fd size.
803 * The first two fds must be there at all time.
805 static int update_apps_cmd_pollfd(unsigned int nb_fd
, unsigned int old_nb_fd
,
806 struct pollfd
**pollfd
)
809 struct pollfd
*old_pollfd
= NULL
;
811 /* Can't accept pollfd less than 2 */
818 old_pollfd
= *pollfd
;
821 *pollfd
= malloc(nb_fd
* sizeof(struct pollfd
));
822 if (*pollfd
== NULL
) {
823 perror("malloc manage apps pollfd");
827 /* First fd is always the quit pipe */
828 (*pollfd
)[0].fd
= thread_quit_pipe
[0];
829 /* Apps command pipe */
830 (*pollfd
)[1].fd
= apps_cmd_pipe
[0];
831 (*pollfd
)[1].events
= POLLIN
;
833 /* Start count after the two pipes below */
835 for (i
= 2; i
< old_nb_fd
; i
++) {
836 /* Add to new pollfd */
837 if (old_pollfd
[i
].fd
!= -1) {
838 (*pollfd
)[count
].fd
= old_pollfd
[i
].fd
;
839 (*pollfd
)[count
].events
= POLLHUP
| POLLNVAL
| POLLERR
;
844 ERR("Updating poll fd wrong size");
849 /* Destroy old pollfd */
852 DBG("Apps cmd pollfd realloc of size %d", nb_fd
);
858 /* Destroy old pollfd */
864 * This thread manage application communication.
866 static void *thread_manage_apps(void *data
)
869 unsigned int nb_fd
= 2;
870 int update_poll_flag
= 1;
871 struct pollfd
*pollfd
= NULL
;
872 struct ust_command ust_cmd
;
874 DBG("[thread] Manage application started");
879 /* See if we have a valid socket to add to pollfd */
880 if (ust_cmd
.sock
!= -1) {
882 update_poll_flag
= 1;
885 /* The pollfd struct must be updated */
886 if (update_poll_flag
) {
887 ret
= update_apps_cmd_pollfd(nb_fd
, ARRAY_SIZE(pollfd
), &pollfd
);
889 /* malloc failed so we quit */
893 if (ust_cmd
.sock
!= -1) {
894 /* Update pollfd with the new UST socket */
895 DBG("Adding sock %d to apps cmd pollfd", ust_cmd
.sock
);
896 pollfd
[nb_fd
- 1].fd
= ust_cmd
.sock
;
897 pollfd
[nb_fd
- 1].events
= POLLHUP
| POLLNVAL
| POLLERR
;
902 DBG("Apps thread polling on %d fds", nb_fd
);
904 /* Inifinite blocking call, waiting for transmission */
905 ret
= poll(pollfd
, nb_fd
, -1);
907 perror("poll apps thread");
911 /* Thread quit pipe has been closed. Killing thread. */
912 if (pollfd
[0].revents
== POLLNVAL
) {
915 /* apps_cmd_pipe pipe events */
916 switch (pollfd
[1].revents
) {
918 ERR("Apps command pipe poll error");
922 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
923 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
924 perror("read apps cmd pipe");
928 /* Register applicaton to the session daemon */
929 ret
= register_traceable_app(&ust_cmd
.reg_msg
, ust_cmd
.sock
);
931 /* Only critical ENOMEM error can be returned here */
935 ret
= ustctl_register_done(ust_cmd
.sock
);
938 * If the registration is not possible, we simply unregister
939 * the apps and continue
941 unregister_traceable_app(ust_cmd
.sock
);
948 for (i
= 2; i
< count
; i
++) {
949 /* Apps socket is closed/hungup */
950 switch (pollfd
[i
].revents
) {
955 unregister_traceable_app(pollfd
[i
].fd
);
956 /* Indicate to remove this fd from the pollfd */
963 if (nb_fd
!= count
) {
964 update_poll_flag
= 1;
969 DBG("Application communication apps dying");
970 close(apps_cmd_pipe
[0]);
971 close(apps_cmd_pipe
[1]);
979 * Dispatch request from the registration threads to the application
980 * communication thread.
982 static void *thread_dispatch_ust_registration(void *data
)
985 struct cds_wfq_node
*node
;
986 struct ust_command
*ust_cmd
= NULL
;
988 DBG("[thread] Dispatch UST command started");
990 while (!dispatch_thread_exit
) {
991 /* Atomically prepare the queue futex */
992 futex_nto1_prepare(&ust_cmd_queue
.futex
);
995 /* Dequeue command for registration */
996 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
998 DBG("Waked up but nothing in the UST command queue");
999 /* Continue thread execution */
1003 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1005 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1006 " gid:%d sock:%d name:%s (version %d.%d)",
1007 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1008 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1009 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1010 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1012 * Inform apps thread of the new application registration. This
1013 * call is blocking so we can be assured that the data will be read
1014 * at some point in time or wait to the end of the world :)
1016 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1017 sizeof(struct ust_command
));
1019 perror("write apps cmd pipe");
1020 if (errno
== EBADF
) {
1022 * We can't inform the application thread to process
1023 * registration. We will exit or else application
1024 * registration will not occur and tracing will never
1031 } while (node
!= NULL
);
1033 /* Futex wait on queue. Blocking call on futex() */
1034 futex_nto1_wait(&ust_cmd_queue
.futex
);
1038 DBG("Dispatch thread dying");
1043 * This thread manage application registration.
1045 static void *thread_registration_apps(void *data
)
1048 struct pollfd pollfd
[2];
1050 * Get allocated in this thread, enqueued to a global queue, dequeued and
1051 * freed in the manage apps thread.
1053 struct ust_command
*ust_cmd
= NULL
;
1055 DBG("[thread] Manage application registration started");
1057 ret
= lttcomm_listen_unix_sock(apps_sock
);
1062 /* First fd is always the quit pipe */
1063 pollfd
[0].fd
= thread_quit_pipe
[0];
1066 pollfd
[1].fd
= apps_sock
;
1067 pollfd
[1].events
= POLLIN
;
1069 /* Notify all applications to register */
1070 //notify_apps(default_global_apps_pipe);
1073 DBG("Accepting application registration");
1075 /* Inifinite blocking call, waiting for transmission */
1076 ret
= poll(pollfd
, 2, -1);
1078 perror("poll register apps thread");
1082 /* Thread quit pipe has been closed. Killing thread. */
1083 if (pollfd
[0].revents
== POLLNVAL
) {
1085 } else if (pollfd
[1].revents
== POLLERR
) {
1086 ERR("Register apps socket poll error");
1090 sock
= lttcomm_accept_unix_sock(apps_sock
);
1095 /* Create UST registration command for enqueuing */
1096 ust_cmd
= malloc(sizeof(struct ust_command
));
1097 if (ust_cmd
== NULL
) {
1098 perror("ust command malloc");
1103 * Using message-based transmissions to ensure we don't have to deal
1104 * with partially received messages.
1106 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1107 sizeof(struct ust_register_msg
));
1108 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1110 perror("lttcomm_recv_unix_sock register apps");
1112 ERR("Wrong size received on apps register");
1119 ust_cmd
->sock
= sock
;
1121 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1122 " gid:%d sock:%d name:%s (version %d.%d)",
1123 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1124 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1125 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1126 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1128 * Lock free enqueue the registration request.
1129 * The red pill has been taken! This apps will be part of the *system*
1131 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1134 * Wake the registration queue futex.
1135 * Implicit memory barrier with the exchange in cds_wfq_enqueue.
1137 futex_nto1_wake(&ust_cmd_queue
.futex
);
1141 DBG("Register apps thread dying");
1149 unlink(apps_unix_sock_path
);
1154 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
1155 * exec or it will fails.
1157 static int spawn_kconsumerd_thread(void)
1161 /* Setup semaphore */
1162 sem_init(&kconsumerd_sem
, 0, 0);
1164 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
1166 perror("pthread_create kconsumerd");
1170 /* Wait for the kconsumerd thread to be ready */
1171 sem_wait(&kconsumerd_sem
);
1173 if (kconsumerd_pid
== 0) {
1174 ERR("Kconsumerd did not start");
1181 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1186 * Join kernel consumer thread
1188 static int join_kconsumerd_thread(void)
1193 if (kconsumerd_pid
!= 0) {
1194 ret
= kill(kconsumerd_pid
, SIGTERM
);
1196 ERR("Error killing kconsumerd");
1199 return pthread_join(kconsumerd_thread
, &status
);
1206 * Fork and exec a kernel consumer daemon (kconsumerd).
1208 * Return pid if successful else -1.
1210 static pid_t
spawn_kconsumerd(void)
1214 const char *verbosity
;
1216 DBG("Spawning kconsumerd");
1223 if (opt_verbose
> 1 || opt_verbose_kconsumerd
) {
1224 verbosity
= "--verbose";
1226 verbosity
= "--quiet";
1228 execl(INSTALL_BIN_PATH
"/ltt-kconsumerd", "ltt-kconsumerd", verbosity
, NULL
);
1230 perror("kernel start consumer exec");
1233 } else if (pid
> 0) {
1237 perror("kernel start consumer fork");
1247 * Spawn the kconsumerd daemon and session daemon thread.
1249 static int start_kconsumerd(void)
1253 pthread_mutex_lock(&kconsumerd_pid_mutex
);
1254 if (kconsumerd_pid
!= 0) {
1255 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1259 ret
= spawn_kconsumerd();
1261 ERR("Spawning kconsumerd failed");
1262 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1263 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1267 /* Setting up the global kconsumerd_pid */
1268 kconsumerd_pid
= ret
;
1269 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1271 DBG("Kconsumerd pid %d", ret
);
1273 DBG("Spawning kconsumerd thread");
1274 ret
= spawn_kconsumerd_thread();
1276 ERR("Fatal error spawning kconsumerd thread");
1288 * modprobe_kernel_modules
1290 static int modprobe_kernel_modules(void)
1295 for (i
= 0; i
< ARRAY_SIZE(kernel_modules_list
); i
++) {
1296 ret
= snprintf(modprobe
, sizeof(modprobe
),
1297 "/sbin/modprobe %s%s",
1298 kernel_modules_list
[i
].required
? "" : "--quiet ",
1299 kernel_modules_list
[i
].name
);
1301 perror("snprintf modprobe");
1304 modprobe
[sizeof(modprobe
) - 1] = '\0';
1305 ret
= system(modprobe
);
1307 ERR("Unable to launch modprobe for module %s",
1308 kernel_modules_list
[i
].name
);
1309 } else if (kernel_modules_list
[i
].required
1310 && WEXITSTATUS(ret
) != 0) {
1311 ERR("Unable to load module %s",
1312 kernel_modules_list
[i
].name
);
1314 DBG("Modprobe successfully %s",
1315 kernel_modules_list
[i
].name
);
1324 * modprobe_remove_kernel_modules
1325 * Remove modules in reverse load order.
1327 static int modprobe_remove_kernel_modules(void)
1332 for (i
= ARRAY_SIZE(kernel_modules_list
) - 1; i
>= 0; i
--) {
1333 ret
= snprintf(modprobe
, sizeof(modprobe
),
1334 "/sbin/modprobe --remove --quiet %s",
1335 kernel_modules_list
[i
].name
);
1337 perror("snprintf modprobe --remove");
1340 modprobe
[sizeof(modprobe
) - 1] = '\0';
1341 ret
= system(modprobe
);
1343 ERR("Unable to launch modprobe --remove for module %s",
1344 kernel_modules_list
[i
].name
);
1345 } else if (kernel_modules_list
[i
].required
1346 && WEXITSTATUS(ret
) != 0) {
1347 ERR("Unable to remove module %s",
1348 kernel_modules_list
[i
].name
);
1350 DBG("Modprobe removal successful %s",
1351 kernel_modules_list
[i
].name
);
1362 static int mount_debugfs(char *path
)
1365 char *type
= "debugfs";
1367 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, geteuid(), getegid());
1372 ret
= mount(type
, path
, type
, 0, NULL
);
1374 perror("mount debugfs");
1378 DBG("Mounted debugfs successfully at %s", path
);
1385 * Setup necessary data for kernel tracer action.
1387 static void init_kernel_tracer(void)
1390 char *proc_mounts
= "/proc/mounts";
1392 char *debugfs_path
= NULL
, *lttng_path
;
1395 /* Detect debugfs */
1396 fp
= fopen(proc_mounts
, "r");
1398 ERR("Unable to probe %s", proc_mounts
);
1402 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1403 if (strstr(line
, "debugfs") != NULL
) {
1404 /* Remove first string */
1406 /* Dup string here so we can reuse line later on */
1407 debugfs_path
= strdup(strtok(NULL
, " "));
1408 DBG("Got debugfs path : %s", debugfs_path
);
1415 /* Mount debugfs if needded */
1416 if (debugfs_path
== NULL
) {
1417 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1419 perror("asprintf debugfs path");
1422 ret
= mount_debugfs(debugfs_path
);
1428 /* Modprobe lttng kernel modules */
1429 ret
= modprobe_kernel_modules();
1434 /* Setup lttng kernel path */
1435 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1437 perror("asprintf lttng path");
1441 /* Open debugfs lttng */
1442 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1443 if (kernel_tracer_fd
< 0) {
1444 DBG("Failed to open %s", lttng_path
);
1450 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1460 WARN("No kernel tracer available");
1461 kernel_tracer_fd
= 0;
1466 * Start tracing by creating trace directory and sending FDs to the kernel
1469 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1473 if (session
->kconsumer_fds_sent
== 0) {
1475 * Assign default kernel consumer if no consumer assigned to the kernel
1476 * session. At this point, it's NOT suppose to be 0 but this is an extra
1479 if (session
->consumer_fd
== 0) {
1480 session
->consumer_fd
= kconsumerd_cmd_sock
;
1483 ret
= send_kconsumerd_fds(session
);
1485 ERR("Send kconsumerd fds failed");
1486 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1490 session
->kconsumer_fds_sent
= 1;
1498 * Notify kernel thread to update it's pollfd.
1500 static int notify_kernel_pollfd(void)
1504 /* Inform kernel thread of the new kernel channel */
1505 ret
= write(kernel_poll_pipe
[1], "!", 1);
1507 perror("write kernel poll pipe");
1514 * Allocate a channel structure and fill it.
1516 static struct lttng_channel
*init_default_channel(enum lttng_domain_type domain_type
,
1519 struct lttng_channel
*chan
;
1521 chan
= malloc(sizeof(struct lttng_channel
));
1523 perror("init channel malloc");
1527 if (snprintf(chan
->name
, NAME_MAX
, "%s", name
) < 0) {
1528 perror("snprintf channel name");
1532 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1533 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1534 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1536 switch (domain_type
) {
1537 case LTTNG_DOMAIN_KERNEL
:
1538 chan
->attr
.subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
1539 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1540 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1544 goto error
; /* Not implemented */
1555 * Create an UST session and add it to the session ust list.
1557 static int create_ust_session(pid_t pid
, struct ltt_session
*session
)
1560 struct ltt_ust_session
*lus
;
1562 DBG("Creating UST session");
1564 lus
= trace_ust_create_session(session
->path
, pid
);
1569 ret
= mkdir_recursive(lus
->path
, S_IRWXU
| S_IRWXG
,
1570 geteuid(), allowed_group());
1572 if (ret
!= -EEXIST
) {
1573 ERR("Trace directory creation error");
1578 /* Create session on the UST tracer */
1579 ret
= ustctl_create_session(lus
);
1592 * Create a kernel tracer session then create the default channel.
1594 static int create_kernel_session(struct ltt_session
*session
)
1598 DBG("Creating kernel session");
1600 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1602 ret
= LTTCOMM_KERN_SESS_FAIL
;
1606 /* Set kernel consumer socket fd */
1607 if (kconsumerd_cmd_sock
) {
1608 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
1611 ret
= mkdir_recursive(session
->kernel_session
->trace_path
,
1612 S_IRWXU
| S_IRWXG
, geteuid(), allowed_group());
1614 if (ret
!= -EEXIST
) {
1615 ERR("Trace directory creation error");
1625 * Using the session list, filled a lttng_session array to send back to the
1626 * client for session listing.
1628 * The session list lock MUST be acquired before calling this function. Use
1629 * lock_session_list() and unlock_session_list().
1631 static void list_lttng_sessions(struct lttng_session
*sessions
)
1634 struct ltt_session
*session
;
1636 DBG("Getting all available session");
1638 * Iterate over session list and append data after the control struct in
1641 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1642 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1643 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
1644 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1645 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
1651 * Fill lttng_channel array of all channels.
1653 static void list_lttng_channels(struct ltt_session
*session
,
1654 struct lttng_channel
*channels
)
1657 struct ltt_kernel_channel
*kchan
;
1659 DBG("Listing channels for session %s", session
->name
);
1661 /* Kernel channels */
1662 if (session
->kernel_session
!= NULL
) {
1663 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
1664 /* Copy lttng_channel struct to array */
1665 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1666 channels
[i
].enabled
= kchan
->enabled
;
1671 /* TODO: Missing UST listing */
1675 * Fill lttng_event array of all events in the channel.
1677 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1678 struct lttng_event
*events
)
1681 * TODO: This is ONLY kernel. Need UST support.
1684 struct ltt_kernel_event
*event
;
1686 DBG("Listing events for channel %s", kchan
->channel
->name
);
1688 /* Kernel channels */
1689 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1690 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1691 events
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1692 events
[i
].enabled
= event
->enabled
;
1693 switch (event
->event
->instrumentation
) {
1694 case LTTNG_KERNEL_TRACEPOINT
:
1695 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1697 case LTTNG_KERNEL_KPROBE
:
1698 case LTTNG_KERNEL_KRETPROBE
:
1699 events
[i
].type
= LTTNG_EVENT_PROBE
;
1700 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
1701 sizeof(struct lttng_kernel_kprobe
));
1703 case LTTNG_KERNEL_FUNCTION
:
1704 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
1705 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
1706 sizeof(struct lttng_kernel_function
));
1714 * Process the command requested by the lttng client within the command
1715 * context structure. This function make sure that the return structure (llm)
1716 * is set and ready for transmission before returning.
1718 * Return any error encountered or 0 for success.
1720 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1722 int ret
= LTTCOMM_OK
;
1724 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1727 * Commands that DO NOT need a session.
1729 switch (cmd_ctx
->lsm
->cmd_type
) {
1730 case LTTNG_CREATE_SESSION
:
1731 case LTTNG_LIST_SESSIONS
:
1732 case LTTNG_LIST_TRACEPOINTS
:
1733 case LTTNG_CALIBRATE
:
1736 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
1737 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session
.name
);
1738 if (cmd_ctx
->session
== NULL
) {
1739 /* If session name not found */
1740 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
1741 ret
= LTTCOMM_SESS_NOT_FOUND
;
1742 } else { /* If no session name specified */
1743 ret
= LTTCOMM_SELECT_SESS
;
1747 /* Acquire lock for the session */
1748 lock_session(cmd_ctx
->session
);
1754 * Check domain type for specific "pre-action".
1756 switch (cmd_ctx
->lsm
->domain
.type
) {
1757 case LTTNG_DOMAIN_KERNEL
:
1758 /* Kernel tracer check */
1759 if (kernel_tracer_fd
== 0) {
1760 init_kernel_tracer();
1761 if (kernel_tracer_fd
== 0) {
1762 ret
= LTTCOMM_KERN_NA
;
1766 /* Need a session for kernel command */
1767 switch (cmd_ctx
->lsm
->cmd_type
) {
1768 case LTTNG_CALIBRATE
:
1769 case LTTNG_CREATE_SESSION
:
1770 case LTTNG_LIST_SESSIONS
:
1771 case LTTNG_LIST_TRACEPOINTS
:
1774 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1775 ret
= create_kernel_session(cmd_ctx
->session
);
1777 ret
= LTTCOMM_KERN_SESS_FAIL
;
1780 /* Start the kernel consumer daemon */
1781 if (kconsumerd_pid
== 0 &&
1782 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1783 ret
= start_kconsumerd();
1795 /* Process by command type */
1796 switch (cmd_ctx
->lsm
->cmd_type
) {
1797 case LTTNG_ADD_CONTEXT
:
1799 struct lttng_kernel_context kctx
;
1801 /* Setup lttng message with no payload */
1802 ret
= setup_lttng_msg(cmd_ctx
, 0);
1807 switch (cmd_ctx
->lsm
->domain
.type
) {
1808 case LTTNG_DOMAIN_KERNEL
:
1809 /* Create Kernel context */
1810 kctx
.ctx
= cmd_ctx
->lsm
->u
.context
.ctx
.ctx
;
1811 kctx
.u
.perf_counter
.type
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.type
;
1812 kctx
.u
.perf_counter
.config
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.config
;
1813 strncpy(kctx
.u
.perf_counter
.name
,
1814 cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.name
,
1815 LTTNG_SYMBOL_NAME_LEN
);
1816 kctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1818 /* Add kernel context to kernel tracer. See context.c */
1819 ret
= add_kernel_context(cmd_ctx
->session
->kernel_session
, &kctx
,
1820 cmd_ctx
->lsm
->u
.context
.event_name
,
1821 cmd_ctx
->lsm
->u
.context
.channel_name
);
1822 if (ret
!= LTTCOMM_OK
) {
1827 /* TODO: Userspace tracing */
1828 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1835 case LTTNG_DISABLE_CHANNEL
:
1837 struct ltt_kernel_channel
*kchan
;
1839 /* Setup lttng message with no payload */
1840 ret
= setup_lttng_msg(cmd_ctx
, 0);
1845 switch (cmd_ctx
->lsm
->domain
.type
) {
1846 case LTTNG_DOMAIN_KERNEL
:
1847 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1848 cmd_ctx
->session
->kernel_session
);
1849 if (kchan
== NULL
) {
1850 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1852 } else if (kchan
->enabled
== 1) {
1853 ret
= kernel_disable_channel(kchan
);
1855 if (ret
!= EEXIST
) {
1856 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1861 kernel_wait_quiescent(kernel_tracer_fd
);
1864 /* TODO: Userspace tracing */
1865 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1872 case LTTNG_DISABLE_EVENT
:
1874 struct ltt_kernel_channel
*kchan
;
1875 struct ltt_kernel_event
*kevent
;
1877 /* Setup lttng message with no payload */
1878 ret
= setup_lttng_msg(cmd_ctx
, 0);
1883 switch (cmd_ctx
->lsm
->domain
.type
) {
1884 case LTTNG_DOMAIN_KERNEL
:
1885 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1886 cmd_ctx
->session
->kernel_session
);
1887 if (kchan
== NULL
) {
1888 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1892 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, kchan
);
1893 if (kevent
!= NULL
) {
1894 DBG("Disabling kernel event %s for channel %s.", kevent
->event
->name
,
1895 kchan
->channel
->name
);
1896 ret
= kernel_disable_event(kevent
);
1898 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1903 kernel_wait_quiescent(kernel_tracer_fd
);
1906 /* TODO: Userspace tracing */
1907 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1914 case LTTNG_DISABLE_ALL_EVENT
:
1916 struct ltt_kernel_channel
*kchan
;
1917 struct ltt_kernel_event
*kevent
;
1919 /* Setup lttng message with no payload */
1920 ret
= setup_lttng_msg(cmd_ctx
, 0);
1925 switch (cmd_ctx
->lsm
->domain
.type
) {
1926 case LTTNG_DOMAIN_KERNEL
:
1927 DBG("Disabling all enabled kernel events");
1928 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1929 cmd_ctx
->session
->kernel_session
);
1930 if (kchan
== NULL
) {
1931 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1935 /* For each event in the kernel session */
1936 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1937 DBG("Disabling kernel event %s for channel %s.",
1938 kevent
->event
->name
, kchan
->channel
->name
);
1939 ret
= kernel_disable_event(kevent
);
1945 /* Quiescent wait after event disable */
1946 kernel_wait_quiescent(kernel_tracer_fd
);
1949 /* TODO: Userspace tracing */
1950 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1957 case LTTNG_ENABLE_CHANNEL
:
1959 struct ltt_kernel_channel
*kchan
;
1961 /* Setup lttng message with no payload */
1962 ret
= setup_lttng_msg(cmd_ctx
, 0);
1967 switch (cmd_ctx
->lsm
->domain
.type
) {
1968 case LTTNG_DOMAIN_KERNEL
:
1969 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1970 cmd_ctx
->session
->kernel_session
);
1971 if (kchan
== NULL
) {
1972 /* Channel not found, creating it */
1973 DBG("Creating kernel channel");
1975 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1976 &cmd_ctx
->lsm
->u
.channel
.chan
,
1977 cmd_ctx
->session
->kernel_session
->trace_path
);
1979 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1983 /* Notify kernel thread that there is a new channel */
1984 ret
= notify_kernel_pollfd();
1986 ret
= LTTCOMM_FATAL
;
1989 } else if (kchan
->enabled
== 0) {
1990 ret
= kernel_enable_channel(kchan
);
1992 if (ret
!= EEXIST
) {
1993 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1999 kernel_wait_quiescent(kernel_tracer_fd
);
2001 case LTTNG_DOMAIN_UST_PID
:
2004 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2011 case LTTNG_ENABLE_EVENT
:
2014 struct ltt_kernel_channel
*kchan
;
2015 struct ltt_kernel_event
*kevent
;
2016 struct lttng_channel
*chan
;
2018 /* Setup lttng message with no payload */
2019 ret
= setup_lttng_msg(cmd_ctx
, 0);
2024 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2026 switch (cmd_ctx
->lsm
->domain
.type
) {
2027 case LTTNG_DOMAIN_KERNEL
:
2028 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2029 cmd_ctx
->session
->kernel_session
);
2030 if (kchan
== NULL
) {
2031 DBG("Channel not found. Creating channel %s", channel_name
);
2033 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2035 ret
= LTTCOMM_FATAL
;
2039 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2040 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2042 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2045 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2046 cmd_ctx
->session
->kernel_session
);
2047 if (kchan
== NULL
) {
2048 ERR("Channel %s not found after creation. Internal error, giving up.",
2050 ret
= LTTCOMM_FATAL
;
2055 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, kchan
);
2056 if (kevent
== NULL
) {
2057 DBG("Creating kernel event %s for channel %s.",
2058 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
2059 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, kchan
);
2061 DBG("Enabling kernel event %s for channel %s.",
2062 kevent
->event
->name
, channel_name
);
2063 ret
= kernel_enable_event(kevent
);
2064 if (ret
== -EEXIST
) {
2065 ret
= LTTCOMM_KERN_EVENT_EXIST
;
2071 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2075 kernel_wait_quiescent(kernel_tracer_fd
);
2078 /* TODO: Userspace tracing */
2079 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2085 case LTTNG_ENABLE_ALL_EVENT
:
2089 struct ltt_kernel_channel
*kchan
;
2090 struct ltt_kernel_event
*kevent
;
2091 struct lttng_event
*event_list
;
2092 struct lttng_channel
*chan
;
2094 /* Setup lttng message with no payload */
2095 ret
= setup_lttng_msg(cmd_ctx
, 0);
2100 DBG("Enabling all kernel event");
2102 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2104 switch (cmd_ctx
->lsm
->domain
.type
) {
2105 case LTTNG_DOMAIN_KERNEL
:
2106 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2107 cmd_ctx
->session
->kernel_session
);
2108 if (kchan
== NULL
) {
2109 DBG("Channel not found. Creating channel %s", channel_name
);
2111 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2113 ret
= LTTCOMM_FATAL
;
2117 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2118 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2120 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2123 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2124 cmd_ctx
->session
->kernel_session
);
2125 if (kchan
== NULL
) {
2126 ERR("Channel %s not found after creation. Internal error, giving up.",
2128 ret
= LTTCOMM_FATAL
;
2133 /* For each event in the kernel session */
2134 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
2135 DBG("Enabling kernel event %s for channel %s.",
2136 kevent
->event
->name
, channel_name
);
2137 ret
= kernel_enable_event(kevent
);
2143 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
2145 ret
= LTTCOMM_KERN_LIST_FAIL
;
2149 for (i
= 0; i
< size
; i
++) {
2150 kevent
= trace_kernel_get_event_by_name(event_list
[i
].name
, kchan
);
2151 if (kevent
== NULL
) {
2152 /* Default event type for enable all */
2153 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2154 /* Enable each single tracepoint event */
2155 ret
= kernel_create_event(&event_list
[i
], kchan
);
2157 /* Ignore error here and continue */
2164 /* Quiescent wait after event enable */
2165 kernel_wait_quiescent(kernel_tracer_fd
);
2168 /* TODO: Userspace tracing */
2169 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2176 case LTTNG_LIST_TRACEPOINTS
:
2178 struct lttng_event
*events
;
2179 ssize_t nb_events
= 0;
2181 switch (cmd_ctx
->lsm
->domain
.type
) {
2182 case LTTNG_DOMAIN_KERNEL
:
2183 DBG("Listing kernel events");
2184 nb_events
= kernel_list_events(kernel_tracer_fd
, &events
);
2185 if (nb_events
< 0) {
2186 ret
= LTTCOMM_KERN_LIST_FAIL
;
2191 /* TODO: Userspace listing */
2192 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2197 * Setup lttng message with payload size set to the event list size in
2198 * bytes and then copy list into the llm payload.
2200 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2206 /* Copy event list into message payload */
2207 memcpy(cmd_ctx
->llm
->payload
, events
,
2208 sizeof(struct lttng_event
) * nb_events
);
2215 case LTTNG_START_TRACE
:
2217 struct ltt_kernel_channel
*chan
;
2219 /* Setup lttng message with no payload */
2220 ret
= setup_lttng_msg(cmd_ctx
, 0);
2225 /* Kernel tracing */
2226 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2227 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
2228 DBG("Open kernel metadata");
2229 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
2230 cmd_ctx
->session
->kernel_session
->trace_path
);
2232 ret
= LTTCOMM_KERN_META_FAIL
;
2237 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2238 DBG("Opening kernel metadata stream");
2239 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2240 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
2242 ERR("Kernel create metadata stream failed");
2243 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2249 /* For each channel */
2250 cds_list_for_each_entry(chan
,
2251 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2252 if (chan
->stream_count
== 0) {
2253 ret
= kernel_open_channel_stream(chan
);
2255 ERR("Kernel create channel stream failed");
2256 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2259 /* Update the stream global counter */
2260 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
2264 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
2266 ret
= LTTCOMM_KERN_START_FAIL
;
2270 DBG("Start kernel tracing");
2271 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
2273 ERR("Kernel start session failed");
2274 ret
= LTTCOMM_KERN_START_FAIL
;
2278 /* Quiescent wait after starting trace */
2279 kernel_wait_quiescent(kernel_tracer_fd
);
2282 /* TODO: Start all UST traces */
2287 case LTTNG_STOP_TRACE
:
2289 struct ltt_kernel_channel
*chan
;
2290 /* Setup lttng message with no payload */
2291 ret
= setup_lttng_msg(cmd_ctx
, 0);
2297 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2298 DBG("Stop kernel tracing");
2300 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
2302 ERR("Kernel metadata flush failed");
2305 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2306 ret
= kernel_flush_buffer(chan
);
2308 ERR("Kernel flush buffer error");
2312 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
2314 ERR("Kernel stop session failed");
2315 ret
= LTTCOMM_KERN_STOP_FAIL
;
2319 /* Quiescent wait after stopping trace */
2320 kernel_wait_quiescent(kernel_tracer_fd
);
2323 /* TODO : User-space tracer */
2328 case LTTNG_CREATE_SESSION
:
2330 /* Setup lttng message with no payload */
2331 ret
= setup_lttng_msg(cmd_ctx
, 0);
2336 ret
= create_session(cmd_ctx
->lsm
->session
.name
, cmd_ctx
->lsm
->session
.path
);
2338 if (ret
== -EEXIST
) {
2339 ret
= LTTCOMM_EXIST_SESS
;
2341 ret
= LTTCOMM_FATAL
;
2349 case LTTNG_DESTROY_SESSION
:
2351 /* Setup lttng message with no payload */
2352 ret
= setup_lttng_msg(cmd_ctx
, 0);
2357 /* Clean kernel session teardown */
2358 teardown_kernel_session(cmd_ctx
->session
);
2360 ret
= destroy_session(cmd_ctx
->lsm
->session
.name
);
2362 ret
= LTTCOMM_FATAL
;
2367 * Must notify the kernel thread here to update it's pollfd in order to
2368 * remove the channel(s)' fd just destroyed.
2370 ret
= notify_kernel_pollfd();
2372 ret
= LTTCOMM_FATAL
;
2379 case LTTNG_LIST_DOMAINS
:
2383 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2387 nb_dom
+= cmd_ctx
->session
->ust_session_list
.count
;
2389 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_domain
) * nb_dom
);
2394 ((struct lttng_domain
*)(cmd_ctx
->llm
->payload
))[0].type
=
2395 LTTNG_DOMAIN_KERNEL
;
2397 /* TODO: User-space tracer domain support */
2401 case LTTNG_LIST_CHANNELS
:
2404 * TODO: Only kernel channels are listed here. UST listing
2405 * is needed on lttng-ust 2.0 release.
2408 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2409 nb_chan
+= cmd_ctx
->session
->kernel_session
->channel_count
;
2412 ret
= setup_lttng_msg(cmd_ctx
,
2413 sizeof(struct lttng_channel
) * nb_chan
);
2418 list_lttng_channels(cmd_ctx
->session
,
2419 (struct lttng_channel
*)(cmd_ctx
->llm
->payload
));
2424 case LTTNG_LIST_EVENTS
:
2427 * TODO: Only kernel events are listed here. UST listing
2428 * is needed on lttng-ust 2.0 release.
2430 size_t nb_event
= 0;
2431 struct ltt_kernel_channel
*kchan
= NULL
;
2433 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2434 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.list
.channel_name
,
2435 cmd_ctx
->session
->kernel_session
);
2436 if (kchan
== NULL
) {
2437 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2440 nb_event
+= kchan
->event_count
;
2443 ret
= setup_lttng_msg(cmd_ctx
,
2444 sizeof(struct lttng_event
) * nb_event
);
2449 DBG("Listing events (%zu events)", nb_event
);
2451 list_lttng_events(kchan
,
2452 (struct lttng_event
*)(cmd_ctx
->llm
->payload
));
2457 case LTTNG_LIST_SESSIONS
:
2459 lock_session_list();
2461 if (session_list_ptr
->count
== 0) {
2462 ret
= LTTCOMM_NO_SESSION
;
2463 unlock_session_list();
2467 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
2468 session_list_ptr
->count
);
2470 unlock_session_list();
2474 /* Filled the session array */
2475 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
2477 unlock_session_list();
2482 case LTTNG_CALIBRATE
:
2484 /* Setup lttng message with no payload */
2485 ret
= setup_lttng_msg(cmd_ctx
, 0);
2490 switch (cmd_ctx
->lsm
->domain
.type
) {
2491 case LTTNG_DOMAIN_KERNEL
:
2493 struct lttng_kernel_calibrate kcalibrate
;
2495 kcalibrate
.type
= cmd_ctx
->lsm
->u
.calibrate
.type
;
2496 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2498 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2504 /* TODO: Userspace tracing */
2505 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2511 case LTTNG_REGISTER_CONSUMER
:
2515 /* Setup lttng message with no payload */
2516 ret
= setup_lttng_msg(cmd_ctx
, 0);
2521 switch (cmd_ctx
->lsm
->domain
.type
) {
2522 case LTTNG_DOMAIN_KERNEL
:
2524 /* Can't register a consumer if there is already one */
2525 if (cmd_ctx
->session
->kernel_session
->consumer_fd
!= 0) {
2526 ret
= LTTCOMM_CONNECT_FAIL
;
2530 sock
= lttcomm_connect_unix_sock(cmd_ctx
->lsm
->u
.reg
.path
);
2532 ret
= LTTCOMM_CONNECT_FAIL
;
2536 cmd_ctx
->session
->kernel_session
->consumer_fd
= sock
;
2540 /* TODO: Userspace tracing */
2541 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2550 /* Undefined command */
2551 ret
= setup_lttng_msg(cmd_ctx
, 0);
2560 /* Set return code */
2561 cmd_ctx
->llm
->ret_code
= ret
;
2563 if (cmd_ctx
->session
) {
2564 unlock_session(cmd_ctx
->session
);
2570 if (cmd_ctx
->llm
== NULL
) {
2571 DBG("Missing llm structure. Allocating one.");
2572 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2576 /* Notify client of error */
2577 cmd_ctx
->llm
->ret_code
= ret
;
2580 if (cmd_ctx
->session
) {
2581 unlock_session(cmd_ctx
->session
);
2587 * This thread manage all clients request using the unix client socket for
2590 static void *thread_manage_clients(void *data
)
2593 struct command_ctx
*cmd_ctx
= NULL
;
2594 struct pollfd pollfd
[2];
2596 DBG("[thread] Manage client started");
2598 ret
= lttcomm_listen_unix_sock(client_sock
);
2603 /* First fd is always the quit pipe */
2604 pollfd
[0].fd
= thread_quit_pipe
[0];
2607 pollfd
[1].fd
= client_sock
;
2608 pollfd
[1].events
= POLLIN
;
2610 /* Notify parent pid that we are ready
2611 * to accept command for client side.
2613 if (opt_sig_parent
) {
2614 kill(ppid
, SIGCHLD
);
2618 DBG("Accepting client command ...");
2620 /* Inifinite blocking call, waiting for transmission */
2621 ret
= poll(pollfd
, 2, -1);
2623 perror("poll client thread");
2627 /* Thread quit pipe has been closed. Killing thread. */
2628 if (pollfd
[0].revents
== POLLNVAL
) {
2630 } else if (pollfd
[1].revents
== POLLERR
) {
2631 ERR("Client socket poll error");
2635 sock
= lttcomm_accept_unix_sock(client_sock
);
2640 /* Allocate context command to process the client request */
2641 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2643 /* Allocate data buffer for reception */
2644 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2645 cmd_ctx
->llm
= NULL
;
2646 cmd_ctx
->session
= NULL
;
2649 * Data is received from the lttng client. The struct
2650 * lttcomm_session_msg (lsm) contains the command and data request of
2653 DBG("Receiving data from client ...");
2654 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2659 // TODO: Validate cmd_ctx including sanity check for security purpose.
2662 * This function dispatch the work to the kernel or userspace tracer
2663 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2664 * informations for the client. The command context struct contains
2665 * everything this function may needs.
2667 ret
= process_client_msg(cmd_ctx
);
2669 /* TODO: Inform client somehow of the fatal error. At this point,
2670 * ret < 0 means that a malloc failed (ENOMEM). */
2671 /* Error detected but still accept command */
2672 clean_command_ctx(&cmd_ctx
);
2676 DBG("Sending response (size: %d, retcode: %d)",
2677 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2678 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2680 ERR("Failed to send data back to client");
2683 clean_command_ctx(&cmd_ctx
);
2685 /* End of transmission */
2690 DBG("Client thread dying");
2698 unlink(client_unix_sock_path
);
2700 clean_command_ctx(&cmd_ctx
);
2706 * usage function on stderr
2708 static void usage(void)
2710 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2711 fprintf(stderr
, " -h, --help Display this usage.\n");
2712 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2713 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2714 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2715 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2716 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2717 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2718 fprintf(stderr
, " -V, --version Show version number.\n");
2719 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2720 fprintf(stderr
, " -q, --quiet No output at all.\n");
2721 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2722 fprintf(stderr
, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2726 * daemon argument parsing
2728 static int parse_args(int argc
, char **argv
)
2732 static struct option long_options
[] = {
2733 { "client-sock", 1, 0, 'c' },
2734 { "apps-sock", 1, 0, 'a' },
2735 { "kconsumerd-cmd-sock", 1, 0, 0 },
2736 { "kconsumerd-err-sock", 1, 0, 0 },
2737 { "daemonize", 0, 0, 'd' },
2738 { "sig-parent", 0, 0, 'S' },
2739 { "help", 0, 0, 'h' },
2740 { "group", 1, 0, 'g' },
2741 { "version", 0, 0, 'V' },
2742 { "quiet", 0, 0, 'q' },
2743 { "verbose", 0, 0, 'v' },
2744 { "verbose-kconsumerd", 0, 0, 'Z' },
2749 int option_index
= 0;
2750 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:Z", long_options
, &option_index
);
2757 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2759 fprintf(stderr
, " with arg %s\n", optarg
);
2763 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2766 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2772 opt_tracing_group
= strdup(optarg
);
2778 fprintf(stdout
, "%s\n", VERSION
);
2784 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2787 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2793 /* Verbose level can increase using multiple -v */
2797 opt_verbose_kconsumerd
+= 1;
2800 /* Unknown option or other error.
2801 * Error is printed by getopt, just return */
2810 * Creates the two needed socket by the daemon.
2811 * apps_sock - The communication socket for all UST apps.
2812 * client_sock - The communication of the cli tool (lttng).
2814 static int init_daemon_socket(void)
2819 old_umask
= umask(0);
2821 /* Create client tool unix socket */
2822 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2823 if (client_sock
< 0) {
2824 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2829 /* File permission MUST be 660 */
2830 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2832 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2837 /* Create the application unix socket */
2838 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2839 if (apps_sock
< 0) {
2840 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2845 /* File permission MUST be 666 */
2846 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2848 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2859 * Check if the global socket is available, and if a daemon is answering
2860 * at the other side. If yes, error is returned.
2862 static int check_existing_daemon(void)
2864 if (access(client_unix_sock_path
, F_OK
) < 0 &&
2865 access(apps_unix_sock_path
, F_OK
) < 0) {
2868 /* Is there anybody out there ? */
2869 if (lttng_session_daemon_alive()) {
2877 * Set the tracing group gid onto the client socket.
2879 * Race window between mkdir and chown is OK because we are going from more
2880 * permissive (root.root) to les permissive (root.tracing).
2882 static int set_permissions(void)
2887 gid
= allowed_group();
2890 WARN("No tracing group detected");
2893 ERR("Missing tracing group. Aborting execution.");
2899 /* Set lttng run dir */
2900 ret
= chown(LTTNG_RUNDIR
, 0, gid
);
2902 ERR("Unable to set group on " LTTNG_RUNDIR
);
2906 /* lttng client socket path */
2907 ret
= chown(client_unix_sock_path
, 0, gid
);
2909 ERR("Unable to set group on %s", client_unix_sock_path
);
2913 /* kconsumerd error socket path */
2914 ret
= chown(kconsumerd_err_unix_sock_path
, 0, gid
);
2916 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2920 DBG("All permissions are set");
2927 * Create the pipe used to wake up the kernel thread.
2929 static int create_kernel_poll_pipe(void)
2931 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2935 * Create the application command pipe to wake thread_manage_apps.
2937 static int create_apps_cmd_pipe(void)
2939 return pipe2(apps_cmd_pipe
, O_CLOEXEC
);
2943 * Create the lttng run directory needed for all global sockets and pipe.
2945 static int create_lttng_rundir(void)
2949 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2951 if (errno
!= EEXIST
) {
2952 ERR("Unable to create " LTTNG_RUNDIR
);
2964 * Setup sockets and directory needed by the kconsumerd communication with the
2967 static int set_kconsumerd_sockets(void)
2971 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
2972 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
2975 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
2976 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
2979 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
2981 if (errno
!= EEXIST
) {
2982 ERR("Failed to create " KCONSUMERD_PATH
);
2988 /* Create the kconsumerd error unix socket */
2989 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
2990 if (kconsumerd_err_sock
< 0) {
2991 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
2996 /* File permission MUST be 660 */
2997 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2999 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
3009 * Signal handler for the daemon
3011 * Simply stop all worker threads, leaving main() return gracefully
3012 * after joining all threads and calling cleanup().
3014 static void sighandler(int sig
)
3018 DBG("SIGPIPE catched");
3021 DBG("SIGINT catched");
3025 DBG("SIGTERM catched");
3034 * Setup signal handler for :
3035 * SIGINT, SIGTERM, SIGPIPE
3037 static int set_signal_handler(void)
3040 struct sigaction sa
;
3043 if ((ret
= sigemptyset(&sigset
)) < 0) {
3044 perror("sigemptyset");
3048 sa
.sa_handler
= sighandler
;
3049 sa
.sa_mask
= sigset
;
3051 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3052 perror("sigaction");
3056 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3057 perror("sigaction");
3061 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3062 perror("sigaction");
3066 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3072 * Set open files limit to unlimited. This daemon can open a large number of
3073 * file descriptors in order to consumer multiple kernel traces.
3075 static void set_ulimit(void)
3080 /* The kernel does not allowed an infinite limit for open files */
3081 lim
.rlim_cur
= 65535;
3082 lim
.rlim_max
= 65535;
3084 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3086 perror("failed to set open files limit");
3093 int main(int argc
, char **argv
)
3097 const char *home_path
;
3099 /* Create thread quit pipe */
3100 if ((ret
= init_thread_quit_pipe()) < 0) {
3104 /* Parse arguments */
3106 if ((ret
= parse_args(argc
, argv
) < 0)) {
3119 /* Check if daemon is UID = 0 */
3120 is_root
= !getuid();
3123 ret
= create_lttng_rundir();
3128 if (strlen(apps_unix_sock_path
) == 0) {
3129 snprintf(apps_unix_sock_path
, PATH_MAX
,
3130 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
3133 if (strlen(client_unix_sock_path
) == 0) {
3134 snprintf(client_unix_sock_path
, PATH_MAX
,
3135 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
3138 home_path
= get_home_dir();
3139 if (home_path
== NULL
) {
3140 /* TODO: Add --socket PATH option */
3141 ERR("Can't get HOME directory for sockets creation.");
3146 if (strlen(apps_unix_sock_path
) == 0) {
3147 snprintf(apps_unix_sock_path
, PATH_MAX
,
3148 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
3151 /* Set the cli tool unix socket path */
3152 if (strlen(client_unix_sock_path
) == 0) {
3153 snprintf(client_unix_sock_path
, PATH_MAX
,
3154 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
3158 DBG("Client socket path %s", client_unix_sock_path
);
3159 DBG("Application socket path %s", apps_unix_sock_path
);
3162 * See if daemon already exist.
3164 if ((ret
= check_existing_daemon()) < 0) {
3165 ERR("Already running daemon.\n");
3167 * We do not goto exit because we must not cleanup()
3168 * because a daemon is already running.
3173 /* After this point, we can safely call cleanup() so goto error is used */
3176 * These actions must be executed as root. We do that *after* setting up
3177 * the sockets path because we MUST make the check for another daemon using
3178 * those paths *before* trying to set the kernel consumer sockets and init
3182 ret
= set_kconsumerd_sockets();
3187 /* Setup kernel tracer */
3188 init_kernel_tracer();
3190 /* Set ulimit for open files */
3194 if ((ret
= set_signal_handler()) < 0) {
3198 /* Setup the needed unix socket */
3199 if ((ret
= init_daemon_socket()) < 0) {
3203 /* Set credentials to socket */
3204 if (is_root
&& ((ret
= set_permissions()) < 0)) {
3208 /* Get parent pid if -S, --sig-parent is specified. */
3209 if (opt_sig_parent
) {
3213 /* Setup the kernel pipe for waking up the kernel thread */
3214 if ((ret
= create_kernel_poll_pipe()) < 0) {
3218 /* Setup the thread apps communication pipe. */
3219 if ((ret
= create_apps_cmd_pipe()) < 0) {
3223 /* Init UST command queue. */
3224 cds_wfq_init(&ust_cmd_queue
.queue
);
3227 * Get session list pointer. This pointer MUST NOT be free().
3228 * This list is statically declared in session.c
3230 session_list_ptr
= get_session_list();
3232 /* Create thread to manage the client socket */
3233 ret
= pthread_create(&client_thread
, NULL
,
3234 thread_manage_clients
, (void *) NULL
);
3236 perror("pthread_create clients");
3240 /* Create thread to dispatch registration */
3241 ret
= pthread_create(&dispatch_thread
, NULL
,
3242 thread_dispatch_ust_registration
, (void *) NULL
);
3244 perror("pthread_create dispatch");
3248 /* Create thread to manage application registration. */
3249 ret
= pthread_create(®_apps_thread
, NULL
,
3250 thread_registration_apps
, (void *) NULL
);
3252 perror("pthread_create registration");
3256 /* Create thread to manage application socket */
3257 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
3259 perror("pthread_create apps");
3263 /* Create kernel thread to manage kernel event */
3264 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
3266 perror("pthread_create kernel");
3270 ret
= pthread_join(kernel_thread
, &status
);
3272 perror("pthread_join");
3273 goto error
; /* join error, exit without cleanup */
3277 ret
= pthread_join(apps_thread
, &status
);
3279 perror("pthread_join");
3280 goto error
; /* join error, exit without cleanup */
3284 ret
= pthread_join(reg_apps_thread
, &status
);
3286 perror("pthread_join");
3287 goto error
; /* join error, exit without cleanup */
3291 ret
= pthread_join(dispatch_thread
, &status
);
3293 perror("pthread_join");
3294 goto error
; /* join error, exit without cleanup */
3298 ret
= pthread_join(client_thread
, &status
);
3300 perror("pthread_join");
3301 goto error
; /* join error, exit without cleanup */
3304 ret
= join_kconsumerd_thread();
3306 perror("join_kconsumerd");
3307 goto error
; /* join error, exit without cleanup */
3313 * cleanup() is called when no other thread is running.