Commit 96c6117414942e6d3db56e73f2354a5fd15d4962

Edward Thomson 2022-06-13T11:19:55

cmake: only use `getloadavg` where it exists

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 58e5825..d16cfe5 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -71,6 +71,7 @@ check_function_exists(qsort_s GIT_QSORT_S)
 # random / entropy data
 
 check_function_exists(getentropy GIT_RAND_GETENTROPY)
+check_function_exists(getloadavg GIT_RAND_GETLOADAVG)
 
 # determine architecture of the machine
 
diff --git a/src/features.h.in b/src/features.h.in
index e14e833..fbf0cab 100644
--- a/src/features.h.in
+++ b/src/features.h.in
@@ -57,5 +57,6 @@
 #cmakedefine GIT_SHA256_MBEDTLS 1
 
 #cmakedefine GIT_RAND_GETENTROPY 1
+#cmakedefine GIT_RAND_GETLOADAVG 1
 
 #endif
diff --git a/src/util/rand.c b/src/util/rand.c
index 4324949..d28e4aa 100644
--- a/src/util/rand.c
+++ b/src/util/rand.c
@@ -106,8 +106,6 @@ GIT_INLINE(int) getseed(uint64_t *seed)
 		return -1;
 	}
 
-	getloadavg(loadavg, 3);
-
 	*seed = 0;
 	*seed |= ((uint64_t)tv.tv_usec << 40);
 	*seed |= ((uint64_t)tv.tv_sec);
@@ -119,9 +117,15 @@ GIT_INLINE(int) getseed(uint64_t *seed)
 	*seed ^= ((uint64_t)getuid() << 8);
 	*seed ^= ((uint64_t)getgid());
 
+# if defined(GIT_RAND_GETLOADAVG)
+	getloadavg(loadavg, 3);
+
 	convert.f = loadavg[0]; *seed ^= (convert.d >> 36);
 	convert.f = loadavg[1]; *seed ^= (convert.d);
 	convert.f = loadavg[2]; *seed ^= (convert.d >> 16);
+# else
+	GIT_UNUSED(loadavg[0]);
+# endif
 
 	convert.f = git__timer(); *seed ^= (convert.d);