From f0092fef4eaf31fee44693a01678313d8a2f4f61 Mon Sep 17 00:00:00 2001 From: Bram Matthys Date: Sat, 21 Apr 2018 20:27:53 +0200 Subject: [PATCH] Properly report failure of fork(). Reported by mbw (#5087). --- src/ircd.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/ircd.c b/src/ircd.c index 3658a6875..830c10a1c 100644 --- a/src/ircd.c +++ b/src/ircd.c @@ -1408,9 +1408,21 @@ int InitUnrealIRCd(int argc, char *argv[]) #if !defined(_AMIGA) && !defined(_WIN32) && !defined(NO_FORKING) if (!(bootopt & BOOT_NOFORK)) { - close_std_descriptors(); - if (fork()) + pid_t p; + p = fork(); + if (p < 0) + { + fprintf(stderr, "Could not create background job. Call to fork() failed: %s\n", + strerror(errno)); + exit(-1); + } + if (p > 0) + { + /* Background job created and we are the parent. We can terminate. */ exit(0); + } + /* Background process (child) continues below... */ + close_std_descriptors(); fd_fork(); loop.ircd_forked = 1; }