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