Skip to content

Commit

Permalink
loginctl: use bus_map_all_properties
Browse files Browse the repository at this point in the history
(cherry picked from commit 5b7d1536d0c2ccf0b7688490f31c92c1e766ea44)

Related: #2156786
  • Loading branch information
dtardon committed May 30, 2023
1 parent 19e1625 commit 276a13c
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions src/login/loginctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ static bool arg_ask_password = true;
static unsigned arg_lines = 10;
static OutputMode arg_output = OUTPUT_SHORT;

typedef struct SessionStatusInfo {
const char *id;
uid_t uid;
const char *name;
struct dual_timestamp timestamp;
unsigned int vtnr;
const char *seat;
const char *tty;
const char *display;
bool remote;
const char *remote_host;
const char *remote_user;
const char *service;
pid_t leader;
const char *type;
const char *class;
const char *state;
const char *scope;
const char *desktop;
} SessionStatusInfo;

static OutputFlags get_output_flags(void) {

return
Expand Down Expand Up @@ -112,6 +133,12 @@ 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[] = {
{ "TTY", "s", NULL, offsetof(SessionStatusInfo, tty) },
{},
};

_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_(table_unrefp) Table *table = NULL;
Expand Down Expand Up @@ -148,39 +175,33 @@ static int list_sessions(int argc, char *argv[], void *userdata) {

for (;;) {
_cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply_tty = NULL;
const char *id, *user, *seat, *object, *tty = NULL;
const char *id, *user, *seat, *object;
uint32_t uid;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
SessionStatusInfo i = {};

r = sd_bus_message_read(reply, "(susso)", &id, &uid, &user, &seat, &object);
if (r < 0)
return bus_log_parse_error(r);
if (r == 0)
break;

r = sd_bus_get_property(
bus,
"org.freedesktop.login1",
object,
"org.freedesktop.login1.Session",
"TTY",
&e,
&reply_tty,
"s");
if (r < 0)
log_warning_errno(r, "Failed to get TTY for session %s: %s", id, bus_error_message(&e, r));
else {
r = sd_bus_message_read(reply_tty, "s", &tty);
if (r < 0)
return bus_log_parse_error(r);
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));
}

r = table_add_many(table,
TABLE_STRING, id,
TABLE_UINT32, uid,
TABLE_STRING, user,
TABLE_STRING, seat,
TABLE_STRING, strna(tty));
TABLE_STRING, strna(i.tty));
if (r < 0)
return log_error_errno(r, "Failed to add row to table: %m");
}
Expand Down Expand Up @@ -341,27 +362,6 @@ static int show_unit_cgroup(sd_bus *bus, const char *interface, const char *unit
return 0;
}

typedef struct SessionStatusInfo {
const char *id;
uid_t uid;
const char *name;
struct dual_timestamp timestamp;
unsigned int vtnr;
const char *seat;
const char *tty;
const char *display;
bool remote;
const char *remote_host;
const char *remote_user;
const char *service;
pid_t leader;
const char *type;
const char *class;
const char *state;
const char *scope;
const char *desktop;
} SessionStatusInfo;

typedef struct UserStatusInfo {
uid_t uid;
bool linger;
Expand Down

0 comments on commit 276a13c

Please sign in to comment.