ircd startup: avoid black magic with file descriptors

This *should* fix a reported but as yet unreproducable
ircd abort on restart.
This commit is contained in:
Aaron Jones 2016-08-20 21:14:53 +00:00
parent ffedad8dfb
commit 27c0f6d8f4
No known key found for this signature in database
GPG key ID: EC6F86EE9CD840B5

View file

@ -66,6 +66,10 @@
#include "authproc.h" #include "authproc.h"
#include "operhash.h" #include "operhash.h"
#ifndef _WIN32
static int pip[2];
#endif
static void static void
ircd_die_cb(const char *str) __attribute__((noreturn)); ircd_die_cb(const char *str) __attribute__((noreturn));
@ -193,25 +197,23 @@ ircd_shutdown(const char *reason)
static void static void
print_startup(int pid) print_startup(int pid)
{ {
int fd; inotice("now running in %s mode from %s as pid %d ...",
!server_state_foreground ? "background" : "foreground",
ConfigFileEntry.dpath, pid);
#ifndef _WIN32 #ifndef _WIN32
close(1); int fd = open("/dev/null", O_RDWR);
fd = open("/dev/null", O_RDWR);
if (fd == -1) { if (fd == -1) {
perror("open /dev/null"); perror("open /dev/null");
exit(EXIT_FAILURE); 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 (void) dup2(fd, 0);
(void) dup2(fd, 1);
(void) dup2(fd, 2);
(void) close(fd);
/* let the parent process know the initialization was successful /* let the parent process know the initialization was successful
* -- jilles */ * -- jilles */
if (!server_state_foreground) if (!server_state_foreground)
@ -221,13 +223,11 @@ print_startup(int pid)
* No, casting to void is of no use to shut the warning up. You HAVE to use the value. * No, casting to void is of no use to shut the warning up. You HAVE to use the value.
* --Elizfaox * --Elizfaox
*/ */
if(write(0, ".", 1) < 1) if(write(pip[0], ".", 1) < 1)
abort(); abort();
(void) close(pip[0]);
} }
if (dup2(1, 0) == -1)
abort();
if (dup2(1, 2) == -1)
abort();
#endif #endif
} }
@ -264,7 +264,6 @@ make_daemon(void)
{ {
#ifndef _WIN32 #ifndef _WIN32
int pid; int pid;
int pip[2];
char c; char c;
if (pipe(pip) < 0) if (pipe(pip) < 0)
@ -272,8 +271,7 @@ make_daemon(void)
perror("pipe"); perror("pipe");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
dup2(pip[1], 0);
close(pip[1]);
if((pid = fork()) < 0) if((pid = fork()) < 0)
{ {
perror("fork"); perror("fork");
@ -281,7 +279,7 @@ make_daemon(void)
} }
else if(pid > 0) else if(pid > 0)
{ {
close(0); (void) close(pip[1]);
/* Wait for initialization to finish, successfully or /* Wait for initialization to finish, successfully or
* unsuccessfully. Until this point the child may still * unsuccessfully. Until this point the child may still
* write to stdout/stderr. * write to stdout/stderr.
@ -292,11 +290,8 @@ make_daemon(void)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
close(pip[0]); (void) close(pip[0]);
setsid(); (void) setsid();
/* fclose(stdin);
fclose(stdout);
fclose(stderr); */
#endif #endif
return 0; return 0;
} }
@ -752,18 +747,6 @@ charybdis_main(int argc, char * const argv[])
if (testing_conf) if (testing_conf)
server_state_foreground = true; server_state_foreground = true;
#ifndef _WIN32
/* Make sure fd 0, 1 and 2 are in use -- jilles */
do
{
fd = open("/dev/null", O_RDWR);
} while (fd < 2 && fd != -1);
if (fd > 2)
close(fd);
else if (fd == -1)
exit(1);
#endif
/* Check if there is pidfile and daemon already running */ /* Check if there is pidfile and daemon already running */
if(!testing_conf) if(!testing_conf)
{ {