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 26, 2024
1 parent c700ad7 commit 4562133
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/backend/libc/process/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,3 +742,12 @@ pub(crate) fn getgroups(buf: &mut [Gid]) -> io::Result<usize> {

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

#[cfg(not(target_os = "wasi"))]
#[inline]
pub(crate) fn _exit(status: i32) -> ! {
unsafe {
libc::_exit(status);
}
unreachable!("_exit failed to exit the process");
}
7 changes: 7 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,10 @@ 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));
};
}
23 changes: 17 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,18 @@ 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.
///
/// # 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
#[inline]
pub fn exit(status: i32) -> ! {
backend::process::syscalls::_exit(status);
}

0 comments on commit 4562133

Please sign in to comment.