cosmopolitan/libc/sock/sendfile.c

147 lines
6.1 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
*/
2022-08-11 19:13:18 +00:00
#include "libc/intrin/safemacros.internal.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"
#include "libc/calls/strace.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/intrin/kprintf.h"
#include "libc/nt/enum/wait.h"
#include "libc/nt/errors.h"
#include "libc/nt/files.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) {
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) {
_check_interrupts(true, g_fds.p);
#if _NTTRACE
POLLTRACE("WSAWaitForMultipleEvents...");
#endif
} else {
break;
}
}
if (!WSAGetOverlappedResult(handle, overlapped, &got, false, &flags)) {
NTTRACE("WSAGetOverlappedResult failed %lm");
return __winsockerr();
}
return got;
}
static textwindows ssize_t sendfile_linux2nt(int outfd, int infd,
2020-06-15 14:18:57 +00:00
int64_t *inout_opt_inoffset,
size_t uptobytes) {
ssize_t rc;
int64_t offset;
struct NtOverlapped overlapped;
if (!__isfdkind(outfd, kFdSocket)) return ebadf();
if (!__isfdkind(infd, kFdFile)) return ebadf();
2020-06-15 14:18:57 +00:00
if (inout_opt_inoffset) {
offset = *inout_opt_inoffset;
} else if (!SetFilePointerEx(g_fds.p[infd].handle, 0, &offset, SEEK_CUR)) {
return __winerr();
2020-06-15 14:18:57 +00:00
}
bzero(&overlapped, sizeof(overlapped));
overlapped.Pointer = (void *)(intptr_t)offset;
overlapped.hEvent = WSACreateEvent();
2020-06-15 14:18:57 +00:00
if (TransmitFile(g_fds.p[outfd].handle, g_fds.p[infd].handle, uptobytes, 0,
&overlapped, 0, 0)) {
rc = uptobytes;
2020-06-15 14:18:57 +00:00
} else {
rc = SendfileBlock(g_fds.p[outfd].handle, &overlapped);
}
if (rc != -1 && inout_opt_inoffset) {
*inout_opt_inoffset = offset + rc;
2020-06-15 14:18:57 +00:00
}
WSACloseEvent(overlapped.hEvent);
return rc;
2020-06-15 14:18:57 +00:00
}
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
static ssize_t sendfile_linux2bsd(int outfd, int infd,
int64_t *inout_opt_inoffset,
size_t uptobytes) {
2020-06-15 14:18:57 +00:00
int rc;
int64_t offset, sbytes;
if (inout_opt_inoffset) {
offset = *inout_opt_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);
} else {
rc = sys_sendfile_xnu(infd, outfd, offset, &sbytes, 0, 0);
}
if (rc != -1) {
2020-06-15 14:18:57 +00:00
if (inout_opt_inoffset) *inout_opt_inoffset += sbytes;
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 inout_opt_inoffset may be specified for pread()-like behavior
* @param uptobytes is usually the number of bytes remaining in file; it
* can't exceed INT_MAX-1; some platforms block until everything's
* sent, whereas others won't; zero isn't allowed
* @return number of bytes transmitted which may be fewer than requested
* @see copy_file_range() for file file
* @see splice() for fd pipe
*/
ssize_t sendfile(int outfd, int infd, int64_t *inout_opt_inoffset,
size_t uptobytes) {
if (!uptobytes) return einval();
if (uptobytes > 0x7ffffffe /* Microsoft's off-by-one */) return eoverflow();
if (IsLinux()) {
return sys_sendfile(outfd, infd, inout_opt_inoffset, uptobytes);
2020-06-15 14:18:57 +00:00
} else if (IsFreebsd() || IsXnu()) {
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
return sendfile_linux2bsd(outfd, infd, inout_opt_inoffset, uptobytes);
2020-06-15 14:18:57 +00:00
} else if (IsWindows()) {
return sendfile_linux2nt(outfd, infd, inout_opt_inoffset, uptobytes);
2020-06-15 14:18:57 +00:00
} else {
return copyfd(infd, inout_opt_inoffset, outfd, NULL, uptobytes, 0);
}
}