]>
Commit | Line | Data |
---|---|---|
87f0e418 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
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" |
87f0e418 LP |
45 | |
46 | const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = { | |
47 | [UNIT_SERVICE] = &service_vtable, | |
48 | [UNIT_TIMER] = &timer_vtable, | |
49 | [UNIT_SOCKET] = &socket_vtable, | |
50 | [UNIT_TARGET] = &target_vtable, | |
51 | [UNIT_DEVICE] = &device_vtable, | |
52 | [UNIT_MOUNT] = &mount_vtable, | |
53 | [UNIT_AUTOMOUNT] = &automount_vtable, | |
07b0b134 | 54 | [UNIT_SNAPSHOT] = &snapshot_vtable, |
01f78473 LP |
55 | [UNIT_SWAP] = &swap_vtable, |
56 | [UNIT_PATH] = &path_vtable | |
87f0e418 LP |
57 | }; |
58 | ||
87f0e418 LP |
59 | Unit *unit_new(Manager *m) { |
60 | Unit *u; | |
61 | ||
62 | assert(m); | |
63 | ||
64 | if (!(u = new0(Unit, 1))) | |
65 | return NULL; | |
66 | ||
67 | if (!(u->meta.names = set_new(string_hash_func, string_compare_func))) { | |
68 | free(u); | |
69 | return NULL; | |
70 | } | |
71 | ||
72 | u->meta.manager = m; | |
73 | u->meta.type = _UNIT_TYPE_INVALID; | |
cca098b0 | 74 | u->meta.deserialized_job = _JOB_TYPE_INVALID; |
a40eb732 | 75 | u->meta.default_dependencies = true; |
87f0e418 LP |
76 | |
77 | return u; | |
78 | } | |
79 | ||
f278026d LP |
80 | bool unit_has_name(Unit *u, const char *name) { |
81 | assert(u); | |
82 | assert(name); | |
83 | ||
84 | return !!set_get(u->meta.names, (char*) name); | |
85 | } | |
86 | ||
87f0e418 LP |
87 | int unit_add_name(Unit *u, const char *text) { |
88 | UnitType t; | |
276c3e78 | 89 | char *s, *i = NULL; |
87f0e418 LP |
90 | int r; |
91 | ||
92 | assert(u); | |
93 | assert(text); | |
94 | ||
9e2f7c11 LP |
95 | if (unit_name_is_template(text)) { |
96 | if (!u->meta.instance) | |
97 | return -EINVAL; | |
87f0e418 | 98 | |
9e2f7c11 LP |
99 | s = unit_name_replace_instance(text, u->meta.instance); |
100 | } else | |
101 | s = strdup(text); | |
87f0e418 | 102 | |
9e2f7c11 LP |
103 | if (!s) |
104 | return -ENOMEM; | |
87f0e418 | 105 | |
9e2f7c11 LP |
106 | if (!unit_name_is_valid(s)) { |
107 | r = -EINVAL; | |
108 | goto fail; | |
109 | } | |
e537352b | 110 | |
9e2f7c11 | 111 | assert_se((t = unit_name_to_type(s)) >= 0); |
87f0e418 | 112 | |
9e2f7c11 LP |
113 | if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type) { |
114 | r = -EINVAL; | |
115 | goto fail; | |
116 | } | |
87f0e418 | 117 | |
9e2f7c11 LP |
118 | if ((r = unit_name_to_instance(s, &i)) < 0) |
119 | goto fail; | |
87f0e418 | 120 | |
9e2f7c11 LP |
121 | if (i && unit_vtable[t]->no_instances) |
122 | goto fail; | |
123 | ||
276c3e78 LP |
124 | /* Ensure that this unit is either instanced or not instanced, |
125 | * but not both. */ | |
126 | if (u->meta.type != _UNIT_TYPE_INVALID && !u->meta.instance != !i) { | |
9e2f7c11 LP |
127 | r = -EINVAL; |
128 | goto fail; | |
129 | } | |
130 | ||
131 | if (unit_vtable[t]->no_alias && | |
132 | !set_isempty(u->meta.names) && | |
133 | !set_get(u->meta.names, s)) { | |
134 | r = -EEXIST; | |
135 | goto fail; | |
136 | } | |
137 | ||
4f0f902f LP |
138 | if (hashmap_size(u->meta.manager->units) >= MANAGER_MAX_NAMES) { |
139 | r = -E2BIG; | |
140 | goto fail; | |
141 | } | |
142 | ||
9e2f7c11 LP |
143 | if ((r = set_put(u->meta.names, s)) < 0) { |
144 | if (r == -EEXIST) | |
145 | r = 0; | |
146 | goto fail; | |
87f0e418 LP |
147 | } |
148 | ||
149 | if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) { | |
150 | set_remove(u->meta.names, s); | |
9e2f7c11 | 151 | goto fail; |
87f0e418 LP |
152 | } |
153 | ||
e537352b | 154 | if (u->meta.type == _UNIT_TYPE_INVALID) { |
ef734fd6 | 155 | |
e537352b | 156 | u->meta.type = t; |
9e2f7c11 LP |
157 | u->meta.id = s; |
158 | u->meta.instance = i; | |
159 | ||
160 | LIST_PREPEND(Meta, units_per_type, u->meta.manager->units_per_type[t], &u->meta); | |
e537352b LP |
161 | |
162 | if (UNIT_VTABLE(u)->init) | |
163 | UNIT_VTABLE(u)->init(u); | |
9e2f7c11 LP |
164 | } else |
165 | free(i); | |
87f0e418 | 166 | |
c1e1601e | 167 | unit_add_to_dbus_queue(u); |
87f0e418 | 168 | return 0; |
9e2f7c11 LP |
169 | |
170 | fail: | |
171 | free(s); | |
172 | free(i); | |
173 | ||
174 | return r; | |
87f0e418 LP |
175 | } |
176 | ||
0ae97ec1 | 177 | int unit_choose_id(Unit *u, const char *name) { |
276c3e78 LP |
178 | char *s, *t = NULL, *i; |
179 | int r; | |
0ae97ec1 LP |
180 | |
181 | assert(u); | |
182 | assert(name); | |
183 | ||
9e2f7c11 LP |
184 | if (unit_name_is_template(name)) { |
185 | ||
186 | if (!u->meta.instance) | |
187 | return -EINVAL; | |
188 | ||
189 | if (!(t = unit_name_replace_instance(name, u->meta.instance))) | |
190 | return -ENOMEM; | |
191 | ||
192 | name = t; | |
193 | } | |
194 | ||
0ae97ec1 | 195 | /* Selects one of the names of this unit as the id */ |
9e2f7c11 LP |
196 | s = set_get(u->meta.names, (char*) name); |
197 | free(t); | |
0ae97ec1 | 198 | |
9e2f7c11 | 199 | if (!s) |
0ae97ec1 LP |
200 | return -ENOENT; |
201 | ||
276c3e78 LP |
202 | if ((r = unit_name_to_instance(s, &i)) < 0) |
203 | return r; | |
204 | ||
0ae97ec1 | 205 | u->meta.id = s; |
276c3e78 LP |
206 | |
207 | free(u->meta.instance); | |
208 | u->meta.instance = i; | |
209 | ||
c1e1601e | 210 | unit_add_to_dbus_queue(u); |
9e2f7c11 | 211 | |
0ae97ec1 LP |
212 | return 0; |
213 | } | |
214 | ||
f50e0a01 LP |
215 | int unit_set_description(Unit *u, const char *description) { |
216 | char *s; | |
217 | ||
218 | assert(u); | |
219 | ||
220 | if (!(s = strdup(description))) | |
221 | return -ENOMEM; | |
222 | ||
223 | free(u->meta.description); | |
224 | u->meta.description = s; | |
c1e1601e LP |
225 | |
226 | unit_add_to_dbus_queue(u); | |
f50e0a01 LP |
227 | return 0; |
228 | } | |
229 | ||
701cc384 LP |
230 | bool unit_check_gc(Unit *u) { |
231 | assert(u); | |
232 | ||
b86d44e5 LP |
233 | if (u->meta.load_state == UNIT_STUB) |
234 | return true; | |
235 | ||
701cc384 LP |
236 | if (UNIT_VTABLE(u)->no_gc) |
237 | return true; | |
238 | ||
239 | if (u->meta.job) | |
240 | return true; | |
241 | ||
242 | if (unit_active_state(u) != UNIT_INACTIVE) | |
243 | return true; | |
244 | ||
245 | if (UNIT_VTABLE(u)->check_gc) | |
246 | if (UNIT_VTABLE(u)->check_gc(u)) | |
247 | return true; | |
248 | ||
249 | return false; | |
250 | } | |
251 | ||
87f0e418 LP |
252 | void unit_add_to_load_queue(Unit *u) { |
253 | assert(u); | |
819e213f | 254 | assert(u->meta.type != _UNIT_TYPE_INVALID); |
87f0e418 LP |
255 | |
256 | if (u->meta.load_state != UNIT_STUB || u->meta.in_load_queue) | |
257 | return; | |
258 | ||
259 | LIST_PREPEND(Meta, load_queue, u->meta.manager->load_queue, &u->meta); | |
260 | u->meta.in_load_queue = true; | |
261 | } | |
262 | ||
23a177ef LP |
263 | void unit_add_to_cleanup_queue(Unit *u) { |
264 | assert(u); | |
265 | ||
266 | if (u->meta.in_cleanup_queue) | |
267 | return; | |
268 | ||
269 | LIST_PREPEND(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta); | |
270 | u->meta.in_cleanup_queue = true; | |
271 | } | |
272 | ||
701cc384 LP |
273 | void unit_add_to_gc_queue(Unit *u) { |
274 | assert(u); | |
275 | ||
276 | if (u->meta.in_gc_queue || u->meta.in_cleanup_queue) | |
277 | return; | |
278 | ||
279 | if (unit_check_gc(u)) | |
280 | return; | |
281 | ||
282 | LIST_PREPEND(Meta, gc_queue, u->meta.manager->gc_queue, &u->meta); | |
283 | u->meta.in_gc_queue = true; | |
284 | ||
285 | u->meta.manager->n_in_gc_queue ++; | |
286 | ||
287 | if (u->meta.manager->gc_queue_timestamp <= 0) | |
288 | u->meta.manager->gc_queue_timestamp = now(CLOCK_MONOTONIC); | |
289 | } | |
290 | ||
c1e1601e LP |
291 | void unit_add_to_dbus_queue(Unit *u) { |
292 | assert(u); | |
819e213f | 293 | assert(u->meta.type != _UNIT_TYPE_INVALID); |
c1e1601e | 294 | |
94b6dfa2 | 295 | if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue) |
c1e1601e LP |
296 | return; |
297 | ||
a567261a LP |
298 | /* Shortcut things if nobody cares */ |
299 | if (!bus_has_subscriber(u->meta.manager)) { | |
94b6dfa2 LP |
300 | u->meta.sent_dbus_new_signal = true; |
301 | return; | |
302 | } | |
303 | ||
c1e1601e LP |
304 | LIST_PREPEND(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta); |
305 | u->meta.in_dbus_queue = true; | |
306 | } | |
307 | ||
87f0e418 LP |
308 | static void bidi_set_free(Unit *u, Set *s) { |
309 | Iterator i; | |
310 | Unit *other; | |
311 | ||
312 | assert(u); | |
313 | ||
314 | /* Frees the set and makes sure we are dropped from the | |
315 | * inverse pointers */ | |
316 | ||
317 | SET_FOREACH(other, s, i) { | |
318 | UnitDependency d; | |
319 | ||
320 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) | |
321 | set_remove(other->meta.dependencies[d], u); | |
701cc384 LP |
322 | |
323 | unit_add_to_gc_queue(other); | |
87f0e418 LP |
324 | } |
325 | ||
326 | set_free(s); | |
327 | } | |
328 | ||
329 | void unit_free(Unit *u) { | |
330 | UnitDependency d; | |
331 | Iterator i; | |
332 | char *t; | |
333 | ||
334 | assert(u); | |
335 | ||
c1e1601e LP |
336 | bus_unit_send_removed_signal(u); |
337 | ||
a013b84b LP |
338 | if (u->meta.load_state != UNIT_STUB) |
339 | if (UNIT_VTABLE(u)->done) | |
340 | UNIT_VTABLE(u)->done(u); | |
341 | ||
87f0e418 LP |
342 | SET_FOREACH(t, u->meta.names, i) |
343 | hashmap_remove_value(u->meta.manager->units, t, u); | |
344 | ||
964e0949 LP |
345 | if (u->meta.job) |
346 | job_free(u->meta.job); | |
347 | ||
348 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) | |
349 | bidi_set_free(u, u->meta.dependencies[d]); | |
350 | ||
ef734fd6 LP |
351 | if (u->meta.type != _UNIT_TYPE_INVALID) |
352 | LIST_REMOVE(Meta, units_per_type, u->meta.manager->units_per_type[u->meta.type], &u->meta); | |
353 | ||
87f0e418 LP |
354 | if (u->meta.in_load_queue) |
355 | LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta); | |
356 | ||
c1e1601e LP |
357 | if (u->meta.in_dbus_queue) |
358 | LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta); | |
359 | ||
23a177ef LP |
360 | if (u->meta.in_cleanup_queue) |
361 | LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta); | |
362 | ||
701cc384 LP |
363 | if (u->meta.in_gc_queue) { |
364 | LIST_REMOVE(Meta, gc_queue, u->meta.manager->gc_queue, &u->meta); | |
365 | u->meta.manager->n_in_gc_queue--; | |
366 | } | |
367 | ||
e537352b | 368 | cgroup_bonding_free_list(u->meta.cgroup_bondings); |
87f0e418 | 369 | |
87f0e418 | 370 | free(u->meta.description); |
6be1e7d5 | 371 | free(u->meta.fragment_path); |
87f0e418 | 372 | |
53ec43c6 | 373 | set_free_free(u->meta.names); |
87f0e418 | 374 | |
9e2f7c11 LP |
375 | free(u->meta.instance); |
376 | ||
87f0e418 LP |
377 | free(u); |
378 | } | |
379 | ||
380 | UnitActiveState unit_active_state(Unit *u) { | |
381 | assert(u); | |
382 | ||
6124958c LP |
383 | if (u->meta.load_state == UNIT_MERGED) |
384 | return unit_active_state(unit_follow_merge(u)); | |
385 | ||
386 | /* After a reload it might happen that a unit is not correctly | |
387 | * loaded but still has a process around. That's why we won't | |
388 | * shortcut failed loading to UNIT_INACTIVE_MAINTENANCE. */ | |
87f0e418 LP |
389 | |
390 | return UNIT_VTABLE(u)->active_state(u); | |
391 | } | |
392 | ||
10a94420 LP |
393 | const char* unit_sub_state_to_string(Unit *u) { |
394 | assert(u); | |
395 | ||
396 | return UNIT_VTABLE(u)->sub_state_to_string(u); | |
397 | } | |
398 | ||
23a177ef LP |
399 | static void complete_move(Set **s, Set **other) { |
400 | assert(s); | |
401 | assert(other); | |
87f0e418 | 402 | |
23a177ef LP |
403 | if (!*other) |
404 | return; | |
87f0e418 LP |
405 | |
406 | if (*s) | |
23a177ef LP |
407 | set_move(*s, *other); |
408 | else { | |
409 | *s = *other; | |
410 | *other = NULL; | |
411 | } | |
412 | } | |
87f0e418 | 413 | |
23a177ef LP |
414 | static void merge_names(Unit *u, Unit *other) { |
415 | char *t; | |
416 | Iterator i; | |
87f0e418 | 417 | |
23a177ef LP |
418 | assert(u); |
419 | assert(other); | |
420 | ||
421 | complete_move(&u->meta.names, &other->meta.names); | |
422 | ||
53ec43c6 | 423 | set_free_free(other->meta.names); |
23a177ef LP |
424 | other->meta.names = NULL; |
425 | other->meta.id = NULL; | |
426 | ||
427 | SET_FOREACH(t, u->meta.names, i) | |
428 | assert_se(hashmap_replace(u->meta.manager->units, t, u) == 0); | |
87f0e418 LP |
429 | } |
430 | ||
23a177ef LP |
431 | static void merge_dependencies(Unit *u, Unit *other, UnitDependency d) { |
432 | Iterator i; | |
433 | Unit *back; | |
87f0e418 | 434 | int r; |
23a177ef LP |
435 | |
436 | assert(u); | |
437 | assert(other); | |
438 | assert(d < _UNIT_DEPENDENCY_MAX); | |
439 | ||
440 | SET_FOREACH(back, other->meta.dependencies[d], i) { | |
441 | UnitDependency k; | |
442 | ||
443 | for (k = 0; k < _UNIT_DEPENDENCY_MAX; k++) | |
444 | if ((r = set_remove_and_put(back->meta.dependencies[k], other, u)) < 0) { | |
445 | ||
446 | if (r == -EEXIST) | |
447 | set_remove(back->meta.dependencies[k], other); | |
448 | else | |
449 | assert(r == -ENOENT); | |
450 | } | |
451 | } | |
452 | ||
453 | complete_move(&u->meta.dependencies[d], &other->meta.dependencies[d]); | |
454 | ||
455 | set_free(other->meta.dependencies[d]); | |
456 | other->meta.dependencies[d] = NULL; | |
457 | } | |
458 | ||
459 | int unit_merge(Unit *u, Unit *other) { | |
87f0e418 LP |
460 | UnitDependency d; |
461 | ||
462 | assert(u); | |
463 | assert(other); | |
464 | assert(u->meta.manager == other->meta.manager); | |
9e2f7c11 | 465 | assert(u->meta.type != _UNIT_TYPE_INVALID); |
87f0e418 | 466 | |
cc916967 LP |
467 | other = unit_follow_merge(other); |
468 | ||
23a177ef LP |
469 | if (other == u) |
470 | return 0; | |
471 | ||
9e2f7c11 LP |
472 | if (u->meta.type != other->meta.type) |
473 | return -EINVAL; | |
474 | ||
276c3e78 | 475 | if (!u->meta.instance != !other->meta.instance) |
87f0e418 LP |
476 | return -EINVAL; |
477 | ||
cc916967 LP |
478 | if (other->meta.load_state != UNIT_STUB && |
479 | other->meta.load_state != UNIT_FAILED) | |
23a177ef | 480 | return -EEXIST; |
87f0e418 | 481 | |
819e213f LP |
482 | if (other->meta.job) |
483 | return -EEXIST; | |
484 | ||
6124958c | 485 | if (!UNIT_IS_INACTIVE_OR_MAINTENANCE(unit_active_state(other))) |
819e213f LP |
486 | return -EEXIST; |
487 | ||
87f0e418 | 488 | /* Merge names */ |
23a177ef | 489 | merge_names(u, other); |
87f0e418 LP |
490 | |
491 | /* Merge dependencies */ | |
492 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) | |
23a177ef | 493 | merge_dependencies(u, other, d); |
87f0e418 | 494 | |
23a177ef LP |
495 | other->meta.load_state = UNIT_MERGED; |
496 | other->meta.merged_into = u; | |
497 | ||
3616a49c LP |
498 | /* If there is still some data attached to the other node, we |
499 | * don't need it anymore, and can free it. */ | |
500 | if (other->meta.load_state != UNIT_STUB) | |
501 | if (UNIT_VTABLE(other)->done) | |
502 | UNIT_VTABLE(other)->done(other); | |
503 | ||
504 | unit_add_to_dbus_queue(u); | |
23a177ef LP |
505 | unit_add_to_cleanup_queue(other); |
506 | ||
507 | return 0; | |
508 | } | |
509 | ||
510 | int unit_merge_by_name(Unit *u, const char *name) { | |
511 | Unit *other; | |
9e2f7c11 LP |
512 | int r; |
513 | char *s = NULL; | |
23a177ef LP |
514 | |
515 | assert(u); | |
516 | assert(name); | |
517 | ||
9e2f7c11 LP |
518 | if (unit_name_is_template(name)) { |
519 | if (!u->meta.instance) | |
520 | return -EINVAL; | |
521 | ||
522 | if (!(s = unit_name_replace_instance(name, u->meta.instance))) | |
523 | return -ENOMEM; | |
524 | ||
525 | name = s; | |
526 | } | |
527 | ||
23a177ef | 528 | if (!(other = manager_get_unit(u->meta.manager, name))) |
9e2f7c11 LP |
529 | r = unit_add_name(u, name); |
530 | else | |
531 | r = unit_merge(u, other); | |
23a177ef | 532 | |
9e2f7c11 LP |
533 | free(s); |
534 | return r; | |
23a177ef LP |
535 | } |
536 | ||
537 | Unit* unit_follow_merge(Unit *u) { | |
538 | assert(u); | |
539 | ||
540 | while (u->meta.load_state == UNIT_MERGED) | |
541 | assert_se(u = u->meta.merged_into); | |
542 | ||
543 | return u; | |
544 | } | |
545 | ||
546 | int unit_add_exec_dependencies(Unit *u, ExecContext *c) { | |
547 | int r; | |
548 | ||
549 | assert(u); | |
550 | assert(c); | |
551 | ||
ecc6e2b8 LP |
552 | if (c->std_output != EXEC_OUTPUT_KMSG && |
553 | c->std_output != EXEC_OUTPUT_SYSLOG && | |
554 | c->std_error != EXEC_OUTPUT_KMSG && | |
555 | c->std_error != EXEC_OUTPUT_SYSLOG) | |
23a177ef LP |
556 | return 0; |
557 | ||
558 | /* If syslog or kernel logging is requested, make sure our own | |
559 | * logging daemon is run first. */ | |
560 | ||
701cc384 | 561 | if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0) |
23a177ef LP |
562 | return r; |
563 | ||
a3d4e06d | 564 | if (u->meta.manager->running_as == MANAGER_SYSTEM) |
701cc384 | 565 | if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0) |
23a177ef LP |
566 | return r; |
567 | ||
87f0e418 LP |
568 | return 0; |
569 | } | |
570 | ||
87f0e418 LP |
571 | const char *unit_description(Unit *u) { |
572 | assert(u); | |
573 | ||
574 | if (u->meta.description) | |
575 | return u->meta.description; | |
576 | ||
9e2f7c11 | 577 | return u->meta.id; |
87f0e418 LP |
578 | } |
579 | ||
580 | void unit_dump(Unit *u, FILE *f, const char *prefix) { | |
87f0e418 LP |
581 | char *t; |
582 | UnitDependency d; | |
583 | Iterator i; | |
47be870b LP |
584 | char *p2; |
585 | const char *prefix2; | |
8e274523 | 586 | CGroupBonding *b; |
173e3821 LP |
587 | char |
588 | timestamp1[FORMAT_TIMESTAMP_MAX], | |
589 | timestamp2[FORMAT_TIMESTAMP_MAX], | |
590 | timestamp3[FORMAT_TIMESTAMP_MAX], | |
faf919f1 LP |
591 | timestamp4[FORMAT_TIMESTAMP_MAX], |
592 | timespan[FORMAT_TIMESPAN_MAX]; | |
a7f241db | 593 | Unit *following; |
87f0e418 LP |
594 | |
595 | assert(u); | |
9e2f7c11 | 596 | assert(u->meta.type >= 0); |
87f0e418 LP |
597 | |
598 | if (!prefix) | |
599 | prefix = ""; | |
47be870b LP |
600 | p2 = strappend(prefix, "\t"); |
601 | prefix2 = p2 ? p2 : prefix; | |
87f0e418 LP |
602 | |
603 | fprintf(f, | |
40d50879 | 604 | "%s-> Unit %s:\n" |
87f0e418 | 605 | "%s\tDescription: %s\n" |
9e2f7c11 | 606 | "%s\tInstance: %s\n" |
87f0e418 | 607 | "%s\tUnit Load State: %s\n" |
2fad8195 | 608 | "%s\tUnit Active State: %s\n" |
173e3821 | 609 | "%s\tInactive Exit Timestamp: %s\n" |
2fad8195 | 610 | "%s\tActive Enter Timestamp: %s\n" |
701cc384 | 611 | "%s\tActive Exit Timestamp: %s\n" |
173e3821 | 612 | "%s\tInactive Enter Timestamp: %s\n" |
45fb0699 LP |
613 | "%s\tGC Check Good: %s\n" |
614 | "%s\tNeed Daemon Reload: %s\n", | |
9e2f7c11 | 615 | prefix, u->meta.id, |
87f0e418 | 616 | prefix, unit_description(u), |
9e2f7c11 | 617 | prefix, strna(u->meta.instance), |
94f04347 | 618 | prefix, unit_load_state_to_string(u->meta.load_state), |
2fad8195 | 619 | prefix, unit_active_state_to_string(unit_active_state(u)), |
871d7de4 LP |
620 | prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->meta.inactive_exit_timestamp.realtime)), |
621 | prefix, strna(format_timestamp(timestamp2, sizeof(timestamp2), u->meta.active_enter_timestamp.realtime)), | |
622 | prefix, strna(format_timestamp(timestamp3, sizeof(timestamp3), u->meta.active_exit_timestamp.realtime)), | |
623 | prefix, strna(format_timestamp(timestamp4, sizeof(timestamp4), u->meta.inactive_enter_timestamp.realtime)), | |
45fb0699 LP |
624 | prefix, yes_no(unit_check_gc(u)), |
625 | prefix, yes_no(unit_need_daemon_reload(u))); | |
0301abf4 | 626 | |
87f0e418 LP |
627 | SET_FOREACH(t, u->meta.names, i) |
628 | fprintf(f, "%s\tName: %s\n", prefix, t); | |
629 | ||
a7f241db LP |
630 | if ((following = unit_following(u))) |
631 | fprintf(f, "%s\tFollowing: %s\n", prefix, following->meta.id); | |
8fe914ec | 632 | |
23a177ef LP |
633 | if (u->meta.fragment_path) |
634 | fprintf(f, "%s\tFragment Path: %s\n", prefix, u->meta.fragment_path); | |
635 | ||
faf919f1 LP |
636 | if (u->meta.job_timeout > 0) |
637 | fprintf(f, "%s\tJob Timeout: %s\n", prefix, format_timespan(timespan, sizeof(timespan), u->meta.job_timeout)); | |
638 | ||
87f0e418 LP |
639 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) { |
640 | Unit *other; | |
641 | ||
87f0e418 | 642 | SET_FOREACH(other, u->meta.dependencies[d], i) |
9e2f7c11 | 643 | fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), other->meta.id); |
87f0e418 LP |
644 | } |
645 | ||
23a177ef | 646 | if (u->meta.load_state == UNIT_LOADED) { |
b0650475 LP |
647 | fprintf(f, |
648 | "%s\tRecursive Stop: %s\n" | |
a40eb732 | 649 | "%s\tStopWhenUnneeded: %s\n" |
b5e9dba8 LP |
650 | "%s\tRefuseManualStart: %s\n" |
651 | "%s\tRefuseManualStop: %s\n" | |
3b6fdb5b LP |
652 | "%s\tDefaultDependencies: %s\n" |
653 | "%s\tIgnoreDependencyFailure: %s\n", | |
b0650475 | 654 | prefix, yes_no(u->meta.recursive_stop), |
a40eb732 | 655 | prefix, yes_no(u->meta.stop_when_unneeded), |
b5e9dba8 LP |
656 | prefix, yes_no(u->meta.refuse_manual_start), |
657 | prefix, yes_no(u->meta.refuse_manual_stop), | |
3b6fdb5b LP |
658 | prefix, yes_no(u->meta.default_dependencies), |
659 | prefix, yes_no(u->meta.ignore_dependency_failure)); | |
b0650475 | 660 | |
23a177ef LP |
661 | LIST_FOREACH(by_unit, b, u->meta.cgroup_bondings) |
662 | fprintf(f, "%s\tControlGroup: %s:%s\n", | |
663 | prefix, b->controller, b->path); | |
8e274523 | 664 | |
23a177ef LP |
665 | if (UNIT_VTABLE(u)->dump) |
666 | UNIT_VTABLE(u)->dump(u, f, prefix2); | |
b0650475 LP |
667 | |
668 | } else if (u->meta.load_state == UNIT_MERGED) | |
669 | fprintf(f, | |
670 | "%s\tMerged into: %s\n", | |
9e2f7c11 | 671 | prefix, u->meta.merged_into->meta.id); |
87f0e418 LP |
672 | |
673 | if (u->meta.job) | |
674 | job_dump(u->meta.job, f, prefix2); | |
675 | ||
47be870b | 676 | free(p2); |
87f0e418 LP |
677 | } |
678 | ||
679 | /* Common implementation for multiple backends */ | |
e537352b | 680 | int unit_load_fragment_and_dropin(Unit *u) { |
23a177ef LP |
681 | int r; |
682 | ||
683 | assert(u); | |
23a177ef LP |
684 | |
685 | /* Load a .service file */ | |
e537352b | 686 | if ((r = unit_load_fragment(u)) < 0) |
23a177ef LP |
687 | return r; |
688 | ||
e537352b | 689 | if (u->meta.load_state == UNIT_STUB) |
23a177ef LP |
690 | return -ENOENT; |
691 | ||
692 | /* Load drop-in directory data */ | |
693 | if ((r = unit_load_dropin(unit_follow_merge(u))) < 0) | |
694 | return r; | |
695 | ||
696 | return 0; | |
697 | } | |
698 | ||
699 | /* Common implementation for multiple backends */ | |
e537352b | 700 | int unit_load_fragment_and_dropin_optional(Unit *u) { |
23a177ef | 701 | int r; |
87f0e418 LP |
702 | |
703 | assert(u); | |
704 | ||
23a177ef LP |
705 | /* Same as unit_load_fragment_and_dropin(), but whether |
706 | * something can be loaded or not doesn't matter. */ | |
707 | ||
708 | /* Load a .service file */ | |
e537352b | 709 | if ((r = unit_load_fragment(u)) < 0) |
87f0e418 LP |
710 | return r; |
711 | ||
e537352b LP |
712 | if (u->meta.load_state == UNIT_STUB) |
713 | u->meta.load_state = UNIT_LOADED; | |
d46de8a1 | 714 | |
87f0e418 | 715 | /* Load drop-in directory data */ |
23a177ef | 716 | if ((r = unit_load_dropin(unit_follow_merge(u))) < 0) |
87f0e418 LP |
717 | return r; |
718 | ||
23a177ef | 719 | return 0; |
87f0e418 LP |
720 | } |
721 | ||
a16e1123 LP |
722 | /* Common implementation for multiple backends */ |
723 | int unit_load_nop(Unit *u) { | |
724 | assert(u); | |
725 | ||
726 | if (u->meta.load_state == UNIT_STUB) | |
727 | u->meta.load_state = UNIT_LOADED; | |
728 | ||
729 | return 0; | |
730 | } | |
731 | ||
87f0e418 LP |
732 | int unit_load(Unit *u) { |
733 | int r; | |
734 | ||
735 | assert(u); | |
736 | ||
737 | if (u->meta.in_load_queue) { | |
738 | LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta); | |
739 | u->meta.in_load_queue = false; | |
740 | } | |
741 | ||
e537352b LP |
742 | if (u->meta.type == _UNIT_TYPE_INVALID) |
743 | return -EINVAL; | |
744 | ||
87f0e418 LP |
745 | if (u->meta.load_state != UNIT_STUB) |
746 | return 0; | |
747 | ||
e537352b LP |
748 | if (UNIT_VTABLE(u)->load) |
749 | if ((r = UNIT_VTABLE(u)->load(u)) < 0) | |
87f0e418 | 750 | goto fail; |
23a177ef | 751 | |
e537352b | 752 | if (u->meta.load_state == UNIT_STUB) { |
23a177ef LP |
753 | r = -ENOENT; |
754 | goto fail; | |
755 | } | |
756 | ||
23a177ef LP |
757 | assert((u->meta.load_state != UNIT_MERGED) == !u->meta.merged_into); |
758 | ||
759 | unit_add_to_dbus_queue(unit_follow_merge(u)); | |
701cc384 | 760 | unit_add_to_gc_queue(u); |
87f0e418 | 761 | |
87f0e418 LP |
762 | return 0; |
763 | ||
764 | fail: | |
765 | u->meta.load_state = UNIT_FAILED; | |
c1e1601e | 766 | unit_add_to_dbus_queue(u); |
23a177ef | 767 | |
54165a39 | 768 | log_notice("Failed to load configuration for %s: %s", u->meta.id, strerror(-r)); |
23a177ef | 769 | |
87f0e418 LP |
770 | return r; |
771 | } | |
772 | ||
773 | /* Errors: | |
d5159713 LP |
774 | * -EBADR: This unit type does not support starting. |
775 | * -EALREADY: Unit is already started. | |
776 | * -EAGAIN: An operation is already in progress. Retry later. | |
777 | * -ECANCELED: Too many requests for now. | |
87f0e418 LP |
778 | */ |
779 | int unit_start(Unit *u) { | |
780 | UnitActiveState state; | |
781 | ||
782 | assert(u); | |
783 | ||
6124958c LP |
784 | if (u->meta.load_state != UNIT_LOADED) |
785 | return -EINVAL; | |
786 | ||
7898b0cf LP |
787 | /* If this is already (being) started, then this will |
788 | * succeed. Note that this will even succeed if this unit is | |
789 | * not startable by the user. This is relied on to detect when | |
790 | * we need to wait for units and when waiting is finished. */ | |
87f0e418 LP |
791 | state = unit_active_state(u); |
792 | if (UNIT_IS_ACTIVE_OR_RELOADING(state)) | |
793 | return -EALREADY; | |
794 | ||
7898b0cf LP |
795 | /* If it is stopped, but we cannot start it, then fail */ |
796 | if (!UNIT_VTABLE(u)->start) | |
797 | return -EBADR; | |
798 | ||
87f0e418 LP |
799 | /* We don't suppress calls to ->start() here when we are |
800 | * already starting, to allow this request to be used as a | |
801 | * "hurry up" call, for example when the unit is in some "auto | |
802 | * restart" state where it waits for a holdoff timer to elapse | |
803 | * before it will start again. */ | |
804 | ||
c1e1601e | 805 | unit_add_to_dbus_queue(u); |
9e58ff9c LP |
806 | |
807 | unit_status_printf(u, "Starting %s...\n", unit_description(u)); | |
808 | ||
87f0e418 LP |
809 | return UNIT_VTABLE(u)->start(u); |
810 | } | |
811 | ||
812 | bool unit_can_start(Unit *u) { | |
813 | assert(u); | |
814 | ||
815 | return !!UNIT_VTABLE(u)->start; | |
816 | } | |
817 | ||
818 | /* Errors: | |
819 | * -EBADR: This unit type does not support stopping. | |
820 | * -EALREADY: Unit is already stopped. | |
821 | * -EAGAIN: An operation is already in progress. Retry later. | |
822 | */ | |
823 | int unit_stop(Unit *u) { | |
824 | UnitActiveState state; | |
825 | ||
826 | assert(u); | |
827 | ||
87f0e418 | 828 | state = unit_active_state(u); |
6124958c | 829 | if (UNIT_IS_INACTIVE_OR_MAINTENANCE(state)) |
87f0e418 LP |
830 | return -EALREADY; |
831 | ||
7898b0cf LP |
832 | if (!UNIT_VTABLE(u)->stop) |
833 | return -EBADR; | |
834 | ||
c1e1601e | 835 | unit_add_to_dbus_queue(u); |
9e58ff9c LP |
836 | |
837 | unit_status_printf(u, "Stopping %s...\n", unit_description(u)); | |
838 | ||
87f0e418 LP |
839 | return UNIT_VTABLE(u)->stop(u); |
840 | } | |
841 | ||
842 | /* Errors: | |
843 | * -EBADR: This unit type does not support reloading. | |
844 | * -ENOEXEC: Unit is not started. | |
845 | * -EAGAIN: An operation is already in progress. Retry later. | |
846 | */ | |
847 | int unit_reload(Unit *u) { | |
848 | UnitActiveState state; | |
849 | ||
850 | assert(u); | |
851 | ||
6124958c LP |
852 | if (u->meta.load_state != UNIT_LOADED) |
853 | return -EINVAL; | |
854 | ||
87f0e418 LP |
855 | if (!unit_can_reload(u)) |
856 | return -EBADR; | |
857 | ||
858 | state = unit_active_state(u); | |
032ff4af | 859 | if (unit_active_state(u) == UNIT_RELOADING) |
87f0e418 LP |
860 | return -EALREADY; |
861 | ||
862 | if (unit_active_state(u) != UNIT_ACTIVE) | |
863 | return -ENOEXEC; | |
864 | ||
c1e1601e | 865 | unit_add_to_dbus_queue(u); |
87f0e418 LP |
866 | return UNIT_VTABLE(u)->reload(u); |
867 | } | |
868 | ||
869 | bool unit_can_reload(Unit *u) { | |
870 | assert(u); | |
871 | ||
872 | if (!UNIT_VTABLE(u)->reload) | |
873 | return false; | |
874 | ||
875 | if (!UNIT_VTABLE(u)->can_reload) | |
876 | return true; | |
877 | ||
878 | return UNIT_VTABLE(u)->can_reload(u); | |
879 | } | |
880 | ||
f3bff0eb LP |
881 | static void unit_check_uneeded(Unit *u) { |
882 | Iterator i; | |
883 | Unit *other; | |
884 | ||
885 | assert(u); | |
886 | ||
887 | /* If this service shall be shut down when unneeded then do | |
888 | * so. */ | |
889 | ||
890 | if (!u->meta.stop_when_unneeded) | |
891 | return; | |
892 | ||
893 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) | |
894 | return; | |
895 | ||
896 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i) | |
897 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
898 | return; | |
899 | ||
9e2f7c11 | 900 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i) |
f3bff0eb LP |
901 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
902 | return; | |
903 | ||
904 | SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i) | |
905 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
906 | return; | |
907 | ||
54165a39 | 908 | log_info("Service %s is not needed anymore. Stopping.", u->meta.id); |
f3bff0eb LP |
909 | |
910 | /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */ | |
398ef8ba | 911 | manager_add_job(u->meta.manager, JOB_STOP, u, JOB_FAIL, true, NULL, NULL); |
f3bff0eb LP |
912 | } |
913 | ||
87f0e418 LP |
914 | static void retroactively_start_dependencies(Unit *u) { |
915 | Iterator i; | |
916 | Unit *other; | |
917 | ||
918 | assert(u); | |
919 | assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))); | |
920 | ||
921 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i) | |
922 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
398ef8ba | 923 | manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL); |
87f0e418 | 924 | |
9e2f7c11 | 925 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i) |
87f0e418 | 926 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) |
398ef8ba | 927 | manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL, NULL); |
87f0e418 LP |
928 | |
929 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i) | |
930 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
398ef8ba | 931 | manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL); |
87f0e418 LP |
932 | |
933 | SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i) | |
934 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
398ef8ba | 935 | manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL, NULL); |
87f0e418 LP |
936 | |
937 | SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i) | |
938 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
398ef8ba | 939 | manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL); |
69dd2852 LP |
940 | |
941 | SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTED_BY], i) | |
942 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
943 | manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL); | |
87f0e418 LP |
944 | } |
945 | ||
946 | static void retroactively_stop_dependencies(Unit *u) { | |
947 | Iterator i; | |
948 | Unit *other; | |
949 | ||
950 | assert(u); | |
951 | assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))); | |
952 | ||
f3bff0eb LP |
953 | if (u->meta.recursive_stop) { |
954 | /* Pull down units need us recursively if enabled */ | |
955 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i) | |
956 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
398ef8ba | 957 | manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL); |
f3bff0eb LP |
958 | } |
959 | ||
960 | /* Garbage collect services that might not be needed anymore, if enabled */ | |
961 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i) | |
87f0e418 | 962 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
f3bff0eb | 963 | unit_check_uneeded(other); |
9e2f7c11 | 964 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i) |
f3bff0eb LP |
965 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
966 | unit_check_uneeded(other); | |
967 | SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i) | |
968 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
969 | unit_check_uneeded(other); | |
970 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i) | |
971 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
972 | unit_check_uneeded(other); | |
9e2f7c11 | 973 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE_OVERRIDABLE], i) |
f3bff0eb LP |
974 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
975 | unit_check_uneeded(other); | |
87f0e418 LP |
976 | } |
977 | ||
978 | void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) { | |
63983207 | 979 | dual_timestamp ts; |
7e6e7b06 | 980 | bool unexpected; |
a096ed36 | 981 | |
87f0e418 LP |
982 | assert(u); |
983 | assert(os < _UNIT_ACTIVE_STATE_MAX); | |
984 | assert(ns < _UNIT_ACTIVE_STATE_MAX); | |
87f0e418 | 985 | |
8e471ccd LP |
986 | /* Note that this is called for all low-level state changes, |
987 | * even if they might map to the same high-level | |
988 | * UnitActiveState! That means that ns == os is OK an expected | |
989 | * behaviour here. For example: if a mount point is remounted | |
990 | * this function will be called too and the utmp code below | |
991 | * relies on that! */ | |
87f0e418 | 992 | |
63983207 | 993 | dual_timestamp_get(&ts); |
173e3821 | 994 | |
6124958c | 995 | if (UNIT_IS_INACTIVE_OR_MAINTENANCE(os) && !UNIT_IS_INACTIVE_OR_MAINTENANCE(ns)) |
173e3821 | 996 | u->meta.inactive_exit_timestamp = ts; |
6124958c | 997 | else if (!UNIT_IS_INACTIVE_OR_MAINTENANCE(os) && UNIT_IS_INACTIVE_OR_MAINTENANCE(ns)) |
173e3821 LP |
998 | u->meta.inactive_enter_timestamp = ts; |
999 | ||
87f0e418 | 1000 | if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns)) |
173e3821 | 1001 | u->meta.active_enter_timestamp = ts; |
87f0e418 | 1002 | else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns)) |
173e3821 | 1003 | u->meta.active_exit_timestamp = ts; |
87f0e418 | 1004 | |
fb385181 LP |
1005 | if (UNIT_IS_INACTIVE_OR_MAINTENANCE(ns)) |
1006 | cgroup_bonding_trim_list(u->meta.cgroup_bondings, true); | |
1007 | ||
871d7de4 | 1008 | timer_unit_notify(u, ns); |
01f78473 | 1009 | path_unit_notify(u, ns); |
871d7de4 | 1010 | |
87f0e418 | 1011 | if (u->meta.job) { |
7e6e7b06 | 1012 | unexpected = false; |
87f0e418 LP |
1013 | |
1014 | if (u->meta.job->state == JOB_WAITING) | |
1015 | ||
1016 | /* So we reached a different state for this | |
1017 | * job. Let's see if we can run it now if it | |
1018 | * failed previously due to EAGAIN. */ | |
c1e1601e | 1019 | job_add_to_run_queue(u->meta.job); |
87f0e418 | 1020 | |
b410e6b9 LP |
1021 | /* Let's check whether this state change constitutes a |
1022 | * finished job, or maybe cotradicts a running job and | |
1023 | * hence needs to invalidate jobs. */ | |
87f0e418 | 1024 | |
b410e6b9 | 1025 | switch (u->meta.job->type) { |
87f0e418 | 1026 | |
b410e6b9 LP |
1027 | case JOB_START: |
1028 | case JOB_VERIFY_ACTIVE: | |
87f0e418 | 1029 | |
b410e6b9 LP |
1030 | if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) |
1031 | job_finish_and_invalidate(u->meta.job, true); | |
1032 | else if (u->meta.job->state == JOB_RUNNING && ns != UNIT_ACTIVATING) { | |
1033 | unexpected = true; | |
1034 | job_finish_and_invalidate(u->meta.job, false); | |
1035 | } | |
87f0e418 | 1036 | |
b410e6b9 | 1037 | break; |
87f0e418 | 1038 | |
b410e6b9 LP |
1039 | case JOB_RELOAD: |
1040 | case JOB_RELOAD_OR_START: | |
87f0e418 | 1041 | |
b410e6b9 | 1042 | if (u->meta.job->state == JOB_RUNNING) { |
a096ed36 | 1043 | if (ns == UNIT_ACTIVE) |
87f0e418 | 1044 | job_finish_and_invalidate(u->meta.job, true); |
032ff4af | 1045 | else if (ns != UNIT_ACTIVATING && ns != UNIT_RELOADING) { |
a096ed36 | 1046 | unexpected = true; |
87f0e418 | 1047 | job_finish_and_invalidate(u->meta.job, false); |
a096ed36 | 1048 | } |
b410e6b9 | 1049 | } |
87f0e418 | 1050 | |
b410e6b9 | 1051 | break; |
87f0e418 | 1052 | |
b410e6b9 LP |
1053 | case JOB_STOP: |
1054 | case JOB_RESTART: | |
1055 | case JOB_TRY_RESTART: | |
87f0e418 | 1056 | |
9aab5a73 | 1057 | if (ns == UNIT_INACTIVE || ns == UNIT_MAINTENANCE) |
b410e6b9 LP |
1058 | job_finish_and_invalidate(u->meta.job, true); |
1059 | else if (u->meta.job->state == JOB_RUNNING && ns != UNIT_DEACTIVATING) { | |
1060 | unexpected = true; | |
1061 | job_finish_and_invalidate(u->meta.job, false); | |
1062 | } | |
87f0e418 | 1063 | |
b410e6b9 | 1064 | break; |
87f0e418 | 1065 | |
b410e6b9 LP |
1066 | default: |
1067 | assert_not_reached("Job type unknown"); | |
87f0e418 | 1068 | } |
87f0e418 | 1069 | |
7e6e7b06 LP |
1070 | } else |
1071 | unexpected = true; | |
1072 | ||
9f611ad8 LP |
1073 | /* If this state change happened without being requested by a |
1074 | * job, then let's retroactively start or stop | |
1075 | * dependencies. We skip that step when deserializing, since | |
1076 | * we don't want to create any additional jobs just because | |
1077 | * something is already activated. */ | |
1078 | ||
1079 | if (unexpected && u->meta.manager->n_deserializing <= 0) { | |
7e6e7b06 LP |
1080 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns)) |
1081 | retroactively_start_dependencies(u); | |
1082 | else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns)) | |
1083 | retroactively_stop_dependencies(u); | |
a096ed36 | 1084 | } |
f3bff0eb | 1085 | |
5de9682c LP |
1086 | if (ns != os && ns == UNIT_MAINTENANCE) { |
1087 | Iterator i; | |
1088 | Unit *other; | |
1089 | ||
1090 | SET_FOREACH(other, u->meta.dependencies[UNIT_ON_FAILURE], i) | |
1091 | manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL); | |
1092 | ||
1093 | log_notice("Unit %s entered maintenance state.", u->meta.id); | |
1094 | } | |
1095 | ||
e537352b LP |
1096 | /* Some names are special */ |
1097 | if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) { | |
4927fcae | 1098 | if (unit_has_name(u, SPECIAL_DBUS_SERVICE)) |
e537352b LP |
1099 | /* The bus just might have become available, |
1100 | * hence try to connect to it, if we aren't | |
1101 | * yet connected. */ | |
5e8d1c9a | 1102 | bus_init(u->meta.manager); |
f278026d | 1103 | |
e537352b LP |
1104 | if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE)) |
1105 | /* The syslog daemon just might have become | |
1106 | * available, hence try to connect to it, if | |
1107 | * we aren't yet connected. */ | |
843d2643 | 1108 | log_open(); |
f278026d | 1109 | |
4927fcae LP |
1110 | if (u->meta.type == UNIT_SERVICE && |
1111 | !UNIT_IS_ACTIVE_OR_RELOADING(os)) | |
1112 | /* Write audit record if we have just finished starting up */ | |
1113 | manager_send_unit_audit(u->meta.manager, u, AUDIT_SERVICE_START, 1); | |
e537352b | 1114 | |
4927fcae | 1115 | } else { |
f278026d LP |
1116 | |
1117 | if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE)) | |
e537352b LP |
1118 | /* The syslog daemon might just have |
1119 | * terminated, hence try to disconnect from | |
1120 | * it. */ | |
2882c7ed | 1121 | log_close_syslog(); |
f278026d LP |
1122 | |
1123 | /* We don't care about D-Bus here, since we'll get an | |
1124 | * asynchronous notification for it anyway. */ | |
4927fcae LP |
1125 | |
1126 | if (u->meta.type == UNIT_SERVICE && | |
1127 | UNIT_IS_INACTIVE_OR_MAINTENANCE(ns) && | |
1128 | !UNIT_IS_INACTIVE_OR_MAINTENANCE(os)) | |
1129 | ||
1130 | /* Write audit record if we have just finished shutting down */ | |
1131 | manager_send_unit_audit(u->meta.manager, u, AUDIT_SERVICE_STOP, ns == UNIT_INACTIVE); | |
f278026d LP |
1132 | } |
1133 | ||
f3bff0eb LP |
1134 | /* Maybe we finished startup and are now ready for being |
1135 | * stopped because unneeded? */ | |
1136 | unit_check_uneeded(u); | |
c1e1601e LP |
1137 | |
1138 | unit_add_to_dbus_queue(u); | |
701cc384 | 1139 | unit_add_to_gc_queue(u); |
87f0e418 LP |
1140 | } |
1141 | ||
acbb0225 | 1142 | int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) { |
87f0e418 LP |
1143 | struct epoll_event ev; |
1144 | ||
1145 | assert(u); | |
1146 | assert(fd >= 0); | |
acbb0225 | 1147 | assert(w); |
ea430986 | 1148 | assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u)); |
87f0e418 LP |
1149 | |
1150 | zero(ev); | |
acbb0225 | 1151 | ev.data.ptr = w; |
87f0e418 LP |
1152 | ev.events = events; |
1153 | ||
acbb0225 LP |
1154 | if (epoll_ctl(u->meta.manager->epoll_fd, |
1155 | w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD, | |
1156 | fd, | |
1157 | &ev) < 0) | |
1158 | return -errno; | |
87f0e418 | 1159 | |
acbb0225 LP |
1160 | w->fd = fd; |
1161 | w->type = WATCH_FD; | |
ea430986 | 1162 | w->data.unit = u; |
87f0e418 | 1163 | |
acbb0225 | 1164 | return 0; |
87f0e418 LP |
1165 | } |
1166 | ||
acbb0225 | 1167 | void unit_unwatch_fd(Unit *u, Watch *w) { |
87f0e418 | 1168 | assert(u); |
acbb0225 | 1169 | assert(w); |
87f0e418 | 1170 | |
acbb0225 LP |
1171 | if (w->type == WATCH_INVALID) |
1172 | return; | |
1173 | ||
ea430986 LP |
1174 | assert(w->type == WATCH_FD); |
1175 | assert(w->data.unit == u); | |
acbb0225 LP |
1176 | assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0); |
1177 | ||
1178 | w->fd = -1; | |
1179 | w->type = WATCH_INVALID; | |
ea430986 | 1180 | w->data.unit = NULL; |
87f0e418 LP |
1181 | } |
1182 | ||
1183 | int unit_watch_pid(Unit *u, pid_t pid) { | |
1184 | assert(u); | |
1185 | assert(pid >= 1); | |
1186 | ||
05e343b7 LP |
1187 | /* Watch a specific PID. We only support one unit watching |
1188 | * each PID for now. */ | |
1189 | ||
c6c18be3 | 1190 | return hashmap_put(u->meta.manager->watch_pids, LONG_TO_PTR(pid), u); |
87f0e418 LP |
1191 | } |
1192 | ||
1193 | void unit_unwatch_pid(Unit *u, pid_t pid) { | |
1194 | assert(u); | |
1195 | assert(pid >= 1); | |
1196 | ||
c6c18be3 | 1197 | hashmap_remove_value(u->meta.manager->watch_pids, LONG_TO_PTR(pid), u); |
87f0e418 LP |
1198 | } |
1199 | ||
acbb0225 | 1200 | int unit_watch_timer(Unit *u, usec_t delay, Watch *w) { |
87f0e418 | 1201 | struct itimerspec its; |
acbb0225 | 1202 | int flags, fd; |
87f0e418 LP |
1203 | bool ours; |
1204 | ||
1205 | assert(u); | |
acbb0225 | 1206 | assert(w); |
faf919f1 | 1207 | assert(w->type == WATCH_INVALID || (w->type == WATCH_UNIT_TIMER && w->data.unit == u)); |
87f0e418 LP |
1208 | |
1209 | /* This will try to reuse the old timer if there is one */ | |
1210 | ||
faf919f1 LP |
1211 | if (w->type == WATCH_UNIT_TIMER) { |
1212 | assert(w->data.unit == u); | |
1213 | assert(w->fd >= 0); | |
1214 | ||
87f0e418 | 1215 | ours = false; |
acbb0225 | 1216 | fd = w->fd; |
faf919f1 LP |
1217 | } else if (w->type == WATCH_INVALID) { |
1218 | ||
87f0e418 | 1219 | ours = true; |
87f0e418 LP |
1220 | if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0) |
1221 | return -errno; | |
faf919f1 LP |
1222 | } else |
1223 | assert_not_reached("Invalid watch type"); | |
87f0e418 LP |
1224 | |
1225 | zero(its); | |
1226 | ||
1227 | if (delay <= 0) { | |
1228 | /* Set absolute time in the past, but not 0, since we | |
1229 | * don't want to disarm the timer */ | |
1230 | its.it_value.tv_sec = 0; | |
1231 | its.it_value.tv_nsec = 1; | |
1232 | ||
1233 | flags = TFD_TIMER_ABSTIME; | |
1234 | } else { | |
1235 | timespec_store(&its.it_value, delay); | |
1236 | flags = 0; | |
1237 | } | |
1238 | ||
1239 | /* This will also flush the elapse counter */ | |
1240 | if (timerfd_settime(fd, flags, &its, NULL) < 0) | |
1241 | goto fail; | |
1242 | ||
acbb0225 LP |
1243 | if (w->type == WATCH_INVALID) { |
1244 | struct epoll_event ev; | |
87f0e418 | 1245 | |
acbb0225 LP |
1246 | zero(ev); |
1247 | ev.data.ptr = w; | |
f94ea366 | 1248 | ev.events = EPOLLIN; |
acbb0225 LP |
1249 | |
1250 | if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) | |
1251 | goto fail; | |
1252 | } | |
1253 | ||
faf919f1 | 1254 | w->type = WATCH_UNIT_TIMER; |
acbb0225 | 1255 | w->fd = fd; |
ea430986 | 1256 | w->data.unit = u; |
87f0e418 | 1257 | |
87f0e418 LP |
1258 | return 0; |
1259 | ||
1260 | fail: | |
1261 | if (ours) | |
ea430986 | 1262 | close_nointr_nofail(fd); |
87f0e418 LP |
1263 | |
1264 | return -errno; | |
1265 | } | |
1266 | ||
acbb0225 | 1267 | void unit_unwatch_timer(Unit *u, Watch *w) { |
87f0e418 | 1268 | assert(u); |
acbb0225 | 1269 | assert(w); |
87f0e418 | 1270 | |
acbb0225 | 1271 | if (w->type == WATCH_INVALID) |
87f0e418 LP |
1272 | return; |
1273 | ||
faf919f1 LP |
1274 | assert(w->type == WATCH_UNIT_TIMER); |
1275 | assert(w->data.unit == u); | |
1276 | assert(w->fd >= 0); | |
acbb0225 LP |
1277 | |
1278 | assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0); | |
a16e1123 | 1279 | close_nointr_nofail(w->fd); |
acbb0225 LP |
1280 | |
1281 | w->fd = -1; | |
1282 | w->type = WATCH_INVALID; | |
ea430986 | 1283 | w->data.unit = NULL; |
87f0e418 LP |
1284 | } |
1285 | ||
1286 | bool unit_job_is_applicable(Unit *u, JobType j) { | |
1287 | assert(u); | |
1288 | assert(j >= 0 && j < _JOB_TYPE_MAX); | |
1289 | ||
1290 | switch (j) { | |
1291 | ||
1292 | case JOB_VERIFY_ACTIVE: | |
1293 | case JOB_START: | |
1294 | return true; | |
1295 | ||
1296 | case JOB_STOP: | |
1297 | case JOB_RESTART: | |
1298 | case JOB_TRY_RESTART: | |
1299 | return unit_can_start(u); | |
1300 | ||
1301 | case JOB_RELOAD: | |
1302 | return unit_can_reload(u); | |
1303 | ||
1304 | case JOB_RELOAD_OR_START: | |
1305 | return unit_can_reload(u) && unit_can_start(u); | |
1306 | ||
1307 | default: | |
1308 | assert_not_reached("Invalid job type"); | |
1309 | } | |
1310 | } | |
1311 | ||
701cc384 | 1312 | int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference) { |
87f0e418 LP |
1313 | |
1314 | static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = { | |
1315 | [UNIT_REQUIRES] = UNIT_REQUIRED_BY, | |
9e2f7c11 | 1316 | [UNIT_REQUIRES_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE, |
87f0e418 LP |
1317 | [UNIT_WANTS] = UNIT_WANTED_BY, |
1318 | [UNIT_REQUISITE] = UNIT_REQUIRED_BY, | |
9e2f7c11 | 1319 | [UNIT_REQUISITE_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE, |
87f0e418 | 1320 | [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID, |
9e2f7c11 | 1321 | [UNIT_REQUIRED_BY_OVERRIDABLE] = _UNIT_DEPENDENCY_INVALID, |
87f0e418 | 1322 | [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID, |
69dd2852 LP |
1323 | [UNIT_CONFLICTS] = UNIT_CONFLICTED_BY, |
1324 | [UNIT_CONFLICTED_BY] = UNIT_CONFLICTS, | |
87f0e418 | 1325 | [UNIT_BEFORE] = UNIT_AFTER, |
701cc384 | 1326 | [UNIT_AFTER] = UNIT_BEFORE, |
5de9682c | 1327 | [UNIT_ON_FAILURE] = _UNIT_DEPENDENCY_INVALID, |
701cc384 LP |
1328 | [UNIT_REFERENCES] = UNIT_REFERENCED_BY, |
1329 | [UNIT_REFERENCED_BY] = UNIT_REFERENCES | |
87f0e418 | 1330 | }; |
701cc384 | 1331 | int r, q = 0, v = 0, w = 0; |
87f0e418 LP |
1332 | |
1333 | assert(u); | |
1334 | assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX); | |
87f0e418 LP |
1335 | assert(other); |
1336 | ||
1337 | /* We won't allow dependencies on ourselves. We will not | |
1338 | * consider them an error however. */ | |
1339 | if (u == other) | |
1340 | return 0; | |
1341 | ||
9e2f7c11 LP |
1342 | if (UNIT_VTABLE(u)->no_requires && |
1343 | (d == UNIT_REQUIRES || | |
1344 | d == UNIT_REQUIRES_OVERRIDABLE || | |
1345 | d == UNIT_REQUISITE || | |
1346 | d == UNIT_REQUISITE_OVERRIDABLE)) { | |
1347 | return -EINVAL; | |
1348 | } | |
1349 | ||
5de9682c | 1350 | if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0) |
87f0e418 LP |
1351 | return r; |
1352 | ||
5de9682c LP |
1353 | if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID) |
1354 | if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0) | |
1355 | return r; | |
1356 | ||
701cc384 LP |
1357 | if (add_reference) |
1358 | if ((r = set_ensure_allocated(&u->meta.dependencies[UNIT_REFERENCES], trivial_hash_func, trivial_compare_func)) < 0 || | |
1359 | (r = set_ensure_allocated(&other->meta.dependencies[UNIT_REFERENCED_BY], trivial_hash_func, trivial_compare_func)) < 0) | |
1360 | return r; | |
87f0e418 | 1361 | |
701cc384 LP |
1362 | if ((q = set_put(u->meta.dependencies[d], other)) < 0) |
1363 | return q; | |
87f0e418 | 1364 | |
5de9682c LP |
1365 | if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID) |
1366 | if ((v = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) { | |
1367 | r = v; | |
1368 | goto fail; | |
1369 | } | |
701cc384 LP |
1370 | |
1371 | if (add_reference) { | |
1372 | if ((w = set_put(u->meta.dependencies[UNIT_REFERENCES], other)) < 0) { | |
1373 | r = w; | |
1374 | goto fail; | |
1375 | } | |
1376 | ||
1377 | if ((r = set_put(other->meta.dependencies[UNIT_REFERENCED_BY], u)) < 0) | |
1378 | goto fail; | |
87f0e418 LP |
1379 | } |
1380 | ||
c1e1601e | 1381 | unit_add_to_dbus_queue(u); |
87f0e418 | 1382 | return 0; |
701cc384 LP |
1383 | |
1384 | fail: | |
1385 | if (q > 0) | |
1386 | set_remove(u->meta.dependencies[d], other); | |
1387 | ||
1388 | if (v > 0) | |
1389 | set_remove(other->meta.dependencies[inverse_table[d]], u); | |
1390 | ||
1391 | if (w > 0) | |
1392 | set_remove(u->meta.dependencies[UNIT_REFERENCES], other); | |
1393 | ||
1394 | return r; | |
87f0e418 | 1395 | } |
0301abf4 | 1396 | |
2c966c03 LP |
1397 | int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference) { |
1398 | int r; | |
1399 | ||
1400 | assert(u); | |
1401 | ||
1402 | if ((r = unit_add_dependency(u, d, other, add_reference)) < 0) | |
1403 | return r; | |
1404 | ||
1405 | if ((r = unit_add_dependency(u, e, other, add_reference)) < 0) | |
1406 | return r; | |
1407 | ||
1408 | return 0; | |
1409 | } | |
1410 | ||
9e2f7c11 LP |
1411 | static const char *resolve_template(Unit *u, const char *name, const char*path, char **p) { |
1412 | char *s; | |
1413 | ||
1414 | assert(u); | |
1415 | assert(name || path); | |
1416 | ||
1417 | if (!name) | |
1418 | name = file_name_from_path(path); | |
1419 | ||
1420 | if (!unit_name_is_template(name)) { | |
1421 | *p = NULL; | |
1422 | return name; | |
1423 | } | |
1424 | ||
1425 | if (u->meta.instance) | |
1426 | s = unit_name_replace_instance(name, u->meta.instance); | |
1427 | else { | |
1428 | char *i; | |
1429 | ||
1430 | if (!(i = unit_name_to_prefix(u->meta.id))) | |
1431 | return NULL; | |
1432 | ||
1433 | s = unit_name_replace_instance(name, i); | |
1434 | free(i); | |
1435 | } | |
1436 | ||
1437 | if (!s) | |
1438 | return NULL; | |
1439 | ||
1440 | *p = s; | |
1441 | return s; | |
1442 | } | |
1443 | ||
701cc384 | 1444 | int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) { |
09b6b09f LP |
1445 | Unit *other; |
1446 | int r; | |
9e2f7c11 | 1447 | char *s; |
09b6b09f | 1448 | |
9e2f7c11 LP |
1449 | assert(u); |
1450 | assert(name || path); | |
09b6b09f | 1451 | |
9e2f7c11 LP |
1452 | if (!(name = resolve_template(u, name, path, &s))) |
1453 | return -ENOMEM; | |
09b6b09f | 1454 | |
398ef8ba | 1455 | if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0) |
9e2f7c11 LP |
1456 | goto finish; |
1457 | ||
701cc384 | 1458 | r = unit_add_dependency(u, d, other, add_reference); |
9e2f7c11 LP |
1459 | |
1460 | finish: | |
1461 | free(s); | |
1462 | return r; | |
09b6b09f LP |
1463 | } |
1464 | ||
2c966c03 LP |
1465 | int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) { |
1466 | Unit *other; | |
1467 | int r; | |
1468 | char *s; | |
1469 | ||
1470 | assert(u); | |
1471 | assert(name || path); | |
1472 | ||
1473 | if (!(name = resolve_template(u, name, path, &s))) | |
1474 | return -ENOMEM; | |
1475 | ||
398ef8ba | 1476 | if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0) |
2c966c03 LP |
1477 | goto finish; |
1478 | ||
1479 | r = unit_add_two_dependencies(u, d, e, other, add_reference); | |
1480 | ||
1481 | finish: | |
1482 | free(s); | |
1483 | return r; | |
1484 | } | |
1485 | ||
701cc384 | 1486 | int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) { |
bd77d0fc LP |
1487 | Unit *other; |
1488 | int r; | |
9e2f7c11 | 1489 | char *s; |
bd77d0fc | 1490 | |
9e2f7c11 LP |
1491 | assert(u); |
1492 | assert(name || path); | |
bd77d0fc | 1493 | |
9e2f7c11 LP |
1494 | if (!(name = resolve_template(u, name, path, &s))) |
1495 | return -ENOMEM; | |
bd77d0fc | 1496 | |
398ef8ba | 1497 | if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0) |
9e2f7c11 LP |
1498 | goto finish; |
1499 | ||
701cc384 | 1500 | r = unit_add_dependency(other, d, u, add_reference); |
9e2f7c11 LP |
1501 | |
1502 | finish: | |
1503 | free(s); | |
1504 | return r; | |
bd77d0fc LP |
1505 | } |
1506 | ||
2c966c03 LP |
1507 | int unit_add_two_dependencies_by_name_inverse(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) { |
1508 | Unit *other; | |
1509 | int r; | |
1510 | char *s; | |
1511 | ||
1512 | assert(u); | |
1513 | assert(name || path); | |
1514 | ||
1515 | if (!(name = resolve_template(u, name, path, &s))) | |
1516 | return -ENOMEM; | |
1517 | ||
398ef8ba | 1518 | if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0) |
2c966c03 LP |
1519 | goto finish; |
1520 | ||
1521 | if ((r = unit_add_two_dependencies(other, d, e, u, add_reference)) < 0) | |
1522 | goto finish; | |
1523 | ||
1524 | finish: | |
1525 | free(s); | |
1526 | return r; | |
1527 | } | |
1528 | ||
0301abf4 LP |
1529 | int set_unit_path(const char *p) { |
1530 | char *cwd, *c; | |
1531 | int r; | |
1532 | ||
1533 | /* This is mostly for debug purposes */ | |
1534 | ||
1535 | if (path_is_absolute(p)) { | |
1536 | if (!(c = strdup(p))) | |
1537 | return -ENOMEM; | |
1538 | } else { | |
1539 | if (!(cwd = get_current_dir_name())) | |
1540 | return -errno; | |
1541 | ||
1542 | r = asprintf(&c, "%s/%s", cwd, p); | |
1543 | free(cwd); | |
1544 | ||
1545 | if (r < 0) | |
1546 | return -ENOMEM; | |
1547 | } | |
1548 | ||
036643a2 | 1549 | if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0) { |
0301abf4 LP |
1550 | r = -errno; |
1551 | free(c); | |
1552 | return r; | |
1553 | } | |
1554 | ||
1555 | return 0; | |
1556 | } | |
88066b3a | 1557 | |
ea430986 LP |
1558 | char *unit_dbus_path(Unit *u) { |
1559 | char *p, *e; | |
1560 | ||
1561 | assert(u); | |
1562 | ||
04ade7d2 LP |
1563 | if (!u->meta.id) |
1564 | return NULL; | |
1565 | ||
9e2f7c11 | 1566 | if (!(e = bus_path_escape(u->meta.id))) |
ea430986 LP |
1567 | return NULL; |
1568 | ||
35d2e7ec | 1569 | p = strappend("/org/freedesktop/systemd1/unit/", e); |
ea430986 | 1570 | free(e); |
35d2e7ec | 1571 | |
ea430986 LP |
1572 | return p; |
1573 | } | |
1574 | ||
8e274523 LP |
1575 | int unit_add_cgroup(Unit *u, CGroupBonding *b) { |
1576 | CGroupBonding *l; | |
1577 | int r; | |
1578 | ||
1579 | assert(u); | |
1580 | assert(b); | |
35d2e7ec | 1581 | |
8e274523 LP |
1582 | assert(b->path); |
1583 | ||
35d2e7ec LP |
1584 | if (!b->controller) |
1585 | if (!(b->controller = strdup(SYSTEMD_CGROUP_CONTROLLER))) | |
1586 | return -ENOMEM; | |
1587 | ||
8e274523 LP |
1588 | /* Ensure this hasn't been added yet */ |
1589 | assert(!b->unit); | |
1590 | ||
1591 | l = hashmap_get(u->meta.manager->cgroup_bondings, b->path); | |
1592 | LIST_PREPEND(CGroupBonding, by_path, l, b); | |
1593 | ||
1594 | if ((r = hashmap_replace(u->meta.manager->cgroup_bondings, b->path, l)) < 0) { | |
1595 | LIST_REMOVE(CGroupBonding, by_path, l, b); | |
1596 | return r; | |
1597 | } | |
1598 | ||
1599 | LIST_PREPEND(CGroupBonding, by_unit, u->meta.cgroup_bondings, b); | |
1600 | b->unit = u; | |
1601 | ||
1602 | return 0; | |
1603 | } | |
1604 | ||
013b87c0 LP |
1605 | static char *default_cgroup_path(Unit *u) { |
1606 | char *p; | |
4f2d528d | 1607 | int r; |
013b87c0 LP |
1608 | |
1609 | assert(u); | |
1610 | ||
4f2d528d LP |
1611 | if (u->meta.instance) { |
1612 | char *t; | |
013b87c0 | 1613 | |
4f2d528d LP |
1614 | if (!(t = unit_name_template(u->meta.id))) |
1615 | return NULL; | |
1616 | ||
1617 | r = asprintf(&p, "%s/%s/%s", u->meta.manager->cgroup_hierarchy, t, u->meta.instance); | |
1618 | free(t); | |
1619 | } else | |
1620 | r = asprintf(&p, "%s/%s", u->meta.manager->cgroup_hierarchy, u->meta.id); | |
1621 | ||
1622 | return r < 0 ? NULL : p; | |
013b87c0 LP |
1623 | } |
1624 | ||
8e274523 | 1625 | int unit_add_cgroup_from_text(Unit *u, const char *name) { |
013b87c0 LP |
1626 | char *controller = NULL, *path = NULL; |
1627 | CGroupBonding *b = NULL; | |
8e274523 LP |
1628 | int r; |
1629 | ||
1630 | assert(u); | |
1631 | assert(name); | |
1632 | ||
35d2e7ec LP |
1633 | if ((r = cg_split_spec(name, &controller, &path)) < 0) |
1634 | return r; | |
8e274523 | 1635 | |
35d2e7ec LP |
1636 | if (!path) |
1637 | path = default_cgroup_path(u); | |
013b87c0 | 1638 | |
35d2e7ec | 1639 | if (!controller) |
55096547 | 1640 | controller = strdup(SYSTEMD_CGROUP_CONTROLLER); |
013b87c0 | 1641 | |
35d2e7ec LP |
1642 | if (!path || !controller) { |
1643 | free(path); | |
1644 | free(controller); | |
1645 | ||
1646 | return -ENOMEM; | |
8e274523 LP |
1647 | } |
1648 | ||
013b87c0 LP |
1649 | if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller)) { |
1650 | r = -EEXIST; | |
1651 | goto fail; | |
1652 | } | |
8e274523 | 1653 | |
013b87c0 | 1654 | if (!(b = new0(CGroupBonding, 1))) { |
8e274523 LP |
1655 | r = -ENOMEM; |
1656 | goto fail; | |
1657 | } | |
1658 | ||
013b87c0 LP |
1659 | b->controller = controller; |
1660 | b->path = path; | |
8e274523 LP |
1661 | b->only_us = false; |
1662 | b->clean_up = false; | |
1663 | ||
1664 | if ((r = unit_add_cgroup(u, b)) < 0) | |
1665 | goto fail; | |
1666 | ||
1667 | return 0; | |
1668 | ||
1669 | fail: | |
013b87c0 LP |
1670 | free(path); |
1671 | free(controller); | |
8e274523 LP |
1672 | free(b); |
1673 | ||
1674 | return r; | |
1675 | } | |
1676 | ||
1677 | int unit_add_default_cgroup(Unit *u) { | |
1678 | CGroupBonding *b; | |
1679 | int r = -ENOMEM; | |
1680 | ||
1681 | assert(u); | |
1682 | ||
1683 | /* Adds in the default cgroup data, if it wasn't specified yet */ | |
1684 | ||
1685 | if (unit_get_default_cgroup(u)) | |
1686 | return 0; | |
1687 | ||
1688 | if (!(b = new0(CGroupBonding, 1))) | |
1689 | return -ENOMEM; | |
1690 | ||
013b87c0 | 1691 | if (!(b->path = default_cgroup_path(u))) |
8e274523 LP |
1692 | goto fail; |
1693 | ||
1694 | b->clean_up = true; | |
1695 | b->only_us = true; | |
1696 | ||
1697 | if ((r = unit_add_cgroup(u, b)) < 0) | |
1698 | goto fail; | |
1699 | ||
1700 | return 0; | |
1701 | ||
1702 | fail: | |
1703 | free(b->path); | |
1704 | free(b->controller); | |
1705 | free(b); | |
1706 | ||
1707 | return r; | |
1708 | } | |
1709 | ||
1710 | CGroupBonding* unit_get_default_cgroup(Unit *u) { | |
1711 | assert(u); | |
1712 | ||
55096547 | 1713 | return cgroup_bonding_find_list(u->meta.cgroup_bondings, SYSTEMD_CGROUP_CONTROLLER); |
8e274523 LP |
1714 | } |
1715 | ||
f6ff8c29 LP |
1716 | int unit_load_related_unit(Unit *u, const char *type, Unit **_found) { |
1717 | char *t; | |
1718 | int r; | |
1719 | ||
1720 | assert(u); | |
1721 | assert(type); | |
1722 | assert(_found); | |
1723 | ||
9e2f7c11 | 1724 | if (!(t = unit_name_change_suffix(u->meta.id, type))) |
f6ff8c29 LP |
1725 | return -ENOMEM; |
1726 | ||
1727 | assert(!unit_has_name(u, t)); | |
1728 | ||
398ef8ba | 1729 | r = manager_load_unit(u->meta.manager, t, NULL, NULL, _found); |
f6ff8c29 LP |
1730 | free(t); |
1731 | ||
9e2f7c11 | 1732 | assert(r < 0 || *_found != u); |
f6ff8c29 LP |
1733 | |
1734 | return r; | |
1735 | } | |
1736 | ||
a16e1123 LP |
1737 | int unit_get_related_unit(Unit *u, const char *type, Unit **_found) { |
1738 | Unit *found; | |
1739 | char *t; | |
1740 | ||
1741 | assert(u); | |
1742 | assert(type); | |
1743 | assert(_found); | |
1744 | ||
1745 | if (!(t = unit_name_change_suffix(u->meta.id, type))) | |
1746 | return -ENOMEM; | |
1747 | ||
1748 | assert(!unit_has_name(u, t)); | |
1749 | ||
1750 | found = manager_get_unit(u->meta.manager, t); | |
1751 | free(t); | |
1752 | ||
1753 | if (!found) | |
1754 | return -ENOENT; | |
1755 | ||
1756 | *_found = found; | |
1757 | return 0; | |
1758 | } | |
1759 | ||
9e2f7c11 LP |
1760 | static char *specifier_prefix_and_instance(char specifier, void *data, void *userdata) { |
1761 | Unit *u = userdata; | |
1762 | assert(u); | |
1763 | ||
1764 | return unit_name_to_prefix_and_instance(u->meta.id); | |
1765 | } | |
1766 | ||
1767 | static char *specifier_prefix(char specifier, void *data, void *userdata) { | |
1768 | Unit *u = userdata; | |
1769 | assert(u); | |
1770 | ||
1771 | return unit_name_to_prefix(u->meta.id); | |
1772 | } | |
1773 | ||
1774 | static char *specifier_prefix_unescaped(char specifier, void *data, void *userdata) { | |
1775 | Unit *u = userdata; | |
1776 | char *p, *r; | |
1777 | ||
1778 | assert(u); | |
1779 | ||
1780 | if (!(p = unit_name_to_prefix(u->meta.id))) | |
1781 | return NULL; | |
1782 | ||
1783 | r = unit_name_unescape(p); | |
1784 | free(p); | |
1785 | ||
1786 | return r; | |
1787 | } | |
1788 | ||
1789 | static char *specifier_instance_unescaped(char specifier, void *data, void *userdata) { | |
1790 | Unit *u = userdata; | |
1791 | assert(u); | |
1792 | ||
1793 | if (u->meta.instance) | |
1794 | return unit_name_unescape(u->meta.instance); | |
1795 | ||
1796 | return strdup(""); | |
1797 | } | |
1798 | ||
1799 | char *unit_name_printf(Unit *u, const char* format) { | |
1800 | ||
1801 | /* | |
1802 | * This will use the passed string as format string and | |
1803 | * replace the following specifiers: | |
1804 | * | |
1805 | * %n: the full id of the unit (foo@bar.waldo) | |
1806 | * %N: the id of the unit without the suffix (foo@bar) | |
1807 | * %p: the prefix (foo) | |
1808 | * %i: the instance (bar) | |
1809 | */ | |
1810 | ||
1811 | const Specifier table[] = { | |
1812 | { 'n', specifier_string, u->meta.id }, | |
1813 | { 'N', specifier_prefix_and_instance, NULL }, | |
1814 | { 'p', specifier_prefix, NULL }, | |
1815 | { 'i', specifier_string, u->meta.instance }, | |
1816 | { 0, NULL, NULL } | |
1817 | }; | |
1818 | ||
1819 | assert(u); | |
1820 | assert(format); | |
1821 | ||
1822 | return specifier_printf(format, table, u); | |
1823 | } | |
1824 | ||
1825 | char *unit_full_printf(Unit *u, const char *format) { | |
1826 | ||
1827 | /* This is similar to unit_name_printf() but also supports | |
1828 | * unescaping */ | |
1829 | ||
1830 | const Specifier table[] = { | |
1831 | { 'n', specifier_string, u->meta.id }, | |
1832 | { 'N', specifier_prefix_and_instance, NULL }, | |
1833 | { 'p', specifier_prefix, NULL }, | |
1834 | { 'P', specifier_prefix_unescaped, NULL }, | |
1835 | { 'i', specifier_string, u->meta.instance }, | |
1836 | { 'I', specifier_instance_unescaped, NULL }, | |
1837 | { 0, NULL, NULL } | |
1838 | }; | |
1839 | ||
1840 | assert(u); | |
1841 | assert(format); | |
1842 | ||
1843 | return specifier_printf(format, table, u); | |
1844 | } | |
1845 | ||
1846 | char **unit_full_printf_strv(Unit *u, char **l) { | |
1847 | size_t n; | |
1848 | char **r, **i, **j; | |
1849 | ||
1850 | /* Applies unit_full_printf to every entry in l */ | |
1851 | ||
1852 | assert(u); | |
1853 | ||
1854 | n = strv_length(l); | |
1855 | if (!(r = new(char*, n+1))) | |
1856 | return NULL; | |
1857 | ||
1858 | for (i = l, j = r; *i; i++, j++) | |
1859 | if (!(*j = unit_full_printf(u, *i))) | |
1860 | goto fail; | |
1861 | ||
1862 | *j = NULL; | |
1863 | return r; | |
1864 | ||
1865 | fail: | |
1866 | j--; | |
1867 | while (j >= r) | |
1868 | free(*j); | |
1869 | ||
1870 | free(r); | |
1871 | ||
1872 | return NULL; | |
1873 | } | |
1874 | ||
05e343b7 LP |
1875 | int unit_watch_bus_name(Unit *u, const char *name) { |
1876 | assert(u); | |
1877 | assert(name); | |
1878 | ||
1879 | /* Watch a specific name on the bus. We only support one unit | |
1880 | * watching each name for now. */ | |
1881 | ||
1882 | return hashmap_put(u->meta.manager->watch_bus, name, u); | |
1883 | } | |
1884 | ||
1885 | void unit_unwatch_bus_name(Unit *u, const char *name) { | |
1886 | assert(u); | |
1887 | assert(name); | |
1888 | ||
1889 | hashmap_remove_value(u->meta.manager->watch_bus, name, u); | |
1890 | } | |
1891 | ||
a16e1123 LP |
1892 | bool unit_can_serialize(Unit *u) { |
1893 | assert(u); | |
1894 | ||
1895 | return UNIT_VTABLE(u)->serialize && UNIT_VTABLE(u)->deserialize_item; | |
1896 | } | |
1897 | ||
1898 | int unit_serialize(Unit *u, FILE *f, FDSet *fds) { | |
1899 | int r; | |
1900 | ||
1901 | assert(u); | |
1902 | assert(f); | |
1903 | assert(fds); | |
1904 | ||
1905 | if (!unit_can_serialize(u)) | |
1906 | return 0; | |
1907 | ||
1908 | if ((r = UNIT_VTABLE(u)->serialize(u, f, fds)) < 0) | |
1909 | return r; | |
1910 | ||
cca098b0 LP |
1911 | if (u->meta.job) |
1912 | unit_serialize_item(u, f, "job", job_type_to_string(u->meta.job->type)); | |
1913 | ||
a16e1123 LP |
1914 | /* End marker */ |
1915 | fputc('\n', f); | |
1916 | return 0; | |
1917 | } | |
1918 | ||
1919 | void unit_serialize_item_format(Unit *u, FILE *f, const char *key, const char *format, ...) { | |
1920 | va_list ap; | |
1921 | ||
1922 | assert(u); | |
1923 | assert(f); | |
1924 | assert(key); | |
1925 | assert(format); | |
1926 | ||
1927 | fputs(key, f); | |
1928 | fputc('=', f); | |
1929 | ||
1930 | va_start(ap, format); | |
1931 | vfprintf(f, format, ap); | |
1932 | va_end(ap); | |
1933 | ||
1934 | fputc('\n', f); | |
1935 | } | |
1936 | ||
1937 | void unit_serialize_item(Unit *u, FILE *f, const char *key, const char *value) { | |
1938 | assert(u); | |
1939 | assert(f); | |
1940 | assert(key); | |
1941 | assert(value); | |
1942 | ||
1943 | fprintf(f, "%s=%s\n", key, value); | |
1944 | } | |
1945 | ||
1946 | int unit_deserialize(Unit *u, FILE *f, FDSet *fds) { | |
1947 | int r; | |
1948 | ||
1949 | assert(u); | |
1950 | assert(f); | |
1951 | assert(fds); | |
1952 | ||
1953 | if (!unit_can_serialize(u)) | |
1954 | return 0; | |
1955 | ||
1956 | for (;;) { | |
1957 | char line[1024], *l, *v; | |
1958 | size_t k; | |
1959 | ||
1960 | if (!fgets(line, sizeof(line), f)) { | |
1961 | if (feof(f)) | |
1962 | return 0; | |
1963 | return -errno; | |
1964 | } | |
1965 | ||
1966 | l = strstrip(line); | |
1967 | ||
1968 | /* End marker */ | |
1969 | if (l[0] == 0) | |
1970 | return 0; | |
1971 | ||
1972 | k = strcspn(l, "="); | |
1973 | ||
1974 | if (l[k] == '=') { | |
1975 | l[k] = 0; | |
1976 | v = l+k+1; | |
1977 | } else | |
1978 | v = l+k; | |
1979 | ||
cca098b0 LP |
1980 | if (streq(l, "job")) { |
1981 | JobType type; | |
1982 | ||
1983 | if ((type = job_type_from_string(v)) < 0) | |
1984 | log_debug("Failed to parse job type value %s", v); | |
1985 | else | |
1986 | u->meta.deserialized_job = type; | |
1987 | ||
1988 | continue; | |
1989 | } | |
1990 | ||
a16e1123 LP |
1991 | if ((r = UNIT_VTABLE(u)->deserialize_item(u, l, v, fds)) < 0) |
1992 | return r; | |
1993 | } | |
1994 | } | |
1995 | ||
6e2ef85b LP |
1996 | int unit_add_node_link(Unit *u, const char *what, bool wants) { |
1997 | Unit *device; | |
1998 | char *e; | |
1999 | int r; | |
2000 | ||
2001 | assert(u); | |
2002 | ||
2003 | if (!what) | |
2004 | return 0; | |
2005 | ||
2006 | /* Adds in links to the device node that this unit is based on */ | |
2007 | ||
8407a5d0 | 2008 | if (!is_device_path(what)) |
6e2ef85b LP |
2009 | return 0; |
2010 | ||
2011 | if (!(e = unit_name_build_escape(what+1, NULL, ".device"))) | |
2012 | return -ENOMEM; | |
2013 | ||
398ef8ba | 2014 | r = manager_load_unit(u->meta.manager, e, NULL, NULL, &device); |
6e2ef85b LP |
2015 | free(e); |
2016 | ||
2017 | if (r < 0) | |
2018 | return r; | |
2019 | ||
2c966c03 | 2020 | if ((r = unit_add_two_dependencies(u, UNIT_AFTER, UNIT_REQUIRES, device, true)) < 0) |
6e2ef85b LP |
2021 | return r; |
2022 | ||
2023 | if (wants) | |
2024 | if ((r = unit_add_dependency(device, UNIT_WANTS, u, false)) < 0) | |
2025 | return r; | |
2026 | ||
2027 | return 0; | |
2028 | } | |
a16e1123 | 2029 | |
cca098b0 LP |
2030 | int unit_coldplug(Unit *u) { |
2031 | int r; | |
2032 | ||
2033 | assert(u); | |
2034 | ||
2035 | if (UNIT_VTABLE(u)->coldplug) | |
2036 | if ((r = UNIT_VTABLE(u)->coldplug(u)) < 0) | |
2037 | return r; | |
2038 | ||
2039 | if (u->meta.deserialized_job >= 0) { | |
398ef8ba | 2040 | if ((r = manager_add_job(u->meta.manager, u->meta.deserialized_job, u, JOB_FAIL, false, NULL, NULL)) < 0) |
cca098b0 LP |
2041 | return r; |
2042 | ||
2043 | u->meta.deserialized_job = _JOB_TYPE_INVALID; | |
2044 | } | |
2045 | ||
2046 | return 0; | |
2047 | } | |
2048 | ||
9e58ff9c LP |
2049 | void unit_status_printf(Unit *u, const char *format, ...) { |
2050 | va_list ap; | |
2051 | ||
2052 | assert(u); | |
2053 | assert(format); | |
2054 | ||
2055 | if (!UNIT_VTABLE(u)->show_status) | |
2056 | return; | |
2057 | ||
2058 | if (u->meta.manager->running_as != MANAGER_SYSTEM) | |
2059 | return; | |
2060 | ||
2061 | if (!u->meta.manager->show_status) | |
2062 | return; | |
2063 | ||
2064 | if (!manager_is_booting_or_shutting_down(u->meta.manager)) | |
2065 | return; | |
2066 | ||
2067 | va_start(ap, format); | |
2068 | status_vprintf(format, ap); | |
2069 | va_end(ap); | |
2070 | } | |
2071 | ||
45fb0699 LP |
2072 | bool unit_need_daemon_reload(Unit *u) { |
2073 | struct stat st; | |
2074 | ||
2075 | assert(u); | |
2076 | ||
2077 | if (!u->meta.fragment_path) | |
2078 | return false; | |
2079 | ||
2080 | zero(st); | |
2081 | if (stat(u->meta.fragment_path, &st) < 0) | |
2082 | /* What, cannot access this anymore? */ | |
2083 | return true; | |
2084 | ||
2085 | return | |
2086 | u->meta.fragment_mtime && | |
2087 | timespec_load(&st.st_mtim) != u->meta.fragment_mtime; | |
2088 | } | |
2089 | ||
5632e374 LP |
2090 | void unit_reset_maintenance(Unit *u) { |
2091 | assert(u); | |
2092 | ||
2093 | if (UNIT_VTABLE(u)->reset_maintenance) | |
2094 | UNIT_VTABLE(u)->reset_maintenance(u); | |
2095 | } | |
2096 | ||
a7f241db LP |
2097 | Unit *unit_following(Unit *u) { |
2098 | assert(u); | |
2099 | ||
2100 | if (UNIT_VTABLE(u)->following) | |
2101 | return UNIT_VTABLE(u)->following(u); | |
2102 | ||
2103 | return NULL; | |
2104 | } | |
2105 | ||
94f04347 LP |
2106 | static const char* const unit_type_table[_UNIT_TYPE_MAX] = { |
2107 | [UNIT_SERVICE] = "service", | |
2108 | [UNIT_TIMER] = "timer", | |
2109 | [UNIT_SOCKET] = "socket", | |
2110 | [UNIT_TARGET] = "target", | |
2111 | [UNIT_DEVICE] = "device", | |
2112 | [UNIT_MOUNT] = "mount", | |
2113 | [UNIT_AUTOMOUNT] = "automount", | |
07b0b134 ML |
2114 | [UNIT_SNAPSHOT] = "snapshot", |
2115 | [UNIT_SWAP] = "swap" | |
94f04347 LP |
2116 | }; |
2117 | ||
2118 | DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType); | |
2119 | ||
2120 | static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = { | |
2121 | [UNIT_STUB] = "stub", | |
2122 | [UNIT_LOADED] = "loaded", | |
23a177ef LP |
2123 | [UNIT_FAILED] = "failed", |
2124 | [UNIT_MERGED] = "merged" | |
94f04347 LP |
2125 | }; |
2126 | ||
2127 | DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState); | |
2128 | ||
2129 | static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = { | |
2130 | [UNIT_ACTIVE] = "active", | |
032ff4af | 2131 | [UNIT_RELOADING] = "reloading", |
94f04347 | 2132 | [UNIT_INACTIVE] = "inactive", |
032ff4af | 2133 | [UNIT_MAINTENANCE] = "maintenance", |
94f04347 LP |
2134 | [UNIT_ACTIVATING] = "activating", |
2135 | [UNIT_DEACTIVATING] = "deactivating" | |
2136 | }; | |
2137 | ||
2138 | DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState); | |
2139 | ||
2140 | static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = { | |
2141 | [UNIT_REQUIRES] = "Requires", | |
9e2f7c11 | 2142 | [UNIT_REQUIRES_OVERRIDABLE] = "RequiresOverridable", |
94f04347 LP |
2143 | [UNIT_WANTS] = "Wants", |
2144 | [UNIT_REQUISITE] = "Requisite", | |
9e2f7c11 | 2145 | [UNIT_REQUISITE_OVERRIDABLE] = "RequisiteOverridable", |
94f04347 | 2146 | [UNIT_REQUIRED_BY] = "RequiredBy", |
9e2f7c11 | 2147 | [UNIT_REQUIRED_BY_OVERRIDABLE] = "RequiredByOverridable", |
94f04347 LP |
2148 | [UNIT_WANTED_BY] = "WantedBy", |
2149 | [UNIT_CONFLICTS] = "Conflicts", | |
69dd2852 | 2150 | [UNIT_CONFLICTED_BY] = "ConflictedBy", |
94f04347 LP |
2151 | [UNIT_BEFORE] = "Before", |
2152 | [UNIT_AFTER] = "After", | |
701cc384 | 2153 | [UNIT_REFERENCES] = "References", |
5de9682c LP |
2154 | [UNIT_REFERENCED_BY] = "ReferencedBy", |
2155 | [UNIT_ON_FAILURE] = "OnFailure" | |
94f04347 LP |
2156 | }; |
2157 | ||
2158 | DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency); | |
50159e6a LP |
2159 | |
2160 | static const char* const kill_mode_table[_KILL_MODE_MAX] = { | |
80876c20 | 2161 | [KILL_CONTROL_GROUP] = "control-group", |
50159e6a | 2162 | [KILL_PROCESS_GROUP] = "process-group", |
80876c20 LP |
2163 | [KILL_PROCESS] = "process", |
2164 | [KILL_NONE] = "none" | |
50159e6a LP |
2165 | }; |
2166 | ||
2167 | DEFINE_STRING_TABLE_LOOKUP(kill_mode, KillMode); |