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