Skip to content

Commit 1c1fc80

Browse files
authored
Miscellaneous cleanups (#672)
* Miscellaneous cleanups - Add more comments. - Fix a few clippy lints. * Define `SIGEMT` on more platforms. * Mark `sigwait` etc. as `unsafe`. * Use `CLOEXEC` in code examples that call `openat`.
1 parent c2699e3 commit 1c1fc80

File tree

11 files changed

+88
-27
lines changed

11 files changed

+88
-27
lines changed

build.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ fn main() {
8282
use_feature("linux_raw");
8383
use_feature_or_nothing("core_intrinsics");
8484

85-
// Use inline asm if we have it, or outline asm otherwise. On PowerPC
86-
// and MIPS, Rust's inline asm is considered experimental, so only use
87-
// it if `--cfg=rustix_use_experimental_asm` is given.
85+
// Use inline asm if we have it, or outline asm otherwise. On 32-bit
86+
// x86 our asm support requires naked functions. On PowerPC and MIPS,
87+
// Rust's inline asm is considered experimental, so only use it if
88+
// `--cfg=rustix_use_experimental_asm` is given.
8889
if (feature_rustc_dep_of_std || vendor == "mustang" || can_compile("use std::arch::asm;"))
8990
&& (arch != "x86" || has_feature("naked_functions"))
9091
&& ((arch != "powerpc64" && arch != "mips" && arch != "mips64")

examples/kq.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ fn main() -> std::io::Result<()> {
1010
let mut out = Vec::with_capacity(10);
1111

1212
#[cfg(feature = "fs")]
13-
let dir = fs::openat(fs::cwd(), ".", fs::OFlags::DIRECTORY, fs::Mode::empty())?;
13+
let dir = fs::openat(
14+
fs::cwd(),
15+
".",
16+
fs::OFlags::RDONLY | fs::OFlags::DIRECTORY | fs::OFlags::CLOEXEC,
17+
fs::Mode::empty(),
18+
)?;
1419

1520
let subs = [
1621
#[cfg(feature = "process")]

src/backend/libc/process/types.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,19 @@ pub enum Signal {
205205
#[doc(alias = "Unused")]
206206
Sys = c::SIGSYS,
207207
/// `SIGEMT`
208-
#[cfg(bsd)]
208+
#[cfg(any(bsd, solarish, target_os = "aix", target_os = "hermit",))]
209209
Emt = c::SIGEMT,
210+
/// `SIGEMT`
211+
#[cfg(all(
212+
any(target_os = "android", target_os = "linux"),
213+
any(
214+
target_arch = "mips",
215+
target_arch = "mips64",
216+
target_arch = "sparc",
217+
target_arch = "sparc64"
218+
)
219+
))]
220+
Emt = linux_raw_sys::general::SIGEMT as i32,
210221
/// `SIGINFO`
211222
#[cfg(bsd)]
212223
Info = c::SIGINFO,

src/backend/linux_raw/net/addr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl SocketAddrUnix {
5555
let len = len.try_into().unwrap();
5656
Ok(Self { unix, len })
5757
} else {
58-
return Err(io::Errno::NAMETOOLONG);
58+
Err(io::Errno::NAMETOOLONG)
5959
}
6060
}
6161

src/backend/linux_raw/process/types.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ pub enum Signal {
104104
#[doc(alias = "Iot")]
105105
#[doc(alias = "Abrt")]
106106
Abort = linux_raw_sys::general::SIGABRT,
107+
/// `SIGEMT`
108+
#[cfg(any(
109+
target_arch = "mips",
110+
target_arch = "mips64",
111+
target_arch = "sparc",
112+
target_arch = "sparc64"
113+
))]
114+
Emt = linux_raw_sys::general::SIGEMT,
107115
/// `SIGBUS`
108116
Bus = linux_raw_sys::general::SIGBUS,
109117
/// `SIGFPE`

src/fs/raw_dir.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ impl<'buf, Fd: AsFd> RawDir<'buf, Fd> {
4545
/// # use std::mem::MaybeUninit;
4646
/// # use rustix::fs::{cwd, Mode, OFlags, openat, RawDir};
4747
///
48-
/// let fd = openat(cwd(), ".", OFlags::RDONLY | OFlags::DIRECTORY, Mode::empty()).unwrap();
48+
/// let fd = openat(
49+
/// cwd(),
50+
/// ".",
51+
/// OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
52+
/// Mode::empty(),
53+
/// )
54+
/// .unwrap();
4955
///
5056
/// let mut buf = Vec::with_capacity(8192);
5157
/// let mut iter = RawDir::new(fd, buf.spare_capacity_mut());
@@ -64,7 +70,7 @@ impl<'buf, Fd: AsFd> RawDir<'buf, Fd> {
6470
/// let fd = openat(
6571
/// cwd(),
6672
/// ".",
67-
/// OFlags::RDONLY | OFlags::DIRECTORY,
73+
/// OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
6874
/// Mode::empty(),
6975
/// )
7076
/// .unwrap();
@@ -88,7 +94,13 @@ impl<'buf, Fd: AsFd> RawDir<'buf, Fd> {
8894
/// # use rustix::fs::{cwd, Mode, OFlags, openat, RawDir};
8995
/// # use rustix::io::Errno;
9096
///
91-
/// let fd = openat(cwd(), ".", OFlags::RDONLY | OFlags::DIRECTORY, Mode::empty()).unwrap();
97+
/// let fd = openat(
98+
/// cwd(),
99+
/// ".",
100+
/// OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
101+
/// Mode::empty(),
102+
/// )
103+
/// .unwrap();
92104
///
93105
/// let mut buf = Vec::with_capacity(8192);
94106
/// 'read: loop {

src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
//! - Constants use `enum`s and [`bitflags`] types.
6060
//! - Multiplexed functions (eg. `fcntl`, `ioctl`, etc.) are de-multiplexed.
6161
//! - Variadic functions (eg. `openat`, etc.) are presented as non-variadic.
62+
//! - Functions that return strings automatically allocate sufficient memory
63+
//! and retry the syscall as needed to determine the needed length.
6264
//! - Functions and types which need `l` prefixes or `64` suffixes to enable
6365
//! large-file support (LFS) are used automatically. File sizes and offsets
6466
//! are always presented as `u64` and `i64`.
@@ -109,10 +111,9 @@
109111
#![cfg_attr(alloc_c_string, feature(alloc_ffi))]
110112
#![cfg_attr(alloc_c_string, feature(alloc_c_string))]
111113
#![cfg_attr(not(feature = "std"), no_std)]
112-
#![cfg_attr(feature = "rustc-dep-of-std", feature(core_intrinsics))]
113114
#![cfg_attr(feature = "rustc-dep-of-std", feature(ip))]
114115
#![cfg_attr(
115-
all(not(feature = "rustc-dep-of-std"), core_intrinsics),
116+
any(feature = "rustc-dep-of-std", core_intrinsics),
116117
feature(core_intrinsics)
117118
)]
118119
#![cfg_attr(asm_experimental_arch, feature(asm_experimental_arch))]

src/runtime.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,25 @@ pub unsafe fn arm_set_tls(data: *mut c_void) -> io::Result<()> {
8585
backend::runtime::syscalls::tls::arm_set_tls(data)
8686
}
8787

88+
/// `prctl(PR_SET_FS, data)`—Set the x86_64 `fs` register.
89+
///
90+
/// # Safety
91+
///
92+
/// This is a very low-level feature for implementing threading libraries.
93+
/// See the references links above.
8894
#[cfg(linux_raw)]
8995
#[cfg(target_arch = "x86_64")]
9096
#[inline]
9197
pub unsafe fn set_fs(data: *mut c_void) {
9298
backend::runtime::syscalls::tls::set_fs(data)
9399
}
94100

101+
/// Set the x86_64 thread ID address.
102+
///
103+
/// # Safety
104+
///
105+
/// This is a very low-level feature for implementing threading libraries.
106+
/// See the references links above.
95107
#[cfg(linux_raw)]
96108
#[inline]
97109
pub unsafe fn set_tid_address(data: *mut c_void) -> Pid {
@@ -384,36 +396,54 @@ pub unsafe fn sigprocmask(how: How, set: Option<&Sigset>) -> io::Result<Sigset>
384396

385397
/// `sigwait(set)`—Wait for signals.
386398
///
399+
/// # Safety
400+
///
401+
/// If code elsewhere in the process is depending on delivery of a signal to
402+
/// prevent it from executing some code, this could cause it to miss that
403+
/// signal and execute that code.
404+
///
387405
/// # References
388406
/// - [Linux]
389407
///
390408
/// [Linux]: https://man7.org/linux/man-pages/man3/sigwait.3.html
391409
#[cfg(linux_raw)]
392410
#[inline]
393-
pub fn sigwait(set: &Sigset) -> io::Result<Signal> {
411+
pub unsafe fn sigwait(set: &Sigset) -> io::Result<Signal> {
394412
backend::runtime::syscalls::sigwait(set)
395413
}
396414

397415
/// `sigwait(set)`—Wait for signals, returning a [`Siginfo`].
398416
///
417+
/// # Safety
418+
///
419+
/// If code elsewhere in the process is depending on delivery of a signal to
420+
/// prevent it from executing some code, this could cause it to miss that
421+
/// signal and execute that code.
422+
///
399423
/// # References
400424
/// - [Linux]
401425
///
402426
/// [Linux]: https://man7.org/linux/man-pages/man2/sigwaitinfo.2.html
403427
#[cfg(linux_raw)]
404428
#[inline]
405-
pub fn sigwaitinfo(set: &Sigset) -> io::Result<Siginfo> {
429+
pub unsafe fn sigwaitinfo(set: &Sigset) -> io::Result<Siginfo> {
406430
backend::runtime::syscalls::sigwaitinfo(set)
407431
}
408432

409433
/// `sigtimedwait(set)`—Wait for signals, optionally with a timeout.
410434
///
435+
/// # Safety
436+
///
437+
/// If code elsewhere in the process is depending on delivery of a signal to
438+
/// prevent it from executing some code, this could cause it to miss that
439+
/// signal and execute that code.
440+
///
411441
/// # References
412442
/// - [Linux]
413443
///
414444
/// [Linux]: https://man7.org/linux/man-pages/man2/sigtimedwait.2.html
415445
#[cfg(linux_raw)]
416446
#[inline]
417-
pub fn sigtimedwait(set: &Sigset, timeout: Option<Timespec>) -> io::Result<Siginfo> {
447+
pub unsafe fn sigtimedwait(set: &Sigset, timeout: Option<Timespec>) -> io::Result<Siginfo> {
418448
backend::runtime::syscalls::sigtimedwait(set, timeout)
419449
}

src/termios/tty.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
//! Functions which operate on file descriptors which might be terminals.
22
33
use crate::backend;
4-
#[cfg(any(
5-
all(linux_raw, feature = "procfs"),
6-
all(libc, not(any(target_os = "fuchsia", target_os = "wasi"))),
7-
))]
8-
use crate::io;
94
use backend::fd::AsFd;
10-
#[cfg(any(
11-
all(linux_raw, feature = "procfs"),
12-
all(libc, not(any(target_os = "fuchsia", target_os = "wasi"))),
13-
))]
5+
#[cfg(feature = "procfs")]
6+
#[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))]
147
use {
15-
crate::ffi::CString, crate::path::SMALL_PATH_BUFFER_SIZE, alloc::vec::Vec,
8+
crate::ffi::CString, crate::io, crate::path::SMALL_PATH_BUFFER_SIZE, alloc::vec::Vec,
169
backend::fd::BorrowedFd,
1710
};
1811

tests/fs/makedev.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustix::fs::{major, makedev, minor};
22

33
#[test]
44
fn makedev_roundtrip() {
5-
// Apple's, FreeBSD 11's, and Dragonfly's `makedev` doesn't handle extra
5+
// Apple's, FreeBSD 11's, and DragonFly's `makedev` doesn't handle extra
66
// bits set.
77
#[cfg(freebsdlike)]
88
let (maj, min) = (0x0000_0026, 0x6564_0061);

0 commit comments

Comments
 (0)