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];
93 /* Pthread, Mutexes and Semaphores */
94 static pthread_t kconsumerd_thread
;
95 static pthread_t apps_thread
;
96 static pthread_t client_thread
;
97 static pthread_t kernel_thread
;
98 static sem_t kconsumerd_sem
;
100 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
103 * Pointer initialized before thread creation.
105 * This points to the tracing session list containing the session count and a
106 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
107 * MUST NOT be taken if you call a public function in session.c.
109 * The lock is nested inside the structure: session_list_ptr->lock.
111 static struct ltt_session_list
*session_list_ptr
;
114 * teardown_kernel_session
116 * Complete teardown of a kernel session. This free all data structure related
117 * to a kernel session and update counter.
119 static void teardown_kernel_session(struct ltt_session
*session
)
121 if (session
->kernel_session
!= NULL
) {
122 DBG("Tearing down kernel session");
123 trace_destroy_kernel_session(session
->kernel_session
);
124 /* Extra precaution */
125 session
->kernel_session
= NULL
;
132 * Cleanup the daemon on exit
134 static void cleanup()
138 struct ltt_session
*sess
;
143 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
144 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
147 /* Stopping all threads */
148 DBG("Terminating all threads");
149 pthread_cancel(client_thread
);
150 pthread_cancel(apps_thread
);
151 pthread_cancel(kernel_thread
);
152 if (kconsumerd_pid
!= 0) {
153 pthread_cancel(kconsumerd_thread
);
156 DBG("Unlinking all unix socket");
157 unlink(client_unix_sock_path
);
158 unlink(apps_unix_sock_path
);
159 unlink(kconsumerd_err_unix_sock_path
);
161 DBG("Removing %s directory", LTTNG_RUNDIR
);
162 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
164 ERR("asprintf failed. Something is really wrong!");
167 /* Remove lttng run directory */
170 ERR("Unable to clean " LTTNG_RUNDIR
);
173 DBG("Cleaning up all session");
174 /* Cleanup ALL session */
175 cds_list_for_each_entry(sess
, &session_list_ptr
->head
, list
) {
176 teardown_kernel_session(sess
);
177 // TODO complete session cleanup (including UST)
180 /* Destroy session list mutex */
181 pthread_mutex_destroy(&session_list_ptr
->lock
);
183 DBG("Closing kernel fd");
184 close(kernel_tracer_fd
);
185 close(kernel_poll_pipe
[0]);
186 close(kernel_poll_pipe
[1]);
192 * Send data on a unix socket using the liblttsessiondcomm API.
194 * Return lttcomm error code.
196 static int send_unix_sock(int sock
, void *buf
, size_t len
)
198 /* Check valid length */
203 return lttcomm_send_unix_sock(sock
, buf
, len
);
209 * Free memory of a command context structure.
211 static void clean_command_ctx(struct command_ctx
*cmd_ctx
)
213 DBG("Clean command context structure %p", cmd_ctx
);
227 * send_kconsumerd_channel_fds
229 * Send all stream fds of kernel channel to the consumer.
231 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
235 struct ltt_kernel_stream
*stream
;
236 struct lttcomm_kconsumerd_header lkh
;
237 struct lttcomm_kconsumerd_msg lkm
;
239 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
241 nb_fd
= channel
->stream_count
;
244 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
245 lkh
.cmd_type
= ADD_STREAM
;
247 DBG("Sending kconsumerd header");
249 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
251 perror("send kconsumerd header");
255 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
256 if (stream
->fd
!= 0) {
258 lkm
.state
= stream
->state
;
259 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
260 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
262 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
264 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
266 perror("send kconsumerd fd");
272 DBG("Kconsumerd channel fds sent");
281 * send_kconsumerd_fds
283 * Send all stream fds of the kernel session to the consumer.
285 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
288 struct ltt_kernel_channel
*chan
;
289 struct lttcomm_kconsumerd_header lkh
;
290 struct lttcomm_kconsumerd_msg lkm
;
293 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
294 lkh
.cmd_type
= ADD_STREAM
;
296 DBG("Sending kconsumerd header for metadata");
298 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
300 perror("send kconsumerd header");
304 DBG("Sending metadata stream fd");
306 if (session
->metadata_stream_fd
!= 0) {
307 /* Send metadata stream fd first */
308 lkm
.fd
= session
->metadata_stream_fd
;
309 lkm
.state
= ACTIVE_FD
;
310 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
311 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
313 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
315 perror("send kconsumerd fd");
320 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
321 ret
= send_kconsumerd_channel_fds(sock
, chan
);
327 DBG("Kconsumerd fds (metadata and channel streams) sent");
338 * Return a socket connected to the libust communication socket
339 * of the application identified by the pid.
341 * If the pid is not found in the traceable list,
342 * return -1 to indicate error.
344 static int ust_connect_app(pid_t pid
)
347 struct ltt_traceable_app
*lta
;
349 DBG("Connect to application pid %d", pid
);
351 lta
= find_app_by_pid(pid
);
354 DBG("Application pid %d not found", pid
);
358 sock
= ustctl_connect_pid(lta
->pid
);
360 ERR("Fail connecting to the PID %d", pid
);
369 * Notify apps by writing 42 to a named pipe using name.
370 * Every applications waiting for a ltt-sessiond will be notified
371 * and re-register automatically to the session daemon.
373 * Return open or write error value.
375 static int notify_apps(const char *name
)
380 DBG("Notify the global application pipe");
382 /* Try opening the global pipe */
383 fd
= open(name
, O_WRONLY
);
388 /* Notify by writing on the pipe */
389 ret
= write(fd
, "42", 2);
401 * Setup the outgoing data buffer for the response (llm) by allocating the
402 * right amount of memory and copying the original information from the lsm
405 * Return total size of the buffer pointed by buf.
407 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
413 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
414 if (cmd_ctx
->llm
== NULL
) {
420 /* Copy common data */
421 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
422 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->pid
;
424 cmd_ctx
->llm
->data_size
= size
;
425 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
434 * update_kernel_pollfd
436 * Update the kernel pollfd set of all channel fd available over
437 * all tracing session. Add the wakeup pipe at the end of the set.
439 static int update_kernel_pollfd(void)
442 unsigned int nb_fd
= 1;
443 struct ltt_session
*session
;
444 struct ltt_kernel_channel
*channel
;
446 DBG("Updating kernel_pollfd");
448 /* Get the number of channel of all kernel session */
449 pthread_mutex_lock(&session_list_ptr
->lock
);
450 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
451 lock_session(session
);
452 if (session
->kernel_session
== NULL
) {
453 unlock_session(session
);
456 nb_fd
+= session
->kernel_session
->channel_count
;
457 unlock_session(session
);
460 DBG("Resizing kernel_pollfd to size %d", nb_fd
);
462 kernel_pollfd
= realloc(kernel_pollfd
, nb_fd
* sizeof(struct pollfd
));
463 if (kernel_pollfd
== NULL
) {
464 perror("malloc kernel_pollfd");
468 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
469 lock_session(session
);
470 if (session
->kernel_session
== NULL
) {
471 unlock_session(session
);
475 ERR("To much channel for kernel_pollfd size");
476 unlock_session(session
);
479 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
480 kernel_pollfd
[i
].fd
= channel
->fd
;
481 kernel_pollfd
[i
].events
= POLLIN
| POLLRDNORM
;
484 unlock_session(session
);
486 pthread_mutex_unlock(&session_list_ptr
->lock
);
488 /* Adding wake up pipe */
489 kernel_pollfd
[nb_fd
- 1].fd
= kernel_poll_pipe
[0];
490 kernel_pollfd
[nb_fd
- 1].events
= POLLIN
;
495 pthread_mutex_unlock(&session_list_ptr
->lock
);
500 * update_kernel_stream
502 * Find the channel fd from 'fd' over all tracing session. When found, check
503 * for new channel stream and send those stream fds to the kernel consumer.
505 * Useful for CPU hotplug feature.
507 static int update_kernel_stream(int fd
)
510 struct ltt_session
*session
;
511 struct ltt_kernel_channel
*channel
;
513 DBG("Updating kernel streams for channel fd %d", fd
);
515 pthread_mutex_lock(&session_list_ptr
->lock
);
516 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
517 lock_session(session
);
518 if (session
->kernel_session
== NULL
) {
519 unlock_session(session
);
522 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
523 if (channel
->fd
== fd
) {
524 DBG("Channel found, updating kernel streams");
525 ret
= kernel_open_channel_stream(channel
);
530 * Have we already sent fds to the consumer? If yes, it means that
531 * tracing is started so it is safe to send our updated stream fds.
533 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
534 ret
= send_kconsumerd_channel_fds(kconsumerd_cmd_sock
, channel
);
542 unlock_session(session
);
547 unlock_session(session
);
549 pthread_mutex_unlock(&session_list_ptr
->lock
);
554 * thread_manage_kernel
556 * This thread manage event coming from the kernel.
558 * Features supported in this thread:
561 static void *thread_manage_kernel(void *data
)
563 int ret
, i
, nb_fd
= 0;
565 int update_poll_flag
= 1;
567 DBG("Thread manage kernel started");
570 if (update_poll_flag
== 1) {
571 nb_fd
= update_kernel_pollfd();
575 update_poll_flag
= 0;
578 DBG("Polling on %d fds", nb_fd
);
580 /* Poll infinite value of time */
581 ret
= poll(kernel_pollfd
, nb_fd
, -1);
583 perror("poll kernel thread");
585 } else if (ret
== 0) {
586 /* Should not happen since timeout is infinite */
590 DBG("Kernel poll event triggered");
593 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
596 switch (kernel_pollfd
[nb_fd
- 1].revents
) {
598 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
599 update_poll_flag
= 1;
607 for (i
= 0; i
< nb_fd
; i
++) {
608 switch (kernel_pollfd
[i
].revents
) {
610 * New CPU detected by the kernel. Adding kernel stream to kernel
611 * session and updating the kernel consumer
613 case POLLIN
| POLLRDNORM
:
614 ret
= update_kernel_stream(kernel_pollfd
[i
].fd
);
624 DBG("Kernel thread dying");
632 * thread_manage_kconsumerd
634 * This thread manage the kconsumerd error sent
635 * back to the session daemon.
637 static void *thread_manage_kconsumerd(void *data
)
640 enum lttcomm_return_code code
;
642 DBG("[thread] Manage kconsumerd started");
644 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
649 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
654 /* Getting status code from kconsumerd */
655 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
660 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
661 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
662 if (kconsumerd_cmd_sock
< 0) {
663 sem_post(&kconsumerd_sem
);
664 perror("kconsumerd connect");
667 /* Signal condition to tell that the kconsumerd is ready */
668 sem_post(&kconsumerd_sem
);
669 DBG("Kconsumerd command socket ready");
671 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
672 lttcomm_get_readable_code(-code
));
676 /* Wait for any kconsumerd error */
677 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
679 ERR("Kconsumerd closed the command socket");
683 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
687 DBG("Kconsumerd thread dying");
694 * This thread manage the application socket communication
696 static void *thread_manage_apps(void *data
)
700 /* TODO: Something more elegant is needed but fine for now */
701 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
702 * for 32-bit vs 64-bit compat processes. */
703 /* replicate in ust with version number */
705 int reg
; /* 1:register, 0:unregister */
710 DBG("[thread] Manage apps started");
712 ret
= lttcomm_listen_unix_sock(apps_sock
);
717 /* Notify all applications to register */
718 notify_apps(default_global_apps_pipe
);
721 DBG("Accepting application registration");
722 /* Blocking call, waiting for transmission */
723 sock
= lttcomm_accept_unix_sock(apps_sock
);
728 /* Basic recv here to handle the very simple data
729 * that the libust send to register (reg_msg).
731 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
737 /* Add application to the global traceable list */
738 if (reg_msg
.reg
== 1) {
740 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
742 /* register_traceable_app only return an error with
743 * ENOMEM. At this point, we better stop everything.
749 unregister_traceable_app(reg_msg
.pid
);
759 * spawn_kconsumerd_thread
761 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
762 * exec or it will fails.
764 static int spawn_kconsumerd_thread(void)
768 /* Setup semaphore */
769 sem_init(&kconsumerd_sem
, 0, 0);
771 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
773 perror("pthread_create kconsumerd");
777 /* Wait for the kconsumerd thread to be ready */
778 sem_wait(&kconsumerd_sem
);
780 if (kconsumerd_pid
== 0) {
781 ERR("Kconsumerd did not start");
788 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
795 * Fork and exec a kernel consumer daemon (kconsumerd).
797 * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel
798 * file descriptor using the libkernelctl or kernel-ctl functions. So, a
799 * kernel consumer MUST only be spawned before creating a kernel session.
801 * Return pid if successful else -1.
803 static pid_t
spawn_kconsumerd(void)
808 DBG("Spawning kconsumerd");
815 execlp("ltt-kconsumerd", "ltt-kconsumerd", "--verbose", NULL
);
817 perror("kernel start consumer exec");
820 } else if (pid
> 0) {
824 perror("kernel start consumer fork");
836 * Spawn the kconsumerd daemon and session daemon thread.
838 static int start_kconsumerd(void)
842 pthread_mutex_lock(&kconsumerd_pid_mutex
);
843 if (kconsumerd_pid
!= 0) {
844 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
848 ret
= spawn_kconsumerd();
850 ERR("Spawning kconsumerd failed");
851 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
852 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
856 /* Setting up the global kconsumerd_pid */
857 kconsumerd_pid
= ret
;
858 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
860 DBG("Kconsumerd pid %d", ret
);
862 DBG("Spawning kconsumerd thread");
863 ret
= spawn_kconsumerd_thread();
865 ERR("Fatal error spawning kconsumerd thread");
877 * modprobe_kernel_modules
879 static int modprobe_kernel_modules(void)
884 while (kernel_modules_list
[i
] != NULL
) {
885 ret
= snprintf(modprobe
, sizeof(modprobe
), "/sbin/modprobe %s",
886 kernel_modules_list
[i
]);
888 perror("snprintf modprobe");
891 ret
= system(modprobe
);
893 ERR("Unable to load module %s", kernel_modules_list
[i
]);
895 DBG("Modprobe successfully %s", kernel_modules_list
[i
]);
906 static int mount_debugfs(char *path
)
909 char *type
= "debugfs";
911 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
);
916 ret
= mount(type
, path
, type
, 0, NULL
);
918 perror("mount debugfs");
922 DBG("Mounted debugfs successfully at %s", path
);
931 * Setup necessary data for kernel tracer action.
933 static void init_kernel_tracer(void)
936 char *proc_mounts
= "/proc/mounts";
938 char *debugfs_path
= NULL
, *lttng_path
;
942 fp
= fopen(proc_mounts
, "r");
944 ERR("Unable to probe %s", proc_mounts
);
948 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
949 if (strstr(line
, "debugfs") != NULL
) {
950 /* Remove first string */
952 /* Dup string here so we can reuse line later on */
953 debugfs_path
= strdup(strtok(NULL
, " "));
954 DBG("Got debugfs path : %s", debugfs_path
);
961 /* Mount debugfs if needded */
962 if (debugfs_path
== NULL
) {
963 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
965 perror("asprintf debugfs path");
968 ret
= mount_debugfs(debugfs_path
);
974 /* Modprobe lttng kernel modules */
975 ret
= modprobe_kernel_modules();
980 /* Setup lttng kernel path */
981 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
983 perror("asprintf lttng path");
987 /* Open debugfs lttng */
988 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
989 if (kernel_tracer_fd
< 0) {
990 DBG("Failed to open %s", lttng_path
);
996 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1006 WARN("No kernel tracer available");
1007 kernel_tracer_fd
= 0;
1012 * start_kernel_trace
1014 * Start tracing by creating trace directory and sending FDs to the kernel
1017 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1021 if (session
->kconsumer_fds_sent
== 0) {
1022 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, session
);
1024 ERR("Send kconsumerd fds failed");
1025 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1029 session
->kconsumer_fds_sent
= 1;
1037 * Notify kernel thread to update it's pollfd.
1039 static int notify_kernel_pollfd(void)
1043 /* Inform kernel thread of the new kernel channel */
1044 ret
= write(kernel_poll_pipe
[1], "!", 1);
1046 perror("write kernel poll pipe");
1053 * init_default_channel
1055 * Allocate a channel structure and fill it.
1057 static struct lttng_channel
*init_default_channel(void)
1059 struct lttng_channel
*chan
;
1061 chan
= malloc(sizeof(struct lttng_channel
));
1063 perror("init channel malloc");
1067 if (snprintf(chan
->name
, NAME_MAX
, DEFAULT_CHANNEL_NAME
) < 0) {
1068 perror("snprintf defautl channel name");
1072 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1073 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
1074 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
1075 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1076 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1077 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1084 * create_kernel_session
1086 * Create a kernel tracer session then create the default channel.
1088 static int create_kernel_session(struct ltt_session
*session
)
1091 struct lttng_channel
*chan
;
1093 DBG("Creating kernel session");
1095 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1097 ret
= LTTCOMM_KERN_SESS_FAIL
;
1101 chan
= init_default_channel();
1103 ret
= LTTCOMM_FATAL
;
1107 ret
= mkdir_recursive(session
->path
, S_IRWXU
| S_IRWXG
);
1109 if (ret
!= EEXIST
) {
1110 ERR("Trace directory creation error");
1115 DBG("Creating default kernel channel %s", DEFAULT_CHANNEL_NAME
);
1117 ret
= kernel_create_channel(session
->kernel_session
, chan
, session
->path
);
1119 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1123 ret
= notify_kernel_pollfd();
1130 * process_client_msg
1132 * Process the command requested by the lttng client within the command
1133 * context structure. This function make sure that the return structure (llm)
1134 * is set and ready for transmission before returning.
1136 * Return any error encountered or 0 for success.
1138 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1142 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1144 /* Listing commands don't need a session */
1145 switch (cmd_ctx
->lsm
->cmd_type
) {
1146 case LTTNG_CREATE_SESSION
:
1147 case LTTNG_LIST_SESSIONS
:
1148 case LTTNG_LIST_EVENTS
:
1149 case LTTNG_KERNEL_LIST_EVENTS
:
1150 case LTTNG_LIST_TRACEABLE_APPS
:
1153 DBG("Getting session %s by name", cmd_ctx
->lsm
->session_name
);
1154 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session_name
);
1155 if (cmd_ctx
->session
== NULL
) {
1156 /* If session name not found */
1157 if (cmd_ctx
->lsm
->session_name
!= NULL
) {
1158 ret
= LTTCOMM_SESS_NOT_FOUND
;
1159 } else { /* If no session name specified */
1160 ret
= LTTCOMM_SELECT_SESS
;
1164 /* Acquire lock for the session */
1165 lock_session(cmd_ctx
->session
);
1171 * Check kernel command for kernel session.
1173 switch (cmd_ctx
->lsm
->cmd_type
) {
1174 case LTTNG_KERNEL_ADD_CONTEXT
:
1175 case LTTNG_KERNEL_CREATE_CHANNEL
:
1176 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
1177 case LTTNG_KERNEL_DISABLE_CHANNEL
:
1178 case LTTNG_KERNEL_DISABLE_EVENT
:
1179 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
1180 case LTTNG_KERNEL_ENABLE_CHANNEL
:
1181 case LTTNG_KERNEL_ENABLE_EVENT
:
1182 case LTTNG_KERNEL_LIST_EVENTS
:
1183 /* Kernel tracer check */
1184 if (kernel_tracer_fd
== 0) {
1185 init_kernel_tracer();
1186 if (kernel_tracer_fd
== 0) {
1187 ret
= LTTCOMM_KERN_NA
;
1192 /* Need a session for kernel command */
1193 if (cmd_ctx
->lsm
->cmd_type
!= LTTNG_KERNEL_LIST_EVENTS
&&
1194 cmd_ctx
->session
->kernel_session
== NULL
) {
1196 ret
= create_kernel_session(cmd_ctx
->session
);
1198 ret
= LTTCOMM_KERN_SESS_FAIL
;
1202 /* Start the kernel consumer daemon */
1203 if (kconsumerd_pid
== 0) {
1204 ret
= start_kconsumerd();
1212 /* Connect to ust apps if available pid */
1213 if (cmd_ctx
->lsm
->pid
> 0) {
1214 /* Connect to app using ustctl API */
1215 cmd_ctx
->ust_sock
= ust_connect_app(cmd_ctx
->lsm
->pid
);
1216 if (cmd_ctx
->ust_sock
< 0) {
1217 ret
= LTTCOMM_NO_TRACEABLE
;
1222 /* Process by command type */
1223 switch (cmd_ctx
->lsm
->cmd_type
) {
1224 case LTTNG_KERNEL_ADD_CONTEXT
:
1226 int found
= 0, no_event
= 0;
1227 struct ltt_kernel_channel
*chan
;
1228 struct ltt_kernel_event
*event
;
1230 /* Setup lttng message with no payload */
1231 ret
= setup_lttng_msg(cmd_ctx
, 0);
1236 /* Check if event name is given */
1237 if (strlen(cmd_ctx
->lsm
->u
.context
.event_name
) == 0) {
1241 if (strlen(cmd_ctx
->lsm
->u
.context
.channel_name
) == 0) {
1242 /* Go over all channels */
1243 DBG("Adding context to all channels");
1244 cds_list_for_each_entry(chan
,
1245 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1247 ret
= kernel_add_channel_context(chan
,
1248 &cmd_ctx
->lsm
->u
.context
.ctx
);
1253 event
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.context
.event_name
, chan
);
1254 if (event
!= NULL
) {
1255 ret
= kernel_add_event_context(event
,
1256 &cmd_ctx
->lsm
->u
.context
.ctx
);
1258 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1267 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.context
.channel_name
,
1268 cmd_ctx
->session
->kernel_session
);
1270 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1275 ret
= kernel_add_channel_context(chan
,
1276 &cmd_ctx
->lsm
->u
.context
.ctx
);
1278 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1282 event
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.context
.event_name
, chan
);
1283 if (event
!= NULL
) {
1284 ret
= kernel_add_event_context(event
,
1285 &cmd_ctx
->lsm
->u
.context
.ctx
);
1287 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1294 if (!found
&& !no_event
) {
1295 ret
= LTTCOMM_NO_EVENT
;
1302 case LTTNG_KERNEL_CREATE_CHANNEL
:
1304 /* Setup lttng message with no payload */
1305 ret
= setup_lttng_msg(cmd_ctx
, 0);
1311 DBG("Creating kernel channel");
1313 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1314 &cmd_ctx
->lsm
->u
.channel
.chan
, cmd_ctx
->session
->path
);
1316 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1320 ret
= notify_kernel_pollfd();
1322 ret
= LTTCOMM_FATAL
;
1329 case LTTNG_KERNEL_DISABLE_CHANNEL
:
1331 struct ltt_kernel_channel
*chan
;
1333 /* Setup lttng message with no payload */
1334 ret
= setup_lttng_msg(cmd_ctx
, 0);
1339 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1340 cmd_ctx
->session
->kernel_session
);
1342 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1344 } else if (chan
->enabled
== 1) {
1345 ret
= kernel_disable_channel(chan
);
1347 if (ret
!= EEXIST
) {
1348 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1354 kernel_wait_quiescent(kernel_tracer_fd
);
1358 case LTTNG_KERNEL_DISABLE_EVENT
:
1360 struct ltt_kernel_channel
*chan
;
1361 struct ltt_kernel_event
*ev
;
1363 /* Setup lttng message with no payload */
1364 ret
= setup_lttng_msg(cmd_ctx
, 0);
1369 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1370 cmd_ctx
->session
->kernel_session
);
1372 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1376 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, chan
);
1378 DBG("Disabling kernel event %s for channel %s.",
1379 cmd_ctx
->lsm
->u
.disable
.name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
1380 ret
= kernel_disable_event(ev
);
1382 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1387 kernel_wait_quiescent(kernel_tracer_fd
);
1391 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
1393 struct ltt_kernel_channel
*chan
;
1394 struct ltt_kernel_event
*ev
;
1396 /* Setup lttng message with no payload */
1397 ret
= setup_lttng_msg(cmd_ctx
, 0);
1402 DBG("Disabling all enabled kernel events");
1404 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1405 cmd_ctx
->session
->kernel_session
);
1407 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1411 /* For each event in the kernel session */
1412 cds_list_for_each_entry(ev
, &chan
->events_list
.head
, list
) {
1413 DBG("Disabling kernel event %s for channel %s.",
1414 ev
->event
->name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
1415 ret
= kernel_disable_event(ev
);
1421 /* Quiescent wait after event disable */
1422 kernel_wait_quiescent(kernel_tracer_fd
);
1426 case LTTNG_KERNEL_ENABLE_CHANNEL
:
1428 struct ltt_kernel_channel
*chan
;
1430 /* Setup lttng message with no payload */
1431 ret
= setup_lttng_msg(cmd_ctx
, 0);
1436 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1437 cmd_ctx
->session
->kernel_session
);
1439 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1441 } else if (chan
->enabled
== 0) {
1442 ret
= kernel_enable_channel(chan
);
1444 if (ret
!= EEXIST
) {
1445 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1451 kernel_wait_quiescent(kernel_tracer_fd
);
1455 case LTTNG_KERNEL_ENABLE_EVENT
:
1457 struct ltt_kernel_channel
*chan
;
1458 struct ltt_kernel_event
*ev
;
1460 /* Setup lttng message with no payload */
1461 ret
= setup_lttng_msg(cmd_ctx
, 0);
1466 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1467 cmd_ctx
->session
->kernel_session
);
1469 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1473 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
);
1475 DBG("Creating kernel event %s for channel %s.",
1476 cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
->channel
->name
);
1477 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, chan
);
1479 DBG("Enabling kernel event %s for channel %s.",
1480 cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
->channel
->name
);
1481 ret
= kernel_enable_event(ev
);
1485 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1489 kernel_wait_quiescent(kernel_tracer_fd
);
1493 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
1496 char *event_list
, *event
, *ptr
;
1497 struct ltt_kernel_channel
*chan
;
1498 struct ltt_kernel_event
*ev
;
1499 struct lttng_event ev_attr
;
1501 /* Setup lttng message with no payload */
1502 ret
= setup_lttng_msg(cmd_ctx
, 0);
1507 DBG("Enabling all kernel event");
1509 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1510 cmd_ctx
->session
->kernel_session
);
1512 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1516 /* For each event in the kernel session */
1517 cds_list_for_each_entry(ev
, &chan
->events_list
.head
, list
) {
1518 DBG("Enabling kernel event %s for channel %s.",
1519 ev
->event
->name
, chan
->channel
->name
);
1520 ret
= kernel_enable_event(ev
);
1526 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1528 ret
= LTTCOMM_KERN_LIST_FAIL
;
1533 while ((size
= sscanf(ptr
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
1534 ev
= get_kernel_event_by_name(event
, chan
);
1536 strncpy(ev_attr
.name
, event
, LTTNG_SYM_NAME_LEN
);
1537 /* Default event type for enable all */
1538 ev_attr
.type
= LTTNG_EVENT_TRACEPOINTS
;
1539 /* Enable each single tracepoint event */
1540 ret
= kernel_create_event(&ev_attr
, chan
);
1542 /* Ignore error here and continue */
1546 /* Move pointer to the next line */
1553 /* Quiescent wait after event enable */
1554 kernel_wait_quiescent(kernel_tracer_fd
);
1558 case LTTNG_KERNEL_LIST_EVENTS
:
1563 DBG("Listing kernel events");
1565 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1567 ret
= LTTCOMM_KERN_LIST_FAIL
;
1572 * Setup lttng message with payload size set to the event list size in
1573 * bytes and then copy list into the llm payload.
1575 ret
= setup_lttng_msg(cmd_ctx
, size
);
1580 /* Copy event list into message payload */
1581 memcpy(cmd_ctx
->llm
->payload
, event_list
, size
);
1588 case LTTNG_START_TRACE
:
1590 struct ltt_kernel_channel
*chan
;
1592 /* Setup lttng message with no payload */
1593 ret
= setup_lttng_msg(cmd_ctx
, 0);
1598 /* Kernel tracing */
1599 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1600 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
1601 DBG("Open kernel metadata");
1602 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
1603 cmd_ctx
->session
->path
);
1605 ret
= LTTCOMM_KERN_META_FAIL
;
1610 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1611 DBG("Opening kernel metadata stream");
1612 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1613 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
1615 ERR("Kernel create metadata stream failed");
1616 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1622 /* For each channel */
1623 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1624 if (chan
->stream_count
== 0) {
1625 ret
= kernel_open_channel_stream(chan
);
1627 ERR("Kernel create channel stream failed");
1628 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1631 /* Update the stream global counter */
1632 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
1636 DBG("Start kernel tracing");
1637 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
1639 ERR("Kernel start session failed");
1640 ret
= LTTCOMM_KERN_START_FAIL
;
1644 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
1646 ret
= LTTCOMM_KERN_START_FAIL
;
1650 /* Quiescent wait after starting trace */
1651 kernel_wait_quiescent(kernel_tracer_fd
);
1654 /* TODO: Start all UST traces */
1659 case LTTNG_STOP_TRACE
:
1661 struct ltt_kernel_channel
*chan
;
1662 /* Setup lttng message with no payload */
1663 ret
= setup_lttng_msg(cmd_ctx
, 0);
1669 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1670 DBG("Stop kernel tracing");
1672 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
1674 ERR("Kernel metadata flush failed");
1677 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1678 ret
= kernel_flush_buffer(chan
);
1680 ERR("Kernel flush buffer error");
1684 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
1686 ERR("Kernel stop session failed");
1687 ret
= LTTCOMM_KERN_STOP_FAIL
;
1691 /* Quiescent wait after stopping trace */
1692 kernel_wait_quiescent(kernel_tracer_fd
);
1695 /* TODO : User-space tracer */
1700 case LTTNG_CREATE_SESSION
:
1702 /* Setup lttng message with no payload */
1703 ret
= setup_lttng_msg(cmd_ctx
, 0);
1708 ret
= create_session(cmd_ctx
->lsm
->session_name
, cmd_ctx
->lsm
->path
);
1710 if (ret
== -EEXIST
) {
1711 ret
= LTTCOMM_EXIST_SESS
;
1713 ret
= LTTCOMM_FATAL
;
1721 case LTTNG_DESTROY_SESSION
:
1723 /* Setup lttng message with no payload */
1724 ret
= setup_lttng_msg(cmd_ctx
, 0);
1729 /* Clean kernel session teardown */
1730 teardown_kernel_session(cmd_ctx
->session
);
1732 ret
= destroy_session(cmd_ctx
->lsm
->session_name
);
1734 ret
= LTTCOMM_FATAL
;
1739 * Must notify the kernel thread here to update it's pollfd in order to
1740 * remove the channel(s)' fd just destroyed.
1742 ret
= notify_kernel_pollfd();
1744 ret
= LTTCOMM_FATAL
;
1752 case LTTNG_LIST_TRACES:
1754 unsigned int trace_count;
1756 trace_count = get_trace_count_per_session(cmd_ctx->session);
1757 if (trace_count == 0) {
1758 ret = LTTCOMM_NO_TRACE;
1762 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count);
1767 get_traces_per_session(cmd_ctx->session,
1768 (struct lttng_trace *)(cmd_ctx->llm->payload));
1775 case UST_CREATE_TRACE:
1777 ret = setup_lttng_msg(cmd_ctx, 0);
1782 ret = ust_create_trace(cmd_ctx);
1789 case LTTNG_LIST_TRACEABLE_APPS
:
1791 unsigned int app_count
;
1793 app_count
= get_app_count();
1794 DBG("Traceable application count : %d", app_count
);
1795 if (app_count
== 0) {
1796 ret
= LTTCOMM_NO_APPS
;
1800 ret
= setup_lttng_msg(cmd_ctx
, sizeof(pid_t
) * app_count
);
1805 get_app_list_pids((pid_t
*)(cmd_ctx
->llm
->payload
));
1811 case UST_START_TRACE:
1813 ret = setup_lttng_msg(cmd_ctx, 0);
1818 ret = ust_start_trace(cmd_ctx);
1824 case UST_STOP_TRACE:
1826 ret = setup_lttng_msg(cmd_ctx, 0);
1831 ret = ust_stop_trace(cmd_ctx);
1838 case LTTNG_LIST_SESSIONS
:
1840 unsigned int session_count
;
1842 session_count
= get_session_count();
1843 if (session_count
== 0) {
1844 ret
= LTTCOMM_NO_SESSION
;
1848 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * session_count
);
1853 get_lttng_session((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
1859 /* Undefined command */
1860 ret
= setup_lttng_msg(cmd_ctx
, 0);
1869 /* Set return code */
1870 cmd_ctx
->llm
->ret_code
= ret
;
1872 if (cmd_ctx
->session
) {
1873 unlock_session(cmd_ctx
->session
);
1879 if (cmd_ctx
->llm
== NULL
) {
1880 DBG("Missing llm structure. Allocating one.");
1881 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
1885 /* Notify client of error */
1886 cmd_ctx
->llm
->ret_code
= ret
;
1889 if (cmd_ctx
->session
) {
1890 unlock_session(cmd_ctx
->session
);
1896 * thread_manage_clients
1898 * This thread manage all clients request using the unix
1899 * client socket for communication.
1901 static void *thread_manage_clients(void *data
)
1904 struct command_ctx
*cmd_ctx
;
1906 DBG("[thread] Manage client started");
1908 ret
= lttcomm_listen_unix_sock(client_sock
);
1913 /* Notify parent pid that we are ready
1914 * to accept command for client side.
1916 if (opt_sig_parent
) {
1917 kill(ppid
, SIGCHLD
);
1921 /* Blocking call, waiting for transmission */
1922 DBG("Accepting client command ...");
1923 sock
= lttcomm_accept_unix_sock(client_sock
);
1928 /* Allocate context command to process the client request */
1929 cmd_ctx
= malloc(sizeof(struct command_ctx
));
1931 /* Allocate data buffer for reception */
1932 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
1933 cmd_ctx
->llm
= NULL
;
1934 cmd_ctx
->session
= NULL
;
1937 * Data is received from the lttng client. The struct
1938 * lttcomm_session_msg (lsm) contains the command and data request of
1941 DBG("Receiving data from client ...");
1942 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
1947 // TODO: Validate cmd_ctx including sanity check for security purpose.
1950 * This function dispatch the work to the kernel or userspace tracer
1951 * libs and fill the lttcomm_lttng_msg data structure of all the needed
1952 * informations for the client. The command context struct contains
1953 * everything this function may needs.
1955 ret
= process_client_msg(cmd_ctx
);
1957 /* TODO: Inform client somehow of the fatal error. At this point,
1958 * ret < 0 means that a malloc failed (ENOMEM). */
1959 /* Error detected but still accept command */
1960 clean_command_ctx(cmd_ctx
);
1964 DBG("Sending response (size: %d, retcode: %d)",
1965 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
1966 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
1968 ERR("Failed to send data back to client");
1971 clean_command_ctx(cmd_ctx
);
1973 /* End of transmission */
1983 * usage function on stderr
1985 static void usage(void)
1987 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
1988 fprintf(stderr
, " -h, --help Display this usage.\n");
1989 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
1990 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
1991 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
1992 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
1993 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
1994 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
1995 fprintf(stderr
, " -V, --version Show version number.\n");
1996 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
1997 fprintf(stderr
, " -q, --quiet No output at all.\n");
1998 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2002 * daemon argument parsing
2004 static int parse_args(int argc
, char **argv
)
2008 static struct option long_options
[] = {
2009 { "client-sock", 1, 0, 'c' },
2010 { "apps-sock", 1, 0, 'a' },
2011 { "kconsumerd-cmd-sock", 1, 0, 0 },
2012 { "kconsumerd-err-sock", 1, 0, 0 },
2013 { "daemonize", 0, 0, 'd' },
2014 { "sig-parent", 0, 0, 'S' },
2015 { "help", 0, 0, 'h' },
2016 { "group", 1, 0, 'g' },
2017 { "version", 0, 0, 'V' },
2018 { "quiet", 0, 0, 'q' },
2019 { "verbose", 0, 0, 'v' },
2024 int option_index
= 0;
2025 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
2032 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2034 fprintf(stderr
, " with arg %s\n", optarg
);
2038 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2041 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2047 opt_tracing_group
= strdup(optarg
);
2053 fprintf(stdout
, "%s\n", VERSION
);
2059 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2062 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2071 /* Unknown option or other error.
2072 * Error is printed by getopt, just return */
2081 * init_daemon_socket
2083 * Creates the two needed socket by the daemon.
2084 * apps_sock - The communication socket for all UST apps.
2085 * client_sock - The communication of the cli tool (lttng).
2087 static int init_daemon_socket()
2092 old_umask
= umask(0);
2094 /* Create client tool unix socket */
2095 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2096 if (client_sock
< 0) {
2097 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2102 /* File permission MUST be 660 */
2103 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2105 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2110 /* Create the application unix socket */
2111 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2112 if (apps_sock
< 0) {
2113 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2118 /* File permission MUST be 666 */
2119 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2121 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2132 * check_existing_daemon
2134 * Check if the global socket is available.
2135 * If yes, error is returned.
2137 static int check_existing_daemon()
2141 ret
= access(client_unix_sock_path
, F_OK
);
2143 ret
= access(apps_unix_sock_path
, F_OK
);
2152 * Set the tracing group gid onto the client socket.
2154 * Race window between mkdir and chown is OK because we are going from
2155 * more permissive (root.root) to les permissive (root.tracing).
2157 static int set_permissions(void)
2162 /* Decide which group name to use */
2163 (opt_tracing_group
!= NULL
) ?
2164 (grp
= getgrnam(opt_tracing_group
)) :
2165 (grp
= getgrnam(default_tracing_group
));
2169 WARN("No tracing group detected");
2172 ERR("Missing tracing group. Aborting execution.");
2178 /* Set lttng run dir */
2179 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
2181 ERR("Unable to set group on " LTTNG_RUNDIR
);
2185 /* lttng client socket path */
2186 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
2188 ERR("Unable to set group on %s", client_unix_sock_path
);
2192 /* kconsumerd error socket path */
2193 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
2195 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2199 DBG("All permissions are set");
2206 * create_kernel_poll_pipe
2208 * Create the pipe used to wake up the kernel thread.
2210 static int create_kernel_poll_pipe(void)
2212 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2216 * create_lttng_rundir
2218 * Create the lttng run directory needed for all
2219 * global sockets and pipe.
2221 static int create_lttng_rundir(void)
2225 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2227 if (errno
!= EEXIST
) {
2228 ERR("Unable to create " LTTNG_RUNDIR
);
2240 * set_kconsumerd_sockets
2242 * Setup sockets and directory needed by the kconsumerd
2243 * communication with the session daemon.
2245 static int set_kconsumerd_sockets(void)
2249 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
2250 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
2253 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
2254 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
2257 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
2259 if (errno
!= EEXIST
) {
2260 ERR("Failed to create " KCONSUMERD_PATH
);
2266 /* Create the kconsumerd error unix socket */
2267 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
2268 if (kconsumerd_err_sock
< 0) {
2269 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
2274 /* File permission MUST be 660 */
2275 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2277 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
2289 * Signal handler for the daemon
2291 static void sighandler(int sig
)
2295 DBG("SIGPIPE catched");
2298 DBG("SIGINT catched");
2302 DBG("SIGTERM catched");
2313 * set_signal_handler
2315 * Setup signal handler for :
2316 * SIGINT, SIGTERM, SIGPIPE
2318 static int set_signal_handler(void)
2321 struct sigaction sa
;
2324 if ((ret
= sigemptyset(&sigset
)) < 0) {
2325 perror("sigemptyset");
2329 sa
.sa_handler
= sighandler
;
2330 sa
.sa_mask
= sigset
;
2332 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
2333 perror("sigaction");
2337 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
2338 perror("sigaction");
2342 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
2343 perror("sigaction");
2347 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2355 * Set open files limit to unlimited. This daemon can open a large number of
2356 * file descriptors in order to consumer multiple kernel traces.
2358 static void set_ulimit(void)
2363 lim
.rlim_cur
= 65535;
2364 lim
.rlim_max
= 65535;
2366 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
2368 perror("failed to set open files limit");
2375 int main(int argc
, char **argv
)
2379 const char *home_path
;
2381 /* Parse arguments */
2383 if ((ret
= parse_args(argc
, argv
) < 0)) {
2396 /* Check if daemon is UID = 0 */
2397 is_root
= !getuid();
2400 ret
= create_lttng_rundir();
2405 if (strlen(apps_unix_sock_path
) == 0) {
2406 snprintf(apps_unix_sock_path
, PATH_MAX
,
2407 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
2410 if (strlen(client_unix_sock_path
) == 0) {
2411 snprintf(client_unix_sock_path
, PATH_MAX
,
2412 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
2415 ret
= set_kconsumerd_sockets();
2420 /* Setup kernel tracer */
2421 init_kernel_tracer();
2423 /* Set ulimit for open files */
2426 home_path
= get_home_dir();
2427 if (home_path
== NULL
) {
2428 ERR("Can't get HOME directory for sockets creation.\n \
2429 Please specify --socket PATH.");
2433 if (strlen(apps_unix_sock_path
) == 0) {
2434 snprintf(apps_unix_sock_path
, PATH_MAX
,
2435 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
2438 /* Set the cli tool unix socket path */
2439 if (strlen(client_unix_sock_path
) == 0) {
2440 snprintf(client_unix_sock_path
, PATH_MAX
,
2441 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
2445 DBG("Client socket path %s", client_unix_sock_path
);
2446 DBG("Application socket path %s", apps_unix_sock_path
);
2448 /* See if daemon already exist. If any of the two
2449 * socket needed by the daemon are present, this test fails
2451 if ((ret
= check_existing_daemon()) == 0) {
2452 ERR("Already running daemon.\n");
2453 /* We do not goto error because we must not
2454 * cleanup() because a daemon is already running.
2459 if (set_signal_handler() < 0) {
2463 /* Setup the needed unix socket */
2464 if (init_daemon_socket() < 0) {
2468 /* Set credentials to socket */
2469 if (is_root
&& (set_permissions() < 0)) {
2473 /* Get parent pid if -S, --sig-parent is specified. */
2474 if (opt_sig_parent
) {
2478 /* Setup the kernel pipe for waking up the kernel thread */
2479 if (create_kernel_poll_pipe() < 0) {
2483 /* Get session list pointer */
2484 session_list_ptr
= get_session_list();
2487 /* Create thread to manage the client socket */
2488 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
2490 perror("pthread_create");
2494 /* Create thread to manage application socket */
2495 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
2497 perror("pthread_create");
2501 /* Create kernel thread to manage kernel event */
2502 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
2504 perror("pthread_create");
2508 ret
= pthread_join(client_thread
, &status
);
2510 perror("pthread_join");