2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2012-05-10 06:06:41 -04:00
|
|
|
|
2012-04-22 10:45:31 -04:00
|
|
|
#ifndef UTILITY_H
|
|
|
|
|
#define UTILITY_H
|
|
|
|
|
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/i2-base.hpp"
|
2014-10-19 08:48:19 -04:00
|
|
|
#include "base/string.hpp"
|
2014-11-02 01:22:00 -05:00
|
|
|
#include "base/array.hpp"
|
2015-03-28 19:03:47 -04:00
|
|
|
#include "base/threadpool.hpp"
|
2021-06-17 02:49:54 -04:00
|
|
|
#include "base/tlsutility.hpp"
|
2013-03-25 13:36:15 -04:00
|
|
|
#include <boost/thread/tss.hpp>
|
2021-06-17 02:49:54 -04:00
|
|
|
#include <openssl/sha.h>
|
2021-01-12 11:32:28 -05:00
|
|
|
#include <functional>
|
2015-03-28 19:03:47 -04:00
|
|
|
#include <typeinfo>
|
2014-04-28 03:16:27 -04:00
|
|
|
#include <vector>
|
2013-03-15 13:21:29 -04:00
|
|
|
|
2012-04-22 10:45:31 -04:00
|
|
|
namespace icinga
|
|
|
|
|
{
|
|
|
|
|
|
2013-08-30 04:19:32 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
#define MS_VC_EXCEPTION 0x406D1388
|
|
|
|
|
|
|
|
|
|
# pragma pack(push, 8)
|
|
|
|
|
struct THREADNAME_INFO
|
|
|
|
|
{
|
|
|
|
|
DWORD dwType;
|
|
|
|
|
LPCSTR szName;
|
|
|
|
|
DWORD dwThreadID;
|
|
|
|
|
DWORD dwFlags;
|
|
|
|
|
};
|
|
|
|
|
# pragma pack(pop)
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-11-22 03:03:52 -05:00
|
|
|
enum GlobType
|
|
|
|
|
{
|
|
|
|
|
GlobFile = 1,
|
|
|
|
|
GlobDirectory = 2
|
|
|
|
|
};
|
|
|
|
|
|
2012-04-22 10:45:31 -04:00
|
|
|
/**
|
2012-05-18 17:25:06 -04:00
|
|
|
* Helper functions.
|
2012-05-18 16:21:28 -04:00
|
|
|
*
|
|
|
|
|
* @ingroup base
|
2012-04-22 10:45:31 -04:00
|
|
|
*/
|
2017-12-31 01:22:16 -05:00
|
|
|
class Utility
|
2012-04-22 10:45:31 -04:00
|
|
|
{
|
|
|
|
|
public:
|
2012-09-27 03:38:28 -04:00
|
|
|
static String DemangleSymbolName(const String& sym);
|
2013-03-16 16:18:53 -04:00
|
|
|
static String GetTypeName(const std::type_info& ti);
|
2014-03-23 14:39:25 -04:00
|
|
|
static String GetSymbolName(const void *addr);
|
2012-04-22 10:45:31 -04:00
|
|
|
|
2013-02-02 03:19:49 -05:00
|
|
|
static bool Match(const String& pattern, const String& text);
|
2015-10-14 04:11:49 -04:00
|
|
|
static bool CidrMatch(const String& pattern, const String& ip);
|
2012-05-21 17:42:54 -04:00
|
|
|
|
2012-08-02 03:38:08 -04:00
|
|
|
static String DirName(const String& path);
|
|
|
|
|
static String BaseName(const String& path);
|
2012-07-08 15:18:35 -04:00
|
|
|
|
2012-09-17 07:35:55 -04:00
|
|
|
static void NullDeleter(void *);
|
2012-07-24 04:50:53 -04:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
static double GetTime();
|
2012-07-25 06:59:17 -04:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
static pid_t GetPid();
|
2012-09-19 07:00:48 -04:00
|
|
|
|
2012-09-25 09:41:43 -04:00
|
|
|
static void Sleep(double timeout);
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
static String NewUniqueID();
|
2013-01-30 03:08:48 -05:00
|
|
|
|
2017-11-21 05:52:55 -05:00
|
|
|
static bool Glob(const String& pathSpec, const std::function<void (const String&)>& callback, int type = GlobFile | GlobDirectory);
|
|
|
|
|
static bool GlobRecursive(const String& path, const String& pattern, const std::function<void (const String&)>& callback, int type = GlobFile | GlobDirectory);
|
2015-10-13 03:15:06 -04:00
|
|
|
static void MkDir(const String& path, int mode);
|
|
|
|
|
static void MkDirP(const String& path, int mode);
|
2014-10-30 14:52:22 -04:00
|
|
|
static bool SetFileOwnership(const String& file, const String& user, const String& group);
|
2013-02-01 18:28:00 -05:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
static void QueueAsyncCallback(const std::function<void ()>& callback, SchedulerPolicy policy = DefaultScheduler);
|
2013-02-18 08:40:24 -05:00
|
|
|
|
2014-04-28 03:16:27 -04:00
|
|
|
static String NaturalJoin(const std::vector<String>& tokens);
|
2016-08-16 16:16:37 -04:00
|
|
|
static String Join(const Array::Ptr& tokens, char separator, bool escapeSeparator = true);
|
2014-04-28 03:16:27 -04:00
|
|
|
|
2014-09-02 07:02:22 -04:00
|
|
|
static String FormatDuration(double duration);
|
2013-02-28 04:27:33 -05:00
|
|
|
static String FormatDateTime(const char *format, double ts);
|
2014-06-05 08:08:01 -04:00
|
|
|
static String FormatErrorNumber(int code);
|
2013-02-28 04:27:33 -05:00
|
|
|
|
2013-02-13 07:03:21 -05:00
|
|
|
#ifndef _WIN32
|
2015-11-09 14:39:26 -05:00
|
|
|
static void SetNonBlocking(int fd, bool nb = true);
|
|
|
|
|
static void SetCloExec(int fd, bool cloexec = true);
|
2021-01-12 11:32:28 -05:00
|
|
|
|
|
|
|
|
static void CloseAllFDs(const std::vector<int>& except, std::function<void(int)> onClose = nullptr);
|
2013-02-13 07:03:21 -05:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
2015-11-09 14:39:26 -05:00
|
|
|
static void SetNonBlockingSocket(SOCKET s, bool nb = true);
|
2013-02-13 07:03:21 -05:00
|
|
|
|
2013-03-22 05:58:47 -04:00
|
|
|
static String EscapeShellCmd(const String& s);
|
2014-04-18 06:14:21 -04:00
|
|
|
static String EscapeShellArg(const String& s);
|
2015-09-30 05:05:20 -04:00
|
|
|
#ifdef _WIN32
|
2015-09-30 04:54:34 -04:00
|
|
|
static String EscapeCreateProcessArg(const String& arg);
|
2015-09-30 05:05:20 -04:00
|
|
|
#endif /* _WIN32 */
|
2013-03-22 05:58:47 -04:00
|
|
|
|
2015-06-26 09:37:47 -04:00
|
|
|
static String EscapeString(const String& s, const String& chars, const bool illegal);
|
2014-12-10 07:20:16 -05:00
|
|
|
static String UnescapeString(const String& s);
|
|
|
|
|
|
2013-09-03 09:44:31 -04:00
|
|
|
static void SetThreadName(const String& name, bool os = true);
|
2018-01-03 22:25:35 -05:00
|
|
|
static String GetThreadName();
|
2013-03-25 13:36:15 -04:00
|
|
|
|
2013-12-13 06:54:14 -05:00
|
|
|
static unsigned long SDBM(const String& str, size_t len = String::NPos);
|
2013-09-12 04:03:48 -04:00
|
|
|
|
2019-08-14 05:22:55 -04:00
|
|
|
static String ParseVersion(const String& v);
|
2013-09-27 09:30:17 -04:00
|
|
|
static int CompareVersion(const String& v1, const String& v2);
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
static int Random();
|
2013-10-03 16:10:46 -04:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
static String GetHostName();
|
|
|
|
|
static String GetFQDN();
|
2014-02-06 09:27:50 -05:00
|
|
|
|
2013-11-13 08:56:31 -05:00
|
|
|
static tm LocalTime(time_t ts);
|
|
|
|
|
|
2014-05-13 08:40:12 -04:00
|
|
|
static bool PathExists(const String& path);
|
2021-01-14 12:39:14 -05:00
|
|
|
static time_t GetFileCreationTime(const String& path);
|
2014-05-13 08:40:12 -04:00
|
|
|
|
2019-04-10 08:16:39 -04:00
|
|
|
static void Remove(const String& path);
|
2015-07-21 10:10:13 -04:00
|
|
|
static void RemoveDirRecursive(const String& path);
|
2014-10-29 08:53:59 -04:00
|
|
|
static void CopyFile(const String& source, const String& target);
|
2019-04-10 07:44:13 -04:00
|
|
|
static void RenameFile(const String& source, const String& target);
|
2014-10-22 13:25:29 -04:00
|
|
|
|
2014-10-27 10:12:19 -04:00
|
|
|
static Value LoadJsonFile(const String& path);
|
2016-02-24 05:00:44 -05:00
|
|
|
static void SaveJsonFile(const String& path, int mode, const Value& value);
|
2014-10-27 10:12:19 -04:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
static String GetPlatformKernel();
|
|
|
|
|
static String GetPlatformKernelVersion();
|
|
|
|
|
static String GetPlatformName();
|
|
|
|
|
static String GetPlatformVersion();
|
|
|
|
|
static String GetPlatformArchitecture();
|
2015-11-22 06:36:50 -05:00
|
|
|
|
2015-12-10 06:25:46 -05:00
|
|
|
static String ValidateUTF8(const String& input);
|
|
|
|
|
|
2016-02-24 05:00:44 -05:00
|
|
|
static String CreateTempFile(const String& path, int mode, std::fstream& fp);
|
2016-02-22 10:47:41 -05:00
|
|
|
|
2022-07-21 11:53:08 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
static int MksTemp(char *tmpl);
|
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
2016-03-30 12:59:23 -04:00
|
|
|
#ifdef _WIN32
|
2018-01-03 22:25:35 -05:00
|
|
|
static String GetIcingaInstallPath();
|
|
|
|
|
static String GetIcingaDataPath();
|
2016-03-30 12:59:23 -04:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
2018-05-23 07:37:29 -04:00
|
|
|
static String GetFromEnvironment(const String& env);
|
2018-01-26 10:27:16 -05:00
|
|
|
|
2019-02-22 05:37:07 -05:00
|
|
|
static bool ComparePasswords(const String& enteredPassword, const String& actualPassword);
|
|
|
|
|
|
2016-05-31 11:09:22 -04:00
|
|
|
#ifdef I2_DEBUG
|
|
|
|
|
static void SetTime(double);
|
|
|
|
|
static void IncrementTime(double);
|
|
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
|
2021-06-17 02:49:54 -04:00
|
|
|
/**
|
|
|
|
|
* TruncateUsingHash truncates a given string to an allowed maximum length while avoiding collisions in the output
|
|
|
|
|
* using a hash function (SHA1).
|
|
|
|
|
*
|
|
|
|
|
* For inputs shorter than the maximum output length, the output will be the same as the input. If the input has at
|
|
|
|
|
* least the maximum output length, it is hashed used SHA1 and the output has the format "A...B" where A is a prefix
|
|
|
|
|
* of the input and B is the hex-encoded SHA1 hash of the input. The length of A is chosen so that the result has
|
|
|
|
|
* the maximum allowed output length.
|
|
|
|
|
*
|
|
|
|
|
* @tparam maxLength Maximum length of the output string (must be at least 44)
|
|
|
|
|
* @param in String to truncate
|
|
|
|
|
* @return A truncated string derived from in of at most length maxLength
|
|
|
|
|
*/
|
|
|
|
|
template<size_t maxLength>
|
|
|
|
|
static String TruncateUsingHash(const String &in) {
|
|
|
|
|
/*
|
|
|
|
|
* Note: be careful when changing this function as it is used to derive file names that should not change
|
|
|
|
|
* between versions or would need special handling if they do (/var/lib/icinga2/api/packages/_api).
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const size_t sha1HexLength = SHA_DIGEST_LENGTH*2;
|
|
|
|
|
static_assert(maxLength >= 1 + 3 + sha1HexLength,
|
|
|
|
|
"maxLength must be at least 44 to hold one character, '...', and a hex-encoded SHA1 hash");
|
|
|
|
|
|
|
|
|
|
/* If the input is shorter than the limit, no truncation is needed */
|
|
|
|
|
if (in.GetLength() < maxLength) {
|
|
|
|
|
return in;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *trunc = "...";
|
|
|
|
|
|
|
|
|
|
return in.SubStr(0, maxLength - sha1HexLength - strlen(trunc)) + trunc + SHA1(in);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-21 17:42:54 -04:00
|
|
|
private:
|
2018-01-03 22:25:35 -05:00
|
|
|
Utility();
|
2013-03-25 13:36:15 -04:00
|
|
|
|
2016-05-31 11:09:22 -04:00
|
|
|
#ifdef I2_DEBUG
|
|
|
|
|
static double m_DebugTime;
|
|
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
|
2013-03-25 13:36:15 -04:00
|
|
|
static boost::thread_specific_ptr<String> m_ThreadName;
|
2013-12-06 15:46:50 -05:00
|
|
|
static boost::thread_specific_ptr<unsigned int> m_RandSeed;
|
2012-04-22 10:45:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* UTILITY_H */
|