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