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