From af1285bdf7a0365bae5bf23f4022abbc0d2cb526 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 21 Apr 2017 07:02:36 +0200 Subject: Make compilable on Windows --- bbot/agent | 9 +++++++ bbot/agent.cxx | 77 ++++++++++++++++++++++++++++++++++---------------------- bbot/utility | 9 ------- bbot/utility.cxx | 15 ----------- 4 files changed, 56 insertions(+), 54 deletions(-) diff --git a/bbot/agent b/bbot/agent index 1271a87..b88c892 100644 --- a/bbot/agent +++ b/bbot/agent @@ -26,6 +26,15 @@ namespace bbot extern uid_t uid; // Our effective user id. extern string uname; // Our effective user name. + // Random number generator (currently not MT-safe and limited to RAND_MAX). + // + size_t + genrand (); + + template + inline T + genrand () {return static_cast (genrand ());} + // Return the IPv4 address of an interface. // string diff --git a/bbot/agent.cxx b/bbot/agent.cxx index 4ade0b4..55d95ec 100644 --- a/bbot/agent.cxx +++ b/bbot/agent.cxx @@ -7,6 +7,7 @@ #include // getpwuid() #include // PATH_MAX #include // signal() +#include // rand_r() #include // sleep(), realink(), getuid() #include // ifreq @@ -15,6 +16,7 @@ #include #include +#include #include #include @@ -48,36 +50,6 @@ namespace bbot string hname; uid_t uid; string uname; - - // Note: Linux-specific implementation. - // - string - iface_addr (const string& i) - { - if (i.size () >= IFNAMSIZ) - throw invalid_argument ("interface nama too long"); - - auto_fd fd (socket (AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); - - if (fd.get () == -1) - throw_system_error (errno); - - ifreq ifr; - ifr.ifr_addr.sa_family = AF_INET; - strcpy (ifr.ifr_name, i.c_str ()); - - if (ioctl (fd.get (), SIOCGIFADDR, &ifr) == -1) - throw_system_error (errno); - - char buf[3 * 4 + 3 + 1]; // IPv4 address. - if (inet_ntop (AF_INET, - &reinterpret_cast (&ifr.ifr_addr)->sin_addr, - buf, - sizeof (buf)) == nullptr) - throw_system_error (errno); - - return buf; - } } // The btrfs tool likes to print informational messages, like "Created @@ -1024,3 +996,48 @@ catch (const cli::exception& e) error << e; return 1; } + +namespace bbot +{ + static unsigned int rand_seed; // Seed for rand_r(); + + size_t + genrand () + { + if (rand_seed == 0) + rand_seed = static_cast ( + chrono::system_clock::now ().time_since_epoch ().count ()); + + return static_cast (rand_r (&rand_seed)); + } + + // Note: Linux-specific implementation. + // + string + iface_addr (const string& i) + { + if (i.size () >= IFNAMSIZ) + throw invalid_argument ("interface nama too long"); + + auto_fd fd (socket (AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); + + if (fd.get () == -1) + throw_system_error (errno); + + ifreq ifr; + ifr.ifr_addr.sa_family = AF_INET; + strcpy (ifr.ifr_name, i.c_str ()); + + if (ioctl (fd.get (), SIOCGIFADDR, &ifr) == -1) + throw_system_error (errno); + + char buf[3 * 4 + 3 + 1]; // IPv4 address. + if (inet_ntop (AF_INET, + &reinterpret_cast (&ifr.ifr_addr)->sin_addr, + buf, + sizeof (buf)) == nullptr) + throw_system_error (errno); + + return buf; + } +} diff --git a/bbot/utility b/bbot/utility index f154a9e..7fe6bfb 100644 --- a/bbot/utility +++ b/bbot/utility @@ -52,15 +52,6 @@ namespace bbot using butl::auto_rmdir; using butl::auto_rmfile; - // Random number generator (currently not MT-safe and limited to RAND_MAX). - // - size_t - genrand (); - - template - inline T - genrand () {return static_cast (genrand ());} - // Process execution. // class tracer; diff --git a/bbot/utility.cxx b/bbot/utility.cxx index 83249ad..6fcc463 100644 --- a/bbot/utility.cxx +++ b/bbot/utility.cxx @@ -4,9 +4,6 @@ #include -#include -#include // rand_r() - #include using namespace std; @@ -14,18 +11,6 @@ using namespace butl; namespace bbot { - static unsigned int rand_seed; // Seed for rand_r(); - - size_t - genrand () - { - if (rand_seed == 0) - rand_seed = static_cast ( - chrono::system_clock::now ().time_since_epoch ().count ()); - - return static_cast (rand_r (&rand_seed)); - } - void run_trace (tracer& t, const char* cmd[], size_t n) { -- cgit v1.1