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.
25 #include <semaphore.h>
32 #include <sys/socket.h>
34 #include <sys/types.h>
36 #include <sys/resource.h>
39 #include <urcu/list.h> /* URCU list library (-lurcu) */
40 #include <lttng/lttng.h>
42 #include "liblttsessiondcomm.h"
43 #include "ltt-sessiond.h"
45 #include "kernel-ctl.h"
48 #include "traceable-app.h"
49 #include "lttng-kconsumerd.h"
50 #include "libustctl.h"
54 * teardown: signal SIGTERM handler -> write into pipe. Threads waits
55 * with epoll on pipe and on other pipes/sockets for commands. Main
56 * simply waits on pthread join.
60 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
61 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
62 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
63 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
66 int opt_verbose
; /* Not static for lttngerr.h */
67 int opt_quiet
; /* Not static for lttngerr.h */
69 const char *opt_tracing_group
;
70 static int opt_sig_parent
;
71 static int opt_daemon
;
72 static int is_root
; /* Set to 1 if the daemon is running as root */
73 static pid_t ppid
; /* Parent PID for --sig-parent option */
74 static pid_t kconsumerd_pid
;
76 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
77 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
78 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
79 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
82 static int client_sock
;
84 static int kconsumerd_err_sock
;
85 static int kconsumerd_cmd_sock
;
86 static int kernel_tracer_fd
;
88 /* Pthread, Mutexes and Semaphores */
89 static pthread_t kconsumerd_thread
;
90 static pthread_t apps_thread
;
91 static pthread_t client_thread
;
92 static sem_t kconsumerd_sem
;
94 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
97 * teardown_kernel_session
99 * Complete teardown of a kernel session. This free all data structure
100 * related to a kernel session and update counter.
102 static void teardown_kernel_session(struct ltt_session
*session
)
104 if (session
->kernel_session
!= NULL
) {
105 DBG("Tearing down kernel session");
106 trace_destroy_kernel_session(session
->kernel_session
);
107 /* Extra precaution */
108 session
->kernel_session
= NULL
;
109 /* Decrement session count */
110 session
->kern_session_count
--;
117 * Cleanup the daemon on exit
119 static void cleanup()
123 struct ltt_session
*sess
;
128 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
129 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
132 /* Stopping all threads */
133 DBG("Terminating all threads");
134 pthread_cancel(client_thread
);
135 pthread_cancel(apps_thread
);
136 if (kconsumerd_pid
!= 0) {
137 pthread_cancel(kconsumerd_thread
);
140 DBG("Unlinking all unix socket");
141 unlink(client_unix_sock_path
);
142 unlink(apps_unix_sock_path
);
143 unlink(kconsumerd_err_unix_sock_path
);
145 DBG("Removing %s directory", LTTNG_RUNDIR
);
146 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
148 ERR("asprintf failed. Something is really wrong!");
151 /* Remove lttng run directory */
154 ERR("Unable to clean " LTTNG_RUNDIR
);
157 DBG("Cleaning up all session");
158 /* Cleanup ALL session */
159 cds_list_for_each_entry(sess
, <t_session_list
.head
, list
) {
160 teardown_kernel_session(sess
);
161 // TODO complete session cleanup (including UST)
164 DBG("Closing kernel fd");
165 close(kernel_tracer_fd
);
171 * Send data on a unix socket using the liblttsessiondcomm API.
173 * Return lttcomm error code.
175 static int send_unix_sock(int sock
, void *buf
, size_t len
)
177 /* Check valid length */
182 return lttcomm_send_unix_sock(sock
, buf
, len
);
188 * Free memory of a command context structure.
190 static void clean_command_ctx(struct command_ctx
*cmd_ctx
)
192 DBG("Clean command context structure %p", cmd_ctx
);
206 * send_kconsumerd_fds
208 * Send all stream fds of the kernel session to the consumer.
210 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
214 struct ltt_kernel_stream
*stream
;
215 struct ltt_kernel_channel
*chan
;
216 struct lttcomm_kconsumerd_header lkh
;
217 struct lttcomm_kconsumerd_msg lkm
;
219 nb_fd
= session
->stream_count_global
;
222 lkh
.payload_size
= (nb_fd
+ 1) * sizeof(struct lttcomm_kconsumerd_msg
);
223 lkh
.cmd_type
= ADD_STREAM
;
225 DBG("Sending kconsumerd header");
227 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
229 perror("send kconsumerd header");
233 DBG("Sending metadata stream fd");
235 /* Send metadata stream fd first */
236 lkm
.fd
= session
->metadata_stream_fd
;
237 lkm
.state
= ACTIVE_FD
;
238 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
239 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
241 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
243 perror("send kconsumerd fd");
247 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
248 cds_list_for_each_entry(stream
, &chan
->stream_list
.head
, list
) {
250 lkm
.state
= stream
->state
;
251 lkm
.max_sb_size
= chan
->channel
->attr
.subbuf_size
;
252 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
254 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
256 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
258 perror("send kconsumerd fd");
264 DBG("Kconsumerd fds sent");
275 * Create the trace output directory.
277 static int create_trace_dir(struct ltt_kernel_session
*session
)
280 struct ltt_kernel_channel
*chan
;
282 /* Create all channel directories */
283 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
284 DBG("Creating trace directory at %s", chan
->pathname
);
285 // TODO: recursive create dir
286 ret
= mkdir(chan
->pathname
, S_IRWXU
| S_IRWXG
);
289 perror("mkdir trace path");
305 * Return a socket connected to the libust communication socket
306 * of the application identified by the pid.
308 * If the pid is not found in the traceable list,
309 * return -1 to indicate error.
311 static int ust_connect_app(pid_t pid
)
314 struct ltt_traceable_app
*lta
;
316 DBG("Connect to application pid %d", pid
);
318 lta
= find_app_by_pid(pid
);
321 DBG("Application pid %d not found", pid
);
325 sock
= ustctl_connect_pid(lta
->pid
);
327 ERR("Fail connecting to the PID %d", pid
);
336 * Notify apps by writing 42 to a named pipe using name.
337 * Every applications waiting for a ltt-sessiond will be notified
338 * and re-register automatically to the session daemon.
340 * Return open or write error value.
342 static int notify_apps(const char *name
)
347 DBG("Notify the global application pipe");
349 /* Try opening the global pipe */
350 fd
= open(name
, O_WRONLY
);
355 /* Notify by writing on the pipe */
356 ret
= write(fd
, "42", 2);
368 * Setup the outgoing data buffer for the response (llm) by allocating the
369 * right amount of memory and copying the original information from the lsm
372 * Return total size of the buffer pointed by buf.
374 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
380 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
381 if (cmd_ctx
->llm
== NULL
) {
387 /* Copy common data */
388 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
389 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->pid
;
391 cmd_ctx
->llm
->data_size
= size
;
392 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
401 * thread_manage_kconsumerd
403 * This thread manage the kconsumerd error sent
404 * back to the session daemon.
406 static void *thread_manage_kconsumerd(void *data
)
409 enum lttcomm_return_code code
;
411 DBG("[thread] Manage kconsumerd started");
413 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
418 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
423 /* Getting status code from kconsumerd */
424 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
429 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
430 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
431 if (kconsumerd_cmd_sock
< 0) {
432 sem_post(&kconsumerd_sem
);
433 perror("kconsumerd connect");
436 /* Signal condition to tell that the kconsumerd is ready */
437 sem_post(&kconsumerd_sem
);
438 DBG("Kconsumerd command socket ready");
440 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
441 lttcomm_get_readable_code(-code
));
445 /* Wait for any kconsumerd error */
446 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
448 ERR("Kconsumerd closed the command socket");
452 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
456 DBG("Kconsumerd thread dying");
463 * This thread manage the application socket communication
465 static void *thread_manage_apps(void *data
)
469 /* TODO: Something more elegant is needed but fine for now */
470 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
471 * for 32-bit vs 64-bit compat processes. */
472 /* replicate in ust with version number */
474 int reg
; /* 1:register, 0:unregister */
479 DBG("[thread] Manage apps started");
481 ret
= lttcomm_listen_unix_sock(apps_sock
);
486 /* Notify all applications to register */
487 notify_apps(default_global_apps_pipe
);
490 DBG("Accepting application registration");
491 /* Blocking call, waiting for transmission */
492 sock
= lttcomm_accept_unix_sock(apps_sock
);
497 /* Basic recv here to handle the very simple data
498 * that the libust send to register (reg_msg).
500 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
506 /* Add application to the global traceable list */
507 if (reg_msg
.reg
== 1) {
509 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
511 /* register_traceable_app only return an error with
512 * ENOMEM. At this point, we better stop everything.
518 unregister_traceable_app(reg_msg
.pid
);
528 * spawn_kconsumerd_thread
530 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
531 * exec or it will fails.
533 static int spawn_kconsumerd_thread(void)
537 /* Setup semaphore */
538 sem_init(&kconsumerd_sem
, 0, 0);
540 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
542 perror("pthread_create kconsumerd");
546 /* Wait for the kconsumerd thread to be ready */
547 sem_wait(&kconsumerd_sem
);
549 if (kconsumerd_pid
== 0) {
550 ERR("Kconsumerd did not start");
557 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
564 * Fork and exec a kernel consumer daemon (kconsumerd).
566 * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel
567 * file descriptor using the libkernelctl or kernel-ctl functions. So, a
568 * kernel consumer MUST only be spawned before creating a kernel session.
570 * Return pid if successful else -1.
572 static pid_t
spawn_kconsumerd(void)
577 DBG("Spawning kconsumerd");
584 execlp("kconsumerd", "kconsumerd", "--verbose", NULL
);
586 perror("kernel start consumer exec");
589 } else if (pid
> 0) {
593 perror("kernel start consumer fork");
605 * Spawn the kconsumerd daemon and session daemon thread.
607 static int start_kconsumerd(void)
611 pthread_mutex_lock(&kconsumerd_pid_mutex
);
612 if (kconsumerd_pid
!= 0) {
616 ret
= spawn_kconsumerd();
618 ERR("Spawning kconsumerd failed");
619 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
620 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
624 /* Setting up the global kconsumerd_pid */
625 kconsumerd_pid
= ret
;
626 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
628 DBG("Kconsumerd pid %d", ret
);
630 DBG("Spawning kconsumerd thread");
631 ret
= spawn_kconsumerd_thread();
633 ERR("Fatal error spawning kconsumerd thread");
638 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
648 * Setup necessary data for kernel tracer action.
650 static void init_kernel_tracer(void)
652 /* Set the global kernel tracer fd */
653 kernel_tracer_fd
= open(DEFAULT_KERNEL_TRACER_PATH
, O_RDWR
);
654 if (kernel_tracer_fd
< 0) {
655 WARN("No kernel tracer available");
656 kernel_tracer_fd
= 0;
659 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
665 * Start tracing by creating trace directory and sending FDs to the kernel
668 static int start_kernel_trace(struct ltt_kernel_session
*session
)
672 /* Create trace directory */
673 ret
= create_trace_dir(session
);
675 if (ret
== -EEXIST
) {
676 ret
= LTTCOMM_KERN_DIR_EXIST
;
678 ret
= LTTCOMM_KERN_DIR_FAIL
;
683 if (session
->kconsumer_fds_sent
== 0) {
684 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, session
);
686 ERR("Send kconsumerd fds failed");
687 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
691 session
->kconsumer_fds_sent
= 1;
699 * init_default_channel
701 * Allocate a channel structure and fill it.
703 static struct lttng_channel
*init_default_channel(void)
705 struct lttng_channel
*chan
;
707 chan
= malloc(sizeof(struct lttng_channel
));
709 perror("init channel malloc");
713 if (snprintf(chan
->name
, NAME_MAX
, DEFAULT_CHANNEL_NAME
) < 0) {
714 perror("snprintf defautl channel name");
718 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
719 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
720 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
721 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
722 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
729 * create_kernel_session
731 * Create a kernel tracer session then create the default channel.
733 static int create_kernel_session(struct ltt_session
*session
)
736 struct lttng_channel
*chan
;
738 DBG("Creating kernel session");
740 ret
= kernel_create_session(session
, kernel_tracer_fd
);
742 ret
= LTTCOMM_KERN_SESS_FAIL
;
746 chan
= init_default_channel();
752 DBG("Creating default kernel channel %s", DEFAULT_CHANNEL_NAME
);
754 ret
= kernel_create_channel(session
->kernel_session
, chan
);
756 ret
= LTTCOMM_KERN_CHAN_FAIL
;
767 * Process the command requested by the lttng client within the command
768 * context structure. This function make sure that the return structure (llm)
769 * is set and ready for transmission before returning.
771 * Return any error encountered or 0 for success.
773 static int process_client_msg(struct command_ctx
*cmd_ctx
)
777 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
779 /* Listing commands don't need a session */
780 switch (cmd_ctx
->lsm
->cmd_type
) {
781 case LTTNG_CREATE_SESSION
:
782 case LTTNG_LIST_SESSIONS
:
783 case LTTNG_LIST_EVENTS
:
784 case LTTNG_KERNEL_LIST_EVENTS
:
785 case LTTNG_LIST_TRACEABLE_APPS
:
788 DBG("Getting session %s by name", cmd_ctx
->lsm
->session_name
);
789 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session_name
);
790 if (cmd_ctx
->session
== NULL
) {
791 /* If session name not found */
792 if (cmd_ctx
->lsm
->session_name
!= NULL
) {
793 ret
= LTTCOMM_SESS_NOT_FOUND
;
794 } else { /* If no session name specified */
795 ret
= LTTCOMM_SELECT_SESS
;
803 * Check kernel command for kernel session.
805 switch (cmd_ctx
->lsm
->cmd_type
) {
806 case LTTNG_KERNEL_CREATE_CHANNEL
:
807 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
808 case LTTNG_KERNEL_DISABLE_CHANNEL
:
809 case LTTNG_KERNEL_DISABLE_EVENT
:
810 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
811 case LTTNG_KERNEL_ENABLE_CHANNEL
:
812 case LTTNG_KERNEL_ENABLE_EVENT
:
813 case LTTNG_KERNEL_LIST_EVENTS
:
814 /* Kernel tracer check */
815 if (kernel_tracer_fd
== 0) {
816 init_kernel_tracer();
817 if (kernel_tracer_fd
== 0) {
818 ret
= LTTCOMM_KERN_NA
;
823 /* Need a session for kernel command */
824 if (cmd_ctx
->lsm
->cmd_type
!= LTTNG_KERNEL_LIST_EVENTS
&&
825 cmd_ctx
->session
->kernel_session
== NULL
) {
827 ret
= create_kernel_session(cmd_ctx
->session
);
829 ret
= LTTCOMM_KERN_SESS_FAIL
;
833 /* Start the kernel consumer daemon */
834 if (kconsumerd_pid
== 0) {
835 ret
= start_kconsumerd();
843 /* Connect to ust apps if available pid */
844 if (cmd_ctx
->lsm
->pid
> 0) {
845 /* Connect to app using ustctl API */
846 cmd_ctx
->ust_sock
= ust_connect_app(cmd_ctx
->lsm
->pid
);
847 if (cmd_ctx
->ust_sock
< 0) {
848 ret
= LTTCOMM_NO_TRACEABLE
;
853 /* Process by command type */
854 switch (cmd_ctx
->lsm
->cmd_type
) {
855 case LTTNG_KERNEL_CREATE_CHANNEL
:
857 /* Setup lttng message with no payload */
858 ret
= setup_lttng_msg(cmd_ctx
, 0);
864 DBG("Creating kernel channel");
866 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
867 &cmd_ctx
->lsm
->u
.channel
.chan
);
869 ret
= LTTCOMM_KERN_CHAN_FAIL
;
876 case LTTNG_KERNEL_DISABLE_CHANNEL
:
878 struct ltt_kernel_channel
*chan
;
880 /* Setup lttng message with no payload */
881 ret
= setup_lttng_msg(cmd_ctx
, 0);
886 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
887 cmd_ctx
->session
->kernel_session
);
889 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
891 } else if (chan
->enabled
== 1) {
892 ret
= kernel_disable_channel(chan
);
895 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
901 kernel_wait_quiescent(kernel_tracer_fd
);
905 case LTTNG_KERNEL_DISABLE_EVENT
:
907 struct ltt_kernel_channel
*chan
;
908 struct ltt_kernel_event
*ev
;
910 /* Setup lttng message with no payload */
911 ret
= setup_lttng_msg(cmd_ctx
, 0);
916 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
917 cmd_ctx
->session
->kernel_session
);
919 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
923 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, chan
);
925 DBG("Disabling kernel event %s for channel %s.",
926 cmd_ctx
->lsm
->u
.disable
.name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
927 ret
= kernel_disable_event(ev
);
929 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
934 kernel_wait_quiescent(kernel_tracer_fd
);
938 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
940 struct ltt_kernel_channel
*chan
;
941 struct ltt_kernel_event
*ev
;
943 /* Setup lttng message with no payload */
944 ret
= setup_lttng_msg(cmd_ctx
, 0);
949 DBG("Disabling all enabled kernel events");
951 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
952 cmd_ctx
->session
->kernel_session
);
954 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
958 /* For each event in the kernel session */
959 cds_list_for_each_entry(ev
, &chan
->events_list
.head
, list
) {
960 DBG("Disabling kernel event %s for channel %s.",
961 ev
->event
->name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
962 ret
= kernel_disable_event(ev
);
968 /* Quiescent wait after event disable */
969 kernel_wait_quiescent(kernel_tracer_fd
);
973 case LTTNG_KERNEL_ENABLE_CHANNEL
:
975 struct ltt_kernel_channel
*chan
;
977 /* Setup lttng message with no payload */
978 ret
= setup_lttng_msg(cmd_ctx
, 0);
983 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
984 cmd_ctx
->session
->kernel_session
);
986 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
988 } else if (chan
->enabled
== 0) {
989 ret
= kernel_enable_channel(chan
);
992 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
998 kernel_wait_quiescent(kernel_tracer_fd
);
1002 case LTTNG_KERNEL_ENABLE_EVENT
:
1004 struct ltt_kernel_channel
*chan
;
1005 struct ltt_kernel_event
*ev
;
1007 /* Setup lttng message with no payload */
1008 ret
= setup_lttng_msg(cmd_ctx
, 0);
1013 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1014 cmd_ctx
->session
->kernel_session
);
1016 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1020 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
);
1022 DBG("Creating kernel event %s for channel %s.",
1023 cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
->channel
->name
);
1024 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, chan
);
1026 DBG("Enabling kernel event %s for channel %s.",
1027 cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
->channel
->name
);
1028 ret
= kernel_enable_event(ev
);
1032 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1036 kernel_wait_quiescent(kernel_tracer_fd
);
1040 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
1043 char *event_list
, *event
, *ptr
;
1044 struct ltt_kernel_channel
*chan
;
1045 struct ltt_kernel_event
*ev
;
1046 struct lttng_event ev_attr
;
1048 /* Setup lttng message with no payload */
1049 ret
= setup_lttng_msg(cmd_ctx
, 0);
1054 DBG("Enabling all kernel event");
1056 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1057 cmd_ctx
->session
->kernel_session
);
1059 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1063 /* For each event in the kernel session */
1064 cds_list_for_each_entry(ev
, &chan
->events_list
.head
, list
) {
1065 DBG("Enabling kernel event %s for channel %s.",
1066 ev
->event
->name
, chan
->channel
->name
);
1067 ret
= kernel_enable_event(ev
);
1073 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1075 ret
= LTTCOMM_KERN_LIST_FAIL
;
1080 while ((size
= sscanf(ptr
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
1081 ev
= get_kernel_event_by_name(event
, chan
);
1083 strncpy(ev_attr
.name
, event
, LTTNG_SYM_NAME_LEN
);
1084 /* Default event type for enable all */
1085 ev_attr
.type
= LTTNG_EVENT_TRACEPOINTS
;
1086 /* Enable each single tracepoint event */
1087 ret
= kernel_create_event(&ev_attr
, chan
);
1089 /* Ignore error here and continue */
1094 /* Move pointer to the next line */
1101 /* Quiescent wait after event enable */
1102 kernel_wait_quiescent(kernel_tracer_fd
);
1106 case LTTNG_KERNEL_LIST_EVENTS
:
1111 DBG("Listing kernel events");
1113 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1115 ret
= LTTCOMM_KERN_LIST_FAIL
;
1120 * Setup lttng message with payload size set to the event list size in
1121 * bytes and then copy list into the llm payload.
1123 ret
= setup_lttng_msg(cmd_ctx
, size
);
1128 /* Copy event list into message payload */
1129 memcpy(cmd_ctx
->llm
->payload
, event_list
, size
);
1136 case LTTNG_START_TRACE
:
1138 struct ltt_kernel_channel
*chan
;
1140 /* Setup lttng message with no payload */
1141 ret
= setup_lttng_msg(cmd_ctx
, 0);
1146 /* Kernel tracing */
1147 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1148 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
1149 DBG("Open kernel metadata");
1150 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
);
1152 ret
= LTTCOMM_KERN_META_FAIL
;
1157 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1158 DBG("Opening kernel metadata stream");
1159 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1160 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
1162 ERR("Kernel create metadata stream failed");
1163 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1169 /* For each channel */
1170 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1171 if (chan
->stream_count
== 0) {
1172 ret
= kernel_open_channel_stream(chan
);
1174 ERR("Kernel create channel stream failed");
1175 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1178 /* Update the stream global counter */
1179 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
1183 DBG("Start kernel tracing");
1184 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
1186 ERR("Kernel start session failed");
1187 ret
= LTTCOMM_KERN_START_FAIL
;
1191 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
1193 ret
= LTTCOMM_KERN_START_FAIL
;
1197 /* Quiescent wait after starting trace */
1198 kernel_wait_quiescent(kernel_tracer_fd
);
1201 /* TODO: Start all UST traces */
1206 case LTTNG_STOP_TRACE
:
1208 struct ltt_kernel_channel
*chan
;
1209 /* Setup lttng message with no payload */
1210 ret
= setup_lttng_msg(cmd_ctx
, 0);
1216 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1217 DBG("Stop kernel tracing");
1219 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
1221 ERR("Kernel metadata flush failed");
1224 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1225 ret
= kernel_flush_buffer(chan
);
1227 ERR("Kernel flush buffer error");
1231 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
1233 ERR("Kernel stop session failed");
1234 ret
= LTTCOMM_KERN_STOP_FAIL
;
1238 /* Quiescent wait after stopping trace */
1239 kernel_wait_quiescent(kernel_tracer_fd
);
1242 /* TODO : User-space tracer */
1247 case LTTNG_CREATE_SESSION
:
1249 /* Setup lttng message with no payload */
1250 ret
= setup_lttng_msg(cmd_ctx
, 0);
1255 ret
= create_session(cmd_ctx
->lsm
->session_name
, cmd_ctx
->lsm
->path
);
1257 if (ret
== -EEXIST
) {
1258 ret
= LTTCOMM_EXIST_SESS
;
1260 ret
= LTTCOMM_FATAL
;
1268 case LTTNG_DESTROY_SESSION
:
1270 /* Setup lttng message with no payload */
1271 ret
= setup_lttng_msg(cmd_ctx
, 0);
1276 /* Clean kernel session teardown */
1277 teardown_kernel_session(cmd_ctx
->session
);
1279 ret
= destroy_session(cmd_ctx
->lsm
->session_name
);
1281 ret
= LTTCOMM_FATAL
;
1289 case LTTNG_LIST_TRACES:
1291 unsigned int trace_count;
1293 trace_count = get_trace_count_per_session(cmd_ctx->session);
1294 if (trace_count == 0) {
1295 ret = LTTCOMM_NO_TRACE;
1299 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count);
1304 get_traces_per_session(cmd_ctx->session,
1305 (struct lttng_trace *)(cmd_ctx->llm->payload));
1312 case UST_CREATE_TRACE:
1314 ret = setup_lttng_msg(cmd_ctx, 0);
1319 ret = ust_create_trace(cmd_ctx);
1326 case LTTNG_LIST_TRACEABLE_APPS
:
1328 unsigned int app_count
;
1330 app_count
= get_app_count();
1331 DBG("Traceable application count : %d", app_count
);
1332 if (app_count
== 0) {
1333 ret
= LTTCOMM_NO_APPS
;
1337 ret
= setup_lttng_msg(cmd_ctx
, sizeof(pid_t
) * app_count
);
1342 get_app_list_pids((pid_t
*)(cmd_ctx
->llm
->payload
));
1348 case UST_START_TRACE:
1350 ret = setup_lttng_msg(cmd_ctx, 0);
1355 ret = ust_start_trace(cmd_ctx);
1361 case UST_STOP_TRACE:
1363 ret = setup_lttng_msg(cmd_ctx, 0);
1368 ret = ust_stop_trace(cmd_ctx);
1375 case LTTNG_LIST_SESSIONS
:
1377 unsigned int session_count
;
1379 session_count
= get_session_count();
1380 if (session_count
== 0) {
1381 ret
= LTTCOMM_NO_SESSION
;
1385 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * session_count
);
1390 get_lttng_session((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
1396 /* Undefined command */
1397 ret
= setup_lttng_msg(cmd_ctx
, 0);
1406 /* Set return code */
1407 cmd_ctx
->llm
->ret_code
= ret
;
1412 if (cmd_ctx
->llm
== NULL
) {
1413 DBG("Missing llm structure. Allocating one.");
1414 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
1418 /* Notify client of error */
1419 cmd_ctx
->llm
->ret_code
= ret
;
1426 * thread_manage_clients
1428 * This thread manage all clients request using the unix
1429 * client socket for communication.
1431 static void *thread_manage_clients(void *data
)
1434 struct command_ctx
*cmd_ctx
;
1436 DBG("[thread] Manage client started");
1438 ret
= lttcomm_listen_unix_sock(client_sock
);
1443 /* Notify parent pid that we are ready
1444 * to accept command for client side.
1446 if (opt_sig_parent
) {
1447 kill(ppid
, SIGCHLD
);
1451 /* Blocking call, waiting for transmission */
1452 DBG("Accepting client command ...");
1453 sock
= lttcomm_accept_unix_sock(client_sock
);
1458 /* Allocate context command to process the client request */
1459 cmd_ctx
= malloc(sizeof(struct command_ctx
));
1461 /* Allocate data buffer for reception */
1462 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
1463 cmd_ctx
->llm
= NULL
;
1464 cmd_ctx
->session
= NULL
;
1467 * Data is received from the lttng client. The struct
1468 * lttcomm_session_msg (lsm) contains the command and data request of
1471 DBG("Receiving data from client ...");
1472 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
1477 // TODO: Validate cmd_ctx including sanity check for security purpose.
1480 * This function dispatch the work to the kernel or userspace tracer
1481 * libs and fill the lttcomm_lttng_msg data structure of all the needed
1482 * informations for the client. The command context struct contains
1483 * everything this function may needs.
1485 ret
= process_client_msg(cmd_ctx
);
1487 /* TODO: Inform client somehow of the fatal error. At this point,
1488 * ret < 0 means that a malloc failed (ENOMEM). */
1489 /* Error detected but still accept command */
1490 clean_command_ctx(cmd_ctx
);
1494 DBG("Sending response (size: %d, retcode: %d)",
1495 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
1496 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
1498 ERR("Failed to send data back to client");
1501 clean_command_ctx(cmd_ctx
);
1503 /* End of transmission */
1513 * usage function on stderr
1515 static void usage(void)
1517 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
1518 fprintf(stderr
, " -h, --help Display this usage.\n");
1519 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
1520 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
1521 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
1522 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
1523 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
1524 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
1525 fprintf(stderr
, " -V, --version Show version number.\n");
1526 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
1527 fprintf(stderr
, " -q, --quiet No output at all.\n");
1528 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
1532 * daemon argument parsing
1534 static int parse_args(int argc
, char **argv
)
1538 static struct option long_options
[] = {
1539 { "client-sock", 1, 0, 'c' },
1540 { "apps-sock", 1, 0, 'a' },
1541 { "kconsumerd-cmd-sock", 1, 0, 0 },
1542 { "kconsumerd-err-sock", 1, 0, 0 },
1543 { "daemonize", 0, 0, 'd' },
1544 { "sig-parent", 0, 0, 'S' },
1545 { "help", 0, 0, 'h' },
1546 { "group", 1, 0, 'g' },
1547 { "version", 0, 0, 'V' },
1548 { "quiet", 0, 0, 'q' },
1549 { "verbose", 0, 0, 'v' },
1554 int option_index
= 0;
1555 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
1562 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
1564 fprintf(stderr
, " with arg %s\n", optarg
);
1568 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1571 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1577 opt_tracing_group
= strdup(optarg
);
1583 fprintf(stdout
, "%s\n", VERSION
);
1589 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1592 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1601 /* Unknown option or other error.
1602 * Error is printed by getopt, just return */
1611 * init_daemon_socket
1613 * Creates the two needed socket by the daemon.
1614 * apps_sock - The communication socket for all UST apps.
1615 * client_sock - The communication of the cli tool (lttng).
1617 static int init_daemon_socket()
1622 old_umask
= umask(0);
1624 /* Create client tool unix socket */
1625 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
1626 if (client_sock
< 0) {
1627 ERR("Create unix sock failed: %s", client_unix_sock_path
);
1632 /* File permission MUST be 660 */
1633 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1635 ERR("Set file permissions failed: %s", client_unix_sock_path
);
1640 /* Create the application unix socket */
1641 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
1642 if (apps_sock
< 0) {
1643 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
1648 /* File permission MUST be 666 */
1649 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
1651 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
1662 * check_existing_daemon
1664 * Check if the global socket is available.
1665 * If yes, error is returned.
1667 static int check_existing_daemon()
1671 ret
= access(client_unix_sock_path
, F_OK
);
1673 ret
= access(apps_unix_sock_path
, F_OK
);
1682 * Return pointer to home directory path using
1683 * the env variable HOME.
1687 static const char *get_home_dir(void)
1689 const char *home_path
;
1691 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
1692 home_path
= default_home_dir
;
1701 * Set the tracing group gid onto the client socket.
1703 * Race window between mkdir and chown is OK because we are going from
1704 * less permissive (root.root) to more permissive (root.tracing).
1706 static int set_permissions(void)
1711 /* Decide which group name to use */
1712 (opt_tracing_group
!= NULL
) ?
1713 (grp
= getgrnam(opt_tracing_group
)) :
1714 (grp
= getgrnam(default_tracing_group
));
1717 ERR("Missing tracing group. Aborting execution.\n");
1722 /* Set lttng run dir */
1723 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
1725 ERR("Unable to set group on " LTTNG_RUNDIR
);
1729 /* lttng client socket path */
1730 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
1732 ERR("Unable to set group on %s", client_unix_sock_path
);
1736 /* kconsumerd error socket path */
1737 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
1739 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
1743 DBG("All permissions are set");
1750 * create_lttng_rundir
1752 * Create the lttng run directory needed for all
1753 * global sockets and pipe.
1755 static int create_lttng_rundir(void)
1759 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
1761 if (errno
!= EEXIST
) {
1762 ERR("Unable to create " LTTNG_RUNDIR
);
1774 * set_kconsumerd_sockets
1776 * Setup sockets and directory needed by the kconsumerd
1777 * communication with the session daemon.
1779 static int set_kconsumerd_sockets(void)
1783 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
1784 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
1787 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
1788 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
1791 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
1793 if (errno
!= EEXIST
) {
1794 ERR("Failed to create " KCONSUMERD_PATH
);
1800 /* Create the kconsumerd error unix socket */
1801 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
1802 if (kconsumerd_err_sock
< 0) {
1803 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
1808 /* File permission MUST be 660 */
1809 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1811 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
1823 * Signal handler for the daemon
1825 static void sighandler(int sig
)
1829 DBG("SIGPIPE catched");
1832 DBG("SIGINT catched");
1836 DBG("SIGTERM catched");
1847 * set_signal_handler
1849 * Setup signal handler for :
1850 * SIGINT, SIGTERM, SIGPIPE
1852 static int set_signal_handler(void)
1855 struct sigaction sa
;
1858 if ((ret
= sigemptyset(&sigset
)) < 0) {
1859 perror("sigemptyset");
1863 sa
.sa_handler
= sighandler
;
1864 sa
.sa_mask
= sigset
;
1866 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1867 perror("sigaction");
1871 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1872 perror("sigaction");
1876 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1877 perror("sigaction");
1881 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
1889 * Set open files limit to unlimited. This daemon can open a large number of
1890 * file descriptors in order to consumer multiple kernel traces.
1892 static void set_ulimit(void)
1897 lim
.rlim_cur
= 65535;
1898 lim
.rlim_max
= 65535;
1900 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1902 perror("failed to set open files limit");
1909 int main(int argc
, char **argv
)
1914 /* Parse arguments */
1916 if ((ret
= parse_args(argc
, argv
) < 0)) {
1929 /* Check if daemon is UID = 0 */
1930 is_root
= !getuid();
1932 /* Set all sockets path */
1934 ret
= create_lttng_rundir();
1939 if (strlen(apps_unix_sock_path
) == 0) {
1940 snprintf(apps_unix_sock_path
, PATH_MAX
,
1941 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
1944 if (strlen(client_unix_sock_path
) == 0) {
1945 snprintf(client_unix_sock_path
, PATH_MAX
,
1946 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
1949 ret
= set_kconsumerd_sockets();
1954 /* Setup kernel tracer */
1955 init_kernel_tracer();
1957 /* Set ulimit for open files */
1960 if (strlen(apps_unix_sock_path
) == 0) {
1961 snprintf(apps_unix_sock_path
, PATH_MAX
,
1962 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir());
1965 /* Set the cli tool unix socket path */
1966 if (strlen(client_unix_sock_path
) == 0) {
1967 snprintf(client_unix_sock_path
, PATH_MAX
,
1968 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir());
1972 DBG("Client socket path %s", client_unix_sock_path
);
1973 DBG("Application socket path %s", apps_unix_sock_path
);
1975 /* See if daemon already exist. If any of the two
1976 * socket needed by the daemon are present, this test fails
1978 if ((ret
= check_existing_daemon()) == 0) {
1979 ERR("Already running daemon.\n");
1980 /* We do not goto error because we must not
1981 * cleanup() because a daemon is already running.
1986 if (set_signal_handler() < 0) {
1990 /* Setup the needed unix socket */
1991 if (init_daemon_socket() < 0) {
1995 /* Set credentials to socket */
1996 if (is_root
&& (set_permissions() < 0)) {
2000 /* Get parent pid if -S, --sig-parent is specified. */
2001 if (opt_sig_parent
) {
2006 /* Create thread to manage the client socket */
2007 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
2009 perror("pthread_create");
2013 /* Create thread to manage application socket */
2014 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
2016 perror("pthread_create");
2020 ret
= pthread_join(client_thread
, &status
);
2022 perror("pthread_join");