CLOCK_BOOTTIME has been added in Linux >= 2.6.39,
so fallback to CLOCK_MONOTONIC on older kernels.

Also, ensure proper compilation with older glibc versions.

https://www.man7.org/linux/man-pages/man3/clock_settime.3.html

Signed-off-by: Thomas Devoogdt <thomas.devoo...@barco.com>
---
 miscutils/seedrng.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/miscutils/seedrng.c b/miscutils/seedrng.c
index 3bf6e2ea7..07ecc8a82 100644
--- a/miscutils/seedrng.c
+++ b/miscutils/seedrng.c
@@ -69,6 +69,21 @@ static ssize_t getrandom(void *buffer, size_t length, 
unsigned flags)
 #define GRND_INSECURE 0x0004
 #endif
 
+/* fix for glibc < 2.3.4 */
+#ifndef CLOCK_REALTIME
+#define CLOCK_REALTIME 0
+#endif
+
+/* fix for glibc < 2.3.4 */
+#ifndef CLOCK_MONOTONIC
+#define CLOCK_MONOTONIC 1
+#endif
+
+/* fix for glibc < 2.14 */
+#ifndef CLOCK_BOOTTIME
+#define CLOCK_BOOTTIME 7
+#endif
+
 #define DEFAULT_SEED_DIR         "/var/lib/seedrng"
 #define CREDITABLE_SEED_NAME     "seed.credit"
 #define NON_CREDITABLE_SEED_NAME "seed.no-credit"
@@ -228,7 +243,9 @@ int seedrng_main(int argc UNUSED_PARAM, char **argv)
 //Hashing in a constant string doesn't add any entropy
 //     sha256_hash(&hash, "SeedRNG v1 Old+New Prefix", 25);
        clock_gettime(CLOCK_REALTIME, &timestamp[0]);
-       clock_gettime(CLOCK_BOOTTIME, &timestamp[1]);
+       if (clock_gettime(CLOCK_BOOTTIME, &timestamp[1]) < 0 && errno == EINVAL)
+               clock_gettime(CLOCK_MONOTONIC, &timestamp[1]); /* fallback for 
Linux < 2.6.39 */
+
        sha256_hash(&hash, timestamp, sizeof(timestamp));
 
        for (i = 0; i <= 1; i++) {
-- 
2.43.0

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to