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