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