Revert "Revert "Upgrade to 2.1.8-stable (2017-01-22)." and "Probably Mac build fix?""

This reverts commit 83a0c9c65a60a92d3ea5542596b3ba56db492c37.

Bug: 64543673
Test: make checkbuild
Test: Manual tombstoned test
Change-Id: I84bb128d1dec433195f2cbdbf70236ba17fa9955
diff --git a/configure.ac b/configure.ac
index d42edd8..7528d37 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,4 +1,3 @@
-dnl configure.in for libevent
 dnl Copyright 2000-2007 Niels Provos
 dnl Copyright 2007-2012 Niels Provos and Nick Mathewson
 dnl
@@ -6,34 +5,44 @@
 dnl
 dnl Original version Dug Song <dugsong@monkey.org>
 
+AC_INIT(libevent,2.1.8-stable)
 AC_PREREQ(2.59)
-AC_INIT(event.c)
+AC_CONFIG_SRCDIR(event.c)
 
 AC_CONFIG_MACRO_DIR([m4])
-
-AM_INIT_AUTOMAKE(libevent,2.0.22-stable)
-AC_CONFIG_HEADERS(config.h)
-AC_DEFINE(NUMERIC_VERSION, 0x02001600, [Numeric representation of the version])
+AM_INIT_AUTOMAKE
+dnl AM_SILENT_RULES req. automake 1.11.  [no] defaults V=1
+m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
+AC_CONFIG_HEADERS(config.h  evconfig-private.h:evconfig-private.h.in)
+AC_DEFINE(NUMERIC_VERSION, 0x02010800, [Numeric representation of the version])
 
 dnl Initialize prefix.
 if test "$prefix" = "NONE"; then
    prefix="/usr/local"
 fi
 
+dnl Try and get a full POSIX environment on obscure systems
+ifdef([AC_USE_SYSTEM_EXTENSIONS], [
+AC_USE_SYSTEM_EXTENSIONS
+], [
+AC_AIX
+AC_GNU_SOURCE
+AC_MINIX
+])
+
 AC_CANONICAL_BUILD
 AC_CANONICAL_HOST
 dnl the 'build' machine is where we run configure and compile
 dnl the 'host' machine is where the resulting stuff runs.
 
-case "$host_os" in
-
- osf5*)
-    CFLAGS="$CFLAGS -D_OSF_SOURCE"
-    ;;
-esac
+#case "$host_os" in
+#
+# osf5*)
+#    CFLAGS="$CFLAGS -D_OSF_SOURCE"
+#    ;;
+#esac
 
 dnl Checks for programs.
-AC_PROG_CC
 AM_PROG_CC_C_O
 AC_PROG_INSTALL
 AC_PROG_LN_S
@@ -65,16 +74,26 @@
 fi
 
 # OS X Lion started deprecating the system openssl. Let's just disable
-# all deprecation warnings on OS X.
-case "$host_os" in
-
- darwin*)
-    CFLAGS="$CFLAGS -Wno-deprecated-declarations"
-    ;;
-esac
+# all deprecation warnings on OS X; but do so only for gcc...
+if test "$GCC" = "yes" ; then
+	case "$host_os" in
+	 darwin*)
+	    CFLAGS="$CFLAGS -Wno-deprecated-declarations"
+	    ;;
+	esac
+fi
 
 AC_ARG_ENABLE(gcc-warnings,
-     AS_HELP_STRING(--enable-gcc-warnings, enable verbose warnings with GCC))
+     AS_HELP_STRING(--disable-gcc-warnings, disable verbose warnings with GCC))
+
+AC_ARG_ENABLE(gcc-hardening,
+     AS_HELP_STRING(--enable-gcc-hardening, enable compiler security checks),
+[if test x$enableval = xyes; then
+    CFLAGS="$CFLAGS -D_FORTIFY_SOURCE=2 -fstack-protector-all"
+    CFLAGS="$CFLAGS -fwrapv -fPIE -Wstack-protector"
+    CFLAGS="$CFLAGS --param ssp-buffer-size=1"
+fi])
+
 AC_ARG_ENABLE(thread-support,
      AS_HELP_STRING(--disable-thread-support, disable support for threading),
 	[], [enable_thread_support=yes])
@@ -93,34 +112,47 @@
 AC_ARG_ENABLE([libevent-regress],
      AS_HELP_STRING([--disable-libevent-regress, skip regress in make check]),
 	[], [enable_libevent_regress=yes])
+AC_ARG_ENABLE([samples],
+     AS_HELP_STRING([--disable-samples, skip building of sample programs]),
+	[], [enable_samples=yes])
 AC_ARG_ENABLE([function-sections],
      AS_HELP_STRING([--enable-function-sections, make static library allow smaller binaries with --gc-sections]),
 	[], [enable_function_sections=no])
+AC_ARG_ENABLE([verbose-debug],
+		AS_HELP_STRING([--enable-verbose-debug, verbose debug logging]),
+	[], [enable_verbose_debug=no])
+AC_ARG_ENABLE([clock-gettime],
+     AS_HELP_STRING(--disable-clock-gettime, do not use clock_gettime even if it is available),
+  [], [enable_clock_gettime=yes])
 
 
 AC_PROG_LIBTOOL
 
-dnl   Uncomment "AC_DISABLE_SHARED" to make shared librraries not get
+dnl   Uncomment "AC_DISABLE_SHARED" to make shared libraries not get
 dnl   built by default.  You can also turn shared libs on and off from
 dnl   the command line with --enable-shared and --disable-shared.
 dnl AC_DISABLE_SHARED
 AC_SUBST(LIBTOOL_DEPS)
 
+AM_CONDITIONAL([BUILD_SAMPLES], [test "$enable_samples" = "yes"])
 AM_CONDITIONAL([BUILD_REGRESS], [test "$enable_libevent_regress" = "yes"])
 
 dnl Checks for libraries.
 AC_SEARCH_LIBS([inet_ntoa], [nsl])
 AC_SEARCH_LIBS([socket], [socket])
 AC_SEARCH_LIBS([inet_aton], [resolv])
-AC_SEARCH_LIBS([clock_gettime], [rt])
+if test "x$enable_clock_gettime" = "xyes"; then
+  AC_SEARCH_LIBS([clock_gettime], [rt])
+  AC_CHECK_FUNCS([clock_gettime])
+fi
 AC_SEARCH_LIBS([sendfile], [sendfile])
 
-dnl - check if the macro WIN32 is defined on this compiler.
-dnl - (this is how we check for a windows version of GCC)
+dnl - check if the macro _WIN32 is defined on this compiler.
+dnl - (this is how we check for a windows compiler)
 AC_MSG_CHECKING(for WIN32)
 AC_TRY_COMPILE(,
 	[
-#ifndef WIN32
+#ifndef _WIN32
 die horribly
 #endif
 	],
@@ -171,27 +203,44 @@
 AC_SUBST(EV_LIB_GDI)
 AC_SUBST(OPENSSL_LIBADD)
 
-AC_CHECK_HEADERS([openssl/bio.h])
+AC_SYS_LARGEFILE
 
-if test "$enable_openssl" = "yes"; then
-save_LIBS="$LIBS"
-LIBS=""
-OPENSSL_LIBS=""
-have_openssl=no
-AC_SEARCH_LIBS([SSL_new], [ssl],
-	[have_openssl=yes
-	OPENSSL_LIBS="$LIBS -lcrypto $EV_LIB_GDI $EV_LIB_WS32 $OPENSSL_LIBADD"
-	AC_DEFINE(HAVE_OPENSSL, 1, [Define if the system has openssl])],
-	[have_openssl=no],
-	[-lcrypto $EV_LIB_GDI $EV_LIB_WS32 $OPENSSL_LIBADD])
-LIBS="$save_LIBS"
-AC_SUBST(OPENSSL_LIBS)
-fi
+LIBEVENT_OPENSSL
 
 dnl Checks for header files.
-AC_HEADER_STDC
-AC_CHECK_HEADERS([fcntl.h stdarg.h inttypes.h stdint.h stddef.h poll.h unistd.h sys/epoll.h sys/time.h sys/queue.h sys/event.h sys/param.h sys/ioctl.h sys/select.h sys/devpoll.h port.h netinet/in.h netinet/in6.h sys/socket.h sys/uio.h arpa/inet.h sys/eventfd.h sys/mman.h sys/sendfile.h sys/wait.h netdb.h])
-AC_CHECK_HEADERS([sys/stat.h])
+AC_CHECK_HEADERS([ \
+  arpa/inet.h \
+  fcntl.h \
+  ifaddrs.h \
+  mach/mach_time.h \
+  netdb.h \
+  netinet/in.h \
+  netinet/in6.h \
+  netinet/tcp.h \
+  poll.h \
+  port.h \
+  stdarg.h \
+  stddef.h \
+  sys/devpoll.h \
+  sys/epoll.h \
+  sys/event.h \
+  sys/eventfd.h \
+  sys/ioctl.h \
+  sys/mman.h \
+  sys/param.h \
+  sys/queue.h \
+  sys/resource.h \
+  sys/select.h \
+  sys/sendfile.h \
+  sys/socket.h \
+  sys/stat.h \
+  sys/time.h \
+  sys/timerfd.h \
+  sys/uio.h \
+  sys/wait.h \
+  errno.h \
+])
+
 AC_CHECK_HEADERS(sys/sysctl.h, [], [], [
 #ifdef HAVE_SYS_PARAM_H
 #include <sys/param.h>
@@ -289,9 +338,47 @@
 AC_HEADER_TIME
 
 dnl Checks for library functions.
-AC_CHECK_FUNCS([gettimeofday vasprintf fcntl clock_gettime strtok_r strsep])
-AC_CHECK_FUNCS([getnameinfo strlcpy inet_ntop inet_pton signal sigaction strtoll inet_aton pipe eventfd sendfile mmap splice arc4random arc4random_buf issetugid geteuid getegid getprotobynumber setenv unsetenv putenv sysctl])
-AC_CHECK_FUNCS([umask])
+AC_CHECK_FUNCS([ \
+  accept4 \
+  arc4random \
+  arc4random_buf \
+  eventfd \
+  epoll_create1 \
+  fcntl \
+  getegid \
+  geteuid \
+  getifaddrs \
+  getnameinfo \
+  getprotobynumber \
+  gettimeofday \
+  inet_ntop \
+  inet_pton \
+  issetugid \
+  mach_absolute_time \
+  mmap \
+  nanosleep \
+  pipe \
+  pipe2 \
+  putenv \
+  sendfile \
+  setenv \
+  setrlimit \
+  sigaction \
+  signal \
+  splice \
+  strlcpy \
+  strsep \
+  strtok_r \
+  strtoll \
+  sysctl \
+  timerfd_create \
+  umask \
+  unsetenv \
+  usleep \
+  vasprintf \
+  getservbyname \
+])
+AM_CONDITIONAL(STRLCPY_IMPL, [test x"$ac_cv_func_strlcpy" = xno])
 
 AC_CACHE_CHECK(
     [for getaddrinfo],
@@ -315,7 +402,6 @@
     AC_DEFINE([HAVE_GETADDRINFO], [1], [Do we have getaddrinfo()?])
 else
 
-AC_CHECK_FUNCS([getservbyname])
 # Check for gethostbyname_r in all its glorious incompatible versions.
 #   (This is cut-and-pasted from Tor, which based its logic on
 #   Python's configure.in.)
@@ -374,8 +460,6 @@
 
 fi
 
-AC_CHECK_SIZEOF(long)
-
 AC_MSG_CHECKING(for F_SETFD in fcntl.h)
 AC_EGREP_CPP(yes,
 [
@@ -515,6 +599,26 @@
 fi
 AM_CONDITIONAL(EPOLL_BACKEND, [test "x$haveepoll" = "xyes"])
 
+AC_MSG_CHECKING(waitpid support WNOWAIT)
+AC_TRY_RUN(
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <stdlib.h>
+
+int
+main(int argc, char** argv)
+{
+    pid_t pid;
+    int status;
+    if ((pid = fork()) == 0) _exit(0);
+    _exit(waitpid(pid, &status, WNOWAIT) == -1);
+}, [AC_MSG_RESULT(yes)
+AC_DEFINE(HAVE_WAITPID_WITH_WNOWAIT, 1,
+[Define if waitpid() supports WNOWAIT])
+], AC_MSG_RESULT(no), AC_MSG_RESULT(no))
+
+
 haveeventports=no
 AC_CHECK_FUNCS(port_create, [haveeventports=yes], )
 if test "x$haveeventports" = "xyes" ; then
@@ -575,7 +679,7 @@
 #ifdef HAVE_NETDB_H
 #include <netdb.h>
 #endif
-#ifdef WIN32
+#ifdef _WIN32
 #define WIN32_WINNT 0x400
 #define _WIN32_WINNT 0x400
 #define WIN32_LEAN_AND_MEAN
@@ -598,7 +702,7 @@
 #ifdef HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
 #endif
-#ifdef WIN32
+#ifdef _WIN32
 #define WIN32_WINNT 0x400
 #define _WIN32_WINNT 0x400
 #define WIN32_LEAN_AND_MEAN
@@ -611,6 +715,14 @@
 #endif
 ])
 
+AC_CHECK_TYPES([struct so_linger],
+[#define HAVE_SO_LINGER], ,
+[
+#ifdef HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+])
+
 AC_MSG_CHECKING([for socklen_t])
 AC_TRY_COMPILE([
  #include <sys/types.h>
@@ -651,6 +763,7 @@
       #include <pthread.h> ]
   )
 fi
+AM_CONDITIONAL(THREADS, [test "$enable_thread_support" != "no"])
 AM_CONDITIONAL(PTHREADS, [test "$have_pthreads" != "no" && test "$enable_thread_support" != "no"])
 
 # check if we should compile locking into the library
@@ -671,12 +784,21 @@
         [Define if libevent should build without support for a debug mode])
 fi
 
+# check if we should enable verbose debugging 
+if test x$enable_verbose_debug = xyes; then
+	CFLAGS="$CFLAGS -DUSE_DEBUG"
+fi
+
 # check if we have and should use openssl
 AM_CONDITIONAL(OPENSSL, [test "$enable_openssl" != "no" && test "$have_openssl" = "yes"])
+if test "x$enable_openssl" = "xyes"; then
+	AC_SEARCH_LIBS([ERR_remove_thread_state], [crypto eay32],
+		[AC_DEFINE(HAVE_ERR_REMOVE_THREAD_STATE, 1, [Define to 1 if you have ERR_remove_thread_stat().])])
+fi
 
 # Add some more warnings which we use in development but not in the
 # released versions.  (Some relevant gcc versions can't handle these.)
-if test x$enable_gcc_warnings = xyes && test "$GCC" = "yes"; then
+if test x$enable_gcc_warnings != xno && test "$GCC" = "yes"; then
 
   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [
 #if !defined(__GNUC__) || (__GNUC__ < 4)
@@ -698,7 +820,11 @@
 #error
 #endif])], have_clang=yes, have_clang=no)
 
-  CFLAGS="$CFLAGS -W -Wfloat-equal -Wundef -Wpointer-arith -Wstrict-prototypes -Wmissing-prototypes -Wwrite-strings -Wredundant-decls -Wchar-subscripts -Wcomment -Wformat -Wwrite-strings -Wmissing-declarations -Wredundant-decls -Wnested-externs -Wbad-function-cast -Wswitch-enum -Werror"
+  CFLAGS="$CFLAGS -W -Wfloat-equal -Wundef -Wpointer-arith -Wstrict-prototypes -Wmissing-prototypes -Wwrite-strings -Wredundant-decls -Wchar-subscripts -Wcomment -Wformat -Wwrite-strings -Wmissing-declarations -Wredundant-decls -Wnested-externs -Wbad-function-cast -Wswitch"
+  if test x$enable_gcc_warnings = xyes; then
+    CFLAGS="$CFLAGS -Werror"
+  fi
+
   CFLAGS="$CFLAGS -Wno-unused-parameter -Wstrict-aliasing"
 
   if test x$have_gcc4 = xyes ; then
@@ -817,5 +943,5 @@
 
 AM_CONDITIONAL([INSTALL_LIBEVENT], [test "$enable_libevent_install" = "yes"])
 
-AC_CONFIG_FILES( [libevent.pc libevent_openssl.pc libevent_pthreads.pc] )
-AC_OUTPUT(Makefile include/Makefile test/Makefile sample/Makefile)
+AC_CONFIG_FILES( [libevent.pc libevent_openssl.pc libevent_pthreads.pc libevent_core.pc libevent_extra.pc] )
+AC_OUTPUT(Makefile)