]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/unit-serialize.c
Rename {dual,triple}_timestamp_get to {dual,triple}_timestamp_now
[thirdparty/systemd.git] / src / core / unit-serialize.c
CommitLineData
2d3b784d
ZJS
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
cd09a5f3 3#include "bpf-socket-bind.h"
2d3b784d
ZJS
4#include "bus-util.h"
5#include "dbus.h"
6#include "fileio-label.h"
7#include "fileio.h"
8#include "format-util.h"
9#include "parse-util.h"
6f50d4f7 10#include "restrict-ifaces.h"
2d3b784d
ZJS
11#include "serialize.h"
12#include "string-table.h"
13#include "unit-serialize.h"
14#include "user-util.h"
15
16static int serialize_cgroup_mask(FILE *f, const char *key, CGroupMask mask) {
17 _cleanup_free_ char *s = NULL;
18 int r;
19
20 assert(f);
21 assert(key);
22
23 if (mask == 0)
24 return 0;
25
26 r = cg_mask_to_string(mask, &s);
27 if (r < 0)
28 return log_error_errno(r, "Failed to format cgroup mask: %m");
29
30 return serialize_item(f, key, s);
31}
32
ff68472a
ZJS
33/* Make sure out values fit in the bitfield. */
34assert_cc(_UNIT_MARKER_MAX <= sizeof(((Unit){}).markers) * 8);
35
36static int serialize_markers(FILE *f, unsigned markers) {
37 assert(f);
38
39 if (markers == 0)
40 return 0;
41
42 fputs("markers=", f);
43 for (UnitMarker m = 0; m < _UNIT_MARKER_MAX; m++)
44 if (FLAGS_SET(markers, 1u << m))
45 fputs(unit_marker_to_string(m), f);
46 fputc('\n', f);
47 return 0;
48}
49
50static int deserialize_markers(Unit *u, const char *value) {
51 assert(u);
52 assert(value);
53 int r;
54
55 for (const char *p = value;;) {
56 _cleanup_free_ char *word = NULL;
57
58 r = extract_first_word(&p, &word, NULL, 0);
59 if (r <= 0)
60 return r;
61
62 UnitMarker m = unit_marker_from_string(word);
63 if (m < 0) {
64 log_unit_debug_errno(u, m, "Unknown unit marker \"%s\", ignoring.", word);
65 continue;
66 }
67
68 u->markers |= 1u << m;
69 }
70}
71
2d3b784d
ZJS
72static const char *const ip_accounting_metric_field[_CGROUP_IP_ACCOUNTING_METRIC_MAX] = {
73 [CGROUP_IP_INGRESS_BYTES] = "ip-accounting-ingress-bytes",
74 [CGROUP_IP_INGRESS_PACKETS] = "ip-accounting-ingress-packets",
75 [CGROUP_IP_EGRESS_BYTES] = "ip-accounting-egress-bytes",
76 [CGROUP_IP_EGRESS_PACKETS] = "ip-accounting-egress-packets",
77};
78
79static const char *const io_accounting_metric_field_base[_CGROUP_IO_ACCOUNTING_METRIC_MAX] = {
80 [CGROUP_IO_READ_BYTES] = "io-accounting-read-bytes-base",
81 [CGROUP_IO_WRITE_BYTES] = "io-accounting-write-bytes-base",
82 [CGROUP_IO_READ_OPERATIONS] = "io-accounting-read-operations-base",
83 [CGROUP_IO_WRITE_OPERATIONS] = "io-accounting-write-operations-base",
84};
85
86static const char *const io_accounting_metric_field_last[_CGROUP_IO_ACCOUNTING_METRIC_MAX] = {
87 [CGROUP_IO_READ_BYTES] = "io-accounting-read-bytes-last",
88 [CGROUP_IO_WRITE_BYTES] = "io-accounting-write-bytes-last",
89 [CGROUP_IO_READ_OPERATIONS] = "io-accounting-read-operations-last",
90 [CGROUP_IO_WRITE_OPERATIONS] = "io-accounting-write-operations-last",
91};
92
5699a168 93int unit_serialize_state(Unit *u, FILE *f, FDSet *fds, bool switching_root) {
2d3b784d
ZJS
94 int r;
95
96 assert(u);
97 assert(f);
98 assert(fds);
99
755021d4
ZJS
100 if (switching_root && UNIT_VTABLE(u)->exclude_from_switch_root_serialization) {
101 /* In the new root, paths for mounts and automounts will be different, so it doesn't make
102 * much sense to serialize things. API file systems will be moved to the new root, but we
103 * don't have mount units for those. */
104 log_unit_debug(u, "not serializing before switch-root");
105 return 0;
106 }
107
108 /* Start marker */
109 fputs(u->id, f);
110 fputc('\n', f);
111
1085c0eb
ZJS
112 assert(!!UNIT_VTABLE(u)->serialize == !!UNIT_VTABLE(u)->deserialize_item);
113
114 if (UNIT_VTABLE(u)->serialize) {
2d3b784d
ZJS
115 r = UNIT_VTABLE(u)->serialize(u, f, fds);
116 if (r < 0)
117 return r;
118 }
119
120 (void) serialize_dual_timestamp(f, "state-change-timestamp", &u->state_change_timestamp);
121
122 (void) serialize_dual_timestamp(f, "inactive-exit-timestamp", &u->inactive_exit_timestamp);
123 (void) serialize_dual_timestamp(f, "active-enter-timestamp", &u->active_enter_timestamp);
124 (void) serialize_dual_timestamp(f, "active-exit-timestamp", &u->active_exit_timestamp);
125 (void) serialize_dual_timestamp(f, "inactive-enter-timestamp", &u->inactive_enter_timestamp);
126
127 (void) serialize_dual_timestamp(f, "condition-timestamp", &u->condition_timestamp);
128 (void) serialize_dual_timestamp(f, "assert-timestamp", &u->assert_timestamp);
129
130 if (dual_timestamp_is_set(&u->condition_timestamp))
131 (void) serialize_bool(f, "condition-result", u->condition_result);
132
133 if (dual_timestamp_is_set(&u->assert_timestamp))
134 (void) serialize_bool(f, "assert-result", u->assert_result);
135
136 (void) serialize_bool(f, "transient", u->transient);
137 (void) serialize_bool(f, "in-audit", u->in_audit);
138
139 (void) serialize_bool(f, "exported-invocation-id", u->exported_invocation_id);
140 (void) serialize_bool(f, "exported-log-level-max", u->exported_log_level_max);
141 (void) serialize_bool(f, "exported-log-extra-fields", u->exported_log_extra_fields);
142 (void) serialize_bool(f, "exported-log-rate-limit-interval", u->exported_log_ratelimit_interval);
143 (void) serialize_bool(f, "exported-log-rate-limit-burst", u->exported_log_ratelimit_burst);
144
145 (void) serialize_item_format(f, "cpu-usage-base", "%" PRIu64, u->cpu_usage_base);
146 if (u->cpu_usage_last != NSEC_INFINITY)
147 (void) serialize_item_format(f, "cpu-usage-last", "%" PRIu64, u->cpu_usage_last);
148
149 if (u->managed_oom_kill_last > 0)
150 (void) serialize_item_format(f, "managed-oom-kill-last", "%" PRIu64, u->managed_oom_kill_last);
151
152 if (u->oom_kill_last > 0)
153 (void) serialize_item_format(f, "oom-kill-last", "%" PRIu64, u->oom_kill_last);
154
155 for (CGroupIOAccountingMetric im = 0; im < _CGROUP_IO_ACCOUNTING_METRIC_MAX; im++) {
156 (void) serialize_item_format(f, io_accounting_metric_field_base[im], "%" PRIu64, u->io_accounting_base[im]);
157
158 if (u->io_accounting_last[im] != UINT64_MAX)
159 (void) serialize_item_format(f, io_accounting_metric_field_last[im], "%" PRIu64, u->io_accounting_last[im]);
160 }
161
162 if (u->cgroup_path)
163 (void) serialize_item(f, "cgroup", u->cgroup_path);
164
165 (void) serialize_bool(f, "cgroup-realized", u->cgroup_realized);
166 (void) serialize_cgroup_mask(f, "cgroup-realized-mask", u->cgroup_realized_mask);
167 (void) serialize_cgroup_mask(f, "cgroup-enabled-mask", u->cgroup_enabled_mask);
168 (void) serialize_cgroup_mask(f, "cgroup-invalidated-mask", u->cgroup_invalidated_mask);
169
cd09a5f3 170 (void) bpf_serialize_socket_bind(u, f, fds);
3d027d4d 171
b57d7523
LP
172 (void) bpf_program_serialize_attachment(f, fds, "ip-bpf-ingress-installed", u->ip_bpf_ingress_installed);
173 (void) bpf_program_serialize_attachment(f, fds, "ip-bpf-egress-installed", u->ip_bpf_egress_installed);
0b4f8d94 174 (void) bpf_program_serialize_attachment(f, fds, "bpf-device-control-installed", u->bpf_device_control_installed);
b57d7523
LP
175 (void) bpf_program_serialize_attachment_set(f, fds, "ip-bpf-custom-ingress-installed", u->ip_bpf_custom_ingress_installed);
176 (void) bpf_program_serialize_attachment_set(f, fds, "ip-bpf-custom-egress-installed", u->ip_bpf_custom_egress_installed);
177
6f50d4f7
MV
178 (void) serialize_restrict_network_interfaces(u, f, fds);
179
2d3b784d
ZJS
180 if (uid_is_valid(u->ref_uid))
181 (void) serialize_item_format(f, "ref-uid", UID_FMT, u->ref_uid);
182 if (gid_is_valid(u->ref_gid))
183 (void) serialize_item_format(f, "ref-gid", GID_FMT, u->ref_gid);
184
185 if (!sd_id128_is_null(u->invocation_id))
186 (void) serialize_item_format(f, "invocation-id", SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(u->invocation_id));
187
188 (void) serialize_item_format(f, "freezer-state", "%s", freezer_state_to_string(unit_freezer_state(u)));
ff68472a 189 (void) serialize_markers(f, u->markers);
2d3b784d
ZJS
190
191 bus_track_serialize(u->bus_track, f, "ref");
192
193 for (CGroupIPAccountingMetric m = 0; m < _CGROUP_IP_ACCOUNTING_METRIC_MAX; m++) {
194 uint64_t v;
195
196 r = unit_get_ip_accounting(u, m, &v);
197 if (r >= 0)
198 (void) serialize_item_format(f, ip_accounting_metric_field[m], "%" PRIu64, v);
199 }
200
755021d4 201 if (!switching_root) {
2d3b784d
ZJS
202 if (u->job) {
203 fputs("job\n", f);
204 job_serialize(u->job, f);
205 }
206
207 if (u->nop_job) {
208 fputs("job\n", f);
209 job_serialize(u->nop_job, f);
210 }
211 }
212
213 /* End marker */
214 fputc('\n', f);
215 return 0;
216}
217
218static int unit_deserialize_job(Unit *u, FILE *f) {
219 _cleanup_(job_freep) Job *j = NULL;
220 int r;
221
222 assert(u);
223 assert(f);
224
225 j = job_new_raw(u);
226 if (!j)
227 return log_oom();
228
229 r = job_deserialize(j, f);
230 if (r < 0)
231 return r;
232
233 r = job_install_deserialized(j);
234 if (r < 0)
235 return r;
236
237 TAKE_PTR(j);
238 return 0;
239}
240
9466ec13
ZJS
241#define MATCH_DESERIALIZE(key, l, v, parse_func, target) \
242 ({ \
243 bool _deserialize_matched = streq(l, key); \
244 if (_deserialize_matched) { \
245 int _deserialize_r = parse_func(v); \
246 if (_deserialize_r < 0) \
247 log_unit_debug_errno(u, _deserialize_r, \
248 "Failed to parse \"%s=%s\", ignoring.", l, v); \
249 else \
250 target = _deserialize_r; \
251 }; \
252 _deserialize_matched; \
253 })
254
255#define MATCH_DESERIALIZE_IMMEDIATE(key, l, v, parse_func, target) \
256 ({ \
257 bool _deserialize_matched = streq(l, key); \
258 if (_deserialize_matched) { \
259 int _deserialize_r = parse_func(v, &target); \
260 if (_deserialize_r < 0) \
261 log_unit_debug_errno(u, _deserialize_r, \
262 "Failed to parse \"%s=%s\", ignoring", l, v); \
263 }; \
264 _deserialize_matched; \
265 })
266
5699a168 267int unit_deserialize_state(Unit *u, FILE *f, FDSet *fds) {
2d3b784d
ZJS
268 int r;
269
270 assert(u);
271 assert(f);
272 assert(fds);
273
274 for (;;) {
0df7d525 275 _cleanup_free_ char *l = NULL;
2d3b784d
ZJS
276 ssize_t m;
277 size_t k;
0df7d525 278 char *v;
2d3b784d 279
0df7d525 280 r = deserialize_read_line(f, &l);
2d3b784d 281 if (r < 0)
0df7d525
LP
282 return r;
283 if (r == 0) /* eof or end marker */
2d3b784d
ZJS
284 break;
285
286 k = strcspn(l, "=");
287
288 if (l[k] == '=') {
289 l[k] = 0;
290 v = l+k+1;
291 } else
292 v = l+k;
293
294 if (streq(l, "job")) {
295 if (v[0] == '\0') {
296 /* New-style serialized job */
297 r = unit_deserialize_job(u, f);
298 if (r < 0)
299 return r;
300 } else /* Legacy for pre-44 */
301 log_unit_warning(u, "Update from too old systemd versions are unsupported, cannot deserialize job: %s", v);
302 continue;
303 } else if (streq(l, "state-change-timestamp")) {
304 (void) deserialize_dual_timestamp(v, &u->state_change_timestamp);
305 continue;
306 } else if (streq(l, "inactive-exit-timestamp")) {
307 (void) deserialize_dual_timestamp(v, &u->inactive_exit_timestamp);
308 continue;
309 } else if (streq(l, "active-enter-timestamp")) {
310 (void) deserialize_dual_timestamp(v, &u->active_enter_timestamp);
311 continue;
312 } else if (streq(l, "active-exit-timestamp")) {
313 (void) deserialize_dual_timestamp(v, &u->active_exit_timestamp);
314 continue;
315 } else if (streq(l, "inactive-enter-timestamp")) {
316 (void) deserialize_dual_timestamp(v, &u->inactive_enter_timestamp);
317 continue;
318 } else if (streq(l, "condition-timestamp")) {
319 (void) deserialize_dual_timestamp(v, &u->condition_timestamp);
320 continue;
321 } else if (streq(l, "assert-timestamp")) {
322 (void) deserialize_dual_timestamp(v, &u->assert_timestamp);
323 continue;
2d3b784d 324
9466ec13 325 } else if (MATCH_DESERIALIZE("condition-result", l, v, parse_boolean, u->condition_result))
2d3b784d
ZJS
326 continue;
327
9466ec13 328 else if (MATCH_DESERIALIZE("assert-result", l, v, parse_boolean, u->assert_result))
2d3b784d
ZJS
329 continue;
330
9466ec13 331 else if (MATCH_DESERIALIZE("transient", l, v, parse_boolean, u->transient))
2d3b784d
ZJS
332 continue;
333
9466ec13 334 else if (MATCH_DESERIALIZE("in-audit", l, v, parse_boolean, u->in_audit))
2d3b784d
ZJS
335 continue;
336
9466ec13 337 else if (MATCH_DESERIALIZE("exported-invocation-id", l, v, parse_boolean, u->exported_invocation_id))
2d3b784d
ZJS
338 continue;
339
9466ec13 340 else if (MATCH_DESERIALIZE("exported-log-level-max", l, v, parse_boolean, u->exported_log_level_max))
2d3b784d
ZJS
341 continue;
342
9466ec13 343 else if (MATCH_DESERIALIZE("exported-log-extra-fields", l, v, parse_boolean, u->exported_log_extra_fields))
2d3b784d
ZJS
344 continue;
345
9466ec13 346 else if (MATCH_DESERIALIZE("exported-log-rate-limit-interval", l, v, parse_boolean, u->exported_log_ratelimit_interval))
2d3b784d
ZJS
347 continue;
348
9466ec13 349 else if (MATCH_DESERIALIZE("exported-log-rate-limit-burst", l, v, parse_boolean, u->exported_log_ratelimit_burst))
2d3b784d
ZJS
350 continue;
351
9466ec13
ZJS
352 else if (MATCH_DESERIALIZE_IMMEDIATE("cpu-usage-base", l, v, safe_atou64, u->cpu_usage_base) ||
353 MATCH_DESERIALIZE_IMMEDIATE("cpuacct-usage-base", l, v, safe_atou64, u->cpu_usage_base))
2d3b784d
ZJS
354 continue;
355
9466ec13 356 else if (MATCH_DESERIALIZE_IMMEDIATE("cpu-usage-last", l, v, safe_atou64, u->cpu_usage_last))
2d3b784d
ZJS
357 continue;
358
9466ec13 359 else if (MATCH_DESERIALIZE_IMMEDIATE("managed-oom-kill-last", l, v, safe_atou64, u->managed_oom_kill_last))
2d3b784d
ZJS
360 continue;
361
9466ec13 362 else if (MATCH_DESERIALIZE_IMMEDIATE("oom-kill-last", l, v, safe_atou64, u->oom_kill_last))
2d3b784d
ZJS
363 continue;
364
9466ec13 365 else if (streq(l, "cgroup")) {
2d3b784d
ZJS
366 r = unit_set_cgroup_path(u, v);
367 if (r < 0)
368 log_unit_debug_errno(u, r, "Failed to set cgroup path %s, ignoring: %m", v);
369
370 (void) unit_watch_cgroup(u);
371 (void) unit_watch_cgroup_memory(u);
372
373 continue;
2d3b784d 374
9466ec13 375 } else if (MATCH_DESERIALIZE("cgroup-realized", l, v, parse_boolean, u->cgroup_realized))
2d3b784d
ZJS
376 continue;
377
9466ec13 378 else if (MATCH_DESERIALIZE_IMMEDIATE("cgroup-realized-mask", l, v, cg_mask_from_string, u->cgroup_realized_mask))
2d3b784d
ZJS
379 continue;
380
9466ec13 381 else if (MATCH_DESERIALIZE_IMMEDIATE("cgroup-enabled-mask", l, v, cg_mask_from_string, u->cgroup_enabled_mask))
2d3b784d
ZJS
382 continue;
383
9466ec13 384 else if (MATCH_DESERIALIZE_IMMEDIATE("cgroup-invalidated-mask", l, v, cg_mask_from_string, u->cgroup_invalidated_mask))
2d3b784d
ZJS
385 continue;
386
3d027d4d
JK
387 else if (STR_IN_SET(l, "ipv4-socket-bind-bpf-link-fd", "ipv6-socket-bind-bpf-link-fd")) {
388 int fd;
389
dff9808a
LP
390 fd = deserialize_fd(fds, v);
391 if (fd >= 0)
cd09a5f3 392 (void) bpf_socket_bind_add_initial_link_fd(u, fd);
3d027d4d 393 continue;
3d027d4d 394
b57d7523
LP
395 } else if (streq(l, "ip-bpf-ingress-installed")) {
396 (void) bpf_program_deserialize_attachment(v, fds, &u->ip_bpf_ingress_installed);
397 continue;
398 } else if (streq(l, "ip-bpf-egress-installed")) {
399 (void) bpf_program_deserialize_attachment(v, fds, &u->ip_bpf_egress_installed);
400 continue;
0b4f8d94
AZ
401 } else if (streq(l, "bpf-device-control-installed")) {
402 (void) bpf_program_deserialize_attachment(v, fds, &u->bpf_device_control_installed);
403 continue;
b57d7523
LP
404
405 } else if (streq(l, "ip-bpf-custom-ingress-installed")) {
406 (void) bpf_program_deserialize_attachment_set(v, fds, &u->ip_bpf_custom_ingress_installed);
407 continue;
408 } else if (streq(l, "ip-bpf-custom-egress-installed")) {
409 (void) bpf_program_deserialize_attachment_set(v, fds, &u->ip_bpf_custom_egress_installed);
410 continue;
411
6f50d4f7
MV
412 } else if (streq(l, "restrict-ifaces-bpf-fd")) {
413 int fd;
414
dff9808a
LP
415 fd = deserialize_fd(fds, v);
416 if (fd >= 0)
417 (void) restrict_network_interfaces_add_initial_link_fd(u, fd);
6f50d4f7 418
6f50d4f7
MV
419 continue;
420
b57d7523 421 } else if (streq(l, "ref-uid")) {
2d3b784d
ZJS
422 uid_t uid;
423
424 r = parse_uid(v, &uid);
425 if (r < 0)
9466ec13 426 log_unit_debug(u, "Failed to parse \"%s=%s\", ignoring.", l, v);
2d3b784d
ZJS
427 else
428 unit_ref_uid_gid(u, uid, GID_INVALID);
2d3b784d
ZJS
429 continue;
430
431 } else if (streq(l, "ref-gid")) {
432 gid_t gid;
433
434 r = parse_gid(v, &gid);
435 if (r < 0)
9466ec13 436 log_unit_debug(u, "Failed to parse \"%s=%s\", ignoring.", l, v);
2d3b784d
ZJS
437 else
438 unit_ref_uid_gid(u, UID_INVALID, gid);
2d3b784d
ZJS
439 continue;
440
441 } else if (streq(l, "ref")) {
2d3b784d
ZJS
442 r = strv_extend(&u->deserialized_refs, v);
443 if (r < 0)
444 return log_oom();
2d3b784d 445 continue;
9466ec13 446
2d3b784d
ZJS
447 } else if (streq(l, "invocation-id")) {
448 sd_id128_t id;
449
450 r = sd_id128_from_string(v, &id);
451 if (r < 0)
9466ec13 452 log_unit_debug(u, "Failed to parse \"%s=%s\", ignoring.", l, v);
2d3b784d
ZJS
453 else {
454 r = unit_set_invocation_id(u, id);
455 if (r < 0)
456 log_unit_warning_errno(u, r, "Failed to set invocation ID for unit: %m");
457 }
458
459 continue;
2d3b784d 460
9466ec13 461 } else if (MATCH_DESERIALIZE("freezer-state", l, v, freezer_state_from_string, u->freezer_state))
2d3b784d 462 continue;
2d3b784d 463
ff68472a
ZJS
464 else if (streq(l, "markers")) {
465 r = deserialize_markers(u, v);
466 if (r < 0)
467 log_unit_debug_errno(u, r, "Failed to deserialize \"%s=%s\", ignoring: %m", l, v);
468 continue;
469 }
470
2d3b784d
ZJS
471 /* Check if this is an IP accounting metric serialization field */
472 m = string_table_lookup(ip_accounting_metric_field, ELEMENTSOF(ip_accounting_metric_field), l);
473 if (m >= 0) {
474 uint64_t c;
475
476 r = safe_atou64(v, &c);
477 if (r < 0)
478 log_unit_debug(u, "Failed to parse IP accounting value %s, ignoring.", v);
479 else
480 u->ip_accounting_extra[m] = c;
481 continue;
482 }
483
484 m = string_table_lookup(io_accounting_metric_field_base, ELEMENTSOF(io_accounting_metric_field_base), l);
485 if (m >= 0) {
486 uint64_t c;
487
488 r = safe_atou64(v, &c);
489 if (r < 0)
490 log_unit_debug(u, "Failed to parse IO accounting base value %s, ignoring.", v);
491 else
492 u->io_accounting_base[m] = c;
493 continue;
494 }
495
496 m = string_table_lookup(io_accounting_metric_field_last, ELEMENTSOF(io_accounting_metric_field_last), l);
497 if (m >= 0) {
498 uint64_t c;
499
500 r = safe_atou64(v, &c);
501 if (r < 0)
502 log_unit_debug(u, "Failed to parse IO accounting last value %s, ignoring.", v);
503 else
504 u->io_accounting_last[m] = c;
505 continue;
506 }
507
e76506b7 508 r = exec_shared_runtime_deserialize_compat(u, l, v, fds);
fe50aae5
ZJS
509 if (r < 0) {
510 log_unit_warning(u, "Failed to deserialize runtime parameter '%s', ignoring.", l);
511 continue;
512 } else if (r > 0)
2d3b784d 513 /* Returns positive if key was handled by the call */
fe50aae5 514 continue;
2d3b784d 515
1085c0eb 516 if (UNIT_VTABLE(u)->deserialize_item) {
2d3b784d
ZJS
517 r = UNIT_VTABLE(u)->deserialize_item(u, l, v, fds);
518 if (r < 0)
519 log_unit_warning(u, "Failed to deserialize unit parameter '%s', ignoring.", l);
520 }
521 }
522
523 /* Versions before 228 did not carry a state change timestamp. In this case, take the current
524 * time. This is useful, so that timeouts based on this timestamp don't trigger too early, and is
525 * in-line with the logic from before 228 where the base for timeouts was not persistent across
526 * reboots. */
527
528 if (!dual_timestamp_is_set(&u->state_change_timestamp))
fa5a0251 529 dual_timestamp_now(&u->state_change_timestamp);
2d3b784d
ZJS
530
531 /* Let's make sure that everything that is deserialized also gets any potential new cgroup settings
532 * applied after we are done. For that we invalidate anything already realized, so that we can
533 * realize it again. */
cc815b7f
MK
534 if (u->cgroup_realized) {
535 unit_invalidate_cgroup(u, _CGROUP_MASK_ALL);
536 unit_invalidate_cgroup_bpf(u);
537 }
2d3b784d
ZJS
538
539 return 0;
540}
541
5699a168 542int unit_deserialize_state_skip(FILE *f) {
2d3b784d 543 int r;
0ff6ff2b 544
2d3b784d
ZJS
545 assert(f);
546
547 /* Skip serialized data for this unit. We don't know what it is. */
548
549 for (;;) {
550 _cleanup_free_ char *line = NULL;
2d3b784d 551
0ff6ff2b 552 r = read_stripped_line(f, LONG_LINE_MAX, &line);
2d3b784d
ZJS
553 if (r < 0)
554 return log_error_errno(r, "Failed to read serialization line: %m");
555 if (r == 0)
556 return 0;
557
2d3b784d 558 /* End marker */
0ff6ff2b 559 if (isempty(line))
2d3b784d
ZJS
560 return 1;
561 }
562}
563
564static void print_unit_dependency_mask(FILE *f, const char *kind, UnitDependencyMask mask, bool *space) {
565 const struct {
566 UnitDependencyMask mask;
567 const char *name;
568 } table[] = {
569 { UNIT_DEPENDENCY_FILE, "file" },
570 { UNIT_DEPENDENCY_IMPLICIT, "implicit" },
571 { UNIT_DEPENDENCY_DEFAULT, "default" },
572 { UNIT_DEPENDENCY_UDEV, "udev" },
573 { UNIT_DEPENDENCY_PATH, "path" },
87c734ee
FB
574 { UNIT_DEPENDENCY_MOUNT_FILE, "mount-file" },
575 { UNIT_DEPENDENCY_MOUNTINFO, "mountinfo" },
2d3b784d 576 { UNIT_DEPENDENCY_PROC_SWAP, "proc-swap" },
899acf5c 577 { UNIT_DEPENDENCY_SLICE_PROPERTY, "slice-property" },
2d3b784d
ZJS
578 };
579
580 assert(f);
581 assert(kind);
582 assert(space);
583
584 for (size_t i = 0; i < ELEMENTSOF(table); i++) {
585
586 if (mask == 0)
587 break;
588
589 if (FLAGS_SET(mask, table[i].mask)) {
590 if (*space)
591 fputc(' ', f);
592 else
593 *space = true;
594
595 fputs(kind, f);
596 fputs("-", f);
597 fputs(table[i].name, f);
598
599 mask &= ~table[i].mask;
600 }
601 }
602
603 assert(mask == 0);
604}
605
606void unit_dump(Unit *u, FILE *f, const char *prefix) {
de010b0b 607 char *t;
2d3b784d 608 const char *prefix2;
2d3b784d
ZJS
609 Unit *following;
610 _cleanup_set_free_ Set *following_set = NULL;
611 CGroupMask m;
612 int r;
613
614 assert(u);
615 assert(u->type >= 0);
616
617 prefix = strempty(prefix);
618 prefix2 = strjoina(prefix, "\t");
619
620 fprintf(f,
621 "%s-> Unit %s:\n",
622 prefix, u->id);
623
624 SET_FOREACH(t, u->aliases)
625 fprintf(f, "%s\tAlias: %s\n", prefix, t);
626
627 fprintf(f,
628 "%s\tDescription: %s\n"
629 "%s\tInstance: %s\n"
630 "%s\tUnit Load State: %s\n"
631 "%s\tUnit Active State: %s\n"
632 "%s\tState Change Timestamp: %s\n"
633 "%s\tInactive Exit Timestamp: %s\n"
634 "%s\tActive Enter Timestamp: %s\n"
635 "%s\tActive Exit Timestamp: %s\n"
636 "%s\tInactive Enter Timestamp: %s\n"
637 "%s\tMay GC: %s\n"
638 "%s\tNeed Daemon Reload: %s\n"
639 "%s\tTransient: %s\n"
640 "%s\tPerpetual: %s\n"
39628fed 641 "%s\tGarbage Collection Mode: %s\n",
2d3b784d
ZJS
642 prefix, unit_description(u),
643 prefix, strna(u->instance),
644 prefix, unit_load_state_to_string(u->load_state),
645 prefix, unit_active_state_to_string(unit_active_state(u)),
04f5c018
ZJS
646 prefix, strna(FORMAT_TIMESTAMP(u->state_change_timestamp.realtime)),
647 prefix, strna(FORMAT_TIMESTAMP(u->inactive_exit_timestamp.realtime)),
648 prefix, strna(FORMAT_TIMESTAMP(u->active_enter_timestamp.realtime)),
649 prefix, strna(FORMAT_TIMESTAMP(u->active_exit_timestamp.realtime)),
650 prefix, strna(FORMAT_TIMESTAMP(u->inactive_enter_timestamp.realtime)),
2d3b784d
ZJS
651 prefix, yes_no(unit_may_gc(u)),
652 prefix, yes_no(unit_need_daemon_reload(u)),
653 prefix, yes_no(u->transient),
654 prefix, yes_no(u->perpetual),
39628fed 655 prefix, collect_mode_to_string(u->collect_mode));
2d3b784d 656
ff68472a
ZJS
657 if (u->markers != 0) {
658 fprintf(f, "%s\tMarkers:", prefix);
659
660 for (UnitMarker marker = 0; marker < _UNIT_MARKER_MAX; marker++)
661 if (FLAGS_SET(u->markers, 1u << marker))
662 fprintf(f, " %s", unit_marker_to_string(marker));
663 fputs("\n", f);
664 }
665
39628fed
LP
666 if (UNIT_HAS_CGROUP_CONTEXT(u)) {
667 fprintf(f,
668 "%s\tSlice: %s\n"
669 "%s\tCGroup: %s\n"
670 "%s\tCGroup realized: %s\n",
671 prefix, strna(unit_slice_name(u)),
672 prefix, strna(u->cgroup_path),
673 prefix, yes_no(u->cgroup_realized));
674
675 if (u->cgroup_realized_mask != 0) {
676 _cleanup_free_ char *s = NULL;
677 (void) cg_mask_to_string(u->cgroup_realized_mask, &s);
678 fprintf(f, "%s\tCGroup realized mask: %s\n", prefix, strnull(s));
679 }
2d3b784d 680
39628fed
LP
681 if (u->cgroup_enabled_mask != 0) {
682 _cleanup_free_ char *s = NULL;
683 (void) cg_mask_to_string(u->cgroup_enabled_mask, &s);
684 fprintf(f, "%s\tCGroup enabled mask: %s\n", prefix, strnull(s));
685 }
2d3b784d 686
39628fed
LP
687 m = unit_get_own_mask(u);
688 if (m != 0) {
689 _cleanup_free_ char *s = NULL;
690 (void) cg_mask_to_string(m, &s);
691 fprintf(f, "%s\tCGroup own mask: %s\n", prefix, strnull(s));
692 }
2d3b784d 693
39628fed
LP
694 m = unit_get_members_mask(u);
695 if (m != 0) {
696 _cleanup_free_ char *s = NULL;
697 (void) cg_mask_to_string(m, &s);
698 fprintf(f, "%s\tCGroup members mask: %s\n", prefix, strnull(s));
699 }
2d3b784d 700
39628fed
LP
701 m = unit_get_delegate_mask(u);
702 if (m != 0) {
703 _cleanup_free_ char *s = NULL;
704 (void) cg_mask_to_string(m, &s);
705 fprintf(f, "%s\tCGroup delegate mask: %s\n", prefix, strnull(s));
706 }
2d3b784d
ZJS
707 }
708
709 if (!sd_id128_is_null(u->invocation_id))
710 fprintf(f, "%s\tInvocation ID: " SD_ID128_FORMAT_STR "\n",
711 prefix, SD_ID128_FORMAT_VAL(u->invocation_id));
712
713 STRV_FOREACH(j, u->documentation)
714 fprintf(f, "%s\tDocumentation: %s\n", prefix, *j);
715
23e9a7dd
LP
716 if (u->access_selinux_context)
717 fprintf(f, "%s\tAccess SELinux Context: %s\n", prefix, u->access_selinux_context);
718
2d3b784d
ZJS
719 following = unit_following(u);
720 if (following)
721 fprintf(f, "%s\tFollowing: %s\n", prefix, following->id);
722
723 r = unit_following_set(u, &following_set);
724 if (r >= 0) {
725 Unit *other;
726
727 SET_FOREACH(other, following_set)
728 fprintf(f, "%s\tFollowing Set Member: %s\n", prefix, other->id);
729 }
730
731 if (u->fragment_path)
732 fprintf(f, "%s\tFragment Path: %s\n", prefix, u->fragment_path);
733
734 if (u->source_path)
735 fprintf(f, "%s\tSource Path: %s\n", prefix, u->source_path);
736
737 STRV_FOREACH(j, u->dropin_paths)
738 fprintf(f, "%s\tDropIn Path: %s\n", prefix, *j);
739
740 if (u->failure_action != EMERGENCY_ACTION_NONE)
741 fprintf(f, "%s\tFailure Action: %s\n", prefix, emergency_action_to_string(u->failure_action));
742 if (u->failure_action_exit_status >= 0)
743 fprintf(f, "%s\tFailure Action Exit Status: %i\n", prefix, u->failure_action_exit_status);
744 if (u->success_action != EMERGENCY_ACTION_NONE)
745 fprintf(f, "%s\tSuccess Action: %s\n", prefix, emergency_action_to_string(u->success_action));
746 if (u->success_action_exit_status >= 0)
747 fprintf(f, "%s\tSuccess Action Exit Status: %i\n", prefix, u->success_action_exit_status);
748
749 if (u->job_timeout != USEC_INFINITY)
5291f26d 750 fprintf(f, "%s\tJob Timeout: %s\n", prefix, FORMAT_TIMESPAN(u->job_timeout, 0));
2d3b784d
ZJS
751
752 if (u->job_timeout_action != EMERGENCY_ACTION_NONE)
753 fprintf(f, "%s\tJob Timeout Action: %s\n", prefix, emergency_action_to_string(u->job_timeout_action));
754
755 if (u->job_timeout_reboot_arg)
756 fprintf(f, "%s\tJob Timeout Reboot Argument: %s\n", prefix, u->job_timeout_reboot_arg);
757
758 condition_dump_list(u->conditions, f, prefix, condition_type_to_string);
759 condition_dump_list(u->asserts, f, prefix, assert_type_to_string);
760
761 if (dual_timestamp_is_set(&u->condition_timestamp))
762 fprintf(f,
763 "%s\tCondition Timestamp: %s\n"
764 "%s\tCondition Result: %s\n",
04f5c018 765 prefix, strna(FORMAT_TIMESTAMP(u->condition_timestamp.realtime)),
2d3b784d
ZJS
766 prefix, yes_no(u->condition_result));
767
768 if (dual_timestamp_is_set(&u->assert_timestamp))
769 fprintf(f,
770 "%s\tAssert Timestamp: %s\n"
771 "%s\tAssert Result: %s\n",
04f5c018 772 prefix, strna(FORMAT_TIMESTAMP(u->assert_timestamp.realtime)),
2d3b784d
ZJS
773 prefix, yes_no(u->assert_result));
774
775 for (UnitDependency d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
776 UnitDependencyInfo di;
777 Unit *other;
778
15ed3c3a 779 HASHMAP_FOREACH_KEY(di.data, other, unit_get_dependencies(u, d)) {
2d3b784d
ZJS
780 bool space = false;
781
782 fprintf(f, "%s\t%s: %s (", prefix, unit_dependency_to_string(d), other->id);
783
784 print_unit_dependency_mask(f, "origin", di.origin_mask, &space);
785 print_unit_dependency_mask(f, "destination", di.destination_mask, &space);
786
787 fputs(")\n", f);
788 }
789 }
790
791 if (!hashmap_isempty(u->requires_mounts_for)) {
792 UnitDependencyInfo di;
793 const char *path;
794
795 HASHMAP_FOREACH_KEY(di.data, path, u->requires_mounts_for) {
796 bool space = false;
797
798 fprintf(f, "%s\tRequiresMountsFor: %s (", prefix, path);
799
800 print_unit_dependency_mask(f, "origin", di.origin_mask, &space);
801 print_unit_dependency_mask(f, "destination", di.destination_mask, &space);
802
803 fputs(")\n", f);
804 }
805 }
806
807 if (u->load_state == UNIT_LOADED) {
808
809 fprintf(f,
810 "%s\tStopWhenUnneeded: %s\n"
811 "%s\tRefuseManualStart: %s\n"
812 "%s\tRefuseManualStop: %s\n"
813 "%s\tDefaultDependencies: %s\n"
559214cb 814 "%s\tSurviveFinalKillSignal: %s\n"
294446dc 815 "%s\tOnSuccessJobMode: %s\n"
2d3b784d
ZJS
816 "%s\tOnFailureJobMode: %s\n"
817 "%s\tIgnoreOnIsolate: %s\n",
818 prefix, yes_no(u->stop_when_unneeded),
819 prefix, yes_no(u->refuse_manual_start),
820 prefix, yes_no(u->refuse_manual_stop),
821 prefix, yes_no(u->default_dependencies),
559214cb 822 prefix, yes_no(u->survive_final_kill_signal),
294446dc 823 prefix, job_mode_to_string(u->on_success_job_mode),
2d3b784d
ZJS
824 prefix, job_mode_to_string(u->on_failure_job_mode),
825 prefix, yes_no(u->ignore_on_isolate));
826
827 if (UNIT_VTABLE(u)->dump)
828 UNIT_VTABLE(u)->dump(u, f, prefix2);
829
830 } else if (u->load_state == UNIT_MERGED)
831 fprintf(f,
832 "%s\tMerged into: %s\n",
833 prefix, u->merged_into->id);
38553034
ZJS
834 else if (u->load_state == UNIT_ERROR) {
835 errno = abs(u->load_error);
836 fprintf(f, "%s\tLoad Error Code: %m\n", prefix);
837 }
2d3b784d
ZJS
838
839 for (const char *n = sd_bus_track_first(u->bus_track); n; n = sd_bus_track_next(u->bus_track))
840 fprintf(f, "%s\tBus Ref: %s\n", prefix, n);
841
842 if (u->job)
843 job_dump(u->job, f, prefix2);
844
845 if (u->nop_job)
846 job_dump(u->nop_job, f, prefix2);
847}