Skip to content

Commit 672b790

Browse files
zackmdavisdanburkert
authored andcommitted
cwd
1 parent 62cac55 commit 672b790

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/pid/cwd.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//! Concerning the current working directory of a process, from
2+
//! `/proc/[pid]/cwd`.
3+
4+
use std::fs;
5+
use std::io::Result;
6+
use std::path::PathBuf;
7+
8+
use libc::pid_t;
9+
10+
/// Gets path of current working directory for the process with the provided
11+
/// pid.
12+
pub fn cwd(pid: pid_t) -> Result<PathBuf> {
13+
fs::read_link(format!("/proc/{}/cwd", pid))
14+
}
15+
16+
/// Gets path of current working directory for the current process.
17+
pub fn cwd_self() -> Result<PathBuf> {
18+
fs::read_link("/proc/self/cwd")
19+
}
20+
21+
#[cfg(test)]
22+
pub mod tests {
23+
use super::cwd_self;
24+
use std::env;
25+
26+
#[test]
27+
fn test_cwd_self() {
28+
assert_eq!(env::current_dir().unwrap(), cwd_self().unwrap());
29+
}
30+
}

src/pid/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
//! Process-specific information from `/proc/[pid]/`.
22
3+
mod cwd;
34
mod stat;
45
mod statm;
56
mod status;
67

8+
pub use pid::cwd::{cwd, cwd_self};
79
pub use pid::statm::{Statm, statm, statm_self};
810
pub use pid::status::{SeccompMode, Status, status, status_self};
911
pub use pid::stat::{Stat};

0 commit comments

Comments
 (0)