Dash Core  0.12.2.1
P2P Digital Currency
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-2017 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 #include "chainparams.h"
8 #include "clientversion.h"
9 #include "rpc/server.h"
10 #include "init.h"
11 #include "noui.h"
12 #include "scheduler.h"
13 #include "util.h"
14 #include "masternodeconfig.h"
15 #include "httpserver.h"
16 #include "httprpc.h"
17 
18 #include <boost/algorithm/string/predicate.hpp>
19 #include <boost/filesystem.hpp>
20 #include <boost/thread.hpp>
21 
22 #include <stdio.h>
23 
24 /* Introduction text for doxygen: */
25 
40 static bool fDaemon;
41 
42 void WaitForShutdown(boost::thread_group* threadGroup)
43 {
44  bool fShutdown = ShutdownRequested();
45  // Tell the main threads to shutdown.
46  while (!fShutdown)
47  {
48  MilliSleep(200);
49  fShutdown = ShutdownRequested();
50  }
51  if (threadGroup)
52  {
53  Interrupt(*threadGroup);
54  threadGroup->join_all();
55  }
56 }
57 
59 //
60 // Start
61 //
62 bool AppInit(int argc, char* argv[])
63 {
64  boost::thread_group threadGroup;
65  CScheduler scheduler;
66 
67  bool fRet = false;
68 
69  //
70  // Parameters
71  //
72  // If Qt is used, parameters/dash.conf are parsed in qt/dash.cpp's main()
73  ParseParameters(argc, argv);
74 
75  // Process help and version before taking care about datadir
76  if (mapArgs.count("-?") || mapArgs.count("-h") || mapArgs.count("-help") || mapArgs.count("-version"))
77  {
78  std::string strUsage = _("Dash Core Daemon") + " " + _("version") + " " + FormatFullVersion() + "\n";
79 
80  if (mapArgs.count("-version"))
81  {
82  strUsage += LicenseInfo();
83  }
84  else
85  {
86  strUsage += "\n" + _("Usage:") + "\n" +
87  " dashd [options] " + _("Start Dash Core Daemon") + "\n";
88 
89  strUsage += "\n" + HelpMessage(HMM_BITCOIND);
90  }
91 
92  fprintf(stdout, "%s", strUsage.c_str());
93  return true;
94  }
95 
96  try
97  {
98  bool datadirFromCmdLine = mapArgs.count("-datadir") != 0;
99  if (datadirFromCmdLine && !boost::filesystem::is_directory(GetDataDir(false)))
100  {
101  fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str());
102  return false;
103  }
104  try
105  {
107  } catch (const std::exception& e) {
108  fprintf(stderr,"Error reading configuration file: %s\n", e.what());
109  return false;
110  }
111  if (!datadirFromCmdLine && !boost::filesystem::is_directory(GetDataDir(false)))
112  {
113  fprintf(stderr, "Error: Specified data directory \"%s\" from config file does not exist.\n", mapArgs["-datadir"].c_str());
114  return EXIT_FAILURE;
115  }
116  // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
117  try {
119  } catch (const std::exception& e) {
120  fprintf(stderr, "Error: %s\n", e.what());
121  return false;
122  }
123 
124  // parse masternode.conf
125  std::string strErr;
126  if(!masternodeConfig.read(strErr)) {
127  fprintf(stderr,"Error reading masternode configuration file: %s\n", strErr.c_str());
128  return false;
129  }
130 
131  // Command-line RPC
132  bool fCommandLine = false;
133  for (int i = 1; i < argc; i++)
134  if (!IsSwitchChar(argv[i][0]) && !boost::algorithm::istarts_with(argv[i], "dash:"))
135  fCommandLine = true;
136 
137  if (fCommandLine)
138  {
139  fprintf(stderr, "Error: There is no RPC client functionality in dashd anymore. Use the dash-cli utility instead.\n");
140  exit(EXIT_FAILURE);
141  }
142 #ifndef WIN32
143  fDaemon = GetBoolArg("-daemon", false);
144  if (fDaemon)
145  {
146  fprintf(stdout, "Dash Core server starting\n");
147 
148  // Daemonize
149  pid_t pid = fork();
150  if (pid < 0)
151  {
152  fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
153  return false;
154  }
155  if (pid > 0) // Parent process, pid is child process id
156  {
157  return true;
158  }
159  // Child process falls through to rest of initialization
160 
161  pid_t sid = setsid();
162  if (sid < 0)
163  fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
164  }
165 #endif
166  SoftSetBoolArg("-server", true);
167 
168  // Set this early so that parameter interactions go to console
169  InitLogging();
171  fRet = AppInit2(threadGroup, scheduler);
172  }
173  catch (const std::exception& e) {
174  PrintExceptionContinue(&e, "AppInit()");
175  } catch (...) {
176  PrintExceptionContinue(NULL, "AppInit()");
177  }
178 
179  if (!fRet)
180  {
181  Interrupt(threadGroup);
182  // threadGroup.join_all(); was left out intentionally here, because we didn't re-test all of
183  // the startup-failure cases to make sure they don't result in a hang due to some
184  // thread-blocking-waiting-for-another-thread-during-startup case
185  } else {
186  WaitForShutdown(&threadGroup);
187  }
188  Shutdown();
189 
190  return fRet;
191 }
192 
193 int main(int argc, char* argv[])
194 {
196 
197  // Connect dashd signal handlers
198  noui_connect();
199 
200  return (AppInit(argc, argv) ? EXIT_SUCCESS : EXIT_FAILURE);
201 }
const boost::filesystem::path & GetDataDir(bool fNetSpecific)
Definition: util.cpp:547
bool AppInit2(boost::thread_group &threadGroup, CScheduler &scheduler)
Definition: init.cpp:942
void InitLogging()
Initialize the logging infrastructure.
Definition: init.cpp:926
CMasternodeConfig masternodeConfig
void MilliSleep(int64_t n)
Definition: utiltime.cpp:63
bool ShutdownRequested()
Definition: init.cpp:168
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Definition: util.cpp:470
bool read(std::string &strErr)
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: util.cpp:509
void noui_connect()
Definition: noui.cpp:53
int main(int argc, char *argv[])
Definition: dashd.cpp:193
static bool fDaemon
Definition: dashd.cpp:40
std::string LicenseInfo()
Definition: init.cpp:637
std::string HelpMessage(HelpMessageMode mode)
Definition: init.cpp:384
void InitParameterInteraction()
Parameter interaction: change current parameters depending on various rules.
Definition: init.cpp:818
bool GetBoolArg(const std::string &strArg, bool fDefault)
Definition: util.cpp:455
void WaitForShutdown(boost::thread_group *threadGroup)
Definition: dashd.cpp:42
void Interrupt(boost::thread_group &threadGroup)
Definition: init.cpp:197
void SelectParams(const std::string &network)
void Shutdown()
Definition: init.cpp:315
bool AppInit(int argc, char *argv[])
Definition: dashd.cpp:62
void ParseParameters(int argc, const char *const argv[])
Definition: util.cpp:406
std::string FormatFullVersion()
bool IsSwitchChar(char c)
Definition: util.h:164
void ReadConfigFile(map< string, string > &mapSettingsRet, map< string, vector< string > > &mapMultiSettingsRet)
Definition: util.cpp:627
std::string ChainNameFromCommandLine()
void SetupEnvironment()
Definition: util.cpp:904
map< string, vector< string > > mapMultiArgs
Definition: util.cpp:123
std::string _(const char *psz)
Definition: util.h:84
map< string, string > mapArgs
Definition: util.cpp:122