]>
Commit | Line | Data |
---|---|---|
c862d2ff | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2013 The Bitcoin Core developers |
78253fcb | 3 | // Distributed under the MIT software license, see the accompanying |
c862d2ff CF |
4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | ||
71697f97 | 6 | #include "clientversion.h" |
fb78cc23 | 7 | #include "rpcserver.h" |
c862d2ff | 8 | #include "init.h" |
722fa283 | 9 | #include "main.h" |
51ed9ec9 | 10 | #include "noui.h" |
ddd0acd3 | 11 | #include "scheduler.h" |
51ed9ec9 BD |
12 | #include "util.h" |
13 | ||
c862d2ff | 14 | #include <boost/algorithm/string/predicate.hpp> |
51ed9ec9 | 15 | #include <boost/filesystem.hpp> |
ad49c256 | 16 | #include <boost/thread.hpp> |
c862d2ff | 17 | |
0e165b97 WL |
18 | /* Introduction text for doxygen: */ |
19 | ||
20 | /*! \mainpage Developer documentation | |
21 | * | |
22 | * \section intro_sec Introduction | |
23 | * | |
447d37e7 | 24 | * This is the developer documentation of the reference client for an experimental new digital currency called Bitcoin (https://www.bitcoin.org/), |
0e165b97 WL |
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 | ||
8b9adca4 WL |
34 | static bool fDaemon; |
35 | ||
28ee7e8b | 36 | void WaitForShutdown(boost::thread_group* threadGroup) |
c862d2ff | 37 | { |
c55d1600 | 38 | bool fShutdown = ShutdownRequested(); |
c862d2ff | 39 | // Tell the main threads to shutdown. |
c55d1600 | 40 | while (!fShutdown) |
c862d2ff CF |
41 | { |
42 | MilliSleep(200); | |
c55d1600 | 43 | fShutdown = ShutdownRequested(); |
c862d2ff CF |
44 | } |
45 | if (threadGroup) | |
c55d1600 | 46 | { |
c862d2ff | 47 | threadGroup->interrupt_all(); |
c55d1600 PK |
48 | threadGroup->join_all(); |
49 | } | |
c862d2ff CF |
50 | } |
51 | ||
52 | ////////////////////////////////////////////////////////////////////////////// | |
53 | // | |
54 | // Start | |
55 | // | |
7bfc207a | 56 | extern int32_t IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY; |
86ea7309 | 57 | extern std::string NOTARY_PUBKEY; |
58 | ||
c862d2ff CF |
59 | bool AppInit(int argc, char* argv[]) |
60 | { | |
61 | boost::thread_group threadGroup; | |
ddd0acd3 | 62 | CScheduler scheduler; |
c862d2ff CF |
63 | |
64 | bool fRet = false; | |
3d0a1ce1 PJ |
65 | |
66 | // | |
67 | // Parameters | |
68 | // | |
5166804f | 69 | // If Qt is used, parameters/komodo.conf are parsed in qt/bitcoin.cpp's main() |
3d0a1ce1 PJ |
70 | ParseParameters(argc, argv); |
71 | ||
72 | // Process help and version before taking care about datadir | |
af6edac0 | 73 | if (mapArgs.count("-?") || mapArgs.count("-h") || mapArgs.count("-help") || mapArgs.count("-version")) |
3d0a1ce1 | 74 | { |
5166804f | 75 | std::string strUsage = _("Komodo Daemon") + " " + _("version") + " " + FormatFullVersion() + "\n"; |
3d0a1ce1 PJ |
76 | |
77 | if (mapArgs.count("-version")) | |
78 | { | |
79 | strUsage += LicenseInfo(); | |
80 | } | |
81 | else | |
82 | { | |
83 | strUsage += "\n" + _("Usage:") + "\n" + | |
5166804f | 84 | " komodod [options] " + _("Start Komodo Daemon") + "\n"; |
3d0a1ce1 PJ |
85 | |
86 | strUsage += "\n" + HelpMessage(HMM_BITCOIND); | |
87 | } | |
88 | ||
89 | fprintf(stdout, "%s", strUsage.c_str()); | |
90 | return false; | |
91 | } | |
92 | ||
c862d2ff CF |
93 | try |
94 | { | |
c862d2ff CF |
95 | if (!boost::filesystem::is_directory(GetDataDir(false))) |
96 | { | |
a2189fba PK |
97 | fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str()); |
98 | return false; | |
c862d2ff | 99 | } |
4ae5e721 WL |
100 | try |
101 | { | |
102 | ReadConfigFile(mapArgs, mapMultiArgs); | |
27df4123 | 103 | } catch (const std::exception& e) { |
4ae5e721 WL |
104 | fprintf(stderr,"Error reading configuration file: %s\n", e.what()); |
105 | return false; | |
106 | } | |
a3d946eb | 107 | // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause) |
a2189fba PK |
108 | if (!SelectParamsFromCommandLine()) { |
109 | fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n"); | |
110 | return false; | |
111 | } | |
1cf67c70 | 112 | IS_KOMODO_NOTARY = GetBoolArg("-notary", false); |
ee1db778 | 113 | NOTARY_PUBKEY = GetArg("-pubkey", ""); |
7bfc207a | 114 | if ( strlen(NOTARY_PUBKEY.c_str()) == 66 ) |
115 | USE_EXTERNAL_PUBKEY = 1; | |
de8d24dd | 116 | fprintf(stderr,"IS_KOMODO_NOTARY %d %s\n",IS_KOMODO_NOTARY,NOTARY_PUBKEY.c_str()); |
7bfc207a | 117 | |
c862d2ff | 118 | // Command-line RPC |
917ac1dc | 119 | bool fCommandLine = false; |
c862d2ff | 120 | for (int i = 1; i < argc; i++) |
5166804f | 121 | if (!IsSwitchChar(argv[i][0]) && !boost::algorithm::istarts_with(argv[i], "komodo:")) |
c862d2ff CF |
122 | fCommandLine = true; |
123 | ||
124 | if (fCommandLine) | |
125 | { | |
5166804f | 126 | fprintf(stderr, "Error: There is no RPC client functionality in komodod. Use the komodo-cli utility instead.\n"); |
b750cf1f | 127 | exit(1); |
c862d2ff | 128 | } |
998397aa | 129 | |
a034c7eb | 130 | #ifndef WIN32 |
c862d2ff CF |
131 | fDaemon = GetBoolArg("-daemon", false); |
132 | if (fDaemon) | |
133 | { | |
5166804f | 134 | fprintf(stdout, "Komodo server starting\n"); |
8b9adca4 | 135 | |
c862d2ff CF |
136 | // Daemonize |
137 | pid_t pid = fork(); | |
138 | if (pid < 0) | |
139 | { | |
140 | fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno); | |
141 | return false; | |
142 | } | |
143 | if (pid > 0) // Parent process, pid is child process id | |
144 | { | |
c862d2ff CF |
145 | return true; |
146 | } | |
147 | // Child process falls through to rest of initialization | |
148 | ||
149 | pid_t sid = setsid(); | |
150 | if (sid < 0) | |
151 | fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno); | |
152 | } | |
153 | #endif | |
8b9adca4 | 154 | SoftSetBoolArg("-server", true); |
c862d2ff | 155 | |
ddd0acd3 | 156 | fRet = AppInit2(threadGroup, scheduler); |
c862d2ff | 157 | } |
27df4123 | 158 | catch (const std::exception& e) { |
c862d2ff CF |
159 | PrintExceptionContinue(&e, "AppInit()"); |
160 | } catch (...) { | |
161 | PrintExceptionContinue(NULL, "AppInit()"); | |
162 | } | |
c55d1600 PK |
163 | |
164 | if (!fRet) | |
165 | { | |
c862d2ff | 166 | threadGroup.interrupt_all(); |
c55d1600 PK |
167 | // threadGroup.join_all(); was left out intentionally here, because we didn't re-test all of |
168 | // the startup-failure cases to make sure they don't result in a hang due to some | |
169 | // thread-blocking-waiting-for-another-thread-during-startup case | |
28ee7e8b WL |
170 | } else { |
171 | WaitForShutdown(&threadGroup); | |
c862d2ff CF |
172 | } |
173 | Shutdown(); | |
174 | ||
175 | return fRet; | |
176 | } | |
177 | ||
c862d2ff CF |
178 | int main(int argc, char* argv[]) |
179 | { | |
5248ff40 SC |
180 | SetupEnvironment(); |
181 | ||
c862d2ff CF |
182 | // Connect bitcoind signal handlers |
183 | noui_connect(); | |
184 | ||
0b78ba8a | 185 | return (AppInit(argc, argv) ? 0 : 1); |
c862d2ff | 186 | } |