2011-05-26 02:21:16 +02:00
|
|
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
|
|
|
|
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2011 Lennart Poettering
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
2012-04-12 00:20:58 +02:00
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
2011-05-26 02:21:16 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2012-04-12 00:20:58 +02:00
|
|
|
Lesser General Public License for more details.
|
2011-05-26 02:21:16 +02:00
|
|
|
|
2012-04-12 00:20:58 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2011-05-26 02:21:16 +02:00
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
2011-06-17 15:59:18 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2011-06-24 18:50:50 +02:00
|
|
|
#include <unistd.h>
|
2011-06-28 03:52:22 +02:00
|
|
|
#include <pwd.h>
|
2011-06-17 15:59:18 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
#include "sd-id128.h"
|
|
|
|
#include "sd-messages.h"
|
2011-06-24 18:50:50 +02:00
|
|
|
#include "strv.h"
|
2012-04-10 21:54:31 +02:00
|
|
|
#include "mkdir.h"
|
2012-05-07 21:36:12 +02:00
|
|
|
#include "path-util.h"
|
2011-07-15 01:52:52 +02:00
|
|
|
#include "special.h"
|
2013-05-04 18:31:28 +02:00
|
|
|
#include "sleep-config.h"
|
2013-02-14 12:26:13 +01:00
|
|
|
#include "fileio-label.h"
|
|
|
|
#include "label.h"
|
2013-06-20 03:45:08 +02:00
|
|
|
#include "utf8.h"
|
|
|
|
#include "unit-name.h"
|
2013-07-02 01:46:30 +02:00
|
|
|
#include "virt.h"
|
2013-11-05 01:10:21 +01:00
|
|
|
#include "audit.h"
|
|
|
|
#include "bus-util.h"
|
|
|
|
#include "bus-error.h"
|
|
|
|
#include "logind.h"
|
2011-05-26 02:21:16 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int property_get_idle_hint(
|
|
|
|
sd_bus *bus,
|
|
|
|
const char *path,
|
|
|
|
const char *interface,
|
|
|
|
const char *property,
|
|
|
|
sd_bus_message *reply,
|
|
|
|
sd_bus_error *error,
|
|
|
|
void *userdata) {
|
2011-06-17 15:59:18 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
Manager *m = userdata;
|
2011-06-17 15:59:18 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(bus);
|
|
|
|
assert(reply);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, NULL) > 0);
|
2011-06-17 15:59:18 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int property_get_idle_since_hint(
|
|
|
|
sd_bus *bus,
|
|
|
|
const char *path,
|
|
|
|
const char *interface,
|
|
|
|
const char *property,
|
|
|
|
sd_bus_message *reply,
|
|
|
|
sd_bus_error *error,
|
|
|
|
void *userdata) {
|
|
|
|
|
|
|
|
Manager *m = userdata;
|
2011-06-17 15:59:18 +02:00
|
|
|
dual_timestamp t;
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(bus);
|
|
|
|
assert(reply);
|
2011-06-17 15:59:18 +02:00
|
|
|
assert(m);
|
|
|
|
|
|
|
|
manager_get_idle_hint(m, &t);
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic);
|
2011-06-17 15:59:18 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int property_get_inhibited(
|
|
|
|
sd_bus *bus,
|
|
|
|
const char *path,
|
|
|
|
const char *interface,
|
|
|
|
const char *property,
|
|
|
|
sd_bus_message *reply,
|
|
|
|
sd_bus_error *error,
|
|
|
|
void *userdata) {
|
|
|
|
|
|
|
|
Manager *m = userdata;
|
2012-04-16 16:47:33 +02:00
|
|
|
InhibitWhat w;
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(bus);
|
|
|
|
assert(reply);
|
|
|
|
assert(m);
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
w = manager_inhibit_what(m, streq(property, "BlockInhibited") ? INHIBIT_BLOCK : INHIBIT_DELAY);
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_message_append(reply, "s", inhibit_what_to_string(w));
|
2012-04-16 16:47:33 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int property_get_preparing(
|
|
|
|
sd_bus *bus,
|
|
|
|
const char *path,
|
|
|
|
const char *interface,
|
|
|
|
const char *property,
|
|
|
|
sd_bus_message *reply,
|
|
|
|
sd_bus_error *error,
|
|
|
|
void *userdata) {
|
|
|
|
|
|
|
|
Manager *m = userdata;
|
|
|
|
bool b;
|
2012-06-29 19:38:35 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(bus);
|
|
|
|
assert(reply);
|
|
|
|
assert(m);
|
2012-06-29 19:38:35 +02:00
|
|
|
|
|
|
|
if (streq(property, "PreparingForShutdown"))
|
2013-01-25 06:30:23 +01:00
|
|
|
b = !!(m->action_what & INHIBIT_SHUTDOWN);
|
2012-06-29 19:38:35 +02:00
|
|
|
else
|
2013-01-25 06:30:23 +01:00
|
|
|
b = !!(m->action_what & INHIBIT_SLEEP);
|
2012-06-29 19:38:35 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_message_append(reply, "b", b);
|
2012-06-29 19:38:35 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction);
|
2013-07-02 01:46:30 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int method_get_session(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
Manager *m = userdata;
|
|
|
|
const char *name;
|
|
|
|
Session *session;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "s", &name);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
session = hashmap_get(m->sessions, name);
|
|
|
|
if (!session)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
|
|
|
|
|
|
|
|
p = session_bus_path(session);
|
|
|
|
if (!p)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL);
|
|
|
|
|
|
|
|
return sd_bus_reply_method_return(bus, message, "o", p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_get_session_by_pid(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
2013-03-05 03:29:54 +01:00
|
|
|
Session *session = NULL;
|
2013-11-05 01:10:21 +01:00
|
|
|
Manager *m = userdata;
|
|
|
|
uint32_t pid;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "u", &pid);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
r = manager_get_session_by_pid(m, pid, &session);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
if (!session)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_SESSION_FOR_PID, "PID %lu does not belong to any known session", (unsigned long) pid);
|
|
|
|
|
|
|
|
p = session_bus_path(session);
|
|
|
|
if (!p)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL);
|
|
|
|
|
|
|
|
return sd_bus_reply_method_return(bus, message, "o", p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_get_user(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
Manager *m = userdata;
|
|
|
|
uint32_t uid;
|
|
|
|
User *user;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "u", &uid);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
|
|
|
|
if (!user)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_SUCH_USER, "No user '%lu' known or logged in", (unsigned long) uid);
|
|
|
|
|
|
|
|
p = user_bus_path(user);
|
|
|
|
if (!p)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL);
|
|
|
|
|
|
|
|
return sd_bus_reply_method_return(bus, message, "o", p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_get_user_by_pid(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
Manager *m = userdata;
|
2013-03-05 03:29:54 +01:00
|
|
|
User *user = NULL;
|
2013-11-05 01:10:21 +01:00
|
|
|
uint32_t pid;
|
2013-07-02 01:46:30 +02:00
|
|
|
int r;
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
2011-06-24 18:50:50 +02:00
|
|
|
assert(m);
|
2013-11-05 01:10:21 +01:00
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "u", &pid);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
r = manager_get_user_by_pid(m, pid, &user);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
if (!user)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_USER_FOR_PID, "PID %lu does not belong to any known or logged in user", (unsigned long) pid);
|
|
|
|
|
|
|
|
p = user_bus_path(user);
|
|
|
|
if (!p)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL);
|
|
|
|
|
|
|
|
return sd_bus_reply_method_return(bus, message, "o", p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_get_seat(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
Manager *m = userdata;
|
|
|
|
const char *name;
|
|
|
|
Seat *seat;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
2011-06-24 18:50:50 +02:00
|
|
|
assert(message);
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(m);
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_read(message, "s", &name);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
seat = hashmap_get(m->seats, name);
|
|
|
|
if (!seat)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name);
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
p = seat_bus_path(seat);
|
|
|
|
if (!p)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL);
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_return(bus, message, "o", p);
|
|
|
|
}
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int method_list_sessions(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
|
|
|
|
Manager *m = userdata;
|
|
|
|
Session *session;
|
|
|
|
Iterator i;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_new_method_return(bus, message, &reply);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_open_container(reply, 'a', "(susso)");
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
HASHMAP_FOREACH(session, m->sessions, i) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
|
|
|
|
p = session_bus_path(session);
|
|
|
|
if (!p)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL);
|
|
|
|
|
|
|
|
r = sd_bus_message_append(reply, "(susso)",
|
|
|
|
session->id,
|
|
|
|
(uint32_t) session->user->uid,
|
|
|
|
session->user->name,
|
|
|
|
session->seat ? session->seat->id : "",
|
|
|
|
p);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_bus_message_close_container(reply);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
return sd_bus_send(bus, reply, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_list_users(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
|
|
|
|
Manager *m = userdata;
|
|
|
|
User *user;
|
|
|
|
Iterator i;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_new_method_return(bus, message, &reply);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
r = sd_bus_message_open_container(reply, 'a', "(uso)");
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
HASHMAP_FOREACH(user, m->users, i) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
|
|
|
|
p = user_bus_path(user);
|
|
|
|
if (!p)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL);
|
|
|
|
|
|
|
|
r = sd_bus_message_append(reply, "(uso)",
|
|
|
|
(uint32_t) user->uid,
|
|
|
|
user->name,
|
|
|
|
p);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_bus_message_close_container(reply);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
return sd_bus_send(bus, reply, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_list_seats(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
|
|
|
|
Manager *m = userdata;
|
|
|
|
Seat *seat;
|
|
|
|
Iterator i;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_new_method_return(bus, message, &reply);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
r = sd_bus_message_open_container(reply, 'a', "(so)");
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
HASHMAP_FOREACH(seat, m->seats, i) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
|
|
|
|
p = seat_bus_path(seat);
|
|
|
|
if (!p)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL);
|
|
|
|
|
2013-11-05 01:20:52 +01:00
|
|
|
r = sd_bus_message_append(reply, "(so)", seat->id, p);
|
2013-11-05 01:10:21 +01:00
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_bus_message_close_container(reply);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
return sd_bus_send(bus, reply, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_list_inhibitors(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
|
|
|
|
Manager *m = userdata;
|
|
|
|
Inhibitor *inhibitor;
|
|
|
|
Iterator i;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = sd_bus_message_new_method_return(bus, message, &reply);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
r = sd_bus_message_open_container(reply, 'a', "(ssssuu)");
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
HASHMAP_FOREACH(inhibitor, m->inhibitors, i) {
|
|
|
|
|
2013-11-05 17:47:15 +01:00
|
|
|
r = sd_bus_message_append(reply, "(ssssuu)",
|
2013-11-05 01:10:21 +01:00
|
|
|
strempty(inhibit_what_to_string(inhibitor->what)),
|
|
|
|
strempty(inhibitor->who),
|
|
|
|
strempty(inhibitor->why),
|
|
|
|
strempty(inhibit_mode_to_string(inhibitor->mode)),
|
|
|
|
(uint32_t) inhibitor->uid,
|
|
|
|
(uint32_t) inhibitor->pid);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_bus_message_close_container(reply);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
return sd_bus_send(bus, reply, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_create_session(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host;
|
|
|
|
uint32_t uid, leader, audit_id = 0;
|
|
|
|
_cleanup_free_ char *id = NULL;
|
|
|
|
Session *session = NULL;
|
|
|
|
Manager *m = userdata;
|
|
|
|
User *user = NULL;
|
|
|
|
Seat *seat = NULL;
|
|
|
|
int remote;
|
|
|
|
uint32_t vtnr = 0;
|
|
|
|
SessionType t;
|
|
|
|
SessionClass c;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "uussssussbss", &uid, &leader, &service, &type, &class, &cseat, &vtnr, &tty, &display, &remote, &remote_user, &remote_host);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
if (leader == 1)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID");
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-04-09 22:18:16 +02:00
|
|
|
if (isempty(type))
|
|
|
|
t = _SESSION_TYPE_INVALID;
|
|
|
|
else {
|
|
|
|
t = session_type_from_string(type);
|
|
|
|
if (t < 0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid session type %s", type);
|
2013-04-09 22:18:16 +02:00
|
|
|
}
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2012-02-14 21:33:51 +01:00
|
|
|
if (isempty(class))
|
2013-04-09 22:18:16 +02:00
|
|
|
c = _SESSION_CLASS_INVALID;
|
|
|
|
else {
|
2012-02-14 21:33:51 +01:00
|
|
|
c = session_class_from_string(class);
|
2013-04-09 22:18:16 +02:00
|
|
|
if (c < 0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid session class %s", class);
|
2013-04-09 22:18:16 +02:00
|
|
|
}
|
2012-02-14 21:33:51 +01:00
|
|
|
|
2013-03-05 03:29:54 +01:00
|
|
|
if (isempty(cseat))
|
|
|
|
seat = NULL;
|
2011-06-24 18:50:50 +02:00
|
|
|
else {
|
2013-03-05 03:29:54 +01:00
|
|
|
seat = hashmap_get(m->seats, cseat);
|
|
|
|
if (!seat)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", seat);
|
2011-06-24 18:50:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tty_is_vc(tty)) {
|
2011-06-27 22:44:12 +02:00
|
|
|
int v;
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-03-05 03:29:54 +01:00
|
|
|
if (!seat)
|
2013-09-17 17:39:59 +02:00
|
|
|
seat = m->seat0;
|
|
|
|
else if (seat != m->seat0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "TTY %s is virtual console but seat %s is not seat0", tty, seat);
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2011-06-27 22:44:12 +02:00
|
|
|
v = vtnr_from_tty(tty);
|
|
|
|
if (v <= 0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Cannot determine VT number from virtual console TTY %s", tty);
|
2011-06-24 18:50:50 +02:00
|
|
|
|
|
|
|
if (vtnr <= 0)
|
2011-06-27 22:44:12 +02:00
|
|
|
vtnr = (uint32_t) v;
|
|
|
|
else if (vtnr != (uint32_t) v)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Specified TTY and VT number do not match");
|
|
|
|
|
2012-04-22 02:30:13 +02:00
|
|
|
} else if (tty_is_console(tty)) {
|
|
|
|
|
2013-03-05 03:29:54 +01:00
|
|
|
if (!seat)
|
2013-09-17 17:39:59 +02:00
|
|
|
seat = m->seat0;
|
|
|
|
else if (seat != m->seat0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but seat is not seat0");
|
2012-04-22 02:30:13 +02:00
|
|
|
|
|
|
|
if (vtnr != 0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but VT number is not 0");
|
2012-10-28 17:37:16 +01:00
|
|
|
}
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-03-05 03:29:54 +01:00
|
|
|
if (seat) {
|
2013-09-17 17:40:02 +02:00
|
|
|
if (seat_has_vts(seat)) {
|
2012-04-22 02:30:13 +02:00
|
|
|
if (vtnr > 63)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "VT number out of range");
|
2011-06-27 22:44:12 +02:00
|
|
|
} else {
|
2012-04-22 02:30:13 +02:00
|
|
|
if (vtnr != 0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Seat has no VTs but VT number not 0");
|
2011-06-27 22:44:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_enter_container(message, 'a', "(sv)");
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-04-09 22:18:16 +02:00
|
|
|
if (t == _SESSION_TYPE_INVALID) {
|
|
|
|
if (!isempty(display))
|
|
|
|
t = SESSION_X11;
|
|
|
|
else if (!isempty(tty))
|
|
|
|
t = SESSION_TTY;
|
|
|
|
else
|
|
|
|
t = SESSION_UNSPECIFIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == _SESSION_CLASS_INVALID) {
|
|
|
|
if (!isempty(display) || !isempty(tty))
|
|
|
|
c = SESSION_USER;
|
|
|
|
else
|
|
|
|
c = SESSION_BACKGROUND;
|
|
|
|
}
|
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
if (leader <= 0) {
|
2013-11-05 01:10:21 +01:00
|
|
|
assert_cc(sizeof(uint32_t) == sizeof(pid_t));
|
|
|
|
|
|
|
|
r = sd_bus_get_owner_pid(bus, sd_bus_message_get_sender(message), (pid_t*) &leader);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
2013-06-20 03:45:08 +02:00
|
|
|
}
|
|
|
|
|
2013-10-12 01:34:17 +02:00
|
|
|
manager_get_session_by_pid(m, leader, &session);
|
2013-07-02 01:46:30 +02:00
|
|
|
if (session) {
|
|
|
|
_cleanup_free_ char *path = NULL;
|
|
|
|
_cleanup_close_ int fifo_fd = -1;
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-07-02 01:46:30 +02:00
|
|
|
/* Session already exists, client is probably
|
|
|
|
* something like "su" which changes uid but is still
|
|
|
|
* the same session */
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-03-05 03:29:54 +01:00
|
|
|
fifo_fd = session_create_fifo(session);
|
2013-11-05 01:10:21 +01:00
|
|
|
if (fifo_fd < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, fifo_fd, NULL);
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-07-02 01:46:30 +02:00
|
|
|
path = session_bus_path(session);
|
2013-11-05 01:10:21 +01:00
|
|
|
if (!path)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL);
|
2011-06-24 22:55:39 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_return(
|
|
|
|
bus, message, "soshsub",
|
|
|
|
session->id,
|
|
|
|
path,
|
|
|
|
session->user->runtime_path,
|
|
|
|
fifo_fd,
|
|
|
|
session->seat ? session->seat->id : "",
|
|
|
|
(uint32_t) session->vtnr,
|
|
|
|
true);
|
2013-03-05 03:29:54 +01:00
|
|
|
}
|
2011-06-24 22:55:39 +02:00
|
|
|
|
2013-03-05 03:29:54 +01:00
|
|
|
audit_session_from_pid(leader, &audit_id);
|
|
|
|
if (audit_id > 0) {
|
|
|
|
/* Keep our session IDs and the audit session IDs in sync */
|
2011-06-24 22:55:39 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (asprintf(&id, "%lu", (unsigned long) audit_id) < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL);
|
2011-06-24 22:55:39 +02:00
|
|
|
|
2013-03-05 03:29:54 +01:00
|
|
|
/* Wut? There's already a session by this name and we
|
|
|
|
* didn't find it above? Weird, then let's not trust
|
|
|
|
* the audit data and let's better register a new
|
|
|
|
* ID */
|
|
|
|
if (hashmap_get(m->sessions, id)) {
|
2013-09-16 04:26:56 +02:00
|
|
|
log_warning("Existing logind session ID %s used by new audit session, ignoring", id);
|
2013-03-05 03:29:54 +01:00
|
|
|
audit_id = 0;
|
2011-09-23 01:43:28 +02:00
|
|
|
|
2013-03-05 03:29:54 +01:00
|
|
|
free(id);
|
|
|
|
id = NULL;
|
2011-06-24 20:46:22 +02:00
|
|
|
}
|
2013-03-05 03:29:54 +01:00
|
|
|
}
|
2011-06-24 20:46:22 +02:00
|
|
|
|
2013-03-05 03:29:54 +01:00
|
|
|
if (!id) {
|
2011-06-24 20:46:22 +02:00
|
|
|
do {
|
|
|
|
free(id);
|
2012-04-16 16:47:33 +02:00
|
|
|
id = NULL;
|
2011-06-24 20:46:22 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (asprintf(&id, "c%lu", ++m->session_counter) < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL);
|
2011-06-24 20:46:22 +02:00
|
|
|
|
|
|
|
} while (hashmap_get(m->sessions, id));
|
2011-06-24 18:50:50 +02:00
|
|
|
}
|
|
|
|
|
2013-03-05 03:29:54 +01:00
|
|
|
r = manager_add_user_by_uid(m, uid, &user);
|
2013-11-05 01:10:21 +01:00
|
|
|
if (r < 0) {
|
|
|
|
r = sd_bus_reply_method_errno(bus, message, r, NULL);
|
2013-03-05 03:29:54 +01:00
|
|
|
goto fail;
|
2013-11-05 01:10:21 +01:00
|
|
|
}
|
2013-03-05 03:29:54 +01:00
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
r = manager_add_session(m, id, &session);
|
2013-11-05 01:10:21 +01:00
|
|
|
if (r < 0) {
|
|
|
|
r = sd_bus_reply_method_errno(bus, message, r, NULL);
|
2011-06-24 18:50:50 +02:00
|
|
|
goto fail;
|
2013-11-05 01:10:21 +01:00
|
|
|
}
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
session_set_user(session, user);
|
|
|
|
|
2011-06-24 18:50:50 +02:00
|
|
|
session->leader = leader;
|
|
|
|
session->audit_id = audit_id;
|
|
|
|
session->type = t;
|
2012-02-14 21:33:51 +01:00
|
|
|
session->class = c;
|
2011-06-24 18:50:50 +02:00
|
|
|
session->remote = remote;
|
|
|
|
session->vtnr = vtnr;
|
|
|
|
|
|
|
|
if (!isempty(tty)) {
|
|
|
|
session->tty = strdup(tty);
|
|
|
|
if (!session->tty) {
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL);
|
2011-06-24 18:50:50 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isempty(display)) {
|
|
|
|
session->display = strdup(display);
|
|
|
|
if (!session->display) {
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL);
|
2011-06-24 18:50:50 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isempty(remote_user)) {
|
|
|
|
session->remote_user = strdup(remote_user);
|
|
|
|
if (!session->remote_user) {
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL);
|
2011-06-24 18:50:50 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isempty(remote_host)) {
|
|
|
|
session->remote_host = strdup(remote_host);
|
|
|
|
if (!session->remote_host) {
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL);
|
2011-06-24 18:50:50 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isempty(service)) {
|
|
|
|
session->service = strdup(service);
|
|
|
|
if (!session->service) {
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL);
|
2011-06-24 18:50:50 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-05 03:29:54 +01:00
|
|
|
if (seat) {
|
|
|
|
r = seat_attach_session(seat, session);
|
2013-11-05 01:10:21 +01:00
|
|
|
if (r < 0) {
|
|
|
|
r = sd_bus_reply_method_errno(bus, message, r, NULL);
|
2011-06-24 18:50:50 +02:00
|
|
|
goto fail;
|
2013-11-05 01:10:21 +01:00
|
|
|
}
|
2011-06-24 18:50:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
r = session_start(session);
|
2013-11-05 01:10:21 +01:00
|
|
|
if (r < 0) {
|
|
|
|
r = sd_bus_reply_method_errno(bus, message, r, NULL);
|
2011-06-24 18:50:50 +02:00
|
|
|
goto fail;
|
2013-11-05 01:10:21 +01:00
|
|
|
}
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
session->create_message = sd_bus_message_ref(message);
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-07-26 17:32:19 +02:00
|
|
|
/* Now, let's wait until the slice unit and stuff got
|
|
|
|
* created. We send the reply back from
|
|
|
|
* session_send_create_reply().*/
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return 1;
|
2011-06-24 18:50:50 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
if (session)
|
|
|
|
session_add_to_gc_queue(session);
|
|
|
|
|
|
|
|
if (user)
|
|
|
|
user_add_to_gc_queue(user);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int method_release_session(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
|
|
|
Session *session;
|
|
|
|
const char *name;
|
|
|
|
int r;
|
2013-01-25 06:30:23 +01:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "s", &name);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
session = hashmap_get(m->sessions, name);
|
|
|
|
if (!session)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
|
|
|
|
|
|
|
|
/* We use the FIFO to detect stray sessions where the process
|
|
|
|
invoking PAM dies abnormally. We need to make sure that
|
|
|
|
that process is not killed if at the clean end of the
|
|
|
|
session it closes the FIFO. Hence, with this call
|
|
|
|
explicitly turn off the FIFO logic, so that the PAM code
|
|
|
|
can finish clean up on its own */
|
|
|
|
session_remove_fifo(session);
|
|
|
|
session_save(session);
|
|
|
|
user_save(session->user);
|
|
|
|
|
|
|
|
return sd_bus_reply_method_return(bus, message, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_activate_session(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
|
|
|
Session *session;
|
|
|
|
const char *name;
|
|
|
|
int r;
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
2012-04-16 16:47:33 +02:00
|
|
|
assert(m);
|
2013-11-05 01:10:21 +01:00
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "s", &name);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
session = hashmap_get(m->sessions, name);
|
|
|
|
if (!session)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
|
|
|
|
|
|
|
|
r = session_activate(session);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
return sd_bus_reply_method_return(bus, message, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_activate_session_on_seat(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
const char *session_name, *seat_name;
|
|
|
|
Manager *m = userdata;
|
|
|
|
Session *session;
|
|
|
|
Seat *seat;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
2012-04-16 16:47:33 +02:00
|
|
|
assert(message);
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(m);
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
/* Same as ActivateSession() but refuses to work if
|
|
|
|
* the seat doesn't match */
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_read(message, "ss", &session_name, &seat_name);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
2012-05-05 00:34:48 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
session = hashmap_get(m->sessions, session_name);
|
|
|
|
if (!session)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", session_name);
|
2012-09-19 13:10:10 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
seat = hashmap_get(m->seats, seat_name);
|
|
|
|
if (!seat)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", seat_name);
|
2013-01-25 06:30:23 +01:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (session->seat != seat)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", session_name, seat_name);
|
|
|
|
|
|
|
|
r = session_activate(session);
|
2012-04-16 16:47:33 +02:00
|
|
|
if (r < 0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_return(bus, message, NULL);
|
|
|
|
}
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int method_lock_session(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
|
|
|
Session *session;
|
|
|
|
const char *name;
|
|
|
|
int r;
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_read(message, "s", &name);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
session = hashmap_get(m->sessions, name);
|
|
|
|
if (!session)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = session_send_lock(session, streq(sd_bus_message_get_member(message), "LockSession"));
|
2012-04-16 16:47:33 +02:00
|
|
|
if (r < 0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_return(bus, message, NULL);
|
|
|
|
}
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int method_lock_sessions(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
|
|
|
int r;
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = session_send_lock_all(m, streq(sd_bus_message_get_member(message), "LockSessions"));
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_return(bus, message, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_kill_session(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
const char *name, *swho;
|
|
|
|
Manager *m = userdata;
|
|
|
|
Session *session;
|
|
|
|
int32_t signo;
|
|
|
|
KillWho who;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "ssi", &name, &swho, &signo);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
if (isempty(swho))
|
|
|
|
who = KILL_ALL;
|
|
|
|
else {
|
|
|
|
who = kill_who_from_string(swho);
|
|
|
|
if (who < 0)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid kill parameter '%s'", swho);
|
2012-04-16 16:47:33 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (signo <= 0 || signo >= _NSIG)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
session = hashmap_get(m->sessions, name);
|
|
|
|
if (!session)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = session_kill(session, who, signo);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_return(bus, message, NULL);
|
|
|
|
}
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int method_kill_user(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
|
|
|
uint32_t uid;
|
|
|
|
int32_t signo;
|
|
|
|
User *user;
|
|
|
|
int r;
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "ui", &uid, &signo);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
if (signo <= 0 || signo >= _NSIG)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
|
|
|
|
|
|
|
|
user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
|
|
|
|
if (!user)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_SUCH_USER, "No user '%lu' known or logged in", (unsigned long) uid);
|
|
|
|
|
|
|
|
r = user_kill(user, signo);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
return sd_bus_reply_method_return(bus, message, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_terminate_session(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
|
|
|
const char *name;
|
|
|
|
Session *session;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "s", &name);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
session = hashmap_get(m->sessions, name);
|
|
|
|
if (!session)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
|
|
|
|
|
|
|
|
r = session_stop(session);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
return sd_bus_reply_method_return(bus, message, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_terminate_user(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
|
|
|
uint32_t uid;
|
|
|
|
User *user;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "u", &uid);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
|
|
|
|
if (!user)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_SUCH_USER, "No user '%lu' known or logged in", (unsigned long) uid);
|
|
|
|
|
|
|
|
r = user_stop(user);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
return sd_bus_reply_method_return(bus, message, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_terminate_seat(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
|
|
|
const char *name;
|
|
|
|
Seat *seat;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "s", &name);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
seat = hashmap_get(m->seats, name);
|
|
|
|
if (!seat)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name);
|
|
|
|
|
|
|
|
r = seat_stop_sessions(seat);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
return sd_bus_reply_method_return(bus, message, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_set_user_linger(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
_cleanup_free_ char *cc = NULL;
|
|
|
|
Manager *m = userdata;
|
|
|
|
int b, r;
|
|
|
|
struct passwd *pw;
|
|
|
|
const char *path;
|
|
|
|
uint32_t uid;
|
|
|
|
int interactive;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "ubb", &uid, &b, &interactive);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
pw = getpwuid(uid);
|
|
|
|
if (!pw)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, errno ? errno : ENOENT, NULL);
|
|
|
|
|
|
|
|
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 sd_bus_reply_method_errno(bus, message, r, &error);
|
|
|
|
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 sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
cc = cescape(pw->pw_name);
|
|
|
|
if (!cc)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, ENOMEM, NULL);
|
|
|
|
|
|
|
|
path = strappenda("/var/lib/systemd/linger/", cc);
|
|
|
|
if (b) {
|
|
|
|
User *u;
|
|
|
|
|
|
|
|
r = touch(path);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
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 sd_bus_reply_method_errno(bus, message, errno, NULL);
|
|
|
|
|
|
|
|
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(bus, message, NULL);
|
2012-04-16 16:47:33 +02:00
|
|
|
}
|
|
|
|
|
2011-07-13 21:49:21 +02:00
|
|
|
static int trigger_device(Manager *m, struct udev_device *d) {
|
2011-06-29 03:12:23 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-07-13 21:49:21 +02:00
|
|
|
if (d) {
|
|
|
|
if (udev_enumerate_add_match_parent(e, d) < 0) {
|
|
|
|
r = -EIO;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-29 03:12:23 +02:00
|
|
|
if (udev_enumerate_scan_devices(e) < 0) {
|
|
|
|
r = -EIO;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
first = udev_enumerate_get_list_entry(e);
|
|
|
|
udev_list_entry_foreach(item, first) {
|
2013-11-05 01:10:21 +01:00
|
|
|
_cleanup_free_ char *t = NULL;
|
2011-06-29 03:12:23 +02:00
|
|
|
const char *p;
|
|
|
|
|
|
|
|
p = udev_list_entry_get_name(item);
|
|
|
|
|
|
|
|
t = strappend(p, "/uevent");
|
|
|
|
if (!t) {
|
|
|
|
r = -ENOMEM;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2013-04-02 20:31:42 +02:00
|
|
|
write_string_file(t, "change");
|
2011-06-29 03:12:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
r = 0;
|
|
|
|
|
|
|
|
finish:
|
|
|
|
if (e)
|
|
|
|
udev_enumerate_unref(e);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-06-28 20:50:43 +02:00
|
|
|
static int attach_device(Manager *m, const char *seat, const char *sysfs) {
|
2013-04-18 09:11:22 +02:00
|
|
|
_cleanup_free_ char *rule = NULL, *file = NULL;
|
2011-06-28 21:54:52 +02:00
|
|
|
const char *id_for_seat;
|
2013-11-05 01:10:21 +01:00
|
|
|
struct udev_device *d;
|
2011-06-28 20:50:43 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
assert(seat);
|
|
|
|
assert(sysfs);
|
|
|
|
|
|
|
|
d = udev_device_new_from_syspath(m->udev, sysfs);
|
|
|
|
if (!d)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2011-07-13 20:11:23 +02:00
|
|
|
if (!udev_device_has_tag(d, "seat")) {
|
2011-06-28 20:50:43 +02:00
|
|
|
r = -ENODEV;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2011-06-28 21:54:52 +02:00
|
|
|
id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
|
|
|
|
if (!id_for_seat) {
|
2011-06-28 20:50:43 +02:00
|
|
|
r = -ENODEV;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2011-06-28 21:54:52 +02:00
|
|
|
if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) {
|
2011-06-28 20:50:43 +02:00
|
|
|
r = -ENOMEM;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2011-06-28 21:54:52 +02:00
|
|
|
if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) {
|
2011-06-28 20:50:43 +02:00
|
|
|
r = -ENOMEM;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2012-05-31 12:40:20 +02:00
|
|
|
mkdir_p_label("/etc/udev/rules.d", 0755);
|
2013-02-14 12:26:13 +01:00
|
|
|
label_init("/etc");
|
2013-04-02 20:31:42 +02:00
|
|
|
r = write_string_file_atomic_label(file, rule);
|
2011-06-28 23:03:56 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto finish;
|
|
|
|
|
2011-07-13 21:49:21 +02:00
|
|
|
r = trigger_device(m, d);
|
2011-06-28 20:50:43 +02:00
|
|
|
|
|
|
|
finish:
|
|
|
|
if (d)
|
|
|
|
udev_device_unref(d);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-06-29 03:12:23 +02:00
|
|
|
static int flush_devices(Manager *m) {
|
2013-04-18 09:11:22 +02:00
|
|
|
_cleanup_closedir_ DIR *d;
|
2011-06-29 03:12:23 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int method_attach_device(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
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 sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
if (!path_startswith(sysfs, "/sys"))
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not in /sys", sysfs);
|
|
|
|
|
|
|
|
if (!seat_name_is_valid(seat))
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, 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 sd_bus_reply_method_errno(bus, message, r, &error);
|
|
|
|
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 sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
return sd_bus_reply_method_return(bus, message, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_flush_devices(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
Manager *m = userdata;
|
|
|
|
int interactive, r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "b", &interactive);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
|
|
|
|
|
|
|
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 sd_bus_reply_method_errno(bus, message, r, &error);
|
|
|
|
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 sd_bus_reply_method_errno(bus, message, r, &error);
|
|
|
|
|
|
|
|
return sd_bus_reply_method_return(bus, message, NULL);
|
|
|
|
}
|
|
|
|
|
2012-02-11 00:13:10 +01:00
|
|
|
static int have_multiple_sessions(
|
|
|
|
Manager *m,
|
2012-09-21 11:57:48 +02:00
|
|
|
uid_t uid) {
|
2012-02-11 00:13:10 +01:00
|
|
|
|
2012-06-12 09:31:43 +02:00
|
|
|
Session *session;
|
|
|
|
Iterator i;
|
2012-02-11 00:13:10 +01:00
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
2013-01-14 21:40:38 +01:00
|
|
|
/* Check for other users' sessions. Greeter sessions do not
|
|
|
|
* count, and non-login sessions do not count either. */
|
2012-06-12 09:31:43 +02:00
|
|
|
HASHMAP_FOREACH(session, m->sessions, i)
|
2013-01-14 21:40:38 +01:00
|
|
|
if (session->class == SESSION_USER &&
|
2013-09-26 21:32:47 +02:00
|
|
|
!session->closing &&
|
2013-01-14 21:40:38 +01:00
|
|
|
session->user->uid != uid)
|
2012-06-12 09:31:43 +02:00
|
|
|
return true;
|
2012-02-11 00:13:10 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-25 06:30:23 +01:00
|
|
|
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,
|
2013-11-05 01:10:21 +01:00
|
|
|
sd_bus_error *error) {
|
2013-01-25 06:30:23 +01:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
|
|
|
|
const char *p;
|
2013-01-24 04:56:44 +01:00
|
|
|
char *c;
|
2013-11-05 01:10:21 +01:00
|
|
|
int r;
|
2012-05-05 00:34:48 +02:00
|
|
|
|
2013-01-24 04:56:44 +01:00
|
|
|
assert(m);
|
2013-01-25 06:30:23 +01:00
|
|
|
assert(w >= 0);
|
|
|
|
assert(w < _INHIBIT_WHAT_MAX);
|
2012-05-08 19:02:25 +02:00
|
|
|
assert(unit_name);
|
2012-05-05 00:34:48 +02:00
|
|
|
|
2013-01-25 06:30:23 +01:00
|
|
|
bus_manager_log_shutdown(m, w, unit_name);
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_call_method(
|
2013-01-24 04:56:44 +01:00
|
|
|
m->bus,
|
2012-05-05 00:34:48 +02:00
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
"/org/freedesktop/systemd1",
|
|
|
|
"org.freedesktop.systemd1.Manager",
|
2012-08-08 17:20:04 +02:00
|
|
|
"StartUnit",
|
2013-01-24 04:56:44 +01:00
|
|
|
error,
|
2013-11-05 01:10:21 +01:00
|
|
|
&reply,
|
|
|
|
"ss", unit_name, "replace-irreversibly");
|
2013-01-24 04:56:44 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_read(reply, "o", &p);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-01-24 04:56:44 +01:00
|
|
|
|
|
|
|
c = strdup(p);
|
|
|
|
if (!c)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-01-25 06:30:23 +01:00
|
|
|
m->action_unit = unit_name;
|
2013-01-24 04:56:44 +01:00
|
|
|
free(m->action_job);
|
|
|
|
m->action_job = c;
|
2013-01-25 06:30:23 +01:00
|
|
|
m->action_what = w;
|
2013-01-24 04:56:44 +01:00
|
|
|
|
|
|
|
return 0;
|
2012-05-05 00:34:48 +02:00
|
|
|
}
|
|
|
|
|
2013-01-25 06:30:23 +01:00
|
|
|
static int delay_shutdown_or_sleep(
|
|
|
|
Manager *m,
|
|
|
|
InhibitWhat w,
|
|
|
|
const char *unit_name) {
|
2012-05-05 00:34:48 +02:00
|
|
|
|
|
|
|
assert(m);
|
2012-05-08 19:02:25 +02:00
|
|
|
assert(w >= 0);
|
|
|
|
assert(w < _INHIBIT_WHAT_MAX);
|
2013-01-25 06:30:23 +01:00
|
|
|
assert(unit_name);
|
2012-05-05 00:34:48 +02:00
|
|
|
|
2013-01-25 06:30:23 +01:00
|
|
|
m->action_timestamp = now(CLOCK_MONOTONIC);
|
|
|
|
m->action_unit = unit_name;
|
|
|
|
m->action_what = w;
|
2012-05-08 19:02:25 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
|
2012-05-08 19:02:25 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static const char * const signal_name[_INHIBIT_WHAT_MAX] = {
|
|
|
|
[INHIBIT_SHUTDOWN] = "PrepareForShutdown",
|
|
|
|
[INHIBIT_SLEEP] = "PrepareForSleep"
|
|
|
|
};
|
|
|
|
|
|
|
|
int active = _active;
|
2012-08-24 22:21:20 +02:00
|
|
|
|
|
|
|
assert(m);
|
2013-01-25 06:30:23 +01:00
|
|
|
assert(w >= 0);
|
|
|
|
assert(w < _INHIBIT_WHAT_MAX);
|
|
|
|
assert(signal_name[w]);
|
2012-08-24 22:21:20 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_emit_signal(m->bus,
|
|
|
|
"/org/freedesktop/login1",
|
|
|
|
"org.freedesktop.login1.Manager",
|
|
|
|
signal_name[w],
|
|
|
|
"b",
|
2013-11-05 17:59:55 +01:00
|
|
|
active);
|
2012-08-24 22:21:20 +02:00
|
|
|
}
|
|
|
|
|
2012-05-30 15:01:51 +02:00
|
|
|
int bus_manager_shutdown_or_sleep_now_or_later(
|
|
|
|
Manager *m,
|
|
|
|
const char *unit_name,
|
|
|
|
InhibitWhat w,
|
2013-11-05 01:10:21 +01:00
|
|
|
sd_bus_error *error) {
|
2012-05-30 15:01:51 +02:00
|
|
|
|
|
|
|
bool delayed;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
assert(unit_name);
|
|
|
|
assert(w >= 0);
|
|
|
|
assert(w <= _INHIBIT_WHAT_MAX);
|
2013-01-24 04:56:44 +01:00
|
|
|
assert(!m->action_job);
|
2012-05-30 15:01:51 +02:00
|
|
|
|
2013-01-25 06:30:23 +01:00
|
|
|
/* Tell everybody to prepare for shutdown/sleep */
|
|
|
|
send_prepare_for(m, w, true);
|
|
|
|
|
2012-05-30 15:01:51 +02:00
|
|
|
delayed =
|
|
|
|
m->inhibit_delay_max > 0 &&
|
2012-09-21 11:57:48 +02:00
|
|
|
manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0);
|
2012-05-30 15:01:51 +02:00
|
|
|
|
|
|
|
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);
|
2013-01-25 06:30:23 +01:00
|
|
|
else
|
2012-05-30 15:01:51 +02:00
|
|
|
/* Shutdown is not delayed, execute it
|
|
|
|
* immediately */
|
2013-01-25 06:30:23 +01:00
|
|
|
r = execute_shutdown_or_sleep(m, w, unit_name, error);
|
2012-05-30 15:01:51 +02:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int method_do_shutdown_or_sleep(
|
2012-05-08 19:02:25 +02:00
|
|
|
Manager *m,
|
2013-11-05 01:10:21 +01:00
|
|
|
sd_bus_message *message,
|
2012-05-08 19:02:25 +02:00
|
|
|
const char *unit_name,
|
|
|
|
InhibitWhat w,
|
|
|
|
const char *action,
|
|
|
|
const char *action_multiple_sessions,
|
|
|
|
const char *action_ignore_inhibit,
|
2013-05-04 18:31:28 +02:00
|
|
|
const char *sleep_verb,
|
2013-11-05 01:10:21 +01:00
|
|
|
sd_bus_message_handler_t method) {
|
2012-05-08 19:02:25 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
|
2012-05-30 15:01:51 +02:00
|
|
|
bool multiple_sessions, blocked;
|
2013-11-05 01:10:21 +01:00
|
|
|
int interactive, r;
|
|
|
|
uid_t uid;
|
2012-05-08 19:02:25 +02:00
|
|
|
|
|
|
|
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);
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(method);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "b", &interactive);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(m->bus, message, r, NULL);
|
2012-05-08 19:02:25 +02:00
|
|
|
|
2013-01-25 06:30:23 +01:00
|
|
|
/* Don't allow multiple jobs being executed at the same time */
|
|
|
|
if (m->action_what)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errorf(m->bus, message, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
|
2012-05-08 19:02:25 +02:00
|
|
|
|
2013-05-04 18:31:28 +02:00
|
|
|
if (sleep_verb) {
|
|
|
|
r = can_sleep(sleep_verb);
|
2012-10-28 00:49:04 +02:00
|
|
|
if (r < 0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errno(m->bus, message, r, NULL);
|
2012-10-28 00:49:04 +02:00
|
|
|
|
|
|
|
if (r == 0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errorf(m->bus, message, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Sleep verb not supported");
|
2012-10-28 00:49:04 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_get_owner_uid(m->bus, sd_bus_message_get_sender(message), &uid);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(m->bus, message, r, NULL);
|
2012-09-21 11:57:48 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = have_multiple_sessions(m, uid);
|
2012-05-08 19:02:25 +02:00
|
|
|
if (r < 0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errno(m->bus, message, r, NULL);
|
2012-05-08 19:02:25 +02:00
|
|
|
|
|
|
|
multiple_sessions = r > 0;
|
2013-11-05 01:10:21 +01:00
|
|
|
blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid);
|
2012-05-08 19:02:25 +02:00
|
|
|
|
|
|
|
if (multiple_sessions) {
|
2013-11-05 01:10:21 +01:00
|
|
|
r = bus_verify_polkit_async(m->bus, &m->polkit_registry, message,
|
|
|
|
action_multiple_sessions, interactive, &error, method, m);
|
2012-05-08 19:02:25 +02:00
|
|
|
if (r < 0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errno(m->bus, message, r, &error);
|
2012-05-08 19:02:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (blocked) {
|
2013-11-05 01:10:21 +01:00
|
|
|
r = bus_verify_polkit_async(m->bus, &m->polkit_registry, message,
|
|
|
|
action_ignore_inhibit, interactive, &error, method, m);
|
2012-05-08 19:02:25 +02:00
|
|
|
if (r < 0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errno(m->bus, message, r, &error);
|
2012-05-08 19:02:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!multiple_sessions && !blocked) {
|
2013-11-05 01:10:21 +01:00
|
|
|
r = bus_verify_polkit_async(m->bus, &m->polkit_registry, message,
|
|
|
|
action, interactive, &error, method, m);
|
2012-05-08 19:02:25 +02:00
|
|
|
if (r < 0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errno(m->bus, message, r, &error);
|
2012-05-08 19:02:25 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, &error);
|
2012-05-08 19:02:25 +02:00
|
|
|
if (r < 0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errno(m->bus, message, r, &error);
|
2012-05-08 19:02:25 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_return(m->bus, message, NULL);
|
2012-05-05 00:34:48 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int method_poweroff(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
2011-05-26 02:21:16 +02:00
|
|
|
Manager *m = userdata;
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
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);
|
|
|
|
}
|
2011-07-09 02:58:05 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int method_reboot(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
2011-07-09 02:58:05 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
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);
|
|
|
|
}
|
2011-07-09 02:58:05 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int method_suspend(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
2011-07-09 02:58:05 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
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);
|
|
|
|
}
|
2011-07-09 02:58:05 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int method_hibernate(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
2013-01-24 05:29:37 +01:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
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);
|
|
|
|
}
|
2012-07-29 16:17:06 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int method_hybrid_sleep(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
2012-07-29 16:17:06 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
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);
|
|
|
|
}
|
2011-07-13 19:58:35 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
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) {
|
2011-07-13 19:58:35 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
bool multiple_sessions, challenge, blocked;
|
|
|
|
const char *result = NULL;
|
|
|
|
uid_t uid;
|
|
|
|
int r;
|
2011-07-13 19:58:35 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(m);
|
|
|
|
assert(message);
|
|
|
|
assert(w >= 0);
|
|
|
|
assert(w <= _INHIBIT_WHAT_MAX);
|
|
|
|
assert(action);
|
|
|
|
assert(action_multiple_sessions);
|
|
|
|
assert(action_ignore_inhibit);
|
2011-07-13 19:58:35 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (sleep_verb) {
|
|
|
|
r = can_sleep(sleep_verb);
|
2011-07-13 19:58:35 +02:00
|
|
|
if (r < 0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errno(m->bus, message, r, NULL);
|
|
|
|
if (r == 0)
|
|
|
|
return sd_bus_reply_method_return(m->bus, message, "s", "na");
|
|
|
|
}
|
2011-07-13 19:58:35 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_get_owner_uid(m->bus, sd_bus_message_get_sender(message), &uid);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(m->bus, message, r, NULL);
|
2011-07-13 19:58:35 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = have_multiple_sessions(m, uid);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(m->bus, message, r, NULL);
|
2011-07-13 19:58:35 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
multiple_sessions = r > 0;
|
|
|
|
blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid);
|
2011-07-13 19:58:35 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (multiple_sessions) {
|
|
|
|
r = bus_verify_polkit(m->bus, message, action_multiple_sessions, false, &challenge, &error);
|
2011-07-13 19:58:35 +02:00
|
|
|
if (r < 0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errno(m->bus, message, r, &error);
|
2011-06-21 19:20:05 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (r > 0)
|
|
|
|
result = "yes";
|
|
|
|
else if (challenge)
|
|
|
|
result = "challenge";
|
|
|
|
else
|
|
|
|
result = "no";
|
|
|
|
}
|
2011-06-21 19:20:05 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (blocked) {
|
|
|
|
r = bus_verify_polkit(m->bus, message, action_ignore_inhibit, false, &challenge, &error);
|
2011-06-21 19:20:05 +02:00
|
|
|
if (r < 0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errno(m->bus, message, r, &error);
|
2011-06-21 19:20:05 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (r > 0 && !result)
|
|
|
|
result = "yes";
|
|
|
|
else if (challenge && (!result || streq(result, "yes")))
|
|
|
|
result = "challenge";
|
|
|
|
else
|
|
|
|
result = "no";
|
|
|
|
}
|
2011-06-21 19:20:05 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (!multiple_sessions && !blocked) {
|
|
|
|
/* If neither inhibit nor multiple sessions
|
|
|
|
* apply then just check the normal policy */
|
2011-06-21 19:20:05 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = bus_verify_polkit(m->bus, message, action, false, &challenge, &error);
|
2011-06-21 19:20:05 +02:00
|
|
|
if (r < 0)
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_errno(m->bus, message, r, &error);
|
2011-06-21 19:20:05 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (r > 0)
|
|
|
|
result = "yes";
|
|
|
|
else if (challenge)
|
|
|
|
result = "challenge";
|
|
|
|
else
|
|
|
|
result = "no";
|
|
|
|
}
|
2011-06-21 19:20:05 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_return(m->bus, message, "s", result);
|
|
|
|
}
|
2011-06-21 19:20:05 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int method_can_poweroff(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
2011-06-21 19:20:05 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
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);
|
|
|
|
}
|
2011-06-21 19:20:05 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int method_can_reboot(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
2011-06-21 19:20:05 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
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);
|
|
|
|
}
|
2011-06-21 19:20:05 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int method_can_suspend(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
2011-06-28 03:52:22 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
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");
|
|
|
|
}
|
2011-06-28 03:52:22 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int method_can_hibernate(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
2012-01-27 20:48:28 +01:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
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");
|
|
|
|
}
|
2011-06-28 03:52:22 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int method_can_hybrid_sleep(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
2011-06-28 03:52:22 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
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");
|
|
|
|
}
|
2011-06-28 18:16:32 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int method_inhibit(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
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;
|
2011-06-28 03:52:22 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
2011-06-28 18:16:32 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
2011-06-28 18:16:32 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
w = inhibit_what_from_string(what);
|
|
|
|
if (w <= 0)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid what specification %s", what);
|
2011-06-28 18:16:32 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
mm = inhibit_mode_from_string(mode);
|
|
|
|
if (mm < 0)
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid mode specification %s", mode);
|
2011-06-28 03:52:22 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
/* Delay is only supported for shutdown/sleep */
|
|
|
|
if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP)))
|
|
|
|
return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Delay inhibitors only supported for shutdown and sleep");
|
2011-06-28 18:16:32 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
/* 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_reply_method_errorf(bus, message, 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 sd_bus_reply_method_errno(bus, message, r, &error);
|
|
|
|
if (r == 0)
|
|
|
|
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
|
2011-06-28 03:52:22 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_get_owner_uid(m->bus, sd_bus_message_get_sender(message), &uid);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
2011-06-28 03:52:22 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_get_owner_pid(m->bus, sd_bus_message_get_sender(message), &pid);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
2011-06-28 20:50:43 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
do {
|
|
|
|
free(id);
|
|
|
|
id = NULL;
|
2011-06-28 20:50:43 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL);
|
2011-06-28 20:50:43 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
} while (hashmap_get(m->inhibitors, id));
|
2011-06-28 20:50:43 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = manager_add_inhibitor(m, id, &i);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_reply_method_errno(bus, message, r, NULL);
|
2011-06-28 20:50:43 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
i->what = w;
|
|
|
|
i->mode = mm;
|
|
|
|
i->pid = pid;
|
|
|
|
i->uid = uid;
|
|
|
|
i->why = strdup(why);
|
|
|
|
i->who = strdup(who);
|
2011-06-28 03:52:22 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (!i->why || !i->who) {
|
|
|
|
r = sd_bus_reply_method_errno(bus, message, -ENOMEM, NULL);
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-06-29 03:12:23 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
fifo_fd = inhibitor_create_fifo(i);
|
|
|
|
if (fifo_fd < 0) {
|
|
|
|
r = sd_bus_reply_method_errno(bus, message, fifo_fd, NULL);
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-06-29 03:12:23 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
inhibitor_start(i);
|
2011-06-29 03:12:23 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_reply_method_return(bus, message, "h", fifo_fd);
|
2011-06-29 03:12:23 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
fail:
|
|
|
|
if (i)
|
|
|
|
inhibitor_free(i);
|
2012-02-11 00:13:10 +01:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return r;
|
|
|
|
}
|
2011-05-26 02:21:16 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
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)", "soshsub", 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
|
|
|
|
};
|
2011-05-26 02:21:16 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
int match_job_removed(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
const char *path, *result, *unit;
|
|
|
|
Manager *m = userdata;
|
|
|
|
Session *session;
|
|
|
|
uint32_t id;
|
|
|
|
User *user;
|
|
|
|
int r;
|
2011-05-26 02:21:16 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
2011-05-26 02:21:16 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to parse JobRemoved message: %s", strerror(-r));
|
|
|
|
return 0;
|
|
|
|
}
|
2011-05-26 02:21:16 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (m->action_job && streq(m->action_job, path)) {
|
|
|
|
log_info("Operation finished.");
|
2011-05-26 02:21:16 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
/* Tell people that they now may take a lock again */
|
|
|
|
send_prepare_for(m, m->action_what, false);
|
2011-05-26 02:21:16 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
free(m->action_job);
|
|
|
|
m->action_job = NULL;
|
|
|
|
m->action_unit = NULL;
|
|
|
|
m->action_what = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2011-05-26 02:21:16 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
session = hashmap_get(m->session_units, unit);
|
|
|
|
if (session) {
|
2011-05-26 02:21:16 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (streq_ptr(path, session->scope_job)) {
|
|
|
|
free(session->scope_job);
|
|
|
|
session->scope_job = NULL;
|
2011-05-26 02:21:16 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (session->started) {
|
|
|
|
if (streq(result, "done"))
|
|
|
|
session_send_create_reply(session, NULL);
|
|
|
|
else {
|
|
|
|
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
|
2011-05-26 02:21:16 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
sd_bus_error_setf(&error, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
|
|
|
|
session_send_create_reply(session, &error);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
session_save(session);
|
2011-05-26 02:21:16 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
session_add_to_gc_queue(session);
|
|
|
|
}
|
2011-05-26 02:21:16 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
user = hashmap_get(m->user_units, unit);
|
|
|
|
if (user) {
|
2011-05-26 02:21:16 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (streq_ptr(path, user->service_job)) {
|
|
|
|
free(user->service_job);
|
|
|
|
user->service_job = NULL;
|
2011-05-26 02:21:16 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (streq_ptr(path, user->slice_job)) {
|
|
|
|
free(user->slice_job);
|
|
|
|
user->slice_job = NULL;
|
|
|
|
}
|
2011-05-26 02:21:16 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
user_save(user);
|
|
|
|
user_add_to_gc_queue(user);
|
2011-05-26 02:21:16 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return 0;
|
2011-05-26 02:21:16 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
int match_unit_removed(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
const char *path, *unit;
|
2011-06-24 20:41:56 +02:00
|
|
|
Manager *m = userdata;
|
2013-11-05 01:10:21 +01:00
|
|
|
Session *session;
|
|
|
|
User *user;
|
|
|
|
int r;
|
2011-06-24 20:41:56 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(bus);
|
2011-06-24 20:41:56 +02:00
|
|
|
assert(message);
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(m);
|
2011-06-24 20:41:56 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_read(message, "so", &unit, &path);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to parse UnitRemoved message: %s", strerror(-r));
|
|
|
|
return 0;
|
|
|
|
}
|
2013-07-02 01:46:30 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
session = hashmap_get(m->session_units, unit);
|
|
|
|
if (session)
|
|
|
|
session_add_to_gc_queue(session);
|
2013-07-02 01:46:30 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
user = hashmap_get(m->user_units, unit);
|
|
|
|
if (user)
|
|
|
|
user_add_to_gc_queue(user);
|
2013-07-02 01:46:30 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2013-07-02 01:46:30 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
int match_properties_changed(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
_cleanup_free_ char *unit = NULL;
|
|
|
|
Manager *m = userdata;
|
|
|
|
const char *path;
|
|
|
|
Session *session;
|
|
|
|
User *user;
|
2013-07-02 01:46:30 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
2013-07-02 01:46:30 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
path = sd_bus_message_get_path(message);
|
|
|
|
if (!path)
|
|
|
|
return 0;
|
2013-07-02 01:46:30 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
unit_name_from_dbus_path(path, &unit);
|
|
|
|
if (!unit)
|
|
|
|
return 0;
|
2013-07-02 01:46:30 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
session = hashmap_get(m->session_units, unit);
|
|
|
|
if (session)
|
|
|
|
session_add_to_gc_queue(session);
|
2013-07-02 01:46:30 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
user = hashmap_get(m->user_units, unit);
|
|
|
|
if (user)
|
|
|
|
user_add_to_gc_queue(user);
|
2013-07-02 01:46:30 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2013-07-10 19:24:03 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
int match_reloading(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
|
|
|
Session *session;
|
|
|
|
Iterator i;
|
|
|
|
int b, r;
|
2013-07-03 15:12:58 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(bus);
|
2013-07-03 15:12:58 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_read(message, "b", &b);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to parse Reloading message: %s", strerror(-r));
|
|
|
|
return 0;
|
|
|
|
}
|
2013-07-03 15:12:58 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (b)
|
|
|
|
return 0;
|
2013-07-03 15:12:58 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
/* systemd finished reloading, let's recheck all our sessions */
|
|
|
|
log_debug("System manager has been reloaded, rechecking sessions...");
|
2013-07-10 23:31:40 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
HASHMAP_FOREACH(session, m->sessions, i)
|
|
|
|
session_add_to_gc_queue(session);
|
2013-07-10 23:31:40 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2013-07-03 15:12:58 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
int match_name_owner_changed(sd_bus *bus, sd_bus_message *message, void *userdata) {
|
|
|
|
const char *name, *old, *new;
|
|
|
|
Manager *m = userdata;
|
|
|
|
Session *session;
|
|
|
|
Iterator i;
|
|
|
|
int r;
|
2013-07-03 15:12:58 +02:00
|
|
|
|
2013-07-10 23:31:40 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
char *key;
|
2013-09-17 17:39:55 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_read(message, "sss", &name, &old, &new);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to parse NameOwnerChanged message: %s", strerror(-r));
|
|
|
|
return 0;
|
|
|
|
}
|
2013-09-17 17:39:55 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (isempty(old) || !isempty(new))
|
|
|
|
return 0;
|
2013-09-17 17:39:55 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
key = set_remove(m->busnames, (char*) old);
|
|
|
|
if (!key)
|
|
|
|
return 0;
|
2013-09-17 17:39:56 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
/* Drop all controllers owned by this name */
|
2013-09-17 17:39:56 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
free(key);
|
2011-06-24 20:41:56 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
HASHMAP_FOREACH(session, m->sessions, i)
|
|
|
|
if (session_is_controller(session, old))
|
|
|
|
session_drop_controller(session);
|
2011-06-24 20:41:56 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return 0;
|
2011-06-24 20:41:56 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
int manager_send_changed(Manager *manager, const char *property, ...) {
|
|
|
|
char **l;
|
2011-06-21 21:46:13 +02:00
|
|
|
|
|
|
|
assert(manager);
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
l = strv_from_stdarg_alloca(property);
|
2011-06-21 21:46:13 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_emit_properties_changed_strv(
|
|
|
|
manager->bus,
|
|
|
|
"/org/freedesktop/login1",
|
|
|
|
"org.freedesktop.login1.Manager",
|
|
|
|
l);
|
2011-06-21 21:46:13 +02:00
|
|
|
}
|
2012-05-05 00:34:48 +02:00
|
|
|
|
2012-05-08 19:02:25 +02:00
|
|
|
int manager_dispatch_delayed(Manager *manager) {
|
2013-11-05 01:10:21 +01:00
|
|
|
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
|
2012-05-05 00:34:48 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(manager);
|
|
|
|
|
2013-04-24 20:23:01 +02:00
|
|
|
if (manager->action_what == 0 || manager->action_job)
|
2012-05-05 00:34:48 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Continue delay? */
|
2013-01-25 06:30:23 +01:00
|
|
|
if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0)) {
|
2012-05-05 00:34:48 +02:00
|
|
|
|
2013-01-25 06:30:23 +01:00
|
|
|
if (manager->action_timestamp + manager->inhibit_delay_max > now(CLOCK_MONOTONIC))
|
|
|
|
return 0;
|
2013-01-24 04:56:44 +01:00
|
|
|
|
2013-01-25 06:30:23 +01:00
|
|
|
log_info("Delay lock is active but inhibitor timeout is reached.");
|
|
|
|
}
|
2012-05-05 00:34:48 +02:00
|
|
|
|
2013-01-25 06:30:23 +01:00
|
|
|
/* Actually do the operation */
|
|
|
|
r = execute_shutdown_or_sleep(manager, manager->action_what, manager->action_unit, &error);
|
2012-05-05 00:34:48 +02:00
|
|
|
if (r < 0) {
|
2013-11-05 01:10:21 +01:00
|
|
|
log_warning("Failed to send delayed message: %s", bus_error_message(&error, r));
|
2013-01-25 06:30:23 +01:00
|
|
|
|
|
|
|
manager->action_unit = NULL;
|
|
|
|
manager->action_what = 0;
|
2012-05-05 00:34:48 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2013-07-02 01:46:30 +02:00
|
|
|
|
|
|
|
int manager_start_scope(
|
|
|
|
Manager *manager,
|
|
|
|
const char *scope,
|
|
|
|
pid_t pid,
|
|
|
|
const char *slice,
|
|
|
|
const char *description,
|
2013-07-10 23:39:46 +02:00
|
|
|
const char *after,
|
2013-08-13 17:59:28 +02:00
|
|
|
const char *kill_mode,
|
2013-11-05 01:10:21 +01:00
|
|
|
sd_bus_error *error,
|
2013-07-02 01:46:30 +02:00
|
|
|
char **job) {
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
|
|
|
|
int r;
|
2013-07-02 01:46:30 +02:00
|
|
|
|
|
|
|
assert(manager);
|
|
|
|
assert(scope);
|
|
|
|
assert(pid > 1);
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_new_method_call(
|
|
|
|
manager->bus,
|
2013-07-02 01:46:30 +02:00
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
"/org/freedesktop/systemd1",
|
|
|
|
"org.freedesktop.systemd1.Manager",
|
2013-11-05 01:10:21 +01:00
|
|
|
"StartTransientUnit",
|
|
|
|
&m);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 01:46:30 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_append(m, "ss", strempty(scope), "fail");
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 01:46:30 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_open_container(m, 'a', "(sv)");
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 01:46:30 +02:00
|
|
|
|
|
|
|
if (!isempty(slice)) {
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 01:46:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isempty(description)) {
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 01:46:30 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (!isempty(description)) {
|
|
|
|
r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-10 23:39:46 +02:00
|
|
|
}
|
|
|
|
|
2013-08-13 17:59:28 +02:00
|
|
|
if (!isempty(kill_mode)) {
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_append(m, "(sv)", "KillMode", "s", kill_mode);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-08-13 17:59:28 +02:00
|
|
|
}
|
|
|
|
|
2013-07-02 01:46:30 +02:00
|
|
|
/* 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. */
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_append(m, "(sv)", "TimeoutStopUSec", "t", 500 * USEC_PER_MSEC);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 01:46:30 +02:00
|
|
|
|
2013-07-30 02:51:12 +02:00
|
|
|
/* Make sure that the session shells are terminated with
|
|
|
|
* SIGHUP since bash and friends tend to ignore SIGTERM */
|
2013-11-05 01:10:21 +01:00
|
|
|
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_send_with_reply_and_block(manager->bus, m, 0, error, &reply);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 01:46:30 +02:00
|
|
|
|
|
|
|
if (job) {
|
|
|
|
const char *j;
|
|
|
|
char *copy;
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_read(reply, "o", &j);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 01:46:30 +02:00
|
|
|
|
|
|
|
copy = strdup(j);
|
|
|
|
if (!copy)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
*job = copy;
|
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return 1;
|
2013-07-02 01:46:30 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
|
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
|
2013-07-02 01:46:30 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(manager);
|
|
|
|
assert(unit);
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_call_method(
|
2013-07-02 01:46:30 +02:00
|
|
|
manager->bus,
|
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
"/org/freedesktop/systemd1",
|
|
|
|
"org.freedesktop.systemd1.Manager",
|
|
|
|
"StartUnit",
|
|
|
|
error,
|
2013-11-05 01:10:21 +01:00
|
|
|
&reply,
|
|
|
|
"ss", unit, "fail");
|
|
|
|
if (r < 0)
|
2013-07-02 01:46:30 +02:00
|
|
|
return r;
|
|
|
|
|
|
|
|
if (job) {
|
|
|
|
const char *j;
|
|
|
|
char *copy;
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_read(reply, "o", &j);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 01:46:30 +02:00
|
|
|
|
|
|
|
copy = strdup(j);
|
|
|
|
if (!copy)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
*job = copy;
|
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return 1;
|
2013-07-02 01:46:30 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
|
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
|
2013-07-02 01:46:30 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(manager);
|
|
|
|
assert(unit);
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_call_method(
|
2013-07-02 01:46:30 +02:00
|
|
|
manager->bus,
|
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
"/org/freedesktop/systemd1",
|
|
|
|
"org.freedesktop.systemd1.Manager",
|
|
|
|
"StopUnit",
|
|
|
|
error,
|
2013-11-05 01:10:21 +01:00
|
|
|
&reply,
|
|
|
|
"ss", unit, "fail");
|
2013-07-02 01:46:30 +02:00
|
|
|
if (r < 0) {
|
2013-11-05 01:10:21 +01:00
|
|
|
if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
|
|
|
|
sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
|
2013-07-10 23:31:40 +02:00
|
|
|
|
|
|
|
if (job)
|
|
|
|
*job = NULL;
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
sd_bus_error_free(error);
|
2013-07-10 23:31:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-02 01:46:30 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (job) {
|
|
|
|
const char *j;
|
|
|
|
char *copy;
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_read(reply, "o", &j);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 01:46:30 +02:00
|
|
|
|
|
|
|
copy = strdup(j);
|
|
|
|
if (!copy)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
*job = copy;
|
|
|
|
}
|
|
|
|
|
2013-07-10 23:31:40 +02:00
|
|
|
return 1;
|
2013-07-02 01:46:30 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) {
|
2013-07-02 01:46:30 +02:00
|
|
|
assert(manager);
|
|
|
|
assert(unit);
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return sd_bus_call_method(
|
2013-07-02 01:46:30 +02:00
|
|
|
manager->bus,
|
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
"/org/freedesktop/systemd1",
|
|
|
|
"org.freedesktop.systemd1.Manager",
|
|
|
|
"KillUnit",
|
|
|
|
error,
|
2013-11-05 01:10:21 +01:00
|
|
|
NULL,
|
|
|
|
"ssi", unit, who == KILL_LEADER ? "main" : "all", signo);
|
2013-07-02 01:46:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int manager_unit_is_active(Manager *manager, const char *unit) {
|
2013-11-05 01:10:21 +01:00
|
|
|
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
|
2013-07-02 01:46:30 +02:00
|
|
|
_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;
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_get_property(
|
2013-07-02 01:46:30 +02:00
|
|
|
manager->bus,
|
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
path,
|
2013-11-05 01:10:21 +01:00
|
|
|
"org.freedesktop.systemd1.Unit",
|
|
|
|
"ActiveState",
|
2013-07-02 01:46:30 +02:00
|
|
|
&error,
|
2013-11-05 01:10:21 +01:00
|
|
|
&reply,
|
|
|
|
"s");
|
2013-07-02 01:46:30 +02:00
|
|
|
if (r < 0) {
|
2013-11-05 01:10:21 +01:00
|
|
|
/* 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))
|
2013-07-10 23:31:40 +02:00
|
|
|
return true;
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
/* 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))
|
2013-07-10 23:31:40 +02:00
|
|
|
return false;
|
|
|
|
|
2013-07-02 01:46:30 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_bus_message_read(reply, "s", &state);
|
|
|
|
if (r < 0)
|
2013-07-02 01:46:30 +02:00
|
|
|
return -EINVAL;
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
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;
|
2013-07-02 01:46:30 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
/* We don't actually care about the state really. The fact
|
|
|
|
* that we could read the job state is enough for us */
|
2013-07-02 01:46:30 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
return true;
|
2013-07-02 01:46:30 +02:00
|
|
|
}
|