All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH BlueZ] fix build with glibc < 2.25
@ 2022-02-14 13:14 Fabrice Fontaine
  2022-02-14 14:14 ` Marcel Holtmann
  2022-02-14 14:26 ` [BlueZ] " bluez.test.bot
  0 siblings, 2 replies; 4+ messages in thread
From: Fabrice Fontaine @ 2022-02-14 13:14 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Fabrice Fontaine

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
so put back rand() as a fallback:

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

Fixes:
 - http://autobuild.buildroot.org/results/6b8870d12e0804d6154230a7322c49416c1dc0e2

Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
---
 configure.ac           |  2 ++
 emulator/le.c          |  7 +++++++
 emulator/phy.c         |  7 +++++++
 peripheral/main.c      | 10 ++++++++++
 plugins/autopair.c     |  6 ++++++
 profiles/health/hdp.c  | 11 +++++++++++
 profiles/health/mcap.c | 10 ++++++++++
 tools/btgatt-server.c  |  6 ++++++
 8 files changed, 59 insertions(+)

diff --git a/configure.ac b/configure.ac
index 07d068a4d..cdd693da3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -54,6 +54,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,
diff --git a/emulator/le.c b/emulator/le.c
index f8f313f2c..9ef0636d0 100644
--- a/emulator/le.c
+++ b/emulator/le.c
@@ -20,7 +20,9 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <sys/uio.h>
+#ifdef HAVE_GETRANDOM
 #include <sys/random.h>
+#endif
 #include <time.h>
 
 #include "lib/bluetooth.h"
@@ -509,10 +511,15 @@ 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.
 	 */
+#ifdef HAVE_GETRANDOM
 	if (getrandom(&val, sizeof(val), 0) < 0) {
 		/* If it fails to get the random number, use a static value */
 		val = 5;
 	}
+#else
+	srand(time(NULL));
+	val = rand();
+#endif
 
 	return (val % 11);
 }
diff --git a/emulator/phy.c b/emulator/phy.c
index 44cace438..e41aaf9c2 100644
--- a/emulator/phy.c
+++ b/emulator/phy.c
@@ -19,7 +19,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/socket.h>
+#ifdef HAVE_GETRANDOM
 #include <sys/random.h>
+#endif
 #include <netinet/in.h>
 #include <netinet/ip.h>
 #include <time.h>
@@ -174,6 +176,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))) {
+#ifdef GAVE_GETRANDOM
 		if (getrandom(&phy->id, sizeof(phy->id), 0) < 0) {
 			mainloop_remove_fd(phy->rx_fd);
 			close(phy->tx_fd);
@@ -181,6 +184,10 @@ struct bt_phy *bt_phy_new(void)
 			free(phy);
 			return NULL;
 		}
+#else
+		srandom(time(NULL));
+		phy->id = random();
+#endif
 	}
 
 	bt_phy_send(phy, BT_PHY_PKT_NULL, NULL, 0);
diff --git a/peripheral/main.c b/peripheral/main.c
index 91adb45fc..542adc330 100644
--- a/peripheral/main.c
+++ b/peripheral/main.c
@@ -25,7 +25,9 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/mount.h>
+#ifdef HAVE_GETRANDOM
 #include <sys/random.h>
+#endif
 
 #ifndef WAIT_ANY
 #define WAIT_ANY (-1)
@@ -192,10 +194,18 @@ int main(int argc, char *argv[])
 							addr, 6) < 0) {
 			printf("Generating new persistent static address\n");
 
+#ifdef HAVE_GETRANDOM
 			if (getrandom(addr, sizeof(addr), 0) < 0) {
 				perror("Failed to get random static address");
 				return EXIT_FAILURE;
 			}
+#else
+			addr[0] = rand();
+			addr[1] = rand();
+			addr[2] = rand();
+			addr[3] = 0x34;
+			addr[4] = 0x12;
+#endif
 			/* Overwrite the MSB to make it a static address */
 			addr[5] = 0xc0;
 
diff --git a/plugins/autopair.c b/plugins/autopair.c
index a75ecebe4..59d65807c 100644
--- a/plugins/autopair.c
+++ b/plugins/autopair.c
@@ -17,7 +17,9 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
+#ifdef HAVE_GETRANDOM
 #include <sys/random.h>
+#endif
 
 #include <glib.h>
 
@@ -131,10 +133,14 @@ static ssize_t autopair_pincb(struct btd_adapter *adapter,
 			if (attempt >= 4)
 				return 0;
 
+#ifdef HAVE_GETRANDOM
 			if (getrandom(&val, sizeof(val), 0) < 0) {
 				error("Failed to get a random pincode");
 				return 0;
 			}
+#else
+			val = rand();
+#endif
 			snprintf(pinstr, sizeof(pinstr), "%06u",
 						val % 1000000);
 			*display = true;
diff --git a/profiles/health/hdp.c b/profiles/health/hdp.c
index 9d9d1e824..ca59be3e8 100644
--- a/profiles/health/hdp.c
+++ b/profiles/health/hdp.c
@@ -16,7 +16,9 @@
 #include <stdint.h>
 #include <stdbool.h>
 #include <unistd.h>
+#ifdef HAVE_GETRANDOM
 #include <sys/random.h>
+#endif
 
 #include <glib.h>
 
@@ -1485,15 +1487,24 @@ static void destroy_create_dc_data(gpointer data)
 static void *generate_echo_packet(void)
 {
 	uint8_t *buf;
+#ifndef HAVE_GETRANDOM
+	int i;
+#endif
 
 	buf = g_malloc(HDP_ECHO_LEN);
 	if (!buf)
 		return NULL;
 
+#ifdef HAVE_GETRANDOM
 	if (getrandom(buf, HDP_ECHO_LEN, 0) < 0) {
 		g_free(buf);
 		return NULL;
 	}
+#else
+	srand(time(NULL));
+	for(i = 0; i < HDP_ECHO_LEN; i++)
+		buf[i] = rand() % UINT8_MAX;
+#endif
 
 	return buf;
 }
diff --git a/profiles/health/mcap.c b/profiles/health/mcap.c
index aad0a08a3..9bd994fda 100644
--- a/profiles/health/mcap.c
+++ b/profiles/health/mcap.c
@@ -19,7 +19,9 @@
 #include <errno.h>
 #include <unistd.h>
 #include <time.h>
+#ifdef HAVE_GETRANDOM
 #include <sys/random.h>
+#endif
 
 #include <glib.h>
 
@@ -1905,11 +1907,15 @@ gboolean mcap_create_mcl(struct mcap_instance *mi,
 		mcl->state = MCL_IDLE;
 		bacpy(&mcl->addr, addr);
 		set_default_cb(mcl);
+#ifdef HAVE_GETRANDOM
 		if (getrandom(&val, sizeof(val), 0) < 0) {
 			mcap_instance_unref(mcl->mi);
 			g_free(mcl);
 			return FALSE;
 		}
+#else
+		val = rand();
+#endif
 		mcl->next_mdl = (val % MCAP_MDLID_FINAL) + 1;
 	}
 
@@ -2049,11 +2055,15 @@ static void connect_mcl_event_cb(GIOChannel *chan, GError *gerr,
 		mcl->mi = mcap_instance_ref(mi);
 		bacpy(&mcl->addr, &dst);
 		set_default_cb(mcl);
+#ifdef HAVE_GETRANDOM
 		if (getrandom(&val, sizeof(val), 0) < 0) {
 			mcap_instance_unref(mcl->mi);
 			g_free(mcl);
 			goto drop;
 		}
+#else
+		val = rand();
+#endif
 		mcl->next_mdl = (val % MCAP_MDLID_FINAL) + 1;
 	}
 
diff --git a/tools/btgatt-server.c b/tools/btgatt-server.c
index 15d49a464..6367ccd9d 100644
--- a/tools/btgatt-server.c
+++ b/tools/btgatt-server.c
@@ -20,7 +20,9 @@
 #include <getopt.h>
 #include <unistd.h>
 #include <errno.h>
+#ifdef HAVE_GETRANDOM
 #include <sys/random.h>
+#endif
 
 #include "lib/bluetooth.h"
 #include "lib/hci.h"
@@ -287,8 +289,12 @@ static bool hr_msrmt_cb(void *user_data)
 	uint32_t cur_ee;
 	uint32_t val;
 
+#ifdef HAVE_GETRANDOM
 	if (getrandom(&val, sizeof(val), 0) < 0)
 		return false;
+#else
+	val = rand();
+#endif
 
 	pdu[0] = 0x06;
 	pdu[1] = 90 + (val % 40);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH BlueZ] fix build with glibc < 2.25
  2022-02-14 13:14 [PATCH BlueZ] fix build with glibc < 2.25 Fabrice Fontaine
@ 2022-02-14 14:14 ` Marcel Holtmann
  2022-02-14 15:21   ` Fabrice Fontaine
  2022-02-14 14:26 ` [BlueZ] " bluez.test.bot
  1 sibling, 1 reply; 4+ messages in thread
From: Marcel Holtmann @ 2022-02-14 14:14 UTC (permalink / raw)
  To: Fabrice Fontaine; +Cc: linux-bluetooth

Hi Fabrice,

> 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
> so put back rand() as a fallback:
> 
> plugins/autopair.c:20:24: fatal error: sys/random.h: No such file or directory
> #include <sys/random.h>
>                        ^
> 
> Fixes:
> - http://autobuild.buildroot.org/results/6b8870d12e0804d6154230a7322c49416c1dc0e2
> 
> Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
> ---
> configure.ac           |  2 ++
> emulator/le.c          |  7 +++++++
> emulator/phy.c         |  7 +++++++
> peripheral/main.c      | 10 ++++++++++
> plugins/autopair.c     |  6 ++++++
> profiles/health/hdp.c  | 11 +++++++++++
> profiles/health/mcap.c | 10 ++++++++++
> tools/btgatt-server.c  |  6 ++++++
> 8 files changed, 59 insertions(+)
> 
> diff --git a/configure.ac b/configure.ac
> index 07d068a4d..cdd693da3 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -54,6 +54,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,
> diff --git a/emulator/le.c b/emulator/le.c
> index f8f313f2c..9ef0636d0 100644
> --- a/emulator/le.c
> +++ b/emulator/le.c
> @@ -20,7 +20,9 @@
> #include <sys/socket.h>
> #include <sys/un.h>
> #include <sys/uio.h>
> +#ifdef HAVE_GETRANDOM
> #include <sys/random.h>
> +#endif
> #include <time.h>
> 
> #include "lib/bluetooth.h"
> @@ -509,10 +511,15 @@ 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.
> 	 */
> +#ifdef HAVE_GETRANDOM
> 	if (getrandom(&val, sizeof(val), 0) < 0) {
> 		/* If it fails to get the random number, use a static value */
> 		val = 5;
> 	}
> +#else
> +	srand(time(NULL));
> +	val = rand();
> +#endif

you need to introduce a src/missing.h and provide a getrandom fallback. I am not allowing to spread #ifdef around the code for some old glibc compatibility.

Regards

Marcel


^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [BlueZ] fix build with glibc < 2.25
  2022-02-14 13:14 [PATCH BlueZ] fix build with glibc < 2.25 Fabrice Fontaine
  2022-02-14 14:14 ` Marcel Holtmann
@ 2022-02-14 14:26 ` bluez.test.bot
  1 sibling, 0 replies; 4+ messages in thread
From: bluez.test.bot @ 2022-02-14 14:26 UTC (permalink / raw)
  To: linux-bluetooth, fontaine.fabrice

[-- Attachment #1: Type: text/plain, Size: 2352 bytes --]

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=614113

---Test result---

Test Summary:
CheckPatch                    FAIL      1.52 seconds
GitLint                       FAIL      1.09 seconds
Prep - Setup ELL              PASS      42.43 seconds
Build - Prep                  PASS      0.70 seconds
Build - Configure             PASS      8.54 seconds
Build - Make                  PASS      1374.98 seconds
Make Check                    PASS      11.33 seconds
Make Check w/Valgrind         PASS      440.73 seconds
Make Distcheck                PASS      228.35 seconds
Build w/ext ELL - Configure   PASS      9.06 seconds
Build w/ext ELL - Make        PASS      1360.65 seconds
Incremental Build with patchesPASS      0.00 seconds

Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script with rule in .checkpatch.conf
Output:
[BlueZ] fix build with glibc < 2.25
WARNING:COMMIT_LOG_LONG_LINE: Possible unwrapped commit description (prefer a maximum 75 chars per line)
#81: 
https://www.gnu.org/software/gnulib/manual/html_node/sys_002frandom_002eh.html

ERROR:SPACING: space required before the open parenthesis '('
#276: FILE: profiles/health/hdp.c:1505:
+	for(i = 0; i < HDP_ECHO_LEN; i++)

/github/workspace/src/12745617.patch total: 1 errors, 1 warnings, 201 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

/github/workspace/src/12745617.patch has style problems, please review.

NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO

NOTE: If any of the errors are false positives, please report
      them to the maintainer, see CHECKPATCH in MAINTAINERS.


##############################
Test: GitLint - FAIL
Desc: Run gitlint with rule in .gitlint
Output:
[BlueZ] fix build with glibc < 2.25
14: B1 Line exceeds max length (82>80): " - http://autobuild.buildroot.org/results/6b8870d12e0804d6154230a7322c49416c1dc0e2"




---
Regards,
Linux Bluetooth


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH BlueZ] fix build with glibc < 2.25
  2022-02-14 14:14 ` Marcel Holtmann
@ 2022-02-14 15:21   ` Fabrice Fontaine
  0 siblings, 0 replies; 4+ messages in thread
From: Fabrice Fontaine @ 2022-02-14 15:21 UTC (permalink / raw)
  To: Marcel Holtmann; +Cc: linux-bluetooth

Hi Marcel,

Le lun. 14 févr. 2022 à 15:14, Marcel Holtmann <marcel@holtmann.org> a écrit :
>
> Hi Fabrice,
>
> > 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
> > so put back rand() as a fallback:
> >
> > plugins/autopair.c:20:24: fatal error: sys/random.h: No such file or directory
> > #include <sys/random.h>
> >                        ^
> >
> > Fixes:
> > - http://autobuild.buildroot.org/results/6b8870d12e0804d6154230a7322c49416c1dc0e2
> >
> > Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
> > ---
> > configure.ac           |  2 ++
> > emulator/le.c          |  7 +++++++
> > emulator/phy.c         |  7 +++++++
> > peripheral/main.c      | 10 ++++++++++
> > plugins/autopair.c     |  6 ++++++
> > profiles/health/hdp.c  | 11 +++++++++++
> > profiles/health/mcap.c | 10 ++++++++++
> > tools/btgatt-server.c  |  6 ++++++
> > 8 files changed, 59 insertions(+)
> >
> > diff --git a/configure.ac b/configure.ac
> > index 07d068a4d..cdd693da3 100644
> > --- a/configure.ac
> > +++ b/configure.ac
> > @@ -54,6 +54,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,
> > diff --git a/emulator/le.c b/emulator/le.c
> > index f8f313f2c..9ef0636d0 100644
> > --- a/emulator/le.c
> > +++ b/emulator/le.c
> > @@ -20,7 +20,9 @@
> > #include <sys/socket.h>
> > #include <sys/un.h>
> > #include <sys/uio.h>
> > +#ifdef HAVE_GETRANDOM
> > #include <sys/random.h>
> > +#endif
> > #include <time.h>
> >
> > #include "lib/bluetooth.h"
> > @@ -509,10 +511,15 @@ 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.
> >        */
> > +#ifdef HAVE_GETRANDOM
> >       if (getrandom(&val, sizeof(val), 0) < 0) {
> >               /* If it fails to get the random number, use a static value */
> >               val = 5;
> >       }
> > +#else
> > +     srand(time(NULL));
> > +     val = rand();
> > +#endif
>
> you need to introduce a src/missing.h and provide a getrandom fallback. I am not allowing to spread #ifdef around the code for some old glibc compatibility.
OK, I'll send a v2.
>
> Regards
>
> Marcel
>
Best Regards,

Fabrice

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-02-14 15:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-14 13:14 [PATCH BlueZ] fix build with glibc < 2.25 Fabrice Fontaine
2022-02-14 14:14 ` Marcel Holtmann
2022-02-14 15:21   ` Fabrice Fontaine
2022-02-14 14:26 ` [BlueZ] " bluez.test.bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.