Skip to content

Commit

Permalink
Try both --output=json and --json=short when running `loginctl li…
Browse files Browse the repository at this point in the history
…st-sessions` output (#1759)
  • Loading branch information
RebeccaMahany authored Jun 25, 2024
1 parent edc91c6 commit 25f1513
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions ee/consoleuser/consoleuser_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,9 @@ type listSessionsResult []struct {
}

func CurrentUids(ctx context.Context) ([]string, error) {
cmd, err := allowedcmd.Loginctl(ctx, "list-sessions", "--no-legend", "--no-pager", "--output=json")
sessions, err := listSessions(ctx)
if err != nil {
return nil, fmt.Errorf("creating loginctl command: %w", err)
}
output, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("loginctl list-sessions: %w", err)
}

// unmarshall json output into listSessionsResult
var sessions listSessionsResult
if err := json.Unmarshal(output, &sessions); err != nil {
return nil, fmt.Errorf("loginctl list-sessions unmarshall json output: %w", err)
return nil, fmt.Errorf("listing sessions: %w", err)
}

var uids []string
Expand Down Expand Up @@ -73,3 +63,38 @@ func CurrentUids(ctx context.Context) ([]string, error) {

return uids, nil
}

// listSessions execs `loginctl list-sessions` in order to retrieve the current list of sessions.
// Depending on the systemd version, we have to use different flags to output the results as JSON.
// We may want to attempt parsing the output regardless in the future -- see launcher #1522.
func listSessions(ctx context.Context) (listSessionsResult, error) {
var sessions listSessionsResult

// Try with `--output=json` first, to support the more widely-used older versions of systemd
legacyCmd, err := allowedcmd.Loginctl(ctx, "list-sessions", "--no-legend", "--no-pager", "--output=json")
if err != nil {
return nil, fmt.Errorf("creating loginctl command --no-legend --no-pager --output=json: %w", err)
}
legacyOut, err := legacyCmd.Output()
if err == nil {
// Newer versions of systemd ignore `--output=json` rather than returning an error, so we also
// need to unmarshal the result to confirm we got expected output.
if err := json.Unmarshal(legacyOut, &sessions); err == nil {
return sessions, nil
}
}

cmd, err := allowedcmd.Loginctl(ctx, "list-sessions", "--no-legend", "--no-pager", "--json=short")
if err != nil {
return nil, fmt.Errorf("loginctl list-sessions --no-legend --no-pager --json=short: %w", err)
}
output, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("loginctl list-sessions: %w", err)
}
if err := json.Unmarshal(output, &sessions); err != nil {
return nil, fmt.Errorf("unmarshalling loginctl list-sessions output: %w", err)
}

return sessions, nil
}

0 comments on commit 25f1513

Please sign in to comment.