Add /proc/[pid]/cmdline support

This commit is contained in:
Weijie Liu
2024-05-23 19:05:22 +08:00
committed by Tate, Hongliang Tian
parent 9e03ac7d0f
commit 3b66b0686e
5 changed files with 77 additions and 9 deletions

View File

@ -0,0 +1,40 @@
// SPDX-License-Identifier: MPL-2.0
use crate::{
fs::{
procfs::template::{FileOps, ProcFileBuilder},
utils::Inode,
},
prelude::*,
Process,
};
/// Represents the inode at `/proc/[pid]/cmdline`.
pub struct CmdlineFileOps(Arc<Process>);
impl CmdlineFileOps {
pub fn new_inode(process_ref: Arc<Process>, parent: Weak<dyn Inode>) -> Arc<dyn Inode> {
ProcFileBuilder::new(Self(process_ref))
.parent(parent)
.build()
.unwrap()
}
}
impl FileOps for CmdlineFileOps {
fn data(&self) -> Result<Vec<u8>> {
let cmdline_output = if self.0.is_zombie() {
// Returns 0 characters for zombie process.
Vec::new()
} else {
let Ok(argv_cstrs) = self.0.vm().init_stack_reader().argv() else {
return Ok(Vec::new());
};
argv_cstrs
.into_iter()
.flat_map(|c_str| c_str.into_bytes_with_nul().into_iter())
.collect()
};
Ok(cmdline_output)
}
}

View File

@ -1,6 +1,13 @@
// SPDX-License-Identifier: MPL-2.0
use super::*;
use crate::{
fs::{
procfs::template::{FileOps, ProcFileBuilder},
utils::Inode,
},
prelude::*,
Process,
};
/// Represents the inode at `/proc/[pid]/comm`.
pub struct CommFileOps(Arc<Process>);

View File

@ -1,6 +1,13 @@
// SPDX-License-Identifier: MPL-2.0
use super::*;
use crate::{
fs::{
procfs::{ProcSymBuilder, SymOps},
utils::Inode,
},
prelude::*,
Process,
};
/// Represents the inode at `/proc/[pid]/exe`.
pub struct ExeSymOps(Arc<Process>);

View File

@ -1,7 +1,18 @@
// SPDX-License-Identifier: MPL-2.0
use super::*;
use crate::fs::{file_handle::FileLike, file_table::FileDesc, inode_handle::InodeHandle};
use crate::{
fs::{
file_handle::FileLike,
file_table::FileDesc,
inode_handle::InodeHandle,
procfs::{
pid::FdEvents, DirOps, Observer, ProcDir, ProcDirBuilder, ProcSymBuilder, SymOps,
},
utils::{DirEntryVecExt, Inode},
},
prelude::*,
Process,
};
/// Represents the inode at `/proc/[pid]/fd`.
pub struct FdDirOps(Arc<Process>);

View File

@ -1,9 +1,7 @@
// SPDX-License-Identifier: MPL-2.0
use self::{comm::CommFileOps, exe::ExeSymOps, fd::FdDirOps};
use super::template::{
DirOps, FileOps, ProcDir, ProcDirBuilder, ProcFileBuilder, ProcSymBuilder, SymOps,
};
use self::{cmdline::CmdlineFileOps, comm::CommFileOps, exe::ExeSymOps, fd::FdDirOps};
use super::template::{DirOps, ProcDir, ProcDirBuilder};
use crate::{
events::Observer,
fs::{
@ -14,6 +12,7 @@ use crate::{
process::Process,
};
mod cmdline;
mod comm;
mod exe;
mod fd;
@ -51,6 +50,7 @@ impl DirOps for PidDirOps {
"exe" => ExeSymOps::new_inode(self.0.clone(), this_ptr.clone()),
"comm" => CommFileOps::new_inode(self.0.clone(), this_ptr.clone()),
"fd" => FdDirOps::new_inode(self.0.clone(), this_ptr.clone()),
"cmdline" => CmdlineFileOps::new_inode(self.0.clone(), this_ptr.clone()),
_ => return_errno!(Errno::ENOENT),
};
Ok(inode)
@ -70,6 +70,9 @@ impl DirOps for PidDirOps {
});
cached_children.put_entry_if_not_found("fd", || {
FdDirOps::new_inode(self.0.clone(), this_ptr.clone())
})
});
cached_children.put_entry_if_not_found("cmdline", || {
CmdlineFileOps::new_inode(self.0.clone(), this_ptr.clone())
});
}
}