X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flogind-session-dbus.c;h=dc0ef5b3d6336c9370d7ed2f31dc09876231aebb;hb=7fc2a89a7387db1e5daa4892393c9e9536920c25;hp=8b5b3ad51f8210d83572d5480c3506c0c5e71593;hpb=a185c5aa2d8bef98716f8cf160da263c17e588b2;p=elogind.git diff --git a/src/logind-session-dbus.c b/src/logind-session-dbus.c index 8b5b3ad51..dc0ef5b3d 100644 --- a/src/logind-session-dbus.c +++ b/src/logind-session-dbus.c @@ -36,7 +36,11 @@ " \n" \ " \n" \ " \n" \ - " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ " \n" \ " \n" \ " \n" \ @@ -49,6 +53,7 @@ " \n" \ " \n" \ " \n" \ + " \n" \ " \n" \ " \n" \ " \n" \ @@ -144,7 +149,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 +164,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 +241,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 +255,121 @@ 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")) { + + if (session_send_signal(s, streq(dbus_message_get_member(message), "Lock")) < 0) + 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 if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Kill")) { + const char *swho; + int32_t signo; + KillWho who; + + if (!dbus_message_get_args( + message, + &error, + DBUS_TYPE_STRING, &swho, + DBUS_TYPE_INT32, &signo, + DBUS_TYPE_INVALID)) + return bus_send_error_reply(connection, message, &error, -EINVAL); + + if (isempty(swho)) + who = KILL_ALL; + else { + who = kill_who_from_string(swho); + if (who < 0) + return bus_send_error_reply(connection, message, &error, -EINVAL); + } + + if (signo <= 0 || signo >= _NSIG) + return bus_send_error_reply(connection, message, &error, -EINVAL); + + r = session_kill(s, who, signo); + if (r < 0) + return bus_send_error_reply(connection, message, NULL, r); + + 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 +419,97 @@ 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; +} + +int session_send_lock(Session *s, bool lock) { + DBusMessage *m; + bool b; + char *p; + + assert(s); + + p = session_bus_path(s); + if (!p) + return -ENOMEM; + + m = dbus_message_new_signal(p, "org.freedesktop.login1.Session", lock ? "Lock" : "Unlock"); + free(p); + + if (!m) + return -ENOMEM; + + b = dbus_connection_send(s->manager->bus, m, NULL); + dbus_message_unref(m); + + if (!b) + return -ENOMEM; + + return 0; +}