9 #if defined(HAVE_CONFIG_H)
24 #include <boost/date_time/posix_time/posix_time.hpp>
25 #include <openssl/bio.h>
26 #include <openssl/buffer.h>
27 #include <openssl/evp.h>
37 #ifdef _POSIX_C_SOURCE
38 #undef _POSIX_C_SOURCE
41 #define _POSIX_C_SOURCE 200112L
47 #include <sys/resource.h>
53 #pragma warning(disable : 4786)
54 #pragma warning(disable : 4804)
55 #pragma warning(disable : 4805)
56 #pragma warning(disable : 4717)
67 #ifdef HAVE_SYS_PRCTL_H
68 #include <sys/prctl.h>
71 #include <boost/algorithm/string/case_conv.hpp>
72 #include <boost/algorithm/string/join.hpp>
73 #include <boost/algorithm/string/predicate.hpp>
74 #include <boost/program_options/detail/config_file.hpp>
75 #include <boost/program_options/parsers.hpp>
76 #include <boost/thread.hpp>
77 #include <openssl/conf.h>
78 #include <openssl/crypto.h>
79 #include <openssl/rand.h>
87 namespace program_options
89 std::string to_internal(
const std::string&);
121 if (mode & CRYPTO_LOCK) {
136 for (
int i = 0; i < CRYPTO_num_locks(); i++)
160 CRYPTO_set_locking_callback(NULL);
161 for (
int i = 0; i < CRYPTO_num_locks(); i++)
162 delete ppmutexOpenSSL[i];
163 OPENSSL_free(ppmutexOpenSSL);
170 int n = str.length();
171 char char_array[n + 1];
172 strcpy(char_array, str.c_str());
174 for (
int i = 0; i < n; i++) {
175 if (char_array[i] ==
'\r' || char_array[i] ==
'\t' || char_array[i] ==
'\0')
177 else if (char_array[i] ==
'\n') {
182 else if (char_array[i] ==
'%' || char_array[i] ==
'&' || char_array[i] ==
'<' ||
183 char_array[i] ==
'>' || char_array[i] ==
'\"' || char_array[i] ==
'\'')
187 std::string result(char_array);
192 static bool InterpretBool(
const std::string& strValue)
194 if (strValue.empty())
196 return (
atoi(strValue) != 0);
200 static void InterpretNegativeSetting(std::string& strKey, std::string& strValue)
202 if (strKey.length()>3 && strKey[0]==
'-' && strKey[1]==
'n' && strKey[2]==
'o') {
203 strKey =
"-" + strKey.substr(3);
204 strValue = InterpretBool(strValue) ?
"0" :
"1";
213 for (
int i = 1; i < argc; i++) {
214 std::string str(argv[i]);
215 std::string strValue;
216 size_t is_index = str.find(
'=');
217 if (is_index != std::string::npos) {
218 strValue = str.substr(is_index + 1);
219 str = str.substr(0, is_index);
222 boost::to_lower(str);
223 if (boost::algorithm::starts_with(str,
"/"))
224 str =
"-" + str.substr(1);
232 if (str.length() > 1 && str[1] ==
'-')
234 InterpretNegativeSetting(str, strValue);
241 std::string
GetArg(
const std::string& strArg,
const std::string& strDefault)
248 int64_t
GetArg(
const std::string& strArg, int64_t nDefault)
258 return InterpretBool(
mapArgs[strArg]);
262 bool SoftSetArg(
const std::string& strArg,
const std::string& strValue)
278 static const int screenWidth = 79;
279 static const int optIndent = 2;
280 static const int msgIndent = 7;
283 return std::string(message) + std::string(
"\n\n");
286 std::string
HelpMessageOpt(
const std::string &option,
const std::string &message) {
287 return std::string(optIndent,
' ') + std::string(option) +
288 std::string(
"\n") + std::string(msgIndent,
' ') +
293 static std::string FormatException(
const std::exception* pex,
const char* pszThread)
297 GetModuleFileNameA(NULL, pszModule,
sizeof(pszModule));
299 const char* pszModule =
"prcycoin";
303 "EXCEPTION: %s \n%s \n%s in %s \n",
typeid(*pex).name(), pex->what(), pszModule, pszThread);
306 "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
311 std::string message = FormatException(pex, pszThread);
312 LogPrintf(
"\n\n************************\n%s\n", message);
313 fprintf(stderr,
"\n\n************************\n%s\n", message.c_str());
325 return GetSpecialFolderPath(CSIDL_APPDATA) /
"PRCYcoin";
328 char* pszHome = getenv(
"HOME");
329 if (pszHome == NULL || strlen(pszHome) == 0)
330 pathRet = fs::path(
"/");
332 pathRet = fs::path(pszHome);
335 pathRet /=
"Library/Application Support";
337 return pathRet /
"PRCYcoin";
340 return pathRet /
".prcycoin";
345 static fs::path pathCached;
346 static fs::path pathCachedNetSpecific;
353 fs::path& path = fNetSpecific ? pathCachedNetSpecific : pathCached;
360 if (
mapArgs.count(
"-datadir")) {
361 path = fs::system_complete(
mapArgs[
"-datadir"]);
362 if (!fs::is_directory(path)) {
372 fs::create_directories(path);
379 pathCached = fs::path();
380 pathCachedNetSpecific = fs::path();
385 fs::path pathConfigFile(
GetArg(
"-conf",
"prcycoin.conf"));
391 fs::path pathConfigFile(
GetArg(
"-mnconf",
"masternode.conf"));
396 std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet)
399 if (!streamConfig.good()) {
402 if (configFile != NULL)
407 std::set<std::string> setOptions;
408 setOptions.insert(
"*");
410 for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it) {
412 std::string strKey = std::string(
"-") + it->string_key;
413 std::string strValue = it->value[0];
414 InterpretNegativeSetting(strKey, strValue);
415 if (mapSettingsRet.count(strKey) == 0)
416 mapSettingsRet[strKey] = strValue;
417 mapMultiSettingsRet[strKey].push_back(strValue);
425 if (path.is_absolute()) {
428 return fs::absolute(path,
GetDataDir(net_specific));
434 fs::path pathPidFile(
GetArg(
"-pid",
"prcycoind.pid"));
442 fprintf(file,
"%d\n", pid);
451 return MoveFileExA(src.string().c_str(), dest.string().c_str(),
452 MOVEFILE_REPLACE_EXISTING) != 0;
454 int rc = std::rename(src.string().c_str(), dest.string().c_str());
467 return fs::create_directory(p);
468 }
catch (
const fs::filesystem_error&) {
469 if (!fs::exists(p) || !fs::is_directory(p))
481 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(fileout));
482 FlushFileBuffers(hFile);
484 #if defined(__linux__) || defined(__NetBSD__)
485 fdatasync(fileno(fileout));
486 #elif defined(MAC_OSX) && defined(F_FULLFSYNC)
487 fcntl(fileno(fileout), F_FULLFSYNC, 0);
489 fsync(fileno(fileout));
497 return _chsize(_fileno(file), length) == 0;
499 return ftruncate(fileno(file), length) == 0;
512 struct rlimit limitFD;
513 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
514 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
515 limitFD.rlim_cur = nMinFD;
516 if (limitFD.rlim_cur > limitFD.rlim_max)
517 limitFD.rlim_cur = limitFD.rlim_max;
518 setrlimit(RLIMIT_NOFILE, &limitFD);
519 getrlimit(RLIMIT_NOFILE, &limitFD);
521 return limitFD.rlim_cur;
535 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
536 LARGE_INTEGER nFileSize;
537 int64_t nEndPos = (int64_t)offset + length;
538 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
539 nFileSize.u.HighPart = nEndPos >> 32;
540 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
542 #elif defined(MAC_OSX)
545 fst.fst_flags = F_ALLOCATECONTIG;
546 fst.fst_posmode = F_PEOFPOSMODE;
548 fst.fst_length = (off_t)offset + length;
549 fst.fst_bytesalloc = 0;
550 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
551 fst.fst_flags = F_ALLOCATEALL;
552 fcntl(fileno(file), F_PREALLOCATE, &fst);
554 ftruncate(fileno(file), fst.fst_length);
555 #elif defined(__linux__)
557 off_t nEndPos = (off_t)offset + length;
558 posix_fallocate(fileno(file), 0, nEndPos);
562 static const char buf[65536] = {};
563 fseek(file, offset, SEEK_SET);
565 unsigned int now = 65536;
568 fwrite(buf, 1, now, file);
575 fs::path GetSpecialFolderPath(
int nFolder,
bool fCreate)
579 if (SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate)) {
580 return fs::path(pszPath);
583 LogPrintf(
"SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
590 return fs::temp_directory_path();
595 int nErr = ::system(strCommand.c_str());
597 LogPrintf(
"runCommand error: system(%s) returned %d\n", strCommand, nErr);
604 #if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
607 }
catch (
const std::runtime_error&) {
608 setenv(
"LC_ALL",
"C", 1);
615 std::locale loc = fs::path::imbue(std::locale::classic());
616 fs::path::imbue(loc);
620 unsigned char pubData[65];
622 pubData[0] = *(pk.
begin());
624 CPubKey newPubKey(pubData, pubData + 33);
628 pubData[0] = *(newPubKey.
begin());
630 newPubKey.
Set(pubData, pubData + 33);
641 int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
642 if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2)
654 setpriority(PRIO_THREAD, 0, nPriority);
656 setpriority(PRIO_PROCESS, 0, nPriority);
657 #endif // PRIO_THREAD