Commit Graph

236 Commits

Author SHA1 Message Date
Dan Gohman 54d04c9bc8 Support preadv2 and pwritev2 on all non-glibc Linux ABIs. (#489)
Use `libc::syscall` on ABIs where libc doesn't have bindings for
`preadv2` and `pwritev2`.
2022-12-26 20:56:54 -08:00
Alex Saveau 0b844fc778 Fix another UB in raw dir (#474)
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
2022-12-13 10:40:42 -08:00
Dan Gohman 5225e27a1f Add From impls for SocketAddrAny. (#482)
This allows more convenient construction of `SocketAddrAny` values.
2022-12-13 10:27:53 -08:00
Dan Gohman 3502eb064b Fix the custom definition of Timespec to implement Copy. (#484) 2022-12-13 10:25:25 -08:00
Dan Gohman 4e2599b729 Run the tests on Rust 1.48, and check compilation of tests and examples on more targets (#465)
* Fix the tests to run on 1.48.

This puts the benchmarks behind a `criterion` cfg, so add
`--cfg=criterion` to RUSTFLAGS when running cargo bench.

An MSRV of 1.48 enables us to support users such as async-io, which also
has an MSRV of 1.48.

* Add a rust-version field.

This prints a warning under Rust 1.48, but only when rustix is the
top-level build; when rustix is a dependency, it doesn't warn, so this
seems ok.
2022-12-01 11:42:09 -08:00
Alex Saveau 2aff00c77e Fix UB due to misaligned RawDir pointer (#464)
* Fix UB due to misaligned RawDir pointer

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>

* Add alignment and stack tests to RawDir

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
2022-11-29 17:05:39 -08:00
Dan Gohman 8fca74091c Tidy up cfgs. 2022-11-28 08:58:36 -08:00
Alex Saveau b5c2ea7127 Add zero-compromise directory iteration (#457)
* Add zero-compromise directory iteration

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>

* Address review feedback

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>

* Vastly simplify file_name calculation and remove pointer manipulation BS, yielding a 2x instruction count reduction

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>

* Implement docs changes I mentioned

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>

* Fix debug impl

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>

* Support libc backend

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>

* Clarify portability of fixed-sized buffers

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>

* Turns out from_ptr is faster, oh well

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>

* Make cookie a u64

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>

* Fix cfgs

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
2022-11-23 15:42:07 -08:00
Dan Gohman c7146a721e Fix compilation with --no-default-features. (#454)
Move `fcntl_getfd`, `fcntl_setfd`, and `fcntl_dupfd_cloexec` out of
the `fs` module and into the `io` module, since they're not specific
to files, directories or memfd. This allows them to be used with the
"fs" feature is not enabled.

Also, add public aliases for then in `fs`, for compatibility, and to
have a place where all `fcntl` function are present.

And add a CI check for --no-default-features with no API features.

Fixes #453.
2022-11-21 10:56:46 -08:00
Dan Gohman 8b5196283a Fix test compilation when "threads" or "procfs" aren't enabled. 2022-11-18 08:19:32 -08:00
Dan Gohman 2fc4f1be16 Disable the internal fs module when the "fs" feature isn't enabled. (#449)
Rustix no longer has anything in the tree that needs the `fs` module
when the public "fs" feature isn't enabled, so make the `fd` module
conditional on `cfg(feature = "fs")`.
2022-11-16 12:19:50 -08:00
Dan Gohman 12dd67ff6a Enable the "close" feature for io-lifetimes. (#440)
* Enable the "close" feature for io-lifetimes.

This follows up on #422, but enables "close" on the regular dependency
too, and not just the dev-dependency.

* Temporarily disable the backends no-libc test.
2022-11-07 17:39:05 -08:00
niluxv 760c0b517c Add part of the FreeBSD procctl API (#429) 2022-11-07 07:39:44 -08:00
nivkner e3dd07d50b add splice syscall (#421)
* add splice function

* add documentation to splice function

* add libc backend for splice

* update linux-raw-sys

* add linux_raw backend

* export splice items

* add tests for splice

* add cfgs

* remove unsafe vmsplice flag

* add IoSliceRaw to linux backend

* add IoSliceRaw to libc backend

* export IoSliceRaw

* add vmsplice function

* implement linux backend vmsplice

* implement libc backend vmsplice

* export vmsplice

* change description of splice

* add documentation of vmsplice

* add vmsplice tests

* Revert "remove unsafe vmsplice flag"

This reverts commit 9a2781e1d91d091e3f069c3c48d7e4a9bb51a45c.

* add vmsplice gift safety docs

* add lifetime parameter to IoSliceRaw

* change splice offsets to in-out args
2022-10-20 14:30:00 -07:00
Dan Gohman b72d4291d2 Add a proc_self_status function for reading /proc/self/status on Linux.
Fixes #423.
2022-10-12 13:27:49 -07:00
Dan Gohman 350877a391 Fix compilation on sparc targets. (#417)
* Fix compilation on sparc targets.

Adjust cfg's as needed to fix compilation errors on sparc.

Fixes #408.

* Rust's 32-bit sparc Linux target has a 32-bit time_t.
2022-09-27 10:05:29 -07:00
Dan Gohman a4434a6c12 Fix various compilation errors on haiku. (#411)
This builds on top of #407!
2022-09-25 18:20:34 -07:00
Dan Gohman e5d9aa159f More fixes for compilation on x86_64-unknown-dragonfly. (#410)
Add a tier-3 CI check for x86_64-unknown-dragonfly, and fix the
compilation errors needed to compile with --features=all-apis.

This builds on top of #409!
2022-09-25 13:15:28 -07:00
Koutheir Attouchi 974f83d24b Add support for setns(). (#398) 2022-09-20 10:15:24 -07:00
Koutheir Attouchi e328c738bc Add support for prctl(). 2022-09-16 07:14:37 -07:00
gco ccaebe7c3a Fix compilation errors on solaris. 2022-09-07 14:05:12 -07:00
Dan Gohman 325d679ee4 Remove rustix::io::OwnedFd.
Remove rustix::io::OwnedFd, and just use the std and io_lifetimes'
OwnedFd definition. Now that rustix is using io_lifetimes 1.0, which has
switched to using the `OwnedFd` in std, this is no longer needed.

This simplifies rustix' API, as users no longer have to be aware of the
difference between rustix::io::OwnedFd and rustix::fd::OwnedFd.
2022-08-20 08:30:47 -07:00
Dan Gohman 9dfc94bcba Update to io-lifetimes 1.0.
The main change here is the removal of `FromFd` and `IntoFd` in favor of
`From<OwnedFd>` and `impl<T> or OwnedFd`.

This also adds impls for std types for rustix::io::OwnedFd to make it
less inconvenient to work with.
2022-08-17 16:20:04 -07:00
Dan Gohman b0c34b998c Make the linux-raw-sys dependency non-optional on Linux.
Rustix's statx code now depends on linux-raw-sys, in order to obtain
Linux's `STATX__RESERVED` value, which musl doesn't export. As such,
always enable the linux-raw-sys dependency on Linux targets.

Fixes #389.
2022-08-17 14:00:41 -07:00
Dan Gohman 4eed038018 Switch from .init_array constructors to /proc/self/auxv. (#385)
* Switch from `.init_array` constructors to /proc/self/auxv.

In the linux_raw backend, switch from using a `.init_array` constructor
for obtaining the aux values to reading them from /proc/self/auxv. This avoids
problems in situation where other Rust code can run before the constructor,
potentially distrupting the `__environ` value.

Also, for the linux_raw backend, introduce a new "use-libc-auxv" feature,
which enables use of libc to read the aux values, instead of reading
them from /proc/self/auxv.

The "use-libc-auxv" option is enabled by default, because it's more
efficient and doesn't depend on /proc, so it's likely better for most
users.

Mustang, for its part, continues to be able to use the incoming auxv on
the stack because it controls program startup. Since it doesn't have to
worry about the hazards of /proc or QEMU, it can trust the incoming
values, and do less checking.

Fixes #382.

* Remove the param `init` function from the libc backend.

* Fix the no-std build.

* Fix the backends test to accept that the default options now depend on libc.

* Use `NonNull` in `check_raw_pointer`'s return type.

This allows it to pack the return value into a single pointer-sized
value.

* Update more code to the new `check_raw_pointer` API.

* Fix an unused-import warning.

* Fix copy+paste.

* Thread `check_elf_base` through `check_vdso_base` too.
2022-08-08 11:06:28 -07:00
Dan Gohman 26baf41126 Add a Result to getpgid, and add getpgrp. (#386)
* Add a `Result` to `getpgid`, and add `getpgrp`.

Add a `Result` return type to `getpgid`, as it can fail if there's no
process with the given pid.

And, add a a `getpgrp` function, which is similar to `getpgid`, but
doesn't take a pid argument and always operates on the current process,
which means it doesn't need a `Result` return type.

* Use `getpgid` on platforms which don't have a `getpgrp` syscall.
2022-07-26 05:49:50 -07:00
carbotaniuman ad5b66e657 Add getpgid (#380)
* Add getpgid
2022-07-12 20:41:40 -07:00
Dan Gohman 4aae063274 Guard against future statx fields. (#379)
* Guard against future `statx` fields.

Future versions of Linux may recognize new `statx` mask flags, which
enable writing to new `struct statx` fields. If a rustix user constructs
a flags value with `from_bits_unchecked`, they could potentially create
a flags value that causes `statx` on such a future Linux version to write
fields outside the buffer, evoking undefined behavior.

This is very unlikely to be a practical problem today, because:

 - Scanning all known public users of rustix, I don't see any constructing
   `StatxFlags` values this way.

 - It's unlikely that any rustix user would ever have a need to construct
   `StatxFlags` values this way.

 - No existing version of Linux increases the size of `struct statx`
   beyond what rustix currently knows about.

 - `struct statx` contains several spare fields, including a
   `__u64 __spare3[12]` specifically for expansion, so even if a future
   Linux version defines new fields, it has lots of space available before
   it needs to start increasing the size of `struct statx`.
2022-07-12 20:39:31 -07:00
Dan Gohman ec92ea492e Fix miscellaneous clippy lints. 2022-07-07 07:38:07 -07:00
Dan Gohman e82a2c1ea5 Implement statvfs and fstatvfs. (#375)
* Implement `statvfs` and `fstatvfs`.

On libc, just go with whatever the libc `statvfs` gives us. On
linux_raw, translate the fields from `statfs` in the manner of
libc implementations.

* Preserve as much of the `f_fsid` field as possible.

* Document the relationship between `statfs` and `statvfs`.

* Add a `StatVfsMountFlags` type for the `f_flag` field.
2022-07-03 22:23:37 -07:00
Dan Gohman 17835de254 Fix compilation of a test on FreeBSD. 2022-07-01 07:33:34 -07:00
Dan Gohman 14ae449ee5 Sync rustix with Windows Unix ioctls.
Define `FIONREAD` on Windows.
2022-06-15 15:46:31 -07:00
Dan Gohman 8c59ab2c5e Fix typos in comments. 2022-06-14 21:42:56 -07:00
Dan Gohman 2882c02b78 Don't assume that the tests are connected to a tty. 2022-06-14 16:19:55 -07:00
Dan Gohman 7c3fc231b5 Fix the error values the libc backend ttyname implementation.
libc'c `ttyname_r` returns its error code rather than setting `errno`;
fix the wrapper code accordingly.
2022-06-14 16:19:55 -07:00
Dan Gohman d8a8fb9658 Fail with INVAL when tv_nsec requests are out of range. 2022-06-14 16:19:44 -07:00
Dan Gohman acbc8d7571 Rename with_retrying to retry_on_intr.
This makes it more explicit what it's doing.
2022-06-14 16:19:44 -07:00
Dan Gohman 717541870d Use consistent syntax for array elements and lengths. 2022-06-14 16:19:44 -07:00
Dan Gohman 1bb71fbf50 Fix a few clippy lints. 2022-06-14 16:19:44 -07:00
Dan Gohman 801a48149f Code formatting cleanups.
Wrap long lines that were hidden from rustfmt by macros, be consistent
about trailing commas in `cfg`s, alphabetize operands to `any`/`all`,
and add some comments.
2022-06-14 16:19:44 -07:00
Dan Gohman 11630efc0d Add more cfg's to fix the build with various combinations of features. 2022-06-13 15:49:17 -07:00
Dan Gohman c298a1cef4 Make cwd and io_uring_register_files_skip const functions
Fixes #316.
2022-06-06 15:51:00 -07:00
Dan Gohman 6c6a86c6fa Avoid using .init_array and __environ on non-musl non-glibc platforms. (#339)
* Avoid using `.init_array` and `__environ` on non-musl non-glibc platforms.

Don't assume that non-musl non-glibc platforms have `.init_array` and
`__environ`; use an explicit `init` function on such platforms, and add a
`debug_assert` to check that it's called.

* Add a safety comment.

* Move the auxv functions into a new top-level "param" module.

"param" is short for "process parameter".

* Use `CStr` instead of `ZStr`.
2022-06-03 06:28:23 -07:00
Dan Gohman ffd2ef83a4 Remove ZStr and ZString. (#337)
* Remove `ZStr` and `ZString`.

Use `CStr` and `CString` instead, now that nightly has then in core and
alloc, respectively.

Fixes #336.

* Enable "core_c_str" on nightly to use core::ffi::CStr.

* Don't test --no-default-features on stable.

--no-default-features disables "std", and building without "std" will
require nightly now.

* `CString` and `NulError` are in alloc.

* Add comments to the relevant Cargo.toml features.

* rustfmt
2022-05-27 07:37:16 -07:00
Dan Gohman fc82d704bc Change dup2's second operand from &OwnedFd to &mut OwnedFd.
And similar for `dup3`.

The idea behind using `&OwnedFd` is that `dup2`'s second operand isn't like a
normal borrow. It effectively closes the old file descriptor, and creates a
new one with the same index. This could break assumptions of classes that have
an `AsFd` to allow users to do special I/O operations, but which don't expect
users can close and reopen their file descriptor as some completely unrelated
resource.

However, the existence of things like [`FilelikeView`], as well as the
`ManuallyDrop` pattern, mean that `&OwnedFd` doesn't actually prevent
users from using `dup2` on a `BorrowedFd`.

With sunfishcode/io-lifetimes#32 though, `&mut OwnedFd` would be
sufficient, because it removes the `DerefMut` implementation.

So change `rustix` stance to be that `dup2` requires `&mut OwnedFd`.

This means that it's no longer possible to pass the same file descriptor
to both operands of `dup2` or `dup3` with safe Rust, which means it's not
possible to observe the difference in behavior in that case, so remove
the `dup3.rs` test.

[`FilelikeView`]: https://docs.rs/io-lifetimes/latest/io_lifetimes/views/struct.FilelikeView.html
2022-05-26 12:41:01 -07:00
Dan Gohman 0483d272e9 Tidy up more comments. 2022-05-16 09:03:19 -07:00
Dan Gohman 971b363ee4 Add comments. (#319)
* Add comments.

* Fix a redundant word.

* Fix more redundant words.

* Fix a heading level in README.md.

* Fix line-wrapping in README.md.

* Fix typos in comments.

* Replace tabs with spaces.

* Fix a broken doc link.

* Reformat some comments.

* Add more comments.

* Name the linux_raw backend consistently.

* Add some comments to the vDSO parsing code.
2022-05-16 05:34:45 -07:00
Dan Gohman ac7a407740 Add some more miscellaneous tests. 2022-05-16 04:06:25 -07:00
Dan Gohman a84a2db5a1 Remove some unneeded unused_imports attributes. 2022-05-16 04:00:59 -07:00
Dan Gohman 243e8c5197 Rename rustix::io::Error to rustix::io::Errno.
Fixes #254.
2022-05-16 04:00:10 -07:00