]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme: fix fatal error for sys/random.h
authorSteven Seungcheol Lee <sc108.lee@samsung.com>
Mon, 22 Nov 2021 08:34:58 +0000 (17:34 +0900)
committerDaniel Wagner <dwagner@suse.de>
Fri, 10 Dec 2021 13:55:47 +0000 (14:55 +0100)
fatal error: sys/random.h: No such file or directory
sys/random.h supported from glibc version 2.25
so add header file existence check
if there is no sys/random.h, try to read /dev/urandom or /dev/random

Signed-off-by: Steven Seungcheol Lee <sc108.lee@samsung.com>
[dwagner: cache errno value, close will overwrite it]
Signed-off-by: Daniel Wagner <dwagner@suse.de>
ccan/tools/configurator/configurator.c
meson.build
nvme.c

index 9487e694ce2329a5e74300c1ca596661943ced27..0518c9b5e5d40d0088e59f9d96bad727bac2c63d 100644 (file)
@@ -498,6 +498,9 @@ static const struct test base_tests[] = {
          "     return __builtin_cpu_supports(\"mmx\");\n"
          "}"
        },
+       { "HAVE_SYS_RANDOM", "<sys/random.h>",
+         "OUTSIDE_MAIN", NULL, NULL,
+         "#include <sys/random.h>\n" },
 };
 
 static void c12r_err(int eval, const char *fmt, ...)
index 8efc85251520198a05b7efe34d54d2d56170b2fa..c81efe017f0ac51fe0246245b053f3a265932a7a 100644 (file)
@@ -123,6 +123,14 @@ conf.set10(
     ),
     description: 'Is isblank() available?'
 )
+conf.set10(
+    'HAVE_SYS_RANDOM',
+    cc.compiles(
+        '''#include <sys/random.h>''',
+        name: 'sys/random.h'
+    ),
+    description: 'Is sys/random.h(getrandom) include-able?'
+)
 
 configure_file(
     output: 'config.h',
diff --git a/nvme.c b/nvme.c
index 6ebfa928636c4dd43cc74d2af2dd7b93f8d1fa6e..09cb2fd80f1a1e8bb37a006f8f7feaa95affa70f 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -23,7 +23,7 @@
 /**
  * This program uses NVMe IOCTLs to run native nvme commands to a device.
  */
-
+#include "config.h"
 #include "nvme/tree.h"
 #include "nvme/types.h"
 #include <errno.h>
 #include <sys/mman.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/random.h>
+
+#if HAVE_SYS_RANDOM
+       #include <sys/random.h>
+#endif
 
 #include "common.h"
 #include "nvme.h"
@@ -159,6 +162,26 @@ void *nvme_alloc(size_t len, bool *huge)
 }
 #endif
 
+static ssize_t getrandom_bytes(void *buf, size_t buflen)
+{
+#if !HAVE_SYS_RANDOM
+       return getrandom(buf, buflen, GRND_NONBLOCK);
+#else
+       ssize_t result;
+       int fd, err = 0;
+
+       fd = open("/dev/urandom", O_RDONLY);
+       if (fd < 0)
+               return fd;
+       result = read(fd, buf, buflen);
+       if (result < 0)
+               err = errno;
+       close(fd);
+       errno = err;
+       return result;
+#endif
+}
+
 static bool is_chardev(void)
 {
        return S_ISCHR(nvme_stat.st_mode);
@@ -6757,8 +6780,8 @@ static int gen_dhchap_key(int argc, char **argv, struct command *command, struct
        if (!raw_secret)
                return -ENOMEM;
        if (!cfg.secret) {
-               if (getrandom(raw_secret, cfg.key_len, GRND_NONBLOCK) < 0)
-                       return errno;
+               if (getrandom_bytes(raw_secret, cfg.key_len) < 0)
+                       return -errno;
        } else {
                int secret_len = 0, i;
                unsigned int c;