|
| 1 | +/* |
| 2 | + * Copyright 2026 The Emscripten Authors. All rights reserved. |
| 3 | + * Emscripten is available under two separate licenses, the MIT license and the |
| 4 | + * University of Illinois/NCSA Open Source License. Both these licenses can be |
| 5 | + * found in the LICENSE file. |
| 6 | + * |
| 7 | + * accept4(SOCK_NONBLOCK) must yield a non-blocking accepted socket even off a |
| 8 | + * *blocking* listener: the flag is applied on top of the flags inherited from |
| 9 | + * the listener, not dropped. A poll()-driven main loop (single-threaded, zero |
| 10 | + * timeout) waits for the incoming connection, accept4()s it with SOCK_NONBLOCK, |
| 11 | + * then checks F_GETFL reports O_NONBLOCK and that a data-less recv() would-block |
| 12 | + * with EAGAIN rather than hanging. Plain POSIX, so it also runs natively. |
| 13 | + */ |
| 14 | + |
| 15 | +#include <arpa/inet.h> |
| 16 | +#include <assert.h> |
| 17 | +#include <errno.h> |
| 18 | +#include <fcntl.h> |
| 19 | +#include <netinet/in.h> |
| 20 | +#include <poll.h> |
| 21 | +#include <stdbool.h> |
| 22 | +#include <stdio.h> |
| 23 | +#include <string.h> |
| 24 | +#include <sys/socket.h> |
| 25 | +#include <unistd.h> |
| 26 | + |
| 27 | +#ifdef __EMSCRIPTEN__ |
| 28 | +#include <emscripten.h> |
| 29 | +#endif |
| 30 | + |
| 31 | +static int listen_fd = -1; |
| 32 | +static int client_fd = -1; |
| 33 | +static int peer_fd = -1; |
| 34 | + |
| 35 | +static void finish(void) { |
| 36 | + if (client_fd >= 0) close(client_fd); |
| 37 | + if (peer_fd >= 0) close(peer_fd); |
| 38 | + if (listen_fd >= 0) close(listen_fd); |
| 39 | + printf("done\n"); |
| 40 | +#ifdef __EMSCRIPTEN__ |
| 41 | + emscripten_cancel_main_loop(); |
| 42 | +#endif |
| 43 | +} |
| 44 | + |
| 45 | +static void main_loop(void) { |
| 46 | + struct pollfd pfd = { .fd = listen_fd, .events = POLLIN }; |
| 47 | + if (poll(&pfd, 1, 0) <= 0 || !(pfd.revents & POLLIN)) { |
| 48 | + return; // no connection queued yet |
| 49 | + } |
| 50 | + |
| 51 | + // The listener is blocking (never marked O_NONBLOCK), so inheritance alone |
| 52 | + // would give a blocking socket; SOCK_NONBLOCK must override that. |
| 53 | + peer_fd = accept4(listen_fd, NULL, NULL, SOCK_NONBLOCK); |
| 54 | + assert(peer_fd >= 0); |
| 55 | + |
| 56 | + int fl = fcntl(peer_fd, F_GETFL); |
| 57 | + assert(fl >= 0 && (fl & O_NONBLOCK) && "accept4 SOCK_NONBLOCK not honored"); |
| 58 | + |
| 59 | + // A non-blocking recv with no data pending returns EAGAIN immediately instead |
| 60 | + // of blocking, confirming the fd is really non-blocking. |
| 61 | + char buf[4]; |
| 62 | + ssize_t n = recv(peer_fd, buf, sizeof(buf), 0); |
| 63 | + assert(n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)); |
| 64 | + |
| 65 | + finish(); |
| 66 | +} |
| 67 | + |
| 68 | +int main(void) { |
| 69 | + listen_fd = socket(AF_INET, SOCK_STREAM, 0); |
| 70 | + assert(listen_fd >= 0); |
| 71 | + |
| 72 | + struct sockaddr_in addr; |
| 73 | + memset(&addr, 0, sizeof(addr)); |
| 74 | + addr.sin_family = AF_INET; |
| 75 | + inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr); |
| 76 | + assert(bind(listen_fd, (struct sockaddr*)&addr, sizeof(addr)) == 0); |
| 77 | + socklen_t l = sizeof(addr); |
| 78 | + assert(getsockname(listen_fd, (struct sockaddr*)&addr, &l) == 0); |
| 79 | + assert(listen(listen_fd, 4) == 0); |
| 80 | + // Deliberately leave listen_fd blocking to prove SOCK_NONBLOCK is applied on |
| 81 | + // top of the inherited (blocking) listener flags. |
| 82 | + |
| 83 | + client_fd = socket(AF_INET, SOCK_STREAM, 0); |
| 84 | + assert(client_fd >= 0); |
| 85 | + fcntl(client_fd, F_SETFL, O_NONBLOCK); |
| 86 | + int r = connect(client_fd, (struct sockaddr*)&addr, sizeof(addr)); |
| 87 | + assert(r == 0 || errno == EINPROGRESS); |
| 88 | + |
| 89 | +#ifdef __EMSCRIPTEN__ |
| 90 | + emscripten_set_main_loop(main_loop, 0, 0); |
| 91 | +#else |
| 92 | + while (peer_fd < 0) { |
| 93 | + main_loop(); |
| 94 | + usleep(1000); |
| 95 | + } |
| 96 | +#endif |
| 97 | + return 0; |
| 98 | +} |
0 commit comments