cosmopolitan/libc/sock/sendfile.c

209 lines
8.3 KiB
C
Raw Normal View History

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 net 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
*/
#include "libc/assert.h"
2020-06-15 14:18:57 +00:00
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/sig.internal.h"
2022-05-23 22:06:11 +00:00
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
2020-06-15 14:18:57 +00:00
#include "libc/dce.h"
#include "libc/errno.h"
2022-09-13 11:32:29 +00:00
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/safemacros.internal.h"
2022-09-13 11:32:29 +00:00
#include "libc/intrin/strace.internal.h"
#include "libc/macros.internal.h"
#include "libc/nt/enum/filetype.h"
#include "libc/nt/enum/wait.h"
#include "libc/nt/errors.h"
#include "libc/nt/files.h"
#include "libc/nt/struct/byhandlefileinformation.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"
2020-06-15 14:18:57 +00:00
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"
// sendfile() isn't specified as raising eintr
static textwindows int SendfileBlock(int64_t handle,
struct NtOverlapped *overlapped) {
int rc;
uint32_t i, got, flags = 0;
if (WSAGetLastError() != kNtErrorIoPending &&
WSAGetLastError() != WSAEINPROGRESS) {
NTTRACE("TransmitFile failed %lm");
return __winsockerr();
}
for (;;) {
i = WSAWaitForMultipleEvents(1, &overlapped->hEvent, true,
__SIG_POLLING_INTERVAL_MS, true);
if (i == kNtWaitFailed) {
NTTRACE("WSAWaitForMultipleEvents failed %lm");
return __winsockerr();
} else if (i == kNtWaitTimeout || i == kNtWaitIoCompletion) {
if (_check_interrupts(true, g_fds.p)) return -1;
#if _NTTRACE
POLLTRACE("WSAWaitForMultipleEvents...");
#endif
} else {
break;
}
}
if (WSAGetOverlappedResult(handle, overlapped, &got, false, &flags)) {
return got;
} else {
return -1;
}
}
static dontinline textwindows ssize_t sys_sendfile_nt(
int outfd, int infd, int64_t *opt_in_out_inoffset, uint32_t uptobytes) {
ssize_t rc;
int64_t ih, oh, pos, eof, offset;
struct NtByHandleFileInformation wst;
if (!__isfdkind(infd, kFdFile)) return ebadf();
if (!__isfdkind(outfd, kFdSocket)) return ebadf();
ih = g_fds.p[infd].handle;
oh = g_fds.p[outfd].handle;
if (!SetFilePointerEx(ih, 0, &pos, SEEK_CUR)) {
return __winerr();
2020-06-15 14:18:57 +00:00
}
if (opt_in_out_inoffset) {
offset = *opt_in_out_inoffset;
} else {
offset = pos;
}
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 {
return ebadf();
}
struct NtOverlapped ov = {
.Pointer = (void *)(intptr_t)offset,
.hEvent = WSACreateEvent(),
};
if (TransmitFile(oh, ih, uptobytes, 0, &ov, 0, 0)) {
rc = uptobytes;
2020-06-15 14:18:57 +00:00
} else {
rc = SendfileBlock(oh, &ov);
}
if (rc != -1) {
if (opt_in_out_inoffset) {
*opt_in_out_inoffset = offset + rc;
_npassert(SetFilePointerEx(ih, pos, 0, SEEK_SET));
} else {
_npassert(SetFilePointerEx(ih, offset + rc, 0, SEEK_SET));
}
2020-06-15 14:18:57 +00:00
}
WSACloseEvent(ov.hEvent);
return rc;
2020-06-15 14:18:57 +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;
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;
}
Undiamond Python headers This change gets the Python codebase into a state where it conforms to the conventions of this codebase. It's now possible to include headers from Python, without worrying about ordering. Python has traditionally solved that problem by "diamonding" everything in Python.h, but that's problematic since it means any change to any Python header invalidates all the build artifacts. Lastly it makes tooling not work. Since it is hard to explain to Emacs when I press C-c C-h to add an import line it shouldn't add the header that actually defines the symbol, and instead do follow the nonstandard Python convention. Progress has been made on letting Python load source code from the zip executable structure via the standard C library APIs. System calss now recognizes zip!FILENAME alternative URIs as equivalent to zip:FILENAME since Python uses colon as its delimiter. Some progress has been made on embedding the notice license terms into the Python object code. This is easier said than done since Python has an extremely complicated ownership story. - Some termios APIs have been added - Implement rewinddir() dirstream API - GetCpuCount() API added to Cosmopolitan Libc - More bugs in Cosmopolitan Libc have been fixed - zipobj.com now has flags for mangling the path - Fixed bug a priori with sendfile() on certain BSDs - Polyfill F_DUPFD and F_DUPFD_CLOEXEC across platforms - FIOCLEX / FIONCLEX now polyfilled for fast O_CLOEXEC changes - APE now supports a hybrid solution to no-self-modify for builds - Many BSD-only magnums added, e.g. O_SEARCH, O_SHLOCK, SF_NODISKIO
2021-08-12 07:42:14 +00:00
if (IsFreebsd()) {
rc = sys_sendfile_freebsd(infd, outfd, offset, uptobytes, 0, &sbytes, 0);
if (rc == -1 && errno == ENOBUFS) errno = ENOMEM;
Undiamond Python headers This change gets the Python codebase into a state where it conforms to the conventions of this codebase. It's now possible to include headers from Python, without worrying about ordering. Python has traditionally solved that problem by "diamonding" everything in Python.h, but that's problematic since it means any change to any Python header invalidates all the build artifacts. Lastly it makes tooling not work. Since it is hard to explain to Emacs when I press C-c C-h to add an import line it shouldn't add the header that actually defines the symbol, and instead do follow the nonstandard Python convention. Progress has been made on letting Python load source code from the zip executable structure via the standard C library APIs. System calss now recognizes zip!FILENAME alternative URIs as equivalent to zip:FILENAME since Python uses colon as its delimiter. Some progress has been made on embedding the notice license terms into the Python object code. This is easier said than done since Python has an extremely complicated ownership story. - Some termios APIs have been added - Implement rewinddir() dirstream API - GetCpuCount() API added to Cosmopolitan Libc - More bugs in Cosmopolitan Libc have been fixed - zipobj.com now has flags for mangling the path - Fixed bug a priori with sendfile() on certain BSDs - Polyfill F_DUPFD and F_DUPFD_CLOEXEC across platforms - FIOCLEX / FIONCLEX now polyfilled for fast O_CLOEXEC changes - APE now supports a hybrid solution to no-self-modify for builds - Many BSD-only magnums added, e.g. O_SEARCH, O_SHLOCK, SF_NODISKIO
2021-08-12 07:42:14 +00:00
} else {
2022-09-13 11:32:29 +00:00
sbytes = uptobytes;
Undiamond Python headers This change gets the Python codebase into a state where it conforms to the conventions of this codebase. It's now possible to include headers from Python, without worrying about ordering. Python has traditionally solved that problem by "diamonding" everything in Python.h, but that's problematic since it means any change to any Python header invalidates all the build artifacts. Lastly it makes tooling not work. Since it is hard to explain to Emacs when I press C-c C-h to add an import line it shouldn't add the header that actually defines the symbol, and instead do follow the nonstandard Python convention. Progress has been made on letting Python load source code from the zip executable structure via the standard C library APIs. System calss now recognizes zip!FILENAME alternative URIs as equivalent to zip:FILENAME since Python uses colon as its delimiter. Some progress has been made on embedding the notice license terms into the Python object code. This is easier said than done since Python has an extremely complicated ownership story. - Some termios APIs have been added - Implement rewinddir() dirstream API - GetCpuCount() API added to Cosmopolitan Libc - More bugs in Cosmopolitan Libc have been fixed - zipobj.com now has flags for mangling the path - Fixed bug a priori with sendfile() on certain BSDs - Polyfill F_DUPFD and F_DUPFD_CLOEXEC across platforms - FIOCLEX / FIONCLEX now polyfilled for fast O_CLOEXEC changes - APE now supports a hybrid solution to no-self-modify for builds - Many BSD-only magnums added, e.g. O_SEARCH, O_SHLOCK, SF_NODISKIO
2021-08-12 07:42:14 +00:00
rc = sys_sendfile_xnu(infd, outfd, offset, &sbytes, 0, 0);
}
if (rc == -1 && errno == ENOTSOCK) errno = EBADF;
Undiamond Python headers This change gets the Python codebase into a state where it conforms to the conventions of this codebase. It's now possible to include headers from Python, without worrying about ordering. Python has traditionally solved that problem by "diamonding" everything in Python.h, but that's problematic since it means any change to any Python header invalidates all the build artifacts. Lastly it makes tooling not work. Since it is hard to explain to Emacs when I press C-c C-h to add an import line it shouldn't add the header that actually defines the symbol, and instead do follow the nonstandard Python convention. Progress has been made on letting Python load source code from the zip executable structure via the standard C library APIs. System calss now recognizes zip!FILENAME alternative URIs as equivalent to zip:FILENAME since Python uses colon as its delimiter. Some progress has been made on embedding the notice license terms into the Python object code. This is easier said than done since Python has an extremely complicated ownership story. - Some termios APIs have been added - Implement rewinddir() dirstream API - GetCpuCount() API added to Cosmopolitan Libc - More bugs in Cosmopolitan Libc have been fixed - zipobj.com now has flags for mangling the path - Fixed bug a priori with sendfile() on certain BSDs - Polyfill F_DUPFD and F_DUPFD_CLOEXEC across platforms - FIOCLEX / FIONCLEX now polyfilled for fast O_CLOEXEC changes - APE now supports a hybrid solution to no-self-modify for builds - Many BSD-only magnums added, e.g. O_SEARCH, O_SHLOCK, SF_NODISKIO
2021-08-12 07:42:14 +00:00
if (rc != -1) {
if (opt_in_out_inoffset) {
*opt_in_out_inoffset += sbytes;
} else {
_npassert(lseek(infd, offset + sbytes, SEEK_SET) == 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
* @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
*/
ssize_t sendfile(int outfd, int infd, int64_t *opt_in_out_inoffset,
2020-06-15 14:18:57 +00:00
size_t uptobytes) {
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);
if (IsAsan() && opt_in_out_inoffset &&
!__asan_is_valid(opt_in_out_inoffset, 8)) {
2022-09-13 11:32:29 +00:00
rc = efault();
} else if (IsLinux()) {
rc = sys_sendfile(outfd, infd, opt_in_out_inoffset, uptobytes);
2020-06-15 14:18:57 +00:00
} else if (IsFreebsd() || IsXnu()) {
rc = sys_sendfile_bsd(outfd, infd, opt_in_out_inoffset, uptobytes);
2020-06-15 14:18:57 +00:00
} else if (IsWindows()) {
rc = sys_sendfile_nt(outfd, infd, opt_in_out_inoffset, uptobytes);
2020-06-15 14:18:57 +00:00
} else {
rc = enosys();
2020-06-15 14:18:57 +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
}