1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/utility.hpp"
4 #include "base/convert.hpp"
5 #include "base/application.hpp"
6 #include "base/logger.hpp"
7 #include "base/exception.hpp"
8 #include "base/socket.hpp"
9 #include "base/utility.hpp"
10 #include "base/json.hpp"
11 #include "base/objectlock.hpp"
14 #include <boost/filesystem/path.hpp>
15 #include <boost/filesystem/operations.hpp>
16 #include <boost/lexical_cast.hpp>
17 #include <boost/system/error_code.hpp>
18 #include <boost/thread/tss.hpp>
19 #include <boost/algorithm/string/trim.hpp>
20 #include <boost/algorithm/string/replace.hpp>
21 #include <boost/uuid/uuid_io.hpp>
22 #include <boost/uuid/uuid_generators.hpp>
33 # include <pthread_np.h>
34 #endif /* __FreeBSD__ */
38 #endif /* HAVE_CXXABI_H */
41 # include <sys/types.h>
42 # include <sys/utsname.h>
49 # include <VersionHelpers.h>
56 using namespace icinga;
58 boost::thread_specific_ptr<String> Utility::m_ThreadName;
59 boost::thread_specific_ptr<unsigned int> Utility::m_RandSeed;
62 double Utility::m_DebugTime = -1;
66 * Demangles a symbol name.
68 * @param sym The symbol name.
69 * @returns A human-readable version of the symbol name.
71 String Utility::DemangleSymbolName(const String& sym)
77 char *realname = abi::__cxa_demangle(sym.CStr(), nullptr, nullptr, &status);
80 result = String(realname);
83 #elif defined(_MSC_VER) /* HAVE_CXXABI_H */
86 if (UnDecorateSymbolName(sym.CStr(), output, sizeof(output), UNDNAME_COMPLETE) > 0)
89 /* We're pretty much out of options here. */
96 * Returns a human-readable type name of a type_info object.
98 * @param ti A type_info object.
99 * @returns The type name of the object.
101 String Utility::GetTypeName(const std::type_info& ti)
103 return DemangleSymbolName(ti.name());
106 String Utility::GetSymbolName(const void *addr)
111 if (dladdr(const_cast<void *>(addr), &dli) > 0)
112 return dli.dli_sname;
113 #endif /* HAVE_DLADDR */
116 char buffer[sizeof(SYMBOL_INFO)+MAX_SYM_NAME * sizeof(TCHAR)];
117 PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
118 pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
119 pSymbol->MaxNameLen = MAX_SYM_NAME;
121 DWORD64 dwAddress = (DWORD64)addr;
122 DWORD64 dwDisplacement;
124 IMAGEHLP_LINE64 line;
125 line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
127 if (SymFromAddr(GetCurrentProcess(), dwAddress, &dwDisplacement, pSymbol)) {
129 if (UnDecorateSymbolName(pSymbol->Name, output, sizeof(output), UNDNAME_COMPLETE))
130 return String(output) + "+" + Convert::ToString(dwDisplacement);
132 return String(pSymbol->Name) + "+" + Convert::ToString(dwDisplacement);
136 return "(unknown function)";
140 * Performs wildcard pattern matching.
142 * @param pattern The wildcard pattern.
143 * @param text The String that should be checked.
144 * @returns true if the wildcard pattern matches, false otherwise.
146 bool Utility::Match(const String& pattern, const String& text)
148 return (match(pattern.CStr(), text.CStr()) == 0);
151 static bool ParseIp(const String& ip, char addr[16], int *proto)
153 if (inet_pton(AF_INET, ip.CStr(), addr + 12) == 1) {
154 /* IPv4-mapped IPv6 address (::ffff:<ipv4-bits>) */
156 memset(addr + 10, 0xff, 2);
162 if (inet_pton(AF_INET6, ip.CStr(), addr) == 1) {
171 static void ParseIpMask(const String& ip, char mask[16], int *bits)
173 String::SizeType slashp = ip.FindFirstOf("/");
176 if (slashp == String::NPos) {
180 uip = ip.SubStr(0, slashp);
181 *bits = Convert::ToLong(ip.SubStr(slashp + 1));
186 if (!ParseIp(uip, mask, &proto))
187 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid IP address specified."));
189 if (proto == AF_INET) {
190 if (*bits > 32 || *bits < 0)
191 BOOST_THROW_EXCEPTION(std::invalid_argument("Mask must be between 0 and 32 for IPv4 CIDR masks."));
196 if (slashp == String::NPos)
199 if (*bits > 128 || *bits < 0)
200 BOOST_THROW_EXCEPTION(std::invalid_argument("Mask must be between 0 and 128 for IPv6 CIDR masks."));
202 for (int i = 0; i < 16; i++) {
203 int lbits = std::max(0, *bits - i * 8);
208 if (mask[i] & (0xff >> lbits))
209 BOOST_THROW_EXCEPTION(std::invalid_argument("Masked-off bits must all be zero."));
213 static bool IpMaskCheck(char addr[16], char mask[16], int bits)
215 for (int i = 0; i < 16; i++) {
217 return !((addr[i] ^ mask[i]) >> (8 - bits));
219 if (mask[i] != addr[i])
231 bool Utility::CidrMatch(const String& pattern, const String& ip)
236 ParseIpMask(pattern, mask, &bits);
241 if (!ParseIp(ip, addr, &proto))
244 return IpMaskCheck(addr, mask, bits);
248 * Returns the directory component of a path. See dirname(3) for details.
250 * @param path The full path.
251 * @returns The directory.
253 String Utility::DirName(const String& path)
255 return boost::filesystem::path(path.Begin(), path.End()).parent_path().string();
259 * Returns the file component of a path. See basename(3) for details.
261 * @param path The full path.
262 * @returns The filename.
264 String Utility::BaseName(const String& path)
266 return boost::filesystem::path(path.Begin(), path.End()).filename().string();
270 * Null deleter. Used as a parameter for the shared_ptr constructor.
272 * @param - The object that should be deleted.
274 void Utility::NullDeleter(void *)
276 /* Nothing to do here. */
281 * (DEBUG / TESTING ONLY) Sets the current system time to a static value,
282 * that will be be retrieved by any component of Icinga, when using GetTime().
284 * This should be only used for testing purposes, e.g. unit tests and debugging of certain functionalities.
286 void Utility::SetTime(double time)
292 * (DEBUG / TESTING ONLY) Increases the set debug system time by X seconds.
294 * This should be only used for testing purposes, e.g. unit tests and debugging of certain functionalities.
296 void Utility::IncrementTime(double diff)
300 #endif /* I2_DEBUG */
303 * Returns the current UNIX timestamp including fractions of seconds.
305 * @returns The current time.
307 double Utility::GetTime()
310 if (m_DebugTime >= 0) {
311 // (DEBUG / TESTING ONLY) this will return a *STATIC* system time, if the value has been set!
314 #endif /* I2_DEBUG */
317 GetSystemTimeAsFileTime(&cft);
320 ucft.HighPart = cft.dwHighDateTime;
321 ucft.LowPart = cft.dwLowDateTime;
323 SYSTEMTIME est = { 1970, 1, 4, 1, 0, 0, 0, 0};
325 SystemTimeToFileTime(&est, &eft);
328 ueft.HighPart = eft.dwHighDateTime;
329 ueft.LowPart = eft.dwLowDateTime;
331 return ((ucft.QuadPart - ueft.QuadPart) / 10000) / 1000.0;
335 int rc = gettimeofday(&tv, nullptr);
338 return tv.tv_sec + tv.tv_usec / 1000000.0;
343 * Returns the ID of the current process.
347 pid_t Utility::GetPid()
352 return GetCurrentProcessId();
357 * Sleeps for the specified amount of time.
359 * @param timeout The timeout in seconds.
361 void Utility::Sleep(double timeout)
364 unsigned long micros = timeout * 1000000u;
366 sleep((unsigned)timeout);
368 usleep(micros % 1000000u);
370 ::Sleep(timeout * 1000);
375 * Generates a new unique ID.
377 * @returns The new unique ID.
379 String Utility::NewUniqueID()
381 return boost::lexical_cast<std::string>(boost::uuids::random_generator()());
385 static bool GlobHelper(const String& pathSpec, int type, std::vector<String>& files, std::vector<String>& dirs)
390 handle = FindFirstFile(pathSpec.CStr(), &wfd);
392 if (handle == INVALID_HANDLE_VALUE) {
393 DWORD errorCode = GetLastError();
395 if (errorCode == ERROR_FILE_NOT_FOUND)
398 BOOST_THROW_EXCEPTION(win32_error()
399 << boost::errinfo_api_function("FindFirstFile")
400 << errinfo_win32_error(errorCode)
401 << boost::errinfo_file_name(pathSpec));
405 if (strcmp(wfd.cFileName, ".") == 0 || strcmp(wfd.cFileName, "..") == 0)
408 String path = Utility::DirName(pathSpec) + "/" + wfd.cFileName;
410 if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (type & GlobDirectory))
411 dirs.push_back(path);
412 else if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (type & GlobFile))
413 files.push_back(path);
414 } while (FindNextFile(handle, &wfd));
416 if (!FindClose(handle)) {
417 BOOST_THROW_EXCEPTION(win32_error()
418 << boost::errinfo_api_function("FindClose")
419 << errinfo_win32_error(GetLastError()));
427 static int GlobErrorHandler(const char *epath, int eerrno)
429 if (eerrno == ENOTDIR)
437 * Calls the specified callback for each file matching the path specification.
439 * @param pathSpec The path specification.
440 * @param callback The callback which is invoked for each matching file.
441 * @param type The file type (a combination of GlobFile and GlobDirectory)
443 bool Utility::Glob(const String& pathSpec, const std::function<void (const String&)>& callback, int type)
445 std::vector<String> files, dirs;
448 std::vector<String> tokens = pathSpec.Split("\\/");
452 for (std::vector<String>::size_type i = 0; i < tokens.size() - 1; i++) {
453 const String& token = tokens[i];
455 if (!part1.IsEmpty())
460 if (token.FindFirstOf("?*") != String::NPos) {
463 for (std::vector<String>::size_type k = i + 1; k < tokens.size(); k++) {
464 if (!part2.IsEmpty())
470 std::vector<String> files2, dirs2;
472 if (!GlobHelper(part1, GlobDirectory, files2, dirs2))
475 for (const String& dir : dirs2) {
476 if (!Utility::Glob(dir + "/" + part2, callback, type))
484 if (!GlobHelper(part1 + "/" + tokens[tokens.size() - 1], type, files, dirs))
489 int rc = glob(pathSpec.CStr(), GLOB_NOSORT, GlobErrorHandler, &gr);
492 if (rc == GLOB_NOMATCH)
495 BOOST_THROW_EXCEPTION(posix_error()
496 << boost::errinfo_api_function("glob")
497 << boost::errinfo_errno(errno)
498 << boost::errinfo_file_name(pathSpec));
501 if (gr.gl_pathc == 0) {
508 for (gp = gr.gl_pathv, left = gr.gl_pathc; left > 0; gp++, left--) {
511 if (stat(*gp, &statbuf) < 0)
514 if (!S_ISDIR(statbuf.st_mode) && !S_ISREG(statbuf.st_mode))
517 if (S_ISDIR(statbuf.st_mode) && (type & GlobDirectory))
518 dirs.emplace_back(*gp);
519 else if (!S_ISDIR(statbuf.st_mode) && (type & GlobFile))
520 files.emplace_back(*gp);
526 std::sort(files.begin(), files.end());
527 for (const String& cpath : files) {
531 std::sort(dirs.begin(), dirs.end());
532 for (const String& cpath : dirs) {
540 * Calls the specified callback for each file in the specified directory
541 * or any of its child directories if the file name matches the specified
544 * @param path The path.
545 * @param pattern The pattern.
546 * @param callback The callback which is invoked for each matching file.
547 * @param type The file type (a combination of GlobFile and GlobDirectory)
549 bool Utility::GlobRecursive(const String& path, const String& pattern, const std::function<void (const String&)>& callback, int type)
551 std::vector<String> files, dirs, alldirs;
557 String pathSpec = path + "/*";
559 handle = FindFirstFile(pathSpec.CStr(), &wfd);
561 if (handle == INVALID_HANDLE_VALUE) {
562 DWORD errorCode = GetLastError();
564 if (errorCode == ERROR_FILE_NOT_FOUND)
567 BOOST_THROW_EXCEPTION(win32_error()
568 << boost::errinfo_api_function("FindFirstFile")
569 << errinfo_win32_error(errorCode)
570 << boost::errinfo_file_name(pathSpec));
574 if (strcmp(wfd.cFileName, ".") == 0 || strcmp(wfd.cFileName, "..") == 0)
577 String cpath = path + "/" + wfd.cFileName;
579 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
580 alldirs.push_back(cpath);
582 if (!Utility::Match(pattern, wfd.cFileName))
585 if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (type & GlobFile))
586 files.push_back(cpath);
588 if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (type & GlobDirectory))
589 dirs.push_back(cpath);
590 } while (FindNextFile(handle, &wfd));
592 if (!FindClose(handle)) {
593 BOOST_THROW_EXCEPTION(win32_error()
594 << boost::errinfo_api_function("FindClose")
595 << errinfo_win32_error(GetLastError()));
600 dirp = opendir(path.CStr());
603 BOOST_THROW_EXCEPTION(posix_error()
604 << boost::errinfo_api_function("opendir")
605 << boost::errinfo_errno(errno)
606 << boost::errinfo_file_name(path));
612 pent = readdir(dirp);
613 if (!pent && errno != 0) {
616 BOOST_THROW_EXCEPTION(posix_error()
617 << boost::errinfo_api_function("readdir")
618 << boost::errinfo_errno(errno)
619 << boost::errinfo_file_name(path));
625 if (strcmp(pent->d_name, ".") == 0 || strcmp(pent->d_name, "..") == 0)
628 String cpath = path + "/" + pent->d_name;
632 if (stat(cpath.CStr(), &statbuf) < 0)
635 if (S_ISDIR(statbuf.st_mode))
636 alldirs.push_back(cpath);
638 if (!Utility::Match(pattern, pent->d_name))
641 if (S_ISDIR(statbuf.st_mode) && (type & GlobDirectory))
642 dirs.push_back(cpath);
644 if (!S_ISDIR(statbuf.st_mode) && (type & GlobFile))
645 files.push_back(cpath);
652 std::sort(files.begin(), files.end());
653 for (const String& cpath : files) {
657 std::sort(dirs.begin(), dirs.end());
658 for (const String& cpath : dirs) {
662 std::sort(alldirs.begin(), alldirs.end());
663 for (const String& cpath : alldirs) {
664 GlobRecursive(cpath, pattern, callback, type);
671 void Utility::MkDir(const String& path, int mode)
675 if (mkdir(path.CStr(), mode) < 0 && errno != EEXIST) {
677 if (mkdir(path.CStr()) < 0 && errno != EEXIST) {
680 BOOST_THROW_EXCEPTION(posix_error()
681 << boost::errinfo_api_function("mkdir")
682 << boost::errinfo_errno(errno)
683 << boost::errinfo_file_name(path));
687 void Utility::MkDirP(const String& path, int mode)
691 while (pos != String::NPos) {
693 pos = path.Find("/", pos + 1);
695 pos = path.FindFirstOf("/\\", pos + 1);
698 String spath = path.SubStr(0, pos + 1);
700 if (stat(spath.CStr(), &statbuf) < 0 && errno == ENOENT)
701 MkDir(path.SubStr(0, pos), mode);
705 void Utility::Remove(const String& path)
707 namespace fs = boost::filesystem;
709 (void)fs::remove(fs::path(path.Begin(), path.End()));
712 void Utility::RemoveDirRecursive(const String& path)
714 namespace fs = boost::filesystem;
716 (void)fs::remove_all(fs::path(path.Begin(), path.End()));
720 * Copies a source file to a target location.
721 * Caller must ensure that the target's base directory exists and is writable.
723 void Utility::CopyFile(const String& source, const String& target)
725 namespace fs = boost::filesystem;
727 fs::copy_file(fs::path(source.Begin(), source.End()), fs::path(target.Begin(), target.End()), fs::copy_option::overwrite_if_exists);
731 * Renames a source file to a target location.
732 * Caller must ensure that the target's base directory exists and is writable.
734 void Utility::RenameFile(const String& source, const String& target)
736 namespace fs = boost::filesystem;
738 fs::rename(fs::path(source.Begin(), source.End()), fs::path(target.Begin(), target.End()));
742 * Set file permissions
744 bool Utility::SetFileOwnership(const String& file, const String& user, const String& group)
748 struct passwd *pw = getpwnam(user.CStr());
752 Log(LogCritical, "cli")
753 << "Invalid user specified: " << user;
756 Log(LogCritical, "cli")
757 << "getpwnam() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
763 struct group *gr = getgrnam(group.CStr());
767 Log(LogCritical, "cli")
768 << "Invalid group specified: " << group;
771 Log(LogCritical, "cli")
772 << "getgrnam() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
777 if (chown(file.CStr(), pw->pw_uid, gr->gr_gid) < 0) {
778 Log(LogCritical, "cli")
779 << "chown() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
788 void Utility::SetNonBlocking(int fd, bool nb)
790 int flags = fcntl(fd, F_GETFL, 0);
793 BOOST_THROW_EXCEPTION(posix_error()
794 << boost::errinfo_api_function("fcntl")
795 << boost::errinfo_errno(errno));
801 flags &= ~O_NONBLOCK;
803 if (fcntl(fd, F_SETFL, flags) < 0) {
804 BOOST_THROW_EXCEPTION(posix_error()
805 << boost::errinfo_api_function("fcntl")
806 << boost::errinfo_errno(errno));
810 void Utility::SetCloExec(int fd, bool cloexec)
812 int flags = fcntl(fd, F_GETFD, 0);
815 BOOST_THROW_EXCEPTION(posix_error()
816 << boost::errinfo_api_function("fcntl")
817 << boost::errinfo_errno(errno));
823 flags &= ~FD_CLOEXEC;
825 if (fcntl(fd, F_SETFD, flags) < 0) {
826 BOOST_THROW_EXCEPTION(posix_error()
827 << boost::errinfo_api_function("fcntl")
828 << boost::errinfo_errno(errno));
833 void Utility::SetNonBlockingSocket(SOCKET s, bool nb)
836 SetNonBlocking(s, nb);
838 unsigned long lflag = nb;
839 ioctlsocket(s, FIONBIO, &lflag);
843 void Utility::QueueAsyncCallback(const std::function<void ()>& callback, SchedulerPolicy policy)
845 Application::GetTP().Post(callback, policy);
848 String Utility::NaturalJoin(const std::vector<String>& tokens)
852 for (std::vector<String>::size_type i = 0; i < tokens.size(); i++) {
855 if (tokens.size() > i + 1) {
856 if (i < tokens.size() - 2)
858 else if (i == tokens.size() - 2)
866 String Utility::Join(const Array::Ptr& tokens, char separator, bool escapeSeparator)
871 ObjectLock olock(tokens);
872 for (const Value& vtoken : tokens) {
873 String token = Convert::ToString(vtoken);
875 if (escapeSeparator) {
876 boost::algorithm::replace_all(token, "\\", "\\\\");
878 char sep_before[2], sep_after[3];
879 sep_before[0] = separator;
880 sep_before[1] = '\0';
882 sep_after[1] = separator;
884 boost::algorithm::replace_all(token, sep_before, sep_after);
890 result += String(1, separator);
898 String Utility::FormatDuration(double duration)
900 std::vector<String> tokens;
903 if (duration >= 86400) {
904 int days = duration / 86400;
905 tokens.emplace_back(Convert::ToString(days) + (days != 1 ? " days" : " day"));
906 duration = static_cast<int>(duration) % 86400;
909 if (duration >= 3600) {
910 int hours = duration / 3600;
911 tokens.emplace_back(Convert::ToString(hours) + (hours != 1 ? " hours" : " hour"));
912 duration = static_cast<int>(duration) % 3600;
915 if (duration >= 60) {
916 int minutes = duration / 60;
917 tokens.emplace_back(Convert::ToString(minutes) + (minutes != 1 ? " minutes" : " minute"));
918 duration = static_cast<int>(duration) % 60;
922 int seconds = duration;
923 tokens.emplace_back(Convert::ToString(seconds) + (seconds != 1 ? " seconds" : " second"));
926 if (tokens.size() == 0) {
927 int milliseconds = std::floor(duration * 1000);
928 if (milliseconds >= 1)
929 tokens.emplace_back(Convert::ToString(milliseconds) + (milliseconds != 1 ? " milliseconds" : " millisecond"));
931 tokens.emplace_back("less than 1 millisecond");
934 return NaturalJoin(tokens);
937 String Utility::FormatDateTime(const char *format, double ts)
940 auto tempts = (time_t)ts; /* We don't handle sub-second timestamps here just yet. */
944 tm *temp = localtime(&tempts);
947 BOOST_THROW_EXCEPTION(posix_error()
948 << boost::errinfo_api_function("localtime")
949 << boost::errinfo_errno(errno));
954 if (!localtime_r(&tempts, &tmthen)) {
955 BOOST_THROW_EXCEPTION(posix_error()
956 << boost::errinfo_api_function("localtime_r")
957 << boost::errinfo_errno(errno));
959 #endif /* _MSC_VER */
961 strftime(timestamp, sizeof(timestamp), format, &tmthen);
966 String Utility::FormatErrorNumber(int code) {
967 std::ostringstream msgbuf;
971 String result = "Unknown error.";
973 DWORD rc = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
974 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, code, 0, (char *)&message,
978 result = String(message);
981 /* remove trailing new-line characters */
982 boost::algorithm::trim_right(result);
985 msgbuf << code << ", \"" << result << "\"";
987 msgbuf << strerror(code);
992 String Utility::EscapeShellCmd(const String& s)
995 size_t prev_quote = String::NPos;
1004 if (ch == '%' || ch == '"' || ch == '\'')
1007 if (ch == '"' || ch == '\'') {
1008 /* Find a matching closing quotation character. */
1009 if (prev_quote == String::NPos && (prev_quote = s.FindFirstOf(ch, index + 1)) != String::NPos)
1010 ; /* Empty statement. */
1011 else if (prev_quote != String::NPos && s[prev_quote] == ch)
1012 prev_quote = String::NPos;
1018 if (ch == '#' || ch == '&' || ch == ';' || ch == '`' || ch == '|' ||
1019 ch == '*' || ch == '?' || ch == '~' || ch == '<' || ch == '>' ||
1020 ch == '^' || ch == '(' || ch == ')' || ch == '[' || ch == ']' ||
1021 ch == '{' || ch == '}' || ch == '$' || ch == '\\' || ch == '\x0A' ||
1038 String Utility::EscapeShellArg(const String& s)
1050 if (ch == '"' || ch == '%') {
1070 String Utility::EscapeCreateProcessArg(const String& arg)
1072 if (arg.FindFirstOf(" \t\n\v\"") == String::NPos)
1075 String result = "\"";
1077 for (String::ConstIterator it = arg.Begin(); ; it++) {
1078 int numBackslashes = 0;
1080 while (it != arg.End() && *it == '\\') {
1085 if (it == arg.End()) {
1086 result.Append(numBackslashes * 2, '\\');
1088 } else if (*it == '"') {
1089 result.Append(numBackslashes * 2, '\\');
1090 result.Append(1, *it);
1092 result.Append(numBackslashes, '\\');
1093 result.Append(1, *it);
1104 static void WindowsSetThreadName(const char *name)
1106 THREADNAME_INFO info;
1107 info.dwType = 0x1000;
1109 info.dwThreadID = -1;
1113 RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR *)&info);
1114 } __except(EXCEPTION_EXECUTE_HANDLER) {
1115 /* Nothing to do here. */
1120 void Utility::SetThreadName(const String& name, bool os)
1122 m_ThreadName.reset(new String(name));
1128 WindowsSetThreadName(name.CStr());
1131 #ifdef HAVE_PTHREAD_SET_NAME_NP
1132 pthread_set_name_np(pthread_self(), name.CStr());
1133 #endif /* HAVE_PTHREAD_SET_NAME_NP */
1135 #ifdef HAVE_PTHREAD_SETNAME_NP
1137 pthread_setname_np(name.CStr());
1138 # else /* __APPLE__ */
1139 String tname = name.SubStr(0, 15);
1140 pthread_setname_np(pthread_self(), tname.CStr());
1141 # endif /* __APPLE__ */
1142 #endif /* HAVE_PTHREAD_SETNAME_NP */
1145 String Utility::GetThreadName()
1147 String *name = m_ThreadName.get();
1150 std::ostringstream idbuf;
1151 idbuf << std::this_thread::get_id();
1158 unsigned long Utility::SDBM(const String& str, size_t len)
1160 unsigned long hash = 0;
1163 for (char c : str) {
1167 hash = c + (hash << 6) + (hash << 16) - hash;
1175 int Utility::CompareVersion(const String& v1, const String& v2)
1177 std::vector<String> tokensv1 = v1.Split(".");
1178 std::vector<String> tokensv2 = v2.Split(".");
1180 for (std::vector<String>::size_type i = 0; i < tokensv2.size() - tokensv1.size(); i++)
1181 tokensv1.emplace_back("0");
1183 for (std::vector<String>::size_type i = 0; i < tokensv1.size() - tokensv2.size(); i++)
1184 tokensv2.emplace_back("0");
1186 for (std::vector<String>::size_type i = 0; i < tokensv1.size(); i++) {
1187 if (Convert::ToLong(tokensv2[i]) > Convert::ToLong(tokensv1[i]))
1189 else if (Convert::ToLong(tokensv2[i]) < Convert::ToLong(tokensv1[i]))
1196 String Utility::GetHostName()
1200 if (gethostname(name, sizeof(name)) < 0)
1207 * Returns the fully-qualified domain name for the host
1210 * @returns The FQDN.
1212 String Utility::GetFQDN()
1214 String hostname = GetHostName();
1217 memset(&hints, 0, sizeof(hints));
1218 hints.ai_family = AF_UNSPEC;
1219 hints.ai_socktype = SOCK_DGRAM;
1220 hints.ai_flags = AI_CANONNAME;
1223 int rc = getaddrinfo(hostname.CStr(), nullptr, &hints, &result);
1229 if (strcmp(result->ai_canonname, "localhost") != 0)
1230 hostname = result->ai_canonname;
1232 freeaddrinfo(result);
1238 int Utility::Random()
1243 unsigned int *seed = m_RandSeed.get();
1246 seed = new unsigned int(Utility::GetTime());
1247 m_RandSeed.reset(seed);
1250 return rand_r(seed);
1254 tm Utility::LocalTime(time_t ts)
1257 tm *result = localtime(&ts);
1260 BOOST_THROW_EXCEPTION(posix_error()
1261 << boost::errinfo_api_function("localtime")
1262 << boost::errinfo_errno(errno));
1266 #else /* _MSC_VER */
1269 if (!localtime_r(&ts, &result)) {
1270 BOOST_THROW_EXCEPTION(posix_error()
1271 << boost::errinfo_api_function("localtime_r")
1272 << boost::errinfo_errno(errno));
1276 #endif /* _MSC_VER */
1279 bool Utility::PathExists(const String& path)
1281 namespace fs = boost::filesystem;
1283 boost::system::error_code ec;
1285 return fs::exists(fs::path(path.Begin(), path.End()), ec) && !ec;
1288 Value Utility::LoadJsonFile(const String& path)
1291 fp.open(path.CStr());
1293 String json((std::istreambuf_iterator<char>(fp)), std::istreambuf_iterator<char>());
1298 BOOST_THROW_EXCEPTION(std::runtime_error("Could not read JSON file '" + path + "'."));
1300 return JsonDecode(json);
1303 void Utility::SaveJsonFile(const String& path, int mode, const Value& value)
1305 namespace fs = boost::filesystem;
1308 String tempFilename = Utility::CreateTempFile(path + ".XXXXXX", mode, fp);
1310 fp.exceptions(std::ofstream::failbit | std::ofstream::badbit);
1311 fp << JsonEncode(value);
1314 RenameFile(tempFilename, path);
1317 static void HexEncode(char ch, std::ostream& os)
1319 const char *hex_chars = "0123456789ABCDEF";
1321 os << hex_chars[ch >> 4 & 0x0f];
1322 os << hex_chars[ch & 0x0f];
1325 static int HexDecode(char hc)
1327 if (hc >= '0' && hc <= '9')
1329 else if (hc >= 'a' && hc <= 'f')
1330 return hc - 'a' + 10;
1331 else if (hc >= 'A' && hc <= 'F')
1332 return hc - 'A' + 10;
1334 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid hex character."));
1337 String Utility::EscapeString(const String& s, const String& chars, const bool illegal)
1339 std::ostringstream result;
1342 if (chars.FindFirstOf(ch) != String::NPos || ch == '%') {
1344 HexEncode(ch, result);
1350 if (chars.FindFirstOf(ch) == String::NPos || ch == '%') {
1352 HexEncode(ch, result);
1358 return result.str();
1361 String Utility::UnescapeString(const String& s)
1363 std::ostringstream result;
1365 for (String::SizeType i = 0; i < s.GetLength(); i++) {
1367 if (i + 2 > s.GetLength() - 1)
1368 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid escape sequence."));
1370 char ch = HexDecode(s[i + 1]) * 16 + HexDecode(s[i + 2]);
1378 return result.str();
1382 static String UnameHelper(char type)
1384 struct utsname name;
1389 return (char*)name.machine;
1391 return (char*)name.nodename;
1393 return (char*)name.release;
1395 return (char*)name.sysname;
1397 return (char*)name.version;
1399 VERIFY(!"Invalid uname query.");
1403 static bool ReleaseHelper(String *platformName, String *platformVersion)
1407 *platformName = "Windows";
1409 if (platformVersion) {
1410 *platformVersion = "Vista";
1411 if (IsWindowsVistaSP1OrGreater())
1412 *platformVersion = "Vista SP1";
1413 if (IsWindowsVistaSP2OrGreater())
1414 *platformVersion = "Vista SP2";
1415 if (IsWindows7OrGreater())
1416 *platformVersion = "7";
1417 if (IsWindows7SP1OrGreater())
1418 *platformVersion = "7 SP1";
1419 if (IsWindows8OrGreater())
1420 *platformVersion = "8";
1421 if (IsWindows8Point1OrGreater())
1422 *platformVersion = "8.1 or greater";
1423 if (IsWindowsServer())
1424 *platformVersion += " (Server)";
1430 *platformName = "Unknown";
1432 if (platformVersion)
1433 *platformVersion = "Unknown";
1435 /* You have systemd or Ubuntu etc. */
1436 std::ifstream release("/etc/os-release");
1437 if (release.is_open()) {
1438 std::string release_line;
1439 while (getline(release, release_line)) {
1440 std::string::size_type pos = release_line.find("=");
1442 if (pos == std::string::npos)
1445 std::string key = release_line.substr(0, pos);
1446 std::string value = release_line.substr(pos + 1);
1448 std::string::size_type firstQuote = value.find("\"");
1450 if (firstQuote != std::string::npos)
1451 value.erase(0, firstQuote + 1);
1453 std::string::size_type lastQuote = value.rfind("\"");
1455 if (lastQuote != std::string::npos)
1456 value.erase(lastQuote);
1458 if (platformName && key == "NAME")
1459 *platformName = value;
1461 if (platformVersion && key == "VERSION")
1462 *platformVersion = value;
1468 /* You are using a distribution which supports LSB. */
1469 FILE *fp = popen("type lsb_release >/dev/null 2>&1 && lsb_release -s -i 2>&1", "r");
1472 std::ostringstream msgbuf;
1474 while (fgets(line, sizeof(line), fp))
1476 int status = pclose(fp);
1477 if (WEXITSTATUS(status) == 0) {
1479 *platformName = msgbuf.str();
1483 fp = popen("type lsb_release >/dev/null 2>&1 && lsb_release -s -r 2>&1", "r");
1486 std::ostringstream msgbuf;
1488 while (fgets(line, sizeof(line), fp))
1490 int status = pclose(fp);
1491 if (WEXITSTATUS(status) == 0) {
1492 if (platformVersion)
1493 *platformVersion = msgbuf.str();
1498 fp = popen("type sw_vers >/dev/null 2>&1 && sw_vers -productName 2>&1", "r");
1501 std::ostringstream msgbuf;
1503 while (fgets(line, sizeof(line), fp))
1505 int status = pclose(fp);
1506 if (WEXITSTATUS(status) == 0) {
1507 String info = msgbuf.str();
1511 *platformName = info;
1515 fp = popen("type sw_vers >/dev/null 2>&1 && sw_vers -productVersion 2>&1", "r");
1518 std::ostringstream msgbuf;
1520 while (fgets(line, sizeof(line), fp))
1522 int status = pclose(fp);
1523 if (WEXITSTATUS(status) == 0) {
1524 String info = msgbuf.str();
1527 if (platformVersion)
1528 *platformVersion = info;
1534 /* Centos/RHEL < 7 */
1536 release.open("/etc/redhat-release");
1537 if (release.is_open()) {
1538 std::string release_line;
1539 getline(release, release_line);
1541 String info = release_line;
1543 /* example: Red Hat Enterprise Linux Server release 6.7 (Santiago) */
1545 *platformName = info.SubStr(0, info.Find("release") - 1);
1547 if (platformVersion)
1548 *platformVersion = info.SubStr(info.Find("release") + 8);
1553 /* sles 11 sp3, opensuse w/e */
1555 release.open("/etc/SuSE-release");
1556 if (release.is_open()) {
1557 std::string release_line;
1558 getline(release, release_line);
1560 String info = release_line;
1563 *platformName = info.SubStr(0, info.FindFirstOf(" "));
1565 if (platformVersion)
1566 *platformVersion = info.SubStr(info.FindFirstOf(" ") + 1);
1576 String Utility::GetPlatformKernel()
1581 return UnameHelper('s');
1585 String Utility::GetPlatformKernelVersion()
1589 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1590 GetVersionEx(&info);
1592 std::ostringstream msgbuf;
1593 msgbuf << info.dwMajorVersion << "." << info.dwMinorVersion;
1595 return msgbuf.str();
1597 return UnameHelper('r');
1601 String Utility::GetPlatformName()
1603 String platformName;
1604 if (!ReleaseHelper(&platformName, nullptr))
1606 return platformName;
1609 String Utility::GetPlatformVersion()
1611 String platformVersion;
1612 if (!ReleaseHelper(nullptr, &platformVersion))
1614 return platformVersion;
1617 String Utility::GetPlatformArchitecture()
1621 GetNativeSystemInfo(&info);
1622 switch (info.wProcessorArchitecture) {
1623 case PROCESSOR_ARCHITECTURE_AMD64:
1625 case PROCESSOR_ARCHITECTURE_ARM:
1627 case PROCESSOR_ARCHITECTURE_INTEL:
1633 return UnameHelper('m');
1637 const char l_Utf8Replacement[] = "\xEF\xBF\xBD";
1639 String Utility::ValidateUTF8(const String& input)
1641 std::vector<char> output;
1642 output.reserve(input.GetLength() * 3u);
1645 utf8::replace_invalid(input.Begin(), input.End(), std::back_inserter(output));
1646 } catch (const utf8::not_enough_room&) {
1647 output.insert(output.end(), (const char*)l_Utf8Replacement, (const char*)l_Utf8Replacement + 3);
1650 return String(output.begin(), output.end());
1653 String Utility::CreateTempFile(const String& path, int mode, std::fstream& fp)
1655 std::vector<char> targetPath(path.Begin(), path.End());
1656 targetPath.push_back('\0');
1660 fd = mkstemp(&targetPath[0]);
1662 fd = MksTemp(&targetPath[0]);
1666 BOOST_THROW_EXCEPTION(posix_error()
1667 << boost::errinfo_api_function("mkstemp")
1668 << boost::errinfo_errno(errno)
1669 << boost::errinfo_file_name(path));
1673 fp.open(&targetPath[0], std::ios_base::trunc | std::ios_base::out);
1674 } catch (const std::fstream::failure&) {
1681 String resultPath = String(targetPath.begin(), targetPath.end() - 1);
1683 if (chmod(resultPath.CStr(), mode) < 0) {
1684 BOOST_THROW_EXCEPTION(posix_error()
1685 << boost::errinfo_api_function("chmod")
1686 << boost::errinfo_errno(errno)
1687 << boost::errinfo_file_name(resultPath));
1694 /* mkstemp extracted from libc/sysdeps/posix/tempname.c. Copyright
1695 * (C) 1991-1999, 2000, 2001, 2006 Free Software Foundation, Inc.
1697 * The GNU C Library is free software; you can redistribute it and/or
1698 * modify it under the terms of the GNU Lesser General Public
1699 * License as published by the Free Software Foundation; either
1700 * version 2.1 of the License, or (at your option) any later version.
1703 #define _O_EXCL 0x0400
1704 #define _O_CREAT 0x0100
1705 #define _O_RDWR 0x0002
1706 #define O_EXCL _O_EXCL
1707 #define O_CREAT _O_CREAT
1708 #define O_RDWR _O_RDWR
1710 static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
1712 /* Generate a temporary file name based on TMPL. TMPL must match the
1713 * rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
1714 * does not exist at the time of the call to mkstemp. TMPL is
1715 * overwritten with the result.
1717 int Utility::MksTemp(char *tmpl)
1721 static unsigned long long value;
1722 unsigned long long random_time_bits;
1725 int save_errno = errno;
1727 /* A lower bound on the number of temporary files to attempt to
1728 * generate. The maximum total number of temporary file names that
1729 * can exist for a given template is 62**6. It should never be
1730 * necessary to try all these combinations. Instead if a reasonable
1731 * number of names is tried (we define reasonable as 62**3) fail to
1732 * give the system administrator the chance to remove the problems.
1734 #define ATTEMPTS_MIN (62 * 62 * 62)
1736 /* The number of times to attempt to generate a temporary file
1737 * To conform to POSIX, this must be no smaller than TMP_MAX.
1739 #if ATTEMPTS_MIN < TMP_MAX
1740 unsigned int attempts = TMP_MAX;
1742 unsigned int attempts = ATTEMPTS_MIN;
1745 len = strlen (tmpl);
1746 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX")) {
1751 /* This is where the Xs start. */
1752 XXXXXX = &tmpl[len - 6];
1754 /* Get some more or less random data. */
1760 GetSystemTime(&stNow);
1761 stNow.wMilliseconds = 500;
1762 if (!SystemTimeToFileTime(&stNow, &ftNow)) {
1767 random_time_bits = (((unsigned long long)ftNow.dwHighDateTime << 32) | (unsigned long long)ftNow.dwLowDateTime);
1770 value += random_time_bits ^ (unsigned long long)GetCurrentThreadId();
1772 for (count = 0; count < attempts; value += 7777, ++count) {
1773 unsigned long long v = value;
1775 /* Fill in the random bits. */
1776 XXXXXX[0] = letters[v % 62];
1778 XXXXXX[1] = letters[v % 62];
1780 XXXXXX[2] = letters[v % 62];
1782 XXXXXX[3] = letters[v % 62];
1784 XXXXXX[4] = letters[v % 62];
1786 XXXXXX[5] = letters[v % 62];
1788 fd = open(tmpl, O_RDWR | O_CREAT | O_EXCL, _S_IREAD | _S_IWRITE);
1792 } else if (errno != EEXIST)
1796 /* We got out of the loop because we ran out of combinations to try. */
1801 String Utility::GetIcingaInstallPath()
1805 for (int i = 0; MsiEnumProducts(i, szProduct) == ERROR_SUCCESS; i++) {
1807 DWORD cbName = sizeof(szName);
1808 if (MsiGetProductInfo(szProduct, INSTALLPROPERTY_INSTALLEDPRODUCTNAME, szName, &cbName) != ERROR_SUCCESS)
1811 if (strcmp(szName, "Icinga 2") != 0)
1814 char szLocation[1024];
1815 DWORD cbLocation = sizeof(szLocation);
1816 if (MsiGetProductInfo(szProduct, INSTALLPROPERTY_INSTALLLOCATION, szLocation, &cbLocation) == ERROR_SUCCESS)
1823 String Utility::GetIcingaDataPath()
1825 char path[MAX_PATH];
1826 if (!SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_COMMON_APPDATA, nullptr, 0, path)))
1828 return String(path) + "\\icinga2";
1834 * Retrieve the environment variable value by given key.
1836 * @param env Environment variable name.
1839 String Utility::GetFromEnvironment(const String& env)
1841 const char *envValue = getenv(env.CStr());
1843 if (envValue == NULL)
1846 return String(envValue);
1850 * Compare the password entered by a client with the actual password.
1851 * The comparision is safe against timing attacks.
1853 bool Utility::ComparePasswords(const String& enteredPassword, const String& actualPassword)
1855 volatile const char * volatile enteredPasswordCStr = enteredPassword.CStr();
1856 volatile size_t enteredPasswordLen = enteredPassword.GetLength();
1858 volatile const char * volatile actualPasswordCStr = actualPassword.CStr();
1859 volatile size_t actualPasswordLen = actualPassword.GetLength();
1861 volatile uint_fast8_t result = enteredPasswordLen == actualPasswordLen;
1864 auto cStr (actualPasswordCStr);
1865 auto len (actualPasswordLen);
1867 actualPasswordCStr = cStr;
1868 actualPasswordLen = len;
1870 auto cStr (enteredPasswordCStr);
1871 auto len (enteredPasswordLen);
1873 actualPasswordCStr = cStr;
1874 actualPasswordLen = len;
1877 for (volatile size_t i = 0; i < enteredPasswordLen; ++i) {
1878 result &= uint_fast8_t(enteredPasswordCStr[i] == actualPasswordCStr[i]);