]> Git Repo - VerusCoin.git/blob - src/bitcoind.cpp
Merge pull request #6033
[VerusCoin.git] / src / bitcoind.cpp
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin Core developers
3 // Distributed under the MIT 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 (https://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 WaitForShutdown(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
60     bool fRet = false;
61
62     //
63     // Parameters
64     //
65     // If Qt is used, parameters/bitcoin.conf are parsed in qt/bitcoin.cpp's main()
66     ParseParameters(argc, argv);
67
68     // Process help and version before taking care about datadir
69     if (mapArgs.count("-?") || mapArgs.count("-help") || mapArgs.count("-version"))
70     {
71         std::string strUsage = _("Bitcoin Core Daemon") + " " + _("version") + " " + FormatFullVersion() + "\n";
72
73         if (mapArgs.count("-version"))
74         {
75             strUsage += LicenseInfo();
76         }
77         else
78         {
79             strUsage += "\n" + _("Usage:") + "\n" +
80                   "  bitcoind [options]                     " + _("Start Bitcoin Core Daemon") + "\n";
81
82             strUsage += "\n" + HelpMessage(HMM_BITCOIND);
83         }
84
85         fprintf(stdout, "%s", strUsage.c_str());
86         return false;
87     }
88
89     try
90     {
91         if (!boost::filesystem::is_directory(GetDataDir(false)))
92         {
93             fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str());
94             return false;
95         }
96         try
97         {
98             ReadConfigFile(mapArgs, mapMultiArgs);
99         } catch (const std::exception& e) {
100             fprintf(stderr,"Error reading configuration file: %s\n", e.what());
101             return false;
102         }
103         // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
104         if (!SelectParamsFromCommandLine()) {
105             fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n");
106             return false;
107         }
108
109         // Command-line RPC
110         bool fCommandLine = false;
111         for (int i = 1; i < argc; i++)
112             if (!IsSwitchChar(argv[i][0]) && !boost::algorithm::istarts_with(argv[i], "bitcoin:"))
113                 fCommandLine = true;
114
115         if (fCommandLine)
116         {
117             fprintf(stderr, "Error: There is no RPC client functionality in bitcoind anymore. Use the bitcoin-cli utility instead.\n");
118             exit(1);
119         }
120 #ifndef WIN32
121         fDaemon = GetBoolArg("-daemon", false);
122         if (fDaemon)
123         {
124             fprintf(stdout, "Bitcoin server starting\n");
125
126             // Daemonize
127             pid_t pid = fork();
128             if (pid < 0)
129             {
130                 fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
131                 return false;
132             }
133             if (pid > 0) // Parent process, pid is child process id
134             {
135                 return true;
136             }
137             // Child process falls through to rest of initialization
138
139             pid_t sid = setsid();
140             if (sid < 0)
141                 fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
142         }
143 #endif
144         SoftSetBoolArg("-server", true);
145
146         fRet = AppInit2(threadGroup);
147     }
148     catch (const std::exception& e) {
149         PrintExceptionContinue(&e, "AppInit()");
150     } catch (...) {
151         PrintExceptionContinue(NULL, "AppInit()");
152     }
153
154     if (!fRet)
155     {
156         threadGroup.interrupt_all();
157         // threadGroup.join_all(); was left out intentionally here, because we didn't re-test all of
158         // the startup-failure cases to make sure they don't result in a hang due to some
159         // thread-blocking-waiting-for-another-thread-during-startup case
160     } else {
161         WaitForShutdown(&threadGroup);
162     }
163     Shutdown();
164
165     return fRet;
166 }
167
168 int main(int argc, char* argv[])
169 {
170     SetupEnvironment();
171
172     // Connect bitcoind signal handlers
173     noui_connect();
174
175     return (AppInit(argc, argv) ? 0 : 1);
176 }
This page took 0.034413 seconds and 4 git commands to generate.