Skip to content

Commit

Permalink
loginctl: show session idle status in list-sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
dtardon committed May 24, 2023
1 parent 5b7d153 commit 556723e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/login/loginctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ typedef struct SessionStatusInfo {
const char *state;
const char *scope;
const char *desktop;
bool idle_hint;
dual_timestamp idle_hint_timestamp;
} SessionStatusInfo;

static OutputFlags get_output_flags(void) {
Expand Down Expand Up @@ -138,8 +140,10 @@ static int show_table(Table *table, const char *word) {
static int list_sessions(int argc, char *argv[], void *userdata) {

static const struct bus_properties_map map[] = {
{ "State", "s", NULL, offsetof(SessionStatusInfo, state) },
{ "TTY", "s", NULL, offsetof(SessionStatusInfo, tty) },
{ "IdleHint", "b", NULL, offsetof(SessionStatusInfo, idle_hint) },
{ "IdleSinceHintMonotonic", "t", NULL, offsetof(SessionStatusInfo, idle_hint_timestamp.monotonic) },
{ "State", "s", NULL, offsetof(SessionStatusInfo, state) },
{ "TTY", "s", NULL, offsetof(SessionStatusInfo, tty) },
{},
};

Expand All @@ -161,7 +165,7 @@ static int list_sessions(int argc, char *argv[], void *userdata) {
if (r < 0)
return bus_log_parse_error(r);

table = table_new("session", "uid", "user", "seat", "tty", "state");
table = table_new("session", "uid", "user", "seat", "tty", "state", "idle", "since");
if (!table)
return log_oom();

Expand All @@ -184,12 +188,11 @@ static int list_sessions(int argc, char *argv[], void *userdata) {

r = bus_map_all_properties(bus, "org.freedesktop.login1", object, map, BUS_MAP_BOOLEAN_AS_BOOL, &e, &m, &i);
if (r < 0) {
if (sd_bus_error_has_name(&e, SD_BUS_ERROR_UNKNOWN_OBJECT))
/* The session is already closed when we're querying the property */
continue;

log_warning_errno(r, "Failed to get properties of session %s, ignoring: %s",
id, bus_error_message(&e, r));
log_full_errno(sd_bus_error_has_name(&e, SD_BUS_ERROR_UNKNOWN_OBJECT) ? LOG_DEBUG : LOG_WARNING,
r,
"Failed to get properties of session %s, ignoring: %s",
id, bus_error_message(&e, r));
continue;
}

r = table_add_many(table,
Expand All @@ -198,7 +201,15 @@ static int list_sessions(int argc, char *argv[], void *userdata) {
TABLE_STRING, user,
TABLE_STRING, seat,
TABLE_STRING, strna(i.tty),
TABLE_STRING, i.state);
TABLE_STRING, i.state,
TABLE_BOOLEAN, i.idle_hint);
if (r < 0)
return table_log_add_error(r);

if (i.idle_hint)
r = table_add_cell(table, NULL, TABLE_TIMESTAMP_RELATIVE, &i.idle_hint_timestamp.monotonic);
else
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return table_log_add_error(r);
}
Expand Down
2 changes: 2 additions & 0 deletions test/units/testsuite-35.sh
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,8 @@ testcase_list_users_sessions_seats() {
seat=$(loginctl list-sessions --no-legend | awk '$3 == "logind-test-user" { print $4 }')
assert_eq "$(loginctl list-sessions --no-legend | awk '$3 == "logind-test-user" { print $5 }')" tty2
assert_eq "$(loginctl list-sessions --no-legend | awk '$3 == "logind-test-user" { print $6 }')" active
assert_eq "$(loginctl list-sessions --no-legend | awk '$3 == "logind-test-user" { print $7 }')" no
assert_eq "$(loginctl list-sessions --no-legend | awk '$3 == "logind-test-user" { print $8 }')" ''

loginctl list-seats --no-legend | grep -Fwq "${seat?}"

Expand Down

0 comments on commit 556723e

Please sign in to comment.