diff options
-rw-r--r-- | bbot/agent | 9 | ||||
-rw-r--r-- | bbot/agent.cxx | 77 | ||||
-rw-r--r-- | bbot/utility | 9 | ||||
-rw-r--r-- | bbot/utility.cxx | 15 |
4 files changed, 56 insertions, 54 deletions
@@ -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 <typename T> + inline T + genrand () {return static_cast<T> (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 <pwd.h> // getpwuid() #include <limits.h> // PATH_MAX #include <signal.h> // signal() +#include <stdlib.h> // rand_r() #include <unistd.h> // sleep(), realink(), getuid() #include <net/if.h> // ifreq @@ -15,6 +16,7 @@ #include <sys/ioctl.h> #include <sys/socket.h> +#include <chrono> #include <iostream> #include <butl/pager> @@ -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<sockaddr_in*> (&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<unsigned int> ( + chrono::system_clock::now ().time_since_epoch ().count ()); + + return static_cast<size_t> (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<sockaddr_in*> (&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 <typename T> - inline T - genrand () {return static_cast<T> (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 <bbot/utility> -#include <chrono> -#include <cstdlib> // rand_r() - #include <bbot/diagnostics> 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<unsigned int> ( - chrono::system_clock::now ().time_since_epoch ().count ()); - - return static_cast<size_t> (rand_r (&rand_seed)); - } - void run_trace (tracer& t, const char* cmd[], size_t n) { |