1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/epoll.h>
26 #include <sys/timerfd.h>
35 #include "load-fragment.h"
36 #include "load-dropin.h"
39 const UnitVTable
* const unit_vtable
[_UNIT_TYPE_MAX
] = {
40 [UNIT_SERVICE
] = &service_vtable
,
41 [UNIT_TIMER
] = &timer_vtable
,
42 [UNIT_SOCKET
] = &socket_vtable
,
43 [UNIT_TARGET
] = &target_vtable
,
44 [UNIT_DEVICE
] = &device_vtable
,
45 [UNIT_MOUNT
] = &mount_vtable
,
46 [UNIT_AUTOMOUNT
] = &automount_vtable
,
47 [UNIT_SNAPSHOT
] = &snapshot_vtable
50 UnitType
unit_name_to_type(const char *n
) {
55 for (t
= 0; t
< _UNIT_TYPE_MAX
; t
++)
56 if (endswith(n
, unit_vtable
[t
]->suffix
))
59 return _UNIT_TYPE_INVALID
;
64 "abcdefghijklmnopqrstuvwxyz" \
65 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
68 bool unit_name_is_valid(const char *n
) {
74 if (strlen(n
) >= UNIT_NAME_MAX
)
77 t
= unit_name_to_type(n
);
78 if (t
< 0 || t
>= _UNIT_TYPE_MAX
)
81 if (!(e
= strrchr(n
, '.')))
87 for (i
= n
; i
< e
; i
++)
88 if (!strchr(VALID_CHARS
, *i
))
94 char *unit_name_change_suffix(const char *n
, const char *suffix
) {
99 assert(unit_name_is_valid(n
));
102 assert_se(e
= strrchr(n
, '.'));
106 if (!(r
= new(char, a
+ b
+ 1)))
110 memcpy(r
+a
, suffix
, b
+1);
115 Unit
*unit_new(Manager
*m
) {
120 if (!(u
= new0(Unit
, 1)))
123 if (!(u
->meta
.names
= set_new(string_hash_func
, string_compare_func
))) {
129 u
->meta
.type
= _UNIT_TYPE_INVALID
;
134 int unit_add_name(Unit
*u
, const char *text
) {
142 if (!unit_name_is_valid(text
))
145 if ((t
= unit_name_to_type(text
)) == _UNIT_TYPE_INVALID
)
148 if (u
->meta
.type
!= _UNIT_TYPE_INVALID
&& t
!= u
->meta
.type
)
151 if (!(s
= strdup(text
)))
154 if ((r
= set_put(u
->meta
.names
, s
)) < 0) {
163 if ((r
= hashmap_put(u
->meta
.manager
->units
, s
, u
)) < 0) {
164 set_remove(u
->meta
.names
, s
);
169 if (u
->meta
.type
== _UNIT_TYPE_INVALID
)
170 LIST_PREPEND(Meta
, units_per_type
, u
->meta
.manager
->units_per_type
[t
], &u
->meta
);
180 int unit_choose_id(Unit
*u
, const char *name
) {
186 /* Selects one of the names of this unit as the id */
188 if (!(s
= set_get(u
->meta
.names
, (char*) name
)))
195 int unit_set_description(Unit
*u
, const char *description
) {
200 if (!(s
= strdup(description
)))
203 free(u
->meta
.description
);
204 u
->meta
.description
= s
;
208 void unit_add_to_load_queue(Unit
*u
) {
211 if (u
->meta
.load_state
!= UNIT_STUB
|| u
->meta
.in_load_queue
)
214 LIST_PREPEND(Meta
, load_queue
, u
->meta
.manager
->load_queue
, &u
->meta
);
215 u
->meta
.in_load_queue
= true;
218 static void bidi_set_free(Unit
*u
, Set
*s
) {
224 /* Frees the set and makes sure we are dropped from the
225 * inverse pointers */
227 SET_FOREACH(other
, s
, i
) {
230 for (d
= 0; d
< _UNIT_DEPENDENCY_MAX
; d
++)
231 set_remove(other
->meta
.dependencies
[d
], u
);
237 void unit_free(Unit
*u
) {
244 /* Detach from next 'bigger' objects */
246 SET_FOREACH(t
, u
->meta
.names
, i
)
247 hashmap_remove_value(u
->meta
.manager
->units
, t
, u
);
249 if (u
->meta
.type
!= _UNIT_TYPE_INVALID
)
250 LIST_REMOVE(Meta
, units_per_type
, u
->meta
.manager
->units_per_type
[u
->meta
.type
], &u
->meta
);
252 if (u
->meta
.in_load_queue
)
253 LIST_REMOVE(Meta
, load_queue
, u
->meta
.manager
->load_queue
, &u
->meta
);
255 if (u
->meta
.load_state
== UNIT_LOADED
)
256 if (UNIT_VTABLE(u
)->done
)
257 UNIT_VTABLE(u
)->done(u
);
259 /* Free data and next 'smaller' objects */
261 job_free(u
->meta
.job
);
263 for (d
= 0; d
< _UNIT_DEPENDENCY_MAX
; d
++)
264 bidi_set_free(u
, u
->meta
.dependencies
[d
]);
266 free(u
->meta
.description
);
267 free(u
->meta
.load_path
);
269 while ((t
= set_steal_first(u
->meta
.names
)))
271 set_free(u
->meta
.names
);
276 UnitActiveState
unit_active_state(Unit
*u
) {
279 if (u
->meta
.load_state
!= UNIT_LOADED
)
280 return UNIT_INACTIVE
;
282 return UNIT_VTABLE(u
)->active_state(u
);
285 static int ensure_merge(Set
**s
, Set
*other
) {
291 return set_merge(*s
, other
);
293 if (!(*s
= set_copy(other
)))
299 /* FIXME: Does not rollback on failure! Needs to fix special unit
300 * pointers. Needs to merge names and dependencies properly.*/
301 int unit_merge(Unit
*u
, Unit
*other
) {
307 assert(u
->meta
.manager
== other
->meta
.manager
);
309 /* This merges 'other' into 'unit'. FIXME: This does not
310 * rollback on failure. */
312 if (u
->meta
.type
!= u
->meta
.type
)
315 if (u
->meta
.load_state
!= UNIT_STUB
)
319 if ((r
= ensure_merge(&u
->meta
.names
, other
->meta
.names
)) < 0)
322 /* Merge dependencies */
323 for (d
= 0; d
< _UNIT_DEPENDENCY_MAX
; d
++)
324 /* fixme, the inverse mapping is missing */
325 if ((r
= ensure_merge(&u
->meta
.dependencies
[d
], other
->meta
.dependencies
[d
])) < 0)
331 const char* unit_id(Unit
*u
) {
337 return set_first(u
->meta
.names
);
340 const char *unit_description(Unit
*u
) {
343 if (u
->meta
.description
)
344 return u
->meta
.description
;
349 void unit_dump(Unit
*u
, FILE *f
, const char *prefix
) {
360 prefix2
= strappend(prefix
, "\t");
366 "%s\tDescription: %s\n"
367 "%s\tUnit Load State: %s\n"
368 "%s\tUnit Active State: %s\n"
369 "%s\tRecursive Stop: %s\n"
370 "%s\tStop When Unneeded: %s\n",
372 prefix
, unit_description(u
),
373 prefix
, unit_load_state_to_string(u
->meta
.load_state
),
374 prefix
, unit_active_state_to_string(unit_active_state(u
)),
375 prefix
, yes_no(u
->meta
.recursive_stop
),
376 prefix
, yes_no(u
->meta
.stop_when_unneeded
));
378 if (u
->meta
.load_path
)
379 fprintf(f
, "%s\tLoad Path: %s\n", prefix
, u
->meta
.load_path
);
381 SET_FOREACH(t
, u
->meta
.names
, i
)
382 fprintf(f
, "%s\tName: %s\n", prefix
, t
);
384 for (d
= 0; d
< _UNIT_DEPENDENCY_MAX
; d
++) {
387 if (set_isempty(u
->meta
.dependencies
[d
]))
390 SET_FOREACH(other
, u
->meta
.dependencies
[d
], i
)
391 fprintf(f
, "%s\t%s: %s\n", prefix
, unit_dependency_to_string(d
), unit_id(other
));
394 if (UNIT_VTABLE(u
)->dump
)
395 UNIT_VTABLE(u
)->dump(u
, f
, prefix2
);
398 job_dump(u
->meta
.job
, f
, prefix2
);
403 /* Common implementation for multiple backends */
404 int unit_load_fragment_and_dropin(Unit
*u
) {
409 /* Load a .socket file */
410 if ((r
= unit_load_fragment(u
)) < 0)
415 /* Load drop-in directory data */
416 if ((r
= unit_load_dropin(u
)) < 0)
422 int unit_load(Unit
*u
) {
427 if (u
->meta
.in_load_queue
) {
428 LIST_REMOVE(Meta
, load_queue
, u
->meta
.manager
->load_queue
, &u
->meta
);
429 u
->meta
.in_load_queue
= false;
432 if (u
->meta
.load_state
!= UNIT_STUB
)
435 if (UNIT_VTABLE(u
)->init
)
436 if ((r
= UNIT_VTABLE(u
)->init(u
)) < 0)
439 u
->meta
.load_state
= UNIT_LOADED
;
443 u
->meta
.load_state
= UNIT_FAILED
;
448 * -EBADR: This unit type does not support starting.
449 * -EALREADY: Unit is already started.
450 * -EAGAIN: An operation is already in progress. Retry later.
452 int unit_start(Unit
*u
) {
453 UnitActiveState state
;
457 if (!UNIT_VTABLE(u
)->start
)
460 state
= unit_active_state(u
);
461 if (UNIT_IS_ACTIVE_OR_RELOADING(state
))
464 /* We don't suppress calls to ->start() here when we are
465 * already starting, to allow this request to be used as a
466 * "hurry up" call, for example when the unit is in some "auto
467 * restart" state where it waits for a holdoff timer to elapse
468 * before it will start again. */
470 return UNIT_VTABLE(u
)->start(u
);
473 bool unit_can_start(Unit
*u
) {
476 return !!UNIT_VTABLE(u
)->start
;
480 * -EBADR: This unit type does not support stopping.
481 * -EALREADY: Unit is already stopped.
482 * -EAGAIN: An operation is already in progress. Retry later.
484 int unit_stop(Unit
*u
) {
485 UnitActiveState state
;
489 if (!UNIT_VTABLE(u
)->stop
)
492 state
= unit_active_state(u
);
493 if (state
== UNIT_INACTIVE
)
496 if (state
== UNIT_DEACTIVATING
)
499 return UNIT_VTABLE(u
)->stop(u
);
503 * -EBADR: This unit type does not support reloading.
504 * -ENOEXEC: Unit is not started.
505 * -EAGAIN: An operation is already in progress. Retry later.
507 int unit_reload(Unit
*u
) {
508 UnitActiveState state
;
512 if (!unit_can_reload(u
))
515 state
= unit_active_state(u
);
516 if (unit_active_state(u
) == UNIT_ACTIVE_RELOADING
)
519 if (unit_active_state(u
) != UNIT_ACTIVE
)
522 return UNIT_VTABLE(u
)->reload(u
);
525 bool unit_can_reload(Unit
*u
) {
528 if (!UNIT_VTABLE(u
)->reload
)
531 if (!UNIT_VTABLE(u
)->can_reload
)
534 return UNIT_VTABLE(u
)->can_reload(u
);
537 static void unit_check_uneeded(Unit
*u
) {
543 /* If this service shall be shut down when unneeded then do
546 if (!u
->meta
.stop_when_unneeded
)
549 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u
)))
552 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRED_BY
], i
)
553 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
556 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_SOFT_REQUIRED_BY
], i
)
557 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
560 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_WANTED_BY
], i
)
561 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
564 log_debug("Service %s is not needed anymore. Stopping.", unit_id(u
));
566 /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
567 manager_add_job(u
->meta
.manager
, JOB_STOP
, u
, JOB_FAIL
, true, NULL
);
570 static void retroactively_start_dependencies(Unit
*u
) {
575 assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u
)));
577 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRES
], i
)
578 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other
)))
579 manager_add_job(u
->meta
.manager
, JOB_START
, other
, JOB_REPLACE
, true, NULL
);
581 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_SOFT_REQUIRES
], i
)
582 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other
)))
583 manager_add_job(u
->meta
.manager
, JOB_START
, other
, JOB_FAIL
, false, NULL
);
585 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUISITE
], i
)
586 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other
)))
587 manager_add_job(u
->meta
.manager
, JOB_START
, other
, JOB_REPLACE
, true, NULL
);
589 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_WANTS
], i
)
590 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other
)))
591 manager_add_job(u
->meta
.manager
, JOB_START
, other
, JOB_FAIL
, false, NULL
);
593 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_CONFLICTS
], i
)
594 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other
)))
595 manager_add_job(u
->meta
.manager
, JOB_STOP
, other
, JOB_REPLACE
, true, NULL
);
598 static void retroactively_stop_dependencies(Unit
*u
) {
603 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u
)));
605 if (u
->meta
.recursive_stop
) {
606 /* Pull down units need us recursively if enabled */
607 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRED_BY
], i
)
608 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
609 manager_add_job(u
->meta
.manager
, JOB_STOP
, other
, JOB_REPLACE
, true, NULL
);
612 /* Garbage collect services that might not be needed anymore, if enabled */
613 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRES
], i
)
614 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
615 unit_check_uneeded(other
);
616 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_SOFT_REQUIRES
], i
)
617 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
618 unit_check_uneeded(other
);
619 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_WANTS
], i
)
620 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
621 unit_check_uneeded(other
);
622 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUISITE
], i
)
623 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
624 unit_check_uneeded(other
);
625 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_SOFT_REQUISITE
], i
)
626 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
627 unit_check_uneeded(other
);
630 void unit_notify(Unit
*u
, UnitActiveState os
, UnitActiveState ns
) {
632 assert(os
< _UNIT_ACTIVE_STATE_MAX
);
633 assert(ns
< _UNIT_ACTIVE_STATE_MAX
);
634 assert(!(os
== UNIT_ACTIVE
&& ns
== UNIT_ACTIVATING
));
635 assert(!(os
== UNIT_INACTIVE
&& ns
== UNIT_DEACTIVATING
));
640 if (!UNIT_IS_ACTIVE_OR_RELOADING(os
) && UNIT_IS_ACTIVE_OR_RELOADING(ns
))
641 u
->meta
.active_enter_timestamp
= now(CLOCK_REALTIME
);
642 else if (UNIT_IS_ACTIVE_OR_RELOADING(os
) && !UNIT_IS_ACTIVE_OR_RELOADING(ns
))
643 u
->meta
.active_exit_timestamp
= now(CLOCK_REALTIME
);
647 if (u
->meta
.job
->state
== JOB_WAITING
)
649 /* So we reached a different state for this
650 * job. Let's see if we can run it now if it
651 * failed previously due to EAGAIN. */
652 job_schedule_run(u
->meta
.job
);
655 assert(u
->meta
.job
->state
== JOB_RUNNING
);
657 /* Let's check whether this state change
658 * constitutes a finished job, or maybe
659 * cotradicts a running job and hence needs to
660 * invalidate jobs. */
662 switch (u
->meta
.job
->type
) {
665 case JOB_VERIFY_ACTIVE
:
667 if (UNIT_IS_ACTIVE_OR_RELOADING(ns
)) {
668 job_finish_and_invalidate(u
->meta
.job
, true);
670 } else if (ns
== UNIT_ACTIVATING
)
673 job_finish_and_invalidate(u
->meta
.job
, false);
678 case JOB_RELOAD_OR_START
:
680 if (ns
== UNIT_ACTIVE
) {
681 job_finish_and_invalidate(u
->meta
.job
, true);
683 } else if (ns
== UNIT_ACTIVATING
|| ns
== UNIT_ACTIVE_RELOADING
)
686 job_finish_and_invalidate(u
->meta
.job
, false);
692 case JOB_TRY_RESTART
:
694 if (ns
== UNIT_INACTIVE
) {
695 job_finish_and_invalidate(u
->meta
.job
, true);
697 } else if (ns
== UNIT_DEACTIVATING
)
700 job_finish_and_invalidate(u
->meta
.job
, false);
705 assert_not_reached("Job type unknown");
710 /* If this state change happened without being requested by a
711 * job, then let's retroactively start or stop dependencies */
713 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os
) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns
))
714 retroactively_start_dependencies(u
);
715 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os
) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns
))
716 retroactively_stop_dependencies(u
);
718 /* Maybe we finished startup and are now ready for being
719 * stopped because unneeded? */
720 unit_check_uneeded(u
);
723 int unit_watch_fd(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
724 struct epoll_event ev
;
729 assert(w
->type
== WATCH_INVALID
|| (w
->type
== WATCH_FD
&& w
->fd
== fd
&& w
->data
.unit
== u
));
735 if (epoll_ctl(u
->meta
.manager
->epoll_fd
,
736 w
->type
== WATCH_INVALID
? EPOLL_CTL_ADD
: EPOLL_CTL_MOD
,
748 void unit_unwatch_fd(Unit
*u
, Watch
*w
) {
752 if (w
->type
== WATCH_INVALID
)
755 assert(w
->type
== WATCH_FD
);
756 assert(w
->data
.unit
== u
);
757 assert_se(epoll_ctl(u
->meta
.manager
->epoll_fd
, EPOLL_CTL_DEL
, w
->fd
, NULL
) >= 0);
760 w
->type
= WATCH_INVALID
;
764 int unit_watch_pid(Unit
*u
, pid_t pid
) {
768 return hashmap_put(u
->meta
.manager
->watch_pids
, UINT32_TO_PTR(pid
), u
);
771 void unit_unwatch_pid(Unit
*u
, pid_t pid
) {
775 hashmap_remove(u
->meta
.manager
->watch_pids
, UINT32_TO_PTR(pid
));
778 int unit_watch_timer(Unit
*u
, usec_t delay
, Watch
*w
) {
779 struct itimerspec its
;
785 assert(w
->type
== WATCH_INVALID
|| (w
->type
== WATCH_TIMER
&& w
->data
.unit
== u
));
787 /* This will try to reuse the old timer if there is one */
789 if (w
->type
== WATCH_TIMER
) {
794 if ((fd
= timerfd_create(CLOCK_MONOTONIC
, TFD_NONBLOCK
|TFD_CLOEXEC
)) < 0)
801 /* Set absolute time in the past, but not 0, since we
802 * don't want to disarm the timer */
803 its
.it_value
.tv_sec
= 0;
804 its
.it_value
.tv_nsec
= 1;
806 flags
= TFD_TIMER_ABSTIME
;
808 timespec_store(&its
.it_value
, delay
);
812 /* This will also flush the elapse counter */
813 if (timerfd_settime(fd
, flags
, &its
, NULL
) < 0)
816 if (w
->type
== WATCH_INVALID
) {
817 struct epoll_event ev
;
823 if (epoll_ctl(u
->meta
.manager
->epoll_fd
, EPOLL_CTL_ADD
, fd
, &ev
) < 0)
828 w
->type
= WATCH_TIMER
;
835 close_nointr_nofail(fd
);
840 void unit_unwatch_timer(Unit
*u
, Watch
*w
) {
844 if (w
->type
== WATCH_INVALID
)
847 assert(w
->type
== WATCH_TIMER
&& w
->data
.unit
== u
);
849 assert_se(epoll_ctl(u
->meta
.manager
->epoll_fd
, EPOLL_CTL_DEL
, w
->fd
, NULL
) >= 0);
850 assert_se(close_nointr(w
->fd
) == 0);
853 w
->type
= WATCH_INVALID
;
857 bool unit_job_is_applicable(Unit
*u
, JobType j
) {
859 assert(j
>= 0 && j
< _JOB_TYPE_MAX
);
863 case JOB_VERIFY_ACTIVE
:
869 case JOB_TRY_RESTART
:
870 return unit_can_start(u
);
873 return unit_can_reload(u
);
875 case JOB_RELOAD_OR_START
:
876 return unit_can_reload(u
) && unit_can_start(u
);
879 assert_not_reached("Invalid job type");
883 int unit_add_dependency(Unit
*u
, UnitDependency d
, Unit
*other
) {
885 static const UnitDependency inverse_table
[_UNIT_DEPENDENCY_MAX
] = {
886 [UNIT_REQUIRES
] = UNIT_REQUIRED_BY
,
887 [UNIT_SOFT_REQUIRES
] = UNIT_SOFT_REQUIRED_BY
,
888 [UNIT_WANTS
] = UNIT_WANTED_BY
,
889 [UNIT_REQUISITE
] = UNIT_REQUIRED_BY
,
890 [UNIT_SOFT_REQUISITE
] = UNIT_SOFT_REQUIRED_BY
,
891 [UNIT_REQUIRED_BY
] = _UNIT_DEPENDENCY_INVALID
,
892 [UNIT_SOFT_REQUIRED_BY
] = _UNIT_DEPENDENCY_INVALID
,
893 [UNIT_WANTED_BY
] = _UNIT_DEPENDENCY_INVALID
,
894 [UNIT_CONFLICTS
] = UNIT_CONFLICTS
,
895 [UNIT_BEFORE
] = UNIT_AFTER
,
896 [UNIT_AFTER
] = UNIT_BEFORE
901 assert(d
>= 0 && d
< _UNIT_DEPENDENCY_MAX
);
902 assert(inverse_table
[d
] != _UNIT_DEPENDENCY_INVALID
);
905 /* We won't allow dependencies on ourselves. We will not
906 * consider them an error however. */
910 if ((r
= set_ensure_allocated(&u
->meta
.dependencies
[d
], trivial_hash_func
, trivial_compare_func
)) < 0)
913 if ((r
= set_ensure_allocated(&other
->meta
.dependencies
[inverse_table
[d
]], trivial_hash_func
, trivial_compare_func
)) < 0)
916 if ((r
= set_put(u
->meta
.dependencies
[d
], other
)) < 0)
919 if ((r
= set_put(other
->meta
.dependencies
[inverse_table
[d
]], u
)) < 0) {
920 set_remove(u
->meta
.dependencies
[d
], other
);
927 int unit_add_dependency_by_name(Unit
*u
, UnitDependency d
, const char *name
) {
931 if ((r
= manager_load_unit(u
->meta
.manager
, name
, &other
)) < 0)
934 if ((r
= unit_add_dependency(u
, d
, other
)) < 0)
940 const char *unit_path(void) {
943 if ((e
= getenv("UNIT_PATH")))
944 if (path_is_absolute(e
))
950 int set_unit_path(const char *p
) {
954 /* This is mostly for debug purposes */
956 if (path_is_absolute(p
)) {
957 if (!(c
= strdup(p
)))
960 if (!(cwd
= get_current_dir_name()))
963 r
= asprintf(&c
, "%s/%s", cwd
, p
);
970 if (setenv("UNIT_PATH", c
, 0) < 0) {
979 char *unit_name_escape_path(const char *prefix
, const char *path
, const char *suffix
) {
986 /* Takes a path and a suffix and prefix and makes a nice
987 * string suitable as unit name of it, escaping all weird
990 * / becomes ., and all chars not alloweed in a unit name get
991 * escaped as \xFF, including \ and ., of course. This
992 * escaping is hence reversible.
1005 if (!(r
= new(char, a
+b
*4+c
+1)))
1008 memcpy(r
, prefix
, a
);
1010 for (f
= path
, t
= r
+a
; *f
; f
++) {
1013 else if (*f
== '.' || *f
== '\\' || !strchr(VALID_CHARS
, *f
)) {
1016 *(t
++) = hexchar(*f
> 4);
1017 *(t
++) = hexchar(*f
);
1022 memcpy(t
, suffix
, c
+1);
1027 char *unit_dbus_path(Unit
*u
) {
1032 if (!(e
= bus_path_escape(unit_id(u
))))
1035 if (asprintf(&p
, "/org/freedesktop/systemd1/unit/%s", e
) < 0) {
1044 static const char* const unit_type_table
[_UNIT_TYPE_MAX
] = {
1045 [UNIT_SERVICE
] = "service",
1046 [UNIT_TIMER
] = "timer",
1047 [UNIT_SOCKET
] = "socket",
1048 [UNIT_TARGET
] = "target",
1049 [UNIT_DEVICE
] = "device",
1050 [UNIT_MOUNT
] = "mount",
1051 [UNIT_AUTOMOUNT
] = "automount",
1052 [UNIT_SNAPSHOT
] = "snapshot"
1055 DEFINE_STRING_TABLE_LOOKUP(unit_type
, UnitType
);
1057 static const char* const unit_load_state_table
[_UNIT_LOAD_STATE_MAX
] = {
1058 [UNIT_STUB
] = "stub",
1059 [UNIT_LOADED
] = "loaded",
1060 [UNIT_FAILED
] = "failed"
1063 DEFINE_STRING_TABLE_LOOKUP(unit_load_state
, UnitLoadState
);
1065 static const char* const unit_active_state_table
[_UNIT_ACTIVE_STATE_MAX
] = {
1066 [UNIT_ACTIVE
] = "active",
1067 [UNIT_INACTIVE
] = "inactive",
1068 [UNIT_ACTIVATING
] = "activating",
1069 [UNIT_DEACTIVATING
] = "deactivating"
1072 DEFINE_STRING_TABLE_LOOKUP(unit_active_state
, UnitActiveState
);
1074 static const char* const unit_dependency_table
[_UNIT_DEPENDENCY_MAX
] = {
1075 [UNIT_REQUIRES
] = "Requires",
1076 [UNIT_SOFT_REQUIRES
] = "SoftRequires",
1077 [UNIT_WANTS
] = "Wants",
1078 [UNIT_REQUISITE
] = "Requisite",
1079 [UNIT_SOFT_REQUISITE
] = "SoftRequisite",
1080 [UNIT_REQUIRED_BY
] = "RequiredBy",
1081 [UNIT_SOFT_REQUIRED_BY
] = "SoftRequiredBy",
1082 [UNIT_WANTED_BY
] = "WantedBy",
1083 [UNIT_CONFLICTS
] = "Conflicts",
1084 [UNIT_BEFORE
] = "Before",
1085 [UNIT_AFTER
] = "After",
1088 DEFINE_STRING_TABLE_LOOKUP(unit_dependency
, UnitDependency
);