The following functions were replaced with their *at variants or
similar:
- lstat -> lstat -> fstatat
- stat -> stat -> fstatat
- fstat -> fstatat
- fchmod -> fchmodat
- fchown -> fchownat
- lchown -> fchownat
- mkfifo -> mkfifoat
- open -> openat
- renameat -> renameat2
- rmdir -> unlinkat
- unlink -> unlinkat
- symlink-> symlinkat
The `open_flags` logic for fstatat was redundant, as this is already
handled (and better so) in `openat2`.
Additionally, the fstatat test succeeds in os-test, and no longer
returns EINVAL. This is because `O_SYMLINK` is no longer
unconditionally passed like it was before. This is a problem because
redoxfs returns EINVAL if a node isn't a symlink but set `O_SYMLINK`.
Most of these changes are very simple. Among the changes made involve
taking advantage of auto-deref (`(*val).foo()` -> `val.foo()`) and
removing instances where we create a ref and immediately dereference it
(`&*val` -> `val`). There was a pretty neat case in `posix_openpt` where
some pointer verbosity was able to be reduced by using the more modern C
strings rather than the byte strings with an explicit NUL at the end.
Additionally, `exit()` now calls `unreachable!()` at the end. We
previously did `loop {}`, but clippy didn't like this. It can be up for
debate whether we want to make this `unreachable_unchecked` or similar.
There is only one change that might cause any sort of concern, and that
is the change from `.skip_while(!p).next()` -> `.find(p)`. This, like
everything else, was caught in a Clippy lint but I believe it deserves
some explanation because it isn't immediately obvious. Info about the
lint is here: https://rust-lang.github.io/rust-clippy/rust-1.89.0/index.html#skip_while_next
I noticed that the Linux and Redox PALs both do very similar things for
a given FS function and its *at variant. For example, `mkdir()` is just
a call to `mkdirat()`. POSIX requires these to be equivalent.
Additionally, we use AT_EMPTY_PATH in some places but note that this is
not POSIX and is instead an extension that Linux (and Redox) implement.
That doesn't really matter though, since this is an implementation
detail. Implementations can choose to implement these functions anyway
and ignore the default impl. Such a case is `fstat`, because the current
Redox impl of `fstatat` relies on `fstat`, and this would cause infinite
recursion.
Future work:
POSIX says that `fcntl(fd, F_DUPFD, 0);` shall be equivalent to
`dup(fd);`. `dup2` might have cases where it can be implemented using
`dup3`. `dup` seemingly cannot be implemented in terms of `dup2`, so the
`fcntl` default implementation is sufficient. `pipe(fds)` is equivalent to
`pipe2(fds, 0);`.
Additionally, we were using the `access` syscall for x86_64 for our
linux PAL. I looked at the linux source and found that using the
`access`
syscall is equivalent to using `faccessat` as we do now: https://elixir.bootlin.com/linux/v6.17.5/source/fs/open.c#L550
Also, some of the `fcntl.h` functions were accidentally implemented and exported from `unistd.h`. They are supposed to be implemented in `fcntl.h` and exported to `unistd.h`. This is now the behavior.
`linkat(2)` doesn't have `AT_EMPTY_PATH` as a valid flag in this
implementation because it isn't POSIX. We have it (and have
support for it), but it is more effort to add it. If we need it at some
point, it can be added in about 3 lines.
`openat(2)` previously wasn't exposed, and William was not aware of
`Sys::openat`'s existence. We use it under the hood for `mkfifoat(2)`
and friends, so expose it as a libc API. This helps to pass more os-test tests.
Lastly, the 3 implemented syscalls here help pass some os-test tests.
Linux's variant uses the syscall as intended. Redox's variant uses fpath to build a path to pass to mkdir from the file descriptor plus the file name, which isn't atomic due to the fpath lookup being subject to TOCTOU when paired with mkdir.
Init no longer changes the default scheme to initfs at any point in
time. And for sandboxing you would be switching scheme namespace, not
default scheme.
It should be possible to mix and match relibc version from before and
after this change without breaking exec, though I haven't tested it.
`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.
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.
As we know, vectors amortize the cost of adding new elements by reserving
space for multiple elements when full. This is useful but may lead to
allocating more memory than necessary.
`relibc` generally avoids over allocations by reserving the exact amount
of space when possible. I fixed a few areas that still over allocated or
reallocated unnecessarily by leveraging iterators that are more likely
to know sizes.