]>
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 | ||
332 | static const char* const load_state_table[_UNIT_LOAD_STATE_MAX] = { | |
333 | [UNIT_STUB] = "stub", | |
334 | [UNIT_LOADED] = "loaded", | |
335 | [UNIT_FAILED] = "failed" | |
336 | }; | |
337 | ||
338 | static const char* const active_state_table[_UNIT_ACTIVE_STATE_MAX] = { | |
339 | [UNIT_ACTIVE] = "active", | |
340 | [UNIT_INACTIVE] = "inactive", | |
341 | [UNIT_ACTIVATING] = "activating", | |
342 | [UNIT_DEACTIVATING] = "deactivating" | |
343 | }; | |
344 | ||
345 | static const char* const dependency_table[_UNIT_DEPENDENCY_MAX] = { | |
346 | [UNIT_REQUIRES] = "Requires", | |
347 | [UNIT_SOFT_REQUIRES] = "SoftRequires", | |
348 | [UNIT_WANTS] = "Wants", | |
349 | [UNIT_REQUISITE] = "Requisite", | |
350 | [UNIT_SOFT_REQUISITE] = "SoftRequisite", | |
351 | [UNIT_REQUIRED_BY] = "RequiredBy", | |
352 | [UNIT_SOFT_REQUIRED_BY] = "SoftRequiredBy", | |
353 | [UNIT_WANTED_BY] = "WantedBy", | |
354 | [UNIT_CONFLICTS] = "Conflicts", | |
355 | [UNIT_BEFORE] = "Before", | |
356 | [UNIT_AFTER] = "After", | |
357 | }; | |
358 | ||
359 | char *t; | |
360 | UnitDependency d; | |
361 | Iterator i; | |
362 | char *prefix2; | |
363 | ||
364 | assert(u); | |
365 | ||
366 | if (!prefix) | |
367 | prefix = ""; | |
368 | prefix2 = strappend(prefix, "\t"); | |
369 | if (!prefix2) | |
370 | prefix2 = ""; | |
371 | ||
372 | fprintf(f, | |
373 | "%s→ Unit %s:\n" | |
374 | "%s\tDescription: %s\n" | |
375 | "%s\tUnit Load State: %s\n" | |
376 | "%s\tUnit Active State: %s\n", | |
377 | prefix, unit_id(u), | |
378 | prefix, unit_description(u), | |
379 | prefix, load_state_table[u->meta.load_state], | |
380 | prefix, active_state_table[unit_active_state(u)]); | |
381 | ||
0301abf4 LP |
382 | if (u->meta.load_path) |
383 | fprintf(f, "%s\tLoad Path: %s\n", prefix, u->meta.load_path); | |
384 | ||
87f0e418 LP |
385 | SET_FOREACH(t, u->meta.names, i) |
386 | fprintf(f, "%s\tName: %s\n", prefix, t); | |
387 | ||
388 | for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) { | |
389 | Unit *other; | |
390 | ||
391 | if (set_isempty(u->meta.dependencies[d])) | |
392 | continue; | |
393 | ||
394 | SET_FOREACH(other, u->meta.dependencies[d], i) | |
395 | fprintf(f, "%s\t%s: %s\n", prefix, dependency_table[d], unit_id(other)); | |
396 | } | |
397 | ||
398 | if (UNIT_VTABLE(u)->dump) | |
399 | UNIT_VTABLE(u)->dump(u, f, prefix2); | |
400 | ||
401 | if (u->meta.job) | |
402 | job_dump(u->meta.job, f, prefix2); | |
403 | ||
404 | free(prefix2); | |
405 | } | |
406 | ||
407 | /* Common implementation for multiple backends */ | |
408 | int unit_load_fragment_and_dropin(Unit *u) { | |
d46de8a1 | 409 | int r, ret; |
87f0e418 LP |
410 | |
411 | assert(u); | |
412 | ||
413 | /* Load a .socket file */ | |
414 | if ((r = unit_load_fragment(u)) < 0) | |
415 | return r; | |
416 | ||
d46de8a1 LP |
417 | ret = r > 0; |
418 | ||
87f0e418 LP |
419 | /* Load drop-in directory data */ |
420 | if ((r = unit_load_dropin(u)) < 0) | |
421 | return r; | |
422 | ||
d46de8a1 | 423 | return ret; |
87f0e418 LP |
424 | } |
425 | ||
426 | int unit_load(Unit *u) { | |
427 | int r; | |
428 | ||
429 | assert(u); | |
430 | ||
431 | if (u->meta.in_load_queue) { | |
432 | LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta); | |
433 | u->meta.in_load_queue = false; | |
434 | } | |
435 | ||
436 | if (u->meta.load_state != UNIT_STUB) | |
437 | return 0; | |
438 | ||
439 | if (UNIT_VTABLE(u)->init) | |
440 | if ((r = UNIT_VTABLE(u)->init(u)) < 0) | |
441 | goto fail; | |
442 | ||
443 | u->meta.load_state = UNIT_LOADED; | |
444 | return 0; | |
445 | ||
446 | fail: | |
447 | u->meta.load_state = UNIT_FAILED; | |
448 | return r; | |
449 | } | |
450 | ||
451 | /* Errors: | |
452 | * -EBADR: This unit type does not support starting. | |
453 | * -EALREADY: Unit is already started. | |
454 | * -EAGAIN: An operation is already in progress. Retry later. | |
455 | */ | |
456 | int unit_start(Unit *u) { | |
457 | UnitActiveState state; | |
458 | ||
459 | assert(u); | |
460 | ||
461 | if (!UNIT_VTABLE(u)->start) | |
462 | return -EBADR; | |
463 | ||
464 | state = unit_active_state(u); | |
465 | if (UNIT_IS_ACTIVE_OR_RELOADING(state)) | |
466 | return -EALREADY; | |
467 | ||
468 | /* We don't suppress calls to ->start() here when we are | |
469 | * already starting, to allow this request to be used as a | |
470 | * "hurry up" call, for example when the unit is in some "auto | |
471 | * restart" state where it waits for a holdoff timer to elapse | |
472 | * before it will start again. */ | |
473 | ||
474 | return UNIT_VTABLE(u)->start(u); | |
475 | } | |
476 | ||
477 | bool unit_can_start(Unit *u) { | |
478 | assert(u); | |
479 | ||
480 | return !!UNIT_VTABLE(u)->start; | |
481 | } | |
482 | ||
483 | /* Errors: | |
484 | * -EBADR: This unit type does not support stopping. | |
485 | * -EALREADY: Unit is already stopped. | |
486 | * -EAGAIN: An operation is already in progress. Retry later. | |
487 | */ | |
488 | int unit_stop(Unit *u) { | |
489 | UnitActiveState state; | |
490 | ||
491 | assert(u); | |
492 | ||
493 | if (!UNIT_VTABLE(u)->stop) | |
494 | return -EBADR; | |
495 | ||
496 | state = unit_active_state(u); | |
497 | if (state == UNIT_INACTIVE) | |
498 | return -EALREADY; | |
499 | ||
500 | if (state == UNIT_DEACTIVATING) | |
501 | return 0; | |
502 | ||
503 | return UNIT_VTABLE(u)->stop(u); | |
504 | } | |
505 | ||
506 | /* Errors: | |
507 | * -EBADR: This unit type does not support reloading. | |
508 | * -ENOEXEC: Unit is not started. | |
509 | * -EAGAIN: An operation is already in progress. Retry later. | |
510 | */ | |
511 | int unit_reload(Unit *u) { | |
512 | UnitActiveState state; | |
513 | ||
514 | assert(u); | |
515 | ||
516 | if (!unit_can_reload(u)) | |
517 | return -EBADR; | |
518 | ||
519 | state = unit_active_state(u); | |
520 | if (unit_active_state(u) == UNIT_ACTIVE_RELOADING) | |
521 | return -EALREADY; | |
522 | ||
523 | if (unit_active_state(u) != UNIT_ACTIVE) | |
524 | return -ENOEXEC; | |
525 | ||
526 | return UNIT_VTABLE(u)->reload(u); | |
527 | } | |
528 | ||
529 | bool unit_can_reload(Unit *u) { | |
530 | assert(u); | |
531 | ||
532 | if (!UNIT_VTABLE(u)->reload) | |
533 | return false; | |
534 | ||
535 | if (!UNIT_VTABLE(u)->can_reload) | |
536 | return true; | |
537 | ||
538 | return UNIT_VTABLE(u)->can_reload(u); | |
539 | } | |
540 | ||
541 | static void retroactively_start_dependencies(Unit *u) { | |
542 | Iterator i; | |
543 | Unit *other; | |
544 | ||
545 | assert(u); | |
546 | assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))); | |
547 | ||
548 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i) | |
549 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
550 | manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL); | |
551 | ||
552 | SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i) | |
553 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
554 | manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL); | |
555 | ||
556 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i) | |
557 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
558 | manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL); | |
559 | ||
560 | SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i) | |
561 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
562 | manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL); | |
563 | ||
564 | SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i) | |
565 | if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) | |
566 | manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL); | |
567 | } | |
568 | ||
569 | static void retroactively_stop_dependencies(Unit *u) { | |
570 | Iterator i; | |
571 | Unit *other; | |
572 | ||
573 | assert(u); | |
574 | assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))); | |
575 | ||
576 | SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i) | |
577 | if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) | |
578 | manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL); | |
579 | } | |
580 | ||
581 | void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) { | |
582 | assert(u); | |
583 | assert(os < _UNIT_ACTIVE_STATE_MAX); | |
584 | assert(ns < _UNIT_ACTIVE_STATE_MAX); | |
585 | assert(!(os == UNIT_ACTIVE && ns == UNIT_ACTIVATING)); | |
586 | assert(!(os == UNIT_INACTIVE && ns == UNIT_DEACTIVATING)); | |
587 | ||
588 | if (os == ns) | |
589 | return; | |
590 | ||
591 | if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns)) | |
592 | u->meta.active_enter_timestamp = now(CLOCK_REALTIME); | |
593 | else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns)) | |
594 | u->meta.active_exit_timestamp = now(CLOCK_REALTIME); | |
595 | ||
596 | if (u->meta.job) { | |
597 | ||
598 | if (u->meta.job->state == JOB_WAITING) | |
599 | ||
600 | /* So we reached a different state for this | |
601 | * job. Let's see if we can run it now if it | |
602 | * failed previously due to EAGAIN. */ | |
603 | job_schedule_run(u->meta.job); | |
604 | ||
605 | else { | |
606 | assert(u->meta.job->state == JOB_RUNNING); | |
607 | ||
f50e0a01 | 608 | /* Let's check whether this state change |
87f0e418 LP |
609 | * constitutes a finished job, or maybe |
610 | * cotradicts a running job and hence needs to | |
611 | * invalidate jobs. */ | |
612 | ||
613 | switch (u->meta.job->type) { | |
614 | ||
615 | case JOB_START: | |
616 | case JOB_VERIFY_ACTIVE: | |
617 | ||
618 | if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) { | |
619 | job_finish_and_invalidate(u->meta.job, true); | |
620 | return; | |
621 | } else if (ns == UNIT_ACTIVATING) | |
622 | return; | |
623 | else | |
624 | job_finish_and_invalidate(u->meta.job, false); | |
625 | ||
626 | break; | |
627 | ||
628 | case JOB_RELOAD: | |
629 | case JOB_RELOAD_OR_START: | |
630 | ||
631 | if (ns == UNIT_ACTIVE) { | |
632 | job_finish_and_invalidate(u->meta.job, true); | |
633 | return; | |
634 | } else if (ns == UNIT_ACTIVATING || ns == UNIT_ACTIVE_RELOADING) | |
635 | return; | |
636 | else | |
637 | job_finish_and_invalidate(u->meta.job, false); | |
638 | ||
639 | break; | |
640 | ||
641 | case JOB_STOP: | |
642 | case JOB_RESTART: | |
643 | case JOB_TRY_RESTART: | |
644 | ||
645 | if (ns == UNIT_INACTIVE) { | |
646 | job_finish_and_invalidate(u->meta.job, true); | |
647 | return; | |
648 | } else if (ns == UNIT_DEACTIVATING) | |
649 | return; | |
650 | else | |
651 | job_finish_and_invalidate(u->meta.job, false); | |
652 | ||
653 | break; | |
654 | ||
655 | default: | |
656 | assert_not_reached("Job type unknown"); | |
657 | } | |
658 | } | |
659 | } | |
660 | ||
661 | /* If this state change happened without being requested by a | |
662 | * job, then let's retroactively start or stop dependencies */ | |
663 | ||
664 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns)) | |
665 | retroactively_start_dependencies(u); | |
666 | else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns)) | |
667 | retroactively_stop_dependencies(u); | |
668 | } | |
669 | ||
acbb0225 | 670 | int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) { |
87f0e418 LP |
671 | struct epoll_event ev; |
672 | ||
673 | assert(u); | |
674 | assert(fd >= 0); | |
acbb0225 LP |
675 | assert(w); |
676 | assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->unit == u)); | |
87f0e418 LP |
677 | |
678 | zero(ev); | |
acbb0225 | 679 | ev.data.ptr = w; |
87f0e418 LP |
680 | ev.events = events; |
681 | ||
acbb0225 LP |
682 | if (epoll_ctl(u->meta.manager->epoll_fd, |
683 | w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD, | |
684 | fd, | |
685 | &ev) < 0) | |
686 | return -errno; | |
87f0e418 | 687 | |
acbb0225 LP |
688 | w->fd = fd; |
689 | w->type = WATCH_FD; | |
690 | w->unit = u; | |
87f0e418 | 691 | |
acbb0225 | 692 | return 0; |
87f0e418 LP |
693 | } |
694 | ||
acbb0225 | 695 | void unit_unwatch_fd(Unit *u, Watch *w) { |
87f0e418 | 696 | assert(u); |
acbb0225 | 697 | assert(w); |
87f0e418 | 698 | |
acbb0225 LP |
699 | if (w->type == WATCH_INVALID) |
700 | return; | |
701 | ||
702 | assert(w->type == WATCH_FD && w->unit == u); | |
703 | assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0); | |
704 | ||
705 | w->fd = -1; | |
706 | w->type = WATCH_INVALID; | |
707 | w->unit = NULL; | |
87f0e418 LP |
708 | } |
709 | ||
710 | int unit_watch_pid(Unit *u, pid_t pid) { | |
711 | assert(u); | |
712 | assert(pid >= 1); | |
713 | ||
714 | return hashmap_put(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u); | |
715 | } | |
716 | ||
717 | void unit_unwatch_pid(Unit *u, pid_t pid) { | |
718 | assert(u); | |
719 | assert(pid >= 1); | |
720 | ||
721 | hashmap_remove(u->meta.manager->watch_pids, UINT32_TO_PTR(pid)); | |
722 | } | |
723 | ||
acbb0225 | 724 | int unit_watch_timer(Unit *u, usec_t delay, Watch *w) { |
87f0e418 | 725 | struct itimerspec its; |
acbb0225 | 726 | int flags, fd; |
87f0e418 LP |
727 | bool ours; |
728 | ||
729 | assert(u); | |
acbb0225 LP |
730 | assert(w); |
731 | assert(w->type == WATCH_INVALID || (w->type == WATCH_TIMER && w->unit == u)); | |
87f0e418 LP |
732 | |
733 | /* This will try to reuse the old timer if there is one */ | |
734 | ||
acbb0225 | 735 | if (w->type == WATCH_TIMER) { |
87f0e418 | 736 | ours = false; |
acbb0225 | 737 | fd = w->fd; |
87f0e418 LP |
738 | } else { |
739 | ours = true; | |
87f0e418 LP |
740 | if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0) |
741 | return -errno; | |
742 | } | |
743 | ||
744 | zero(its); | |
745 | ||
746 | if (delay <= 0) { | |
747 | /* Set absolute time in the past, but not 0, since we | |
748 | * don't want to disarm the timer */ | |
749 | its.it_value.tv_sec = 0; | |
750 | its.it_value.tv_nsec = 1; | |
751 | ||
752 | flags = TFD_TIMER_ABSTIME; | |
753 | } else { | |
754 | timespec_store(&its.it_value, delay); | |
755 | flags = 0; | |
756 | } | |
757 | ||
758 | /* This will also flush the elapse counter */ | |
759 | if (timerfd_settime(fd, flags, &its, NULL) < 0) | |
760 | goto fail; | |
761 | ||
acbb0225 LP |
762 | if (w->type == WATCH_INVALID) { |
763 | struct epoll_event ev; | |
87f0e418 | 764 | |
acbb0225 LP |
765 | zero(ev); |
766 | ev.data.ptr = w; | |
f94ea366 | 767 | ev.events = EPOLLIN; |
acbb0225 LP |
768 | |
769 | if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) | |
770 | goto fail; | |
771 | } | |
772 | ||
773 | w->fd = fd; | |
774 | w->type = WATCH_TIMER; | |
775 | w->unit = u; | |
87f0e418 | 776 | |
87f0e418 LP |
777 | return 0; |
778 | ||
779 | fail: | |
780 | if (ours) | |
781 | assert_se(close_nointr(fd) == 0); | |
782 | ||
783 | return -errno; | |
784 | } | |
785 | ||
acbb0225 | 786 | void unit_unwatch_timer(Unit *u, Watch *w) { |
87f0e418 | 787 | assert(u); |
acbb0225 | 788 | assert(w); |
87f0e418 | 789 | |
acbb0225 | 790 | if (w->type == WATCH_INVALID) |
87f0e418 LP |
791 | return; |
792 | ||
acbb0225 LP |
793 | assert(w->type == WATCH_TIMER && w->unit == u); |
794 | ||
795 | assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0); | |
796 | assert_se(close_nointr(w->fd) == 0); | |
797 | ||
798 | w->fd = -1; | |
799 | w->type = WATCH_INVALID; | |
800 | w->unit = NULL; | |
87f0e418 LP |
801 | } |
802 | ||
803 | bool unit_job_is_applicable(Unit *u, JobType j) { | |
804 | assert(u); | |
805 | assert(j >= 0 && j < _JOB_TYPE_MAX); | |
806 | ||
807 | switch (j) { | |
808 | ||
809 | case JOB_VERIFY_ACTIVE: | |
810 | case JOB_START: | |
811 | return true; | |
812 | ||
813 | case JOB_STOP: | |
814 | case JOB_RESTART: | |
815 | case JOB_TRY_RESTART: | |
816 | return unit_can_start(u); | |
817 | ||
818 | case JOB_RELOAD: | |
819 | return unit_can_reload(u); | |
820 | ||
821 | case JOB_RELOAD_OR_START: | |
822 | return unit_can_reload(u) && unit_can_start(u); | |
823 | ||
824 | default: | |
825 | assert_not_reached("Invalid job type"); | |
826 | } | |
827 | } | |
828 | ||
829 | int unit_add_dependency(Unit *u, UnitDependency d, Unit *other) { | |
830 | ||
831 | static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = { | |
832 | [UNIT_REQUIRES] = UNIT_REQUIRED_BY, | |
833 | [UNIT_SOFT_REQUIRES] = UNIT_SOFT_REQUIRED_BY, | |
834 | [UNIT_WANTS] = UNIT_WANTED_BY, | |
835 | [UNIT_REQUISITE] = UNIT_REQUIRED_BY, | |
836 | [UNIT_SOFT_REQUISITE] = UNIT_SOFT_REQUIRED_BY, | |
837 | [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID, | |
838 | [UNIT_SOFT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID, | |
839 | [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID, | |
840 | [UNIT_CONFLICTS] = UNIT_CONFLICTS, | |
841 | [UNIT_BEFORE] = UNIT_AFTER, | |
842 | [UNIT_AFTER] = UNIT_BEFORE | |
843 | }; | |
844 | int r; | |
845 | ||
846 | assert(u); | |
847 | assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX); | |
848 | assert(inverse_table[d] != _UNIT_DEPENDENCY_INVALID); | |
849 | assert(other); | |
850 | ||
851 | /* We won't allow dependencies on ourselves. We will not | |
852 | * consider them an error however. */ | |
853 | if (u == other) | |
854 | return 0; | |
855 | ||
856 | if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0) | |
857 | return r; | |
858 | ||
859 | if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0) | |
860 | return r; | |
861 | ||
862 | if ((r = set_put(u->meta.dependencies[d], other)) < 0) | |
863 | return r; | |
864 | ||
865 | if ((r = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) { | |
866 | set_remove(u->meta.dependencies[d], other); | |
867 | return r; | |
868 | } | |
869 | ||
870 | return 0; | |
871 | } | |
0301abf4 | 872 | |
09b6b09f LP |
873 | int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name) { |
874 | Unit *other; | |
875 | int r; | |
876 | ||
877 | if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0) | |
878 | return r; | |
879 | ||
880 | if ((r = unit_add_dependency(u, d, other)) < 0) | |
881 | return r; | |
882 | ||
883 | return 0; | |
884 | } | |
885 | ||
0301abf4 LP |
886 | const char *unit_path(void) { |
887 | char *e; | |
888 | ||
889 | if ((e = getenv("UNIT_PATH"))) | |
890 | if (path_is_absolute(e)) | |
891 | return e; | |
892 | ||
893 | return UNIT_PATH; | |
894 | } | |
895 | ||
896 | int set_unit_path(const char *p) { | |
897 | char *cwd, *c; | |
898 | int r; | |
899 | ||
900 | /* This is mostly for debug purposes */ | |
901 | ||
902 | if (path_is_absolute(p)) { | |
903 | if (!(c = strdup(p))) | |
904 | return -ENOMEM; | |
905 | } else { | |
906 | if (!(cwd = get_current_dir_name())) | |
907 | return -errno; | |
908 | ||
909 | r = asprintf(&c, "%s/%s", cwd, p); | |
910 | free(cwd); | |
911 | ||
912 | if (r < 0) | |
913 | return -ENOMEM; | |
914 | } | |
915 | ||
916 | if (setenv("UNIT_PATH", c, 0) < 0) { | |
917 | r = -errno; | |
918 | free(c); | |
919 | return r; | |
920 | } | |
921 | ||
922 | return 0; | |
923 | } | |
88066b3a | 924 | |
b08d03ff | 925 | char *unit_name_escape_path(const char *prefix, const char *path, const char *suffix) { |
88066b3a LP |
926 | char *r, *t; |
927 | const char *f; | |
b08d03ff | 928 | size_t a, b, c; |
88066b3a LP |
929 | |
930 | assert(path); | |
88066b3a | 931 | |
b08d03ff LP |
932 | /* Takes a path and a suffix and prefix and makes a nice |
933 | * string suitable as unit name of it, escaping all weird | |
934 | * chars on the way. | |
88066b3a | 935 | * |
b08d03ff LP |
936 | * / becomes ., and all chars not alloweed in a unit name get |
937 | * escaped as \xFF, including \ and ., of course. This | |
938 | * escaping is hence reversible. | |
88066b3a LP |
939 | */ |
940 | ||
b08d03ff LP |
941 | if (!prefix) |
942 | prefix = ""; | |
943 | ||
944 | if (!suffix) | |
945 | suffix = ""; | |
88066b3a | 946 | |
b08d03ff LP |
947 | a = strlen(prefix); |
948 | b = strlen(path); | |
949 | c = strlen(suffix); | |
950 | ||
951 | if (!(r = new(char, a+b*4+c+1))) | |
88066b3a LP |
952 | return NULL; |
953 | ||
b08d03ff LP |
954 | memcpy(r, prefix, a); |
955 | ||
956 | for (f = path, t = r+a; *f; f++) { | |
88066b3a | 957 | if (*f == '/') |
b08d03ff LP |
958 | *(t++) = '.'; |
959 | else if (*f == '.' || *f == '\\' || !strchr(VALID_CHARS, *f)) { | |
88066b3a LP |
960 | *(t++) = '\\'; |
961 | *(t++) = 'x'; | |
962 | *(t++) = hexchar(*f > 4); | |
963 | *(t++) = hexchar(*f); | |
964 | } else | |
965 | *(t++) = *f; | |
966 | } | |
967 | ||
b08d03ff | 968 | memcpy(t, suffix, c+1); |
88066b3a LP |
969 | |
970 | return r; | |
971 | } |