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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2012 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. #include "init.h"
  6. #include "bitcoinrpc.h"
  7. #include <boost/algorithm/string/predicate.hpp>
  8. void DetectShutdownThread(boost::thread_group* threadGroup)
  9. {
  10. bool shutdown = ShutdownRequested();
  11. // Tell the main threads to shutdown.
  12. while (!shutdown)
  13. {
  14. MilliSleep(200);
  15. shutdown = ShutdownRequested();
  16. }
  17. if (threadGroup)
  18. threadGroup->interrupt_all();
  19. }
  20. //////////////////////////////////////////////////////////////////////////////
  21. //
  22. // Start
  23. //
  24. bool AppInit(int argc, char* argv[])
  25. {
  26. boost::thread_group threadGroup;
  27. boost::thread* detectShutdownThread = NULL;
  28. bool fRet = false;
  29. try
  30. {
  31. //
  32. // Parameters
  33. //
  34. // If Qt is used, parameters/bitcoin.conf are parsed in qt/bitcoin.cpp's main()
  35. ParseParameters(argc, argv);
  36. if (!boost::filesystem::is_directory(GetDataDir(false)))
  37. {
  38. fprintf(stderr, "Error: Specified directory does not exist\n");
  39. Shutdown();
  40. }
  41. ReadConfigFile(mapArgs, mapMultiArgs);
  42. if (mapArgs.count("-?") || mapArgs.count("--help"))
  43. {
  44. // First part of help message is specific to bitcoind / RPC client
  45. std::string strUsage = _("Bitcoin version") + " " + FormatFullVersion() + "\n\n" +
  46. _("Usage:") + "\n" +
  47. " bitcoind [options] " + "\n" +
  48. " bitcoind [options] <command> [params] " + _("Send command to -server or bitcoind") + "\n" +
  49. " bitcoind [options] help " + _("List commands") + "\n" +
  50. " bitcoind [options] help <command> " + _("Get help for a command") + "\n";
  51. strUsage += "\n" + HelpMessage();
  52. fprintf(stdout, "%s", strUsage.c_str());
  53. return false;
  54. }
  55. // Command-line RPC
  56. for (int i = 1; i < argc; i++)
  57. if (!IsSwitchChar(argv[i][0]) && !boost::algorithm::istarts_with(argv[i], "bitcoin:"))
  58. fCommandLine = true;
  59. if (fCommandLine)
  60. {
  61. if (!SelectParamsFromCommandLine()) {
  62. fprintf(stderr, "Error: invalid combination of -regtest and -testnet.\n");
  63. return false;
  64. }
  65. int ret = CommandLineRPC(argc, argv);
  66. exit(ret);
  67. }
  68. #if !defined(WIN32)
  69. fDaemon = GetBoolArg("-daemon", false);
  70. if (fDaemon)
  71. {
  72. // Daemonize
  73. pid_t pid = fork();
  74. if (pid < 0)
  75. {
  76. fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
  77. return false;
  78. }
  79. if (pid > 0) // Parent process, pid is child process id
  80. {
  81. CreatePidFile(GetPidFile(), pid);
  82. return true;
  83. }
  84. // Child process falls through to rest of initialization
  85. pid_t sid = setsid();
  86. if (sid < 0)
  87. fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
  88. }
  89. #endif
  90. detectShutdownThread = new boost::thread(boost::bind(&DetectShutdownThread, &threadGroup));
  91. fRet = AppInit2(threadGroup);
  92. }
  93. catch (std::exception& e) {
  94. PrintExceptionContinue(&e, "AppInit()");
  95. } catch (...) {
  96. PrintExceptionContinue(NULL, "AppInit()");
  97. }
  98. if (!fRet) {
  99. if (detectShutdownThread)
  100. detectShutdownThread->interrupt();
  101. threadGroup.interrupt_all();
  102. }
  103. if (detectShutdownThread)
  104. {
  105. detectShutdownThread->join();
  106. delete detectShutdownThread;
  107. detectShutdownThread = NULL;
  108. }
  109. Shutdown();
  110. return fRet;
  111. }
  112. extern void noui_connect();
  113. int main(int argc, char* argv[])
  114. {
  115. bool fRet = false;
  116. fHaveGUI = false;
  117. // Connect bitcoind signal handlers
  118. noui_connect();
  119. fRet = AppInit(argc, argv);
  120. if (fRet && fDaemon)
  121. return 0;
  122. return (fRet ? 0 : 1);
  123. }