]> Git Repo - VerusCoin.git/blob - src/bitcoind.cpp
Merge pull request #5207
[VerusCoin.git] / src / bitcoind.cpp
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 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 "clientversion.h"
7 #include "rpcserver.h"
8 #include "init.h"
9 #include "main.h"
10 #include "noui.h"
11 #include "ui_interface.h"
12 #include "util.h"
13
14 #include <boost/algorithm/string/predicate.hpp>
15 #include <boost/filesystem.hpp>
16 #include <boost/thread.hpp>
17
18 /* Introduction text for doxygen: */
19
20 /*! \mainpage Developer documentation
21  *
22  * \section intro_sec Introduction
23  *
24  * This is the developer documentation of the reference client for an experimental new digital currency called Bitcoin (http://www.bitcoin.org/),
25  * which enables instant payments to anyone, anywhere in the world. Bitcoin uses peer-to-peer technology to operate
26  * with no central authority: managing transactions and issuing money are carried out collectively by the network.
27  *
28  * The software is a community-driven open source project, released under the MIT license.
29  *
30  * \section Navigation
31  * Use the buttons <code>Namespaces</code>, <code>Classes</code> or <code>Files</code> at the top of the page to start navigating the code.
32  */
33
34 static bool fDaemon;
35
36 void DetectShutdownThread(boost::thread_group* threadGroup)
37 {
38     bool fShutdown = ShutdownRequested();
39     // Tell the main threads to shutdown.
40     while (!fShutdown)
41     {
42         MilliSleep(200);
43         fShutdown = ShutdownRequested();
44     }
45     if (threadGroup)
46     {
47         threadGroup->interrupt_all();
48         threadGroup->join_all();
49     }
50 }
51
52 //////////////////////////////////////////////////////////////////////////////
53 //
54 // Start
55 //
56 bool AppInit(int argc, char* argv[])
57 {
58     boost::thread_group threadGroup;
59     boost::thread* detectShutdownThread = NULL;
60
61     bool fRet = false;
62
63     //
64     // Parameters
65     //
66     // If Qt is used, parameters/bitcoin.conf are parsed in qt/bitcoin.cpp's main()
67     ParseParameters(argc, argv);
68
69     // Process help and version before taking care about datadir
70     if (mapArgs.count("-?") || mapArgs.count("-help") || mapArgs.count("-version"))
71     {
72         std::string strUsage = _("Bitcoin Core Daemon") + " " + _("version") + " " + FormatFullVersion() + "\n";
73
74         if (mapArgs.count("-version"))
75         {
76             strUsage += LicenseInfo();
77         }
78         else
79         {
80             strUsage += "\n" + _("Usage:") + "\n" +
81                   "  bitcoind [options]                     " + _("Start Bitcoin Core Daemon") + "\n";
82
83             strUsage += "\n" + HelpMessage(HMM_BITCOIND);
84         }
85
86         fprintf(stdout, "%s", strUsage.c_str());
87         return false;
88     }
89
90     try
91     {
92         if (!boost::filesystem::is_directory(GetDataDir(false)))
93         {
94             fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str());
95             return false;
96         }
97         try
98         {
99             ReadConfigFile(mapArgs, mapMultiArgs);
100         } catch(std::exception &e) {
101             fprintf(stderr,"Error reading configuration file: %s\n", e.what());
102             return false;
103         }
104         // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
105         if (!SelectParamsFromCommandLine()) {
106             fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n");
107             return false;
108         }
109
110         // Command-line RPC
111         bool fCommandLine = false;
112         for (int i = 1; i < argc; i++)
113             if (!IsSwitchChar(argv[i][0]) && !boost::algorithm::istarts_with(argv[i], "bitcoin:"))
114                 fCommandLine = true;
115
116         if (fCommandLine)
117         {
118             fprintf(stderr, "Error: There is no RPC client functionality in bitcoind anymore. Use the bitcoin-cli utility instead.\n");
119             exit(1);
120         }
121 #ifndef WIN32
122         fDaemon = GetBoolArg("-daemon", false);
123         if (fDaemon)
124         {
125             fprintf(stdout, "Bitcoin server starting\n");
126
127             // Daemonize
128             pid_t pid = fork();
129             if (pid < 0)
130             {
131                 fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
132                 return false;
133             }
134             if (pid > 0) // Parent process, pid is child process id
135             {
136                 return true;
137             }
138             // Child process falls through to rest of initialization
139
140             pid_t sid = setsid();
141             if (sid < 0)
142                 fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
143         }
144 #endif
145         SoftSetBoolArg("-server", true);
146
147         detectShutdownThread = new boost::thread(boost::bind(&DetectShutdownThread, &threadGroup));
148         fRet = AppInit2(threadGroup);
149     }
150     catch (std::exception& e) {
151         PrintExceptionContinue(&e, "AppInit()");
152     } catch (...) {
153         PrintExceptionContinue(NULL, "AppInit()");
154     }
155
156     if (!fRet)
157     {
158         if (detectShutdownThread)
159             detectShutdownThread->interrupt();
160
161         threadGroup.interrupt_all();
162         // threadGroup.join_all(); was left out intentionally here, because we didn't re-test all of
163         // the startup-failure cases to make sure they don't result in a hang due to some
164         // thread-blocking-waiting-for-another-thread-during-startup case
165     }
166
167     if (detectShutdownThread)
168     {
169         detectShutdownThread->join();
170         delete detectShutdownThread;
171         detectShutdownThread = NULL;
172     }
173     Shutdown();
174
175     return fRet;
176 }
177
178 int main(int argc, char* argv[])
179 {
180     SetupEnvironment();
181
182     // Connect bitcoind signal handlers
183     noui_connect();
184
185     return (AppInit(argc, argv) ? 0 : 1);
186 }
This page took 0.032892 seconds and 4 git commands to generate.