build: Fix errors with glibc < 2.25
authorFabrice Fontaine <fontaine.fabrice@gmail.com>
Mon, 14 Feb 2022 20:17:39 +0000 (21:17 +0100)
committerAyush Garg <ayush.garg@samsung.com>
Mon, 15 May 2023 09:25:53 +0000 (14:55 +0530)
getrandom and sys/random.h are only available since glibc 2.25:
https://www.gnu.org/software/gnulib/manual/html_node/sys_002frandom_002eh.html
resulting in the following build failures since version 5.63 and
https://git.kernel.org/pub/scm/bluetooth/bluez.git/log/?qt=grep&q=getrandom:

plugins/autopair.c:20:24: fatal error: sys/random.h: No such file or directory
 #include <sys/random.h>
                        ^

To fix this build failure, add util_getrandom and a fallback (borrowed
from pipewire and licensed under MIT):
https://gitlab.freedesktop.org/pipewire/pipewire/-/blob/master/src/pipewire/utils.c

Fixes:
 - http://autobuild.buildroot.org/results/6b8870d12e0804d6154230a7322c49416c1dc0e2
Signed-off-by: Manika Shrivastava <manika.sh@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
configure.ac
emulator/le.c
emulator/phy.c
peripheral/main.c
plugins/autopair.c
profiles/health/hdp.c
profiles/health/mcap.c
src/shared/util.c
src/shared/util.h
tools/btgatt-server.c

index 9c754d0..9f287ee 100755 (executable)
@@ -55,6 +55,8 @@ AC_ARG_ENABLE(threads, AS_HELP_STRING([--enable-threads],
 
 AC_CHECK_FUNCS(explicit_bzero)
 
+AC_CHECK_FUNCS(getrandom)
+
 AC_CHECK_FUNCS(rawmemchr)
 
 AC_CHECK_FUNC(signalfd, dummy=yes,
@@ -69,7 +71,7 @@ AC_CHECK_LIB(pthread, pthread_create, dummy=yes,
 AC_CHECK_LIB(dl, dlopen, dummy=yes,
                        AC_MSG_ERROR(dynamic linking loader is required))
 
-AC_CHECK_HEADERS(linux/types.h linux/if_alg.h linux/uinput.h linux/uhid.h)
+AC_CHECK_HEADERS(linux/types.h linux/if_alg.h linux/uinput.h linux/uhid.h sys/random.h)
 
 PKG_CHECK_MODULES(GLIB, glib-2.0 >= 2.28, dummy=yes,
                                AC_MSG_ERROR(GLib >= 2.28 is required))
index f8f313f..7656a65 100755 (executable)
@@ -20,7 +20,6 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <sys/uio.h>
-#include <sys/random.h>
 #include <time.h>
 
 #include "lib/bluetooth.h"
@@ -509,7 +508,7 @@ static unsigned int get_adv_delay(void)
        /* The advertising delay is a pseudo-random value with a range
         * of 0 ms to 10 ms generated for each advertising event.
         */
-       if (getrandom(&val, sizeof(val), 0) < 0) {
+       if (util_getrandom(&val, sizeof(val), 0) < 0) {
                /* If it fails to get the random number, use a static value */
                val = 5;
        }
index 42e808c..67c35e5 100755 (executable)
@@ -18,7 +18,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/socket.h>
-#include <sys/random.h>
 #include <netinet/in.h>
 #include <netinet/ip.h>
 #include <time.h>
@@ -173,7 +172,7 @@ struct bt_phy *bt_phy_new(void)
        mainloop_add_fd(phy->rx_fd, EPOLLIN, phy_rx_callback, phy, NULL);
 
        if (!get_random_bytes(&phy->id, sizeof(phy->id))) {
-               if (getrandom(&phy->id, sizeof(phy->id), 0) < 0) {
+               if (util_getrandom(&phy->id, sizeof(phy->id), 0) < 0) {
                        mainloop_remove_fd(phy->rx_fd);
                        close(phy->tx_fd);
                        close(phy->rx_fd);
index b4063d6..d3b22a6 100755 (executable)
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/mount.h>
-#include <sys/random.h>
 
 #ifndef WAIT_ANY
 #define WAIT_ANY (-1)
 #endif
 
 #include "src/shared/mainloop.h"
+#include "src/shared/util.h"
 #include "peripheral/efivars.h"
 #include "peripheral/attach.h"
 #include "peripheral/gap.h"
@@ -191,7 +191,7 @@ int main(int argc, char *argv[])
                                                        addr, 6) < 0) {
                        printf("Generating new persistent static address\n");
 
-                       if (getrandom(addr, sizeof(addr), 0) < 0) {
+                       if (util_getrandom(addr, sizeof(addr), 0) < 0) {
                                perror("Failed to get random static address");
                                return EXIT_FAILURE;
                        }
index a75eceb..0b09e89 100755 (executable)
@@ -17,7 +17,6 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
-#include <sys/random.h>
 
 #include <glib.h>
 
@@ -29,6 +28,7 @@
 #include "src/device.h"
 #include "src/log.h"
 #include "src/storage.h"
+#include "src/shared/util.h"
 
 /*
  * Plugin to handle automatic pairing of devices with reduced user
@@ -131,7 +131,7 @@ static ssize_t autopair_pincb(struct btd_adapter *adapter,
                        if (attempt >= 4)
                                return 0;
 
-                       if (getrandom(&val, sizeof(val), 0) < 0) {
+                       if (util_getrandom(&val, sizeof(val), 0) < 0) {
                                error("Failed to get a random pincode");
                                return 0;
                        }
index 0fa48e8..c3a9834 100755 (executable)
@@ -15,7 +15,6 @@
 #include <stdint.h>
 #include <stdbool.h>
 #include <unistd.h>
-#include <sys/random.h>
 
 #include <glib.h>
 
@@ -32,6 +31,7 @@
 #include "src/device.h"
 #include "src/sdpd.h"
 #include "src/shared/timeout.h"
+#include "src/shared/util.h"
 #include "btio/btio.h"
 
 #include "hdp_types.h"
@@ -1489,7 +1489,7 @@ static void *generate_echo_packet(void)
        if (!buf)
                return NULL;
 
-       if (getrandom(buf, HDP_ECHO_LEN, 0) < 0) {
+       if (util_getrandom(buf, HDP_ECHO_LEN, 0) < 0) {
                g_free(buf);
                return NULL;
        }
index 9055ead..c8fef78 100755 (executable)
@@ -17,7 +17,6 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <unistd.h>
-#include <sys/random.h>
 
 #include <glib.h>
 
@@ -26,6 +25,7 @@
 #include "btio/btio.h"
 #include "src/log.h"
 #include "src/shared/timeout.h"
+#include "src/shared/util.h"
 
 #include "mcap.h"
 
@@ -1903,7 +1903,7 @@ gboolean mcap_create_mcl(struct mcap_instance *mi,
                mcl->state = MCL_IDLE;
                bacpy(&mcl->addr, addr);
                set_default_cb(mcl);
-               if (getrandom(&val, sizeof(val), 0) < 0) {
+               if (util_getrandom(&val, sizeof(val), 0) < 0) {
                        mcap_instance_unref(mcl->mi);
                        g_free(mcl);
                        return FALSE;
@@ -2047,7 +2047,7 @@ static void connect_mcl_event_cb(GIOChannel *chan, GError *gerr,
                mcl->mi = mcap_instance_ref(mi);
                bacpy(&mcl->addr, &dst);
                set_default_cb(mcl);
-               if (getrandom(&val, sizeof(val), 0) < 0) {
+               if (util_getrandom(&val, sizeof(val), 0) < 0) {
                        mcap_instance_unref(mcl->mi);
                        g_free(mcl);
                        goto drop;
index db760ae..0f009b5 100755 (executable)
@@ -12,6 +12,7 @@
 #include <config.h>
 #endif
 
+#include <fcntl.h>
 #include <stdio.h>
 #include <ctype.h>
 #include <stdbool.h>
 #include <limits.h>
 #include <string.h>
 
+#ifdef HAVE_SYS_RANDOM_H
+#include <sys/random.h>
+#endif
+
 #include "src/shared/util.h"
 
 void *util_malloc(size_t size)
@@ -137,6 +142,26 @@ unsigned char util_get_dt(const char *parent, const char *name)
        return DT_UNKNOWN;
 }
 
+/* Helper for getting a random in case getrandom unavailable (glibc < 2.25) */
+ssize_t util_getrandom(void *buf, size_t buflen, unsigned int flags)
+{
+#ifdef HAVE_GETRANDOM
+       return getrandom(buf, buflen, flags);
+#else
+       int fd;
+       ssize_t bytes;
+
+       fd = open("/dev/urandom", O_CLOEXEC);
+       if (fd < 0)
+               return -1;
+
+       bytes = read(fd, buf, buflen);
+       close(fd);
+
+       return bytes;
+#endif
+}
+
 /* Helpers for bitfield operations */
 
 /* Find unique id in range from 1 to max but no bigger than 64. */
index 8ef6132..c01eccf 100755 (executable)
@@ -103,6 +103,8 @@ void util_hexdump(const char dir, const unsigned char *buf, size_t len,
 
 unsigned char util_get_dt(const char *parent, const char *name);
 
+ssize_t util_getrandom(void *buf, size_t buflen, unsigned int flags);
+
 uint8_t util_get_uid(uint64_t *bitmap, uint8_t max);
 void util_clear_uid(uint64_t *bitmap, uint8_t id);
 
index 6596971..6c36d47 100755 (executable)
@@ -19,7 +19,6 @@
 #include <getopt.h>
 #include <unistd.h>
 #include <errno.h>
-#include <sys/random.h>
 
 #include "lib/bluetooth.h"
 #include "lib/hci.h"
@@ -286,7 +285,7 @@ static bool hr_msrmt_cb(void *user_data)
        uint32_t cur_ee;
        uint32_t val;
 
-       if (getrandom(&val, sizeof(val), 0) < 0)
+       if (util_getrandom(&val, sizeof(val), 0) < 0)
                return false;
 
        pdu[0] = 0x06;