Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /proc/[pid]/cmdline support #877

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
});
}
}
Loading