]>
Commit | Line | Data |
---|---|---|
87f0e418 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
3 | #include <assert.h> | |
4 | #include <errno.h> | |
5 | #include <string.h> | |
6 | #include <sys/epoll.h> | |
7 | #include <sys/timerfd.h> | |
8 | #include <sys/poll.h> | |
0301abf4 LP |
9 | #include <stdlib.h> |
10 | #include <unistd.h> | |
87f0e418 LP |
11 | |
12 | #include "set.h" | |
13 | #include "unit.h" | |
14 | #include "macro.h" | |
15 | #include "strv.h" | |
16 | #include "load-fragment.h" | |
17 | #include "load-dropin.h" | |
18 | #include "log.h" | |
19 | ||
20 | const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = { | |
21 | [UNIT_SERVICE] = &service_vtable, | |
22 | [UNIT_TIMER] = &timer_vtable, | |
23 | [UNIT_SOCKET] = &socket_vtable, | |
24 | [UNIT_TARGET] = &target_vtable, | |
25 | [UNIT_DEVICE] = &device_vtable, | |
26 | [UNIT_MOUNT] = &mount_vtable, | |
27 | [UNIT_AUTOMOUNT] = &automount_vtable, | |
28 | [UNIT_SNAPSHOT] = &snapshot_vtable | |
29 | }; | |
30 | ||
31 | UnitType unit_name_to_type(const char *n) { | |
32 | UnitType t; | |
33 | ||
34 | assert(n); | |
35 | ||
36 | for (t = 0; t < _UNIT_TYPE_MAX; t++) | |
37 | if (endswith(n, unit_vtable[t]->suffix)) | |
38 | return t; | |
39 | ||
40 | return _UNIT_TYPE_INVALID; | |
41 | } | |
42 | ||
43 | #define VALID_CHARS \ | |
44 | "0123456789" \ | |
45 | "abcdefghijklmnopqrstuvwxyz" \ | |
46 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ | |
b5ea5d95 | 47 | "-_.\\" |
87f0e418 LP |
48 | |
49 | bool unit_name_is_valid(const char *n) { | |
50 | UnitType t; | |
51 | const char *e, *i; | |
52 | ||
53 | assert(n); | |
54 | ||
55 | if (strlen(n) >= UNIT_NAME_MAX) | |
56 | return false; | |
57 | ||
58 | t = unit_name_to_type(n); | |
59 | if (t < 0 || t >= _UNIT_TYPE_MAX) | |
60 | return false; | |
61 | ||
62 | if (!(e = strrchr(n, '.'))) | |
63 | return false; | |
64 | ||
48e11fe8 LP |
65 | if (e == n) |
66 | return false; | |
67 | ||
87f0e418 LP |
68 | for (i = n; i < e; i++) |
69 | if (!strchr(VALID_CHARS, *i)) | |
70 | return false; | |
71 | ||
72 | return true; | |
73 | } | |
74 | ||
75 | char *unit_name_change_suffix(const char *n, const char *suffix) { | |
76 | char *e, *r; | |
77 | size_t a, b; | |
78 | ||
79 | assert(n); | |
80 | assert(unit_name_is_valid(n)); | |
81 | assert(suffix); | |
82 | ||
83 | assert_se(e = strrchr(n, '.')); | |
84 | a = e - n; | |
85 | b = strlen(suffix); | |
86 | ||
87 | if (!(r = new(char, a + b + 1))) | |
88 | return NULL; | |
89 | ||
90 | memcpy(r, n, a); | |
91 | memcpy(r+a, suffix, b+1); | |
92 | ||
93 | return r; | |
94 | } | |
95 | ||
96 | Unit *unit_new(Manager *m) { | |
97 | Unit *u; | |
98 | ||
99 | assert(m); | |
100 | ||
101 | if (!(u = new0(Unit, 1))) | |
102 | return NULL; | |
103 | ||
104 | if (!(u->meta.names = set_new(string_hash_func, string_compare_func))) { | |
105 | free(u); | |
106 | return NULL; | |
107 | } | |
108 | ||
109 | u->meta.manager = m; | |
110 | u->meta.type = _UNIT_TYPE_INVALID; | |
111 | ||
112 | return u; | |
113 | } | |
114 | ||
115 | int unit_add_name(Unit *u, const char *text) { | |
116 | UnitType t; | |
117 | char *s; | |
118 | int r; | |
119 | ||
120 | assert(u); | |
121 | assert(text); | |
122 | ||
123 | if (!unit_name_is_valid(text)) | |
124 | return -EINVAL; | |
125 | ||
126 | if ((t = unit_name_to_type(text)) == _UNIT_TYPE_INVALID) | |
127 | return -EINVAL; | |
128 | ||
129 | if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type) | |
130 | return -EINVAL; | |
131 | ||
132 | if (!(s = strdup(text))) | |
133 | return -ENOMEM; | |
134 | ||
135 | if ((r = set_put(u->meta.names, s)) < 0) { | |
136 | free(s); | |
137 | ||
138 | if (r == -EEXIST) | |
139 | return 0; | |
140 | ||
141 | return r; | |
142 | } | |
143 | ||
144 | if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) { | |
145 | set_remove(u->meta.names, s); | |
146 | free(s); | |
147 | return r; | |
148 | } | |
149 | ||
ef734fd6 LP |
150 | if (u->meta.type == _UNIT_TYPE_INVALID) |
151 | LIST_PREPEND(Meta, units_per_type, u->meta.manager->units_per_type[t], &u->meta); | |
152 | ||
87f0e418 LP |
153 | u->meta.type = t; |
154 | ||
155 | if (!u->meta.id) | |
156 | u->meta.id = s; | |
157 | ||
158 | return 0; | |
159 | } | |
160 | ||
0ae97ec1 LP |
161 | int unit_choose_id(Unit *u, const char *name) { |
162 | char *s; | |
163 | ||
164 | assert(u); | |
165 | assert(name); | |
166 | ||
167 | /* Selects one of the names of this unit as the id */ | |
168 | ||
169 | if (!(s = set_get(u->meta.names, (char*) name))) | |
170 | return -ENOENT; | |
171 | ||
172 | u->meta.id = s; | |
173 | return 0; | |
174 | } | |
175 | ||
f50e0a01 LP |
176 | int unit_set_description(Unit *u, const char *description) { |
177 | char *s; | |
178 | ||
179 | assert(u); | |
180 | ||
181 | if (!(s = strdup(description))) | |
182 | return -ENOMEM; | |
183 | ||
184 | free(u->meta.description); | |
185 | u->meta.description = s; | |
186 | return 0; | |
187 | } | |
188 | ||
87f0e418 LP |
189 | void unit_add_to_load_queue(Unit *u) { |
190 | assert(u); | |
191 | ||
192 | if (u->meta.load_state != UNIT_STUB || u->meta.in_load_queue) | |
193 | return; | |
194 | ||
195 | LIST_PREPEND(Meta, load_queue, u->meta.manager->load_queue, &u->meta); | |
196 | u->meta.in_load_queue = true; | |
197 | } | |
198 | ||
199 | static void bidi_set_free(Unit *u, Set *s) { | |
200 | Iterator i; | |
201 | Unit *other; | |
202 | ||
203 | assert(u); | |
204 | ||
205 | /* Frees the set and makes sure we are dropped from the | |
206 | * inverse pointers */ | |
207 | ||
208 | SET_FOREACH(other, s, i) { | |
209 | UnitDependency d; | |
210 | ||
211 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) | |
212 | set_remove(other->meta.dependencies[d], u); | |
213 | } | |
214 | ||
215 | set_free(s); | |
216 | } | |
217 | ||
218 | void unit_free(Unit *u) { | |
219 | UnitDependency d; | |
220 | Iterator i; | |
221 | char *t; | |
222 | ||
223 | assert(u); | |
224 | ||
225 | /* Detach from next 'bigger' objects */ | |
226 | ||
227 | SET_FOREACH(t, u->meta.names, i) | |
228 | hashmap_remove_value(u->meta.manager->units, t, u); | |
229 | ||
ef734fd6 LP |
230 | if (u->meta.type != _UNIT_TYPE_INVALID) |
231 | LIST_REMOVE(Meta, units_per_type, u->meta.manager->units_per_type[u->meta.type], &u->meta); | |
232 | ||
87f0e418 LP |
233 | if (u->meta.in_load_queue) |
234 | LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta); | |
235 | ||
236 | if (u->meta.load_state == UNIT_LOADED) | |
237 | if (UNIT_VTABLE(u)->done) | |
238 | UNIT_VTABLE(u)->done(u); | |
239 | ||
240 | /* Free data and next 'smaller' objects */ | |
241 | if (u->meta.job) | |
242 | job_free(u->meta.job); | |
243 | ||
244 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) | |
245 | bidi_set_free(u, u->meta.dependencies[d]); | |
246 | ||
247 | free(u->meta.description); | |
0301abf4 | 248 | free(u->meta.load_path); |
87f0e418 LP |
249 | |
250 | while ((t = set_steal_first(u->meta.names))) | |
251 | free(t); | |
252 | set_free(u->meta.names); | |
253 | ||
254 | free(u); | |
255 | } | |
256 | ||
257 | UnitActiveState unit_active_state(Unit *u) { | |
258 | assert(u); | |
259 | ||
260 | if (u->meta.load_state != UNIT_LOADED) | |
261 | return UNIT_INACTIVE; | |
262 | ||
263 | return UNIT_VTABLE(u)->active_state(u); | |
264 | } | |
265 | ||
266 | static int ensure_merge(Set **s, Set *other) { | |
267 | ||
268 | if (!other) | |
269 | return 0; | |
270 | ||
271 | if (*s) | |
272 | return set_merge(*s, other); | |
273 | ||
274 | if (!(*s = set_copy(other))) | |
275 | return -ENOMEM; | |
276 | ||
277 | return 0; | |
278 | } | |
279 | ||
fdf88f5f LP |
280 | /* FIXME: Does not rollback on failure! Needs to fix special unit |
281 | * pointers. Needs to merge names and dependencies properly.*/ | |
87f0e418 LP |
282 | int unit_merge(Unit *u, Unit *other) { |
283 | int r; | |
284 | UnitDependency d; | |
285 | ||
286 | assert(u); | |
287 | assert(other); | |
288 | assert(u->meta.manager == other->meta.manager); | |
289 | ||
290 | /* This merges 'other' into 'unit'. FIXME: This does not | |
291 | * rollback on failure. */ | |
292 | ||
293 | if (u->meta.type != u->meta.type) | |
294 | return -EINVAL; | |
295 | ||
296 | if (u->meta.load_state != UNIT_STUB) | |
297 | return -EINVAL; | |
298 | ||
299 | /* Merge names */ | |
300 | if ((r = ensure_merge(&u->meta.names, other->meta.names)) < 0) | |
301 | return r; | |
302 | ||
303 | /* Merge dependencies */ | |
304 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) | |
305 | /* fixme, the inverse mapping is missing */ | |
306 | if ((r = ensure_merge(&u->meta.dependencies[d], other->meta.dependencies[d])) < 0) | |
307 | return r; | |
308 | ||
309 | return 0; | |
310 | } | |
311 | ||
312 | const char* unit_id(Unit *u) { | |
313 | assert(u); | |
314 | ||
315 | if (u->meta.id) | |
316 | return u->meta.id; | |
317 | ||
318 | return set_first(u->meta.names); | |
319 | } | |
320 | ||
321 | const char *unit_description(Unit *u) { | |
322 | assert(u); | |
323 | ||
324 | if (u->meta.description) | |
325 | return u->meta.description; | |
326 | ||
327 | return unit_id(u); | |
328 | } | |
329 | ||
330 | void unit_dump(Unit *u, FILE *f, const char *prefix) { | |
331 | ||
87f0e418 LP |
332 | char *t; |
333 | UnitDependency d; | |
334 | Iterator i; | |
335 | char *prefix2; | |
336 | ||
337 | assert(u); | |
338 | ||
339 | if (!prefix) | |
340 | prefix = ""; | |
341 | prefix2 = strappend(prefix, "\t"); | |
342 | if (!prefix2) | |
343 | prefix2 = ""; | |
344 | ||
345 | fprintf(f, | |
346 | "%s→ Unit %s:\n" | |
347 | "%s\tDescription: %s\n" | |
348 | "%s\tUnit Load State: %s\n" | |
f3bff0eb | 349 | "%s\tUnit Active State: %s\n" |
94f04347 | 350 | "%s\tRecursive Stop: %s\n" |
f3bff0eb | 351 | "%s\tStop When Unneeded: %s\n", |
87f0e418 LP |
352 | prefix, unit_id(u), |
353 | prefix, unit_description(u), | |
94f04347 LP |
354 | prefix, unit_load_state_to_string(u->meta.load_state), |
355 | prefix, unit_active_state_to_string(unit_active_state(u)), | |
f3bff0eb LP |
356 | prefix, yes_no(u->meta.recursive_stop), |
357 | prefix, yes_no(u->meta.stop_when_unneeded)); | |
87f0e418 | 358 | |
0301abf4 LP |
359 | if (u->meta.load_path) |
360 | fprintf(f, "%s\tLoad Path: %s\n", prefix, u->meta.load_path); | |
361 | ||
87f0e418 LP |
362 | SET_FOREACH(t, u->meta.names, i) |
363 | fprintf(f, "%s\tName: %s\n", prefix, t); | |
364 | ||
365 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) { | |
366 | Unit *other; | |
367 | ||
368 | if (set_isempty(u->meta.dependencies[d])) | |
369 | continue; | |
370 | ||
371 | SET_FOREACH(other, u->meta.dependencies[d], i) | |
94f04347 | 372 | fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), unit_id(other)); |
87f0e418 LP |
373 | } |
374 | ||
375 | if (UNIT_VTABLE(u)->dump) | |
376 | UNIT_VTABLE(u)->dump(u, f, prefix2); | |
377 | ||
378 | if (u->meta.job) | |
379 | job_dump(u->meta.job, f, prefix2); | |
380 | ||
381 | free(prefix2); | |
382 | } | |
383 | ||
384 | /* Common implementation for multiple backends */ | |
385 | int unit_load_fragment_and_dropin(Unit *u) { | |
d46de8a1 | 386 | int r, ret; |
87f0e418 LP |
387 | |
388 | assert(u); | |
389 | ||
390 | /* Load a .socket file */ | |
391 | if ((r = unit_load_fragment(u)) < 0) | |
392 | return r; | |
393 | ||
d46de8a1 LP |
394 | ret = r > 0; |
395 | ||
87f0e418 LP |
396 | /* Load drop-in directory data */ |
397 | if ((r = unit_load_dropin(u)) < 0) | |
398 | return r; | |
399 | ||
d46de8a1 | 400 | return ret; |
87f0e418 LP |
401 | } |
402 | ||
403 | int unit_load(Unit *u) { | |
404 | int r; | |
405 | ||
406 | assert(u); | |
407 | ||
408 | if (u->meta.in_load_queue) { | |
409 | LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta); | |
410 | u->meta.in_load_queue = false; | |
411 | } | |
412 | ||
413 | if (u->meta.load_state != UNIT_STUB) | |
414 | return 0; | |
415 | ||
416 | if (UNIT_VTABLE(u)->init) | |
417 | if ((r = UNIT_VTABLE(u)->init(u)) < 0) | |
418 | goto fail; | |
419 | ||
420 | u->meta.load_state = UNIT_LOADED; | |
421 | return 0; | |
422 | ||
423 | fail: | |
424 | u->meta.load_state = UNIT_FAILED; | |
425 | return r; | |
426 | } | |
427 | ||
428 | /* Errors: | |
429 | * -EBADR: This unit type does not support starting. | |
430 | * -EALREADY: Unit is already started. | |
431 | * -EAGAIN: An operation is already in progress. Retry later. | |
432 | */ | |
433 | int unit_start(Unit *u) { | |
434 | UnitActiveState state; | |
435 | ||
436 | assert(u); | |
437 | ||
438 | if (!UNIT_VTABLE(u)->start) | |
439 | return -EBADR; | |
440 | ||
441 | state = unit_active_state(u); | |
442 | if (UNIT_IS_ACTIVE_OR_RELOADING(state)) | |
443 | return -EALREADY; | |
444 | ||
445 | /* We don't suppress calls to ->start() here when we are | |
446 | * already starting, to allow this request to be used as a | |
447 | * "hurry up" call, for example when the unit is in some "auto | |
448 | * restart" state where it waits for a holdoff timer to elapse | |
449 | * before it will start again. */ | |
450 | ||
451 | return UNIT_VTABLE(u)->start(u); | |
452 | } | |
453 | ||
454 | bool unit_can_start(Unit *u) { | |
455 | assert(u); | |
456 | ||
457 | return !!UNIT_VTABLE(u)->start; | |
458 | } | |
459 | ||
460 | /* Errors: | |
461 | * -EBADR: This unit type does not support stopping. | |
462 | * -EALREADY: Unit is already stopped. | |
463 | * -EAGAIN: An operation is already in progress. Retry later. | |
464 | */ | |
465 | int unit_stop(Unit *u) { | |
466 | UnitActiveState state; | |
467 | ||
468 | assert(u); | |
469 | ||
470 | if (!UNIT_VTABLE(u)->stop) | |
471 | return -EBADR; | |
472 | ||
473 | state = unit_active_state(u); | |
474 | if (state == UNIT_INACTIVE) | |
475 | return -EALREADY; | |
476 | ||
477 | if (state == UNIT_DEACTIVATING) | |
478 | return 0; | |
479 | ||
480 | return UNIT_VTABLE(u)->stop(u); | |
481 | } | |
482 | ||
483 | /* Errors: | |
484 | * -EBADR: This unit type does not support reloading. | |
485 | * -ENOEXEC: Unit is not started. | |
486 | * -EAGAIN: An operation is already in progress. Retry later. | |
487 | */ | |
488 | int unit_reload(Unit *u) { | |
489 | UnitActiveState state; | |
490 | ||
491 | assert(u); | |
492 | ||
493 | if (!unit_can_reload(u)) | |
494 | return -EBADR; | |
495 | ||
496 | state = unit_active_state(u); | |
497 | if (unit_active_state(u) == UNIT_ACTIVE_RELOADING) | |
498 | return -EALREADY; | |
499 | ||
500 | if (unit_active_state(u) != UNIT_ACTIVE) | |
501 | return -ENOEXEC; | |
502 | ||
503 | return UNIT_VTABLE(u)->reload(u); | |
504 | } | |
505 | ||
506 | bool unit_can_reload(Unit *u) { | |
507 | assert(u); | |
508 | ||
509 | if (!UNIT_VTABLE(u)->reload) | |
510 | return false; | |
511 | ||
512 | if (!UNIT_VTABLE(u)->can_reload) | |
513 | return true; | |
514 | ||
515 | return UNIT_VTABLE(u)->can_reload(u); | |
516 | } | |
517 | ||
f3bff0eb LP |
518 | static void unit_check_uneeded(Unit *u) { |
519 | Iterator i; | |
520 | Unit *other; | |
521 | ||
522 | assert(u); | |
523 | ||
524 | /* If this service shall be shut down when unneeded then do | |
525 | * so. */ | |
526 | ||
527 | if (!u->meta.stop_when_unneeded) | |
528 | return; | |
529 | ||
530 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) | |
531 | return; | |
532 | ||
533 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i) | |
534 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
535 | return; | |
536 | ||
537 | SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRED_BY], i) | |
538 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
539 | return; | |
540 | ||
541 | SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i) | |
542 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
543 | return; | |
544 | ||
545 | log_debug("Service %s is not needed anymore. Stopping.", unit_id(u)); | |
546 | ||
547 | /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */ | |
548 | manager_add_job(u->meta.manager, JOB_STOP, u, JOB_FAIL, true, NULL); | |
549 | } | |
550 | ||
87f0e418 LP |
551 | static void retroactively_start_dependencies(Unit *u) { |
552 | Iterator i; | |
553 | Unit *other; | |
554 | ||
555 | assert(u); | |
556 | assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))); | |
557 | ||
558 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i) | |
559 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
560 | manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL); | |
561 | ||
562 | SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i) | |
563 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
564 | manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL); | |
565 | ||
566 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i) | |
567 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
568 | manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL); | |
569 | ||
570 | SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i) | |
571 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
572 | manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL); | |
573 | ||
574 | SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i) | |
575 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
576 | manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL); | |
577 | } | |
578 | ||
579 | static void retroactively_stop_dependencies(Unit *u) { | |
580 | Iterator i; | |
581 | Unit *other; | |
582 | ||
583 | assert(u); | |
584 | assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))); | |
585 | ||
f3bff0eb LP |
586 | if (u->meta.recursive_stop) { |
587 | /* Pull down units need us recursively if enabled */ | |
588 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i) | |
589 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
590 | manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL); | |
591 | } | |
592 | ||
593 | /* Garbage collect services that might not be needed anymore, if enabled */ | |
594 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i) | |
87f0e418 | 595 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
f3bff0eb LP |
596 | unit_check_uneeded(other); |
597 | SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i) | |
598 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
599 | unit_check_uneeded(other); | |
600 | SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i) | |
601 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
602 | unit_check_uneeded(other); | |
603 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i) | |
604 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
605 | unit_check_uneeded(other); | |
606 | SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUISITE], i) | |
607 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
608 | unit_check_uneeded(other); | |
87f0e418 LP |
609 | } |
610 | ||
611 | void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) { | |
612 | assert(u); | |
613 | assert(os < _UNIT_ACTIVE_STATE_MAX); | |
614 | assert(ns < _UNIT_ACTIVE_STATE_MAX); | |
615 | assert(!(os == UNIT_ACTIVE && ns == UNIT_ACTIVATING)); | |
616 | assert(!(os == UNIT_INACTIVE && ns == UNIT_DEACTIVATING)); | |
617 | ||
618 | if (os == ns) | |
619 | return; | |
620 | ||
621 | if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns)) | |
622 | u->meta.active_enter_timestamp = now(CLOCK_REALTIME); | |
623 | else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns)) | |
624 | u->meta.active_exit_timestamp = now(CLOCK_REALTIME); | |
625 | ||
626 | if (u->meta.job) { | |
627 | ||
628 | if (u->meta.job->state == JOB_WAITING) | |
629 | ||
630 | /* So we reached a different state for this | |
631 | * job. Let's see if we can run it now if it | |
632 | * failed previously due to EAGAIN. */ | |
633 | job_schedule_run(u->meta.job); | |
634 | ||
635 | else { | |
636 | assert(u->meta.job->state == JOB_RUNNING); | |
637 | ||
f50e0a01 | 638 | /* Let's check whether this state change |
87f0e418 LP |
639 | * constitutes a finished job, or maybe |
640 | * cotradicts a running job and hence needs to | |
641 | * invalidate jobs. */ | |
642 | ||
643 | switch (u->meta.job->type) { | |
644 | ||
645 | case JOB_START: | |
646 | case JOB_VERIFY_ACTIVE: | |
647 | ||
648 | if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) { | |
649 | job_finish_and_invalidate(u->meta.job, true); | |
650 | return; | |
651 | } else if (ns == UNIT_ACTIVATING) | |
652 | return; | |
653 | else | |
654 | job_finish_and_invalidate(u->meta.job, false); | |
655 | ||
656 | break; | |
657 | ||
658 | case JOB_RELOAD: | |
659 | case JOB_RELOAD_OR_START: | |
660 | ||
661 | if (ns == UNIT_ACTIVE) { | |
662 | job_finish_and_invalidate(u->meta.job, true); | |
663 | return; | |
664 | } else if (ns == UNIT_ACTIVATING || ns == UNIT_ACTIVE_RELOADING) | |
665 | return; | |
666 | else | |
667 | job_finish_and_invalidate(u->meta.job, false); | |
668 | ||
669 | break; | |
670 | ||
671 | case JOB_STOP: | |
672 | case JOB_RESTART: | |
673 | case JOB_TRY_RESTART: | |
674 | ||
675 | if (ns == UNIT_INACTIVE) { | |
676 | job_finish_and_invalidate(u->meta.job, true); | |
677 | return; | |
678 | } else if (ns == UNIT_DEACTIVATING) | |
679 | return; | |
680 | else | |
681 | job_finish_and_invalidate(u->meta.job, false); | |
682 | ||
683 | break; | |
684 | ||
685 | default: | |
686 | assert_not_reached("Job type unknown"); | |
687 | } | |
688 | } | |
689 | } | |
690 | ||
691 | /* If this state change happened without being requested by a | |
692 | * job, then let's retroactively start or stop dependencies */ | |
693 | ||
694 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns)) | |
695 | retroactively_start_dependencies(u); | |
696 | else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns)) | |
697 | retroactively_stop_dependencies(u); | |
f3bff0eb LP |
698 | |
699 | /* Maybe we finished startup and are now ready for being | |
700 | * stopped because unneeded? */ | |
701 | unit_check_uneeded(u); | |
87f0e418 LP |
702 | } |
703 | ||
acbb0225 | 704 | int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) { |
87f0e418 LP |
705 | struct epoll_event ev; |
706 | ||
707 | assert(u); | |
708 | assert(fd >= 0); | |
acbb0225 | 709 | assert(w); |
ea430986 | 710 | assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u)); |
87f0e418 LP |
711 | |
712 | zero(ev); | |
acbb0225 | 713 | ev.data.ptr = w; |
87f0e418 LP |
714 | ev.events = events; |
715 | ||
acbb0225 LP |
716 | if (epoll_ctl(u->meta.manager->epoll_fd, |
717 | w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD, | |
718 | fd, | |
719 | &ev) < 0) | |
720 | return -errno; | |
87f0e418 | 721 | |
acbb0225 LP |
722 | w->fd = fd; |
723 | w->type = WATCH_FD; | |
ea430986 | 724 | w->data.unit = u; |
87f0e418 | 725 | |
acbb0225 | 726 | return 0; |
87f0e418 LP |
727 | } |
728 | ||
acbb0225 | 729 | void unit_unwatch_fd(Unit *u, Watch *w) { |
87f0e418 | 730 | assert(u); |
acbb0225 | 731 | assert(w); |
87f0e418 | 732 | |
acbb0225 LP |
733 | if (w->type == WATCH_INVALID) |
734 | return; | |
735 | ||
ea430986 LP |
736 | assert(w->type == WATCH_FD); |
737 | assert(w->data.unit == u); | |
acbb0225 LP |
738 | assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0); |
739 | ||
740 | w->fd = -1; | |
741 | w->type = WATCH_INVALID; | |
ea430986 | 742 | w->data.unit = NULL; |
87f0e418 LP |
743 | } |
744 | ||
745 | int unit_watch_pid(Unit *u, pid_t pid) { | |
746 | assert(u); | |
747 | assert(pid >= 1); | |
748 | ||
749 | return hashmap_put(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u); | |
750 | } | |
751 | ||
752 | void unit_unwatch_pid(Unit *u, pid_t pid) { | |
753 | assert(u); | |
754 | assert(pid >= 1); | |
755 | ||
756 | hashmap_remove(u->meta.manager->watch_pids, UINT32_TO_PTR(pid)); | |
757 | } | |
758 | ||
acbb0225 | 759 | int unit_watch_timer(Unit *u, usec_t delay, Watch *w) { |
87f0e418 | 760 | struct itimerspec its; |
acbb0225 | 761 | int flags, fd; |
87f0e418 LP |
762 | bool ours; |
763 | ||
764 | assert(u); | |
acbb0225 | 765 | assert(w); |
ea430986 | 766 | assert(w->type == WATCH_INVALID || (w->type == WATCH_TIMER && w->data.unit == u)); |
87f0e418 LP |
767 | |
768 | /* This will try to reuse the old timer if there is one */ | |
769 | ||
acbb0225 | 770 | if (w->type == WATCH_TIMER) { |
87f0e418 | 771 | ours = false; |
acbb0225 | 772 | fd = w->fd; |
87f0e418 LP |
773 | } else { |
774 | ours = true; | |
87f0e418 LP |
775 | if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0) |
776 | return -errno; | |
777 | } | |
778 | ||
779 | zero(its); | |
780 | ||
781 | if (delay <= 0) { | |
782 | /* Set absolute time in the past, but not 0, since we | |
783 | * don't want to disarm the timer */ | |
784 | its.it_value.tv_sec = 0; | |
785 | its.it_value.tv_nsec = 1; | |
786 | ||
787 | flags = TFD_TIMER_ABSTIME; | |
788 | } else { | |
789 | timespec_store(&its.it_value, delay); | |
790 | flags = 0; | |
791 | } | |
792 | ||
793 | /* This will also flush the elapse counter */ | |
794 | if (timerfd_settime(fd, flags, &its, NULL) < 0) | |
795 | goto fail; | |
796 | ||
acbb0225 LP |
797 | if (w->type == WATCH_INVALID) { |
798 | struct epoll_event ev; | |
87f0e418 | 799 | |
acbb0225 LP |
800 | zero(ev); |
801 | ev.data.ptr = w; | |
f94ea366 | 802 | ev.events = EPOLLIN; |
acbb0225 LP |
803 | |
804 | if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) | |
805 | goto fail; | |
806 | } | |
807 | ||
808 | w->fd = fd; | |
809 | w->type = WATCH_TIMER; | |
ea430986 | 810 | w->data.unit = u; |
87f0e418 | 811 | |
87f0e418 LP |
812 | return 0; |
813 | ||
814 | fail: | |
815 | if (ours) | |
ea430986 | 816 | close_nointr_nofail(fd); |
87f0e418 LP |
817 | |
818 | return -errno; | |
819 | } | |
820 | ||
acbb0225 | 821 | void unit_unwatch_timer(Unit *u, Watch *w) { |
87f0e418 | 822 | assert(u); |
acbb0225 | 823 | assert(w); |
87f0e418 | 824 | |
acbb0225 | 825 | if (w->type == WATCH_INVALID) |
87f0e418 LP |
826 | return; |
827 | ||
ea430986 | 828 | assert(w->type == WATCH_TIMER && w->data.unit == u); |
acbb0225 LP |
829 | |
830 | assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0); | |
831 | assert_se(close_nointr(w->fd) == 0); | |
832 | ||
833 | w->fd = -1; | |
834 | w->type = WATCH_INVALID; | |
ea430986 | 835 | w->data.unit = NULL; |
87f0e418 LP |
836 | } |
837 | ||
838 | bool unit_job_is_applicable(Unit *u, JobType j) { | |
839 | assert(u); | |
840 | assert(j >= 0 && j < _JOB_TYPE_MAX); | |
841 | ||
842 | switch (j) { | |
843 | ||
844 | case JOB_VERIFY_ACTIVE: | |
845 | case JOB_START: | |
846 | return true; | |
847 | ||
848 | case JOB_STOP: | |
849 | case JOB_RESTART: | |
850 | case JOB_TRY_RESTART: | |
851 | return unit_can_start(u); | |
852 | ||
853 | case JOB_RELOAD: | |
854 | return unit_can_reload(u); | |
855 | ||
856 | case JOB_RELOAD_OR_START: | |
857 | return unit_can_reload(u) && unit_can_start(u); | |
858 | ||
859 | default: | |
860 | assert_not_reached("Invalid job type"); | |
861 | } | |
862 | } | |
863 | ||
864 | int unit_add_dependency(Unit *u, UnitDependency d, Unit *other) { | |
865 | ||
866 | static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = { | |
867 | [UNIT_REQUIRES] = UNIT_REQUIRED_BY, | |
868 | [UNIT_SOFT_REQUIRES] = UNIT_SOFT_REQUIRED_BY, | |
869 | [UNIT_WANTS] = UNIT_WANTED_BY, | |
870 | [UNIT_REQUISITE] = UNIT_REQUIRED_BY, | |
871 | [UNIT_SOFT_REQUISITE] = UNIT_SOFT_REQUIRED_BY, | |
872 | [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID, | |
873 | [UNIT_SOFT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID, | |
874 | [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID, | |
875 | [UNIT_CONFLICTS] = UNIT_CONFLICTS, | |
876 | [UNIT_BEFORE] = UNIT_AFTER, | |
877 | [UNIT_AFTER] = UNIT_BEFORE | |
878 | }; | |
879 | int r; | |
880 | ||
881 | assert(u); | |
882 | assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX); | |
883 | assert(inverse_table[d] != _UNIT_DEPENDENCY_INVALID); | |
884 | assert(other); | |
885 | ||
886 | /* We won't allow dependencies on ourselves. We will not | |
887 | * consider them an error however. */ | |
888 | if (u == other) | |
889 | return 0; | |
890 | ||
891 | if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0) | |
892 | return r; | |
893 | ||
894 | if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0) | |
895 | return r; | |
896 | ||
897 | if ((r = set_put(u->meta.dependencies[d], other)) < 0) | |
898 | return r; | |
899 | ||
900 | if ((r = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) { | |
901 | set_remove(u->meta.dependencies[d], other); | |
902 | return r; | |
903 | } | |
904 | ||
905 | return 0; | |
906 | } | |
0301abf4 | 907 | |
09b6b09f LP |
908 | int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name) { |
909 | Unit *other; | |
910 | int r; | |
911 | ||
912 | if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0) | |
913 | return r; | |
914 | ||
915 | if ((r = unit_add_dependency(u, d, other)) < 0) | |
916 | return r; | |
917 | ||
918 | return 0; | |
919 | } | |
920 | ||
0301abf4 LP |
921 | const char *unit_path(void) { |
922 | char *e; | |
923 | ||
924 | if ((e = getenv("UNIT_PATH"))) | |
925 | if (path_is_absolute(e)) | |
926 | return e; | |
927 | ||
928 | return UNIT_PATH; | |
929 | } | |
930 | ||
931 | int set_unit_path(const char *p) { | |
932 | char *cwd, *c; | |
933 | int r; | |
934 | ||
935 | /* This is mostly for debug purposes */ | |
936 | ||
937 | if (path_is_absolute(p)) { | |
938 | if (!(c = strdup(p))) | |
939 | return -ENOMEM; | |
940 | } else { | |
941 | if (!(cwd = get_current_dir_name())) | |
942 | return -errno; | |
943 | ||
944 | r = asprintf(&c, "%s/%s", cwd, p); | |
945 | free(cwd); | |
946 | ||
947 | if (r < 0) | |
948 | return -ENOMEM; | |
949 | } | |
950 | ||
951 | if (setenv("UNIT_PATH", c, 0) < 0) { | |
952 | r = -errno; | |
953 | free(c); | |
954 | return r; | |
955 | } | |
956 | ||
957 | return 0; | |
958 | } | |
88066b3a | 959 | |
b08d03ff | 960 | char *unit_name_escape_path(const char *prefix, const char *path, const char *suffix) { |
88066b3a LP |
961 | char *r, *t; |
962 | const char *f; | |
b08d03ff | 963 | size_t a, b, c; |
88066b3a LP |
964 | |
965 | assert(path); | |
88066b3a | 966 | |
b08d03ff LP |
967 | /* Takes a path and a suffix and prefix and makes a nice |
968 | * string suitable as unit name of it, escaping all weird | |
969 | * chars on the way. | |
88066b3a | 970 | * |
b08d03ff LP |
971 | * / becomes ., and all chars not alloweed in a unit name get |
972 | * escaped as \xFF, including \ and ., of course. This | |
973 | * escaping is hence reversible. | |
88066b3a LP |
974 | */ |
975 | ||
b08d03ff LP |
976 | if (!prefix) |
977 | prefix = ""; | |
978 | ||
979 | if (!suffix) | |
980 | suffix = ""; | |
88066b3a | 981 | |
b08d03ff LP |
982 | a = strlen(prefix); |
983 | b = strlen(path); | |
984 | c = strlen(suffix); | |
985 | ||
986 | if (!(r = new(char, a+b*4+c+1))) | |
88066b3a LP |
987 | return NULL; |
988 | ||
b08d03ff LP |
989 | memcpy(r, prefix, a); |
990 | ||
991 | for (f = path, t = r+a; *f; f++) { | |
88066b3a | 992 | if (*f == '/') |
b08d03ff LP |
993 | *(t++) = '.'; |
994 | else if (*f == '.' || *f == '\\' || !strchr(VALID_CHARS, *f)) { | |
88066b3a LP |
995 | *(t++) = '\\'; |
996 | *(t++) = 'x'; | |
997 | *(t++) = hexchar(*f > 4); | |
998 | *(t++) = hexchar(*f); | |
999 | } else | |
1000 | *(t++) = *f; | |
1001 | } | |
1002 | ||
b08d03ff | 1003 | memcpy(t, suffix, c+1); |
88066b3a LP |
1004 | |
1005 | return r; | |
1006 | } | |
94f04347 | 1007 | |
ea430986 LP |
1008 | char *unit_dbus_path(Unit *u) { |
1009 | char *p, *e; | |
1010 | ||
1011 | assert(u); | |
1012 | ||
1013 | if (!(e = bus_path_escape(unit_id(u)))) | |
1014 | return NULL; | |
1015 | ||
1016 | if (asprintf(&p, "/org/freedesktop/systemd1/unit/%s", e) < 0) { | |
1017 | free(e); | |
1018 | return NULL; | |
1019 | } | |
1020 | ||
1021 | free(e); | |
1022 | return p; | |
1023 | } | |
1024 | ||
94f04347 LP |
1025 | static const char* const unit_type_table[_UNIT_TYPE_MAX] = { |
1026 | [UNIT_SERVICE] = "service", | |
1027 | [UNIT_TIMER] = "timer", | |
1028 | [UNIT_SOCKET] = "socket", | |
1029 | [UNIT_TARGET] = "target", | |
1030 | [UNIT_DEVICE] = "device", | |
1031 | [UNIT_MOUNT] = "mount", | |
1032 | [UNIT_AUTOMOUNT] = "automount", | |
1033 | [UNIT_SNAPSHOT] = "snapshot" | |
1034 | }; | |
1035 | ||
1036 | DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType); | |
1037 | ||
1038 | static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = { | |
1039 | [UNIT_STUB] = "stub", | |
1040 | [UNIT_LOADED] = "loaded", | |
1041 | [UNIT_FAILED] = "failed" | |
1042 | }; | |
1043 | ||
1044 | DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState); | |
1045 | ||
1046 | static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = { | |
1047 | [UNIT_ACTIVE] = "active", | |
1048 | [UNIT_INACTIVE] = "inactive", | |
1049 | [UNIT_ACTIVATING] = "activating", | |
1050 | [UNIT_DEACTIVATING] = "deactivating" | |
1051 | }; | |
1052 | ||
1053 | DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState); | |
1054 | ||
1055 | static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = { | |
1056 | [UNIT_REQUIRES] = "Requires", | |
1057 | [UNIT_SOFT_REQUIRES] = "SoftRequires", | |
1058 | [UNIT_WANTS] = "Wants", | |
1059 | [UNIT_REQUISITE] = "Requisite", | |
1060 | [UNIT_SOFT_REQUISITE] = "SoftRequisite", | |
1061 | [UNIT_REQUIRED_BY] = "RequiredBy", | |
1062 | [UNIT_SOFT_REQUIRED_BY] = "SoftRequiredBy", | |
1063 | [UNIT_WANTED_BY] = "WantedBy", | |
1064 | [UNIT_CONFLICTS] = "Conflicts", | |
1065 | [UNIT_BEFORE] = "Before", | |
1066 | [UNIT_AFTER] = "After", | |
1067 | }; | |
1068 | ||
1069 | DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency); |