From a4b0b9afe5a782a7f4a8fe153ecf51bcb0180ae3 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 4 Mar 2021 10:57:48 +0100 Subject: [PATCH] build: check for getrandom and sys/random.h Fixes #833 --- config.h.meson | 4 ++++ meson.build | 5 +++++ src/pipewire/impl-core.c | 23 ++++++++++++++++++----- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/config.h.meson b/config.h.meson index e2223d8c5..a5fc15f6b 100644 --- a/config.h.meson +++ b/config.h.meson @@ -269,6 +269,9 @@ /* Define to 1 if you have the header file. */ #mesondefine HAVE_SYS_PRCTL_H +/* Define to 1 if you have the header file. */ +#mesondefine HAVE_SYS_RANDOM_H + /* Define to 1 if you have the header file. */ #mesondefine HAVE_SYS_SOCKET_H @@ -473,6 +476,7 @@ #mesondefine HAVE_DECL_LOCALTIME_R #mesondefine HAVE_DECL_STRSIGNAL #mesondefine HAVE_MEMFD_CREATE +#mesondefine HAVE_GETRANDOM #mesondefine PIPEWIRE_VERSION_MAJOR #mesondefine PIPEWIRE_VERSION_MINOR diff --git a/meson.build b/meson.build index 6f810221a..b722dbc2b 100644 --- a/meson.build +++ b/meson.build @@ -218,6 +218,7 @@ check_headers = [['dlfcn.h','HAVE_DLFCN_H'], ['sys/param.h', 'HAVE_SYS_PARAM_H'], ['sys/poll.h', 'HAVE_SYS_POLL_H'], ['sys/prctl.h', 'HAVE_SYS_PRCTL_H'], + ['sys/random.h', 'HAVE_SYS_RANDOM_H'], ['sys/socket.h', 'HAVE_SYS_SOCKET_H'], ['sys/stat.h', 'HAVE_SYS_STAT_H'], ['sys/times.h', 'HAVE_SYS_TIMES_H'], @@ -272,6 +273,10 @@ if cc.has_function('memfd_create', prefix : '#include ', args : [ '- cdata.set('HAVE_MEMFD_CREATE', 1) endif +if cc.has_function('getrandom', prefix : '#include ') + cdata.set('HAVE_GETRANDOM', 1) +endif + if get_option('systemd') systemd = dependency('systemd', required: false) systemd_dep = dependency('libsystemd', required: false) diff --git a/src/pipewire/impl-core.c b/src/pipewire/impl-core.c index d491f994a..92909bce6 100644 --- a/src/pipewire/impl-core.c +++ b/src/pipewire/impl-core.c @@ -22,20 +22,34 @@ * DEALINGS IN THE SOFTWARE. */ +#include "config.h" + #include #include #ifndef ENODATA #define ENODATA 9919 #endif +#if HAVE_SYS_RANDOM_H #include +#endif -#ifdef __FreeBSD__ -#include -#include +#undef GETRANDOM_FALLBACK +#ifndef HAVE_GETRANDOM +# ifdef __FreeBSD__ +# include +# include // FreeBSD versions < 12 do not have getrandom() syscall // Give a poor-man implementation here // Can be removed after September 30, 2021 -#if __FreeBSD_version < 1200000 +# if __FreeBSD_version < 1200000 +# define GETRANDOM_FALLBACK 1 +# endif +# else +# define GETRANDOM_FALLBACK 1 +# endif +#endif + +#ifdef GETRANDOM_FALLBACK ssize_t getrandom(void *buf, size_t buflen, unsigned int flags) { int fd = open("/dev/random", O_CLOEXEC); if (fd < 0) @@ -45,7 +59,6 @@ ssize_t getrandom(void *buf, size_t buflen, unsigned int flags) { return bytes; } #endif -#endif #include