2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2010-02-03 13:03:47 +01:00
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2010 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
|
2010-02-03 13:03:47 +01: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.
|
2010-02-03 13:03:47 +01:00
|
|
|
|
2012-04-12 00:20:58 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2010-02-03 13:03:47 +01:00
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
2013-11-19 21:12:59 +01:00
|
|
|
#include "sd-bus.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
|
|
|
#include "dbus-job.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "dbus.h"
|
2013-11-19 21:12:59 +01:00
|
|
|
#include "job.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "log.h"
|
|
|
|
#include "selinux-access.h"
|
|
|
|
#include "string-util.h"
|
2013-11-19 21:12:59 +01:00
|
|
|
|
|
|
|
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,
|
2013-11-21 19:34:37 +01:00
|
|
|
void *userdata,
|
|
|
|
sd_bus_error *error) {
|
2010-02-01 03:33:24 +01:00
|
|
|
|
2013-07-25 17:36:01 +02:00
|
|
|
_cleanup_free_ char *p = NULL;
|
2013-11-19 21:12:59 +01:00
|
|
|
Job *j = userdata;
|
2010-02-02 12:42:08 +01:00
|
|
|
|
2013-11-19 21:12:59 +01:00
|
|
|
assert(bus);
|
|
|
|
assert(reply);
|
2010-02-02 12:42:08 +01:00
|
|
|
assert(j);
|
|
|
|
|
2012-10-02 23:07:00 +02:00
|
|
|
p = unit_dbus_path(j->unit);
|
|
|
|
if (!p)
|
2010-02-02 12:42:08 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-11-19 21:12:59 +01:00
|
|
|
return sd_bus_message_append(reply, "(so)", j->unit->id, p);
|
2010-02-02 12:42:08 +01:00
|
|
|
}
|
|
|
|
|
2015-04-29 18:35:10 +02:00
|
|
|
int bus_job_method_cancel(sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
2013-11-19 21:12:59 +01:00
|
|
|
Job *j = userdata;
|
2013-11-21 19:34:37 +01:00
|
|
|
int r;
|
2010-02-01 03:33:24 +01:00
|
|
|
|
|
|
|
assert(message);
|
2013-11-19 21:12:59 +01:00
|
|
|
assert(j);
|
2010-02-02 12:42:08 +01:00
|
|
|
|
2014-10-24 14:15:25 +02:00
|
|
|
r = mac_selinux_unit_access_check(j->unit, message, "stop", error);
|
2013-11-21 19:34:37 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-02-18 17:40:57 +01:00
|
|
|
/* Access is granted to the job owner */
|
2016-11-15 19:29:50 +01:00
|
|
|
if (!sd_bus_track_contains(j->bus_track, sd_bus_message_get_sender(message))) {
|
2015-02-18 17:40:57 +01:00
|
|
|
|
|
|
|
/* And for everybody else consult PolicyKit */
|
|
|
|
r = bus_verify_manage_units_async(j->unit->manager, message, error);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (r == 0)
|
|
|
|
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:24:51 +02:00
|
|
|
job_finish_and_invalidate(j, JOB_CANCELED, true, false);
|
2010-10-13 03:03:31 +02:00
|
|
|
|
2013-11-21 01:51:16 +01:00
|
|
|
return sd_bus_reply_method_return(message, NULL);
|
2010-02-01 03:33:24 +01:00
|
|
|
}
|
|
|
|
|
2016-11-16 16:07:32 +01:00
|
|
|
int bus_job_method_get_waiting_jobs(sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
|
|
|
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
|
|
|
|
_cleanup_free_ Job **list = NULL;
|
|
|
|
Job *j = userdata;
|
|
|
|
int r, i, n;
|
|
|
|
|
|
|
|
if (strstr(sd_bus_message_get_member(message), "After"))
|
|
|
|
n = job_get_after(j, &list);
|
|
|
|
else
|
|
|
|
n = job_get_before(j, &list);
|
|
|
|
if (n < 0)
|
|
|
|
return n;
|
|
|
|
|
|
|
|
r = sd_bus_message_new_method_return(message, &reply);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_message_open_container(reply, 'a', "(usssoo)");
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i ++) {
|
|
|
|
_cleanup_free_ char *unit_path = NULL, *job_path = NULL;
|
|
|
|
|
|
|
|
job_path = job_dbus_path(list[i]);
|
|
|
|
if (!job_path)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
unit_path = unit_dbus_path(list[i]->unit);
|
|
|
|
if (!unit_path)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
r = sd_bus_message_append(reply, "(usssoo)",
|
|
|
|
list[i]->id,
|
|
|
|
list[i]->unit->id,
|
|
|
|
job_type_to_string(list[i]->type),
|
|
|
|
job_state_to_string(list[i]->state),
|
|
|
|
job_path,
|
|
|
|
unit_path);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_bus_message_close_container(reply);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return sd_bus_send(NULL, reply, NULL);
|
|
|
|
}
|
|
|
|
|
2013-11-19 21:12:59 +01:00
|
|
|
const sd_bus_vtable bus_job_vtable[] = {
|
|
|
|
SD_BUS_VTABLE_START(0),
|
2014-08-06 11:45:36 +02:00
|
|
|
SD_BUS_METHOD("Cancel", NULL, NULL, bus_job_method_cancel, SD_BUS_VTABLE_UNPRIVILEGED),
|
2016-11-16 16:07:32 +01:00
|
|
|
SD_BUS_METHOD("GetAfter", NULL, "a(usssoo)", bus_job_method_get_waiting_jobs, SD_BUS_VTABLE_UNPRIVILEGED),
|
|
|
|
SD_BUS_METHOD("GetBefore", NULL, "a(usssoo)", bus_job_method_get_waiting_jobs, SD_BUS_VTABLE_UNPRIVILEGED),
|
2013-12-22 02:24:05 +01:00
|
|
|
SD_BUS_PROPERTY("Id", "u", NULL, offsetof(Job, id), SD_BUS_VTABLE_PROPERTY_CONST),
|
|
|
|
SD_BUS_PROPERTY("Unit", "(so)", property_get_unit, 0, SD_BUS_VTABLE_PROPERTY_CONST),
|
|
|
|
SD_BUS_PROPERTY("JobType", "s", property_get_type, offsetof(Job, type), SD_BUS_VTABLE_PROPERTY_CONST),
|
2013-11-19 21:12:59 +01:00
|
|
|
SD_BUS_PROPERTY("State", "s", property_get_state, offsetof(Job, state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
|
|
|
|
SD_BUS_VTABLE_END
|
2010-02-01 03:33:24 +01:00
|
|
|
};
|
2010-02-05 00:38:41 +01:00
|
|
|
|
2014-03-03 01:33:45 +01:00
|
|
|
static int send_new_signal(sd_bus *bus, void *userdata) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
|
2013-06-28 03:47:40 +02:00
|
|
|
_cleanup_free_ char *p = NULL;
|
2014-03-03 01:33:45 +01:00
|
|
|
Job *j = userdata;
|
2013-11-19 21:12:59 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(j);
|
2010-02-05 00:38:41 +01:00
|
|
|
|
2012-04-20 12:28:31 +02:00
|
|
|
p = job_dbus_path(j);
|
|
|
|
if (!p)
|
2013-11-19 21:12:59 +01:00
|
|
|
return -ENOMEM;
|
2010-02-05 00:38:41 +01:00
|
|
|
|
2013-11-19 21:12:59 +01:00
|
|
|
r = sd_bus_message_new_signal(
|
|
|
|
bus,
|
2014-02-19 23:54:58 +01:00
|
|
|
&m,
|
2013-11-19 21:12:59 +01:00
|
|
|
"/org/freedesktop/systemd1",
|
|
|
|
"org.freedesktop.systemd1.Manager",
|
2014-02-19 23:54:58 +01:00
|
|
|
"JobNew");
|
2013-11-19 21:12:59 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_message_append(m, "uos", j->id, p, j->unit->id);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2014-03-03 01:33:45 +01:00
|
|
|
return sd_bus_send(bus, m, NULL);
|
2010-02-05 00:38:41 +01:00
|
|
|
}
|
|
|
|
|
2014-03-03 01:33:45 +01:00
|
|
|
static int send_changed_signal(sd_bus *bus, void *userdata) {
|
2013-06-28 03:47:40 +02:00
|
|
|
_cleanup_free_ char *p = NULL;
|
2014-03-03 01:33:45 +01:00
|
|
|
Job *j = userdata;
|
2013-11-19 21:12:59 +01:00
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(j);
|
2010-02-05 00:38:41 +01:00
|
|
|
|
2012-04-20 12:28:31 +02:00
|
|
|
p = job_dbus_path(j);
|
|
|
|
if (!p)
|
2013-11-19 21:12:59 +01:00
|
|
|
return -ENOMEM;
|
2010-02-05 00:38:41 +01:00
|
|
|
|
2013-11-19 21:12:59 +01:00
|
|
|
return sd_bus_emit_properties_changed(bus, p, "org.freedesktop.systemd1.Job", "State", NULL);
|
2012-04-20 12:28:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void bus_job_send_change_signal(Job *j) {
|
2013-11-19 21:12:59 +01:00
|
|
|
int r;
|
|
|
|
|
2012-04-20 12:28:31 +02:00
|
|
|
assert(j);
|
|
|
|
|
|
|
|
if (j->in_dbus_queue) {
|
2013-10-14 06:10:14 +02:00
|
|
|
LIST_REMOVE(dbus_queue, j->manager->dbus_job_queue, j);
|
2012-04-20 12:28:31 +02:00
|
|
|
j->in_dbus_queue = false;
|
|
|
|
}
|
|
|
|
|
2016-11-15 19:29:50 +01:00
|
|
|
r = bus_foreach_bus(j->manager, j->bus_track, j->sent_dbus_new_signal ? send_changed_signal : send_new_signal, j);
|
2013-11-19 21:12:59 +01:00
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_debug_errno(r, "Failed to send job change signal for %u: %m", j->id);
|
2012-04-20 12:28:31 +02:00
|
|
|
|
|
|
|
j->sent_dbus_new_signal = true;
|
2013-11-19 21:12:59 +01:00
|
|
|
}
|
|
|
|
|
2014-03-03 01:33:45 +01:00
|
|
|
static int send_removed_signal(sd_bus *bus, void *userdata) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
|
2013-11-19 21:12:59 +01:00
|
|
|
_cleanup_free_ char *p = NULL;
|
2014-03-03 01:33:45 +01:00
|
|
|
Job *j = userdata;
|
2013-11-19 21:12:59 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(j);
|
|
|
|
|
|
|
|
p = job_dbus_path(j);
|
|
|
|
if (!p)
|
|
|
|
return -ENOMEM;
|
2010-02-05 00:38:41 +01:00
|
|
|
|
2013-11-19 21:12:59 +01:00
|
|
|
r = sd_bus_message_new_signal(
|
|
|
|
bus,
|
2014-02-19 23:54:58 +01:00
|
|
|
&m,
|
2013-11-19 21:12:59 +01:00
|
|
|
"/org/freedesktop/systemd1",
|
|
|
|
"org.freedesktop.systemd1.Manager",
|
2014-02-19 23:54:58 +01:00
|
|
|
"JobRemoved");
|
2013-11-19 21:12:59 +01:00
|
|
|
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;
|
|
|
|
|
2014-03-03 01:33:45 +01:00
|
|
|
return sd_bus_send(bus, m, NULL);
|
2012-04-20 12:28:31 +02:00
|
|
|
}
|
2010-02-05 00:38:41 +01:00
|
|
|
|
2012-04-20 12:28:31 +02:00
|
|
|
void bus_job_send_removed_signal(Job *j) {
|
2013-11-19 21:12:59 +01:00
|
|
|
int r;
|
2010-02-05 00:38:41 +01:00
|
|
|
|
2013-11-19 21:12:59 +01:00
|
|
|
assert(j);
|
2012-04-20 12:28:31 +02:00
|
|
|
|
|
|
|
if (!j->sent_dbus_new_signal)
|
|
|
|
bus_job_send_change_signal(j);
|
|
|
|
|
2016-11-15 19:29:50 +01:00
|
|
|
r = bus_foreach_bus(j->manager, j->bus_track, send_removed_signal, j);
|
2013-11-19 21:12:59 +01:00
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_debug_errno(r, "Failed to send job remove signal for %u: %m", j->id);
|
2010-02-05 00:38:41 +01:00
|
|
|
}
|
2016-11-15 19:32:50 +01:00
|
|
|
|
|
|
|
static int bus_job_track_handler(sd_bus_track *t, void *userdata) {
|
|
|
|
Job *j = userdata;
|
|
|
|
|
|
|
|
assert(t);
|
|
|
|
assert(j);
|
|
|
|
|
|
|
|
j->bus_track = sd_bus_track_unref(j->bus_track); /* make sure we aren't called again */
|
|
|
|
|
|
|
|
/* Last client dropped off the bus, maybe we should GC this now? */
|
|
|
|
job_add_to_gc_queue(j);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bus_job_allocate_bus_track(Job *j) {
|
|
|
|
|
|
|
|
assert(j);
|
|
|
|
|
|
|
|
if (j->bus_track)
|
|
|
|
return 0;
|
|
|
|
|
2016-11-17 03:01:11 +01:00
|
|
|
return sd_bus_track_new(j->unit->manager->api_bus, &j->bus_track, bus_job_track_handler, j);
|
2016-11-15 19:32:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int bus_job_coldplug_bus_track(Job *j) {
|
|
|
|
int r = 0;
|
2016-11-17 03:01:11 +01:00
|
|
|
_cleanup_strv_free_ char **deserialized_clients = NULL;
|
2016-11-15 19:32:50 +01:00
|
|
|
|
|
|
|
assert(j);
|
|
|
|
|
2018-03-22 16:53:26 +01:00
|
|
|
deserialized_clients = TAKE_PTR(j->deserialized_clients);
|
2016-11-17 03:01:11 +01:00
|
|
|
|
|
|
|
if (strv_isempty(deserialized_clients))
|
|
|
|
return 0;
|
2016-11-15 19:32:50 +01:00
|
|
|
|
|
|
|
if (!j->manager->api_bus)
|
2016-11-17 03:01:11 +01:00
|
|
|
return 0;
|
2016-11-15 19:32:50 +01:00
|
|
|
|
|
|
|
r = bus_job_allocate_bus_track(j);
|
|
|
|
if (r < 0)
|
2016-11-17 03:01:11 +01:00
|
|
|
return r;
|
2016-11-15 19:32:50 +01:00
|
|
|
|
2016-11-17 03:01:11 +01:00
|
|
|
return bus_track_add_name_many(j->bus_track, deserialized_clients);
|
2016-11-15 19:32:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int bus_job_track_sender(Job *j, sd_bus_message *m) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(j);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
if (sd_bus_message_get_bus(m) != j->unit->manager->api_bus) {
|
|
|
|
j->ref_by_private_bus = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = bus_job_allocate_bus_track(j);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return sd_bus_track_add_sender(j->bus_track, m);
|
|
|
|
}
|