Skip to content

Commit e1f4d48

Browse files
authored
Add pivot_root syscall. (#1116)
It is enabled only in Linux, and with the feature `fs` (like `chroot`).
1 parent 635b87b commit e1f4d48

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

src/backend/libc/process/syscalls.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,17 @@ pub(crate) fn pidfd_getfd(
725725
}
726726
}
727727

728+
#[cfg(target_os = "linux")]
729+
pub(crate) fn pivot_root(new_root: &CStr, put_old: &CStr) -> io::Result<()> {
730+
syscall! {
731+
fn pivot_root(
732+
new_root: *const c::c_char,
733+
put_old: *const c::c_char
734+
) via SYS_pivot_root -> c::c_int
735+
}
736+
unsafe { ret(pivot_root(c_str(new_root), c_str(put_old))) }
737+
}
738+
728739
#[cfg(all(feature = "alloc", not(target_os = "wasi")))]
729740
pub(crate) fn getgroups(buf: &mut [Gid]) -> io::Result<usize> {
730741
let len = buf.len().try_into().map_err(|_| io::Errno::NOMEM)?;

src/backend/linux_raw/process/syscalls.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,12 @@ pub(crate) fn pidfd_send_signal(fd: BorrowedFd<'_>, sig: Signal) -> io::Result<(
601601
}
602602
}
603603

604+
#[cfg(feature = "fs")]
605+
#[inline]
606+
pub(crate) fn pivot_root(new_root: &CStr, put_old: &CStr) -> io::Result<()> {
607+
unsafe { ret(syscall_readonly!(__NR_pivot_root, new_root, put_old)) }
608+
}
609+
604610
#[cfg(feature = "alloc")]
605611
#[inline]
606612
pub(crate) fn getgroups(buf: &mut [Gid]) -> io::Result<usize> {

src/process/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ mod membarrier;
1717
mod pidfd;
1818
#[cfg(target_os = "linux")]
1919
mod pidfd_getfd;
20+
#[cfg(target_os = "linux")]
21+
mod pivot_root;
2022
#[cfg(linux_kernel)]
2123
mod prctl;
2224
#[cfg(not(any(target_os = "fuchsia", target_os = "vita", target_os = "wasi")))]
@@ -57,6 +59,8 @@ pub use membarrier::*;
5759
pub use pidfd::*;
5860
#[cfg(target_os = "linux")]
5961
pub use pidfd_getfd::*;
62+
#[cfg(target_os = "linux")]
63+
pub use pivot_root::*;
6064
#[cfg(linux_kernel)]
6165
pub use prctl::*;
6266
#[cfg(not(any(target_os = "fuchsia", target_os = "vita", target_os = "wasi")))]

src/process/pivot_root.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#[cfg(feature = "fs")]
2+
#[cfg_attr(doc_cfg, doc(cfg(feature = "fs")))]
3+
use crate::{backend, io, path};
4+
5+
/// `pivot_root(new_root, put_old)`—Change the root mount.
6+
///
7+
/// # References
8+
/// - [Linux]
9+
///
10+
/// [Linux]: https://man7.org/linux/man-pages/man2/pivot_root.2.html
11+
#[cfg(feature = "fs")]
12+
#[cfg_attr(doc_cfg, doc(cfg(feature = "fs")))]
13+
#[inline]
14+
pub fn pivot_root<P: path::Arg, Q: path::Arg>(new_root: P, put_old: Q) -> io::Result<()> {
15+
new_root.into_with_c_str(|new_root| {
16+
put_old.into_with_c_str(|put_old| backend::process::syscalls::pivot_root(new_root, put_old))
17+
})
18+
}

0 commit comments

Comments
 (0)