mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-08 04:08:32 +00:00
Share file offset across execve() on Windows
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.
This commit is contained in:
parent
761c6ad615
commit
3f26dfbb31
29 changed files with 572 additions and 249 deletions
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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;
|
||||
|
@ -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);
|
||||
|
|
|
@ -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='");
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
@ -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)) {
|
||||
|
@ -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();
|
||||
|
|
|
@ -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"
|
||||
|
@ -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;
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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))) {
|
||||
|
@ -122,10 +122,10 @@ RestartOperation:
|
|||
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
@ -134,31 +134,31 @@ RestartOperation:
|
|||
// 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#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"
|
||||
|
@ -26,6 +25,7 @@
|
|||
#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"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue