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