| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-id128.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "bpf-restrict-fsaccess.h" |
| 7 |
#include "dbus.h" |
| 8 |
#include "dynamic-user.h" |
| 9 |
#include "fd-util.h" |
| 10 |
#include "fdset.h" |
| 11 |
#include "fileio.h" |
| 12 |
#include "format-util.h" |
| 13 |
#include "glyph-util.h" |
| 14 |
#include "hashmap.h" |
| 15 |
#include "initrd-util.h" |
| 16 |
#include "job.h" |
| 17 |
#include "manager.h" |
| 18 |
#include "manager-serialize.h" |
| 19 |
#include "parse-util.h" |
| 20 |
#include "serialize.h" |
| 21 |
#include "set.h" |
| 22 |
#include "string-util.h" |
| 23 |
#include "strv.h" |
| 24 |
#include "syslog-util.h" |
| 25 |
#include "unit.h" |
| 26 |
#include "unit-name.h" |
| 27 |
#include "unit-serialize.h" |
| 28 |
#include "user-util.h" |
| 29 |
#include "varlink.h" |
| 30 |
#include "varlink-serialize.h" |
| 31 |
|
| 32 |
int manager_open_serialization(Manager *m, FILE **ret_f) { |
| 33 |
assert(ret_f); |
| 34 |
|
| 35 |
return open_serialization_file("systemd-state", ret_f); |
| 36 |
} |
| 37 |
|
| 38 |
static bool manager_timestamp_shall_serialize(ManagerObjective o, ManagerTimestamp t) { |
| 39 |
if (!in_initrd() && o != MANAGER_SOFT_REBOOT) |
| 40 |
return true; |
| 41 |
|
| 42 |
/* The following timestamps only apply to the host system (or first boot in case of soft-reboot), |
| 43 |
* hence only serialize them there. */ |
| 44 |
return !IN_SET(t, |
| 45 |
MANAGER_TIMESTAMP_USERSPACE, MANAGER_TIMESTAMP_FINISH, |
| 46 |
MANAGER_TIMESTAMP_SECURITY_START, MANAGER_TIMESTAMP_SECURITY_FINISH, |
| 47 |
MANAGER_TIMESTAMP_GENERATORS_START, MANAGER_TIMESTAMP_GENERATORS_FINISH, |
| 48 |
MANAGER_TIMESTAMP_UNITS_LOAD_START, MANAGER_TIMESTAMP_UNITS_LOAD_FINISH); |
| 49 |
} |
| 50 |
|
| 51 |
static void manager_serialize_uid_refs_internal( |
| 52 |
FILE *f, |
| 53 |
Hashmap *uid_refs, |
| 54 |
const char *field_name) { |
| 55 |
|
| 56 |
void *p, *k; |
| 57 |
|
| 58 |
assert(f); |
| 59 |
assert(field_name); |
| 60 |
|
| 61 |
/* Serialize the UID reference table. Or actually, just the IPC destruction flag of it, as |
| 62 |
* the actual counter of it is better rebuild after a reload/reexec. */ |
| 63 |
|
| 64 |
HASHMAP_FOREACH_KEY(p, k, uid_refs) { |
| 65 |
uint32_t c; |
| 66 |
uid_t uid; |
| 67 |
|
| 68 |
uid = PTR_TO_UID(k); |
| 69 |
c = PTR_TO_UINT32(p); |
| 70 |
|
| 71 |
if (!(c & DESTROY_IPC_FLAG)) |
| 72 |
continue; |
| 73 |
|
| 74 |
(void) serialize_item_format(f, field_name, UID_FMT, uid); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
static void manager_serialize_uid_refs(Manager *m, FILE *f) { |
| 79 |
manager_serialize_uid_refs_internal(f, m->uid_refs, "destroy-ipc-uid"); |
| 80 |
} |
| 81 |
|
| 82 |
static void manager_serialize_gid_refs(Manager *m, FILE *f) { |
| 83 |
manager_serialize_uid_refs_internal(f, m->gid_refs, "destroy-ipc-gid"); |
| 84 |
} |
| 85 |
|
| 86 |
int manager_serialize( |
| 87 |
Manager *m, |
| 88 |
FILE *f, |
| 89 |
FDSet *fds, |
| 90 |
bool switching_root) { |
| 91 |
|
| 92 |
const char *t; |
| 93 |
Unit *u; |
| 94 |
int r; |
| 95 |
|
| 96 |
assert(m); |
| 97 |
assert(f); |
| 98 |
assert(fds); |
| 99 |
|
| 100 |
_cleanup_(manager_reloading_stopp) _unused_ Manager *reloading = manager_reloading_start(m); |
| 101 |
|
| 102 |
(void) serialize_item_format(f, "last-transaction-id", "%" PRIu64, m->last_transaction_id); |
| 103 |
|
| 104 |
(void) serialize_item_format(f, "current-job-id", "%" PRIu32, m->current_job_id); |
| 105 |
(void) serialize_item_format(f, "n-installed-jobs", "%u", m->n_installed_jobs); |
| 106 |
(void) serialize_item_format(f, "n-failed-jobs", "%u", m->n_failed_jobs); |
| 107 |
(void) serialize_bool(f, "taint-logged", m->taint_logged); |
| 108 |
(void) serialize_bool(f, "service-watchdogs", m->service_watchdogs); |
| 109 |
|
| 110 |
if (m->show_status_overridden != _SHOW_STATUS_INVALID) |
| 111 |
(void) serialize_item(f, "show-status-overridden", |
| 112 |
show_status_to_string(m->show_status_overridden)); |
| 113 |
|
| 114 |
if (m->log_level_overridden) |
| 115 |
(void) serialize_item_format(f, "log-level-override", "%i", log_get_max_level()); |
| 116 |
if (m->log_target_overridden) |
| 117 |
(void) serialize_item(f, "log-target-override", log_target_to_string(log_get_target())); |
| 118 |
|
| 119 |
(void) serialize_usec(f, "runtime-watchdog-overridden", m->watchdog_overridden[WATCHDOG_RUNTIME]); |
| 120 |
(void) serialize_usec(f, "reboot-watchdog-overridden", m->watchdog_overridden[WATCHDOG_REBOOT]); |
| 121 |
(void) serialize_usec(f, "kexec-watchdog-overridden", m->watchdog_overridden[WATCHDOG_KEXEC]); |
| 122 |
(void) serialize_usec(f, "pretimeout-watchdog-overridden", m->watchdog_overridden[WATCHDOG_PRETIMEOUT]); |
| 123 |
(void) serialize_item(f, "pretimeout-watchdog-governor-overridden", m->watchdog_pretimeout_governor_overridden); |
| 124 |
|
| 125 |
(void) serialize_item(f, "previous-objective", manager_objective_to_string(m->objective)); |
| 126 |
(void) serialize_item_format(f, "soft-reboots-count", "%u", m->soft_reboots_count); |
| 127 |
(void) serialize_item_format(f, "fd-store-upstream-next-index", "%" PRIu64, m->fd_store_upstream_next_index); |
| 128 |
|
| 129 |
for (ManagerTimestamp q = 0; q < _MANAGER_TIMESTAMP_MAX; q++) { |
| 130 |
_cleanup_free_ char *joined = NULL; |
| 131 |
|
| 132 |
if (!manager_timestamp_shall_serialize(m->objective, q)) |
| 133 |
continue; |
| 134 |
|
| 135 |
joined = strjoin(manager_timestamp_to_string(q), "-timestamp"); |
| 136 |
if (!joined) |
| 137 |
return log_oom(); |
| 138 |
|
| 139 |
(void) serialize_dual_timestamp(f, joined, m->timestamps + q); |
| 140 |
} |
| 141 |
|
| 142 |
if (!switching_root) |
| 143 |
(void) serialize_strv(f, "env", m->client_environment); |
| 144 |
|
| 145 |
if (m->notify_fd >= 0) { |
| 146 |
r = serialize_fd(f, fds, "notify-fd", m->notify_fd); |
| 147 |
if (r < 0) |
| 148 |
return r; |
| 149 |
|
| 150 |
(void) serialize_item(f, "notify-socket", m->notify_socket); |
| 151 |
} |
| 152 |
|
| 153 |
if (m->user_lookup_fds[0] >= 0) { |
| 154 |
r = serialize_fd_many(f, fds, "user-lookup", m->user_lookup_fds, 2); |
| 155 |
if (r < 0) |
| 156 |
return r; |
| 157 |
} |
| 158 |
|
| 159 |
if (m->handoff_timestamp_fds[0] >= 0) { |
| 160 |
r = serialize_fd_many(f, fds, "handoff-timestamp-fds", m->handoff_timestamp_fds, 2); |
| 161 |
if (r < 0) |
| 162 |
return r; |
| 163 |
} |
| 164 |
|
| 165 |
(void) serialize_ratelimit(f, "dump-ratelimit", &m->dump_ratelimit); |
| 166 |
(void) serialize_ratelimit(f, "reload-reexec-ratelimit", &m->reload_reexec_ratelimit); |
| 167 |
(void) serialize_ratelimit(f, "event-loop-ratelimit", &m->event_loop_ratelimit); |
| 168 |
|
| 169 |
(void) serialize_id128(f, "bus-id", m->bus_id); |
| 170 |
bus_track_serialize(m->subscribed, f, "subscribed"); |
| 171 |
|
| 172 |
r = dynamic_user_serialize(m, f, fds); |
| 173 |
if (r < 0) |
| 174 |
return r; |
| 175 |
|
| 176 |
manager_serialize_uid_refs(m, f); |
| 177 |
manager_serialize_gid_refs(m, f); |
| 178 |
|
| 179 |
r = exec_shared_runtime_serialize(m, f, fds); |
| 180 |
if (r < 0) |
| 181 |
return r; |
| 182 |
|
| 183 |
r = varlink_server_serialize(m->varlink_server, /* name = */ NULL, f, fds); |
| 184 |
if (r < 0) |
| 185 |
return r; |
| 186 |
|
| 187 |
r = varlink_server_serialize(m->metrics_varlink_server, "metrics", f, fds); |
| 188 |
if (r < 0) |
| 189 |
return r; |
| 190 |
|
| 191 |
r = bpf_restrict_fsaccess_serialize(m, f, fds); |
| 192 |
if (r < 0) |
| 193 |
return r; |
| 194 |
|
| 195 |
(void) fputc('\n', f); |
| 196 |
|
| 197 |
HASHMAP_FOREACH_KEY(u, t, m->units) { |
| 198 |
if (u->id != t) |
| 199 |
continue; |
| 200 |
|
| 201 |
r = unit_serialize_state(u, f, fds, switching_root); |
| 202 |
if (r < 0) |
| 203 |
return r; |
| 204 |
} |
| 205 |
|
| 206 |
r = bus_fdset_add_all(m, fds); |
| 207 |
if (r < 0) |
| 208 |
return log_error_errno(r, "Failed to add bus sockets to serialization: %m"); |
| 209 |
|
| 210 |
return 0; |
| 211 |
} |
| 212 |
|
| 213 |
static int manager_collect_serialized_unit_names(FILE *f, Set **ret) { |
| 214 |
_cleanup_set_free_ Set *serialized_units = NULL; |
| 215 |
off_t offset; |
| 216 |
int r; |
| 217 |
|
| 218 |
assert(f); |
| 219 |
assert(ret); |
| 220 |
|
| 221 |
offset = ftello(f); |
| 222 |
if (offset < 0) |
| 223 |
return log_error_errno(errno, "Failed to determine serialization offset: %m"); |
| 224 |
|
| 225 |
for (;;) { |
| 226 |
_cleanup_free_ char *line = NULL; |
| 227 |
|
| 228 |
r = read_stripped_line(f, LONG_LINE_MAX, &line); |
| 229 |
if (r < 0) |
| 230 |
return log_error_errno(r, "Failed to read serialization line: %m"); |
| 231 |
if (r == 0) |
| 232 |
break; |
| 233 |
|
| 234 |
r = set_ensure_consume(&serialized_units, &string_hash_ops_free, TAKE_PTR(line)); |
| 235 |
if (r < 0) |
| 236 |
return log_oom(); |
| 237 |
|
| 238 |
r = unit_deserialize_state_skip(f); |
| 239 |
if (r < 0) |
| 240 |
return r; |
| 241 |
} |
| 242 |
|
| 243 |
if (fseeko(f, offset, SEEK_SET) < 0) |
| 244 |
return log_error_errno(errno, "Failed to reset serialization offset: %m"); |
| 245 |
|
| 246 |
*ret = TAKE_PTR(serialized_units); |
| 247 |
return 0; |
| 248 |
} |
| 249 |
|
| 250 |
static int manager_synthesize_orphaned_unit( |
| 251 |
Manager *m, |
| 252 |
const char *original_name, |
| 253 |
const char *canonical_name, |
| 254 |
FILE *f, |
| 255 |
FDSet *fds) { |
| 256 |
|
| 257 |
_cleanup_(unit_freep) Unit *orphan = NULL; |
| 258 |
_cleanup_free_ char *orphaned_name = NULL; |
| 259 |
sd_id128_t rnd; |
| 260 |
UnitType t; |
| 261 |
int r; |
| 262 |
|
| 263 |
assert(m); |
| 264 |
assert(original_name); |
| 265 |
assert(canonical_name); |
| 266 |
assert(f); |
| 267 |
assert(fds); |
| 268 |
|
| 269 |
t = unit_name_to_type(original_name); |
| 270 |
if (t < 0) |
| 271 |
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), |
| 272 |
"Cannot synthesize unit for '%s' (overridden by alias to '%s'): invalid unit type. Skipping stale state.", |
| 273 |
original_name, canonical_name); |
| 274 |
|
| 275 |
/* Only transition units that track external resources and can be renamed/know aliases, forget internal ones (eg: timers) */ |
| 276 |
if (!unit_vtable[t]->track_orphaned) |
| 277 |
return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 278 |
"Cannot synthesize unit for '%s' (overridden by alias to '%s'): unsupported unit type. Skipping stale state.", |
| 279 |
original_name, canonical_name); |
| 280 |
|
| 281 |
/* Use a naming convention with an "orphaned-" prefix to make it clear at a glance that these units |
| 282 |
* were synthesized to adopt resources from a now-aliased unit */ |
| 283 |
r = sd_id128_randomize(&rnd); |
| 284 |
if (r < 0) |
| 285 |
return log_warning_errno(r, "Failed to generate random ID for orphan unit: %m"); |
| 286 |
|
| 287 |
if (asprintf(&orphaned_name, "orphaned-r" SD_ID128_FORMAT_STR ".%s", |
| 288 |
SD_ID128_FORMAT_VAL(rnd), unit_type_to_string(t)) < 0) |
| 289 |
return log_oom(); |
| 290 |
|
| 291 |
r = unit_new_for_name(m, unit_vtable[t]->object_size, orphaned_name, &orphan); |
| 292 |
if (r < 0) |
| 293 |
return log_warning_errno(r, "Failed to allocate orphan unit '%s': %m", orphaned_name); |
| 294 |
|
| 295 |
/* Force the state as we know the unit file is gone, so it will hang around as long as resources are |
| 296 |
* active, and then it will be automatically collected */ |
| 297 |
orphan->load_state = UNIT_NOT_FOUND; |
| 298 |
/* Ensure the orphan also gets garbage-collected when it ends up in 'failed' state. */ |
| 299 |
orphan->collect_mode = COLLECT_INACTIVE_OR_FAILED; |
| 300 |
|
| 301 |
_cleanup_free_ char *description = strjoin("Orphaned resources adopted from aliased unit ", original_name); |
| 302 |
if (!description) |
| 303 |
return log_oom(); |
| 304 |
|
| 305 |
r = unit_deserialize_state(orphan, f, fds); |
| 306 |
if (r < 0) |
| 307 |
return log_notice_errno(r, "Failed to deserialize state into orphan unit '%s': %m", orphaned_name); |
| 308 |
|
| 309 |
/* From this point on the serialized stream for this unit has been fully consumed, so avoid failing. */ |
| 310 |
|
| 311 |
log_warning("Unit file for '%s' was overridden by a symlink to '%s'. Synthesized orphan unit '%s' to retain tracking of the previous unit's processes.", |
| 312 |
original_name, canonical_name, orphaned_name); |
| 313 |
|
| 314 |
/* Override Description= so it's clear what this is for and can be traced back to the original. */ |
| 315 |
free_and_replace(orphan->description, description); |
| 316 |
|
| 317 |
/* Any jobs reinstalled from the deserialized state targeted the original unit. Most of them |
| 318 |
* (start, restart, reload, ...) make no sense for the synthesized orphan, especially after a |
| 319 |
* service-to-scope conversion, so cancel them. JOB_STOP is the exception: a stop request that was |
| 320 |
* already pending on the original unit is exactly what we want to keep around, since it'll cause |
| 321 |
* the synthesized unit to just go away immediately. */ |
| 322 |
if (orphan->job && orphan->job->type != JOB_STOP) |
| 323 |
job_finish_and_invalidate(orphan->job, JOB_CANCELED, /* recursive= */ false, /* already= */ false); |
| 324 |
if (orphan->nop_job) |
| 325 |
job_finish_and_invalidate(orphan->nop_job, JOB_CANCELED, /* recursive= */ false, /* already= */ false); |
| 326 |
|
| 327 |
TAKE_PTR(orphan); |
| 328 |
return 0; |
| 329 |
} |
| 330 |
|
| 331 |
static int manager_deserialize_one_unit( |
| 332 |
Manager *m, |
| 333 |
const char *name, |
| 334 |
FILE *f, |
| 335 |
FDSet *fds, |
| 336 |
Set *serialized_units) { |
| 337 |
|
| 338 |
Unit *u; |
| 339 |
int r; |
| 340 |
|
| 341 |
r = manager_load_unit(m, name, NULL, NULL, &u); |
| 342 |
if (r == -ENOMEM) |
| 343 |
return log_oom(); |
| 344 |
if (r < 0) |
| 345 |
return log_notice_errno(r, "Failed to load unit \"%s\", skipping deserialization: %m", name); |
| 346 |
|
| 347 |
if (!streq(u->id, name) && |
| 348 |
set_contains(serialized_units, u->id)) { |
| 349 |
/* |
| 350 |
* The unit from the state file (name) resolved to a different canonical unit (u->id), and |
| 351 |
* the canonical unit also has its own state entry. |
| 352 |
* |
| 353 |
* This means the state entry for the unit name is stale. That is, when the state was |
| 354 |
* serialized, the name referred to an independent unit, but it now resolves as an alias to |
| 355 |
* the canonical unit. Deserializing it would overwrite the canonical unit's own serialized |
| 356 |
* state, and thus corrupt its live runtime state. |
| 357 |
* |
| 358 |
* It is very important to note that this only affects units that were independent when the |
| 359 |
* state file was written, but are now aliases (either because a reload created the symlink, |
| 360 |
* or the symlink existed but this is the first reload). Normal aliases that were already |
| 361 |
* aliases during the most recent serialization are filtered out in manager_serialize(), so |
| 362 |
* they never appear in the state file. |
| 363 |
* |
| 364 |
* If the canonical unit does not have its own state entry, then this is instead a rename or |
| 365 |
* canonical ID change, and this state entry is the only state we have for the unit. In that |
| 366 |
* case we must preserve it. After doing so, we insert the canonical unit's ID into the set |
| 367 |
* so that any further aliases resolving to the same unit are skipped. |
| 368 |
* |
| 369 |
* The serialized data represents the old, independent unit. Deserializing this stale state |
| 370 |
* onto the canonical unit would corrupt its live state. Instead, we synthesize a new unloaded |
| 371 |
* unit with a unique name and migrate the cgroup/PID/etc. tracking from the stale state |
| 372 |
* into it, so that the resources from the previously independent unit remain tracked. |
| 373 |
* |
| 374 |
* Take as an example, a.service is running. Someone created symlink b.service -> a.service. |
| 375 |
* On first reload, the state file still has b.service as an independent unit (from before |
| 376 |
* the symlink existed), but b.service now resolves to a.service. We retain a.service's |
| 377 |
* running state, and synthesize an unloaded unit to keep tracking the processes that |
| 378 |
* b.service used to own. |
| 379 |
* |
| 380 |
* Note: Log messages from this code path are checked in TEST-07-PID1.alias-corruption.sh, |
| 381 |
* so the test case may need adjustment if they are changed. |
| 382 |
*/ |
| 383 |
r = manager_synthesize_orphaned_unit(m, name, u->id, f, fds); |
| 384 |
if (r < 0) /* If we fail to orphan for any reason, then discard the unit */ |
| 385 |
r = unit_deserialize_state_skip(f); |
| 386 |
return r; |
| 387 |
} |
| 388 |
|
| 389 |
r = unit_deserialize_state(u, f, fds); |
| 390 |
if (r == -ENOMEM) |
| 391 |
return log_oom(); |
| 392 |
if (r < 0) |
| 393 |
return log_notice_errno(r, "Failed to deserialize unit \"%s\", skipping: %m", name); |
| 394 |
|
| 395 |
/* If this unit was deserialized under an alias name (that is, it is a rename), record the canonical |
| 396 |
* ID so that any further aliases pointing to the same unit are correctly skipped. */ |
| 397 |
if (!streq(u->id, name)) { |
| 398 |
r = set_put_strdup(&serialized_units, u->id); |
| 399 |
if (r < 0) |
| 400 |
return log_oom(); |
| 401 |
} |
| 402 |
|
| 403 |
return 0; |
| 404 |
} |
| 405 |
|
| 406 |
static int manager_deserialize_units( |
| 407 |
Manager *m, |
| 408 |
FILE *f, |
| 409 |
FDSet *fds) { |
| 410 |
|
| 411 |
_cleanup_set_free_ Set *serialized_units = NULL; |
| 412 |
int r; |
| 413 |
|
| 414 |
r = manager_collect_serialized_unit_names(f, &serialized_units); |
| 415 |
if (r < 0) |
| 416 |
return r; |
| 417 |
|
| 418 |
for (;;) { |
| 419 |
_cleanup_free_ char *line = NULL; |
| 420 |
|
| 421 |
/* Start marker */ |
| 422 |
r = read_stripped_line(f, LONG_LINE_MAX, &line); |
| 423 |
if (r < 0) |
| 424 |
return log_error_errno(r, "Failed to read serialization line: %m"); |
| 425 |
if (r == 0) |
| 426 |
break; |
| 427 |
|
| 428 |
r = manager_deserialize_one_unit(m, line, f, fds, serialized_units); |
| 429 |
if (r == -ENOMEM) |
| 430 |
return r; |
| 431 |
if (r < 0) { |
| 432 |
r = unit_deserialize_state_skip(f); |
| 433 |
if (r < 0) |
| 434 |
return r; |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
return 0; |
| 439 |
} |
| 440 |
|
| 441 |
static void manager_deserialize_uid_refs_one_internal( |
| 442 |
Hashmap** uid_refs, |
| 443 |
const char *value) { |
| 444 |
|
| 445 |
uid_t uid; |
| 446 |
uint32_t c; |
| 447 |
int r; |
| 448 |
|
| 449 |
assert(uid_refs); |
| 450 |
assert(value); |
| 451 |
|
| 452 |
r = parse_uid(value, &uid); |
| 453 |
if (r < 0 || uid == 0) { |
| 454 |
log_debug("Unable to parse UID/GID reference serialization: %s", value); |
| 455 |
return; |
| 456 |
} |
| 457 |
|
| 458 |
if (hashmap_ensure_allocated(uid_refs, &trivial_hash_ops) < 0) { |
| 459 |
log_oom(); |
| 460 |
return; |
| 461 |
} |
| 462 |
|
| 463 |
c = PTR_TO_UINT32(hashmap_get(*uid_refs, UID_TO_PTR(uid))); |
| 464 |
if (c & DESTROY_IPC_FLAG) |
| 465 |
return; |
| 466 |
|
| 467 |
c |= DESTROY_IPC_FLAG; |
| 468 |
|
| 469 |
r = hashmap_replace(*uid_refs, UID_TO_PTR(uid), UINT32_TO_PTR(c)); |
| 470 |
if (r < 0) { |
| 471 |
log_debug_errno(r, "Failed to add UID/GID reference entry: %m"); |
| 472 |
return; |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
static void manager_deserialize_uid_refs_one(Manager *m, const char *value) { |
| 477 |
manager_deserialize_uid_refs_one_internal(&m->uid_refs, value); |
| 478 |
} |
| 479 |
|
| 480 |
static void manager_deserialize_gid_refs_one(Manager *m, const char *value) { |
| 481 |
manager_deserialize_uid_refs_one_internal(&m->gid_refs, value); |
| 482 |
} |
| 483 |
|
| 484 |
static void deserialize_restrict_fsaccess(Manager *m, const char *l, FDSet *fds) { |
| 485 |
const char *val; |
| 486 |
int fd; |
| 487 |
|
| 488 |
FOREACH_ELEMENT(name, restrict_fsaccess_link_names) { |
| 489 |
val = startswith(l, *name); |
| 490 |
if (!val) |
| 491 |
continue; |
| 492 |
val = startswith(val, "="); |
| 493 |
if (!val) |
| 494 |
continue; |
| 495 |
fd = deserialize_fd(fds, val); |
| 496 |
if (fd < 0) { |
| 497 |
log_warning_errno(fd, "bpf-restrict-fsaccess: Failed to deserialize FD for %s: %m", *name); |
| 498 |
return; |
| 499 |
} |
| 500 |
close_and_replace(m->restrict_fsaccess_link_fds[name - restrict_fsaccess_link_names], fd); |
| 501 |
return; |
| 502 |
} |
| 503 |
|
| 504 |
val = startswith(l, "restrict-fsaccess-bss-map="); |
| 505 |
if (!val) |
| 506 |
return; |
| 507 |
|
| 508 |
fd = deserialize_fd(fds, val); |
| 509 |
if (fd < 0) { |
| 510 |
log_warning_errno(fd, "bpf-restrict-fsaccess: Failed to deserialize FD for .bss map: %m"); |
| 511 |
return; |
| 512 |
} |
| 513 |
close_and_replace(m->restrict_fsaccess_bss_map_fd, fd); |
| 514 |
} |
| 515 |
|
| 516 |
int manager_deserialize(Manager *m, FILE *f, FDSet *fds) { |
| 517 |
int r; |
| 518 |
|
| 519 |
assert(m); |
| 520 |
assert(f); |
| 521 |
|
| 522 |
if (DEBUG_LOGGING) { |
| 523 |
if (fdset_isempty(fds)) |
| 524 |
log_debug("No file descriptors passed"); |
| 525 |
else { |
| 526 |
int fd; |
| 527 |
|
| 528 |
FDSET_FOREACH(fd, fds) { |
| 529 |
_cleanup_free_ char *fn = NULL; |
| 530 |
|
| 531 |
r = fd_get_path(fd, &fn); |
| 532 |
if (r < 0) |
| 533 |
log_debug_errno(r, "Received serialized fd %i %s %m", |
| 534 |
fd, glyph(GLYPH_ARROW_RIGHT)); |
| 535 |
else |
| 536 |
log_debug("Received serialized fd %i %s %s", |
| 537 |
fd, glyph(GLYPH_ARROW_RIGHT), strna(fn)); |
| 538 |
} |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
log_debug("Deserializing state..."); |
| 543 |
|
| 544 |
/* If we are not in reload mode yet, enter it now. Not that this is recursive, a caller might already have |
| 545 |
* increased it to non-zero, which is why we just increase it by one here and down again at the end of this |
| 546 |
* call. */ |
| 547 |
_cleanup_(manager_reloading_stopp) _unused_ Manager *reloading = manager_reloading_start(m); |
| 548 |
|
| 549 |
for (;;) { |
| 550 |
_cleanup_free_ char *l = NULL; |
| 551 |
const char *val; |
| 552 |
|
| 553 |
r = deserialize_read_line(f, &l); |
| 554 |
if (r < 0) |
| 555 |
return r; |
| 556 |
if (r == 0) /* eof or end marker */ |
| 557 |
break; |
| 558 |
|
| 559 |
if ((val = startswith(l, "last-transaction-id="))) { |
| 560 |
uint64_t id; |
| 561 |
|
| 562 |
if (safe_atou64(val, &id) < 0) |
| 563 |
log_notice("Failed to parse last transaction id value '%s', ignoring.", val); |
| 564 |
else |
| 565 |
m->last_transaction_id = MAX(m->last_transaction_id, id); |
| 566 |
|
| 567 |
} else if ((val = startswith(l, "current-job-id="))) { |
| 568 |
uint32_t id; |
| 569 |
|
| 570 |
if (safe_atou32(val, &id) < 0) |
| 571 |
log_notice("Failed to parse current job id value '%s', ignoring.", val); |
| 572 |
else |
| 573 |
m->current_job_id = MAX(m->current_job_id, id); |
| 574 |
|
| 575 |
} else if ((val = startswith(l, "n-installed-jobs="))) { |
| 576 |
uint32_t n; |
| 577 |
|
| 578 |
if (safe_atou32(val, &n) < 0) |
| 579 |
log_notice("Failed to parse installed jobs counter '%s', ignoring.", val); |
| 580 |
else |
| 581 |
m->n_installed_jobs += n; |
| 582 |
|
| 583 |
} else if ((val = startswith(l, "n-failed-jobs="))) { |
| 584 |
uint32_t n; |
| 585 |
|
| 586 |
if (safe_atou32(val, &n) < 0) |
| 587 |
log_notice("Failed to parse failed jobs counter '%s', ignoring.", val); |
| 588 |
else |
| 589 |
m->n_failed_jobs += n; |
| 590 |
|
| 591 |
} else if ((val = startswith(l, "taint-logged="))) { |
| 592 |
r = parse_boolean(val); |
| 593 |
if (r < 0) |
| 594 |
log_notice("Failed to parse taint-logged flag '%s', ignoring.", val); |
| 595 |
else |
| 596 |
m->taint_logged = m->taint_logged || r; |
| 597 |
|
| 598 |
} else if ((val = startswith(l, "service-watchdogs="))) { |
| 599 |
r = parse_boolean(val); |
| 600 |
if (r < 0) |
| 601 |
log_notice("Failed to parse service-watchdogs flag '%s', ignoring.", val); |
| 602 |
else |
| 603 |
m->service_watchdogs = r; |
| 604 |
|
| 605 |
} else if ((val = startswith(l, "show-status-overridden="))) { |
| 606 |
ShowStatus s; |
| 607 |
|
| 608 |
s = show_status_from_string(val); |
| 609 |
if (s < 0) |
| 610 |
log_notice("Failed to parse show-status-overridden flag '%s', ignoring.", val); |
| 611 |
else |
| 612 |
manager_override_show_status(m, s, "deserialize"); |
| 613 |
|
| 614 |
} else if ((val = startswith(l, "log-level-override="))) { |
| 615 |
int level; |
| 616 |
|
| 617 |
level = log_level_from_string(val); |
| 618 |
if (level < 0) |
| 619 |
log_notice("Failed to parse log-level-override value '%s', ignoring.", val); |
| 620 |
else |
| 621 |
manager_override_log_level(m, level); |
| 622 |
|
| 623 |
} else if ((val = startswith(l, "log-target-override="))) { |
| 624 |
LogTarget target; |
| 625 |
|
| 626 |
target = log_target_from_string(val); |
| 627 |
if (target < 0) |
| 628 |
log_notice("Failed to parse log-target-override value '%s', ignoring.", val); |
| 629 |
else |
| 630 |
manager_override_log_target(m, target); |
| 631 |
|
| 632 |
} else if ((val = startswith(l, "runtime-watchdog-overridden="))) { |
| 633 |
usec_t t; |
| 634 |
|
| 635 |
if (deserialize_usec(val, &t) < 0) |
| 636 |
log_notice("Failed to parse runtime-watchdog-overridden value '%s', ignoring.", val); |
| 637 |
else |
| 638 |
manager_override_watchdog(m, WATCHDOG_RUNTIME, t); |
| 639 |
|
| 640 |
} else if ((val = startswith(l, "reboot-watchdog-overridden="))) { |
| 641 |
usec_t t; |
| 642 |
|
| 643 |
if (deserialize_usec(val, &t) < 0) |
| 644 |
log_notice("Failed to parse reboot-watchdog-overridden value '%s', ignoring.", val); |
| 645 |
else |
| 646 |
manager_override_watchdog(m, WATCHDOG_REBOOT, t); |
| 647 |
|
| 648 |
} else if ((val = startswith(l, "kexec-watchdog-overridden="))) { |
| 649 |
usec_t t; |
| 650 |
|
| 651 |
if (deserialize_usec(val, &t) < 0) |
| 652 |
log_notice("Failed to parse kexec-watchdog-overridden value '%s', ignoring.", val); |
| 653 |
else |
| 654 |
manager_override_watchdog(m, WATCHDOG_KEXEC, t); |
| 655 |
|
| 656 |
} else if ((val = startswith(l, "pretimeout-watchdog-overridden="))) { |
| 657 |
usec_t t; |
| 658 |
|
| 659 |
if (deserialize_usec(val, &t) < 0) |
| 660 |
log_notice("Failed to parse pretimeout-watchdog-overridden value '%s', ignoring.", val); |
| 661 |
else |
| 662 |
manager_override_watchdog(m, WATCHDOG_PRETIMEOUT, t); |
| 663 |
|
| 664 |
} else if ((val = startswith(l, "pretimeout-watchdog-governor-overridden="))) { |
| 665 |
r = free_and_strdup(&m->watchdog_pretimeout_governor_overridden, val); |
| 666 |
if (r < 0) |
| 667 |
return r; |
| 668 |
|
| 669 |
} else if ((val = startswith(l, "env="))) { |
| 670 |
r = deserialize_environment(val, &m->client_environment); |
| 671 |
if (r < 0) |
| 672 |
log_notice_errno(r, "Failed to parse environment entry: \"%s\", ignoring: %m", val); |
| 673 |
|
| 674 |
} else if ((val = startswith(l, "notify-fd="))) { |
| 675 |
int fd; |
| 676 |
|
| 677 |
fd = deserialize_fd(fds, val); |
| 678 |
if (fd >= 0) { |
| 679 |
m->notify_event_source = sd_event_source_disable_unref(m->notify_event_source); |
| 680 |
close_and_replace(m->notify_fd, fd); |
| 681 |
} |
| 682 |
|
| 683 |
} else if ((val = startswith(l, "notify-socket="))) { |
| 684 |
r = free_and_strdup(&m->notify_socket, val); |
| 685 |
if (r < 0) |
| 686 |
return r; |
| 687 |
|
| 688 |
} else if ((val = startswith(l, "user-lookup="))) { |
| 689 |
|
| 690 |
m->user_lookup_event_source = sd_event_source_disable_unref(m->user_lookup_event_source); |
| 691 |
safe_close_pair(m->user_lookup_fds); |
| 692 |
|
| 693 |
r = deserialize_fd_many(fds, val, 2, m->user_lookup_fds); |
| 694 |
if (r < 0) |
| 695 |
log_warning_errno(r, "Failed to parse user-lookup fds: \"%s\", ignoring: %m", val); |
| 696 |
|
| 697 |
} else if ((val = startswith(l, "handoff-timestamp-fds="))) { |
| 698 |
|
| 699 |
m->handoff_timestamp_event_source = sd_event_source_disable_unref(m->handoff_timestamp_event_source); |
| 700 |
safe_close_pair(m->handoff_timestamp_fds); |
| 701 |
|
| 702 |
r = deserialize_fd_many(fds, val, 2, m->handoff_timestamp_fds); |
| 703 |
if (r < 0) |
| 704 |
log_warning_errno(r, "Failed to parse handoff-timestamp fds: \"%s\", ignoring: %m", val); |
| 705 |
|
| 706 |
} else if ((val = startswith(l, "dynamic-user="))) |
| 707 |
dynamic_user_deserialize_one(m, val, fds, NULL); |
| 708 |
else if ((val = startswith(l, "destroy-ipc-uid="))) |
| 709 |
manager_deserialize_uid_refs_one(m, val); |
| 710 |
else if ((val = startswith(l, "destroy-ipc-gid="))) |
| 711 |
manager_deserialize_gid_refs_one(m, val); |
| 712 |
else if ((val = startswith(l, "exec-runtime="))) |
| 713 |
(void) exec_shared_runtime_deserialize_one(m, val, fds); |
| 714 |
else if ((val = startswith(l, "bus-id="))) { |
| 715 |
|
| 716 |
r = sd_id128_from_string(val, &m->deserialized_bus_id); |
| 717 |
if (r < 0) |
| 718 |
return r; |
| 719 |
} else if ((val = startswith(l, "subscribed="))) { |
| 720 |
|
| 721 |
r = strv_extend(&m->subscribed_as_strv, val); |
| 722 |
if (r < 0) |
| 723 |
return r; |
| 724 |
} else if ((val = startswith(l, "varlink-server-metrics-"))) { |
| 725 |
if (m->objective == MANAGER_RELOAD) |
| 726 |
/* We don't destroy varlink server on daemon-reload (in contrast to reexec) -> skip! */ |
| 727 |
continue; |
| 728 |
|
| 729 |
r = manager_setup_varlink_metrics_server(m); |
| 730 |
if (r < 0) |
| 731 |
log_warning_errno(r, "Failed to setup metrics varlink server, ignoring: %m"); |
| 732 |
else |
| 733 |
(void) varlink_server_deserialize_one(m->metrics_varlink_server, val, fds); |
| 734 |
|
| 735 |
} else if ((val = startswith(l, "varlink-server-"))) { |
| 736 |
if (m->objective == MANAGER_RELOAD) |
| 737 |
/* We don't destroy varlink server on daemon-reload (in contrast to reexec) -> skip! */ |
| 738 |
continue; |
| 739 |
|
| 740 |
r = manager_setup_varlink_server(m); |
| 741 |
if (r < 0) |
| 742 |
log_warning_errno(r, "Failed to setup varlink server, ignoring: %m"); |
| 743 |
else |
| 744 |
(void) varlink_server_deserialize_one(m->varlink_server, val, fds); |
| 745 |
|
| 746 |
} else if (startswith(l, "restrict-fsaccess-")) |
| 747 |
deserialize_restrict_fsaccess(m, l, fds); |
| 748 |
else if ((val = startswith(l, "dump-ratelimit="))) |
| 749 |
deserialize_ratelimit(&m->dump_ratelimit, "dump-ratelimit", val); |
| 750 |
else if ((val = startswith(l, "reload-reexec-ratelimit="))) |
| 751 |
deserialize_ratelimit(&m->reload_reexec_ratelimit, "reload-reexec-ratelimit", val); |
| 752 |
else if ((val = startswith(l, "event-loop-ratelimit="))) |
| 753 |
deserialize_ratelimit(&m->event_loop_ratelimit, "event-loop-ratelimit", val); |
| 754 |
else if ((val = startswith(l, "soft-reboots-count="))) { |
| 755 |
unsigned n; |
| 756 |
|
| 757 |
if (safe_atou(val, &n) < 0) |
| 758 |
log_notice("Failed to parse soft reboots counter '%s', ignoring.", val); |
| 759 |
else |
| 760 |
m->soft_reboots_count = n; |
| 761 |
} else if ((val = startswith(l, "fd-store-upstream-next-index="))) { |
| 762 |
if (safe_atou64(val, &m->fd_store_upstream_next_index) < 0) |
| 763 |
log_notice("Failed to parse fd-store-upstream-next-index '%s', ignoring.", val); |
| 764 |
} else if ((val = startswith(l, "previous-objective="))) { |
| 765 |
ManagerObjective objective; |
| 766 |
|
| 767 |
objective = manager_objective_from_string(val); |
| 768 |
if (objective < 0) |
| 769 |
log_notice("Failed to parse previous objective '%s', ignoring.", val); |
| 770 |
else |
| 771 |
m->previous_objective = objective; |
| 772 |
|
| 773 |
} else { |
| 774 |
ManagerTimestamp q; |
| 775 |
|
| 776 |
for (q = 0; q < _MANAGER_TIMESTAMP_MAX; q++) { |
| 777 |
val = startswith(l, manager_timestamp_to_string(q)); |
| 778 |
if (!val) |
| 779 |
continue; |
| 780 |
|
| 781 |
val = startswith(val, "-timestamp="); |
| 782 |
if (val) |
| 783 |
break; |
| 784 |
} |
| 785 |
|
| 786 |
if (q < _MANAGER_TIMESTAMP_MAX) /* found it */ |
| 787 |
(void) deserialize_dual_timestamp(val, m->timestamps + q); |
| 788 |
else if (!STARTSWITH_SET(l, "kdbus-fd=", "honor-device-enumeration=", "ready-sent=", "cgroups-agent-fd=")) /* ignore deprecated values */ |
| 789 |
log_notice("Unknown serialization item '%s', ignoring.", l); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
return manager_deserialize_units(m, f, fds); |
| 794 |
} |