summaryrefslogtreecommitdiffstats
path: root/libc-bottom-half/sources/__wasilibc_fd_renumber.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-26 16:08:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-26 16:08:03 +0000
commitf1db79e6e5c383cf76f3bf0dd42115d19591a72b (patch)
tree3f9509008e8a130c45b7e31b1520d66d720493ec /libc-bottom-half/sources/__wasilibc_fd_renumber.c
parentAdding upstream version 0.0~git20230821.ec4566b. (diff)
downloadwasi-libc-upstream/0.0_git20240411.9e8c542.tar.xz
wasi-libc-upstream/0.0_git20240411.9e8c542.zip
Adding upstream version 0.0~git20240411.9e8c542.upstream/0.0_git20240411.9e8c542upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'libc-bottom-half/sources/__wasilibc_fd_renumber.c')
-rw-r--r--libc-bottom-half/sources/__wasilibc_fd_renumber.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/libc-bottom-half/sources/__wasilibc_fd_renumber.c b/libc-bottom-half/sources/__wasilibc_fd_renumber.c
index 47992e9..7690d13 100644
--- a/libc-bottom-half/sources/__wasilibc_fd_renumber.c
+++ b/libc-bottom-half/sources/__wasilibc_fd_renumber.c
@@ -15,10 +15,85 @@ int __wasilibc_fd_renumber(int fd, int newfd) {
return 0;
}
+#ifdef __wasilibc_use_wasip2
+#include <wasi/descriptor_table.h>
+
+void drop_tcp_socket(tcp_socket_t socket) {
+ switch (socket.state.tag) {
+ case TCP_SOCKET_STATE_UNBOUND:
+ case TCP_SOCKET_STATE_BOUND:
+ case TCP_SOCKET_STATE_CONNECTING:
+ case TCP_SOCKET_STATE_LISTENING:
+ case TCP_SOCKET_STATE_CONNECT_FAILED:
+ // No additional resources to drop.
+ break;
+ case TCP_SOCKET_STATE_CONNECTED: {
+ tcp_socket_state_connected_t connection = socket.state.connected;
+
+ poll_pollable_drop_own(connection.input_pollable);
+ poll_pollable_drop_own(connection.output_pollable);
+ streams_input_stream_drop_own(connection.input);
+ streams_output_stream_drop_own(connection.output);
+ break;
+ }
+ default: /* unreachable */ abort();
+ }
+
+ poll_pollable_drop_own(socket.socket_pollable);
+ tcp_tcp_socket_drop_own(socket.socket);
+}
+
+void drop_udp_socket_streams(udp_socket_streams_t streams) {
+ poll_pollable_drop_own(streams.incoming_pollable);
+ poll_pollable_drop_own(streams.outgoing_pollable);
+ udp_incoming_datagram_stream_drop_own(streams.incoming);
+ udp_outgoing_datagram_stream_drop_own(streams.outgoing);
+}
+
+void drop_udp_socket(udp_socket_t socket) {
+ switch (socket.state.tag) {
+ case UDP_SOCKET_STATE_UNBOUND:
+ case UDP_SOCKET_STATE_BOUND_NOSTREAMS:
+ // No additional resources to drop.
+ break;
+ case UDP_SOCKET_STATE_BOUND_STREAMING:
+ drop_udp_socket_streams(socket.state.bound_streaming.streams);
+ break;
+ case UDP_SOCKET_STATE_CONNECTED: {
+ drop_udp_socket_streams(socket.state.connected.streams);
+ break;
+ }
+ default: /* unreachable */ abort();
+ }
+
+ poll_pollable_drop_own(socket.socket_pollable);
+ udp_udp_socket_drop_own(socket.socket);
+}
+#endif // __wasilibc_use_wasip2
+
int close(int fd) {
// Scan the preopen fds before making any changes.
__wasilibc_populate_preopens();
+#ifdef __wasilibc_use_wasip2
+ descriptor_table_entry_t entry;
+ if (descriptor_table_remove(fd, &entry)) {
+
+ switch (entry.tag)
+ {
+ case DESCRIPTOR_TABLE_ENTRY_TCP_SOCKET:
+ drop_tcp_socket(entry.tcp_socket);
+ break;
+ case DESCRIPTOR_TABLE_ENTRY_UDP_SOCKET:
+ drop_udp_socket(entry.udp_socket);
+ break;
+ default: /* unreachable */ abort();
+ }
+
+ return 0;
+ }
+#endif // __wasilibc_use_wasip2
+
__wasi_errno_t error = __wasi_fd_close(fd);
if (error != 0) {
errno = error;