X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Flogind-session-dbus.c;h=eca33e5e24788673e38df8cf9d97a9b1ad7f76f2;hp=8b5b3ad51f8210d83572d5480c3506c0c5e71593;hb=1c7dde3e475978c569a982d65fd86d4b4e3caad8;hpb=a185c5aa2d8bef98716f8cf160da263c17e588b2 diff --git a/src/logind-session-dbus.c b/src/logind-session-dbus.c index 8b5b3ad51..eca33e5e2 100644 --- a/src/logind-session-dbus.c +++ b/src/logind-session-dbus.c @@ -36,7 +36,7 @@ " \n" \ " \n" \ " \n" \ - " \n" \ + " \n" \ " \n" \ " \n" \ " \n" \ @@ -49,6 +49,7 @@ " \n" \ " \n" \ " \n" \ + " \n" \ " \n" \ " \n" \ " \n" \ @@ -144,7 +145,7 @@ static int bus_session_append_user(DBusMessageIter *i, const char *property, voi static int bus_session_append_active(DBusMessageIter *i, const char *property, void *data) { Session *s = data; - bool b; + dbus_bool_t b; assert(i); assert(property); @@ -159,13 +160,13 @@ static int bus_session_append_active(DBusMessageIter *i, const char *property, v static int bus_session_append_idle_hint(DBusMessageIter *i, const char *property, void *data) { Session *s = data; - bool b; + int b; assert(i); assert(property); assert(s); - b = session_get_idle_hint(s, NULL); + b = session_get_idle_hint(s, NULL) > 0; if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b)) return -ENOMEM; @@ -236,6 +237,7 @@ static DBusHandlerResult session_message_dispatch( { "org.freedesktop.login1.Session", "Remote", bus_property_append_bool, "b", &s->remote }, { "org.freedesktop.login1.Session", "RemoteUser", bus_property_append_string, "s", s->remote_user }, { "org.freedesktop.login1.Session", "RemoteHost", bus_property_append_string, "s", s->remote_host }, + { "org.freedesktop.login1.Session", "Service", bus_property_append_string, "s", s->service }, { "org.freedesktop.login1.Session", "Leader", bus_property_append_pid, "u", &s->leader }, { "org.freedesktop.login1.Session", "Audit", bus_property_append_uint32, "u", &s->audit_id }, { "org.freedesktop.login1.Session", "Type", bus_session_append_type, "s", &s->type }, @@ -249,11 +251,98 @@ static DBusHandlerResult session_message_dispatch( { NULL, NULL, NULL, NULL, NULL } }; + DBusError error; + DBusMessage *reply = NULL; + int r; + assert(s); assert(connection); assert(message); - return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties); + dbus_error_init(&error); + + if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Terminate")) { + + r = session_stop(s); + if (r < 0) + return bus_send_error_reply(connection, message, NULL, r); + + reply = dbus_message_new_method_return(message); + if (!reply) + goto oom; + + } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Activate")) { + + r = session_activate(s); + if (r < 0) + return bus_send_error_reply(connection, message, NULL, r); + + reply = dbus_message_new_method_return(message); + if (!reply) + goto oom; + + } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Lock") || + dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Unlock")) { + bool b; + DBusMessage *sig; + + sig = dbus_message_new_signal(dbus_message_get_path(message), "org.freedesktop.login1.Session", dbus_message_get_member(message)); + if (!sig) + goto oom; + + b = dbus_connection_send(connection, sig, NULL); + dbus_message_unref(sig); + + if (!b) + goto oom; + + reply = dbus_message_new_method_return(message); + if (!reply) + goto oom; + + } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "SetIdleHint")) { + dbus_bool_t b; + unsigned long ul; + + if (!dbus_message_get_args( + message, + &error, + DBUS_TYPE_BOOLEAN, &b, + DBUS_TYPE_INVALID)) + return bus_send_error_reply(connection, message, &error, -EINVAL); + + ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), &error); + if (ul == (unsigned long) -1) + return bus_send_error_reply(connection, message, &error, -EIO); + + if (ul != 0 && ul != s->user->uid) + return bus_send_error_reply(connection, message, NULL, -EPERM); + + session_set_idle_hint(s, b); + + reply = dbus_message_new_method_return(message); + if (!reply) + goto oom; + + } else + return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties); + + if (reply) { + if (!dbus_connection_send(connection, reply, NULL)) + goto oom; + + dbus_message_unref(reply); + } + + return DBUS_HANDLER_RESULT_HANDLED; + +oom: + if (reply) + dbus_message_unref(reply); + + dbus_error_free(&error); + + return DBUS_HANDLER_RESULT_NEED_MEMORY; } static DBusHandlerResult session_message_handler( @@ -303,3 +392,71 @@ char *session_bus_path(Session *s) { return r; } + +int session_send_signal(Session *s, bool new_session) { + DBusMessage *m; + int r = -ENOMEM; + char *p = NULL; + + assert(s); + + m = dbus_message_new_signal("/org/freedesktop/login1", + "org.freedesktop.login1.Manager", + new_session ? "SessionNew" : "SessionRemoved"); + + if (!m) + return -ENOMEM; + + p = session_bus_path(s); + if (!p) + goto finish; + + if (!dbus_message_append_args( + m, + DBUS_TYPE_STRING, &s->id, + DBUS_TYPE_OBJECT_PATH, &p, + DBUS_TYPE_INVALID)) + goto finish; + + if (!dbus_connection_send(s->manager->bus, m, NULL)) + goto finish; + + r = 0; + +finish: + dbus_message_unref(m); + free(p); + + return r; +} + +int session_send_changed(Session *s, const char *properties) { + DBusMessage *m; + int r = -ENOMEM; + char *p = NULL; + + assert(s); + + if (!s->started) + return 0; + + p = session_bus_path(s); + if (!p) + return -ENOMEM; + + m = bus_properties_changed_new(p, "org.freedesktop.login1.Session", 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); + + return r; +}