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"
53 #include "benchmark.h"
56 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
57 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
58 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
59 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
62 int opt_verbose
; /* Not static for lttngerr.h */
63 int opt_verbose_kconsumerd
; /* Not static for lttngerr.h */
64 int opt_quiet
; /* Not static for lttngerr.h */
67 const char *opt_tracing_group
;
68 static int opt_sig_parent
;
69 static int opt_daemon
;
70 static int is_root
; /* Set to 1 if the daemon is running as root */
71 static pid_t ppid
; /* Parent PID for --sig-parent option */
72 static pid_t kconsumerd_pid
;
73 static struct pollfd
*kernel_pollfd
;
74 static int dispatch_thread_exit
;
76 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
77 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
78 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
79 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
82 static int client_sock
;
84 static int kconsumerd_err_sock
;
85 static int kconsumerd_cmd_sock
;
86 static int kernel_tracer_fd
;
87 static int kernel_poll_pipe
[2];
90 * Quit pipe for all threads. This permits a single cancellation point
91 * for all threads when receiving an event on the pipe.
93 static int thread_quit_pipe
[2];
96 * This pipe is used to inform the thread managing application communication
97 * that a command is queued and ready to be processed.
99 static int apps_cmd_pipe
[2];
101 /* Pthread, Mutexes and Semaphores */
102 static pthread_t kconsumerd_thread
;
103 static pthread_t apps_thread
;
104 static pthread_t reg_apps_thread
;
105 static pthread_t client_thread
;
106 static pthread_t kernel_thread
;
107 static pthread_t dispatch_thread
;
108 static sem_t kconsumerd_sem
;
110 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
112 static int modprobe_remove_kernel_modules(void);
115 * UST registration command queue. This queue is tied with a futex and uses a N
116 * wakers / 1 waiter implemented and detailed in futex.c/.h
118 * The thread_manage_apps and thread_dispatch_ust_registration interact with
119 * this queue and the wait/wake scheme.
121 static struct ust_cmd_queue ust_cmd_queue
;
124 * Pointer initialized before thread creation.
126 * This points to the tracing session list containing the session count and a
127 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
128 * MUST NOT be taken if you call a public function in session.c.
130 * The lock is nested inside the structure: session_list_ptr->lock. Please use
131 * lock_session_list and unlock_session_list for lock acquisition.
133 static struct ltt_session_list
*session_list_ptr
;
135 static gid_t
allowed_group(void)
139 if (opt_tracing_group
) {
140 grp
= getgrnam(opt_tracing_group
);
142 grp
= getgrnam(default_tracing_group
);
154 * Return -1 on error or 0 if all pipes are created.
156 static int init_thread_quit_pipe(void)
160 ret
= pipe2(thread_quit_pipe
, O_CLOEXEC
);
162 perror("thread quit pipe");
171 * Complete teardown of a kernel session. This free all data structure related
172 * to a kernel session and update counter.
174 static void teardown_kernel_session(struct ltt_session
*session
)
176 if (session
->kernel_session
!= NULL
) {
177 DBG("Tearing down kernel session");
180 * If a custom kernel consumer was registered, close the socket before
181 * tearing down the complete kernel session structure
183 if (session
->kernel_session
->consumer_fd
!= kconsumerd_cmd_sock
) {
184 lttcomm_close_unix_sock(session
->kernel_session
->consumer_fd
);
187 trace_destroy_kernel_session(session
->kernel_session
);
188 /* Extra precaution */
189 session
->kernel_session
= NULL
;
194 * Stop all threads by closing the thread quit pipe.
196 static void stop_threads(void)
198 /* Stopping all threads */
199 DBG("Terminating all threads");
200 close(thread_quit_pipe
[0]);
201 close(thread_quit_pipe
[1]);
202 /* Dispatch thread */
203 dispatch_thread_exit
= 1;
204 futex_nto1_wake(&ust_cmd_queue
.futex
);
210 static void cleanup(void)
214 struct ltt_session
*sess
, *stmp
;
219 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
220 "Matthew, BEET driven development works!%c[%dm",
221 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
224 DBG("Removing %s directory", LTTNG_RUNDIR
);
225 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
227 ERR("asprintf failed. Something is really wrong!");
230 /* Remove lttng run directory */
233 ERR("Unable to clean " LTTNG_RUNDIR
);
236 DBG("Cleaning up all session");
238 /* Destroy session list mutex */
239 if (session_list_ptr
!= NULL
) {
240 pthread_mutex_destroy(&session_list_ptr
->lock
);
242 /* Cleanup ALL session */
243 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
244 teardown_kernel_session(sess
);
245 // TODO complete session cleanup (including UST)
249 DBG("Closing all UST sockets");
250 clean_traceable_apps_list();
252 pthread_mutex_destroy(&kconsumerd_pid_mutex
);
254 DBG("Closing kernel fd");
255 close(kernel_tracer_fd
);
257 DBG("Unloading kernel modules");
258 modprobe_remove_kernel_modules();
260 /* OUTPUT BENCHMARK RESULTS */
263 bench_print_boot_process();
270 * Send data on a unix socket using the liblttsessiondcomm API.
272 * Return lttcomm error code.
274 static int send_unix_sock(int sock
, void *buf
, size_t len
)
276 /* Check valid length */
281 return lttcomm_send_unix_sock(sock
, buf
, len
);
285 * Free memory of a command context structure.
287 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
289 DBG("Clean command context structure");
291 if ((*cmd_ctx
)->llm
) {
292 free((*cmd_ctx
)->llm
);
294 if ((*cmd_ctx
)->lsm
) {
295 free((*cmd_ctx
)->lsm
);
303 * Send all stream fds of kernel channel to the consumer.
305 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
309 struct ltt_kernel_stream
*stream
;
310 struct lttcomm_kconsumerd_header lkh
;
311 struct lttcomm_kconsumerd_msg lkm
;
313 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
315 nb_fd
= channel
->stream_count
;
318 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
319 lkh
.cmd_type
= ADD_STREAM
;
321 DBG("Sending kconsumerd header");
323 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
325 perror("send kconsumerd header");
329 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
330 if (stream
->fd
!= 0) {
332 lkm
.state
= stream
->state
;
333 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
334 lkm
.output
= channel
->channel
->attr
.output
;
335 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
336 lkm
.path_name
[PATH_MAX
- 1] = '\0';
338 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
340 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
342 perror("send kconsumerd fd");
348 DBG("Kconsumerd channel fds sent");
357 * Send all stream fds of the kernel session to the consumer.
359 static int send_kconsumerd_fds(struct ltt_kernel_session
*session
)
362 struct ltt_kernel_channel
*chan
;
363 struct lttcomm_kconsumerd_header lkh
;
364 struct lttcomm_kconsumerd_msg lkm
;
367 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
368 lkh
.cmd_type
= ADD_STREAM
;
370 DBG("Sending kconsumerd header for metadata");
372 ret
= lttcomm_send_unix_sock(session
->consumer_fd
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
374 perror("send kconsumerd header");
378 DBG("Sending metadata stream fd");
380 /* Extra protection. It's NOT suppose to be set to 0 at this point */
381 if (session
->consumer_fd
== 0) {
382 session
->consumer_fd
= kconsumerd_cmd_sock
;
385 if (session
->metadata_stream_fd
!= 0) {
386 /* Send metadata stream fd first */
387 lkm
.fd
= session
->metadata_stream_fd
;
388 lkm
.state
= ACTIVE_FD
;
389 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
390 lkm
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
391 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
392 lkm
.path_name
[PATH_MAX
- 1] = '\0';
394 ret
= lttcomm_send_fds_unix_sock(session
->consumer_fd
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
396 perror("send kconsumerd fd");
401 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
402 ret
= send_kconsumerd_channel_fds(session
->consumer_fd
, chan
);
408 DBG("Kconsumerd fds (metadata and channel streams) sent");
418 * Return a socket connected to the libust communication socket of the
419 * application identified by the pid.
421 * If the pid is not found in the traceable list, return -1 to indicate error.
423 static int ust_connect_app(pid_t pid
)
426 struct ltt_traceable_app
*lta
;
428 DBG("Connect to application pid %d", pid
);
430 lta
= find_app_by_pid(pid
);
433 DBG("Application pid %d not found", pid
);
437 sock
= ustctl_connect_pid(lta
->pid
);
439 ERR("Fail connecting to the PID %d", pid
);
446 * Notify apps by writing 42 to a named pipe using name. Every applications
447 * waiting for a ltt-sessiond will be notified and re-register automatically to
448 * the session daemon.
450 * Return open or write error value.
452 static int notify_apps(const char *name
)
457 DBG("Notify the global application pipe");
459 /* Try opening the global pipe */
460 fd
= open(name
, O_WRONLY
);
465 /* Notify by writing on the pipe */
466 ret
= write(fd
, "42", 2);
474 #endif /* DISABLED */
477 * Setup the outgoing data buffer for the response (llm) by allocating the
478 * right amount of memory and copying the original information from the lsm
481 * Return total size of the buffer pointed by buf.
483 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
489 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
490 if (cmd_ctx
->llm
== NULL
) {
496 /* Copy common data */
497 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
498 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
500 cmd_ctx
->llm
->data_size
= size
;
501 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
510 * Update the kernel pollfd set of all channel fd available over all tracing
511 * session. Add the wakeup pipe at the end of the set.
513 static int update_kernel_pollfd(void)
517 * The wakup pipe and the quit pipe are needed so the number of fds starts
518 * at 2 for those pipes.
520 unsigned int nb_fd
= 2;
521 struct ltt_session
*session
;
522 struct ltt_kernel_channel
*channel
;
524 DBG("Updating kernel_pollfd");
526 /* Get the number of channel of all kernel session */
528 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
529 lock_session(session
);
530 if (session
->kernel_session
== NULL
) {
531 unlock_session(session
);
534 nb_fd
+= session
->kernel_session
->channel_count
;
535 unlock_session(session
);
538 DBG("Resizing kernel_pollfd to size %d", nb_fd
);
540 kernel_pollfd
= realloc(kernel_pollfd
, nb_fd
* sizeof(struct pollfd
));
541 if (kernel_pollfd
== NULL
) {
542 perror("malloc kernel_pollfd");
546 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
547 lock_session(session
);
548 if (session
->kernel_session
== NULL
) {
549 unlock_session(session
);
553 ERR("To much channel for kernel_pollfd size");
554 unlock_session(session
);
557 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
558 kernel_pollfd
[i
].fd
= channel
->fd
;
559 kernel_pollfd
[i
].events
= POLLIN
| POLLRDNORM
;
562 unlock_session(session
);
564 unlock_session_list();
566 /* Adding wake up pipe */
567 kernel_pollfd
[nb_fd
- 2].fd
= kernel_poll_pipe
[0];
568 kernel_pollfd
[nb_fd
- 2].events
= POLLIN
;
570 /* Adding the quit pipe */
571 kernel_pollfd
[nb_fd
- 1].fd
= thread_quit_pipe
[0];
576 unlock_session_list();
581 * Find the channel fd from 'fd' over all tracing session. When found, check
582 * for new channel stream and send those stream fds to the kernel consumer.
584 * Useful for CPU hotplug feature.
586 static int update_kernel_stream(int fd
)
589 struct ltt_session
*session
;
590 struct ltt_kernel_channel
*channel
;
592 DBG("Updating kernel streams for channel fd %d", fd
);
595 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
596 lock_session(session
);
597 if (session
->kernel_session
== NULL
) {
598 unlock_session(session
);
602 /* This is not suppose to be 0 but this is an extra security check */
603 if (session
->kernel_session
->consumer_fd
== 0) {
604 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
607 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
608 if (channel
->fd
== fd
) {
609 DBG("Channel found, updating kernel streams");
610 ret
= kernel_open_channel_stream(channel
);
616 * Have we already sent fds to the consumer? If yes, it means that
617 * tracing is started so it is safe to send our updated stream fds.
619 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
620 ret
= send_kconsumerd_channel_fds(session
->kernel_session
->consumer_fd
,
629 unlock_session(session
);
633 unlock_session_list();
635 unlock_session(session
);
641 * This thread manage event coming from the kernel.
643 * Features supported in this thread:
646 static void *thread_manage_kernel(void *data
)
648 int ret
, i
, nb_fd
= 0;
650 int update_poll_flag
= 1;
652 tracepoint(sessiond_th_kern_start
);
654 DBG("Thread manage kernel started");
657 if (update_poll_flag
== 1) {
658 nb_fd
= update_kernel_pollfd();
662 update_poll_flag
= 0;
665 DBG("Polling on %d fds", nb_fd
);
667 tracepoint(sessiond_th_kern_poll
);
669 /* Poll infinite value of time */
670 ret
= poll(kernel_pollfd
, nb_fd
, -1);
672 perror("poll kernel thread");
674 } else if (ret
== 0) {
675 /* Should not happen since timeout is infinite */
679 /* Thread quit pipe has been closed. Killing thread. */
680 if (kernel_pollfd
[nb_fd
- 1].revents
== POLLNVAL
) {
684 DBG("Kernel poll event triggered");
687 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
690 switch (kernel_pollfd
[nb_fd
- 2].revents
) {
692 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
693 update_poll_flag
= 1;
701 for (i
= 0; i
< nb_fd
; i
++) {
702 switch (kernel_pollfd
[i
].revents
) {
704 * New CPU detected by the kernel. Adding kernel stream to kernel
705 * session and updating the kernel consumer
707 case POLLIN
| POLLRDNORM
:
708 ret
= update_kernel_stream(kernel_pollfd
[i
].fd
);
718 DBG("Kernel thread dying");
723 close(kernel_poll_pipe
[0]);
724 close(kernel_poll_pipe
[1]);
729 * This thread manage the kconsumerd error sent back to the session daemon.
731 static void *thread_manage_kconsumerd(void *data
)
734 enum lttcomm_return_code code
;
735 struct pollfd pollfd
[2];
737 tracepoint(sessiond_th_kcon_start
);
739 DBG("[thread] Manage kconsumerd started");
741 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
746 /* First fd is always the quit pipe */
747 pollfd
[0].fd
= thread_quit_pipe
[0];
750 pollfd
[1].fd
= kconsumerd_err_sock
;
751 pollfd
[1].events
= POLLIN
;
753 tracepoint(sessiond_th_kcon_poll
);
755 /* Inifinite blocking call, waiting for transmission */
756 ret
= poll(pollfd
, 2, -1);
758 perror("poll kconsumerd thread");
762 /* Thread quit pipe has been closed. Killing thread. */
763 if (pollfd
[0].revents
== POLLNVAL
) {
765 } else if (pollfd
[1].revents
== POLLERR
) {
766 ERR("Kconsumerd err socket poll error");
770 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
775 /* Getting status code from kconsumerd */
776 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
781 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
782 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
783 if (kconsumerd_cmd_sock
< 0) {
784 sem_post(&kconsumerd_sem
);
785 perror("kconsumerd connect");
788 /* Signal condition to tell that the kconsumerd is ready */
789 sem_post(&kconsumerd_sem
);
790 DBG("Kconsumerd command socket ready");
792 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
793 lttcomm_get_readable_code(-code
));
797 /* Kconsumerd err socket */
799 pollfd
[1].events
= POLLIN
;
801 /* Inifinite blocking call, waiting for transmission */
802 ret
= poll(pollfd
, 2, -1);
804 perror("poll kconsumerd thread");
808 /* Thread quit pipe has been closed. Killing thread. */
809 if (pollfd
[0].revents
== POLLNVAL
) {
811 } else if (pollfd
[1].revents
== POLLERR
) {
812 ERR("Kconsumerd err socket second poll error");
816 /* Wait for any kconsumerd error */
817 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
819 ERR("Kconsumerd closed the command socket");
823 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
826 DBG("Kconsumerd thread dying");
827 if (kconsumerd_err_sock
) {
828 close(kconsumerd_err_sock
);
830 if (kconsumerd_cmd_sock
) {
831 close(kconsumerd_cmd_sock
);
837 unlink(kconsumerd_err_unix_sock_path
);
838 unlink(kconsumerd_cmd_unix_sock_path
);
845 * Reallocate the apps command pollfd structure of nb_fd size.
847 * The first two fds must be there at all time.
849 static int update_apps_cmd_pollfd(unsigned int nb_fd
, struct pollfd
**pollfd
)
851 /* Can't accept pollfd less than 2 */
856 *pollfd
= realloc(*pollfd
, nb_fd
* sizeof(struct pollfd
));
857 if (*pollfd
== NULL
) {
858 perror("realloc manage apps pollfd");
862 /* First fd is always the quit pipe */
863 (*pollfd
)[0].fd
= thread_quit_pipe
[0];
864 /* Apps command pipe */
865 (*pollfd
)[1].fd
= apps_cmd_pipe
[0];
866 (*pollfd
)[1].events
= POLLIN
;
868 DBG("Apps cmd pollfd realloc of size %d", nb_fd
);
878 * Send registration done packet to the application.
880 static int send_ust_register_done(int sock
)
882 struct lttcomm_ust_msg lum
;
884 DBG("Sending register done command to %d", sock
);
886 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
887 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
889 return ustcomm_send_command(sock
, &lum
);
893 * This thread manage application communication.
895 static void *thread_manage_apps(void *data
)
898 unsigned int nb_fd
= 2;
899 int update_poll_flag
= 1;
900 struct pollfd
*pollfd
= NULL
;
901 struct ust_command ust_cmd
;
903 DBG("[thread] Manage application started");
908 /* See if we have a valid socket to add to pollfd */
909 if (ust_cmd
.sock
!= -1) {
911 update_poll_flag
= 1;
914 /* The pollfd struct must be updated */
915 if (update_poll_flag
) {
916 ret
= update_apps_cmd_pollfd(nb_fd
, &pollfd
);
918 /* malloc failed so we quit */
921 if (ust_cmd
.sock
!= -1) {
922 /* Update pollfd with the new UST socket */
923 DBG("Adding sock %d to apps cmd pollfd", ust_cmd
.sock
);
924 pollfd
[nb_fd
- 1].fd
= ust_cmd
.sock
;
925 pollfd
[nb_fd
- 1].events
= POLLHUP
| POLLNVAL
;
930 DBG("Apps thread polling on %d fds", nb_fd
);
932 /* Inifinite blocking call, waiting for transmission */
933 ret
= poll(pollfd
, nb_fd
, -1);
935 perror("poll apps thread");
939 /* Thread quit pipe has been closed. Killing thread. */
940 if (pollfd
[0].revents
== POLLNVAL
) {
942 } else if (pollfd
[1].revents
== POLLERR
) {
943 ERR("Apps command pipe poll error");
945 } else if (pollfd
[1].revents
== POLLIN
) {
947 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
948 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
949 perror("read apps cmd pipe");
953 /* Register applicaton to the session daemon */
954 ret
= register_traceable_app(&ust_cmd
.reg_msg
, ust_cmd
.sock
);
956 /* Only critical ENOMEM error can be returned here */
960 ret
= send_ust_register_done(ust_cmd
.sock
);
963 * If the registration is not possible, we simply unregister
964 * the apps and continue
966 unregister_traceable_app(ust_cmd
.sock
);
971 for (i
= 2; i
< count
; i
++) {
972 /* Apps socket is closed/hungup */
973 switch (pollfd
[i
].revents
) {
977 unregister_traceable_app(pollfd
[i
].fd
);
982 if (nb_fd
!= count
) {
983 update_poll_flag
= 1;
988 DBG("Application communication apps dying");
989 close(apps_cmd_pipe
[0]);
990 close(apps_cmd_pipe
[1]);
998 * Dispatch request from the registration threads to the application
999 * communication thread.
1001 static void *thread_dispatch_ust_registration(void *data
)
1004 struct cds_wfq_node
*node
;
1005 struct ust_command
*ust_cmd
= NULL
;
1007 DBG("[thread] Dispatch UST command started");
1009 while (!dispatch_thread_exit
) {
1010 /* Atomically prepare the queue futex */
1011 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1014 /* Dequeue command for registration */
1015 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1017 DBG("Waked up but nothing in the UST command queue");
1018 /* Continue thread execution */
1022 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1024 DBG("Dispatching UST registration pid:%d sock:%d",
1025 ust_cmd
->reg_msg
.pid
, ust_cmd
->sock
);
1027 * Inform apps thread of the new application registration. This
1028 * call is blocking so we can be assured that the data will be read
1029 * at some point in time or wait to the end of the world :)
1031 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1032 sizeof(struct ust_command
));
1034 perror("write apps cmd pipe");
1035 if (errno
== EBADF
) {
1037 * We can't inform the application thread to process
1038 * registration. We will exit or else application
1039 * registration will not occur and tracing will never
1046 } while (node
!= NULL
);
1048 /* Futex wait on queue. Blocking call on futex() */
1049 futex_nto1_wait(&ust_cmd_queue
.futex
);
1053 DBG("Dispatch thread dying");
1058 * This thread manage application registration.
1060 static void *thread_registration_apps(void *data
)
1063 struct pollfd pollfd
[2];
1065 * Get allocated in this thread, enqueued to a global queue, dequeued and
1066 * freed in the manage apps thread.
1068 struct ust_command
*ust_cmd
= NULL
;
1070 tracepoint(sessiond_th_apps_start
);
1072 /* TODO: Something more elegant is needed but fine for now */
1073 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
1074 * for 32-bit vs 64-bit compat processes. */
1075 /* replicate in ust with version number */
1077 int reg
; /* 1:register, 0:unregister */
1082 DBG("[thread] Manage application registration started");
1084 ret
= lttcomm_listen_unix_sock(apps_sock
);
1089 /* First fd is always the quit pipe */
1090 pollfd
[0].fd
= thread_quit_pipe
[0];
1093 pollfd
[1].fd
= apps_sock
;
1094 pollfd
[1].events
= POLLIN
;
1096 /* Notify all applications to register */
1097 //notify_apps(default_global_apps_pipe);
1100 DBG("Accepting application registration");
1102 tracepoint(sessiond_th_apps_poll
);
1104 /* Inifinite blocking call, waiting for transmission */
1105 ret
= poll(pollfd
, 2, -1);
1107 perror("poll register apps thread");
1111 /* Thread quit pipe has been closed. Killing thread. */
1112 if (pollfd
[0].revents
== POLLNVAL
) {
1114 } else if (pollfd
[1].revents
== POLLERR
) {
1115 ERR("Register apps socket poll error");
1119 sock
= lttcomm_accept_unix_sock(apps_sock
);
1124 /* Create UST registration command for enqueuing */
1125 ust_cmd
= malloc(sizeof(struct ust_command
));
1126 if (ust_cmd
== NULL
) {
1127 perror("ust command malloc");
1132 * Using message-based transmissions to ensure we don't have to deal
1133 * with partially received messages.
1135 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1136 sizeof(struct ust_register_msg
));
1137 if (ret
< 0 || ret
!= sizeof(struct ust_register_msg
)) {
1138 perror("lttcomm_recv_unix_sock register apps");
1144 ust_cmd
->sock
= sock
;
1147 * Lock free enqueue the registration request.
1148 * The red pill has been taken! This apps will be part of the *system*
1150 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1153 * Wake the registration queue futex.
1154 * Implicit memory barrier with the exchange in cds_wfq_enqueue.
1156 futex_nto1_wake(&ust_cmd_queue
.futex
);
1158 DBG("Thread manage apps informed of queued node with sock:%d pid:%d",
1159 sock
, ust_cmd
->reg_msg
.pid
);
1163 DBG("Register apps thread dying");
1171 unlink(apps_unix_sock_path
);
1176 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
1177 * exec or it will fails.
1179 static int spawn_kconsumerd_thread(void)
1183 /* Setup semaphore */
1184 sem_init(&kconsumerd_sem
, 0, 0);
1186 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
1188 perror("pthread_create kconsumerd");
1192 /* Wait for the kconsumerd thread to be ready */
1193 sem_wait(&kconsumerd_sem
);
1195 if (kconsumerd_pid
== 0) {
1196 ERR("Kconsumerd did not start");
1203 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1208 * Join kernel consumer thread
1210 static int join_kconsumerd_thread(void)
1215 if (kconsumerd_pid
!= 0) {
1216 ret
= kill(kconsumerd_pid
, SIGTERM
);
1218 ERR("Error killing kconsumerd");
1221 return pthread_join(kconsumerd_thread
, &status
);
1228 * Fork and exec a kernel consumer daemon (kconsumerd).
1230 * Return pid if successful else -1.
1232 static pid_t
spawn_kconsumerd(void)
1236 const char *verbosity
;
1238 DBG("Spawning kconsumerd");
1245 if (opt_verbose
> 1 || opt_verbose_kconsumerd
) {
1246 verbosity
= "--verbose";
1248 verbosity
= "--quiet";
1250 execl(INSTALL_BIN_PATH
"/ltt-kconsumerd", "ltt-kconsumerd", verbosity
, NULL
);
1252 perror("kernel start consumer exec");
1255 } else if (pid
> 0) {
1259 perror("kernel start consumer fork");
1269 * Spawn the kconsumerd daemon and session daemon thread.
1271 static int start_kconsumerd(void)
1275 pthread_mutex_lock(&kconsumerd_pid_mutex
);
1276 if (kconsumerd_pid
!= 0) {
1277 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1281 ret
= spawn_kconsumerd();
1283 ERR("Spawning kconsumerd failed");
1284 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1285 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1289 /* Setting up the global kconsumerd_pid */
1290 kconsumerd_pid
= ret
;
1291 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1293 DBG("Kconsumerd pid %d", ret
);
1295 DBG("Spawning kconsumerd thread");
1296 ret
= spawn_kconsumerd_thread();
1298 ERR("Fatal error spawning kconsumerd thread");
1310 * modprobe_kernel_modules
1312 static int modprobe_kernel_modules(void)
1317 for (i
= 0; i
< ARRAY_SIZE(kernel_modules_list
); i
++) {
1318 ret
= snprintf(modprobe
, sizeof(modprobe
),
1319 "/sbin/modprobe %s%s",
1320 kernel_modules_list
[i
].required
? "" : "--quiet ",
1321 kernel_modules_list
[i
].name
);
1323 perror("snprintf modprobe");
1326 modprobe
[sizeof(modprobe
) - 1] = '\0';
1327 ret
= system(modprobe
);
1329 ERR("Unable to launch modprobe for module %s",
1330 kernel_modules_list
[i
].name
);
1331 } else if (kernel_modules_list
[i
].required
1332 && WEXITSTATUS(ret
) != 0) {
1333 ERR("Unable to load module %s",
1334 kernel_modules_list
[i
].name
);
1336 DBG("Modprobe successfully %s",
1337 kernel_modules_list
[i
].name
);
1346 * modprobe_remove_kernel_modules
1347 * Remove modules in reverse load order.
1349 static int modprobe_remove_kernel_modules(void)
1354 for (i
= ARRAY_SIZE(kernel_modules_list
) - 1; i
>= 0; i
--) {
1355 ret
= snprintf(modprobe
, sizeof(modprobe
),
1356 "/sbin/modprobe --remove --quiet %s",
1357 kernel_modules_list
[i
].name
);
1359 perror("snprintf modprobe --remove");
1362 modprobe
[sizeof(modprobe
) - 1] = '\0';
1363 ret
= system(modprobe
);
1365 ERR("Unable to launch modprobe --remove for module %s",
1366 kernel_modules_list
[i
].name
);
1367 } else if (kernel_modules_list
[i
].required
1368 && WEXITSTATUS(ret
) != 0) {
1369 ERR("Unable to remove module %s",
1370 kernel_modules_list
[i
].name
);
1372 DBG("Modprobe removal successful %s",
1373 kernel_modules_list
[i
].name
);
1384 static int mount_debugfs(char *path
)
1387 char *type
= "debugfs";
1389 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, geteuid(), getegid());
1394 ret
= mount(type
, path
, type
, 0, NULL
);
1396 perror("mount debugfs");
1400 DBG("Mounted debugfs successfully at %s", path
);
1407 * Setup necessary data for kernel tracer action.
1409 static void init_kernel_tracer(void)
1412 char *proc_mounts
= "/proc/mounts";
1414 char *debugfs_path
= NULL
, *lttng_path
;
1417 /* Detect debugfs */
1418 fp
= fopen(proc_mounts
, "r");
1420 ERR("Unable to probe %s", proc_mounts
);
1424 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1425 if (strstr(line
, "debugfs") != NULL
) {
1426 /* Remove first string */
1428 /* Dup string here so we can reuse line later on */
1429 debugfs_path
= strdup(strtok(NULL
, " "));
1430 DBG("Got debugfs path : %s", debugfs_path
);
1437 /* Mount debugfs if needded */
1438 if (debugfs_path
== NULL
) {
1439 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1441 perror("asprintf debugfs path");
1444 ret
= mount_debugfs(debugfs_path
);
1450 /* Modprobe lttng kernel modules */
1451 ret
= modprobe_kernel_modules();
1456 /* Setup lttng kernel path */
1457 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1459 perror("asprintf lttng path");
1463 /* Open debugfs lttng */
1464 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1465 if (kernel_tracer_fd
< 0) {
1466 DBG("Failed to open %s", lttng_path
);
1472 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1482 WARN("No kernel tracer available");
1483 kernel_tracer_fd
= 0;
1488 * Start tracing by creating trace directory and sending FDs to the kernel
1491 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1495 if (session
->kconsumer_fds_sent
== 0) {
1497 * Assign default kernel consumer if no consumer assigned to the kernel
1498 * session. At this point, it's NOT suppose to be 0 but this is an extra
1501 if (session
->consumer_fd
== 0) {
1502 session
->consumer_fd
= kconsumerd_cmd_sock
;
1505 ret
= send_kconsumerd_fds(session
);
1507 ERR("Send kconsumerd fds failed");
1508 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1512 session
->kconsumer_fds_sent
= 1;
1520 * Notify kernel thread to update it's pollfd.
1522 static int notify_kernel_pollfd(void)
1526 /* Inform kernel thread of the new kernel channel */
1527 ret
= write(kernel_poll_pipe
[1], "!", 1);
1529 perror("write kernel poll pipe");
1536 * Allocate a channel structure and fill it.
1538 static struct lttng_channel
*init_default_channel(enum lttng_domain_type domain_type
,
1541 struct lttng_channel
*chan
;
1543 chan
= malloc(sizeof(struct lttng_channel
));
1545 perror("init channel malloc");
1549 if (snprintf(chan
->name
, NAME_MAX
, "%s", name
) < 0) {
1550 perror("snprintf channel name");
1554 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1555 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1556 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1558 switch (domain_type
) {
1559 case LTTNG_DOMAIN_KERNEL
:
1560 chan
->attr
.subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
1561 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1562 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1566 goto error
; /* Not implemented */
1577 * Create a kernel tracer session then create the default channel.
1579 static int create_kernel_session(struct ltt_session
*session
)
1583 DBG("Creating kernel session");
1585 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1587 ret
= LTTCOMM_KERN_SESS_FAIL
;
1591 /* Set kernel consumer socket fd */
1592 if (kconsumerd_cmd_sock
) {
1593 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
1596 ret
= asprintf(&session
->kernel_session
->trace_path
, "%s/kernel",
1599 perror("asprintf kernel traces path");
1603 ret
= mkdir_recursive(session
->kernel_session
->trace_path
,
1604 S_IRWXU
| S_IRWXG
, geteuid(), allowed_group());
1606 if (ret
!= -EEXIST
) {
1607 ERR("Trace directory creation error");
1617 * Using the session list, filled a lttng_session array to send back to the
1618 * client for session listing.
1620 * The session list lock MUST be acquired before calling this function. Use
1621 * lock_session_list() and unlock_session_list().
1623 static void list_lttng_sessions(struct lttng_session
*sessions
)
1626 struct ltt_session
*session
;
1628 DBG("Getting all available session");
1630 * Iterate over session list and append data after the control struct in
1633 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1634 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1635 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
1636 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1637 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
1643 * Fill lttng_channel array of all channels.
1645 static void list_lttng_channels(struct ltt_session
*session
,
1646 struct lttng_channel
*channels
)
1649 struct ltt_kernel_channel
*kchan
;
1651 DBG("Listing channels for session %s", session
->name
);
1653 /* Kernel channels */
1654 if (session
->kernel_session
!= NULL
) {
1655 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
1656 /* Copy lttng_channel struct to array */
1657 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1658 channels
[i
].enabled
= kchan
->enabled
;
1663 /* TODO: Missing UST listing */
1667 * Fill lttng_event array of all events in the channel.
1669 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1670 struct lttng_event
*events
)
1673 * TODO: This is ONLY kernel. Need UST support.
1676 struct ltt_kernel_event
*event
;
1678 DBG("Listing events for channel %s", kchan
->channel
->name
);
1680 /* Kernel channels */
1681 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1682 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1683 events
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1684 events
[i
].enabled
= event
->enabled
;
1685 switch (event
->event
->instrumentation
) {
1686 case LTTNG_KERNEL_TRACEPOINT
:
1687 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1689 case LTTNG_KERNEL_KPROBE
:
1690 case LTTNG_KERNEL_KRETPROBE
:
1691 events
[i
].type
= LTTNG_EVENT_PROBE
;
1692 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
1693 sizeof(struct lttng_kernel_kprobe
));
1695 case LTTNG_KERNEL_FUNCTION
:
1696 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
1697 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
1698 sizeof(struct lttng_kernel_function
));
1706 * Process the command requested by the lttng client within the command
1707 * context structure. This function make sure that the return structure (llm)
1708 * is set and ready for transmission before returning.
1710 * Return any error encountered or 0 for success.
1712 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1714 int ret
= LTTCOMM_OK
;
1716 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1719 * Commands that DO NOT need a session.
1721 switch (cmd_ctx
->lsm
->cmd_type
) {
1722 case LTTNG_CREATE_SESSION
:
1723 case LTTNG_LIST_SESSIONS
:
1724 case LTTNG_LIST_TRACEPOINTS
:
1725 case LTTNG_CALIBRATE
:
1728 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
1729 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session
.name
);
1730 if (cmd_ctx
->session
== NULL
) {
1731 /* If session name not found */
1732 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
1733 ret
= LTTCOMM_SESS_NOT_FOUND
;
1734 } else { /* If no session name specified */
1735 ret
= LTTCOMM_SELECT_SESS
;
1739 /* Acquire lock for the session */
1740 lock_session(cmd_ctx
->session
);
1746 * Check domain type for specific "pre-action".
1748 switch (cmd_ctx
->lsm
->domain
.type
) {
1749 case LTTNG_DOMAIN_KERNEL
:
1750 /* Kernel tracer check */
1751 if (kernel_tracer_fd
== 0) {
1752 init_kernel_tracer();
1753 if (kernel_tracer_fd
== 0) {
1754 ret
= LTTCOMM_KERN_NA
;
1759 /* Need a session for kernel command */
1760 switch (cmd_ctx
->lsm
->cmd_type
) {
1761 case LTTNG_CALIBRATE
:
1762 case LTTNG_CREATE_SESSION
:
1763 case LTTNG_LIST_SESSIONS
:
1764 case LTTNG_LIST_TRACEPOINTS
:
1767 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1768 ret
= create_kernel_session(cmd_ctx
->session
);
1770 ret
= LTTCOMM_KERN_SESS_FAIL
;
1774 /* Start the kernel consumer daemon */
1776 if (kconsumerd_pid
== 0 &&
1777 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1778 ret
= start_kconsumerd();
1790 /* Process by command type */
1791 switch (cmd_ctx
->lsm
->cmd_type
) {
1792 case LTTNG_ADD_CONTEXT
:
1794 struct lttng_kernel_context kctx
;
1796 /* Setup lttng message with no payload */
1797 ret
= setup_lttng_msg(cmd_ctx
, 0);
1802 switch (cmd_ctx
->lsm
->domain
.type
) {
1803 case LTTNG_DOMAIN_KERNEL
:
1804 /* Create Kernel context */
1805 kctx
.ctx
= cmd_ctx
->lsm
->u
.context
.ctx
.ctx
;
1806 kctx
.u
.perf_counter
.type
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.type
;
1807 kctx
.u
.perf_counter
.config
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.config
;
1808 strncpy(kctx
.u
.perf_counter
.name
,
1809 cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.name
,
1810 LTTNG_SYMBOL_NAME_LEN
);
1811 kctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1813 /* Add kernel context to kernel tracer. See context.c */
1814 ret
= add_kernel_context(cmd_ctx
->session
->kernel_session
, &kctx
,
1815 cmd_ctx
->lsm
->u
.context
.event_name
,
1816 cmd_ctx
->lsm
->u
.context
.channel_name
);
1817 if (ret
!= LTTCOMM_OK
) {
1822 /* TODO: Userspace tracing */
1823 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1830 case LTTNG_DISABLE_CHANNEL
:
1832 struct ltt_kernel_channel
*kchan
;
1834 /* Setup lttng message with no payload */
1835 ret
= setup_lttng_msg(cmd_ctx
, 0);
1840 switch (cmd_ctx
->lsm
->domain
.type
) {
1841 case LTTNG_DOMAIN_KERNEL
:
1842 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1843 cmd_ctx
->session
->kernel_session
);
1844 if (kchan
== NULL
) {
1845 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1847 } else if (kchan
->enabled
== 1) {
1848 ret
= kernel_disable_channel(kchan
);
1850 if (ret
!= EEXIST
) {
1851 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1856 kernel_wait_quiescent(kernel_tracer_fd
);
1859 /* TODO: Userspace tracing */
1860 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1867 case LTTNG_DISABLE_EVENT
:
1869 struct ltt_kernel_channel
*kchan
;
1870 struct ltt_kernel_event
*kevent
;
1872 /* Setup lttng message with no payload */
1873 ret
= setup_lttng_msg(cmd_ctx
, 0);
1878 switch (cmd_ctx
->lsm
->domain
.type
) {
1879 case LTTNG_DOMAIN_KERNEL
:
1880 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1881 cmd_ctx
->session
->kernel_session
);
1882 if (kchan
== NULL
) {
1883 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1887 kevent
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, kchan
);
1888 if (kevent
!= NULL
) {
1889 DBG("Disabling kernel event %s for channel %s.", kevent
->event
->name
,
1890 kchan
->channel
->name
);
1891 ret
= kernel_disable_event(kevent
);
1893 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1898 kernel_wait_quiescent(kernel_tracer_fd
);
1901 /* TODO: Userspace tracing */
1902 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1909 case LTTNG_DISABLE_ALL_EVENT
:
1911 struct ltt_kernel_channel
*kchan
;
1912 struct ltt_kernel_event
*kevent
;
1914 /* Setup lttng message with no payload */
1915 ret
= setup_lttng_msg(cmd_ctx
, 0);
1920 switch (cmd_ctx
->lsm
->domain
.type
) {
1921 case LTTNG_DOMAIN_KERNEL
:
1922 DBG("Disabling all enabled kernel events");
1923 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1924 cmd_ctx
->session
->kernel_session
);
1925 if (kchan
== NULL
) {
1926 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1930 /* For each event in the kernel session */
1931 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1932 DBG("Disabling kernel event %s for channel %s.",
1933 kevent
->event
->name
, kchan
->channel
->name
);
1934 ret
= kernel_disable_event(kevent
);
1940 /* Quiescent wait after event disable */
1941 kernel_wait_quiescent(kernel_tracer_fd
);
1944 /* TODO: Userspace tracing */
1945 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1952 case LTTNG_ENABLE_CHANNEL
:
1954 struct ltt_kernel_channel
*kchan
;
1956 /* Setup lttng message with no payload */
1957 ret
= setup_lttng_msg(cmd_ctx
, 0);
1962 switch (cmd_ctx
->lsm
->domain
.type
) {
1963 case LTTNG_DOMAIN_KERNEL
:
1964 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1965 cmd_ctx
->session
->kernel_session
);
1966 if (kchan
== NULL
) {
1967 /* Channel not found, creating it */
1968 DBG("Creating kernel channel");
1970 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1971 &cmd_ctx
->lsm
->u
.channel
.chan
,
1972 cmd_ctx
->session
->kernel_session
->trace_path
);
1974 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1978 /* Notify kernel thread that there is a new channel */
1979 ret
= notify_kernel_pollfd();
1981 ret
= LTTCOMM_FATAL
;
1984 } else if (kchan
->enabled
== 0) {
1985 ret
= kernel_enable_channel(kchan
);
1987 if (ret
!= EEXIST
) {
1988 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1994 kernel_wait_quiescent(kernel_tracer_fd
);
1997 /* TODO: Userspace tracing */
1998 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2005 case LTTNG_ENABLE_EVENT
:
2008 struct ltt_kernel_channel
*kchan
;
2009 struct ltt_kernel_event
*kevent
;
2010 struct lttng_channel
*chan
;
2012 /* Setup lttng message with no payload */
2013 ret
= setup_lttng_msg(cmd_ctx
, 0);
2018 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2020 switch (cmd_ctx
->lsm
->domain
.type
) {
2021 case LTTNG_DOMAIN_KERNEL
:
2022 kchan
= get_kernel_channel_by_name(channel_name
,
2023 cmd_ctx
->session
->kernel_session
);
2024 if (kchan
== NULL
) {
2025 DBG("Channel not found. Creating channel %s", channel_name
);
2027 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2029 ret
= LTTCOMM_FATAL
;
2033 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2034 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2036 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2039 kchan
= get_kernel_channel_by_name(channel_name
,
2040 cmd_ctx
->session
->kernel_session
);
2041 if (kchan
== NULL
) {
2042 ERR("Channel %s not found after creation. Internal error, giving up.",
2044 ret
= LTTCOMM_FATAL
;
2049 kevent
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, kchan
);
2050 if (kevent
== NULL
) {
2051 DBG("Creating kernel event %s for channel %s.",
2052 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
2053 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, kchan
);
2055 DBG("Enabling kernel event %s for channel %s.",
2056 kevent
->event
->name
, channel_name
);
2057 ret
= kernel_enable_event(kevent
);
2058 if (ret
== -EEXIST
) {
2059 ret
= LTTCOMM_KERN_EVENT_EXIST
;
2065 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2069 kernel_wait_quiescent(kernel_tracer_fd
);
2072 /* TODO: Userspace tracing */
2073 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2079 case LTTNG_ENABLE_ALL_EVENT
:
2083 struct ltt_kernel_channel
*kchan
;
2084 struct ltt_kernel_event
*kevent
;
2085 struct lttng_event
*event_list
;
2086 struct lttng_channel
*chan
;
2088 /* Setup lttng message with no payload */
2089 ret
= setup_lttng_msg(cmd_ctx
, 0);
2094 DBG("Enabling all kernel event");
2096 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2098 switch (cmd_ctx
->lsm
->domain
.type
) {
2099 case LTTNG_DOMAIN_KERNEL
:
2100 kchan
= get_kernel_channel_by_name(channel_name
,
2101 cmd_ctx
->session
->kernel_session
);
2102 if (kchan
== NULL
) {
2103 DBG("Channel not found. Creating channel %s", channel_name
);
2105 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2107 ret
= LTTCOMM_FATAL
;
2111 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2112 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2114 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2117 kchan
= get_kernel_channel_by_name(channel_name
,
2118 cmd_ctx
->session
->kernel_session
);
2119 if (kchan
== NULL
) {
2120 ERR("Channel %s not found after creation. Internal error, giving up.",
2122 ret
= LTTCOMM_FATAL
;
2127 /* For each event in the kernel session */
2128 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
2129 DBG("Enabling kernel event %s for channel %s.",
2130 kevent
->event
->name
, channel_name
);
2131 ret
= kernel_enable_event(kevent
);
2137 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
2139 ret
= LTTCOMM_KERN_LIST_FAIL
;
2143 for (i
= 0; i
< size
; i
++) {
2144 kevent
= get_kernel_event_by_name(event_list
[i
].name
, kchan
);
2145 if (kevent
== NULL
) {
2146 /* Default event type for enable all */
2147 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2148 /* Enable each single tracepoint event */
2149 ret
= kernel_create_event(&event_list
[i
], kchan
);
2151 /* Ignore error here and continue */
2158 /* Quiescent wait after event enable */
2159 kernel_wait_quiescent(kernel_tracer_fd
);
2162 /* TODO: Userspace tracing */
2163 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2170 case LTTNG_LIST_TRACEPOINTS
:
2172 struct lttng_event
*events
;
2173 ssize_t nb_events
= 0;
2175 switch (cmd_ctx
->lsm
->domain
.type
) {
2176 case LTTNG_DOMAIN_KERNEL
:
2177 DBG("Listing kernel events");
2178 nb_events
= kernel_list_events(kernel_tracer_fd
, &events
);
2179 if (nb_events
< 0) {
2180 ret
= LTTCOMM_KERN_LIST_FAIL
;
2185 /* TODO: Userspace listing */
2186 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2191 * Setup lttng message with payload size set to the event list size in
2192 * bytes and then copy list into the llm payload.
2194 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2200 /* Copy event list into message payload */
2201 memcpy(cmd_ctx
->llm
->payload
, events
,
2202 sizeof(struct lttng_event
) * nb_events
);
2209 case LTTNG_START_TRACE
:
2211 struct ltt_kernel_channel
*chan
;
2213 /* Setup lttng message with no payload */
2214 ret
= setup_lttng_msg(cmd_ctx
, 0);
2219 /* Kernel tracing */
2220 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2221 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
2222 DBG("Open kernel metadata");
2223 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
2224 cmd_ctx
->session
->kernel_session
->trace_path
);
2226 ret
= LTTCOMM_KERN_META_FAIL
;
2231 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2232 DBG("Opening kernel metadata stream");
2233 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2234 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
2236 ERR("Kernel create metadata stream failed");
2237 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2243 /* For each channel */
2244 cds_list_for_each_entry(chan
,
2245 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2246 if (chan
->stream_count
== 0) {
2247 ret
= kernel_open_channel_stream(chan
);
2249 ERR("Kernel create channel stream failed");
2250 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2253 /* Update the stream global counter */
2254 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
2258 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
2260 ret
= LTTCOMM_KERN_START_FAIL
;
2264 DBG("Start kernel tracing");
2265 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
2267 ERR("Kernel start session failed");
2268 ret
= LTTCOMM_KERN_START_FAIL
;
2272 /* Quiescent wait after starting trace */
2273 kernel_wait_quiescent(kernel_tracer_fd
);
2276 /* TODO: Start all UST traces */
2281 case LTTNG_STOP_TRACE
:
2283 struct ltt_kernel_channel
*chan
;
2284 /* Setup lttng message with no payload */
2285 ret
= setup_lttng_msg(cmd_ctx
, 0);
2291 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2292 DBG("Stop kernel tracing");
2294 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
2296 ERR("Kernel metadata flush failed");
2299 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2300 ret
= kernel_flush_buffer(chan
);
2302 ERR("Kernel flush buffer error");
2306 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
2308 ERR("Kernel stop session failed");
2309 ret
= LTTCOMM_KERN_STOP_FAIL
;
2313 /* Quiescent wait after stopping trace */
2314 kernel_wait_quiescent(kernel_tracer_fd
);
2317 /* TODO : User-space tracer */
2322 case LTTNG_CREATE_SESSION
:
2324 /* Setup lttng message with no payload */
2325 ret
= setup_lttng_msg(cmd_ctx
, 0);
2330 tracepoint(create_session_start
);
2331 ret
= create_session(cmd_ctx
->lsm
->session
.name
, cmd_ctx
->lsm
->session
.path
);
2332 tracepoint(create_session_end
);
2334 if (ret
== -EEXIST
) {
2335 ret
= LTTCOMM_EXIST_SESS
;
2337 ret
= LTTCOMM_FATAL
;
2345 case LTTNG_DESTROY_SESSION
:
2347 /* Setup lttng message with no payload */
2348 ret
= setup_lttng_msg(cmd_ctx
, 0);
2353 /* Clean kernel session teardown */
2354 teardown_kernel_session(cmd_ctx
->session
);
2356 tracepoint(destroy_session_start
);
2357 ret
= destroy_session(cmd_ctx
->lsm
->session
.name
);
2358 tracepoint(destroy_session_end
);
2360 ret
= LTTCOMM_FATAL
;
2365 * Must notify the kernel thread here to update it's pollfd in order to
2366 * remove the channel(s)' fd just destroyed.
2368 ret
= notify_kernel_pollfd();
2370 ret
= LTTCOMM_FATAL
;
2377 case LTTNG_LIST_DOMAINS
:
2381 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2385 nb_dom
+= cmd_ctx
->session
->ust_trace_count
;
2387 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_domain
) * nb_dom
);
2392 ((struct lttng_domain
*)(cmd_ctx
->llm
->payload
))[0].type
=
2393 LTTNG_DOMAIN_KERNEL
;
2395 /* TODO: User-space tracer domain support */
2399 case LTTNG_LIST_CHANNELS
:
2402 * TODO: Only kernel channels are listed here. UST listing
2403 * is needed on lttng-ust 2.0 release.
2406 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2407 nb_chan
+= cmd_ctx
->session
->kernel_session
->channel_count
;
2410 ret
= setup_lttng_msg(cmd_ctx
,
2411 sizeof(struct lttng_channel
) * nb_chan
);
2416 list_lttng_channels(cmd_ctx
->session
,
2417 (struct lttng_channel
*)(cmd_ctx
->llm
->payload
));
2422 case LTTNG_LIST_EVENTS
:
2425 * TODO: Only kernel events are listed here. UST listing
2426 * is needed on lttng-ust 2.0 release.
2428 size_t nb_event
= 0;
2429 struct ltt_kernel_channel
*kchan
= NULL
;
2431 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2432 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.list
.channel_name
,
2433 cmd_ctx
->session
->kernel_session
);
2434 if (kchan
== NULL
) {
2435 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2438 nb_event
+= kchan
->event_count
;
2441 ret
= setup_lttng_msg(cmd_ctx
,
2442 sizeof(struct lttng_event
) * nb_event
);
2447 DBG("Listing events (%zu events)", nb_event
);
2449 list_lttng_events(kchan
,
2450 (struct lttng_event
*)(cmd_ctx
->llm
->payload
));
2455 case LTTNG_LIST_SESSIONS
:
2457 lock_session_list();
2459 if (session_list_ptr
->count
== 0) {
2460 ret
= LTTCOMM_NO_SESSION
;
2461 unlock_session_list();
2465 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
2466 session_list_ptr
->count
);
2468 unlock_session_list();
2472 /* Filled the session array */
2473 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
2475 unlock_session_list();
2480 case LTTNG_CALIBRATE
:
2482 /* Setup lttng message with no payload */
2483 ret
= setup_lttng_msg(cmd_ctx
, 0);
2488 switch (cmd_ctx
->lsm
->domain
.type
) {
2489 case LTTNG_DOMAIN_KERNEL
:
2491 struct lttng_kernel_calibrate kcalibrate
;
2493 kcalibrate
.type
= cmd_ctx
->lsm
->u
.calibrate
.type
;
2494 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2496 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2502 /* TODO: Userspace tracing */
2503 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2509 case LTTNG_REGISTER_CONSUMER
:
2513 /* Setup lttng message with no payload */
2514 ret
= setup_lttng_msg(cmd_ctx
, 0);
2519 switch (cmd_ctx
->lsm
->domain
.type
) {
2520 case LTTNG_DOMAIN_KERNEL
:
2522 /* Can't register a consumer if there is already one */
2523 if (cmd_ctx
->session
->kernel_session
->consumer_fd
!= 0) {
2524 ret
= LTTCOMM_CONNECT_FAIL
;
2528 sock
= lttcomm_connect_unix_sock(cmd_ctx
->lsm
->u
.reg
.path
);
2530 ret
= LTTCOMM_CONNECT_FAIL
;
2534 cmd_ctx
->session
->kernel_session
->consumer_fd
= sock
;
2538 /* TODO: Userspace tracing */
2539 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2548 /* Undefined command */
2549 ret
= setup_lttng_msg(cmd_ctx
, 0);
2558 /* Set return code */
2559 cmd_ctx
->llm
->ret_code
= ret
;
2561 if (cmd_ctx
->session
) {
2562 unlock_session(cmd_ctx
->session
);
2568 if (cmd_ctx
->llm
== NULL
) {
2569 DBG("Missing llm structure. Allocating one.");
2570 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2574 /* Notify client of error */
2575 cmd_ctx
->llm
->ret_code
= ret
;
2578 if (cmd_ctx
->session
) {
2579 unlock_session(cmd_ctx
->session
);
2585 * This thread manage all clients request using the unix client socket for
2588 static void *thread_manage_clients(void *data
)
2591 struct command_ctx
*cmd_ctx
= NULL
;
2592 struct pollfd pollfd
[2];
2594 tracepoint(sessiond_th_cli_start
);
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 tracepoint(sessiond_th_cli_poll
);
2622 /* Inifinite blocking call, waiting for transmission */
2623 ret
= poll(pollfd
, 2, -1);
2625 perror("poll client thread");
2629 /* Thread quit pipe has been closed. Killing thread. */
2630 if (pollfd
[0].revents
== POLLNVAL
) {
2632 } else if (pollfd
[1].revents
== POLLERR
) {
2633 ERR("Client socket poll error");
2637 sock
= lttcomm_accept_unix_sock(client_sock
);
2642 /* Allocate context command to process the client request */
2643 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2645 /* Allocate data buffer for reception */
2646 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2647 cmd_ctx
->llm
= NULL
;
2648 cmd_ctx
->session
= NULL
;
2651 * Data is received from the lttng client. The struct
2652 * lttcomm_session_msg (lsm) contains the command and data request of
2655 DBG("Receiving data from client ...");
2656 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2661 // TODO: Validate cmd_ctx including sanity check for security purpose.
2664 * This function dispatch the work to the kernel or userspace tracer
2665 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2666 * informations for the client. The command context struct contains
2667 * everything this function may needs.
2669 ret
= process_client_msg(cmd_ctx
);
2671 /* TODO: Inform client somehow of the fatal error. At this point,
2672 * ret < 0 means that a malloc failed (ENOMEM). */
2673 /* Error detected but still accept command */
2674 clean_command_ctx(&cmd_ctx
);
2678 DBG("Sending response (size: %d, retcode: %d)",
2679 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2680 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2682 ERR("Failed to send data back to client");
2685 clean_command_ctx(&cmd_ctx
);
2687 /* End of transmission */
2692 DBG("Client thread dying");
2700 unlink(client_unix_sock_path
);
2702 clean_command_ctx(&cmd_ctx
);
2708 * usage function on stderr
2710 static void usage(void)
2712 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2713 fprintf(stderr
, " -h, --help Display this usage.\n");
2714 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2715 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2716 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2717 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2718 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2719 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2720 fprintf(stderr
, " -V, --version Show version number.\n");
2721 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2722 fprintf(stderr
, " -q, --quiet No output at all.\n");
2723 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2724 fprintf(stderr
, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2728 * daemon argument parsing
2730 static int parse_args(int argc
, char **argv
)
2734 static struct option long_options
[] = {
2735 { "client-sock", 1, 0, 'c' },
2736 { "apps-sock", 1, 0, 'a' },
2737 { "kconsumerd-cmd-sock", 1, 0, 0 },
2738 { "kconsumerd-err-sock", 1, 0, 0 },
2739 { "daemonize", 0, 0, 'd' },
2740 { "sig-parent", 0, 0, 'S' },
2741 { "help", 0, 0, 'h' },
2742 { "group", 1, 0, 'g' },
2743 { "version", 0, 0, 'V' },
2744 { "quiet", 0, 0, 'q' },
2745 { "verbose", 0, 0, 'v' },
2746 { "verbose-kconsumerd", 0, 0, 'Z' },
2751 int option_index
= 0;
2752 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:Z", long_options
, &option_index
);
2759 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2761 fprintf(stderr
, " with arg %s\n", optarg
);
2765 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2768 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2774 opt_tracing_group
= strdup(optarg
);
2780 fprintf(stdout
, "%s\n", VERSION
);
2786 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2789 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2795 /* Verbose level can increase using multiple -v */
2799 opt_verbose_kconsumerd
+= 1;
2802 /* Unknown option or other error.
2803 * Error is printed by getopt, just return */
2812 * Creates the two needed socket by the daemon.
2813 * apps_sock - The communication socket for all UST apps.
2814 * client_sock - The communication of the cli tool (lttng).
2816 static int init_daemon_socket(void)
2821 old_umask
= umask(0);
2823 /* Create client tool unix socket */
2824 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2825 if (client_sock
< 0) {
2826 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2831 /* File permission MUST be 660 */
2832 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2834 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2839 /* Create the application unix socket */
2840 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2841 if (apps_sock
< 0) {
2842 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2847 /* File permission MUST be 666 */
2848 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2850 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2861 * Check if the global socket is available, and if a daemon is answering
2862 * at the other side. If yes, error is returned.
2864 static int check_existing_daemon(void)
2866 if (access(client_unix_sock_path
, F_OK
) < 0 &&
2867 access(apps_unix_sock_path
, F_OK
) < 0) {
2870 /* Is there anybody out there ? */
2871 if (lttng_session_daemon_alive()) {
2879 * Set the tracing group gid onto the client socket.
2881 * Race window between mkdir and chown is OK because we are going from more
2882 * permissive (root.root) to les permissive (root.tracing).
2884 static int set_permissions(void)
2889 gid
= allowed_group();
2892 WARN("No tracing group detected");
2895 ERR("Missing tracing group. Aborting execution.");
2901 /* Set lttng run dir */
2902 ret
= chown(LTTNG_RUNDIR
, 0, gid
);
2904 ERR("Unable to set group on " LTTNG_RUNDIR
);
2908 /* lttng client socket path */
2909 ret
= chown(client_unix_sock_path
, 0, gid
);
2911 ERR("Unable to set group on %s", client_unix_sock_path
);
2915 /* kconsumerd error socket path */
2916 ret
= chown(kconsumerd_err_unix_sock_path
, 0, gid
);
2918 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2922 DBG("All permissions are set");
2929 * Create the pipe used to wake up the kernel thread.
2931 static int create_kernel_poll_pipe(void)
2933 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2937 * Create the application command pipe to wake thread_manage_apps.
2939 static int create_apps_cmd_pipe(void)
2941 return pipe2(apps_cmd_pipe
, O_CLOEXEC
);
2945 * Create the lttng run directory needed for all global sockets and pipe.
2947 static int create_lttng_rundir(void)
2951 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2953 if (errno
!= EEXIST
) {
2954 ERR("Unable to create " LTTNG_RUNDIR
);
2966 * Setup sockets and directory needed by the kconsumerd communication with the
2969 static int set_kconsumerd_sockets(void)
2973 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
2974 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
2977 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
2978 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
2981 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
2983 if (errno
!= EEXIST
) {
2984 ERR("Failed to create " KCONSUMERD_PATH
);
2990 /* Create the kconsumerd error unix socket */
2991 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
2992 if (kconsumerd_err_sock
< 0) {
2993 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
2998 /* File permission MUST be 660 */
2999 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3001 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
3011 * Signal handler for the daemon
3013 * Simply stop all worker threads, leaving main() return gracefully
3014 * after joining all threads and calling cleanup().
3016 static void sighandler(int sig
)
3020 DBG("SIGPIPE catched");
3023 DBG("SIGINT catched");
3027 DBG("SIGTERM catched");
3036 * Setup signal handler for :
3037 * SIGINT, SIGTERM, SIGPIPE
3039 static int set_signal_handler(void)
3042 struct sigaction sa
;
3045 if ((ret
= sigemptyset(&sigset
)) < 0) {
3046 perror("sigemptyset");
3050 sa
.sa_handler
= sighandler
;
3051 sa
.sa_mask
= sigset
;
3053 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3054 perror("sigaction");
3058 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3059 perror("sigaction");
3063 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3064 perror("sigaction");
3068 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3074 * Set open files limit to unlimited. This daemon can open a large number of
3075 * file descriptors in order to consumer multiple kernel traces.
3077 static void set_ulimit(void)
3082 /* The kernel does not allowed an infinite limit for open files */
3083 lim
.rlim_cur
= 65535;
3084 lim
.rlim_max
= 65535;
3086 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3088 perror("failed to set open files limit");
3095 int main(int argc
, char **argv
)
3099 const char *home_path
;
3101 tracepoint(sessiond_boot_start
);
3103 /* Create thread quit pipe */
3104 if ((ret
= init_thread_quit_pipe()) < 0) {
3108 /* Parse arguments */
3110 if ((ret
= parse_args(argc
, argv
) < 0)) {
3123 /* Check if daemon is UID = 0 */
3124 is_root
= !getuid();
3127 ret
= create_lttng_rundir();
3132 if (strlen(apps_unix_sock_path
) == 0) {
3133 snprintf(apps_unix_sock_path
, PATH_MAX
,
3134 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
3137 if (strlen(client_unix_sock_path
) == 0) {
3138 snprintf(client_unix_sock_path
, PATH_MAX
,
3139 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
3142 home_path
= get_home_dir();
3143 if (home_path
== NULL
) {
3144 /* TODO: Add --socket PATH option */
3145 ERR("Can't get HOME directory for sockets creation.");
3150 if (strlen(apps_unix_sock_path
) == 0) {
3151 snprintf(apps_unix_sock_path
, PATH_MAX
,
3152 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
3155 /* Set the cli tool unix socket path */
3156 if (strlen(client_unix_sock_path
) == 0) {
3157 snprintf(client_unix_sock_path
, PATH_MAX
,
3158 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
3162 DBG("Client socket path %s", client_unix_sock_path
);
3163 DBG("Application socket path %s", apps_unix_sock_path
);
3166 * See if daemon already exist.
3168 if ((ret
= check_existing_daemon()) < 0) {
3169 ERR("Already running daemon.\n");
3171 * We do not goto exit because we must not cleanup()
3172 * because a daemon is already running.
3177 /* After this point, we can safely call cleanup() so goto error is used */
3180 * These actions must be executed as root. We do that *after* setting up
3181 * the sockets path because we MUST make the check for another daemon using
3182 * those paths *before* trying to set the kernel consumer sockets and init
3186 ret
= set_kconsumerd_sockets();
3191 /* Setup kernel tracer */
3192 init_kernel_tracer();
3194 /* Set ulimit for open files */
3198 if ((ret
= set_signal_handler()) < 0) {
3202 /* Setup the needed unix socket */
3203 if ((ret
= init_daemon_socket()) < 0) {
3207 /* Set credentials to socket */
3208 if (is_root
&& ((ret
= set_permissions()) < 0)) {
3212 /* Get parent pid if -S, --sig-parent is specified. */
3213 if (opt_sig_parent
) {
3217 /* Setup the kernel pipe for waking up the kernel thread */
3218 if ((ret
= create_kernel_poll_pipe()) < 0) {
3222 /* Setup the thread apps communication pipe. */
3223 if ((ret
= create_apps_cmd_pipe()) < 0) {
3227 /* Init UST command queue. */
3228 cds_wfq_init(&ust_cmd_queue
.queue
);
3231 * Get session list pointer. This pointer MUST NOT be free().
3232 * This list is statically declared in session.c
3234 session_list_ptr
= get_session_list();
3236 /* Create thread to manage the client socket */
3237 ret
= pthread_create(&client_thread
, NULL
,
3238 thread_manage_clients
, (void *) NULL
);
3240 perror("pthread_create clients");
3244 /* Create thread to dispatch registration */
3245 ret
= pthread_create(&dispatch_thread
, NULL
,
3246 thread_dispatch_ust_registration
, (void *) NULL
);
3248 perror("pthread_create dispatch");
3252 /* Create thread to manage application registration. */
3253 ret
= pthread_create(®_apps_thread
, NULL
,
3254 thread_registration_apps
, (void *) NULL
);
3256 perror("pthread_create registration");
3260 /* Create thread to manage application socket */
3261 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
3263 perror("pthread_create apps");
3267 /* Create kernel thread to manage kernel event */
3268 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
3270 perror("pthread_create kernel");
3274 tracepoint(sessiond_boot_end
);
3276 ret
= pthread_join(kernel_thread
, &status
);
3278 perror("pthread_join");
3279 goto error
; /* join error, exit without cleanup */
3283 ret
= pthread_join(apps_thread
, &status
);
3285 perror("pthread_join");
3286 goto error
; /* join error, exit without cleanup */
3290 ret
= pthread_join(reg_apps_thread
, &status
);
3292 perror("pthread_join");
3293 goto error
; /* join error, exit without cleanup */
3297 ret
= pthread_join(dispatch_thread
, &status
);
3299 perror("pthread_join");
3300 goto error
; /* join error, exit without cleanup */
3304 ret
= pthread_join(client_thread
, &status
);
3306 perror("pthread_join");
3307 goto error
; /* join error, exit without cleanup */
3310 ret
= join_kconsumerd_thread();
3312 perror("join_kconsumerd");
3313 goto error
; /* join error, exit without cleanup */
3319 * cleanup() is called when no other thread is running.