mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-06 19:28:29 +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
|
@ -18,8 +18,10 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/syscall_support-nt.internal.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/itoa.h"
|
||||
#include "libc/intrin/fds.h"
|
||||
#include "libc/intrin/maps.h"
|
||||
#include "libc/intrin/strace.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nt/files.h"
|
||||
|
@ -27,7 +29,7 @@
|
|||
#include "libc/nt/struct/startupinfo.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
|
||||
#define FDS_VAR "_COSMO_FDS="
|
||||
#define FDS_VAR "_COSMO_FDS_V2="
|
||||
|
||||
#define MAX_ENTRY_BYTES 256
|
||||
|
||||
|
@ -99,6 +101,8 @@ textwindows char *__describe_fds(const struct Fd *fds, size_t fdslen,
|
|||
if (__is_cloexec(f))
|
||||
continue;
|
||||
++handlecount;
|
||||
if (f->cursor)
|
||||
++handlecount;
|
||||
}
|
||||
if (!(handles = calloc(handlecount, sizeof(*handles)))) {
|
||||
OnFailure:
|
||||
|
@ -116,17 +120,31 @@ textwindows char *__describe_fds(const struct Fd *fds, size_t fdslen,
|
|||
// make inheritable version of handle exist in creator process
|
||||
if (!DuplicateHandle(GetCurrentProcess(), f->handle, hCreatorProcess,
|
||||
&handle, 0, true, kNtDuplicateSameAccess)) {
|
||||
STRACE("__describe_fds() DuplicateHandle() failed w/ %d", GetLastError());
|
||||
__winerr();
|
||||
goto OnFailure;
|
||||
}
|
||||
for (uint32_t i = 0; i < 3; ++i) {
|
||||
if (lpStartupInfo->stdiofds[i] == f->handle) {
|
||||
for (uint32_t i = 0; i < 3; ++i)
|
||||
if (lpStartupInfo->stdiofds[i] == f->handle)
|
||||
lpStartupInfo->stdiofds[i] = handle;
|
||||
}
|
||||
}
|
||||
handles[hi++] = handle;
|
||||
|
||||
// get shared memory handle for the file offset pointer
|
||||
intptr_t shand = 0;
|
||||
if (f->cursor) {
|
||||
struct Map *map;
|
||||
if (!(map = __maps_floor((const char *)f->cursor->shared)) ||
|
||||
map->addr != (const char *)f->cursor->shared) {
|
||||
errno = EFAULT;
|
||||
goto OnFailure;
|
||||
}
|
||||
if (!DuplicateHandle(GetCurrentProcess(), map->hand, hCreatorProcess,
|
||||
&shand, 0, true, kNtDuplicateSameAccess)) {
|
||||
__winerr();
|
||||
goto OnFailure;
|
||||
}
|
||||
handles[hi++] = shand;
|
||||
}
|
||||
|
||||
// ensure output string has enough space for new entry
|
||||
if (sb.i + MAX_ENTRY_BYTES > sb.n) {
|
||||
char *p2;
|
||||
|
@ -151,12 +169,7 @@ textwindows char *__describe_fds(const struct Fd *fds, size_t fdslen,
|
|||
*p++ = '_';
|
||||
p = FormatInt64(p, f->mode);
|
||||
*p++ = '_';
|
||||
//
|
||||
// - v1 abi: This field was originally the file pointer.
|
||||
//
|
||||
// - v2 abi: This field is the negated shared memory address.
|
||||
//
|
||||
p = FormatInt64(p, -(uintptr_t)f->shared);
|
||||
p = FormatInt64(p, shand);
|
||||
*p++ = '_';
|
||||
p = FormatInt64(p, f->type);
|
||||
*p++ = '_';
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
#include "libc/nt/struct/startupinfo.h"
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
#define CURSOR_ADDRESS_FLAG 0x4000000000000000
|
||||
|
||||
bool __is_cloexec(const struct Fd *) libcesque;
|
||||
void __undescribe_fds(int64_t, int64_t *, uint32_t) libcesque;
|
||||
char *__describe_fds(const struct Fd *, size_t, struct NtStartupInfo *, int64_t,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue