Skip to content

Commit d1a745c

Browse files
authored
Implement __zipos_dup (#972)
* Implement __zipos_dup Makes ZiposHandle reference-counted by an `rc` field in a union with its freelist `next` pointer. The functions `__zipos_free` and `__zipos_keep` function as incref/decref for it. Adds `__zipos_postdup` to fix metadata on file descriptors after dup-like operations, and adds zipos support to `sys_dup_nt` + `sys_close_nt`. * Remove noop __zipos_postdup rc is never a zipos file because it is always a previously unused file descriptor. fd is never a zipos file because that case has been handled above by __zipos_fcntl.
1 parent 6556dd2 commit d1a745c

File tree

11 files changed

+138
-39
lines changed

11 files changed

+138
-39
lines changed

libc/calls/close-nt.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "libc/nt/enum/filetype.h"
2525
#include "libc/nt/files.h"
2626
#include "libc/nt/runtime.h"
27+
#include "libc/runtime/zipos.internal.h"
2728
#include "libc/sock/syscall_fd.internal.h"
2829
#include "libc/sysv/consts/o.h"
2930
#include "libc/sysv/errfuns.h"
@@ -32,6 +33,8 @@ textwindows int sys_close_nt(int fd, int fildes) {
3233
if (fd + 0u >= g_fds.n) return ebadf();
3334
struct Fd *f = g_fds.p + fd;
3435
switch (f->kind) {
36+
case kFdZip:
37+
return _weaken(__zipos_close)(fd);
3538
case kFdEmpty:
3639
return ebadf();
3740
case kFdFile:

libc/calls/dup-nt.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,30 +59,34 @@ static textwindows int sys_dup_nt_impl(int oldfd, int newfd, int flags,
5959
return -1;
6060
}
6161
if (g_fds.p[newfd].kind) {
62-
if (g_fds.p[newfd].kind == kFdZip) {
63-
_weaken(__zipos_close)(newfd);
64-
} else {
65-
sys_close_nt(newfd, newfd);
66-
}
62+
sys_close_nt(newfd, newfd);
6763
}
6864
}
6965

70-
if (DuplicateHandle(GetCurrentProcess(), g_fds.p[oldfd].handle,
71-
GetCurrentProcess(), &handle, 0, true,
72-
kNtDuplicateSameAccess)) {
73-
g_fds.p[newfd] = g_fds.p[oldfd];
74-
g_fds.p[newfd].handle = handle;
75-
if (flags & _O_CLOEXEC) {
76-
g_fds.p[newfd].flags |= _O_CLOEXEC;
77-
} else {
78-
g_fds.p[newfd].flags &= ~_O_CLOEXEC;
79-
}
66+
if (__isfdkind(oldfd, kFdZip)) {
67+
handle = (intptr_t)_weaken(__zipos_keep)(
68+
(struct ZiposHandle *)(intptr_t)g_fds.p[oldfd].handle);
8069
rc = newfd;
8170
} else {
82-
__releasefd(newfd);
83-
rc = __winerr();
71+
if (DuplicateHandle(GetCurrentProcess(), g_fds.p[oldfd].handle,
72+
GetCurrentProcess(), &handle, 0, true,
73+
kNtDuplicateSameAccess)) {
74+
rc = newfd;
75+
} else {
76+
rc = __winerr();
77+
__releasefd(newfd);
78+
__fds_unlock();
79+
return rc;
80+
}
8481
}
8582

83+
g_fds.p[newfd] = g_fds.p[oldfd];
84+
g_fds.p[newfd].handle = handle;
85+
if (flags & _O_CLOEXEC) {
86+
g_fds.p[newfd].flags |= _O_CLOEXEC;
87+
} else {
88+
g_fds.p[newfd].flags &= ~_O_CLOEXEC;
89+
}
8690
__fds_unlock();
8791
return rc;
8892
}

libc/calls/dup.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
#include "libc/calls/syscall-sysv.internal.h"
2323
#include "libc/dce.h"
2424
#include "libc/intrin/strace.internal.h"
25+
#include "libc/intrin/weaken.h"
2526
#include "libc/sysv/errfuns.h"
27+
#include "libc/runtime/zipos.internal.h"
2628

2729
/**
2830
* Duplicates file descriptor.
@@ -51,10 +53,11 @@
5153
*/
5254
int dup(int fd) {
5355
int rc;
54-
if (__isfdkind(fd, kFdZip)) {
55-
rc = enotsup();
56-
} else if (!IsWindows()) {
56+
if (!IsWindows()) {
5757
rc = sys_dup(fd);
58+
if (rc != -1 && __isfdkind(fd, kFdZip)) {
59+
_weaken(__zipos_postdup)(fd, rc);
60+
}
5861
} else {
5962
rc = sys_dup_nt(fd, -1, 0, -1);
6063
}

libc/calls/dup2.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,24 @@ int dup2(int oldfd, int newfd) {
6767
int rc;
6868
// helps guarantee stderr log gets duplicated before user closes
6969
if (_weaken(kloghandle)) _weaken(kloghandle)();
70-
if (__isfdkind(oldfd, kFdZip)) {
71-
rc = enotsup();
7270
#ifdef __aarch64__
73-
} else if (oldfd == newfd) {
71+
if (oldfd == newfd) {
7472
// linux aarch64 defines dup3() but not dup2(), which wasn't such a
7573
// great decision, since the two syscalls don't behave the same way
7674
if (!(rc = read(oldfd, 0, 0))) rc = oldfd;
75+
} else
7776
#endif
78-
} else if (!IsWindows()) {
79-
rc = sys_dup2(oldfd, newfd, 0);
80-
if (rc != -1 && oldfd != newfd && __isfdkind(newfd, kFdZip) && !__vforked) {
81-
_weaken(__zipos_free)(
82-
(struct ZiposHandle *)(intptr_t)g_fds.p[newfd].handle);
83-
bzero(g_fds.p + newfd, sizeof(*g_fds.p));
77+
if (!IsWindows()) {
78+
if (__isfdkind(oldfd, kFdZip) || __isfdkind(newfd, kFdZip)) {
79+
if (__vforked) {
80+
return enotsup();
81+
}
82+
rc = sys_dup2(oldfd, newfd, 0);
83+
if (rc != -1) {
84+
_weaken(__zipos_postdup)(oldfd, newfd);
85+
}
86+
} else {
87+
rc = sys_dup2(oldfd, newfd, 0);
8488
}
8589
} else if (newfd < 0) {
8690
rc = ebadf();

libc/calls/dup3.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,17 @@ int dup3(int oldfd, int newfd, int flags) {
7070
rc = einval(); // NetBSD doesn't do this
7171
} else if (oldfd < 0 || newfd < 0) {
7272
rc = ebadf();
73-
} else if (__isfdkind(oldfd, kFdZip)) {
74-
rc = enotsup();
7573
} else if (!IsWindows()) {
76-
rc = sys_dup3(oldfd, newfd, flags);
77-
if (rc != -1 && __isfdkind(newfd, kFdZip) && !__vforked) {
78-
_weaken(__zipos_free)(
79-
(struct ZiposHandle *)(intptr_t)g_fds.p[newfd].handle);
80-
bzero(g_fds.p + newfd, sizeof(*g_fds.p));
74+
if (__isfdkind(oldfd, kFdZip) || __isfdkind(newfd, kFdZip)) {
75+
if (__vforked) {
76+
return enotsup();
77+
}
78+
rc = sys_dup3(oldfd, newfd, flags);
79+
if (rc != -1) {
80+
_weaken(__zipos_postdup)(oldfd, newfd);
81+
}
82+
} else {
83+
rc = sys_dup3(oldfd, newfd, flags);
8184
}
8285
} else {
8386
rc = sys_dup_nt(oldfd, newfd, flags, -1);

libc/runtime/zipos-fcntl.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,30 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/calls/createfileflags.internal.h"
1920
#include "libc/calls/internal.h"
21+
#include "libc/calls/syscall-nt.internal.h"
22+
#include "libc/calls/syscall-sysv.internal.h"
2023
#include "libc/runtime/zipos.internal.h"
2124
#include "libc/sysv/consts/f.h"
2225
#include "libc/sysv/consts/fd.h"
2326
#include "libc/sysv/consts/o.h"
2427
#include "libc/sysv/errfuns.h"
2528

29+
static int __zipos_dupfd(int fd, int cmd, int start) {
30+
int rc;
31+
if (start < 0) return einval();
32+
if (IsWindows()) {
33+
return sys_dup_nt(fd, -1, (cmd == F_DUPFD_CLOEXEC ? _O_CLOEXEC : 0),
34+
start);
35+
}
36+
rc = sys_fcntl(fd, cmd, start, __sys_fcntl);
37+
if (rc != -1) {
38+
__zipos_postdup(fd, rc);
39+
}
40+
return rc;
41+
}
42+
2643
int __zipos_fcntl(int fd, int cmd, uintptr_t arg) {
2744
if (cmd == F_GETFD) {
2845
if (g_fds.p[fd].flags & O_CLOEXEC) {
@@ -38,6 +55,8 @@ int __zipos_fcntl(int fd, int cmd, uintptr_t arg) {
3855
g_fds.p[fd].flags &= ~O_CLOEXEC;
3956
return 0;
4057
}
58+
} else if (cmd == F_DUPFD || cmd == F_DUPFD_CLOEXEC) {
59+
return __zipos_dupfd(fd, cmd, arg);
4160
} else {
4261
return einval();
4362
}

libc/runtime/zipos-open.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,20 @@ static void *__zipos_mmap_space(size_t mapsize) {
7878
return start + offset;
7979
}
8080

81+
struct ZiposHandle *__zipos_keep(struct ZiposHandle *h) {
82+
atomic_fetch_add_explicit(&h->refs, 1, memory_order_relaxed);
83+
return h;
84+
}
85+
86+
static bool __zipos_drop(struct ZiposHandle *h) {
87+
int refs = atomic_load_explicit(&h->refs, memory_order_acquire);
88+
return -1 == refs || -1 == atomic_fetch_sub(&h->refs, 1);
89+
}
90+
8191
void __zipos_free(struct ZiposHandle *h) {
92+
if (!__zipos_drop(h)) {
93+
return;
94+
}
8295
if (IsAsan()) {
8396
__asan_poison((char *)h + sizeof(struct ZiposHandle),
8497
h->mapsize - sizeof(struct ZiposHandle), kAsanHeapFree);
@@ -100,7 +113,7 @@ static struct ZiposHandle *__zipos_alloc(struct Zipos *zipos, size_t size) {
100113
while ((h = *ph)) {
101114
if (h->mapsize >= mapsize) {
102115
if (!_cmpxchg(ph, h, h->next)) goto StartOver;
103-
h->next = 0;
116+
atomic_init(&h->refs, 0);
104117
break;
105118
}
106119
ph = &h->next;
@@ -209,6 +222,27 @@ static int __zipos_load(struct Zipos *zipos, size_t cf, int flags,
209222
return -1;
210223
}
211224

225+
void __zipos_postdup(int oldfd, int newfd) {
226+
if (oldfd == newfd) {
227+
return;
228+
}
229+
if (__isfdkind(newfd, kFdZip)) {
230+
__zipos_free((struct ZiposHandle *)(intptr_t)g_fds.p[newfd].handle);
231+
if (!__isfdkind(oldfd, kFdZip)) {
232+
__fds_lock();
233+
bzero(g_fds.p + newfd, sizeof(*g_fds.p));
234+
__fds_unlock();
235+
}
236+
}
237+
if (__isfdkind(oldfd, kFdZip)) {
238+
__zipos_keep((struct ZiposHandle *)(intptr_t)g_fds.p[oldfd].handle);
239+
__fds_lock();
240+
__ensurefds_unlocked(newfd);
241+
g_fds.p[newfd] = g_fds.p[oldfd];
242+
__fds_unlock();
243+
}
244+
}
245+
212246
/**
213247
* Loads compressed file from αcτµαlly pδrταblε εxεcµταblε object store.
214248
*

libc/runtime/zipos.S

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
.yoink __zipos_stat
3434
.yoink __zipos_notat
3535
.yoink __zipos_mmap
36+
.yoink __zipos_postdup
37+
.yoink __zipos_keep
38+
.yoink __zipos_free
3639

3740
// TODO(jart): why does corruption happen when zip has no assets?
3841
.yoink .cosmo

libc/runtime/zipos.internal.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ struct ZiposHandle {
2020
struct Zipos *zipos;
2121
size_t size;
2222
size_t mapsize;
23-
size_t pos;
2423
size_t cfile;
24+
_Atomic(int) refs;
25+
size_t pos; // TODO atomic
2526
uint8_t *mem;
2627
uint8_t data[];
2728
};
@@ -38,13 +39,15 @@ struct Zipos {
3839

3940
int __zipos_close(int);
4041
void __zipos_free(struct ZiposHandle *);
42+
struct ZiposHandle *__zipos_keep(struct ZiposHandle *);
4143
struct Zipos *__zipos_get(void) pureconst;
4244
size_t __zipos_normpath(char *, const char *, size_t);
4345
ssize_t __zipos_find(struct Zipos *, struct ZiposUri *);
4446
ssize_t __zipos_scan(struct Zipos *, struct ZiposUri *);
4547
ssize_t __zipos_parseuri(const char *, struct ZiposUri *);
4648
uint64_t __zipos_inode(struct Zipos *, int64_t, const void *, size_t);
4749
int __zipos_open(struct ZiposUri *, int);
50+
void __zipos_postdup(int, int);
4851
int __zipos_access(struct ZiposUri *, int);
4952
int __zipos_stat(struct ZiposUri *, struct stat *);
5053
int __zipos_fstat(struct ZiposHandle *, struct stat *);

test/libc/calls/dup_test.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ TEST(dup, bigNumber) {
7575
ASSERT_SYS(0, 0, close(100));
7676
}
7777

78-
TEST(dup2, zipos) {
78+
TEST(dup2, ziposdest) {
7979
ASSERT_SYS(0, 3, creat("real", 0644));
8080
ASSERT_SYS(0, 4, open("/zip/libc/testlib/hyperion.txt", O_RDONLY));
8181
ASSERT_SYS(0, 2, write(3, "hi", 2));
@@ -86,6 +86,15 @@ TEST(dup2, zipos) {
8686
ASSERT_SYS(0, 0, close(3));
8787
}
8888

89+
TEST(dup2, zipossrc) {
90+
char b[8];
91+
ASSERT_SYS(0, 3, open("/zip/libc/testlib/hyperion.txt", O_RDONLY));
92+
ASSERT_SYS(0, 4, dup2(3, 4));
93+
ASSERT_SYS(0, 8, read(4, b, 8));
94+
ASSERT_SYS(0, 0, close(4));
95+
ASSERT_SYS(0, 0, close(3));
96+
}
97+
8998
#ifdef __x86_64__
9099
TEST(dup, clearsCloexecFlag) {
91100
static bool once;

0 commit comments

Comments
 (0)