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"
49 * teardown: signal SIGTERM handler -> write into pipe. Threads waits
50 * with epoll on pipe and on other pipes/sockets for commands. Main
51 * simply waits on pthread join.
55 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
56 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
57 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
58 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
60 /* Static functions */
61 static int check_existing_daemon(void);
62 static int ust_connect_app(pid_t pid
);
63 static int init_daemon_socket(void);
64 static int notify_apps(const char* name
);
65 static int process_client_msg(struct command_ctx
*cmd_ctx
);
66 static int send_unix_sock(int sock
, void *buf
, size_t len
);
67 static int set_signal_handler(void);
68 static int set_permissions(void);
69 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
);
70 static int create_lttng_rundir(void);
71 static int set_kconsumerd_sockets(void);
72 static void cleanup(void);
73 static void sighandler(int sig
);
74 static void clean_command_ctx(struct command_ctx
*cmd_ctx
);
76 static void *thread_manage_clients(void *data
);
77 static void *thread_manage_apps(void *data
);
78 static void *thread_manage_kconsumerd(void *data
);
84 const char *opt_tracing_group
;
85 static int opt_sig_parent
;
86 static int opt_daemon
;
87 static int is_root
; /* Set to 1 if the daemon is running as root */
90 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
91 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
92 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
93 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
95 static int client_sock
;
97 static int kconsumerd_err_sock
;
100 * thread_manage_kconsumerd
102 * This thread manage the kconsumerd error sent
103 * back to the session daemon.
105 static void *thread_manage_kconsumerd(void *data
)
109 DBG("[thread] Manage kconsumerd started");
111 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
116 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
122 //ret = lttcomm_recv_unix_sock(sock, &lsm, sizeof(lsm));
124 /* TODO: Consumerd died? */
136 * This thread manage the application socket communication
138 static void *thread_manage_apps(void *data
)
142 /* TODO: Something more elegant is needed but fine for now */
143 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
144 * for 32-bit vs 64-bit compat processes. */
145 /* replicate in ust with version number */
147 int reg
; /* 1:register, 0:unregister */
152 DBG("[thread] Manage apps started");
154 ret
= lttcomm_listen_unix_sock(apps_sock
);
159 /* Notify all applications to register */
160 notify_apps(default_global_apps_pipe
);
163 DBG("Accepting application registration");
164 /* Blocking call, waiting for transmission */
165 sock
= lttcomm_accept_unix_sock(apps_sock
);
170 /* Basic recv here to handle the very simple data
171 * that the libust send to register (reg_msg).
173 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
179 /* Add application to the global traceable list */
180 if (reg_msg
.reg
== 1) {
182 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
184 /* register_traceable_app only return an error with
185 * ENOMEM. At this point, we better stop everything.
191 unregister_traceable_app(reg_msg
.pid
);
201 * thread_manage_clients
203 * This thread manage all clients request using the unix
204 * client socket for communication.
206 static void *thread_manage_clients(void *data
)
209 struct command_ctx
*cmd_ctx
;
211 DBG("[thread] Manage client started");
213 ret
= lttcomm_listen_unix_sock(client_sock
);
218 /* Notify parent pid that we are ready
219 * to accept command for client side.
221 if (opt_sig_parent
) {
226 /* Blocking call, waiting for transmission */
227 DBG("Accepting client command ...");
228 sock
= lttcomm_accept_unix_sock(client_sock
);
233 /* Allocate context command to process the client request */
234 cmd_ctx
= malloc(sizeof(struct command_ctx
));
236 /* Allocate data buffer for reception */
237 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
239 cmd_ctx
->session
= NULL
;
242 * Data is received from the lttng client. The struct
243 * lttcomm_session_msg (lsm) contains the command and data request of
246 DBG("Receiving data from client ...");
247 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
253 * This function dispatch the work to the kernel or userspace tracer
254 * libs and fill the lttcomm_lttng_msg data structure of all the needed
255 * informations for the client. The command context struct contains
256 * everything this function may needs.
258 ret
= process_client_msg(cmd_ctx
);
260 /* TODO: Inform client somehow of the fatal error. At this point,
261 * ret < 0 means that a malloc failed (ENOMEM). */
262 /* Error detected but still accept command */
263 clean_command_ctx(cmd_ctx
);
267 DBG("Sending response (size: %d, retcode: %d)",
268 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
269 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
271 ERR("Failed to send data back to client");
274 clean_command_ctx(cmd_ctx
);
284 * Send data on a unix socket using the liblttsessiondcomm API.
286 * Return lttcomm error code.
288 static int send_unix_sock(int sock
, void *buf
, size_t len
)
290 /* Check valid length */
295 return lttcomm_send_unix_sock(sock
, buf
, len
);
301 * Free memory of a command context structure.
303 static void clean_command_ctx(struct command_ctx
*cmd_ctx
)
305 DBG("Clean command context structure %p", cmd_ctx
);
321 * Return a socket connected to the libust communication socket
322 * of the application identified by the pid.
324 * If the pid is not found in the traceable list,
325 * return -1 to indicate error.
327 static int ust_connect_app(pid_t pid
)
330 struct ltt_traceable_app
*lta
;
332 DBG("Connect to application pid %d", pid
);
334 lta
= find_app_by_pid(pid
);
337 DBG("Application pid %d not found", pid
);
341 sock
= ustctl_connect_pid(lta
->pid
);
343 ERR("Fail connecting to the PID %d\n", pid
);
352 * Notify apps by writing 42 to a named pipe using name.
353 * Every applications waiting for a ltt-sessiond will be notified
354 * and re-register automatically to the session daemon.
356 * Return open or write error value.
358 static int notify_apps(const char *name
)
363 DBG("Notify the global application pipe");
365 /* Try opening the global pipe */
366 fd
= open(name
, O_WRONLY
);
371 /* Notify by writing on the pipe */
372 ret
= write(fd
, "42", 2);
384 * Setup the outgoing data buffer for the response (llm) by allocating the
385 * right amount of memory and copying the original information from the lsm
388 * Return total size of the buffer pointed by buf.
390 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
392 int ret
, buf_size
, trace_name_size
;
395 * Check for the trace_name. If defined, it's part of the payload data of
398 trace_name_size
= strlen(cmd_ctx
->lsm
->trace_name
);
399 buf_size
= trace_name_size
+ size
;
401 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
402 if (cmd_ctx
->llm
== NULL
) {
408 /* Copy common data */
409 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
410 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->pid
;
411 if (!uuid_is_null(cmd_ctx
->lsm
->session_uuid
)) {
412 uuid_copy(cmd_ctx
->llm
->session_uuid
, cmd_ctx
->lsm
->session_uuid
);
415 cmd_ctx
->llm
->trace_name_offset
= trace_name_size
;
416 cmd_ctx
->llm
->data_size
= size
;
417 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
419 /* Copy trace name to the llm structure. Begining of the payload. */
420 memcpy(cmd_ctx
->llm
->payload
, cmd_ctx
->lsm
->trace_name
, trace_name_size
);
431 * Process the command requested by the lttng client within the command
432 * context structure. This function make sure that the return structure (llm)
433 * is set and ready for transmission before returning.
435 * Return any error encountered or 0 for success.
437 static int process_client_msg(struct command_ctx
*cmd_ctx
)
441 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
443 /* Check command that needs a session */
444 switch (cmd_ctx
->lsm
->cmd_type
) {
445 case LTTNG_CREATE_SESSION
:
446 case LTTNG_LIST_SESSIONS
:
450 cmd_ctx
->session
= find_session_by_uuid(cmd_ctx
->lsm
->session_uuid
);
451 if (cmd_ctx
->session
== NULL
) {
452 ret
= LTTCOMM_SELECT_SESS
;
458 /* Connect to ust apps if available pid */
459 if (cmd_ctx
->lsm
->pid
> 0) {
460 /* Connect to app using ustctl API */
461 cmd_ctx
->ust_sock
= ust_connect_app(cmd_ctx
->lsm
->pid
);
462 if (cmd_ctx
->ust_sock
< 0) {
463 ret
= LTTCOMM_NO_TRACEABLE
;
468 /* Process by command type */
469 switch (cmd_ctx
->lsm
->cmd_type
) {
470 case KERNEL_ENABLE_EVENT
:
472 /* Setup lttng message with no payload */
473 ret
= setup_lttng_msg(cmd_ctx
, 0);
478 DBG("Enabling kernel event %s", cmd_ctx
->lsm
->u
.event
.event_name
);
483 case LTTNG_CREATE_SESSION
:
485 /* Setup lttng message with no payload */
486 ret
= setup_lttng_msg(cmd_ctx
, 0);
491 ret
= create_session(cmd_ctx
->lsm
->session_name
, &cmd_ctx
->llm
->session_uuid
);
494 ret
= LTTCOMM_EXIST_SESS
;
504 case LTTNG_DESTROY_SESSION
:
506 /* Setup lttng message with no payload */
507 ret
= setup_lttng_msg(cmd_ctx
, 0);
512 ret
= destroy_session(&cmd_ctx
->lsm
->session_uuid
);
514 ret
= LTTCOMM_NO_SESS
;
521 case LTTNG_LIST_TRACES
:
523 unsigned int trace_count
;
525 trace_count
= get_trace_count_per_session(cmd_ctx
->session
);
526 if (trace_count
== 0) {
527 ret
= LTTCOMM_NO_TRACE
;
531 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_trace
) * trace_count
);
536 get_traces_per_session(cmd_ctx
->session
,
537 (struct lttng_trace
*)(cmd_ctx
->llm
->payload
));
542 case UST_CREATE_TRACE
:
544 /* Setup lttng message with no payload */
545 ret
= setup_lttng_msg(cmd_ctx
, 0);
550 ret
= ust_create_trace(cmd_ctx
);
558 unsigned int app_count
;
560 app_count
= get_app_count();
561 DBG("Traceable application count : %d", app_count
);
562 if (app_count
== 0) {
563 ret
= LTTCOMM_NO_APPS
;
567 ret
= setup_lttng_msg(cmd_ctx
, sizeof(pid_t
) * app_count
);
572 get_app_list_pids((pid_t
*)(cmd_ctx
->llm
->payload
));
577 case UST_START_TRACE
:
579 /* Setup lttng message with no payload */
580 ret
= setup_lttng_msg(cmd_ctx
, 0);
585 ret
= ust_start_trace(cmd_ctx
);
593 /* Setup lttng message with no payload */
594 ret
= setup_lttng_msg(cmd_ctx
, 0);
599 ret
= ust_stop_trace(cmd_ctx
);
605 case LTTNG_LIST_SESSIONS
:
607 unsigned int session_count
;
609 session_count
= get_session_count();
610 if (session_count
== 0) {
611 ret
= LTTCOMM_NO_SESS
;
615 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * session_count
);
620 get_lttng_session((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
626 /* Undefined command */
627 ret
= setup_lttng_msg(cmd_ctx
, 0);
636 /* Set return code */
637 cmd_ctx
->llm
->ret_code
= ret
;
642 if (cmd_ctx
->llm
== NULL
) {
643 DBG("Missing llm structure. Allocating one.");
644 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
648 /* Notify client of error */
649 cmd_ctx
->llm
->ret_code
= ret
;
656 * usage function on stderr
658 static void usage(void)
660 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
661 fprintf(stderr
, " -h, --help Display this usage.\n");
662 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
663 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
664 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
665 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
666 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
667 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
668 fprintf(stderr
, " -V, --version Show version number.\n");
669 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
670 fprintf(stderr
, " -q, --quiet No output at all.\n");
671 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
675 * daemon argument parsing
677 static int parse_args(int argc
, char **argv
)
681 static struct option long_options
[] = {
682 { "client-sock", 1, 0, 'c' },
683 { "apps-sock", 1, 0, 'a' },
684 { "kconsumerd-cmd-sock", 1, 0, 0 },
685 { "kconsumerd-err-sock", 1, 0, 0 },
686 { "daemonize", 0, 0, 'd' },
687 { "sig-parent", 0, 0, 'S' },
688 { "help", 0, 0, 'h' },
689 { "group", 1, 0, 'g' },
690 { "version", 0, 0, 'V' },
691 { "quiet", 0, 0, 'q' },
692 { "verbose", 0, 0, 'v' },
697 int option_index
= 0;
698 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
705 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
707 fprintf(stderr
, " with arg %s\n", optarg
);
711 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
714 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
720 opt_tracing_group
= strdup(optarg
);
726 fprintf(stdout
, "%s\n", VERSION
);
732 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
735 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
744 /* Unknown option or other error.
745 * Error is printed by getopt, just return */
756 * Creates the two needed socket by the daemon.
757 * apps_sock - The communication socket for all UST apps.
758 * client_sock - The communication of the cli tool (lttng).
760 static int init_daemon_socket()
765 old_umask
= umask(0);
767 /* Create client tool unix socket */
768 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
769 if (client_sock
< 0) {
770 ERR("Create unix sock failed: %s", client_unix_sock_path
);
775 /* File permission MUST be 660 */
776 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
778 ERR("Set file permissions failed: %s", client_unix_sock_path
);
783 /* Create the application unix socket */
784 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
786 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
791 /* File permission MUST be 666 */
792 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
794 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
805 * check_existing_daemon
807 * Check if the global socket is available.
808 * If yes, error is returned.
810 static int check_existing_daemon()
814 ret
= access(client_unix_sock_path
, F_OK
);
816 ret
= access(apps_unix_sock_path
, F_OK
);
825 * Return pointer to home directory path using
826 * the env variable HOME.
830 static const char *get_home_dir(void)
832 const char *home_path
;
834 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
835 home_path
= default_home_dir
;
844 * Set the tracing group gid onto the client socket.
846 * Race window between mkdir and chown is OK because we are going from
847 * less permissive (root.root) to more permissive (root.tracing).
849 static int set_permissions(void)
854 /* Decide which group name to use */
855 (opt_tracing_group
!= NULL
) ?
856 (grp
= getgrnam(opt_tracing_group
)) :
857 (grp
= getgrnam(default_tracing_group
));
860 ERR("Missing tracing group. Aborting execution.\n");
865 /* Set lttng run dir */
866 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
868 ERR("Unable to set group on " LTTNG_RUNDIR
);
872 /* lttng client socket path */
873 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
875 ERR("Unable to set group on %s", client_unix_sock_path
);
879 /* kconsumerd error socket path */
880 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
882 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
886 DBG("All permissions are set");
893 * create_lttng_rundir
895 * Create the lttng run directory needed for all
896 * global sockets and pipe.
898 static int create_lttng_rundir(void)
902 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
904 ERR("Unable to create " LTTNG_RUNDIR
);
913 * set_kconsumerd_sockets
915 * Setup sockets and directory needed by the kconsumerd
916 * communication with the session daemon.
918 static int set_kconsumerd_sockets(void)
922 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
923 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
926 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
927 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
930 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
932 ERR("Failed to create " KCONSUMERD_PATH
);
936 /* Create the kconsumerd error unix socket */
937 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
938 if (kconsumerd_err_sock
< 0) {
939 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
944 /* File permission MUST be 660 */
945 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
947 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
959 * Setup signal handler for :
960 * SIGINT, SIGTERM, SIGPIPE
962 static int set_signal_handler(void)
968 if ((ret
= sigemptyset(&sigset
)) < 0) {
969 perror("sigemptyset");
973 sa
.sa_handler
= sighandler
;
976 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
981 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
986 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
991 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
999 * Signal handler for the daemon
1001 static void sighandler(int sig
)
1005 DBG("SIGPIPE catched");
1008 DBG("SIGINT catched");
1012 DBG("SIGTERM catched");
1025 * Cleanup the daemon on exit
1027 static void cleanup()
1035 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
1036 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
1039 unlink(client_unix_sock_path
);
1040 unlink(apps_unix_sock_path
);
1041 unlink(kconsumerd_err_unix_sock_path
);
1043 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
1045 ERR("asprintf failed. Something is really wrong!");
1048 /* Remove lttng run directory */
1051 ERR("Unable to clean " LTTNG_RUNDIR
);
1058 int main(int argc
, char **argv
)
1063 pthread_t threads
[2];
1065 /* Parse arguments */
1067 if ((ret
= parse_args(argc
, argv
) < 0)) {
1080 /* Check if daemon is UID = 0 */
1081 is_root
= !getuid();
1083 /* Set all sockets path */
1085 ret
= create_lttng_rundir();
1090 if (strlen(apps_unix_sock_path
) == 0) {
1091 snprintf(apps_unix_sock_path
, PATH_MAX
,
1092 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
1095 if (strlen(client_unix_sock_path
) == 0) {
1096 snprintf(client_unix_sock_path
, PATH_MAX
,
1097 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
1100 ret
= set_kconsumerd_sockets();
1105 if (strlen(apps_unix_sock_path
) == 0) {
1106 snprintf(apps_unix_sock_path
, PATH_MAX
,
1107 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir());
1110 /* Set the cli tool unix socket path */
1111 if (strlen(client_unix_sock_path
) == 0) {
1112 snprintf(client_unix_sock_path
, PATH_MAX
,
1113 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir());
1117 DBG("Client socket path %s", client_unix_sock_path
);
1118 DBG("Application socket path %s", apps_unix_sock_path
);
1120 /* See if daemon already exist. If any of the two
1121 * socket needed by the daemon are present, this test fails
1123 if ((ret
= check_existing_daemon()) == 0) {
1124 ERR("Already running daemon.\n");
1125 /* We do not goto error because we must not
1126 * cleanup() because a daemon is already running.
1131 if (set_signal_handler() < 0) {
1135 /* Setup the needed unix socket */
1136 if (init_daemon_socket() < 0) {
1140 /* Set credentials to socket */
1141 if (is_root
&& (set_permissions() < 0)) {
1145 /* Get parent pid if -S, --sig-parent is specified. */
1146 if (opt_sig_parent
) {
1151 /* Create thread to manage the client socket */
1152 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
1154 perror("pthread_create");
1158 /* Create thread to manage application socket */
1159 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
1161 perror("pthread_create");
1165 for (i
= 0; i
< 2; i
++) {
1166 ret
= pthread_join(threads
[i
], &status
);
1168 perror("pthread_join");