Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

dashd.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Copyright (c) 2014-2019 The Dash Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #if defined(HAVE_CONFIG_H)
8 #include <config/dash-config.h>
9 #endif
10 
11 #include <chainparams.h>
12 #include <clientversion.h>
13 #include <compat.h>
14 #include <fs.h>
15 #include <rpc/server.h>
16 #include <init.h>
17 #include <noui.h>
18 #include <util.h>
19 #include <httpserver.h>
20 #include <httprpc.h>
21 #include <utilstrencodings.h>
22 #include <walletinitinterface.h>
23 #include <stacktraces.h>
24 
25 #include <boost/thread.hpp>
26 
27 #include <stdio.h>
28 
29 /* Introduction text for doxygen: */
30 
46 {
47  while (!ShutdownRequested())
48  {
49  MilliSleep(200);
50  }
51  Interrupt();
52 }
53 
55 //
56 // Start
57 //
58 bool AppInit(int argc, char* argv[])
59 {
60  bool fRet = false;
61 
62  //
63  // Parameters
64  //
65  // If Qt is used, parameters/dash.conf are parsed in qt/dash.cpp's main()
66  gArgs.ParseParameters(argc, argv);
67 
68  if (gArgs.IsArgSet("-printcrashinfo")) {
69  std::cout << GetCrashInfoStrFromSerializedStr(gArgs.GetArg("-printcrashinfo", "")) << std::endl;
70  return true;
71  }
72 
73  // Process help and version before taking care about datadir
74  if (gArgs.IsArgSet("-?") || gArgs.IsArgSet("-h") || gArgs.IsArgSet("-help") || gArgs.IsArgSet("-version"))
75  {
76  std::string strUsage = strprintf(_("%s Daemon"), _(PACKAGE_NAME)) + " " + _("version") + " " + FormatFullVersion() + "\n";
77 
78  if (gArgs.IsArgSet("-version"))
79  {
80  strUsage += FormatParagraph(LicenseInfo());
81  }
82  else
83  {
84  strUsage += "\n" + _("Usage:") + "\n" +
85  " dashd [options] " + strprintf(_("Start %s Daemon"), _(PACKAGE_NAME)) + "\n";
86 
87  strUsage += "\n" + HelpMessage(HMM_BITCOIND);
88  }
89 
90  fprintf(stdout, "%s", strUsage.c_str());
91  return true;
92  }
93 
94  try
95  {
96  bool datadirFromCmdLine = gArgs.IsArgSet("-datadir");
97  if (datadirFromCmdLine && !fs::is_directory(GetDataDir(false)))
98  {
99  fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
100  return false;
101  }
102  try
103  {
105  } catch (const std::exception& e) {
106  fprintf(stderr,"Error reading configuration file: %s\n", e.what());
107  return false;
108  }
109  if (!datadirFromCmdLine && !fs::is_directory(GetDataDir(false)))
110  {
111  fprintf(stderr, "Error: Specified data directory \"%s\" from config file does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
112  return EXIT_FAILURE;
113  }
114  // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
115  try {
117  } catch (const std::exception& e) {
118  fprintf(stderr, "Error: %s\n", e.what());
119  return false;
120  }
121 
122  // Error out when loose non-argument tokens are encountered on command line
123  for (int i = 1; i < argc; i++) {
124  if (!IsSwitchChar(argv[i][0])) {
125  fprintf(stderr, "Error: Command line contains unexpected token '%s', see dashd -h for a list of options.\n", argv[i]);
126  return false;
127  }
128  }
129 
130  // -server defaults to true for dashd but not for the GUI so do this here
131  gArgs.SoftSetBoolArg("-server", true);
132  // Set this early so that parameter interactions go to console
133  InitLogging();
135  if (!AppInitBasicSetup())
136  {
137  // InitError will have been called with detailed error, which ends up on console
138  return false;
139  }
141  {
142  // InitError will have been called with detailed error, which ends up on console
143  return false;
144  }
145  if (!AppInitSanityChecks())
146  {
147  // InitError will have been called with detailed error, which ends up on console
148  return false;
149  }
150  if (gArgs.GetBoolArg("-daemon", false))
151  {
152 #if HAVE_DECL_DAEMON
153  fprintf(stdout, "Dash Core server starting\n");
154 
155  // Daemonize
156  if (daemon(1, 0)) { // don't chdir (1), do close FDs (0)
157  fprintf(stderr, "Error: daemon() failed: %s\n", strerror(errno));
158  return false;
159  }
160 #else
161  fprintf(stderr, "Error: -daemon is not supported on this operating system\n");
162  return false;
163 #endif // HAVE_DECL_DAEMON
164  }
165  // Lock data directory after daemonization
167  {
168  // If locking the data directory failed, exit immediately
169  return false;
170  }
171  fRet = AppInitMain();
172  } catch (...) {
173  PrintExceptionContinue(std::current_exception(), "AppInit()");
174  }
175 
176  if (!fRet)
177  {
178  Interrupt();
179  } else {
180  WaitForShutdown();
181  }
182  Shutdown();
183 
184  return fRet;
185 }
186 
187 int main(int argc, char* argv[])
188 {
191 
193 
194  // Connect dashd signal handlers
195  noui_connect();
196 
197  return (AppInit(argc, argv) ? EXIT_SUCCESS : EXIT_FAILURE);
198 }
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: util.cpp:784
void ParseParameters(int argc, const char *const argv[])
Definition: util.cpp:730
void InitLogging()
Initialize the logging infrastructure.
Definition: init.cpp:1024
void MilliSleep(int64_t n)
Definition: utiltime.cpp:75
const char *const BITCOIN_CONF_FILENAME
Definition: util.cpp:104
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn&#39;t already have a value.
Definition: util.cpp:840
bool AppInitMain()
Dash Core main initialization.
Definition: init.cpp:1586
bool ShutdownRequested()
Definition: init.cpp:179
#define strprintf
Definition: tinyformat.h:1066
std::string GetCrashInfoStrFromSerializedStr(const std::string &ciStr)
void Interrupt()
Interrupt threads.
Definition: init.cpp:215
void noui_connect()
Definition: noui.cpp:53
int main(int argc, char *argv[])
Definition: dashd.cpp:187
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: util.cpp:824
bool AppInitBasicSetup()
Initialize Dash Core: Basic context setup.
Definition: init.cpp:1059
std::string LicenseInfo()
Returns licensing information (for -version)
Definition: init.cpp:669
void ReadConfigFile(const std::string &confPath)
Definition: util.cpp:1000
std::string HelpMessage(HelpMessageMode mode)
Help for options shared between UI and daemon (for -help)
Definition: init.cpp:449
void InitParameterInteraction()
Parameter interaction: change current parameters depending on various rules.
Definition: init.cpp:923
bool AppInitSanityChecks()
Initialization sanity checks: ecc init, sanity checks, dir lock.
Definition: init.cpp:1553
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given BIP70 chain name.
void Shutdown()
Shutdown is split into 2 parts: Part 1: shut down everything but the main wallet instance (done in Pr...
Definition: init.cpp:375
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...
bool AppInit(int argc, char *argv[])
Definition: dashd.cpp:58
void RegisterPrettyTerminateHander()
std::string FormatFullVersion()
bool AppInitParameterInteraction()
Initialization: parameter interaction.
Definition: init.cpp:1109
void PrintExceptionContinue(const std::exception_ptr pex, const char *pszExceptionOrigin)
Definition: util.cpp:891
ArgsManager gArgs
Definition: util.cpp:108
bool IsSwitchChar(char c)
Definition: util.h:269
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: util.cpp:808
void RegisterPrettySignalHandlers()
std::string GetChainName() const
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
Definition: util.cpp:1026
const fs::path & GetDataDir(bool fNetSpecific)
Definition: util.cpp:928
bool AppInitLockDataDirectory()
Lock Dash Core data directory.
Definition: init.cpp:1574
void WaitForShutdown()
Definition: dashd.cpp:45
void SetupEnvironment()
Definition: util.cpp:1314
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a boost::optional result...
Definition: util.h:92
Released under the MIT license