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