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