Fabcoin Core  0.16.2
P2P Digital Currency
fabcoind.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 <scheduler.h>
18 #include <util.h>
19 #include <httpserver.h>
20 #include <httprpc.h>
21 #include <utilstrencodings.h>
22 
23 #include <boost/thread.hpp>
24 
25 #include <stdio.h>
26 
27 /* Introduction text for doxygen: */
28 
43 void WaitForShutdown(boost::thread_group* threadGroup)
44 {
45  bool fShutdown = ShutdownRequested();
46  // Tell the main threads to shutdown.
47  while (!fShutdown)
48  {
49  MilliSleep(200);
50  fShutdown = ShutdownRequested();
51  }
52  if (threadGroup)
53  {
54  Interrupt(*threadGroup);
55  threadGroup->join_all();
56  }
57 }
58 
60 //
61 // Start
62 //
63 bool AppInit(int argc, char* argv[])
64 {
65  boost::thread_group threadGroup;
66  CScheduler scheduler;
67 
68  bool fRet = false;
69 
70  //
71  // Parameters
72  //
73  // If Qt is used, parameters/fabcoin.conf are parsed in qt/fabcoin.cpp's main()
74  gArgs.ParseParameters(argc, argv);
75 
76  // Process help and version before taking care about datadir
77  if (gArgs.IsArgSet("-?") || gArgs.IsArgSet("-h") || gArgs.IsArgSet("-help") || gArgs.IsArgSet("-version"))
78  {
79  std::string strUsage = strprintf(_("%s Daemon"), _(PACKAGE_NAME)) + " " + _("version") + " " + FormatFullVersion() + "\n";
80 
81  if (gArgs.IsArgSet("-version"))
82  {
83  strUsage += FormatParagraph(LicenseInfo());
84  }
85  else
86  {
87  strUsage += "\n" + _("Usage:") + "\n" +
88  " fabcoind [options] " + strprintf(_("Start %s Daemon"), _(PACKAGE_NAME)) + "\n";
89 
90  strUsage += "\n" + HelpMessage(HMM_FABCOIND);
91  }
92 
93  fprintf(stdout, "%s", strUsage.c_str());
94  return true;
95  }
96 
97  try
98  {
99  if (!fs::is_directory(GetDataDir(false)))
100  {
101  fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-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  // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
112  try {
114  } catch (const std::exception& e) {
115  fprintf(stderr, "Error: %s\n", e.what());
116  return false;
117  }
118 
119  // Error out when loose non-argument tokens are encountered on command line
120  for (int i = 1; i < argc; i++) {
121  if (!IsSwitchChar(argv[i][0])) {
122  fprintf(stderr, "Error: Command line contains unexpected token '%s', see fabcoind -h for a list of options.\n", argv[i]);
123  exit(EXIT_FAILURE);
124  }
125  }
126 
127  // -server defaults to true for fabcoind but not for the GUI so do this here
128  gArgs.SoftSetBoolArg("-server", true);
129  // Set this early so that parameter interactions go to console
130  InitLogging();
132  if (!AppInitBasicSetup())
133  {
134  // InitError will have been called with detailed error, which ends up on console
135  exit(EXIT_FAILURE);
136  }
138  {
139  // InitError will have been called with detailed error, which ends up on console
140  exit(EXIT_FAILURE);
141  }
142  if (!AppInitSanityChecks())
143  {
144  // InitError will have been called with detailed error, which ends up on console
145  exit(EXIT_FAILURE);
146  }
147  if (gArgs.GetBoolArg("-daemon", false))
148  {
149 #if HAVE_DECL_DAEMON
150  fprintf(stdout, "Fabcoin server starting\n");
151 
152  // Daemonize
153  if (daemon(1, 0)) { // don't chdir (1), do close FDs (0)
154  fprintf(stderr, "Error: daemon() failed: %s\n", strerror(errno));
155  return false;
156  }
157 #else
158  fprintf(stderr, "Error: -daemon is not supported on this operating system\n");
159  return false;
160 #endif // HAVE_DECL_DAEMON
161  }
162  // Lock data directory after daemonization
164  {
165  // If locking the data directory failed, exit immediately
166  exit(EXIT_FAILURE);
167  }
168  fRet = AppInitMain(threadGroup, scheduler);
169  }
170  catch (const std::exception& e) {
171  PrintExceptionContinue(&e, "AppInit()");
172  } catch (...) {
173  PrintExceptionContinue(nullptr, "AppInit()");
174  }
175 
176  if (!fRet)
177  {
178  Interrupt(threadGroup);
179  threadGroup.join_all();
180  } else {
181  WaitForShutdown(&threadGroup);
182  }
183  Shutdown();
184 
185  return fRet;
186 }
187 
188 int main(int argc, char* argv[])
189 {
191 
192  // Connect fabcoind signal handlers
193  noui_connect();
194 
195  return (AppInit(argc, argv) ? EXIT_SUCCESS : EXIT_FAILURE);
196 }
void ParseParameters(int argc, const char *const argv[])
Definition: util.cpp:454
void InitLogging()
Initialize the logging infrastructure.
Definition: init.cpp:851
void MilliSleep(int64_t n)
Definition: utiltime.cpp:60
bool IsArgSet(const std::string &strArg)
Return true if the given argument has been manually set.
Definition: util.cpp:498
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn&#39;t already have a value.
Definition: util.cpp:537
#define PACKAGE_NAME
bool ShutdownRequested()
Definition: init.cpp:126
#define strprintf
Definition: tinyformat.h:1054
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
Definition: util.cpp:520
int main(int argc, char *argv[])
Definition: fabcoind.cpp:188
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: util.cpp:586
void noui_connect()
Definition: noui.cpp:52
bool AppInitBasicSetup()
Initialize fabcoin core: Basic context setup.
Definition: init.cpp:885
std::string LicenseInfo()
Returns licensing information (for -version)
Definition: init.cpp:558
void ReadConfigFile(const std::string &confPath)
Definition: util.cpp:669
std::string HelpMessage(HelpMessageMode mode)
Help for options shared between UI and daemon (for -help)
Definition: init.cpp:338
void InitParameterInteraction()
Parameter interaction: change current parameters depending on various rules.
Definition: init.cpp:774
bool AppInitSanityChecks()
Initialization sanity checks: ecc init, sanity checks, dir lock.
Definition: init.cpp:1220
const char *const FABCOIN_CONF_FILENAME
Definition: util.cpp:91
void Interrupt(boost::thread_group &threadGroup)
Interrupt threads.
Definition: init.cpp:159
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given BIP70 chain name.
void Shutdown()
Definition: init.cpp:171
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:933
bool AppInit(int argc, char *argv[])
Definition: fabcoind.cpp:63
ArgsManager gArgs
Definition: util.cpp:94
bool IsSwitchChar(char c)
Definition: util.h:206
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
Definition: util.cpp:504
std::string ChainNameFromCommandLine()
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
#define e(i)
Definition: sha.cpp:733
void WaitForShutdown(boost::thread_group *threadGroup)
Definition: fabcoind.cpp:43
const fs::path & GetDataDir(bool fNetSpecific)
Definition: util.cpp:623
bool AppInitLockDataDirectory()
Lock fabcoin core data directory.
Definition: init.cpp:1241
bool AppInitMain(boost::thread_group &threadGroup, CScheduler &scheduler)
Fabcoin core main initialization.
Definition: init.cpp:1253
void SetupEnvironment()
Definition: util.cpp:904
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a boost::optional result...
Definition: util.h:71