From dda48ccd0f7c7dd13486a580880390a43382d340 Mon Sep 17 00:00:00 2001 From: tcatm Date: Thu, 3 Mar 2011 22:26:00 +0100 Subject: [PATCH 1/4] fix -daemon switch --- init.cpp | 64 ++++++++++++++++++++++++++++++++------------------------ util.cpp | 1 + util.h | 1 + 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/init.cpp b/init.cpp index 9c84dca1..a9b7a7d2 100644 --- a/init.cpp +++ b/init.cpp @@ -74,32 +74,11 @@ void HandleSIGTERM(int) #ifndef GUI int main(int argc, char* argv[]) { - for (int i = 1; i < argc; i++) - if (!IsSwitchChar(argv[i][0])) - fCommandLine = true; - fDaemon = !fCommandLine; - -#ifdef __WXGTK__ - if (!fCommandLine) - { - // Daemonize - pid_t pid = fork(); - if (pid < 0) - { - fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno); - return 1; - } - if (pid > 0) - pthread_exit((void*)0); - } -#endif - - if (!AppInit(argc, argv)) - return 1; + bool fRet = false; + fRet = AppInit(argc, argv); - while (!fShutdown) - Sleep(1000000); - return 0; + if (fRet && fDaemon) + pthread_exit((void*)0); } #endif @@ -113,7 +92,7 @@ bool AppInit(int argc, char* argv[]) catch (std::exception& e) { PrintException(&e, "AppInit()"); } catch (...) { - PrintException(NULL, "AppInit()"); + PrintException(NULL, "AppInit()"); } if (!fRet) Shutdown(NULL); @@ -213,6 +192,18 @@ bool AppInit2(int argc, char* argv[]) fDebug = GetBoolArg("-debug"); + fDaemon = GetBoolArg("-daemon"); + + if (fDaemon) + fServer = true; + else + fServer = GetBoolArg("-server"); + + /* force fServer when running without GUI */ +#ifndef GUI + fServer = true; +#endif + fPrintToConsole = GetBoolArg("-printtoconsole"); fPrintToDebugger = GetBoolArg("-printtodebugger"); @@ -220,12 +211,31 @@ bool AppInit2(int argc, char* argv[]) fNoListen = GetBoolArg("-nolisten"); + for (int i = 1; i < argc; i++) + if (!IsSwitchChar(argv[i][0])) + fCommandLine = true; + if (fCommandLine) { int ret = CommandLineRPC(argc, argv); exit(ret); } +#ifndef GUI + if (fDaemon) + { + // Daemonize + pid_t pid = fork(); + if (pid < 0) + { + fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno); + return false; + } + if (pid > 0) + return true; + } +#endif + if (!fDebug && !pszSetDataDir[0]) ShrinkDebugFile(); printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); @@ -443,7 +453,7 @@ bool AppInit2(int argc, char* argv[]) if (!CreateThread(StartNode, NULL)) wxMessageBox("Error: CreateThread(StartNode) failed", "Bitcoin"); - if (GetBoolArg("-server") || fDaemon) + if (fServer) CreateThread(ThreadRPCServer, NULL); #if defined(__WXMSW__) && defined(GUI) diff --git a/util.cpp b/util.cpp index 8a2f9d52..124174bd 100644 --- a/util.cpp +++ b/util.cpp @@ -14,6 +14,7 @@ char pszSetDataDir[MAX_PATH] = ""; bool fRequestShutdown = false; bool fShutdown = false; bool fDaemon = false; +bool fServer = false; bool fCommandLine = false; string strMiscWarning; bool fTestNet = false; diff --git a/util.h b/util.h index c69bf1ce..1b780d55 100644 --- a/util.h +++ b/util.h @@ -143,6 +143,7 @@ extern char pszSetDataDir[MAX_PATH]; extern bool fRequestShutdown; extern bool fShutdown; extern bool fDaemon; +extern bool fServer; extern bool fCommandLine; extern string strMiscWarning; extern bool fTestNet; From a79409afa9c552eb5117d3474ba8dde1a649ce08 Mon Sep 17 00:00:00 2001 From: tcatm Date: Thu, 3 Mar 2011 22:27:20 +0100 Subject: [PATCH 2/4] fix whitespace --- init.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init.cpp b/init.cpp index a9b7a7d2..0548accb 100644 --- a/init.cpp +++ b/init.cpp @@ -92,7 +92,7 @@ bool AppInit(int argc, char* argv[]) catch (std::exception& e) { PrintException(&e, "AppInit()"); } catch (...) { - PrintException(NULL, "AppInit()"); + PrintException(NULL, "AppInit()"); } if (!fRet) Shutdown(NULL); From ea7cd317516a6ec911e6a29bc0407a42a2534251 Mon Sep 17 00:00:00 2001 From: tcatm Date: Thu, 3 Mar 2011 22:31:44 +0100 Subject: [PATCH 3/4] force fDaemon in bitcoind --- init.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/init.cpp b/init.cpp index 0548accb..f3ec28ba 100644 --- a/init.cpp +++ b/init.cpp @@ -199,9 +199,10 @@ bool AppInit2(int argc, char* argv[]) else fServer = GetBoolArg("-server"); - /* force fServer when running without GUI */ + /* force fServer and fDaemon when running without GUI */ #ifndef GUI fServer = true; + fDaemon = true; #endif fPrintToConsole = GetBoolArg("-printtoconsole"); From e1205e4d15403fdbb9b8d081aa7a9c3190529262 Mon Sep 17 00:00:00 2001 From: tcatm Date: Thu, 3 Mar 2011 22:34:13 +0100 Subject: [PATCH 4/4] -help: do not show -server and -daemon in bitcoind --- init.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/init.cpp b/init.cpp index f3ec28ba..ac2721db 100644 --- a/init.cpp +++ b/init.cpp @@ -156,8 +156,10 @@ bool AppInit2(int argc, char* argv[]) " -connect= \t\t " + _("Connect only to the specified node\n") + " -nolisten \t " + _("Don't accept connections from outside\n") + " -paytxfee= \t " + _("Fee per KB to add to transactions you send\n") + +#ifdef GUI " -server \t\t " + _("Accept command line and JSON-RPC commands\n") + " -daemon \t\t " + _("Run in the background as a daemon and accept commands\n") + +#endif " -testnet \t\t " + _("Use the test network\n") + " -rpcuser= \t " + _("Username for JSON-RPC connections\n") + " -rpcpassword=\t " + _("Password for JSON-RPC connections\n") +