nghttpd: Make use of accept4

This commit is contained in:
Tatsuhiro Tsujikawa 2015-01-05 15:59:51 +09:00
parent e253d8f6db
commit fcf0ceeac6
3 changed files with 20 additions and 17 deletions

View File

@ -520,6 +520,7 @@ if test "x$cross_compiling" != "xyes"; then
fi fi
AC_CHECK_FUNCS([ \ AC_CHECK_FUNCS([ \
_Exit \ _Exit \
accept4 \
getpwnam \ getpwnam \
memmove \ memmove \
memset \ memset \

View File

@ -64,18 +64,6 @@ const std::string DEFAULT_HTML = "index.html";
const std::string NGHTTPD_SERVER = "nghttpd nghttp2/" NGHTTP2_VERSION; const std::string NGHTTPD_SERVER = "nghttpd nghttp2/" NGHTTP2_VERSION;
} // namespace } // namespace
namespace {
int make_socket_nonblocking(int fd) {
int flags;
int rv;
while ((flags = fcntl(fd, F_GETFL, 0)) == -1 && errno == EINTR)
;
while ((rv = fcntl(fd, F_SETFL, flags | O_NONBLOCK)) == -1 && errno == EINTR)
;
return rv;
}
} // namespace
namespace { namespace {
void delete_handler(Http2Handler *handler) { void delete_handler(Http2Handler *handler) {
handler->remove_self(); handler->remove_self();
@ -235,9 +223,7 @@ public:
} }
const nghttp2_session_callbacks *get_callbacks() const { return callbacks_; } const nghttp2_session_callbacks *get_callbacks() const { return callbacks_; }
void accept_connection(int fd) { void accept_connection(int fd) {
int val = 1; util::make_socket_nodelay(fd);
(void)setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
reinterpret_cast<char *>(&val), sizeof(val));
SSL *ssl = nullptr; SSL *ssl = nullptr;
if (ssl_ctx_) { if (ssl_ctx_) {
ssl = ssl_session_new(fd); ssl = ssl_session_new(fd);
@ -1397,14 +1383,20 @@ public:
} }
void accept_connection() { void accept_connection() {
for (;;) { for (;;) {
#ifdef HAVE_ACCEPT4
auto fd = accept4(fd_, nullptr, nullptr, SOCK_NONBLOCK);
#else // !HAVE_ACCEPT4
auto fd = accept(fd_, nullptr, nullptr); auto fd = accept(fd_, nullptr, nullptr);
#endif // !HAVE_ACCEPT4
if (fd == -1) { if (fd == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
break; break;
} }
continue; continue;
} }
make_socket_nonblocking(fd); #ifndef HAVE_ACCEPT4
util::make_socket_nonblocking(fd);
#endif // !HAVE_ACCEPT4
acceptor_->accept_connection(fd); acceptor_->accept_connection(fd);
} }
} }
@ -1478,7 +1470,7 @@ int start_listen(struct ev_loop *loop, Sessions *sessions,
close(fd); close(fd);
continue; continue;
} }
make_socket_nonblocking(fd); util::make_socket_nonblocking(fd);
#ifdef IPV6_V6ONLY #ifdef IPV6_V6ONLY
if (rp->ai_family == AF_INET6) { if (rp->ai_family == AF_INET6) {
if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val,

View File

@ -839,6 +839,16 @@ int make_socket_closeonexec(int fd) {
return rv; return rv;
} }
int make_socket_nonblocking(int fd) {
int flags;
int rv;
while ((flags = fcntl(fd, F_GETFL, 0)) == -1 && errno == EINTR)
;
while ((rv = fcntl(fd, F_SETFL, flags | O_NONBLOCK)) == -1 && errno == EINTR)
;
return rv;
}
int make_socket_nodelay(int fd) { int make_socket_nodelay(int fd) {
int val = 1; int val = 1;
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&val), if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&val),