36 #include <gnu/libc-version.h> 40 #include <sys/types.h> 42 #include <sys/resource.h> 53 #include <sys/sysmacros.h> 73 #ifndef STRSAFE_NO_DEPRECATE 74 #define STRSAFE_NO_DEPRECATE 81 #include <sys/utsname.h> 84 #include <boost/filesystem.hpp> 85 #include <boost/algorithm/string.hpp> 86 #include <boost/asio.hpp> 87 #include <boost/format.hpp> 88 #include <openssl/sha.h> 90 #undef ELECTRONEUM_DEFAULT_LOG_CATEGORY 91 #define ELECTRONEUM_DEFAULT_LOG_CATEGORY "util" 97 static int flock_exnb(
int fd)
102 memset(&fl, 0,
sizeof(fl));
104 fl.l_whence = SEEK_SET;
107 ret = fcntl(fd, F_SETLK, &fl);
109 MERROR(
"Error locking fd " << fd <<
": " << errno <<
" (" << strerror(errno) <<
")");
118 std::function<void(int)> signal_handler::m_handler;
120 private_file::private_file() noexcept : m_handle(), m_filename() {}
122 private_file::private_file(std::FILE* handle,
std::string&& filename) noexcept
123 : m_handle(handle), m_filename(
std::move(filename)) {}
130 void operator()(HANDLE handle)
const noexcept
136 std::unique_ptr<void, close_handle> process =
nullptr;
139 const bool fail = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, std::addressof(temp)) == 0;
146 GetTokenInformation(process.get(), TokenOwner,
nullptr, 0, std::addressof(sid_size));
147 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
150 std::unique_ptr<char[]> sid{
new char[sid_size]};
151 if (!GetTokenInformation(process.get(), TokenOwner, sid.get(), sid_size, std::addressof(sid_size)))
154 const PSID psid =
reinterpret_cast<const PTOKEN_OWNER
>(sid.get())->Owner;
155 const DWORD daclSize =
156 sizeof(ACL) +
sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(psid) -
sizeof(DWORD);
158 const std::unique_ptr<char[]> dacl{
new char[daclSize]};
159 if (!InitializeAcl(reinterpret_cast<PACL>(dacl.get()), daclSize, ACL_REVISION))
162 if (!AddAccessAllowedAce(reinterpret_cast<PACL>(dacl.get()), ACL_REVISION, (READ_CONTROL | FILE_GENERIC_READ | DELETE), psid))
165 SECURITY_DESCRIPTOR descriptor{};
166 if (!InitializeSecurityDescriptor(std::addressof(descriptor), SECURITY_DESCRIPTOR_REVISION))
169 if (!SetSecurityDescriptorDacl(std::addressof(descriptor),
true,
reinterpret_cast<PACL
>(dacl.get()),
false))
172 SECURITY_ATTRIBUTES attributes{
sizeof(SECURITY_ATTRIBUTES), std::addressof(descriptor),
false};
173 std::unique_ptr<void, close_handle> file{
176 GENERIC_WRITE, FILE_SHARE_READ,
177 std::addressof(attributes),
178 CREATE_NEW, (FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE),
184 const int fd = _open_osfhandle(reinterpret_cast<intptr_t>(file.get()), 0);
188 std::FILE* real_file = _fdopen(fd,
"w");
197 const int fdr = open(
name.c_str(), (O_RDONLY | O_CREAT), S_IRUSR);
200 struct stat rstats = {};
201 if (fstat(fdr, std::addressof(rstats)) != 0)
206 fchmod(fdr, (S_IRUSR | S_IWUSR));
207 const int fdw = open(
name.c_str(), O_RDWR);
208 fchmod(fdr, rstats.st_mode);
213 struct stat wstats = {};
214 if (fstat(fdw, std::addressof(wstats)) == 0 &&
215 rstats.st_dev == wstats.st_dev && rstats.st_ino == wstats.st_ino &&
216 flock_exnb(fdw) == 0 && ftruncate(fdw, 0) == 0)
218 std::FILE* file = fdopen(fdw,
"w");
228 private_file::~private_file() noexcept
232 boost::system::error_code ec{};
233 boost::filesystem::remove(filename(), ec);
241 m_fd = INVALID_HANDLE_VALUE;
245 filename_wide = string_tools::utf8_to_utf16(filename);
247 catch (
const std::exception &e)
249 MERROR(
"Failed to convert path \"" << filename <<
"\" to UTF-16: " << e.what());
252 m_fd = CreateFileW(filename_wide.c_str(), GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
253 if (m_fd != INVALID_HANDLE_VALUE)
256 memset(&ov, 0,
sizeof(ov));
257 if (!LockFileEx(m_fd, LOCKFILE_FAIL_IMMEDIATELY | LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &ov))
259 MERROR(
"Failed to lock " << filename <<
": " << std::error_code(GetLastError(), std::system_category()));
261 m_fd = INVALID_HANDLE_VALUE;
266 MERROR(
"Failed to open " << filename <<
": " << std::error_code(GetLastError(), std::system_category()));
269 m_fd = open(filename.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, 0666);
272 if (flock_exnb(m_fd) == -1)
274 MERROR(
"Failed to lock " << filename <<
": " << std::strerror(errno));
281 MERROR(
"Failed to open " << filename <<
": " << std::strerror(errno));
285 file_locker::~file_locker()
296 bool file_locker::locked()
const 299 return m_fd != INVALID_HANDLE_VALUE;
308 typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
309 typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);
310 #define BUFSIZE 10000 312 char pszOS[BUFSIZE] = {0};
313 OSVERSIONINFOEX osvi;
317 BOOL bOsVersionInfoEx;
320 ZeroMemory(&si,
sizeof(SYSTEM_INFO));
321 ZeroMemory(&osvi,
sizeof(OSVERSIONINFOEX));
323 osvi.dwOSVersionInfoSize =
sizeof(OSVERSIONINFOEX);
324 bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*) &osvi);
326 if(!bOsVersionInfoEx)
return pszOS;
330 pGNSI = (PGNSI) GetProcAddress(
331 GetModuleHandle(TEXT(
"kernel32.dll")),
332 "GetNativeSystemInfo");
335 else GetSystemInfo(&si);
337 if ( VER_PLATFORM_WIN32_NT==osvi.dwPlatformId &&
338 osvi.dwMajorVersion > 4 )
340 StringCchCopy(pszOS, BUFSIZE, TEXT(
"Microsoft "));
343 if ( osvi.dwMajorVersion == 10 )
345 if ( osvi.dwMinorVersion == 0 )
347 if( osvi.wProductType == VER_NT_WORKSTATION )
348 StringCchCat(pszOS, BUFSIZE, TEXT(
"Windows 10 "));
349 else StringCchCat(pszOS, BUFSIZE, TEXT(
"Windows Server 2016 " ));
353 if ( osvi.dwMajorVersion == 6 )
355 if ( osvi.dwMinorVersion == 0 )
357 if( osvi.wProductType == VER_NT_WORKSTATION )
358 StringCchCat(pszOS, BUFSIZE, TEXT(
"Windows Vista "));
359 else StringCchCat(pszOS, BUFSIZE, TEXT(
"Windows Server 2008 " ));
362 if ( osvi.dwMinorVersion == 1 )
364 if( osvi.wProductType == VER_NT_WORKSTATION )
365 StringCchCat(pszOS, BUFSIZE, TEXT(
"Windows 7 "));
366 else StringCchCat(pszOS, BUFSIZE, TEXT(
"Windows Server 2008 R2 " ));
369 if ( osvi.dwMinorVersion == 2 )
371 if( osvi.wProductType == VER_NT_WORKSTATION )
372 StringCchCat(pszOS, BUFSIZE, TEXT(
"Windows 8 "));
373 else StringCchCat(pszOS, BUFSIZE, TEXT(
"Windows Server 2012 " ));
376 if ( osvi.dwMinorVersion == 3 )
378 if( osvi.wProductType == VER_NT_WORKSTATION )
379 StringCchCat(pszOS, BUFSIZE, TEXT(
"Windows 8.1 "));
380 else StringCchCat(pszOS, BUFSIZE, TEXT(
"Windows Server 2012 R2 " ));
383 pGPI = (PGPI) GetProcAddress(
384 GetModuleHandle(TEXT(
"kernel32.dll")),
387 pGPI( osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);
391 case PRODUCT_ULTIMATE:
392 StringCchCat(pszOS, BUFSIZE, TEXT(
"Ultimate Edition" ));
394 case PRODUCT_PROFESSIONAL:
395 StringCchCat(pszOS, BUFSIZE, TEXT(
"Professional" ));
397 case PRODUCT_HOME_PREMIUM:
398 StringCchCat(pszOS, BUFSIZE, TEXT(
"Home Premium Edition" ));
400 case PRODUCT_HOME_BASIC:
401 StringCchCat(pszOS, BUFSIZE, TEXT(
"Home Basic Edition" ));
403 case PRODUCT_ENTERPRISE:
404 StringCchCat(pszOS, BUFSIZE, TEXT(
"Enterprise Edition" ));
406 case PRODUCT_BUSINESS:
407 StringCchCat(pszOS, BUFSIZE, TEXT(
"Business Edition" ));
409 case PRODUCT_STARTER:
410 StringCchCat(pszOS, BUFSIZE, TEXT(
"Starter Edition" ));
412 case PRODUCT_CLUSTER_SERVER:
413 StringCchCat(pszOS, BUFSIZE, TEXT(
"Cluster Server Edition" ));
415 case PRODUCT_DATACENTER_SERVER:
416 StringCchCat(pszOS, BUFSIZE, TEXT(
"Datacenter Edition" ));
418 case PRODUCT_DATACENTER_SERVER_CORE:
419 StringCchCat(pszOS, BUFSIZE, TEXT(
"Datacenter Edition (core installation)" ));
421 case PRODUCT_ENTERPRISE_SERVER:
422 StringCchCat(pszOS, BUFSIZE, TEXT(
"Enterprise Edition" ));
424 case PRODUCT_ENTERPRISE_SERVER_CORE:
425 StringCchCat(pszOS, BUFSIZE, TEXT(
"Enterprise Edition (core installation)" ));
427 case PRODUCT_ENTERPRISE_SERVER_IA64:
428 StringCchCat(pszOS, BUFSIZE, TEXT(
"Enterprise Edition for Itanium-based Systems" ));
430 case PRODUCT_SMALLBUSINESS_SERVER:
431 StringCchCat(pszOS, BUFSIZE, TEXT(
"Small Business Server" ));
433 case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
434 StringCchCat(pszOS, BUFSIZE, TEXT(
"Small Business Server Premium Edition" ));
436 case PRODUCT_STANDARD_SERVER:
437 StringCchCat(pszOS, BUFSIZE, TEXT(
"Standard Edition" ));
439 case PRODUCT_STANDARD_SERVER_CORE:
440 StringCchCat(pszOS, BUFSIZE, TEXT(
"Standard Edition (core installation)" ));
442 case PRODUCT_WEB_SERVER:
443 StringCchCat(pszOS, BUFSIZE, TEXT(
"Web Server Edition" ));
448 if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
450 if( GetSystemMetrics(SM_SERVERR2) )
451 StringCchCat(pszOS, BUFSIZE, TEXT(
"Windows Server 2003 R2, "));
452 else if ( osvi.wSuiteMask & VER_SUITE_STORAGE_SERVER )
453 StringCchCat(pszOS, BUFSIZE, TEXT(
"Windows Storage Server 2003"));
454 else if ( osvi.wSuiteMask & VER_SUITE_WH_SERVER )
455 StringCchCat(pszOS, BUFSIZE, TEXT(
"Windows Home Server"));
456 else if( osvi.wProductType == VER_NT_WORKSTATION &&
457 si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
459 StringCchCat(pszOS, BUFSIZE, TEXT(
"Windows XP Professional x64 Edition"));
461 else StringCchCat(pszOS, BUFSIZE, TEXT(
"Windows Server 2003, "));
464 if ( osvi.wProductType != VER_NT_WORKSTATION )
466 if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64 )
468 if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
469 StringCchCat(pszOS, BUFSIZE, TEXT(
"Datacenter Edition for Itanium-based Systems" ));
470 else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
471 StringCchCat(pszOS, BUFSIZE, TEXT(
"Enterprise Edition for Itanium-based Systems" ));
474 else if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
476 if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
477 StringCchCat(pszOS, BUFSIZE, TEXT(
"Datacenter x64 Edition" ));
478 else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
479 StringCchCat(pszOS, BUFSIZE, TEXT(
"Enterprise x64 Edition" ));
480 else StringCchCat(pszOS, BUFSIZE, TEXT(
"Standard x64 Edition" ));
485 if ( osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER )
486 StringCchCat(pszOS, BUFSIZE, TEXT(
"Compute Cluster Edition" ));
487 else if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
488 StringCchCat(pszOS, BUFSIZE, TEXT(
"Datacenter Edition" ));
489 else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
490 StringCchCat(pszOS, BUFSIZE, TEXT(
"Enterprise Edition" ));
491 else if ( osvi.wSuiteMask & VER_SUITE_BLADE )
492 StringCchCat(pszOS, BUFSIZE, TEXT(
"Web Edition" ));
493 else StringCchCat(pszOS, BUFSIZE, TEXT(
"Standard Edition" ));
498 if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
500 StringCchCat(pszOS, BUFSIZE, TEXT(
"Windows XP "));
501 if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
502 StringCchCat(pszOS, BUFSIZE, TEXT(
"Home Edition" ));
503 else StringCchCat(pszOS, BUFSIZE, TEXT(
"Professional" ));
506 if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
508 StringCchCat(pszOS, BUFSIZE, TEXT(
"Windows 2000 "));
510 if ( osvi.wProductType == VER_NT_WORKSTATION )
512 StringCchCat(pszOS, BUFSIZE, TEXT(
"Professional" ));
516 if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
517 StringCchCat(pszOS, BUFSIZE, TEXT(
"Datacenter Server" ));
518 else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
519 StringCchCat(pszOS, BUFSIZE, TEXT(
"Advanced Server" ));
520 else StringCchCat(pszOS, BUFSIZE, TEXT(
"Server" ));
526 if( strlen(osvi.szCSDVersion) > 0 )
528 StringCchCat(pszOS, BUFSIZE, TEXT(
" ") );
529 StringCchCat(pszOS, BUFSIZE, osvi.szCSDVersion);
534 StringCchPrintf(
buf, 80, TEXT(
" (build %d)"), osvi.dwBuildNumber);
535 StringCchCat(pszOS, BUFSIZE,
buf);
537 if ( osvi.dwMajorVersion >= 6 )
539 if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
540 StringCchCat(pszOS, BUFSIZE, TEXT(
", 64-bit" ));
541 else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_INTEL )
542 StringCchCat(pszOS, BUFSIZE, TEXT(
", 32-bit"));
549 printf(
"This sample does not support this version of Windows.\n");
559 return std::string(
"*nix: failed to get os version");
560 return std::string() + un.sysname +
" " + un.version +
" " + un.release;
569 return get_windows_version_display_string();
578 std::string get_special_folder_path(
int nfolder,
bool iscreate)
580 WCHAR psz_path[MAX_PATH] = L
"";
582 if (SHGetSpecialFolderPathW(NULL, psz_path, nfolder, iscreate))
586 return string_tools::utf16_to_utf8(psz_path);
588 catch (
const std::exception &e)
590 MERROR(
"utf16_to_utf8 failed: " << e.what());
595 LOG_ERROR(
"SHGetSpecialFolderPathW() failed, could not obtain requested path.");
611 config_folder = get_special_folder_path(CSIDL_COMMON_APPDATA,
true) +
"\\" +
CRYPTONOTE_NAME;
614 char* pszHome = getenv(
"HOME");
615 if (pszHome == NULL || strlen(pszHome) == 0)
622 return config_folder;
627 namespace fs = boost::filesystem;
628 boost::system::error_code ec;
629 fs::path fs_path(path);
630 if (fs::is_directory(fs_path, ec))
635 bool res = fs::create_directories(fs_path, ec);
642 LOG_PRINT_L2(
"Can't create directory: " << path <<
", err: "<< ec.message());
654 try { wide_replacement_name = string_tools::utf8_to_utf16(old_name); }
655 catch (...) {
return std::error_code(GetLastError(), std::system_category()); }
657 try { wide_replaced_name = string_tools::utf8_to_utf16(new_name); }
658 catch (...) {
return std::error_code(GetLastError(), std::system_category()); }
660 DWORD attributes = ::GetFileAttributesW(wide_replaced_name.c_str());
661 if (INVALID_FILE_ATTRIBUTES != attributes)
663 ::SetFileAttributesW(wide_replaced_name.c_str(), attributes & (~FILE_ATTRIBUTE_READONLY));
666 bool ok = 0 != ::MoveFileExW(wide_replacement_name.c_str(), wide_replaced_name.c_str(), MOVEFILE_REPLACE_EXISTING);
667 code = ok ? 0 :
static_cast<int>(::GetLastError());
669 bool ok = 0 == std::rename(old_name.c_str(), new_name.c_str());
670 code = ok ? 0 : errno;
672 return std::error_code(code, std::system_category());
675 static bool unbound_built_with_threads()
678 if (!ctx)
return false;
679 char *
electroneum = strdup(
"electroneum"), *unbound = strdup(
"unbound");
686 MINFO(
"libunbound was built " << (with_threads ?
"with" :
"without") <<
" threads");
701 #if defined(__MINGW32__) || defined(__MINGW__) 705 setenv(
"LC_ALL",
"C", 1);
706 setenv(
"LANG",
"C", 1);
716 static LONG WINAPI windows_crash_handler(PEXCEPTION_POINTERS pExceptionInfo)
720 return EXCEPTION_CONTINUE_SEARCH;
722 static void setup_crash_dump()
724 SetUnhandledExceptionFilter(windows_crash_handler);
727 static void posix_crash_handler(
int signal)
736 static void setup_crash_dump()
738 signal(SIGSEGV, posix_crash_handler);
739 signal(SIGBUS, posix_crash_handler);
740 signal(SIGILL, posix_crash_handler);
741 signal(SIGFPE, posix_crash_handler);
745 static void setup_crash_dump() {}
752 struct rlimit rlimit;
753 rlimit.rlim_cur = rlimit.rlim_max = 0;
754 if (setrlimit(RLIMIT_CORE, &rlimit))
756 MWARNING(
"Failed to disable core dumps");
767 if (getrlimit(RLIMIT_MEMLOCK, &rlim) < 0)
769 MERROR(
"Failed to determine the lockable memory limit");
772 return rlim.rlim_cur;
787 const char *ver = gnu_get_libc_version();
788 if (!strcmp(ver,
"2.25"))
792 #if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(LIBRESSL_VERSION_TEXT) 795 OPENSSL_init_ssl(0, NULL);
798 if (!unbound_built_with_threads())
805 #if defined(__MINGW32__) || defined(__MINGW__) 808 mode_t mode = strict ? 077 : 0;
813 boost::optional<bool>
is_hdd(
const char *file_path)
818 if(stat(file_path, &st) == 0)
820 std::ostringstream s;
821 s <<
"/sys/dev/block/" << major(st.st_dev) <<
":" << minor(st.st_dev);
828 std::string attr_path = prefix +
"/queue/rotational";
829 std::ifstream f(attr_path, std::ios_base::in);
832 attr_path = prefix +
"/../queue/rotational";
833 f.open(attr_path, std::ios_base::in);
839 unsigned short val = 0xdead;
853 boost::mutex max_concurrency_lock;
854 unsigned max_concurrency = boost::thread::hardware_concurrency();
860 n = boost::thread::hardware_concurrency();
861 unsigned hwc = boost::thread::hardware_concurrency();
864 boost::lock_guard<boost::mutex> lock(max_concurrency_lock);
870 boost::lock_guard<boost::mutex> lock(max_concurrency_lock);
871 return max_concurrency;
877 if (boost::ends_with(
address,
".onion") || boost::ends_with(
address,
".i2p"))
887 MWARNING(
"Failed to determine whether address '" <<
address <<
"' is local, assuming not");
890 if (u_c.
host.empty())
892 MWARNING(
"Failed to determine whether address '" <<
address <<
"' is local, assuming not");
897 boost::asio::io_service io_service;
898 boost::asio::ip::tcp::resolver resolver(io_service);
899 boost::asio::ip::tcp::resolver::query query(u_c.
host,
"");
900 boost::asio::ip::tcp::resolver::iterator i = resolver.resolve(query);
901 while (i != boost::asio::ip::tcp::resolver::iterator())
903 const boost::asio::ip::tcp::endpoint &ep = *i;
904 if (ep.address().is_loopback())
915 int vercmp(
const char *v0,
const char *v1)
917 std::vector<std::string> f0,
f1;
918 boost::split(f0, v0, boost::is_any_of(
".-"));
919 boost::split(
f1, v1, boost::is_any_of(
".-"));
920 for (
size_t i = 0; i < std::max(f0.size(),
f1.size()); ++i) {
925 int f0i = atoi(f0[i].c_str()), f1i = atoi(
f1[i].c_str());
936 if (!SHA256_Init(&ctx))
938 if (!SHA256_Update(&ctx, data, len))
940 if (!SHA256_Final((
unsigned char*)
hash.data, &ctx))
950 f.exceptions(std::ifstream::failbit | std::ifstream::badbit);
951 f.open(filename, std::ios_base::binary | std::ios_base::in | std::ios::ate);
954 std::ifstream::pos_type file_size = f.tellg();
956 if (!SHA256_Init(&ctx))
958 size_t size_left = file_size;
959 f.seekg(0, std::ios::beg);
963 std::ifstream::pos_type read_size = size_left >
sizeof(
buf) ?
sizeof(
buf) : size_left;
964 f.read(
buf, read_size);
967 if (!SHA256_Update(&ctx,
buf, read_size))
969 size_left -= read_size;
972 if (!SHA256_Final((
unsigned char*)
hash.data, &ctx))
979 auto pos = str.find(
":");
980 bool r = pos != std::string::npos;
987 return std::make_pair(major, minor);
1000 }
else if (x == 2) {
1002 }
else if (x == 3) {
1008 std::cout <<
"\r" << s << std::flush;
1016 bool escape =
false;
1020 newval += escape ?
"*" :
".*";
1022 newval += escape ?
"?" :
".";
1024 newval +=
'\\', escape = !escape;
1034 HANDLE hConIn = CreateFileW(L
"CONIN$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr, OPEN_EXISTING, 0,
nullptr);
1037 FlushConsoleInputBuffer(hConIn);
1038 GetConsoleMode(hConIn, &oldMode);
1039 SetConsoleMode(hConIn, ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT);
1041 wchar_t buffer[1024];
1044 ReadConsoleW(hConIn, buffer,
sizeof(buffer)/
sizeof(
wchar_t)-1, &read,
nullptr);
1047 SetConsoleMode(hConIn, oldMode);
1048 CloseHandle(hConIn);
1050 int size_needed = WideCharToMultiByte(CP_UTF8, 0, buffer, -1, NULL, 0, NULL, NULL);
1052 WideCharToMultiByte(CP_UTF8, 0, buffer, -1, &
buf[0], size_needed, NULL, NULL);
1060 #if defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__ || defined __DragonFly__ 1063 #if defined __GLIBC__ 1064 const int sc_open_max = sysconf(_SC_OPEN_MAX);
1065 const int MAX_FDS = std::min(65536, sc_open_max);
1067 const int MAX_FDS = 65536;
1069 while (fd < MAX_FDS)
1080 if (ts < 1234567890)
1085 strftime(buffer,
sizeof(buffer),
"%Y-%m-%d %H:%M:%S", &tm);
1095 const char*
const format;
1099 static constexpr
const byte_map sizes[] =
1102 {
"%.2f KB", 1024 * 1024},
1105 {
"%.2f TB",
std::uint64_t(1024) * 1024 * 1024 * 1024 * 1024}
1110 bool operator()(
const byte_map& lhs,
const byte_map& rhs)
const noexcept
1112 return lhs.bytes < rhs.bytes;
1116 const auto size = std::upper_bound(
1117 std::begin(sizes), std::end(sizes) - 1, byte_map{
"", bytes}, bytes_less{}
1120 return (boost::format(size->format) % (
double(bytes) / divisor)).str();
void mlog_configure(const std::string &filename_base, bool console, const std::size_t max_log_file_size=MAX_LOG_FILE_SIZE, const std::size_t max_log_files=MAX_LOG_FILES)
#define MCLOG_RED(level, cat, x)
bool is_file_exist(const std::string &path)
struct ub_ctx * ub_ctx_create(void)
bool get_gmt_time(time_t t, struct tm &tm)
unsigned __int64 uint64_t
Useful when application has potentially harmful situtaions.
int ub_ctx_zone_add(struct ub_ctx *ctx, const char *zone_name, const char *zone_type)
const T & move(const T &t)
bool parse_url(const std::string url_str, http::url_content &content)
void ub_ctx_delete(struct ub_ctx *ctx)
int ub_ctx_async(struct ub_ctx *ctx, int dothread)
std::string to_string(t_connection_type type)