2020-06-15 14:18:57 +00:00
|
|
|
/*-*- 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 │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-09-19 22:01:48 +00:00
|
|
|
#include "libc/assert.h"
|
2023-10-13 01:53:17 +00:00
|
|
|
#include "libc/atomic.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/calls.h"
|
|
|
|
#include "libc/calls/internal.h"
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
#include "libc/calls/struct/sigset.internal.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
2023-10-13 01:53:17 +00:00
|
|
|
#include "libc/cosmo.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/dce.h"
|
2022-09-19 22:01:48 +00:00
|
|
|
#include "libc/errno.h"
|
|
|
|
#include "libc/intrin/describeflags.h"
|
2022-09-13 11:32:29 +00:00
|
|
|
#include "libc/intrin/strace.h"
|
2023-10-13 01:53:17 +00:00
|
|
|
#include "libc/nt/enum/wsaid.h"
|
2022-05-26 05:29:10 +00:00
|
|
|
#include "libc/nt/errors.h"
|
2023-10-13 01:53:17 +00:00
|
|
|
#include "libc/nt/events.h"
|
2022-05-26 05:29:10 +00:00
|
|
|
#include "libc/nt/files.h"
|
2022-09-19 22:01:48 +00:00
|
|
|
#include "libc/nt/struct/byhandlefileinformation.h"
|
2023-10-13 01:53:17 +00:00
|
|
|
#include "libc/nt/struct/guid.h"
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
#include "libc/nt/struct/overlapped.h"
|
2023-10-13 01:53:17 +00:00
|
|
|
#include "libc/nt/thunk/msabi.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/winsock.h"
|
|
|
|
#include "libc/sock/internal.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/sock/sendfile.internal.h"
|
2024-09-13 06:01:20 +00:00
|
|
|
#include "libc/sock/syscall_fd.internal.h"
|
2023-10-13 01:53:17 +00:00
|
|
|
#include "libc/sock/wsaid.internal.h"
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
#include "libc/stdio/sysparam.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
2022-05-26 05:29:10 +00:00
|
|
|
|
2023-10-13 01:53:17 +00:00
|
|
|
static struct {
|
|
|
|
atomic_uint once;
|
|
|
|
errno_t err;
|
|
|
|
bool32 (*__msabi lpTransmitFile)(
|
|
|
|
int64_t hSocket, int64_t hFile, uint32_t opt_nNumberOfBytesToWrite,
|
|
|
|
uint32_t opt_nNumberOfBytesPerSend,
|
|
|
|
struct NtOverlapped *opt_inout_lpOverlapped,
|
|
|
|
const struct NtTransmitFileBuffers *opt_lpTransmitBuffers,
|
|
|
|
uint32_t dwReserved);
|
|
|
|
} g_transmitfile;
|
|
|
|
|
|
|
|
static void transmitfile_init(void) {
|
|
|
|
static struct NtGuid TransmitfileGuid = WSAID_TRANSMITFILE;
|
|
|
|
g_transmitfile.lpTransmitFile = __get_wsaid(&TransmitfileGuid);
|
|
|
|
}
|
|
|
|
|
2024-09-13 06:01:20 +00:00
|
|
|
textwindows dontinline static ssize_t sys_sendfile_nt(
|
2022-09-19 22:01:48 +00:00
|
|
|
int outfd, int infd, int64_t *opt_in_out_inoffset, uint32_t uptobytes) {
|
2022-05-26 05:29:10 +00:00
|
|
|
ssize_t rc;
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
uint32_t flags = 0;
|
2024-08-03 08:24:46 +00:00
|
|
|
bool locked = false;
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
int64_t ih, oh, eof, offset;
|
2022-09-19 22:01:48 +00:00
|
|
|
struct NtByHandleFileInformation wst;
|
2024-08-04 00:48:00 +00:00
|
|
|
if (!__isfdkind(infd, kFdFile) || !g_fds.p[infd].cursor)
|
2022-05-26 05:29:10 +00:00
|
|
|
return ebadf();
|
2022-09-19 22:01:48 +00:00
|
|
|
if (!__isfdkind(outfd, kFdSocket))
|
|
|
|
return ebadf();
|
|
|
|
ih = g_fds.p[infd].handle;
|
|
|
|
oh = g_fds.p[outfd].handle;
|
|
|
|
if (opt_in_out_inoffset) {
|
|
|
|
offset = *opt_in_out_inoffset;
|
|
|
|
} else {
|
2024-08-03 08:24:46 +00:00
|
|
|
locked = true;
|
2024-08-04 00:48:00 +00:00
|
|
|
__cursor_lock(g_fds.p[infd].cursor);
|
|
|
|
offset = g_fds.p[infd].cursor->shared->pointer;
|
2022-09-19 22:01:48 +00:00
|
|
|
}
|
|
|
|
if (GetFileInformationByHandle(ih, &wst)) {
|
|
|
|
// TransmitFile() returns EINVAL if `uptobytes` goes past EOF.
|
|
|
|
eof = (uint64_t)wst.nFileSizeHigh << 32 | wst.nFileSizeLow;
|
|
|
|
if (offset + uptobytes > eof) {
|
|
|
|
uptobytes = eof - offset;
|
|
|
|
}
|
|
|
|
} else {
|
2024-08-03 08:24:46 +00:00
|
|
|
if (locked)
|
2024-08-04 00:48:00 +00:00
|
|
|
__cursor_unlock(g_fds.p[infd].cursor);
|
2022-09-19 22:01:48 +00:00
|
|
|
return ebadf();
|
|
|
|
}
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
struct NtOverlapped ov = {.hEvent = WSACreateEvent(), .Pointer = offset};
|
2023-10-13 01:53:17 +00:00
|
|
|
cosmo_once(&g_transmitfile.once, transmitfile_init);
|
|
|
|
if (g_transmitfile.lpTransmitFile(oh, ih, uptobytes, 0, &ov, 0, 0) ||
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
WSAGetLastError() == kNtErrorIoPending ||
|
|
|
|
WSAGetLastError() == WSAEINPROGRESS) {
|
|
|
|
if (WSAGetOverlappedResult(oh, &ov, &uptobytes, true, &flags)) {
|
|
|
|
rc = uptobytes;
|
|
|
|
if (opt_in_out_inoffset) {
|
|
|
|
*opt_in_out_inoffset = offset + rc;
|
|
|
|
} else {
|
2024-08-04 00:48:00 +00:00
|
|
|
g_fds.p[infd].cursor->shared->pointer = offset + rc;
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
}
|
2022-09-19 22:01:48 +00:00
|
|
|
} else {
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
rc = __winsockerr();
|
2022-09-19 22:01:48 +00:00
|
|
|
}
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
} else {
|
|
|
|
rc = __winsockerr();
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2024-08-03 08:24:46 +00:00
|
|
|
if (locked)
|
2024-08-04 00:48:00 +00:00
|
|
|
__cursor_unlock(g_fds.p[infd].cursor);
|
2022-09-19 22:01:48 +00:00
|
|
|
WSACloseEvent(ov.hEvent);
|
2022-05-26 05:29:10 +00:00
|
|
|
return rc;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2022-09-19 22:01:48 +00:00
|
|
|
static ssize_t sys_sendfile_bsd(int outfd, int infd,
|
|
|
|
int64_t *opt_in_out_inoffset,
|
|
|
|
size_t uptobytes) {
|
|
|
|
ssize_t rc;
|
2020-06-15 14:18:57 +00:00
|
|
|
int64_t offset, sbytes;
|
2022-09-19 22:01:48 +00:00
|
|
|
if (opt_in_out_inoffset) {
|
|
|
|
offset = *opt_in_out_inoffset;
|
2021-02-07 14:11:44 +00:00
|
|
|
} else if ((offset = lseek(infd, 0, SEEK_CUR)) == -1) {
|
2020-06-15 14:18:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2021-08-12 07:42:14 +00:00
|
|
|
if (IsFreebsd()) {
|
|
|
|
rc = sys_sendfile_freebsd(infd, outfd, offset, uptobytes, 0, &sbytes, 0);
|
2022-09-19 22:01:48 +00:00
|
|
|
if (rc == -1 && errno == ENOBUFS)
|
|
|
|
errno = ENOMEM;
|
2021-08-12 07:42:14 +00:00
|
|
|
} else {
|
2022-09-13 11:32:29 +00:00
|
|
|
sbytes = uptobytes;
|
2021-08-12 07:42:14 +00:00
|
|
|
rc = sys_sendfile_xnu(infd, outfd, offset, &sbytes, 0, 0);
|
|
|
|
}
|
2022-09-19 22:01:48 +00:00
|
|
|
if (rc == -1 && errno == ENOTSOCK)
|
|
|
|
errno = EBADF;
|
2021-08-12 07:42:14 +00:00
|
|
|
if (rc != -1) {
|
2022-09-19 22:01:48 +00:00
|
|
|
if (opt_in_out_inoffset) {
|
|
|
|
*opt_in_out_inoffset += sbytes;
|
|
|
|
} else {
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
unassert(sys_lseek(infd, offset + sbytes, SEEK_SET, 0) ==
|
|
|
|
offset + sbytes);
|
2022-09-13 11:32:29 +00:00
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
return sbytes;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transfers data from file to network.
|
|
|
|
*
|
|
|
|
* @param outfd needs to be a socket
|
|
|
|
* @param infd needs to be a file
|
2022-09-19 22:01:48 +00:00
|
|
|
* @param opt_in_out_inoffset may be specified for pread()-like behavior
|
|
|
|
* in which case the file position won't be changed; otherwise, this
|
|
|
|
* shall read from the file pointer which is advanced accordingly
|
|
|
|
* @param uptobytes is the maximum number of bytes to send; some platforms
|
|
|
|
* block until everything's sent, whereas others won't; the behavior of
|
|
|
|
* zero is undefined; this value may overlap the end of file in which
|
|
|
|
* case what remains is sent; this is silently reduced to `0x7ffff000`
|
|
|
|
* @return number of bytes transmitted which may be fewer than requested in
|
|
|
|
* which case caller must be prepared to call sendfile() again
|
|
|
|
* @raise ESPIPE on Linux RHEL7+ if offset is used but `infd` isn't seekable,
|
|
|
|
* otherwise this could be EINVAL
|
|
|
|
* @raise EPIPE on most systems if socket has been shutdown for reading or
|
|
|
|
* the remote end closed the connection, otherwise this could be EINVAL
|
|
|
|
* @raise EBADF if `outfd` isn't a valid writeable stream sock descriptor
|
|
|
|
* @raise EAGAIN if `O_NONBLOCK` is in play and it would have blocked
|
|
|
|
* @raise EBADF if `infd` isn't a valid readable file descriptor
|
|
|
|
* @raise EFAULT if `opt_in_out_inoffset` is a bad pointer
|
|
|
|
* @raise EINVAL if `*opt_in_out_inoffset` is negative
|
|
|
|
* @raise EOVERFLOW is documented as possible on Linux
|
|
|
|
* @raise EIO if `infd` had a low-level i/o error
|
|
|
|
* @raise ENOMEM if we require more vespene gas
|
|
|
|
* @raise ENOTCONN if `outfd` isn't connected
|
|
|
|
* @raise ENOSYS on NetBSD and OpenBSD
|
2020-06-15 14:18:57 +00:00
|
|
|
* @see copy_file_range() for file ↔ file
|
|
|
|
* @see splice() for fd ↔ pipe
|
|
|
|
*/
|
2022-09-19 22:01:48 +00:00
|
|
|
ssize_t sendfile(int outfd, int infd, int64_t *opt_in_out_inoffset,
|
2020-06-15 14:18:57 +00:00
|
|
|
size_t uptobytes) {
|
2022-09-19 22:01:48 +00:00
|
|
|
ssize_t rc;
|
|
|
|
|
|
|
|
// We must reduce this due to the uint32_t type conversion on Windows
|
|
|
|
// which has a maximum of 0x7ffffffe. It also makes sendfile(..., -1)
|
|
|
|
// less error prone, since Linux may EINVAL if greater than INT64_MAX
|
|
|
|
uptobytes = MIN(uptobytes, 0x7ffff000);
|
|
|
|
|
2024-06-22 12:45:49 +00:00
|
|
|
if (IsLinux()) {
|
2022-09-19 22:01:48 +00:00
|
|
|
rc = sys_sendfile(outfd, infd, opt_in_out_inoffset, uptobytes);
|
2020-06-15 14:18:57 +00:00
|
|
|
} else if (IsFreebsd() || IsXnu()) {
|
2022-09-19 22:01:48 +00:00
|
|
|
rc = sys_sendfile_bsd(outfd, infd, opt_in_out_inoffset, uptobytes);
|
2020-06-15 14:18:57 +00:00
|
|
|
} else if (IsWindows()) {
|
2024-08-03 08:24:46 +00:00
|
|
|
BLOCK_SIGNALS;
|
2022-09-19 22:01:48 +00:00
|
|
|
rc = sys_sendfile_nt(outfd, infd, opt_in_out_inoffset, uptobytes);
|
2024-08-03 08:24:46 +00:00
|
|
|
ALLOW_SIGNALS;
|
2020-06-15 14:18:57 +00:00
|
|
|
} else {
|
2022-09-19 22:01:48 +00:00
|
|
|
rc = enosys();
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-09-19 22:01:48 +00:00
|
|
|
|
|
|
|
STRACE("sendfile(%d, %d, %p, %'zu) → %ld% m", outfd, infd,
|
|
|
|
DescribeInOutInt64(rc, opt_in_out_inoffset), uptobytes, rc);
|
2022-09-13 11:32:29 +00:00
|
|
|
return rc;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2023-09-21 14:30:39 +00:00
|
|
|
|
|
|
|
__weak_reference(sendfile, sendfile64);
|