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 <ust/ustctl.h> /* UST control lib (-lust) */
39 #include <lttng/lttng.h>
41 #include "liblttsessiondcomm.h"
42 #include "ltt-sessiond.h"
44 #include "kernel-ctl.h"
47 #include "traceable-app.h"
51 * teardown: signal SIGTERM handler -> write into pipe. Threads waits
52 * with epoll on pipe and on other pipes/sockets for commands. Main
53 * simply waits on pthread join.
57 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
58 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
59 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
60 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
63 int opt_verbose
; /* Not static for lttngerr.h */
64 int opt_quiet
; /* Not static for lttngerr.h */
66 const char *opt_tracing_group
;
67 static int opt_sig_parent
;
68 static int opt_daemon
;
69 static int is_root
; /* Set to 1 if the daemon is running as root */
70 static pid_t ppid
; /* Parent PID for --sig-parent option */
71 static pid_t kconsumerd_pid
;
73 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
74 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
75 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
76 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
79 static int client_sock
;
81 static int kconsumerd_err_sock
;
82 static int kconsumerd_cmd_sock
;
83 static int kernel_tracer_fd
;
85 /* Pthread, Mutexes and Semaphores */
86 static pthread_t kconsumerd_thread
;
87 static pthread_t apps_thread
;
88 static pthread_t client_thread
;
89 static sem_t kconsumerd_sem
;
91 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
94 * teardown_kernel_session
96 * Complete teardown of a kernel session. This free all data structure
97 * related to a kernel session and update counter.
99 static void teardown_kernel_session(struct ltt_session
*session
)
101 if (session
->kernel_session
!= NULL
) {
102 DBG("Tearing down kernel session");
103 trace_destroy_kernel_session(session
->kernel_session
);
104 /* Extra precaution */
105 session
->kernel_session
= NULL
;
106 /* Decrement session count */
107 session
->kern_session_count
--;
114 * Cleanup the daemon on exit
116 static void cleanup()
120 struct ltt_session
*sess
;
125 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
126 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
129 /* Stopping all threads */
130 DBG("Terminating all threads");
131 pthread_cancel(client_thread
);
132 pthread_cancel(apps_thread
);
133 if (kconsumerd_pid
!= 0) {
134 pthread_cancel(kconsumerd_thread
);
137 DBG("Unlinking all unix socket");
138 unlink(client_unix_sock_path
);
139 unlink(apps_unix_sock_path
);
140 unlink(kconsumerd_err_unix_sock_path
);
142 DBG("Removing %s directory", LTTNG_RUNDIR
);
143 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
145 ERR("asprintf failed. Something is really wrong!");
148 /* Remove lttng run directory */
151 ERR("Unable to clean " LTTNG_RUNDIR
);
154 DBG("Cleaning up all session");
155 /* Cleanup ALL session */
156 cds_list_for_each_entry(sess
, <t_session_list
.head
, list
) {
157 teardown_kernel_session(sess
);
158 // TODO complete session cleanup (including UST)
161 close(kernel_tracer_fd
);
167 * Send data on a unix socket using the liblttsessiondcomm API.
169 * Return lttcomm error code.
171 static int send_unix_sock(int sock
, void *buf
, size_t len
)
173 /* Check valid length */
178 return lttcomm_send_unix_sock(sock
, buf
, len
);
184 * Free memory of a command context structure.
186 static void clean_command_ctx(struct command_ctx
*cmd_ctx
)
188 DBG("Clean command context structure %p", cmd_ctx
);
204 * Return a socket connected to the libust communication socket
205 * of the application identified by the pid.
207 * If the pid is not found in the traceable list,
208 * return -1 to indicate error.
210 static int ust_connect_app(pid_t pid
)
213 struct ltt_traceable_app
*lta
;
215 DBG("Connect to application pid %d", pid
);
217 lta
= find_app_by_pid(pid
);
220 DBG("Application pid %d not found", pid
);
224 sock
= ustctl_connect_pid(lta
->pid
);
226 ERR("Fail connecting to the PID %d\n", pid
);
235 * Notify apps by writing 42 to a named pipe using name.
236 * Every applications waiting for a ltt-sessiond will be notified
237 * and re-register automatically to the session daemon.
239 * Return open or write error value.
241 static int notify_apps(const char *name
)
246 DBG("Notify the global application pipe");
248 /* Try opening the global pipe */
249 fd
= open(name
, O_WRONLY
);
254 /* Notify by writing on the pipe */
255 ret
= write(fd
, "42", 2);
267 * Setup the outgoing data buffer for the response (llm) by allocating the
268 * right amount of memory and copying the original information from the lsm
271 * Return total size of the buffer pointed by buf.
273 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
275 int ret
, buf_size
, trace_name_size
;
278 * Check for the trace_name. If defined, it's part of the payload data of
281 trace_name_size
= strlen(cmd_ctx
->lsm
->trace_name
);
282 buf_size
= trace_name_size
+ size
;
284 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
285 if (cmd_ctx
->llm
== NULL
) {
291 /* Copy common data */
292 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
293 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->pid
;
294 if (!uuid_is_null(cmd_ctx
->lsm
->session_uuid
)) {
295 uuid_copy(cmd_ctx
->llm
->session_uuid
, cmd_ctx
->lsm
->session_uuid
);
298 cmd_ctx
->llm
->trace_name_offset
= trace_name_size
;
299 cmd_ctx
->llm
->data_size
= size
;
300 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
302 /* Copy trace name to the llm structure. Begining of the payload. */
303 memcpy(cmd_ctx
->llm
->payload
, cmd_ctx
->lsm
->trace_name
, trace_name_size
);
312 * thread_manage_kconsumerd
314 * This thread manage the kconsumerd error sent
315 * back to the session daemon.
317 static void *thread_manage_kconsumerd(void *data
)
320 enum lttcomm_return_code code
;
322 DBG("[thread] Manage kconsumerd started");
324 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
329 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
334 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
339 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
340 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
341 if (kconsumerd_cmd_sock
< 0) {
342 perror("kconsumerd connect");
345 /* Signal condition to tell that the kconsumerd is ready */
346 sem_post(&kconsumerd_sem
);
347 DBG("Kconsumerd command socket ready");
349 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
350 lttcomm_get_readable_code(-code
));
355 /* Wait for any kconsumerd error */
356 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
358 ERR("Kconsumerd closed the command socket");
362 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
363 if (code
!= KCONSUMERD_POLL_HUP
) {
370 DBG("Kconsumerd thread dying");
377 * This thread manage the application socket communication
379 static void *thread_manage_apps(void *data
)
383 /* TODO: Something more elegant is needed but fine for now */
384 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
385 * for 32-bit vs 64-bit compat processes. */
386 /* replicate in ust with version number */
388 int reg
; /* 1:register, 0:unregister */
393 DBG("[thread] Manage apps started");
395 ret
= lttcomm_listen_unix_sock(apps_sock
);
400 /* Notify all applications to register */
401 notify_apps(default_global_apps_pipe
);
404 DBG("Accepting application registration");
405 /* Blocking call, waiting for transmission */
406 sock
= lttcomm_accept_unix_sock(apps_sock
);
411 /* Basic recv here to handle the very simple data
412 * that the libust send to register (reg_msg).
414 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
420 /* Add application to the global traceable list */
421 if (reg_msg
.reg
== 1) {
423 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
425 /* register_traceable_app only return an error with
426 * ENOMEM. At this point, we better stop everything.
432 unregister_traceable_app(reg_msg
.pid
);
442 * spawn_kconsumerd_thread
444 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
445 * exec or it will fails.
447 static int spawn_kconsumerd_thread(void)
451 /* Setup semaphore */
452 sem_init(&kconsumerd_sem
, 0, 0);
454 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
456 perror("pthread_create kconsumerd");
460 /* Wait for the kconsumerd thread to be ready */
461 sem_wait(&kconsumerd_sem
);
472 * Fork and exec a kernel consumer daemon (kconsumerd).
474 * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel
475 * file descriptor using the libkernelctl or kernel-ctl functions. So, a
476 * kernel consumer MUST only be spawned before creating a kernel session.
478 * Return pid if successful else -1.
480 static pid_t
spawn_kconsumerd(void)
490 execlp("kconsumerd", "kconsumerd", "--daemonize", NULL
);
492 perror("kernel start consumer exec");
495 } else if (pid
> 0) {
499 perror("kernel start consumer fork");
511 * Spawn the kconsumerd daemon and session daemon thread.
513 static int start_kconsumerd(void)
517 DBG("Spawning kconsumerd");
519 pthread_mutex_lock(&kconsumerd_pid_mutex
);
520 if (kconsumerd_pid
== 0) {
521 ret
= spawn_kconsumerd();
523 ERR("Spawning kconsumerd failed");
524 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
525 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
529 /* Setting up the global kconsumerd_pid */
530 kconsumerd_pid
= ret
;
532 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
534 DBG("Kconsumerd pid %d", ret
);
536 DBG("Spawning kconsumerd thread");
538 ret
= spawn_kconsumerd_thread();
540 ERR("Fatal error spawning kconsumerd thread");
552 * send_kconsumerd_fds
554 * Send all stream fds of the kernel session to the consumer.
556 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
559 /* Plus one here for the metadata fd */
560 size_t nb_fd
= session
->stream_count_global
+ 1;
562 struct ltt_kernel_stream
*stream
;
563 struct ltt_kernel_channel
*chan
;
564 struct lttcomm_kconsumerd_header lkh
;
565 struct lttcomm_kconsumerd_msg buf
[nb_fd
];
567 /* Add metadata data */
568 fds
[i
] = session
->metadata_stream_fd
;
570 buf
[i
].state
= ACTIVE_FD
;
571 buf
[i
].max_sb_size
= session
->metadata
->conf
->subbuf_size
;
572 strncpy(buf
[i
].path_name
, session
->metadata
->pathname
, PATH_MAX
);
574 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
575 cds_list_for_each_entry(stream
, &chan
->stream_list
.head
, list
) {
578 buf
[i
].fd
= stream
->fd
;
579 buf
[i
].state
= stream
->state
;
580 buf
[i
].max_sb_size
= chan
->channel
->subbuf_size
;
581 strncpy(buf
[i
].path_name
, stream
->pathname
, PATH_MAX
);
586 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
587 lkh
.cmd_type
= LTTCOMM_ADD_STREAM
;
589 DBG("Sending kconsumerd header");
591 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
593 perror("send kconsumerd header");
597 DBG("Sending all fds to kconsumerd");
599 ret
= lttcomm_send_fds_unix_sock(sock
, buf
, fds
, nb_fd
, lkh
.payload_size
);
601 perror("send kconsumerd fds");
605 DBG("Kconsumerd fds sent");
616 * Create the trace output directory.
618 static int create_trace_dir(struct ltt_kernel_session
*session
)
621 struct ltt_kernel_channel
*chan
;
623 /* Create all channel directories */
624 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
625 DBG("Creating trace directory at %s", chan
->pathname
);
626 ret
= mkdir(chan
->pathname
, S_IRWXU
| S_IRWXG
);
628 perror("mkdir trace path");
643 * Process the command requested by the lttng client within the command
644 * context structure. This function make sure that the return structure (llm)
645 * is set and ready for transmission before returning.
647 * Return any error encountered or 0 for success.
649 static int process_client_msg(struct command_ctx
*cmd_ctx
)
653 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
655 /* Check command that needs a session */
656 switch (cmd_ctx
->lsm
->cmd_type
) {
657 case LTTNG_CREATE_SESSION
:
658 case LTTNG_LIST_SESSIONS
:
662 cmd_ctx
->session
= find_session_by_uuid(cmd_ctx
->lsm
->session_uuid
);
663 if (cmd_ctx
->session
== NULL
) {
664 ret
= LTTCOMM_SELECT_SESS
;
670 /* Check command for kernel tracing */
671 switch (cmd_ctx
->lsm
->cmd_type
) {
672 case KERNEL_CREATE_SESSION
:
673 case KERNEL_CREATE_CHANNEL
:
674 case KERNEL_CREATE_STREAM
:
675 case KERNEL_DISABLE_EVENT
:
676 case KERNEL_ENABLE_EVENT
:
677 case KERNEL_OPEN_METADATA
:
678 case KERNEL_START_TRACE
:
679 case KERNEL_STOP_TRACE
:
680 /* TODO: reconnect to kernel tracer to check if
682 if (kernel_tracer_fd
== 0) {
683 ret
= LTTCOMM_KERN_NA
;
689 /* Connect to ust apps if available pid */
690 if (cmd_ctx
->lsm
->pid
> 0) {
691 /* Connect to app using ustctl API */
692 cmd_ctx
->ust_sock
= ust_connect_app(cmd_ctx
->lsm
->pid
);
693 if (cmd_ctx
->ust_sock
< 0) {
694 ret
= LTTCOMM_NO_TRACEABLE
;
699 /* Process by command type */
700 switch (cmd_ctx
->lsm
->cmd_type
) {
701 case KERNEL_CREATE_SESSION
:
703 ret
= setup_lttng_msg(cmd_ctx
, 0);
708 ret
= start_kconsumerd();
713 DBG("Creating kernel session");
715 ret
= kernel_create_session(cmd_ctx
->session
, kernel_tracer_fd
);
717 ret
= LTTCOMM_KERN_SESS_FAIL
;
724 case KERNEL_CREATE_CHANNEL
:
726 ret
= setup_lttng_msg(cmd_ctx
, 0);
731 DBG("Creating kernel channel");
733 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
);
736 ret
= LTTCOMM_KERN_CHAN_FAIL
;
743 case KERNEL_ENABLE_EVENT
:
745 /* Setup lttng message with no payload */
746 ret
= setup_lttng_msg(cmd_ctx
, 0);
751 DBG("Enabling kernel event %s", cmd_ctx
->lsm
->u
.event
.event_name
);
753 ret
= kernel_enable_event(cmd_ctx
->session
->kernel_session
, cmd_ctx
->lsm
->u
.event
.event_name
);
755 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
762 case KERNEL_OPEN_METADATA
:
764 /* Setup lttng message with no payload */
765 ret
= setup_lttng_msg(cmd_ctx
, 0);
770 DBG("Open kernel metadata");
772 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
);
774 ret
= LTTCOMM_KERN_META_FAIL
;
781 case KERNEL_CREATE_STREAM
:
783 struct ltt_kernel_channel
*chan
;
784 /* Setup lttng message with no payload */
785 ret
= setup_lttng_msg(cmd_ctx
, 0);
790 DBG("Creating kernel stream");
792 ret
= kernel_create_metadata_stream(cmd_ctx
->session
->kernel_session
);
794 ERR("Kernel create metadata stream failed");
795 ret
= LTTCOMM_KERN_STREAM_FAIL
;
799 /* For each channel */
800 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
801 ret
= kernel_create_channel_stream(chan
);
803 ERR("Kernel create channel stream failed");
804 ret
= LTTCOMM_KERN_STREAM_FAIL
;
807 /* Update the stream global counter */
808 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
814 case KERNEL_START_TRACE
:
816 /* Setup lttng message with no payload */
817 ret
= setup_lttng_msg(cmd_ctx
, 0);
822 DBG("Start kernel tracing");
824 ret
= create_trace_dir(cmd_ctx
->session
->kernel_session
);
826 if (ret
== -EEXIST
) {
827 ret
= LTTCOMM_KERN_DIR_EXIST
;
829 ret
= LTTCOMM_KERN_DIR_FAIL
;
834 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
836 ERR("Kernel start session failed");
837 ret
= LTTCOMM_KERN_START_FAIL
;
841 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, cmd_ctx
->session
->kernel_session
);
843 ERR("Send kconsumerd fds failed");
844 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
851 case KERNEL_STOP_TRACE
:
853 /* Setup lttng message with no payload */
854 ret
= setup_lttng_msg(cmd_ctx
, 0);
859 if (cmd_ctx
->session
->kernel_session
== NULL
) {
860 ret
= LTTCOMM_KERN_NO_SESSION
;
864 DBG("Stop kernel tracing");
866 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
868 ERR("Kernel stop session failed");
869 ret
= LTTCOMM_KERN_STOP_FAIL
;
873 /* Clean kernel session teardown */
874 teardown_kernel_session(cmd_ctx
->session
);
879 case LTTNG_CREATE_SESSION
:
881 /* Setup lttng message with no payload */
882 ret
= setup_lttng_msg(cmd_ctx
, 0);
887 ret
= create_session(cmd_ctx
->lsm
->session_name
, &cmd_ctx
->llm
->session_uuid
);
890 ret
= LTTCOMM_EXIST_SESS
;
900 case LTTNG_DESTROY_SESSION
:
902 /* Setup lttng message with no payload */
903 ret
= setup_lttng_msg(cmd_ctx
, 0);
908 ret
= destroy_session(&cmd_ctx
->lsm
->session_uuid
);
910 ret
= LTTCOMM_NO_SESS
;
917 case LTTNG_LIST_TRACES
:
919 unsigned int trace_count
;
921 trace_count
= get_trace_count_per_session(cmd_ctx
->session
);
922 if (trace_count
== 0) {
923 ret
= LTTCOMM_NO_TRACE
;
927 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_trace
) * trace_count
);
932 get_traces_per_session(cmd_ctx
->session
,
933 (struct lttng_trace
*)(cmd_ctx
->llm
->payload
));
938 case UST_CREATE_TRACE
:
940 /* Setup lttng message with no payload */
941 ret
= setup_lttng_msg(cmd_ctx
, 0);
946 ret
= ust_create_trace(cmd_ctx
);
954 unsigned int app_count
;
956 app_count
= get_app_count();
957 DBG("Traceable application count : %d", app_count
);
958 if (app_count
== 0) {
959 ret
= LTTCOMM_NO_APPS
;
963 ret
= setup_lttng_msg(cmd_ctx
, sizeof(pid_t
) * app_count
);
968 get_app_list_pids((pid_t
*)(cmd_ctx
->llm
->payload
));
973 case UST_START_TRACE
:
975 /* Setup lttng message with no payload */
976 ret
= setup_lttng_msg(cmd_ctx
, 0);
981 ret
= ust_start_trace(cmd_ctx
);
989 /* Setup lttng message with no payload */
990 ret
= setup_lttng_msg(cmd_ctx
, 0);
995 ret
= ust_stop_trace(cmd_ctx
);
1001 case LTTNG_LIST_SESSIONS
:
1003 unsigned int session_count
;
1005 session_count
= get_session_count();
1006 if (session_count
== 0) {
1007 ret
= LTTCOMM_NO_SESS
;
1011 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * session_count
);
1016 get_lttng_session((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
1022 /* Undefined command */
1023 ret
= setup_lttng_msg(cmd_ctx
, 0);
1032 /* Set return code */
1033 cmd_ctx
->llm
->ret_code
= ret
;
1038 if (cmd_ctx
->llm
== NULL
) {
1039 DBG("Missing llm structure. Allocating one.");
1040 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
1044 /* Notify client of error */
1045 cmd_ctx
->llm
->ret_code
= ret
;
1052 * thread_manage_clients
1054 * This thread manage all clients request using the unix
1055 * client socket for communication.
1057 static void *thread_manage_clients(void *data
)
1060 struct command_ctx
*cmd_ctx
;
1062 DBG("[thread] Manage client started");
1064 ret
= lttcomm_listen_unix_sock(client_sock
);
1069 /* Notify parent pid that we are ready
1070 * to accept command for client side.
1072 if (opt_sig_parent
) {
1073 kill(ppid
, SIGCHLD
);
1077 /* Blocking call, waiting for transmission */
1078 DBG("Accepting client command ...");
1079 sock
= lttcomm_accept_unix_sock(client_sock
);
1084 /* Allocate context command to process the client request */
1085 cmd_ctx
= malloc(sizeof(struct command_ctx
));
1087 /* Allocate data buffer for reception */
1088 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
1089 cmd_ctx
->llm
= NULL
;
1090 cmd_ctx
->session
= NULL
;
1093 * Data is received from the lttng client. The struct
1094 * lttcomm_session_msg (lsm) contains the command and data request of
1097 DBG("Receiving data from client ...");
1098 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
1104 * This function dispatch the work to the kernel or userspace tracer
1105 * libs and fill the lttcomm_lttng_msg data structure of all the needed
1106 * informations for the client. The command context struct contains
1107 * everything this function may needs.
1109 ret
= process_client_msg(cmd_ctx
);
1111 /* TODO: Inform client somehow of the fatal error. At this point,
1112 * ret < 0 means that a malloc failed (ENOMEM). */
1113 /* Error detected but still accept command */
1114 clean_command_ctx(cmd_ctx
);
1118 DBG("Sending response (size: %d, retcode: %d)",
1119 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
1120 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
1122 ERR("Failed to send data back to client");
1125 clean_command_ctx(cmd_ctx
);
1134 * usage function on stderr
1136 static void usage(void)
1138 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
1139 fprintf(stderr
, " -h, --help Display this usage.\n");
1140 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
1141 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
1142 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
1143 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
1144 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
1145 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
1146 fprintf(stderr
, " -V, --version Show version number.\n");
1147 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
1148 fprintf(stderr
, " -q, --quiet No output at all.\n");
1149 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
1153 * daemon argument parsing
1155 static int parse_args(int argc
, char **argv
)
1159 static struct option long_options
[] = {
1160 { "client-sock", 1, 0, 'c' },
1161 { "apps-sock", 1, 0, 'a' },
1162 { "kconsumerd-cmd-sock", 1, 0, 0 },
1163 { "kconsumerd-err-sock", 1, 0, 0 },
1164 { "daemonize", 0, 0, 'd' },
1165 { "sig-parent", 0, 0, 'S' },
1166 { "help", 0, 0, 'h' },
1167 { "group", 1, 0, 'g' },
1168 { "version", 0, 0, 'V' },
1169 { "quiet", 0, 0, 'q' },
1170 { "verbose", 0, 0, 'v' },
1175 int option_index
= 0;
1176 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
1183 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
1185 fprintf(stderr
, " with arg %s\n", optarg
);
1189 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1192 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1198 opt_tracing_group
= strdup(optarg
);
1204 fprintf(stdout
, "%s\n", VERSION
);
1210 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1213 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1222 /* Unknown option or other error.
1223 * Error is printed by getopt, just return */
1232 * init_daemon_socket
1234 * Creates the two needed socket by the daemon.
1235 * apps_sock - The communication socket for all UST apps.
1236 * client_sock - The communication of the cli tool (lttng).
1238 static int init_daemon_socket()
1243 old_umask
= umask(0);
1245 /* Create client tool unix socket */
1246 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
1247 if (client_sock
< 0) {
1248 ERR("Create unix sock failed: %s", client_unix_sock_path
);
1253 /* File permission MUST be 660 */
1254 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1256 ERR("Set file permissions failed: %s", client_unix_sock_path
);
1261 /* Create the application unix socket */
1262 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
1263 if (apps_sock
< 0) {
1264 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
1269 /* File permission MUST be 666 */
1270 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
1272 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
1283 * check_existing_daemon
1285 * Check if the global socket is available.
1286 * If yes, error is returned.
1288 static int check_existing_daemon()
1292 ret
= access(client_unix_sock_path
, F_OK
);
1294 ret
= access(apps_unix_sock_path
, F_OK
);
1303 * Return pointer to home directory path using
1304 * the env variable HOME.
1308 static const char *get_home_dir(void)
1310 const char *home_path
;
1312 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
1313 home_path
= default_home_dir
;
1322 * Set the tracing group gid onto the client socket.
1324 * Race window between mkdir and chown is OK because we are going from
1325 * less permissive (root.root) to more permissive (root.tracing).
1327 static int set_permissions(void)
1332 /* Decide which group name to use */
1333 (opt_tracing_group
!= NULL
) ?
1334 (grp
= getgrnam(opt_tracing_group
)) :
1335 (grp
= getgrnam(default_tracing_group
));
1338 ERR("Missing tracing group. Aborting execution.\n");
1343 /* Set lttng run dir */
1344 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
1346 ERR("Unable to set group on " LTTNG_RUNDIR
);
1350 /* lttng client socket path */
1351 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
1353 ERR("Unable to set group on %s", client_unix_sock_path
);
1357 /* kconsumerd error socket path */
1358 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
1360 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
1364 DBG("All permissions are set");
1371 * create_lttng_rundir
1373 * Create the lttng run directory needed for all
1374 * global sockets and pipe.
1376 static int create_lttng_rundir(void)
1380 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
1382 ERR("Unable to create " LTTNG_RUNDIR
);
1391 * init_kernel_tracer
1393 * Setup necessary data for kernel tracer action.
1395 static void init_kernel_tracer(void)
1397 /* Set the global kernel tracer fd */
1398 kernel_tracer_fd
= open(DEFAULT_KERNEL_TRACER_PATH
, O_RDWR
);
1399 if (kernel_tracer_fd
< 0) {
1400 WARN("No kernel tracer available");
1401 kernel_tracer_fd
= 0;
1404 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1408 * set_kconsumerd_sockets
1410 * Setup sockets and directory needed by the kconsumerd
1411 * communication with the session daemon.
1413 static int set_kconsumerd_sockets(void)
1417 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
1418 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
1421 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
1422 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
1425 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
1427 ERR("Failed to create " KCONSUMERD_PATH
);
1431 /* Create the kconsumerd error unix socket */
1432 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
1433 if (kconsumerd_err_sock
< 0) {
1434 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
1439 /* File permission MUST be 660 */
1440 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1442 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
1454 * Signal handler for the daemon
1456 static void sighandler(int sig
)
1460 DBG("SIGPIPE catched");
1463 DBG("SIGINT catched");
1467 DBG("SIGTERM catched");
1478 * set_signal_handler
1480 * Setup signal handler for :
1481 * SIGINT, SIGTERM, SIGPIPE
1483 static int set_signal_handler(void)
1486 struct sigaction sa
;
1489 if ((ret
= sigemptyset(&sigset
)) < 0) {
1490 perror("sigemptyset");
1494 sa
.sa_handler
= sighandler
;
1495 sa
.sa_mask
= sigset
;
1497 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1498 perror("sigaction");
1502 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1503 perror("sigaction");
1507 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1508 perror("sigaction");
1512 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
1520 int main(int argc
, char **argv
)
1525 /* Parse arguments */
1527 if ((ret
= parse_args(argc
, argv
) < 0)) {
1540 /* Check if daemon is UID = 0 */
1541 is_root
= !getuid();
1543 /* Set all sockets path */
1545 ret
= create_lttng_rundir();
1550 if (strlen(apps_unix_sock_path
) == 0) {
1551 snprintf(apps_unix_sock_path
, PATH_MAX
,
1552 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
1555 if (strlen(client_unix_sock_path
) == 0) {
1556 snprintf(client_unix_sock_path
, PATH_MAX
,
1557 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
1560 ret
= set_kconsumerd_sockets();
1565 /* Setup kernel tracer */
1566 init_kernel_tracer();
1568 if (strlen(apps_unix_sock_path
) == 0) {
1569 snprintf(apps_unix_sock_path
, PATH_MAX
,
1570 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir());
1573 /* Set the cli tool unix socket path */
1574 if (strlen(client_unix_sock_path
) == 0) {
1575 snprintf(client_unix_sock_path
, PATH_MAX
,
1576 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir());
1580 DBG("Client socket path %s", client_unix_sock_path
);
1581 DBG("Application socket path %s", apps_unix_sock_path
);
1583 /* See if daemon already exist. If any of the two
1584 * socket needed by the daemon are present, this test fails
1586 if ((ret
= check_existing_daemon()) == 0) {
1587 ERR("Already running daemon.\n");
1588 /* We do not goto error because we must not
1589 * cleanup() because a daemon is already running.
1594 if (set_signal_handler() < 0) {
1598 /* Setup the needed unix socket */
1599 if (init_daemon_socket() < 0) {
1603 /* Set credentials to socket */
1604 if (is_root
&& (set_permissions() < 0)) {
1608 /* Get parent pid if -S, --sig-parent is specified. */
1609 if (opt_sig_parent
) {
1614 /* Create thread to manage the client socket */
1615 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
1617 perror("pthread_create");
1621 /* Create thread to manage application socket */
1622 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
1624 perror("pthread_create");
1628 ret
= pthread_join(client_thread
, &status
);
1630 perror("pthread_join");