Import CStringView from the Babeltrace tree
[lttng-tools.git] / src / common / random.cpp
index 8d9bcfd41e0beb09147918c569d03b83fcfbf32e..ba9d937ba32b95f43f877348ac0f358ea9c7e071 100644 (file)
@@ -5,6 +5,7 @@
  *
  */
 
+#include <common/error.hpp>
 #include <common/file-descriptor.hpp>
 #include <common/format.hpp>
 #include <common/hashtable/utils.hpp>
@@ -66,13 +67,25 @@ 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),
+                       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 <stdlib.h>
+
+/*
+ * According to the MacOS / FreeBSD manpage, this function never fails nor blocks.
+ */
 void getrandom_nonblock(char *out_data, std::size_t size)
+{
+       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");
 }
@@ -136,11 +149,11 @@ lttng::random::seed_t produce_random_seed_from_urandom()
        }() };
 
        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;
@@ -168,9 +181,9 @@ 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(
+                    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());
@@ -182,10 +195,10 @@ 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())
+                    lttng::format("Failed to produce a random seed from the urandom device: {}",
+                                  e.what())
                             .c_str());
        }
 
This page took 0.036969 seconds and 4 git commands to generate.