mirror of
https://github.com/znc/znc.git
synced 2026-05-02 19:42:32 +02:00
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
This commit is contained in:
89
main.cpp
89
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);
|
||||
|
||||
Reference in New Issue
Block a user