0005-build-Fix-errors-with-glibc-2-25.patch 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. From fb57ad9b9d107856e5f1c8135da04ffa2f7a11ac Mon Sep 17 00:00:00 2001
  2. From: Fabrice Fontaine <fontaine.fabrice@gmail.com>
  3. Date: Mon, 14 Feb 2022 21:17:39 +0100
  4. Subject: build: Fix errors with glibc < 2.25
  5. getrandom and sys/random.h are only available since glibc 2.25:
  6. https://www.gnu.org/software/gnulib/manual/html_node/sys_002frandom_002eh.html
  7. resulting in the following build failures since version 5.63 and
  8. https://git.kernel.org/pub/scm/bluetooth/bluez.git/log/?qt=grep&q=getrandom:
  9. plugins/autopair.c:20:24: fatal error: sys/random.h: No such file or directory
  10. #include <sys/random.h>
  11. ^
  12. To fix this build failure, add util_getrandom and a fallback (borrowed
  13. from pipewire and licensed under MIT):
  14. https://gitlab.freedesktop.org/pipewire/pipewire/-/blob/master/src/pipewire/utils.c
  15. Fixes:
  16. - http://autobuild.buildroot.org/results/6b8870d12e0804d6154230a7322c49416c1dc0e2
  17. [Retrieved from:
  18. https://git.kernel.org/pub/scm/bluetooth/bluez.git/commit/?id=fb57ad9b9d107856e5f1c8135da04ffa2f7a11ac]
  19. Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
  20. ---
  21. configure.ac | 4 +++-
  22. emulator/le.c | 3 +--
  23. emulator/phy.c | 3 +--
  24. peripheral/main.c | 4 ++--
  25. plugins/autopair.c | 4 ++--
  26. profiles/health/hdp.c | 4 ++--
  27. profiles/health/mcap.c | 6 +++---
  28. src/shared/util.c | 25 +++++++++++++++++++++++++
  29. src/shared/util.h | 2 ++
  30. tools/btgatt-server.c | 3 +--
  31. 10 files changed, 42 insertions(+), 16 deletions(-)
  32. diff --git a/configure.ac b/configure.ac
  33. index 07d068a4d..441bd5f29 100644
  34. --- a/configure.ac
  35. +++ b/configure.ac
  36. @@ -54,6 +54,8 @@ AC_ARG_ENABLE(threads, AS_HELP_STRING([--enable-threads],
  37. AC_CHECK_FUNCS(explicit_bzero)
  38. +AC_CHECK_FUNCS(getrandom)
  39. +
  40. AC_CHECK_FUNCS(rawmemchr)
  41. AC_CHECK_FUNC(signalfd, dummy=yes,
  42. @@ -68,7 +70,7 @@ AC_CHECK_LIB(pthread, pthread_create, dummy=yes,
  43. AC_CHECK_LIB(dl, dlopen, dummy=yes,
  44. AC_MSG_ERROR(dynamic linking loader is required))
  45. -AC_CHECK_HEADERS(linux/types.h linux/if_alg.h linux/uinput.h linux/uhid.h)
  46. +AC_CHECK_HEADERS(linux/types.h linux/if_alg.h linux/uinput.h linux/uhid.h sys/random.h)
  47. PKG_CHECK_MODULES(GLIB, glib-2.0 >= 2.28, dummy=yes,
  48. AC_MSG_ERROR(GLib >= 2.28 is required))
  49. diff --git a/emulator/le.c b/emulator/le.c
  50. index f8f313f2c..7656a657c 100644
  51. --- a/emulator/le.c
  52. +++ b/emulator/le.c
  53. @@ -20,7 +20,6 @@
  54. #include <sys/socket.h>
  55. #include <sys/un.h>
  56. #include <sys/uio.h>
  57. -#include <sys/random.h>
  58. #include <time.h>
  59. #include "lib/bluetooth.h"
  60. @@ -509,7 +508,7 @@ static unsigned int get_adv_delay(void)
  61. /* The advertising delay is a pseudo-random value with a range
  62. * of 0 ms to 10 ms generated for each advertising event.
  63. */
  64. - if (getrandom(&val, sizeof(val), 0) < 0) {
  65. + if (util_getrandom(&val, sizeof(val), 0) < 0) {
  66. /* If it fails to get the random number, use a static value */
  67. val = 5;
  68. }
  69. diff --git a/emulator/phy.c b/emulator/phy.c
  70. index 44cace438..7de85fb05 100644
  71. --- a/emulator/phy.c
  72. +++ b/emulator/phy.c
  73. @@ -19,7 +19,6 @@
  74. #include <stdlib.h>
  75. #include <string.h>
  76. #include <sys/socket.h>
  77. -#include <sys/random.h>
  78. #include <netinet/in.h>
  79. #include <netinet/ip.h>
  80. #include <time.h>
  81. @@ -174,7 +173,7 @@ struct bt_phy *bt_phy_new(void)
  82. mainloop_add_fd(phy->rx_fd, EPOLLIN, phy_rx_callback, phy, NULL);
  83. if (!get_random_bytes(&phy->id, sizeof(phy->id))) {
  84. - if (getrandom(&phy->id, sizeof(phy->id), 0) < 0) {
  85. + if (util_getrandom(&phy->id, sizeof(phy->id), 0) < 0) {
  86. mainloop_remove_fd(phy->rx_fd);
  87. close(phy->tx_fd);
  88. close(phy->rx_fd);
  89. diff --git a/peripheral/main.c b/peripheral/main.c
  90. index 91adb45fc..b82d7caf6 100644
  91. --- a/peripheral/main.c
  92. +++ b/peripheral/main.c
  93. @@ -25,13 +25,13 @@
  94. #include <sys/stat.h>
  95. #include <sys/types.h>
  96. #include <sys/mount.h>
  97. -#include <sys/random.h>
  98. #ifndef WAIT_ANY
  99. #define WAIT_ANY (-1)
  100. #endif
  101. #include "src/shared/mainloop.h"
  102. +#include "src/shared/util.h"
  103. #include "peripheral/efivars.h"
  104. #include "peripheral/attach.h"
  105. #include "peripheral/gap.h"
  106. @@ -192,7 +192,7 @@ int main(int argc, char *argv[])
  107. addr, 6) < 0) {
  108. printf("Generating new persistent static address\n");
  109. - if (getrandom(addr, sizeof(addr), 0) < 0) {
  110. + if (util_getrandom(addr, sizeof(addr), 0) < 0) {
  111. perror("Failed to get random static address");
  112. return EXIT_FAILURE;
  113. }
  114. diff --git a/plugins/autopair.c b/plugins/autopair.c
  115. index a75ecebe4..0b09e893f 100644
  116. --- a/plugins/autopair.c
  117. +++ b/plugins/autopair.c
  118. @@ -17,7 +17,6 @@
  119. #include <fcntl.h>
  120. #include <unistd.h>
  121. #include <errno.h>
  122. -#include <sys/random.h>
  123. #include <glib.h>
  124. @@ -29,6 +28,7 @@
  125. #include "src/device.h"
  126. #include "src/log.h"
  127. #include "src/storage.h"
  128. +#include "src/shared/util.h"
  129. /*
  130. * Plugin to handle automatic pairing of devices with reduced user
  131. @@ -131,7 +131,7 @@ static ssize_t autopair_pincb(struct btd_adapter *adapter,
  132. if (attempt >= 4)
  133. return 0;
  134. - if (getrandom(&val, sizeof(val), 0) < 0) {
  135. + if (util_getrandom(&val, sizeof(val), 0) < 0) {
  136. error("Failed to get a random pincode");
  137. return 0;
  138. }
  139. diff --git a/profiles/health/hdp.c b/profiles/health/hdp.c
  140. index 9d9d1e824..b6590cd3a 100644
  141. --- a/profiles/health/hdp.c
  142. +++ b/profiles/health/hdp.c
  143. @@ -16,7 +16,6 @@
  144. #include <stdint.h>
  145. #include <stdbool.h>
  146. #include <unistd.h>
  147. -#include <sys/random.h>
  148. #include <glib.h>
  149. @@ -33,6 +32,7 @@
  150. #include "src/device.h"
  151. #include "src/sdpd.h"
  152. #include "src/shared/timeout.h"
  153. +#include "src/shared/util.h"
  154. #include "btio/btio.h"
  155. #include "hdp_types.h"
  156. @@ -1490,7 +1490,7 @@ static void *generate_echo_packet(void)
  157. if (!buf)
  158. return NULL;
  159. - if (getrandom(buf, HDP_ECHO_LEN, 0) < 0) {
  160. + if (util_getrandom(buf, HDP_ECHO_LEN, 0) < 0) {
  161. g_free(buf);
  162. return NULL;
  163. }
  164. diff --git a/profiles/health/mcap.c b/profiles/health/mcap.c
  165. index aad0a08a3..5d2bac3d9 100644
  166. --- a/profiles/health/mcap.c
  167. +++ b/profiles/health/mcap.c
  168. @@ -19,7 +19,6 @@
  169. #include <errno.h>
  170. #include <unistd.h>
  171. #include <time.h>
  172. -#include <sys/random.h>
  173. #include <glib.h>
  174. @@ -28,6 +27,7 @@
  175. #include "btio/btio.h"
  176. #include "src/log.h"
  177. #include "src/shared/timeout.h"
  178. +#include "src/shared/util.h"
  179. #include "mcap.h"
  180. @@ -1905,7 +1905,7 @@ gboolean mcap_create_mcl(struct mcap_instance *mi,
  181. mcl->state = MCL_IDLE;
  182. bacpy(&mcl->addr, addr);
  183. set_default_cb(mcl);
  184. - if (getrandom(&val, sizeof(val), 0) < 0) {
  185. + if (util_getrandom(&val, sizeof(val), 0) < 0) {
  186. mcap_instance_unref(mcl->mi);
  187. g_free(mcl);
  188. return FALSE;
  189. @@ -2049,7 +2049,7 @@ static void connect_mcl_event_cb(GIOChannel *chan, GError *gerr,
  190. mcl->mi = mcap_instance_ref(mi);
  191. bacpy(&mcl->addr, &dst);
  192. set_default_cb(mcl);
  193. - if (getrandom(&val, sizeof(val), 0) < 0) {
  194. + if (util_getrandom(&val, sizeof(val), 0) < 0) {
  195. mcap_instance_unref(mcl->mi);
  196. g_free(mcl);
  197. goto drop;
  198. diff --git a/src/shared/util.c b/src/shared/util.c
  199. index 6e1c83057..33196bf8b 100644
  200. --- a/src/shared/util.c
  201. +++ b/src/shared/util.c
  202. @@ -13,6 +13,7 @@
  203. #endif
  204. #define _GNU_SOURCE
  205. +#include <fcntl.h>
  206. #include <stdio.h>
  207. #include <ctype.h>
  208. #include <stdbool.h>
  209. @@ -23,6 +24,10 @@
  210. #include <limits.h>
  211. #include <string.h>
  212. +#ifdef HAVE_SYS_RANDOM_H
  213. +#include <sys/random.h>
  214. +#endif
  215. +
  216. #include "src/shared/util.h"
  217. void *util_malloc(size_t size)
  218. @@ -138,6 +143,26 @@ unsigned char util_get_dt(const char *parent, const char *name)
  219. return DT_UNKNOWN;
  220. }
  221. +/* Helper for getting a random in case getrandom unavailable (glibc < 2.25) */
  222. +ssize_t util_getrandom(void *buf, size_t buflen, unsigned int flags)
  223. +{
  224. +#ifdef HAVE_GETRANDOM
  225. + return getrandom(buf, buflen, flags);
  226. +#else
  227. + int fd;
  228. + ssize_t bytes;
  229. +
  230. + fd = open("/dev/urandom", O_CLOEXEC);
  231. + if (fd < 0)
  232. + return -1;
  233. +
  234. + bytes = read(fd, buf, buflen);
  235. + close(fd);
  236. +
  237. + return bytes;
  238. +#endif
  239. +}
  240. +
  241. /* Helpers for bitfield operations */
  242. /* Find unique id in range from 1 to max but no bigger than 64. */
  243. diff --git a/src/shared/util.h b/src/shared/util.h
  244. index 8ef6132c4..c01eccf8a 100644
  245. --- a/src/shared/util.h
  246. +++ b/src/shared/util.h
  247. @@ -103,6 +103,8 @@ void util_hexdump(const char dir, const unsigned char *buf, size_t len,
  248. unsigned char util_get_dt(const char *parent, const char *name);
  249. +ssize_t util_getrandom(void *buf, size_t buflen, unsigned int flags);
  250. +
  251. uint8_t util_get_uid(uint64_t *bitmap, uint8_t max);
  252. void util_clear_uid(uint64_t *bitmap, uint8_t id);
  253. diff --git a/tools/btgatt-server.c b/tools/btgatt-server.c
  254. index 15d49a464..4a5d2b720 100644
  255. --- a/tools/btgatt-server.c
  256. +++ b/tools/btgatt-server.c
  257. @@ -20,7 +20,6 @@
  258. #include <getopt.h>
  259. #include <unistd.h>
  260. #include <errno.h>
  261. -#include <sys/random.h>
  262. #include "lib/bluetooth.h"
  263. #include "lib/hci.h"
  264. @@ -287,7 +286,7 @@ static bool hr_msrmt_cb(void *user_data)
  265. uint32_t cur_ee;
  266. uint32_t val;
  267. - if (getrandom(&val, sizeof(val), 0) < 0)
  268. + if (util_getrandom(&val, sizeof(val), 0) < 0)
  269. return false;
  270. pdu[0] = 0x06;
  271. --
  272. cgit