Skip to content

Commit 22e4b61

Browse files
committed
implement stub for /proc/mountinfo
So far, this doesn't use actual mount information yet and just returns hard-coded data.
1 parent 1f02680 commit 22e4b61

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

tee/kernel/src/fs/node/procfs.rs

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,7 @@ pub struct ProcessInos {
12001200
exe_link: u64,
12011201
maps_file: u64,
12021202
mem_file: u64,
1203+
mountinfo_file: u64,
12031204
root_symlink: u64,
12041205
stat_file: u64,
12051206
status_file: u64,
@@ -1217,6 +1218,7 @@ impl ProcessInos {
12171218
exe_link: new_ino(),
12181219
maps_file: new_ino(),
12191220
mem_file: new_ino(),
1221+
mountinfo_file: new_ino(),
12201222
root_symlink: new_ino(),
12211223
stat_file: new_ino(),
12221224
status_file: new_ino(),
@@ -1247,6 +1249,9 @@ struct ProcessDir {
12471249
mem_bsd_file_lock_record: LazyBsdFileLockRecord,
12481250
mem_unix_file_lock_record: LazyUnixFileLockRecord,
12491251
mem_file_watchers: Arc<Watchers>,
1252+
mountinfo_bsd_file_lock_record: LazyBsdFileLockRecord,
1253+
mountinfo_unix_file_lock_record: LazyUnixFileLockRecord,
1254+
mountinfo_file_watchers: Arc<Watchers>,
12501255
root_bsd_file_lock_record: LazyBsdFileLockRecord,
12511256
root_file_watchers: Arc<Watchers>,
12521257
stat_bsd_file_lock_record: LazyBsdFileLockRecord,
@@ -1283,6 +1288,9 @@ impl ProcessDir {
12831288
mem_bsd_file_lock_record: LazyBsdFileLockRecord::new(),
12841289
mem_unix_file_lock_record: LazyUnixFileLockRecord::new(),
12851290
mem_file_watchers: Arc::new(Watchers::new()),
1291+
mountinfo_bsd_file_lock_record: LazyBsdFileLockRecord::new(),
1292+
mountinfo_unix_file_lock_record: LazyUnixFileLockRecord::new(),
1293+
mountinfo_file_watchers: Arc::new(Watchers::new()),
12861294
root_bsd_file_lock_record: LazyBsdFileLockRecord::new(),
12871295
root_file_watchers: Arc::new(Watchers::new()),
12881296
stat_bsd_file_lock_record: LazyBsdFileLockRecord::new(),
@@ -1401,6 +1409,13 @@ impl Directory for ProcessDir {
14011409
self.mem_unix_file_lock_record.get().clone(),
14021410
self.mem_file_watchers.clone(),
14031411
),
1412+
b"mountinfo" => MountInfoFile::new(
1413+
self.fs.clone(),
1414+
self.process.clone(),
1415+
self.mountinfo_bsd_file_lock_record.get().clone(),
1416+
self.mountinfo_unix_file_lock_record.get().clone(),
1417+
self.mountinfo_file_watchers.clone(),
1418+
),
14041419
b"root" => RootLink::new(
14051420
self.fs.clone(),
14061421
self.process.clone(),
@@ -1550,6 +1565,11 @@ impl Directory for ProcessDir {
15501565
ty: FileType::File,
15511566
name: DirEntryName::FileName(FileName::new(b"mem").unwrap()),
15521567
});
1568+
entries.push(DirEntry {
1569+
ino: process.inos.mountinfo_file,
1570+
ty: FileType::File,
1571+
name: DirEntryName::FileName(FileName::new(b"mountinfo").unwrap()),
1572+
});
15531573
entries.push(DirEntry {
15541574
ino: process.inos.root_symlink,
15551575
ty: FileType::Link,
@@ -2956,6 +2976,138 @@ impl File for MemFile {
29562976
}
29572977
}
29582978

2979+
struct MountInfoFile {
2980+
this: Weak<Self>,
2981+
fs: Arc<ProcFs>,
2982+
process: Weak<Process>,
2983+
bsd_file_lock_record: Arc<BsdFileLockRecord>,
2984+
unix_file_lock_record: Arc<UnixFileLockRecord>,
2985+
watchers: Arc<Watchers>,
2986+
}
2987+
2988+
impl MountInfoFile {
2989+
pub fn new(
2990+
fs: Arc<ProcFs>,
2991+
process: Weak<Process>,
2992+
bsd_file_lock_record: Arc<BsdFileLockRecord>,
2993+
unix_file_lock_record: Arc<UnixFileLockRecord>,
2994+
watchers: Arc<Watchers>,
2995+
) -> Arc<Self> {
2996+
Arc::new_cyclic(|this| Self {
2997+
this: this.clone(),
2998+
fs,
2999+
process,
3000+
bsd_file_lock_record,
3001+
unix_file_lock_record,
3002+
watchers,
3003+
})
3004+
}
3005+
3006+
fn content(&self) -> Vec<u8> {
3007+
let mut content = Vec::new();
3008+
writeln!(
3009+
content,
3010+
"3 2 0:1 / /dev rw,relatime shared:1 - devtmpfs devtmpfs rw,size=1609224k,nr_inodes=4019697,mode=755"
3011+
).unwrap();
3012+
writeln!(
3013+
content,
3014+
"2 1 0:2 / / rw,relatime shared:1 - tmpfs tmpfs rw,size=1609224k,nr_inodes=4019697,mode=755"
3015+
).unwrap();
3016+
content
3017+
}
3018+
}
3019+
3020+
impl INode for MountInfoFile {
3021+
fn stat(&self) -> Result<Stat> {
3022+
let process = self.process.upgrade().ok_or(err!(Srch))?;
3023+
Ok(Stat {
3024+
dev: self.fs.dev,
3025+
ino: process.inos.mountinfo_file,
3026+
nlink: 1,
3027+
mode: FileTypeAndMode::new(FileType::File, FileMode::from_bits_retain(0o444)),
3028+
uid: Uid::SUPER_USER,
3029+
gid: Gid::SUPER_USER,
3030+
rdev: 0,
3031+
size: 0,
3032+
blksize: 0,
3033+
blocks: 0,
3034+
atime: Timespec::ZERO,
3035+
mtime: Timespec::ZERO,
3036+
ctime: Timespec::ZERO,
3037+
})
3038+
}
3039+
3040+
fn fs(&self) -> Result<Arc<dyn FileSystem>> {
3041+
Ok(self.fs.clone())
3042+
}
3043+
3044+
fn open(
3045+
&self,
3046+
location: LinkLocation,
3047+
flags: OpenFlags,
3048+
_: &FileAccessContext,
3049+
) -> Result<StrongFileDescriptor> {
3050+
open_file(self.this.upgrade().unwrap(), location, flags)
3051+
}
3052+
3053+
fn chmod(&self, _: FileMode, _: &FileAccessContext) -> Result<()> {
3054+
bail!(Perm)
3055+
}
3056+
3057+
fn chown(&self, _: Uid, _: Gid, _: &FileAccessContext) -> Result<()> {
3058+
Ok(())
3059+
}
3060+
3061+
fn truncate(&self, _length: usize) -> Result<()> {
3062+
bail!(Acces)
3063+
}
3064+
3065+
fn update_times(&self, _ctime: Timespec, _atime: Option<Timespec>, _mtime: Option<Timespec>) {}
3066+
3067+
fn bsd_file_lock_record(&self) -> &Arc<BsdFileLockRecord> {
3068+
&self.bsd_file_lock_record
3069+
}
3070+
3071+
fn watchers(&self) -> &Watchers {
3072+
&self.watchers
3073+
}
3074+
}
3075+
3076+
impl File for MountInfoFile {
3077+
fn get_page(&self, _page_idx: usize, _shared: bool) -> Result<KernelPage> {
3078+
bail!(NoDev)
3079+
}
3080+
3081+
fn read(&self, offset: usize, buf: &mut dyn ReadBuf, _no_atime: bool) -> Result<usize> {
3082+
let maps = self.content();
3083+
let offset = cmp::min(offset, maps.len());
3084+
let maps = &maps[offset..];
3085+
let len = cmp::min(maps.len(), buf.buffer_len());
3086+
buf.write(0, &maps[..len])?;
3087+
Ok(len)
3088+
}
3089+
3090+
fn write(&self, _offset: usize, _buf: &dyn WriteBuf) -> Result<usize> {
3091+
bail!(Acces)
3092+
}
3093+
3094+
fn append(&self, _buf: &dyn WriteBuf) -> Result<(usize, usize)> {
3095+
bail!(Acces)
3096+
}
3097+
3098+
fn allocate(&self, _mode: FallocateMode, _offset: usize, _len: usize) -> Result<()> {
3099+
bail!(Acces)
3100+
}
3101+
3102+
fn deleted(&self) -> bool {
3103+
false
3104+
}
3105+
3106+
fn unix_file_lock_record(&self) -> &Arc<UnixFileLockRecord> {
3107+
&self.unix_file_lock_record
3108+
}
3109+
}
3110+
29593111
struct RootLink {
29603112
fs: Arc<ProcFs>,
29613113
process: Weak<Process>,

0 commit comments

Comments
 (0)