1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
7 #include <sys/timerfd.h>
16 #include "load-fragment.h"
17 #include "load-dropin.h"
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
31 UnitType
unit_name_to_type(const char *n
) {
36 for (t
= 0; t
< _UNIT_TYPE_MAX
; t
++)
37 if (endswith(n
, unit_vtable
[t
]->suffix
))
40 return _UNIT_TYPE_INVALID
;
45 "abcdefghijklmnopqrstuvwxyz" \
46 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
49 bool unit_name_is_valid(const char *n
) {
55 if (strlen(n
) >= UNIT_NAME_MAX
)
58 t
= unit_name_to_type(n
);
59 if (t
< 0 || t
>= _UNIT_TYPE_MAX
)
62 if (!(e
= strrchr(n
, '.')))
68 for (i
= n
; i
< e
; i
++)
69 if (!strchr(VALID_CHARS
, *i
))
75 char *unit_name_change_suffix(const char *n
, const char *suffix
) {
80 assert(unit_name_is_valid(n
));
83 assert_se(e
= strrchr(n
, '.'));
87 if (!(r
= new(char, a
+ b
+ 1)))
91 memcpy(r
+a
, suffix
, b
+1);
96 Unit
*unit_new(Manager
*m
) {
101 if (!(u
= new0(Unit
, 1)))
104 if (!(u
->meta
.names
= set_new(string_hash_func
, string_compare_func
))) {
110 u
->meta
.type
= _UNIT_TYPE_INVALID
;
115 int unit_add_name(Unit
*u
, const char *text
) {
123 if (!unit_name_is_valid(text
))
126 if ((t
= unit_name_to_type(text
)) == _UNIT_TYPE_INVALID
)
129 if (u
->meta
.type
!= _UNIT_TYPE_INVALID
&& t
!= u
->meta
.type
)
132 if (!(s
= strdup(text
)))
135 if ((r
= set_put(u
->meta
.names
, s
)) < 0) {
144 if ((r
= hashmap_put(u
->meta
.manager
->units
, s
, u
)) < 0) {
145 set_remove(u
->meta
.names
, s
);
150 if (u
->meta
.type
== _UNIT_TYPE_INVALID
)
151 LIST_PREPEND(Meta
, units_per_type
, u
->meta
.manager
->units_per_type
[t
], &u
->meta
);
161 int unit_choose_id(Unit
*u
, const char *name
) {
167 /* Selects one of the names of this unit as the id */
169 if (!(s
= set_get(u
->meta
.names
, (char*) name
)))
176 int unit_set_description(Unit
*u
, const char *description
) {
181 if (!(s
= strdup(description
)))
184 free(u
->meta
.description
);
185 u
->meta
.description
= s
;
189 void unit_add_to_load_queue(Unit
*u
) {
192 if (u
->meta
.load_state
!= UNIT_STUB
|| u
->meta
.in_load_queue
)
195 LIST_PREPEND(Meta
, load_queue
, u
->meta
.manager
->load_queue
, &u
->meta
);
196 u
->meta
.in_load_queue
= true;
199 static void bidi_set_free(Unit
*u
, Set
*s
) {
205 /* Frees the set and makes sure we are dropped from the
206 * inverse pointers */
208 SET_FOREACH(other
, s
, i
) {
211 for (d
= 0; d
< _UNIT_DEPENDENCY_MAX
; d
++)
212 set_remove(other
->meta
.dependencies
[d
], u
);
218 void unit_free(Unit
*u
) {
225 /* Detach from next 'bigger' objects */
227 SET_FOREACH(t
, u
->meta
.names
, i
)
228 hashmap_remove_value(u
->meta
.manager
->units
, t
, u
);
230 if (u
->meta
.type
!= _UNIT_TYPE_INVALID
)
231 LIST_REMOVE(Meta
, units_per_type
, u
->meta
.manager
->units_per_type
[u
->meta
.type
], &u
->meta
);
233 if (u
->meta
.in_load_queue
)
234 LIST_REMOVE(Meta
, load_queue
, u
->meta
.manager
->load_queue
, &u
->meta
);
236 if (u
->meta
.load_state
== UNIT_LOADED
)
237 if (UNIT_VTABLE(u
)->done
)
238 UNIT_VTABLE(u
)->done(u
);
240 /* Free data and next 'smaller' objects */
242 job_free(u
->meta
.job
);
244 for (d
= 0; d
< _UNIT_DEPENDENCY_MAX
; d
++)
245 bidi_set_free(u
, u
->meta
.dependencies
[d
]);
247 free(u
->meta
.description
);
248 free(u
->meta
.load_path
);
250 while ((t
= set_steal_first(u
->meta
.names
)))
252 set_free(u
->meta
.names
);
257 UnitActiveState
unit_active_state(Unit
*u
) {
260 if (u
->meta
.load_state
!= UNIT_LOADED
)
261 return UNIT_INACTIVE
;
263 return UNIT_VTABLE(u
)->active_state(u
);
266 static int ensure_merge(Set
**s
, Set
*other
) {
272 return set_merge(*s
, other
);
274 if (!(*s
= set_copy(other
)))
280 /* FIXME: Does not rollback on failure! Needs to fix special unit
281 * pointers. Needs to merge names and dependencies properly.*/
282 int unit_merge(Unit
*u
, Unit
*other
) {
288 assert(u
->meta
.manager
== other
->meta
.manager
);
290 /* This merges 'other' into 'unit'. FIXME: This does not
291 * rollback on failure. */
293 if (u
->meta
.type
!= u
->meta
.type
)
296 if (u
->meta
.load_state
!= UNIT_STUB
)
300 if ((r
= ensure_merge(&u
->meta
.names
, other
->meta
.names
)) < 0)
303 /* Merge dependencies */
304 for (d
= 0; d
< _UNIT_DEPENDENCY_MAX
; d
++)
305 /* fixme, the inverse mapping is missing */
306 if ((r
= ensure_merge(&u
->meta
.dependencies
[d
], other
->meta
.dependencies
[d
])) < 0)
312 const char* unit_id(Unit
*u
) {
318 return set_first(u
->meta
.names
);
321 const char *unit_description(Unit
*u
) {
324 if (u
->meta
.description
)
325 return u
->meta
.description
;
330 void unit_dump(Unit
*u
, FILE *f
, const char *prefix
) {
341 prefix2
= strappend(prefix
, "\t");
347 "%s\tDescription: %s\n"
348 "%s\tUnit Load State: %s\n"
349 "%s\tUnit Active State: %s\n"
350 "%s\tRecursive Stop: %s\n"
351 "%s\tStop When Unneeded: %s\n",
353 prefix
, unit_description(u
),
354 prefix
, unit_load_state_to_string(u
->meta
.load_state
),
355 prefix
, unit_active_state_to_string(unit_active_state(u
)),
356 prefix
, yes_no(u
->meta
.recursive_stop
),
357 prefix
, yes_no(u
->meta
.stop_when_unneeded
));
359 if (u
->meta
.load_path
)
360 fprintf(f
, "%s\tLoad Path: %s\n", prefix
, u
->meta
.load_path
);
362 SET_FOREACH(t
, u
->meta
.names
, i
)
363 fprintf(f
, "%s\tName: %s\n", prefix
, t
);
365 for (d
= 0; d
< _UNIT_DEPENDENCY_MAX
; d
++) {
368 if (set_isempty(u
->meta
.dependencies
[d
]))
371 SET_FOREACH(other
, u
->meta
.dependencies
[d
], i
)
372 fprintf(f
, "%s\t%s: %s\n", prefix
, unit_dependency_to_string(d
), unit_id(other
));
375 if (UNIT_VTABLE(u
)->dump
)
376 UNIT_VTABLE(u
)->dump(u
, f
, prefix2
);
379 job_dump(u
->meta
.job
, f
, prefix2
);
384 /* Common implementation for multiple backends */
385 int unit_load_fragment_and_dropin(Unit
*u
) {
390 /* Load a .socket file */
391 if ((r
= unit_load_fragment(u
)) < 0)
396 /* Load drop-in directory data */
397 if ((r
= unit_load_dropin(u
)) < 0)
403 int unit_load(Unit
*u
) {
408 if (u
->meta
.in_load_queue
) {
409 LIST_REMOVE(Meta
, load_queue
, u
->meta
.manager
->load_queue
, &u
->meta
);
410 u
->meta
.in_load_queue
= false;
413 if (u
->meta
.load_state
!= UNIT_STUB
)
416 if (UNIT_VTABLE(u
)->init
)
417 if ((r
= UNIT_VTABLE(u
)->init(u
)) < 0)
420 u
->meta
.load_state
= UNIT_LOADED
;
424 u
->meta
.load_state
= UNIT_FAILED
;
429 * -EBADR: This unit type does not support starting.
430 * -EALREADY: Unit is already started.
431 * -EAGAIN: An operation is already in progress. Retry later.
433 int unit_start(Unit
*u
) {
434 UnitActiveState state
;
438 if (!UNIT_VTABLE(u
)->start
)
441 state
= unit_active_state(u
);
442 if (UNIT_IS_ACTIVE_OR_RELOADING(state
))
445 /* We don't suppress calls to ->start() here when we are
446 * already starting, to allow this request to be used as a
447 * "hurry up" call, for example when the unit is in some "auto
448 * restart" state where it waits for a holdoff timer to elapse
449 * before it will start again. */
451 return UNIT_VTABLE(u
)->start(u
);
454 bool unit_can_start(Unit
*u
) {
457 return !!UNIT_VTABLE(u
)->start
;
461 * -EBADR: This unit type does not support stopping.
462 * -EALREADY: Unit is already stopped.
463 * -EAGAIN: An operation is already in progress. Retry later.
465 int unit_stop(Unit
*u
) {
466 UnitActiveState state
;
470 if (!UNIT_VTABLE(u
)->stop
)
473 state
= unit_active_state(u
);
474 if (state
== UNIT_INACTIVE
)
477 if (state
== UNIT_DEACTIVATING
)
480 return UNIT_VTABLE(u
)->stop(u
);
484 * -EBADR: This unit type does not support reloading.
485 * -ENOEXEC: Unit is not started.
486 * -EAGAIN: An operation is already in progress. Retry later.
488 int unit_reload(Unit
*u
) {
489 UnitActiveState state
;
493 if (!unit_can_reload(u
))
496 state
= unit_active_state(u
);
497 if (unit_active_state(u
) == UNIT_ACTIVE_RELOADING
)
500 if (unit_active_state(u
) != UNIT_ACTIVE
)
503 return UNIT_VTABLE(u
)->reload(u
);
506 bool unit_can_reload(Unit
*u
) {
509 if (!UNIT_VTABLE(u
)->reload
)
512 if (!UNIT_VTABLE(u
)->can_reload
)
515 return UNIT_VTABLE(u
)->can_reload(u
);
518 static void unit_check_uneeded(Unit
*u
) {
524 /* If this service shall be shut down when unneeded then do
527 if (!u
->meta
.stop_when_unneeded
)
530 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u
)))
533 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRED_BY
], i
)
534 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
537 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_SOFT_REQUIRED_BY
], i
)
538 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
541 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_WANTED_BY
], i
)
542 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
545 log_debug("Service %s is not needed anymore. Stopping.", unit_id(u
));
547 /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
548 manager_add_job(u
->meta
.manager
, JOB_STOP
, u
, JOB_FAIL
, true, NULL
);
551 static void retroactively_start_dependencies(Unit
*u
) {
556 assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u
)));
558 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRES
], i
)
559 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other
)))
560 manager_add_job(u
->meta
.manager
, JOB_START
, other
, JOB_REPLACE
, true, NULL
);
562 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_SOFT_REQUIRES
], i
)
563 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other
)))
564 manager_add_job(u
->meta
.manager
, JOB_START
, other
, JOB_FAIL
, false, NULL
);
566 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUISITE
], i
)
567 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other
)))
568 manager_add_job(u
->meta
.manager
, JOB_START
, other
, JOB_REPLACE
, true, NULL
);
570 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_WANTS
], i
)
571 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other
)))
572 manager_add_job(u
->meta
.manager
, JOB_START
, other
, JOB_FAIL
, false, NULL
);
574 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_CONFLICTS
], i
)
575 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other
)))
576 manager_add_job(u
->meta
.manager
, JOB_STOP
, other
, JOB_REPLACE
, true, NULL
);
579 static void retroactively_stop_dependencies(Unit
*u
) {
584 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u
)));
586 if (u
->meta
.recursive_stop
) {
587 /* Pull down units need us recursively if enabled */
588 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRED_BY
], i
)
589 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
590 manager_add_job(u
->meta
.manager
, JOB_STOP
, other
, JOB_REPLACE
, true, NULL
);
593 /* Garbage collect services that might not be needed anymore, if enabled */
594 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRES
], i
)
595 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
596 unit_check_uneeded(other
);
597 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_SOFT_REQUIRES
], i
)
598 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
599 unit_check_uneeded(other
);
600 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_WANTS
], i
)
601 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
602 unit_check_uneeded(other
);
603 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUISITE
], i
)
604 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
605 unit_check_uneeded(other
);
606 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_SOFT_REQUISITE
], i
)
607 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
608 unit_check_uneeded(other
);
611 void unit_notify(Unit
*u
, UnitActiveState os
, UnitActiveState ns
) {
613 assert(os
< _UNIT_ACTIVE_STATE_MAX
);
614 assert(ns
< _UNIT_ACTIVE_STATE_MAX
);
615 assert(!(os
== UNIT_ACTIVE
&& ns
== UNIT_ACTIVATING
));
616 assert(!(os
== UNIT_INACTIVE
&& ns
== UNIT_DEACTIVATING
));
621 if (!UNIT_IS_ACTIVE_OR_RELOADING(os
) && UNIT_IS_ACTIVE_OR_RELOADING(ns
))
622 u
->meta
.active_enter_timestamp
= now(CLOCK_REALTIME
);
623 else if (UNIT_IS_ACTIVE_OR_RELOADING(os
) && !UNIT_IS_ACTIVE_OR_RELOADING(ns
))
624 u
->meta
.active_exit_timestamp
= now(CLOCK_REALTIME
);
628 if (u
->meta
.job
->state
== JOB_WAITING
)
630 /* So we reached a different state for this
631 * job. Let's see if we can run it now if it
632 * failed previously due to EAGAIN. */
633 job_schedule_run(u
->meta
.job
);
636 assert(u
->meta
.job
->state
== JOB_RUNNING
);
638 /* Let's check whether this state change
639 * constitutes a finished job, or maybe
640 * cotradicts a running job and hence needs to
641 * invalidate jobs. */
643 switch (u
->meta
.job
->type
) {
646 case JOB_VERIFY_ACTIVE
:
648 if (UNIT_IS_ACTIVE_OR_RELOADING(ns
)) {
649 job_finish_and_invalidate(u
->meta
.job
, true);
651 } else if (ns
== UNIT_ACTIVATING
)
654 job_finish_and_invalidate(u
->meta
.job
, false);
659 case JOB_RELOAD_OR_START
:
661 if (ns
== UNIT_ACTIVE
) {
662 job_finish_and_invalidate(u
->meta
.job
, true);
664 } else if (ns
== UNIT_ACTIVATING
|| ns
== UNIT_ACTIVE_RELOADING
)
667 job_finish_and_invalidate(u
->meta
.job
, false);
673 case JOB_TRY_RESTART
:
675 if (ns
== UNIT_INACTIVE
) {
676 job_finish_and_invalidate(u
->meta
.job
, true);
678 } else if (ns
== UNIT_DEACTIVATING
)
681 job_finish_and_invalidate(u
->meta
.job
, false);
686 assert_not_reached("Job type unknown");
691 /* If this state change happened without being requested by a
692 * job, then let's retroactively start or stop dependencies */
694 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os
) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns
))
695 retroactively_start_dependencies(u
);
696 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os
) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns
))
697 retroactively_stop_dependencies(u
);
699 /* Maybe we finished startup and are now ready for being
700 * stopped because unneeded? */
701 unit_check_uneeded(u
);
704 int unit_watch_fd(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
705 struct epoll_event ev
;
710 assert(w
->type
== WATCH_INVALID
|| (w
->type
== WATCH_FD
&& w
->fd
== fd
&& w
->unit
== u
));
716 if (epoll_ctl(u
->meta
.manager
->epoll_fd
,
717 w
->type
== WATCH_INVALID
? EPOLL_CTL_ADD
: EPOLL_CTL_MOD
,
729 void unit_unwatch_fd(Unit
*u
, Watch
*w
) {
733 if (w
->type
== WATCH_INVALID
)
736 assert(w
->type
== WATCH_FD
&& w
->unit
== u
);
737 assert_se(epoll_ctl(u
->meta
.manager
->epoll_fd
, EPOLL_CTL_DEL
, w
->fd
, NULL
) >= 0);
740 w
->type
= WATCH_INVALID
;
744 int unit_watch_pid(Unit
*u
, pid_t pid
) {
748 return hashmap_put(u
->meta
.manager
->watch_pids
, UINT32_TO_PTR(pid
), u
);
751 void unit_unwatch_pid(Unit
*u
, pid_t pid
) {
755 hashmap_remove(u
->meta
.manager
->watch_pids
, UINT32_TO_PTR(pid
));
758 int unit_watch_timer(Unit
*u
, usec_t delay
, Watch
*w
) {
759 struct itimerspec its
;
765 assert(w
->type
== WATCH_INVALID
|| (w
->type
== WATCH_TIMER
&& w
->unit
== u
));
767 /* This will try to reuse the old timer if there is one */
769 if (w
->type
== WATCH_TIMER
) {
774 if ((fd
= timerfd_create(CLOCK_MONOTONIC
, TFD_NONBLOCK
|TFD_CLOEXEC
)) < 0)
781 /* Set absolute time in the past, but not 0, since we
782 * don't want to disarm the timer */
783 its
.it_value
.tv_sec
= 0;
784 its
.it_value
.tv_nsec
= 1;
786 flags
= TFD_TIMER_ABSTIME
;
788 timespec_store(&its
.it_value
, delay
);
792 /* This will also flush the elapse counter */
793 if (timerfd_settime(fd
, flags
, &its
, NULL
) < 0)
796 if (w
->type
== WATCH_INVALID
) {
797 struct epoll_event ev
;
803 if (epoll_ctl(u
->meta
.manager
->epoll_fd
, EPOLL_CTL_ADD
, fd
, &ev
) < 0)
808 w
->type
= WATCH_TIMER
;
815 assert_se(close_nointr(fd
) == 0);
820 void unit_unwatch_timer(Unit
*u
, Watch
*w
) {
824 if (w
->type
== WATCH_INVALID
)
827 assert(w
->type
== WATCH_TIMER
&& w
->unit
== u
);
829 assert_se(epoll_ctl(u
->meta
.manager
->epoll_fd
, EPOLL_CTL_DEL
, w
->fd
, NULL
) >= 0);
830 assert_se(close_nointr(w
->fd
) == 0);
833 w
->type
= WATCH_INVALID
;
837 bool unit_job_is_applicable(Unit
*u
, JobType j
) {
839 assert(j
>= 0 && j
< _JOB_TYPE_MAX
);
843 case JOB_VERIFY_ACTIVE
:
849 case JOB_TRY_RESTART
:
850 return unit_can_start(u
);
853 return unit_can_reload(u
);
855 case JOB_RELOAD_OR_START
:
856 return unit_can_reload(u
) && unit_can_start(u
);
859 assert_not_reached("Invalid job type");
863 int unit_add_dependency(Unit
*u
, UnitDependency d
, Unit
*other
) {
865 static const UnitDependency inverse_table
[_UNIT_DEPENDENCY_MAX
] = {
866 [UNIT_REQUIRES
] = UNIT_REQUIRED_BY
,
867 [UNIT_SOFT_REQUIRES
] = UNIT_SOFT_REQUIRED_BY
,
868 [UNIT_WANTS
] = UNIT_WANTED_BY
,
869 [UNIT_REQUISITE
] = UNIT_REQUIRED_BY
,
870 [UNIT_SOFT_REQUISITE
] = UNIT_SOFT_REQUIRED_BY
,
871 [UNIT_REQUIRED_BY
] = _UNIT_DEPENDENCY_INVALID
,
872 [UNIT_SOFT_REQUIRED_BY
] = _UNIT_DEPENDENCY_INVALID
,
873 [UNIT_WANTED_BY
] = _UNIT_DEPENDENCY_INVALID
,
874 [UNIT_CONFLICTS
] = UNIT_CONFLICTS
,
875 [UNIT_BEFORE
] = UNIT_AFTER
,
876 [UNIT_AFTER
] = UNIT_BEFORE
881 assert(d
>= 0 && d
< _UNIT_DEPENDENCY_MAX
);
882 assert(inverse_table
[d
] != _UNIT_DEPENDENCY_INVALID
);
885 /* We won't allow dependencies on ourselves. We will not
886 * consider them an error however. */
890 if ((r
= set_ensure_allocated(&u
->meta
.dependencies
[d
], trivial_hash_func
, trivial_compare_func
)) < 0)
893 if ((r
= set_ensure_allocated(&other
->meta
.dependencies
[inverse_table
[d
]], trivial_hash_func
, trivial_compare_func
)) < 0)
896 if ((r
= set_put(u
->meta
.dependencies
[d
], other
)) < 0)
899 if ((r
= set_put(other
->meta
.dependencies
[inverse_table
[d
]], u
)) < 0) {
900 set_remove(u
->meta
.dependencies
[d
], other
);
907 int unit_add_dependency_by_name(Unit
*u
, UnitDependency d
, const char *name
) {
911 if ((r
= manager_load_unit(u
->meta
.manager
, name
, &other
)) < 0)
914 if ((r
= unit_add_dependency(u
, d
, other
)) < 0)
920 const char *unit_path(void) {
923 if ((e
= getenv("UNIT_PATH")))
924 if (path_is_absolute(e
))
930 int set_unit_path(const char *p
) {
934 /* This is mostly for debug purposes */
936 if (path_is_absolute(p
)) {
937 if (!(c
= strdup(p
)))
940 if (!(cwd
= get_current_dir_name()))
943 r
= asprintf(&c
, "%s/%s", cwd
, p
);
950 if (setenv("UNIT_PATH", c
, 0) < 0) {
959 char *unit_name_escape_path(const char *prefix
, const char *path
, const char *suffix
) {
966 /* Takes a path and a suffix and prefix and makes a nice
967 * string suitable as unit name of it, escaping all weird
970 * / becomes ., and all chars not alloweed in a unit name get
971 * escaped as \xFF, including \ and ., of course. This
972 * escaping is hence reversible.
985 if (!(r
= new(char, a
+b
*4+c
+1)))
988 memcpy(r
, prefix
, a
);
990 for (f
= path
, t
= r
+a
; *f
; f
++) {
993 else if (*f
== '.' || *f
== '\\' || !strchr(VALID_CHARS
, *f
)) {
996 *(t
++) = hexchar(*f
> 4);
997 *(t
++) = hexchar(*f
);
1002 memcpy(t
, suffix
, c
+1);
1007 static const char* const unit_type_table
[_UNIT_TYPE_MAX
] = {
1008 [UNIT_SERVICE
] = "service",
1009 [UNIT_TIMER
] = "timer",
1010 [UNIT_SOCKET
] = "socket",
1011 [UNIT_TARGET
] = "target",
1012 [UNIT_DEVICE
] = "device",
1013 [UNIT_MOUNT
] = "mount",
1014 [UNIT_AUTOMOUNT
] = "automount",
1015 [UNIT_SNAPSHOT
] = "snapshot"
1018 DEFINE_STRING_TABLE_LOOKUP(unit_type
, UnitType
);
1020 static const char* const unit_load_state_table
[_UNIT_LOAD_STATE_MAX
] = {
1021 [UNIT_STUB
] = "stub",
1022 [UNIT_LOADED
] = "loaded",
1023 [UNIT_FAILED
] = "failed"
1026 DEFINE_STRING_TABLE_LOOKUP(unit_load_state
, UnitLoadState
);
1028 static const char* const unit_active_state_table
[_UNIT_ACTIVE_STATE_MAX
] = {
1029 [UNIT_ACTIVE
] = "active",
1030 [UNIT_INACTIVE
] = "inactive",
1031 [UNIT_ACTIVATING
] = "activating",
1032 [UNIT_DEACTIVATING
] = "deactivating"
1035 DEFINE_STRING_TABLE_LOOKUP(unit_active_state
, UnitActiveState
);
1037 static const char* const unit_dependency_table
[_UNIT_DEPENDENCY_MAX
] = {
1038 [UNIT_REQUIRES
] = "Requires",
1039 [UNIT_SOFT_REQUIRES
] = "SoftRequires",
1040 [UNIT_WANTS
] = "Wants",
1041 [UNIT_REQUISITE
] = "Requisite",
1042 [UNIT_SOFT_REQUISITE
] = "SoftRequisite",
1043 [UNIT_REQUIRED_BY
] = "RequiredBy",
1044 [UNIT_SOFT_REQUIRED_BY
] = "SoftRequiredBy",
1045 [UNIT_WANTED_BY
] = "WantedBy",
1046 [UNIT_CONFLICTS
] = "Conflicts",
1047 [UNIT_BEFORE
] = "Before",
1048 [UNIT_AFTER
] = "After",
1051 DEFINE_STRING_TABLE_LOOKUP(unit_dependency
, UnitDependency
);