2021-07-19 21:55:20 +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 2021 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. │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/syscall-nt.internal.h"
|
|
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
2021-08-12 07:42:14 +00:00
|
|
|
#include "libc/dce.h"
|
2021-08-18 21:21:30 +00:00
|
|
|
#include "libc/intrin/asan.internal.h"
|
2022-05-12 13:43:59 +00:00
|
|
|
#include "libc/intrin/describeflags.internal.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
|
|
|
#include "libc/intrin/weaken.h"
|
2022-05-18 23:41:29 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2023-08-12 06:14:02 +00:00
|
|
|
#include "libc/runtime/zipos.internal.h"
|
2023-09-07 05:39:36 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
2021-08-12 07:42:14 +00:00
|
|
|
|
|
|
|
/**
|
2021-08-18 21:21:30 +00:00
|
|
|
* Reads symbolic link.
|
|
|
|
*
|
|
|
|
* This does *not* nul-terminate the buffer.
|
2021-08-12 07:42:14 +00:00
|
|
|
*
|
2021-08-18 21:21:30 +00:00
|
|
|
* @param dirfd is normally AT_FDCWD but if it's an open directory and
|
|
|
|
* file is a relative path, then file is opened relative to dirfd
|
|
|
|
* @param path must be a symbolic link pathname
|
|
|
|
* @param buf will receive symbolic link contents, and won't be modified
|
|
|
|
* unless the function succeeds (with the exception of no-malloc nt)
|
2022-04-20 16:56:53 +00:00
|
|
|
* and this buffer will *not* be nul-terminated
|
2021-08-18 21:21:30 +00:00
|
|
|
* @return number of bytes written to buf, or -1 w/ errno; if the
|
|
|
|
* return is equal to bufsiz then truncation may have occurred
|
2023-09-07 05:39:36 +00:00
|
|
|
* @raise EINVAL if path isn't a symbolic link
|
|
|
|
* @raise ENOENT if `path` didn't exist
|
|
|
|
* @raise ENOTDIR if parent component existed that's not a directory
|
|
|
|
* @raise ENOTDIR if base component ends with slash and is not a dir
|
|
|
|
* @raise ENAMETOOLONG if symlink-resolved `path` length exceeds `PATH_MAX`
|
|
|
|
* @raise ENAMETOOLONG if component in `path` exists longer than `NAME_MAX`
|
|
|
|
* @raise EBADF on relative `path` when `dirfd` isn't open or `AT_FDCWD`
|
|
|
|
* @raise ELOOP if a loop was detected resolving parent components
|
2021-08-18 21:21:30 +00:00
|
|
|
* @asyncsignalsafe
|
2021-08-12 07:42:14 +00:00
|
|
|
*/
|
2021-08-18 21:21:30 +00:00
|
|
|
ssize_t readlinkat(int dirfd, const char *path, char *buf, size_t bufsiz) {
|
2021-10-14 00:27:13 +00:00
|
|
|
ssize_t bytes;
|
2023-09-07 05:39:36 +00:00
|
|
|
if ((bufsiz && !buf) || (IsAsan() && (!__asan_is_valid_str(path) ||
|
|
|
|
!__asan_is_valid(buf, bufsiz)))) {
|
2021-10-14 00:27:13 +00:00
|
|
|
bytes = efault();
|
2022-09-13 06:10:38 +00:00
|
|
|
} else if (_weaken(__zipos_notat) &&
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
(bytes = __zipos_notat(dirfd, path)) == -1) {
|
2022-07-20 21:01:15 +00:00
|
|
|
STRACE("TODO: zipos support for readlinkat");
|
2021-10-14 00:27:13 +00:00
|
|
|
} else if (!IsWindows()) {
|
|
|
|
bytes = sys_readlinkat(dirfd, path, buf, bufsiz);
|
2021-08-12 07:42:14 +00:00
|
|
|
} else {
|
2021-10-14 00:27:13 +00:00
|
|
|
bytes = sys_readlinkat_nt(dirfd, path, buf, bufsiz);
|
2021-08-12 07:42:14 +00:00
|
|
|
}
|
2022-06-26 01:17:31 +00:00
|
|
|
STRACE("readlinkat(%s, %#s, [%#.*s]) → %d% m", DescribeDirfd(dirfd), path,
|
Support non-blocking i/o across platforms
This change introduces new tests for `O_NONBLOCK` and `SOCK_NONBLOCK` to
confirm that non-blocking i/o is now working on all supported platforms,
including Windows. For example, you can now say on Windows, MacOS, etc.:
socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
To create a non-blocking IPv4 TCP socket. Or you can enable non-blocking
i/o on an existing socket / pipe / etc. file descriptor by calling fcntl
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
This functionality is polyfilled on older Linux kernels too, e.g. RHEL5.
Now that fcntl() support is much better the FIOCLEX / FIONCLEX polyfills
for ioctl() have been removed since they're ugly non-POSIX diameond APIs
This change fixes a weakness in kprintf() that was causing Windows trace
tools to frequently crash.
2023-07-23 09:56:47 +00:00
|
|
|
(int)MAX(0, bytes), buf, bytes);
|
2021-10-14 00:27:13 +00:00
|
|
|
return bytes;
|
2021-08-12 07:42:14 +00:00
|
|
|
}
|