]> Git Repo - VerusCoin.git/blob - src/bitcoind.cpp
Merge pull request #2855 from Diapolo/guard_CreatePidFile
[VerusCoin.git] / src / bitcoind.cpp
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6 #include "init.h"
7 #include "bitcoinrpc.h"
8 #include <boost/algorithm/string/predicate.hpp>
9
10 void DetectShutdownThread(boost::thread_group* threadGroup)
11 {
12     bool shutdown = ShutdownRequested();
13     // Tell the main threads to shutdown.
14     while (!shutdown)
15     {
16         MilliSleep(200);
17         shutdown = ShutdownRequested();
18     }
19     if (threadGroup)
20         threadGroup->interrupt_all();
21 }
22
23 //////////////////////////////////////////////////////////////////////////////
24 //
25 // Start
26 //
27 bool AppInit(int argc, char* argv[])
28 {
29     boost::thread_group threadGroup;
30     boost::thread* detectShutdownThread = NULL;
31
32     bool fRet = false;
33     try
34     {
35         //
36         // Parameters
37         //
38         // If Qt is used, parameters/bitcoin.conf are parsed in qt/bitcoin.cpp's main()
39         ParseParameters(argc, argv);
40         if (!boost::filesystem::is_directory(GetDataDir(false)))
41         {
42             fprintf(stderr, "Error: Specified directory does not exist\n");
43             Shutdown();
44         }
45         ReadConfigFile(mapArgs, mapMultiArgs);
46
47         if (mapArgs.count("-?") || mapArgs.count("--help"))
48         {
49             // First part of help message is specific to bitcoind / RPC client
50             std::string strUsage = _("Bitcoin version") + " " + FormatFullVersion() + "\n\n" +
51                 _("Usage:") + "\n" +
52                   "  bitcoind [options]                     " + "\n" +
53                   "  bitcoind [options] <command> [params]  " + _("Send command to -server or bitcoind") + "\n" +
54                   "  bitcoind [options] help                " + _("List commands") + "\n" +
55                   "  bitcoind [options] help <command>      " + _("Get help for a command") + "\n";
56
57             strUsage += "\n" + HelpMessage();
58
59             fprintf(stdout, "%s", strUsage.c_str());
60             return false;
61         }
62
63         // Command-line RPC
64         for (int i = 1; i < argc; i++)
65             if (!IsSwitchChar(argv[i][0]) && !boost::algorithm::istarts_with(argv[i], "bitcoin:"))
66                 fCommandLine = true;
67
68         if (fCommandLine)
69         {
70             if (!SelectParamsFromCommandLine()) {
71                 fprintf(stderr, "Error: invalid combination of -regtest and -testnet.\n");
72                 return false;
73             }
74             int ret = CommandLineRPC(argc, argv);
75             exit(ret);
76         }
77 #ifndef WIN32
78         fDaemon = GetBoolArg("-daemon", false);
79         if (fDaemon)
80         {
81             // Daemonize
82             pid_t pid = fork();
83             if (pid < 0)
84             {
85                 fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
86                 return false;
87             }
88             if (pid > 0) // Parent process, pid is child process id
89             {
90                 CreatePidFile(GetPidFile(), pid);
91                 return true;
92             }
93             // Child process falls through to rest of initialization
94
95             pid_t sid = setsid();
96             if (sid < 0)
97                 fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
98         }
99 #endif
100
101         detectShutdownThread = new boost::thread(boost::bind(&DetectShutdownThread, &threadGroup));
102         fRet = AppInit2(threadGroup);
103     }
104     catch (std::exception& e) {
105         PrintExceptionContinue(&e, "AppInit()");
106     } catch (...) {
107         PrintExceptionContinue(NULL, "AppInit()");
108     }
109     if (!fRet) {
110         if (detectShutdownThread)
111             detectShutdownThread->interrupt();
112         threadGroup.interrupt_all();
113     }
114
115     if (detectShutdownThread)
116     {
117         detectShutdownThread->join();
118         delete detectShutdownThread;
119         detectShutdownThread = NULL;
120     }
121     Shutdown();
122
123     return fRet;
124 }
125
126 extern void noui_connect();
127 int main(int argc, char* argv[])
128 {
129     bool fRet = false;
130     fHaveGUI = false;
131
132     // Connect bitcoind signal handlers
133     noui_connect();
134
135     fRet = AppInit(argc, argv);
136
137     if (fRet && fDaemon)
138         return 0;
139
140     return (fRet ? 0 : 1);
141 }
This page took 0.031525 seconds and 4 git commands to generate.