Skip to content

Commit

Permalink
build: fix sysctl() detection on macOS
Browse files Browse the repository at this point in the history
sysctl() on *BSD takes a "const int *name", whereas sysctl() on macOS
it takes an "int *name". So our configure check and sysctl() detection on
macOS currently fails:

```bash
/usr/include/sys/sysctl.h:759:9: note: candidate function not viable:
	no known conversion from 'const int [2]' to 'int *' for 1st argument
int     sysctl(int *, u_int, void *, size_t *, void *, size_t);
```

This change removes the name argument from the sysctl() detection check,
meaning we will detect correctly on macOS and *BSD.

For consistency we also switch to using the more generic, non-const
version of the name parameter in the rest of our usage.
  • Loading branch information
fanquake committed Mar 19, 2020
1 parent a421e0a commit e90e3e6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 deletions configure.ac
Expand Up @@ -935,19 +935,21 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
AC_MSG_CHECKING(for sysctl)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/types.h>
#include <sys/sysctl.h>]],
[[ static const int name[2] = {CTL_KERN, KERN_VERSION};
#ifdef __linux__
[[ #ifdef __linux__
#error "Don't use sysctl on Linux, it's deprecated even when it works"
#endif
sysctl(name, 2, nullptr, nullptr, nullptr, 0); ]])],
sysctl(nullptr, 2, nullptr, nullptr, nullptr, 0); ]])],
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_SYSCTL, 1,[Define this symbol if the BSD sysctl() is available]) ],
[ AC_MSG_RESULT(no)]
)

AC_MSG_CHECKING(for sysctl KERN_ARND)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/types.h>
#include <sys/sysctl.h>]],
[[ static const int name[2] = {CTL_KERN, KERN_ARND};
[[ #ifdef __linux__
#error "Don't use sysctl on Linux, it's deprecated even when it works"
#endif
static int name[2] = {CTL_KERN, KERN_ARND};
sysctl(name, 2, nullptr, nullptr, nullptr, 0); ]])],
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_SYSCTL_ARND, 1,[Define this symbol if the BSD sysctl(KERN_ARND) is available]) ],
[ AC_MSG_RESULT(no)]
Expand Down
4 changes: 2 additions & 2 deletions src/random.cpp
Expand Up @@ -321,10 +321,10 @@ void GetOSRand(unsigned char *ent32)
RandFailure();
}
#elif defined(HAVE_SYSCTL_ARND)
/* FreeBSD and similar. It is possible for the call to return less
/* FreeBSD, NetBSD and similar. It is possible for the call to return less
* bytes than requested, so need to read in a loop.
*/
static const int name[2] = {CTL_KERN, KERN_ARND};
static int name[2] = {CTL_KERN, KERN_ARND};
int have = 0;
do {
size_t len = NUM_OS_RANDOM_BYTES - have;
Expand Down

0 comments on commit e90e3e6

Please sign in to comment.