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