From 21cbf417c83c02acd9e51aa25264d8ce31876208 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 13 Aug 2014 22:09:35 +0900 Subject: [PATCH] nghttpx: Use SOCK_NONBLOCK and SOCK_CLOEXEC flag in socketpair to avoid race --- src/shrpx_http2_session.cc | 8 +++----- src/shrpx_listen_handler.cc | 8 ++------ 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/shrpx_http2_session.cc b/src/shrpx_http2_session.cc index 54a10157..81238769 100644 --- a/src/shrpx_http2_session.cc +++ b/src/shrpx_http2_session.cc @@ -193,15 +193,13 @@ int Http2Session::init_notification() { int rv; int sockpair[2]; - rv = socketpair(AF_UNIX, SOCK_STREAM, 0, sockpair); + rv = socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, + sockpair); if(rv == -1) { SSLOG(FATAL, this) << "socketpair() failed: errno=" << errno; return -1; } - for(int i = 0; i < 2; ++i) { - evutil_make_socket_nonblocking(sockpair[i]); - evutil_make_socket_closeonexec(sockpair[i]); - } + wrbev_ = bufferevent_socket_new(evbase_, sockpair[0], BEV_OPT_CLOSE_ON_FREE| BEV_OPT_DEFER_CALLBACKS); diff --git a/src/shrpx_listen_handler.cc b/src/shrpx_listen_handler.cc index 4ce67eee..21eef4d3 100644 --- a/src/shrpx_listen_handler.cc +++ b/src/shrpx_listen_handler.cc @@ -103,17 +103,13 @@ void ListenHandler::create_worker_thread(size_t num) for(size_t i = 0; i < num; ++i) { int rv; auto info = util::make_unique(); - rv = socketpair(AF_UNIX, SOCK_STREAM, 0, info->sv); + rv = socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, + info->sv); if(rv == -1) { LLOG(ERROR, this) << "socketpair() failed: errno=" << errno; continue; } - for(int j = 0; j < 2; ++j) { - evutil_make_socket_nonblocking(info->sv[j]); - evutil_make_socket_closeonexec(info->sv[j]); - } - info->sv_ssl_ctx = sv_ssl_ctx_; info->cl_ssl_ctx = cl_ssl_ctx_;