mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-16 23:50: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
|
@ -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 += \
|
||||
|
|
64
libc/intrin/cursor.c
Normal file
64
libc/intrin/cursor.c
Normal file
|
@ -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);
|
||||
}
|
|
@ -23,7 +23,7 @@
|
|||
#include "libc/dce.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/intrin/extend.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/maps.h"
|
||||
#include "libc/intrin/nomultics.h"
|
||||
#include "libc/intrin/pushpop.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
|
@ -32,7 +32,9 @@
|
|||
#include "libc/nt/enum/accessmask.h"
|
||||
#include "libc/nt/enum/creationdisposition.h"
|
||||
#include "libc/nt/enum/fileflagandattributes.h"
|
||||
#include "libc/nt/enum/filemapflags.h"
|
||||
#include "libc/nt/enum/filesharemode.h"
|
||||
#include "libc/nt/memory.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/memtrack.internal.h"
|
||||
|
@ -55,7 +57,11 @@ static struct Fd g_fds_static[OPEN_MAX];
|
|||
static bool TokAtoi(const char **str, long *res) {
|
||||
int c, d;
|
||||
unsigned long x = 0;
|
||||
d = **str == '-' ? -1 : 1;
|
||||
d = 1;
|
||||
if (**str == '-') {
|
||||
(*str)++;
|
||||
d = -1;
|
||||
}
|
||||
while ((c = *(*str)++)) {
|
||||
if (('0' <= c && c <= '9')) {
|
||||
x *= 10;
|
||||
|
@ -122,10 +128,11 @@ textstartup void __init_fds(int argc, char **argv, char **envp) {
|
|||
// inherit file descriptors from cosmo parent process
|
||||
if (IsWindows()) {
|
||||
const char *fdspec;
|
||||
if ((fdspec = getenv("_COSMO_FDS"))) {
|
||||
if ((fdspec = getenv("_COSMO_FDS_V2"))) {
|
||||
unsetenv("_COSMO_FDS");
|
||||
unsetenv("_COSMO_FDS_V2");
|
||||
for (;;) {
|
||||
long fd, kind, flags, mode, handle, pointer, type, family, protocol;
|
||||
long fd, kind, flags, mode, handle, shand, type, family, protocol;
|
||||
if (!TokAtoi(&fdspec, &fd))
|
||||
break;
|
||||
if (!TokAtoi(&fdspec, &handle))
|
||||
|
@ -136,7 +143,7 @@ textstartup void __init_fds(int argc, char **argv, char **envp) {
|
|||
break;
|
||||
if (!TokAtoi(&fdspec, &mode))
|
||||
break;
|
||||
if (!TokAtoi(&fdspec, &pointer))
|
||||
if (!TokAtoi(&fdspec, &shand))
|
||||
break;
|
||||
if (!TokAtoi(&fdspec, &type))
|
||||
break;
|
||||
|
@ -149,9 +156,8 @@ textstartup void __init_fds(int argc, char **argv, char **envp) {
|
|||
struct Fd *f = fds->p + fd;
|
||||
if (f->handle && f->handle != -1 && f->handle != handle) {
|
||||
CloseHandle(f->handle);
|
||||
if (fd < 3) {
|
||||
if (fd < 3)
|
||||
SetStdHandle(kNtStdio[fd], handle);
|
||||
}
|
||||
}
|
||||
f->handle = handle;
|
||||
f->kind = kind;
|
||||
|
@ -162,24 +168,31 @@ textstartup void __init_fds(int argc, char **argv, char **envp) {
|
|||
f->protocol = protocol;
|
||||
atomic_store_explicit(&fds->f, fd + 1, memory_order_relaxed);
|
||||
|
||||
//
|
||||
// - v1 abi: This field was originally the file pointer.
|
||||
//
|
||||
// - v2 abi: This field is the negated shared memory address.
|
||||
//
|
||||
if (f->kind == kFdFile) {
|
||||
if (pointer < 0) {
|
||||
f->shared = (struct Cursor *)(uintptr_t)-pointer;
|
||||
} else if ((f->shared = __cursor_new())) {
|
||||
f->shared->pointer = pointer;
|
||||
if (shand) {
|
||||
struct Map *map;
|
||||
struct CursorShared *shared;
|
||||
if ((shared = MapViewOfFileEx(shand, kNtFileMapWrite, 0, 0,
|
||||
sizeof(struct CursorShared), 0))) {
|
||||
if ((f->cursor = _mapanon(sizeof(struct Cursor)))) {
|
||||
f->cursor->shared = shared;
|
||||
if ((map = __maps_alloc())) {
|
||||
map->addr = (char *)shared;
|
||||
map->size = sizeof(struct CursorShared);
|
||||
map->off = 0;
|
||||
map->prot = PROT_READ | PROT_WRITE;
|
||||
map->flags = MAP_SHARED | MAP_ANONYMOUS;
|
||||
map->hand = shand;
|
||||
__maps_insert(map);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
struct Fd *f = fds->p + i;
|
||||
if (f->kind == kFdFile && !f->shared)
|
||||
f->shared = __cursor_new();
|
||||
if (f->kind == kFdFile && !f->cursor)
|
||||
f->cursor = __cursor_new();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_CALLS_STRUCT_FD_INTERNAL_H_
|
||||
#define COSMOPOLITAN_LIBC_CALLS_STRUCT_FD_INTERNAL_H_
|
||||
#include "libc/atomic.h"
|
||||
#include "libc/thread/thread.h"
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
|
@ -15,14 +14,18 @@ COSMOPOLITAN_C_START_
|
|||
#define kFdDevNull 9
|
||||
#define kFdDevRandom 10
|
||||
|
||||
struct Cursor {
|
||||
struct CursorShared {
|
||||
pthread_mutex_t lock;
|
||||
long pointer;
|
||||
};
|
||||
|
||||
struct Cursor {
|
||||
struct CursorShared *shared;
|
||||
_Atomic(int) refs;
|
||||
};
|
||||
|
||||
struct Fd {
|
||||
char kind;
|
||||
bool isdup;
|
||||
bool isbound;
|
||||
unsigned flags;
|
||||
unsigned mode;
|
||||
|
@ -33,7 +36,7 @@ struct Fd {
|
|||
unsigned rcvtimeo; /* millis; 0 means wait forever */
|
||||
unsigned sndtimeo; /* millis; 0 means wait forever */
|
||||
void *connect_op;
|
||||
struct Cursor *shared;
|
||||
struct Cursor *cursor;
|
||||
};
|
||||
|
||||
struct Fds {
|
||||
|
@ -42,9 +45,11 @@ struct Fds {
|
|||
struct Fd *p, *e;
|
||||
};
|
||||
|
||||
void __fd_lock(struct Fd *);
|
||||
void __fd_unlock(struct Fd *);
|
||||
struct Cursor *__cursor_new(void);
|
||||
void __cursor_ref(struct Cursor *);
|
||||
int __cursor_unref(struct Cursor *);
|
||||
void __cursor_lock(struct Cursor *);
|
||||
void __cursor_unlock(struct Cursor *);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* COSMOPOLITAN_LIBC_CALLS_STRUCT_FD_INTERNAL_H_ */
|
||||
|
|
|
@ -17,8 +17,6 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/state.internal.h"
|
||||
#include "libc/intrin/fds.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
void __fds_lock(void) {
|
||||
|
@ -28,23 +26,3 @@ void __fds_lock(void) {
|
|||
void __fds_unlock(void) {
|
||||
pthread_mutex_unlock(&__fds_lock_obj);
|
||||
}
|
||||
|
||||
void __fd_lock(struct Fd *f) {
|
||||
pthread_mutex_lock(&f->shared->lock);
|
||||
}
|
||||
|
||||
void __fd_unlock(struct Fd *f) {
|
||||
pthread_mutex_unlock(&f->shared->lock);
|
||||
}
|
||||
|
||||
struct Cursor *__cursor_new(void) {
|
||||
struct Cursor *c;
|
||||
if ((c = _mapshared(sizeof(struct Cursor)))) {
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
|
||||
pthread_mutex_init(&c->lock, &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
|
66
libc/intrin/mapanon.c
Normal file
66
libc/intrin/mapanon.c
Normal file
|
@ -0,0 +1,66 @@
|
|||
/*-*- 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 2020 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/calls/calls.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/consts/map.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
|
||||
/**
|
||||
* Helper function for allocating anonymous mapping.
|
||||
*
|
||||
* This function is equivalent to:
|
||||
*
|
||||
* mmap(NULL, mapsize, PROT_READ | PROT_WRITE,
|
||||
* MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
*
|
||||
* If mmap() fails, possibly because the parent process did this:
|
||||
*
|
||||
* if (!vfork()) {
|
||||
* setrlimit(RLIMIT_AS, &(struct rlimit){maxbytes, maxbytes});
|
||||
* execv(prog, (char *const[]){prog, 0});
|
||||
* }
|
||||
* wait(0);
|
||||
*
|
||||
* Then this function will call:
|
||||
*
|
||||
* __oom_hook(size);
|
||||
*
|
||||
* If it's linked. The LIBC_TESTLIB library provides an implementation,
|
||||
* which can be installed as follows:
|
||||
*
|
||||
* int main() {
|
||||
* InstallQuotaHandlers();
|
||||
* // ...
|
||||
* }
|
||||
*
|
||||
* That is performed automatically for unit test executables.
|
||||
*
|
||||
* @return memory map address on success, or null w/ errno
|
||||
*/
|
||||
void *_mapanon(size_t size) {
|
||||
void *m;
|
||||
m = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
if (m != MAP_FAILED)
|
||||
return m;
|
||||
if (errno == ENOMEM && _weaken(__oom_hook))
|
||||
_weaken(__oom_hook)(size);
|
||||
return 0;
|
||||
}
|
|
@ -52,6 +52,7 @@ void *__maps_randaddr(void);
|
|||
void *__maps_pickaddr(size_t);
|
||||
void __maps_add(struct Map *);
|
||||
void __maps_free(struct Map *);
|
||||
void __maps_insert(struct Map *);
|
||||
struct Map *__maps_alloc(void);
|
||||
struct Map *__maps_floor(const char *);
|
||||
void __maps_stack(char *, int, int, size_t, int, intptr_t);
|
||||
|
|
|
@ -138,7 +138,7 @@ StartOver:
|
|||
__maps.count -= 1;
|
||||
__maps_check();
|
||||
} else if (IsWindows()) {
|
||||
// you can't carve up memory maps on windows ;_;
|
||||
STRACE("you can't carve up memory maps on windows ;_;");
|
||||
rc = einval();
|
||||
} else if (addr <= map_addr) {
|
||||
// shave off lefthand side of mapping
|
||||
|
@ -246,7 +246,7 @@ static void __maps_free_all(struct Map *list) {
|
|||
}
|
||||
}
|
||||
|
||||
static void __maps_insert(struct Map *map) {
|
||||
void __maps_insert(struct Map *map) {
|
||||
map->flags &= MAP_TYPE | MAP_ANONYMOUS | MAP_NOFORK;
|
||||
|
||||
// coalesce adjacent mappings
|
||||
|
@ -351,12 +351,12 @@ static int __munmap(char *addr, size_t size) {
|
|||
}
|
||||
|
||||
// untrack mappings
|
||||
int rc;
|
||||
struct Map *deleted = 0;
|
||||
__muntrack(addr, pgup_size, pagesz, &deleted);
|
||||
rc = __muntrack(addr, pgup_size, pagesz, &deleted);
|
||||
__maps_unlock();
|
||||
|
||||
// delete mappings
|
||||
int rc = 0;
|
||||
for (struct Map *map = deleted; map; map = map->freed) {
|
||||
if (!IsWindows()) {
|
||||
if (sys_munmap(map->addr, map->size))
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#define SYSDEBUG 0
|
||||
#endif
|
||||
|
||||
#define _NTTRACE 0 /* not configurable w/ flag yet */
|
||||
#define _NTTRACE 1 /* not configurable w/ flag yet */
|
||||
#define _POLLTRACE 0 /* not configurable w/ flag yet */
|
||||
#define _DATATRACE 1 /* not configurable w/ flag yet */
|
||||
#define _LOCKTRACE 0 /* not configurable w/ flag yet */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue