Skip to content

Commit 3cadf4a

Browse files
authored
Merge pull request #1091 from omertuc/loginctl
reinstall: handle loginctl compatibility issues
2 parents e6a370e + ac07fb6 commit 3cadf4a

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

system-reinstall-bootc/src/users.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@ use std::process::Command;
1111
use uzers::os::unix::UserExt;
1212

1313
fn loginctl_users() -> Result<Vec<String>> {
14-
let users: Value = Command::new("loginctl")
15-
.arg("list-sessions")
16-
.arg("--output")
17-
.arg("json")
18-
.run_and_parse_json()
19-
.context("loginctl failed")?;
14+
let users = loginctl_run_compat()?;
2015

2116
users
2217
.as_array()
@@ -39,6 +34,25 @@ fn loginctl_users() -> Result<Vec<String>> {
3934
.context("error parsing users")
4035
}
4136

37+
/// Run `loginctl` with some compatibility maneuvers to get JSON output
38+
fn loginctl_run_compat() -> Result<Value> {
39+
let mut command = Command::new("loginctl");
40+
command.arg("list-sessions").arg("--output").arg("json");
41+
let output = command.run_get_output().context("running loginctl")?;
42+
let users: Value = match serde_json::from_reader(output) {
43+
Ok(users) => users,
44+
// Failing to parse means loginctl is not outputting JSON despite `--output`
45+
// (https://github.com/systemd/systemd/issues/15275), we need to use the `--json` flag
46+
Err(_err) => Command::new("loginctl")
47+
.arg("list-sessions")
48+
.arg("--json")
49+
.arg("short")
50+
.run_and_parse_json()
51+
.context("running loginctl")?,
52+
};
53+
Ok(users)
54+
}
55+
4256
struct UidChange {
4357
uid: Uid,
4458
euid: Uid,

0 commit comments

Comments
 (0)