From 3b302e63795331555952ed71f0c96b259dfa1e3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C5=8Dshin?= Date: Wed, 13 Dec 2023 04:36:44 -0500 Subject: [PATCH] Cleanup zipos vfork (#1004) --- libc/calls/close.c | 2 +- libc/runtime/zipos-close.c | 4 ++-- test/libc/runtime/zipos_test.c | 13 +++++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/libc/calls/close.c b/libc/calls/close.c index b5c8fedbf..774e97e72 100644 --- a/libc/calls/close.c +++ b/libc/calls/close.c @@ -49,7 +49,7 @@ static int close_impl(int fd) { } if (__isfdkind(fd, kFdZip)) { - if (!__vforked && _weaken(__zipos_close)) { + if (_weaken(__zipos_close)) { return _weaken(__zipos_close)(fd); } if (!IsWindows() && !IsMetal()) { diff --git a/libc/runtime/zipos-close.c b/libc/runtime/zipos-close.c index b316d8c94..c6559016a 100644 --- a/libc/runtime/zipos-close.c +++ b/libc/runtime/zipos-close.c @@ -31,14 +31,14 @@ */ int __zipos_close(int fd) { int rc; - struct ZiposHandle *h; - h = (struct ZiposHandle *)(intptr_t)g_fds.p[fd].handle; if (!IsWindows()) { rc = sys_close(fd); } else { rc = 0; // no system file descriptor needed on nt } if (!__vforked) { + struct ZiposHandle *h; + h = (struct ZiposHandle *)(intptr_t)g_fds.p[fd].handle; __zipos_free(h); } return rc; diff --git a/test/libc/runtime/zipos_test.c b/test/libc/runtime/zipos_test.c index e7befe201..6b6c62f8d 100644 --- a/test/libc/runtime/zipos_test.c +++ b/test/libc/runtime/zipos_test.c @@ -28,6 +28,7 @@ #include "libc/str/str.h" #include "libc/sysv/consts/o.h" #include "libc/testlib/hyperion.h" +#include "libc/testlib/subprocess.h" #include "libc/testlib/testlib.h" #include "libc/thread/thread.h" @@ -111,3 +112,15 @@ TEST(zipos, lseek) { EXPECT_EQ(0, memcmp(b1, b2, 512)); EXPECT_SYS(0, 0, close(3)); } + +TEST(zipos, closeAfterVfork) { + ASSERT_SYS(0, 3, open("/zip/libc/testlib/hyperion.txt", O_RDONLY)); + SPAWN(vfork); + ASSERT_SYS(0, 0, close(3)); + ASSERT_SYS(0, 3, open("/etc/hosts", O_RDONLY)); + ASSERT_SYS(0, 0, close(3)); + ASSERT_SYS(EBADF, -1, close(3)); + EXITS(0); + ASSERT_SYS(0, 0, close(3)); + ASSERT_SYS(EBADF, -1, close(3)); +}