changed the way to set/unset O_NONBLOCK & O_CLOEXEC

This commit is contained in:
hyung-hwan 2018-06-29 06:27:38 +00:00
parent 1d6951f22d
commit 34e5508881

View File

@ -1936,17 +1936,30 @@ hcl_server_t* hcl_server_open (hcl_mmgr_t* mmgr, hcl_oow_t xtnsize, hcl_server_p
goto oops; goto oops;
} }
#if defined(O_CLOEXEC) #if defined(O_NONBLOCK) || defined(O_CLOEXEC)
fcv = fcntl(pfd[0], F_GETFD, 0); fcv = fcntl(pfd[0], F_GETFD, 0);
if (fcv >= 0) fcntl(pfd[0], F_SETFD, fcv | O_CLOEXEC); if (fcv >= 0)
{
#if defined(O_NONBLOCK)
fcv |= O_NONBLOCK;
#endif
#if defined(O_CLOEXEC)
fcv |= O_CLOEXEC;
#endif
fcntl(pfd[0], F_SETFD, fcv);
}
fcv = fcntl(pfd[1], F_GETFD, 0); fcv = fcntl(pfd[1], F_GETFD, 0);
if (fcv >= 0) fcntl(pfd[1], F_SETFD, fcv | O_CLOEXEC); if (fcv >= 0)
#endif {
#if defined(O_NONBLOCK) #if defined(O_NONBLOCK)
fcv = fcntl(pfd[0], F_GETFL, 0); fcv |= O_NONBLOCK;
if (fcv >= 0) fcntl(pfd[0], F_SETFL, fcv | O_NONBLOCK); #endif
fcv = fcntl(pfd[1], F_GETFL, 0); #if defined(O_CLOEXEC)
if (fcv >= 0) fcntl(pfd[1], F_SETFL, fcv | O_NONBLOCK); fcv |= O_CLOEXEC;
#endif
fcntl(pfd[1], F_SETFD, fcv);
}
#endif #endif
xtn = (dummy_hcl_xtn_t*)hcl_getxtn(hcl); xtn = (dummy_hcl_xtn_t*)hcl_getxtn(hcl);
@ -2443,13 +2456,19 @@ static int setup_listeners (hcl_server_t* server, const hcl_bch_t* addrs)
optval = 1; optval = 1;
setsockopt (srv_fd, SOL_SOCKET, SO_REUSEADDR, &optval, HCL_SIZEOF(int)); setsockopt (srv_fd, SOL_SOCKET, SO_REUSEADDR, &optval, HCL_SIZEOF(int));
#if defined(O_CLOEXEC) #if defined(O_NONBLOCK) || defined(O_CLOEXEC)
fcv = fcntl(srv_fd, F_GETFD, 0); fcv = fcntl(srv_fd, F_GETFD, 0);
if (fcv >= 0) fcntl(srv_fd, F_SETFD, fcv | O_CLOEXEC); if (fcv >= 0)
#endif {
#if defined(O_NONBLOCK) #if defined(O_NONBLOCK)
fcv = fcntl(srv_fd, F_GETFL, 0); fcv |= O_NONBLOCK;
if (fcv >= 0) fcntl(srv_fd, F_SETFL, fcv | O_NONBLOCK); #endif
#if defined(O_CLOEXEC)
fcv |= O_CLOEXEC;
#endif
fcntl(srv_fd, F_SETFL, fcv);
}
#endif #endif
if (bind(srv_fd, (struct sockaddr*)&srv_addr, srv_len) == -1) if (bind(srv_fd, (struct sockaddr*)&srv_addr, srv_len) == -1)
@ -2590,9 +2609,18 @@ int hcl_server_start (hcl_server_t* server, const hcl_bch_t* addrs)
break; break;
} }
#if defined(O_CLOEXEC) #if defined(O_NONBLOCK) || defined(O_CLOEXEC)
fcv = fcntl(cli_fd, F_GETFD, 0); fcv = fcntl(cli_fd, F_GETFD, 0);
if (fcv >= 0) fcntl(cli_fd, F_SETFD, fcv | O_CLOEXEC); if (fcv >= 0)
{
#if defined(O_NONBLOCK)
fcv &= ~O_NONBLOCK; // force the accepted socket to be blocking
#endif
#if defined(O_CLOEXEC)
fcv |= O_CLOEXEC;
#endif
fcntl(cli_fd, F_SETFD, fcv);
}
#endif #endif
if (server->cfg.worker_max_count > 0) if (server->cfg.worker_max_count > 0)