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