Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add std::os::unix::fs::chroot to change the root directory of the current process #84716

Merged
merged 1 commit into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions library/std/src/sys/unix/ext/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,3 +884,29 @@ impl DirBuilderExt for fs::DirBuilder {
self
}
}

/// Change the root directory of the current process to the specified path.
///
/// This typically requires privileges, such as root or a specific capability.
///
/// This does not change the current working directory; you should call
/// [`std::env::set_current_dir`][`crate::env::set_current_dir`] afterwards.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding extern crate self as std; in std's lib.rs would avoid having to do the link target explicitly here (and definitely elsewhere too).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to @jyn514, doing that currently produces an ICE.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is #73445. I haven't tried it in a while, it may work now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, you can shorten a bit like so:

Suggested change
/// [`std::env::set_current_dir`][`crate::env::set_current_dir`] afterwards.
/// [`std::env::set_current_dir`](crate::env::set_current_dir) afterwards.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you go the extern crate route, you might want to do

#[cfg(doc)]
extern crate self as std;

to ensure it's only used for docs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@camelid that will break any links that are re-exported, cfg(doc) isn't set for dependencies.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tried it in a while, it may work now.

Just tried it, extern crate self as core works but not extern crate self as std (#84755, #73445 (comment)).

///
/// # Examples
///
/// ```no_run
/// #![feature(unix_chroot)]
/// use std::os::unix::fs;
///
/// fn main() -> std::io::Result<()> {
/// fs::chroot("/sandbox")?;
/// std::env::set_current_dir("/")?;
/// // continue working in sandbox
/// Ok(())
/// }
/// ```
#[unstable(feature = "unix_chroot", issue = "84715")]
#[cfg(not(target_os = "fuchsia"))]
pub fn chroot<P: AsRef<Path>>(dir: P) -> io::Result<()> {
sys::fs::chroot(dir.as_ref())
}
7 changes: 7 additions & 0 deletions library/std/src/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,3 +1328,10 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
})?;
Ok(bytes_copied as u64)
}

#[cfg(not(target_os = "fuchsia"))]
pub fn chroot(dir: &Path) -> io::Result<()> {
let dir = cstr(dir)?;
cvt(unsafe { libc::chroot(dir.as_ptr()) })?;
Ok(())
}