on netbsd, the accepted socket inherited O_NONBLOCK if the accepting socket has it. i made changes to prevent this

This commit is contained in:
hyung-hwan 2018-06-29 04:53:28 +00:00
parent 8ce9ff41a0
commit 51f61a31b6
3 changed files with 60 additions and 46 deletions

View File

@ -32,7 +32,6 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <stdarg.h> #include <stdarg.h>
#include <errno.h> #include <errno.h>
#include <string.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
@ -62,7 +61,7 @@ int Socket::fdopen (int handle) QSE_CPP_NOEXCEPT
int Socket::open (int domain, int type, int protocol, int traits) QSE_CPP_NOEXCEPT int Socket::open (int domain, int type, int protocol, int traits) QSE_CPP_NOEXCEPT
{ {
int x; int x;
bool fcntl_v = 0; int fcntl_v = 0;
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC) #if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
if (traits & Socket::T_NONBLOCK) type |= SOCK_NONBLOCK; if (traits & Socket::T_NONBLOCK) type |= SOCK_NONBLOCK;
@ -76,43 +75,43 @@ open_socket:
if (errno == EINVAL && (type & (SOCK_NONBLOCK | SOCK_CLOEXEC))) if (errno == EINVAL && (type & (SOCK_NONBLOCK | SOCK_CLOEXEC)))
{ {
type &= ~(SOCK_NONBLOCK | SOCK_CLOEXEC); type &= ~(SOCK_NONBLOCK | SOCK_CLOEXEC);
if (traits & Socket::T_NONBLOCK ) fcntl_v |= O_NONBLOCK;
#if defined(O_CLOEXEC)
if (traits & Socket::T_CLOEXEC) fcntl_v |= O_CLOEXEC;
#endif
goto open_socket; goto open_socket;
} }
#endif #endif
this->setErrorCode (syserr_to_errnum(errno)); this->setErrorCode (syserr_to_errnum(errno));
return -1; return -1;
} }
else
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
// do nothing
#else
if (traits & Socket::T_NONBLOCK ) fcntl_v |= O_NONBLOCK;
#if defined(O_CLOEXEC)
if (traits & O_CLOEXEC) fcntl_v |= O_CLOEXEC;
#endif
#endif
if (fcntl_v)
{ {
int fl = fcntl(x, F_GETFL, 0); #if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
if (fl == -1) if (type & (SOCK_NONBLOCK | SOCK_CLOEXEC)) goto done;
#endif
}
if (traits)
{
fcntl_v = fcntl(x, F_GETFL, 0);
if (fcntl_v == -1)
{ {
fcntl_failure: fcntl_failure:
this->setErrorCode (syserr_to_errnum(errno)); this->setErrorCode (syserr_to_errnum(errno));
::close (x); ::close (x);
x = -1; return -1;
}
else
{
if (fcntl(x, F_SETFL, fl | fcntl_v) == -1) goto fcntl_failure;
} }
if (traits & Socket::T_NONBLOCK) fcntl_v |= O_NONBLOCK;
else fcntl_v &= ~O_NONBLOCK;
#if defined(O_CLOEXEC)
if (traits & Socket::T_CLOEXEC) fcntl_v |= O_CLOEXEC;
else fcntl_v &= ~O_CLOEXEC;
#endif
if (fcntl(x, F_SETFL, fcntl_v) == -1) goto fcntl_failure;
} }
this->close (); done:
this->close (); // close the existing handle if open.
this->handle = x; this->handle = x;
return 0; return 0;
} }
@ -334,16 +333,10 @@ int Socket::accept (Socket* newsck, SocketAddress* newaddr, int traits) QSE_CPP_
return -1; return -1;
} }
flag_v = 0; if (traits)
if (traits & Socket::T_NONBLOCK ) flag_v |= O_NONBLOCK;
#if defined(O_CLOEXEC)
if (traits & Socket::T_CLOEXEC) flag_v |= O_CLOEXEC;
#endif
if (flag_v)
{ {
int fl = ::fcntl(newfd, F_GETFL, 0); flag_v = ::fcntl(newfd, F_GETFL, 0);
if (fl == -1) if (flag_v == -1)
{ {
fcntl_failure: fcntl_failure:
this->setErrorCode (syserr_to_errnum(errno)); this->setErrorCode (syserr_to_errnum(errno));
@ -352,7 +345,15 @@ int Socket::accept (Socket* newsck, SocketAddress* newaddr, int traits) QSE_CPP_
} }
else else
{ {
if (::fcntl(newfd, F_SETFL, fl | flag_v) == -1) goto fcntl_failure; if (traits & Socket::T_NONBLOCK) flag_v |= O_NONBLOCK;
else flag_v &= ~O_NONBLOCK;
#if defined(O_CLOEXEC)
if (traits & Socket::T_CLOEXEC) flag_v |= O_CLOEXEC;
else flag_v &= ~O_CLOEXEC;
#endif
if (::fcntl(newfd, F_SETFL, flag_v) == -1) goto fcntl_failure;
} }
} }

View File

@ -29,7 +29,6 @@
#include <qse/cmn/str.h> #include <qse/cmn/str.h>
#include "../cmn/mem-prv.h" #include "../cmn/mem-prv.h"
#include <sys/epoll.h>
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>

View File

@ -24,17 +24,24 @@ class ClientHandler
public: public:
int operator() (QSE::TcpServer* server, QSE::Socket* clisock, QSE::SocketAddress* cliaddr) int operator() (QSE::TcpServer* server, QSE::Socket* clisock, QSE::SocketAddress* cliaddr)
{ {
qse_char_t buf[128]; qse_char_t addrbuf[128];
qse_uint8_t bb[256]; qse_uint8_t bb[256];
qse_ssize_t n; qse_ssize_t n;
cliaddr->toStrBuf(addrbuf, QSE_COUNTOF(addrbuf));
qse_printf (QSE_T("hello word..from %s\n"), addrbuf);
while (!server->isStopRequested()) while (!server->isStopRequested())
{ {
qse_printf (QSE_T("hello word..from %s\n"), cliaddr->toStrBuf(buf, QSE_COUNTOF(buf))); if ((n = clisock->receive(bb, QSE_COUNTOF(bb))) <= 0)
if ((n = clisock->receive(bb, QSE_COUNTOF(bb))) <= 0) break; {
qse_printf (QSE_T("%zd bytes received from %s\n"), n, addrbuf);
break;
}
clisock->send (bb, n); clisock->send (bb, n);
} }
qse_printf (QSE_T("bye..to %s\n"), cliaddr->toStrBuf(buf, QSE_COUNTOF(buf)));
qse_printf (QSE_T("byte to %s\n"), addrbuf);
return 0; return 0;
} }
}; };
@ -49,17 +56,24 @@ static int test1 (void)
// workload by lambda // workload by lambda
([&server](QSE::Socket* clisock, QSE::SocketAddress* cliaddr) { ([&server](QSE::Socket* clisock, QSE::SocketAddress* cliaddr) {
qse_char_t buf[128]; qse_char_t addrbuf[128];
qse_uint8_t bb[256]; qse_uint8_t bb[256];
qse_ssize_t n; qse_ssize_t n;
while (!server.isStopRequested()) cliaddr->toStrBuf(addrbuf, QSE_COUNTOF(addrbuf));
qse_printf (QSE_T("hello word..from %s\n"), addrbuf);
while (!server->isStopRequested())
{ {
qse_printf (QSE_T("hello word..from %s\n"), cliaddr->toStrBuf(buf, QSE_COUNTOF(buf))); if ((n = clisock->receive(bb, QSE_COUNTOF(bb))) <= 0)
if ((n = clisock->receive(bb, QSE_COUNTOF(bb))) <= 0) break; {
qse_printf (QSE_T("%zd bytes received from %s\n"), n, addrbuf);
break;
}
clisock->send (bb, n); clisock->send (bb, n);
} }
qse_printf (QSE_T("bye..to %s\n"), cliaddr->toStrBuf(buf, QSE_COUNTOF(buf)));
qse_printf (QSE_T("byte to %s\n"), addrbuf);
return 0; return 0;
}) })