| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-bus.h" |
| 6 |
#include "sd-id128.h" |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "bus-common-errors.h" |
| 10 |
#include "bus-error.h" |
| 11 |
#include "bus-internal.h" |
| 12 |
#include "bus-object.h" |
| 13 |
#include "bus-util.h" |
| 14 |
#include "dbus.h" |
| 15 |
#include "dbus-automount.h" |
| 16 |
#include "dbus-cgroup.h" |
| 17 |
#include "dbus-device.h" |
| 18 |
#include "dbus-execute.h" |
| 19 |
#include "dbus-job.h" |
| 20 |
#include "dbus-kill.h" |
| 21 |
#include "dbus-manager.h" |
| 22 |
#include "dbus-mount.h" |
| 23 |
#include "dbus-path.h" |
| 24 |
#include "dbus-scope.h" |
| 25 |
#include "dbus-service.h" |
| 26 |
#include "dbus-slice.h" |
| 27 |
#include "dbus-socket.h" |
| 28 |
#include "dbus-swap.h" |
| 29 |
#include "dbus-target.h" |
| 30 |
#include "dbus-timer.h" |
| 31 |
#include "dbus-unit.h" |
| 32 |
#include "errno-util.h" |
| 33 |
#include "fd-util.h" |
| 34 |
#include "fdset.h" |
| 35 |
#include "format-util.h" |
| 36 |
#include "fs-util.h" |
| 37 |
#include "log.h" |
| 38 |
#include "manager.h" |
| 39 |
#include "path-util.h" |
| 40 |
#include "pidref.h" |
| 41 |
#include "process-util.h" |
| 42 |
#include "selinux-access.h" /* IWYU pragma: keep */ |
| 43 |
#include "serialize.h" |
| 44 |
#include "set.h" |
| 45 |
#include "special.h" |
| 46 |
#include "string-util.h" |
| 47 |
#include "strv.h" |
| 48 |
#include "umask-util.h" |
| 49 |
|
| 50 |
#define CONNECTIONS_MAX 4096 |
| 51 |
|
| 52 |
static void destroy_bus(Manager *m, sd_bus **bus); |
| 53 |
|
| 54 |
void bus_send_pending_reload_message(Manager *m) { |
| 55 |
int r; |
| 56 |
|
| 57 |
assert(m); |
| 58 |
|
| 59 |
if (!m->pending_reload_message_dbus) |
| 60 |
return; |
| 61 |
|
| 62 |
/* If we cannot get rid of this message we won't dispatch any D-Bus messages, so that we won't end up wanting |
| 63 |
* to queue another message. */ |
| 64 |
|
| 65 |
r = sd_bus_message_send(m->pending_reload_message_dbus); |
| 66 |
if (r < 0) |
| 67 |
log_warning_errno(r, "Failed to send queued reload message, ignoring: %m"); |
| 68 |
|
| 69 |
m->pending_reload_message_dbus = sd_bus_message_unref(m->pending_reload_message_dbus); |
| 70 |
|
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
static int signal_disconnected(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) { |
| 75 |
Manager *m = ASSERT_PTR(userdata); |
| 76 |
sd_bus *bus; |
| 77 |
|
| 78 |
assert(message); |
| 79 |
assert_se(bus = sd_bus_message_get_bus(message)); |
| 80 |
|
| 81 |
if (bus == m->api_bus) { |
| 82 |
log_notice("Got disconnect on API bus."); |
| 83 |
bus_done_api(m); |
| 84 |
} |
| 85 |
if (bus == m->system_bus) { |
| 86 |
/* If we are the system manager, this is already logged by the API bus. */ |
| 87 |
if (!MANAGER_IS_SYSTEM(m)) |
| 88 |
log_notice("Got disconnect on system bus."); |
| 89 |
bus_done_system(m); |
| 90 |
} |
| 91 |
|
| 92 |
if (set_remove(m->private_buses, bus)) { |
| 93 |
log_debug("Got disconnect on private connection."); |
| 94 |
destroy_bus(m, &bus); |
| 95 |
} |
| 96 |
|
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
static int signal_activation_request(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { |
| 101 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 102 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 103 |
Manager *m = ASSERT_PTR(userdata); |
| 104 |
const char *name; |
| 105 |
Unit *u; |
| 106 |
int r; |
| 107 |
|
| 108 |
assert(message); |
| 109 |
|
| 110 |
r = sd_bus_message_read(message, "s", &name); |
| 111 |
if (r < 0) { |
| 112 |
bus_log_parse_error(r); |
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
if (manager_unit_inactive_or_pending(m, SPECIAL_DBUS_SOCKET) || |
| 117 |
manager_unit_inactive_or_pending(m, SPECIAL_DBUS_SERVICE)) { |
| 118 |
r = sd_bus_error_set(&error, BUS_ERROR_SHUTTING_DOWN, "Refusing activation, D-Bus is shutting down."); |
| 119 |
goto failed; |
| 120 |
} |
| 121 |
|
| 122 |
r = manager_load_unit(m, name, NULL, &error, &u); |
| 123 |
if (r < 0) |
| 124 |
goto failed; |
| 125 |
|
| 126 |
if (u->refuse_manual_start) { |
| 127 |
r = sd_bus_error_setf(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, %s may be requested by dependency only (it is configured to refuse manual start/stop).", u->id); |
| 128 |
goto failed; |
| 129 |
} |
| 130 |
|
| 131 |
r = manager_add_job(m, JOB_START, u, JOB_REPLACE, &error, /* ret= */ NULL); |
| 132 |
if (r < 0) |
| 133 |
goto failed; |
| 134 |
|
| 135 |
/* Successfully queued, that's it for us */ |
| 136 |
return 0; |
| 137 |
|
| 138 |
failed: |
| 139 |
if (!sd_bus_error_is_set(&error)) |
| 140 |
sd_bus_error_set_errno(&error, r); |
| 141 |
|
| 142 |
log_debug("D-Bus activation failed for %s: %s", name, bus_error_message(&error, r)); |
| 143 |
|
| 144 |
r = sd_bus_message_new_signal(sd_bus_message_get_bus(message), &reply, "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Activator", "ActivationFailure"); |
| 145 |
if (r < 0) { |
| 146 |
bus_log_create_error(r); |
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
r = sd_bus_message_append(reply, "sss", name, error.name, error.message); |
| 151 |
if (r < 0) { |
| 152 |
bus_log_create_error(r); |
| 153 |
return 0; |
| 154 |
} |
| 155 |
|
| 156 |
r = sd_bus_send_to(NULL, reply, "org.freedesktop.DBus", NULL); |
| 157 |
if (r < 0) |
| 158 |
return log_error_errno(r, "Failed to respond with to bus activation request: %m"); |
| 159 |
|
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
#if HAVE_SELINUX |
| 164 |
static int mac_selinux_filter(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) { |
| 165 |
Manager *m = userdata; |
| 166 |
const char *verb, *path; |
| 167 |
Unit *u = NULL; |
| 168 |
Job *j; |
| 169 |
int r; |
| 170 |
|
| 171 |
assert(message); |
| 172 |
|
| 173 |
/* Our own method calls are all protected individually with |
| 174 |
* selinux checks, but the built-in interfaces need to be |
| 175 |
* protected too. */ |
| 176 |
|
| 177 |
if (sd_bus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Set")) |
| 178 |
verb = "reload"; |
| 179 |
else if (sd_bus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", NULL) || |
| 180 |
sd_bus_message_is_method_call(message, "org.freedesktop.DBus.Properties", NULL) || |
| 181 |
sd_bus_message_is_method_call(message, "org.freedesktop.DBus.ObjectManager", NULL) || |
| 182 |
sd_bus_message_is_method_call(message, "org.freedesktop.DBus.Peer", NULL)) |
| 183 |
verb = "status"; |
| 184 |
else |
| 185 |
return 0; |
| 186 |
|
| 187 |
path = sd_bus_message_get_path(message); |
| 188 |
if (!path) |
| 189 |
return 0; |
| 190 |
|
| 191 |
if (object_path_startswith("/org/freedesktop/systemd1", path)) { |
| 192 |
r = mac_selinux_access_check(message, verb, reterr_error); |
| 193 |
if (r < 0) |
| 194 |
return r; |
| 195 |
|
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
if (streq(path, "/org/freedesktop/systemd1/unit/self")) { |
| 200 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 201 |
|
| 202 |
r = bus_query_sender_pidref(message, &pidref); |
| 203 |
if (r < 0) |
| 204 |
return 0; |
| 205 |
|
| 206 |
u = manager_get_unit_by_pidref(m, &pidref); |
| 207 |
} else { |
| 208 |
r = manager_get_job_from_dbus_path(m, path, &j); |
| 209 |
if (r >= 0) |
| 210 |
u = j->unit; |
| 211 |
else |
| 212 |
(void) manager_load_unit_from_dbus_path(m, path, NULL, &u); |
| 213 |
} |
| 214 |
if (!u) |
| 215 |
return 0; |
| 216 |
|
| 217 |
r = mac_selinux_unit_access_check(u, message, verb, reterr_error); |
| 218 |
if (r < 0) |
| 219 |
return r; |
| 220 |
|
| 221 |
return 0; |
| 222 |
} |
| 223 |
#endif |
| 224 |
|
| 225 |
static int find_unit(Manager *m, sd_bus *bus, const char *path, Unit **unit, sd_bus_error *reterr_error) { |
| 226 |
Unit *u = NULL; /* just to appease gcc, initialization is not really necessary */ |
| 227 |
int r; |
| 228 |
|
| 229 |
assert(m); |
| 230 |
assert(bus); |
| 231 |
assert(path); |
| 232 |
assert(unit); |
| 233 |
|
| 234 |
if (streq(path, "/org/freedesktop/systemd1/unit/self")) { |
| 235 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 236 |
sd_bus_message *message; |
| 237 |
|
| 238 |
message = sd_bus_get_current_message(bus); |
| 239 |
if (!message) |
| 240 |
return 0; |
| 241 |
|
| 242 |
r = bus_query_sender_pidref(message, &pidref); |
| 243 |
if (r < 0) |
| 244 |
return r; |
| 245 |
|
| 246 |
u = manager_get_unit_by_pidref(m, &pidref); |
| 247 |
if (!u) |
| 248 |
return 0; |
| 249 |
} else { |
| 250 |
r = manager_load_unit_from_dbus_path(m, path, reterr_error, &u); |
| 251 |
if (r < 0) |
| 252 |
return 0; |
| 253 |
assert(u); |
| 254 |
} |
| 255 |
|
| 256 |
*unit = u; |
| 257 |
return 1; |
| 258 |
} |
| 259 |
|
| 260 |
static int bus_unit_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *reterr_error) { |
| 261 |
Manager *m = ASSERT_PTR(userdata); |
| 262 |
|
| 263 |
assert(bus); |
| 264 |
assert(path); |
| 265 |
assert(interface); |
| 266 |
assert(found); |
| 267 |
|
| 268 |
return find_unit(m, bus, path, (Unit**) found, reterr_error); |
| 269 |
} |
| 270 |
|
| 271 |
static int bus_unit_interface_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *reterr_error) { |
| 272 |
Manager *m = ASSERT_PTR(userdata); |
| 273 |
Unit *u; |
| 274 |
int r; |
| 275 |
|
| 276 |
assert(bus); |
| 277 |
assert(path); |
| 278 |
assert(interface); |
| 279 |
assert(found); |
| 280 |
|
| 281 |
r = find_unit(m, bus, path, &u, reterr_error); |
| 282 |
if (r <= 0) |
| 283 |
return r; |
| 284 |
|
| 285 |
if (!streq_ptr(interface, unit_dbus_interface_from_type(u->type))) |
| 286 |
return 0; |
| 287 |
|
| 288 |
*found = u; |
| 289 |
return 1; |
| 290 |
} |
| 291 |
|
| 292 |
static int bus_unit_cgroup_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *reterr_error) { |
| 293 |
Manager *m = ASSERT_PTR(userdata); |
| 294 |
Unit *u; |
| 295 |
int r; |
| 296 |
|
| 297 |
assert(bus); |
| 298 |
assert(path); |
| 299 |
assert(interface); |
| 300 |
assert(found); |
| 301 |
|
| 302 |
r = find_unit(m, bus, path, &u, reterr_error); |
| 303 |
if (r <= 0) |
| 304 |
return r; |
| 305 |
|
| 306 |
if (!streq_ptr(interface, unit_dbus_interface_from_type(u->type))) |
| 307 |
return 0; |
| 308 |
|
| 309 |
if (!UNIT_HAS_CGROUP_CONTEXT(u)) |
| 310 |
return 0; |
| 311 |
|
| 312 |
*found = u; |
| 313 |
return 1; |
| 314 |
} |
| 315 |
|
| 316 |
static int bus_cgroup_context_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *reterr_error) { |
| 317 |
Manager *m = ASSERT_PTR(userdata); |
| 318 |
CGroupContext *c; |
| 319 |
Unit *u; |
| 320 |
int r; |
| 321 |
|
| 322 |
assert(bus); |
| 323 |
assert(path); |
| 324 |
assert(interface); |
| 325 |
assert(found); |
| 326 |
|
| 327 |
r = find_unit(m, bus, path, &u, reterr_error); |
| 328 |
if (r <= 0) |
| 329 |
return r; |
| 330 |
|
| 331 |
if (!streq_ptr(interface, unit_dbus_interface_from_type(u->type))) |
| 332 |
return 0; |
| 333 |
|
| 334 |
c = unit_get_cgroup_context(u); |
| 335 |
if (!c) |
| 336 |
return 0; |
| 337 |
|
| 338 |
*found = c; |
| 339 |
return 1; |
| 340 |
} |
| 341 |
|
| 342 |
static int bus_unit_exec_context_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *reterr_error) { |
| 343 |
Manager *m = ASSERT_PTR(userdata); |
| 344 |
Unit *u; |
| 345 |
int r; |
| 346 |
|
| 347 |
assert(bus); |
| 348 |
assert(path); |
| 349 |
assert(interface); |
| 350 |
assert(found); |
| 351 |
|
| 352 |
r = find_unit(m, bus, path, &u, reterr_error); |
| 353 |
if (r <= 0) |
| 354 |
return r; |
| 355 |
|
| 356 |
if (!streq_ptr(interface, unit_dbus_interface_from_type(u->type))) |
| 357 |
return 0; |
| 358 |
|
| 359 |
if (!UNIT_HAS_EXEC_CONTEXT(u)) |
| 360 |
return 0; |
| 361 |
|
| 362 |
*found = u; |
| 363 |
return 1; |
| 364 |
} |
| 365 |
|
| 366 |
static int bus_exec_context_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *reterr_error) { |
| 367 |
ExecContext *c; |
| 368 |
int r; |
| 369 |
|
| 370 |
assert(bus); |
| 371 |
assert(path); |
| 372 |
assert(interface); |
| 373 |
assert(found); |
| 374 |
|
| 375 |
Unit *u; |
| 376 |
r = bus_unit_exec_context_find(bus, path, interface, userdata, (void**) &u, reterr_error); |
| 377 |
if (r <= 0) |
| 378 |
return r; |
| 379 |
|
| 380 |
c = unit_get_exec_context(u); |
| 381 |
if (!c) |
| 382 |
return 0; |
| 383 |
|
| 384 |
*found = c; |
| 385 |
return 1; |
| 386 |
} |
| 387 |
|
| 388 |
static int bus_kill_context_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *reterr_error) { |
| 389 |
Manager *m = ASSERT_PTR(userdata); |
| 390 |
KillContext *c; |
| 391 |
Unit *u; |
| 392 |
int r; |
| 393 |
|
| 394 |
assert(bus); |
| 395 |
assert(path); |
| 396 |
assert(interface); |
| 397 |
assert(found); |
| 398 |
|
| 399 |
r = find_unit(m, bus, path, &u, reterr_error); |
| 400 |
if (r <= 0) |
| 401 |
return r; |
| 402 |
|
| 403 |
if (!streq_ptr(interface, unit_dbus_interface_from_type(u->type))) |
| 404 |
return 0; |
| 405 |
|
| 406 |
c = unit_get_kill_context(u); |
| 407 |
if (!c) |
| 408 |
return 0; |
| 409 |
|
| 410 |
*found = c; |
| 411 |
return 1; |
| 412 |
} |
| 413 |
|
| 414 |
static int bus_unit_enumerate(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *reterr_error) { |
| 415 |
_cleanup_strv_free_ char **l = NULL; |
| 416 |
Manager *m = userdata; |
| 417 |
unsigned k = 0; |
| 418 |
Unit *u; |
| 419 |
|
| 420 |
l = new0(char*, hashmap_size(m->units)+1); |
| 421 |
if (!l) |
| 422 |
return -ENOMEM; |
| 423 |
|
| 424 |
HASHMAP_FOREACH(u, m->units) { |
| 425 |
l[k] = unit_dbus_path(u); |
| 426 |
if (!l[k]) |
| 427 |
return -ENOMEM; |
| 428 |
|
| 429 |
k++; |
| 430 |
} |
| 431 |
|
| 432 |
*nodes = TAKE_PTR(l); |
| 433 |
|
| 434 |
return k; |
| 435 |
} |
| 436 |
|
| 437 |
static const BusObjectImplementation unit_object = { |
| 438 |
"/org/freedesktop/systemd1/unit", |
| 439 |
"org.freedesktop.systemd1.Unit", |
| 440 |
.fallback_vtables = BUS_FALLBACK_VTABLES( |
| 441 |
{ bus_unit_vtable, bus_unit_find }), |
| 442 |
.node_enumerator = bus_unit_enumerate, |
| 443 |
}; |
| 444 |
|
| 445 |
static const BusObjectImplementation bus_automount_object = { |
| 446 |
"/org/freedesktop/systemd1/unit", |
| 447 |
"org.freedesktop.systemd1.Automount", |
| 448 |
.fallback_vtables = BUS_FALLBACK_VTABLES( |
| 449 |
{ bus_automount_vtable, bus_unit_interface_find }), |
| 450 |
}; |
| 451 |
|
| 452 |
static const BusObjectImplementation bus_device_object = { |
| 453 |
"/org/freedesktop/systemd1/unit", |
| 454 |
"org.freedesktop.systemd1.Device", |
| 455 |
.fallback_vtables = BUS_FALLBACK_VTABLES( |
| 456 |
{ bus_device_vtable, bus_unit_interface_find }), |
| 457 |
}; |
| 458 |
|
| 459 |
static const BusObjectImplementation bus_mount_object = { |
| 460 |
"/org/freedesktop/systemd1/unit", |
| 461 |
"org.freedesktop.systemd1.Mount", |
| 462 |
.fallback_vtables = BUS_FALLBACK_VTABLES( |
| 463 |
{ bus_mount_vtable, bus_unit_interface_find }, |
| 464 |
{ bus_unit_cgroup_vtable, bus_unit_cgroup_find }, |
| 465 |
{ bus_cgroup_vtable, bus_cgroup_context_find }, |
| 466 |
{ bus_exec_vtable, bus_exec_context_find }, |
| 467 |
{ bus_unit_exec_vtable, bus_unit_exec_context_find }, |
| 468 |
{ bus_kill_vtable, bus_kill_context_find }), |
| 469 |
}; |
| 470 |
|
| 471 |
static const BusObjectImplementation bus_path_object = { |
| 472 |
"/org/freedesktop/systemd1/unit", |
| 473 |
"org.freedesktop.systemd1.Path", |
| 474 |
.fallback_vtables = BUS_FALLBACK_VTABLES( |
| 475 |
{ bus_path_vtable, bus_unit_interface_find }), |
| 476 |
}; |
| 477 |
|
| 478 |
static const BusObjectImplementation bus_scope_object = { |
| 479 |
"/org/freedesktop/systemd1/unit", |
| 480 |
"org.freedesktop.systemd1.Scope", |
| 481 |
.fallback_vtables = BUS_FALLBACK_VTABLES( |
| 482 |
{ bus_scope_vtable, bus_unit_interface_find }, |
| 483 |
{ bus_unit_cgroup_vtable, bus_unit_cgroup_find }, |
| 484 |
{ bus_cgroup_vtable, bus_cgroup_context_find }, |
| 485 |
{ bus_kill_vtable, bus_kill_context_find }), |
| 486 |
}; |
| 487 |
|
| 488 |
static const BusObjectImplementation bus_service_object = { |
| 489 |
"/org/freedesktop/systemd1/unit", |
| 490 |
"org.freedesktop.systemd1.Service", |
| 491 |
.fallback_vtables = BUS_FALLBACK_VTABLES( |
| 492 |
{ bus_service_vtable, bus_unit_interface_find }, |
| 493 |
{ bus_unit_cgroup_vtable, bus_unit_cgroup_find }, |
| 494 |
{ bus_cgroup_vtable, bus_cgroup_context_find }, |
| 495 |
{ bus_exec_vtable, bus_exec_context_find }, |
| 496 |
{ bus_unit_exec_vtable, bus_unit_exec_context_find }, |
| 497 |
{ bus_kill_vtable, bus_kill_context_find }), |
| 498 |
}; |
| 499 |
|
| 500 |
static const BusObjectImplementation bus_slice_object = { |
| 501 |
"/org/freedesktop/systemd1/unit", |
| 502 |
"org.freedesktop.systemd1.Slice", |
| 503 |
.fallback_vtables = BUS_FALLBACK_VTABLES( |
| 504 |
{ bus_slice_vtable, bus_unit_interface_find }, |
| 505 |
{ bus_unit_cgroup_vtable, bus_unit_cgroup_find }, |
| 506 |
{ bus_cgroup_vtable, bus_cgroup_context_find }), |
| 507 |
}; |
| 508 |
|
| 509 |
static const BusObjectImplementation bus_socket_object = { |
| 510 |
"/org/freedesktop/systemd1/unit", |
| 511 |
"org.freedesktop.systemd1.Socket", |
| 512 |
.fallback_vtables = BUS_FALLBACK_VTABLES( |
| 513 |
{ bus_socket_vtable, bus_unit_interface_find }, |
| 514 |
{ bus_unit_cgroup_vtable, bus_unit_cgroup_find }, |
| 515 |
{ bus_cgroup_vtable, bus_cgroup_context_find }, |
| 516 |
{ bus_exec_vtable, bus_exec_context_find }, |
| 517 |
{ bus_unit_exec_vtable, bus_unit_exec_context_find }, |
| 518 |
{ bus_kill_vtable, bus_kill_context_find }), |
| 519 |
}; |
| 520 |
|
| 521 |
static const BusObjectImplementation bus_swap_object = { |
| 522 |
"/org/freedesktop/systemd1/unit", |
| 523 |
"org.freedesktop.systemd1.Swap", |
| 524 |
.fallback_vtables = BUS_FALLBACK_VTABLES( |
| 525 |
{ bus_swap_vtable, bus_unit_interface_find }, |
| 526 |
{ bus_unit_cgroup_vtable, bus_unit_cgroup_find }, |
| 527 |
{ bus_cgroup_vtable, bus_cgroup_context_find }, |
| 528 |
{ bus_exec_vtable, bus_exec_context_find }, |
| 529 |
{ bus_unit_exec_vtable, bus_unit_exec_context_find }, |
| 530 |
{ bus_kill_vtable, bus_kill_context_find }), |
| 531 |
}; |
| 532 |
|
| 533 |
static const BusObjectImplementation bus_target_object = { |
| 534 |
"/org/freedesktop/systemd1/unit", |
| 535 |
"org.freedesktop.systemd1.Target", |
| 536 |
.fallback_vtables = BUS_FALLBACK_VTABLES( |
| 537 |
{ bus_target_vtable, bus_unit_interface_find }), |
| 538 |
}; |
| 539 |
|
| 540 |
static const BusObjectImplementation bus_timer_object = { |
| 541 |
"/org/freedesktop/systemd1/unit", |
| 542 |
"org.freedesktop.systemd1.Timer", |
| 543 |
.fallback_vtables = BUS_FALLBACK_VTABLES( |
| 544 |
{ bus_timer_vtable, bus_unit_interface_find }), |
| 545 |
}; |
| 546 |
|
| 547 |
static const BusObjectImplementation bus_manager_object = { |
| 548 |
"/org/freedesktop/systemd1", |
| 549 |
"org.freedesktop.systemd1.Manager", |
| 550 |
.vtables = BUS_VTABLES(bus_manager_vtable), |
| 551 |
.children = BUS_IMPLEMENTATIONS( |
| 552 |
&job_object, |
| 553 |
&unit_object, |
| 554 |
&bus_automount_object, |
| 555 |
&bus_device_object, |
| 556 |
&bus_mount_object, |
| 557 |
&bus_path_object, |
| 558 |
&bus_scope_object, |
| 559 |
&bus_service_object, |
| 560 |
&bus_slice_object, |
| 561 |
&bus_socket_object, |
| 562 |
&bus_swap_object, |
| 563 |
&bus_target_object, |
| 564 |
&bus_timer_object), |
| 565 |
}; |
| 566 |
|
| 567 |
static const BusObjectImplementation manager_log_control_object = { |
| 568 |
"/org/freedesktop/LogControl1", |
| 569 |
"org.freedesktop.LogControl1", |
| 570 |
.vtables = BUS_VTABLES(bus_manager_log_control_vtable), |
| 571 |
}; |
| 572 |
|
| 573 |
int bus_manager_introspect_implementations(FILE *out, const char *pattern) { |
| 574 |
return bus_introspect_implementations( |
| 575 |
out, |
| 576 |
pattern, |
| 577 |
BUS_IMPLEMENTATIONS(&bus_manager_object, |
| 578 |
&manager_log_control_object)); |
| 579 |
} |
| 580 |
|
| 581 |
static int bus_setup_api_vtables(Manager *m, sd_bus *bus) { |
| 582 |
int r; |
| 583 |
|
| 584 |
assert(m); |
| 585 |
assert(bus); |
| 586 |
|
| 587 |
#if HAVE_SELINUX |
| 588 |
r = sd_bus_add_filter(bus, NULL, mac_selinux_filter, m); |
| 589 |
if (r < 0) |
| 590 |
return log_error_errno(r, "Failed to add SELinux access filter: %m"); |
| 591 |
#endif |
| 592 |
|
| 593 |
r = bus_add_implementation(bus, &bus_manager_object, m); |
| 594 |
if (r < 0) |
| 595 |
return r; |
| 596 |
|
| 597 |
return bus_add_implementation(bus, &manager_log_control_object, m); |
| 598 |
} |
| 599 |
|
| 600 |
static int bus_setup_disconnected_match(Manager *m, sd_bus *bus) { |
| 601 |
int r; |
| 602 |
|
| 603 |
assert(m); |
| 604 |
assert(bus); |
| 605 |
|
| 606 |
r = sd_bus_match_signal_async( |
| 607 |
bus, |
| 608 |
NULL, |
| 609 |
"org.freedesktop.DBus.Local", |
| 610 |
"/org/freedesktop/DBus/Local", |
| 611 |
"org.freedesktop.DBus.Local", |
| 612 |
"Disconnected", |
| 613 |
signal_disconnected, NULL, m); |
| 614 |
if (r < 0) |
| 615 |
return log_error_errno(r, "Failed to request match for Disconnected message: %m"); |
| 616 |
|
| 617 |
return 0; |
| 618 |
} |
| 619 |
|
| 620 |
static int bus_on_connection(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 621 |
_cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL; |
| 622 |
_cleanup_close_ int nfd = -EBADF; |
| 623 |
Manager *m = ASSERT_PTR(userdata); |
| 624 |
sd_id128_t id; |
| 625 |
int r; |
| 626 |
|
| 627 |
assert(s); |
| 628 |
|
| 629 |
nfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC); |
| 630 |
if (nfd < 0) { |
| 631 |
if (ERRNO_IS_ACCEPT_AGAIN(errno)) |
| 632 |
return 0; |
| 633 |
|
| 634 |
log_warning_errno(errno, "Failed to accept private connection, ignoring: %m"); |
| 635 |
return 0; |
| 636 |
} |
| 637 |
|
| 638 |
if (set_size(m->private_buses) >= CONNECTIONS_MAX) { |
| 639 |
log_warning("Too many concurrent connections, refusing"); |
| 640 |
return 0; |
| 641 |
} |
| 642 |
|
| 643 |
r = sd_bus_new(&bus); |
| 644 |
if (r < 0) { |
| 645 |
log_warning_errno(r, "Failed to allocate new private connection bus: %m"); |
| 646 |
return 0; |
| 647 |
} |
| 648 |
|
| 649 |
(void) sd_bus_set_description(bus, "private-bus-connection"); |
| 650 |
|
| 651 |
r = sd_bus_set_fd(bus, nfd, nfd); |
| 652 |
if (r < 0) { |
| 653 |
log_warning_errno(r, "Failed to set fd on new connection bus: %m"); |
| 654 |
return 0; |
| 655 |
} |
| 656 |
|
| 657 |
TAKE_FD(nfd); |
| 658 |
|
| 659 |
r = bus_check_peercred(bus); |
| 660 |
if (r < 0) { |
| 661 |
log_warning_errno(r, "Incoming private connection from unprivileged client, refusing: %m"); |
| 662 |
return 0; |
| 663 |
} |
| 664 |
|
| 665 |
assert_se(sd_id128_randomize(&id) >= 0); |
| 666 |
|
| 667 |
r = sd_bus_set_server(bus, 1, id); |
| 668 |
if (r < 0) { |
| 669 |
log_warning_errno(r, "Failed to enable server support for new connection bus: %m"); |
| 670 |
return 0; |
| 671 |
} |
| 672 |
|
| 673 |
r = sd_bus_negotiate_creds(bus, 1, |
| 674 |
SD_BUS_CREDS_PID|SD_BUS_CREDS_UID| |
| 675 |
SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS| |
| 676 |
SD_BUS_CREDS_SELINUX_CONTEXT| |
| 677 |
SD_BUS_CREDS_COMM|SD_BUS_CREDS_DESCRIPTION); |
| 678 |
if (r < 0) { |
| 679 |
log_warning_errno(r, "Failed to enable credentials for new connection: %m"); |
| 680 |
return 0; |
| 681 |
} |
| 682 |
|
| 683 |
r = sd_bus_set_sender(bus, "org.freedesktop.systemd1"); |
| 684 |
if (r < 0) { |
| 685 |
log_warning_errno(r, "Failed to set direct connection sender: %m"); |
| 686 |
return 0; |
| 687 |
} |
| 688 |
|
| 689 |
r = sd_bus_start(bus); |
| 690 |
if (r < 0) { |
| 691 |
log_warning_errno(r, "Failed to start new connection bus: %m"); |
| 692 |
return 0; |
| 693 |
} |
| 694 |
|
| 695 |
if (DEBUG_LOGGING) { |
| 696 |
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *c = NULL; |
| 697 |
const char *comm = NULL, *description = NULL; |
| 698 |
pid_t pid = 0; |
| 699 |
|
| 700 |
r = sd_bus_get_owner_creds(bus, SD_BUS_CREDS_PID|SD_BUS_CREDS_COMM|SD_BUS_CREDS_DESCRIPTION, &c); |
| 701 |
if (r < 0) |
| 702 |
log_warning_errno(r, "Failed to get peer creds, ignoring: %m"); |
| 703 |
else { |
| 704 |
(void) sd_bus_creds_get_pid(c, &pid); |
| 705 |
(void) sd_bus_creds_get_comm(c, &comm); |
| 706 |
(void) sd_bus_creds_get_description(c, &description); |
| 707 |
} |
| 708 |
|
| 709 |
log_debug("Accepting direct incoming connection from " PID_FMT " (%s) [%s]", pid, strna(comm), strna(description)); |
| 710 |
} |
| 711 |
|
| 712 |
r = sd_bus_attach_event(bus, m->event, EVENT_PRIORITY_IPC); |
| 713 |
if (r < 0) { |
| 714 |
log_warning_errno(r, "Failed to attach new connection bus to event loop: %m"); |
| 715 |
return 0; |
| 716 |
} |
| 717 |
|
| 718 |
r = bus_setup_disconnected_match(m, bus); |
| 719 |
if (r < 0) |
| 720 |
return 0; |
| 721 |
|
| 722 |
r = bus_setup_api_vtables(m, bus); |
| 723 |
if (r < 0) { |
| 724 |
log_warning_errno(r, "Failed to set up API vtables on new connection bus: %m"); |
| 725 |
return 0; |
| 726 |
} |
| 727 |
|
| 728 |
r = bus_register_malloc_status(bus, "org.freedesktop.systemd1"); |
| 729 |
if (r < 0) |
| 730 |
log_warning_errno(r, "Failed to register MemoryAllocation1, ignoring: %m"); |
| 731 |
|
| 732 |
r = set_ensure_put(&m->private_buses, NULL, bus); |
| 733 |
if (r == -ENOMEM) { |
| 734 |
log_oom(); |
| 735 |
return 0; |
| 736 |
} |
| 737 |
if (r < 0) { |
| 738 |
log_warning_errno(r, "Failed to add new connection bus to set: %m"); |
| 739 |
return 0; |
| 740 |
} |
| 741 |
|
| 742 |
TAKE_PTR(bus); |
| 743 |
|
| 744 |
log_debug("Accepted new private connection."); |
| 745 |
|
| 746 |
return 0; |
| 747 |
} |
| 748 |
|
| 749 |
static int bus_track_coldplug(sd_bus *bus, sd_bus_track **t, char * const *l) { |
| 750 |
int r; |
| 751 |
|
| 752 |
assert(bus); |
| 753 |
assert(t); |
| 754 |
|
| 755 |
if (strv_isempty(l)) |
| 756 |
return 0; |
| 757 |
|
| 758 |
if (!*t) { |
| 759 |
r = sd_bus_track_new(bus, t, NULL, NULL); |
| 760 |
if (r < 0) |
| 761 |
return r; |
| 762 |
} |
| 763 |
|
| 764 |
return bus_track_add_name_many(*t, l); |
| 765 |
} |
| 766 |
|
| 767 |
static int bus_setup_api(Manager *m, sd_bus *bus) { |
| 768 |
char *name; |
| 769 |
Unit *u; |
| 770 |
int r; |
| 771 |
|
| 772 |
assert(m); |
| 773 |
assert(bus); |
| 774 |
|
| 775 |
/* Let's make sure we have enough credential bits so that we can make security and selinux decisions */ |
| 776 |
r = sd_bus_negotiate_creds(bus, 1, |
| 777 |
SD_BUS_CREDS_PID|SD_BUS_CREDS_UID| |
| 778 |
SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS| |
| 779 |
SD_BUS_CREDS_SELINUX_CONTEXT); |
| 780 |
if (r < 0) |
| 781 |
log_warning_errno(r, "Failed to enable credential passing, ignoring: %m"); |
| 782 |
|
| 783 |
r = bus_setup_api_vtables(m, bus); |
| 784 |
if (r < 0) |
| 785 |
return r; |
| 786 |
|
| 787 |
HASHMAP_FOREACH_KEY(u, name, m->watch_bus) { |
| 788 |
r = unit_install_bus_match(u, bus, name); |
| 789 |
if (r < 0) |
| 790 |
log_error_errno(r, "Failed to subscribe to NameOwnerChanged signal for '%s': %m", name); |
| 791 |
} |
| 792 |
|
| 793 |
r = sd_bus_match_signal_async( |
| 794 |
bus, |
| 795 |
NULL, |
| 796 |
"org.freedesktop.DBus", |
| 797 |
"/org/freedesktop/DBus", |
| 798 |
"org.freedesktop.systemd1.Activator", |
| 799 |
"ActivationRequest", |
| 800 |
signal_activation_request, NULL, m); |
| 801 |
if (r < 0) |
| 802 |
log_warning_errno(r, "Failed to subscribe to activation signal: %m"); |
| 803 |
|
| 804 |
/* Allow replacing of our name, to ease implementation of reexecution, where we keep the old connection open |
| 805 |
* until after the new connection is set up and the name installed to allow clients to synchronously wait for |
| 806 |
* reexecution to finish */ |
| 807 |
r = sd_bus_request_name_async(bus, NULL, "org.freedesktop.systemd1", SD_BUS_NAME_REPLACE_EXISTING|SD_BUS_NAME_ALLOW_REPLACEMENT, NULL, NULL); |
| 808 |
if (r < 0) |
| 809 |
return log_error_errno(r, "Failed to request name: %m"); |
| 810 |
|
| 811 |
r = bus_register_malloc_status(bus, "org.freedesktop.systemd1"); |
| 812 |
if (r < 0) |
| 813 |
log_warning_errno(r, "Failed to register MemoryAllocation1, ignoring: %m"); |
| 814 |
|
| 815 |
log_debug("Successfully connected to API bus."); |
| 816 |
|
| 817 |
return 0; |
| 818 |
} |
| 819 |
|
| 820 |
int bus_init_api(Manager *m) { |
| 821 |
_cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL; |
| 822 |
int r; |
| 823 |
|
| 824 |
assert(m); |
| 825 |
|
| 826 |
if (m->api_bus) |
| 827 |
return 0; |
| 828 |
|
| 829 |
/* The API and system bus is the same if we are running in system mode */ |
| 830 |
if (MANAGER_IS_SYSTEM(m) && m->system_bus) |
| 831 |
bus = sd_bus_ref(m->system_bus); |
| 832 |
else { |
| 833 |
if (MANAGER_IS_SYSTEM(m)) |
| 834 |
r = sd_bus_open_system_with_description(&bus, "bus-api-system"); |
| 835 |
else |
| 836 |
r = sd_bus_open_user_with_description(&bus, "bus-api-user"); |
| 837 |
if (r < 0) |
| 838 |
return log_error_errno(r, "Failed to connect to API bus: %m"); |
| 839 |
|
| 840 |
r = sd_bus_attach_event(bus, m->event, EVENT_PRIORITY_IPC); |
| 841 |
if (r < 0) |
| 842 |
return log_error_errno(r, "Failed to attach API bus to event loop: %m"); |
| 843 |
|
| 844 |
r = bus_setup_disconnected_match(m, bus); |
| 845 |
if (r < 0) |
| 846 |
return r; |
| 847 |
} |
| 848 |
|
| 849 |
r = bus_setup_api(m, bus); |
| 850 |
if (r < 0) |
| 851 |
return log_error_errno(r, "Failed to set up API bus: %m"); |
| 852 |
|
| 853 |
r = bus_get_instance_id(bus, &m->bus_id); |
| 854 |
if (r < 0) |
| 855 |
log_warning_errno(r, "Failed to query API bus instance ID, not deserializing subscriptions: %m"); |
| 856 |
else if (sd_id128_is_null(m->deserialized_bus_id) || sd_id128_equal(m->bus_id, m->deserialized_bus_id)) |
| 857 |
(void) bus_track_coldplug(bus, &m->subscribed, m->subscribed_as_strv); |
| 858 |
m->subscribed_as_strv = strv_free(m->subscribed_as_strv); |
| 859 |
m->deserialized_bus_id = SD_ID128_NULL; |
| 860 |
|
| 861 |
m->api_bus = TAKE_PTR(bus); |
| 862 |
return 0; |
| 863 |
} |
| 864 |
|
| 865 |
int bus_init_system(Manager *m) { |
| 866 |
_cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL; |
| 867 |
int r; |
| 868 |
|
| 869 |
if (m->system_bus) |
| 870 |
return 0; |
| 871 |
|
| 872 |
/* The API and system bus is the same if we are running in system mode */ |
| 873 |
if (MANAGER_IS_SYSTEM(m) && m->api_bus) |
| 874 |
bus = sd_bus_ref(m->api_bus); |
| 875 |
else { |
| 876 |
r = sd_bus_open_system_with_description(&bus, "bus-system"); |
| 877 |
if (r < 0) |
| 878 |
return log_error_errno(r, "Failed to connect to system bus: %m"); |
| 879 |
|
| 880 |
r = sd_bus_attach_event(bus, m->event, EVENT_PRIORITY_IPC); |
| 881 |
if (r < 0) |
| 882 |
return log_error_errno(r, "Failed to attach system bus to event loop: %m"); |
| 883 |
|
| 884 |
r = bus_setup_disconnected_match(m, bus); |
| 885 |
if (r < 0) |
| 886 |
return r; |
| 887 |
} |
| 888 |
|
| 889 |
m->system_bus = TAKE_PTR(bus); |
| 890 |
|
| 891 |
log_debug("Successfully connected to system bus."); |
| 892 |
|
| 893 |
return 0; |
| 894 |
} |
| 895 |
|
| 896 |
int bus_init_private(Manager *m) { |
| 897 |
_cleanup_close_ int fd = -EBADF; |
| 898 |
union sockaddr_union sa; |
| 899 |
socklen_t sa_len; |
| 900 |
sd_event_source *s; |
| 901 |
int r; |
| 902 |
|
| 903 |
assert(m); |
| 904 |
|
| 905 |
if (m->private_listen_fd >= 0) |
| 906 |
return 0; |
| 907 |
|
| 908 |
if (MANAGER_IS_SYSTEM(m)) { |
| 909 |
|
| 910 |
/* We want the private bus only when running as init */ |
| 911 |
if (getpid_cached() != 1) |
| 912 |
return 0; |
| 913 |
|
| 914 |
r = sockaddr_un_set_path(&sa.un, "/run/systemd/private"); |
| 915 |
} else { |
| 916 |
_cleanup_free_ char *p = NULL; |
| 917 |
|
| 918 |
p = path_join(m->prefix[EXEC_DIRECTORY_RUNTIME], "systemd/private"); |
| 919 |
if (!p) |
| 920 |
return log_oom(); |
| 921 |
|
| 922 |
r = sockaddr_un_set_path(&sa.un, p); |
| 923 |
} |
| 924 |
if (r < 0) |
| 925 |
return log_error_errno(r, "Failed to set socket path for private bus: %m"); |
| 926 |
sa_len = r; |
| 927 |
|
| 928 |
(void) sockaddr_un_unlink(&sa.un); |
| 929 |
|
| 930 |
fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
| 931 |
if (fd < 0) |
| 932 |
return log_error_errno(errno, "Failed to allocate private socket: %m"); |
| 933 |
|
| 934 |
WITH_UMASK(0077) |
| 935 |
r = bind(fd, &sa.sa, sa_len); |
| 936 |
if (r < 0) |
| 937 |
return log_error_errno(errno, "Failed to bind private socket: %m"); |
| 938 |
|
| 939 |
r = listen(fd, SOMAXCONN_DELUXE); |
| 940 |
if (r < 0) |
| 941 |
return log_error_errno(errno, "Failed to make private socket listening: %m"); |
| 942 |
|
| 943 |
/* Generate an inotify event in case somebody waits for this socket to appear using inotify() */ |
| 944 |
(void) touch(sa.un.sun_path); |
| 945 |
|
| 946 |
r = sd_event_add_io(m->event, &s, fd, EPOLLIN, bus_on_connection, m); |
| 947 |
if (r < 0) |
| 948 |
return log_error_errno(r, "Failed to allocate event source: %m"); |
| 949 |
|
| 950 |
(void) sd_event_source_set_description(s, "bus-connection"); |
| 951 |
|
| 952 |
m->private_listen_fd = TAKE_FD(fd); |
| 953 |
m->private_listen_event_source = s; |
| 954 |
|
| 955 |
log_debug("Successfully created private D-Bus server."); |
| 956 |
|
| 957 |
return 0; |
| 958 |
} |
| 959 |
|
| 960 |
static void destroy_bus(Manager *m, sd_bus **bus) { |
| 961 |
Unit *u; |
| 962 |
Job *j; |
| 963 |
|
| 964 |
assert(m); |
| 965 |
assert(bus); |
| 966 |
|
| 967 |
if (!*bus) |
| 968 |
return; |
| 969 |
|
| 970 |
/* Make sure all bus slots watching names are released. */ |
| 971 |
HASHMAP_FOREACH(u, m->watch_bus) { |
| 972 |
if (u->match_bus_slot && sd_bus_slot_get_bus(u->match_bus_slot) == *bus) |
| 973 |
u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot); |
| 974 |
if (u->get_name_owner_slot && sd_bus_slot_get_bus(u->get_name_owner_slot) == *bus) |
| 975 |
u->get_name_owner_slot = sd_bus_slot_unref(u->get_name_owner_slot); |
| 976 |
} |
| 977 |
|
| 978 |
/* Get rid of tracked clients on this bus */ |
| 979 |
if (m->subscribed && sd_bus_track_get_bus(m->subscribed) == *bus) { |
| 980 |
_cleanup_strv_free_ char **subscribed = NULL; |
| 981 |
int r; |
| 982 |
|
| 983 |
r = bus_track_to_strv(m->subscribed, &subscribed); |
| 984 |
if (r < 0) |
| 985 |
log_warning_errno(r, "Failed to serialize api subscribers, ignoring: %m"); |
| 986 |
strv_free_and_replace(m->subscribed_as_strv, subscribed); |
| 987 |
|
| 988 |
m->deserialized_bus_id = m->bus_id; |
| 989 |
m->bus_id = SD_ID128_NULL; |
| 990 |
|
| 991 |
m->subscribed = sd_bus_track_unref(m->subscribed); |
| 992 |
} |
| 993 |
|
| 994 |
HASHMAP_FOREACH(j, m->jobs) |
| 995 |
if (j->bus_track && sd_bus_track_get_bus(j->bus_track) == *bus) |
| 996 |
j->bus_track = sd_bus_track_unref(j->bus_track); |
| 997 |
|
| 998 |
HASHMAP_FOREACH(u, m->units) { |
| 999 |
if (u->bus_track && sd_bus_track_get_bus(u->bus_track) == *bus) |
| 1000 |
u->bus_track = sd_bus_track_unref(u->bus_track); |
| 1001 |
|
| 1002 |
/* Get rid of pending freezer messages on this bus */ |
| 1003 |
if (u->pending_freezer_invocation && sd_bus_message_get_bus(u->pending_freezer_invocation) == *bus) |
| 1004 |
u->pending_freezer_invocation = sd_bus_message_unref(u->pending_freezer_invocation); |
| 1005 |
} |
| 1006 |
|
| 1007 |
/* Get rid of queued message on this bus */ |
| 1008 |
if (m->pending_reload_message_dbus && sd_bus_message_get_bus(m->pending_reload_message_dbus) == *bus) |
| 1009 |
m->pending_reload_message_dbus = sd_bus_message_unref(m->pending_reload_message_dbus); |
| 1010 |
|
| 1011 |
/* Possibly flush unwritten data, but only if we are |
| 1012 |
* unprivileged, since we don't want to sync here */ |
| 1013 |
if (!MANAGER_IS_SYSTEM(m)) |
| 1014 |
sd_bus_flush(*bus); |
| 1015 |
|
| 1016 |
/* And destroy the object */ |
| 1017 |
*bus = sd_bus_close_unref(*bus); |
| 1018 |
} |
| 1019 |
|
| 1020 |
void bus_done_api(Manager *m) { |
| 1021 |
destroy_bus(m, &m->api_bus); |
| 1022 |
} |
| 1023 |
|
| 1024 |
void bus_done_system(Manager *m) { |
| 1025 |
destroy_bus(m, &m->system_bus); |
| 1026 |
} |
| 1027 |
|
| 1028 |
void bus_done_private(Manager *m) { |
| 1029 |
sd_bus *b; |
| 1030 |
|
| 1031 |
assert(m); |
| 1032 |
|
| 1033 |
while ((b = set_steal_first(m->private_buses))) |
| 1034 |
destroy_bus(m, &b); |
| 1035 |
|
| 1036 |
m->private_buses = set_free(m->private_buses); |
| 1037 |
|
| 1038 |
m->private_listen_event_source = sd_event_source_disable_unref(m->private_listen_event_source); |
| 1039 |
m->private_listen_fd = safe_close(m->private_listen_fd); |
| 1040 |
} |
| 1041 |
|
| 1042 |
void bus_done(Manager *m) { |
| 1043 |
assert(m); |
| 1044 |
|
| 1045 |
bus_done_api(m); |
| 1046 |
bus_done_system(m); |
| 1047 |
bus_done_private(m); |
| 1048 |
|
| 1049 |
assert(!m->subscribed); |
| 1050 |
|
| 1051 |
m->polkit_registry = hashmap_free(m->polkit_registry); |
| 1052 |
} |
| 1053 |
|
| 1054 |
int bus_fdset_add_all(Manager *m, FDSet *fds) { |
| 1055 |
sd_bus *b; |
| 1056 |
int fd; |
| 1057 |
|
| 1058 |
assert(m); |
| 1059 |
assert(fds); |
| 1060 |
|
| 1061 |
/* When we are about to reexecute we add all D-Bus fds to the |
| 1062 |
* set to pass over to the newly executed systemd. They won't |
| 1063 |
* be used there however, except that they are closed at the |
| 1064 |
* very end of deserialization, those making it possible for |
| 1065 |
* clients to synchronously wait for systemd to reexec by |
| 1066 |
* simply waiting for disconnection */ |
| 1067 |
|
| 1068 |
if (m->api_bus) { |
| 1069 |
fd = sd_bus_get_fd(m->api_bus); |
| 1070 |
if (fd >= 0) { |
| 1071 |
fd = fdset_put_dup(fds, fd); |
| 1072 |
if (fd < 0) |
| 1073 |
return fd; |
| 1074 |
} |
| 1075 |
} |
| 1076 |
|
| 1077 |
SET_FOREACH(b, m->private_buses) { |
| 1078 |
fd = sd_bus_get_fd(b); |
| 1079 |
if (fd >= 0) { |
| 1080 |
fd = fdset_put_dup(fds, fd); |
| 1081 |
if (fd < 0) |
| 1082 |
return fd; |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
/* We don't offer any APIs on the system bus (well, unless it |
| 1087 |
* is the same as the API bus) hence we don't bother with it |
| 1088 |
* here */ |
| 1089 |
|
| 1090 |
return 0; |
| 1091 |
} |
| 1092 |
|
| 1093 |
int bus_foreach_bus( |
| 1094 |
Manager *m, |
| 1095 |
sd_bus_track *subscribed2, |
| 1096 |
int (*send_message)(sd_bus *bus, void *userdata), |
| 1097 |
void *userdata) { |
| 1098 |
|
| 1099 |
int r = 0; |
| 1100 |
|
| 1101 |
assert(m); |
| 1102 |
assert(send_message); |
| 1103 |
|
| 1104 |
/* Send to all direct buses, unconditionally */ |
| 1105 |
sd_bus *b; |
| 1106 |
SET_FOREACH(b, m->private_buses) { |
| 1107 |
|
| 1108 |
/* Don't bother with enqueuing these messages to clients that haven't started yet */ |
| 1109 |
if (sd_bus_is_ready(b) <= 0) |
| 1110 |
continue; |
| 1111 |
|
| 1112 |
RET_GATHER(r, send_message(b, userdata)); |
| 1113 |
} |
| 1114 |
|
| 1115 |
/* Send to API bus, but only if somebody is subscribed */ |
| 1116 |
if (m->api_bus && |
| 1117 |
(sd_bus_track_count(m->subscribed) > 0 || |
| 1118 |
sd_bus_track_count(subscribed2) > 0)) |
| 1119 |
RET_GATHER(r, send_message(m->api_bus, userdata)); |
| 1120 |
|
| 1121 |
return r; |
| 1122 |
} |
| 1123 |
|
| 1124 |
void bus_track_serialize(sd_bus_track *t, FILE *f, const char *prefix) { |
| 1125 |
const char *n; |
| 1126 |
|
| 1127 |
assert(f); |
| 1128 |
assert(prefix); |
| 1129 |
|
| 1130 |
for (n = sd_bus_track_first(t); n; n = sd_bus_track_next(t)) { |
| 1131 |
int c, j; |
| 1132 |
|
| 1133 |
c = sd_bus_track_count_name(t, n); |
| 1134 |
for (j = 0; j < c; j++) |
| 1135 |
(void) serialize_item(f, prefix, n); |
| 1136 |
} |
| 1137 |
} |
| 1138 |
|
| 1139 |
uint64_t manager_bus_n_queued_write(Manager *m) { |
| 1140 |
uint64_t c = 0; |
| 1141 |
sd_bus *b; |
| 1142 |
int r; |
| 1143 |
|
| 1144 |
/* Returns the total number of messages queued for writing on all our direct and API buses. */ |
| 1145 |
|
| 1146 |
SET_FOREACH(b, m->private_buses) { |
| 1147 |
uint64_t k; |
| 1148 |
|
| 1149 |
r = sd_bus_get_n_queued_write(b, &k); |
| 1150 |
if (r < 0) |
| 1151 |
log_debug_errno(r, "Failed to query queued messages for private bus: %m"); |
| 1152 |
else |
| 1153 |
c += k; |
| 1154 |
} |
| 1155 |
|
| 1156 |
if (m->api_bus) { |
| 1157 |
uint64_t k; |
| 1158 |
|
| 1159 |
r = sd_bus_get_n_queued_write(m->api_bus, &k); |
| 1160 |
if (r < 0) |
| 1161 |
log_debug_errno(r, "Failed to query queued messages for API bus: %m"); |
| 1162 |
else |
| 1163 |
c += k; |
| 1164 |
} |
| 1165 |
|
| 1166 |
return c; |
| 1167 |
} |
| 1168 |
|
| 1169 |
static void vtable_dump_bus_properties(FILE *f, const sd_bus_vtable *table) { |
| 1170 |
const sd_bus_vtable *i; |
| 1171 |
|
| 1172 |
for (i = table; i->type != _SD_BUS_VTABLE_END; i++) { |
| 1173 |
if (!IN_SET(i->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY) || |
| 1174 |
(i->flags & (SD_BUS_VTABLE_DEPRECATED | SD_BUS_VTABLE_HIDDEN)) != 0) |
| 1175 |
continue; |
| 1176 |
|
| 1177 |
fprintf(f, "%s\n", i->x.property.member); |
| 1178 |
} |
| 1179 |
} |
| 1180 |
|
| 1181 |
void dump_bus_properties(FILE *f) { |
| 1182 |
assert(f); |
| 1183 |
|
| 1184 |
vtable_dump_bus_properties(f, bus_automount_vtable); |
| 1185 |
vtable_dump_bus_properties(f, bus_cgroup_vtable); |
| 1186 |
vtable_dump_bus_properties(f, bus_device_vtable); |
| 1187 |
vtable_dump_bus_properties(f, bus_exec_vtable); |
| 1188 |
vtable_dump_bus_properties(f, bus_unit_exec_vtable); |
| 1189 |
vtable_dump_bus_properties(f, bus_job_vtable); |
| 1190 |
vtable_dump_bus_properties(f, bus_kill_vtable); |
| 1191 |
vtable_dump_bus_properties(f, bus_manager_vtable); |
| 1192 |
vtable_dump_bus_properties(f, bus_mount_vtable); |
| 1193 |
vtable_dump_bus_properties(f, bus_path_vtable); |
| 1194 |
vtable_dump_bus_properties(f, bus_scope_vtable); |
| 1195 |
vtable_dump_bus_properties(f, bus_service_vtable); |
| 1196 |
vtable_dump_bus_properties(f, bus_slice_vtable); |
| 1197 |
vtable_dump_bus_properties(f, bus_socket_vtable); |
| 1198 |
vtable_dump_bus_properties(f, bus_swap_vtable); |
| 1199 |
vtable_dump_bus_properties(f, bus_target_vtable); |
| 1200 |
vtable_dump_bus_properties(f, bus_timer_vtable); |
| 1201 |
vtable_dump_bus_properties(f, bus_unit_vtable); |
| 1202 |
vtable_dump_bus_properties(f, bus_unit_cgroup_vtable); |
| 1203 |
} |