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