PIVX Core  5.6.99
P2P Digital Currency
pivxd.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2014-2015 The Dash developers
4 // Copyright (c) 2015-2022 The PIVX Core developers
5 // Distributed under the MIT/X11 software license, see the accompanying
6 // file COPYING or https://www.opensource.org/licenses/mit-license.php.
7 
8 #if defined(HAVE_CONFIG_H)
9 #include "config/pivx-config.h"
10 #endif
11 
12 #include "chainparams.h"
13 #include "clientversion.h"
14 #include "fs.h"
15 #include "init.h"
16 #include "masternodeconfig.h"
17 #include "noui.h"
18 #include "shutdown.h"
19 #include "util/system.h"
20 
21 #include <stdio.h>
22 
23 /* Introduction text for doxygen: */
24 
40 {
41  while (!ShutdownRequested()) {
42  MilliSleep(200);
43  }
44  Interrupt();
45 }
46 
48 //
49 // Start
50 //
51 bool AppInit(int argc, char* argv[])
52 {
53  bool fRet = false;
54 
55  //
56  // Parameters
57  //
58  // If Qt is used, parameters/pivx.conf are parsed in qt/pivx.cpp's main()
59  gArgs.ParseParameters(argc, argv);
60 
61  // Process help and version before taking care about datadir
62  if (gArgs.IsArgSet("-?") || gArgs.IsArgSet("-h") || gArgs.IsArgSet("-help") || gArgs.IsArgSet("-version")) {
63  std::string strUsage = PACKAGE_NAME " Daemon version " + FormatFullVersion() + "\n";
64 
65  if (gArgs.IsArgSet("-version")) {
66  strUsage += LicenseInfo();
67  } else {
68  strUsage += "\nUsage: pivxd [options] Start " PACKAGE_NAME " Daemon\n";
69  strUsage += "\n" + HelpMessage(HMM_BITCOIND);
70  }
71 
72  fprintf(stdout, "%s", strUsage.c_str());
73  return true;
74  }
75 
76  try {
77  if (!CheckDataDirOption()) {
78  fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
79  return false;
80  }
81  try {
83  } catch (const std::exception& e) {
84  fprintf(stderr, "Error reading configuration file: %s\n", e.what());
85  return false;
86  }
87  // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
88  try {
90  } catch(const std::exception& e) {
91  fprintf(stderr, "Error: %s\n", e.what());
92  return false;
93  }
94 
95  // Error out when loose non-argument tokens are encountered on command line
96  for (int i = 1; i < argc; i++) {
97  if (!IsSwitchChar(argv[i][0])) {
98  fprintf(stderr, "Error: Command line contains unexpected token '%s', see pivxd -h for a list of options.\n", argv[i]);
99  return false;
100  }
101  }
102 
103  // -server defaults to true for pivxd but not for the GUI so do this here
104  gArgs.SoftSetBoolArg("-server", true);
105  // Set this early so that parameter interactions go to console
106  InitLogging();
108  if (!AppInitBasicSetup()) {
109  // UIError will have been called with detailed error, which ends up on console
110  return false;
111  }
113  // UIError will have been called with detailed error, which ends up on console
114  return false;
115  }
116  if (!AppInitSanityChecks()) {
117  // UIError will have been called with detailed error, which ends up on console
118  return false;
119  }
120 
121 #ifndef WIN32
122  if (gArgs.GetBoolArg("-daemon", false)) {
123  fprintf(stdout, "PIVX server starting\n");
124 
125  // Daemonize
126  pid_t pid = fork();
127  if (pid < 0) {
128  fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
129  return false;
130  }
131  if (pid > 0) // Parent process, pid is child process id
132  {
133  return true;
134  }
135  // Child process falls through to rest of initialization
136 
137  pid_t sid = setsid();
138  if (sid < 0)
139  fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
140  }
141 #endif
142 
143  // Set this early so that parameter interactions go to console
144  fRet = AppInitMain();
145  } catch (const std::exception& e) {
146  PrintExceptionContinue(&e, "AppInit()");
147  } catch (...) {
148  PrintExceptionContinue(nullptr, "AppInit()");
149  }
150 
151  if (!fRet) {
152  Interrupt();
153  } else {
154  WaitForShutdown();
155  }
156  Shutdown();
157 
158  return fRet;
159 }
160 
161 int main(int argc, char* argv[])
162 {
163 #ifdef WIN32
164  util::WinCmdLineArgs winArgs;
165  std::tie(argc, argv) = winArgs.get();
166 #endif
168 
169  // Connect pivxd signal handlers
170  noui_connect();
171 
172  return (AppInit(argc, argv) ? 0 : 1);
173 }
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given chain name.
void ReadConfigFile(const std::string &confPath)
Definition: system.cpp:832
void ParseParameters(int argc, const char *const argv[])
Definition: system.cpp:371
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: system.cpp:425
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:449
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn't already have a value.
Definition: system.cpp:481
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:465
std::string GetChainName() const
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
Definition: system.cpp:861
std::string FormatFullVersion()
char ** argv
Definition: fuzz.cpp:52
void InitParameterInteraction()
Parameter interaction: change current parameters depending on various rules.
Definition: init.cpp:856
bool AppInitMain()
Bitcoin core main initialization.
Definition: init.cpp:1168
std::string HelpMessage(HelpMessageMode mode)
Help for options shared between UI and daemon (for -help)
Definition: init.cpp:381
bool AppInitParameterInteraction()
Initialization: parameter interaction.
Definition: init.cpp:974
bool AppInitBasicSetup()
Initialize PIVX core: Basic context setup.
Definition: init.cpp:805
bool AppInitSanityChecks()
Initialization sanity checks: ecc init, sanity checks, dir lock.
Definition: init.cpp:1151
void InitLogging()
Initialize the logging infrastructure.
Definition: init.cpp:949
void Interrupt()
Interrupt threads.
Definition: init.cpp:186
void Shutdown()
Definition: init.cpp:199
std::string LicenseInfo()
Returns licensing information (for -version)
Definition: init.cpp:570
@ HMM_BITCOIND
Definition: init.h:54
void noui_connect()
Definition: noui.cpp:49
#define PACKAGE_NAME
Definition: pivx-config.h:366
int main(int argc, char *argv[])
Definition: pivxd.cpp:161
void WaitForShutdown()
Definition: pivxd.cpp:39
bool AppInit(int argc, char *argv[])
Definition: pivxd.cpp:51
bool ShutdownRequested()
Definition: shutdown.cpp:22
const char *const PIVX_CONF_FILENAME
Definition: system.cpp:81
bool CheckDataDirOption()
Definition: system.cpp:755
ArgsManager gArgs
Definition: system.cpp:89
void SetupEnvironment()
Definition: system.cpp:1043
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: system.cpp:526
bool IsSwitchChar(char c)
Definition: system.h:118
void MilliSleep(int64_t n)
Definition: utiltime.cpp:82