eminence / procfs

Rust library for reading the Linux procfs filesystem
Other
367 stars 106 forks source link

API for deleted files / raw FD #317

Open tatref opened 1 day ago

tatref commented 1 day ago

Hi,

If a file is deleted while still opened by a process, the fd under /proc/$pid/fd/$fd will show up as

[tatref@srv dev]$ ls -ld /proc/12779/fd/*
lr-x------ 1 tatref tatref 64 Oct 16 01:00 /proc/12779/fd/4 -> '/home/tatref/dev/tes (deleted)'

We can get that info with

use std::fs::File;
use std::fs::Metadata;
use std::os::unix::fs::MetadataExt;

let f = File::open(&format!("/proc/{}/fd/{}", p.pid, fd.fd)).unwrap();
let metadata = f.metadata().unwrap();

if metadata.nlink() == 0 {}

So this means format a String, open a file, and do a syscall to get metadata.

Currently it seems there is no easier API in procfs to access this information (please correct me if I'm wrong). FDTarget has a Path variant, but there is no guarantee that another file with the same name hasn't be created.

Maybe we could store a File in the FDInfo, and add it where it is created:

https://github.com/eminence/procfs/blob/e303757cd3e21c763e339f6f7e4c9cb4e435ff83/procfs/src/process/mod.rs#L149-L175

What do you think?