Skip to content

Commit 138dc81

Browse files
committed
process: Make current_process_info public
pidof will utilize it.
1 parent 1f72da9 commit 138dc81

File tree

2 files changed

+17
-30
lines changed

2 files changed

+17
-30
lines changed

src/uu/pgrep/src/process.rs

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,13 @@ impl ProcessInformation {
250250
})
251251
}
252252

253+
pub fn current_process_info() -> Result<ProcessInformation, io::Error> {
254+
use std::str::FromStr;
255+
use uucore::process::getpid;
256+
257+
ProcessInformation::try_new(PathBuf::from_str(&format!("/proc/{}", getpid())).unwrap())
258+
}
259+
253260
pub fn proc_status(&self) -> &str {
254261
&self.inner_status
255262
}
@@ -495,7 +502,9 @@ pub fn walk_threads() -> impl Iterator<Item = ProcessInformation> {
495502
mod tests {
496503
use super::*;
497504
#[cfg(target_os = "linux")]
498-
use std::{collections::HashSet, str::FromStr};
505+
use std::collections::HashSet;
506+
#[cfg(target_os = "linux")]
507+
use uucore::process::getpid;
499508

500509
#[test]
501510
fn test_run_state_conversion() {
@@ -515,41 +524,19 @@ mod tests {
515524
assert!(RunState::try_from("Rg").is_err());
516525
}
517526

518-
#[cfg(target_os = "linux")]
519-
fn current_pid() -> usize {
520-
// Direct read link of /proc/self.
521-
// It's result must be current programs pid.
522-
fs::read_link("/proc/self")
523-
.unwrap()
524-
.to_str()
525-
.unwrap()
526-
.parse::<usize>()
527-
.unwrap()
528-
}
529-
530-
#[cfg(target_os = "linux")]
531-
fn current_process_info() -> ProcessInformation {
532-
ProcessInformation::try_new(PathBuf::from_str(&format!("/proc/{}", current_pid())).unwrap())
533-
.unwrap()
534-
}
535-
536527
#[test]
537528
#[cfg(target_os = "linux")]
538529
fn test_walk_pid() {
539-
let current_pid = current_pid();
540-
541-
let find = walk_process().find(|it| it.pid == current_pid);
530+
let find = walk_process().find(|it| it.pid == getpid() as usize);
542531

543532
assert!(find.is_some());
544533
}
545534

546535
#[test]
547536
#[cfg(target_os = "linux")]
548537
fn test_pid_entry() {
549-
let current_pid = current_pid();
550-
551-
let pid_entry = current_process_info();
552-
let mut result = WalkDir::new(format!("/proc/{}/fd", current_pid))
538+
let pid_entry = ProcessInformation::current_process_info().unwrap();
539+
let mut result = WalkDir::new(format!("/proc/{}/fd", getpid()))
553540
.into_iter()
554541
.flatten()
555542
.map(DirEntry::into_path)
@@ -569,7 +556,7 @@ mod tests {
569556
fn test_thread_ids() {
570557
let main_tid = unsafe { uucore::libc::gettid() };
571558
std::thread::spawn(move || {
572-
let mut pid_entry = current_process_info();
559+
let mut pid_entry = ProcessInformation::current_process_info().unwrap();
573560
let thread_ids = pid_entry.thread_ids();
574561

575562
assert!(thread_ids.contains(&(main_tid as usize)));
@@ -599,7 +586,7 @@ mod tests {
599586
#[test]
600587
#[cfg(target_os = "linux")]
601588
fn test_ids() {
602-
let mut pid_entry = current_process_info();
589+
let mut pid_entry = ProcessInformation::current_process_info().unwrap();
603590
assert_eq!(
604591
pid_entry.ppid().unwrap(),
605592
unsafe { uucore::libc::getppid() } as u64
@@ -615,7 +602,7 @@ mod tests {
615602
#[test]
616603
#[cfg(target_os = "linux")]
617604
fn test_uid_gid() {
618-
let mut pid_entry = current_process_info();
605+
let mut pid_entry = ProcessInformation::current_process_info().unwrap();
619606
assert_eq!(pid_entry.uid().unwrap(), uucore::process::getuid());
620607
assert_eq!(pid_entry.euid().unwrap(), uucore::process::geteuid());
621608
assert_eq!(pid_entry.gid().unwrap(), uucore::process::getgid());

src/uu/pidof/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ categories = ["command-line-utilities"]
1313

1414

1515
[dependencies]
16-
uucore = { workspace = true }
16+
uucore = { workspace = true, features = ["process"] }
1717
clap = { workspace = true }
1818
uu_pgrep = { path = "../pgrep" }
1919

0 commit comments

Comments
 (0)