X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flogin%2Flogind-seat-dbus.c;h=c59dfd93388bc4b9d908cd0210ec20717513bd5d;hb=c49b30a23583ff39daaa26696bcab478d2fee0bb;hp=a15689b18f0074ded40e15442ad9fc88a4c6ec79;hpb=addedec48ba0ffc4472ef6d3b5a45c9d4239f1cd;p=elogind.git
diff --git a/src/login/logind-seat-dbus.c b/src/login/logind-seat-dbus.c
index a15689b18..c59dfd933 100644
--- a/src/login/logind-seat-dbus.c
+++ b/src/login/logind-seat-dbus.c
@@ -6,321 +6,308 @@
Copyright 2011 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
- under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
+ Lesser General Public License for more details.
- You should have received a copy of the GNU General Public License
+ You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see .
***/
#include
#include
+#include "util.h"
+#include "bus-util.h"
+#include "strv.h"
#include "logind.h"
#include "logind-seat.h"
-#include "dbus-common.h"
-#include "util.h"
-#define BUS_SEAT_INTERFACE \
- " \n" \
- " \n" \
- " \n" \
- " \n" \
- " \n" \
- " \n" \
- " \n" \
- " \n" \
- " \n" \
- " \n" \
- " \n" \
- " \n" \
- " \n" \
-
-#define INTROSPECTION \
- DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
- "\n" \
- BUS_SEAT_INTERFACE \
- BUS_PROPERTIES_INTERFACE \
- BUS_PEER_INTERFACE \
- BUS_INTROSPECTABLE_INTERFACE \
- "\n"
-
-#define INTERFACES_LIST \
- BUS_GENERIC_INTERFACES_LIST \
- "org.freedesktop.login1.Seat\0"
-
-static int bus_seat_append_active(DBusMessageIter *i, const char *property, void *data) {
- DBusMessageIter sub;
- Seat *s = data;
- const char *id, *path;
- char *p = NULL;
-
- assert(i);
- assert(property);
+static int property_get_active_session(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ sd_bus_error *error,
+ void *userdata) {
+
+ _cleanup_free_ char *p = NULL;
+ Seat *s = userdata;
+
+ assert(bus);
+ assert(reply);
assert(s);
- if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
+ p = s->active ? session_bus_path(s->active) : strdup("/");
+ if (!p)
return -ENOMEM;
- if (s->active) {
- id = s->active->id;
- path = p = session_bus_path(s->active);
+ return sd_bus_message_append(reply, "(so)", s->active ? s->active->id : "", p);
+}
- if (!p)
- return -ENOMEM;
- } else {
- id = "";
- path = "/";
- }
+static int property_get_can_multi_session(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ sd_bus_error *error,
+ void *userdata) {
- if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
- !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path)) {
- free(p);
- return -ENOMEM;
- }
+ Seat *s = userdata;
+
+ assert(bus);
+ assert(reply);
+ assert(s);
- free(p);
+ return sd_bus_message_append(reply, "b", seat_can_multi_session(s));
+}
- if (!dbus_message_iter_close_container(i, &sub))
- return -ENOMEM;
+static int property_get_can_tty(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ sd_bus_error *error,
+ void *userdata) {
- return 0;
+ Seat *s = userdata;
+
+ assert(bus);
+ assert(reply);
+ assert(s);
+
+ return sd_bus_message_append(reply, "b", seat_can_tty(s));
}
-static int bus_seat_append_sessions(DBusMessageIter *i, const char *property, void *data) {
- DBusMessageIter sub, sub2;
- Seat *s = data;
+static int property_get_can_graphical(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ sd_bus_error *error,
+ void *userdata) {
+
+ Seat *s = userdata;
+
+ assert(bus);
+ assert(reply);
+ assert(s);
+
+ return sd_bus_message_append(reply, "b", seat_can_tty(s));
+}
+
+static int property_get_sessions(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ sd_bus_error *error,
+ void *userdata) {
+
+ Seat *s = userdata;
Session *session;
+ int r;
- assert(i);
- assert(property);
+ assert(bus);
+ assert(reply);
assert(s);
- if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "(so)", &sub))
- return -ENOMEM;
+ r = sd_bus_message_open_container(reply, 'a', "(so)");
+ if (r < 0)
+ return r;
LIST_FOREACH(sessions_by_seat, session, s->sessions) {
- char *p;
-
- if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
- return -ENOMEM;
+ _cleanup_free_ char *p = NULL;
p = session_bus_path(session);
if (!p)
return -ENOMEM;
- if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
- free(p);
- return -ENOMEM;
- }
-
- free(p);
+ r = sd_bus_message_append(reply, "(so)", session->id, p);
+ if (r < 0)
+ return r;
- if (!dbus_message_iter_close_container(&sub, &sub2))
- return -ENOMEM;
}
- if (!dbus_message_iter_close_container(i, &sub))
- return -ENOMEM;
+ r = sd_bus_message_close_container(reply);
+ if (r < 0)
+ return r;
- return 0;
+ return 1;
}
-static int bus_seat_append_multi_session(DBusMessageIter *i, const char *property, void *data) {
- Seat *s = data;
- dbus_bool_t b;
-
- assert(i);
- assert(property);
- assert(s);
-
- b = seat_can_multi_session(s);
-
- if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
- return -ENOMEM;
-
- return 0;
-}
+static int property_get_idle_hint(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ sd_bus_error *error,
+ void *userdata) {
-static int bus_seat_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
- Seat *s = data;
- dbus_bool_t b;
+ Seat *s = userdata;
- assert(i);
- assert(property);
+ assert(bus);
+ assert(reply);
assert(s);
- b = seat_get_idle_hint(s, NULL) > 0;
- if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
- return -ENOMEM;
-
- return 0;
+ return sd_bus_message_append(reply, "b", seat_get_idle_hint(s, NULL) > 0);
}
-static int bus_seat_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
- Seat *s = data;
+static int property_get_idle_since_hint(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ sd_bus_error *error,
+ void *userdata) {
+
+ Seat *s = userdata;
dual_timestamp t;
- uint64_t k;
+ uint64_t u;
+ int r;
- assert(i);
- assert(property);
+ assert(bus);
+ assert(reply);
assert(s);
- seat_get_idle_hint(s, &t);
- k = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
+ r = seat_get_idle_hint(s, &t);
+ if (r < 0)
+ return r;
- if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &k))
- return -ENOMEM;
+ u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
- return 0;
+ return sd_bus_message_append(reply, "t", u);
}
-static int get_seat_for_path(Manager *m, const char *path, Seat **_s) {
- Seat *s;
- char *id;
-
- assert(m);
- assert(path);
- assert(_s);
-
- if (!startswith(path, "/org/freedesktop/login1/seat/"))
- return -EINVAL;
-
- id = bus_path_unescape(path + 29);
- if (!id)
- return -ENOMEM;
+static int method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata) {
+ Seat *s = userdata;
+ int r;
- s = hashmap_get(m->seats, id);
- free(id);
+ assert(bus);
+ assert(message);
+ assert(s);
- if (!s)
- return -ENOENT;
+ r = seat_stop_sessions(s);
+ if (r < 0)
+ return sd_bus_reply_method_errno(bus, message, r, NULL);
- *_s = s;
- return 0;
+ return sd_bus_reply_method_return(bus, message, NULL);
}
-static DBusHandlerResult seat_message_dispatch(
- Seat *s,
- DBusConnection *connection,
- DBusMessage *message) {
-
- const BusProperty properties[] = {
- { "org.freedesktop.login1.Seat", "Id", bus_property_append_string, "s", s->id },
- { "org.freedesktop.login1.Seat", "ActiveSession", bus_seat_append_active, "(so)", s },
- { "org.freedesktop.login1.Seat", "CanMultiSession", bus_seat_append_multi_session, "b", s },
- { "org.freedesktop.login1.Seat", "Sessions", bus_seat_append_sessions, "a(so)", s },
- { "org.freedesktop.login1.Seat", "IdleHint", bus_seat_append_idle_hint, "b", s },
- { "org.freedesktop.login1.Seat", "IdleSinceHint", bus_seat_append_idle_hint_since, "t", s },
- { "org.freedesktop.login1.Seat", "IdleSinceHintMonotonic", bus_seat_append_idle_hint_since, "t", s },
- { NULL, NULL, NULL, NULL, NULL }
- };
-
- DBusError error;
- DBusMessage *reply = NULL;
+static int method_activate_session(sd_bus *bus, sd_bus_message *message, void *userdata) {
+ Seat *s = userdata;
+ const char *name;
+ Session *session;
int r;
- assert(s);
- assert(connection);
+ assert(bus);
assert(message);
+ assert(s);
- dbus_error_init(&error);
+ r = sd_bus_message_read(message, "s", &name);
+ if (r < 0)
+ return sd_bus_reply_method_errno(bus, message, r, NULL);
- if (dbus_message_is_method_call(message, "org.freedesktop.login1.Seat", "Terminate")) {
+ session = hashmap_get(s->manager->sessions, name);
+ if (!session)
+ return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
- r = seat_stop_sessions(s);
- if (r < 0)
- return bus_send_error_reply(connection, message, NULL, r);
+ if (session->seat != s)
+ return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", name, s->id);
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+ r = session_activate(session);
+ if (r < 0)
+ return sd_bus_reply_method_errno(bus, message, r, NULL);
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Seat", "ActivateSession")) {
- const char *name;
- Session *session;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- session = hashmap_get(s->manager->sessions, name);
- if (!session || session->seat != s)
- return bus_send_error_reply(connection, message, &error, -ENOENT);
+ return sd_bus_reply_method_return(bus, message, NULL);
+}
- r = session_activate(session);
- if (r < 0)
- return bus_send_error_reply(connection, message, NULL, r);
+const sd_bus_vtable seat_vtable[] = {
+ SD_BUS_VTABLE_START(0),
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
- } else
- return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
+ SD_BUS_PROPERTY("Id", "s", NULL, offsetof(Seat, id), 0),
+ SD_BUS_PROPERTY("ActiveSession", "(so)", property_get_active_session, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+ SD_BUS_PROPERTY("CanMultiSession", "b", property_get_can_multi_session, 0, 0),
+ SD_BUS_PROPERTY("CanTTY", "b", property_get_can_tty, 0, 0),
+ SD_BUS_PROPERTY("CanGraphical", "b", property_get_can_graphical, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+ SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+ SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+ SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+ SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
- if (reply) {
- if (!dbus_connection_send(connection, reply, NULL))
- goto oom;
+ SD_BUS_METHOD("Terminate", NULL, NULL, method_terminate, 0),
+ SD_BUS_METHOD("ActivateSession", "s", NULL, method_activate_session, 0),
- dbus_message_unref(reply);
- }
+ SD_BUS_VTABLE_END
+};
- return DBUS_HANDLER_RESULT_HANDLED;
+int seat_object_find(sd_bus *bus, const char *path, const char *interface, void **found, void *userdata) {
+ Manager *m = userdata;
+ Seat *seat;
+ int r;
-oom:
- if (reply)
- dbus_message_unref(reply);
+ assert(bus);
+ assert(path);
+ assert(interface);
+ assert(found);
+ assert(m);
- dbus_error_free(&error);
+ if (streq(path, "/org/freedesktop/login1/seat/self")) {
+ sd_bus_message *message;
+ Session *session;
+ pid_t pid;
- return DBUS_HANDLER_RESULT_NEED_MEMORY;
-}
+ message = sd_bus_get_current(bus);
+ if (!message)
+ return 0;
-static DBusHandlerResult seat_message_handler(
- DBusConnection *connection,
- DBusMessage *message,
- void *userdata) {
+ r = sd_bus_get_owner_pid(bus, sd_bus_message_get_sender(message), &pid);
+ if (r < 0)
+ return 0;
- Manager *m = userdata;
- Seat *s;
- int r;
+ r = manager_get_session_by_pid(m, pid, &session);
+ if (r <= 0)
+ return 0;
- r = get_seat_for_path(m, dbus_message_get_path(message), &s);
- if (r < 0) {
+ if (!session->seat)
+ return 0;
- if (r == -ENOMEM)
- return DBUS_HANDLER_RESULT_NEED_MEMORY;
+ seat = session->seat;
+ } else {
+ _cleanup_free_ char *e = NULL;
+ const char *p;
- if (r == -ENOENT) {
- DBusError e;
+ p = startswith(path, "/org/freedesktop/login1/seat/");
+ if (!p)
+ return 0;
- dbus_error_init(&e);
- dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown seat");
- return bus_send_error_reply(connection, message, &e, r);
- }
+ e = bus_path_unescape(p);
+ if (!e)
+ return -ENOMEM;
- return bus_send_error_reply(connection, message, NULL, r);
+ seat = hashmap_get(m->seats, e);
+ if (!seat)
+ return 0;
}
- return seat_message_dispatch(s, connection, message);
+ *found = seat;
+ return 1;
}
-const DBusObjectPathVTable bus_seat_vtable = {
- .message_function = seat_message_handler
-};
-
char *seat_bus_path(Seat *s) {
- char *t, *r;
+ _cleanup_free_ char *t = NULL;
assert(s);
@@ -328,53 +315,60 @@ char *seat_bus_path(Seat *s) {
if (!t)
return NULL;
- r = strappend("/org/freedesktop/login1/seat/", t);
- free(t);
-
- return r;
+ return strappend("/org/freedesktop/login1/seat/", t);
}
-int seat_send_signal(Seat *s, bool new_seat) {
- DBusMessage *m;
- int r = -ENOMEM;
- char *p = NULL;
+int seat_node_enumerator(sd_bus *bus, const char *path, char ***nodes, void *userdata) {
+ _cleanup_strv_free_ char **l = NULL;
+ Manager *m = userdata;
+ Seat *seat;
+ Iterator i;
+ int r;
- assert(s);
+ assert(bus);
+ assert(path);
+ assert(nodes);
- m = dbus_message_new_signal("/org/freedesktop/login1",
- "org.freedesktop.login1.Manager",
- new_seat ? "SeatNew" : "SeatRemoved");
+ HASHMAP_FOREACH(seat, m->seats, i) {
+ char *p;
- if (!m)
- return -ENOMEM;
+ p = seat_bus_path(seat);
+ if (!p)
+ return -ENOMEM;
- p = seat_bus_path(s);
- if (!p)
- goto finish;
+ r = strv_push(&l, p);
+ if (r < 0) {
+ free(p);
+ return r;
+ }
+ }
- if (!dbus_message_append_args(
- m,
- DBUS_TYPE_STRING, &s->id,
- DBUS_TYPE_OBJECT_PATH, &p,
- DBUS_TYPE_INVALID))
- goto finish;
+ *nodes = l;
+ l = NULL;
- if (!dbus_connection_send(s->manager->bus, m, NULL))
- goto finish;
+ return 1;
+}
+
+int seat_send_signal(Seat *s, bool new_seat) {
+ _cleanup_free_ char *p = NULL;
- r = 0;
+ assert(s);
-finish:
- dbus_message_unref(m);
- free(p);
+ p = seat_bus_path(s);
+ if (!p)
+ return -ENOMEM;
- return r;
+ return sd_bus_emit_signal(
+ s->manager->bus,
+ "/org/freedesktop/login1",
+ "org.freedesktop.login1.Manager",
+ new_seat ? "SeatNew" : "SeatRemoved",
+ "so", s->id, p);
}
-int seat_send_changed(Seat *s, const char *properties) {
- DBusMessage *m;
- int r = -ENOMEM;
- char *p = NULL;
+int seat_send_changed(Seat *s, const char *properties, ...) {
+ _cleanup_free_ char *p = NULL;
+ char **l;
assert(s);
@@ -385,19 +379,7 @@ int seat_send_changed(Seat *s, const char *properties) {
if (!p)
return -ENOMEM;
- m = bus_properties_changed_new(p, "org.freedesktop.login1.Seat", properties);
- if (!m)
- goto finish;
-
- if (!dbus_connection_send(s->manager->bus, m, NULL))
- goto finish;
-
- r = 0;
-
-finish:
- if (m)
- dbus_message_unref(m);
- free(p);
+ l = strv_from_stdarg_alloca(properties);
- return r;
+ return sd_bus_emit_properties_changed_strv(s->manager->bus, p, "org.freedesktop.login1.Seat", l);
}