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