logind-dbus.c revision d02608170e599b1ffbc7c9a22062bae2579d6e36
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering This file is part of systemd.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering Copyright 2011 Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering (at your option) any later version.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering systemd is distributed in the hope that it will be useful, but
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering Lesser General Public License for more details.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringint manager_get_session_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Session **ret) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT, &creds);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering r = sd_bus_creds_get_session(creds, &name);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering session = hashmap_get(m->sessions, name);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringint manager_get_user_from_creds(Manager *m, sd_bus_message *message, uid_t uid, sd_bus_error *error, User **ret) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering /* Note that we get the owner UID of the session, not the actual client UID here! */
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering r = sd_bus_creds_get_owner_uid(creds, &uid);
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering user = hashmap_get(m->users, UID_TO_PTR(uid));
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "No user "UID_FMT" known or logged in", uid);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringint manager_get_seat_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Seat **ret) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering r = manager_get_session_from_creds(m, message, NULL, error, &session);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "Session has no seat.");
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, NULL) > 0);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering w = manager_inhibit_what(m, streq(property, "BlockInhibited") ? INHIBIT_BLOCK : INHIBIT_DELAY);
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt return sd_bus_message_append(reply, "s", inhibit_what_to_string(w));
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (streq(property, "PreparingForShutdown"))
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering b = !!(m->action_what & INHIBIT_SHUTDOWN);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering return sd_bus_message_append(reply, "b", b);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers const char *path,
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt r = sd_bus_message_open_container(reply, 'r', "st");
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering r = sd_bus_message_append(reply, "st", m->scheduled_shutdown_type, m->scheduled_shutdown_timeout);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sieversstatic BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers const char *path,
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers return sd_bus_message_append(reply, "b", manager_is_docked_or_external_displays(m));
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sieversstatic int method_get_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers const char *name;
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers r = manager_get_session_from_creds(m, message, name, error, &session);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers return sd_bus_reply_method_return(message, "o", p);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sieversstatic int method_get_session_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering r = sd_bus_message_read(message, "u", &pid);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers r = manager_get_session_from_creds(m, message, NULL, error, &session);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers r = manager_get_session_by_pid(m, pid, &session);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, "PID "PID_FMT" does not belong to any known session", pid);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers return sd_bus_reply_method_return(message, "o", p);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sieversstatic int method_get_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering r = manager_get_user_from_creds(m, message, uid, error, &user);
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering return sd_bus_reply_method_return(message, "o", p);
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poetteringstatic int method_get_user_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering assert_cc(sizeof(pid_t) == sizeof(uint32_t));
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering r = sd_bus_message_read(message, "u", &pid);
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user);
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering r = manager_get_user_by_pid(m, pid, &user);
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, "PID "PID_FMT" does not belong to any known or logged in user", pid);
a0b1209c4a59754f428894e0485413542da50014Zbigniew Jędrzejewski-Szmek return sd_bus_reply_method_return(message, "o", p);
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poetteringstatic int method_get_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers r = manager_get_seat_from_creds(m, message, name, error, &seat);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers return sd_bus_reply_method_return(message, "o", p);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sieversstatic int method_list_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers r = sd_bus_message_new_method_return(message, &reply);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers r = sd_bus_message_open_container(reply, 'a', "(susso)");
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sieversstatic int method_list_users(sd_bus_message *message, void *userdata, sd_bus_error *error) {
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers r = sd_bus_message_new_method_return(message, &reply);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers r = sd_bus_message_open_container(reply, 'a', "(uso)");
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sieversstatic int method_list_seats(sd_bus_message *message, void *userdata, sd_bus_error *error) {
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers r = sd_bus_message_new_method_return(message, &reply);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers r = sd_bus_message_open_container(reply, 'a', "(so)");
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers r = sd_bus_message_append(reply, "(so)", seat->id, p);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sieversstatic int method_list_inhibitors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek r = sd_bus_message_new_method_return(message, &reply);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering r = sd_bus_message_open_container(reply, 'a', "(ssssuu)");
cca1dfddd4ce4357113663532696488427cc54e4Lennart Poettering HASHMAP_FOREACH(inhibitor, m->inhibitors, i) {
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering r = sd_bus_message_append(reply, "(ssssuu)",
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering strempty(inhibit_what_to_string(inhibitor->what)),
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering strempty(inhibit_mode_to_string(inhibitor->mode)),
d2a623823f8d83c97c35fcd28f90e8cd59066f8aZbigniew Jędrzejewski-Szmek r = sd_bus_message_close_container(reply);
d2a623823f8d83c97c35fcd28f90e8cd59066f8aZbigniew Jędrzejewski-Szmek return sd_bus_send(NULL, reply, NULL);
d2a623823f8d83c97c35fcd28f90e8cd59066f8aZbigniew Jędrzejewski-Szmekstatic int method_create_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
d2a623823f8d83c97c35fcd28f90e8cd59066f8aZbigniew Jędrzejewski-Szmek const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host, *desktop;
d2a623823f8d83c97c35fcd28f90e8cd59066f8aZbigniew Jędrzejewski-Szmek r = sd_bus_message_read(message, "uusssssussbss", &uid, &leader, &service, &type, &class, &desktop, &cseat, &vtnr, &tty, &display, &remote, &remote_user, &remote_host);
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID");
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session type %s", type);
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session class %s", class);
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid desktop string %s", desktop);
d2a623823f8d83c97c35fcd28f90e8cd59066f8aZbigniew Jędrzejewski-Szmek seat = hashmap_get(m->seats, cseat);
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", cseat);
d2a623823f8d83c97c35fcd28f90e8cd59066f8aZbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "TTY %s is virtual console but seat %s is not seat0", tty, seat->id);
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot determine VT number from virtual console TTY %s", tty);
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified TTY and VT number do not match");
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but seat is not seat0");
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but VT number is not 0");
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "VT number out of range");
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat has no VTs but VT number not 0");
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering r = sd_bus_message_enter_container(message, 'a', "(sv)");
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering r = sd_bus_creds_get_pid(creds, (pid_t*) &leader);
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering r = manager_get_session_by_pid(m, leader, NULL);
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already running in a session");
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek * Old gdm and lightdm start the user-session on the same VT as
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering * the greeter session. But they destroy the greeter session
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering * after the user-session and want the user-session to take
0238d4c660e732dd03ba0cdb54a29ec5870ee849Kay Sievers * over the VT. We need to support this for
0238d4c660e732dd03ba0cdb54a29ec5870ee849Kay Sievers * backwards-compatibility, so make sure we allow new sessions
0238d4c660e732dd03ba0cdb54a29ec5870ee849Kay Sievers * on a VT that a greeter is running on. Furthermore, to allow
0238d4c660e732dd03ba0cdb54a29ec5870ee849Kay Sievers * re-logins, we have to allow a greeter to take over a used VT for
0238d4c660e732dd03ba0cdb54a29ec5870ee849Kay Sievers * the exact same reasons.
cca1dfddd4ce4357113663532696488427cc54e4Lennart Poettering m->seat0->positions[vtnr]->class != SESSION_GREETER)
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already occupied by a session");
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering audit_session_from_pid(leader, &audit_id);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers /* Keep our session IDs and the audit session IDs in sync */
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers /* Wut? There's already a session by this name and we
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers * didn't find it above? Weird, then let's not trust
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers * the audit data and let's better register a new
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers log_warning("Existing logind session ID %s used by new audit session, ignoring", id);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering if (asprintf(&id, "c%lu", ++m->session_counter) < 0)
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering session->remote_user = strdup(remote_user);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering session->remote_host = strdup(remote_host);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering session->create_message = sd_bus_message_ref(message);
d2a623823f8d83c97c35fcd28f90e8cd59066f8aZbigniew Jędrzejewski-Szmek /* Now, let's wait until the slice unit and stuff got
8086ffacdb1bfec5ec115d24626538bda6cc372eZbigniew Jędrzejewski-Szmek * created. We send the reply back from
8086ffacdb1bfec5ec115d24626538bda6cc372eZbigniew Jędrzejewski-Szmek * session_send_create_reply(). */
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poetteringstatic int method_release_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering r = sd_bus_message_read(message, "s", &name);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering r = manager_get_session_from_creds(m, message, name, error, &session);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering return sd_bus_reply_method_return(message, NULL);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poetteringstatic int method_activate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering r = sd_bus_message_read(message, "s", &name);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering r = manager_get_session_from_creds(m, message, name, error, &session);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering return bus_session_method_activate(message, session, error);
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidtstatic int method_activate_session_on_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering /* Same as ActivateSession() but refuses to work if
61331eab0a53cd9b8446eab6d1ebf1a046d8efc1Lennart Poettering * the seat doesn't match */
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering r = sd_bus_message_read(message, "ss", &session_name, &seat_name);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering r = manager_get_session_from_creds(m, message, session_name, error, &session);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering r = manager_get_seat_from_creds(m, message, seat_name, error, &seat);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", session_name, seat_name);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering return sd_bus_reply_method_return(message, NULL);
3db604b907323b8df0fc810216f6112056d26a02Lennart Poetteringstatic int method_lock_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering r = sd_bus_message_read(message, "s", &name);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering r = manager_get_session_from_creds(m, message, name, error, &session);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering return bus_session_method_lock(message, session, error);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poetteringstatic int method_lock_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
assert(m);
NULL,
&m->polkit_registry,
error);
const char *name;
assert(m);
assert(m);
const char *name;
assert(m);
assert(m);
const char *name;
assert(m);
const char *path;
int interactive;
assert(m);
errno = 0;
if (!pw)
"org.freedesktop.login1.set-user-linger",
NULL,
&m->polkit_registry,
error);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
if (!cc)
return -ENOMEM;
User *u;
user_start(u);
User *u;
return -errno;
assert(m);
return -ENOMEM;
r = udev_enumerate_add_match_parent(e, d);
r = udev_enumerate_scan_devices(e);
return -ENOMEM;
const char *id_for_seat;
assert(m);
return -ENODEV;
return -ENODEV;
if (!id_for_seat)
return -ENODEV;
return -ENOMEM;
if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0)
return -ENOMEM;
return trigger_device(m, d);
assert(m);
int interactive, r;
assert(m);
NULL,
&m->polkit_registry,
error);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
int interactive, r;
assert(m);
NULL,
&m->polkit_registry,
error);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = flush_devices(m);
static int have_multiple_sessions(
Manager *m,
Iterator i;
assert(m);
static int bus_manager_log_shutdown(
Manager *m,
InhibitWhat w,
const char *unit_name) {
assert(m);
if (w != INHIBIT_SHUTDOWN)
q = NULL;
NULL);
assert(e);
assert(m);
assert(m);
if (m->lid_switch_ignore_event_source) {
usec_t u;
if (until <= u)
r = sd_event_add_time(
m->event,
until, 0,
m->scheduled_shutdown_timeout = 0;
m->shutdown_dry_run = false;
if (m->unlink_nologin) {
m->unlink_nologin = false;
static int execute_shutdown_or_sleep(
Manager *m,
InhibitWhat w,
const char *unit_name,
char *c = NULL;
assert(m);
assert(w >= 0);
if (m->shutdown_dry_run) {
r = sd_bus_call_method(
m->bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&reply,
c = strdup(p);
return -ENOMEM;
m->action_job = c;
m->action_what = w;
if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
if (!timeout)
log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
static int manager_inhibit_timeout_handler(
sd_event_source *s,
void *userdata) {
static int delay_shutdown_or_sleep(
Manager *m,
InhibitWhat w,
const char *unit_name) {
assert(m);
assert(w >= 0);
if (m->inhibit_timeout_source) {
m->action_what = w;
assert(m);
assert(w >= 0);
"/org/freedesktop/login1",
signal_name[w],
active);
Manager *m,
const char *unit_name,
InhibitWhat w,
bool delayed;
assert(m);
assert(w >= 0);
send_prepare_for(m, w, true);
delayed =
m->inhibit_delay_max > 0 &&
if (delayed)
static int verify_shutdown_creds(
Manager *m,
InhibitWhat w,
bool interactive,
const char *action,
const char *action_multiple_sessions,
const char *action_ignore_inhibit,
assert(m);
assert(w >= 0);
multiple_sessions = r > 0;
r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
static int method_do_shutdown_or_sleep(
Manager *m,
const char *unit_name,
InhibitWhat w,
const char *action,
const char *action_multiple_sessions,
const char *action_ignore_inhibit,
const char *sleep_verb,
int interactive, r;
assert(m);
assert(w >= 0);
if (m->action_what)
return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
if (sleep_verb) {
return method_do_shutdown_or_sleep(
m, message,
NULL,
error);
return method_do_shutdown_or_sleep(
m, message,
NULL,
error);
return method_do_shutdown_or_sleep(
m, message,
"org.freedesktop.login1.suspend",
"org.freedesktop.login1.suspend-multiple-sessions",
"org.freedesktop.login1.suspend-ignore-inhibit",
error);
static int nologin_timeout_handler(
sd_event_source *s,
void *userdata) {
r = write_string_file("/run/nologin", "System is going down.", WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
m->unlink_nologin = true;
assert(m);
fprintf(f,
_cleanup_free_ char *t;
r = -ENOMEM;
goto fail;
r = fflush_and_check(f);
goto fail;
r = -errno;
goto fail;
fail:
static int manager_scheduled_shutdown_handler(
sd_event_source *s,
void *userdata) {
const char *target;
assert(m);
char *type;
assert(m);
m->shutdown_dry_run = true;
if (m->scheduled_shutdown_timeout_source) {
return log_oom();
if (m->nologin_timeout_source) {
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
const char *tty;
return log_oom();
r = manager_setup_wall_message_timer(m);
r = update_schedule_file(m);
static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
bool cancelled;
assert(m);
if (cancelled) {
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
return method_do_shutdown_or_sleep(
m, message,
"org.freedesktop.login1.hibernate",
"org.freedesktop.login1.hibernate-multiple-sessions",
"org.freedesktop.login1.hibernate-ignore-inhibit",
error);
return method_do_shutdown_or_sleep(
m, message,
"org.freedesktop.login1.hibernate",
"org.freedesktop.login1.hibernate-multiple-sessions",
"org.freedesktop.login1.hibernate-ignore-inhibit",
error);
static int method_can_shutdown_or_sleep(
Manager *m,
InhibitWhat w,
const char *action,
const char *action_multiple_sessions,
const char *action_ignore_inhibit,
const char *sleep_verb,
assert(m);
assert(w >= 0);
if (sleep_verb) {
multiple_sessions = r > 0;
if (multiple_sessions) {
r = bus_test_polkit(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, UID_INVALID, &challenge, error);
else if (challenge)
if (blocked) {
r = bus_test_polkit(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, UID_INVALID, &challenge, error);
if (r > 0 && !result)
else if (challenge)
return method_can_shutdown_or_sleep(
m, message,
NULL,
error);
return method_can_shutdown_or_sleep(
m, message,
NULL,
error);
return method_can_shutdown_or_sleep(
m, message,
"org.freedesktop.login1.suspend",
"org.freedesktop.login1.suspend-multiple-sessions",
"org.freedesktop.login1.suspend-ignore-inhibit",
error);
return method_can_shutdown_or_sleep(
m, message,
"org.freedesktop.login1.hibernate",
"org.freedesktop.login1.hibernate-multiple-sessions",
"org.freedesktop.login1.hibernate-ignore-inhibit",
error);
return method_can_shutdown_or_sleep(
m, message,
"org.freedesktop.login1.hibernate",
"org.freedesktop.login1.hibernate-multiple-sessions",
"org.freedesktop.login1.hibernate-ignore-inhibit",
error);
static int property_get_reboot_to_firmware_setup(
const char *path,
const char *interface,
const char *property,
void *userdata,
r = efi_get_reboot_to_firmware();
if (r < 0 && r != -EOPNOTSUPP)
static int method_set_reboot_to_firmware_setup(
void *userdata,
assert(m);
"org.freedesktop.login1.set-reboot-to-firmware-setup",
NULL,
&m->polkit_registry,
error);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = efi_set_reboot_to_firmware(b);
static int method_can_reboot_to_firmware_setup(
void *userdata,
bool challenge;
const char *result;
assert(m);
if (r == -EOPNOTSUPP)
"org.freedesktop.login1.set-reboot-to-firmware-setup",
NULL,
error);
else if (challenge)
static int method_set_wall_message(
void *userdata,
char *wall_message;
int enable_wall_messages;
assert(m);
"org.freedesktop.login1.set-wall-message",
NULL,
&m->polkit_registry,
error);
return log_oom();
InhibitWhat w;
assert(m);
if (mm < 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Delay inhibitors only supported for shutdown and sleep");
if (m->action_what & w)
return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "The operation inhibition has been requested for is already running");
w == INHIBIT_SHUTDOWN ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
w == INHIBIT_SLEEP ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
NULL,
&m->polkit_registry,
error);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
return -ENOMEM;
i->what = w;
r = -ENOMEM;
goto fail;
if (fifo_fd < 0) {
r = fifo_fd;
goto fail;
inhibitor_start(i);
fail:
inhibitor_free(i);
SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL, NULL, offsetof(Manager, enable_wall_messages), 0),
SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("KillUserProcesses", "b", NULL, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup, 0, SD_BUS_VTABLE_PROPERTY_CONST),
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),
SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_docked), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL, offsetof(Manager, holdoff_timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_METHOD("ListInhibitors", NULL, "a(ssssuu)", method_list_inhibitors, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL, method_activate_session_on_seat, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ScheduleShutdown", "st", NULL, method_schedule_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("CancelScheduledShutdown", NULL, "b", method_cancel_scheduled_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL, "s", method_can_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL, method_set_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
assert(s);
if (!s->started)
sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
r = session_send_create_reply(s, &e);
assert(m);
m->action_what = 0;
if (session) {
if (user) {
assert(m);
if (session)
if (user)
const char *path;
assert(m);
if (!path)
log_oom();
if (session)
if (user)
Iterator i;
assert(m);
"/org/freedesktop/login1",
int manager_start_scope(
const char *scope,
const char *slice,
const char *description,
char **job) {
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
r = sd_bus_message_close_container(m);
if (job) {
char *copy;
if (!copy)
return -ENOMEM;
r = sd_bus_call_method(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&reply,
if (job) {
char *copy;
if (!copy)
return -ENOMEM;
r = sd_bus_call_method(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&reply,
if (job)
if (job) {
char *copy;
if (!copy)
return -ENOMEM;
if (!path)
return -ENOMEM;
r = sd_bus_call_method(
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Scope",
NULL,
NULL);
int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) {
return sd_bus_call_method(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
NULL,
const char *state;
if (!path)
return -ENOMEM;
r = sd_bus_get_property(
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
&error,
&reply,
return -EINVAL;
r = sd_bus_get_property(
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Job",
&error,
&reply,