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("Kconsumerd error when waiting for SOCK_READY : %s",
394 lttcomm_get_readable_code(-code
));
399 /* Wait for any kconsumerd error */
400 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
402 ERR("Kconsumerd closed the command socket");
406 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
407 if (code
!= KCONSUMERD_POLL_HUP
) {
414 DBG("Kconsumerd thread dying");
421 * This thread manage the application socket communication
423 static void *thread_manage_apps(void *data
)
427 /* TODO: Something more elegant is needed but fine for now */
428 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
429 * for 32-bit vs 64-bit compat processes. */
430 /* replicate in ust with version number */
432 int reg
; /* 1:register, 0:unregister */
437 DBG("[thread] Manage apps started");
439 ret
= lttcomm_listen_unix_sock(apps_sock
);
444 /* Notify all applications to register */
445 notify_apps(default_global_apps_pipe
);
448 DBG("Accepting application registration");
449 /* Blocking call, waiting for transmission */
450 sock
= lttcomm_accept_unix_sock(apps_sock
);
455 /* Basic recv here to handle the very simple data
456 * that the libust send to register (reg_msg).
458 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
464 /* Add application to the global traceable list */
465 if (reg_msg
.reg
== 1) {
467 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
469 /* register_traceable_app only return an error with
470 * ENOMEM. At this point, we better stop everything.
476 unregister_traceable_app(reg_msg
.pid
);
486 * spawn_kconsumerd_thread
488 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
489 * exec or it will fails.
491 static int spawn_kconsumerd_thread(void)
495 /* Setup semaphore */
496 sem_init(&kconsumerd_sem
, 0, 0);
498 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
500 perror("pthread_create kconsumerd");
504 /* Wait for the kconsumerd thread to be ready */
505 sem_wait(&kconsumerd_sem
);
516 * Fork and exec a kernel consumer daemon (kconsumerd).
518 * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel
519 * file descriptor using the libkernelctl or kernel-ctl functions. So, a
520 * kernel consumer MUST only be spawned before creating a kernel session.
522 * Return pid if successful else -1.
524 static pid_t
spawn_kconsumerd(void)
534 execlp("kconsumerd", "kconsumerd", "--daemonize", NULL
);
536 perror("kernel start consumer exec");
539 } else if (pid
> 0) {
543 perror("kernel start consumer fork");
555 * Spawn the kconsumerd daemon and session daemon thread.
557 static int start_kconsumerd(void)
561 DBG("Spawning kconsumerd");
563 pthread_mutex_lock(&kconsumerd_pid_mutex
);
564 if (kconsumerd_pid
== 0) {
565 ret
= spawn_kconsumerd();
567 ERR("Spawning kconsumerd failed");
568 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
569 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
573 /* Setting up the global kconsumerd_pid */
574 kconsumerd_pid
= ret
;
576 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
578 DBG("Kconsumerd pid %d", ret
);
580 DBG("Spawning kconsumerd thread");
582 ret
= spawn_kconsumerd_thread();
584 ERR("Fatal error spawning kconsumerd thread");
596 * send_kconsumerd_fds
598 * Send all stream fds of the kernel session to the consumer.
600 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
603 /* Plus one here for the metadata fd */
604 size_t nb_fd
= session
->stream_count_global
+ 1;
606 struct ltt_kernel_stream
*stream
;
607 struct ltt_kernel_channel
*chan
;
608 struct lttcomm_kconsumerd_header lkh
;
609 struct lttcomm_kconsumerd_msg buf
[nb_fd
];
611 /* Add metadata data */
612 fds
[i
] = session
->metadata_stream_fd
;
614 buf
[i
].state
= ACTIVE_FD
;
615 buf
[i
].max_sb_size
= session
->metadata
->conf
->subbuf_size
;
616 strncpy(buf
[i
].path_name
, session
->metadata
->pathname
, PATH_MAX
);
618 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
619 cds_list_for_each_entry(stream
, &chan
->stream_list
.head
, list
) {
622 buf
[i
].fd
= stream
->fd
;
623 buf
[i
].state
= stream
->state
;
624 buf
[i
].max_sb_size
= chan
->channel
->subbuf_size
;
625 strncpy(buf
[i
].path_name
, stream
->pathname
, PATH_MAX
);
630 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
631 lkh
.cmd_type
= LTTCOMM_ADD_STREAM
;
633 DBG("Sending kconsumerd header");
635 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
637 perror("send kconsumerd header");
641 DBG("Sending all fds to kconsumerd");
643 ret
= lttcomm_send_fds_unix_sock(sock
, buf
, fds
, nb_fd
, lkh
.payload_size
);
645 perror("send kconsumerd fds");
649 DBG("Kconsumerd fds sent");
660 * Create the trace output directory.
662 static int create_trace_dir(struct ltt_kernel_session
*session
)
665 struct ltt_kernel_channel
*chan
;
667 /* Create all channel directories */
668 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
669 ret
= mkdir(chan
->pathname
, S_IRWXU
| S_IRWXG
);
671 perror("mkdir trace path");
687 * Process the command requested by the lttng client within the command
688 * context structure. This function make sure that the return structure (llm)
689 * is set and ready for transmission before returning.
691 * Return any error encountered or 0 for success.
693 static int process_client_msg(struct command_ctx
*cmd_ctx
)
697 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
699 /* Check command that needs a session */
700 switch (cmd_ctx
->lsm
->cmd_type
) {
701 case LTTNG_CREATE_SESSION
:
702 case LTTNG_LIST_SESSIONS
:
706 cmd_ctx
->session
= find_session_by_uuid(cmd_ctx
->lsm
->session_uuid
);
707 if (cmd_ctx
->session
== NULL
) {
708 ret
= LTTCOMM_SELECT_SESS
;
714 /* Check command for kernel tracing */
715 switch (cmd_ctx
->lsm
->cmd_type
) {
716 case KERNEL_CREATE_SESSION
:
717 case KERNEL_CREATE_CHANNEL
:
718 case KERNEL_CREATE_STREAM
:
719 case KERNEL_DISABLE_EVENT
:
720 case KERNEL_ENABLE_EVENT
:
721 case KERNEL_OPEN_METADATA
:
722 case KERNEL_START_TRACE
:
723 case KERNEL_STOP_TRACE
:
724 /* TODO: reconnect to kernel tracer to check if
726 if (kernel_tracer_fd
== 0) {
727 ret
= LTTCOMM_KERN_NA
;
733 /* Connect to ust apps if available pid */
734 if (cmd_ctx
->lsm
->pid
> 0) {
735 /* Connect to app using ustctl API */
736 cmd_ctx
->ust_sock
= ust_connect_app(cmd_ctx
->lsm
->pid
);
737 if (cmd_ctx
->ust_sock
< 0) {
738 ret
= LTTCOMM_NO_TRACEABLE
;
743 /* Process by command type */
744 switch (cmd_ctx
->lsm
->cmd_type
) {
745 case KERNEL_CREATE_SESSION
:
747 ret
= setup_lttng_msg(cmd_ctx
, 0);
752 ret
= start_kconsumerd();
757 DBG("Creating kernel session");
759 ret
= kernel_create_session(cmd_ctx
->session
, kernel_tracer_fd
);
761 ret
= LTTCOMM_KERN_SESS_FAIL
;
768 case KERNEL_CREATE_CHANNEL
:
770 ret
= setup_lttng_msg(cmd_ctx
, 0);
775 DBG("Creating kernel channel");
777 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
);
780 ret
= LTTCOMM_KERN_CHAN_FAIL
;
787 case KERNEL_ENABLE_EVENT
:
789 /* Setup lttng message with no payload */
790 ret
= setup_lttng_msg(cmd_ctx
, 0);
795 DBG("Enabling kernel event %s", cmd_ctx
->lsm
->u
.event
.event_name
);
797 ret
= kernel_enable_event(cmd_ctx
->session
->kernel_session
, cmd_ctx
->lsm
->u
.event
.event_name
);
799 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
806 case KERNEL_OPEN_METADATA
:
808 /* Setup lttng message with no payload */
809 ret
= setup_lttng_msg(cmd_ctx
, 0);
814 DBG("Open kernel metadata");
816 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
);
818 ret
= LTTCOMM_KERN_META_FAIL
;
825 case KERNEL_CREATE_STREAM
:
827 struct ltt_kernel_channel
*chan
;
828 /* Setup lttng message with no payload */
829 ret
= setup_lttng_msg(cmd_ctx
, 0);
834 DBG("Creating kernel stream");
836 ret
= kernel_create_metadata_stream(cmd_ctx
->session
->kernel_session
);
838 ERR("Kernel create metadata stream failed");
839 ret
= LTTCOMM_KERN_STREAM_FAIL
;
843 /* For each channel */
844 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
845 ret
= kernel_create_channel_stream(chan
);
847 ERR("Kernel create channel stream failed");
848 ret
= LTTCOMM_KERN_STREAM_FAIL
;
851 /* Update the stream global counter */
852 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
858 case KERNEL_START_TRACE
:
860 /* Setup lttng message with no payload */
861 ret
= setup_lttng_msg(cmd_ctx
, 0);
866 DBG("Start kernel tracing");
868 ret
= create_trace_dir(cmd_ctx
->session
->kernel_session
);
870 if (ret
== -EEXIST
) {
871 ret
= LTTCOMM_KERN_DIR_EXIST
;
873 ret
= LTTCOMM_KERN_DIR_FAIL
;
878 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
880 ERR("Kernel start session failed");
881 ret
= LTTCOMM_KERN_START_FAIL
;
885 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, cmd_ctx
->session
->kernel_session
);
887 ERR("Send kconsumerd fds failed");
888 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
895 case KERNEL_STOP_TRACE
:
897 /* Setup lttng message with no payload */
898 ret
= setup_lttng_msg(cmd_ctx
, 0);
903 if (cmd_ctx
->session
->kernel_session
== NULL
) {
904 ret
= LTTCOMM_KERN_NO_SESSION
;
908 DBG("Stop kernel tracing");
910 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
912 ERR("Kernel stop session failed");
913 ret
= LTTCOMM_KERN_STOP_FAIL
;
917 /* Clean kernel session teardown */
918 teardown_kernel_session(cmd_ctx
->session
);
923 case LTTNG_CREATE_SESSION
:
925 /* Setup lttng message with no payload */
926 ret
= setup_lttng_msg(cmd_ctx
, 0);
931 ret
= create_session(cmd_ctx
->lsm
->session_name
, &cmd_ctx
->llm
->session_uuid
);
934 ret
= LTTCOMM_EXIST_SESS
;
944 case LTTNG_DESTROY_SESSION
:
946 /* Setup lttng message with no payload */
947 ret
= setup_lttng_msg(cmd_ctx
, 0);
952 ret
= destroy_session(&cmd_ctx
->lsm
->session_uuid
);
954 ret
= LTTCOMM_NO_SESS
;
961 case LTTNG_LIST_TRACES
:
963 unsigned int trace_count
;
965 trace_count
= get_trace_count_per_session(cmd_ctx
->session
);
966 if (trace_count
== 0) {
967 ret
= LTTCOMM_NO_TRACE
;
971 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_trace
) * trace_count
);
976 get_traces_per_session(cmd_ctx
->session
,
977 (struct lttng_trace
*)(cmd_ctx
->llm
->payload
));
982 case UST_CREATE_TRACE
:
984 /* Setup lttng message with no payload */
985 ret
= setup_lttng_msg(cmd_ctx
, 0);
990 ret
= ust_create_trace(cmd_ctx
);
998 unsigned int app_count
;
1000 app_count
= get_app_count();
1001 DBG("Traceable application count : %d", app_count
);
1002 if (app_count
== 0) {
1003 ret
= LTTCOMM_NO_APPS
;
1007 ret
= setup_lttng_msg(cmd_ctx
, sizeof(pid_t
) * app_count
);
1012 get_app_list_pids((pid_t
*)(cmd_ctx
->llm
->payload
));
1017 case UST_START_TRACE
:
1019 /* Setup lttng message with no payload */
1020 ret
= setup_lttng_msg(cmd_ctx
, 0);
1025 ret
= ust_start_trace(cmd_ctx
);
1031 case UST_STOP_TRACE
:
1033 /* Setup lttng message with no payload */
1034 ret
= setup_lttng_msg(cmd_ctx
, 0);
1039 ret
= ust_stop_trace(cmd_ctx
);
1045 case LTTNG_LIST_SESSIONS
:
1047 unsigned int session_count
;
1049 session_count
= get_session_count();
1050 if (session_count
== 0) {
1051 ret
= LTTCOMM_NO_SESS
;
1055 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * session_count
);
1060 get_lttng_session((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
1066 /* Undefined command */
1067 ret
= setup_lttng_msg(cmd_ctx
, 0);
1076 /* Set return code */
1077 cmd_ctx
->llm
->ret_code
= ret
;
1082 if (cmd_ctx
->llm
== NULL
) {
1083 DBG("Missing llm structure. Allocating one.");
1084 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
1088 /* Notify client of error */
1089 cmd_ctx
->llm
->ret_code
= ret
;
1096 * thread_manage_clients
1098 * This thread manage all clients request using the unix
1099 * client socket for communication.
1101 static void *thread_manage_clients(void *data
)
1104 struct command_ctx
*cmd_ctx
;
1106 DBG("[thread] Manage client started");
1108 ret
= lttcomm_listen_unix_sock(client_sock
);
1113 /* Notify parent pid that we are ready
1114 * to accept command for client side.
1116 if (opt_sig_parent
) {
1117 kill(ppid
, SIGCHLD
);
1121 /* Blocking call, waiting for transmission */
1122 DBG("Accepting client command ...");
1123 sock
= lttcomm_accept_unix_sock(client_sock
);
1128 /* Allocate context command to process the client request */
1129 cmd_ctx
= malloc(sizeof(struct command_ctx
));
1131 /* Allocate data buffer for reception */
1132 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
1133 cmd_ctx
->llm
= NULL
;
1134 cmd_ctx
->session
= NULL
;
1137 * Data is received from the lttng client. The struct
1138 * lttcomm_session_msg (lsm) contains the command and data request of
1141 DBG("Receiving data from client ...");
1142 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
1148 * This function dispatch the work to the kernel or userspace tracer
1149 * libs and fill the lttcomm_lttng_msg data structure of all the needed
1150 * informations for the client. The command context struct contains
1151 * everything this function may needs.
1153 ret
= process_client_msg(cmd_ctx
);
1155 /* TODO: Inform client somehow of the fatal error. At this point,
1156 * ret < 0 means that a malloc failed (ENOMEM). */
1157 /* Error detected but still accept command */
1158 clean_command_ctx(cmd_ctx
);
1162 DBG("Sending response (size: %d, retcode: %d)",
1163 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
1164 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
1166 ERR("Failed to send data back to client");
1169 clean_command_ctx(cmd_ctx
);
1178 * usage function on stderr
1180 static void usage(void)
1182 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
1183 fprintf(stderr
, " -h, --help Display this usage.\n");
1184 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
1185 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
1186 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
1187 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
1188 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
1189 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
1190 fprintf(stderr
, " -V, --version Show version number.\n");
1191 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
1192 fprintf(stderr
, " -q, --quiet No output at all.\n");
1193 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
1197 * daemon argument parsing
1199 static int parse_args(int argc
, char **argv
)
1203 static struct option long_options
[] = {
1204 { "client-sock", 1, 0, 'c' },
1205 { "apps-sock", 1, 0, 'a' },
1206 { "kconsumerd-cmd-sock", 1, 0, 0 },
1207 { "kconsumerd-err-sock", 1, 0, 0 },
1208 { "daemonize", 0, 0, 'd' },
1209 { "sig-parent", 0, 0, 'S' },
1210 { "help", 0, 0, 'h' },
1211 { "group", 1, 0, 'g' },
1212 { "version", 0, 0, 'V' },
1213 { "quiet", 0, 0, 'q' },
1214 { "verbose", 0, 0, 'v' },
1219 int option_index
= 0;
1220 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
1227 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
1229 fprintf(stderr
, " with arg %s\n", optarg
);
1233 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1236 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1242 opt_tracing_group
= strdup(optarg
);
1248 fprintf(stdout
, "%s\n", VERSION
);
1254 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1257 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1266 /* Unknown option or other error.
1267 * Error is printed by getopt, just return */
1276 * init_daemon_socket
1278 * Creates the two needed socket by the daemon.
1279 * apps_sock - The communication socket for all UST apps.
1280 * client_sock - The communication of the cli tool (lttng).
1282 static int init_daemon_socket()
1287 old_umask
= umask(0);
1289 /* Create client tool unix socket */
1290 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
1291 if (client_sock
< 0) {
1292 ERR("Create unix sock failed: %s", client_unix_sock_path
);
1297 /* File permission MUST be 660 */
1298 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1300 ERR("Set file permissions failed: %s", client_unix_sock_path
);
1305 /* Create the application unix socket */
1306 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
1307 if (apps_sock
< 0) {
1308 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
1313 /* File permission MUST be 666 */
1314 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
1316 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
1327 * check_existing_daemon
1329 * Check if the global socket is available.
1330 * If yes, error is returned.
1332 static int check_existing_daemon()
1336 ret
= access(client_unix_sock_path
, F_OK
);
1338 ret
= access(apps_unix_sock_path
, F_OK
);
1347 * Return pointer to home directory path using
1348 * the env variable HOME.
1352 static const char *get_home_dir(void)
1354 const char *home_path
;
1356 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
1357 home_path
= default_home_dir
;
1366 * Set the tracing group gid onto the client socket.
1368 * Race window between mkdir and chown is OK because we are going from
1369 * less permissive (root.root) to more permissive (root.tracing).
1371 static int set_permissions(void)
1376 /* Decide which group name to use */
1377 (opt_tracing_group
!= NULL
) ?
1378 (grp
= getgrnam(opt_tracing_group
)) :
1379 (grp
= getgrnam(default_tracing_group
));
1382 ERR("Missing tracing group. Aborting execution.\n");
1387 /* Set lttng run dir */
1388 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
1390 ERR("Unable to set group on " LTTNG_RUNDIR
);
1394 /* lttng client socket path */
1395 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
1397 ERR("Unable to set group on %s", client_unix_sock_path
);
1401 /* kconsumerd error socket path */
1402 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
1404 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
1408 DBG("All permissions are set");
1415 * create_lttng_rundir
1417 * Create the lttng run directory needed for all
1418 * global sockets and pipe.
1420 static int create_lttng_rundir(void)
1424 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
1426 ERR("Unable to create " LTTNG_RUNDIR
);
1435 * init_kernel_tracer
1437 * Setup necessary data for kernel tracer action.
1439 static void init_kernel_tracer(void)
1441 /* Set the global kernel tracer fd */
1442 kernel_tracer_fd
= open(DEFAULT_KERNEL_TRACER_PATH
, O_RDWR
);
1443 if (kernel_tracer_fd
< 0) {
1444 WARN("No kernel tracer available");
1445 kernel_tracer_fd
= 0;
1448 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1452 * set_kconsumerd_sockets
1454 * Setup sockets and directory needed by the kconsumerd
1455 * communication with the session daemon.
1457 static int set_kconsumerd_sockets(void)
1461 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
1462 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
1465 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
1466 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
1469 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
1471 ERR("Failed to create " KCONSUMERD_PATH
);
1475 /* Create the kconsumerd error unix socket */
1476 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
1477 if (kconsumerd_err_sock
< 0) {
1478 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
1483 /* File permission MUST be 660 */
1484 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1486 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
1498 * Signal handler for the daemon
1500 static void sighandler(int sig
)
1504 DBG("SIGPIPE catched");
1507 DBG("SIGINT catched");
1511 DBG("SIGTERM catched");
1522 * set_signal_handler
1524 * Setup signal handler for :
1525 * SIGINT, SIGTERM, SIGPIPE
1527 static int set_signal_handler(void)
1530 struct sigaction sa
;
1533 if ((ret
= sigemptyset(&sigset
)) < 0) {
1534 perror("sigemptyset");
1538 sa
.sa_handler
= sighandler
;
1539 sa
.sa_mask
= sigset
;
1541 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1542 perror("sigaction");
1546 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1547 perror("sigaction");
1551 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1552 perror("sigaction");
1556 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
1564 int main(int argc
, char **argv
)
1569 /* Parse arguments */
1571 if ((ret
= parse_args(argc
, argv
) < 0)) {
1584 /* Check if daemon is UID = 0 */
1585 is_root
= !getuid();
1587 /* Set all sockets path */
1589 ret
= create_lttng_rundir();
1594 if (strlen(apps_unix_sock_path
) == 0) {
1595 snprintf(apps_unix_sock_path
, PATH_MAX
,
1596 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
1599 if (strlen(client_unix_sock_path
) == 0) {
1600 snprintf(client_unix_sock_path
, PATH_MAX
,
1601 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
1604 ret
= set_kconsumerd_sockets();
1609 /* Setup kernel tracer */
1610 init_kernel_tracer();
1612 if (strlen(apps_unix_sock_path
) == 0) {
1613 snprintf(apps_unix_sock_path
, PATH_MAX
,
1614 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir());
1617 /* Set the cli tool unix socket path */
1618 if (strlen(client_unix_sock_path
) == 0) {
1619 snprintf(client_unix_sock_path
, PATH_MAX
,
1620 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir());
1624 DBG("Client socket path %s", client_unix_sock_path
);
1625 DBG("Application socket path %s", apps_unix_sock_path
);
1627 /* See if daemon already exist. If any of the two
1628 * socket needed by the daemon are present, this test fails
1630 if ((ret
= check_existing_daemon()) == 0) {
1631 ERR("Already running daemon.\n");
1632 /* We do not goto error because we must not
1633 * cleanup() because a daemon is already running.
1638 if (set_signal_handler() < 0) {
1642 /* Setup the needed unix socket */
1643 if (init_daemon_socket() < 0) {
1647 /* Set credentials to socket */
1648 if (is_root
&& (set_permissions() < 0)) {
1652 /* Get parent pid if -S, --sig-parent is specified. */
1653 if (opt_sig_parent
) {
1658 /* Create thread to manage the client socket */
1659 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
1661 perror("pthread_create");
1665 /* Create thread to manage application socket */
1666 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
1668 perror("pthread_create");
1672 ret
= pthread_join(client_thread
, &status
);
1674 perror("pthread_join");