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
;
62 /* Static functions */
63 static int check_existing_daemon(void);
64 static int ust_connect_app(pid_t pid
);
65 static int init_daemon_socket(void);
66 static int notify_apps(const char* name
);
67 static int process_client_msg(struct command_ctx
*cmd_ctx
);
68 static int send_unix_sock(int sock
, void *buf
, size_t len
);
69 static int set_signal_handler(void);
70 static int set_permissions(void);
71 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
);
72 static int create_lttng_rundir(void);
73 static int create_trace_dir(struct ltt_kernel_session
*session
);
74 static int set_kconsumerd_sockets(void);
75 static void cleanup(void);
76 static void sighandler(int sig
);
77 static void clean_command_ctx(struct command_ctx
*cmd_ctx
);
78 static void teardown_kernel_session(struct ltt_session
*session
);
80 static void *thread_manage_clients(void *data
);
81 static void *thread_manage_apps(void *data
);
82 static void *thread_manage_kconsumerd(void *data
);
88 const char *opt_tracing_group
;
89 static int opt_sig_parent
;
90 static int opt_daemon
;
91 static int is_root
; /* Set to 1 if the daemon is running as root */
94 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
95 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
96 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
97 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
99 static int client_sock
;
100 static int apps_sock
;
101 static int kconsumerd_err_sock
;
102 static int kconsumerd_cmd_sock
;
103 static int kernel_tracer_fd
;
104 static pthread_t kconsumerd_thread
;
105 static sem_t kconsumerd_sem
;
107 static pthread_mutex_t kconsumerd_pid_mutex
;
108 static pid_t kconsumerd_pid
;
111 * thread_manage_kconsumerd
113 * This thread manage the kconsumerd error sent
114 * back to the session daemon.
116 static void *thread_manage_kconsumerd(void *data
)
119 enum lttcomm_return_code code
;
121 DBG("[thread] Manage kconsumerd started");
123 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
128 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
133 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
138 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
139 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
140 if (kconsumerd_cmd_sock
< 0) {
141 perror("kconsumerd connect");
144 /* Signal condition to tell that the kconsumerd is ready */
145 sem_post(&kconsumerd_sem
);
146 DBG("Kconsumerd command socket ready");
148 DBG("[sessiond] Kconsumerd error when waiting for SOCK_READY : %s",
149 lttcomm_get_readable_code(-code
));
153 /* Wait for any kconsumerd error */
154 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
156 ERR("[sessiond] Kconsumerd closed the command socket");
160 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
163 teardown_kernel_session((struct ltt_session
*) data
);
170 * This thread manage the application socket communication
172 static void *thread_manage_apps(void *data
)
176 /* TODO: Something more elegant is needed but fine for now */
177 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
178 * for 32-bit vs 64-bit compat processes. */
179 /* replicate in ust with version number */
181 int reg
; /* 1:register, 0:unregister */
186 DBG("[thread] Manage apps started");
188 ret
= lttcomm_listen_unix_sock(apps_sock
);
193 /* Notify all applications to register */
194 notify_apps(default_global_apps_pipe
);
197 DBG("Accepting application registration");
198 /* Blocking call, waiting for transmission */
199 sock
= lttcomm_accept_unix_sock(apps_sock
);
204 /* Basic recv here to handle the very simple data
205 * that the libust send to register (reg_msg).
207 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
213 /* Add application to the global traceable list */
214 if (reg_msg
.reg
== 1) {
216 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
218 /* register_traceable_app only return an error with
219 * ENOMEM. At this point, we better stop everything.
225 unregister_traceable_app(reg_msg
.pid
);
235 * thread_manage_clients
237 * This thread manage all clients request using the unix
238 * client socket for communication.
240 static void *thread_manage_clients(void *data
)
243 struct command_ctx
*cmd_ctx
;
245 DBG("[thread] Manage client started");
247 ret
= lttcomm_listen_unix_sock(client_sock
);
252 /* Notify parent pid that we are ready
253 * to accept command for client side.
255 if (opt_sig_parent
) {
260 /* Blocking call, waiting for transmission */
261 DBG("Accepting client command ...");
262 sock
= lttcomm_accept_unix_sock(client_sock
);
267 /* Allocate context command to process the client request */
268 cmd_ctx
= malloc(sizeof(struct command_ctx
));
270 /* Allocate data buffer for reception */
271 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
273 cmd_ctx
->session
= NULL
;
276 * Data is received from the lttng client. The struct
277 * lttcomm_session_msg (lsm) contains the command and data request of
280 DBG("Receiving data from client ...");
281 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
287 * This function dispatch the work to the kernel or userspace tracer
288 * libs and fill the lttcomm_lttng_msg data structure of all the needed
289 * informations for the client. The command context struct contains
290 * everything this function may needs.
292 ret
= process_client_msg(cmd_ctx
);
294 /* TODO: Inform client somehow of the fatal error. At this point,
295 * ret < 0 means that a malloc failed (ENOMEM). */
296 /* Error detected but still accept command */
297 clean_command_ctx(cmd_ctx
);
301 DBG("Sending response (size: %d, retcode: %d)",
302 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
303 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
305 ERR("Failed to send data back to client");
308 clean_command_ctx(cmd_ctx
);
318 * Send data on a unix socket using the liblttsessiondcomm API.
320 * Return lttcomm error code.
322 static int send_unix_sock(int sock
, void *buf
, size_t len
)
324 /* Check valid length */
329 return lttcomm_send_unix_sock(sock
, buf
, len
);
335 * Free memory of a command context structure.
337 static void clean_command_ctx(struct command_ctx
*cmd_ctx
)
339 DBG("Clean command context structure %p", cmd_ctx
);
355 * Return a socket connected to the libust communication socket
356 * of the application identified by the pid.
358 * If the pid is not found in the traceable list,
359 * return -1 to indicate error.
361 static int ust_connect_app(pid_t pid
)
364 struct ltt_traceable_app
*lta
;
366 DBG("Connect to application pid %d", pid
);
368 lta
= find_app_by_pid(pid
);
371 DBG("Application pid %d not found", pid
);
375 sock
= ustctl_connect_pid(lta
->pid
);
377 ERR("Fail connecting to the PID %d\n", pid
);
386 * Notify apps by writing 42 to a named pipe using name.
387 * Every applications waiting for a ltt-sessiond will be notified
388 * and re-register automatically to the session daemon.
390 * Return open or write error value.
392 static int notify_apps(const char *name
)
397 DBG("Notify the global application pipe");
399 /* Try opening the global pipe */
400 fd
= open(name
, O_WRONLY
);
405 /* Notify by writing on the pipe */
406 ret
= write(fd
, "42", 2);
418 * Setup the outgoing data buffer for the response (llm) by allocating the
419 * right amount of memory and copying the original information from the lsm
422 * Return total size of the buffer pointed by buf.
424 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
426 int ret
, buf_size
, trace_name_size
;
429 * Check for the trace_name. If defined, it's part of the payload data of
432 trace_name_size
= strlen(cmd_ctx
->lsm
->trace_name
);
433 buf_size
= trace_name_size
+ size
;
435 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
436 if (cmd_ctx
->llm
== NULL
) {
442 /* Copy common data */
443 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
444 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->pid
;
445 if (!uuid_is_null(cmd_ctx
->lsm
->session_uuid
)) {
446 uuid_copy(cmd_ctx
->llm
->session_uuid
, cmd_ctx
->lsm
->session_uuid
);
449 cmd_ctx
->llm
->trace_name_offset
= trace_name_size
;
450 cmd_ctx
->llm
->data_size
= size
;
451 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
453 /* Copy trace name to the llm structure. Begining of the payload. */
454 memcpy(cmd_ctx
->llm
->payload
, cmd_ctx
->lsm
->trace_name
, trace_name_size
);
463 * start_kconsumerd_thread
465 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
466 * exec or it will fails.
468 static int start_kconsumerd_thread(struct ltt_session
*session
)
472 /* Setup semaphore */
473 sem_init(&kconsumerd_sem
, 0, 0);
475 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) session
);
477 perror("pthread_create kconsumerd");
481 sem_wait(&kconsumerd_sem
);
490 * kernel_start_consumer
492 * Start a kernel consumer daemon (kconsumerd).
494 * Return pid if successful else -1.
496 pid_t
kernel_start_consumer(void)
506 execlp("kconsumerd", "kconsumerd", "--daemonize", NULL
);
508 perror("kernel start consumer exec");
511 } else if (pid
> 0) {
515 perror("kernel start consumer fork");
525 * send_kconsumerd_fds
527 * Send all stream fds of the kernel session to the consumer.
529 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
532 /* Plus one here for the metadata fd */
533 size_t nb_fd
= session
->stream_count_global
+ 1;
535 struct ltt_kernel_stream
*stream
;
536 struct ltt_kernel_channel
*chan
;
537 struct lttcomm_kconsumerd_header lkh
;
538 struct lttcomm_kconsumerd_msg buf
[nb_fd
];
540 /* Add metadata data */
541 fds
[i
] = session
->metadata_stream_fd
;
543 buf
[i
].state
= ACTIVE_FD
;
544 buf
[i
].max_sb_size
= session
->metadata
->conf
->subbuf_size
;
545 strncpy(buf
[i
].path_name
, session
->metadata
->pathname
, PATH_MAX
);
547 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
548 cds_list_for_each_entry(stream
, &chan
->stream_list
.head
, list
) {
551 buf
[i
].fd
= stream
->fd
;
552 buf
[i
].state
= stream
->state
;
553 buf
[i
].max_sb_size
= chan
->channel
->subbuf_size
;
554 strncpy(buf
[i
].path_name
, stream
->pathname
, PATH_MAX
);
559 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
560 lkh
.cmd_type
= LTTCOMM_ADD_STREAM
;
562 DBG("Sending kconsumerd header");
564 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
566 perror("send kconsumerd header");
570 DBG("Sending all fds to kconsumerd");
572 ret
= lttcomm_send_fds_unix_sock(sock
, buf
, fds
, nb_fd
, lkh
.payload_size
);
574 perror("send kconsumerd fds");
578 DBG("Kconsumerd fds sent");
587 * free_kernel_session
589 * Free all data structure inside a kernel session and the session pointer.
591 static void free_kernel_session(struct ltt_kernel_session
*session
)
593 struct ltt_kernel_channel
*chan
;
594 struct ltt_kernel_stream
*stream
;
595 struct ltt_kernel_event
*event
;
598 close(session
->metadata_stream_fd
);
599 close(session
->metadata
->fd
);
600 free(session
->metadata
->conf
);
601 free(session
->metadata
);
603 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
604 /* Clean all event(s) */
605 cds_list_for_each_entry(event
, &chan
->events_list
.head
, list
) {
612 cds_list_for_each_entry(stream
, &chan
->stream_list
.head
, list
) {
614 free(stream
->pathname
);
620 free(chan
->pathname
);
627 DBG("All kernel session data structures freed");
631 * teardown_kernel_session
633 * Complete teardown of a kernel session. This free all data structure
634 * related to a kernel session and update counter.
636 static void teardown_kernel_session(struct ltt_session
*session
)
638 if (session
->kernel_session
!= NULL
) {
639 DBG("Tearing down kernel session");
640 free_kernel_session(session
->kernel_session
);
641 /* Extra precaution */
642 session
->kernel_session
= NULL
;
643 /* Decrement session count */
644 session
->kern_session_count
--;
645 /* Set kconsumerd pid to 0 (inactive) */
646 session
->kernel_consumer
= 0;
653 * Process the command requested by the lttng client within the command
654 * context structure. This function make sure that the return structure (llm)
655 * is set and ready for transmission before returning.
657 * Return any error encountered or 0 for success.
659 static int process_client_msg(struct command_ctx
*cmd_ctx
)
663 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
665 /* Check command that needs a session */
666 switch (cmd_ctx
->lsm
->cmd_type
) {
667 case LTTNG_CREATE_SESSION
:
668 case LTTNG_LIST_SESSIONS
:
672 cmd_ctx
->session
= find_session_by_uuid(cmd_ctx
->lsm
->session_uuid
);
673 if (cmd_ctx
->session
== NULL
) {
674 ret
= LTTCOMM_SELECT_SESS
;
680 /* Check command for kernel tracing */
681 switch (cmd_ctx
->lsm
->cmd_type
) {
682 case KERNEL_CREATE_SESSION
:
683 case KERNEL_CREATE_CHANNEL
:
684 case KERNEL_CREATE_STREAM
:
685 case KERNEL_DISABLE_EVENT
:
686 case KERNEL_ENABLE_EVENT
:
687 case KERNEL_OPEN_METADATA
:
688 case KERNEL_START_TRACE
:
689 case KERNEL_STOP_TRACE
:
690 /* TODO: reconnect to kernel tracer to check if
692 if (kernel_tracer_fd
== 0) {
693 ret
= LTTCOMM_KERN_NA
;
699 /* Connect to ust apps if available pid */
700 if (cmd_ctx
->lsm
->pid
> 0) {
701 /* Connect to app using ustctl API */
702 cmd_ctx
->ust_sock
= ust_connect_app(cmd_ctx
->lsm
->pid
);
703 if (cmd_ctx
->ust_sock
< 0) {
704 ret
= LTTCOMM_NO_TRACEABLE
;
709 /* Process by command type */
710 switch (cmd_ctx
->lsm
->cmd_type
) {
711 case KERNEL_CREATE_SESSION
:
713 ret
= setup_lttng_msg(cmd_ctx
, 0);
718 DBG("Checking if kconsumerd is alive");
719 pthread_mutex_lock(&kconsumerd_pid_mutex
);
720 if (kconsumerd_pid
== 0) {
721 ret
= kernel_start_consumer();
723 ERR("Kernel start kconsumerd failed");
724 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
725 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
729 /* Setting up the global kconsumerd_pid */
730 kconsumerd_pid
= ret
;
732 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
734 ret
= start_kconsumerd_thread(cmd_ctx
->session
);
736 ERR("Fatal error : start_kconsumerd_thread()");
741 DBG("Creating kernel session");
743 ret
= kernel_create_session(cmd_ctx
->session
, kernel_tracer_fd
);
745 ret
= LTTCOMM_KERN_SESS_FAIL
;
752 case KERNEL_CREATE_CHANNEL
:
754 ret
= setup_lttng_msg(cmd_ctx
, 0);
759 DBG("Creating kernel channel");
761 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
);
764 ret
= LTTCOMM_KERN_CHAN_FAIL
;
771 case KERNEL_ENABLE_EVENT
:
773 /* Setup lttng message with no payload */
774 ret
= setup_lttng_msg(cmd_ctx
, 0);
779 DBG("Enabling kernel event %s", cmd_ctx
->lsm
->u
.event
.event_name
);
781 ret
= kernel_enable_event(cmd_ctx
->session
->kernel_session
, cmd_ctx
->lsm
->u
.event
.event_name
);
783 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
790 case KERNEL_OPEN_METADATA
:
792 /* Setup lttng message with no payload */
793 ret
= setup_lttng_msg(cmd_ctx
, 0);
798 DBG("Open kernel metadata");
800 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
);
802 ret
= LTTCOMM_KERN_META_FAIL
;
809 case KERNEL_CREATE_STREAM
:
811 struct ltt_kernel_channel
*chan
;
812 /* Setup lttng message with no payload */
813 ret
= setup_lttng_msg(cmd_ctx
, 0);
818 DBG("Creating kernel stream");
820 ret
= kernel_create_metadata_stream(cmd_ctx
->session
->kernel_session
);
822 ERR("Kernel create metadata stream failed");
823 ret
= LTTCOMM_KERN_STREAM_FAIL
;
827 /* For each channel */
828 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
829 ret
= kernel_create_channel_stream(chan
);
831 ERR("Kernel create channel stream failed");
832 ret
= LTTCOMM_KERN_STREAM_FAIL
;
835 /* Update the stream global counter */
836 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
842 case KERNEL_START_TRACE
:
844 /* Setup lttng message with no payload */
845 ret
= setup_lttng_msg(cmd_ctx
, 0);
850 DBG("Start kernel tracing");
852 ret
= create_trace_dir(cmd_ctx
->session
->kernel_session
);
854 if (ret
== -EEXIST
) {
855 ret
= LTTCOMM_KERN_DIR_EXIST
;
857 ret
= LTTCOMM_KERN_DIR_FAIL
;
862 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
864 ERR("Kernel start session failed");
865 ret
= LTTCOMM_KERN_START_FAIL
;
869 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, cmd_ctx
->session
->kernel_session
);
871 ERR("Send kconsumerd fds failed");
872 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
879 case KERNEL_STOP_TRACE
:
881 /* Setup lttng message with no payload */
882 ret
= setup_lttng_msg(cmd_ctx
, 0);
887 DBG("Stop kernel tracing");
889 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
891 ERR("Kernel stop session failed");
892 ret
= LTTCOMM_KERN_STOP_FAIL
;
896 /* Clean kernel session teardown */
897 teardown_kernel_session(cmd_ctx
->session
);
902 case LTTNG_CREATE_SESSION
:
904 /* Setup lttng message with no payload */
905 ret
= setup_lttng_msg(cmd_ctx
, 0);
910 ret
= create_session(cmd_ctx
->lsm
->session_name
, &cmd_ctx
->llm
->session_uuid
);
913 ret
= LTTCOMM_EXIST_SESS
;
923 case LTTNG_DESTROY_SESSION
:
925 /* Setup lttng message with no payload */
926 ret
= setup_lttng_msg(cmd_ctx
, 0);
931 ret
= destroy_session(&cmd_ctx
->lsm
->session_uuid
);
933 ret
= LTTCOMM_NO_SESS
;
940 case LTTNG_LIST_TRACES
:
942 unsigned int trace_count
;
944 trace_count
= get_trace_count_per_session(cmd_ctx
->session
);
945 if (trace_count
== 0) {
946 ret
= LTTCOMM_NO_TRACE
;
950 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_trace
) * trace_count
);
955 get_traces_per_session(cmd_ctx
->session
,
956 (struct lttng_trace
*)(cmd_ctx
->llm
->payload
));
961 case UST_CREATE_TRACE
:
963 /* Setup lttng message with no payload */
964 ret
= setup_lttng_msg(cmd_ctx
, 0);
969 ret
= ust_create_trace(cmd_ctx
);
977 unsigned int app_count
;
979 app_count
= get_app_count();
980 DBG("Traceable application count : %d", app_count
);
981 if (app_count
== 0) {
982 ret
= LTTCOMM_NO_APPS
;
986 ret
= setup_lttng_msg(cmd_ctx
, sizeof(pid_t
) * app_count
);
991 get_app_list_pids((pid_t
*)(cmd_ctx
->llm
->payload
));
996 case UST_START_TRACE
:
998 /* Setup lttng message with no payload */
999 ret
= setup_lttng_msg(cmd_ctx
, 0);
1004 ret
= ust_start_trace(cmd_ctx
);
1010 case UST_STOP_TRACE
:
1012 /* Setup lttng message with no payload */
1013 ret
= setup_lttng_msg(cmd_ctx
, 0);
1018 ret
= ust_stop_trace(cmd_ctx
);
1024 case LTTNG_LIST_SESSIONS
:
1026 unsigned int session_count
;
1028 session_count
= get_session_count();
1029 if (session_count
== 0) {
1030 ret
= LTTCOMM_NO_SESS
;
1034 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * session_count
);
1039 get_lttng_session((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
1045 /* Undefined command */
1046 ret
= setup_lttng_msg(cmd_ctx
, 0);
1055 /* Set return code */
1056 cmd_ctx
->llm
->ret_code
= ret
;
1061 if (cmd_ctx
->llm
== NULL
) {
1062 DBG("Missing llm structure. Allocating one.");
1063 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
1067 /* Notify client of error */
1068 cmd_ctx
->llm
->ret_code
= ret
;
1075 * usage function on stderr
1077 static void usage(void)
1079 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
1080 fprintf(stderr
, " -h, --help Display this usage.\n");
1081 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
1082 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
1083 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
1084 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
1085 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
1086 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
1087 fprintf(stderr
, " -V, --version Show version number.\n");
1088 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
1089 fprintf(stderr
, " -q, --quiet No output at all.\n");
1090 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
1094 * daemon argument parsing
1096 static int parse_args(int argc
, char **argv
)
1100 static struct option long_options
[] = {
1101 { "client-sock", 1, 0, 'c' },
1102 { "apps-sock", 1, 0, 'a' },
1103 { "kconsumerd-cmd-sock", 1, 0, 0 },
1104 { "kconsumerd-err-sock", 1, 0, 0 },
1105 { "daemonize", 0, 0, 'd' },
1106 { "sig-parent", 0, 0, 'S' },
1107 { "help", 0, 0, 'h' },
1108 { "group", 1, 0, 'g' },
1109 { "version", 0, 0, 'V' },
1110 { "quiet", 0, 0, 'q' },
1111 { "verbose", 0, 0, 'v' },
1116 int option_index
= 0;
1117 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
1124 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
1126 fprintf(stderr
, " with arg %s\n", optarg
);
1130 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1133 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1139 opt_tracing_group
= strdup(optarg
);
1145 fprintf(stdout
, "%s\n", VERSION
);
1151 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1154 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1163 /* Unknown option or other error.
1164 * Error is printed by getopt, just return */
1173 * init_daemon_socket
1175 * Creates the two needed socket by the daemon.
1176 * apps_sock - The communication socket for all UST apps.
1177 * client_sock - The communication of the cli tool (lttng).
1179 static int init_daemon_socket()
1184 old_umask
= umask(0);
1186 /* Create client tool unix socket */
1187 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
1188 if (client_sock
< 0) {
1189 ERR("Create unix sock failed: %s", client_unix_sock_path
);
1194 /* File permission MUST be 660 */
1195 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1197 ERR("Set file permissions failed: %s", client_unix_sock_path
);
1202 /* Create the application unix socket */
1203 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
1204 if (apps_sock
< 0) {
1205 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
1210 /* File permission MUST be 666 */
1211 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
1213 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
1224 * check_existing_daemon
1226 * Check if the global socket is available.
1227 * If yes, error is returned.
1229 static int check_existing_daemon()
1233 ret
= access(client_unix_sock_path
, F_OK
);
1235 ret
= access(apps_unix_sock_path
, F_OK
);
1244 * Return pointer to home directory path using
1245 * the env variable HOME.
1249 static const char *get_home_dir(void)
1251 const char *home_path
;
1253 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
1254 home_path
= default_home_dir
;
1263 * Set the tracing group gid onto the client socket.
1265 * Race window between mkdir and chown is OK because we are going from
1266 * less permissive (root.root) to more permissive (root.tracing).
1268 static int set_permissions(void)
1273 /* Decide which group name to use */
1274 (opt_tracing_group
!= NULL
) ?
1275 (grp
= getgrnam(opt_tracing_group
)) :
1276 (grp
= getgrnam(default_tracing_group
));
1279 ERR("Missing tracing group. Aborting execution.\n");
1284 /* Set lttng run dir */
1285 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
1287 ERR("Unable to set group on " LTTNG_RUNDIR
);
1291 /* lttng client socket path */
1292 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
1294 ERR("Unable to set group on %s", client_unix_sock_path
);
1298 /* kconsumerd error socket path */
1299 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
1301 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
1305 DBG("All permissions are set");
1314 * Create the trace output directory.
1316 static int create_trace_dir(struct ltt_kernel_session
*session
)
1319 struct ltt_kernel_channel
*chan
;
1321 /* Create all channel directories */
1322 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
1323 ret
= mkdir(chan
->pathname
, S_IRWXU
| S_IRWXG
);
1325 perror("mkdir trace path");
1338 * create_lttng_rundir
1340 * Create the lttng run directory needed for all
1341 * global sockets and pipe.
1343 static int create_lttng_rundir(void)
1347 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
1349 ERR("Unable to create " LTTNG_RUNDIR
);
1358 * init_kernel_tracer
1360 * Setup necessary data for kernel tracer action.
1362 static void init_kernel_tracer(void)
1364 /* Set the global kernel tracer fd */
1365 kernel_tracer_fd
= open(DEFAULT_KERNEL_TRACER_PATH
, O_RDWR
);
1366 if (kernel_tracer_fd
< 0) {
1367 WARN("No kernel tracer available");
1368 kernel_tracer_fd
= 0;
1371 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1375 * set_kconsumerd_sockets
1377 * Setup sockets and directory needed by the kconsumerd
1378 * communication with the session daemon.
1380 static int set_kconsumerd_sockets(void)
1384 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
1385 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
1388 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
1389 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
1392 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
1394 ERR("Failed to create " KCONSUMERD_PATH
);
1398 /* Create the kconsumerd error unix socket */
1399 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
1400 if (kconsumerd_err_sock
< 0) {
1401 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
1406 /* File permission MUST be 660 */
1407 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1409 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
1419 * set_signal_handler
1421 * Setup signal handler for :
1422 * SIGINT, SIGTERM, SIGPIPE
1424 static int set_signal_handler(void)
1427 struct sigaction sa
;
1430 if ((ret
= sigemptyset(&sigset
)) < 0) {
1431 perror("sigemptyset");
1435 sa
.sa_handler
= sighandler
;
1436 sa
.sa_mask
= sigset
;
1438 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1439 perror("sigaction");
1443 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1444 perror("sigaction");
1448 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1449 perror("sigaction");
1453 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
1461 * Signal handler for the daemon
1463 static void sighandler(int sig
)
1467 DBG("SIGPIPE catched");
1470 DBG("SIGINT catched");
1474 DBG("SIGTERM catched");
1487 * Cleanup the daemon on exit
1489 static void cleanup()
1493 struct ltt_session
*sess
;
1494 struct ltt_session_list
*session_list
= get_session_list();
1499 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
1500 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
1503 unlink(client_unix_sock_path
);
1504 unlink(apps_unix_sock_path
);
1505 unlink(kconsumerd_err_unix_sock_path
);
1507 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
1509 ERR("asprintf failed. Something is really wrong!");
1512 /* Remove lttng run directory */
1515 ERR("Unable to clean " LTTNG_RUNDIR
);
1518 /* Cleanup ALL session */
1519 cds_list_for_each_entry(sess
, &session_list
->head
, list
) {
1520 teardown_kernel_session(sess
);
1521 // TODO complete session cleanup (including UST)
1524 close(kernel_tracer_fd
);
1530 int main(int argc
, char **argv
)
1535 pthread_t threads
[2];
1537 /* Parse arguments */
1539 if ((ret
= parse_args(argc
, argv
) < 0)) {
1552 /* Check if daemon is UID = 0 */
1553 is_root
= !getuid();
1555 /* Set all sockets path */
1557 ret
= create_lttng_rundir();
1562 if (strlen(apps_unix_sock_path
) == 0) {
1563 snprintf(apps_unix_sock_path
, PATH_MAX
,
1564 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
1567 if (strlen(client_unix_sock_path
) == 0) {
1568 snprintf(client_unix_sock_path
, PATH_MAX
,
1569 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
1572 ret
= set_kconsumerd_sockets();
1577 /* Setup kernel tracer */
1578 init_kernel_tracer();
1580 if (strlen(apps_unix_sock_path
) == 0) {
1581 snprintf(apps_unix_sock_path
, PATH_MAX
,
1582 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir());
1585 /* Set the cli tool unix socket path */
1586 if (strlen(client_unix_sock_path
) == 0) {
1587 snprintf(client_unix_sock_path
, PATH_MAX
,
1588 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir());
1592 DBG("Client socket path %s", client_unix_sock_path
);
1593 DBG("Application socket path %s", apps_unix_sock_path
);
1595 /* See if daemon already exist. If any of the two
1596 * socket needed by the daemon are present, this test fails
1598 if ((ret
= check_existing_daemon()) == 0) {
1599 ERR("Already running daemon.\n");
1600 /* We do not goto error because we must not
1601 * cleanup() because a daemon is already running.
1606 if (set_signal_handler() < 0) {
1610 /* Setup the needed unix socket */
1611 if (init_daemon_socket() < 0) {
1615 /* Set credentials to socket */
1616 if (is_root
&& (set_permissions() < 0)) {
1620 /* Get parent pid if -S, --sig-parent is specified. */
1621 if (opt_sig_parent
) {
1626 /* Create thread to manage the client socket */
1627 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
1629 perror("pthread_create");
1633 /* Create thread to manage application socket */
1634 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
1636 perror("pthread_create");
1640 for (i
= 0; i
< 2; i
++) {
1641 ret
= pthread_join(threads
[i
], &status
);
1643 perror("pthread_join");