Skip to content

Commit

Permalink
Add /proc/[pid]/cmdline support
Browse files Browse the repository at this point in the history
  • Loading branch information
Weijie Liu committed Jun 12, 2024
1 parent c6aa9f9 commit 5a12f11
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 9 deletions.
40 changes: 40 additions & 0 deletions kernel/aster-nix/src/fs/procfs/pid/cmdline.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
9 changes: 8 additions & 1 deletion kernel/aster-nix/src/fs/procfs/pid/comm.rs
Original file line number Diff line number Diff line change
@@ -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>);
Expand Down
9 changes: 8 additions & 1 deletion kernel/aster-nix/src/fs/procfs/pid/exe.rs
Original file line number Diff line number Diff line change
@@ -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>);
Expand Down
15 changes: 13 additions & 2 deletions kernel/aster-nix/src/fs/procfs/pid/fd.rs
Original file line number Diff line number Diff line change
@@ -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>);
Expand Down
13 changes: 8 additions & 5 deletions kernel/aster-nix/src/fs/procfs/pid/mod.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -14,6 +12,7 @@ use crate::{
process::Process,
};

mod cmdline;
mod comm;
mod exe;
mod fd;
Expand Down Expand Up @@ -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)
Expand All @@ -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())
});
}
}

0 comments on commit 5a12f11

Please sign in to comment.