Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
feat: procfs mount
Browse files Browse the repository at this point in the history
  • Loading branch information
llenotre committed Jun 10, 2024
1 parent 4d3ec56 commit 315e6d5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ const TESTS: &[TestSuite] = &[
// TODO SSE/MMX/AVX states consistency
TestSuite {
name: "procfs",
desc: "",
desc: "Test correctness of the procfs filesystem",
tests: &[
Test {
name: "mount",
desc: "Mount the procfs",
start: procfs::mount,
},
Test {
name: "/proc/self/cwd",
desc: "/proc/self/cwd",
Expand Down
22 changes: 20 additions & 2 deletions src/procfs.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
//! TODO doc
//! procfs filesystem testing.
use crate::test_assert_eq;
use crate::util;
use crate::util::{TestError, TestResult};
use std::collections::HashMap;
use std::env::current_dir;
use std::ffi::CString;
use std::os::unix::ffi::OsStrExt;
use std::ptr::null;
use std::{env, fs};

pub fn mount() -> TestResult {
fs::create_dir_all("/proc")?;
let src = CString::new("procfs")?;
let target = CString::new("/proc")?;
let fstype = CString::new("tmpfs")?;
util::mount(
src.as_c_str(),
target.as_c_str(),
fstype.as_c_str(),
0,
null(),
)?;
Ok(())
}

pub fn cwd() -> TestResult {
let cwd = fs::read_link("/proc/self/cwd")?;
test_assert_eq!(cwd, current_dir()?);
Expand Down Expand Up @@ -39,7 +57,7 @@ pub fn environ() -> TestResult {
.enumerate()
.find(|(_, b)| **b == b'=')
.map(|(i, _)| i)
.ok_or_else(|| TestError("missing `=` in environment variable".to_owned()))?;
.ok_or_else(|| TestError("missing `=` for environment variable".to_owned()))?;
let (name, value) = var.split_at(off);
Ok((name, &value[1..]))
})
Expand Down
18 changes: 18 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
use libc::mode_t;
use std::error::Error;
use std::ffi::c_int;
use std::ffi::c_ulong;
use std::ffi::c_void;
use std::ffi::CStr;
use std::os::unix::ffi::OsStrExt;
use std::path::Path;
use std::process::{Command, Stdio};
Expand Down Expand Up @@ -92,6 +95,21 @@ pub fn fstat(fd: c_int) -> io::Result<libc::stat> {
}
}

pub fn mount(
src: &CStr,
target: &CStr,
fstype: &CStr,
flags: c_ulong,
data: *const c_void,
) -> io::Result<()> {
let res = unsafe { libc::mount(src.as_ptr(), target.as_ptr(), fstype.as_ptr(), flags, data) };
if res >= 0 {
Ok(())
} else {
Err(io::Error::last_os_error())
}
}

/// TODO doc
pub fn exec(cmd: &mut Command) -> TestResult {
// TODO capture output and compare to expected output?
Expand Down

0 comments on commit 315e6d5

Please sign in to comment.