Skip to content

Commit 355f97b

Browse files
authored
[Sockets] Add ioctl support for FIOBIO (#24641)
This allows changing the socket's blocking mode. (Of course, this doesn't actually change or implement any support for O_NONBLOCK inside of libsockfs, it's just enabling the flag to be set just as it would be through fcntl.)
1 parent 31a6f1e commit 355f97b

File tree

6 files changed

+30
-9
lines changed

6 files changed

+30
-9
lines changed

src/lib/libsockfs.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,14 @@ addToLibrary({
421421
}
422422
{{{ makeSetValue('arg', '0', 'bytes', 'i32') }}};
423423
return 0;
424+
case {{{ cDefs.FIONBIO }}}:
425+
var on = {{{ makeGetValue('arg', '0', 'i32') }}};
426+
if (on) {
427+
sock.stream.flags |= {{{ cDefs.O_NONBLOCK }}};
428+
} else {
429+
sock.stream.flags &= ~{{{ cDefs.O_NONBLOCK }}};
430+
}
431+
return 0;
424432
default:
425433
return {{{ cDefs.EINVAL }}};
426434
}

src/lib/libsyscall.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ var SyscallsLibrary = {
284284
if (!stream.tty) return -{{{ cDefs.ENOTTY }}};
285285
return -{{{ cDefs.EINVAL }}}; // not supported
286286
}
287+
case {{{ cDefs.FIONBIO }}}:
287288
case {{{ cDefs.FIONREAD }}}: {
288289
var argp = syscallGetVarargP();
289290
return FS.ioctl(stream, op, argp);

src/struct_info.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@
312312
{
313313
"file": "bits/ioctl.h",
314314
"defines": [
315+
"FIONBIO",
315316
"FIONREAD",
316317
"TCGETA",
317318
"TCGETS",

src/struct_info_generated.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@
292292
"EWOULDBLOCK": 6,
293293
"EXDEV": 75,
294294
"EXFULL": 115,
295+
"FIONBIO": 21537,
295296
"FIONREAD": 21531,
296297
"F_DUPFD": 0,
297298
"F_GETFD": 1,

src/struct_info_generated_wasm64.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@
292292
"EWOULDBLOCK": 6,
293293
"EXDEV": 75,
294294
"EXFULL": 115,
295+
"FIONBIO": 21537,
295296
"FIONREAD": 21531,
296297
"F_DUPFD": 0,
297298
"F_GETFD": 1,

test/sockets/test_sockets_echo_server.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ typedef int socklen_t;
2929
#include <emscripten.h>
3030
#endif
3131

32+
#ifdef _WIN32
33+
typedef u_long ioctlarg_t;
34+
#else
35+
#define INVALID_SOCKET -1
36+
#define SOCKET_ERROR -1
37+
#define ioctlsocket ioctl
38+
typedef int ioctlarg_t;
39+
#endif
40+
3241
#include "test_sockets_msg.h"
3342

3443
typedef enum {
@@ -193,16 +202,16 @@ int main() {
193202
#else
194203
server.fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
195204
#endif
196-
if (server.fd == -1) {
205+
if (server.fd == INVALID_SOCKET) {
197206
perror("cannot create socket");
198207
exit(EXIT_FAILURE);
199208
}
200-
#ifdef _WIN32
201-
unsigned long nonblocking = 1;
202-
ioctlsocket(server.fd, FIONBIO, &nonblocking);
203-
#else
204-
fcntl(server.fd, F_SETFL, O_NONBLOCK);
205-
#endif
209+
210+
ioctlarg_t on = 1;
211+
if (ioctlsocket(server.fd, FIONBIO, &on) == SOCKET_ERROR) {
212+
perror("ioctlsocket failed");
213+
exit(EXIT_FAILURE);
214+
}
206215

207216
memset(&addr, 0, sizeof(addr));
208217
addr.sin_family = AF_INET;
@@ -213,14 +222,14 @@ int main() {
213222
}
214223

215224
res = bind(server.fd, (struct sockaddr *)&addr, sizeof(addr));
216-
if (res == -1) {
225+
if (res == SOCKET_ERROR) {
217226
perror("bind failed");
218227
exit(EXIT_FAILURE);
219228
}
220229

221230
#if !TEST_DGRAM
222231
res = listen(server.fd, 50);
223-
if (res == -1) {
232+
if (res == SOCKET_ERROR) {
224233
perror("listen failed");
225234
exit(EXIT_FAILURE);
226235
}

0 commit comments

Comments
 (0)