Anoncoin  0.9.4
P2P Digital Currency
anoncoind.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Copyright (c) 2013-2014 The Anoncoin Core developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include "rpcserver.h"
8 #include "rpcclient.h"
9 #include "init.h"
10 #include "main.h"
11 #include "noui.h"
12 #include "ui_interface.h"
13 #include "util.h"
14 
15 #include <boost/algorithm/string/predicate.hpp>
16 #include <boost/filesystem.hpp>
17 
18 /* Introduction text for doxygen: */
19 
34 static bool fDaemon;
35 
36 void DetectShutdownThread(boost::thread_group* threadGroup)
37 {
38  bool fShutdown = ShutdownRequested();
39  // Tell the main threads to shutdown.
40  while (!fShutdown)
41  {
42  MilliSleep(200);
43  fShutdown = ShutdownRequested();
44  }
45  if (threadGroup)
46  {
47  threadGroup->interrupt_all();
48  threadGroup->join_all();
49  }
50 }
51 
53 //
54 // Start
55 //
56 bool AppInit(int argc, char* argv[])
57 {
58  boost::thread_group threadGroup;
59  boost::thread* detectShutdownThread = NULL;
60 
61  bool fRet = false;
62  try
63  {
64  //
65  // Parameters
66  //
67  // If Qt is used, parameters/anoncoin.conf are parsed in qt/anoncoin.cpp's main()
68  ParseParameters(argc, argv);
69  if (!boost::filesystem::is_directory(GetDataDir(false)))
70  {
71  fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str());
72  return false;
73  }
74  try
75  {
77  } catch(std::exception &e) {
78  fprintf(stderr,"Error reading configuration file: %s\n", e.what());
79  return false;
80  }
81  // Check for -testnet or -regtest parameter (TestNet() calls are only valid after this clause)
83  fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n");
84  return false;
85  }
86 
87  if (mapArgs.count("-?") || mapArgs.count("--help"))
88  {
89  // First part of help message is specific to anoncoind / RPC client
90  std::string strUsage = _("Anoncoin Core Daemon") + " " + _("version") + " " + FormatFullVersion() + "\n\n" +
91  _("Usage:") + "\n" +
92  " anoncoind [options] " + _("Start Anoncoin Core Daemon") + "\n" +
93  _("Usage (deprecated, use anoncoin-cli):") + "\n" +
94  " anoncoind [options] <command> [params] " + _("Send command to Anoncoin Core") + "\n" +
95  " anoncoind [options] help " + _("List commands") + "\n" +
96  " anoncoind [options] help <command> " + _("Get help for a command") + "\n";
97 
98  strUsage += "\n" + HelpMessage(HMM_ANONCOIND);
99  strUsage += "\n" + HelpMessageCli(false);
100 
101  fprintf(stdout, "%s", strUsage.c_str());
102  return false;
103  }
104 
105  // Command-line RPC
106  bool fCommandLine = false;
107  for (int i = 1; i < argc; i++)
108  if (!IsSwitchChar(argv[i][0]) && !boost::algorithm::istarts_with(argv[i], "anoncoin:"))
109  fCommandLine = true;
110 
111  if (fCommandLine)
112  {
113  int ret = CommandLineRPC(argc, argv);
114  exit(ret);
115  }
116 #ifndef WIN32
117  fDaemon = GetBoolArg("-daemon", false);
118  if (fDaemon)
119  {
120  fprintf(stdout, "Anoncoin server starting\n");
121 
122  // Daemonize
123  pid_t pid = fork();
124  if (pid < 0)
125  {
126  fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
127  return false;
128  }
129  if (pid > 0) // Parent process, pid is child process id
130  {
131  CreatePidFile(GetPidFile(), pid);
132  return true;
133  }
134  // Child process falls through to rest of initialization
135 
136  pid_t sid = setsid();
137  if (sid < 0)
138  fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
139  }
140 #endif
141  SoftSetBoolArg("-server", true);
142 
143  detectShutdownThread = new boost::thread(boost::bind(&DetectShutdownThread, &threadGroup));
144  fRet = AppInit2(threadGroup);
145  }
146  catch (std::exception& e) {
147  PrintExceptionContinue(&e, "AppInit()");
148  } catch (...) {
149  PrintExceptionContinue(NULL, "AppInit()");
150  }
151 
152  if (!fRet)
153  {
154  if (detectShutdownThread)
155  detectShutdownThread->interrupt();
156 
157  threadGroup.interrupt_all();
158  // threadGroup.join_all(); was left out intentionally here, because we didn't re-test all of
159  // the startup-failure cases to make sure they don't result in a hang due to some
160  // thread-blocking-waiting-for-another-thread-during-startup case
161  }
162 
163  if (detectShutdownThread)
164  {
165  detectShutdownThread->join();
166  delete detectShutdownThread;
167  detectShutdownThread = NULL;
168  }
169  Shutdown();
170 
171  return fRet;
172 }
173 
174 int main(int argc, char* argv[])
175 {
177 
178  bool fRet = false;
179 
180  // Connect anoncoind signal handlers
181  noui_connect();
182 
183  fRet = AppInit(argc, argv);
184 
185  if (fRet && fDaemon)
186  return 0;
187 
188  return (fRet ? 0 : 1);
189 }
const boost::filesystem::path & GetDataDir(bool fNetSpecific)
Definition: util.cpp:968
std::string HelpMessageCli(bool mainProgram)
Show help message for anoncoin-cli.
Definition: rpcclient.cpp:284
void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
Definition: util.cpp:1058
void MilliSleep(int64_t n)
Definition: util.h:80
bool ShutdownRequested()
Definition: init.cpp:107
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn't already have a value.
Definition: util.cpp:539
boost::filesystem::path GetPidFile()
Definition: util.cpp:1050
bool SelectParamsFromCommandLine()
Looks for -regtest or -testnet and then calls SelectParams as appropriate.
void noui_connect()
Definition: noui.cpp:65
std::string HelpMessage(HelpMessageMode hmm)
Definition: init.cpp:197
int main(int argc, char *argv[])
Definition: anoncoind.cpp:174
bool fDaemon
Definition: util.cpp:94
bool AppInit(int argc, char *argv[])
Definition: anoncoind.cpp:56
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
Definition: util.cpp:520
bool AppInit2(boost::thread_group &threadGroup)
Initialize anoncoin.
Definition: init.cpp:420
void Shutdown()
Definition: init.cpp:114
void PrintExceptionContinue(std::exception *pex, const char *pszThread)
Definition: util.cpp:928
int CommandLineRPC(int argc, char *argv[])
Definition: rpcclient.cpp:218
void ParseParameters(int argc, const char *const argv[])
Definition: util.cpp:460
std::string FormatFullVersion()
bool IsSwitchChar(char c)
Definition: util.h:325
void ReadConfigFile(map< string, string > &mapSettingsRet, map< string, vector< string > > &mapMultiSettingsRet)
Definition: util.cpp:1021
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a boost::optional result...
Definition: ui_interface.h:125
void DetectShutdownThread(boost::thread_group *threadGroup)
Definition: anoncoind.cpp:36
void SetupEnvironment()
Definition: util.cpp:1394
map< string, vector< string > > mapMultiArgs
Definition: util.cpp:90
map< string, string > mapArgs
Definition: util.cpp:89