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.

starwelsd.cpp 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #if defined(HAVE_CONFIG_H)
  6. #include "config/starwels-config.h"
  7. #endif
  8. #include "chainparams.h"
  9. #include "clientversion.h"
  10. #include "compat.h"
  11. #include "fs.h"
  12. #include "rpc/server.h"
  13. #include "init.h"
  14. #include "noui.h"
  15. #include "scheduler.h"
  16. #include "util.h"
  17. #include "httpserver.h"
  18. #include "httprpc.h"
  19. #include "utilstrencodings.h"
  20. #include <boost/thread.hpp>
  21. #include <stdio.h>
  22. /* Introduction text for doxygen: */
  23. /*! \mainpage Developer documentation
  24. *
  25. * \section intro_sec Introduction
  26. *
  27. * This is the developer documentation of the reference client for an experimental new digital currency called Starwels (https://github.com/starwels/),
  28. * which enables instant payments to anyone, anywhere in the world. Starwels uses peer-to-peer technology to operate
  29. * with no central authority: managing transactions and issuing money are carried out collectively by the network.
  30. *
  31. * The software is a community-driven open source project, released under the MIT license.
  32. *
  33. * \section Navigation
  34. * Use the buttons <code>Namespaces</code>, <code>Classes</code> or <code>Files</code> at the top of the page to start navigating the code.
  35. */
  36. void WaitForShutdown(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. Interrupt(*threadGroup);
  48. threadGroup->join_all();
  49. }
  50. }
  51. //////////////////////////////////////////////////////////////////////////////
  52. //
  53. // Start
  54. //
  55. bool AppInit(int argc, char* argv[])
  56. {
  57. boost::thread_group threadGroup;
  58. CScheduler scheduler;
  59. bool fRet = false;
  60. //
  61. // Parameters
  62. //
  63. // If Qt is used, parameters/starwels.conf are parsed in qt/starwels.cpp's main()
  64. gArgs.ParseParameters(argc, argv);
  65. // Process help and version before taking care about datadir
  66. if (gArgs.IsArgSet("-?") || gArgs.IsArgSet("-h") || gArgs.IsArgSet("-help") || gArgs.IsArgSet("-version"))
  67. {
  68. std::string strUsage = strprintf(_("%s Daemon"), _(PACKAGE_NAME)) + " " + _("version") + " " + FormatFullVersion() + "\n";
  69. if (gArgs.IsArgSet("-version"))
  70. {
  71. strUsage += FormatParagraph(LicenseInfo());
  72. }
  73. else
  74. {
  75. strUsage += "\n" + _("Usage:") + "\n" +
  76. " starwelsd [options] " + strprintf(_("Start %s Daemon"), _(PACKAGE_NAME)) + "\n";
  77. strUsage += "\n" + HelpMessage(HMM_STARWELSD);
  78. }
  79. fprintf(stdout, "%s", strUsage.c_str());
  80. return true;
  81. }
  82. try
  83. {
  84. if (!fs::is_directory(GetDataDir(false)))
  85. {
  86. fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
  87. return false;
  88. }
  89. try
  90. {
  91. gArgs.ReadConfigFile(gArgs.GetArg("-conf", STARWELS_CONF_FILENAME));
  92. } catch (const std::exception& e) {
  93. fprintf(stderr,"Error reading configuration file: %s\n", e.what());
  94. return false;
  95. }
  96. // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
  97. try {
  98. SelectParams(ChainNameFromCommandLine());
  99. } catch (const std::exception& e) {
  100. fprintf(stderr, "Error: %s\n", e.what());
  101. return false;
  102. }
  103. // Error out when loose non-argument tokens are encountered on command line
  104. for (int i = 1; i < argc; i++) {
  105. if (!IsSwitchChar(argv[i][0])) {
  106. fprintf(stderr, "Error: Command line contains unexpected token '%s', see starwelsd -h for a list of options.\n", argv[i]);
  107. exit(EXIT_FAILURE);
  108. }
  109. }
  110. // -server defaults to true for starwelsd but not for the GUI so do this here
  111. gArgs.SoftSetBoolArg("-server", true);
  112. // Set this early so that parameter interactions go to console
  113. InitLogging();
  114. InitParameterInteraction();
  115. if (!AppInitBasicSetup())
  116. {
  117. // InitError will have been called with detailed error, which ends up on console
  118. exit(EXIT_FAILURE);
  119. }
  120. if (!AppInitParameterInteraction())
  121. {
  122. // InitError will have been called with detailed error, which ends up on console
  123. exit(EXIT_FAILURE);
  124. }
  125. if (!AppInitSanityChecks())
  126. {
  127. // InitError will have been called with detailed error, which ends up on console
  128. exit(EXIT_FAILURE);
  129. }
  130. if (gArgs.GetBoolArg("-daemon", false))
  131. {
  132. #if HAVE_DECL_DAEMON
  133. fprintf(stdout, "Starwels server starting\n");
  134. // Daemonize
  135. if (daemon(1, 0)) { // don't chdir (1), do close FDs (0)
  136. fprintf(stderr, "Error: daemon() failed: %s\n", strerror(errno));
  137. return false;
  138. }
  139. #else
  140. fprintf(stderr, "Error: -daemon is not supported on this operating system\n");
  141. return false;
  142. #endif // HAVE_DECL_DAEMON
  143. }
  144. // Lock data directory after daemonization
  145. if (!AppInitLockDataDirectory())
  146. {
  147. // If locking the data directory failed, exit immediately
  148. exit(EXIT_FAILURE);
  149. }
  150. fRet = AppInitMain(threadGroup, scheduler);
  151. }
  152. catch (const std::exception& e) {
  153. PrintExceptionContinue(&e, "AppInit()");
  154. } catch (...) {
  155. PrintExceptionContinue(nullptr, "AppInit()");
  156. }
  157. if (!fRet)
  158. {
  159. Interrupt(threadGroup);
  160. threadGroup.join_all();
  161. } else {
  162. WaitForShutdown(&threadGroup);
  163. }
  164. Shutdown();
  165. return fRet;
  166. }
  167. int main(int argc, char* argv[])
  168. {
  169. SetupEnvironment();
  170. // Connect starwelsd signal handlers
  171. noui_connect();
  172. return (AppInit(argc, argv) ? EXIT_SUCCESS : EXIT_FAILURE);
  173. }