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 "operhash.h"
#ifndef _WIN32
static int pip[2];
#endif
static void
ircd_die_cb(const char *str) __attribute__((noreturn));
@ -193,25 +197,23 @@ ircd_shutdown(const char *reason)
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
int fd = open("/dev/null", O_RDWR);
if (fd == -1) {
perror("open /dev/null");
exit(EXIT_FAILURE);
}
(void) dup2(fd, 0);
(void) dup2(fd, 1);
(void) dup2(fd, 2);
(void) close(fd);
/* let the parent process know the initialization was successful
* -- jilles */
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.
* --Elizfaox
*/
if(write(0, ".", 1) < 1)
if(write(pip[0], ".", 1) < 1)
abort();
(void) close(pip[0]);
}
if (dup2(1, 0) == -1)
abort();
if (dup2(1, 2) == -1)
abort();
#endif
}
@ -264,7 +264,6 @@ make_daemon(void)
{
#ifndef _WIN32
int pid;
int pip[2];
char c;
if (pipe(pip) < 0)
@ -272,8 +271,7 @@ make_daemon(void)
perror("pipe");
exit(EXIT_FAILURE);
}
dup2(pip[1], 0);
close(pip[1]);
if((pid = fork()) < 0)
{
perror("fork");
@ -281,7 +279,7 @@ make_daemon(void)
}
else if(pid > 0)
{
close(0);
(void) close(pip[1]);
/* Wait for initialization to finish, successfully or
* unsuccessfully. Until this point the child may still
* write to stdout/stderr.
@ -292,11 +290,8 @@ make_daemon(void)
exit(EXIT_FAILURE);
}
close(pip[0]);
setsid();
/* fclose(stdin);
fclose(stdout);
fclose(stderr); */
(void) close(pip[0]);
(void) setsid();
#endif
return 0;
}
@ -752,18 +747,6 @@ charybdis_main(int argc, char * const argv[])
if (testing_conf)
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 */
if(!testing_conf)
{