dbus-job.c revision df2d202e6ed4001a21c6512c244acad5d4706c87
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
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
(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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "log.h"
#include "sd-bus.h"
#include "selinux-access.h"
#include "job.h"
#include "dbus-job.h"
#include "dbus-client-track.h"
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type, job_type, JobType);
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_state, job_state, JobState);
static int property_get_unit(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
sd_bus_error *error,
void *userdata) {
_cleanup_free_ char *p = NULL;
Job *j = userdata;
assert(bus);
assert(reply);
assert(j);
p = unit_dbus_path(j->unit);
if (!p)
return -ENOMEM;
return sd_bus_message_append(reply, "(so)", j->unit->id, p);
}
static int method_cancel(sd_bus *bus, sd_bus_message *message, void *userdata) {
Job *j = userdata;
assert(bus);
assert(message);
assert(j);
SELINUX_UNIT_ACCESS_CHECK(j->unit, bus, message, "stop");
job_finish_and_invalidate(j, JOB_CANCELED, true);
return sd_bus_reply_method_return(message, NULL);
}
const sd_bus_vtable bus_job_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_METHOD("Cancel", NULL, NULL, method_cancel, 0),
SD_BUS_PROPERTY("Id", "u", NULL, offsetof(Job, id), 0),
SD_BUS_PROPERTY("Unit", "(so)", property_get_unit, 0, 0),
SD_BUS_PROPERTY("JobType", "s", property_get_type, offsetof(Job, type), 0),
SD_BUS_PROPERTY("State", "s", property_get_state, offsetof(Job, state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_VTABLE_END
};
static int foreach_client(Job *j, int (*send_message)(sd_bus *bus, const char *name, Job *j)) {
BusTrackedClient *one_destination = NULL;
Iterator i;
sd_bus *b;
unsigned n, m;
int r;
assert(j);
assert(send_message);
n = set_size(j->manager->subscribed);
m = set_size(j->subscribed);
if (n <= 0 && m <= 0)
return 0;
if (n == 1 && m == 0)
one_destination = set_first(j->manager->subscribed);
else if (n == 0 && m == 1)
one_destination = set_first(j->subscribed);
else
one_destination = NULL;
if (one_destination)
return send_message(one_destination->bus, isempty(one_destination->name) ? NULL : one_destination->name, j);
/* Send to everybody */
SET_FOREACH(b, j->manager->private_buses, i) {
r = send_message(b, NULL, j);
if (r < 0)
return r;
}
if (j->manager->api_bus)
return send_message(j->manager->api_bus, NULL, j);
return 0;
}
static int send_new_signal(sd_bus *bus, const char *destination, Job *j) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
_cleanup_free_ char *p = NULL;
int r;
assert(bus);
assert(j);
p = job_dbus_path(j);
if (!p)
return -ENOMEM;
r = sd_bus_message_new_signal(
bus,
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"JobNew",
&m);
if (r < 0)
return r;
r = sd_bus_message_append(m, "uos", j->id, p, j->unit->id);
if (r < 0)
return r;
return sd_bus_send_to(bus, m, destination, NULL);
}
static int send_changed_signal(sd_bus *bus, const char *destination, Job *j) {
_cleanup_free_ char *p = NULL;
assert(bus);
assert(j);
p = job_dbus_path(j);
if (!p)
return -ENOMEM;
return sd_bus_emit_properties_changed(bus, p, "org.freedesktop.systemd1.Job", "State", NULL);
}
void bus_job_send_change_signal(Job *j) {
int r;
assert(j);
if (j->in_dbus_queue) {
LIST_REMOVE(dbus_queue, j->manager->dbus_job_queue, j);
j->in_dbus_queue = false;
}
r = foreach_client(j, j->sent_dbus_new_signal ? send_changed_signal : send_new_signal);
if (r < 0)
log_warning("Failed to send job change signal for %u/%s: %s", j->id, j->unit->id, strerror(-r));
j->sent_dbus_new_signal = true;
}
static int send_removed_signal(sd_bus *bus, const char *destination, Job *j) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
_cleanup_free_ char *p = NULL;
int r;
assert(bus);
assert(j);
p = job_dbus_path(j);
if (!p)
return -ENOMEM;
r = sd_bus_message_new_signal(
bus,
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"JobRemoved",
&m);
if (r < 0)
return r;
r = sd_bus_message_append(m, "uoss", j->id, p, j->unit->id, job_result_to_string(j->result));
if (r < 0)
return r;
return sd_bus_send_to(bus, m, destination, NULL);
}
void bus_job_send_removed_signal(Job *j) {
int r;
assert(j);
if (!j->sent_dbus_new_signal)
bus_job_send_change_signal(j);
r = foreach_client(j, send_removed_signal);
if (r < 0)
log_warning("Failed to send job removal signal for %u/%s: %s", j->id, j->unit->id, strerror(-r));
return;
}