From 42e67be22ec73497c6612f9cd8724580308bb8e8 Mon Sep 17 00:00:00 2001 From: psychon Date: Tue, 24 Mar 2009 12:03:51 +0000 Subject: [PATCH] Add znc -f / --foreground This argument makes znc not fork into the background. It has no effect if configure is called with --enable-debug, znc will always stay in the foreground in this case. The hunk at the end of main.cpp is just whitespace stuff. The only difference in there is that #ifdef _DEBUG #else #endif is changed into if (bForeground) { } else { } (which makes this whitespace stuff necessary). git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1441 726aef4b-f618-498e-8847-2d620e286838 --- main.cpp | 89 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 39 deletions(-) diff --git a/main.cpp b/main.cpp index 13113837..31966db0 100644 --- a/main.cpp +++ b/main.cpp @@ -12,6 +12,7 @@ static struct option g_LongOpts[] = { { "help", no_argument, 0, 'h' }, { "version", no_argument, 0, 'v' }, + { "foreground", no_argument, 0, 'f' }, { "no-color", no_argument, 0, 'n' }, { "allow-root", no_argument, 0, 'r' }, { "makeconf", no_argument, 0, 'c' }, @@ -29,6 +30,7 @@ static void GenerateHelp(const char *appname) { CUtils::PrintMessage("Options are:"); CUtils::PrintMessage("\t-h, --help List available command line options (this page)"); CUtils::PrintMessage("\t-v, --version Output version information and exit"); + CUtils::PrintMessage("\t-f, --foreground Don't fork into the background"); CUtils::PrintMessage("\t-n, --no-color Don't use escape sequences in the output"); CUtils::PrintMessage("\t-r, --allow-root Don't complain if ZNC is run as root"); CUtils::PrintMessage("\t-c, --makeconf Interactively create a new config"); @@ -43,8 +45,8 @@ static void GenerateHelp(const char *appname) { static void die(int sig) { signal(SIGPIPE, SIG_DFL); -#ifdef _DEBUG CUtils::PrintMessage("Exiting on SIG [" + CString(sig) + "]"); +#ifdef _DEBUG if ((sig == SIGABRT) || (sig == SIGSEGV)) { abort(); } @@ -78,13 +80,19 @@ int main(int argc, char** argv) { bool bMakeConf = false; bool bMakePass = false; bool bAllowRoot = false; + bool bForeground = +#ifdef _DEBUG + true; +#else + false; +#endif #ifdef HAVE_LIBSSL bool bMakePem = false; bool bEncPem = false; - while ((iArg = getopt_long(argc, argv, "hvnrcsped:", g_LongOpts, &iOptIndex)) != -1) { + while ((iArg = getopt_long(argc, argv, "hvnrcsped:f", g_LongOpts, &iOptIndex)) != -1) { #else - while ((iArg = getopt_long(argc, argv, "hvnrcsd:", g_LongOpts, &iOptIndex)) != -1) { + while ((iArg = getopt_long(argc, argv, "hvnrcsd:f", g_LongOpts, &iOptIndex)) != -1) { #endif /* HAVE_LIBSSL */ switch (iArg) { case 'h': @@ -116,6 +124,9 @@ int main(int argc, char** argv) { case 'd': sDataDir = CString(optarg); break; + case 'f': + bForeground = true; + break; case '?': default: GenerateHelp(argv[0]); @@ -190,47 +201,47 @@ int main(int argc, char** argv) { sleep(30); } -#ifdef _DEBUG - int iPid = getpid(); - CUtils::PrintMessage("Staying open for debugging [pid: " + CString(iPid) + "]"); - - pZNC->WritePidFile(iPid); - CUtils::PrintMessage(CZNC::GetTag()); -#else - CUtils::PrintAction("Forking into the background"); - - int iPid = fork(); - - if (iPid == -1) { - CUtils::PrintStatus(false, strerror(errno)); - delete pZNC; - return 1; - } - - if (iPid > 0) { - // We are the parent. We are done and will go to bed. - CUtils::PrintStatus(true, "[pid: " + CString(iPid) + "]"); + if (bForeground) { + int iPid = getpid(); + CUtils::PrintMessage("Staying open for debugging [pid: " + CString(iPid) + "]"); pZNC->WritePidFile(iPid); CUtils::PrintMessage(CZNC::GetTag()); - /* Don't destroy pZNC here or it will delete the pid file. */ - return 0; + } else { + CUtils::PrintAction("Forking into the background"); + + int iPid = fork(); + + if (iPid == -1) { + CUtils::PrintStatus(false, strerror(errno)); + delete pZNC; + return 1; + } + + if (iPid > 0) { + // We are the parent. We are done and will go to bed. + CUtils::PrintStatus(true, "[pid: " + CString(iPid) + "]"); + + pZNC->WritePidFile(iPid); + CUtils::PrintMessage(CZNC::GetTag()); + /* Don't destroy pZNC here or it will delete the pid file. */ + return 0; + } + + // Redirect std in/out/err to /dev/null + close(0); open("/dev/null", O_RDONLY); + close(1); open("/dev/null", O_WRONLY); + close(2); open("/dev/null", O_WRONLY); + + CUtils::SetStdoutIsTTY(false); + + // We are the child. There is no way we can be a process group + // leader, thus setsid() must succeed. + setsid(); + // Now we are in our own process group and session (no controlling + // terminal). We are independent! } - // Redirect std in/out/err to /dev/null - close(0); open("/dev/null", O_RDONLY); - close(1); open("/dev/null", O_WRONLY); - close(2); open("/dev/null", O_WRONLY); - - CUtils::SetStdoutIsTTY(false); - - // We are the child. There is no way we can be a process group - // leader, thus setsid() must succeed. - setsid(); - // Now we are in our own process group and session (no controlling - // terminal). We are independent! -#endif - struct sigaction sa; sa.sa_flags = 0; sigemptyset(&sa.sa_mask);