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