Skip to content

Commit e48b93c

Browse files
authored
Rename some runtime functions to kernel_*. (#1342)
To help avoid confusion with functions that have the same name as libc functions but have behavior that differs from similarly-named libc functions, rename `runtime::fork` and similar to `runtime::kernel_fork` and similar. Also rename types such as `Sigaction` and similar to `KernelSigaction` and similar. And rename `SIGRTMIN`/`SIGRTMAX` to `KERNEL_SIGRTMIN`/`KERNEL_SIGRTMAX`. These make it easier to keep these are the kernel items straight from the similarly-named libc structures.
1 parent c33e4f6 commit e48b93c

2 files changed

Lines changed: 75 additions & 56 deletions

File tree

src/backend/linux_raw/runtime/syscalls.rs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,19 @@ use crate::ffi::CStr;
2121
use crate::fs::AtFlags;
2222
use crate::io;
2323
use crate::pid::{Pid, RawPid};
24-
use crate::runtime::{Fork, How, Sigaction, Siginfo, Sigset, Stack};
24+
use crate::runtime::{Fork, How, KernelSigSet, KernelSigaction, SigSet, Siginfo, Stack};
2525
use crate::signal::Signal;
2626
use crate::timespec::Timespec;
2727
use crate::utils::option_as_ptr;
2828
use core::ffi::c_void;
2929
use core::mem::MaybeUninit;
3030
#[cfg(target_pointer_width = "32")]
3131
use linux_raw_sys::general::__kernel_old_timespec;
32-
use linux_raw_sys::general::kernel_sigset_t;
3332
#[cfg(target_arch = "x86_64")]
3433
use linux_raw_sys::general::ARCH_SET_FS;
3534

3635
#[inline]
37-
pub(crate) unsafe fn fork() -> io::Result<Fork> {
36+
pub(crate) unsafe fn kernel_fork() -> io::Result<Fork> {
3837
let mut child_pid = MaybeUninit::<RawPid>::uninit();
3938

4039
// Unix `fork` only returns the child PID in the parent; we'd like it in
@@ -148,15 +147,18 @@ pub(crate) mod tls {
148147
}
149148

150149
#[inline]
151-
pub(crate) unsafe fn sigaction(signal: Signal, new: Option<Sigaction>) -> io::Result<Sigaction> {
152-
let mut old = MaybeUninit::<Sigaction>::uninit();
150+
pub(crate) unsafe fn kernel_sigaction(
151+
signal: Signal,
152+
new: Option<KernelSigaction>,
153+
) -> io::Result<KernelSigaction> {
154+
let mut old = MaybeUninit::<KernelSigaction>::uninit();
153155
let new = option_as_ptr(new.as_ref());
154156
ret(syscall!(
155157
__NR_rt_sigaction,
156158
signal,
157159
new,
158160
&mut old,
159-
size_of::<kernel_sigset_t, _>()
161+
size_of::<KernelSigSet, _>()
160162
))?;
161163
Ok(old.assume_init())
162164
}
@@ -175,73 +177,76 @@ pub(crate) unsafe fn tkill(tid: Pid, sig: Signal) -> io::Result<()> {
175177
}
176178

177179
#[inline]
178-
pub(crate) unsafe fn sigprocmask(how: How, new: Option<&Sigset>) -> io::Result<Sigset> {
179-
let mut old = MaybeUninit::<Sigset>::uninit();
180+
pub(crate) unsafe fn kernel_sigprocmask(
181+
how: How,
182+
new: Option<&KernelSigSet>,
183+
) -> io::Result<KernelSigSet> {
184+
let mut old = MaybeUninit::<KernelSigSet>::uninit();
180185
let new = option_as_ptr(new);
181186
ret(syscall!(
182187
__NR_rt_sigprocmask,
183188
how,
184189
new,
185190
&mut old,
186-
size_of::<kernel_sigset_t, _>()
191+
size_of::<KernelSigSet, _>()
187192
))?;
188193
Ok(old.assume_init())
189194
}
190195

191196
#[inline]
192-
pub(crate) fn sigpending() -> Sigset {
193-
let mut pending = MaybeUninit::<Sigset>::uninit();
197+
pub(crate) fn sigpending() -> SigSet {
198+
let mut pending = MaybeUninit::<SigSet>::uninit();
194199
unsafe {
195200
ret_infallible(syscall!(
196201
__NR_rt_sigpending,
197202
&mut pending,
198-
size_of::<kernel_sigset_t, _>()
203+
size_of::<SigSet, _>()
199204
));
200205
pending.assume_init()
201206
}
202207
}
203208

204209
#[inline]
205-
pub(crate) fn sigsuspend(set: &Sigset) -> io::Result<()> {
210+
pub(crate) fn sigsuspend(set: &SigSet) -> io::Result<()> {
206211
unsafe {
207212
ret(syscall_readonly!(
208213
__NR_rt_sigsuspend,
209214
by_ref(set),
210-
size_of::<kernel_sigset_t, _>()
215+
size_of::<SigSet, _>()
211216
))
212217
}
213218
}
214219

215220
#[inline]
216-
pub(crate) fn sigwait(set: &Sigset) -> io::Result<Signal> {
221+
pub(crate) fn sigwait(set: &SigSet) -> io::Result<Signal> {
217222
unsafe {
218223
Ok(Signal::from_raw_unchecked(ret_c_int(syscall_readonly!(
219224
__NR_rt_sigtimedwait,
220225
by_ref(set),
221226
zero(),
222227
zero(),
223-
size_of::<kernel_sigset_t, _>()
228+
size_of::<SigSet, _>()
224229
))?))
225230
}
226231
}
227232

228233
#[inline]
229-
pub(crate) fn sigwaitinfo(set: &Sigset) -> io::Result<Siginfo> {
234+
pub(crate) fn sigwaitinfo(set: &SigSet) -> io::Result<Siginfo> {
230235
let mut info = MaybeUninit::<Siginfo>::uninit();
231236
unsafe {
232237
let _signum = ret_c_int(syscall!(
233238
__NR_rt_sigtimedwait,
234239
by_ref(set),
235240
&mut info,
236241
zero(),
237-
size_of::<kernel_sigset_t, _>()
242+
size_of::<SigSet, _>()
238243
))?;
239244
Ok(info.assume_init())
240245
}
241246
}
242247

243248
#[inline]
244-
pub(crate) fn sigtimedwait(set: &Sigset, timeout: Option<Timespec>) -> io::Result<Siginfo> {
249+
pub(crate) fn sigtimedwait(set: &SigSet, timeout: Option<Timespec>) -> io::Result<Siginfo> {
245250
let mut info = MaybeUninit::<Siginfo>::uninit();
246251
let timeout_ptr = option_as_ptr(timeout.as_ref());
247252

@@ -255,7 +260,7 @@ pub(crate) fn sigtimedwait(set: &Sigset, timeout: Option<Timespec>) -> io::Resul
255260
by_ref(set),
256261
&mut info,
257262
timeout_ptr,
258-
size_of::<kernel_sigset_t, _>()
263+
size_of::<SigSet, _>()
259264
)) {
260265
Ok(_signum) => (),
261266
Err(io::Errno::NOSYS) => sigtimedwait_old(set, timeout, &mut info)?,
@@ -271,15 +276,15 @@ pub(crate) fn sigtimedwait(set: &Sigset, timeout: Option<Timespec>) -> io::Resul
271276
by_ref(set),
272277
&mut info,
273278
timeout_ptr,
274-
size_of::<kernel_sigset_t, _>()
279+
size_of::<SigSet, _>()
275280
))?;
276281
Ok(info.assume_init())
277282
}
278283
}
279284

280285
#[cfg(target_pointer_width = "32")]
281286
unsafe fn sigtimedwait_old(
282-
set: &Sigset,
287+
set: &SigSet,
283288
timeout: Option<Timespec>,
284289
info: &mut MaybeUninit<Siginfo>,
285290
) -> io::Result<()> {
@@ -298,7 +303,7 @@ unsafe fn sigtimedwait_old(
298303
by_ref(set),
299304
info,
300305
old_timeout_ptr,
301-
size_of::<kernel_sigset_t, _>()
306+
size_of::<SigSet, _>()
302307
))?;
303308

304309
Ok(())
@@ -310,7 +315,7 @@ pub(crate) fn exit_group(code: c::c_int) -> ! {
310315
}
311316

312317
#[inline]
313-
pub(crate) unsafe fn brk(addr: *mut c::c_void) -> io::Result<*mut c_void> {
318+
pub(crate) unsafe fn kernel_brk(addr: *mut c::c_void) -> io::Result<*mut c_void> {
314319
// This is non-`readonly`, to prevent loads from being reordered past it.
315320
ret_void_star(syscall!(__NR_brk, addr))
316321
}

src/runtime.rs

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,38 +62,49 @@ pub use crate::signal::Signal;
6262
/// If we want to expose this in public APIs, we should encapsulate the
6363
/// `linux_raw_sys` type.
6464
#[cfg(linux_raw)]
65-
pub type Sigaction = linux_raw_sys::general::kernel_sigaction;
65+
pub use linux_raw_sys::general::kernel_sigaction as KernelSigaction;
6666

6767
/// `stack_t`
6868
///
6969
/// If we want to expose this in public APIs, we should encapsulate the
7070
/// `linux_raw_sys` type.
7171
#[cfg(linux_raw)]
72-
pub type Stack = linux_raw_sys::general::stack_t;
72+
pub use linux_raw_sys::general::stack_t as Stack;
7373

74-
/// `sigset_t`.
74+
/// `sigset_t`
7575
///
76-
/// Undefined behavior could happen in some functions if `Sigset` ever
77-
/// contains signal numbers in the range from
78-
/// `linux_raw_sys::general::SIGRTMIN` to what the libc thinks `SIGRTMIN` is.
79-
/// Unless you are implementing the libc. Which you may indeed be doing, if
80-
/// you're reading this.
76+
/// Undefined behavior could happen in some functions if `SigSet` ever
77+
/// contains signal numbers in the range from `KERNEL_SIGRTMIN` to the libc
78+
/// `SIGRTMIN`. Unless you are implementing the libc. Which you may indeed be
79+
/// doing, if you're reading this.
8180
///
8281
/// If we want to expose this in public APIs, we should encapsulate the
8382
/// `linux_raw_sys` type.
8483
#[cfg(linux_raw)]
85-
pub type Sigset = linux_raw_sys::general::kernel_sigset_t;
84+
pub use linux_raw_sys::general::sigset_t as SigSet;
85+
86+
/// `kernel_sigset_t`.
87+
///
88+
/// Undefined behavior could happen in some functions if `KernelSigSet` ever
89+
/// contains signal numbers in the range from `KERNEL_SIGRTMIN` to the libc
90+
/// `SIGRTMIN`. Unless you are implementing the libc. Which you may indeed be
91+
/// doing, if you're reading this.
92+
///
93+
/// If we want to expose this in public APIs, we should encapsulate the
94+
/// `linux_raw_sys` type.
95+
#[cfg(linux_raw)]
96+
pub use linux_raw_sys::general::kernel_sigset_t as KernelSigSet;
8697

8798
/// `siginfo_t`
8899
///
89100
/// If we want to expose this in public APIs, we should encapsulate the
90101
/// `linux_raw_sys` type.
91102
#[cfg(linux_raw)]
92-
pub type Siginfo = linux_raw_sys::general::siginfo_t;
103+
pub use linux_raw_sys::general::siginfo_t as Siginfo;
93104

94105
pub use crate::timespec::{Nsecs, Secs, Timespec};
95106

96-
/// `SIG_*` constants for use with [`sigprocmask`].
107+
/// `SIG_*` constants for use with [`kernel_sigprocmask`].
97108
#[cfg(linux_raw)]
98109
#[repr(u32)]
99110
pub enum How {
@@ -341,8 +352,8 @@ pub use backend::runtime::tls::StartupTlsInfo;
341352
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/fork.html
342353
/// [Linux]: https://man7.org/linux/man-pages/man2/fork.2.html
343354
/// [async-signal-safe]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/V2_chap02.html#tag_15_04_03
344-
pub unsafe fn fork() -> io::Result<Fork> {
345-
backend::runtime::syscalls::fork()
355+
pub unsafe fn kernel_fork() -> io::Result<Fork> {
356+
backend::runtime::syscalls::kernel_fork()
346357
}
347358

348359
/// Regular Unix `fork` doesn't tell the child its own PID because it assumes
@@ -423,8 +434,11 @@ pub unsafe fn execve(path: &CStr, argv: *const *const u8, envp: *const *const u8
423434
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/sigaction.html
424435
/// [Linux]: https://man7.org/linux/man-pages/man2/sigaction.2.html
425436
#[inline]
426-
pub unsafe fn sigaction(signal: Signal, new: Option<Sigaction>) -> io::Result<Sigaction> {
427-
backend::runtime::syscalls::sigaction(signal, new)
437+
pub unsafe fn kernel_sigaction(
438+
signal: Signal,
439+
new: Option<KernelSigaction>,
440+
) -> io::Result<KernelSigaction> {
441+
backend::runtime::syscalls::kernel_sigaction(signal, new)
428442
}
429443

430444
/// `sigaltstack(new, old)`—Modify or query a signal stack.
@@ -479,8 +493,8 @@ pub unsafe fn tkill(tid: Pid, sig: Signal) -> io::Result<()> {
479493
#[inline]
480494
#[doc(alias = "pthread_sigmask")]
481495
#[doc(alias = "rt_sigprocmask")]
482-
pub unsafe fn sigprocmask(how: How, set: Option<&Sigset>) -> io::Result<Sigset> {
483-
backend::runtime::syscalls::sigprocmask(how, set)
496+
pub unsafe fn kernel_sigprocmask(how: How, set: Option<&KernelSigSet>) -> io::Result<KernelSigSet> {
497+
backend::runtime::syscalls::kernel_sigprocmask(how, set)
484498
}
485499

486500
/// `sigpending()`—Query the pending signals.
@@ -493,7 +507,7 @@ pub unsafe fn sigprocmask(how: How, set: Option<&Sigset>) -> io::Result<Sigset>
493507
///
494508
/// [Linux `sigpending`]: https://man7.org/linux/man-pages/man2/sigpending.2.html
495509
#[inline]
496-
pub fn sigpending() -> Sigset {
510+
pub fn sigpending() -> SigSet {
497511
backend::runtime::syscalls::sigpending()
498512
}
499513

@@ -507,7 +521,7 @@ pub fn sigpending() -> Sigset {
507521
///
508522
/// [Linux `sigsuspend`]: https://man7.org/linux/man-pages/man2/sigsuspend.2.html
509523
#[inline]
510-
pub fn sigsuspend(set: &Sigset) -> io::Result<()> {
524+
pub fn sigsuspend(set: &SigSet) -> io::Result<()> {
511525
backend::runtime::syscalls::sigsuspend(set)
512526
}
513527

@@ -524,7 +538,7 @@ pub fn sigsuspend(set: &Sigset) -> io::Result<()> {
524538
///
525539
/// [Linux]: https://man7.org/linux/man-pages/man3/sigwait.3.html
526540
#[inline]
527-
pub unsafe fn sigwait(set: &Sigset) -> io::Result<Signal> {
541+
pub unsafe fn sigwait(set: &SigSet) -> io::Result<Signal> {
528542
backend::runtime::syscalls::sigwait(set)
529543
}
530544

@@ -541,7 +555,7 @@ pub unsafe fn sigwait(set: &Sigset) -> io::Result<Signal> {
541555
///
542556
/// [Linux]: https://man7.org/linux/man-pages/man2/sigwaitinfo.2.html
543557
#[inline]
544-
pub unsafe fn sigwaitinfo(set: &Sigset) -> io::Result<Siginfo> {
558+
pub unsafe fn sigwaitinfo(set: &SigSet) -> io::Result<Siginfo> {
545559
backend::runtime::syscalls::sigwaitinfo(set)
546560
}
547561

@@ -558,7 +572,7 @@ pub unsafe fn sigwaitinfo(set: &Sigset) -> io::Result<Siginfo> {
558572
///
559573
/// [Linux]: https://man7.org/linux/man-pages/man2/sigtimedwait.2.html
560574
#[inline]
561-
pub unsafe fn sigtimedwait(set: &Sigset, timeout: Option<Timespec>) -> io::Result<Siginfo> {
575+
pub unsafe fn sigtimedwait(set: &SigSet, timeout: Option<Timespec>) -> io::Result<Siginfo> {
562576
backend::runtime::syscalls::sigtimedwait(set, timeout)
563577
}
564578

@@ -598,8 +612,8 @@ pub fn linux_secure() -> bool {
598612
/// (perhaps because you yourself are implementing a libc).
599613
#[cfg(linux_raw)]
600614
#[inline]
601-
pub unsafe fn brk(addr: *mut c_void) -> io::Result<*mut c_void> {
602-
backend::runtime::syscalls::brk(addr)
615+
pub unsafe fn kernel_brk(addr: *mut c_void) -> io::Result<*mut c_void> {
616+
backend::runtime::syscalls::kernel_brk(addr)
603617
}
604618

605619
/// `SIGRTMIN`—The start of the raw OS “real-time” signal range.
@@ -609,9 +623,9 @@ pub unsafe fn brk(addr: *mut c_void) -> io::Result<*mut c_void> {
609623
/// won't share a process with a libc (perhaps because you yourself are
610624
/// implementing a libc).
611625
///
612-
/// See [`sigrt`] for a convenient way to construct `SIGRTMIN + n` values.
626+
/// See [`sigrt`] for a convenient way to construct `KERNEL_SIGRTMIN + n` values.
613627
#[cfg(linux_raw)]
614-
pub const SIGRTMIN: i32 = linux_raw_sys::general::SIGRTMIN as i32;
628+
pub const KERNEL_SIGRTMIN: i32 = linux_raw_sys::general::SIGRTMIN as i32;
615629

616630
/// `SIGRTMAX`—The last of the raw OS “real-time” signal range.
617631
///
@@ -620,7 +634,7 @@ pub const SIGRTMIN: i32 = linux_raw_sys::general::SIGRTMIN as i32;
620634
/// won't share a process with a libc (perhaps because you yourself are
621635
/// implementing a libc).
622636
#[cfg(linux_raw)]
623-
pub const SIGRTMAX: i32 = {
637+
pub const KERNEL_SIGRTMAX: i32 = {
624638
// Use the actual `SIGRTMAX` value on platforms which define it.
625639
#[cfg(not(any(
626640
target_arch = "arm",
@@ -644,17 +658,17 @@ pub const SIGRTMAX: i32 = {
644658
}
645659
};
646660

647-
/// Return a [`Signal`] corresponding to `SIGRTMIN + n`.
661+
/// Return a [`Signal`] corresponding to `KERNEL_SIGRTMIN + n`.
648662
///
649-
/// This is similar to [`Signal::rt`], but uses the raw OS `SIGRTMIN` value
663+
/// This is similar to [`Signal::rt`], but uses the `KERNEL_SIGRTMIN` value
650664
/// instead of the libc `SIGRTMIN` value. Don't use this unless you know your
651665
/// code won't share a process with a libc (perhaps because you yourself are
652666
/// implementing a libc).
653667
#[cfg(linux_raw)]
654668
#[doc(alias = "SIGRTMIN")]
655-
pub const fn sigrt(n: i32) -> Option<Signal> {
656-
let sig = SIGRTMIN.wrapping_add(n);
657-
if sig >= SIGRTMIN && sig <= SIGRTMAX {
669+
pub const fn kernel_sigrt(n: i32) -> Option<Signal> {
670+
let sig = KERNEL_SIGRTMIN.wrapping_add(n);
671+
if sig >= KERNEL_SIGRTMIN && sig <= KERNEL_SIGRTMAX {
658672
// SAFETY: We've checked that `sig` is in the expected range. It could
659673
// still conflict with libc's reserved values, however users of the
660674
// `runtime` module here must already know that there's no other libc

0 commit comments

Comments
 (0)