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>
37 #include <urcu/list.h> /* URCU list library (-lurcu) */
38 #include <lttng/lttng.h>
40 #include "liblttsessiondcomm.h"
41 #include "ltt-sessiond.h"
43 #include "kernel-ctl.h"
46 #include "traceable-app.h"
47 #include "lttng-kconsumerd.h"
48 #include "libustctl.h"
52 * teardown: signal SIGTERM handler -> write into pipe. Threads waits
53 * with epoll on pipe and on other pipes/sockets for commands. Main
54 * simply waits on pthread join.
58 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
59 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
60 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
61 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
64 int opt_verbose
; /* Not static for lttngerr.h */
65 int opt_quiet
; /* Not static for lttngerr.h */
67 const char *opt_tracing_group
;
68 static int opt_sig_parent
;
69 static int opt_daemon
;
70 static int is_root
; /* Set to 1 if the daemon is running as root */
71 static pid_t ppid
; /* Parent PID for --sig-parent option */
72 static pid_t kconsumerd_pid
;
74 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
75 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
76 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
77 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
80 static int client_sock
;
82 static int kconsumerd_err_sock
;
83 static int kconsumerd_cmd_sock
;
84 static int kernel_tracer_fd
;
86 /* Pthread, Mutexes and Semaphores */
87 static pthread_t kconsumerd_thread
;
88 static pthread_t apps_thread
;
89 static pthread_t client_thread
;
90 static sem_t kconsumerd_sem
;
92 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
95 * teardown_kernel_session
97 * Complete teardown of a kernel session. This free all data structure
98 * related to a kernel session and update counter.
100 static void teardown_kernel_session(struct ltt_session
*session
)
102 if (session
->kernel_session
!= NULL
) {
103 DBG("Tearing down kernel session");
104 trace_destroy_kernel_session(session
->kernel_session
);
105 /* Extra precaution */
106 session
->kernel_session
= NULL
;
107 /* Decrement session count */
108 session
->kern_session_count
--;
115 * Cleanup the daemon on exit
117 static void cleanup()
121 struct ltt_session
*sess
;
126 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
127 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
130 /* Stopping all threads */
131 DBG("Terminating all threads");
132 pthread_cancel(client_thread
);
133 pthread_cancel(apps_thread
);
134 if (kconsumerd_pid
!= 0) {
135 pthread_cancel(kconsumerd_thread
);
138 DBG("Unlinking all unix socket");
139 unlink(client_unix_sock_path
);
140 unlink(apps_unix_sock_path
);
141 unlink(kconsumerd_err_unix_sock_path
);
143 DBG("Removing %s directory", LTTNG_RUNDIR
);
144 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
146 ERR("asprintf failed. Something is really wrong!");
149 /* Remove lttng run directory */
152 ERR("Unable to clean " LTTNG_RUNDIR
);
155 DBG("Cleaning up all session");
156 /* Cleanup ALL session */
157 cds_list_for_each_entry(sess
, <t_session_list
.head
, list
) {
158 teardown_kernel_session(sess
);
159 // TODO complete session cleanup (including UST)
162 close(kernel_tracer_fd
);
168 * Send data on a unix socket using the liblttsessiondcomm API.
170 * Return lttcomm error code.
172 static int send_unix_sock(int sock
, void *buf
, size_t len
)
174 /* Check valid length */
179 return lttcomm_send_unix_sock(sock
, buf
, len
);
185 * Free memory of a command context structure.
187 static void clean_command_ctx(struct command_ctx
*cmd_ctx
)
189 DBG("Clean command context structure %p", cmd_ctx
);
205 * Return a socket connected to the libust communication socket
206 * of the application identified by the pid.
208 * If the pid is not found in the traceable list,
209 * return -1 to indicate error.
211 static int ust_connect_app(pid_t pid
)
214 struct ltt_traceable_app
*lta
;
216 DBG("Connect to application pid %d", pid
);
218 lta
= find_app_by_pid(pid
);
221 DBG("Application pid %d not found", pid
);
225 sock
= ustctl_connect_pid(lta
->pid
);
227 ERR("Fail connecting to the PID %d", pid
);
236 * Notify apps by writing 42 to a named pipe using name.
237 * Every applications waiting for a ltt-sessiond will be notified
238 * and re-register automatically to the session daemon.
240 * Return open or write error value.
242 static int notify_apps(const char *name
)
247 DBG("Notify the global application pipe");
249 /* Try opening the global pipe */
250 fd
= open(name
, O_WRONLY
);
255 /* Notify by writing on the pipe */
256 ret
= write(fd
, "42", 2);
268 * Setup the outgoing data buffer for the response (llm) by allocating the
269 * right amount of memory and copying the original information from the lsm
272 * Return total size of the buffer pointed by buf.
274 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
276 int ret
, buf_size
, trace_name_size
;
279 * Check for the trace_name. If defined, it's part of the payload data of
282 trace_name_size
= strlen(cmd_ctx
->lsm
->trace_name
);
283 buf_size
= trace_name_size
+ size
;
285 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
286 if (cmd_ctx
->llm
== NULL
) {
292 /* Copy common data */
293 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
294 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->pid
;
295 if (!uuid_is_null(cmd_ctx
->lsm
->session_uuid
)) {
296 uuid_copy(cmd_ctx
->llm
->session_uuid
, cmd_ctx
->lsm
->session_uuid
);
299 cmd_ctx
->llm
->trace_name_offset
= trace_name_size
;
300 cmd_ctx
->llm
->data_size
= size
;
301 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
303 /* Copy trace name to the llm structure. Begining of the payload. */
304 memcpy(cmd_ctx
->llm
->payload
, cmd_ctx
->lsm
->trace_name
, trace_name_size
);
313 * thread_manage_kconsumerd
315 * This thread manage the kconsumerd error sent
316 * back to the session daemon.
318 static void *thread_manage_kconsumerd(void *data
)
321 enum lttcomm_return_code code
;
323 DBG("[thread] Manage kconsumerd started");
325 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
330 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
335 /* Getting status code from kconsumerd */
336 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
341 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
342 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
343 if (kconsumerd_cmd_sock
< 0) {
344 sem_post(&kconsumerd_sem
);
345 perror("kconsumerd connect");
348 /* Signal condition to tell that the kconsumerd is ready */
349 sem_post(&kconsumerd_sem
);
350 DBG("Kconsumerd command socket ready");
352 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
353 lttcomm_get_readable_code(-code
));
357 /* Wait for any kconsumerd error */
358 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
360 ERR("Kconsumerd closed the command socket");
364 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
368 DBG("Kconsumerd thread dying");
375 * This thread manage the application socket communication
377 static void *thread_manage_apps(void *data
)
381 /* TODO: Something more elegant is needed but fine for now */
382 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
383 * for 32-bit vs 64-bit compat processes. */
384 /* replicate in ust with version number */
386 int reg
; /* 1:register, 0:unregister */
391 DBG("[thread] Manage apps started");
393 ret
= lttcomm_listen_unix_sock(apps_sock
);
398 /* Notify all applications to register */
399 notify_apps(default_global_apps_pipe
);
402 DBG("Accepting application registration");
403 /* Blocking call, waiting for transmission */
404 sock
= lttcomm_accept_unix_sock(apps_sock
);
409 /* Basic recv here to handle the very simple data
410 * that the libust send to register (reg_msg).
412 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
418 /* Add application to the global traceable list */
419 if (reg_msg
.reg
== 1) {
421 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
423 /* register_traceable_app only return an error with
424 * ENOMEM. At this point, we better stop everything.
430 unregister_traceable_app(reg_msg
.pid
);
440 * spawn_kconsumerd_thread
442 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
443 * exec or it will fails.
445 static int spawn_kconsumerd_thread(void)
449 /* Setup semaphore */
450 sem_init(&kconsumerd_sem
, 0, 0);
452 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
454 perror("pthread_create kconsumerd");
458 /* Wait for the kconsumerd thread to be ready */
459 sem_wait(&kconsumerd_sem
);
461 if (kconsumerd_pid
== 0) {
462 ERR("Kconsumerd did not start");
469 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
476 * Fork and exec a kernel consumer daemon (kconsumerd).
478 * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel
479 * file descriptor using the libkernelctl or kernel-ctl functions. So, a
480 * kernel consumer MUST only be spawned before creating a kernel session.
482 * Return pid if successful else -1.
484 static pid_t
spawn_kconsumerd(void)
489 DBG("Spawning kconsumerd");
496 execlp("kconsumerd", "kconsumerd", "--verbose", NULL
);
498 perror("kernel start consumer exec");
501 } else if (pid
> 0) {
505 perror("kernel start consumer fork");
517 * Spawn the kconsumerd daemon and session daemon thread.
519 static int start_kconsumerd(void)
523 pthread_mutex_lock(&kconsumerd_pid_mutex
);
524 if (kconsumerd_pid
!= 0) {
528 ret
= spawn_kconsumerd();
530 ERR("Spawning kconsumerd failed");
531 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
532 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
536 /* Setting up the global kconsumerd_pid */
537 kconsumerd_pid
= ret
;
538 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
540 DBG("Kconsumerd pid %d", ret
);
542 DBG("Spawning kconsumerd thread");
543 ret
= spawn_kconsumerd_thread();
545 ERR("Fatal error spawning kconsumerd thread");
550 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
558 * send_kconsumerd_fds
560 * Send all stream fds of the kernel session to the consumer.
562 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
566 struct ltt_kernel_stream
*stream
;
567 struct ltt_kernel_channel
*chan
;
568 struct lttcomm_kconsumerd_header lkh
;
569 struct lttcomm_kconsumerd_msg lkm
;
571 nb_fd
= session
->stream_count_global
;
574 lkh
.payload_size
= (nb_fd
+ 1) * sizeof(struct lttcomm_kconsumerd_msg
);
575 lkh
.cmd_type
= ADD_STREAM
;
577 DBG("Sending kconsumerd header");
579 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
581 perror("send kconsumerd header");
585 DBG("Sending metadata stream fd");
587 /* Send metadata stream fd first */
588 lkm
.fd
= session
->metadata_stream_fd
;
589 lkm
.state
= ACTIVE_FD
;
590 lkm
.max_sb_size
= session
->metadata
->conf
->subbuf_size
;
591 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
593 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
595 perror("send kconsumerd fd");
599 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
600 cds_list_for_each_entry(stream
, &chan
->stream_list
.head
, list
) {
602 lkm
.state
= stream
->state
;
603 lkm
.max_sb_size
= chan
->channel
->subbuf_size
;
604 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
606 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
608 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
610 perror("send kconsumerd fd");
616 DBG("Kconsumerd fds sent");
627 * Create the trace output directory.
629 static int create_trace_dir(struct ltt_kernel_session
*session
)
632 struct ltt_kernel_channel
*chan
;
634 /* Create all channel directories */
635 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
636 DBG("Creating trace directory at %s", chan
->pathname
);
637 // TODO: recursive create dir
638 ret
= mkdir(chan
->pathname
, S_IRWXU
| S_IRWXG
);
641 perror("mkdir trace path");
657 * Setup necessary data for kernel tracer action.
659 static void init_kernel_tracer(void)
661 /* Set the global kernel tracer fd */
662 kernel_tracer_fd
= open(DEFAULT_KERNEL_TRACER_PATH
, O_RDWR
);
663 if (kernel_tracer_fd
< 0) {
664 WARN("No kernel tracer available");
665 kernel_tracer_fd
= 0;
668 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
674 * Process the command requested by the lttng client within the command
675 * context structure. This function make sure that the return structure (llm)
676 * is set and ready for transmission before returning.
678 * Return any error encountered or 0 for success.
680 static int process_client_msg(struct command_ctx
*cmd_ctx
)
684 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
686 /* Check command that needs a session */
687 switch (cmd_ctx
->lsm
->cmd_type
) {
688 case LTTNG_CREATE_SESSION
:
689 case LTTNG_LIST_SESSIONS
:
690 case KERNEL_LIST_EVENTS
:
694 cmd_ctx
->session
= find_session_by_uuid(cmd_ctx
->lsm
->session_uuid
);
695 if (cmd_ctx
->session
== NULL
) {
696 ret
= LTTCOMM_SELECT_SESS
;
702 /* Check command for kernel tracing */
703 switch (cmd_ctx
->lsm
->cmd_type
) {
704 case KERNEL_CREATE_SESSION
:
705 case KERNEL_CREATE_CHANNEL
:
706 case KERNEL_CREATE_STREAM
:
707 case KERNEL_DISABLE_EVENT
:
708 case KERNEL_ENABLE_EVENT
:
709 case KERNEL_LIST_EVENTS
:
710 case KERNEL_OPEN_METADATA
:
711 case KERNEL_START_TRACE
:
712 case KERNEL_STOP_TRACE
:
713 /* Kernel tracer check */
714 if (kernel_tracer_fd
== 0) {
715 init_kernel_tracer();
716 if (kernel_tracer_fd
== 0) {
717 ret
= LTTCOMM_KERN_NA
;
724 /* Connect to ust apps if available pid */
725 if (cmd_ctx
->lsm
->pid
> 0) {
726 /* Connect to app using ustctl API */
727 cmd_ctx
->ust_sock
= ust_connect_app(cmd_ctx
->lsm
->pid
);
728 if (cmd_ctx
->ust_sock
< 0) {
729 ret
= LTTCOMM_NO_TRACEABLE
;
734 /* Process by command type */
735 switch (cmd_ctx
->lsm
->cmd_type
) {
736 case KERNEL_CREATE_SESSION
:
738 ret
= setup_lttng_msg(cmd_ctx
, 0);
743 ret
= start_kconsumerd();
748 DBG("Creating kernel session");
750 ret
= kernel_create_session(cmd_ctx
->session
, kernel_tracer_fd
);
752 ret
= LTTCOMM_KERN_SESS_FAIL
;
759 case KERNEL_CREATE_CHANNEL
:
761 ret
= setup_lttng_msg(cmd_ctx
, 0);
766 DBG("Creating kernel channel");
768 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
);
771 ret
= LTTCOMM_KERN_CHAN_FAIL
;
778 case KERNEL_ENABLE_EVENT
:
780 /* Setup lttng message with no payload */
781 ret
= setup_lttng_msg(cmd_ctx
, 0);
786 DBG("Enabling kernel event %s", cmd_ctx
->lsm
->u
.event
.event_name
);
788 ret
= kernel_enable_event(cmd_ctx
->session
->kernel_session
, cmd_ctx
->lsm
->u
.event
.event_name
);
790 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
797 case KERNEL_ENABLE_ALL_EVENT
:
800 char *event_list
, *event
, *ptr
;
802 /* Setup lttng message with no payload */
803 ret
= setup_lttng_msg(cmd_ctx
, 0);
808 DBG("Enabling all kernel event");
810 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
812 ret
= LTTCOMM_KERN_LIST_FAIL
;
817 while ((size
= sscanf(ptr
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
818 /* Enable each single event */
819 ret
= kernel_enable_event(cmd_ctx
->session
->kernel_session
, event
);
821 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
824 /* Move pointer to the next line */
834 case KERNEL_LIST_EVENTS
:
839 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
841 ret
= LTTCOMM_KERN_LIST_FAIL
;
846 * Setup lttng message with payload size set to the event list size in
847 * bytes and then copy list into the llm payload.
849 ret
= setup_lttng_msg(cmd_ctx
, size
);
854 /* Copy event list into message payload */
855 memcpy(cmd_ctx
->llm
->payload
, event_list
, size
);
862 case KERNEL_OPEN_METADATA
:
864 /* Setup lttng message with no payload */
865 ret
= setup_lttng_msg(cmd_ctx
, 0);
870 DBG("Open kernel metadata");
872 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
);
874 ret
= LTTCOMM_KERN_META_FAIL
;
881 case KERNEL_CREATE_STREAM
:
883 struct ltt_kernel_channel
*chan
;
884 /* Setup lttng message with no payload */
885 ret
= setup_lttng_msg(cmd_ctx
, 0);
890 DBG("Creating kernel stream");
892 ret
= kernel_create_metadata_stream(cmd_ctx
->session
->kernel_session
);
894 ERR("Kernel create metadata stream failed");
895 ret
= LTTCOMM_KERN_STREAM_FAIL
;
899 /* For each channel */
900 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
901 ret
= kernel_create_channel_stream(chan
);
903 ERR("Kernel create channel stream failed");
904 ret
= LTTCOMM_KERN_STREAM_FAIL
;
907 /* Update the stream global counter */
908 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
914 case KERNEL_START_TRACE
:
916 /* Setup lttng message with no payload */
917 ret
= setup_lttng_msg(cmd_ctx
, 0);
922 DBG("Start kernel tracing");
924 ret
= create_trace_dir(cmd_ctx
->session
->kernel_session
);
926 if (ret
== -EEXIST
) {
927 ret
= LTTCOMM_KERN_DIR_EXIST
;
929 ret
= LTTCOMM_KERN_DIR_FAIL
;
934 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
936 ERR("Kernel start session failed");
937 ret
= LTTCOMM_KERN_START_FAIL
;
941 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, cmd_ctx
->session
->kernel_session
);
943 ERR("Send kconsumerd fds failed");
944 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
951 case KERNEL_STOP_TRACE
:
953 /* Setup lttng message with no payload */
954 ret
= setup_lttng_msg(cmd_ctx
, 0);
959 if (cmd_ctx
->session
->kernel_session
== NULL
) {
960 ret
= LTTCOMM_KERN_NO_SESSION
;
964 DBG("Stop kernel tracing");
966 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
968 ERR("Kernel stop session failed");
969 ret
= LTTCOMM_KERN_STOP_FAIL
;
973 /* Clean kernel session teardown */
974 teardown_kernel_session(cmd_ctx
->session
);
979 case LTTNG_CREATE_SESSION
:
981 /* Setup lttng message with no payload */
982 ret
= setup_lttng_msg(cmd_ctx
, 0);
987 ret
= create_session(cmd_ctx
->lsm
->session_name
, &cmd_ctx
->llm
->session_uuid
);
990 ret
= LTTCOMM_EXIST_SESS
;
1000 case LTTNG_DESTROY_SESSION
:
1002 /* Setup lttng message with no payload */
1003 ret
= setup_lttng_msg(cmd_ctx
, 0);
1008 ret
= destroy_session(&cmd_ctx
->lsm
->session_uuid
);
1010 ret
= LTTCOMM_NO_SESS
;
1017 case LTTNG_LIST_TRACES
:
1019 unsigned int trace_count
;
1021 trace_count
= get_trace_count_per_session(cmd_ctx
->session
);
1022 if (trace_count
== 0) {
1023 ret
= LTTCOMM_NO_TRACE
;
1027 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_trace
) * trace_count
);
1032 get_traces_per_session(cmd_ctx
->session
,
1033 (struct lttng_trace
*)(cmd_ctx
->llm
->payload
));
1038 case UST_CREATE_TRACE
:
1040 /* Setup lttng message with no payload */
1041 ret
= setup_lttng_msg(cmd_ctx
, 0);
1046 ret
= ust_create_trace(cmd_ctx
);
1054 unsigned int app_count
;
1056 app_count
= get_app_count();
1057 DBG("Traceable application count : %d", app_count
);
1058 if (app_count
== 0) {
1059 ret
= LTTCOMM_NO_APPS
;
1063 ret
= setup_lttng_msg(cmd_ctx
, sizeof(pid_t
) * app_count
);
1068 get_app_list_pids((pid_t
*)(cmd_ctx
->llm
->payload
));
1073 case UST_START_TRACE
:
1075 /* Setup lttng message with no payload */
1076 ret
= setup_lttng_msg(cmd_ctx
, 0);
1081 ret
= ust_start_trace(cmd_ctx
);
1087 case UST_STOP_TRACE
:
1089 /* Setup lttng message with no payload */
1090 ret
= setup_lttng_msg(cmd_ctx
, 0);
1095 ret
= ust_stop_trace(cmd_ctx
);
1101 case LTTNG_LIST_SESSIONS
:
1103 unsigned int session_count
;
1105 session_count
= get_session_count();
1106 if (session_count
== 0) {
1107 ret
= LTTCOMM_NO_SESS
;
1111 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * session_count
);
1116 get_lttng_session((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
1122 /* Undefined command */
1123 ret
= setup_lttng_msg(cmd_ctx
, 0);
1132 /* Set return code */
1133 cmd_ctx
->llm
->ret_code
= ret
;
1138 if (cmd_ctx
->llm
== NULL
) {
1139 DBG("Missing llm structure. Allocating one.");
1140 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
1144 /* Notify client of error */
1145 cmd_ctx
->llm
->ret_code
= ret
;
1152 * thread_manage_clients
1154 * This thread manage all clients request using the unix
1155 * client socket for communication.
1157 static void *thread_manage_clients(void *data
)
1160 struct command_ctx
*cmd_ctx
;
1162 DBG("[thread] Manage client started");
1164 ret
= lttcomm_listen_unix_sock(client_sock
);
1169 /* Notify parent pid that we are ready
1170 * to accept command for client side.
1172 if (opt_sig_parent
) {
1173 kill(ppid
, SIGCHLD
);
1177 /* Blocking call, waiting for transmission */
1178 DBG("Accepting client command ...");
1179 sock
= lttcomm_accept_unix_sock(client_sock
);
1184 /* Allocate context command to process the client request */
1185 cmd_ctx
= malloc(sizeof(struct command_ctx
));
1187 /* Allocate data buffer for reception */
1188 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
1189 cmd_ctx
->llm
= NULL
;
1190 cmd_ctx
->session
= NULL
;
1193 * Data is received from the lttng client. The struct
1194 * lttcomm_session_msg (lsm) contains the command and data request of
1197 DBG("Receiving data from client ...");
1198 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
1203 // TODO: Validate cmd_ctx including sanity check for security purpose.
1206 * This function dispatch the work to the kernel or userspace tracer
1207 * libs and fill the lttcomm_lttng_msg data structure of all the needed
1208 * informations for the client. The command context struct contains
1209 * everything this function may needs.
1211 ret
= process_client_msg(cmd_ctx
);
1213 /* TODO: Inform client somehow of the fatal error. At this point,
1214 * ret < 0 means that a malloc failed (ENOMEM). */
1215 /* Error detected but still accept command */
1216 clean_command_ctx(cmd_ctx
);
1220 DBG("Sending response (size: %d, retcode: %d)",
1221 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
1222 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
1224 ERR("Failed to send data back to client");
1227 clean_command_ctx(cmd_ctx
);
1229 /* End of transmission */
1239 * usage function on stderr
1241 static void usage(void)
1243 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
1244 fprintf(stderr
, " -h, --help Display this usage.\n");
1245 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
1246 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
1247 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
1248 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
1249 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
1250 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
1251 fprintf(stderr
, " -V, --version Show version number.\n");
1252 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
1253 fprintf(stderr
, " -q, --quiet No output at all.\n");
1254 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
1258 * daemon argument parsing
1260 static int parse_args(int argc
, char **argv
)
1264 static struct option long_options
[] = {
1265 { "client-sock", 1, 0, 'c' },
1266 { "apps-sock", 1, 0, 'a' },
1267 { "kconsumerd-cmd-sock", 1, 0, 0 },
1268 { "kconsumerd-err-sock", 1, 0, 0 },
1269 { "daemonize", 0, 0, 'd' },
1270 { "sig-parent", 0, 0, 'S' },
1271 { "help", 0, 0, 'h' },
1272 { "group", 1, 0, 'g' },
1273 { "version", 0, 0, 'V' },
1274 { "quiet", 0, 0, 'q' },
1275 { "verbose", 0, 0, 'v' },
1280 int option_index
= 0;
1281 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
1288 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
1290 fprintf(stderr
, " with arg %s\n", optarg
);
1294 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1297 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1303 opt_tracing_group
= strdup(optarg
);
1309 fprintf(stdout
, "%s\n", VERSION
);
1315 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1318 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1327 /* Unknown option or other error.
1328 * Error is printed by getopt, just return */
1337 * init_daemon_socket
1339 * Creates the two needed socket by the daemon.
1340 * apps_sock - The communication socket for all UST apps.
1341 * client_sock - The communication of the cli tool (lttng).
1343 static int init_daemon_socket()
1348 old_umask
= umask(0);
1350 /* Create client tool unix socket */
1351 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
1352 if (client_sock
< 0) {
1353 ERR("Create unix sock failed: %s", client_unix_sock_path
);
1358 /* File permission MUST be 660 */
1359 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1361 ERR("Set file permissions failed: %s", client_unix_sock_path
);
1366 /* Create the application unix socket */
1367 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
1368 if (apps_sock
< 0) {
1369 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
1374 /* File permission MUST be 666 */
1375 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
1377 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
1388 * check_existing_daemon
1390 * Check if the global socket is available.
1391 * If yes, error is returned.
1393 static int check_existing_daemon()
1397 ret
= access(client_unix_sock_path
, F_OK
);
1399 ret
= access(apps_unix_sock_path
, F_OK
);
1408 * Return pointer to home directory path using
1409 * the env variable HOME.
1413 static const char *get_home_dir(void)
1415 const char *home_path
;
1417 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
1418 home_path
= default_home_dir
;
1427 * Set the tracing group gid onto the client socket.
1429 * Race window between mkdir and chown is OK because we are going from
1430 * less permissive (root.root) to more permissive (root.tracing).
1432 static int set_permissions(void)
1437 /* Decide which group name to use */
1438 (opt_tracing_group
!= NULL
) ?
1439 (grp
= getgrnam(opt_tracing_group
)) :
1440 (grp
= getgrnam(default_tracing_group
));
1443 ERR("Missing tracing group. Aborting execution.\n");
1448 /* Set lttng run dir */
1449 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
1451 ERR("Unable to set group on " LTTNG_RUNDIR
);
1455 /* lttng client socket path */
1456 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
1458 ERR("Unable to set group on %s", client_unix_sock_path
);
1462 /* kconsumerd error socket path */
1463 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
1465 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
1469 DBG("All permissions are set");
1476 * create_lttng_rundir
1478 * Create the lttng run directory needed for all
1479 * global sockets and pipe.
1481 static int create_lttng_rundir(void)
1485 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
1487 if (errno
!= EEXIST
) {
1488 ERR("Unable to create " LTTNG_RUNDIR
);
1500 * set_kconsumerd_sockets
1502 * Setup sockets and directory needed by the kconsumerd
1503 * communication with the session daemon.
1505 static int set_kconsumerd_sockets(void)
1509 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
1510 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
1513 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
1514 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
1517 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
1519 if (errno
!= EEXIST
) {
1520 ERR("Failed to create " KCONSUMERD_PATH
);
1526 /* Create the kconsumerd error unix socket */
1527 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
1528 if (kconsumerd_err_sock
< 0) {
1529 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
1534 /* File permission MUST be 660 */
1535 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1537 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
1549 * Signal handler for the daemon
1551 static void sighandler(int sig
)
1555 DBG("SIGPIPE catched");
1558 DBG("SIGINT catched");
1562 DBG("SIGTERM catched");
1573 * set_signal_handler
1575 * Setup signal handler for :
1576 * SIGINT, SIGTERM, SIGPIPE
1578 static int set_signal_handler(void)
1581 struct sigaction sa
;
1584 if ((ret
= sigemptyset(&sigset
)) < 0) {
1585 perror("sigemptyset");
1589 sa
.sa_handler
= sighandler
;
1590 sa
.sa_mask
= sigset
;
1592 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1593 perror("sigaction");
1597 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1598 perror("sigaction");
1602 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1603 perror("sigaction");
1607 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
1615 int main(int argc
, char **argv
)
1620 /* Parse arguments */
1622 if ((ret
= parse_args(argc
, argv
) < 0)) {
1635 /* Check if daemon is UID = 0 */
1636 is_root
= !getuid();
1638 /* Set all sockets path */
1640 ret
= create_lttng_rundir();
1645 if (strlen(apps_unix_sock_path
) == 0) {
1646 snprintf(apps_unix_sock_path
, PATH_MAX
,
1647 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
1650 if (strlen(client_unix_sock_path
) == 0) {
1651 snprintf(client_unix_sock_path
, PATH_MAX
,
1652 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
1655 ret
= set_kconsumerd_sockets();
1660 /* Setup kernel tracer */
1661 init_kernel_tracer();
1663 if (strlen(apps_unix_sock_path
) == 0) {
1664 snprintf(apps_unix_sock_path
, PATH_MAX
,
1665 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir());
1668 /* Set the cli tool unix socket path */
1669 if (strlen(client_unix_sock_path
) == 0) {
1670 snprintf(client_unix_sock_path
, PATH_MAX
,
1671 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir());
1675 DBG("Client socket path %s", client_unix_sock_path
);
1676 DBG("Application socket path %s", apps_unix_sock_path
);
1678 /* See if daemon already exist. If any of the two
1679 * socket needed by the daemon are present, this test fails
1681 if ((ret
= check_existing_daemon()) == 0) {
1682 ERR("Already running daemon.\n");
1683 /* We do not goto error because we must not
1684 * cleanup() because a daemon is already running.
1689 if (set_signal_handler() < 0) {
1693 /* Setup the needed unix socket */
1694 if (init_daemon_socket() < 0) {
1698 /* Set credentials to socket */
1699 if (is_root
&& (set_permissions() < 0)) {
1703 /* Get parent pid if -S, --sig-parent is specified. */
1704 if (opt_sig_parent
) {
1709 /* Create thread to manage the client socket */
1710 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
1712 perror("pthread_create");
1716 /* Create thread to manage application socket */
1717 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
1719 perror("pthread_create");
1723 ret
= pthread_join(client_thread
, &status
);
1725 perror("pthread_join");