changed the socket module to set socket descriptors to be CLOEXEC and NONBLOCK

This commit is contained in:
hyunghwan.chung 2018-01-18 16:27:17 +00:00
parent 08cc62478b
commit 741d45cf0a
2 changed files with 65 additions and 36 deletions

View File

@ -118,8 +118,8 @@
#elif defined(MOO_SIZEOF_LONG) && (MOO_SIZEOF_LONG == 4) #elif defined(MOO_SIZEOF_LONG) && (MOO_SIZEOF_LONG == 4)
# define MOO_HAVE_UINT32_T # define MOO_HAVE_UINT32_T
# define MOO_HAVE_INT32_T # define MOO_HAVE_INT32_T
typedef unsigned long moo_uint32_t; typedef unsigned long int moo_uint32_t;
typedef signed long moo_int32_t; typedef signed long int moo_int32_t;
#elif defined(MOO_SIZEOF___INT32) && (MOO_SIZEOF___INT32 == 4) #elif defined(MOO_SIZEOF___INT32) && (MOO_SIZEOF___INT32 == 4)
# define MOO_HAVE_UINT32_T # define MOO_HAVE_UINT32_T
# define MOO_HAVE_INT32_T # define MOO_HAVE_INT32_T
@ -151,13 +151,13 @@
#elif defined(MOO_SIZEOF_LONG) && (MOO_SIZEOF_LONG == 8) #elif defined(MOO_SIZEOF_LONG) && (MOO_SIZEOF_LONG == 8)
# define MOO_HAVE_UINT64_T # define MOO_HAVE_UINT64_T
# define MOO_HAVE_INT64_T # define MOO_HAVE_INT64_T
typedef unsigned long moo_uint64_t; typedef unsigned long int moo_uint64_t;
typedef signed long moo_int64_t; typedef signed long int moo_int64_t;
#elif defined(MOO_SIZEOF_LONG_LONG) && (MOO_SIZEOF_LONG_LONG == 8) #elif defined(MOO_SIZEOF_LONG_LONG) && (MOO_SIZEOF_LONG_LONG == 8)
# define MOO_HAVE_UINT64_T # define MOO_HAVE_UINT64_T
# define MOO_HAVE_INT64_T # define MOO_HAVE_INT64_T
typedef unsigned long long moo_uint64_t; typedef unsigned long long int moo_uint64_t;
typedef signed long long moo_int64_t; typedef signed long long int moo_int64_t;
#elif defined(MOO_SIZEOF___INT64) && (MOO_SIZEOF___INT64 == 8) #elif defined(MOO_SIZEOF___INT64) && (MOO_SIZEOF___INT64 == 8)
# define MOO_HAVE_UINT64_T # define MOO_HAVE_UINT64_T
# define MOO_HAVE_INT64_T # define MOO_HAVE_INT64_T
@ -181,13 +181,13 @@
#elif defined(MOO_SIZEOF_LONG) && (MOO_SIZEOF_LONG == 16) #elif defined(MOO_SIZEOF_LONG) && (MOO_SIZEOF_LONG == 16)
# define MOO_HAVE_UINT128_T # define MOO_HAVE_UINT128_T
# define MOO_HAVE_INT128_T # define MOO_HAVE_INT128_T
typedef unsigned long moo_uint128_t; typedef unsigned long int moo_uint128_t;
typedef signed long moo_int128_t; typedef signed long int moo_int128_t;
#elif defined(MOO_SIZEOF_LONG_LONG) && (MOO_SIZEOF_LONG_LONG == 16) #elif defined(MOO_SIZEOF_LONG_LONG) && (MOO_SIZEOF_LONG_LONG == 16)
# define MOO_HAVE_UINT128_T # define MOO_HAVE_UINT128_T
# define MOO_HAVE_INT128_T # define MOO_HAVE_INT128_T
typedef unsigned long long moo_uint128_t; typedef unsigned long long int moo_uint128_t;
typedef signed long long moo_int128_t; typedef signed long long int moo_int128_t;
#elif defined(MOO_SIZEOF___INT128) && (MOO_SIZEOF___INT128 == 16) #elif defined(MOO_SIZEOF___INT128) && (MOO_SIZEOF___INT128 == 16)
# define MOO_HAVE_UINT128_T # define MOO_HAVE_UINT128_T
# define MOO_HAVE_INT128_T # define MOO_HAVE_INT128_T

View File

@ -26,6 +26,10 @@
#include "_sck.h" #include "_sck.h"
#if defined(HAVE_ACCEPT4)
# define _GNU_SOURCE
#endif
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
@ -40,8 +44,7 @@ static moo_pfrc_t pf_open_socket (moo_t* moo, moo_ooi_t nargs)
{ {
oop_sck_t sck; oop_sck_t sck;
moo_oop_t dom, type, proto; moo_oop_t dom, type, proto;
int fd = -1; int fd = -1, typev;
moo_errnum_t errnum;
sck = (oop_sck_t)MOO_STACK_GETRCV(moo, nargs); sck = (oop_sck_t)MOO_STACK_GETRCV(moo, nargs);
MOO_PF_CHECK_RCV (moo, MOO_PF_CHECK_RCV (moo,
@ -55,9 +58,21 @@ static moo_pfrc_t pf_open_socket (moo_t* moo, moo_ooi_t nargs)
MOO_PF_CHECK_ARGS (moo, nargs, MOO_OOP_IS_SMOOI(dom) && MOO_OOP_IS_SMOOI(type) && MOO_OOP_IS_SMOOI(proto)); MOO_PF_CHECK_ARGS (moo, nargs, MOO_OOP_IS_SMOOI(dom) && MOO_OOP_IS_SMOOI(type) && MOO_OOP_IS_SMOOI(proto));
fd = socket (MOO_OOP_TO_SMOOI(dom), MOO_OOP_TO_SMOOI(type), MOO_OOP_TO_SMOOI(proto)); typev = MOO_OOP_TO_SMOOI(type);
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
typev |= SOCK_NONBLOCK | SOCK_CLOEXEC;
create_socket:
#endif
fd = socket (MOO_OOP_TO_SMOOI(dom), typev, MOO_OOP_TO_SMOOI(proto));
if (fd == -1) if (fd == -1)
{ {
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
if (errno == EINVAL && (typev & (SOCK_NONBLOCK | SOCK_CLOEXEC)))
{
typev &= ~(SOCK_NONBLOCK | SOCK_CLOEXEC);
goto create_socket;
}
#endif
moo_seterrwithsyserr (moo, errno); moo_seterrwithsyserr (moo, errno);
goto oops; goto oops;
} }
@ -69,6 +84,20 @@ static moo_pfrc_t pf_open_socket (moo_t* moo, moo_ooi_t nargs)
goto oops; goto oops;
} }
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
if (!(typev & (SOCK_NONBLOCK | SOCK_CLOEXEC)))
#endif
{
int oldfl;
oldfl = fcntl(fd, F_GETFL, 0);
if (oldfl == -1 || fcntl(fd, F_SETFL, oldfl | O_CLOEXEC | O_NONBLOCK) == -1)
{
moo_seterrwithsyserr (moo, errno);
goto oops;
}
}
sck->handle = MOO_SMOOI_TO_OOP(fd); sck->handle = MOO_SMOOI_TO_OOP(fd);
MOO_STACK_SETRETTORCV (moo, nargs); MOO_STACK_SETRETTORCV (moo, nargs);
@ -171,21 +200,34 @@ static moo_pfrc_t pf_accept_socket (moo_t* moo, moo_ooi_t nargs)
addrlen = MOO_OBJ_GET_SIZE(arg); addrlen = MOO_OBJ_GET_SIZE(arg);
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC) && defined(HAVE_ACCEPT4) #if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC) && defined(HAVE_ACCEPT4)
newfd = accept4(fd, (struct sockaddr*)MOO_OBJ_GET_BYTE_SLOT(arg), &addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC); newfd = accept4(fd, (struct sockaddr*)MOO_OBJ_GET_BYTE_SLOT(arg), &addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
#else if (newfd == -1)
oldfl = fcntl(fd, F_GETFL, 0);
if (oldfl == -1 || fcntl(fd, F_SETFL, oldfl | O_NONBLOCK | O_CLOEXEC) == -1)
{ {
moo_seterrwithsyserr (moo, errno); if (errno != ENOSYS)
return MOO_PF_FAILURE; {
moo_seterrwithsyserr (moo, errno);
return MOO_PF_FAILURE;
}
}
else
{
goto accept_done;
} }
newfd = accept(fd, (struct sockaddr*)MOO_OBJ_GET_BYTE_SLOT(arg), &addrlen);
#endif #endif
newfd = accept(fd, (struct sockaddr*)MOO_OBJ_GET_BYTE_SLOT(arg), &addrlen);
if (newfd == -1) if (newfd == -1)
{ {
moo_seterrwithsyserr (moo, errno); moo_seterrwithsyserr (moo, errno);
return MOO_PF_FAILURE; return MOO_PF_FAILURE;
} }
oldfl = fcntl(newfd, F_GETFL, 0);
if (oldfl == -1 || fcntl(newfd, F_SETFL, oldfl | O_NONBLOCK | O_CLOEXEC) == -1)
{
moo_seterrwithsyserr (moo, errno);
close (newfd);
return MOO_PF_FAILURE;
}
accept_done:
newsck = (oop_sck_t)moo_instantiate (moo, MOO_OBJ_GET_CLASS(sck), MOO_NULL, 0); newsck = (oop_sck_t)moo_instantiate (moo, MOO_OBJ_GET_CLASS(sck), MOO_NULL, 0);
if (!newsck) if (!newsck)
{ {
@ -243,8 +285,7 @@ static moo_pfrc_t pf_listen_socket (moo_t* moo, moo_ooi_t nargs)
static moo_pfrc_t pf_connect (moo_t* moo, moo_ooi_t nargs) static moo_pfrc_t pf_connect (moo_t* moo, moo_ooi_t nargs)
{ {
oop_sck_t sck; oop_sck_t sck;
int fd, oldfl, n; int fd, n;
moo_errnum_t errnum;
moo_oop_t arg; moo_oop_t arg;
sck = (oop_sck_t)MOO_STACK_GETRCV(moo, nargs); sck = (oop_sck_t)MOO_STACK_GETRCV(moo, nargs);
@ -263,32 +304,20 @@ static moo_pfrc_t pf_connect (moo_t* moo, moo_ooi_t nargs)
return MOO_PF_FAILURE; return MOO_PF_FAILURE;
} }
oldfl = fcntl(fd, F_GETFL, 0);
if (oldfl == -1 || fcntl(fd, F_SETFL, oldfl | O_NONBLOCK) == -1) goto oops_syserr;
do do
{ {
n = connect(fd, (struct sockaddr*)MOO_OBJ_GET_BYTE_SLOT(arg), moo_sck_addr_len((sck_addr_t*)MOO_OBJ_GET_BYTE_SLOT(arg))); n = connect(fd, (struct sockaddr*)MOO_OBJ_GET_BYTE_SLOT(arg), moo_sck_addr_len((sck_addr_t*)MOO_OBJ_GET_BYTE_SLOT(arg)));
} }
while (n == -1 && errno == EINTR); while (n == -1 && errno == EINTR);
if (n == -1 && errno != EINPROGRESS) if (n == -1 && errno != EINPROGRESS)
{ {
fcntl (fd, F_SETFL, oldfl); moo_seterrwithsyserr (moo, errno);
goto oops_syserr; return MOO_PF_FAILURE;
} }
MOO_STACK_SETRETTORCV (moo, nargs); MOO_STACK_SETRETTORCV (moo, nargs);
return MOO_PF_SUCCESS; return MOO_PF_SUCCESS;
oops_syserr:
moo_seterrwithsyserr (moo, errno);
return MOO_PF_FAILURE;
oops:
moo_seterrnum (moo, errnum);
return MOO_PF_FAILURE;
} }
static moo_pfrc_t pf_get_socket_error (moo_t* moo, moo_ooi_t nargs) static moo_pfrc_t pf_get_socket_error (moo_t* moo, moo_ooi_t nargs)