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