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"
56 * teardown: signal SIGTERM handler -> write into pipe. Threads waits
57 * with epoll on pipe and on other pipes/sockets for commands. Main
58 * simply waits on pthread join.
62 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
63 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
64 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
65 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
68 int opt_verbose
; /* Not static for lttngerr.h */
69 int opt_quiet
; /* Not static for lttngerr.h */
71 const char *opt_tracing_group
;
72 static int opt_sig_parent
;
73 static int opt_daemon
;
74 static int is_root
; /* Set to 1 if the daemon is running as root */
75 static pid_t ppid
; /* Parent PID for --sig-parent option */
76 static pid_t kconsumerd_pid
;
77 static struct pollfd
*kernel_pollfd
;
79 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
80 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
81 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
82 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
85 static int client_sock
;
87 static int kconsumerd_err_sock
;
88 static int kconsumerd_cmd_sock
;
89 static int kernel_tracer_fd
;
90 static int kernel_poll_pipe
[2];
93 * Quit pipe for all threads. This permits a single cancellation point
94 * for all threads when receiving an event on the pipe.
96 static int thread_quit_pipe
[2];
98 /* Pthread, Mutexes and Semaphores */
99 static pthread_t kconsumerd_thread
;
100 static pthread_t apps_thread
;
101 static pthread_t client_thread
;
102 static pthread_t kernel_thread
;
103 static sem_t kconsumerd_sem
;
105 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
108 * Pointer initialized before thread creation.
110 * This points to the tracing session list containing the session count and a
111 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
112 * MUST NOT be taken if you call a public function in session.c.
114 * The lock is nested inside the structure: session_list_ptr->lock.
116 static struct ltt_session_list
*session_list_ptr
;
121 * Return -1 on error or 0 if all pipes are created.
123 static int init_thread_quit_pipe(void)
127 ret
= pipe2(thread_quit_pipe
, O_CLOEXEC
);
129 perror("thread quit pipe");
138 * teardown_kernel_session
140 * Complete teardown of a kernel session. This free all data structure related
141 * to a kernel session and update counter.
143 static void teardown_kernel_session(struct ltt_session
*session
)
145 if (session
->kernel_session
!= NULL
) {
146 DBG("Tearing down kernel session");
147 trace_destroy_kernel_session(session
->kernel_session
);
148 /* Extra precaution */
149 session
->kernel_session
= NULL
;
156 static void cleanup()
160 struct ltt_session
*sess
;
165 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
166 "Matthew, BEET driven development works!%c[%dm",
167 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
170 /* Stopping all threads */
171 DBG("Terminating all threads");
172 close(thread_quit_pipe
[0]);
173 close(thread_quit_pipe
[1]);
175 DBG("Removing %s directory", LTTNG_RUNDIR
);
176 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
178 ERR("asprintf failed. Something is really wrong!");
181 /* Remove lttng run directory */
184 ERR("Unable to clean " LTTNG_RUNDIR
);
187 DBG("Cleaning up all session");
189 /* Destroy session list mutex */
190 if (session_list_ptr
!= NULL
) {
191 pthread_mutex_destroy(&session_list_ptr
->lock
);
193 /* Cleanup ALL session */
194 cds_list_for_each_entry(sess
, &session_list_ptr
->head
, list
) {
195 teardown_kernel_session(sess
);
196 // TODO complete session cleanup (including UST)
200 pthread_mutex_destroy(&kconsumerd_pid_mutex
);
202 DBG("Closing kernel fd");
203 close(kernel_tracer_fd
);
209 * Send data on a unix socket using the liblttsessiondcomm API.
211 * Return lttcomm error code.
213 static int send_unix_sock(int sock
, void *buf
, size_t len
)
215 /* Check valid length */
220 return lttcomm_send_unix_sock(sock
, buf
, len
);
226 * Free memory of a command context structure.
228 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
230 DBG("Clean command context structure");
232 if ((*cmd_ctx
)->llm
) {
233 free((*cmd_ctx
)->llm
);
235 if ((*cmd_ctx
)->lsm
) {
236 free((*cmd_ctx
)->lsm
);
244 * send_kconsumerd_channel_fds
246 * Send all stream fds of kernel channel to the consumer.
248 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
252 struct ltt_kernel_stream
*stream
;
253 struct lttcomm_kconsumerd_header lkh
;
254 struct lttcomm_kconsumerd_msg lkm
;
256 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
258 nb_fd
= channel
->stream_count
;
261 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
262 lkh
.cmd_type
= ADD_STREAM
;
264 DBG("Sending kconsumerd header");
266 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
268 perror("send kconsumerd header");
272 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
273 if (stream
->fd
!= 0) {
275 lkm
.state
= stream
->state
;
276 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
277 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
279 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
281 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
283 perror("send kconsumerd fd");
289 DBG("Kconsumerd channel fds sent");
298 * send_kconsumerd_fds
300 * Send all stream fds of the kernel session to the consumer.
302 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
305 struct ltt_kernel_channel
*chan
;
306 struct lttcomm_kconsumerd_header lkh
;
307 struct lttcomm_kconsumerd_msg lkm
;
310 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
311 lkh
.cmd_type
= ADD_STREAM
;
313 DBG("Sending kconsumerd header for metadata");
315 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
317 perror("send kconsumerd header");
321 DBG("Sending metadata stream fd");
323 if (session
->metadata_stream_fd
!= 0) {
324 /* Send metadata stream fd first */
325 lkm
.fd
= session
->metadata_stream_fd
;
326 lkm
.state
= ACTIVE_FD
;
327 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
328 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
330 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
332 perror("send kconsumerd fd");
337 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
338 ret
= send_kconsumerd_channel_fds(sock
, chan
);
344 DBG("Kconsumerd fds (metadata and channel streams) sent");
356 * Return a socket connected to the libust communication socket
357 * of the application identified by the pid.
359 * If the pid is not found in the traceable list,
360 * return -1 to indicate error.
362 static int ust_connect_app(pid_t pid
)
365 struct ltt_traceable_app
*lta
;
367 DBG("Connect to application pid %d", pid
);
369 lta
= find_app_by_pid(pid
);
372 DBG("Application pid %d not found", pid
);
376 sock
= ustctl_connect_pid(lta
->pid
);
378 ERR("Fail connecting to the PID %d", pid
);
383 #endif /* DISABLED */
388 * Notify apps by writing 42 to a named pipe using name.
389 * Every applications waiting for a ltt-sessiond will be notified
390 * and re-register automatically to the session daemon.
392 * Return open or write error value.
394 static int notify_apps(const char *name
)
399 DBG("Notify the global application pipe");
401 /* Try opening the global pipe */
402 fd
= open(name
, O_WRONLY
);
407 /* Notify by writing on the pipe */
408 ret
= write(fd
, "42", 2);
420 * Setup the outgoing data buffer for the response (llm) by allocating the
421 * right amount of memory and copying the original information from the lsm
424 * Return total size of the buffer pointed by buf.
426 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
432 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
433 if (cmd_ctx
->llm
== NULL
) {
439 /* Copy common data */
440 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
441 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->pid
;
443 cmd_ctx
->llm
->data_size
= size
;
444 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
453 * update_kernel_pollfd
455 * Update the kernel pollfd set of all channel fd available over
456 * all tracing session. Add the wakeup pipe at the end of the set.
458 static int update_kernel_pollfd(void)
462 * The wakup pipe and the quit pipe are needed so the number of fds starts
463 * at 2 for those pipes.
465 unsigned int nb_fd
= 2;
466 struct ltt_session
*session
;
467 struct ltt_kernel_channel
*channel
;
469 DBG("Updating kernel_pollfd");
471 /* Get the number of channel of all kernel session */
473 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
474 lock_session(session
);
475 if (session
->kernel_session
== NULL
) {
476 unlock_session(session
);
479 nb_fd
+= session
->kernel_session
->channel_count
;
480 unlock_session(session
);
483 DBG("Resizing kernel_pollfd to size %d", nb_fd
);
485 kernel_pollfd
= realloc(kernel_pollfd
, nb_fd
* sizeof(struct pollfd
));
486 if (kernel_pollfd
== NULL
) {
487 perror("malloc kernel_pollfd");
491 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
492 lock_session(session
);
493 if (session
->kernel_session
== NULL
) {
494 unlock_session(session
);
498 ERR("To much channel for kernel_pollfd size");
499 unlock_session(session
);
502 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
503 kernel_pollfd
[i
].fd
= channel
->fd
;
504 kernel_pollfd
[i
].events
= POLLIN
| POLLRDNORM
;
507 unlock_session(session
);
509 unlock_session_list();
511 /* Adding wake up pipe */
512 kernel_pollfd
[nb_fd
- 2].fd
= kernel_poll_pipe
[0];
513 kernel_pollfd
[nb_fd
- 2].events
= POLLIN
;
515 /* Adding the quit pipe */
516 kernel_pollfd
[nb_fd
- 1].fd
= thread_quit_pipe
[0];
521 unlock_session_list();
526 * update_kernel_stream
528 * Find the channel fd from 'fd' over all tracing session. When found, check
529 * for new channel stream and send those stream fds to the kernel consumer.
531 * Useful for CPU hotplug feature.
533 static int update_kernel_stream(int fd
)
536 struct ltt_session
*session
;
537 struct ltt_kernel_channel
*channel
;
539 DBG("Updating kernel streams for channel fd %d", fd
);
542 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
543 lock_session(session
);
544 if (session
->kernel_session
== NULL
) {
545 unlock_session(session
);
548 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
549 if (channel
->fd
== fd
) {
550 DBG("Channel found, updating kernel streams");
551 ret
= kernel_open_channel_stream(channel
);
556 * Have we already sent fds to the consumer? If yes, it means that
557 * tracing is started so it is safe to send our updated stream fds.
559 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
560 ret
= send_kconsumerd_channel_fds(kconsumerd_cmd_sock
, channel
);
568 unlock_session(session
);
572 unlock_session_list();
574 unlock_session(session
);
580 * thread_manage_kernel
582 * This thread manage event coming from the kernel.
584 * Features supported in this thread:
587 static void *thread_manage_kernel(void *data
)
589 int ret
, i
, nb_fd
= 0;
591 int update_poll_flag
= 1;
593 DBG("Thread manage kernel started");
596 if (update_poll_flag
== 1) {
597 nb_fd
= update_kernel_pollfd();
601 update_poll_flag
= 0;
604 DBG("Polling on %d fds", nb_fd
);
606 /* Poll infinite value of time */
607 ret
= poll(kernel_pollfd
, nb_fd
, -1);
609 perror("poll kernel thread");
611 } else if (ret
== 0) {
612 /* Should not happen since timeout is infinite */
616 /* Thread quit pipe has been closed. Killing thread. */
617 if (kernel_pollfd
[nb_fd
- 1].revents
== POLLNVAL
) {
621 DBG("Kernel poll event triggered");
624 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
627 switch (kernel_pollfd
[nb_fd
- 2].revents
) {
629 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
630 update_poll_flag
= 1;
638 for (i
= 0; i
< nb_fd
; i
++) {
639 switch (kernel_pollfd
[i
].revents
) {
641 * New CPU detected by the kernel. Adding kernel stream to kernel
642 * session and updating the kernel consumer
644 case POLLIN
| POLLRDNORM
:
645 ret
= update_kernel_stream(kernel_pollfd
[i
].fd
);
655 DBG("Kernel thread dying");
660 close(kernel_poll_pipe
[0]);
661 close(kernel_poll_pipe
[1]);
666 * thread_manage_kconsumerd
668 * This thread manage the kconsumerd error sent
669 * back to the session daemon.
671 static void *thread_manage_kconsumerd(void *data
)
674 enum lttcomm_return_code code
;
675 struct pollfd pollfd
[2];
677 DBG("[thread] Manage kconsumerd started");
679 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
684 /* First fd is always the quit pipe */
685 pollfd
[0].fd
= thread_quit_pipe
[0];
688 pollfd
[1].fd
= kconsumerd_err_sock
;
689 pollfd
[1].events
= POLLIN
;
691 /* Inifinite blocking call, waiting for transmission */
692 ret
= poll(pollfd
, 2, -1);
694 perror("poll kconsumerd thread");
698 /* Thread quit pipe has been closed. Killing thread. */
699 if (pollfd
[0].revents
== POLLNVAL
) {
701 } else if (pollfd
[1].revents
== POLLERR
) {
702 ERR("Kconsumerd err socket poll error");
706 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
711 /* Getting status code from kconsumerd */
712 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
717 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
718 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
719 if (kconsumerd_cmd_sock
< 0) {
720 sem_post(&kconsumerd_sem
);
721 perror("kconsumerd connect");
724 /* Signal condition to tell that the kconsumerd is ready */
725 sem_post(&kconsumerd_sem
);
726 DBG("Kconsumerd command socket ready");
728 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
729 lttcomm_get_readable_code(-code
));
733 /* Wait for any kconsumerd error */
734 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
736 ERR("Kconsumerd closed the command socket");
740 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
743 DBG("Kconsumerd thread dying");
744 if (kconsumerd_err_sock
) {
745 close(kconsumerd_err_sock
);
747 if (kconsumerd_cmd_sock
) {
748 close(kconsumerd_cmd_sock
);
754 unlink(kconsumerd_err_unix_sock_path
);
755 unlink(kconsumerd_cmd_unix_sock_path
);
764 * This thread manage the application socket communication
766 static void *thread_manage_apps(void *data
)
769 struct pollfd pollfd
[2];
771 /* TODO: Something more elegant is needed but fine for now */
772 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
773 * for 32-bit vs 64-bit compat processes. */
774 /* replicate in ust with version number */
776 int reg
; /* 1:register, 0:unregister */
781 DBG("[thread] Manage apps started");
783 ret
= lttcomm_listen_unix_sock(apps_sock
);
788 /* First fd is always the quit pipe */
789 pollfd
[0].fd
= thread_quit_pipe
[0];
792 pollfd
[1].fd
= apps_sock
;
793 pollfd
[1].events
= POLLIN
;
795 /* Notify all applications to register */
796 notify_apps(default_global_apps_pipe
);
799 DBG("Accepting application registration");
801 /* Inifinite blocking call, waiting for transmission */
802 ret
= poll(pollfd
, 2, -1);
804 perror("poll apps thread");
808 /* Thread quit pipe has been closed. Killing thread. */
809 if (pollfd
[0].revents
== POLLNVAL
) {
811 } else if (pollfd
[1].revents
== POLLERR
) {
812 ERR("Apps socket poll error");
816 sock
= lttcomm_accept_unix_sock(apps_sock
);
822 * Basic recv here to handle the very simple data
823 * that the libust send to register (reg_msg).
825 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
831 /* Add application to the global traceable list */
832 if (reg_msg
.reg
== 1) {
834 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
836 /* register_traceable_app only return an error with
837 * ENOMEM. At this point, we better stop everything.
843 unregister_traceable_app(reg_msg
.pid
);
848 DBG("Apps thread dying");
856 unlink(apps_unix_sock_path
);
861 * spawn_kconsumerd_thread
863 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
864 * exec or it will fails.
866 static int spawn_kconsumerd_thread(void)
870 /* Setup semaphore */
871 sem_init(&kconsumerd_sem
, 0, 0);
873 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
875 perror("pthread_create kconsumerd");
879 /* Wait for the kconsumerd thread to be ready */
880 sem_wait(&kconsumerd_sem
);
882 if (kconsumerd_pid
== 0) {
883 ERR("Kconsumerd did not start");
890 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
897 * Fork and exec a kernel consumer daemon (kconsumerd).
899 * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel
900 * file descriptor using the libkernelctl or kernel-ctl functions. So, a
901 * kernel consumer MUST only be spawned before creating a kernel session.
903 * Return pid if successful else -1.
905 static pid_t
spawn_kconsumerd(void)
910 DBG("Spawning kconsumerd");
917 execlp("ltt-kconsumerd", "ltt-kconsumerd", "--verbose", NULL
);
919 perror("kernel start consumer exec");
922 } else if (pid
> 0) {
926 perror("kernel start consumer fork");
938 * Spawn the kconsumerd daemon and session daemon thread.
940 static int start_kconsumerd(void)
944 pthread_mutex_lock(&kconsumerd_pid_mutex
);
945 if (kconsumerd_pid
!= 0) {
946 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
950 ret
= spawn_kconsumerd();
952 ERR("Spawning kconsumerd failed");
953 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
954 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
958 /* Setting up the global kconsumerd_pid */
959 kconsumerd_pid
= ret
;
960 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
962 DBG("Kconsumerd pid %d", ret
);
964 DBG("Spawning kconsumerd thread");
965 ret
= spawn_kconsumerd_thread();
967 ERR("Fatal error spawning kconsumerd thread");
979 * modprobe_kernel_modules
981 static int modprobe_kernel_modules(void)
986 while (kernel_modules_list
[i
] != NULL
) {
987 ret
= snprintf(modprobe
, sizeof(modprobe
), "/sbin/modprobe %s",
988 kernel_modules_list
[i
]);
990 perror("snprintf modprobe");
993 ret
= system(modprobe
);
995 ERR("Unable to load module %s", kernel_modules_list
[i
]);
997 DBG("Modprobe successfully %s", kernel_modules_list
[i
]);
1008 static int mount_debugfs(char *path
)
1011 char *type
= "debugfs";
1013 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
);
1018 ret
= mount(type
, path
, type
, 0, NULL
);
1020 perror("mount debugfs");
1024 DBG("Mounted debugfs successfully at %s", path
);
1031 * init_kernel_tracer
1033 * Setup necessary data for kernel tracer action.
1035 static void init_kernel_tracer(void)
1038 char *proc_mounts
= "/proc/mounts";
1040 char *debugfs_path
= NULL
, *lttng_path
;
1043 /* Detect debugfs */
1044 fp
= fopen(proc_mounts
, "r");
1046 ERR("Unable to probe %s", proc_mounts
);
1050 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1051 if (strstr(line
, "debugfs") != NULL
) {
1052 /* Remove first string */
1054 /* Dup string here so we can reuse line later on */
1055 debugfs_path
= strdup(strtok(NULL
, " "));
1056 DBG("Got debugfs path : %s", debugfs_path
);
1063 /* Mount debugfs if needded */
1064 if (debugfs_path
== NULL
) {
1065 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1067 perror("asprintf debugfs path");
1070 ret
= mount_debugfs(debugfs_path
);
1076 /* Modprobe lttng kernel modules */
1077 ret
= modprobe_kernel_modules();
1082 /* Setup lttng kernel path */
1083 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1085 perror("asprintf lttng path");
1089 /* Open debugfs lttng */
1090 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1091 if (kernel_tracer_fd
< 0) {
1092 DBG("Failed to open %s", lttng_path
);
1098 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1108 WARN("No kernel tracer available");
1109 kernel_tracer_fd
= 0;
1114 * start_kernel_trace
1116 * Start tracing by creating trace directory and sending FDs to the kernel
1119 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1123 if (session
->kconsumer_fds_sent
== 0) {
1124 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, session
);
1126 ERR("Send kconsumerd fds failed");
1127 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1131 session
->kconsumer_fds_sent
= 1;
1139 * Notify kernel thread to update it's pollfd.
1141 static int notify_kernel_pollfd(void)
1145 /* Inform kernel thread of the new kernel channel */
1146 ret
= write(kernel_poll_pipe
[1], "!", 1);
1148 perror("write kernel poll pipe");
1155 * init_default_channel
1157 * Allocate a channel structure and fill it.
1159 static struct lttng_channel
*init_default_channel(void)
1161 struct lttng_channel
*chan
;
1163 chan
= malloc(sizeof(struct lttng_channel
));
1165 perror("init channel malloc");
1169 if (snprintf(chan
->name
, NAME_MAX
, DEFAULT_CHANNEL_NAME
) < 0) {
1170 perror("snprintf defautl channel name");
1174 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1175 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
1176 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
1177 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1178 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1179 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1186 * create_kernel_session
1188 * Create a kernel tracer session then create the default channel.
1190 static int create_kernel_session(struct ltt_session
*session
)
1193 struct lttng_channel
*chan
;
1195 DBG("Creating kernel session");
1197 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1199 ret
= LTTCOMM_KERN_SESS_FAIL
;
1203 chan
= init_default_channel();
1205 ret
= LTTCOMM_FATAL
;
1209 ret
= mkdir_recursive(session
->path
, S_IRWXU
| S_IRWXG
);
1211 if (ret
!= EEXIST
) {
1212 ERR("Trace directory creation error");
1217 DBG("Creating default kernel channel %s", DEFAULT_CHANNEL_NAME
);
1219 ret
= kernel_create_channel(session
->kernel_session
, chan
, session
->path
);
1221 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1225 ret
= notify_kernel_pollfd();
1232 * Using the session list, filled a lttng_session array to send back to the
1233 * client for session listing.
1235 * The session list lock MUST be acquired before calling this function. Use
1236 * lock_session_list() and unlock_session_list().
1238 static void list_lttng_sessions(struct lttng_session
*sessions
)
1241 struct ltt_session
*session
;
1243 DBG("Getting all available session");
1245 * Iterate over session list and append data after the control struct in
1248 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1249 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1250 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1256 * process_client_msg
1258 * Process the command requested by the lttng client within the command
1259 * context structure. This function make sure that the return structure (llm)
1260 * is set and ready for transmission before returning.
1262 * Return any error encountered or 0 for success.
1264 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1268 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1270 /* Listing commands don't need a session */
1271 switch (cmd_ctx
->lsm
->cmd_type
) {
1272 case LTTNG_CREATE_SESSION
:
1273 case LTTNG_LIST_SESSIONS
:
1274 case LTTNG_LIST_EVENTS
:
1275 case LTTNG_KERNEL_LIST_EVENTS
:
1276 case LTTNG_LIST_TRACEABLE_APPS
:
1279 DBG("Getting session %s by name", cmd_ctx
->lsm
->session_name
);
1280 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session_name
);
1281 if (cmd_ctx
->session
== NULL
) {
1282 /* If session name not found */
1283 if (cmd_ctx
->lsm
->session_name
!= NULL
) {
1284 ret
= LTTCOMM_SESS_NOT_FOUND
;
1285 } else { /* If no session name specified */
1286 ret
= LTTCOMM_SELECT_SESS
;
1290 /* Acquire lock for the session */
1291 lock_session(cmd_ctx
->session
);
1297 * Check kernel command for kernel session.
1299 switch (cmd_ctx
->lsm
->cmd_type
) {
1300 case LTTNG_KERNEL_ADD_CONTEXT
:
1301 case LTTNG_KERNEL_CREATE_CHANNEL
:
1302 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
1303 case LTTNG_KERNEL_DISABLE_CHANNEL
:
1304 case LTTNG_KERNEL_DISABLE_EVENT
:
1305 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
1306 case LTTNG_KERNEL_ENABLE_CHANNEL
:
1307 case LTTNG_KERNEL_ENABLE_EVENT
:
1308 case LTTNG_KERNEL_LIST_EVENTS
:
1309 /* Kernel tracer check */
1310 if (kernel_tracer_fd
== 0) {
1311 init_kernel_tracer();
1312 if (kernel_tracer_fd
== 0) {
1313 ret
= LTTCOMM_KERN_NA
;
1318 /* Need a session for kernel command */
1319 if (cmd_ctx
->lsm
->cmd_type
!= LTTNG_KERNEL_LIST_EVENTS
&&
1320 cmd_ctx
->session
->kernel_session
== NULL
) {
1322 ret
= create_kernel_session(cmd_ctx
->session
);
1324 ret
= LTTCOMM_KERN_SESS_FAIL
;
1328 /* Start the kernel consumer daemon */
1329 if (kconsumerd_pid
== 0) {
1330 ret
= start_kconsumerd();
1339 /* Connect to ust apps if available pid */
1340 if (cmd_ctx
->lsm
->pid
> 0) {
1341 /* Connect to app using ustctl API */
1342 cmd_ctx
->ust_sock
= ust_connect_app(cmd_ctx
->lsm
->pid
);
1343 if (cmd_ctx
->ust_sock
< 0) {
1344 ret
= LTTCOMM_NO_TRACEABLE
;
1348 #endif /* DISABLED */
1350 /* Process by command type */
1351 switch (cmd_ctx
->lsm
->cmd_type
) {
1352 case LTTNG_KERNEL_ADD_CONTEXT
:
1354 int found
= 0, no_event
= 0;
1355 struct ltt_kernel_channel
*chan
;
1356 struct ltt_kernel_event
*event
;
1358 /* Setup lttng message with no payload */
1359 ret
= setup_lttng_msg(cmd_ctx
, 0);
1364 /* Check if event name is given */
1365 if (strlen(cmd_ctx
->lsm
->u
.context
.event_name
) == 0) {
1369 if (strlen(cmd_ctx
->lsm
->u
.context
.channel_name
) == 0) {
1370 /* Go over all channels */
1371 DBG("Adding context to all channels");
1372 cds_list_for_each_entry(chan
,
1373 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1375 ret
= kernel_add_channel_context(chan
,
1376 &cmd_ctx
->lsm
->u
.context
.ctx
);
1381 event
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.context
.event_name
, chan
);
1382 if (event
!= NULL
) {
1383 ret
= kernel_add_event_context(event
,
1384 &cmd_ctx
->lsm
->u
.context
.ctx
);
1386 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1395 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.context
.channel_name
,
1396 cmd_ctx
->session
->kernel_session
);
1398 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1403 ret
= kernel_add_channel_context(chan
,
1404 &cmd_ctx
->lsm
->u
.context
.ctx
);
1406 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1410 event
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.context
.event_name
, chan
);
1411 if (event
!= NULL
) {
1412 ret
= kernel_add_event_context(event
,
1413 &cmd_ctx
->lsm
->u
.context
.ctx
);
1415 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1422 if (!found
&& !no_event
) {
1423 ret
= LTTCOMM_NO_EVENT
;
1430 case LTTNG_KERNEL_CREATE_CHANNEL
:
1432 /* Setup lttng message with no payload */
1433 ret
= setup_lttng_msg(cmd_ctx
, 0);
1439 DBG("Creating kernel channel");
1441 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1442 &cmd_ctx
->lsm
->u
.channel
.chan
, cmd_ctx
->session
->path
);
1444 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1448 ret
= notify_kernel_pollfd();
1450 ret
= LTTCOMM_FATAL
;
1457 case LTTNG_KERNEL_DISABLE_CHANNEL
:
1459 struct ltt_kernel_channel
*chan
;
1461 /* Setup lttng message with no payload */
1462 ret
= setup_lttng_msg(cmd_ctx
, 0);
1467 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1468 cmd_ctx
->session
->kernel_session
);
1470 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1472 } else if (chan
->enabled
== 1) {
1473 ret
= kernel_disable_channel(chan
);
1475 if (ret
!= EEXIST
) {
1476 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1482 kernel_wait_quiescent(kernel_tracer_fd
);
1486 case LTTNG_KERNEL_DISABLE_EVENT
:
1488 struct ltt_kernel_channel
*chan
;
1489 struct ltt_kernel_event
*ev
;
1491 /* Setup lttng message with no payload */
1492 ret
= setup_lttng_msg(cmd_ctx
, 0);
1497 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1498 cmd_ctx
->session
->kernel_session
);
1500 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1504 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, chan
);
1506 DBG("Disabling kernel event %s for channel %s.",
1507 cmd_ctx
->lsm
->u
.disable
.name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
1508 ret
= kernel_disable_event(ev
);
1510 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1515 kernel_wait_quiescent(kernel_tracer_fd
);
1519 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
1521 struct ltt_kernel_channel
*chan
;
1522 struct ltt_kernel_event
*ev
;
1524 /* Setup lttng message with no payload */
1525 ret
= setup_lttng_msg(cmd_ctx
, 0);
1530 DBG("Disabling all enabled kernel events");
1532 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1533 cmd_ctx
->session
->kernel_session
);
1535 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1539 /* For each event in the kernel session */
1540 cds_list_for_each_entry(ev
, &chan
->events_list
.head
, list
) {
1541 DBG("Disabling kernel event %s for channel %s.",
1542 ev
->event
->name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
1543 ret
= kernel_disable_event(ev
);
1549 /* Quiescent wait after event disable */
1550 kernel_wait_quiescent(kernel_tracer_fd
);
1554 case LTTNG_KERNEL_ENABLE_CHANNEL
:
1556 struct ltt_kernel_channel
*chan
;
1558 /* Setup lttng message with no payload */
1559 ret
= setup_lttng_msg(cmd_ctx
, 0);
1564 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1565 cmd_ctx
->session
->kernel_session
);
1567 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1569 } else if (chan
->enabled
== 0) {
1570 ret
= kernel_enable_channel(chan
);
1572 if (ret
!= EEXIST
) {
1573 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1579 kernel_wait_quiescent(kernel_tracer_fd
);
1583 case LTTNG_KERNEL_ENABLE_EVENT
:
1585 struct ltt_kernel_channel
*chan
;
1586 struct ltt_kernel_event
*ev
;
1588 /* Setup lttng message with no payload */
1589 ret
= setup_lttng_msg(cmd_ctx
, 0);
1594 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1595 cmd_ctx
->session
->kernel_session
);
1597 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1601 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
);
1603 DBG("Creating kernel event %s for channel %s.",
1604 cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
->channel
->name
);
1605 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, chan
);
1607 DBG("Enabling kernel event %s for channel %s.",
1608 cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
->channel
->name
);
1609 ret
= kernel_enable_event(ev
);
1613 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1617 kernel_wait_quiescent(kernel_tracer_fd
);
1621 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
1624 char *event_list
, *event
, *ptr
;
1625 struct ltt_kernel_channel
*chan
;
1626 struct ltt_kernel_event
*ev
;
1627 struct lttng_event ev_attr
;
1629 /* Setup lttng message with no payload */
1630 ret
= setup_lttng_msg(cmd_ctx
, 0);
1635 DBG("Enabling all kernel event");
1637 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1638 cmd_ctx
->session
->kernel_session
);
1640 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1644 /* For each event in the kernel session */
1645 cds_list_for_each_entry(ev
, &chan
->events_list
.head
, list
) {
1646 DBG("Enabling kernel event %s for channel %s.",
1647 ev
->event
->name
, chan
->channel
->name
);
1648 ret
= kernel_enable_event(ev
);
1654 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1656 ret
= LTTCOMM_KERN_LIST_FAIL
;
1661 while ((size
= sscanf(ptr
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
1662 ev
= get_kernel_event_by_name(event
, chan
);
1664 strncpy(ev_attr
.name
, event
, LTTNG_SYM_NAME_LEN
);
1665 /* Default event type for enable all */
1666 ev_attr
.type
= LTTNG_EVENT_TRACEPOINT
;
1667 /* Enable each single tracepoint event */
1668 ret
= kernel_create_event(&ev_attr
, chan
);
1670 /* Ignore error here and continue */
1674 /* Move pointer to the next line */
1681 /* Quiescent wait after event enable */
1682 kernel_wait_quiescent(kernel_tracer_fd
);
1686 case LTTNG_KERNEL_LIST_EVENTS
:
1691 DBG("Listing kernel events");
1693 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1695 ret
= LTTCOMM_KERN_LIST_FAIL
;
1700 * Setup lttng message with payload size set to the event list size in
1701 * bytes and then copy list into the llm payload.
1703 ret
= setup_lttng_msg(cmd_ctx
, size
);
1708 /* Copy event list into message payload */
1709 memcpy(cmd_ctx
->llm
->payload
, event_list
, size
);
1716 case LTTNG_START_TRACE
:
1718 struct ltt_kernel_channel
*chan
;
1720 /* Setup lttng message with no payload */
1721 ret
= setup_lttng_msg(cmd_ctx
, 0);
1726 /* Kernel tracing */
1727 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1728 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
1729 DBG("Open kernel metadata");
1730 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
1731 cmd_ctx
->session
->path
);
1733 ret
= LTTCOMM_KERN_META_FAIL
;
1738 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1739 DBG("Opening kernel metadata stream");
1740 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1741 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
1743 ERR("Kernel create metadata stream failed");
1744 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1750 /* For each channel */
1751 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1752 if (chan
->stream_count
== 0) {
1753 ret
= kernel_open_channel_stream(chan
);
1755 ERR("Kernel create channel stream failed");
1756 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1759 /* Update the stream global counter */
1760 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
1764 DBG("Start kernel tracing");
1765 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
1767 ERR("Kernel start session failed");
1768 ret
= LTTCOMM_KERN_START_FAIL
;
1772 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
1774 ret
= LTTCOMM_KERN_START_FAIL
;
1778 /* Quiescent wait after starting trace */
1779 kernel_wait_quiescent(kernel_tracer_fd
);
1782 /* TODO: Start all UST traces */
1787 case LTTNG_STOP_TRACE
:
1789 struct ltt_kernel_channel
*chan
;
1790 /* Setup lttng message with no payload */
1791 ret
= setup_lttng_msg(cmd_ctx
, 0);
1797 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1798 DBG("Stop kernel tracing");
1800 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
1802 ERR("Kernel metadata flush failed");
1805 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1806 ret
= kernel_flush_buffer(chan
);
1808 ERR("Kernel flush buffer error");
1812 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
1814 ERR("Kernel stop session failed");
1815 ret
= LTTCOMM_KERN_STOP_FAIL
;
1819 /* Quiescent wait after stopping trace */
1820 kernel_wait_quiescent(kernel_tracer_fd
);
1823 /* TODO : User-space tracer */
1828 case LTTNG_CREATE_SESSION
:
1830 /* Setup lttng message with no payload */
1831 ret
= setup_lttng_msg(cmd_ctx
, 0);
1836 ret
= create_session(cmd_ctx
->lsm
->session_name
, cmd_ctx
->lsm
->path
);
1838 if (ret
== -EEXIST
) {
1839 ret
= LTTCOMM_EXIST_SESS
;
1841 ret
= LTTCOMM_FATAL
;
1849 case LTTNG_DESTROY_SESSION
:
1851 /* Setup lttng message with no payload */
1852 ret
= setup_lttng_msg(cmd_ctx
, 0);
1857 /* Clean kernel session teardown */
1858 teardown_kernel_session(cmd_ctx
->session
);
1860 ret
= destroy_session(cmd_ctx
->lsm
->session_name
);
1862 ret
= LTTCOMM_FATAL
;
1867 * Must notify the kernel thread here to update it's pollfd in order to
1868 * remove the channel(s)' fd just destroyed.
1870 ret
= notify_kernel_pollfd();
1872 ret
= LTTCOMM_FATAL
;
1880 case LTTNG_LIST_TRACES:
1882 unsigned int trace_count;
1884 trace_count = get_trace_count_per_session(cmd_ctx->session);
1885 if (trace_count == 0) {
1886 ret = LTTCOMM_NO_TRACE;
1890 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count);
1895 get_traces_per_session(cmd_ctx->session,
1896 (struct lttng_trace *)(cmd_ctx->llm->payload));
1903 case UST_CREATE_TRACE:
1905 ret = setup_lttng_msg(cmd_ctx, 0);
1910 ret = ust_create_trace(cmd_ctx);
1917 case LTTNG_LIST_TRACEABLE_APPS
:
1919 unsigned int app_count
;
1921 app_count
= get_app_count();
1922 DBG("Traceable application count : %d", app_count
);
1923 if (app_count
== 0) {
1924 ret
= LTTCOMM_NO_APPS
;
1928 ret
= setup_lttng_msg(cmd_ctx
, sizeof(pid_t
) * app_count
);
1933 get_app_list_pids((pid_t
*)(cmd_ctx
->llm
->payload
));
1939 case UST_START_TRACE:
1941 ret = setup_lttng_msg(cmd_ctx, 0);
1946 ret = ust_start_trace(cmd_ctx);
1952 case UST_STOP_TRACE:
1954 ret = setup_lttng_msg(cmd_ctx, 0);
1959 ret = ust_stop_trace(cmd_ctx);
1966 case LTTNG_LIST_SESSIONS
:
1968 lock_session_list();
1970 if (session_list_ptr
->count
== 0) {
1971 ret
= LTTCOMM_NO_SESSION
;
1975 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
1976 session_list_ptr
->count
);
1981 /* Filled the session array */
1982 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
1984 unlock_session_list();
1990 /* Undefined command */
1991 ret
= setup_lttng_msg(cmd_ctx
, 0);
2000 /* Set return code */
2001 cmd_ctx
->llm
->ret_code
= ret
;
2003 if (cmd_ctx
->session
) {
2004 unlock_session(cmd_ctx
->session
);
2010 if (cmd_ctx
->llm
== NULL
) {
2011 DBG("Missing llm structure. Allocating one.");
2012 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2016 /* Notify client of error */
2017 cmd_ctx
->llm
->ret_code
= ret
;
2020 if (cmd_ctx
->session
) {
2021 unlock_session(cmd_ctx
->session
);
2027 * thread_manage_clients
2029 * This thread manage all clients request using the unix
2030 * client socket for communication.
2032 static void *thread_manage_clients(void *data
)
2035 struct command_ctx
*cmd_ctx
= NULL
;
2036 struct pollfd pollfd
[2];
2038 DBG("[thread] Manage client started");
2040 ret
= lttcomm_listen_unix_sock(client_sock
);
2045 /* First fd is always the quit pipe */
2046 pollfd
[0].fd
= thread_quit_pipe
[0];
2049 pollfd
[1].fd
= client_sock
;
2050 pollfd
[1].events
= POLLIN
;
2052 /* Notify parent pid that we are ready
2053 * to accept command for client side.
2055 if (opt_sig_parent
) {
2056 kill(ppid
, SIGCHLD
);
2060 DBG("Accepting client command ...");
2062 /* Inifinite blocking call, waiting for transmission */
2063 ret
= poll(pollfd
, 2, -1);
2065 perror("poll client thread");
2069 /* Thread quit pipe has been closed. Killing thread. */
2070 if (pollfd
[0].revents
== POLLNVAL
) {
2072 } else if (pollfd
[1].revents
== POLLERR
) {
2073 ERR("Client socket poll error");
2077 sock
= lttcomm_accept_unix_sock(client_sock
);
2082 /* Allocate context command to process the client request */
2083 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2085 /* Allocate data buffer for reception */
2086 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2087 cmd_ctx
->llm
= NULL
;
2088 cmd_ctx
->session
= NULL
;
2091 * Data is received from the lttng client. The struct
2092 * lttcomm_session_msg (lsm) contains the command and data request of
2095 DBG("Receiving data from client ...");
2096 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2101 // TODO: Validate cmd_ctx including sanity check for security purpose.
2104 * This function dispatch the work to the kernel or userspace tracer
2105 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2106 * informations for the client. The command context struct contains
2107 * everything this function may needs.
2109 ret
= process_client_msg(cmd_ctx
);
2111 /* TODO: Inform client somehow of the fatal error. At this point,
2112 * ret < 0 means that a malloc failed (ENOMEM). */
2113 /* Error detected but still accept command */
2114 clean_command_ctx(&cmd_ctx
);
2118 DBG("Sending response (size: %d, retcode: %d)",
2119 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2120 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2122 ERR("Failed to send data back to client");
2125 clean_command_ctx(&cmd_ctx
);
2127 /* End of transmission */
2132 DBG("Client thread dying");
2140 unlink(client_unix_sock_path
);
2142 clean_command_ctx(&cmd_ctx
);
2148 * usage function on stderr
2150 static void usage(void)
2152 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2153 fprintf(stderr
, " -h, --help Display this usage.\n");
2154 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2155 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2156 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2157 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2158 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2159 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2160 fprintf(stderr
, " -V, --version Show version number.\n");
2161 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2162 fprintf(stderr
, " -q, --quiet No output at all.\n");
2163 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2167 * daemon argument parsing
2169 static int parse_args(int argc
, char **argv
)
2173 static struct option long_options
[] = {
2174 { "client-sock", 1, 0, 'c' },
2175 { "apps-sock", 1, 0, 'a' },
2176 { "kconsumerd-cmd-sock", 1, 0, 0 },
2177 { "kconsumerd-err-sock", 1, 0, 0 },
2178 { "daemonize", 0, 0, 'd' },
2179 { "sig-parent", 0, 0, 'S' },
2180 { "help", 0, 0, 'h' },
2181 { "group", 1, 0, 'g' },
2182 { "version", 0, 0, 'V' },
2183 { "quiet", 0, 0, 'q' },
2184 { "verbose", 0, 0, 'v' },
2189 int option_index
= 0;
2190 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
2197 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2199 fprintf(stderr
, " with arg %s\n", optarg
);
2203 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2206 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2212 opt_tracing_group
= strdup(optarg
);
2218 fprintf(stdout
, "%s\n", VERSION
);
2224 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2227 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2236 /* Unknown option or other error.
2237 * Error is printed by getopt, just return */
2246 * init_daemon_socket
2248 * Creates the two needed socket by the daemon.
2249 * apps_sock - The communication socket for all UST apps.
2250 * client_sock - The communication of the cli tool (lttng).
2252 static int init_daemon_socket()
2257 old_umask
= umask(0);
2259 /* Create client tool unix socket */
2260 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2261 if (client_sock
< 0) {
2262 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2267 /* File permission MUST be 660 */
2268 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2270 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2275 /* Create the application unix socket */
2276 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2277 if (apps_sock
< 0) {
2278 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2283 /* File permission MUST be 666 */
2284 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2286 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2297 * check_existing_daemon
2299 * Check if the global socket is available.
2300 * If yes, error is returned.
2302 static int check_existing_daemon()
2306 ret
= access(client_unix_sock_path
, F_OK
);
2308 ret
= access(apps_unix_sock_path
, F_OK
);
2317 * Set the tracing group gid onto the client socket.
2319 * Race window between mkdir and chown is OK because we are going from
2320 * more permissive (root.root) to les permissive (root.tracing).
2322 static int set_permissions(void)
2327 /* Decide which group name to use */
2328 (opt_tracing_group
!= NULL
) ?
2329 (grp
= getgrnam(opt_tracing_group
)) :
2330 (grp
= getgrnam(default_tracing_group
));
2334 WARN("No tracing group detected");
2337 ERR("Missing tracing group. Aborting execution.");
2343 /* Set lttng run dir */
2344 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
2346 ERR("Unable to set group on " LTTNG_RUNDIR
);
2350 /* lttng client socket path */
2351 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
2353 ERR("Unable to set group on %s", client_unix_sock_path
);
2357 /* kconsumerd error socket path */
2358 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
2360 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2364 DBG("All permissions are set");
2371 * create_kernel_poll_pipe
2373 * Create the pipe used to wake up the kernel thread.
2375 static int create_kernel_poll_pipe(void)
2377 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2381 * create_lttng_rundir
2383 * Create the lttng run directory needed for all
2384 * global sockets and pipe.
2386 static int create_lttng_rundir(void)
2390 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2392 if (errno
!= EEXIST
) {
2393 ERR("Unable to create " LTTNG_RUNDIR
);
2405 * set_kconsumerd_sockets
2407 * Setup sockets and directory needed by the kconsumerd
2408 * communication with the session daemon.
2410 static int set_kconsumerd_sockets(void)
2414 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
2415 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
2418 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
2419 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
2422 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
2424 if (errno
!= EEXIST
) {
2425 ERR("Failed to create " KCONSUMERD_PATH
);
2431 /* Create the kconsumerd error unix socket */
2432 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
2433 if (kconsumerd_err_sock
< 0) {
2434 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
2439 /* File permission MUST be 660 */
2440 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2442 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
2454 * Signal handler for the daemon
2456 static void sighandler(int sig
)
2460 DBG("SIGPIPE catched");
2463 DBG("SIGINT catched");
2467 DBG("SIGTERM catched");
2478 * set_signal_handler
2480 * Setup signal handler for :
2481 * SIGINT, SIGTERM, SIGPIPE
2483 static int set_signal_handler(void)
2486 struct sigaction sa
;
2489 if ((ret
= sigemptyset(&sigset
)) < 0) {
2490 perror("sigemptyset");
2494 sa
.sa_handler
= sighandler
;
2495 sa
.sa_mask
= sigset
;
2497 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
2498 perror("sigaction");
2502 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
2503 perror("sigaction");
2507 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
2508 perror("sigaction");
2512 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2520 * Set open files limit to unlimited. This daemon can open a large number of
2521 * file descriptors in order to consumer multiple kernel traces.
2523 static void set_ulimit(void)
2528 /* The kernel does not allowed an infinite limit for open files */
2529 lim
.rlim_cur
= 65535;
2530 lim
.rlim_max
= 65535;
2532 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
2534 perror("failed to set open files limit");
2541 int main(int argc
, char **argv
)
2545 const char *home_path
;
2547 /* Create thread quit pipe */
2548 if (init_thread_quit_pipe() < 0) {
2552 /* Parse arguments */
2554 if ((ret
= parse_args(argc
, argv
) < 0)) {
2567 /* Check if daemon is UID = 0 */
2568 is_root
= !getuid();
2571 ret
= create_lttng_rundir();
2576 if (strlen(apps_unix_sock_path
) == 0) {
2577 snprintf(apps_unix_sock_path
, PATH_MAX
,
2578 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
2581 if (strlen(client_unix_sock_path
) == 0) {
2582 snprintf(client_unix_sock_path
, PATH_MAX
,
2583 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
2586 home_path
= get_home_dir();
2587 if (home_path
== NULL
) {
2588 /* TODO: Add --socket PATH option */
2589 ERR("Can't get HOME directory for sockets creation.");
2593 if (strlen(apps_unix_sock_path
) == 0) {
2594 snprintf(apps_unix_sock_path
, PATH_MAX
,
2595 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
2598 /* Set the cli tool unix socket path */
2599 if (strlen(client_unix_sock_path
) == 0) {
2600 snprintf(client_unix_sock_path
, PATH_MAX
,
2601 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
2605 DBG("Client socket path %s", client_unix_sock_path
);
2606 DBG("Application socket path %s", apps_unix_sock_path
);
2609 * See if daemon already exist. If any of the two socket needed by the
2610 * daemon are present, this test fails. However, if the daemon is killed
2611 * with a SIGKILL, those unix socket must be unlinked by hand.
2613 if ((ret
= check_existing_daemon()) == 0) {
2614 ERR("Already running daemon.\n");
2616 * We do not goto error because we must not cleanup() because a daemon
2617 * is already running.
2622 /* After this point, we can safely call cleanup() so goto error is used */
2625 * These actions must be executed as root. We do that *after* setting up
2626 * the sockets path because we MUST make the check for another daemon using
2627 * those paths *before* trying to set the kernel consumer sockets and init
2631 ret
= set_kconsumerd_sockets();
2636 /* Setup kernel tracer */
2637 init_kernel_tracer();
2639 /* Set ulimit for open files */
2643 if (set_signal_handler() < 0) {
2647 /* Setup the needed unix socket */
2648 if (init_daemon_socket() < 0) {
2652 /* Set credentials to socket */
2653 if (is_root
&& (set_permissions() < 0)) {
2657 /* Get parent pid if -S, --sig-parent is specified. */
2658 if (opt_sig_parent
) {
2662 /* Setup the kernel pipe for waking up the kernel thread */
2663 if (create_kernel_poll_pipe() < 0) {
2668 * Get session list pointer. This pointer MUST NOT be free().
2669 * This list is statically declared in session.c
2671 session_list_ptr
= get_session_list();
2674 /* Create thread to manage the client socket */
2675 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
2677 perror("pthread_create");
2681 /* Create thread to manage application socket */
2682 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
2684 perror("pthread_create");
2688 /* Create kernel thread to manage kernel event */
2689 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
2691 perror("pthread_create");
2695 ret
= pthread_join(client_thread
, &status
);
2697 perror("pthread_join");