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
use std::path::Path;
use bitflags::bitflags;
use crate::error::{Error, Result};
use crate::util::parse;
bitflags! {
/// Console capabilities and driver-state flags from `/proc/consoles`.
///
/// The kernel prints three capability letters (`R` read, `W` write,
/// `U` unblank) followed by a seven-letter driver-state field
/// (`E C B N p b a`). Each letter is present only when the
/// corresponding flag is set, so each maps one-to-one onto a bit.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ConsoleFlags: u16 {
/// Console can be read.
const READ = 1 << 0;
/// Console can be written.
const WRITE = 1 << 1;
/// Console supports unblanking.
const UNBLANK = 1 << 2;
/// `CON_ENABLED`: console accepts printed output.
const ENABLED = 1 << 3;
/// `CON_CONSDEV`: preferred console for `/dev/console`.
const CONSDEV = 1 << 4;
/// `CON_BOOT`: console used during boot.
const BOOT = 1 << 5;
/// `CON_NBCON`: non-blocking console.
const NBCON = 1 << 6;
/// `CON_PRINTBUFFER`: console reads the printk buffer.
const PRINTBUFFER = 1 << 7;
/// `CON_BRL`: braille console.
const BRAILLE = 1 << 8;
/// `CON_ANYTIME`: console usable at any time.
const ANYTIME = 1 << 9;
}
}
/// A single console device from `/proc/consoles`.
///
/// The kernel writes the console name and its index back-to-back with no
/// separator (e.g. the VT console appears as `tty0`), so both are kept in
/// [`Console::name`] as a single string.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Console {
/// Console name, as printed by the kernel (e.g. `tty0`).
pub name: Box<str>,
/// Capabilities and driver-state flags (see [`ConsoleFlags`]).
pub flags: ConsoleFlags,
/// Device `major`, present only when the console has a device.
pub major: Option<u32>,
/// Device `minor`, present only when the console has a device.
pub minor: Option<u32>,
}
/// Reads `/proc/consoles` and returns the registered console devices.
///
/// Each line is fixed-width: the name is padded to 20 columns, followed by
/// the three capability letters and a parenthesised seven-letter flag field.
/// Because the flag field can contain spaces, the line is parsed by locating
/// the parentheses rather than splitting on whitespace.
pub fn consoles() -> Result<Vec<Console>> {
let path = Path::new("/proc/consoles");
let bytes = parse::read_file(path)?;
let mut out = Vec::new();
for (line_num, line) in bytes
.split(|&b| b == b'\n')
.filter(|l| !l.is_empty())
.enumerate()
{
out.push(parse_line(line, path, line_num + 1)?);
}
Ok(out)
}
fn parse_line(line: &[u8], path: &Path, line_num: usize) -> Result<Console> {
let line = parse::trim_end(line);
let open = parse::memchr(b'(', line).ok_or_else(|| Error::Parse {
path: path.to_path_buf(),
line: line_num,
msg: "missing '(' in consoles line",
})?;
let close = parse::memchr(b')', &line[open + 1..])
.map(|i| open + 1 + i)
.ok_or_else(|| Error::Parse {
path: path.to_path_buf(),
line: line_num,
msg: "missing ')' in consoles line",
})?;
// Everything before '(' is `<name (20 wide)><3 capability letters> `.
let head = parse::trim_end(&line[..open]);
if head.len() < 3 {
return Err(Error::Parse {
path: path.to_path_buf(),
line: line_num,
msg: "console line too short",
});
}
let caps = &head[head.len() - 3..];
let name = parse::trim_end(&head[..head.len() - 3]);
let name = std::str::from_utf8(name).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num,
msg: "invalid console name",
})?;
let mut flags = ConsoleFlags::empty();
for (b, bit) in caps.iter().zip([
ConsoleFlags::READ,
ConsoleFlags::WRITE,
ConsoleFlags::UNBLANK,
]) {
if *b != b'-' {
flags |= bit;
}
}
for &b in &line[open + 1..close] {
flags |= match b {
b'E' => ConsoleFlags::ENABLED,
b'C' => ConsoleFlags::CONSDEV,
b'B' => ConsoleFlags::BOOT,
b'N' => ConsoleFlags::NBCON,
b'p' => ConsoleFlags::PRINTBUFFER,
b'b' => ConsoleFlags::BRAILLE,
b'a' => ConsoleFlags::ANYTIME,
_ => ConsoleFlags::empty(),
};
}
let dev = parse::trim(&line[close + 1..]);
let (major, minor) = if dev.is_empty() {
(None, None)
} else {
let (major, minor) = parse::split_at_byte(dev, b':');
let major = parse::parse_dec_u32(major).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num,
msg: "invalid console major",
})?;
let minor = parse::parse_dec_u32(minor).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num,
msg: "invalid console minor",
})?;
(Some(major), Some(minor))
};
Ok(Console {
name: name.into(),
flags,
major,
minor,
})
}