1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use std::path::Path;
use crate::error::{Error, Result};
use crate::util::parse;
/// The type of a file lock from `/proc/locks`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LockType {
/// A BSD file lock created with `flock(2)`.
Flock,
/// An open file description (OFD) lock created with `fcntl(2)`.
Ofdlck,
/// A POSIX byte-range lock created with `fcntl(2)`.
Posix,
}
/// Whether a lock is advisory or mandatory.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LockClass {
/// An advisory lock (cooperative, requires the holder to check).
Advisory,
/// A mandatory lock (enforced by the kernel).
Mandatory,
}
/// The access mode of a file lock.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LockAccess {
/// A shared / read lock (POSIX or OFD) or a BSD shared lock.
Read,
/// An exclusive / write lock (POSIX or OFD) or a BSD exclusive lock.
Write,
}
/// A single file lock or lease from `/proc/locks`.
///
/// Each line represents one active `flock(2)`, `fcntl(2)`, or
/// lease lock held by a process. OFD locks show a PID of `-1`
/// because they are not owned by a single process.
#[derive(Debug)]
pub struct Lock {
/// Lock type: BSD (`flock`), POSIX, or OFD.
pub lock_type: LockType,
/// Advisory or mandatory.
pub class: LockClass,
/// Read (shared) or write (exclusive).
pub access: LockAccess,
/// PID of the lock owner, or `-1` for OFD locks.
pub pid: i32,
/// Major device number of the filesystem (reported in hexadecimal).
pub major: u32,
/// Minor device number of the filesystem (reported in hexadecimal).
pub minor: u32,
/// Inode number of the locked file.
pub inode: u64,
/// Byte offset of the first byte of the lock.
pub start: u64,
/// Byte offset of the last byte of the lock, or `None` if the
/// lock extends to end of file (`EOF`).
pub end: Option<u64>,
}
/// Reads `/proc/locks` and returns all active file locks and leases.
///
/// Each line starts with an ordinal (`N:`) followed by seven
/// whitespace-separated fields: type, class, access, pid,
/// `major:minor:inode`, start, and end (or `EOF`).
pub fn locks() -> Result<Vec<Lock>> {
let path = Path::new("/proc/locks");
let bytes = parse::read_file(path)?;
let mut out: Vec<Lock> = Vec::new();
for (line_num, line) in bytes
.split(|&b| b == b'\n')
.filter(|l| !l.is_empty())
.enumerate()
{
let fields = parse::SplitFields::<8>::new(line);
if fields.len() < 8 {
return Err(Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "expected 'N: TYPE CLASS ACCESS PID DEV:INO START END'",
});
}
// skipping fields[0] as its the ordinal (e.g. "1:").
let lock_type = match fields[1] {
b"FLOCK" => LockType::Flock,
b"OFDLCK" => LockType::Ofdlck,
b"POSIX" => LockType::Posix,
_ => {
return Err(Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid lock type",
});
}
};
let class = match fields[2] {
b"ADVISORY" => LockClass::Advisory,
b"MANDATORY" => LockClass::Mandatory,
_ => {
return Err(Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid class",
});
}
};
let access = match fields[3] {
b"READ" => LockAccess::Read,
b"WRITE" => LockAccess::Write,
_ => {
return Err(Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid access",
});
}
};
let pid = parse::parse_dec_i64(fields[4]).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid pid",
})? as i32;
let (major_b, rest) = parse::split_at_byte(fields[5], b':');
let (minor_b, inode_b) = parse::split_at_byte(rest, b':');
let major = parse::parse_hex_u64_fast(major_b).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid major",
})? as u32;
let minor = parse::parse_hex_u64_fast(minor_b).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid minor",
})? as u32;
let inode = parse::parse_dec_u64_fast(inode_b).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid inode",
})?;
let start = parse::parse_dec_u64_fast(fields[6]).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid start",
})?;
let end = match fields[7] {
b"EOF" => None,
_ => Some(
parse::parse_dec_u64_fast(fields[7]).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid end",
})?,
),
};
out.push(Lock {
lock_type,
class,
access,
pid,
major,
minor,
inode,
start,
end,
});
}
Ok(out)
}