2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <semaphore.h>
32 #include <sys/mount.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <sys/resource.h>
41 #include <urcu/list.h> /* URCU list library (-lurcu) */
42 #include <lttng/lttng.h>
44 #include "liblttsessiondcomm.h"
45 #include "ltt-sessiond.h"
47 #include "kernel-ctl.h"
50 #include "traceable-app.h"
51 #include "lttng-kconsumerd.h"
52 #include "libustctl.h"
57 * teardown: signal SIGTERM handler -> write into pipe. Threads waits
58 * with epoll on pipe and on other pipes/sockets for commands. Main
59 * simply waits on pthread join.
63 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
64 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
65 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
66 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
69 int opt_verbose
; /* Not static for lttngerr.h */
70 int opt_quiet
; /* Not static for lttngerr.h */
72 const char *opt_tracing_group
;
73 static int opt_sig_parent
;
74 static int opt_daemon
;
75 static int is_root
; /* Set to 1 if the daemon is running as root */
76 static pid_t ppid
; /* Parent PID for --sig-parent option */
77 static pid_t kconsumerd_pid
;
78 static struct pollfd
*kernel_pollfd
;
80 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
81 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
82 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
83 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
86 static int client_sock
;
88 static int kconsumerd_err_sock
;
89 static int kconsumerd_cmd_sock
;
90 static int kernel_tracer_fd
;
91 static int kernel_poll_pipe
[2];
94 * Quit pipe for all threads. This permits a single cancellation point
95 * for all threads when receiving an event on the pipe.
97 static int thread_quit_pipe
[2];
99 /* Pthread, Mutexes and Semaphores */
100 static pthread_t kconsumerd_thread
;
101 static pthread_t apps_thread
;
102 static pthread_t client_thread
;
103 static pthread_t kernel_thread
;
104 static sem_t kconsumerd_sem
;
106 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
109 * Pointer initialized before thread creation.
111 * This points to the tracing session list containing the session count and a
112 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
113 * MUST NOT be taken if you call a public function in session.c.
115 * The lock is nested inside the structure: session_list_ptr->lock.
117 static struct ltt_session_list
*session_list_ptr
;
122 * Return -1 on error or 0 if all pipes are created.
124 static int init_thread_quit_pipe(void)
128 ret
= pipe2(thread_quit_pipe
, O_CLOEXEC
);
130 perror("thread quit pipe");
139 * teardown_kernel_session
141 * Complete teardown of a kernel session. This free all data structure related
142 * to a kernel session and update counter.
144 static void teardown_kernel_session(struct ltt_session
*session
)
146 if (session
->kernel_session
!= NULL
) {
147 DBG("Tearing down kernel session");
148 trace_destroy_kernel_session(session
->kernel_session
);
149 /* Extra precaution */
150 session
->kernel_session
= NULL
;
157 static void cleanup()
161 struct ltt_session
*sess
;
166 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
167 "Matthew, BEET driven development works!%c[%dm",
168 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
171 /* Stopping all threads */
172 DBG("Terminating all threads");
173 close(thread_quit_pipe
[0]);
174 close(thread_quit_pipe
[1]);
176 DBG("Removing %s directory", LTTNG_RUNDIR
);
177 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
179 ERR("asprintf failed. Something is really wrong!");
182 /* Remove lttng run directory */
185 ERR("Unable to clean " LTTNG_RUNDIR
);
188 DBG("Cleaning up all session");
190 /* Destroy session list mutex */
191 if (session_list_ptr
!= NULL
) {
192 pthread_mutex_destroy(&session_list_ptr
->lock
);
194 /* Cleanup ALL session */
195 cds_list_for_each_entry(sess
, &session_list_ptr
->head
, list
) {
196 teardown_kernel_session(sess
);
197 // TODO complete session cleanup (including UST)
201 pthread_mutex_destroy(&kconsumerd_pid_mutex
);
203 DBG("Closing kernel fd");
204 close(kernel_tracer_fd
);
210 * Send data on a unix socket using the liblttsessiondcomm API.
212 * Return lttcomm error code.
214 static int send_unix_sock(int sock
, void *buf
, size_t len
)
216 /* Check valid length */
221 return lttcomm_send_unix_sock(sock
, buf
, len
);
227 * Free memory of a command context structure.
229 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
231 DBG("Clean command context structure");
233 if ((*cmd_ctx
)->llm
) {
234 free((*cmd_ctx
)->llm
);
236 if ((*cmd_ctx
)->lsm
) {
237 free((*cmd_ctx
)->lsm
);
245 * send_kconsumerd_channel_fds
247 * Send all stream fds of kernel channel to the consumer.
249 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
253 struct ltt_kernel_stream
*stream
;
254 struct lttcomm_kconsumerd_header lkh
;
255 struct lttcomm_kconsumerd_msg lkm
;
257 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
259 nb_fd
= channel
->stream_count
;
262 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
263 lkh
.cmd_type
= ADD_STREAM
;
265 DBG("Sending kconsumerd header");
267 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
269 perror("send kconsumerd header");
273 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
274 if (stream
->fd
!= 0) {
276 lkm
.state
= stream
->state
;
277 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
278 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
280 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
282 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
284 perror("send kconsumerd fd");
290 DBG("Kconsumerd channel fds sent");
299 * send_kconsumerd_fds
301 * Send all stream fds of the kernel session to the consumer.
303 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
306 struct ltt_kernel_channel
*chan
;
307 struct lttcomm_kconsumerd_header lkh
;
308 struct lttcomm_kconsumerd_msg lkm
;
311 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
312 lkh
.cmd_type
= ADD_STREAM
;
314 DBG("Sending kconsumerd header for metadata");
316 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
318 perror("send kconsumerd header");
322 DBG("Sending metadata stream fd");
324 if (session
->metadata_stream_fd
!= 0) {
325 /* Send metadata stream fd first */
326 lkm
.fd
= session
->metadata_stream_fd
;
327 lkm
.state
= ACTIVE_FD
;
328 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
329 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
331 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
333 perror("send kconsumerd fd");
338 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
339 ret
= send_kconsumerd_channel_fds(sock
, chan
);
345 DBG("Kconsumerd fds (metadata and channel streams) sent");
356 * Return a socket connected to the libust communication socket
357 * of the application identified by the pid.
359 * If the pid is not found in the traceable list,
360 * return -1 to indicate error.
362 static int ust_connect_app(pid_t pid
)
365 struct ltt_traceable_app
*lta
;
367 DBG("Connect to application pid %d", pid
);
369 lta
= find_app_by_pid(pid
);
372 DBG("Application pid %d not found", pid
);
376 sock
= ustctl_connect_pid(lta
->pid
);
378 ERR("Fail connecting to the PID %d", pid
);
387 * Notify apps by writing 42 to a named pipe using name.
388 * Every applications waiting for a ltt-sessiond will be notified
389 * and re-register automatically to the session daemon.
391 * Return open or write error value.
393 static int notify_apps(const char *name
)
398 DBG("Notify the global application pipe");
400 /* Try opening the global pipe */
401 fd
= open(name
, O_WRONLY
);
406 /* Notify by writing on the pipe */
407 ret
= write(fd
, "42", 2);
419 * Setup the outgoing data buffer for the response (llm) by allocating the
420 * right amount of memory and copying the original information from the lsm
423 * Return total size of the buffer pointed by buf.
425 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
431 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
432 if (cmd_ctx
->llm
== NULL
) {
438 /* Copy common data */
439 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
440 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->pid
;
442 cmd_ctx
->llm
->data_size
= size
;
443 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
452 * update_kernel_pollfd
454 * Update the kernel pollfd set of all channel fd available over
455 * all tracing session. Add the wakeup pipe at the end of the set.
457 static int update_kernel_pollfd(void)
461 * The wakup pipe and the quit pipe are needed so the number of fds starts
462 * at 2 for those pipes.
464 unsigned int nb_fd
= 2;
465 struct ltt_session
*session
;
466 struct ltt_kernel_channel
*channel
;
468 DBG("Updating kernel_pollfd");
470 /* Get the number of channel of all kernel session */
472 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
473 lock_session(session
);
474 if (session
->kernel_session
== NULL
) {
475 unlock_session(session
);
478 nb_fd
+= session
->kernel_session
->channel_count
;
479 unlock_session(session
);
482 DBG("Resizing kernel_pollfd to size %d", nb_fd
);
484 kernel_pollfd
= realloc(kernel_pollfd
, nb_fd
* sizeof(struct pollfd
));
485 if (kernel_pollfd
== NULL
) {
486 perror("malloc kernel_pollfd");
490 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
491 lock_session(session
);
492 if (session
->kernel_session
== NULL
) {
493 unlock_session(session
);
497 ERR("To much channel for kernel_pollfd size");
498 unlock_session(session
);
501 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
502 kernel_pollfd
[i
].fd
= channel
->fd
;
503 kernel_pollfd
[i
].events
= POLLIN
| POLLRDNORM
;
506 unlock_session(session
);
508 unlock_session_list();
510 /* Adding wake up pipe */
511 kernel_pollfd
[nb_fd
- 2].fd
= kernel_poll_pipe
[0];
512 kernel_pollfd
[nb_fd
- 2].events
= POLLIN
;
514 /* Adding the quit pipe */
515 kernel_pollfd
[nb_fd
- 1].fd
= thread_quit_pipe
[0];
520 unlock_session_list();
525 * update_kernel_stream
527 * Find the channel fd from 'fd' over all tracing session. When found, check
528 * for new channel stream and send those stream fds to the kernel consumer.
530 * Useful for CPU hotplug feature.
532 static int update_kernel_stream(int fd
)
535 struct ltt_session
*session
;
536 struct ltt_kernel_channel
*channel
;
538 DBG("Updating kernel streams for channel fd %d", fd
);
541 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
542 lock_session(session
);
543 if (session
->kernel_session
== NULL
) {
544 unlock_session(session
);
547 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
548 if (channel
->fd
== fd
) {
549 DBG("Channel found, updating kernel streams");
550 ret
= kernel_open_channel_stream(channel
);
555 * Have we already sent fds to the consumer? If yes, it means that
556 * tracing is started so it is safe to send our updated stream fds.
558 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
559 ret
= send_kconsumerd_channel_fds(kconsumerd_cmd_sock
, channel
);
567 unlock_session(session
);
571 unlock_session_list();
573 unlock_session(session
);
579 * thread_manage_kernel
581 * This thread manage event coming from the kernel.
583 * Features supported in this thread:
586 static void *thread_manage_kernel(void *data
)
588 int ret
, i
, nb_fd
= 0;
590 int update_poll_flag
= 1;
592 DBG("Thread manage kernel started");
595 if (update_poll_flag
== 1) {
596 nb_fd
= update_kernel_pollfd();
600 update_poll_flag
= 0;
603 DBG("Polling on %d fds", nb_fd
);
605 /* Poll infinite value of time */
606 ret
= poll(kernel_pollfd
, nb_fd
, -1);
608 perror("poll kernel thread");
610 } else if (ret
== 0) {
611 /* Should not happen since timeout is infinite */
615 /* Thread quit pipe has been closed. Killing thread. */
616 if (kernel_pollfd
[nb_fd
- 1].revents
== POLLNVAL
) {
620 DBG("Kernel poll event triggered");
623 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
626 switch (kernel_pollfd
[nb_fd
- 2].revents
) {
628 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
629 update_poll_flag
= 1;
637 for (i
= 0; i
< nb_fd
; i
++) {
638 switch (kernel_pollfd
[i
].revents
) {
640 * New CPU detected by the kernel. Adding kernel stream to kernel
641 * session and updating the kernel consumer
643 case POLLIN
| POLLRDNORM
:
644 ret
= update_kernel_stream(kernel_pollfd
[i
].fd
);
654 DBG("Kernel thread dying");
659 close(kernel_poll_pipe
[0]);
660 close(kernel_poll_pipe
[1]);
665 * thread_manage_kconsumerd
667 * This thread manage the kconsumerd error sent
668 * back to the session daemon.
670 static void *thread_manage_kconsumerd(void *data
)
673 enum lttcomm_return_code code
;
674 struct pollfd pollfd
[2];
676 DBG("[thread] Manage kconsumerd started");
678 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
683 /* First fd is always the quit pipe */
684 pollfd
[0].fd
= thread_quit_pipe
[0];
687 pollfd
[1].fd
= kconsumerd_err_sock
;
688 pollfd
[1].events
= POLLIN
;
690 /* Inifinite blocking call, waiting for transmission */
691 ret
= poll(pollfd
, 2, -1);
693 perror("poll kconsumerd thread");
697 /* Thread quit pipe has been closed. Killing thread. */
698 if (pollfd
[0].revents
== POLLNVAL
) {
700 } else if (pollfd
[1].revents
== POLLERR
) {
701 ERR("Kconsumerd err socket poll error");
705 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
710 /* Getting status code from kconsumerd */
711 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
716 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
717 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
718 if (kconsumerd_cmd_sock
< 0) {
719 sem_post(&kconsumerd_sem
);
720 perror("kconsumerd connect");
723 /* Signal condition to tell that the kconsumerd is ready */
724 sem_post(&kconsumerd_sem
);
725 DBG("Kconsumerd command socket ready");
727 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
728 lttcomm_get_readable_code(-code
));
732 /* Wait for any kconsumerd error */
733 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
735 ERR("Kconsumerd closed the command socket");
739 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
742 DBG("Kconsumerd thread dying");
743 if (kconsumerd_err_sock
) {
744 close(kconsumerd_err_sock
);
746 if (kconsumerd_cmd_sock
) {
747 close(kconsumerd_cmd_sock
);
753 unlink(kconsumerd_err_unix_sock_path
);
754 unlink(kconsumerd_cmd_unix_sock_path
);
763 * This thread manage the application socket communication
765 static void *thread_manage_apps(void *data
)
768 struct pollfd pollfd
[2];
770 /* TODO: Something more elegant is needed but fine for now */
771 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
772 * for 32-bit vs 64-bit compat processes. */
773 /* replicate in ust with version number */
775 int reg
; /* 1:register, 0:unregister */
780 DBG("[thread] Manage apps started");
782 ret
= lttcomm_listen_unix_sock(apps_sock
);
787 /* First fd is always the quit pipe */
788 pollfd
[0].fd
= thread_quit_pipe
[0];
791 pollfd
[1].fd
= apps_sock
;
792 pollfd
[1].events
= POLLIN
;
794 /* Notify all applications to register */
795 notify_apps(default_global_apps_pipe
);
798 DBG("Accepting application registration");
800 /* Inifinite blocking call, waiting for transmission */
801 ret
= poll(pollfd
, 2, -1);
803 perror("poll apps thread");
807 /* Thread quit pipe has been closed. Killing thread. */
808 if (pollfd
[0].revents
== POLLNVAL
) {
810 } else if (pollfd
[1].revents
== POLLERR
) {
811 ERR("Apps socket poll error");
815 sock
= lttcomm_accept_unix_sock(apps_sock
);
821 * Basic recv here to handle the very simple data
822 * that the libust send to register (reg_msg).
824 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
830 /* Add application to the global traceable list */
831 if (reg_msg
.reg
== 1) {
833 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
835 /* register_traceable_app only return an error with
836 * ENOMEM. At this point, we better stop everything.
842 unregister_traceable_app(reg_msg
.pid
);
847 DBG("Apps thread dying");
855 unlink(apps_unix_sock_path
);
860 * spawn_kconsumerd_thread
862 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
863 * exec or it will fails.
865 static int spawn_kconsumerd_thread(void)
869 /* Setup semaphore */
870 sem_init(&kconsumerd_sem
, 0, 0);
872 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
874 perror("pthread_create kconsumerd");
878 /* Wait for the kconsumerd thread to be ready */
879 sem_wait(&kconsumerd_sem
);
881 if (kconsumerd_pid
== 0) {
882 ERR("Kconsumerd did not start");
889 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
896 * Fork and exec a kernel consumer daemon (kconsumerd).
898 * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel
899 * file descriptor using the libkernelctl or kernel-ctl functions. So, a
900 * kernel consumer MUST only be spawned before creating a kernel session.
902 * Return pid if successful else -1.
904 static pid_t
spawn_kconsumerd(void)
909 DBG("Spawning kconsumerd");
916 execlp("ltt-kconsumerd", "ltt-kconsumerd", "--verbose", NULL
);
918 perror("kernel start consumer exec");
921 } else if (pid
> 0) {
925 perror("kernel start consumer fork");
937 * Spawn the kconsumerd daemon and session daemon thread.
939 static int start_kconsumerd(void)
943 pthread_mutex_lock(&kconsumerd_pid_mutex
);
944 if (kconsumerd_pid
!= 0) {
945 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
949 ret
= spawn_kconsumerd();
951 ERR("Spawning kconsumerd failed");
952 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
953 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
957 /* Setting up the global kconsumerd_pid */
958 kconsumerd_pid
= ret
;
959 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
961 DBG("Kconsumerd pid %d", ret
);
963 DBG("Spawning kconsumerd thread");
964 ret
= spawn_kconsumerd_thread();
966 ERR("Fatal error spawning kconsumerd thread");
978 * modprobe_kernel_modules
980 static int modprobe_kernel_modules(void)
985 while (kernel_modules_list
[i
] != NULL
) {
986 ret
= snprintf(modprobe
, sizeof(modprobe
), "/sbin/modprobe %s",
987 kernel_modules_list
[i
]);
989 perror("snprintf modprobe");
992 ret
= system(modprobe
);
994 ERR("Unable to load module %s", kernel_modules_list
[i
]);
996 DBG("Modprobe successfully %s", kernel_modules_list
[i
]);
1007 static int mount_debugfs(char *path
)
1010 char *type
= "debugfs";
1012 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
);
1017 ret
= mount(type
, path
, type
, 0, NULL
);
1019 perror("mount debugfs");
1023 DBG("Mounted debugfs successfully at %s", path
);
1030 * init_kernel_tracer
1032 * Setup necessary data for kernel tracer action.
1034 static void init_kernel_tracer(void)
1037 char *proc_mounts
= "/proc/mounts";
1039 char *debugfs_path
= NULL
, *lttng_path
;
1042 /* Detect debugfs */
1043 fp
= fopen(proc_mounts
, "r");
1045 ERR("Unable to probe %s", proc_mounts
);
1049 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1050 if (strstr(line
, "debugfs") != NULL
) {
1051 /* Remove first string */
1053 /* Dup string here so we can reuse line later on */
1054 debugfs_path
= strdup(strtok(NULL
, " "));
1055 DBG("Got debugfs path : %s", debugfs_path
);
1062 /* Mount debugfs if needded */
1063 if (debugfs_path
== NULL
) {
1064 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1066 perror("asprintf debugfs path");
1069 ret
= mount_debugfs(debugfs_path
);
1075 /* Modprobe lttng kernel modules */
1076 ret
= modprobe_kernel_modules();
1081 /* Setup lttng kernel path */
1082 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1084 perror("asprintf lttng path");
1088 /* Open debugfs lttng */
1089 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1090 if (kernel_tracer_fd
< 0) {
1091 DBG("Failed to open %s", lttng_path
);
1097 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1107 WARN("No kernel tracer available");
1108 kernel_tracer_fd
= 0;
1113 * start_kernel_trace
1115 * Start tracing by creating trace directory and sending FDs to the kernel
1118 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1122 if (session
->kconsumer_fds_sent
== 0) {
1123 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, session
);
1125 ERR("Send kconsumerd fds failed");
1126 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1130 session
->kconsumer_fds_sent
= 1;
1138 * Notify kernel thread to update it's pollfd.
1140 static int notify_kernel_pollfd(void)
1144 /* Inform kernel thread of the new kernel channel */
1145 ret
= write(kernel_poll_pipe
[1], "!", 1);
1147 perror("write kernel poll pipe");
1154 * init_default_channel
1156 * Allocate a channel structure and fill it.
1158 static struct lttng_channel
*init_default_channel(void)
1160 struct lttng_channel
*chan
;
1162 chan
= malloc(sizeof(struct lttng_channel
));
1164 perror("init channel malloc");
1168 if (snprintf(chan
->name
, NAME_MAX
, DEFAULT_CHANNEL_NAME
) < 0) {
1169 perror("snprintf defautl channel name");
1173 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1174 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
1175 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
1176 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1177 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1178 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1185 * create_kernel_session
1187 * Create a kernel tracer session then create the default channel.
1189 static int create_kernel_session(struct ltt_session
*session
)
1192 struct lttng_channel
*chan
;
1194 DBG("Creating kernel session");
1196 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1198 ret
= LTTCOMM_KERN_SESS_FAIL
;
1202 chan
= init_default_channel();
1204 ret
= LTTCOMM_FATAL
;
1208 ret
= mkdir_recursive(session
->path
, S_IRWXU
| S_IRWXG
);
1210 if (ret
!= EEXIST
) {
1211 ERR("Trace directory creation error");
1216 DBG("Creating default kernel channel %s", DEFAULT_CHANNEL_NAME
);
1218 ret
= kernel_create_channel(session
->kernel_session
, chan
, session
->path
);
1220 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1224 ret
= notify_kernel_pollfd();
1231 * Using the session list, filled a lttng_session array to send back to the
1232 * client for session listing.
1234 * The session list lock MUST be acquired before calling this function. Use
1235 * lock_session_list() and unlock_session_list().
1237 static void list_lttng_sessions(struct lttng_session
*sessions
)
1240 struct ltt_session
*session
;
1242 DBG("Getting all available session");
1244 * Iterate over session list and append data after the control struct in
1247 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1248 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1249 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1255 * process_client_msg
1257 * Process the command requested by the lttng client within the command
1258 * context structure. This function make sure that the return structure (llm)
1259 * is set and ready for transmission before returning.
1261 * Return any error encountered or 0 for success.
1263 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1267 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1269 /* Listing commands don't need a session */
1270 switch (cmd_ctx
->lsm
->cmd_type
) {
1271 case LTTNG_CREATE_SESSION
:
1272 case LTTNG_LIST_SESSIONS
:
1273 case LTTNG_LIST_EVENTS
:
1274 case LTTNG_KERNEL_LIST_EVENTS
:
1275 case LTTNG_LIST_TRACEABLE_APPS
:
1278 DBG("Getting session %s by name", cmd_ctx
->lsm
->session_name
);
1279 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session_name
);
1280 if (cmd_ctx
->session
== NULL
) {
1281 /* If session name not found */
1282 if (cmd_ctx
->lsm
->session_name
!= NULL
) {
1283 ret
= LTTCOMM_SESS_NOT_FOUND
;
1284 } else { /* If no session name specified */
1285 ret
= LTTCOMM_SELECT_SESS
;
1289 /* Acquire lock for the session */
1290 lock_session(cmd_ctx
->session
);
1296 * Check kernel command for kernel session.
1298 switch (cmd_ctx
->lsm
->cmd_type
) {
1299 case LTTNG_KERNEL_ADD_CONTEXT
:
1300 case LTTNG_KERNEL_CREATE_CHANNEL
:
1301 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
1302 case LTTNG_KERNEL_DISABLE_CHANNEL
:
1303 case LTTNG_KERNEL_DISABLE_EVENT
:
1304 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
1305 case LTTNG_KERNEL_ENABLE_CHANNEL
:
1306 case LTTNG_KERNEL_ENABLE_EVENT
:
1307 case LTTNG_KERNEL_LIST_EVENTS
:
1308 /* Kernel tracer check */
1309 if (kernel_tracer_fd
== 0) {
1310 init_kernel_tracer();
1311 if (kernel_tracer_fd
== 0) {
1312 ret
= LTTCOMM_KERN_NA
;
1317 /* Need a session for kernel command */
1318 if (cmd_ctx
->lsm
->cmd_type
!= LTTNG_KERNEL_LIST_EVENTS
&&
1319 cmd_ctx
->session
->kernel_session
== NULL
) {
1321 ret
= create_kernel_session(cmd_ctx
->session
);
1323 ret
= LTTCOMM_KERN_SESS_FAIL
;
1327 /* Start the kernel consumer daemon */
1328 if (kconsumerd_pid
== 0) {
1329 ret
= start_kconsumerd();
1337 /* Connect to ust apps if available pid */
1338 if (cmd_ctx
->lsm
->pid
> 0) {
1339 /* Connect to app using ustctl API */
1340 cmd_ctx
->ust_sock
= ust_connect_app(cmd_ctx
->lsm
->pid
);
1341 if (cmd_ctx
->ust_sock
< 0) {
1342 ret
= LTTCOMM_NO_TRACEABLE
;
1347 /* Process by command type */
1348 switch (cmd_ctx
->lsm
->cmd_type
) {
1349 case LTTNG_KERNEL_ADD_CONTEXT
:
1351 int found
= 0, no_event
= 0;
1352 struct ltt_kernel_channel
*chan
;
1353 struct ltt_kernel_event
*event
;
1355 /* Setup lttng message with no payload */
1356 ret
= setup_lttng_msg(cmd_ctx
, 0);
1361 /* Check if event name is given */
1362 if (strlen(cmd_ctx
->lsm
->u
.context
.event_name
) == 0) {
1366 if (strlen(cmd_ctx
->lsm
->u
.context
.channel_name
) == 0) {
1367 /* Go over all channels */
1368 DBG("Adding context to all channels");
1369 cds_list_for_each_entry(chan
,
1370 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1372 ret
= kernel_add_channel_context(chan
,
1373 &cmd_ctx
->lsm
->u
.context
.ctx
);
1378 event
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.context
.event_name
, chan
);
1379 if (event
!= NULL
) {
1380 ret
= kernel_add_event_context(event
,
1381 &cmd_ctx
->lsm
->u
.context
.ctx
);
1383 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1392 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.context
.channel_name
,
1393 cmd_ctx
->session
->kernel_session
);
1395 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1400 ret
= kernel_add_channel_context(chan
,
1401 &cmd_ctx
->lsm
->u
.context
.ctx
);
1403 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1407 event
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.context
.event_name
, chan
);
1408 if (event
!= NULL
) {
1409 ret
= kernel_add_event_context(event
,
1410 &cmd_ctx
->lsm
->u
.context
.ctx
);
1412 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1419 if (!found
&& !no_event
) {
1420 ret
= LTTCOMM_NO_EVENT
;
1427 case LTTNG_KERNEL_CREATE_CHANNEL
:
1429 /* Setup lttng message with no payload */
1430 ret
= setup_lttng_msg(cmd_ctx
, 0);
1436 DBG("Creating kernel channel");
1438 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1439 &cmd_ctx
->lsm
->u
.channel
.chan
, cmd_ctx
->session
->path
);
1441 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1445 ret
= notify_kernel_pollfd();
1447 ret
= LTTCOMM_FATAL
;
1454 case LTTNG_KERNEL_DISABLE_CHANNEL
:
1456 struct ltt_kernel_channel
*chan
;
1458 /* Setup lttng message with no payload */
1459 ret
= setup_lttng_msg(cmd_ctx
, 0);
1464 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1465 cmd_ctx
->session
->kernel_session
);
1467 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1469 } else if (chan
->enabled
== 1) {
1470 ret
= kernel_disable_channel(chan
);
1472 if (ret
!= EEXIST
) {
1473 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1479 kernel_wait_quiescent(kernel_tracer_fd
);
1483 case LTTNG_KERNEL_DISABLE_EVENT
:
1485 struct ltt_kernel_channel
*chan
;
1486 struct ltt_kernel_event
*ev
;
1488 /* Setup lttng message with no payload */
1489 ret
= setup_lttng_msg(cmd_ctx
, 0);
1494 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1495 cmd_ctx
->session
->kernel_session
);
1497 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1501 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, chan
);
1503 DBG("Disabling kernel event %s for channel %s.",
1504 cmd_ctx
->lsm
->u
.disable
.name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
1505 ret
= kernel_disable_event(ev
);
1507 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1512 kernel_wait_quiescent(kernel_tracer_fd
);
1516 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
1518 struct ltt_kernel_channel
*chan
;
1519 struct ltt_kernel_event
*ev
;
1521 /* Setup lttng message with no payload */
1522 ret
= setup_lttng_msg(cmd_ctx
, 0);
1527 DBG("Disabling all enabled kernel events");
1529 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1530 cmd_ctx
->session
->kernel_session
);
1532 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1536 /* For each event in the kernel session */
1537 cds_list_for_each_entry(ev
, &chan
->events_list
.head
, list
) {
1538 DBG("Disabling kernel event %s for channel %s.",
1539 ev
->event
->name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
1540 ret
= kernel_disable_event(ev
);
1546 /* Quiescent wait after event disable */
1547 kernel_wait_quiescent(kernel_tracer_fd
);
1551 case LTTNG_KERNEL_ENABLE_CHANNEL
:
1553 struct ltt_kernel_channel
*chan
;
1555 /* Setup lttng message with no payload */
1556 ret
= setup_lttng_msg(cmd_ctx
, 0);
1561 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1562 cmd_ctx
->session
->kernel_session
);
1564 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1566 } else if (chan
->enabled
== 0) {
1567 ret
= kernel_enable_channel(chan
);
1569 if (ret
!= EEXIST
) {
1570 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1576 kernel_wait_quiescent(kernel_tracer_fd
);
1580 case LTTNG_KERNEL_ENABLE_EVENT
:
1582 struct ltt_kernel_channel
*chan
;
1583 struct ltt_kernel_event
*ev
;
1585 /* Setup lttng message with no payload */
1586 ret
= setup_lttng_msg(cmd_ctx
, 0);
1591 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1592 cmd_ctx
->session
->kernel_session
);
1594 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1598 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
);
1600 DBG("Creating kernel event %s for channel %s.",
1601 cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
->channel
->name
);
1602 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, chan
);
1604 DBG("Enabling kernel event %s for channel %s.",
1605 cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
->channel
->name
);
1606 ret
= kernel_enable_event(ev
);
1610 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1614 kernel_wait_quiescent(kernel_tracer_fd
);
1618 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
1621 char *event_list
, *event
, *ptr
;
1622 struct ltt_kernel_channel
*chan
;
1623 struct ltt_kernel_event
*ev
;
1624 struct lttng_event ev_attr
;
1626 /* Setup lttng message with no payload */
1627 ret
= setup_lttng_msg(cmd_ctx
, 0);
1632 DBG("Enabling all kernel event");
1634 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1635 cmd_ctx
->session
->kernel_session
);
1637 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1641 /* For each event in the kernel session */
1642 cds_list_for_each_entry(ev
, &chan
->events_list
.head
, list
) {
1643 DBG("Enabling kernel event %s for channel %s.",
1644 ev
->event
->name
, chan
->channel
->name
);
1645 ret
= kernel_enable_event(ev
);
1651 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1653 ret
= LTTCOMM_KERN_LIST_FAIL
;
1658 while ((size
= sscanf(ptr
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
1659 ev
= get_kernel_event_by_name(event
, chan
);
1661 strncpy(ev_attr
.name
, event
, LTTNG_SYM_NAME_LEN
);
1662 /* Default event type for enable all */
1663 ev_attr
.type
= LTTNG_EVENT_TRACEPOINTS
;
1664 /* Enable each single tracepoint event */
1665 ret
= kernel_create_event(&ev_attr
, chan
);
1667 /* Ignore error here and continue */
1671 /* Move pointer to the next line */
1678 /* Quiescent wait after event enable */
1679 kernel_wait_quiescent(kernel_tracer_fd
);
1683 case LTTNG_KERNEL_LIST_EVENTS
:
1688 DBG("Listing kernel events");
1690 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1692 ret
= LTTCOMM_KERN_LIST_FAIL
;
1697 * Setup lttng message with payload size set to the event list size in
1698 * bytes and then copy list into the llm payload.
1700 ret
= setup_lttng_msg(cmd_ctx
, size
);
1705 /* Copy event list into message payload */
1706 memcpy(cmd_ctx
->llm
->payload
, event_list
, size
);
1713 case LTTNG_START_TRACE
:
1715 struct ltt_kernel_channel
*chan
;
1717 /* Setup lttng message with no payload */
1718 ret
= setup_lttng_msg(cmd_ctx
, 0);
1723 /* Kernel tracing */
1724 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1725 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
1726 DBG("Open kernel metadata");
1727 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
1728 cmd_ctx
->session
->path
);
1730 ret
= LTTCOMM_KERN_META_FAIL
;
1735 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1736 DBG("Opening kernel metadata stream");
1737 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1738 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
1740 ERR("Kernel create metadata stream failed");
1741 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1747 /* For each channel */
1748 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1749 if (chan
->stream_count
== 0) {
1750 ret
= kernel_open_channel_stream(chan
);
1752 ERR("Kernel create channel stream failed");
1753 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1756 /* Update the stream global counter */
1757 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
1761 DBG("Start kernel tracing");
1762 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
1764 ERR("Kernel start session failed");
1765 ret
= LTTCOMM_KERN_START_FAIL
;
1769 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
1771 ret
= LTTCOMM_KERN_START_FAIL
;
1775 /* Quiescent wait after starting trace */
1776 kernel_wait_quiescent(kernel_tracer_fd
);
1779 /* TODO: Start all UST traces */
1784 case LTTNG_STOP_TRACE
:
1786 struct ltt_kernel_channel
*chan
;
1787 /* Setup lttng message with no payload */
1788 ret
= setup_lttng_msg(cmd_ctx
, 0);
1794 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1795 DBG("Stop kernel tracing");
1797 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
1799 ERR("Kernel metadata flush failed");
1802 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1803 ret
= kernel_flush_buffer(chan
);
1805 ERR("Kernel flush buffer error");
1809 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
1811 ERR("Kernel stop session failed");
1812 ret
= LTTCOMM_KERN_STOP_FAIL
;
1816 /* Quiescent wait after stopping trace */
1817 kernel_wait_quiescent(kernel_tracer_fd
);
1820 /* TODO : User-space tracer */
1825 case LTTNG_CREATE_SESSION
:
1827 /* Setup lttng message with no payload */
1828 ret
= setup_lttng_msg(cmd_ctx
, 0);
1833 ret
= create_session(cmd_ctx
->lsm
->session_name
, cmd_ctx
->lsm
->path
);
1835 if (ret
== -EEXIST
) {
1836 ret
= LTTCOMM_EXIST_SESS
;
1838 ret
= LTTCOMM_FATAL
;
1846 case LTTNG_DESTROY_SESSION
:
1848 /* Setup lttng message with no payload */
1849 ret
= setup_lttng_msg(cmd_ctx
, 0);
1854 /* Clean kernel session teardown */
1855 teardown_kernel_session(cmd_ctx
->session
);
1857 ret
= destroy_session(cmd_ctx
->lsm
->session_name
);
1859 ret
= LTTCOMM_FATAL
;
1864 * Must notify the kernel thread here to update it's pollfd in order to
1865 * remove the channel(s)' fd just destroyed.
1867 ret
= notify_kernel_pollfd();
1869 ret
= LTTCOMM_FATAL
;
1877 case LTTNG_LIST_TRACES:
1879 unsigned int trace_count;
1881 trace_count = get_trace_count_per_session(cmd_ctx->session);
1882 if (trace_count == 0) {
1883 ret = LTTCOMM_NO_TRACE;
1887 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count);
1892 get_traces_per_session(cmd_ctx->session,
1893 (struct lttng_trace *)(cmd_ctx->llm->payload));
1900 case UST_CREATE_TRACE:
1902 ret = setup_lttng_msg(cmd_ctx, 0);
1907 ret = ust_create_trace(cmd_ctx);
1914 case LTTNG_LIST_TRACEABLE_APPS
:
1916 unsigned int app_count
;
1918 app_count
= get_app_count();
1919 DBG("Traceable application count : %d", app_count
);
1920 if (app_count
== 0) {
1921 ret
= LTTCOMM_NO_APPS
;
1925 ret
= setup_lttng_msg(cmd_ctx
, sizeof(pid_t
) * app_count
);
1930 get_app_list_pids((pid_t
*)(cmd_ctx
->llm
->payload
));
1936 case UST_START_TRACE:
1938 ret = setup_lttng_msg(cmd_ctx, 0);
1943 ret = ust_start_trace(cmd_ctx);
1949 case UST_STOP_TRACE:
1951 ret = setup_lttng_msg(cmd_ctx, 0);
1956 ret = ust_stop_trace(cmd_ctx);
1963 case LTTNG_LIST_SESSIONS
:
1965 lock_session_list();
1967 if (session_list_ptr
->count
== 0) {
1968 ret
= LTTCOMM_NO_SESSION
;
1972 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
1973 session_list_ptr
->count
);
1978 /* Filled the session array */
1979 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
1981 unlock_session_list();
1987 /* Undefined command */
1988 ret
= setup_lttng_msg(cmd_ctx
, 0);
1997 /* Set return code */
1998 cmd_ctx
->llm
->ret_code
= ret
;
2000 if (cmd_ctx
->session
) {
2001 unlock_session(cmd_ctx
->session
);
2007 if (cmd_ctx
->llm
== NULL
) {
2008 DBG("Missing llm structure. Allocating one.");
2009 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2013 /* Notify client of error */
2014 cmd_ctx
->llm
->ret_code
= ret
;
2017 if (cmd_ctx
->session
) {
2018 unlock_session(cmd_ctx
->session
);
2024 * thread_manage_clients
2026 * This thread manage all clients request using the unix
2027 * client socket for communication.
2029 static void *thread_manage_clients(void *data
)
2032 struct command_ctx
*cmd_ctx
= NULL
;
2033 struct pollfd pollfd
[2];
2035 DBG("[thread] Manage client started");
2037 ret
= lttcomm_listen_unix_sock(client_sock
);
2042 /* First fd is always the quit pipe */
2043 pollfd
[0].fd
= thread_quit_pipe
[0];
2046 pollfd
[1].fd
= client_sock
;
2047 pollfd
[1].events
= POLLIN
;
2049 /* Notify parent pid that we are ready
2050 * to accept command for client side.
2052 if (opt_sig_parent
) {
2053 kill(ppid
, SIGCHLD
);
2057 DBG("Accepting client command ...");
2059 /* Inifinite blocking call, waiting for transmission */
2060 ret
= poll(pollfd
, 2, -1);
2062 perror("poll client thread");
2066 /* Thread quit pipe has been closed. Killing thread. */
2067 if (pollfd
[0].revents
== POLLNVAL
) {
2069 } else if (pollfd
[1].revents
== POLLERR
) {
2070 ERR("Client socket poll error");
2074 sock
= lttcomm_accept_unix_sock(client_sock
);
2079 /* Allocate context command to process the client request */
2080 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2082 /* Allocate data buffer for reception */
2083 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2084 cmd_ctx
->llm
= NULL
;
2085 cmd_ctx
->session
= NULL
;
2088 * Data is received from the lttng client. The struct
2089 * lttcomm_session_msg (lsm) contains the command and data request of
2092 DBG("Receiving data from client ...");
2093 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2098 // TODO: Validate cmd_ctx including sanity check for security purpose.
2101 * This function dispatch the work to the kernel or userspace tracer
2102 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2103 * informations for the client. The command context struct contains
2104 * everything this function may needs.
2106 ret
= process_client_msg(cmd_ctx
);
2108 /* TODO: Inform client somehow of the fatal error. At this point,
2109 * ret < 0 means that a malloc failed (ENOMEM). */
2110 /* Error detected but still accept command */
2111 clean_command_ctx(&cmd_ctx
);
2115 DBG("Sending response (size: %d, retcode: %d)",
2116 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2117 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2119 ERR("Failed to send data back to client");
2122 clean_command_ctx(&cmd_ctx
);
2124 /* End of transmission */
2129 DBG("Client thread dying");
2137 unlink(client_unix_sock_path
);
2139 clean_command_ctx(&cmd_ctx
);
2145 * usage function on stderr
2147 static void usage(void)
2149 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2150 fprintf(stderr
, " -h, --help Display this usage.\n");
2151 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2152 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2153 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2154 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2155 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2156 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2157 fprintf(stderr
, " -V, --version Show version number.\n");
2158 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2159 fprintf(stderr
, " -q, --quiet No output at all.\n");
2160 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2164 * daemon argument parsing
2166 static int parse_args(int argc
, char **argv
)
2170 static struct option long_options
[] = {
2171 { "client-sock", 1, 0, 'c' },
2172 { "apps-sock", 1, 0, 'a' },
2173 { "kconsumerd-cmd-sock", 1, 0, 0 },
2174 { "kconsumerd-err-sock", 1, 0, 0 },
2175 { "daemonize", 0, 0, 'd' },
2176 { "sig-parent", 0, 0, 'S' },
2177 { "help", 0, 0, 'h' },
2178 { "group", 1, 0, 'g' },
2179 { "version", 0, 0, 'V' },
2180 { "quiet", 0, 0, 'q' },
2181 { "verbose", 0, 0, 'v' },
2186 int option_index
= 0;
2187 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
2194 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2196 fprintf(stderr
, " with arg %s\n", optarg
);
2200 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2203 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2209 opt_tracing_group
= strdup(optarg
);
2215 fprintf(stdout
, "%s\n", VERSION
);
2221 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2224 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2233 /* Unknown option or other error.
2234 * Error is printed by getopt, just return */
2243 * init_daemon_socket
2245 * Creates the two needed socket by the daemon.
2246 * apps_sock - The communication socket for all UST apps.
2247 * client_sock - The communication of the cli tool (lttng).
2249 static int init_daemon_socket()
2254 old_umask
= umask(0);
2256 /* Create client tool unix socket */
2257 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2258 if (client_sock
< 0) {
2259 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2264 /* File permission MUST be 660 */
2265 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2267 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2272 /* Create the application unix socket */
2273 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2274 if (apps_sock
< 0) {
2275 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2280 /* File permission MUST be 666 */
2281 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2283 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2294 * check_existing_daemon
2296 * Check if the global socket is available.
2297 * If yes, error is returned.
2299 static int check_existing_daemon()
2303 ret
= access(client_unix_sock_path
, F_OK
);
2305 ret
= access(apps_unix_sock_path
, F_OK
);
2314 * Set the tracing group gid onto the client socket.
2316 * Race window between mkdir and chown is OK because we are going from
2317 * more permissive (root.root) to les permissive (root.tracing).
2319 static int set_permissions(void)
2324 /* Decide which group name to use */
2325 (opt_tracing_group
!= NULL
) ?
2326 (grp
= getgrnam(opt_tracing_group
)) :
2327 (grp
= getgrnam(default_tracing_group
));
2331 WARN("No tracing group detected");
2334 ERR("Missing tracing group. Aborting execution.");
2340 /* Set lttng run dir */
2341 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
2343 ERR("Unable to set group on " LTTNG_RUNDIR
);
2347 /* lttng client socket path */
2348 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
2350 ERR("Unable to set group on %s", client_unix_sock_path
);
2354 /* kconsumerd error socket path */
2355 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
2357 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2361 DBG("All permissions are set");
2368 * create_kernel_poll_pipe
2370 * Create the pipe used to wake up the kernel thread.
2372 static int create_kernel_poll_pipe(void)
2374 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2378 * create_lttng_rundir
2380 * Create the lttng run directory needed for all
2381 * global sockets and pipe.
2383 static int create_lttng_rundir(void)
2387 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2389 if (errno
!= EEXIST
) {
2390 ERR("Unable to create " LTTNG_RUNDIR
);
2402 * set_kconsumerd_sockets
2404 * Setup sockets and directory needed by the kconsumerd
2405 * communication with the session daemon.
2407 static int set_kconsumerd_sockets(void)
2411 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
2412 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
2415 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
2416 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
2419 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
2421 if (errno
!= EEXIST
) {
2422 ERR("Failed to create " KCONSUMERD_PATH
);
2428 /* Create the kconsumerd error unix socket */
2429 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
2430 if (kconsumerd_err_sock
< 0) {
2431 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
2436 /* File permission MUST be 660 */
2437 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2439 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
2451 * Signal handler for the daemon
2453 static void sighandler(int sig
)
2457 DBG("SIGPIPE catched");
2460 DBG("SIGINT catched");
2464 DBG("SIGTERM catched");
2475 * set_signal_handler
2477 * Setup signal handler for :
2478 * SIGINT, SIGTERM, SIGPIPE
2480 static int set_signal_handler(void)
2483 struct sigaction sa
;
2486 if ((ret
= sigemptyset(&sigset
)) < 0) {
2487 perror("sigemptyset");
2491 sa
.sa_handler
= sighandler
;
2492 sa
.sa_mask
= sigset
;
2494 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
2495 perror("sigaction");
2499 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
2500 perror("sigaction");
2504 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
2505 perror("sigaction");
2509 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2517 * Set open files limit to unlimited. This daemon can open a large number of
2518 * file descriptors in order to consumer multiple kernel traces.
2520 static void set_ulimit(void)
2525 /* The kernel does not allowed an infinite limit for open files */
2526 lim
.rlim_cur
= 65535;
2527 lim
.rlim_max
= 65535;
2529 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
2531 perror("failed to set open files limit");
2538 int main(int argc
, char **argv
)
2542 const char *home_path
;
2544 /* Create thread quit pipe */
2545 if (init_thread_quit_pipe() < 0) {
2549 /* Parse arguments */
2551 if ((ret
= parse_args(argc
, argv
) < 0)) {
2564 /* Check if daemon is UID = 0 */
2565 is_root
= !getuid();
2568 ret
= create_lttng_rundir();
2573 if (strlen(apps_unix_sock_path
) == 0) {
2574 snprintf(apps_unix_sock_path
, PATH_MAX
,
2575 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
2578 if (strlen(client_unix_sock_path
) == 0) {
2579 snprintf(client_unix_sock_path
, PATH_MAX
,
2580 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
2583 home_path
= get_home_dir();
2584 if (home_path
== NULL
) {
2585 /* TODO: Add --socket PATH option */
2586 ERR("Can't get HOME directory for sockets creation.");
2590 if (strlen(apps_unix_sock_path
) == 0) {
2591 snprintf(apps_unix_sock_path
, PATH_MAX
,
2592 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
2595 /* Set the cli tool unix socket path */
2596 if (strlen(client_unix_sock_path
) == 0) {
2597 snprintf(client_unix_sock_path
, PATH_MAX
,
2598 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
2602 DBG("Client socket path %s", client_unix_sock_path
);
2603 DBG("Application socket path %s", apps_unix_sock_path
);
2606 * See if daemon already exist. If any of the two socket needed by the
2607 * daemon are present, this test fails. However, if the daemon is killed
2608 * with a SIGKILL, those unix socket must be unlinked by hand.
2610 if ((ret
= check_existing_daemon()) == 0) {
2611 ERR("Already running daemon.\n");
2613 * We do not goto error because we must not cleanup() because a daemon
2614 * is already running.
2619 /* After this point, we can safely call cleanup() so goto error is used */
2622 * These actions must be executed as root. We do that *after* setting up
2623 * the sockets path because we MUST make the check for another daemon using
2624 * those paths *before* trying to set the kernel consumer sockets and init
2628 ret
= set_kconsumerd_sockets();
2633 /* Setup kernel tracer */
2634 init_kernel_tracer();
2636 /* Set ulimit for open files */
2640 if (set_signal_handler() < 0) {
2644 /* Setup the needed unix socket */
2645 if (init_daemon_socket() < 0) {
2649 /* Set credentials to socket */
2650 if (is_root
&& (set_permissions() < 0)) {
2654 /* Get parent pid if -S, --sig-parent is specified. */
2655 if (opt_sig_parent
) {
2659 /* Setup the kernel pipe for waking up the kernel thread */
2660 if (create_kernel_poll_pipe() < 0) {
2665 * Get session list pointer. This pointer MUST NOT be free().
2666 * This list is statically declared in session.c
2668 session_list_ptr
= get_session_list();
2671 /* Create thread to manage the client socket */
2672 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
2674 perror("pthread_create");
2678 /* Create thread to manage application socket */
2679 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
2681 perror("pthread_create");
2685 /* Create kernel thread to manage kernel event */
2686 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
2688 perror("pthread_create");
2692 ret
= pthread_join(client_thread
, &status
);
2694 perror("pthread_join");