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.
31 #include <sys/socket.h>
33 #include <sys/types.h>
36 #include <urcu/list.h> /* URCU list library (-lurcu) */
37 #include <ust/ustctl.h> /* UST control lib (-lust) */
38 #include <lttng/lttng.h>
40 #include "liblttsessiondcomm.h"
41 #include "ltt-sessiond.h"
43 #include "kernel-ctl.h"
46 #include "traceable-app.h"
50 * teardown: signal SIGTERM handler -> write into pipe. Threads waits
51 * with epoll on pipe and on other pipes/sockets for commands. Main
52 * simply waits on pthread join.
56 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
57 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
58 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
59 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
61 /* Static functions */
62 static int check_existing_daemon(void);
63 static int ust_connect_app(pid_t pid
);
64 static int init_daemon_socket(void);
65 static int notify_apps(const char* name
);
66 static int process_client_msg(struct command_ctx
*cmd_ctx
);
67 static int send_unix_sock(int sock
, void *buf
, size_t len
);
68 static int set_signal_handler(void);
69 static int set_permissions(void);
70 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
);
71 static int create_lttng_rundir(void);
72 static int set_kconsumerd_sockets(void);
73 static void cleanup(void);
74 static void sighandler(int sig
);
75 static void clean_command_ctx(struct command_ctx
*cmd_ctx
);
77 static void *thread_manage_clients(void *data
);
78 static void *thread_manage_apps(void *data
);
79 static void *thread_manage_kconsumerd(void *data
);
85 const char *opt_tracing_group
;
86 static int opt_sig_parent
;
87 static int opt_daemon
;
88 static int is_root
; /* Set to 1 if the daemon is running as root */
91 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
92 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
93 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
94 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
96 static int client_sock
;
98 static int kconsumerd_err_sock
;
99 static int kernel_tracer_fd
;
102 * thread_manage_kconsumerd
104 * This thread manage the kconsumerd error sent
105 * back to the session daemon.
107 static void *thread_manage_kconsumerd(void *data
)
111 DBG("[thread] Manage kconsumerd started");
113 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
118 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
124 //ret = lttcomm_recv_unix_sock(sock, &lsm, sizeof(lsm));
126 /* TODO: Consumerd died? */
138 * This thread manage the application socket communication
140 static void *thread_manage_apps(void *data
)
144 /* TODO: Something more elegant is needed but fine for now */
145 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
146 * for 32-bit vs 64-bit compat processes. */
147 /* replicate in ust with version number */
149 int reg
; /* 1:register, 0:unregister */
154 DBG("[thread] Manage apps started");
156 ret
= lttcomm_listen_unix_sock(apps_sock
);
161 /* Notify all applications to register */
162 notify_apps(default_global_apps_pipe
);
165 DBG("Accepting application registration");
166 /* Blocking call, waiting for transmission */
167 sock
= lttcomm_accept_unix_sock(apps_sock
);
172 /* Basic recv here to handle the very simple data
173 * that the libust send to register (reg_msg).
175 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
181 /* Add application to the global traceable list */
182 if (reg_msg
.reg
== 1) {
184 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
186 /* register_traceable_app only return an error with
187 * ENOMEM. At this point, we better stop everything.
193 unregister_traceable_app(reg_msg
.pid
);
203 * thread_manage_clients
205 * This thread manage all clients request using the unix
206 * client socket for communication.
208 static void *thread_manage_clients(void *data
)
211 struct command_ctx
*cmd_ctx
;
213 DBG("[thread] Manage client started");
215 ret
= lttcomm_listen_unix_sock(client_sock
);
220 /* Notify parent pid that we are ready
221 * to accept command for client side.
223 if (opt_sig_parent
) {
228 /* Blocking call, waiting for transmission */
229 DBG("Accepting client command ...");
230 sock
= lttcomm_accept_unix_sock(client_sock
);
235 /* Allocate context command to process the client request */
236 cmd_ctx
= malloc(sizeof(struct command_ctx
));
238 /* Allocate data buffer for reception */
239 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
241 cmd_ctx
->session
= NULL
;
244 * Data is received from the lttng client. The struct
245 * lttcomm_session_msg (lsm) contains the command and data request of
248 DBG("Receiving data from client ...");
249 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
255 * This function dispatch the work to the kernel or userspace tracer
256 * libs and fill the lttcomm_lttng_msg data structure of all the needed
257 * informations for the client. The command context struct contains
258 * everything this function may needs.
260 ret
= process_client_msg(cmd_ctx
);
262 /* TODO: Inform client somehow of the fatal error. At this point,
263 * ret < 0 means that a malloc failed (ENOMEM). */
264 /* Error detected but still accept command */
265 clean_command_ctx(cmd_ctx
);
269 DBG("Sending response (size: %d, retcode: %d)",
270 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
271 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
273 ERR("Failed to send data back to client");
276 clean_command_ctx(cmd_ctx
);
286 * Send data on a unix socket using the liblttsessiondcomm API.
288 * Return lttcomm error code.
290 static int send_unix_sock(int sock
, void *buf
, size_t len
)
292 /* Check valid length */
297 return lttcomm_send_unix_sock(sock
, buf
, len
);
303 * Free memory of a command context structure.
305 static void clean_command_ctx(struct command_ctx
*cmd_ctx
)
307 DBG("Clean command context structure %p", cmd_ctx
);
323 * Return a socket connected to the libust communication socket
324 * of the application identified by the pid.
326 * If the pid is not found in the traceable list,
327 * return -1 to indicate error.
329 static int ust_connect_app(pid_t pid
)
332 struct ltt_traceable_app
*lta
;
334 DBG("Connect to application pid %d", pid
);
336 lta
= find_app_by_pid(pid
);
339 DBG("Application pid %d not found", pid
);
343 sock
= ustctl_connect_pid(lta
->pid
);
345 ERR("Fail connecting to the PID %d\n", pid
);
354 * Notify apps by writing 42 to a named pipe using name.
355 * Every applications waiting for a ltt-sessiond will be notified
356 * and re-register automatically to the session daemon.
358 * Return open or write error value.
360 static int notify_apps(const char *name
)
365 DBG("Notify the global application pipe");
367 /* Try opening the global pipe */
368 fd
= open(name
, O_WRONLY
);
373 /* Notify by writing on the pipe */
374 ret
= write(fd
, "42", 2);
386 * Setup the outgoing data buffer for the response (llm) by allocating the
387 * right amount of memory and copying the original information from the lsm
390 * Return total size of the buffer pointed by buf.
392 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
394 int ret
, buf_size
, trace_name_size
;
397 * Check for the trace_name. If defined, it's part of the payload data of
400 trace_name_size
= strlen(cmd_ctx
->lsm
->trace_name
);
401 buf_size
= trace_name_size
+ size
;
403 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
404 if (cmd_ctx
->llm
== NULL
) {
410 /* Copy common data */
411 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
412 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->pid
;
413 if (!uuid_is_null(cmd_ctx
->lsm
->session_uuid
)) {
414 uuid_copy(cmd_ctx
->llm
->session_uuid
, cmd_ctx
->lsm
->session_uuid
);
417 cmd_ctx
->llm
->trace_name_offset
= trace_name_size
;
418 cmd_ctx
->llm
->data_size
= size
;
419 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
421 /* Copy trace name to the llm structure. Begining of the payload. */
422 memcpy(cmd_ctx
->llm
->payload
, cmd_ctx
->lsm
->trace_name
, trace_name_size
);
433 * Process the command requested by the lttng client within the command
434 * context structure. This function make sure that the return structure (llm)
435 * is set and ready for transmission before returning.
437 * Return any error encountered or 0 for success.
439 static int process_client_msg(struct command_ctx
*cmd_ctx
)
443 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
445 /* Check command that needs a session */
446 switch (cmd_ctx
->lsm
->cmd_type
) {
447 case LTTNG_CREATE_SESSION
:
448 case LTTNG_LIST_SESSIONS
:
452 cmd_ctx
->session
= find_session_by_uuid(cmd_ctx
->lsm
->session_uuid
);
453 if (cmd_ctx
->session
== NULL
) {
454 ret
= LTTCOMM_SELECT_SESS
;
460 /* Check command for kernel tracing */
461 switch (cmd_ctx
->lsm
->cmd_type
) {
462 case KERNEL_CREATE_SESSION
:
463 case KERNEL_CREATE_CHANNEL
:
464 case KERNEL_DISABLE_EVENT
:
465 case KERNEL_ENABLE_EVENT
:
466 case KERNEL_OPEN_METADATA
:
467 case KERNEL_START_TRACE
:
468 case KERNEL_STOP_TRACE
:
469 /* TODO: reconnect to kernel tracer to check if
471 if (kernel_tracer_fd
== 0) {
472 ret
= LTTCOMM_KERN_NA
;
478 /* Connect to ust apps if available pid */
479 if (cmd_ctx
->lsm
->pid
> 0) {
480 /* Connect to app using ustctl API */
481 cmd_ctx
->ust_sock
= ust_connect_app(cmd_ctx
->lsm
->pid
);
482 if (cmd_ctx
->ust_sock
< 0) {
483 ret
= LTTCOMM_NO_TRACEABLE
;
488 /* Process by command type */
489 switch (cmd_ctx
->lsm
->cmd_type
) {
490 case KERNEL_CREATE_SESSION
:
492 ret
= setup_lttng_msg(cmd_ctx
, 0);
497 DBG("Creating kernel session");
499 ret
= kernel_create_session(cmd_ctx
, kernel_tracer_fd
);
501 ret
= LTTCOMM_KERN_SESS_FAIL
;
508 case KERNEL_CREATE_CHANNEL
:
510 ret
= setup_lttng_msg(cmd_ctx
, 0);
515 DBG("Creating kernel session");
517 ret
= kernel_create_channel(cmd_ctx
);
519 ret
= LTTCOMM_KERN_CHAN_FAIL
;
526 case KERNEL_ENABLE_EVENT
:
528 /* Setup lttng message with no payload */
529 ret
= setup_lttng_msg(cmd_ctx
, 0);
534 DBG("Enabling kernel event %s", cmd_ctx
->lsm
->u
.event
.event_name
);
536 ret
= kernel_enable_event(cmd_ctx
->session
->kernel_session
->channel
, cmd_ctx
->lsm
->u
.event
.event_name
);
538 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
545 case KERNEL_OPEN_METADATA
:
547 /* Setup lttng message with no payload */
548 ret
= setup_lttng_msg(cmd_ctx
, 0);
553 DBG("Open kernel metadata");
555 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
);
557 ret
= LTTCOMM_KERN_META_FAIL
;
564 case LTTNG_CREATE_SESSION
:
566 /* Setup lttng message with no payload */
567 ret
= setup_lttng_msg(cmd_ctx
, 0);
572 ret
= create_session(cmd_ctx
->lsm
->session_name
, &cmd_ctx
->llm
->session_uuid
);
575 ret
= LTTCOMM_EXIST_SESS
;
585 case LTTNG_DESTROY_SESSION
:
587 /* Setup lttng message with no payload */
588 ret
= setup_lttng_msg(cmd_ctx
, 0);
593 ret
= destroy_session(&cmd_ctx
->lsm
->session_uuid
);
595 ret
= LTTCOMM_NO_SESS
;
602 case LTTNG_LIST_TRACES
:
604 unsigned int trace_count
;
606 trace_count
= get_trace_count_per_session(cmd_ctx
->session
);
607 if (trace_count
== 0) {
608 ret
= LTTCOMM_NO_TRACE
;
612 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_trace
) * trace_count
);
617 get_traces_per_session(cmd_ctx
->session
,
618 (struct lttng_trace
*)(cmd_ctx
->llm
->payload
));
623 case UST_CREATE_TRACE
:
625 /* Setup lttng message with no payload */
626 ret
= setup_lttng_msg(cmd_ctx
, 0);
631 ret
= ust_create_trace(cmd_ctx
);
639 unsigned int app_count
;
641 app_count
= get_app_count();
642 DBG("Traceable application count : %d", app_count
);
643 if (app_count
== 0) {
644 ret
= LTTCOMM_NO_APPS
;
648 ret
= setup_lttng_msg(cmd_ctx
, sizeof(pid_t
) * app_count
);
653 get_app_list_pids((pid_t
*)(cmd_ctx
->llm
->payload
));
658 case UST_START_TRACE
:
660 /* Setup lttng message with no payload */
661 ret
= setup_lttng_msg(cmd_ctx
, 0);
666 ret
= ust_start_trace(cmd_ctx
);
674 /* Setup lttng message with no payload */
675 ret
= setup_lttng_msg(cmd_ctx
, 0);
680 ret
= ust_stop_trace(cmd_ctx
);
686 case LTTNG_LIST_SESSIONS
:
688 unsigned int session_count
;
690 session_count
= get_session_count();
691 if (session_count
== 0) {
692 ret
= LTTCOMM_NO_SESS
;
696 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * session_count
);
701 get_lttng_session((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
707 /* Undefined command */
708 ret
= setup_lttng_msg(cmd_ctx
, 0);
717 /* Set return code */
718 cmd_ctx
->llm
->ret_code
= ret
;
723 if (cmd_ctx
->llm
== NULL
) {
724 DBG("Missing llm structure. Allocating one.");
725 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
729 /* Notify client of error */
730 cmd_ctx
->llm
->ret_code
= ret
;
737 * usage function on stderr
739 static void usage(void)
741 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
742 fprintf(stderr
, " -h, --help Display this usage.\n");
743 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
744 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
745 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
746 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
747 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
748 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
749 fprintf(stderr
, " -V, --version Show version number.\n");
750 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
751 fprintf(stderr
, " -q, --quiet No output at all.\n");
752 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
756 * daemon argument parsing
758 static int parse_args(int argc
, char **argv
)
762 static struct option long_options
[] = {
763 { "client-sock", 1, 0, 'c' },
764 { "apps-sock", 1, 0, 'a' },
765 { "kconsumerd-cmd-sock", 1, 0, 0 },
766 { "kconsumerd-err-sock", 1, 0, 0 },
767 { "daemonize", 0, 0, 'd' },
768 { "sig-parent", 0, 0, 'S' },
769 { "help", 0, 0, 'h' },
770 { "group", 1, 0, 'g' },
771 { "version", 0, 0, 'V' },
772 { "quiet", 0, 0, 'q' },
773 { "verbose", 0, 0, 'v' },
778 int option_index
= 0;
779 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
786 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
788 fprintf(stderr
, " with arg %s\n", optarg
);
792 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
795 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
801 opt_tracing_group
= strdup(optarg
);
807 fprintf(stdout
, "%s\n", VERSION
);
813 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
816 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
825 /* Unknown option or other error.
826 * Error is printed by getopt, just return */
837 * Creates the two needed socket by the daemon.
838 * apps_sock - The communication socket for all UST apps.
839 * client_sock - The communication of the cli tool (lttng).
841 static int init_daemon_socket()
846 old_umask
= umask(0);
848 /* Create client tool unix socket */
849 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
850 if (client_sock
< 0) {
851 ERR("Create unix sock failed: %s", client_unix_sock_path
);
856 /* File permission MUST be 660 */
857 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
859 ERR("Set file permissions failed: %s", client_unix_sock_path
);
864 /* Create the application unix socket */
865 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
867 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
872 /* File permission MUST be 666 */
873 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
875 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
886 * check_existing_daemon
888 * Check if the global socket is available.
889 * If yes, error is returned.
891 static int check_existing_daemon()
895 ret
= access(client_unix_sock_path
, F_OK
);
897 ret
= access(apps_unix_sock_path
, F_OK
);
906 * Return pointer to home directory path using
907 * the env variable HOME.
911 static const char *get_home_dir(void)
913 const char *home_path
;
915 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
916 home_path
= default_home_dir
;
925 * Set the tracing group gid onto the client socket.
927 * Race window between mkdir and chown is OK because we are going from
928 * less permissive (root.root) to more permissive (root.tracing).
930 static int set_permissions(void)
935 /* Decide which group name to use */
936 (opt_tracing_group
!= NULL
) ?
937 (grp
= getgrnam(opt_tracing_group
)) :
938 (grp
= getgrnam(default_tracing_group
));
941 ERR("Missing tracing group. Aborting execution.\n");
946 /* Set lttng run dir */
947 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
949 ERR("Unable to set group on " LTTNG_RUNDIR
);
953 /* lttng client socket path */
954 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
956 ERR("Unable to set group on %s", client_unix_sock_path
);
960 /* kconsumerd error socket path */
961 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
963 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
967 DBG("All permissions are set");
974 * create_lttng_rundir
976 * Create the lttng run directory needed for all
977 * global sockets and pipe.
979 static int create_lttng_rundir(void)
983 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
985 ERR("Unable to create " LTTNG_RUNDIR
);
996 * Setup necessary data for kernel tracer action.
998 static void init_kernel_tracer(void)
1000 /* Set the global kernel tracer fd */
1001 kernel_tracer_fd
= open(DEFAULT_KERNEL_TRACER_PATH
, O_RDWR
);
1002 if (kernel_tracer_fd
< 0) {
1003 WARN("No kernel tracer available");
1004 kernel_tracer_fd
= 0;
1009 * set_kconsumerd_sockets
1011 * Setup sockets and directory needed by the kconsumerd
1012 * communication with the session daemon.
1014 static int set_kconsumerd_sockets(void)
1018 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
1019 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
1022 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
1023 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
1026 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
1028 ERR("Failed to create " KCONSUMERD_PATH
);
1032 /* Create the kconsumerd error unix socket */
1033 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
1034 if (kconsumerd_err_sock
< 0) {
1035 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
1040 /* File permission MUST be 660 */
1041 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1043 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
1053 * set_signal_handler
1055 * Setup signal handler for :
1056 * SIGINT, SIGTERM, SIGPIPE
1058 static int set_signal_handler(void)
1061 struct sigaction sa
;
1064 if ((ret
= sigemptyset(&sigset
)) < 0) {
1065 perror("sigemptyset");
1069 sa
.sa_handler
= sighandler
;
1070 sa
.sa_mask
= sigset
;
1072 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1073 perror("sigaction");
1077 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1078 perror("sigaction");
1082 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1083 perror("sigaction");
1087 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
1095 * Signal handler for the daemon
1097 static void sighandler(int sig
)
1101 DBG("SIGPIPE catched");
1104 DBG("SIGINT catched");
1108 DBG("SIGTERM catched");
1121 * Cleanup the daemon on exit
1123 static void cleanup()
1131 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
1132 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
1135 unlink(client_unix_sock_path
);
1136 unlink(apps_unix_sock_path
);
1137 unlink(kconsumerd_err_unix_sock_path
);
1139 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
1141 ERR("asprintf failed. Something is really wrong!");
1144 /* Remove lttng run directory */
1147 ERR("Unable to clean " LTTNG_RUNDIR
);
1150 // TODO Clean kernel trace fds
1151 close(kernel_tracer_fd
);
1157 int main(int argc
, char **argv
)
1162 pthread_t threads
[2];
1164 /* Parse arguments */
1166 if ((ret
= parse_args(argc
, argv
) < 0)) {
1179 /* Check if daemon is UID = 0 */
1180 is_root
= !getuid();
1182 /* Set all sockets path */
1184 ret
= create_lttng_rundir();
1189 if (strlen(apps_unix_sock_path
) == 0) {
1190 snprintf(apps_unix_sock_path
, PATH_MAX
,
1191 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
1194 if (strlen(client_unix_sock_path
) == 0) {
1195 snprintf(client_unix_sock_path
, PATH_MAX
,
1196 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
1199 ret
= set_kconsumerd_sockets();
1204 /* Setup kernel tracer */
1205 init_kernel_tracer();
1207 if (strlen(apps_unix_sock_path
) == 0) {
1208 snprintf(apps_unix_sock_path
, PATH_MAX
,
1209 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir());
1212 /* Set the cli tool unix socket path */
1213 if (strlen(client_unix_sock_path
) == 0) {
1214 snprintf(client_unix_sock_path
, PATH_MAX
,
1215 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir());
1219 DBG("Client socket path %s", client_unix_sock_path
);
1220 DBG("Application socket path %s", apps_unix_sock_path
);
1222 /* See if daemon already exist. If any of the two
1223 * socket needed by the daemon are present, this test fails
1225 if ((ret
= check_existing_daemon()) == 0) {
1226 ERR("Already running daemon.\n");
1227 /* We do not goto error because we must not
1228 * cleanup() because a daemon is already running.
1233 if (set_signal_handler() < 0) {
1237 /* Setup the needed unix socket */
1238 if (init_daemon_socket() < 0) {
1242 /* Set credentials to socket */
1243 if (is_root
&& (set_permissions() < 0)) {
1247 /* Get parent pid if -S, --sig-parent is specified. */
1248 if (opt_sig_parent
) {
1253 /* Create thread to manage the client socket */
1254 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
1256 perror("pthread_create");
1260 /* Create thread to manage application socket */
1261 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
1263 perror("pthread_create");
1267 for (i
= 0; i
< 2; i
++) {
1268 ret
= pthread_join(threads
[i
], &status
);
1270 perror("pthread_join");