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 static struct ltt_session_list
*session_list_ptr
;
112 * teardown_kernel_session
114 * Complete teardown of a kernel session. This free all data structure related
115 * to a kernel session and update counter.
117 static void teardown_kernel_session(struct ltt_session
*session
)
119 if (session
->kernel_session
!= NULL
) {
120 DBG("Tearing down kernel session");
121 trace_destroy_kernel_session(session
->kernel_session
);
122 /* Extra precaution */
123 session
->kernel_session
= NULL
;
130 * Cleanup the daemon on exit
132 static void cleanup()
136 struct ltt_session
*sess
;
141 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
142 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
145 /* Stopping all threads */
146 DBG("Terminating all threads");
147 pthread_cancel(client_thread
);
148 pthread_cancel(apps_thread
);
149 pthread_cancel(kernel_thread
);
150 if (kconsumerd_pid
!= 0) {
151 pthread_cancel(kconsumerd_thread
);
154 DBG("Unlinking all unix socket");
155 unlink(client_unix_sock_path
);
156 unlink(apps_unix_sock_path
);
157 unlink(kconsumerd_err_unix_sock_path
);
159 DBG("Removing %s directory", LTTNG_RUNDIR
);
160 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
162 ERR("asprintf failed. Something is really wrong!");
165 /* Remove lttng run directory */
168 ERR("Unable to clean " LTTNG_RUNDIR
);
171 DBG("Cleaning up all session");
172 /* Cleanup ALL session */
173 cds_list_for_each_entry(sess
, &session_list_ptr
->head
, list
) {
174 teardown_kernel_session(sess
);
175 // TODO complete session cleanup (including UST)
178 /* Destroy session list mutex */
179 pthread_mutex_destroy(&session_list_ptr
->lock
);
181 DBG("Closing kernel fd");
182 close(kernel_tracer_fd
);
183 close(kernel_poll_pipe
[0]);
184 close(kernel_poll_pipe
[1]);
190 * Send data on a unix socket using the liblttsessiondcomm API.
192 * Return lttcomm error code.
194 static int send_unix_sock(int sock
, void *buf
, size_t len
)
196 /* Check valid length */
201 return lttcomm_send_unix_sock(sock
, buf
, len
);
207 * Free memory of a command context structure.
209 static void clean_command_ctx(struct command_ctx
*cmd_ctx
)
211 DBG("Clean command context structure %p", cmd_ctx
);
225 * send_kconsumerd_channel_fds
227 * Send all stream fds of kernel channel to the consumer.
229 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
233 struct ltt_kernel_stream
*stream
;
234 struct lttcomm_kconsumerd_header lkh
;
235 struct lttcomm_kconsumerd_msg lkm
;
237 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
239 nb_fd
= channel
->stream_count
;
242 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
243 lkh
.cmd_type
= ADD_STREAM
;
245 DBG("Sending kconsumerd header");
247 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
249 perror("send kconsumerd header");
253 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
254 if (stream
->fd
!= 0) {
256 lkm
.state
= stream
->state
;
257 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
258 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
260 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
262 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
264 perror("send kconsumerd fd");
270 DBG("Kconsumerd channel fds sent");
279 * send_kconsumerd_fds
281 * Send all stream fds of the kernel session to the consumer.
283 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
286 struct ltt_kernel_channel
*chan
;
287 struct lttcomm_kconsumerd_header lkh
;
288 struct lttcomm_kconsumerd_msg lkm
;
291 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
292 lkh
.cmd_type
= ADD_STREAM
;
294 DBG("Sending kconsumerd header for metadata");
296 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
298 perror("send kconsumerd header");
302 DBG("Sending metadata stream fd");
304 if (session
->metadata_stream_fd
!= 0) {
305 /* Send metadata stream fd first */
306 lkm
.fd
= session
->metadata_stream_fd
;
307 lkm
.state
= ACTIVE_FD
;
308 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
309 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
311 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
313 perror("send kconsumerd fd");
318 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
319 ret
= send_kconsumerd_channel_fds(sock
, chan
);
325 DBG("Kconsumerd fds (metadata and channel streams) sent");
336 * Return a socket connected to the libust communication socket
337 * of the application identified by the pid.
339 * If the pid is not found in the traceable list,
340 * return -1 to indicate error.
342 static int ust_connect_app(pid_t pid
)
345 struct ltt_traceable_app
*lta
;
347 DBG("Connect to application pid %d", pid
);
349 lta
= find_app_by_pid(pid
);
352 DBG("Application pid %d not found", pid
);
356 sock
= ustctl_connect_pid(lta
->pid
);
358 ERR("Fail connecting to the PID %d", pid
);
367 * Notify apps by writing 42 to a named pipe using name.
368 * Every applications waiting for a ltt-sessiond will be notified
369 * and re-register automatically to the session daemon.
371 * Return open or write error value.
373 static int notify_apps(const char *name
)
378 DBG("Notify the global application pipe");
380 /* Try opening the global pipe */
381 fd
= open(name
, O_WRONLY
);
386 /* Notify by writing on the pipe */
387 ret
= write(fd
, "42", 2);
399 * Setup the outgoing data buffer for the response (llm) by allocating the
400 * right amount of memory and copying the original information from the lsm
403 * Return total size of the buffer pointed by buf.
405 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
411 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
412 if (cmd_ctx
->llm
== NULL
) {
418 /* Copy common data */
419 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
420 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->pid
;
422 cmd_ctx
->llm
->data_size
= size
;
423 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
432 * update_kernel_pollfd
434 * Update the kernel pollfd set of all channel fd available over
435 * all tracing session. Add the wakeup pipe at the end of the set.
437 static int update_kernel_pollfd(void)
440 unsigned int nb_fd
= 1;
441 struct ltt_session
*session
;
442 struct ltt_kernel_channel
*channel
;
444 DBG("Updating kernel_pollfd");
446 /* Get the number of channel of all kernel session */
447 pthread_mutex_lock(&session_list_ptr
->lock
);
448 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
449 lock_session(session
);
450 if (session
->kernel_session
== NULL
) {
451 unlock_session(session
);
454 nb_fd
+= session
->kernel_session
->channel_count
;
455 unlock_session(session
);
458 DBG("Resizing kernel_pollfd to size %d", nb_fd
);
460 kernel_pollfd
= realloc(kernel_pollfd
, nb_fd
* sizeof(struct pollfd
));
461 if (kernel_pollfd
== NULL
) {
462 perror("malloc kernel_pollfd");
466 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
467 lock_session(session
);
468 if (session
->kernel_session
== NULL
) {
469 unlock_session(session
);
473 ERR("To much channel for kernel_pollfd size");
474 unlock_session(session
);
477 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
478 kernel_pollfd
[i
].fd
= channel
->fd
;
479 kernel_pollfd
[i
].events
= POLLIN
| POLLRDNORM
;
482 unlock_session(session
);
484 pthread_mutex_unlock(&session_list_ptr
->lock
);
486 /* Adding wake up pipe */
487 kernel_pollfd
[nb_fd
- 1].fd
= kernel_poll_pipe
[0];
488 kernel_pollfd
[nb_fd
- 1].events
= POLLIN
;
493 pthread_mutex_unlock(&session_list_ptr
->lock
);
498 * update_kernel_stream
500 * Find the channel fd from 'fd' over all tracing session. When found, check
501 * for new channel stream and send those stream fds to the kernel consumer.
503 * Useful for CPU hotplug feature.
505 static int update_kernel_stream(int fd
)
508 struct ltt_session
*session
;
509 struct ltt_kernel_channel
*channel
;
511 DBG("Updating kernel streams for channel fd %d", fd
);
513 pthread_mutex_lock(&session_list_ptr
->lock
);
514 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
515 lock_session(session
);
516 if (session
->kernel_session
== NULL
) {
517 unlock_session(session
);
520 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
521 if (channel
->fd
== fd
) {
522 DBG("Channel found, updating kernel streams");
523 ret
= kernel_open_channel_stream(channel
);
528 * Have we already sent fds to the consumer? If yes, it means that
529 * tracing is started so it is safe to send our updated stream fds.
531 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
532 ret
= send_kconsumerd_channel_fds(kconsumerd_cmd_sock
, channel
);
540 unlock_session(session
);
545 unlock_session(session
);
547 pthread_mutex_unlock(&session_list_ptr
->lock
);
552 * thread_manage_kernel
554 * This thread manage event coming from the kernel.
556 * Features supported in this thread:
559 static void *thread_manage_kernel(void *data
)
561 int ret
, i
, nb_fd
= 0;
563 int update_poll_flag
= 1;
565 DBG("Thread manage kernel started");
568 if (update_poll_flag
== 1) {
569 nb_fd
= update_kernel_pollfd();
573 update_poll_flag
= 0;
576 DBG("Polling on %d fds", nb_fd
);
578 /* Poll infinite value of time */
579 ret
= poll(kernel_pollfd
, nb_fd
, -1);
581 perror("poll kernel thread");
583 } else if (ret
== 0) {
584 /* Should not happen since timeout is infinite */
588 DBG("Kernel poll event triggered");
591 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
594 switch (kernel_pollfd
[nb_fd
- 1].revents
) {
596 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
597 update_poll_flag
= 1;
605 for (i
= 0; i
< nb_fd
; i
++) {
606 switch (kernel_pollfd
[i
].revents
) {
608 * New CPU detected by the kernel. Adding kernel stream to kernel
609 * session and updating the kernel consumer
611 case POLLIN
| POLLRDNORM
:
612 ret
= update_kernel_stream(kernel_pollfd
[i
].fd
);
622 DBG("Kernel thread dying");
630 * thread_manage_kconsumerd
632 * This thread manage the kconsumerd error sent
633 * back to the session daemon.
635 static void *thread_manage_kconsumerd(void *data
)
638 enum lttcomm_return_code code
;
640 DBG("[thread] Manage kconsumerd started");
642 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
647 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
652 /* Getting status code from kconsumerd */
653 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
658 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
659 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
660 if (kconsumerd_cmd_sock
< 0) {
661 sem_post(&kconsumerd_sem
);
662 perror("kconsumerd connect");
665 /* Signal condition to tell that the kconsumerd is ready */
666 sem_post(&kconsumerd_sem
);
667 DBG("Kconsumerd command socket ready");
669 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
670 lttcomm_get_readable_code(-code
));
674 /* Wait for any kconsumerd error */
675 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
677 ERR("Kconsumerd closed the command socket");
681 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
685 DBG("Kconsumerd thread dying");
692 * This thread manage the application socket communication
694 static void *thread_manage_apps(void *data
)
698 /* TODO: Something more elegant is needed but fine for now */
699 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
700 * for 32-bit vs 64-bit compat processes. */
701 /* replicate in ust with version number */
703 int reg
; /* 1:register, 0:unregister */
708 DBG("[thread] Manage apps started");
710 ret
= lttcomm_listen_unix_sock(apps_sock
);
715 /* Notify all applications to register */
716 notify_apps(default_global_apps_pipe
);
719 DBG("Accepting application registration");
720 /* Blocking call, waiting for transmission */
721 sock
= lttcomm_accept_unix_sock(apps_sock
);
726 /* Basic recv here to handle the very simple data
727 * that the libust send to register (reg_msg).
729 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
735 /* Add application to the global traceable list */
736 if (reg_msg
.reg
== 1) {
738 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
740 /* register_traceable_app only return an error with
741 * ENOMEM. At this point, we better stop everything.
747 unregister_traceable_app(reg_msg
.pid
);
757 * spawn_kconsumerd_thread
759 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
760 * exec or it will fails.
762 static int spawn_kconsumerd_thread(void)
766 /* Setup semaphore */
767 sem_init(&kconsumerd_sem
, 0, 0);
769 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
771 perror("pthread_create kconsumerd");
775 /* Wait for the kconsumerd thread to be ready */
776 sem_wait(&kconsumerd_sem
);
778 if (kconsumerd_pid
== 0) {
779 ERR("Kconsumerd did not start");
786 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
793 * Fork and exec a kernel consumer daemon (kconsumerd).
795 * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel
796 * file descriptor using the libkernelctl or kernel-ctl functions. So, a
797 * kernel consumer MUST only be spawned before creating a kernel session.
799 * Return pid if successful else -1.
801 static pid_t
spawn_kconsumerd(void)
806 DBG("Spawning kconsumerd");
813 execlp("ltt-kconsumerd", "ltt-kconsumerd", "--verbose", NULL
);
815 perror("kernel start consumer exec");
818 } else if (pid
> 0) {
822 perror("kernel start consumer fork");
834 * Spawn the kconsumerd daemon and session daemon thread.
836 static int start_kconsumerd(void)
840 pthread_mutex_lock(&kconsumerd_pid_mutex
);
841 if (kconsumerd_pid
!= 0) {
842 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
846 ret
= spawn_kconsumerd();
848 ERR("Spawning kconsumerd failed");
849 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
850 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
854 /* Setting up the global kconsumerd_pid */
855 kconsumerd_pid
= ret
;
856 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
858 DBG("Kconsumerd pid %d", ret
);
860 DBG("Spawning kconsumerd thread");
861 ret
= spawn_kconsumerd_thread();
863 ERR("Fatal error spawning kconsumerd thread");
875 * modprobe_kernel_modules
877 static int modprobe_kernel_modules(void)
882 while (kernel_modules_list
[i
] != NULL
) {
883 ret
= snprintf(modprobe
, sizeof(modprobe
), "/sbin/modprobe %s",
884 kernel_modules_list
[i
]);
886 perror("snprintf modprobe");
889 ret
= system(modprobe
);
891 ERR("Unable to load module %s", kernel_modules_list
[i
]);
893 DBG("Modprobe successfully %s", kernel_modules_list
[i
]);
904 static int mount_debugfs(char *path
)
907 char *type
= "debugfs";
909 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
);
914 ret
= mount(type
, path
, type
, 0, NULL
);
916 perror("mount debugfs");
920 DBG("Mounted debugfs successfully at %s", path
);
929 * Setup necessary data for kernel tracer action.
931 static void init_kernel_tracer(void)
934 char *proc_mounts
= "/proc/mounts";
936 char *debugfs_path
= NULL
, *lttng_path
;
940 fp
= fopen(proc_mounts
, "r");
942 ERR("Unable to probe %s", proc_mounts
);
946 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
947 if (strstr(line
, "debugfs") != NULL
) {
948 /* Remove first string */
950 /* Dup string here so we can reuse line later on */
951 debugfs_path
= strdup(strtok(NULL
, " "));
952 DBG("Got debugfs path : %s", debugfs_path
);
959 /* Mount debugfs if needded */
960 if (debugfs_path
== NULL
) {
961 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
963 perror("asprintf debugfs path");
966 ret
= mount_debugfs(debugfs_path
);
972 /* Modprobe lttng kernel modules */
973 ret
= modprobe_kernel_modules();
978 /* Setup lttng kernel path */
979 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
981 perror("asprintf lttng path");
985 /* Open debugfs lttng */
986 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
987 if (kernel_tracer_fd
< 0) {
988 DBG("Failed to open %s", lttng_path
);
994 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1004 WARN("No kernel tracer available");
1005 kernel_tracer_fd
= 0;
1010 * start_kernel_trace
1012 * Start tracing by creating trace directory and sending FDs to the kernel
1015 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1019 if (session
->kconsumer_fds_sent
== 0) {
1020 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, session
);
1022 ERR("Send kconsumerd fds failed");
1023 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1027 session
->kconsumer_fds_sent
= 1;
1035 * Notify kernel thread to update it's pollfd.
1037 static int notify_kernel_pollfd(void)
1041 /* Inform kernel thread of the new kernel channel */
1042 ret
= write(kernel_poll_pipe
[1], "!", 1);
1044 perror("write kernel poll pipe");
1051 * init_default_channel
1053 * Allocate a channel structure and fill it.
1055 static struct lttng_channel
*init_default_channel(void)
1057 struct lttng_channel
*chan
;
1059 chan
= malloc(sizeof(struct lttng_channel
));
1061 perror("init channel malloc");
1065 if (snprintf(chan
->name
, NAME_MAX
, DEFAULT_CHANNEL_NAME
) < 0) {
1066 perror("snprintf defautl channel name");
1070 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1071 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
1072 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
1073 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1074 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1075 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1082 * create_kernel_session
1084 * Create a kernel tracer session then create the default channel.
1086 static int create_kernel_session(struct ltt_session
*session
)
1089 struct lttng_channel
*chan
;
1091 DBG("Creating kernel session");
1093 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1095 ret
= LTTCOMM_KERN_SESS_FAIL
;
1099 chan
= init_default_channel();
1101 ret
= LTTCOMM_FATAL
;
1105 ret
= mkdir_recursive(session
->path
, S_IRWXU
| S_IRWXG
);
1107 if (ret
!= EEXIST
) {
1108 ERR("Trace directory creation error");
1113 DBG("Creating default kernel channel %s", DEFAULT_CHANNEL_NAME
);
1115 ret
= kernel_create_channel(session
->kernel_session
, chan
, session
->path
);
1117 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1121 ret
= notify_kernel_pollfd();
1128 * process_client_msg
1130 * Process the command requested by the lttng client within the command
1131 * context structure. This function make sure that the return structure (llm)
1132 * is set and ready for transmission before returning.
1134 * Return any error encountered or 0 for success.
1136 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1140 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1142 /* Listing commands don't need a session */
1143 switch (cmd_ctx
->lsm
->cmd_type
) {
1144 case LTTNG_CREATE_SESSION
:
1145 case LTTNG_LIST_SESSIONS
:
1146 case LTTNG_LIST_EVENTS
:
1147 case LTTNG_KERNEL_LIST_EVENTS
:
1148 case LTTNG_LIST_TRACEABLE_APPS
:
1151 DBG("Getting session %s by name", cmd_ctx
->lsm
->session_name
);
1152 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session_name
);
1153 if (cmd_ctx
->session
== NULL
) {
1154 /* If session name not found */
1155 if (cmd_ctx
->lsm
->session_name
!= NULL
) {
1156 ret
= LTTCOMM_SESS_NOT_FOUND
;
1157 } else { /* If no session name specified */
1158 ret
= LTTCOMM_SELECT_SESS
;
1162 /* Acquire lock for the session */
1163 lock_session(cmd_ctx
->session
);
1169 * Check kernel command for kernel session.
1171 switch (cmd_ctx
->lsm
->cmd_type
) {
1172 case LTTNG_KERNEL_ADD_CONTEXT
:
1173 case LTTNG_KERNEL_CREATE_CHANNEL
:
1174 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
1175 case LTTNG_KERNEL_DISABLE_CHANNEL
:
1176 case LTTNG_KERNEL_DISABLE_EVENT
:
1177 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
1178 case LTTNG_KERNEL_ENABLE_CHANNEL
:
1179 case LTTNG_KERNEL_ENABLE_EVENT
:
1180 case LTTNG_KERNEL_LIST_EVENTS
:
1181 /* Kernel tracer check */
1182 if (kernel_tracer_fd
== 0) {
1183 init_kernel_tracer();
1184 if (kernel_tracer_fd
== 0) {
1185 ret
= LTTCOMM_KERN_NA
;
1190 /* Need a session for kernel command */
1191 if (cmd_ctx
->lsm
->cmd_type
!= LTTNG_KERNEL_LIST_EVENTS
&&
1192 cmd_ctx
->session
->kernel_session
== NULL
) {
1194 ret
= create_kernel_session(cmd_ctx
->session
);
1196 ret
= LTTCOMM_KERN_SESS_FAIL
;
1200 /* Start the kernel consumer daemon */
1201 if (kconsumerd_pid
== 0) {
1202 ret
= start_kconsumerd();
1210 /* Connect to ust apps if available pid */
1211 if (cmd_ctx
->lsm
->pid
> 0) {
1212 /* Connect to app using ustctl API */
1213 cmd_ctx
->ust_sock
= ust_connect_app(cmd_ctx
->lsm
->pid
);
1214 if (cmd_ctx
->ust_sock
< 0) {
1215 ret
= LTTCOMM_NO_TRACEABLE
;
1220 /* Process by command type */
1221 switch (cmd_ctx
->lsm
->cmd_type
) {
1222 case LTTNG_KERNEL_ADD_CONTEXT
:
1224 int found
= 0, no_event
= 0;
1225 struct ltt_kernel_channel
*chan
;
1226 struct ltt_kernel_event
*event
;
1228 /* Setup lttng message with no payload */
1229 ret
= setup_lttng_msg(cmd_ctx
, 0);
1234 /* Check if event name is given */
1235 if (strlen(cmd_ctx
->lsm
->u
.context
.event_name
) == 0) {
1239 if (strlen(cmd_ctx
->lsm
->u
.context
.channel_name
) == 0) {
1240 /* Go over all channels */
1241 DBG("Adding context to all channels");
1242 cds_list_for_each_entry(chan
,
1243 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1245 ret
= kernel_add_channel_context(chan
,
1246 &cmd_ctx
->lsm
->u
.context
.ctx
);
1251 event
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.context
.event_name
, chan
);
1252 if (event
!= NULL
) {
1253 ret
= kernel_add_event_context(event
,
1254 &cmd_ctx
->lsm
->u
.context
.ctx
);
1256 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1265 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.context
.channel_name
,
1266 cmd_ctx
->session
->kernel_session
);
1268 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1273 ret
= kernel_add_channel_context(chan
,
1274 &cmd_ctx
->lsm
->u
.context
.ctx
);
1276 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1280 event
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.context
.event_name
, chan
);
1281 if (event
!= NULL
) {
1282 ret
= kernel_add_event_context(event
,
1283 &cmd_ctx
->lsm
->u
.context
.ctx
);
1285 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1292 if (!found
&& !no_event
) {
1293 ret
= LTTCOMM_NO_EVENT
;
1300 case LTTNG_KERNEL_CREATE_CHANNEL
:
1302 /* Setup lttng message with no payload */
1303 ret
= setup_lttng_msg(cmd_ctx
, 0);
1309 DBG("Creating kernel channel");
1311 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1312 &cmd_ctx
->lsm
->u
.channel
.chan
, cmd_ctx
->session
->path
);
1314 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1318 ret
= notify_kernel_pollfd();
1320 ret
= LTTCOMM_FATAL
;
1327 case LTTNG_KERNEL_DISABLE_CHANNEL
:
1329 struct ltt_kernel_channel
*chan
;
1331 /* Setup lttng message with no payload */
1332 ret
= setup_lttng_msg(cmd_ctx
, 0);
1337 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1338 cmd_ctx
->session
->kernel_session
);
1340 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1342 } else if (chan
->enabled
== 1) {
1343 ret
= kernel_disable_channel(chan
);
1345 if (ret
!= EEXIST
) {
1346 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1352 kernel_wait_quiescent(kernel_tracer_fd
);
1356 case LTTNG_KERNEL_DISABLE_EVENT
:
1358 struct ltt_kernel_channel
*chan
;
1359 struct ltt_kernel_event
*ev
;
1361 /* Setup lttng message with no payload */
1362 ret
= setup_lttng_msg(cmd_ctx
, 0);
1367 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1368 cmd_ctx
->session
->kernel_session
);
1370 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1374 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, chan
);
1376 DBG("Disabling kernel event %s for channel %s.",
1377 cmd_ctx
->lsm
->u
.disable
.name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
1378 ret
= kernel_disable_event(ev
);
1380 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1385 kernel_wait_quiescent(kernel_tracer_fd
);
1389 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
1391 struct ltt_kernel_channel
*chan
;
1392 struct ltt_kernel_event
*ev
;
1394 /* Setup lttng message with no payload */
1395 ret
= setup_lttng_msg(cmd_ctx
, 0);
1400 DBG("Disabling all enabled kernel events");
1402 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1403 cmd_ctx
->session
->kernel_session
);
1405 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1409 /* For each event in the kernel session */
1410 cds_list_for_each_entry(ev
, &chan
->events_list
.head
, list
) {
1411 DBG("Disabling kernel event %s for channel %s.",
1412 ev
->event
->name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
1413 ret
= kernel_disable_event(ev
);
1419 /* Quiescent wait after event disable */
1420 kernel_wait_quiescent(kernel_tracer_fd
);
1424 case LTTNG_KERNEL_ENABLE_CHANNEL
:
1426 struct ltt_kernel_channel
*chan
;
1428 /* Setup lttng message with no payload */
1429 ret
= setup_lttng_msg(cmd_ctx
, 0);
1434 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1435 cmd_ctx
->session
->kernel_session
);
1437 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1439 } else if (chan
->enabled
== 0) {
1440 ret
= kernel_enable_channel(chan
);
1442 if (ret
!= EEXIST
) {
1443 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1449 kernel_wait_quiescent(kernel_tracer_fd
);
1453 case LTTNG_KERNEL_ENABLE_EVENT
:
1455 struct ltt_kernel_channel
*chan
;
1456 struct ltt_kernel_event
*ev
;
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
.enable
.channel_name
,
1465 cmd_ctx
->session
->kernel_session
);
1467 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1471 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
);
1473 DBG("Creating kernel event %s for channel %s.",
1474 cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
->channel
->name
);
1475 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, chan
);
1477 DBG("Enabling kernel event %s for channel %s.",
1478 cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
->channel
->name
);
1479 ret
= kernel_enable_event(ev
);
1483 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1487 kernel_wait_quiescent(kernel_tracer_fd
);
1491 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
1494 char *event_list
, *event
, *ptr
;
1495 struct ltt_kernel_channel
*chan
;
1496 struct ltt_kernel_event
*ev
;
1497 struct lttng_event ev_attr
;
1499 /* Setup lttng message with no payload */
1500 ret
= setup_lttng_msg(cmd_ctx
, 0);
1505 DBG("Enabling all kernel event");
1507 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1508 cmd_ctx
->session
->kernel_session
);
1510 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1514 /* For each event in the kernel session */
1515 cds_list_for_each_entry(ev
, &chan
->events_list
.head
, list
) {
1516 DBG("Enabling kernel event %s for channel %s.",
1517 ev
->event
->name
, chan
->channel
->name
);
1518 ret
= kernel_enable_event(ev
);
1524 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1526 ret
= LTTCOMM_KERN_LIST_FAIL
;
1531 while ((size
= sscanf(ptr
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
1532 ev
= get_kernel_event_by_name(event
, chan
);
1534 strncpy(ev_attr
.name
, event
, LTTNG_SYM_NAME_LEN
);
1535 /* Default event type for enable all */
1536 ev_attr
.type
= LTTNG_EVENT_TRACEPOINTS
;
1537 /* Enable each single tracepoint event */
1538 ret
= kernel_create_event(&ev_attr
, chan
);
1540 /* Ignore error here and continue */
1544 /* Move pointer to the next line */
1551 /* Quiescent wait after event enable */
1552 kernel_wait_quiescent(kernel_tracer_fd
);
1556 case LTTNG_KERNEL_LIST_EVENTS
:
1561 DBG("Listing kernel events");
1563 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1565 ret
= LTTCOMM_KERN_LIST_FAIL
;
1570 * Setup lttng message with payload size set to the event list size in
1571 * bytes and then copy list into the llm payload.
1573 ret
= setup_lttng_msg(cmd_ctx
, size
);
1578 /* Copy event list into message payload */
1579 memcpy(cmd_ctx
->llm
->payload
, event_list
, size
);
1586 case LTTNG_START_TRACE
:
1588 struct ltt_kernel_channel
*chan
;
1590 /* Setup lttng message with no payload */
1591 ret
= setup_lttng_msg(cmd_ctx
, 0);
1596 /* Kernel tracing */
1597 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1598 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
1599 DBG("Open kernel metadata");
1600 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
1601 cmd_ctx
->session
->path
);
1603 ret
= LTTCOMM_KERN_META_FAIL
;
1608 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1609 DBG("Opening kernel metadata stream");
1610 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1611 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
1613 ERR("Kernel create metadata stream failed");
1614 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1620 /* For each channel */
1621 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1622 if (chan
->stream_count
== 0) {
1623 ret
= kernel_open_channel_stream(chan
);
1625 ERR("Kernel create channel stream failed");
1626 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1629 /* Update the stream global counter */
1630 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
1634 DBG("Start kernel tracing");
1635 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
1637 ERR("Kernel start session failed");
1638 ret
= LTTCOMM_KERN_START_FAIL
;
1642 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
1644 ret
= LTTCOMM_KERN_START_FAIL
;
1648 /* Quiescent wait after starting trace */
1649 kernel_wait_quiescent(kernel_tracer_fd
);
1652 /* TODO: Start all UST traces */
1657 case LTTNG_STOP_TRACE
:
1659 struct ltt_kernel_channel
*chan
;
1660 /* Setup lttng message with no payload */
1661 ret
= setup_lttng_msg(cmd_ctx
, 0);
1667 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1668 DBG("Stop kernel tracing");
1670 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
1672 ERR("Kernel metadata flush failed");
1675 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1676 ret
= kernel_flush_buffer(chan
);
1678 ERR("Kernel flush buffer error");
1682 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
1684 ERR("Kernel stop session failed");
1685 ret
= LTTCOMM_KERN_STOP_FAIL
;
1689 /* Quiescent wait after stopping trace */
1690 kernel_wait_quiescent(kernel_tracer_fd
);
1693 /* TODO : User-space tracer */
1698 case LTTNG_CREATE_SESSION
:
1700 /* Setup lttng message with no payload */
1701 ret
= setup_lttng_msg(cmd_ctx
, 0);
1706 ret
= create_session(cmd_ctx
->lsm
->session_name
, cmd_ctx
->lsm
->path
);
1708 if (ret
== -EEXIST
) {
1709 ret
= LTTCOMM_EXIST_SESS
;
1711 ret
= LTTCOMM_FATAL
;
1719 case LTTNG_DESTROY_SESSION
:
1721 /* Setup lttng message with no payload */
1722 ret
= setup_lttng_msg(cmd_ctx
, 0);
1727 /* Clean kernel session teardown */
1728 teardown_kernel_session(cmd_ctx
->session
);
1730 ret
= destroy_session(cmd_ctx
->lsm
->session_name
);
1732 ret
= LTTCOMM_FATAL
;
1737 * Must notify the kernel thread here to update it's pollfd in order to
1738 * remove the channel(s)' fd just destroyed.
1740 ret
= notify_kernel_pollfd();
1742 ret
= LTTCOMM_FATAL
;
1750 case LTTNG_LIST_TRACES:
1752 unsigned int trace_count;
1754 trace_count = get_trace_count_per_session(cmd_ctx->session);
1755 if (trace_count == 0) {
1756 ret = LTTCOMM_NO_TRACE;
1760 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count);
1765 get_traces_per_session(cmd_ctx->session,
1766 (struct lttng_trace *)(cmd_ctx->llm->payload));
1773 case UST_CREATE_TRACE:
1775 ret = setup_lttng_msg(cmd_ctx, 0);
1780 ret = ust_create_trace(cmd_ctx);
1787 case LTTNG_LIST_TRACEABLE_APPS
:
1789 unsigned int app_count
;
1791 app_count
= get_app_count();
1792 DBG("Traceable application count : %d", app_count
);
1793 if (app_count
== 0) {
1794 ret
= LTTCOMM_NO_APPS
;
1798 ret
= setup_lttng_msg(cmd_ctx
, sizeof(pid_t
) * app_count
);
1803 get_app_list_pids((pid_t
*)(cmd_ctx
->llm
->payload
));
1809 case UST_START_TRACE:
1811 ret = setup_lttng_msg(cmd_ctx, 0);
1816 ret = ust_start_trace(cmd_ctx);
1822 case UST_STOP_TRACE:
1824 ret = setup_lttng_msg(cmd_ctx, 0);
1829 ret = ust_stop_trace(cmd_ctx);
1836 case LTTNG_LIST_SESSIONS
:
1838 unsigned int session_count
;
1840 session_count
= get_session_count();
1841 if (session_count
== 0) {
1842 ret
= LTTCOMM_NO_SESSION
;
1846 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * session_count
);
1851 get_lttng_session((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
1857 /* Undefined command */
1858 ret
= setup_lttng_msg(cmd_ctx
, 0);
1867 /* Set return code */
1868 cmd_ctx
->llm
->ret_code
= ret
;
1870 if (cmd_ctx
->session
) {
1871 unlock_session(cmd_ctx
->session
);
1877 if (cmd_ctx
->llm
== NULL
) {
1878 DBG("Missing llm structure. Allocating one.");
1879 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
1883 /* Notify client of error */
1884 cmd_ctx
->llm
->ret_code
= ret
;
1887 if (cmd_ctx
->session
) {
1888 unlock_session(cmd_ctx
->session
);
1894 * thread_manage_clients
1896 * This thread manage all clients request using the unix
1897 * client socket for communication.
1899 static void *thread_manage_clients(void *data
)
1902 struct command_ctx
*cmd_ctx
;
1904 DBG("[thread] Manage client started");
1906 ret
= lttcomm_listen_unix_sock(client_sock
);
1911 /* Notify parent pid that we are ready
1912 * to accept command for client side.
1914 if (opt_sig_parent
) {
1915 kill(ppid
, SIGCHLD
);
1919 /* Blocking call, waiting for transmission */
1920 DBG("Accepting client command ...");
1921 sock
= lttcomm_accept_unix_sock(client_sock
);
1926 /* Allocate context command to process the client request */
1927 cmd_ctx
= malloc(sizeof(struct command_ctx
));
1929 /* Allocate data buffer for reception */
1930 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
1931 cmd_ctx
->llm
= NULL
;
1932 cmd_ctx
->session
= NULL
;
1935 * Data is received from the lttng client. The struct
1936 * lttcomm_session_msg (lsm) contains the command and data request of
1939 DBG("Receiving data from client ...");
1940 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
1945 // TODO: Validate cmd_ctx including sanity check for security purpose.
1948 * This function dispatch the work to the kernel or userspace tracer
1949 * libs and fill the lttcomm_lttng_msg data structure of all the needed
1950 * informations for the client. The command context struct contains
1951 * everything this function may needs.
1953 ret
= process_client_msg(cmd_ctx
);
1955 /* TODO: Inform client somehow of the fatal error. At this point,
1956 * ret < 0 means that a malloc failed (ENOMEM). */
1957 /* Error detected but still accept command */
1958 clean_command_ctx(cmd_ctx
);
1962 DBG("Sending response (size: %d, retcode: %d)",
1963 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
1964 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
1966 ERR("Failed to send data back to client");
1969 clean_command_ctx(cmd_ctx
);
1971 /* End of transmission */
1981 * usage function on stderr
1983 static void usage(void)
1985 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
1986 fprintf(stderr
, " -h, --help Display this usage.\n");
1987 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
1988 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
1989 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
1990 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
1991 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
1992 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
1993 fprintf(stderr
, " -V, --version Show version number.\n");
1994 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
1995 fprintf(stderr
, " -q, --quiet No output at all.\n");
1996 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2000 * daemon argument parsing
2002 static int parse_args(int argc
, char **argv
)
2006 static struct option long_options
[] = {
2007 { "client-sock", 1, 0, 'c' },
2008 { "apps-sock", 1, 0, 'a' },
2009 { "kconsumerd-cmd-sock", 1, 0, 0 },
2010 { "kconsumerd-err-sock", 1, 0, 0 },
2011 { "daemonize", 0, 0, 'd' },
2012 { "sig-parent", 0, 0, 'S' },
2013 { "help", 0, 0, 'h' },
2014 { "group", 1, 0, 'g' },
2015 { "version", 0, 0, 'V' },
2016 { "quiet", 0, 0, 'q' },
2017 { "verbose", 0, 0, 'v' },
2022 int option_index
= 0;
2023 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
2030 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2032 fprintf(stderr
, " with arg %s\n", optarg
);
2036 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2039 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2045 opt_tracing_group
= strdup(optarg
);
2051 fprintf(stdout
, "%s\n", VERSION
);
2057 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2060 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2069 /* Unknown option or other error.
2070 * Error is printed by getopt, just return */
2079 * init_daemon_socket
2081 * Creates the two needed socket by the daemon.
2082 * apps_sock - The communication socket for all UST apps.
2083 * client_sock - The communication of the cli tool (lttng).
2085 static int init_daemon_socket()
2090 old_umask
= umask(0);
2092 /* Create client tool unix socket */
2093 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2094 if (client_sock
< 0) {
2095 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2100 /* File permission MUST be 660 */
2101 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2103 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2108 /* Create the application unix socket */
2109 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2110 if (apps_sock
< 0) {
2111 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2116 /* File permission MUST be 666 */
2117 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2119 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2130 * check_existing_daemon
2132 * Check if the global socket is available.
2133 * If yes, error is returned.
2135 static int check_existing_daemon()
2139 ret
= access(client_unix_sock_path
, F_OK
);
2141 ret
= access(apps_unix_sock_path
, F_OK
);
2150 * Set the tracing group gid onto the client socket.
2152 * Race window between mkdir and chown is OK because we are going from
2153 * more permissive (root.root) to les permissive (root.tracing).
2155 static int set_permissions(void)
2160 /* Decide which group name to use */
2161 (opt_tracing_group
!= NULL
) ?
2162 (grp
= getgrnam(opt_tracing_group
)) :
2163 (grp
= getgrnam(default_tracing_group
));
2167 WARN("No tracing group detected");
2170 ERR("Missing tracing group. Aborting execution.");
2176 /* Set lttng run dir */
2177 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
2179 ERR("Unable to set group on " LTTNG_RUNDIR
);
2183 /* lttng client socket path */
2184 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
2186 ERR("Unable to set group on %s", client_unix_sock_path
);
2190 /* kconsumerd error socket path */
2191 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
2193 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2197 DBG("All permissions are set");
2204 * create_kernel_poll_pipe
2206 * Create the pipe used to wake up the kernel thread.
2208 static int create_kernel_poll_pipe(void)
2210 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2214 * create_lttng_rundir
2216 * Create the lttng run directory needed for all
2217 * global sockets and pipe.
2219 static int create_lttng_rundir(void)
2223 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2225 if (errno
!= EEXIST
) {
2226 ERR("Unable to create " LTTNG_RUNDIR
);
2238 * set_kconsumerd_sockets
2240 * Setup sockets and directory needed by the kconsumerd
2241 * communication with the session daemon.
2243 static int set_kconsumerd_sockets(void)
2247 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
2248 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
2251 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
2252 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
2255 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
2257 if (errno
!= EEXIST
) {
2258 ERR("Failed to create " KCONSUMERD_PATH
);
2264 /* Create the kconsumerd error unix socket */
2265 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
2266 if (kconsumerd_err_sock
< 0) {
2267 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
2272 /* File permission MUST be 660 */
2273 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2275 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
2287 * Signal handler for the daemon
2289 static void sighandler(int sig
)
2293 DBG("SIGPIPE catched");
2296 DBG("SIGINT catched");
2300 DBG("SIGTERM catched");
2311 * set_signal_handler
2313 * Setup signal handler for :
2314 * SIGINT, SIGTERM, SIGPIPE
2316 static int set_signal_handler(void)
2319 struct sigaction sa
;
2322 if ((ret
= sigemptyset(&sigset
)) < 0) {
2323 perror("sigemptyset");
2327 sa
.sa_handler
= sighandler
;
2328 sa
.sa_mask
= sigset
;
2330 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
2331 perror("sigaction");
2335 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
2336 perror("sigaction");
2340 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
2341 perror("sigaction");
2345 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2353 * Set open files limit to unlimited. This daemon can open a large number of
2354 * file descriptors in order to consumer multiple kernel traces.
2356 static void set_ulimit(void)
2361 lim
.rlim_cur
= 65535;
2362 lim
.rlim_max
= 65535;
2364 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
2366 perror("failed to set open files limit");
2373 int main(int argc
, char **argv
)
2377 const char *home_path
;
2379 /* Parse arguments */
2381 if ((ret
= parse_args(argc
, argv
) < 0)) {
2394 /* Check if daemon is UID = 0 */
2395 is_root
= !getuid();
2398 ret
= create_lttng_rundir();
2403 if (strlen(apps_unix_sock_path
) == 0) {
2404 snprintf(apps_unix_sock_path
, PATH_MAX
,
2405 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
2408 if (strlen(client_unix_sock_path
) == 0) {
2409 snprintf(client_unix_sock_path
, PATH_MAX
,
2410 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
2413 ret
= set_kconsumerd_sockets();
2418 /* Setup kernel tracer */
2419 init_kernel_tracer();
2421 /* Set ulimit for open files */
2424 home_path
= get_home_dir();
2425 if (home_path
== NULL
) {
2426 ERR("Can't get HOME directory for sockets creation.\n \
2427 Please specify --socket PATH.");
2431 if (strlen(apps_unix_sock_path
) == 0) {
2432 snprintf(apps_unix_sock_path
, PATH_MAX
,
2433 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
2436 /* Set the cli tool unix socket path */
2437 if (strlen(client_unix_sock_path
) == 0) {
2438 snprintf(client_unix_sock_path
, PATH_MAX
,
2439 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
2443 DBG("Client socket path %s", client_unix_sock_path
);
2444 DBG("Application socket path %s", apps_unix_sock_path
);
2446 /* See if daemon already exist. If any of the two
2447 * socket needed by the daemon are present, this test fails
2449 if ((ret
= check_existing_daemon()) == 0) {
2450 ERR("Already running daemon.\n");
2451 /* We do not goto error because we must not
2452 * cleanup() because a daemon is already running.
2457 if (set_signal_handler() < 0) {
2461 /* Setup the needed unix socket */
2462 if (init_daemon_socket() < 0) {
2466 /* Set credentials to socket */
2467 if (is_root
&& (set_permissions() < 0)) {
2471 /* Get parent pid if -S, --sig-parent is specified. */
2472 if (opt_sig_parent
) {
2476 /* Setup the kernel pipe for waking up the kernel thread */
2477 if (create_kernel_poll_pipe() < 0) {
2481 /* Get session list pointer */
2482 session_list_ptr
= get_session_list();
2485 /* Create thread to manage the client socket */
2486 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
2488 perror("pthread_create");
2492 /* Create thread to manage application socket */
2493 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
2495 perror("pthread_create");
2499 /* Create kernel thread to manage kernel event */
2500 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
2502 perror("pthread_create");
2506 ret
= pthread_join(client_thread
, &status
);
2508 perror("pthread_join");