]> git.ipfire.org Git - people/ms/systemd.git/blob - unit.c
implement drop-in directories
[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 for (i = n; i < e; i++)
66 if (!strchr(VALID_CHARS, *i))
67 return false;
68
69 return true;
70 }
71
72 char *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
93 Unit *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
112 int 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
155 void 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
165 static 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
184 void 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);
211 free(u->meta.load_path);
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
220 UnitActiveState 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
229 static 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! */
244 int 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
274 const 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
283 const 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
292 void 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
344 if (u->meta.load_path)
345 fprintf(f, "%s\tLoad Path: %s\n", prefix, u->meta.load_path);
346
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 */
370 int 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
386 int 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
406 fail:
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 */
416 int 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
437 bool 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 */
448 int 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 */
471 int 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
489 bool 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
501 static 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
529 static 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
541 void 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
630 int unit_watch_fd(Unit *u, int fd, uint32_t events) {
631 struct epoll_event ev;
632
633 assert(u);
634 assert(fd >= 0);
635
636 zero(ev);
637 ev.data.fd = fd;
638 ev.data.ptr = u;
639 ev.data.u32 = MANAGER_FD;
640 ev.events = events;
641
642 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) >= 0)
643 return 0;
644
645 if (errno == EEXIST)
646 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_MOD, fd, &ev) >= 0)
647 return 0;
648
649 return -errno;
650 }
651
652 void unit_unwatch_fd(Unit *u, int fd) {
653 assert(u);
654 assert(fd >= 0);
655
656 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, fd, NULL) >= 0 || errno == ENOENT);
657 }
658
659 int unit_watch_pid(Unit *u, pid_t pid) {
660 assert(u);
661 assert(pid >= 1);
662
663 return hashmap_put(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u);
664 }
665
666 void unit_unwatch_pid(Unit *u, pid_t pid) {
667 assert(u);
668 assert(pid >= 1);
669
670 hashmap_remove(u->meta.manager->watch_pids, UINT32_TO_PTR(pid));
671 }
672
673 int unit_watch_timer(Unit *u, usec_t delay, int *id) {
674 struct epoll_event ev;
675 int fd;
676 struct itimerspec its;
677 int flags;
678 bool ours;
679
680 assert(u);
681 assert(id);
682
683 /* This will try to reuse the old timer if there is one */
684
685 if (*id >= 0) {
686 ours = false;
687 fd = *id;
688
689 } else {
690 ours = true;
691
692 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
693 return -errno;
694 }
695
696 zero(its);
697
698 if (delay <= 0) {
699 /* Set absolute time in the past, but not 0, since we
700 * don't want to disarm the timer */
701 its.it_value.tv_sec = 0;
702 its.it_value.tv_nsec = 1;
703
704 flags = TFD_TIMER_ABSTIME;
705 } else {
706 timespec_store(&its.it_value, delay);
707 flags = 0;
708 }
709
710 /* This will also flush the elapse counter */
711 if (timerfd_settime(fd, flags, &its, NULL) < 0)
712 goto fail;
713
714 zero(ev);
715 ev.data.fd = fd;
716 ev.data.ptr = u;
717 ev.data.u32 = MANAGER_TIMER;
718 ev.events = POLLIN;
719
720 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
721 goto fail;
722
723 *id = fd;
724 return 0;
725
726 fail:
727 if (ours)
728 assert_se(close_nointr(fd) == 0);
729
730 return -errno;
731 }
732
733 void unit_unwatch_timer(Unit *u, int *id) {
734 assert(u);
735 assert(id);
736
737 if (*id < 0)
738 return;
739
740 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, *id, NULL) >= 0);
741 assert_se(close_nointr(*id) == 0);
742 *id = -1;
743 }
744
745 bool unit_job_is_applicable(Unit *u, JobType j) {
746 assert(u);
747 assert(j >= 0 && j < _JOB_TYPE_MAX);
748
749 switch (j) {
750
751 case JOB_VERIFY_ACTIVE:
752 case JOB_START:
753 return true;
754
755 case JOB_STOP:
756 case JOB_RESTART:
757 case JOB_TRY_RESTART:
758 return unit_can_start(u);
759
760 case JOB_RELOAD:
761 return unit_can_reload(u);
762
763 case JOB_RELOAD_OR_START:
764 return unit_can_reload(u) && unit_can_start(u);
765
766 default:
767 assert_not_reached("Invalid job type");
768 }
769 }
770
771 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other) {
772
773 static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
774 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
775 [UNIT_SOFT_REQUIRES] = UNIT_SOFT_REQUIRED_BY,
776 [UNIT_WANTS] = UNIT_WANTED_BY,
777 [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
778 [UNIT_SOFT_REQUISITE] = UNIT_SOFT_REQUIRED_BY,
779 [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
780 [UNIT_SOFT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
781 [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
782 [UNIT_CONFLICTS] = UNIT_CONFLICTS,
783 [UNIT_BEFORE] = UNIT_AFTER,
784 [UNIT_AFTER] = UNIT_BEFORE
785 };
786 int r;
787
788 assert(u);
789 assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
790 assert(inverse_table[d] != _UNIT_DEPENDENCY_INVALID);
791 assert(other);
792
793 /* We won't allow dependencies on ourselves. We will not
794 * consider them an error however. */
795 if (u == other)
796 return 0;
797
798 if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
799 return r;
800
801 if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
802 return r;
803
804 if ((r = set_put(u->meta.dependencies[d], other)) < 0)
805 return r;
806
807 if ((r = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
808 set_remove(u->meta.dependencies[d], other);
809 return r;
810 }
811
812 return 0;
813 }
814
815 const char *unit_path(void) {
816 char *e;
817
818 if ((e = getenv("UNIT_PATH")))
819 if (path_is_absolute(e))
820 return e;
821
822 return UNIT_PATH;
823 }
824
825 int set_unit_path(const char *p) {
826 char *cwd, *c;
827 int r;
828
829 /* This is mostly for debug purposes */
830
831 if (path_is_absolute(p)) {
832 if (!(c = strdup(p)))
833 return -ENOMEM;
834 } else {
835 if (!(cwd = get_current_dir_name()))
836 return -errno;
837
838 r = asprintf(&c, "%s/%s", cwd, p);
839 free(cwd);
840
841 if (r < 0)
842 return -ENOMEM;
843 }
844
845 if (setenv("UNIT_PATH", c, 0) < 0) {
846 r = -errno;
847 free(c);
848 return r;
849 }
850
851 return 0;
852 }