Skip to content

Commit

Permalink
Share file offset across execve() on Windows
Browse files Browse the repository at this point in the history
This is a breaking change. It defines the new environment variable named
_COSMO_FDS_V2 which is used for inheriting non-stdio file descriptors on
execve() or posix_spawn(). No effort has been spent thus far integrating
with the older variable. If a new binary launches the older ones or vice
versa they'll only be able to pass stdin / stdout / stderr to each other
therefore it's important that you upgrade all your cosmo binaries if you
depend on this functionality. You'll be glad you did because inheritance
of file descriptors is more aligned with the POSIX standard than before.
  • Loading branch information
jart committed Aug 4, 2024
1 parent 761c6ad commit 3f26dfb
Show file tree
Hide file tree
Showing 29 changed files with 572 additions and 249 deletions.
4 changes: 2 additions & 2 deletions libc/calls/close-nt.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ textwindows int sys_close_nt(int fd, int fildes) {
default:
break;
}
if (f->shared && !f->isdup)
munmap(f->shared, sizeof(struct Cursor));
if (f->cursor)
__cursor_unref(f->cursor);
return CloseHandle(f->handle) ? 0 : __winerr();
}
4 changes: 2 additions & 2 deletions libc/calls/dup-nt.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "libc/calls/struct/sigset.internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/errno.h"
#include "libc/intrin/fds.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/weaken.h"
#include "libc/nt/files.h"
Expand Down Expand Up @@ -82,8 +83,7 @@ static textwindows int sys_dup_nt_impl(int oldfd, int newfd, int flags,

g_fds.p[newfd] = g_fds.p[oldfd];
g_fds.p[newfd].handle = handle;
g_fds.p[newfd].isdup = true;
g_fds.p[oldfd].isdup = true; // TODO(jart): is it possible to avoid leak?
__cursor_ref(g_fds.p[newfd].cursor);
if (flags & _O_CLOEXEC) {
g_fds.p[newfd].flags |= _O_CLOEXEC;
} else {
Expand Down
4 changes: 2 additions & 2 deletions libc/calls/fcntl-nt.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ static textwindows int sys_fcntl_nt_lock(struct Fd *f, int fd, int cmd,
case SEEK_SET:
break;
case SEEK_CUR:
off = f->shared->pointer + off;
off = f->cursor->shared->pointer + off;
break;
case SEEK_END: {
int64_t size;
Expand Down Expand Up @@ -352,7 +352,7 @@ textwindows int sys_fcntl_nt(int fd, int cmd, uintptr_t arg) {
rc = 0;
} else if (cmd == F_SETLK || cmd == F_SETLKW || cmd == F_GETLK) {
struct Fd *f = g_fds.p + fd;
if (f->shared) {
if (f->cursor) {
pthread_mutex_lock(&g_locks.mu);
rc = sys_fcntl_nt_lock(f, fd, cmd, arg);
pthread_mutex_unlock(&g_locks.mu);
Expand Down
3 changes: 1 addition & 2 deletions libc/calls/isapemagic.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@
bool IsApeLoadable(char buf[8]) {
return READ32LE(buf) == READ32LE("\177ELF") ||
READ64LE(buf) == READ64LE("MZqFpD='") ||
READ64LE(buf) == READ64LE("jartsr='") ||
READ64LE(buf) == READ64LE("APEDBG='");
READ64LE(buf) == READ64LE("jartsr='");
}
11 changes: 6 additions & 5 deletions libc/calls/lseek-nt.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/intrin/fds.h"
#include "libc/nt/enum/filetype.h"
#include "libc/nt/files.h"
#include "libc/nt/struct/byhandlefileinformation.h"
Expand All @@ -31,7 +32,7 @@ static textwindows int64_t GetPosition(struct Fd *f, int whence) {
case SEEK_SET:
return 0;
case SEEK_CUR:
return f->shared->pointer;
return f->cursor->shared->pointer;
case SEEK_END: {
struct NtByHandleFileInformation wst;
if (!GetFileInformationByHandle(f->handle, &wst)) {
Expand Down Expand Up @@ -69,12 +70,12 @@ textwindows int64_t sys_lseek_nt(int fd, int64_t offset, int whence) {
int filetype = GetFileType(f->handle);
if (filetype != kNtFileTypePipe && //
filetype != kNtFileTypeChar && //
f->shared) {
f->cursor->shared) {
int64_t res;
__fd_lock(f);
__cursor_lock(f->cursor);
if ((res = Seek(f, offset, whence)) != -1)
f->shared->pointer = res;
__fd_unlock(f);
f->cursor->shared->pointer = res;
__cursor_unlock(f->cursor);
return res;
} else {
return espipe();
Expand Down
5 changes: 3 additions & 2 deletions libc/calls/open-nt.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "libc/calls/syscall-nt.internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/errno.h"
#include "libc/intrin/fds.h"
#include "libc/macros.internal.h"
#include "libc/nt/console.h"
#include "libc/nt/createfile.h"
Expand Down Expand Up @@ -138,7 +139,7 @@ static textwindows int sys_open_nt_file(int dirfd, const char *file,
int64_t handle;
if ((handle = sys_open_nt_impl(dirfd, file, flags, mode,
kNtFileFlagOverlapped)) != -1) {
g_fds.p[fd].shared = __cursor_new();
g_fds.p[fd].cursor = __cursor_new();
g_fds.p[fd].handle = handle;
g_fds.p[fd].kind = kFdFile;
g_fds.p[fd].flags = flags;
Expand Down Expand Up @@ -178,8 +179,8 @@ static textwindows int sys_open_nt_dup(int fd, int flags, int mode, int oldfd) {
kNtDuplicateSameAccess)) {
g_fds.p[fd] = g_fds.p[oldfd];
g_fds.p[fd].handle = handle;
g_fds.p[fd].isdup = true;
g_fds.p[fd].mode = mode;
__cursor_ref(g_fds.p[fd].cursor);
if (!sys_fcntl_nt_setfl(fd, flags)) {
return fd;
} else {
Expand Down
28 changes: 14 additions & 14 deletions libc/calls/readwrite-nt.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,29 @@ sys_readwrite_nt(int fd, void *data, size_t size, ssize_t offset,
return espipe();

// determine if we need to lock a file descriptor across processes
bool locked = isdisk && !pwriting && f->shared;
bool locked = isdisk && !pwriting && f->cursor;
if (locked)
__fd_lock(f);
__cursor_lock(f->cursor);

RestartOperation:
// when a file is opened in overlapped mode win32 requires that we
// take over full responsibility for managing our own file pointer
// which is fine, because the one win32 has was never very good in
// the sense that it behaves so differently from linux, that using
// win32 i/o required more compatibilty toil than doing it by hand
if (!pwriting) {
if (seekable && f->shared) {
offset = f->shared->pointer;
if (seekable && f->cursor) {
offset = f->cursor->shared->pointer;
} else {
offset = 0;
}
}

RestartOperation:
bool eagained = false;
// check for signals and cancelation
if (_check_cancel() == -1) {
if (locked)
__fd_unlock(f);
__cursor_unlock(f->cursor);
return -1; // ECANCELED
}
if (_weaken(__sig_get) && (sig = _weaken(__sig_get)(waitmask))) {
Expand Down Expand Up @@ -122,10 +122,10 @@ sys_readwrite_nt(int fd, void *data, size_t size, ssize_t offset,

// if i/o succeeded then return its result
if (ok) {
if (!pwriting && seekable && f->shared)
f->shared->pointer = offset + exchanged;
if (!pwriting && seekable && f->cursor)
f->cursor->shared->pointer = offset + exchanged;
if (locked)
__fd_unlock(f);
__cursor_unlock(f->cursor);
return exchanged;
}

Expand All @@ -134,31 +134,31 @@ sys_readwrite_nt(int fd, void *data, size_t size, ssize_t offset,
// raise EAGAIN if it's due to O_NONBLOCK mmode
if (eagained) {
if (locked)
__fd_unlock(f);
__cursor_unlock(f->cursor);
return eagain();
}
// otherwise it must be due to a kill() via __sig_cancel()
if (_weaken(__sig_relay) && (sig = _weaken(__sig_get)(waitmask))) {
HandleInterrupt:
if (locked)
__fd_unlock(f);
__cursor_unlock(f->cursor);
int handler_was_called = _weaken(__sig_relay)(sig, SI_KERNEL, waitmask);
if (_check_cancel() == -1)
return -1; // possible if we SIGTHR'd
if (locked)
__fd_lock(f);
__cursor_lock(f->cursor);
// read() is @restartable unless non-SA_RESTART hands were called
if (!(handler_was_called & SIG_HANDLED_NO_RESTART))
goto RestartOperation;
}
if (locked)
__fd_unlock(f);
__cursor_unlock(f->cursor);
return eintr();
}

// read() and write() have generally different error-handling paths
if (locked)
__fd_unlock(f);
__cursor_unlock(f->cursor);
return -2;
}

Expand Down
2 changes: 1 addition & 1 deletion libc/calls/write-nt.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/internal.h"
#include "libc/calls/sig.internal.h"
#include "libc/intrin/fds.h"
#include "libc/calls/struct/iovec.h"
#include "libc/calls/struct/sigset.h"
#include "libc/calls/struct/sigset.internal.h"
#include "libc/calls/syscall-nt.internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/errno.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/fds.h"
#include "libc/intrin/nomultics.h"
#include "libc/intrin/weaken.h"
#include "libc/nt/console.h"
Expand Down
6 changes: 0 additions & 6 deletions libc/integral/c.inc
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
#define COSMOPOLITAN_CXX_USING_
#endif

#ifndef __cplusplus
#pragma GCC diagnostic warning "-Wimplicit-function-declaration"
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#pragma GCC diagnostic warning "-Wint-conversion"
#endif

#if !defined(__GNUC__) && __cplusplus + 0 >= 201103L
#define typeof(x) decltype(x)
#elif !defined(__GNUC__) && __STDC_VERSION__ + 0 < 201112
Expand Down
1 change: 1 addition & 0 deletions libc/intrin/BUILD.mk
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ o/$(MODE)/libc/intrin/kprintf.o: private \
-Wframe-larger-than=128 \
-Walloca-larger-than=128

o/$(MODE)/libc/intrin/cursor.o \
o/$(MODE)/libc/intrin/mmap.o \
o/$(MODE)/libc/intrin/tree.o: private \
CFLAGS += \
Expand Down
64 changes: 64 additions & 0 deletions libc/intrin/cursor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2024 Justine Alexandra Roberts Tunney │
│ │
│ Permission to use, copy, modify, and/or distribute this software for │
│ any purpose with or without fee is hereby granted, provided that the │
│ above copyright notice and this permission notice appear in all copies. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/assert.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/fds.h"
#include "libc/runtime/runtime.h"

struct Cursor *__cursor_new(void) {
struct Cursor *c;
if ((c = _mapanon(sizeof(struct Cursor)))) {
if ((c->shared = _mapshared(sizeof(struct CursorShared)))) {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&c->shared->lock, &attr);
pthread_mutexattr_destroy(&attr);
} else {
munmap(c, sizeof(struct Cursor));
c = 0;
}
}
return c;
}

void __cursor_ref(struct Cursor *c) {
if (!c)
return;
unassert(atomic_fetch_add_explicit(&c->refs, 1, memory_order_relaxed) >= 0);
}

int __cursor_unref(struct Cursor *c) {
if (!c)
return 0;
if (atomic_fetch_sub_explicit(&c->refs, 1, memory_order_release))
return 0;
atomic_thread_fence(memory_order_acquire);
int rc = munmap(c->shared, sizeof(struct CursorShared));
rc |= munmap(c, sizeof(struct Cursor));
return rc;
}

void __cursor_lock(struct Cursor *c) {
pthread_mutex_lock(&c->shared->lock);
}

void __cursor_unlock(struct Cursor *c) {
pthread_mutex_unlock(&c->shared->lock);
}
Loading

0 comments on commit 3f26dfb

Please sign in to comment.