PRCYCoin  2.0.0.7rc1
P2P Digital Currency
prcycoind.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-2018 The PIVX developers
5 // Copyright (c) 2018-2020 The DAPS Project developers
6 // Distributed under the MIT/X11 software license, see the accompanying
7 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
8 
9 #include "clientversion.h"
10 #include "fs.h"
11 #include "init.h"
12 #include "main.h"
13 #include "masternodeconfig.h"
14 #include "noui.h"
15 #include "rpc/server.h"
16 #include "guiinterface.h"
17 #include "util.h"
18 #include "httpserver.h"
19 #include "httprpc.h"
20 #include "rpc/server.h"
21 
22 #include <boost/algorithm/string/predicate.hpp>
23 #include <boost/thread.hpp>
24 
25 #include <stdio.h>
26 
27 /* Introduction text for doxygen: */
28 
43 static bool fDaemon;
44 static bool fMasternode;
45 
47 {
48  bool fShutdown = ShutdownRequested();
49  // Tell the main threads to shutdown.
50  while (!fShutdown) {
51  MilliSleep(200);
52  fShutdown = ShutdownRequested();
53  }
54  Interrupt();
55 }
56 
58 //
59 // Start
60 //
61 bool AppInit(int argc, char* argv[])
62 {
63 
64  bool fRet = false;
65 
66  //
67  // Parameters
68  //
69  // If Qt is used, parameters/prcycoin.conf are parsed in qt/prcycoin.cpp's main()
70  ParseParameters(argc, argv);
71 
72  // Process help and version before taking care about datadir
73  if (mapArgs.count("-?") || mapArgs.count("-help") || mapArgs.count("-version")) {
74  std::string strUsage = _("PRCY Daemon") + " " + _("version") + " " + FormatFullVersion() + "\n";
75 
76  if (mapArgs.count("-version")) {
77  strUsage += LicenseInfo();
78  } else {
79  strUsage += "\n" + _("Usage:") + "\n" +
80  " prcycoind [options] " + _("Start PRCY Daemon") + "\n";
81 
82  strUsage += "\n" + HelpMessage(HMM_BITCOIND);
83  }
84 
85  fprintf(stdout, "%s", strUsage.c_str());
86  return false;
87  }
88 
89  try {
90  if (!fs::is_directory(GetDataDir(false))) {
91  fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str());
92  return false;
93  }
94  try {
96  } catch (const std::exception& e) {
97  fprintf(stderr, "Error reading configuration file: %s\n", e.what());
98  return false;
99  }
100  // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
102  fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n");
103  return false;
104  }
105 
106  // parse masternode.conf
107  std::string strErr;
108  if (!masternodeConfig.read(strErr)) {
109  fprintf(stderr, "Error reading masternode configuration file: %s\n", strErr.c_str());
110  return false;
111  }
112 
113  // Command-line RPC
114  bool fCommandLine = false;
115  for (int i = 1; i < argc; i++)
116  if (!IsSwitchChar(argv[i][0]) && !boost::algorithm::istarts_with(argv[i], "prcycoin:"))
117  fCommandLine = true;
118 
119  if (fCommandLine) {
120  fprintf(stderr, "Error: There is no RPC client functionality in prcycoind anymore. Use the prcycoin-cli utility instead.\n");
121  exit(1);
122  }
123 #ifndef WIN32
124  fDaemon = GetBoolArg("-daemon", false);
125  fMasternode = GetBoolArg("-masternode", false);
126  if (fDaemon) {
127  std::string strUsage = _("PRCY Daemon") + " " + FormatFullVersion() + "\n";
128  fprintf(stdout, "%s", strUsage.c_str());
129  fprintf(stdout, "PRCY server starting\n");
130  if (fMasternode) {
131  fprintf(stdout, "Masternode = 1 - This is a masternode\n");
132  }
133  // Daemonize
134  pid_t pid = fork();
135  if (pid < 0) {
136  fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
137  return false;
138  }
139  if (pid > 0) // Parent process, pid is child process id
140  {
141  return true;
142  }
143  // Child process falls through to rest of initialization
144 
145  pid_t sid = setsid();
146  if (sid < 0)
147  fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
148  }
149 #endif
150  SoftSetBoolArg("-server", true);
151 
152  // Set this early so that parameter interactions go to console
153  InitLogging();
155  fRet = AppInit2(true);
156  } catch (const std::exception& e) {
157  PrintExceptionContinue(&e, "AppInit()");
158  } catch (...) {
159  PrintExceptionContinue(NULL, "AppInit()");
160  }
161 
162  if (!fRet) {
163  Interrupt();
164  } else {
165  WaitForShutdown();
166  }
167  Shutdown();
168 
169  return fRet;
170 }
171 
172 int main(int argc, char* argv[])
173 {
175 
176  // Connect prcycoind signal handlers
177  noui_connect();
178 
179  return (AppInit(argc, argv) ? 0 : 1);
180 }
InitLogging
void InitLogging()
Initialize the logging infrastructure.
Definition: init.cpp:880
SetupEnvironment
void SetupEnvironment()
Definition: util.cpp:600
ShutdownRequested
bool ShutdownRequested()
Definition: init.cpp:135
SoftSetBoolArg
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn't already have a value.
Definition: util.cpp:270
fs.h
ReadConfigFile
void ReadConfigFile(std::map< std::string, std::string > &mapSettingsRet, std::map< std::string, std::vector< std::string > > &mapMultiSettingsRet)
Definition: util.cpp:395
AppInit2
bool AppInit2(bool isDaemon)
Initialize prcy.
Definition: init.cpp:909
Interrupt
void Interrupt()
Interrupt threads.
Definition: init.cpp:166
mapArgs
std::map< std::string, std::string > mapArgs
Definition: util.cpp:111
SelectParamsFromCommandLine
bool SelectParamsFromCommandLine()
Looks for -regtest or -testnet and then calls SelectParams as appropriate.
Definition: chainparams.cpp:490
LicenseInfo
std::string LicenseInfo()
Returns licensing information (for -version)
Definition: init.cpp:574
clientversion.h
guiinterface.h
IsSwitchChar
bool IsSwitchChar(char c)
Definition: util.h:92
PrintExceptionContinue
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: util.cpp:309
noui_connect
void noui_connect()
Definition: noui.cpp:50
_
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a boost::optional result.
Definition: guiinterface.h:119
init.h
fDaemon
bool fDaemon
Definition: util.cpp:114
HelpMessage
std::string HelpMessage(HelpMessageMode mode)
Help for options shared between UI and daemon (for -help)
Definition: init.cpp:354
GetBoolArg
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
Definition: util.cpp:255
CMasternodeConfig::read
bool read(std::string &strErr)
Definition: masternodeconfig.cpp:21
InitParameterInteraction
void InitParameterInteraction()
Parameter interaction: change current parameters depending on various rules.
Definition: init.cpp:811
masternodeConfig
CMasternodeConfig masternodeConfig
Definition: masternodeconfig.cpp:13
httprpc.h
Shutdown
void Shutdown()
Shutdown is split into 2 parts: Part 1: shut down everything but the main wallet instance (done in Pr...
Definition: init.cpp:274
WaitForShutdown
void WaitForShutdown()
Definition: prcycoind.cpp:46
main.h
AppInit
bool AppInit(int argc, char *argv[])
Definition: prcycoind.cpp:61
ParseParameters
void ParseParameters(int argc, const char *const argv[])
Definition: util.cpp:208
GetDataDir
const fs::path & GetDataDir(bool fNetSpecific)
Definition: util.cpp:349
MilliSleep
void MilliSleep(int64_t n)
Definition: utiltime.cpp:45
noui.h
HMM_BITCOIND
@ HMM_BITCOIND
Definition: init.h:39
httpserver.h
FormatFullVersion
std::string FormatFullVersion()
Definition: clientversion.cpp:80
server.h
mapMultiArgs
std::map< std::string, std::vector< std::string > > mapMultiArgs
Definition: util.cpp:112
util.h
masternodeconfig.h
main
int main(int argc, char *argv[])
Definition: prcycoind.cpp:172