1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 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.
6 #include "clientversion.h"
11 #include "scheduler.h"
14 #include <boost/algorithm/string/predicate.hpp>
15 #include <boost/filesystem.hpp>
16 #include <boost/thread.hpp>
18 /* Introduction text for doxygen: */
20 /*! \mainpage Developer documentation
22 * \section intro_sec Introduction
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.
28 * The software is a community-driven open source project, released under the MIT license.
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.
36 void WaitForShutdown(boost::thread_group* threadGroup)
38 bool fShutdown = ShutdownRequested();
39 // Tell the main threads to shutdown.
43 fShutdown = ShutdownRequested();
47 threadGroup->interrupt_all();
48 threadGroup->join_all();
52 //////////////////////////////////////////////////////////////////////////////
56 bool AppInit(int argc, char* argv[])
58 boost::thread_group threadGroup;
66 // If Qt is used, parameters/zcash.conf are parsed in qt/bitcoin.cpp's main()
67 ParseParameters(argc, argv);
69 // Process help and version before taking care about datadir
70 if (mapArgs.count("-?") || mapArgs.count("-h") || mapArgs.count("-help") || mapArgs.count("-version"))
72 std::string strUsage = _("Zcash Daemon") + " " + _("version") + " " + FormatFullVersion() + "\n";
74 if (mapArgs.count("-version"))
76 strUsage += LicenseInfo();
80 strUsage += "\n" + _("Usage:") + "\n" +
81 " zcashd [options] " + _("Start Zcash Daemon") + "\n";
83 strUsage += "\n" + HelpMessage(HMM_BITCOIND);
86 fprintf(stdout, "%s", strUsage.c_str());
92 if (!boost::filesystem::is_directory(GetDataDir(false)))
94 fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str());
99 ReadConfigFile(mapArgs, mapMultiArgs);
100 } catch (const missing_zcash_conf& e) {
102 (_("Before starting zcashd, you need to create a configuration file:\n"
104 "It can be completely empty! That indicates you are happy with the default\n"
105 "configuration of zcashd. But requiring a configuration file to start ensures\n"
106 "that zcashd won't accidentally compromise your privacy if there was a default\n"
107 "option you needed to change.\n"
109 "You can look at the example configuration file for suggestions of default\n"
110 "options that you may want to change. It should be in one of these locations,\n"
111 "depending on how you installed Zcash:\n") +
112 _("- Source code: %s\n"
113 "- .deb package: %s\n")).c_str(),
114 GetConfigFile().string().c_str(),
115 "contrib/DEBIAN/examples/zcash.conf",
116 "/usr/share/doc/zcash/examples/zcash.conf");
118 } catch (const std::exception& e) {
119 fprintf(stderr,"Error reading configuration file: %s\n", e.what());
122 // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
123 if (!SelectParamsFromCommandLine()) {
124 fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n");
129 bool fCommandLine = false;
130 for (int i = 1; i < argc; i++)
131 if (!IsSwitchChar(argv[i][0]) && !boost::algorithm::istarts_with(argv[i], "zcash:"))
136 fprintf(stderr, "Error: There is no RPC client functionality in zcashd. Use the zcash-cli utility instead.\n");
140 fDaemon = GetBoolArg("-daemon", false);
143 fprintf(stdout, "Zcash server starting\n");
149 fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
152 if (pid > 0) // Parent process, pid is child process id
156 // Child process falls through to rest of initialization
158 pid_t sid = setsid();
160 fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
163 SoftSetBoolArg("-server", true);
165 fRet = AppInit2(threadGroup, scheduler);
167 catch (const std::exception& e) {
168 PrintExceptionContinue(&e, "AppInit()");
170 PrintExceptionContinue(NULL, "AppInit()");
175 threadGroup.interrupt_all();
176 // threadGroup.join_all(); was left out intentionally here, because we didn't re-test all of
177 // the startup-failure cases to make sure they don't result in a hang due to some
178 // thread-blocking-waiting-for-another-thread-during-startup case
180 WaitForShutdown(&threadGroup);
187 int main(int argc, char* argv[])
191 // Connect bitcoind signal handlers
194 return (AppInit(argc, argv) ? 0 : 1);