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 * spawn_kconsumerd_thread
482 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
483 * exec or it will fails.
485 static int spawn_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 /* Wait for the kconsumerd thread to be ready */
499 sem_wait(&kconsumerd_sem
);
510 * Fork and exec a kernel consumer daemon (kconsumerd).
512 * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel
513 * file descriptor using the libkernelctl or kernel-ctl functions. So, a
514 * kernel consumer MUST only be spawned before creating a kernel session.
516 * Return pid if successful else -1.
518 static pid_t
spawn_kconsumerd(void)
528 execlp("kconsumerd", "kconsumerd", "--daemonize", NULL
);
530 perror("kernel start consumer exec");
533 } else if (pid
> 0) {
537 perror("kernel start consumer fork");
549 * Spawn the kconsumerd daemon and session daemon thread.
551 static int start_kconsumerd(void)
555 DBG("Spawning kconsumerd");
557 pthread_mutex_lock(&kconsumerd_pid_mutex
);
558 if (kconsumerd_pid
== 0) {
559 ret
= spawn_kconsumerd();
561 ERR("Spawning kconsumerd failed");
562 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
563 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
567 /* Setting up the global kconsumerd_pid */
568 kconsumerd_pid
= ret
;
570 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
572 DBG("Spawning kconsumerd thread");
574 ret
= spawn_kconsumerd_thread();
576 ERR("Fatal error spawning kconsumerd thread");
588 * send_kconsumerd_fds
590 * Send all stream fds of the kernel session to the consumer.
592 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
595 /* Plus one here for the metadata fd */
596 size_t nb_fd
= session
->stream_count_global
+ 1;
598 struct ltt_kernel_stream
*stream
;
599 struct ltt_kernel_channel
*chan
;
600 struct lttcomm_kconsumerd_header lkh
;
601 struct lttcomm_kconsumerd_msg buf
[nb_fd
];
603 /* Add metadata data */
604 fds
[i
] = session
->metadata_stream_fd
;
606 buf
[i
].state
= ACTIVE_FD
;
607 buf
[i
].max_sb_size
= session
->metadata
->conf
->subbuf_size
;
608 strncpy(buf
[i
].path_name
, session
->metadata
->pathname
, PATH_MAX
);
610 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
611 cds_list_for_each_entry(stream
, &chan
->stream_list
.head
, list
) {
614 buf
[i
].fd
= stream
->fd
;
615 buf
[i
].state
= stream
->state
;
616 buf
[i
].max_sb_size
= chan
->channel
->subbuf_size
;
617 strncpy(buf
[i
].path_name
, stream
->pathname
, PATH_MAX
);
622 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
623 lkh
.cmd_type
= LTTCOMM_ADD_STREAM
;
625 DBG("Sending kconsumerd header");
627 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
629 perror("send kconsumerd header");
633 DBG("Sending all fds to kconsumerd");
635 ret
= lttcomm_send_fds_unix_sock(sock
, buf
, fds
, nb_fd
, lkh
.payload_size
);
637 perror("send kconsumerd fds");
641 DBG("Kconsumerd fds sent");
652 * Create the trace output directory.
654 static int create_trace_dir(struct ltt_kernel_session
*session
)
657 struct ltt_kernel_channel
*chan
;
659 /* Create all channel directories */
660 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
661 ret
= mkdir(chan
->pathname
, S_IRWXU
| S_IRWXG
);
663 perror("mkdir trace path");
679 * Process the command requested by the lttng client within the command
680 * context structure. This function make sure that the return structure (llm)
681 * is set and ready for transmission before returning.
683 * Return any error encountered or 0 for success.
685 static int process_client_msg(struct command_ctx
*cmd_ctx
)
689 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
691 /* Check command that needs a session */
692 switch (cmd_ctx
->lsm
->cmd_type
) {
693 case LTTNG_CREATE_SESSION
:
694 case LTTNG_LIST_SESSIONS
:
698 cmd_ctx
->session
= find_session_by_uuid(cmd_ctx
->lsm
->session_uuid
);
699 if (cmd_ctx
->session
== NULL
) {
700 ret
= LTTCOMM_SELECT_SESS
;
706 /* Check command for kernel tracing */
707 switch (cmd_ctx
->lsm
->cmd_type
) {
708 case KERNEL_CREATE_SESSION
:
709 case KERNEL_CREATE_CHANNEL
:
710 case KERNEL_CREATE_STREAM
:
711 case KERNEL_DISABLE_EVENT
:
712 case KERNEL_ENABLE_EVENT
:
713 case KERNEL_OPEN_METADATA
:
714 case KERNEL_START_TRACE
:
715 case KERNEL_STOP_TRACE
:
716 /* TODO: reconnect to kernel tracer to check if
718 if (kernel_tracer_fd
== 0) {
719 ret
= LTTCOMM_KERN_NA
;
725 /* Connect to ust apps if available pid */
726 if (cmd_ctx
->lsm
->pid
> 0) {
727 /* Connect to app using ustctl API */
728 cmd_ctx
->ust_sock
= ust_connect_app(cmd_ctx
->lsm
->pid
);
729 if (cmd_ctx
->ust_sock
< 0) {
730 ret
= LTTCOMM_NO_TRACEABLE
;
735 /* Process by command type */
736 switch (cmd_ctx
->lsm
->cmd_type
) {
737 case KERNEL_CREATE_SESSION
:
739 ret
= setup_lttng_msg(cmd_ctx
, 0);
744 ret
= start_kconsumerd();
749 DBG("Creating kernel session");
751 ret
= kernel_create_session(cmd_ctx
->session
, kernel_tracer_fd
);
753 ret
= LTTCOMM_KERN_SESS_FAIL
;
760 case KERNEL_CREATE_CHANNEL
:
762 ret
= setup_lttng_msg(cmd_ctx
, 0);
767 DBG("Creating kernel channel");
769 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
);
772 ret
= LTTCOMM_KERN_CHAN_FAIL
;
779 case KERNEL_ENABLE_EVENT
:
781 /* Setup lttng message with no payload */
782 ret
= setup_lttng_msg(cmd_ctx
, 0);
787 DBG("Enabling kernel event %s", cmd_ctx
->lsm
->u
.event
.event_name
);
789 ret
= kernel_enable_event(cmd_ctx
->session
->kernel_session
, cmd_ctx
->lsm
->u
.event
.event_name
);
791 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
798 case KERNEL_OPEN_METADATA
:
800 /* Setup lttng message with no payload */
801 ret
= setup_lttng_msg(cmd_ctx
, 0);
806 DBG("Open kernel metadata");
808 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
);
810 ret
= LTTCOMM_KERN_META_FAIL
;
817 case KERNEL_CREATE_STREAM
:
819 struct ltt_kernel_channel
*chan
;
820 /* Setup lttng message with no payload */
821 ret
= setup_lttng_msg(cmd_ctx
, 0);
826 DBG("Creating kernel stream");
828 ret
= kernel_create_metadata_stream(cmd_ctx
->session
->kernel_session
);
830 ERR("Kernel create metadata stream failed");
831 ret
= LTTCOMM_KERN_STREAM_FAIL
;
835 /* For each channel */
836 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
837 ret
= kernel_create_channel_stream(chan
);
839 ERR("Kernel create channel stream failed");
840 ret
= LTTCOMM_KERN_STREAM_FAIL
;
843 /* Update the stream global counter */
844 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
850 case KERNEL_START_TRACE
:
852 /* Setup lttng message with no payload */
853 ret
= setup_lttng_msg(cmd_ctx
, 0);
858 DBG("Start kernel tracing");
860 ret
= create_trace_dir(cmd_ctx
->session
->kernel_session
);
862 if (ret
== -EEXIST
) {
863 ret
= LTTCOMM_KERN_DIR_EXIST
;
865 ret
= LTTCOMM_KERN_DIR_FAIL
;
870 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
872 ERR("Kernel start session failed");
873 ret
= LTTCOMM_KERN_START_FAIL
;
877 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, cmd_ctx
->session
->kernel_session
);
879 ERR("Send kconsumerd fds failed");
880 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
887 case KERNEL_STOP_TRACE
:
889 /* Setup lttng message with no payload */
890 ret
= setup_lttng_msg(cmd_ctx
, 0);
895 DBG("Stop kernel tracing");
897 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
899 ERR("Kernel stop session failed");
900 ret
= LTTCOMM_KERN_STOP_FAIL
;
904 /* Clean kernel session teardown */
905 teardown_kernel_session(cmd_ctx
->session
);
910 case LTTNG_CREATE_SESSION
:
912 /* Setup lttng message with no payload */
913 ret
= setup_lttng_msg(cmd_ctx
, 0);
918 ret
= create_session(cmd_ctx
->lsm
->session_name
, &cmd_ctx
->llm
->session_uuid
);
921 ret
= LTTCOMM_EXIST_SESS
;
931 case LTTNG_DESTROY_SESSION
:
933 /* Setup lttng message with no payload */
934 ret
= setup_lttng_msg(cmd_ctx
, 0);
939 ret
= destroy_session(&cmd_ctx
->lsm
->session_uuid
);
941 ret
= LTTCOMM_NO_SESS
;
948 case LTTNG_LIST_TRACES
:
950 unsigned int trace_count
;
952 trace_count
= get_trace_count_per_session(cmd_ctx
->session
);
953 if (trace_count
== 0) {
954 ret
= LTTCOMM_NO_TRACE
;
958 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_trace
) * trace_count
);
963 get_traces_per_session(cmd_ctx
->session
,
964 (struct lttng_trace
*)(cmd_ctx
->llm
->payload
));
969 case UST_CREATE_TRACE
:
971 /* Setup lttng message with no payload */
972 ret
= setup_lttng_msg(cmd_ctx
, 0);
977 ret
= ust_create_trace(cmd_ctx
);
985 unsigned int app_count
;
987 app_count
= get_app_count();
988 DBG("Traceable application count : %d", app_count
);
989 if (app_count
== 0) {
990 ret
= LTTCOMM_NO_APPS
;
994 ret
= setup_lttng_msg(cmd_ctx
, sizeof(pid_t
) * app_count
);
999 get_app_list_pids((pid_t
*)(cmd_ctx
->llm
->payload
));
1004 case UST_START_TRACE
:
1006 /* Setup lttng message with no payload */
1007 ret
= setup_lttng_msg(cmd_ctx
, 0);
1012 ret
= ust_start_trace(cmd_ctx
);
1018 case UST_STOP_TRACE
:
1020 /* Setup lttng message with no payload */
1021 ret
= setup_lttng_msg(cmd_ctx
, 0);
1026 ret
= ust_stop_trace(cmd_ctx
);
1032 case LTTNG_LIST_SESSIONS
:
1034 unsigned int session_count
;
1036 session_count
= get_session_count();
1037 if (session_count
== 0) {
1038 ret
= LTTCOMM_NO_SESS
;
1042 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * session_count
);
1047 get_lttng_session((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
1053 /* Undefined command */
1054 ret
= setup_lttng_msg(cmd_ctx
, 0);
1063 /* Set return code */
1064 cmd_ctx
->llm
->ret_code
= ret
;
1069 if (cmd_ctx
->llm
== NULL
) {
1070 DBG("Missing llm structure. Allocating one.");
1071 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
1075 /* Notify client of error */
1076 cmd_ctx
->llm
->ret_code
= ret
;
1083 * thread_manage_clients
1085 * This thread manage all clients request using the unix
1086 * client socket for communication.
1088 static void *thread_manage_clients(void *data
)
1091 struct command_ctx
*cmd_ctx
;
1093 DBG("[thread] Manage client started");
1095 ret
= lttcomm_listen_unix_sock(client_sock
);
1100 /* Notify parent pid that we are ready
1101 * to accept command for client side.
1103 if (opt_sig_parent
) {
1104 kill(ppid
, SIGCHLD
);
1108 /* Blocking call, waiting for transmission */
1109 DBG("Accepting client command ...");
1110 sock
= lttcomm_accept_unix_sock(client_sock
);
1115 /* Allocate context command to process the client request */
1116 cmd_ctx
= malloc(sizeof(struct command_ctx
));
1118 /* Allocate data buffer for reception */
1119 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
1120 cmd_ctx
->llm
= NULL
;
1121 cmd_ctx
->session
= NULL
;
1124 * Data is received from the lttng client. The struct
1125 * lttcomm_session_msg (lsm) contains the command and data request of
1128 DBG("Receiving data from client ...");
1129 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
1135 * This function dispatch the work to the kernel or userspace tracer
1136 * libs and fill the lttcomm_lttng_msg data structure of all the needed
1137 * informations for the client. The command context struct contains
1138 * everything this function may needs.
1140 ret
= process_client_msg(cmd_ctx
);
1142 /* TODO: Inform client somehow of the fatal error. At this point,
1143 * ret < 0 means that a malloc failed (ENOMEM). */
1144 /* Error detected but still accept command */
1145 clean_command_ctx(cmd_ctx
);
1149 DBG("Sending response (size: %d, retcode: %d)",
1150 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
1151 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
1153 ERR("Failed to send data back to client");
1156 clean_command_ctx(cmd_ctx
);
1165 * usage function on stderr
1167 static void usage(void)
1169 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
1170 fprintf(stderr
, " -h, --help Display this usage.\n");
1171 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
1172 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
1173 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
1174 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
1175 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
1176 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
1177 fprintf(stderr
, " -V, --version Show version number.\n");
1178 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
1179 fprintf(stderr
, " -q, --quiet No output at all.\n");
1180 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
1184 * daemon argument parsing
1186 static int parse_args(int argc
, char **argv
)
1190 static struct option long_options
[] = {
1191 { "client-sock", 1, 0, 'c' },
1192 { "apps-sock", 1, 0, 'a' },
1193 { "kconsumerd-cmd-sock", 1, 0, 0 },
1194 { "kconsumerd-err-sock", 1, 0, 0 },
1195 { "daemonize", 0, 0, 'd' },
1196 { "sig-parent", 0, 0, 'S' },
1197 { "help", 0, 0, 'h' },
1198 { "group", 1, 0, 'g' },
1199 { "version", 0, 0, 'V' },
1200 { "quiet", 0, 0, 'q' },
1201 { "verbose", 0, 0, 'v' },
1206 int option_index
= 0;
1207 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
1214 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
1216 fprintf(stderr
, " with arg %s\n", optarg
);
1220 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1223 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1229 opt_tracing_group
= strdup(optarg
);
1235 fprintf(stdout
, "%s\n", VERSION
);
1241 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1244 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1253 /* Unknown option or other error.
1254 * Error is printed by getopt, just return */
1263 * init_daemon_socket
1265 * Creates the two needed socket by the daemon.
1266 * apps_sock - The communication socket for all UST apps.
1267 * client_sock - The communication of the cli tool (lttng).
1269 static int init_daemon_socket()
1274 old_umask
= umask(0);
1276 /* Create client tool unix socket */
1277 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
1278 if (client_sock
< 0) {
1279 ERR("Create unix sock failed: %s", client_unix_sock_path
);
1284 /* File permission MUST be 660 */
1285 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1287 ERR("Set file permissions failed: %s", client_unix_sock_path
);
1292 /* Create the application unix socket */
1293 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
1294 if (apps_sock
< 0) {
1295 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
1300 /* File permission MUST be 666 */
1301 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
1303 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
1314 * check_existing_daemon
1316 * Check if the global socket is available.
1317 * If yes, error is returned.
1319 static int check_existing_daemon()
1323 ret
= access(client_unix_sock_path
, F_OK
);
1325 ret
= access(apps_unix_sock_path
, F_OK
);
1334 * Return pointer to home directory path using
1335 * the env variable HOME.
1339 static const char *get_home_dir(void)
1341 const char *home_path
;
1343 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
1344 home_path
= default_home_dir
;
1353 * Set the tracing group gid onto the client socket.
1355 * Race window between mkdir and chown is OK because we are going from
1356 * less permissive (root.root) to more permissive (root.tracing).
1358 static int set_permissions(void)
1363 /* Decide which group name to use */
1364 (opt_tracing_group
!= NULL
) ?
1365 (grp
= getgrnam(opt_tracing_group
)) :
1366 (grp
= getgrnam(default_tracing_group
));
1369 ERR("Missing tracing group. Aborting execution.\n");
1374 /* Set lttng run dir */
1375 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
1377 ERR("Unable to set group on " LTTNG_RUNDIR
);
1381 /* lttng client socket path */
1382 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
1384 ERR("Unable to set group on %s", client_unix_sock_path
);
1388 /* kconsumerd error socket path */
1389 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
1391 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
1395 DBG("All permissions are set");
1402 * create_lttng_rundir
1404 * Create the lttng run directory needed for all
1405 * global sockets and pipe.
1407 static int create_lttng_rundir(void)
1411 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
1413 ERR("Unable to create " LTTNG_RUNDIR
);
1422 * init_kernel_tracer
1424 * Setup necessary data for kernel tracer action.
1426 static void init_kernel_tracer(void)
1428 /* Set the global kernel tracer fd */
1429 kernel_tracer_fd
= open(DEFAULT_KERNEL_TRACER_PATH
, O_RDWR
);
1430 if (kernel_tracer_fd
< 0) {
1431 WARN("No kernel tracer available");
1432 kernel_tracer_fd
= 0;
1435 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1439 * set_kconsumerd_sockets
1441 * Setup sockets and directory needed by the kconsumerd
1442 * communication with the session daemon.
1444 static int set_kconsumerd_sockets(void)
1448 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
1449 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
1452 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
1453 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
1456 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
1458 ERR("Failed to create " KCONSUMERD_PATH
);
1462 /* Create the kconsumerd error unix socket */
1463 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
1464 if (kconsumerd_err_sock
< 0) {
1465 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
1470 /* File permission MUST be 660 */
1471 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1473 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
1485 * Signal handler for the daemon
1487 static void sighandler(int sig
)
1491 DBG("SIGPIPE catched");
1494 DBG("SIGINT catched");
1498 DBG("SIGTERM catched");
1509 * set_signal_handler
1511 * Setup signal handler for :
1512 * SIGINT, SIGTERM, SIGPIPE
1514 static int set_signal_handler(void)
1517 struct sigaction sa
;
1520 if ((ret
= sigemptyset(&sigset
)) < 0) {
1521 perror("sigemptyset");
1525 sa
.sa_handler
= sighandler
;
1526 sa
.sa_mask
= sigset
;
1528 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1529 perror("sigaction");
1533 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1534 perror("sigaction");
1538 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1539 perror("sigaction");
1543 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
1551 int main(int argc
, char **argv
)
1556 /* Parse arguments */
1558 if ((ret
= parse_args(argc
, argv
) < 0)) {
1571 /* Check if daemon is UID = 0 */
1572 is_root
= !getuid();
1574 /* Set all sockets path */
1576 ret
= create_lttng_rundir();
1581 if (strlen(apps_unix_sock_path
) == 0) {
1582 snprintf(apps_unix_sock_path
, PATH_MAX
,
1583 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
1586 if (strlen(client_unix_sock_path
) == 0) {
1587 snprintf(client_unix_sock_path
, PATH_MAX
,
1588 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
1591 ret
= set_kconsumerd_sockets();
1596 /* Setup kernel tracer */
1597 init_kernel_tracer();
1599 if (strlen(apps_unix_sock_path
) == 0) {
1600 snprintf(apps_unix_sock_path
, PATH_MAX
,
1601 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir());
1604 /* Set the cli tool unix socket path */
1605 if (strlen(client_unix_sock_path
) == 0) {
1606 snprintf(client_unix_sock_path
, PATH_MAX
,
1607 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir());
1611 DBG("Client socket path %s", client_unix_sock_path
);
1612 DBG("Application socket path %s", apps_unix_sock_path
);
1614 /* See if daemon already exist. If any of the two
1615 * socket needed by the daemon are present, this test fails
1617 if ((ret
= check_existing_daemon()) == 0) {
1618 ERR("Already running daemon.\n");
1619 /* We do not goto error because we must not
1620 * cleanup() because a daemon is already running.
1625 if (set_signal_handler() < 0) {
1629 /* Setup the needed unix socket */
1630 if (init_daemon_socket() < 0) {
1634 /* Set credentials to socket */
1635 if (is_root
&& (set_permissions() < 0)) {
1639 /* Get parent pid if -S, --sig-parent is specified. */
1640 if (opt_sig_parent
) {
1645 /* Create thread to manage the client socket */
1646 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
1648 perror("pthread_create");
1652 /* Create thread to manage application socket */
1653 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
1655 perror("pthread_create");
1659 ret
= pthread_join(client_thread
, &status
);
1661 perror("pthread_join");