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