`getopt_long` returns an `int` not a `char`. On aarch64 `char` is
unsigned but on x86 is signed. This is why it had worked so far without
any issues.
Signed-off-by: Anhad Singh <andypython@protonmail.com>
`posix_fallocate` ensures that a byte range in a file is allocated so
that subsequent writes don't fail. Unlike ftruncate, posix_fallocate
does not shrink files.
The Linux syscall fallocate is similar to posix_fallocate except with
far more control over how byte ranges are allocated (e.g. it supports
file holes and other features). This MR doesn't implement fallocate as
it requires syscall and redoxfs support.
Finally, I changed the flags for flock from usize to c_int. That matches
what we have in libc and also avoids some silly, needless type casting.
Like the other "at" functions, renameat renames files with respect with
a directory descriptor. "renameat2" is Linux specific but really nice -
it adds a flag which supports atomically swapping two files or failing
if the target already exists. The latter is easily supported in relibc.
The former requires redoxfs support.
Besides the impl itself, I refactored "cap_path_at" into what it really
is: a mini openat2-like function.
Capability based `fchmod`. I also implemented unit tests for chmod,
fchmod, and fchmodat.
The unit tests check Redox-specific behavior, so some of the tests are
disabled for Linux.
`fdopendir` takes ownership of a directory file descriptor instead of
opening one with a path. `fdclosedir` is a FreeBSD extension that
transfers ownership of the file descriptor in DIR to the user.
AT_SYMLINK_NOFOLLOW acts on the symlink itself rather than following it.
AT_EMPTY_PATH acts on the directory if path is empty.
Currently, this is implemented in fstatat itself but the code is general
enough to be refactored once we get the other *at functions. It could
likely just live in openat itself.
Closes: #212
The fix is simply to not follow links when opening a file to be renamed.
O_NOFOLLOW, a non-POSIX extension, does exactly that while not needing
renameat or openat.
Linux's variant uses the syscall as intended. Redox's variant uses fpath
to build a path to pass to fstat from the file descriptor plus the file
name. Unlike the syscall, this isn't atomic so the liminal space between
fpath/getcwd and fstat is subject to TOCTOU.
Beyond fstatat, I moved the stat test to its correct location and added
an assert since the output of the test is unchecked.
I also added AT_FDCWD which seems to be -100 across Unixes. The other
AT_* constants are unimplemented for now.
The implementation for confstr is straightforward. Most of the constants
return 1 on both musl and glibc. The only constant that doesn't is
required for Fish.
I also switched an #[unsafe(no_mangle)] from my last patch back
to a #[no_mangle] because we need to bump cbindgen. #[unsafe(no_mangle)]
is required for Rust 2024.
Most platforms use a c_int though glibc uses a u32. GNU defines a type,
__rlimit_resource_t, for the RLIMIT enum that is a u32.
Using a c_int is nicer for a few reasons. The first is that our
(unimplemented) functions receive a c_int for the enum - using a u32 is
technically wrong but doesn't affect anything since the constants are
turned into macros by cbindgen.
The second reason is that a c_int is nicer for libc and nix (the crate)
too since we don't need to pollute the crates with guards for Redox.
%m is a format specifier that prints an error string for errno. The
specifier is technically only for syslog, but musl and glibc implement
it for printf itself. Parsing for a single specifier in a single
function is error prone, especially when syslog itself is variadic.
* Fix PERROR to match musl/glibc better
* More unit tests + enabled on Linux
* Pack priority and facility into one i32 and check the bits with
bitflags
* Add LOG_UPTO (logic is straight from musl)
Not done:
* "%m" - this could just be added to printf
* LOG_CONS
Linux's syslog is a local socket, so this uses the recent UDS work.
* Most of redox.rs is refactored into mod.rs now which has all of the C
facing functions and consts.
* I wrapped the global logger in a Mutex instead of a RwLock. The logger
is almost always locked for writing so a Mutex is simpler as RwLock
provides no benefits.
* I implemented LOG_PERROR which also prints errors to stderr as well as
the log.
* Syslog should be sys/syslog.h with syslog.h as an alias (the
original code only had syslog.h).
This test should rename disabled until redox-os/relibc#212 is fixed. The
test works on Linux so it should work on Redox eventually too.
The issue is non-trivial because it involves a syscall, frename, that
may need to be redesigned. frename requires a file descriptor, but
opening a file resolves links which in turn fails with broken symlinks.