]> Git Repo - VerusCoin.git/blob - src/bitcoind.cpp
Auto merge of #1978 - str4d:1941-scan-whole-chain-on-zkey-import, r=ebfull
[VerusCoin.git] / src / bitcoind.cpp
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.
5
6 #include "clientversion.h"
7 #include "rpcserver.h"
8 #include "init.h"
9 #include "main.h"
10 #include "noui.h"
11 #include "scheduler.h"
12 #include "util.h"
13
14 #include <boost/algorithm/string/predicate.hpp>
15 #include <boost/filesystem.hpp>
16 #include <boost/thread.hpp>
17
18 /* Introduction text for doxygen: */
19
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
34 static bool fDaemon;
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         threadGroup->interrupt_all();
48         threadGroup->join_all();
49     }
50 }
51
52 //////////////////////////////////////////////////////////////////////////////
53 //
54 // Start
55 //
56 bool AppInit(int argc, char* argv[])
57 {
58     boost::thread_group threadGroup;
59     CScheduler scheduler;
60
61     bool fRet = false;
62
63     //
64     // Parameters
65     //
66     // If Qt is used, parameters/zcash.conf are parsed in qt/bitcoin.cpp's main()
67     ParseParameters(argc, argv);
68
69     // Process help and version before taking care about datadir
70     if (mapArgs.count("-?") || mapArgs.count("-h") ||  mapArgs.count("-help") || mapArgs.count("-version"))
71     {
72         std::string strUsage = _("Zcash Daemon") + " " + _("version") + " " + FormatFullVersion() + "\n";
73
74         if (mapArgs.count("-version"))
75         {
76             strUsage += LicenseInfo();
77         }
78         else
79         {
80             strUsage += "\n" + _("Usage:") + "\n" +
81                   "  zcashd [options]                     " + _("Start Zcash Daemon") + "\n";
82
83             strUsage += "\n" + HelpMessage(HMM_BITCOIND);
84         }
85
86         fprintf(stdout, "%s", strUsage.c_str());
87         return false;
88     }
89
90     try
91     {
92         if (!boost::filesystem::is_directory(GetDataDir(false)))
93         {
94             fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str());
95             return false;
96         }
97         try
98         {
99             ReadConfigFile(mapArgs, mapMultiArgs);
100         } catch (const missing_zcash_conf& e) {
101             fprintf(stderr,
102                 (_("Before starting zcashd, you need to create a configuration file:\n"
103                    "%s\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"
108                    "\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");
117             return false;
118         } catch (const std::exception& e) {
119             fprintf(stderr,"Error reading configuration file: %s\n", e.what());
120             return false;
121         }
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");
125             return false;
126         }
127
128         // Command-line RPC
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:"))
132                 fCommandLine = true;
133
134         if (fCommandLine)
135         {
136             fprintf(stderr, "Error: There is no RPC client functionality in zcashd. Use the zcash-cli utility instead.\n");
137             exit(1);
138         }
139 #ifndef WIN32
140         fDaemon = GetBoolArg("-daemon", false);
141         if (fDaemon)
142         {
143             fprintf(stdout, "Zcash server starting\n");
144
145             // Daemonize
146             pid_t pid = fork();
147             if (pid < 0)
148             {
149                 fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
150                 return false;
151             }
152             if (pid > 0) // Parent process, pid is child process id
153             {
154                 return true;
155             }
156             // Child process falls through to rest of initialization
157
158             pid_t sid = setsid();
159             if (sid < 0)
160                 fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
161         }
162 #endif
163         SoftSetBoolArg("-server", true);
164
165         fRet = AppInit2(threadGroup, scheduler);
166     }
167     catch (const std::exception& e) {
168         PrintExceptionContinue(&e, "AppInit()");
169     } catch (...) {
170         PrintExceptionContinue(NULL, "AppInit()");
171     }
172
173     if (!fRet)
174     {
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
179     } else {
180         WaitForShutdown(&threadGroup);
181     }
182     Shutdown();
183
184     return fRet;
185 }
186
187 int main(int argc, char* argv[])
188 {
189     SetupEnvironment();
190
191     // Connect bitcoind signal handlers
192     noui_connect();
193
194     return (AppInit(argc, argv) ? 0 : 1);
195 }
This page took 0.034805 seconds and 4 git commands to generate.