logind-dbus.c revision c506027af881a9e4210845a7a8a6ec5910aa0f3b
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2011 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, NULL) > 0);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering w = manager_inhibit_what(m, streq(property, "BlockInhibited") ? INHIBIT_BLOCK : INHIBIT_DELAY);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return sd_bus_message_append(reply, "s", inhibit_what_to_string(w));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (streq(property, "PreparingForShutdown"))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering b = !!(m->action_what & INHIBIT_SHUTDOWN);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return sd_bus_message_append(reply, "b", b);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int method_get_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_bus_message_read(message, "s", &name);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering session = hashmap_get(m->sessions, name);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return sd_bus_reply_method_return(message, "o", p);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int method_get_session_by_pid(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering assert_cc(sizeof(pid_t) == sizeof(uint32_t));
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering r = sd_bus_message_read(message, "u", &pid);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_bus_get_owner_pid(bus, sd_bus_message_get_sender(message), &pid);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = manager_get_session_by_pid(m, pid, &session);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, "PID %lu does not belong to any known session", (unsigned long) pid);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return sd_bus_reply_method_return(message, "o", p);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringstatic int method_get_user(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = sd_bus_message_read(message, "u", &uid);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "No user '%lu' known or logged in", (unsigned long) uid);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return sd_bus_reply_method_return(message, "o", p);
d23a27a964748967e1ad20e86de869a753af555bTom Gundersenstatic int method_get_user_by_pid(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert_cc(sizeof(pid_t) == sizeof(uint32_t));
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = sd_bus_message_read(message, "u", &pid);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_bus_get_owner_pid(bus, sd_bus_message_get_sender(message), &pid);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = manager_get_user_by_pid(m, pid, &user);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, "PID %lu does not belong to any known or logged in user", (unsigned long) pid);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering return sd_bus_reply_method_return(message, "o", p);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poetteringstatic int method_get_seat(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_bus_message_read(message, "s", &name);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return sd_bus_reply_method_return(message, "o", p);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmekstatic int method_list_sessions(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek r = sd_bus_message_new_method_return(message, &reply);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_bus_message_open_container(reply, 'a', "(susso)");
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering HASHMAP_FOREACH(session, m->sessions, i) {
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek r = sd_bus_message_append(reply, "(susso)",
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek session->seat ? session->seat->id : "",
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering r = sd_bus_message_close_container(reply);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return sd_bus_send(bus, reply, NULL);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekstatic int method_list_users(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen r = sd_bus_message_new_method_return(message, &reply);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen r = sd_bus_message_open_container(reply, 'a', "(uso)");
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek HASHMAP_FOREACH(user, m->users, i) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_bus_message_append(reply, "(uso)",
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = sd_bus_message_close_container(reply);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringstatic int method_list_seats(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = sd_bus_message_new_method_return(message, &reply);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = sd_bus_message_open_container(reply, 'a', "(so)");
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = sd_bus_message_append(reply, "(so)", seat->id, p);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = sd_bus_message_close_container(reply);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poetteringstatic int method_list_inhibitors(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = sd_bus_message_new_method_return(message, &reply);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = sd_bus_message_open_container(reply, 'a', "(ssssuu)");
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering HASHMAP_FOREACH(inhibitor, m->inhibitors, i) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = sd_bus_message_append(reply, "(ssssuu)",
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering strempty(inhibit_what_to_string(inhibitor->what)),
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering strempty(inhibit_mode_to_string(inhibitor->mode)),
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = sd_bus_message_close_container(reply);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poetteringstatic int method_create_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek r = sd_bus_message_read(message, "uussssussbss", &uid, &leader, &service, &type, &class, &cseat, &vtnr, &tty, &display, &remote, &remote_user, &remote_host);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID");
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session type %s", type);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session class %s", class);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek seat = hashmap_get(m->seats, cseat);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", seat);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew 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);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot determine VT number from virtual console TTY %s", tty);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified TTY and VT number do not match");
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but seat is not seat0");
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but VT number is not 0");
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "VT number out of range");
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat has no VTs but VT number not 0");
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek r = sd_bus_message_enter_container(message, 'a', "(sv)");
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (t == _SESSION_TYPE_INVALID) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert_cc(sizeof(uint32_t) == sizeof(pid_t));
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = sd_bus_get_owner_pid(bus, sd_bus_message_get_sender(message), (pid_t*) &leader);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek manager_get_session_by_pid(m, leader, &session);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *path = NULL;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek _cleanup_close_ int fifo_fd = -1;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek /* Session already exists, client is probably
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek * something like "su" which changes uid but is still
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek * the same session */
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek fifo_fd = session_create_fifo(session);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek path = session_bus_path(session);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return sd_bus_reply_method_return(
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek session->seat ? session->seat->id : "",
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen /* Keep our session IDs and the audit session IDs in sync */
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen if (asprintf(&id, "%lu", (unsigned long) audit_id) < 0)
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen /* Wut? There's already a session by this name and we
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen * didn't find it above? Weird, then let's not trust
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen * the audit data and let's better register a new
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen log_warning("Existing logind session ID %s used by new audit session, ignoring", id);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (asprintf(&id, "c%lu", ++m->session_counter) < 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering session->remote_user = strdup(remote_user);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering session->remote_host = strdup(remote_host);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering session->create_message = sd_bus_message_ref(message);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering /* Now, let's wait until the slice unit and stuff got
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering * created. We send the reply back from
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering * session_send_create_reply().*/
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringstatic int method_release_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek r = sd_bus_message_read(message, "s", &name);
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek session = hashmap_get(m->sessions, name);
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* We use the FIFO to detect stray sessions where the process
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering invoking PAM dies abnormally. We need to make sure that
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering that process is not killed if at the clean end of the
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering session it closes the FIFO. Hence, with this call
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering explicitly turn off the FIFO logic, so that the PAM code
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering can finish clean up on its own */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return sd_bus_reply_method_return(message, NULL);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekstatic int method_activate_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = sd_bus_message_read(message, "s", &name);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering session = hashmap_get(m->sessions, name);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return sd_bus_reply_method_return(message, NULL);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmekstatic int method_activate_session_on_seat(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek const char *session_name, *seat_name;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek /* Same as ActivateSession() but refuses to work if
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek * the seat doesn't match */
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek r = sd_bus_message_read(message, "ss", &session_name, &seat_name);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek session = hashmap_get(m->sessions, session_name);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", session_name);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek seat = hashmap_get(m->seats, seat_name);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", seat_name);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", session_name, seat_name);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return sd_bus_reply_method_return(message, NULL);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersenstatic int method_lock_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering r = sd_bus_message_read(message, "s", &name);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering session = hashmap_get(m->sessions, name);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek r = session_send_lock(session, streq(sd_bus_message_get_member(message), "LockSession"));
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek return sd_bus_reply_method_return(message, NULL);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekstatic int method_lock_sessions(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek r = session_send_lock_all(m, streq(sd_bus_message_get_member(message), "LockSessions"));
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return sd_bus_reply_method_return(message, NULL);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersenstatic int method_kill_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen r = sd_bus_message_read(message, "ssi", &name, &swho, &signo);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek who = kill_who_from_string(swho);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid kill parameter '%s'", swho);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (signo <= 0 || signo >= _NSIG)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek session = hashmap_get(m->sessions, name);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek r = session_kill(session, who, signo);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return sd_bus_reply_method_return(message, NULL);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersenstatic int method_kill_user(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen r = sd_bus_message_read(message, "ui", &uid, &signo);
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "No user '%lu' known or logged in", (unsigned long) uid);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return sd_bus_reply_method_return(message, NULL);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersenstatic int method_terminate_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return sd_bus_reply_method_return(message, NULL);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic int method_terminate_user(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = sd_bus_message_read(message, "u", &uid);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "No user '%lu' known or logged in", (unsigned long) uid);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return sd_bus_reply_method_return(message, NULL);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic int method_terminate_seat(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = sd_bus_message_read(message, "s", &name);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return sd_bus_reply_method_return(message, NULL);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringstatic int method_set_user_linger(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
assert(m);
errno = 0;
if (!pw)
&m->polkit_registry,
"org.freedesktop.login1.set-user-linger",
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;
struct udev_enumerate *e;
assert(m);
r = -ENOMEM;
goto finish;
if (udev_enumerate_add_match_parent(e, d) < 0) {
r = -EIO;
goto finish;
if (udev_enumerate_scan_devices(e) < 0) {
r = -EIO;
goto finish;
r = -ENOMEM;
goto finish;
const char *id_for_seat;
struct udev_device *d;
assert(m);
return -ENODEV;
r = -ENODEV;
goto finish;
if (!id_for_seat) {
r = -ENODEV;
goto finish;
r = -ENOMEM;
goto finish;
if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) {
r = -ENOMEM;
goto finish;
goto finish;
r = trigger_device(m, d);
assert(m);
static int method_attach_device(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
int interactive, r;
assert(m);
&m->polkit_registry,
method_attach_device, m);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
static int method_flush_devices(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
int interactive, r;
assert(m);
&m->polkit_registry,
method_flush_devices, m);
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;
q, NULL);
static int execute_shutdown_or_sleep(
Manager *m,
InhibitWhat w,
const char *unit_name,
assert(m);
assert(w >= 0);
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;
static int delay_shutdown_or_sleep(
Manager *m,
InhibitWhat w,
const char *unit_name) {
assert(m);
assert(w >= 0);
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 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) {
multiple_sessions = r > 0;
if (multiple_sessions) {
if (blocked) {
static int method_poweroff(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
return method_do_shutdown_or_sleep(
m, message,
NULL,
error);
static int method_reboot(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
return method_do_shutdown_or_sleep(
m, message,
NULL,
error);
static int method_suspend(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *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 method_hibernate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *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_hybrid_sleep(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *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) {
else if (challenge)
if (blocked) {
if (r > 0 && !result)
else if (challenge)
static int method_can_poweroff(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
return method_can_shutdown_or_sleep(
m, message,
NULL,
error);
static int method_can_reboot(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
return method_can_shutdown_or_sleep(
m, message,
NULL,
error);
static int method_can_suspend(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *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);
static int method_can_hibernate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *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 method_can_hybrid_sleep(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *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 method_inhibit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
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") :
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_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("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), 0),
SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), 0),
SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), 0),
SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), 0),
assert(m);
m->action_what = 0;
if (session) {
sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
if (user) {
assert(m);
if (session)
if (user)
int match_properties_changed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *path;
assert(m);
if (!path)
if (session)
if (user)
Iterator i;
int match_name_owner_changed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
Iterator i;
char *key;
if (!key)
"/org/freedesktop/login1",
if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
int manager_start_scope(
const char *scope,
const char *slice,
const char *description,
const char *after,
const char *kill_mode,
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;
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,