Skip to content

Commit

Permalink
HevTaskIOSocket: Use SOCK_NONBLOCK to set non-blocking mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
heiher committed Feb 6, 2025
1 parent be9b761 commit 4ab1c9a
Showing 1 changed file with 46 additions and 20 deletions.
66 changes: 46 additions & 20 deletions src/lib/io/socket/hev-task-io-socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
============================================================================
*/

#define _GNU_SOURCE
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
Expand All @@ -21,16 +22,24 @@
EXPORT_SYMBOL int
hev_task_io_socket_socket (int domain, int type, int protocol)
{
int fd, nonblock = 1;
int fd;

#ifdef SOCK_NONBLOCK
type |= SOCK_NONBLOCK;
#endif

fd = socket (domain, type, protocol);
if (0 > fd)
return fd;

if (0 > ioctl (fd, FIONBIO, (char *)&nonblock)) {
close (fd);
return -2;
#ifndef SOCK_NONBLOCK
if (fd >= 0) {
int nonblock = 1;

if (ioctl (fd, FIONBIO, (char *)&nonblock) < 0) {
close (fd);
return -2;
}
}
#endif

return fd;
}
Expand All @@ -39,24 +48,33 @@ EXPORT_SYMBOL int
hev_task_io_socket_socketpair (int domain, int type, int protocol,
int socket_vector[2])
{
int nonblock = 1;
int res;

if (0 > socketpair (domain, type, protocol, socket_vector))
return -1;
#ifdef SOCK_NONBLOCK
type |= SOCK_NONBLOCK;
#endif

if (0 > ioctl (socket_vector[0], FIONBIO, (char *)&nonblock)) {
close (socket_vector[0]);
close (socket_vector[1]);
return -2;
}
res = socketpair (domain, type, protocol, socket_vector);

#ifndef SOCK_NONBLOCK
if (res >= 0) {
int nonblock = 1;

if (0 > ioctl (socket_vector[1], FIONBIO, (char *)&nonblock)) {
close (socket_vector[0]);
close (socket_vector[1]);
return -3;
if (ioctl (socket_vector[0], FIONBIO, (char *)&nonblock) < 0) {
close (socket_vector[0]);
close (socket_vector[1]);
return -2;
}

if (ioctl (socket_vector[1], FIONBIO, (char *)&nonblock) < 0) {
close (socket_vector[0]);
close (socket_vector[1]);
return -3;
}
}
#endif

return 0;
return res;
}

EXPORT_SYMBOL int
Expand Down Expand Up @@ -88,10 +106,14 @@ EXPORT_SYMBOL int
hev_task_io_socket_accept (int fd, struct sockaddr *addr, socklen_t *addr_len,
HevTaskIOYielder yielder, void *yielder_data)
{
int new_fd, nonblock = 1;
int new_fd;

retry:
#ifndef SOCK_NONBLOCK
new_fd = accept (fd, addr, addr_len);
#else
new_fd = accept4 (fd, addr, addr_len, SOCK_NONBLOCK);
#endif
if (new_fd < 0 && errno == EAGAIN) {
if (yielder) {
if (yielder (HEV_TASK_WAITIO, yielder_data))
Expand All @@ -102,12 +124,16 @@ hev_task_io_socket_accept (int fd, struct sockaddr *addr, socklen_t *addr_len,
goto retry;
}

#ifndef SOCK_NONBLOCK
if (new_fd >= 0) {
int nonblock = 1;

if (ioctl (new_fd, FIONBIO, (char *)&nonblock) < 0) {
close (new_fd);
return -3;
}
}
#endif

return new_fd;
}
Expand Down

0 comments on commit 4ab1c9a

Please sign in to comment.