Add support for '/proc/[pid]/root'

This commit is contained in:
LI Qing 2021-05-13 17:38:43 +08:00 committed by Zongmin.Gu
parent 65271ce190
commit 9a76ca1888
3 changed files with 43 additions and 6 deletions

@ -3,14 +3,20 @@ use super::*;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct FsView { pub struct FsView {
root: String,
cwd: String, cwd: String,
} }
impl FsView { impl FsView {
pub fn new() -> FsView { pub fn new() -> FsView {
Self { let root = String::from("/");
cwd: "/".to_owned(), let cwd = root.clone();
} Self { root, cwd }
}
/// Get the root directory
pub fn root(&self) -> &str {
&self.root
} }
/// Get the current working directory. /// Get the current working directory.
@ -203,8 +209,8 @@ impl FsView {
impl Default for FsView { impl Default for FsView {
fn default() -> Self { fn default() -> Self {
Self { let root = String::from("/");
cwd: "/".to_owned(), let cwd = root.clone();
} Self { root, cwd }
} }
} }

@ -38,6 +38,10 @@ impl LockedPidDirINode {
// fd // fd
let fd_inode = LockedProcFdDirINode::new(&file.process_ref, file.this.upgrade().unwrap()); let fd_inode = LockedProcFdDirINode::new(&file.process_ref, file.this.upgrade().unwrap());
file.entries.insert(String::from("fd"), fd_inode); file.entries.insert(String::from("fd"), fd_inode);
// root
let root_inode = ProcRootSymINode::new(&file.process_ref);
file.entries.insert(String::from("root"), root_inode);
Ok(()) Ok(())
} }
} }
@ -166,6 +170,22 @@ impl ProcINode for ProcCwdSymINode {
} }
} }
pub struct ProcRootSymINode(ProcessRef);
impl ProcRootSymINode {
pub fn new(process_ref: &ProcessRef) -> Arc<dyn INode> {
Arc::new(SymLink::new(Self(Arc::clone(process_ref))))
}
}
impl ProcINode for ProcRootSymINode {
fn generate_data_in_bytes(&self) -> vfs::Result<Vec<u8>> {
let main_thread = self.0.main_thread().ok_or(FsError::EntryNotFound)?;
let fs = main_thread.fs().lock().unwrap();
Ok(fs.root().to_owned().into_bytes())
}
}
pub struct FdSymINode(FileRef); pub struct FdSymINode(FileRef);
impl FdSymINode { impl FdSymINode {

@ -66,6 +66,16 @@ static int test_readlink_from_proc_self_cwd() {
return 0; return 0;
} }
static int test_readlink_from_proc_self_root() {
char root_buf[PATH_MAX] = { 0 };
const char *proc_root = "/proc/self/root";
if (test_readlink_from_procfs(proc_root, root_buf, PATH_MAX, "/") < 0) {
THROW_ERROR("failed to call test_readlink_from_procfs");
}
return 0;
}
static int test_read_from_proc_self_cmdline() { static int test_read_from_proc_self_cmdline() {
char absolute_path[PATH_MAX] = { 0 }; char absolute_path[PATH_MAX] = { 0 };
const char *proc_cmdline = "/proc/self/cmdline"; const char *proc_cmdline = "/proc/self/cmdline";
@ -126,6 +136,7 @@ static int test_read_from_proc_cpuinfo() {
static test_case_t test_cases[] = { static test_case_t test_cases[] = {
TEST_CASE(test_readlink_from_proc_self_exe), TEST_CASE(test_readlink_from_proc_self_exe),
TEST_CASE(test_readlink_from_proc_self_cwd), TEST_CASE(test_readlink_from_proc_self_cwd),
TEST_CASE(test_readlink_from_proc_self_root),
TEST_CASE(test_read_from_proc_self_cmdline), TEST_CASE(test_read_from_proc_self_cmdline),
TEST_CASE(test_read_from_proc_meminfo), TEST_CASE(test_read_from_proc_meminfo),
TEST_CASE(test_read_from_proc_cpuinfo), TEST_CASE(test_read_from_proc_cpuinfo),