Raven Core  3.0.0
P2P Digital Currency
ravend.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 // Copyright (c) 2017-2019 The Raven 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/raven-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 "scheduler.h"
19 #include "util.h"
20 #include "httpserver.h"
21 #include "httprpc.h"
22 #include "utilstrencodings.h"
23 
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/raven.conf are parsed in qt/raven.cpp's main()
75  gArgs.ParseParameters(argc, argv);
76 
77  // Process help and version before taking care about datadir
78  if (gArgs.IsArgSet("-?") || gArgs.IsArgSet("-h") || gArgs.IsArgSet("-help") || gArgs.IsArgSet("-version"))
79  {
80  std::string strUsage = strprintf(_("%s Daemon"), _(PACKAGE_NAME)) + " " + _("version") + " " + FormatFullVersion() + "\n";
81 
82  if (gArgs.IsArgSet("-version"))
83  {
84  strUsage += FormatParagraph(LicenseInfo());
85  }
86  else
87  {
88  strUsage += "\n" + _("Usage:") + "\n" +
89  " ravend [options] " + strprintf(_("Start %s Daemon"), _(PACKAGE_NAME)) + "\n";
90 
91  strUsage += "\n" + HelpMessage(HMM_RAVEND);
92  }
93 
94  fprintf(stdout, "%s", strUsage.c_str());
95  return true;
96  }
97 
98  try
99  {
100  if (!fs::is_directory(GetDataDir(false)))
101  {
102  fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.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  // Error out when loose non-argument tokens are encountered on command line
121  for (int i = 1; i < argc; i++) {
122  if (!IsSwitchChar(argv[i][0])) {
123  fprintf(stderr, "Error: Command line contains unexpected token '%s', see ravend -h for a list of options.\n", argv[i]);
124  exit(EXIT_FAILURE);
125  }
126  }
127 
128  // -server defaults to true for ravend but not for the GUI so do this here
129  gArgs.SoftSetBoolArg("-server", true);
130  // Set this early so that parameter interactions go to console
131  InitLogging();
133  if (!AppInitBasicSetup())
134  {
135  // InitError will have been called with detailed error, which ends up on console
136  exit(EXIT_FAILURE);
137  }
139  {
140  // InitError will have been called with detailed error, which ends up on console
141  exit(EXIT_FAILURE);
142  }
143  if (!AppInitSanityChecks())
144  {
145  // InitError will have been called with detailed error, which ends up on console
146  exit(EXIT_FAILURE);
147  }
148  if (gArgs.GetBoolArg("-daemon", false))
149  {
150 #if HAVE_DECL_DAEMON
151 #if defined(MAC_OSX)
152 #pragma GCC diagnostic push
153 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
154 #endif
155  fprintf(stdout, "Raven server starting\n");
156 
157  // Daemonize
158  if (daemon(1, 0)) { // don't chdir (1), do close FDs (0)
159  fprintf(stderr, "Error: daemon() failed: %s\n", strerror(errno));
160  return false;
161  }
162 #if defined(MAC_OSX)
163 #pragma GCC diagnostic pop
164 #endif
165 #else
166  fprintf(stderr, "Error: -daemon is not supported on this operating system\n");
167  return false;
168 #endif // HAVE_DECL_DAEMON
169  }
170  // Lock data directory after daemonization
172  {
173  // If locking the data directory failed, exit immediately
174  exit(EXIT_FAILURE);
175  }
176  fRet = AppInitMain(threadGroup, scheduler);
177  }
178  catch (const std::exception& e) {
179  PrintExceptionContinue(&e, "AppInit()");
180  } catch (...) {
181  PrintExceptionContinue(nullptr, "AppInit()");
182  }
183 
184  if (!fRet)
185  {
186  Interrupt(threadGroup);
187  threadGroup.join_all();
188  } else {
189  WaitForShutdown(&threadGroup);
190  }
191  Shutdown();
192 
193  return fRet;
194 }
195 
196 int main(int argc, char* argv[])
197 {
199 
200  // Connect ravend signal handlers
201  noui_connect();
202 
203  return (AppInit(argc, argv) ? EXIT_SUCCESS : EXIT_FAILURE);
204 }
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: util.cpp:448
const char *const RAVEN_CONF_FILENAME
Definition: util.cpp:91
void ParseParameters(int argc, const char *const argv[])
Definition: util.cpp:404
void InitLogging()
Initialize the logging infrastructure.
Definition: init.cpp:890
void MilliSleep(int64_t n)
Definition: utiltime.cpp:61
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn&#39;t already have a value.
Definition: util.cpp:486
bool ShutdownRequested()
Definition: init.cpp:136
#define strprintf
Definition: tinyformat.h:1054
int main(int argc, char *argv[])
Definition: ravend.cpp:196
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: util.cpp:535
void noui_connect()
Definition: noui.cpp:53
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: util.cpp:470
bool AppInitBasicSetup()
Initialize raven core: Basic context setup.
Definition: init.cpp:923
#define PACKAGE_NAME
Definition: raven-config.h:350
std::string LicenseInfo()
Returns licensing information (for -version)
Definition: init.cpp:597
void ReadConfigFile(const std::string &confPath)
Definition: util.cpp:621
void WaitForShutdown(boost::thread_group *threadGroup)
Definition: ravend.cpp:44
std::string HelpMessage(HelpMessageMode mode)
Help for options shared between UI and daemon (for -help)
Definition: init.cpp:391
void InitParameterInteraction()
Parameter interaction: change current parameters depending on various rules.
Definition: init.cpp:813
bool AppInitSanityChecks()
Initialization sanity checks: ecc init, sanity checks, dir lock.
Definition: init.cpp:1258
void Interrupt(boost::thread_group &threadGroup)
Interrupt threads.
Definition: init.cpp:169
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:336
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...
std::string FormatFullVersion()
bool AppInitParameterInteraction()
Initialization: parameter interaction.
Definition: init.cpp:971
ArgsManager gArgs
Definition: util.cpp:94
bool IsSwitchChar(char c)
Definition: util.h:213
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: util.cpp:454
std::string ChainNameFromCommandLine()
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
bool AppInit(int argc, char *argv[])
Definition: ravend.cpp:64
const fs::path & GetDataDir(bool fNetSpecific)
Definition: util.cpp:572
bool AppInitLockDataDirectory()
Lock raven core data directory.
Definition: init.cpp:1279
bool AppInitMain(boost::thread_group &threadGroup, CScheduler &scheduler)
Raven core main initialization.
Definition: init.cpp:1291
void SetupEnvironment()
Definition: util.cpp:865
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a boost::optional result...
Definition: util.h:66