-static int print_property(const char *name, DBusMessageIter *iter) {
- assert(name);
- assert(iter);
-
- if (arg_property && !strv_find(arg_property, name))
- return 0;
-
- switch (dbus_message_iter_get_arg_type(iter)) {
-
- case DBUS_TYPE_STRUCT: {
- DBusMessageIter sub;
-
- dbus_message_iter_recurse(iter, &sub);
-
- if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING &&
- (streq(name, "Display") || streq(name, "ActiveSession"))) {
- const char *s;
-
- dbus_message_iter_get_basic(&sub, &s);
-
- if (arg_all || !isempty(s))
- printf("%s=%s\n", name, s);
- return 0;
- }
- break;
- }
-
- case DBUS_TYPE_ARRAY:
-
- if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Sessions")) {
- DBusMessageIter sub, sub2;
- bool found = false;
-
- dbus_message_iter_recurse(iter, &sub);
- while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
- const char *id;
- const char *path;
-
- dbus_message_iter_recurse(&sub, &sub2);
-
- if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) >= 0 &&
- bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &path, false) >= 0) {
- if (found)
- printf(" %s", id);
- else {
- printf("%s=%s", name, id);
- found = true;
- }
- }
-
- dbus_message_iter_next(&sub);
- }
-
- if (!found && arg_all)
- printf("%s=\n", name);
- else if (found)
- printf("\n");
-
- return 0;
- }
-
- break;
- }
-
- if (generic_print_property(name, iter, arg_all) > 0)
- return 0;
-
- if (arg_all)
- printf("%s=[unprintable]\n", name);
-
- return 0;
-}
-
-static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
- DBusMessage *m = NULL, *reply = NULL;
- const char *interface = "";
- int r;
- DBusError error;
- DBusMessageIter iter, sub, sub2, sub3;
- SessionStatusInfo session_info;
- UserStatusInfo user_info;
- SeatStatusInfo seat_info;
-
- assert(bus);
- assert(path);
- assert(new_line);
-
- zero(session_info);
- zero(user_info);
- zero(seat_info);
-
- dbus_error_init(&error);
-
- m = dbus_message_new_method_call(
- "org.freedesktop.login1",
- path,
- "org.freedesktop.DBus.Properties",
- "GetAll");
- if (!m) {
- log_error("Could not allocate message.");
- r = -ENOMEM;
- goto finish;
- }
-
- if (!dbus_message_append_args(m,
- DBUS_TYPE_STRING, &interface,
- DBUS_TYPE_INVALID)) {
- log_error("Could not append arguments to message.");
- r = -ENOMEM;
- goto finish;
- }
-
- reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
- if (!reply) {
- log_error("Failed to issue method call: %s", bus_error_message(&error));
- r = -EIO;
- goto finish;
- }
-
- if (!dbus_message_iter_init(reply, &iter) ||
- dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
- dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
- log_error("Failed to parse reply.");
- r = -EIO;
- goto finish;
- }
-
- dbus_message_iter_recurse(&iter, &sub);
-
- if (*new_line)
- printf("\n");
-
- *new_line = true;
-
- while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
- const char *name;
-
- if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
- log_error("Failed to parse reply.");
- r = -EIO;
- goto finish;
- }
-
- dbus_message_iter_recurse(&sub, &sub2);
-
- if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
- log_error("Failed to parse reply.");
- r = -EIO;
- goto finish;
- }
-
- if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
- log_error("Failed to parse reply.");
- r = -EIO;
- goto finish;
- }
-
- dbus_message_iter_recurse(&sub2, &sub3);
-
- if (show_properties)
- r = print_property(name, &sub3);
- else if (strstr(verb, "session"))
- r = status_property_session(name, &sub3, &session_info);
- else if (strstr(verb, "user"))
- r = status_property_user(name, &sub3, &user_info);
- else
- r = status_property_seat(name, &sub3, &seat_info);
-
- if (r < 0) {
- log_error("Failed to parse reply.");
- r = -EIO;
- goto finish;
- }
-
- dbus_message_iter_next(&sub);
- }
-
- if (!show_properties) {
- if (strstr(verb, "session"))
- print_session_status_info(&session_info);
- else if (strstr(verb, "user"))
- print_user_status_info(&user_info);
- else
- print_seat_status_info(&seat_info);
- }
-
- strv_free(seat_info.sessions);
- strv_free(user_info.sessions);
-
- r = 0;
-
-finish:
- if (m)
- dbus_message_unref(m);
-
- if (reply)
- dbus_message_unref(reply);
-
- dbus_error_free(&error);
-
- return r;
-}
-
-static int show(DBusConnection *bus, char **args, unsigned n) {
- DBusMessage *m = NULL, *reply = NULL;
- int r, ret = 0;
- DBusError error;
- unsigned i;
- bool show_properties, new_line = false;