Skip to content

Commit d73aba8

Browse files
authored
Rollup merge of rust-lang#107086 - clubby789:bootstrap-lock-pid-linux, r=albertlarsan68
Print PID holding bootstrap build lock on Linux Partially address rust-lang#107077 Parse `/proc/locks` to find the PID of the process which created the build directory lock
2 parents 8e25863 + 72117ab commit d73aba8

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/bootstrap/bin/main.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@ fn main() {
1616
let mut build_lock;
1717
let _build_lock_guard;
1818
if cfg!(any(unix, windows)) {
19-
build_lock = fd_lock::RwLock::new(t!(std::fs::File::create(config.out.join("lock"))));
19+
let path = config.out.join("lock");
20+
build_lock = fd_lock::RwLock::new(t!(std::fs::File::create(&path)));
2021
_build_lock_guard = match build_lock.try_write() {
2122
Ok(lock) => lock,
2223
err => {
23-
println!("warning: build directory locked, waiting for lock");
2424
drop(err);
25+
if let Some(pid) = get_lock_owner(&path) {
26+
println!("warning: build directory locked by process {pid}, waiting for lock");
27+
} else {
28+
println!("warning: build directory locked, waiting for lock");
29+
}
2530
t!(build_lock.write())
2631
}
2732
};
@@ -98,3 +103,30 @@ fn check_version(config: &Config) -> Option<String> {
98103

99104
Some(msg)
100105
}
106+
107+
/// Get the PID of the process which took the write lock by
108+
/// parsing `/proc/locks`.
109+
#[cfg(target_os = "linux")]
110+
fn get_lock_owner(f: &std::path::Path) -> Option<u64> {
111+
use std::fs::File;
112+
use std::io::{BufRead, BufReader};
113+
use std::os::unix::fs::MetadataExt;
114+
115+
let lock_inode = std::fs::metadata(f).ok()?.ino();
116+
let lockfile = File::open("/proc/locks").ok()?;
117+
BufReader::new(lockfile).lines().find_map(|line| {
118+
// pid--vvvvvv vvvvvvv--- inode
119+
// 21: FLOCK ADVISORY WRITE 359238 08:02:3719774 0 EOF
120+
let line = line.ok()?;
121+
let parts = line.split_whitespace().collect::<Vec<_>>();
122+
let (pid, inode) = (parts[4].parse::<u64>().ok()?, &parts[5]);
123+
let inode = inode.rsplit_once(':')?.1.parse::<u64>().ok()?;
124+
if inode == lock_inode { Some(pid) } else { None }
125+
})
126+
}
127+
128+
#[cfg(not(target_os = "linux"))]
129+
fn get_lock_owner(_: &std::path::Path) -> Option<u64> {
130+
// FIXME: Implement on other OS's
131+
None
132+
}

0 commit comments

Comments
 (0)