]>
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) | |
3f5e8115 | 408 | (void) unlink(u->fragment_path); |
c2756a68 LP |
409 | |
410 | STRV_FOREACH(i, u->dropin_paths) { | |
411 | _cleanup_free_ char *p = NULL; | |
412 | int r; | |
413 | ||
3f5e8115 | 414 | (void) unlink(*i); |
c2756a68 LP |
415 | |
416 | r = path_get_parent(*i, &p); | |
417 | if (r >= 0) | |
3f5e8115 | 418 | (void) rmdir(p); |
c2756a68 LP |
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 | |
35b7ff80 | 1125 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) |
e954c9cf LP |
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 | ||
d79200e2 LP |
2427 | int unit_set_slice(Unit *u, Unit *slice) { |
2428 | assert(u); | |
2429 | assert(slice); | |
2430 | ||
2431 | /* Sets the unit slice if it has not been set before. Is extra | |
2432 | * careful, to only allow this for units that actually have a | |
2433 | * cgroup context. Also, we don't allow to set this for slices | |
2434 | * (since the parent slice is derived from the name). Make | |
2435 | * sure the unit we set is actually a slice. */ | |
2436 | ||
2437 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) | |
2438 | return -EOPNOTSUPP; | |
2439 | ||
2440 | if (u->type == UNIT_SLICE) | |
2441 | return -EINVAL; | |
2442 | ||
2443 | if (slice->type != UNIT_SLICE) | |
2444 | return -EINVAL; | |
2445 | ||
2446 | if (UNIT_DEREF(u->slice) == slice) | |
2447 | return 0; | |
2448 | ||
2449 | if (UNIT_ISSET(u->slice)) | |
2450 | return -EBUSY; | |
2451 | ||
2452 | unit_ref_set(&u->slice, slice); | |
2453 | return 1; | |
2454 | } | |
2455 | ||
2456 | int unit_set_default_slice(Unit *u) { | |
a8833944 LP |
2457 | _cleanup_free_ char *b = NULL; |
2458 | const char *slice_name; | |
a016b922 LP |
2459 | Unit *slice; |
2460 | int r; | |
2461 | ||
2462 | assert(u); | |
2463 | ||
9444b1f2 | 2464 | if (UNIT_ISSET(u->slice)) |
a016b922 LP |
2465 | return 0; |
2466 | ||
a8833944 LP |
2467 | if (u->instance) { |
2468 | _cleanup_free_ char *prefix = NULL, *escaped = NULL; | |
68eda4bd | 2469 | |
a8833944 LP |
2470 | /* Implicitly place all instantiated units in their |
2471 | * own per-template slice */ | |
2472 | ||
7410616c LP |
2473 | r = unit_name_to_prefix(u->id, &prefix); |
2474 | if (r < 0) | |
2475 | return r; | |
a8833944 LP |
2476 | |
2477 | /* The prefix is already escaped, but it might include | |
2478 | * "-" which has a special meaning for slice units, | |
2479 | * hence escape it here extra. */ | |
7410616c | 2480 | escaped = unit_name_escape(prefix); |
a8833944 LP |
2481 | if (!escaped) |
2482 | return -ENOMEM; | |
2483 | ||
b2c23da8 | 2484 | if (u->manager->running_as == MANAGER_SYSTEM) |
a8833944 LP |
2485 | b = strjoin("system-", escaped, ".slice", NULL); |
2486 | else | |
2487 | b = strappend(escaped, ".slice"); | |
2488 | if (!b) | |
2489 | return -ENOMEM; | |
2490 | ||
2491 | slice_name = b; | |
2492 | } else | |
2493 | slice_name = | |
b2c23da8 | 2494 | u->manager->running_as == MANAGER_SYSTEM |
a8833944 LP |
2495 | ? SPECIAL_SYSTEM_SLICE |
2496 | : SPECIAL_ROOT_SLICE; | |
2497 | ||
2498 | r = manager_load_unit(u->manager, slice_name, NULL, NULL, &slice); | |
a016b922 LP |
2499 | if (r < 0) |
2500 | return r; | |
2501 | ||
d79200e2 | 2502 | return unit_set_slice(u, slice); |
a016b922 LP |
2503 | } |
2504 | ||
9444b1f2 LP |
2505 | const char *unit_slice_name(Unit *u) { |
2506 | assert(u); | |
2507 | ||
2508 | if (!UNIT_ISSET(u->slice)) | |
2509 | return NULL; | |
2510 | ||
2511 | return UNIT_DEREF(u->slice)->id; | |
2512 | } | |
2513 | ||
f6ff8c29 | 2514 | int unit_load_related_unit(Unit *u, const char *type, Unit **_found) { |
78edb35a | 2515 | _cleanup_free_ char *t = NULL; |
f6ff8c29 LP |
2516 | int r; |
2517 | ||
2518 | assert(u); | |
2519 | assert(type); | |
2520 | assert(_found); | |
2521 | ||
7410616c LP |
2522 | r = unit_name_change_suffix(u->id, type, &t); |
2523 | if (r < 0) | |
2524 | return r; | |
2525 | if (unit_has_name(u, t)) | |
2526 | return -EINVAL; | |
f6ff8c29 | 2527 | |
ac155bb8 | 2528 | r = manager_load_unit(u->manager, t, NULL, NULL, _found); |
9e2f7c11 | 2529 | assert(r < 0 || *_found != u); |
f6ff8c29 LP |
2530 | return r; |
2531 | } | |
2532 | ||
bbc29086 DM |
2533 | static int signal_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2534 | const char *name, *old_owner, *new_owner; | |
2535 | Unit *u = userdata; | |
2536 | int r; | |
2537 | ||
2538 | assert(message); | |
2539 | assert(u); | |
2540 | ||
2541 | r = sd_bus_message_read(message, "sss", &name, &old_owner, &new_owner); | |
2542 | if (r < 0) { | |
2543 | bus_log_parse_error(r); | |
2544 | return 0; | |
2545 | } | |
2546 | ||
2547 | if (UNIT_VTABLE(u)->bus_name_owner_change) | |
2548 | UNIT_VTABLE(u)->bus_name_owner_change(u, name, old_owner, new_owner); | |
2549 | ||
2550 | return 0; | |
2551 | } | |
2552 | ||
2553 | int unit_install_bus_match(sd_bus *bus, Unit *u, const char *name) { | |
2554 | _cleanup_free_ char *match = NULL; | |
2555 | Manager *m = u->manager; | |
2556 | ||
2557 | assert(m); | |
2558 | ||
2559 | if (u->match_bus_slot) | |
2560 | return -EBUSY; | |
2561 | ||
2562 | match = strjoin("type='signal'," | |
2563 | "sender='org.freedesktop.DBus'," | |
2564 | "path='/org/freedesktop/DBus'," | |
2565 | "interface='org.freedesktop.DBus'," | |
2566 | "member='NameOwnerChanged'," | |
2567 | "arg0='", | |
2568 | name, | |
2569 | "'", | |
2570 | NULL); | |
2571 | if (!match) | |
2572 | return -ENOMEM; | |
2573 | ||
2574 | return sd_bus_add_match(bus, &u->match_bus_slot, match, signal_name_owner_changed, u); | |
2575 | } | |
2576 | ||
05e343b7 | 2577 | int unit_watch_bus_name(Unit *u, const char *name) { |
bbc29086 DM |
2578 | int r; |
2579 | ||
05e343b7 LP |
2580 | assert(u); |
2581 | assert(name); | |
2582 | ||
2583 | /* Watch a specific name on the bus. We only support one unit | |
2584 | * watching each name for now. */ | |
2585 | ||
bbc29086 DM |
2586 | if (u->manager->api_bus) { |
2587 | /* If the bus is already available, install the match directly. | |
2588 | * Otherwise, just put the name in the list. bus_setup_api() will take care later. */ | |
2589 | r = unit_install_bus_match(u->manager->api_bus, u, name); | |
2590 | if (r < 0) | |
2591 | return log_warning_errno(r, "Failed to subscribe to NameOwnerChanged signal: %m"); | |
2592 | } | |
2593 | ||
2594 | r = hashmap_put(u->manager->watch_bus, name, u); | |
2595 | if (r < 0) { | |
2596 | u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot); | |
2597 | return log_warning_errno(r, "Failed to put bus name to hashmap: %m"); | |
2598 | } | |
2599 | ||
2600 | return 0; | |
05e343b7 LP |
2601 | } |
2602 | ||
2603 | void unit_unwatch_bus_name(Unit *u, const char *name) { | |
2604 | assert(u); | |
2605 | assert(name); | |
2606 | ||
ac155bb8 | 2607 | hashmap_remove_value(u->manager->watch_bus, name, u); |
bbc29086 | 2608 | u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot); |
05e343b7 LP |
2609 | } |
2610 | ||
a16e1123 LP |
2611 | bool unit_can_serialize(Unit *u) { |
2612 | assert(u); | |
2613 | ||
2614 | return UNIT_VTABLE(u)->serialize && UNIT_VTABLE(u)->deserialize_item; | |
2615 | } | |
2616 | ||
6b78f9b4 | 2617 | int unit_serialize(Unit *u, FILE *f, FDSet *fds, bool serialize_jobs) { |
a16e1123 LP |
2618 | int r; |
2619 | ||
2620 | assert(u); | |
2621 | assert(f); | |
2622 | assert(fds); | |
2623 | ||
9bdb98c5 LP |
2624 | if (unit_can_serialize(u)) { |
2625 | ExecRuntime *rt; | |
a16e1123 | 2626 | |
9bdb98c5 | 2627 | r = UNIT_VTABLE(u)->serialize(u, f, fds); |
613b411c LP |
2628 | if (r < 0) |
2629 | return r; | |
9bdb98c5 LP |
2630 | |
2631 | rt = unit_get_exec_runtime(u); | |
2632 | if (rt) { | |
f2341e0a | 2633 | r = exec_runtime_serialize(u, rt, f, fds); |
9bdb98c5 LP |
2634 | if (r < 0) |
2635 | return r; | |
2636 | } | |
e0209d83 MS |
2637 | } |
2638 | ||
ac155bb8 MS |
2639 | dual_timestamp_serialize(f, "inactive-exit-timestamp", &u->inactive_exit_timestamp); |
2640 | dual_timestamp_serialize(f, "active-enter-timestamp", &u->active_enter_timestamp); | |
2641 | dual_timestamp_serialize(f, "active-exit-timestamp", &u->active_exit_timestamp); | |
2642 | dual_timestamp_serialize(f, "inactive-enter-timestamp", &u->inactive_enter_timestamp); | |
2643 | dual_timestamp_serialize(f, "condition-timestamp", &u->condition_timestamp); | |
59fccdc5 | 2644 | dual_timestamp_serialize(f, "assert-timestamp", &u->assert_timestamp); |
2791a8f8 | 2645 | |
ac155bb8 MS |
2646 | if (dual_timestamp_is_set(&u->condition_timestamp)) |
2647 | unit_serialize_item(u, f, "condition-result", yes_no(u->condition_result)); | |
10717a1a | 2648 | |
59fccdc5 LP |
2649 | if (dual_timestamp_is_set(&u->assert_timestamp)) |
2650 | unit_serialize_item(u, f, "assert-result", yes_no(u->assert_result)); | |
2651 | ||
c2756a68 | 2652 | unit_serialize_item(u, f, "transient", yes_no(u->transient)); |
5ad096b3 | 2653 | unit_serialize_item_format(u, f, "cpuacct-usage-base", "%" PRIu64, u->cpuacct_usage_base); |
c2756a68 LP |
2654 | |
2655 | if (u->cgroup_path) | |
2656 | unit_serialize_item(u, f, "cgroup", u->cgroup_path); | |
de1d4f9b | 2657 | unit_serialize_item(u, f, "cgroup-realized", yes_no(u->cgroup_realized)); |
c2756a68 | 2658 | |
613b411c LP |
2659 | if (serialize_jobs) { |
2660 | if (u->job) { | |
2661 | fprintf(f, "job\n"); | |
2662 | job_serialize(u->job, f, fds); | |
2663 | } | |
2664 | ||
2665 | if (u->nop_job) { | |
2666 | fprintf(f, "job\n"); | |
2667 | job_serialize(u->nop_job, f, fds); | |
2668 | } | |
2669 | } | |
2670 | ||
a16e1123 LP |
2671 | /* End marker */ |
2672 | fputc('\n', f); | |
2673 | return 0; | |
2674 | } | |
2675 | ||
2676 | void unit_serialize_item_format(Unit *u, FILE *f, const char *key, const char *format, ...) { | |
2677 | va_list ap; | |
2678 | ||
2679 | assert(u); | |
2680 | assert(f); | |
2681 | assert(key); | |
2682 | assert(format); | |
2683 | ||
2684 | fputs(key, f); | |
2685 | fputc('=', f); | |
2686 | ||
2687 | va_start(ap, format); | |
2688 | vfprintf(f, format, ap); | |
2689 | va_end(ap); | |
2690 | ||
2691 | fputc('\n', f); | |
2692 | } | |
2693 | ||
2694 | void unit_serialize_item(Unit *u, FILE *f, const char *key, const char *value) { | |
2695 | assert(u); | |
2696 | assert(f); | |
2697 | assert(key); | |
2698 | assert(value); | |
2699 | ||
2700 | fprintf(f, "%s=%s\n", key, value); | |
2701 | } | |
2702 | ||
e911de99 LP |
2703 | static int unit_set_cgroup_path(Unit *u, const char *path) { |
2704 | _cleanup_free_ char *p = NULL; | |
2705 | int r; | |
2706 | ||
2707 | assert(u); | |
2708 | ||
2709 | if (path) { | |
2710 | p = strdup(path); | |
2711 | if (!p) | |
2712 | return -ENOMEM; | |
2713 | } else | |
2714 | p = NULL; | |
2715 | ||
2716 | if (streq_ptr(u->cgroup_path, p)) | |
2717 | return 0; | |
2718 | ||
2719 | if (p) { | |
2720 | r = hashmap_put(u->manager->cgroup_unit, p, u); | |
2721 | if (r < 0) | |
2722 | return r; | |
2723 | } | |
2724 | ||
2725 | if (u->cgroup_path) { | |
f2341e0a | 2726 | log_unit_debug(u, "Changing cgroup path from %s to %s.", u->cgroup_path, strna(p)); |
e911de99 LP |
2727 | hashmap_remove(u->manager->cgroup_unit, u->cgroup_path); |
2728 | free(u->cgroup_path); | |
2729 | } | |
2730 | ||
2731 | u->cgroup_path = p; | |
2732 | p = NULL; | |
2733 | ||
2734 | return 0; | |
2735 | } | |
2736 | ||
a16e1123 | 2737 | int unit_deserialize(Unit *u, FILE *f, FDSet *fds) { |
28b99ccd | 2738 | ExecRuntime **rt = NULL; |
9bdb98c5 | 2739 | size_t offset; |
a16e1123 LP |
2740 | int r; |
2741 | ||
2742 | assert(u); | |
2743 | assert(f); | |
2744 | assert(fds); | |
2745 | ||
613b411c LP |
2746 | offset = UNIT_VTABLE(u)->exec_runtime_offset; |
2747 | if (offset > 0) | |
2748 | rt = (ExecRuntime**) ((uint8_t*) u + offset); | |
2749 | ||
a16e1123 | 2750 | for (;;) { |
20c03b7b | 2751 | char line[LINE_MAX], *l, *v; |
a16e1123 LP |
2752 | size_t k; |
2753 | ||
2754 | if (!fgets(line, sizeof(line), f)) { | |
2755 | if (feof(f)) | |
2756 | return 0; | |
2757 | return -errno; | |
2758 | } | |
2759 | ||
10f8e83c | 2760 | char_array_0(line); |
a16e1123 LP |
2761 | l = strstrip(line); |
2762 | ||
2763 | /* End marker */ | |
e911de99 | 2764 | if (isempty(l)) |
a16e1123 LP |
2765 | return 0; |
2766 | ||
2767 | k = strcspn(l, "="); | |
2768 | ||
2769 | if (l[k] == '=') { | |
2770 | l[k] = 0; | |
2771 | v = l+k+1; | |
2772 | } else | |
2773 | v = l+k; | |
2774 | ||
cca098b0 | 2775 | if (streq(l, "job")) { |
39a18c60 MS |
2776 | if (v[0] == '\0') { |
2777 | /* new-style serialized job */ | |
9c3349e2 LP |
2778 | Job *j; |
2779 | ||
2780 | j = job_new_raw(u); | |
39a18c60 | 2781 | if (!j) |
e911de99 | 2782 | return log_oom(); |
39a18c60 MS |
2783 | |
2784 | r = job_deserialize(j, f, fds); | |
2785 | if (r < 0) { | |
2786 | job_free(j); | |
2787 | return r; | |
2788 | } | |
cca098b0 | 2789 | |
39a18c60 MS |
2790 | r = hashmap_put(u->manager->jobs, UINT32_TO_PTR(j->id), j); |
2791 | if (r < 0) { | |
2792 | job_free(j); | |
2793 | return r; | |
2794 | } | |
e0209d83 MS |
2795 | |
2796 | r = job_install_deserialized(j); | |
2797 | if (r < 0) { | |
2798 | hashmap_remove(u->manager->jobs, UINT32_TO_PTR(j->id)); | |
2799 | job_free(j); | |
2800 | return r; | |
2801 | } | |
ed10fa8c LP |
2802 | } else /* legacy for pre-44 */ |
2803 | log_unit_warning(u, "Update from too old systemd versions are unsupported, cannot deserialize job: %s", v); | |
cca098b0 | 2804 | continue; |
8aaf019b | 2805 | } else if (streq(l, "inactive-exit-timestamp")) { |
ac155bb8 | 2806 | dual_timestamp_deserialize(v, &u->inactive_exit_timestamp); |
8aaf019b LP |
2807 | continue; |
2808 | } else if (streq(l, "active-enter-timestamp")) { | |
ac155bb8 | 2809 | dual_timestamp_deserialize(v, &u->active_enter_timestamp); |
8aaf019b LP |
2810 | continue; |
2811 | } else if (streq(l, "active-exit-timestamp")) { | |
ac155bb8 | 2812 | dual_timestamp_deserialize(v, &u->active_exit_timestamp); |
8aaf019b LP |
2813 | continue; |
2814 | } else if (streq(l, "inactive-enter-timestamp")) { | |
ac155bb8 | 2815 | dual_timestamp_deserialize(v, &u->inactive_enter_timestamp); |
8aaf019b | 2816 | continue; |
2791a8f8 | 2817 | } else if (streq(l, "condition-timestamp")) { |
ac155bb8 | 2818 | dual_timestamp_deserialize(v, &u->condition_timestamp); |
2791a8f8 | 2819 | continue; |
59fccdc5 LP |
2820 | } else if (streq(l, "assert-timestamp")) { |
2821 | dual_timestamp_deserialize(v, &u->assert_timestamp); | |
2822 | continue; | |
2791a8f8 | 2823 | } else if (streq(l, "condition-result")) { |
2791a8f8 | 2824 | |
e911de99 LP |
2825 | r = parse_boolean(v); |
2826 | if (r < 0) | |
f2341e0a | 2827 | log_unit_debug(u, "Failed to parse condition result value %s, ignoring.", v); |
2791a8f8 | 2828 | else |
e911de99 | 2829 | u->condition_result = r; |
efbac6d2 LP |
2830 | |
2831 | continue; | |
c2756a68 | 2832 | |
59fccdc5 | 2833 | } else if (streq(l, "assert-result")) { |
59fccdc5 | 2834 | |
e911de99 LP |
2835 | r = parse_boolean(v); |
2836 | if (r < 0) | |
f2341e0a | 2837 | log_unit_debug(u, "Failed to parse assert result value %s, ignoring.", v); |
59fccdc5 | 2838 | else |
e911de99 | 2839 | u->assert_result = r; |
59fccdc5 LP |
2840 | |
2841 | continue; | |
2842 | ||
c2756a68 | 2843 | } else if (streq(l, "transient")) { |
c2756a68 | 2844 | |
e911de99 LP |
2845 | r = parse_boolean(v); |
2846 | if (r < 0) | |
f2341e0a | 2847 | log_unit_debug(u, "Failed to parse transient bool %s, ignoring.", v); |
c2756a68 | 2848 | else |
e911de99 | 2849 | u->transient = r; |
c2756a68 LP |
2850 | |
2851 | continue; | |
e911de99 | 2852 | |
5ad096b3 LP |
2853 | } else if (streq(l, "cpuacct-usage-base")) { |
2854 | ||
2855 | r = safe_atou64(v, &u->cpuacct_usage_base); | |
2856 | if (r < 0) | |
f2341e0a | 2857 | log_unit_debug(u, "Failed to parse CPU usage %s, ignoring.", v); |
5ad096b3 | 2858 | |
0f908397 | 2859 | continue; |
4e595329 | 2860 | |
e911de99 | 2861 | } else if (streq(l, "cgroup")) { |
72673e86 | 2862 | |
e911de99 LP |
2863 | r = unit_set_cgroup_path(u, v); |
2864 | if (r < 0) | |
f2341e0a | 2865 | log_unit_debug_errno(u, r, "Failed to set cgroup path %s, ignoring: %m", v); |
4e595329 | 2866 | |
de1d4f9b WF |
2867 | continue; |
2868 | } else if (streq(l, "cgroup-realized")) { | |
2869 | int b; | |
2870 | ||
2871 | b = parse_boolean(v); | |
2872 | if (b < 0) | |
2873 | log_unit_debug(u, "Failed to parse cgroup-realized bool %s, ignoring.", v); | |
2874 | else | |
2875 | u->cgroup_realized = b; | |
2876 | ||
c2756a68 | 2877 | continue; |
8aaf019b | 2878 | } |
cca098b0 | 2879 | |
9bdb98c5 LP |
2880 | if (unit_can_serialize(u)) { |
2881 | if (rt) { | |
f2341e0a | 2882 | r = exec_runtime_deserialize_item(u, rt, l, v, fds); |
e911de99 | 2883 | if (r < 0) { |
f2341e0a | 2884 | log_unit_warning(u, "Failed to deserialize runtime parameter '%s', ignoring.", l); |
e911de99 LP |
2885 | continue; |
2886 | } | |
2887 | ||
2888 | /* Returns positive if key was handled by the call */ | |
9bdb98c5 LP |
2889 | if (r > 0) |
2890 | continue; | |
2891 | } | |
2892 | ||
2893 | r = UNIT_VTABLE(u)->deserialize_item(u, l, v, fds); | |
613b411c | 2894 | if (r < 0) |
f2341e0a | 2895 | log_unit_warning(u, "Failed to deserialize unit parameter '%s', ignoring.", l); |
613b411c | 2896 | } |
a16e1123 LP |
2897 | } |
2898 | } | |
2899 | ||
6e2ef85b LP |
2900 | int unit_add_node_link(Unit *u, const char *what, bool wants) { |
2901 | Unit *device; | |
68eda4bd | 2902 | _cleanup_free_ char *e = NULL; |
6e2ef85b LP |
2903 | int r; |
2904 | ||
2905 | assert(u); | |
2906 | ||
6e2ef85b | 2907 | /* Adds in links to the device node that this unit is based on */ |
47bc12e1 LP |
2908 | if (isempty(what)) |
2909 | return 0; | |
6e2ef85b | 2910 | |
8407a5d0 | 2911 | if (!is_device_path(what)) |
6e2ef85b LP |
2912 | return 0; |
2913 | ||
47bc12e1 LP |
2914 | /* When device units aren't supported (such as in a |
2915 | * container), don't create dependencies on them. */ | |
1c2e9646 | 2916 | if (!unit_type_supported(UNIT_DEVICE)) |
47bc12e1 LP |
2917 | return 0; |
2918 | ||
7410616c LP |
2919 | r = unit_name_from_path(what, ".device", &e); |
2920 | if (r < 0) | |
2921 | return r; | |
6e2ef85b | 2922 | |
ac155bb8 | 2923 | r = manager_load_unit(u->manager, e, NULL, NULL, &device); |
6e2ef85b LP |
2924 | if (r < 0) |
2925 | return r; | |
2926 | ||
b2c23da8 | 2927 | r = unit_add_two_dependencies(u, UNIT_AFTER, u->manager->running_as == MANAGER_SYSTEM ? UNIT_BINDS_TO : UNIT_WANTS, device, true); |
faa368e3 | 2928 | if (r < 0) |
6e2ef85b LP |
2929 | return r; |
2930 | ||
faa368e3 LP |
2931 | if (wants) { |
2932 | r = unit_add_dependency(device, UNIT_WANTS, u, false); | |
2933 | if (r < 0) | |
6e2ef85b | 2934 | return r; |
faa368e3 | 2935 | } |
6e2ef85b LP |
2936 | |
2937 | return 0; | |
2938 | } | |
a16e1123 | 2939 | |
be847e82 | 2940 | int unit_coldplug(Unit *u) { |
cca098b0 LP |
2941 | int r; |
2942 | ||
2943 | assert(u); | |
2944 | ||
f78f265f LP |
2945 | /* Make sure we don't enter a loop, when coldplugging |
2946 | * recursively. */ | |
2947 | if (u->coldplugged) | |
2948 | return 0; | |
2949 | ||
2950 | u->coldplugged = true; | |
2951 | ||
f78f265f LP |
2952 | if (UNIT_VTABLE(u)->coldplug) { |
2953 | r = UNIT_VTABLE(u)->coldplug(u); | |
2954 | if (r < 0) | |
2955 | return r; | |
2956 | } | |
cca098b0 | 2957 | |
39a18c60 MS |
2958 | if (u->job) { |
2959 | r = job_coldplug(u->job); | |
2960 | if (r < 0) | |
2961 | return r; | |
be847e82 | 2962 | } |
cca098b0 LP |
2963 | |
2964 | return 0; | |
2965 | } | |
2966 | ||
49b1d377 | 2967 | void unit_status_printf(Unit *u, const char *status, const char *unit_status_msg_format) { |
bcfce235 | 2968 | DISABLE_WARNING_FORMAT_NONLITERAL; |
127d5fd1 ZJS |
2969 | manager_status_printf(u->manager, STATUS_TYPE_NORMAL, |
2970 | status, unit_status_msg_format, unit_description(u)); | |
bcfce235 | 2971 | REENABLE_WARNING; |
49b1d377 MS |
2972 | } |
2973 | ||
45fb0699 | 2974 | bool unit_need_daemon_reload(Unit *u) { |
ae7a7182 OS |
2975 | _cleanup_strv_free_ char **t = NULL; |
2976 | char **path; | |
1b64d026 | 2977 | struct stat st; |
ae7a7182 | 2978 | unsigned loaded_cnt, current_cnt; |
1b64d026 | 2979 | |
45fb0699 LP |
2980 | assert(u); |
2981 | ||
ac155bb8 | 2982 | if (u->fragment_path) { |
5f4b19f4 | 2983 | zero(st); |
ac155bb8 | 2984 | if (stat(u->fragment_path, &st) < 0) |
5f4b19f4 LP |
2985 | /* What, cannot access this anymore? */ |
2986 | return true; | |
45fb0699 | 2987 | |
ac155bb8 MS |
2988 | if (u->fragment_mtime > 0 && |
2989 | timespec_load(&st.st_mtim) != u->fragment_mtime) | |
5f4b19f4 LP |
2990 | return true; |
2991 | } | |
2992 | ||
1b64d026 LP |
2993 | if (u->source_path) { |
2994 | zero(st); | |
2995 | if (stat(u->source_path, &st) < 0) | |
2996 | return true; | |
2997 | ||
2998 | if (u->source_mtime > 0 && | |
2999 | timespec_load(&st.st_mtim) != u->source_mtime) | |
3000 | return true; | |
3001 | } | |
5f4b19f4 | 3002 | |
1a7f1b38 | 3003 | (void) unit_find_dropin_paths(u, &t); |
ae7a7182 OS |
3004 | loaded_cnt = strv_length(t); |
3005 | current_cnt = strv_length(u->dropin_paths); | |
3006 | ||
3007 | if (loaded_cnt == current_cnt) { | |
3008 | if (loaded_cnt == 0) | |
3009 | return false; | |
3010 | ||
3011 | if (strv_overlap(u->dropin_paths, t)) { | |
3012 | STRV_FOREACH(path, u->dropin_paths) { | |
3013 | zero(st); | |
3014 | if (stat(*path, &st) < 0) | |
3015 | return true; | |
3016 | ||
3017 | if (u->dropin_mtime > 0 && | |
3018 | timespec_load(&st.st_mtim) > u->dropin_mtime) | |
3019 | return true; | |
3020 | } | |
3021 | ||
3022 | return false; | |
3023 | } else | |
3024 | return true; | |
3025 | } else | |
3026 | return true; | |
45fb0699 LP |
3027 | } |
3028 | ||
fdf20a31 | 3029 | void unit_reset_failed(Unit *u) { |
5632e374 LP |
3030 | assert(u); |
3031 | ||
fdf20a31 MM |
3032 | if (UNIT_VTABLE(u)->reset_failed) |
3033 | UNIT_VTABLE(u)->reset_failed(u); | |
5632e374 LP |
3034 | } |
3035 | ||
a7f241db LP |
3036 | Unit *unit_following(Unit *u) { |
3037 | assert(u); | |
3038 | ||
3039 | if (UNIT_VTABLE(u)->following) | |
3040 | return UNIT_VTABLE(u)->following(u); | |
3041 | ||
3042 | return NULL; | |
3043 | } | |
3044 | ||
31afa0a4 | 3045 | bool unit_stop_pending(Unit *u) { |
18ffdfda LP |
3046 | assert(u); |
3047 | ||
31afa0a4 LP |
3048 | /* This call does check the current state of the unit. It's |
3049 | * hence useful to be called from state change calls of the | |
3050 | * unit itself, where the state isn't updated yet. This is | |
3051 | * different from unit_inactive_or_pending() which checks both | |
3052 | * the current state and for a queued job. */ | |
18ffdfda | 3053 | |
31afa0a4 LP |
3054 | return u->job && u->job->type == JOB_STOP; |
3055 | } | |
3056 | ||
3057 | bool unit_inactive_or_pending(Unit *u) { | |
3058 | assert(u); | |
3059 | ||
3060 | /* Returns true if the unit is inactive or going down */ | |
18ffdfda | 3061 | |
d956ac29 LP |
3062 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))) |
3063 | return true; | |
3064 | ||
31afa0a4 | 3065 | if (unit_stop_pending(u)) |
18ffdfda LP |
3066 | return true; |
3067 | ||
3068 | return false; | |
3069 | } | |
3070 | ||
31afa0a4 | 3071 | bool unit_active_or_pending(Unit *u) { |
f976f3f6 LP |
3072 | assert(u); |
3073 | ||
f60c2665 | 3074 | /* Returns true if the unit is active or going up */ |
f976f3f6 LP |
3075 | |
3076 | if (UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) | |
3077 | return true; | |
3078 | ||
ac155bb8 MS |
3079 | if (u->job && |
3080 | (u->job->type == JOB_START || | |
3081 | u->job->type == JOB_RELOAD_OR_START || | |
3082 | u->job->type == JOB_RESTART)) | |
f976f3f6 LP |
3083 | return true; |
3084 | ||
3085 | return false; | |
3086 | } | |
3087 | ||
718db961 | 3088 | int unit_kill(Unit *u, KillWho w, int signo, sd_bus_error *error) { |
8a0867d6 LP |
3089 | assert(u); |
3090 | assert(w >= 0 && w < _KILL_WHO_MAX); | |
8a0867d6 LP |
3091 | assert(signo > 0); |
3092 | assert(signo < _NSIG); | |
3093 | ||
8a0867d6 | 3094 | if (!UNIT_VTABLE(u)->kill) |
15411c0c | 3095 | return -EOPNOTSUPP; |
8a0867d6 | 3096 | |
c74f17d9 | 3097 | return UNIT_VTABLE(u)->kill(u, w, signo, error); |
8a0867d6 LP |
3098 | } |
3099 | ||
82659fd7 LP |
3100 | static Set *unit_pid_set(pid_t main_pid, pid_t control_pid) { |
3101 | Set *pid_set; | |
3102 | int r; | |
3103 | ||
d5099efc | 3104 | pid_set = set_new(NULL); |
82659fd7 LP |
3105 | if (!pid_set) |
3106 | return NULL; | |
3107 | ||
3108 | /* Exclude the main/control pids from being killed via the cgroup */ | |
3109 | if (main_pid > 0) { | |
3110 | r = set_put(pid_set, LONG_TO_PTR(main_pid)); | |
3111 | if (r < 0) | |
3112 | goto fail; | |
3113 | } | |
3114 | ||
3115 | if (control_pid > 0) { | |
3116 | r = set_put(pid_set, LONG_TO_PTR(control_pid)); | |
3117 | if (r < 0) | |
3118 | goto fail; | |
3119 | } | |
3120 | ||
3121 | return pid_set; | |
3122 | ||
3123 | fail: | |
3124 | set_free(pid_set); | |
3125 | return NULL; | |
3126 | } | |
3127 | ||
d91c34f2 LP |
3128 | int unit_kill_common( |
3129 | Unit *u, | |
3130 | KillWho who, | |
3131 | int signo, | |
3132 | pid_t main_pid, | |
3133 | pid_t control_pid, | |
718db961 | 3134 | sd_bus_error *error) { |
d91c34f2 | 3135 | |
814cc562 MS |
3136 | int r = 0; |
3137 | ||
52f448c3 | 3138 | if (who == KILL_MAIN) { |
814cc562 | 3139 | if (main_pid < 0) |
7358dc02 | 3140 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no main processes", unit_type_to_string(u->type)); |
52f448c3 | 3141 | else if (main_pid == 0) |
7358dc02 | 3142 | return sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill"); |
814cc562 MS |
3143 | } |
3144 | ||
52f448c3 | 3145 | if (who == KILL_CONTROL) { |
814cc562 | 3146 | if (control_pid < 0) |
7358dc02 | 3147 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no control processes", unit_type_to_string(u->type)); |
52f448c3 | 3148 | else if (control_pid == 0) |
7358dc02 | 3149 | return sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill"); |
814cc562 MS |
3150 | } |
3151 | ||
3152 | if (who == KILL_CONTROL || who == KILL_ALL) | |
3153 | if (control_pid > 0) | |
3154 | if (kill(control_pid, signo) < 0) | |
3155 | r = -errno; | |
3156 | ||
3157 | if (who == KILL_MAIN || who == KILL_ALL) | |
3158 | if (main_pid > 0) | |
3159 | if (kill(main_pid, signo) < 0) | |
3160 | r = -errno; | |
3161 | ||
4ad49000 | 3162 | if (who == KILL_ALL && u->cgroup_path) { |
814cc562 MS |
3163 | _cleanup_set_free_ Set *pid_set = NULL; |
3164 | int q; | |
3165 | ||
82659fd7 LP |
3166 | /* Exclude the main/control pids from being killed via the cgroup */ |
3167 | pid_set = unit_pid_set(main_pid, control_pid); | |
814cc562 MS |
3168 | if (!pid_set) |
3169 | return -ENOMEM; | |
3170 | ||
4ad49000 | 3171 | q = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, signo, false, true, false, pid_set); |
814cc562 MS |
3172 | if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT) |
3173 | r = q; | |
3174 | } | |
3175 | ||
3176 | return r; | |
3177 | } | |
3178 | ||
6210e7fc LP |
3179 | int unit_following_set(Unit *u, Set **s) { |
3180 | assert(u); | |
3181 | assert(s); | |
3182 | ||
3183 | if (UNIT_VTABLE(u)->following_set) | |
3184 | return UNIT_VTABLE(u)->following_set(u, s); | |
3185 | ||
3186 | *s = NULL; | |
3187 | return 0; | |
3188 | } | |
3189 | ||
a4375746 LP |
3190 | UnitFileState unit_get_unit_file_state(Unit *u) { |
3191 | assert(u); | |
3192 | ||
ac155bb8 MS |
3193 | if (u->unit_file_state < 0 && u->fragment_path) |
3194 | u->unit_file_state = unit_file_get_state( | |
b2c23da8 | 3195 | u->manager->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, |
2b6bf07d | 3196 | NULL, basename(u->fragment_path)); |
a4375746 | 3197 | |
ac155bb8 | 3198 | return u->unit_file_state; |
a4375746 LP |
3199 | } |
3200 | ||
d2dc52db LP |
3201 | int unit_get_unit_file_preset(Unit *u) { |
3202 | assert(u); | |
3203 | ||
3204 | if (u->unit_file_preset < 0 && u->fragment_path) | |
3205 | u->unit_file_preset = unit_file_query_preset( | |
b2c23da8 | 3206 | u->manager->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, |
d2dc52db LP |
3207 | NULL, basename(u->fragment_path)); |
3208 | ||
3209 | return u->unit_file_preset; | |
3210 | } | |
3211 | ||
57020a3a LP |
3212 | Unit* unit_ref_set(UnitRef *ref, Unit *u) { |
3213 | assert(ref); | |
3214 | assert(u); | |
3215 | ||
3216 | if (ref->unit) | |
3217 | unit_ref_unset(ref); | |
3218 | ||
3219 | ref->unit = u; | |
71fda00f | 3220 | LIST_PREPEND(refs, u->refs, ref); |
57020a3a LP |
3221 | return u; |
3222 | } | |
3223 | ||
3224 | void unit_ref_unset(UnitRef *ref) { | |
3225 | assert(ref); | |
3226 | ||
3227 | if (!ref->unit) | |
3228 | return; | |
3229 | ||
71fda00f | 3230 | LIST_REMOVE(refs, ref->unit->refs, ref); |
57020a3a LP |
3231 | ref->unit = NULL; |
3232 | } | |
3233 | ||
598459ce LP |
3234 | int unit_patch_contexts(Unit *u) { |
3235 | CGroupContext *cc; | |
3236 | ExecContext *ec; | |
cba6e062 LP |
3237 | unsigned i; |
3238 | int r; | |
3239 | ||
e06c73cc | 3240 | assert(u); |
e06c73cc | 3241 | |
598459ce LP |
3242 | /* Patch in the manager defaults into the exec and cgroup |
3243 | * contexts, _after_ the rest of the settings have been | |
3244 | * initialized */ | |
085afe36 | 3245 | |
598459ce LP |
3246 | ec = unit_get_exec_context(u); |
3247 | if (ec) { | |
3248 | /* This only copies in the ones that need memory */ | |
3249 | for (i = 0; i < _RLIMIT_MAX; i++) | |
3250 | if (u->manager->rlimit[i] && !ec->rlimit[i]) { | |
3251 | ec->rlimit[i] = newdup(struct rlimit, u->manager->rlimit[i], 1); | |
3252 | if (!ec->rlimit[i]) | |
3253 | return -ENOMEM; | |
3254 | } | |
3255 | ||
b2c23da8 | 3256 | if (u->manager->running_as == MANAGER_USER && |
598459ce LP |
3257 | !ec->working_directory) { |
3258 | ||
3259 | r = get_home_dir(&ec->working_directory); | |
3260 | if (r < 0) | |
3261 | return r; | |
4c08c824 LP |
3262 | |
3263 | /* Allow user services to run, even if the | |
3264 | * home directory is missing */ | |
3265 | ec->working_directory_missing_ok = true; | |
cba6e062 LP |
3266 | } |
3267 | ||
b2c23da8 | 3268 | if (u->manager->running_as == MANAGER_USER && |
598459ce LP |
3269 | (ec->syscall_whitelist || |
3270 | !set_isempty(ec->syscall_filter) || | |
3271 | !set_isempty(ec->syscall_archs) || | |
3272 | ec->address_families_whitelist || | |
3273 | !set_isempty(ec->address_families))) | |
3274 | ec->no_new_privileges = true; | |
e06c73cc | 3275 | |
598459ce LP |
3276 | if (ec->private_devices) |
3277 | ec->capability_bounding_set_drop |= (uint64_t) 1ULL << (uint64_t) CAP_MKNOD; | |
cba6e062 LP |
3278 | } |
3279 | ||
598459ce LP |
3280 | cc = unit_get_cgroup_context(u); |
3281 | if (cc) { | |
f513e420 | 3282 | |
598459ce LP |
3283 | if (ec && |
3284 | ec->private_devices && | |
3285 | cc->device_policy == CGROUP_AUTO) | |
3286 | cc->device_policy = CGROUP_CLOSED; | |
3287 | } | |
f1660f96 | 3288 | |
cba6e062 | 3289 | return 0; |
e06c73cc LP |
3290 | } |
3291 | ||
3ef63c31 LP |
3292 | ExecContext *unit_get_exec_context(Unit *u) { |
3293 | size_t offset; | |
3294 | assert(u); | |
3295 | ||
598459ce LP |
3296 | if (u->type < 0) |
3297 | return NULL; | |
3298 | ||
3ef63c31 LP |
3299 | offset = UNIT_VTABLE(u)->exec_context_offset; |
3300 | if (offset <= 0) | |
3301 | return NULL; | |
3302 | ||
3303 | return (ExecContext*) ((uint8_t*) u + offset); | |
3304 | } | |
3305 | ||
718db961 LP |
3306 | KillContext *unit_get_kill_context(Unit *u) { |
3307 | size_t offset; | |
3308 | assert(u); | |
3309 | ||
598459ce LP |
3310 | if (u->type < 0) |
3311 | return NULL; | |
3312 | ||
718db961 LP |
3313 | offset = UNIT_VTABLE(u)->kill_context_offset; |
3314 | if (offset <= 0) | |
3315 | return NULL; | |
3316 | ||
3317 | return (KillContext*) ((uint8_t*) u + offset); | |
3318 | } | |
3319 | ||
4ad49000 LP |
3320 | CGroupContext *unit_get_cgroup_context(Unit *u) { |
3321 | size_t offset; | |
3322 | ||
598459ce LP |
3323 | if (u->type < 0) |
3324 | return NULL; | |
3325 | ||
4ad49000 LP |
3326 | offset = UNIT_VTABLE(u)->cgroup_context_offset; |
3327 | if (offset <= 0) | |
3328 | return NULL; | |
3329 | ||
3330 | return (CGroupContext*) ((uint8_t*) u + offset); | |
3331 | } | |
3332 | ||
613b411c LP |
3333 | ExecRuntime *unit_get_exec_runtime(Unit *u) { |
3334 | size_t offset; | |
3335 | ||
598459ce LP |
3336 | if (u->type < 0) |
3337 | return NULL; | |
3338 | ||
613b411c LP |
3339 | offset = UNIT_VTABLE(u)->exec_runtime_offset; |
3340 | if (offset <= 0) | |
3341 | return NULL; | |
3342 | ||
3343 | return *(ExecRuntime**) ((uint8_t*) u + offset); | |
3344 | } | |
3345 | ||
29686440 | 3346 | static int unit_drop_in_dir(Unit *u, UnitSetPropertiesMode mode, bool transient, char **dir) { |
3f5e8115 LP |
3347 | assert(u); |
3348 | ||
b2c23da8 | 3349 | if (u->manager->running_as == MANAGER_USER) { |
29686440 | 3350 | int r; |
26d04f86 | 3351 | |
718880ba SA |
3352 | if (mode == UNIT_PERSISTENT && !transient) |
3353 | r = user_config_home(dir); | |
3354 | else | |
4d5dec23 | 3355 | r = user_runtime_dir(dir); |
26d04f86 LP |
3356 | if (r == 0) |
3357 | return -ENOENT; | |
3f5e8115 | 3358 | |
29686440 ZJS |
3359 | return r; |
3360 | } | |
26d04f86 | 3361 | |
29686440 ZJS |
3362 | if (mode == UNIT_PERSISTENT && !transient) |
3363 | *dir = strdup("/etc/systemd/system"); | |
8e2af478 | 3364 | else |
29686440 ZJS |
3365 | *dir = strdup("/run/systemd/system"); |
3366 | if (!*dir) | |
71645aca LP |
3367 | return -ENOMEM; |
3368 | ||
26d04f86 | 3369 | return 0; |
71645aca LP |
3370 | } |
3371 | ||
3f5e8115 | 3372 | static int unit_drop_in_file(Unit *u, UnitSetPropertiesMode mode, const char *name, char **p, char **q) { |
29686440 ZJS |
3373 | _cleanup_free_ char *dir = NULL; |
3374 | int r; | |
3375 | ||
3376 | assert(u); | |
3377 | ||
3378 | r = unit_drop_in_dir(u, mode, u->transient, &dir); | |
3379 | if (r < 0) | |
3380 | return r; | |
3381 | ||
8eea8687 | 3382 | return drop_in_file(dir, u->id, 50, name, p, q); |
29686440 ZJS |
3383 | } |
3384 | ||
8e2af478 | 3385 | int unit_write_drop_in(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *data) { |
29686440 | 3386 | |
adb76a70 | 3387 | _cleanup_free_ char *dir = NULL, *p = NULL, *q = NULL; |
26d04f86 | 3388 | int r; |
71645aca LP |
3389 | |
3390 | assert(u); | |
3391 | ||
6d235724 | 3392 | if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) |
8e2af478 LP |
3393 | return 0; |
3394 | ||
29686440 | 3395 | r = unit_drop_in_dir(u, mode, u->transient, &dir); |
26d04f86 LP |
3396 | if (r < 0) |
3397 | return r; | |
71645aca | 3398 | |
adb76a70 WC |
3399 | r = write_drop_in(dir, u->id, 50, name, data); |
3400 | if (r < 0) | |
3401 | return r; | |
3402 | ||
3403 | r = drop_in_file(dir, u->id, 50, name, &p, &q); | |
3404 | if (r < 0) | |
3405 | return r; | |
3406 | ||
3407 | r = strv_extend(&u->dropin_paths, q); | |
3408 | if (r < 0) | |
3409 | return r; | |
3410 | ||
3411 | strv_sort(u->dropin_paths); | |
3412 | strv_uniq(u->dropin_paths); | |
3413 | ||
3414 | u->dropin_mtime = now(CLOCK_REALTIME); | |
3415 | ||
3416 | return 0; | |
26d04f86 | 3417 | } |
71645aca | 3418 | |
b9ec9359 LP |
3419 | int unit_write_drop_in_format(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *format, ...) { |
3420 | _cleanup_free_ char *p = NULL; | |
3421 | va_list ap; | |
3422 | int r; | |
3423 | ||
3424 | assert(u); | |
3425 | assert(name); | |
3426 | assert(format); | |
3427 | ||
6d235724 | 3428 | if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) |
b9ec9359 LP |
3429 | return 0; |
3430 | ||
3431 | va_start(ap, format); | |
3432 | r = vasprintf(&p, format, ap); | |
3433 | va_end(ap); | |
3434 | ||
3435 | if (r < 0) | |
3436 | return -ENOMEM; | |
3437 | ||
3438 | return unit_write_drop_in(u, mode, name, p); | |
3439 | } | |
3440 | ||
3441 | int unit_write_drop_in_private(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *data) { | |
b42defe3 LP |
3442 | _cleanup_free_ char *ndata = NULL; |
3443 | ||
3444 | assert(u); | |
3445 | assert(name); | |
3446 | assert(data); | |
3447 | ||
3448 | if (!UNIT_VTABLE(u)->private_section) | |
3449 | return -EINVAL; | |
3450 | ||
6d235724 | 3451 | if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) |
b9ec9359 LP |
3452 | return 0; |
3453 | ||
b42defe3 LP |
3454 | ndata = strjoin("[", UNIT_VTABLE(u)->private_section, "]\n", data, NULL); |
3455 | if (!ndata) | |
3456 | return -ENOMEM; | |
3457 | ||
3458 | return unit_write_drop_in(u, mode, name, ndata); | |
3459 | } | |
3460 | ||
b9ec9359 LP |
3461 | int unit_write_drop_in_private_format(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *format, ...) { |
3462 | _cleanup_free_ char *p = NULL; | |
3463 | va_list ap; | |
3464 | int r; | |
3465 | ||
3466 | assert(u); | |
3467 | assert(name); | |
3468 | assert(format); | |
3469 | ||
6d235724 | 3470 | if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) |
b9ec9359 LP |
3471 | return 0; |
3472 | ||
3473 | va_start(ap, format); | |
3474 | r = vasprintf(&p, format, ap); | |
3475 | va_end(ap); | |
3476 | ||
3477 | if (r < 0) | |
3478 | return -ENOMEM; | |
3479 | ||
3480 | return unit_write_drop_in_private(u, mode, name, p); | |
3481 | } | |
3482 | ||
8e2af478 | 3483 | int unit_remove_drop_in(Unit *u, UnitSetPropertiesMode mode, const char *name) { |
26d04f86 LP |
3484 | _cleanup_free_ char *p = NULL, *q = NULL; |
3485 | int r; | |
71645aca | 3486 | |
26d04f86 | 3487 | assert(u); |
71645aca | 3488 | |
6d235724 | 3489 | if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) |
8e2af478 LP |
3490 | return 0; |
3491 | ||
29686440 | 3492 | r = unit_drop_in_file(u, mode, name, &p, &q); |
6891529f ZJS |
3493 | if (r < 0) |
3494 | return r; | |
3495 | ||
71645aca | 3496 | if (unlink(q) < 0) |
241da328 | 3497 | r = errno == ENOENT ? 0 : -errno; |
26d04f86 | 3498 | else |
241da328 | 3499 | r = 1; |
71645aca LP |
3500 | |
3501 | rmdir(p); | |
26d04f86 | 3502 | return r; |
71645aca LP |
3503 | } |
3504 | ||
c2756a68 | 3505 | int unit_make_transient(Unit *u) { |
c2756a68 LP |
3506 | assert(u); |
3507 | ||
3f5e8115 LP |
3508 | if (!UNIT_VTABLE(u)->can_transient) |
3509 | return -EOPNOTSUPP; | |
3510 | ||
c2756a68 LP |
3511 | u->load_state = UNIT_STUB; |
3512 | u->load_error = 0; | |
3513 | u->transient = true; | |
3f5e8115 | 3514 | u->fragment_path = mfree(u->fragment_path); |
c2756a68 | 3515 | |
3f5e8115 | 3516 | return 0; |
c2756a68 LP |
3517 | } |
3518 | ||
cd2086fe LP |
3519 | int unit_kill_context( |
3520 | Unit *u, | |
3521 | KillContext *c, | |
db2cb23b | 3522 | KillOperation k, |
cd2086fe LP |
3523 | pid_t main_pid, |
3524 | pid_t control_pid, | |
3525 | bool main_pid_alien) { | |
3526 | ||
bc6aed7b | 3527 | int sig, wait_for_exit = false, r; |
cd2086fe LP |
3528 | |
3529 | assert(u); | |
3530 | assert(c); | |
3531 | ||
3532 | if (c->kill_mode == KILL_NONE) | |
3533 | return 0; | |
3534 | ||
db2cb23b UTL |
3535 | switch (k) { |
3536 | case KILL_KILL: | |
3537 | sig = SIGKILL; | |
3538 | break; | |
3539 | case KILL_ABORT: | |
3540 | sig = SIGABRT; | |
3541 | break; | |
3542 | case KILL_TERMINATE: | |
3543 | sig = c->kill_signal; | |
3544 | break; | |
3545 | default: | |
3546 | assert_not_reached("KillOperation unknown"); | |
3547 | } | |
cd2086fe LP |
3548 | |
3549 | if (main_pid > 0) { | |
3550 | r = kill_and_sigcont(main_pid, sig); | |
3551 | ||
3552 | if (r < 0 && r != -ESRCH) { | |
3553 | _cleanup_free_ char *comm = NULL; | |
3554 | get_process_comm(main_pid, &comm); | |
3555 | ||
f2341e0a | 3556 | log_unit_warning_errno(u, r, "Failed to kill main process " PID_FMT " (%s): %m", main_pid, strna(comm)); |
82659fd7 | 3557 | } else { |
bc6aed7b LP |
3558 | if (!main_pid_alien) |
3559 | wait_for_exit = true; | |
82659fd7 | 3560 | |
db2cb23b | 3561 | if (c->send_sighup && k != KILL_KILL) |
82659fd7 LP |
3562 | kill(main_pid, SIGHUP); |
3563 | } | |
cd2086fe LP |
3564 | } |
3565 | ||
3566 | if (control_pid > 0) { | |
3567 | r = kill_and_sigcont(control_pid, sig); | |
3568 | ||
3569 | if (r < 0 && r != -ESRCH) { | |
3570 | _cleanup_free_ char *comm = NULL; | |
3571 | get_process_comm(control_pid, &comm); | |
3572 | ||
f2341e0a | 3573 | log_unit_warning_errno(u, r, "Failed to kill control process " PID_FMT " (%s): %m", control_pid, strna(comm)); |
82659fd7 | 3574 | } else { |
cd2086fe | 3575 | wait_for_exit = true; |
82659fd7 | 3576 | |
db2cb23b | 3577 | if (c->send_sighup && k != KILL_KILL) |
82659fd7 LP |
3578 | kill(control_pid, SIGHUP); |
3579 | } | |
cd2086fe LP |
3580 | } |
3581 | ||
db2cb23b | 3582 | if ((c->kill_mode == KILL_CONTROL_GROUP || (c->kill_mode == KILL_MIXED && k == KILL_KILL)) && u->cgroup_path) { |
cd2086fe LP |
3583 | _cleanup_set_free_ Set *pid_set = NULL; |
3584 | ||
82659fd7 LP |
3585 | /* Exclude the main/control pids from being killed via the cgroup */ |
3586 | pid_set = unit_pid_set(main_pid, control_pid); | |
cd2086fe LP |
3587 | if (!pid_set) |
3588 | return -ENOMEM; | |
3589 | ||
4ad49000 | 3590 | r = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, sig, true, true, false, pid_set); |
cd2086fe LP |
3591 | if (r < 0) { |
3592 | if (r != -EAGAIN && r != -ESRCH && r != -ENOENT) | |
f2341e0a | 3593 | log_unit_warning_errno(u, r, "Failed to kill control group: %m"); |
82659fd7 | 3594 | } else if (r > 0) { |
bc6aed7b | 3595 | |
743970d2 LP |
3596 | /* FIXME: For now, we will not wait for the |
3597 | * cgroup members to die, simply because | |
3598 | * cgroup notification is unreliable. It | |
3599 | * doesn't work at all in containers, and | |
3600 | * outside of containers it can be confused | |
3601 | * easily by leaving directories in the | |
3602 | * cgroup. */ | |
3603 | ||
3604 | /* wait_for_exit = true; */ | |
58ea275a | 3605 | |
db2cb23b | 3606 | if (c->send_sighup && k != KILL_KILL) { |
82659fd7 LP |
3607 | set_free(pid_set); |
3608 | ||
3609 | pid_set = unit_pid_set(main_pid, control_pid); | |
3610 | if (!pid_set) | |
3611 | return -ENOMEM; | |
3612 | ||
8190da36 | 3613 | cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, SIGHUP, false, true, false, pid_set); |
82659fd7 LP |
3614 | } |
3615 | } | |
cd2086fe LP |
3616 | } |
3617 | ||
3618 | return wait_for_exit; | |
3619 | } | |
3620 | ||
a57f7e2c LP |
3621 | int unit_require_mounts_for(Unit *u, const char *path) { |
3622 | char prefix[strlen(path) + 1], *p; | |
3623 | int r; | |
3624 | ||
3625 | assert(u); | |
3626 | assert(path); | |
3627 | ||
3628 | /* Registers a unit for requiring a certain path and all its | |
3629 | * prefixes. We keep a simple array of these paths in the | |
3630 | * unit, since its usually short. However, we build a prefix | |
3631 | * table for all possible prefixes so that new appearing mount | |
3632 | * units can easily determine which units to make themselves a | |
3633 | * dependency of. */ | |
3634 | ||
70b64bd3 ZJS |
3635 | if (!path_is_absolute(path)) |
3636 | return -EINVAL; | |
3637 | ||
a57f7e2c LP |
3638 | p = strdup(path); |
3639 | if (!p) | |
3640 | return -ENOMEM; | |
3641 | ||
3642 | path_kill_slashes(p); | |
3643 | ||
a57f7e2c LP |
3644 | if (!path_is_safe(p)) { |
3645 | free(p); | |
3646 | return -EPERM; | |
3647 | } | |
3648 | ||
3649 | if (strv_contains(u->requires_mounts_for, p)) { | |
3650 | free(p); | |
3651 | return 0; | |
3652 | } | |
3653 | ||
6e18964d ZJS |
3654 | r = strv_consume(&u->requires_mounts_for, p); |
3655 | if (r < 0) | |
a57f7e2c | 3656 | return r; |
a57f7e2c LP |
3657 | |
3658 | PATH_FOREACH_PREFIX_MORE(prefix, p) { | |
3659 | Set *x; | |
3660 | ||
3661 | x = hashmap_get(u->manager->units_requiring_mounts_for, prefix); | |
3662 | if (!x) { | |
3663 | char *q; | |
3664 | ||
742f41ad LP |
3665 | r = hashmap_ensure_allocated(&u->manager->units_requiring_mounts_for, &string_hash_ops); |
3666 | if (r < 0) | |
3667 | return r; | |
a57f7e2c LP |
3668 | |
3669 | q = strdup(prefix); | |
3670 | if (!q) | |
3671 | return -ENOMEM; | |
3672 | ||
d5099efc | 3673 | x = set_new(NULL); |
a57f7e2c LP |
3674 | if (!x) { |
3675 | free(q); | |
3676 | return -ENOMEM; | |
3677 | } | |
3678 | ||
3679 | r = hashmap_put(u->manager->units_requiring_mounts_for, q, x); | |
3680 | if (r < 0) { | |
3681 | free(q); | |
3682 | set_free(x); | |
3683 | return r; | |
3684 | } | |
3685 | } | |
3686 | ||
3687 | r = set_put(x, u); | |
3688 | if (r < 0) | |
3689 | return r; | |
3690 | } | |
3691 | ||
3692 | return 0; | |
3693 | } | |
3694 | ||
613b411c LP |
3695 | int unit_setup_exec_runtime(Unit *u) { |
3696 | ExecRuntime **rt; | |
3697 | size_t offset; | |
3698 | Iterator i; | |
3699 | Unit *other; | |
3700 | ||
3701 | offset = UNIT_VTABLE(u)->exec_runtime_offset; | |
3702 | assert(offset > 0); | |
3703 | ||
06b643e7 | 3704 | /* Check if there already is an ExecRuntime for this unit? */ |
613b411c LP |
3705 | rt = (ExecRuntime**) ((uint8_t*) u + offset); |
3706 | if (*rt) | |
3707 | return 0; | |
3708 | ||
3709 | /* Try to get it from somebody else */ | |
3710 | SET_FOREACH(other, u->dependencies[UNIT_JOINS_NAMESPACE_OF], i) { | |
3711 | ||
3712 | *rt = unit_get_exec_runtime(other); | |
3713 | if (*rt) { | |
3714 | exec_runtime_ref(*rt); | |
3715 | return 0; | |
3716 | } | |
3717 | } | |
3718 | ||
3719 | return exec_runtime_make(rt, unit_get_exec_context(u), u->id); | |
3720 | } | |
3721 | ||
1c2e9646 LP |
3722 | bool unit_type_supported(UnitType t) { |
3723 | if (_unlikely_(t < 0)) | |
3724 | return false; | |
3725 | if (_unlikely_(t >= _UNIT_TYPE_MAX)) | |
3726 | return false; | |
3727 | ||
3728 | if (!unit_vtable[t]->supported) | |
3729 | return true; | |
3730 | ||
3731 | return unit_vtable[t]->supported(); | |
3732 | } | |
3733 | ||
8b4305c7 LP |
3734 | void unit_warn_if_dir_nonempty(Unit *u, const char* where) { |
3735 | int r; | |
3736 | ||
3737 | assert(u); | |
3738 | assert(where); | |
3739 | ||
3740 | r = dir_is_empty(where); | |
3741 | if (r > 0) | |
3742 | return; | |
3743 | if (r < 0) { | |
3744 | log_unit_warning_errno(u, r, "Failed to check directory %s: %m", where); | |
3745 | return; | |
3746 | } | |
3747 | ||
3748 | log_struct(LOG_NOTICE, | |
3749 | LOG_MESSAGE_ID(SD_MESSAGE_OVERMOUNTING), | |
3750 | LOG_UNIT_ID(u), | |
3751 | LOG_UNIT_MESSAGE(u, "Directory %s to mount over is not empty, mounting anyway.", where), | |
3752 | "WHERE=%s", where, | |
3753 | NULL); | |
3754 | } | |
3755 | ||
3756 | int unit_fail_if_symlink(Unit *u, const char* where) { | |
3757 | int r; | |
3758 | ||
3759 | assert(u); | |
3760 | assert(where); | |
3761 | ||
3762 | r = is_symlink(where); | |
3763 | if (r < 0) { | |
3764 | log_unit_debug_errno(u, r, "Failed to check symlink %s, ignoring: %m", where); | |
3765 | return 0; | |
3766 | } | |
3767 | if (r == 0) | |
3768 | return 0; | |
3769 | ||
3770 | log_struct(LOG_ERR, | |
3771 | LOG_MESSAGE_ID(SD_MESSAGE_OVERMOUNTING), | |
3772 | LOG_UNIT_ID(u), | |
3773 | LOG_UNIT_MESSAGE(u, "Mount on symlink %s not allowed.", where), | |
3774 | "WHERE=%s", where, | |
3775 | NULL); | |
3776 | ||
3777 | return -ELOOP; | |
3778 | } | |
3779 | ||
94f04347 LP |
3780 | static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = { |
3781 | [UNIT_ACTIVE] = "active", | |
032ff4af | 3782 | [UNIT_RELOADING] = "reloading", |
94f04347 | 3783 | [UNIT_INACTIVE] = "inactive", |
fdf20a31 | 3784 | [UNIT_FAILED] = "failed", |
94f04347 LP |
3785 | [UNIT_ACTIVATING] = "activating", |
3786 | [UNIT_DEACTIVATING] = "deactivating" | |
3787 | }; | |
3788 | ||
3789 | DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState); |