| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-event.h" |
| 6 |
|
| 7 |
#include "cgroup-util.h" |
| 8 |
#include "copy.h" |
| 9 |
#include "discover-image.h" |
| 10 |
#include "env-file.h" |
| 11 |
#include "fd-util.h" |
| 12 |
#include "fileio.h" |
| 13 |
#include "hashmap.h" |
| 14 |
#include "local-addresses.h" |
| 15 |
#include "log.h" |
| 16 |
#include "machine.h" |
| 17 |
#include "machined.h" |
| 18 |
#include "namespace-util.h" |
| 19 |
#include "os-util.h" |
| 20 |
#include "process-util.h" |
| 21 |
#include "strv.h" |
| 22 |
#include "user-util.h" |
| 23 |
|
| 24 |
int manager_get_machine_by_pidref(Manager *m, const PidRef *pidref, Machine **ret) { |
| 25 |
_cleanup_(pidref_done) PidRef current = PIDREF_NULL; |
| 26 |
Machine *mm = NULL; |
| 27 |
|
| 28 |
assert(m); |
| 29 |
assert(pidref_is_set(pidref)); |
| 30 |
assert(ret); |
| 31 |
|
| 32 |
for (;;) { |
| 33 |
/* First, compare by leader */ |
| 34 |
mm = hashmap_get(m->machines_by_leader, pidref); |
| 35 |
if (mm) |
| 36 |
break; |
| 37 |
|
| 38 |
/* Then look for the unit */ |
| 39 |
_cleanup_free_ char *unit = NULL; |
| 40 |
if (cg_pidref_get_unit(pidref, &unit) >= 0) { |
| 41 |
mm = hashmap_get(m->machines_by_unit, unit); |
| 42 |
if (mm) |
| 43 |
break; |
| 44 |
} |
| 45 |
|
| 46 |
/* Maybe this process is in per-user unit? If so, let's go up the process tree, and check |
| 47 |
* that, we should eventually hit PID 1 of the container tree, which we should be able to |
| 48 |
* recognize. */ |
| 49 |
_cleanup_(pidref_done) PidRef parent = PIDREF_NULL; |
| 50 |
if (pidref_get_ppid_as_pidref(pidref, &parent) < 0) |
| 51 |
break; |
| 52 |
|
| 53 |
pidref_done(¤t); |
| 54 |
current = TAKE_PIDREF(parent); |
| 55 |
pidref = ¤t; |
| 56 |
} |
| 57 |
|
| 58 |
*ret = mm; |
| 59 |
return !!mm; |
| 60 |
} |
| 61 |
|
| 62 |
int manager_add_machine(Manager *m, const char *name, Machine **ret) { |
| 63 |
Machine *machine; |
| 64 |
int r; |
| 65 |
|
| 66 |
assert(m); |
| 67 |
assert(name); |
| 68 |
|
| 69 |
machine = hashmap_get(m->machines, name); |
| 70 |
if (!machine) { |
| 71 |
r = machine_new(_MACHINE_CLASS_INVALID, name, &machine); |
| 72 |
if (r < 0) |
| 73 |
return r; |
| 74 |
|
| 75 |
r = machine_link(m, machine); |
| 76 |
if (r < 0) { |
| 77 |
machine_free(machine); |
| 78 |
return r; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
if (ret) |
| 83 |
*ret = machine; |
| 84 |
|
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
int manager_find_machine_for_uid(Manager *m, uid_t uid, Machine **ret_machine, uid_t *ret_internal_uid) { |
| 89 |
Machine *machine; |
| 90 |
int r; |
| 91 |
|
| 92 |
assert(m); |
| 93 |
assert(uid_is_valid(uid)); |
| 94 |
|
| 95 |
/* Finds the machine for the specified host UID and returns it along with the UID translated into the |
| 96 |
* internal UID inside the machine */ |
| 97 |
|
| 98 |
HASHMAP_FOREACH(machine, m->machines) { |
| 99 |
uid_t converted; |
| 100 |
|
| 101 |
r = machine_owns_uid(machine, uid, &converted); |
| 102 |
if (r < 0) |
| 103 |
return r; |
| 104 |
if (r) { |
| 105 |
if (ret_machine) |
| 106 |
*ret_machine = machine; |
| 107 |
|
| 108 |
if (ret_internal_uid) |
| 109 |
*ret_internal_uid = converted; |
| 110 |
|
| 111 |
return true; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
if (ret_machine) |
| 116 |
*ret_machine = NULL; |
| 117 |
if (ret_internal_uid) |
| 118 |
*ret_internal_uid = UID_INVALID; |
| 119 |
|
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
int manager_find_machine_for_gid(Manager *m, gid_t gid, Machine **ret_machine, gid_t *ret_internal_gid) { |
| 124 |
Machine *machine; |
| 125 |
int r; |
| 126 |
|
| 127 |
assert(m); |
| 128 |
assert(gid_is_valid(gid)); |
| 129 |
|
| 130 |
HASHMAP_FOREACH(machine, m->machines) { |
| 131 |
gid_t converted; |
| 132 |
|
| 133 |
r = machine_owns_gid(machine, gid, &converted); |
| 134 |
if (r < 0) |
| 135 |
return r; |
| 136 |
if (r) { |
| 137 |
if (ret_machine) |
| 138 |
*ret_machine = machine; |
| 139 |
|
| 140 |
if (ret_internal_gid) |
| 141 |
*ret_internal_gid = converted; |
| 142 |
|
| 143 |
return true; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if (ret_machine) |
| 148 |
*ret_machine = NULL; |
| 149 |
if (ret_internal_gid) |
| 150 |
*ret_internal_gid = GID_INVALID; |
| 151 |
|
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
void manager_gc(Manager *m, bool drop_not_started) { |
| 156 |
Machine *machine; |
| 157 |
|
| 158 |
assert(m); |
| 159 |
|
| 160 |
while ((machine = LIST_POP(gc_queue, m->machine_gc_queue))) { |
| 161 |
machine->in_gc_queue = false; |
| 162 |
|
| 163 |
/* First, if we are not closing yet, initiate stopping */ |
| 164 |
if (machine_may_gc(machine, drop_not_started) && |
| 165 |
machine_get_state(machine) != MACHINE_CLOSING) { |
| 166 |
log_debug("Stopping machine '%s' due to GC.", machine->name); |
| 167 |
machine_stop(machine); |
| 168 |
} |
| 169 |
|
| 170 |
/* Now, the stop probably made this referenced |
| 171 |
* again, but if it didn't, then it's time to let it |
| 172 |
* go entirely. */ |
| 173 |
if (machine_may_gc(machine, drop_not_started)) { |
| 174 |
log_debug("Finalizing machine '%s' due to GC.", machine->name); |
| 175 |
machine_finalize(machine); |
| 176 |
machine_free(machine); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
static int on_deferred_gc(sd_event_source *s, void *userdata) { |
| 182 |
manager_gc(userdata, /* drop_not_started= */ true); |
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
void manager_enqueue_gc(Manager *m) { |
| 187 |
int r; |
| 188 |
|
| 189 |
assert(m); |
| 190 |
|
| 191 |
if (m->deferred_gc_event_source) { |
| 192 |
r = sd_event_source_set_enabled(m->deferred_gc_event_source, SD_EVENT_ONESHOT); |
| 193 |
if (r < 0) |
| 194 |
log_warning_errno(r, "Failed to enable GC event source, ignoring: %m"); |
| 195 |
|
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
r = sd_event_add_defer(m->event, &m->deferred_gc_event_source, on_deferred_gc, m); |
| 200 |
if (r < 0) |
| 201 |
return (void) log_warning_errno(r, "Failed to allocate GC event source, ignoring: %m"); |
| 202 |
|
| 203 |
r = sd_event_source_set_priority(m->deferred_gc_event_source, SD_EVENT_PRIORITY_IDLE); |
| 204 |
if (r < 0) |
| 205 |
log_warning_errno(r, "Failed to tweak priority of event source, ignoring: %m"); |
| 206 |
|
| 207 |
(void) sd_event_source_set_description(m->deferred_gc_event_source, "deferred-gc"); |
| 208 |
} |
| 209 |
|
| 210 |
int machine_get_addresses(Machine *machine, struct local_address **ret_addresses) { |
| 211 |
assert(machine); |
| 212 |
assert(ret_addresses); |
| 213 |
|
| 214 |
switch (machine->class) { |
| 215 |
|
| 216 |
case MACHINE_HOST: { |
| 217 |
_cleanup_free_ struct local_address *addresses = NULL; |
| 218 |
int n; |
| 219 |
|
| 220 |
n = local_addresses(/* context= */ NULL, /* ifindex= */ 0, AF_UNSPEC, &addresses); |
| 221 |
if (n < 0) |
| 222 |
return log_debug_errno(n, "Failed to get local addresses: %m"); |
| 223 |
|
| 224 |
*ret_addresses = TAKE_PTR(addresses); |
| 225 |
return n; |
| 226 |
} |
| 227 |
|
| 228 |
case MACHINE_CONTAINER: { |
| 229 |
_cleanup_(pidref_done) PidRef child = PIDREF_NULL; |
| 230 |
_cleanup_close_pair_ int pair[2] = EBADF_PAIR; |
| 231 |
_cleanup_close_ int netns_fd = -EBADF; |
| 232 |
int r; |
| 233 |
|
| 234 |
r = pidref_in_same_namespace(/* pid1= */ NULL, &machine->leader, NAMESPACE_NET); |
| 235 |
if (r < 0) |
| 236 |
return log_debug_errno(r, "Failed to check if container has private network: %m"); |
| 237 |
if (r > 0) |
| 238 |
return -ENONET; |
| 239 |
|
| 240 |
r = pidref_namespace_open(&machine->leader, |
| 241 |
/* ret_pidns_fd= */ NULL, |
| 242 |
/* ret_mntns_fd= */ NULL, |
| 243 |
&netns_fd, |
| 244 |
/* ret_userns_fd= */ NULL, |
| 245 |
/* ret_root_fd= */ NULL); |
| 246 |
if (r < 0) |
| 247 |
return log_debug_errno(r, "Failed to open namespace: %m"); |
| 248 |
|
| 249 |
if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, pair) < 0) |
| 250 |
return log_debug_errno(errno, "Failed to call socketpair(): %m"); |
| 251 |
|
| 252 |
r = namespace_fork("(sd-addrns)", |
| 253 |
"(sd-addr)", |
| 254 |
FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL, |
| 255 |
/* pidns_fd= */ -EBADF, |
| 256 |
/* mntns_fd= */ -EBADF, |
| 257 |
netns_fd, |
| 258 |
/* userns_fd= */ -EBADF, |
| 259 |
/* root_fd= */ -EBADF, |
| 260 |
&child); |
| 261 |
if (r < 0) |
| 262 |
return log_debug_errno(r, "Failed to fork(): %m"); |
| 263 |
if (r == 0) { |
| 264 |
_cleanup_free_ struct local_address *addresses = NULL; |
| 265 |
|
| 266 |
pair[0] = safe_close(pair[0]); |
| 267 |
|
| 268 |
int n = local_addresses(/* context= */ NULL, /* ifindex= */ 0, AF_UNSPEC, &addresses); |
| 269 |
if (n < 0) { |
| 270 |
log_debug_errno(n, "Failed to get local addresses: %m"); |
| 271 |
_exit(EXIT_FAILURE); |
| 272 |
} |
| 273 |
|
| 274 |
FOREACH_ARRAY(a, addresses, n) { |
| 275 |
r = write(pair[1], a, sizeof(*a)); |
| 276 |
if (r < 0) { |
| 277 |
log_debug_errno(errno, "Failed to write to socket: %m"); |
| 278 |
_exit(EXIT_FAILURE); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
_exit(EXIT_SUCCESS); |
| 283 |
} |
| 284 |
|
| 285 |
pair[1] = safe_close(pair[1]); |
| 286 |
|
| 287 |
_cleanup_free_ struct local_address *list = NULL; |
| 288 |
size_t n_list = 0; |
| 289 |
|
| 290 |
for (;;) { |
| 291 |
ssize_t n; |
| 292 |
struct local_address la; |
| 293 |
|
| 294 |
n = read(pair[0], &la, sizeof(la)); |
| 295 |
if (n < 0) |
| 296 |
return log_debug_errno(errno, "Failed to read from socket(): %m"); |
| 297 |
if (n == 0) |
| 298 |
break; |
| 299 |
if ((size_t) n < sizeof(la)) |
| 300 |
return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Received unexpectedly short message"); |
| 301 |
|
| 302 |
r = add_local_address(&list, |
| 303 |
&n_list, |
| 304 |
la.ifindex, |
| 305 |
la.scope, |
| 306 |
la.family, |
| 307 |
&la.address); |
| 308 |
if (r < 0) |
| 309 |
return log_debug_errno(r, "Failed to add local address: %m"); |
| 310 |
} |
| 311 |
|
| 312 |
r = pidref_wait_for_terminate_and_check("(sd-addrns)", &child, /* flags= */ 0); |
| 313 |
if (r < 0) |
| 314 |
return log_debug_errno(r, "Failed to wait for child: %m"); |
| 315 |
if (r != EXIT_SUCCESS) |
| 316 |
return log_debug_errno(SYNTHETIC_ERRNO(ESHUTDOWN), "Child died abnormally"); |
| 317 |
|
| 318 |
*ret_addresses = TAKE_PTR(list); |
| 319 |
return (int) n_list; |
| 320 |
} |
| 321 |
|
| 322 |
default: |
| 323 |
return -EOPNOTSUPP; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
#define EXIT_NOT_FOUND 2 |
| 328 |
|
| 329 |
int machine_get_os_release(Machine *machine, char ***ret_os_release) { |
| 330 |
_cleanup_strv_free_ char **l = NULL; |
| 331 |
int r; |
| 332 |
|
| 333 |
assert(machine); |
| 334 |
assert(ret_os_release); |
| 335 |
|
| 336 |
switch (machine->class) { |
| 337 |
|
| 338 |
case MACHINE_HOST: |
| 339 |
r = load_os_release_pairs(/* root= */ NULL, &l); |
| 340 |
if (r < 0) |
| 341 |
return log_debug_errno(r, "Failed to load OS release information: %m"); |
| 342 |
|
| 343 |
break; |
| 344 |
|
| 345 |
case MACHINE_CONTAINER: { |
| 346 |
_cleanup_close_ int mntns_fd = -EBADF, root_fd = -EBADF, pidns_fd = -EBADF; |
| 347 |
_cleanup_(pidref_done) PidRef child = PIDREF_NULL; |
| 348 |
_cleanup_close_pair_ int pair[2] = EBADF_PAIR; |
| 349 |
_cleanup_fclose_ FILE *f = NULL; |
| 350 |
|
| 351 |
r = pidref_namespace_open(&machine->leader, |
| 352 |
&pidns_fd, |
| 353 |
&mntns_fd, |
| 354 |
/* ret_netns_fd= */ NULL, |
| 355 |
/* ret_userns_fd= */ NULL, |
| 356 |
&root_fd); |
| 357 |
if (r < 0) |
| 358 |
return log_debug_errno(r, "Failed to open namespace: %m"); |
| 359 |
|
| 360 |
if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, pair) < 0) |
| 361 |
return log_debug_errno(errno, "Failed to call socketpair(): %m"); |
| 362 |
|
| 363 |
r = namespace_fork("(sd-osrelns)", |
| 364 |
"(sd-osrel)", |
| 365 |
FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL, |
| 366 |
pidns_fd, |
| 367 |
mntns_fd, |
| 368 |
/* netns_fd= */ -EBADF, |
| 369 |
/* userns_fd= */ -EBADF, |
| 370 |
root_fd, |
| 371 |
&child); |
| 372 |
if (r < 0) |
| 373 |
return log_debug_errno(r, "Failed to fork(): %m"); |
| 374 |
if (r == 0) { |
| 375 |
_cleanup_close_ int fd = -EBADF; |
| 376 |
|
| 377 |
pair[0] = safe_close(pair[0]); |
| 378 |
|
| 379 |
r = open_os_release(/* root= */ NULL, /* ret_path= */ NULL, &fd); |
| 380 |
if (r == -ENOENT) |
| 381 |
_exit(EXIT_NOT_FOUND); |
| 382 |
if (r < 0) { |
| 383 |
log_debug_errno(r, "Failed to read OS release: %m"); |
| 384 |
_exit(EXIT_FAILURE); |
| 385 |
} |
| 386 |
|
| 387 |
r = copy_bytes(fd, pair[1], UINT64_MAX, /* copy_flags= */ 0); |
| 388 |
if (r < 0) { |
| 389 |
log_debug_errno(r, "Failed to write to fd: %m"); |
| 390 |
_exit(EXIT_FAILURE); |
| 391 |
} |
| 392 |
|
| 393 |
_exit(EXIT_SUCCESS); |
| 394 |
} |
| 395 |
|
| 396 |
pair[1] = safe_close(pair[1]); |
| 397 |
|
| 398 |
f = take_fdopen(&pair[0], "r"); |
| 399 |
if (!f) |
| 400 |
return log_debug_errno(errno, "Failed to fdopen(): %m"); |
| 401 |
|
| 402 |
r = load_env_file_pairs(f, "/etc/os-release", &l); |
| 403 |
if (r < 0) |
| 404 |
return log_debug_errno(r, "Failed to load OS release information: %m"); |
| 405 |
|
| 406 |
r = pidref_wait_for_terminate_and_check("(sd-osrelns)", &child, /* flags= */ 0); |
| 407 |
if (r < 0) |
| 408 |
return log_debug_errno(r, "Failed to wait for child: %m"); |
| 409 |
if (r == EXIT_NOT_FOUND) |
| 410 |
return -ENOENT; |
| 411 |
if (r != EXIT_SUCCESS) |
| 412 |
return log_debug_errno(SYNTHETIC_ERRNO(ESHUTDOWN), "Child died abnormally"); |
| 413 |
|
| 414 |
break; |
| 415 |
} |
| 416 |
|
| 417 |
default: |
| 418 |
return -EOPNOTSUPP; |
| 419 |
} |
| 420 |
|
| 421 |
*ret_os_release = TAKE_PTR(l); |
| 422 |
return 0; |
| 423 |
} |
| 424 |
|
| 425 |
static int image_flush_cache(sd_event_source *s, void *userdata) { |
| 426 |
Manager *m = ASSERT_PTR(userdata); |
| 427 |
|
| 428 |
assert(s); |
| 429 |
|
| 430 |
hashmap_clear(m->image_cache); |
| 431 |
return 0; |
| 432 |
} |
| 433 |
|
| 434 |
int manager_acquire_image(Manager *m, const char *name, Image **ret) { |
| 435 |
int r; |
| 436 |
|
| 437 |
assert(m); |
| 438 |
assert(name); |
| 439 |
|
| 440 |
Image *existing = hashmap_get(m->image_cache, name); |
| 441 |
if (existing) { |
| 442 |
if (ret) |
| 443 |
*ret = existing; |
| 444 |
return 0; |
| 445 |
} |
| 446 |
|
| 447 |
if (!m->image_cache_defer_event) { |
| 448 |
r = sd_event_add_defer(m->event, &m->image_cache_defer_event, image_flush_cache, m); |
| 449 |
if (r < 0) |
| 450 |
return log_debug_errno(r, "Failed to add deferred event: %m"); |
| 451 |
|
| 452 |
r = sd_event_source_set_priority(m->image_cache_defer_event, SD_EVENT_PRIORITY_IDLE); |
| 453 |
if (r < 0) |
| 454 |
return log_debug_errno(r, "Failed to set source priority for event: %m"); |
| 455 |
} |
| 456 |
|
| 457 |
r = sd_event_source_set_enabled(m->image_cache_defer_event, SD_EVENT_ONESHOT); |
| 458 |
if (r < 0) |
| 459 |
return log_debug_errno(r, "Failed to enable source: %m") ; |
| 460 |
|
| 461 |
_cleanup_(image_unrefp) Image *image = NULL; |
| 462 |
r = image_find(m->runtime_scope, IMAGE_MACHINE, name, NULL, &image); |
| 463 |
if (r < 0) |
| 464 |
return log_debug_errno(r, "Failed to find image: %m"); |
| 465 |
|
| 466 |
image->userdata = m; |
| 467 |
|
| 468 |
r = hashmap_ensure_put(&m->image_cache, &image_hash_ops, image->name, image); |
| 469 |
if (r < 0) |
| 470 |
return r; |
| 471 |
|
| 472 |
if (ret) |
| 473 |
*ret = image; |
| 474 |
|
| 475 |
TAKE_PTR(image); |
| 476 |
return 0; |
| 477 |
} |
| 478 |
|
| 479 |
int rename_image_and_update_cache(Manager *m, Image *image, const char* new_name) { |
| 480 |
int r; |
| 481 |
|
| 482 |
assert(m); |
| 483 |
assert(image); |
| 484 |
assert(new_name); |
| 485 |
|
| 486 |
/* The image is cached with its name, hence it is necessary to remove from the cache before renaming. */ |
| 487 |
assert_se(hashmap_remove_value(m->image_cache, image->name, image)); |
| 488 |
|
| 489 |
r = image_rename(image, new_name, m->runtime_scope); |
| 490 |
if (r < 0) { |
| 491 |
image = image_unref(image); |
| 492 |
return r; |
| 493 |
} |
| 494 |
|
| 495 |
/* Then save the object again in the cache. */ |
| 496 |
r = hashmap_put(m->image_cache, image->name, image); |
| 497 |
if (r < 0) { |
| 498 |
image = image_unref(image); |
| 499 |
log_debug_errno(r, "Failed to put renamed image into cache, ignoring: %m"); |
| 500 |
} |
| 501 |
|
| 502 |
return 0; |
| 503 |
} |