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