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