Dogecoin Core  1.14.2
P2P Digital Currency
bitcoind.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 #if defined(HAVE_CONFIG_H)
7 #include "config/bitcoin-config.h"
8 #endif
9 
10 #include "chainparams.h"
11 #include "clientversion.h"
12 #include "compat.h"
13 #include "rpc/server.h"
14 #include "init.h"
15 #include "noui.h"
16 #include "scheduler.h"
17 #include "util.h"
18 #include "httpserver.h"
19 #include "httprpc.h"
20 #include "utilstrencodings.h"
21 
22 #include <boost/algorithm/string/predicate.hpp>
23 #include <boost/filesystem.hpp>
24 #include <boost/thread.hpp>
25 
26 #include <stdio.h>
27 
28 /* Introduction text for doxygen: */
29 
44 void WaitForShutdown(boost::thread_group* threadGroup)
45 {
46  bool fShutdown = ShutdownRequested();
47  // Tell the main threads to shutdown.
48  while (!fShutdown)
49  {
50  MilliSleep(200);
51  fShutdown = ShutdownRequested();
52  }
53  if (threadGroup)
54  {
55  Interrupt(*threadGroup);
56  threadGroup->join_all();
57  }
58 }
59 
61 //
62 // Start
63 //
64 bool AppInit(int argc, char* argv[])
65 {
66  boost::thread_group threadGroup;
67  CScheduler scheduler;
68 
69  bool fRet = false;
70 
71  //
72  // Parameters
73  //
74  // If Qt is used, parameters/bitcoin.conf are parsed in qt/bitcoin.cpp's main()
75  ParseParameters(argc, argv);
76 
77  // Process help and version before taking care about datadir
78  if (IsArgSet("-?") || IsArgSet("-h") || IsArgSet("-help") || IsArgSet("-version"))
79  {
80  std::string strUsage = strprintf(_("%s Daemon"), _(PACKAGE_NAME)) + " " + _("version") + " " + FormatFullVersion() + "\n";
81 
82  if (IsArgSet("-version"))
83  {
84  strUsage += FormatParagraph(LicenseInfo());
85  }
86  else
87  {
88  strUsage += "\n" + _("Usage:") + "\n" +
89  " dogecoind [options] " + strprintf(_("Start %s Daemon"), _(PACKAGE_NAME)) + "\n";
90 
91  strUsage += "\n" + HelpMessage(HMM_BITCOIND);
92  }
93 
94  fprintf(stdout, "%s", strUsage.c_str());
95  return true;
96  }
97 
98  try
99  {
100  if (!boost::filesystem::is_directory(GetDataDir(false)))
101  {
102  fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", GetArg("-datadir", "").c_str());
103  return false;
104  }
105  try
106  {
108  } catch (const std::exception& e) {
109  fprintf(stderr,"Error reading configuration file: %s\n", e.what());
110  return false;
111  }
112  // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
113  try {
115  } catch (const std::exception& e) {
116  fprintf(stderr, "Error: %s\n", e.what());
117  return false;
118  }
119 
120  // Command-line RPC
121  bool fCommandLine = false;
122  for (int i = 1; i < argc; i++)
123  if (!IsSwitchChar(argv[i][0]) && !boost::algorithm::istarts_with(argv[i], "dogecoin:"))
124  fCommandLine = true;
125 
126  if (fCommandLine)
127  {
128  fprintf(stderr, "Error: There is no RPC client functionality in dogecoind anymore. Use the dogecoin-cli utility instead.\n");
129  exit(EXIT_FAILURE);
130  }
131  // -server defaults to true for bitcoind but not for the GUI so do this here
132  SoftSetBoolArg("-server", true);
133  // Set this early so that parameter interactions go to console
134  InitLogging();
136  if (!AppInitBasicSetup())
137  {
138  // InitError will have been called with detailed error, which ends up on console
139  exit(1);
140  }
142  {
143  // InitError will have been called with detailed error, which ends up on console
144  exit(1);
145  }
146  if (!AppInitSanityChecks())
147  {
148  // InitError will have been called with detailed error, which ends up on console
149  exit(1);
150  }
151  if (GetBoolArg("-daemon", false))
152  {
153 #if HAVE_DECL_DAEMON
154  fprintf(stdout, "Dogecoin server starting\n");
155 
156  // Daemonize
157  if (daemon(1, 0)) { // don't chdir (1), do close FDs (0)
158  fprintf(stderr, "Error: daemon() failed: %s\n", strerror(errno));
159  return false;
160  }
161 #else
162  fprintf(stderr, "Error: -daemon is not supported on this operating system\n");
163  return false;
164 #endif // HAVE_DECL_DAEMON
165  }
166 
167  fRet = AppInitMain(threadGroup, scheduler);
168  }
169  catch (const std::exception& e) {
170  PrintExceptionContinue(&e, "AppInit()");
171  } catch (...) {
172  PrintExceptionContinue(NULL, "AppInit()");
173  }
174 
175  if (!fRet)
176  {
177  Interrupt(threadGroup);
178  // threadGroup.join_all(); was left out intentionally here, because we didn't re-test all of
179  // the startup-failure cases to make sure they don't result in a hang due to some
180  // thread-blocking-waiting-for-another-thread-during-startup case
181  } else {
182  WaitForShutdown(&threadGroup);
183  }
184  Shutdown();
185 
186  return fRet;
187 }
188 
189 int main(int argc, char* argv[])
190 {
192 
193  // Connect bitcoind signal handlers
194  noui_connect();
195 
196  return (AppInit(argc, argv) ? EXIT_SUCCESS : EXIT_FAILURE);
197 }
int main(int argc, char *argv[])
Definition: bitcoind.cpp:189
void WaitForShutdown(boost::thread_group *threadGroup)
Definition: bitcoind.cpp:44
bool AppInit(int argc, char *argv[])
Definition: bitcoind.cpp:64
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given BIP70 chain name.
std::string ChainNameFromCommandLine()
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
std::string FormatFullVersion()
bool AppInitMain(boost::thread_group &threadGroup, CScheduler &scheduler)
Bitcoin core main initialization.
Definition: init.cpp:1156
void InitParameterInteraction()
Parameter interaction: change current parameters depending on various rules.
Definition: init.cpp:719
std::string HelpMessage(HelpMessageMode mode)
Help for options shared between UI and daemon (for -help)
Definition: init.cpp:319
bool ShutdownRequested()
Definition: init.cpp:138
bool AppInitParameterInteraction()
Initialization: parameter interaction.
Definition: init.cpp:879
bool AppInitBasicSetup()
Initialize bitcoin core: Basic context setup.
Definition: init.cpp:821
bool AppInitSanityChecks()
Initialization sanity checks: ecc init, sanity checks, dir lock.
Definition: init.cpp:1140
void InitLogging()
Initialize the logging infrastructure.
Definition: init.cpp:787
void Shutdown()
Definition: init.cpp:184
std::string LicenseInfo()
Returns licensing information (for -version)
Definition: init.cpp:510
void Interrupt(boost::thread_group &threadGroup)
Interrupt threads.
Definition: init.cpp:172
@ HMM_BITCOIND
Definition: init.h:55
void noui_connect()
Definition: noui.cpp:52
#define strprintf
Definition: tinyformat.h:1047
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
Definition: util.cpp:395
void ParseParameters(int argc, const char *const argv[])
Definition: util.cpp:353
void ReadConfigFile(const std::string &confPath)
Definition: util.cpp:560
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
Definition: util.cpp:411
void SetupEnvironment()
Definition: util.cpp:797
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: util.cpp:475
const boost::filesystem::path & GetDataDir(bool fNetSpecific)
Definition: util.cpp:513
const char *const BITCOIN_CONF_FILENAME
Definition: util.cpp:106
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn't already have a value.
Definition: util.cpp:428
bool IsArgSet(const std::string &strArg)
Return true if the given argument has been manually set.
Definition: util.cpp:389
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a boost::optional result.
Definition: util.h:62
bool IsSwitchChar(char c)
Definition: util.h:117
std::string FormatParagraph(const std::string &in, size_t width, size_t indent)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line.
void MilliSleep(int64_t n)
Definition: utiltime.cpp:62