BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
bsha3d.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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)
8 #endif
9 
10 #include <chainparams.h>
11 #include <clientversion.h>
12 #include <compat.h>
13 #include <fs.h>
14 #include <rpc/server.h>
15 #include <init.h>
16 #include <noui.h>
17 #include <shutdown.h>
18 #include <util.h>
19 #include <httpserver.h>
20 #include <httprpc.h>
21 #include <utilstrencodings.h>
22 #include <walletinitinterface.h>
23 
24 #include <stdio.h>
25 
26 const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
27 
28 /* Introduction text for doxygen: */
29 
46 static void WaitForShutdown()
47 {
48  while (!ShutdownRequested())
49  {
50  MilliSleep(200);
51  }
52  Interrupt();
53 }
54 
56 //
57 // Start
58 //
59 static bool AppInit(int argc, char* argv[])
60 {
61  bool fRet = false;
62 
63  //
64  // Parameters
65  //
66  // If Qt is used, parameters/bitcoin.conf are parsed in qt/bitcoin.cpp's main()
68  std::string error;
69  if (!gArgs.ParseParameters(argc, argv, error)) {
70  fprintf(stderr, "Error parsing command line arguments: %s\n", error.c_str());
71  return false;
72  }
73 
74  // Process help and version before taking care about datadir
75  if (HelpRequested(gArgs) || gArgs.IsArgSet("-version")) {
76  std::string strUsage = PACKAGE_NAME " Daemon version " + FormatFullVersion() + "\n";
77 
78  if (gArgs.IsArgSet("-version"))
79  {
80  strUsage += FormatParagraph(LicenseInfo());
81  }
82  else
83  {
84  strUsage += "\nUsage: bsha3d [options] Start " PACKAGE_NAME " Daemon\n";
85  strUsage += "\n" + gArgs.GetHelpMessage();
86  }
87 
88  fprintf(stdout, "%s", strUsage.c_str());
89  return true;
90  }
91 
92  try
93  {
94  if (!fs::is_directory(GetDataDir(false)))
95  {
96  fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
97  return false;
98  }
99  if (!gArgs.ReadConfigFiles(error, true)) {
100  fprintf(stderr, "Error reading configuration file: %s\n", error.c_str());
101  return false;
102  }
103  // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
104  try {
106  } catch (const std::exception& e) {
107  fprintf(stderr, "Error: %s\n", e.what());
108  return false;
109  }
110 
111  // Error out when loose non-argument tokens are encountered on command line
112  for (int i = 1; i < argc; i++) {
113  if (!IsSwitchChar(argv[i][0])) {
114  fprintf(stderr, "Error: Command line contains unexpected token '%s', see bsha3d -h for a list of options.\n", argv[i]);
115  return false;
116  }
117  }
118 
119  // -server defaults to true for bsha3d but not for the GUI so do this here
120  gArgs.SoftSetBoolArg("-server", true);
121  // Set this early so that parameter interactions go to console
122  InitLogging();
124  if (!AppInitBasicSetup())
125  {
126  // InitError will have been called with detailed error, which ends up on console
127  return false;
128  }
130  {
131  // InitError will have been called with detailed error, which ends up on console
132  return false;
133  }
134  if (!AppInitSanityChecks())
135  {
136  // InitError will have been called with detailed error, which ends up on console
137  return false;
138  }
139  if (gArgs.GetBoolArg("-daemon", false))
140  {
141 #if HAVE_DECL_DAEMON
142 #if defined(MAC_OSX)
143 #pragma GCC diagnostic push
144 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
145 #endif
146  fprintf(stdout, "BSHA3 server starting\n");
147 
148  // Daemonize
149  if (daemon(1, 0)) { // don't chdir (1), do close FDs (0)
150  fprintf(stderr, "Error: daemon() failed: %s\n", strerror(errno));
151  return false;
152  }
153 #if defined(MAC_OSX)
154 #pragma GCC diagnostic pop
155 #endif
156 #else
157  fprintf(stderr, "Error: -daemon is not supported on this operating system\n");
158  return false;
159 #endif // HAVE_DECL_DAEMON
160  }
161  // Lock data directory after daemonization
163  {
164  // If locking the data directory failed, exit immediately
165  return false;
166  }
167  fRet = AppInitMain();
168  }
169  catch (const std::exception& e) {
170  PrintExceptionContinue(&e, "AppInit()");
171  } catch (...) {
172  PrintExceptionContinue(nullptr, "AppInit()");
173  }
174 
175  if (!fRet)
176  {
177  Interrupt();
178  } else {
179  WaitForShutdown();
180  }
181  Shutdown();
182 
183  return fRet;
184 }
185 
186 int main(int argc, char* argv[])
187 {
188 #ifdef WIN32
189  util::WinCmdLineArgs winArgs;
190  std::tie(argc, argv) = winArgs.get();
191 #endif
193 
194  // Connect bsha3d signal handlers
195  noui_connect();
196 
197  return (AppInit(argc, argv) ? EXIT_SUCCESS : EXIT_FAILURE);
198 }
bool ReadConfigFiles(std::string &error, bool ignore_invalid_keys=false)
Definition: util.cpp:887
bool HelpRequested(const ArgsManager &args)
Definition: util.cpp:662
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: util.cpp:502
bool ShutdownRequested()
Definition: shutdown.cpp:20
void InitLogging()
Initialize global loggers.
Definition: init.cpp:805
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:558
bool AppInitMain()
Bitcoin core main initialization.
Definition: init.cpp:1152
void Interrupt()
Interrupt threads.
Definition: init.cpp:144
#define PACKAGE_NAME
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: util.cpp:698
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: util.cpp:411
void noui_connect()
Connect all bsha3d signal handlers.
Definition: noui.cpp:54
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: util.cpp:542
bool AppInitBasicSetup()
Initialize bitcoin core: Basic context setup.
Definition: init.cpp:852
std::string LicenseInfo()
Returns licensing information (for -version)
Definition: init.cpp:509
std::string GetHelpMessage() const
Get the help string.
Definition: util.cpp:593
void InitParameterInteraction()
Parameter interaction: change current parameters depending on various rules.
Definition: init.cpp:726
int main(int argc, char *argv[])
Definition: bsha3d.cpp:186
bool AppInitSanityChecks()
Initialization sanity checks: ecc init, sanity checks, dir lock.
Definition: init.cpp:1119
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given BIP70 chain name.
const std::function< std::string(const char *)> G_TRANSLATION_FUN
Translate a message to the native language of the user.
Definition: bsha3d.cpp:26
void Shutdown()
Definition: init.cpp:159
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:902
ArgsManager gArgs
Definition: util.cpp:88
bool IsSwitchChar(char c)
Definition: util.h:104
void SetupServerArgs()
Setup the arguments for gArgs.
Definition: init.cpp:315
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: util.cpp:526
bool error(const char *fmt, const Args &... args)
Definition: util.h:59
std::string GetChainName() const
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
Definition: util.cpp:967
const fs::path & GetDataDir(bool fNetSpecific)
Definition: util.cpp:766
bool AppInitLockDataDirectory()
Lock bitcoin core data directory.
Definition: init.cpp:1140
void SetupEnvironment()
Definition: util.cpp:1184