]>
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 | |
03455c28 | 531 | 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; | |
95ae05c0 WC |
1175 | |
1176 | c = unit_get_cgroup_context(u); | |
db785129 LP |
1177 | if (!c) |
1178 | return 0; | |
1179 | ||
1180 | if (c->startup_cpu_shares == (unsigned long) -1 && | |
1181 | c->startup_blockio_weight == (unsigned long) -1) | |
1182 | return 0; | |
1183 | ||
756c09e6 | 1184 | return set_put(u->manager->startup_units, u); |
95ae05c0 WC |
1185 | } |
1186 | ||
87f0e418 LP |
1187 | int unit_load(Unit *u) { |
1188 | int r; | |
1189 | ||
1190 | assert(u); | |
1191 | ||
ac155bb8 | 1192 | if (u->in_load_queue) { |
71fda00f | 1193 | LIST_REMOVE(load_queue, u->manager->load_queue, u); |
ac155bb8 | 1194 | u->in_load_queue = false; |
87f0e418 LP |
1195 | } |
1196 | ||
ac155bb8 | 1197 | if (u->type == _UNIT_TYPE_INVALID) |
e537352b LP |
1198 | return -EINVAL; |
1199 | ||
ac155bb8 | 1200 | if (u->load_state != UNIT_STUB) |
87f0e418 LP |
1201 | return 0; |
1202 | ||
c2756a68 LP |
1203 | if (UNIT_VTABLE(u)->load) { |
1204 | r = UNIT_VTABLE(u)->load(u); | |
1205 | if (r < 0) | |
87f0e418 | 1206 | goto fail; |
c2756a68 | 1207 | } |
23a177ef | 1208 | |
ac155bb8 | 1209 | if (u->load_state == UNIT_STUB) { |
23a177ef LP |
1210 | r = -ENOENT; |
1211 | goto fail; | |
1212 | } | |
1213 | ||
7c8fa05c | 1214 | if (u->load_state == UNIT_LOADED) { |
c2756a68 | 1215 | |
e954c9cf LP |
1216 | r = unit_add_target_dependencies(u); |
1217 | if (r < 0) | |
1218 | goto fail; | |
1219 | ||
1220 | r = unit_add_slice_dependencies(u); | |
1221 | if (r < 0) | |
1222 | goto fail; | |
c2756a68 | 1223 | |
e954c9cf | 1224 | r = unit_add_mount_dependencies(u); |
7c8fa05c | 1225 | if (r < 0) |
c2756a68 | 1226 | goto fail; |
7c8fa05c | 1227 | |
95ae05c0 WC |
1228 | r = unit_add_startup_units(u); |
1229 | if (r < 0) | |
1230 | goto fail; | |
1231 | ||
bc432dc7 | 1232 | if (u->on_failure_job_mode == JOB_ISOLATE && set_size(u->dependencies[UNIT_ON_FAILURE]) > 1) { |
f2341e0a | 1233 | log_unit_error(u, "More than one OnFailure= dependencies specified but OnFailureJobMode=isolate set. Refusing."); |
c2756a68 LP |
1234 | r = -EINVAL; |
1235 | goto fail; | |
1236 | } | |
bc432dc7 LP |
1237 | |
1238 | unit_update_cgroup_members_masks(u); | |
f68319bb LP |
1239 | } |
1240 | ||
ac155bb8 | 1241 | assert((u->load_state != UNIT_MERGED) == !u->merged_into); |
23a177ef LP |
1242 | |
1243 | unit_add_to_dbus_queue(unit_follow_merge(u)); | |
701cc384 | 1244 | unit_add_to_gc_queue(u); |
87f0e418 | 1245 | |
87f0e418 LP |
1246 | return 0; |
1247 | ||
1248 | fail: | |
c2756a68 | 1249 | u->load_state = u->load_state == UNIT_STUB ? UNIT_NOT_FOUND : UNIT_ERROR; |
ac155bb8 | 1250 | u->load_error = r; |
c1e1601e | 1251 | unit_add_to_dbus_queue(u); |
9a46fc3b | 1252 | unit_add_to_gc_queue(u); |
23a177ef | 1253 | |
f2341e0a | 1254 | log_unit_debug_errno(u, r, "Failed to load configuration: %m"); |
23a177ef | 1255 | |
87f0e418 LP |
1256 | return r; |
1257 | } | |
1258 | ||
49365733 LP |
1259 | static bool unit_condition_test_list(Unit *u, Condition *first, const char *(*to_string)(ConditionType t)) { |
1260 | Condition *c; | |
1261 | int triggered = -1; | |
1262 | ||
1263 | assert(u); | |
1264 | assert(to_string); | |
1265 | ||
1266 | /* If the condition list is empty, then it is true */ | |
1267 | if (!first) | |
1268 | return true; | |
1269 | ||
1270 | /* Otherwise, if all of the non-trigger conditions apply and | |
1271 | * if any of the trigger conditions apply (unless there are | |
1272 | * none) we return true */ | |
1273 | LIST_FOREACH(conditions, c, first) { | |
1274 | int r; | |
1275 | ||
1276 | r = condition_test(c); | |
1277 | if (r < 0) | |
f2341e0a LP |
1278 | log_unit_warning(u, |
1279 | "Couldn't determine result for %s=%s%s%s, assuming failed: %m", | |
49365733 LP |
1280 | to_string(c->type), |
1281 | c->trigger ? "|" : "", | |
1282 | c->negate ? "!" : "", | |
f2341e0a | 1283 | c->parameter); |
49365733 | 1284 | else |
f2341e0a LP |
1285 | log_unit_debug(u, |
1286 | "%s=%s%s%s %s.", | |
49365733 LP |
1287 | to_string(c->type), |
1288 | c->trigger ? "|" : "", | |
1289 | c->negate ? "!" : "", | |
1290 | c->parameter, | |
f2341e0a | 1291 | condition_result_to_string(c->result)); |
49365733 LP |
1292 | |
1293 | if (!c->trigger && r <= 0) | |
1294 | return false; | |
1295 | ||
1296 | if (c->trigger && triggered <= 0) | |
1297 | triggered = r > 0; | |
1298 | } | |
1299 | ||
1300 | return triggered != 0; | |
1301 | } | |
1302 | ||
9588bc32 | 1303 | static bool unit_condition_test(Unit *u) { |
90bbc946 LP |
1304 | assert(u); |
1305 | ||
ac155bb8 | 1306 | dual_timestamp_get(&u->condition_timestamp); |
49365733 | 1307 | u->condition_result = unit_condition_test_list(u, u->conditions, condition_type_to_string); |
90bbc946 | 1308 | |
ac155bb8 | 1309 | return u->condition_result; |
90bbc946 LP |
1310 | } |
1311 | ||
59fccdc5 LP |
1312 | static bool unit_assert_test(Unit *u) { |
1313 | assert(u); | |
1314 | ||
1315 | dual_timestamp_get(&u->assert_timestamp); | |
49365733 | 1316 | u->assert_result = unit_condition_test_list(u, u->asserts, assert_type_to_string); |
59fccdc5 LP |
1317 | |
1318 | return u->assert_result; | |
1319 | } | |
1320 | ||
44a6b1b6 | 1321 | _pure_ static const char* unit_get_status_message_format(Unit *u, JobType t) { |
877d54e9 | 1322 | const char *format; |
a85ca902 | 1323 | const UnitStatusMessageFormats *format_table; |
877d54e9 LP |
1324 | |
1325 | assert(u); | |
b5bf308b | 1326 | assert(t == JOB_START || t == JOB_STOP || t == JOB_RELOAD); |
877d54e9 | 1327 | |
b5bf308b | 1328 | if (t != JOB_RELOAD) { |
a85ca902 MS |
1329 | format_table = &UNIT_VTABLE(u)->status_message_formats; |
1330 | if (format_table) { | |
1331 | format = format_table->starting_stopping[t == JOB_STOP]; | |
1332 | if (format) | |
1333 | return format; | |
1334 | } | |
1335 | } | |
877d54e9 LP |
1336 | |
1337 | /* Return generic strings */ | |
1338 | if (t == JOB_START) | |
1339 | return "Starting %s."; | |
1340 | else if (t == JOB_STOP) | |
1341 | return "Stopping %s."; | |
b5bf308b | 1342 | else |
877d54e9 | 1343 | return "Reloading %s."; |
877d54e9 LP |
1344 | } |
1345 | ||
1346 | static void unit_status_print_starting_stopping(Unit *u, JobType t) { | |
1347 | const char *format; | |
1348 | ||
1349 | assert(u); | |
1350 | ||
877d54e9 | 1351 | format = unit_get_status_message_format(u, t); |
c6918296 | 1352 | |
bcfce235 | 1353 | DISABLE_WARNING_FORMAT_NONLITERAL; |
49b1d377 | 1354 | unit_status_printf(u, "", format); |
bcfce235 | 1355 | REENABLE_WARNING; |
c6918296 MS |
1356 | } |
1357 | ||
877d54e9 LP |
1358 | static void unit_status_log_starting_stopping_reloading(Unit *u, JobType t) { |
1359 | const char *format; | |
1360 | char buf[LINE_MAX]; | |
1361 | sd_id128_t mid; | |
1362 | ||
1363 | assert(u); | |
1364 | ||
1365 | if (t != JOB_START && t != JOB_STOP && t != JOB_RELOAD) | |
1366 | return; | |
1367 | ||
81270860 LP |
1368 | if (log_on_console()) |
1369 | return; | |
1370 | ||
877d54e9 LP |
1371 | /* We log status messages for all units and all operations. */ |
1372 | ||
a85ca902 | 1373 | format = unit_get_status_message_format(u, t); |
877d54e9 | 1374 | |
bcfce235 | 1375 | DISABLE_WARNING_FORMAT_NONLITERAL; |
877d54e9 | 1376 | snprintf(buf, sizeof(buf), format, unit_description(u)); |
bcfce235 | 1377 | REENABLE_WARNING; |
877d54e9 LP |
1378 | |
1379 | mid = t == JOB_START ? SD_MESSAGE_UNIT_STARTING : | |
1380 | t == JOB_STOP ? SD_MESSAGE_UNIT_STOPPING : | |
1381 | SD_MESSAGE_UNIT_RELOADING; | |
1382 | ||
f2341e0a LP |
1383 | /* Note that we deliberately use LOG_MESSAGE() instead of |
1384 | * LOG_UNIT_MESSAGE() here, since this is supposed to mimic | |
1385 | * closely what is written to screen using the status output, | |
1386 | * which is supposed the highest level, friendliest output | |
1387 | * possible, which means we should avoid the low-level unit | |
1388 | * name. */ | |
1389 | log_struct(LOG_INFO, | |
1390 | LOG_MESSAGE_ID(mid), | |
1391 | LOG_UNIT_ID(u), | |
1392 | LOG_MESSAGE("%s", buf), | |
1393 | NULL); | |
877d54e9 | 1394 | } |
877d54e9 | 1395 | |
d1a34ae9 MS |
1396 | void unit_status_emit_starting_stopping_reloading(Unit *u, JobType t) { |
1397 | ||
1398 | unit_status_log_starting_stopping_reloading(u, t); | |
1399 | ||
1400 | /* Reload status messages have traditionally not been printed to console. */ | |
1401 | if (t != JOB_RELOAD) | |
1402 | unit_status_print_starting_stopping(u, t); | |
1403 | } | |
1404 | ||
87f0e418 | 1405 | /* Errors: |
d5159713 LP |
1406 | * -EBADR: This unit type does not support starting. |
1407 | * -EALREADY: Unit is already started. | |
1408 | * -EAGAIN: An operation is already in progress. Retry later. | |
1409 | * -ECANCELED: Too many requests for now. | |
59fccdc5 | 1410 | * -EPROTO: Assert failed |
87f0e418 LP |
1411 | */ |
1412 | int unit_start(Unit *u) { | |
1413 | UnitActiveState state; | |
92ab323c | 1414 | Unit *following; |
87f0e418 LP |
1415 | |
1416 | assert(u); | |
1417 | ||
8ff4d2ab | 1418 | /* Units that aren't loaded cannot be started */ |
ac155bb8 | 1419 | if (u->load_state != UNIT_LOADED) |
6124958c LP |
1420 | return -EINVAL; |
1421 | ||
a82e5507 LP |
1422 | /* If this is already started, then this will succeed. Note |
1423 | * that this will even succeed if this unit is not startable | |
1424 | * by the user. This is relied on to detect when we need to | |
1425 | * wait for units and when waiting is finished. */ | |
87f0e418 LP |
1426 | state = unit_active_state(u); |
1427 | if (UNIT_IS_ACTIVE_OR_RELOADING(state)) | |
1428 | return -EALREADY; | |
1429 | ||
a82e5507 LP |
1430 | /* If the conditions failed, don't do anything at all. If we |
1431 | * already are activating this call might still be useful to | |
1432 | * speed up activation in case there is some hold-off time, | |
1433 | * but we don't want to recheck the condition in that case. */ | |
1434 | if (state != UNIT_ACTIVATING && | |
1435 | !unit_condition_test(u)) { | |
f2341e0a | 1436 | log_unit_debug(u, "Starting requested but condition failed. Not starting unit."); |
52661efd LP |
1437 | return -EALREADY; |
1438 | } | |
1439 | ||
59fccdc5 LP |
1440 | /* If the asserts failed, fail the entire job */ |
1441 | if (state != UNIT_ACTIVATING && | |
1442 | !unit_assert_test(u)) { | |
f2341e0a | 1443 | log_unit_notice(u, "Starting requested but asserts failed."); |
59fccdc5 LP |
1444 | return -EPROTO; |
1445 | } | |
1446 | ||
d11a7645 LP |
1447 | /* Units of types that aren't supported cannot be |
1448 | * started. Note that we do this test only after the condition | |
1449 | * checks, so that we rather return condition check errors | |
1450 | * (which are usually not considered a true failure) than "not | |
1451 | * supported" errors (which are considered a failure). | |
1452 | */ | |
1453 | if (!unit_supported(u)) | |
1454 | return -EOPNOTSUPP; | |
1455 | ||
92ab323c | 1456 | /* Forward to the main object, if we aren't it. */ |
52990c2e ZJS |
1457 | following = unit_following(u); |
1458 | if (following) { | |
f2341e0a | 1459 | log_unit_debug(u, "Redirecting start request from %s to %s.", u->id, following->id); |
92ab323c LP |
1460 | return unit_start(following); |
1461 | } | |
1462 | ||
1463 | /* If it is stopped, but we cannot start it, then fail */ | |
1464 | if (!UNIT_VTABLE(u)->start) | |
1465 | return -EBADR; | |
1466 | ||
87f0e418 LP |
1467 | /* We don't suppress calls to ->start() here when we are |
1468 | * already starting, to allow this request to be used as a | |
1469 | * "hurry up" call, for example when the unit is in some "auto | |
1470 | * restart" state where it waits for a holdoff timer to elapse | |
1471 | * before it will start again. */ | |
1472 | ||
c1e1601e | 1473 | unit_add_to_dbus_queue(u); |
9e58ff9c | 1474 | |
d1a34ae9 | 1475 | return UNIT_VTABLE(u)->start(u); |
87f0e418 LP |
1476 | } |
1477 | ||
1478 | bool unit_can_start(Unit *u) { | |
1479 | assert(u); | |
1480 | ||
8ff4d2ab LP |
1481 | if (u->load_state != UNIT_LOADED) |
1482 | return false; | |
1483 | ||
1484 | if (!unit_supported(u)) | |
1485 | return false; | |
1486 | ||
87f0e418 LP |
1487 | return !!UNIT_VTABLE(u)->start; |
1488 | } | |
1489 | ||
2528a7a6 LP |
1490 | bool unit_can_isolate(Unit *u) { |
1491 | assert(u); | |
1492 | ||
1493 | return unit_can_start(u) && | |
ac155bb8 | 1494 | u->allow_isolate; |
2528a7a6 LP |
1495 | } |
1496 | ||
87f0e418 LP |
1497 | /* Errors: |
1498 | * -EBADR: This unit type does not support stopping. | |
1499 | * -EALREADY: Unit is already stopped. | |
1500 | * -EAGAIN: An operation is already in progress. Retry later. | |
1501 | */ | |
1502 | int unit_stop(Unit *u) { | |
1503 | UnitActiveState state; | |
92ab323c | 1504 | Unit *following; |
87f0e418 LP |
1505 | |
1506 | assert(u); | |
1507 | ||
87f0e418 | 1508 | state = unit_active_state(u); |
fdf20a31 | 1509 | if (UNIT_IS_INACTIVE_OR_FAILED(state)) |
87f0e418 LP |
1510 | return -EALREADY; |
1511 | ||
0faacd47 LP |
1512 | following = unit_following(u); |
1513 | if (following) { | |
f2341e0a | 1514 | log_unit_debug(u, "Redirecting stop request from %s to %s.", u->id, following->id); |
92ab323c LP |
1515 | return unit_stop(following); |
1516 | } | |
1517 | ||
7898b0cf LP |
1518 | if (!UNIT_VTABLE(u)->stop) |
1519 | return -EBADR; | |
1520 | ||
c1e1601e | 1521 | unit_add_to_dbus_queue(u); |
9e58ff9c | 1522 | |
d1a34ae9 | 1523 | return UNIT_VTABLE(u)->stop(u); |
87f0e418 LP |
1524 | } |
1525 | ||
1526 | /* Errors: | |
1527 | * -EBADR: This unit type does not support reloading. | |
1528 | * -ENOEXEC: Unit is not started. | |
1529 | * -EAGAIN: An operation is already in progress. Retry later. | |
1530 | */ | |
1531 | int unit_reload(Unit *u) { | |
1532 | UnitActiveState state; | |
92ab323c | 1533 | Unit *following; |
87f0e418 LP |
1534 | |
1535 | assert(u); | |
1536 | ||
ac155bb8 | 1537 | if (u->load_state != UNIT_LOADED) |
6124958c LP |
1538 | return -EINVAL; |
1539 | ||
87f0e418 LP |
1540 | if (!unit_can_reload(u)) |
1541 | return -EBADR; | |
1542 | ||
1543 | state = unit_active_state(u); | |
e364ad06 | 1544 | if (state == UNIT_RELOADING) |
87f0e418 LP |
1545 | return -EALREADY; |
1546 | ||
6a371e23 | 1547 | if (state != UNIT_ACTIVE) { |
f2341e0a | 1548 | log_unit_warning(u, "Unit cannot be reloaded because it is inactive."); |
87f0e418 | 1549 | return -ENOEXEC; |
6a371e23 | 1550 | } |
87f0e418 | 1551 | |
e48614c4 ZJS |
1552 | following = unit_following(u); |
1553 | if (following) { | |
f2341e0a | 1554 | log_unit_debug(u, "Redirecting reload request from %s to %s.", u->id, following->id); |
92ab323c LP |
1555 | return unit_reload(following); |
1556 | } | |
1557 | ||
c1e1601e | 1558 | unit_add_to_dbus_queue(u); |
82a2b6bb | 1559 | |
d1a34ae9 | 1560 | return UNIT_VTABLE(u)->reload(u); |
87f0e418 LP |
1561 | } |
1562 | ||
1563 | bool unit_can_reload(Unit *u) { | |
1564 | assert(u); | |
1565 | ||
1566 | if (!UNIT_VTABLE(u)->reload) | |
1567 | return false; | |
1568 | ||
1569 | if (!UNIT_VTABLE(u)->can_reload) | |
1570 | return true; | |
1571 | ||
1572 | return UNIT_VTABLE(u)->can_reload(u); | |
1573 | } | |
1574 | ||
b4a16b7b | 1575 | static void unit_check_unneeded(Unit *u) { |
be7d9ff7 LP |
1576 | |
1577 | static const UnitDependency needed_dependencies[] = { | |
1578 | UNIT_REQUIRED_BY, | |
1579 | UNIT_REQUIRED_BY_OVERRIDABLE, | |
084918ba | 1580 | UNIT_REQUISITE_OF, |
be7d9ff7 LP |
1581 | UNIT_REQUISITE_OF_OVERRIDABLE, |
1582 | UNIT_WANTED_BY, | |
1583 | UNIT_BOUND_BY, | |
1584 | }; | |
1585 | ||
f3bff0eb | 1586 | Unit *other; |
be7d9ff7 LP |
1587 | Iterator i; |
1588 | unsigned j; | |
1589 | int r; | |
f3bff0eb LP |
1590 | |
1591 | assert(u); | |
1592 | ||
1593 | /* If this service shall be shut down when unneeded then do | |
1594 | * so. */ | |
1595 | ||
ac155bb8 | 1596 | if (!u->stop_when_unneeded) |
f3bff0eb LP |
1597 | return; |
1598 | ||
1599 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) | |
1600 | return; | |
1601 | ||
be7d9ff7 | 1602 | for (j = 0; j < ELEMENTSOF(needed_dependencies); j++) |
f3b85044 | 1603 | SET_FOREACH(other, u->dependencies[needed_dependencies[j]], i) |
be7d9ff7 LP |
1604 | if (unit_active_or_pending(other)) |
1605 | return; | |
b81884e7 | 1606 | |
bea355da LP |
1607 | /* If stopping a unit fails continously we might enter a stop |
1608 | * loop here, hence stop acting on the service being | |
1609 | * unnecessary after a while. */ | |
67bfdc97 | 1610 | if (!ratelimit_test(&u->auto_stop_ratelimit)) { |
bea355da LP |
1611 | log_unit_warning(u, "Unit not needed anymore, but not stopping since we tried this too often recently."); |
1612 | return; | |
1613 | } | |
1614 | ||
f2341e0a | 1615 | log_unit_info(u, "Unit not needed anymore. Stopping."); |
f3bff0eb LP |
1616 | |
1617 | /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */ | |
be7d9ff7 LP |
1618 | r = manager_add_job(u->manager, JOB_STOP, u, JOB_FAIL, true, NULL, NULL); |
1619 | if (r < 0) | |
1620 | log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %m"); | |
f3bff0eb LP |
1621 | } |
1622 | ||
ff502445 LP |
1623 | static void unit_check_binds_to(Unit *u) { |
1624 | bool stop = false; | |
1625 | Unit *other; | |
1626 | Iterator i; | |
67bfdc97 | 1627 | int r; |
ff502445 LP |
1628 | |
1629 | assert(u); | |
1630 | ||
1631 | if (u->job) | |
1632 | return; | |
1633 | ||
1634 | if (unit_active_state(u) != UNIT_ACTIVE) | |
1635 | return; | |
1636 | ||
1637 | SET_FOREACH(other, u->dependencies[UNIT_BINDS_TO], i) { | |
1638 | if (other->job) | |
1639 | continue; | |
1640 | ||
1641 | if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) | |
1642 | continue; | |
1643 | ||
1644 | stop = true; | |
98f738b6 | 1645 | break; |
ff502445 LP |
1646 | } |
1647 | ||
1648 | if (!stop) | |
1649 | return; | |
1650 | ||
67bfdc97 LP |
1651 | /* If stopping a unit fails continously we might enter a stop |
1652 | * loop here, hence stop acting on the service being | |
1653 | * unnecessary after a while. */ | |
1654 | if (!ratelimit_test(&u->auto_stop_ratelimit)) { | |
1655 | log_unit_warning(u, "Unit is bound to inactive unit %s, but not stopping since we tried this too often recently.", other->id); | |
1656 | return; | |
1657 | } | |
1658 | ||
98f738b6 | 1659 | assert(other); |
f2341e0a | 1660 | log_unit_info(u, "Unit is bound to inactive unit %s. Stopping, too.", other->id); |
ff502445 LP |
1661 | |
1662 | /* A unit we need to run is gone. Sniff. Let's stop this. */ | |
67bfdc97 LP |
1663 | r = manager_add_job(u->manager, JOB_STOP, u, JOB_FAIL, true, NULL, NULL); |
1664 | if (r < 0) | |
1665 | log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %m"); | |
ff502445 LP |
1666 | } |
1667 | ||
87f0e418 LP |
1668 | static void retroactively_start_dependencies(Unit *u) { |
1669 | Iterator i; | |
1670 | Unit *other; | |
1671 | ||
1672 | assert(u); | |
1673 | assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))); | |
1674 | ||
ac155bb8 MS |
1675 | SET_FOREACH(other, u->dependencies[UNIT_REQUIRES], i) |
1676 | if (!set_get(u->dependencies[UNIT_AFTER], other) && | |
b81884e7 | 1677 | !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) |
ac155bb8 | 1678 | manager_add_job(u->manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL); |
b81884e7 | 1679 | |
7f2cddae | 1680 | SET_FOREACH(other, u->dependencies[UNIT_BINDS_TO], i) |
ac155bb8 | 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); |
87f0e418 | 1684 | |
ac155bb8 MS |
1685 | SET_FOREACH(other, u->dependencies[UNIT_REQUIRES_OVERRIDABLE], i) |
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_FAIL, false, NULL, NULL); |
87f0e418 | 1689 | |
ac155bb8 MS |
1690 | SET_FOREACH(other, u->dependencies[UNIT_WANTS], 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 | 1695 | SET_FOREACH(other, u->dependencies[UNIT_CONFLICTS], i) |
b81884e7 | 1696 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
ac155bb8 | 1697 | manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL); |
69dd2852 | 1698 | |
ac155bb8 | 1699 | SET_FOREACH(other, u->dependencies[UNIT_CONFLICTED_BY], i) |
b81884e7 | 1700 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
ac155bb8 | 1701 | manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL); |
87f0e418 LP |
1702 | } |
1703 | ||
1704 | static void retroactively_stop_dependencies(Unit *u) { | |
1705 | Iterator i; | |
1706 | Unit *other; | |
1707 | ||
1708 | assert(u); | |
1709 | assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))); | |
1710 | ||
b81884e7 | 1711 | /* Pull down units which are bound to us recursively if enabled */ |
ac155bb8 | 1712 | SET_FOREACH(other, u->dependencies[UNIT_BOUND_BY], i) |
b81884e7 | 1713 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
ac155bb8 | 1714 | manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL); |
cd0504d0 MS |
1715 | } |
1716 | ||
1717 | static void check_unneeded_dependencies(Unit *u) { | |
1718 | Iterator i; | |
1719 | Unit *other; | |
1720 | ||
1721 | assert(u); | |
1722 | assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))); | |
f3bff0eb LP |
1723 | |
1724 | /* Garbage collect services that might not be needed anymore, if enabled */ | |
ac155bb8 | 1725 | SET_FOREACH(other, u->dependencies[UNIT_REQUIRES], i) |
87f0e418 | 1726 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
b4a16b7b | 1727 | unit_check_unneeded(other); |
ac155bb8 | 1728 | SET_FOREACH(other, u->dependencies[UNIT_REQUIRES_OVERRIDABLE], i) |
f3bff0eb | 1729 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
b4a16b7b | 1730 | unit_check_unneeded(other); |
ac155bb8 | 1731 | SET_FOREACH(other, u->dependencies[UNIT_WANTS], i) |
f3bff0eb | 1732 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
b4a16b7b | 1733 | unit_check_unneeded(other); |
ac155bb8 | 1734 | SET_FOREACH(other, u->dependencies[UNIT_REQUISITE], i) |
f3bff0eb | 1735 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
b4a16b7b | 1736 | unit_check_unneeded(other); |
ac155bb8 | 1737 | SET_FOREACH(other, u->dependencies[UNIT_REQUISITE_OVERRIDABLE], i) |
f3bff0eb | 1738 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
b4a16b7b | 1739 | unit_check_unneeded(other); |
7f2cddae | 1740 | SET_FOREACH(other, u->dependencies[UNIT_BINDS_TO], i) |
b81884e7 LP |
1741 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
1742 | unit_check_unneeded(other); | |
87f0e418 LP |
1743 | } |
1744 | ||
3ecaa09b | 1745 | void unit_start_on_failure(Unit *u) { |
c0daa706 LP |
1746 | Unit *other; |
1747 | Iterator i; | |
1748 | ||
1749 | assert(u); | |
1750 | ||
ac155bb8 | 1751 | if (set_size(u->dependencies[UNIT_ON_FAILURE]) <= 0) |
222ae6a8 LP |
1752 | return; |
1753 | ||
f2341e0a | 1754 | log_unit_info(u, "Triggering OnFailure= dependencies."); |
222ae6a8 | 1755 | |
ac155bb8 | 1756 | SET_FOREACH(other, u->dependencies[UNIT_ON_FAILURE], i) { |
222ae6a8 LP |
1757 | int r; |
1758 | ||
d420282b | 1759 | r = manager_add_job(u->manager, JOB_START, other, u->on_failure_job_mode, true, NULL, NULL); |
c1b6628d | 1760 | if (r < 0) |
f2341e0a | 1761 | log_unit_error_errno(u, r, "Failed to enqueue OnFailure= job: %m"); |
222ae6a8 | 1762 | } |
c0daa706 LP |
1763 | } |
1764 | ||
3ecaa09b LP |
1765 | void unit_trigger_notify(Unit *u) { |
1766 | Unit *other; | |
1767 | Iterator i; | |
1768 | ||
1769 | assert(u); | |
1770 | ||
1771 | SET_FOREACH(other, u->dependencies[UNIT_TRIGGERED_BY], i) | |
1772 | if (UNIT_VTABLE(other)->trigger_notify) | |
1773 | UNIT_VTABLE(other)->trigger_notify(other, u); | |
1774 | } | |
1775 | ||
e2f3b44c | 1776 | void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_success) { |
546ac4f0 | 1777 | Manager *m; |
7e6e7b06 | 1778 | bool unexpected; |
a096ed36 | 1779 | |
87f0e418 LP |
1780 | assert(u); |
1781 | assert(os < _UNIT_ACTIVE_STATE_MAX); | |
1782 | assert(ns < _UNIT_ACTIVE_STATE_MAX); | |
87f0e418 | 1783 | |
8e471ccd LP |
1784 | /* Note that this is called for all low-level state changes, |
1785 | * even if they might map to the same high-level | |
865cc19a | 1786 | * UnitActiveState! That means that ns == os is an expected |
c5315881 | 1787 | * behavior here. For example: if a mount point is remounted |
cd6d0a45 | 1788 | * this function will be called too! */ |
87f0e418 | 1789 | |
546ac4f0 MS |
1790 | m = u->manager; |
1791 | ||
f755e3b7 | 1792 | /* Update timestamps for state changes */ |
546ac4f0 | 1793 | if (m->n_reloading <= 0) { |
bdbf9951 | 1794 | dual_timestamp ts; |
173e3821 | 1795 | |
bdbf9951 | 1796 | dual_timestamp_get(&ts); |
173e3821 | 1797 | |
bdbf9951 | 1798 | if (UNIT_IS_INACTIVE_OR_FAILED(os) && !UNIT_IS_INACTIVE_OR_FAILED(ns)) |
ac155bb8 | 1799 | u->inactive_exit_timestamp = ts; |
bdbf9951 | 1800 | else if (!UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_INACTIVE_OR_FAILED(ns)) |
ac155bb8 | 1801 | u->inactive_enter_timestamp = ts; |
bdbf9951 LP |
1802 | |
1803 | if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns)) | |
ac155bb8 | 1804 | u->active_enter_timestamp = ts; |
bdbf9951 | 1805 | else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns)) |
ac155bb8 | 1806 | u->active_exit_timestamp = ts; |
bdbf9951 | 1807 | } |
87f0e418 | 1808 | |
865cc19a | 1809 | /* Keep track of failed units */ |
03455c28 | 1810 | manager_update_failed_units(u->manager, u, ns == UNIT_FAILED); |
f755e3b7 LP |
1811 | |
1812 | /* Make sure the cgroup is always removed when we become inactive */ | |
fdf20a31 | 1813 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) |
efdb0237 | 1814 | unit_prune_cgroup(u); |
fb385181 | 1815 | |
9cd86184 | 1816 | /* Note that this doesn't apply to RemainAfterExit services exiting |
6f285378 | 1817 | * successfully, since there's no change of state in that case. Which is |
9cd86184 | 1818 | * why it is handled in service_set_state() */ |
7ed9f6cd | 1819 | if (UNIT_IS_INACTIVE_OR_FAILED(os) != UNIT_IS_INACTIVE_OR_FAILED(ns)) { |
b33918c2 LP |
1820 | ExecContext *ec; |
1821 | ||
1822 | ec = unit_get_exec_context(u); | |
7ed9f6cd | 1823 | if (ec && exec_context_may_touch_console(ec)) { |
31a7eb86 ZJS |
1824 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) { |
1825 | m->n_on_console --; | |
1826 | ||
1827 | if (m->n_on_console == 0) | |
1828 | /* unset no_console_output flag, since the console is free */ | |
2f38577f | 1829 | m->no_console_output = false; |
31a7eb86 ZJS |
1830 | } else |
1831 | m->n_on_console ++; | |
7ed9f6cd MS |
1832 | } |
1833 | } | |
1834 | ||
ac155bb8 | 1835 | if (u->job) { |
7e6e7b06 | 1836 | unexpected = false; |
87f0e418 | 1837 | |
ac155bb8 | 1838 | if (u->job->state == JOB_WAITING) |
87f0e418 LP |
1839 | |
1840 | /* So we reached a different state for this | |
1841 | * job. Let's see if we can run it now if it | |
1842 | * failed previously due to EAGAIN. */ | |
ac155bb8 | 1843 | job_add_to_run_queue(u->job); |
87f0e418 | 1844 | |
b410e6b9 | 1845 | /* Let's check whether this state change constitutes a |
35b8ca3a | 1846 | * finished job, or maybe contradicts a running job and |
b410e6b9 | 1847 | * hence needs to invalidate jobs. */ |
87f0e418 | 1848 | |
ac155bb8 | 1849 | switch (u->job->type) { |
87f0e418 | 1850 | |
b410e6b9 LP |
1851 | case JOB_START: |
1852 | case JOB_VERIFY_ACTIVE: | |
87f0e418 | 1853 | |
b410e6b9 | 1854 | if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) |
5273510e | 1855 | job_finish_and_invalidate(u->job, JOB_DONE, true); |
ac155bb8 | 1856 | else if (u->job->state == JOB_RUNNING && ns != UNIT_ACTIVATING) { |
b410e6b9 | 1857 | unexpected = true; |
41b02ec7 | 1858 | |
fdf20a31 | 1859 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) |
5273510e | 1860 | job_finish_and_invalidate(u->job, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE, true); |
b410e6b9 | 1861 | } |
87f0e418 | 1862 | |
b410e6b9 | 1863 | break; |
87f0e418 | 1864 | |
b410e6b9 LP |
1865 | case JOB_RELOAD: |
1866 | case JOB_RELOAD_OR_START: | |
87f0e418 | 1867 | |
ac155bb8 | 1868 | if (u->job->state == JOB_RUNNING) { |
a096ed36 | 1869 | if (ns == UNIT_ACTIVE) |
5273510e | 1870 | job_finish_and_invalidate(u->job, reload_success ? JOB_DONE : JOB_FAILED, true); |
032ff4af | 1871 | else if (ns != UNIT_ACTIVATING && ns != UNIT_RELOADING) { |
a096ed36 | 1872 | unexpected = true; |
41b02ec7 | 1873 | |
fdf20a31 | 1874 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) |
5273510e | 1875 | job_finish_and_invalidate(u->job, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE, true); |
a096ed36 | 1876 | } |
b410e6b9 | 1877 | } |
87f0e418 | 1878 | |
b410e6b9 | 1879 | break; |
87f0e418 | 1880 | |
b410e6b9 LP |
1881 | case JOB_STOP: |
1882 | case JOB_RESTART: | |
1883 | case JOB_TRY_RESTART: | |
87f0e418 | 1884 | |
fdf20a31 | 1885 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) |
5273510e | 1886 | job_finish_and_invalidate(u->job, JOB_DONE, true); |
ac155bb8 | 1887 | else if (u->job->state == JOB_RUNNING && ns != UNIT_DEACTIVATING) { |
b410e6b9 | 1888 | unexpected = true; |
5273510e | 1889 | job_finish_and_invalidate(u->job, JOB_FAILED, true); |
b410e6b9 | 1890 | } |
87f0e418 | 1891 | |
b410e6b9 | 1892 | break; |
87f0e418 | 1893 | |
b410e6b9 LP |
1894 | default: |
1895 | assert_not_reached("Job type unknown"); | |
87f0e418 | 1896 | } |
87f0e418 | 1897 | |
7e6e7b06 LP |
1898 | } else |
1899 | unexpected = true; | |
1900 | ||
546ac4f0 | 1901 | if (m->n_reloading <= 0) { |
f3bff0eb | 1902 | |
bdbf9951 LP |
1903 | /* If this state change happened without being |
1904 | * requested by a job, then let's retroactively start | |
1905 | * or stop dependencies. We skip that step when | |
1906 | * deserializing, since we don't want to create any | |
1907 | * additional jobs just because something is already | |
1908 | * activated. */ | |
1909 | ||
1910 | if (unexpected) { | |
1911 | if (UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns)) | |
1912 | retroactively_start_dependencies(u); | |
1913 | else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns)) | |
1914 | retroactively_stop_dependencies(u); | |
1915 | } | |
5de9682c | 1916 | |
cd0504d0 | 1917 | /* stop unneeded units regardless if going down was expected or not */ |
b33918c2 | 1918 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(ns)) |
cd0504d0 MS |
1919 | check_unneeded_dependencies(u); |
1920 | ||
bdbf9951 | 1921 | if (ns != os && ns == UNIT_FAILED) { |
f2341e0a | 1922 | log_unit_notice(u, "Unit entered failed state."); |
3ecaa09b | 1923 | unit_start_on_failure(u); |
cd6d0a45 | 1924 | } |
3b2775c5 | 1925 | } |
e537352b | 1926 | |
3b2775c5 LP |
1927 | /* Some names are special */ |
1928 | if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) { | |
1929 | ||
1930 | if (unit_has_name(u, SPECIAL_DBUS_SERVICE)) | |
865cc19a | 1931 | /* The bus might have just become available, |
3b2775c5 LP |
1932 | * hence try to connect to it, if we aren't |
1933 | * yet connected. */ | |
546ac4f0 | 1934 | bus_init(m, true); |
3b2775c5 | 1935 | |
ac155bb8 | 1936 | if (u->type == UNIT_SERVICE && |
3b2775c5 | 1937 | !UNIT_IS_ACTIVE_OR_RELOADING(os) && |
546ac4f0 | 1938 | m->n_reloading <= 0) { |
3b2775c5 | 1939 | /* Write audit record if we have just finished starting up */ |
546ac4f0 | 1940 | manager_send_unit_audit(m, u, AUDIT_SERVICE_START, true); |
ac155bb8 | 1941 | u->in_audit = true; |
3b2775c5 | 1942 | } |
e983b760 | 1943 | |
3b2775c5 | 1944 | if (!UNIT_IS_ACTIVE_OR_RELOADING(os)) |
546ac4f0 | 1945 | manager_send_unit_plymouth(m, u); |
bdbf9951 | 1946 | |
3b2775c5 | 1947 | } else { |
bdbf9951 | 1948 | |
3b2775c5 LP |
1949 | /* We don't care about D-Bus here, since we'll get an |
1950 | * asynchronous notification for it anyway. */ | |
cd6d0a45 | 1951 | |
ac155bb8 | 1952 | if (u->type == UNIT_SERVICE && |
3b2775c5 LP |
1953 | UNIT_IS_INACTIVE_OR_FAILED(ns) && |
1954 | !UNIT_IS_INACTIVE_OR_FAILED(os) && | |
546ac4f0 | 1955 | m->n_reloading <= 0) { |
4927fcae | 1956 | |
3b2775c5 LP |
1957 | /* Hmm, if there was no start record written |
1958 | * write it now, so that we always have a nice | |
1959 | * pair */ | |
ac155bb8 | 1960 | if (!u->in_audit) { |
546ac4f0 | 1961 | manager_send_unit_audit(m, u, AUDIT_SERVICE_START, ns == UNIT_INACTIVE); |
cd6d0a45 | 1962 | |
3b2775c5 | 1963 | if (ns == UNIT_INACTIVE) |
546ac4f0 | 1964 | manager_send_unit_audit(m, u, AUDIT_SERVICE_STOP, true); |
3b2775c5 LP |
1965 | } else |
1966 | /* Write audit record if we have just finished shutting down */ | |
546ac4f0 | 1967 | manager_send_unit_audit(m, u, AUDIT_SERVICE_STOP, ns == UNIT_INACTIVE); |
bdbf9951 | 1968 | |
ac155bb8 | 1969 | u->in_audit = false; |
cd6d0a45 | 1970 | } |
f278026d LP |
1971 | } |
1972 | ||
546ac4f0 | 1973 | manager_recheck_journal(m); |
3ecaa09b | 1974 | unit_trigger_notify(u); |
3b2775c5 | 1975 | |
ff502445 LP |
1976 | if (u->manager->n_reloading <= 0) { |
1977 | /* Maybe we finished startup and are now ready for | |
1978 | * being stopped because unneeded? */ | |
bf6dcfa6 | 1979 | unit_check_unneeded(u); |
c1e1601e | 1980 | |
ff502445 LP |
1981 | /* Maybe we finished startup, but something we needed |
1982 | * has vanished? Let's die then. (This happens when | |
1983 | * something BindsTo= to a Type=oneshot unit, as these | |
1984 | * units go directly from starting to inactive, | |
1985 | * without ever entering started.) */ | |
1986 | unit_check_binds_to(u); | |
1987 | } | |
1988 | ||
c1e1601e | 1989 | unit_add_to_dbus_queue(u); |
701cc384 | 1990 | unit_add_to_gc_queue(u); |
87f0e418 LP |
1991 | } |
1992 | ||
87f0e418 | 1993 | int unit_watch_pid(Unit *u, pid_t pid) { |
a911bb9a LP |
1994 | int q, r; |
1995 | ||
87f0e418 LP |
1996 | assert(u); |
1997 | assert(pid >= 1); | |
1998 | ||
5ba6985b LP |
1999 | /* Watch a specific PID. We only support one or two units |
2000 | * watching each PID for now, not more. */ | |
2001 | ||
d5099efc | 2002 | r = set_ensure_allocated(&u->pids, NULL); |
5ba6985b LP |
2003 | if (r < 0) |
2004 | return r; | |
2005 | ||
d5099efc | 2006 | r = hashmap_ensure_allocated(&u->manager->watch_pids1, NULL); |
a911bb9a LP |
2007 | if (r < 0) |
2008 | return r; | |
2009 | ||
fea72cc0 | 2010 | r = hashmap_put(u->manager->watch_pids1, PID_TO_PTR(pid), u); |
5ba6985b | 2011 | if (r == -EEXIST) { |
d5099efc | 2012 | r = hashmap_ensure_allocated(&u->manager->watch_pids2, NULL); |
5ba6985b LP |
2013 | if (r < 0) |
2014 | return r; | |
05e343b7 | 2015 | |
fea72cc0 | 2016 | r = hashmap_put(u->manager->watch_pids2, PID_TO_PTR(pid), u); |
5ba6985b | 2017 | } |
a911bb9a | 2018 | |
fea72cc0 | 2019 | q = set_put(u->pids, PID_TO_PTR(pid)); |
a911bb9a LP |
2020 | if (q < 0) |
2021 | return q; | |
2022 | ||
2023 | return r; | |
87f0e418 LP |
2024 | } |
2025 | ||
2026 | void unit_unwatch_pid(Unit *u, pid_t pid) { | |
2027 | assert(u); | |
2028 | assert(pid >= 1); | |
2029 | ||
fea72cc0 LP |
2030 | (void) hashmap_remove_value(u->manager->watch_pids1, PID_TO_PTR(pid), u); |
2031 | (void) hashmap_remove_value(u->manager->watch_pids2, PID_TO_PTR(pid), u); | |
2032 | (void) set_remove(u->pids, PID_TO_PTR(pid)); | |
a911bb9a LP |
2033 | } |
2034 | ||
bd44e61b LP |
2035 | void unit_unwatch_all_pids(Unit *u) { |
2036 | assert(u); | |
2037 | ||
2038 | while (!set_isempty(u->pids)) | |
fea72cc0 | 2039 | unit_unwatch_pid(u, PTR_TO_PID(set_first(u->pids))); |
bd44e61b | 2040 | |
efdb0237 | 2041 | u->pids = set_free(u->pids); |
a911bb9a LP |
2042 | } |
2043 | ||
2044 | void unit_tidy_watch_pids(Unit *u, pid_t except1, pid_t except2) { | |
2045 | Iterator i; | |
2046 | void *e; | |
2047 | ||
2048 | assert(u); | |
2049 | ||
2050 | /* Cleans dead PIDs from our list */ | |
2051 | ||
2052 | SET_FOREACH(e, u->pids, i) { | |
fea72cc0 | 2053 | pid_t pid = PTR_TO_PID(e); |
a911bb9a LP |
2054 | |
2055 | if (pid == except1 || pid == except2) | |
2056 | continue; | |
2057 | ||
9f5650ae | 2058 | if (!pid_is_unwaited(pid)) |
bd44e61b | 2059 | unit_unwatch_pid(u, pid); |
a911bb9a | 2060 | } |
87f0e418 LP |
2061 | } |
2062 | ||
87f0e418 LP |
2063 | bool unit_job_is_applicable(Unit *u, JobType j) { |
2064 | assert(u); | |
2065 | assert(j >= 0 && j < _JOB_TYPE_MAX); | |
2066 | ||
2067 | switch (j) { | |
2068 | ||
2069 | case JOB_VERIFY_ACTIVE: | |
2070 | case JOB_START: | |
57339f47 | 2071 | case JOB_STOP: |
e0209d83 | 2072 | case JOB_NOP: |
87f0e418 LP |
2073 | return true; |
2074 | ||
87f0e418 LP |
2075 | case JOB_RESTART: |
2076 | case JOB_TRY_RESTART: | |
2077 | return unit_can_start(u); | |
2078 | ||
2079 | case JOB_RELOAD: | |
2080 | return unit_can_reload(u); | |
2081 | ||
2082 | case JOB_RELOAD_OR_START: | |
2083 | return unit_can_reload(u) && unit_can_start(u); | |
2084 | ||
2085 | default: | |
2086 | assert_not_reached("Invalid job type"); | |
2087 | } | |
2088 | } | |
2089 | ||
f2341e0a LP |
2090 | static void maybe_warn_about_dependency(Unit *u, const char *other, UnitDependency dependency) { |
2091 | assert(u); | |
d1fab3fe | 2092 | |
f2341e0a LP |
2093 | /* Only warn about some unit types */ |
2094 | if (!IN_SET(dependency, UNIT_CONFLICTS, UNIT_CONFLICTED_BY, UNIT_BEFORE, UNIT_AFTER, UNIT_ON_FAILURE, UNIT_TRIGGERS, UNIT_TRIGGERED_BY)) | |
2095 | return; | |
3f3cc397 | 2096 | |
f2341e0a LP |
2097 | if (streq_ptr(u->id, other)) |
2098 | log_unit_warning(u, "Dependency %s=%s dropped", unit_dependency_to_string(dependency), u->id); | |
2099 | else | |
2100 | log_unit_warning(u, "Dependency %s=%s dropped, merged into %s", unit_dependency_to_string(dependency), strna(other), u->id); | |
d1fab3fe ZJS |
2101 | } |
2102 | ||
701cc384 | 2103 | int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference) { |
87f0e418 LP |
2104 | |
2105 | static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = { | |
2106 | [UNIT_REQUIRES] = UNIT_REQUIRED_BY, | |
9e2f7c11 | 2107 | [UNIT_REQUIRES_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE, |
87f0e418 | 2108 | [UNIT_WANTS] = UNIT_WANTED_BY, |
be7d9ff7 LP |
2109 | [UNIT_REQUISITE] = UNIT_REQUISITE_OF, |
2110 | [UNIT_REQUISITE_OVERRIDABLE] = UNIT_REQUISITE_OF_OVERRIDABLE, | |
7f2cddae | 2111 | [UNIT_BINDS_TO] = UNIT_BOUND_BY, |
60649f17 | 2112 | [UNIT_PART_OF] = UNIT_CONSISTS_OF, |
be7d9ff7 LP |
2113 | [UNIT_REQUIRED_BY] = UNIT_REQUIRES, |
2114 | [UNIT_REQUIRED_BY_OVERRIDABLE] = UNIT_REQUIRES_OVERRIDABLE, | |
2115 | [UNIT_REQUISITE_OF] = UNIT_REQUISITE, | |
2116 | [UNIT_REQUISITE_OF_OVERRIDABLE] = UNIT_REQUISITE_OVERRIDABLE, | |
2117 | [UNIT_WANTED_BY] = UNIT_WANTS, | |
7f2cddae | 2118 | [UNIT_BOUND_BY] = UNIT_BINDS_TO, |
60649f17 | 2119 | [UNIT_CONSISTS_OF] = UNIT_PART_OF, |
69dd2852 LP |
2120 | [UNIT_CONFLICTS] = UNIT_CONFLICTED_BY, |
2121 | [UNIT_CONFLICTED_BY] = UNIT_CONFLICTS, | |
87f0e418 | 2122 | [UNIT_BEFORE] = UNIT_AFTER, |
701cc384 | 2123 | [UNIT_AFTER] = UNIT_BEFORE, |
5de9682c | 2124 | [UNIT_ON_FAILURE] = _UNIT_DEPENDENCY_INVALID, |
701cc384 | 2125 | [UNIT_REFERENCES] = UNIT_REFERENCED_BY, |
57020a3a LP |
2126 | [UNIT_REFERENCED_BY] = UNIT_REFERENCES, |
2127 | [UNIT_TRIGGERS] = UNIT_TRIGGERED_BY, | |
4dcc1cb4 | 2128 | [UNIT_TRIGGERED_BY] = UNIT_TRIGGERS, |
7f2cddae | 2129 | [UNIT_PROPAGATES_RELOAD_TO] = UNIT_RELOAD_PROPAGATED_FROM, |
85e9a101 | 2130 | [UNIT_RELOAD_PROPAGATED_FROM] = UNIT_PROPAGATES_RELOAD_TO, |
613b411c | 2131 | [UNIT_JOINS_NAMESPACE_OF] = UNIT_JOINS_NAMESPACE_OF, |
87f0e418 | 2132 | }; |
701cc384 | 2133 | int r, q = 0, v = 0, w = 0; |
d1fab3fe | 2134 | Unit *orig_u = u, *orig_other = other; |
87f0e418 LP |
2135 | |
2136 | assert(u); | |
2137 | assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX); | |
87f0e418 LP |
2138 | assert(other); |
2139 | ||
9f151f29 LP |
2140 | u = unit_follow_merge(u); |
2141 | other = unit_follow_merge(other); | |
2142 | ||
87f0e418 LP |
2143 | /* We won't allow dependencies on ourselves. We will not |
2144 | * consider them an error however. */ | |
d1fab3fe | 2145 | if (u == other) { |
f2341e0a | 2146 | maybe_warn_about_dependency(orig_u, orig_other->id, d); |
87f0e418 | 2147 | return 0; |
d1fab3fe | 2148 | } |
87f0e418 | 2149 | |
d5099efc | 2150 | r = set_ensure_allocated(&u->dependencies[d], NULL); |
613b411c | 2151 | if (r < 0) |
87f0e418 LP |
2152 | return r; |
2153 | ||
613b411c | 2154 | if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID) { |
d5099efc | 2155 | r = set_ensure_allocated(&other->dependencies[inverse_table[d]], NULL); |
613b411c LP |
2156 | if (r < 0) |
2157 | return r; | |
2158 | } | |
2159 | ||
2160 | if (add_reference) { | |
d5099efc | 2161 | r = set_ensure_allocated(&u->dependencies[UNIT_REFERENCES], NULL); |
613b411c | 2162 | if (r < 0) |
5de9682c LP |
2163 | return r; |
2164 | ||
d5099efc | 2165 | r = set_ensure_allocated(&other->dependencies[UNIT_REFERENCED_BY], NULL); |
613b411c | 2166 | if (r < 0) |
701cc384 | 2167 | return r; |
613b411c | 2168 | } |
87f0e418 | 2169 | |
613b411c LP |
2170 | q = set_put(u->dependencies[d], other); |
2171 | if (q < 0) | |
701cc384 | 2172 | return q; |
87f0e418 | 2173 | |
613b411c LP |
2174 | if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID && inverse_table[d] != d) { |
2175 | v = set_put(other->dependencies[inverse_table[d]], u); | |
2176 | if (v < 0) { | |
5de9682c LP |
2177 | r = v; |
2178 | goto fail; | |
2179 | } | |
613b411c | 2180 | } |
701cc384 LP |
2181 | |
2182 | if (add_reference) { | |
613b411c LP |
2183 | w = set_put(u->dependencies[UNIT_REFERENCES], other); |
2184 | if (w < 0) { | |
701cc384 LP |
2185 | r = w; |
2186 | goto fail; | |
2187 | } | |
2188 | ||
613b411c LP |
2189 | r = set_put(other->dependencies[UNIT_REFERENCED_BY], u); |
2190 | if (r < 0) | |
701cc384 | 2191 | goto fail; |
87f0e418 LP |
2192 | } |
2193 | ||
c1e1601e | 2194 | unit_add_to_dbus_queue(u); |
87f0e418 | 2195 | return 0; |
701cc384 LP |
2196 | |
2197 | fail: | |
2198 | if (q > 0) | |
ac155bb8 | 2199 | set_remove(u->dependencies[d], other); |
701cc384 LP |
2200 | |
2201 | if (v > 0) | |
ac155bb8 | 2202 | set_remove(other->dependencies[inverse_table[d]], u); |
701cc384 LP |
2203 | |
2204 | if (w > 0) | |
ac155bb8 | 2205 | set_remove(u->dependencies[UNIT_REFERENCES], other); |
701cc384 LP |
2206 | |
2207 | return r; | |
87f0e418 | 2208 | } |
0301abf4 | 2209 | |
2c966c03 LP |
2210 | int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference) { |
2211 | int r; | |
2212 | ||
2213 | assert(u); | |
2214 | ||
3f3cc397 LP |
2215 | r = unit_add_dependency(u, d, other, add_reference); |
2216 | if (r < 0) | |
2c966c03 LP |
2217 | return r; |
2218 | ||
7410616c | 2219 | return unit_add_dependency(u, e, other, add_reference); |
2c966c03 LP |
2220 | } |
2221 | ||
7410616c LP |
2222 | static int resolve_template(Unit *u, const char *name, const char*path, char **buf, const char **ret) { |
2223 | int r; | |
9e2f7c11 LP |
2224 | |
2225 | assert(u); | |
2226 | assert(name || path); | |
7410616c LP |
2227 | assert(buf); |
2228 | assert(ret); | |
9e2f7c11 LP |
2229 | |
2230 | if (!name) | |
2b6bf07d | 2231 | name = basename(path); |
9e2f7c11 | 2232 | |
7410616c LP |
2233 | if (!unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
2234 | *buf = NULL; | |
2235 | *ret = name; | |
2236 | return 0; | |
9e2f7c11 LP |
2237 | } |
2238 | ||
ac155bb8 | 2239 | if (u->instance) |
7410616c | 2240 | r = unit_name_replace_instance(name, u->instance, buf); |
9e2f7c11 | 2241 | else { |
ae018d9b | 2242 | _cleanup_free_ char *i = NULL; |
9e2f7c11 | 2243 | |
7410616c LP |
2244 | r = unit_name_to_prefix(u->id, &i); |
2245 | if (r < 0) | |
2246 | return r; | |
9e2f7c11 | 2247 | |
7410616c | 2248 | r = unit_name_replace_instance(name, i, buf); |
9e2f7c11 | 2249 | } |
7410616c LP |
2250 | if (r < 0) |
2251 | return r; | |
9e2f7c11 | 2252 | |
7410616c LP |
2253 | *ret = *buf; |
2254 | return 0; | |
9e2f7c11 LP |
2255 | } |
2256 | ||
701cc384 | 2257 | int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) { |
7410616c | 2258 | _cleanup_free_ char *buf = NULL; |
09b6b09f LP |
2259 | Unit *other; |
2260 | int r; | |
2261 | ||
9e2f7c11 LP |
2262 | assert(u); |
2263 | assert(name || path); | |
09b6b09f | 2264 | |
7410616c LP |
2265 | r = resolve_template(u, name, path, &buf, &name); |
2266 | if (r < 0) | |
2267 | return r; | |
09b6b09f | 2268 | |
8afbb8e1 LP |
2269 | r = manager_load_unit(u->manager, name, path, NULL, &other); |
2270 | if (r < 0) | |
2271 | return r; | |
9e2f7c11 | 2272 | |
8afbb8e1 | 2273 | return unit_add_dependency(u, d, other, add_reference); |
09b6b09f LP |
2274 | } |
2275 | ||
2c966c03 | 2276 | int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) { |
7410616c | 2277 | _cleanup_free_ char *buf = NULL; |
2c966c03 LP |
2278 | Unit *other; |
2279 | int r; | |
2c966c03 LP |
2280 | |
2281 | assert(u); | |
2282 | assert(name || path); | |
2283 | ||
7410616c LP |
2284 | r = resolve_template(u, name, path, &buf, &name); |
2285 | if (r < 0) | |
2286 | return r; | |
2c966c03 | 2287 | |
3f3cc397 LP |
2288 | r = manager_load_unit(u->manager, name, path, NULL, &other); |
2289 | if (r < 0) | |
68eda4bd | 2290 | return r; |
2c966c03 | 2291 | |
3f3cc397 | 2292 | return unit_add_two_dependencies(u, d, e, other, add_reference); |
2c966c03 LP |
2293 | } |
2294 | ||
701cc384 | 2295 | int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) { |
7410616c | 2296 | _cleanup_free_ char *buf = NULL; |
bd77d0fc LP |
2297 | Unit *other; |
2298 | int r; | |
2299 | ||
9e2f7c11 LP |
2300 | assert(u); |
2301 | assert(name || path); | |
bd77d0fc | 2302 | |
7410616c LP |
2303 | r = resolve_template(u, name, path, &buf, &name); |
2304 | if (r < 0) | |
2305 | return r; | |
bd77d0fc | 2306 | |
3f3cc397 LP |
2307 | r = manager_load_unit(u->manager, name, path, NULL, &other); |
2308 | if (r < 0) | |
68eda4bd | 2309 | return r; |
9e2f7c11 | 2310 | |
3f3cc397 | 2311 | return unit_add_dependency(other, d, u, add_reference); |
bd77d0fc LP |
2312 | } |
2313 | ||
2c966c03 | 2314 | int unit_add_two_dependencies_by_name_inverse(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) { |
7410616c | 2315 | _cleanup_free_ char *buf = NULL; |
2c966c03 LP |
2316 | Unit *other; |
2317 | int r; | |
2c966c03 LP |
2318 | |
2319 | assert(u); | |
2320 | assert(name || path); | |
2321 | ||
7410616c | 2322 | r = resolve_template(u, name, path, &buf, &name); |
3f3cc397 | 2323 | if (r < 0) |
68eda4bd | 2324 | return r; |
2c966c03 | 2325 | |
7410616c | 2326 | r = manager_load_unit(u->manager, name, path, NULL, &other); |
3f3cc397 | 2327 | if (r < 0) |
68eda4bd | 2328 | return r; |
2c966c03 | 2329 | |
7410616c | 2330 | return unit_add_two_dependencies(other, d, e, u, add_reference); |
2c966c03 LP |
2331 | } |
2332 | ||
0301abf4 | 2333 | int set_unit_path(const char *p) { |
0301abf4 | 2334 | /* This is mostly for debug purposes */ |
cf7d80a5 | 2335 | if (setenv("SYSTEMD_UNIT_PATH", p, 0) < 0) |
26d04f86 | 2336 | return -errno; |
0301abf4 LP |
2337 | |
2338 | return 0; | |
2339 | } | |
88066b3a | 2340 | |
ea430986 | 2341 | char *unit_dbus_path(Unit *u) { |
ea430986 LP |
2342 | assert(u); |
2343 | ||
ac155bb8 | 2344 | if (!u->id) |
04ade7d2 LP |
2345 | return NULL; |
2346 | ||
48899192 | 2347 | return unit_dbus_path_from_name(u->id); |
ea430986 LP |
2348 | } |
2349 | ||
d79200e2 LP |
2350 | int unit_set_slice(Unit *u, Unit *slice) { |
2351 | assert(u); | |
2352 | assert(slice); | |
2353 | ||
2354 | /* Sets the unit slice if it has not been set before. Is extra | |
2355 | * careful, to only allow this for units that actually have a | |
2356 | * cgroup context. Also, we don't allow to set this for slices | |
2357 | * (since the parent slice is derived from the name). Make | |
2358 | * sure the unit we set is actually a slice. */ | |
2359 | ||
2360 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) | |
2361 | return -EOPNOTSUPP; | |
2362 | ||
2363 | if (u->type == UNIT_SLICE) | |
2364 | return -EINVAL; | |
2365 | ||
102ef982 LP |
2366 | if (unit_active_state(u) != UNIT_INACTIVE) |
2367 | return -EBUSY; | |
2368 | ||
d79200e2 LP |
2369 | if (slice->type != UNIT_SLICE) |
2370 | return -EINVAL; | |
2371 | ||
efdb0237 LP |
2372 | if (unit_has_name(u, SPECIAL_INIT_SCOPE) && |
2373 | !unit_has_name(slice, SPECIAL_ROOT_SLICE)) | |
2374 | return -EPERM; | |
2375 | ||
d79200e2 LP |
2376 | if (UNIT_DEREF(u->slice) == slice) |
2377 | return 0; | |
2378 | ||
2379 | if (UNIT_ISSET(u->slice)) | |
2380 | return -EBUSY; | |
2381 | ||
2382 | unit_ref_set(&u->slice, slice); | |
2383 | return 1; | |
2384 | } | |
2385 | ||
2386 | int unit_set_default_slice(Unit *u) { | |
a8833944 LP |
2387 | _cleanup_free_ char *b = NULL; |
2388 | const char *slice_name; | |
a016b922 LP |
2389 | Unit *slice; |
2390 | int r; | |
2391 | ||
2392 | assert(u); | |
2393 | ||
9444b1f2 | 2394 | if (UNIT_ISSET(u->slice)) |
a016b922 LP |
2395 | return 0; |
2396 | ||
a8833944 LP |
2397 | if (u->instance) { |
2398 | _cleanup_free_ char *prefix = NULL, *escaped = NULL; | |
68eda4bd | 2399 | |
a8833944 LP |
2400 | /* Implicitly place all instantiated units in their |
2401 | * own per-template slice */ | |
2402 | ||
7410616c LP |
2403 | r = unit_name_to_prefix(u->id, &prefix); |
2404 | if (r < 0) | |
2405 | return r; | |
a8833944 LP |
2406 | |
2407 | /* The prefix is already escaped, but it might include | |
2408 | * "-" which has a special meaning for slice units, | |
2409 | * hence escape it here extra. */ | |
7410616c | 2410 | escaped = unit_name_escape(prefix); |
a8833944 LP |
2411 | if (!escaped) |
2412 | return -ENOMEM; | |
2413 | ||
b2c23da8 | 2414 | if (u->manager->running_as == MANAGER_SYSTEM) |
a8833944 LP |
2415 | b = strjoin("system-", escaped, ".slice", NULL); |
2416 | else | |
2417 | b = strappend(escaped, ".slice"); | |
2418 | if (!b) | |
2419 | return -ENOMEM; | |
2420 | ||
2421 | slice_name = b; | |
2422 | } else | |
2423 | slice_name = | |
efdb0237 | 2424 | u->manager->running_as == MANAGER_SYSTEM && !unit_has_name(u, SPECIAL_INIT_SCOPE) |
a8833944 LP |
2425 | ? SPECIAL_SYSTEM_SLICE |
2426 | : SPECIAL_ROOT_SLICE; | |
2427 | ||
2428 | r = manager_load_unit(u->manager, slice_name, NULL, NULL, &slice); | |
a016b922 LP |
2429 | if (r < 0) |
2430 | return r; | |
2431 | ||
d79200e2 | 2432 | return unit_set_slice(u, slice); |
a016b922 LP |
2433 | } |
2434 | ||
9444b1f2 LP |
2435 | const char *unit_slice_name(Unit *u) { |
2436 | assert(u); | |
2437 | ||
2438 | if (!UNIT_ISSET(u->slice)) | |
2439 | return NULL; | |
2440 | ||
2441 | return UNIT_DEREF(u->slice)->id; | |
2442 | } | |
2443 | ||
f6ff8c29 | 2444 | int unit_load_related_unit(Unit *u, const char *type, Unit **_found) { |
78edb35a | 2445 | _cleanup_free_ char *t = NULL; |
f6ff8c29 LP |
2446 | int r; |
2447 | ||
2448 | assert(u); | |
2449 | assert(type); | |
2450 | assert(_found); | |
2451 | ||
7410616c LP |
2452 | r = unit_name_change_suffix(u->id, type, &t); |
2453 | if (r < 0) | |
2454 | return r; | |
2455 | if (unit_has_name(u, t)) | |
2456 | return -EINVAL; | |
f6ff8c29 | 2457 | |
ac155bb8 | 2458 | r = manager_load_unit(u->manager, t, NULL, NULL, _found); |
9e2f7c11 | 2459 | assert(r < 0 || *_found != u); |
f6ff8c29 LP |
2460 | return r; |
2461 | } | |
2462 | ||
bbc29086 DM |
2463 | static int signal_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2464 | const char *name, *old_owner, *new_owner; | |
2465 | Unit *u = userdata; | |
2466 | int r; | |
2467 | ||
2468 | assert(message); | |
2469 | assert(u); | |
2470 | ||
2471 | r = sd_bus_message_read(message, "sss", &name, &old_owner, &new_owner); | |
2472 | if (r < 0) { | |
2473 | bus_log_parse_error(r); | |
2474 | return 0; | |
2475 | } | |
2476 | ||
2477 | if (UNIT_VTABLE(u)->bus_name_owner_change) | |
2478 | UNIT_VTABLE(u)->bus_name_owner_change(u, name, old_owner, new_owner); | |
2479 | ||
2480 | return 0; | |
2481 | } | |
2482 | ||
2483 | int unit_install_bus_match(sd_bus *bus, Unit *u, const char *name) { | |
2484 | _cleanup_free_ char *match = NULL; | |
2485 | Manager *m = u->manager; | |
2486 | ||
2487 | assert(m); | |
2488 | ||
2489 | if (u->match_bus_slot) | |
2490 | return -EBUSY; | |
2491 | ||
2492 | match = strjoin("type='signal'," | |
2493 | "sender='org.freedesktop.DBus'," | |
2494 | "path='/org/freedesktop/DBus'," | |
2495 | "interface='org.freedesktop.DBus'," | |
2496 | "member='NameOwnerChanged'," | |
2497 | "arg0='", | |
2498 | name, | |
2499 | "'", | |
2500 | NULL); | |
2501 | if (!match) | |
2502 | return -ENOMEM; | |
2503 | ||
2504 | return sd_bus_add_match(bus, &u->match_bus_slot, match, signal_name_owner_changed, u); | |
2505 | } | |
2506 | ||
05e343b7 | 2507 | int unit_watch_bus_name(Unit *u, const char *name) { |
bbc29086 DM |
2508 | int r; |
2509 | ||
05e343b7 LP |
2510 | assert(u); |
2511 | assert(name); | |
2512 | ||
2513 | /* Watch a specific name on the bus. We only support one unit | |
2514 | * watching each name for now. */ | |
2515 | ||
bbc29086 DM |
2516 | if (u->manager->api_bus) { |
2517 | /* If the bus is already available, install the match directly. | |
2518 | * Otherwise, just put the name in the list. bus_setup_api() will take care later. */ | |
2519 | r = unit_install_bus_match(u->manager->api_bus, u, name); | |
2520 | if (r < 0) | |
2521 | return log_warning_errno(r, "Failed to subscribe to NameOwnerChanged signal: %m"); | |
2522 | } | |
2523 | ||
2524 | r = hashmap_put(u->manager->watch_bus, name, u); | |
2525 | if (r < 0) { | |
2526 | u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot); | |
2527 | return log_warning_errno(r, "Failed to put bus name to hashmap: %m"); | |
2528 | } | |
2529 | ||
2530 | return 0; | |
05e343b7 LP |
2531 | } |
2532 | ||
2533 | void unit_unwatch_bus_name(Unit *u, const char *name) { | |
2534 | assert(u); | |
2535 | assert(name); | |
2536 | ||
ac155bb8 | 2537 | hashmap_remove_value(u->manager->watch_bus, name, u); |
bbc29086 | 2538 | u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot); |
05e343b7 LP |
2539 | } |
2540 | ||
a16e1123 LP |
2541 | bool unit_can_serialize(Unit *u) { |
2542 | assert(u); | |
2543 | ||
2544 | return UNIT_VTABLE(u)->serialize && UNIT_VTABLE(u)->deserialize_item; | |
2545 | } | |
2546 | ||
6b78f9b4 | 2547 | int unit_serialize(Unit *u, FILE *f, FDSet *fds, bool serialize_jobs) { |
a16e1123 LP |
2548 | int r; |
2549 | ||
2550 | assert(u); | |
2551 | assert(f); | |
2552 | assert(fds); | |
2553 | ||
9bdb98c5 LP |
2554 | if (unit_can_serialize(u)) { |
2555 | ExecRuntime *rt; | |
a16e1123 | 2556 | |
9bdb98c5 | 2557 | r = UNIT_VTABLE(u)->serialize(u, f, fds); |
613b411c LP |
2558 | if (r < 0) |
2559 | return r; | |
9bdb98c5 LP |
2560 | |
2561 | rt = unit_get_exec_runtime(u); | |
2562 | if (rt) { | |
f2341e0a | 2563 | r = exec_runtime_serialize(u, rt, f, fds); |
9bdb98c5 LP |
2564 | if (r < 0) |
2565 | return r; | |
2566 | } | |
e0209d83 MS |
2567 | } |
2568 | ||
ac155bb8 MS |
2569 | dual_timestamp_serialize(f, "inactive-exit-timestamp", &u->inactive_exit_timestamp); |
2570 | dual_timestamp_serialize(f, "active-enter-timestamp", &u->active_enter_timestamp); | |
2571 | dual_timestamp_serialize(f, "active-exit-timestamp", &u->active_exit_timestamp); | |
2572 | dual_timestamp_serialize(f, "inactive-enter-timestamp", &u->inactive_enter_timestamp); | |
2573 | dual_timestamp_serialize(f, "condition-timestamp", &u->condition_timestamp); | |
59fccdc5 | 2574 | dual_timestamp_serialize(f, "assert-timestamp", &u->assert_timestamp); |
2791a8f8 | 2575 | |
ac155bb8 MS |
2576 | if (dual_timestamp_is_set(&u->condition_timestamp)) |
2577 | unit_serialize_item(u, f, "condition-result", yes_no(u->condition_result)); | |
10717a1a | 2578 | |
59fccdc5 LP |
2579 | if (dual_timestamp_is_set(&u->assert_timestamp)) |
2580 | unit_serialize_item(u, f, "assert-result", yes_no(u->assert_result)); | |
2581 | ||
c2756a68 | 2582 | unit_serialize_item(u, f, "transient", yes_no(u->transient)); |
5ad096b3 | 2583 | unit_serialize_item_format(u, f, "cpuacct-usage-base", "%" PRIu64, u->cpuacct_usage_base); |
c2756a68 LP |
2584 | |
2585 | if (u->cgroup_path) | |
2586 | unit_serialize_item(u, f, "cgroup", u->cgroup_path); | |
de1d4f9b | 2587 | unit_serialize_item(u, f, "cgroup-realized", yes_no(u->cgroup_realized)); |
c2756a68 | 2588 | |
613b411c LP |
2589 | if (serialize_jobs) { |
2590 | if (u->job) { | |
2591 | fprintf(f, "job\n"); | |
2592 | job_serialize(u->job, f, fds); | |
2593 | } | |
2594 | ||
2595 | if (u->nop_job) { | |
2596 | fprintf(f, "job\n"); | |
2597 | job_serialize(u->nop_job, f, fds); | |
2598 | } | |
2599 | } | |
2600 | ||
a16e1123 LP |
2601 | /* End marker */ |
2602 | fputc('\n', f); | |
2603 | return 0; | |
2604 | } | |
2605 | ||
2606 | void unit_serialize_item_format(Unit *u, FILE *f, const char *key, const char *format, ...) { | |
2607 | va_list ap; | |
2608 | ||
2609 | assert(u); | |
2610 | assert(f); | |
2611 | assert(key); | |
2612 | assert(format); | |
2613 | ||
2614 | fputs(key, f); | |
2615 | fputc('=', f); | |
2616 | ||
2617 | va_start(ap, format); | |
2618 | vfprintf(f, format, ap); | |
2619 | va_end(ap); | |
2620 | ||
2621 | fputc('\n', f); | |
2622 | } | |
2623 | ||
2624 | void unit_serialize_item(Unit *u, FILE *f, const char *key, const char *value) { | |
2625 | assert(u); | |
2626 | assert(f); | |
2627 | assert(key); | |
2628 | assert(value); | |
2629 | ||
2630 | fprintf(f, "%s=%s\n", key, value); | |
2631 | } | |
2632 | ||
2633 | int unit_deserialize(Unit *u, FILE *f, FDSet *fds) { | |
28b99ccd | 2634 | ExecRuntime **rt = NULL; |
9bdb98c5 | 2635 | size_t offset; |
a16e1123 LP |
2636 | int r; |
2637 | ||
2638 | assert(u); | |
2639 | assert(f); | |
2640 | assert(fds); | |
2641 | ||
613b411c LP |
2642 | offset = UNIT_VTABLE(u)->exec_runtime_offset; |
2643 | if (offset > 0) | |
2644 | rt = (ExecRuntime**) ((uint8_t*) u + offset); | |
2645 | ||
a16e1123 | 2646 | for (;;) { |
20c03b7b | 2647 | char line[LINE_MAX], *l, *v; |
a16e1123 LP |
2648 | size_t k; |
2649 | ||
2650 | if (!fgets(line, sizeof(line), f)) { | |
2651 | if (feof(f)) | |
2652 | return 0; | |
2653 | return -errno; | |
2654 | } | |
2655 | ||
10f8e83c | 2656 | char_array_0(line); |
a16e1123 LP |
2657 | l = strstrip(line); |
2658 | ||
2659 | /* End marker */ | |
e911de99 | 2660 | if (isempty(l)) |
a16e1123 LP |
2661 | return 0; |
2662 | ||
2663 | k = strcspn(l, "="); | |
2664 | ||
2665 | if (l[k] == '=') { | |
2666 | l[k] = 0; | |
2667 | v = l+k+1; | |
2668 | } else | |
2669 | v = l+k; | |
2670 | ||
cca098b0 | 2671 | if (streq(l, "job")) { |
39a18c60 MS |
2672 | if (v[0] == '\0') { |
2673 | /* new-style serialized job */ | |
9c3349e2 LP |
2674 | Job *j; |
2675 | ||
2676 | j = job_new_raw(u); | |
39a18c60 | 2677 | if (!j) |
e911de99 | 2678 | return log_oom(); |
39a18c60 MS |
2679 | |
2680 | r = job_deserialize(j, f, fds); | |
2681 | if (r < 0) { | |
2682 | job_free(j); | |
2683 | return r; | |
2684 | } | |
cca098b0 | 2685 | |
39a18c60 MS |
2686 | r = hashmap_put(u->manager->jobs, UINT32_TO_PTR(j->id), j); |
2687 | if (r < 0) { | |
2688 | job_free(j); | |
2689 | return r; | |
2690 | } | |
e0209d83 MS |
2691 | |
2692 | r = job_install_deserialized(j); | |
2693 | if (r < 0) { | |
2694 | hashmap_remove(u->manager->jobs, UINT32_TO_PTR(j->id)); | |
2695 | job_free(j); | |
2696 | return r; | |
2697 | } | |
ed10fa8c LP |
2698 | } else /* legacy for pre-44 */ |
2699 | log_unit_warning(u, "Update from too old systemd versions are unsupported, cannot deserialize job: %s", v); | |
cca098b0 | 2700 | continue; |
8aaf019b | 2701 | } else if (streq(l, "inactive-exit-timestamp")) { |
ac155bb8 | 2702 | dual_timestamp_deserialize(v, &u->inactive_exit_timestamp); |
8aaf019b LP |
2703 | continue; |
2704 | } else if (streq(l, "active-enter-timestamp")) { | |
ac155bb8 | 2705 | dual_timestamp_deserialize(v, &u->active_enter_timestamp); |
8aaf019b LP |
2706 | continue; |
2707 | } else if (streq(l, "active-exit-timestamp")) { | |
ac155bb8 | 2708 | dual_timestamp_deserialize(v, &u->active_exit_timestamp); |
8aaf019b LP |
2709 | continue; |
2710 | } else if (streq(l, "inactive-enter-timestamp")) { | |
ac155bb8 | 2711 | dual_timestamp_deserialize(v, &u->inactive_enter_timestamp); |
8aaf019b | 2712 | continue; |
2791a8f8 | 2713 | } else if (streq(l, "condition-timestamp")) { |
ac155bb8 | 2714 | dual_timestamp_deserialize(v, &u->condition_timestamp); |
2791a8f8 | 2715 | continue; |
59fccdc5 LP |
2716 | } else if (streq(l, "assert-timestamp")) { |
2717 | dual_timestamp_deserialize(v, &u->assert_timestamp); | |
2718 | continue; | |
2791a8f8 | 2719 | } else if (streq(l, "condition-result")) { |
2791a8f8 | 2720 | |
e911de99 LP |
2721 | r = parse_boolean(v); |
2722 | if (r < 0) | |
f2341e0a | 2723 | log_unit_debug(u, "Failed to parse condition result value %s, ignoring.", v); |
2791a8f8 | 2724 | else |
e911de99 | 2725 | u->condition_result = r; |
efbac6d2 LP |
2726 | |
2727 | continue; | |
c2756a68 | 2728 | |
59fccdc5 | 2729 | } else if (streq(l, "assert-result")) { |
59fccdc5 | 2730 | |
e911de99 LP |
2731 | r = parse_boolean(v); |
2732 | if (r < 0) | |
f2341e0a | 2733 | log_unit_debug(u, "Failed to parse assert result value %s, ignoring.", v); |
59fccdc5 | 2734 | else |
e911de99 | 2735 | u->assert_result = r; |
59fccdc5 LP |
2736 | |
2737 | continue; | |
2738 | ||
c2756a68 | 2739 | } else if (streq(l, "transient")) { |
c2756a68 | 2740 | |
e911de99 LP |
2741 | r = parse_boolean(v); |
2742 | if (r < 0) | |
f2341e0a | 2743 | log_unit_debug(u, "Failed to parse transient bool %s, ignoring.", v); |
c2756a68 | 2744 | else |
e911de99 | 2745 | u->transient = r; |
c2756a68 LP |
2746 | |
2747 | continue; | |
e911de99 | 2748 | |
5ad096b3 LP |
2749 | } else if (streq(l, "cpuacct-usage-base")) { |
2750 | ||
2751 | r = safe_atou64(v, &u->cpuacct_usage_base); | |
2752 | if (r < 0) | |
f2341e0a | 2753 | log_unit_debug(u, "Failed to parse CPU usage %s, ignoring.", v); |
5ad096b3 | 2754 | |
0f908397 | 2755 | continue; |
4e595329 | 2756 | |
e911de99 | 2757 | } else if (streq(l, "cgroup")) { |
72673e86 | 2758 | |
e911de99 LP |
2759 | r = unit_set_cgroup_path(u, v); |
2760 | if (r < 0) | |
f2341e0a | 2761 | log_unit_debug_errno(u, r, "Failed to set cgroup path %s, ignoring: %m", v); |
4e595329 | 2762 | |
efdb0237 LP |
2763 | (void) unit_watch_cgroup(u); |
2764 | ||
de1d4f9b WF |
2765 | continue; |
2766 | } else if (streq(l, "cgroup-realized")) { | |
2767 | int b; | |
2768 | ||
2769 | b = parse_boolean(v); | |
2770 | if (b < 0) | |
2771 | log_unit_debug(u, "Failed to parse cgroup-realized bool %s, ignoring.", v); | |
2772 | else | |
2773 | u->cgroup_realized = b; | |
2774 | ||
c2756a68 | 2775 | continue; |
8aaf019b | 2776 | } |
cca098b0 | 2777 | |
9bdb98c5 LP |
2778 | if (unit_can_serialize(u)) { |
2779 | if (rt) { | |
f2341e0a | 2780 | r = exec_runtime_deserialize_item(u, rt, l, v, fds); |
e911de99 | 2781 | if (r < 0) { |
f2341e0a | 2782 | log_unit_warning(u, "Failed to deserialize runtime parameter '%s', ignoring.", l); |
e911de99 LP |
2783 | continue; |
2784 | } | |
2785 | ||
2786 | /* Returns positive if key was handled by the call */ | |
9bdb98c5 LP |
2787 | if (r > 0) |
2788 | continue; | |
2789 | } | |
2790 | ||
2791 | r = UNIT_VTABLE(u)->deserialize_item(u, l, v, fds); | |
613b411c | 2792 | if (r < 0) |
f2341e0a | 2793 | log_unit_warning(u, "Failed to deserialize unit parameter '%s', ignoring.", l); |
613b411c | 2794 | } |
a16e1123 LP |
2795 | } |
2796 | } | |
2797 | ||
6e2ef85b LP |
2798 | int unit_add_node_link(Unit *u, const char *what, bool wants) { |
2799 | Unit *device; | |
68eda4bd | 2800 | _cleanup_free_ char *e = NULL; |
6e2ef85b LP |
2801 | int r; |
2802 | ||
2803 | assert(u); | |
2804 | ||
6e2ef85b | 2805 | /* Adds in links to the device node that this unit is based on */ |
47bc12e1 LP |
2806 | if (isempty(what)) |
2807 | return 0; | |
6e2ef85b | 2808 | |
8407a5d0 | 2809 | if (!is_device_path(what)) |
6e2ef85b LP |
2810 | return 0; |
2811 | ||
47bc12e1 LP |
2812 | /* When device units aren't supported (such as in a |
2813 | * container), don't create dependencies on them. */ | |
1c2e9646 | 2814 | if (!unit_type_supported(UNIT_DEVICE)) |
47bc12e1 LP |
2815 | return 0; |
2816 | ||
7410616c LP |
2817 | r = unit_name_from_path(what, ".device", &e); |
2818 | if (r < 0) | |
2819 | return r; | |
6e2ef85b | 2820 | |
ac155bb8 | 2821 | r = manager_load_unit(u->manager, e, NULL, NULL, &device); |
6e2ef85b LP |
2822 | if (r < 0) |
2823 | return r; | |
2824 | ||
b2c23da8 | 2825 | r = unit_add_two_dependencies(u, UNIT_AFTER, u->manager->running_as == MANAGER_SYSTEM ? UNIT_BINDS_TO : UNIT_WANTS, device, true); |
faa368e3 | 2826 | if (r < 0) |
6e2ef85b LP |
2827 | return r; |
2828 | ||
faa368e3 LP |
2829 | if (wants) { |
2830 | r = unit_add_dependency(device, UNIT_WANTS, u, false); | |
2831 | if (r < 0) | |
6e2ef85b | 2832 | return r; |
faa368e3 | 2833 | } |
6e2ef85b LP |
2834 | |
2835 | return 0; | |
2836 | } | |
a16e1123 | 2837 | |
be847e82 | 2838 | int unit_coldplug(Unit *u) { |
cca098b0 LP |
2839 | int r; |
2840 | ||
2841 | assert(u); | |
2842 | ||
f78f265f LP |
2843 | /* Make sure we don't enter a loop, when coldplugging |
2844 | * recursively. */ | |
2845 | if (u->coldplugged) | |
2846 | return 0; | |
2847 | ||
2848 | u->coldplugged = true; | |
2849 | ||
f78f265f LP |
2850 | if (UNIT_VTABLE(u)->coldplug) { |
2851 | r = UNIT_VTABLE(u)->coldplug(u); | |
2852 | if (r < 0) | |
2853 | return r; | |
2854 | } | |
cca098b0 | 2855 | |
39a18c60 MS |
2856 | if (u->job) { |
2857 | r = job_coldplug(u->job); | |
2858 | if (r < 0) | |
2859 | return r; | |
be847e82 | 2860 | } |
cca098b0 LP |
2861 | |
2862 | return 0; | |
2863 | } | |
2864 | ||
49b1d377 | 2865 | void unit_status_printf(Unit *u, const char *status, const char *unit_status_msg_format) { |
bcfce235 | 2866 | DISABLE_WARNING_FORMAT_NONLITERAL; |
127d5fd1 ZJS |
2867 | manager_status_printf(u->manager, STATUS_TYPE_NORMAL, |
2868 | status, unit_status_msg_format, unit_description(u)); | |
bcfce235 | 2869 | REENABLE_WARNING; |
49b1d377 MS |
2870 | } |
2871 | ||
45fb0699 | 2872 | bool unit_need_daemon_reload(Unit *u) { |
ae7a7182 OS |
2873 | _cleanup_strv_free_ char **t = NULL; |
2874 | char **path; | |
1b64d026 | 2875 | struct stat st; |
ae7a7182 | 2876 | unsigned loaded_cnt, current_cnt; |
1b64d026 | 2877 | |
45fb0699 LP |
2878 | assert(u); |
2879 | ||
ac155bb8 | 2880 | if (u->fragment_path) { |
5f4b19f4 | 2881 | zero(st); |
ac155bb8 | 2882 | if (stat(u->fragment_path, &st) < 0) |
5f4b19f4 LP |
2883 | /* What, cannot access this anymore? */ |
2884 | return true; | |
45fb0699 | 2885 | |
ac155bb8 MS |
2886 | if (u->fragment_mtime > 0 && |
2887 | timespec_load(&st.st_mtim) != u->fragment_mtime) | |
5f4b19f4 LP |
2888 | return true; |
2889 | } | |
2890 | ||
1b64d026 LP |
2891 | if (u->source_path) { |
2892 | zero(st); | |
2893 | if (stat(u->source_path, &st) < 0) | |
2894 | return true; | |
2895 | ||
2896 | if (u->source_mtime > 0 && | |
2897 | timespec_load(&st.st_mtim) != u->source_mtime) | |
2898 | return true; | |
2899 | } | |
5f4b19f4 | 2900 | |
1a7f1b38 | 2901 | (void) unit_find_dropin_paths(u, &t); |
ae7a7182 OS |
2902 | loaded_cnt = strv_length(t); |
2903 | current_cnt = strv_length(u->dropin_paths); | |
2904 | ||
2905 | if (loaded_cnt == current_cnt) { | |
2906 | if (loaded_cnt == 0) | |
2907 | return false; | |
2908 | ||
2909 | if (strv_overlap(u->dropin_paths, t)) { | |
2910 | STRV_FOREACH(path, u->dropin_paths) { | |
2911 | zero(st); | |
2912 | if (stat(*path, &st) < 0) | |
2913 | return true; | |
2914 | ||
2915 | if (u->dropin_mtime > 0 && | |
2916 | timespec_load(&st.st_mtim) > u->dropin_mtime) | |
2917 | return true; | |
2918 | } | |
2919 | ||
2920 | return false; | |
2921 | } else | |
2922 | return true; | |
2923 | } else | |
2924 | return true; | |
45fb0699 LP |
2925 | } |
2926 | ||
fdf20a31 | 2927 | void unit_reset_failed(Unit *u) { |
5632e374 LP |
2928 | assert(u); |
2929 | ||
fdf20a31 MM |
2930 | if (UNIT_VTABLE(u)->reset_failed) |
2931 | UNIT_VTABLE(u)->reset_failed(u); | |
5632e374 LP |
2932 | } |
2933 | ||
a7f241db LP |
2934 | Unit *unit_following(Unit *u) { |
2935 | assert(u); | |
2936 | ||
2937 | if (UNIT_VTABLE(u)->following) | |
2938 | return UNIT_VTABLE(u)->following(u); | |
2939 | ||
2940 | return NULL; | |
2941 | } | |
2942 | ||
31afa0a4 | 2943 | bool unit_stop_pending(Unit *u) { |
18ffdfda LP |
2944 | assert(u); |
2945 | ||
31afa0a4 LP |
2946 | /* This call does check the current state of the unit. It's |
2947 | * hence useful to be called from state change calls of the | |
2948 | * unit itself, where the state isn't updated yet. This is | |
2949 | * different from unit_inactive_or_pending() which checks both | |
2950 | * the current state and for a queued job. */ | |
18ffdfda | 2951 | |
31afa0a4 LP |
2952 | return u->job && u->job->type == JOB_STOP; |
2953 | } | |
2954 | ||
2955 | bool unit_inactive_or_pending(Unit *u) { | |
2956 | assert(u); | |
2957 | ||
2958 | /* Returns true if the unit is inactive or going down */ | |
18ffdfda | 2959 | |
d956ac29 LP |
2960 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))) |
2961 | return true; | |
2962 | ||
31afa0a4 | 2963 | if (unit_stop_pending(u)) |
18ffdfda LP |
2964 | return true; |
2965 | ||
2966 | return false; | |
2967 | } | |
2968 | ||
31afa0a4 | 2969 | bool unit_active_or_pending(Unit *u) { |
f976f3f6 LP |
2970 | assert(u); |
2971 | ||
f60c2665 | 2972 | /* Returns true if the unit is active or going up */ |
f976f3f6 LP |
2973 | |
2974 | if (UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) | |
2975 | return true; | |
2976 | ||
ac155bb8 MS |
2977 | if (u->job && |
2978 | (u->job->type == JOB_START || | |
2979 | u->job->type == JOB_RELOAD_OR_START || | |
2980 | u->job->type == JOB_RESTART)) | |
f976f3f6 LP |
2981 | return true; |
2982 | ||
2983 | return false; | |
2984 | } | |
2985 | ||
718db961 | 2986 | int unit_kill(Unit *u, KillWho w, int signo, sd_bus_error *error) { |
8a0867d6 LP |
2987 | assert(u); |
2988 | assert(w >= 0 && w < _KILL_WHO_MAX); | |
8a0867d6 LP |
2989 | assert(signo > 0); |
2990 | assert(signo < _NSIG); | |
2991 | ||
8a0867d6 | 2992 | if (!UNIT_VTABLE(u)->kill) |
15411c0c | 2993 | return -EOPNOTSUPP; |
8a0867d6 | 2994 | |
c74f17d9 | 2995 | return UNIT_VTABLE(u)->kill(u, w, signo, error); |
8a0867d6 LP |
2996 | } |
2997 | ||
82659fd7 LP |
2998 | static Set *unit_pid_set(pid_t main_pid, pid_t control_pid) { |
2999 | Set *pid_set; | |
3000 | int r; | |
3001 | ||
d5099efc | 3002 | pid_set = set_new(NULL); |
82659fd7 LP |
3003 | if (!pid_set) |
3004 | return NULL; | |
3005 | ||
3006 | /* Exclude the main/control pids from being killed via the cgroup */ | |
3007 | if (main_pid > 0) { | |
fea72cc0 | 3008 | r = set_put(pid_set, PID_TO_PTR(main_pid)); |
82659fd7 LP |
3009 | if (r < 0) |
3010 | goto fail; | |
3011 | } | |
3012 | ||
3013 | if (control_pid > 0) { | |
fea72cc0 | 3014 | r = set_put(pid_set, PID_TO_PTR(control_pid)); |
82659fd7 LP |
3015 | if (r < 0) |
3016 | goto fail; | |
3017 | } | |
3018 | ||
3019 | return pid_set; | |
3020 | ||
3021 | fail: | |
3022 | set_free(pid_set); | |
3023 | return NULL; | |
3024 | } | |
3025 | ||
d91c34f2 LP |
3026 | int unit_kill_common( |
3027 | Unit *u, | |
3028 | KillWho who, | |
3029 | int signo, | |
3030 | pid_t main_pid, | |
3031 | pid_t control_pid, | |
718db961 | 3032 | sd_bus_error *error) { |
d91c34f2 | 3033 | |
814cc562 MS |
3034 | int r = 0; |
3035 | ||
52f448c3 | 3036 | if (who == KILL_MAIN) { |
814cc562 | 3037 | if (main_pid < 0) |
7358dc02 | 3038 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no main processes", unit_type_to_string(u->type)); |
52f448c3 | 3039 | else if (main_pid == 0) |
7358dc02 | 3040 | return sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill"); |
814cc562 MS |
3041 | } |
3042 | ||
52f448c3 | 3043 | if (who == KILL_CONTROL) { |
814cc562 | 3044 | if (control_pid < 0) |
7358dc02 | 3045 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no control processes", unit_type_to_string(u->type)); |
52f448c3 | 3046 | else if (control_pid == 0) |
7358dc02 | 3047 | return sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill"); |
814cc562 MS |
3048 | } |
3049 | ||
3050 | if (who == KILL_CONTROL || who == KILL_ALL) | |
3051 | if (control_pid > 0) | |
3052 | if (kill(control_pid, signo) < 0) | |
3053 | r = -errno; | |
3054 | ||
3055 | if (who == KILL_MAIN || who == KILL_ALL) | |
3056 | if (main_pid > 0) | |
3057 | if (kill(main_pid, signo) < 0) | |
3058 | r = -errno; | |
3059 | ||
4ad49000 | 3060 | if (who == KILL_ALL && u->cgroup_path) { |
814cc562 MS |
3061 | _cleanup_set_free_ Set *pid_set = NULL; |
3062 | int q; | |
3063 | ||
82659fd7 LP |
3064 | /* Exclude the main/control pids from being killed via the cgroup */ |
3065 | pid_set = unit_pid_set(main_pid, control_pid); | |
814cc562 MS |
3066 | if (!pid_set) |
3067 | return -ENOMEM; | |
3068 | ||
d0667321 | 3069 | q = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, signo, false, false, false, pid_set); |
814cc562 MS |
3070 | if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT) |
3071 | r = q; | |
3072 | } | |
3073 | ||
3074 | return r; | |
3075 | } | |
3076 | ||
6210e7fc LP |
3077 | int unit_following_set(Unit *u, Set **s) { |
3078 | assert(u); | |
3079 | assert(s); | |
3080 | ||
3081 | if (UNIT_VTABLE(u)->following_set) | |
3082 | return UNIT_VTABLE(u)->following_set(u, s); | |
3083 | ||
3084 | *s = NULL; | |
3085 | return 0; | |
3086 | } | |
3087 | ||
a4375746 LP |
3088 | UnitFileState unit_get_unit_file_state(Unit *u) { |
3089 | assert(u); | |
3090 | ||
ac155bb8 MS |
3091 | if (u->unit_file_state < 0 && u->fragment_path) |
3092 | u->unit_file_state = unit_file_get_state( | |
b2c23da8 | 3093 | u->manager->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, |
2b6bf07d | 3094 | NULL, basename(u->fragment_path)); |
a4375746 | 3095 | |
ac155bb8 | 3096 | return u->unit_file_state; |
a4375746 LP |
3097 | } |
3098 | ||
d2dc52db LP |
3099 | int unit_get_unit_file_preset(Unit *u) { |
3100 | assert(u); | |
3101 | ||
3102 | if (u->unit_file_preset < 0 && u->fragment_path) | |
3103 | u->unit_file_preset = unit_file_query_preset( | |
b2c23da8 | 3104 | u->manager->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, |
d2dc52db LP |
3105 | NULL, basename(u->fragment_path)); |
3106 | ||
3107 | return u->unit_file_preset; | |
3108 | } | |
3109 | ||
57020a3a LP |
3110 | Unit* unit_ref_set(UnitRef *ref, Unit *u) { |
3111 | assert(ref); | |
3112 | assert(u); | |
3113 | ||
3114 | if (ref->unit) | |
3115 | unit_ref_unset(ref); | |
3116 | ||
3117 | ref->unit = u; | |
71fda00f | 3118 | LIST_PREPEND(refs, u->refs, ref); |
57020a3a LP |
3119 | return u; |
3120 | } | |
3121 | ||
3122 | void unit_ref_unset(UnitRef *ref) { | |
3123 | assert(ref); | |
3124 | ||
3125 | if (!ref->unit) | |
3126 | return; | |
3127 | ||
71fda00f | 3128 | LIST_REMOVE(refs, ref->unit->refs, ref); |
57020a3a LP |
3129 | ref->unit = NULL; |
3130 | } | |
3131 | ||
598459ce LP |
3132 | int unit_patch_contexts(Unit *u) { |
3133 | CGroupContext *cc; | |
3134 | ExecContext *ec; | |
cba6e062 LP |
3135 | unsigned i; |
3136 | int r; | |
3137 | ||
e06c73cc | 3138 | assert(u); |
e06c73cc | 3139 | |
598459ce LP |
3140 | /* Patch in the manager defaults into the exec and cgroup |
3141 | * contexts, _after_ the rest of the settings have been | |
3142 | * initialized */ | |
085afe36 | 3143 | |
598459ce LP |
3144 | ec = unit_get_exec_context(u); |
3145 | if (ec) { | |
3146 | /* This only copies in the ones that need memory */ | |
3147 | for (i = 0; i < _RLIMIT_MAX; i++) | |
3148 | if (u->manager->rlimit[i] && !ec->rlimit[i]) { | |
3149 | ec->rlimit[i] = newdup(struct rlimit, u->manager->rlimit[i], 1); | |
3150 | if (!ec->rlimit[i]) | |
3151 | return -ENOMEM; | |
3152 | } | |
3153 | ||
b2c23da8 | 3154 | if (u->manager->running_as == MANAGER_USER && |
598459ce LP |
3155 | !ec->working_directory) { |
3156 | ||
3157 | r = get_home_dir(&ec->working_directory); | |
3158 | if (r < 0) | |
3159 | return r; | |
4c08c824 LP |
3160 | |
3161 | /* Allow user services to run, even if the | |
3162 | * home directory is missing */ | |
3163 | ec->working_directory_missing_ok = true; | |
cba6e062 LP |
3164 | } |
3165 | ||
b2c23da8 | 3166 | if (u->manager->running_as == MANAGER_USER && |
598459ce LP |
3167 | (ec->syscall_whitelist || |
3168 | !set_isempty(ec->syscall_filter) || | |
3169 | !set_isempty(ec->syscall_archs) || | |
3170 | ec->address_families_whitelist || | |
3171 | !set_isempty(ec->address_families))) | |
3172 | ec->no_new_privileges = true; | |
e06c73cc | 3173 | |
598459ce LP |
3174 | if (ec->private_devices) |
3175 | ec->capability_bounding_set_drop |= (uint64_t) 1ULL << (uint64_t) CAP_MKNOD; | |
cba6e062 LP |
3176 | } |
3177 | ||
598459ce LP |
3178 | cc = unit_get_cgroup_context(u); |
3179 | if (cc) { | |
f513e420 | 3180 | |
598459ce LP |
3181 | if (ec && |
3182 | ec->private_devices && | |
3183 | cc->device_policy == CGROUP_AUTO) | |
3184 | cc->device_policy = CGROUP_CLOSED; | |
3185 | } | |
f1660f96 | 3186 | |
cba6e062 | 3187 | return 0; |
e06c73cc LP |
3188 | } |
3189 | ||
3ef63c31 LP |
3190 | ExecContext *unit_get_exec_context(Unit *u) { |
3191 | size_t offset; | |
3192 | assert(u); | |
3193 | ||
598459ce LP |
3194 | if (u->type < 0) |
3195 | return NULL; | |
3196 | ||
3ef63c31 LP |
3197 | offset = UNIT_VTABLE(u)->exec_context_offset; |
3198 | if (offset <= 0) | |
3199 | return NULL; | |
3200 | ||
3201 | return (ExecContext*) ((uint8_t*) u + offset); | |
3202 | } | |
3203 | ||
718db961 LP |
3204 | KillContext *unit_get_kill_context(Unit *u) { |
3205 | size_t offset; | |
3206 | assert(u); | |
3207 | ||
598459ce LP |
3208 | if (u->type < 0) |
3209 | return NULL; | |
3210 | ||
718db961 LP |
3211 | offset = UNIT_VTABLE(u)->kill_context_offset; |
3212 | if (offset <= 0) | |
3213 | return NULL; | |
3214 | ||
3215 | return (KillContext*) ((uint8_t*) u + offset); | |
3216 | } | |
3217 | ||
4ad49000 LP |
3218 | CGroupContext *unit_get_cgroup_context(Unit *u) { |
3219 | size_t offset; | |
3220 | ||
598459ce LP |
3221 | if (u->type < 0) |
3222 | return NULL; | |
3223 | ||
4ad49000 LP |
3224 | offset = UNIT_VTABLE(u)->cgroup_context_offset; |
3225 | if (offset <= 0) | |
3226 | return NULL; | |
3227 | ||
3228 | return (CGroupContext*) ((uint8_t*) u + offset); | |
3229 | } | |
3230 | ||
613b411c LP |
3231 | ExecRuntime *unit_get_exec_runtime(Unit *u) { |
3232 | size_t offset; | |
3233 | ||
598459ce LP |
3234 | if (u->type < 0) |
3235 | return NULL; | |
3236 | ||
613b411c LP |
3237 | offset = UNIT_VTABLE(u)->exec_runtime_offset; |
3238 | if (offset <= 0) | |
3239 | return NULL; | |
3240 | ||
3241 | return *(ExecRuntime**) ((uint8_t*) u + offset); | |
3242 | } | |
3243 | ||
29686440 | 3244 | static int unit_drop_in_dir(Unit *u, UnitSetPropertiesMode mode, bool transient, char **dir) { |
3f5e8115 LP |
3245 | assert(u); |
3246 | ||
b2c23da8 | 3247 | if (u->manager->running_as == MANAGER_USER) { |
29686440 | 3248 | int r; |
26d04f86 | 3249 | |
718880ba SA |
3250 | if (mode == UNIT_PERSISTENT && !transient) |
3251 | r = user_config_home(dir); | |
3252 | else | |
4d5dec23 | 3253 | r = user_runtime_dir(dir); |
26d04f86 LP |
3254 | if (r == 0) |
3255 | return -ENOENT; | |
3f5e8115 | 3256 | |
29686440 ZJS |
3257 | return r; |
3258 | } | |
26d04f86 | 3259 | |
29686440 ZJS |
3260 | if (mode == UNIT_PERSISTENT && !transient) |
3261 | *dir = strdup("/etc/systemd/system"); | |
8e2af478 | 3262 | else |
29686440 ZJS |
3263 | *dir = strdup("/run/systemd/system"); |
3264 | if (!*dir) | |
71645aca LP |
3265 | return -ENOMEM; |
3266 | ||
26d04f86 | 3267 | return 0; |
71645aca LP |
3268 | } |
3269 | ||
3f5e8115 | 3270 | static int unit_drop_in_file(Unit *u, UnitSetPropertiesMode mode, const char *name, char **p, char **q) { |
29686440 ZJS |
3271 | _cleanup_free_ char *dir = NULL; |
3272 | int r; | |
3273 | ||
3274 | assert(u); | |
3275 | ||
3276 | r = unit_drop_in_dir(u, mode, u->transient, &dir); | |
3277 | if (r < 0) | |
3278 | return r; | |
3279 | ||
8eea8687 | 3280 | return drop_in_file(dir, u->id, 50, name, p, q); |
29686440 ZJS |
3281 | } |
3282 | ||
8e2af478 | 3283 | int unit_write_drop_in(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *data) { |
29686440 | 3284 | |
adb76a70 | 3285 | _cleanup_free_ char *dir = NULL, *p = NULL, *q = NULL; |
26d04f86 | 3286 | int r; |
71645aca LP |
3287 | |
3288 | assert(u); | |
3289 | ||
6d235724 | 3290 | if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) |
8e2af478 LP |
3291 | return 0; |
3292 | ||
29686440 | 3293 | r = unit_drop_in_dir(u, mode, u->transient, &dir); |
26d04f86 LP |
3294 | if (r < 0) |
3295 | return r; | |
71645aca | 3296 | |
adb76a70 WC |
3297 | r = write_drop_in(dir, u->id, 50, name, data); |
3298 | if (r < 0) | |
3299 | return r; | |
3300 | ||
3301 | r = drop_in_file(dir, u->id, 50, name, &p, &q); | |
3302 | if (r < 0) | |
3303 | return r; | |
3304 | ||
3305 | r = strv_extend(&u->dropin_paths, q); | |
3306 | if (r < 0) | |
3307 | return r; | |
3308 | ||
3309 | strv_sort(u->dropin_paths); | |
3310 | strv_uniq(u->dropin_paths); | |
3311 | ||
3312 | u->dropin_mtime = now(CLOCK_REALTIME); | |
3313 | ||
3314 | return 0; | |
26d04f86 | 3315 | } |
71645aca | 3316 | |
b9ec9359 LP |
3317 | int unit_write_drop_in_format(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *format, ...) { |
3318 | _cleanup_free_ char *p = NULL; | |
3319 | va_list ap; | |
3320 | int r; | |
3321 | ||
3322 | assert(u); | |
3323 | assert(name); | |
3324 | assert(format); | |
3325 | ||
6d235724 | 3326 | if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) |
b9ec9359 LP |
3327 | return 0; |
3328 | ||
3329 | va_start(ap, format); | |
3330 | r = vasprintf(&p, format, ap); | |
3331 | va_end(ap); | |
3332 | ||
3333 | if (r < 0) | |
3334 | return -ENOMEM; | |
3335 | ||
3336 | return unit_write_drop_in(u, mode, name, p); | |
3337 | } | |
3338 | ||
3339 | int unit_write_drop_in_private(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *data) { | |
b42defe3 LP |
3340 | _cleanup_free_ char *ndata = NULL; |
3341 | ||
3342 | assert(u); | |
3343 | assert(name); | |
3344 | assert(data); | |
3345 | ||
3346 | if (!UNIT_VTABLE(u)->private_section) | |
3347 | return -EINVAL; | |
3348 | ||
6d235724 | 3349 | if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) |
b9ec9359 LP |
3350 | return 0; |
3351 | ||
b42defe3 LP |
3352 | ndata = strjoin("[", UNIT_VTABLE(u)->private_section, "]\n", data, NULL); |
3353 | if (!ndata) | |
3354 | return -ENOMEM; | |
3355 | ||
3356 | return unit_write_drop_in(u, mode, name, ndata); | |
3357 | } | |
3358 | ||
b9ec9359 LP |
3359 | int unit_write_drop_in_private_format(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *format, ...) { |
3360 | _cleanup_free_ char *p = NULL; | |
3361 | va_list ap; | |
3362 | int r; | |
3363 | ||
3364 | assert(u); | |
3365 | assert(name); | |
3366 | assert(format); | |
3367 | ||
6d235724 | 3368 | if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) |
b9ec9359 LP |
3369 | return 0; |
3370 | ||
3371 | va_start(ap, format); | |
3372 | r = vasprintf(&p, format, ap); | |
3373 | va_end(ap); | |
3374 | ||
3375 | if (r < 0) | |
3376 | return -ENOMEM; | |
3377 | ||
3378 | return unit_write_drop_in_private(u, mode, name, p); | |
3379 | } | |
3380 | ||
8e2af478 | 3381 | int unit_remove_drop_in(Unit *u, UnitSetPropertiesMode mode, const char *name) { |
26d04f86 LP |
3382 | _cleanup_free_ char *p = NULL, *q = NULL; |
3383 | int r; | |
71645aca | 3384 | |
26d04f86 | 3385 | assert(u); |
71645aca | 3386 | |
6d235724 | 3387 | if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) |
8e2af478 LP |
3388 | return 0; |
3389 | ||
29686440 | 3390 | r = unit_drop_in_file(u, mode, name, &p, &q); |
6891529f ZJS |
3391 | if (r < 0) |
3392 | return r; | |
3393 | ||
71645aca | 3394 | if (unlink(q) < 0) |
241da328 | 3395 | r = errno == ENOENT ? 0 : -errno; |
26d04f86 | 3396 | else |
241da328 | 3397 | r = 1; |
71645aca LP |
3398 | |
3399 | rmdir(p); | |
26d04f86 | 3400 | return r; |
71645aca LP |
3401 | } |
3402 | ||
c2756a68 | 3403 | int unit_make_transient(Unit *u) { |
c2756a68 LP |
3404 | assert(u); |
3405 | ||
3f5e8115 LP |
3406 | if (!UNIT_VTABLE(u)->can_transient) |
3407 | return -EOPNOTSUPP; | |
3408 | ||
c2756a68 LP |
3409 | u->load_state = UNIT_STUB; |
3410 | u->load_error = 0; | |
3411 | u->transient = true; | |
3f5e8115 | 3412 | u->fragment_path = mfree(u->fragment_path); |
c2756a68 | 3413 | |
3f5e8115 | 3414 | return 0; |
c2756a68 LP |
3415 | } |
3416 | ||
cd2086fe LP |
3417 | int unit_kill_context( |
3418 | Unit *u, | |
3419 | KillContext *c, | |
db2cb23b | 3420 | KillOperation k, |
cd2086fe LP |
3421 | pid_t main_pid, |
3422 | pid_t control_pid, | |
3423 | bool main_pid_alien) { | |
3424 | ||
b821a397 LP |
3425 | bool wait_for_exit = false; |
3426 | int sig, r; | |
cd2086fe LP |
3427 | |
3428 | assert(u); | |
3429 | assert(c); | |
3430 | ||
3431 | if (c->kill_mode == KILL_NONE) | |
3432 | return 0; | |
3433 | ||
db2cb23b UTL |
3434 | switch (k) { |
3435 | case KILL_KILL: | |
3436 | sig = SIGKILL; | |
3437 | break; | |
3438 | case KILL_ABORT: | |
3439 | sig = SIGABRT; | |
3440 | break; | |
3441 | case KILL_TERMINATE: | |
3442 | sig = c->kill_signal; | |
3443 | break; | |
3444 | default: | |
3445 | assert_not_reached("KillOperation unknown"); | |
3446 | } | |
cd2086fe LP |
3447 | |
3448 | if (main_pid > 0) { | |
3449 | r = kill_and_sigcont(main_pid, sig); | |
3450 | ||
3451 | if (r < 0 && r != -ESRCH) { | |
3452 | _cleanup_free_ char *comm = NULL; | |
3453 | get_process_comm(main_pid, &comm); | |
3454 | ||
b821a397 | 3455 | log_unit_warning_errno(u, r, "Failed to kill main process " PID_FMT " (%s), ignoring: %m", main_pid, strna(comm)); |
82659fd7 | 3456 | } else { |
bc6aed7b LP |
3457 | if (!main_pid_alien) |
3458 | wait_for_exit = true; | |
82659fd7 | 3459 | |
d0667321 LP |
3460 | if (c->send_sighup && k == KILL_TERMINATE) |
3461 | (void) kill(main_pid, SIGHUP); | |
82659fd7 | 3462 | } |
cd2086fe LP |
3463 | } |
3464 | ||
3465 | if (control_pid > 0) { | |
3466 | r = kill_and_sigcont(control_pid, sig); | |
3467 | ||
3468 | if (r < 0 && r != -ESRCH) { | |
3469 | _cleanup_free_ char *comm = NULL; | |
3470 | get_process_comm(control_pid, &comm); | |
3471 | ||
b821a397 | 3472 | log_unit_warning_errno(u, r, "Failed to kill control process " PID_FMT " (%s), ignoring: %m", control_pid, strna(comm)); |
82659fd7 | 3473 | } else { |
cd2086fe | 3474 | wait_for_exit = true; |
82659fd7 | 3475 | |
d0667321 LP |
3476 | if (c->send_sighup && k == KILL_TERMINATE) |
3477 | (void) kill(control_pid, SIGHUP); | |
82659fd7 | 3478 | } |
cd2086fe LP |
3479 | } |
3480 | ||
b821a397 LP |
3481 | if (u->cgroup_path && |
3482 | (c->kill_mode == KILL_CONTROL_GROUP || (c->kill_mode == KILL_MIXED && k == KILL_KILL))) { | |
cd2086fe LP |
3483 | _cleanup_set_free_ Set *pid_set = NULL; |
3484 | ||
82659fd7 LP |
3485 | /* Exclude the main/control pids from being killed via the cgroup */ |
3486 | pid_set = unit_pid_set(main_pid, control_pid); | |
cd2086fe LP |
3487 | if (!pid_set) |
3488 | return -ENOMEM; | |
3489 | ||
d0667321 | 3490 | r = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, sig, true, k != KILL_TERMINATE, false, pid_set); |
cd2086fe LP |
3491 | if (r < 0) { |
3492 | if (r != -EAGAIN && r != -ESRCH && r != -ENOENT) | |
b821a397 LP |
3493 | log_unit_warning_errno(u, r, "Failed to kill control group %s, ignoring: %m", u->cgroup_path); |
3494 | ||
82659fd7 | 3495 | } else if (r > 0) { |
bc6aed7b | 3496 | |
efdb0237 LP |
3497 | /* FIXME: For now, on the legacy hierarchy, we |
3498 | * will not wait for the cgroup members to die | |
3499 | * if we are running in a container or if this | |
3500 | * is a delegation unit, simply because cgroup | |
3501 | * notification is unreliable in these | |
3502 | * cases. It doesn't work at all in | |
3503 | * containers, and outside of containers it | |
3504 | * can be confused easily by left-over | |
3505 | * directories in the cgroup -- which however | |
3506 | * should not exist in non-delegated units. On | |
3507 | * the unified hierarchy that's different, | |
3508 | * there we get proper events. Hence rely on | |
3509 | * them.*/ | |
3510 | ||
3511 | if (cg_unified() > 0 || | |
75f86906 | 3512 | (detect_container() == 0 && !unit_cgroup_delegate(u))) |
e9db43d5 | 3513 | wait_for_exit = true; |
58ea275a | 3514 | |
db2cb23b | 3515 | if (c->send_sighup && k != KILL_KILL) { |
82659fd7 LP |
3516 | set_free(pid_set); |
3517 | ||
3518 | pid_set = unit_pid_set(main_pid, control_pid); | |
3519 | if (!pid_set) | |
3520 | return -ENOMEM; | |
3521 | ||
8190da36 | 3522 | cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, SIGHUP, false, true, false, pid_set); |
82659fd7 LP |
3523 | } |
3524 | } | |
cd2086fe LP |
3525 | } |
3526 | ||
3527 | return wait_for_exit; | |
3528 | } | |
3529 | ||
a57f7e2c LP |
3530 | int unit_require_mounts_for(Unit *u, const char *path) { |
3531 | char prefix[strlen(path) + 1], *p; | |
3532 | int r; | |
3533 | ||
3534 | assert(u); | |
3535 | assert(path); | |
3536 | ||
3537 | /* Registers a unit for requiring a certain path and all its | |
3538 | * prefixes. We keep a simple array of these paths in the | |
3539 | * unit, since its usually short. However, we build a prefix | |
3540 | * table for all possible prefixes so that new appearing mount | |
3541 | * units can easily determine which units to make themselves a | |
3542 | * dependency of. */ | |
3543 | ||
70b64bd3 ZJS |
3544 | if (!path_is_absolute(path)) |
3545 | return -EINVAL; | |
3546 | ||
a57f7e2c LP |
3547 | p = strdup(path); |
3548 | if (!p) | |
3549 | return -ENOMEM; | |
3550 | ||
3551 | path_kill_slashes(p); | |
3552 | ||
a57f7e2c LP |
3553 | if (!path_is_safe(p)) { |
3554 | free(p); | |
3555 | return -EPERM; | |
3556 | } | |
3557 | ||
3558 | if (strv_contains(u->requires_mounts_for, p)) { | |
3559 | free(p); | |
3560 | return 0; | |
3561 | } | |
3562 | ||
6e18964d ZJS |
3563 | r = strv_consume(&u->requires_mounts_for, p); |
3564 | if (r < 0) | |
a57f7e2c | 3565 | return r; |
a57f7e2c LP |
3566 | |
3567 | PATH_FOREACH_PREFIX_MORE(prefix, p) { | |
3568 | Set *x; | |
3569 | ||
3570 | x = hashmap_get(u->manager->units_requiring_mounts_for, prefix); | |
3571 | if (!x) { | |
3572 | char *q; | |
3573 | ||
742f41ad LP |
3574 | r = hashmap_ensure_allocated(&u->manager->units_requiring_mounts_for, &string_hash_ops); |
3575 | if (r < 0) | |
3576 | return r; | |
a57f7e2c LP |
3577 | |
3578 | q = strdup(prefix); | |
3579 | if (!q) | |
3580 | return -ENOMEM; | |
3581 | ||
d5099efc | 3582 | x = set_new(NULL); |
a57f7e2c LP |
3583 | if (!x) { |
3584 | free(q); | |
3585 | return -ENOMEM; | |
3586 | } | |
3587 | ||
3588 | r = hashmap_put(u->manager->units_requiring_mounts_for, q, x); | |
3589 | if (r < 0) { | |
3590 | free(q); | |
3591 | set_free(x); | |
3592 | return r; | |
3593 | } | |
3594 | } | |
3595 | ||
3596 | r = set_put(x, u); | |
3597 | if (r < 0) | |
3598 | return r; | |
3599 | } | |
3600 | ||
3601 | return 0; | |
3602 | } | |
3603 | ||
613b411c LP |
3604 | int unit_setup_exec_runtime(Unit *u) { |
3605 | ExecRuntime **rt; | |
3606 | size_t offset; | |
3607 | Iterator i; | |
3608 | Unit *other; | |
3609 | ||
3610 | offset = UNIT_VTABLE(u)->exec_runtime_offset; | |
3611 | assert(offset > 0); | |
3612 | ||
06b643e7 | 3613 | /* Check if there already is an ExecRuntime for this unit? */ |
613b411c LP |
3614 | rt = (ExecRuntime**) ((uint8_t*) u + offset); |
3615 | if (*rt) | |
3616 | return 0; | |
3617 | ||
3618 | /* Try to get it from somebody else */ | |
3619 | SET_FOREACH(other, u->dependencies[UNIT_JOINS_NAMESPACE_OF], i) { | |
3620 | ||
3621 | *rt = unit_get_exec_runtime(other); | |
3622 | if (*rt) { | |
3623 | exec_runtime_ref(*rt); | |
3624 | return 0; | |
3625 | } | |
3626 | } | |
3627 | ||
3628 | return exec_runtime_make(rt, unit_get_exec_context(u), u->id); | |
3629 | } | |
3630 | ||
1c2e9646 LP |
3631 | bool unit_type_supported(UnitType t) { |
3632 | if (_unlikely_(t < 0)) | |
3633 | return false; | |
3634 | if (_unlikely_(t >= _UNIT_TYPE_MAX)) | |
3635 | return false; | |
3636 | ||
3637 | if (!unit_vtable[t]->supported) | |
3638 | return true; | |
3639 | ||
3640 | return unit_vtable[t]->supported(); | |
3641 | } | |
3642 | ||
8b4305c7 LP |
3643 | void unit_warn_if_dir_nonempty(Unit *u, const char* where) { |
3644 | int r; | |
3645 | ||
3646 | assert(u); | |
3647 | assert(where); | |
3648 | ||
3649 | r = dir_is_empty(where); | |
3650 | if (r > 0) | |
3651 | return; | |
3652 | if (r < 0) { | |
3653 | log_unit_warning_errno(u, r, "Failed to check directory %s: %m", where); | |
3654 | return; | |
3655 | } | |
3656 | ||
3657 | log_struct(LOG_NOTICE, | |
3658 | LOG_MESSAGE_ID(SD_MESSAGE_OVERMOUNTING), | |
3659 | LOG_UNIT_ID(u), | |
3660 | LOG_UNIT_MESSAGE(u, "Directory %s to mount over is not empty, mounting anyway.", where), | |
3661 | "WHERE=%s", where, | |
3662 | NULL); | |
3663 | } | |
3664 | ||
3665 | int unit_fail_if_symlink(Unit *u, const char* where) { | |
3666 | int r; | |
3667 | ||
3668 | assert(u); | |
3669 | assert(where); | |
3670 | ||
3671 | r = is_symlink(where); | |
3672 | if (r < 0) { | |
3673 | log_unit_debug_errno(u, r, "Failed to check symlink %s, ignoring: %m", where); | |
3674 | return 0; | |
3675 | } | |
3676 | if (r == 0) | |
3677 | return 0; | |
3678 | ||
3679 | log_struct(LOG_ERR, | |
3680 | LOG_MESSAGE_ID(SD_MESSAGE_OVERMOUNTING), | |
3681 | LOG_UNIT_ID(u), | |
3682 | LOG_UNIT_MESSAGE(u, "Mount on symlink %s not allowed.", where), | |
3683 | "WHERE=%s", where, | |
3684 | NULL); | |
3685 | ||
3686 | return -ELOOP; | |
3687 | } | |
3688 | ||
94f04347 LP |
3689 | static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = { |
3690 | [UNIT_ACTIVE] = "active", | |
032ff4af | 3691 | [UNIT_RELOADING] = "reloading", |
94f04347 | 3692 | [UNIT_INACTIVE] = "inactive", |
fdf20a31 | 3693 | [UNIT_FAILED] = "failed", |
94f04347 LP |
3694 | [UNIT_ACTIVATING] = "activating", |
3695 | [UNIT_DEACTIVATING] = "deactivating" | |
3696 | }; | |
3697 | ||
3698 | DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState); |