]>
Commit | Line | Data |
---|---|---|
a7334b09 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
7 | under the terms of the GNU Lesser General Public License as published by |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
a7334b09 LP |
9 | (at your option) any later version. |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 14 | Lesser General Public License for more details. |
a7334b09 | 15 | |
5430f7f2 | 16 | You should have received a copy of the GNU Lesser General Public License |
a7334b09 LP |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
18 | ***/ | |
19 | ||
87f0e418 | 20 | #include <errno.h> |
0301abf4 | 21 | #include <stdlib.h> |
4f5dd394 | 22 | #include <string.h> |
45fb0699 | 23 | #include <sys/stat.h> |
4f5dd394 | 24 | #include <unistd.h> |
87f0e418 | 25 | |
718db961 LP |
26 | #include "sd-id128.h" |
27 | #include "sd-messages.h" | |
4f5dd394 | 28 | |
b5efdb8a | 29 | #include "alloc-util.h" |
4f5dd394 LP |
30 | #include "bus-common-errors.h" |
31 | #include "bus-util.h" | |
c6c18be3 | 32 | #include "cgroup-util.h" |
4f5dd394 LP |
33 | #include "dbus-unit.h" |
34 | #include "dbus.h" | |
35 | #include "dropin.h" | |
36 | #include "escape.h" | |
37 | #include "execute.h" | |
a5c32cff | 38 | #include "fileio-label.h" |
f97b34a6 | 39 | #include "format-util.h" |
4b58153d | 40 | #include "id128-util.h" |
4f5dd394 LP |
41 | #include "load-dropin.h" |
42 | #include "load-fragment.h" | |
43 | #include "log.h" | |
44 | #include "macro.h" | |
45 | #include "missing.h" | |
46 | #include "mkdir.h" | |
6bedfcbb | 47 | #include "parse-util.h" |
4f5dd394 | 48 | #include "path-util.h" |
0b452006 | 49 | #include "process-util.h" |
4f5dd394 | 50 | #include "set.h" |
6eb7c172 | 51 | #include "signal-util.h" |
e9db43d5 | 52 | #include "special.h" |
8fcde012 | 53 | #include "stat-util.h" |
d054f0a4 | 54 | #include "stdio-util.h" |
07630cea | 55 | #include "string-util.h" |
4f5dd394 | 56 | #include "strv.h" |
4f4afc88 | 57 | #include "umask-util.h" |
4f5dd394 | 58 | #include "unit-name.h" |
e9db43d5 | 59 | #include "unit.h" |
b1d4f8e1 LP |
60 | #include "user-util.h" |
61 | #include "virt.h" | |
87f0e418 LP |
62 | |
63 | const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = { | |
64 | [UNIT_SERVICE] = &service_vtable, | |
87f0e418 | 65 | [UNIT_SOCKET] = &socket_vtable, |
e821075a | 66 | [UNIT_BUSNAME] = &busname_vtable, |
87f0e418 LP |
67 | [UNIT_TARGET] = &target_vtable, |
68 | [UNIT_DEVICE] = &device_vtable, | |
69 | [UNIT_MOUNT] = &mount_vtable, | |
70 | [UNIT_AUTOMOUNT] = &automount_vtable, | |
01f78473 | 71 | [UNIT_SWAP] = &swap_vtable, |
e821075a | 72 | [UNIT_TIMER] = &timer_vtable, |
a016b922 | 73 | [UNIT_PATH] = &path_vtable, |
6c12b52e LP |
74 | [UNIT_SLICE] = &slice_vtable, |
75 | [UNIT_SCOPE] = &scope_vtable | |
87f0e418 LP |
76 | }; |
77 | ||
f2341e0a | 78 | static void maybe_warn_about_dependency(Unit *u, const char *other, UnitDependency dependency); |
d1fab3fe | 79 | |
7d17cfbc | 80 | Unit *unit_new(Manager *m, size_t size) { |
87f0e418 LP |
81 | Unit *u; |
82 | ||
83 | assert(m); | |
ac155bb8 | 84 | assert(size >= sizeof(Unit)); |
87f0e418 | 85 | |
7d17cfbc MS |
86 | u = malloc0(size); |
87 | if (!u) | |
87f0e418 LP |
88 | return NULL; |
89 | ||
d5099efc | 90 | u->names = set_new(&string_hash_ops); |
6b430fdb ZJS |
91 | if (!u->names) |
92 | return mfree(u); | |
87f0e418 | 93 | |
ac155bb8 MS |
94 | u->manager = m; |
95 | u->type = _UNIT_TYPE_INVALID; | |
ac155bb8 MS |
96 | u->default_dependencies = true; |
97 | u->unit_file_state = _UNIT_FILE_STATE_INVALID; | |
d2dc52db | 98 | u->unit_file_preset = -1; |
d420282b | 99 | u->on_failure_job_mode = JOB_REPLACE; |
efdb0237 | 100 | u->cgroup_inotify_wd = -1; |
36c16a7c | 101 | u->job_timeout = USEC_INFINITY; |
00d9ef85 LP |
102 | u->ref_uid = UID_INVALID; |
103 | u->ref_gid = GID_INVALID; | |
fe700f46 | 104 | u->cpu_usage_last = NSEC_INFINITY; |
87f0e418 | 105 | |
6bf0f408 | 106 | RATELIMIT_INIT(u->start_limit, m->default_start_limit_interval, m->default_start_limit_burst); |
67bfdc97 | 107 | RATELIMIT_INIT(u->auto_stop_ratelimit, 10 * USEC_PER_SEC, 16); |
bea355da | 108 | |
87f0e418 LP |
109 | return u; |
110 | } | |
111 | ||
a581e45a LP |
112 | int unit_new_for_name(Manager *m, size_t size, const char *name, Unit **ret) { |
113 | Unit *u; | |
114 | int r; | |
115 | ||
116 | u = unit_new(m, size); | |
117 | if (!u) | |
118 | return -ENOMEM; | |
119 | ||
120 | r = unit_add_name(u, name); | |
121 | if (r < 0) { | |
122 | unit_free(u); | |
123 | return r; | |
124 | } | |
125 | ||
126 | *ret = u; | |
127 | return r; | |
128 | } | |
129 | ||
f278026d LP |
130 | bool unit_has_name(Unit *u, const char *name) { |
131 | assert(u); | |
132 | assert(name); | |
133 | ||
390bc2b1 | 134 | return set_contains(u->names, (char*) name); |
f278026d LP |
135 | } |
136 | ||
598459ce LP |
137 | static void unit_init(Unit *u) { |
138 | CGroupContext *cc; | |
139 | ExecContext *ec; | |
140 | KillContext *kc; | |
141 | ||
142 | assert(u); | |
143 | assert(u->manager); | |
144 | assert(u->type >= 0); | |
145 | ||
146 | cc = unit_get_cgroup_context(u); | |
147 | if (cc) { | |
148 | cgroup_context_init(cc); | |
149 | ||
150 | /* Copy in the manager defaults into the cgroup | |
151 | * context, _before_ the rest of the settings have | |
152 | * been initialized */ | |
153 | ||
154 | cc->cpu_accounting = u->manager->default_cpu_accounting; | |
13c31542 | 155 | cc->io_accounting = u->manager->default_io_accounting; |
598459ce LP |
156 | cc->blockio_accounting = u->manager->default_blockio_accounting; |
157 | cc->memory_accounting = u->manager->default_memory_accounting; | |
03a7b521 | 158 | cc->tasks_accounting = u->manager->default_tasks_accounting; |
0af20ea2 LP |
159 | |
160 | if (u->type != UNIT_SLICE) | |
161 | cc->tasks_max = u->manager->default_tasks_max; | |
598459ce LP |
162 | } |
163 | ||
164 | ec = unit_get_exec_context(u); | |
165 | if (ec) | |
166 | exec_context_init(ec); | |
167 | ||
168 | kc = unit_get_kill_context(u); | |
169 | if (kc) | |
170 | kill_context_init(kc); | |
171 | ||
172 | if (UNIT_VTABLE(u)->init) | |
173 | UNIT_VTABLE(u)->init(u); | |
174 | } | |
175 | ||
87f0e418 | 176 | int unit_add_name(Unit *u, const char *text) { |
598459ce | 177 | _cleanup_free_ char *s = NULL, *i = NULL; |
87f0e418 | 178 | UnitType t; |
87f0e418 LP |
179 | int r; |
180 | ||
181 | assert(u); | |
182 | assert(text); | |
183 | ||
7410616c | 184 | if (unit_name_is_valid(text, UNIT_NAME_TEMPLATE)) { |
598459ce | 185 | |
ac155bb8 | 186 | if (!u->instance) |
9e2f7c11 | 187 | return -EINVAL; |
87f0e418 | 188 | |
7410616c LP |
189 | r = unit_name_replace_instance(text, u->instance, &s); |
190 | if (r < 0) | |
191 | return r; | |
192 | } else { | |
9e2f7c11 | 193 | s = strdup(text); |
7410616c LP |
194 | if (!s) |
195 | return -ENOMEM; | |
196 | } | |
87f0e418 | 197 | |
7410616c LP |
198 | if (set_contains(u->names, s)) |
199 | return 0; | |
200 | if (hashmap_contains(u->manager->units, s)) | |
201 | return -EEXIST; | |
202 | ||
203 | if (!unit_name_is_valid(s, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) | |
598459ce | 204 | return -EINVAL; |
e537352b | 205 | |
7410616c LP |
206 | t = unit_name_to_type(s); |
207 | if (t < 0) | |
208 | return -EINVAL; | |
87f0e418 | 209 | |
598459ce LP |
210 | if (u->type != _UNIT_TYPE_INVALID && t != u->type) |
211 | return -EINVAL; | |
87f0e418 | 212 | |
e48614c4 ZJS |
213 | r = unit_name_to_instance(s, &i); |
214 | if (r < 0) | |
598459ce | 215 | return r; |
87f0e418 | 216 | |
ce99c68a | 217 | if (i && !unit_type_may_template(t)) |
598459ce | 218 | return -EINVAL; |
9e2f7c11 | 219 | |
276c3e78 | 220 | /* Ensure that this unit is either instanced or not instanced, |
7410616c LP |
221 | * but not both. Note that we do allow names with different |
222 | * instance names however! */ | |
598459ce LP |
223 | if (u->type != _UNIT_TYPE_INVALID && !u->instance != !i) |
224 | return -EINVAL; | |
9e2f7c11 | 225 | |
8a993b61 | 226 | if (!unit_type_may_alias(t) && !set_isempty(u->names)) |
598459ce | 227 | return -EEXIST; |
9e2f7c11 | 228 | |
598459ce LP |
229 | if (hashmap_size(u->manager->units) >= MANAGER_MAX_NAMES) |
230 | return -E2BIG; | |
4f0f902f | 231 | |
e48614c4 | 232 | r = set_put(u->names, s); |
7410616c | 233 | if (r < 0) |
598459ce | 234 | return r; |
7410616c | 235 | assert(r > 0); |
87f0e418 | 236 | |
e48614c4 ZJS |
237 | r = hashmap_put(u->manager->units, s, u); |
238 | if (r < 0) { | |
7410616c | 239 | (void) set_remove(u->names, s); |
598459ce | 240 | return r; |
87f0e418 LP |
241 | } |
242 | ||
ac155bb8 | 243 | if (u->type == _UNIT_TYPE_INVALID) { |
ac155bb8 MS |
244 | u->type = t; |
245 | u->id = s; | |
246 | u->instance = i; | |
9e2f7c11 | 247 | |
71fda00f | 248 | LIST_PREPEND(units_by_type, u->manager->units_by_type[t], u); |
e537352b | 249 | |
598459ce | 250 | unit_init(u); |
87f0e418 | 251 | |
598459ce LP |
252 | i = NULL; |
253 | } | |
9e2f7c11 | 254 | |
598459ce | 255 | s = NULL; |
9e2f7c11 | 256 | |
598459ce LP |
257 | unit_add_to_dbus_queue(u); |
258 | return 0; | |
87f0e418 LP |
259 | } |
260 | ||
0ae97ec1 | 261 | int unit_choose_id(Unit *u, const char *name) { |
68eda4bd | 262 | _cleanup_free_ char *t = NULL; |
598459ce | 263 | char *s, *i; |
276c3e78 | 264 | int r; |
0ae97ec1 LP |
265 | |
266 | assert(u); | |
267 | assert(name); | |
268 | ||
7410616c | 269 | if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
9e2f7c11 | 270 | |
ac155bb8 | 271 | if (!u->instance) |
9e2f7c11 LP |
272 | return -EINVAL; |
273 | ||
7410616c LP |
274 | r = unit_name_replace_instance(name, u->instance, &t); |
275 | if (r < 0) | |
276 | return r; | |
9e2f7c11 LP |
277 | |
278 | name = t; | |
279 | } | |
280 | ||
0ae97ec1 | 281 | /* Selects one of the names of this unit as the id */ |
ac155bb8 | 282 | s = set_get(u->names, (char*) name); |
9e2f7c11 | 283 | if (!s) |
0ae97ec1 LP |
284 | return -ENOENT; |
285 | ||
7410616c | 286 | /* Determine the new instance from the new id */ |
e48614c4 ZJS |
287 | r = unit_name_to_instance(s, &i); |
288 | if (r < 0) | |
276c3e78 LP |
289 | return r; |
290 | ||
ac155bb8 | 291 | u->id = s; |
276c3e78 | 292 | |
ac155bb8 MS |
293 | free(u->instance); |
294 | u->instance = i; | |
276c3e78 | 295 | |
c1e1601e | 296 | unit_add_to_dbus_queue(u); |
9e2f7c11 | 297 | |
0ae97ec1 LP |
298 | return 0; |
299 | } | |
300 | ||
f50e0a01 LP |
301 | int unit_set_description(Unit *u, const char *description) { |
302 | char *s; | |
303 | ||
304 | assert(u); | |
305 | ||
8aec412f LP |
306 | if (isempty(description)) |
307 | s = NULL; | |
308 | else { | |
309 | s = strdup(description); | |
310 | if (!s) | |
311 | return -ENOMEM; | |
312 | } | |
f50e0a01 | 313 | |
ac155bb8 MS |
314 | free(u->description); |
315 | u->description = s; | |
c1e1601e LP |
316 | |
317 | unit_add_to_dbus_queue(u); | |
f50e0a01 LP |
318 | return 0; |
319 | } | |
320 | ||
701cc384 | 321 | bool unit_check_gc(Unit *u) { |
a354329f | 322 | UnitActiveState state; |
f0bfbfac | 323 | bool inactive; |
701cc384 LP |
324 | assert(u); |
325 | ||
a354329f | 326 | if (u->job) |
701cc384 LP |
327 | return true; |
328 | ||
a354329f | 329 | if (u->nop_job) |
6c073082 LP |
330 | return true; |
331 | ||
a354329f | 332 | state = unit_active_state(u); |
f0bfbfac | 333 | inactive = state == UNIT_INACTIVE; |
a354329f LP |
334 | |
335 | /* If the unit is inactive and failed and no job is queued for | |
336 | * it, then release its runtime resources */ | |
337 | if (UNIT_IS_INACTIVE_OR_FAILED(state) && | |
338 | UNIT_VTABLE(u)->release_resources) | |
f0bfbfac | 339 | UNIT_VTABLE(u)->release_resources(u, inactive); |
a354329f LP |
340 | |
341 | /* But we keep the unit object around for longer when it is | |
342 | * referenced or configured to not be gc'ed */ | |
f0bfbfac | 343 | if (!inactive) |
701cc384 LP |
344 | return true; |
345 | ||
f5869324 | 346 | if (u->perpetual) |
701cc384 LP |
347 | return true; |
348 | ||
9d576438 LP |
349 | if (u->refs) |
350 | return true; | |
351 | ||
05a98afd LP |
352 | if (sd_bus_track_count(u->bus_track) > 0) |
353 | return true; | |
354 | ||
701cc384 LP |
355 | if (UNIT_VTABLE(u)->check_gc) |
356 | if (UNIT_VTABLE(u)->check_gc(u)) | |
357 | return true; | |
358 | ||
359 | return false; | |
360 | } | |
361 | ||
87f0e418 LP |
362 | void unit_add_to_load_queue(Unit *u) { |
363 | assert(u); | |
ac155bb8 | 364 | assert(u->type != _UNIT_TYPE_INVALID); |
87f0e418 | 365 | |
ac155bb8 | 366 | if (u->load_state != UNIT_STUB || u->in_load_queue) |
87f0e418 LP |
367 | return; |
368 | ||
71fda00f | 369 | LIST_PREPEND(load_queue, u->manager->load_queue, u); |
ac155bb8 | 370 | u->in_load_queue = true; |
87f0e418 LP |
371 | } |
372 | ||
23a177ef LP |
373 | void unit_add_to_cleanup_queue(Unit *u) { |
374 | assert(u); | |
375 | ||
ac155bb8 | 376 | if (u->in_cleanup_queue) |
23a177ef LP |
377 | return; |
378 | ||
71fda00f | 379 | LIST_PREPEND(cleanup_queue, u->manager->cleanup_queue, u); |
ac155bb8 | 380 | u->in_cleanup_queue = true; |
23a177ef LP |
381 | } |
382 | ||
701cc384 LP |
383 | void unit_add_to_gc_queue(Unit *u) { |
384 | assert(u); | |
385 | ||
ac155bb8 | 386 | if (u->in_gc_queue || u->in_cleanup_queue) |
701cc384 LP |
387 | return; |
388 | ||
389 | if (unit_check_gc(u)) | |
390 | return; | |
391 | ||
c5a97ed1 | 392 | LIST_PREPEND(gc_queue, u->manager->gc_unit_queue, u); |
ac155bb8 | 393 | u->in_gc_queue = true; |
701cc384 LP |
394 | } |
395 | ||
c1e1601e LP |
396 | void unit_add_to_dbus_queue(Unit *u) { |
397 | assert(u); | |
ac155bb8 | 398 | assert(u->type != _UNIT_TYPE_INVALID); |
c1e1601e | 399 | |
ac155bb8 | 400 | if (u->load_state == UNIT_STUB || u->in_dbus_queue) |
c1e1601e LP |
401 | return; |
402 | ||
a567261a | 403 | /* Shortcut things if nobody cares */ |
8f8f05a9 LP |
404 | if (sd_bus_track_count(u->manager->subscribed) <= 0 && |
405 | set_isempty(u->manager->private_buses)) { | |
ac155bb8 | 406 | u->sent_dbus_new_signal = true; |
94b6dfa2 LP |
407 | return; |
408 | } | |
409 | ||
71fda00f | 410 | LIST_PREPEND(dbus_queue, u->manager->dbus_unit_queue, u); |
ac155bb8 | 411 | u->in_dbus_queue = true; |
c1e1601e LP |
412 | } |
413 | ||
87f0e418 LP |
414 | static void bidi_set_free(Unit *u, Set *s) { |
415 | Iterator i; | |
416 | Unit *other; | |
417 | ||
418 | assert(u); | |
419 | ||
420 | /* Frees the set and makes sure we are dropped from the | |
421 | * inverse pointers */ | |
422 | ||
423 | SET_FOREACH(other, s, i) { | |
424 | UnitDependency d; | |
425 | ||
426 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) | |
ac155bb8 | 427 | set_remove(other->dependencies[d], u); |
701cc384 LP |
428 | |
429 | unit_add_to_gc_queue(other); | |
87f0e418 LP |
430 | } |
431 | ||
432 | set_free(s); | |
433 | } | |
434 | ||
c2756a68 LP |
435 | static void unit_remove_transient(Unit *u) { |
436 | char **i; | |
437 | ||
438 | assert(u); | |
439 | ||
440 | if (!u->transient) | |
441 | return; | |
442 | ||
443 | if (u->fragment_path) | |
3f5e8115 | 444 | (void) unlink(u->fragment_path); |
c2756a68 LP |
445 | |
446 | STRV_FOREACH(i, u->dropin_paths) { | |
39591351 | 447 | _cleanup_free_ char *p = NULL, *pp = NULL; |
c2756a68 | 448 | |
39591351 LP |
449 | p = dirname_malloc(*i); /* Get the drop-in directory from the drop-in file */ |
450 | if (!p) | |
451 | continue; | |
452 | ||
453 | pp = dirname_malloc(p); /* Get the config directory from the drop-in directory */ | |
454 | if (!pp) | |
455 | continue; | |
456 | ||
457 | /* Only drop transient drop-ins */ | |
458 | if (!path_equal(u->manager->lookup_paths.transient, pp)) | |
459 | continue; | |
c2756a68 | 460 | |
39591351 LP |
461 | (void) unlink(*i); |
462 | (void) rmdir(p); | |
c2756a68 LP |
463 | } |
464 | } | |
465 | ||
a57f7e2c LP |
466 | static void unit_free_requires_mounts_for(Unit *u) { |
467 | char **j; | |
468 | ||
469 | STRV_FOREACH(j, u->requires_mounts_for) { | |
470 | char s[strlen(*j) + 1]; | |
471 | ||
472 | PATH_FOREACH_PREFIX_MORE(s, *j) { | |
473 | char *y; | |
474 | Set *x; | |
475 | ||
476 | x = hashmap_get2(u->manager->units_requiring_mounts_for, s, (void**) &y); | |
477 | if (!x) | |
478 | continue; | |
479 | ||
480 | set_remove(x, u); | |
481 | ||
482 | if (set_isempty(x)) { | |
483 | hashmap_remove(u->manager->units_requiring_mounts_for, y); | |
484 | free(y); | |
485 | set_free(x); | |
486 | } | |
487 | } | |
488 | } | |
489 | ||
6796073e | 490 | u->requires_mounts_for = strv_free(u->requires_mounts_for); |
a57f7e2c LP |
491 | } |
492 | ||
598459ce LP |
493 | static void unit_done(Unit *u) { |
494 | ExecContext *ec; | |
495 | CGroupContext *cc; | |
496 | ||
497 | assert(u); | |
498 | ||
499 | if (u->type < 0) | |
500 | return; | |
501 | ||
502 | if (UNIT_VTABLE(u)->done) | |
503 | UNIT_VTABLE(u)->done(u); | |
504 | ||
505 | ec = unit_get_exec_context(u); | |
506 | if (ec) | |
507 | exec_context_done(ec); | |
508 | ||
509 | cc = unit_get_cgroup_context(u); | |
510 | if (cc) | |
511 | cgroup_context_done(cc); | |
512 | } | |
513 | ||
87f0e418 LP |
514 | void unit_free(Unit *u) { |
515 | UnitDependency d; | |
516 | Iterator i; | |
517 | char *t; | |
518 | ||
c9d5c9c0 LP |
519 | if (!u) |
520 | return; | |
87f0e418 | 521 | |
4f4afc88 LP |
522 | if (u->transient_file) |
523 | fclose(u->transient_file); | |
524 | ||
2c289ea8 | 525 | if (!MANAGER_IS_RELOADING(u->manager)) |
c2756a68 LP |
526 | unit_remove_transient(u); |
527 | ||
c1e1601e LP |
528 | bus_unit_send_removed_signal(u); |
529 | ||
598459ce | 530 | unit_done(u); |
a013b84b | 531 | |
cf9fd508 DM |
532 | sd_bus_slot_unref(u->match_bus_slot); |
533 | ||
05a98afd LP |
534 | sd_bus_track_unref(u->bus_track); |
535 | u->deserialized_refs = strv_free(u->deserialized_refs); | |
536 | ||
a57f7e2c LP |
537 | unit_free_requires_mounts_for(u); |
538 | ||
ac155bb8 MS |
539 | SET_FOREACH(t, u->names, i) |
540 | hashmap_remove_value(u->manager->units, t, u); | |
87f0e418 | 541 | |
4b58153d LP |
542 | if (!sd_id128_is_null(u->invocation_id)) |
543 | hashmap_remove_value(u->manager->units_by_invocation_id, &u->invocation_id, u); | |
544 | ||
97e7d748 MS |
545 | if (u->job) { |
546 | Job *j = u->job; | |
547 | job_uninstall(j); | |
548 | job_free(j); | |
549 | } | |
964e0949 | 550 | |
e0209d83 MS |
551 | if (u->nop_job) { |
552 | Job *j = u->nop_job; | |
553 | job_uninstall(j); | |
554 | job_free(j); | |
555 | } | |
556 | ||
964e0949 | 557 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) |
ac155bb8 | 558 | bidi_set_free(u, u->dependencies[d]); |
964e0949 | 559 | |
ac155bb8 | 560 | if (u->type != _UNIT_TYPE_INVALID) |
71fda00f | 561 | LIST_REMOVE(units_by_type, u->manager->units_by_type[u->type], u); |
ef734fd6 | 562 | |
ac155bb8 | 563 | if (u->in_load_queue) |
71fda00f | 564 | LIST_REMOVE(load_queue, u->manager->load_queue, u); |
87f0e418 | 565 | |
ac155bb8 | 566 | if (u->in_dbus_queue) |
71fda00f | 567 | LIST_REMOVE(dbus_queue, u->manager->dbus_unit_queue, u); |
c1e1601e | 568 | |
ac155bb8 | 569 | if (u->in_cleanup_queue) |
71fda00f | 570 | LIST_REMOVE(cleanup_queue, u->manager->cleanup_queue, u); |
23a177ef | 571 | |
a2d72e26 | 572 | if (u->in_gc_queue) |
c5a97ed1 | 573 | LIST_REMOVE(gc_queue, u->manager->gc_unit_queue, u); |
701cc384 | 574 | |
4ad49000 | 575 | if (u->in_cgroup_queue) |
71fda00f | 576 | LIST_REMOVE(cgroup_queue, u->manager->cgroup_queue, u); |
87f0e418 | 577 | |
efdb0237 | 578 | unit_release_cgroup(u); |
72673e86 | 579 | |
00d9ef85 LP |
580 | unit_unref_uid_gid(u, false); |
581 | ||
5269eb6b | 582 | (void) manager_update_failed_units(u->manager, u, false); |
db785129 | 583 | set_remove(u->manager->startup_units, u); |
f755e3b7 | 584 | |
ac155bb8 | 585 | free(u->description); |
49dbfa7b | 586 | strv_free(u->documentation); |
ac155bb8 | 587 | free(u->fragment_path); |
1b64d026 | 588 | free(u->source_path); |
ae7a7182 | 589 | strv_free(u->dropin_paths); |
ac155bb8 | 590 | free(u->instance); |
87f0e418 | 591 | |
f189ab18 LP |
592 | free(u->job_timeout_reboot_arg); |
593 | ||
ac155bb8 | 594 | set_free_free(u->names); |
87f0e418 | 595 | |
a911bb9a LP |
596 | unit_unwatch_all_pids(u); |
597 | ||
ac155bb8 | 598 | condition_free_list(u->conditions); |
59fccdc5 | 599 | condition_free_list(u->asserts); |
52661efd | 600 | |
6bf0f408 LP |
601 | free(u->reboot_arg); |
602 | ||
702a2d8f LP |
603 | unit_ref_unset(&u->slice); |
604 | ||
ac155bb8 MS |
605 | while (u->refs) |
606 | unit_ref_unset(u->refs); | |
57020a3a | 607 | |
87f0e418 LP |
608 | free(u); |
609 | } | |
610 | ||
611 | UnitActiveState unit_active_state(Unit *u) { | |
612 | assert(u); | |
613 | ||
ac155bb8 | 614 | if (u->load_state == UNIT_MERGED) |
6124958c LP |
615 | return unit_active_state(unit_follow_merge(u)); |
616 | ||
617 | /* After a reload it might happen that a unit is not correctly | |
618 | * loaded but still has a process around. That's why we won't | |
fdf20a31 | 619 | * shortcut failed loading to UNIT_INACTIVE_FAILED. */ |
87f0e418 LP |
620 | |
621 | return UNIT_VTABLE(u)->active_state(u); | |
622 | } | |
623 | ||
10a94420 LP |
624 | const char* unit_sub_state_to_string(Unit *u) { |
625 | assert(u); | |
626 | ||
627 | return UNIT_VTABLE(u)->sub_state_to_string(u); | |
628 | } | |
629 | ||
7c0b05e5 MS |
630 | static int complete_move(Set **s, Set **other) { |
631 | int r; | |
632 | ||
23a177ef LP |
633 | assert(s); |
634 | assert(other); | |
87f0e418 | 635 | |
23a177ef | 636 | if (!*other) |
7c0b05e5 | 637 | return 0; |
87f0e418 | 638 | |
7c0b05e5 MS |
639 | if (*s) { |
640 | r = set_move(*s, *other); | |
641 | if (r < 0) | |
642 | return r; | |
643 | } else { | |
23a177ef LP |
644 | *s = *other; |
645 | *other = NULL; | |
646 | } | |
7c0b05e5 MS |
647 | |
648 | return 0; | |
23a177ef | 649 | } |
87f0e418 | 650 | |
7c0b05e5 | 651 | static int merge_names(Unit *u, Unit *other) { |
23a177ef LP |
652 | char *t; |
653 | Iterator i; | |
7c0b05e5 | 654 | int r; |
87f0e418 | 655 | |
23a177ef LP |
656 | assert(u); |
657 | assert(other); | |
658 | ||
7c0b05e5 MS |
659 | r = complete_move(&u->names, &other->names); |
660 | if (r < 0) | |
661 | return r; | |
23a177ef | 662 | |
ac155bb8 MS |
663 | set_free_free(other->names); |
664 | other->names = NULL; | |
665 | other->id = NULL; | |
23a177ef | 666 | |
ac155bb8 MS |
667 | SET_FOREACH(t, u->names, i) |
668 | assert_se(hashmap_replace(u->manager->units, t, u) == 0); | |
7c0b05e5 MS |
669 | |
670 | return 0; | |
87f0e418 LP |
671 | } |
672 | ||
09a65f92 MS |
673 | static int reserve_dependencies(Unit *u, Unit *other, UnitDependency d) { |
674 | unsigned n_reserve; | |
675 | ||
676 | assert(u); | |
677 | assert(other); | |
678 | assert(d < _UNIT_DEPENDENCY_MAX); | |
679 | ||
680 | /* | |
681 | * If u does not have this dependency set allocated, there is no need | |
f131770b | 682 | * to reserve anything. In that case other's set will be transferred |
09a65f92 MS |
683 | * as a whole to u by complete_move(). |
684 | */ | |
685 | if (!u->dependencies[d]) | |
686 | return 0; | |
687 | ||
688 | /* merge_dependencies() will skip a u-on-u dependency */ | |
689 | n_reserve = set_size(other->dependencies[d]) - !!set_get(other->dependencies[d], u); | |
690 | ||
691 | return set_reserve(u->dependencies[d], n_reserve); | |
692 | } | |
693 | ||
d1fab3fe | 694 | static void merge_dependencies(Unit *u, Unit *other, const char *other_id, UnitDependency d) { |
23a177ef LP |
695 | Iterator i; |
696 | Unit *back; | |
87f0e418 | 697 | int r; |
23a177ef LP |
698 | |
699 | assert(u); | |
700 | assert(other); | |
701 | assert(d < _UNIT_DEPENDENCY_MAX); | |
702 | ||
83a95334 | 703 | /* Fix backwards pointers */ |
ac155bb8 | 704 | SET_FOREACH(back, other->dependencies[d], i) { |
23a177ef LP |
705 | UnitDependency k; |
706 | ||
e48614c4 | 707 | for (k = 0; k < _UNIT_DEPENDENCY_MAX; k++) { |
e66047ff ZJS |
708 | /* Do not add dependencies between u and itself */ |
709 | if (back == u) { | |
d1fab3fe | 710 | if (set_remove(back->dependencies[k], other)) |
f2341e0a | 711 | maybe_warn_about_dependency(u, other_id, k); |
e66047ff ZJS |
712 | } else { |
713 | r = set_remove_and_put(back->dependencies[k], other, u); | |
714 | if (r == -EEXIST) | |
715 | set_remove(back->dependencies[k], other); | |
716 | else | |
717 | assert(r >= 0 || r == -ENOENT); | |
718 | } | |
e48614c4 | 719 | } |
23a177ef LP |
720 | } |
721 | ||
e66047ff | 722 | /* Also do not move dependencies on u to itself */ |
d1fab3fe ZJS |
723 | back = set_remove(other->dependencies[d], u); |
724 | if (back) | |
f2341e0a | 725 | maybe_warn_about_dependency(u, other_id, d); |
e66047ff | 726 | |
7c0b05e5 MS |
727 | /* The move cannot fail. The caller must have performed a reservation. */ |
728 | assert_se(complete_move(&u->dependencies[d], &other->dependencies[d]) == 0); | |
23a177ef | 729 | |
525d3cc7 | 730 | other->dependencies[d] = set_free(other->dependencies[d]); |
23a177ef LP |
731 | } |
732 | ||
733 | int unit_merge(Unit *u, Unit *other) { | |
87f0e418 | 734 | UnitDependency d; |
d1fab3fe | 735 | const char *other_id = NULL; |
09a65f92 | 736 | int r; |
87f0e418 LP |
737 | |
738 | assert(u); | |
739 | assert(other); | |
ac155bb8 MS |
740 | assert(u->manager == other->manager); |
741 | assert(u->type != _UNIT_TYPE_INVALID); | |
87f0e418 | 742 | |
cc916967 LP |
743 | other = unit_follow_merge(other); |
744 | ||
23a177ef LP |
745 | if (other == u) |
746 | return 0; | |
747 | ||
ac155bb8 | 748 | if (u->type != other->type) |
9e2f7c11 LP |
749 | return -EINVAL; |
750 | ||
ac155bb8 | 751 | if (!u->instance != !other->instance) |
87f0e418 LP |
752 | return -EINVAL; |
753 | ||
8a993b61 | 754 | if (!unit_type_may_alias(u->type)) /* Merging only applies to unit names that support aliases */ |
934e749e LP |
755 | return -EEXIST; |
756 | ||
ac155bb8 | 757 | if (other->load_state != UNIT_STUB && |
c2756a68 | 758 | other->load_state != UNIT_NOT_FOUND) |
23a177ef | 759 | return -EEXIST; |
87f0e418 | 760 | |
ac155bb8 | 761 | if (other->job) |
819e213f LP |
762 | return -EEXIST; |
763 | ||
e0209d83 MS |
764 | if (other->nop_job) |
765 | return -EEXIST; | |
766 | ||
fdf20a31 | 767 | if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) |
819e213f LP |
768 | return -EEXIST; |
769 | ||
d1fab3fe ZJS |
770 | if (other->id) |
771 | other_id = strdupa(other->id); | |
772 | ||
09a65f92 MS |
773 | /* Make reservations to ensure merge_dependencies() won't fail */ |
774 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) { | |
775 | r = reserve_dependencies(u, other, d); | |
776 | /* | |
777 | * We don't rollback reservations if we fail. We don't have | |
778 | * a way to undo reservations. A reservation is not a leak. | |
779 | */ | |
780 | if (r < 0) | |
781 | return r; | |
782 | } | |
783 | ||
87f0e418 | 784 | /* Merge names */ |
7c0b05e5 MS |
785 | r = merge_names(u, other); |
786 | if (r < 0) | |
787 | return r; | |
87f0e418 | 788 | |
57020a3a | 789 | /* Redirect all references */ |
ac155bb8 MS |
790 | while (other->refs) |
791 | unit_ref_set(other->refs, u); | |
57020a3a | 792 | |
87f0e418 LP |
793 | /* Merge dependencies */ |
794 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) | |
d1fab3fe | 795 | merge_dependencies(u, other, other_id, d); |
87f0e418 | 796 | |
ac155bb8 MS |
797 | other->load_state = UNIT_MERGED; |
798 | other->merged_into = u; | |
23a177ef | 799 | |
3616a49c LP |
800 | /* If there is still some data attached to the other node, we |
801 | * don't need it anymore, and can free it. */ | |
ac155bb8 | 802 | if (other->load_state != UNIT_STUB) |
3616a49c LP |
803 | if (UNIT_VTABLE(other)->done) |
804 | UNIT_VTABLE(other)->done(other); | |
805 | ||
806 | unit_add_to_dbus_queue(u); | |
23a177ef LP |
807 | unit_add_to_cleanup_queue(other); |
808 | ||
809 | return 0; | |
810 | } | |
811 | ||
812 | int unit_merge_by_name(Unit *u, const char *name) { | |
934e749e | 813 | _cleanup_free_ char *s = NULL; |
23a177ef | 814 | Unit *other; |
9e2f7c11 | 815 | int r; |
23a177ef LP |
816 | |
817 | assert(u); | |
818 | assert(name); | |
819 | ||
7410616c | 820 | if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
ac155bb8 | 821 | if (!u->instance) |
9e2f7c11 LP |
822 | return -EINVAL; |
823 | ||
7410616c LP |
824 | r = unit_name_replace_instance(name, u->instance, &s); |
825 | if (r < 0) | |
826 | return r; | |
9e2f7c11 LP |
827 | |
828 | name = s; | |
829 | } | |
830 | ||
c2756a68 | 831 | other = manager_get_unit(u->manager, name); |
7410616c LP |
832 | if (other) |
833 | return unit_merge(u, other); | |
23a177ef | 834 | |
7410616c | 835 | return unit_add_name(u, name); |
23a177ef LP |
836 | } |
837 | ||
838 | Unit* unit_follow_merge(Unit *u) { | |
839 | assert(u); | |
840 | ||
ac155bb8 MS |
841 | while (u->load_state == UNIT_MERGED) |
842 | assert_se(u = u->merged_into); | |
23a177ef LP |
843 | |
844 | return u; | |
845 | } | |
846 | ||
847 | int unit_add_exec_dependencies(Unit *u, ExecContext *c) { | |
848 | int r; | |
849 | ||
850 | assert(u); | |
851 | assert(c); | |
852 | ||
36be24c8 ZJS |
853 | if (c->working_directory) { |
854 | r = unit_require_mounts_for(u, c->working_directory); | |
855 | if (r < 0) | |
856 | return r; | |
857 | } | |
858 | ||
859 | if (c->root_directory) { | |
860 | r = unit_require_mounts_for(u, c->root_directory); | |
861 | if (r < 0) | |
862 | return r; | |
863 | } | |
864 | ||
915e6d16 LP |
865 | if (c->root_image) { |
866 | r = unit_require_mounts_for(u, c->root_image); | |
867 | if (r < 0) | |
868 | return r; | |
869 | } | |
870 | ||
463d0d15 | 871 | if (!MANAGER_IS_SYSTEM(u->manager)) |
b46a529c LP |
872 | return 0; |
873 | ||
874 | if (c->private_tmp) { | |
d71f0505 LP |
875 | const char *p; |
876 | ||
877 | FOREACH_STRING(p, "/tmp", "/var/tmp") { | |
878 | r = unit_require_mounts_for(u, p); | |
879 | if (r < 0) | |
880 | return r; | |
881 | } | |
b46a529c | 882 | |
d71f0505 | 883 | r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_TMPFILES_SETUP_SERVICE, NULL, true); |
b46a529c LP |
884 | if (r < 0) |
885 | return r; | |
886 | } | |
887 | ||
52c239d7 LB |
888 | if (!IN_SET(c->std_output, |
889 | EXEC_OUTPUT_JOURNAL, EXEC_OUTPUT_JOURNAL_AND_CONSOLE, | |
890 | EXEC_OUTPUT_KMSG, EXEC_OUTPUT_KMSG_AND_CONSOLE, | |
891 | EXEC_OUTPUT_SYSLOG, EXEC_OUTPUT_SYSLOG_AND_CONSOLE) && | |
892 | !IN_SET(c->std_error, | |
893 | EXEC_OUTPUT_JOURNAL, EXEC_OUTPUT_JOURNAL_AND_CONSOLE, | |
894 | EXEC_OUTPUT_KMSG, EXEC_OUTPUT_KMSG_AND_CONSOLE, | |
895 | EXEC_OUTPUT_SYSLOG, EXEC_OUTPUT_SYSLOG_AND_CONSOLE)) | |
23a177ef LP |
896 | return 0; |
897 | ||
898 | /* If syslog or kernel logging is requested, make sure our own | |
899 | * logging daemon is run first. */ | |
900 | ||
b46a529c LP |
901 | r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_JOURNALD_SOCKET, NULL, true); |
902 | if (r < 0) | |
903 | return r; | |
23a177ef | 904 | |
87f0e418 LP |
905 | return 0; |
906 | } | |
907 | ||
87f0e418 LP |
908 | const char *unit_description(Unit *u) { |
909 | assert(u); | |
910 | ||
ac155bb8 MS |
911 | if (u->description) |
912 | return u->description; | |
87f0e418 | 913 | |
ac155bb8 | 914 | return strna(u->id); |
87f0e418 LP |
915 | } |
916 | ||
917 | void unit_dump(Unit *u, FILE *f, const char *prefix) { | |
49dbfa7b | 918 | char *t, **j; |
87f0e418 LP |
919 | UnitDependency d; |
920 | Iterator i; | |
47be870b | 921 | const char *prefix2; |
173e3821 | 922 | char |
a483fb59 | 923 | timestamp0[FORMAT_TIMESTAMP_MAX], |
173e3821 LP |
924 | timestamp1[FORMAT_TIMESTAMP_MAX], |
925 | timestamp2[FORMAT_TIMESTAMP_MAX], | |
926 | timestamp3[FORMAT_TIMESTAMP_MAX], | |
faf919f1 LP |
927 | timestamp4[FORMAT_TIMESTAMP_MAX], |
928 | timespan[FORMAT_TIMESPAN_MAX]; | |
a7f241db | 929 | Unit *following; |
eeaedb7c LP |
930 | _cleanup_set_free_ Set *following_set = NULL; |
931 | int r; | |
05a98afd | 932 | const char *n; |
87f0e418 LP |
933 | |
934 | assert(u); | |
ac155bb8 | 935 | assert(u->type >= 0); |
87f0e418 | 936 | |
4c940960 | 937 | prefix = strempty(prefix); |
63c372cb | 938 | prefix2 = strjoina(prefix, "\t"); |
87f0e418 LP |
939 | |
940 | fprintf(f, | |
40d50879 | 941 | "%s-> Unit %s:\n" |
87f0e418 | 942 | "%s\tDescription: %s\n" |
9e2f7c11 | 943 | "%s\tInstance: %s\n" |
87f0e418 | 944 | "%s\tUnit Load State: %s\n" |
2fad8195 | 945 | "%s\tUnit Active State: %s\n" |
b895d155 | 946 | "%s\tState Change Timestamp: %s\n" |
173e3821 | 947 | "%s\tInactive Exit Timestamp: %s\n" |
2fad8195 | 948 | "%s\tActive Enter Timestamp: %s\n" |
701cc384 | 949 | "%s\tActive Exit Timestamp: %s\n" |
173e3821 | 950 | "%s\tInactive Enter Timestamp: %s\n" |
45fb0699 | 951 | "%s\tGC Check Good: %s\n" |
9444b1f2 | 952 | "%s\tNeed Daemon Reload: %s\n" |
c2756a68 | 953 | "%s\tTransient: %s\n" |
f5869324 | 954 | "%s\tPerpetual: %s\n" |
4ad49000 LP |
955 | "%s\tSlice: %s\n" |
956 | "%s\tCGroup: %s\n" | |
957 | "%s\tCGroup realized: %s\n" | |
6414b7c9 DS |
958 | "%s\tCGroup mask: 0x%x\n" |
959 | "%s\tCGroup members mask: 0x%x\n", | |
ac155bb8 | 960 | prefix, u->id, |
87f0e418 | 961 | prefix, unit_description(u), |
ac155bb8 MS |
962 | prefix, strna(u->instance), |
963 | prefix, unit_load_state_to_string(u->load_state), | |
2fad8195 | 964 | prefix, unit_active_state_to_string(unit_active_state(u)), |
a483fb59 | 965 | prefix, strna(format_timestamp(timestamp0, sizeof(timestamp0), u->state_change_timestamp.realtime)), |
ac155bb8 MS |
966 | prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->inactive_exit_timestamp.realtime)), |
967 | prefix, strna(format_timestamp(timestamp2, sizeof(timestamp2), u->active_enter_timestamp.realtime)), | |
968 | prefix, strna(format_timestamp(timestamp3, sizeof(timestamp3), u->active_exit_timestamp.realtime)), | |
969 | prefix, strna(format_timestamp(timestamp4, sizeof(timestamp4), u->inactive_enter_timestamp.realtime)), | |
45fb0699 | 970 | prefix, yes_no(unit_check_gc(u)), |
9444b1f2 | 971 | prefix, yes_no(unit_need_daemon_reload(u)), |
c2756a68 | 972 | prefix, yes_no(u->transient), |
f5869324 | 973 | prefix, yes_no(u->perpetual), |
4ad49000 LP |
974 | prefix, strna(unit_slice_name(u)), |
975 | prefix, strna(u->cgroup_path), | |
976 | prefix, yes_no(u->cgroup_realized), | |
bc432dc7 | 977 | prefix, u->cgroup_realized_mask, |
6414b7c9 | 978 | prefix, u->cgroup_members_mask); |
0301abf4 | 979 | |
ac155bb8 | 980 | SET_FOREACH(t, u->names, i) |
87f0e418 LP |
981 | fprintf(f, "%s\tName: %s\n", prefix, t); |
982 | ||
4b58153d LP |
983 | if (!sd_id128_is_null(u->invocation_id)) |
984 | fprintf(f, "%s\tInvocation ID: " SD_ID128_FORMAT_STR "\n", | |
985 | prefix, SD_ID128_FORMAT_VAL(u->invocation_id)); | |
986 | ||
49dbfa7b LP |
987 | STRV_FOREACH(j, u->documentation) |
988 | fprintf(f, "%s\tDocumentation: %s\n", prefix, *j); | |
989 | ||
eeaedb7c LP |
990 | following = unit_following(u); |
991 | if (following) | |
ac155bb8 | 992 | fprintf(f, "%s\tFollowing: %s\n", prefix, following->id); |
8fe914ec | 993 | |
eeaedb7c LP |
994 | r = unit_following_set(u, &following_set); |
995 | if (r >= 0) { | |
996 | Unit *other; | |
997 | ||
998 | SET_FOREACH(other, following_set, i) | |
999 | fprintf(f, "%s\tFollowing Set Member: %s\n", prefix, other->id); | |
1000 | } | |
1001 | ||
ac155bb8 MS |
1002 | if (u->fragment_path) |
1003 | fprintf(f, "%s\tFragment Path: %s\n", prefix, u->fragment_path); | |
23a177ef | 1004 | |
1b64d026 LP |
1005 | if (u->source_path) |
1006 | fprintf(f, "%s\tSource Path: %s\n", prefix, u->source_path); | |
1007 | ||
ae7a7182 | 1008 | STRV_FOREACH(j, u->dropin_paths) |
2875e22b | 1009 | fprintf(f, "%s\tDropIn Path: %s\n", prefix, *j); |
ae7a7182 | 1010 | |
36c16a7c | 1011 | if (u->job_timeout != USEC_INFINITY) |
2fa4092c | 1012 | fprintf(f, "%s\tJob Timeout: %s\n", prefix, format_timespan(timespan, sizeof(timespan), u->job_timeout, 0)); |
faf919f1 | 1013 | |
87a47f99 LN |
1014 | if (u->job_timeout_action != EMERGENCY_ACTION_NONE) |
1015 | fprintf(f, "%s\tJob Timeout Action: %s\n", prefix, emergency_action_to_string(u->job_timeout_action)); | |
f189ab18 LP |
1016 | |
1017 | if (u->job_timeout_reboot_arg) | |
1018 | fprintf(f, "%s\tJob Timeout Reboot Argument: %s\n", prefix, u->job_timeout_reboot_arg); | |
1019 | ||
59fccdc5 LP |
1020 | condition_dump_list(u->conditions, f, prefix, condition_type_to_string); |
1021 | condition_dump_list(u->asserts, f, prefix, assert_type_to_string); | |
52661efd | 1022 | |
ac155bb8 | 1023 | if (dual_timestamp_is_set(&u->condition_timestamp)) |
2791a8f8 LP |
1024 | fprintf(f, |
1025 | "%s\tCondition Timestamp: %s\n" | |
1026 | "%s\tCondition Result: %s\n", | |
ac155bb8 MS |
1027 | prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->condition_timestamp.realtime)), |
1028 | prefix, yes_no(u->condition_result)); | |
2791a8f8 | 1029 | |
59fccdc5 LP |
1030 | if (dual_timestamp_is_set(&u->assert_timestamp)) |
1031 | fprintf(f, | |
1032 | "%s\tAssert Timestamp: %s\n" | |
1033 | "%s\tAssert Result: %s\n", | |
1034 | prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->assert_timestamp.realtime)), | |
1035 | prefix, yes_no(u->assert_result)); | |
1036 | ||
87f0e418 LP |
1037 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) { |
1038 | Unit *other; | |
1039 | ||
ac155bb8 MS |
1040 | SET_FOREACH(other, u->dependencies[d], i) |
1041 | fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), other->id); | |
87f0e418 LP |
1042 | } |
1043 | ||
7c8fa05c | 1044 | if (!strv_isempty(u->requires_mounts_for)) { |
7c8fa05c LP |
1045 | fprintf(f, |
1046 | "%s\tRequiresMountsFor:", prefix); | |
1047 | ||
1048 | STRV_FOREACH(j, u->requires_mounts_for) | |
1049 | fprintf(f, " %s", *j); | |
1050 | ||
1051 | fputs("\n", f); | |
1052 | } | |
1053 | ||
ac155bb8 | 1054 | if (u->load_state == UNIT_LOADED) { |
ab1f0633 | 1055 | |
b0650475 | 1056 | fprintf(f, |
a40eb732 | 1057 | "%s\tStopWhenUnneeded: %s\n" |
b5e9dba8 LP |
1058 | "%s\tRefuseManualStart: %s\n" |
1059 | "%s\tRefuseManualStop: %s\n" | |
222ae6a8 | 1060 | "%s\tDefaultDependencies: %s\n" |
d420282b | 1061 | "%s\tOnFailureJobMode: %s\n" |
36b4a7ba | 1062 | "%s\tIgnoreOnIsolate: %s\n", |
ac155bb8 MS |
1063 | prefix, yes_no(u->stop_when_unneeded), |
1064 | prefix, yes_no(u->refuse_manual_start), | |
1065 | prefix, yes_no(u->refuse_manual_stop), | |
1066 | prefix, yes_no(u->default_dependencies), | |
d420282b | 1067 | prefix, job_mode_to_string(u->on_failure_job_mode), |
36b4a7ba | 1068 | prefix, yes_no(u->ignore_on_isolate)); |
ac155bb8 | 1069 | |
23a177ef LP |
1070 | if (UNIT_VTABLE(u)->dump) |
1071 | UNIT_VTABLE(u)->dump(u, f, prefix2); | |
b0650475 | 1072 | |
ac155bb8 | 1073 | } else if (u->load_state == UNIT_MERGED) |
b0650475 LP |
1074 | fprintf(f, |
1075 | "%s\tMerged into: %s\n", | |
ac155bb8 MS |
1076 | prefix, u->merged_into->id); |
1077 | else if (u->load_state == UNIT_ERROR) | |
1078 | fprintf(f, "%s\tLoad Error Code: %s\n", prefix, strerror(-u->load_error)); | |
8821a00f | 1079 | |
05a98afd LP |
1080 | for (n = sd_bus_track_first(u->bus_track); n; n = sd_bus_track_next(u->bus_track)) |
1081 | fprintf(f, "%s\tBus Ref: %s\n", prefix, n); | |
87f0e418 | 1082 | |
ac155bb8 MS |
1083 | if (u->job) |
1084 | job_dump(u->job, f, prefix2); | |
87f0e418 | 1085 | |
e0209d83 MS |
1086 | if (u->nop_job) |
1087 | job_dump(u->nop_job, f, prefix2); | |
87f0e418 LP |
1088 | } |
1089 | ||
1090 | /* Common implementation for multiple backends */ | |
e537352b | 1091 | int unit_load_fragment_and_dropin(Unit *u) { |
2d058a87 | 1092 | Unit *t; |
23a177ef LP |
1093 | int r; |
1094 | ||
1095 | assert(u); | |
23a177ef | 1096 | |
e48614c4 | 1097 | /* Load a .{service,socket,...} file */ |
4ad49000 LP |
1098 | r = unit_load_fragment(u); |
1099 | if (r < 0) | |
23a177ef LP |
1100 | return r; |
1101 | ||
ac155bb8 | 1102 | if (u->load_state == UNIT_STUB) |
23a177ef LP |
1103 | return -ENOENT; |
1104 | ||
2d058a87 FB |
1105 | /* If the unit is an alias and the final unit has already been |
1106 | * loaded, there's no point in reloading the dropins one more time. */ | |
1107 | t = unit_follow_merge(u); | |
1108 | if (t != u && t->load_state != UNIT_STUB) | |
1109 | return 0; | |
23a177ef | 1110 | |
2d058a87 | 1111 | return unit_load_dropin(t); |
23a177ef LP |
1112 | } |
1113 | ||
1114 | /* Common implementation for multiple backends */ | |
e537352b | 1115 | int unit_load_fragment_and_dropin_optional(Unit *u) { |
2d058a87 | 1116 | Unit *t; |
23a177ef | 1117 | int r; |
87f0e418 LP |
1118 | |
1119 | assert(u); | |
1120 | ||
23a177ef LP |
1121 | /* Same as unit_load_fragment_and_dropin(), but whether |
1122 | * something can be loaded or not doesn't matter. */ | |
1123 | ||
1124 | /* Load a .service file */ | |
4ad49000 LP |
1125 | r = unit_load_fragment(u); |
1126 | if (r < 0) | |
87f0e418 LP |
1127 | return r; |
1128 | ||
ac155bb8 MS |
1129 | if (u->load_state == UNIT_STUB) |
1130 | u->load_state = UNIT_LOADED; | |
d46de8a1 | 1131 | |
2d058a87 FB |
1132 | /* If the unit is an alias and the final unit has already been |
1133 | * loaded, there's no point in reloading the dropins one more time. */ | |
1134 | t = unit_follow_merge(u); | |
1135 | if (t != u && t->load_state != UNIT_STUB) | |
1136 | return 0; | |
87f0e418 | 1137 | |
2d058a87 | 1138 | return unit_load_dropin(t); |
87f0e418 LP |
1139 | } |
1140 | ||
bba34eed | 1141 | int unit_add_default_target_dependency(Unit *u, Unit *target) { |
98bc2000 LP |
1142 | assert(u); |
1143 | assert(target); | |
1144 | ||
ac155bb8 | 1145 | if (target->type != UNIT_TARGET) |
98bc2000 LP |
1146 | return 0; |
1147 | ||
35b8ca3a | 1148 | /* Only add the dependency if both units are loaded, so that |
bba34eed | 1149 | * that loop check below is reliable */ |
ac155bb8 MS |
1150 | if (u->load_state != UNIT_LOADED || |
1151 | target->load_state != UNIT_LOADED) | |
bba34eed LP |
1152 | return 0; |
1153 | ||
21256a2b LP |
1154 | /* If either side wants no automatic dependencies, then let's |
1155 | * skip this */ | |
ac155bb8 MS |
1156 | if (!u->default_dependencies || |
1157 | !target->default_dependencies) | |
21256a2b LP |
1158 | return 0; |
1159 | ||
98bc2000 | 1160 | /* Don't create loops */ |
ac155bb8 | 1161 | if (set_get(target->dependencies[UNIT_BEFORE], u)) |
98bc2000 LP |
1162 | return 0; |
1163 | ||
1164 | return unit_add_dependency(target, UNIT_AFTER, u, true); | |
1165 | } | |
1166 | ||
e954c9cf | 1167 | static int unit_add_target_dependencies(Unit *u) { |
a016b922 | 1168 | |
21256a2b LP |
1169 | static const UnitDependency deps[] = { |
1170 | UNIT_REQUIRED_BY, | |
be7d9ff7 | 1171 | UNIT_REQUISITE_OF, |
21256a2b LP |
1172 | UNIT_WANTED_BY, |
1173 | UNIT_BOUND_BY | |
1174 | }; | |
1175 | ||
bba34eed | 1176 | Unit *target; |
98bc2000 | 1177 | Iterator i; |
21256a2b | 1178 | unsigned k; |
db57f3c6 | 1179 | int r = 0; |
98bc2000 LP |
1180 | |
1181 | assert(u); | |
1182 | ||
21256a2b | 1183 | for (k = 0; k < ELEMENTSOF(deps); k++) |
a016b922 LP |
1184 | SET_FOREACH(target, u->dependencies[deps[k]], i) { |
1185 | r = unit_add_default_target_dependency(u, target); | |
1186 | if (r < 0) | |
21256a2b | 1187 | return r; |
a016b922 LP |
1188 | } |
1189 | ||
e954c9cf LP |
1190 | return r; |
1191 | } | |
4ad49000 | 1192 | |
e954c9cf LP |
1193 | static int unit_add_slice_dependencies(Unit *u) { |
1194 | assert(u); | |
b81884e7 | 1195 | |
35b7ff80 | 1196 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) |
e954c9cf LP |
1197 | return 0; |
1198 | ||
1199 | if (UNIT_ISSET(u->slice)) | |
8c8da0e0 | 1200 | return unit_add_two_dependencies(u, UNIT_AFTER, UNIT_REQUIRES, UNIT_DEREF(u->slice), true); |
e954c9cf | 1201 | |
8c8da0e0 | 1202 | if (unit_has_name(u, SPECIAL_ROOT_SLICE)) |
d1fab3fe ZJS |
1203 | return 0; |
1204 | ||
8c8da0e0 | 1205 | return unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_ROOT_SLICE, NULL, true); |
98bc2000 LP |
1206 | } |
1207 | ||
e954c9cf | 1208 | static int unit_add_mount_dependencies(Unit *u) { |
9588bc32 LP |
1209 | char **i; |
1210 | int r; | |
1211 | ||
1212 | assert(u); | |
1213 | ||
1214 | STRV_FOREACH(i, u->requires_mounts_for) { | |
1215 | char prefix[strlen(*i) + 1]; | |
1216 | ||
1217 | PATH_FOREACH_PREFIX_MORE(prefix, *i) { | |
c7c89abb | 1218 | _cleanup_free_ char *p = NULL; |
9588bc32 LP |
1219 | Unit *m; |
1220 | ||
c7c89abb | 1221 | r = unit_name_from_path(prefix, ".mount", &p); |
9588bc32 LP |
1222 | if (r < 0) |
1223 | return r; | |
c7c89abb FB |
1224 | |
1225 | m = manager_get_unit(u->manager, p); | |
1226 | if (!m) { | |
1227 | /* Make sure to load the mount unit if | |
1228 | * it exists. If so the dependencies | |
1229 | * on this unit will be added later | |
1230 | * during the loading of the mount | |
1231 | * unit. */ | |
1232 | (void) manager_load_unit_prepare(u->manager, p, NULL, NULL, &m); | |
9588bc32 | 1233 | continue; |
c7c89abb | 1234 | } |
9588bc32 LP |
1235 | if (m == u) |
1236 | continue; | |
1237 | ||
1238 | if (m->load_state != UNIT_LOADED) | |
1239 | continue; | |
1240 | ||
1241 | r = unit_add_dependency(u, UNIT_AFTER, m, true); | |
1242 | if (r < 0) | |
1243 | return r; | |
1244 | ||
1245 | if (m->fragment_path) { | |
1246 | r = unit_add_dependency(u, UNIT_REQUIRES, m, true); | |
1247 | if (r < 0) | |
1248 | return r; | |
1249 | } | |
1250 | } | |
1251 | } | |
1252 | ||
1253 | return 0; | |
1254 | } | |
1255 | ||
95ae05c0 WC |
1256 | static int unit_add_startup_units(Unit *u) { |
1257 | CGroupContext *c; | |
5269eb6b | 1258 | int r; |
95ae05c0 WC |
1259 | |
1260 | c = unit_get_cgroup_context(u); | |
db785129 LP |
1261 | if (!c) |
1262 | return 0; | |
1263 | ||
d53d9474 | 1264 | if (c->startup_cpu_shares == CGROUP_CPU_SHARES_INVALID && |
13c31542 | 1265 | c->startup_io_weight == CGROUP_WEIGHT_INVALID && |
d53d9474 | 1266 | c->startup_blockio_weight == CGROUP_BLKIO_WEIGHT_INVALID) |
db785129 LP |
1267 | return 0; |
1268 | ||
5269eb6b LP |
1269 | r = set_ensure_allocated(&u->manager->startup_units, NULL); |
1270 | if (r < 0) | |
1271 | return r; | |
1272 | ||
756c09e6 | 1273 | return set_put(u->manager->startup_units, u); |
95ae05c0 WC |
1274 | } |
1275 | ||
87f0e418 LP |
1276 | int unit_load(Unit *u) { |
1277 | int r; | |
1278 | ||
1279 | assert(u); | |
1280 | ||
ac155bb8 | 1281 | if (u->in_load_queue) { |
71fda00f | 1282 | LIST_REMOVE(load_queue, u->manager->load_queue, u); |
ac155bb8 | 1283 | u->in_load_queue = false; |
87f0e418 LP |
1284 | } |
1285 | ||
ac155bb8 | 1286 | if (u->type == _UNIT_TYPE_INVALID) |
e537352b LP |
1287 | return -EINVAL; |
1288 | ||
ac155bb8 | 1289 | if (u->load_state != UNIT_STUB) |
87f0e418 LP |
1290 | return 0; |
1291 | ||
4f4afc88 LP |
1292 | if (u->transient_file) { |
1293 | r = fflush_and_check(u->transient_file); | |
1294 | if (r < 0) | |
1295 | goto fail; | |
1296 | ||
1297 | fclose(u->transient_file); | |
1298 | u->transient_file = NULL; | |
f9ba08fb | 1299 | |
f76707da | 1300 | u->fragment_mtime = now(CLOCK_REALTIME); |
4f4afc88 LP |
1301 | } |
1302 | ||
c2756a68 LP |
1303 | if (UNIT_VTABLE(u)->load) { |
1304 | r = UNIT_VTABLE(u)->load(u); | |
1305 | if (r < 0) | |
87f0e418 | 1306 | goto fail; |
c2756a68 | 1307 | } |
23a177ef | 1308 | |
ac155bb8 | 1309 | if (u->load_state == UNIT_STUB) { |
23a177ef LP |
1310 | r = -ENOENT; |
1311 | goto fail; | |
1312 | } | |
1313 | ||
7c8fa05c | 1314 | if (u->load_state == UNIT_LOADED) { |
c2756a68 | 1315 | |
e954c9cf LP |
1316 | r = unit_add_target_dependencies(u); |
1317 | if (r < 0) | |
1318 | goto fail; | |
1319 | ||
1320 | r = unit_add_slice_dependencies(u); | |
1321 | if (r < 0) | |
1322 | goto fail; | |
c2756a68 | 1323 | |
e954c9cf | 1324 | r = unit_add_mount_dependencies(u); |
7c8fa05c | 1325 | if (r < 0) |
c2756a68 | 1326 | goto fail; |
7c8fa05c | 1327 | |
95ae05c0 WC |
1328 | r = unit_add_startup_units(u); |
1329 | if (r < 0) | |
1330 | goto fail; | |
1331 | ||
bc432dc7 | 1332 | if (u->on_failure_job_mode == JOB_ISOLATE && set_size(u->dependencies[UNIT_ON_FAILURE]) > 1) { |
f2341e0a | 1333 | log_unit_error(u, "More than one OnFailure= dependencies specified but OnFailureJobMode=isolate set. Refusing."); |
c2756a68 LP |
1334 | r = -EINVAL; |
1335 | goto fail; | |
1336 | } | |
bc432dc7 LP |
1337 | |
1338 | unit_update_cgroup_members_masks(u); | |
f68319bb LP |
1339 | } |
1340 | ||
ac155bb8 | 1341 | assert((u->load_state != UNIT_MERGED) == !u->merged_into); |
23a177ef LP |
1342 | |
1343 | unit_add_to_dbus_queue(unit_follow_merge(u)); | |
701cc384 | 1344 | unit_add_to_gc_queue(u); |
87f0e418 | 1345 | |
87f0e418 LP |
1346 | return 0; |
1347 | ||
1348 | fail: | |
c2756a68 | 1349 | u->load_state = u->load_state == UNIT_STUB ? UNIT_NOT_FOUND : UNIT_ERROR; |
ac155bb8 | 1350 | u->load_error = r; |
c1e1601e | 1351 | unit_add_to_dbus_queue(u); |
9a46fc3b | 1352 | unit_add_to_gc_queue(u); |
23a177ef | 1353 | |
f2341e0a | 1354 | log_unit_debug_errno(u, r, "Failed to load configuration: %m"); |
23a177ef | 1355 | |
87f0e418 LP |
1356 | return r; |
1357 | } | |
1358 | ||
49365733 LP |
1359 | static bool unit_condition_test_list(Unit *u, Condition *first, const char *(*to_string)(ConditionType t)) { |
1360 | Condition *c; | |
1361 | int triggered = -1; | |
1362 | ||
1363 | assert(u); | |
1364 | assert(to_string); | |
1365 | ||
1366 | /* If the condition list is empty, then it is true */ | |
1367 | if (!first) | |
1368 | return true; | |
1369 | ||
1370 | /* Otherwise, if all of the non-trigger conditions apply and | |
1371 | * if any of the trigger conditions apply (unless there are | |
1372 | * none) we return true */ | |
1373 | LIST_FOREACH(conditions, c, first) { | |
1374 | int r; | |
1375 | ||
1376 | r = condition_test(c); | |
1377 | if (r < 0) | |
f2341e0a LP |
1378 | log_unit_warning(u, |
1379 | "Couldn't determine result for %s=%s%s%s, assuming failed: %m", | |
49365733 LP |
1380 | to_string(c->type), |
1381 | c->trigger ? "|" : "", | |
1382 | c->negate ? "!" : "", | |
f2341e0a | 1383 | c->parameter); |
49365733 | 1384 | else |
f2341e0a LP |
1385 | log_unit_debug(u, |
1386 | "%s=%s%s%s %s.", | |
49365733 LP |
1387 | to_string(c->type), |
1388 | c->trigger ? "|" : "", | |
1389 | c->negate ? "!" : "", | |
1390 | c->parameter, | |
f2341e0a | 1391 | condition_result_to_string(c->result)); |
49365733 LP |
1392 | |
1393 | if (!c->trigger && r <= 0) | |
1394 | return false; | |
1395 | ||
1396 | if (c->trigger && triggered <= 0) | |
1397 | triggered = r > 0; | |
1398 | } | |
1399 | ||
1400 | return triggered != 0; | |
1401 | } | |
1402 | ||
9588bc32 | 1403 | static bool unit_condition_test(Unit *u) { |
90bbc946 LP |
1404 | assert(u); |
1405 | ||
ac155bb8 | 1406 | dual_timestamp_get(&u->condition_timestamp); |
49365733 | 1407 | u->condition_result = unit_condition_test_list(u, u->conditions, condition_type_to_string); |
90bbc946 | 1408 | |
ac155bb8 | 1409 | return u->condition_result; |
90bbc946 LP |
1410 | } |
1411 | ||
59fccdc5 LP |
1412 | static bool unit_assert_test(Unit *u) { |
1413 | assert(u); | |
1414 | ||
1415 | dual_timestamp_get(&u->assert_timestamp); | |
49365733 | 1416 | u->assert_result = unit_condition_test_list(u, u->asserts, assert_type_to_string); |
59fccdc5 LP |
1417 | |
1418 | return u->assert_result; | |
1419 | } | |
1420 | ||
df446f96 LP |
1421 | void unit_status_printf(Unit *u, const char *status, const char *unit_status_msg_format) { |
1422 | DISABLE_WARNING_FORMAT_NONLITERAL; | |
1423 | manager_status_printf(u->manager, STATUS_TYPE_NORMAL, status, unit_status_msg_format, unit_description(u)); | |
1424 | REENABLE_WARNING; | |
1425 | } | |
1426 | ||
44a6b1b6 | 1427 | _pure_ static const char* unit_get_status_message_format(Unit *u, JobType t) { |
877d54e9 | 1428 | const char *format; |
a85ca902 | 1429 | const UnitStatusMessageFormats *format_table; |
877d54e9 LP |
1430 | |
1431 | assert(u); | |
df446f96 | 1432 | assert(IN_SET(t, JOB_START, JOB_STOP, JOB_RELOAD)); |
877d54e9 | 1433 | |
b5bf308b | 1434 | if (t != JOB_RELOAD) { |
a85ca902 MS |
1435 | format_table = &UNIT_VTABLE(u)->status_message_formats; |
1436 | if (format_table) { | |
1437 | format = format_table->starting_stopping[t == JOB_STOP]; | |
1438 | if (format) | |
1439 | return format; | |
1440 | } | |
1441 | } | |
877d54e9 LP |
1442 | |
1443 | /* Return generic strings */ | |
1444 | if (t == JOB_START) | |
1445 | return "Starting %s."; | |
1446 | else if (t == JOB_STOP) | |
1447 | return "Stopping %s."; | |
b5bf308b | 1448 | else |
877d54e9 | 1449 | return "Reloading %s."; |
877d54e9 LP |
1450 | } |
1451 | ||
1452 | static void unit_status_print_starting_stopping(Unit *u, JobType t) { | |
1453 | const char *format; | |
1454 | ||
1455 | assert(u); | |
1456 | ||
df446f96 LP |
1457 | /* Reload status messages have traditionally not been printed to console. */ |
1458 | if (!IN_SET(t, JOB_START, JOB_STOP)) | |
1459 | return; | |
1460 | ||
877d54e9 | 1461 | format = unit_get_status_message_format(u, t); |
c6918296 | 1462 | |
bcfce235 | 1463 | DISABLE_WARNING_FORMAT_NONLITERAL; |
49b1d377 | 1464 | unit_status_printf(u, "", format); |
bcfce235 | 1465 | REENABLE_WARNING; |
c6918296 MS |
1466 | } |
1467 | ||
877d54e9 LP |
1468 | static void unit_status_log_starting_stopping_reloading(Unit *u, JobType t) { |
1469 | const char *format; | |
1470 | char buf[LINE_MAX]; | |
1471 | sd_id128_t mid; | |
1472 | ||
1473 | assert(u); | |
1474 | ||
df446f96 | 1475 | if (!IN_SET(t, JOB_START, JOB_STOP, JOB_RELOAD)) |
877d54e9 LP |
1476 | return; |
1477 | ||
81270860 LP |
1478 | if (log_on_console()) |
1479 | return; | |
1480 | ||
877d54e9 LP |
1481 | /* We log status messages for all units and all operations. */ |
1482 | ||
a85ca902 | 1483 | format = unit_get_status_message_format(u, t); |
877d54e9 | 1484 | |
bcfce235 | 1485 | DISABLE_WARNING_FORMAT_NONLITERAL; |
e68eedbb | 1486 | snprintf(buf, sizeof buf, format, unit_description(u)); |
bcfce235 | 1487 | REENABLE_WARNING; |
877d54e9 LP |
1488 | |
1489 | mid = t == JOB_START ? SD_MESSAGE_UNIT_STARTING : | |
1490 | t == JOB_STOP ? SD_MESSAGE_UNIT_STOPPING : | |
1491 | SD_MESSAGE_UNIT_RELOADING; | |
1492 | ||
f2341e0a LP |
1493 | /* Note that we deliberately use LOG_MESSAGE() instead of |
1494 | * LOG_UNIT_MESSAGE() here, since this is supposed to mimic | |
1495 | * closely what is written to screen using the status output, | |
1496 | * which is supposed the highest level, friendliest output | |
1497 | * possible, which means we should avoid the low-level unit | |
1498 | * name. */ | |
1499 | log_struct(LOG_INFO, | |
1500 | LOG_MESSAGE_ID(mid), | |
1501 | LOG_UNIT_ID(u), | |
1502 | LOG_MESSAGE("%s", buf), | |
1503 | NULL); | |
877d54e9 | 1504 | } |
877d54e9 | 1505 | |
d1a34ae9 | 1506 | void unit_status_emit_starting_stopping_reloading(Unit *u, JobType t) { |
df446f96 LP |
1507 | assert(u); |
1508 | assert(t >= 0); | |
1509 | assert(t < _JOB_TYPE_MAX); | |
d1a34ae9 MS |
1510 | |
1511 | unit_status_log_starting_stopping_reloading(u, t); | |
df446f96 | 1512 | unit_status_print_starting_stopping(u, t); |
d1a34ae9 MS |
1513 | } |
1514 | ||
07299350 | 1515 | int unit_start_limit_test(Unit *u) { |
6bf0f408 LP |
1516 | assert(u); |
1517 | ||
1518 | if (ratelimit_test(&u->start_limit)) { | |
1519 | u->start_limit_hit = false; | |
1520 | return 0; | |
1521 | } | |
1522 | ||
1523 | log_unit_warning(u, "Start request repeated too quickly."); | |
1524 | u->start_limit_hit = true; | |
1525 | ||
87a47f99 | 1526 | return emergency_action(u->manager, u->start_limit_action, u->reboot_arg, "unit failed"); |
6bf0f408 LP |
1527 | } |
1528 | ||
c891efaf FB |
1529 | bool unit_shall_confirm_spawn(Unit *u) { |
1530 | ||
1531 | if (manager_is_confirm_spawn_disabled(u->manager)) | |
1532 | return false; | |
1533 | ||
1534 | /* For some reasons units remaining in the same process group | |
1535 | * as PID 1 fail to acquire the console even if it's not used | |
1536 | * by any process. So skip the confirmation question for them. */ | |
1537 | return !unit_get_exec_context(u)->same_pgrp; | |
1538 | } | |
1539 | ||
87f0e418 | 1540 | /* Errors: |
6bf0f408 LP |
1541 | * -EBADR: This unit type does not support starting. |
1542 | * -EALREADY: Unit is already started. | |
1543 | * -EAGAIN: An operation is already in progress. Retry later. | |
1544 | * -ECANCELED: Too many requests for now. | |
1545 | * -EPROTO: Assert failed | |
1546 | * -EINVAL: Unit not loaded | |
1547 | * -EOPNOTSUPP: Unit type not supported | |
87f0e418 LP |
1548 | */ |
1549 | int unit_start(Unit *u) { | |
1550 | UnitActiveState state; | |
92ab323c | 1551 | Unit *following; |
87f0e418 LP |
1552 | |
1553 | assert(u); | |
1554 | ||
a82e5507 LP |
1555 | /* If this is already started, then this will succeed. Note |
1556 | * that this will even succeed if this unit is not startable | |
1557 | * by the user. This is relied on to detect when we need to | |
1558 | * wait for units and when waiting is finished. */ | |
87f0e418 LP |
1559 | state = unit_active_state(u); |
1560 | if (UNIT_IS_ACTIVE_OR_RELOADING(state)) | |
1561 | return -EALREADY; | |
1562 | ||
6bf0f408 LP |
1563 | /* Units that aren't loaded cannot be started */ |
1564 | if (u->load_state != UNIT_LOADED) | |
1565 | return -EINVAL; | |
1566 | ||
a82e5507 LP |
1567 | /* If the conditions failed, don't do anything at all. If we |
1568 | * already are activating this call might still be useful to | |
1569 | * speed up activation in case there is some hold-off time, | |
1570 | * but we don't want to recheck the condition in that case. */ | |
1571 | if (state != UNIT_ACTIVATING && | |
1572 | !unit_condition_test(u)) { | |
f2341e0a | 1573 | log_unit_debug(u, "Starting requested but condition failed. Not starting unit."); |
52661efd LP |
1574 | return -EALREADY; |
1575 | } | |
1576 | ||
59fccdc5 LP |
1577 | /* If the asserts failed, fail the entire job */ |
1578 | if (state != UNIT_ACTIVATING && | |
1579 | !unit_assert_test(u)) { | |
f2341e0a | 1580 | log_unit_notice(u, "Starting requested but asserts failed."); |
59fccdc5 LP |
1581 | return -EPROTO; |
1582 | } | |
1583 | ||
d11a7645 LP |
1584 | /* Units of types that aren't supported cannot be |
1585 | * started. Note that we do this test only after the condition | |
1586 | * checks, so that we rather return condition check errors | |
1587 | * (which are usually not considered a true failure) than "not | |
1588 | * supported" errors (which are considered a failure). | |
1589 | */ | |
1590 | if (!unit_supported(u)) | |
1591 | return -EOPNOTSUPP; | |
1592 | ||
92ab323c | 1593 | /* Forward to the main object, if we aren't it. */ |
52990c2e ZJS |
1594 | following = unit_following(u); |
1595 | if (following) { | |
f2341e0a | 1596 | log_unit_debug(u, "Redirecting start request from %s to %s.", u->id, following->id); |
92ab323c LP |
1597 | return unit_start(following); |
1598 | } | |
1599 | ||
1600 | /* If it is stopped, but we cannot start it, then fail */ | |
1601 | if (!UNIT_VTABLE(u)->start) | |
1602 | return -EBADR; | |
1603 | ||
87f0e418 LP |
1604 | /* We don't suppress calls to ->start() here when we are |
1605 | * already starting, to allow this request to be used as a | |
1606 | * "hurry up" call, for example when the unit is in some "auto | |
1607 | * restart" state where it waits for a holdoff timer to elapse | |
1608 | * before it will start again. */ | |
1609 | ||
c1e1601e | 1610 | unit_add_to_dbus_queue(u); |
9e58ff9c | 1611 | |
d1a34ae9 | 1612 | return UNIT_VTABLE(u)->start(u); |
87f0e418 LP |
1613 | } |
1614 | ||
1615 | bool unit_can_start(Unit *u) { | |
1616 | assert(u); | |
1617 | ||
8ff4d2ab LP |
1618 | if (u->load_state != UNIT_LOADED) |
1619 | return false; | |
1620 | ||
1621 | if (!unit_supported(u)) | |
1622 | return false; | |
1623 | ||
87f0e418 LP |
1624 | return !!UNIT_VTABLE(u)->start; |
1625 | } | |
1626 | ||
2528a7a6 LP |
1627 | bool unit_can_isolate(Unit *u) { |
1628 | assert(u); | |
1629 | ||
1630 | return unit_can_start(u) && | |
ac155bb8 | 1631 | u->allow_isolate; |
2528a7a6 LP |
1632 | } |
1633 | ||
87f0e418 LP |
1634 | /* Errors: |
1635 | * -EBADR: This unit type does not support stopping. | |
1636 | * -EALREADY: Unit is already stopped. | |
1637 | * -EAGAIN: An operation is already in progress. Retry later. | |
1638 | */ | |
1639 | int unit_stop(Unit *u) { | |
1640 | UnitActiveState state; | |
92ab323c | 1641 | Unit *following; |
87f0e418 LP |
1642 | |
1643 | assert(u); | |
1644 | ||
87f0e418 | 1645 | state = unit_active_state(u); |
fdf20a31 | 1646 | if (UNIT_IS_INACTIVE_OR_FAILED(state)) |
87f0e418 LP |
1647 | return -EALREADY; |
1648 | ||
0faacd47 LP |
1649 | following = unit_following(u); |
1650 | if (following) { | |
f2341e0a | 1651 | log_unit_debug(u, "Redirecting stop request from %s to %s.", u->id, following->id); |
92ab323c LP |
1652 | return unit_stop(following); |
1653 | } | |
1654 | ||
7898b0cf LP |
1655 | if (!UNIT_VTABLE(u)->stop) |
1656 | return -EBADR; | |
1657 | ||
c1e1601e | 1658 | unit_add_to_dbus_queue(u); |
9e58ff9c | 1659 | |
d1a34ae9 | 1660 | return UNIT_VTABLE(u)->stop(u); |
87f0e418 LP |
1661 | } |
1662 | ||
f5869324 LP |
1663 | bool unit_can_stop(Unit *u) { |
1664 | assert(u); | |
1665 | ||
1666 | if (!unit_supported(u)) | |
1667 | return false; | |
1668 | ||
1669 | if (u->perpetual) | |
1670 | return false; | |
1671 | ||
1672 | return !!UNIT_VTABLE(u)->stop; | |
1673 | } | |
1674 | ||
87f0e418 LP |
1675 | /* Errors: |
1676 | * -EBADR: This unit type does not support reloading. | |
1677 | * -ENOEXEC: Unit is not started. | |
1678 | * -EAGAIN: An operation is already in progress. Retry later. | |
1679 | */ | |
1680 | int unit_reload(Unit *u) { | |
1681 | UnitActiveState state; | |
92ab323c | 1682 | Unit *following; |
87f0e418 LP |
1683 | |
1684 | assert(u); | |
1685 | ||
ac155bb8 | 1686 | if (u->load_state != UNIT_LOADED) |
6124958c LP |
1687 | return -EINVAL; |
1688 | ||
87f0e418 LP |
1689 | if (!unit_can_reload(u)) |
1690 | return -EBADR; | |
1691 | ||
1692 | state = unit_active_state(u); | |
e364ad06 | 1693 | if (state == UNIT_RELOADING) |
87f0e418 LP |
1694 | return -EALREADY; |
1695 | ||
6a371e23 | 1696 | if (state != UNIT_ACTIVE) { |
f2341e0a | 1697 | log_unit_warning(u, "Unit cannot be reloaded because it is inactive."); |
87f0e418 | 1698 | return -ENOEXEC; |
6a371e23 | 1699 | } |
87f0e418 | 1700 | |
e48614c4 ZJS |
1701 | following = unit_following(u); |
1702 | if (following) { | |
f2341e0a | 1703 | log_unit_debug(u, "Redirecting reload request from %s to %s.", u->id, following->id); |
92ab323c LP |
1704 | return unit_reload(following); |
1705 | } | |
1706 | ||
c1e1601e | 1707 | unit_add_to_dbus_queue(u); |
82a2b6bb | 1708 | |
d1a34ae9 | 1709 | return UNIT_VTABLE(u)->reload(u); |
87f0e418 LP |
1710 | } |
1711 | ||
1712 | bool unit_can_reload(Unit *u) { | |
1713 | assert(u); | |
1714 | ||
1715 | if (!UNIT_VTABLE(u)->reload) | |
1716 | return false; | |
1717 | ||
1718 | if (!UNIT_VTABLE(u)->can_reload) | |
1719 | return true; | |
1720 | ||
1721 | return UNIT_VTABLE(u)->can_reload(u); | |
1722 | } | |
1723 | ||
b4a16b7b | 1724 | static void unit_check_unneeded(Unit *u) { |
be7d9ff7 | 1725 | |
4afd3348 | 1726 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4bd29fe5 | 1727 | |
be7d9ff7 LP |
1728 | static const UnitDependency needed_dependencies[] = { |
1729 | UNIT_REQUIRED_BY, | |
084918ba | 1730 | UNIT_REQUISITE_OF, |
be7d9ff7 LP |
1731 | UNIT_WANTED_BY, |
1732 | UNIT_BOUND_BY, | |
1733 | }; | |
1734 | ||
f3bff0eb | 1735 | Unit *other; |
be7d9ff7 LP |
1736 | Iterator i; |
1737 | unsigned j; | |
1738 | int r; | |
f3bff0eb LP |
1739 | |
1740 | assert(u); | |
1741 | ||
1742 | /* If this service shall be shut down when unneeded then do | |
1743 | * so. */ | |
1744 | ||
ac155bb8 | 1745 | if (!u->stop_when_unneeded) |
f3bff0eb LP |
1746 | return; |
1747 | ||
1748 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) | |
1749 | return; | |
1750 | ||
be7d9ff7 | 1751 | for (j = 0; j < ELEMENTSOF(needed_dependencies); j++) |
f3b85044 | 1752 | SET_FOREACH(other, u->dependencies[needed_dependencies[j]], i) |
be7d9ff7 LP |
1753 | if (unit_active_or_pending(other)) |
1754 | return; | |
b81884e7 | 1755 | |
595bfe7d | 1756 | /* If stopping a unit fails continuously we might enter a stop |
bea355da LP |
1757 | * loop here, hence stop acting on the service being |
1758 | * unnecessary after a while. */ | |
67bfdc97 | 1759 | if (!ratelimit_test(&u->auto_stop_ratelimit)) { |
bea355da LP |
1760 | log_unit_warning(u, "Unit not needed anymore, but not stopping since we tried this too often recently."); |
1761 | return; | |
1762 | } | |
1763 | ||
f2341e0a | 1764 | log_unit_info(u, "Unit not needed anymore. Stopping."); |
f3bff0eb LP |
1765 | |
1766 | /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */ | |
4bd29fe5 | 1767 | r = manager_add_job(u->manager, JOB_STOP, u, JOB_FAIL, &error, NULL); |
be7d9ff7 | 1768 | if (r < 0) |
4bd29fe5 | 1769 | log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %s", bus_error_message(&error, r)); |
f3bff0eb LP |
1770 | } |
1771 | ||
ff502445 | 1772 | static void unit_check_binds_to(Unit *u) { |
4afd3348 | 1773 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
ff502445 LP |
1774 | bool stop = false; |
1775 | Unit *other; | |
1776 | Iterator i; | |
67bfdc97 | 1777 | int r; |
ff502445 LP |
1778 | |
1779 | assert(u); | |
1780 | ||
1781 | if (u->job) | |
1782 | return; | |
1783 | ||
1784 | if (unit_active_state(u) != UNIT_ACTIVE) | |
1785 | return; | |
1786 | ||
1787 | SET_FOREACH(other, u->dependencies[UNIT_BINDS_TO], i) { | |
1788 | if (other->job) | |
1789 | continue; | |
1790 | ||
1791 | if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) | |
1792 | continue; | |
1793 | ||
1794 | stop = true; | |
98f738b6 | 1795 | break; |
ff502445 LP |
1796 | } |
1797 | ||
1798 | if (!stop) | |
1799 | return; | |
1800 | ||
595bfe7d | 1801 | /* If stopping a unit fails continuously we might enter a stop |
67bfdc97 LP |
1802 | * loop here, hence stop acting on the service being |
1803 | * unnecessary after a while. */ | |
1804 | if (!ratelimit_test(&u->auto_stop_ratelimit)) { | |
1805 | log_unit_warning(u, "Unit is bound to inactive unit %s, but not stopping since we tried this too often recently.", other->id); | |
1806 | return; | |
1807 | } | |
1808 | ||
98f738b6 | 1809 | assert(other); |
f2341e0a | 1810 | log_unit_info(u, "Unit is bound to inactive unit %s. Stopping, too.", other->id); |
ff502445 LP |
1811 | |
1812 | /* A unit we need to run is gone. Sniff. Let's stop this. */ | |
4bd29fe5 | 1813 | r = manager_add_job(u->manager, JOB_STOP, u, JOB_FAIL, &error, NULL); |
67bfdc97 | 1814 | if (r < 0) |
4bd29fe5 | 1815 | log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %s", bus_error_message(&error, r)); |
ff502445 LP |
1816 | } |
1817 | ||
87f0e418 LP |
1818 | static void retroactively_start_dependencies(Unit *u) { |
1819 | Iterator i; | |
1820 | Unit *other; | |
1821 | ||
1822 | assert(u); | |
1823 | assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))); | |
1824 | ||
ac155bb8 MS |
1825 | SET_FOREACH(other, u->dependencies[UNIT_REQUIRES], i) |
1826 | if (!set_get(u->dependencies[UNIT_AFTER], other) && | |
b81884e7 | 1827 | !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) |
4bd29fe5 | 1828 | manager_add_job(u->manager, JOB_START, other, JOB_REPLACE, NULL, NULL); |
b81884e7 | 1829 | |
7f2cddae | 1830 | SET_FOREACH(other, u->dependencies[UNIT_BINDS_TO], i) |
ac155bb8 | 1831 | if (!set_get(u->dependencies[UNIT_AFTER], other) && |
b81884e7 | 1832 | !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) |
4bd29fe5 | 1833 | manager_add_job(u->manager, JOB_START, other, JOB_REPLACE, NULL, NULL); |
87f0e418 | 1834 | |
ac155bb8 MS |
1835 | SET_FOREACH(other, u->dependencies[UNIT_WANTS], i) |
1836 | if (!set_get(u->dependencies[UNIT_AFTER], other) && | |
b81884e7 | 1837 | !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) |
4bd29fe5 | 1838 | manager_add_job(u->manager, JOB_START, other, JOB_FAIL, NULL, NULL); |
87f0e418 | 1839 | |
ac155bb8 | 1840 | SET_FOREACH(other, u->dependencies[UNIT_CONFLICTS], i) |
b81884e7 | 1841 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
4bd29fe5 | 1842 | manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, NULL, NULL); |
69dd2852 | 1843 | |
ac155bb8 | 1844 | SET_FOREACH(other, u->dependencies[UNIT_CONFLICTED_BY], i) |
b81884e7 | 1845 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
4bd29fe5 | 1846 | manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, NULL, NULL); |
87f0e418 LP |
1847 | } |
1848 | ||
1849 | static void retroactively_stop_dependencies(Unit *u) { | |
1850 | Iterator i; | |
1851 | Unit *other; | |
1852 | ||
1853 | assert(u); | |
1854 | assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))); | |
1855 | ||
b81884e7 | 1856 | /* Pull down units which are bound to us recursively if enabled */ |
ac155bb8 | 1857 | SET_FOREACH(other, u->dependencies[UNIT_BOUND_BY], i) |
b81884e7 | 1858 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
4bd29fe5 | 1859 | manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, NULL, NULL); |
cd0504d0 MS |
1860 | } |
1861 | ||
1862 | static void check_unneeded_dependencies(Unit *u) { | |
1863 | Iterator i; | |
1864 | Unit *other; | |
1865 | ||
1866 | assert(u); | |
1867 | assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))); | |
f3bff0eb LP |
1868 | |
1869 | /* Garbage collect services that might not be needed anymore, if enabled */ | |
ac155bb8 | 1870 | SET_FOREACH(other, u->dependencies[UNIT_REQUIRES], i) |
87f0e418 | 1871 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
b4a16b7b | 1872 | unit_check_unneeded(other); |
ac155bb8 | 1873 | SET_FOREACH(other, u->dependencies[UNIT_WANTS], i) |
f3bff0eb | 1874 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
b4a16b7b | 1875 | unit_check_unneeded(other); |
ac155bb8 | 1876 | SET_FOREACH(other, u->dependencies[UNIT_REQUISITE], i) |
f3bff0eb | 1877 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
b4a16b7b | 1878 | unit_check_unneeded(other); |
7f2cddae | 1879 | SET_FOREACH(other, u->dependencies[UNIT_BINDS_TO], i) |
b81884e7 LP |
1880 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
1881 | unit_check_unneeded(other); | |
87f0e418 LP |
1882 | } |
1883 | ||
3ecaa09b | 1884 | void unit_start_on_failure(Unit *u) { |
c0daa706 LP |
1885 | Unit *other; |
1886 | Iterator i; | |
1887 | ||
1888 | assert(u); | |
1889 | ||
ac155bb8 | 1890 | if (set_size(u->dependencies[UNIT_ON_FAILURE]) <= 0) |
222ae6a8 LP |
1891 | return; |
1892 | ||
f2341e0a | 1893 | log_unit_info(u, "Triggering OnFailure= dependencies."); |
222ae6a8 | 1894 | |
ac155bb8 | 1895 | SET_FOREACH(other, u->dependencies[UNIT_ON_FAILURE], i) { |
222ae6a8 LP |
1896 | int r; |
1897 | ||
4bd29fe5 | 1898 | r = manager_add_job(u->manager, JOB_START, other, u->on_failure_job_mode, NULL, NULL); |
c1b6628d | 1899 | if (r < 0) |
f2341e0a | 1900 | log_unit_error_errno(u, r, "Failed to enqueue OnFailure= job: %m"); |
222ae6a8 | 1901 | } |
c0daa706 LP |
1902 | } |
1903 | ||
3ecaa09b LP |
1904 | void unit_trigger_notify(Unit *u) { |
1905 | Unit *other; | |
1906 | Iterator i; | |
1907 | ||
1908 | assert(u); | |
1909 | ||
1910 | SET_FOREACH(other, u->dependencies[UNIT_TRIGGERED_BY], i) | |
1911 | if (UNIT_VTABLE(other)->trigger_notify) | |
1912 | UNIT_VTABLE(other)->trigger_notify(other, u); | |
1913 | } | |
1914 | ||
e2f3b44c | 1915 | void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_success) { |
546ac4f0 | 1916 | Manager *m; |
7e6e7b06 | 1917 | bool unexpected; |
a096ed36 | 1918 | |
87f0e418 LP |
1919 | assert(u); |
1920 | assert(os < _UNIT_ACTIVE_STATE_MAX); | |
1921 | assert(ns < _UNIT_ACTIVE_STATE_MAX); | |
87f0e418 | 1922 | |
8e471ccd LP |
1923 | /* Note that this is called for all low-level state changes, |
1924 | * even if they might map to the same high-level | |
865cc19a | 1925 | * UnitActiveState! That means that ns == os is an expected |
c5315881 | 1926 | * behavior here. For example: if a mount point is remounted |
cd6d0a45 | 1927 | * this function will be called too! */ |
87f0e418 | 1928 | |
546ac4f0 MS |
1929 | m = u->manager; |
1930 | ||
f755e3b7 | 1931 | /* Update timestamps for state changes */ |
2c289ea8 | 1932 | if (!MANAGER_IS_RELOADING(m)) { |
a483fb59 | 1933 | dual_timestamp_get(&u->state_change_timestamp); |
173e3821 | 1934 | |
bdbf9951 | 1935 | if (UNIT_IS_INACTIVE_OR_FAILED(os) && !UNIT_IS_INACTIVE_OR_FAILED(ns)) |
a483fb59 | 1936 | u->inactive_exit_timestamp = u->state_change_timestamp; |
bdbf9951 | 1937 | else if (!UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_INACTIVE_OR_FAILED(ns)) |
a483fb59 | 1938 | u->inactive_enter_timestamp = u->state_change_timestamp; |
bdbf9951 LP |
1939 | |
1940 | if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns)) | |
a483fb59 | 1941 | u->active_enter_timestamp = u->state_change_timestamp; |
bdbf9951 | 1942 | else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns)) |
a483fb59 | 1943 | u->active_exit_timestamp = u->state_change_timestamp; |
bdbf9951 | 1944 | } |
87f0e418 | 1945 | |
865cc19a | 1946 | /* Keep track of failed units */ |
5269eb6b | 1947 | (void) manager_update_failed_units(u->manager, u, ns == UNIT_FAILED); |
f755e3b7 LP |
1948 | |
1949 | /* Make sure the cgroup is always removed when we become inactive */ | |
fdf20a31 | 1950 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) |
efdb0237 | 1951 | unit_prune_cgroup(u); |
fb385181 | 1952 | |
9cd86184 | 1953 | /* Note that this doesn't apply to RemainAfterExit services exiting |
6f285378 | 1954 | * successfully, since there's no change of state in that case. Which is |
9cd86184 | 1955 | * why it is handled in service_set_state() */ |
7ed9f6cd | 1956 | if (UNIT_IS_INACTIVE_OR_FAILED(os) != UNIT_IS_INACTIVE_OR_FAILED(ns)) { |
b33918c2 LP |
1957 | ExecContext *ec; |
1958 | ||
1959 | ec = unit_get_exec_context(u); | |
7ed9f6cd | 1960 | if (ec && exec_context_may_touch_console(ec)) { |
31a7eb86 | 1961 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) { |
313cefa1 | 1962 | m->n_on_console--; |
31a7eb86 ZJS |
1963 | |
1964 | if (m->n_on_console == 0) | |
1965 | /* unset no_console_output flag, since the console is free */ | |
2f38577f | 1966 | m->no_console_output = false; |
31a7eb86 | 1967 | } else |
313cefa1 | 1968 | m->n_on_console++; |
7ed9f6cd MS |
1969 | } |
1970 | } | |
1971 | ||
ac155bb8 | 1972 | if (u->job) { |
7e6e7b06 | 1973 | unexpected = false; |
87f0e418 | 1974 | |
ac155bb8 | 1975 | if (u->job->state == JOB_WAITING) |
87f0e418 LP |
1976 | |
1977 | /* So we reached a different state for this | |
1978 | * job. Let's see if we can run it now if it | |
1979 | * failed previously due to EAGAIN. */ | |
ac155bb8 | 1980 | job_add_to_run_queue(u->job); |
87f0e418 | 1981 | |
b410e6b9 | 1982 | /* Let's check whether this state change constitutes a |
35b8ca3a | 1983 | * finished job, or maybe contradicts a running job and |
b410e6b9 | 1984 | * hence needs to invalidate jobs. */ |
87f0e418 | 1985 | |
ac155bb8 | 1986 | switch (u->job->type) { |
87f0e418 | 1987 | |
b410e6b9 LP |
1988 | case JOB_START: |
1989 | case JOB_VERIFY_ACTIVE: | |
87f0e418 | 1990 | |
b410e6b9 | 1991 | if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) |
833f92ad | 1992 | job_finish_and_invalidate(u->job, JOB_DONE, true, false); |
ac155bb8 | 1993 | else if (u->job->state == JOB_RUNNING && ns != UNIT_ACTIVATING) { |
b410e6b9 | 1994 | unexpected = true; |
41b02ec7 | 1995 | |
fdf20a31 | 1996 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) |
833f92ad | 1997 | job_finish_and_invalidate(u->job, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE, true, false); |
b410e6b9 | 1998 | } |
87f0e418 | 1999 | |
b410e6b9 | 2000 | break; |
87f0e418 | 2001 | |
b410e6b9 LP |
2002 | case JOB_RELOAD: |
2003 | case JOB_RELOAD_OR_START: | |
3282591d | 2004 | case JOB_TRY_RELOAD: |
87f0e418 | 2005 | |
ac155bb8 | 2006 | if (u->job->state == JOB_RUNNING) { |
a096ed36 | 2007 | if (ns == UNIT_ACTIVE) |
833f92ad | 2008 | job_finish_and_invalidate(u->job, reload_success ? JOB_DONE : JOB_FAILED, true, false); |
032ff4af | 2009 | else if (ns != UNIT_ACTIVATING && ns != UNIT_RELOADING) { |
a096ed36 | 2010 | unexpected = true; |
41b02ec7 | 2011 | |
fdf20a31 | 2012 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) |
833f92ad | 2013 | job_finish_and_invalidate(u->job, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE, true, false); |
a096ed36 | 2014 | } |
b410e6b9 | 2015 | } |
87f0e418 | 2016 | |
b410e6b9 | 2017 | break; |
87f0e418 | 2018 | |
b410e6b9 LP |
2019 | case JOB_STOP: |
2020 | case JOB_RESTART: | |
2021 | case JOB_TRY_RESTART: | |
87f0e418 | 2022 | |
fdf20a31 | 2023 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) |
833f92ad | 2024 | job_finish_and_invalidate(u->job, JOB_DONE, true, false); |
ac155bb8 | 2025 | else if (u->job->state == JOB_RUNNING && ns != UNIT_DEACTIVATING) { |
b410e6b9 | 2026 | unexpected = true; |
833f92ad | 2027 | job_finish_and_invalidate(u->job, JOB_FAILED, true, false); |
b410e6b9 | 2028 | } |
87f0e418 | 2029 | |
b410e6b9 | 2030 | break; |
87f0e418 | 2031 | |
b410e6b9 LP |
2032 | default: |
2033 | assert_not_reached("Job type unknown"); | |
87f0e418 | 2034 | } |
87f0e418 | 2035 | |
7e6e7b06 LP |
2036 | } else |
2037 | unexpected = true; | |
2038 | ||
2c289ea8 | 2039 | if (!MANAGER_IS_RELOADING(m)) { |
f3bff0eb | 2040 | |
bdbf9951 LP |
2041 | /* If this state change happened without being |
2042 | * requested by a job, then let's retroactively start | |
2043 | * or stop dependencies. We skip that step when | |
2044 | * deserializing, since we don't want to create any | |
2045 | * additional jobs just because something is already | |
2046 | * activated. */ | |
2047 | ||
2048 | if (unexpected) { | |
2049 | if (UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns)) | |
2050 | retroactively_start_dependencies(u); | |
2051 | else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns)) | |
2052 | retroactively_stop_dependencies(u); | |
2053 | } | |
5de9682c | 2054 | |
cd0504d0 | 2055 | /* stop unneeded units regardless if going down was expected or not */ |
b33918c2 | 2056 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(ns)) |
cd0504d0 MS |
2057 | check_unneeded_dependencies(u); |
2058 | ||
bdbf9951 | 2059 | if (ns != os && ns == UNIT_FAILED) { |
f2341e0a | 2060 | log_unit_notice(u, "Unit entered failed state."); |
3ecaa09b | 2061 | unit_start_on_failure(u); |
cd6d0a45 | 2062 | } |
3b2775c5 | 2063 | } |
e537352b | 2064 | |
3b2775c5 LP |
2065 | /* Some names are special */ |
2066 | if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) { | |
2067 | ||
2068 | if (unit_has_name(u, SPECIAL_DBUS_SERVICE)) | |
865cc19a | 2069 | /* The bus might have just become available, |
3b2775c5 LP |
2070 | * hence try to connect to it, if we aren't |
2071 | * yet connected. */ | |
546ac4f0 | 2072 | bus_init(m, true); |
3b2775c5 | 2073 | |
ac155bb8 | 2074 | if (u->type == UNIT_SERVICE && |
3b2775c5 | 2075 | !UNIT_IS_ACTIVE_OR_RELOADING(os) && |
2c289ea8 | 2076 | !MANAGER_IS_RELOADING(m)) { |
3b2775c5 | 2077 | /* Write audit record if we have just finished starting up */ |
546ac4f0 | 2078 | manager_send_unit_audit(m, u, AUDIT_SERVICE_START, true); |
ac155bb8 | 2079 | u->in_audit = true; |
3b2775c5 | 2080 | } |
e983b760 | 2081 | |
3b2775c5 | 2082 | if (!UNIT_IS_ACTIVE_OR_RELOADING(os)) |
546ac4f0 | 2083 | manager_send_unit_plymouth(m, u); |
bdbf9951 | 2084 | |
3b2775c5 | 2085 | } else { |
bdbf9951 | 2086 | |
3b2775c5 LP |
2087 | /* We don't care about D-Bus here, since we'll get an |
2088 | * asynchronous notification for it anyway. */ | |
cd6d0a45 | 2089 | |
ac155bb8 | 2090 | if (u->type == UNIT_SERVICE && |
3b2775c5 LP |
2091 | UNIT_IS_INACTIVE_OR_FAILED(ns) && |
2092 | !UNIT_IS_INACTIVE_OR_FAILED(os) && | |
2c289ea8 | 2093 | !MANAGER_IS_RELOADING(m)) { |
4927fcae | 2094 | |
3b2775c5 LP |
2095 | /* Hmm, if there was no start record written |
2096 | * write it now, so that we always have a nice | |
2097 | * pair */ | |
ac155bb8 | 2098 | if (!u->in_audit) { |
546ac4f0 | 2099 | manager_send_unit_audit(m, u, AUDIT_SERVICE_START, ns == UNIT_INACTIVE); |
cd6d0a45 | 2100 | |
3b2775c5 | 2101 | if (ns == UNIT_INACTIVE) |
546ac4f0 | 2102 | manager_send_unit_audit(m, u, AUDIT_SERVICE_STOP, true); |
3b2775c5 LP |
2103 | } else |
2104 | /* Write audit record if we have just finished shutting down */ | |
546ac4f0 | 2105 | manager_send_unit_audit(m, u, AUDIT_SERVICE_STOP, ns == UNIT_INACTIVE); |
bdbf9951 | 2106 | |
ac155bb8 | 2107 | u->in_audit = false; |
cd6d0a45 | 2108 | } |
f278026d LP |
2109 | } |
2110 | ||
546ac4f0 | 2111 | manager_recheck_journal(m); |
3ecaa09b | 2112 | unit_trigger_notify(u); |
3b2775c5 | 2113 | |
2c289ea8 | 2114 | if (!MANAGER_IS_RELOADING(u->manager)) { |
ff502445 LP |
2115 | /* Maybe we finished startup and are now ready for |
2116 | * being stopped because unneeded? */ | |
bf6dcfa6 | 2117 | unit_check_unneeded(u); |
c1e1601e | 2118 | |
ff502445 LP |
2119 | /* Maybe we finished startup, but something we needed |
2120 | * has vanished? Let's die then. (This happens when | |
2121 | * something BindsTo= to a Type=oneshot unit, as these | |
2122 | * units go directly from starting to inactive, | |
2123 | * without ever entering started.) */ | |
2124 | unit_check_binds_to(u); | |
2125 | } | |
2126 | ||
c1e1601e | 2127 | unit_add_to_dbus_queue(u); |
701cc384 | 2128 | unit_add_to_gc_queue(u); |
87f0e418 LP |
2129 | } |
2130 | ||
87f0e418 | 2131 | int unit_watch_pid(Unit *u, pid_t pid) { |
a911bb9a LP |
2132 | int q, r; |
2133 | ||
87f0e418 LP |
2134 | assert(u); |
2135 | assert(pid >= 1); | |
2136 | ||
5ba6985b LP |
2137 | /* Watch a specific PID. We only support one or two units |
2138 | * watching each PID for now, not more. */ | |
2139 | ||
d5099efc | 2140 | r = set_ensure_allocated(&u->pids, NULL); |
5ba6985b LP |
2141 | if (r < 0) |
2142 | return r; | |
2143 | ||
d5099efc | 2144 | r = hashmap_ensure_allocated(&u->manager->watch_pids1, NULL); |
a911bb9a LP |
2145 | if (r < 0) |
2146 | return r; | |
2147 | ||
fea72cc0 | 2148 | r = hashmap_put(u->manager->watch_pids1, PID_TO_PTR(pid), u); |
5ba6985b | 2149 | if (r == -EEXIST) { |
d5099efc | 2150 | r = hashmap_ensure_allocated(&u->manager->watch_pids2, NULL); |
5ba6985b LP |
2151 | if (r < 0) |
2152 | return r; | |
05e343b7 | 2153 | |
fea72cc0 | 2154 | r = hashmap_put(u->manager->watch_pids2, PID_TO_PTR(pid), u); |
5ba6985b | 2155 | } |
a911bb9a | 2156 | |
fea72cc0 | 2157 | q = set_put(u->pids, PID_TO_PTR(pid)); |
a911bb9a LP |
2158 | if (q < 0) |
2159 | return q; | |
2160 | ||
2161 | return r; | |
87f0e418 LP |
2162 | } |
2163 | ||
2164 | void unit_unwatch_pid(Unit *u, pid_t pid) { | |
2165 | assert(u); | |
2166 | assert(pid >= 1); | |
2167 | ||
fea72cc0 LP |
2168 | (void) hashmap_remove_value(u->manager->watch_pids1, PID_TO_PTR(pid), u); |
2169 | (void) hashmap_remove_value(u->manager->watch_pids2, PID_TO_PTR(pid), u); | |
2170 | (void) set_remove(u->pids, PID_TO_PTR(pid)); | |
a911bb9a LP |
2171 | } |
2172 | ||
bd44e61b LP |
2173 | void unit_unwatch_all_pids(Unit *u) { |
2174 | assert(u); | |
2175 | ||
2176 | while (!set_isempty(u->pids)) | |
fea72cc0 | 2177 | unit_unwatch_pid(u, PTR_TO_PID(set_first(u->pids))); |
bd44e61b | 2178 | |
efdb0237 | 2179 | u->pids = set_free(u->pids); |
a911bb9a LP |
2180 | } |
2181 | ||
2182 | void unit_tidy_watch_pids(Unit *u, pid_t except1, pid_t except2) { | |
2183 | Iterator i; | |
2184 | void *e; | |
2185 | ||
2186 | assert(u); | |
2187 | ||
2188 | /* Cleans dead PIDs from our list */ | |
2189 | ||
2190 | SET_FOREACH(e, u->pids, i) { | |
fea72cc0 | 2191 | pid_t pid = PTR_TO_PID(e); |
a911bb9a LP |
2192 | |
2193 | if (pid == except1 || pid == except2) | |
2194 | continue; | |
2195 | ||
9f5650ae | 2196 | if (!pid_is_unwaited(pid)) |
bd44e61b | 2197 | unit_unwatch_pid(u, pid); |
a911bb9a | 2198 | } |
87f0e418 LP |
2199 | } |
2200 | ||
87f0e418 LP |
2201 | bool unit_job_is_applicable(Unit *u, JobType j) { |
2202 | assert(u); | |
2203 | assert(j >= 0 && j < _JOB_TYPE_MAX); | |
2204 | ||
2205 | switch (j) { | |
2206 | ||
2207 | case JOB_VERIFY_ACTIVE: | |
2208 | case JOB_START: | |
e0209d83 | 2209 | case JOB_NOP: |
f5869324 LP |
2210 | /* Note that we don't check unit_can_start() here. That's because .device units and suchlike are not |
2211 | * startable by us but may appear due to external events, and it thus makes sense to permit enqueing | |
2212 | * jobs for it. */ | |
87f0e418 LP |
2213 | return true; |
2214 | ||
f5869324 LP |
2215 | case JOB_STOP: |
2216 | /* Similar as above. However, perpetual units can never be stopped (neither explicitly nor due to | |
2217 | * external events), hence it makes no sense to permit enqueing such a request either. */ | |
2218 | return !u->perpetual; | |
2219 | ||
87f0e418 LP |
2220 | case JOB_RESTART: |
2221 | case JOB_TRY_RESTART: | |
f5869324 | 2222 | return unit_can_stop(u) && unit_can_start(u); |
87f0e418 LP |
2223 | |
2224 | case JOB_RELOAD: | |
3282591d | 2225 | case JOB_TRY_RELOAD: |
87f0e418 LP |
2226 | return unit_can_reload(u); |
2227 | ||
2228 | case JOB_RELOAD_OR_START: | |
2229 | return unit_can_reload(u) && unit_can_start(u); | |
2230 | ||
2231 | default: | |
2232 | assert_not_reached("Invalid job type"); | |
2233 | } | |
2234 | } | |
2235 | ||
f2341e0a LP |
2236 | static void maybe_warn_about_dependency(Unit *u, const char *other, UnitDependency dependency) { |
2237 | assert(u); | |
d1fab3fe | 2238 | |
f2341e0a LP |
2239 | /* Only warn about some unit types */ |
2240 | if (!IN_SET(dependency, UNIT_CONFLICTS, UNIT_CONFLICTED_BY, UNIT_BEFORE, UNIT_AFTER, UNIT_ON_FAILURE, UNIT_TRIGGERS, UNIT_TRIGGERED_BY)) | |
2241 | return; | |
3f3cc397 | 2242 | |
f2341e0a LP |
2243 | if (streq_ptr(u->id, other)) |
2244 | log_unit_warning(u, "Dependency %s=%s dropped", unit_dependency_to_string(dependency), u->id); | |
2245 | else | |
2246 | log_unit_warning(u, "Dependency %s=%s dropped, merged into %s", unit_dependency_to_string(dependency), strna(other), u->id); | |
d1fab3fe ZJS |
2247 | } |
2248 | ||
701cc384 | 2249 | int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference) { |
87f0e418 LP |
2250 | |
2251 | static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = { | |
2252 | [UNIT_REQUIRES] = UNIT_REQUIRED_BY, | |
87f0e418 | 2253 | [UNIT_WANTS] = UNIT_WANTED_BY, |
be7d9ff7 | 2254 | [UNIT_REQUISITE] = UNIT_REQUISITE_OF, |
7f2cddae | 2255 | [UNIT_BINDS_TO] = UNIT_BOUND_BY, |
60649f17 | 2256 | [UNIT_PART_OF] = UNIT_CONSISTS_OF, |
be7d9ff7 | 2257 | [UNIT_REQUIRED_BY] = UNIT_REQUIRES, |
be7d9ff7 | 2258 | [UNIT_REQUISITE_OF] = UNIT_REQUISITE, |
be7d9ff7 | 2259 | [UNIT_WANTED_BY] = UNIT_WANTS, |
7f2cddae | 2260 | [UNIT_BOUND_BY] = UNIT_BINDS_TO, |
60649f17 | 2261 | [UNIT_CONSISTS_OF] = UNIT_PART_OF, |
69dd2852 LP |
2262 | [UNIT_CONFLICTS] = UNIT_CONFLICTED_BY, |
2263 | [UNIT_CONFLICTED_BY] = UNIT_CONFLICTS, | |
87f0e418 | 2264 | [UNIT_BEFORE] = UNIT_AFTER, |
701cc384 | 2265 | [UNIT_AFTER] = UNIT_BEFORE, |
5de9682c | 2266 | [UNIT_ON_FAILURE] = _UNIT_DEPENDENCY_INVALID, |
701cc384 | 2267 | [UNIT_REFERENCES] = UNIT_REFERENCED_BY, |
57020a3a LP |
2268 | [UNIT_REFERENCED_BY] = UNIT_REFERENCES, |
2269 | [UNIT_TRIGGERS] = UNIT_TRIGGERED_BY, | |
4dcc1cb4 | 2270 | [UNIT_TRIGGERED_BY] = UNIT_TRIGGERS, |
7f2cddae | 2271 | [UNIT_PROPAGATES_RELOAD_TO] = UNIT_RELOAD_PROPAGATED_FROM, |
85e9a101 | 2272 | [UNIT_RELOAD_PROPAGATED_FROM] = UNIT_PROPAGATES_RELOAD_TO, |
613b411c | 2273 | [UNIT_JOINS_NAMESPACE_OF] = UNIT_JOINS_NAMESPACE_OF, |
87f0e418 | 2274 | }; |
701cc384 | 2275 | int r, q = 0, v = 0, w = 0; |
d1fab3fe | 2276 | Unit *orig_u = u, *orig_other = other; |
87f0e418 LP |
2277 | |
2278 | assert(u); | |
2279 | assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX); | |
87f0e418 LP |
2280 | assert(other); |
2281 | ||
9f151f29 LP |
2282 | u = unit_follow_merge(u); |
2283 | other = unit_follow_merge(other); | |
2284 | ||
87f0e418 LP |
2285 | /* We won't allow dependencies on ourselves. We will not |
2286 | * consider them an error however. */ | |
d1fab3fe | 2287 | if (u == other) { |
f2341e0a | 2288 | maybe_warn_about_dependency(orig_u, orig_other->id, d); |
87f0e418 | 2289 | return 0; |
d1fab3fe | 2290 | } |
87f0e418 | 2291 | |
dd5e7000 ZJS |
2292 | if (d == UNIT_BEFORE && other->type == UNIT_DEVICE) { |
2293 | log_unit_warning(u, "Dependency Before=%s ignored (.device units cannot be delayed)", other->id); | |
2294 | return 0; | |
2295 | } | |
2296 | ||
d5099efc | 2297 | r = set_ensure_allocated(&u->dependencies[d], NULL); |
613b411c | 2298 | if (r < 0) |
87f0e418 LP |
2299 | return r; |
2300 | ||
613b411c | 2301 | if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID) { |
d5099efc | 2302 | r = set_ensure_allocated(&other->dependencies[inverse_table[d]], NULL); |
613b411c LP |
2303 | if (r < 0) |
2304 | return r; | |
2305 | } | |
2306 | ||
2307 | if (add_reference) { | |
d5099efc | 2308 | r = set_ensure_allocated(&u->dependencies[UNIT_REFERENCES], NULL); |
613b411c | 2309 | if (r < 0) |
5de9682c LP |
2310 | return r; |
2311 | ||
d5099efc | 2312 | r = set_ensure_allocated(&other->dependencies[UNIT_REFERENCED_BY], NULL); |
613b411c | 2313 | if (r < 0) |
701cc384 | 2314 | return r; |
613b411c | 2315 | } |
87f0e418 | 2316 | |
613b411c LP |
2317 | q = set_put(u->dependencies[d], other); |
2318 | if (q < 0) | |
701cc384 | 2319 | return q; |
87f0e418 | 2320 | |
613b411c LP |
2321 | if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID && inverse_table[d] != d) { |
2322 | v = set_put(other->dependencies[inverse_table[d]], u); | |
2323 | if (v < 0) { | |
5de9682c LP |
2324 | r = v; |
2325 | goto fail; | |
2326 | } | |
613b411c | 2327 | } |
701cc384 LP |
2328 | |
2329 | if (add_reference) { | |
613b411c LP |
2330 | w = set_put(u->dependencies[UNIT_REFERENCES], other); |
2331 | if (w < 0) { | |
701cc384 LP |
2332 | r = w; |
2333 | goto fail; | |
2334 | } | |
2335 | ||
613b411c LP |
2336 | r = set_put(other->dependencies[UNIT_REFERENCED_BY], u); |
2337 | if (r < 0) | |
701cc384 | 2338 | goto fail; |
87f0e418 LP |
2339 | } |
2340 | ||
c1e1601e | 2341 | unit_add_to_dbus_queue(u); |
87f0e418 | 2342 | return 0; |
701cc384 LP |
2343 | |
2344 | fail: | |
2345 | if (q > 0) | |
ac155bb8 | 2346 | set_remove(u->dependencies[d], other); |
701cc384 LP |
2347 | |
2348 | if (v > 0) | |
ac155bb8 | 2349 | set_remove(other->dependencies[inverse_table[d]], u); |
701cc384 LP |
2350 | |
2351 | if (w > 0) | |
ac155bb8 | 2352 | set_remove(u->dependencies[UNIT_REFERENCES], other); |
701cc384 LP |
2353 | |
2354 | return r; | |
87f0e418 | 2355 | } |
0301abf4 | 2356 | |
2c966c03 LP |
2357 | int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference) { |
2358 | int r; | |
2359 | ||
2360 | assert(u); | |
2361 | ||
3f3cc397 LP |
2362 | r = unit_add_dependency(u, d, other, add_reference); |
2363 | if (r < 0) | |
2c966c03 LP |
2364 | return r; |
2365 | ||
7410616c | 2366 | return unit_add_dependency(u, e, other, add_reference); |
2c966c03 LP |
2367 | } |
2368 | ||
7410616c LP |
2369 | static int resolve_template(Unit *u, const char *name, const char*path, char **buf, const char **ret) { |
2370 | int r; | |
9e2f7c11 LP |
2371 | |
2372 | assert(u); | |
2373 | assert(name || path); | |
7410616c LP |
2374 | assert(buf); |
2375 | assert(ret); | |
9e2f7c11 LP |
2376 | |
2377 | if (!name) | |
2b6bf07d | 2378 | name = basename(path); |
9e2f7c11 | 2379 | |
7410616c LP |
2380 | if (!unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
2381 | *buf = NULL; | |
2382 | *ret = name; | |
2383 | return 0; | |
9e2f7c11 LP |
2384 | } |
2385 | ||
ac155bb8 | 2386 | if (u->instance) |
7410616c | 2387 | r = unit_name_replace_instance(name, u->instance, buf); |
9e2f7c11 | 2388 | else { |
ae018d9b | 2389 | _cleanup_free_ char *i = NULL; |
9e2f7c11 | 2390 | |
7410616c LP |
2391 | r = unit_name_to_prefix(u->id, &i); |
2392 | if (r < 0) | |
2393 | return r; | |
9e2f7c11 | 2394 | |
7410616c | 2395 | r = unit_name_replace_instance(name, i, buf); |
9e2f7c11 | 2396 | } |
7410616c LP |
2397 | if (r < 0) |
2398 | return r; | |
9e2f7c11 | 2399 | |
7410616c LP |
2400 | *ret = *buf; |
2401 | return 0; | |
9e2f7c11 LP |
2402 | } |
2403 | ||
701cc384 | 2404 | int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) { |
7410616c | 2405 | _cleanup_free_ char *buf = NULL; |
09b6b09f LP |
2406 | Unit *other; |
2407 | int r; | |
2408 | ||
9e2f7c11 LP |
2409 | assert(u); |
2410 | assert(name || path); | |
09b6b09f | 2411 | |
7410616c LP |
2412 | r = resolve_template(u, name, path, &buf, &name); |
2413 | if (r < 0) | |
2414 | return r; | |
09b6b09f | 2415 | |
8afbb8e1 LP |
2416 | r = manager_load_unit(u->manager, name, path, NULL, &other); |
2417 | if (r < 0) | |
2418 | return r; | |
9e2f7c11 | 2419 | |
8afbb8e1 | 2420 | return unit_add_dependency(u, d, other, add_reference); |
09b6b09f LP |
2421 | } |
2422 | ||
2c966c03 | 2423 | int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) { |
7410616c | 2424 | _cleanup_free_ char *buf = NULL; |
2c966c03 LP |
2425 | Unit *other; |
2426 | int r; | |
2c966c03 LP |
2427 | |
2428 | assert(u); | |
2429 | assert(name || path); | |
2430 | ||
7410616c LP |
2431 | r = resolve_template(u, name, path, &buf, &name); |
2432 | if (r < 0) | |
2433 | return r; | |
2c966c03 | 2434 | |
3f3cc397 LP |
2435 | r = manager_load_unit(u->manager, name, path, NULL, &other); |
2436 | if (r < 0) | |
68eda4bd | 2437 | return r; |
2c966c03 | 2438 | |
3f3cc397 | 2439 | return unit_add_two_dependencies(u, d, e, other, add_reference); |
2c966c03 LP |
2440 | } |
2441 | ||
0301abf4 | 2442 | int set_unit_path(const char *p) { |
0301abf4 | 2443 | /* This is mostly for debug purposes */ |
cbe46ead | 2444 | if (setenv("SYSTEMD_UNIT_PATH", p, 1) < 0) |
26d04f86 | 2445 | return -errno; |
0301abf4 LP |
2446 | |
2447 | return 0; | |
2448 | } | |
88066b3a | 2449 | |
ea430986 | 2450 | char *unit_dbus_path(Unit *u) { |
ea430986 LP |
2451 | assert(u); |
2452 | ||
ac155bb8 | 2453 | if (!u->id) |
04ade7d2 LP |
2454 | return NULL; |
2455 | ||
48899192 | 2456 | return unit_dbus_path_from_name(u->id); |
ea430986 LP |
2457 | } |
2458 | ||
4b58153d LP |
2459 | char *unit_dbus_path_invocation_id(Unit *u) { |
2460 | assert(u); | |
2461 | ||
2462 | if (sd_id128_is_null(u->invocation_id)) | |
2463 | return NULL; | |
2464 | ||
2465 | return unit_dbus_path_from_name(u->invocation_id_string); | |
2466 | } | |
2467 | ||
d79200e2 LP |
2468 | int unit_set_slice(Unit *u, Unit *slice) { |
2469 | assert(u); | |
2470 | assert(slice); | |
2471 | ||
2472 | /* Sets the unit slice if it has not been set before. Is extra | |
2473 | * careful, to only allow this for units that actually have a | |
2474 | * cgroup context. Also, we don't allow to set this for slices | |
2475 | * (since the parent slice is derived from the name). Make | |
2476 | * sure the unit we set is actually a slice. */ | |
2477 | ||
2478 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) | |
2479 | return -EOPNOTSUPP; | |
2480 | ||
2481 | if (u->type == UNIT_SLICE) | |
2482 | return -EINVAL; | |
2483 | ||
102ef982 LP |
2484 | if (unit_active_state(u) != UNIT_INACTIVE) |
2485 | return -EBUSY; | |
2486 | ||
d79200e2 LP |
2487 | if (slice->type != UNIT_SLICE) |
2488 | return -EINVAL; | |
2489 | ||
efdb0237 LP |
2490 | if (unit_has_name(u, SPECIAL_INIT_SCOPE) && |
2491 | !unit_has_name(slice, SPECIAL_ROOT_SLICE)) | |
2492 | return -EPERM; | |
2493 | ||
d79200e2 LP |
2494 | if (UNIT_DEREF(u->slice) == slice) |
2495 | return 0; | |
2496 | ||
99e66921 TH |
2497 | /* Disallow slice changes if @u is already bound to cgroups */ |
2498 | if (UNIT_ISSET(u->slice) && u->cgroup_realized) | |
d79200e2 LP |
2499 | return -EBUSY; |
2500 | ||
99e66921 | 2501 | unit_ref_unset(&u->slice); |
d79200e2 LP |
2502 | unit_ref_set(&u->slice, slice); |
2503 | return 1; | |
2504 | } | |
2505 | ||
2506 | int unit_set_default_slice(Unit *u) { | |
a8833944 LP |
2507 | _cleanup_free_ char *b = NULL; |
2508 | const char *slice_name; | |
a016b922 LP |
2509 | Unit *slice; |
2510 | int r; | |
2511 | ||
2512 | assert(u); | |
2513 | ||
9444b1f2 | 2514 | if (UNIT_ISSET(u->slice)) |
a016b922 LP |
2515 | return 0; |
2516 | ||
a8833944 LP |
2517 | if (u->instance) { |
2518 | _cleanup_free_ char *prefix = NULL, *escaped = NULL; | |
68eda4bd | 2519 | |
a8833944 LP |
2520 | /* Implicitly place all instantiated units in their |
2521 | * own per-template slice */ | |
2522 | ||
7410616c LP |
2523 | r = unit_name_to_prefix(u->id, &prefix); |
2524 | if (r < 0) | |
2525 | return r; | |
a8833944 LP |
2526 | |
2527 | /* The prefix is already escaped, but it might include | |
2528 | * "-" which has a special meaning for slice units, | |
2529 | * hence escape it here extra. */ | |
7410616c | 2530 | escaped = unit_name_escape(prefix); |
a8833944 LP |
2531 | if (!escaped) |
2532 | return -ENOMEM; | |
2533 | ||
463d0d15 | 2534 | if (MANAGER_IS_SYSTEM(u->manager)) |
605405c6 | 2535 | b = strjoin("system-", escaped, ".slice"); |
a8833944 LP |
2536 | else |
2537 | b = strappend(escaped, ".slice"); | |
2538 | if (!b) | |
2539 | return -ENOMEM; | |
2540 | ||
2541 | slice_name = b; | |
2542 | } else | |
2543 | slice_name = | |
463d0d15 | 2544 | MANAGER_IS_SYSTEM(u->manager) && !unit_has_name(u, SPECIAL_INIT_SCOPE) |
a8833944 LP |
2545 | ? SPECIAL_SYSTEM_SLICE |
2546 | : SPECIAL_ROOT_SLICE; | |
2547 | ||
2548 | r = manager_load_unit(u->manager, slice_name, NULL, NULL, &slice); | |
a016b922 LP |
2549 | if (r < 0) |
2550 | return r; | |
2551 | ||
d79200e2 | 2552 | return unit_set_slice(u, slice); |
a016b922 LP |
2553 | } |
2554 | ||
9444b1f2 LP |
2555 | const char *unit_slice_name(Unit *u) { |
2556 | assert(u); | |
2557 | ||
2558 | if (!UNIT_ISSET(u->slice)) | |
2559 | return NULL; | |
2560 | ||
2561 | return UNIT_DEREF(u->slice)->id; | |
2562 | } | |
2563 | ||
f6ff8c29 | 2564 | int unit_load_related_unit(Unit *u, const char *type, Unit **_found) { |
78edb35a | 2565 | _cleanup_free_ char *t = NULL; |
f6ff8c29 LP |
2566 | int r; |
2567 | ||
2568 | assert(u); | |
2569 | assert(type); | |
2570 | assert(_found); | |
2571 | ||
7410616c LP |
2572 | r = unit_name_change_suffix(u->id, type, &t); |
2573 | if (r < 0) | |
2574 | return r; | |
2575 | if (unit_has_name(u, t)) | |
2576 | return -EINVAL; | |
f6ff8c29 | 2577 | |
ac155bb8 | 2578 | r = manager_load_unit(u->manager, t, NULL, NULL, _found); |
9e2f7c11 | 2579 | assert(r < 0 || *_found != u); |
f6ff8c29 LP |
2580 | return r; |
2581 | } | |
2582 | ||
bbc29086 DM |
2583 | static int signal_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2584 | const char *name, *old_owner, *new_owner; | |
2585 | Unit *u = userdata; | |
2586 | int r; | |
2587 | ||
2588 | assert(message); | |
2589 | assert(u); | |
2590 | ||
2591 | r = sd_bus_message_read(message, "sss", &name, &old_owner, &new_owner); | |
2592 | if (r < 0) { | |
2593 | bus_log_parse_error(r); | |
2594 | return 0; | |
2595 | } | |
2596 | ||
2597 | if (UNIT_VTABLE(u)->bus_name_owner_change) | |
2598 | UNIT_VTABLE(u)->bus_name_owner_change(u, name, old_owner, new_owner); | |
2599 | ||
2600 | return 0; | |
2601 | } | |
2602 | ||
9806e87d LP |
2603 | int unit_install_bus_match(Unit *u, sd_bus *bus, const char *name) { |
2604 | const char *match; | |
bbc29086 | 2605 | |
9806e87d LP |
2606 | assert(u); |
2607 | assert(bus); | |
2608 | assert(name); | |
bbc29086 DM |
2609 | |
2610 | if (u->match_bus_slot) | |
2611 | return -EBUSY; | |
2612 | ||
9806e87d | 2613 | match = strjoina("type='signal'," |
81d62103 ZJS |
2614 | "sender='org.freedesktop.DBus'," |
2615 | "path='/org/freedesktop/DBus'," | |
2616 | "interface='org.freedesktop.DBus'," | |
2617 | "member='NameOwnerChanged'," | |
2618 | "arg0='", name, "'"); | |
bbc29086 DM |
2619 | |
2620 | return sd_bus_add_match(bus, &u->match_bus_slot, match, signal_name_owner_changed, u); | |
2621 | } | |
2622 | ||
05e343b7 | 2623 | int unit_watch_bus_name(Unit *u, const char *name) { |
bbc29086 DM |
2624 | int r; |
2625 | ||
05e343b7 LP |
2626 | assert(u); |
2627 | assert(name); | |
2628 | ||
2629 | /* Watch a specific name on the bus. We only support one unit | |
2630 | * watching each name for now. */ | |
2631 | ||
bbc29086 DM |
2632 | if (u->manager->api_bus) { |
2633 | /* If the bus is already available, install the match directly. | |
2634 | * Otherwise, just put the name in the list. bus_setup_api() will take care later. */ | |
9806e87d | 2635 | r = unit_install_bus_match(u, u->manager->api_bus, name); |
bbc29086 | 2636 | if (r < 0) |
8ea823b6 | 2637 | return log_warning_errno(r, "Failed to subscribe to NameOwnerChanged signal for '%s': %m", name); |
bbc29086 DM |
2638 | } |
2639 | ||
2640 | r = hashmap_put(u->manager->watch_bus, name, u); | |
2641 | if (r < 0) { | |
2642 | u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot); | |
2643 | return log_warning_errno(r, "Failed to put bus name to hashmap: %m"); | |
2644 | } | |
2645 | ||
2646 | return 0; | |
05e343b7 LP |
2647 | } |
2648 | ||
2649 | void unit_unwatch_bus_name(Unit *u, const char *name) { | |
2650 | assert(u); | |
2651 | assert(name); | |
2652 | ||
ac155bb8 | 2653 | hashmap_remove_value(u->manager->watch_bus, name, u); |
bbc29086 | 2654 | u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot); |
05e343b7 LP |
2655 | } |
2656 | ||
a16e1123 LP |
2657 | bool unit_can_serialize(Unit *u) { |
2658 | assert(u); | |
2659 | ||
2660 | return UNIT_VTABLE(u)->serialize && UNIT_VTABLE(u)->deserialize_item; | |
2661 | } | |
2662 | ||
6b78f9b4 | 2663 | int unit_serialize(Unit *u, FILE *f, FDSet *fds, bool serialize_jobs) { |
a16e1123 LP |
2664 | int r; |
2665 | ||
2666 | assert(u); | |
2667 | assert(f); | |
2668 | assert(fds); | |
2669 | ||
9bdb98c5 LP |
2670 | if (unit_can_serialize(u)) { |
2671 | ExecRuntime *rt; | |
a16e1123 | 2672 | |
9bdb98c5 | 2673 | r = UNIT_VTABLE(u)->serialize(u, f, fds); |
613b411c LP |
2674 | if (r < 0) |
2675 | return r; | |
9bdb98c5 LP |
2676 | |
2677 | rt = unit_get_exec_runtime(u); | |
2678 | if (rt) { | |
f2341e0a | 2679 | r = exec_runtime_serialize(u, rt, f, fds); |
9bdb98c5 LP |
2680 | if (r < 0) |
2681 | return r; | |
2682 | } | |
e0209d83 MS |
2683 | } |
2684 | ||
a483fb59 LP |
2685 | dual_timestamp_serialize(f, "state-change-timestamp", &u->state_change_timestamp); |
2686 | ||
ac155bb8 MS |
2687 | dual_timestamp_serialize(f, "inactive-exit-timestamp", &u->inactive_exit_timestamp); |
2688 | dual_timestamp_serialize(f, "active-enter-timestamp", &u->active_enter_timestamp); | |
2689 | dual_timestamp_serialize(f, "active-exit-timestamp", &u->active_exit_timestamp); | |
2690 | dual_timestamp_serialize(f, "inactive-enter-timestamp", &u->inactive_enter_timestamp); | |
a483fb59 | 2691 | |
ac155bb8 | 2692 | dual_timestamp_serialize(f, "condition-timestamp", &u->condition_timestamp); |
59fccdc5 | 2693 | dual_timestamp_serialize(f, "assert-timestamp", &u->assert_timestamp); |
2791a8f8 | 2694 | |
ac155bb8 MS |
2695 | if (dual_timestamp_is_set(&u->condition_timestamp)) |
2696 | unit_serialize_item(u, f, "condition-result", yes_no(u->condition_result)); | |
10717a1a | 2697 | |
59fccdc5 LP |
2698 | if (dual_timestamp_is_set(&u->assert_timestamp)) |
2699 | unit_serialize_item(u, f, "assert-result", yes_no(u->assert_result)); | |
2700 | ||
c2756a68 | 2701 | unit_serialize_item(u, f, "transient", yes_no(u->transient)); |
fe700f46 | 2702 | |
66ebf6c0 | 2703 | unit_serialize_item_format(u, f, "cpu-usage-base", "%" PRIu64, u->cpu_usage_base); |
fe700f46 LP |
2704 | if (u->cpu_usage_last != NSEC_INFINITY) |
2705 | unit_serialize_item_format(u, f, "cpu-usage-last", "%" PRIu64, u->cpu_usage_last); | |
c2756a68 LP |
2706 | |
2707 | if (u->cgroup_path) | |
2708 | unit_serialize_item(u, f, "cgroup", u->cgroup_path); | |
de1d4f9b | 2709 | unit_serialize_item(u, f, "cgroup-realized", yes_no(u->cgroup_realized)); |
c2756a68 | 2710 | |
00d9ef85 LP |
2711 | if (uid_is_valid(u->ref_uid)) |
2712 | unit_serialize_item_format(u, f, "ref-uid", UID_FMT, u->ref_uid); | |
2713 | if (gid_is_valid(u->ref_gid)) | |
2714 | unit_serialize_item_format(u, f, "ref-gid", GID_FMT, u->ref_gid); | |
2715 | ||
4b58153d LP |
2716 | if (!sd_id128_is_null(u->invocation_id)) |
2717 | unit_serialize_item_format(u, f, "invocation-id", SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(u->invocation_id)); | |
2718 | ||
05a98afd LP |
2719 | bus_track_serialize(u->bus_track, f, "ref"); |
2720 | ||
613b411c LP |
2721 | if (serialize_jobs) { |
2722 | if (u->job) { | |
2723 | fprintf(f, "job\n"); | |
05a98afd | 2724 | job_serialize(u->job, f); |
613b411c LP |
2725 | } |
2726 | ||
2727 | if (u->nop_job) { | |
2728 | fprintf(f, "job\n"); | |
05a98afd | 2729 | job_serialize(u->nop_job, f); |
613b411c LP |
2730 | } |
2731 | } | |
2732 | ||
a16e1123 LP |
2733 | /* End marker */ |
2734 | fputc('\n', f); | |
2735 | return 0; | |
2736 | } | |
2737 | ||
a34ceba6 LP |
2738 | int unit_serialize_item(Unit *u, FILE *f, const char *key, const char *value) { |
2739 | assert(u); | |
2740 | assert(f); | |
2741 | assert(key); | |
2742 | ||
2743 | if (!value) | |
2744 | return 0; | |
2745 | ||
2746 | fputs(key, f); | |
2747 | fputc('=', f); | |
2748 | fputs(value, f); | |
2749 | fputc('\n', f); | |
2750 | ||
2751 | return 1; | |
2752 | } | |
2753 | ||
2754 | int unit_serialize_item_escaped(Unit *u, FILE *f, const char *key, const char *value) { | |
2755 | _cleanup_free_ char *c = NULL; | |
2756 | ||
2757 | assert(u); | |
2758 | assert(f); | |
2759 | assert(key); | |
2760 | ||
2761 | if (!value) | |
2762 | return 0; | |
2763 | ||
2764 | c = cescape(value); | |
2765 | if (!c) | |
2766 | return -ENOMEM; | |
2767 | ||
2768 | fputs(key, f); | |
2769 | fputc('=', f); | |
2770 | fputs(c, f); | |
2771 | fputc('\n', f); | |
2772 | ||
2773 | return 1; | |
2774 | } | |
2775 | ||
2776 | int unit_serialize_item_fd(Unit *u, FILE *f, FDSet *fds, const char *key, int fd) { | |
2777 | int copy; | |
2778 | ||
2779 | assert(u); | |
2780 | assert(f); | |
2781 | assert(key); | |
2782 | ||
2783 | if (fd < 0) | |
2784 | return 0; | |
2785 | ||
2786 | copy = fdset_put_dup(fds, fd); | |
2787 | if (copy < 0) | |
2788 | return copy; | |
2789 | ||
2790 | fprintf(f, "%s=%i\n", key, copy); | |
2791 | return 1; | |
2792 | } | |
2793 | ||
a16e1123 LP |
2794 | void unit_serialize_item_format(Unit *u, FILE *f, const char *key, const char *format, ...) { |
2795 | va_list ap; | |
2796 | ||
2797 | assert(u); | |
2798 | assert(f); | |
2799 | assert(key); | |
2800 | assert(format); | |
2801 | ||
2802 | fputs(key, f); | |
2803 | fputc('=', f); | |
2804 | ||
2805 | va_start(ap, format); | |
2806 | vfprintf(f, format, ap); | |
2807 | va_end(ap); | |
2808 | ||
2809 | fputc('\n', f); | |
2810 | } | |
2811 | ||
a16e1123 | 2812 | int unit_deserialize(Unit *u, FILE *f, FDSet *fds) { |
28b99ccd | 2813 | ExecRuntime **rt = NULL; |
9bdb98c5 | 2814 | size_t offset; |
a16e1123 LP |
2815 | int r; |
2816 | ||
2817 | assert(u); | |
2818 | assert(f); | |
2819 | assert(fds); | |
2820 | ||
613b411c LP |
2821 | offset = UNIT_VTABLE(u)->exec_runtime_offset; |
2822 | if (offset > 0) | |
2823 | rt = (ExecRuntime**) ((uint8_t*) u + offset); | |
2824 | ||
a16e1123 | 2825 | for (;;) { |
20c03b7b | 2826 | char line[LINE_MAX], *l, *v; |
a16e1123 LP |
2827 | size_t k; |
2828 | ||
2829 | if (!fgets(line, sizeof(line), f)) { | |
2830 | if (feof(f)) | |
2831 | return 0; | |
2832 | return -errno; | |
2833 | } | |
2834 | ||
10f8e83c | 2835 | char_array_0(line); |
a16e1123 LP |
2836 | l = strstrip(line); |
2837 | ||
2838 | /* End marker */ | |
e911de99 | 2839 | if (isempty(l)) |
a483fb59 | 2840 | break; |
a16e1123 LP |
2841 | |
2842 | k = strcspn(l, "="); | |
2843 | ||
2844 | if (l[k] == '=') { | |
2845 | l[k] = 0; | |
2846 | v = l+k+1; | |
2847 | } else | |
2848 | v = l+k; | |
2849 | ||
cca098b0 | 2850 | if (streq(l, "job")) { |
39a18c60 MS |
2851 | if (v[0] == '\0') { |
2852 | /* new-style serialized job */ | |
9c3349e2 LP |
2853 | Job *j; |
2854 | ||
2855 | j = job_new_raw(u); | |
39a18c60 | 2856 | if (!j) |
e911de99 | 2857 | return log_oom(); |
39a18c60 | 2858 | |
05a98afd | 2859 | r = job_deserialize(j, f); |
39a18c60 MS |
2860 | if (r < 0) { |
2861 | job_free(j); | |
2862 | return r; | |
2863 | } | |
cca098b0 | 2864 | |
39a18c60 MS |
2865 | r = hashmap_put(u->manager->jobs, UINT32_TO_PTR(j->id), j); |
2866 | if (r < 0) { | |
2867 | job_free(j); | |
2868 | return r; | |
2869 | } | |
e0209d83 MS |
2870 | |
2871 | r = job_install_deserialized(j); | |
2872 | if (r < 0) { | |
2873 | hashmap_remove(u->manager->jobs, UINT32_TO_PTR(j->id)); | |
2874 | job_free(j); | |
2875 | return r; | |
2876 | } | |
ed10fa8c LP |
2877 | } else /* legacy for pre-44 */ |
2878 | log_unit_warning(u, "Update from too old systemd versions are unsupported, cannot deserialize job: %s", v); | |
cca098b0 | 2879 | continue; |
a483fb59 LP |
2880 | } else if (streq(l, "state-change-timestamp")) { |
2881 | dual_timestamp_deserialize(v, &u->state_change_timestamp); | |
2882 | continue; | |
8aaf019b | 2883 | } else if (streq(l, "inactive-exit-timestamp")) { |
ac155bb8 | 2884 | dual_timestamp_deserialize(v, &u->inactive_exit_timestamp); |
8aaf019b LP |
2885 | continue; |
2886 | } else if (streq(l, "active-enter-timestamp")) { | |
ac155bb8 | 2887 | dual_timestamp_deserialize(v, &u->active_enter_timestamp); |
8aaf019b LP |
2888 | continue; |
2889 | } else if (streq(l, "active-exit-timestamp")) { | |
ac155bb8 | 2890 | dual_timestamp_deserialize(v, &u->active_exit_timestamp); |
8aaf019b LP |
2891 | continue; |
2892 | } else if (streq(l, "inactive-enter-timestamp")) { | |
ac155bb8 | 2893 | dual_timestamp_deserialize(v, &u->inactive_enter_timestamp); |
8aaf019b | 2894 | continue; |
2791a8f8 | 2895 | } else if (streq(l, "condition-timestamp")) { |
ac155bb8 | 2896 | dual_timestamp_deserialize(v, &u->condition_timestamp); |
2791a8f8 | 2897 | continue; |
59fccdc5 LP |
2898 | } else if (streq(l, "assert-timestamp")) { |
2899 | dual_timestamp_deserialize(v, &u->assert_timestamp); | |
2900 | continue; | |
2791a8f8 | 2901 | } else if (streq(l, "condition-result")) { |
2791a8f8 | 2902 | |
e911de99 LP |
2903 | r = parse_boolean(v); |
2904 | if (r < 0) | |
f2341e0a | 2905 | log_unit_debug(u, "Failed to parse condition result value %s, ignoring.", v); |
2791a8f8 | 2906 | else |
e911de99 | 2907 | u->condition_result = r; |
efbac6d2 LP |
2908 | |
2909 | continue; | |
c2756a68 | 2910 | |
59fccdc5 | 2911 | } else if (streq(l, "assert-result")) { |
59fccdc5 | 2912 | |
e911de99 LP |
2913 | r = parse_boolean(v); |
2914 | if (r < 0) | |
f2341e0a | 2915 | log_unit_debug(u, "Failed to parse assert result value %s, ignoring.", v); |
59fccdc5 | 2916 | else |
e911de99 | 2917 | u->assert_result = r; |
59fccdc5 LP |
2918 | |
2919 | continue; | |
2920 | ||
c2756a68 | 2921 | } else if (streq(l, "transient")) { |
c2756a68 | 2922 | |
e911de99 LP |
2923 | r = parse_boolean(v); |
2924 | if (r < 0) | |
f2341e0a | 2925 | log_unit_debug(u, "Failed to parse transient bool %s, ignoring.", v); |
c2756a68 | 2926 | else |
e911de99 | 2927 | u->transient = r; |
c2756a68 LP |
2928 | |
2929 | continue; | |
e911de99 | 2930 | |
fe700f46 | 2931 | } else if (STR_IN_SET(l, "cpu-usage-base", "cpuacct-usage-base")) { |
5ad096b3 | 2932 | |
66ebf6c0 | 2933 | r = safe_atou64(v, &u->cpu_usage_base); |
5ad096b3 | 2934 | if (r < 0) |
fe700f46 LP |
2935 | log_unit_debug(u, "Failed to parse CPU usage base %s, ignoring.", v); |
2936 | ||
2937 | continue; | |
2938 | ||
2939 | } else if (streq(l, "cpu-usage-last")) { | |
2940 | ||
2941 | r = safe_atou64(v, &u->cpu_usage_last); | |
2942 | if (r < 0) | |
2943 | log_unit_debug(u, "Failed to read CPU usage last %s, ignoring.", v); | |
5ad096b3 | 2944 | |
0f908397 | 2945 | continue; |
4e595329 | 2946 | |
e911de99 | 2947 | } else if (streq(l, "cgroup")) { |
72673e86 | 2948 | |
e911de99 LP |
2949 | r = unit_set_cgroup_path(u, v); |
2950 | if (r < 0) | |
f2341e0a | 2951 | log_unit_debug_errno(u, r, "Failed to set cgroup path %s, ignoring: %m", v); |
4e595329 | 2952 | |
efdb0237 LP |
2953 | (void) unit_watch_cgroup(u); |
2954 | ||
de1d4f9b WF |
2955 | continue; |
2956 | } else if (streq(l, "cgroup-realized")) { | |
2957 | int b; | |
2958 | ||
2959 | b = parse_boolean(v); | |
2960 | if (b < 0) | |
2961 | log_unit_debug(u, "Failed to parse cgroup-realized bool %s, ignoring.", v); | |
2962 | else | |
2963 | u->cgroup_realized = b; | |
2964 | ||
c2756a68 | 2965 | continue; |
00d9ef85 LP |
2966 | |
2967 | } else if (streq(l, "ref-uid")) { | |
2968 | uid_t uid; | |
2969 | ||
2970 | r = parse_uid(v, &uid); | |
2971 | if (r < 0) | |
2972 | log_unit_debug(u, "Failed to parse referenced UID %s, ignoring.", v); | |
2973 | else | |
2974 | unit_ref_uid_gid(u, uid, GID_INVALID); | |
2975 | ||
2976 | continue; | |
2977 | ||
2978 | } else if (streq(l, "ref-gid")) { | |
2979 | gid_t gid; | |
2980 | ||
2981 | r = parse_gid(v, &gid); | |
2982 | if (r < 0) | |
2983 | log_unit_debug(u, "Failed to parse referenced GID %s, ignoring.", v); | |
2984 | else | |
2985 | unit_ref_uid_gid(u, UID_INVALID, gid); | |
2986 | ||
05a98afd LP |
2987 | } else if (streq(l, "ref")) { |
2988 | ||
2989 | r = strv_extend(&u->deserialized_refs, v); | |
2990 | if (r < 0) | |
2991 | log_oom(); | |
2992 | ||
4b58153d LP |
2993 | continue; |
2994 | } else if (streq(l, "invocation-id")) { | |
2995 | sd_id128_t id; | |
2996 | ||
2997 | r = sd_id128_from_string(v, &id); | |
2998 | if (r < 0) | |
2999 | log_unit_debug(u, "Failed to parse invocation id %s, ignoring.", v); | |
3000 | else { | |
3001 | r = unit_set_invocation_id(u, id); | |
3002 | if (r < 0) | |
3003 | log_unit_warning_errno(u, r, "Failed to set invocation ID for unit: %m"); | |
3004 | } | |
3005 | ||
00d9ef85 | 3006 | continue; |
8aaf019b | 3007 | } |
cca098b0 | 3008 | |
9bdb98c5 LP |
3009 | if (unit_can_serialize(u)) { |
3010 | if (rt) { | |
f2341e0a | 3011 | r = exec_runtime_deserialize_item(u, rt, l, v, fds); |
e911de99 | 3012 | if (r < 0) { |
f2341e0a | 3013 | log_unit_warning(u, "Failed to deserialize runtime parameter '%s', ignoring.", l); |
e911de99 LP |
3014 | continue; |
3015 | } | |
3016 | ||
3017 | /* Returns positive if key was handled by the call */ | |
9bdb98c5 LP |
3018 | if (r > 0) |
3019 | continue; | |
3020 | } | |
3021 | ||
3022 | r = UNIT_VTABLE(u)->deserialize_item(u, l, v, fds); | |
613b411c | 3023 | if (r < 0) |
f2341e0a | 3024 | log_unit_warning(u, "Failed to deserialize unit parameter '%s', ignoring.", l); |
613b411c | 3025 | } |
a16e1123 | 3026 | } |
a483fb59 LP |
3027 | |
3028 | /* Versions before 228 did not carry a state change timestamp. In this case, take the current time. This is | |
3029 | * useful, so that timeouts based on this timestamp don't trigger too early, and is in-line with the logic from | |
1f133e0d | 3030 | * before 228 where the base for timeouts was not persistent across reboots. */ |
a483fb59 LP |
3031 | |
3032 | if (!dual_timestamp_is_set(&u->state_change_timestamp)) | |
3033 | dual_timestamp_get(&u->state_change_timestamp); | |
3034 | ||
3035 | return 0; | |
a16e1123 LP |
3036 | } |
3037 | ||
9d06297e | 3038 | int unit_add_node_link(Unit *u, const char *what, bool wants, UnitDependency dep) { |
6e2ef85b | 3039 | Unit *device; |
68eda4bd | 3040 | _cleanup_free_ char *e = NULL; |
6e2ef85b LP |
3041 | int r; |
3042 | ||
3043 | assert(u); | |
3044 | ||
6e2ef85b | 3045 | /* Adds in links to the device node that this unit is based on */ |
47bc12e1 LP |
3046 | if (isempty(what)) |
3047 | return 0; | |
6e2ef85b | 3048 | |
8407a5d0 | 3049 | if (!is_device_path(what)) |
6e2ef85b LP |
3050 | return 0; |
3051 | ||
47bc12e1 LP |
3052 | /* When device units aren't supported (such as in a |
3053 | * container), don't create dependencies on them. */ | |
1c2e9646 | 3054 | if (!unit_type_supported(UNIT_DEVICE)) |
47bc12e1 LP |
3055 | return 0; |
3056 | ||
7410616c LP |
3057 | r = unit_name_from_path(what, ".device", &e); |
3058 | if (r < 0) | |
3059 | return r; | |
6e2ef85b | 3060 | |
ac155bb8 | 3061 | r = manager_load_unit(u->manager, e, NULL, NULL, &device); |
6e2ef85b LP |
3062 | if (r < 0) |
3063 | return r; | |
3064 | ||
ebc8968b FB |
3065 | if (dep == UNIT_REQUIRES && device_shall_be_bound_by(device, u)) |
3066 | dep = UNIT_BINDS_TO; | |
3067 | ||
9d06297e | 3068 | r = unit_add_two_dependencies(u, UNIT_AFTER, |
463d0d15 | 3069 | MANAGER_IS_SYSTEM(u->manager) ? dep : UNIT_WANTS, |
9d06297e | 3070 | device, true); |
faa368e3 | 3071 | if (r < 0) |
6e2ef85b LP |
3072 | return r; |
3073 | ||
faa368e3 LP |
3074 | if (wants) { |
3075 | r = unit_add_dependency(device, UNIT_WANTS, u, false); | |
3076 | if (r < 0) | |
6e2ef85b | 3077 | return r; |
faa368e3 | 3078 | } |
6e2ef85b LP |
3079 | |
3080 | return 0; | |
3081 | } | |
a16e1123 | 3082 | |
be847e82 | 3083 | int unit_coldplug(Unit *u) { |
05a98afd LP |
3084 | int r = 0, q; |
3085 | char **i; | |
cca098b0 LP |
3086 | |
3087 | assert(u); | |
3088 | ||
f78f265f LP |
3089 | /* Make sure we don't enter a loop, when coldplugging |
3090 | * recursively. */ | |
3091 | if (u->coldplugged) | |
3092 | return 0; | |
3093 | ||
3094 | u->coldplugged = true; | |
3095 | ||
05a98afd LP |
3096 | STRV_FOREACH(i, u->deserialized_refs) { |
3097 | q = bus_unit_track_add_name(u, *i); | |
3098 | if (q < 0 && r >= 0) | |
3099 | r = q; | |
3100 | } | |
3101 | u->deserialized_refs = strv_free(u->deserialized_refs); | |
cca098b0 | 3102 | |
05a98afd LP |
3103 | if (UNIT_VTABLE(u)->coldplug) { |
3104 | q = UNIT_VTABLE(u)->coldplug(u); | |
3105 | if (q < 0 && r >= 0) | |
3106 | r = q; | |
3107 | } | |
5a6158b6 | 3108 | |
05a98afd LP |
3109 | if (u->job) { |
3110 | q = job_coldplug(u->job); | |
3111 | if (q < 0 && r >= 0) | |
3112 | r = q; | |
3113 | } | |
cca098b0 | 3114 | |
05a98afd | 3115 | return r; |
cca098b0 LP |
3116 | } |
3117 | ||
ba25d39e | 3118 | static bool fragment_mtime_newer(const char *path, usec_t mtime, bool path_masked) { |
21b95806 ZJS |
3119 | struct stat st; |
3120 | ||
3121 | if (!path) | |
3122 | return false; | |
3123 | ||
3124 | if (stat(path, &st) < 0) | |
3125 | /* What, cannot access this anymore? */ | |
3126 | return true; | |
3127 | ||
ba25d39e ZJS |
3128 | if (path_masked) |
3129 | /* For masked files check if they are still so */ | |
3130 | return !null_or_empty(&st); | |
3131 | else | |
3a8db9fe | 3132 | /* For non-empty files check the mtime */ |
87ec20ef | 3133 | return timespec_load(&st.st_mtim) > mtime; |
21b95806 ZJS |
3134 | |
3135 | return false; | |
3136 | } | |
3137 | ||
45fb0699 | 3138 | bool unit_need_daemon_reload(Unit *u) { |
ae7a7182 OS |
3139 | _cleanup_strv_free_ char **t = NULL; |
3140 | char **path; | |
1b64d026 | 3141 | |
45fb0699 LP |
3142 | assert(u); |
3143 | ||
ba25d39e ZJS |
3144 | /* For unit files, we allow masking… */ |
3145 | if (fragment_mtime_newer(u->fragment_path, u->fragment_mtime, | |
3146 | u->load_state == UNIT_MASKED)) | |
21b95806 | 3147 | return true; |
5f4b19f4 | 3148 | |
ba25d39e ZJS |
3149 | /* Source paths should not be masked… */ |
3150 | if (fragment_mtime_newer(u->source_path, u->source_mtime, false)) | |
ab932a62 | 3151 | return true; |
ae7a7182 | 3152 | |
ab932a62 LP |
3153 | (void) unit_find_dropin_paths(u, &t); |
3154 | if (!strv_equal(u->dropin_paths, t)) | |
3155 | return true; | |
6d10d308 | 3156 | |
ba25d39e | 3157 | /* … any drop-ins that are masked are simply omitted from the list. */ |
ab932a62 | 3158 | STRV_FOREACH(path, u->dropin_paths) |
ba25d39e | 3159 | if (fragment_mtime_newer(*path, u->dropin_mtime, false)) |
ab932a62 | 3160 | return true; |
21b95806 | 3161 | |
ab932a62 | 3162 | return false; |
45fb0699 LP |
3163 | } |
3164 | ||
fdf20a31 | 3165 | void unit_reset_failed(Unit *u) { |
5632e374 LP |
3166 | assert(u); |
3167 | ||
fdf20a31 MM |
3168 | if (UNIT_VTABLE(u)->reset_failed) |
3169 | UNIT_VTABLE(u)->reset_failed(u); | |
6bf0f408 LP |
3170 | |
3171 | RATELIMIT_RESET(u->start_limit); | |
3172 | u->start_limit_hit = false; | |
5632e374 LP |
3173 | } |
3174 | ||
a7f241db LP |
3175 | Unit *unit_following(Unit *u) { |
3176 | assert(u); | |
3177 | ||
3178 | if (UNIT_VTABLE(u)->following) | |
3179 | return UNIT_VTABLE(u)->following(u); | |
3180 | ||
3181 | return NULL; | |
3182 | } | |
3183 | ||
31afa0a4 | 3184 | bool unit_stop_pending(Unit *u) { |
18ffdfda LP |
3185 | assert(u); |
3186 | ||
31afa0a4 LP |
3187 | /* This call does check the current state of the unit. It's |
3188 | * hence useful to be called from state change calls of the | |
3189 | * unit itself, where the state isn't updated yet. This is | |
3190 | * different from unit_inactive_or_pending() which checks both | |
3191 | * the current state and for a queued job. */ | |
18ffdfda | 3192 | |
31afa0a4 LP |
3193 | return u->job && u->job->type == JOB_STOP; |
3194 | } | |
3195 | ||
3196 | bool unit_inactive_or_pending(Unit *u) { | |
3197 | assert(u); | |
3198 | ||
3199 | /* Returns true if the unit is inactive or going down */ | |
18ffdfda | 3200 | |
d956ac29 LP |
3201 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))) |
3202 | return true; | |
3203 | ||
31afa0a4 | 3204 | if (unit_stop_pending(u)) |
18ffdfda LP |
3205 | return true; |
3206 | ||
3207 | return false; | |
3208 | } | |
3209 | ||
31afa0a4 | 3210 | bool unit_active_or_pending(Unit *u) { |
f976f3f6 LP |
3211 | assert(u); |
3212 | ||
f60c2665 | 3213 | /* Returns true if the unit is active or going up */ |
f976f3f6 LP |
3214 | |
3215 | if (UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) | |
3216 | return true; | |
3217 | ||
ac155bb8 MS |
3218 | if (u->job && |
3219 | (u->job->type == JOB_START || | |
3220 | u->job->type == JOB_RELOAD_OR_START || | |
3221 | u->job->type == JOB_RESTART)) | |
f976f3f6 LP |
3222 | return true; |
3223 | ||
3224 | return false; | |
3225 | } | |
3226 | ||
718db961 | 3227 | int unit_kill(Unit *u, KillWho w, int signo, sd_bus_error *error) { |
8a0867d6 LP |
3228 | assert(u); |
3229 | assert(w >= 0 && w < _KILL_WHO_MAX); | |
6eb7c172 | 3230 | assert(SIGNAL_VALID(signo)); |
8a0867d6 | 3231 | |
8a0867d6 | 3232 | if (!UNIT_VTABLE(u)->kill) |
15411c0c | 3233 | return -EOPNOTSUPP; |
8a0867d6 | 3234 | |
c74f17d9 | 3235 | return UNIT_VTABLE(u)->kill(u, w, signo, error); |
8a0867d6 LP |
3236 | } |
3237 | ||
82659fd7 LP |
3238 | static Set *unit_pid_set(pid_t main_pid, pid_t control_pid) { |
3239 | Set *pid_set; | |
3240 | int r; | |
3241 | ||
d5099efc | 3242 | pid_set = set_new(NULL); |
82659fd7 LP |
3243 | if (!pid_set) |
3244 | return NULL; | |
3245 | ||
3246 | /* Exclude the main/control pids from being killed via the cgroup */ | |
3247 | if (main_pid > 0) { | |
fea72cc0 | 3248 | r = set_put(pid_set, PID_TO_PTR(main_pid)); |
82659fd7 LP |
3249 | if (r < 0) |
3250 | goto fail; | |
3251 | } | |
3252 | ||
3253 | if (control_pid > 0) { | |
fea72cc0 | 3254 | r = set_put(pid_set, PID_TO_PTR(control_pid)); |
82659fd7 LP |
3255 | if (r < 0) |
3256 | goto fail; | |
3257 | } | |
3258 | ||
3259 | return pid_set; | |
3260 | ||
3261 | fail: | |
3262 | set_free(pid_set); | |
3263 | return NULL; | |
3264 | } | |
3265 | ||
d91c34f2 LP |
3266 | int unit_kill_common( |
3267 | Unit *u, | |
3268 | KillWho who, | |
3269 | int signo, | |
3270 | pid_t main_pid, | |
3271 | pid_t control_pid, | |
718db961 | 3272 | sd_bus_error *error) { |
d91c34f2 | 3273 | |
814cc562 | 3274 | int r = 0; |
ac5e3a50 | 3275 | bool killed = false; |
814cc562 | 3276 | |
ac5e3a50 | 3277 | if (IN_SET(who, KILL_MAIN, KILL_MAIN_FAIL)) { |
814cc562 | 3278 | if (main_pid < 0) |
7358dc02 | 3279 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no main processes", unit_type_to_string(u->type)); |
52f448c3 | 3280 | else if (main_pid == 0) |
7358dc02 | 3281 | return sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill"); |
814cc562 MS |
3282 | } |
3283 | ||
ac5e3a50 | 3284 | if (IN_SET(who, KILL_CONTROL, KILL_CONTROL_FAIL)) { |
814cc562 | 3285 | if (control_pid < 0) |
7358dc02 | 3286 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no control processes", unit_type_to_string(u->type)); |
52f448c3 | 3287 | else if (control_pid == 0) |
7358dc02 | 3288 | return sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill"); |
814cc562 MS |
3289 | } |
3290 | ||
ac5e3a50 JS |
3291 | if (IN_SET(who, KILL_CONTROL, KILL_CONTROL_FAIL, KILL_ALL, KILL_ALL_FAIL)) |
3292 | if (control_pid > 0) { | |
814cc562 MS |
3293 | if (kill(control_pid, signo) < 0) |
3294 | r = -errno; | |
ac5e3a50 JS |
3295 | else |
3296 | killed = true; | |
3297 | } | |
814cc562 | 3298 | |
ac5e3a50 JS |
3299 | if (IN_SET(who, KILL_MAIN, KILL_MAIN_FAIL, KILL_ALL, KILL_ALL_FAIL)) |
3300 | if (main_pid > 0) { | |
814cc562 MS |
3301 | if (kill(main_pid, signo) < 0) |
3302 | r = -errno; | |
ac5e3a50 JS |
3303 | else |
3304 | killed = true; | |
3305 | } | |
814cc562 | 3306 | |
ac5e3a50 | 3307 | if (IN_SET(who, KILL_ALL, KILL_ALL_FAIL) && u->cgroup_path) { |
814cc562 MS |
3308 | _cleanup_set_free_ Set *pid_set = NULL; |
3309 | int q; | |
3310 | ||
82659fd7 LP |
3311 | /* Exclude the main/control pids from being killed via the cgroup */ |
3312 | pid_set = unit_pid_set(main_pid, control_pid); | |
814cc562 MS |
3313 | if (!pid_set) |
3314 | return -ENOMEM; | |
3315 | ||
1d98fef1 | 3316 | q = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, signo, 0, pid_set, NULL, NULL); |
814cc562 MS |
3317 | if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT) |
3318 | r = q; | |
ac5e3a50 JS |
3319 | else |
3320 | killed = true; | |
814cc562 MS |
3321 | } |
3322 | ||
201f0c91 | 3323 | if (r == 0 && !killed && IN_SET(who, KILL_ALL_FAIL, KILL_CONTROL_FAIL)) |
ac5e3a50 JS |
3324 | return -ESRCH; |
3325 | ||
814cc562 MS |
3326 | return r; |
3327 | } | |
3328 | ||
6210e7fc LP |
3329 | int unit_following_set(Unit *u, Set **s) { |
3330 | assert(u); | |
3331 | assert(s); | |
3332 | ||
3333 | if (UNIT_VTABLE(u)->following_set) | |
3334 | return UNIT_VTABLE(u)->following_set(u, s); | |
3335 | ||
3336 | *s = NULL; | |
3337 | return 0; | |
3338 | } | |
3339 | ||
a4375746 | 3340 | UnitFileState unit_get_unit_file_state(Unit *u) { |
0ec0deaa LP |
3341 | int r; |
3342 | ||
a4375746 LP |
3343 | assert(u); |
3344 | ||
0ec0deaa LP |
3345 | if (u->unit_file_state < 0 && u->fragment_path) { |
3346 | r = unit_file_get_state( | |
463d0d15 | 3347 | u->manager->unit_file_scope, |
0ec0deaa LP |
3348 | NULL, |
3349 | basename(u->fragment_path), | |
3350 | &u->unit_file_state); | |
3351 | if (r < 0) | |
3352 | u->unit_file_state = UNIT_FILE_BAD; | |
3353 | } | |
a4375746 | 3354 | |
ac155bb8 | 3355 | return u->unit_file_state; |
a4375746 LP |
3356 | } |
3357 | ||
d2dc52db LP |
3358 | int unit_get_unit_file_preset(Unit *u) { |
3359 | assert(u); | |
3360 | ||
3361 | if (u->unit_file_preset < 0 && u->fragment_path) | |
3362 | u->unit_file_preset = unit_file_query_preset( | |
463d0d15 | 3363 | u->manager->unit_file_scope, |
0ec0deaa LP |
3364 | NULL, |
3365 | basename(u->fragment_path)); | |
d2dc52db LP |
3366 | |
3367 | return u->unit_file_preset; | |
3368 | } | |
3369 | ||
57020a3a LP |
3370 | Unit* unit_ref_set(UnitRef *ref, Unit *u) { |
3371 | assert(ref); | |
3372 | assert(u); | |
3373 | ||
3374 | if (ref->unit) | |
3375 | unit_ref_unset(ref); | |
3376 | ||
3377 | ref->unit = u; | |
71fda00f | 3378 | LIST_PREPEND(refs, u->refs, ref); |
57020a3a LP |
3379 | return u; |
3380 | } | |
3381 | ||
3382 | void unit_ref_unset(UnitRef *ref) { | |
3383 | assert(ref); | |
3384 | ||
3385 | if (!ref->unit) | |
3386 | return; | |
3387 | ||
b75102e5 LP |
3388 | /* We are about to drop a reference to the unit, make sure the garbage collection has a look at it as it might |
3389 | * be unreferenced now. */ | |
3390 | unit_add_to_gc_queue(ref->unit); | |
3391 | ||
71fda00f | 3392 | LIST_REMOVE(refs, ref->unit->refs, ref); |
57020a3a LP |
3393 | ref->unit = NULL; |
3394 | } | |
3395 | ||
29206d46 LP |
3396 | static int user_from_unit_name(Unit *u, char **ret) { |
3397 | ||
3398 | static const uint8_t hash_key[] = { | |
3399 | 0x58, 0x1a, 0xaf, 0xe6, 0x28, 0x58, 0x4e, 0x96, | |
3400 | 0xb4, 0x4e, 0xf5, 0x3b, 0x8c, 0x92, 0x07, 0xec | |
3401 | }; | |
3402 | ||
3403 | _cleanup_free_ char *n = NULL; | |
3404 | int r; | |
3405 | ||
3406 | r = unit_name_to_prefix(u->id, &n); | |
3407 | if (r < 0) | |
3408 | return r; | |
3409 | ||
3410 | if (valid_user_group_name(n)) { | |
3411 | *ret = n; | |
3412 | n = NULL; | |
3413 | return 0; | |
3414 | } | |
3415 | ||
3416 | /* If we can't use the unit name as a user name, then let's hash it and use that */ | |
3417 | if (asprintf(ret, "_du%016" PRIx64, siphash24(n, strlen(n), hash_key)) < 0) | |
3418 | return -ENOMEM; | |
3419 | ||
3420 | return 0; | |
3421 | } | |
3422 | ||
598459ce LP |
3423 | int unit_patch_contexts(Unit *u) { |
3424 | CGroupContext *cc; | |
3425 | ExecContext *ec; | |
cba6e062 LP |
3426 | unsigned i; |
3427 | int r; | |
3428 | ||
e06c73cc | 3429 | assert(u); |
e06c73cc | 3430 | |
598459ce LP |
3431 | /* Patch in the manager defaults into the exec and cgroup |
3432 | * contexts, _after_ the rest of the settings have been | |
3433 | * initialized */ | |
085afe36 | 3434 | |
598459ce LP |
3435 | ec = unit_get_exec_context(u); |
3436 | if (ec) { | |
3437 | /* This only copies in the ones that need memory */ | |
3438 | for (i = 0; i < _RLIMIT_MAX; i++) | |
3439 | if (u->manager->rlimit[i] && !ec->rlimit[i]) { | |
3440 | ec->rlimit[i] = newdup(struct rlimit, u->manager->rlimit[i], 1); | |
3441 | if (!ec->rlimit[i]) | |
3442 | return -ENOMEM; | |
3443 | } | |
3444 | ||
463d0d15 | 3445 | if (MANAGER_IS_USER(u->manager) && |
598459ce LP |
3446 | !ec->working_directory) { |
3447 | ||
3448 | r = get_home_dir(&ec->working_directory); | |
3449 | if (r < 0) | |
3450 | return r; | |
4c08c824 LP |
3451 | |
3452 | /* Allow user services to run, even if the | |
3453 | * home directory is missing */ | |
3454 | ec->working_directory_missing_ok = true; | |
cba6e062 LP |
3455 | } |
3456 | ||
598459ce | 3457 | if (ec->private_devices) |
2cd0a735 | 3458 | ec->capability_bounding_set &= ~((UINT64_C(1) << CAP_MKNOD) | (UINT64_C(1) << CAP_SYS_RAWIO)); |
502d704e DH |
3459 | |
3460 | if (ec->protect_kernel_modules) | |
3461 | ec->capability_bounding_set &= ~(UINT64_C(1) << CAP_SYS_MODULE); | |
29206d46 LP |
3462 | |
3463 | if (ec->dynamic_user) { | |
3464 | if (!ec->user) { | |
3465 | r = user_from_unit_name(u, &ec->user); | |
3466 | if (r < 0) | |
3467 | return r; | |
3468 | } | |
3469 | ||
3470 | if (!ec->group) { | |
3471 | ec->group = strdup(ec->user); | |
3472 | if (!ec->group) | |
3473 | return -ENOMEM; | |
3474 | } | |
3475 | ||
63bb64a0 LP |
3476 | /* If the dynamic user option is on, let's make sure that the unit can't leave its UID/GID |
3477 | * around in the file system or on IPC objects. Hence enforce a strict sandbox. */ | |
3478 | ||
29206d46 | 3479 | ec->private_tmp = true; |
00d9ef85 | 3480 | ec->remove_ipc = true; |
63bb64a0 LP |
3481 | ec->protect_system = PROTECT_SYSTEM_STRICT; |
3482 | if (ec->protect_home == PROTECT_HOME_NO) | |
3483 | ec->protect_home = PROTECT_HOME_READ_ONLY; | |
29206d46 | 3484 | } |
cba6e062 LP |
3485 | } |
3486 | ||
598459ce LP |
3487 | cc = unit_get_cgroup_context(u); |
3488 | if (cc) { | |
f513e420 | 3489 | |
598459ce LP |
3490 | if (ec && |
3491 | ec->private_devices && | |
3492 | cc->device_policy == CGROUP_AUTO) | |
3493 | cc->device_policy = CGROUP_CLOSED; | |
3494 | } | |
f1660f96 | 3495 | |
cba6e062 | 3496 | return 0; |
e06c73cc LP |
3497 | } |
3498 | ||
3ef63c31 LP |
3499 | ExecContext *unit_get_exec_context(Unit *u) { |
3500 | size_t offset; | |
3501 | assert(u); | |
3502 | ||
598459ce LP |
3503 | if (u->type < 0) |
3504 | return NULL; | |
3505 | ||
3ef63c31 LP |
3506 | offset = UNIT_VTABLE(u)->exec_context_offset; |
3507 | if (offset <= 0) | |
3508 | return NULL; | |
3509 | ||
3510 | return (ExecContext*) ((uint8_t*) u + offset); | |
3511 | } | |
3512 | ||
718db961 LP |
3513 | KillContext *unit_get_kill_context(Unit *u) { |
3514 | size_t offset; | |
3515 | assert(u); | |
3516 | ||
598459ce LP |
3517 | if (u->type < 0) |
3518 | return NULL; | |
3519 | ||
718db961 LP |
3520 | offset = UNIT_VTABLE(u)->kill_context_offset; |
3521 | if (offset <= 0) | |
3522 | return NULL; | |
3523 | ||
3524 | return (KillContext*) ((uint8_t*) u + offset); | |
3525 | } | |
3526 | ||
4ad49000 LP |
3527 | CGroupContext *unit_get_cgroup_context(Unit *u) { |
3528 | size_t offset; | |
3529 | ||
598459ce LP |
3530 | if (u->type < 0) |
3531 | return NULL; | |
3532 | ||
4ad49000 LP |
3533 | offset = UNIT_VTABLE(u)->cgroup_context_offset; |
3534 | if (offset <= 0) | |
3535 | return NULL; | |
3536 | ||
3537 | return (CGroupContext*) ((uint8_t*) u + offset); | |
3538 | } | |
3539 | ||
613b411c LP |
3540 | ExecRuntime *unit_get_exec_runtime(Unit *u) { |
3541 | size_t offset; | |
3542 | ||
598459ce LP |
3543 | if (u->type < 0) |
3544 | return NULL; | |
3545 | ||
613b411c LP |
3546 | offset = UNIT_VTABLE(u)->exec_runtime_offset; |
3547 | if (offset <= 0) | |
3548 | return NULL; | |
3549 | ||
3550 | return *(ExecRuntime**) ((uint8_t*) u + offset); | |
3551 | } | |
3552 | ||
39591351 | 3553 | static const char* unit_drop_in_dir(Unit *u, UnitSetPropertiesMode mode) { |
3f5e8115 LP |
3554 | assert(u); |
3555 | ||
4f4afc88 LP |
3556 | if (!IN_SET(mode, UNIT_RUNTIME, UNIT_PERSISTENT)) |
3557 | return NULL; | |
3558 | ||
39591351 LP |
3559 | if (u->transient) /* Redirect drop-ins for transient units always into the transient directory. */ |
3560 | return u->manager->lookup_paths.transient; | |
26d04f86 | 3561 | |
39591351 | 3562 | if (mode == UNIT_RUNTIME) |
4f4afc88 | 3563 | return u->manager->lookup_paths.runtime_control; |
3f5e8115 | 3564 | |
39591351 | 3565 | if (mode == UNIT_PERSISTENT) |
4f4afc88 | 3566 | return u->manager->lookup_paths.persistent_control; |
26d04f86 | 3567 | |
39591351 | 3568 | return NULL; |
71645aca LP |
3569 | } |
3570 | ||
8e2af478 | 3571 | int unit_write_drop_in(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *data) { |
39591351 | 3572 | _cleanup_free_ char *p = NULL, *q = NULL; |
2a9a6f8a | 3573 | const char *dir, *wrapped; |
26d04f86 | 3574 | int r; |
71645aca LP |
3575 | |
3576 | assert(u); | |
3577 | ||
4f4afc88 LP |
3578 | if (u->transient_file) { |
3579 | /* When this is a transient unit file in creation, then let's not create a new drop-in but instead | |
3580 | * write to the transient unit file. */ | |
3581 | fputs(data, u->transient_file); | |
fc40065b | 3582 | fputc('\n', u->transient_file); |
4f4afc88 LP |
3583 | return 0; |
3584 | } | |
3585 | ||
6d235724 | 3586 | if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) |
8e2af478 LP |
3587 | return 0; |
3588 | ||
39591351 LP |
3589 | dir = unit_drop_in_dir(u, mode); |
3590 | if (!dir) | |
3591 | return -EINVAL; | |
71645aca | 3592 | |
2a9a6f8a | 3593 | wrapped = strjoina("# This is a drop-in unit file extension, created via \"systemctl set-property\"\n" |
3f71dec5 | 3594 | "# or an equivalent operation. Do not edit.\n", |
2a9a6f8a ZJS |
3595 | data, |
3596 | "\n"); | |
e20b2a86 | 3597 | |
815b09d3 | 3598 | r = drop_in_file(dir, u->id, 50, name, &p, &q); |
adb76a70 WC |
3599 | if (r < 0) |
3600 | return r; | |
3601 | ||
815b09d3 | 3602 | (void) mkdir_p(p, 0755); |
2a9a6f8a | 3603 | r = write_string_file_atomic_label(q, wrapped); |
adb76a70 WC |
3604 | if (r < 0) |
3605 | return r; | |
3606 | ||
815b09d3 | 3607 | r = strv_push(&u->dropin_paths, q); |
adb76a70 WC |
3608 | if (r < 0) |
3609 | return r; | |
815b09d3 | 3610 | q = NULL; |
adb76a70 | 3611 | |
adb76a70 WC |
3612 | strv_uniq(u->dropin_paths); |
3613 | ||
3614 | u->dropin_mtime = now(CLOCK_REALTIME); | |
3615 | ||
3616 | return 0; | |
26d04f86 | 3617 | } |
71645aca | 3618 | |
b9ec9359 LP |
3619 | int unit_write_drop_in_format(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *format, ...) { |
3620 | _cleanup_free_ char *p = NULL; | |
3621 | va_list ap; | |
3622 | int r; | |
3623 | ||
3624 | assert(u); | |
3625 | assert(name); | |
3626 | assert(format); | |
3627 | ||
6d235724 | 3628 | if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) |
b9ec9359 LP |
3629 | return 0; |
3630 | ||
3631 | va_start(ap, format); | |
3632 | r = vasprintf(&p, format, ap); | |
3633 | va_end(ap); | |
3634 | ||
3635 | if (r < 0) | |
3636 | return -ENOMEM; | |
3637 | ||
3638 | return unit_write_drop_in(u, mode, name, p); | |
3639 | } | |
3640 | ||
3641 | int unit_write_drop_in_private(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *data) { | |
39591351 | 3642 | const char *ndata; |
b42defe3 LP |
3643 | |
3644 | assert(u); | |
3645 | assert(name); | |
3646 | assert(data); | |
3647 | ||
3648 | if (!UNIT_VTABLE(u)->private_section) | |
3649 | return -EINVAL; | |
3650 | ||
6d235724 | 3651 | if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) |
b9ec9359 LP |
3652 | return 0; |
3653 | ||
81d62103 | 3654 | ndata = strjoina("[", UNIT_VTABLE(u)->private_section, "]\n", data); |
b42defe3 LP |
3655 | |
3656 | return unit_write_drop_in(u, mode, name, ndata); | |
3657 | } | |
3658 | ||
b9ec9359 LP |
3659 | int unit_write_drop_in_private_format(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *format, ...) { |
3660 | _cleanup_free_ char *p = NULL; | |
3661 | va_list ap; | |
3662 | int r; | |
3663 | ||
3664 | assert(u); | |
3665 | assert(name); | |
3666 | assert(format); | |
3667 | ||
6d235724 | 3668 | if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) |
b9ec9359 LP |
3669 | return 0; |
3670 | ||
3671 | va_start(ap, format); | |
3672 | r = vasprintf(&p, format, ap); | |
3673 | va_end(ap); | |
3674 | ||
3675 | if (r < 0) | |
3676 | return -ENOMEM; | |
3677 | ||
3678 | return unit_write_drop_in_private(u, mode, name, p); | |
3679 | } | |
71645aca | 3680 | |
c2756a68 | 3681 | int unit_make_transient(Unit *u) { |
4f4afc88 LP |
3682 | FILE *f; |
3683 | char *path; | |
3684 | ||
c2756a68 LP |
3685 | assert(u); |
3686 | ||
3f5e8115 LP |
3687 | if (!UNIT_VTABLE(u)->can_transient) |
3688 | return -EOPNOTSUPP; | |
3689 | ||
605405c6 | 3690 | path = strjoin(u->manager->lookup_paths.transient, "/", u->id); |
4f4afc88 LP |
3691 | if (!path) |
3692 | return -ENOMEM; | |
3693 | ||
3694 | /* Let's open the file we'll write the transient settings into. This file is kept open as long as we are | |
3695 | * creating the transient, and is closed in unit_load(), as soon as we start loading the file. */ | |
3696 | ||
78e334b5 | 3697 | RUN_WITH_UMASK(0022) { |
4f4afc88 | 3698 | f = fopen(path, "we"); |
78e334b5 ZJS |
3699 | if (!f) { |
3700 | free(path); | |
3701 | return -errno; | |
3702 | } | |
4f4afc88 LP |
3703 | } |
3704 | ||
3705 | if (u->transient_file) | |
3706 | fclose(u->transient_file); | |
3707 | u->transient_file = f; | |
3708 | ||
3709 | free(u->fragment_path); | |
3710 | u->fragment_path = path; | |
7c65093a | 3711 | |
7c65093a LP |
3712 | u->source_path = mfree(u->source_path); |
3713 | u->dropin_paths = strv_free(u->dropin_paths); | |
3714 | u->fragment_mtime = u->source_mtime = u->dropin_mtime = 0; | |
3715 | ||
4f4afc88 LP |
3716 | u->load_state = UNIT_STUB; |
3717 | u->load_error = 0; | |
3718 | u->transient = true; | |
3719 | ||
7c65093a LP |
3720 | unit_add_to_dbus_queue(u); |
3721 | unit_add_to_gc_queue(u); | |
c2756a68 | 3722 | |
4f4afc88 LP |
3723 | fputs("# This is a transient unit file, created programmatically via the systemd API. Do not edit.\n", |
3724 | u->transient_file); | |
3725 | ||
3f5e8115 | 3726 | return 0; |
c2756a68 LP |
3727 | } |
3728 | ||
1d98fef1 LP |
3729 | static void log_kill(pid_t pid, int sig, void *userdata) { |
3730 | _cleanup_free_ char *comm = NULL; | |
3731 | ||
3732 | (void) get_process_comm(pid, &comm); | |
3733 | ||
3734 | /* Don't log about processes marked with brackets, under the assumption that these are temporary processes | |
3735 | only, like for example systemd's own PAM stub process. */ | |
3736 | if (comm && comm[0] == '(') | |
3737 | return; | |
3738 | ||
3739 | log_unit_notice(userdata, | |
3740 | "Killing process " PID_FMT " (%s) with signal SIG%s.", | |
3741 | pid, | |
3742 | strna(comm), | |
3743 | signal_to_string(sig)); | |
3744 | } | |
3745 | ||
3746 | static int operation_to_signal(KillContext *c, KillOperation k) { | |
3747 | assert(c); | |
3748 | ||
3749 | switch (k) { | |
3750 | ||
3751 | case KILL_TERMINATE: | |
3752 | case KILL_TERMINATE_AND_LOG: | |
3753 | return c->kill_signal; | |
3754 | ||
3755 | case KILL_KILL: | |
3756 | return SIGKILL; | |
3757 | ||
3758 | case KILL_ABORT: | |
3759 | return SIGABRT; | |
3760 | ||
3761 | default: | |
3762 | assert_not_reached("KillOperation unknown"); | |
3763 | } | |
3764 | } | |
3765 | ||
cd2086fe LP |
3766 | int unit_kill_context( |
3767 | Unit *u, | |
3768 | KillContext *c, | |
db2cb23b | 3769 | KillOperation k, |
cd2086fe LP |
3770 | pid_t main_pid, |
3771 | pid_t control_pid, | |
3772 | bool main_pid_alien) { | |
3773 | ||
1d98fef1 | 3774 | bool wait_for_exit = false, send_sighup; |
59ec09a8 | 3775 | cg_kill_log_func_t log_func = NULL; |
b821a397 | 3776 | int sig, r; |
cd2086fe LP |
3777 | |
3778 | assert(u); | |
3779 | assert(c); | |
3780 | ||
59ec09a8 ZJS |
3781 | /* Kill the processes belonging to this unit, in preparation for shutting the unit down. |
3782 | * Returns > 0 if we killed something worth waiting for, 0 otherwise. */ | |
1d98fef1 | 3783 | |
cd2086fe LP |
3784 | if (c->kill_mode == KILL_NONE) |
3785 | return 0; | |
3786 | ||
1d98fef1 LP |
3787 | sig = operation_to_signal(c, k); |
3788 | ||
3789 | send_sighup = | |
3790 | c->send_sighup && | |
3791 | IN_SET(k, KILL_TERMINATE, KILL_TERMINATE_AND_LOG) && | |
3792 | sig != SIGHUP; | |
3793 | ||
59ec09a8 ZJS |
3794 | if (k != KILL_TERMINATE || IN_SET(sig, SIGKILL, SIGABRT)) |
3795 | log_func = log_kill; | |
cd2086fe LP |
3796 | |
3797 | if (main_pid > 0) { | |
1d98fef1 LP |
3798 | if (log_func) |
3799 | log_func(main_pid, sig, u); | |
cd2086fe | 3800 | |
1d98fef1 | 3801 | r = kill_and_sigcont(main_pid, sig); |
cd2086fe LP |
3802 | if (r < 0 && r != -ESRCH) { |
3803 | _cleanup_free_ char *comm = NULL; | |
1d98fef1 | 3804 | (void) get_process_comm(main_pid, &comm); |
cd2086fe | 3805 | |
b821a397 | 3806 | log_unit_warning_errno(u, r, "Failed to kill main process " PID_FMT " (%s), ignoring: %m", main_pid, strna(comm)); |
82659fd7 | 3807 | } else { |
bc6aed7b LP |
3808 | if (!main_pid_alien) |
3809 | wait_for_exit = true; | |
82659fd7 | 3810 | |
1d98fef1 | 3811 | if (r != -ESRCH && send_sighup) |
d0667321 | 3812 | (void) kill(main_pid, SIGHUP); |
82659fd7 | 3813 | } |
cd2086fe LP |
3814 | } |
3815 | ||
3816 | if (control_pid > 0) { | |
1d98fef1 LP |
3817 | if (log_func) |
3818 | log_func(control_pid, sig, u); | |
cd2086fe | 3819 | |
1d98fef1 | 3820 | r = kill_and_sigcont(control_pid, sig); |
cd2086fe LP |
3821 | if (r < 0 && r != -ESRCH) { |
3822 | _cleanup_free_ char *comm = NULL; | |
1d98fef1 | 3823 | (void) get_process_comm(control_pid, &comm); |
cd2086fe | 3824 | |
b821a397 | 3825 | log_unit_warning_errno(u, r, "Failed to kill control process " PID_FMT " (%s), ignoring: %m", control_pid, strna(comm)); |
82659fd7 | 3826 | } else { |
cd2086fe | 3827 | wait_for_exit = true; |
82659fd7 | 3828 | |
1d98fef1 | 3829 | if (r != -ESRCH && send_sighup) |
d0667321 | 3830 | (void) kill(control_pid, SIGHUP); |
82659fd7 | 3831 | } |
cd2086fe LP |
3832 | } |
3833 | ||
b821a397 LP |
3834 | if (u->cgroup_path && |
3835 | (c->kill_mode == KILL_CONTROL_GROUP || (c->kill_mode == KILL_MIXED && k == KILL_KILL))) { | |
cd2086fe LP |
3836 | _cleanup_set_free_ Set *pid_set = NULL; |
3837 | ||
82659fd7 LP |
3838 | /* Exclude the main/control pids from being killed via the cgroup */ |
3839 | pid_set = unit_pid_set(main_pid, control_pid); | |
cd2086fe LP |
3840 | if (!pid_set) |
3841 | return -ENOMEM; | |
3842 | ||
1d98fef1 LP |
3843 | r = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, |
3844 | sig, | |
3845 | CGROUP_SIGCONT|CGROUP_IGNORE_SELF, | |
3846 | pid_set, | |
3847 | log_func, u); | |
cd2086fe LP |
3848 | if (r < 0) { |
3849 | if (r != -EAGAIN && r != -ESRCH && r != -ENOENT) | |
b821a397 LP |
3850 | log_unit_warning_errno(u, r, "Failed to kill control group %s, ignoring: %m", u->cgroup_path); |
3851 | ||
82659fd7 | 3852 | } else if (r > 0) { |
bc6aed7b | 3853 | |
efdb0237 LP |
3854 | /* FIXME: For now, on the legacy hierarchy, we |
3855 | * will not wait for the cgroup members to die | |
3856 | * if we are running in a container or if this | |
3857 | * is a delegation unit, simply because cgroup | |
3858 | * notification is unreliable in these | |
3859 | * cases. It doesn't work at all in | |
3860 | * containers, and outside of containers it | |
3861 | * can be confused easily by left-over | |
ccddd104 | 3862 | * directories in the cgroup — which however |
efdb0237 LP |
3863 | * should not exist in non-delegated units. On |
3864 | * the unified hierarchy that's different, | |
3865 | * there we get proper events. Hence rely on | |
3866 | * them.*/ | |
3867 | ||
5da38d07 | 3868 | if (cg_unified(SYSTEMD_CGROUP_CONTROLLER) > 0 || |
75f86906 | 3869 | (detect_container() == 0 && !unit_cgroup_delegate(u))) |
e9db43d5 | 3870 | wait_for_exit = true; |
58ea275a | 3871 | |
1d98fef1 | 3872 | if (send_sighup) { |
82659fd7 LP |
3873 | set_free(pid_set); |
3874 | ||
3875 | pid_set = unit_pid_set(main_pid, control_pid); | |
3876 | if (!pid_set) | |
3877 | return -ENOMEM; | |
3878 | ||
1d98fef1 LP |
3879 | cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, |
3880 | SIGHUP, | |
3881 | CGROUP_IGNORE_SELF, | |
3882 | pid_set, | |
3883 | NULL, NULL); | |
82659fd7 LP |
3884 | } |
3885 | } | |
cd2086fe LP |
3886 | } |
3887 | ||
3888 | return wait_for_exit; | |
3889 | } | |
3890 | ||
a57f7e2c LP |
3891 | int unit_require_mounts_for(Unit *u, const char *path) { |
3892 | char prefix[strlen(path) + 1], *p; | |
3893 | int r; | |
3894 | ||
3895 | assert(u); | |
3896 | assert(path); | |
3897 | ||
3898 | /* Registers a unit for requiring a certain path and all its | |
3899 | * prefixes. We keep a simple array of these paths in the | |
3900 | * unit, since its usually short. However, we build a prefix | |
3901 | * table for all possible prefixes so that new appearing mount | |
3902 | * units can easily determine which units to make themselves a | |
3903 | * dependency of. */ | |
3904 | ||
70b64bd3 ZJS |
3905 | if (!path_is_absolute(path)) |
3906 | return -EINVAL; | |
3907 | ||
a57f7e2c LP |
3908 | p = strdup(path); |
3909 | if (!p) | |
3910 | return -ENOMEM; | |
3911 | ||
3912 | path_kill_slashes(p); | |
3913 | ||
a57f7e2c LP |
3914 | if (!path_is_safe(p)) { |
3915 | free(p); | |
3916 | return -EPERM; | |
3917 | } | |
3918 | ||
3919 | if (strv_contains(u->requires_mounts_for, p)) { | |
3920 | free(p); | |
3921 | return 0; | |
3922 | } | |
3923 | ||
6e18964d ZJS |
3924 | r = strv_consume(&u->requires_mounts_for, p); |
3925 | if (r < 0) | |
a57f7e2c | 3926 | return r; |
a57f7e2c LP |
3927 | |
3928 | PATH_FOREACH_PREFIX_MORE(prefix, p) { | |
3929 | Set *x; | |
3930 | ||
3931 | x = hashmap_get(u->manager->units_requiring_mounts_for, prefix); | |
3932 | if (!x) { | |
3933 | char *q; | |
3934 | ||
742f41ad LP |
3935 | r = hashmap_ensure_allocated(&u->manager->units_requiring_mounts_for, &string_hash_ops); |
3936 | if (r < 0) | |
3937 | return r; | |
a57f7e2c LP |
3938 | |
3939 | q = strdup(prefix); | |
3940 | if (!q) | |
3941 | return -ENOMEM; | |
3942 | ||
d5099efc | 3943 | x = set_new(NULL); |
a57f7e2c LP |
3944 | if (!x) { |
3945 | free(q); | |
3946 | return -ENOMEM; | |
3947 | } | |
3948 | ||
3949 | r = hashmap_put(u->manager->units_requiring_mounts_for, q, x); | |
3950 | if (r < 0) { | |
3951 | free(q); | |
3952 | set_free(x); | |
3953 | return r; | |
3954 | } | |
3955 | } | |
3956 | ||
3957 | r = set_put(x, u); | |
3958 | if (r < 0) | |
3959 | return r; | |
3960 | } | |
3961 | ||
3962 | return 0; | |
3963 | } | |
3964 | ||
613b411c LP |
3965 | int unit_setup_exec_runtime(Unit *u) { |
3966 | ExecRuntime **rt; | |
3967 | size_t offset; | |
3968 | Iterator i; | |
3969 | Unit *other; | |
3970 | ||
3971 | offset = UNIT_VTABLE(u)->exec_runtime_offset; | |
3972 | assert(offset > 0); | |
3973 | ||
06b643e7 | 3974 | /* Check if there already is an ExecRuntime for this unit? */ |
613b411c LP |
3975 | rt = (ExecRuntime**) ((uint8_t*) u + offset); |
3976 | if (*rt) | |
3977 | return 0; | |
3978 | ||
3979 | /* Try to get it from somebody else */ | |
3980 | SET_FOREACH(other, u->dependencies[UNIT_JOINS_NAMESPACE_OF], i) { | |
3981 | ||
3982 | *rt = unit_get_exec_runtime(other); | |
3983 | if (*rt) { | |
3984 | exec_runtime_ref(*rt); | |
3985 | return 0; | |
3986 | } | |
3987 | } | |
3988 | ||
3989 | return exec_runtime_make(rt, unit_get_exec_context(u), u->id); | |
3990 | } | |
3991 | ||
29206d46 LP |
3992 | int unit_setup_dynamic_creds(Unit *u) { |
3993 | ExecContext *ec; | |
3994 | DynamicCreds *dcreds; | |
3995 | size_t offset; | |
3996 | ||
3997 | assert(u); | |
3998 | ||
3999 | offset = UNIT_VTABLE(u)->dynamic_creds_offset; | |
4000 | assert(offset > 0); | |
4001 | dcreds = (DynamicCreds*) ((uint8_t*) u + offset); | |
4002 | ||
4003 | ec = unit_get_exec_context(u); | |
4004 | assert(ec); | |
4005 | ||
4006 | if (!ec->dynamic_user) | |
4007 | return 0; | |
4008 | ||
4009 | return dynamic_creds_acquire(dcreds, u->manager, ec->user, ec->group); | |
4010 | } | |
4011 | ||
1c2e9646 LP |
4012 | bool unit_type_supported(UnitType t) { |
4013 | if (_unlikely_(t < 0)) | |
4014 | return false; | |
4015 | if (_unlikely_(t >= _UNIT_TYPE_MAX)) | |
4016 | return false; | |
4017 | ||
4018 | if (!unit_vtable[t]->supported) | |
4019 | return true; | |
4020 | ||
4021 | return unit_vtable[t]->supported(); | |
4022 | } | |
4023 | ||
8b4305c7 LP |
4024 | void unit_warn_if_dir_nonempty(Unit *u, const char* where) { |
4025 | int r; | |
4026 | ||
4027 | assert(u); | |
4028 | assert(where); | |
4029 | ||
4030 | r = dir_is_empty(where); | |
4031 | if (r > 0) | |
4032 | return; | |
4033 | if (r < 0) { | |
4034 | log_unit_warning_errno(u, r, "Failed to check directory %s: %m", where); | |
4035 | return; | |
4036 | } | |
4037 | ||
4038 | log_struct(LOG_NOTICE, | |
4039 | LOG_MESSAGE_ID(SD_MESSAGE_OVERMOUNTING), | |
4040 | LOG_UNIT_ID(u), | |
4041 | LOG_UNIT_MESSAGE(u, "Directory %s to mount over is not empty, mounting anyway.", where), | |
4042 | "WHERE=%s", where, | |
4043 | NULL); | |
4044 | } | |
4045 | ||
4046 | int unit_fail_if_symlink(Unit *u, const char* where) { | |
4047 | int r; | |
4048 | ||
4049 | assert(u); | |
4050 | assert(where); | |
4051 | ||
4052 | r = is_symlink(where); | |
4053 | if (r < 0) { | |
4054 | log_unit_debug_errno(u, r, "Failed to check symlink %s, ignoring: %m", where); | |
4055 | return 0; | |
4056 | } | |
4057 | if (r == 0) | |
4058 | return 0; | |
4059 | ||
4060 | log_struct(LOG_ERR, | |
4061 | LOG_MESSAGE_ID(SD_MESSAGE_OVERMOUNTING), | |
4062 | LOG_UNIT_ID(u), | |
4063 | LOG_UNIT_MESSAGE(u, "Mount on symlink %s not allowed.", where), | |
4064 | "WHERE=%s", where, | |
4065 | NULL); | |
4066 | ||
4067 | return -ELOOP; | |
4068 | } | |
0f13f3bd LP |
4069 | |
4070 | bool unit_is_pristine(Unit *u) { | |
4071 | assert(u); | |
4072 | ||
7c65093a | 4073 | /* Check if the unit already exists or is already around, |
0f13f3bd LP |
4074 | * in a number of different ways. Note that to cater for unit |
4075 | * types such as slice, we are generally fine with units that | |
61233823 | 4076 | * are marked UNIT_LOADED even though nothing was |
0f13f3bd LP |
4077 | * actually loaded, as those unit types don't require a file |
4078 | * on disk to validly load. */ | |
4079 | ||
4080 | return !(!IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_LOADED) || | |
4081 | u->fragment_path || | |
4082 | u->source_path || | |
4083 | !strv_isempty(u->dropin_paths) || | |
0f13f3bd LP |
4084 | u->job || |
4085 | u->merged_into); | |
4086 | } | |
291d565a LP |
4087 | |
4088 | pid_t unit_control_pid(Unit *u) { | |
4089 | assert(u); | |
4090 | ||
4091 | if (UNIT_VTABLE(u)->control_pid) | |
4092 | return UNIT_VTABLE(u)->control_pid(u); | |
4093 | ||
4094 | return 0; | |
4095 | } | |
4096 | ||
4097 | pid_t unit_main_pid(Unit *u) { | |
4098 | assert(u); | |
4099 | ||
4100 | if (UNIT_VTABLE(u)->main_pid) | |
4101 | return UNIT_VTABLE(u)->main_pid(u); | |
4102 | ||
4103 | return 0; | |
4104 | } | |
00d9ef85 LP |
4105 | |
4106 | static void unit_unref_uid_internal( | |
4107 | Unit *u, | |
4108 | uid_t *ref_uid, | |
4109 | bool destroy_now, | |
4110 | void (*_manager_unref_uid)(Manager *m, uid_t uid, bool destroy_now)) { | |
4111 | ||
4112 | assert(u); | |
4113 | assert(ref_uid); | |
4114 | assert(_manager_unref_uid); | |
4115 | ||
4116 | /* Generic implementation of both unit_unref_uid() and unit_unref_gid(), under the assumption that uid_t and | |
4117 | * gid_t are actually the same time, with the same validity rules. | |
4118 | * | |
4119 | * Drops a reference to UID/GID from a unit. */ | |
4120 | ||
4121 | assert_cc(sizeof(uid_t) == sizeof(gid_t)); | |
4122 | assert_cc(UID_INVALID == (uid_t) GID_INVALID); | |
4123 | ||
4124 | if (!uid_is_valid(*ref_uid)) | |
4125 | return; | |
4126 | ||
4127 | _manager_unref_uid(u->manager, *ref_uid, destroy_now); | |
4128 | *ref_uid = UID_INVALID; | |
4129 | } | |
4130 | ||
4131 | void unit_unref_uid(Unit *u, bool destroy_now) { | |
4132 | unit_unref_uid_internal(u, &u->ref_uid, destroy_now, manager_unref_uid); | |
4133 | } | |
4134 | ||
4135 | void unit_unref_gid(Unit *u, bool destroy_now) { | |
4136 | unit_unref_uid_internal(u, (uid_t*) &u->ref_gid, destroy_now, manager_unref_gid); | |
4137 | } | |
4138 | ||
4139 | static int unit_ref_uid_internal( | |
4140 | Unit *u, | |
4141 | uid_t *ref_uid, | |
4142 | uid_t uid, | |
4143 | bool clean_ipc, | |
4144 | int (*_manager_ref_uid)(Manager *m, uid_t uid, bool clean_ipc)) { | |
4145 | ||
4146 | int r; | |
4147 | ||
4148 | assert(u); | |
4149 | assert(ref_uid); | |
4150 | assert(uid_is_valid(uid)); | |
4151 | assert(_manager_ref_uid); | |
4152 | ||
4153 | /* Generic implementation of both unit_ref_uid() and unit_ref_guid(), under the assumption that uid_t and gid_t | |
4154 | * are actually the same type, and have the same validity rules. | |
4155 | * | |
4156 | * Adds a reference on a specific UID/GID to this unit. Each unit referencing the same UID/GID maintains a | |
4157 | * reference so that we can destroy the UID/GID's IPC resources as soon as this is requested and the counter | |
4158 | * drops to zero. */ | |
4159 | ||
4160 | assert_cc(sizeof(uid_t) == sizeof(gid_t)); | |
4161 | assert_cc(UID_INVALID == (uid_t) GID_INVALID); | |
4162 | ||
4163 | if (*ref_uid == uid) | |
4164 | return 0; | |
4165 | ||
4166 | if (uid_is_valid(*ref_uid)) /* Already set? */ | |
4167 | return -EBUSY; | |
4168 | ||
4169 | r = _manager_ref_uid(u->manager, uid, clean_ipc); | |
4170 | if (r < 0) | |
4171 | return r; | |
4172 | ||
4173 | *ref_uid = uid; | |
4174 | return 1; | |
4175 | } | |
4176 | ||
4177 | int unit_ref_uid(Unit *u, uid_t uid, bool clean_ipc) { | |
4178 | return unit_ref_uid_internal(u, &u->ref_uid, uid, clean_ipc, manager_ref_uid); | |
4179 | } | |
4180 | ||
4181 | int unit_ref_gid(Unit *u, gid_t gid, bool clean_ipc) { | |
4182 | return unit_ref_uid_internal(u, (uid_t*) &u->ref_gid, (uid_t) gid, clean_ipc, manager_ref_gid); | |
4183 | } | |
4184 | ||
4185 | static int unit_ref_uid_gid_internal(Unit *u, uid_t uid, gid_t gid, bool clean_ipc) { | |
4186 | int r = 0, q = 0; | |
4187 | ||
4188 | assert(u); | |
4189 | ||
4190 | /* Reference both a UID and a GID in one go. Either references both, or neither. */ | |
4191 | ||
4192 | if (uid_is_valid(uid)) { | |
4193 | r = unit_ref_uid(u, uid, clean_ipc); | |
4194 | if (r < 0) | |
4195 | return r; | |
4196 | } | |
4197 | ||
4198 | if (gid_is_valid(gid)) { | |
4199 | q = unit_ref_gid(u, gid, clean_ipc); | |
4200 | if (q < 0) { | |
4201 | if (r > 0) | |
4202 | unit_unref_uid(u, false); | |
4203 | ||
4204 | return q; | |
4205 | } | |
4206 | } | |
4207 | ||
4208 | return r > 0 || q > 0; | |
4209 | } | |
4210 | ||
4211 | int unit_ref_uid_gid(Unit *u, uid_t uid, gid_t gid) { | |
4212 | ExecContext *c; | |
4213 | int r; | |
4214 | ||
4215 | assert(u); | |
4216 | ||
4217 | c = unit_get_exec_context(u); | |
4218 | ||
4219 | r = unit_ref_uid_gid_internal(u, uid, gid, c ? c->remove_ipc : false); | |
4220 | if (r < 0) | |
4221 | return log_unit_warning_errno(u, r, "Couldn't add UID/GID reference to unit, proceeding without: %m"); | |
4222 | ||
4223 | return r; | |
4224 | } | |
4225 | ||
4226 | void unit_unref_uid_gid(Unit *u, bool destroy_now) { | |
4227 | assert(u); | |
4228 | ||
4229 | unit_unref_uid(u, destroy_now); | |
4230 | unit_unref_gid(u, destroy_now); | |
4231 | } | |
4232 | ||
4233 | void unit_notify_user_lookup(Unit *u, uid_t uid, gid_t gid) { | |
4234 | int r; | |
4235 | ||
4236 | assert(u); | |
4237 | ||
4238 | /* This is invoked whenever one of the forked off processes let's us know the UID/GID its user name/group names | |
4239 | * resolved to. We keep track of which UID/GID is currently assigned in order to be able to destroy its IPC | |
4240 | * objects when no service references the UID/GID anymore. */ | |
4241 | ||
4242 | r = unit_ref_uid_gid(u, uid, gid); | |
4243 | if (r > 0) | |
4244 | bus_unit_send_change_signal(u); | |
4245 | } | |
4b58153d LP |
4246 | |
4247 | int unit_set_invocation_id(Unit *u, sd_id128_t id) { | |
4248 | int r; | |
4249 | ||
4250 | assert(u); | |
4251 | ||
4252 | /* Set the invocation ID for this unit. If we cannot, this will not roll back, but reset the whole thing. */ | |
4253 | ||
4254 | if (sd_id128_equal(u->invocation_id, id)) | |
4255 | return 0; | |
4256 | ||
4257 | if (!sd_id128_is_null(u->invocation_id)) | |
4258 | (void) hashmap_remove_value(u->manager->units_by_invocation_id, &u->invocation_id, u); | |
4259 | ||
4260 | if (sd_id128_is_null(id)) { | |
4261 | r = 0; | |
4262 | goto reset; | |
4263 | } | |
4264 | ||
4265 | r = hashmap_ensure_allocated(&u->manager->units_by_invocation_id, &id128_hash_ops); | |
4266 | if (r < 0) | |
4267 | goto reset; | |
4268 | ||
4269 | u->invocation_id = id; | |
4270 | sd_id128_to_string(id, u->invocation_id_string); | |
4271 | ||
4272 | r = hashmap_put(u->manager->units_by_invocation_id, &u->invocation_id, u); | |
4273 | if (r < 0) | |
4274 | goto reset; | |
4275 | ||
4276 | return 0; | |
4277 | ||
4278 | reset: | |
4279 | u->invocation_id = SD_ID128_NULL; | |
4280 | u->invocation_id_string[0] = 0; | |
4281 | return r; | |
4282 | } | |
4283 | ||
4284 | int unit_acquire_invocation_id(Unit *u) { | |
4285 | sd_id128_t id; | |
4286 | int r; | |
4287 | ||
4288 | assert(u); | |
4289 | ||
4290 | r = sd_id128_randomize(&id); | |
4291 | if (r < 0) | |
4292 | return log_unit_error_errno(u, r, "Failed to generate invocation ID for unit: %m"); | |
4293 | ||
4294 | r = unit_set_invocation_id(u, id); | |
4295 | if (r < 0) | |
4296 | return log_unit_error_errno(u, r, "Failed to set invocation ID for unit: %m"); | |
4297 | ||
4298 | return 0; | |
4299 | } |