X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Frandom.cpp;h=ba9d937ba32b95f43f877348ac0f358ea9c7e071;hb=69e98ad60c01ddfbfa6eb843960f77804173dd0f;hp=b5669d097d93f95477a9598e70d5aadfa1cf08e4;hpb=57b90af7b1977684094706818e387433f50b7d48;p=lttng-tools.git diff --git a/src/common/random.cpp b/src/common/random.cpp index b5669d097..ba9d937ba 100644 --- a/src/common/random.cpp +++ b/src/common/random.cpp @@ -5,8 +5,7 @@ * */ -#include - +#include #include #include #include @@ -14,6 +13,8 @@ #include #include +#include + #include #ifdef HAVE_SYS_SYSCALL_H @@ -66,16 +67,27 @@ void getrandom_nonblock(char *out_data, std::size_t size) if (ret < 0) { LTTNG_THROW_POSIX( - fmt::format("Failed to get true random data using getrandom(): size={}", - size), - errno); + lttng::format("Failed to get true random data using getrandom(): size={}", + size), + errno); } } -#else /* defined(__linux__) && defined(SYS_getrandom) && defined(HAVE_SYS_RANDOM_H) */ +#elif defined(HAVE_ARC4RANDOM) + +#include + +/* + * According to the MacOS / FreeBSD manpage, this function never fails nor blocks. + */ void getrandom_nonblock(char *out_data, std::size_t size) { - LTTNG_THROW_RANDOM_PRODUCTION_ERROR( - "getrandom() is not supported by this platform"); + arc4random_buf(out_data, size); +} +#else /* defined(__linux__) && defined(SYS_getrandom) && defined(HAVE_SYS_RANDOM_H) */ +__attribute__((noreturn)) void getrandom_nonblock(char *out_data __attribute__((unused)), + std::size_t size __attribute__((unused))) +{ + LTTNG_THROW_RANDOM_PRODUCTION_ERROR("getrandom() is not supported by this platform"); } #endif /* defined(__linux__) && defined(SYS_getrandom) && defined(HAVE_SYS_RANDOM_H) */ @@ -91,25 +103,23 @@ lttng::random::seed_t produce_pseudo_random_seed() ret = clock_gettime(CLOCK_REALTIME, &real_time); if (ret) { LTTNG_THROW_POSIX("Failed to read real time while generating pseudo-random seed", - errno); + errno); } ret = clock_gettime(CLOCK_MONOTONIC, &monotonic_time); if (ret) { LTTNG_THROW_POSIX( - "Failed to read monotonic time while generating pseudo-random seed", - errno); + "Failed to read monotonic time while generating pseudo-random seed", errno); } ret = gethostname(hostname, sizeof(hostname)); if (ret) { LTTNG_THROW_POSIX("Failed to get host name while generating pseudo-random seed", - errno); + errno); } hash_seed = (unsigned long) real_time.tv_nsec ^ (unsigned long) real_time.tv_sec ^ - (unsigned long) monotonic_time.tv_nsec ^ - (unsigned long) monotonic_time.tv_sec; + (unsigned long) monotonic_time.tv_nsec ^ (unsigned long) monotonic_time.tv_sec; seed = hash_key_ulong((void *) real_time.tv_sec, hash_seed); seed ^= hash_key_ulong((void *) real_time.tv_nsec, hash_seed); seed ^= hash_key_ulong((void *) monotonic_time.tv_sec, hash_seed); @@ -128,7 +138,7 @@ lttng::random::seed_t produce_random_seed_from_urandom() * Open /dev/urandom as a file_descriptor, or throw on error. The * lambda is used to reduce the scope of the raw fd as much as possible. */ - lttng::file_descriptor urandom{[]() { + lttng::file_descriptor urandom{ []() { const auto urandom_raw_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); if (urandom_raw_fd < 0) { @@ -136,14 +146,14 @@ lttng::random::seed_t produce_random_seed_from_urandom() } return urandom_raw_fd; - }()}; + }() }; lttng::random::seed_t seed; - const auto read_ret = lttng_read(urandom.fd(), &seed, sizeof(seed)); - if (read_ret != sizeof(seed)) { - LTTNG_THROW_POSIX(fmt::format("Failed to read from `/dev/urandom`: size={}", - sizeof(seed)), - errno); + try { + urandom.read(&seed, sizeof(seed)); + } catch (const std::exception& e) { + LTTNG_THROW_RANDOM_PRODUCTION_ERROR(lttng::format( + "Failed to read from `/dev/urandom`: size={}: {}", sizeof(seed), e.what())); } return seed; @@ -152,9 +162,9 @@ lttng::random::seed_t produce_random_seed_from_urandom() } /* namespace */ lttng::random::production_error::production_error(const std::string& msg, - const char *file_name, - const char *function_name, - unsigned int line_number) : + const char *file_name, + const char *function_name, + unsigned int line_number) : lttng::runtime_error(msg, file_name, function_name, line_number) { } @@ -171,11 +181,12 @@ lttng::random::seed_t lttng::random::produce_best_effort_random_seed() { try { return lttng::random::produce_true_random_seed(); - } catch (std::exception& e) { + } catch (const std::exception& e) { WARN("%s", - fmt::format("Failed to produce a random seed using getrandom(), falling back to pseudo-random device seed generation which will block until its pool is initialized: {}", - e.what()) - .c_str()); + lttng::format( + "Failed to produce a random seed using getrandom(), falling back to pseudo-random device seed generation which will block until its pool is initialized: {}", + e.what()) + .c_str()); } try { @@ -184,11 +195,11 @@ lttng::random::seed_t lttng::random::produce_best_effort_random_seed() * under some containerized environments. */ produce_random_seed_from_urandom(); - } catch (std::exception& e) { + } catch (const std::exception& e) { WARN("%s", - fmt::format("Failed to produce a random seed from the urandom device: {}", - e.what()) - .c_str()); + lttng::format("Failed to produce a random seed from the urandom device: {}", + e.what()) + .c_str()); } /* Fallback to time-based seed generation. */