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