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"
45 #include "traceable-app.h"
48 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
49 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
50 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
51 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
53 /* Static functions */
54 static int check_existing_daemon(void);
55 static int connect_app(pid_t pid
);
56 static int init_daemon_socket(void);
57 static int notify_apps(const char* name
);
58 static int process_client_msg(int sock
, struct lttcomm_session_msg
*);
59 static int send_unix_sock(int sock
, void *buf
, size_t len
);
60 static int set_signal_handler(void);
61 static int set_permissions(void);
62 static int setup_data_buffer(char **buf
, size_t size
, struct lttcomm_lttng_header
*llh
);
63 static int create_lttng_rundir(void);
64 static int set_kconsumerd_sockets(void);
65 static void cleanup(void);
66 static void copy_common_data(struct lttcomm_lttng_header
*llh
, struct lttcomm_session_msg
*lsm
);
67 static void sighandler(int sig
);
69 static void *thread_manage_clients(void *data
);
70 static void *thread_manage_apps(void *data
);
71 static void *thread_manage_kconsumerd(void *data
);
77 const char *opt_tracing_group
;
78 static int opt_sig_parent
;
79 static int opt_daemon
;
80 static int is_root
; /* Set to 1 if the daemon is running as root */
83 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
84 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
85 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
86 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
88 static int client_sock
;
90 static int kconsumerd_err_sock
;
92 static struct ltt_session
*current_session
;
95 * thread_manage_kconsumerd
97 * This thread manage the kconsumerd error sent
98 * back to the session daemon.
100 static void *thread_manage_kconsumerd(void *data
)
104 DBG("[thread] Manage kconsumerd started");
106 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
111 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
117 //ret = lttcomm_recv_unix_sock(sock, &lsm, sizeof(lsm));
119 /* TODO: Consumerd died? */
131 * This thread manage the application socket communication
133 static void *thread_manage_apps(void *data
)
137 /* TODO: Something more elegant is needed but fine for now */
139 int reg
; /* 1:register, 0:unregister */
144 DBG("[thread] Manage apps started");
146 /* Notify all applications to register */
147 notify_apps(default_global_apps_pipe
);
149 ret
= lttcomm_listen_unix_sock(apps_sock
);
155 /* Blocking call, waiting for transmission */
156 sock
= lttcomm_accept_unix_sock(apps_sock
);
161 /* Basic recv here to handle the very simple data
162 * that the libust send to register (reg_msg).
164 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
170 /* Add application to the global traceable list */
171 if (reg_msg
.reg
== 1) {
173 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
175 /* register_traceable_app only return an error with
176 * ENOMEM. At this point, we better stop everything.
182 unregister_traceable_app(reg_msg
.pid
);
192 * thread_manage_clients
194 * This thread manage all clients request using the unix
195 * client socket for communication.
197 static void *thread_manage_clients(void *data
)
200 struct lttcomm_session_msg lsm
;
202 DBG("[thread] Manage client started");
204 ret
= lttcomm_listen_unix_sock(client_sock
);
209 /* Notify parent pid that we are ready
210 * to accept command for client side.
212 if (opt_sig_parent
) {
217 /* Blocking call, waiting for transmission */
218 sock
= lttcomm_accept_unix_sock(client_sock
);
224 * Data is received from the lttng client. The struct
225 * lttcomm_session_msg (lsm) contains the command and data
226 * request of the client.
228 ret
= lttcomm_recv_unix_sock(sock
, &lsm
, sizeof(lsm
));
233 /* This function dispatch the work to the LTTng or UST libs
234 * and then sends back the response to the client. This is needed
235 * because there might be more then one lttcomm_lttng_header to
236 * send out so process_client_msg do both jobs.
238 ret
= process_client_msg(sock
, &lsm
);
240 /* Error detected but still accept command */
252 * Send data on a unix socket using the liblttsessiondcomm API.
254 * Return lttcomm error code.
256 static int send_unix_sock(int sock
, void *buf
, size_t len
)
258 /* Check valid length */
263 return lttcomm_send_unix_sock(sock
, buf
, len
);
269 * Return a socket connected to the libust communication socket
270 * of the application identified by the pid.
272 * If the pid is not found in the traceable list,
273 * return -1 to indicate error.
275 static int connect_app(pid_t pid
)
278 struct ltt_traceable_app
*lta
;
280 DBG("Connect to application pid %d", pid
);
282 lta
= find_app_by_pid(pid
);
285 DBG("Application pid %d not found", pid
);
289 sock
= ustctl_connect_pid(lta
->pid
);
291 ERR("Fail connecting to the PID %d\n", pid
);
300 * Notify apps by writing 42 to a named pipe using name.
301 * Every applications waiting for a ltt-sessiond will be notified
302 * and re-register automatically to the session daemon.
304 * Return open or write error value.
306 static int notify_apps(const char *name
)
311 DBG("Notify the global application pipe");
313 /* Try opening the global pipe */
314 fd
= open(name
, O_WRONLY
);
319 /* Notify by writing on the pipe */
320 ret
= write(fd
, "42", 2);
332 * Create an userspace trace using pid.
333 * This trace is then appended to the current session
336 static int ust_create_trace(pid_t pid
)
339 struct ltt_ust_trace
*trace
;
341 DBG("Creating trace for pid %d", pid
);
343 trace
= malloc(sizeof(struct ltt_ust_trace
));
353 /* NOTE: to be removed. Trace name will no longer be
354 * required for LTTng userspace tracer. For now, we set it
355 * to 'auto' for API compliance.
357 snprintf(trace
->name
, 5, "auto");
359 /* Connect to app using ustctl API */
360 sock
= connect_app(pid
);
362 ret
= LTTCOMM_NO_TRACEABLE
;
366 ret
= ustctl_create_trace(sock
, trace
->name
);
368 ret
= LTTCOMM_CREATE_FAIL
;
372 /* Check if current session is valid */
373 if (current_session
) {
374 cds_list_add(&trace
->list
, ¤t_session
->ust_traces
);
375 current_session
->ust_trace_count
++;
385 * Start a trace. This trace, identified by the pid, must be
386 * in the current session ust_traces list.
388 static int ust_start_trace(pid_t pid
)
391 struct ltt_ust_trace
*trace
;
393 DBG("Starting trace for pid %d", pid
);
395 trace
= find_session_ust_trace_by_pid(current_session
, pid
);
397 ret
= LTTCOMM_NO_TRACE
;
401 /* Connect to app using ustctl API */
402 sock
= connect_app(pid
);
404 ret
= LTTCOMM_NO_TRACEABLE
;
408 ret
= ustctl_start_trace(sock
, "auto");
410 ret
= LTTCOMM_START_FAIL
;
421 * Stop a trace. This trace, identified by the pid, must be
422 * in the current session ust_traces list.
424 static int ust_stop_trace(pid_t pid
)
427 struct ltt_ust_trace
*trace
;
429 DBG("Stopping trace for pid %d", pid
);
431 trace
= find_session_ust_trace_by_pid(current_session
, pid
);
433 ret
= LTTCOMM_NO_TRACE
;
437 /* Connect to app using ustctl API */
438 sock
= connect_app(pid
);
440 ret
= LTTCOMM_NO_TRACEABLE
;
444 ret
= ustctl_stop_trace(sock
, trace
->name
);
446 ret
= LTTCOMM_STOP_FAIL
;
457 * Copy common data between lttcomm_lttng_header and lttcomm_session_msg
459 static void copy_common_data(struct lttcomm_lttng_header
*llh
, struct lttcomm_session_msg
*lsm
)
461 llh
->cmd_type
= lsm
->cmd_type
;
465 if (!uuid_is_null(lsm
->session_id
)) {
466 uuid_copy(llh
->session_id
, lsm
->session_id
);
469 strncpy(llh
->trace_name
, lsm
->trace_name
, strlen(llh
->trace_name
));
470 llh
->trace_name
[strlen(llh
->trace_name
) - 1] = '\0';
476 * Setup the outgoing data buffer for the response
477 * data allocating the right amount of memory.
479 * Return total size of the buffer pointed by buf.
481 static int setup_data_buffer(char **buf
, size_t s_data
, struct lttcomm_lttng_header
*llh
)
486 buf_size
= sizeof(struct lttcomm_lttng_header
) + s_data
;
487 *buf
= malloc(buf_size
);
494 /* Setup lttcomm_lttng_header data and copy
495 * it to the newly allocated buffer.
497 llh
->payload_size
= s_data
;
498 memcpy(*buf
, llh
, sizeof(struct lttcomm_lttng_header
));
509 * This takes the lttcomm_session_msg struct and process the command requested
510 * by the client. It then creates response(s) and send it back to the
511 * given socket (sock).
513 * Return any error encountered or 0 for success.
515 static int process_client_msg(int sock
, struct lttcomm_session_msg
*lsm
)
520 char *send_buf
= NULL
;
521 struct lttcomm_lttng_header llh
;
523 DBG("Processing client message");
525 /* Copy common data to identify the response
526 * on the lttng client side.
528 copy_common_data(&llh
, lsm
);
530 /* Check command that needs a session */
531 if (lsm
->cmd_type
!= LTTNG_CREATE_SESSION
&&
532 lsm
->cmd_type
!= LTTNG_LIST_SESSIONS
&&
533 lsm
->cmd_type
!= UST_LIST_APPS
)
535 current_session
= find_session_by_uuid(lsm
->session_id
);
536 if (current_session
== NULL
) {
537 ret
= LTTCOMM_SELECT_SESS
;
542 /* Default return code.
543 * In our world, everything is OK... right? ;)
545 llh
.ret_code
= LTTCOMM_OK
;
547 header_size
= sizeof(struct lttcomm_lttng_header
);
549 /* Process by command type */
550 switch (lsm
->cmd_type
) {
551 case LTTNG_CREATE_SESSION
:
553 ret
= create_session(lsm
->session_name
, &llh
.session_id
);
556 ret
= LTTCOMM_EXIST_SESS
;
563 buf_size
= setup_data_buffer(&send_buf
, 0, &llh
);
571 case LTTNG_DESTROY_SESSION
:
573 ret
= destroy_session(&lsm
->session_id
);
575 ret
= LTTCOMM_NO_SESS
;
580 /* No auxiliary data so only send the llh struct. */
583 case LTTNG_LIST_TRACES
:
585 unsigned int trace_count
= get_trace_count_per_session(current_session
);
587 if (trace_count
== 0) {
588 ret
= LTTCOMM_NO_TRACE
;
592 buf_size
= setup_data_buffer(&send_buf
,
593 sizeof(struct lttng_trace
) * trace_count
, &llh
);
599 get_traces_per_session(current_session
, (struct lttng_trace
*)(send_buf
+ header_size
));
602 case UST_CREATE_TRACE
:
604 ret
= ust_create_trace(lsm
->pid
);
606 /* If -1 is returned from ust_create_trace, malloc
607 * failed so it's pretty much a fatal error.
613 /* No auxiliary data so only send the llh struct. */
618 unsigned int app_count
= get_app_count();
619 /* Stop right now if no apps */
620 if (app_count
== 0) {
621 ret
= LTTCOMM_NO_APPS
;
625 /* Setup data buffer and details for transmission */
626 buf_size
= setup_data_buffer(&send_buf
,
627 sizeof(pid_t
) * app_count
, &llh
);
633 get_app_list_pids((pid_t
*)(send_buf
+ header_size
));
637 case UST_START_TRACE
:
639 ret
= ust_start_trace(lsm
->pid
);
641 /* No auxiliary data so only send the llh struct. */
646 ret
= ust_stop_trace(lsm
->pid
);
648 /* No auxiliary data so only send the llh struct. */
651 case LTTNG_LIST_SESSIONS
:
653 unsigned int session_count
= get_session_count();
654 /* Stop right now if no session */
655 if (session_count
== 0) {
656 ret
= LTTCOMM_NO_SESS
;
660 /* Setup data buffer and details for transmission */
661 buf_size
= setup_data_buffer(&send_buf
,
662 (sizeof(struct lttng_session
) * session_count
), &llh
);
668 get_lttng_session((struct lttng_session
*)(send_buf
+ header_size
));
674 /* Undefined command */
680 ret
= send_unix_sock(sock
, send_buf
, buf_size
);
682 if (send_buf
!= NULL
) {
689 DBG("Return code to client %d", ret
);
690 /* Notify client of error */
692 llh
.payload_size
= 0;
693 send_unix_sock(sock
, (void*) &llh
, sizeof(llh
));
699 * usage function on stderr
701 static void usage(void)
703 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
704 fprintf(stderr
, " -h, --help Display this usage.\n");
705 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
706 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
707 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
708 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
709 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
710 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
711 fprintf(stderr
, " -V, --version Show version number.\n");
712 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
713 fprintf(stderr
, " -q, --quiet No output at all.\n");
714 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
718 * daemon argument parsing
720 static int parse_args(int argc
, char **argv
)
724 static struct option long_options
[] = {
725 { "client-sock", 1, 0, 'c' },
726 { "apps-sock", 1, 0, 'a' },
727 { "kconsumerd-cmd-sock", 1, 0, 0 },
728 { "kconsumerd-err-sock", 1, 0, 0 },
729 { "daemonize", 0, 0, 'd' },
730 { "sig-parent", 0, 0, 'S' },
731 { "help", 0, 0, 'h' },
732 { "group", 1, 0, 'g' },
733 { "version", 0, 0, 'V' },
734 { "quiet", 0, 0, 'q' },
735 { "verbose", 0, 0, 'v' },
740 int option_index
= 0;
741 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
748 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
750 fprintf(stderr
, " with arg %s\n", optarg
);
754 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
757 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
763 opt_tracing_group
= strdup(optarg
);
769 fprintf(stdout
, "%s\n", VERSION
);
775 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
778 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
787 /* Unknown option or other error.
788 * Error is printed by getopt, just return */
799 * Creates the two needed socket by the daemon.
800 * apps_sock - The communication socket for all UST apps.
801 * client_sock - The communication of the cli tool (lttng).
803 static int init_daemon_socket()
808 old_umask
= umask(0);
810 /* Create client tool unix socket */
811 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
812 if (client_sock
< 0) {
813 ERR("Create unix sock failed: %s", client_unix_sock_path
);
818 /* File permission MUST be 660 */
819 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
821 ERR("Set file permissions failed: %s", client_unix_sock_path
);
826 /* Create the application unix socket */
827 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
829 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
834 /* File permission MUST be 666 */
835 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
837 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
848 * check_existing_daemon
850 * Check if the global socket is available.
851 * If yes, error is returned.
853 static int check_existing_daemon()
857 ret
= access(client_unix_sock_path
, F_OK
);
859 ret
= access(apps_unix_sock_path
, F_OK
);
868 * Return pointer to home directory path using
869 * the env variable HOME.
873 static const char *get_home_dir(void)
875 const char *home_path
;
877 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
878 home_path
= default_home_dir
;
887 * Set the tracing group gid onto the client socket.
889 static int set_permissions(void)
894 /* Decide which group name to use */
895 (opt_tracing_group
!= NULL
) ?
896 (grp
= getgrnam(opt_tracing_group
)) :
897 (grp
= getgrnam(default_tracing_group
));
900 ERR("Missing tracing group. Aborting execution.\n");
905 /* Set lttng run dir */
906 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
908 ERR("Unable to set group on " LTTNG_RUNDIR
);
912 /* lttng client socket path */
913 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
915 ERR("Unable to set group on %s", client_unix_sock_path
);
919 /* kconsumerd error socket path */
920 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
922 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
926 DBG("All permissions are set");
933 * create_lttng_rundir
935 * Create the lttng run directory needed for all
936 * global sockets and pipe.
938 static int create_lttng_rundir(void)
942 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
944 ERR("Unable to create " LTTNG_RUNDIR
);
953 * set_kconsumerd_sockets
955 * Setup sockets and directory needed by the kconsumerd
956 * communication with the session daemon.
958 static int set_kconsumerd_sockets(void)
962 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
963 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
966 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
967 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
970 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
972 ERR("Failed to create " KCONSUMERD_PATH
);
976 /* Create the kconsumerd error unix socket */
977 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
978 if (kconsumerd_err_sock
< 0) {
979 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
984 /* File permission MUST be 660 */
985 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
987 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
999 * Setup signal handler for :
1000 * SIGINT, SIGTERM, SIGPIPE
1002 static int set_signal_handler(void)
1005 struct sigaction sa
;
1008 if ((ret
= sigemptyset(&sigset
)) < 0) {
1009 perror("sigemptyset");
1013 sa
.sa_handler
= sighandler
;
1014 sa
.sa_mask
= sigset
;
1016 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1017 perror("sigaction");
1021 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1022 perror("sigaction");
1026 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1027 perror("sigaction");
1031 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
1039 * Signal handler for the daemon
1041 static void sighandler(int sig
)
1045 DBG("SIGPIPE catched");
1048 DBG("SIGINT catched");
1052 DBG("SIGTERM catched");
1065 * Cleanup the daemon on exit
1067 static void cleanup()
1075 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
1076 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
1079 unlink(client_unix_sock_path
);
1080 unlink(apps_unix_sock_path
);
1081 unlink(kconsumerd_err_unix_sock_path
);
1083 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
1085 ERR("asprintf failed. Something is really wrong!");
1088 /* Remove lttng run directory */
1091 ERR("Unable to clean " LTTNG_RUNDIR
);
1098 int main(int argc
, char **argv
)
1103 pthread_t threads
[2];
1105 /* Parse arguments */
1107 if ((ret
= parse_args(argc
, argv
) < 0)) {
1120 /* Check if daemon is UID = 0 */
1121 is_root
= !getuid();
1123 /* Set all sockets path */
1125 ret
= create_lttng_rundir();
1130 if (strlen(apps_unix_sock_path
) == 0) {
1131 snprintf(apps_unix_sock_path
, PATH_MAX
,
1132 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
1135 if (strlen(client_unix_sock_path
) == 0) {
1136 snprintf(client_unix_sock_path
, PATH_MAX
,
1137 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
1140 ret
= set_kconsumerd_sockets();
1145 if (strlen(apps_unix_sock_path
) == 0) {
1146 snprintf(apps_unix_sock_path
, PATH_MAX
,
1147 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir());
1150 /* Set the cli tool unix socket path */
1151 if (strlen(client_unix_sock_path
) == 0) {
1152 snprintf(client_unix_sock_path
, PATH_MAX
,
1153 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir());
1157 /* See if daemon already exist. If any of the two
1158 * socket needed by the daemon are present, this test fails
1160 if ((ret
= check_existing_daemon()) == 0) {
1161 ERR("Already running daemon.\n");
1162 /* We do not goto error because we must not
1163 * cleanup() because a daemon is already running.
1165 return EXIT_FAILURE
;
1168 if (set_signal_handler() < 0) {
1172 /* Setup the needed unix socket */
1173 if (init_daemon_socket() < 0) {
1177 /* Set credentials to socket */
1178 if (is_root
&& (set_permissions() < 0)) {
1182 /* Get parent pid if -S, --sig-parent is specified. */
1183 if (opt_sig_parent
) {
1188 /* Create thread to manage the client socket */
1189 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
1191 perror("pthread_create");
1195 /* Create thread to manage application socket */
1196 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
1198 perror("pthread_create");
1202 for (i
= 0; i
< 2; i
++) {
1203 ret
= pthread_join(threads
[i
], &status
);
1205 perror("pthread_join");
1217 return EXIT_FAILURE
;