| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "bus-get-properties.h" |
| 7 |
#include "bus-object.h" |
| 8 |
#include "bus-util.h" |
| 9 |
#include "dbus.h" |
| 10 |
#include "dbus-job.h" |
| 11 |
#include "dbus-unit.h" |
| 12 |
#include "dbus-util.h" |
| 13 |
#include "hashmap.h" |
| 14 |
#include "job.h" |
| 15 |
#include "log.h" |
| 16 |
#include "manager.h" |
| 17 |
#include "selinux-access.h" |
| 18 |
#include "strv.h" |
| 19 |
|
| 20 |
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type, job_type, JobType); |
| 21 |
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_state, job_state, JobState); |
| 22 |
|
| 23 |
static int property_get_unit( |
| 24 |
sd_bus *bus, |
| 25 |
const char *path, |
| 26 |
const char *interface, |
| 27 |
const char *property, |
| 28 |
sd_bus_message *reply, |
| 29 |
void *userdata, |
| 30 |
sd_bus_error *reterr_error) { |
| 31 |
|
| 32 |
_cleanup_free_ char *p = NULL; |
| 33 |
Job *j = ASSERT_PTR(userdata); |
| 34 |
|
| 35 |
assert(bus); |
| 36 |
assert(reply); |
| 37 |
|
| 38 |
p = unit_dbus_path(j->unit); |
| 39 |
if (!p) |
| 40 |
return -ENOMEM; |
| 41 |
|
| 42 |
return sd_bus_message_append(reply, "(so)", j->unit->id, p); |
| 43 |
} |
| 44 |
|
| 45 |
int bus_job_method_cancel(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) { |
| 46 |
Job *j = ASSERT_PTR(userdata); |
| 47 |
int r; |
| 48 |
|
| 49 |
assert(message); |
| 50 |
|
| 51 |
r = mac_selinux_unit_access_check(j->unit, message, "stop", reterr_error); |
| 52 |
if (r < 0) |
| 53 |
return r; |
| 54 |
|
| 55 |
/* Access is granted to the job owner */ |
| 56 |
if (!sd_bus_track_contains(j->bus_track, sd_bus_message_get_sender(message))) { |
| 57 |
|
| 58 |
/* And for everybody else consult polkit */ |
| 59 |
r = bus_verify_manage_units_async(j->manager, message, reterr_error); |
| 60 |
if (r < 0) |
| 61 |
return r; |
| 62 |
if (r == 0) |
| 63 |
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
| 64 |
} |
| 65 |
|
| 66 |
job_finish_and_invalidate(j, JOB_CANCELED, true, false); |
| 67 |
|
| 68 |
return sd_bus_reply_method_return(message, NULL); |
| 69 |
} |
| 70 |
|
| 71 |
int bus_job_method_get_waiting_jobs(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) { |
| 72 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 73 |
_cleanup_free_ Job **list = NULL; |
| 74 |
Job *j = userdata; |
| 75 |
int r, n; |
| 76 |
|
| 77 |
if (strstr(sd_bus_message_get_member(message), "After")) |
| 78 |
n = job_get_after(j, &list); |
| 79 |
else |
| 80 |
n = job_get_before(j, &list); |
| 81 |
if (n < 0) |
| 82 |
return n; |
| 83 |
|
| 84 |
r = sd_bus_message_new_method_return(message, &reply); |
| 85 |
if (r < 0) |
| 86 |
return r; |
| 87 |
|
| 88 |
r = sd_bus_message_open_container(reply, 'a', "(usssoo)"); |
| 89 |
if (r < 0) |
| 90 |
return r; |
| 91 |
|
| 92 |
FOREACH_ARRAY(i, list, n) { |
| 93 |
_cleanup_free_ char *unit_path = NULL, *job_path = NULL; |
| 94 |
Job *job = *i; |
| 95 |
|
| 96 |
job_path = job_dbus_path(job); |
| 97 |
if (!job_path) |
| 98 |
return -ENOMEM; |
| 99 |
|
| 100 |
unit_path = unit_dbus_path(job->unit); |
| 101 |
if (!unit_path) |
| 102 |
return -ENOMEM; |
| 103 |
|
| 104 |
r = sd_bus_message_append(reply, "(usssoo)", |
| 105 |
job->id, |
| 106 |
job->unit->id, |
| 107 |
job_type_to_string(job->type), |
| 108 |
job_state_to_string(job->state), |
| 109 |
job_path, |
| 110 |
unit_path); |
| 111 |
if (r < 0) |
| 112 |
return r; |
| 113 |
} |
| 114 |
|
| 115 |
r = sd_bus_message_close_container(reply); |
| 116 |
if (r < 0) |
| 117 |
return r; |
| 118 |
|
| 119 |
return sd_bus_message_send(reply); |
| 120 |
} |
| 121 |
|
| 122 |
const sd_bus_vtable bus_job_vtable[] = { |
| 123 |
SD_BUS_VTABLE_START(0), |
| 124 |
|
| 125 |
SD_BUS_METHOD("Cancel", NULL, NULL, bus_job_method_cancel, SD_BUS_VTABLE_UNPRIVILEGED), |
| 126 |
SD_BUS_METHOD_WITH_ARGS("GetAfter", |
| 127 |
SD_BUS_NO_ARGS, |
| 128 |
SD_BUS_RESULT("a(usssoo)", jobs), |
| 129 |
bus_job_method_get_waiting_jobs, |
| 130 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 131 |
SD_BUS_METHOD_WITH_ARGS("GetBefore", |
| 132 |
SD_BUS_NO_ARGS, |
| 133 |
SD_BUS_RESULT("a(usssoo)", jobs), |
| 134 |
bus_job_method_get_waiting_jobs, |
| 135 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 136 |
|
| 137 |
SD_BUS_PROPERTY("Id", "u", NULL, offsetof(Job, id), SD_BUS_VTABLE_PROPERTY_CONST), |
| 138 |
SD_BUS_PROPERTY("Unit", "(so)", property_get_unit, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 139 |
SD_BUS_PROPERTY("JobType", "s", property_get_type, offsetof(Job, type), SD_BUS_VTABLE_PROPERTY_CONST), |
| 140 |
SD_BUS_PROPERTY("State", "s", property_get_state, offsetof(Job, state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 141 |
SD_BUS_PROPERTY("ActivationDetails", "a(ss)", bus_property_get_activation_details, offsetof(Job, activation_details), SD_BUS_VTABLE_PROPERTY_CONST), |
| 142 |
SD_BUS_VTABLE_END |
| 143 |
}; |
| 144 |
|
| 145 |
static int bus_job_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *reterr_error) { |
| 146 |
Manager *m = ASSERT_PTR(userdata); |
| 147 |
Job *j; |
| 148 |
int r; |
| 149 |
|
| 150 |
assert(bus); |
| 151 |
assert(path); |
| 152 |
assert(interface); |
| 153 |
assert(found); |
| 154 |
|
| 155 |
r = manager_get_job_from_dbus_path(m, path, &j); |
| 156 |
if (r < 0) |
| 157 |
return 0; |
| 158 |
|
| 159 |
*found = j; |
| 160 |
return 1; |
| 161 |
} |
| 162 |
|
| 163 |
static int bus_job_enumerate(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *reterr_error) { |
| 164 |
_cleanup_strv_free_ char **l = NULL; |
| 165 |
Manager *m = userdata; |
| 166 |
unsigned k = 0; |
| 167 |
Job *j; |
| 168 |
|
| 169 |
l = new0(char*, hashmap_size(m->jobs)+1); |
| 170 |
if (!l) |
| 171 |
return -ENOMEM; |
| 172 |
|
| 173 |
HASHMAP_FOREACH(j, m->jobs) { |
| 174 |
l[k] = job_dbus_path(j); |
| 175 |
if (!l[k]) |
| 176 |
return -ENOMEM; |
| 177 |
|
| 178 |
k++; |
| 179 |
} |
| 180 |
|
| 181 |
assert(hashmap_size(m->jobs) == k); |
| 182 |
|
| 183 |
*nodes = TAKE_PTR(l); |
| 184 |
|
| 185 |
return k; |
| 186 |
} |
| 187 |
|
| 188 |
const BusObjectImplementation job_object = { |
| 189 |
"/org/freedesktop/systemd1/job", |
| 190 |
"org.freedesktop.systemd1.Job", |
| 191 |
.fallback_vtables = BUS_FALLBACK_VTABLES({bus_job_vtable, bus_job_find}), |
| 192 |
.node_enumerator = bus_job_enumerate, |
| 193 |
}; |
| 194 |
|
| 195 |
static int send_new_signal(sd_bus *bus, void *userdata) { |
| 196 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 197 |
_cleanup_free_ char *p = NULL; |
| 198 |
Job *j = ASSERT_PTR(userdata); |
| 199 |
int r; |
| 200 |
|
| 201 |
assert(bus); |
| 202 |
|
| 203 |
p = job_dbus_path(j); |
| 204 |
if (!p) |
| 205 |
return -ENOMEM; |
| 206 |
|
| 207 |
r = sd_bus_message_new_signal( |
| 208 |
bus, |
| 209 |
&m, |
| 210 |
"/org/freedesktop/systemd1", |
| 211 |
"org.freedesktop.systemd1.Manager", |
| 212 |
"JobNew"); |
| 213 |
if (r < 0) |
| 214 |
return r; |
| 215 |
|
| 216 |
r = sd_bus_message_append(m, "uos", j->id, p, j->unit->id); |
| 217 |
if (r < 0) |
| 218 |
return r; |
| 219 |
|
| 220 |
return sd_bus_send(bus, m, NULL); |
| 221 |
} |
| 222 |
|
| 223 |
static int send_changed_signal(sd_bus *bus, void *userdata) { |
| 224 |
_cleanup_free_ char *p = NULL; |
| 225 |
Job *j = ASSERT_PTR(userdata); |
| 226 |
|
| 227 |
assert(bus); |
| 228 |
|
| 229 |
p = job_dbus_path(j); |
| 230 |
if (!p) |
| 231 |
return -ENOMEM; |
| 232 |
|
| 233 |
return sd_bus_emit_properties_changed(bus, p, "org.freedesktop.systemd1.Job", "State", NULL); |
| 234 |
} |
| 235 |
|
| 236 |
void bus_job_send_change_signal(Job *j) { |
| 237 |
int r; |
| 238 |
|
| 239 |
assert(j); |
| 240 |
|
| 241 |
/* Make sure that any change signal on the unit is reflected before we send out the change signal on the job */ |
| 242 |
bus_unit_send_pending_change_signal(j->unit, true); |
| 243 |
|
| 244 |
if (j->in_dbus_queue) { |
| 245 |
LIST_REMOVE(dbus_queue, j->manager->dbus_job_queue, j); |
| 246 |
j->in_dbus_queue = false; |
| 247 |
|
| 248 |
/* The job might be good to be GC once its pending signals have been sent */ |
| 249 |
job_add_to_gc_queue(j); |
| 250 |
} |
| 251 |
|
| 252 |
r = bus_foreach_bus(j->manager, j->bus_track, j->sent_dbus_new_signal ? send_changed_signal : send_new_signal, j); |
| 253 |
if (r < 0) |
| 254 |
log_debug_errno(r, "Failed to send job change signal for %u: %m", j->id); |
| 255 |
|
| 256 |
j->sent_dbus_new_signal = true; |
| 257 |
} |
| 258 |
|
| 259 |
void bus_job_send_pending_change_signal(Job *j, bool including_new) { |
| 260 |
assert(j); |
| 261 |
|
| 262 |
if (!j->in_dbus_queue) |
| 263 |
return; |
| 264 |
|
| 265 |
if (!j->sent_dbus_new_signal && !including_new) |
| 266 |
return; |
| 267 |
|
| 268 |
if (MANAGER_IS_RELOADING(j->manager)) |
| 269 |
return; |
| 270 |
|
| 271 |
bus_job_send_change_signal(j); |
| 272 |
} |
| 273 |
|
| 274 |
static int send_removed_signal(sd_bus *bus, void *userdata) { |
| 275 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 276 |
_cleanup_free_ char *p = NULL; |
| 277 |
Job *j = ASSERT_PTR(userdata); |
| 278 |
int r; |
| 279 |
|
| 280 |
assert(bus); |
| 281 |
|
| 282 |
p = job_dbus_path(j); |
| 283 |
if (!p) |
| 284 |
return -ENOMEM; |
| 285 |
|
| 286 |
r = sd_bus_message_new_signal( |
| 287 |
bus, |
| 288 |
&m, |
| 289 |
"/org/freedesktop/systemd1", |
| 290 |
"org.freedesktop.systemd1.Manager", |
| 291 |
"JobRemoved"); |
| 292 |
if (r < 0) |
| 293 |
return r; |
| 294 |
|
| 295 |
r = sd_bus_message_append(m, "uoss", j->id, p, j->unit->id, job_result_to_string(j->result)); |
| 296 |
if (r < 0) |
| 297 |
return r; |
| 298 |
|
| 299 |
return sd_bus_send(bus, m, NULL); |
| 300 |
} |
| 301 |
|
| 302 |
void bus_job_send_removed_signal(Job *j) { |
| 303 |
int r; |
| 304 |
|
| 305 |
assert(j); |
| 306 |
|
| 307 |
if (!j->sent_dbus_new_signal) |
| 308 |
bus_job_send_change_signal(j); |
| 309 |
|
| 310 |
/* Make sure that any change signal on the unit is reflected before we send out the change signal on the job */ |
| 311 |
bus_unit_send_pending_change_signal(j->unit, true); |
| 312 |
|
| 313 |
r = bus_foreach_bus(j->manager, j->bus_track, send_removed_signal, j); |
| 314 |
if (r < 0) |
| 315 |
log_debug_errno(r, "Failed to send job remove signal for %u: %m", j->id); |
| 316 |
} |
| 317 |
|
| 318 |
static int bus_job_track_handler(sd_bus_track *t, void *userdata) { |
| 319 |
Job *j = ASSERT_PTR(userdata); |
| 320 |
|
| 321 |
assert(t); |
| 322 |
|
| 323 |
j->bus_track = sd_bus_track_unref(j->bus_track); /* make sure we aren't called again */ |
| 324 |
|
| 325 |
/* Last client dropped off the bus, maybe we should GC this now? */ |
| 326 |
job_add_to_gc_queue(j); |
| 327 |
return 0; |
| 328 |
} |
| 329 |
|
| 330 |
static int bus_job_allocate_bus_track(Job *j) { |
| 331 |
|
| 332 |
assert(j); |
| 333 |
|
| 334 |
if (j->bus_track) |
| 335 |
return 0; |
| 336 |
|
| 337 |
return sd_bus_track_new(j->manager->api_bus, &j->bus_track, bus_job_track_handler, j); |
| 338 |
} |
| 339 |
|
| 340 |
int bus_job_coldplug_bus_track(Job *j) { |
| 341 |
_cleanup_strv_free_ char **deserialized_clients = NULL; |
| 342 |
int r; |
| 343 |
|
| 344 |
assert(j); |
| 345 |
|
| 346 |
deserialized_clients = TAKE_PTR(j->deserialized_clients); |
| 347 |
|
| 348 |
if (strv_isempty(deserialized_clients)) |
| 349 |
return 0; |
| 350 |
|
| 351 |
if (!j->manager->api_bus) |
| 352 |
return 0; |
| 353 |
|
| 354 |
r = bus_job_allocate_bus_track(j); |
| 355 |
if (r < 0) |
| 356 |
return r; |
| 357 |
|
| 358 |
return bus_track_add_name_many(j->bus_track, deserialized_clients); |
| 359 |
} |
| 360 |
|
| 361 |
int bus_job_track_sender(Job *j, sd_bus_message *m) { |
| 362 |
int r; |
| 363 |
|
| 364 |
assert(j); |
| 365 |
assert(m); |
| 366 |
|
| 367 |
if (sd_bus_message_get_bus(m) != j->manager->api_bus) { |
| 368 |
j->ref_by_private_bus = true; |
| 369 |
return 0; |
| 370 |
} |
| 371 |
|
| 372 |
r = bus_job_allocate_bus_track(j); |
| 373 |
if (r < 0) |
| 374 |
return r; |
| 375 |
|
| 376 |
return sd_bus_track_add_sender(j->bus_track, m); |
| 377 |
} |