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