You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bitcoind.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2015 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. #include "chainparams.h"
  6. #include "clientversion.h"
  7. #include "rpcserver.h"
  8. #include "init.h"
  9. #include "noui.h"
  10. #include "scheduler.h"
  11. #include "util.h"
  12. #include "httpserver.h"
  13. #include "httprpc.h"
  14. #include "rpcserver.h"
  15. #include <boost/algorithm/string/predicate.hpp>
  16. #include <boost/filesystem.hpp>
  17. #include <boost/thread.hpp>
  18. #include <stdio.h>
  19. /* Introduction text for doxygen: */
  20. /*! \mainpage Developer documentation
  21. *
  22. * \section intro_sec Introduction
  23. *
  24. * This is the developer documentation of the reference client for an experimental new digital currency called Bitcoin (https://www.bitcoin.org/),
  25. * which enables instant payments to anyone, anywhere in the world. Bitcoin uses peer-to-peer technology to operate
  26. * with no central authority: managing transactions and issuing money are carried out collectively by the network.
  27. *
  28. * The software is a community-driven open source project, released under the MIT license.
  29. *
  30. * \section Navigation
  31. * Use the buttons <code>Namespaces</code>, <code>Classes</code> or <code>Files</code> at the top of the page to start navigating the code.
  32. */
  33. static bool fDaemon;
  34. void WaitForShutdown(boost::thread_group* threadGroup)
  35. {
  36. bool fShutdown = ShutdownRequested();
  37. // Tell the main threads to shutdown.
  38. while (!fShutdown)
  39. {
  40. MilliSleep(200);
  41. fShutdown = ShutdownRequested();
  42. }
  43. if (threadGroup)
  44. {
  45. Interrupt(*threadGroup);
  46. threadGroup->join_all();
  47. }
  48. }
  49. //////////////////////////////////////////////////////////////////////////////
  50. //
  51. // Start
  52. //
  53. bool AppInit(int argc, char* argv[])
  54. {
  55. boost::thread_group threadGroup;
  56. CScheduler scheduler;
  57. bool fRet = false;
  58. //
  59. // Parameters
  60. //
  61. // If Qt is used, parameters/bitcoin.conf are parsed in qt/bitcoin.cpp's main()
  62. ParseParameters(argc, argv);
  63. // Process help and version before taking care about datadir
  64. if (mapArgs.count("-?") || mapArgs.count("-h") || mapArgs.count("-help") || mapArgs.count("-version"))
  65. {
  66. std::string strUsage = _("Bitcoin Core Daemon") + " " + _("version") + " " + FormatFullVersion() + "\n";
  67. if (mapArgs.count("-version"))
  68. {
  69. strUsage += LicenseInfo();
  70. }
  71. else
  72. {
  73. strUsage += "\n" + _("Usage:") + "\n" +
  74. " bitcoind [options] " + _("Start Bitcoin Core Daemon") + "\n";
  75. strUsage += "\n" + HelpMessage(HMM_BITCOIND);
  76. }
  77. fprintf(stdout, "%s", strUsage.c_str());
  78. return false;
  79. }
  80. try
  81. {
  82. if (!boost::filesystem::is_directory(GetDataDir(false)))
  83. {
  84. fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str());
  85. return false;
  86. }
  87. try
  88. {
  89. ReadConfigFile(mapArgs, mapMultiArgs);
  90. } catch (const std::exception& e) {
  91. fprintf(stderr,"Error reading configuration file: %s\n", e.what());
  92. return false;
  93. }
  94. // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
  95. try {
  96. SelectParams(ChainNameFromCommandLine());
  97. } catch (const std::exception& e) {
  98. fprintf(stderr, "Error: %s\n", e.what());
  99. return false;
  100. }
  101. // Command-line RPC
  102. bool fCommandLine = false;
  103. for (int i = 1; i < argc; i++)
  104. if (!IsSwitchChar(argv[i][0]) && !boost::algorithm::istarts_with(argv[i], "bitcoin:"))
  105. fCommandLine = true;
  106. if (fCommandLine)
  107. {
  108. fprintf(stderr, "Error: There is no RPC client functionality in bitcoind anymore. Use the bitcoin-cli utility instead.\n");
  109. exit(1);
  110. }
  111. #ifndef WIN32
  112. fDaemon = GetBoolArg("-daemon", false);
  113. if (fDaemon)
  114. {
  115. fprintf(stdout, "Bitcoin server starting\n");
  116. // Daemonize
  117. pid_t pid = fork();
  118. if (pid < 0)
  119. {
  120. fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
  121. return false;
  122. }
  123. if (pid > 0) // Parent process, pid is child process id
  124. {
  125. return true;
  126. }
  127. // Child process falls through to rest of initialization
  128. pid_t sid = setsid();
  129. if (sid < 0)
  130. fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
  131. }
  132. #endif
  133. SoftSetBoolArg("-server", true);
  134. // Set this early so that parameter interactions go to console
  135. InitLogging();
  136. InitParameterInteraction();
  137. fRet = AppInit2(threadGroup, scheduler);
  138. }
  139. catch (const std::exception& e) {
  140. PrintExceptionContinue(&e, "AppInit()");
  141. } catch (...) {
  142. PrintExceptionContinue(NULL, "AppInit()");
  143. }
  144. if (!fRet)
  145. {
  146. Interrupt(threadGroup);
  147. // threadGroup.join_all(); was left out intentionally here, because we didn't re-test all of
  148. // the startup-failure cases to make sure they don't result in a hang due to some
  149. // thread-blocking-waiting-for-another-thread-during-startup case
  150. } else {
  151. WaitForShutdown(&threadGroup);
  152. }
  153. Shutdown();
  154. return fRet;
  155. }
  156. int main(int argc, char* argv[])
  157. {
  158. SetupEnvironment();
  159. // Connect bitcoind signal handlers
  160. noui_connect();
  161. return (AppInit(argc, argv) ? 0 : 1);
  162. }