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