Attempt #2 at fixing the file descriptor mess.
This commit defers daemonisation to the end of initialisation as that makes it vastly simpler to get this right.
This commit is contained in:
parent
ef24ede3e2
commit
0c433865d3
1 changed files with 35 additions and 75 deletions
110
ircd/ircd.c
110
ircd/ircd.c
|
@ -187,50 +187,6 @@ ircd_shutdown(const char *reason)
|
|||
exit(0);
|
||||
}
|
||||
|
||||
/*
|
||||
* print_startup - print startup information
|
||||
*/
|
||||
static void
|
||||
print_startup(int pid)
|
||||
{
|
||||
int fd;
|
||||
|
||||
#ifndef _WIN32
|
||||
close(1);
|
||||
fd = open("/dev/null", O_RDWR);
|
||||
if (fd == -1) {
|
||||
perror("open /dev/null");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (fd == 0)
|
||||
fd = dup(fd);
|
||||
if (fd != 1)
|
||||
abort();
|
||||
#endif
|
||||
inotice("now running in %s mode from %s as pid %d ...",
|
||||
!server_state_foreground ? "background" : "foreground",
|
||||
ConfigFileEntry.dpath, pid);
|
||||
|
||||
#ifndef _WIN32
|
||||
/* let the parent process know the initialization was successful
|
||||
* -- jilles */
|
||||
if (!server_state_foreground)
|
||||
{
|
||||
/* GCC complains on Linux if we don't check the value of write pedantically.
|
||||
* Technically you're supposed to check the value, yes, but it probably can't fail.
|
||||
* No, casting to void is of no use to shut the warning up. You HAVE to use the value.
|
||||
* --Elizfaox
|
||||
*/
|
||||
if(write(0, ".", 1) < 1)
|
||||
abort();
|
||||
}
|
||||
if (dup2(1, 0) == -1)
|
||||
abort();
|
||||
if (dup2(1, 2) == -1)
|
||||
abort();
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* init_sys
|
||||
*
|
||||
|
@ -259,21 +215,16 @@ init_sys(void)
|
|||
maxconnections = MAXCONNECTIONS;
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
static int
|
||||
make_daemon(void)
|
||||
{
|
||||
#ifndef _WIN32
|
||||
int pid;
|
||||
int pip[2];
|
||||
char c;
|
||||
int pid, nullfd, fdx;
|
||||
|
||||
/* The below is approximately what daemon(1, 0) does, but
|
||||
we need control over the parent after forking to print
|
||||
the startup message -- Aaron */
|
||||
|
||||
if (pipe(pip) < 0)
|
||||
{
|
||||
perror("pipe");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
dup2(pip[1], 0);
|
||||
close(pip[1]);
|
||||
if((pid = fork()) < 0)
|
||||
{
|
||||
perror("fork");
|
||||
|
@ -281,25 +232,30 @@ make_daemon(void)
|
|||
}
|
||||
else if(pid > 0)
|
||||
{
|
||||
close(0);
|
||||
/* Wait for initialization to finish, successfully or
|
||||
* unsuccessfully. Until this point the child may still
|
||||
* write to stdout/stderr.
|
||||
* -- jilles */
|
||||
if (read(pip[0], &c, 1) > 0)
|
||||
exit(EXIT_SUCCESS);
|
||||
else
|
||||
exit(EXIT_FAILURE);
|
||||
inotice("now running in background mode from %s as pid %d ...",
|
||||
ConfigFileEntry.dpath, pid);
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
close(pip[0]);
|
||||
setsid();
|
||||
/* fclose(stdin);
|
||||
fclose(stdout);
|
||||
fclose(stderr); */
|
||||
#endif
|
||||
if((nullfd = open("/dev/null", O_RDWR)) < 0)
|
||||
{
|
||||
perror("open /dev/null");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for(fdx = 0; fdx <= 2; fdx++)
|
||||
if (fdx != nullfd)
|
||||
(void) dup2(nullfd, fdx);
|
||||
|
||||
if(nullfd > 2)
|
||||
(void) close(nullfd);
|
||||
|
||||
(void) setsid();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int printVersion = 0;
|
||||
|
||||
|
@ -768,9 +724,6 @@ charybdis_main(int argc, char * const argv[])
|
|||
if(!testing_conf)
|
||||
{
|
||||
check_pidfile(pidFileName);
|
||||
|
||||
if(!server_state_foreground)
|
||||
make_daemon();
|
||||
inotice("starting %s ...", ircd_version);
|
||||
inotice("%s", rb_lib_version());
|
||||
}
|
||||
|
@ -885,7 +838,6 @@ charybdis_main(int argc, char * const argv[])
|
|||
construct_umodebuf();
|
||||
|
||||
check_class();
|
||||
write_pidfile(pidFileName);
|
||||
load_help();
|
||||
open_logfiles();
|
||||
|
||||
|
@ -906,7 +858,15 @@ charybdis_main(int argc, char * const argv[])
|
|||
if(splitmode)
|
||||
check_splitmode_ev = rb_event_add("check_splitmode", check_splitmode, NULL, 5);
|
||||
|
||||
print_startup(getpid());
|
||||
if(server_state_foreground)
|
||||
inotice("now running in foreground mode from %s as pid %d ...",
|
||||
ConfigFileEntry.dpath, getpid());
|
||||
#ifndef _WIN32
|
||||
else
|
||||
make_daemon();
|
||||
#endif
|
||||
|
||||
write_pidfile(pidFileName);
|
||||
|
||||
rb_lib_loop(0);
|
||||
|
||||
|
|
Loading…
Reference in a new issue