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 * Return a socket connected to the libust communication socket of the
411 * application identified by the pid.
413 * If the pid is not found in the traceable list, return -1 to indicate error.
415 static int ust_connect_app(pid_t pid
)
418 struct ltt_traceable_app
*lta
;
420 DBG("Connect to application pid %d", pid
);
422 lta
= find_app_by_pid(pid
);
425 DBG("Application pid %d not found", pid
);
429 sock
= ustctl_connect_pid(lta
->pid
);
431 ERR("Fail connecting to the PID %d", pid
);
438 * Notify apps by writing 42 to a named pipe using name. Every applications
439 * waiting for a ltt-sessiond will be notified and re-register automatically to
440 * the session daemon.
442 * Return open or write error value.
444 static int notify_apps(const char *name
)
449 DBG("Notify the global application pipe");
451 /* Try opening the global pipe */
452 fd
= open(name
, O_WRONLY
);
457 /* Notify by writing on the pipe */
458 ret
= write(fd
, "42", 2);
466 #endif /* DISABLED */
469 * Setup the outgoing data buffer for the response (llm) by allocating the
470 * right amount of memory and copying the original information from the lsm
473 * Return total size of the buffer pointed by buf.
475 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
481 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
482 if (cmd_ctx
->llm
== NULL
) {
488 /* Copy common data */
489 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
490 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
492 cmd_ctx
->llm
->data_size
= size
;
493 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
502 * Update the kernel pollfd set of all channel fd available over all tracing
503 * session. Add the wakeup pipe at the end of the set.
505 static int update_kernel_pollfd(void)
509 * The wakup pipe and the quit pipe are needed so the number of fds starts
510 * at 2 for those pipes.
512 unsigned int nb_fd
= 2;
513 struct ltt_session
*session
;
514 struct ltt_kernel_channel
*channel
;
516 DBG("Updating kernel_pollfd");
518 /* Get the number of channel of all kernel session */
520 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
521 lock_session(session
);
522 if (session
->kernel_session
== NULL
) {
523 unlock_session(session
);
526 nb_fd
+= session
->kernel_session
->channel_count
;
527 unlock_session(session
);
530 DBG("Resizing kernel_pollfd to size %d", nb_fd
);
532 kernel_pollfd
= realloc(kernel_pollfd
, nb_fd
* sizeof(struct pollfd
));
533 if (kernel_pollfd
== NULL
) {
534 perror("malloc kernel_pollfd");
538 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
539 lock_session(session
);
540 if (session
->kernel_session
== NULL
) {
541 unlock_session(session
);
545 ERR("To much channel for kernel_pollfd size");
546 unlock_session(session
);
549 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
550 kernel_pollfd
[i
].fd
= channel
->fd
;
551 kernel_pollfd
[i
].events
= POLLIN
| POLLRDNORM
;
554 unlock_session(session
);
556 unlock_session_list();
558 /* Adding wake up pipe */
559 kernel_pollfd
[nb_fd
- 2].fd
= kernel_poll_pipe
[0];
560 kernel_pollfd
[nb_fd
- 2].events
= POLLIN
;
562 /* Adding the quit pipe */
563 kernel_pollfd
[nb_fd
- 1].fd
= thread_quit_pipe
[0];
568 unlock_session_list();
573 * Find the channel fd from 'fd' over all tracing session. When found, check
574 * for new channel stream and send those stream fds to the kernel consumer.
576 * Useful for CPU hotplug feature.
578 static int update_kernel_stream(int fd
)
581 struct ltt_session
*session
;
582 struct ltt_kernel_channel
*channel
;
584 DBG("Updating kernel streams for channel fd %d", fd
);
587 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
588 lock_session(session
);
589 if (session
->kernel_session
== NULL
) {
590 unlock_session(session
);
594 /* This is not suppose to be 0 but this is an extra security check */
595 if (session
->kernel_session
->consumer_fd
== 0) {
596 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
599 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
600 if (channel
->fd
== fd
) {
601 DBG("Channel found, updating kernel streams");
602 ret
= kernel_open_channel_stream(channel
);
608 * Have we already sent fds to the consumer? If yes, it means that
609 * tracing is started so it is safe to send our updated stream fds.
611 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
612 ret
= send_kconsumerd_channel_fds(session
->kernel_session
->consumer_fd
,
621 unlock_session(session
);
625 unlock_session_list();
627 unlock_session(session
);
633 * This thread manage event coming from the kernel.
635 * Features supported in this thread:
638 static void *thread_manage_kernel(void *data
)
640 int ret
, i
, nb_fd
= 0;
642 int update_poll_flag
= 1;
644 DBG("Thread manage kernel started");
647 if (update_poll_flag
== 1) {
648 nb_fd
= update_kernel_pollfd();
652 update_poll_flag
= 0;
655 DBG("Polling on %d fds", nb_fd
);
657 /* Poll infinite value of time */
658 ret
= poll(kernel_pollfd
, nb_fd
, -1);
660 perror("poll kernel thread");
662 } else if (ret
== 0) {
663 /* Should not happen since timeout is infinite */
667 /* Thread quit pipe has been closed. Killing thread. */
668 if (kernel_pollfd
[nb_fd
- 1].revents
== POLLNVAL
) {
672 DBG("Kernel poll event triggered");
675 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
678 switch (kernel_pollfd
[nb_fd
- 2].revents
) {
680 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
681 update_poll_flag
= 1;
689 for (i
= 0; i
< nb_fd
; i
++) {
690 switch (kernel_pollfd
[i
].revents
) {
692 * New CPU detected by the kernel. Adding kernel stream to kernel
693 * session and updating the kernel consumer
695 case POLLIN
| POLLRDNORM
:
696 ret
= update_kernel_stream(kernel_pollfd
[i
].fd
);
706 DBG("Kernel thread dying");
711 close(kernel_poll_pipe
[0]);
712 close(kernel_poll_pipe
[1]);
717 * This thread manage the kconsumerd error sent back to the session daemon.
719 static void *thread_manage_kconsumerd(void *data
)
722 enum lttcomm_return_code code
;
723 struct pollfd pollfd
[2];
725 DBG("[thread] Manage kconsumerd started");
727 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
732 /* First fd is always the quit pipe */
733 pollfd
[0].fd
= thread_quit_pipe
[0];
736 pollfd
[1].fd
= kconsumerd_err_sock
;
737 pollfd
[1].events
= POLLIN
;
739 /* Inifinite blocking call, waiting for transmission */
740 ret
= poll(pollfd
, 2, -1);
742 perror("poll kconsumerd thread");
746 /* Thread quit pipe has been closed. Killing thread. */
747 if (pollfd
[0].revents
== POLLNVAL
) {
749 } else if (pollfd
[1].revents
== POLLERR
) {
750 ERR("Kconsumerd err socket poll error");
754 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
759 /* Getting status code from kconsumerd */
760 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
765 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
766 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
767 if (kconsumerd_cmd_sock
< 0) {
768 sem_post(&kconsumerd_sem
);
769 perror("kconsumerd connect");
772 /* Signal condition to tell that the kconsumerd is ready */
773 sem_post(&kconsumerd_sem
);
774 DBG("Kconsumerd command socket ready");
776 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
777 lttcomm_get_readable_code(-code
));
781 /* Kconsumerd err socket */
783 pollfd
[1].events
= POLLIN
;
785 /* Inifinite blocking call, waiting for transmission */
786 ret
= poll(pollfd
, 2, -1);
788 perror("poll kconsumerd thread");
792 /* Thread quit pipe has been closed. Killing thread. */
793 if (pollfd
[0].revents
== POLLNVAL
) {
795 } else if (pollfd
[1].revents
== POLLERR
) {
796 ERR("Kconsumerd err socket second poll error");
800 /* Wait for any kconsumerd error */
801 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
803 ERR("Kconsumerd closed the command socket");
807 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
810 DBG("Kconsumerd thread dying");
811 if (kconsumerd_err_sock
) {
812 close(kconsumerd_err_sock
);
814 if (kconsumerd_cmd_sock
) {
815 close(kconsumerd_cmd_sock
);
821 unlink(kconsumerd_err_unix_sock_path
);
822 unlink(kconsumerd_cmd_unix_sock_path
);
829 * Reallocate the apps command pollfd structure of nb_fd size.
831 * The first two fds must be there at all time.
833 static int update_apps_cmd_pollfd(unsigned int nb_fd
, struct pollfd
**pollfd
)
835 /* Can't accept pollfd less than 2 */
840 *pollfd
= realloc(*pollfd
, nb_fd
* sizeof(struct pollfd
));
841 if (*pollfd
== NULL
) {
842 perror("realloc manage apps pollfd");
846 /* First fd is always the quit pipe */
847 (*pollfd
)[0].fd
= thread_quit_pipe
[0];
848 /* Apps command pipe */
849 (*pollfd
)[1].fd
= apps_cmd_pipe
[0];
850 (*pollfd
)[1].events
= POLLIN
;
852 DBG("Apps cmd pollfd realloc of size %d", nb_fd
);
862 * Send registration done packet to the application.
864 static int send_ust_register_done(int sock
)
866 struct lttcomm_ust_msg lum
;
868 DBG("Sending register done command to %d", sock
);
870 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
871 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
873 return ustcomm_send_command(sock
, &lum
);
877 * This thread manage application communication.
879 static void *thread_manage_apps(void *data
)
882 unsigned int nb_fd
= 2;
883 int update_poll_flag
= 1;
884 struct pollfd
*pollfd
= NULL
;
885 struct ust_command ust_cmd
;
887 DBG("[thread] Manage application started");
892 /* See if we have a valid socket to add to pollfd */
893 if (ust_cmd
.sock
!= -1) {
895 update_poll_flag
= 1;
898 /* The pollfd struct must be updated */
899 if (update_poll_flag
) {
900 ret
= update_apps_cmd_pollfd(nb_fd
, &pollfd
);
902 /* malloc failed so we quit */
905 if (ust_cmd
.sock
!= -1) {
906 /* Update pollfd with the new UST socket */
907 DBG("Adding sock %d to apps cmd pollfd", ust_cmd
.sock
);
908 pollfd
[nb_fd
- 1].fd
= ust_cmd
.sock
;
909 pollfd
[nb_fd
- 1].events
= POLLHUP
| POLLNVAL
;
914 DBG("Apps thread polling on %d fds", nb_fd
);
916 /* Inifinite blocking call, waiting for transmission */
917 ret
= poll(pollfd
, nb_fd
, -1);
919 perror("poll apps thread");
923 /* Thread quit pipe has been closed. Killing thread. */
924 if (pollfd
[0].revents
== POLLNVAL
) {
926 } else if (pollfd
[1].revents
== POLLERR
) {
927 ERR("Apps command pipe poll error");
929 } else if (pollfd
[1].revents
== POLLIN
) {
931 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
932 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
933 perror("read apps cmd pipe");
937 /* Register applicaton to the session daemon */
938 ret
= register_traceable_app(&ust_cmd
.reg_msg
, ust_cmd
.sock
);
940 /* Only critical ENOMEM error can be returned here */
944 ret
= send_ust_register_done(ust_cmd
.sock
);
947 * If the registration is not possible, we simply unregister
948 * the apps and continue
950 unregister_traceable_app(ust_cmd
.sock
);
955 for (i
= 2; i
< count
; i
++) {
956 /* Apps socket is closed/hungup */
957 switch (pollfd
[i
].revents
) {
961 unregister_traceable_app(pollfd
[i
].fd
);
966 if (nb_fd
!= count
) {
967 update_poll_flag
= 1;
972 DBG("Application communication apps dying");
973 close(apps_cmd_pipe
[0]);
974 close(apps_cmd_pipe
[1]);
982 * Dispatch request from the registration threads to the application
983 * communication thread.
985 static void *thread_dispatch_ust_registration(void *data
)
988 struct cds_wfq_node
*node
;
989 struct ust_command
*ust_cmd
= NULL
;
991 DBG("[thread] Dispatch UST command started");
993 while (!dispatch_thread_exit
) {
994 /* Atomically prepare the queue futex */
995 futex_nto1_prepare(&ust_cmd_queue
.futex
);
998 /* Dequeue command for registration */
999 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1001 DBG("Waked up but nothing in the UST command queue");
1002 /* Continue thread execution */
1006 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1008 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1009 " gid:%d sock:%d name:%s (version %d.%d)",
1010 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1011 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1012 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1013 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1015 * Inform apps thread of the new application registration. This
1016 * call is blocking so we can be assured that the data will be read
1017 * at some point in time or wait to the end of the world :)
1019 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1020 sizeof(struct ust_command
));
1022 perror("write apps cmd pipe");
1023 if (errno
== EBADF
) {
1025 * We can't inform the application thread to process
1026 * registration. We will exit or else application
1027 * registration will not occur and tracing will never
1034 } while (node
!= NULL
);
1036 /* Futex wait on queue. Blocking call on futex() */
1037 futex_nto1_wait(&ust_cmd_queue
.futex
);
1041 DBG("Dispatch thread dying");
1046 * This thread manage application registration.
1048 static void *thread_registration_apps(void *data
)
1051 struct pollfd pollfd
[2];
1053 * Get allocated in this thread, enqueued to a global queue, dequeued and
1054 * freed in the manage apps thread.
1056 struct ust_command
*ust_cmd
= NULL
;
1058 DBG("[thread] Manage application registration started");
1060 ret
= lttcomm_listen_unix_sock(apps_sock
);
1065 /* First fd is always the quit pipe */
1066 pollfd
[0].fd
= thread_quit_pipe
[0];
1069 pollfd
[1].fd
= apps_sock
;
1070 pollfd
[1].events
= POLLIN
;
1072 /* Notify all applications to register */
1073 //notify_apps(default_global_apps_pipe);
1076 DBG("Accepting application registration");
1078 /* Inifinite blocking call, waiting for transmission */
1079 ret
= poll(pollfd
, 2, -1);
1081 perror("poll register apps thread");
1085 /* Thread quit pipe has been closed. Killing thread. */
1086 if (pollfd
[0].revents
== POLLNVAL
) {
1088 } else if (pollfd
[1].revents
== POLLERR
) {
1089 ERR("Register apps socket poll error");
1093 sock
= lttcomm_accept_unix_sock(apps_sock
);
1098 /* Create UST registration command for enqueuing */
1099 ust_cmd
= malloc(sizeof(struct ust_command
));
1100 if (ust_cmd
== NULL
) {
1101 perror("ust command malloc");
1106 * Using message-based transmissions to ensure we don't have to deal
1107 * with partially received messages.
1109 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1110 sizeof(struct ust_register_msg
));
1111 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1113 perror("lttcomm_recv_unix_sock register apps");
1115 ERR("Wrong size received on apps register");
1122 ust_cmd
->sock
= sock
;
1124 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1125 " gid:%d sock:%d name:%s (version %d.%d)",
1126 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1127 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1128 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1129 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1131 * Lock free enqueue the registration request.
1132 * The red pill has been taken! This apps will be part of the *system*
1134 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1137 * Wake the registration queue futex.
1138 * Implicit memory barrier with the exchange in cds_wfq_enqueue.
1140 futex_nto1_wake(&ust_cmd_queue
.futex
);
1144 DBG("Register apps thread dying");
1152 unlink(apps_unix_sock_path
);
1157 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
1158 * exec or it will fails.
1160 static int spawn_kconsumerd_thread(void)
1164 /* Setup semaphore */
1165 sem_init(&kconsumerd_sem
, 0, 0);
1167 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
1169 perror("pthread_create kconsumerd");
1173 /* Wait for the kconsumerd thread to be ready */
1174 sem_wait(&kconsumerd_sem
);
1176 if (kconsumerd_pid
== 0) {
1177 ERR("Kconsumerd did not start");
1184 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1189 * Join kernel consumer thread
1191 static int join_kconsumerd_thread(void)
1196 if (kconsumerd_pid
!= 0) {
1197 ret
= kill(kconsumerd_pid
, SIGTERM
);
1199 ERR("Error killing kconsumerd");
1202 return pthread_join(kconsumerd_thread
, &status
);
1209 * Fork and exec a kernel consumer daemon (kconsumerd).
1211 * Return pid if successful else -1.
1213 static pid_t
spawn_kconsumerd(void)
1217 const char *verbosity
;
1219 DBG("Spawning kconsumerd");
1226 if (opt_verbose
> 1 || opt_verbose_kconsumerd
) {
1227 verbosity
= "--verbose";
1229 verbosity
= "--quiet";
1231 execl(INSTALL_BIN_PATH
"/ltt-kconsumerd", "ltt-kconsumerd", verbosity
, NULL
);
1233 perror("kernel start consumer exec");
1236 } else if (pid
> 0) {
1240 perror("kernel start consumer fork");
1250 * Spawn the kconsumerd daemon and session daemon thread.
1252 static int start_kconsumerd(void)
1256 pthread_mutex_lock(&kconsumerd_pid_mutex
);
1257 if (kconsumerd_pid
!= 0) {
1258 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1262 ret
= spawn_kconsumerd();
1264 ERR("Spawning kconsumerd failed");
1265 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1266 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1270 /* Setting up the global kconsumerd_pid */
1271 kconsumerd_pid
= ret
;
1272 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1274 DBG("Kconsumerd pid %d", ret
);
1276 DBG("Spawning kconsumerd thread");
1277 ret
= spawn_kconsumerd_thread();
1279 ERR("Fatal error spawning kconsumerd thread");
1291 * modprobe_kernel_modules
1293 static int modprobe_kernel_modules(void)
1298 for (i
= 0; i
< ARRAY_SIZE(kernel_modules_list
); i
++) {
1299 ret
= snprintf(modprobe
, sizeof(modprobe
),
1300 "/sbin/modprobe %s%s",
1301 kernel_modules_list
[i
].required
? "" : "--quiet ",
1302 kernel_modules_list
[i
].name
);
1304 perror("snprintf modprobe");
1307 modprobe
[sizeof(modprobe
) - 1] = '\0';
1308 ret
= system(modprobe
);
1310 ERR("Unable to launch modprobe for module %s",
1311 kernel_modules_list
[i
].name
);
1312 } else if (kernel_modules_list
[i
].required
1313 && WEXITSTATUS(ret
) != 0) {
1314 ERR("Unable to load module %s",
1315 kernel_modules_list
[i
].name
);
1317 DBG("Modprobe successfully %s",
1318 kernel_modules_list
[i
].name
);
1327 * modprobe_remove_kernel_modules
1328 * Remove modules in reverse load order.
1330 static int modprobe_remove_kernel_modules(void)
1335 for (i
= ARRAY_SIZE(kernel_modules_list
) - 1; i
>= 0; i
--) {
1336 ret
= snprintf(modprobe
, sizeof(modprobe
),
1337 "/sbin/modprobe --remove --quiet %s",
1338 kernel_modules_list
[i
].name
);
1340 perror("snprintf modprobe --remove");
1343 modprobe
[sizeof(modprobe
) - 1] = '\0';
1344 ret
= system(modprobe
);
1346 ERR("Unable to launch modprobe --remove for module %s",
1347 kernel_modules_list
[i
].name
);
1348 } else if (kernel_modules_list
[i
].required
1349 && WEXITSTATUS(ret
) != 0) {
1350 ERR("Unable to remove module %s",
1351 kernel_modules_list
[i
].name
);
1353 DBG("Modprobe removal successful %s",
1354 kernel_modules_list
[i
].name
);
1365 static int mount_debugfs(char *path
)
1368 char *type
= "debugfs";
1370 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, geteuid(), getegid());
1375 ret
= mount(type
, path
, type
, 0, NULL
);
1377 perror("mount debugfs");
1381 DBG("Mounted debugfs successfully at %s", path
);
1388 * Setup necessary data for kernel tracer action.
1390 static void init_kernel_tracer(void)
1393 char *proc_mounts
= "/proc/mounts";
1395 char *debugfs_path
= NULL
, *lttng_path
;
1398 /* Detect debugfs */
1399 fp
= fopen(proc_mounts
, "r");
1401 ERR("Unable to probe %s", proc_mounts
);
1405 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1406 if (strstr(line
, "debugfs") != NULL
) {
1407 /* Remove first string */
1409 /* Dup string here so we can reuse line later on */
1410 debugfs_path
= strdup(strtok(NULL
, " "));
1411 DBG("Got debugfs path : %s", debugfs_path
);
1418 /* Mount debugfs if needded */
1419 if (debugfs_path
== NULL
) {
1420 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1422 perror("asprintf debugfs path");
1425 ret
= mount_debugfs(debugfs_path
);
1431 /* Modprobe lttng kernel modules */
1432 ret
= modprobe_kernel_modules();
1437 /* Setup lttng kernel path */
1438 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1440 perror("asprintf lttng path");
1444 /* Open debugfs lttng */
1445 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1446 if (kernel_tracer_fd
< 0) {
1447 DBG("Failed to open %s", lttng_path
);
1453 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1463 WARN("No kernel tracer available");
1464 kernel_tracer_fd
= 0;
1469 * Start tracing by creating trace directory and sending FDs to the kernel
1472 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1476 if (session
->kconsumer_fds_sent
== 0) {
1478 * Assign default kernel consumer if no consumer assigned to the kernel
1479 * session. At this point, it's NOT suppose to be 0 but this is an extra
1482 if (session
->consumer_fd
== 0) {
1483 session
->consumer_fd
= kconsumerd_cmd_sock
;
1486 ret
= send_kconsumerd_fds(session
);
1488 ERR("Send kconsumerd fds failed");
1489 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1493 session
->kconsumer_fds_sent
= 1;
1501 * Notify kernel thread to update it's pollfd.
1503 static int notify_kernel_pollfd(void)
1507 /* Inform kernel thread of the new kernel channel */
1508 ret
= write(kernel_poll_pipe
[1], "!", 1);
1510 perror("write kernel poll pipe");
1517 * Allocate a channel structure and fill it.
1519 static struct lttng_channel
*init_default_channel(enum lttng_domain_type domain_type
,
1522 struct lttng_channel
*chan
;
1524 chan
= malloc(sizeof(struct lttng_channel
));
1526 perror("init channel malloc");
1530 if (snprintf(chan
->name
, NAME_MAX
, "%s", name
) < 0) {
1531 perror("snprintf channel name");
1535 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1536 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1537 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1539 switch (domain_type
) {
1540 case LTTNG_DOMAIN_KERNEL
:
1541 chan
->attr
.subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
1542 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1543 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1547 goto error
; /* Not implemented */
1558 * Create a kernel tracer session then create the default channel.
1560 static int create_kernel_session(struct ltt_session
*session
)
1564 DBG("Creating kernel session");
1566 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1568 ret
= LTTCOMM_KERN_SESS_FAIL
;
1572 /* Set kernel consumer socket fd */
1573 if (kconsumerd_cmd_sock
) {
1574 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
1577 ret
= mkdir_recursive(session
->kernel_session
->trace_path
,
1578 S_IRWXU
| S_IRWXG
, geteuid(), allowed_group());
1580 if (ret
!= -EEXIST
) {
1581 ERR("Trace directory creation error");
1591 * Using the session list, filled a lttng_session array to send back to the
1592 * client for session listing.
1594 * The session list lock MUST be acquired before calling this function. Use
1595 * lock_session_list() and unlock_session_list().
1597 static void list_lttng_sessions(struct lttng_session
*sessions
)
1600 struct ltt_session
*session
;
1602 DBG("Getting all available session");
1604 * Iterate over session list and append data after the control struct in
1607 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1608 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1609 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
1610 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1611 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
1617 * Fill lttng_channel array of all channels.
1619 static void list_lttng_channels(struct ltt_session
*session
,
1620 struct lttng_channel
*channels
)
1623 struct ltt_kernel_channel
*kchan
;
1625 DBG("Listing channels for session %s", session
->name
);
1627 /* Kernel channels */
1628 if (session
->kernel_session
!= NULL
) {
1629 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
1630 /* Copy lttng_channel struct to array */
1631 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1632 channels
[i
].enabled
= kchan
->enabled
;
1637 /* TODO: Missing UST listing */
1641 * Fill lttng_event array of all events in the channel.
1643 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1644 struct lttng_event
*events
)
1647 * TODO: This is ONLY kernel. Need UST support.
1650 struct ltt_kernel_event
*event
;
1652 DBG("Listing events for channel %s", kchan
->channel
->name
);
1654 /* Kernel channels */
1655 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1656 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1657 events
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1658 events
[i
].enabled
= event
->enabled
;
1659 switch (event
->event
->instrumentation
) {
1660 case LTTNG_KERNEL_TRACEPOINT
:
1661 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1663 case LTTNG_KERNEL_KPROBE
:
1664 case LTTNG_KERNEL_KRETPROBE
:
1665 events
[i
].type
= LTTNG_EVENT_PROBE
;
1666 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
1667 sizeof(struct lttng_kernel_kprobe
));
1669 case LTTNG_KERNEL_FUNCTION
:
1670 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
1671 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
1672 sizeof(struct lttng_kernel_function
));
1680 * Process the command requested by the lttng client within the command
1681 * context structure. This function make sure that the return structure (llm)
1682 * is set and ready for transmission before returning.
1684 * Return any error encountered or 0 for success.
1686 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1688 int ret
= LTTCOMM_OK
;
1690 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1693 * Commands that DO NOT need a session.
1695 switch (cmd_ctx
->lsm
->cmd_type
) {
1696 case LTTNG_CREATE_SESSION
:
1697 case LTTNG_LIST_SESSIONS
:
1698 case LTTNG_LIST_TRACEPOINTS
:
1699 case LTTNG_CALIBRATE
:
1702 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
1703 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session
.name
);
1704 if (cmd_ctx
->session
== NULL
) {
1705 /* If session name not found */
1706 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
1707 ret
= LTTCOMM_SESS_NOT_FOUND
;
1708 } else { /* If no session name specified */
1709 ret
= LTTCOMM_SELECT_SESS
;
1713 /* Acquire lock for the session */
1714 lock_session(cmd_ctx
->session
);
1720 * Check domain type for specific "pre-action".
1722 switch (cmd_ctx
->lsm
->domain
.type
) {
1723 case LTTNG_DOMAIN_KERNEL
:
1724 /* Kernel tracer check */
1725 if (kernel_tracer_fd
== 0) {
1726 init_kernel_tracer();
1727 if (kernel_tracer_fd
== 0) {
1728 ret
= LTTCOMM_KERN_NA
;
1733 /* Need a session for kernel command */
1734 switch (cmd_ctx
->lsm
->cmd_type
) {
1735 case LTTNG_CALIBRATE
:
1736 case LTTNG_CREATE_SESSION
:
1737 case LTTNG_LIST_SESSIONS
:
1738 case LTTNG_LIST_TRACEPOINTS
:
1741 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1742 ret
= create_kernel_session(cmd_ctx
->session
);
1744 ret
= LTTCOMM_KERN_SESS_FAIL
;
1748 /* Start the kernel consumer daemon */
1750 if (kconsumerd_pid
== 0 &&
1751 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1752 ret
= start_kconsumerd();
1764 /* Process by command type */
1765 switch (cmd_ctx
->lsm
->cmd_type
) {
1766 case LTTNG_ADD_CONTEXT
:
1768 struct lttng_kernel_context kctx
;
1770 /* Setup lttng message with no payload */
1771 ret
= setup_lttng_msg(cmd_ctx
, 0);
1776 switch (cmd_ctx
->lsm
->domain
.type
) {
1777 case LTTNG_DOMAIN_KERNEL
:
1778 /* Create Kernel context */
1779 kctx
.ctx
= cmd_ctx
->lsm
->u
.context
.ctx
.ctx
;
1780 kctx
.u
.perf_counter
.type
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.type
;
1781 kctx
.u
.perf_counter
.config
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.config
;
1782 strncpy(kctx
.u
.perf_counter
.name
,
1783 cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.name
,
1784 LTTNG_SYMBOL_NAME_LEN
);
1785 kctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1787 /* Add kernel context to kernel tracer. See context.c */
1788 ret
= add_kernel_context(cmd_ctx
->session
->kernel_session
, &kctx
,
1789 cmd_ctx
->lsm
->u
.context
.event_name
,
1790 cmd_ctx
->lsm
->u
.context
.channel_name
);
1791 if (ret
!= LTTCOMM_OK
) {
1796 /* TODO: Userspace tracing */
1797 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1804 case LTTNG_DISABLE_CHANNEL
:
1806 struct ltt_kernel_channel
*kchan
;
1808 /* Setup lttng message with no payload */
1809 ret
= setup_lttng_msg(cmd_ctx
, 0);
1814 switch (cmd_ctx
->lsm
->domain
.type
) {
1815 case LTTNG_DOMAIN_KERNEL
:
1816 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1817 cmd_ctx
->session
->kernel_session
);
1818 if (kchan
== NULL
) {
1819 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1821 } else if (kchan
->enabled
== 1) {
1822 ret
= kernel_disable_channel(kchan
);
1824 if (ret
!= EEXIST
) {
1825 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1830 kernel_wait_quiescent(kernel_tracer_fd
);
1833 /* TODO: Userspace tracing */
1834 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1841 case LTTNG_DISABLE_EVENT
:
1843 struct ltt_kernel_channel
*kchan
;
1844 struct ltt_kernel_event
*kevent
;
1846 /* Setup lttng message with no payload */
1847 ret
= setup_lttng_msg(cmd_ctx
, 0);
1852 switch (cmd_ctx
->lsm
->domain
.type
) {
1853 case LTTNG_DOMAIN_KERNEL
:
1854 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1855 cmd_ctx
->session
->kernel_session
);
1856 if (kchan
== NULL
) {
1857 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1861 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, kchan
);
1862 if (kevent
!= NULL
) {
1863 DBG("Disabling kernel event %s for channel %s.", kevent
->event
->name
,
1864 kchan
->channel
->name
);
1865 ret
= kernel_disable_event(kevent
);
1867 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1872 kernel_wait_quiescent(kernel_tracer_fd
);
1875 /* TODO: Userspace tracing */
1876 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1883 case LTTNG_DISABLE_ALL_EVENT
:
1885 struct ltt_kernel_channel
*kchan
;
1886 struct ltt_kernel_event
*kevent
;
1888 /* Setup lttng message with no payload */
1889 ret
= setup_lttng_msg(cmd_ctx
, 0);
1894 switch (cmd_ctx
->lsm
->domain
.type
) {
1895 case LTTNG_DOMAIN_KERNEL
:
1896 DBG("Disabling all enabled kernel events");
1897 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1898 cmd_ctx
->session
->kernel_session
);
1899 if (kchan
== NULL
) {
1900 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1904 /* For each event in the kernel session */
1905 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1906 DBG("Disabling kernel event %s for channel %s.",
1907 kevent
->event
->name
, kchan
->channel
->name
);
1908 ret
= kernel_disable_event(kevent
);
1914 /* Quiescent wait after event disable */
1915 kernel_wait_quiescent(kernel_tracer_fd
);
1918 /* TODO: Userspace tracing */
1919 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1926 case LTTNG_ENABLE_CHANNEL
:
1928 struct ltt_kernel_channel
*kchan
;
1930 /* Setup lttng message with no payload */
1931 ret
= setup_lttng_msg(cmd_ctx
, 0);
1936 switch (cmd_ctx
->lsm
->domain
.type
) {
1937 case LTTNG_DOMAIN_KERNEL
:
1938 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1939 cmd_ctx
->session
->kernel_session
);
1940 if (kchan
== NULL
) {
1941 /* Channel not found, creating it */
1942 DBG("Creating kernel channel");
1944 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1945 &cmd_ctx
->lsm
->u
.channel
.chan
,
1946 cmd_ctx
->session
->kernel_session
->trace_path
);
1948 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1952 /* Notify kernel thread that there is a new channel */
1953 ret
= notify_kernel_pollfd();
1955 ret
= LTTCOMM_FATAL
;
1958 } else if (kchan
->enabled
== 0) {
1959 ret
= kernel_enable_channel(kchan
);
1961 if (ret
!= EEXIST
) {
1962 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1968 kernel_wait_quiescent(kernel_tracer_fd
);
1971 /* TODO: Userspace tracing */
1972 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1979 case LTTNG_ENABLE_EVENT
:
1982 struct ltt_kernel_channel
*kchan
;
1983 struct ltt_kernel_event
*kevent
;
1984 struct lttng_channel
*chan
;
1986 /* Setup lttng message with no payload */
1987 ret
= setup_lttng_msg(cmd_ctx
, 0);
1992 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
1994 switch (cmd_ctx
->lsm
->domain
.type
) {
1995 case LTTNG_DOMAIN_KERNEL
:
1996 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1997 cmd_ctx
->session
->kernel_session
);
1998 if (kchan
== NULL
) {
1999 DBG("Channel not found. Creating channel %s", channel_name
);
2001 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2003 ret
= LTTCOMM_FATAL
;
2007 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2008 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2010 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2013 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2014 cmd_ctx
->session
->kernel_session
);
2015 if (kchan
== NULL
) {
2016 ERR("Channel %s not found after creation. Internal error, giving up.",
2018 ret
= LTTCOMM_FATAL
;
2023 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, kchan
);
2024 if (kevent
== NULL
) {
2025 DBG("Creating kernel event %s for channel %s.",
2026 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
2027 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, kchan
);
2029 DBG("Enabling kernel event %s for channel %s.",
2030 kevent
->event
->name
, channel_name
);
2031 ret
= kernel_enable_event(kevent
);
2032 if (ret
== -EEXIST
) {
2033 ret
= LTTCOMM_KERN_EVENT_EXIST
;
2039 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2043 kernel_wait_quiescent(kernel_tracer_fd
);
2046 /* TODO: Userspace tracing */
2047 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2053 case LTTNG_ENABLE_ALL_EVENT
:
2057 struct ltt_kernel_channel
*kchan
;
2058 struct ltt_kernel_event
*kevent
;
2059 struct lttng_event
*event_list
;
2060 struct lttng_channel
*chan
;
2062 /* Setup lttng message with no payload */
2063 ret
= setup_lttng_msg(cmd_ctx
, 0);
2068 DBG("Enabling all kernel event");
2070 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2072 switch (cmd_ctx
->lsm
->domain
.type
) {
2073 case LTTNG_DOMAIN_KERNEL
:
2074 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2075 cmd_ctx
->session
->kernel_session
);
2076 if (kchan
== NULL
) {
2077 DBG("Channel not found. Creating channel %s", channel_name
);
2079 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2081 ret
= LTTCOMM_FATAL
;
2085 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2086 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2088 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2091 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2092 cmd_ctx
->session
->kernel_session
);
2093 if (kchan
== NULL
) {
2094 ERR("Channel %s not found after creation. Internal error, giving up.",
2096 ret
= LTTCOMM_FATAL
;
2101 /* For each event in the kernel session */
2102 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
2103 DBG("Enabling kernel event %s for channel %s.",
2104 kevent
->event
->name
, channel_name
);
2105 ret
= kernel_enable_event(kevent
);
2111 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
2113 ret
= LTTCOMM_KERN_LIST_FAIL
;
2117 for (i
= 0; i
< size
; i
++) {
2118 kevent
= trace_kernel_get_event_by_name(event_list
[i
].name
, kchan
);
2119 if (kevent
== NULL
) {
2120 /* Default event type for enable all */
2121 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2122 /* Enable each single tracepoint event */
2123 ret
= kernel_create_event(&event_list
[i
], kchan
);
2125 /* Ignore error here and continue */
2132 /* Quiescent wait after event enable */
2133 kernel_wait_quiescent(kernel_tracer_fd
);
2136 /* TODO: Userspace tracing */
2137 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2144 case LTTNG_LIST_TRACEPOINTS
:
2146 struct lttng_event
*events
;
2147 ssize_t nb_events
= 0;
2149 switch (cmd_ctx
->lsm
->domain
.type
) {
2150 case LTTNG_DOMAIN_KERNEL
:
2151 DBG("Listing kernel events");
2152 nb_events
= kernel_list_events(kernel_tracer_fd
, &events
);
2153 if (nb_events
< 0) {
2154 ret
= LTTCOMM_KERN_LIST_FAIL
;
2159 /* TODO: Userspace listing */
2160 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2165 * Setup lttng message with payload size set to the event list size in
2166 * bytes and then copy list into the llm payload.
2168 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2174 /* Copy event list into message payload */
2175 memcpy(cmd_ctx
->llm
->payload
, events
,
2176 sizeof(struct lttng_event
) * nb_events
);
2183 case LTTNG_START_TRACE
:
2185 struct ltt_kernel_channel
*chan
;
2187 /* Setup lttng message with no payload */
2188 ret
= setup_lttng_msg(cmd_ctx
, 0);
2193 /* Kernel tracing */
2194 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2195 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
2196 DBG("Open kernel metadata");
2197 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
2198 cmd_ctx
->session
->kernel_session
->trace_path
);
2200 ret
= LTTCOMM_KERN_META_FAIL
;
2205 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2206 DBG("Opening kernel metadata stream");
2207 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2208 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
2210 ERR("Kernel create metadata stream failed");
2211 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2217 /* For each channel */
2218 cds_list_for_each_entry(chan
,
2219 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2220 if (chan
->stream_count
== 0) {
2221 ret
= kernel_open_channel_stream(chan
);
2223 ERR("Kernel create channel stream failed");
2224 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2227 /* Update the stream global counter */
2228 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
2232 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
2234 ret
= LTTCOMM_KERN_START_FAIL
;
2238 DBG("Start kernel tracing");
2239 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
2241 ERR("Kernel start session failed");
2242 ret
= LTTCOMM_KERN_START_FAIL
;
2246 /* Quiescent wait after starting trace */
2247 kernel_wait_quiescent(kernel_tracer_fd
);
2250 /* TODO: Start all UST traces */
2255 case LTTNG_STOP_TRACE
:
2257 struct ltt_kernel_channel
*chan
;
2258 /* Setup lttng message with no payload */
2259 ret
= setup_lttng_msg(cmd_ctx
, 0);
2265 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2266 DBG("Stop kernel tracing");
2268 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
2270 ERR("Kernel metadata flush failed");
2273 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2274 ret
= kernel_flush_buffer(chan
);
2276 ERR("Kernel flush buffer error");
2280 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
2282 ERR("Kernel stop session failed");
2283 ret
= LTTCOMM_KERN_STOP_FAIL
;
2287 /* Quiescent wait after stopping trace */
2288 kernel_wait_quiescent(kernel_tracer_fd
);
2291 /* TODO : User-space tracer */
2296 case LTTNG_CREATE_SESSION
:
2298 /* Setup lttng message with no payload */
2299 ret
= setup_lttng_msg(cmd_ctx
, 0);
2304 ret
= create_session(cmd_ctx
->lsm
->session
.name
, cmd_ctx
->lsm
->session
.path
);
2306 if (ret
== -EEXIST
) {
2307 ret
= LTTCOMM_EXIST_SESS
;
2309 ret
= LTTCOMM_FATAL
;
2317 case LTTNG_DESTROY_SESSION
:
2319 /* Setup lttng message with no payload */
2320 ret
= setup_lttng_msg(cmd_ctx
, 0);
2325 /* Clean kernel session teardown */
2326 teardown_kernel_session(cmd_ctx
->session
);
2328 ret
= destroy_session(cmd_ctx
->lsm
->session
.name
);
2330 ret
= LTTCOMM_FATAL
;
2335 * Must notify the kernel thread here to update it's pollfd in order to
2336 * remove the channel(s)' fd just destroyed.
2338 ret
= notify_kernel_pollfd();
2340 ret
= LTTCOMM_FATAL
;
2347 case LTTNG_LIST_DOMAINS
:
2351 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2355 nb_dom
+= cmd_ctx
->session
->ust_trace_count
;
2357 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_domain
) * nb_dom
);
2362 ((struct lttng_domain
*)(cmd_ctx
->llm
->payload
))[0].type
=
2363 LTTNG_DOMAIN_KERNEL
;
2365 /* TODO: User-space tracer domain support */
2369 case LTTNG_LIST_CHANNELS
:
2372 * TODO: Only kernel channels are listed here. UST listing
2373 * is needed on lttng-ust 2.0 release.
2376 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2377 nb_chan
+= cmd_ctx
->session
->kernel_session
->channel_count
;
2380 ret
= setup_lttng_msg(cmd_ctx
,
2381 sizeof(struct lttng_channel
) * nb_chan
);
2386 list_lttng_channels(cmd_ctx
->session
,
2387 (struct lttng_channel
*)(cmd_ctx
->llm
->payload
));
2392 case LTTNG_LIST_EVENTS
:
2395 * TODO: Only kernel events are listed here. UST listing
2396 * is needed on lttng-ust 2.0 release.
2398 size_t nb_event
= 0;
2399 struct ltt_kernel_channel
*kchan
= NULL
;
2401 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2402 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.list
.channel_name
,
2403 cmd_ctx
->session
->kernel_session
);
2404 if (kchan
== NULL
) {
2405 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2408 nb_event
+= kchan
->event_count
;
2411 ret
= setup_lttng_msg(cmd_ctx
,
2412 sizeof(struct lttng_event
) * nb_event
);
2417 DBG("Listing events (%zu events)", nb_event
);
2419 list_lttng_events(kchan
,
2420 (struct lttng_event
*)(cmd_ctx
->llm
->payload
));
2425 case LTTNG_LIST_SESSIONS
:
2427 lock_session_list();
2429 if (session_list_ptr
->count
== 0) {
2430 ret
= LTTCOMM_NO_SESSION
;
2431 unlock_session_list();
2435 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
2436 session_list_ptr
->count
);
2438 unlock_session_list();
2442 /* Filled the session array */
2443 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
2445 unlock_session_list();
2450 case LTTNG_CALIBRATE
:
2452 /* Setup lttng message with no payload */
2453 ret
= setup_lttng_msg(cmd_ctx
, 0);
2458 switch (cmd_ctx
->lsm
->domain
.type
) {
2459 case LTTNG_DOMAIN_KERNEL
:
2461 struct lttng_kernel_calibrate kcalibrate
;
2463 kcalibrate
.type
= cmd_ctx
->lsm
->u
.calibrate
.type
;
2464 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2466 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2472 /* TODO: Userspace tracing */
2473 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2479 case LTTNG_REGISTER_CONSUMER
:
2483 /* Setup lttng message with no payload */
2484 ret
= setup_lttng_msg(cmd_ctx
, 0);
2489 switch (cmd_ctx
->lsm
->domain
.type
) {
2490 case LTTNG_DOMAIN_KERNEL
:
2492 /* Can't register a consumer if there is already one */
2493 if (cmd_ctx
->session
->kernel_session
->consumer_fd
!= 0) {
2494 ret
= LTTCOMM_CONNECT_FAIL
;
2498 sock
= lttcomm_connect_unix_sock(cmd_ctx
->lsm
->u
.reg
.path
);
2500 ret
= LTTCOMM_CONNECT_FAIL
;
2504 cmd_ctx
->session
->kernel_session
->consumer_fd
= sock
;
2508 /* TODO: Userspace tracing */
2509 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2518 /* Undefined command */
2519 ret
= setup_lttng_msg(cmd_ctx
, 0);
2528 /* Set return code */
2529 cmd_ctx
->llm
->ret_code
= ret
;
2531 if (cmd_ctx
->session
) {
2532 unlock_session(cmd_ctx
->session
);
2538 if (cmd_ctx
->llm
== NULL
) {
2539 DBG("Missing llm structure. Allocating one.");
2540 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2544 /* Notify client of error */
2545 cmd_ctx
->llm
->ret_code
= ret
;
2548 if (cmd_ctx
->session
) {
2549 unlock_session(cmd_ctx
->session
);
2555 * This thread manage all clients request using the unix client socket for
2558 static void *thread_manage_clients(void *data
)
2561 struct command_ctx
*cmd_ctx
= NULL
;
2562 struct pollfd pollfd
[2];
2564 DBG("[thread] Manage client started");
2566 ret
= lttcomm_listen_unix_sock(client_sock
);
2571 /* First fd is always the quit pipe */
2572 pollfd
[0].fd
= thread_quit_pipe
[0];
2575 pollfd
[1].fd
= client_sock
;
2576 pollfd
[1].events
= POLLIN
;
2578 /* Notify parent pid that we are ready
2579 * to accept command for client side.
2581 if (opt_sig_parent
) {
2582 kill(ppid
, SIGCHLD
);
2586 DBG("Accepting client command ...");
2588 /* Inifinite blocking call, waiting for transmission */
2589 ret
= poll(pollfd
, 2, -1);
2591 perror("poll client thread");
2595 /* Thread quit pipe has been closed. Killing thread. */
2596 if (pollfd
[0].revents
== POLLNVAL
) {
2598 } else if (pollfd
[1].revents
== POLLERR
) {
2599 ERR("Client socket poll error");
2603 sock
= lttcomm_accept_unix_sock(client_sock
);
2608 /* Allocate context command to process the client request */
2609 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2611 /* Allocate data buffer for reception */
2612 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2613 cmd_ctx
->llm
= NULL
;
2614 cmd_ctx
->session
= NULL
;
2617 * Data is received from the lttng client. The struct
2618 * lttcomm_session_msg (lsm) contains the command and data request of
2621 DBG("Receiving data from client ...");
2622 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2627 // TODO: Validate cmd_ctx including sanity check for security purpose.
2630 * This function dispatch the work to the kernel or userspace tracer
2631 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2632 * informations for the client. The command context struct contains
2633 * everything this function may needs.
2635 ret
= process_client_msg(cmd_ctx
);
2637 /* TODO: Inform client somehow of the fatal error. At this point,
2638 * ret < 0 means that a malloc failed (ENOMEM). */
2639 /* Error detected but still accept command */
2640 clean_command_ctx(&cmd_ctx
);
2644 DBG("Sending response (size: %d, retcode: %d)",
2645 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2646 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2648 ERR("Failed to send data back to client");
2651 clean_command_ctx(&cmd_ctx
);
2653 /* End of transmission */
2658 DBG("Client thread dying");
2666 unlink(client_unix_sock_path
);
2668 clean_command_ctx(&cmd_ctx
);
2674 * usage function on stderr
2676 static void usage(void)
2678 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2679 fprintf(stderr
, " -h, --help Display this usage.\n");
2680 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2681 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2682 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2683 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2684 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2685 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2686 fprintf(stderr
, " -V, --version Show version number.\n");
2687 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2688 fprintf(stderr
, " -q, --quiet No output at all.\n");
2689 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2690 fprintf(stderr
, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2694 * daemon argument parsing
2696 static int parse_args(int argc
, char **argv
)
2700 static struct option long_options
[] = {
2701 { "client-sock", 1, 0, 'c' },
2702 { "apps-sock", 1, 0, 'a' },
2703 { "kconsumerd-cmd-sock", 1, 0, 0 },
2704 { "kconsumerd-err-sock", 1, 0, 0 },
2705 { "daemonize", 0, 0, 'd' },
2706 { "sig-parent", 0, 0, 'S' },
2707 { "help", 0, 0, 'h' },
2708 { "group", 1, 0, 'g' },
2709 { "version", 0, 0, 'V' },
2710 { "quiet", 0, 0, 'q' },
2711 { "verbose", 0, 0, 'v' },
2712 { "verbose-kconsumerd", 0, 0, 'Z' },
2717 int option_index
= 0;
2718 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:Z", long_options
, &option_index
);
2725 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2727 fprintf(stderr
, " with arg %s\n", optarg
);
2731 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2734 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2740 opt_tracing_group
= strdup(optarg
);
2746 fprintf(stdout
, "%s\n", VERSION
);
2752 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2755 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2761 /* Verbose level can increase using multiple -v */
2765 opt_verbose_kconsumerd
+= 1;
2768 /* Unknown option or other error.
2769 * Error is printed by getopt, just return */
2778 * Creates the two needed socket by the daemon.
2779 * apps_sock - The communication socket for all UST apps.
2780 * client_sock - The communication of the cli tool (lttng).
2782 static int init_daemon_socket(void)
2787 old_umask
= umask(0);
2789 /* Create client tool unix socket */
2790 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2791 if (client_sock
< 0) {
2792 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2797 /* File permission MUST be 660 */
2798 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2800 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2805 /* Create the application unix socket */
2806 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2807 if (apps_sock
< 0) {
2808 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2813 /* File permission MUST be 666 */
2814 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2816 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2827 * Check if the global socket is available, and if a daemon is answering
2828 * at the other side. If yes, error is returned.
2830 static int check_existing_daemon(void)
2832 if (access(client_unix_sock_path
, F_OK
) < 0 &&
2833 access(apps_unix_sock_path
, F_OK
) < 0) {
2836 /* Is there anybody out there ? */
2837 if (lttng_session_daemon_alive()) {
2845 * Set the tracing group gid onto the client socket.
2847 * Race window between mkdir and chown is OK because we are going from more
2848 * permissive (root.root) to les permissive (root.tracing).
2850 static int set_permissions(void)
2855 gid
= allowed_group();
2858 WARN("No tracing group detected");
2861 ERR("Missing tracing group. Aborting execution.");
2867 /* Set lttng run dir */
2868 ret
= chown(LTTNG_RUNDIR
, 0, gid
);
2870 ERR("Unable to set group on " LTTNG_RUNDIR
);
2874 /* lttng client socket path */
2875 ret
= chown(client_unix_sock_path
, 0, gid
);
2877 ERR("Unable to set group on %s", client_unix_sock_path
);
2881 /* kconsumerd error socket path */
2882 ret
= chown(kconsumerd_err_unix_sock_path
, 0, gid
);
2884 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2888 DBG("All permissions are set");
2895 * Create the pipe used to wake up the kernel thread.
2897 static int create_kernel_poll_pipe(void)
2899 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2903 * Create the application command pipe to wake thread_manage_apps.
2905 static int create_apps_cmd_pipe(void)
2907 return pipe2(apps_cmd_pipe
, O_CLOEXEC
);
2911 * Create the lttng run directory needed for all global sockets and pipe.
2913 static int create_lttng_rundir(void)
2917 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2919 if (errno
!= EEXIST
) {
2920 ERR("Unable to create " LTTNG_RUNDIR
);
2932 * Setup sockets and directory needed by the kconsumerd communication with the
2935 static int set_kconsumerd_sockets(void)
2939 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
2940 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
2943 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
2944 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
2947 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
2949 if (errno
!= EEXIST
) {
2950 ERR("Failed to create " KCONSUMERD_PATH
);
2956 /* Create the kconsumerd error unix socket */
2957 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
2958 if (kconsumerd_err_sock
< 0) {
2959 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
2964 /* File permission MUST be 660 */
2965 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2967 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
2977 * Signal handler for the daemon
2979 * Simply stop all worker threads, leaving main() return gracefully
2980 * after joining all threads and calling cleanup().
2982 static void sighandler(int sig
)
2986 DBG("SIGPIPE catched");
2989 DBG("SIGINT catched");
2993 DBG("SIGTERM catched");
3002 * Setup signal handler for :
3003 * SIGINT, SIGTERM, SIGPIPE
3005 static int set_signal_handler(void)
3008 struct sigaction sa
;
3011 if ((ret
= sigemptyset(&sigset
)) < 0) {
3012 perror("sigemptyset");
3016 sa
.sa_handler
= sighandler
;
3017 sa
.sa_mask
= sigset
;
3019 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3020 perror("sigaction");
3024 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3025 perror("sigaction");
3029 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3030 perror("sigaction");
3034 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3040 * Set open files limit to unlimited. This daemon can open a large number of
3041 * file descriptors in order to consumer multiple kernel traces.
3043 static void set_ulimit(void)
3048 /* The kernel does not allowed an infinite limit for open files */
3049 lim
.rlim_cur
= 65535;
3050 lim
.rlim_max
= 65535;
3052 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3054 perror("failed to set open files limit");
3061 int main(int argc
, char **argv
)
3065 const char *home_path
;
3067 /* Create thread quit pipe */
3068 if ((ret
= init_thread_quit_pipe()) < 0) {
3072 /* Parse arguments */
3074 if ((ret
= parse_args(argc
, argv
) < 0)) {
3087 /* Check if daemon is UID = 0 */
3088 is_root
= !getuid();
3091 ret
= create_lttng_rundir();
3096 if (strlen(apps_unix_sock_path
) == 0) {
3097 snprintf(apps_unix_sock_path
, PATH_MAX
,
3098 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
3101 if (strlen(client_unix_sock_path
) == 0) {
3102 snprintf(client_unix_sock_path
, PATH_MAX
,
3103 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
3106 home_path
= get_home_dir();
3107 if (home_path
== NULL
) {
3108 /* TODO: Add --socket PATH option */
3109 ERR("Can't get HOME directory for sockets creation.");
3114 if (strlen(apps_unix_sock_path
) == 0) {
3115 snprintf(apps_unix_sock_path
, PATH_MAX
,
3116 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
3119 /* Set the cli tool unix socket path */
3120 if (strlen(client_unix_sock_path
) == 0) {
3121 snprintf(client_unix_sock_path
, PATH_MAX
,
3122 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
3126 DBG("Client socket path %s", client_unix_sock_path
);
3127 DBG("Application socket path %s", apps_unix_sock_path
);
3130 * See if daemon already exist.
3132 if ((ret
= check_existing_daemon()) < 0) {
3133 ERR("Already running daemon.\n");
3135 * We do not goto exit because we must not cleanup()
3136 * because a daemon is already running.
3141 /* After this point, we can safely call cleanup() so goto error is used */
3144 * These actions must be executed as root. We do that *after* setting up
3145 * the sockets path because we MUST make the check for another daemon using
3146 * those paths *before* trying to set the kernel consumer sockets and init
3150 ret
= set_kconsumerd_sockets();
3155 /* Setup kernel tracer */
3156 init_kernel_tracer();
3158 /* Set ulimit for open files */
3162 if ((ret
= set_signal_handler()) < 0) {
3166 /* Setup the needed unix socket */
3167 if ((ret
= init_daemon_socket()) < 0) {
3171 /* Set credentials to socket */
3172 if (is_root
&& ((ret
= set_permissions()) < 0)) {
3176 /* Get parent pid if -S, --sig-parent is specified. */
3177 if (opt_sig_parent
) {
3181 /* Setup the kernel pipe for waking up the kernel thread */
3182 if ((ret
= create_kernel_poll_pipe()) < 0) {
3186 /* Setup the thread apps communication pipe. */
3187 if ((ret
= create_apps_cmd_pipe()) < 0) {
3191 /* Init UST command queue. */
3192 cds_wfq_init(&ust_cmd_queue
.queue
);
3195 * Get session list pointer. This pointer MUST NOT be free().
3196 * This list is statically declared in session.c
3198 session_list_ptr
= get_session_list();
3200 /* Create thread to manage the client socket */
3201 ret
= pthread_create(&client_thread
, NULL
,
3202 thread_manage_clients
, (void *) NULL
);
3204 perror("pthread_create clients");
3208 /* Create thread to dispatch registration */
3209 ret
= pthread_create(&dispatch_thread
, NULL
,
3210 thread_dispatch_ust_registration
, (void *) NULL
);
3212 perror("pthread_create dispatch");
3216 /* Create thread to manage application registration. */
3217 ret
= pthread_create(®_apps_thread
, NULL
,
3218 thread_registration_apps
, (void *) NULL
);
3220 perror("pthread_create registration");
3224 /* Create thread to manage application socket */
3225 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
3227 perror("pthread_create apps");
3231 /* Create kernel thread to manage kernel event */
3232 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
3234 perror("pthread_create kernel");
3238 ret
= pthread_join(kernel_thread
, &status
);
3240 perror("pthread_join");
3241 goto error
; /* join error, exit without cleanup */
3245 ret
= pthread_join(apps_thread
, &status
);
3247 perror("pthread_join");
3248 goto error
; /* join error, exit without cleanup */
3252 ret
= pthread_join(reg_apps_thread
, &status
);
3254 perror("pthread_join");
3255 goto error
; /* join error, exit without cleanup */
3259 ret
= pthread_join(dispatch_thread
, &status
);
3261 perror("pthread_join");
3262 goto error
; /* join error, exit without cleanup */
3266 ret
= pthread_join(client_thread
, &status
);
3268 perror("pthread_join");
3269 goto error
; /* join error, exit without cleanup */
3272 ret
= join_kconsumerd_thread();
3274 perror("join_kconsumerd");
3275 goto error
; /* join error, exit without cleanup */
3281 * cleanup() is called when no other thread is running.