2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <semaphore.h>
32 #include <sys/mount.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <sys/resource.h>
41 #include <urcu/list.h> /* URCU list library (-lurcu) */
42 #include <lttng/lttng.h>
44 #include "liblttsessiondcomm.h"
45 #include "ltt-sessiond.h"
47 #include "kernel-ctl.h"
50 #include "traceable-app.h"
51 #include "lttng-kconsumerd.h"
52 #include "libustctl.h"
57 * teardown: signal SIGTERM handler -> write into pipe. Threads waits
58 * with epoll on pipe and on other pipes/sockets for commands. Main
59 * simply waits on pthread join.
63 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
64 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
65 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
66 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
69 int opt_verbose
; /* Not static for lttngerr.h */
70 int opt_quiet
; /* Not static for lttngerr.h */
72 const char *opt_tracing_group
;
73 static int opt_sig_parent
;
74 static int opt_daemon
;
75 static int is_root
; /* Set to 1 if the daemon is running as root */
76 static pid_t ppid
; /* Parent PID for --sig-parent option */
77 static pid_t kconsumerd_pid
;
78 static struct pollfd
*kernel_pollfd
;
80 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
81 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
82 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
83 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
86 static int client_sock
;
88 static int kconsumerd_err_sock
;
89 static int kconsumerd_cmd_sock
;
90 static int kernel_tracer_fd
;
91 static int kernel_poll_pipe
[2];
93 /* Pthread, Mutexes and Semaphores */
94 static pthread_t kconsumerd_thread
;
95 static pthread_t apps_thread
;
96 static pthread_t client_thread
;
97 static pthread_t kernel_thread
;
98 static sem_t kconsumerd_sem
;
100 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
103 * teardown_kernel_session
105 * Complete teardown of a kernel session. This free all data structure
106 * related to a kernel session and update counter.
108 static void teardown_kernel_session(struct ltt_session
*session
)
110 if (session
->kernel_session
!= NULL
) {
111 DBG("Tearing down kernel session");
112 trace_destroy_kernel_session(session
->kernel_session
);
113 /* Extra precaution */
114 session
->kernel_session
= NULL
;
121 * Cleanup the daemon on exit
123 static void cleanup()
127 struct ltt_session
*sess
;
132 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
133 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
136 /* Stopping all threads */
137 DBG("Terminating all threads");
138 pthread_cancel(client_thread
);
139 pthread_cancel(apps_thread
);
140 pthread_cancel(kernel_thread
);
141 if (kconsumerd_pid
!= 0) {
142 pthread_cancel(kconsumerd_thread
);
145 DBG("Unlinking all unix socket");
146 unlink(client_unix_sock_path
);
147 unlink(apps_unix_sock_path
);
148 unlink(kconsumerd_err_unix_sock_path
);
150 DBG("Removing %s directory", LTTNG_RUNDIR
);
151 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
153 ERR("asprintf failed. Something is really wrong!");
156 /* Remove lttng run directory */
159 ERR("Unable to clean " LTTNG_RUNDIR
);
162 DBG("Cleaning up all session");
163 /* Cleanup ALL session */
164 cds_list_for_each_entry(sess
, <t_session_list
.head
, list
) {
165 teardown_kernel_session(sess
);
166 // TODO complete session cleanup (including UST)
169 DBG("Closing kernel fd");
170 close(kernel_tracer_fd
);
171 close(kernel_poll_pipe
[0]);
172 close(kernel_poll_pipe
[1]);
178 * Send data on a unix socket using the liblttsessiondcomm API.
180 * Return lttcomm error code.
182 static int send_unix_sock(int sock
, void *buf
, size_t len
)
184 /* Check valid length */
189 return lttcomm_send_unix_sock(sock
, buf
, len
);
195 * Free memory of a command context structure.
197 static void clean_command_ctx(struct command_ctx
*cmd_ctx
)
199 DBG("Clean command context structure %p", cmd_ctx
);
213 * send_kconsumerd_channel_fds
215 * Send all stream fds of kernel channel to the consumer.
217 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
221 struct ltt_kernel_stream
*stream
;
222 struct lttcomm_kconsumerd_header lkh
;
223 struct lttcomm_kconsumerd_msg lkm
;
225 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
227 nb_fd
= channel
->stream_count
;
230 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
231 lkh
.cmd_type
= ADD_STREAM
;
233 DBG("Sending kconsumerd header");
235 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
237 perror("send kconsumerd header");
241 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
242 if (stream
->fd
!= 0) {
244 lkm
.state
= stream
->state
;
245 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
246 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
248 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
250 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
252 perror("send kconsumerd fd");
258 DBG("Kconsumerd channel fds sent");
267 * send_kconsumerd_fds
269 * Send all stream fds of the kernel session to the consumer.
271 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
274 struct ltt_kernel_channel
*chan
;
275 struct lttcomm_kconsumerd_header lkh
;
276 struct lttcomm_kconsumerd_msg lkm
;
279 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
280 lkh
.cmd_type
= ADD_STREAM
;
282 DBG("Sending kconsumerd header for metadata");
284 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
286 perror("send kconsumerd header");
290 DBG("Sending metadata stream fd");
292 if (session
->metadata_stream_fd
!= 0) {
293 /* Send metadata stream fd first */
294 lkm
.fd
= session
->metadata_stream_fd
;
295 lkm
.state
= ACTIVE_FD
;
296 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
297 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
299 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
301 perror("send kconsumerd fd");
306 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
307 ret
= send_kconsumerd_channel_fds(sock
, chan
);
313 DBG("Kconsumerd fds (metadata and channel streams) sent");
324 * Return a socket connected to the libust communication socket
325 * of the application identified by the pid.
327 * If the pid is not found in the traceable list,
328 * return -1 to indicate error.
330 static int ust_connect_app(pid_t pid
)
333 struct ltt_traceable_app
*lta
;
335 DBG("Connect to application pid %d", pid
);
337 lta
= find_app_by_pid(pid
);
340 DBG("Application pid %d not found", pid
);
344 sock
= ustctl_connect_pid(lta
->pid
);
346 ERR("Fail connecting to the PID %d", pid
);
355 * Notify apps by writing 42 to a named pipe using name.
356 * Every applications waiting for a ltt-sessiond will be notified
357 * and re-register automatically to the session daemon.
359 * Return open or write error value.
361 static int notify_apps(const char *name
)
366 DBG("Notify the global application pipe");
368 /* Try opening the global pipe */
369 fd
= open(name
, O_WRONLY
);
374 /* Notify by writing on the pipe */
375 ret
= write(fd
, "42", 2);
387 * Setup the outgoing data buffer for the response (llm) by allocating the
388 * right amount of memory and copying the original information from the lsm
391 * Return total size of the buffer pointed by buf.
393 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
399 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
400 if (cmd_ctx
->llm
== NULL
) {
406 /* Copy common data */
407 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
408 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->pid
;
410 cmd_ctx
->llm
->data_size
= size
;
411 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
420 * update_kernel_pollfd
422 * Update the kernel pollfd set of all channel fd available over
423 * all tracing session. Add the wakeup pipe at the end of the set.
425 static int update_kernel_pollfd(void)
428 unsigned int nb_fd
= 1;
429 struct ltt_session
*session
;
430 struct ltt_kernel_channel
*channel
;
432 DBG("Updating kernel_pollfd");
434 /* Get the number of channel of all kernel session */
435 cds_list_for_each_entry(session
, <t_session_list
.head
, list
) {
436 if (session
->kernel_session
== NULL
) {
439 nb_fd
+= session
->kernel_session
->channel_count
;
442 DBG("Resizing kernel_pollfd to size %d", nb_fd
);
444 kernel_pollfd
= realloc(kernel_pollfd
, nb_fd
* sizeof(struct pollfd
));
445 if (kernel_pollfd
== NULL
) {
446 perror("malloc kernel_pollfd");
450 cds_list_for_each_entry(session
, <t_session_list
.head
, list
) {
451 if (session
->kernel_session
== NULL
) {
455 ERR("To much channel for kernel_pollfd size");
458 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
459 kernel_pollfd
[i
].fd
= channel
->fd
;
460 kernel_pollfd
[i
].events
= POLLIN
| POLLRDNORM
;
465 /* Adding wake up pipe */
466 kernel_pollfd
[nb_fd
- 1].fd
= kernel_poll_pipe
[0];
467 kernel_pollfd
[nb_fd
- 1].events
= POLLIN
;
476 * update_kernel_stream
478 * Find the channel fd from 'fd' over all tracing session. When found, check
479 * for new channel stream and send those stream fds to the kernel consumer.
481 * Useful for CPU hotplug feature.
483 static int update_kernel_stream(int fd
)
486 struct ltt_session
*session
;
487 struct ltt_kernel_channel
*channel
;
489 DBG("Updating kernel streams for channel fd %d", fd
);
491 cds_list_for_each_entry(session
, <t_session_list
.head
, list
) {
492 if (session
->kernel_session
== NULL
) {
495 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
496 if (channel
->fd
== fd
) {
497 DBG("Channel found, updating kernel streams");
498 ret
= kernel_open_channel_stream(channel
);
503 * Have we already sent fds to the consumer? If yes, it means that
504 * tracing is started so it is safe to send our updated stream fds.
506 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
507 ret
= send_kconsumerd_channel_fds(kconsumerd_cmd_sock
, channel
);
522 * thread_manage_kernel
524 * This thread manage event coming from the kernel.
526 * Features supported in this thread:
529 static void *thread_manage_kernel(void *data
)
531 int ret
, i
, nb_fd
= 0;
533 int update_poll_flag
= 1;
535 DBG("Thread manage kernel started");
538 if (update_poll_flag
== 1) {
539 nb_fd
= update_kernel_pollfd();
543 update_poll_flag
= 0;
546 DBG("Polling on %d fds", nb_fd
);
548 /* Poll infinite value of time */
549 ret
= poll(kernel_pollfd
, nb_fd
, -1);
551 perror("poll kernel thread");
553 } else if (ret
== 0) {
554 /* Should not happen since timeout is infinite */
558 DBG("Kernel poll event triggered");
561 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
564 if (kernel_pollfd
[nb_fd
- 1].revents
== POLLIN
) {
565 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
566 update_poll_flag
= 1;
570 for (i
= 0; i
< nb_fd
; i
++) {
571 switch (kernel_pollfd
[i
].revents
) {
573 * New CPU detected by the kernel. Adding kernel stream to kernel
574 * session and updating the kernel consumer
576 case POLLIN
| POLLRDNORM
:
577 ret
= update_kernel_stream(kernel_pollfd
[i
].fd
);
587 DBG("Kernel thread dying");
595 * thread_manage_kconsumerd
597 * This thread manage the kconsumerd error sent
598 * back to the session daemon.
600 static void *thread_manage_kconsumerd(void *data
)
603 enum lttcomm_return_code code
;
605 DBG("[thread] Manage kconsumerd started");
607 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
612 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
617 /* Getting status code from kconsumerd */
618 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
623 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
624 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
625 if (kconsumerd_cmd_sock
< 0) {
626 sem_post(&kconsumerd_sem
);
627 perror("kconsumerd connect");
630 /* Signal condition to tell that the kconsumerd is ready */
631 sem_post(&kconsumerd_sem
);
632 DBG("Kconsumerd command socket ready");
634 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
635 lttcomm_get_readable_code(-code
));
639 /* Wait for any kconsumerd error */
640 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
642 ERR("Kconsumerd closed the command socket");
646 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
650 DBG("Kconsumerd thread dying");
657 * This thread manage the application socket communication
659 static void *thread_manage_apps(void *data
)
663 /* TODO: Something more elegant is needed but fine for now */
664 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
665 * for 32-bit vs 64-bit compat processes. */
666 /* replicate in ust with version number */
668 int reg
; /* 1:register, 0:unregister */
673 DBG("[thread] Manage apps started");
675 ret
= lttcomm_listen_unix_sock(apps_sock
);
680 /* Notify all applications to register */
681 notify_apps(default_global_apps_pipe
);
684 DBG("Accepting application registration");
685 /* Blocking call, waiting for transmission */
686 sock
= lttcomm_accept_unix_sock(apps_sock
);
691 /* Basic recv here to handle the very simple data
692 * that the libust send to register (reg_msg).
694 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
700 /* Add application to the global traceable list */
701 if (reg_msg
.reg
== 1) {
703 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
705 /* register_traceable_app only return an error with
706 * ENOMEM. At this point, we better stop everything.
712 unregister_traceable_app(reg_msg
.pid
);
722 * spawn_kconsumerd_thread
724 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
725 * exec or it will fails.
727 static int spawn_kconsumerd_thread(void)
731 /* Setup semaphore */
732 sem_init(&kconsumerd_sem
, 0, 0);
734 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
736 perror("pthread_create kconsumerd");
740 /* Wait for the kconsumerd thread to be ready */
741 sem_wait(&kconsumerd_sem
);
743 if (kconsumerd_pid
== 0) {
744 ERR("Kconsumerd did not start");
751 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
758 * Fork and exec a kernel consumer daemon (kconsumerd).
760 * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel
761 * file descriptor using the libkernelctl or kernel-ctl functions. So, a
762 * kernel consumer MUST only be spawned before creating a kernel session.
764 * Return pid if successful else -1.
766 static pid_t
spawn_kconsumerd(void)
771 DBG("Spawning kconsumerd");
778 execlp("ltt-kconsumerd", "ltt-kconsumerd", "--verbose", NULL
);
780 perror("kernel start consumer exec");
783 } else if (pid
> 0) {
787 perror("kernel start consumer fork");
799 * Spawn the kconsumerd daemon and session daemon thread.
801 static int start_kconsumerd(void)
805 pthread_mutex_lock(&kconsumerd_pid_mutex
);
806 if (kconsumerd_pid
!= 0) {
810 ret
= spawn_kconsumerd();
812 ERR("Spawning kconsumerd failed");
813 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
814 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
818 /* Setting up the global kconsumerd_pid */
819 kconsumerd_pid
= ret
;
820 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
822 DBG("Kconsumerd pid %d", ret
);
824 DBG("Spawning kconsumerd thread");
825 ret
= spawn_kconsumerd_thread();
827 ERR("Fatal error spawning kconsumerd thread");
832 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
840 * modprobe_kernel_modules
842 static int modprobe_kernel_modules(void)
847 while (kernel_modules_list
[i
] != NULL
) {
848 ret
= snprintf(modprobe
, sizeof(modprobe
), "/sbin/modprobe %s",
849 kernel_modules_list
[i
]);
851 perror("snprintf modprobe");
854 ret
= system(modprobe
);
856 ERR("Unable to load module %s", kernel_modules_list
[i
]);
858 DBG("Modprobe successfully %s", kernel_modules_list
[i
]);
869 static int mount_debugfs(char *path
)
872 char *type
= "debugfs";
874 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
);
879 ret
= mount(type
, path
, type
, 0, NULL
);
881 perror("mount debugfs");
885 DBG("Mounted debugfs successfully at %s", path
);
894 * Setup necessary data for kernel tracer action.
896 static void init_kernel_tracer(void)
899 char *proc_mounts
= "/proc/mounts";
901 char *debugfs_path
= NULL
, *lttng_path
;
905 fp
= fopen(proc_mounts
, "r");
907 ERR("Unable to probe %s", proc_mounts
);
911 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
912 if (strstr(line
, "debugfs") != NULL
) {
913 /* Remove first string */
915 /* Dup string here so we can reuse line later on */
916 debugfs_path
= strdup(strtok(NULL
, " "));
917 DBG("Got debugfs path : %s", debugfs_path
);
924 /* Mount debugfs if needded */
925 if (debugfs_path
== NULL
) {
926 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
928 perror("asprintf debugfs path");
931 ret
= mount_debugfs(debugfs_path
);
937 /* Modprobe lttng kernel modules */
938 ret
= modprobe_kernel_modules();
943 /* Setup lttng kernel path */
944 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
946 perror("asprintf lttng path");
950 /* Open debugfs lttng */
951 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
952 if (kernel_tracer_fd
< 0) {
953 DBG("Failed to open %s", lttng_path
);
959 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
969 WARN("No kernel tracer available");
970 kernel_tracer_fd
= 0;
977 * Start tracing by creating trace directory and sending FDs to the kernel
980 static int start_kernel_trace(struct ltt_kernel_session
*session
)
984 if (session
->kconsumer_fds_sent
== 0) {
985 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, session
);
987 ERR("Send kconsumerd fds failed");
988 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
992 session
->kconsumer_fds_sent
= 1;
1000 * Notify kernel thread to update it's pollfd.
1002 static int notify_kernel_pollfd(void)
1006 /* Inform kernel thread of the new kernel channel */
1007 ret
= write(kernel_poll_pipe
[1], "!", 1);
1009 perror("write kernel poll pipe");
1016 * init_default_channel
1018 * Allocate a channel structure and fill it.
1020 static struct lttng_channel
*init_default_channel(void)
1022 struct lttng_channel
*chan
;
1024 chan
= malloc(sizeof(struct lttng_channel
));
1026 perror("init channel malloc");
1030 if (snprintf(chan
->name
, NAME_MAX
, DEFAULT_CHANNEL_NAME
) < 0) {
1031 perror("snprintf defautl channel name");
1035 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1036 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
1037 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
1038 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1039 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1040 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1047 * create_kernel_session
1049 * Create a kernel tracer session then create the default channel.
1051 static int create_kernel_session(struct ltt_session
*session
)
1054 struct lttng_channel
*chan
;
1056 DBG("Creating kernel session");
1058 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1060 ret
= LTTCOMM_KERN_SESS_FAIL
;
1064 chan
= init_default_channel();
1066 ret
= LTTCOMM_FATAL
;
1070 ret
= mkdir_recursive(session
->path
, S_IRWXU
| S_IRWXG
);
1072 if (ret
!= EEXIST
) {
1073 ERR("Trace directory creation error");
1078 DBG("Creating default kernel channel %s", DEFAULT_CHANNEL_NAME
);
1080 ret
= kernel_create_channel(session
->kernel_session
, chan
, session
->path
);
1082 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1086 ret
= notify_kernel_pollfd();
1093 * process_client_msg
1095 * Process the command requested by the lttng client within the command
1096 * context structure. This function make sure that the return structure (llm)
1097 * is set and ready for transmission before returning.
1099 * Return any error encountered or 0 for success.
1101 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1105 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1107 /* Listing commands don't need a session */
1108 switch (cmd_ctx
->lsm
->cmd_type
) {
1109 case LTTNG_CREATE_SESSION
:
1110 case LTTNG_LIST_SESSIONS
:
1111 case LTTNG_LIST_EVENTS
:
1112 case LTTNG_KERNEL_LIST_EVENTS
:
1113 case LTTNG_LIST_TRACEABLE_APPS
:
1116 DBG("Getting session %s by name", cmd_ctx
->lsm
->session_name
);
1117 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session_name
);
1118 if (cmd_ctx
->session
== NULL
) {
1119 /* If session name not found */
1120 if (cmd_ctx
->lsm
->session_name
!= NULL
) {
1121 ret
= LTTCOMM_SESS_NOT_FOUND
;
1122 } else { /* If no session name specified */
1123 ret
= LTTCOMM_SELECT_SESS
;
1131 * Check kernel command for kernel session.
1133 switch (cmd_ctx
->lsm
->cmd_type
) {
1134 case LTTNG_KERNEL_ADD_CONTEXT
:
1135 case LTTNG_KERNEL_CREATE_CHANNEL
:
1136 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
1137 case LTTNG_KERNEL_DISABLE_CHANNEL
:
1138 case LTTNG_KERNEL_DISABLE_EVENT
:
1139 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
1140 case LTTNG_KERNEL_ENABLE_CHANNEL
:
1141 case LTTNG_KERNEL_ENABLE_EVENT
:
1142 case LTTNG_KERNEL_LIST_EVENTS
:
1143 /* Kernel tracer check */
1144 if (kernel_tracer_fd
== 0) {
1145 init_kernel_tracer();
1146 if (kernel_tracer_fd
== 0) {
1147 ret
= LTTCOMM_KERN_NA
;
1152 /* Need a session for kernel command */
1153 if (cmd_ctx
->lsm
->cmd_type
!= LTTNG_KERNEL_LIST_EVENTS
&&
1154 cmd_ctx
->session
->kernel_session
== NULL
) {
1156 ret
= create_kernel_session(cmd_ctx
->session
);
1158 ret
= LTTCOMM_KERN_SESS_FAIL
;
1162 /* Start the kernel consumer daemon */
1163 if (kconsumerd_pid
== 0) {
1164 ret
= start_kconsumerd();
1172 /* Connect to ust apps if available pid */
1173 if (cmd_ctx
->lsm
->pid
> 0) {
1174 /* Connect to app using ustctl API */
1175 cmd_ctx
->ust_sock
= ust_connect_app(cmd_ctx
->lsm
->pid
);
1176 if (cmd_ctx
->ust_sock
< 0) {
1177 ret
= LTTCOMM_NO_TRACEABLE
;
1182 /* Process by command type */
1183 switch (cmd_ctx
->lsm
->cmd_type
) {
1184 case LTTNG_KERNEL_ADD_CONTEXT
:
1186 int found
= 0, no_event
= 0;
1187 struct ltt_kernel_channel
*chan
;
1188 struct ltt_kernel_event
*event
;
1190 /* Setup lttng message with no payload */
1191 ret
= setup_lttng_msg(cmd_ctx
, 0);
1196 /* Check if event name is given */
1197 if (strlen(cmd_ctx
->lsm
->u
.context
.event_name
) == 0) {
1201 if (strlen(cmd_ctx
->lsm
->u
.context
.channel_name
) == 0) {
1202 /* Go over all channels */
1203 DBG("Adding context to all channels");
1204 cds_list_for_each_entry(chan
,
1205 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1207 ret
= kernel_add_channel_context(chan
,
1208 &cmd_ctx
->lsm
->u
.context
.ctx
);
1213 event
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.context
.event_name
, chan
);
1214 if (event
!= NULL
) {
1215 ret
= kernel_add_event_context(event
,
1216 &cmd_ctx
->lsm
->u
.context
.ctx
);
1218 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1227 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.context
.channel_name
,
1228 cmd_ctx
->session
->kernel_session
);
1230 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1235 ret
= kernel_add_channel_context(chan
,
1236 &cmd_ctx
->lsm
->u
.context
.ctx
);
1238 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1242 event
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.context
.event_name
, chan
);
1243 if (event
!= NULL
) {
1244 ret
= kernel_add_event_context(event
,
1245 &cmd_ctx
->lsm
->u
.context
.ctx
);
1247 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1254 if (!found
&& !no_event
) {
1255 ret
= LTTCOMM_NO_EVENT
;
1262 case LTTNG_KERNEL_CREATE_CHANNEL
:
1264 /* Setup lttng message with no payload */
1265 ret
= setup_lttng_msg(cmd_ctx
, 0);
1271 DBG("Creating kernel channel");
1273 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1274 &cmd_ctx
->lsm
->u
.channel
.chan
, cmd_ctx
->session
->path
);
1276 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1280 ret
= notify_kernel_pollfd();
1282 ret
= LTTCOMM_FATAL
;
1289 case LTTNG_KERNEL_DISABLE_CHANNEL
:
1291 struct ltt_kernel_channel
*chan
;
1293 /* Setup lttng message with no payload */
1294 ret
= setup_lttng_msg(cmd_ctx
, 0);
1299 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1300 cmd_ctx
->session
->kernel_session
);
1302 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1304 } else if (chan
->enabled
== 1) {
1305 ret
= kernel_disable_channel(chan
);
1307 if (ret
!= EEXIST
) {
1308 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1314 kernel_wait_quiescent(kernel_tracer_fd
);
1318 case LTTNG_KERNEL_DISABLE_EVENT
:
1320 struct ltt_kernel_channel
*chan
;
1321 struct ltt_kernel_event
*ev
;
1323 /* Setup lttng message with no payload */
1324 ret
= setup_lttng_msg(cmd_ctx
, 0);
1329 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1330 cmd_ctx
->session
->kernel_session
);
1332 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1336 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, chan
);
1338 DBG("Disabling kernel event %s for channel %s.",
1339 cmd_ctx
->lsm
->u
.disable
.name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
1340 ret
= kernel_disable_event(ev
);
1342 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1347 kernel_wait_quiescent(kernel_tracer_fd
);
1351 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
1353 struct ltt_kernel_channel
*chan
;
1354 struct ltt_kernel_event
*ev
;
1356 /* Setup lttng message with no payload */
1357 ret
= setup_lttng_msg(cmd_ctx
, 0);
1362 DBG("Disabling all enabled kernel events");
1364 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1365 cmd_ctx
->session
->kernel_session
);
1367 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1371 /* For each event in the kernel session */
1372 cds_list_for_each_entry(ev
, &chan
->events_list
.head
, list
) {
1373 DBG("Disabling kernel event %s for channel %s.",
1374 ev
->event
->name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
1375 ret
= kernel_disable_event(ev
);
1381 /* Quiescent wait after event disable */
1382 kernel_wait_quiescent(kernel_tracer_fd
);
1386 case LTTNG_KERNEL_ENABLE_CHANNEL
:
1388 struct ltt_kernel_channel
*chan
;
1390 /* Setup lttng message with no payload */
1391 ret
= setup_lttng_msg(cmd_ctx
, 0);
1396 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1397 cmd_ctx
->session
->kernel_session
);
1399 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1401 } else if (chan
->enabled
== 0) {
1402 ret
= kernel_enable_channel(chan
);
1404 if (ret
!= EEXIST
) {
1405 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1411 kernel_wait_quiescent(kernel_tracer_fd
);
1415 case LTTNG_KERNEL_ENABLE_EVENT
:
1417 struct ltt_kernel_channel
*chan
;
1418 struct ltt_kernel_event
*ev
;
1420 /* Setup lttng message with no payload */
1421 ret
= setup_lttng_msg(cmd_ctx
, 0);
1426 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1427 cmd_ctx
->session
->kernel_session
);
1429 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1433 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
);
1435 DBG("Creating kernel event %s for channel %s.",
1436 cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
->channel
->name
);
1437 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, chan
);
1439 DBG("Enabling kernel event %s for channel %s.",
1440 cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
->channel
->name
);
1441 ret
= kernel_enable_event(ev
);
1445 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1449 kernel_wait_quiescent(kernel_tracer_fd
);
1453 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
1456 char *event_list
, *event
, *ptr
;
1457 struct ltt_kernel_channel
*chan
;
1458 struct ltt_kernel_event
*ev
;
1459 struct lttng_event ev_attr
;
1461 /* Setup lttng message with no payload */
1462 ret
= setup_lttng_msg(cmd_ctx
, 0);
1467 DBG("Enabling all kernel event");
1469 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1470 cmd_ctx
->session
->kernel_session
);
1472 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1476 /* For each event in the kernel session */
1477 cds_list_for_each_entry(ev
, &chan
->events_list
.head
, list
) {
1478 DBG("Enabling kernel event %s for channel %s.",
1479 ev
->event
->name
, chan
->channel
->name
);
1480 ret
= kernel_enable_event(ev
);
1486 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1488 ret
= LTTCOMM_KERN_LIST_FAIL
;
1493 while ((size
= sscanf(ptr
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
1494 ev
= get_kernel_event_by_name(event
, chan
);
1496 strncpy(ev_attr
.name
, event
, LTTNG_SYM_NAME_LEN
);
1497 /* Default event type for enable all */
1498 ev_attr
.type
= LTTNG_EVENT_TRACEPOINTS
;
1499 /* Enable each single tracepoint event */
1500 ret
= kernel_create_event(&ev_attr
, chan
);
1502 /* Ignore error here and continue */
1506 /* Move pointer to the next line */
1513 /* Quiescent wait after event enable */
1514 kernel_wait_quiescent(kernel_tracer_fd
);
1518 case LTTNG_KERNEL_LIST_EVENTS
:
1523 DBG("Listing kernel events");
1525 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1527 ret
= LTTCOMM_KERN_LIST_FAIL
;
1532 * Setup lttng message with payload size set to the event list size in
1533 * bytes and then copy list into the llm payload.
1535 ret
= setup_lttng_msg(cmd_ctx
, size
);
1540 /* Copy event list into message payload */
1541 memcpy(cmd_ctx
->llm
->payload
, event_list
, size
);
1548 case LTTNG_START_TRACE
:
1550 struct ltt_kernel_channel
*chan
;
1552 /* Setup lttng message with no payload */
1553 ret
= setup_lttng_msg(cmd_ctx
, 0);
1558 /* Kernel tracing */
1559 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1560 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
1561 DBG("Open kernel metadata");
1562 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
1563 cmd_ctx
->session
->path
);
1565 ret
= LTTCOMM_KERN_META_FAIL
;
1570 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1571 DBG("Opening kernel metadata stream");
1572 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1573 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
1575 ERR("Kernel create metadata stream failed");
1576 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1582 /* For each channel */
1583 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1584 if (chan
->stream_count
== 0) {
1585 ret
= kernel_open_channel_stream(chan
);
1587 ERR("Kernel create channel stream failed");
1588 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1591 /* Update the stream global counter */
1592 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
1596 DBG("Start kernel tracing");
1597 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
1599 ERR("Kernel start session failed");
1600 ret
= LTTCOMM_KERN_START_FAIL
;
1604 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
1606 ret
= LTTCOMM_KERN_START_FAIL
;
1610 /* Quiescent wait after starting trace */
1611 kernel_wait_quiescent(kernel_tracer_fd
);
1614 /* TODO: Start all UST traces */
1619 case LTTNG_STOP_TRACE
:
1621 struct ltt_kernel_channel
*chan
;
1622 /* Setup lttng message with no payload */
1623 ret
= setup_lttng_msg(cmd_ctx
, 0);
1629 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1630 DBG("Stop kernel tracing");
1632 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
1634 ERR("Kernel metadata flush failed");
1637 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1638 ret
= kernel_flush_buffer(chan
);
1640 ERR("Kernel flush buffer error");
1644 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
1646 ERR("Kernel stop session failed");
1647 ret
= LTTCOMM_KERN_STOP_FAIL
;
1651 /* Quiescent wait after stopping trace */
1652 kernel_wait_quiescent(kernel_tracer_fd
);
1655 /* TODO : User-space tracer */
1660 case LTTNG_CREATE_SESSION
:
1662 /* Setup lttng message with no payload */
1663 ret
= setup_lttng_msg(cmd_ctx
, 0);
1668 ret
= create_session(cmd_ctx
->lsm
->session_name
, cmd_ctx
->lsm
->path
);
1670 if (ret
== -EEXIST
) {
1671 ret
= LTTCOMM_EXIST_SESS
;
1673 ret
= LTTCOMM_FATAL
;
1681 case LTTNG_DESTROY_SESSION
:
1683 /* Setup lttng message with no payload */
1684 ret
= setup_lttng_msg(cmd_ctx
, 0);
1689 /* Clean kernel session teardown */
1690 teardown_kernel_session(cmd_ctx
->session
);
1692 ret
= destroy_session(cmd_ctx
->lsm
->session_name
);
1694 ret
= LTTCOMM_FATAL
;
1699 * Must notify the kernel thread here to update it's pollfd in order to
1700 * remove the channel(s)' fd just destroyed.
1702 ret
= notify_kernel_pollfd();
1704 ret
= LTTCOMM_FATAL
;
1712 case LTTNG_LIST_TRACES:
1714 unsigned int trace_count;
1716 trace_count = get_trace_count_per_session(cmd_ctx->session);
1717 if (trace_count == 0) {
1718 ret = LTTCOMM_NO_TRACE;
1722 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count);
1727 get_traces_per_session(cmd_ctx->session,
1728 (struct lttng_trace *)(cmd_ctx->llm->payload));
1735 case UST_CREATE_TRACE:
1737 ret = setup_lttng_msg(cmd_ctx, 0);
1742 ret = ust_create_trace(cmd_ctx);
1749 case LTTNG_LIST_TRACEABLE_APPS
:
1751 unsigned int app_count
;
1753 app_count
= get_app_count();
1754 DBG("Traceable application count : %d", app_count
);
1755 if (app_count
== 0) {
1756 ret
= LTTCOMM_NO_APPS
;
1760 ret
= setup_lttng_msg(cmd_ctx
, sizeof(pid_t
) * app_count
);
1765 get_app_list_pids((pid_t
*)(cmd_ctx
->llm
->payload
));
1771 case UST_START_TRACE:
1773 ret = setup_lttng_msg(cmd_ctx, 0);
1778 ret = ust_start_trace(cmd_ctx);
1784 case UST_STOP_TRACE:
1786 ret = setup_lttng_msg(cmd_ctx, 0);
1791 ret = ust_stop_trace(cmd_ctx);
1798 case LTTNG_LIST_SESSIONS
:
1800 unsigned int session_count
;
1802 session_count
= get_session_count();
1803 if (session_count
== 0) {
1804 ret
= LTTCOMM_NO_SESSION
;
1808 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * session_count
);
1813 get_lttng_session((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
1819 /* Undefined command */
1820 ret
= setup_lttng_msg(cmd_ctx
, 0);
1829 /* Set return code */
1830 cmd_ctx
->llm
->ret_code
= ret
;
1835 if (cmd_ctx
->llm
== NULL
) {
1836 DBG("Missing llm structure. Allocating one.");
1837 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
1841 /* Notify client of error */
1842 cmd_ctx
->llm
->ret_code
= ret
;
1849 * thread_manage_clients
1851 * This thread manage all clients request using the unix
1852 * client socket for communication.
1854 static void *thread_manage_clients(void *data
)
1857 struct command_ctx
*cmd_ctx
;
1859 DBG("[thread] Manage client started");
1861 ret
= lttcomm_listen_unix_sock(client_sock
);
1866 /* Notify parent pid that we are ready
1867 * to accept command for client side.
1869 if (opt_sig_parent
) {
1870 kill(ppid
, SIGCHLD
);
1874 /* Blocking call, waiting for transmission */
1875 DBG("Accepting client command ...");
1876 sock
= lttcomm_accept_unix_sock(client_sock
);
1881 /* Allocate context command to process the client request */
1882 cmd_ctx
= malloc(sizeof(struct command_ctx
));
1884 /* Allocate data buffer for reception */
1885 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
1886 cmd_ctx
->llm
= NULL
;
1887 cmd_ctx
->session
= NULL
;
1890 * Data is received from the lttng client. The struct
1891 * lttcomm_session_msg (lsm) contains the command and data request of
1894 DBG("Receiving data from client ...");
1895 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
1900 // TODO: Validate cmd_ctx including sanity check for security purpose.
1903 * This function dispatch the work to the kernel or userspace tracer
1904 * libs and fill the lttcomm_lttng_msg data structure of all the needed
1905 * informations for the client. The command context struct contains
1906 * everything this function may needs.
1908 ret
= process_client_msg(cmd_ctx
);
1910 /* TODO: Inform client somehow of the fatal error. At this point,
1911 * ret < 0 means that a malloc failed (ENOMEM). */
1912 /* Error detected but still accept command */
1913 clean_command_ctx(cmd_ctx
);
1917 DBG("Sending response (size: %d, retcode: %d)",
1918 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
1919 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
1921 ERR("Failed to send data back to client");
1924 clean_command_ctx(cmd_ctx
);
1926 /* End of transmission */
1936 * usage function on stderr
1938 static void usage(void)
1940 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
1941 fprintf(stderr
, " -h, --help Display this usage.\n");
1942 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
1943 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
1944 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
1945 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
1946 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
1947 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
1948 fprintf(stderr
, " -V, --version Show version number.\n");
1949 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
1950 fprintf(stderr
, " -q, --quiet No output at all.\n");
1951 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
1955 * daemon argument parsing
1957 static int parse_args(int argc
, char **argv
)
1961 static struct option long_options
[] = {
1962 { "client-sock", 1, 0, 'c' },
1963 { "apps-sock", 1, 0, 'a' },
1964 { "kconsumerd-cmd-sock", 1, 0, 0 },
1965 { "kconsumerd-err-sock", 1, 0, 0 },
1966 { "daemonize", 0, 0, 'd' },
1967 { "sig-parent", 0, 0, 'S' },
1968 { "help", 0, 0, 'h' },
1969 { "group", 1, 0, 'g' },
1970 { "version", 0, 0, 'V' },
1971 { "quiet", 0, 0, 'q' },
1972 { "verbose", 0, 0, 'v' },
1977 int option_index
= 0;
1978 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
1985 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
1987 fprintf(stderr
, " with arg %s\n", optarg
);
1991 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1994 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2000 opt_tracing_group
= strdup(optarg
);
2006 fprintf(stdout
, "%s\n", VERSION
);
2012 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2015 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2024 /* Unknown option or other error.
2025 * Error is printed by getopt, just return */
2034 * init_daemon_socket
2036 * Creates the two needed socket by the daemon.
2037 * apps_sock - The communication socket for all UST apps.
2038 * client_sock - The communication of the cli tool (lttng).
2040 static int init_daemon_socket()
2045 old_umask
= umask(0);
2047 /* Create client tool unix socket */
2048 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2049 if (client_sock
< 0) {
2050 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2055 /* File permission MUST be 660 */
2056 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2058 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2063 /* Create the application unix socket */
2064 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2065 if (apps_sock
< 0) {
2066 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2071 /* File permission MUST be 666 */
2072 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2074 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2085 * check_existing_daemon
2087 * Check if the global socket is available.
2088 * If yes, error is returned.
2090 static int check_existing_daemon()
2094 ret
= access(client_unix_sock_path
, F_OK
);
2096 ret
= access(apps_unix_sock_path
, F_OK
);
2105 * Set the tracing group gid onto the client socket.
2107 * Race window between mkdir and chown is OK because we are going from
2108 * more permissive (root.root) to les permissive (root.tracing).
2110 static int set_permissions(void)
2115 /* Decide which group name to use */
2116 (opt_tracing_group
!= NULL
) ?
2117 (grp
= getgrnam(opt_tracing_group
)) :
2118 (grp
= getgrnam(default_tracing_group
));
2122 WARN("No tracing group detected");
2125 ERR("Missing tracing group. Aborting execution.");
2131 /* Set lttng run dir */
2132 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
2134 ERR("Unable to set group on " LTTNG_RUNDIR
);
2138 /* lttng client socket path */
2139 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
2141 ERR("Unable to set group on %s", client_unix_sock_path
);
2145 /* kconsumerd error socket path */
2146 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
2148 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2152 DBG("All permissions are set");
2159 * create_kernel_poll_pipe
2161 * Create the pipe used to wake up the kernel thread.
2163 static int create_kernel_poll_pipe(void)
2165 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2169 * create_lttng_rundir
2171 * Create the lttng run directory needed for all
2172 * global sockets and pipe.
2174 static int create_lttng_rundir(void)
2178 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2180 if (errno
!= EEXIST
) {
2181 ERR("Unable to create " LTTNG_RUNDIR
);
2193 * set_kconsumerd_sockets
2195 * Setup sockets and directory needed by the kconsumerd
2196 * communication with the session daemon.
2198 static int set_kconsumerd_sockets(void)
2202 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
2203 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
2206 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
2207 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
2210 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
2212 if (errno
!= EEXIST
) {
2213 ERR("Failed to create " KCONSUMERD_PATH
);
2219 /* Create the kconsumerd error unix socket */
2220 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
2221 if (kconsumerd_err_sock
< 0) {
2222 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
2227 /* File permission MUST be 660 */
2228 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2230 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
2242 * Signal handler for the daemon
2244 static void sighandler(int sig
)
2248 DBG("SIGPIPE catched");
2251 DBG("SIGINT catched");
2255 DBG("SIGTERM catched");
2266 * set_signal_handler
2268 * Setup signal handler for :
2269 * SIGINT, SIGTERM, SIGPIPE
2271 static int set_signal_handler(void)
2274 struct sigaction sa
;
2277 if ((ret
= sigemptyset(&sigset
)) < 0) {
2278 perror("sigemptyset");
2282 sa
.sa_handler
= sighandler
;
2283 sa
.sa_mask
= sigset
;
2285 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
2286 perror("sigaction");
2290 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
2291 perror("sigaction");
2295 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
2296 perror("sigaction");
2300 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2308 * Set open files limit to unlimited. This daemon can open a large number of
2309 * file descriptors in order to consumer multiple kernel traces.
2311 static void set_ulimit(void)
2316 lim
.rlim_cur
= 65535;
2317 lim
.rlim_max
= 65535;
2319 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
2321 perror("failed to set open files limit");
2328 int main(int argc
, char **argv
)
2332 const char *home_path
;
2334 /* Parse arguments */
2336 if ((ret
= parse_args(argc
, argv
) < 0)) {
2349 /* Check if daemon is UID = 0 */
2350 is_root
= !getuid();
2353 ret
= create_lttng_rundir();
2358 if (strlen(apps_unix_sock_path
) == 0) {
2359 snprintf(apps_unix_sock_path
, PATH_MAX
,
2360 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
2363 if (strlen(client_unix_sock_path
) == 0) {
2364 snprintf(client_unix_sock_path
, PATH_MAX
,
2365 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
2368 ret
= set_kconsumerd_sockets();
2373 /* Setup kernel tracer */
2374 init_kernel_tracer();
2376 /* Set ulimit for open files */
2379 home_path
= get_home_dir();
2380 if (home_path
== NULL
) {
2381 ERR("Can't get HOME directory for sockets creation.\n \
2382 Please specify --socket PATH.");
2386 if (strlen(apps_unix_sock_path
) == 0) {
2387 snprintf(apps_unix_sock_path
, PATH_MAX
,
2388 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
2391 /* Set the cli tool unix socket path */
2392 if (strlen(client_unix_sock_path
) == 0) {
2393 snprintf(client_unix_sock_path
, PATH_MAX
,
2394 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
2398 DBG("Client socket path %s", client_unix_sock_path
);
2399 DBG("Application socket path %s", apps_unix_sock_path
);
2401 /* See if daemon already exist. If any of the two
2402 * socket needed by the daemon are present, this test fails
2404 if ((ret
= check_existing_daemon()) == 0) {
2405 ERR("Already running daemon.\n");
2406 /* We do not goto error because we must not
2407 * cleanup() because a daemon is already running.
2412 if (set_signal_handler() < 0) {
2416 /* Setup the needed unix socket */
2417 if (init_daemon_socket() < 0) {
2421 /* Set credentials to socket */
2422 if (is_root
&& (set_permissions() < 0)) {
2426 /* Get parent pid if -S, --sig-parent is specified. */
2427 if (opt_sig_parent
) {
2431 /* Setup the kernel pipe for waking up the kernel thread */
2432 if (create_kernel_poll_pipe() < 0) {
2437 /* Create thread to manage the client socket */
2438 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
2440 perror("pthread_create");
2444 /* Create thread to manage application socket */
2445 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
2447 perror("pthread_create");
2451 /* Create kernel thread to manage kernel event */
2452 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
2454 perror("pthread_create");
2458 ret
= pthread_join(client_thread
, &status
);
2460 perror("pthread_join");