Skip to content

Commit fb57ad9

Browse files
ffontaineVudentz
authored andcommitted
build: Fix errors with glibc < 2.25
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
1 parent b5ff08b commit fb57ad9

File tree

10 files changed

+42
-16
lines changed

10 files changed

+42
-16
lines changed

configure.ac

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ AC_ARG_ENABLE(threads, AS_HELP_STRING([--enable-threads],
5454

5555
AC_CHECK_FUNCS(explicit_bzero)
5656

57+
AC_CHECK_FUNCS(getrandom)
58+
5759
AC_CHECK_FUNCS(rawmemchr)
5860

5961
AC_CHECK_FUNC(signalfd, dummy=yes,
@@ -68,7 +70,7 @@ AC_CHECK_LIB(pthread, pthread_create, dummy=yes,
6870
AC_CHECK_LIB(dl, dlopen, dummy=yes,
6971
AC_MSG_ERROR(dynamic linking loader is required))
7072

71-
AC_CHECK_HEADERS(linux/types.h linux/if_alg.h linux/uinput.h linux/uhid.h)
73+
AC_CHECK_HEADERS(linux/types.h linux/if_alg.h linux/uinput.h linux/uhid.h sys/random.h)
7274

7375
PKG_CHECK_MODULES(GLIB, glib-2.0 >= 2.28, dummy=yes,
7476
AC_MSG_ERROR(GLib >= 2.28 is required))

emulator/le.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <sys/socket.h>
2121
#include <sys/un.h>
2222
#include <sys/uio.h>
23-
#include <sys/random.h>
2423
#include <time.h>
2524

2625
#include "lib/bluetooth.h"
@@ -509,7 +508,7 @@ static unsigned int get_adv_delay(void)
509508
/* The advertising delay is a pseudo-random value with a range
510509
* of 0 ms to 10 ms generated for each advertising event.
511510
*/
512-
if (getrandom(&val, sizeof(val), 0) < 0) {
511+
if (util_getrandom(&val, sizeof(val), 0) < 0) {
513512
/* If it fails to get the random number, use a static value */
514513
val = 5;
515514
}

emulator/phy.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include <stdlib.h>
2020
#include <string.h>
2121
#include <sys/socket.h>
22-
#include <sys/random.h>
2322
#include <netinet/in.h>
2423
#include <netinet/ip.h>
2524
#include <time.h>
@@ -174,7 +173,7 @@ struct bt_phy *bt_phy_new(void)
174173
mainloop_add_fd(phy->rx_fd, EPOLLIN, phy_rx_callback, phy, NULL);
175174

176175
if (!get_random_bytes(&phy->id, sizeof(phy->id))) {
177-
if (getrandom(&phy->id, sizeof(phy->id), 0) < 0) {
176+
if (util_getrandom(&phy->id, sizeof(phy->id), 0) < 0) {
178177
mainloop_remove_fd(phy->rx_fd);
179178
close(phy->tx_fd);
180179
close(phy->rx_fd);

peripheral/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
#include <sys/stat.h>
2626
#include <sys/types.h>
2727
#include <sys/mount.h>
28-
#include <sys/random.h>
2928

3029
#ifndef WAIT_ANY
3130
#define WAIT_ANY (-1)
3231
#endif
3332

3433
#include "src/shared/mainloop.h"
34+
#include "src/shared/util.h"
3535
#include "peripheral/efivars.h"
3636
#include "peripheral/attach.h"
3737
#include "peripheral/gap.h"
@@ -192,7 +192,7 @@ int main(int argc, char *argv[])
192192
addr, 6) < 0) {
193193
printf("Generating new persistent static address\n");
194194

195-
if (getrandom(addr, sizeof(addr), 0) < 0) {
195+
if (util_getrandom(addr, sizeof(addr), 0) < 0) {
196196
perror("Failed to get random static address");
197197
return EXIT_FAILURE;
198198
}

plugins/autopair.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <fcntl.h>
1818
#include <unistd.h>
1919
#include <errno.h>
20-
#include <sys/random.h>
2120

2221
#include <glib.h>
2322

@@ -29,6 +28,7 @@
2928
#include "src/device.h"
3029
#include "src/log.h"
3130
#include "src/storage.h"
31+
#include "src/shared/util.h"
3232

3333
/*
3434
* Plugin to handle automatic pairing of devices with reduced user
@@ -131,7 +131,7 @@ static ssize_t autopair_pincb(struct btd_adapter *adapter,
131131
if (attempt >= 4)
132132
return 0;
133133

134-
if (getrandom(&val, sizeof(val), 0) < 0) {
134+
if (util_getrandom(&val, sizeof(val), 0) < 0) {
135135
error("Failed to get a random pincode");
136136
return 0;
137137
}

profiles/health/hdp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <stdint.h>
1717
#include <stdbool.h>
1818
#include <unistd.h>
19-
#include <sys/random.h>
2019

2120
#include <glib.h>
2221

@@ -33,6 +32,7 @@
3332
#include "src/device.h"
3433
#include "src/sdpd.h"
3534
#include "src/shared/timeout.h"
35+
#include "src/shared/util.h"
3636
#include "btio/btio.h"
3737

3838
#include "hdp_types.h"
@@ -1490,7 +1490,7 @@ static void *generate_echo_packet(void)
14901490
if (!buf)
14911491
return NULL;
14921492

1493-
if (getrandom(buf, HDP_ECHO_LEN, 0) < 0) {
1493+
if (util_getrandom(buf, HDP_ECHO_LEN, 0) < 0) {
14941494
g_free(buf);
14951495
return NULL;
14961496
}

profiles/health/mcap.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include <errno.h>
2020
#include <unistd.h>
2121
#include <time.h>
22-
#include <sys/random.h>
2322

2423
#include <glib.h>
2524

@@ -28,6 +27,7 @@
2827
#include "btio/btio.h"
2928
#include "src/log.h"
3029
#include "src/shared/timeout.h"
30+
#include "src/shared/util.h"
3131

3232
#include "mcap.h"
3333

@@ -1905,7 +1905,7 @@ gboolean mcap_create_mcl(struct mcap_instance *mi,
19051905
mcl->state = MCL_IDLE;
19061906
bacpy(&mcl->addr, addr);
19071907
set_default_cb(mcl);
1908-
if (getrandom(&val, sizeof(val), 0) < 0) {
1908+
if (util_getrandom(&val, sizeof(val), 0) < 0) {
19091909
mcap_instance_unref(mcl->mi);
19101910
g_free(mcl);
19111911
return FALSE;
@@ -2049,7 +2049,7 @@ static void connect_mcl_event_cb(GIOChannel *chan, GError *gerr,
20492049
mcl->mi = mcap_instance_ref(mi);
20502050
bacpy(&mcl->addr, &dst);
20512051
set_default_cb(mcl);
2052-
if (getrandom(&val, sizeof(val), 0) < 0) {
2052+
if (util_getrandom(&val, sizeof(val), 0) < 0) {
20532053
mcap_instance_unref(mcl->mi);
20542054
g_free(mcl);
20552055
goto drop;

src/shared/util.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#endif
1414

1515
#define _GNU_SOURCE
16+
#include <fcntl.h>
1617
#include <stdio.h>
1718
#include <ctype.h>
1819
#include <stdbool.h>
@@ -23,6 +24,10 @@
2324
#include <limits.h>
2425
#include <string.h>
2526

27+
#ifdef HAVE_SYS_RANDOM_H
28+
#include <sys/random.h>
29+
#endif
30+
2631
#include "src/shared/util.h"
2732

2833
void *util_malloc(size_t size)
@@ -138,6 +143,26 @@ unsigned char util_get_dt(const char *parent, const char *name)
138143
return DT_UNKNOWN;
139144
}
140145

146+
/* Helper for getting a random in case getrandom unavailable (glibc < 2.25) */
147+
ssize_t util_getrandom(void *buf, size_t buflen, unsigned int flags)
148+
{
149+
#ifdef HAVE_GETRANDOM
150+
return getrandom(buf, buflen, flags);
151+
#else
152+
int fd;
153+
ssize_t bytes;
154+
155+
fd = open("/dev/urandom", O_CLOEXEC);
156+
if (fd < 0)
157+
return -1;
158+
159+
bytes = read(fd, buf, buflen);
160+
close(fd);
161+
162+
return bytes;
163+
#endif
164+
}
165+
141166
/* Helpers for bitfield operations */
142167

143168
/* Find unique id in range from 1 to max but no bigger than 64. */

src/shared/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ void util_hexdump(const char dir, const unsigned char *buf, size_t len,
103103

104104
unsigned char util_get_dt(const char *parent, const char *name);
105105

106+
ssize_t util_getrandom(void *buf, size_t buflen, unsigned int flags);
107+
106108
uint8_t util_get_uid(uint64_t *bitmap, uint8_t max);
107109
void util_clear_uid(uint64_t *bitmap, uint8_t id);
108110

tools/btgatt-server.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <getopt.h>
2121
#include <unistd.h>
2222
#include <errno.h>
23-
#include <sys/random.h>
2423

2524
#include "lib/bluetooth.h"
2625
#include "lib/hci.h"
@@ -287,7 +286,7 @@ static bool hr_msrmt_cb(void *user_data)
287286
uint32_t cur_ee;
288287
uint32_t val;
289288

290-
if (getrandom(&val, sizeof(val), 0) < 0)
289+
if (util_getrandom(&val, sizeof(val), 0) < 0)
291290
return false;
292291

293292
pdu[0] = 0x06;

0 commit comments

Comments
 (0)