]>
Commit | Line | Data |
---|---|---|
2097c09a | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
c63a73d1 | 3 | // Distributed under the MIT software license, see the accompanying |
3a25a2b9 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
36949554 | 5 | |
4c61ba40 WL |
6 | #if defined(HAVE_CONFIG_H) |
7 | #include "config/bitcoin-config.h" | |
8 | #endif | |
9 | ||
51ed9ec9 BD |
10 | #include "util.h" |
11 | ||
84ce18ca | 12 | #include "chainparamsbase.h" |
6354935c | 13 | #include "random.h" |
ad49c256 | 14 | #include "serialize.h" |
51ed9ec9 | 15 | #include "sync.h" |
ad49c256 WL |
16 | #include "utilstrencodings.h" |
17 | #include "utiltime.h" | |
51ed9ec9 | 18 | |
51ed9ec9 | 19 | #include <stdarg.h> |
51ed9ec9 | 20 | |
5207f33f PK |
21 | #if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)) |
22 | #include <pthread.h> | |
23 | #include <pthread_np.h> | |
24 | #endif | |
25 | ||
288fdc09 PW |
26 | #ifndef WIN32 |
27 | // for posix_fallocate | |
76c49c41 | 28 | #ifdef __linux__ |
51ed9ec9 BD |
29 | |
30 | #ifdef _POSIX_C_SOURCE | |
31 | #undef _POSIX_C_SOURCE | |
32 | #endif | |
33 | ||
288fdc09 | 34 | #define _POSIX_C_SOURCE 200112L |
51ed9ec9 | 35 | |
76c49c41 | 36 | #endif // __linux__ |
51ed9ec9 | 37 | |
b94595bb | 38 | #include <algorithm> |
288fdc09 | 39 | #include <fcntl.h> |
ba29a559 | 40 | #include <sys/resource.h> |
51ed9ec9 | 41 | #include <sys/stat.h> |
1f29d399 | 42 | |
51ed9ec9 | 43 | #else |
ed6d0b5f | 44 | |
ed6d0b5f PW |
45 | #ifdef _MSC_VER |
46 | #pragma warning(disable:4786) | |
47 | #pragma warning(disable:4804) | |
48 | #pragma warning(disable:4805) | |
49 | #pragma warning(disable:4717) | |
50 | #endif | |
51ed9ec9 | 51 | |
ed6d0b5f PW |
52 | #ifdef _WIN32_WINNT |
53 | #undef _WIN32_WINNT | |
54 | #endif | |
55 | #define _WIN32_WINNT 0x0501 | |
51ed9ec9 | 56 | |
ed6d0b5f PW |
57 | #ifdef _WIN32_IE |
58 | #undef _WIN32_IE | |
59 | #endif | |
234db30d | 60 | #define _WIN32_IE 0x0501 |
51ed9ec9 | 61 | |
ed6d0b5f PW |
62 | #define WIN32_LEAN_AND_MEAN 1 |
63 | #ifndef NOMINMAX | |
64 | #define NOMINMAX | |
65 | #endif | |
51ed9ec9 | 66 | |
5f986195 | 67 | #include <io.h> /* for _commit */ |
51ed9ec9 | 68 | #include <shlobj.h> |
ed6d0b5f | 69 | #endif |
2097c09a | 70 | |
4c61ba40 WL |
71 | #ifdef HAVE_SYS_PRCTL_H |
72 | #include <sys/prctl.h> | |
73 | #endif | |
74 | ||
51ed9ec9 BD |
75 | #include <boost/algorithm/string/case_conv.hpp> // for to_lower() |
76 | #include <boost/algorithm/string/join.hpp> | |
77 | #include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith() | |
78 | #include <boost/filesystem.hpp> | |
79 | #include <boost/filesystem/fstream.hpp> | |
80 | #include <boost/foreach.hpp> | |
81 | #include <boost/program_options/detail/config_file.hpp> | |
82 | #include <boost/program_options/parsers.hpp> | |
ad49c256 | 83 | #include <boost/thread.hpp> |
51ed9ec9 BD |
84 | #include <openssl/crypto.h> |
85 | #include <openssl/rand.h> | |
25314386 | 86 | #include <openssl/conf.h> |
51ed9ec9 BD |
87 | |
88 | // Work around clang compilation problem in Boost 1.46: | |
89 | // /usr/include/boost/program_options/detail/config_file.hpp:163:17: error: call to function 'to_internal' that is neither visible in the template definition nor found by argument-dependent lookup | |
90 | // See also: http://stackoverflow.com/questions/10020179/compilation-fail-in-boost-librairies-program-options | |
91 | // http://clang.debian.net/status.php?version=3.0&key=CANNOT_FIND_FUNCTION | |
92 | namespace boost { | |
e10dcf27 | 93 | |
51ed9ec9 BD |
94 | namespace program_options { |
95 | std::string to_internal(const std::string&); | |
96 | } | |
51ed9ec9 | 97 | |
e10dcf27 | 98 | } // namespace boost |
51ed9ec9 | 99 | |
2097c09a WL |
100 | using namespace std; |
101 | ||
102 | map<string, string> mapArgs; | |
103 | map<string, vector<string> > mapMultiArgs; | |
104 | bool fDebug = false; | |
105 | bool fPrintToConsole = false; | |
9e9056cd | 106 | bool fPrintToDebugLog = true; |
2097c09a WL |
107 | bool fDaemon = false; |
108 | bool fServer = false; | |
2097c09a | 109 | string strMiscWarning; |
7a4e0e09 CD |
110 | bool fLogTimestamps = DEFAULT_LOGTIMESTAMPS; |
111 | bool fLogTimeMicros = DEFAULT_LOGTIMEMICROS; | |
112 | bool fLogIPs = DEFAULT_LOGIPS; | |
bf673640 | 113 | std::atomic<bool> fReopenDebugLog(false); |
b74dcb3b | 114 | CTranslationInterface translationInterface; |
2097c09a | 115 | |
c63a73d1 | 116 | /** Init OpenSSL library multithreading support */ |
7f3ccb59 | 117 | static CCriticalSection** ppmutexOpenSSL; |
60457d3c | 118 | void locking_callback(int mode, int i, const char* file, int line) NO_THREAD_SAFETY_ANALYSIS |
2097c09a | 119 | { |
7f3ccb59 PW |
120 | if (mode & CRYPTO_LOCK) { |
121 | ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]); | |
122 | } else { | |
123 | LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]); | |
124 | } | |
2097c09a WL |
125 | } |
126 | ||
127 | // Init | |
128 | class CInit | |
129 | { | |
130 | public: | |
131 | CInit() | |
132 | { | |
a7f82808 | 133 | // Init OpenSSL library multithreading support |
7f3ccb59 | 134 | ppmutexOpenSSL = (CCriticalSection**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(CCriticalSection*)); |
2097c09a | 135 | for (int i = 0; i < CRYPTO_num_locks(); i++) |
7f3ccb59 | 136 | ppmutexOpenSSL[i] = new CCriticalSection(); |
2097c09a WL |
137 | CRYPTO_set_locking_callback(locking_callback); |
138 | ||
25314386 CF |
139 | // OpenSSL can optionally load a config file which lists optional loadable modules and engines. |
140 | // We don't use them so we don't require the config. However some of our libs may call functions | |
141 | // which attempt to load the config file, possibly resulting in an exit() or crash if it is missing | |
142 | // or corrupt. Explicitly tell OpenSSL not to try to load the file. The result for our libs will be | |
143 | // that the config appears to have been loaded and there are no modules/engines available. | |
144 | OPENSSL_no_config(); | |
145 | ||
6853e627 | 146 | #ifdef WIN32 |
cf04d836 | 147 | // Seed OpenSSL PRNG with current contents of the screen |
2097c09a WL |
148 | RAND_screen(); |
149 | #endif | |
150 | ||
cf04d836 | 151 | // Seed OpenSSL PRNG with performance counter |
2097c09a WL |
152 | RandAddSeed(); |
153 | } | |
154 | ~CInit() | |
155 | { | |
cf04d836 PK |
156 | // Securely erase the memory used by the PRNG |
157 | RAND_cleanup(); | |
a7f82808 | 158 | // Shutdown OpenSSL library multithreading support |
2097c09a WL |
159 | CRYPTO_set_locking_callback(NULL); |
160 | for (int i = 0; i < CRYPTO_num_locks(); i++) | |
161 | delete ppmutexOpenSSL[i]; | |
162 | OPENSSL_free(ppmutexOpenSSL); | |
163 | } | |
164 | } | |
165 | instance_of_cinit; | |
166 | ||
c63a73d1 MF |
167 | /** |
168 | * LogPrintf() has been broken a couple of times now | |
169 | * by well-meaning people adding mutexes in the most straightforward way. | |
170 | * It breaks because it may be called by global destructors during shutdown. | |
171 | * Since the order of destruction of static/global objects is undefined, | |
172 | * defining a mutex as a global object doesn't work (the mutex gets | |
173 | * destroyed, and then some later destructor calls OutputDebugStringF, | |
174 | * maybe indirectly, and you get a core dump at shutdown trying to lock | |
175 | * the mutex). | |
176 | */ | |
2097c09a | 177 | |
ee337423 | 178 | static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT; |
c63a73d1 MF |
179 | /** |
180 | * We use boost::call_once() to make sure these are initialized | |
181 | * in a thread-safe manner the first time called: | |
182 | */ | |
ee337423 GA |
183 | static FILE* fileout = NULL; |
184 | static boost::mutex* mutexDebugLog = NULL; | |
185 | ||
186 | static void DebugPrintInit() | |
2097c09a | 187 | { |
ee337423 GA |
188 | assert(fileout == NULL); |
189 | assert(mutexDebugLog == NULL); | |
190 | ||
191 | boost::filesystem::path pathDebug = GetDataDir() / "debug.log"; | |
192 | fileout = fopen(pathDebug.string().c_str(), "a"); | |
193 | if (fileout) setbuf(fileout, NULL); // unbuffered | |
194 | ||
195 | mutexDebugLog = new boost::mutex(); | |
196 | } | |
197 | ||
b77dfdc9 | 198 | bool LogAcceptCategory(const char* category) |
ee337423 | 199 | { |
e51321fb GA |
200 | if (category != NULL) |
201 | { | |
3b570559 | 202 | if (!fDebug) |
b77dfdc9 | 203 | return false; |
3b570559 | 204 | |
0b238b27 GA |
205 | // Give each thread quick access to -debug settings. |
206 | // This helps prevent issues debugging global destructors, | |
207 | // where mapMultiArgs might be deleted before another | |
208 | // global destructor calls LogPrint() | |
209 | static boost::thread_specific_ptr<set<string> > ptrCategory; | |
210 | if (ptrCategory.get() == NULL) | |
3b570559 | 211 | { |
0b238b27 GA |
212 | const vector<string>& categories = mapMultiArgs["-debug"]; |
213 | ptrCategory.reset(new set<string>(categories.begin(), categories.end())); | |
214 | // thread_specific_ptr automatically deletes the set when the thread ends. | |
3b570559 | 215 | } |
0b238b27 GA |
216 | const set<string>& setCategories = *ptrCategory.get(); |
217 | ||
218 | // if not debugging everything and not debugging specific category, LogPrint does nothing. | |
219 | if (setCategories.count(string("")) == 0 && | |
220 | setCategories.count(string(category)) == 0) | |
b77dfdc9 | 221 | return false; |
e51321fb | 222 | } |
b77dfdc9 WL |
223 | return true; |
224 | } | |
e51321fb | 225 | |
b77dfdc9 WL |
226 | int LogPrintStr(const std::string &str) |
227 | { | |
ee337423 | 228 | int ret = 0; // Returns total number of characters written |
2097c09a WL |
229 | if (fPrintToConsole) |
230 | { | |
231 | // print to console | |
b77dfdc9 | 232 | ret = fwrite(str.data(), 1, str.size(), stdout); |
e982b574 | 233 | fflush(stdout); |
2097c09a | 234 | } |
96ff9d64 | 235 | else if (fPrintToDebugLog && AreBaseParamsConfigured()) |
2097c09a | 236 | { |
ee337423 GA |
237 | static bool fStartedNewLine = true; |
238 | boost::call_once(&DebugPrintInit, debugPrintInitFlag); | |
2097c09a | 239 | |
ee337423 GA |
240 | if (fileout == NULL) |
241 | return ret; | |
242 | ||
243 | boost::mutex::scoped_lock scoped_lock(*mutexDebugLog); | |
244 | ||
245 | // reopen the log file, if requested | |
246 | if (fReopenDebugLog) { | |
247 | fReopenDebugLog = false; | |
ee12c3d6 | 248 | boost::filesystem::path pathDebug = GetDataDir() / "debug.log"; |
ee337423 GA |
249 | if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL) |
250 | setbuf(fileout, NULL); // unbuffered | |
2097c09a | 251 | } |
2097c09a | 252 | |
ee337423 GA |
253 | // Debug print useful for profiling |
254 | if (fLogTimestamps && fStartedNewLine) | |
255 | ret += fprintf(fileout, "%s ", DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()).c_str()); | |
b77dfdc9 | 256 | if (!str.empty() && str[str.size()-1] == '\n') |
ee337423 GA |
257 | fStartedNewLine = true; |
258 | else | |
259 | fStartedNewLine = false; | |
2097c09a | 260 | |
b77dfdc9 | 261 | ret = fwrite(str.data(), 1, str.size(), fileout); |
2097c09a WL |
262 | } |
263 | ||
2097c09a WL |
264 | return ret; |
265 | } | |
266 | ||
d64e124c CM |
267 | static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet) |
268 | { | |
269 | // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set | |
270 | if (name.find("-no") == 0) | |
271 | { | |
272 | std::string positive("-"); | |
273 | positive.append(name.begin()+3, name.end()); | |
274 | if (mapSettingsRet.count(positive) == 0) | |
275 | { | |
3260b4c0 | 276 | bool value = !GetBoolArg(name, false); |
d64e124c CM |
277 | mapSettingsRet[positive] = (value ? "1" : "0"); |
278 | } | |
279 | } | |
280 | } | |
281 | ||
3e468840 | 282 | void ParseParameters(int argc, const char* const argv[]) |
2097c09a WL |
283 | { |
284 | mapArgs.clear(); | |
285 | mapMultiArgs.clear(); | |
71aaff39 | 286 | |
2097c09a WL |
287 | for (int i = 1; i < argc; i++) |
288 | { | |
6032e4f4 WL |
289 | std::string str(argv[i]); |
290 | std::string strValue; | |
291 | size_t is_index = str.find('='); | |
292 | if (is_index != std::string::npos) | |
2097c09a | 293 | { |
6032e4f4 WL |
294 | strValue = str.substr(is_index+1); |
295 | str = str.substr(0, is_index); | |
2097c09a | 296 | } |
6032e4f4 WL |
297 | #ifdef WIN32 |
298 | boost::to_lower(str); | |
299 | if (boost::algorithm::starts_with(str, "/")) | |
300 | str = "-" + str.substr(1); | |
301 | #endif | |
71aaff39 | 302 | |
6032e4f4 | 303 | if (str[0] != '-') |
2097c09a | 304 | break; |
3ad9f8a7 | 305 | |
71aaff39 KZ |
306 | // Interpret --foo as -foo. |
307 | // If both --foo and -foo are set, the last takes effect. | |
308 | if (str.length() > 1 && str[1] == '-') | |
309 | str = str.substr(1); | |
310 | ||
6032e4f4 WL |
311 | mapArgs[str] = strValue; |
312 | mapMultiArgs[str].push_back(strValue); | |
2097c09a | 313 | } |
3ad9f8a7 GA |
314 | |
315 | // New 0.6 features: | |
316 | BOOST_FOREACH(const PAIRTYPE(string,string)& entry, mapArgs) | |
317 | { | |
d64e124c | 318 | // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set |
71aaff39 | 319 | InterpretNegativeSetting(entry.first, mapArgs); |
3ad9f8a7 | 320 | } |
2097c09a WL |
321 | } |
322 | ||
3ae07355 GA |
323 | std::string GetArg(const std::string& strArg, const std::string& strDefault) |
324 | { | |
325 | if (mapArgs.count(strArg)) | |
326 | return mapArgs[strArg]; | |
327 | return strDefault; | |
328 | } | |
329 | ||
51ed9ec9 | 330 | int64_t GetArg(const std::string& strArg, int64_t nDefault) |
3ae07355 GA |
331 | { |
332 | if (mapArgs.count(strArg)) | |
333 | return atoi64(mapArgs[strArg]); | |
334 | return nDefault; | |
335 | } | |
336 | ||
337 | bool GetBoolArg(const std::string& strArg, bool fDefault) | |
338 | { | |
339 | if (mapArgs.count(strArg)) | |
340 | { | |
341 | if (mapArgs[strArg].empty()) | |
342 | return true; | |
343 | return (atoi(mapArgs[strArg]) != 0); | |
344 | } | |
345 | return fDefault; | |
346 | } | |
347 | ||
0fcf91ea GA |
348 | bool SoftSetArg(const std::string& strArg, const std::string& strValue) |
349 | { | |
350 | if (mapArgs.count(strArg)) | |
351 | return false; | |
352 | mapArgs[strArg] = strValue; | |
353 | return true; | |
354 | } | |
355 | ||
7bf8b7c2 | 356 | bool SoftSetBoolArg(const std::string& strArg, bool fValue) |
0fcf91ea GA |
357 | { |
358 | if (fValue) | |
359 | return SoftSetArg(strArg, std::string("1")); | |
360 | else | |
361 | return SoftSetArg(strArg, std::string("0")); | |
362 | } | |
363 | ||
1fdb9fa3 LV |
364 | static const int screenWidth = 79; |
365 | static const int optIndent = 2; | |
366 | static const int msgIndent = 7; | |
367 | ||
368 | std::string HelpMessageGroup(const std::string &message) { | |
369 | return std::string(message) + std::string("\n\n"); | |
370 | } | |
371 | ||
372 | std::string HelpMessageOpt(const std::string &option, const std::string &message) { | |
373 | return std::string(optIndent,' ') + std::string(option) + | |
374 | std::string("\n") + std::string(msgIndent,' ') + | |
375 | FormatParagraph(message, screenWidth - msgIndent, msgIndent) + | |
376 | std::string("\n\n"); | |
377 | } | |
378 | ||
27df4123 | 379 | static std::string FormatException(const std::exception* pex, const char* pszThread) |
2097c09a | 380 | { |
6853e627 | 381 | #ifdef WIN32 |
3e468840 | 382 | char pszModule[MAX_PATH] = ""; |
2097c09a WL |
383 | GetModuleFileNameA(NULL, pszModule, sizeof(pszModule)); |
384 | #else | |
5166804f | 385 | const char* pszModule = "Komodo"; |
2097c09a WL |
386 | #endif |
387 | if (pex) | |
29b79e4c | 388 | return strprintf( |
2097c09a WL |
389 | "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread); |
390 | else | |
29b79e4c | 391 | return strprintf( |
2097c09a WL |
392 | "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread); |
393 | } | |
394 | ||
27df4123 | 395 | void PrintExceptionContinue(const std::exception* pex, const char* pszThread) |
2097c09a | 396 | { |
29b79e4c | 397 | std::string message = FormatException(pex, pszThread); |
7d9d134b | 398 | LogPrintf("\n\n************************\n%s\n", message); |
29b79e4c WL |
399 | fprintf(stderr, "\n\n************************\n%s\n", message.c_str()); |
400 | strMiscWarning = message; | |
2097c09a WL |
401 | } |
402 | ||
9f480280 | 403 | extern char ASSETCHAINS_SYMBOL[16]; |
f71f89bb | 404 | |
ee12c3d6 | 405 | boost::filesystem::path GetDefaultDataDir() |
2097c09a | 406 | { |
ee12c3d6 | 407 | namespace fs = boost::filesystem; |
dadaab0a | 408 | char symbol[16]; |
409 | if ( ASSETCHAINS_SYMBOL[0] != 0 ) | |
ab918767 | 410 | strcpy(symbol,ASSETCHAINS_SYMBOL); |
411 | else symbol[0] = 0; | |
8a183335 TH |
412 | // Windows < Vista: C:\Documents and Settings\Username\Application Data\Zcash |
413 | // Windows >= Vista: C:\Users\Username\AppData\Roaming\Zcash | |
414 | // Mac: ~/Library/Application Support/Zcash | |
415 | // Unix: ~/.zcash | |
6853e627 | 416 | #ifdef WIN32 |
2097c09a | 417 | // Windows |
dadaab0a | 418 | if ( symbol[0] == 0 ) |
892dc7ac | 419 | return GetSpecialFolderPath(CSIDL_APPDATA) / "Komodo"; |
dadaab0a | 420 | else return GetSpecialFolderPath(CSIDL_APPDATA) / "Komodo" / symbol; |
2097c09a | 421 | #else |
ee12c3d6 | 422 | fs::path pathRet; |
2097c09a WL |
423 | char* pszHome = getenv("HOME"); |
424 | if (pszHome == NULL || strlen(pszHome) == 0) | |
ee12c3d6 PW |
425 | pathRet = fs::path("/"); |
426 | else | |
427 | pathRet = fs::path(pszHome); | |
6853e627 | 428 | #ifdef MAC_OSX |
2097c09a | 429 | // Mac |
940e22fd | 430 | pathRet /= "Library/Application Support"; |
2b7709dc | 431 | TryCreateDirectory(pathRet); |
dadaab0a | 432 | if ( symbol[0] == 0 ) |
892dc7ac | 433 | return pathRet / "Komodo"; |
434 | else | |
435 | { | |
436 | pathRet /= "Komodo"; | |
437 | TryCreateDirectory(pathRet); | |
dadaab0a | 438 | return pathRet / symbol; |
892dc7ac | 439 | } |
2097c09a WL |
440 | #else |
441 | // Unix | |
dadaab0a | 442 | if ( symbol[0] == 0 ) |
892dc7ac | 443 | return pathRet / ".komodo"; |
dadaab0a | 444 | else return pathRet / ".komodo" / symbol; |
2097c09a WL |
445 | #endif |
446 | #endif | |
447 | } | |
448 | ||
ebdb9ff6 | 449 | static boost::filesystem::path pathCached; |
450 | static boost::filesystem::path pathCachedNetSpecific; | |
4f1c3798 | 451 | static boost::filesystem::path zc_paramsPathCached; |
b94595bb GA |
452 | static CCriticalSection csPathCached; |
453 | ||
4f1c3798 SB |
454 | static boost::filesystem::path ZC_GetBaseParamsDir() |
455 | { | |
456 | // Copied from GetDefaultDataDir and adapter for zcash params. | |
457 | ||
458 | namespace fs = boost::filesystem; | |
459 | // Windows < Vista: C:\Documents and Settings\Username\Application Data\ZcashParams | |
460 | // Windows >= Vista: C:\Users\Username\AppData\Roaming\ZcashParams | |
461 | // Mac: ~/Library/Application Support/ZcashParams | |
462 | // Unix: ~/.zcash-params | |
463 | #ifdef WIN32 | |
464 | // Windows | |
465 | return GetSpecialFolderPath(CSIDL_APPDATA) / "ZcashParams"; | |
466 | #else | |
467 | fs::path pathRet; | |
468 | char* pszHome = getenv("HOME"); | |
469 | if (pszHome == NULL || strlen(pszHome) == 0) | |
470 | pathRet = fs::path("/"); | |
471 | else | |
472 | pathRet = fs::path(pszHome); | |
473 | #ifdef MAC_OSX | |
474 | // Mac | |
475 | pathRet /= "Library/Application Support"; | |
476 | TryCreateDirectory(pathRet); | |
477 | return pathRet / "ZcashParams"; | |
478 | #else | |
479 | // Unix | |
480 | return pathRet / ".zcash-params"; | |
481 | #endif | |
482 | #endif | |
483 | } | |
484 | ||
485 | const boost::filesystem::path &ZC_GetParamsDir() | |
486 | { | |
487 | namespace fs = boost::filesystem; | |
488 | ||
489 | LOCK(csPathCached); // Reuse the same lock as upstream. | |
490 | ||
491 | fs::path &path = zc_paramsPathCached; | |
492 | ||
493 | // This can be called during exceptions by LogPrintf(), so we cache the | |
494 | // value so we don't have to do memory allocations after that. | |
495 | if (!path.empty()) | |
496 | return path; | |
497 | ||
498 | path = ZC_GetBaseParamsDir(); | |
4f1c3798 SB |
499 | |
500 | return path; | |
501 | } | |
502 | ||
ee12c3d6 | 503 | const boost::filesystem::path &GetDataDir(bool fNetSpecific) |
2097c09a | 504 | { |
ee12c3d6 PW |
505 | namespace fs = boost::filesystem; |
506 | ||
b94595bb GA |
507 | LOCK(csPathCached); |
508 | ||
ebdb9ff6 | 509 | fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached; |
ee12c3d6 | 510 | |
faaeae1e | 511 | // This can be called during exceptions by LogPrintf(), so we cache the |
ee12c3d6 | 512 | // value so we don't have to do memory allocations after that. |
b94595bb | 513 | if (!path.empty()) |
ee12c3d6 PW |
514 | return path; |
515 | ||
ee12c3d6 | 516 | if (mapArgs.count("-datadir")) { |
f4203de3 PW |
517 | path = fs::system_complete(mapArgs["-datadir"]); |
518 | if (!fs::is_directory(path)) { | |
519 | path = ""; | |
520 | return path; | |
521 | } | |
ee12c3d6 PW |
522 | } else { |
523 | path = GetDefaultDataDir(); | |
2097c09a | 524 | } |
0e4b3175 | 525 | if (fNetSpecific) |
84ce18ca | 526 | path /= BaseParams().DataDir(); |
2097c09a | 527 | |
3e9c8bab | 528 | fs::create_directories(path); |
ee12c3d6 | 529 | |
ee12c3d6 | 530 | return path; |
2097c09a WL |
531 | } |
532 | ||
b94595bb GA |
533 | void ClearDatadirCache() |
534 | { | |
ebdb9ff6 | 535 | pathCached = boost::filesystem::path(); |
536 | pathCachedNetSpecific = boost::filesystem::path(); | |
b94595bb GA |
537 | } |
538 | ||
ee12c3d6 | 539 | boost::filesystem::path GetConfigFile() |
2097c09a | 540 | { |
323832c3 | 541 | char confname[512]; |
542 | if ( ASSETCHAINS_SYMBOL[0] != 0 ) | |
543 | sprintf(confname,"%s.conf",ASSETCHAINS_SYMBOL); | |
544 | else strcpy(confname,"komodo.conf"); | |
545 | boost::filesystem::path pathConfigFile(GetArg("-conf",confname)); | |
ac14bcc1 PK |
546 | if (!pathConfigFile.is_complete()) |
547 | pathConfigFile = GetDataDir(false) / pathConfigFile; | |
548 | ||
ee12c3d6 | 549 | return pathConfigFile; |
2097c09a WL |
550 | } |
551 | ||
f4203de3 | 552 | void ReadConfigFile(map<string, string>& mapSettingsRet, |
2097c09a WL |
553 | map<string, vector<string> >& mapMultiSettingsRet) |
554 | { | |
3e468840 | 555 | boost::filesystem::ifstream streamConfig(GetConfigFile()); |
2097c09a | 556 | if (!streamConfig.good()) |
5166804f | 557 | return; // No komodo.conf file is OK |
2097c09a WL |
558 | |
559 | set<string> setOptions; | |
560 | setOptions.insert("*"); | |
ee12c3d6 | 561 | |
3e468840 | 562 | for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it) |
2097c09a | 563 | { |
5166804f | 564 | // Don't overwrite existing settings so command line settings override komodo.conf |
2097c09a WL |
565 | string strKey = string("-") + it->string_key; |
566 | if (mapSettingsRet.count(strKey) == 0) | |
d64e124c | 567 | { |
2097c09a | 568 | mapSettingsRet[strKey] = it->value[0]; |
3e468840 | 569 | // interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set) |
d64e124c CM |
570 | InterpretNegativeSetting(strKey, mapSettingsRet); |
571 | } | |
2097c09a WL |
572 | mapMultiSettingsRet[strKey].push_back(it->value[0]); |
573 | } | |
b94595bb GA |
574 | // If datadir is changed in .conf file: |
575 | ClearDatadirCache(); | |
2097c09a WL |
576 | } |
577 | ||
d6712db3 | 578 | #ifndef WIN32 |
ee12c3d6 | 579 | boost::filesystem::path GetPidFile() |
2097c09a | 580 | { |
5166804f | 581 | boost::filesystem::path pathPidFile(GetArg("-pid", "komodod.pid")); |
ee12c3d6 PW |
582 | if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile; |
583 | return pathPidFile; | |
2097c09a WL |
584 | } |
585 | ||
ee12c3d6 | 586 | void CreatePidFile(const boost::filesystem::path &path, pid_t pid) |
2097c09a | 587 | { |
ee12c3d6 | 588 | FILE* file = fopen(path.string().c_str(), "w"); |
f85c0974 | 589 | if (file) |
2097c09a WL |
590 | { |
591 | fprintf(file, "%d\n", pid); | |
592 | fclose(file); | |
593 | } | |
594 | } | |
a034c7eb | 595 | #endif |
2097c09a | 596 | |
768e5d52 JG |
597 | bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest) |
598 | { | |
599 | #ifdef WIN32 | |
5f986195 | 600 | return MoveFileExA(src.string().c_str(), dest.string().c_str(), |
8d657a65 | 601 | MOVEFILE_REPLACE_EXISTING) != 0; |
768e5d52 JG |
602 | #else |
603 | int rc = std::rename(src.string().c_str(), dest.string().c_str()); | |
604 | return (rc == 0); | |
605 | #endif /* WIN32 */ | |
606 | } | |
607 | ||
c63a73d1 MF |
608 | /** |
609 | * Ignores exceptions thrown by Boost's create_directory if the requested directory exists. | |
610 | * Specifically handles case where path p exists, but it wasn't possible for the user to | |
611 | * write to the parent directory. | |
612 | */ | |
2b7709dc BD |
613 | bool TryCreateDirectory(const boost::filesystem::path& p) |
614 | { | |
615 | try | |
616 | { | |
617 | return boost::filesystem::create_directory(p); | |
27df4123 | 618 | } catch (const boost::filesystem::filesystem_error&) { |
2b7709dc BD |
619 | if (!boost::filesystem::exists(p) || !boost::filesystem::is_directory(p)) |
620 | throw; | |
621 | } | |
622 | ||
623 | // create_directory didn't create the directory, it had to have existed already | |
624 | return false; | |
625 | } | |
626 | ||
768e5d52 JG |
627 | void FileCommit(FILE *fileout) |
628 | { | |
4c0b2cde | 629 | fflush(fileout); // harmless if redundantly called |
768e5d52 | 630 | #ifdef WIN32 |
4c0b2cde PK |
631 | HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(fileout)); |
632 | FlushFileBuffers(hFile); | |
768e5d52 | 633 | #else |
e9965213 PW |
634 | #if defined(__linux__) || defined(__NetBSD__) |
635 | fdatasync(fileno(fileout)); | |
e7bad10c GM |
636 | #elif defined(__APPLE__) && defined(F_FULLFSYNC) |
637 | fcntl(fileno(fileout), F_FULLFSYNC, 0); | |
e9965213 | 638 | #else |
768e5d52 | 639 | fsync(fileno(fileout)); |
e9965213 | 640 | #endif |
768e5d52 JG |
641 | #endif |
642 | } | |
643 | ||
1eb57879 PW |
644 | bool TruncateFile(FILE *file, unsigned int length) { |
645 | #if defined(WIN32) | |
646 | return _chsize(_fileno(file), length) == 0; | |
647 | #else | |
648 | return ftruncate(fileno(file), length) == 0; | |
649 | #endif | |
650 | } | |
651 | ||
c63a73d1 MF |
652 | /** |
653 | * this function tries to raise the file descriptor limit to the requested number. | |
654 | * It returns the actual file descriptor limit (which may be more or less than nMinFD) | |
655 | */ | |
ba29a559 PW |
656 | int RaiseFileDescriptorLimit(int nMinFD) { |
657 | #if defined(WIN32) | |
658 | return 2048; | |
659 | #else | |
660 | struct rlimit limitFD; | |
661 | if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) { | |
662 | if (limitFD.rlim_cur < (rlim_t)nMinFD) { | |
663 | limitFD.rlim_cur = nMinFD; | |
664 | if (limitFD.rlim_cur > limitFD.rlim_max) | |
665 | limitFD.rlim_cur = limitFD.rlim_max; | |
666 | setrlimit(RLIMIT_NOFILE, &limitFD); | |
667 | getrlimit(RLIMIT_NOFILE, &limitFD); | |
668 | } | |
669 | return limitFD.rlim_cur; | |
670 | } | |
671 | return nMinFD; // getrlimit failed, assume it's fine | |
672 | #endif | |
673 | } | |
674 | ||
c63a73d1 MF |
675 | /** |
676 | * this function tries to make a particular range of a file allocated (corresponding to disk space) | |
677 | * it is advisory, and the range specified in the arguments will never contain live data | |
678 | */ | |
bba89aa8 | 679 | void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) { |
288fdc09 PW |
680 | #if defined(WIN32) |
681 | // Windows-specific version | |
682 | HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file)); | |
683 | LARGE_INTEGER nFileSize; | |
51ed9ec9 | 684 | int64_t nEndPos = (int64_t)offset + length; |
288fdc09 PW |
685 | nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF; |
686 | nFileSize.u.HighPart = nEndPos >> 32; | |
687 | SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN); | |
688 | SetEndOfFile(hFile); | |
689 | #elif defined(MAC_OSX) | |
690 | // OSX specific version | |
691 | fstore_t fst; | |
692 | fst.fst_flags = F_ALLOCATECONTIG; | |
693 | fst.fst_posmode = F_PEOFPOSMODE; | |
694 | fst.fst_offset = 0; | |
695 | fst.fst_length = (off_t)offset + length; | |
696 | fst.fst_bytesalloc = 0; | |
697 | if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) { | |
698 | fst.fst_flags = F_ALLOCATEALL; | |
699 | fcntl(fileno(file), F_PREALLOCATE, &fst); | |
700 | } | |
701 | ftruncate(fileno(file), fst.fst_length); | |
702 | #elif defined(__linux__) | |
703 | // Version using posix_fallocate | |
704 | off_t nEndPos = (off_t)offset + length; | |
705 | posix_fallocate(fileno(file), 0, nEndPos); | |
706 | #else | |
707 | // Fallback version | |
708 | // TODO: just write one byte per block | |
bba89aa8 PW |
709 | static const char buf[65536] = {}; |
710 | fseek(file, offset, SEEK_SET); | |
711 | while (length > 0) { | |
712 | unsigned int now = 65536; | |
713 | if (length < now) | |
714 | now = length; | |
715 | fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway | |
716 | length -= now; | |
717 | } | |
288fdc09 | 718 | #endif |
bba89aa8 PW |
719 | } |
720 | ||
2097c09a WL |
721 | void ShrinkDebugFile() |
722 | { | |
723 | // Scroll debug.log if it's getting too big | |
ee12c3d6 PW |
724 | boost::filesystem::path pathLog = GetDataDir() / "debug.log"; |
725 | FILE* file = fopen(pathLog.string().c_str(), "r"); | |
a486abd4 | 726 | if (file && boost::filesystem::file_size(pathLog) > 10 * 1000000) |
2097c09a WL |
727 | { |
728 | // Restart the file with some of the end | |
fcb0a1bb | 729 | std::vector <char> vch(200000,0); |
1e735048 | 730 | fseek(file, -((long)vch.size()), SEEK_END); |
fcb0a1bb | 731 | int nBytes = fread(begin_ptr(vch), 1, vch.size(), file); |
2097c09a | 732 | fclose(file); |
f85c0974 | 733 | |
ee12c3d6 | 734 | file = fopen(pathLog.string().c_str(), "w"); |
f85c0974 | 735 | if (file) |
2097c09a | 736 | { |
fcb0a1bb | 737 | fwrite(begin_ptr(vch), 1, nBytes, file); |
2097c09a WL |
738 | fclose(file); |
739 | } | |
740 | } | |
3260b4c0 PK |
741 | else if (file != NULL) |
742 | fclose(file); | |
2097c09a WL |
743 | } |
744 | ||
ed6d0b5f | 745 | #ifdef WIN32 |
3e468840 PK |
746 | boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate) |
747 | { | |
748 | namespace fs = boost::filesystem; | |
749 | ||
750 | char pszPath[MAX_PATH] = ""; | |
751 | ||
752 | if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate)) | |
753 | { | |
754 | return fs::path(pszPath); | |
755 | } | |
756 | ||
881a85a2 | 757 | LogPrintf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n"); |
3e468840 PK |
758 | return fs::path(""); |
759 | } | |
ed6d0b5f | 760 | #endif |
429039d4 | 761 | |
597fa4cd PW |
762 | boost::filesystem::path GetTempPath() { |
763 | #if BOOST_FILESYSTEM_VERSION == 3 | |
764 | return boost::filesystem::temp_directory_path(); | |
765 | #else | |
766 | // TODO: remove when we don't support filesystem v2 anymore | |
767 | boost::filesystem::path path; | |
768 | #ifdef WIN32 | |
769 | char pszPath[MAX_PATH] = ""; | |
770 | ||
771 | if (GetTempPathA(MAX_PATH, pszPath)) | |
772 | path = boost::filesystem::path(pszPath); | |
773 | #else | |
774 | path = boost::filesystem::path("/tmp"); | |
775 | #endif | |
776 | if (path.empty() || !boost::filesystem::is_directory(path)) { | |
881a85a2 | 777 | LogPrintf("GetTempPath(): failed to find temp path\n"); |
597fa4cd PW |
778 | return boost::filesystem::path(""); |
779 | } | |
780 | return path; | |
781 | #endif | |
782 | } | |
783 | ||
429039d4 JG |
784 | void runCommand(std::string strCommand) |
785 | { | |
786 | int nErr = ::system(strCommand.c_str()); | |
787 | if (nErr) | |
7d9d134b | 788 | LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr); |
429039d4 JG |
789 | } |
790 | ||
96931d6f GS |
791 | void RenameThread(const char* name) |
792 | { | |
b277b0f1 | 793 | #if defined(PR_SET_NAME) |
96931d6f GS |
794 | // Only the first 15 characters are used (16 - NUL terminator) |
795 | ::prctl(PR_SET_NAME, name, 0, 0, 0); | |
7f386d2f | 796 | #elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)) |
304ca955 | 797 | pthread_set_name_np(pthread_self(), name); |
cd58f058 | 798 | |
850c570d | 799 | #elif defined(MAC_OSX) |
c8c2fbe0 | 800 | pthread_setname_np(name); |
96931d6f GS |
801 | #else |
802 | // Prevent warnings for unused parameters... | |
803 | (void)name; | |
804 | #endif | |
805 | } | |
0b47fe6b | 806 | |
5248ff40 SC |
807 | void SetupEnvironment() |
808 | { | |
317e66c7 | 809 | // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale |
810 | // may be invalid, in which case the "C" locale is used as fallback. | |
811 | #if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__) | |
812 | try { | |
3da78490 | 813 | std::locale(""); // Raises a runtime error if current locale is invalid |
27df4123 | 814 | } catch (const std::runtime_error&) { |
ba0fa0d9 | 815 | setenv("LC_ALL", "C", 1); |
5248ff40 | 816 | } |
ac14bcc1 | 817 | #endif |
3da78490 | 818 | // The path locale is lazy initialized and to avoid deinitialization errors |
317e66c7 | 819 | // in multithreading environments, it is set explicitly by the main thread. |
3da78490 JS |
820 | // A dummy locale is used to extract the internal default locale, used by |
821 | // boost::filesystem::path, which is then used to explicitly imbue the path. | |
822 | std::locale loc = boost::filesystem::path::imbue(std::locale::classic()); | |
ba0fa0d9 | 823 | boost::filesystem::path::imbue(loc); |
5248ff40 | 824 | } |
3e8ac6af | 825 | |
610a8c07 WL |
826 | void SetThreadPriority(int nPriority) |
827 | { | |
828 | #ifdef WIN32 | |
829 | SetThreadPriority(GetCurrentThread(), nPriority); | |
830 | #else // WIN32 | |
831 | #ifdef PRIO_THREAD | |
832 | setpriority(PRIO_THREAD, 0, nPriority); | |
833 | #else // PRIO_THREAD | |
834 | setpriority(PRIO_PROCESS, 0, nPriority); | |
835 | #endif // PRIO_THREAD | |
836 | #endif // WIN32 | |
837 | } |