build: check for getrandom and sys/random.h

Fixes #833
This commit is contained in:
Wim Taymans 2021-03-04 10:57:48 +01:00
parent 3af768f124
commit a4b0b9afe5
3 changed files with 27 additions and 5 deletions

View File

@ -269,6 +269,9 @@
/* Define to 1 if you have the <sys/prctl.h> header file. */
#mesondefine HAVE_SYS_PRCTL_H
/* Define to 1 if you have the <sys/random.h> header file. */
#mesondefine HAVE_SYS_RANDOM_H
/* Define to 1 if you have the <sys/socket.h> 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

View File

@ -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 <sys/mman.h>', args : [ '-
cdata.set('HAVE_MEMFD_CREATE', 1)
endif
if cc.has_function('getrandom', prefix : '#include <sys/random.h>')
cdata.set('HAVE_GETRANDOM', 1)
endif
if get_option('systemd')
systemd = dependency('systemd', required: false)
systemd_dep = dependency('libsystemd', required: false)

View File

@ -22,20 +22,34 @@
* DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#include <unistd.h>
#include <errno.h>
#ifndef ENODATA
#define ENODATA 9919
#endif
#if HAVE_SYS_RANDOM_H
#include <sys/random.h>
#endif
#ifdef __FreeBSD__
#include <sys/param.h>
#include <fcntl.h>
#undef GETRANDOM_FALLBACK
#ifndef HAVE_GETRANDOM
# ifdef __FreeBSD__
# include <sys/param.h>
# include <fcntl.h>
// 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 <spa/debug/types.h>