2 DBG("Creating kernel event %s for channel %s.",
3 cmd_ctx->lsm->u.enable.event.name, cmd_ctx->lsm->u.enable.channel_name);
5 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include <semaphore.h>
35 #include <sys/socket.h>
37 #include <sys/types.h>
39 #include <sys/resource.h>
42 #include <urcu/list.h> /* URCU list library (-lurcu) */
43 #include <lttng/lttng.h>
45 #include "liblttsessiondcomm.h"
46 #include "ltt-sessiond.h"
48 #include "kernel-ctl.h"
51 #include "traceable-app.h"
52 #include "lttng-kconsumerd.h"
53 #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
;
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
;
91 /* Pthread, Mutexes and Semaphores */
92 static pthread_t kconsumerd_thread
;
93 static pthread_t apps_thread
;
94 static pthread_t client_thread
;
95 static sem_t kconsumerd_sem
;
97 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
100 * teardown_kernel_session
102 * Complete teardown of a kernel session. This free all data structure
103 * related to a kernel session and update counter.
105 static void teardown_kernel_session(struct ltt_session
*session
)
107 if (session
->kernel_session
!= NULL
) {
108 DBG("Tearing down kernel session");
109 trace_destroy_kernel_session(session
->kernel_session
);
110 /* Extra precaution */
111 session
->kernel_session
= NULL
;
112 /* Decrement session count */
113 session
->kern_session_count
--;
120 * Cleanup the daemon on exit
122 static void cleanup()
126 struct ltt_session
*sess
;
131 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
132 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
135 /* Stopping all threads */
136 DBG("Terminating all threads");
137 pthread_cancel(client_thread
);
138 pthread_cancel(apps_thread
);
139 if (kconsumerd_pid
!= 0) {
140 pthread_cancel(kconsumerd_thread
);
143 DBG("Unlinking all unix socket");
144 unlink(client_unix_sock_path
);
145 unlink(apps_unix_sock_path
);
146 unlink(kconsumerd_err_unix_sock_path
);
148 DBG("Removing %s directory", LTTNG_RUNDIR
);
149 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
151 ERR("asprintf failed. Something is really wrong!");
154 /* Remove lttng run directory */
157 ERR("Unable to clean " LTTNG_RUNDIR
);
160 DBG("Cleaning up all session");
161 /* Cleanup ALL session */
162 cds_list_for_each_entry(sess
, <t_session_list
.head
, list
) {
163 teardown_kernel_session(sess
);
164 // TODO complete session cleanup (including UST)
167 DBG("Closing kernel fd");
168 close(kernel_tracer_fd
);
174 * Send data on a unix socket using the liblttsessiondcomm API.
176 * Return lttcomm error code.
178 static int send_unix_sock(int sock
, void *buf
, size_t len
)
180 /* Check valid length */
185 return lttcomm_send_unix_sock(sock
, buf
, len
);
191 * Free memory of a command context structure.
193 static void clean_command_ctx(struct command_ctx
*cmd_ctx
)
195 DBG("Clean command context structure %p", cmd_ctx
);
209 * send_kconsumerd_fds
211 * Send all stream fds of the kernel session to the consumer.
213 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
217 struct ltt_kernel_stream
*stream
;
218 struct ltt_kernel_channel
*chan
;
219 struct lttcomm_kconsumerd_header lkh
;
220 struct lttcomm_kconsumerd_msg lkm
;
222 nb_fd
= session
->stream_count_global
;
225 lkh
.payload_size
= (nb_fd
+ 1) * sizeof(struct lttcomm_kconsumerd_msg
);
226 lkh
.cmd_type
= ADD_STREAM
;
228 DBG("Sending kconsumerd header");
230 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
232 perror("send kconsumerd header");
236 DBG("Sending metadata stream fd");
238 /* Send metadata stream fd first */
239 lkm
.fd
= session
->metadata_stream_fd
;
240 lkm
.state
= ACTIVE_FD
;
241 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
242 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
244 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
246 perror("send kconsumerd fd");
250 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
251 cds_list_for_each_entry(stream
, &chan
->stream_list
.head
, list
) {
253 lkm
.state
= stream
->state
;
254 lkm
.max_sb_size
= chan
->channel
->attr
.subbuf_size
;
255 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
257 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
259 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
261 perror("send kconsumerd fd");
267 DBG("Kconsumerd fds sent");
278 * Create the trace output directory.
280 static int create_trace_dir(struct ltt_kernel_session
*session
)
283 struct ltt_kernel_channel
*chan
;
285 /* Create all channel directories */
286 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
287 DBG("Creating trace directory at %s", chan
->pathname
);
288 // TODO: recursive create dir
289 ret
= mkdir(chan
->pathname
, S_IRWXU
| S_IRWXG
);
292 perror("mkdir trace path");
308 * Return a socket connected to the libust communication socket
309 * of the application identified by the pid.
311 * If the pid is not found in the traceable list,
312 * return -1 to indicate error.
314 static int ust_connect_app(pid_t pid
)
317 struct ltt_traceable_app
*lta
;
319 DBG("Connect to application pid %d", pid
);
321 lta
= find_app_by_pid(pid
);
324 DBG("Application pid %d not found", pid
);
328 sock
= ustctl_connect_pid(lta
->pid
);
330 ERR("Fail connecting to the PID %d", pid
);
339 * Notify apps by writing 42 to a named pipe using name.
340 * Every applications waiting for a ltt-sessiond will be notified
341 * and re-register automatically to the session daemon.
343 * Return open or write error value.
345 static int notify_apps(const char *name
)
350 DBG("Notify the global application pipe");
352 /* Try opening the global pipe */
353 fd
= open(name
, O_WRONLY
);
358 /* Notify by writing on the pipe */
359 ret
= write(fd
, "42", 2);
371 * Setup the outgoing data buffer for the response (llm) by allocating the
372 * right amount of memory and copying the original information from the lsm
375 * Return total size of the buffer pointed by buf.
377 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
383 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
384 if (cmd_ctx
->llm
== NULL
) {
390 /* Copy common data */
391 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
392 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->pid
;
394 cmd_ctx
->llm
->data_size
= size
;
395 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
404 * thread_manage_kconsumerd
406 * This thread manage the kconsumerd error sent
407 * back to the session daemon.
409 static void *thread_manage_kconsumerd(void *data
)
412 enum lttcomm_return_code code
;
414 DBG("[thread] Manage kconsumerd started");
416 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
421 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
426 /* Getting status code from kconsumerd */
427 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
432 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
433 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
434 if (kconsumerd_cmd_sock
< 0) {
435 sem_post(&kconsumerd_sem
);
436 perror("kconsumerd connect");
439 /* Signal condition to tell that the kconsumerd is ready */
440 sem_post(&kconsumerd_sem
);
441 DBG("Kconsumerd command socket ready");
443 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
444 lttcomm_get_readable_code(-code
));
448 /* Wait for any kconsumerd error */
449 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
451 ERR("Kconsumerd closed the command socket");
455 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
459 DBG("Kconsumerd thread dying");
466 * This thread manage the application socket communication
468 static void *thread_manage_apps(void *data
)
472 /* TODO: Something more elegant is needed but fine for now */
473 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
474 * for 32-bit vs 64-bit compat processes. */
475 /* replicate in ust with version number */
477 int reg
; /* 1:register, 0:unregister */
482 DBG("[thread] Manage apps started");
484 ret
= lttcomm_listen_unix_sock(apps_sock
);
489 /* Notify all applications to register */
490 notify_apps(default_global_apps_pipe
);
493 DBG("Accepting application registration");
494 /* Blocking call, waiting for transmission */
495 sock
= lttcomm_accept_unix_sock(apps_sock
);
500 /* Basic recv here to handle the very simple data
501 * that the libust send to register (reg_msg).
503 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
509 /* Add application to the global traceable list */
510 if (reg_msg
.reg
== 1) {
512 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
514 /* register_traceable_app only return an error with
515 * ENOMEM. At this point, we better stop everything.
521 unregister_traceable_app(reg_msg
.pid
);
531 * spawn_kconsumerd_thread
533 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
534 * exec or it will fails.
536 static int spawn_kconsumerd_thread(void)
540 /* Setup semaphore */
541 sem_init(&kconsumerd_sem
, 0, 0);
543 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
545 perror("pthread_create kconsumerd");
549 /* Wait for the kconsumerd thread to be ready */
550 sem_wait(&kconsumerd_sem
);
552 if (kconsumerd_pid
== 0) {
553 ERR("Kconsumerd did not start");
560 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
567 * Fork and exec a kernel consumer daemon (kconsumerd).
569 * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel
570 * file descriptor using the libkernelctl or kernel-ctl functions. So, a
571 * kernel consumer MUST only be spawned before creating a kernel session.
573 * Return pid if successful else -1.
575 static pid_t
spawn_kconsumerd(void)
580 DBG("Spawning kconsumerd");
587 execlp("kconsumerd", "kconsumerd", "--verbose", NULL
);
589 perror("kernel start consumer exec");
592 } else if (pid
> 0) {
596 perror("kernel start consumer fork");
608 * Spawn the kconsumerd daemon and session daemon thread.
610 static int start_kconsumerd(void)
614 pthread_mutex_lock(&kconsumerd_pid_mutex
);
615 if (kconsumerd_pid
!= 0) {
619 ret
= spawn_kconsumerd();
621 ERR("Spawning kconsumerd failed");
622 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
623 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
627 /* Setting up the global kconsumerd_pid */
628 kconsumerd_pid
= ret
;
629 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
631 DBG("Kconsumerd pid %d", ret
);
633 DBG("Spawning kconsumerd thread");
634 ret
= spawn_kconsumerd_thread();
636 ERR("Fatal error spawning kconsumerd thread");
641 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
651 * Setup necessary data for kernel tracer action.
653 static void init_kernel_tracer(void)
655 /* Set the global kernel tracer fd */
656 kernel_tracer_fd
= open(DEFAULT_KERNEL_TRACER_PATH
, O_RDWR
);
657 if (kernel_tracer_fd
< 0) {
658 WARN("No kernel tracer available");
659 kernel_tracer_fd
= 0;
662 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
668 * Start tracing by creating trace directory and sending FDs to the kernel
671 static int start_kernel_trace(struct ltt_kernel_session
*session
)
675 /* Create trace directory */
676 ret
= create_trace_dir(session
);
678 if (ret
== -EEXIST
) {
679 ret
= LTTCOMM_KERN_DIR_EXIST
;
681 ret
= LTTCOMM_KERN_DIR_FAIL
;
686 if (session
->kconsumer_fds_sent
== 0) {
687 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, session
);
689 ERR("Send kconsumerd fds failed");
690 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
694 session
->kconsumer_fds_sent
= 1;
702 * init_default_channel
704 * Allocate a channel structure and fill it.
706 static struct lttng_channel
*init_default_channel(void)
708 struct lttng_channel
*chan
;
710 chan
= malloc(sizeof(struct lttng_channel
));
712 perror("init channel malloc");
716 if (snprintf(chan
->name
, NAME_MAX
, DEFAULT_CHANNEL_NAME
) < 0) {
717 perror("snprintf defautl channel name");
721 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
722 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
723 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
724 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
725 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
732 * create_kernel_session
734 * Create a kernel tracer session then create the default channel.
736 static int create_kernel_session(struct ltt_session
*session
)
739 struct lttng_channel
*chan
;
741 DBG("Creating kernel session");
743 ret
= kernel_create_session(session
, kernel_tracer_fd
);
745 ret
= LTTCOMM_KERN_SESS_FAIL
;
749 chan
= init_default_channel();
755 DBG("Creating default kernel channel %s", DEFAULT_CHANNEL_NAME
);
757 ret
= kernel_create_channel(session
->kernel_session
, chan
);
759 ret
= LTTCOMM_KERN_CHAN_FAIL
;
770 * Process the command requested by the lttng client within the command
771 * context structure. This function make sure that the return structure (llm)
772 * is set and ready for transmission before returning.
774 * Return any error encountered or 0 for success.
776 static int process_client_msg(struct command_ctx
*cmd_ctx
)
780 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
782 /* Listing commands don't need a session */
783 switch (cmd_ctx
->lsm
->cmd_type
) {
784 case LTTNG_CREATE_SESSION
:
785 case LTTNG_LIST_SESSIONS
:
786 case LTTNG_LIST_EVENTS
:
787 case LTTNG_KERNEL_LIST_EVENTS
:
788 case LTTNG_LIST_TRACEABLE_APPS
:
791 DBG("Getting session %s by name", cmd_ctx
->lsm
->session_name
);
792 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session_name
);
793 if (cmd_ctx
->session
== NULL
) {
794 /* If session name not found */
795 if (cmd_ctx
->lsm
->session_name
!= NULL
) {
796 ret
= LTTCOMM_SESS_NOT_FOUND
;
797 } else { /* If no session name specified */
798 ret
= LTTCOMM_SELECT_SESS
;
806 * Check kernel command for kernel session.
808 switch (cmd_ctx
->lsm
->cmd_type
) {
809 case LTTNG_KERNEL_CREATE_CHANNEL
:
810 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
811 case LTTNG_KERNEL_DISABLE_CHANNEL
:
812 case LTTNG_KERNEL_DISABLE_EVENT
:
813 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
814 case LTTNG_KERNEL_ENABLE_CHANNEL
:
815 case LTTNG_KERNEL_ENABLE_EVENT
:
816 case LTTNG_KERNEL_LIST_EVENTS
:
817 /* Kernel tracer check */
818 if (kernel_tracer_fd
== 0) {
819 init_kernel_tracer();
820 if (kernel_tracer_fd
== 0) {
821 ret
= LTTCOMM_KERN_NA
;
826 /* Need a session for kernel command */
827 if (cmd_ctx
->lsm
->cmd_type
!= LTTNG_KERNEL_LIST_EVENTS
&&
828 cmd_ctx
->session
->kernel_session
== NULL
) {
830 ret
= create_kernel_session(cmd_ctx
->session
);
832 ret
= LTTCOMM_KERN_SESS_FAIL
;
836 /* Start the kernel consumer daemon */
837 if (kconsumerd_pid
== 0) {
838 ret
= start_kconsumerd();
846 /* Connect to ust apps if available pid */
847 if (cmd_ctx
->lsm
->pid
> 0) {
848 /* Connect to app using ustctl API */
849 cmd_ctx
->ust_sock
= ust_connect_app(cmd_ctx
->lsm
->pid
);
850 if (cmd_ctx
->ust_sock
< 0) {
851 ret
= LTTCOMM_NO_TRACEABLE
;
856 /* Process by command type */
857 switch (cmd_ctx
->lsm
->cmd_type
) {
858 case LTTNG_KERNEL_CREATE_CHANNEL
:
860 /* Setup lttng message with no payload */
861 ret
= setup_lttng_msg(cmd_ctx
, 0);
867 DBG("Creating kernel channel");
869 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
870 &cmd_ctx
->lsm
->u
.channel
.chan
);
872 ret
= LTTCOMM_KERN_CHAN_FAIL
;
879 case LTTNG_KERNEL_DISABLE_EVENT
:
882 struct ltt_kernel_channel
*chan
;
884 /* Setup lttng message with no payload */
885 ret
= setup_lttng_msg(cmd_ctx
, 0);
890 /* Get channel by name and create event for that channel */
891 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
892 if (strcmp(cmd_ctx
->lsm
->u
.disable
.channel_name
, chan
->channel
->name
) == 0) {
893 DBG("Disabling kernel event %s for channel %s.",
894 cmd_ctx
->lsm
->u
.disable
.name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
896 ret
= kernel_disable_event(cmd_ctx
->lsm
->u
.disable
.name
, chan
);
898 ret
= LTTCOMM_KERN_DISABLE_FAIL
;
907 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
909 kernel_wait_quiescent(kernel_tracer_fd
);
914 case LTTNG_KERNEL_ENABLE_EVENT
:
917 struct ltt_kernel_channel
*chan
;
919 /* Setup lttng message with no payload */
920 ret
= setup_lttng_msg(cmd_ctx
, 0);
925 /* Get channel by name and create event for that channel */
926 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
927 if (strcmp(cmd_ctx
->lsm
->u
.enable
.channel_name
, chan
->channel
->name
) == 0) {
928 DBG("Creating kernel event %s for channel %s.",
929 cmd_ctx
->lsm
->u
.enable
.event
.name
, cmd_ctx
->lsm
->u
.enable
.channel_name
);
931 ret
= kernel_create_event(chan
, &cmd_ctx
->lsm
->u
.enable
.event
);
933 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
942 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
944 kernel_wait_quiescent(kernel_tracer_fd
);
949 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
951 int pos
, size
, found
;
952 char *event_list
, *event
, *ptr
;
953 struct ltt_kernel_channel
*chan
;
954 struct lttng_event ev
;
956 /* Setup lttng message with no payload */
957 ret
= setup_lttng_msg(cmd_ctx
, 0);
962 DBG("Enabling all kernel event");
964 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
966 ret
= LTTCOMM_KERN_LIST_FAIL
;
970 /* Get channel by name and create event for that channel */
971 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
972 if (strcmp(cmd_ctx
->lsm
->u
.enable
.channel_name
, chan
->channel
->name
) == 0) {
979 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
984 while ((size
= sscanf(ptr
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
985 strncpy(ev
.name
, event
, LTTNG_SYM_NAME_LEN
);
986 /* Default event type for enable all */
987 ev
.type
= LTTNG_EVENT_TRACEPOINTS
;
988 /* Enable each single tracepoint event */
989 ret
= kernel_create_event(chan
, &ev
);
991 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
994 /* Move pointer to the next line */
1001 /* Quiescent wait after event enable */
1002 kernel_wait_quiescent(kernel_tracer_fd
);
1006 case LTTNG_KERNEL_LIST_EVENTS
:
1011 DBG("Listing kernel events");
1013 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1015 ret
= LTTCOMM_KERN_LIST_FAIL
;
1020 * Setup lttng message with payload size set to the event list size in
1021 * bytes and then copy list into the llm payload.
1023 ret
= setup_lttng_msg(cmd_ctx
, size
);
1028 /* Copy event list into message payload */
1029 memcpy(cmd_ctx
->llm
->payload
, event_list
, size
);
1036 case LTTNG_START_TRACE
:
1038 struct ltt_kernel_channel
*chan
;
1040 /* Setup lttng message with no payload */
1041 ret
= setup_lttng_msg(cmd_ctx
, 0);
1046 /* Kernel tracing */
1047 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1048 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
1049 DBG("Open kernel metadata");
1050 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
);
1052 ret
= LTTCOMM_KERN_META_FAIL
;
1057 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1058 DBG("Opening kernel metadata stream");
1059 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1060 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
1062 ERR("Kernel create metadata stream failed");
1063 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1069 /* For each channel */
1070 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1071 if (chan
->stream_count
== 0) {
1072 ret
= kernel_open_channel_stream(chan
);
1074 ERR("Kernel create channel stream failed");
1075 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1078 /* Update the stream global counter */
1079 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
1083 DBG("Start kernel tracing");
1084 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
1086 ERR("Kernel start session failed");
1087 ret
= LTTCOMM_KERN_START_FAIL
;
1091 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
1093 ret
= LTTCOMM_KERN_START_FAIL
;
1097 /* Quiescent wait after starting trace */
1098 kernel_wait_quiescent(kernel_tracer_fd
);
1101 /* TODO: Start all UST traces */
1106 case LTTNG_STOP_TRACE
:
1108 struct ltt_kernel_channel
*chan
;
1109 /* Setup lttng message with no payload */
1110 ret
= setup_lttng_msg(cmd_ctx
, 0);
1116 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1117 DBG("Stop kernel tracing");
1119 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
1121 ERR("Kernel metadata flush failed");
1124 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1125 ret
= kernel_flush_buffer(chan
);
1127 ERR("Kernel flush buffer error");
1131 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
1133 ERR("Kernel stop session failed");
1134 ret
= LTTCOMM_KERN_STOP_FAIL
;
1138 /* Quiescent wait after stopping trace */
1139 kernel_wait_quiescent(kernel_tracer_fd
);
1142 /* TODO : User-space tracer */
1147 case LTTNG_CREATE_SESSION
:
1149 /* Setup lttng message with no payload */
1150 ret
= setup_lttng_msg(cmd_ctx
, 0);
1155 ret
= create_session(cmd_ctx
->lsm
->session_name
, cmd_ctx
->lsm
->path
);
1157 if (ret
== -EEXIST
) {
1158 ret
= LTTCOMM_EXIST_SESS
;
1160 ret
= LTTCOMM_FATAL
;
1168 case LTTNG_DESTROY_SESSION
:
1170 /* Setup lttng message with no payload */
1171 ret
= setup_lttng_msg(cmd_ctx
, 0);
1176 /* Clean kernel session teardown */
1177 teardown_kernel_session(cmd_ctx
->session
);
1179 ret
= destroy_session(cmd_ctx
->lsm
->session_name
);
1181 ret
= LTTCOMM_FATAL
;
1189 case LTTNG_LIST_TRACES:
1191 unsigned int trace_count;
1193 trace_count = get_trace_count_per_session(cmd_ctx->session);
1194 if (trace_count == 0) {
1195 ret = LTTCOMM_NO_TRACE;
1199 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count);
1204 get_traces_per_session(cmd_ctx->session,
1205 (struct lttng_trace *)(cmd_ctx->llm->payload));
1212 case UST_CREATE_TRACE:
1214 ret = setup_lttng_msg(cmd_ctx, 0);
1219 ret = ust_create_trace(cmd_ctx);
1226 case LTTNG_LIST_TRACEABLE_APPS
:
1228 unsigned int app_count
;
1230 app_count
= get_app_count();
1231 DBG("Traceable application count : %d", app_count
);
1232 if (app_count
== 0) {
1233 ret
= LTTCOMM_NO_APPS
;
1237 ret
= setup_lttng_msg(cmd_ctx
, sizeof(pid_t
) * app_count
);
1242 get_app_list_pids((pid_t
*)(cmd_ctx
->llm
->payload
));
1248 case UST_START_TRACE:
1250 ret = setup_lttng_msg(cmd_ctx, 0);
1255 ret = ust_start_trace(cmd_ctx);
1261 case UST_STOP_TRACE:
1263 ret = setup_lttng_msg(cmd_ctx, 0);
1268 ret = ust_stop_trace(cmd_ctx);
1275 case LTTNG_LIST_SESSIONS
:
1277 unsigned int session_count
;
1279 session_count
= get_session_count();
1280 if (session_count
== 0) {
1281 ret
= LTTCOMM_NO_SESSION
;
1285 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * session_count
);
1290 get_lttng_session((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
1296 /* Undefined command */
1297 ret
= setup_lttng_msg(cmd_ctx
, 0);
1306 /* Set return code */
1307 cmd_ctx
->llm
->ret_code
= ret
;
1312 if (cmd_ctx
->llm
== NULL
) {
1313 DBG("Missing llm structure. Allocating one.");
1314 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
1318 /* Notify client of error */
1319 cmd_ctx
->llm
->ret_code
= ret
;
1326 * thread_manage_clients
1328 * This thread manage all clients request using the unix
1329 * client socket for communication.
1331 static void *thread_manage_clients(void *data
)
1334 struct command_ctx
*cmd_ctx
;
1336 DBG("[thread] Manage client started");
1338 ret
= lttcomm_listen_unix_sock(client_sock
);
1343 /* Notify parent pid that we are ready
1344 * to accept command for client side.
1346 if (opt_sig_parent
) {
1347 kill(ppid
, SIGCHLD
);
1351 /* Blocking call, waiting for transmission */
1352 DBG("Accepting client command ...");
1353 sock
= lttcomm_accept_unix_sock(client_sock
);
1358 /* Allocate context command to process the client request */
1359 cmd_ctx
= malloc(sizeof(struct command_ctx
));
1361 /* Allocate data buffer for reception */
1362 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
1363 cmd_ctx
->llm
= NULL
;
1364 cmd_ctx
->session
= NULL
;
1367 * Data is received from the lttng client. The struct
1368 * lttcomm_session_msg (lsm) contains the command and data request of
1371 DBG("Receiving data from client ...");
1372 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
1377 // TODO: Validate cmd_ctx including sanity check for security purpose.
1380 * This function dispatch the work to the kernel or userspace tracer
1381 * libs and fill the lttcomm_lttng_msg data structure of all the needed
1382 * informations for the client. The command context struct contains
1383 * everything this function may needs.
1385 ret
= process_client_msg(cmd_ctx
);
1387 /* TODO: Inform client somehow of the fatal error. At this point,
1388 * ret < 0 means that a malloc failed (ENOMEM). */
1389 /* Error detected but still accept command */
1390 clean_command_ctx(cmd_ctx
);
1394 DBG("Sending response (size: %d, retcode: %d)",
1395 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
1396 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
1398 ERR("Failed to send data back to client");
1401 clean_command_ctx(cmd_ctx
);
1403 /* End of transmission */
1413 * usage function on stderr
1415 static void usage(void)
1417 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
1418 fprintf(stderr
, " -h, --help Display this usage.\n");
1419 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
1420 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
1421 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
1422 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
1423 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
1424 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
1425 fprintf(stderr
, " -V, --version Show version number.\n");
1426 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
1427 fprintf(stderr
, " -q, --quiet No output at all.\n");
1428 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
1432 * daemon argument parsing
1434 static int parse_args(int argc
, char **argv
)
1438 static struct option long_options
[] = {
1439 { "client-sock", 1, 0, 'c' },
1440 { "apps-sock", 1, 0, 'a' },
1441 { "kconsumerd-cmd-sock", 1, 0, 0 },
1442 { "kconsumerd-err-sock", 1, 0, 0 },
1443 { "daemonize", 0, 0, 'd' },
1444 { "sig-parent", 0, 0, 'S' },
1445 { "help", 0, 0, 'h' },
1446 { "group", 1, 0, 'g' },
1447 { "version", 0, 0, 'V' },
1448 { "quiet", 0, 0, 'q' },
1449 { "verbose", 0, 0, 'v' },
1454 int option_index
= 0;
1455 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
1462 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
1464 fprintf(stderr
, " with arg %s\n", optarg
);
1468 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1471 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1477 opt_tracing_group
= strdup(optarg
);
1483 fprintf(stdout
, "%s\n", VERSION
);
1489 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1492 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1501 /* Unknown option or other error.
1502 * Error is printed by getopt, just return */
1511 * init_daemon_socket
1513 * Creates the two needed socket by the daemon.
1514 * apps_sock - The communication socket for all UST apps.
1515 * client_sock - The communication of the cli tool (lttng).
1517 static int init_daemon_socket()
1522 old_umask
= umask(0);
1524 /* Create client tool unix socket */
1525 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
1526 if (client_sock
< 0) {
1527 ERR("Create unix sock failed: %s", client_unix_sock_path
);
1532 /* File permission MUST be 660 */
1533 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1535 ERR("Set file permissions failed: %s", client_unix_sock_path
);
1540 /* Create the application unix socket */
1541 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
1542 if (apps_sock
< 0) {
1543 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
1548 /* File permission MUST be 666 */
1549 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
1551 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
1562 * check_existing_daemon
1564 * Check if the global socket is available.
1565 * If yes, error is returned.
1567 static int check_existing_daemon()
1571 ret
= access(client_unix_sock_path
, F_OK
);
1573 ret
= access(apps_unix_sock_path
, F_OK
);
1582 * Return pointer to home directory path using
1583 * the env variable HOME.
1587 static const char *get_home_dir(void)
1589 const char *home_path
;
1591 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
1592 home_path
= default_home_dir
;
1601 * Set the tracing group gid onto the client socket.
1603 * Race window between mkdir and chown is OK because we are going from
1604 * less permissive (root.root) to more permissive (root.tracing).
1606 static int set_permissions(void)
1611 /* Decide which group name to use */
1612 (opt_tracing_group
!= NULL
) ?
1613 (grp
= getgrnam(opt_tracing_group
)) :
1614 (grp
= getgrnam(default_tracing_group
));
1617 ERR("Missing tracing group. Aborting execution.\n");
1622 /* Set lttng run dir */
1623 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
1625 ERR("Unable to set group on " LTTNG_RUNDIR
);
1629 /* lttng client socket path */
1630 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
1632 ERR("Unable to set group on %s", client_unix_sock_path
);
1636 /* kconsumerd error socket path */
1637 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
1639 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
1643 DBG("All permissions are set");
1650 * create_lttng_rundir
1652 * Create the lttng run directory needed for all
1653 * global sockets and pipe.
1655 static int create_lttng_rundir(void)
1659 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
1661 if (errno
!= EEXIST
) {
1662 ERR("Unable to create " LTTNG_RUNDIR
);
1674 * set_kconsumerd_sockets
1676 * Setup sockets and directory needed by the kconsumerd
1677 * communication with the session daemon.
1679 static int set_kconsumerd_sockets(void)
1683 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
1684 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
1687 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
1688 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
1691 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
1693 if (errno
!= EEXIST
) {
1694 ERR("Failed to create " KCONSUMERD_PATH
);
1700 /* Create the kconsumerd error unix socket */
1701 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
1702 if (kconsumerd_err_sock
< 0) {
1703 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
1708 /* File permission MUST be 660 */
1709 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1711 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
1723 * Signal handler for the daemon
1725 static void sighandler(int sig
)
1729 DBG("SIGPIPE catched");
1732 DBG("SIGINT catched");
1736 DBG("SIGTERM catched");
1747 * set_signal_handler
1749 * Setup signal handler for :
1750 * SIGINT, SIGTERM, SIGPIPE
1752 static int set_signal_handler(void)
1755 struct sigaction sa
;
1758 if ((ret
= sigemptyset(&sigset
)) < 0) {
1759 perror("sigemptyset");
1763 sa
.sa_handler
= sighandler
;
1764 sa
.sa_mask
= sigset
;
1766 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1767 perror("sigaction");
1771 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1772 perror("sigaction");
1776 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1777 perror("sigaction");
1781 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
1789 * Set open files limit to unlimited. This daemon can open a large number of
1790 * file descriptors in order to consumer multiple kernel traces.
1792 static void set_ulimit(void)
1797 lim
.rlim_cur
= 65535;
1798 lim
.rlim_max
= 65535;
1800 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1802 perror("failed to set open files limit");
1809 int main(int argc
, char **argv
)
1814 /* Parse arguments */
1816 if ((ret
= parse_args(argc
, argv
) < 0)) {
1829 /* Check if daemon is UID = 0 */
1830 is_root
= !getuid();
1832 /* Set all sockets path */
1834 ret
= create_lttng_rundir();
1839 if (strlen(apps_unix_sock_path
) == 0) {
1840 snprintf(apps_unix_sock_path
, PATH_MAX
,
1841 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
1844 if (strlen(client_unix_sock_path
) == 0) {
1845 snprintf(client_unix_sock_path
, PATH_MAX
,
1846 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
1849 ret
= set_kconsumerd_sockets();
1854 /* Setup kernel tracer */
1855 init_kernel_tracer();
1857 /* Set ulimit for open files */
1860 if (strlen(apps_unix_sock_path
) == 0) {
1861 snprintf(apps_unix_sock_path
, PATH_MAX
,
1862 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir());
1865 /* Set the cli tool unix socket path */
1866 if (strlen(client_unix_sock_path
) == 0) {
1867 snprintf(client_unix_sock_path
, PATH_MAX
,
1868 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir());
1872 DBG("Client socket path %s", client_unix_sock_path
);
1873 DBG("Application socket path %s", apps_unix_sock_path
);
1875 /* See if daemon already exist. If any of the two
1876 * socket needed by the daemon are present, this test fails
1878 if ((ret
= check_existing_daemon()) == 0) {
1879 ERR("Already running daemon.\n");
1880 /* We do not goto error because we must not
1881 * cleanup() because a daemon is already running.
1886 if (set_signal_handler() < 0) {
1890 /* Setup the needed unix socket */
1891 if (init_daemon_socket() < 0) {
1895 /* Set credentials to socket */
1896 if (is_root
&& (set_permissions() < 0)) {
1900 /* Get parent pid if -S, --sig-parent is specified. */
1901 if (opt_sig_parent
) {
1906 /* Create thread to manage the client socket */
1907 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
1909 perror("pthread_create");
1913 /* Create thread to manage application socket */
1914 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
1916 perror("pthread_create");
1920 ret
= pthread_join(client_thread
, &status
);
1922 perror("pthread_join");