Revert "ircd startup: avoid black magic with file descriptors"

This reverts commit 27c0f6d8f4.

A more extensive investigation and refactoring of the code is
necessary.
This commit is contained in:
Aaron Jones 2016-08-20 22:22:37 +00:00
parent 27c0f6d8f4
commit ef24ede3e2
No known key found for this signature in database
GPG key ID: EC6F86EE9CD840B5

View file

@ -66,10 +66,6 @@
#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));
@ -197,23 +193,25 @@ ircd_shutdown(const char *reason)
static void static void
print_startup(int pid) print_startup(int pid)
{ {
inotice("now running in %s mode from %s as pid %d ...", int fd;
!server_state_foreground ? "background" : "foreground",
ConfigFileEntry.dpath, pid);
#ifndef _WIN32 #ifndef _WIN32
int fd = open("/dev/null", O_RDWR); close(1);
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);
(void) dup2(fd, 0); #ifndef _WIN32
(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)
@ -223,11 +221,13 @@ 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(pip[0], ".", 1) < 1) if(write(0, ".", 1) < 1)
abort(); abort();
(void) close(pip[0]);
} }
if (dup2(1, 0) == -1)
abort();
if (dup2(1, 2) == -1)
abort();
#endif #endif
} }
@ -264,6 +264,7 @@ 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)
@ -271,7 +272,8 @@ 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");
@ -279,7 +281,7 @@ make_daemon(void)
} }
else if(pid > 0) else if(pid > 0)
{ {
(void) close(pip[1]); close(0);
/* 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.
@ -290,8 +292,11 @@ make_daemon(void)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
(void) close(pip[0]); close(pip[0]);
(void) setsid(); setsid();
/* fclose(stdin);
fclose(stdout);
fclose(stderr); */
#endif #endif
return 0; return 0;
} }
@ -747,6 +752,18 @@ 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)
{ {