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; only version 2
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"
55 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
56 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
57 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
58 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
61 int opt_verbose
; /* Not static for lttngerr.h */
62 int opt_quiet
; /* Not static for lttngerr.h */
65 const char *opt_tracing_group
;
66 static int opt_sig_parent
;
67 static int opt_daemon
;
68 static int is_root
; /* Set to 1 if the daemon is running as root */
69 static pid_t ppid
; /* Parent PID for --sig-parent option */
70 static pid_t kconsumerd_pid
;
71 static struct pollfd
*kernel_pollfd
;
73 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
74 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
75 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
76 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
79 static int client_sock
;
81 static int kconsumerd_err_sock
;
82 static int kconsumerd_cmd_sock
;
83 static int kernel_tracer_fd
;
84 static int kernel_poll_pipe
[2];
87 * Quit pipe for all threads. This permits a single cancellation point
88 * for all threads when receiving an event on the pipe.
90 static int thread_quit_pipe
[2];
92 /* Pthread, Mutexes and Semaphores */
93 static pthread_t kconsumerd_thread
;
94 static pthread_t apps_thread
;
95 static pthread_t client_thread
;
96 static pthread_t kernel_thread
;
97 static sem_t kconsumerd_sem
;
99 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
102 * Pointer initialized before thread creation.
104 * This points to the tracing session list containing the session count and a
105 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
106 * MUST NOT be taken if you call a public function in session.c.
108 * The lock is nested inside the structure: session_list_ptr->lock. Please use
109 * lock_session_list and unlock_session_list for lock acquisition.
111 static struct ltt_session_list
*session_list_ptr
;
116 * Return -1 on error or 0 if all pipes are created.
118 static int init_thread_quit_pipe(void)
122 ret
= pipe2(thread_quit_pipe
, O_CLOEXEC
);
124 perror("thread quit pipe");
133 * Complete teardown of a kernel session. This free all data structure related
134 * to a kernel session and update counter.
136 static void teardown_kernel_session(struct ltt_session
*session
)
138 if (session
->kernel_session
!= NULL
) {
139 DBG("Tearing down kernel session");
140 trace_destroy_kernel_session(session
->kernel_session
);
141 /* Extra precaution */
142 session
->kernel_session
= NULL
;
149 static void cleanup()
153 struct ltt_session
*sess
, *stmp
;
158 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
159 "Matthew, BEET driven development works!%c[%dm",
160 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
163 /* Stopping all threads */
164 DBG("Terminating all threads");
165 close(thread_quit_pipe
[0]);
166 close(thread_quit_pipe
[1]);
168 DBG("Removing %s directory", LTTNG_RUNDIR
);
169 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
171 ERR("asprintf failed. Something is really wrong!");
174 /* Remove lttng run directory */
177 ERR("Unable to clean " LTTNG_RUNDIR
);
180 DBG("Cleaning up all session");
182 /* Destroy session list mutex */
183 if (session_list_ptr
!= NULL
) {
184 pthread_mutex_destroy(&session_list_ptr
->lock
);
186 /* Cleanup ALL session */
187 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
188 teardown_kernel_session(sess
);
189 // TODO complete session cleanup (including UST)
193 pthread_mutex_destroy(&kconsumerd_pid_mutex
);
195 DBG("Closing kernel fd");
196 close(kernel_tracer_fd
);
200 * Send data on a unix socket using the liblttsessiondcomm API.
202 * Return lttcomm error code.
204 static int send_unix_sock(int sock
, void *buf
, size_t len
)
206 /* Check valid length */
211 return lttcomm_send_unix_sock(sock
, buf
, len
);
215 * Free memory of a command context structure.
217 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
219 DBG("Clean command context structure");
221 if ((*cmd_ctx
)->llm
) {
222 free((*cmd_ctx
)->llm
);
224 if ((*cmd_ctx
)->lsm
) {
225 free((*cmd_ctx
)->lsm
);
233 * Send all stream fds of kernel channel to the consumer.
235 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
239 struct ltt_kernel_stream
*stream
;
240 struct lttcomm_kconsumerd_header lkh
;
241 struct lttcomm_kconsumerd_msg lkm
;
243 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
245 nb_fd
= channel
->stream_count
;
248 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
249 lkh
.cmd_type
= ADD_STREAM
;
251 DBG("Sending kconsumerd header");
253 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
255 perror("send kconsumerd header");
259 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
260 if (stream
->fd
!= 0) {
262 lkm
.state
= stream
->state
;
263 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
264 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
266 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
268 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
270 perror("send kconsumerd fd");
276 DBG("Kconsumerd channel fds sent");
285 * Send all stream fds of the kernel session to the consumer.
287 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
290 struct ltt_kernel_channel
*chan
;
291 struct lttcomm_kconsumerd_header lkh
;
292 struct lttcomm_kconsumerd_msg lkm
;
295 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
296 lkh
.cmd_type
= ADD_STREAM
;
298 DBG("Sending kconsumerd header for metadata");
300 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
302 perror("send kconsumerd header");
306 DBG("Sending metadata stream fd");
308 if (session
->metadata_stream_fd
!= 0) {
309 /* Send metadata stream fd first */
310 lkm
.fd
= session
->metadata_stream_fd
;
311 lkm
.state
= ACTIVE_FD
;
312 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
313 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
315 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
317 perror("send kconsumerd fd");
322 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
323 ret
= send_kconsumerd_channel_fds(sock
, chan
);
329 DBG("Kconsumerd fds (metadata and channel streams) sent");
339 * Return a socket connected to the libust communication socket of the
340 * application identified by the pid.
342 * If the pid is not found in the traceable list, 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
);
365 #endif /* DISABLED */
368 * Notify apps by writing 42 to a named pipe using name. Every applications
369 * waiting for a ltt-sessiond will be notified and re-register automatically to
370 * the session daemon.
372 * Return open or write error value.
374 static int notify_apps(const char *name
)
379 DBG("Notify the global application pipe");
381 /* Try opening the global pipe */
382 fd
= open(name
, O_WRONLY
);
387 /* Notify by writing on the pipe */
388 ret
= write(fd
, "42", 2);
398 * Setup the outgoing data buffer for the response (llm) by allocating the
399 * right amount of memory and copying the original information from the lsm
402 * Return total size of the buffer pointed by buf.
404 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
410 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
411 if (cmd_ctx
->llm
== NULL
) {
417 /* Copy common data */
418 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
419 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->pid
;
421 cmd_ctx
->llm
->data_size
= size
;
422 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
431 * Update the kernel pollfd set of all channel fd available over all tracing
432 * session. Add the wakeup pipe at the end of the set.
434 static int update_kernel_pollfd(void)
438 * The wakup pipe and the quit pipe are needed so the number of fds starts
439 * at 2 for those pipes.
441 unsigned int nb_fd
= 2;
442 struct ltt_session
*session
;
443 struct ltt_kernel_channel
*channel
;
445 DBG("Updating kernel_pollfd");
447 /* Get the number of channel of all kernel session */
449 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
450 lock_session(session
);
451 if (session
->kernel_session
== NULL
) {
452 unlock_session(session
);
455 nb_fd
+= session
->kernel_session
->channel_count
;
456 unlock_session(session
);
459 DBG("Resizing kernel_pollfd to size %d", nb_fd
);
461 kernel_pollfd
= realloc(kernel_pollfd
, nb_fd
* sizeof(struct pollfd
));
462 if (kernel_pollfd
== NULL
) {
463 perror("malloc kernel_pollfd");
467 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
468 lock_session(session
);
469 if (session
->kernel_session
== NULL
) {
470 unlock_session(session
);
474 ERR("To much channel for kernel_pollfd size");
475 unlock_session(session
);
478 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
479 kernel_pollfd
[i
].fd
= channel
->fd
;
480 kernel_pollfd
[i
].events
= POLLIN
| POLLRDNORM
;
483 unlock_session(session
);
485 unlock_session_list();
487 /* Adding wake up pipe */
488 kernel_pollfd
[nb_fd
- 2].fd
= kernel_poll_pipe
[0];
489 kernel_pollfd
[nb_fd
- 2].events
= POLLIN
;
491 /* Adding the quit pipe */
492 kernel_pollfd
[nb_fd
- 1].fd
= thread_quit_pipe
[0];
497 unlock_session_list();
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
);
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
);
546 unlock_session_list();
548 unlock_session(session
);
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 /* Thread quit pipe has been closed. Killing thread. */
589 if (kernel_pollfd
[nb_fd
- 1].revents
== POLLNVAL
) {
593 DBG("Kernel poll event triggered");
596 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
599 switch (kernel_pollfd
[nb_fd
- 2].revents
) {
601 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
602 update_poll_flag
= 1;
610 for (i
= 0; i
< nb_fd
; i
++) {
611 switch (kernel_pollfd
[i
].revents
) {
613 * New CPU detected by the kernel. Adding kernel stream to kernel
614 * session and updating the kernel consumer
616 case POLLIN
| POLLRDNORM
:
617 ret
= update_kernel_stream(kernel_pollfd
[i
].fd
);
627 DBG("Kernel thread dying");
632 close(kernel_poll_pipe
[0]);
633 close(kernel_poll_pipe
[1]);
638 * This thread manage the kconsumerd error sent back to the session daemon.
640 static void *thread_manage_kconsumerd(void *data
)
643 enum lttcomm_return_code code
;
644 struct pollfd pollfd
[2];
646 DBG("[thread] Manage kconsumerd started");
648 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
653 /* First fd is always the quit pipe */
654 pollfd
[0].fd
= thread_quit_pipe
[0];
657 pollfd
[1].fd
= kconsumerd_err_sock
;
658 pollfd
[1].events
= POLLIN
;
660 /* Inifinite blocking call, waiting for transmission */
661 ret
= poll(pollfd
, 2, -1);
663 perror("poll kconsumerd thread");
667 /* Thread quit pipe has been closed. Killing thread. */
668 if (pollfd
[0].revents
== POLLNVAL
) {
670 } else if (pollfd
[1].revents
== POLLERR
) {
671 ERR("Kconsumerd err socket poll error");
675 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
680 /* Getting status code from kconsumerd */
681 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
686 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
687 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
688 if (kconsumerd_cmd_sock
< 0) {
689 sem_post(&kconsumerd_sem
);
690 perror("kconsumerd connect");
693 /* Signal condition to tell that the kconsumerd is ready */
694 sem_post(&kconsumerd_sem
);
695 DBG("Kconsumerd command socket ready");
697 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
698 lttcomm_get_readable_code(-code
));
702 /* Wait for any kconsumerd error */
703 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
705 ERR("Kconsumerd closed the command socket");
709 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
712 DBG("Kconsumerd thread dying");
713 if (kconsumerd_err_sock
) {
714 close(kconsumerd_err_sock
);
716 if (kconsumerd_cmd_sock
) {
717 close(kconsumerd_cmd_sock
);
723 unlink(kconsumerd_err_unix_sock_path
);
724 unlink(kconsumerd_cmd_unix_sock_path
);
731 * This thread manage the application socket communication
733 static void *thread_manage_apps(void *data
)
736 struct pollfd pollfd
[2];
738 /* TODO: Something more elegant is needed but fine for now */
739 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
740 * for 32-bit vs 64-bit compat processes. */
741 /* replicate in ust with version number */
743 int reg
; /* 1:register, 0:unregister */
748 DBG("[thread] Manage apps started");
750 ret
= lttcomm_listen_unix_sock(apps_sock
);
755 /* First fd is always the quit pipe */
756 pollfd
[0].fd
= thread_quit_pipe
[0];
759 pollfd
[1].fd
= apps_sock
;
760 pollfd
[1].events
= POLLIN
;
762 /* Notify all applications to register */
763 notify_apps(default_global_apps_pipe
);
766 DBG("Accepting application registration");
768 /* Inifinite blocking call, waiting for transmission */
769 ret
= poll(pollfd
, 2, -1);
771 perror("poll apps thread");
775 /* Thread quit pipe has been closed. Killing thread. */
776 if (pollfd
[0].revents
== POLLNVAL
) {
778 } else if (pollfd
[1].revents
== POLLERR
) {
779 ERR("Apps socket poll error");
783 sock
= lttcomm_accept_unix_sock(apps_sock
);
789 * Basic recv here to handle the very simple data
790 * that the libust send to register (reg_msg).
792 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
798 /* Add application to the global traceable list */
799 if (reg_msg
.reg
== 1) {
801 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
803 /* register_traceable_app only return an error with
804 * ENOMEM. At this point, we better stop everything.
810 unregister_traceable_app(reg_msg
.pid
);
815 DBG("Apps thread dying");
823 unlink(apps_unix_sock_path
);
828 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
829 * exec or it will fails.
831 static int spawn_kconsumerd_thread(void)
835 /* Setup semaphore */
836 sem_init(&kconsumerd_sem
, 0, 0);
838 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
840 perror("pthread_create kconsumerd");
844 /* Wait for the kconsumerd thread to be ready */
845 sem_wait(&kconsumerd_sem
);
847 if (kconsumerd_pid
== 0) {
848 ERR("Kconsumerd did not start");
855 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
860 * Fork and exec a kernel consumer daemon (kconsumerd).
862 * Return pid if successful else -1.
864 static pid_t
spawn_kconsumerd(void)
868 const char *verbosity
;
870 DBG("Spawning kconsumerd");
877 if (opt_verbose
> 1) {
878 verbosity
= "--verbose";
880 verbosity
= "--quiet";
882 execl(INSTALL_BIN_PATH
"/ltt-kconsumerd", "ltt-kconsumerd", verbosity
, NULL
);
884 perror("kernel start consumer exec");
887 } else if (pid
> 0) {
891 perror("kernel start consumer fork");
901 * Spawn the kconsumerd daemon and session daemon thread.
903 static int start_kconsumerd(void)
907 pthread_mutex_lock(&kconsumerd_pid_mutex
);
908 if (kconsumerd_pid
!= 0) {
909 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
913 ret
= spawn_kconsumerd();
915 ERR("Spawning kconsumerd failed");
916 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
917 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
921 /* Setting up the global kconsumerd_pid */
922 kconsumerd_pid
= ret
;
923 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
925 DBG("Kconsumerd pid %d", ret
);
927 DBG("Spawning kconsumerd thread");
928 ret
= spawn_kconsumerd_thread();
930 ERR("Fatal error spawning kconsumerd thread");
942 * modprobe_kernel_modules
944 static int modprobe_kernel_modules(void)
949 while (kernel_modules_list
[i
] != NULL
) {
950 ret
= snprintf(modprobe
, sizeof(modprobe
), "/sbin/modprobe %s",
951 kernel_modules_list
[i
]);
953 perror("snprintf modprobe");
956 ret
= system(modprobe
);
958 ERR("Unable to load module %s", kernel_modules_list
[i
]);
960 DBG("Modprobe successfully %s", kernel_modules_list
[i
]);
971 static int mount_debugfs(char *path
)
974 char *type
= "debugfs";
976 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
);
981 ret
= mount(type
, path
, type
, 0, NULL
);
983 perror("mount debugfs");
987 DBG("Mounted debugfs successfully at %s", path
);
994 * Setup necessary data for kernel tracer action.
996 static void init_kernel_tracer(void)
999 char *proc_mounts
= "/proc/mounts";
1001 char *debugfs_path
= NULL
, *lttng_path
;
1004 /* Detect debugfs */
1005 fp
= fopen(proc_mounts
, "r");
1007 ERR("Unable to probe %s", proc_mounts
);
1011 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1012 if (strstr(line
, "debugfs") != NULL
) {
1013 /* Remove first string */
1015 /* Dup string here so we can reuse line later on */
1016 debugfs_path
= strdup(strtok(NULL
, " "));
1017 DBG("Got debugfs path : %s", debugfs_path
);
1024 /* Mount debugfs if needded */
1025 if (debugfs_path
== NULL
) {
1026 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1028 perror("asprintf debugfs path");
1031 ret
= mount_debugfs(debugfs_path
);
1037 /* Modprobe lttng kernel modules */
1038 ret
= modprobe_kernel_modules();
1043 /* Setup lttng kernel path */
1044 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1046 perror("asprintf lttng path");
1050 /* Open debugfs lttng */
1051 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1052 if (kernel_tracer_fd
< 0) {
1053 DBG("Failed to open %s", lttng_path
);
1059 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1069 WARN("No kernel tracer available");
1070 kernel_tracer_fd
= 0;
1075 * Start tracing by creating trace directory and sending FDs to the kernel
1078 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1082 if (session
->kconsumer_fds_sent
== 0) {
1083 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, session
);
1085 ERR("Send kconsumerd fds failed");
1086 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1090 session
->kconsumer_fds_sent
= 1;
1098 * Notify kernel thread to update it's pollfd.
1100 static int notify_kernel_pollfd(void)
1104 /* Inform kernel thread of the new kernel channel */
1105 ret
= write(kernel_poll_pipe
[1], "!", 1);
1107 perror("write kernel poll pipe");
1114 * Allocate a channel structure and fill it.
1116 static struct lttng_channel
*init_default_channel(char *name
)
1118 struct lttng_channel
*chan
;
1120 chan
= malloc(sizeof(struct lttng_channel
));
1122 perror("init channel malloc");
1126 if (snprintf(chan
->name
, NAME_MAX
, "%s", name
) < 0) {
1127 perror("snprintf defautl channel name");
1131 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1132 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
1133 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
1134 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1135 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1136 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1143 * Create a kernel tracer session then create the default channel.
1145 static int create_kernel_session(struct ltt_session
*session
)
1149 DBG("Creating kernel session");
1151 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1153 ret
= LTTCOMM_KERN_SESS_FAIL
;
1157 ret
= mkdir_recursive(session
->path
, S_IRWXU
| S_IRWXG
);
1159 if (ret
!= EEXIST
) {
1160 ERR("Trace directory creation error");
1170 * Using the session list, filled a lttng_session array to send back to the
1171 * client for session listing.
1173 * The session list lock MUST be acquired before calling this function. Use
1174 * lock_session_list() and unlock_session_list().
1176 static void list_lttng_sessions(struct lttng_session
*sessions
)
1179 struct ltt_session
*session
;
1181 DBG("Getting all available session");
1183 * Iterate over session list and append data after the control struct in
1186 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1187 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1188 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1194 * Process the command requested by the lttng client within the command
1195 * context structure. This function make sure that the return structure (llm)
1196 * is set and ready for transmission before returning.
1198 * Return any error encountered or 0 for success.
1200 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1202 int ret
= LTTCOMM_OK
;
1204 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1206 /* Listing commands don't need a session */
1207 switch (cmd_ctx
->lsm
->cmd_type
) {
1208 case LTTNG_CREATE_SESSION
:
1209 case LTTNG_LIST_SESSIONS
:
1210 case LTTNG_LIST_EVENTS
:
1211 case LTTNG_KERNEL_LIST_EVENTS
:
1212 case LTTNG_LIST_TRACEABLE_APPS
:
1215 DBG("Getting session %s by name", cmd_ctx
->lsm
->session_name
);
1216 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session_name
);
1217 if (cmd_ctx
->session
== NULL
) {
1218 /* If session name not found */
1219 if (cmd_ctx
->lsm
->session_name
!= NULL
) {
1220 ret
= LTTCOMM_SESS_NOT_FOUND
;
1221 } else { /* If no session name specified */
1222 ret
= LTTCOMM_SELECT_SESS
;
1226 /* Acquire lock for the session */
1227 lock_session(cmd_ctx
->session
);
1233 * Check kernel command for kernel session.
1235 switch (cmd_ctx
->lsm
->cmd_type
) {
1236 case LTTNG_KERNEL_ADD_CONTEXT
:
1237 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
1238 case LTTNG_KERNEL_DISABLE_CHANNEL
:
1239 case LTTNG_KERNEL_DISABLE_EVENT
:
1240 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
1241 case LTTNG_KERNEL_ENABLE_CHANNEL
:
1242 case LTTNG_KERNEL_ENABLE_EVENT
:
1243 case LTTNG_KERNEL_LIST_EVENTS
:
1244 /* Kernel tracer check */
1245 if (kernel_tracer_fd
== 0) {
1246 init_kernel_tracer();
1247 if (kernel_tracer_fd
== 0) {
1248 ret
= LTTCOMM_KERN_NA
;
1253 /* Need a session for kernel command */
1254 if (cmd_ctx
->lsm
->cmd_type
!= LTTNG_KERNEL_LIST_EVENTS
&&
1255 cmd_ctx
->session
->kernel_session
== NULL
) {
1257 ret
= create_kernel_session(cmd_ctx
->session
);
1259 ret
= LTTCOMM_KERN_SESS_FAIL
;
1263 /* Start the kernel consumer daemon */
1264 if (kconsumerd_pid
== 0) {
1265 ret
= start_kconsumerd();
1274 /* Connect to ust apps if available pid */
1275 if (cmd_ctx
->lsm
->pid
> 0) {
1276 /* Connect to app using ustctl API */
1277 cmd_ctx
->ust_sock
= ust_connect_app(cmd_ctx
->lsm
->pid
);
1278 if (cmd_ctx
->ust_sock
< 0) {
1279 ret
= LTTCOMM_NO_TRACEABLE
;
1283 #endif /* DISABLED */
1285 /* Process by command type */
1286 switch (cmd_ctx
->lsm
->cmd_type
) {
1287 case LTTNG_KERNEL_ADD_CONTEXT
:
1289 int found
= 0, no_event
= 0;
1290 struct ltt_kernel_channel
*chan
;
1291 struct ltt_kernel_event
*event
;
1292 struct lttng_kernel_context ctx
;
1294 /* Setup lttng message with no payload */
1295 ret
= setup_lttng_msg(cmd_ctx
, 0);
1300 /* Check if event name is given */
1301 if (strlen(cmd_ctx
->lsm
->u
.context
.event_name
) == 0) {
1305 /* Create Kernel context */
1306 ctx
.ctx
= cmd_ctx
->lsm
->u
.context
.ctx
.ctx
;
1307 ctx
.u
.perf_counter
.type
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.type
;
1308 ctx
.u
.perf_counter
.config
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.config
;
1309 strncpy(ctx
.u
.perf_counter
.name
,
1310 cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.name
,
1311 sizeof(ctx
.u
.perf_counter
.name
));
1313 if (strlen(cmd_ctx
->lsm
->u
.context
.channel_name
) == 0) {
1314 /* Go over all channels */
1315 DBG("Adding context to all channels");
1316 cds_list_for_each_entry(chan
,
1317 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1319 ret
= kernel_add_channel_context(chan
, &ctx
);
1321 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1325 event
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.context
.event_name
, chan
);
1326 if (event
!= NULL
) {
1327 ret
= kernel_add_event_context(event
, &ctx
);
1329 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1338 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.context
.channel_name
,
1339 cmd_ctx
->session
->kernel_session
);
1341 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1346 ret
= kernel_add_channel_context(chan
, &ctx
);
1348 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1352 event
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.context
.event_name
, chan
);
1353 if (event
!= NULL
) {
1354 ret
= kernel_add_event_context(event
, &ctx
);
1356 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1363 if (!found
&& !no_event
) {
1364 ret
= LTTCOMM_NO_EVENT
;
1371 case LTTNG_KERNEL_DISABLE_CHANNEL
:
1373 struct ltt_kernel_channel
*chan
;
1375 /* Setup lttng message with no payload */
1376 ret
= setup_lttng_msg(cmd_ctx
, 0);
1381 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1382 cmd_ctx
->session
->kernel_session
);
1384 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1386 } else if (chan
->enabled
== 1) {
1387 ret
= kernel_disable_channel(chan
);
1389 if (ret
!= EEXIST
) {
1390 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1396 kernel_wait_quiescent(kernel_tracer_fd
);
1400 case LTTNG_KERNEL_DISABLE_EVENT
:
1402 struct ltt_kernel_channel
*chan
;
1403 struct ltt_kernel_event
*ev
;
1405 /* Setup lttng message with no payload */
1406 ret
= setup_lttng_msg(cmd_ctx
, 0);
1411 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1412 cmd_ctx
->session
->kernel_session
);
1414 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1418 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, chan
);
1420 DBG("Disabling kernel event %s for channel %s.",
1421 cmd_ctx
->lsm
->u
.disable
.name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
1422 ret
= kernel_disable_event(ev
);
1424 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1429 kernel_wait_quiescent(kernel_tracer_fd
);
1433 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
1435 struct ltt_kernel_channel
*chan
;
1436 struct ltt_kernel_event
*ev
;
1438 /* Setup lttng message with no payload */
1439 ret
= setup_lttng_msg(cmd_ctx
, 0);
1444 DBG("Disabling all enabled kernel events");
1446 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1447 cmd_ctx
->session
->kernel_session
);
1449 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1453 /* For each event in the kernel session */
1454 cds_list_for_each_entry(ev
, &chan
->events_list
.head
, list
) {
1455 DBG("Disabling kernel event %s for channel %s.",
1456 ev
->event
->name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
1457 ret
= kernel_disable_event(ev
);
1463 /* Quiescent wait after event disable */
1464 kernel_wait_quiescent(kernel_tracer_fd
);
1468 case LTTNG_KERNEL_ENABLE_CHANNEL
:
1470 struct ltt_kernel_channel
*chan
;
1472 /* Setup lttng message with no payload */
1473 ret
= setup_lttng_msg(cmd_ctx
, 0);
1478 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1479 cmd_ctx
->session
->kernel_session
);
1481 /* Channel not found, creating it */
1482 DBG("Creating kernel channel");
1484 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1485 &cmd_ctx
->lsm
->u
.channel
.chan
, cmd_ctx
->session
->path
);
1487 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1491 /* Notify kernel thread that there is a new channel */
1492 ret
= notify_kernel_pollfd();
1494 ret
= LTTCOMM_FATAL
;
1497 } else if (chan
->enabled
== 0) {
1498 ret
= kernel_enable_channel(chan
);
1500 if (ret
!= EEXIST
) {
1501 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1507 kernel_wait_quiescent(kernel_tracer_fd
);
1511 case LTTNG_KERNEL_ENABLE_EVENT
:
1514 struct ltt_kernel_channel
*kchan
;
1515 struct ltt_kernel_event
*ev
;
1516 struct lttng_channel
*chan
;
1518 /* Setup lttng message with no payload */
1519 ret
= setup_lttng_msg(cmd_ctx
, 0);
1524 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
1527 kchan
= get_kernel_channel_by_name(channel_name
,
1528 cmd_ctx
->session
->kernel_session
);
1529 if (kchan
== NULL
) {
1530 DBG("Channel not found. Creating channel %s", channel_name
);
1532 chan
= init_default_channel(channel_name
);
1534 ret
= LTTCOMM_FATAL
;
1538 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1539 chan
, cmd_ctx
->session
->path
);
1541 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1545 } while (kchan
== NULL
);
1547 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, kchan
);
1549 DBG("Creating kernel event %s for channel %s.",
1550 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
1551 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, kchan
);
1553 DBG("Enabling kernel event %s for channel %s.",
1554 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
1555 ret
= kernel_enable_event(ev
);
1556 if (ret
== -EEXIST
) {
1557 ret
= LTTCOMM_KERN_EVENT_EXIST
;
1563 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1567 kernel_wait_quiescent(kernel_tracer_fd
);
1571 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
1574 char *event_list
, *event
, *ptr
, *channel_name
;
1575 struct ltt_kernel_channel
*kchan
;
1576 struct ltt_kernel_event
*ev
;
1577 struct lttng_event ev_attr
;
1578 struct lttng_channel
*chan
;
1580 /* Setup lttng message with no payload */
1581 ret
= setup_lttng_msg(cmd_ctx
, 0);
1586 DBG("Enabling all kernel event");
1588 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
1591 kchan
= get_kernel_channel_by_name(channel_name
,
1592 cmd_ctx
->session
->kernel_session
);
1593 if (kchan
== NULL
) {
1594 DBG("Channel not found. Creating channel %s", channel_name
);
1596 chan
= init_default_channel(channel_name
);
1598 ret
= LTTCOMM_FATAL
;
1602 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1603 chan
, cmd_ctx
->session
->path
);
1605 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1609 } while (kchan
== NULL
);
1611 /* For each event in the kernel session */
1612 cds_list_for_each_entry(ev
, &kchan
->events_list
.head
, list
) {
1613 DBG("Enabling kernel event %s for channel %s.",
1614 ev
->event
->name
, channel_name
);
1615 ret
= kernel_enable_event(ev
);
1621 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1623 ret
= LTTCOMM_KERN_LIST_FAIL
;
1628 while ((size
= sscanf(ptr
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
1629 ev
= get_kernel_event_by_name(event
, kchan
);
1631 strncpy(ev_attr
.name
, event
, LTTNG_SYM_NAME_LEN
);
1632 /* Default event type for enable all */
1633 ev_attr
.type
= LTTNG_EVENT_TRACEPOINT
;
1634 /* Enable each single tracepoint event */
1635 ret
= kernel_create_event(&ev_attr
, kchan
);
1637 /* Ignore error here and continue */
1641 /* Move pointer to the next line */
1648 /* Quiescent wait after event enable */
1649 kernel_wait_quiescent(kernel_tracer_fd
);
1653 case LTTNG_KERNEL_LIST_EVENTS
:
1658 DBG("Listing kernel events");
1660 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1662 ret
= LTTCOMM_KERN_LIST_FAIL
;
1667 * Setup lttng message with payload size set to the event list size in
1668 * bytes and then copy list into the llm payload.
1670 ret
= setup_lttng_msg(cmd_ctx
, size
);
1675 /* Copy event list into message payload */
1676 memcpy(cmd_ctx
->llm
->payload
, event_list
, size
);
1683 case LTTNG_START_TRACE
:
1685 struct ltt_kernel_channel
*chan
;
1687 /* Setup lttng message with no payload */
1688 ret
= setup_lttng_msg(cmd_ctx
, 0);
1693 /* Kernel tracing */
1694 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1695 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
1696 DBG("Open kernel metadata");
1697 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
1698 cmd_ctx
->session
->path
);
1700 ret
= LTTCOMM_KERN_META_FAIL
;
1705 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1706 DBG("Opening kernel metadata stream");
1707 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1708 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
1710 ERR("Kernel create metadata stream failed");
1711 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1717 /* For each channel */
1718 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1719 if (chan
->stream_count
== 0) {
1720 ret
= kernel_open_channel_stream(chan
);
1722 ERR("Kernel create channel stream failed");
1723 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1726 /* Update the stream global counter */
1727 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
1731 DBG("Start kernel tracing");
1732 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
1734 ERR("Kernel start session failed");
1735 ret
= LTTCOMM_KERN_START_FAIL
;
1739 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
1741 ret
= LTTCOMM_KERN_START_FAIL
;
1745 /* Quiescent wait after starting trace */
1746 kernel_wait_quiescent(kernel_tracer_fd
);
1749 /* TODO: Start all UST traces */
1754 case LTTNG_STOP_TRACE
:
1756 struct ltt_kernel_channel
*chan
;
1757 /* Setup lttng message with no payload */
1758 ret
= setup_lttng_msg(cmd_ctx
, 0);
1764 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1765 DBG("Stop kernel tracing");
1767 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
1769 ERR("Kernel metadata flush failed");
1772 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1773 ret
= kernel_flush_buffer(chan
);
1775 ERR("Kernel flush buffer error");
1779 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
1781 ERR("Kernel stop session failed");
1782 ret
= LTTCOMM_KERN_STOP_FAIL
;
1786 /* Quiescent wait after stopping trace */
1787 kernel_wait_quiescent(kernel_tracer_fd
);
1790 /* TODO : User-space tracer */
1795 case LTTNG_CREATE_SESSION
:
1797 /* Setup lttng message with no payload */
1798 ret
= setup_lttng_msg(cmd_ctx
, 0);
1803 ret
= create_session(cmd_ctx
->lsm
->session_name
, cmd_ctx
->lsm
->path
);
1805 if (ret
== -EEXIST
) {
1806 ret
= LTTCOMM_EXIST_SESS
;
1808 ret
= LTTCOMM_FATAL
;
1816 case LTTNG_DESTROY_SESSION
:
1818 /* Setup lttng message with no payload */
1819 ret
= setup_lttng_msg(cmd_ctx
, 0);
1824 /* Clean kernel session teardown */
1825 teardown_kernel_session(cmd_ctx
->session
);
1827 ret
= destroy_session(cmd_ctx
->lsm
->session_name
);
1829 ret
= LTTCOMM_FATAL
;
1834 * Must notify the kernel thread here to update it's pollfd in order to
1835 * remove the channel(s)' fd just destroyed.
1837 ret
= notify_kernel_pollfd();
1839 ret
= LTTCOMM_FATAL
;
1847 case LTTNG_LIST_TRACES:
1849 unsigned int trace_count;
1851 trace_count = get_trace_count_per_session(cmd_ctx->session);
1852 if (trace_count == 0) {
1853 ret = LTTCOMM_NO_TRACE;
1857 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count);
1862 get_traces_per_session(cmd_ctx->session,
1863 (struct lttng_trace *)(cmd_ctx->llm->payload));
1870 case UST_CREATE_TRACE:
1872 ret = setup_lttng_msg(cmd_ctx, 0);
1877 ret = ust_create_trace(cmd_ctx);
1884 case LTTNG_LIST_TRACEABLE_APPS
:
1886 unsigned int app_count
;
1888 app_count
= get_app_count();
1889 DBG("Traceable application count : %d", app_count
);
1890 if (app_count
== 0) {
1891 ret
= LTTCOMM_NO_APPS
;
1895 ret
= setup_lttng_msg(cmd_ctx
, sizeof(pid_t
) * app_count
);
1900 get_app_list_pids((pid_t
*)(cmd_ctx
->llm
->payload
));
1906 case UST_START_TRACE:
1908 ret = setup_lttng_msg(cmd_ctx, 0);
1913 ret = ust_start_trace(cmd_ctx);
1919 case UST_STOP_TRACE:
1921 ret = setup_lttng_msg(cmd_ctx, 0);
1926 ret = ust_stop_trace(cmd_ctx);
1933 case LTTNG_LIST_SESSIONS
:
1935 lock_session_list();
1937 if (session_list_ptr
->count
== 0) {
1938 ret
= LTTCOMM_NO_SESSION
;
1939 unlock_session_list();
1943 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
1944 session_list_ptr
->count
);
1946 unlock_session_list();
1950 /* Filled the session array */
1951 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
1953 unlock_session_list();
1959 /* Undefined command */
1960 ret
= setup_lttng_msg(cmd_ctx
, 0);
1969 /* Set return code */
1970 cmd_ctx
->llm
->ret_code
= ret
;
1972 if (cmd_ctx
->session
) {
1973 unlock_session(cmd_ctx
->session
);
1979 if (cmd_ctx
->llm
== NULL
) {
1980 DBG("Missing llm structure. Allocating one.");
1981 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
1985 /* Notify client of error */
1986 cmd_ctx
->llm
->ret_code
= ret
;
1989 if (cmd_ctx
->session
) {
1990 unlock_session(cmd_ctx
->session
);
1996 * This thread manage all clients request using the unix client socket for
1999 static void *thread_manage_clients(void *data
)
2002 struct command_ctx
*cmd_ctx
= NULL
;
2003 struct pollfd pollfd
[2];
2005 DBG("[thread] Manage client started");
2007 ret
= lttcomm_listen_unix_sock(client_sock
);
2012 /* First fd is always the quit pipe */
2013 pollfd
[0].fd
= thread_quit_pipe
[0];
2016 pollfd
[1].fd
= client_sock
;
2017 pollfd
[1].events
= POLLIN
;
2019 /* Notify parent pid that we are ready
2020 * to accept command for client side.
2022 if (opt_sig_parent
) {
2023 kill(ppid
, SIGCHLD
);
2027 DBG("Accepting client command ...");
2029 /* Inifinite blocking call, waiting for transmission */
2030 ret
= poll(pollfd
, 2, -1);
2032 perror("poll client thread");
2036 /* Thread quit pipe has been closed. Killing thread. */
2037 if (pollfd
[0].revents
== POLLNVAL
) {
2039 } else if (pollfd
[1].revents
== POLLERR
) {
2040 ERR("Client socket poll error");
2044 sock
= lttcomm_accept_unix_sock(client_sock
);
2049 /* Allocate context command to process the client request */
2050 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2052 /* Allocate data buffer for reception */
2053 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2054 cmd_ctx
->llm
= NULL
;
2055 cmd_ctx
->session
= NULL
;
2058 * Data is received from the lttng client. The struct
2059 * lttcomm_session_msg (lsm) contains the command and data request of
2062 DBG("Receiving data from client ...");
2063 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2068 // TODO: Validate cmd_ctx including sanity check for security purpose.
2071 * This function dispatch the work to the kernel or userspace tracer
2072 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2073 * informations for the client. The command context struct contains
2074 * everything this function may needs.
2076 ret
= process_client_msg(cmd_ctx
);
2078 /* TODO: Inform client somehow of the fatal error. At this point,
2079 * ret < 0 means that a malloc failed (ENOMEM). */
2080 /* Error detected but still accept command */
2081 clean_command_ctx(&cmd_ctx
);
2085 DBG("Sending response (size: %d, retcode: %d)",
2086 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2087 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2089 ERR("Failed to send data back to client");
2092 clean_command_ctx(&cmd_ctx
);
2094 /* End of transmission */
2099 DBG("Client thread dying");
2107 unlink(client_unix_sock_path
);
2109 clean_command_ctx(&cmd_ctx
);
2115 * usage function on stderr
2117 static void usage(void)
2119 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2120 fprintf(stderr
, " -h, --help Display this usage.\n");
2121 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2122 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2123 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2124 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2125 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2126 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2127 fprintf(stderr
, " -V, --version Show version number.\n");
2128 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2129 fprintf(stderr
, " -q, --quiet No output at all.\n");
2130 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2134 * daemon argument parsing
2136 static int parse_args(int argc
, char **argv
)
2140 static struct option long_options
[] = {
2141 { "client-sock", 1, 0, 'c' },
2142 { "apps-sock", 1, 0, 'a' },
2143 { "kconsumerd-cmd-sock", 1, 0, 0 },
2144 { "kconsumerd-err-sock", 1, 0, 0 },
2145 { "daemonize", 0, 0, 'd' },
2146 { "sig-parent", 0, 0, 'S' },
2147 { "help", 0, 0, 'h' },
2148 { "group", 1, 0, 'g' },
2149 { "version", 0, 0, 'V' },
2150 { "quiet", 0, 0, 'q' },
2151 { "verbose", 0, 0, 'v' },
2156 int option_index
= 0;
2157 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
2164 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2166 fprintf(stderr
, " with arg %s\n", optarg
);
2170 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2173 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2179 opt_tracing_group
= strdup(optarg
);
2185 fprintf(stdout
, "%s\n", VERSION
);
2191 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2194 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2200 /* Verbose level can increase using multiple -v */
2204 /* Unknown option or other error.
2205 * Error is printed by getopt, just return */
2214 * Creates the two needed socket by the daemon.
2215 * apps_sock - The communication socket for all UST apps.
2216 * client_sock - The communication of the cli tool (lttng).
2218 static int init_daemon_socket()
2223 old_umask
= umask(0);
2225 /* Create client tool unix socket */
2226 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2227 if (client_sock
< 0) {
2228 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2233 /* File permission MUST be 660 */
2234 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2236 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2241 /* Create the application unix socket */
2242 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2243 if (apps_sock
< 0) {
2244 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2249 /* File permission MUST be 666 */
2250 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2252 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2263 * Check if the global socket is available. If yes, error is returned.
2265 static int check_existing_daemon()
2269 ret
= access(client_unix_sock_path
, F_OK
);
2271 ret
= access(apps_unix_sock_path
, F_OK
);
2278 * Set the tracing group gid onto the client socket.
2280 * Race window between mkdir and chown is OK because we are going from more
2281 * permissive (root.root) to les permissive (root.tracing).
2283 static int set_permissions(void)
2288 /* Decide which group name to use */
2289 (opt_tracing_group
!= NULL
) ?
2290 (grp
= getgrnam(opt_tracing_group
)) :
2291 (grp
= getgrnam(default_tracing_group
));
2295 WARN("No tracing group detected");
2298 ERR("Missing tracing group. Aborting execution.");
2304 /* Set lttng run dir */
2305 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
2307 ERR("Unable to set group on " LTTNG_RUNDIR
);
2311 /* lttng client socket path */
2312 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
2314 ERR("Unable to set group on %s", client_unix_sock_path
);
2318 /* kconsumerd error socket path */
2319 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
2321 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2325 DBG("All permissions are set");
2332 * Create the pipe used to wake up the kernel thread.
2334 static int create_kernel_poll_pipe(void)
2336 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2340 * Create the lttng run directory needed for all global sockets and pipe.
2342 static int create_lttng_rundir(void)
2346 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2348 if (errno
!= EEXIST
) {
2349 ERR("Unable to create " LTTNG_RUNDIR
);
2361 * Setup sockets and directory needed by the kconsumerd communication with the
2364 static int set_kconsumerd_sockets(void)
2368 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
2369 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
2372 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
2373 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
2376 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
2378 if (errno
!= EEXIST
) {
2379 ERR("Failed to create " KCONSUMERD_PATH
);
2385 /* Create the kconsumerd error unix socket */
2386 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
2387 if (kconsumerd_err_sock
< 0) {
2388 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
2393 /* File permission MUST be 660 */
2394 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2396 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
2406 * Signal handler for the daemon
2408 static void sighandler(int sig
)
2412 DBG("SIGPIPE catched");
2415 DBG("SIGINT catched");
2419 DBG("SIGTERM catched");
2430 * Setup signal handler for :
2431 * SIGINT, SIGTERM, SIGPIPE
2433 static int set_signal_handler(void)
2436 struct sigaction sa
;
2439 if ((ret
= sigemptyset(&sigset
)) < 0) {
2440 perror("sigemptyset");
2444 sa
.sa_handler
= sighandler
;
2445 sa
.sa_mask
= sigset
;
2447 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
2448 perror("sigaction");
2452 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
2453 perror("sigaction");
2457 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
2458 perror("sigaction");
2462 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2468 * Set open files limit to unlimited. This daemon can open a large number of
2469 * file descriptors in order to consumer multiple kernel traces.
2471 static void set_ulimit(void)
2476 /* The kernel does not allowed an infinite limit for open files */
2477 lim
.rlim_cur
= 65535;
2478 lim
.rlim_max
= 65535;
2480 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
2482 perror("failed to set open files limit");
2489 int main(int argc
, char **argv
)
2493 const char *home_path
;
2495 /* Create thread quit pipe */
2496 if (init_thread_quit_pipe() < 0) {
2500 /* Parse arguments */
2502 if ((ret
= parse_args(argc
, argv
) < 0)) {
2515 /* Check if daemon is UID = 0 */
2516 is_root
= !getuid();
2519 ret
= create_lttng_rundir();
2524 if (strlen(apps_unix_sock_path
) == 0) {
2525 snprintf(apps_unix_sock_path
, PATH_MAX
,
2526 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
2529 if (strlen(client_unix_sock_path
) == 0) {
2530 snprintf(client_unix_sock_path
, PATH_MAX
,
2531 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
2534 home_path
= get_home_dir();
2535 if (home_path
== NULL
) {
2536 /* TODO: Add --socket PATH option */
2537 ERR("Can't get HOME directory for sockets creation.");
2541 if (strlen(apps_unix_sock_path
) == 0) {
2542 snprintf(apps_unix_sock_path
, PATH_MAX
,
2543 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
2546 /* Set the cli tool unix socket path */
2547 if (strlen(client_unix_sock_path
) == 0) {
2548 snprintf(client_unix_sock_path
, PATH_MAX
,
2549 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
2553 DBG("Client socket path %s", client_unix_sock_path
);
2554 DBG("Application socket path %s", apps_unix_sock_path
);
2557 * See if daemon already exist. If any of the two socket needed by the
2558 * daemon are present, this test fails. However, if the daemon is killed
2559 * with a SIGKILL, those unix socket must be unlinked by hand.
2561 if ((ret
= check_existing_daemon()) == 0) {
2562 ERR("Already running daemon.\n");
2564 * We do not goto error because we must not cleanup() because a daemon
2565 * is already running.
2570 /* After this point, we can safely call cleanup() so goto error is used */
2573 * These actions must be executed as root. We do that *after* setting up
2574 * the sockets path because we MUST make the check for another daemon using
2575 * those paths *before* trying to set the kernel consumer sockets and init
2579 ret
= set_kconsumerd_sockets();
2584 /* Setup kernel tracer */
2585 init_kernel_tracer();
2587 /* Set ulimit for open files */
2591 if (set_signal_handler() < 0) {
2595 /* Setup the needed unix socket */
2596 if (init_daemon_socket() < 0) {
2600 /* Set credentials to socket */
2601 if (is_root
&& (set_permissions() < 0)) {
2605 /* Get parent pid if -S, --sig-parent is specified. */
2606 if (opt_sig_parent
) {
2610 /* Setup the kernel pipe for waking up the kernel thread */
2611 if (create_kernel_poll_pipe() < 0) {
2616 * Get session list pointer. This pointer MUST NOT be free().
2617 * This list is statically declared in session.c
2619 session_list_ptr
= get_session_list();
2622 /* Create thread to manage the client socket */
2623 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
2625 perror("pthread_create");
2629 /* Create thread to manage application socket */
2630 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
2632 perror("pthread_create");
2636 /* Create kernel thread to manage kernel event */
2637 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
2639 perror("pthread_create");
2643 ret
= pthread_join(client_thread
, &status
);
2645 perror("pthread_join");