]>
Commit | Line | Data |
---|---|---|
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 | ||
15 | static 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. */ |
33 | assert_cc(_UNIT_MARKER_MAX <= sizeof(((Unit){}).markers) * 8); | |
34 | ||
35 | static 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 | ||
49 | static 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 |
71 | static 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 | ||
78 | static 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 | ||
85 | static 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 | 92 | int 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 | ||
214 | static 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 |
263 | int 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. */ | |
529 | unit_invalidate_cgroup(u, _CGROUP_MASK_ALL); | |
530 | unit_invalidate_cgroup_bpf(u); | |
531 | ||
532 | return 0; | |
533 | } | |
534 | ||
535 | int unit_deserialize_skip(FILE *f) { | |
536 | int r; | |
537 | assert(f); | |
538 | ||
539 | /* Skip serialized data for this unit. We don't know what it is. */ | |
540 | ||
541 | for (;;) { | |
542 | _cleanup_free_ char *line = NULL; | |
543 | char *l; | |
544 | ||
545 | r = read_line(f, LONG_LINE_MAX, &line); | |
546 | if (r < 0) | |
547 | return log_error_errno(r, "Failed to read serialization line: %m"); | |
548 | if (r == 0) | |
549 | return 0; | |
550 | ||
551 | l = strstrip(line); | |
552 | ||
553 | /* End marker */ | |
554 | if (isempty(l)) | |
555 | return 1; | |
556 | } | |
557 | } | |
558 | ||
559 | static void print_unit_dependency_mask(FILE *f, const char *kind, UnitDependencyMask mask, bool *space) { | |
560 | const struct { | |
561 | UnitDependencyMask mask; | |
562 | const char *name; | |
563 | } table[] = { | |
564 | { UNIT_DEPENDENCY_FILE, "file" }, | |
565 | { UNIT_DEPENDENCY_IMPLICIT, "implicit" }, | |
566 | { UNIT_DEPENDENCY_DEFAULT, "default" }, | |
567 | { UNIT_DEPENDENCY_UDEV, "udev" }, | |
568 | { UNIT_DEPENDENCY_PATH, "path" }, | |
569 | { UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT, "mountinfo-implicit" }, | |
570 | { UNIT_DEPENDENCY_MOUNTINFO_DEFAULT, "mountinfo-default" }, | |
571 | { UNIT_DEPENDENCY_PROC_SWAP, "proc-swap" }, | |
572 | }; | |
573 | ||
574 | assert(f); | |
575 | assert(kind); | |
576 | assert(space); | |
577 | ||
578 | for (size_t i = 0; i < ELEMENTSOF(table); i++) { | |
579 | ||
580 | if (mask == 0) | |
581 | break; | |
582 | ||
583 | if (FLAGS_SET(mask, table[i].mask)) { | |
584 | if (*space) | |
585 | fputc(' ', f); | |
586 | else | |
587 | *space = true; | |
588 | ||
589 | fputs(kind, f); | |
590 | fputs("-", f); | |
591 | fputs(table[i].name, f); | |
592 | ||
593 | mask &= ~table[i].mask; | |
594 | } | |
595 | } | |
596 | ||
597 | assert(mask == 0); | |
598 | } | |
599 | ||
600 | void unit_dump(Unit *u, FILE *f, const char *prefix) { | |
601 | char *t, **j; | |
602 | const char *prefix2; | |
603 | char timestamp[5][FORMAT_TIMESTAMP_MAX], timespan[FORMAT_TIMESPAN_MAX]; | |
604 | Unit *following; | |
605 | _cleanup_set_free_ Set *following_set = NULL; | |
606 | CGroupMask m; | |
607 | int r; | |
608 | ||
609 | assert(u); | |
610 | assert(u->type >= 0); | |
611 | ||
612 | prefix = strempty(prefix); | |
613 | prefix2 = strjoina(prefix, "\t"); | |
614 | ||
615 | fprintf(f, | |
616 | "%s-> Unit %s:\n", | |
617 | prefix, u->id); | |
618 | ||
619 | SET_FOREACH(t, u->aliases) | |
620 | fprintf(f, "%s\tAlias: %s\n", prefix, t); | |
621 | ||
622 | fprintf(f, | |
623 | "%s\tDescription: %s\n" | |
624 | "%s\tInstance: %s\n" | |
625 | "%s\tUnit Load State: %s\n" | |
626 | "%s\tUnit Active State: %s\n" | |
627 | "%s\tState Change Timestamp: %s\n" | |
628 | "%s\tInactive Exit Timestamp: %s\n" | |
629 | "%s\tActive Enter Timestamp: %s\n" | |
630 | "%s\tActive Exit Timestamp: %s\n" | |
631 | "%s\tInactive Enter Timestamp: %s\n" | |
632 | "%s\tMay GC: %s\n" | |
633 | "%s\tNeed Daemon Reload: %s\n" | |
634 | "%s\tTransient: %s\n" | |
635 | "%s\tPerpetual: %s\n" | |
39628fed | 636 | "%s\tGarbage Collection Mode: %s\n", |
2d3b784d ZJS |
637 | prefix, unit_description(u), |
638 | prefix, strna(u->instance), | |
639 | prefix, unit_load_state_to_string(u->load_state), | |
640 | prefix, unit_active_state_to_string(unit_active_state(u)), | |
641 | prefix, strna(format_timestamp(timestamp[0], sizeof(timestamp[0]), u->state_change_timestamp.realtime)), | |
642 | prefix, strna(format_timestamp(timestamp[1], sizeof(timestamp[1]), u->inactive_exit_timestamp.realtime)), | |
643 | prefix, strna(format_timestamp(timestamp[2], sizeof(timestamp[2]), u->active_enter_timestamp.realtime)), | |
644 | prefix, strna(format_timestamp(timestamp[3], sizeof(timestamp[3]), u->active_exit_timestamp.realtime)), | |
645 | prefix, strna(format_timestamp(timestamp[4], sizeof(timestamp[4]), u->inactive_enter_timestamp.realtime)), | |
646 | prefix, yes_no(unit_may_gc(u)), | |
647 | prefix, yes_no(unit_need_daemon_reload(u)), | |
648 | prefix, yes_no(u->transient), | |
649 | prefix, yes_no(u->perpetual), | |
39628fed | 650 | prefix, collect_mode_to_string(u->collect_mode)); |
2d3b784d | 651 | |
ff68472a ZJS |
652 | if (u->markers != 0) { |
653 | fprintf(f, "%s\tMarkers:", prefix); | |
654 | ||
655 | for (UnitMarker marker = 0; marker < _UNIT_MARKER_MAX; marker++) | |
656 | if (FLAGS_SET(u->markers, 1u << marker)) | |
657 | fprintf(f, " %s", unit_marker_to_string(marker)); | |
658 | fputs("\n", f); | |
659 | } | |
660 | ||
39628fed LP |
661 | if (UNIT_HAS_CGROUP_CONTEXT(u)) { |
662 | fprintf(f, | |
663 | "%s\tSlice: %s\n" | |
664 | "%s\tCGroup: %s\n" | |
665 | "%s\tCGroup realized: %s\n", | |
666 | prefix, strna(unit_slice_name(u)), | |
667 | prefix, strna(u->cgroup_path), | |
668 | prefix, yes_no(u->cgroup_realized)); | |
669 | ||
670 | if (u->cgroup_realized_mask != 0) { | |
671 | _cleanup_free_ char *s = NULL; | |
672 | (void) cg_mask_to_string(u->cgroup_realized_mask, &s); | |
673 | fprintf(f, "%s\tCGroup realized mask: %s\n", prefix, strnull(s)); | |
674 | } | |
2d3b784d | 675 | |
39628fed LP |
676 | if (u->cgroup_enabled_mask != 0) { |
677 | _cleanup_free_ char *s = NULL; | |
678 | (void) cg_mask_to_string(u->cgroup_enabled_mask, &s); | |
679 | fprintf(f, "%s\tCGroup enabled mask: %s\n", prefix, strnull(s)); | |
680 | } | |
2d3b784d | 681 | |
39628fed LP |
682 | m = unit_get_own_mask(u); |
683 | if (m != 0) { | |
684 | _cleanup_free_ char *s = NULL; | |
685 | (void) cg_mask_to_string(m, &s); | |
686 | fprintf(f, "%s\tCGroup own mask: %s\n", prefix, strnull(s)); | |
687 | } | |
2d3b784d | 688 | |
39628fed LP |
689 | m = unit_get_members_mask(u); |
690 | if (m != 0) { | |
691 | _cleanup_free_ char *s = NULL; | |
692 | (void) cg_mask_to_string(m, &s); | |
693 | fprintf(f, "%s\tCGroup members mask: %s\n", prefix, strnull(s)); | |
694 | } | |
2d3b784d | 695 | |
39628fed LP |
696 | m = unit_get_delegate_mask(u); |
697 | if (m != 0) { | |
698 | _cleanup_free_ char *s = NULL; | |
699 | (void) cg_mask_to_string(m, &s); | |
700 | fprintf(f, "%s\tCGroup delegate mask: %s\n", prefix, strnull(s)); | |
701 | } | |
2d3b784d ZJS |
702 | } |
703 | ||
704 | if (!sd_id128_is_null(u->invocation_id)) | |
705 | fprintf(f, "%s\tInvocation ID: " SD_ID128_FORMAT_STR "\n", | |
706 | prefix, SD_ID128_FORMAT_VAL(u->invocation_id)); | |
707 | ||
708 | STRV_FOREACH(j, u->documentation) | |
709 | fprintf(f, "%s\tDocumentation: %s\n", prefix, *j); | |
710 | ||
711 | following = unit_following(u); | |
712 | if (following) | |
713 | fprintf(f, "%s\tFollowing: %s\n", prefix, following->id); | |
714 | ||
715 | r = unit_following_set(u, &following_set); | |
716 | if (r >= 0) { | |
717 | Unit *other; | |
718 | ||
719 | SET_FOREACH(other, following_set) | |
720 | fprintf(f, "%s\tFollowing Set Member: %s\n", prefix, other->id); | |
721 | } | |
722 | ||
723 | if (u->fragment_path) | |
724 | fprintf(f, "%s\tFragment Path: %s\n", prefix, u->fragment_path); | |
725 | ||
726 | if (u->source_path) | |
727 | fprintf(f, "%s\tSource Path: %s\n", prefix, u->source_path); | |
728 | ||
729 | STRV_FOREACH(j, u->dropin_paths) | |
730 | fprintf(f, "%s\tDropIn Path: %s\n", prefix, *j); | |
731 | ||
732 | if (u->failure_action != EMERGENCY_ACTION_NONE) | |
733 | fprintf(f, "%s\tFailure Action: %s\n", prefix, emergency_action_to_string(u->failure_action)); | |
734 | if (u->failure_action_exit_status >= 0) | |
735 | fprintf(f, "%s\tFailure Action Exit Status: %i\n", prefix, u->failure_action_exit_status); | |
736 | if (u->success_action != EMERGENCY_ACTION_NONE) | |
737 | fprintf(f, "%s\tSuccess Action: %s\n", prefix, emergency_action_to_string(u->success_action)); | |
738 | if (u->success_action_exit_status >= 0) | |
739 | fprintf(f, "%s\tSuccess Action Exit Status: %i\n", prefix, u->success_action_exit_status); | |
740 | ||
741 | if (u->job_timeout != USEC_INFINITY) | |
742 | fprintf(f, "%s\tJob Timeout: %s\n", prefix, format_timespan(timespan, sizeof(timespan), u->job_timeout, 0)); | |
743 | ||
744 | if (u->job_timeout_action != EMERGENCY_ACTION_NONE) | |
745 | fprintf(f, "%s\tJob Timeout Action: %s\n", prefix, emergency_action_to_string(u->job_timeout_action)); | |
746 | ||
747 | if (u->job_timeout_reboot_arg) | |
748 | fprintf(f, "%s\tJob Timeout Reboot Argument: %s\n", prefix, u->job_timeout_reboot_arg); | |
749 | ||
750 | condition_dump_list(u->conditions, f, prefix, condition_type_to_string); | |
751 | condition_dump_list(u->asserts, f, prefix, assert_type_to_string); | |
752 | ||
753 | if (dual_timestamp_is_set(&u->condition_timestamp)) | |
754 | fprintf(f, | |
755 | "%s\tCondition Timestamp: %s\n" | |
756 | "%s\tCondition Result: %s\n", | |
757 | prefix, strna(format_timestamp(timestamp[0], sizeof(timestamp[0]), u->condition_timestamp.realtime)), | |
758 | prefix, yes_no(u->condition_result)); | |
759 | ||
760 | if (dual_timestamp_is_set(&u->assert_timestamp)) | |
761 | fprintf(f, | |
762 | "%s\tAssert Timestamp: %s\n" | |
763 | "%s\tAssert Result: %s\n", | |
764 | prefix, strna(format_timestamp(timestamp[0], sizeof(timestamp[0]), u->assert_timestamp.realtime)), | |
765 | prefix, yes_no(u->assert_result)); | |
766 | ||
767 | for (UnitDependency d = 0; d < _UNIT_DEPENDENCY_MAX; d++) { | |
768 | UnitDependencyInfo di; | |
769 | Unit *other; | |
770 | ||
15ed3c3a | 771 | HASHMAP_FOREACH_KEY(di.data, other, unit_get_dependencies(u, d)) { |
2d3b784d ZJS |
772 | bool space = false; |
773 | ||
774 | fprintf(f, "%s\t%s: %s (", prefix, unit_dependency_to_string(d), other->id); | |
775 | ||
776 | print_unit_dependency_mask(f, "origin", di.origin_mask, &space); | |
777 | print_unit_dependency_mask(f, "destination", di.destination_mask, &space); | |
778 | ||
779 | fputs(")\n", f); | |
780 | } | |
781 | } | |
782 | ||
783 | if (!hashmap_isempty(u->requires_mounts_for)) { | |
784 | UnitDependencyInfo di; | |
785 | const char *path; | |
786 | ||
787 | HASHMAP_FOREACH_KEY(di.data, path, u->requires_mounts_for) { | |
788 | bool space = false; | |
789 | ||
790 | fprintf(f, "%s\tRequiresMountsFor: %s (", prefix, path); | |
791 | ||
792 | print_unit_dependency_mask(f, "origin", di.origin_mask, &space); | |
793 | print_unit_dependency_mask(f, "destination", di.destination_mask, &space); | |
794 | ||
795 | fputs(")\n", f); | |
796 | } | |
797 | } | |
798 | ||
799 | if (u->load_state == UNIT_LOADED) { | |
800 | ||
801 | fprintf(f, | |
802 | "%s\tStopWhenUnneeded: %s\n" | |
803 | "%s\tRefuseManualStart: %s\n" | |
804 | "%s\tRefuseManualStop: %s\n" | |
805 | "%s\tDefaultDependencies: %s\n" | |
294446dc | 806 | "%s\tOnSuccessJobMode: %s\n" |
2d3b784d ZJS |
807 | "%s\tOnFailureJobMode: %s\n" |
808 | "%s\tIgnoreOnIsolate: %s\n", | |
809 | prefix, yes_no(u->stop_when_unneeded), | |
810 | prefix, yes_no(u->refuse_manual_start), | |
811 | prefix, yes_no(u->refuse_manual_stop), | |
812 | prefix, yes_no(u->default_dependencies), | |
294446dc | 813 | prefix, job_mode_to_string(u->on_success_job_mode), |
2d3b784d ZJS |
814 | prefix, job_mode_to_string(u->on_failure_job_mode), |
815 | prefix, yes_no(u->ignore_on_isolate)); | |
816 | ||
817 | if (UNIT_VTABLE(u)->dump) | |
818 | UNIT_VTABLE(u)->dump(u, f, prefix2); | |
819 | ||
820 | } else if (u->load_state == UNIT_MERGED) | |
821 | fprintf(f, | |
822 | "%s\tMerged into: %s\n", | |
823 | prefix, u->merged_into->id); | |
824 | else if (u->load_state == UNIT_ERROR) | |
825 | fprintf(f, "%s\tLoad Error Code: %s\n", prefix, strerror_safe(u->load_error)); | |
826 | ||
827 | for (const char *n = sd_bus_track_first(u->bus_track); n; n = sd_bus_track_next(u->bus_track)) | |
828 | fprintf(f, "%s\tBus Ref: %s\n", prefix, n); | |
829 | ||
830 | if (u->job) | |
831 | job_dump(u->job, f, prefix2); | |
832 | ||
833 | if (u->nop_job) | |
834 | job_dump(u->nop_job, f, prefix2); | |
835 | } |