changeset 460:c8fd1ed13260 draft

fix -daemon switch
author tcatm <tcatm@gawab.com>
date Thu, 03 Mar 2011 22:26:00 +0100
parents 39f8c9430b2d
children f9508c3678dc
files init.cpp util.cpp util.h
diffstat 3 files changed, 39 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/init.cpp
+++ b/init.cpp
@@ -74,32 +74,11 @@
 #ifndef GUI
 int main(int argc, char* argv[])
 {
-    for (int i = 1; i < argc; i++)
-        if (!IsSwitchChar(argv[i][0]))
-            fCommandLine = true;
-    fDaemon = !fCommandLine;
+    bool fRet = false;
+    fRet = AppInit(argc, argv);
 
-#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;
-
-    while (!fShutdown)
-        Sleep(1000000);
-    return 0;
+    if (fRet && fDaemon)
+        pthread_exit((void*)0);
 }
 #endif
 
@@ -113,7 +92,7 @@
     catch (std::exception& e) {
         PrintException(&e, "AppInit()");
     } catch (...) {
-        PrintException(NULL, "AppInit()");
+	    PrintException(NULL, "AppInit()");
     }
     if (!fRet)
         Shutdown(NULL);
@@ -213,6 +192,18 @@
 
     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 @@
     
     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 @@
     if (!CreateThread(StartNode, NULL))
         wxMessageBox("Error: CreateThread(StartNode) failed", "Bitcoin");
 
-    if (GetBoolArg("-server") || fDaemon)
+    if (fServer)
         CreateThread(ThreadRPCServer, NULL);
 
 #if defined(__WXMSW__) && defined(GUI)
--- a/util.cpp
+++ b/util.cpp
@@ -14,6 +14,7 @@
 bool fRequestShutdown = false;
 bool fShutdown = false;
 bool fDaemon = false;
+bool fServer = false;
 bool fCommandLine = false;
 string strMiscWarning;
 bool fTestNet = false;
--- a/util.h
+++ b/util.h
@@ -143,6 +143,7 @@
 extern bool fRequestShutdown;
 extern bool fShutdown;
 extern bool fDaemon;
+extern bool fServer;
 extern bool fCommandLine;
 extern string strMiscWarning;
 extern bool fTestNet;