Skip to content

Commit

Permalink
Add rustix::process::exit to terminate processes
Browse files Browse the repository at this point in the history
  • Loading branch information
danielschemmel committed Aug 27, 2024
1 parent c700ad7 commit 28e9665
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/backend/libc/process/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,3 +742,14 @@ pub(crate) fn getgroups(buf: &mut [Gid]) -> io::Result<usize> {

unsafe { ret_usize(c::getgroups(len, buf.as_mut_ptr().cast()) as isize) }
}

#[inline]
pub(crate) fn _exit(status: i32) -> ! {
unsafe {
libc::_exit(status);
}
#[allow(unreachable_code)]
{
unreachable!("_exit failed to exit the process")
}
}
2 changes: 1 addition & 1 deletion src/backend/linux_raw/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ macro_rules! syscall_readonly {
}

/// Like `syscall`, but indicates that the syscall does not return.
#[cfg(feature = "runtime")]
#[cfg(any(feature = "runtime", feature = "process"))]
macro_rules! syscall_noreturn {
($nr:ident, $a0:expr) => {
$crate::backend::arch::choose::syscall1_noreturn(
Expand Down
11 changes: 11 additions & 0 deletions src/backend/linux_raw/process/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,14 @@ pub(crate) fn getgroups(buf: &mut [Gid]) -> io::Result<usize> {
))
}
}

#[inline]
pub(crate) fn _exit(status: i32) -> ! {
unsafe {
syscall_noreturn!(__NR_exit_group, c_int(status));
};
#[allow(unreachable_code)]
{
unreachable!("_exit failed to exit the process")
}
}
35 changes: 29 additions & 6 deletions src/process/exit.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::backend;

/// `EXIT_SUCCESS` for use with [`exit`].
///
/// [`exit`]: std::process::exit
/// `EXIT_SUCCESS` for use with [`exit`] or [`std::process::exit`].
///
/// # References
/// - [POSIX]
Expand All @@ -12,9 +10,7 @@ use crate::backend;
/// [Linux]: https://man7.org/linux/man-pages/man3/exit.3.html
pub const EXIT_SUCCESS: i32 = backend::c::EXIT_SUCCESS;

/// `EXIT_FAILURE` for use with [`exit`].
///
/// [`exit`]: std::process::exit
/// `EXIT_FAILURE` for use with [`exit`] or [`std::process::exit`].
///
/// # References
/// - [POSIX]
Expand All @@ -34,3 +30,30 @@ pub const EXIT_FAILURE: i32 = backend::c::EXIT_FAILURE;
/// [`Signal::Abort`]: crate::process::Signal::Abort
#[cfg(not(any(target_os = "espidf", target_os = "wasi")))]
pub const EXIT_SIGNALED_SIGABRT: i32 = backend::c::EXIT_SIGNALED_SIGABRT;

/// Immediately exits the process. Exiting via this function does not unwind the
/// stack and does not call any further user code. This behavior is similar to
/// the POSIX/C `_Exit` and `_exit` functions.
///
/// Notably, this function does:
/// - *Not* flush any buffers, such as Rust or C standard output or files.
/// - *Not* call any destructors, neither in the form of stack unwinding, nor
/// any global destructors.
/// - *Not* call functions registered with [`atexit`] or [`at_quick_exit`]
///
/// In general, most code should call [`std::process::exit`] instead, if it is
/// available.
///
/// # References
/// - [POSIX]
/// - [Linux]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stdlib.h.html
/// [Linux]: https://www.man7.org/linux/man-pages/man2/exit.2.html
/// [`atexit`]: https://www.man7.org/linux/man-pages/man3/atexit.3.html
/// [`at_quick_exit`]: https://en.cppreference.com/w/c/program/at_quick_exit
#[doc(alias = "_exit")]
#[inline]
pub fn exit(status: i32) -> ! {
backend::process::syscalls::_exit(status);
}

0 comments on commit 28e9665

Please sign in to comment.