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