[PATCH] erofs-utils: lib: uuid: fix compilation error if __NR_getrandom doesn't exist

Gao Xiang hsiangkao at linux.alibaba.com
Mon Jul 29 21:25:24 AEST 2024


Let's just use rand() for insecure randomness without getrandom() support
on very old kernels to resolve issues as below:

uuid.c: In function ‘s_getrandom’:
uuid.c:44:32: error: ‘__NR_getrandom’ undeclared (first use in this function); did you mean ‘s_getrandom’?
   44 |   ssize_t r = (ssize_t)syscall(__NR_getrandom, out, size, flags);
      |                                ^~~~~~~~~~~~~~
      |                                s_getrandom

I'm not sure who cares since most users just use `--with-uuid` instead.

Fixes: 5de439566bc5 ("erofs-utils: Provide identical functionality without libuuid")
Signed-off-by: Gao Xiang <hsiangkao at linux.alibaba.com>
---
 lib/uuid.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/lib/uuid.c b/lib/uuid.c
index ec0f9d9..e6a37fb 100644
--- a/lib/uuid.c
+++ b/lib/uuid.c
@@ -38,18 +38,29 @@ static int s_getrandom(void *out, unsigned size, bool insecure)
 
 	for (;;)
 	{
+		ssize_t r;
+		int err;
+
 #ifdef HAVE_SYS_RANDOM_H
-		ssize_t r = getrandom(out, size, flags);
+		r = getrandom(out, size, flags);
+#elif defined(__NR_getrandom)
+		r = (ssize_t)syscall(__NR_getrandom, out, size, flags);
 #else
-		ssize_t r = (ssize_t)syscall(__NR_getrandom, out, size, flags);
+		r = -1;
+		errno = ENOSYS;
 #endif
-		int err;
 
 		if (r == size)
 			break;
 		err = errno;
 		if (err != EINTR) {
-			if (err == EINVAL && kflags) {
+			if (unlikely(err == ENOSYS && insecure)) {
+				while (size) {
+					*out++ = rand() % 256;
+					--size;
+				}
+				err = 0;
+			} else if (err == EINVAL && kflags) {
 				// Kernel likely does not support GRND_INSECURE
 				erofs_grnd_flag = 0;
 				kflags = 0;
-- 
2.43.5



More information about the Linux-erofs mailing list