logind-dbus.c revision c506027af881a9e4210845a7a8a6ec5910aa0f3b
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2011 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart 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
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
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/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#include <errno.h>
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#include <string.h>
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include <unistd.h>
4ad7f2761da661853dcc29d542efb4727abb1101Nick Owens#include <pwd.h>
7263f72499e962b3fd54cdb7c79d49ca72121edeZbigniew Jędrzejewski-Szmek
e4e73a632524c382139034d4271f53b6089ab4cbLennart Poettering#include "sd-id128.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "sd-messages.h"
e4e73a632524c382139034d4271f53b6089ab4cbLennart Poettering#include "strv.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "mkdir.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "path-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "special.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "sleep-config.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "fileio-label.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "label.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "utf8.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "unit-name.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "virt.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "audit.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "bus-util.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "bus-error.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "logind.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "bus-errors.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int property_get_idle_hint(
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering sd_bus *bus,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *path,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *interface,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *property,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering sd_bus_message *reply,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering void *userdata,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering sd_bus_error *error) {
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen Manager *m = userdata;
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen assert(bus);
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen assert(reply);
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen assert(m);
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, NULL) > 0);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering}
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersenstatic int property_get_idle_since_hint(
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen sd_bus *bus,
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering const char *path,
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering const char *interface,
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering const char *property,
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering sd_bus_message *reply,
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering void *userdata,
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering sd_bus_error *error) {
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering Manager *m = userdata;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering dual_timestamp t;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering assert(bus);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering assert(reply);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering assert(m);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering manager_get_idle_hint(m, &t);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering}
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poetteringstatic int property_get_inhibited(
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering sd_bus *bus,
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering const char *path,
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen const char *interface,
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen const char *property,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering sd_bus_message *reply,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering void *userdata,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering sd_bus_error *error) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Manager *m = userdata;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering InhibitWhat w;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(bus);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(reply);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(m);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering w = manager_inhibit_what(m, streq(property, "BlockInhibited") ? INHIBIT_BLOCK : INHIBIT_DELAY);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return sd_bus_message_append(reply, "s", inhibit_what_to_string(w));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int property_get_preparing(
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering sd_bus *bus,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *path,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *interface,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *property,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering sd_bus_message *reply,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering void *userdata,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering sd_bus_error *error) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Manager *m = userdata;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering bool b;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(bus);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(reply);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(m);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (streq(property, "PreparingForShutdown"))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering b = !!(m->action_what & INHIBIT_SHUTDOWN);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering else
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering b = !!(m->action_what & INHIBIT_SLEEP);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return sd_bus_message_append(reply, "b", b);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int method_get_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_free_ char *p = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Manager *m = userdata;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *name;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Session *session;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(bus);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(message);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(m);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_bus_message_read(message, "s", &name);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering session = hashmap_get(m->sessions, name);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!session)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering p = session_bus_path(session);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!p)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -ENOMEM;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return sd_bus_reply_method_return(message, "o", p);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int method_get_session_by_pid(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_free_ char *p = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Session *session = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Manager *m = userdata;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering pid_t pid;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(bus);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(message);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(m);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering assert_cc(sizeof(pid_t) == sizeof(uint32_t));
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering r = sd_bus_message_read(message, "u", &pid);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (r < 0)
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (pid == 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_bus_get_owner_pid(bus, sd_bus_message_get_sender(message), &pid);
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen if (r < 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return r;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = manager_get_session_by_pid(m, pid, &session);
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen if (r < 0)
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen return r;
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen if (!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);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt p = session_bus_path(session);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!p)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return -ENOMEM;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return sd_bus_reply_method_return(message, "o", p);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringstatic int method_get_user(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering _cleanup_free_ char *p = NULL;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering Manager *m = userdata;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering uint32_t uid;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering User *user;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering int r;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(bus);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(message);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(m);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = sd_bus_message_read(message, "u", &uid);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (r < 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return r;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt if (!user)
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "No user '%lu' known or logged in", (unsigned long) uid);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering p = user_bus_path(user);
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen if (!p)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return -ENOMEM;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return sd_bus_reply_method_return(message, "o", p);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
d23a27a964748967e1ad20e86de869a753af555bTom Gundersenstatic int method_get_user_by_pid(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering _cleanup_free_ char *p = NULL;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering Manager *m = userdata;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering User *user = NULL;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering pid_t pid;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering int r;
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(bus);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(message);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(m);
23432a1c249b9c513e438bffe9778a7ce2ee74feZbigniew Jędrzejewski-Szmek
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert_cc(sizeof(pid_t) == sizeof(uint32_t));
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = sd_bus_message_read(message, "u", &pid);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (pid == 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_bus_get_owner_pid(bus, sd_bus_message_get_sender(message), &pid);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = manager_get_user_by_pid(m, pid, &user);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!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);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering p = user_bus_path(user);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering if (!p)
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering return -ENOMEM;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering return sd_bus_reply_method_return(message, "o", p);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering}
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poetteringstatic int method_get_seat(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering _cleanup_free_ char *p = NULL;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering Manager *m = userdata;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering const char *name;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Seat *seat;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(bus);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(message);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(m);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_bus_message_read(message, "s", &name);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering seat = hashmap_get(m->seats, name);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!seat)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering p = seat_bus_path(seat);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!p)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -ENOMEM;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return sd_bus_reply_method_return(message, "o", p);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
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;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering Manager *m = userdata;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering Session *session;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering Iterator i;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering int r;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek assert(bus);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek assert(message);
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering assert(m);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek r = sd_bus_message_new_method_return(message, &reply);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (r < 0)
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek return r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_bus_message_open_container(reply, 'a', "(susso)");
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (r < 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return r;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering HASHMAP_FOREACH(session, m->sessions, i) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering _cleanup_free_ char *p = NULL;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering p = session_bus_path(session);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (!p)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return -ENOMEM;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek r = sd_bus_message_append(reply, "(susso)",
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering session->id,
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek (uint32_t) session->user->uid,
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek session->user->name,
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek session->seat ? session->seat->id : "",
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering p);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (r < 0)
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return r;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen }
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering r = sd_bus_message_close_container(reply);
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen if (r < 0)
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return r;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return sd_bus_send(bus, reply, NULL);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek}
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
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;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek Manager *m = userdata;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek User *user;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek Iterator i;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek int r;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen assert(bus);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen assert(message);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen assert(m);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen r = sd_bus_message_new_method_return(message, &reply);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (r < 0)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return r;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen r = sd_bus_message_open_container(reply, 'a', "(uso)");
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (r < 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return r;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek HASHMAP_FOREACH(user, m->users, i) {
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek _cleanup_free_ char *p = NULL;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek p = user_bus_path(user);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!p)
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek return -ENOMEM;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_bus_message_append(reply, "(uso)",
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering (uint32_t) user->uid,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering user->name,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering p);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (r < 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return r;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = sd_bus_message_close_container(reply);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (r < 0)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return sd_bus_send(bus, reply, NULL);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering}
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
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 Manager *m = userdata;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering Seat *seat;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering Iterator i;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering int r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering assert(bus);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering assert(message);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering assert(m);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = sd_bus_message_new_method_return(message, &reply);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (r < 0)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = sd_bus_message_open_container(reply, 'a', "(so)");
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (r < 0)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering HASHMAP_FOREACH(seat, m->seats, i) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering _cleanup_free_ char *p = NULL;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering p = seat_bus_path(seat);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (!p)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return -ENOMEM;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = sd_bus_message_append(reply, "(so)", seat->id, p);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (r < 0)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return r;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering }
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = sd_bus_message_close_container(reply);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (r < 0)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return r;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return sd_bus_send(bus, reply, NULL);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering}
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
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 Manager *m = userdata;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering Inhibitor *inhibitor;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering Iterator i;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering int r;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = sd_bus_message_new_method_return(message, &reply);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (r < 0)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return r;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = sd_bus_message_open_container(reply, 'a', "(ssssuu)");
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (r < 0)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return r;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering HASHMAP_FOREACH(inhibitor, m->inhibitors, i) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = sd_bus_message_append(reply, "(ssssuu)",
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering strempty(inhibit_what_to_string(inhibitor->what)),
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering strempty(inhibitor->who),
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering strempty(inhibitor->why),
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering strempty(inhibit_mode_to_string(inhibitor->mode)),
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering (uint32_t) inhibitor->uid,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering (uint32_t) inhibitor->pid);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = sd_bus_message_close_container(reply);
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering if (r < 0)
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering return r;
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering return sd_bus_send(bus, reply, NULL);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
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;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering uint32_t uid, leader, audit_id = 0;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering _cleanup_free_ char *id = NULL;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering Session *session = NULL;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering Manager *m = userdata;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering User *user = NULL;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering Seat *seat = NULL;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering int remote;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering uint32_t vtnr = 0;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering SessionType t;
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering SessionClass c;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering int r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(bus);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(message);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(m);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
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);
0f84a72e3c0f58d71cff2121e6df1611eaf9c9eaDavid Herrmann if (r < 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return r;
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (leader == 1)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID");
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (isempty(type))
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering t = _SESSION_TYPE_INVALID;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering else {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering t = session_type_from_string(type);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (t < 0)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session type %s", type);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering }
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (isempty(class))
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering c = _SESSION_CLASS_INVALID;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering else {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering c = session_class_from_string(class);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (c < 0)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session class %s", class);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering }
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (isempty(cseat))
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek seat = NULL;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek else {
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek seat = hashmap_get(m->seats, cseat);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (!seat)
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", seat);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek }
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (tty_is_vc(tty)) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek int v;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (!seat)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek seat = m->seat0;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek else if (seat != m->seat0)
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);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek v = vtnr_from_tty(tty);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (v <= 0)
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
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (!vtnr)
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen vtnr = (uint32_t) v;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen else if (vtnr != (uint32_t) v)
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified TTY and VT number do not match");
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering } else if (tty_is_console(tty)) {
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen if (!seat)
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen seat = m->seat0;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering else if (seat != m->seat0)
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
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (vtnr != 0)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but VT number is not 0");
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek }
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (seat) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (seat_has_vts(seat)) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (!vtnr || vtnr > 63)
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "VT number out of range");
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn } else {
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn if (vtnr != 0)
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat has no VTs but VT number not 0");
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn }
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn }
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek r = sd_bus_message_enter_container(message, 'a', "(sv)");
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (r < 0)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return r;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (t == _SESSION_TYPE_INVALID) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (!isempty(display))
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen t = SESSION_X11;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen else if (!isempty(tty))
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen t = SESSION_TTY;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen else
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen t = SESSION_UNSPECIFIED;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen }
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (c == _SESSION_CLASS_INVALID) {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (!isempty(display) || !isempty(tty))
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen c = SESSION_USER;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen else
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen c = SESSION_BACKGROUND;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (leader <= 0) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert_cc(sizeof(uint32_t) == sizeof(pid_t));
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = sd_bus_get_owner_pid(bus, sd_bus_message_get_sender(message), (pid_t*) &leader);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (r < 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return r;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek }
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek manager_get_session_by_pid(m, leader, &session);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (session) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *path = NULL;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek _cleanup_close_ int fifo_fd = -1;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
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
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek fifo_fd = session_create_fifo(session);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (fifo_fd < 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return fifo_fd;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek path = session_bus_path(session);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (!path)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return -ENOMEM;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return sd_bus_reply_method_return(
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek message, "soshusub",
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek session->id,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek path,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek session->user->runtime_path,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek fifo_fd,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek (uint32_t) session->user->uid,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek session->seat ? session->seat->id : "",
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek (uint32_t) session->vtnr,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek true);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek }
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen audit_session_from_pid(leader, &audit_id);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen if (audit_id > 0) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen /* Keep our session IDs and the audit session IDs in sync */
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen if (asprintf(&id, "%lu", (unsigned long) audit_id) < 0)
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return -ENOMEM;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
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 * ID */
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen if (hashmap_get(m->sessions, id)) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen log_warning("Existing logind session ID %s used by new audit session, ignoring", id);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen audit_id = 0;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen free(id);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen id = NULL;
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (!id) {
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen do {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen free(id);
2c1fb4f71206bf970d493294208c5d7597194856Lennart Poettering id = NULL;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (asprintf(&id, "c%lu", ++m->session_counter) < 0)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return -ENOMEM;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen } while (hashmap_get(m->sessions, id));
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen r = manager_add_user_by_uid(m, uid, &user);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (r < 0)
2c1fb4f71206bf970d493294208c5d7597194856Lennart Poettering goto fail;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen r = manager_add_session(m, id, &session);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (r < 0)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen goto fail;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen session_set_user(session, user);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen session->leader = leader;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen session->audit_id = audit_id;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen session->type = t;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen session->class = c;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen session->remote = remote;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering session->vtnr = vtnr;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (!isempty(tty)) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering session->tty = strdup(tty);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (!session->tty) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = -ENOMEM;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering goto fail;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (!isempty(display)) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering session->display = strdup(display);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (!session->display) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = -ENOMEM;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering goto fail;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (!isempty(remote_user)) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering session->remote_user = strdup(remote_user);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (!session->remote_user) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = -ENOMEM;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering goto fail;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (!isempty(remote_host)) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering session->remote_host = strdup(remote_host);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (!session->remote_host) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = -ENOMEM;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering goto fail;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (!isempty(service)) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering session->service = strdup(service);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (!session->service) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = -ENOMEM;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek goto fail;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (seat) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = seat_attach_session(seat, session);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering goto fail;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = session_start(session);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (r < 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering goto fail;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering session->create_message = sd_bus_message_ref(message);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
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().*/
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return 1;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poetteringfail:
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (session)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering session_add_to_gc_queue(session);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering if (user)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering user_add_to_gc_queue(user);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringstatic int method_release_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering Manager *m = userdata;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering Session *session;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering const char *name;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering int r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(bus);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek assert(message);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek assert(m);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek r = sd_bus_message_read(message, "s", &name);
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek if (r < 0)
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek return r;
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek session = hashmap_get(m->sessions, name);
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek if (!session)
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek
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 */
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering session_remove_fifo(session);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering session_save(session);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering user_save(session->user);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return sd_bus_reply_method_return(message, NULL);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekstatic int method_activate_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek Manager *m = userdata;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering Session *session;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering const char *name;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering int r;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(bus);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(message);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(m);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = sd_bus_message_read(message, "s", &name);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering session = hashmap_get(m->sessions, name);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (!session)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = session_activate(session);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return sd_bus_reply_method_return(message, NULL);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
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;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek Manager *m = userdata;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek Session *session;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek Seat *seat;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek int r;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek assert(bus);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek assert(message);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek assert(m);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
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
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek r = sd_bus_message_read(message, "ss", &session_name, &seat_name);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (r < 0)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return r;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek session = hashmap_get(m->sessions, session_name);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (!session)
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
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek seat = hashmap_get(m->seats, seat_name);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (!seat)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", seat_name);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (session->seat != seat)
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
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen r = session_activate(session);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (r < 0)
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return r;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return sd_bus_reply_method_return(message, NULL);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen}
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersenstatic int method_lock_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen Manager *m = userdata;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen Session *session;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen const char *name;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek int r;
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek assert(bus);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering assert(message);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering assert(m);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering r = sd_bus_message_read(message, "s", &name);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering if (r < 0)
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return r;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering session = hashmap_get(m->sessions, name);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering if (!session)
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek r = session_send_lock(session, streq(sd_bus_message_get_member(message), "LockSession"));
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek if (r < 0)
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek return r;
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek return sd_bus_reply_method_return(message, NULL);
1bf968f36393666f2c57953b1748e6219c027deeTom Gundersen}
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekstatic int method_lock_sessions(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering Manager *m = userdata;
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek int r;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek assert(bus);
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek assert(message);
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek assert(m);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek r = session_send_lock_all(m, streq(sd_bus_message_get_member(message), "LockSessions"));
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return r;
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return sd_bus_reply_method_return(message, NULL);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek}
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersenstatic int method_kill_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek const char *name, *swho;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek Manager *m = userdata;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek Session *session;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek int32_t signo;
1bf968f36393666f2c57953b1748e6219c027deeTom Gundersen KillWho who;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek int r;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek assert(bus);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen assert(message);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen assert(m);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen r = sd_bus_message_read(message, "ssi", &name, &swho, &signo);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen if (r < 0)
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return r;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen if (isempty(swho))
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek who = KILL_ALL;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek else {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek who = kill_who_from_string(swho);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen if (who < 0)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid kill parameter '%s'", swho);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek }
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
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
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek session = hashmap_get(m->sessions, name);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen if (!session)
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek r = session_kill(session, who, signo);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (r < 0)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return r;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return sd_bus_reply_method_return(message, NULL);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek}
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersenstatic int method_kill_user(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen Manager *m = userdata;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen uint32_t uid;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen int32_t signo;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen User *user;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen int r;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen assert(bus);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen assert(message);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen assert(m);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen r = sd_bus_message_read(message, "ui", &uid, &signo);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (r < 0)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return r;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (signo <= 0 || signo >= _NSIG)
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (!user)
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
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen r = user_kill(user, signo);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (r < 0)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return r;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return sd_bus_reply_method_return(message, NULL);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen}
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersenstatic int method_terminate_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen Manager *m = userdata;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen const char *name;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen Session *session;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen int r;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering assert(bus);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen assert(message);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen assert(m);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen r = sd_bus_message_read(message, "s", &name);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (r < 0)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return r;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen session = hashmap_get(m->sessions, name);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (!session)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek r = session_stop(session);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (r < 0)
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering return r;
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return sd_bus_reply_method_return(message, NULL);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic int method_terminate_user(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering Manager *m = userdata;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering uint32_t uid;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering User *user;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering int r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(bus);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(message);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(m);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = sd_bus_message_read(message, "u", &uid);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (r < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!user)
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
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = user_stop(user);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return sd_bus_reply_method_return(message, NULL);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic int method_terminate_seat(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
816b4547dac1b052239ad6ee3801c78c691e5cb4Daniel Mack Manager *m = userdata;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering const char *name;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering Seat *seat;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering int r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(bus);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering assert(message);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering assert(m);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = sd_bus_message_read(message, "s", &name);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (r < 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return r;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering seat = hashmap_get(m->seats, name);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (!seat)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = seat_stop_sessions(seat);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (r < 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return r;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return sd_bus_reply_method_return(message, NULL);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering}
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringstatic int method_set_user_linger(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering _cleanup_free_ char *cc = NULL;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering Manager *m = userdata;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering int b, r;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering struct passwd *pw;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering const char *path;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering uint32_t uid;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering int interactive;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering assert(bus);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering assert(message);
assert(m);
r = sd_bus_message_read(message, "ubb", &uid, &b, &interactive);
if (r < 0)
return r;
errno = 0;
pw = getpwuid(uid);
if (!pw)
return errno ? -errno : -ENOENT;
r = bus_verify_polkit_async(bus,
&m->polkit_registry,
message,
"org.freedesktop.login1.set-user-linger",
interactive,
error,
method_set_user_linger, m);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
mkdir_p_label("/var/lib/systemd", 0755);
r = mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0);
if (r < 0)
return r;
cc = cescape(pw->pw_name);
if (!cc)
return -ENOMEM;
path = strappenda("/var/lib/systemd/linger/", cc);
if (b) {
User *u;
r = touch(path);
if (r < 0)
return r;
if (manager_add_user_by_uid(m, uid, &u) >= 0)
user_start(u);
} else {
User *u;
r = unlink(path);
if (r < 0 && errno != ENOENT)
return -errno;
u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
if (u)
user_add_to_gc_queue(u);
}
return sd_bus_reply_method_return(message, NULL);
}
static int trigger_device(Manager *m, struct udev_device *d) {
struct udev_enumerate *e;
struct udev_list_entry *first, *item;
int r;
assert(m);
e = udev_enumerate_new(m->udev);
if (!e) {
r = -ENOMEM;
goto finish;
}
if (d) {
if (udev_enumerate_add_match_parent(e, d) < 0) {
r = -EIO;
goto finish;
}
}
if (udev_enumerate_scan_devices(e) < 0) {
r = -EIO;
goto finish;
}
first = udev_enumerate_get_list_entry(e);
udev_list_entry_foreach(item, first) {
_cleanup_free_ char *t = NULL;
const char *p;
p = udev_list_entry_get_name(item);
t = strappend(p, "/uevent");
if (!t) {
r = -ENOMEM;
goto finish;
}
write_string_file(t, "change");
}
r = 0;
finish:
if (e)
udev_enumerate_unref(e);
return r;
}
static int attach_device(Manager *m, const char *seat, const char *sysfs) {
_cleanup_free_ char *rule = NULL, *file = NULL;
const char *id_for_seat;
struct udev_device *d;
int r;
assert(m);
assert(seat);
assert(sysfs);
d = udev_device_new_from_syspath(m->udev, sysfs);
if (!d)
return -ENODEV;
if (!udev_device_has_tag(d, "seat")) {
r = -ENODEV;
goto finish;
}
id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
if (!id_for_seat) {
r = -ENODEV;
goto finish;
}
if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) {
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;
}
mkdir_p_label("/etc/udev/rules.d", 0755);
label_init("/etc");
r = write_string_file_atomic_label(file, rule);
if (r < 0)
goto finish;
r = trigger_device(m, d);
finish:
if (d)
udev_device_unref(d);
return r;
}
static int flush_devices(Manager *m) {
_cleanup_closedir_ DIR *d;
assert(m);
d = opendir("/etc/udev/rules.d");
if (!d) {
if (errno != ENOENT)
log_warning("Failed to open /etc/udev/rules.d: %m");
} else {
struct dirent *de;
while ((de = readdir(d))) {
if (!dirent_is_file(de))
continue;
if (!startswith(de->d_name, "72-seat-"))
continue;
if (!endswith(de->d_name, ".rules"))
continue;
if (unlinkat(dirfd(d), de->d_name, 0) < 0)
log_warning("Failed to unlink %s: %m", de->d_name);
}
}
return trigger_device(m, NULL);
}
static int method_attach_device(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *sysfs, *seat;
Manager *m = userdata;
int interactive, r;
assert(bus);
assert(message);
assert(m);
r = sd_bus_message_read(message, "ssb", &seat, &sysfs, &interactive);
if (r < 0)
return r;
if (!path_startswith(sysfs, "/sys"))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not in /sys", sysfs);
if (!seat_name_is_valid(seat))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat %s is not valid", seat);
r = bus_verify_polkit_async(bus,
&m->polkit_registry,
message,
"org.freedesktop.login1.attach-device",
interactive,
error,
method_attach_device, m);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = attach_device(m, seat, sysfs);
if (r < 0)
return r;
return sd_bus_reply_method_return(message, NULL);
}
static int method_flush_devices(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
int interactive, r;
assert(bus);
assert(message);
assert(m);
r = sd_bus_message_read(message, "b", &interactive);
if (r < 0)
return r;
r = bus_verify_polkit_async(bus,
&m->polkit_registry,
message,
"org.freedesktop.login1.flush-devices",
interactive,
error,
method_flush_devices, m);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = flush_devices(m);
if (r < 0)
return r;
return sd_bus_reply_method_return(message, NULL);
}
static int have_multiple_sessions(
Manager *m,
uid_t uid) {
Session *session;
Iterator i;
assert(m);
/* Check for other users' sessions. Greeter sessions do not
* count, and non-login sessions do not count either. */
HASHMAP_FOREACH(session, m->sessions, i)
if (session->class == SESSION_USER &&
!session->closing &&
session->user->uid != uid)
return true;
return false;
}
static int bus_manager_log_shutdown(
Manager *m,
InhibitWhat w,
const char *unit_name) {
const char *p, *q;
assert(m);
assert(unit_name);
if (w != INHIBIT_SHUTDOWN)
return 0;
if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) {
p = "MESSAGE=System is powering down.";
q = "SHUTDOWN=power-off";
} else if (streq(unit_name, SPECIAL_HALT_TARGET)) {
p = "MESSAGE=System is halting.";
q = "SHUTDOWN=halt";
} else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) {
p = "MESSAGE=System is rebooting.";
q = "SHUTDOWN=reboot";
} else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) {
p = "MESSAGE=System is rebooting with kexec.";
q = "SHUTDOWN=kexec";
} else {
p = "MESSAGE=System is shutting down.";
q = NULL;
}
return log_struct(LOG_NOTICE, MESSAGE_ID(SD_MESSAGE_SHUTDOWN),
p,
q, NULL);
}
static int execute_shutdown_or_sleep(
Manager *m,
InhibitWhat w,
const char *unit_name,
sd_bus_error *error) {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
const char *p;
char *c;
int r;
assert(m);
assert(w >= 0);
assert(w < _INHIBIT_WHAT_MAX);
assert(unit_name);
bus_manager_log_shutdown(m, w, unit_name);
r = sd_bus_call_method(
m->bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"StartUnit",
error,
&reply,
"ss", unit_name, "replace-irreversibly");
if (r < 0)
return r;
r = sd_bus_message_read(reply, "o", &p);
if (r < 0)
return r;
c = strdup(p);
if (!c)
return -ENOMEM;
m->action_unit = unit_name;
free(m->action_job);
m->action_job = c;
m->action_what = w;
return 0;
}
static int delay_shutdown_or_sleep(
Manager *m,
InhibitWhat w,
const char *unit_name) {
assert(m);
assert(w >= 0);
assert(w < _INHIBIT_WHAT_MAX);
assert(unit_name);
m->action_timestamp = now(CLOCK_MONOTONIC);
m->action_unit = unit_name;
m->action_what = w;
return 0;
}
static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
static const char * const signal_name[_INHIBIT_WHAT_MAX] = {
[INHIBIT_SHUTDOWN] = "PrepareForShutdown",
[INHIBIT_SLEEP] = "PrepareForSleep"
};
int active = _active;
assert(m);
assert(w >= 0);
assert(w < _INHIBIT_WHAT_MAX);
assert(signal_name[w]);
return sd_bus_emit_signal(m->bus,
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
signal_name[w],
"b",
active);
}
int bus_manager_shutdown_or_sleep_now_or_later(
Manager *m,
const char *unit_name,
InhibitWhat w,
sd_bus_error *error) {
bool delayed;
int r;
assert(m);
assert(unit_name);
assert(w >= 0);
assert(w <= _INHIBIT_WHAT_MAX);
assert(!m->action_job);
/* Tell everybody to prepare for shutdown/sleep */
send_prepare_for(m, w, true);
delayed =
m->inhibit_delay_max > 0 &&
manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
if (delayed)
/* Shutdown is delayed, keep in mind what we
* want to do, and start a timeout */
r = delay_shutdown_or_sleep(m, w, unit_name);
else
/* Shutdown is not delayed, execute it
* immediately */
r = execute_shutdown_or_sleep(m, w, unit_name, error);
return r;
}
static int method_do_shutdown_or_sleep(
Manager *m,
sd_bus_message *message,
const char *unit_name,
InhibitWhat w,
const char *action,
const char *action_multiple_sessions,
const char *action_ignore_inhibit,
const char *sleep_verb,
sd_bus_message_handler_t method,
sd_bus_error *error) {
bool multiple_sessions, blocked;
int interactive, r;
uid_t uid;
assert(m);
assert(message);
assert(unit_name);
assert(w >= 0);
assert(w <= _INHIBIT_WHAT_MAX);
assert(action);
assert(action_multiple_sessions);
assert(action_ignore_inhibit);
assert(method);
r = sd_bus_message_read(message, "b", &interactive);
if (r < 0)
return r;
/* Don't allow multiple jobs being executed at the same time */
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) {
r = can_sleep(sleep_verb);
if (r < 0)
return r;
if (r == 0)
return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Sleep verb not supported");
}
r = sd_bus_get_owner_uid(m->bus, sd_bus_message_get_sender(message), &uid);
if (r < 0)
return r;
r = have_multiple_sessions(m, uid);
if (r < 0)
return r;
multiple_sessions = r > 0;
blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
if (multiple_sessions) {
r = bus_verify_polkit_async(m->bus, &m->polkit_registry, message,
action_multiple_sessions, interactive, error, method, m);
if (r < 0)
return r;
}
if (blocked) {
r = bus_verify_polkit_async(m->bus, &m->polkit_registry, message,
action_ignore_inhibit, interactive, error, method, m);
if (r < 0)
return r;
}
if (!multiple_sessions && !blocked) {
r = bus_verify_polkit_async(m->bus, &m->polkit_registry, message,
action, interactive, error, method, m);
if (r < 0)
return r;
}
r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error);
if (r < 0)
return r;
return sd_bus_reply_method_return(message, NULL);
}
static int method_poweroff(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_do_shutdown_or_sleep(
m, message,
SPECIAL_POWEROFF_TARGET,
INHIBIT_SHUTDOWN,
"org.freedesktop.login1.power-off",
"org.freedesktop.login1.power-off-multiple-sessions",
"org.freedesktop.login1.power-off-ignore-inhibit",
NULL,
method_poweroff,
error);
}
static int method_reboot(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_do_shutdown_or_sleep(
m, message,
SPECIAL_REBOOT_TARGET,
INHIBIT_SHUTDOWN,
"org.freedesktop.login1.reboot",
"org.freedesktop.login1.reboot-multiple-sessions",
"org.freedesktop.login1.reboot-ignore-inhibit",
NULL,
method_reboot,
error);
}
static int method_suspend(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_do_shutdown_or_sleep(
m, message,
SPECIAL_SUSPEND_TARGET,
INHIBIT_SLEEP,
"org.freedesktop.login1.suspend",
"org.freedesktop.login1.suspend-multiple-sessions",
"org.freedesktop.login1.suspend-ignore-inhibit",
"suspend",
method_suspend,
error);
}
static int method_hibernate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_do_shutdown_or_sleep(
m, message,
SPECIAL_HIBERNATE_TARGET,
INHIBIT_SLEEP,
"org.freedesktop.login1.hibernate",
"org.freedesktop.login1.hibernate-multiple-sessions",
"org.freedesktop.login1.hibernate-ignore-inhibit",
"hibernate",
method_hibernate,
error);
}
static int method_hybrid_sleep(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_do_shutdown_or_sleep(
m, message,
SPECIAL_HYBRID_SLEEP_TARGET,
INHIBIT_SLEEP,
"org.freedesktop.login1.hibernate",
"org.freedesktop.login1.hibernate-multiple-sessions",
"org.freedesktop.login1.hibernate-ignore-inhibit",
"hybrid-sleep",
method_hybrid_sleep,
error);
}
static int method_can_shutdown_or_sleep(
Manager *m,
sd_bus_message *message,
InhibitWhat w,
const char *action,
const char *action_multiple_sessions,
const char *action_ignore_inhibit,
const char *sleep_verb,
sd_bus_error *error) {
bool multiple_sessions, challenge, blocked;
const char *result = NULL;
uid_t uid;
int r;
assert(m);
assert(message);
assert(w >= 0);
assert(w <= _INHIBIT_WHAT_MAX);
assert(action);
assert(action_multiple_sessions);
assert(action_ignore_inhibit);
if (sleep_verb) {
r = can_sleep(sleep_verb);
if (r < 0)
return r;
if (r == 0)
return sd_bus_reply_method_return(message, "s", "na");
}
r = sd_bus_get_owner_uid(m->bus, sd_bus_message_get_sender(message), &uid);
if (r < 0)
return r;
r = have_multiple_sessions(m, uid);
if (r < 0)
return r;
multiple_sessions = r > 0;
blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
if (multiple_sessions) {
r = bus_verify_polkit(m->bus, message, action_multiple_sessions, false, &challenge, error);
if (r < 0)
return r;
if (r > 0)
result = "yes";
else if (challenge)
result = "challenge";
else
result = "no";
}
if (blocked) {
r = bus_verify_polkit(m->bus, message, action_ignore_inhibit, false, &challenge, error);
if (r < 0)
return r;
if (r > 0 && !result)
result = "yes";
else if (challenge && (!result || streq(result, "yes")))
result = "challenge";
else
result = "no";
}
if (!multiple_sessions && !blocked) {
/* If neither inhibit nor multiple sessions
* apply then just check the normal policy */
r = bus_verify_polkit(m->bus, message, action, false, &challenge, error);
if (r < 0)
return r;
if (r > 0)
result = "yes";
else if (challenge)
result = "challenge";
else
result = "no";
}
return sd_bus_reply_method_return(message, "s", result);
}
static int method_can_poweroff(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_can_shutdown_or_sleep(
m, message,
INHIBIT_SHUTDOWN,
"org.freedesktop.login1.power-off",
"org.freedesktop.login1.power-off-multiple-sessions",
"org.freedesktop.login1.power-off-ignore-inhibit",
NULL,
error);
}
static int method_can_reboot(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_can_shutdown_or_sleep(
m, message,
INHIBIT_SHUTDOWN,
"org.freedesktop.login1.reboot",
"org.freedesktop.login1.reboot-multiple-sessions",
"org.freedesktop.login1.reboot-ignore-inhibit",
NULL,
error);
}
static int method_can_suspend(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_can_shutdown_or_sleep(
m, message,
INHIBIT_SLEEP,
"org.freedesktop.login1.suspend",
"org.freedesktop.login1.suspend-multiple-sessions",
"org.freedesktop.login1.suspend-ignore-inhibit",
"suspend",
error);
}
static int method_can_hibernate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_can_shutdown_or_sleep(
m, message,
INHIBIT_SLEEP,
"org.freedesktop.login1.hibernate",
"org.freedesktop.login1.hibernate-multiple-sessions",
"org.freedesktop.login1.hibernate-ignore-inhibit",
"hibernate",
error);
}
static int method_can_hybrid_sleep(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_can_shutdown_or_sleep(
m, message,
INHIBIT_SLEEP,
"org.freedesktop.login1.hibernate",
"org.freedesktop.login1.hibernate-multiple-sessions",
"org.freedesktop.login1.hibernate-ignore-inhibit",
"hybrid-sleep",
error);
}
static int method_inhibit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *who, *why, *what, *mode;
_cleanup_free_ char *id = NULL;
_cleanup_close_ int fifo_fd = -1;
Manager *m = userdata;
Inhibitor *i = NULL;
InhibitMode mm;
InhibitWhat w;
pid_t pid;
uid_t uid;
int r;
assert(bus);
assert(message);
assert(m);
r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode);
if (r < 0)
return r;
w = inhibit_what_from_string(what);
if (w <= 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid what specification %s", what);
mm = inhibit_mode_from_string(mode);
if (mm < 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid mode specification %s", mode);
/* Delay is only supported for shutdown/sleep */
if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP)))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Delay inhibitors only supported for shutdown and sleep");
/* Don't allow taking delay locks while we are already
* executing the operation. We shouldn't create the impression
* that the lock was successful if the machine is about to go
* down/suspend any moment. */
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");
r = bus_verify_polkit_async(bus, &m->polkit_registry, message,
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") :
w == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" :
w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" :
w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" :
w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
"org.freedesktop.login1.inhibit-handle-lid-switch",
false, error, method_inhibit, m);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = sd_bus_get_owner_uid(m->bus, sd_bus_message_get_sender(message), &uid);
if (r < 0)
return r;
r = sd_bus_get_owner_pid(m->bus, sd_bus_message_get_sender(message), &pid);
if (r < 0)
return r;
do {
free(id);
id = NULL;
if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0)
return -ENOMEM;
} while (hashmap_get(m->inhibitors, id));
r = manager_add_inhibitor(m, id, &i);
if (r < 0)
return r;
i->what = w;
i->mode = mm;
i->pid = pid;
i->uid = uid;
i->why = strdup(why);
i->who = strdup(who);
if (!i->why || !i->who) {
r = -ENOMEM;
goto fail;
}
fifo_fd = inhibitor_create_fifo(i);
if (fifo_fd < 0) {
r = fifo_fd;
goto fail;
}
inhibitor_start(i);
return sd_bus_reply_method_return(message, "h", fifo_fd);
fail:
if (i)
inhibitor_free(i);
return r;
}
const sd_bus_vtable manager_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), 0),
SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), 0),
SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), 0),
SD_BUS_PROPERTY("KillUserProcesses", "b", NULL, offsetof(Manager, kill_user_processes), 0),
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),
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), 0),
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),
SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), 0),
SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), 0),
SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0),
SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0),
SD_BUS_METHOD("GetSession", "s", "o", method_get_session, 0),
SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid, 0),
SD_BUS_METHOD("GetUser", "u", "o", method_get_user, 0),
SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid, 0),
SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat, 0),
SD_BUS_METHOD("ListSessions", NULL, "a(susso)", method_list_sessions, 0),
SD_BUS_METHOD("ListUsers", NULL, "a(uso)", method_list_users, 0),
SD_BUS_METHOD("ListSeats", NULL, "a(so)", method_list_seats, 0),
SD_BUS_METHOD("ListInhibitors", NULL, "a(ssssuu)", method_list_inhibitors, 0),
SD_BUS_METHOD("CreateSession", "uussssussbssa(sv)", "soshusub", method_create_session, 0),
SD_BUS_METHOD("ReleaseSession", "s", NULL, method_release_session, 0),
SD_BUS_METHOD("ActivateSession", "s", NULL, method_activate_session, 0),
SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL, method_activate_session_on_seat, 0),
SD_BUS_METHOD("LockSession", "s", NULL, method_lock_session, 0),
SD_BUS_METHOD("UnlockSession", "s", NULL, method_lock_session, 0),
SD_BUS_METHOD("LockSessions", NULL, NULL, method_lock_sessions, 0),
SD_BUS_METHOD("UnlockSessions", NULL, NULL, method_lock_sessions, 0),
SD_BUS_METHOD("KillSession", "ssi", NULL, method_kill_session, 0),
SD_BUS_METHOD("KillUser", "ui", NULL, method_kill_user, 0),
SD_BUS_METHOD("TerminateSession", "s", NULL, method_terminate_session, 0),
SD_BUS_METHOD("TerminateUser", "u", NULL, method_terminate_user, 0),
SD_BUS_METHOD("TerminateSeat", "s", NULL, method_terminate_seat, 0),
SD_BUS_METHOD("SetUserLinger", "ubb", NULL, method_set_user_linger, 0),
SD_BUS_METHOD("AttachDevice", "ssb", NULL, method_attach_device, 0),
SD_BUS_METHOD("FlushDevices", "b", NULL, method_flush_devices, 0),
SD_BUS_METHOD("PowerOff", "b", NULL, method_poweroff, 0),
SD_BUS_METHOD("Reboot", "b", NULL, method_reboot, 0),
SD_BUS_METHOD("Suspend", "b", NULL, method_suspend, 0),
SD_BUS_METHOD("Hibernate", "b", NULL, method_hibernate, 0),
SD_BUS_METHOD("HybridSleep", "b", NULL, method_hybrid_sleep, 0),
SD_BUS_METHOD("CanPowerOff", NULL, "s", method_can_poweroff, 0),
SD_BUS_METHOD("CanReboot", NULL, "s", method_can_reboot, 0),
SD_BUS_METHOD("CanSuspend", NULL, "s", method_can_suspend, 0),
SD_BUS_METHOD("CanHibernate", NULL, "s", method_can_hibernate, 0),
SD_BUS_METHOD("CanHybridSleep", NULL, "s", method_can_hybrid_sleep, 0),
SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit, 0),
SD_BUS_SIGNAL("SessionNew", "so", 0),
SD_BUS_SIGNAL("SessionRemoved", "so", 0),
SD_BUS_SIGNAL("UserNew", "uo", 0),
SD_BUS_SIGNAL("UserRemoved", "uo", 0),
SD_BUS_SIGNAL("SeatNew", "so", 0),
SD_BUS_SIGNAL("SeatRemoved", "so", 0),
SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
SD_BUS_VTABLE_END
};
int match_job_removed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *path, *result, *unit;
Manager *m = userdata;
Session *session;
uint32_t id;
User *user;
int r;
assert(bus);
assert(message);
assert(m);
r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
if (r < 0) {
bus_log_parse_error(r);
return r;
}
if (m->action_job && streq(m->action_job, path)) {
log_info("Operation finished.");
/* Tell people that they now may take a lock again */
send_prepare_for(m, m->action_what, false);
free(m->action_job);
m->action_job = NULL;
m->action_unit = NULL;
m->action_what = 0;
return 0;
}
session = hashmap_get(m->session_units, unit);
if (session) {
if (streq_ptr(path, session->scope_job)) {
free(session->scope_job);
session->scope_job = NULL;
}
if (session->started) {
if (streq(result, "done"))
session_send_create_reply(session, NULL);
else {
_cleanup_bus_error_free_ sd_bus_error e = SD_BUS_ERROR_NULL;
sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
session_send_create_reply(session, &e);
}
} else
session_save(session);
session_add_to_gc_queue(session);
}
user = hashmap_get(m->user_units, unit);
if (user) {
if (streq_ptr(path, user->service_job)) {
free(user->service_job);
user->service_job = NULL;
}
if (streq_ptr(path, user->slice_job)) {
free(user->slice_job);
user->slice_job = NULL;
}
user_save(user);
user_add_to_gc_queue(user);
}
return 0;
}
int match_unit_removed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *path, *unit;
Manager *m = userdata;
Session *session;
User *user;
int r;
assert(bus);
assert(message);
assert(m);
r = sd_bus_message_read(message, "so", &unit, &path);
if (r < 0) {
bus_log_parse_error(r);
return r;
}
session = hashmap_get(m->session_units, unit);
if (session)
session_add_to_gc_queue(session);
user = hashmap_get(m->user_units, unit);
if (user)
user_add_to_gc_queue(user);
return 0;
}
int match_properties_changed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_free_ char *unit = NULL;
Manager *m = userdata;
const char *path;
Session *session;
User *user;
int r;
assert(bus);
assert(message);
assert(m);
path = sd_bus_message_get_path(message);
if (!path)
return 0;
r = unit_name_from_dbus_path(path, &unit);
if (r < 0)
return r;
session = hashmap_get(m->session_units, unit);
if (session)
session_add_to_gc_queue(session);
user = hashmap_get(m->user_units, unit);
if (user)
user_add_to_gc_queue(user);
return 0;
}
int match_reloading(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
Session *session;
Iterator i;
int b, r;
assert(bus);
r = sd_bus_message_read(message, "b", &b);
if (r < 0) {
bus_log_parse_error(r);
return r;
}
if (b)
return 0;
/* systemd finished reloading, let's recheck all our sessions */
log_debug("System manager has been reloaded, rechecking sessions...");
HASHMAP_FOREACH(session, m->sessions, i)
session_add_to_gc_queue(session);
return 0;
}
int match_name_owner_changed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name, *old, *new;
Manager *m = userdata;
Session *session;
Iterator i;
int r;
char *key;
r = sd_bus_message_read(message, "sss", &name, &old, &new);
if (r < 0) {
bus_log_parse_error(r);
return r;
}
if (isempty(old) || !isempty(new))
return 0;
key = set_remove(m->busnames, (char*) old);
if (!key)
return 0;
/* Drop all controllers owned by this name */
free(key);
HASHMAP_FOREACH(session, m->sessions, i)
if (session_is_controller(session, old))
session_drop_controller(session);
return 0;
}
int manager_send_changed(Manager *manager, const char *property, ...) {
char **l;
assert(manager);
l = strv_from_stdarg_alloca(property);
return sd_bus_emit_properties_changed_strv(
manager->bus,
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
l);
}
int manager_dispatch_delayed(Manager *manager) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
Inhibitor *offending = NULL;
int r;
assert(manager);
if (manager->action_what == 0 || manager->action_job)
return 0;
/* Continue delay? */
if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
_cleanup_free_ char *comm = NULL, *u = NULL;
get_process_comm(offending->pid, &comm);
u = uid_to_name(offending->uid);
if (manager->action_timestamp + manager->inhibit_delay_max > now(CLOCK_MONOTONIC))
return 0;
log_info("Delay lock is active (UID %lu/%s, PID %lu/%s) but inhibitor timeout is reached.",
(unsigned long) offending->uid, strna(u),
(unsigned long) offending->pid, strna(comm));
}
/* Actually do the operation */
r = execute_shutdown_or_sleep(manager, manager->action_what, manager->action_unit, &error);
if (r < 0) {
log_warning("Failed to send delayed message: %s", bus_error_message(&error, r));
manager->action_unit = NULL;
manager->action_what = 0;
return r;
}
return 1;
}
int manager_start_scope(
Manager *manager,
const char *scope,
pid_t pid,
const char *slice,
const char *description,
const char *after,
const char *kill_mode,
sd_bus_error *error,
char **job) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
int r;
assert(manager);
assert(scope);
assert(pid > 1);
r = sd_bus_message_new_method_call(
manager->bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"StartTransientUnit",
&m);
if (r < 0)
return r;
r = sd_bus_message_append(m, "ss", strempty(scope), "fail");
if (r < 0)
return r;
r = sd_bus_message_open_container(m, 'a', "(sv)");
if (r < 0)
return r;
if (!isempty(slice)) {
r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
if (r < 0)
return r;
}
if (!isempty(description)) {
r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
if (r < 0)
return r;
}
if (!isempty(description)) {
r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after);
if (r < 0)
return r;
}
if (!isempty(kill_mode)) {
r = sd_bus_message_append(m, "(sv)", "KillMode", "s", kill_mode);
if (r < 0)
return r;
}
/* cgroup empty notification is not available in containers
* currently. To make this less problematic, let's shorten the
* stop timeout for sessions, so that we don't wait
* forever. */
r = sd_bus_message_append(m, "(sv)", "TimeoutStopUSec", "t", 500 * USEC_PER_MSEC);
if (r < 0)
return r;
/* Make sure that the session shells are terminated with
* SIGHUP since bash and friends tend to ignore SIGTERM */
r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true);
if (r < 0)
return r;
r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid);
if (r < 0)
return r;
r = sd_bus_message_close_container(m);
if (r < 0)
return r;
r = sd_bus_message_append(m, "a(sa(sv))", 0);
if (r < 0)
return r;
r = sd_bus_call(manager->bus, m, 0, error, &reply);
if (r < 0)
return r;
if (job) {
const char *j;
char *copy;
r = sd_bus_message_read(reply, "o", &j);
if (r < 0)
return r;
copy = strdup(j);
if (!copy)
return -ENOMEM;
*job = copy;
}
return 1;
}
int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
int r;
assert(manager);
assert(unit);
r = sd_bus_call_method(
manager->bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"StartUnit",
error,
&reply,
"ss", unit, "fail");
if (r < 0)
return r;
if (job) {
const char *j;
char *copy;
r = sd_bus_message_read(reply, "o", &j);
if (r < 0)
return r;
copy = strdup(j);
if (!copy)
return -ENOMEM;
*job = copy;
}
return 1;
}
int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
int r;
assert(manager);
assert(unit);
r = sd_bus_call_method(
manager->bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"StopUnit",
error,
&reply,
"ss", unit, "fail");
if (r < 0) {
if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
if (job)
*job = NULL;
sd_bus_error_free(error);
return 0;
}
return r;
}
if (job) {
const char *j;
char *copy;
r = sd_bus_message_read(reply, "o", &j);
if (r < 0)
return r;
copy = strdup(j);
if (!copy)
return -ENOMEM;
*job = copy;
}
return 1;
}
int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) {
assert(manager);
assert(unit);
return sd_bus_call_method(
manager->bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"KillUnit",
error,
NULL,
"ssi", unit, who == KILL_LEADER ? "main" : "all", signo);
}
int manager_unit_is_active(Manager *manager, const char *unit) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
_cleanup_free_ char *path = NULL;
const char *state;
int r;
assert(manager);
assert(unit);
path = unit_dbus_path_from_name(unit);
if (!path)
return -ENOMEM;
r = sd_bus_get_property(
manager->bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
"ActiveState",
&error,
&reply,
"s");
if (r < 0) {
/* systemd might have droppped off momentarily, let's
* not make this an error */
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
return true;
/* If the unit is already unloaded then it's not
* active */
if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
return false;
return r;
}
r = sd_bus_message_read(reply, "s", &state);
if (r < 0)
return -EINVAL;
return !streq(state, "inactive") && !streq(state, "failed");
}
int manager_job_is_active(Manager *manager, const char *path) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
int r;
assert(manager);
assert(path);
r = sd_bus_get_property(
manager->bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Job",
"State",
&error,
&reply,
"s");
if (r < 0) {
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
return true;
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
return false;
return r;
}
/* We don't actually care about the state really. The fact
* that we could read the job state is enough for us */
return true;
}