Skip to content

Commit f8cdaa2

Browse files
committed
Disable epoll and poll automatically.
Disable epoll and poll functions if the system doesn't support them
1 parent d20ae8d commit f8cdaa2

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed

configure.ac

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,83 @@ if test x"$fastopen" = x"yes"; then
133133
fi
134134
fi
135135

136+
AC_ARG_ENABLE([[poll]],
137+
[AS_HELP_STRING([[--enable-poll[=ARG]]], [enable poll support (yes, no, auto) [auto]])],
138+
[enable_poll=${enableval}],
139+
[enable_poll='auto']
140+
)
141+
142+
if test "$enable_poll" != "no"; then
143+
if test "$os_is_native_w32" != "yes"; then
144+
AC_CHECK_HEADERS([poll.h],
145+
[
146+
AC_CHECK_FUNCS([poll], [have_poll='yes'], [have_poll='no'])
147+
], [], [AC_INCLUDES_DEFAULT])
148+
else
149+
AC_MSG_CHECKING([for WSAPoll()])
150+
AC_LINK_IFELSE([
151+
AC_LANG_PROGRAM([[
152+
#include <winsock2.h>
153+
]], [[
154+
WSAPOLLFD fda[2];
155+
WSAPoll(fda, 2, 0);]])],
156+
[
157+
have_poll='yes'
158+
AC_DEFINE([HAVE_POLL],[1])
159+
], [have_poll='no'])
160+
AC_MSG_RESULT([$have_poll])
161+
fi
162+
if test "$enable_poll" = "yes" && test "$have_poll" != "yes"; then
163+
AC_MSG_ERROR([[Support for poll was explicitly requested but cannot be enabled on this platform.]])
164+
fi
165+
enable_poll="$have_poll"
166+
fi
167+
168+
if test x"$enable_poll" = x"yes"; then
169+
AM_CXXFLAGS="$AM_CXXFLAGS -DENABLE_POLL"
170+
AM_CFLAGS="$AM_CXXFLAGS -DENABLE_POLL"
171+
fi
172+
173+
AC_ARG_ENABLE([[epoll]],
174+
[AS_HELP_STRING([[--enable-epoll[=ARG]]], [enable epoll support (yes, no, auto) [auto]])],
175+
[enable_epoll=${enableval}],
176+
[enable_epoll='auto']
177+
)
178+
179+
if test "$enable_epoll" != "no"; then
180+
AX_HAVE_EPOLL
181+
if test "${ax_cv_have_epoll}" = "yes"; then
182+
AC_DEFINE([[EPOLL_SUPPORT]],[[1]],[Define to 1 to enable epoll support])
183+
enable_epoll='yes'
184+
else
185+
if test "$enable_epoll" = "yes"; then
186+
AC_MSG_ERROR([[Support for epoll was explicitly requested but cannot be enabled on this platform.]])
187+
fi
188+
enable_epoll='no'
189+
fi
190+
fi
191+
192+
AM_CONDITIONAL([MHD_HAVE_EPOLL], [[test "x$enable_epoll" = xyes]])
193+
194+
if test "x$enable_epoll" = "xyes"; then
195+
AC_CACHE_CHECK([for epoll_create1()], [mhd_cv_have_epoll_create1], [
196+
AC_LINK_IFELSE([
197+
AC_LANG_PROGRAM([[
198+
#include <sys/epoll.h>
199+
]], [[
200+
int fd;
201+
fd = epoll_create1(EPOLL_CLOEXEC);]])],
202+
[mhd_cv_have_epoll_create1=yes],
203+
[mhd_cv_have_epoll_create1=no])])
204+
AS_IF([test "x$mhd_cv_have_epoll_create1" = "xyes"],[
205+
AC_DEFINE([[HAVE_EPOLL_CREATE1]], [[1]], [Define if you have epoll_create1 function.])])
206+
fi
207+
208+
if test x"$enable_epoll" = x"yes"; then
209+
AM_CXXFLAGS="$AM_CXXFLAGS -DENABLE_EPOLL"
210+
AM_CFLAGS="$AM_CXXFLAGS -DENABLE_EPOLL"
211+
fi
212+
136213
AC_MSG_CHECKING([whether to link statically])
137214
AC_ARG_ENABLE([static],
138215
[AS_HELP_STRING([--enable-static],
@@ -235,5 +312,7 @@ AC_MSG_NOTICE([Configuration Summary:
235312
Debug : ${debugit}
236313
TLS Enabled : ${have_gnutls}
237314
TCP_FASTOPEN : ${is_fastopen_supported}
315+
poll support : ${enable_poll=no}
316+
epoll support : ${enable_epoll=no}
238317
Static : ${static}
239318
])

m4/ax_have_epoll.m4

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# ===========================================================================
2+
# https://www.gnu.org/software/autoconf-archive/ax_have_epoll.html
3+
# ===========================================================================
4+
#
5+
# SYNOPSIS
6+
#
7+
# AX_HAVE_EPOLL([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
8+
# AX_HAVE_EPOLL_PWAIT([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
9+
#
10+
# DESCRIPTION
11+
#
12+
# This macro determines whether the system supports the epoll I/O event
13+
# interface. A neat usage example would be:
14+
#
15+
# AX_HAVE_EPOLL(
16+
# [AX_CONFIG_FEATURE_ENABLE(epoll)],
17+
# [AX_CONFIG_FEATURE_DISABLE(epoll)])
18+
# AX_CONFIG_FEATURE(
19+
# [epoll], [This platform supports epoll(7)],
20+
# [HAVE_EPOLL], [This platform supports epoll(7).])
21+
#
22+
# The epoll interface was added to the Linux kernel in version 2.5.45, and
23+
# the macro verifies that a kernel newer than this is installed. This
24+
# check is somewhat unreliable if <linux/version.h> doesn't match the
25+
# running kernel, but it is necessary regardless, because glibc comes with
26+
# stubs for the epoll_create(), epoll_wait(), etc. that allow programs to
27+
# compile and link even if the kernel is too old; the problem would then
28+
# be detected only at runtime.
29+
#
30+
# Linux kernel version 2.6.19 adds the epoll_pwait() call in addition to
31+
# epoll_wait(). The availability of that function can be tested with the
32+
# second macro. Generally speaking, it is safe to assume that
33+
# AX_HAVE_EPOLL would succeed if AX_HAVE_EPOLL_PWAIT has, but not the
34+
# other way round.
35+
#
36+
# LICENSE
37+
#
38+
# Copyright (c) 2008 Peter Simons <simons@cryp.to>
39+
#
40+
# Copying and distribution of this file, with or without modification, are
41+
# permitted in any medium without royalty provided the copyright notice
42+
# and this notice are preserved. This file is offered as-is, without any
43+
# warranty.
44+
45+
#serial 11
46+
47+
AC_DEFUN([AX_HAVE_EPOLL], [dnl
48+
ax_have_epoll_cppflags="${CPPFLAGS}"
49+
AC_CHECK_HEADER([linux/version.h], [CPPFLAGS="${CPPFLAGS} -DHAVE_LINUX_VERSION_H"])
50+
AC_MSG_CHECKING([for Linux epoll(7) interface])
51+
AC_CACHE_VAL([ax_cv_have_epoll], [dnl
52+
AC_LINK_IFELSE([dnl
53+
AC_LANG_PROGRAM([dnl
54+
#include <sys/epoll.h>
55+
#ifdef HAVE_LINUX_VERSION_H
56+
# include <linux/version.h>
57+
# if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,45)
58+
# error linux kernel version is too old to have epoll
59+
# endif
60+
#endif
61+
], [dnl
62+
int fd, rc;
63+
struct epoll_event ev;
64+
fd = epoll_create(128);
65+
rc = epoll_wait(fd, &ev, 1, 0);])],
66+
[ax_cv_have_epoll=yes],
67+
[ax_cv_have_epoll=no])])
68+
CPPFLAGS="${ax_have_epoll_cppflags}"
69+
AS_IF([test "${ax_cv_have_epoll}" = "yes"],
70+
[AC_MSG_RESULT([yes])
71+
$1],[AC_MSG_RESULT([no])
72+
$2])
73+
])dnl
74+
75+
AC_DEFUN([AX_HAVE_EPOLL_PWAIT], [dnl
76+
ax_have_epoll_cppflags="${CPPFLAGS}"
77+
AC_CHECK_HEADER([linux/version.h],
78+
[CPPFLAGS="${CPPFLAGS} -DHAVE_LINUX_VERSION_H"])
79+
AC_MSG_CHECKING([for Linux epoll(7) interface with signals extension])
80+
AC_CACHE_VAL([ax_cv_have_epoll_pwait], [dnl
81+
AC_LINK_IFELSE([dnl
82+
AC_LANG_PROGRAM([dnl
83+
#ifdef HAVE_LINUX_VERSION_H
84+
# include <linux/version.h>
85+
# if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
86+
# error linux kernel version is too old to have epoll_pwait
87+
# endif
88+
#endif
89+
#include <sys/epoll.h>
90+
#include <signal.h>
91+
], [dnl
92+
int fd, rc;
93+
struct epoll_event ev;
94+
fd = epoll_create(128);
95+
rc = epoll_wait(fd, &ev, 1, 0);
96+
rc = epoll_pwait(fd, &ev, 1, 0, (sigset_t const *)(0));])],
97+
[ax_cv_have_epoll_pwait=yes],
98+
[ax_cv_have_epoll_pwait=no])])
99+
CPPFLAGS="${ax_have_epoll_cppflags}"
100+
AS_IF([test "${ax_cv_have_epoll_pwait}" = "yes"],
101+
[AC_MSG_RESULT([yes])
102+
$1],[AC_MSG_RESULT([no])
103+
$2])
104+
])dnl

src/httpserver/http_utils.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,23 @@ class http_utils
7777
enum start_method_T
7878
{
7979
#if defined(__MINGW32__) || defined(__CYGWIN32__)
80+
#ifdef ENABLE_POLL
8081
INTERNAL_SELECT = MHD_USE_SELECT_INTERNALLY | MHD_USE_POLL,
82+
#else
83+
INTERNAL_SELECT = MHD_USE_SELECT_INTERNALLY,
84+
#endif
8185
#else
86+
#ifdef ENABLE_EPOLL
8287
INTERNAL_SELECT = MHD_USE_SELECT_INTERNALLY | MHD_USE_EPOLL | MHD_USE_EPOLL_TURBO,
88+
#else
89+
INTERNAL_SELECT = MHD_USE_SELECT_INTERNALLY,
90+
#endif
8391
#endif
92+
#ifdef ENABLE_POLL
8493
THREAD_PER_CONNECTION = MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL
94+
#else
95+
THREAD_PER_CONNECTION = MHD_USE_THREAD_PER_CONNECTION
96+
#endif
8597
};
8698

8799
enum policy_T

0 commit comments

Comments
 (0)