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 */
96 * Free all data structure inside a kernel session and the session pointer.
98 static void free_kernel_session(struct ltt_kernel_session
*session
)
100 struct ltt_kernel_channel
*chan
;
101 struct ltt_kernel_stream
*stream
;
102 struct ltt_kernel_event
*event
;
105 close(session
->metadata_stream_fd
);
106 close(session
->metadata
->fd
);
107 free(session
->metadata
->conf
);
108 free(session
->metadata
);
110 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
111 /* Clean all event(s) */
112 cds_list_for_each_entry(event
, &chan
->events_list
.head
, list
) {
119 cds_list_for_each_entry(stream
, &chan
->stream_list
.head
, list
) {
121 free(stream
->pathname
);
127 free(chan
->pathname
);
134 DBG("All kernel session data structures freed");
138 * teardown_kernel_session
140 * Complete teardown of a kernel session. This free all data structure
141 * related to a kernel session and update counter.
143 static void teardown_kernel_session(struct ltt_session
*session
)
145 if (session
->kernel_session
!= NULL
) {
146 DBG("Tearing down kernel session");
147 free_kernel_session(session
->kernel_session
);
148 /* Extra precaution */
149 session
->kernel_session
= NULL
;
150 /* Decrement session count */
151 session
->kern_session_count
--;
158 * Cleanup the daemon on exit
160 static void cleanup()
164 struct ltt_session
*sess
;
169 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
170 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
173 /* Stopping all threads */
174 DBG("Terminating all threads");
175 pthread_cancel(client_thread
);
176 pthread_cancel(apps_thread
);
177 if (kconsumerd_pid
!= 0) {
178 pthread_cancel(kconsumerd_thread
);
181 DBG("Unlinking all unix socket");
182 unlink(client_unix_sock_path
);
183 unlink(apps_unix_sock_path
);
184 unlink(kconsumerd_err_unix_sock_path
);
186 DBG("Removing %s directory", LTTNG_RUNDIR
);
187 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
189 ERR("asprintf failed. Something is really wrong!");
192 /* Remove lttng run directory */
195 ERR("Unable to clean " LTTNG_RUNDIR
);
198 DBG("Cleaning up all session");
199 /* Cleanup ALL session */
200 cds_list_for_each_entry(sess
, <t_session_list
.head
, list
) {
201 teardown_kernel_session(sess
);
202 // TODO complete session cleanup (including UST)
205 close(kernel_tracer_fd
);
211 * Send data on a unix socket using the liblttsessiondcomm API.
213 * Return lttcomm error code.
215 static int send_unix_sock(int sock
, void *buf
, size_t len
)
217 /* Check valid length */
222 return lttcomm_send_unix_sock(sock
, buf
, len
);
228 * Free memory of a command context structure.
230 static void clean_command_ctx(struct command_ctx
*cmd_ctx
)
232 DBG("Clean command context structure %p", cmd_ctx
);
248 * Return a socket connected to the libust communication socket
249 * of the application identified by the pid.
251 * If the pid is not found in the traceable list,
252 * return -1 to indicate error.
254 static int ust_connect_app(pid_t pid
)
257 struct ltt_traceable_app
*lta
;
259 DBG("Connect to application pid %d", pid
);
261 lta
= find_app_by_pid(pid
);
264 DBG("Application pid %d not found", pid
);
268 sock
= ustctl_connect_pid(lta
->pid
);
270 ERR("Fail connecting to the PID %d\n", pid
);
279 * Notify apps by writing 42 to a named pipe using name.
280 * Every applications waiting for a ltt-sessiond will be notified
281 * and re-register automatically to the session daemon.
283 * Return open or write error value.
285 static int notify_apps(const char *name
)
290 DBG("Notify the global application pipe");
292 /* Try opening the global pipe */
293 fd
= open(name
, O_WRONLY
);
298 /* Notify by writing on the pipe */
299 ret
= write(fd
, "42", 2);
311 * Setup the outgoing data buffer for the response (llm) by allocating the
312 * right amount of memory and copying the original information from the lsm
315 * Return total size of the buffer pointed by buf.
317 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
319 int ret
, buf_size
, trace_name_size
;
322 * Check for the trace_name. If defined, it's part of the payload data of
325 trace_name_size
= strlen(cmd_ctx
->lsm
->trace_name
);
326 buf_size
= trace_name_size
+ size
;
328 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
329 if (cmd_ctx
->llm
== NULL
) {
335 /* Copy common data */
336 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
337 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->pid
;
338 if (!uuid_is_null(cmd_ctx
->lsm
->session_uuid
)) {
339 uuid_copy(cmd_ctx
->llm
->session_uuid
, cmd_ctx
->lsm
->session_uuid
);
342 cmd_ctx
->llm
->trace_name_offset
= trace_name_size
;
343 cmd_ctx
->llm
->data_size
= size
;
344 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
346 /* Copy trace name to the llm structure. Begining of the payload. */
347 memcpy(cmd_ctx
->llm
->payload
, cmd_ctx
->lsm
->trace_name
, trace_name_size
);
356 * thread_manage_kconsumerd
358 * This thread manage the kconsumerd error sent
359 * back to the session daemon.
361 static void *thread_manage_kconsumerd(void *data
)
364 enum lttcomm_return_code code
;
366 DBG("[thread] Manage kconsumerd started");
368 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
373 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
378 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
383 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
384 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
385 if (kconsumerd_cmd_sock
< 0) {
386 perror("kconsumerd connect");
389 /* Signal condition to tell that the kconsumerd is ready */
390 sem_post(&kconsumerd_sem
);
391 DBG("Kconsumerd command socket ready");
393 DBG("[sessiond] Kconsumerd error when waiting for SOCK_READY : %s",
394 lttcomm_get_readable_code(-code
));
398 /* Wait for any kconsumerd error */
399 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
401 ERR("[sessiond] Kconsumerd closed the command socket");
405 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
415 * This thread manage the application socket communication
417 static void *thread_manage_apps(void *data
)
421 /* TODO: Something more elegant is needed but fine for now */
422 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
423 * for 32-bit vs 64-bit compat processes. */
424 /* replicate in ust with version number */
426 int reg
; /* 1:register, 0:unregister */
431 DBG("[thread] Manage apps started");
433 ret
= lttcomm_listen_unix_sock(apps_sock
);
438 /* Notify all applications to register */
439 notify_apps(default_global_apps_pipe
);
442 DBG("Accepting application registration");
443 /* Blocking call, waiting for transmission */
444 sock
= lttcomm_accept_unix_sock(apps_sock
);
449 /* Basic recv here to handle the very simple data
450 * that the libust send to register (reg_msg).
452 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
458 /* Add application to the global traceable list */
459 if (reg_msg
.reg
== 1) {
461 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
463 /* register_traceable_app only return an error with
464 * ENOMEM. At this point, we better stop everything.
470 unregister_traceable_app(reg_msg
.pid
);
480 * start_kconsumerd_thread
482 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
483 * exec or it will fails.
485 static int start_kconsumerd_thread(void)
489 /* Setup semaphore */
490 sem_init(&kconsumerd_sem
, 0, 0);
492 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
494 perror("pthread_create kconsumerd");
498 sem_wait(&kconsumerd_sem
);
507 * kernel_start_consumer
509 * Start a kernel consumer daemon (kconsumerd).
511 * Return pid if successful else -1.
513 pid_t
kernel_start_consumer(void)
523 execlp("kconsumerd", "kconsumerd", "--daemonize", NULL
);
525 perror("kernel start consumer exec");
528 } else if (pid
> 0) {
532 perror("kernel start consumer fork");
542 * send_kconsumerd_fds
544 * Send all stream fds of the kernel session to the consumer.
546 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
549 /* Plus one here for the metadata fd */
550 size_t nb_fd
= session
->stream_count_global
+ 1;
552 struct ltt_kernel_stream
*stream
;
553 struct ltt_kernel_channel
*chan
;
554 struct lttcomm_kconsumerd_header lkh
;
555 struct lttcomm_kconsumerd_msg buf
[nb_fd
];
557 /* Add metadata data */
558 fds
[i
] = session
->metadata_stream_fd
;
560 buf
[i
].state
= ACTIVE_FD
;
561 buf
[i
].max_sb_size
= session
->metadata
->conf
->subbuf_size
;
562 strncpy(buf
[i
].path_name
, session
->metadata
->pathname
, PATH_MAX
);
564 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
565 cds_list_for_each_entry(stream
, &chan
->stream_list
.head
, list
) {
568 buf
[i
].fd
= stream
->fd
;
569 buf
[i
].state
= stream
->state
;
570 buf
[i
].max_sb_size
= chan
->channel
->subbuf_size
;
571 strncpy(buf
[i
].path_name
, stream
->pathname
, PATH_MAX
);
576 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
577 lkh
.cmd_type
= LTTCOMM_ADD_STREAM
;
579 DBG("Sending kconsumerd header");
581 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
583 perror("send kconsumerd header");
587 DBG("Sending all fds to kconsumerd");
589 ret
= lttcomm_send_fds_unix_sock(sock
, buf
, fds
, nb_fd
, lkh
.payload_size
);
591 perror("send kconsumerd fds");
595 DBG("Kconsumerd fds sent");
606 * Create the trace output directory.
608 static int create_trace_dir(struct ltt_kernel_session
*session
)
611 struct ltt_kernel_channel
*chan
;
613 /* Create all channel directories */
614 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
615 ret
= mkdir(chan
->pathname
, S_IRWXU
| S_IRWXG
);
617 perror("mkdir trace path");
633 * Process the command requested by the lttng client within the command
634 * context structure. This function make sure that the return structure (llm)
635 * is set and ready for transmission before returning.
637 * Return any error encountered or 0 for success.
639 static int process_client_msg(struct command_ctx
*cmd_ctx
)
643 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
645 /* Check command that needs a session */
646 switch (cmd_ctx
->lsm
->cmd_type
) {
647 case LTTNG_CREATE_SESSION
:
648 case LTTNG_LIST_SESSIONS
:
652 cmd_ctx
->session
= find_session_by_uuid(cmd_ctx
->lsm
->session_uuid
);
653 if (cmd_ctx
->session
== NULL
) {
654 ret
= LTTCOMM_SELECT_SESS
;
660 /* Check command for kernel tracing */
661 switch (cmd_ctx
->lsm
->cmd_type
) {
662 case KERNEL_CREATE_SESSION
:
663 case KERNEL_CREATE_CHANNEL
:
664 case KERNEL_CREATE_STREAM
:
665 case KERNEL_DISABLE_EVENT
:
666 case KERNEL_ENABLE_EVENT
:
667 case KERNEL_OPEN_METADATA
:
668 case KERNEL_START_TRACE
:
669 case KERNEL_STOP_TRACE
:
670 /* TODO: reconnect to kernel tracer to check if
672 if (kernel_tracer_fd
== 0) {
673 ret
= LTTCOMM_KERN_NA
;
679 /* Connect to ust apps if available pid */
680 if (cmd_ctx
->lsm
->pid
> 0) {
681 /* Connect to app using ustctl API */
682 cmd_ctx
->ust_sock
= ust_connect_app(cmd_ctx
->lsm
->pid
);
683 if (cmd_ctx
->ust_sock
< 0) {
684 ret
= LTTCOMM_NO_TRACEABLE
;
689 /* Process by command type */
690 switch (cmd_ctx
->lsm
->cmd_type
) {
691 case KERNEL_CREATE_SESSION
:
693 ret
= setup_lttng_msg(cmd_ctx
, 0);
698 DBG("Checking if kconsumerd is alive");
699 pthread_mutex_lock(&kconsumerd_pid_mutex
);
700 if (kconsumerd_pid
== 0) {
701 ret
= kernel_start_consumer();
703 ERR("Kernel start kconsumerd failed");
704 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
705 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
709 /* Setting up the global kconsumerd_pid */
710 kconsumerd_pid
= ret
;
712 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
714 ret
= start_kconsumerd_thread();
716 ERR("Fatal error : start_kconsumerd_thread()");
721 DBG("Creating kernel session");
723 ret
= kernel_create_session(cmd_ctx
->session
, kernel_tracer_fd
);
725 ret
= LTTCOMM_KERN_SESS_FAIL
;
732 case KERNEL_CREATE_CHANNEL
:
734 ret
= setup_lttng_msg(cmd_ctx
, 0);
739 DBG("Creating kernel channel");
741 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
);
744 ret
= LTTCOMM_KERN_CHAN_FAIL
;
751 case KERNEL_ENABLE_EVENT
:
753 /* Setup lttng message with no payload */
754 ret
= setup_lttng_msg(cmd_ctx
, 0);
759 DBG("Enabling kernel event %s", cmd_ctx
->lsm
->u
.event
.event_name
);
761 ret
= kernel_enable_event(cmd_ctx
->session
->kernel_session
, cmd_ctx
->lsm
->u
.event
.event_name
);
763 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
770 case KERNEL_OPEN_METADATA
:
772 /* Setup lttng message with no payload */
773 ret
= setup_lttng_msg(cmd_ctx
, 0);
778 DBG("Open kernel metadata");
780 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
);
782 ret
= LTTCOMM_KERN_META_FAIL
;
789 case KERNEL_CREATE_STREAM
:
791 struct ltt_kernel_channel
*chan
;
792 /* Setup lttng message with no payload */
793 ret
= setup_lttng_msg(cmd_ctx
, 0);
798 DBG("Creating kernel stream");
800 ret
= kernel_create_metadata_stream(cmd_ctx
->session
->kernel_session
);
802 ERR("Kernel create metadata stream failed");
803 ret
= LTTCOMM_KERN_STREAM_FAIL
;
807 /* For each channel */
808 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
809 ret
= kernel_create_channel_stream(chan
);
811 ERR("Kernel create channel stream failed");
812 ret
= LTTCOMM_KERN_STREAM_FAIL
;
815 /* Update the stream global counter */
816 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
822 case KERNEL_START_TRACE
:
824 /* Setup lttng message with no payload */
825 ret
= setup_lttng_msg(cmd_ctx
, 0);
830 DBG("Start kernel tracing");
832 ret
= create_trace_dir(cmd_ctx
->session
->kernel_session
);
834 if (ret
== -EEXIST
) {
835 ret
= LTTCOMM_KERN_DIR_EXIST
;
837 ret
= LTTCOMM_KERN_DIR_FAIL
;
842 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
844 ERR("Kernel start session failed");
845 ret
= LTTCOMM_KERN_START_FAIL
;
849 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, cmd_ctx
->session
->kernel_session
);
851 ERR("Send kconsumerd fds failed");
852 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
859 case KERNEL_STOP_TRACE
:
861 /* Setup lttng message with no payload */
862 ret
= setup_lttng_msg(cmd_ctx
, 0);
867 DBG("Stop kernel tracing");
869 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
871 ERR("Kernel stop session failed");
872 ret
= LTTCOMM_KERN_STOP_FAIL
;
876 /* Clean kernel session teardown */
877 teardown_kernel_session(cmd_ctx
->session
);
882 case LTTNG_CREATE_SESSION
:
884 /* Setup lttng message with no payload */
885 ret
= setup_lttng_msg(cmd_ctx
, 0);
890 ret
= create_session(cmd_ctx
->lsm
->session_name
, &cmd_ctx
->llm
->session_uuid
);
893 ret
= LTTCOMM_EXIST_SESS
;
903 case LTTNG_DESTROY_SESSION
:
905 /* Setup lttng message with no payload */
906 ret
= setup_lttng_msg(cmd_ctx
, 0);
911 ret
= destroy_session(&cmd_ctx
->lsm
->session_uuid
);
913 ret
= LTTCOMM_NO_SESS
;
920 case LTTNG_LIST_TRACES
:
922 unsigned int trace_count
;
924 trace_count
= get_trace_count_per_session(cmd_ctx
->session
);
925 if (trace_count
== 0) {
926 ret
= LTTCOMM_NO_TRACE
;
930 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_trace
) * trace_count
);
935 get_traces_per_session(cmd_ctx
->session
,
936 (struct lttng_trace
*)(cmd_ctx
->llm
->payload
));
941 case UST_CREATE_TRACE
:
943 /* Setup lttng message with no payload */
944 ret
= setup_lttng_msg(cmd_ctx
, 0);
949 ret
= ust_create_trace(cmd_ctx
);
957 unsigned int app_count
;
959 app_count
= get_app_count();
960 DBG("Traceable application count : %d", app_count
);
961 if (app_count
== 0) {
962 ret
= LTTCOMM_NO_APPS
;
966 ret
= setup_lttng_msg(cmd_ctx
, sizeof(pid_t
) * app_count
);
971 get_app_list_pids((pid_t
*)(cmd_ctx
->llm
->payload
));
976 case UST_START_TRACE
:
978 /* Setup lttng message with no payload */
979 ret
= setup_lttng_msg(cmd_ctx
, 0);
984 ret
= ust_start_trace(cmd_ctx
);
992 /* Setup lttng message with no payload */
993 ret
= setup_lttng_msg(cmd_ctx
, 0);
998 ret
= ust_stop_trace(cmd_ctx
);
1004 case LTTNG_LIST_SESSIONS
:
1006 unsigned int session_count
;
1008 session_count
= get_session_count();
1009 if (session_count
== 0) {
1010 ret
= LTTCOMM_NO_SESS
;
1014 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * session_count
);
1019 get_lttng_session((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
1025 /* Undefined command */
1026 ret
= setup_lttng_msg(cmd_ctx
, 0);
1035 /* Set return code */
1036 cmd_ctx
->llm
->ret_code
= ret
;
1041 if (cmd_ctx
->llm
== NULL
) {
1042 DBG("Missing llm structure. Allocating one.");
1043 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
1047 /* Notify client of error */
1048 cmd_ctx
->llm
->ret_code
= ret
;
1055 * thread_manage_clients
1057 * This thread manage all clients request using the unix
1058 * client socket for communication.
1060 static void *thread_manage_clients(void *data
)
1063 struct command_ctx
*cmd_ctx
;
1065 DBG("[thread] Manage client started");
1067 ret
= lttcomm_listen_unix_sock(client_sock
);
1072 /* Notify parent pid that we are ready
1073 * to accept command for client side.
1075 if (opt_sig_parent
) {
1076 kill(ppid
, SIGCHLD
);
1080 /* Blocking call, waiting for transmission */
1081 DBG("Accepting client command ...");
1082 sock
= lttcomm_accept_unix_sock(client_sock
);
1087 /* Allocate context command to process the client request */
1088 cmd_ctx
= malloc(sizeof(struct command_ctx
));
1090 /* Allocate data buffer for reception */
1091 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
1092 cmd_ctx
->llm
= NULL
;
1093 cmd_ctx
->session
= NULL
;
1096 * Data is received from the lttng client. The struct
1097 * lttcomm_session_msg (lsm) contains the command and data request of
1100 DBG("Receiving data from client ...");
1101 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
1107 * This function dispatch the work to the kernel or userspace tracer
1108 * libs and fill the lttcomm_lttng_msg data structure of all the needed
1109 * informations for the client. The command context struct contains
1110 * everything this function may needs.
1112 ret
= process_client_msg(cmd_ctx
);
1114 /* TODO: Inform client somehow of the fatal error. At this point,
1115 * ret < 0 means that a malloc failed (ENOMEM). */
1116 /* Error detected but still accept command */
1117 clean_command_ctx(cmd_ctx
);
1121 DBG("Sending response (size: %d, retcode: %d)",
1122 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
1123 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
1125 ERR("Failed to send data back to client");
1128 clean_command_ctx(cmd_ctx
);
1137 * usage function on stderr
1139 static void usage(void)
1141 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
1142 fprintf(stderr
, " -h, --help Display this usage.\n");
1143 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
1144 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
1145 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
1146 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
1147 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
1148 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
1149 fprintf(stderr
, " -V, --version Show version number.\n");
1150 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
1151 fprintf(stderr
, " -q, --quiet No output at all.\n");
1152 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
1156 * daemon argument parsing
1158 static int parse_args(int argc
, char **argv
)
1162 static struct option long_options
[] = {
1163 { "client-sock", 1, 0, 'c' },
1164 { "apps-sock", 1, 0, 'a' },
1165 { "kconsumerd-cmd-sock", 1, 0, 0 },
1166 { "kconsumerd-err-sock", 1, 0, 0 },
1167 { "daemonize", 0, 0, 'd' },
1168 { "sig-parent", 0, 0, 'S' },
1169 { "help", 0, 0, 'h' },
1170 { "group", 1, 0, 'g' },
1171 { "version", 0, 0, 'V' },
1172 { "quiet", 0, 0, 'q' },
1173 { "verbose", 0, 0, 'v' },
1178 int option_index
= 0;
1179 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
1186 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
1188 fprintf(stderr
, " with arg %s\n", optarg
);
1192 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1195 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1201 opt_tracing_group
= strdup(optarg
);
1207 fprintf(stdout
, "%s\n", VERSION
);
1213 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1216 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1225 /* Unknown option or other error.
1226 * Error is printed by getopt, just return */
1235 * init_daemon_socket
1237 * Creates the two needed socket by the daemon.
1238 * apps_sock - The communication socket for all UST apps.
1239 * client_sock - The communication of the cli tool (lttng).
1241 static int init_daemon_socket()
1246 old_umask
= umask(0);
1248 /* Create client tool unix socket */
1249 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
1250 if (client_sock
< 0) {
1251 ERR("Create unix sock failed: %s", client_unix_sock_path
);
1256 /* File permission MUST be 660 */
1257 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1259 ERR("Set file permissions failed: %s", client_unix_sock_path
);
1264 /* Create the application unix socket */
1265 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
1266 if (apps_sock
< 0) {
1267 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
1272 /* File permission MUST be 666 */
1273 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
1275 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
1286 * check_existing_daemon
1288 * Check if the global socket is available.
1289 * If yes, error is returned.
1291 static int check_existing_daemon()
1295 ret
= access(client_unix_sock_path
, F_OK
);
1297 ret
= access(apps_unix_sock_path
, F_OK
);
1306 * Return pointer to home directory path using
1307 * the env variable HOME.
1311 static const char *get_home_dir(void)
1313 const char *home_path
;
1315 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
1316 home_path
= default_home_dir
;
1325 * Set the tracing group gid onto the client socket.
1327 * Race window between mkdir and chown is OK because we are going from
1328 * less permissive (root.root) to more permissive (root.tracing).
1330 static int set_permissions(void)
1335 /* Decide which group name to use */
1336 (opt_tracing_group
!= NULL
) ?
1337 (grp
= getgrnam(opt_tracing_group
)) :
1338 (grp
= getgrnam(default_tracing_group
));
1341 ERR("Missing tracing group. Aborting execution.\n");
1346 /* Set lttng run dir */
1347 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
1349 ERR("Unable to set group on " LTTNG_RUNDIR
);
1353 /* lttng client socket path */
1354 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
1356 ERR("Unable to set group on %s", client_unix_sock_path
);
1360 /* kconsumerd error socket path */
1361 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
1363 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
1367 DBG("All permissions are set");
1374 * create_lttng_rundir
1376 * Create the lttng run directory needed for all
1377 * global sockets and pipe.
1379 static int create_lttng_rundir(void)
1383 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
1385 ERR("Unable to create " LTTNG_RUNDIR
);
1394 * init_kernel_tracer
1396 * Setup necessary data for kernel tracer action.
1398 static void init_kernel_tracer(void)
1400 /* Set the global kernel tracer fd */
1401 kernel_tracer_fd
= open(DEFAULT_KERNEL_TRACER_PATH
, O_RDWR
);
1402 if (kernel_tracer_fd
< 0) {
1403 WARN("No kernel tracer available");
1404 kernel_tracer_fd
= 0;
1407 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1411 * set_kconsumerd_sockets
1413 * Setup sockets and directory needed by the kconsumerd
1414 * communication with the session daemon.
1416 static int set_kconsumerd_sockets(void)
1420 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
1421 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
1424 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
1425 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
1428 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
1430 ERR("Failed to create " KCONSUMERD_PATH
);
1434 /* Create the kconsumerd error unix socket */
1435 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
1436 if (kconsumerd_err_sock
< 0) {
1437 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
1442 /* File permission MUST be 660 */
1443 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1445 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
1457 * Signal handler for the daemon
1459 static void sighandler(int sig
)
1463 DBG("SIGPIPE catched");
1466 DBG("SIGINT catched");
1470 DBG("SIGTERM catched");
1481 * set_signal_handler
1483 * Setup signal handler for :
1484 * SIGINT, SIGTERM, SIGPIPE
1486 static int set_signal_handler(void)
1489 struct sigaction sa
;
1492 if ((ret
= sigemptyset(&sigset
)) < 0) {
1493 perror("sigemptyset");
1497 sa
.sa_handler
= sighandler
;
1498 sa
.sa_mask
= sigset
;
1500 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1501 perror("sigaction");
1505 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1506 perror("sigaction");
1510 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1511 perror("sigaction");
1515 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
1523 int main(int argc
, char **argv
)
1528 /* Parse arguments */
1530 if ((ret
= parse_args(argc
, argv
) < 0)) {
1543 /* Check if daemon is UID = 0 */
1544 is_root
= !getuid();
1546 /* Set all sockets path */
1548 ret
= create_lttng_rundir();
1553 if (strlen(apps_unix_sock_path
) == 0) {
1554 snprintf(apps_unix_sock_path
, PATH_MAX
,
1555 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
1558 if (strlen(client_unix_sock_path
) == 0) {
1559 snprintf(client_unix_sock_path
, PATH_MAX
,
1560 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
1563 ret
= set_kconsumerd_sockets();
1568 /* Setup kernel tracer */
1569 init_kernel_tracer();
1571 if (strlen(apps_unix_sock_path
) == 0) {
1572 snprintf(apps_unix_sock_path
, PATH_MAX
,
1573 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir());
1576 /* Set the cli tool unix socket path */
1577 if (strlen(client_unix_sock_path
) == 0) {
1578 snprintf(client_unix_sock_path
, PATH_MAX
,
1579 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir());
1583 DBG("Client socket path %s", client_unix_sock_path
);
1584 DBG("Application socket path %s", apps_unix_sock_path
);
1586 /* See if daemon already exist. If any of the two
1587 * socket needed by the daemon are present, this test fails
1589 if ((ret
= check_existing_daemon()) == 0) {
1590 ERR("Already running daemon.\n");
1591 /* We do not goto error because we must not
1592 * cleanup() because a daemon is already running.
1597 if (set_signal_handler() < 0) {
1601 /* Setup the needed unix socket */
1602 if (init_daemon_socket() < 0) {
1606 /* Set credentials to socket */
1607 if (is_root
&& (set_permissions() < 0)) {
1611 /* Get parent pid if -S, --sig-parent is specified. */
1612 if (opt_sig_parent
) {
1617 /* Create thread to manage the client socket */
1618 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
1620 perror("pthread_create");
1624 /* Create thread to manage application socket */
1625 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
1627 perror("pthread_create");
1631 ret
= pthread_join(client_thread
, &status
);
1633 perror("pthread_join");