Skip to content

Commit

Permalink
Merge pull request #1091 from omertuc/loginctl
Browse files Browse the repository at this point in the history
reinstall: handle loginctl compatibility issues
  • Loading branch information
cgwalters authored Feb 7, 2025
2 parents e6a370e + ac07fb6 commit 3cadf4a
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions system-reinstall-bootc/src/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ use std::process::Command;
use uzers::os::unix::UserExt;

fn loginctl_users() -> Result<Vec<String>> {
let users: Value = Command::new("loginctl")
.arg("list-sessions")
.arg("--output")
.arg("json")
.run_and_parse_json()
.context("loginctl failed")?;
let users = loginctl_run_compat()?;

users
.as_array()
Expand All @@ -39,6 +34,25 @@ fn loginctl_users() -> Result<Vec<String>> {
.context("error parsing users")
}

/// Run `loginctl` with some compatibility maneuvers to get JSON output
fn loginctl_run_compat() -> Result<Value> {
let mut command = Command::new("loginctl");
command.arg("list-sessions").arg("--output").arg("json");
let output = command.run_get_output().context("running loginctl")?;
let users: Value = match serde_json::from_reader(output) {
Ok(users) => users,
// Failing to parse means loginctl is not outputting JSON despite `--output`
// (https://github.com/systemd/systemd/issues/15275), we need to use the `--json` flag
Err(_err) => Command::new("loginctl")
.arg("list-sessions")
.arg("--json")
.arg("short")
.run_and_parse_json()
.context("running loginctl")?,
};
Ok(users)
}

struct UidChange {
uid: Uid,
euid: Uid,
Expand Down

0 comments on commit 3cadf4a

Please sign in to comment.