nghttpx: Use SOCK_NONBLOCK and SOCK_CLOEXEC flag in socketpair to avoid race

This commit is contained in:
Tatsuhiro Tsujikawa 2014-08-13 22:09:35 +09:00
parent ca680c16e3
commit 21cbf417c8
2 changed files with 5 additions and 11 deletions

View File

@ -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);

View File

@ -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<WorkerInfo>();
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_;