]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
87f0e418 | 2 | |
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
87f0e418 LP |
22 | #include <assert.h> |
23 | #include <errno.h> | |
24 | #include <string.h> | |
25 | #include <sys/epoll.h> | |
26 | #include <sys/timerfd.h> | |
27 | #include <sys/poll.h> | |
0301abf4 LP |
28 | #include <stdlib.h> |
29 | #include <unistd.h> | |
45fb0699 | 30 | #include <sys/stat.h> |
87f0e418 LP |
31 | |
32 | #include "set.h" | |
33 | #include "unit.h" | |
34 | #include "macro.h" | |
35 | #include "strv.h" | |
36 | #include "load-fragment.h" | |
37 | #include "load-dropin.h" | |
38 | #include "log.h" | |
9e2f7c11 LP |
39 | #include "unit-name.h" |
40 | #include "specifier.h" | |
4139c1b2 | 41 | #include "dbus-unit.h" |
514f4ef5 | 42 | #include "special.h" |
c6c18be3 | 43 | #include "cgroup-util.h" |
4927fcae | 44 | #include "missing.h" |
ab1f0633 | 45 | #include "cgroup-attr.h" |
87f0e418 LP |
46 | |
47 | const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = { | |
48 | [UNIT_SERVICE] = &service_vtable, | |
49 | [UNIT_TIMER] = &timer_vtable, | |
50 | [UNIT_SOCKET] = &socket_vtable, | |
51 | [UNIT_TARGET] = &target_vtable, | |
52 | [UNIT_DEVICE] = &device_vtable, | |
53 | [UNIT_MOUNT] = &mount_vtable, | |
54 | [UNIT_AUTOMOUNT] = &automount_vtable, | |
07b0b134 | 55 | [UNIT_SNAPSHOT] = &snapshot_vtable, |
01f78473 LP |
56 | [UNIT_SWAP] = &swap_vtable, |
57 | [UNIT_PATH] = &path_vtable | |
87f0e418 LP |
58 | }; |
59 | ||
87f0e418 LP |
60 | Unit *unit_new(Manager *m) { |
61 | Unit *u; | |
62 | ||
63 | assert(m); | |
64 | ||
65 | if (!(u = new0(Unit, 1))) | |
66 | return NULL; | |
67 | ||
68 | if (!(u->meta.names = set_new(string_hash_func, string_compare_func))) { | |
69 | free(u); | |
70 | return NULL; | |
71 | } | |
72 | ||
73 | u->meta.manager = m; | |
74 | u->meta.type = _UNIT_TYPE_INVALID; | |
cca098b0 | 75 | u->meta.deserialized_job = _JOB_TYPE_INVALID; |
a40eb732 | 76 | u->meta.default_dependencies = true; |
a4375746 | 77 | u->meta.unit_file_state = _UNIT_FILE_STATE_INVALID; |
87f0e418 LP |
78 | |
79 | return u; | |
80 | } | |
81 | ||
f278026d LP |
82 | bool unit_has_name(Unit *u, const char *name) { |
83 | assert(u); | |
84 | assert(name); | |
85 | ||
86 | return !!set_get(u->meta.names, (char*) name); | |
87 | } | |
88 | ||
87f0e418 LP |
89 | int unit_add_name(Unit *u, const char *text) { |
90 | UnitType t; | |
276c3e78 | 91 | char *s, *i = NULL; |
87f0e418 LP |
92 | int r; |
93 | ||
94 | assert(u); | |
95 | assert(text); | |
96 | ||
9e2f7c11 LP |
97 | if (unit_name_is_template(text)) { |
98 | if (!u->meta.instance) | |
99 | return -EINVAL; | |
87f0e418 | 100 | |
9e2f7c11 LP |
101 | s = unit_name_replace_instance(text, u->meta.instance); |
102 | } else | |
103 | s = strdup(text); | |
87f0e418 | 104 | |
9e2f7c11 LP |
105 | if (!s) |
106 | return -ENOMEM; | |
87f0e418 | 107 | |
b9c0d441 | 108 | if (!unit_name_is_valid(s, false)) { |
9e2f7c11 LP |
109 | r = -EINVAL; |
110 | goto fail; | |
111 | } | |
e537352b | 112 | |
9e2f7c11 | 113 | assert_se((t = unit_name_to_type(s)) >= 0); |
87f0e418 | 114 | |
9e2f7c11 LP |
115 | if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type) { |
116 | r = -EINVAL; | |
117 | goto fail; | |
118 | } | |
87f0e418 | 119 | |
9e2f7c11 LP |
120 | if ((r = unit_name_to_instance(s, &i)) < 0) |
121 | goto fail; | |
87f0e418 | 122 | |
796ba554 LP |
123 | if (i && unit_vtable[t]->no_instances) { |
124 | r = -EINVAL; | |
9e2f7c11 | 125 | goto fail; |
796ba554 | 126 | } |
9e2f7c11 | 127 | |
276c3e78 LP |
128 | /* Ensure that this unit is either instanced or not instanced, |
129 | * but not both. */ | |
130 | if (u->meta.type != _UNIT_TYPE_INVALID && !u->meta.instance != !i) { | |
9e2f7c11 LP |
131 | r = -EINVAL; |
132 | goto fail; | |
133 | } | |
134 | ||
135 | if (unit_vtable[t]->no_alias && | |
136 | !set_isempty(u->meta.names) && | |
137 | !set_get(u->meta.names, s)) { | |
138 | r = -EEXIST; | |
139 | goto fail; | |
140 | } | |
141 | ||
4f0f902f LP |
142 | if (hashmap_size(u->meta.manager->units) >= MANAGER_MAX_NAMES) { |
143 | r = -E2BIG; | |
144 | goto fail; | |
145 | } | |
146 | ||
9e2f7c11 LP |
147 | if ((r = set_put(u->meta.names, s)) < 0) { |
148 | if (r == -EEXIST) | |
149 | r = 0; | |
150 | goto fail; | |
87f0e418 LP |
151 | } |
152 | ||
153 | if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) { | |
154 | set_remove(u->meta.names, s); | |
9e2f7c11 | 155 | goto fail; |
87f0e418 LP |
156 | } |
157 | ||
e537352b | 158 | if (u->meta.type == _UNIT_TYPE_INVALID) { |
ef734fd6 | 159 | |
e537352b | 160 | u->meta.type = t; |
9e2f7c11 LP |
161 | u->meta.id = s; |
162 | u->meta.instance = i; | |
163 | ||
ab5c3e3f | 164 | LIST_PREPEND(Meta, units_by_type, u->meta.manager->units_by_type[t], &u->meta); |
e537352b LP |
165 | |
166 | if (UNIT_VTABLE(u)->init) | |
167 | UNIT_VTABLE(u)->init(u); | |
9e2f7c11 LP |
168 | } else |
169 | free(i); | |
87f0e418 | 170 | |
c1e1601e | 171 | unit_add_to_dbus_queue(u); |
87f0e418 | 172 | return 0; |
9e2f7c11 LP |
173 | |
174 | fail: | |
175 | free(s); | |
176 | free(i); | |
177 | ||
178 | return r; | |
87f0e418 LP |
179 | } |
180 | ||
0ae97ec1 | 181 | int unit_choose_id(Unit *u, const char *name) { |
276c3e78 LP |
182 | char *s, *t = NULL, *i; |
183 | int r; | |
0ae97ec1 LP |
184 | |
185 | assert(u); | |
186 | assert(name); | |
187 | ||
9e2f7c11 LP |
188 | if (unit_name_is_template(name)) { |
189 | ||
190 | if (!u->meta.instance) | |
191 | return -EINVAL; | |
192 | ||
193 | if (!(t = unit_name_replace_instance(name, u->meta.instance))) | |
194 | return -ENOMEM; | |
195 | ||
196 | name = t; | |
197 | } | |
198 | ||
0ae97ec1 | 199 | /* Selects one of the names of this unit as the id */ |
9e2f7c11 LP |
200 | s = set_get(u->meta.names, (char*) name); |
201 | free(t); | |
0ae97ec1 | 202 | |
9e2f7c11 | 203 | if (!s) |
0ae97ec1 LP |
204 | return -ENOENT; |
205 | ||
276c3e78 LP |
206 | if ((r = unit_name_to_instance(s, &i)) < 0) |
207 | return r; | |
208 | ||
0ae97ec1 | 209 | u->meta.id = s; |
276c3e78 LP |
210 | |
211 | free(u->meta.instance); | |
212 | u->meta.instance = i; | |
213 | ||
c1e1601e | 214 | unit_add_to_dbus_queue(u); |
9e2f7c11 | 215 | |
0ae97ec1 LP |
216 | return 0; |
217 | } | |
218 | ||
f50e0a01 LP |
219 | int unit_set_description(Unit *u, const char *description) { |
220 | char *s; | |
221 | ||
222 | assert(u); | |
223 | ||
224 | if (!(s = strdup(description))) | |
225 | return -ENOMEM; | |
226 | ||
227 | free(u->meta.description); | |
228 | u->meta.description = s; | |
c1e1601e LP |
229 | |
230 | unit_add_to_dbus_queue(u); | |
f50e0a01 LP |
231 | return 0; |
232 | } | |
233 | ||
701cc384 LP |
234 | bool unit_check_gc(Unit *u) { |
235 | assert(u); | |
236 | ||
b86d44e5 LP |
237 | if (u->meta.load_state == UNIT_STUB) |
238 | return true; | |
239 | ||
701cc384 LP |
240 | if (UNIT_VTABLE(u)->no_gc) |
241 | return true; | |
242 | ||
6c073082 LP |
243 | if (u->meta.no_gc) |
244 | return true; | |
245 | ||
701cc384 LP |
246 | if (u->meta.job) |
247 | return true; | |
248 | ||
249 | if (unit_active_state(u) != UNIT_INACTIVE) | |
250 | return true; | |
251 | ||
252 | if (UNIT_VTABLE(u)->check_gc) | |
253 | if (UNIT_VTABLE(u)->check_gc(u)) | |
254 | return true; | |
255 | ||
256 | return false; | |
257 | } | |
258 | ||
87f0e418 LP |
259 | void unit_add_to_load_queue(Unit *u) { |
260 | assert(u); | |
819e213f | 261 | assert(u->meta.type != _UNIT_TYPE_INVALID); |
87f0e418 LP |
262 | |
263 | if (u->meta.load_state != UNIT_STUB || u->meta.in_load_queue) | |
264 | return; | |
265 | ||
266 | LIST_PREPEND(Meta, load_queue, u->meta.manager->load_queue, &u->meta); | |
267 | u->meta.in_load_queue = true; | |
268 | } | |
269 | ||
23a177ef LP |
270 | void unit_add_to_cleanup_queue(Unit *u) { |
271 | assert(u); | |
272 | ||
273 | if (u->meta.in_cleanup_queue) | |
274 | return; | |
275 | ||
276 | LIST_PREPEND(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta); | |
277 | u->meta.in_cleanup_queue = true; | |
278 | } | |
279 | ||
701cc384 LP |
280 | void unit_add_to_gc_queue(Unit *u) { |
281 | assert(u); | |
282 | ||
283 | if (u->meta.in_gc_queue || u->meta.in_cleanup_queue) | |
284 | return; | |
285 | ||
286 | if (unit_check_gc(u)) | |
287 | return; | |
288 | ||
289 | LIST_PREPEND(Meta, gc_queue, u->meta.manager->gc_queue, &u->meta); | |
290 | u->meta.in_gc_queue = true; | |
291 | ||
292 | u->meta.manager->n_in_gc_queue ++; | |
293 | ||
294 | if (u->meta.manager->gc_queue_timestamp <= 0) | |
295 | u->meta.manager->gc_queue_timestamp = now(CLOCK_MONOTONIC); | |
296 | } | |
297 | ||
c1e1601e LP |
298 | void unit_add_to_dbus_queue(Unit *u) { |
299 | assert(u); | |
819e213f | 300 | assert(u->meta.type != _UNIT_TYPE_INVALID); |
c1e1601e | 301 | |
94b6dfa2 | 302 | if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue) |
c1e1601e LP |
303 | return; |
304 | ||
a567261a LP |
305 | /* Shortcut things if nobody cares */ |
306 | if (!bus_has_subscriber(u->meta.manager)) { | |
94b6dfa2 LP |
307 | u->meta.sent_dbus_new_signal = true; |
308 | return; | |
309 | } | |
310 | ||
c1e1601e LP |
311 | LIST_PREPEND(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta); |
312 | u->meta.in_dbus_queue = true; | |
313 | } | |
314 | ||
87f0e418 LP |
315 | static void bidi_set_free(Unit *u, Set *s) { |
316 | Iterator i; | |
317 | Unit *other; | |
318 | ||
319 | assert(u); | |
320 | ||
321 | /* Frees the set and makes sure we are dropped from the | |
322 | * inverse pointers */ | |
323 | ||
324 | SET_FOREACH(other, s, i) { | |
325 | UnitDependency d; | |
326 | ||
327 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) | |
328 | set_remove(other->meta.dependencies[d], u); | |
701cc384 LP |
329 | |
330 | unit_add_to_gc_queue(other); | |
87f0e418 LP |
331 | } |
332 | ||
333 | set_free(s); | |
334 | } | |
335 | ||
336 | void unit_free(Unit *u) { | |
337 | UnitDependency d; | |
338 | Iterator i; | |
339 | char *t; | |
340 | ||
341 | assert(u); | |
342 | ||
c1e1601e LP |
343 | bus_unit_send_removed_signal(u); |
344 | ||
a013b84b LP |
345 | if (u->meta.load_state != UNIT_STUB) |
346 | if (UNIT_VTABLE(u)->done) | |
347 | UNIT_VTABLE(u)->done(u); | |
348 | ||
87f0e418 LP |
349 | SET_FOREACH(t, u->meta.names, i) |
350 | hashmap_remove_value(u->meta.manager->units, t, u); | |
351 | ||
964e0949 LP |
352 | if (u->meta.job) |
353 | job_free(u->meta.job); | |
354 | ||
355 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) | |
356 | bidi_set_free(u, u->meta.dependencies[d]); | |
357 | ||
ef734fd6 | 358 | if (u->meta.type != _UNIT_TYPE_INVALID) |
ab5c3e3f | 359 | LIST_REMOVE(Meta, units_by_type, u->meta.manager->units_by_type[u->meta.type], &u->meta); |
ef734fd6 | 360 | |
87f0e418 LP |
361 | if (u->meta.in_load_queue) |
362 | LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta); | |
363 | ||
c1e1601e LP |
364 | if (u->meta.in_dbus_queue) |
365 | LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta); | |
366 | ||
23a177ef LP |
367 | if (u->meta.in_cleanup_queue) |
368 | LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta); | |
369 | ||
701cc384 LP |
370 | if (u->meta.in_gc_queue) { |
371 | LIST_REMOVE(Meta, gc_queue, u->meta.manager->gc_queue, &u->meta); | |
372 | u->meta.manager->n_in_gc_queue--; | |
373 | } | |
374 | ||
a7556052 | 375 | cgroup_bonding_free_list(u->meta.cgroup_bondings, u->meta.manager->n_reloading <= 0); |
ab1f0633 | 376 | cgroup_attribute_free_list(u->meta.cgroup_attributes); |
87f0e418 | 377 | |
87f0e418 | 378 | free(u->meta.description); |
6be1e7d5 | 379 | free(u->meta.fragment_path); |
87f0e418 | 380 | |
53ec43c6 | 381 | set_free_free(u->meta.names); |
87f0e418 | 382 | |
52661efd LP |
383 | condition_free_list(u->meta.conditions); |
384 | ||
9e2f7c11 | 385 | free(u->meta.instance); |
87f0e418 LP |
386 | free(u); |
387 | } | |
388 | ||
389 | UnitActiveState unit_active_state(Unit *u) { | |
390 | assert(u); | |
391 | ||
6124958c LP |
392 | if (u->meta.load_state == UNIT_MERGED) |
393 | return unit_active_state(unit_follow_merge(u)); | |
394 | ||
395 | /* After a reload it might happen that a unit is not correctly | |
396 | * loaded but still has a process around. That's why we won't | |
fdf20a31 | 397 | * shortcut failed loading to UNIT_INACTIVE_FAILED. */ |
87f0e418 LP |
398 | |
399 | return UNIT_VTABLE(u)->active_state(u); | |
400 | } | |
401 | ||
10a94420 LP |
402 | const char* unit_sub_state_to_string(Unit *u) { |
403 | assert(u); | |
404 | ||
405 | return UNIT_VTABLE(u)->sub_state_to_string(u); | |
406 | } | |
407 | ||
23a177ef LP |
408 | static void complete_move(Set **s, Set **other) { |
409 | assert(s); | |
410 | assert(other); | |
87f0e418 | 411 | |
23a177ef LP |
412 | if (!*other) |
413 | return; | |
87f0e418 LP |
414 | |
415 | if (*s) | |
23a177ef LP |
416 | set_move(*s, *other); |
417 | else { | |
418 | *s = *other; | |
419 | *other = NULL; | |
420 | } | |
421 | } | |
87f0e418 | 422 | |
23a177ef LP |
423 | static void merge_names(Unit *u, Unit *other) { |
424 | char *t; | |
425 | Iterator i; | |
87f0e418 | 426 | |
23a177ef LP |
427 | assert(u); |
428 | assert(other); | |
429 | ||
430 | complete_move(&u->meta.names, &other->meta.names); | |
431 | ||
53ec43c6 | 432 | set_free_free(other->meta.names); |
23a177ef LP |
433 | other->meta.names = NULL; |
434 | other->meta.id = NULL; | |
435 | ||
436 | SET_FOREACH(t, u->meta.names, i) | |
437 | assert_se(hashmap_replace(u->meta.manager->units, t, u) == 0); | |
87f0e418 LP |
438 | } |
439 | ||
23a177ef LP |
440 | static void merge_dependencies(Unit *u, Unit *other, UnitDependency d) { |
441 | Iterator i; | |
442 | Unit *back; | |
87f0e418 | 443 | int r; |
23a177ef LP |
444 | |
445 | assert(u); | |
446 | assert(other); | |
447 | assert(d < _UNIT_DEPENDENCY_MAX); | |
448 | ||
83a95334 | 449 | /* Fix backwards pointers */ |
23a177ef LP |
450 | SET_FOREACH(back, other->meta.dependencies[d], i) { |
451 | UnitDependency k; | |
452 | ||
453 | for (k = 0; k < _UNIT_DEPENDENCY_MAX; k++) | |
454 | if ((r = set_remove_and_put(back->meta.dependencies[k], other, u)) < 0) { | |
455 | ||
456 | if (r == -EEXIST) | |
457 | set_remove(back->meta.dependencies[k], other); | |
458 | else | |
459 | assert(r == -ENOENT); | |
460 | } | |
461 | } | |
462 | ||
463 | complete_move(&u->meta.dependencies[d], &other->meta.dependencies[d]); | |
464 | ||
465 | set_free(other->meta.dependencies[d]); | |
466 | other->meta.dependencies[d] = NULL; | |
467 | } | |
468 | ||
469 | int unit_merge(Unit *u, Unit *other) { | |
87f0e418 LP |
470 | UnitDependency d; |
471 | ||
472 | assert(u); | |
473 | assert(other); | |
474 | assert(u->meta.manager == other->meta.manager); | |
9e2f7c11 | 475 | assert(u->meta.type != _UNIT_TYPE_INVALID); |
87f0e418 | 476 | |
cc916967 LP |
477 | other = unit_follow_merge(other); |
478 | ||
23a177ef LP |
479 | if (other == u) |
480 | return 0; | |
481 | ||
9e2f7c11 LP |
482 | if (u->meta.type != other->meta.type) |
483 | return -EINVAL; | |
484 | ||
276c3e78 | 485 | if (!u->meta.instance != !other->meta.instance) |
87f0e418 LP |
486 | return -EINVAL; |
487 | ||
cc916967 | 488 | if (other->meta.load_state != UNIT_STUB && |
fdf20a31 | 489 | other->meta.load_state != UNIT_ERROR) |
23a177ef | 490 | return -EEXIST; |
87f0e418 | 491 | |
819e213f LP |
492 | if (other->meta.job) |
493 | return -EEXIST; | |
494 | ||
fdf20a31 | 495 | if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) |
819e213f LP |
496 | return -EEXIST; |
497 | ||
87f0e418 | 498 | /* Merge names */ |
23a177ef | 499 | merge_names(u, other); |
87f0e418 LP |
500 | |
501 | /* Merge dependencies */ | |
502 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) | |
23a177ef | 503 | merge_dependencies(u, other, d); |
87f0e418 | 504 | |
23a177ef LP |
505 | other->meta.load_state = UNIT_MERGED; |
506 | other->meta.merged_into = u; | |
507 | ||
3616a49c LP |
508 | /* If there is still some data attached to the other node, we |
509 | * don't need it anymore, and can free it. */ | |
510 | if (other->meta.load_state != UNIT_STUB) | |
511 | if (UNIT_VTABLE(other)->done) | |
512 | UNIT_VTABLE(other)->done(other); | |
513 | ||
514 | unit_add_to_dbus_queue(u); | |
23a177ef LP |
515 | unit_add_to_cleanup_queue(other); |
516 | ||
517 | return 0; | |
518 | } | |
519 | ||
520 | int unit_merge_by_name(Unit *u, const char *name) { | |
521 | Unit *other; | |
9e2f7c11 LP |
522 | int r; |
523 | char *s = NULL; | |
23a177ef LP |
524 | |
525 | assert(u); | |
526 | assert(name); | |
527 | ||
9e2f7c11 LP |
528 | if (unit_name_is_template(name)) { |
529 | if (!u->meta.instance) | |
530 | return -EINVAL; | |
531 | ||
532 | if (!(s = unit_name_replace_instance(name, u->meta.instance))) | |
533 | return -ENOMEM; | |
534 | ||
535 | name = s; | |
536 | } | |
537 | ||
23a177ef | 538 | if (!(other = manager_get_unit(u->meta.manager, name))) |
9e2f7c11 LP |
539 | r = unit_add_name(u, name); |
540 | else | |
541 | r = unit_merge(u, other); | |
23a177ef | 542 | |
9e2f7c11 LP |
543 | free(s); |
544 | return r; | |
23a177ef LP |
545 | } |
546 | ||
547 | Unit* unit_follow_merge(Unit *u) { | |
548 | assert(u); | |
549 | ||
550 | while (u->meta.load_state == UNIT_MERGED) | |
551 | assert_se(u = u->meta.merged_into); | |
552 | ||
553 | return u; | |
554 | } | |
555 | ||
556 | int unit_add_exec_dependencies(Unit *u, ExecContext *c) { | |
557 | int r; | |
558 | ||
559 | assert(u); | |
560 | assert(c); | |
561 | ||
ecc6e2b8 LP |
562 | if (c->std_output != EXEC_OUTPUT_KMSG && |
563 | c->std_output != EXEC_OUTPUT_SYSLOG && | |
28dbc1e8 LP |
564 | c->std_output != EXEC_OUTPUT_KMSG_AND_CONSOLE && |
565 | c->std_output != EXEC_OUTPUT_SYSLOG_AND_CONSOLE && | |
ecc6e2b8 | 566 | c->std_error != EXEC_OUTPUT_KMSG && |
085c98af TJ |
567 | c->std_error != EXEC_OUTPUT_SYSLOG && |
568 | c->std_error != EXEC_OUTPUT_KMSG_AND_CONSOLE && | |
28dbc1e8 | 569 | c->std_error != EXEC_OUTPUT_SYSLOG_AND_CONSOLE) |
23a177ef LP |
570 | return 0; |
571 | ||
572 | /* If syslog or kernel logging is requested, make sure our own | |
573 | * logging daemon is run first. */ | |
574 | ||
a3d4e06d | 575 | if (u->meta.manager->running_as == MANAGER_SYSTEM) |
346bce1f | 576 | if ((r = unit_add_two_dependencies_by_name(u, UNIT_REQUIRES, UNIT_AFTER, SPECIAL_STDOUT_SYSLOG_BRIDGE_SOCKET, NULL, true)) < 0) |
23a177ef LP |
577 | return r; |
578 | ||
87f0e418 LP |
579 | return 0; |
580 | } | |
581 | ||
87f0e418 LP |
582 | const char *unit_description(Unit *u) { |
583 | assert(u); | |
584 | ||
585 | if (u->meta.description) | |
586 | return u->meta.description; | |
587 | ||
9f151f29 | 588 | return strna(u->meta.id); |
87f0e418 LP |
589 | } |
590 | ||
591 | void unit_dump(Unit *u, FILE *f, const char *prefix) { | |
87f0e418 LP |
592 | char *t; |
593 | UnitDependency d; | |
594 | Iterator i; | |
47be870b LP |
595 | char *p2; |
596 | const char *prefix2; | |
173e3821 LP |
597 | char |
598 | timestamp1[FORMAT_TIMESTAMP_MAX], | |
599 | timestamp2[FORMAT_TIMESTAMP_MAX], | |
600 | timestamp3[FORMAT_TIMESTAMP_MAX], | |
faf919f1 LP |
601 | timestamp4[FORMAT_TIMESTAMP_MAX], |
602 | timespan[FORMAT_TIMESPAN_MAX]; | |
a7f241db | 603 | Unit *following; |
87f0e418 LP |
604 | |
605 | assert(u); | |
9e2f7c11 | 606 | assert(u->meta.type >= 0); |
87f0e418 LP |
607 | |
608 | if (!prefix) | |
609 | prefix = ""; | |
47be870b LP |
610 | p2 = strappend(prefix, "\t"); |
611 | prefix2 = p2 ? p2 : prefix; | |
87f0e418 LP |
612 | |
613 | fprintf(f, | |
40d50879 | 614 | "%s-> Unit %s:\n" |
87f0e418 | 615 | "%s\tDescription: %s\n" |
9e2f7c11 | 616 | "%s\tInstance: %s\n" |
87f0e418 | 617 | "%s\tUnit Load State: %s\n" |
2fad8195 | 618 | "%s\tUnit Active State: %s\n" |
173e3821 | 619 | "%s\tInactive Exit Timestamp: %s\n" |
2fad8195 | 620 | "%s\tActive Enter Timestamp: %s\n" |
701cc384 | 621 | "%s\tActive Exit Timestamp: %s\n" |
173e3821 | 622 | "%s\tInactive Enter Timestamp: %s\n" |
45fb0699 LP |
623 | "%s\tGC Check Good: %s\n" |
624 | "%s\tNeed Daemon Reload: %s\n", | |
9e2f7c11 | 625 | prefix, u->meta.id, |
87f0e418 | 626 | prefix, unit_description(u), |
9e2f7c11 | 627 | prefix, strna(u->meta.instance), |
94f04347 | 628 | prefix, unit_load_state_to_string(u->meta.load_state), |
2fad8195 | 629 | prefix, unit_active_state_to_string(unit_active_state(u)), |
871d7de4 LP |
630 | prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->meta.inactive_exit_timestamp.realtime)), |
631 | prefix, strna(format_timestamp(timestamp2, sizeof(timestamp2), u->meta.active_enter_timestamp.realtime)), | |
632 | prefix, strna(format_timestamp(timestamp3, sizeof(timestamp3), u->meta.active_exit_timestamp.realtime)), | |
633 | prefix, strna(format_timestamp(timestamp4, sizeof(timestamp4), u->meta.inactive_enter_timestamp.realtime)), | |
45fb0699 LP |
634 | prefix, yes_no(unit_check_gc(u)), |
635 | prefix, yes_no(unit_need_daemon_reload(u))); | |
0301abf4 | 636 | |
87f0e418 LP |
637 | SET_FOREACH(t, u->meta.names, i) |
638 | fprintf(f, "%s\tName: %s\n", prefix, t); | |
639 | ||
a7f241db LP |
640 | if ((following = unit_following(u))) |
641 | fprintf(f, "%s\tFollowing: %s\n", prefix, following->meta.id); | |
8fe914ec | 642 | |
23a177ef LP |
643 | if (u->meta.fragment_path) |
644 | fprintf(f, "%s\tFragment Path: %s\n", prefix, u->meta.fragment_path); | |
645 | ||
faf919f1 LP |
646 | if (u->meta.job_timeout > 0) |
647 | fprintf(f, "%s\tJob Timeout: %s\n", prefix, format_timespan(timespan, sizeof(timespan), u->meta.job_timeout)); | |
648 | ||
52661efd LP |
649 | condition_dump_list(u->meta.conditions, f, prefix); |
650 | ||
2791a8f8 LP |
651 | if (dual_timestamp_is_set(&u->meta.condition_timestamp)) |
652 | fprintf(f, | |
653 | "%s\tCondition Timestamp: %s\n" | |
654 | "%s\tCondition Result: %s\n", | |
655 | prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->meta.condition_timestamp.realtime)), | |
656 | prefix, yes_no(u->meta.condition_result)); | |
657 | ||
87f0e418 LP |
658 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) { |
659 | Unit *other; | |
660 | ||
87f0e418 | 661 | SET_FOREACH(other, u->meta.dependencies[d], i) |
9e2f7c11 | 662 | fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), other->meta.id); |
87f0e418 LP |
663 | } |
664 | ||
23a177ef | 665 | if (u->meta.load_state == UNIT_LOADED) { |
ab1f0633 LP |
666 | CGroupBonding *b; |
667 | CGroupAttribute *a; | |
668 | ||
b0650475 | 669 | fprintf(f, |
a40eb732 | 670 | "%s\tStopWhenUnneeded: %s\n" |
b5e9dba8 LP |
671 | "%s\tRefuseManualStart: %s\n" |
672 | "%s\tRefuseManualStop: %s\n" | |
222ae6a8 | 673 | "%s\tDefaultDependencies: %s\n" |
c8f4d764 | 674 | "%s\tOnFailureIsolate: %s\n" |
7a6000a6 LP |
675 | "%s\tIgnoreOnIsolate: %s\n" |
676 | "%s\tIgnoreOnSnapshot: %s\n", | |
a40eb732 | 677 | prefix, yes_no(u->meta.stop_when_unneeded), |
b5e9dba8 LP |
678 | prefix, yes_no(u->meta.refuse_manual_start), |
679 | prefix, yes_no(u->meta.refuse_manual_stop), | |
222ae6a8 | 680 | prefix, yes_no(u->meta.default_dependencies), |
c8f4d764 | 681 | prefix, yes_no(u->meta.on_failure_isolate), |
7a6000a6 LP |
682 | prefix, yes_no(u->meta.ignore_on_isolate), |
683 | prefix, yes_no(u->meta.ignore_on_snapshot)); | |
b0650475 | 684 | |
23a177ef LP |
685 | LIST_FOREACH(by_unit, b, u->meta.cgroup_bondings) |
686 | fprintf(f, "%s\tControlGroup: %s:%s\n", | |
687 | prefix, b->controller, b->path); | |
8e274523 | 688 | |
d8bbda91 LP |
689 | LIST_FOREACH(by_unit, a, u->meta.cgroup_attributes) { |
690 | char *v = NULL; | |
691 | ||
692 | if (a->map_callback) | |
693 | a->map_callback(a->controller, a->name, a->value, &v); | |
694 | ||
ab1f0633 | 695 | fprintf(f, "%s\tControlGroupAttribute: %s %s \"%s\"\n", |
d8bbda91 LP |
696 | prefix, a->controller, a->name, v ? v : a->value); |
697 | ||
698 | free(v); | |
699 | } | |
ab1f0633 | 700 | |
23a177ef LP |
701 | if (UNIT_VTABLE(u)->dump) |
702 | UNIT_VTABLE(u)->dump(u, f, prefix2); | |
b0650475 LP |
703 | |
704 | } else if (u->meta.load_state == UNIT_MERGED) | |
705 | fprintf(f, | |
706 | "%s\tMerged into: %s\n", | |
9e2f7c11 | 707 | prefix, u->meta.merged_into->meta.id); |
fdf20a31 | 708 | else if (u->meta.load_state == UNIT_ERROR) |
8821a00f LP |
709 | fprintf(f, "%s\tLoad Error Code: %s\n", prefix, strerror(-u->meta.load_error)); |
710 | ||
87f0e418 LP |
711 | |
712 | if (u->meta.job) | |
713 | job_dump(u->meta.job, f, prefix2); | |
714 | ||
47be870b | 715 | free(p2); |
87f0e418 LP |
716 | } |
717 | ||
718 | /* Common implementation for multiple backends */ | |
e537352b | 719 | int unit_load_fragment_and_dropin(Unit *u) { |
23a177ef LP |
720 | int r; |
721 | ||
722 | assert(u); | |
23a177ef LP |
723 | |
724 | /* Load a .service file */ | |
e537352b | 725 | if ((r = unit_load_fragment(u)) < 0) |
23a177ef LP |
726 | return r; |
727 | ||
e537352b | 728 | if (u->meta.load_state == UNIT_STUB) |
23a177ef LP |
729 | return -ENOENT; |
730 | ||
731 | /* Load drop-in directory data */ | |
732 | if ((r = unit_load_dropin(unit_follow_merge(u))) < 0) | |
733 | return r; | |
734 | ||
735 | return 0; | |
736 | } | |
737 | ||
738 | /* Common implementation for multiple backends */ | |
e537352b | 739 | int unit_load_fragment_and_dropin_optional(Unit *u) { |
23a177ef | 740 | int r; |
87f0e418 LP |
741 | |
742 | assert(u); | |
743 | ||
23a177ef LP |
744 | /* Same as unit_load_fragment_and_dropin(), but whether |
745 | * something can be loaded or not doesn't matter. */ | |
746 | ||
747 | /* Load a .service file */ | |
e537352b | 748 | if ((r = unit_load_fragment(u)) < 0) |
87f0e418 LP |
749 | return r; |
750 | ||
e537352b LP |
751 | if (u->meta.load_state == UNIT_STUB) |
752 | u->meta.load_state = UNIT_LOADED; | |
d46de8a1 | 753 | |
87f0e418 | 754 | /* Load drop-in directory data */ |
23a177ef | 755 | if ((r = unit_load_dropin(unit_follow_merge(u))) < 0) |
87f0e418 LP |
756 | return r; |
757 | ||
23a177ef | 758 | return 0; |
87f0e418 LP |
759 | } |
760 | ||
bba34eed | 761 | int unit_add_default_target_dependency(Unit *u, Unit *target) { |
98bc2000 LP |
762 | assert(u); |
763 | assert(target); | |
764 | ||
765 | if (target->meta.type != UNIT_TARGET) | |
766 | return 0; | |
767 | ||
35b8ca3a | 768 | /* Only add the dependency if both units are loaded, so that |
bba34eed LP |
769 | * that loop check below is reliable */ |
770 | if (u->meta.load_state != UNIT_LOADED || | |
771 | target->meta.load_state != UNIT_LOADED) | |
772 | return 0; | |
773 | ||
21256a2b LP |
774 | /* If either side wants no automatic dependencies, then let's |
775 | * skip this */ | |
776 | if (!u->meta.default_dependencies || | |
a65cb51f | 777 | !target->meta.default_dependencies) |
21256a2b LP |
778 | return 0; |
779 | ||
98bc2000 LP |
780 | /* Don't create loops */ |
781 | if (set_get(target->meta.dependencies[UNIT_BEFORE], u)) | |
782 | return 0; | |
783 | ||
784 | return unit_add_dependency(target, UNIT_AFTER, u, true); | |
785 | } | |
786 | ||
787 | static int unit_add_default_dependencies(Unit *u) { | |
21256a2b LP |
788 | static const UnitDependency deps[] = { |
789 | UNIT_REQUIRED_BY, | |
790 | UNIT_REQUIRED_BY_OVERRIDABLE, | |
791 | UNIT_WANTED_BY, | |
792 | UNIT_BOUND_BY | |
793 | }; | |
794 | ||
bba34eed | 795 | Unit *target; |
98bc2000 LP |
796 | Iterator i; |
797 | int r; | |
21256a2b | 798 | unsigned k; |
98bc2000 LP |
799 | |
800 | assert(u); | |
801 | ||
21256a2b LP |
802 | for (k = 0; k < ELEMENTSOF(deps); k++) |
803 | SET_FOREACH(target, u->meta.dependencies[deps[k]], i) | |
804 | if ((r = unit_add_default_target_dependency(u, target)) < 0) | |
805 | return r; | |
b81884e7 | 806 | |
98bc2000 LP |
807 | return 0; |
808 | } | |
809 | ||
87f0e418 LP |
810 | int unit_load(Unit *u) { |
811 | int r; | |
812 | ||
813 | assert(u); | |
814 | ||
815 | if (u->meta.in_load_queue) { | |
816 | LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta); | |
817 | u->meta.in_load_queue = false; | |
818 | } | |
819 | ||
e537352b LP |
820 | if (u->meta.type == _UNIT_TYPE_INVALID) |
821 | return -EINVAL; | |
822 | ||
87f0e418 LP |
823 | if (u->meta.load_state != UNIT_STUB) |
824 | return 0; | |
825 | ||
e537352b LP |
826 | if (UNIT_VTABLE(u)->load) |
827 | if ((r = UNIT_VTABLE(u)->load(u)) < 0) | |
87f0e418 | 828 | goto fail; |
23a177ef | 829 | |
e537352b | 830 | if (u->meta.load_state == UNIT_STUB) { |
23a177ef LP |
831 | r = -ENOENT; |
832 | goto fail; | |
833 | } | |
834 | ||
98bc2000 LP |
835 | if (u->meta.load_state == UNIT_LOADED && |
836 | u->meta.default_dependencies) | |
837 | if ((r = unit_add_default_dependencies(u)) < 0) | |
838 | goto fail; | |
839 | ||
f68319bb LP |
840 | if (u->meta.on_failure_isolate && |
841 | set_size(u->meta.dependencies[UNIT_ON_FAILURE]) > 1) { | |
842 | ||
843 | log_error("More than one OnFailure= dependencies specified for %s but OnFailureIsolate= enabled. Refusing.", | |
844 | u->meta.id); | |
845 | ||
846 | r = -EINVAL; | |
847 | goto fail; | |
848 | } | |
849 | ||
23a177ef LP |
850 | assert((u->meta.load_state != UNIT_MERGED) == !u->meta.merged_into); |
851 | ||
852 | unit_add_to_dbus_queue(unit_follow_merge(u)); | |
701cc384 | 853 | unit_add_to_gc_queue(u); |
87f0e418 | 854 | |
87f0e418 LP |
855 | return 0; |
856 | ||
857 | fail: | |
fdf20a31 | 858 | u->meta.load_state = UNIT_ERROR; |
8821a00f | 859 | u->meta.load_error = r; |
c1e1601e | 860 | unit_add_to_dbus_queue(u); |
9a46fc3b | 861 | unit_add_to_gc_queue(u); |
23a177ef | 862 | |
8821a00f | 863 | log_debug("Failed to load configuration for %s: %s", u->meta.id, strerror(-r)); |
23a177ef | 864 | |
87f0e418 LP |
865 | return r; |
866 | } | |
867 | ||
90bbc946 LP |
868 | bool unit_condition_test(Unit *u) { |
869 | assert(u); | |
870 | ||
871 | dual_timestamp_get(&u->meta.condition_timestamp); | |
872 | u->meta.condition_result = condition_test_list(u->meta.conditions); | |
873 | ||
874 | return u->meta.condition_result; | |
875 | } | |
876 | ||
87f0e418 | 877 | /* Errors: |
d5159713 LP |
878 | * -EBADR: This unit type does not support starting. |
879 | * -EALREADY: Unit is already started. | |
880 | * -EAGAIN: An operation is already in progress. Retry later. | |
881 | * -ECANCELED: Too many requests for now. | |
87f0e418 LP |
882 | */ |
883 | int unit_start(Unit *u) { | |
884 | UnitActiveState state; | |
92ab323c | 885 | Unit *following; |
87f0e418 LP |
886 | |
887 | assert(u); | |
888 | ||
6124958c LP |
889 | if (u->meta.load_state != UNIT_LOADED) |
890 | return -EINVAL; | |
891 | ||
a82e5507 LP |
892 | /* If this is already started, then this will succeed. Note |
893 | * that this will even succeed if this unit is not startable | |
894 | * by the user. This is relied on to detect when we need to | |
895 | * wait for units and when waiting is finished. */ | |
87f0e418 LP |
896 | state = unit_active_state(u); |
897 | if (UNIT_IS_ACTIVE_OR_RELOADING(state)) | |
898 | return -EALREADY; | |
899 | ||
a82e5507 LP |
900 | /* If the conditions failed, don't do anything at all. If we |
901 | * already are activating this call might still be useful to | |
902 | * speed up activation in case there is some hold-off time, | |
903 | * but we don't want to recheck the condition in that case. */ | |
904 | if (state != UNIT_ACTIVATING && | |
905 | !unit_condition_test(u)) { | |
52661efd LP |
906 | log_debug("Starting of %s requested but condition failed. Ignoring.", u->meta.id); |
907 | return -EALREADY; | |
908 | } | |
909 | ||
92ab323c LP |
910 | /* Forward to the main object, if we aren't it. */ |
911 | if ((following = unit_following(u))) { | |
912 | log_debug("Redirecting start request from %s to %s.", u->meta.id, following->meta.id); | |
913 | return unit_start(following); | |
914 | } | |
915 | ||
916 | /* If it is stopped, but we cannot start it, then fail */ | |
917 | if (!UNIT_VTABLE(u)->start) | |
918 | return -EBADR; | |
919 | ||
87f0e418 LP |
920 | /* We don't suppress calls to ->start() here when we are |
921 | * already starting, to allow this request to be used as a | |
922 | * "hurry up" call, for example when the unit is in some "auto | |
923 | * restart" state where it waits for a holdoff timer to elapse | |
924 | * before it will start again. */ | |
925 | ||
c1e1601e | 926 | unit_add_to_dbus_queue(u); |
9e58ff9c | 927 | |
5831e9b7 | 928 | unit_status_printf(u, NULL, "Starting %s...", unit_description(u)); |
87f0e418 LP |
929 | return UNIT_VTABLE(u)->start(u); |
930 | } | |
931 | ||
932 | bool unit_can_start(Unit *u) { | |
933 | assert(u); | |
934 | ||
935 | return !!UNIT_VTABLE(u)->start; | |
936 | } | |
937 | ||
2528a7a6 LP |
938 | bool unit_can_isolate(Unit *u) { |
939 | assert(u); | |
940 | ||
941 | return unit_can_start(u) && | |
942 | u->meta.allow_isolate; | |
943 | } | |
944 | ||
87f0e418 LP |
945 | /* Errors: |
946 | * -EBADR: This unit type does not support stopping. | |
947 | * -EALREADY: Unit is already stopped. | |
948 | * -EAGAIN: An operation is already in progress. Retry later. | |
949 | */ | |
950 | int unit_stop(Unit *u) { | |
951 | UnitActiveState state; | |
92ab323c | 952 | Unit *following; |
87f0e418 LP |
953 | |
954 | assert(u); | |
955 | ||
87f0e418 | 956 | state = unit_active_state(u); |
fdf20a31 | 957 | if (UNIT_IS_INACTIVE_OR_FAILED(state)) |
87f0e418 LP |
958 | return -EALREADY; |
959 | ||
92ab323c LP |
960 | if ((following = unit_following(u))) { |
961 | log_debug("Redirecting stop request from %s to %s.", u->meta.id, following->meta.id); | |
962 | return unit_stop(following); | |
963 | } | |
964 | ||
7898b0cf LP |
965 | if (!UNIT_VTABLE(u)->stop) |
966 | return -EBADR; | |
967 | ||
c1e1601e | 968 | unit_add_to_dbus_queue(u); |
9e58ff9c | 969 | |
5831e9b7 | 970 | unit_status_printf(u, NULL, "Stopping %s...", unit_description(u)); |
87f0e418 LP |
971 | return UNIT_VTABLE(u)->stop(u); |
972 | } | |
973 | ||
974 | /* Errors: | |
975 | * -EBADR: This unit type does not support reloading. | |
976 | * -ENOEXEC: Unit is not started. | |
977 | * -EAGAIN: An operation is already in progress. Retry later. | |
978 | */ | |
979 | int unit_reload(Unit *u) { | |
980 | UnitActiveState state; | |
92ab323c | 981 | Unit *following; |
87f0e418 LP |
982 | |
983 | assert(u); | |
984 | ||
6124958c LP |
985 | if (u->meta.load_state != UNIT_LOADED) |
986 | return -EINVAL; | |
987 | ||
87f0e418 LP |
988 | if (!unit_can_reload(u)) |
989 | return -EBADR; | |
990 | ||
991 | state = unit_active_state(u); | |
e364ad06 | 992 | if (state == UNIT_RELOADING) |
87f0e418 LP |
993 | return -EALREADY; |
994 | ||
e364ad06 | 995 | if (state != UNIT_ACTIVE) |
87f0e418 LP |
996 | return -ENOEXEC; |
997 | ||
92ab323c LP |
998 | if ((following = unit_following(u))) { |
999 | log_debug("Redirecting reload request from %s to %s.", u->meta.id, following->meta.id); | |
1000 | return unit_reload(following); | |
1001 | } | |
1002 | ||
c1e1601e | 1003 | unit_add_to_dbus_queue(u); |
87f0e418 LP |
1004 | return UNIT_VTABLE(u)->reload(u); |
1005 | } | |
1006 | ||
1007 | bool unit_can_reload(Unit *u) { | |
1008 | assert(u); | |
1009 | ||
1010 | if (!UNIT_VTABLE(u)->reload) | |
1011 | return false; | |
1012 | ||
1013 | if (!UNIT_VTABLE(u)->can_reload) | |
1014 | return true; | |
1015 | ||
1016 | return UNIT_VTABLE(u)->can_reload(u); | |
1017 | } | |
1018 | ||
b4a16b7b | 1019 | static void unit_check_unneeded(Unit *u) { |
f3bff0eb LP |
1020 | Iterator i; |
1021 | Unit *other; | |
1022 | ||
1023 | assert(u); | |
1024 | ||
1025 | /* If this service shall be shut down when unneeded then do | |
1026 | * so. */ | |
1027 | ||
1028 | if (!u->meta.stop_when_unneeded) | |
1029 | return; | |
1030 | ||
1031 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) | |
1032 | return; | |
1033 | ||
1034 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i) | |
1035 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
1036 | return; | |
1037 | ||
9e2f7c11 | 1038 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i) |
f3bff0eb LP |
1039 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
1040 | return; | |
1041 | ||
1042 | SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i) | |
1043 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
1044 | return; | |
1045 | ||
b81884e7 LP |
1046 | SET_FOREACH(other, u->meta.dependencies[UNIT_BOUND_BY], i) |
1047 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
1048 | return; | |
1049 | ||
54165a39 | 1050 | log_info("Service %s is not needed anymore. Stopping.", u->meta.id); |
f3bff0eb LP |
1051 | |
1052 | /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */ | |
398ef8ba | 1053 | manager_add_job(u->meta.manager, JOB_STOP, u, JOB_FAIL, true, NULL, NULL); |
f3bff0eb LP |
1054 | } |
1055 | ||
87f0e418 LP |
1056 | static void retroactively_start_dependencies(Unit *u) { |
1057 | Iterator i; | |
1058 | Unit *other; | |
1059 | ||
1060 | assert(u); | |
1061 | assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))); | |
1062 | ||
1063 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i) | |
b81884e7 LP |
1064 | if (!set_get(u->meta.dependencies[UNIT_AFTER], other) && |
1065 | !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
1066 | manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL); | |
1067 | ||
1068 | SET_FOREACH(other, u->meta.dependencies[UNIT_BIND_TO], i) | |
1069 | if (!set_get(u->meta.dependencies[UNIT_AFTER], other) && | |
1070 | !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
398ef8ba | 1071 | manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL); |
87f0e418 | 1072 | |
9e2f7c11 | 1073 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i) |
b81884e7 LP |
1074 | if (!set_get(u->meta.dependencies[UNIT_AFTER], other) && |
1075 | !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
398ef8ba | 1076 | manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL, NULL); |
87f0e418 LP |
1077 | |
1078 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i) | |
b81884e7 LP |
1079 | if (!set_get(u->meta.dependencies[UNIT_AFTER], other) && |
1080 | !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
398ef8ba | 1081 | manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL); |
87f0e418 LP |
1082 | |
1083 | SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i) | |
b81884e7 LP |
1084 | if (!set_get(u->meta.dependencies[UNIT_AFTER], other) && |
1085 | !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
398ef8ba | 1086 | manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL, NULL); |
87f0e418 LP |
1087 | |
1088 | SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i) | |
b81884e7 | 1089 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
398ef8ba | 1090 | manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL); |
69dd2852 LP |
1091 | |
1092 | SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTED_BY], i) | |
b81884e7 | 1093 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
69dd2852 | 1094 | manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL); |
87f0e418 LP |
1095 | } |
1096 | ||
1097 | static void retroactively_stop_dependencies(Unit *u) { | |
1098 | Iterator i; | |
1099 | Unit *other; | |
1100 | ||
1101 | assert(u); | |
1102 | assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))); | |
1103 | ||
b81884e7 LP |
1104 | /* Pull down units which are bound to us recursively if enabled */ |
1105 | SET_FOREACH(other, u->meta.dependencies[UNIT_BOUND_BY], i) | |
1106 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
f14e15f8 | 1107 | manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL); |
f3bff0eb LP |
1108 | |
1109 | /* Garbage collect services that might not be needed anymore, if enabled */ | |
1110 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i) | |
87f0e418 | 1111 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
b4a16b7b | 1112 | unit_check_unneeded(other); |
9e2f7c11 | 1113 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i) |
f3bff0eb | 1114 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
b4a16b7b | 1115 | unit_check_unneeded(other); |
f3bff0eb LP |
1116 | SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i) |
1117 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
b4a16b7b | 1118 | unit_check_unneeded(other); |
f3bff0eb LP |
1119 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i) |
1120 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
b4a16b7b | 1121 | unit_check_unneeded(other); |
9e2f7c11 | 1122 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE_OVERRIDABLE], i) |
f3bff0eb | 1123 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
b4a16b7b | 1124 | unit_check_unneeded(other); |
b81884e7 LP |
1125 | SET_FOREACH(other, u->meta.dependencies[UNIT_BIND_TO], i) |
1126 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
1127 | unit_check_unneeded(other); | |
87f0e418 LP |
1128 | } |
1129 | ||
c0daa706 LP |
1130 | void unit_trigger_on_failure(Unit *u) { |
1131 | Unit *other; | |
1132 | Iterator i; | |
1133 | ||
1134 | assert(u); | |
1135 | ||
222ae6a8 LP |
1136 | if (set_size(u->meta.dependencies[UNIT_ON_FAILURE]) <= 0) |
1137 | return; | |
1138 | ||
1139 | log_info("Triggering OnFailure= dependencies of %s.", u->meta.id); | |
1140 | ||
1141 | SET_FOREACH(other, u->meta.dependencies[UNIT_ON_FAILURE], i) { | |
1142 | int r; | |
1143 | ||
1144 | if ((r = manager_add_job(u->meta.manager, JOB_START, other, u->meta.on_failure_isolate ? JOB_ISOLATE : JOB_REPLACE, true, NULL, NULL)) < 0) | |
1145 | log_error("Failed to enqueue OnFailure= job: %s", strerror(-r)); | |
1146 | } | |
c0daa706 LP |
1147 | } |
1148 | ||
e2f3b44c | 1149 | void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_success) { |
7e6e7b06 | 1150 | bool unexpected; |
a096ed36 | 1151 | |
87f0e418 LP |
1152 | assert(u); |
1153 | assert(os < _UNIT_ACTIVE_STATE_MAX); | |
1154 | assert(ns < _UNIT_ACTIVE_STATE_MAX); | |
87f0e418 | 1155 | |
8e471ccd LP |
1156 | /* Note that this is called for all low-level state changes, |
1157 | * even if they might map to the same high-level | |
1158 | * UnitActiveState! That means that ns == os is OK an expected | |
1159 | * behaviour here. For example: if a mount point is remounted | |
cd6d0a45 | 1160 | * this function will be called too! */ |
87f0e418 | 1161 | |
a7556052 | 1162 | if (u->meta.manager->n_reloading <= 0) { |
bdbf9951 | 1163 | dual_timestamp ts; |
173e3821 | 1164 | |
bdbf9951 | 1165 | dual_timestamp_get(&ts); |
173e3821 | 1166 | |
bdbf9951 LP |
1167 | if (UNIT_IS_INACTIVE_OR_FAILED(os) && !UNIT_IS_INACTIVE_OR_FAILED(ns)) |
1168 | u->meta.inactive_exit_timestamp = ts; | |
1169 | else if (!UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_INACTIVE_OR_FAILED(ns)) | |
1170 | u->meta.inactive_enter_timestamp = ts; | |
1171 | ||
1172 | if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns)) | |
1173 | u->meta.active_enter_timestamp = ts; | |
1174 | else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns)) | |
1175 | u->meta.active_exit_timestamp = ts; | |
1176 | ||
1177 | timer_unit_notify(u, ns); | |
1178 | path_unit_notify(u, ns); | |
1179 | } | |
87f0e418 | 1180 | |
fdf20a31 | 1181 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) |
fb385181 LP |
1182 | cgroup_bonding_trim_list(u->meta.cgroup_bondings, true); |
1183 | ||
87f0e418 | 1184 | if (u->meta.job) { |
7e6e7b06 | 1185 | unexpected = false; |
87f0e418 LP |
1186 | |
1187 | if (u->meta.job->state == JOB_WAITING) | |
1188 | ||
1189 | /* So we reached a different state for this | |
1190 | * job. Let's see if we can run it now if it | |
1191 | * failed previously due to EAGAIN. */ | |
c1e1601e | 1192 | job_add_to_run_queue(u->meta.job); |
87f0e418 | 1193 | |
b410e6b9 | 1194 | /* Let's check whether this state change constitutes a |
35b8ca3a | 1195 | * finished job, or maybe contradicts a running job and |
b410e6b9 | 1196 | * hence needs to invalidate jobs. */ |
87f0e418 | 1197 | |
b410e6b9 | 1198 | switch (u->meta.job->type) { |
87f0e418 | 1199 | |
b410e6b9 LP |
1200 | case JOB_START: |
1201 | case JOB_VERIFY_ACTIVE: | |
87f0e418 | 1202 | |
b410e6b9 | 1203 | if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) |
5d44db4a | 1204 | job_finish_and_invalidate(u->meta.job, JOB_DONE); |
b410e6b9 LP |
1205 | else if (u->meta.job->state == JOB_RUNNING && ns != UNIT_ACTIVATING) { |
1206 | unexpected = true; | |
41b02ec7 | 1207 | |
fdf20a31 | 1208 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) |
5d44db4a | 1209 | job_finish_and_invalidate(u->meta.job, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE); |
b410e6b9 | 1210 | } |
87f0e418 | 1211 | |
b410e6b9 | 1212 | break; |
87f0e418 | 1213 | |
b410e6b9 LP |
1214 | case JOB_RELOAD: |
1215 | case JOB_RELOAD_OR_START: | |
87f0e418 | 1216 | |
b410e6b9 | 1217 | if (u->meta.job->state == JOB_RUNNING) { |
a096ed36 | 1218 | if (ns == UNIT_ACTIVE) |
5d44db4a | 1219 | job_finish_and_invalidate(u->meta.job, reload_success ? JOB_DONE : JOB_FAILED); |
032ff4af | 1220 | else if (ns != UNIT_ACTIVATING && ns != UNIT_RELOADING) { |
a096ed36 | 1221 | unexpected = true; |
41b02ec7 | 1222 | |
fdf20a31 | 1223 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) |
5d44db4a | 1224 | job_finish_and_invalidate(u->meta.job, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE); |
a096ed36 | 1225 | } |
b410e6b9 | 1226 | } |
87f0e418 | 1227 | |
b410e6b9 | 1228 | break; |
87f0e418 | 1229 | |
b410e6b9 LP |
1230 | case JOB_STOP: |
1231 | case JOB_RESTART: | |
1232 | case JOB_TRY_RESTART: | |
87f0e418 | 1233 | |
fdf20a31 | 1234 | if (UNIT_IS_INACTIVE_OR_FAILED(ns)) |
5d44db4a | 1235 | job_finish_and_invalidate(u->meta.job, JOB_DONE); |
b410e6b9 LP |
1236 | else if (u->meta.job->state == JOB_RUNNING && ns != UNIT_DEACTIVATING) { |
1237 | unexpected = true; | |
5d44db4a | 1238 | job_finish_and_invalidate(u->meta.job, JOB_FAILED); |
b410e6b9 | 1239 | } |
87f0e418 | 1240 | |
b410e6b9 | 1241 | break; |
87f0e418 | 1242 | |
b410e6b9 LP |
1243 | default: |
1244 | assert_not_reached("Job type unknown"); | |
87f0e418 | 1245 | } |
87f0e418 | 1246 | |
7e6e7b06 LP |
1247 | } else |
1248 | unexpected = true; | |
1249 | ||
a7556052 | 1250 | if (u->meta.manager->n_reloading <= 0) { |
f3bff0eb | 1251 | |
bdbf9951 LP |
1252 | /* If this state change happened without being |
1253 | * requested by a job, then let's retroactively start | |
1254 | * or stop dependencies. We skip that step when | |
1255 | * deserializing, since we don't want to create any | |
1256 | * additional jobs just because something is already | |
1257 | * activated. */ | |
1258 | ||
1259 | if (unexpected) { | |
1260 | if (UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns)) | |
1261 | retroactively_start_dependencies(u); | |
1262 | else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns)) | |
1263 | retroactively_stop_dependencies(u); | |
1264 | } | |
5de9682c | 1265 | |
bdbf9951 LP |
1266 | if (ns != os && ns == UNIT_FAILED) { |
1267 | log_notice("Unit %s entered failed state.", u->meta.id); | |
1268 | unit_trigger_on_failure(u); | |
cd6d0a45 | 1269 | } |
3b2775c5 | 1270 | } |
e537352b | 1271 | |
3b2775c5 LP |
1272 | /* Some names are special */ |
1273 | if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) { | |
1274 | ||
1275 | if (unit_has_name(u, SPECIAL_DBUS_SERVICE)) | |
1276 | /* The bus just might have become available, | |
1277 | * hence try to connect to it, if we aren't | |
1278 | * yet connected. */ | |
1279 | bus_init(u->meta.manager, true); | |
1280 | ||
1281 | if (u->meta.type == UNIT_SERVICE && | |
1282 | !UNIT_IS_ACTIVE_OR_RELOADING(os) && | |
a7556052 | 1283 | u->meta.manager->n_reloading <= 0) { |
3b2775c5 LP |
1284 | /* Write audit record if we have just finished starting up */ |
1285 | manager_send_unit_audit(u->meta.manager, u, AUDIT_SERVICE_START, true); | |
1286 | u->meta.in_audit = true; | |
1287 | } | |
e983b760 | 1288 | |
3b2775c5 LP |
1289 | if (!UNIT_IS_ACTIVE_OR_RELOADING(os)) |
1290 | manager_send_unit_plymouth(u->meta.manager, u); | |
bdbf9951 | 1291 | |
3b2775c5 | 1292 | } else { |
bdbf9951 | 1293 | |
3b2775c5 LP |
1294 | /* We don't care about D-Bus here, since we'll get an |
1295 | * asynchronous notification for it anyway. */ | |
cd6d0a45 | 1296 | |
3b2775c5 LP |
1297 | if (u->meta.type == UNIT_SERVICE && |
1298 | UNIT_IS_INACTIVE_OR_FAILED(ns) && | |
1299 | !UNIT_IS_INACTIVE_OR_FAILED(os) && | |
a7556052 | 1300 | u->meta.manager->n_reloading <= 0) { |
4927fcae | 1301 | |
3b2775c5 LP |
1302 | /* Hmm, if there was no start record written |
1303 | * write it now, so that we always have a nice | |
1304 | * pair */ | |
1305 | if (!u->meta.in_audit) { | |
1306 | manager_send_unit_audit(u->meta.manager, u, AUDIT_SERVICE_START, ns == UNIT_INACTIVE); | |
cd6d0a45 | 1307 | |
3b2775c5 LP |
1308 | if (ns == UNIT_INACTIVE) |
1309 | manager_send_unit_audit(u->meta.manager, u, AUDIT_SERVICE_STOP, true); | |
1310 | } else | |
1311 | /* Write audit record if we have just finished shutting down */ | |
1312 | manager_send_unit_audit(u->meta.manager, u, AUDIT_SERVICE_STOP, ns == UNIT_INACTIVE); | |
bdbf9951 | 1313 | |
3b2775c5 | 1314 | u->meta.in_audit = false; |
cd6d0a45 | 1315 | } |
f278026d LP |
1316 | } |
1317 | ||
3b2775c5 LP |
1318 | manager_recheck_syslog(u->meta.manager); |
1319 | ||
f3bff0eb LP |
1320 | /* Maybe we finished startup and are now ready for being |
1321 | * stopped because unneeded? */ | |
b4a16b7b | 1322 | unit_check_unneeded(u); |
c1e1601e LP |
1323 | |
1324 | unit_add_to_dbus_queue(u); | |
701cc384 | 1325 | unit_add_to_gc_queue(u); |
87f0e418 LP |
1326 | } |
1327 | ||
acbb0225 | 1328 | int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) { |
87f0e418 LP |
1329 | struct epoll_event ev; |
1330 | ||
1331 | assert(u); | |
1332 | assert(fd >= 0); | |
acbb0225 | 1333 | assert(w); |
ea430986 | 1334 | assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u)); |
87f0e418 LP |
1335 | |
1336 | zero(ev); | |
acbb0225 | 1337 | ev.data.ptr = w; |
87f0e418 LP |
1338 | ev.events = events; |
1339 | ||
acbb0225 LP |
1340 | if (epoll_ctl(u->meta.manager->epoll_fd, |
1341 | w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD, | |
1342 | fd, | |
1343 | &ev) < 0) | |
1344 | return -errno; | |
87f0e418 | 1345 | |
acbb0225 LP |
1346 | w->fd = fd; |
1347 | w->type = WATCH_FD; | |
ea430986 | 1348 | w->data.unit = u; |
87f0e418 | 1349 | |
acbb0225 | 1350 | return 0; |
87f0e418 LP |
1351 | } |
1352 | ||
acbb0225 | 1353 | void unit_unwatch_fd(Unit *u, Watch *w) { |
87f0e418 | 1354 | assert(u); |
acbb0225 | 1355 | assert(w); |
87f0e418 | 1356 | |
acbb0225 LP |
1357 | if (w->type == WATCH_INVALID) |
1358 | return; | |
1359 | ||
ea430986 LP |
1360 | assert(w->type == WATCH_FD); |
1361 | assert(w->data.unit == u); | |
acbb0225 LP |
1362 | assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0); |
1363 | ||
1364 | w->fd = -1; | |
1365 | w->type = WATCH_INVALID; | |
ea430986 | 1366 | w->data.unit = NULL; |
87f0e418 LP |
1367 | } |
1368 | ||
1369 | int unit_watch_pid(Unit *u, pid_t pid) { | |
1370 | assert(u); | |
1371 | assert(pid >= 1); | |
1372 | ||
05e343b7 LP |
1373 | /* Watch a specific PID. We only support one unit watching |
1374 | * each PID for now. */ | |
1375 | ||
c6c18be3 | 1376 | return hashmap_put(u->meta.manager->watch_pids, LONG_TO_PTR(pid), u); |
87f0e418 LP |
1377 | } |
1378 | ||
1379 | void unit_unwatch_pid(Unit *u, pid_t pid) { | |
1380 | assert(u); | |
1381 | assert(pid >= 1); | |
1382 | ||
c6c18be3 | 1383 | hashmap_remove_value(u->meta.manager->watch_pids, LONG_TO_PTR(pid), u); |
87f0e418 LP |
1384 | } |
1385 | ||
acbb0225 | 1386 | int unit_watch_timer(Unit *u, usec_t delay, Watch *w) { |
87f0e418 | 1387 | struct itimerspec its; |
acbb0225 | 1388 | int flags, fd; |
87f0e418 LP |
1389 | bool ours; |
1390 | ||
1391 | assert(u); | |
acbb0225 | 1392 | assert(w); |
faf919f1 | 1393 | assert(w->type == WATCH_INVALID || (w->type == WATCH_UNIT_TIMER && w->data.unit == u)); |
87f0e418 LP |
1394 | |
1395 | /* This will try to reuse the old timer if there is one */ | |
1396 | ||
faf919f1 LP |
1397 | if (w->type == WATCH_UNIT_TIMER) { |
1398 | assert(w->data.unit == u); | |
1399 | assert(w->fd >= 0); | |
1400 | ||
87f0e418 | 1401 | ours = false; |
acbb0225 | 1402 | fd = w->fd; |
faf919f1 LP |
1403 | } else if (w->type == WATCH_INVALID) { |
1404 | ||
87f0e418 | 1405 | ours = true; |
87f0e418 LP |
1406 | if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0) |
1407 | return -errno; | |
faf919f1 LP |
1408 | } else |
1409 | assert_not_reached("Invalid watch type"); | |
87f0e418 LP |
1410 | |
1411 | zero(its); | |
1412 | ||
1413 | if (delay <= 0) { | |
1414 | /* Set absolute time in the past, but not 0, since we | |
1415 | * don't want to disarm the timer */ | |
1416 | its.it_value.tv_sec = 0; | |
1417 | its.it_value.tv_nsec = 1; | |
1418 | ||
1419 | flags = TFD_TIMER_ABSTIME; | |
1420 | } else { | |
1421 | timespec_store(&its.it_value, delay); | |
1422 | flags = 0; | |
1423 | } | |
1424 | ||
1425 | /* This will also flush the elapse counter */ | |
1426 | if (timerfd_settime(fd, flags, &its, NULL) < 0) | |
1427 | goto fail; | |
1428 | ||
acbb0225 LP |
1429 | if (w->type == WATCH_INVALID) { |
1430 | struct epoll_event ev; | |
87f0e418 | 1431 | |
acbb0225 LP |
1432 | zero(ev); |
1433 | ev.data.ptr = w; | |
f94ea366 | 1434 | ev.events = EPOLLIN; |
acbb0225 LP |
1435 | |
1436 | if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) | |
1437 | goto fail; | |
1438 | } | |
1439 | ||
faf919f1 | 1440 | w->type = WATCH_UNIT_TIMER; |
acbb0225 | 1441 | w->fd = fd; |
ea430986 | 1442 | w->data.unit = u; |
87f0e418 | 1443 | |
87f0e418 LP |
1444 | return 0; |
1445 | ||
1446 | fail: | |
1447 | if (ours) | |
ea430986 | 1448 | close_nointr_nofail(fd); |
87f0e418 LP |
1449 | |
1450 | return -errno; | |
1451 | } | |
1452 | ||
acbb0225 | 1453 | void unit_unwatch_timer(Unit *u, Watch *w) { |
87f0e418 | 1454 | assert(u); |
acbb0225 | 1455 | assert(w); |
87f0e418 | 1456 | |
acbb0225 | 1457 | if (w->type == WATCH_INVALID) |
87f0e418 LP |
1458 | return; |
1459 | ||
faf919f1 LP |
1460 | assert(w->type == WATCH_UNIT_TIMER); |
1461 | assert(w->data.unit == u); | |
1462 | assert(w->fd >= 0); | |
acbb0225 LP |
1463 | |
1464 | assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0); | |
a16e1123 | 1465 | close_nointr_nofail(w->fd); |
acbb0225 LP |
1466 | |
1467 | w->fd = -1; | |
1468 | w->type = WATCH_INVALID; | |
ea430986 | 1469 | w->data.unit = NULL; |
87f0e418 LP |
1470 | } |
1471 | ||
1472 | bool unit_job_is_applicable(Unit *u, JobType j) { | |
1473 | assert(u); | |
1474 | assert(j >= 0 && j < _JOB_TYPE_MAX); | |
1475 | ||
1476 | switch (j) { | |
1477 | ||
1478 | case JOB_VERIFY_ACTIVE: | |
1479 | case JOB_START: | |
57339f47 | 1480 | case JOB_STOP: |
87f0e418 LP |
1481 | return true; |
1482 | ||
87f0e418 LP |
1483 | case JOB_RESTART: |
1484 | case JOB_TRY_RESTART: | |
1485 | return unit_can_start(u); | |
1486 | ||
1487 | case JOB_RELOAD: | |
1488 | return unit_can_reload(u); | |
1489 | ||
1490 | case JOB_RELOAD_OR_START: | |
1491 | return unit_can_reload(u) && unit_can_start(u); | |
1492 | ||
1493 | default: | |
1494 | assert_not_reached("Invalid job type"); | |
1495 | } | |
1496 | } | |
1497 | ||
701cc384 | 1498 | int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference) { |
87f0e418 LP |
1499 | |
1500 | static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = { | |
1501 | [UNIT_REQUIRES] = UNIT_REQUIRED_BY, | |
9e2f7c11 | 1502 | [UNIT_REQUIRES_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE, |
87f0e418 LP |
1503 | [UNIT_WANTS] = UNIT_WANTED_BY, |
1504 | [UNIT_REQUISITE] = UNIT_REQUIRED_BY, | |
9e2f7c11 | 1505 | [UNIT_REQUISITE_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE, |
b81884e7 | 1506 | [UNIT_BIND_TO] = UNIT_BOUND_BY, |
87f0e418 | 1507 | [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID, |
9e2f7c11 | 1508 | [UNIT_REQUIRED_BY_OVERRIDABLE] = _UNIT_DEPENDENCY_INVALID, |
87f0e418 | 1509 | [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID, |
b81884e7 | 1510 | [UNIT_BOUND_BY] = UNIT_BIND_TO, |
69dd2852 LP |
1511 | [UNIT_CONFLICTS] = UNIT_CONFLICTED_BY, |
1512 | [UNIT_CONFLICTED_BY] = UNIT_CONFLICTS, | |
87f0e418 | 1513 | [UNIT_BEFORE] = UNIT_AFTER, |
701cc384 | 1514 | [UNIT_AFTER] = UNIT_BEFORE, |
5de9682c | 1515 | [UNIT_ON_FAILURE] = _UNIT_DEPENDENCY_INVALID, |
701cc384 LP |
1516 | [UNIT_REFERENCES] = UNIT_REFERENCED_BY, |
1517 | [UNIT_REFERENCED_BY] = UNIT_REFERENCES | |
87f0e418 | 1518 | }; |
701cc384 | 1519 | int r, q = 0, v = 0, w = 0; |
87f0e418 LP |
1520 | |
1521 | assert(u); | |
1522 | assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX); | |
87f0e418 LP |
1523 | assert(other); |
1524 | ||
9f151f29 LP |
1525 | u = unit_follow_merge(u); |
1526 | other = unit_follow_merge(other); | |
1527 | ||
87f0e418 LP |
1528 | /* We won't allow dependencies on ourselves. We will not |
1529 | * consider them an error however. */ | |
1530 | if (u == other) | |
1531 | return 0; | |
1532 | ||
5de9682c | 1533 | if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0) |
87f0e418 LP |
1534 | return r; |
1535 | ||
5de9682c LP |
1536 | if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID) |
1537 | if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0) | |
1538 | return r; | |
1539 | ||
701cc384 LP |
1540 | if (add_reference) |
1541 | if ((r = set_ensure_allocated(&u->meta.dependencies[UNIT_REFERENCES], trivial_hash_func, trivial_compare_func)) < 0 || | |
1542 | (r = set_ensure_allocated(&other->meta.dependencies[UNIT_REFERENCED_BY], trivial_hash_func, trivial_compare_func)) < 0) | |
1543 | return r; | |
87f0e418 | 1544 | |
701cc384 LP |
1545 | if ((q = set_put(u->meta.dependencies[d], other)) < 0) |
1546 | return q; | |
87f0e418 | 1547 | |
5de9682c LP |
1548 | if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID) |
1549 | if ((v = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) { | |
1550 | r = v; | |
1551 | goto fail; | |
1552 | } | |
701cc384 LP |
1553 | |
1554 | if (add_reference) { | |
1555 | if ((w = set_put(u->meta.dependencies[UNIT_REFERENCES], other)) < 0) { | |
1556 | r = w; | |
1557 | goto fail; | |
1558 | } | |
1559 | ||
1560 | if ((r = set_put(other->meta.dependencies[UNIT_REFERENCED_BY], u)) < 0) | |
1561 | goto fail; | |
87f0e418 LP |
1562 | } |
1563 | ||
c1e1601e | 1564 | unit_add_to_dbus_queue(u); |
87f0e418 | 1565 | return 0; |
701cc384 LP |
1566 | |
1567 | fail: | |
1568 | if (q > 0) | |
1569 | set_remove(u->meta.dependencies[d], other); | |
1570 | ||
1571 | if (v > 0) | |
1572 | set_remove(other->meta.dependencies[inverse_table[d]], u); | |
1573 | ||
1574 | if (w > 0) | |
1575 | set_remove(u->meta.dependencies[UNIT_REFERENCES], other); | |
1576 | ||
1577 | return r; | |
87f0e418 | 1578 | } |
0301abf4 | 1579 | |
2c966c03 LP |
1580 | int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference) { |
1581 | int r; | |
1582 | ||
1583 | assert(u); | |
1584 | ||
1585 | if ((r = unit_add_dependency(u, d, other, add_reference)) < 0) | |
1586 | return r; | |
1587 | ||
1588 | if ((r = unit_add_dependency(u, e, other, add_reference)) < 0) | |
1589 | return r; | |
1590 | ||
1591 | return 0; | |
1592 | } | |
1593 | ||
9e2f7c11 LP |
1594 | static const char *resolve_template(Unit *u, const char *name, const char*path, char **p) { |
1595 | char *s; | |
1596 | ||
1597 | assert(u); | |
1598 | assert(name || path); | |
1599 | ||
1600 | if (!name) | |
1601 | name = file_name_from_path(path); | |
1602 | ||
1603 | if (!unit_name_is_template(name)) { | |
1604 | *p = NULL; | |
1605 | return name; | |
1606 | } | |
1607 | ||
1608 | if (u->meta.instance) | |
1609 | s = unit_name_replace_instance(name, u->meta.instance); | |
1610 | else { | |
1611 | char *i; | |
1612 | ||
1613 | if (!(i = unit_name_to_prefix(u->meta.id))) | |
1614 | return NULL; | |
1615 | ||
1616 | s = unit_name_replace_instance(name, i); | |
1617 | free(i); | |
1618 | } | |
1619 | ||
1620 | if (!s) | |
1621 | return NULL; | |
1622 | ||
1623 | *p = s; | |
1624 | return s; | |
1625 | } | |
1626 | ||
701cc384 | 1627 | int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) { |
09b6b09f LP |
1628 | Unit *other; |
1629 | int r; | |
9e2f7c11 | 1630 | char *s; |
09b6b09f | 1631 | |
9e2f7c11 LP |
1632 | assert(u); |
1633 | assert(name || path); | |
09b6b09f | 1634 | |
9e2f7c11 LP |
1635 | if (!(name = resolve_template(u, name, path, &s))) |
1636 | return -ENOMEM; | |
09b6b09f | 1637 | |
398ef8ba | 1638 | if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0) |
9e2f7c11 LP |
1639 | goto finish; |
1640 | ||
701cc384 | 1641 | r = unit_add_dependency(u, d, other, add_reference); |
9e2f7c11 LP |
1642 | |
1643 | finish: | |
1644 | free(s); | |
1645 | return r; | |
09b6b09f LP |
1646 | } |
1647 | ||
2c966c03 LP |
1648 | int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) { |
1649 | Unit *other; | |
1650 | int r; | |
1651 | char *s; | |
1652 | ||
1653 | assert(u); | |
1654 | assert(name || path); | |
1655 | ||
1656 | if (!(name = resolve_template(u, name, path, &s))) | |
1657 | return -ENOMEM; | |
1658 | ||
398ef8ba | 1659 | if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0) |
2c966c03 LP |
1660 | goto finish; |
1661 | ||
1662 | r = unit_add_two_dependencies(u, d, e, other, add_reference); | |
1663 | ||
1664 | finish: | |
1665 | free(s); | |
1666 | return r; | |
1667 | } | |
1668 | ||
701cc384 | 1669 | int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) { |
bd77d0fc LP |
1670 | Unit *other; |
1671 | int r; | |
9e2f7c11 | 1672 | char *s; |
bd77d0fc | 1673 | |
9e2f7c11 LP |
1674 | assert(u); |
1675 | assert(name || path); | |
bd77d0fc | 1676 | |
9e2f7c11 LP |
1677 | if (!(name = resolve_template(u, name, path, &s))) |
1678 | return -ENOMEM; | |
bd77d0fc | 1679 | |
398ef8ba | 1680 | if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0) |
9e2f7c11 LP |
1681 | goto finish; |
1682 | ||
701cc384 | 1683 | r = unit_add_dependency(other, d, u, add_reference); |
9e2f7c11 LP |
1684 | |
1685 | finish: | |
1686 | free(s); | |
1687 | return r; | |
bd77d0fc LP |
1688 | } |
1689 | ||
2c966c03 LP |
1690 | int unit_add_two_dependencies_by_name_inverse(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) { |
1691 | Unit *other; | |
1692 | int r; | |
1693 | char *s; | |
1694 | ||
1695 | assert(u); | |
1696 | assert(name || path); | |
1697 | ||
1698 | if (!(name = resolve_template(u, name, path, &s))) | |
1699 | return -ENOMEM; | |
1700 | ||
398ef8ba | 1701 | if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0) |
2c966c03 LP |
1702 | goto finish; |
1703 | ||
1704 | if ((r = unit_add_two_dependencies(other, d, e, u, add_reference)) < 0) | |
1705 | goto finish; | |
1706 | ||
1707 | finish: | |
1708 | free(s); | |
1709 | return r; | |
1710 | } | |
1711 | ||
0301abf4 LP |
1712 | int set_unit_path(const char *p) { |
1713 | char *cwd, *c; | |
1714 | int r; | |
1715 | ||
1716 | /* This is mostly for debug purposes */ | |
1717 | ||
1718 | if (path_is_absolute(p)) { | |
1719 | if (!(c = strdup(p))) | |
1720 | return -ENOMEM; | |
1721 | } else { | |
1722 | if (!(cwd = get_current_dir_name())) | |
1723 | return -errno; | |
1724 | ||
1725 | r = asprintf(&c, "%s/%s", cwd, p); | |
1726 | free(cwd); | |
1727 | ||
1728 | if (r < 0) | |
1729 | return -ENOMEM; | |
1730 | } | |
1731 | ||
036643a2 | 1732 | if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0) { |
0301abf4 LP |
1733 | r = -errno; |
1734 | free(c); | |
1735 | return r; | |
1736 | } | |
1737 | ||
1738 | return 0; | |
1739 | } | |
88066b3a | 1740 | |
ea430986 LP |
1741 | char *unit_dbus_path(Unit *u) { |
1742 | char *p, *e; | |
1743 | ||
1744 | assert(u); | |
1745 | ||
04ade7d2 LP |
1746 | if (!u->meta.id) |
1747 | return NULL; | |
1748 | ||
9e2f7c11 | 1749 | if (!(e = bus_path_escape(u->meta.id))) |
ea430986 LP |
1750 | return NULL; |
1751 | ||
35d2e7ec | 1752 | p = strappend("/org/freedesktop/systemd1/unit/", e); |
ea430986 | 1753 | free(e); |
35d2e7ec | 1754 | |
ea430986 LP |
1755 | return p; |
1756 | } | |
1757 | ||
8e274523 | 1758 | int unit_add_cgroup(Unit *u, CGroupBonding *b) { |
8e274523 LP |
1759 | int r; |
1760 | ||
1761 | assert(u); | |
1762 | assert(b); | |
35d2e7ec | 1763 | |
8e274523 LP |
1764 | assert(b->path); |
1765 | ||
e025b4c3 | 1766 | if (!b->controller) { |
35d2e7ec LP |
1767 | if (!(b->controller = strdup(SYSTEMD_CGROUP_CONTROLLER))) |
1768 | return -ENOMEM; | |
1769 | ||
e025b4c3 LP |
1770 | b->ours = true; |
1771 | } | |
1772 | ||
8e274523 LP |
1773 | /* Ensure this hasn't been added yet */ |
1774 | assert(!b->unit); | |
1775 | ||
d686d8a9 LP |
1776 | if (streq(b->controller, SYSTEMD_CGROUP_CONTROLLER)) { |
1777 | CGroupBonding *l; | |
8e274523 | 1778 | |
d686d8a9 LP |
1779 | l = hashmap_get(u->meta.manager->cgroup_bondings, b->path); |
1780 | LIST_PREPEND(CGroupBonding, by_path, l, b); | |
1781 | ||
1782 | if ((r = hashmap_replace(u->meta.manager->cgroup_bondings, b->path, l)) < 0) { | |
1783 | LIST_REMOVE(CGroupBonding, by_path, l, b); | |
1784 | return r; | |
1785 | } | |
8e274523 LP |
1786 | } |
1787 | ||
1788 | LIST_PREPEND(CGroupBonding, by_unit, u->meta.cgroup_bondings, b); | |
1789 | b->unit = u; | |
1790 | ||
1791 | return 0; | |
1792 | } | |
1793 | ||
013b87c0 LP |
1794 | static char *default_cgroup_path(Unit *u) { |
1795 | char *p; | |
1796 | ||
1797 | assert(u); | |
1798 | ||
4f2d528d LP |
1799 | if (u->meta.instance) { |
1800 | char *t; | |
013b87c0 | 1801 | |
cd0ed1db LP |
1802 | t = unit_name_template(u->meta.id); |
1803 | if (!t) | |
4f2d528d LP |
1804 | return NULL; |
1805 | ||
cd0ed1db | 1806 | p = join(u->meta.manager->cgroup_hierarchy, "/", t, "/", u->meta.instance, NULL); |
4f2d528d LP |
1807 | free(t); |
1808 | } else | |
cd0ed1db | 1809 | p = join(u->meta.manager->cgroup_hierarchy, "/", u->meta.id, NULL); |
4f2d528d | 1810 | |
cd0ed1db | 1811 | return p; |
013b87c0 LP |
1812 | } |
1813 | ||
8e274523 | 1814 | int unit_add_cgroup_from_text(Unit *u, const char *name) { |
013b87c0 LP |
1815 | char *controller = NULL, *path = NULL; |
1816 | CGroupBonding *b = NULL; | |
e025b4c3 | 1817 | bool ours = false; |
8e274523 LP |
1818 | int r; |
1819 | ||
1820 | assert(u); | |
1821 | assert(name); | |
1822 | ||
35d2e7ec LP |
1823 | if ((r = cg_split_spec(name, &controller, &path)) < 0) |
1824 | return r; | |
8e274523 | 1825 | |
e025b4c3 | 1826 | if (!path) { |
35d2e7ec | 1827 | path = default_cgroup_path(u); |
e025b4c3 LP |
1828 | ours = true; |
1829 | } | |
013b87c0 | 1830 | |
e025b4c3 | 1831 | if (!controller) { |
55096547 | 1832 | controller = strdup(SYSTEMD_CGROUP_CONTROLLER); |
e025b4c3 LP |
1833 | ours = true; |
1834 | } | |
013b87c0 | 1835 | |
35d2e7ec LP |
1836 | if (!path || !controller) { |
1837 | free(path); | |
1838 | free(controller); | |
1839 | ||
1840 | return -ENOMEM; | |
8e274523 LP |
1841 | } |
1842 | ||
013b87c0 LP |
1843 | if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller)) { |
1844 | r = -EEXIST; | |
1845 | goto fail; | |
1846 | } | |
8e274523 | 1847 | |
013b87c0 | 1848 | if (!(b = new0(CGroupBonding, 1))) { |
8e274523 LP |
1849 | r = -ENOMEM; |
1850 | goto fail; | |
1851 | } | |
1852 | ||
013b87c0 LP |
1853 | b->controller = controller; |
1854 | b->path = path; | |
e025b4c3 LP |
1855 | b->ours = ours; |
1856 | b->essential = streq(controller, SYSTEMD_CGROUP_CONTROLLER); | |
8e274523 LP |
1857 | |
1858 | if ((r = unit_add_cgroup(u, b)) < 0) | |
1859 | goto fail; | |
1860 | ||
1861 | return 0; | |
1862 | ||
1863 | fail: | |
013b87c0 LP |
1864 | free(path); |
1865 | free(controller); | |
8e274523 LP |
1866 | free(b); |
1867 | ||
1868 | return r; | |
1869 | } | |
1870 | ||
06d4c99a | 1871 | static int unit_add_one_default_cgroup(Unit *u, const char *controller) { |
d686d8a9 | 1872 | CGroupBonding *b = NULL; |
8e274523 LP |
1873 | int r = -ENOMEM; |
1874 | ||
1875 | assert(u); | |
1876 | ||
06d4c99a LP |
1877 | if (!controller) |
1878 | controller = SYSTEMD_CGROUP_CONTROLLER; | |
8e274523 | 1879 | |
06d4c99a LP |
1880 | if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller)) |
1881 | return 0; | |
8e274523 | 1882 | |
06d4c99a LP |
1883 | if (!(b = new0(CGroupBonding, 1))) |
1884 | return -ENOMEM; | |
8e274523 | 1885 | |
06d4c99a LP |
1886 | if (!(b->controller = strdup(controller))) |
1887 | goto fail; | |
d686d8a9 | 1888 | |
06d4c99a LP |
1889 | if (!(b->path = default_cgroup_path(u))) |
1890 | goto fail; | |
d686d8a9 | 1891 | |
06d4c99a LP |
1892 | b->ours = true; |
1893 | b->essential = streq(controller, SYSTEMD_CGROUP_CONTROLLER); | |
d686d8a9 | 1894 | |
06d4c99a LP |
1895 | if ((r = unit_add_cgroup(u, b)) < 0) |
1896 | goto fail; | |
8e274523 LP |
1897 | |
1898 | return 0; | |
1899 | ||
1900 | fail: | |
06d4c99a LP |
1901 | free(b->path); |
1902 | free(b->controller); | |
1903 | free(b); | |
8e274523 LP |
1904 | |
1905 | return r; | |
1906 | } | |
1907 | ||
06d4c99a | 1908 | int unit_add_default_cgroups(Unit *u) { |
ab1f0633 | 1909 | CGroupAttribute *a; |
06d4c99a LP |
1910 | char **c; |
1911 | int r; | |
ab1f0633 | 1912 | |
06d4c99a LP |
1913 | assert(u); |
1914 | ||
1915 | /* Adds in the default cgroups, if they weren't specified | |
1916 | * otherwise. */ | |
1917 | ||
df18d8c8 LP |
1918 | if (!u->meta.manager->cgroup_hierarchy) |
1919 | return 0; | |
1920 | ||
06d4c99a LP |
1921 | if ((r = unit_add_one_default_cgroup(u, NULL)) < 0) |
1922 | return r; | |
1923 | ||
1924 | STRV_FOREACH(c, u->meta.manager->default_controllers) | |
ab1f0633 LP |
1925 | unit_add_one_default_cgroup(u, *c); |
1926 | ||
1927 | LIST_FOREACH(by_unit, a, u->meta.cgroup_attributes) | |
1928 | unit_add_one_default_cgroup(u, a->controller); | |
06d4c99a LP |
1929 | |
1930 | return 0; | |
1931 | } | |
1932 | ||
8e274523 LP |
1933 | CGroupBonding* unit_get_default_cgroup(Unit *u) { |
1934 | assert(u); | |
1935 | ||
55096547 | 1936 | return cgroup_bonding_find_list(u->meta.cgroup_bondings, SYSTEMD_CGROUP_CONTROLLER); |
8e274523 LP |
1937 | } |
1938 | ||
ab1f0633 LP |
1939 | int unit_add_cgroup_attribute(Unit *u, const char *controller, const char *name, const char *value, CGroupAttributeMapCallback map_callback) { |
1940 | int r; | |
1941 | char *c = NULL; | |
1942 | CGroupAttribute *a; | |
1943 | ||
1944 | assert(u); | |
1945 | assert(name); | |
1946 | assert(value); | |
1947 | ||
1948 | if (!controller) { | |
1949 | const char *dot; | |
1950 | ||
1951 | dot = strchr(name, '.'); | |
1952 | if (!dot) | |
1953 | return -EINVAL; | |
1954 | ||
1955 | c = strndup(name, dot - name); | |
1956 | if (!c) | |
1957 | return -ENOMEM; | |
1958 | ||
1959 | controller = c; | |
1960 | } | |
1961 | ||
1962 | if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) { | |
1963 | r = -EINVAL; | |
1964 | goto finish; | |
1965 | } | |
1966 | ||
1967 | a = new0(CGroupAttribute, 1); | |
1968 | if (!a) { | |
1969 | r = -ENOMEM; | |
1970 | goto finish; | |
1971 | } | |
1972 | ||
1973 | if (c) { | |
1974 | a->controller = c; | |
1975 | c = NULL; | |
1976 | } else | |
1977 | a->controller = strdup(controller); | |
1978 | ||
1979 | a->name = strdup(name); | |
1980 | a->value = strdup(value); | |
1981 | ||
1982 | if (!a->controller || !a->name || !a->value) { | |
1983 | free(a->controller); | |
1984 | free(a->name); | |
1985 | free(a->value); | |
1986 | free(a); | |
1987 | ||
1988 | return -ENOMEM; | |
1989 | } | |
1990 | ||
1991 | a->map_callback = map_callback; | |
1992 | ||
1993 | LIST_PREPEND(CGroupAttribute, by_unit, u->meta.cgroup_attributes, a); | |
1994 | ||
1995 | r = 0; | |
1996 | ||
1997 | finish: | |
1998 | free(c); | |
1999 | return r; | |
2000 | } | |
2001 | ||
f6ff8c29 LP |
2002 | int unit_load_related_unit(Unit *u, const char *type, Unit **_found) { |
2003 | char *t; | |
2004 | int r; | |
2005 | ||
2006 | assert(u); | |
2007 | assert(type); | |
2008 | assert(_found); | |
2009 | ||
9e2f7c11 | 2010 | if (!(t = unit_name_change_suffix(u->meta.id, type))) |
f6ff8c29 LP |
2011 | return -ENOMEM; |
2012 | ||
2013 | assert(!unit_has_name(u, t)); | |
2014 | ||
398ef8ba | 2015 | r = manager_load_unit(u->meta.manager, t, NULL, NULL, _found); |
f6ff8c29 LP |
2016 | free(t); |
2017 | ||
9e2f7c11 | 2018 | assert(r < 0 || *_found != u); |
f6ff8c29 LP |
2019 | |
2020 | return r; | |
2021 | } | |
2022 | ||
a16e1123 LP |
2023 | int unit_get_related_unit(Unit *u, const char *type, Unit **_found) { |
2024 | Unit *found; | |
2025 | char *t; | |
2026 | ||
2027 | assert(u); | |
2028 | assert(type); | |
2029 | assert(_found); | |
2030 | ||
2031 | if (!(t = unit_name_change_suffix(u->meta.id, type))) | |
2032 | return -ENOMEM; | |
2033 | ||
2034 | assert(!unit_has_name(u, t)); | |
2035 | ||
2036 | found = manager_get_unit(u->meta.manager, t); | |
2037 | free(t); | |
2038 | ||
2039 | if (!found) | |
2040 | return -ENOENT; | |
2041 | ||
2042 | *_found = found; | |
2043 | return 0; | |
2044 | } | |
2045 | ||
9e2f7c11 LP |
2046 | static char *specifier_prefix_and_instance(char specifier, void *data, void *userdata) { |
2047 | Unit *u = userdata; | |
2048 | assert(u); | |
2049 | ||
2050 | return unit_name_to_prefix_and_instance(u->meta.id); | |
2051 | } | |
2052 | ||
2053 | static char *specifier_prefix(char specifier, void *data, void *userdata) { | |
2054 | Unit *u = userdata; | |
2055 | assert(u); | |
2056 | ||
2057 | return unit_name_to_prefix(u->meta.id); | |
2058 | } | |
2059 | ||
2060 | static char *specifier_prefix_unescaped(char specifier, void *data, void *userdata) { | |
2061 | Unit *u = userdata; | |
2062 | char *p, *r; | |
2063 | ||
2064 | assert(u); | |
2065 | ||
2066 | if (!(p = unit_name_to_prefix(u->meta.id))) | |
2067 | return NULL; | |
2068 | ||
2069 | r = unit_name_unescape(p); | |
2070 | free(p); | |
2071 | ||
2072 | return r; | |
2073 | } | |
2074 | ||
2075 | static char *specifier_instance_unescaped(char specifier, void *data, void *userdata) { | |
2076 | Unit *u = userdata; | |
2077 | assert(u); | |
2078 | ||
2079 | if (u->meta.instance) | |
2080 | return unit_name_unescape(u->meta.instance); | |
2081 | ||
2082 | return strdup(""); | |
2083 | } | |
2084 | ||
9fc50704 LP |
2085 | static char *specifier_filename(char specifier, void *data, void *userdata) { |
2086 | Unit *u = userdata; | |
2087 | assert(u); | |
2088 | ||
2089 | if (u->meta.instance) | |
2090 | return unit_name_path_unescape(u->meta.instance); | |
2091 | ||
2092 | return unit_name_to_path(u->meta.instance); | |
2093 | } | |
2094 | ||
0aef4345 LP |
2095 | static char *specifier_cgroup(char specifier, void *data, void *userdata) { |
2096 | Unit *u = userdata; | |
2097 | assert(u); | |
2098 | ||
2099 | return default_cgroup_path(u); | |
2100 | } | |
2101 | ||
2102 | static char *specifier_cgroup_root(char specifier, void *data, void *userdata) { | |
2103 | Unit *u = userdata; | |
2104 | char *p; | |
2105 | assert(u); | |
2106 | ||
2107 | if (specifier == 'r') | |
2108 | return strdup(u->meta.manager->cgroup_hierarchy); | |
2109 | ||
2110 | if (parent_of_path(u->meta.manager->cgroup_hierarchy, &p) < 0) | |
2111 | return strdup(""); | |
2112 | ||
2113 | if (streq(p, "/")) { | |
2114 | free(p); | |
2115 | return strdup(""); | |
2116 | } | |
2117 | ||
2118 | return p; | |
2119 | } | |
2120 | ||
2121 | static char *specifier_runtime(char specifier, void *data, void *userdata) { | |
2122 | Unit *u = userdata; | |
2123 | assert(u); | |
2124 | ||
2125 | if (u->meta.manager->running_as == MANAGER_USER) { | |
2126 | const char *e; | |
2127 | ||
2128 | e = getenv("XDG_RUNTIME_DIR"); | |
2129 | if (e) | |
2130 | return strdup(e); | |
2131 | } | |
2132 | ||
2133 | return strdup("/run"); | |
2134 | } | |
2135 | ||
9e2f7c11 LP |
2136 | char *unit_name_printf(Unit *u, const char* format) { |
2137 | ||
2138 | /* | |
2139 | * This will use the passed string as format string and | |
2140 | * replace the following specifiers: | |
2141 | * | |
2142 | * %n: the full id of the unit (foo@bar.waldo) | |
2143 | * %N: the id of the unit without the suffix (foo@bar) | |
2144 | * %p: the prefix (foo) | |
2145 | * %i: the instance (bar) | |
2146 | */ | |
2147 | ||
2148 | const Specifier table[] = { | |
2149 | { 'n', specifier_string, u->meta.id }, | |
2150 | { 'N', specifier_prefix_and_instance, NULL }, | |
2151 | { 'p', specifier_prefix, NULL }, | |
2152 | { 'i', specifier_string, u->meta.instance }, | |
2153 | { 0, NULL, NULL } | |
2154 | }; | |
2155 | ||
2156 | assert(u); | |
2157 | assert(format); | |
2158 | ||
2159 | return specifier_printf(format, table, u); | |
2160 | } | |
2161 | ||
2162 | char *unit_full_printf(Unit *u, const char *format) { | |
2163 | ||
2164 | /* This is similar to unit_name_printf() but also supports | |
0aef4345 LP |
2165 | * unescaping. Also, adds a couple of additional codes: |
2166 | * | |
2167 | * %c cgroup path of unit | |
2168 | * %r root cgroup path of this systemd instance (e.g. "/user/lennart/shared/systemd-4711") | |
2169 | * %R parent of root cgroup path (e.g. "/usr/lennart/shared") | |
2170 | * %t the runtime directory to place sockets in (e.g. "/run" or $XDG_RUNTIME_DIR) | |
2171 | */ | |
9e2f7c11 LP |
2172 | |
2173 | const Specifier table[] = { | |
2174 | { 'n', specifier_string, u->meta.id }, | |
2175 | { 'N', specifier_prefix_and_instance, NULL }, | |
2176 | { 'p', specifier_prefix, NULL }, | |
2177 | { 'P', specifier_prefix_unescaped, NULL }, | |
2178 | { 'i', specifier_string, u->meta.instance }, | |
2179 | { 'I', specifier_instance_unescaped, NULL }, | |
9fc50704 | 2180 | { 'f', specifier_filename, NULL }, |
0aef4345 LP |
2181 | { 'c', specifier_cgroup, NULL }, |
2182 | { 'r', specifier_cgroup_root, NULL }, | |
2183 | { 'R', specifier_cgroup_root, NULL }, | |
2184 | { 't', specifier_runtime, NULL }, | |
9e2f7c11 LP |
2185 | { 0, NULL, NULL } |
2186 | }; | |
2187 | ||
2188 | assert(u); | |
2189 | assert(format); | |
2190 | ||
2191 | return specifier_printf(format, table, u); | |
2192 | } | |
2193 | ||
2194 | char **unit_full_printf_strv(Unit *u, char **l) { | |
2195 | size_t n; | |
2196 | char **r, **i, **j; | |
2197 | ||
2198 | /* Applies unit_full_printf to every entry in l */ | |
2199 | ||
2200 | assert(u); | |
2201 | ||
2202 | n = strv_length(l); | |
2203 | if (!(r = new(char*, n+1))) | |
2204 | return NULL; | |
2205 | ||
2206 | for (i = l, j = r; *i; i++, j++) | |
2207 | if (!(*j = unit_full_printf(u, *i))) | |
2208 | goto fail; | |
2209 | ||
2210 | *j = NULL; | |
2211 | return r; | |
2212 | ||
2213 | fail: | |
da19d5c1 | 2214 | for (j--; j >= r; j--) |
9e2f7c11 LP |
2215 | free(*j); |
2216 | ||
2217 | free(r); | |
2218 | ||
2219 | return NULL; | |
2220 | } | |
2221 | ||
05e343b7 LP |
2222 | int unit_watch_bus_name(Unit *u, const char *name) { |
2223 | assert(u); | |
2224 | assert(name); | |
2225 | ||
2226 | /* Watch a specific name on the bus. We only support one unit | |
2227 | * watching each name for now. */ | |
2228 | ||
2229 | return hashmap_put(u->meta.manager->watch_bus, name, u); | |
2230 | } | |
2231 | ||
2232 | void unit_unwatch_bus_name(Unit *u, const char *name) { | |
2233 | assert(u); | |
2234 | assert(name); | |
2235 | ||
2236 | hashmap_remove_value(u->meta.manager->watch_bus, name, u); | |
2237 | } | |
2238 | ||
a16e1123 LP |
2239 | bool unit_can_serialize(Unit *u) { |
2240 | assert(u); | |
2241 | ||
2242 | return UNIT_VTABLE(u)->serialize && UNIT_VTABLE(u)->deserialize_item; | |
2243 | } | |
2244 | ||
2245 | int unit_serialize(Unit *u, FILE *f, FDSet *fds) { | |
2246 | int r; | |
2247 | ||
2248 | assert(u); | |
2249 | assert(f); | |
2250 | assert(fds); | |
2251 | ||
2252 | if (!unit_can_serialize(u)) | |
2253 | return 0; | |
2254 | ||
2255 | if ((r = UNIT_VTABLE(u)->serialize(u, f, fds)) < 0) | |
2256 | return r; | |
2257 | ||
cca098b0 LP |
2258 | if (u->meta.job) |
2259 | unit_serialize_item(u, f, "job", job_type_to_string(u->meta.job->type)); | |
2260 | ||
10717a1a LP |
2261 | dual_timestamp_serialize(f, "inactive-exit-timestamp", &u->meta.inactive_exit_timestamp); |
2262 | dual_timestamp_serialize(f, "active-enter-timestamp", &u->meta.active_enter_timestamp); | |
2263 | dual_timestamp_serialize(f, "active-exit-timestamp", &u->meta.active_exit_timestamp); | |
2264 | dual_timestamp_serialize(f, "inactive-enter-timestamp", &u->meta.inactive_enter_timestamp); | |
2791a8f8 LP |
2265 | dual_timestamp_serialize(f, "condition-timestamp", &u->meta.condition_timestamp); |
2266 | ||
2267 | if (dual_timestamp_is_set(&u->meta.condition_timestamp)) | |
2268 | unit_serialize_item(u, f, "condition-result", yes_no(u->meta.condition_result)); | |
10717a1a | 2269 | |
a16e1123 LP |
2270 | /* End marker */ |
2271 | fputc('\n', f); | |
2272 | return 0; | |
2273 | } | |
2274 | ||
2275 | void unit_serialize_item_format(Unit *u, FILE *f, const char *key, const char *format, ...) { | |
2276 | va_list ap; | |
2277 | ||
2278 | assert(u); | |
2279 | assert(f); | |
2280 | assert(key); | |
2281 | assert(format); | |
2282 | ||
2283 | fputs(key, f); | |
2284 | fputc('=', f); | |
2285 | ||
2286 | va_start(ap, format); | |
2287 | vfprintf(f, format, ap); | |
2288 | va_end(ap); | |
2289 | ||
2290 | fputc('\n', f); | |
2291 | } | |
2292 | ||
2293 | void unit_serialize_item(Unit *u, FILE *f, const char *key, const char *value) { | |
2294 | assert(u); | |
2295 | assert(f); | |
2296 | assert(key); | |
2297 | assert(value); | |
2298 | ||
2299 | fprintf(f, "%s=%s\n", key, value); | |
2300 | } | |
2301 | ||
2302 | int unit_deserialize(Unit *u, FILE *f, FDSet *fds) { | |
2303 | int r; | |
2304 | ||
2305 | assert(u); | |
2306 | assert(f); | |
2307 | assert(fds); | |
2308 | ||
2309 | if (!unit_can_serialize(u)) | |
2310 | return 0; | |
2311 | ||
2312 | for (;;) { | |
20c03b7b | 2313 | char line[LINE_MAX], *l, *v; |
a16e1123 LP |
2314 | size_t k; |
2315 | ||
2316 | if (!fgets(line, sizeof(line), f)) { | |
2317 | if (feof(f)) | |
2318 | return 0; | |
2319 | return -errno; | |
2320 | } | |
2321 | ||
10f8e83c | 2322 | char_array_0(line); |
a16e1123 LP |
2323 | l = strstrip(line); |
2324 | ||
2325 | /* End marker */ | |
2326 | if (l[0] == 0) | |
2327 | return 0; | |
2328 | ||
2329 | k = strcspn(l, "="); | |
2330 | ||
2331 | if (l[k] == '=') { | |
2332 | l[k] = 0; | |
2333 | v = l+k+1; | |
2334 | } else | |
2335 | v = l+k; | |
2336 | ||
cca098b0 LP |
2337 | if (streq(l, "job")) { |
2338 | JobType type; | |
2339 | ||
2340 | if ((type = job_type_from_string(v)) < 0) | |
2341 | log_debug("Failed to parse job type value %s", v); | |
2342 | else | |
2343 | u->meta.deserialized_job = type; | |
2344 | ||
2345 | continue; | |
8aaf019b | 2346 | } else if (streq(l, "inactive-exit-timestamp")) { |
799fd0fd | 2347 | dual_timestamp_deserialize(v, &u->meta.inactive_exit_timestamp); |
8aaf019b LP |
2348 | continue; |
2349 | } else if (streq(l, "active-enter-timestamp")) { | |
799fd0fd | 2350 | dual_timestamp_deserialize(v, &u->meta.active_enter_timestamp); |
8aaf019b LP |
2351 | continue; |
2352 | } else if (streq(l, "active-exit-timestamp")) { | |
799fd0fd | 2353 | dual_timestamp_deserialize(v, &u->meta.active_exit_timestamp); |
8aaf019b LP |
2354 | continue; |
2355 | } else if (streq(l, "inactive-enter-timestamp")) { | |
799fd0fd | 2356 | dual_timestamp_deserialize(v, &u->meta.inactive_enter_timestamp); |
8aaf019b | 2357 | continue; |
2791a8f8 LP |
2358 | } else if (streq(l, "condition-timestamp")) { |
2359 | dual_timestamp_deserialize(v, &u->meta.condition_timestamp); | |
2360 | continue; | |
2361 | } else if (streq(l, "condition-result")) { | |
2362 | int b; | |
2363 | ||
2364 | if ((b = parse_boolean(v)) < 0) | |
2365 | log_debug("Failed to parse condition result value %s", v); | |
2366 | else | |
2367 | u->meta.condition_result = b; | |
efbac6d2 LP |
2368 | |
2369 | continue; | |
8aaf019b | 2370 | } |
cca098b0 | 2371 | |
a16e1123 LP |
2372 | if ((r = UNIT_VTABLE(u)->deserialize_item(u, l, v, fds)) < 0) |
2373 | return r; | |
2374 | } | |
2375 | } | |
2376 | ||
6e2ef85b LP |
2377 | int unit_add_node_link(Unit *u, const char *what, bool wants) { |
2378 | Unit *device; | |
2379 | char *e; | |
2380 | int r; | |
2381 | ||
2382 | assert(u); | |
2383 | ||
2384 | if (!what) | |
2385 | return 0; | |
2386 | ||
2387 | /* Adds in links to the device node that this unit is based on */ | |
2388 | ||
8407a5d0 | 2389 | if (!is_device_path(what)) |
6e2ef85b LP |
2390 | return 0; |
2391 | ||
2392 | if (!(e = unit_name_build_escape(what+1, NULL, ".device"))) | |
2393 | return -ENOMEM; | |
2394 | ||
398ef8ba | 2395 | r = manager_load_unit(u->meta.manager, e, NULL, NULL, &device); |
6e2ef85b LP |
2396 | free(e); |
2397 | ||
2398 | if (r < 0) | |
2399 | return r; | |
2400 | ||
b81884e7 | 2401 | if ((r = unit_add_two_dependencies(u, UNIT_AFTER, UNIT_BIND_TO, device, true)) < 0) |
6e2ef85b LP |
2402 | return r; |
2403 | ||
2404 | if (wants) | |
2405 | if ((r = unit_add_dependency(device, UNIT_WANTS, u, false)) < 0) | |
2406 | return r; | |
2407 | ||
2408 | return 0; | |
2409 | } | |
a16e1123 | 2410 | |
cca098b0 LP |
2411 | int unit_coldplug(Unit *u) { |
2412 | int r; | |
2413 | ||
2414 | assert(u); | |
2415 | ||
2416 | if (UNIT_VTABLE(u)->coldplug) | |
2417 | if ((r = UNIT_VTABLE(u)->coldplug(u)) < 0) | |
2418 | return r; | |
2419 | ||
2420 | if (u->meta.deserialized_job >= 0) { | |
cebe0d41 | 2421 | if ((r = manager_add_job(u->meta.manager, u->meta.deserialized_job, u, JOB_IGNORE_REQUIREMENTS, false, NULL, NULL)) < 0) |
cca098b0 LP |
2422 | return r; |
2423 | ||
2424 | u->meta.deserialized_job = _JOB_TYPE_INVALID; | |
2425 | } | |
2426 | ||
2427 | return 0; | |
2428 | } | |
2429 | ||
5831e9b7 | 2430 | void unit_status_printf(Unit *u, const char *status, const char *format, ...) { |
9e58ff9c | 2431 | va_list ap; |
5831e9b7 MS |
2432 | char *s, *e; |
2433 | int err; | |
2434 | const unsigned emax = status ? 80 - (sizeof("[ OK ]")-1) : 80; | |
9e58ff9c LP |
2435 | |
2436 | assert(u); | |
2437 | assert(format); | |
2438 | ||
2439 | if (!UNIT_VTABLE(u)->show_status) | |
2440 | return; | |
2441 | ||
27d340c7 | 2442 | if (!manager_get_show_status(u->meta.manager)) |
9e58ff9c LP |
2443 | return; |
2444 | ||
2445 | if (!manager_is_booting_or_shutting_down(u->meta.manager)) | |
2446 | return; | |
2447 | ||
2448 | va_start(ap, format); | |
5831e9b7 | 2449 | err = vasprintf(&s, format, ap); |
9e58ff9c | 2450 | va_end(ap); |
5831e9b7 MS |
2451 | if (err < 0) |
2452 | return; | |
2453 | ||
2454 | e = ellipsize(s, emax, 100); | |
2455 | free(s); | |
2456 | if (!e) | |
2457 | return; | |
2458 | ||
2459 | if (status) | |
2460 | status_printf("%s%*s[%s]\n", e, emax - strlen(e), "", status); | |
2461 | else | |
2462 | status_printf("%s\n", e); | |
2463 | free(e); | |
9e58ff9c LP |
2464 | } |
2465 | ||
45fb0699 | 2466 | bool unit_need_daemon_reload(Unit *u) { |
45fb0699 LP |
2467 | assert(u); |
2468 | ||
5f4b19f4 LP |
2469 | if (u->meta.fragment_path) { |
2470 | struct stat st; | |
45fb0699 | 2471 | |
5f4b19f4 LP |
2472 | zero(st); |
2473 | if (stat(u->meta.fragment_path, &st) < 0) | |
2474 | /* What, cannot access this anymore? */ | |
2475 | return true; | |
45fb0699 | 2476 | |
5f4b19f4 LP |
2477 | if (u->meta.fragment_mtime > 0 && |
2478 | timespec_load(&st.st_mtim) != u->meta.fragment_mtime) | |
2479 | return true; | |
2480 | } | |
2481 | ||
2482 | if (UNIT_VTABLE(u)->need_daemon_reload) | |
2483 | return UNIT_VTABLE(u)->need_daemon_reload(u); | |
2484 | ||
2485 | return false; | |
45fb0699 LP |
2486 | } |
2487 | ||
fdf20a31 | 2488 | void unit_reset_failed(Unit *u) { |
5632e374 LP |
2489 | assert(u); |
2490 | ||
fdf20a31 MM |
2491 | if (UNIT_VTABLE(u)->reset_failed) |
2492 | UNIT_VTABLE(u)->reset_failed(u); | |
5632e374 LP |
2493 | } |
2494 | ||
a7f241db LP |
2495 | Unit *unit_following(Unit *u) { |
2496 | assert(u); | |
2497 | ||
2498 | if (UNIT_VTABLE(u)->following) | |
2499 | return UNIT_VTABLE(u)->following(u); | |
2500 | ||
2501 | return NULL; | |
2502 | } | |
2503 | ||
18ffdfda LP |
2504 | bool unit_pending_inactive(Unit *u) { |
2505 | assert(u); | |
2506 | ||
2507 | /* Returns true if the unit is inactive or going down */ | |
2508 | ||
2509 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))) | |
2510 | return true; | |
2511 | ||
2512 | if (u->meta.job && u->meta.job->type == JOB_STOP) | |
2513 | return true; | |
2514 | ||
2515 | return false; | |
2516 | } | |
2517 | ||
f976f3f6 LP |
2518 | bool unit_pending_active(Unit *u) { |
2519 | assert(u); | |
2520 | ||
2521 | /* Returns true if the unit is inactive or going down */ | |
2522 | ||
2523 | if (UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) | |
2524 | return true; | |
2525 | ||
2526 | if (u->meta.job && | |
2527 | (u->meta.job->type == JOB_START || | |
2528 | u->meta.job->type == JOB_RELOAD_OR_START || | |
2529 | u->meta.job->type == JOB_RESTART)) | |
2530 | return true; | |
2531 | ||
2532 | return false; | |
2533 | } | |
2534 | ||
71fad675 LP |
2535 | UnitType unit_name_to_type(const char *n) { |
2536 | UnitType t; | |
2537 | ||
2538 | assert(n); | |
2539 | ||
2540 | for (t = 0; t < _UNIT_TYPE_MAX; t++) | |
2541 | if (endswith(n, unit_vtable[t]->suffix)) | |
2542 | return t; | |
2543 | ||
2544 | return _UNIT_TYPE_INVALID; | |
2545 | } | |
2546 | ||
b9c0d441 | 2547 | bool unit_name_is_valid(const char *n, bool template_ok) { |
71fad675 LP |
2548 | UnitType t; |
2549 | ||
2550 | t = unit_name_to_type(n); | |
2551 | if (t < 0 || t >= _UNIT_TYPE_MAX) | |
2552 | return false; | |
2553 | ||
b9c0d441 | 2554 | return unit_name_is_valid_no_type(n, template_ok); |
71fad675 LP |
2555 | } |
2556 | ||
8a0867d6 LP |
2557 | int unit_kill(Unit *u, KillWho w, KillMode m, int signo, DBusError *error) { |
2558 | assert(u); | |
2559 | assert(w >= 0 && w < _KILL_WHO_MAX); | |
2560 | assert(m >= 0 && m < _KILL_MODE_MAX); | |
2561 | assert(signo > 0); | |
2562 | assert(signo < _NSIG); | |
2563 | ||
2564 | if (m == KILL_NONE) | |
2565 | return 0; | |
2566 | ||
2567 | if (!UNIT_VTABLE(u)->kill) | |
2568 | return -ENOTSUP; | |
2569 | ||
2570 | return UNIT_VTABLE(u)->kill(u, w, m, signo, error); | |
2571 | } | |
2572 | ||
6210e7fc LP |
2573 | int unit_following_set(Unit *u, Set **s) { |
2574 | assert(u); | |
2575 | assert(s); | |
2576 | ||
2577 | if (UNIT_VTABLE(u)->following_set) | |
2578 | return UNIT_VTABLE(u)->following_set(u, s); | |
2579 | ||
2580 | *s = NULL; | |
2581 | return 0; | |
2582 | } | |
2583 | ||
a4375746 LP |
2584 | UnitFileState unit_get_unit_file_state(Unit *u) { |
2585 | assert(u); | |
2586 | ||
2587 | if (u->meta.unit_file_state < 0 && u->meta.fragment_path) | |
2588 | u->meta.unit_file_state = unit_file_get_state( | |
2589 | u->meta.manager->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, | |
2590 | NULL, file_name_from_path(u->meta.fragment_path)); | |
2591 | ||
2592 | return u->meta.unit_file_state; | |
2593 | } | |
2594 | ||
94f04347 LP |
2595 | static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = { |
2596 | [UNIT_STUB] = "stub", | |
2597 | [UNIT_LOADED] = "loaded", | |
fdf20a31 | 2598 | [UNIT_ERROR] = "error", |
00dc5d76 | 2599 | [UNIT_MERGED] = "merged", |
6daf4f90 | 2600 | [UNIT_MASKED] = "masked" |
94f04347 LP |
2601 | }; |
2602 | ||
2603 | DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState); | |
2604 | ||
2605 | static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = { | |
2606 | [UNIT_ACTIVE] = "active", | |
032ff4af | 2607 | [UNIT_RELOADING] = "reloading", |
94f04347 | 2608 | [UNIT_INACTIVE] = "inactive", |
fdf20a31 | 2609 | [UNIT_FAILED] = "failed", |
94f04347 LP |
2610 | [UNIT_ACTIVATING] = "activating", |
2611 | [UNIT_DEACTIVATING] = "deactivating" | |
2612 | }; | |
2613 | ||
2614 | DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState); | |
2615 | ||
2616 | static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = { | |
2617 | [UNIT_REQUIRES] = "Requires", | |
9e2f7c11 | 2618 | [UNIT_REQUIRES_OVERRIDABLE] = "RequiresOverridable", |
94f04347 LP |
2619 | [UNIT_WANTS] = "Wants", |
2620 | [UNIT_REQUISITE] = "Requisite", | |
9e2f7c11 | 2621 | [UNIT_REQUISITE_OVERRIDABLE] = "RequisiteOverridable", |
94f04347 | 2622 | [UNIT_REQUIRED_BY] = "RequiredBy", |
9e2f7c11 | 2623 | [UNIT_REQUIRED_BY_OVERRIDABLE] = "RequiredByOverridable", |
b81884e7 | 2624 | [UNIT_BIND_TO] = "BindTo", |
94f04347 LP |
2625 | [UNIT_WANTED_BY] = "WantedBy", |
2626 | [UNIT_CONFLICTS] = "Conflicts", | |
69dd2852 | 2627 | [UNIT_CONFLICTED_BY] = "ConflictedBy", |
b81884e7 | 2628 | [UNIT_BOUND_BY] = "BoundBy", |
94f04347 LP |
2629 | [UNIT_BEFORE] = "Before", |
2630 | [UNIT_AFTER] = "After", | |
701cc384 | 2631 | [UNIT_REFERENCES] = "References", |
5de9682c LP |
2632 | [UNIT_REFERENCED_BY] = "ReferencedBy", |
2633 | [UNIT_ON_FAILURE] = "OnFailure" | |
94f04347 LP |
2634 | }; |
2635 | ||
2636 | DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency); |