Merge #21486: build: link against -lsocket if required for *ifaddrs

4783115fd4cccb46a7f8c592b34fa7c094c29410 net: add ifaddrs.h include (fanquake)
879215e665a9f348c8d3fa92701c34065bc86a69 build: check if -lsocket is required with *ifaddrs (fanquake)
87deac66aa747481e6f34fc80599e1e490de3ea0 rand: only try and use freeifaddrs if available (fanquake)

Pull request description:

  Fixes #21485 by linking against `-lsocket` when it's required for using `*ifaddrs` functions.

ACKs for top commit:
  laanwj:
    Code review ACK 4783115fd4cccb46a7f8c592b34fa7c094c29410
  hebasto:
    ACK 4783115fd4cccb46a7f8c592b34fa7c094c29410, I have reviewed the code and it looks OK, I agree it can be merged.

Tree-SHA512: 4542e036e9b029de970eff8a9230fe45d9204bb22313d075f474295d49bdaf1f1cbb36c0c6e2fa8dbbcdba518d8d3a68a6116ce304b82414315f333baf9af0e4
This commit is contained in:
fanquake 2021-03-31 14:37:06 +08:00 committed by pasta
parent dfd5f68041
commit 2966452a1b
No known key found for this signature in database
GPG Key ID: 52527BEDABE87984
4 changed files with 43 additions and 3 deletions

36
build-aux/m4/l_socket.m4 Normal file
View File

@ -0,0 +1,36 @@
# Illumos/SmartOS requires linking with -lsocket if
# using getifaddrs & freeifaddrs
m4_define([_CHECK_SOCKET_testbody], [[
#include <sys/types.h>
#include <ifaddrs.h>
int main() {
struct ifaddrs *ifaddr;
getifaddrs(&ifaddr);
freeifaddrs(ifaddr);
}
]])
AC_DEFUN([CHECK_SOCKET], [
AC_LANG_PUSH(C++)
AC_MSG_CHECKING([whether ifaddrs funcs can be used without link library])
AC_LINK_IFELSE([AC_LANG_SOURCE([_CHECK_SOCKET_testbody])],[
AC_MSG_RESULT([yes])
],[
AC_MSG_RESULT([no])
LIBS="$LIBS -lsocket"
AC_MSG_CHECKING([whether getifaddrs needs -lsocket])
AC_LINK_IFELSE([AC_LANG_SOURCE([_CHECK_SOCKET_testbody])],[
AC_MSG_RESULT([yes])
],[
AC_MSG_RESULT([no])
AC_MSG_FAILURE([cannot figure out how to use getifaddrs])
])
])
AC_LANG_POP
])

View File

@ -935,7 +935,7 @@ fi
AC_CHECK_HEADERS([endian.h sys/endian.h byteswap.h stdio.h stdlib.h unistd.h strings.h sys/types.h sys/stat.h sys/select.h sys/prctl.h sys/sysctl.h vm/vm_param.h sys/vmmeter.h sys/resources.h]) AC_CHECK_HEADERS([endian.h sys/endian.h byteswap.h stdio.h stdlib.h unistd.h strings.h sys/types.h sys/stat.h sys/select.h sys/prctl.h sys/sysctl.h vm/vm_param.h sys/vmmeter.h sys/resources.h])
AC_CHECK_DECLS([getifaddrs, freeifaddrs],,, AC_CHECK_DECLS([getifaddrs, freeifaddrs],[CHECK_SOCKET],,
[#include <sys/types.h> [#include <sys/types.h>
#include <ifaddrs.h>] #include <ifaddrs.h>]
) )

View File

@ -38,6 +38,10 @@
#include <fcntl.h> #include <fcntl.h>
#endif #endif
#if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS
#include <ifaddrs.h>
#endif
#ifdef USE_POLL #ifdef USE_POLL
#include <poll.h> #include <poll.h>
#endif #endif

View File

@ -38,7 +38,7 @@
#include <sys/utsname.h> #include <sys/utsname.h>
#include <unistd.h> #include <unistd.h>
#endif #endif
#if HAVE_DECL_GETIFADDRS #if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS
#include <ifaddrs.h> #include <ifaddrs.h>
#endif #endif
#if HAVE_SYSCTL #if HAVE_SYSCTL
@ -361,7 +361,7 @@ void RandAddStaticEnv(CSHA512& hasher)
hasher.Write((const unsigned char*)hname, strnlen(hname, 256)); hasher.Write((const unsigned char*)hname, strnlen(hname, 256));
} }
#if HAVE_DECL_GETIFADDRS #if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS
// Network interfaces // Network interfaces
struct ifaddrs *ifad = NULL; struct ifaddrs *ifad = NULL;
getifaddrs(&ifad); getifaddrs(&ifad);