Skip to content

Commit f95c6a4

Browse files
committed
pidof: Support '-t' flag
This flag makes pidof print thread ids of threads belonging to the matching processes.
1 parent f934fa5 commit f95c6a4

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/uu/pgrep/src/process.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ pub struct ProcessInformation {
186186
cached_stat: Option<Rc<Vec<String>>>,
187187

188188
cached_start_time: Option<u64>,
189+
190+
cached_thread_ids: Option<Rc<Vec<usize>>>,
189191
}
190192

191193
impl ProcessInformation {
@@ -328,6 +330,31 @@ impl ProcessInformation {
328330

329331
Teletype::Unknown
330332
}
333+
334+
pub fn thread_ids(&mut self) -> Rc<Vec<usize>> {
335+
if let Some(c) = &self.cached_thread_ids {
336+
return Rc::clone(c);
337+
}
338+
339+
let tids_dir = format!("/proc/{}/task", self.pid);
340+
let result = Rc::new(
341+
WalkDir::new(tids_dir)
342+
.min_depth(1)
343+
.max_depth(1)
344+
.follow_links(false)
345+
.into_iter()
346+
.flatten()
347+
.flat_map(|it| {
348+
it.path()
349+
.file_name()
350+
.map(|it| it.to_str().unwrap().parse::<usize>().unwrap())
351+
})
352+
.collect::<Vec<_>>(),
353+
);
354+
355+
self.cached_thread_ids = Some(Rc::clone(&result));
356+
Rc::clone(&result)
357+
}
331358
}
332359
impl TryFrom<DirEntry> for ProcessInformation {
333360
type Error = io::Error;

src/uu/pidof/src/pidof.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,23 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
2525
};
2626

2727
let collected = collect_matched_pids(&matches);
28+
let to_print = if matches.get_flag("t") {
29+
collected
30+
.into_iter()
31+
.flat_map(|mut it| (*it.thread_ids()).clone())
32+
.collect::<Vec<_>>()
33+
} else {
34+
collected.into_iter().map(|it| it.pid).collect::<Vec<_>>()
35+
};
2836

29-
if collected.is_empty() {
37+
if to_print.is_empty() {
3038
uucore::error::set_exit_code(1);
3139
return Ok(());
3240
};
3341

34-
let output = collected
42+
let output = to_print
3543
.into_iter()
36-
.map(|it| it.pid.to_string())
44+
.map(|it| it.to_string())
3745
.collect::<Vec<_>>()
3846
.join(arg_separator);
3947

@@ -164,6 +172,13 @@ pub fn uu_app() -> Command {
164172
.help("Only return one PID")
165173
.action(ArgAction::SetTrue),
166174
)
175+
.arg(
176+
Arg::new("t")
177+
.short('t')
178+
.long("lightweight")
179+
.help("Show thread ids instead of process ids")
180+
.action(ArgAction::SetTrue),
181+
)
167182
// .arg(
168183
// Arg::new("x")
169184
// .short('x')

0 commit comments

Comments
 (0)