]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
a7334b09 | 2 | |
87f0e418 | 3 | #include <errno.h> |
0301abf4 | 4 | #include <stdlib.h> |
4c253ed1 | 5 | #include <sys/prctl.h> |
4f5dd394 | 6 | #include <unistd.h> |
87f0e418 | 7 | |
718db961 LP |
8 | #include "sd-id128.h" |
9 | #include "sd-messages.h" | |
4f5dd394 | 10 | |
57b7a260 | 11 | #include "all-units.h" |
d68c645b | 12 | #include "alloc-util.h" |
fab34748 | 13 | #include "bpf-firewall.h" |
4f5dd394 LP |
14 | #include "bus-common-errors.h" |
15 | #include "bus-util.h" | |
fdb3deca | 16 | #include "cgroup-setup.h" |
c6c18be3 | 17 | #include "cgroup-util.h" |
e30bbc90 | 18 | #include "core-varlink.h" |
4f5dd394 LP |
19 | #include "dbus-unit.h" |
20 | #include "dbus.h" | |
21 | #include "dropin.h" | |
22 | #include "escape.h" | |
23 | #include "execute.h" | |
6a48d82f | 24 | #include "fd-util.h" |
a5c32cff | 25 | #include "fileio-label.h" |
ee228be1 | 26 | #include "fileio.h" |
f97b34a6 | 27 | #include "format-util.h" |
4b58153d | 28 | #include "id128-util.h" |
915b1d01 | 29 | #include "io-util.h" |
5cfa33e0 | 30 | #include "install.h" |
a3f5fd96 | 31 | #include "label.h" |
4f5dd394 LP |
32 | #include "load-dropin.h" |
33 | #include "load-fragment.h" | |
34 | #include "log.h" | |
35 | #include "macro.h" | |
f5947a5e | 36 | #include "missing_audit.h" |
4f5dd394 | 37 | #include "mkdir.h" |
6bedfcbb | 38 | #include "parse-util.h" |
4f5dd394 | 39 | #include "path-util.h" |
0b452006 | 40 | #include "process-util.h" |
810ef318 | 41 | #include "rm-rf.h" |
d68c645b | 42 | #include "serialize.h" |
4f5dd394 | 43 | #include "set.h" |
6eb7c172 | 44 | #include "signal-util.h" |
d3070fbd | 45 | #include "sparse-endian.h" |
e9db43d5 | 46 | #include "special.h" |
2e59b241 | 47 | #include "specifier.h" |
8fcde012 | 48 | #include "stat-util.h" |
d054f0a4 | 49 | #include "stdio-util.h" |
5afe510c | 50 | #include "string-table.h" |
07630cea | 51 | #include "string-util.h" |
4f5dd394 | 52 | #include "strv.h" |
5b262f74 | 53 | #include "terminal-util.h" |
e4de7287 | 54 | #include "tmpfile-util.h" |
4f4afc88 | 55 | #include "umask-util.h" |
4f5dd394 | 56 | #include "unit-name.h" |
e9db43d5 | 57 | #include "unit.h" |
b1d4f8e1 LP |
58 | #include "user-util.h" |
59 | #include "virt.h" | |
87f0e418 | 60 | |
37109b85 ZJS |
61 | /* Thresholds for logging at INFO level about resource consumption */ |
62 | #define MENTIONWORTHY_CPU_NSEC (1 * NSEC_PER_SEC) | |
63 | #define MENTIONWORTHY_IO_BYTES (1024 * 1024ULL) | |
64 | #define MENTIONWORTHY_IP_BYTES (0ULL) | |
65 | ||
66 | /* Thresholds for logging at INFO level about resource consumption */ | |
67 | #define NOTICEWORTHY_CPU_NSEC (10*60 * NSEC_PER_SEC) /* 10 minutes */ | |
68 | #define NOTICEWORTHY_IO_BYTES (10 * 1024 * 1024ULL) /* 10 MB */ | |
69 | #define NOTICEWORTHY_IP_BYTES (128 * 1024 * 1024ULL) /* 128 MB */ | |
70 | ||
87f0e418 LP |
71 | const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = { |
72 | [UNIT_SERVICE] = &service_vtable, | |
87f0e418 LP |
73 | [UNIT_SOCKET] = &socket_vtable, |
74 | [UNIT_TARGET] = &target_vtable, | |
75 | [UNIT_DEVICE] = &device_vtable, | |
76 | [UNIT_MOUNT] = &mount_vtable, | |
77 | [UNIT_AUTOMOUNT] = &automount_vtable, | |
01f78473 | 78 | [UNIT_SWAP] = &swap_vtable, |
e821075a | 79 | [UNIT_TIMER] = &timer_vtable, |
a016b922 | 80 | [UNIT_PATH] = &path_vtable, |
6c12b52e | 81 | [UNIT_SLICE] = &slice_vtable, |
5afe510c | 82 | [UNIT_SCOPE] = &scope_vtable, |
87f0e418 LP |
83 | }; |
84 | ||
f2341e0a | 85 | static void maybe_warn_about_dependency(Unit *u, const char *other, UnitDependency dependency); |
d1fab3fe | 86 | |
7d17cfbc | 87 | Unit *unit_new(Manager *m, size_t size) { |
87f0e418 LP |
88 | Unit *u; |
89 | ||
90 | assert(m); | |
ac155bb8 | 91 | assert(size >= sizeof(Unit)); |
87f0e418 | 92 | |
7d17cfbc MS |
93 | u = malloc0(size); |
94 | if (!u) | |
87f0e418 LP |
95 | return NULL; |
96 | ||
ac155bb8 MS |
97 | u->manager = m; |
98 | u->type = _UNIT_TYPE_INVALID; | |
ac155bb8 MS |
99 | u->default_dependencies = true; |
100 | u->unit_file_state = _UNIT_FILE_STATE_INVALID; | |
d2dc52db | 101 | u->unit_file_preset = -1; |
d420282b | 102 | u->on_failure_job_mode = JOB_REPLACE; |
0bb814c2 | 103 | u->cgroup_control_inotify_wd = -1; |
afcfaa69 | 104 | u->cgroup_memory_inotify_wd = -1; |
36c16a7c | 105 | u->job_timeout = USEC_INFINITY; |
a2df3ea4 | 106 | u->job_running_timeout = USEC_INFINITY; |
00d9ef85 LP |
107 | u->ref_uid = UID_INVALID; |
108 | u->ref_gid = GID_INVALID; | |
fe700f46 | 109 | u->cpu_usage_last = NSEC_INFINITY; |
17f14955 | 110 | u->cgroup_invalidated_mask |= CGROUP_MASK_BPF_FIREWALL; |
7af67e9a | 111 | u->failure_action_exit_status = u->success_action_exit_status = -1; |
87f0e418 | 112 | |
6a48d82f DM |
113 | u->ip_accounting_ingress_map_fd = -1; |
114 | u->ip_accounting_egress_map_fd = -1; | |
115 | u->ipv4_allow_map_fd = -1; | |
116 | u->ipv6_allow_map_fd = -1; | |
117 | u->ipv4_deny_map_fd = -1; | |
118 | u->ipv6_deny_map_fd = -1; | |
119 | ||
2e59b241 LP |
120 | u->last_section_private = -1; |
121 | ||
7bf081a1 | 122 | u->start_ratelimit = (RateLimit) { m->default_start_limit_interval, m->default_start_limit_burst }; |
8c227e7f | 123 | u->auto_stop_ratelimit = (RateLimit) { 10 * USEC_PER_SEC, 16 }; |
bea355da | 124 | |
fbe14fc9 LP |
125 | for (CGroupIOAccountingMetric i = 0; i < _CGROUP_IO_ACCOUNTING_METRIC_MAX; i++) |
126 | u->io_accounting_last[i] = UINT64_MAX; | |
127 | ||
87f0e418 LP |
128 | return u; |
129 | } | |
130 | ||
a581e45a | 131 | int unit_new_for_name(Manager *m, size_t size, const char *name, Unit **ret) { |
dc409696 | 132 | _cleanup_(unit_freep) Unit *u = NULL; |
a581e45a LP |
133 | int r; |
134 | ||
135 | u = unit_new(m, size); | |
136 | if (!u) | |
137 | return -ENOMEM; | |
138 | ||
139 | r = unit_add_name(u, name); | |
dc409696 | 140 | if (r < 0) |
a581e45a | 141 | return r; |
a581e45a | 142 | |
1cc6c93a YW |
143 | *ret = TAKE_PTR(u); |
144 | ||
a581e45a LP |
145 | return r; |
146 | } | |
147 | ||
303ee601 | 148 | bool unit_has_name(const Unit *u, const char *name) { |
f278026d LP |
149 | assert(u); |
150 | assert(name); | |
151 | ||
4562c355 ZJS |
152 | return streq_ptr(name, u->id) || |
153 | set_contains(u->aliases, name); | |
f278026d LP |
154 | } |
155 | ||
598459ce LP |
156 | static void unit_init(Unit *u) { |
157 | CGroupContext *cc; | |
158 | ExecContext *ec; | |
159 | KillContext *kc; | |
160 | ||
161 | assert(u); | |
162 | assert(u->manager); | |
163 | assert(u->type >= 0); | |
164 | ||
165 | cc = unit_get_cgroup_context(u); | |
166 | if (cc) { | |
167 | cgroup_context_init(cc); | |
168 | ||
169 | /* Copy in the manager defaults into the cgroup | |
170 | * context, _before_ the rest of the settings have | |
171 | * been initialized */ | |
172 | ||
173 | cc->cpu_accounting = u->manager->default_cpu_accounting; | |
13c31542 | 174 | cc->io_accounting = u->manager->default_io_accounting; |
598459ce LP |
175 | cc->blockio_accounting = u->manager->default_blockio_accounting; |
176 | cc->memory_accounting = u->manager->default_memory_accounting; | |
03a7b521 | 177 | cc->tasks_accounting = u->manager->default_tasks_accounting; |
6a48d82f | 178 | cc->ip_accounting = u->manager->default_ip_accounting; |
0af20ea2 LP |
179 | |
180 | if (u->type != UNIT_SLICE) | |
181 | cc->tasks_max = u->manager->default_tasks_max; | |
598459ce LP |
182 | } |
183 | ||
184 | ec = unit_get_exec_context(u); | |
b1edf445 | 185 | if (ec) { |
598459ce LP |
186 | exec_context_init(ec); |
187 | ||
5e37d193 FB |
188 | if (MANAGER_IS_SYSTEM(u->manager)) |
189 | ec->keyring_mode = EXEC_KEYRING_SHARED; | |
190 | else { | |
191 | ec->keyring_mode = EXEC_KEYRING_INHERIT; | |
192 | ||
193 | /* User manager might have its umask redefined by PAM or UMask=. In this | |
194 | * case let the units it manages inherit this value by default. They can | |
195 | * still tune this value through their own unit file */ | |
196 | (void) get_process_umask(getpid_cached(), &ec->umask); | |
197 | } | |
b1edf445 LP |
198 | } |
199 | ||
598459ce LP |
200 | kc = unit_get_kill_context(u); |
201 | if (kc) | |
202 | kill_context_init(kc); | |
203 | ||
204 | if (UNIT_VTABLE(u)->init) | |
205 | UNIT_VTABLE(u)->init(u); | |
206 | } | |
207 | ||
4562c355 ZJS |
208 | static int unit_add_alias(Unit *u, char *donated_name) { |
209 | int r; | |
210 | ||
211 | /* Make sure that u->names is allocated. We may leave u->names | |
212 | * empty if we fail later, but this is not a problem. */ | |
973bc32a | 213 | r = set_ensure_put(&u->aliases, &string_hash_ops, donated_name); |
4562c355 ZJS |
214 | if (r < 0) |
215 | return r; | |
216 | assert(r > 0); | |
217 | ||
218 | return 0; | |
219 | } | |
220 | ||
87f0e418 | 221 | int unit_add_name(Unit *u, const char *text) { |
4562c355 | 222 | _cleanup_free_ char *name = NULL, *instance = NULL; |
87f0e418 | 223 | UnitType t; |
87f0e418 LP |
224 | int r; |
225 | ||
226 | assert(u); | |
227 | assert(text); | |
228 | ||
7410616c | 229 | if (unit_name_is_valid(text, UNIT_NAME_TEMPLATE)) { |
ac155bb8 | 230 | if (!u->instance) |
acd1987a WY |
231 | return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL), |
232 | "instance is not set when adding name '%s': %m", text); | |
87f0e418 | 233 | |
4562c355 | 234 | r = unit_name_replace_instance(text, u->instance, &name); |
7410616c | 235 | if (r < 0) |
acd1987a WY |
236 | return log_unit_debug_errno(u, r, |
237 | "failed to build instance name from '%s': %m", text); | |
7410616c | 238 | } else { |
4562c355 ZJS |
239 | name = strdup(text); |
240 | if (!name) | |
7410616c LP |
241 | return -ENOMEM; |
242 | } | |
87f0e418 | 243 | |
4562c355 | 244 | if (unit_has_name(u, name)) |
7410616c | 245 | return 0; |
4562c355 ZJS |
246 | |
247 | if (hashmap_contains(u->manager->units, name)) | |
acd1987a | 248 | return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EEXIST), |
4562c355 | 249 | "unit already exist when adding name '%s': %m", name); |
7410616c | 250 | |
4562c355 | 251 | if (!unit_name_is_valid(name, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) |
acd1987a | 252 | return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL), |
4562c355 | 253 | "name '%s' is invalid: %m", name); |
e537352b | 254 | |
4562c355 | 255 | t = unit_name_to_type(name); |
7410616c | 256 | if (t < 0) |
acd1987a | 257 | return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL), |
cd990847 | 258 | "failed to derive unit type from name '%s': %m", name); |
87f0e418 | 259 | |
598459ce | 260 | if (u->type != _UNIT_TYPE_INVALID && t != u->type) |
acd1987a WY |
261 | return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL), |
262 | "unit type is illegal: u->type(%d) and t(%d) for name '%s': %m", | |
4562c355 | 263 | u->type, t, name); |
87f0e418 | 264 | |
4562c355 | 265 | r = unit_name_to_instance(name, &instance); |
e48614c4 | 266 | if (r < 0) |
4562c355 | 267 | return log_unit_debug_errno(u, r, "failed to extract instance from name '%s': %m", name); |
87f0e418 | 268 | |
4562c355 ZJS |
269 | if (instance && !unit_type_may_template(t)) |
270 | return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL), "templates are not allowed for name '%s': %m", name); | |
9e2f7c11 | 271 | |
d383acad ZJS |
272 | /* Ensure that this unit either has no instance, or that the instance matches. */ |
273 | if (u->type != _UNIT_TYPE_INVALID && !streq_ptr(u->instance, instance)) | |
acd1987a | 274 | return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL), |
ada4b34e ZJS |
275 | "cannot add name %s, the instances don't match (\"%s\" != \"%s\").", |
276 | name, instance, u->instance); | |
9e2f7c11 | 277 | |
4562c355 | 278 | if (u->id && !unit_type_may_alias(t)) |
ada4b34e ZJS |
279 | return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EEXIST), |
280 | "cannot add name %s, aliases are not allowed for %s units.", | |
281 | name, unit_type_to_string(t)); | |
9e2f7c11 | 282 | |
598459ce | 283 | if (hashmap_size(u->manager->units) >= MANAGER_MAX_NAMES) |
ada4b34e | 284 | return log_unit_warning_errno(u, SYNTHETIC_ERRNO(E2BIG), "cannot add name, manager has too many units: %m"); |
4f0f902f | 285 | |
4562c355 ZJS |
286 | /* Add name to the global hashmap first, because that's easier to undo */ |
287 | r = hashmap_put(u->manager->units, name, u); | |
7410616c | 288 | if (r < 0) |
acd1987a | 289 | return log_unit_debug_errno(u, r, "add unit to hashmap failed for name '%s': %m", text); |
87f0e418 | 290 | |
4562c355 ZJS |
291 | if (u->id) { |
292 | r = unit_add_alias(u, name); /* unit_add_alias() takes ownership of the name on success */ | |
293 | if (r < 0) { | |
294 | hashmap_remove(u->manager->units, name); | |
295 | return r; | |
296 | } | |
297 | TAKE_PTR(name); | |
298 | ||
299 | } else { | |
300 | /* A new name, we don't need the set yet. */ | |
301 | assert(u->type == _UNIT_TYPE_INVALID); | |
302 | assert(!u->instance); | |
303 | ||
ac155bb8 | 304 | u->type = t; |
4562c355 ZJS |
305 | u->id = TAKE_PTR(name); |
306 | u->instance = TAKE_PTR(instance); | |
9e2f7c11 | 307 | |
71fda00f | 308 | LIST_PREPEND(units_by_type, u->manager->units_by_type[t], u); |
598459ce | 309 | unit_init(u); |
598459ce | 310 | } |
9e2f7c11 | 311 | |
598459ce LP |
312 | unit_add_to_dbus_queue(u); |
313 | return 0; | |
87f0e418 LP |
314 | } |
315 | ||
0ae97ec1 | 316 | int unit_choose_id(Unit *u, const char *name) { |
68eda4bd | 317 | _cleanup_free_ char *t = NULL; |
4562c355 | 318 | char *s; |
276c3e78 | 319 | int r; |
0ae97ec1 LP |
320 | |
321 | assert(u); | |
322 | assert(name); | |
323 | ||
7410616c | 324 | if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
ac155bb8 | 325 | if (!u->instance) |
9e2f7c11 LP |
326 | return -EINVAL; |
327 | ||
7410616c LP |
328 | r = unit_name_replace_instance(name, u->instance, &t); |
329 | if (r < 0) | |
330 | return r; | |
9e2f7c11 LP |
331 | |
332 | name = t; | |
333 | } | |
334 | ||
4562c355 ZJS |
335 | if (streq_ptr(u->id, name)) |
336 | return 0; /* Nothing to do. */ | |
337 | ||
338 | /* Selects one of the aliases of this unit as the id */ | |
339 | s = set_get(u->aliases, (char*) name); | |
9e2f7c11 | 340 | if (!s) |
0ae97ec1 LP |
341 | return -ENOENT; |
342 | ||
4562c355 ZJS |
343 | if (u->id) { |
344 | r = set_remove_and_put(u->aliases, name, u->id); | |
345 | if (r < 0) | |
346 | return r; | |
347 | } else | |
348 | assert_se(set_remove(u->aliases, name)); /* see set_get() above… */ | |
276c3e78 | 349 | |
4562c355 | 350 | u->id = s; /* Old u->id is now stored in the set, and s is not stored anywhere */ |
c1e1601e | 351 | unit_add_to_dbus_queue(u); |
9e2f7c11 | 352 | |
0ae97ec1 LP |
353 | return 0; |
354 | } | |
355 | ||
f50e0a01 | 356 | int unit_set_description(Unit *u, const char *description) { |
84b26d51 | 357 | int r; |
f50e0a01 LP |
358 | |
359 | assert(u); | |
360 | ||
84b26d51 LP |
361 | r = free_and_strdup(&u->description, empty_to_null(description)); |
362 | if (r < 0) | |
363 | return r; | |
364 | if (r > 0) | |
365 | unit_add_to_dbus_queue(u); | |
c1e1601e | 366 | |
f50e0a01 LP |
367 | return 0; |
368 | } | |
369 | ||
f2f725e5 | 370 | bool unit_may_gc(Unit *u) { |
a354329f | 371 | UnitActiveState state; |
e98b2fbb | 372 | int r; |
5afe510c | 373 | |
701cc384 LP |
374 | assert(u); |
375 | ||
f2f725e5 ZJS |
376 | /* Checks whether the unit is ready to be unloaded for garbage collection. |
377 | * Returns true when the unit may be collected, and false if there's some | |
2641f02e ZJS |
378 | * reason to keep it loaded. |
379 | * | |
380 | * References from other units are *not* checked here. Instead, this is done | |
381 | * in unit_gc_sweep(), but using markers to properly collect dependency loops. | |
382 | */ | |
5afe510c | 383 | |
a354329f | 384 | if (u->job) |
f2f725e5 | 385 | return false; |
701cc384 | 386 | |
a354329f | 387 | if (u->nop_job) |
f2f725e5 | 388 | return false; |
6c073082 | 389 | |
a354329f LP |
390 | state = unit_active_state(u); |
391 | ||
7eb2a8a1 | 392 | /* If the unit is inactive and failed and no job is queued for it, then release its runtime resources */ |
a354329f LP |
393 | if (UNIT_IS_INACTIVE_OR_FAILED(state) && |
394 | UNIT_VTABLE(u)->release_resources) | |
7eb2a8a1 | 395 | UNIT_VTABLE(u)->release_resources(u); |
a354329f | 396 | |
f5869324 | 397 | if (u->perpetual) |
f2f725e5 | 398 | return false; |
9d576438 | 399 | |
05a98afd | 400 | if (sd_bus_track_count(u->bus_track) > 0) |
f2f725e5 | 401 | return false; |
05a98afd | 402 | |
5afe510c LP |
403 | /* But we keep the unit object around for longer when it is referenced or configured to not be gc'ed */ |
404 | switch (u->collect_mode) { | |
405 | ||
406 | case COLLECT_INACTIVE: | |
407 | if (state != UNIT_INACTIVE) | |
f2f725e5 | 408 | return false; |
5afe510c LP |
409 | |
410 | break; | |
411 | ||
412 | case COLLECT_INACTIVE_OR_FAILED: | |
413 | if (!IN_SET(state, UNIT_INACTIVE, UNIT_FAILED)) | |
f2f725e5 | 414 | return false; |
5afe510c LP |
415 | |
416 | break; | |
417 | ||
418 | default: | |
419 | assert_not_reached("Unknown garbage collection mode"); | |
420 | } | |
421 | ||
e98b2fbb LP |
422 | if (u->cgroup_path) { |
423 | /* If the unit has a cgroup, then check whether there's anything in it. If so, we should stay | |
424 | * around. Units with active processes should never be collected. */ | |
425 | ||
426 | r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path); | |
427 | if (r < 0) | |
428 | log_unit_debug_errno(u, r, "Failed to determine whether cgroup %s is empty: %m", u->cgroup_path); | |
429 | if (r <= 0) | |
f2f725e5 | 430 | return false; |
e98b2fbb LP |
431 | } |
432 | ||
f2f725e5 ZJS |
433 | if (UNIT_VTABLE(u)->may_gc && !UNIT_VTABLE(u)->may_gc(u)) |
434 | return false; | |
701cc384 | 435 | |
f2f725e5 | 436 | return true; |
701cc384 LP |
437 | } |
438 | ||
87f0e418 LP |
439 | void unit_add_to_load_queue(Unit *u) { |
440 | assert(u); | |
ac155bb8 | 441 | assert(u->type != _UNIT_TYPE_INVALID); |
87f0e418 | 442 | |
ac155bb8 | 443 | if (u->load_state != UNIT_STUB || u->in_load_queue) |
87f0e418 LP |
444 | return; |
445 | ||
71fda00f | 446 | LIST_PREPEND(load_queue, u->manager->load_queue, u); |
ac155bb8 | 447 | u->in_load_queue = true; |
87f0e418 LP |
448 | } |
449 | ||
23a177ef LP |
450 | void unit_add_to_cleanup_queue(Unit *u) { |
451 | assert(u); | |
452 | ||
ac155bb8 | 453 | if (u->in_cleanup_queue) |
23a177ef LP |
454 | return; |
455 | ||
71fda00f | 456 | LIST_PREPEND(cleanup_queue, u->manager->cleanup_queue, u); |
ac155bb8 | 457 | u->in_cleanup_queue = true; |
23a177ef LP |
458 | } |
459 | ||
701cc384 LP |
460 | void unit_add_to_gc_queue(Unit *u) { |
461 | assert(u); | |
462 | ||
ac155bb8 | 463 | if (u->in_gc_queue || u->in_cleanup_queue) |
701cc384 LP |
464 | return; |
465 | ||
f2f725e5 | 466 | if (!unit_may_gc(u)) |
701cc384 LP |
467 | return; |
468 | ||
c5a97ed1 | 469 | LIST_PREPEND(gc_queue, u->manager->gc_unit_queue, u); |
ac155bb8 | 470 | u->in_gc_queue = true; |
701cc384 LP |
471 | } |
472 | ||
c1e1601e LP |
473 | void unit_add_to_dbus_queue(Unit *u) { |
474 | assert(u); | |
ac155bb8 | 475 | assert(u->type != _UNIT_TYPE_INVALID); |
c1e1601e | 476 | |
ac155bb8 | 477 | if (u->load_state == UNIT_STUB || u->in_dbus_queue) |
c1e1601e LP |
478 | return; |
479 | ||
a567261a | 480 | /* Shortcut things if nobody cares */ |
8f8f05a9 | 481 | if (sd_bus_track_count(u->manager->subscribed) <= 0 && |
ae572acd | 482 | sd_bus_track_count(u->bus_track) <= 0 && |
8f8f05a9 | 483 | set_isempty(u->manager->private_buses)) { |
ac155bb8 | 484 | u->sent_dbus_new_signal = true; |
94b6dfa2 LP |
485 | return; |
486 | } | |
487 | ||
71fda00f | 488 | LIST_PREPEND(dbus_queue, u->manager->dbus_unit_queue, u); |
ac155bb8 | 489 | u->in_dbus_queue = true; |
c1e1601e LP |
490 | } |
491 | ||
fda09318 | 492 | void unit_submit_to_stop_when_unneeded_queue(Unit *u) { |
a3c1168a LP |
493 | assert(u); |
494 | ||
495 | if (u->in_stop_when_unneeded_queue) | |
496 | return; | |
497 | ||
498 | if (!u->stop_when_unneeded) | |
499 | return; | |
500 | ||
501 | if (!UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) | |
502 | return; | |
503 | ||
504 | LIST_PREPEND(stop_when_unneeded_queue, u->manager->stop_when_unneeded_queue, u); | |
505 | u->in_stop_when_unneeded_queue = true; | |
506 | } | |
507 | ||
eef85c4a | 508 | static void bidi_set_free(Unit *u, Hashmap *h) { |
87f0e418 | 509 | Unit *other; |
eef85c4a | 510 | void *v; |
87f0e418 LP |
511 | |
512 | assert(u); | |
513 | ||
eef85c4a | 514 | /* Frees the hashmap and makes sure we are dropped from the inverse pointers */ |
87f0e418 | 515 | |
90e74a66 | 516 | HASHMAP_FOREACH_KEY(v, other, h) { |
f6173cb9 | 517 | for (UnitDependency d = 0; d < _UNIT_DEPENDENCY_MAX; d++) |
eef85c4a | 518 | hashmap_remove(other->dependencies[d], u); |
701cc384 LP |
519 | |
520 | unit_add_to_gc_queue(other); | |
87f0e418 LP |
521 | } |
522 | ||
eef85c4a | 523 | hashmap_free(h); |
87f0e418 LP |
524 | } |
525 | ||
c2756a68 LP |
526 | static void unit_remove_transient(Unit *u) { |
527 | char **i; | |
528 | ||
529 | assert(u); | |
530 | ||
531 | if (!u->transient) | |
532 | return; | |
533 | ||
534 | if (u->fragment_path) | |
3f5e8115 | 535 | (void) unlink(u->fragment_path); |
c2756a68 LP |
536 | |
537 | STRV_FOREACH(i, u->dropin_paths) { | |
39591351 | 538 | _cleanup_free_ char *p = NULL, *pp = NULL; |
c2756a68 | 539 | |
39591351 LP |
540 | p = dirname_malloc(*i); /* Get the drop-in directory from the drop-in file */ |
541 | if (!p) | |
542 | continue; | |
543 | ||
544 | pp = dirname_malloc(p); /* Get the config directory from the drop-in directory */ | |
545 | if (!pp) | |
546 | continue; | |
547 | ||
548 | /* Only drop transient drop-ins */ | |
549 | if (!path_equal(u->manager->lookup_paths.transient, pp)) | |
550 | continue; | |
c2756a68 | 551 | |
39591351 LP |
552 | (void) unlink(*i); |
553 | (void) rmdir(p); | |
c2756a68 LP |
554 | } |
555 | } | |
556 | ||
a57f7e2c | 557 | static void unit_free_requires_mounts_for(Unit *u) { |
eef85c4a | 558 | assert(u); |
a57f7e2c | 559 | |
eef85c4a LP |
560 | for (;;) { |
561 | _cleanup_free_ char *path; | |
a57f7e2c | 562 | |
eef85c4a LP |
563 | path = hashmap_steal_first_key(u->requires_mounts_for); |
564 | if (!path) | |
565 | break; | |
566 | else { | |
567 | char s[strlen(path) + 1]; | |
a57f7e2c | 568 | |
eef85c4a LP |
569 | PATH_FOREACH_PREFIX_MORE(s, path) { |
570 | char *y; | |
571 | Set *x; | |
572 | ||
573 | x = hashmap_get2(u->manager->units_requiring_mounts_for, s, (void**) &y); | |
574 | if (!x) | |
575 | continue; | |
a57f7e2c | 576 | |
eef85c4a | 577 | (void) set_remove(x, u); |
a57f7e2c | 578 | |
eef85c4a LP |
579 | if (set_isempty(x)) { |
580 | (void) hashmap_remove(u->manager->units_requiring_mounts_for, y); | |
581 | free(y); | |
582 | set_free(x); | |
583 | } | |
a57f7e2c LP |
584 | } |
585 | } | |
586 | } | |
587 | ||
eef85c4a | 588 | u->requires_mounts_for = hashmap_free(u->requires_mounts_for); |
a57f7e2c LP |
589 | } |
590 | ||
598459ce LP |
591 | static void unit_done(Unit *u) { |
592 | ExecContext *ec; | |
593 | CGroupContext *cc; | |
594 | ||
595 | assert(u); | |
596 | ||
597 | if (u->type < 0) | |
598 | return; | |
599 | ||
600 | if (UNIT_VTABLE(u)->done) | |
601 | UNIT_VTABLE(u)->done(u); | |
602 | ||
603 | ec = unit_get_exec_context(u); | |
604 | if (ec) | |
605 | exec_context_done(ec); | |
606 | ||
607 | cc = unit_get_cgroup_context(u); | |
608 | if (cc) | |
609 | cgroup_context_done(cc); | |
610 | } | |
611 | ||
87f0e418 | 612 | void unit_free(Unit *u) { |
87f0e418 LP |
613 | char *t; |
614 | ||
c9d5c9c0 LP |
615 | if (!u) |
616 | return; | |
87f0e418 | 617 | |
50fb00b7 | 618 | u->transient_file = safe_fclose(u->transient_file); |
4f4afc88 | 619 | |
2c289ea8 | 620 | if (!MANAGER_IS_RELOADING(u->manager)) |
c2756a68 LP |
621 | unit_remove_transient(u); |
622 | ||
c1e1601e LP |
623 | bus_unit_send_removed_signal(u); |
624 | ||
598459ce | 625 | unit_done(u); |
a013b84b | 626 | |
50be4f4a | 627 | unit_dequeue_rewatch_pids(u); |
cf9fd508 | 628 | |
50be4f4a | 629 | sd_bus_slot_unref(u->match_bus_slot); |
05a98afd LP |
630 | sd_bus_track_unref(u->bus_track); |
631 | u->deserialized_refs = strv_free(u->deserialized_refs); | |
d9e45bc3 | 632 | u->pending_freezer_message = sd_bus_message_unref(u->pending_freezer_message); |
05a98afd | 633 | |
a57f7e2c LP |
634 | unit_free_requires_mounts_for(u); |
635 | ||
90e74a66 | 636 | SET_FOREACH(t, u->aliases) |
ac155bb8 | 637 | hashmap_remove_value(u->manager->units, t, u); |
4562c355 ZJS |
638 | if (u->id) |
639 | hashmap_remove_value(u->manager->units, u->id, u); | |
87f0e418 | 640 | |
4b58153d LP |
641 | if (!sd_id128_is_null(u->invocation_id)) |
642 | hashmap_remove_value(u->manager->units_by_invocation_id, &u->invocation_id, u); | |
643 | ||
97e7d748 MS |
644 | if (u->job) { |
645 | Job *j = u->job; | |
646 | job_uninstall(j); | |
647 | job_free(j); | |
648 | } | |
964e0949 | 649 | |
e0209d83 MS |
650 | if (u->nop_job) { |
651 | Job *j = u->nop_job; | |
652 | job_uninstall(j); | |
653 | job_free(j); | |
654 | } | |
655 | ||
f6173cb9 | 656 | for (UnitDependency d = 0; d < _UNIT_DEPENDENCY_MAX; d++) |
ac155bb8 | 657 | bidi_set_free(u, u->dependencies[d]); |
964e0949 | 658 | |
4c591f39 MK |
659 | /* A unit is being dropped from the tree, make sure our family is realized properly. Do this after we |
660 | * detach the unit from slice tree in order to eliminate its effect on controller masks. */ | |
f23ba94d | 661 | if (UNIT_ISSET(u->slice)) |
4c591f39 | 662 | unit_add_family_to_cgroup_realize_queue(UNIT_DEREF(u->slice)); |
fb46fca7 | 663 | |
adefcf28 LP |
664 | if (u->on_console) |
665 | manager_unref_console(u->manager); | |
666 | ||
efdb0237 | 667 | unit_release_cgroup(u); |
72673e86 | 668 | |
d3070fbd LP |
669 | if (!MANAGER_IS_RELOADING(u->manager)) |
670 | unit_unlink_state_files(u); | |
671 | ||
00d9ef85 LP |
672 | unit_unref_uid_gid(u, false); |
673 | ||
5269eb6b | 674 | (void) manager_update_failed_units(u->manager, u, false); |
db785129 | 675 | set_remove(u->manager->startup_units, u); |
f755e3b7 | 676 | |
a911bb9a LP |
677 | unit_unwatch_all_pids(u); |
678 | ||
702a2d8f | 679 | unit_ref_unset(&u->slice); |
7f7d01ed ZJS |
680 | while (u->refs_by_target) |
681 | unit_ref_unset(u->refs_by_target); | |
57020a3a | 682 | |
ac155bb8 | 683 | if (u->type != _UNIT_TYPE_INVALID) |
71fda00f | 684 | LIST_REMOVE(units_by_type, u->manager->units_by_type[u->type], u); |
ef734fd6 | 685 | |
ac155bb8 | 686 | if (u->in_load_queue) |
71fda00f | 687 | LIST_REMOVE(load_queue, u->manager->load_queue, u); |
87f0e418 | 688 | |
ac155bb8 | 689 | if (u->in_dbus_queue) |
71fda00f | 690 | LIST_REMOVE(dbus_queue, u->manager->dbus_unit_queue, u); |
c1e1601e | 691 | |
a2d72e26 | 692 | if (u->in_gc_queue) |
c5a97ed1 | 693 | LIST_REMOVE(gc_queue, u->manager->gc_unit_queue, u); |
701cc384 | 694 | |
91a6073e LP |
695 | if (u->in_cgroup_realize_queue) |
696 | LIST_REMOVE(cgroup_realize_queue, u->manager->cgroup_realize_queue, u); | |
87f0e418 | 697 | |
09e24654 LP |
698 | if (u->in_cgroup_empty_queue) |
699 | LIST_REMOVE(cgroup_empty_queue, u->manager->cgroup_empty_queue, u); | |
700 | ||
1bdf2790 ZJS |
701 | if (u->in_cleanup_queue) |
702 | LIST_REMOVE(cleanup_queue, u->manager->cleanup_queue, u); | |
adefcf28 | 703 | |
19496554 MS |
704 | if (u->in_target_deps_queue) |
705 | LIST_REMOVE(target_deps_queue, u->manager->target_deps_queue, u); | |
706 | ||
a3c1168a LP |
707 | if (u->in_stop_when_unneeded_queue) |
708 | LIST_REMOVE(stop_when_unneeded_queue, u->manager->stop_when_unneeded_queue, u); | |
709 | ||
6a48d82f DM |
710 | safe_close(u->ip_accounting_ingress_map_fd); |
711 | safe_close(u->ip_accounting_egress_map_fd); | |
72673e86 | 712 | |
6a48d82f DM |
713 | safe_close(u->ipv4_allow_map_fd); |
714 | safe_close(u->ipv6_allow_map_fd); | |
715 | safe_close(u->ipv4_deny_map_fd); | |
716 | safe_close(u->ipv6_deny_map_fd); | |
d3070fbd | 717 | |
6a48d82f | 718 | bpf_program_unref(u->ip_bpf_ingress); |
aa2b6f1d | 719 | bpf_program_unref(u->ip_bpf_ingress_installed); |
6a48d82f | 720 | bpf_program_unref(u->ip_bpf_egress); |
aa2b6f1d | 721 | bpf_program_unref(u->ip_bpf_egress_installed); |
00d9ef85 | 722 | |
fab34748 KL |
723 | set_free(u->ip_bpf_custom_ingress); |
724 | set_free(u->ip_bpf_custom_egress); | |
725 | set_free(u->ip_bpf_custom_ingress_installed); | |
726 | set_free(u->ip_bpf_custom_egress_installed); | |
727 | ||
084c7007 RG |
728 | bpf_program_unref(u->bpf_device_control_installed); |
729 | ||
a946fa9b ZJS |
730 | condition_free_list(u->conditions); |
731 | condition_free_list(u->asserts); | |
f755e3b7 | 732 | |
ac155bb8 | 733 | free(u->description); |
49dbfa7b | 734 | strv_free(u->documentation); |
ac155bb8 | 735 | free(u->fragment_path); |
1b64d026 | 736 | free(u->source_path); |
ae7a7182 | 737 | strv_free(u->dropin_paths); |
ac155bb8 | 738 | free(u->instance); |
87f0e418 | 739 | |
f189ab18 | 740 | free(u->job_timeout_reboot_arg); |
6bf0f408 LP |
741 | free(u->reboot_arg); |
742 | ||
4562c355 ZJS |
743 | set_free_free(u->aliases); |
744 | free(u->id); | |
745 | ||
87f0e418 LP |
746 | free(u); |
747 | } | |
748 | ||
d9e45bc3 MS |
749 | FreezerState unit_freezer_state(Unit *u) { |
750 | assert(u); | |
751 | ||
752 | return u->freezer_state; | |
753 | } | |
754 | ||
755 | int unit_freezer_state_kernel(Unit *u, FreezerState *ret) { | |
756 | char *values[1] = {}; | |
757 | int r; | |
758 | ||
759 | assert(u); | |
760 | ||
761 | r = cg_get_keyed_attribute(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, "cgroup.events", | |
762 | STRV_MAKE("frozen"), values); | |
763 | if (r < 0) | |
764 | return r; | |
765 | ||
766 | r = _FREEZER_STATE_INVALID; | |
767 | ||
768 | if (values[0]) { | |
769 | if (streq(values[0], "0")) | |
770 | r = FREEZER_RUNNING; | |
771 | else if (streq(values[0], "1")) | |
772 | r = FREEZER_FROZEN; | |
773 | } | |
774 | ||
775 | free(values[0]); | |
776 | *ret = r; | |
777 | ||
778 | return 0; | |
779 | } | |
780 | ||
87f0e418 LP |
781 | UnitActiveState unit_active_state(Unit *u) { |
782 | assert(u); | |
783 | ||
ac155bb8 | 784 | if (u->load_state == UNIT_MERGED) |
6124958c LP |
785 | return unit_active_state(unit_follow_merge(u)); |
786 | ||
787 | /* After a reload it might happen that a unit is not correctly | |
788 | * loaded but still has a process around. That's why we won't | |
fdf20a31 | 789 | * shortcut failed loading to UNIT_INACTIVE_FAILED. */ |
87f0e418 LP |
790 | |
791 | return UNIT_VTABLE(u)->active_state(u); | |
792 | } | |
793 | ||
10a94420 LP |
794 | const char* unit_sub_state_to_string(Unit *u) { |
795 | assert(u); | |
796 | ||
797 | return UNIT_VTABLE(u)->sub_state_to_string(u); | |
798 | } | |
799 | ||
eef85c4a | 800 | static int hashmap_complete_move(Hashmap **s, Hashmap **other) { |
23a177ef LP |
801 | assert(s); |
802 | assert(other); | |
87f0e418 | 803 | |
23a177ef | 804 | if (!*other) |
7c0b05e5 | 805 | return 0; |
87f0e418 | 806 | |
eef85c4a LP |
807 | if (*s) |
808 | return hashmap_move(*s, *other); | |
ae2a15bc LP |
809 | else |
810 | *s = TAKE_PTR(*other); | |
7c0b05e5 MS |
811 | |
812 | return 0; | |
23a177ef | 813 | } |
87f0e418 | 814 | |
7c0b05e5 | 815 | static int merge_names(Unit *u, Unit *other) { |
4562c355 | 816 | char *name; |
7c0b05e5 | 817 | int r; |
87f0e418 | 818 | |
23a177ef LP |
819 | assert(u); |
820 | assert(other); | |
821 | ||
4562c355 | 822 | r = unit_add_alias(u, other->id); |
7c0b05e5 MS |
823 | if (r < 0) |
824 | return r; | |
23a177ef | 825 | |
4562c355 ZJS |
826 | r = set_move(u->aliases, other->aliases); |
827 | if (r < 0) { | |
828 | set_remove(u->aliases, other->id); | |
829 | return r; | |
830 | } | |
23a177ef | 831 | |
4562c355 ZJS |
832 | TAKE_PTR(other->id); |
833 | other->aliases = set_free_free(other->aliases); | |
23a177ef | 834 | |
90e74a66 | 835 | SET_FOREACH(name, u->aliases) |
4562c355 | 836 | assert_se(hashmap_replace(u->manager->units, name, u) == 0); |
7c0b05e5 MS |
837 | |
838 | return 0; | |
87f0e418 LP |
839 | } |
840 | ||
09a65f92 MS |
841 | static int reserve_dependencies(Unit *u, Unit *other, UnitDependency d) { |
842 | unsigned n_reserve; | |
843 | ||
844 | assert(u); | |
845 | assert(other); | |
846 | assert(d < _UNIT_DEPENDENCY_MAX); | |
847 | ||
848 | /* | |
849 | * If u does not have this dependency set allocated, there is no need | |
f131770b | 850 | * to reserve anything. In that case other's set will be transferred |
09a65f92 MS |
851 | * as a whole to u by complete_move(). |
852 | */ | |
853 | if (!u->dependencies[d]) | |
854 | return 0; | |
855 | ||
856 | /* merge_dependencies() will skip a u-on-u dependency */ | |
eef85c4a | 857 | n_reserve = hashmap_size(other->dependencies[d]) - !!hashmap_get(other->dependencies[d], u); |
09a65f92 | 858 | |
eef85c4a | 859 | return hashmap_reserve(u->dependencies[d], n_reserve); |
09a65f92 MS |
860 | } |
861 | ||
d1fab3fe | 862 | static void merge_dependencies(Unit *u, Unit *other, const char *other_id, UnitDependency d) { |
23a177ef | 863 | Unit *back; |
eef85c4a | 864 | void *v; |
87f0e418 | 865 | int r; |
23a177ef | 866 | |
eef85c4a LP |
867 | /* Merges all dependencies of type 'd' of the unit 'other' into the deps of the unit 'u' */ |
868 | ||
23a177ef LP |
869 | assert(u); |
870 | assert(other); | |
871 | assert(d < _UNIT_DEPENDENCY_MAX); | |
872 | ||
5238e957 | 873 | /* Fix backwards pointers. Let's iterate through all dependent units of the other unit. */ |
90e74a66 | 874 | HASHMAP_FOREACH_KEY(v, back, other->dependencies[d]) |
eef85c4a | 875 | |
f6173cb9 ZJS |
876 | /* Let's now iterate through the dependencies of that dependencies of the other units, |
877 | * looking for pointers back, and let's fix them up, to instead point to 'u'. */ | |
878 | for (UnitDependency k = 0; k < _UNIT_DEPENDENCY_MAX; k++) | |
e66047ff | 879 | if (back == u) { |
eef85c4a LP |
880 | /* Do not add dependencies between u and itself. */ |
881 | if (hashmap_remove(back->dependencies[k], other)) | |
f2341e0a | 882 | maybe_warn_about_dependency(u, other_id, k); |
e66047ff | 883 | } else { |
f6173cb9 | 884 | UnitDependencyInfo di_u, di_other; |
eef85c4a LP |
885 | |
886 | /* Let's drop this dependency between "back" and "other", and let's create it between | |
887 | * "back" and "u" instead. Let's merge the bit masks of the dependency we are moving, | |
888 | * and any such dependency which might already exist */ | |
889 | ||
890 | di_other.data = hashmap_get(back->dependencies[k], other); | |
891 | if (!di_other.data) | |
892 | continue; /* dependency isn't set, let's try the next one */ | |
893 | ||
894 | di_u.data = hashmap_get(back->dependencies[k], u); | |
895 | ||
f6173cb9 | 896 | UnitDependencyInfo di_merged = { |
eef85c4a LP |
897 | .origin_mask = di_u.origin_mask | di_other.origin_mask, |
898 | .destination_mask = di_u.destination_mask | di_other.destination_mask, | |
899 | }; | |
900 | ||
901 | r = hashmap_remove_and_replace(back->dependencies[k], other, u, di_merged.data); | |
902 | if (r < 0) | |
903 | log_warning_errno(r, "Failed to remove/replace: back=%s other=%s u=%s: %m", back->id, other_id, u->id); | |
904 | assert(r >= 0); | |
905 | ||
906 | /* assert_se(hashmap_remove_and_replace(back->dependencies[k], other, u, di_merged.data) >= 0); */ | |
e66047ff | 907 | } |
23a177ef | 908 | |
e66047ff | 909 | /* Also do not move dependencies on u to itself */ |
eef85c4a | 910 | back = hashmap_remove(other->dependencies[d], u); |
d1fab3fe | 911 | if (back) |
f2341e0a | 912 | maybe_warn_about_dependency(u, other_id, d); |
e66047ff | 913 | |
7c0b05e5 | 914 | /* The move cannot fail. The caller must have performed a reservation. */ |
eef85c4a | 915 | assert_se(hashmap_complete_move(&u->dependencies[d], &other->dependencies[d]) == 0); |
23a177ef | 916 | |
eef85c4a | 917 | other->dependencies[d] = hashmap_free(other->dependencies[d]); |
23a177ef LP |
918 | } |
919 | ||
920 | int unit_merge(Unit *u, Unit *other) { | |
d1fab3fe | 921 | const char *other_id = NULL; |
09a65f92 | 922 | int r; |
87f0e418 LP |
923 | |
924 | assert(u); | |
925 | assert(other); | |
ac155bb8 MS |
926 | assert(u->manager == other->manager); |
927 | assert(u->type != _UNIT_TYPE_INVALID); | |
87f0e418 | 928 | |
cc916967 LP |
929 | other = unit_follow_merge(other); |
930 | ||
23a177ef LP |
931 | if (other == u) |
932 | return 0; | |
933 | ||
ac155bb8 | 934 | if (u->type != other->type) |
9e2f7c11 LP |
935 | return -EINVAL; |
936 | ||
8a993b61 | 937 | if (!unit_type_may_alias(u->type)) /* Merging only applies to unit names that support aliases */ |
934e749e LP |
938 | return -EEXIST; |
939 | ||
ec2ce0c5 | 940 | if (!IN_SET(other->load_state, UNIT_STUB, UNIT_NOT_FOUND)) |
23a177ef | 941 | return -EEXIST; |
87f0e418 | 942 | |
d383acad ZJS |
943 | if (!streq_ptr(u->instance, other->instance)) |
944 | return -EINVAL; | |
945 | ||
ac155bb8 | 946 | if (other->job) |
819e213f LP |
947 | return -EEXIST; |
948 | ||
e0209d83 MS |
949 | if (other->nop_job) |
950 | return -EEXIST; | |
951 | ||
fdf20a31 | 952 | if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) |
819e213f LP |
953 | return -EEXIST; |
954 | ||
d1fab3fe ZJS |
955 | if (other->id) |
956 | other_id = strdupa(other->id); | |
957 | ||
09a65f92 | 958 | /* Make reservations to ensure merge_dependencies() won't fail */ |
f6173cb9 | 959 | for (UnitDependency d = 0; d < _UNIT_DEPENDENCY_MAX; d++) { |
09a65f92 MS |
960 | r = reserve_dependencies(u, other, d); |
961 | /* | |
962 | * We don't rollback reservations if we fail. We don't have | |
963 | * a way to undo reservations. A reservation is not a leak. | |
964 | */ | |
965 | if (r < 0) | |
966 | return r; | |
967 | } | |
968 | ||
87f0e418 | 969 | /* Merge names */ |
7c0b05e5 MS |
970 | r = merge_names(u, other); |
971 | if (r < 0) | |
972 | return r; | |
87f0e418 | 973 | |
57020a3a | 974 | /* Redirect all references */ |
7f7d01ed ZJS |
975 | while (other->refs_by_target) |
976 | unit_ref_set(other->refs_by_target, other->refs_by_target->source, u); | |
57020a3a | 977 | |
87f0e418 | 978 | /* Merge dependencies */ |
f6173cb9 | 979 | for (UnitDependency d = 0; d < _UNIT_DEPENDENCY_MAX; d++) |
d1fab3fe | 980 | merge_dependencies(u, other, other_id, d); |
87f0e418 | 981 | |
ac155bb8 MS |
982 | other->load_state = UNIT_MERGED; |
983 | other->merged_into = u; | |
23a177ef | 984 | |
3616a49c LP |
985 | /* If there is still some data attached to the other node, we |
986 | * don't need it anymore, and can free it. */ | |
ac155bb8 | 987 | if (other->load_state != UNIT_STUB) |
3616a49c LP |
988 | if (UNIT_VTABLE(other)->done) |
989 | UNIT_VTABLE(other)->done(other); | |
990 | ||
991 | unit_add_to_dbus_queue(u); | |
23a177ef LP |
992 | unit_add_to_cleanup_queue(other); |
993 | ||
994 | return 0; | |
995 | } | |
996 | ||
997 | int unit_merge_by_name(Unit *u, const char *name) { | |
934e749e | 998 | _cleanup_free_ char *s = NULL; |
23a177ef | 999 | Unit *other; |
9e2f7c11 | 1000 | int r; |
23a177ef | 1001 | |
e8630e69 ZJS |
1002 | /* Either add name to u, or if a unit with name already exists, merge it with u. |
1003 | * If name is a template, do the same for name@instance, where instance is u's instance. */ | |
1004 | ||
23a177ef LP |
1005 | assert(u); |
1006 | assert(name); | |
1007 | ||
7410616c | 1008 | if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
ac155bb8 | 1009 | if (!u->instance) |
9e2f7c11 LP |
1010 | return -EINVAL; |
1011 | ||
7410616c LP |
1012 | r = unit_name_replace_instance(name, u->instance, &s); |
1013 | if (r < 0) | |
1014 | return r; | |
9e2f7c11 LP |
1015 | |
1016 | name = s; | |
1017 | } | |
1018 | ||
c2756a68 | 1019 | other = manager_get_unit(u->manager, name); |
7410616c LP |
1020 | if (other) |
1021 | return unit_merge(u, other); | |
23a177ef | 1022 | |
7410616c | 1023 | return unit_add_name(u, name); |
23a177ef LP |
1024 | } |
1025 | ||
1026 | Unit* unit_follow_merge(Unit *u) { | |
1027 | assert(u); | |
1028 | ||
ac155bb8 MS |
1029 | while (u->load_state == UNIT_MERGED) |
1030 | assert_se(u = u->merged_into); | |
23a177ef LP |
1031 | |
1032 | return u; | |
1033 | } | |
1034 | ||
1035 | int unit_add_exec_dependencies(Unit *u, ExecContext *c) { | |
1036 | int r; | |
1037 | ||
1038 | assert(u); | |
1039 | assert(c); | |
1040 | ||
e1e74614 | 1041 | if (c->working_directory && !c->working_directory_missing_ok) { |
eef85c4a | 1042 | r = unit_require_mounts_for(u, c->working_directory, UNIT_DEPENDENCY_FILE); |
36be24c8 ZJS |
1043 | if (r < 0) |
1044 | return r; | |
1045 | } | |
1046 | ||
1047 | if (c->root_directory) { | |
eef85c4a | 1048 | r = unit_require_mounts_for(u, c->root_directory, UNIT_DEPENDENCY_FILE); |
36be24c8 ZJS |
1049 | if (r < 0) |
1050 | return r; | |
1051 | } | |
1052 | ||
915e6d16 | 1053 | if (c->root_image) { |
eef85c4a | 1054 | r = unit_require_mounts_for(u, c->root_image, UNIT_DEPENDENCY_FILE); |
915e6d16 LP |
1055 | if (r < 0) |
1056 | return r; | |
1057 | } | |
1058 | ||
12375b95 | 1059 | for (ExecDirectoryType dt = 0; dt < _EXEC_DIRECTORY_TYPE_MAX; dt++) { |
ada5e276 YW |
1060 | if (!u->manager->prefix[dt]) |
1061 | continue; | |
1062 | ||
12375b95 | 1063 | char **dp; |
ada5e276 YW |
1064 | STRV_FOREACH(dp, c->directories[dt].paths) { |
1065 | _cleanup_free_ char *p; | |
1066 | ||
657ee2d8 | 1067 | p = path_join(u->manager->prefix[dt], *dp); |
ada5e276 YW |
1068 | if (!p) |
1069 | return -ENOMEM; | |
1070 | ||
eef85c4a | 1071 | r = unit_require_mounts_for(u, p, UNIT_DEPENDENCY_FILE); |
ada5e276 YW |
1072 | if (r < 0) |
1073 | return r; | |
1074 | } | |
1075 | } | |
1076 | ||
463d0d15 | 1077 | if (!MANAGER_IS_SYSTEM(u->manager)) |
b46a529c LP |
1078 | return 0; |
1079 | ||
f3b7a79b LP |
1080 | /* For the following three directory types we need write access, and /var/ is possibly on the root |
1081 | * fs. Hence order after systemd-remount-fs.service, to ensure things are writable. */ | |
1082 | if (!strv_isempty(c->directories[EXEC_DIRECTORY_STATE].paths) || | |
1083 | !strv_isempty(c->directories[EXEC_DIRECTORY_CACHE].paths) || | |
1084 | !strv_isempty(c->directories[EXEC_DIRECTORY_LOGS].paths)) { | |
1085 | r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_REMOUNT_FS_SERVICE, true, UNIT_DEPENDENCY_FILE); | |
1086 | if (r < 0) | |
1087 | return r; | |
1088 | } | |
1089 | ||
b46a529c | 1090 | if (c->private_tmp) { |
d71f0505 LP |
1091 | const char *p; |
1092 | ||
1093 | FOREACH_STRING(p, "/tmp", "/var/tmp") { | |
eef85c4a | 1094 | r = unit_require_mounts_for(u, p, UNIT_DEPENDENCY_FILE); |
d71f0505 LP |
1095 | if (r < 0) |
1096 | return r; | |
1097 | } | |
b46a529c | 1098 | |
35d8c19a | 1099 | r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_TMPFILES_SETUP_SERVICE, true, UNIT_DEPENDENCY_FILE); |
b46a529c LP |
1100 | if (r < 0) |
1101 | return r; | |
1102 | } | |
1103 | ||
33b58dfb LP |
1104 | if (c->root_image) { |
1105 | /* We need to wait for /dev/loopX to appear when doing RootImage=, hence let's add an | |
1106 | * implicit dependency on udev */ | |
1107 | ||
1108 | r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_UDEVD_SERVICE, true, UNIT_DEPENDENCY_FILE); | |
1109 | if (r < 0) | |
1110 | return r; | |
1111 | } | |
1112 | ||
52c239d7 LB |
1113 | if (!IN_SET(c->std_output, |
1114 | EXEC_OUTPUT_JOURNAL, EXEC_OUTPUT_JOURNAL_AND_CONSOLE, | |
f3dc6af2 | 1115 | EXEC_OUTPUT_KMSG, EXEC_OUTPUT_KMSG_AND_CONSOLE) && |
52c239d7 LB |
1116 | !IN_SET(c->std_error, |
1117 | EXEC_OUTPUT_JOURNAL, EXEC_OUTPUT_JOURNAL_AND_CONSOLE, | |
f3dc6af2 | 1118 | EXEC_OUTPUT_KMSG, EXEC_OUTPUT_KMSG_AND_CONSOLE) && |
91dd5f7c | 1119 | !c->log_namespace) |
23a177ef LP |
1120 | return 0; |
1121 | ||
91dd5f7c LP |
1122 | /* If syslog or kernel logging is requested (or log namespacing is), make sure our own logging daemon |
1123 | * is run first. */ | |
1124 | ||
1125 | if (c->log_namespace) { | |
dc5437c7 | 1126 | _cleanup_free_ char *socket_unit = NULL, *varlink_socket_unit = NULL; |
23a177ef | 1127 | |
91dd5f7c LP |
1128 | r = unit_name_build_from_type("systemd-journald", c->log_namespace, UNIT_SOCKET, &socket_unit); |
1129 | if (r < 0) | |
1130 | return r; | |
1131 | ||
1132 | r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, socket_unit, true, UNIT_DEPENDENCY_FILE); | |
1133 | if (r < 0) | |
1134 | return r; | |
dc5437c7 LP |
1135 | |
1136 | r = unit_name_build_from_type("systemd-journald-varlink", c->log_namespace, UNIT_SOCKET, &varlink_socket_unit); | |
1137 | if (r < 0) | |
1138 | return r; | |
1139 | ||
1140 | r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, varlink_socket_unit, true, UNIT_DEPENDENCY_FILE); | |
1141 | if (r < 0) | |
1142 | return r; | |
91dd5f7c LP |
1143 | } else |
1144 | r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_JOURNALD_SOCKET, true, UNIT_DEPENDENCY_FILE); | |
b46a529c LP |
1145 | if (r < 0) |
1146 | return r; | |
23a177ef | 1147 | |
87f0e418 LP |
1148 | return 0; |
1149 | } | |
1150 | ||
87f0e418 LP |
1151 | const char *unit_description(Unit *u) { |
1152 | assert(u); | |
1153 | ||
ac155bb8 MS |
1154 | if (u->description) |
1155 | return u->description; | |
87f0e418 | 1156 | |
ac155bb8 | 1157 | return strna(u->id); |
87f0e418 LP |
1158 | } |
1159 | ||
2a8f53c6 ZJS |
1160 | const char *unit_status_string(Unit *u) { |
1161 | assert(u); | |
1162 | ||
1163 | if (u->manager->status_unit_format == STATUS_UNIT_FORMAT_NAME && u->id) | |
1164 | return u->id; | |
1165 | ||
1166 | return unit_description(u); | |
1167 | } | |
1168 | ||
eef85c4a LP |
1169 | static void print_unit_dependency_mask(FILE *f, const char *kind, UnitDependencyMask mask, bool *space) { |
1170 | const struct { | |
1171 | UnitDependencyMask mask; | |
1172 | const char *name; | |
1173 | } table[] = { | |
1174 | { UNIT_DEPENDENCY_FILE, "file" }, | |
1175 | { UNIT_DEPENDENCY_IMPLICIT, "implicit" }, | |
1176 | { UNIT_DEPENDENCY_DEFAULT, "default" }, | |
1177 | { UNIT_DEPENDENCY_UDEV, "udev" }, | |
1178 | { UNIT_DEPENDENCY_PATH, "path" }, | |
1179 | { UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT, "mountinfo-implicit" }, | |
1180 | { UNIT_DEPENDENCY_MOUNTINFO_DEFAULT, "mountinfo-default" }, | |
1181 | { UNIT_DEPENDENCY_PROC_SWAP, "proc-swap" }, | |
1182 | }; | |
eef85c4a LP |
1183 | |
1184 | assert(f); | |
1185 | assert(kind); | |
1186 | assert(space); | |
1187 | ||
12375b95 | 1188 | for (size_t i = 0; i < ELEMENTSOF(table); i++) { |
eef85c4a LP |
1189 | |
1190 | if (mask == 0) | |
1191 | break; | |
1192 | ||
d94a24ca | 1193 | if (FLAGS_SET(mask, table[i].mask)) { |
eef85c4a LP |
1194 | if (*space) |
1195 | fputc(' ', f); | |
1196 | else | |
1197 | *space = true; | |
1198 | ||
1199 | fputs(kind, f); | |
1200 | fputs("-", f); | |
1201 | fputs(table[i].name, f); | |
1202 | ||
1203 | mask &= ~table[i].mask; | |
1204 | } | |
1205 | } | |
1206 | ||
1207 | assert(mask == 0); | |
1208 | } | |
1209 | ||
87f0e418 | 1210 | void unit_dump(Unit *u, FILE *f, const char *prefix) { |
49dbfa7b | 1211 | char *t, **j; |
47be870b | 1212 | const char *prefix2; |
8d5e5931 | 1213 | char timestamp[5][FORMAT_TIMESTAMP_MAX], timespan[FORMAT_TIMESPAN_MAX]; |
a7f241db | 1214 | Unit *following; |
eeaedb7c | 1215 | _cleanup_set_free_ Set *following_set = NULL; |
02638280 LP |
1216 | CGroupMask m; |
1217 | int r; | |
87f0e418 LP |
1218 | |
1219 | assert(u); | |
ac155bb8 | 1220 | assert(u->type >= 0); |
87f0e418 | 1221 | |
4c940960 | 1222 | prefix = strempty(prefix); |
63c372cb | 1223 | prefix2 = strjoina(prefix, "\t"); |
87f0e418 LP |
1224 | |
1225 | fprintf(f, | |
0121d1f2 ZJS |
1226 | "%s-> Unit %s:\n", |
1227 | prefix, u->id); | |
1228 | ||
90e74a66 | 1229 | SET_FOREACH(t, u->aliases) |
4562c355 | 1230 | fprintf(f, "%s\tAlias: %s\n", prefix, t); |
0121d1f2 ZJS |
1231 | |
1232 | fprintf(f, | |
87f0e418 | 1233 | "%s\tDescription: %s\n" |
9e2f7c11 | 1234 | "%s\tInstance: %s\n" |
87f0e418 | 1235 | "%s\tUnit Load State: %s\n" |
2fad8195 | 1236 | "%s\tUnit Active State: %s\n" |
b895d155 | 1237 | "%s\tState Change Timestamp: %s\n" |
173e3821 | 1238 | "%s\tInactive Exit Timestamp: %s\n" |
2fad8195 | 1239 | "%s\tActive Enter Timestamp: %s\n" |
701cc384 | 1240 | "%s\tActive Exit Timestamp: %s\n" |
173e3821 | 1241 | "%s\tInactive Enter Timestamp: %s\n" |
f2f725e5 | 1242 | "%s\tMay GC: %s\n" |
9444b1f2 | 1243 | "%s\tNeed Daemon Reload: %s\n" |
c2756a68 | 1244 | "%s\tTransient: %s\n" |
f5869324 | 1245 | "%s\tPerpetual: %s\n" |
5afe510c | 1246 | "%s\tGarbage Collection Mode: %s\n" |
4ad49000 LP |
1247 | "%s\tSlice: %s\n" |
1248 | "%s\tCGroup: %s\n" | |
aae7e17f | 1249 | "%s\tCGroup realized: %s\n", |
87f0e418 | 1250 | prefix, unit_description(u), |
ac155bb8 MS |
1251 | prefix, strna(u->instance), |
1252 | prefix, unit_load_state_to_string(u->load_state), | |
2fad8195 | 1253 | prefix, unit_active_state_to_string(unit_active_state(u)), |
8d5e5931 ZJS |
1254 | prefix, strna(format_timestamp(timestamp[0], sizeof(timestamp[0]), u->state_change_timestamp.realtime)), |
1255 | prefix, strna(format_timestamp(timestamp[1], sizeof(timestamp[1]), u->inactive_exit_timestamp.realtime)), | |
1256 | prefix, strna(format_timestamp(timestamp[2], sizeof(timestamp[2]), u->active_enter_timestamp.realtime)), | |
1257 | prefix, strna(format_timestamp(timestamp[3], sizeof(timestamp[3]), u->active_exit_timestamp.realtime)), | |
1258 | prefix, strna(format_timestamp(timestamp[4], sizeof(timestamp[4]), u->inactive_enter_timestamp.realtime)), | |
f2f725e5 | 1259 | prefix, yes_no(unit_may_gc(u)), |
9444b1f2 | 1260 | prefix, yes_no(unit_need_daemon_reload(u)), |
c2756a68 | 1261 | prefix, yes_no(u->transient), |
f5869324 | 1262 | prefix, yes_no(u->perpetual), |
5afe510c | 1263 | prefix, collect_mode_to_string(u->collect_mode), |
4ad49000 LP |
1264 | prefix, strna(unit_slice_name(u)), |
1265 | prefix, strna(u->cgroup_path), | |
aae7e17f FB |
1266 | prefix, yes_no(u->cgroup_realized)); |
1267 | ||
1268 | if (u->cgroup_realized_mask != 0) { | |
1269 | _cleanup_free_ char *s = NULL; | |
1270 | (void) cg_mask_to_string(u->cgroup_realized_mask, &s); | |
02638280 LP |
1271 | fprintf(f, "%s\tCGroup realized mask: %s\n", prefix, strnull(s)); |
1272 | } | |
0adf88b6 | 1273 | |
02638280 LP |
1274 | if (u->cgroup_enabled_mask != 0) { |
1275 | _cleanup_free_ char *s = NULL; | |
1276 | (void) cg_mask_to_string(u->cgroup_enabled_mask, &s); | |
1277 | fprintf(f, "%s\tCGroup enabled mask: %s\n", prefix, strnull(s)); | |
1278 | } | |
0adf88b6 | 1279 | |
02638280 LP |
1280 | m = unit_get_own_mask(u); |
1281 | if (m != 0) { | |
1282 | _cleanup_free_ char *s = NULL; | |
1283 | (void) cg_mask_to_string(m, &s); | |
1284 | fprintf(f, "%s\tCGroup own mask: %s\n", prefix, strnull(s)); | |
aae7e17f | 1285 | } |
0adf88b6 | 1286 | |
02638280 LP |
1287 | m = unit_get_members_mask(u); |
1288 | if (m != 0) { | |
aae7e17f | 1289 | _cleanup_free_ char *s = NULL; |
02638280 | 1290 | (void) cg_mask_to_string(m, &s); |
aae7e17f FB |
1291 | fprintf(f, "%s\tCGroup members mask: %s\n", prefix, strnull(s)); |
1292 | } | |
0301abf4 | 1293 | |
0adf88b6 LP |
1294 | m = unit_get_delegate_mask(u); |
1295 | if (m != 0) { | |
1296 | _cleanup_free_ char *s = NULL; | |
1297 | (void) cg_mask_to_string(m, &s); | |
1298 | fprintf(f, "%s\tCGroup delegate mask: %s\n", prefix, strnull(s)); | |
1299 | } | |
1300 | ||
4b58153d LP |
1301 | if (!sd_id128_is_null(u->invocation_id)) |
1302 | fprintf(f, "%s\tInvocation ID: " SD_ID128_FORMAT_STR "\n", | |
1303 | prefix, SD_ID128_FORMAT_VAL(u->invocation_id)); | |
1304 | ||
49dbfa7b LP |
1305 | STRV_FOREACH(j, u->documentation) |
1306 | fprintf(f, "%s\tDocumentation: %s\n", prefix, *j); | |
1307 | ||
eeaedb7c LP |
1308 | following = unit_following(u); |
1309 | if (following) | |
ac155bb8 | 1310 | fprintf(f, "%s\tFollowing: %s\n", prefix, following->id); |
8fe914ec | 1311 | |
eeaedb7c LP |
1312 | r = unit_following_set(u, &following_set); |
1313 | if (r >= 0) { | |
1314 | Unit *other; | |
1315 | ||
90e74a66 | 1316 | SET_FOREACH(other, following_set) |
eeaedb7c LP |
1317 | fprintf(f, "%s\tFollowing Set Member: %s\n", prefix, other->id); |
1318 | } | |
1319 | ||
ac155bb8 MS |
1320 | if (u->fragment_path) |
1321 | fprintf(f, "%s\tFragment Path: %s\n", prefix, u->fragment_path); | |
23a177ef | 1322 | |
1b64d026 LP |
1323 | if (u->source_path) |
1324 | fprintf(f, "%s\tSource Path: %s\n", prefix, u->source_path); | |
1325 | ||
ae7a7182 | 1326 | STRV_FOREACH(j, u->dropin_paths) |
2875e22b | 1327 | fprintf(f, "%s\tDropIn Path: %s\n", prefix, *j); |
ae7a7182 | 1328 | |
e7dfbb4e LP |
1329 | if (u->failure_action != EMERGENCY_ACTION_NONE) |
1330 | fprintf(f, "%s\tFailure Action: %s\n", prefix, emergency_action_to_string(u->failure_action)); | |
7af67e9a LP |
1331 | if (u->failure_action_exit_status >= 0) |
1332 | fprintf(f, "%s\tFailure Action Exit Status: %i\n", prefix, u->failure_action_exit_status); | |
e7dfbb4e LP |
1333 | if (u->success_action != EMERGENCY_ACTION_NONE) |
1334 | fprintf(f, "%s\tSuccess Action: %s\n", prefix, emergency_action_to_string(u->success_action)); | |
7af67e9a LP |
1335 | if (u->success_action_exit_status >= 0) |
1336 | fprintf(f, "%s\tSuccess Action Exit Status: %i\n", prefix, u->success_action_exit_status); | |
e7dfbb4e | 1337 | |
36c16a7c | 1338 | if (u->job_timeout != USEC_INFINITY) |
2fa4092c | 1339 | fprintf(f, "%s\tJob Timeout: %s\n", prefix, format_timespan(timespan, sizeof(timespan), u->job_timeout, 0)); |
faf919f1 | 1340 | |
87a47f99 LN |
1341 | if (u->job_timeout_action != EMERGENCY_ACTION_NONE) |
1342 | fprintf(f, "%s\tJob Timeout Action: %s\n", prefix, emergency_action_to_string(u->job_timeout_action)); | |
f189ab18 LP |
1343 | |
1344 | if (u->job_timeout_reboot_arg) | |
1345 | fprintf(f, "%s\tJob Timeout Reboot Argument: %s\n", prefix, u->job_timeout_reboot_arg); | |
1346 | ||
59fccdc5 LP |
1347 | condition_dump_list(u->conditions, f, prefix, condition_type_to_string); |
1348 | condition_dump_list(u->asserts, f, prefix, assert_type_to_string); | |
52661efd | 1349 | |
ac155bb8 | 1350 | if (dual_timestamp_is_set(&u->condition_timestamp)) |
2791a8f8 LP |
1351 | fprintf(f, |
1352 | "%s\tCondition Timestamp: %s\n" | |
1353 | "%s\tCondition Result: %s\n", | |
8d5e5931 | 1354 | prefix, strna(format_timestamp(timestamp[0], sizeof(timestamp[0]), u->condition_timestamp.realtime)), |
ac155bb8 | 1355 | prefix, yes_no(u->condition_result)); |
2791a8f8 | 1356 | |
59fccdc5 LP |
1357 | if (dual_timestamp_is_set(&u->assert_timestamp)) |
1358 | fprintf(f, | |
1359 | "%s\tAssert Timestamp: %s\n" | |
1360 | "%s\tAssert Result: %s\n", | |
8d5e5931 | 1361 | prefix, strna(format_timestamp(timestamp[0], sizeof(timestamp[0]), u->assert_timestamp.realtime)), |
59fccdc5 LP |
1362 | prefix, yes_no(u->assert_result)); |
1363 | ||
f6173cb9 | 1364 | for (UnitDependency d = 0; d < _UNIT_DEPENDENCY_MAX; d++) { |
eef85c4a | 1365 | UnitDependencyInfo di; |
87f0e418 LP |
1366 | Unit *other; |
1367 | ||
90e74a66 | 1368 | HASHMAP_FOREACH_KEY(di.data, other, u->dependencies[d]) { |
eef85c4a LP |
1369 | bool space = false; |
1370 | ||
1371 | fprintf(f, "%s\t%s: %s (", prefix, unit_dependency_to_string(d), other->id); | |
1372 | ||
1373 | print_unit_dependency_mask(f, "origin", di.origin_mask, &space); | |
1374 | print_unit_dependency_mask(f, "destination", di.destination_mask, &space); | |
1375 | ||
1376 | fputs(")\n", f); | |
1377 | } | |
87f0e418 LP |
1378 | } |
1379 | ||
eef85c4a LP |
1380 | if (!hashmap_isempty(u->requires_mounts_for)) { |
1381 | UnitDependencyInfo di; | |
1382 | const char *path; | |
1383 | ||
90e74a66 | 1384 | HASHMAP_FOREACH_KEY(di.data, path, u->requires_mounts_for) { |
eef85c4a | 1385 | bool space = false; |
7c8fa05c | 1386 | |
eef85c4a | 1387 | fprintf(f, "%s\tRequiresMountsFor: %s (", prefix, path); |
7c8fa05c | 1388 | |
eef85c4a LP |
1389 | print_unit_dependency_mask(f, "origin", di.origin_mask, &space); |
1390 | print_unit_dependency_mask(f, "destination", di.destination_mask, &space); | |
1391 | ||
1392 | fputs(")\n", f); | |
1393 | } | |
7c8fa05c LP |
1394 | } |
1395 | ||
ac155bb8 | 1396 | if (u->load_state == UNIT_LOADED) { |
ab1f0633 | 1397 | |
b0650475 | 1398 | fprintf(f, |
a40eb732 | 1399 | "%s\tStopWhenUnneeded: %s\n" |
b5e9dba8 LP |
1400 | "%s\tRefuseManualStart: %s\n" |
1401 | "%s\tRefuseManualStop: %s\n" | |
222ae6a8 | 1402 | "%s\tDefaultDependencies: %s\n" |
d420282b | 1403 | "%s\tOnFailureJobMode: %s\n" |
36b4a7ba | 1404 | "%s\tIgnoreOnIsolate: %s\n", |
ac155bb8 MS |
1405 | prefix, yes_no(u->stop_when_unneeded), |
1406 | prefix, yes_no(u->refuse_manual_start), | |
1407 | prefix, yes_no(u->refuse_manual_stop), | |
1408 | prefix, yes_no(u->default_dependencies), | |
d420282b | 1409 | prefix, job_mode_to_string(u->on_failure_job_mode), |
36b4a7ba | 1410 | prefix, yes_no(u->ignore_on_isolate)); |
ac155bb8 | 1411 | |
23a177ef LP |
1412 | if (UNIT_VTABLE(u)->dump) |
1413 | UNIT_VTABLE(u)->dump(u, f, prefix2); | |
b0650475 | 1414 | |
ac155bb8 | 1415 | } else if (u->load_state == UNIT_MERGED) |
b0650475 LP |
1416 | fprintf(f, |
1417 | "%s\tMerged into: %s\n", | |
ac155bb8 MS |
1418 | prefix, u->merged_into->id); |
1419 | else if (u->load_state == UNIT_ERROR) | |
4bbccb02 | 1420 | fprintf(f, "%s\tLoad Error Code: %s\n", prefix, strerror_safe(u->load_error)); |
8821a00f | 1421 | |
12375b95 | 1422 | for (const char *n = sd_bus_track_first(u->bus_track); n; n = sd_bus_track_next(u->bus_track)) |
05a98afd | 1423 | fprintf(f, "%s\tBus Ref: %s\n", prefix, n); |
87f0e418 | 1424 | |
ac155bb8 MS |
1425 | if (u->job) |
1426 | job_dump(u->job, f, prefix2); | |
87f0e418 | 1427 | |
e0209d83 MS |
1428 | if (u->nop_job) |
1429 | job_dump(u->nop_job, f, prefix2); | |
87f0e418 LP |
1430 | } |
1431 | ||
1432 | /* Common implementation for multiple backends */ | |
c3620770 | 1433 | int unit_load_fragment_and_dropin(Unit *u, bool fragment_required) { |
23a177ef LP |
1434 | int r; |
1435 | ||
1436 | assert(u); | |
23a177ef | 1437 | |
e48614c4 | 1438 | /* Load a .{service,socket,...} file */ |
4ad49000 LP |
1439 | r = unit_load_fragment(u); |
1440 | if (r < 0) | |
23a177ef LP |
1441 | return r; |
1442 | ||
c3620770 ZJS |
1443 | if (u->load_state == UNIT_STUB) { |
1444 | if (fragment_required) | |
1445 | return -ENOENT; | |
1446 | ||
1447 | u->load_state = UNIT_LOADED; | |
1448 | } | |
23a177ef | 1449 | |
9e4ea9cc ZJS |
1450 | /* Load drop-in directory data. If u is an alias, we might be reloading the |
1451 | * target unit needlessly. But we cannot be sure which drops-ins have already | |
1452 | * been loaded and which not, at least without doing complicated book-keeping, | |
1453 | * so let's always reread all drop-ins. */ | |
c9e06956 ZJS |
1454 | r = unit_load_dropin(unit_follow_merge(u)); |
1455 | if (r < 0) | |
1456 | return r; | |
1457 | ||
1458 | if (u->source_path) { | |
1459 | struct stat st; | |
1460 | ||
1461 | if (stat(u->source_path, &st) >= 0) | |
1462 | u->source_mtime = timespec_load(&st.st_mtim); | |
1463 | else | |
1464 | u->source_mtime = 0; | |
1465 | } | |
1466 | ||
1467 | return 0; | |
23a177ef LP |
1468 | } |
1469 | ||
19496554 MS |
1470 | void unit_add_to_target_deps_queue(Unit *u) { |
1471 | Manager *m = u->manager; | |
1472 | ||
1473 | assert(u); | |
1474 | ||
1475 | if (u->in_target_deps_queue) | |
1476 | return; | |
1477 | ||
1478 | LIST_PREPEND(target_deps_queue, m->target_deps_queue, u); | |
1479 | u->in_target_deps_queue = true; | |
1480 | } | |
1481 | ||
bba34eed | 1482 | int unit_add_default_target_dependency(Unit *u, Unit *target) { |
98bc2000 LP |
1483 | assert(u); |
1484 | assert(target); | |
1485 | ||
ac155bb8 | 1486 | if (target->type != UNIT_TARGET) |
98bc2000 LP |
1487 | return 0; |
1488 | ||
35b8ca3a | 1489 | /* Only add the dependency if both units are loaded, so that |
bba34eed | 1490 | * that loop check below is reliable */ |
ac155bb8 MS |
1491 | if (u->load_state != UNIT_LOADED || |
1492 | target->load_state != UNIT_LOADED) | |
bba34eed LP |
1493 | return 0; |
1494 | ||
21256a2b LP |
1495 | /* If either side wants no automatic dependencies, then let's |
1496 | * skip this */ | |
ac155bb8 MS |
1497 | if (!u->default_dependencies || |
1498 | !target->default_dependencies) | |
21256a2b LP |
1499 | return 0; |
1500 | ||
98bc2000 | 1501 | /* Don't create loops */ |
eef85c4a | 1502 | if (hashmap_get(target->dependencies[UNIT_BEFORE], u)) |
98bc2000 LP |
1503 | return 0; |
1504 | ||
eef85c4a | 1505 | return unit_add_dependency(target, UNIT_AFTER, u, true, UNIT_DEPENDENCY_DEFAULT); |
98bc2000 LP |
1506 | } |
1507 | ||
e954c9cf LP |
1508 | static int unit_add_slice_dependencies(Unit *u) { |
1509 | assert(u); | |
b81884e7 | 1510 | |
35b7ff80 | 1511 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) |
e954c9cf LP |
1512 | return 0; |
1513 | ||
eef85c4a LP |
1514 | /* Slice units are implicitly ordered against their parent slices (as this relationship is encoded in the |
1515 | name), while all other units are ordered based on configuration (as in their case Slice= configures the | |
1516 | relationship). */ | |
f6173cb9 | 1517 | UnitDependencyMask mask = u->type == UNIT_SLICE ? UNIT_DEPENDENCY_IMPLICIT : UNIT_DEPENDENCY_FILE; |
eef85c4a | 1518 | |
e954c9cf | 1519 | if (UNIT_ISSET(u->slice)) |
eef85c4a | 1520 | return unit_add_two_dependencies(u, UNIT_AFTER, UNIT_REQUIRES, UNIT_DEREF(u->slice), true, mask); |
e954c9cf | 1521 | |
8c8da0e0 | 1522 | if (unit_has_name(u, SPECIAL_ROOT_SLICE)) |
d1fab3fe ZJS |
1523 | return 0; |
1524 | ||
5a724170 | 1525 | return unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_ROOT_SLICE, true, mask); |
98bc2000 LP |
1526 | } |
1527 | ||
e954c9cf | 1528 | static int unit_add_mount_dependencies(Unit *u) { |
eef85c4a LP |
1529 | UnitDependencyInfo di; |
1530 | const char *path; | |
9588bc32 LP |
1531 | int r; |
1532 | ||
1533 | assert(u); | |
1534 | ||
90e74a66 | 1535 | HASHMAP_FOREACH_KEY(di.data, path, u->requires_mounts_for) { |
eef85c4a | 1536 | char prefix[strlen(path) + 1]; |
9588bc32 | 1537 | |
eef85c4a | 1538 | PATH_FOREACH_PREFIX_MORE(prefix, path) { |
c7c89abb | 1539 | _cleanup_free_ char *p = NULL; |
9588bc32 LP |
1540 | Unit *m; |
1541 | ||
c7c89abb | 1542 | r = unit_name_from_path(prefix, ".mount", &p); |
9588bc32 LP |
1543 | if (r < 0) |
1544 | return r; | |
c7c89abb FB |
1545 | |
1546 | m = manager_get_unit(u->manager, p); | |
1547 | if (!m) { | |
1548 | /* Make sure to load the mount unit if | |
1549 | * it exists. If so the dependencies | |
1550 | * on this unit will be added later | |
1551 | * during the loading of the mount | |
1552 | * unit. */ | |
1553 | (void) manager_load_unit_prepare(u->manager, p, NULL, NULL, &m); | |
9588bc32 | 1554 | continue; |
c7c89abb | 1555 | } |
9588bc32 LP |
1556 | if (m == u) |
1557 | continue; | |
1558 | ||
1559 | if (m->load_state != UNIT_LOADED) | |
1560 | continue; | |
1561 | ||
eef85c4a | 1562 | r = unit_add_dependency(u, UNIT_AFTER, m, true, di.origin_mask); |
9588bc32 LP |
1563 | if (r < 0) |
1564 | return r; | |
1565 | ||
1566 | if (m->fragment_path) { | |
eef85c4a | 1567 | r = unit_add_dependency(u, UNIT_REQUIRES, m, true, di.origin_mask); |
9588bc32 LP |
1568 | if (r < 0) |
1569 | return r; | |
1570 | } | |
1571 | } | |
1572 | } | |
1573 | ||
1574 | return 0; | |
1575 | } | |
1576 | ||
a2db0225 AZ |
1577 | static int unit_add_oomd_dependencies(Unit *u) { |
1578 | CGroupContext *c; | |
1579 | bool wants_oomd; | |
1580 | int r; | |
1581 | ||
1582 | assert(u); | |
1583 | ||
1584 | if (!u->default_dependencies) | |
1585 | return 0; | |
1586 | ||
1587 | c = unit_get_cgroup_context(u); | |
1588 | if (!c) | |
1589 | return 0; | |
1590 | ||
1591 | wants_oomd = (c->moom_swap == MANAGED_OOM_KILL || c->moom_mem_pressure == MANAGED_OOM_KILL); | |
1592 | if (!wants_oomd) | |
1593 | return 0; | |
1594 | ||
1595 | r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_WANTS, "systemd-oomd.service", true, UNIT_DEPENDENCY_FILE); | |
1596 | if (r < 0) | |
1597 | return r; | |
1598 | ||
1599 | return 0; | |
1600 | } | |
1601 | ||
95ae05c0 WC |
1602 | static int unit_add_startup_units(Unit *u) { |
1603 | CGroupContext *c; | |
95ae05c0 WC |
1604 | |
1605 | c = unit_get_cgroup_context(u); | |
db785129 LP |
1606 | if (!c) |
1607 | return 0; | |
1608 | ||
d53d9474 | 1609 | if (c->startup_cpu_shares == CGROUP_CPU_SHARES_INVALID && |
13c31542 | 1610 | c->startup_io_weight == CGROUP_WEIGHT_INVALID && |
d53d9474 | 1611 | c->startup_blockio_weight == CGROUP_BLKIO_WEIGHT_INVALID) |
db785129 LP |
1612 | return 0; |
1613 | ||
de7fef4b | 1614 | return set_ensure_put(&u->manager->startup_units, NULL, u); |
95ae05c0 WC |
1615 | } |
1616 | ||
87f0e418 LP |
1617 | int unit_load(Unit *u) { |
1618 | int r; | |
1619 | ||
1620 | assert(u); | |
1621 | ||
ac155bb8 | 1622 | if (u->in_load_queue) { |
71fda00f | 1623 | LIST_REMOVE(load_queue, u->manager->load_queue, u); |
ac155bb8 | 1624 | u->in_load_queue = false; |
87f0e418 LP |
1625 | } |
1626 | ||
ac155bb8 | 1627 | if (u->type == _UNIT_TYPE_INVALID) |
e537352b LP |
1628 | return -EINVAL; |
1629 | ||
ac155bb8 | 1630 | if (u->load_state != UNIT_STUB) |
87f0e418 LP |
1631 | return 0; |
1632 | ||
4f4afc88 | 1633 | if (u->transient_file) { |
66fa4bdd LP |
1634 | /* Finalize transient file: if this is a transient unit file, as soon as we reach unit_load() the setup |
1635 | * is complete, hence let's synchronize the unit file we just wrote to disk. */ | |
1636 | ||
4f4afc88 LP |
1637 | r = fflush_and_check(u->transient_file); |
1638 | if (r < 0) | |
1639 | goto fail; | |
1640 | ||
50fb00b7 | 1641 | u->transient_file = safe_fclose(u->transient_file); |
f76707da | 1642 | u->fragment_mtime = now(CLOCK_REALTIME); |
4f4afc88 LP |
1643 | } |
1644 | ||
c3784a7d ZJS |
1645 | r = UNIT_VTABLE(u)->load(u); |
1646 | if (r < 0) | |
23a177ef | 1647 | goto fail; |
c3784a7d ZJS |
1648 | |
1649 | assert(u->load_state != UNIT_STUB); | |
23a177ef | 1650 | |
7c8fa05c | 1651 | if (u->load_state == UNIT_LOADED) { |
19496554 | 1652 | unit_add_to_target_deps_queue(u); |
e954c9cf LP |
1653 | |
1654 | r = unit_add_slice_dependencies(u); | |
1655 | if (r < 0) | |
1656 | goto fail; | |
c2756a68 | 1657 | |
e954c9cf | 1658 | r = unit_add_mount_dependencies(u); |
7c8fa05c | 1659 | if (r < 0) |
c2756a68 | 1660 | goto fail; |
7c8fa05c | 1661 | |
a2db0225 AZ |
1662 | r = unit_add_oomd_dependencies(u); |
1663 | if (r < 0) | |
1664 | goto fail; | |
1665 | ||
95ae05c0 WC |
1666 | r = unit_add_startup_units(u); |
1667 | if (r < 0) | |
1668 | goto fail; | |
1669 | ||
eef85c4a | 1670 | if (u->on_failure_job_mode == JOB_ISOLATE && hashmap_size(u->dependencies[UNIT_ON_FAILURE]) > 1) { |
f2341e0a | 1671 | log_unit_error(u, "More than one OnFailure= dependencies specified but OnFailureJobMode=isolate set. Refusing."); |
6f40aa45 | 1672 | r = -ENOEXEC; |
c2756a68 LP |
1673 | goto fail; |
1674 | } | |
bc432dc7 | 1675 | |
a2df3ea4 MK |
1676 | if (u->job_running_timeout != USEC_INFINITY && u->job_running_timeout > u->job_timeout) |
1677 | log_unit_warning(u, "JobRunningTimeoutSec= is greater than JobTimeoutSec=, it has no effect."); | |
1678 | ||
5af88058 LP |
1679 | /* We finished loading, let's ensure our parents recalculate the members mask */ |
1680 | unit_invalidate_cgroup_members_masks(u); | |
f68319bb LP |
1681 | } |
1682 | ||
ac155bb8 | 1683 | assert((u->load_state != UNIT_MERGED) == !u->merged_into); |
23a177ef LP |
1684 | |
1685 | unit_add_to_dbus_queue(unit_follow_merge(u)); | |
701cc384 | 1686 | unit_add_to_gc_queue(u); |
f561e8c6 | 1687 | (void) manager_varlink_send_managed_oom_update(u); |
87f0e418 | 1688 | |
87f0e418 LP |
1689 | return 0; |
1690 | ||
1691 | fail: | |
81be2388 ZJS |
1692 | /* We convert ENOEXEC errors to the UNIT_BAD_SETTING load state here. Configuration parsing code |
1693 | * should hence return ENOEXEC to ensure units are placed in this state after loading. */ | |
c4555ad8 LP |
1694 | |
1695 | u->load_state = u->load_state == UNIT_STUB ? UNIT_NOT_FOUND : | |
1696 | r == -ENOEXEC ? UNIT_BAD_SETTING : | |
1697 | UNIT_ERROR; | |
ac155bb8 | 1698 | u->load_error = r; |
c4555ad8 | 1699 | |
c149d2b4 ZJS |
1700 | /* Record the timestamp on the cache, so that if the cache gets updated between now and the next time |
1701 | * an attempt is made to load this unit, we know we need to check again. */ | |
7233e91a | 1702 | if (u->load_state == UNIT_NOT_FOUND) |
c2911d48 | 1703 | u->fragment_not_found_timestamp_hash = u->manager->unit_cache_timestamp_hash; |
7233e91a | 1704 | |
c1e1601e | 1705 | unit_add_to_dbus_queue(u); |
9a46fc3b | 1706 | unit_add_to_gc_queue(u); |
23a177ef | 1707 | |
c4555ad8 | 1708 | return log_unit_debug_errno(u, r, "Failed to load configuration: %m"); |
87f0e418 LP |
1709 | } |
1710 | ||
f9f88198 YW |
1711 | _printf_(7, 8) |
1712 | static int log_unit_internal(void *userdata, int level, int error, const char *file, int line, const char *func, const char *format, ...) { | |
1713 | Unit *u = userdata; | |
1714 | va_list ap; | |
1715 | int r; | |
49365733 | 1716 | |
f9f88198 YW |
1717 | va_start(ap, format); |
1718 | if (u) | |
1719 | r = log_object_internalv(level, error, file, line, func, | |
1720 | u->manager->unit_log_field, | |
1721 | u->id, | |
1722 | u->manager->invocation_log_field, | |
1723 | u->invocation_id_string, | |
1724 | format, ap); | |
1725 | else | |
1726 | r = log_internalv(level, error, file, line, func, format, ap); | |
1727 | va_end(ap); | |
49365733 | 1728 | |
f9f88198 | 1729 | return r; |
49365733 LP |
1730 | } |
1731 | ||
97a3f4ee | 1732 | static bool unit_test_condition(Unit *u) { |
a0b191b7 LP |
1733 | _cleanup_strv_free_ char **env = NULL; |
1734 | int r; | |
1735 | ||
90bbc946 LP |
1736 | assert(u); |
1737 | ||
ac155bb8 | 1738 | dual_timestamp_get(&u->condition_timestamp); |
90bbc946 | 1739 | |
a0b191b7 LP |
1740 | r = manager_get_effective_environment(u->manager, &env); |
1741 | if (r < 0) { | |
1742 | log_unit_error_errno(u, r, "Failed to determine effective environment: %m"); | |
1743 | u->condition_result = CONDITION_ERROR; | |
1744 | } else | |
1745 | u->condition_result = condition_test_list( | |
1746 | u->conditions, | |
1747 | env, | |
1748 | condition_type_to_string, | |
1749 | log_unit_internal, | |
1750 | u); | |
e18f8852 | 1751 | |
a0b191b7 | 1752 | unit_add_to_dbus_queue(u); |
ac155bb8 | 1753 | return u->condition_result; |
90bbc946 LP |
1754 | } |
1755 | ||
97a3f4ee | 1756 | static bool unit_test_assert(Unit *u) { |
a0b191b7 LP |
1757 | _cleanup_strv_free_ char **env = NULL; |
1758 | int r; | |
1759 | ||
59fccdc5 LP |
1760 | assert(u); |
1761 | ||
1762 | dual_timestamp_get(&u->assert_timestamp); | |
59fccdc5 | 1763 | |
a0b191b7 LP |
1764 | r = manager_get_effective_environment(u->manager, &env); |
1765 | if (r < 0) { | |
1766 | log_unit_error_errno(u, r, "Failed to determine effective environment: %m"); | |
1767 | u->assert_result = CONDITION_ERROR; | |
1768 | } else | |
1769 | u->assert_result = condition_test_list( | |
1770 | u->asserts, | |
1771 | env, | |
1772 | assert_type_to_string, | |
1773 | log_unit_internal, | |
1774 | u); | |
e18f8852 | 1775 | |
a0b191b7 | 1776 | unit_add_to_dbus_queue(u); |
59fccdc5 LP |
1777 | return u->assert_result; |
1778 | } | |
1779 | ||
5bcf34eb | 1780 | void unit_status_printf(Unit *u, StatusType status_type, const char *status, const char *unit_status_msg_format) { |
5b262f74 LP |
1781 | const char *d; |
1782 | ||
2a8f53c6 | 1783 | d = unit_status_string(u); |
5b262f74 LP |
1784 | if (log_get_show_color()) |
1785 | d = strjoina(ANSI_HIGHLIGHT, d, ANSI_NORMAL); | |
1786 | ||
df446f96 | 1787 | DISABLE_WARNING_FORMAT_NONLITERAL; |
5bcf34eb | 1788 | manager_status_printf(u->manager, status_type, status, unit_status_msg_format, d); |
df446f96 LP |
1789 | REENABLE_WARNING; |
1790 | } | |
1791 | ||
97a3f4ee | 1792 | int unit_test_start_limit(Unit *u) { |
429926e9 TR |
1793 | const char *reason; |
1794 | ||
6bf0f408 LP |
1795 | assert(u); |
1796 | ||
7bf081a1 | 1797 | if (ratelimit_below(&u->start_ratelimit)) { |
6bf0f408 LP |
1798 | u->start_limit_hit = false; |
1799 | return 0; | |
1800 | } | |
1801 | ||
1802 | log_unit_warning(u, "Start request repeated too quickly."); | |
1803 | u->start_limit_hit = true; | |
1804 | ||
429926e9 TR |
1805 | reason = strjoina("unit ", u->id, " failed"); |
1806 | ||
36c4dc08 LP |
1807 | emergency_action(u->manager, u->start_limit_action, |
1808 | EMERGENCY_ACTION_IS_WATCHDOG|EMERGENCY_ACTION_WARN, | |
1809 | u->reboot_arg, -1, reason); | |
1810 | ||
1811 | return -ECANCELED; | |
6bf0f408 LP |
1812 | } |
1813 | ||
c891efaf | 1814 | bool unit_shall_confirm_spawn(Unit *u) { |
631b676b | 1815 | assert(u); |
c891efaf FB |
1816 | |
1817 | if (manager_is_confirm_spawn_disabled(u->manager)) | |
1818 | return false; | |
1819 | ||
1820 | /* For some reasons units remaining in the same process group | |
1821 | * as PID 1 fail to acquire the console even if it's not used | |
1822 | * by any process. So skip the confirmation question for them. */ | |
1823 | return !unit_get_exec_context(u)->same_pgrp; | |
1824 | } | |
1825 | ||
631b676b LP |
1826 | static bool unit_verify_deps(Unit *u) { |
1827 | Unit *other; | |
eef85c4a | 1828 | void *v; |
631b676b LP |
1829 | |
1830 | assert(u); | |
1831 | ||
1832 | /* Checks whether all BindsTo= dependencies of this unit are fulfilled — if they are also combined with | |
1833 | * After=. We do not check Requires= or Requisite= here as they only should have an effect on the job | |
1834 | * processing, but do not have any effect afterwards. We don't check BindsTo= dependencies that are not used in | |
1835 | * conjunction with After= as for them any such check would make things entirely racy. */ | |
1836 | ||
90e74a66 | 1837 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_BINDS_TO]) { |
631b676b | 1838 | |
eef85c4a | 1839 | if (!hashmap_contains(u->dependencies[UNIT_AFTER], other)) |
631b676b LP |
1840 | continue; |
1841 | ||
1842 | if (!UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(other))) { | |
1843 | log_unit_notice(u, "Bound to unit %s, but unit isn't active.", other->id); | |
1844 | return false; | |
1845 | } | |
1846 | } | |
1847 | ||
1848 | return true; | |
1849 | } | |
1850 | ||
9adb6959 | 1851 | /* Errors that aren't really errors: |
6bf0f408 | 1852 | * -EALREADY: Unit is already started. |
9adb6959 | 1853 | * -ECOMM: Condition failed |
6bf0f408 | 1854 | * -EAGAIN: An operation is already in progress. Retry later. |
9adb6959 LP |
1855 | * |
1856 | * Errors that are real errors: | |
1857 | * -EBADR: This unit type does not support starting. | |
2de9b979 | 1858 | * -ECANCELED: Start limit hit, too many requests for now |
6bf0f408 LP |
1859 | * -EPROTO: Assert failed |
1860 | * -EINVAL: Unit not loaded | |
1861 | * -EOPNOTSUPP: Unit type not supported | |
631b676b | 1862 | * -ENOLINK: The necessary dependencies are not fulfilled. |
d4fd1cf2 | 1863 | * -ESTALE: This unit has been started before and can't be started a second time |
a4191c9f | 1864 | * -ENOENT: This is a triggering unit and unit to trigger is not loaded |
87f0e418 LP |
1865 | */ |
1866 | int unit_start(Unit *u) { | |
1867 | UnitActiveState state; | |
92ab323c | 1868 | Unit *following; |
87f0e418 LP |
1869 | |
1870 | assert(u); | |
1871 | ||
5766aca8 LP |
1872 | /* If this is already started, then this will succeed. Note that this will even succeed if this unit |
1873 | * is not startable by the user. This is relied on to detect when we need to wait for units and when | |
1874 | * waiting is finished. */ | |
87f0e418 LP |
1875 | state = unit_active_state(u); |
1876 | if (UNIT_IS_ACTIVE_OR_RELOADING(state)) | |
1877 | return -EALREADY; | |
380dc8b0 LP |
1878 | if (state == UNIT_MAINTENANCE) |
1879 | return -EAGAIN; | |
87f0e418 | 1880 | |
6bf0f408 LP |
1881 | /* Units that aren't loaded cannot be started */ |
1882 | if (u->load_state != UNIT_LOADED) | |
1883 | return -EINVAL; | |
1884 | ||
d4fd1cf2 LP |
1885 | /* Refuse starting scope units more than once */ |
1886 | if (UNIT_VTABLE(u)->once_only && dual_timestamp_is_set(&u->inactive_enter_timestamp)) | |
1887 | return -ESTALE; | |
1888 | ||
5766aca8 LP |
1889 | /* If the conditions failed, don't do anything at all. If we already are activating this call might |
1890 | * still be useful to speed up activation in case there is some hold-off time, but we don't want to | |
1891 | * recheck the condition in that case. */ | |
a82e5507 | 1892 | if (state != UNIT_ACTIVATING && |
5af6aa58 | 1893 | !unit_test_condition(u)) |
5766aca8 | 1894 | return log_unit_debug_errno(u, SYNTHETIC_ERRNO(ECOMM), "Starting requested but condition failed. Not starting unit."); |
52661efd | 1895 | |
59fccdc5 LP |
1896 | /* If the asserts failed, fail the entire job */ |
1897 | if (state != UNIT_ACTIVATING && | |
5766aca8 LP |
1898 | !unit_test_assert(u)) |
1899 | return log_unit_notice_errno(u, SYNTHETIC_ERRNO(EPROTO), "Starting requested but asserts failed."); | |
59fccdc5 | 1900 | |
5766aca8 LP |
1901 | /* Units of types that aren't supported cannot be started. Note that we do this test only after the |
1902 | * condition checks, so that we rather return condition check errors (which are usually not | |
1903 | * considered a true failure) than "not supported" errors (which are considered a failure). | |
d11a7645 | 1904 | */ |
96cf3ec9 | 1905 | if (!unit_type_supported(u->type)) |
d11a7645 LP |
1906 | return -EOPNOTSUPP; |
1907 | ||
5766aca8 LP |
1908 | /* Let's make sure that the deps really are in order before we start this. Normally the job engine |
1909 | * should have taken care of this already, but let's check this here again. After all, our | |
1910 | * dependencies might not be in effect anymore, due to a reload or due to a failed condition. */ | |
631b676b LP |
1911 | if (!unit_verify_deps(u)) |
1912 | return -ENOLINK; | |
1913 | ||
92ab323c | 1914 | /* Forward to the main object, if we aren't it. */ |
52990c2e ZJS |
1915 | following = unit_following(u); |
1916 | if (following) { | |
f2341e0a | 1917 | log_unit_debug(u, "Redirecting start request from %s to %s.", u->id, following->id); |
92ab323c LP |
1918 | return unit_start(following); |
1919 | } | |
1920 | ||
1921 | /* If it is stopped, but we cannot start it, then fail */ | |
1922 | if (!UNIT_VTABLE(u)->start) | |
1923 | return -EBADR; | |
1924 | ||
5766aca8 LP |
1925 | /* We don't suppress calls to ->start() here when we are already starting, to allow this request to |
1926 | * be used as a "hurry up" call, for example when the unit is in some "auto restart" state where it | |
1927 | * waits for a holdoff timer to elapse before it will start again. */ | |
87f0e418 | 1928 | |
c1e1601e | 1929 | unit_add_to_dbus_queue(u); |
d9e45bc3 | 1930 | unit_cgroup_freezer_action(u, FREEZER_THAW); |
9e58ff9c | 1931 | |
d1a34ae9 | 1932 | return UNIT_VTABLE(u)->start(u); |
87f0e418 LP |
1933 | } |
1934 | ||
1935 | bool unit_can_start(Unit *u) { | |
1936 | assert(u); | |
1937 | ||
8ff4d2ab LP |
1938 | if (u->load_state != UNIT_LOADED) |
1939 | return false; | |
1940 | ||
96cf3ec9 | 1941 | if (!unit_type_supported(u->type)) |
8ff4d2ab LP |
1942 | return false; |
1943 | ||
d4fd1cf2 LP |
1944 | /* Scope units may be started only once */ |
1945 | if (UNIT_VTABLE(u)->once_only && dual_timestamp_is_set(&u->inactive_exit_timestamp)) | |
1946 | return false; | |
1947 | ||
87f0e418 LP |
1948 | return !!UNIT_VTABLE(u)->start; |
1949 | } | |
1950 | ||
2528a7a6 LP |
1951 | bool unit_can_isolate(Unit *u) { |
1952 | assert(u); | |
1953 | ||
1954 | return unit_can_start(u) && | |
ac155bb8 | 1955 | u->allow_isolate; |
2528a7a6 LP |
1956 | } |
1957 | ||
87f0e418 LP |
1958 | /* Errors: |
1959 | * -EBADR: This unit type does not support stopping. | |
1960 | * -EALREADY: Unit is already stopped. | |
1961 | * -EAGAIN: An operation is already in progress. Retry later. | |
1962 | */ | |
1963 | int unit_stop(Unit *u) { | |
1964 | UnitActiveState state; | |
92ab323c | 1965 | Unit *following; |
87f0e418 LP |
1966 | |
1967 | assert(u); | |
1968 | ||
87f0e418 | 1969 | state = unit_active_state(u); |
fdf20a31 | 1970 | if (UNIT_IS_INACTIVE_OR_FAILED(state)) |
87f0e418 LP |
1971 | return -EALREADY; |
1972 | ||
0faacd47 LP |
1973 | following = unit_following(u); |
1974 | if (following) { | |
f2341e0a | 1975 | log_unit_debug(u, "Redirecting stop request from %s to %s.", u->id, following->id); |
92ab323c LP |
1976 | return unit_stop(following); |
1977 | } | |
1978 | ||
7898b0cf LP |
1979 | if (!UNIT_VTABLE(u)->stop) |
1980 | return -EBADR; | |
1981 | ||
c1e1601e | 1982 | unit_add_to_dbus_queue(u); |
d9e45bc3 | 1983 | unit_cgroup_freezer_action(u, FREEZER_THAW); |
9e58ff9c | 1984 | |
d1a34ae9 | 1985 | return UNIT_VTABLE(u)->stop(u); |
87f0e418 LP |
1986 | } |
1987 | ||
f5869324 LP |
1988 | bool unit_can_stop(Unit *u) { |
1989 | assert(u); | |
1990 | ||
39c79477 ZJS |
1991 | /* Note: if we return true here, it does not mean that the unit may be successfully stopped. |
1992 | * Extrinsic units follow external state and they may stop following external state changes | |
1993 | * (hence we return true here), but an attempt to do this through the manager will fail. */ | |
1994 | ||
96cf3ec9 | 1995 | if (!unit_type_supported(u->type)) |
f5869324 LP |
1996 | return false; |
1997 | ||
1998 | if (u->perpetual) | |
1999 | return false; | |
2000 | ||
2001 | return !!UNIT_VTABLE(u)->stop; | |
2002 | } | |
2003 | ||
87f0e418 LP |
2004 | /* Errors: |
2005 | * -EBADR: This unit type does not support reloading. | |
2006 | * -ENOEXEC: Unit is not started. | |
2007 | * -EAGAIN: An operation is already in progress. Retry later. | |
2008 | */ | |
2009 | int unit_reload(Unit *u) { | |
2010 | UnitActiveState state; | |
92ab323c | 2011 | Unit *following; |
87f0e418 LP |
2012 | |
2013 | assert(u); | |
2014 | ||
ac155bb8 | 2015 | if (u->load_state != UNIT_LOADED) |
6124958c LP |
2016 | return -EINVAL; |
2017 | ||
87f0e418 LP |
2018 | if (!unit_can_reload(u)) |
2019 | return -EBADR; | |
2020 | ||
2021 | state = unit_active_state(u); | |
e364ad06 | 2022 | if (state == UNIT_RELOADING) |
6255af75 | 2023 | return -EAGAIN; |
87f0e418 | 2024 | |
6a371e23 | 2025 | if (state != UNIT_ACTIVE) { |
f2341e0a | 2026 | log_unit_warning(u, "Unit cannot be reloaded because it is inactive."); |
87f0e418 | 2027 | return -ENOEXEC; |
6a371e23 | 2028 | } |
87f0e418 | 2029 | |
e48614c4 ZJS |
2030 | following = unit_following(u); |
2031 | if (following) { | |
f2341e0a | 2032 | log_unit_debug(u, "Redirecting reload request from %s to %s.", u->id, following->id); |
92ab323c LP |
2033 | return unit_reload(following); |
2034 | } | |
2035 | ||
c1e1601e | 2036 | unit_add_to_dbus_queue(u); |
82a2b6bb | 2037 | |
f54bcca5 JR |
2038 | if (!UNIT_VTABLE(u)->reload) { |
2039 | /* Unit doesn't have a reload function, but we need to propagate the reload anyway */ | |
2ad2e41a | 2040 | unit_notify(u, unit_active_state(u), unit_active_state(u), 0); |
f54bcca5 JR |
2041 | return 0; |
2042 | } | |
2043 | ||
d9e45bc3 MS |
2044 | unit_cgroup_freezer_action(u, FREEZER_THAW); |
2045 | ||
d1a34ae9 | 2046 | return UNIT_VTABLE(u)->reload(u); |
87f0e418 LP |
2047 | } |
2048 | ||
2049 | bool unit_can_reload(Unit *u) { | |
2050 | assert(u); | |
2051 | ||
f54bcca5 JR |
2052 | if (UNIT_VTABLE(u)->can_reload) |
2053 | return UNIT_VTABLE(u)->can_reload(u); | |
87f0e418 | 2054 | |
eef85c4a | 2055 | if (!hashmap_isempty(u->dependencies[UNIT_PROPAGATES_RELOAD_TO])) |
87f0e418 LP |
2056 | return true; |
2057 | ||
f54bcca5 | 2058 | return UNIT_VTABLE(u)->reload; |
87f0e418 LP |
2059 | } |
2060 | ||
a3c1168a LP |
2061 | bool unit_is_unneeded(Unit *u) { |
2062 | static const UnitDependency deps[] = { | |
be7d9ff7 | 2063 | UNIT_REQUIRED_BY, |
084918ba | 2064 | UNIT_REQUISITE_OF, |
be7d9ff7 LP |
2065 | UNIT_WANTED_BY, |
2066 | UNIT_BOUND_BY, | |
2067 | }; | |
f3bff0eb LP |
2068 | |
2069 | assert(u); | |
2070 | ||
ac155bb8 | 2071 | if (!u->stop_when_unneeded) |
a3c1168a | 2072 | return false; |
f3bff0eb | 2073 | |
a3c1168a LP |
2074 | /* Don't clean up while the unit is transitioning or is even inactive. */ |
2075 | if (!UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) | |
2076 | return false; | |
2077 | if (u->job) | |
2078 | return false; | |
f3bff0eb | 2079 | |
12375b95 | 2080 | for (size_t j = 0; j < ELEMENTSOF(deps); j++) { |
eef85c4a | 2081 | Unit *other; |
eef85c4a LP |
2082 | void *v; |
2083 | ||
fda09318 | 2084 | /* If a dependent unit has a job queued, is active or transitioning, or is marked for |
a3c1168a | 2085 | * restart, then don't clean this one up. */ |
b81884e7 | 2086 | |
90e74a66 | 2087 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[deps[j]]) { |
93d4cb09 | 2088 | if (other->job) |
a3c1168a LP |
2089 | return false; |
2090 | ||
2091 | if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) | |
2092 | return false; | |
2093 | ||
2094 | if (unit_will_restart(other)) | |
2095 | return false; | |
2096 | } | |
bea355da LP |
2097 | } |
2098 | ||
a3c1168a LP |
2099 | return true; |
2100 | } | |
f3bff0eb | 2101 | |
a3c1168a LP |
2102 | static void check_unneeded_dependencies(Unit *u) { |
2103 | ||
2104 | static const UnitDependency deps[] = { | |
2105 | UNIT_REQUIRES, | |
2106 | UNIT_REQUISITE, | |
2107 | UNIT_WANTS, | |
2108 | UNIT_BINDS_TO, | |
2109 | }; | |
a3c1168a LP |
2110 | |
2111 | assert(u); | |
2112 | ||
2113 | /* Add all units this unit depends on to the queue that processes StopWhenUnneeded= behaviour. */ | |
2114 | ||
12375b95 | 2115 | for (size_t j = 0; j < ELEMENTSOF(deps); j++) { |
a3c1168a | 2116 | Unit *other; |
a3c1168a LP |
2117 | void *v; |
2118 | ||
90e74a66 | 2119 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[deps[j]]) |
fda09318 | 2120 | unit_submit_to_stop_when_unneeded_queue(other); |
a3c1168a | 2121 | } |
f3bff0eb LP |
2122 | } |
2123 | ||
ff502445 | 2124 | static void unit_check_binds_to(Unit *u) { |
4afd3348 | 2125 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
ff502445 LP |
2126 | bool stop = false; |
2127 | Unit *other; | |
eef85c4a | 2128 | void *v; |
67bfdc97 | 2129 | int r; |
ff502445 LP |
2130 | |
2131 | assert(u); | |
2132 | ||
2133 | if (u->job) | |
2134 | return; | |
2135 | ||
2136 | if (unit_active_state(u) != UNIT_ACTIVE) | |
2137 | return; | |
2138 | ||
90e74a66 | 2139 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_BINDS_TO]) { |
ff502445 LP |
2140 | if (other->job) |
2141 | continue; | |
13ddc3fc ZJS |
2142 | |
2143 | if (!other->coldplugged) | |
2144 | /* We might yet create a job for the other unit… */ | |
2145 | continue; | |
ff502445 LP |
2146 | |
2147 | if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) | |
2148 | continue; | |
2149 | ||
2150 | stop = true; | |
98f738b6 | 2151 | break; |
ff502445 LP |
2152 | } |
2153 | ||
2154 | if (!stop) | |
2155 | return; | |
2156 | ||
595bfe7d | 2157 | /* If stopping a unit fails continuously we might enter a stop |
67bfdc97 LP |
2158 | * loop here, hence stop acting on the service being |
2159 | * unnecessary after a while. */ | |
7994ac1d | 2160 | if (!ratelimit_below(&u->auto_stop_ratelimit)) { |
67bfdc97 LP |
2161 | log_unit_warning(u, "Unit is bound to inactive unit %s, but not stopping since we tried this too often recently.", other->id); |
2162 | return; | |
2163 | } | |
2164 | ||
98f738b6 | 2165 | assert(other); |
f2341e0a | 2166 | log_unit_info(u, "Unit is bound to inactive unit %s. Stopping, too.", other->id); |
ff502445 LP |
2167 | |
2168 | /* A unit we need to run is gone. Sniff. Let's stop this. */ | |
50cbaba4 | 2169 | r = manager_add_job(u->manager, JOB_STOP, u, JOB_FAIL, NULL, &error, NULL); |
67bfdc97 | 2170 | if (r < 0) |
4bd29fe5 | 2171 | log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %s", bus_error_message(&error, r)); |
ff502445 LP |
2172 | } |
2173 | ||
87f0e418 | 2174 | static void retroactively_start_dependencies(Unit *u) { |
87f0e418 | 2175 | Unit *other; |
eef85c4a | 2176 | void *v; |
87f0e418 LP |
2177 | |
2178 | assert(u); | |
2179 | assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))); | |
2180 | ||
90e74a66 | 2181 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_REQUIRES]) |
eef85c4a | 2182 | if (!hashmap_get(u->dependencies[UNIT_AFTER], other) && |
b81884e7 | 2183 | !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) |
50cbaba4 | 2184 | manager_add_job(u->manager, JOB_START, other, JOB_REPLACE, NULL, NULL, NULL); |
b81884e7 | 2185 | |
90e74a66 | 2186 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_BINDS_TO]) |
eef85c4a | 2187 | if (!hashmap_get(u->dependencies[UNIT_AFTER], other) && |
b81884e7 | 2188 | !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) |
50cbaba4 | 2189 | manager_add_job(u->manager, JOB_START, other, JOB_REPLACE, NULL, NULL, NULL); |
87f0e418 | 2190 | |
90e74a66 | 2191 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_WANTS]) |
eef85c4a | 2192 | if (!hashmap_get(u->dependencies[UNIT_AFTER], other) && |
b81884e7 | 2193 | !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) |
50cbaba4 | 2194 | manager_add_job(u->manager, JOB_START, other, JOB_FAIL, NULL, NULL, NULL); |
87f0e418 | 2195 | |
90e74a66 | 2196 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_CONFLICTS]) |
b81884e7 | 2197 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
50cbaba4 | 2198 | manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, NULL, NULL, NULL); |
69dd2852 | 2199 | |
90e74a66 | 2200 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_CONFLICTED_BY]) |
b81884e7 | 2201 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
50cbaba4 | 2202 | manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, NULL, NULL, NULL); |
87f0e418 LP |
2203 | } |
2204 | ||
2205 | static void retroactively_stop_dependencies(Unit *u) { | |
87f0e418 | 2206 | Unit *other; |
eef85c4a | 2207 | void *v; |
87f0e418 LP |
2208 | |
2209 | assert(u); | |
2210 | assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))); | |
2211 | ||
b81884e7 | 2212 | /* Pull down units which are bound to us recursively if enabled */ |
90e74a66 | 2213 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_BOUND_BY]) |
b81884e7 | 2214 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
50cbaba4 | 2215 | manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, NULL, NULL, NULL); |
cd0504d0 MS |
2216 | } |
2217 | ||
3ecaa09b | 2218 | void unit_start_on_failure(Unit *u) { |
c0daa706 | 2219 | Unit *other; |
eef85c4a | 2220 | void *v; |
7f66b026 | 2221 | int r; |
c0daa706 LP |
2222 | |
2223 | assert(u); | |
2224 | ||
eef85c4a | 2225 | if (hashmap_size(u->dependencies[UNIT_ON_FAILURE]) <= 0) |
222ae6a8 LP |
2226 | return; |
2227 | ||
f2341e0a | 2228 | log_unit_info(u, "Triggering OnFailure= dependencies."); |
222ae6a8 | 2229 | |
90e74a66 | 2230 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_ON_FAILURE]) { |
7f66b026 | 2231 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
222ae6a8 | 2232 | |
50cbaba4 | 2233 | r = manager_add_job(u->manager, JOB_START, other, u->on_failure_job_mode, NULL, &error, NULL); |
c1b6628d | 2234 | if (r < 0) |
7f66b026 | 2235 | log_unit_warning_errno(u, r, "Failed to enqueue OnFailure= job, ignoring: %s", bus_error_message(&error, r)); |
222ae6a8 | 2236 | } |
c0daa706 LP |
2237 | } |
2238 | ||
3ecaa09b LP |
2239 | void unit_trigger_notify(Unit *u) { |
2240 | Unit *other; | |
eef85c4a | 2241 | void *v; |
3ecaa09b LP |
2242 | |
2243 | assert(u); | |
2244 | ||
90e74a66 | 2245 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_TRIGGERED_BY]) |
3ecaa09b LP |
2246 | if (UNIT_VTABLE(other)->trigger_notify) |
2247 | UNIT_VTABLE(other)->trigger_notify(other, u); | |
2248 | } | |
2249 | ||
37109b85 ZJS |
2250 | static int raise_level(int log_level, bool condition_info, bool condition_notice) { |
2251 | if (condition_notice && log_level > LOG_NOTICE) | |
2252 | return LOG_NOTICE; | |
2253 | if (condition_info && log_level > LOG_INFO) | |
2254 | return LOG_INFO; | |
2255 | return log_level; | |
2256 | } | |
2257 | ||
915b1d01 | 2258 | static int unit_log_resources(Unit *u) { |
bc40a20e LP |
2259 | struct iovec iovec[1 + _CGROUP_IP_ACCOUNTING_METRIC_MAX + _CGROUP_IO_ACCOUNTING_METRIC_MAX + 4]; |
2260 | bool any_traffic = false, have_ip_accounting = false, any_io = false, have_io_accounting = false; | |
2261 | _cleanup_free_ char *igress = NULL, *egress = NULL, *rr = NULL, *wr = NULL; | |
162392b7 | 2262 | int log_level = LOG_DEBUG; /* May be raised if resources consumed over a threshold */ |
915b1d01 | 2263 | size_t n_message_parts = 0, n_iovec = 0; |
bc40a20e | 2264 | char* message_parts[1 + 2 + 2 + 1], *t; |
915b1d01 | 2265 | nsec_t nsec = NSEC_INFINITY; |
915b1d01 LP |
2266 | int r; |
2267 | const char* const ip_fields[_CGROUP_IP_ACCOUNTING_METRIC_MAX] = { | |
2268 | [CGROUP_IP_INGRESS_BYTES] = "IP_METRIC_INGRESS_BYTES", | |
2269 | [CGROUP_IP_INGRESS_PACKETS] = "IP_METRIC_INGRESS_PACKETS", | |
2270 | [CGROUP_IP_EGRESS_BYTES] = "IP_METRIC_EGRESS_BYTES", | |
2271 | [CGROUP_IP_EGRESS_PACKETS] = "IP_METRIC_EGRESS_PACKETS", | |
2272 | }; | |
bc40a20e LP |
2273 | const char* const io_fields[_CGROUP_IO_ACCOUNTING_METRIC_MAX] = { |
2274 | [CGROUP_IO_READ_BYTES] = "IO_METRIC_READ_BYTES", | |
2275 | [CGROUP_IO_WRITE_BYTES] = "IO_METRIC_WRITE_BYTES", | |
2276 | [CGROUP_IO_READ_OPERATIONS] = "IO_METRIC_READ_OPERATIONS", | |
2277 | [CGROUP_IO_WRITE_OPERATIONS] = "IO_METRIC_WRITE_OPERATIONS", | |
2278 | }; | |
915b1d01 LP |
2279 | |
2280 | assert(u); | |
2281 | ||
2282 | /* Invoked whenever a unit enters failed or dead state. Logs information about consumed resources if resource | |
2283 | * accounting was enabled for a unit. It does this in two ways: a friendly human readable string with reduced | |
2284 | * information and the complete data in structured fields. */ | |
2285 | ||
2286 | (void) unit_get_cpu_usage(u, &nsec); | |
2287 | if (nsec != NSEC_INFINITY) { | |
2288 | char buf[FORMAT_TIMESPAN_MAX] = ""; | |
2289 | ||
2290 | /* Format the CPU time for inclusion in the structured log message */ | |
2291 | if (asprintf(&t, "CPU_USAGE_NSEC=%" PRIu64, nsec) < 0) { | |
2292 | r = log_oom(); | |
2293 | goto finish; | |
2294 | } | |
2295 | iovec[n_iovec++] = IOVEC_MAKE_STRING(t); | |
2296 | ||
2297 | /* Format the CPU time for inclusion in the human language message string */ | |
2298 | format_timespan(buf, sizeof(buf), nsec / NSEC_PER_USEC, USEC_PER_MSEC); | |
ec9d636b | 2299 | t = strjoin("consumed ", buf, " CPU time"); |
915b1d01 LP |
2300 | if (!t) { |
2301 | r = log_oom(); | |
2302 | goto finish; | |
2303 | } | |
2304 | ||
2305 | message_parts[n_message_parts++] = t; | |
37109b85 ZJS |
2306 | |
2307 | log_level = raise_level(log_level, | |
2308 | nsec > NOTICEWORTHY_CPU_NSEC, | |
2309 | nsec > MENTIONWORTHY_CPU_NSEC); | |
915b1d01 LP |
2310 | } |
2311 | ||
bc40a20e LP |
2312 | for (CGroupIOAccountingMetric k = 0; k < _CGROUP_IO_ACCOUNTING_METRIC_MAX; k++) { |
2313 | char buf[FORMAT_BYTES_MAX] = ""; | |
2314 | uint64_t value = UINT64_MAX; | |
2315 | ||
2316 | assert(io_fields[k]); | |
2317 | ||
2318 | (void) unit_get_io_accounting(u, k, k > 0, &value); | |
2319 | if (value == UINT64_MAX) | |
2320 | continue; | |
2321 | ||
2322 | have_io_accounting = true; | |
2323 | if (value > 0) | |
2324 | any_io = true; | |
2325 | ||
2326 | /* Format IO accounting data for inclusion in the structured log message */ | |
2327 | if (asprintf(&t, "%s=%" PRIu64, io_fields[k], value) < 0) { | |
2328 | r = log_oom(); | |
2329 | goto finish; | |
2330 | } | |
2331 | iovec[n_iovec++] = IOVEC_MAKE_STRING(t); | |
2332 | ||
2333 | /* Format the IO accounting data for inclusion in the human language message string, but only | |
2334 | * for the bytes counters (and not for the operations counters) */ | |
2335 | if (k == CGROUP_IO_READ_BYTES) { | |
2336 | assert(!rr); | |
2337 | rr = strjoin("read ", format_bytes(buf, sizeof(buf), value), " from disk"); | |
2338 | if (!rr) { | |
2339 | r = log_oom(); | |
2340 | goto finish; | |
2341 | } | |
2342 | } else if (k == CGROUP_IO_WRITE_BYTES) { | |
2343 | assert(!wr); | |
2344 | wr = strjoin("written ", format_bytes(buf, sizeof(buf), value), " to disk"); | |
2345 | if (!wr) { | |
2346 | r = log_oom(); | |
2347 | goto finish; | |
2348 | } | |
2349 | } | |
37109b85 ZJS |
2350 | |
2351 | if (IN_SET(k, CGROUP_IO_READ_BYTES, CGROUP_IO_WRITE_BYTES)) | |
2352 | log_level = raise_level(log_level, | |
2353 | value > MENTIONWORTHY_IO_BYTES, | |
2354 | value > NOTICEWORTHY_IO_BYTES); | |
bc40a20e LP |
2355 | } |
2356 | ||
2357 | if (have_io_accounting) { | |
2358 | if (any_io) { | |
2359 | if (rr) | |
2360 | message_parts[n_message_parts++] = TAKE_PTR(rr); | |
2361 | if (wr) | |
2362 | message_parts[n_message_parts++] = TAKE_PTR(wr); | |
2363 | ||
2364 | } else { | |
2365 | char *k; | |
2366 | ||
2367 | k = strdup("no IO"); | |
2368 | if (!k) { | |
2369 | r = log_oom(); | |
2370 | goto finish; | |
2371 | } | |
2372 | ||
2373 | message_parts[n_message_parts++] = k; | |
2374 | } | |
2375 | } | |
2376 | ||
12375b95 | 2377 | for (CGroupIPAccountingMetric m = 0; m < _CGROUP_IP_ACCOUNTING_METRIC_MAX; m++) { |
915b1d01 LP |
2378 | char buf[FORMAT_BYTES_MAX] = ""; |
2379 | uint64_t value = UINT64_MAX; | |
2380 | ||
2381 | assert(ip_fields[m]); | |
2382 | ||
2383 | (void) unit_get_ip_accounting(u, m, &value); | |
2384 | if (value == UINT64_MAX) | |
2385 | continue; | |
82044702 LP |
2386 | |
2387 | have_ip_accounting = true; | |
a87b1faa LP |
2388 | if (value > 0) |
2389 | any_traffic = true; | |
915b1d01 LP |
2390 | |
2391 | /* Format IP accounting data for inclusion in the structured log message */ | |
2392 | if (asprintf(&t, "%s=%" PRIu64, ip_fields[m], value) < 0) { | |
2393 | r = log_oom(); | |
2394 | goto finish; | |
2395 | } | |
2396 | iovec[n_iovec++] = IOVEC_MAKE_STRING(t); | |
2397 | ||
2398 | /* Format the IP accounting data for inclusion in the human language message string, but only for the | |
2399 | * bytes counters (and not for the packets counters) */ | |
a87b1faa LP |
2400 | if (m == CGROUP_IP_INGRESS_BYTES) { |
2401 | assert(!igress); | |
ec9d636b | 2402 | igress = strjoin("received ", format_bytes(buf, sizeof(buf), value), " IP traffic"); |
a87b1faa LP |
2403 | if (!igress) { |
2404 | r = log_oom(); | |
2405 | goto finish; | |
2406 | } | |
2407 | } else if (m == CGROUP_IP_EGRESS_BYTES) { | |
2408 | assert(!egress); | |
ec9d636b | 2409 | egress = strjoin("sent ", format_bytes(buf, sizeof(buf), value), " IP traffic"); |
a87b1faa LP |
2410 | if (!egress) { |
2411 | r = log_oom(); | |
2412 | goto finish; | |
2413 | } | |
2414 | } | |
37109b85 ZJS |
2415 | |
2416 | if (IN_SET(m, CGROUP_IP_INGRESS_BYTES, CGROUP_IP_EGRESS_BYTES)) | |
2417 | log_level = raise_level(log_level, | |
2418 | value > MENTIONWORTHY_IP_BYTES, | |
2419 | value > NOTICEWORTHY_IP_BYTES); | |
a87b1faa LP |
2420 | } |
2421 | ||
82044702 LP |
2422 | if (have_ip_accounting) { |
2423 | if (any_traffic) { | |
2424 | if (igress) | |
2425 | message_parts[n_message_parts++] = TAKE_PTR(igress); | |
2426 | if (egress) | |
2427 | message_parts[n_message_parts++] = TAKE_PTR(egress); | |
a87b1faa | 2428 | |
82044702 LP |
2429 | } else { |
2430 | char *k; | |
915b1d01 | 2431 | |
82044702 LP |
2432 | k = strdup("no IP traffic"); |
2433 | if (!k) { | |
2434 | r = log_oom(); | |
2435 | goto finish; | |
2436 | } | |
2437 | ||
2438 | message_parts[n_message_parts++] = k; | |
2439 | } | |
915b1d01 LP |
2440 | } |
2441 | ||
2442 | /* Is there any accounting data available at all? */ | |
2443 | if (n_iovec == 0) { | |
2444 | r = 0; | |
2445 | goto finish; | |
2446 | } | |
2447 | ||
2448 | if (n_message_parts == 0) | |
a87b1faa | 2449 | t = strjoina("MESSAGE=", u->id, ": Completed."); |
915b1d01 LP |
2450 | else { |
2451 | _cleanup_free_ char *joined; | |
2452 | ||
2453 | message_parts[n_message_parts] = NULL; | |
2454 | ||
2455 | joined = strv_join(message_parts, ", "); | |
2456 | if (!joined) { | |
2457 | r = log_oom(); | |
2458 | goto finish; | |
2459 | } | |
2460 | ||
ec9d636b | 2461 | joined[0] = ascii_toupper(joined[0]); |
a87b1faa | 2462 | t = strjoina("MESSAGE=", u->id, ": ", joined, "."); |
915b1d01 LP |
2463 | } |
2464 | ||
2465 | /* The following four fields we allocate on the stack or are static strings, we hence don't want to free them, | |
2466 | * and hence don't increase n_iovec for them */ | |
2467 | iovec[n_iovec] = IOVEC_MAKE_STRING(t); | |
2468 | iovec[n_iovec + 1] = IOVEC_MAKE_STRING("MESSAGE_ID=" SD_MESSAGE_UNIT_RESOURCES_STR); | |
2469 | ||
2470 | t = strjoina(u->manager->unit_log_field, u->id); | |
2471 | iovec[n_iovec + 2] = IOVEC_MAKE_STRING(t); | |
2472 | ||
2473 | t = strjoina(u->manager->invocation_log_field, u->invocation_id_string); | |
2474 | iovec[n_iovec + 3] = IOVEC_MAKE_STRING(t); | |
2475 | ||
37109b85 | 2476 | log_struct_iovec(log_level, iovec, n_iovec + 4); |
915b1d01 LP |
2477 | r = 0; |
2478 | ||
2479 | finish: | |
12375b95 | 2480 | for (size_t i = 0; i < n_message_parts; i++) |
915b1d01 LP |
2481 | free(message_parts[i]); |
2482 | ||
12375b95 | 2483 | for (size_t i = 0; i < n_iovec; i++) |
915b1d01 LP |
2484 | free(iovec[i].iov_base); |
2485 | ||
2486 | return r; | |
2487 | ||
2488 | } | |
2489 | ||
adefcf28 LP |
2490 | static void unit_update_on_console(Unit *u) { |
2491 | bool b; | |
2492 | ||
2493 | assert(u); | |
2494 | ||
2495 | b = unit_needs_console(u); | |
2496 | if (u->on_console == b) | |
2497 | return; | |
2498 | ||
2499 | u->on_console = b; | |
2500 | if (b) | |
2501 | manager_ref_console(u->manager); | |
2502 | else | |
2503 | manager_unref_console(u->manager); | |
adefcf28 LP |
2504 | } |
2505 | ||
6eb65e7c LP |
2506 | static void unit_emit_audit_start(Unit *u) { |
2507 | assert(u); | |
2508 | ||
2509 | if (u->type != UNIT_SERVICE) | |
2510 | return; | |
2511 | ||
2512 | /* Write audit record if we have just finished starting up */ | |
2513 | manager_send_unit_audit(u->manager, u, AUDIT_SERVICE_START, true); | |
2514 | u->in_audit = true; | |
2515 | } | |
2516 | ||
2517 | static void unit_emit_audit_stop(Unit *u, UnitActiveState state) { | |
2518 | assert(u); | |
2519 | ||
2520 | if (u->type != UNIT_SERVICE) | |
2521 | return; | |
2522 | ||
2523 | if (u->in_audit) { | |
2524 | /* Write audit record if we have just finished shutting down */ | |
2525 | manager_send_unit_audit(u->manager, u, AUDIT_SERVICE_STOP, state == UNIT_INACTIVE); | |
2526 | u->in_audit = false; | |
2527 | } else { | |
2528 | /* Hmm, if there was no start record written write it now, so that we always have a nice pair */ | |
2529 | manager_send_unit_audit(u->manager, u, AUDIT_SERVICE_START, state == UNIT_INACTIVE); | |
2530 | ||
2531 | if (state == UNIT_INACTIVE) | |
2532 | manager_send_unit_audit(u->manager, u, AUDIT_SERVICE_STOP, true); | |
2533 | } | |
2534 | } | |
2535 | ||
16c74914 LP |
2536 | static bool unit_process_job(Job *j, UnitActiveState ns, UnitNotifyFlags flags) { |
2537 | bool unexpected = false; | |
31cd5f63 | 2538 | JobResult result; |
16c74914 LP |
2539 | |
2540 | assert(j); | |
2541 | ||
2542 | if (j->state == JOB_WAITING) | |
2543 | ||
2544 | /* So we reached a different state for this job. Let's see if we can run it now if it failed previously | |
2545 | * due to EAGAIN. */ | |
2546 | job_add_to_run_queue(j); | |
2547 | ||
2548 | /* Let's check whether the unit's new state constitutes a finished job, or maybe contradicts a running job and | |
2549 | * hence needs to invalidate jobs. */ | |
2550 | ||
2551 | switch (j->type) { | |
2552 | ||
2553 | case JOB_START: | |
2554 | case JOB_VERIFY_ACTIVE: | |
2555 | ||
2556 | if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) | |
2557 | job_finish_and_invalidate(j, JOB_DONE, true, false); | |
2558 | else if (j->state == JOB_RUNNING && ns != UNIT_ACTIVATING) { | |
2559 | unexpected = true; | |
2560 | ||
31cd5f63 AZ |
2561 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) { |
2562 | if (ns == UNIT_FAILED) | |
2563 | result = JOB_FAILED; | |
2564 | else if (FLAGS_SET(flags, UNIT_NOTIFY_SKIP_CONDITION)) | |
2565 | result = JOB_SKIPPED; | |
2566 | else | |
2567 | result = JOB_DONE; | |
2568 | ||
2569 | job_finish_and_invalidate(j, result, true, false); | |
2570 | } | |
16c74914 LP |
2571 | } |
2572 | ||
2573 | break; | |
2574 | ||
2575 | case JOB_RELOAD: | |
2576 | case JOB_RELOAD_OR_START: | |
2577 | case JOB_TRY_RELOAD: | |
2578 | ||
2579 | if (j->state == JOB_RUNNING) { | |
2580 | if (ns == UNIT_ACTIVE) | |
2581 | job_finish_and_invalidate(j, (flags & UNIT_NOTIFY_RELOAD_FAILURE) ? JOB_FAILED : JOB_DONE, true, false); | |
2582 | else if (!IN_SET(ns, UNIT_ACTIVATING, UNIT_RELOADING)) { | |
2583 | unexpected = true; | |
2584 | ||
2585 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) | |
2586 | job_finish_and_invalidate(j, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE, true, false); | |
2587 | } | |
2588 | } | |
2589 | ||
2590 | break; | |
2591 | ||
2592 | case JOB_STOP: | |
2593 | case JOB_RESTART: | |
2594 | case JOB_TRY_RESTART: | |
2595 | ||
2596 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) | |
2597 | job_finish_and_invalidate(j, JOB_DONE, true, false); | |
2598 | else if (j->state == JOB_RUNNING && ns != UNIT_DEACTIVATING) { | |
2599 | unexpected = true; | |
2600 | job_finish_and_invalidate(j, JOB_FAILED, true, false); | |
2601 | } | |
2602 | ||
2603 | break; | |
2604 | ||
2605 | default: | |
2606 | assert_not_reached("Job type unknown"); | |
2607 | } | |
2608 | ||
2609 | return unexpected; | |
2610 | } | |
2611 | ||
2ad2e41a | 2612 | void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, UnitNotifyFlags flags) { |
429926e9 | 2613 | const char *reason; |
8559b3b7 | 2614 | Manager *m; |
a096ed36 | 2615 | |
87f0e418 LP |
2616 | assert(u); |
2617 | assert(os < _UNIT_ACTIVE_STATE_MAX); | |
2618 | assert(ns < _UNIT_ACTIVE_STATE_MAX); | |
87f0e418 | 2619 | |
8559b3b7 LP |
2620 | /* Note that this is called for all low-level state changes, even if they might map to the same high-level |
2621 | * UnitActiveState! That means that ns == os is an expected behavior here. For example: if a mount point is | |
2622 | * remounted this function will be called too! */ | |
87f0e418 | 2623 | |
546ac4f0 MS |
2624 | m = u->manager; |
2625 | ||
3c4832ad LP |
2626 | /* Let's enqueue the change signal early. In case this unit has a job associated we want that this unit is in |
2627 | * the bus queue, so that any job change signal queued will force out the unit change signal first. */ | |
2628 | unit_add_to_dbus_queue(u); | |
2629 | ||
e30bbc90 AZ |
2630 | /* Update systemd-oomd on the property/state change */ |
2631 | if (os != ns) { | |
2632 | /* Always send an update if the unit is going into an inactive state so systemd-oomd knows to stop | |
2633 | * monitoring. | |
2634 | * Also send an update whenever the unit goes active; this is to handle a case where an override file | |
2635 | * sets one of the ManagedOOM*= properties to "kill", then later removes it. systemd-oomd needs to | |
2636 | * know to stop monitoring when the unit changes from "kill" -> "auto" on daemon-reload, but we don't | |
2637 | * have the information on the property. Thus, indiscriminately send an update. */ | |
f561e8c6 | 2638 | if (UNIT_IS_INACTIVE_OR_FAILED(ns) || UNIT_IS_ACTIVE_OR_RELOADING(ns)) |
e30bbc90 AZ |
2639 | (void) manager_varlink_send_managed_oom_update(u); |
2640 | } | |
2641 | ||
f755e3b7 | 2642 | /* Update timestamps for state changes */ |
2c289ea8 | 2643 | if (!MANAGER_IS_RELOADING(m)) { |
a483fb59 | 2644 | dual_timestamp_get(&u->state_change_timestamp); |
173e3821 | 2645 | |
bdbf9951 | 2646 | if (UNIT_IS_INACTIVE_OR_FAILED(os) && !UNIT_IS_INACTIVE_OR_FAILED(ns)) |
a483fb59 | 2647 | u->inactive_exit_timestamp = u->state_change_timestamp; |
bdbf9951 | 2648 | else if (!UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_INACTIVE_OR_FAILED(ns)) |
a483fb59 | 2649 | u->inactive_enter_timestamp = u->state_change_timestamp; |
bdbf9951 LP |
2650 | |
2651 | if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns)) | |
a483fb59 | 2652 | u->active_enter_timestamp = u->state_change_timestamp; |
bdbf9951 | 2653 | else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns)) |
a483fb59 | 2654 | u->active_exit_timestamp = u->state_change_timestamp; |
bdbf9951 | 2655 | } |
87f0e418 | 2656 | |
865cc19a | 2657 | /* Keep track of failed units */ |
8724defe | 2658 | (void) manager_update_failed_units(m, u, ns == UNIT_FAILED); |
f755e3b7 | 2659 | |
d3070fbd LP |
2660 | /* Make sure the cgroup and state files are always removed when we become inactive */ |
2661 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) { | |
efdb0237 | 2662 | unit_prune_cgroup(u); |
d3070fbd LP |
2663 | unit_unlink_state_files(u); |
2664 | } | |
fb385181 | 2665 | |
adefcf28 | 2666 | unit_update_on_console(u); |
7ed9f6cd | 2667 | |
2c289ea8 | 2668 | if (!MANAGER_IS_RELOADING(m)) { |
a1c7334b LP |
2669 | bool unexpected; |
2670 | ||
2671 | /* Let's propagate state changes to the job */ | |
2672 | if (u->job) | |
2673 | unexpected = unit_process_job(u->job, ns, flags); | |
2674 | else | |
2675 | unexpected = true; | |
f3bff0eb | 2676 | |
a1c7334b LP |
2677 | /* If this state change happened without being requested by a job, then let's retroactively start or |
2678 | * stop dependencies. We skip that step when deserializing, since we don't want to create any | |
2679 | * additional jobs just because something is already activated. */ | |
bdbf9951 LP |
2680 | |
2681 | if (unexpected) { | |
2682 | if (UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns)) | |
2683 | retroactively_start_dependencies(u); | |
2684 | else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns)) | |
2685 | retroactively_stop_dependencies(u); | |
2686 | } | |
5de9682c | 2687 | |
cd0504d0 | 2688 | /* stop unneeded units regardless if going down was expected or not */ |
a3c1168a | 2689 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) |
cd0504d0 MS |
2690 | check_unneeded_dependencies(u); |
2691 | ||
bdbf9951 | 2692 | if (ns != os && ns == UNIT_FAILED) { |
ed77d407 | 2693 | log_unit_debug(u, "Unit entered failed state."); |
2ad2e41a LP |
2694 | |
2695 | if (!(flags & UNIT_NOTIFY_WILL_AUTO_RESTART)) | |
2696 | unit_start_on_failure(u); | |
cd6d0a45 | 2697 | } |
e537352b | 2698 | |
256f65d0 LP |
2699 | if (UNIT_IS_ACTIVE_OR_RELOADING(ns) && !UNIT_IS_ACTIVE_OR_RELOADING(os)) { |
2700 | /* This unit just finished starting up */ | |
3b2775c5 | 2701 | |
6eb65e7c | 2702 | unit_emit_audit_start(u); |
546ac4f0 | 2703 | manager_send_unit_plymouth(m, u); |
256f65d0 | 2704 | } |
bdbf9951 | 2705 | |
256f65d0 | 2706 | if (UNIT_IS_INACTIVE_OR_FAILED(ns) && !UNIT_IS_INACTIVE_OR_FAILED(os)) { |
915b1d01 | 2707 | /* This unit just stopped/failed. */ |
256f65d0 | 2708 | |
6eb65e7c | 2709 | unit_emit_audit_stop(u, ns); |
915b1d01 | 2710 | unit_log_resources(u); |
cd6d0a45 | 2711 | } |
f278026d LP |
2712 | } |
2713 | ||
31dc1ca3 LP |
2714 | manager_recheck_journal(m); |
2715 | manager_recheck_dbus(m); | |
e63ebf71 | 2716 | |
3ecaa09b | 2717 | unit_trigger_notify(u); |
3b2775c5 | 2718 | |
8724defe | 2719 | if (!MANAGER_IS_RELOADING(m)) { |
8559b3b7 | 2720 | /* Maybe we finished startup and are now ready for being stopped because unneeded? */ |
fda09318 | 2721 | unit_submit_to_stop_when_unneeded_queue(u); |
c1e1601e | 2722 | |
8559b3b7 LP |
2723 | /* Maybe we finished startup, but something we needed has vanished? Let's die then. (This happens when |
2724 | * something BindsTo= to a Type=oneshot unit, as these units go directly from starting to inactive, | |
ff502445 LP |
2725 | * without ever entering started.) */ |
2726 | unit_check_binds_to(u); | |
53c35a76 | 2727 | |
429926e9 TR |
2728 | if (os != UNIT_FAILED && ns == UNIT_FAILED) { |
2729 | reason = strjoina("unit ", u->id, " failed"); | |
36c4dc08 | 2730 | emergency_action(m, u->failure_action, 0, u->reboot_arg, unit_failure_action_exit_status(u), reason); |
429926e9 TR |
2731 | } else if (!UNIT_IS_INACTIVE_OR_FAILED(os) && ns == UNIT_INACTIVE) { |
2732 | reason = strjoina("unit ", u->id, " succeeded"); | |
36c4dc08 | 2733 | emergency_action(m, u->success_action, 0, u->reboot_arg, unit_success_action_exit_status(u), reason); |
429926e9 | 2734 | } |
ff502445 LP |
2735 | } |
2736 | ||
701cc384 | 2737 | unit_add_to_gc_queue(u); |
87f0e418 LP |
2738 | } |
2739 | ||
f75f613d | 2740 | int unit_watch_pid(Unit *u, pid_t pid, bool exclusive) { |
62a76913 | 2741 | int r; |
a911bb9a | 2742 | |
87f0e418 | 2743 | assert(u); |
62a76913 | 2744 | assert(pid_is_valid(pid)); |
87f0e418 | 2745 | |
62a76913 | 2746 | /* Watch a specific PID */ |
5ba6985b | 2747 | |
f75f613d FB |
2748 | /* Caller might be sure that this PID belongs to this unit only. Let's take this |
2749 | * opportunity to remove any stalled references to this PID as they can be created | |
2750 | * easily (when watching a process which is not our direct child). */ | |
2751 | if (exclusive) | |
2752 | manager_unwatch_pid(u->manager, pid); | |
2753 | ||
d5099efc | 2754 | r = set_ensure_allocated(&u->pids, NULL); |
5ba6985b LP |
2755 | if (r < 0) |
2756 | return r; | |
2757 | ||
62a76913 | 2758 | r = hashmap_ensure_allocated(&u->manager->watch_pids, NULL); |
a911bb9a LP |
2759 | if (r < 0) |
2760 | return r; | |
2761 | ||
62a76913 LP |
2762 | /* First try, let's add the unit keyed by "pid". */ |
2763 | r = hashmap_put(u->manager->watch_pids, PID_TO_PTR(pid), u); | |
2764 | if (r == -EEXIST) { | |
2765 | Unit **array; | |
2766 | bool found = false; | |
2767 | size_t n = 0; | |
05e343b7 | 2768 | |
62a76913 LP |
2769 | /* OK, the "pid" key is already assigned to a different unit. Let's see if the "-pid" key (which points |
2770 | * to an array of Units rather than just a Unit), lists us already. */ | |
a911bb9a | 2771 | |
62a76913 LP |
2772 | array = hashmap_get(u->manager->watch_pids, PID_TO_PTR(-pid)); |
2773 | if (array) | |
2774 | for (; array[n]; n++) | |
2775 | if (array[n] == u) | |
2776 | found = true; | |
a911bb9a | 2777 | |
62a76913 LP |
2778 | if (found) /* Found it already? if so, do nothing */ |
2779 | r = 0; | |
2780 | else { | |
2781 | Unit **new_array; | |
2782 | ||
2783 | /* Allocate a new array */ | |
2784 | new_array = new(Unit*, n + 2); | |
2785 | if (!new_array) | |
2786 | return -ENOMEM; | |
2787 | ||
2788 | memcpy_safe(new_array, array, sizeof(Unit*) * n); | |
2789 | new_array[n] = u; | |
2790 | new_array[n+1] = NULL; | |
2791 | ||
2792 | /* Add or replace the old array */ | |
2793 | r = hashmap_replace(u->manager->watch_pids, PID_TO_PTR(-pid), new_array); | |
2794 | if (r < 0) { | |
2795 | free(new_array); | |
2796 | return r; | |
2797 | } | |
2798 | ||
2799 | free(array); | |
2800 | } | |
2801 | } else if (r < 0) | |
2802 | return r; | |
2803 | ||
2804 | r = set_put(u->pids, PID_TO_PTR(pid)); | |
2805 | if (r < 0) | |
2806 | return r; | |
2807 | ||
2808 | return 0; | |
87f0e418 LP |
2809 | } |
2810 | ||
2811 | void unit_unwatch_pid(Unit *u, pid_t pid) { | |
62a76913 LP |
2812 | Unit **array; |
2813 | ||
87f0e418 | 2814 | assert(u); |
62a76913 LP |
2815 | assert(pid_is_valid(pid)); |
2816 | ||
2817 | /* First let's drop the unit in case it's keyed as "pid". */ | |
2818 | (void) hashmap_remove_value(u->manager->watch_pids, PID_TO_PTR(pid), u); | |
2819 | ||
2820 | /* Then, let's also drop the unit, in case it's in the array keyed by -pid */ | |
2821 | array = hashmap_get(u->manager->watch_pids, PID_TO_PTR(-pid)); | |
2822 | if (array) { | |
62a76913 | 2823 | /* Let's iterate through the array, dropping our own entry */ |
12375b95 ZJS |
2824 | |
2825 | size_t m = 0; | |
2826 | for (size_t n = 0; array[n]; n++) | |
62a76913 LP |
2827 | if (array[n] != u) |
2828 | array[m++] = array[n]; | |
2829 | array[m] = NULL; | |
2830 | ||
2831 | if (m == 0) { | |
2832 | /* The array is now empty, remove the entire entry */ | |
20c3acfa | 2833 | assert_se(hashmap_remove(u->manager->watch_pids, PID_TO_PTR(-pid)) == array); |
62a76913 LP |
2834 | free(array); |
2835 | } | |
2836 | } | |
87f0e418 | 2837 | |
fea72cc0 | 2838 | (void) set_remove(u->pids, PID_TO_PTR(pid)); |
a911bb9a LP |
2839 | } |
2840 | ||
bd44e61b LP |
2841 | void unit_unwatch_all_pids(Unit *u) { |
2842 | assert(u); | |
2843 | ||
2844 | while (!set_isempty(u->pids)) | |
fea72cc0 | 2845 | unit_unwatch_pid(u, PTR_TO_PID(set_first(u->pids))); |
bd44e61b | 2846 | |
efdb0237 | 2847 | u->pids = set_free(u->pids); |
a911bb9a LP |
2848 | } |
2849 | ||
50be4f4a LP |
2850 | static void unit_tidy_watch_pids(Unit *u) { |
2851 | pid_t except1, except2; | |
a911bb9a LP |
2852 | void *e; |
2853 | ||
2854 | assert(u); | |
2855 | ||
2856 | /* Cleans dead PIDs from our list */ | |
2857 | ||
50be4f4a LP |
2858 | except1 = unit_main_pid(u); |
2859 | except2 = unit_control_pid(u); | |
2860 | ||
90e74a66 | 2861 | SET_FOREACH(e, u->pids) { |
fea72cc0 | 2862 | pid_t pid = PTR_TO_PID(e); |
a911bb9a LP |
2863 | |
2864 | if (pid == except1 || pid == except2) | |
2865 | continue; | |
2866 | ||
9f5650ae | 2867 | if (!pid_is_unwaited(pid)) |
bd44e61b | 2868 | unit_unwatch_pid(u, pid); |
a911bb9a | 2869 | } |
87f0e418 LP |
2870 | } |
2871 | ||
50be4f4a LP |
2872 | static int on_rewatch_pids_event(sd_event_source *s, void *userdata) { |
2873 | Unit *u = userdata; | |
2874 | ||
2875 | assert(s); | |
2876 | assert(u); | |
2877 | ||
2878 | unit_tidy_watch_pids(u); | |
2879 | unit_watch_all_pids(u); | |
2880 | ||
2881 | /* If the PID set is empty now, then let's finish this off. */ | |
2882 | unit_synthesize_cgroup_empty_event(u); | |
2883 | ||
2884 | return 0; | |
2885 | } | |
2886 | ||
2887 | int unit_enqueue_rewatch_pids(Unit *u) { | |
2888 | int r; | |
2889 | ||
2890 | assert(u); | |
2891 | ||
2892 | if (!u->cgroup_path) | |
2893 | return -ENOENT; | |
2894 | ||
2895 | r = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER); | |
2896 | if (r < 0) | |
2897 | return r; | |
2898 | if (r > 0) /* On unified we can use proper notifications */ | |
2899 | return 0; | |
2900 | ||
2901 | /* Enqueues a low-priority job that will clean up dead PIDs from our list of PIDs to watch and subscribe to new | |
2902 | * PIDs that might have appeared. We do this in a delayed job because the work might be quite slow, as it | |
2903 | * involves issuing kill(pid, 0) on all processes we watch. */ | |
2904 | ||
2905 | if (!u->rewatch_pids_event_source) { | |
2906 | _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; | |
2907 | ||
2908 | r = sd_event_add_defer(u->manager->event, &s, on_rewatch_pids_event, u); | |
2909 | if (r < 0) | |
2910 | return log_error_errno(r, "Failed to allocate event source for tidying watched PIDs: %m"); | |
2911 | ||
2912 | r = sd_event_source_set_priority(s, SD_EVENT_PRIORITY_IDLE); | |
2913 | if (r < 0) | |
28b77ab2 | 2914 | return log_error_errno(r, "Failed to adjust priority of event source for tidying watched PIDs: %m"); |
50be4f4a LP |
2915 | |
2916 | (void) sd_event_source_set_description(s, "tidy-watch-pids"); | |
2917 | ||
2918 | u->rewatch_pids_event_source = TAKE_PTR(s); | |
2919 | } | |
2920 | ||
2921 | r = sd_event_source_set_enabled(u->rewatch_pids_event_source, SD_EVENT_ONESHOT); | |
2922 | if (r < 0) | |
2923 | return log_error_errno(r, "Failed to enable event source for tidying watched PIDs: %m"); | |
2924 | ||
2925 | return 0; | |
2926 | } | |
2927 | ||
2928 | void unit_dequeue_rewatch_pids(Unit *u) { | |
2929 | int r; | |
2930 | assert(u); | |
2931 | ||
2932 | if (!u->rewatch_pids_event_source) | |
2933 | return; | |
2934 | ||
2935 | r = sd_event_source_set_enabled(u->rewatch_pids_event_source, SD_EVENT_OFF); | |
2936 | if (r < 0) | |
2937 | log_warning_errno(r, "Failed to disable event source for tidying watched PIDs, ignoring: %m"); | |
2938 | ||
2939 | u->rewatch_pids_event_source = sd_event_source_unref(u->rewatch_pids_event_source); | |
2940 | } | |
2941 | ||
87f0e418 LP |
2942 | bool unit_job_is_applicable(Unit *u, JobType j) { |
2943 | assert(u); | |
2944 | assert(j >= 0 && j < _JOB_TYPE_MAX); | |
2945 | ||
2946 | switch (j) { | |
2947 | ||
2948 | case JOB_VERIFY_ACTIVE: | |
2949 | case JOB_START: | |
e0209d83 | 2950 | case JOB_NOP: |
f5869324 | 2951 | /* Note that we don't check unit_can_start() here. That's because .device units and suchlike are not |
86b52a39 | 2952 | * startable by us but may appear due to external events, and it thus makes sense to permit enqueuing |
f5869324 | 2953 | * jobs for it. */ |
87f0e418 LP |
2954 | return true; |
2955 | ||
f5869324 LP |
2956 | case JOB_STOP: |
2957 | /* Similar as above. However, perpetual units can never be stopped (neither explicitly nor due to | |
86b52a39 | 2958 | * external events), hence it makes no sense to permit enqueuing such a request either. */ |
f5869324 LP |
2959 | return !u->perpetual; |
2960 | ||
87f0e418 LP |
2961 | case JOB_RESTART: |
2962 | case JOB_TRY_RESTART: | |
f5869324 | 2963 | return unit_can_stop(u) && unit_can_start(u); |
87f0e418 LP |
2964 | |
2965 | case JOB_RELOAD: | |
3282591d | 2966 | case JOB_TRY_RELOAD: |
87f0e418 LP |
2967 | return unit_can_reload(u); |
2968 | ||
2969 | case JOB_RELOAD_OR_START: | |
2970 | return unit_can_reload(u) && unit_can_start(u); | |
2971 | ||
2972 | default: | |
2973 | assert_not_reached("Invalid job type"); | |
2974 | } | |
2975 | } | |
2976 | ||
f2341e0a LP |
2977 | static void maybe_warn_about_dependency(Unit *u, const char *other, UnitDependency dependency) { |
2978 | assert(u); | |
d1fab3fe | 2979 | |
f2341e0a LP |
2980 | /* Only warn about some unit types */ |
2981 | if (!IN_SET(dependency, UNIT_CONFLICTS, UNIT_CONFLICTED_BY, UNIT_BEFORE, UNIT_AFTER, UNIT_ON_FAILURE, UNIT_TRIGGERS, UNIT_TRIGGERED_BY)) | |
2982 | return; | |
3f3cc397 | 2983 | |
f2341e0a LP |
2984 | if (streq_ptr(u->id, other)) |
2985 | log_unit_warning(u, "Dependency %s=%s dropped", unit_dependency_to_string(dependency), u->id); | |
2986 | else | |
2987 | log_unit_warning(u, "Dependency %s=%s dropped, merged into %s", unit_dependency_to_string(dependency), strna(other), u->id); | |
d1fab3fe ZJS |
2988 | } |
2989 | ||
eef85c4a LP |
2990 | static int unit_add_dependency_hashmap( |
2991 | Hashmap **h, | |
2992 | Unit *other, | |
2993 | UnitDependencyMask origin_mask, | |
2994 | UnitDependencyMask destination_mask) { | |
2995 | ||
2996 | UnitDependencyInfo info; | |
2997 | int r; | |
2998 | ||
2999 | assert(h); | |
3000 | assert(other); | |
3001 | assert(origin_mask < _UNIT_DEPENDENCY_MASK_FULL); | |
3002 | assert(destination_mask < _UNIT_DEPENDENCY_MASK_FULL); | |
3003 | assert(origin_mask > 0 || destination_mask > 0); | |
3004 | ||
3005 | r = hashmap_ensure_allocated(h, NULL); | |
3006 | if (r < 0) | |
3007 | return r; | |
3008 | ||
3009 | assert_cc(sizeof(void*) == sizeof(info)); | |
3010 | ||
3011 | info.data = hashmap_get(*h, other); | |
3012 | if (info.data) { | |
3013 | /* Entry already exists. Add in our mask. */ | |
3014 | ||
d94a24ca ZJS |
3015 | if (FLAGS_SET(origin_mask, info.origin_mask) && |
3016 | FLAGS_SET(destination_mask, info.destination_mask)) | |
eef85c4a LP |
3017 | return 0; /* NOP */ |
3018 | ||
3019 | info.origin_mask |= origin_mask; | |
3020 | info.destination_mask |= destination_mask; | |
3021 | ||
3022 | r = hashmap_update(*h, other, info.data); | |
3023 | } else { | |
3024 | info = (UnitDependencyInfo) { | |
3025 | .origin_mask = origin_mask, | |
3026 | .destination_mask = destination_mask, | |
3027 | }; | |
3028 | ||
3029 | r = hashmap_put(*h, other, info.data); | |
3030 | } | |
3031 | if (r < 0) | |
3032 | return r; | |
3033 | ||
3034 | return 1; | |
3035 | } | |
3036 | ||
3037 | int unit_add_dependency( | |
3038 | Unit *u, | |
3039 | UnitDependency d, | |
3040 | Unit *other, | |
3041 | bool add_reference, | |
3042 | UnitDependencyMask mask) { | |
87f0e418 LP |
3043 | |
3044 | static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = { | |
3045 | [UNIT_REQUIRES] = UNIT_REQUIRED_BY, | |
87f0e418 | 3046 | [UNIT_WANTS] = UNIT_WANTED_BY, |
be7d9ff7 | 3047 | [UNIT_REQUISITE] = UNIT_REQUISITE_OF, |
7f2cddae | 3048 | [UNIT_BINDS_TO] = UNIT_BOUND_BY, |
60649f17 | 3049 | [UNIT_PART_OF] = UNIT_CONSISTS_OF, |
be7d9ff7 | 3050 | [UNIT_REQUIRED_BY] = UNIT_REQUIRES, |
be7d9ff7 | 3051 | [UNIT_REQUISITE_OF] = UNIT_REQUISITE, |
be7d9ff7 | 3052 | [UNIT_WANTED_BY] = UNIT_WANTS, |
7f2cddae | 3053 | [UNIT_BOUND_BY] = UNIT_BINDS_TO, |
60649f17 | 3054 | [UNIT_CONSISTS_OF] = UNIT_PART_OF, |
69dd2852 LP |
3055 | [UNIT_CONFLICTS] = UNIT_CONFLICTED_BY, |
3056 | [UNIT_CONFLICTED_BY] = UNIT_CONFLICTS, | |
87f0e418 | 3057 | [UNIT_BEFORE] = UNIT_AFTER, |
701cc384 | 3058 | [UNIT_AFTER] = UNIT_BEFORE, |
5de9682c | 3059 | [UNIT_ON_FAILURE] = _UNIT_DEPENDENCY_INVALID, |
701cc384 | 3060 | [UNIT_REFERENCES] = UNIT_REFERENCED_BY, |
57020a3a LP |
3061 | [UNIT_REFERENCED_BY] = UNIT_REFERENCES, |
3062 | [UNIT_TRIGGERS] = UNIT_TRIGGERED_BY, | |
4dcc1cb4 | 3063 | [UNIT_TRIGGERED_BY] = UNIT_TRIGGERS, |
7f2cddae | 3064 | [UNIT_PROPAGATES_RELOAD_TO] = UNIT_RELOAD_PROPAGATED_FROM, |
85e9a101 | 3065 | [UNIT_RELOAD_PROPAGATED_FROM] = UNIT_PROPAGATES_RELOAD_TO, |
613b411c | 3066 | [UNIT_JOINS_NAMESPACE_OF] = UNIT_JOINS_NAMESPACE_OF, |
87f0e418 | 3067 | }; |
eef85c4a LP |
3068 | Unit *original_u = u, *original_other = other; |
3069 | int r; | |
5177cb0a RM |
3070 | /* Helper to know whether sending a notification is necessary or not: |
3071 | * if the dependency is already there, no need to notify! */ | |
3072 | bool noop = true; | |
87f0e418 LP |
3073 | |
3074 | assert(u); | |
3075 | assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX); | |
87f0e418 LP |
3076 | assert(other); |
3077 | ||
9f151f29 LP |
3078 | u = unit_follow_merge(u); |
3079 | other = unit_follow_merge(other); | |
3080 | ||
87f0e418 LP |
3081 | /* We won't allow dependencies on ourselves. We will not |
3082 | * consider them an error however. */ | |
d1fab3fe | 3083 | if (u == other) { |
eef85c4a | 3084 | maybe_warn_about_dependency(original_u, original_other->id, d); |
87f0e418 | 3085 | return 0; |
d1fab3fe | 3086 | } |
87f0e418 | 3087 | |
b862c257 FB |
3088 | /* Note that ordering a device unit after a unit is permitted since it |
3089 | * allows to start its job running timeout at a specific time. */ | |
3090 | if (d == UNIT_BEFORE && other->type == UNIT_DEVICE) { | |
3091 | log_unit_warning(u, "Dependency Before=%s ignored (.device units cannot be delayed)", other->id); | |
c80a9a33 LP |
3092 | return 0; |
3093 | } | |
3094 | ||
3095 | if (d == UNIT_ON_FAILURE && !UNIT_VTABLE(u)->can_fail) { | |
3096 | log_unit_warning(u, "Requested dependency OnFailure=%s ignored (%s units cannot fail).", other->id, unit_type_to_string(u->type)); | |
3097 | return 0; | |
3098 | } | |
3099 | ||
3100 | if (d == UNIT_TRIGGERS && !UNIT_VTABLE(u)->can_trigger) | |
3101 | return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL), | |
3102 | "Requested dependency Triggers=%s refused (%s units cannot trigger other units).", other->id, unit_type_to_string(u->type)); | |
3103 | if (d == UNIT_TRIGGERED_BY && !UNIT_VTABLE(other)->can_trigger) | |
3104 | return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL), | |
3105 | "Requested dependency TriggeredBy=%s refused (%s units cannot trigger other units).", other->id, unit_type_to_string(other->type)); | |
3106 | ||
eef85c4a | 3107 | r = unit_add_dependency_hashmap(u->dependencies + d, other, mask, 0); |
613b411c | 3108 | if (r < 0) |
87f0e418 | 3109 | return r; |
5177cb0a RM |
3110 | else if (r > 0) |
3111 | noop = false; | |
87f0e418 | 3112 | |
eef85c4a LP |
3113 | if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID && inverse_table[d] != d) { |
3114 | r = unit_add_dependency_hashmap(other->dependencies + inverse_table[d], u, 0, mask); | |
613b411c LP |
3115 | if (r < 0) |
3116 | return r; | |
5177cb0a RM |
3117 | else if (r > 0) |
3118 | noop = false; | |
613b411c LP |
3119 | } |
3120 | ||
3121 | if (add_reference) { | |
eef85c4a | 3122 | r = unit_add_dependency_hashmap(u->dependencies + UNIT_REFERENCES, other, mask, 0); |
613b411c | 3123 | if (r < 0) |
5de9682c | 3124 | return r; |
5177cb0a RM |
3125 | else if (r > 0) |
3126 | noop = false; | |
5de9682c | 3127 | |
eef85c4a | 3128 | r = unit_add_dependency_hashmap(other->dependencies + UNIT_REFERENCED_BY, u, 0, mask); |
613b411c | 3129 | if (r < 0) |
701cc384 | 3130 | return r; |
5177cb0a RM |
3131 | else if (r > 0) |
3132 | noop = false; | |
613b411c | 3133 | } |
87f0e418 | 3134 | |
5177cb0a RM |
3135 | if (!noop) |
3136 | unit_add_to_dbus_queue(u); | |
87f0e418 LP |
3137 | return 0; |
3138 | } | |
0301abf4 | 3139 | |
eef85c4a | 3140 | int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference, UnitDependencyMask mask) { |
2c966c03 LP |
3141 | int r; |
3142 | ||
3143 | assert(u); | |
3144 | ||
eef85c4a | 3145 | r = unit_add_dependency(u, d, other, add_reference, mask); |
3f3cc397 | 3146 | if (r < 0) |
2c966c03 LP |
3147 | return r; |
3148 | ||
eef85c4a | 3149 | return unit_add_dependency(u, e, other, add_reference, mask); |
2c966c03 LP |
3150 | } |
3151 | ||
23e8c796 | 3152 | static int resolve_template(Unit *u, const char *name, char **buf, const char **ret) { |
7410616c | 3153 | int r; |
9e2f7c11 LP |
3154 | |
3155 | assert(u); | |
23e8c796 | 3156 | assert(name); |
7410616c LP |
3157 | assert(buf); |
3158 | assert(ret); | |
9e2f7c11 | 3159 | |
7410616c LP |
3160 | if (!unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
3161 | *buf = NULL; | |
3162 | *ret = name; | |
3163 | return 0; | |
9e2f7c11 LP |
3164 | } |
3165 | ||
ac155bb8 | 3166 | if (u->instance) |
7410616c | 3167 | r = unit_name_replace_instance(name, u->instance, buf); |
9e2f7c11 | 3168 | else { |
ae018d9b | 3169 | _cleanup_free_ char *i = NULL; |
9e2f7c11 | 3170 | |
7410616c LP |
3171 | r = unit_name_to_prefix(u->id, &i); |
3172 | if (r < 0) | |
3173 | return r; | |
9e2f7c11 | 3174 | |
7410616c | 3175 | r = unit_name_replace_instance(name, i, buf); |
9e2f7c11 | 3176 | } |
7410616c LP |
3177 | if (r < 0) |
3178 | return r; | |
9e2f7c11 | 3179 | |
7410616c LP |
3180 | *ret = *buf; |
3181 | return 0; | |
9e2f7c11 LP |
3182 | } |
3183 | ||
35d8c19a | 3184 | int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, bool add_reference, UnitDependencyMask mask) { |
7410616c | 3185 | _cleanup_free_ char *buf = NULL; |
09b6b09f LP |
3186 | Unit *other; |
3187 | int r; | |
3188 | ||
9e2f7c11 | 3189 | assert(u); |
35d8c19a | 3190 | assert(name); |
09b6b09f | 3191 | |
23e8c796 | 3192 | r = resolve_template(u, name, &buf, &name); |
7410616c LP |
3193 | if (r < 0) |
3194 | return r; | |
09b6b09f | 3195 | |
35d8c19a | 3196 | r = manager_load_unit(u->manager, name, NULL, NULL, &other); |
8afbb8e1 LP |
3197 | if (r < 0) |
3198 | return r; | |
9e2f7c11 | 3199 | |
eef85c4a | 3200 | return unit_add_dependency(u, d, other, add_reference, mask); |
09b6b09f LP |
3201 | } |
3202 | ||
5a724170 | 3203 | int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, bool add_reference, UnitDependencyMask mask) { |
7410616c | 3204 | _cleanup_free_ char *buf = NULL; |
2c966c03 LP |
3205 | Unit *other; |
3206 | int r; | |
2c966c03 LP |
3207 | |
3208 | assert(u); | |
5a724170 | 3209 | assert(name); |
2c966c03 | 3210 | |
23e8c796 | 3211 | r = resolve_template(u, name, &buf, &name); |
7410616c LP |
3212 | if (r < 0) |
3213 | return r; | |
2c966c03 | 3214 | |
5a724170 | 3215 | r = manager_load_unit(u->manager, name, NULL, NULL, &other); |
3f3cc397 | 3216 | if (r < 0) |
68eda4bd | 3217 | return r; |
2c966c03 | 3218 | |
eef85c4a | 3219 | return unit_add_two_dependencies(u, d, e, other, add_reference, mask); |
2c966c03 LP |
3220 | } |
3221 | ||
0301abf4 | 3222 | int set_unit_path(const char *p) { |
0301abf4 | 3223 | /* This is mostly for debug purposes */ |
cbe46ead | 3224 | if (setenv("SYSTEMD_UNIT_PATH", p, 1) < 0) |
26d04f86 | 3225 | return -errno; |
0301abf4 LP |
3226 | |
3227 | return 0; | |
3228 | } | |
88066b3a | 3229 | |
ea430986 | 3230 | char *unit_dbus_path(Unit *u) { |
ea430986 LP |
3231 | assert(u); |
3232 | ||
ac155bb8 | 3233 | if (!u->id) |
04ade7d2 LP |
3234 | return NULL; |
3235 | ||
48899192 | 3236 | return unit_dbus_path_from_name(u->id); |
ea430986 LP |
3237 | } |
3238 | ||
4b58153d LP |
3239 | char *unit_dbus_path_invocation_id(Unit *u) { |
3240 | assert(u); | |
3241 | ||
3242 | if (sd_id128_is_null(u->invocation_id)) | |
3243 | return NULL; | |
3244 | ||
3245 | return unit_dbus_path_from_name(u->invocation_id_string); | |
3246 | } | |
3247 | ||
db868d45 ZJS |
3248 | static int unit_set_invocation_id(Unit *u, sd_id128_t id) { |
3249 | int r; | |
3250 | ||
3251 | assert(u); | |
3252 | ||
3253 | /* Set the invocation ID for this unit. If we cannot, this will not roll back, but reset the whole thing. */ | |
3254 | ||
3255 | if (sd_id128_equal(u->invocation_id, id)) | |
3256 | return 0; | |
3257 | ||
3258 | if (!sd_id128_is_null(u->invocation_id)) | |
3259 | (void) hashmap_remove_value(u->manager->units_by_invocation_id, &u->invocation_id, u); | |
3260 | ||
3261 | if (sd_id128_is_null(id)) { | |
3262 | r = 0; | |
3263 | goto reset; | |
3264 | } | |
3265 | ||
3266 | r = hashmap_ensure_allocated(&u->manager->units_by_invocation_id, &id128_hash_ops); | |
3267 | if (r < 0) | |
3268 | goto reset; | |
3269 | ||
3270 | u->invocation_id = id; | |
3271 | sd_id128_to_string(id, u->invocation_id_string); | |
3272 | ||
3273 | r = hashmap_put(u->manager->units_by_invocation_id, &u->invocation_id, u); | |
3274 | if (r < 0) | |
3275 | goto reset; | |
3276 | ||
3277 | return 0; | |
3278 | ||
3279 | reset: | |
3280 | u->invocation_id = SD_ID128_NULL; | |
3281 | u->invocation_id_string[0] = 0; | |
3282 | return r; | |
3283 | } | |
3284 | ||
d79200e2 LP |
3285 | int unit_set_slice(Unit *u, Unit *slice) { |
3286 | assert(u); | |
3287 | assert(slice); | |
3288 | ||
3289 | /* Sets the unit slice if it has not been set before. Is extra | |
3290 | * careful, to only allow this for units that actually have a | |
3291 | * cgroup context. Also, we don't allow to set this for slices | |
3292 | * (since the parent slice is derived from the name). Make | |
3293 | * sure the unit we set is actually a slice. */ | |
3294 | ||
3295 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) | |
3296 | return -EOPNOTSUPP; | |
3297 | ||
3298 | if (u->type == UNIT_SLICE) | |
3299 | return -EINVAL; | |
3300 | ||
102ef982 LP |
3301 | if (unit_active_state(u) != UNIT_INACTIVE) |
3302 | return -EBUSY; | |
3303 | ||
d79200e2 LP |
3304 | if (slice->type != UNIT_SLICE) |
3305 | return -EINVAL; | |
3306 | ||
efdb0237 LP |
3307 | if (unit_has_name(u, SPECIAL_INIT_SCOPE) && |
3308 | !unit_has_name(slice, SPECIAL_ROOT_SLICE)) | |
3309 | return -EPERM; | |
3310 | ||
d79200e2 LP |
3311 | if (UNIT_DEREF(u->slice) == slice) |
3312 | return 0; | |
3313 | ||
99e66921 TH |
3314 | /* Disallow slice changes if @u is already bound to cgroups */ |
3315 | if (UNIT_ISSET(u->slice) && u->cgroup_realized) | |
d79200e2 LP |
3316 | return -EBUSY; |
3317 | ||
7f7d01ed | 3318 | unit_ref_set(&u->slice, u, slice); |
d79200e2 LP |
3319 | return 1; |
3320 | } | |
3321 | ||
3322 | int unit_set_default_slice(Unit *u) { | |
a8833944 | 3323 | const char *slice_name; |
a016b922 LP |
3324 | Unit *slice; |
3325 | int r; | |
3326 | ||
3327 | assert(u); | |
3328 | ||
9444b1f2 | 3329 | if (UNIT_ISSET(u->slice)) |
a016b922 LP |
3330 | return 0; |
3331 | ||
a8833944 LP |
3332 | if (u->instance) { |
3333 | _cleanup_free_ char *prefix = NULL, *escaped = NULL; | |
68eda4bd | 3334 | |
a8833944 LP |
3335 | /* Implicitly place all instantiated units in their |
3336 | * own per-template slice */ | |
3337 | ||
7410616c LP |
3338 | r = unit_name_to_prefix(u->id, &prefix); |
3339 | if (r < 0) | |
3340 | return r; | |
a8833944 LP |
3341 | |
3342 | /* The prefix is already escaped, but it might include | |
3343 | * "-" which has a special meaning for slice units, | |
3344 | * hence escape it here extra. */ | |
7410616c | 3345 | escaped = unit_name_escape(prefix); |
a8833944 LP |
3346 | if (!escaped) |
3347 | return -ENOMEM; | |
3348 | ||
463d0d15 | 3349 | if (MANAGER_IS_SYSTEM(u->manager)) |
00e7b3c8 | 3350 | slice_name = strjoina("system-", escaped, ".slice"); |
a8833944 | 3351 | else |
7f3b86a4 | 3352 | slice_name = strjoina("app-", escaped, ".slice"); |
5ee24fa0 ZJS |
3353 | |
3354 | } else if (unit_is_extrinsic(u)) | |
3355 | /* Keep all extrinsic units (e.g. perpetual units and swap and mount units in user mode) in | |
3356 | * the root slice. They don't really belong in one of the subslices. */ | |
3357 | slice_name = SPECIAL_ROOT_SLICE; | |
3358 | ||
3359 | else if (MANAGER_IS_SYSTEM(u->manager)) | |
3360 | slice_name = SPECIAL_SYSTEM_SLICE; | |
3361 | else | |
3362 | slice_name = SPECIAL_APP_SLICE; | |
a8833944 LP |
3363 | |
3364 | r = manager_load_unit(u->manager, slice_name, NULL, NULL, &slice); | |
a016b922 LP |
3365 | if (r < 0) |
3366 | return r; | |
3367 | ||
d79200e2 | 3368 | return unit_set_slice(u, slice); |
a016b922 LP |
3369 | } |
3370 | ||
9444b1f2 LP |
3371 | const char *unit_slice_name(Unit *u) { |
3372 | assert(u); | |
3373 | ||
3374 | if (!UNIT_ISSET(u->slice)) | |
3375 | return NULL; | |
3376 | ||
3377 | return UNIT_DEREF(u->slice)->id; | |
3378 | } | |
3379 | ||
f6ff8c29 | 3380 | int unit_load_related_unit(Unit *u, const char *type, Unit **_found) { |
78edb35a | 3381 | _cleanup_free_ char *t = NULL; |
f6ff8c29 LP |
3382 | int r; |
3383 | ||
3384 | assert(u); | |
3385 | assert(type); | |
3386 | assert(_found); | |
3387 | ||
7410616c LP |
3388 | r = unit_name_change_suffix(u->id, type, &t); |
3389 | if (r < 0) | |
3390 | return r; | |
3391 | if (unit_has_name(u, t)) | |
3392 | return -EINVAL; | |
f6ff8c29 | 3393 | |
ac155bb8 | 3394 | r = manager_load_unit(u->manager, t, NULL, NULL, _found); |
9e2f7c11 | 3395 | assert(r < 0 || *_found != u); |
f6ff8c29 LP |
3396 | return r; |
3397 | } | |
3398 | ||
bbc29086 | 3399 | static int signal_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
fc67a943 | 3400 | const char *new_owner; |
bbc29086 DM |
3401 | Unit *u = userdata; |
3402 | int r; | |
3403 | ||
3404 | assert(message); | |
3405 | assert(u); | |
3406 | ||
fc67a943 | 3407 | r = sd_bus_message_read(message, "sss", NULL, NULL, &new_owner); |
bbc29086 DM |
3408 | if (r < 0) { |
3409 | bus_log_parse_error(r); | |
3410 | return 0; | |
3411 | } | |
3412 | ||
3413 | if (UNIT_VTABLE(u)->bus_name_owner_change) | |
fc67a943 | 3414 | UNIT_VTABLE(u)->bus_name_owner_change(u, empty_to_null(new_owner)); |
a5a8776a MJ |
3415 | |
3416 | return 0; | |
3417 | } | |
3418 | ||
3419 | static int get_name_owner_handler(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
3420 | const sd_bus_error *e; | |
3421 | const char *new_owner; | |
3422 | Unit *u = userdata; | |
3423 | int r; | |
3424 | ||
3425 | assert(message); | |
3426 | assert(u); | |
3427 | ||
3428 | u->get_name_owner_slot = sd_bus_slot_unref(u->get_name_owner_slot); | |
3429 | ||
a5a8776a | 3430 | e = sd_bus_message_get_error(message); |
a5a8776a | 3431 | if (e) { |
a54654ba LP |
3432 | if (!sd_bus_error_has_name(e, "org.freedesktop.DBus.Error.NameHasNoOwner")) |
3433 | log_unit_error(u, "Unexpected error response from GetNameOwner(): %s", e->message); | |
a5a8776a | 3434 | |
fc67a943 LP |
3435 | new_owner = NULL; |
3436 | } else { | |
3437 | r = sd_bus_message_read(message, "s", &new_owner); | |
3438 | if (r < 0) | |
3439 | return bus_log_parse_error(r); | |
a5a8776a | 3440 | |
fc67a943 LP |
3441 | assert(!isempty(new_owner)); |
3442 | } | |
a5a8776a MJ |
3443 | |
3444 | if (UNIT_VTABLE(u)->bus_name_owner_change) | |
fc67a943 | 3445 | UNIT_VTABLE(u)->bus_name_owner_change(u, new_owner); |
bbc29086 DM |
3446 | |
3447 | return 0; | |
3448 | } | |
3449 | ||
9806e87d LP |
3450 | int unit_install_bus_match(Unit *u, sd_bus *bus, const char *name) { |
3451 | const char *match; | |
a5b07847 | 3452 | int r; |
bbc29086 | 3453 | |
9806e87d LP |
3454 | assert(u); |
3455 | assert(bus); | |
3456 | assert(name); | |
bbc29086 | 3457 | |
a5b07847 | 3458 | if (u->match_bus_slot || u->get_name_owner_slot) |
bbc29086 DM |
3459 | return -EBUSY; |
3460 | ||
9806e87d | 3461 | match = strjoina("type='signal'," |
81d62103 ZJS |
3462 | "sender='org.freedesktop.DBus'," |
3463 | "path='/org/freedesktop/DBus'," | |
3464 | "interface='org.freedesktop.DBus'," | |
3465 | "member='NameOwnerChanged'," | |
3466 | "arg0='", name, "'"); | |
bbc29086 | 3467 | |
a5b07847 | 3468 | r = sd_bus_add_match_async(bus, &u->match_bus_slot, match, signal_name_owner_changed, NULL, u); |
a5a8776a MJ |
3469 | if (r < 0) |
3470 | return r; | |
3471 | ||
a5b07847 LP |
3472 | r = sd_bus_call_method_async( |
3473 | bus, | |
3474 | &u->get_name_owner_slot, | |
3475 | "org.freedesktop.DBus", | |
3476 | "/org/freedesktop/DBus", | |
3477 | "org.freedesktop.DBus", | |
3478 | "GetNameOwner", | |
3479 | get_name_owner_handler, | |
3480 | u, | |
3481 | "s", name); | |
3482 | if (r < 0) { | |
3483 | u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot); | |
3484 | return r; | |
3485 | } | |
3486 | ||
3487 | log_unit_debug(u, "Watching D-Bus name '%s'.", name); | |
3488 | return 0; | |
bbc29086 DM |
3489 | } |
3490 | ||
05e343b7 | 3491 | int unit_watch_bus_name(Unit *u, const char *name) { |
bbc29086 DM |
3492 | int r; |
3493 | ||
05e343b7 LP |
3494 | assert(u); |
3495 | assert(name); | |
3496 | ||
3497 | /* Watch a specific name on the bus. We only support one unit | |
3498 | * watching each name for now. */ | |
3499 | ||
bbc29086 DM |
3500 | if (u->manager->api_bus) { |
3501 | /* If the bus is already available, install the match directly. | |
3502 | * Otherwise, just put the name in the list. bus_setup_api() will take care later. */ | |
9806e87d | 3503 | r = unit_install_bus_match(u, u->manager->api_bus, name); |
bbc29086 | 3504 | if (r < 0) |
8ea823b6 | 3505 | return log_warning_errno(r, "Failed to subscribe to NameOwnerChanged signal for '%s': %m", name); |
bbc29086 DM |
3506 | } |
3507 | ||
3508 | r = hashmap_put(u->manager->watch_bus, name, u); | |
3509 | if (r < 0) { | |
3510 | u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot); | |
a5b07847 | 3511 | u->get_name_owner_slot = sd_bus_slot_unref(u->get_name_owner_slot); |
bbc29086 DM |
3512 | return log_warning_errno(r, "Failed to put bus name to hashmap: %m"); |
3513 | } | |
3514 | ||
3515 | return 0; | |
05e343b7 LP |
3516 | } |
3517 | ||
3518 | void unit_unwatch_bus_name(Unit *u, const char *name) { | |
3519 | assert(u); | |
3520 | assert(name); | |
3521 | ||
8367fea5 | 3522 | (void) hashmap_remove_value(u->manager->watch_bus, name, u); |
bbc29086 | 3523 | u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot); |
a5a8776a | 3524 | u->get_name_owner_slot = sd_bus_slot_unref(u->get_name_owner_slot); |
05e343b7 LP |
3525 | } |
3526 | ||
a16e1123 LP |
3527 | bool unit_can_serialize(Unit *u) { |
3528 | assert(u); | |
3529 | ||
3530 | return UNIT_VTABLE(u)->serialize && UNIT_VTABLE(u)->deserialize_item; | |
3531 | } | |
3532 | ||
d68c645b | 3533 | static int serialize_cgroup_mask(FILE *f, const char *key, CGroupMask mask) { |
8b108bd0 | 3534 | _cleanup_free_ char *s = NULL; |
d68c645b | 3535 | int r; |
8b108bd0 FB |
3536 | |
3537 | assert(f); | |
3538 | assert(key); | |
3539 | ||
d68c645b LP |
3540 | if (mask == 0) |
3541 | return 0; | |
3542 | ||
3543 | r = cg_mask_to_string(mask, &s); | |
3544 | if (r < 0) | |
3545 | return log_error_errno(r, "Failed to format cgroup mask: %m"); | |
3546 | ||
3547 | return serialize_item(f, key, s); | |
8b108bd0 FB |
3548 | } |
3549 | ||
b82f71c7 | 3550 | static const char *const ip_accounting_metric_field[_CGROUP_IP_ACCOUNTING_METRIC_MAX] = { |
6b659ed8 LP |
3551 | [CGROUP_IP_INGRESS_BYTES] = "ip-accounting-ingress-bytes", |
3552 | [CGROUP_IP_INGRESS_PACKETS] = "ip-accounting-ingress-packets", | |
3553 | [CGROUP_IP_EGRESS_BYTES] = "ip-accounting-egress-bytes", | |
3554 | [CGROUP_IP_EGRESS_PACKETS] = "ip-accounting-egress-packets", | |
3555 | }; | |
3556 | ||
fbe14fc9 LP |
3557 | static const char *const io_accounting_metric_field_base[_CGROUP_IO_ACCOUNTING_METRIC_MAX] = { |
3558 | [CGROUP_IO_READ_BYTES] = "io-accounting-read-bytes-base", | |
3559 | [CGROUP_IO_WRITE_BYTES] = "io-accounting-write-bytes-base", | |
3560 | [CGROUP_IO_READ_OPERATIONS] = "io-accounting-read-operations-base", | |
3561 | [CGROUP_IO_WRITE_OPERATIONS] = "io-accounting-write-operations-base", | |
3562 | }; | |
3563 | ||
3564 | static const char *const io_accounting_metric_field_last[_CGROUP_IO_ACCOUNTING_METRIC_MAX] = { | |
3565 | [CGROUP_IO_READ_BYTES] = "io-accounting-read-bytes-last", | |
3566 | [CGROUP_IO_WRITE_BYTES] = "io-accounting-write-bytes-last", | |
3567 | [CGROUP_IO_READ_OPERATIONS] = "io-accounting-read-operations-last", | |
3568 | [CGROUP_IO_WRITE_OPERATIONS] = "io-accounting-write-operations-last", | |
3569 | }; | |
3570 | ||
6b78f9b4 | 3571 | int unit_serialize(Unit *u, FILE *f, FDSet *fds, bool serialize_jobs) { |
a16e1123 LP |
3572 | int r; |
3573 | ||
3574 | assert(u); | |
3575 | assert(f); | |
3576 | assert(fds); | |
3577 | ||
9bdb98c5 | 3578 | if (unit_can_serialize(u)) { |
9bdb98c5 | 3579 | r = UNIT_VTABLE(u)->serialize(u, f, fds); |
613b411c LP |
3580 | if (r < 0) |
3581 | return r; | |
e0209d83 MS |
3582 | } |
3583 | ||
d68c645b | 3584 | (void) serialize_dual_timestamp(f, "state-change-timestamp", &u->state_change_timestamp); |
a483fb59 | 3585 | |
d68c645b LP |
3586 | (void) serialize_dual_timestamp(f, "inactive-exit-timestamp", &u->inactive_exit_timestamp); |
3587 | (void) serialize_dual_timestamp(f, "active-enter-timestamp", &u->active_enter_timestamp); | |
3588 | (void) serialize_dual_timestamp(f, "active-exit-timestamp", &u->active_exit_timestamp); | |
3589 | (void) serialize_dual_timestamp(f, "inactive-enter-timestamp", &u->inactive_enter_timestamp); | |
a483fb59 | 3590 | |
d68c645b LP |
3591 | (void) serialize_dual_timestamp(f, "condition-timestamp", &u->condition_timestamp); |
3592 | (void) serialize_dual_timestamp(f, "assert-timestamp", &u->assert_timestamp); | |
2791a8f8 | 3593 | |
ac155bb8 | 3594 | if (dual_timestamp_is_set(&u->condition_timestamp)) |
d68c645b | 3595 | (void) serialize_bool(f, "condition-result", u->condition_result); |
10717a1a | 3596 | |
59fccdc5 | 3597 | if (dual_timestamp_is_set(&u->assert_timestamp)) |
d68c645b | 3598 | (void) serialize_bool(f, "assert-result", u->assert_result); |
59fccdc5 | 3599 | |
d68c645b LP |
3600 | (void) serialize_bool(f, "transient", u->transient); |
3601 | (void) serialize_bool(f, "in-audit", u->in_audit); | |
fe700f46 | 3602 | |
d68c645b LP |
3603 | (void) serialize_bool(f, "exported-invocation-id", u->exported_invocation_id); |
3604 | (void) serialize_bool(f, "exported-log-level-max", u->exported_log_level_max); | |
3605 | (void) serialize_bool(f, "exported-log-extra-fields", u->exported_log_extra_fields); | |
5ac1530e ZJS |
3606 | (void) serialize_bool(f, "exported-log-rate-limit-interval", u->exported_log_ratelimit_interval); |
3607 | (void) serialize_bool(f, "exported-log-rate-limit-burst", u->exported_log_ratelimit_burst); | |
0e699122 | 3608 | |
d68c645b | 3609 | (void) serialize_item_format(f, "cpu-usage-base", "%" PRIu64, u->cpu_usage_base); |
fe700f46 | 3610 | if (u->cpu_usage_last != NSEC_INFINITY) |
d68c645b | 3611 | (void) serialize_item_format(f, "cpu-usage-last", "%" PRIu64, u->cpu_usage_last); |
c2756a68 | 3612 | |
fe8d22fb AZ |
3613 | if (u->managed_oom_kill_last > 0) |
3614 | (void) serialize_item_format(f, "managed-oom-kill-last", "%" PRIu64, u->managed_oom_kill_last); | |
3615 | ||
afcfaa69 LP |
3616 | if (u->oom_kill_last > 0) |
3617 | (void) serialize_item_format(f, "oom-kill-last", "%" PRIu64, u->oom_kill_last); | |
3618 | ||
fbe14fc9 LP |
3619 | for (CGroupIOAccountingMetric im = 0; im < _CGROUP_IO_ACCOUNTING_METRIC_MAX; im++) { |
3620 | (void) serialize_item_format(f, io_accounting_metric_field_base[im], "%" PRIu64, u->io_accounting_base[im]); | |
3621 | ||
3622 | if (u->io_accounting_last[im] != UINT64_MAX) | |
3623 | (void) serialize_item_format(f, io_accounting_metric_field_last[im], "%" PRIu64, u->io_accounting_last[im]); | |
3624 | } | |
3625 | ||
c2756a68 | 3626 | if (u->cgroup_path) |
d68c645b LP |
3627 | (void) serialize_item(f, "cgroup", u->cgroup_path); |
3628 | ||
3629 | (void) serialize_bool(f, "cgroup-realized", u->cgroup_realized); | |
3630 | (void) serialize_cgroup_mask(f, "cgroup-realized-mask", u->cgroup_realized_mask); | |
3631 | (void) serialize_cgroup_mask(f, "cgroup-enabled-mask", u->cgroup_enabled_mask); | |
3632 | (void) serialize_cgroup_mask(f, "cgroup-invalidated-mask", u->cgroup_invalidated_mask); | |
c2756a68 | 3633 | |
00d9ef85 | 3634 | if (uid_is_valid(u->ref_uid)) |
d68c645b | 3635 | (void) serialize_item_format(f, "ref-uid", UID_FMT, u->ref_uid); |
00d9ef85 | 3636 | if (gid_is_valid(u->ref_gid)) |
d68c645b | 3637 | (void) serialize_item_format(f, "ref-gid", GID_FMT, u->ref_gid); |
00d9ef85 | 3638 | |
4b58153d | 3639 | if (!sd_id128_is_null(u->invocation_id)) |
d68c645b | 3640 | (void) serialize_item_format(f, "invocation-id", SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(u->invocation_id)); |
4b58153d | 3641 | |
d9e45bc3 MS |
3642 | (void) serialize_item_format(f, "freezer-state", "%s", freezer_state_to_string(unit_freezer_state(u))); |
3643 | ||
05a98afd LP |
3644 | bus_track_serialize(u->bus_track, f, "ref"); |
3645 | ||
12375b95 | 3646 | for (CGroupIPAccountingMetric m = 0; m < _CGROUP_IP_ACCOUNTING_METRIC_MAX; m++) { |
6b659ed8 LP |
3647 | uint64_t v; |
3648 | ||
3649 | r = unit_get_ip_accounting(u, m, &v); | |
3650 | if (r >= 0) | |
d68c645b | 3651 | (void) serialize_item_format(f, ip_accounting_metric_field[m], "%" PRIu64, v); |
6b659ed8 LP |
3652 | } |
3653 | ||
613b411c LP |
3654 | if (serialize_jobs) { |
3655 | if (u->job) { | |
d68c645b | 3656 | fputs("job\n", f); |
05a98afd | 3657 | job_serialize(u->job, f); |
613b411c LP |
3658 | } |
3659 | ||
3660 | if (u->nop_job) { | |
d68c645b | 3661 | fputs("job\n", f); |
05a98afd | 3662 | job_serialize(u->nop_job, f); |
613b411c LP |
3663 | } |
3664 | } | |
3665 | ||
a16e1123 LP |
3666 | /* End marker */ |
3667 | fputc('\n', f); | |
3668 | return 0; | |
3669 | } | |
3670 | ||
b17c9620 LP |
3671 | static int unit_deserialize_job(Unit *u, FILE *f) { |
3672 | _cleanup_(job_freep) Job *j = NULL; | |
3673 | int r; | |
3674 | ||
3675 | assert(u); | |
3676 | assert(f); | |
3677 | ||
3678 | j = job_new_raw(u); | |
3679 | if (!j) | |
3680 | return log_oom(); | |
3681 | ||
3682 | r = job_deserialize(j, f); | |
3683 | if (r < 0) | |
3684 | return r; | |
3685 | ||
3686 | r = job_install_deserialized(j); | |
3687 | if (r < 0) | |
3688 | return r; | |
3689 | ||
3690 | TAKE_PTR(j); | |
3691 | return 0; | |
3692 | } | |
3693 | ||
a16e1123 LP |
3694 | int unit_deserialize(Unit *u, FILE *f, FDSet *fds) { |
3695 | int r; | |
3696 | ||
3697 | assert(u); | |
3698 | assert(f); | |
3699 | assert(fds); | |
3700 | ||
a16e1123 | 3701 | for (;;) { |
8948b341 | 3702 | _cleanup_free_ char *line = NULL; |
8948b341 | 3703 | char *l, *v; |
83f18c91 | 3704 | ssize_t m; |
a16e1123 LP |
3705 | size_t k; |
3706 | ||
8948b341 LP |
3707 | r = read_line(f, LONG_LINE_MAX, &line); |
3708 | if (r < 0) | |
3709 | return log_error_errno(r, "Failed to read serialization line: %m"); | |
3710 | if (r == 0) /* eof */ | |
3711 | break; | |
a16e1123 LP |
3712 | |
3713 | l = strstrip(line); | |
8948b341 | 3714 | if (isempty(l)) /* End marker */ |
a483fb59 | 3715 | break; |
a16e1123 LP |
3716 | |
3717 | k = strcspn(l, "="); | |
3718 | ||
3719 | if (l[k] == '=') { | |
3720 | l[k] = 0; | |
3721 | v = l+k+1; | |
3722 | } else | |
3723 | v = l+k; | |
3724 | ||
cca098b0 | 3725 | if (streq(l, "job")) { |
39a18c60 | 3726 | if (v[0] == '\0') { |
b17c9620 LP |
3727 | /* New-style serialized job */ |
3728 | r = unit_deserialize_job(u, f); | |
3729 | if (r < 0) | |
e0209d83 | 3730 | return r; |
b17c9620 | 3731 | } else /* Legacy for pre-44 */ |
ed10fa8c | 3732 | log_unit_warning(u, "Update from too old systemd versions are unsupported, cannot deserialize job: %s", v); |
cca098b0 | 3733 | continue; |
a483fb59 | 3734 | } else if (streq(l, "state-change-timestamp")) { |
d68c645b | 3735 | (void) deserialize_dual_timestamp(v, &u->state_change_timestamp); |
a483fb59 | 3736 | continue; |
8aaf019b | 3737 | } else if (streq(l, "inactive-exit-timestamp")) { |
d68c645b | 3738 | (void) deserialize_dual_timestamp(v, &u->inactive_exit_timestamp); |
8aaf019b LP |
3739 | continue; |
3740 | } else if (streq(l, "active-enter-timestamp")) { | |
d68c645b | 3741 | (void) deserialize_dual_timestamp(v, &u->active_enter_timestamp); |
8aaf019b LP |
3742 | continue; |
3743 | } else if (streq(l, "active-exit-timestamp")) { | |
d68c645b | 3744 | (void) deserialize_dual_timestamp(v, &u->active_exit_timestamp); |
8aaf019b LP |
3745 | continue; |
3746 | } else if (streq(l, "inactive-enter-timestamp")) { | |
d68c645b | 3747 | (void) deserialize_dual_timestamp(v, &u->inactive_enter_timestamp); |
8aaf019b | 3748 | continue; |
2791a8f8 | 3749 | } else if (streq(l, "condition-timestamp")) { |
d68c645b | 3750 | (void) deserialize_dual_timestamp(v, &u->condition_timestamp); |
2791a8f8 | 3751 | continue; |
59fccdc5 | 3752 | } else if (streq(l, "assert-timestamp")) { |
d68c645b | 3753 | (void) deserialize_dual_timestamp(v, &u->assert_timestamp); |
59fccdc5 | 3754 | continue; |
2791a8f8 | 3755 | } else if (streq(l, "condition-result")) { |
2791a8f8 | 3756 | |
e911de99 LP |
3757 | r = parse_boolean(v); |
3758 | if (r < 0) | |
f2341e0a | 3759 | log_unit_debug(u, "Failed to parse condition result value %s, ignoring.", v); |
2791a8f8 | 3760 | else |
e911de99 | 3761 | u->condition_result = r; |
efbac6d2 LP |
3762 | |
3763 | continue; | |
c2756a68 | 3764 | |
59fccdc5 | 3765 | } else if (streq(l, "assert-result")) { |
59fccdc5 | 3766 | |
e911de99 LP |
3767 | r = parse_boolean(v); |
3768 | if (r < 0) | |
f2341e0a | 3769 | log_unit_debug(u, "Failed to parse assert result value %s, ignoring.", v); |
59fccdc5 | 3770 | else |
e911de99 | 3771 | u->assert_result = r; |
59fccdc5 LP |
3772 | |
3773 | continue; | |
3774 | ||
c2756a68 | 3775 | } else if (streq(l, "transient")) { |
c2756a68 | 3776 | |
e911de99 LP |
3777 | r = parse_boolean(v); |
3778 | if (r < 0) | |
f2341e0a | 3779 | log_unit_debug(u, "Failed to parse transient bool %s, ignoring.", v); |
c2756a68 | 3780 | else |
e911de99 | 3781 | u->transient = r; |
c2756a68 LP |
3782 | |
3783 | continue; | |
e911de99 | 3784 | |
0e699122 LP |
3785 | } else if (streq(l, "in-audit")) { |
3786 | ||
3787 | r = parse_boolean(v); | |
3788 | if (r < 0) | |
3789 | log_unit_debug(u, "Failed to parse in-audit bool %s, ignoring.", v); | |
3790 | else | |
3791 | u->in_audit = r; | |
3792 | ||
3793 | continue; | |
3794 | ||
d3070fbd LP |
3795 | } else if (streq(l, "exported-invocation-id")) { |
3796 | ||
3797 | r = parse_boolean(v); | |
3798 | if (r < 0) | |
3799 | log_unit_debug(u, "Failed to parse exported invocation ID bool %s, ignoring.", v); | |
3800 | else | |
3801 | u->exported_invocation_id = r; | |
3802 | ||
3803 | continue; | |
3804 | ||
3805 | } else if (streq(l, "exported-log-level-max")) { | |
3806 | ||
3807 | r = parse_boolean(v); | |
3808 | if (r < 0) | |
3809 | log_unit_debug(u, "Failed to parse exported log level max bool %s, ignoring.", v); | |
3810 | else | |
3811 | u->exported_log_level_max = r; | |
3812 | ||
3813 | continue; | |
3814 | ||
3815 | } else if (streq(l, "exported-log-extra-fields")) { | |
3816 | ||
3817 | r = parse_boolean(v); | |
3818 | if (r < 0) | |
3819 | log_unit_debug(u, "Failed to parse exported log extra fields bool %s, ignoring.", v); | |
3820 | else | |
3821 | u->exported_log_extra_fields = r; | |
3822 | ||
3823 | continue; | |
3824 | ||
90fc172e AZ |
3825 | } else if (streq(l, "exported-log-rate-limit-interval")) { |
3826 | ||
3827 | r = parse_boolean(v); | |
3828 | if (r < 0) | |
3829 | log_unit_debug(u, "Failed to parse exported log rate limit interval %s, ignoring.", v); | |
3830 | else | |
5ac1530e | 3831 | u->exported_log_ratelimit_interval = r; |
90fc172e AZ |
3832 | |
3833 | continue; | |
3834 | ||
3835 | } else if (streq(l, "exported-log-rate-limit-burst")) { | |
3836 | ||
3837 | r = parse_boolean(v); | |
3838 | if (r < 0) | |
3839 | log_unit_debug(u, "Failed to parse exported log rate limit burst %s, ignoring.", v); | |
3840 | else | |
5ac1530e | 3841 | u->exported_log_ratelimit_burst = r; |
90fc172e AZ |
3842 | |
3843 | continue; | |
3844 | ||
fe700f46 | 3845 | } else if (STR_IN_SET(l, "cpu-usage-base", "cpuacct-usage-base")) { |
5ad096b3 | 3846 | |
66ebf6c0 | 3847 | r = safe_atou64(v, &u->cpu_usage_base); |
5ad096b3 | 3848 | if (r < 0) |
fe700f46 LP |
3849 | log_unit_debug(u, "Failed to parse CPU usage base %s, ignoring.", v); |
3850 | ||
3851 | continue; | |
3852 | ||
3853 | } else if (streq(l, "cpu-usage-last")) { | |
3854 | ||
3855 | r = safe_atou64(v, &u->cpu_usage_last); | |
3856 | if (r < 0) | |
3857 | log_unit_debug(u, "Failed to read CPU usage last %s, ignoring.", v); | |
5ad096b3 | 3858 | |
0f908397 | 3859 | continue; |
4e595329 | 3860 | |
fe8d22fb AZ |
3861 | } else if (streq(l, "managed-oom-kill-last")) { |
3862 | ||
3863 | r = safe_atou64(v, &u->managed_oom_kill_last); | |
3864 | if (r < 0) | |
3865 | log_unit_debug(u, "Failed to read managed OOM kill last %s, ignoring.", v); | |
3866 | ||
3867 | continue; | |
3868 | ||
afcfaa69 LP |
3869 | } else if (streq(l, "oom-kill-last")) { |
3870 | ||
3871 | r = safe_atou64(v, &u->oom_kill_last); | |
3872 | if (r < 0) | |
3873 | log_unit_debug(u, "Failed to read OOM kill last %s, ignoring.", v); | |
3874 | ||
3875 | continue; | |
3876 | ||
e911de99 | 3877 | } else if (streq(l, "cgroup")) { |
72673e86 | 3878 | |
e911de99 LP |
3879 | r = unit_set_cgroup_path(u, v); |
3880 | if (r < 0) | |
f2341e0a | 3881 | log_unit_debug_errno(u, r, "Failed to set cgroup path %s, ignoring: %m", v); |
4e595329 | 3882 | |
efdb0237 | 3883 | (void) unit_watch_cgroup(u); |
afcfaa69 | 3884 | (void) unit_watch_cgroup_memory(u); |
efdb0237 | 3885 | |
de1d4f9b WF |
3886 | continue; |
3887 | } else if (streq(l, "cgroup-realized")) { | |
3888 | int b; | |
3889 | ||
3890 | b = parse_boolean(v); | |
3891 | if (b < 0) | |
3892 | log_unit_debug(u, "Failed to parse cgroup-realized bool %s, ignoring.", v); | |
3893 | else | |
3894 | u->cgroup_realized = b; | |
3895 | ||
c2756a68 | 3896 | continue; |
00d9ef85 | 3897 | |
8b108bd0 FB |
3898 | } else if (streq(l, "cgroup-realized-mask")) { |
3899 | ||
3900 | r = cg_mask_from_string(v, &u->cgroup_realized_mask); | |
3901 | if (r < 0) | |
3902 | log_unit_debug(u, "Failed to parse cgroup-realized-mask %s, ignoring.", v); | |
3903 | continue; | |
3904 | ||
3905 | } else if (streq(l, "cgroup-enabled-mask")) { | |
3906 | ||
3907 | r = cg_mask_from_string(v, &u->cgroup_enabled_mask); | |
3908 | if (r < 0) | |
3909 | log_unit_debug(u, "Failed to parse cgroup-enabled-mask %s, ignoring.", v); | |
3910 | continue; | |
3911 | ||
17f14955 | 3912 | } else if (streq(l, "cgroup-invalidated-mask")) { |
906c06f6 | 3913 | |
17f14955 | 3914 | r = cg_mask_from_string(v, &u->cgroup_invalidated_mask); |
906c06f6 | 3915 | if (r < 0) |
17f14955 | 3916 | log_unit_debug(u, "Failed to parse cgroup-invalidated-mask %s, ignoring.", v); |
906c06f6 DM |
3917 | continue; |
3918 | ||
00d9ef85 LP |
3919 | } else if (streq(l, "ref-uid")) { |
3920 | uid_t uid; | |
3921 | ||
3922 | r = parse_uid(v, &uid); | |
3923 | if (r < 0) | |
3924 | log_unit_debug(u, "Failed to parse referenced UID %s, ignoring.", v); | |
3925 | else | |
3926 | unit_ref_uid_gid(u, uid, GID_INVALID); | |
3927 | ||
3928 | continue; | |
3929 | ||
3930 | } else if (streq(l, "ref-gid")) { | |
3931 | gid_t gid; | |
3932 | ||
3933 | r = parse_gid(v, &gid); | |
3934 | if (r < 0) | |
3935 | log_unit_debug(u, "Failed to parse referenced GID %s, ignoring.", v); | |
3936 | else | |
3937 | unit_ref_uid_gid(u, UID_INVALID, gid); | |
3938 | ||
5f616d5f LP |
3939 | continue; |
3940 | ||
05a98afd LP |
3941 | } else if (streq(l, "ref")) { |
3942 | ||
3943 | r = strv_extend(&u->deserialized_refs, v); | |
3944 | if (r < 0) | |
d68c645b | 3945 | return log_oom(); |
05a98afd | 3946 | |
4b58153d LP |
3947 | continue; |
3948 | } else if (streq(l, "invocation-id")) { | |
3949 | sd_id128_t id; | |
3950 | ||
3951 | r = sd_id128_from_string(v, &id); | |
3952 | if (r < 0) | |
3953 | log_unit_debug(u, "Failed to parse invocation id %s, ignoring.", v); | |
3954 | else { | |
3955 | r = unit_set_invocation_id(u, id); | |
3956 | if (r < 0) | |
3957 | log_unit_warning_errno(u, r, "Failed to set invocation ID for unit: %m"); | |
3958 | } | |
3959 | ||
d9e45bc3 MS |
3960 | continue; |
3961 | } else if (streq(l, "freezer-state")) { | |
3962 | FreezerState s; | |
3963 | ||
3964 | s = freezer_state_from_string(v); | |
3965 | if (s < 0) | |
3966 | log_unit_debug(u, "Failed to deserialize freezer-state '%s', ignoring.", v); | |
3967 | else | |
3968 | u->freezer_state = s; | |
3969 | ||
00d9ef85 | 3970 | continue; |
8aaf019b | 3971 | } |
cca098b0 | 3972 | |
6b659ed8 | 3973 | /* Check if this is an IP accounting metric serialization field */ |
83f18c91 LP |
3974 | m = string_table_lookup(ip_accounting_metric_field, ELEMENTSOF(ip_accounting_metric_field), l); |
3975 | if (m >= 0) { | |
6b659ed8 LP |
3976 | uint64_t c; |
3977 | ||
3978 | r = safe_atou64(v, &c); | |
3979 | if (r < 0) | |
3980 | log_unit_debug(u, "Failed to parse IP accounting value %s, ignoring.", v); | |
3981 | else | |
3982 | u->ip_accounting_extra[m] = c; | |
3983 | continue; | |
3984 | } | |
3985 | ||
fbe14fc9 LP |
3986 | m = string_table_lookup(io_accounting_metric_field_base, ELEMENTSOF(io_accounting_metric_field_base), l); |
3987 | if (m >= 0) { | |
3988 | uint64_t c; | |
3989 | ||
3990 | r = safe_atou64(v, &c); | |
3991 | if (r < 0) | |
3992 | log_unit_debug(u, "Failed to parse IO accounting base value %s, ignoring.", v); | |
3993 | else | |
3994 | u->io_accounting_base[m] = c; | |
3995 | continue; | |
3996 | } | |
3997 | ||
3998 | m = string_table_lookup(io_accounting_metric_field_last, ELEMENTSOF(io_accounting_metric_field_last), l); | |
3999 | if (m >= 0) { | |
4000 | uint64_t c; | |
4001 | ||
4002 | r = safe_atou64(v, &c); | |
4003 | if (r < 0) | |
4004 | log_unit_debug(u, "Failed to parse IO accounting last value %s, ignoring.", v); | |
4005 | else | |
4006 | u->io_accounting_last[m] = c; | |
4007 | continue; | |
4008 | } | |
4009 | ||
9bdb98c5 | 4010 | if (unit_can_serialize(u)) { |
e8a565cb YW |
4011 | r = exec_runtime_deserialize_compat(u, l, v, fds); |
4012 | if (r < 0) { | |
4013 | log_unit_warning(u, "Failed to deserialize runtime parameter '%s', ignoring.", l); | |
4014 | continue; | |
9bdb98c5 LP |
4015 | } |
4016 | ||
e8a565cb YW |
4017 | /* Returns positive if key was handled by the call */ |
4018 | if (r > 0) | |
4019 | continue; | |
4020 | ||
9bdb98c5 | 4021 | r = UNIT_VTABLE(u)->deserialize_item(u, l, v, fds); |
613b411c | 4022 | if (r < 0) |
f2341e0a | 4023 | log_unit_warning(u, "Failed to deserialize unit parameter '%s', ignoring.", l); |
613b411c | 4024 | } |
a16e1123 | 4025 | } |
a483fb59 LP |
4026 | |
4027 | /* Versions before 228 did not carry a state change timestamp. In this case, take the current time. This is | |
4028 | * useful, so that timeouts based on this timestamp don't trigger too early, and is in-line with the logic from | |
1f133e0d | 4029 | * before 228 where the base for timeouts was not persistent across reboots. */ |
a483fb59 LP |
4030 | |
4031 | if (!dual_timestamp_is_set(&u->state_change_timestamp)) | |
4032 | dual_timestamp_get(&u->state_change_timestamp); | |
4033 | ||
58d83430 LP |
4034 | /* Let's make sure that everything that is deserialized also gets any potential new cgroup settings applied |
4035 | * after we are done. For that we invalidate anything already realized, so that we can realize it again. */ | |
4036 | unit_invalidate_cgroup(u, _CGROUP_MASK_ALL); | |
4037 | unit_invalidate_cgroup_bpf(u); | |
4038 | ||
a483fb59 | 4039 | return 0; |
a16e1123 LP |
4040 | } |
4041 | ||
8948b341 LP |
4042 | int unit_deserialize_skip(FILE *f) { |
4043 | int r; | |
07429866 ZJS |
4044 | assert(f); |
4045 | ||
4046 | /* Skip serialized data for this unit. We don't know what it is. */ | |
4047 | ||
4048 | for (;;) { | |
8948b341 LP |
4049 | _cleanup_free_ char *line = NULL; |
4050 | char *l; | |
07429866 | 4051 | |
8948b341 LP |
4052 | r = read_line(f, LONG_LINE_MAX, &line); |
4053 | if (r < 0) | |
4054 | return log_error_errno(r, "Failed to read serialization line: %m"); | |
4055 | if (r == 0) | |
4056 | return 0; | |
07429866 | 4057 | |
07429866 ZJS |
4058 | l = strstrip(line); |
4059 | ||
4060 | /* End marker */ | |
4061 | if (isempty(l)) | |
8948b341 | 4062 | return 1; |
07429866 ZJS |
4063 | } |
4064 | } | |
4065 | ||
d336ba9f | 4066 | int unit_add_node_dependency(Unit *u, const char *what, UnitDependency dep, UnitDependencyMask mask) { |
68eda4bd | 4067 | _cleanup_free_ char *e = NULL; |
44b0d1fd | 4068 | Unit *device; |
6e2ef85b LP |
4069 | int r; |
4070 | ||
4071 | assert(u); | |
4072 | ||
6e2ef85b | 4073 | /* Adds in links to the device node that this unit is based on */ |
47bc12e1 LP |
4074 | if (isempty(what)) |
4075 | return 0; | |
6e2ef85b | 4076 | |
8407a5d0 | 4077 | if (!is_device_path(what)) |
6e2ef85b LP |
4078 | return 0; |
4079 | ||
44b0d1fd | 4080 | /* When device units aren't supported (such as in a container), don't create dependencies on them. */ |
1c2e9646 | 4081 | if (!unit_type_supported(UNIT_DEVICE)) |
47bc12e1 LP |
4082 | return 0; |
4083 | ||
7410616c LP |
4084 | r = unit_name_from_path(what, ".device", &e); |
4085 | if (r < 0) | |
4086 | return r; | |
6e2ef85b | 4087 | |
ac155bb8 | 4088 | r = manager_load_unit(u->manager, e, NULL, NULL, &device); |
6e2ef85b LP |
4089 | if (r < 0) |
4090 | return r; | |
4091 | ||
ebc8968b FB |
4092 | if (dep == UNIT_REQUIRES && device_shall_be_bound_by(device, u)) |
4093 | dep = UNIT_BINDS_TO; | |
4094 | ||
d336ba9f FB |
4095 | return unit_add_two_dependencies(u, UNIT_AFTER, |
4096 | MANAGER_IS_SYSTEM(u->manager) ? dep : UNIT_WANTS, | |
4097 | device, true, mask); | |
6e2ef85b | 4098 | } |
a16e1123 | 4099 | |
44b0d1fd LP |
4100 | int unit_add_blockdev_dependency(Unit *u, const char *what, UnitDependencyMask mask) { |
4101 | _cleanup_free_ char *escaped = NULL, *target = NULL; | |
4102 | int r; | |
4103 | ||
4104 | assert(u); | |
4105 | ||
4106 | if (isempty(what)) | |
4107 | return 0; | |
4108 | ||
4109 | if (!path_startswith(what, "/dev/")) | |
4110 | return 0; | |
4111 | ||
4112 | /* If we don't support devices, then also don't bother with blockdev@.target */ | |
4113 | if (!unit_type_supported(UNIT_DEVICE)) | |
4114 | return 0; | |
4115 | ||
4116 | r = unit_name_path_escape(what, &escaped); | |
4117 | if (r < 0) | |
4118 | return r; | |
4119 | ||
4120 | r = unit_name_build("blockdev", escaped, ".target", &target); | |
4121 | if (r < 0) | |
4122 | return r; | |
4123 | ||
4124 | return unit_add_dependency_by_name(u, UNIT_AFTER, target, true, mask); | |
4125 | } | |
4126 | ||
be847e82 | 4127 | int unit_coldplug(Unit *u) { |
05a98afd LP |
4128 | int r = 0, q; |
4129 | char **i; | |
b49e14d5 | 4130 | Job *uj; |
cca098b0 LP |
4131 | |
4132 | assert(u); | |
4133 | ||
f0831ed2 | 4134 | /* Make sure we don't enter a loop, when coldplugging recursively. */ |
f78f265f LP |
4135 | if (u->coldplugged) |
4136 | return 0; | |
4137 | ||
4138 | u->coldplugged = true; | |
4139 | ||
05a98afd LP |
4140 | STRV_FOREACH(i, u->deserialized_refs) { |
4141 | q = bus_unit_track_add_name(u, *i); | |
4142 | if (q < 0 && r >= 0) | |
4143 | r = q; | |
4144 | } | |
4145 | u->deserialized_refs = strv_free(u->deserialized_refs); | |
cca098b0 | 4146 | |
05a98afd LP |
4147 | if (UNIT_VTABLE(u)->coldplug) { |
4148 | q = UNIT_VTABLE(u)->coldplug(u); | |
4149 | if (q < 0 && r >= 0) | |
4150 | r = q; | |
4151 | } | |
5a6158b6 | 4152 | |
b49e14d5 | 4153 | uj = u->job ?: u->nop_job; |
4154 | if (uj) { | |
4155 | q = job_coldplug(uj); | |
05a98afd LP |
4156 | if (q < 0 && r >= 0) |
4157 | r = q; | |
4158 | } | |
cca098b0 | 4159 | |
05a98afd | 4160 | return r; |
cca098b0 LP |
4161 | } |
4162 | ||
f0831ed2 LP |
4163 | void unit_catchup(Unit *u) { |
4164 | assert(u); | |
4165 | ||
4166 | if (UNIT_VTABLE(u)->catchup) | |
4167 | UNIT_VTABLE(u)->catchup(u); | |
4168 | } | |
4169 | ||
ba25d39e | 4170 | static bool fragment_mtime_newer(const char *path, usec_t mtime, bool path_masked) { |
21b95806 ZJS |
4171 | struct stat st; |
4172 | ||
4173 | if (!path) | |
4174 | return false; | |
4175 | ||
77969722 LP |
4176 | /* If the source is some virtual kernel file system, then we assume we watch it anyway, and hence pretend we |
4177 | * are never out-of-date. */ | |
4178 | if (PATH_STARTSWITH_SET(path, "/proc", "/sys")) | |
4179 | return false; | |
4180 | ||
21b95806 ZJS |
4181 | if (stat(path, &st) < 0) |
4182 | /* What, cannot access this anymore? */ | |
4183 | return true; | |
4184 | ||
ba25d39e ZJS |
4185 | if (path_masked) |
4186 | /* For masked files check if they are still so */ | |
4187 | return !null_or_empty(&st); | |
4188 | else | |
3a8db9fe | 4189 | /* For non-empty files check the mtime */ |
87ec20ef | 4190 | return timespec_load(&st.st_mtim) > mtime; |
21b95806 ZJS |
4191 | |
4192 | return false; | |
4193 | } | |
4194 | ||
45fb0699 | 4195 | bool unit_need_daemon_reload(Unit *u) { |
ae7a7182 OS |
4196 | _cleanup_strv_free_ char **t = NULL; |
4197 | char **path; | |
1b64d026 | 4198 | |
45fb0699 LP |
4199 | assert(u); |
4200 | ||
ba25d39e ZJS |
4201 | /* For unit files, we allow masking… */ |
4202 | if (fragment_mtime_newer(u->fragment_path, u->fragment_mtime, | |
4203 | u->load_state == UNIT_MASKED)) | |
21b95806 | 4204 | return true; |
5f4b19f4 | 4205 | |
ba25d39e ZJS |
4206 | /* Source paths should not be masked… */ |
4207 | if (fragment_mtime_newer(u->source_path, u->source_mtime, false)) | |
ab932a62 | 4208 | return true; |
ae7a7182 | 4209 | |
19a44dfe LR |
4210 | if (u->load_state == UNIT_LOADED) |
4211 | (void) unit_find_dropin_paths(u, &t); | |
ab932a62 LP |
4212 | if (!strv_equal(u->dropin_paths, t)) |
4213 | return true; | |
6d10d308 | 4214 | |
ba25d39e | 4215 | /* … any drop-ins that are masked are simply omitted from the list. */ |
ab932a62 | 4216 | STRV_FOREACH(path, u->dropin_paths) |
ba25d39e | 4217 | if (fragment_mtime_newer(*path, u->dropin_mtime, false)) |
ab932a62 | 4218 | return true; |
21b95806 | 4219 | |
ab932a62 | 4220 | return false; |
45fb0699 LP |
4221 | } |
4222 | ||
fdf20a31 | 4223 | void unit_reset_failed(Unit *u) { |
5632e374 LP |
4224 | assert(u); |
4225 | ||
fdf20a31 MM |
4226 | if (UNIT_VTABLE(u)->reset_failed) |
4227 | UNIT_VTABLE(u)->reset_failed(u); | |
6bf0f408 | 4228 | |
7bf081a1 | 4229 | ratelimit_reset(&u->start_ratelimit); |
6bf0f408 | 4230 | u->start_limit_hit = false; |
5632e374 LP |
4231 | } |
4232 | ||
a7f241db LP |
4233 | Unit *unit_following(Unit *u) { |
4234 | assert(u); | |
4235 | ||
4236 | if (UNIT_VTABLE(u)->following) | |
4237 | return UNIT_VTABLE(u)->following(u); | |
4238 | ||
4239 | return NULL; | |
4240 | } | |
4241 | ||
31afa0a4 | 4242 | bool unit_stop_pending(Unit *u) { |
18ffdfda LP |
4243 | assert(u); |
4244 | ||
31afa0a4 LP |
4245 | /* This call does check the current state of the unit. It's |
4246 | * hence useful to be called from state change calls of the | |
4247 | * unit itself, where the state isn't updated yet. This is | |
4248 | * different from unit_inactive_or_pending() which checks both | |
4249 | * the current state and for a queued job. */ | |
18ffdfda | 4250 | |
28a2dfe8 | 4251 | return unit_has_job_type(u, JOB_STOP); |
31afa0a4 LP |
4252 | } |
4253 | ||
4254 | bool unit_inactive_or_pending(Unit *u) { | |
4255 | assert(u); | |
4256 | ||
4257 | /* Returns true if the unit is inactive or going down */ | |
18ffdfda | 4258 | |
d956ac29 LP |
4259 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))) |
4260 | return true; | |
4261 | ||
31afa0a4 | 4262 | if (unit_stop_pending(u)) |
18ffdfda LP |
4263 | return true; |
4264 | ||
4265 | return false; | |
4266 | } | |
4267 | ||
31afa0a4 | 4268 | bool unit_active_or_pending(Unit *u) { |
f976f3f6 LP |
4269 | assert(u); |
4270 | ||
f60c2665 | 4271 | /* Returns true if the unit is active or going up */ |
f976f3f6 LP |
4272 | |
4273 | if (UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) | |
4274 | return true; | |
4275 | ||
ac155bb8 | 4276 | if (u->job && |
3742095b | 4277 | IN_SET(u->job->type, JOB_START, JOB_RELOAD_OR_START, JOB_RESTART)) |
f976f3f6 LP |
4278 | return true; |
4279 | ||
4280 | return false; | |
4281 | } | |
4282 | ||
52a12341 YW |
4283 | bool unit_will_restart_default(Unit *u) { |
4284 | assert(u); | |
4285 | ||
28a2dfe8 | 4286 | return unit_has_job_type(u, JOB_START); |
52a12341 YW |
4287 | } |
4288 | ||
deb4e708 MK |
4289 | bool unit_will_restart(Unit *u) { |
4290 | assert(u); | |
4291 | ||
4292 | if (!UNIT_VTABLE(u)->will_restart) | |
4293 | return false; | |
4294 | ||
4295 | return UNIT_VTABLE(u)->will_restart(u); | |
4296 | } | |
4297 | ||
718db961 | 4298 | int unit_kill(Unit *u, KillWho w, int signo, sd_bus_error *error) { |
8a0867d6 LP |
4299 | assert(u); |
4300 | assert(w >= 0 && w < _KILL_WHO_MAX); | |
6eb7c172 | 4301 | assert(SIGNAL_VALID(signo)); |
8a0867d6 | 4302 | |
8a0867d6 | 4303 | if (!UNIT_VTABLE(u)->kill) |
15411c0c | 4304 | return -EOPNOTSUPP; |
8a0867d6 | 4305 | |
c74f17d9 | 4306 | return UNIT_VTABLE(u)->kill(u, w, signo, error); |
8a0867d6 LP |
4307 | } |
4308 | ||
82659fd7 | 4309 | static Set *unit_pid_set(pid_t main_pid, pid_t control_pid) { |
af4fa99d | 4310 | _cleanup_set_free_ Set *pid_set = NULL; |
82659fd7 LP |
4311 | int r; |
4312 | ||
d5099efc | 4313 | pid_set = set_new(NULL); |
82659fd7 LP |
4314 | if (!pid_set) |
4315 | return NULL; | |
4316 | ||
4317 | /* Exclude the main/control pids from being killed via the cgroup */ | |
4318 | if (main_pid > 0) { | |
fea72cc0 | 4319 | r = set_put(pid_set, PID_TO_PTR(main_pid)); |
82659fd7 | 4320 | if (r < 0) |
95f14a3e | 4321 | return NULL; |
82659fd7 LP |
4322 | } |
4323 | ||
4324 | if (control_pid > 0) { | |
fea72cc0 | 4325 | r = set_put(pid_set, PID_TO_PTR(control_pid)); |
82659fd7 | 4326 | if (r < 0) |
95f14a3e | 4327 | return NULL; |
82659fd7 LP |
4328 | } |
4329 | ||
95f14a3e | 4330 | return TAKE_PTR(pid_set); |
82659fd7 LP |
4331 | } |
4332 | ||
d9911002 LP |
4333 | static int kill_common_log(pid_t pid, int signo, void *userdata) { |
4334 | _cleanup_free_ char *comm = NULL; | |
4335 | Unit *u = userdata; | |
4336 | ||
4337 | assert(u); | |
4338 | ||
4339 | (void) get_process_comm(pid, &comm); | |
4340 | log_unit_info(u, "Sending signal SIG%s to process " PID_FMT " (%s) on client request.", | |
4341 | signal_to_string(signo), pid, strna(comm)); | |
4342 | ||
4343 | return 1; | |
4344 | } | |
4345 | ||
d91c34f2 LP |
4346 | int unit_kill_common( |
4347 | Unit *u, | |
4348 | KillWho who, | |
4349 | int signo, | |
4350 | pid_t main_pid, | |
4351 | pid_t control_pid, | |
718db961 | 4352 | sd_bus_error *error) { |
d91c34f2 | 4353 | |
814cc562 | 4354 | int r = 0; |
ac5e3a50 | 4355 | bool killed = false; |
814cc562 | 4356 | |
8aff7ac4 LP |
4357 | /* This is the common implementation for explicit user-requested killing of unit processes, shared by |
4358 | * various unit types. Do not confuse with unit_kill_context(), which is what we use when we want to | |
4359 | * stop a service ourselves. */ | |
4360 | ||
ac5e3a50 | 4361 | if (IN_SET(who, KILL_MAIN, KILL_MAIN_FAIL)) { |
814cc562 | 4362 | if (main_pid < 0) |
7358dc02 | 4363 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no main processes", unit_type_to_string(u->type)); |
8aff7ac4 | 4364 | if (main_pid == 0) |
7358dc02 | 4365 | return sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill"); |
814cc562 MS |
4366 | } |
4367 | ||
ac5e3a50 | 4368 | if (IN_SET(who, KILL_CONTROL, KILL_CONTROL_FAIL)) { |
814cc562 | 4369 | if (control_pid < 0) |
7358dc02 | 4370 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no control processes", unit_type_to_string(u->type)); |
8aff7ac4 | 4371 | if (control_pid == 0) |
7358dc02 | 4372 | return sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill"); |
814cc562 MS |
4373 | } |
4374 | ||
ac5e3a50 JS |
4375 | if (IN_SET(who, KILL_CONTROL, KILL_CONTROL_FAIL, KILL_ALL, KILL_ALL_FAIL)) |
4376 | if (control_pid > 0) { | |
d9911002 LP |
4377 | _cleanup_free_ char *comm = NULL; |
4378 | (void) get_process_comm(control_pid, &comm); | |
4379 | ||
4380 | if (kill(control_pid, signo) < 0) { | |
4381 | /* Report this failure both to the logs and to the client */ | |
4382 | sd_bus_error_set_errnof( | |
4383 | error, errno, | |
4384 | "Failed to send signal SIG%s to control process " PID_FMT " (%s): %m", | |
4385 | signal_to_string(signo), control_pid, strna(comm)); | |
4386 | r = log_unit_warning_errno( | |
4387 | u, errno, | |
4388 | "Failed to send signal SIG%s to control process " PID_FMT " (%s) on client request: %m", | |
4389 | signal_to_string(signo), control_pid, strna(comm)); | |
4390 | } else { | |
4391 | log_unit_info(u, "Sent signal SIG%s to control process " PID_FMT " (%s) on client request.", | |
4392 | signal_to_string(signo), control_pid, strna(comm)); | |
ac5e3a50 | 4393 | killed = true; |
d9911002 | 4394 | } |
ac5e3a50 | 4395 | } |
814cc562 | 4396 | |
ac5e3a50 JS |
4397 | if (IN_SET(who, KILL_MAIN, KILL_MAIN_FAIL, KILL_ALL, KILL_ALL_FAIL)) |
4398 | if (main_pid > 0) { | |
d9911002 LP |
4399 | _cleanup_free_ char *comm = NULL; |
4400 | (void) get_process_comm(main_pid, &comm); | |
4401 | ||
4402 | if (kill(main_pid, signo) < 0) { | |
4403 | if (r == 0) | |
4404 | sd_bus_error_set_errnof( | |
4405 | error, errno, | |
4406 | "Failed to send signal SIG%s to main process " PID_FMT " (%s): %m", | |
4407 | signal_to_string(signo), main_pid, strna(comm)); | |
4408 | ||
4409 | r = log_unit_warning_errno( | |
4410 | u, errno, | |
4411 | "Failed to send signal SIG%s to main process " PID_FMT " (%s) on client request: %m", | |
4412 | signal_to_string(signo), main_pid, strna(comm)); | |
4413 | } else { | |
4414 | log_unit_info(u, "Sent signal SIG%s to main process " PID_FMT " (%s) on client request.", | |
4415 | signal_to_string(signo), main_pid, strna(comm)); | |
ac5e3a50 | 4416 | killed = true; |
d9911002 | 4417 | } |
ac5e3a50 | 4418 | } |
814cc562 | 4419 | |
ac5e3a50 | 4420 | if (IN_SET(who, KILL_ALL, KILL_ALL_FAIL) && u->cgroup_path) { |
814cc562 MS |
4421 | _cleanup_set_free_ Set *pid_set = NULL; |
4422 | int q; | |
4423 | ||
82659fd7 LP |
4424 | /* Exclude the main/control pids from being killed via the cgroup */ |
4425 | pid_set = unit_pid_set(main_pid, control_pid); | |
814cc562 | 4426 | if (!pid_set) |
d9911002 LP |
4427 | return log_oom(); |
4428 | ||
4429 | q = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, signo, 0, pid_set, kill_common_log, u); | |
4430 | if (q < 0) { | |
4431 | if (!IN_SET(q, -ESRCH, -ENOENT)) { | |
4432 | if (r == 0) | |
4433 | sd_bus_error_set_errnof( | |
4434 | error, q, | |
4435 | "Failed to send signal SIG%s to auxiliary processes: %m", | |
4436 | signal_to_string(signo)); | |
4437 | ||
4438 | r = log_unit_warning_errno( | |
4439 | u, q, | |
4440 | "Failed to send signal SIG%s to auxiliary processes on client request: %m", | |
4441 | signal_to_string(signo)); | |
4442 | } | |
4443 | } else | |
ac5e3a50 | 4444 | killed = true; |
814cc562 MS |
4445 | } |
4446 | ||
2ae0508e LP |
4447 | /* If the "fail" versions of the operation are requested, then complain if the set of processes we killed is empty */ |
4448 | if (r == 0 && !killed && IN_SET(who, KILL_ALL_FAIL, KILL_CONTROL_FAIL, KILL_MAIN_FAIL)) | |
4449 | return sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No matching processes to kill"); | |
ac5e3a50 | 4450 | |
814cc562 MS |
4451 | return r; |
4452 | } | |
4453 | ||
6210e7fc LP |
4454 | int unit_following_set(Unit *u, Set **s) { |
4455 | assert(u); | |
4456 | assert(s); | |
4457 | ||
4458 | if (UNIT_VTABLE(u)->following_set) | |
4459 | return UNIT_VTABLE(u)->following_set(u, s); | |
4460 | ||
4461 | *s = NULL; | |
4462 | return 0; | |
4463 | } | |
4464 | ||
a4375746 | 4465 | UnitFileState unit_get_unit_file_state(Unit *u) { |
0ec0deaa LP |
4466 | int r; |
4467 | ||
a4375746 LP |
4468 | assert(u); |
4469 | ||
0ec0deaa LP |
4470 | if (u->unit_file_state < 0 && u->fragment_path) { |
4471 | r = unit_file_get_state( | |
463d0d15 | 4472 | u->manager->unit_file_scope, |
0ec0deaa | 4473 | NULL, |
9ea3a0e7 | 4474 | u->id, |
0ec0deaa LP |
4475 | &u->unit_file_state); |
4476 | if (r < 0) | |
4477 | u->unit_file_state = UNIT_FILE_BAD; | |
4478 | } | |
a4375746 | 4479 | |
ac155bb8 | 4480 | return u->unit_file_state; |
a4375746 LP |
4481 | } |
4482 | ||
d2dc52db LP |
4483 | int unit_get_unit_file_preset(Unit *u) { |
4484 | assert(u); | |
4485 | ||
4486 | if (u->unit_file_preset < 0 && u->fragment_path) | |
4487 | u->unit_file_preset = unit_file_query_preset( | |
463d0d15 | 4488 | u->manager->unit_file_scope, |
0ec0deaa | 4489 | NULL, |
8f7b2566 ZJS |
4490 | basename(u->fragment_path), |
4491 | NULL); | |
d2dc52db LP |
4492 | |
4493 | return u->unit_file_preset; | |
4494 | } | |
4495 | ||
7f7d01ed | 4496 | Unit* unit_ref_set(UnitRef *ref, Unit *source, Unit *target) { |
57020a3a | 4497 | assert(ref); |
7f7d01ed ZJS |
4498 | assert(source); |
4499 | assert(target); | |
57020a3a | 4500 | |
7f7d01ed | 4501 | if (ref->target) |
57020a3a LP |
4502 | unit_ref_unset(ref); |
4503 | ||
7f7d01ed ZJS |
4504 | ref->source = source; |
4505 | ref->target = target; | |
4506 | LIST_PREPEND(refs_by_target, target->refs_by_target, ref); | |
4507 | return target; | |
57020a3a LP |
4508 | } |
4509 | ||
4510 | void unit_ref_unset(UnitRef *ref) { | |
4511 | assert(ref); | |
4512 | ||
7f7d01ed | 4513 | if (!ref->target) |
57020a3a LP |
4514 | return; |
4515 | ||
b75102e5 LP |
4516 | /* We are about to drop a reference to the unit, make sure the garbage collection has a look at it as it might |
4517 | * be unreferenced now. */ | |
7f7d01ed | 4518 | unit_add_to_gc_queue(ref->target); |
b75102e5 | 4519 | |
7f7d01ed ZJS |
4520 | LIST_REMOVE(refs_by_target, ref->target->refs_by_target, ref); |
4521 | ref->source = ref->target = NULL; | |
57020a3a LP |
4522 | } |
4523 | ||
29206d46 LP |
4524 | static int user_from_unit_name(Unit *u, char **ret) { |
4525 | ||
4526 | static const uint8_t hash_key[] = { | |
4527 | 0x58, 0x1a, 0xaf, 0xe6, 0x28, 0x58, 0x4e, 0x96, | |
4528 | 0xb4, 0x4e, 0xf5, 0x3b, 0x8c, 0x92, 0x07, 0xec | |
4529 | }; | |
4530 | ||
4531 | _cleanup_free_ char *n = NULL; | |
4532 | int r; | |
4533 | ||
4534 | r = unit_name_to_prefix(u->id, &n); | |
4535 | if (r < 0) | |
4536 | return r; | |
4537 | ||
7a8867ab | 4538 | if (valid_user_group_name(n, 0)) { |
ae2a15bc | 4539 | *ret = TAKE_PTR(n); |
29206d46 LP |
4540 | return 0; |
4541 | } | |
4542 | ||
4543 | /* If we can't use the unit name as a user name, then let's hash it and use that */ | |
4544 | if (asprintf(ret, "_du%016" PRIx64, siphash24(n, strlen(n), hash_key)) < 0) | |
4545 | return -ENOMEM; | |
4546 | ||
4547 | return 0; | |
4548 | } | |
4549 | ||
598459ce LP |
4550 | int unit_patch_contexts(Unit *u) { |
4551 | CGroupContext *cc; | |
4552 | ExecContext *ec; | |
cba6e062 LP |
4553 | int r; |
4554 | ||
e06c73cc | 4555 | assert(u); |
e06c73cc | 4556 | |
598459ce LP |
4557 | /* Patch in the manager defaults into the exec and cgroup |
4558 | * contexts, _after_ the rest of the settings have been | |
4559 | * initialized */ | |
085afe36 | 4560 | |
598459ce LP |
4561 | ec = unit_get_exec_context(u); |
4562 | if (ec) { | |
4563 | /* This only copies in the ones that need memory */ | |
12375b95 | 4564 | for (unsigned i = 0; i < _RLIMIT_MAX; i++) |
598459ce LP |
4565 | if (u->manager->rlimit[i] && !ec->rlimit[i]) { |
4566 | ec->rlimit[i] = newdup(struct rlimit, u->manager->rlimit[i], 1); | |
4567 | if (!ec->rlimit[i]) | |
4568 | return -ENOMEM; | |
4569 | } | |
4570 | ||
463d0d15 | 4571 | if (MANAGER_IS_USER(u->manager) && |
598459ce LP |
4572 | !ec->working_directory) { |
4573 | ||
4574 | r = get_home_dir(&ec->working_directory); | |
4575 | if (r < 0) | |
4576 | return r; | |
4c08c824 LP |
4577 | |
4578 | /* Allow user services to run, even if the | |
4579 | * home directory is missing */ | |
4580 | ec->working_directory_missing_ok = true; | |
cba6e062 LP |
4581 | } |
4582 | ||
598459ce | 4583 | if (ec->private_devices) |
2cd0a735 | 4584 | ec->capability_bounding_set &= ~((UINT64_C(1) << CAP_MKNOD) | (UINT64_C(1) << CAP_SYS_RAWIO)); |
502d704e DH |
4585 | |
4586 | if (ec->protect_kernel_modules) | |
4587 | ec->capability_bounding_set &= ~(UINT64_C(1) << CAP_SYS_MODULE); | |
29206d46 | 4588 | |
84703040 KK |
4589 | if (ec->protect_kernel_logs) |
4590 | ec->capability_bounding_set &= ~(UINT64_C(1) << CAP_SYSLOG); | |
4591 | ||
fc64760d KK |
4592 | if (ec->protect_clock) |
4593 | ec->capability_bounding_set &= ~((UINT64_C(1) << CAP_SYS_TIME) | (UINT64_C(1) << CAP_WAKE_ALARM)); | |
4594 | ||
29206d46 LP |
4595 | if (ec->dynamic_user) { |
4596 | if (!ec->user) { | |
4597 | r = user_from_unit_name(u, &ec->user); | |
4598 | if (r < 0) | |
4599 | return r; | |
4600 | } | |
4601 | ||
4602 | if (!ec->group) { | |
4603 | ec->group = strdup(ec->user); | |
4604 | if (!ec->group) | |
4605 | return -ENOMEM; | |
4606 | } | |
4607 | ||
bf65b7e0 LP |
4608 | /* If the dynamic user option is on, let's make sure that the unit can't leave its |
4609 | * UID/GID around in the file system or on IPC objects. Hence enforce a strict | |
4610 | * sandbox. */ | |
63bb64a0 | 4611 | |
29206d46 | 4612 | ec->private_tmp = true; |
00d9ef85 | 4613 | ec->remove_ipc = true; |
63bb64a0 LP |
4614 | ec->protect_system = PROTECT_SYSTEM_STRICT; |
4615 | if (ec->protect_home == PROTECT_HOME_NO) | |
4616 | ec->protect_home = PROTECT_HOME_READ_ONLY; | |
bf65b7e0 LP |
4617 | |
4618 | /* Make sure this service can neither benefit from SUID/SGID binaries nor create | |
4619 | * them. */ | |
4620 | ec->no_new_privileges = true; | |
4621 | ec->restrict_suid_sgid = true; | |
29206d46 | 4622 | } |
cba6e062 LP |
4623 | } |
4624 | ||
598459ce | 4625 | cc = unit_get_cgroup_context(u); |
fe65e88b | 4626 | if (cc && ec) { |
f513e420 | 4627 | |
fe65e88b | 4628 | if (ec->private_devices && |
084870f9 ZJS |
4629 | cc->device_policy == CGROUP_DEVICE_POLICY_AUTO) |
4630 | cc->device_policy = CGROUP_DEVICE_POLICY_CLOSED; | |
fe65e88b | 4631 | |
b3d13314 | 4632 | if ((ec->root_image || !LIST_IS_EMPTY(ec->mount_images)) && |
084870f9 | 4633 | (cc->device_policy != CGROUP_DEVICE_POLICY_AUTO || cc->device_allow)) { |
0cffae95 | 4634 | const char *p; |
fe65e88b | 4635 | |
b3d13314 | 4636 | /* When RootImage= or MountImages= is specified, the following devices are touched. */ |
0cffae95 LB |
4637 | FOREACH_STRING(p, "/dev/loop-control", "/dev/mapper/control") { |
4638 | r = cgroup_add_device_allow(cc, p, "rw"); | |
4639 | if (r < 0) | |
4640 | return r; | |
4641 | } | |
4642 | FOREACH_STRING(p, "block-loop", "block-blkext", "block-device-mapper") { | |
4643 | r = cgroup_add_device_allow(cc, p, "rwm"); | |
4644 | if (r < 0) | |
4645 | return r; | |
4646 | } | |
867af728 | 4647 | |
0cffae95 LB |
4648 | /* Make sure "block-loop" can be resolved, i.e. make sure "loop" shows up in /proc/devices. |
4649 | * Same for mapper and verity. */ | |
4650 | FOREACH_STRING(p, "modprobe@loop.service", "modprobe@dm_mod.service", "modprobe@dm_verity.service") { | |
4651 | r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_WANTS, p, true, UNIT_DEPENDENCY_FILE); | |
4652 | if (r < 0) | |
4653 | return r; | |
4654 | } | |
fe65e88b | 4655 | } |
fc64760d KK |
4656 | |
4657 | if (ec->protect_clock) { | |
4658 | r = cgroup_add_device_allow(cc, "char-rtc", "r"); | |
4659 | if (r < 0) | |
4660 | return r; | |
4661 | } | |
598459ce | 4662 | } |
f1660f96 | 4663 | |
cba6e062 | 4664 | return 0; |
e06c73cc LP |
4665 | } |
4666 | ||
3ef63c31 LP |
4667 | ExecContext *unit_get_exec_context(Unit *u) { |
4668 | size_t offset; | |
4669 | assert(u); | |
4670 | ||
598459ce LP |
4671 | if (u->type < 0) |
4672 | return NULL; | |
4673 | ||
3ef63c31 LP |
4674 | offset = UNIT_VTABLE(u)->exec_context_offset; |
4675 | if (offset <= 0) | |
4676 | return NULL; | |
4677 | ||
4678 | return (ExecContext*) ((uint8_t*) u + offset); | |
4679 | } | |
4680 | ||
718db961 LP |
4681 | KillContext *unit_get_kill_context(Unit *u) { |
4682 | size_t offset; | |
4683 | assert(u); | |
4684 | ||
598459ce LP |
4685 | if (u->type < 0) |
4686 | return NULL; | |
4687 | ||
718db961 LP |
4688 | offset = UNIT_VTABLE(u)->kill_context_offset; |
4689 | if (offset <= 0) | |
4690 | return NULL; | |
4691 | ||
4692 | return (KillContext*) ((uint8_t*) u + offset); | |
4693 | } | |
4694 | ||
4ad49000 LP |
4695 | CGroupContext *unit_get_cgroup_context(Unit *u) { |
4696 | size_t offset; | |
4697 | ||
598459ce LP |
4698 | if (u->type < 0) |
4699 | return NULL; | |
4700 | ||
4ad49000 LP |
4701 | offset = UNIT_VTABLE(u)->cgroup_context_offset; |
4702 | if (offset <= 0) | |
4703 | return NULL; | |
4704 | ||
4705 | return (CGroupContext*) ((uint8_t*) u + offset); | |
4706 | } | |
4707 | ||
613b411c LP |
4708 | ExecRuntime *unit_get_exec_runtime(Unit *u) { |
4709 | size_t offset; | |
4710 | ||
598459ce LP |
4711 | if (u->type < 0) |
4712 | return NULL; | |
4713 | ||
613b411c LP |
4714 | offset = UNIT_VTABLE(u)->exec_runtime_offset; |
4715 | if (offset <= 0) | |
4716 | return NULL; | |
4717 | ||
4718 | return *(ExecRuntime**) ((uint8_t*) u + offset); | |
4719 | } | |
4720 | ||
2e59b241 | 4721 | static const char* unit_drop_in_dir(Unit *u, UnitWriteFlags flags) { |
3f5e8115 LP |
4722 | assert(u); |
4723 | ||
2e59b241 | 4724 | if (UNIT_WRITE_FLAGS_NOOP(flags)) |
4f4afc88 LP |
4725 | return NULL; |
4726 | ||
39591351 LP |
4727 | if (u->transient) /* Redirect drop-ins for transient units always into the transient directory. */ |
4728 | return u->manager->lookup_paths.transient; | |
26d04f86 | 4729 | |
2e59b241 | 4730 | if (flags & UNIT_PERSISTENT) |
4f4afc88 | 4731 | return u->manager->lookup_paths.persistent_control; |
26d04f86 | 4732 | |
2e59b241 LP |
4733 | if (flags & UNIT_RUNTIME) |
4734 | return u->manager->lookup_paths.runtime_control; | |
4735 | ||
39591351 | 4736 | return NULL; |
71645aca LP |
4737 | } |
4738 | ||
2e59b241 LP |
4739 | char* unit_escape_setting(const char *s, UnitWriteFlags flags, char **buf) { |
4740 | char *ret = NULL; | |
4741 | ||
4742 | if (!s) | |
4743 | return NULL; | |
4744 | ||
4745 | /* Escapes the input string as requested. Returns the escaped string. If 'buf' is specified then the allocated | |
4746 | * return buffer pointer is also written to *buf, except if no escaping was necessary, in which case *buf is | |
4747 | * set to NULL, and the input pointer is returned as-is. This means the return value always contains a properly | |
4748 | * escaped version, but *buf when passed only contains a pointer if an allocation was necessary. If *buf is | |
4749 | * not specified, then the return value always needs to be freed. Callers can use this to optimize memory | |
4750 | * allocations. */ | |
4751 | ||
4752 | if (flags & UNIT_ESCAPE_SPECIFIERS) { | |
4753 | ret = specifier_escape(s); | |
4754 | if (!ret) | |
4755 | return NULL; | |
4756 | ||
4757 | s = ret; | |
4758 | } | |
4759 | ||
4760 | if (flags & UNIT_ESCAPE_C) { | |
4761 | char *a; | |
4762 | ||
4763 | a = cescape(s); | |
4764 | free(ret); | |
4765 | if (!a) | |
4766 | return NULL; | |
4767 | ||
4768 | ret = a; | |
4769 | } | |
4770 | ||
4771 | if (buf) { | |
4772 | *buf = ret; | |
4773 | return ret ?: (char*) s; | |
4774 | } | |
4775 | ||
4776 | return ret ?: strdup(s); | |
4777 | } | |
4778 | ||
4779 | char* unit_concat_strv(char **l, UnitWriteFlags flags) { | |
4780 | _cleanup_free_ char *result = NULL; | |
4781 | size_t n = 0, allocated = 0; | |
ae2a15bc | 4782 | char **i; |
2e59b241 LP |
4783 | |
4784 | /* Takes a list of strings, escapes them, and concatenates them. This may be used to format command lines in a | |
4785 | * way suitable for ExecStart= stanzas */ | |
4786 | ||
4787 | STRV_FOREACH(i, l) { | |
4788 | _cleanup_free_ char *buf = NULL; | |
4789 | const char *p; | |
4790 | size_t a; | |
4791 | char *q; | |
4792 | ||
4793 | p = unit_escape_setting(*i, flags, &buf); | |
4794 | if (!p) | |
4795 | return NULL; | |
4796 | ||
4797 | a = (n > 0) + 1 + strlen(p) + 1; /* separating space + " + entry + " */ | |
4798 | if (!GREEDY_REALLOC(result, allocated, n + a + 1)) | |
4799 | return NULL; | |
4800 | ||
4801 | q = result + n; | |
4802 | if (n > 0) | |
4803 | *(q++) = ' '; | |
4804 | ||
4805 | *(q++) = '"'; | |
4806 | q = stpcpy(q, p); | |
4807 | *(q++) = '"'; | |
4808 | ||
4809 | n += a; | |
4810 | } | |
4811 | ||
4812 | if (!GREEDY_REALLOC(result, allocated, n + 1)) | |
4813 | return NULL; | |
4814 | ||
4815 | result[n] = 0; | |
4816 | ||
ae2a15bc | 4817 | return TAKE_PTR(result); |
2e59b241 LP |
4818 | } |
4819 | ||
4820 | int unit_write_setting(Unit *u, UnitWriteFlags flags, const char *name, const char *data) { | |
4821 | _cleanup_free_ char *p = NULL, *q = NULL, *escaped = NULL; | |
2a9a6f8a | 4822 | const char *dir, *wrapped; |
26d04f86 | 4823 | int r; |
71645aca LP |
4824 | |
4825 | assert(u); | |
2e59b241 LP |
4826 | assert(name); |
4827 | assert(data); | |
4828 | ||
4829 | if (UNIT_WRITE_FLAGS_NOOP(flags)) | |
4830 | return 0; | |
4831 | ||
4832 | data = unit_escape_setting(data, flags, &escaped); | |
4833 | if (!data) | |
4834 | return -ENOMEM; | |
4835 | ||
4836 | /* Prefix the section header. If we are writing this out as transient file, then let's suppress this if the | |
4837 | * previous section header is the same */ | |
4838 | ||
4839 | if (flags & UNIT_PRIVATE) { | |
4840 | if (!UNIT_VTABLE(u)->private_section) | |
4841 | return -EINVAL; | |
4842 | ||
4843 | if (!u->transient_file || u->last_section_private < 0) | |
4844 | data = strjoina("[", UNIT_VTABLE(u)->private_section, "]\n", data); | |
4845 | else if (u->last_section_private == 0) | |
4846 | data = strjoina("\n[", UNIT_VTABLE(u)->private_section, "]\n", data); | |
4847 | } else { | |
4848 | if (!u->transient_file || u->last_section_private < 0) | |
4849 | data = strjoina("[Unit]\n", data); | |
4850 | else if (u->last_section_private > 0) | |
4851 | data = strjoina("\n[Unit]\n", data); | |
4852 | } | |
71645aca | 4853 | |
4f4afc88 LP |
4854 | if (u->transient_file) { |
4855 | /* When this is a transient unit file in creation, then let's not create a new drop-in but instead | |
4856 | * write to the transient unit file. */ | |
4857 | fputs(data, u->transient_file); | |
4f4afc88 | 4858 | |
2e59b241 LP |
4859 | if (!endswith(data, "\n")) |
4860 | fputc('\n', u->transient_file); | |
4861 | ||
4862 | /* Remember which section we wrote this entry to */ | |
4863 | u->last_section_private = !!(flags & UNIT_PRIVATE); | |
8e2af478 | 4864 | return 0; |
2e59b241 | 4865 | } |
8e2af478 | 4866 | |
2e59b241 | 4867 | dir = unit_drop_in_dir(u, flags); |
39591351 LP |
4868 | if (!dir) |
4869 | return -EINVAL; | |
71645aca | 4870 | |
2a9a6f8a | 4871 | wrapped = strjoina("# This is a drop-in unit file extension, created via \"systemctl set-property\"\n" |
3f71dec5 | 4872 | "# or an equivalent operation. Do not edit.\n", |
2a9a6f8a ZJS |
4873 | data, |
4874 | "\n"); | |
e20b2a86 | 4875 | |
815b09d3 | 4876 | r = drop_in_file(dir, u->id, 50, name, &p, &q); |
adb76a70 WC |
4877 | if (r < 0) |
4878 | return r; | |
4879 | ||
45639f1b | 4880 | (void) mkdir_p_label(p, 0755); |
4dba44a5 ZJS |
4881 | |
4882 | /* Make sure the drop-in dir is registered in our path cache. This way we don't need to stupidly | |
4883 | * recreate the cache after every drop-in we write. */ | |
4884 | if (u->manager->unit_path_cache) { | |
be327321 | 4885 | r = set_put_strdup(&u->manager->unit_path_cache, p); |
4dba44a5 ZJS |
4886 | if (r < 0) |
4887 | return r; | |
4888 | } | |
4889 | ||
2a9a6f8a | 4890 | r = write_string_file_atomic_label(q, wrapped); |
adb76a70 WC |
4891 | if (r < 0) |
4892 | return r; | |
4893 | ||
815b09d3 | 4894 | r = strv_push(&u->dropin_paths, q); |
adb76a70 WC |
4895 | if (r < 0) |
4896 | return r; | |
815b09d3 | 4897 | q = NULL; |
adb76a70 | 4898 | |
adb76a70 WC |
4899 | strv_uniq(u->dropin_paths); |
4900 | ||
4901 | u->dropin_mtime = now(CLOCK_REALTIME); | |
4902 | ||
4903 | return 0; | |
26d04f86 | 4904 | } |
71645aca | 4905 | |
2e59b241 | 4906 | int unit_write_settingf(Unit *u, UnitWriteFlags flags, const char *name, const char *format, ...) { |
b9ec9359 LP |
4907 | _cleanup_free_ char *p = NULL; |
4908 | va_list ap; | |
4909 | int r; | |
4910 | ||
4911 | assert(u); | |
4912 | assert(name); | |
4913 | assert(format); | |
4914 | ||
2e59b241 | 4915 | if (UNIT_WRITE_FLAGS_NOOP(flags)) |
b9ec9359 LP |
4916 | return 0; |
4917 | ||
4918 | va_start(ap, format); | |
4919 | r = vasprintf(&p, format, ap); | |
4920 | va_end(ap); | |
4921 | ||
4922 | if (r < 0) | |
4923 | return -ENOMEM; | |
4924 | ||
2e59b241 | 4925 | return unit_write_setting(u, flags, name, p); |
b9ec9359 | 4926 | } |
71645aca | 4927 | |
c2756a68 | 4928 | int unit_make_transient(Unit *u) { |
0126c8f3 | 4929 | _cleanup_free_ char *path = NULL; |
4f4afc88 | 4930 | FILE *f; |
4f4afc88 | 4931 | |
c2756a68 LP |
4932 | assert(u); |
4933 | ||
3f5e8115 LP |
4934 | if (!UNIT_VTABLE(u)->can_transient) |
4935 | return -EOPNOTSUPP; | |
4936 | ||
45639f1b LP |
4937 | (void) mkdir_p_label(u->manager->lookup_paths.transient, 0755); |
4938 | ||
657ee2d8 | 4939 | path = path_join(u->manager->lookup_paths.transient, u->id); |
4f4afc88 LP |
4940 | if (!path) |
4941 | return -ENOMEM; | |
4942 | ||
4943 | /* Let's open the file we'll write the transient settings into. This file is kept open as long as we are | |
4944 | * creating the transient, and is closed in unit_load(), as soon as we start loading the file. */ | |
4945 | ||
78e334b5 | 4946 | RUN_WITH_UMASK(0022) { |
4f4afc88 | 4947 | f = fopen(path, "we"); |
0126c8f3 | 4948 | if (!f) |
78e334b5 | 4949 | return -errno; |
4f4afc88 LP |
4950 | } |
4951 | ||
0126c8f3 | 4952 | safe_fclose(u->transient_file); |
4f4afc88 LP |
4953 | u->transient_file = f; |
4954 | ||
0126c8f3 | 4955 | free_and_replace(u->fragment_path, path); |
7c65093a | 4956 | |
7c65093a LP |
4957 | u->source_path = mfree(u->source_path); |
4958 | u->dropin_paths = strv_free(u->dropin_paths); | |
4959 | u->fragment_mtime = u->source_mtime = u->dropin_mtime = 0; | |
4960 | ||
4f4afc88 LP |
4961 | u->load_state = UNIT_STUB; |
4962 | u->load_error = 0; | |
4963 | u->transient = true; | |
4964 | ||
7c65093a LP |
4965 | unit_add_to_dbus_queue(u); |
4966 | unit_add_to_gc_queue(u); | |
c2756a68 | 4967 | |
4f4afc88 LP |
4968 | fputs("# This is a transient unit file, created programmatically via the systemd API. Do not edit.\n", |
4969 | u->transient_file); | |
4970 | ||
3f5e8115 | 4971 | return 0; |
c2756a68 LP |
4972 | } |
4973 | ||
c53d2d54 | 4974 | static int log_kill(pid_t pid, int sig, void *userdata) { |
1d98fef1 LP |
4975 | _cleanup_free_ char *comm = NULL; |
4976 | ||
4977 | (void) get_process_comm(pid, &comm); | |
4978 | ||
4979 | /* Don't log about processes marked with brackets, under the assumption that these are temporary processes | |
4980 | only, like for example systemd's own PAM stub process. */ | |
4981 | if (comm && comm[0] == '(') | |
c53d2d54 | 4982 | return 0; |
1d98fef1 LP |
4983 | |
4984 | log_unit_notice(userdata, | |
4985 | "Killing process " PID_FMT " (%s) with signal SIG%s.", | |
4986 | pid, | |
4987 | strna(comm), | |
4988 | signal_to_string(sig)); | |
c53d2d54 DB |
4989 | |
4990 | return 1; | |
1d98fef1 LP |
4991 | } |
4992 | ||
4ab1670f | 4993 | static int operation_to_signal(const KillContext *c, KillOperation k, bool *noteworthy) { |
1d98fef1 LP |
4994 | assert(c); |
4995 | ||
4996 | switch (k) { | |
4997 | ||
4998 | case KILL_TERMINATE: | |
4999 | case KILL_TERMINATE_AND_LOG: | |
4ab1670f | 5000 | *noteworthy = false; |
1d98fef1 LP |
5001 | return c->kill_signal; |
5002 | ||
a232ebcc | 5003 | case KILL_RESTART: |
4ab1670f | 5004 | *noteworthy = false; |
a232ebcc ZJS |
5005 | return restart_kill_signal(c); |
5006 | ||
1d98fef1 | 5007 | case KILL_KILL: |
4ab1670f | 5008 | *noteworthy = true; |
fbb48d4c | 5009 | return c->final_kill_signal; |
1d98fef1 | 5010 | |
c87700a1 | 5011 | case KILL_WATCHDOG: |
4ab1670f | 5012 | *noteworthy = true; |
c87700a1 | 5013 | return c->watchdog_signal; |
1d98fef1 LP |
5014 | |
5015 | default: | |
5016 | assert_not_reached("KillOperation unknown"); | |
5017 | } | |
5018 | } | |
5019 | ||
cd2086fe LP |
5020 | int unit_kill_context( |
5021 | Unit *u, | |
5022 | KillContext *c, | |
db2cb23b | 5023 | KillOperation k, |
cd2086fe LP |
5024 | pid_t main_pid, |
5025 | pid_t control_pid, | |
5026 | bool main_pid_alien) { | |
5027 | ||
1d98fef1 | 5028 | bool wait_for_exit = false, send_sighup; |
59ec09a8 | 5029 | cg_kill_log_func_t log_func = NULL; |
b821a397 | 5030 | int sig, r; |
cd2086fe LP |
5031 | |
5032 | assert(u); | |
5033 | assert(c); | |
5034 | ||
8aff7ac4 LP |
5035 | /* Kill the processes belonging to this unit, in preparation for shutting the unit down. Returns > 0 |
5036 | * if we killed something worth waiting for, 0 otherwise. Do not confuse with unit_kill_common() | |
5037 | * which is used for user-requested killing of unit processes. */ | |
1d98fef1 | 5038 | |
cd2086fe LP |
5039 | if (c->kill_mode == KILL_NONE) |
5040 | return 0; | |
5041 | ||
4ab1670f ZJS |
5042 | bool noteworthy; |
5043 | sig = operation_to_signal(c, k, ¬eworthy); | |
5044 | if (noteworthy) | |
5045 | log_func = log_kill; | |
1d98fef1 LP |
5046 | |
5047 | send_sighup = | |
5048 | c->send_sighup && | |
5049 | IN_SET(k, KILL_TERMINATE, KILL_TERMINATE_AND_LOG) && | |
5050 | sig != SIGHUP; | |
5051 | ||
cd2086fe | 5052 | if (main_pid > 0) { |
1d98fef1 LP |
5053 | if (log_func) |
5054 | log_func(main_pid, sig, u); | |
cd2086fe | 5055 | |
1d98fef1 | 5056 | r = kill_and_sigcont(main_pid, sig); |
cd2086fe LP |
5057 | if (r < 0 && r != -ESRCH) { |
5058 | _cleanup_free_ char *comm = NULL; | |
1d98fef1 | 5059 | (void) get_process_comm(main_pid, &comm); |
cd2086fe | 5060 | |
b821a397 | 5061 | log_unit_warning_errno(u, r, "Failed to kill main process " PID_FMT " (%s), ignoring: %m", main_pid, strna(comm)); |
82659fd7 | 5062 | } else { |
bc6aed7b LP |
5063 | if (!main_pid_alien) |
5064 | wait_for_exit = true; | |
82659fd7 | 5065 | |
1d98fef1 | 5066 | if (r != -ESRCH && send_sighup) |
d0667321 | 5067 | (void) kill(main_pid, SIGHUP); |
82659fd7 | 5068 | } |
cd2086fe LP |
5069 | } |
5070 | ||
5071 | if (control_pid > 0) { | |
1d98fef1 LP |
5072 | if (log_func) |
5073 | log_func(control_pid, sig, u); | |
cd2086fe | 5074 | |
1d98fef1 | 5075 | r = kill_and_sigcont(control_pid, sig); |
cd2086fe LP |
5076 | if (r < 0 && r != -ESRCH) { |
5077 | _cleanup_free_ char *comm = NULL; | |
1d98fef1 | 5078 | (void) get_process_comm(control_pid, &comm); |
cd2086fe | 5079 | |
b821a397 | 5080 | log_unit_warning_errno(u, r, "Failed to kill control process " PID_FMT " (%s), ignoring: %m", control_pid, strna(comm)); |
82659fd7 | 5081 | } else { |
cd2086fe | 5082 | wait_for_exit = true; |
82659fd7 | 5083 | |
1d98fef1 | 5084 | if (r != -ESRCH && send_sighup) |
d0667321 | 5085 | (void) kill(control_pid, SIGHUP); |
82659fd7 | 5086 | } |
cd2086fe LP |
5087 | } |
5088 | ||
b821a397 LP |
5089 | if (u->cgroup_path && |
5090 | (c->kill_mode == KILL_CONTROL_GROUP || (c->kill_mode == KILL_MIXED && k == KILL_KILL))) { | |
cd2086fe LP |
5091 | _cleanup_set_free_ Set *pid_set = NULL; |
5092 | ||
82659fd7 LP |
5093 | /* Exclude the main/control pids from being killed via the cgroup */ |
5094 | pid_set = unit_pid_set(main_pid, control_pid); | |
cd2086fe LP |
5095 | if (!pid_set) |
5096 | return -ENOMEM; | |
5097 | ||
1d98fef1 LP |
5098 | r = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, |
5099 | sig, | |
5100 | CGROUP_SIGCONT|CGROUP_IGNORE_SELF, | |
5101 | pid_set, | |
5102 | log_func, u); | |
cd2086fe | 5103 | if (r < 0) { |
4c701096 | 5104 | if (!IN_SET(r, -EAGAIN, -ESRCH, -ENOENT)) |
b821a397 LP |
5105 | log_unit_warning_errno(u, r, "Failed to kill control group %s, ignoring: %m", u->cgroup_path); |
5106 | ||
82659fd7 | 5107 | } else if (r > 0) { |
bc6aed7b | 5108 | |
1d9cc876 LP |
5109 | /* FIXME: For now, on the legacy hierarchy, we will not wait for the cgroup members to die if |
5110 | * we are running in a container or if this is a delegation unit, simply because cgroup | |
5111 | * notification is unreliable in these cases. It doesn't work at all in containers, and outside | |
5112 | * of containers it can be confused easily by left-over directories in the cgroup — which | |
5113 | * however should not exist in non-delegated units. On the unified hierarchy that's different, | |
5114 | * there we get proper events. Hence rely on them. */ | |
efdb0237 | 5115 | |
c22800e4 | 5116 | if (cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) > 0 || |
1d9cc876 | 5117 | (detect_container() == 0 && !unit_cgroup_delegate(u))) |
e9db43d5 | 5118 | wait_for_exit = true; |
58ea275a | 5119 | |
1d98fef1 | 5120 | if (send_sighup) { |
82659fd7 LP |
5121 | set_free(pid_set); |
5122 | ||
5123 | pid_set = unit_pid_set(main_pid, control_pid); | |
5124 | if (!pid_set) | |
5125 | return -ENOMEM; | |
5126 | ||
c8aa4b5b LP |
5127 | (void) cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, |
5128 | SIGHUP, | |
5129 | CGROUP_IGNORE_SELF, | |
5130 | pid_set, | |
5131 | NULL, NULL); | |
82659fd7 LP |
5132 | } |
5133 | } | |
cd2086fe LP |
5134 | } |
5135 | ||
5136 | return wait_for_exit; | |
5137 | } | |
5138 | ||
eef85c4a | 5139 | int unit_require_mounts_for(Unit *u, const char *path, UnitDependencyMask mask) { |
ca8700e9 | 5140 | _cleanup_free_ char *p = NULL; |
eef85c4a | 5141 | UnitDependencyInfo di; |
a57f7e2c LP |
5142 | int r; |
5143 | ||
5144 | assert(u); | |
5145 | assert(path); | |
5146 | ||
eef85c4a LP |
5147 | /* Registers a unit for requiring a certain path and all its prefixes. We keep a hashtable of these paths in |
5148 | * the unit (from the path to the UnitDependencyInfo structure indicating how to the dependency came to | |
5149 | * be). However, we build a prefix table for all possible prefixes so that new appearing mount units can easily | |
5150 | * determine which units to make themselves a dependency of. */ | |
a57f7e2c | 5151 | |
70b64bd3 ZJS |
5152 | if (!path_is_absolute(path)) |
5153 | return -EINVAL; | |
5154 | ||
548f6937 | 5155 | r = hashmap_ensure_allocated(&u->requires_mounts_for, &path_hash_ops); |
eef85c4a LP |
5156 | if (r < 0) |
5157 | return r; | |
5158 | ||
a57f7e2c LP |
5159 | p = strdup(path); |
5160 | if (!p) | |
5161 | return -ENOMEM; | |
5162 | ||
106bf8e4 | 5163 | path = path_simplify(p, true); |
a57f7e2c | 5164 | |
ca8700e9 | 5165 | if (!path_is_normalized(path)) |
a57f7e2c | 5166 | return -EPERM; |
a57f7e2c | 5167 | |
ca8700e9 | 5168 | if (hashmap_contains(u->requires_mounts_for, path)) |
a57f7e2c | 5169 | return 0; |
a57f7e2c | 5170 | |
eef85c4a LP |
5171 | di = (UnitDependencyInfo) { |
5172 | .origin_mask = mask | |
5173 | }; | |
5174 | ||
ca8700e9 ZJS |
5175 | r = hashmap_put(u->requires_mounts_for, path, di.data); |
5176 | if (r < 0) | |
a57f7e2c | 5177 | return r; |
ca8700e9 | 5178 | p = NULL; |
a57f7e2c | 5179 | |
4cb06c59 | 5180 | char prefix[strlen(path) + 1]; |
ca8700e9 | 5181 | PATH_FOREACH_PREFIX_MORE(prefix, path) { |
a57f7e2c LP |
5182 | Set *x; |
5183 | ||
5184 | x = hashmap_get(u->manager->units_requiring_mounts_for, prefix); | |
5185 | if (!x) { | |
ca8700e9 | 5186 | _cleanup_free_ char *q = NULL; |
a57f7e2c | 5187 | |
548f6937 | 5188 | r = hashmap_ensure_allocated(&u->manager->units_requiring_mounts_for, &path_hash_ops); |
742f41ad LP |
5189 | if (r < 0) |
5190 | return r; | |
a57f7e2c LP |
5191 | |
5192 | q = strdup(prefix); | |
5193 | if (!q) | |
5194 | return -ENOMEM; | |
5195 | ||
d5099efc | 5196 | x = set_new(NULL); |
ca8700e9 | 5197 | if (!x) |
a57f7e2c | 5198 | return -ENOMEM; |
a57f7e2c LP |
5199 | |
5200 | r = hashmap_put(u->manager->units_requiring_mounts_for, q, x); | |
5201 | if (r < 0) { | |
a57f7e2c LP |
5202 | set_free(x); |
5203 | return r; | |
5204 | } | |
ca8700e9 | 5205 | q = NULL; |
a57f7e2c LP |
5206 | } |
5207 | ||
5208 | r = set_put(x, u); | |
5209 | if (r < 0) | |
5210 | return r; | |
5211 | } | |
5212 | ||
5213 | return 0; | |
5214 | } | |
5215 | ||
613b411c LP |
5216 | int unit_setup_exec_runtime(Unit *u) { |
5217 | ExecRuntime **rt; | |
5218 | size_t offset; | |
613b411c | 5219 | Unit *other; |
eef85c4a | 5220 | void *v; |
e8a565cb | 5221 | int r; |
613b411c LP |
5222 | |
5223 | offset = UNIT_VTABLE(u)->exec_runtime_offset; | |
5224 | assert(offset > 0); | |
5225 | ||
06b643e7 | 5226 | /* Check if there already is an ExecRuntime for this unit? */ |
613b411c LP |
5227 | rt = (ExecRuntime**) ((uint8_t*) u + offset); |
5228 | if (*rt) | |
5229 | return 0; | |
5230 | ||
5231 | /* Try to get it from somebody else */ | |
90e74a66 | 5232 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_JOINS_NAMESPACE_OF]) { |
e8a565cb YW |
5233 | r = exec_runtime_acquire(u->manager, NULL, other->id, false, rt); |
5234 | if (r == 1) | |
5235 | return 1; | |
613b411c LP |
5236 | } |
5237 | ||
e8a565cb | 5238 | return exec_runtime_acquire(u->manager, unit_get_exec_context(u), u->id, true, rt); |
613b411c LP |
5239 | } |
5240 | ||
29206d46 LP |
5241 | int unit_setup_dynamic_creds(Unit *u) { |
5242 | ExecContext *ec; | |
5243 | DynamicCreds *dcreds; | |
5244 | size_t offset; | |
5245 | ||
5246 | assert(u); | |
5247 | ||
5248 | offset = UNIT_VTABLE(u)->dynamic_creds_offset; | |
5249 | assert(offset > 0); | |
5250 | dcreds = (DynamicCreds*) ((uint8_t*) u + offset); | |
5251 | ||
5252 | ec = unit_get_exec_context(u); | |
5253 | assert(ec); | |
5254 | ||
5255 | if (!ec->dynamic_user) | |
5256 | return 0; | |
5257 | ||
5258 | return dynamic_creds_acquire(dcreds, u->manager, ec->user, ec->group); | |
5259 | } | |
5260 | ||
1c2e9646 LP |
5261 | bool unit_type_supported(UnitType t) { |
5262 | if (_unlikely_(t < 0)) | |
5263 | return false; | |
5264 | if (_unlikely_(t >= _UNIT_TYPE_MAX)) | |
5265 | return false; | |
5266 | ||
5267 | if (!unit_vtable[t]->supported) | |
5268 | return true; | |
5269 | ||
5270 | return unit_vtable[t]->supported(); | |
5271 | } | |
5272 | ||
8b4305c7 LP |
5273 | void unit_warn_if_dir_nonempty(Unit *u, const char* where) { |
5274 | int r; | |
5275 | ||
5276 | assert(u); | |
5277 | assert(where); | |
5278 | ||
5279 | r = dir_is_empty(where); | |
3f602115 | 5280 | if (r > 0 || r == -ENOTDIR) |
8b4305c7 LP |
5281 | return; |
5282 | if (r < 0) { | |
5283 | log_unit_warning_errno(u, r, "Failed to check directory %s: %m", where); | |
5284 | return; | |
5285 | } | |
5286 | ||
5287 | log_struct(LOG_NOTICE, | |
2b044526 | 5288 | "MESSAGE_ID=" SD_MESSAGE_OVERMOUNTING_STR, |
8b4305c7 | 5289 | LOG_UNIT_ID(u), |
f1c50bec | 5290 | LOG_UNIT_INVOCATION_ID(u), |
8b4305c7 | 5291 | LOG_UNIT_MESSAGE(u, "Directory %s to mount over is not empty, mounting anyway.", where), |
a1230ff9 | 5292 | "WHERE=%s", where); |
8b4305c7 LP |
5293 | } |
5294 | ||
25cd4964 | 5295 | int unit_fail_if_noncanonical(Unit *u, const char* where) { |
58d9d89b | 5296 | _cleanup_free_ char *canonical_where = NULL; |
8b4305c7 LP |
5297 | int r; |
5298 | ||
5299 | assert(u); | |
5300 | assert(where); | |
5301 | ||
a5648b80 | 5302 | r = chase_symlinks(where, NULL, CHASE_NONEXISTENT, &canonical_where, NULL); |
8b4305c7 | 5303 | if (r < 0) { |
25cd4964 | 5304 | log_unit_debug_errno(u, r, "Failed to check %s for symlinks, ignoring: %m", where); |
8b4305c7 LP |
5305 | return 0; |
5306 | } | |
25cd4964 AJ |
5307 | |
5308 | /* We will happily ignore a trailing slash (or any redundant slashes) */ | |
5309 | if (path_equal(where, canonical_where)) | |
8b4305c7 LP |
5310 | return 0; |
5311 | ||
25cd4964 | 5312 | /* No need to mention "." or "..", they would already have been rejected by unit_name_from_path() */ |
8b4305c7 | 5313 | log_struct(LOG_ERR, |
2b044526 | 5314 | "MESSAGE_ID=" SD_MESSAGE_OVERMOUNTING_STR, |
8b4305c7 | 5315 | LOG_UNIT_ID(u), |
f1c50bec | 5316 | LOG_UNIT_INVOCATION_ID(u), |
25cd4964 | 5317 | LOG_UNIT_MESSAGE(u, "Mount path %s is not canonical (contains a symlink).", where), |
a1230ff9 | 5318 | "WHERE=%s", where); |
8b4305c7 LP |
5319 | |
5320 | return -ELOOP; | |
5321 | } | |
0f13f3bd LP |
5322 | |
5323 | bool unit_is_pristine(Unit *u) { | |
5324 | assert(u); | |
5325 | ||
7c65093a | 5326 | /* Check if the unit already exists or is already around, |
0f13f3bd LP |
5327 | * in a number of different ways. Note that to cater for unit |
5328 | * types such as slice, we are generally fine with units that | |
e9e8cbc8 ZJS |
5329 | * are marked UNIT_LOADED even though nothing was actually |
5330 | * loaded, as those unit types don't require a file on disk. */ | |
0f13f3bd LP |
5331 | |
5332 | return !(!IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_LOADED) || | |
5333 | u->fragment_path || | |
5334 | u->source_path || | |
5335 | !strv_isempty(u->dropin_paths) || | |
0f13f3bd LP |
5336 | u->job || |
5337 | u->merged_into); | |
5338 | } | |
291d565a LP |
5339 | |
5340 | pid_t unit_control_pid(Unit *u) { | |
5341 | assert(u); | |
5342 | ||
5343 | if (UNIT_VTABLE(u)->control_pid) | |
5344 | return UNIT_VTABLE(u)->control_pid(u); | |
5345 | ||
5346 | return 0; | |
5347 | } | |
5348 | ||
5349 | pid_t unit_main_pid(Unit *u) { | |
5350 | assert(u); | |
5351 | ||
5352 | if (UNIT_VTABLE(u)->main_pid) | |
5353 | return UNIT_VTABLE(u)->main_pid(u); | |
5354 | ||
5355 | return 0; | |
5356 | } | |
00d9ef85 LP |
5357 | |
5358 | static void unit_unref_uid_internal( | |
5359 | Unit *u, | |
5360 | uid_t *ref_uid, | |
5361 | bool destroy_now, | |
5362 | void (*_manager_unref_uid)(Manager *m, uid_t uid, bool destroy_now)) { | |
5363 | ||
5364 | assert(u); | |
5365 | assert(ref_uid); | |
5366 | assert(_manager_unref_uid); | |
5367 | ||
5368 | /* Generic implementation of both unit_unref_uid() and unit_unref_gid(), under the assumption that uid_t and | |
5369 | * gid_t are actually the same time, with the same validity rules. | |
5370 | * | |
5371 | * Drops a reference to UID/GID from a unit. */ | |
5372 | ||
5373 | assert_cc(sizeof(uid_t) == sizeof(gid_t)); | |
5374 | assert_cc(UID_INVALID == (uid_t) GID_INVALID); | |
5375 | ||
5376 | if (!uid_is_valid(*ref_uid)) | |
5377 | return; | |
5378 | ||
5379 | _manager_unref_uid(u->manager, *ref_uid, destroy_now); | |
5380 | *ref_uid = UID_INVALID; | |
5381 | } | |
5382 | ||
b90cf102 | 5383 | static void unit_unref_uid(Unit *u, bool destroy_now) { |
00d9ef85 LP |
5384 | unit_unref_uid_internal(u, &u->ref_uid, destroy_now, manager_unref_uid); |
5385 | } | |
5386 | ||
b90cf102 | 5387 | static void unit_unref_gid(Unit *u, bool destroy_now) { |
00d9ef85 LP |
5388 | unit_unref_uid_internal(u, (uid_t*) &u->ref_gid, destroy_now, manager_unref_gid); |
5389 | } | |
5390 | ||
b90cf102 LP |
5391 | void unit_unref_uid_gid(Unit *u, bool destroy_now) { |
5392 | assert(u); | |
5393 | ||
5394 | unit_unref_uid(u, destroy_now); | |
5395 | unit_unref_gid(u, destroy_now); | |
5396 | } | |
5397 | ||
00d9ef85 LP |
5398 | static int unit_ref_uid_internal( |
5399 | Unit *u, | |
5400 | uid_t *ref_uid, | |
5401 | uid_t uid, | |
5402 | bool clean_ipc, | |
5403 | int (*_manager_ref_uid)(Manager *m, uid_t uid, bool clean_ipc)) { | |
5404 | ||
5405 | int r; | |
5406 | ||
5407 | assert(u); | |
5408 | assert(ref_uid); | |
5409 | assert(uid_is_valid(uid)); | |
5410 | assert(_manager_ref_uid); | |
5411 | ||
5412 | /* Generic implementation of both unit_ref_uid() and unit_ref_guid(), under the assumption that uid_t and gid_t | |
5413 | * are actually the same type, and have the same validity rules. | |
5414 | * | |
5415 | * Adds a reference on a specific UID/GID to this unit. Each unit referencing the same UID/GID maintains a | |
5416 | * reference so that we can destroy the UID/GID's IPC resources as soon as this is requested and the counter | |
5417 | * drops to zero. */ | |
5418 | ||
5419 | assert_cc(sizeof(uid_t) == sizeof(gid_t)); | |
5420 | assert_cc(UID_INVALID == (uid_t) GID_INVALID); | |
5421 | ||
5422 | if (*ref_uid == uid) | |
5423 | return 0; | |
5424 | ||
5425 | if (uid_is_valid(*ref_uid)) /* Already set? */ | |
5426 | return -EBUSY; | |
5427 | ||
5428 | r = _manager_ref_uid(u->manager, uid, clean_ipc); | |
5429 | if (r < 0) | |
5430 | return r; | |
5431 | ||
5432 | *ref_uid = uid; | |
5433 | return 1; | |
5434 | } | |
5435 | ||
b90cf102 | 5436 | static int unit_ref_uid(Unit *u, uid_t uid, bool clean_ipc) { |
00d9ef85 LP |
5437 | return unit_ref_uid_internal(u, &u->ref_uid, uid, clean_ipc, manager_ref_uid); |
5438 | } | |
5439 | ||
b90cf102 | 5440 | static int unit_ref_gid(Unit *u, gid_t gid, bool clean_ipc) { |
00d9ef85 LP |
5441 | return unit_ref_uid_internal(u, (uid_t*) &u->ref_gid, (uid_t) gid, clean_ipc, manager_ref_gid); |
5442 | } | |
5443 | ||
5444 | static int unit_ref_uid_gid_internal(Unit *u, uid_t uid, gid_t gid, bool clean_ipc) { | |
5445 | int r = 0, q = 0; | |
5446 | ||
5447 | assert(u); | |
5448 | ||
5449 | /* Reference both a UID and a GID in one go. Either references both, or neither. */ | |
5450 | ||
5451 | if (uid_is_valid(uid)) { | |
5452 | r = unit_ref_uid(u, uid, clean_ipc); | |
5453 | if (r < 0) | |
5454 | return r; | |
5455 | } | |
5456 | ||
5457 | if (gid_is_valid(gid)) { | |
5458 | q = unit_ref_gid(u, gid, clean_ipc); | |
5459 | if (q < 0) { | |
5460 | if (r > 0) | |
5461 | unit_unref_uid(u, false); | |
5462 | ||
5463 | return q; | |
5464 | } | |
5465 | } | |
5466 | ||
5467 | return r > 0 || q > 0; | |
5468 | } | |
5469 | ||
5470 | int unit_ref_uid_gid(Unit *u, uid_t uid, gid_t gid) { | |
5471 | ExecContext *c; | |
5472 | int r; | |
5473 | ||
5474 | assert(u); | |
5475 | ||
5476 | c = unit_get_exec_context(u); | |
5477 | ||
5478 | r = unit_ref_uid_gid_internal(u, uid, gid, c ? c->remove_ipc : false); | |
5479 | if (r < 0) | |
5480 | return log_unit_warning_errno(u, r, "Couldn't add UID/GID reference to unit, proceeding without: %m"); | |
5481 | ||
5482 | return r; | |
5483 | } | |
5484 | ||
00d9ef85 LP |
5485 | void unit_notify_user_lookup(Unit *u, uid_t uid, gid_t gid) { |
5486 | int r; | |
5487 | ||
5488 | assert(u); | |
5489 | ||
5490 | /* This is invoked whenever one of the forked off processes let's us know the UID/GID its user name/group names | |
5491 | * resolved to. We keep track of which UID/GID is currently assigned in order to be able to destroy its IPC | |
5492 | * objects when no service references the UID/GID anymore. */ | |
5493 | ||
5494 | r = unit_ref_uid_gid(u, uid, gid); | |
5495 | if (r > 0) | |
37d0b962 | 5496 | unit_add_to_dbus_queue(u); |
00d9ef85 | 5497 | } |
4b58153d | 5498 | |
4b58153d LP |
5499 | int unit_acquire_invocation_id(Unit *u) { |
5500 | sd_id128_t id; | |
5501 | int r; | |
5502 | ||
5503 | assert(u); | |
5504 | ||
5505 | r = sd_id128_randomize(&id); | |
5506 | if (r < 0) | |
5507 | return log_unit_error_errno(u, r, "Failed to generate invocation ID for unit: %m"); | |
5508 | ||
5509 | r = unit_set_invocation_id(u, id); | |
5510 | if (r < 0) | |
5511 | return log_unit_error_errno(u, r, "Failed to set invocation ID for unit: %m"); | |
5512 | ||
af92c603 | 5513 | unit_add_to_dbus_queue(u); |
4b58153d LP |
5514 | return 0; |
5515 | } | |
f0d47797 | 5516 | |
1ad6e8b3 LP |
5517 | int unit_set_exec_params(Unit *u, ExecParameters *p) { |
5518 | int r; | |
5519 | ||
7960b0c7 LP |
5520 | assert(u); |
5521 | assert(p); | |
f0d47797 | 5522 | |
004c7f16 | 5523 | /* Copy parameters from manager */ |
1ad6e8b3 LP |
5524 | r = manager_get_effective_environment(u->manager, &p->environment); |
5525 | if (r < 0) | |
5526 | return r; | |
5527 | ||
004c7f16 LP |
5528 | p->confirm_spawn = manager_get_confirm_spawn(u->manager); |
5529 | p->cgroup_supported = u->manager->cgroup_supported; | |
5530 | p->prefix = u->manager->prefix; | |
5531 | SET_FLAG(p->flags, EXEC_PASS_LOG_UNIT|EXEC_CHOWN_DIRECTORIES, MANAGER_IS_SYSTEM(u->manager)); | |
5532 | ||
5238e957 | 5533 | /* Copy parameters from unit */ |
7960b0c7 | 5534 | p->cgroup_path = u->cgroup_path; |
1d9cc876 | 5535 | SET_FLAG(p->flags, EXEC_CGROUP_DELEGATE, unit_cgroup_delegate(u)); |
1ad6e8b3 | 5536 | |
bb0c0d6f LP |
5537 | p->received_credentials = u->manager->received_credentials; |
5538 | ||
1ad6e8b3 | 5539 | return 0; |
f0d47797 | 5540 | } |
a79279c7 | 5541 | |
4c253ed1 | 5542 | int unit_fork_helper_process(Unit *u, const char *name, pid_t *ret) { |
a79279c7 LP |
5543 | int r; |
5544 | ||
5545 | assert(u); | |
5546 | assert(ret); | |
5547 | ||
5548 | /* Forks off a helper process and makes sure it is a member of the unit's cgroup. Returns == 0 in the child, | |
5549 | * and > 0 in the parent. The pid parameter is always filled in with the child's PID. */ | |
5550 | ||
5551 | (void) unit_realize_cgroup(u); | |
5552 | ||
4c253ed1 LP |
5553 | r = safe_fork(name, FORK_REOPEN_LOG, ret); |
5554 | if (r != 0) | |
5555 | return r; | |
a79279c7 | 5556 | |
4c253ed1 LP |
5557 | (void) default_signals(SIGNALS_CRASH_HANDLER, SIGNALS_IGNORE, -1); |
5558 | (void) ignore_signals(SIGPIPE, -1); | |
a79279c7 | 5559 | |
4c253ed1 | 5560 | (void) prctl(PR_SET_PDEATHSIG, SIGTERM); |
a79279c7 | 5561 | |
4c253ed1 LP |
5562 | if (u->cgroup_path) { |
5563 | r = cg_attach_everywhere(u->manager->cgroup_supported, u->cgroup_path, 0, NULL, NULL); | |
5564 | if (r < 0) { | |
5565 | log_unit_error_errno(u, r, "Failed to join unit cgroup %s: %m", u->cgroup_path); | |
5566 | _exit(EXIT_CGROUP); | |
a79279c7 | 5567 | } |
a79279c7 LP |
5568 | } |
5569 | ||
4c253ed1 | 5570 | return 0; |
a79279c7 | 5571 | } |
c999cf38 | 5572 | |
810ef318 YW |
5573 | int unit_fork_and_watch_rm_rf(Unit *u, char **paths, pid_t *ret_pid) { |
5574 | pid_t pid; | |
5575 | int r; | |
5576 | ||
5577 | assert(u); | |
5578 | assert(ret_pid); | |
5579 | ||
5580 | r = unit_fork_helper_process(u, "(sd-rmrf)", &pid); | |
5581 | if (r < 0) | |
5582 | return r; | |
5583 | if (r == 0) { | |
5584 | int ret = EXIT_SUCCESS; | |
5585 | char **i; | |
5586 | ||
5587 | STRV_FOREACH(i, paths) { | |
5588 | r = rm_rf(*i, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_MISSING_OK); | |
5589 | if (r < 0) { | |
5590 | log_error_errno(r, "Failed to remove '%s': %m", *i); | |
5591 | ret = EXIT_FAILURE; | |
5592 | } | |
5593 | } | |
5594 | ||
5595 | _exit(ret); | |
5596 | } | |
5597 | ||
5598 | r = unit_watch_pid(u, pid, true); | |
5599 | if (r < 0) | |
5600 | return r; | |
5601 | ||
5602 | *ret_pid = pid; | |
5603 | return 0; | |
5604 | } | |
5605 | ||
c999cf38 LP |
5606 | static void unit_update_dependency_mask(Unit *u, UnitDependency d, Unit *other, UnitDependencyInfo di) { |
5607 | assert(u); | |
5608 | assert(d >= 0); | |
5609 | assert(d < _UNIT_DEPENDENCY_MAX); | |
5610 | assert(other); | |
5611 | ||
5612 | if (di.origin_mask == 0 && di.destination_mask == 0) { | |
5613 | /* No bit set anymore, let's drop the whole entry */ | |
5614 | assert_se(hashmap_remove(u->dependencies[d], other)); | |
29a743f9 | 5615 | log_unit_debug(u, "lost dependency %s=%s", unit_dependency_to_string(d), other->id); |
c999cf38 LP |
5616 | } else |
5617 | /* Mask was reduced, let's update the entry */ | |
5618 | assert_se(hashmap_update(u->dependencies[d], other, di.data) == 0); | |
5619 | } | |
5620 | ||
5621 | void unit_remove_dependencies(Unit *u, UnitDependencyMask mask) { | |
c999cf38 LP |
5622 | assert(u); |
5623 | ||
5624 | /* Removes all dependencies u has on other units marked for ownership by 'mask'. */ | |
5625 | ||
5626 | if (mask == 0) | |
5627 | return; | |
5628 | ||
f6173cb9 | 5629 | for (UnitDependency d = 0; d < _UNIT_DEPENDENCY_MAX; d++) { |
c999cf38 LP |
5630 | bool done; |
5631 | ||
5632 | do { | |
5633 | UnitDependencyInfo di; | |
5634 | Unit *other; | |
c999cf38 LP |
5635 | |
5636 | done = true; | |
5637 | ||
90e74a66 | 5638 | HASHMAP_FOREACH_KEY(di.data, other, u->dependencies[d]) { |
1d6cc5d0 | 5639 | if (FLAGS_SET(~mask, di.origin_mask)) |
c999cf38 LP |
5640 | continue; |
5641 | di.origin_mask &= ~mask; | |
5642 | unit_update_dependency_mask(u, d, other, di); | |
5643 | ||
5644 | /* We updated the dependency from our unit to the other unit now. But most dependencies | |
5645 | * imply a reverse dependency. Hence, let's delete that one too. For that we go through | |
5646 | * all dependency types on the other unit and delete all those which point to us and | |
5647 | * have the right mask set. */ | |
5648 | ||
f6173cb9 | 5649 | for (UnitDependency q = 0; q < _UNIT_DEPENDENCY_MAX; q++) { |
c999cf38 LP |
5650 | UnitDependencyInfo dj; |
5651 | ||
5652 | dj.data = hashmap_get(other->dependencies[q], u); | |
1d6cc5d0 | 5653 | if (FLAGS_SET(~mask, dj.destination_mask)) |
c999cf38 LP |
5654 | continue; |
5655 | dj.destination_mask &= ~mask; | |
5656 | ||
5657 | unit_update_dependency_mask(other, q, u, dj); | |
5658 | } | |
5659 | ||
5660 | unit_add_to_gc_queue(other); | |
5661 | ||
5662 | done = false; | |
5663 | break; | |
5664 | } | |
5665 | ||
5666 | } while (!done); | |
5667 | } | |
5668 | } | |
d3070fbd | 5669 | |
2f8c48b6 AZ |
5670 | static int unit_get_invocation_path(Unit *u, char **ret) { |
5671 | char *p; | |
5672 | int r; | |
5673 | ||
5674 | assert(u); | |
5675 | assert(ret); | |
5676 | ||
5677 | if (MANAGER_IS_SYSTEM(u->manager)) | |
5678 | p = strjoin("/run/systemd/units/invocation:", u->id); | |
5679 | else { | |
5680 | _cleanup_free_ char *user_path = NULL; | |
5681 | r = xdg_user_runtime_dir(&user_path, "/systemd/units/invocation:"); | |
5682 | if (r < 0) | |
5683 | return r; | |
5684 | p = strjoin(user_path, u->id); | |
5685 | } | |
5686 | ||
5687 | if (!p) | |
5688 | return -ENOMEM; | |
5689 | ||
5690 | *ret = p; | |
5691 | return 0; | |
5692 | } | |
5693 | ||
d3070fbd | 5694 | static int unit_export_invocation_id(Unit *u) { |
2f8c48b6 | 5695 | _cleanup_free_ char *p = NULL; |
d3070fbd LP |
5696 | int r; |
5697 | ||
5698 | assert(u); | |
5699 | ||
5700 | if (u->exported_invocation_id) | |
5701 | return 0; | |
5702 | ||
5703 | if (sd_id128_is_null(u->invocation_id)) | |
5704 | return 0; | |
5705 | ||
2f8c48b6 AZ |
5706 | r = unit_get_invocation_path(u, &p); |
5707 | if (r < 0) | |
5708 | return log_unit_debug_errno(u, r, "Failed to get invocation path: %m"); | |
5709 | ||
a3f5fd96 | 5710 | r = symlink_atomic_label(u->invocation_id_string, p); |
d3070fbd LP |
5711 | if (r < 0) |
5712 | return log_unit_debug_errno(u, r, "Failed to create invocation ID symlink %s: %m", p); | |
5713 | ||
5714 | u->exported_invocation_id = true; | |
5715 | return 0; | |
5716 | } | |
5717 | ||
5718 | static int unit_export_log_level_max(Unit *u, const ExecContext *c) { | |
5719 | const char *p; | |
5720 | char buf[2]; | |
5721 | int r; | |
5722 | ||
5723 | assert(u); | |
5724 | assert(c); | |
5725 | ||
5726 | if (u->exported_log_level_max) | |
5727 | return 0; | |
5728 | ||
5729 | if (c->log_level_max < 0) | |
5730 | return 0; | |
5731 | ||
5732 | assert(c->log_level_max <= 7); | |
5733 | ||
5734 | buf[0] = '0' + c->log_level_max; | |
5735 | buf[1] = 0; | |
5736 | ||
5737 | p = strjoina("/run/systemd/units/log-level-max:", u->id); | |
5738 | r = symlink_atomic(buf, p); | |
5739 | if (r < 0) | |
5740 | return log_unit_debug_errno(u, r, "Failed to create maximum log level symlink %s: %m", p); | |
5741 | ||
5742 | u->exported_log_level_max = true; | |
5743 | return 0; | |
5744 | } | |
5745 | ||
5746 | static int unit_export_log_extra_fields(Unit *u, const ExecContext *c) { | |
5747 | _cleanup_close_ int fd = -1; | |
5748 | struct iovec *iovec; | |
5749 | const char *p; | |
5750 | char *pattern; | |
5751 | le64_t *sizes; | |
5752 | ssize_t n; | |
d3070fbd LP |
5753 | int r; |
5754 | ||
5755 | if (u->exported_log_extra_fields) | |
5756 | return 0; | |
5757 | ||
5758 | if (c->n_log_extra_fields <= 0) | |
5759 | return 0; | |
5760 | ||
5761 | sizes = newa(le64_t, c->n_log_extra_fields); | |
5762 | iovec = newa(struct iovec, c->n_log_extra_fields * 2); | |
5763 | ||
12375b95 | 5764 | for (size_t i = 0; i < c->n_log_extra_fields; i++) { |
d3070fbd LP |
5765 | sizes[i] = htole64(c->log_extra_fields[i].iov_len); |
5766 | ||
5767 | iovec[i*2] = IOVEC_MAKE(sizes + i, sizeof(le64_t)); | |
5768 | iovec[i*2+1] = c->log_extra_fields[i]; | |
5769 | } | |
5770 | ||
5771 | p = strjoina("/run/systemd/units/log-extra-fields:", u->id); | |
5772 | pattern = strjoina(p, ".XXXXXX"); | |
5773 | ||
5774 | fd = mkostemp_safe(pattern); | |
5775 | if (fd < 0) | |
5776 | return log_unit_debug_errno(u, fd, "Failed to create extra fields file %s: %m", p); | |
5777 | ||
5778 | n = writev(fd, iovec, c->n_log_extra_fields*2); | |
5779 | if (n < 0) { | |
5780 | r = log_unit_debug_errno(u, errno, "Failed to write extra fields: %m"); | |
5781 | goto fail; | |
5782 | } | |
5783 | ||
5784 | (void) fchmod(fd, 0644); | |
5785 | ||
5786 | if (rename(pattern, p) < 0) { | |
5787 | r = log_unit_debug_errno(u, errno, "Failed to rename extra fields file: %m"); | |
5788 | goto fail; | |
5789 | } | |
5790 | ||
5791 | u->exported_log_extra_fields = true; | |
5792 | return 0; | |
5793 | ||
5794 | fail: | |
5795 | (void) unlink(pattern); | |
5796 | return r; | |
5797 | } | |
5798 | ||
5ac1530e | 5799 | static int unit_export_log_ratelimit_interval(Unit *u, const ExecContext *c) { |
90fc172e AZ |
5800 | _cleanup_free_ char *buf = NULL; |
5801 | const char *p; | |
5802 | int r; | |
5803 | ||
5804 | assert(u); | |
5805 | assert(c); | |
5806 | ||
5ac1530e | 5807 | if (u->exported_log_ratelimit_interval) |
90fc172e AZ |
5808 | return 0; |
5809 | ||
5ac1530e | 5810 | if (c->log_ratelimit_interval_usec == 0) |
90fc172e AZ |
5811 | return 0; |
5812 | ||
5813 | p = strjoina("/run/systemd/units/log-rate-limit-interval:", u->id); | |
5814 | ||
5ac1530e | 5815 | if (asprintf(&buf, "%" PRIu64, c->log_ratelimit_interval_usec) < 0) |
90fc172e AZ |
5816 | return log_oom(); |
5817 | ||
5818 | r = symlink_atomic(buf, p); | |
5819 | if (r < 0) | |
5820 | return log_unit_debug_errno(u, r, "Failed to create log rate limit interval symlink %s: %m", p); | |
5821 | ||
5ac1530e | 5822 | u->exported_log_ratelimit_interval = true; |
90fc172e AZ |
5823 | return 0; |
5824 | } | |
5825 | ||
5ac1530e | 5826 | static int unit_export_log_ratelimit_burst(Unit *u, const ExecContext *c) { |
90fc172e AZ |
5827 | _cleanup_free_ char *buf = NULL; |
5828 | const char *p; | |
5829 | int r; | |
5830 | ||
5831 | assert(u); | |
5832 | assert(c); | |
5833 | ||
5ac1530e | 5834 | if (u->exported_log_ratelimit_burst) |
90fc172e AZ |
5835 | return 0; |
5836 | ||
5ac1530e | 5837 | if (c->log_ratelimit_burst == 0) |
90fc172e AZ |
5838 | return 0; |
5839 | ||
5840 | p = strjoina("/run/systemd/units/log-rate-limit-burst:", u->id); | |
5841 | ||
5ac1530e | 5842 | if (asprintf(&buf, "%u", c->log_ratelimit_burst) < 0) |
90fc172e AZ |
5843 | return log_oom(); |
5844 | ||
5845 | r = symlink_atomic(buf, p); | |
5846 | if (r < 0) | |
5847 | return log_unit_debug_errno(u, r, "Failed to create log rate limit burst symlink %s: %m", p); | |
5848 | ||
5ac1530e | 5849 | u->exported_log_ratelimit_burst = true; |
90fc172e AZ |
5850 | return 0; |
5851 | } | |
5852 | ||
d3070fbd LP |
5853 | void unit_export_state_files(Unit *u) { |
5854 | const ExecContext *c; | |
5855 | ||
5856 | assert(u); | |
5857 | ||
5858 | if (!u->id) | |
5859 | return; | |
5860 | ||
638cece4 | 5861 | if (MANAGER_IS_TEST_RUN(u->manager)) |
8f632531 LP |
5862 | return; |
5863 | ||
d3070fbd LP |
5864 | /* Exports a couple of unit properties to /run/systemd/units/, so that journald can quickly query this data |
5865 | * from there. Ideally, journald would use IPC to query this, like everybody else, but that's hard, as long as | |
5866 | * the IPC system itself and PID 1 also log to the journal. | |
5867 | * | |
5868 | * Note that these files really shouldn't be considered API for anyone else, as use a runtime file system as | |
5869 | * IPC replacement is not compatible with today's world of file system namespaces. However, this doesn't really | |
5870 | * apply to communication between the journal and systemd, as we assume that these two daemons live in the same | |
5871 | * namespace at least. | |
5872 | * | |
5873 | * Note that some of the "files" exported here are actually symlinks and not regular files. Symlinks work | |
5874 | * better for storing small bits of data, in particular as we can write them with two system calls, and read | |
5875 | * them with one. */ | |
5876 | ||
5877 | (void) unit_export_invocation_id(u); | |
5878 | ||
2f8c48b6 AZ |
5879 | if (!MANAGER_IS_SYSTEM(u->manager)) |
5880 | return; | |
5881 | ||
d3070fbd LP |
5882 | c = unit_get_exec_context(u); |
5883 | if (c) { | |
5884 | (void) unit_export_log_level_max(u, c); | |
5885 | (void) unit_export_log_extra_fields(u, c); | |
5ac1530e ZJS |
5886 | (void) unit_export_log_ratelimit_interval(u, c); |
5887 | (void) unit_export_log_ratelimit_burst(u, c); | |
d3070fbd LP |
5888 | } |
5889 | } | |
5890 | ||
5891 | void unit_unlink_state_files(Unit *u) { | |
5892 | const char *p; | |
5893 | ||
5894 | assert(u); | |
5895 | ||
5896 | if (!u->id) | |
5897 | return; | |
5898 | ||
d3070fbd LP |
5899 | /* Undoes the effect of unit_export_state() */ |
5900 | ||
5901 | if (u->exported_invocation_id) { | |
2f8c48b6 AZ |
5902 | _cleanup_free_ char *invocation_path = NULL; |
5903 | int r = unit_get_invocation_path(u, &invocation_path); | |
5904 | if (r >= 0) { | |
5905 | (void) unlink(invocation_path); | |
5906 | u->exported_invocation_id = false; | |
5907 | } | |
d3070fbd LP |
5908 | } |
5909 | ||
2f8c48b6 AZ |
5910 | if (!MANAGER_IS_SYSTEM(u->manager)) |
5911 | return; | |
5912 | ||
d3070fbd LP |
5913 | if (u->exported_log_level_max) { |
5914 | p = strjoina("/run/systemd/units/log-level-max:", u->id); | |
5915 | (void) unlink(p); | |
5916 | ||
5917 | u->exported_log_level_max = false; | |
5918 | } | |
5919 | ||
5920 | if (u->exported_log_extra_fields) { | |
5921 | p = strjoina("/run/systemd/units/extra-fields:", u->id); | |
5922 | (void) unlink(p); | |
5923 | ||
5924 | u->exported_log_extra_fields = false; | |
5925 | } | |
90fc172e | 5926 | |
5ac1530e | 5927 | if (u->exported_log_ratelimit_interval) { |
90fc172e AZ |
5928 | p = strjoina("/run/systemd/units/log-rate-limit-interval:", u->id); |
5929 | (void) unlink(p); | |
5930 | ||
5ac1530e | 5931 | u->exported_log_ratelimit_interval = false; |
90fc172e AZ |
5932 | } |
5933 | ||
5ac1530e | 5934 | if (u->exported_log_ratelimit_burst) { |
90fc172e AZ |
5935 | p = strjoina("/run/systemd/units/log-rate-limit-burst:", u->id); |
5936 | (void) unlink(p); | |
5937 | ||
5ac1530e | 5938 | u->exported_log_ratelimit_burst = false; |
90fc172e | 5939 | } |
d3070fbd | 5940 | } |
5afe510c | 5941 | |
3c7416b6 LP |
5942 | int unit_prepare_exec(Unit *u) { |
5943 | int r; | |
5944 | ||
5945 | assert(u); | |
5946 | ||
fab34748 KL |
5947 | /* Load any custom firewall BPF programs here once to test if they are existing and actually loadable. |
5948 | * Fail here early since later errors in the call chain unit_realize_cgroup to cgroup_context_apply are ignored. */ | |
5949 | r = bpf_firewall_load_custom(u); | |
5950 | if (r < 0) | |
5951 | return r; | |
5952 | ||
3c7416b6 LP |
5953 | /* Prepares everything so that we can fork of a process for this unit */ |
5954 | ||
5955 | (void) unit_realize_cgroup(u); | |
5956 | ||
5957 | if (u->reset_accounting) { | |
9b2559a1 | 5958 | (void) unit_reset_accounting(u); |
3c7416b6 LP |
5959 | u->reset_accounting = false; |
5960 | } | |
5961 | ||
5962 | unit_export_state_files(u); | |
5963 | ||
5964 | r = unit_setup_exec_runtime(u); | |
5965 | if (r < 0) | |
5966 | return r; | |
5967 | ||
5968 | r = unit_setup_dynamic_creds(u); | |
5969 | if (r < 0) | |
5970 | return r; | |
5971 | ||
5972 | return 0; | |
5973 | } | |
5974 | ||
4c425434 LP |
5975 | static bool ignore_leftover_process(const char *comm) { |
5976 | return comm && comm[0] == '('; /* Most likely our own helper process (PAM?), ignore */ | |
5977 | } | |
5978 | ||
5979 | int unit_log_leftover_process_start(pid_t pid, int sig, void *userdata) { | |
a4634b21 LP |
5980 | _cleanup_free_ char *comm = NULL; |
5981 | ||
5982 | (void) get_process_comm(pid, &comm); | |
5983 | ||
4c425434 | 5984 | if (ignore_leftover_process(comm)) |
c53d2d54 | 5985 | return 0; |
a4634b21 | 5986 | |
4c425434 LP |
5987 | /* During start we print a warning */ |
5988 | ||
a4634b21 LP |
5989 | log_unit_warning(userdata, |
5990 | "Found left-over process " PID_FMT " (%s) in control group while starting unit. Ignoring.\n" | |
5991 | "This usually indicates unclean termination of a previous run, or service implementation deficiencies.", | |
5992 | pid, strna(comm)); | |
c53d2d54 DB |
5993 | |
5994 | return 1; | |
a4634b21 LP |
5995 | } |
5996 | ||
4c425434 LP |
5997 | int unit_log_leftover_process_stop(pid_t pid, int sig, void *userdata) { |
5998 | _cleanup_free_ char *comm = NULL; | |
5999 | ||
6000 | (void) get_process_comm(pid, &comm); | |
6001 | ||
6002 | if (ignore_leftover_process(comm)) | |
6003 | return 0; | |
6004 | ||
6005 | /* During stop we only print an informational message */ | |
6006 | ||
6007 | log_unit_info(userdata, | |
6008 | "Unit process " PID_FMT " (%s) remains running after unit stopped.", | |
6009 | pid, strna(comm)); | |
6010 | ||
6011 | return 1; | |
6012 | } | |
6013 | ||
6014 | int unit_warn_leftover_processes(Unit *u, cg_kill_log_func_t log_func) { | |
a4634b21 LP |
6015 | assert(u); |
6016 | ||
6017 | (void) unit_pick_cgroup_path(u); | |
6018 | ||
6019 | if (!u->cgroup_path) | |
c53d2d54 | 6020 | return 0; |
a4634b21 | 6021 | |
4c425434 | 6022 | return cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, 0, 0, NULL, log_func, u); |
a4634b21 LP |
6023 | } |
6024 | ||
bb2c7685 LP |
6025 | bool unit_needs_console(Unit *u) { |
6026 | ExecContext *ec; | |
6027 | UnitActiveState state; | |
6028 | ||
6029 | assert(u); | |
6030 | ||
6031 | state = unit_active_state(u); | |
6032 | ||
6033 | if (UNIT_IS_INACTIVE_OR_FAILED(state)) | |
6034 | return false; | |
6035 | ||
6036 | if (UNIT_VTABLE(u)->needs_console) | |
6037 | return UNIT_VTABLE(u)->needs_console(u); | |
6038 | ||
6039 | /* If this unit type doesn't implement this call, let's use a generic fallback implementation: */ | |
6040 | ec = unit_get_exec_context(u); | |
6041 | if (!ec) | |
6042 | return false; | |
6043 | ||
6044 | return exec_context_may_touch_console(ec); | |
6045 | } | |
6046 | ||
f156e60c | 6047 | const char *unit_label_path(const Unit *u) { |
81e9871e LP |
6048 | const char *p; |
6049 | ||
f156e60c CG |
6050 | assert(u); |
6051 | ||
81e9871e LP |
6052 | /* Returns the file system path to use for MAC access decisions, i.e. the file to read the SELinux label off |
6053 | * when validating access checks. */ | |
6054 | ||
6055 | p = u->source_path ?: u->fragment_path; | |
6056 | if (!p) | |
6057 | return NULL; | |
6058 | ||
6059 | /* If a unit is masked, then don't read the SELinux label of /dev/null, as that really makes no sense */ | |
640f3b14 | 6060 | if (null_or_empty_path(p) > 0) |
81e9871e LP |
6061 | return NULL; |
6062 | ||
6063 | return p; | |
6064 | } | |
6065 | ||
6592b975 LP |
6066 | int unit_pid_attachable(Unit *u, pid_t pid, sd_bus_error *error) { |
6067 | int r; | |
6068 | ||
6069 | assert(u); | |
6070 | ||
6071 | /* Checks whether the specified PID is generally good for attaching, i.e. a valid PID, not our manager itself, | |
6072 | * and not a kernel thread either */ | |
6073 | ||
6074 | /* First, a simple range check */ | |
6075 | if (!pid_is_valid(pid)) | |
6076 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Process identifier " PID_FMT " is not valid.", pid); | |
6077 | ||
6078 | /* Some extra safety check */ | |
6079 | if (pid == 1 || pid == getpid_cached()) | |
3fe91079 | 6080 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Process " PID_FMT " is a manager process, refusing.", pid); |
6592b975 LP |
6081 | |
6082 | /* Don't even begin to bother with kernel threads */ | |
6083 | r = is_kernel_thread(pid); | |
6084 | if (r == -ESRCH) | |
6085 | return sd_bus_error_setf(error, SD_BUS_ERROR_UNIX_PROCESS_ID_UNKNOWN, "Process with ID " PID_FMT " does not exist.", pid); | |
6086 | if (r < 0) | |
6087 | return sd_bus_error_set_errnof(error, r, "Failed to determine whether process " PID_FMT " is a kernel thread: %m", pid); | |
6088 | if (r > 0) | |
6089 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Process " PID_FMT " is a kernel thread, refusing.", pid); | |
6090 | ||
6091 | return 0; | |
6092 | } | |
6093 | ||
523ee2d4 LP |
6094 | void unit_log_success(Unit *u) { |
6095 | assert(u); | |
6096 | ||
6097 | log_struct(LOG_INFO, | |
6098 | "MESSAGE_ID=" SD_MESSAGE_UNIT_SUCCESS_STR, | |
6099 | LOG_UNIT_ID(u), | |
6100 | LOG_UNIT_INVOCATION_ID(u), | |
6101 | LOG_UNIT_MESSAGE(u, "Succeeded.")); | |
6102 | } | |
6103 | ||
7c047d74 LP |
6104 | void unit_log_failure(Unit *u, const char *result) { |
6105 | assert(u); | |
6106 | assert(result); | |
6107 | ||
6108 | log_struct(LOG_WARNING, | |
6109 | "MESSAGE_ID=" SD_MESSAGE_UNIT_FAILURE_RESULT_STR, | |
6110 | LOG_UNIT_ID(u), | |
6111 | LOG_UNIT_INVOCATION_ID(u), | |
6112 | LOG_UNIT_MESSAGE(u, "Failed with result '%s'.", result), | |
6113 | "UNIT_RESULT=%s", result); | |
6114 | } | |
6115 | ||
31cd5f63 AZ |
6116 | void unit_log_skip(Unit *u, const char *result) { |
6117 | assert(u); | |
6118 | assert(result); | |
6119 | ||
6120 | log_struct(LOG_INFO, | |
6121 | "MESSAGE_ID=" SD_MESSAGE_UNIT_SKIPPED_STR, | |
6122 | LOG_UNIT_ID(u), | |
6123 | LOG_UNIT_INVOCATION_ID(u), | |
6124 | LOG_UNIT_MESSAGE(u, "Skipped due to '%s'.", result), | |
6125 | "UNIT_RESULT=%s", result); | |
6126 | } | |
6127 | ||
91bbd9b7 LP |
6128 | void unit_log_process_exit( |
6129 | Unit *u, | |
91bbd9b7 LP |
6130 | const char *kind, |
6131 | const char *command, | |
5cc2cd1c | 6132 | bool success, |
91bbd9b7 LP |
6133 | int code, |
6134 | int status) { | |
6135 | ||
5cc2cd1c ZJS |
6136 | int level; |
6137 | ||
91bbd9b7 LP |
6138 | assert(u); |
6139 | assert(kind); | |
6140 | ||
5cc2cd1c ZJS |
6141 | /* If this is a successful exit, let's log about the exit code on DEBUG level. If this is a failure |
6142 | * and the process exited on its own via exit(), then let's make this a NOTICE, under the assumption | |
6143 | * that the service already logged the reason at a higher log level on its own. Otherwise, make it a | |
6144 | * WARNING. */ | |
6145 | if (success) | |
6146 | level = LOG_DEBUG; | |
6147 | else if (code == CLD_EXITED) | |
6148 | level = LOG_NOTICE; | |
6149 | else | |
91bbd9b7 LP |
6150 | level = LOG_WARNING; |
6151 | ||
6152 | log_struct(level, | |
6153 | "MESSAGE_ID=" SD_MESSAGE_UNIT_PROCESS_EXIT_STR, | |
6154 | LOG_UNIT_MESSAGE(u, "%s exited, code=%s, status=%i/%s", | |
6155 | kind, | |
6156 | sigchld_code_to_string(code), status, | |
6157 | strna(code == CLD_EXITED | |
6158 | ? exit_status_to_string(status, EXIT_STATUS_FULL) | |
6159 | : signal_to_string(status))), | |
6160 | "EXIT_CODE=%s", sigchld_code_to_string(code), | |
6161 | "EXIT_STATUS=%i", status, | |
6162 | "COMMAND=%s", strna(command), | |
6163 | LOG_UNIT_ID(u), | |
6164 | LOG_UNIT_INVOCATION_ID(u)); | |
6165 | } | |
6166 | ||
7af67e9a LP |
6167 | int unit_exit_status(Unit *u) { |
6168 | assert(u); | |
6169 | ||
6170 | /* Returns the exit status to propagate for the most recent cycle of this unit. Returns a value in the range | |
6171 | * 0…255 if there's something to propagate. EOPNOTSUPP if the concept does not apply to this unit type, ENODATA | |
6172 | * if no data is currently known (for example because the unit hasn't deactivated yet) and EBADE if the main | |
6173 | * service process has exited abnormally (signal/coredump). */ | |
6174 | ||
6175 | if (!UNIT_VTABLE(u)->exit_status) | |
6176 | return -EOPNOTSUPP; | |
6177 | ||
6178 | return UNIT_VTABLE(u)->exit_status(u); | |
6179 | } | |
6180 | ||
6181 | int unit_failure_action_exit_status(Unit *u) { | |
6182 | int r; | |
6183 | ||
6184 | assert(u); | |
6185 | ||
6186 | /* Returns the exit status to propagate on failure, or an error if there's nothing to propagate */ | |
6187 | ||
6188 | if (u->failure_action_exit_status >= 0) | |
6189 | return u->failure_action_exit_status; | |
6190 | ||
6191 | r = unit_exit_status(u); | |
6192 | if (r == -EBADE) /* Exited, but not cleanly (i.e. by signal or such) */ | |
6193 | return 255; | |
6194 | ||
6195 | return r; | |
6196 | } | |
6197 | ||
6198 | int unit_success_action_exit_status(Unit *u) { | |
6199 | int r; | |
6200 | ||
6201 | assert(u); | |
6202 | ||
6203 | /* Returns the exit status to propagate on success, or an error if there's nothing to propagate */ | |
6204 | ||
6205 | if (u->success_action_exit_status >= 0) | |
6206 | return u->success_action_exit_status; | |
6207 | ||
6208 | r = unit_exit_status(u); | |
6209 | if (r == -EBADE) /* Exited, but not cleanly (i.e. by signal or such) */ | |
6210 | return 255; | |
6211 | ||
6212 | return r; | |
6213 | } | |
6214 | ||
a4191c9f LP |
6215 | int unit_test_trigger_loaded(Unit *u) { |
6216 | Unit *trigger; | |
6217 | ||
6218 | /* Tests whether the unit to trigger is loaded */ | |
6219 | ||
6220 | trigger = UNIT_TRIGGER(u); | |
6221 | if (!trigger) | |
e7b9f4d9 ZJS |
6222 | return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOENT), |
6223 | "Refusing to start, no unit to trigger."); | |
a4191c9f | 6224 | if (trigger->load_state != UNIT_LOADED) |
e7b9f4d9 ZJS |
6225 | return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOENT), |
6226 | "Refusing to start, unit %s to trigger not loaded.", trigger->id); | |
a4191c9f LP |
6227 | |
6228 | return 0; | |
6229 | } | |
6230 | ||
bb0c0d6f LP |
6231 | void unit_destroy_runtime_data(Unit *u, const ExecContext *context) { |
6232 | assert(u); | |
6233 | assert(context); | |
6234 | ||
95939aed YW |
6235 | if (context->runtime_directory_preserve_mode == EXEC_PRESERVE_NO || |
6236 | (context->runtime_directory_preserve_mode == EXEC_PRESERVE_RESTART && !unit_will_restart(u))) | |
6237 | exec_context_destroy_runtime_directory(context, u->manager->prefix[EXEC_DIRECTORY_RUNTIME]); | |
bb0c0d6f LP |
6238 | |
6239 | exec_context_destroy_credentials(context, u->manager->prefix[EXEC_DIRECTORY_RUNTIME], u->id); | |
95939aed YW |
6240 | } |
6241 | ||
380dc8b0 LP |
6242 | int unit_clean(Unit *u, ExecCleanMask mask) { |
6243 | UnitActiveState state; | |
6244 | ||
6245 | assert(u); | |
6246 | ||
6247 | /* Special return values: | |
6248 | * | |
6249 | * -EOPNOTSUPP → cleaning not supported for this unit type | |
6250 | * -EUNATCH → cleaning not defined for this resource type | |
6251 | * -EBUSY → unit currently can't be cleaned since it's running or not properly loaded, or has | |
6252 | * a job queued or similar | |
6253 | */ | |
6254 | ||
6255 | if (!UNIT_VTABLE(u)->clean) | |
6256 | return -EOPNOTSUPP; | |
6257 | ||
6258 | if (mask == 0) | |
6259 | return -EUNATCH; | |
6260 | ||
6261 | if (u->load_state != UNIT_LOADED) | |
6262 | return -EBUSY; | |
6263 | ||
6264 | if (u->job) | |
6265 | return -EBUSY; | |
6266 | ||
6267 | state = unit_active_state(u); | |
6268 | if (!IN_SET(state, UNIT_INACTIVE)) | |
6269 | return -EBUSY; | |
6270 | ||
6271 | return UNIT_VTABLE(u)->clean(u, mask); | |
6272 | } | |
6273 | ||
6274 | int unit_can_clean(Unit *u, ExecCleanMask *ret) { | |
6275 | assert(u); | |
6276 | ||
6277 | if (!UNIT_VTABLE(u)->clean || | |
6278 | u->load_state != UNIT_LOADED) { | |
6279 | *ret = 0; | |
6280 | return 0; | |
6281 | } | |
6282 | ||
6283 | /* When the clean() method is set, can_clean() really should be set too */ | |
6284 | assert(UNIT_VTABLE(u)->can_clean); | |
6285 | ||
6286 | return UNIT_VTABLE(u)->can_clean(u, ret); | |
6287 | } | |
6288 | ||
d9e45bc3 MS |
6289 | bool unit_can_freeze(Unit *u) { |
6290 | assert(u); | |
6291 | ||
6292 | if (UNIT_VTABLE(u)->can_freeze) | |
6293 | return UNIT_VTABLE(u)->can_freeze(u); | |
6294 | ||
6295 | return UNIT_VTABLE(u)->freeze; | |
6296 | } | |
6297 | ||
6298 | void unit_frozen(Unit *u) { | |
6299 | assert(u); | |
6300 | ||
6301 | u->freezer_state = FREEZER_FROZEN; | |
6302 | ||
6303 | bus_unit_send_pending_freezer_message(u); | |
6304 | } | |
6305 | ||
6306 | void unit_thawed(Unit *u) { | |
6307 | assert(u); | |
6308 | ||
6309 | u->freezer_state = FREEZER_RUNNING; | |
6310 | ||
6311 | bus_unit_send_pending_freezer_message(u); | |
6312 | } | |
6313 | ||
6314 | static int unit_freezer_action(Unit *u, FreezerAction action) { | |
6315 | UnitActiveState s; | |
6316 | int (*method)(Unit*); | |
6317 | int r; | |
6318 | ||
6319 | assert(u); | |
6320 | assert(IN_SET(action, FREEZER_FREEZE, FREEZER_THAW)); | |
6321 | ||
6322 | method = action == FREEZER_FREEZE ? UNIT_VTABLE(u)->freeze : UNIT_VTABLE(u)->thaw; | |
6323 | if (!method || !cg_freezer_supported()) | |
6324 | return -EOPNOTSUPP; | |
6325 | ||
6326 | if (u->job) | |
6327 | return -EBUSY; | |
6328 | ||
6329 | if (u->load_state != UNIT_LOADED) | |
6330 | return -EHOSTDOWN; | |
6331 | ||
6332 | s = unit_active_state(u); | |
6333 | if (s != UNIT_ACTIVE) | |
6334 | return -EHOSTDOWN; | |
6335 | ||
6336 | if (IN_SET(u->freezer_state, FREEZER_FREEZING, FREEZER_THAWING)) | |
6337 | return -EALREADY; | |
6338 | ||
6339 | r = method(u); | |
6340 | if (r <= 0) | |
6341 | return r; | |
6342 | ||
6343 | return 1; | |
6344 | } | |
6345 | ||
6346 | int unit_freeze(Unit *u) { | |
6347 | return unit_freezer_action(u, FREEZER_FREEZE); | |
6348 | } | |
6349 | ||
6350 | int unit_thaw(Unit *u) { | |
6351 | return unit_freezer_action(u, FREEZER_THAW); | |
6352 | } | |
6353 | ||
6354 | /* Wrappers around low-level cgroup freezer operations common for service and scope units */ | |
6355 | int unit_freeze_vtable_common(Unit *u) { | |
6356 | return unit_cgroup_freezer_action(u, FREEZER_FREEZE); | |
6357 | } | |
6358 | ||
6359 | int unit_thaw_vtable_common(Unit *u) { | |
6360 | return unit_cgroup_freezer_action(u, FREEZER_THAW); | |
6361 | } | |
6362 | ||
5afe510c LP |
6363 | static const char* const collect_mode_table[_COLLECT_MODE_MAX] = { |
6364 | [COLLECT_INACTIVE] = "inactive", | |
6365 | [COLLECT_INACTIVE_OR_FAILED] = "inactive-or-failed", | |
6366 | }; | |
6367 | ||
6368 | DEFINE_STRING_TABLE_LOOKUP(collect_mode, CollectMode); |