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