2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
2012-05-11 13:33:57 +02:00
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
2012-05-10 12:06:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2012-04-22 16:45:31 +02:00
|
|
|
#include "i2-base.h"
|
2012-05-09 10:15:51 +02:00
|
|
|
#include <mmatch.h>
|
2013-02-14 10:55:15 +01:00
|
|
|
#if HAVE_BACKTRACE_SYMBOLS
|
2013-01-30 11:51:15 +01:00
|
|
|
# include <execinfo.h>
|
|
|
|
#endif /* HAVE_BACKTRACE_SYMBOLS */
|
2012-04-22 16:45:31 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
bool I2_EXPORT Utility::m_SSLInitialized = false;
|
|
|
|
|
2012-07-11 20:55:46 +02:00
|
|
|
/**
|
2012-09-27 09:38:28 +02:00
|
|
|
* Demangles a symbol name.
|
2012-07-11 20:55:46 +02:00
|
|
|
*
|
2012-09-27 09:38:28 +02:00
|
|
|
* @param sym The symbol name.
|
|
|
|
* @returns A human-readable version of the symbol name.
|
2012-07-11 20:55:46 +02:00
|
|
|
*/
|
2012-09-27 09:38:28 +02:00
|
|
|
String Utility::DemangleSymbolName(const String& sym)
|
2012-07-11 20:55:46 +02:00
|
|
|
{
|
2012-09-27 09:38:28 +02:00
|
|
|
String result = sym;
|
2012-07-11 20:55:46 +02:00
|
|
|
|
2013-02-14 10:55:15 +01:00
|
|
|
#if HAVE_GCC_ABI_DEMANGLE
|
2012-07-11 20:55:46 +02:00
|
|
|
int status;
|
2012-09-27 09:38:28 +02:00
|
|
|
char *realname = abi::__cxa_demangle(sym.CStr(), 0, 0, &status);
|
2012-07-11 20:55:46 +02:00
|
|
|
|
|
|
|
if (realname != NULL) {
|
2012-09-27 09:38:28 +02:00
|
|
|
result = String(realname);
|
2012-07-11 20:55:46 +02:00
|
|
|
free(realname);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_GCC_ABI_DEMANGLE */
|
|
|
|
|
2012-09-27 09:38:28 +02:00
|
|
|
return result;
|
2012-07-11 20:55:46 +02:00
|
|
|
}
|
|
|
|
|
2012-09-27 09:38:28 +02:00
|
|
|
/**
|
|
|
|
* Returns a human-readable type name of a type_info object.
|
|
|
|
*
|
|
|
|
* @param ti A type_info object.
|
|
|
|
* @returns The type name of the object.
|
|
|
|
*/
|
|
|
|
String Utility::GetTypeName(const type_info& ti)
|
|
|
|
{
|
|
|
|
return DemangleSymbolName(ti.name());
|
|
|
|
}
|
2012-07-11 20:55:46 +02:00
|
|
|
|
2013-01-30 11:51:15 +01:00
|
|
|
/**
|
|
|
|
* Prints a stacktrace to the specified stream.
|
|
|
|
*
|
|
|
|
* @param fp The stream.
|
|
|
|
* @param ignoreFrames The number of stackframes to ignore (in addition to
|
|
|
|
* the one this function is executing in).
|
|
|
|
* @returns true if the stacktrace was printed, false otherwise.
|
|
|
|
*/
|
|
|
|
bool Utility::PrintStacktrace(ostream& fp, int ignoreFrames)
|
|
|
|
{
|
2013-02-14 10:55:15 +01:00
|
|
|
#if HAVE_BACKTRACE_SYMBOLS
|
2013-01-30 11:51:15 +01:00
|
|
|
void *frames[50];
|
|
|
|
int framecount = backtrace(frames, sizeof(frames) / sizeof(frames[0]));
|
|
|
|
|
|
|
|
char **messages = backtrace_symbols(frames, framecount);
|
|
|
|
|
|
|
|
fp << std::endl << "Stacktrace:" << std::endl;
|
|
|
|
|
|
|
|
for (int i = ignoreFrames + 1; i < framecount && messages != NULL; ++i) {
|
|
|
|
String message = messages[i];
|
|
|
|
|
|
|
|
char *sym_begin = strchr(messages[i], '(');
|
|
|
|
|
|
|
|
if (sym_begin != NULL) {
|
|
|
|
char *sym_end = strchr(sym_begin, '+');
|
|
|
|
|
|
|
|
if (sym_end != NULL) {
|
|
|
|
String sym = String(sym_begin + 1, sym_end);
|
|
|
|
String sym_demangled = Utility::DemangleSymbolName(sym);
|
|
|
|
|
|
|
|
if (sym_demangled.IsEmpty())
|
|
|
|
sym_demangled = "<unknown function>";
|
|
|
|
|
|
|
|
message = String(messages[i], sym_begin) + ": " + sym_demangled + " (" + String(sym_end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fp << "\t(" << i - ignoreFrames - 1 << ") " << message << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(messages);
|
|
|
|
|
|
|
|
fp << std::endl;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
#else /* HAVE_BACKTRACE_SYMBOLS */
|
|
|
|
return false;
|
|
|
|
#endif /* HAVE_BACKTRACE_SYMBOLS */
|
|
|
|
}
|
|
|
|
|
2012-04-22 16:45:31 +02:00
|
|
|
/**
|
|
|
|
* Detaches from the controlling terminal.
|
|
|
|
*/
|
|
|
|
void Utility::Daemonize(void) {
|
|
|
|
#ifndef _WIN32
|
|
|
|
pid_t pid;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
pid = fork();
|
2012-04-23 08:42:24 +02:00
|
|
|
if (pid < 0)
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(PosixException("fork() failed", errno));
|
2012-04-22 16:45:31 +02:00
|
|
|
|
|
|
|
if (pid)
|
2012-09-27 08:21:07 +02:00
|
|
|
_exit(0);
|
2012-04-22 16:45:31 +02:00
|
|
|
|
|
|
|
fd = open("/dev/null", O_RDWR);
|
2012-04-23 08:42:24 +02:00
|
|
|
|
|
|
|
if (fd < 0)
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(PosixException("open() failed", errno));
|
2012-04-23 08:42:24 +02:00
|
|
|
|
2012-07-10 16:24:17 +02:00
|
|
|
if (fd != STDIN_FILENO)
|
|
|
|
dup2(fd, STDIN_FILENO);
|
2012-04-23 08:42:24 +02:00
|
|
|
|
2012-07-10 16:24:17 +02:00
|
|
|
if (fd != STDOUT_FILENO)
|
|
|
|
dup2(fd, STDOUT_FILENO);
|
2012-04-23 08:42:24 +02:00
|
|
|
|
2012-07-10 16:24:17 +02:00
|
|
|
if (fd != STDERR_FILENO)
|
|
|
|
dup2(fd, STDERR_FILENO);
|
2012-04-23 08:42:24 +02:00
|
|
|
|
2012-07-10 16:24:17 +02:00
|
|
|
if (fd > STDERR_FILENO)
|
2012-04-23 08:42:24 +02:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (setsid() < 0)
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(PosixException("setsid() failed", errno));
|
2012-04-22 16:45:31 +02:00
|
|
|
#endif
|
|
|
|
}
|
2012-04-24 14:02:15 +02:00
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Initializes the OpenSSL library.
|
|
|
|
*/
|
2012-04-24 14:02:15 +02:00
|
|
|
void Utility::InitializeOpenSSL(void)
|
|
|
|
{
|
2012-05-14 19:14:23 +02:00
|
|
|
if (m_SSLInitialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SSL_library_init();
|
|
|
|
SSL_load_error_strings();
|
2012-04-24 14:02:15 +02:00
|
|
|
|
2012-05-14 19:14:23 +02:00
|
|
|
m_SSLInitialized = true;
|
2012-04-24 14:02:15 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Initializes an SSL context using the specified certificates.
|
|
|
|
*
|
|
|
|
* @param pubkey The public key.
|
|
|
|
* @param privkey The matching private key.
|
|
|
|
* @param cakey CA certificate chain file.
|
|
|
|
* @returns An SSL context.
|
|
|
|
*/
|
2013-02-02 09:19:49 +01:00
|
|
|
shared_ptr<SSL_CTX> Utility::MakeSSLContext(const String& pubkey, const String& privkey, const String& cakey)
|
2012-04-24 14:02:15 +02:00
|
|
|
{
|
|
|
|
InitializeOpenSSL();
|
|
|
|
|
2012-06-17 16:37:36 +02:00
|
|
|
shared_ptr<SSL_CTX> sslContext = shared_ptr<SSL_CTX>(SSL_CTX_new(TLSv1_method()), SSL_CTX_free);
|
2012-04-24 14:02:15 +02:00
|
|
|
|
|
|
|
SSL_CTX_set_mode(sslContext.get(), SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
if (!SSL_CTX_use_certificate_chain_file(sslContext.get(), pubkey.CStr()))
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(OpenSSLException("Could not load public X509 key file", ERR_get_error()));
|
2012-04-24 14:02:15 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
if (!SSL_CTX_use_PrivateKey_file(sslContext.get(), privkey.CStr(), SSL_FILETYPE_PEM))
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(OpenSSLException("Could not load private X509 key file", ERR_get_error()));
|
2012-04-24 14:02:15 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
if (!SSL_CTX_load_verify_locations(sslContext.get(), cakey.CStr(), NULL))
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(OpenSSLException("Could not load public CA key file", ERR_get_error()));
|
2012-04-24 14:02:15 +02:00
|
|
|
|
2012-06-21 12:51:50 +02:00
|
|
|
STACK_OF(X509_NAME) *cert_names;
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
cert_names = SSL_load_client_CA_file(cakey.CStr());
|
2012-06-21 12:51:50 +02:00
|
|
|
if (cert_names == NULL)
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(OpenSSLException("SSL_load_client_CA_file() failed", ERR_get_error()));
|
2012-06-21 12:51:50 +02:00
|
|
|
|
|
|
|
SSL_CTX_set_client_CA_list(sslContext.get(), cert_names);
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
return sslContext;
|
|
|
|
}
|
2012-04-24 15:56:48 +02:00
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
2012-05-14 19:14:23 +02:00
|
|
|
* Retrieves the common name for an X509 certificate.
|
2012-05-08 15:14:20 +02:00
|
|
|
*
|
|
|
|
* @param certificate The X509 certificate.
|
|
|
|
* @returns The common name.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
String Utility::GetCertificateCN(const shared_ptr<X509>& certificate)
|
2012-04-24 15:56:48 +02:00
|
|
|
{
|
|
|
|
char buffer[256];
|
|
|
|
|
2012-09-21 09:43:06 +02:00
|
|
|
int rc = X509_NAME_get_text_by_NID(X509_get_subject_name(certificate.get()),
|
|
|
|
NID_commonName, buffer, sizeof(buffer));
|
2012-04-24 15:56:48 +02:00
|
|
|
|
|
|
|
if (rc == -1)
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(OpenSSLException("X509 certificate has no CN"
|
2012-09-21 09:43:06 +02:00
|
|
|
" attribute", ERR_get_error()));
|
2012-04-24 15:56:48 +02:00
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
2012-04-27 11:44:34 +02:00
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Retrieves an X509 certificate from the specified file.
|
|
|
|
*
|
|
|
|
* @param pemfile The filename.
|
|
|
|
* @returns An X509 certificate.
|
|
|
|
*/
|
2013-02-02 09:19:49 +01:00
|
|
|
shared_ptr<X509> Utility::GetX509Certificate(const String& pemfile)
|
2012-04-27 11:44:34 +02:00
|
|
|
{
|
|
|
|
X509 *cert;
|
|
|
|
BIO *fpcert = BIO_new(BIO_s_file());
|
|
|
|
|
|
|
|
if (fpcert == NULL)
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(OpenSSLException("BIO_new failed",
|
2012-09-21 09:43:06 +02:00
|
|
|
ERR_get_error()));
|
2012-04-27 11:44:34 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
if (BIO_read_filename(fpcert, pemfile.CStr()) < 0)
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(OpenSSLException("BIO_read_filename failed",
|
2012-09-21 09:43:06 +02:00
|
|
|
ERR_get_error()));
|
2012-04-27 11:44:34 +02:00
|
|
|
|
|
|
|
cert = PEM_read_bio_X509_AUX(fpcert, NULL, NULL, NULL);
|
|
|
|
if (cert == NULL)
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(OpenSSLException("PEM_read_bio_X509_AUX failed",
|
2012-09-21 09:43:06 +02:00
|
|
|
ERR_get_error()));
|
2012-04-27 11:44:34 +02:00
|
|
|
|
|
|
|
BIO_free(fpcert);
|
|
|
|
|
|
|
|
return shared_ptr<X509>(cert, X509_free);
|
|
|
|
}
|
2012-05-09 10:15:51 +02:00
|
|
|
|
2012-05-14 19:14:23 +02:00
|
|
|
/**
|
|
|
|
* Performs wildcard pattern matching.
|
|
|
|
*
|
|
|
|
* @param pattern The wildcard pattern.
|
2012-08-02 09:38:08 +02:00
|
|
|
* @param text The String that should be checked.
|
2012-05-14 19:14:23 +02:00
|
|
|
* @returns true if the wildcard pattern matches, false otherwise.
|
|
|
|
*/
|
2013-02-02 09:19:49 +01:00
|
|
|
bool Utility::Match(const String& pattern, const String& text)
|
2012-05-09 10:15:51 +02:00
|
|
|
{
|
2012-08-02 09:38:08 +02:00
|
|
|
return (match(pattern.CStr(), text.CStr()) == 0);
|
2012-05-09 10:15:51 +02:00
|
|
|
}
|
2012-07-08 21:18:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the directory component of a path. See dirname(3) for details.
|
|
|
|
*
|
|
|
|
* @param path The full path.
|
|
|
|
* @returns The directory.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
String Utility::DirName(const String& path)
|
2012-07-08 21:18:35 +02:00
|
|
|
{
|
2013-02-09 10:43:11 +01:00
|
|
|
char *dir;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
String dupPath = path;
|
|
|
|
|
|
|
|
/* PathRemoveFileSpec doesn't properly handle forward slashes. */
|
|
|
|
BOOST_FOREACH(char& ch, dupPath) {
|
|
|
|
if (ch == '/')
|
|
|
|
ch = '\\';
|
|
|
|
}
|
|
|
|
|
|
|
|
dir = strdup(dupPath.CStr());
|
|
|
|
#else /* _WIN32 */
|
|
|
|
dir = strdup(path.CStr());
|
|
|
|
#endif /* _WIN32 */
|
2012-07-08 21:18:35 +02:00
|
|
|
|
|
|
|
if (dir == NULL)
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(bad_alloc());
|
2012-07-08 21:18:35 +02:00
|
|
|
|
2013-02-09 10:43:11 +01:00
|
|
|
String result;
|
|
|
|
|
2012-07-08 21:18:35 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
result = dirname(dir);
|
|
|
|
#else /* _WIN32 */
|
|
|
|
if (!PathRemoveFileSpec(dir)) {
|
|
|
|
free(dir);
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(Win32Exception("PathRemoveFileSpec() failed",
|
2012-09-21 09:43:06 +02:00
|
|
|
GetLastError()));
|
2012-07-08 21:18:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
result = dir;
|
2013-02-02 00:28:00 +01:00
|
|
|
|
|
|
|
if (result.IsEmpty())
|
|
|
|
result = ".";
|
2012-07-08 21:18:35 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
free(dir);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the file component of a path. See basename(3) for details.
|
|
|
|
*
|
|
|
|
* @param path The full path.
|
|
|
|
* @returns The filename.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
String Utility::BaseName(const String& path)
|
2012-07-08 21:18:35 +02:00
|
|
|
{
|
2012-08-02 09:38:08 +02:00
|
|
|
char *dir = strdup(path.CStr());
|
|
|
|
String result;
|
2012-07-08 21:18:35 +02:00
|
|
|
|
|
|
|
if (dir == NULL)
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(bad_alloc());
|
2012-07-08 21:18:35 +02:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
result = basename(dir);
|
|
|
|
#else /* _WIN32 */
|
|
|
|
result = PathFindFileName(dir);
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
free(dir);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2012-07-24 10:50:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Null deleter. Used as a parameter for the shared_ptr constructor.
|
|
|
|
*
|
2012-09-17 13:35:55 +02:00
|
|
|
* @param - The object that should be deleted.
|
2012-07-24 10:50:53 +02:00
|
|
|
*/
|
2012-08-07 21:02:12 +02:00
|
|
|
void Utility::NullDeleter(void *)
|
2012-07-24 10:50:53 +02:00
|
|
|
{
|
|
|
|
/* Nothing to do here. */
|
|
|
|
}
|
2012-07-25 12:59:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current UNIX timestamp including fractions of seconds.
|
|
|
|
*
|
|
|
|
* @returns The current time.
|
|
|
|
*/
|
|
|
|
double Utility::GetTime(void)
|
|
|
|
{
|
2012-07-27 16:05:02 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
FILETIME cft;
|
|
|
|
GetSystemTimeAsFileTime(&cft);
|
|
|
|
|
|
|
|
ULARGE_INTEGER ucft;
|
|
|
|
ucft.HighPart = cft.dwHighDateTime;
|
|
|
|
ucft.LowPart = cft.dwLowDateTime;
|
|
|
|
|
|
|
|
SYSTEMTIME est = { 1970, 1, 4, 1, 0, 0, 0, 0};
|
|
|
|
FILETIME eft;
|
|
|
|
SystemTimeToFileTime(&est, &eft);
|
|
|
|
|
|
|
|
ULARGE_INTEGER ueft;
|
|
|
|
ueft.HighPart = eft.dwHighDateTime;
|
|
|
|
ueft.LowPart = eft.dwLowDateTime;
|
|
|
|
|
|
|
|
return ((ucft.QuadPart - ueft.QuadPart) / 10000) / 1000.0;
|
|
|
|
#else /* _WIN32 */
|
2012-07-25 12:59:17 +02:00
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
if (gettimeofday(&tv, NULL) < 0)
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(PosixException("gettimeofday() failed", errno));
|
2012-07-25 12:59:17 +02:00
|
|
|
|
|
|
|
return tv.tv_sec + tv.tv_usec / 1000000.0;
|
2012-07-27 16:05:02 +02:00
|
|
|
#endif /* _WIN32 */
|
2012-07-25 12:59:17 +02:00
|
|
|
}
|
2012-09-19 13:00:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the ID of the current process.
|
|
|
|
*
|
|
|
|
* @returns The PID.
|
|
|
|
*/
|
|
|
|
pid_t Utility::GetPid(void)
|
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
return getpid();
|
|
|
|
#else /* _WIN32 */
|
|
|
|
return GetCurrentProcessId();
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
2012-09-25 15:41:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sleeps for the specified amount of time.
|
|
|
|
*
|
|
|
|
* @param timeout The timeout in seconds.
|
|
|
|
*/
|
|
|
|
void Utility::Sleep(double timeout)
|
|
|
|
{
|
2013-02-14 10:55:47 +01:00
|
|
|
if (Application::IsMainThread())
|
|
|
|
Application::GetMutex().unlock();
|
|
|
|
|
2012-09-25 15:41:43 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
usleep(timeout * 1000 * 1000);
|
|
|
|
#else /* _WIN32 */
|
2012-09-27 10:05:54 +02:00
|
|
|
::Sleep(timeout * 1000);
|
2012-09-25 15:41:43 +02:00
|
|
|
#endif /* _WIN32 */
|
2013-02-14 10:55:47 +01:00
|
|
|
|
|
|
|
if (Application::IsMainThread())
|
|
|
|
Application::GetMutex().lock();
|
2012-09-25 15:41:43 +02:00
|
|
|
}
|
|
|
|
|
2013-01-17 15:05:34 +01:00
|
|
|
/**
|
|
|
|
* Loads the specified library and invokes an Icinga-specific init
|
|
|
|
* function if available.
|
|
|
|
*
|
|
|
|
* @param library The name of the library.
|
|
|
|
* @param module Whether the library is a module (non-module libraries have a
|
|
|
|
* "lib" prefix on *NIX).
|
|
|
|
*/
|
|
|
|
#ifdef _WIN32
|
|
|
|
HMODULE
|
|
|
|
#else /* _WIN32 */
|
|
|
|
lt_dlhandle
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
Utility::LoadIcingaLibrary(const String& library, bool module)
|
|
|
|
{
|
|
|
|
String path;
|
|
|
|
#ifdef _WIN32
|
|
|
|
path = library + ".dll";
|
|
|
|
#else /* _WIN32 */
|
|
|
|
path = (module ? "" : "lib") + library + ".la";
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
Logger::Write(LogInformation, "base", "Loading library '" + path + "'");
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
HMODULE hModule = LoadLibrary(path.CStr());
|
|
|
|
|
|
|
|
if (hModule == NULL)
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(Win32Exception("LoadLibrary('" + path + "') failed", GetLastError()));
|
2013-01-17 15:05:34 +01:00
|
|
|
#else /* _WIN32 */
|
|
|
|
lt_dlhandle hModule = lt_dlopen(path.CStr());
|
|
|
|
|
|
|
|
if (hModule == NULL) {
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Could not load library '" + path + "': " + lt_dlerror()));
|
2013-01-17 15:05:34 +01:00
|
|
|
}
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
return hModule;
|
|
|
|
}
|
2013-01-30 09:08:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a new UUID.
|
|
|
|
*
|
|
|
|
* @returns The new UUID in text form.
|
|
|
|
*/
|
|
|
|
String Utility::NewUUID(void)
|
|
|
|
{
|
|
|
|
boost::uuids::uuid uuid = boost::uuids::random_generator()();
|
2013-01-31 10:10:51 +01:00
|
|
|
return boost::lexical_cast<String>(uuid);
|
2013-01-30 09:08:48 +01:00
|
|
|
}
|
|
|
|
|
2013-02-02 00:28:00 +01:00
|
|
|
/**
|
|
|
|
* Calls the specified callback for each file matching the path specification.
|
|
|
|
*
|
|
|
|
* @param pathSpec The path specification.
|
|
|
|
*/
|
|
|
|
bool Utility::Glob(const String& pathSpec, const function<void (const String&)>& callback)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
HANDLE handle;
|
|
|
|
WIN32_FIND_DATA wfd;
|
|
|
|
|
|
|
|
handle = FindFirstFile(pathSpec.CStr(), &wfd);
|
|
|
|
|
|
|
|
if (handle == INVALID_HANDLE_VALUE) {
|
|
|
|
DWORD errorCode = GetLastError();
|
|
|
|
|
|
|
|
if (errorCode == ERROR_FILE_NOT_FOUND)
|
|
|
|
return false;
|
|
|
|
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(Win32Exception("FindFirstFile() failed", errorCode));
|
2013-02-02 00:28:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2013-02-09 10:43:11 +01:00
|
|
|
callback(DirName(pathSpec) + "/" + wfd.cFileName);
|
2013-02-02 00:28:00 +01:00
|
|
|
} while (FindNextFile(handle, &wfd));
|
|
|
|
|
|
|
|
if (!FindClose(handle))
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(Win32Exception("FindClose() failed", GetLastError()));
|
2013-02-02 00:28:00 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
#else /* _WIN32 */
|
|
|
|
glob_t gr;
|
|
|
|
|
|
|
|
int rc = glob(pathSpec.CStr(), GLOB_ERR | GLOB_NOSORT, NULL, &gr);
|
|
|
|
|
|
|
|
if (rc < 0) {
|
|
|
|
if (rc == GLOB_NOMATCH)
|
|
|
|
return false;
|
|
|
|
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(PosixException("glob() failed", errno));
|
2013-02-02 00:28:00 +01:00
|
|
|
}
|
|
|
|
|
2013-02-03 01:30:19 +01:00
|
|
|
if (gr.gl_pathc == 0) {
|
|
|
|
globfree(&gr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-02 00:28:00 +01:00
|
|
|
size_t left;
|
|
|
|
char **gp;
|
|
|
|
for (gp = gr.gl_pathv, left = gr.gl_pathc; left > 0; gp++, left--) {
|
|
|
|
callback(*gp);
|
|
|
|
}
|
|
|
|
|
|
|
|
globfree(&gr);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
2013-02-06 10:53:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Waits until the given predicate is true. Executes events while waiting.
|
|
|
|
*
|
|
|
|
* @param predicate The predicate.
|
|
|
|
*/
|
|
|
|
void Utility::WaitUntil(const function<bool (void)>& predicate)
|
|
|
|
{
|
2013-02-13 07:25:36 +01:00
|
|
|
while (!predicate())
|
|
|
|
Application::ProcessEvents();
|
2013-02-06 10:53:28 +01:00
|
|
|
}
|
|
|
|
|
2013-02-14 10:59:01 +01:00
|
|
|
#ifndef _WIN32
|
2013-02-13 13:03:21 +01:00
|
|
|
void Utility::SetNonBlocking(int fd)
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
if (flags < 0)
|
|
|
|
BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno));
|
|
|
|
|
|
|
|
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)
|
|
|
|
BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Utility::SetCloExec(int fd)
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
flags = fcntl(fd, F_GETFD, 0);
|
|
|
|
if (flags < 0)
|
|
|
|
BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno));
|
|
|
|
|
|
|
|
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
|
|
|
|
BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno));
|
|
|
|
}
|
2013-02-14 10:59:01 +01:00
|
|
|
#endif /* _WIN32 */
|
2013-02-13 13:03:21 +01:00
|
|
|
|
|
|
|
void Utility::SetNonBlockingSocket(SOCKET s)
|
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
SetNonBlocking(s);
|
|
|
|
#else /* _WIN32 */
|
|
|
|
unsigned long lTrue = 1;
|
|
|
|
ioctlsocket(s, FIONBIO, &lTrue);
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|