diff --git a/src/lib/libsockfs.js b/src/lib/libsockfs.js index 7ada365896fdc..10cf2afc65629 100644 --- a/src/lib/libsockfs.js +++ b/src/lib/libsockfs.js @@ -421,6 +421,14 @@ addToLibrary({ } {{{ makeSetValue('arg', '0', 'bytes', 'i32') }}}; return 0; + case {{{ cDefs.FIONBIO }}}: + var on = {{{ makeGetValue('arg', '0', 'i32') }}}; + if (on) { + sock.stream.flags |= {{{ cDefs.O_NONBLOCK }}}; + } else { + sock.stream.flags &= ~{{{ cDefs.O_NONBLOCK }}}; + } + return 0; default: return {{{ cDefs.EINVAL }}}; } diff --git a/src/lib/libsyscall.js b/src/lib/libsyscall.js index 6e11512f27e18..61af4001bd53f 100644 --- a/src/lib/libsyscall.js +++ b/src/lib/libsyscall.js @@ -284,6 +284,7 @@ var SyscallsLibrary = { if (!stream.tty) return -{{{ cDefs.ENOTTY }}}; return -{{{ cDefs.EINVAL }}}; // not supported } + case {{{ cDefs.FIONBIO }}}: case {{{ cDefs.FIONREAD }}}: { var argp = syscallGetVarargP(); return FS.ioctl(stream, op, argp); diff --git a/src/struct_info.json b/src/struct_info.json index 4a6f2a812a25f..e94e38326b46e 100644 --- a/src/struct_info.json +++ b/src/struct_info.json @@ -312,6 +312,7 @@ { "file": "bits/ioctl.h", "defines": [ + "FIONBIO", "FIONREAD", "TCGETA", "TCGETS", diff --git a/src/struct_info_generated.json b/src/struct_info_generated.json index 3595af1b6d7a5..a31f2e572669e 100644 --- a/src/struct_info_generated.json +++ b/src/struct_info_generated.json @@ -292,6 +292,7 @@ "EWOULDBLOCK": 6, "EXDEV": 75, "EXFULL": 115, + "FIONBIO": 21537, "FIONREAD": 21531, "F_DUPFD": 0, "F_GETFD": 1, diff --git a/src/struct_info_generated_wasm64.json b/src/struct_info_generated_wasm64.json index 31ab797e96a84..b3fdd4c8208f0 100644 --- a/src/struct_info_generated_wasm64.json +++ b/src/struct_info_generated_wasm64.json @@ -292,6 +292,7 @@ "EWOULDBLOCK": 6, "EXDEV": 75, "EXFULL": 115, + "FIONBIO": 21537, "FIONREAD": 21531, "F_DUPFD": 0, "F_GETFD": 1, diff --git a/test/sockets/test_sockets_echo_server.c b/test/sockets/test_sockets_echo_server.c index 8465ccbbf42cf..151a793b10324 100644 --- a/test/sockets/test_sockets_echo_server.c +++ b/test/sockets/test_sockets_echo_server.c @@ -29,6 +29,15 @@ typedef int socklen_t; #include #endif +#ifdef _WIN32 +typedef u_long ioctlarg_t; +#else +#define INVALID_SOCKET -1 +#define SOCKET_ERROR -1 +#define ioctlsocket ioctl +typedef int ioctlarg_t; +#endif + #include "test_sockets_msg.h" typedef enum { @@ -193,16 +202,16 @@ int main() { #else server.fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); #endif - if (server.fd == -1) { + if (server.fd == INVALID_SOCKET) { perror("cannot create socket"); exit(EXIT_FAILURE); } -#ifdef _WIN32 - unsigned long nonblocking = 1; - ioctlsocket(server.fd, FIONBIO, &nonblocking); -#else - fcntl(server.fd, F_SETFL, O_NONBLOCK); -#endif + + ioctlarg_t on = 1; + if (ioctlsocket(server.fd, FIONBIO, &on) == SOCKET_ERROR) { + perror("ioctlsocket failed"); + exit(EXIT_FAILURE); + } memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; @@ -213,14 +222,14 @@ int main() { } res = bind(server.fd, (struct sockaddr *)&addr, sizeof(addr)); - if (res == -1) { + if (res == SOCKET_ERROR) { perror("bind failed"); exit(EXIT_FAILURE); } #if !TEST_DGRAM res = listen(server.fd, 50); - if (res == -1) { + if (res == SOCKET_ERROR) { perror("listen failed"); exit(EXIT_FAILURE); }