Commit | Line | Data |
---|---|---|
57b90af7 JG |
1 | /* |
2 | * Copyright (C) 2023 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: LGPL-2.1-only | |
5 | * | |
6 | */ | |
7 | ||
8 | #ifndef LTTNG_RANDOM_H | |
9 | #define LTTNG_RANDOM_H | |
10 | ||
11 | #include "exception.hpp" | |
12 | ||
13 | #include <cstddef> | |
14 | #include <string> | |
15 | ||
16 | namespace lttng { | |
17 | namespace random { | |
18 | ||
19 | using seed_t = unsigned int; | |
20 | ||
21 | class production_error : public ::lttng::runtime_error { | |
22 | public: | |
23 | explicit production_error(const std::string& msg, | |
9f4d1ef3 | 24 | const lttng::source_location& source_location); |
57b90af7 JG |
25 | }; |
26 | ||
27 | /* | |
28 | * Get a seed from a reliable source of randomness without blocking, raising | |
29 | * an exception on failure. | |
30 | */ | |
31 | seed_t produce_true_random_seed(); | |
32 | ||
33 | /* | |
34 | * Get a random seed making a best-effort to use a true randomness source, | |
35 | * but falling back to a pseudo-random seed based on the time and various system | |
36 | * configuration values on failure. | |
37 | * | |
38 | * Note that this function attempts to use the urandom device, which will block | |
39 | * in the unlikely event that its pool is uninitialized, on platforms that don't | |
40 | * provide getrandom(). | |
41 | */ | |
42 | seed_t produce_best_effort_random_seed(); | |
43 | ||
44 | } /* namespace random */ | |
45 | } /* namespace lttng */ | |
46 | ||
47 | #endif /* LTTNG_RANDOM_H */ |