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:
parent
8ce9ff41a0
commit
51f61a31b6
@ -32,7 +32,6 @@
|
||||
#include <sys/socket.h>
|
||||
#include <stdarg.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.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 x;
|
||||
bool fcntl_v = 0;
|
||||
int fcntl_v = 0;
|
||||
|
||||
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
|
||||
if (traits & Socket::T_NONBLOCK) type |= SOCK_NONBLOCK;
|
||||
@ -76,43 +75,43 @@ open_socket:
|
||||
if (errno == EINVAL && (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;
|
||||
}
|
||||
#endif
|
||||
this->setErrorCode (syserr_to_errnum(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
#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)
|
||||
else
|
||||
{
|
||||
int fl = fcntl(x, F_GETFL, 0);
|
||||
if (fl == -1)
|
||||
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
|
||||
if (type & (SOCK_NONBLOCK | SOCK_CLOEXEC)) goto done;
|
||||
#endif
|
||||
}
|
||||
|
||||
if (traits)
|
||||
{
|
||||
fcntl_v = fcntl(x, F_GETFL, 0);
|
||||
if (fcntl_v == -1)
|
||||
{
|
||||
fcntl_failure:
|
||||
this->setErrorCode (syserr_to_errnum(errno));
|
||||
::close (x);
|
||||
x = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (fcntl(x, F_SETFL, fl | fcntl_v) == -1) goto fcntl_failure;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
this->close ();
|
||||
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;
|
||||
}
|
||||
|
||||
done:
|
||||
this->close (); // close the existing handle if open.
|
||||
this->handle = x;
|
||||
return 0;
|
||||
}
|
||||
@ -334,16 +333,10 @@ int Socket::accept (Socket* newsck, SocketAddress* newaddr, int traits) QSE_CPP_
|
||||
return -1;
|
||||
}
|
||||
|
||||
flag_v = 0;
|
||||
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)
|
||||
if (traits)
|
||||
{
|
||||
int fl = ::fcntl(newfd, F_GETFL, 0);
|
||||
if (fl == -1)
|
||||
flag_v = ::fcntl(newfd, F_GETFL, 0);
|
||||
if (flag_v == -1)
|
||||
{
|
||||
fcntl_failure:
|
||||
this->setErrorCode (syserr_to_errnum(errno));
|
||||
@ -352,7 +345,15 @@ int Socket::accept (Socket* newsck, SocketAddress* newaddr, int traits) QSE_CPP_
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -29,7 +29,6 @@
|
||||
#include <qse/cmn/str.h>
|
||||
#include "../cmn/mem-prv.h"
|
||||
|
||||
#include <sys/epoll.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
@ -24,17 +24,24 @@ class ClientHandler
|
||||
public:
|
||||
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_ssize_t n;
|
||||
|
||||
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) break;
|
||||
if ((n = clisock->receive(bb, QSE_COUNTOF(bb))) <= 0)
|
||||
{
|
||||
qse_printf (QSE_T("%zd bytes received from %s\n"), n, addrbuf);
|
||||
break;
|
||||
}
|
||||
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;
|
||||
}
|
||||
};
|
||||
@ -49,17 +56,24 @@ static int test1 (void)
|
||||
|
||||
// workload by lambda
|
||||
([&server](QSE::Socket* clisock, QSE::SocketAddress* cliaddr) {
|
||||
qse_char_t buf[128];
|
||||
qse_char_t addrbuf[128];
|
||||
qse_uint8_t bb[256];
|
||||
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) break;
|
||||
if ((n = clisock->receive(bb, QSE_COUNTOF(bb))) <= 0)
|
||||
{
|
||||
qse_printf (QSE_T("%zd bytes received from %s\n"), n, addrbuf);
|
||||
break;
|
||||
}
|
||||
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;
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user