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
use std::path::Path;
use crate::error::Result;
use crate::util::parse;
/// Reads the kernel boot command line from `/proc/cmdline`.
///
/// Returns the boot parameters as a list of space-separated arguments,
/// each either a `key=value` pair (e.g. `root=UUID=...`) or a bare
/// flag (e.g. `rw`, `quiet`). The trailing newline is stripped.
///
/// The kernel stores the string passed by the bootloader verbatim and
/// does not re-parse quoting. Bootloaders such as GRUB may therefore
/// embed literal `"..."` sections whose inner spaces belong to a single
/// argument. This function treats a quoted section as one token and
/// strips the quote characters, matching the bootloader's intent.
pub fn cmdline() -> Result<Vec<String>> {
let bytes = parse::read_file(Path::new("/proc/cmdline"))?;
let mut args = Vec::new();
let mut cur = Vec::new();
let mut in_quote = false;
let mut iter = bytes.iter();
while let Some(&b) = iter.next() {
match b {
b'"' | b'\'' => in_quote = !in_quote,
b' ' | b'\t' if !in_quote => {
if !cur.is_empty() {
args.push(String::from_utf8_lossy(&cur).into_owned());
cur.clear();
}
}
b'\\' => {
if let Some(&next) = iter.next() {
cur.push(next);
}
}
_ => cur.push(b),
}
}
if !cur.is_empty() {
args.push(String::from_utf8_lossy(&cur).into_owned());
}
Ok(args)
}