| 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-locator.h" |
| 11 |
#include "bus-message-util.h" |
| 12 |
#include "bus-object.h" |
| 13 |
#include "bus-polkit.h" |
| 14 |
#include "bus-util.h" |
| 15 |
#include "cgroup-util.h" |
| 16 |
#include "discover-image.h" |
| 17 |
#include "errno-util.h" |
| 18 |
#include "fd-util.h" |
| 19 |
#include "fileio.h" |
| 20 |
#include "format-util.h" |
| 21 |
#include "hashmap.h" |
| 22 |
#include "hostname-util.h" |
| 23 |
#include "image.h" |
| 24 |
#include "image-dbus.h" |
| 25 |
#include "io-util.h" |
| 26 |
#include "machine.h" |
| 27 |
#include "machine-dbus.h" |
| 28 |
#include "machined.h" |
| 29 |
#include "machined-dbus.h" |
| 30 |
#include "namespace-util.h" |
| 31 |
#include "operation.h" |
| 32 |
#include "os-util.h" |
| 33 |
#include "path-util.h" |
| 34 |
#include "socket-util.h" |
| 35 |
#include "string-util.h" |
| 36 |
#include "strv.h" |
| 37 |
#include "unit-def.h" |
| 38 |
#include "user-util.h" |
| 39 |
|
| 40 |
static int property_get_pool_path( |
| 41 |
sd_bus *bus, |
| 42 |
const char *path, |
| 43 |
const char *interface, |
| 44 |
const char *property, |
| 45 |
sd_bus_message *reply, |
| 46 |
void *userdata, |
| 47 |
sd_bus_error *error) { |
| 48 |
|
| 49 |
_cleanup_free_ char *poolpath = NULL; |
| 50 |
Manager *m = ASSERT_PTR(userdata); |
| 51 |
|
| 52 |
assert(bus); |
| 53 |
assert(reply); |
| 54 |
|
| 55 |
(void) image_get_pool_path(m->runtime_scope, IMAGE_MACHINE, &poolpath); |
| 56 |
|
| 57 |
return sd_bus_message_append(reply, "s", strempty(poolpath)); |
| 58 |
} |
| 59 |
|
| 60 |
static int property_get_pool_usage( |
| 61 |
sd_bus *bus, |
| 62 |
const char *path, |
| 63 |
const char *interface, |
| 64 |
const char *property, |
| 65 |
sd_bus_message *reply, |
| 66 |
void *userdata, |
| 67 |
sd_bus_error *error) { |
| 68 |
|
| 69 |
Manager *m = ASSERT_PTR(userdata); |
| 70 |
uint64_t usage = UINT64_MAX; |
| 71 |
|
| 72 |
assert(bus); |
| 73 |
assert(reply); |
| 74 |
|
| 75 |
(void) image_get_pool_usage(m->runtime_scope, IMAGE_MACHINE, &usage); |
| 76 |
|
| 77 |
return sd_bus_message_append(reply, "t", usage); |
| 78 |
} |
| 79 |
|
| 80 |
static int property_get_pool_limit( |
| 81 |
sd_bus *bus, |
| 82 |
const char *path, |
| 83 |
const char *interface, |
| 84 |
const char *property, |
| 85 |
sd_bus_message *reply, |
| 86 |
void *userdata, |
| 87 |
sd_bus_error *error) { |
| 88 |
|
| 89 |
Manager *m = ASSERT_PTR(userdata); |
| 90 |
uint64_t size = UINT64_MAX; |
| 91 |
|
| 92 |
assert(bus); |
| 93 |
assert(reply); |
| 94 |
|
| 95 |
(void) image_get_pool_limit(m->runtime_scope, IMAGE_MACHINE, &size); |
| 96 |
|
| 97 |
return sd_bus_message_append(reply, "t", size); |
| 98 |
} |
| 99 |
|
| 100 |
static int method_get_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 101 |
_cleanup_free_ char *p = NULL; |
| 102 |
Manager *m = ASSERT_PTR(userdata); |
| 103 |
Machine *machine; |
| 104 |
const char *name; |
| 105 |
int r; |
| 106 |
|
| 107 |
assert(message); |
| 108 |
|
| 109 |
r = sd_bus_message_read(message, "s", &name); |
| 110 |
if (r < 0) |
| 111 |
return r; |
| 112 |
|
| 113 |
machine = hashmap_get(m->machines, name); |
| 114 |
if (!machine) |
| 115 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); |
| 116 |
|
| 117 |
p = machine_bus_path(machine); |
| 118 |
if (!p) |
| 119 |
return -ENOMEM; |
| 120 |
|
| 121 |
return sd_bus_reply_method_return(message, "o", p); |
| 122 |
} |
| 123 |
|
| 124 |
static int method_get_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 125 |
_cleanup_free_ char *p = NULL; |
| 126 |
_unused_ Manager *m = ASSERT_PTR(userdata); |
| 127 |
const char *name; |
| 128 |
int r; |
| 129 |
|
| 130 |
assert(message); |
| 131 |
|
| 132 |
r = sd_bus_message_read(message, "s", &name); |
| 133 |
if (r < 0) |
| 134 |
return r; |
| 135 |
|
| 136 |
r = image_find(m->runtime_scope, IMAGE_MACHINE, name, NULL, NULL); |
| 137 |
if (r == -ENOENT) |
| 138 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name); |
| 139 |
if (r < 0) |
| 140 |
return r; |
| 141 |
|
| 142 |
p = image_bus_path(name); |
| 143 |
if (!p) |
| 144 |
return -ENOMEM; |
| 145 |
|
| 146 |
return sd_bus_reply_method_return(message, "o", p); |
| 147 |
} |
| 148 |
|
| 149 |
static int method_get_machine_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 150 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 151 |
_cleanup_free_ char *p = NULL; |
| 152 |
Manager *m = ASSERT_PTR(userdata); |
| 153 |
Machine *machine = NULL; |
| 154 |
pid_t pid; |
| 155 |
int r; |
| 156 |
|
| 157 |
assert(message); |
| 158 |
|
| 159 |
assert_cc(sizeof(pid_t) == sizeof(uint32_t)); |
| 160 |
|
| 161 |
r = sd_bus_message_read(message, "u", &pid); |
| 162 |
if (r < 0) |
| 163 |
return r; |
| 164 |
|
| 165 |
if (pid < 0) |
| 166 |
return -EINVAL; |
| 167 |
|
| 168 |
pidref = PIDREF_MAKE_FROM_PID(pid); |
| 169 |
|
| 170 |
if (pid == 0) { |
| 171 |
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
| 172 |
|
| 173 |
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID|SD_BUS_CREDS_PIDFD, &creds); |
| 174 |
if (r < 0) |
| 175 |
return r; |
| 176 |
|
| 177 |
r = bus_creds_get_pidref(creds, &pidref); |
| 178 |
if (r < 0) |
| 179 |
return r; |
| 180 |
} |
| 181 |
|
| 182 |
r = manager_get_machine_by_pidref(m, &pidref, &machine); |
| 183 |
if (r < 0) |
| 184 |
return r; |
| 185 |
if (r == 0) |
| 186 |
return sd_bus_error_setf(error, BUS_ERROR_NO_MACHINE_FOR_PID, "PID "PID_FMT" does not belong to any known machine", pid); |
| 187 |
|
| 188 |
p = machine_bus_path(machine); |
| 189 |
if (!p) |
| 190 |
return -ENOMEM; |
| 191 |
|
| 192 |
return sd_bus_reply_method_return(message, "o", p); |
| 193 |
} |
| 194 |
|
| 195 |
static int method_list_machines(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 196 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 197 |
Manager *m = ASSERT_PTR(userdata); |
| 198 |
Machine *machine; |
| 199 |
int r; |
| 200 |
|
| 201 |
assert(message); |
| 202 |
|
| 203 |
r = sd_bus_message_new_method_return(message, &reply); |
| 204 |
if (r < 0) |
| 205 |
return sd_bus_error_set_errno(error, r); |
| 206 |
|
| 207 |
r = sd_bus_message_open_container(reply, 'a', "(ssso)"); |
| 208 |
if (r < 0) |
| 209 |
return sd_bus_error_set_errno(error, r); |
| 210 |
|
| 211 |
HASHMAP_FOREACH(machine, m->machines) { |
| 212 |
_cleanup_free_ char *p = NULL; |
| 213 |
|
| 214 |
p = machine_bus_path(machine); |
| 215 |
if (!p) |
| 216 |
return -ENOMEM; |
| 217 |
|
| 218 |
r = sd_bus_message_append(reply, "(ssso)", |
| 219 |
machine->name, |
| 220 |
strempty(machine_class_to_string(machine->class)), |
| 221 |
machine->service, |
| 222 |
p); |
| 223 |
if (r < 0) |
| 224 |
return sd_bus_error_set_errno(error, r); |
| 225 |
} |
| 226 |
|
| 227 |
r = sd_bus_message_close_container(reply); |
| 228 |
if (r < 0) |
| 229 |
return sd_bus_error_set_errno(error, r); |
| 230 |
|
| 231 |
return sd_bus_message_send(reply); |
| 232 |
} |
| 233 |
|
| 234 |
static int machine_add_from_params( |
| 235 |
Manager *manager, |
| 236 |
sd_bus_message *message, |
| 237 |
const char *polkit_action, |
| 238 |
const char *name, |
| 239 |
MachineClass c, |
| 240 |
sd_id128_t id, |
| 241 |
const char *service, |
| 242 |
PidRef *leader_pidref, |
| 243 |
PidRef *supervisor_pidref, |
| 244 |
const char *root_directory, |
| 245 |
const int32_t *netif, |
| 246 |
size_t n_netif, |
| 247 |
unsigned cid, |
| 248 |
const char *ssh_address, |
| 249 |
const char *ssh_private_key_path, |
| 250 |
Machine **ret, |
| 251 |
sd_bus_error *error) { |
| 252 |
|
| 253 |
Machine *m; |
| 254 |
int r; |
| 255 |
|
| 256 |
assert(manager); |
| 257 |
assert(message); |
| 258 |
assert(name); |
| 259 |
assert(c == _MACHINE_CLASS_INVALID || MACHINE_CLASS_CAN_REGISTER(c)); |
| 260 |
assert(leader_pidref); |
| 261 |
assert(supervisor_pidref); |
| 262 |
assert(ret); |
| 263 |
|
| 264 |
if (leader_pidref->pid == 1) |
| 265 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID"); |
| 266 |
if (supervisor_pidref->pid == 1) |
| 267 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid supervisor PID"); |
| 268 |
|
| 269 |
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
| 270 |
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds); |
| 271 |
if (r < 0) |
| 272 |
return r; |
| 273 |
|
| 274 |
uid_t uid; |
| 275 |
r = sd_bus_creds_get_euid(creds, &uid); |
| 276 |
if (r < 0) |
| 277 |
return r; |
| 278 |
|
| 279 |
/* Ensure an unprivileged user cannot claim any process they don't control as their own machine */ |
| 280 |
switch (manager->runtime_scope) { |
| 281 |
|
| 282 |
case RUNTIME_SCOPE_SYSTEM: |
| 283 |
/* In system mode root may register anything */ |
| 284 |
if (uid == 0) |
| 285 |
break; |
| 286 |
|
| 287 |
/* And non-root may only register things if they own the userns */ |
| 288 |
r = process_is_owned_by_uid(leader_pidref, uid); |
| 289 |
if (r < 0) |
| 290 |
return r; |
| 291 |
if (r > 0) |
| 292 |
break; |
| 293 |
|
| 294 |
/* Nothing else may */ |
| 295 |
return sd_bus_error_set(error, SD_BUS_ERROR_ACCESS_DENIED, "Only root may register machines for other users"); |
| 296 |
|
| 297 |
case RUNTIME_SCOPE_USER: |
| 298 |
/* In user mode the user owning our instance may register anything. */ |
| 299 |
if (uid == getuid()) |
| 300 |
break; |
| 301 |
|
| 302 |
/* Nothing else may */ |
| 303 |
return sd_bus_error_set(error, SD_BUS_ERROR_ACCESS_DENIED, "Other users may not register machines with us, sorry."); |
| 304 |
|
| 305 |
default: |
| 306 |
assert_not_reached(); |
| 307 |
} |
| 308 |
|
| 309 |
if (manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 310 |
const char *details[] = { |
| 311 |
"name", name, |
| 312 |
"class", machine_class_to_string(c), |
| 313 |
NULL |
| 314 |
}; |
| 315 |
|
| 316 |
r = bus_verify_polkit_async( |
| 317 |
message, |
| 318 |
polkit_action, |
| 319 |
details, |
| 320 |
&manager->polkit_registry, |
| 321 |
error); |
| 322 |
if (r <= 0) |
| 323 |
return r; /* 0 means Polkit will call us back, see method_create_machine() */ |
| 324 |
} |
| 325 |
|
| 326 |
r = manager_add_machine(manager, name, &m); |
| 327 |
if (r < 0) |
| 328 |
return r; |
| 329 |
|
| 330 |
m->leader = TAKE_PIDREF(*leader_pidref); |
| 331 |
m->supervisor = TAKE_PIDREF(*supervisor_pidref); |
| 332 |
m->class = c; |
| 333 |
m->id = id; |
| 334 |
m->uid = uid; |
| 335 |
m->vsock_cid = cid; |
| 336 |
|
| 337 |
if (!isempty(service)) { |
| 338 |
m->service = strdup(service); |
| 339 |
if (!m->service) { |
| 340 |
r = -ENOMEM; |
| 341 |
goto fail; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
if (!isempty(root_directory)) { |
| 346 |
m->root_directory = strdup(root_directory); |
| 347 |
if (!m->root_directory) { |
| 348 |
r = -ENOMEM; |
| 349 |
goto fail; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
if (n_netif > 0) { |
| 354 |
assert_cc(sizeof(int32_t) == sizeof(int)); |
| 355 |
m->netif = memdup(netif, sizeof(int32_t) * n_netif); |
| 356 |
if (!m->netif) { |
| 357 |
r = -ENOMEM; |
| 358 |
goto fail; |
| 359 |
} |
| 360 |
|
| 361 |
m->n_netif = n_netif; |
| 362 |
} |
| 363 |
|
| 364 |
if (!isempty(ssh_address)) { |
| 365 |
m->ssh_address = strdup(ssh_address); |
| 366 |
if (!m->ssh_address) { |
| 367 |
r = -ENOMEM; |
| 368 |
goto fail; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
if (!isempty(ssh_private_key_path)) { |
| 373 |
m->ssh_private_key_path = strdup(ssh_private_key_path); |
| 374 |
if (!m->ssh_private_key_path) { |
| 375 |
r = -ENOMEM; |
| 376 |
goto fail; |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
*ret = m; |
| 381 |
return 1; |
| 382 |
|
| 383 |
fail: |
| 384 |
machine_add_to_gc_queue(m); |
| 385 |
return r; |
| 386 |
} |
| 387 |
|
| 388 |
static int method_create_or_register_machine( |
| 389 |
Manager *manager, |
| 390 |
sd_bus_message *message, |
| 391 |
const char *polkit_action, |
| 392 |
Machine **ret, |
| 393 |
sd_bus_error *error) { |
| 394 |
|
| 395 |
_cleanup_(pidref_done) PidRef leader_pidref = PIDREF_NULL, supervisor_pidref = PIDREF_NULL; |
| 396 |
const char *name, *service, *class, *root_directory; |
| 397 |
const int32_t *netif = NULL; |
| 398 |
MachineClass c; |
| 399 |
uint32_t leader; |
| 400 |
sd_id128_t id; |
| 401 |
size_t n_netif = 0; |
| 402 |
int r; |
| 403 |
|
| 404 |
assert(manager); |
| 405 |
assert(message); |
| 406 |
assert(ret); |
| 407 |
|
| 408 |
r = sd_bus_message_read(message, "s", &name); |
| 409 |
if (r < 0) |
| 410 |
return r; |
| 411 |
if (!hostname_is_valid(name, 0)) |
| 412 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine name"); |
| 413 |
|
| 414 |
r = bus_message_read_id128(message, &id); |
| 415 |
if (r < 0) |
| 416 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine ID parameter"); |
| 417 |
|
| 418 |
r = sd_bus_message_read(message, "ssus", &service, &class, &leader, &root_directory); |
| 419 |
if (r < 0) |
| 420 |
return r; |
| 421 |
|
| 422 |
if (endswith(sd_bus_message_get_member(message), "WithNetwork")) { |
| 423 |
r = sd_bus_message_read_array(message, 'i', (const void**) &netif, &n_netif); |
| 424 |
if (r < 0) |
| 425 |
return r; |
| 426 |
|
| 427 |
n_netif /= sizeof(int32_t); |
| 428 |
|
| 429 |
for (size_t i = 0; i < n_netif; i++) { |
| 430 |
if (netif[i] <= 0) |
| 431 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid network interface index %i", netif[i]); |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
if (isempty(class)) |
| 436 |
c = _MACHINE_CLASS_INVALID; |
| 437 |
else { |
| 438 |
c = machine_class_from_string(class); |
| 439 |
if (c < 0 || !MACHINE_CLASS_CAN_REGISTER(c)) |
| 440 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine class parameter"); |
| 441 |
} |
| 442 |
|
| 443 |
if (leader == 1) |
| 444 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID"); |
| 445 |
|
| 446 |
if (!isempty(root_directory) && (!path_is_absolute(root_directory) || !path_is_valid(root_directory))) |
| 447 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Root directory must be empty or an absolute path"); |
| 448 |
|
| 449 |
if (leader == 0) { |
| 450 |
/* If no PID is specified, the client is the leader */ |
| 451 |
r = bus_query_sender_pidref(message, &leader_pidref); |
| 452 |
if (r < 0) |
| 453 |
return sd_bus_error_set_errnof(error, r, "Failed to pin client process: %m"); |
| 454 |
} else { |
| 455 |
/* If a PID is specified that's the leader, but if the client process is different from it, than that's the supervisor */ |
| 456 |
r = pidref_set_pid(&leader_pidref, leader); |
| 457 |
if (r < 0) |
| 458 |
return sd_bus_error_set_errnof(error, r, "Failed to pin process " PID_FMT ": %m", (pid_t) leader); |
| 459 |
|
| 460 |
_cleanup_(pidref_done) PidRef client_pidref = PIDREF_NULL; |
| 461 |
r = bus_query_sender_pidref(message, &client_pidref); |
| 462 |
if (r < 0) |
| 463 |
return sd_bus_error_set_errnof(error, r, "Failed to pin client process: %m"); |
| 464 |
|
| 465 |
if (!pidref_equal(&client_pidref, &leader_pidref)) |
| 466 |
supervisor_pidref = TAKE_PIDREF(client_pidref); |
| 467 |
} |
| 468 |
|
| 469 |
if (hashmap_contains(manager->machines, name)) |
| 470 |
return sd_bus_error_setf(error, BUS_ERROR_MACHINE_EXISTS, "Machine '%s' already exists", name); |
| 471 |
|
| 472 |
return machine_add_from_params( |
| 473 |
manager, |
| 474 |
message, |
| 475 |
polkit_action, |
| 476 |
name, |
| 477 |
c, |
| 478 |
id, |
| 479 |
service, |
| 480 |
&leader_pidref, |
| 481 |
&supervisor_pidref, |
| 482 |
root_directory, |
| 483 |
netif, |
| 484 |
n_netif, |
| 485 |
/* cid= */ VMADDR_CID_ANY, |
| 486 |
/* ssh_address= */ NULL, |
| 487 |
/* ssh_private_key_path= */ NULL, |
| 488 |
ret, |
| 489 |
error); |
| 490 |
} |
| 491 |
|
| 492 |
static int method_create_or_register_machine_ex( |
| 493 |
Manager *manager, |
| 494 |
sd_bus_message *message, |
| 495 |
const char *polkit_action, |
| 496 |
Machine **ret, |
| 497 |
sd_bus_error *error) { |
| 498 |
|
| 499 |
const char *name = NULL, *service = NULL, *class = NULL, *root_directory = NULL, *ssh_address = NULL, *ssh_private_key_path = NULL; |
| 500 |
_cleanup_(pidref_done) PidRef leader_pidref = PIDREF_NULL, supervisor_pidref = PIDREF_NULL; |
| 501 |
sd_id128_t id = SD_ID128_NULL; |
| 502 |
const int32_t *netif = NULL; |
| 503 |
size_t n_netif = 0; |
| 504 |
unsigned cid = 0; |
| 505 |
MachineClass c; |
| 506 |
uint64_t leader_pidfdid = 0; |
| 507 |
uint32_t leader_pid = 0; |
| 508 |
int r, leader_pidfd = -EBADF; |
| 509 |
|
| 510 |
assert(manager); |
| 511 |
assert(message); |
| 512 |
assert(ret); |
| 513 |
|
| 514 |
r = sd_bus_message_read(message, "s", &name); |
| 515 |
if (r < 0) |
| 516 |
return r; |
| 517 |
if (!hostname_is_valid(name, 0)) |
| 518 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine name"); |
| 519 |
|
| 520 |
r = sd_bus_message_enter_container(message, 'a', "(sv)"); |
| 521 |
if (r < 0) |
| 522 |
return r; |
| 523 |
|
| 524 |
for (;;) { |
| 525 |
const char *key; |
| 526 |
|
| 527 |
r = sd_bus_message_enter_container(message, 'r', "sv"); |
| 528 |
if (r < 0) |
| 529 |
return r; |
| 530 |
if (r == 0) |
| 531 |
break; |
| 532 |
|
| 533 |
r = sd_bus_message_read(message, "s", &key); |
| 534 |
if (r < 0) |
| 535 |
return r; |
| 536 |
|
| 537 |
r = sd_bus_message_enter_container(message, 'v', NULL); |
| 538 |
if (r < 0) |
| 539 |
return r; |
| 540 |
|
| 541 |
if (streq(key, "Id")) { |
| 542 |
r = bus_message_read_id128(message, &id); |
| 543 |
if (r < 0) |
| 544 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine ID parameter"); |
| 545 |
} else if (streq(key, "Service")) { |
| 546 |
r = sd_bus_message_read(message, "s", &service); |
| 547 |
if (r < 0) |
| 548 |
return r; |
| 549 |
} else if (streq(key, "Class")) { |
| 550 |
r = sd_bus_message_read(message, "s", &class); |
| 551 |
if (r < 0) |
| 552 |
return r; |
| 553 |
} else if (streq(key, "LeaderPID")) { |
| 554 |
r = sd_bus_message_read(message, "u", &leader_pid); |
| 555 |
if (r < 0) |
| 556 |
return r; |
| 557 |
} else if (streq(key, "LeaderPIDFD")) { |
| 558 |
r = sd_bus_message_read(message, "h", &leader_pidfd); |
| 559 |
if (r < 0) |
| 560 |
return r; |
| 561 |
} else if (streq(key, "LeaderPIDFDID")) { |
| 562 |
r = sd_bus_message_read(message, "t", &leader_pidfdid); |
| 563 |
if (r < 0) |
| 564 |
return r; |
| 565 |
} else if (streq(key, "RootDirectory")) { |
| 566 |
r = sd_bus_message_read(message, "s", &root_directory); |
| 567 |
if (r < 0) |
| 568 |
return r; |
| 569 |
} else if (streq(key, "NetworkInterfaces")) { |
| 570 |
r = sd_bus_message_read_array(message, 'i', (const void**) &netif, &n_netif); |
| 571 |
if (r < 0) |
| 572 |
return r; |
| 573 |
|
| 574 |
n_netif /= sizeof(int32_t); |
| 575 |
|
| 576 |
for (size_t i = 0; i < n_netif; i++) |
| 577 |
if (netif[i] <= 0) |
| 578 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid network interface index %i", netif[i]); |
| 579 |
} else if (streq(key, "VSockCID")) { |
| 580 |
r = sd_bus_message_read(message, "u", &cid); |
| 581 |
if (r < 0) |
| 582 |
return r; |
| 583 |
|
| 584 |
if (!VSOCK_CID_IS_REGULAR(cid)) |
| 585 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "JSON field 'vSockCid' is not a regular VSOCK CID."); |
| 586 |
} else if (streq(key, "SSHAddress")) { |
| 587 |
r = sd_bus_message_read(message, "s", &ssh_address); |
| 588 |
if (r < 0) |
| 589 |
return r; |
| 590 |
} else if (streq(key, "SSHPrivateKeyPath")) { |
| 591 |
r = sd_bus_message_read(message, "s", &ssh_private_key_path); |
| 592 |
if (r < 0) |
| 593 |
return r; |
| 594 |
} else |
| 595 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown property '%s'", key); |
| 596 |
|
| 597 |
r = sd_bus_message_exit_container(message); |
| 598 |
if (r < 0) |
| 599 |
return r; |
| 600 |
|
| 601 |
r = sd_bus_message_exit_container(message); |
| 602 |
if (r < 0) |
| 603 |
return r; |
| 604 |
} |
| 605 |
|
| 606 |
r = sd_bus_message_exit_container(message); |
| 607 |
if (r < 0) |
| 608 |
return r; |
| 609 |
|
| 610 |
if (isempty(class)) |
| 611 |
c = _MACHINE_CLASS_INVALID; |
| 612 |
else { |
| 613 |
c = machine_class_from_string(class); |
| 614 |
if (c < 0 || !MACHINE_CLASS_CAN_REGISTER(c)) |
| 615 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine class parameter"); |
| 616 |
} |
| 617 |
|
| 618 |
if (!isempty(root_directory) && (!path_is_absolute(root_directory) || !path_is_valid(root_directory))) |
| 619 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Root directory must be empty or an absolute path"); |
| 620 |
|
| 621 |
if (hashmap_contains(manager->machines, name)) |
| 622 |
return sd_bus_error_setf(error, BUS_ERROR_MACHINE_EXISTS, "Machine '%s' already exists", name); |
| 623 |
|
| 624 |
/* If a PID is specified that's the leader, but if the client process is different from it, than that's the supervisor */ |
| 625 |
if (leader_pidfd >= 0) { |
| 626 |
r = pidref_set_pidfd(&leader_pidref, leader_pidfd); |
| 627 |
if (r < 0) |
| 628 |
return sd_bus_error_set_errnof(error, r, "Failed to parse PIDFD %d: %m", leader_pidfd); |
| 629 |
|
| 630 |
if (leader_pid > 0 && leader_pidref.pid != (pid_t) leader_pid) |
| 631 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "LeaderPID and LeaderPIDFD refer to different processes"); |
| 632 |
if (leader_pidfdid > 0) { |
| 633 |
r = pidref_acquire_pidfd_id(&leader_pidref); |
| 634 |
if (r >= 0 && leader_pidref.fd_id != leader_pidfdid) |
| 635 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "LeaderPIDFDID does not match the inode number of LeaderPIDFD"); |
| 636 |
} |
| 637 |
} else if (leader_pid > 0 && leader_pidfdid > 0) { |
| 638 |
r = pidref_set_pid_and_pidfd_id(&leader_pidref, leader_pid, leader_pidfdid); |
| 639 |
if (r < 0) |
| 640 |
return sd_bus_error_set_errnof(error, r, "Failed to pin process " PID_FMT " by PIDFDID %" PRIu64 ": %m", (pid_t) leader_pid, leader_pidfdid); |
| 641 |
} else if (leader_pid > 0 || leader_pidfdid > 0) |
| 642 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Both LeaderPID and LeaderPIDFDID must be specified to identify the leader process by PIDFDID"); |
| 643 |
|
| 644 |
if (pidref_is_set(&leader_pidref)) { |
| 645 |
_cleanup_(pidref_done) PidRef client_pidref = PIDREF_NULL; |
| 646 |
r = bus_query_sender_pidref(message, &client_pidref); |
| 647 |
if (r < 0) |
| 648 |
return sd_bus_error_set_errnof(error, r, "Failed to pin client process: %m"); |
| 649 |
|
| 650 |
if (!pidref_equal(&client_pidref, &leader_pidref)) |
| 651 |
supervisor_pidref = TAKE_PIDREF(client_pidref); |
| 652 |
} else { |
| 653 |
/* If no PID is specified, the client is the leader */ |
| 654 |
r = bus_query_sender_pidref(message, &leader_pidref); |
| 655 |
if (r < 0) |
| 656 |
return sd_bus_error_set_errnof(error, r, "Failed to pin client process: %m"); |
| 657 |
} |
| 658 |
|
| 659 |
return machine_add_from_params( |
| 660 |
manager, |
| 661 |
message, |
| 662 |
polkit_action, |
| 663 |
name, |
| 664 |
c, |
| 665 |
id, |
| 666 |
service, |
| 667 |
&leader_pidref, |
| 668 |
&supervisor_pidref, |
| 669 |
root_directory, |
| 670 |
netif, |
| 671 |
n_netif, |
| 672 |
cid, |
| 673 |
ssh_address, |
| 674 |
ssh_private_key_path, |
| 675 |
ret, |
| 676 |
error); |
| 677 |
} |
| 678 |
|
| 679 |
static int method_create_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 680 |
Manager *manager = ASSERT_PTR(userdata); |
| 681 |
Machine *m = NULL; |
| 682 |
int r; |
| 683 |
|
| 684 |
assert(message); |
| 685 |
|
| 686 |
if (sd_bus_message_is_method_call(message, NULL, "CreateMachineEx")) |
| 687 |
r = method_create_or_register_machine_ex(manager, message, "org.freedesktop.machine1.create-machines", &m, error); |
| 688 |
else |
| 689 |
r = method_create_or_register_machine(manager, message, "org.freedesktop.machine1.create-machine", &m, error); |
| 690 |
if (r < 0) |
| 691 |
return r; |
| 692 |
if (r == 0) |
| 693 |
return 1; /* Will call us back */ |
| 694 |
|
| 695 |
r = sd_bus_message_enter_container(message, 'a', "(sv)"); |
| 696 |
if (r < 0) |
| 697 |
goto fail; |
| 698 |
|
| 699 |
r = machine_start(m, message, error); |
| 700 |
if (r < 0) |
| 701 |
goto fail; |
| 702 |
|
| 703 |
m->create_message = sd_bus_message_ref(message); |
| 704 |
return 1; |
| 705 |
|
| 706 |
fail: |
| 707 |
machine_add_to_gc_queue(m); |
| 708 |
return r; |
| 709 |
} |
| 710 |
|
| 711 |
static int method_register_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 712 |
Manager *manager = ASSERT_PTR(userdata); |
| 713 |
_cleanup_free_ char *p = NULL; |
| 714 |
Machine *m = NULL; |
| 715 |
int r; |
| 716 |
|
| 717 |
assert(message); |
| 718 |
|
| 719 |
if (sd_bus_message_is_method_call(message, NULL, "RegisterMachineEx")) |
| 720 |
r = method_create_or_register_machine_ex(manager, message, "org.freedesktop.machine1.register-machine", &m, error); |
| 721 |
else |
| 722 |
r = method_create_or_register_machine(manager, message, "org.freedesktop.machine1.register-machine", &m, error); |
| 723 |
if (r < 0) |
| 724 |
return r; |
| 725 |
if (r == 0) |
| 726 |
return 1; /* Will call us back */ |
| 727 |
|
| 728 |
switch (manager->runtime_scope) { |
| 729 |
case RUNTIME_SCOPE_USER: |
| 730 |
r = cg_pidref_get_user_unit_full(&m->leader, &m->unit, &m->subgroup); |
| 731 |
break; |
| 732 |
|
| 733 |
case RUNTIME_SCOPE_SYSTEM: |
| 734 |
r = cg_pidref_get_unit_full(&m->leader, &m->unit, &m->subgroup); |
| 735 |
break; |
| 736 |
|
| 737 |
default: |
| 738 |
assert_not_reached(); |
| 739 |
} |
| 740 |
if (r < 0) { |
| 741 |
r = sd_bus_error_set_errnof(error, r, |
| 742 |
"Failed to determine unit of process "PID_FMT" : %m", |
| 743 |
m->leader.pid); |
| 744 |
goto fail; |
| 745 |
} |
| 746 |
|
| 747 |
if (!empty_or_root(m->subgroup)) { |
| 748 |
/* If this is not a top-level cgroup, then we need the cgroup path to be able to watch when |
| 749 |
* it empties */ |
| 750 |
|
| 751 |
r = cg_pidref_get_path(&m->leader, &m->cgroup); |
| 752 |
if (r < 0) { |
| 753 |
r = sd_bus_error_set_errnof(error, r, |
| 754 |
"Failed to determine cgroup of process "PID_FMT" : %m", |
| 755 |
m->leader.pid); |
| 756 |
goto fail; |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
r = machine_start(m, NULL, error); |
| 761 |
if (r < 0) |
| 762 |
goto fail; |
| 763 |
|
| 764 |
p = machine_bus_path(m); |
| 765 |
if (!p) { |
| 766 |
r = -ENOMEM; |
| 767 |
goto fail; |
| 768 |
} |
| 769 |
|
| 770 |
return sd_bus_reply_method_return(message, "o", p); |
| 771 |
|
| 772 |
fail: |
| 773 |
machine_add_to_gc_queue(m); |
| 774 |
return r; |
| 775 |
} |
| 776 |
|
| 777 |
static int redirect_method_to_machine(sd_bus_message *message, Manager *m, sd_bus_error *error, sd_bus_message_handler_t method) { |
| 778 |
Machine *machine; |
| 779 |
const char *name; |
| 780 |
int r; |
| 781 |
|
| 782 |
assert(message); |
| 783 |
assert(m); |
| 784 |
assert(method); |
| 785 |
|
| 786 |
r = sd_bus_message_read(message, "s", &name); |
| 787 |
if (r < 0) |
| 788 |
return sd_bus_error_set_errno(error, r); |
| 789 |
|
| 790 |
machine = hashmap_get(m->machines, name); |
| 791 |
if (!machine) |
| 792 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); |
| 793 |
|
| 794 |
return method(message, machine, error); |
| 795 |
} |
| 796 |
|
| 797 |
static int method_unregister_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 798 |
return redirect_method_to_machine(message, userdata, error, bus_machine_method_unregister); |
| 799 |
} |
| 800 |
|
| 801 |
static int method_terminate_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 802 |
return redirect_method_to_machine(message, userdata, error, bus_machine_method_terminate); |
| 803 |
} |
| 804 |
|
| 805 |
static int method_kill_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 806 |
return redirect_method_to_machine(message, userdata, error, bus_machine_method_kill); |
| 807 |
} |
| 808 |
|
| 809 |
static int method_get_machine_addresses(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 810 |
return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_addresses); |
| 811 |
} |
| 812 |
|
| 813 |
static int method_get_machine_ssh_info(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 814 |
return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_ssh_info); |
| 815 |
} |
| 816 |
|
| 817 |
static int method_get_machine_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 818 |
return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_os_release); |
| 819 |
} |
| 820 |
|
| 821 |
static int method_list_images(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 822 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 823 |
Manager *m = ASSERT_PTR(userdata); |
| 824 |
int r; |
| 825 |
|
| 826 |
assert(message); |
| 827 |
|
| 828 |
_cleanup_hashmap_free_ Hashmap *images = NULL; |
| 829 |
r = image_discover(m->runtime_scope, IMAGE_MACHINE, NULL, &images); |
| 830 |
if (r < 0) |
| 831 |
return r; |
| 832 |
|
| 833 |
r = sd_bus_message_new_method_return(message, &reply); |
| 834 |
if (r < 0) |
| 835 |
return r; |
| 836 |
|
| 837 |
r = sd_bus_message_open_container(reply, 'a', "(ssbttto)"); |
| 838 |
if (r < 0) |
| 839 |
return r; |
| 840 |
|
| 841 |
Image *image; |
| 842 |
HASHMAP_FOREACH(image, images) { |
| 843 |
_cleanup_free_ char *p = NULL; |
| 844 |
|
| 845 |
p = image_bus_path(image->name); |
| 846 |
if (!p) |
| 847 |
return -ENOMEM; |
| 848 |
|
| 849 |
r = sd_bus_message_append(reply, "(ssbttto)", |
| 850 |
image->name, |
| 851 |
image_type_to_string(image->type), |
| 852 |
image_is_read_only(image), |
| 853 |
image->crtime, |
| 854 |
image->mtime, |
| 855 |
image->usage, |
| 856 |
p); |
| 857 |
if (r < 0) |
| 858 |
return r; |
| 859 |
} |
| 860 |
|
| 861 |
r = sd_bus_message_close_container(reply); |
| 862 |
if (r < 0) |
| 863 |
return r; |
| 864 |
|
| 865 |
return sd_bus_message_send(reply); |
| 866 |
} |
| 867 |
|
| 868 |
static int method_open_machine_pty(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 869 |
return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_pty); |
| 870 |
} |
| 871 |
|
| 872 |
static int method_open_machine_login(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 873 |
return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_login); |
| 874 |
} |
| 875 |
|
| 876 |
static int method_open_machine_shell(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 877 |
return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_shell); |
| 878 |
} |
| 879 |
|
| 880 |
static int method_bind_mount_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 881 |
return redirect_method_to_machine(message, userdata, error, bus_machine_method_bind_mount); |
| 882 |
} |
| 883 |
|
| 884 |
static int method_copy_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 885 |
return redirect_method_to_machine(message, userdata, error, bus_machine_method_copy); |
| 886 |
} |
| 887 |
|
| 888 |
static int method_open_machine_root_directory(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 889 |
return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_root_directory); |
| 890 |
} |
| 891 |
|
| 892 |
static int method_get_machine_uid_shift(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 893 |
return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_uid_shift); |
| 894 |
} |
| 895 |
|
| 896 |
static int redirect_method_to_image(sd_bus_message *message, Manager *m, sd_bus_error *error, sd_bus_message_handler_t method) { |
| 897 |
const char *name; |
| 898 |
Image *i; |
| 899 |
int r; |
| 900 |
|
| 901 |
assert(message); |
| 902 |
assert(m); |
| 903 |
assert(method); |
| 904 |
|
| 905 |
r = sd_bus_message_read(message, "s", &name); |
| 906 |
if (r < 0) |
| 907 |
return r; |
| 908 |
|
| 909 |
if (!image_name_is_valid(name)) |
| 910 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name); |
| 911 |
|
| 912 |
r = manager_acquire_image(m, name, &i); |
| 913 |
if (r == -ENOENT) |
| 914 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name); |
| 915 |
if (r < 0) |
| 916 |
return r; |
| 917 |
|
| 918 |
return method(message, i, error); |
| 919 |
} |
| 920 |
|
| 921 |
static int method_remove_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 922 |
return redirect_method_to_image(message, userdata, error, bus_image_method_remove); |
| 923 |
} |
| 924 |
|
| 925 |
static int method_rename_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 926 |
return redirect_method_to_image(message, userdata, error, bus_image_method_rename); |
| 927 |
} |
| 928 |
|
| 929 |
static int method_clone_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 930 |
return redirect_method_to_image(message, userdata, error, bus_image_method_clone); |
| 931 |
} |
| 932 |
|
| 933 |
static int method_mark_image_read_only(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 934 |
return redirect_method_to_image(message, userdata, error, bus_image_method_mark_read_only); |
| 935 |
} |
| 936 |
|
| 937 |
static int method_get_image_hostname(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 938 |
return redirect_method_to_image(message, userdata, error, bus_image_method_get_hostname); |
| 939 |
} |
| 940 |
|
| 941 |
static int method_get_image_machine_id(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 942 |
return redirect_method_to_image(message, userdata, error, bus_image_method_get_machine_id); |
| 943 |
} |
| 944 |
|
| 945 |
static int method_get_image_machine_info(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 946 |
return redirect_method_to_image(message, userdata, error, bus_image_method_get_machine_info); |
| 947 |
} |
| 948 |
|
| 949 |
static int method_get_image_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 950 |
return redirect_method_to_image(message, userdata, error, bus_image_method_get_os_release); |
| 951 |
} |
| 952 |
|
| 953 |
static int clean_pool_done(Operation *operation, int child_error, sd_bus_error *error) { |
| 954 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 955 |
_cleanup_fclose_ FILE *file = NULL; |
| 956 |
int r; |
| 957 |
|
| 958 |
assert(operation); |
| 959 |
assert(operation->message); |
| 960 |
assert(operation->extra_fd >= 0); |
| 961 |
|
| 962 |
file = take_fdopen(&operation->extra_fd, "r"); |
| 963 |
if (!file) |
| 964 |
return log_debug_errno(errno, "Failed to take opened tmp file's fd: %m"); |
| 965 |
|
| 966 |
r = clean_pool_read_first_entry(file, child_error, error); |
| 967 |
if (r < 0) |
| 968 |
return r; |
| 969 |
|
| 970 |
r = sd_bus_message_new_method_return(operation->message, &reply); |
| 971 |
if (r < 0) |
| 972 |
return r; |
| 973 |
|
| 974 |
r = sd_bus_message_open_container(reply, 'a', "(st)"); |
| 975 |
if (r < 0) |
| 976 |
return r; |
| 977 |
|
| 978 |
/* On success the resulting temporary file will contain a list of image names that were removed followed by |
| 979 |
* their size on disk. Let's read that and turn it into a bus message. */ |
| 980 |
for (;;) { |
| 981 |
_cleanup_free_ char *name = NULL; |
| 982 |
uint64_t usage; |
| 983 |
|
| 984 |
r = clean_pool_read_next_entry(file, &name, &usage); |
| 985 |
if (r < 0) |
| 986 |
return r; |
| 987 |
if (r == 0) |
| 988 |
break; |
| 989 |
|
| 990 |
r = sd_bus_message_append(reply, "(st)", name, usage); |
| 991 |
if (r < 0) |
| 992 |
return r; |
| 993 |
} |
| 994 |
|
| 995 |
r = sd_bus_message_close_container(reply); |
| 996 |
if (r < 0) |
| 997 |
return r; |
| 998 |
|
| 999 |
return sd_bus_message_send(reply); |
| 1000 |
} |
| 1001 |
|
| 1002 |
static int method_clean_pool(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1003 |
ImageCleanPoolMode mode; |
| 1004 |
Manager *m = userdata; |
| 1005 |
Operation *operation; |
| 1006 |
const char *mm; |
| 1007 |
int r; |
| 1008 |
|
| 1009 |
assert(message); |
| 1010 |
|
| 1011 |
if (m->n_operations >= OPERATIONS_MAX) |
| 1012 |
return sd_bus_error_set(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Too many ongoing operations."); |
| 1013 |
|
| 1014 |
r = sd_bus_message_read(message, "s", &mm); |
| 1015 |
if (r < 0) |
| 1016 |
return r; |
| 1017 |
|
| 1018 |
if (streq(mm, "all")) |
| 1019 |
mode = IMAGE_CLEAN_POOL_REMOVE_ALL; |
| 1020 |
else if (streq(mm, "hidden")) |
| 1021 |
mode = IMAGE_CLEAN_POOL_REMOVE_HIDDEN; |
| 1022 |
else |
| 1023 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown mode '%s'.", mm); |
| 1024 |
|
| 1025 |
if (m->runtime_scope != RUNTIME_SCOPE_USER) { |
| 1026 |
const char *details[] = { |
| 1027 |
"verb", "clean_pool", |
| 1028 |
"mode", mm, |
| 1029 |
NULL |
| 1030 |
}; |
| 1031 |
|
| 1032 |
r = bus_verify_polkit_async( |
| 1033 |
message, |
| 1034 |
"org.freedesktop.machine1.manage-machines", |
| 1035 |
details, |
| 1036 |
&m->polkit_registry, |
| 1037 |
error); |
| 1038 |
if (r < 0) |
| 1039 |
return r; |
| 1040 |
if (r == 0) |
| 1041 |
return 1; /* Will call us back */ |
| 1042 |
} |
| 1043 |
|
| 1044 |
r = image_clean_pool_operation(m, mode, &operation); |
| 1045 |
if (r < 0) |
| 1046 |
return log_debug_errno(r, "Failed to clean pool of images: %m"); |
| 1047 |
|
| 1048 |
operation_attach_bus_reply(operation, message); |
| 1049 |
operation->done = clean_pool_done; |
| 1050 |
return 1; |
| 1051 |
} |
| 1052 |
|
| 1053 |
static int method_set_pool_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1054 |
Manager *m = userdata; |
| 1055 |
uint64_t limit; |
| 1056 |
int r; |
| 1057 |
|
| 1058 |
assert(message); |
| 1059 |
|
| 1060 |
r = sd_bus_message_read(message, "t", &limit); |
| 1061 |
if (r < 0) |
| 1062 |
return r; |
| 1063 |
if (!FILE_SIZE_VALID_OR_INFINITY(limit)) |
| 1064 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "New limit out of range"); |
| 1065 |
|
| 1066 |
if (m->runtime_scope != RUNTIME_SCOPE_USER) { |
| 1067 |
const char *details[] = { |
| 1068 |
"verb", "set_pool_limit", |
| 1069 |
NULL |
| 1070 |
}; |
| 1071 |
|
| 1072 |
r = bus_verify_polkit_async( |
| 1073 |
message, |
| 1074 |
"org.freedesktop.machine1.manage-machines", |
| 1075 |
details, |
| 1076 |
&m->polkit_registry, |
| 1077 |
error); |
| 1078 |
if (r < 0) |
| 1079 |
return r; |
| 1080 |
if (r == 0) |
| 1081 |
return 1; /* Will call us back */ |
| 1082 |
} |
| 1083 |
|
| 1084 |
/* Set up the machine directory if necessary */ |
| 1085 |
r = image_setup_pool( |
| 1086 |
m->runtime_scope, |
| 1087 |
IMAGE_MACHINE, |
| 1088 |
/* use_btrfs_subvol= */ true, |
| 1089 |
/* use_btrfs_quota= */ true); |
| 1090 |
if (r < 0) |
| 1091 |
return sd_bus_error_set_errnof(error, r, "Failed to set up machine pool: %m"); |
| 1092 |
|
| 1093 |
r = image_set_pool_limit(m->runtime_scope, IMAGE_MACHINE, limit); |
| 1094 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 1095 |
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Quota is only supported on btrfs."); |
| 1096 |
if (r < 0) |
| 1097 |
return sd_bus_error_set_errnof(error, r, "Failed to adjust quota limit: %m"); |
| 1098 |
|
| 1099 |
return sd_bus_reply_method_return(message, NULL); |
| 1100 |
} |
| 1101 |
|
| 1102 |
static int method_set_image_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1103 |
return redirect_method_to_image(message, userdata, error, bus_image_method_set_limit); |
| 1104 |
} |
| 1105 |
|
| 1106 |
static int method_map_from_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1107 |
Manager *m = userdata; |
| 1108 |
const char *name; |
| 1109 |
Machine *machine; |
| 1110 |
uint32_t uid; |
| 1111 |
uid_t converted; |
| 1112 |
int r; |
| 1113 |
|
| 1114 |
r = sd_bus_message_read(message, "su", &name, &uid); |
| 1115 |
if (r < 0) |
| 1116 |
return r; |
| 1117 |
|
| 1118 |
if (!uid_is_valid(uid)) |
| 1119 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid); |
| 1120 |
|
| 1121 |
machine = hashmap_get(m->machines, name); |
| 1122 |
if (!machine) |
| 1123 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); |
| 1124 |
|
| 1125 |
if (machine->class != MACHINE_CONTAINER) |
| 1126 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines."); |
| 1127 |
|
| 1128 |
r = machine_translate_uid(machine, uid, &converted); |
| 1129 |
if (r == -ESRCH) |
| 1130 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "Machine '%s' has no matching user mappings.", name); |
| 1131 |
if (r < 0) |
| 1132 |
return r; |
| 1133 |
|
| 1134 |
return sd_bus_reply_method_return(message, "u", (uint32_t) converted); |
| 1135 |
} |
| 1136 |
|
| 1137 |
static int method_map_to_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1138 |
_cleanup_free_ char *o = NULL; |
| 1139 |
Manager *m = userdata; |
| 1140 |
Machine *machine; |
| 1141 |
uid_t uid, converted; |
| 1142 |
int r; |
| 1143 |
|
| 1144 |
r = sd_bus_message_read(message, "u", &uid); |
| 1145 |
if (r < 0) |
| 1146 |
return r; |
| 1147 |
if (!uid_is_valid(uid)) |
| 1148 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid); |
| 1149 |
if (uid < 0x10000) |
| 1150 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "User " UID_FMT " belongs to host UID range", uid); |
| 1151 |
|
| 1152 |
r = manager_find_machine_for_uid(m, uid, &machine, &converted); |
| 1153 |
if (r < 0) |
| 1154 |
return r; |
| 1155 |
if (!r) |
| 1156 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "No matching user mapping for " UID_FMT ".", uid); |
| 1157 |
|
| 1158 |
o = machine_bus_path(machine); |
| 1159 |
if (!o) |
| 1160 |
return -ENOMEM; |
| 1161 |
|
| 1162 |
return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted); |
| 1163 |
} |
| 1164 |
|
| 1165 |
static int method_map_from_machine_group(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1166 |
Manager *m = userdata; |
| 1167 |
const char *name; |
| 1168 |
Machine *machine; |
| 1169 |
gid_t converted; |
| 1170 |
uint32_t gid; |
| 1171 |
int r; |
| 1172 |
|
| 1173 |
r = sd_bus_message_read(message, "su", &name, &gid); |
| 1174 |
if (r < 0) |
| 1175 |
return r; |
| 1176 |
|
| 1177 |
if (!gid_is_valid(gid)) |
| 1178 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid); |
| 1179 |
|
| 1180 |
machine = hashmap_get(m->machines, name); |
| 1181 |
if (!machine) |
| 1182 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); |
| 1183 |
|
| 1184 |
if (machine->class != MACHINE_CONTAINER) |
| 1185 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines."); |
| 1186 |
|
| 1187 |
r = machine_translate_gid(machine, gid, &converted); |
| 1188 |
if (r == -ESRCH) |
| 1189 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "Machine '%s' has no matching group mappings.", name); |
| 1190 |
if (r < 0) |
| 1191 |
return r; |
| 1192 |
|
| 1193 |
return sd_bus_reply_method_return(message, "u", (uint32_t) converted); |
| 1194 |
} |
| 1195 |
|
| 1196 |
static int method_map_to_machine_group(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1197 |
_cleanup_free_ char *o = NULL; |
| 1198 |
Manager *m = userdata; |
| 1199 |
Machine *machine; |
| 1200 |
gid_t gid, converted; |
| 1201 |
int r; |
| 1202 |
|
| 1203 |
r = sd_bus_message_read(message, "u", &gid); |
| 1204 |
if (r < 0) |
| 1205 |
return r; |
| 1206 |
if (!gid_is_valid(gid)) |
| 1207 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid); |
| 1208 |
if (gid < 0x10000) |
| 1209 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "Group " GID_FMT " belongs to host GID range", gid); |
| 1210 |
|
| 1211 |
r = manager_find_machine_for_gid(m, gid, &machine, &converted); |
| 1212 |
if (r < 0) |
| 1213 |
return r; |
| 1214 |
if (!r) |
| 1215 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "No matching group mapping for " GID_FMT ".", gid); |
| 1216 |
|
| 1217 |
o = machine_bus_path(machine); |
| 1218 |
if (!o) |
| 1219 |
return -ENOMEM; |
| 1220 |
|
| 1221 |
return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted); |
| 1222 |
} |
| 1223 |
|
| 1224 |
static const sd_bus_vtable manager_vtable[] = { |
| 1225 |
SD_BUS_VTABLE_START(0), |
| 1226 |
|
| 1227 |
SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path, 0, 0), |
| 1228 |
SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage, 0, 0), |
| 1229 |
SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit, 0, 0), |
| 1230 |
|
| 1231 |
SD_BUS_METHOD_WITH_ARGS("GetMachine", |
| 1232 |
SD_BUS_ARGS("s", name), |
| 1233 |
SD_BUS_RESULT("o", machine), |
| 1234 |
method_get_machine, |
| 1235 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1236 |
SD_BUS_METHOD_WITH_ARGS("GetImage", |
| 1237 |
SD_BUS_ARGS("s", name), |
| 1238 |
SD_BUS_RESULT("o", image), |
| 1239 |
method_get_image, |
| 1240 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1241 |
SD_BUS_METHOD_WITH_ARGS("GetMachineByPID", |
| 1242 |
SD_BUS_ARGS("u", pid), |
| 1243 |
SD_BUS_RESULT("o", machine), |
| 1244 |
method_get_machine_by_pid, |
| 1245 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1246 |
SD_BUS_METHOD_WITH_ARGS("ListMachines", |
| 1247 |
SD_BUS_NO_ARGS, |
| 1248 |
SD_BUS_RESULT("a(ssso)", machines), |
| 1249 |
method_list_machines, |
| 1250 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1251 |
SD_BUS_METHOD_WITH_ARGS("ListImages", |
| 1252 |
SD_BUS_NO_ARGS, |
| 1253 |
SD_BUS_RESULT("a(ssbttto)", images), |
| 1254 |
method_list_images, |
| 1255 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1256 |
SD_BUS_METHOD_WITH_ARGS("CreateMachine", |
| 1257 |
SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory, "a(sv)", scope_properties), |
| 1258 |
SD_BUS_RESULT("o", path), |
| 1259 |
method_create_machine, |
| 1260 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1261 |
SD_BUS_METHOD_WITH_ARGS("CreateMachineWithNetwork", |
| 1262 |
SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory, "ai", ifindices, "a(sv)", scope_properties), |
| 1263 |
SD_BUS_RESULT("o", path), |
| 1264 |
method_create_machine, |
| 1265 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1266 |
SD_BUS_METHOD_WITH_ARGS("CreateMachineEx", |
| 1267 |
SD_BUS_ARGS("s", name, "a(sv)", properties, "a(sv)", scope_properties), |
| 1268 |
SD_BUS_RESULT("o", path), |
| 1269 |
method_create_machine, |
| 1270 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1271 |
SD_BUS_METHOD_WITH_ARGS("RegisterMachine", |
| 1272 |
SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory), |
| 1273 |
SD_BUS_RESULT("o", path), |
| 1274 |
method_register_machine, |
| 1275 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1276 |
SD_BUS_METHOD_WITH_ARGS("RegisterMachineWithNetwork", |
| 1277 |
SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory, "ai", ifindices), |
| 1278 |
SD_BUS_RESULT("o", path), |
| 1279 |
method_register_machine, |
| 1280 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1281 |
SD_BUS_METHOD_WITH_ARGS("RegisterMachineEx", |
| 1282 |
SD_BUS_ARGS("s", name, "a(sv)", properties), |
| 1283 |
SD_BUS_RESULT("o", path), |
| 1284 |
method_register_machine, |
| 1285 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1286 |
SD_BUS_METHOD_WITH_ARGS("UnregisterMachine", |
| 1287 |
SD_BUS_ARGS("s", name), |
| 1288 |
SD_BUS_NO_RESULT, |
| 1289 |
method_unregister_machine, |
| 1290 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1291 |
SD_BUS_METHOD_WITH_ARGS("TerminateMachine", |
| 1292 |
SD_BUS_ARGS("s", id), |
| 1293 |
SD_BUS_NO_RESULT, |
| 1294 |
method_terminate_machine, |
| 1295 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1296 |
SD_BUS_METHOD_WITH_ARGS("KillMachine", |
| 1297 |
SD_BUS_ARGS("s", name, "s", whom, "i", signal), |
| 1298 |
SD_BUS_NO_RESULT, |
| 1299 |
method_kill_machine, |
| 1300 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1301 |
SD_BUS_METHOD_WITH_ARGS("GetMachineAddresses", |
| 1302 |
SD_BUS_ARGS("s", name), |
| 1303 |
SD_BUS_RESULT("a(iay)", addresses), |
| 1304 |
method_get_machine_addresses, |
| 1305 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1306 |
SD_BUS_METHOD_WITH_ARGS("GetMachineSSHInfo", |
| 1307 |
SD_BUS_ARGS("s", name), |
| 1308 |
SD_BUS_RESULT("s", ssh_address, "s", ssh_private_key_path), |
| 1309 |
method_get_machine_ssh_info, |
| 1310 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1311 |
SD_BUS_METHOD_WITH_ARGS("GetMachineOSRelease", |
| 1312 |
SD_BUS_ARGS("s", name), |
| 1313 |
SD_BUS_RESULT("a{ss}", fields), |
| 1314 |
method_get_machine_os_release, |
| 1315 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1316 |
SD_BUS_METHOD_WITH_ARGS("OpenMachinePTY", |
| 1317 |
SD_BUS_ARGS("s", name), |
| 1318 |
SD_BUS_RESULT("h", pty, "s", pty_path), |
| 1319 |
method_open_machine_pty, |
| 1320 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1321 |
SD_BUS_METHOD_WITH_ARGS("OpenMachineLogin", |
| 1322 |
SD_BUS_ARGS("s", name), |
| 1323 |
SD_BUS_RESULT("h", pty, "s", pty_path), |
| 1324 |
method_open_machine_login, |
| 1325 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1326 |
SD_BUS_METHOD_WITH_ARGS("OpenMachineShell", |
| 1327 |
SD_BUS_ARGS("s", name, "s", user, "s", path, "as", args, "as", environment), |
| 1328 |
SD_BUS_RESULT("h", pty, "s", pty_path), |
| 1329 |
method_open_machine_shell, |
| 1330 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1331 |
SD_BUS_METHOD_WITH_ARGS("BindMountMachine", |
| 1332 |
SD_BUS_ARGS("s", name, "s", source, "s", destination, "b", read_only, "b", mkdir), |
| 1333 |
SD_BUS_NO_RESULT, |
| 1334 |
method_bind_mount_machine, |
| 1335 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1336 |
SD_BUS_METHOD_WITH_ARGS("CopyFromMachine", |
| 1337 |
SD_BUS_ARGS("s", name, "s", source, "s", destination), |
| 1338 |
SD_BUS_NO_RESULT, |
| 1339 |
method_copy_machine, |
| 1340 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1341 |
SD_BUS_METHOD_WITH_ARGS("CopyToMachine", |
| 1342 |
SD_BUS_ARGS("s", name, "s", source, "s", destination), |
| 1343 |
SD_BUS_NO_RESULT, |
| 1344 |
method_copy_machine, |
| 1345 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1346 |
SD_BUS_METHOD_WITH_ARGS("CopyFromMachineWithFlags", |
| 1347 |
SD_BUS_ARGS("s", name, "s", source, "s", destination, "t", flags), |
| 1348 |
SD_BUS_NO_RESULT, |
| 1349 |
method_copy_machine, |
| 1350 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1351 |
SD_BUS_METHOD_WITH_ARGS("CopyToMachineWithFlags", |
| 1352 |
SD_BUS_ARGS("s", name, "s", source, "s", destination, "t", flags), |
| 1353 |
SD_BUS_NO_RESULT, |
| 1354 |
method_copy_machine, |
| 1355 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1356 |
SD_BUS_METHOD_WITH_ARGS("OpenMachineRootDirectory", |
| 1357 |
SD_BUS_ARGS("s", name), |
| 1358 |
SD_BUS_RESULT("h", fd), |
| 1359 |
method_open_machine_root_directory, |
| 1360 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1361 |
SD_BUS_METHOD_WITH_ARGS("GetMachineUIDShift", |
| 1362 |
SD_BUS_ARGS("s", name), |
| 1363 |
SD_BUS_RESULT("u", shift), |
| 1364 |
method_get_machine_uid_shift, |
| 1365 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1366 |
SD_BUS_METHOD_WITH_ARGS("RemoveImage", |
| 1367 |
SD_BUS_ARGS("s", name), |
| 1368 |
SD_BUS_NO_RESULT, |
| 1369 |
method_remove_image, |
| 1370 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1371 |
SD_BUS_METHOD_WITH_ARGS("RenameImage", |
| 1372 |
SD_BUS_ARGS("s", name, "s", new_name), |
| 1373 |
SD_BUS_NO_RESULT, |
| 1374 |
method_rename_image, |
| 1375 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1376 |
SD_BUS_METHOD_WITH_ARGS("CloneImage", |
| 1377 |
SD_BUS_ARGS("s", name, "s", new_name, "b", read_only), |
| 1378 |
SD_BUS_NO_RESULT, |
| 1379 |
method_clone_image, |
| 1380 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1381 |
SD_BUS_METHOD_WITH_ARGS("MarkImageReadOnly", |
| 1382 |
SD_BUS_ARGS("s", name, "b", read_only), |
| 1383 |
SD_BUS_NO_RESULT, |
| 1384 |
method_mark_image_read_only, |
| 1385 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1386 |
SD_BUS_METHOD_WITH_ARGS("GetImageHostname", |
| 1387 |
SD_BUS_ARGS("s", name), |
| 1388 |
SD_BUS_RESULT("s", hostname), |
| 1389 |
method_get_image_hostname, |
| 1390 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1391 |
SD_BUS_METHOD_WITH_ARGS("GetImageMachineID", |
| 1392 |
SD_BUS_ARGS("s", name), |
| 1393 |
SD_BUS_RESULT("ay", id), |
| 1394 |
method_get_image_machine_id, |
| 1395 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1396 |
SD_BUS_METHOD_WITH_ARGS("GetImageMachineInfo", |
| 1397 |
SD_BUS_ARGS("s", name), |
| 1398 |
SD_BUS_RESULT("a{ss}", machine_info), |
| 1399 |
method_get_image_machine_info, |
| 1400 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1401 |
SD_BUS_METHOD_WITH_ARGS("GetImageOSRelease", |
| 1402 |
SD_BUS_ARGS("s", name), |
| 1403 |
SD_BUS_RESULT("a{ss}", os_release), |
| 1404 |
method_get_image_os_release, |
| 1405 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1406 |
SD_BUS_METHOD_WITH_ARGS("SetPoolLimit", |
| 1407 |
SD_BUS_ARGS("t", size), |
| 1408 |
SD_BUS_NO_RESULT, |
| 1409 |
method_set_pool_limit, |
| 1410 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1411 |
SD_BUS_METHOD_WITH_ARGS("SetImageLimit", |
| 1412 |
SD_BUS_ARGS("s", name, "t", size), |
| 1413 |
SD_BUS_NO_RESULT, |
| 1414 |
method_set_image_limit, |
| 1415 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1416 |
SD_BUS_METHOD_WITH_ARGS("CleanPool", |
| 1417 |
SD_BUS_ARGS("s", mode), |
| 1418 |
SD_BUS_RESULT("a(st)",images), |
| 1419 |
method_clean_pool, |
| 1420 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1421 |
SD_BUS_METHOD_WITH_ARGS("MapFromMachineUser", |
| 1422 |
SD_BUS_ARGS("s", name, "u", uid_inner), |
| 1423 |
SD_BUS_RESULT("u", uid_outer), |
| 1424 |
method_map_from_machine_user, |
| 1425 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1426 |
SD_BUS_METHOD_WITH_ARGS("MapToMachineUser", |
| 1427 |
SD_BUS_ARGS("u", uid_outer), |
| 1428 |
SD_BUS_RESULT("s", machine_name, "o", machine_path, "u", uid_inner), |
| 1429 |
method_map_to_machine_user, |
| 1430 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1431 |
SD_BUS_METHOD_WITH_ARGS("MapFromMachineGroup", |
| 1432 |
SD_BUS_ARGS("s", name, "u", gid_inner), |
| 1433 |
SD_BUS_RESULT("u", gid_outer), |
| 1434 |
method_map_from_machine_group, |
| 1435 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1436 |
SD_BUS_METHOD_WITH_ARGS("MapToMachineGroup", |
| 1437 |
SD_BUS_ARGS("u", gid_outer), |
| 1438 |
SD_BUS_RESULT("s", machine_name, "o", machine_path, "u", gid_inner), |
| 1439 |
method_map_to_machine_group, |
| 1440 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1441 |
|
| 1442 |
SD_BUS_SIGNAL_WITH_ARGS("MachineNew", |
| 1443 |
SD_BUS_ARGS("s", machine, "o", path), |
| 1444 |
0), |
| 1445 |
SD_BUS_SIGNAL_WITH_ARGS("MachineRemoved", |
| 1446 |
SD_BUS_ARGS("s", machine, "o", path), |
| 1447 |
0), |
| 1448 |
|
| 1449 |
SD_BUS_VTABLE_END |
| 1450 |
}; |
| 1451 |
|
| 1452 |
const BusObjectImplementation manager_object = { |
| 1453 |
"/org/freedesktop/machine1", |
| 1454 |
"org.freedesktop.machine1.Manager", |
| 1455 |
.vtables = BUS_VTABLES(manager_vtable), |
| 1456 |
.children = BUS_IMPLEMENTATIONS(&machine_object, |
| 1457 |
&image_object), |
| 1458 |
}; |
| 1459 |
|
| 1460 |
int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1461 |
const char *path, *result, *unit; |
| 1462 |
Manager *m = ASSERT_PTR(userdata); |
| 1463 |
Machine *machine; |
| 1464 |
uint32_t id; |
| 1465 |
int r; |
| 1466 |
|
| 1467 |
assert(message); |
| 1468 |
|
| 1469 |
r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result); |
| 1470 |
if (r < 0) { |
| 1471 |
bus_log_parse_error(r); |
| 1472 |
return 0; |
| 1473 |
} |
| 1474 |
|
| 1475 |
machine = hashmap_get(m->machines_by_unit, unit); |
| 1476 |
if (!machine) |
| 1477 |
return 0; |
| 1478 |
|
| 1479 |
if (streq_ptr(path, machine->scope_job)) { |
| 1480 |
machine->scope_job = mfree(machine->scope_job); |
| 1481 |
|
| 1482 |
if (machine->started) { |
| 1483 |
if (streq(result, "done")) |
| 1484 |
machine_send_create_reply(machine, NULL); |
| 1485 |
else { |
| 1486 |
_cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL; |
| 1487 |
|
| 1488 |
sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result); |
| 1489 |
|
| 1490 |
machine_send_create_reply(machine, &e); |
| 1491 |
} |
| 1492 |
} |
| 1493 |
|
| 1494 |
machine_save(machine); |
| 1495 |
} |
| 1496 |
|
| 1497 |
machine_add_to_gc_queue(machine); |
| 1498 |
return 0; |
| 1499 |
} |
| 1500 |
|
| 1501 |
int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1502 |
_cleanup_free_ char *unit = NULL; |
| 1503 |
const char *path; |
| 1504 |
Manager *m = ASSERT_PTR(userdata); |
| 1505 |
Machine *machine; |
| 1506 |
int r; |
| 1507 |
|
| 1508 |
assert(message); |
| 1509 |
|
| 1510 |
path = sd_bus_message_get_path(message); |
| 1511 |
if (!path) |
| 1512 |
return 0; |
| 1513 |
|
| 1514 |
r = unit_name_from_dbus_path(path, &unit); |
| 1515 |
if (r == -EINVAL) /* not for a unit */ |
| 1516 |
return 0; |
| 1517 |
if (r < 0) { |
| 1518 |
log_oom(); |
| 1519 |
return 0; |
| 1520 |
} |
| 1521 |
|
| 1522 |
machine = hashmap_get(m->machines_by_unit, unit); |
| 1523 |
if (!machine) |
| 1524 |
return 0; |
| 1525 |
|
| 1526 |
machine_add_to_gc_queue(machine); |
| 1527 |
return 0; |
| 1528 |
} |
| 1529 |
|
| 1530 |
int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1531 |
const char *path, *unit; |
| 1532 |
Manager *m = ASSERT_PTR(userdata); |
| 1533 |
Machine *machine; |
| 1534 |
int r; |
| 1535 |
|
| 1536 |
assert(message); |
| 1537 |
|
| 1538 |
r = sd_bus_message_read(message, "so", &unit, &path); |
| 1539 |
if (r < 0) { |
| 1540 |
bus_log_parse_error(r); |
| 1541 |
return 0; |
| 1542 |
} |
| 1543 |
|
| 1544 |
machine = hashmap_get(m->machines_by_unit, unit); |
| 1545 |
if (!machine) |
| 1546 |
return 0; |
| 1547 |
|
| 1548 |
machine_add_to_gc_queue(machine); |
| 1549 |
return 0; |
| 1550 |
} |
| 1551 |
|
| 1552 |
int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1553 |
Manager *m = ASSERT_PTR(userdata); |
| 1554 |
Machine *machine; |
| 1555 |
int b, r; |
| 1556 |
|
| 1557 |
assert(message); |
| 1558 |
|
| 1559 |
r = sd_bus_message_read(message, "b", &b); |
| 1560 |
if (r < 0) { |
| 1561 |
bus_log_parse_error(r); |
| 1562 |
return 0; |
| 1563 |
} |
| 1564 |
if (b) |
| 1565 |
return 0; |
| 1566 |
|
| 1567 |
/* systemd finished reloading, let's recheck all our machines */ |
| 1568 |
log_debug("System manager has been reloaded, rechecking machines..."); |
| 1569 |
|
| 1570 |
HASHMAP_FOREACH(machine, m->machines) |
| 1571 |
machine_add_to_gc_queue(machine); |
| 1572 |
|
| 1573 |
return 0; |
| 1574 |
} |
| 1575 |
|
| 1576 |
int manager_unref_unit( |
| 1577 |
Manager *m, |
| 1578 |
const char *unit, |
| 1579 |
sd_bus_error *error) { |
| 1580 |
|
| 1581 |
assert(m); |
| 1582 |
assert(unit); |
| 1583 |
|
| 1584 |
return bus_call_method(m->api_bus, bus_systemd_mgr, "UnrefUnit", error, NULL, "s", unit); |
| 1585 |
} |
| 1586 |
|
| 1587 |
int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) { |
| 1588 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 1589 |
int r; |
| 1590 |
|
| 1591 |
assert(manager); |
| 1592 |
assert(unit); |
| 1593 |
|
| 1594 |
r = bus_call_method(manager->api_bus, bus_systemd_mgr, "StopUnit", error, &reply, "ss", unit, "fail"); |
| 1595 |
if (r < 0) { |
| 1596 |
if (sd_bus_error_has_names(error, BUS_ERROR_NO_SUCH_UNIT, |
| 1597 |
BUS_ERROR_LOAD_FAILED)) { |
| 1598 |
|
| 1599 |
if (job) |
| 1600 |
*job = NULL; |
| 1601 |
|
| 1602 |
sd_bus_error_free(error); |
| 1603 |
return 0; |
| 1604 |
} |
| 1605 |
|
| 1606 |
return r; |
| 1607 |
} |
| 1608 |
|
| 1609 |
if (job) { |
| 1610 |
const char *j; |
| 1611 |
char *copy; |
| 1612 |
|
| 1613 |
r = sd_bus_message_read(reply, "o", &j); |
| 1614 |
if (r < 0) |
| 1615 |
return r; |
| 1616 |
|
| 1617 |
copy = strdup(j); |
| 1618 |
if (!copy) |
| 1619 |
return -ENOMEM; |
| 1620 |
|
| 1621 |
*job = copy; |
| 1622 |
} |
| 1623 |
|
| 1624 |
return 1; |
| 1625 |
} |
| 1626 |
|
| 1627 |
int manager_kill_unit(Manager *manager, const char *unit, const char *subgroup, int signo, sd_bus_error *reterr_error) { |
| 1628 |
assert(manager); |
| 1629 |
assert(unit); |
| 1630 |
|
| 1631 |
if (empty_or_root(subgroup)) |
| 1632 |
return bus_call_method(manager->api_bus, bus_systemd_mgr, "KillUnit", reterr_error, NULL, "ssi", unit, "all", signo); |
| 1633 |
|
| 1634 |
return bus_call_method(manager->api_bus, bus_systemd_mgr, "KillUnitSubgroup", reterr_error, NULL, "sssi", unit, "cgroup", subgroup, signo); |
| 1635 |
} |
| 1636 |
|
| 1637 |
int manager_unit_is_active(Manager *manager, const char *unit, sd_bus_error *reterr_error) { |
| 1638 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1639 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 1640 |
_cleanup_free_ char *path = NULL; |
| 1641 |
const char *state; |
| 1642 |
int r; |
| 1643 |
|
| 1644 |
assert(manager); |
| 1645 |
assert(unit); |
| 1646 |
|
| 1647 |
r = sd_bus_is_ready(manager->api_bus); |
| 1648 |
if (r < 0) |
| 1649 |
return r; |
| 1650 |
if (r == 0) |
| 1651 |
return -ENOTCONN; |
| 1652 |
|
| 1653 |
path = unit_dbus_path_from_name(unit); |
| 1654 |
if (!path) |
| 1655 |
return -ENOMEM; |
| 1656 |
|
| 1657 |
r = sd_bus_get_property( |
| 1658 |
manager->api_bus, |
| 1659 |
"org.freedesktop.systemd1", |
| 1660 |
path, |
| 1661 |
"org.freedesktop.systemd1.Unit", |
| 1662 |
"ActiveState", |
| 1663 |
&error, |
| 1664 |
&reply, |
| 1665 |
"s"); |
| 1666 |
if (r < 0) { |
| 1667 |
if (bus_error_is_connection(&error)) |
| 1668 |
return true; |
| 1669 |
|
| 1670 |
if (sd_bus_error_has_names(&error, BUS_ERROR_NO_SUCH_UNIT, |
| 1671 |
BUS_ERROR_LOAD_FAILED)) |
| 1672 |
return false; |
| 1673 |
|
| 1674 |
sd_bus_error_move(reterr_error, &error); |
| 1675 |
return r; |
| 1676 |
} |
| 1677 |
|
| 1678 |
r = sd_bus_message_read(reply, "s", &state); |
| 1679 |
if (r < 0) |
| 1680 |
return -EINVAL; |
| 1681 |
|
| 1682 |
return !STR_IN_SET(state, "inactive", "failed"); |
| 1683 |
} |
| 1684 |
|
| 1685 |
int manager_job_is_active(Manager *manager, const char *path, sd_bus_error *reterr_error) { |
| 1686 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1687 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 1688 |
int r; |
| 1689 |
|
| 1690 |
assert(manager); |
| 1691 |
assert(path); |
| 1692 |
|
| 1693 |
r = sd_bus_is_ready(manager->api_bus); |
| 1694 |
if (r < 0) |
| 1695 |
return r; |
| 1696 |
if (r == 0) |
| 1697 |
return -ENOTCONN; |
| 1698 |
|
| 1699 |
r = sd_bus_get_property( |
| 1700 |
manager->api_bus, |
| 1701 |
"org.freedesktop.systemd1", |
| 1702 |
path, |
| 1703 |
"org.freedesktop.systemd1.Job", |
| 1704 |
"State", |
| 1705 |
&error, |
| 1706 |
&reply, |
| 1707 |
"s"); |
| 1708 |
if (r < 0) { |
| 1709 |
if (bus_error_is_connection(&error)) |
| 1710 |
return true; |
| 1711 |
|
| 1712 |
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT)) |
| 1713 |
return false; |
| 1714 |
|
| 1715 |
sd_bus_error_move(reterr_error, &error); |
| 1716 |
return r; |
| 1717 |
} |
| 1718 |
|
| 1719 |
/* We don't actually care about the state really. The fact |
| 1720 |
* that we could read the job state is enough for us */ |
| 1721 |
|
| 1722 |
return true; |
| 1723 |
} |