Skip to content

Commit

Permalink
Add a specifier for the filename.
Browse files Browse the repository at this point in the history
  • Loading branch information
ayosec committed Aug 26, 2021
1 parent 8da2bda commit f5368be
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions FORMAT.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ The following resource specifiers are accepted in the format string:
|`%I`<br>`%(inblock)` | File system inputs. |
|`%M`<br>`%(maxrss)` | Maximum resident set size in Kib. |
|`%n` | Entry number in the history. |
|`%N`<br>`%(filename)` | Filename of the executable. |
|`%O`<br>`%(oublock)` | File system outputs. |
|`%P`<br>`%(cpu)` | Percent of CPU this job got. |
|`%R`<br>`%(minflt)` | Minor page faults (reclaims; no physical I/O involved). |
Expand Down
7 changes: 6 additions & 1 deletion src/format/format.spec
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,16 @@

// Specifiers.

: %N %(filename)
//! [label] FILENAME
//! Filename of the executable.
w!(EscapeArgument(entry.filename.as_bytes()));

: %C %(args)
//! [label] COMMAND
//! Command name and arguments.
let mut need_space = false;
for arg in entry.args.iter().skip(1) {
for arg in entry.args.iter() {
if mem::replace(&mut need_space, true) {
w!(" ");
}
Expand Down
3 changes: 2 additions & 1 deletion src/format/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ where
number: 1234,
pid: 10000,
start_time: Local.timestamp(1000000000, 9999),
args: ["/bin/ls", "ls", "F"].iter().map(OsString::from).collect(),
filename: OsString::from("/bin/ls"),
args: ["ls", "F"].iter().map(OsString::from).collect(),
state: State::Running {
start: libc::timespec {
tv_sec: 0,
Expand Down
4 changes: 4 additions & 0 deletions src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub struct Entry {
pub start_time: DateTime<Local>,

#[serde(serialize_with = "crate::jsonext::serialize_os_string")]
pub filename: OsString,

#[serde(serialize_with = "crate::jsonext::serialize_vec_os_string")]
pub args: Vec<OsString>,

#[serde(serialize_with = "crate::jsonext::serialize_state")]
Expand Down Expand Up @@ -92,6 +95,7 @@ impl History {
number: self.last_number,
pid: event.pid,
start_time: Local.timestamp(event.start_time.tv_sec, event.start_time.tv_nsec as u32),
filename: event.filename,
args: event.args,
state: State::Running {
start: event.monotonic_time,
Expand Down
3 changes: 3 additions & 0 deletions src/ipc/events/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct ExecEvent {
pub pid: libc::pid_t,
pub monotonic_time: libc::timespec,
pub start_time: libc::timespec,
pub filename: OsString,
pub args: Vec<OsString>,
}

Expand Down Expand Up @@ -71,6 +72,7 @@ impl ExecEvent {
let start_time = unsafe { reader.read_value()? };

// Read arguments as C strings.
let filename = reader.read_cstr()?;
let mut args = Vec::new();
while reader.position() < reader.get_ref().len() as u64 {
args.push(reader.read_cstr()?);
Expand All @@ -80,6 +82,7 @@ impl ExecEvent {
pid,
monotonic_time,
start_time,
filename,
args,
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/ipc/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ mod tests {
assert_eq!(event.monotonic_time.tv_nsec, 20000 + idx);
assert_eq!(event.start_time.tv_sec, 1000000 + idx);
assert_eq!(event.start_time.tv_nsec, 2000000 + idx);
assert_eq!(event.filename, OsString::from("/bin/ls"));
assert_eq!(
event.args,
[
OsString::from("/bin/ls"),
OsString::from("ls"),
OsString::from("-l"),
OsString::from(format!("file{}", idx)),
Expand Down
11 changes: 9 additions & 2 deletions src/jsonext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

use crate::history::State;
use serde::ser::{Serialize, SerializeMap, SerializeSeq, Serializer};
use std::ffi::OsString;
use std::ffi::{OsStr, OsString};
use std::os::unix::ffi::OsStrExt;

pub fn serialize_os_string<S: Serializer>(strings: &[OsString], ser: S) -> Result<S::Ok, S::Error> {
pub fn serialize_os_string<S: Serializer>(string: &OsStr, ser: S) -> Result<S::Ok, S::Error> {
ser.serialize_str(&String::from_utf8_lossy(string.as_bytes()))
}

pub fn serialize_vec_os_string<S: Serializer>(
strings: &[OsString],
ser: S,
) -> Result<S::Ok, S::Error> {
let mut seq = ser.serialize_seq(Some(strings.len()))?;
for s in strings {
let s = String::from_utf8_lossy(s.as_bytes());
Expand Down

0 comments on commit f5368be

Please sign in to comment.