Skip to content

Commit

Permalink
update_agent: handle new output format for loginctl
Browse files Browse the repository at this point in the history
In systemd 254 the `loginctl list-sessions --json` output was modified slightly:

Before
```
[{"session":"4","uid":1000,"user":"core","seat":"","tty":""}]
```

After
```
[{"session":"17","uid":1000,"user":"core","seat":"","tty":"n/a","state":"active","idle":false,"since":null}]
```

Notice the change from `""` to `"n/a"` for the `tty` field.

This change probably happened in one of:

- systemd/systemd#27769
- systemd/systemd#27740
- systemd/systemd#27606

Let's update the code to handle the new case and rename the function to
be a little more appropriate since the use case for it is narrow.

Fixes coreos/fedora-coreos-tracker#1547
  • Loading branch information
dustymabe committed Aug 15, 2023
1 parent ec7705c commit 6db7656
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/update_agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ lazy_static::lazy_static! {
#[derive(Debug, Deserialize)]
pub struct SessionJson {
user: String,
#[serde(deserialize_with = "empty_string_as_none")]
#[serde(deserialize_with = "deserialize_systemd_tty_canonicalized")]
tty: Option<String>,
}

Expand All @@ -79,14 +79,16 @@ pub struct InteractiveSession {
tty_dev: String,
}

/// Function to deserialize field to `Option<String>`, where empty strings are
/// deserialized into `None`.
fn empty_string_as_none<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
/// Function to deserialize field to `Option<String>`, where empty strings or
/// `n/a` (not available) strings are deserialized into `None`. In systemd v254+
/// loginctl list-sessions --json started outputting `n/a` instead of an empty
/// string for tty.
fn deserialize_systemd_tty_canonicalized<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
if s.is_empty() {
if s.is_empty() || s == "n/a" {
Ok(None)
} else {
Ok(Some(s))
Expand Down

0 comments on commit 6db7656

Please sign in to comment.