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"
38 #include "unit-name.h"
39 #include "specifier.h"
41 const UnitVTable
* const unit_vtable
[_UNIT_TYPE_MAX
] = {
42 [UNIT_SERVICE
] = &service_vtable
,
43 [UNIT_TIMER
] = &timer_vtable
,
44 [UNIT_SOCKET
] = &socket_vtable
,
45 [UNIT_TARGET
] = &target_vtable
,
46 [UNIT_DEVICE
] = &device_vtable
,
47 [UNIT_MOUNT
] = &mount_vtable
,
48 [UNIT_AUTOMOUNT
] = &automount_vtable
,
49 [UNIT_SNAPSHOT
] = &snapshot_vtable
52 Unit
*unit_new(Manager
*m
) {
57 if (!(u
= new0(Unit
, 1)))
60 if (!(u
->meta
.names
= set_new(string_hash_func
, string_compare_func
))) {
66 u
->meta
.type
= _UNIT_TYPE_INVALID
;
71 bool unit_has_name(Unit
*u
, const char *name
) {
75 return !!set_get(u
->meta
.names
, (char*) name
);
78 int unit_add_name(Unit
*u
, const char *text
) {
80 char *s
= NULL
, *i
= NULL
;
86 if (unit_name_is_template(text
)) {
87 if (!u
->meta
.instance
)
90 s
= unit_name_replace_instance(text
, u
->meta
.instance
);
97 if (!unit_name_is_valid(s
)) {
102 assert_se((t
= unit_name_to_type(s
)) >= 0);
104 if (u
->meta
.type
!= _UNIT_TYPE_INVALID
&& t
!= u
->meta
.type
) {
109 if ((r
= unit_name_to_instance(s
, &i
)) < 0)
112 if (i
&& unit_vtable
[t
]->no_instances
)
115 if (u
->meta
.type
!= _UNIT_TYPE_INVALID
&& !streq_ptr(u
->meta
.instance
, i
)) {
120 if (unit_vtable
[t
]->no_alias
&&
121 !set_isempty(u
->meta
.names
) &&
122 !set_get(u
->meta
.names
, s
)) {
127 if ((r
= set_put(u
->meta
.names
, s
)) < 0) {
133 if ((r
= hashmap_put(u
->meta
.manager
->units
, s
, u
)) < 0) {
134 set_remove(u
->meta
.names
, s
);
138 if (u
->meta
.type
== _UNIT_TYPE_INVALID
) {
142 u
->meta
.instance
= i
;
144 LIST_PREPEND(Meta
, units_per_type
, u
->meta
.manager
->units_per_type
[t
], &u
->meta
);
146 if (UNIT_VTABLE(u
)->init
)
147 UNIT_VTABLE(u
)->init(u
);
151 unit_add_to_dbus_queue(u
);
161 int unit_choose_id(Unit
*u
, const char *name
) {
167 if (unit_name_is_template(name
)) {
169 if (!u
->meta
.instance
)
172 if (!(t
= unit_name_replace_instance(name
, u
->meta
.instance
)))
178 /* Selects one of the names of this unit as the id */
179 s
= set_get(u
->meta
.names
, (char*) name
);
186 unit_add_to_dbus_queue(u
);
191 int unit_set_description(Unit
*u
, const char *description
) {
196 if (!(s
= strdup(description
)))
199 free(u
->meta
.description
);
200 u
->meta
.description
= s
;
202 unit_add_to_dbus_queue(u
);
206 void unit_add_to_load_queue(Unit
*u
) {
208 assert(u
->meta
.type
!= _UNIT_TYPE_INVALID
);
210 if (u
->meta
.load_state
!= UNIT_STUB
|| u
->meta
.in_load_queue
)
213 LIST_PREPEND(Meta
, load_queue
, u
->meta
.manager
->load_queue
, &u
->meta
);
214 u
->meta
.in_load_queue
= true;
217 void unit_add_to_cleanup_queue(Unit
*u
) {
220 if (u
->meta
.in_cleanup_queue
)
223 LIST_PREPEND(Meta
, cleanup_queue
, u
->meta
.manager
->cleanup_queue
, &u
->meta
);
224 u
->meta
.in_cleanup_queue
= true;
227 void unit_add_to_dbus_queue(Unit
*u
) {
229 assert(u
->meta
.type
!= _UNIT_TYPE_INVALID
);
231 if (u
->meta
.load_state
== UNIT_STUB
|| u
->meta
.in_dbus_queue
|| set_isempty(u
->meta
.manager
->subscribed
))
234 LIST_PREPEND(Meta
, dbus_queue
, u
->meta
.manager
->dbus_unit_queue
, &u
->meta
);
235 u
->meta
.in_dbus_queue
= true;
238 static void bidi_set_free(Unit
*u
, Set
*s
) {
244 /* Frees the set and makes sure we are dropped from the
245 * inverse pointers */
247 SET_FOREACH(other
, s
, i
) {
250 for (d
= 0; d
< _UNIT_DEPENDENCY_MAX
; d
++)
251 set_remove(other
->meta
.dependencies
[d
], u
);
257 void unit_free(Unit
*u
) {
264 bus_unit_send_removed_signal(u
);
266 /* Detach from next 'bigger' objects */
267 SET_FOREACH(t
, u
->meta
.names
, i
)
268 hashmap_remove_value(u
->meta
.manager
->units
, t
, u
);
270 if (u
->meta
.type
!= _UNIT_TYPE_INVALID
)
271 LIST_REMOVE(Meta
, units_per_type
, u
->meta
.manager
->units_per_type
[u
->meta
.type
], &u
->meta
);
273 if (u
->meta
.in_load_queue
)
274 LIST_REMOVE(Meta
, load_queue
, u
->meta
.manager
->load_queue
, &u
->meta
);
276 if (u
->meta
.in_dbus_queue
)
277 LIST_REMOVE(Meta
, dbus_queue
, u
->meta
.manager
->dbus_unit_queue
, &u
->meta
);
279 if (u
->meta
.in_cleanup_queue
)
280 LIST_REMOVE(Meta
, cleanup_queue
, u
->meta
.manager
->cleanup_queue
, &u
->meta
);
282 /* Free data and next 'smaller' objects */
284 job_free(u
->meta
.job
);
286 if (u
->meta
.load_state
!= UNIT_STUB
)
287 if (UNIT_VTABLE(u
)->done
)
288 UNIT_VTABLE(u
)->done(u
);
290 cgroup_bonding_free_list(u
->meta
.cgroup_bondings
);
292 for (d
= 0; d
< _UNIT_DEPENDENCY_MAX
; d
++)
293 bidi_set_free(u
, u
->meta
.dependencies
[d
]);
295 free(u
->meta
.description
);
296 free(u
->meta
.fragment_path
);
298 while ((t
= set_steal_first(u
->meta
.names
)))
300 set_free(u
->meta
.names
);
302 free(u
->meta
.instance
);
307 UnitActiveState
unit_active_state(Unit
*u
) {
310 if (u
->meta
.load_state
!= UNIT_LOADED
)
311 return UNIT_INACTIVE
;
313 return UNIT_VTABLE(u
)->active_state(u
);
316 const char* unit_sub_state_to_string(Unit
*u
) {
319 return UNIT_VTABLE(u
)->sub_state_to_string(u
);
322 static void complete_move(Set
**s
, Set
**other
) {
330 set_move(*s
, *other
);
337 static void merge_names(Unit
*u
, Unit
*other
) {
344 complete_move(&u
->meta
.names
, &other
->meta
.names
);
346 while ((t
= set_steal_first(other
->meta
.names
)))
349 set_free(other
->meta
.names
);
350 other
->meta
.names
= NULL
;
351 other
->meta
.id
= NULL
;
353 SET_FOREACH(t
, u
->meta
.names
, i
)
354 assert_se(hashmap_replace(u
->meta
.manager
->units
, t
, u
) == 0);
357 static void merge_dependencies(Unit
*u
, Unit
*other
, UnitDependency d
) {
364 assert(d
< _UNIT_DEPENDENCY_MAX
);
366 SET_FOREACH(back
, other
->meta
.dependencies
[d
], i
) {
369 for (k
= 0; k
< _UNIT_DEPENDENCY_MAX
; k
++)
370 if ((r
= set_remove_and_put(back
->meta
.dependencies
[k
], other
, u
)) < 0) {
373 set_remove(back
->meta
.dependencies
[k
], other
);
375 assert(r
== -ENOENT
);
379 complete_move(&u
->meta
.dependencies
[d
], &other
->meta
.dependencies
[d
]);
381 set_free(other
->meta
.dependencies
[d
]);
382 other
->meta
.dependencies
[d
] = NULL
;
385 int unit_merge(Unit
*u
, Unit
*other
) {
390 assert(u
->meta
.manager
== other
->meta
.manager
);
391 assert(u
->meta
.type
!= _UNIT_TYPE_INVALID
);
393 other
= unit_follow_merge(other
);
398 if (u
->meta
.type
!= other
->meta
.type
)
401 if (!streq_ptr(u
->meta
.instance
, other
->meta
.instance
))
404 if (other
->meta
.load_state
!= UNIT_STUB
&&
405 other
->meta
.load_state
!= UNIT_FAILED
)
411 if (unit_active_state(other
) != UNIT_INACTIVE
)
415 merge_names(u
, other
);
417 /* Merge dependencies */
418 for (d
= 0; d
< _UNIT_DEPENDENCY_MAX
; d
++)
419 merge_dependencies(u
, other
, d
);
421 other
->meta
.load_state
= UNIT_MERGED
;
422 other
->meta
.merged_into
= u
;
424 /* If there is still some data attached to the other node, we
425 * don't need it anymore, and can free it. */
426 if (other
->meta
.load_state
!= UNIT_STUB
)
427 if (UNIT_VTABLE(other
)->done
)
428 UNIT_VTABLE(other
)->done(other
);
430 unit_add_to_dbus_queue(u
);
431 unit_add_to_cleanup_queue(other
);
436 int unit_merge_by_name(Unit
*u
, const char *name
) {
444 if (unit_name_is_template(name
)) {
445 if (!u
->meta
.instance
)
448 if (!(s
= unit_name_replace_instance(name
, u
->meta
.instance
)))
454 if (!(other
= manager_get_unit(u
->meta
.manager
, name
)))
455 r
= unit_add_name(u
, name
);
457 r
= unit_merge(u
, other
);
463 Unit
* unit_follow_merge(Unit
*u
) {
466 while (u
->meta
.load_state
== UNIT_MERGED
)
467 assert_se(u
= u
->meta
.merged_into
);
472 int unit_add_exec_dependencies(Unit
*u
, ExecContext
*c
) {
478 if (c
->std_output
!= EXEC_OUTPUT_KERNEL
&& c
->std_output
!= EXEC_OUTPUT_SYSLOG
)
481 /* If syslog or kernel logging is requested, make sure our own
482 * logging daemon is run first. */
484 if ((r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, SPECIAL_LOGGER_SOCKET
, NULL
)) < 0)
487 if (u
->meta
.manager
->running_as
!= MANAGER_SESSION
)
488 if ((r
= unit_add_dependency_by_name(u
, UNIT_REQUIRES
, SPECIAL_LOGGER_SOCKET
, NULL
)) < 0)
494 const char *unit_description(Unit
*u
) {
497 if (u
->meta
.description
)
498 return u
->meta
.description
;
503 void unit_dump(Unit
*u
, FILE *f
, const char *prefix
) {
510 char timestamp1
[FORMAT_TIMESTAMP_MAX
], timestamp2
[FORMAT_TIMESTAMP_MAX
];
513 assert(u
->meta
.type
>= 0);
517 p2
= strappend(prefix
, "\t");
518 prefix2
= p2
? p2
: prefix
;
522 "%s\tDescription: %s\n"
524 "%s\tUnit Load State: %s\n"
525 "%s\tUnit Active State: %s\n"
526 "%s\tActive Enter Timestamp: %s\n"
527 "%s\tActive Exit Timestamp: %s\n",
529 prefix
, unit_description(u
),
530 prefix
, strna(u
->meta
.instance
),
531 prefix
, unit_load_state_to_string(u
->meta
.load_state
),
532 prefix
, unit_active_state_to_string(unit_active_state(u
)),
533 prefix
, strna(format_timestamp(timestamp1
, sizeof(timestamp1
), u
->meta
.active_enter_timestamp
)),
534 prefix
, strna(format_timestamp(timestamp2
, sizeof(timestamp2
), u
->meta
.active_exit_timestamp
)));
536 SET_FOREACH(t
, u
->meta
.names
, i
)
537 fprintf(f
, "%s\tName: %s\n", prefix
, t
);
539 if (u
->meta
.fragment_path
)
540 fprintf(f
, "%s\tFragment Path: %s\n", prefix
, u
->meta
.fragment_path
);
542 for (d
= 0; d
< _UNIT_DEPENDENCY_MAX
; d
++) {
545 SET_FOREACH(other
, u
->meta
.dependencies
[d
], i
)
546 fprintf(f
, "%s\t%s: %s\n", prefix
, unit_dependency_to_string(d
), other
->meta
.id
);
549 if (u
->meta
.load_state
== UNIT_LOADED
) {
551 "%s\tRecursive Stop: %s\n"
552 "%s\tStop When Unneeded: %s\n",
553 prefix
, yes_no(u
->meta
.recursive_stop
),
554 prefix
, yes_no(u
->meta
.stop_when_unneeded
));
556 LIST_FOREACH(by_unit
, b
, u
->meta
.cgroup_bondings
)
557 fprintf(f
, "%s\tControlGroup: %s:%s\n",
558 prefix
, b
->controller
, b
->path
);
560 if (UNIT_VTABLE(u
)->dump
)
561 UNIT_VTABLE(u
)->dump(u
, f
, prefix2
);
563 } else if (u
->meta
.load_state
== UNIT_MERGED
)
565 "%s\tMerged into: %s\n",
566 prefix
, u
->meta
.merged_into
->meta
.id
);
569 job_dump(u
->meta
.job
, f
, prefix2
);
574 /* Common implementation for multiple backends */
575 int unit_load_fragment_and_dropin(Unit
*u
) {
580 /* Load a .service file */
581 if ((r
= unit_load_fragment(u
)) < 0)
584 if (u
->meta
.load_state
== UNIT_STUB
)
587 /* Load drop-in directory data */
588 if ((r
= unit_load_dropin(unit_follow_merge(u
))) < 0)
594 /* Common implementation for multiple backends */
595 int unit_load_fragment_and_dropin_optional(Unit
*u
) {
600 /* Same as unit_load_fragment_and_dropin(), but whether
601 * something can be loaded or not doesn't matter. */
603 /* Load a .service file */
604 if ((r
= unit_load_fragment(u
)) < 0)
607 if (u
->meta
.load_state
== UNIT_STUB
)
608 u
->meta
.load_state
= UNIT_LOADED
;
610 /* Load drop-in directory data */
611 if ((r
= unit_load_dropin(unit_follow_merge(u
))) < 0)
617 int unit_load(Unit
*u
) {
622 if (u
->meta
.in_load_queue
) {
623 LIST_REMOVE(Meta
, load_queue
, u
->meta
.manager
->load_queue
, &u
->meta
);
624 u
->meta
.in_load_queue
= false;
627 if (u
->meta
.type
== _UNIT_TYPE_INVALID
)
630 if (u
->meta
.load_state
!= UNIT_STUB
)
633 if (UNIT_VTABLE(u
)->load
)
634 if ((r
= UNIT_VTABLE(u
)->load(u
)) < 0)
637 if (u
->meta
.load_state
== UNIT_STUB
) {
642 assert((u
->meta
.load_state
!= UNIT_MERGED
) == !u
->meta
.merged_into
);
644 unit_add_to_dbus_queue(unit_follow_merge(u
));
649 u
->meta
.load_state
= UNIT_FAILED
;
650 unit_add_to_dbus_queue(u
);
652 log_error("Failed to load configuration for %s: %s", u
->meta
.id
, strerror(-r
));
658 * -EBADR: This unit type does not support starting.
659 * -EALREADY: Unit is already started.
660 * -EAGAIN: An operation is already in progress. Retry later.
662 int unit_start(Unit
*u
) {
663 UnitActiveState state
;
667 /* If this is already (being) started, then this will
668 * succeed. Note that this will even succeed if this unit is
669 * not startable by the user. This is relied on to detect when
670 * we need to wait for units and when waiting is finished. */
671 state
= unit_active_state(u
);
672 if (UNIT_IS_ACTIVE_OR_RELOADING(state
))
675 /* If it is stopped, but we cannot start it, then fail */
676 if (!UNIT_VTABLE(u
)->start
)
679 /* We don't suppress calls to ->start() here when we are
680 * already starting, to allow this request to be used as a
681 * "hurry up" call, for example when the unit is in some "auto
682 * restart" state where it waits for a holdoff timer to elapse
683 * before it will start again. */
685 unit_add_to_dbus_queue(u
);
686 return UNIT_VTABLE(u
)->start(u
);
689 bool unit_can_start(Unit
*u
) {
692 return !!UNIT_VTABLE(u
)->start
;
696 * -EBADR: This unit type does not support stopping.
697 * -EALREADY: Unit is already stopped.
698 * -EAGAIN: An operation is already in progress. Retry later.
700 int unit_stop(Unit
*u
) {
701 UnitActiveState state
;
705 state
= unit_active_state(u
);
706 if (state
== UNIT_INACTIVE
)
709 if (!UNIT_VTABLE(u
)->stop
)
712 unit_add_to_dbus_queue(u
);
713 return UNIT_VTABLE(u
)->stop(u
);
717 * -EBADR: This unit type does not support reloading.
718 * -ENOEXEC: Unit is not started.
719 * -EAGAIN: An operation is already in progress. Retry later.
721 int unit_reload(Unit
*u
) {
722 UnitActiveState state
;
726 if (!unit_can_reload(u
))
729 state
= unit_active_state(u
);
730 if (unit_active_state(u
) == UNIT_ACTIVE_RELOADING
)
733 if (unit_active_state(u
) != UNIT_ACTIVE
)
736 unit_add_to_dbus_queue(u
);
737 return UNIT_VTABLE(u
)->reload(u
);
740 bool unit_can_reload(Unit
*u
) {
743 if (!UNIT_VTABLE(u
)->reload
)
746 if (!UNIT_VTABLE(u
)->can_reload
)
749 return UNIT_VTABLE(u
)->can_reload(u
);
752 static void unit_check_uneeded(Unit
*u
) {
758 /* If this service shall be shut down when unneeded then do
761 if (!u
->meta
.stop_when_unneeded
)
764 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u
)))
767 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRED_BY
], i
)
768 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
771 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRED_BY_OVERRIDABLE
], i
)
772 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
775 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_WANTED_BY
], i
)
776 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
779 log_debug("Service %s is not needed anymore. Stopping.", u
->meta
.id
);
781 /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
782 manager_add_job(u
->meta
.manager
, JOB_STOP
, u
, JOB_FAIL
, true, NULL
);
785 static void retroactively_start_dependencies(Unit
*u
) {
790 assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u
)));
792 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRES
], i
)
793 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other
)))
794 manager_add_job(u
->meta
.manager
, JOB_START
, other
, JOB_REPLACE
, true, NULL
);
796 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRES_OVERRIDABLE
], i
)
797 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other
)))
798 manager_add_job(u
->meta
.manager
, JOB_START
, other
, JOB_FAIL
, false, NULL
);
800 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUISITE
], i
)
801 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other
)))
802 manager_add_job(u
->meta
.manager
, JOB_START
, other
, JOB_REPLACE
, true, NULL
);
804 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_WANTS
], i
)
805 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other
)))
806 manager_add_job(u
->meta
.manager
, JOB_START
, other
, JOB_FAIL
, false, NULL
);
808 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_CONFLICTS
], i
)
809 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other
)))
810 manager_add_job(u
->meta
.manager
, JOB_STOP
, other
, JOB_REPLACE
, true, NULL
);
813 static void retroactively_stop_dependencies(Unit
*u
) {
818 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u
)));
820 if (u
->meta
.recursive_stop
) {
821 /* Pull down units need us recursively if enabled */
822 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRED_BY
], i
)
823 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
824 manager_add_job(u
->meta
.manager
, JOB_STOP
, other
, JOB_REPLACE
, true, NULL
);
827 /* Garbage collect services that might not be needed anymore, if enabled */
828 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRES
], i
)
829 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
830 unit_check_uneeded(other
);
831 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRES_OVERRIDABLE
], i
)
832 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
833 unit_check_uneeded(other
);
834 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_WANTS
], i
)
835 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
836 unit_check_uneeded(other
);
837 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUISITE
], i
)
838 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
839 unit_check_uneeded(other
);
840 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUISITE_OVERRIDABLE
], i
)
841 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other
)))
842 unit_check_uneeded(other
);
845 void unit_notify(Unit
*u
, UnitActiveState os
, UnitActiveState ns
) {
846 bool unexpected
= false;
849 assert(os
< _UNIT_ACTIVE_STATE_MAX
);
850 assert(ns
< _UNIT_ACTIVE_STATE_MAX
);
852 /* Note that this is called for all low-level state changes,
853 * even if they might map to the same high-level
854 * UnitActiveState! That means that ns == os is OK an expected
855 * behaviour here. For example: if a mount point is remounted
856 * this function will be called too and the utmp code below
859 if (!UNIT_IS_ACTIVE_OR_RELOADING(os
) && UNIT_IS_ACTIVE_OR_RELOADING(ns
))
860 u
->meta
.active_enter_timestamp
= now(CLOCK_REALTIME
);
861 else if (UNIT_IS_ACTIVE_OR_RELOADING(os
) && !UNIT_IS_ACTIVE_OR_RELOADING(ns
))
862 u
->meta
.active_exit_timestamp
= now(CLOCK_REALTIME
);
866 if (u
->meta
.job
->state
== JOB_WAITING
)
868 /* So we reached a different state for this
869 * job. Let's see if we can run it now if it
870 * failed previously due to EAGAIN. */
871 job_add_to_run_queue(u
->meta
.job
);
874 assert(u
->meta
.job
->state
== JOB_RUNNING
);
876 /* Let's check whether this state change
877 * constitutes a finished job, or maybe
878 * cotradicts a running job and hence needs to
879 * invalidate jobs. */
881 switch (u
->meta
.job
->type
) {
884 case JOB_VERIFY_ACTIVE
:
886 if (UNIT_IS_ACTIVE_OR_RELOADING(ns
))
887 job_finish_and_invalidate(u
->meta
.job
, true);
888 else if (ns
!= UNIT_ACTIVATING
) {
890 job_finish_and_invalidate(u
->meta
.job
, false);
896 case JOB_RELOAD_OR_START
:
898 if (ns
== UNIT_ACTIVE
)
899 job_finish_and_invalidate(u
->meta
.job
, true);
900 else if (ns
!= UNIT_ACTIVATING
&& ns
!= UNIT_ACTIVE_RELOADING
) {
902 job_finish_and_invalidate(u
->meta
.job
, false);
909 case JOB_TRY_RESTART
:
911 if (ns
== UNIT_INACTIVE
)
912 job_finish_and_invalidate(u
->meta
.job
, true);
913 else if (ns
!= UNIT_DEACTIVATING
) {
915 job_finish_and_invalidate(u
->meta
.job
, false);
921 assert_not_reached("Job type unknown");
926 /* If this state change happened without being requested by a
927 * job, then let's retroactively start or stop dependencies */
930 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os
) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns
))
931 retroactively_start_dependencies(u
);
932 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os
) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns
))
933 retroactively_stop_dependencies(u
);
936 /* Some names are special */
937 if (UNIT_IS_ACTIVE_OR_RELOADING(ns
)) {
938 if (unit_has_name(u
, SPECIAL_DBUS_SERVICE
)) {
939 /* The bus just might have become available,
940 * hence try to connect to it, if we aren't
942 bus_init_system(u
->meta
.manager
);
943 bus_init_api(u
->meta
.manager
);
946 if (unit_has_name(u
, SPECIAL_SYSLOG_SERVICE
))
947 /* The syslog daemon just might have become
948 * available, hence try to connect to it, if
949 * we aren't yet connected. */
952 if (u
->meta
.type
== UNIT_MOUNT
)
953 /* Another directory became available, let's
954 * check if that is enough to write our utmp
956 manager_write_utmp_reboot(u
->meta
.manager
);
958 if (u
->meta
.type
== UNIT_TARGET
)
959 /* A target got activated, maybe this is a runlevel? */
960 manager_write_utmp_runlevel(u
->meta
.manager
, u
);
962 } else if (!UNIT_IS_ACTIVE_OR_RELOADING(ns
)) {
964 if (unit_has_name(u
, SPECIAL_SYSLOG_SERVICE
))
965 /* The syslog daemon might just have
966 * terminated, hence try to disconnect from
970 /* We don't care about D-Bus here, since we'll get an
971 * asynchronous notification for it anyway. */
974 /* Maybe we finished startup and are now ready for being
975 * stopped because unneeded? */
976 unit_check_uneeded(u
);
978 unit_add_to_dbus_queue(u
);
981 int unit_watch_fd(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
982 struct epoll_event ev
;
987 assert(w
->type
== WATCH_INVALID
|| (w
->type
== WATCH_FD
&& w
->fd
== fd
&& w
->data
.unit
== u
));
993 if (epoll_ctl(u
->meta
.manager
->epoll_fd
,
994 w
->type
== WATCH_INVALID
? EPOLL_CTL_ADD
: EPOLL_CTL_MOD
,
1006 void unit_unwatch_fd(Unit
*u
, Watch
*w
) {
1010 if (w
->type
== WATCH_INVALID
)
1013 assert(w
->type
== WATCH_FD
);
1014 assert(w
->data
.unit
== u
);
1015 assert_se(epoll_ctl(u
->meta
.manager
->epoll_fd
, EPOLL_CTL_DEL
, w
->fd
, NULL
) >= 0);
1018 w
->type
= WATCH_INVALID
;
1019 w
->data
.unit
= NULL
;
1022 int unit_watch_pid(Unit
*u
, pid_t pid
) {
1026 /* Watch a specific PID. We only support one unit watching
1027 * each PID for now. */
1029 return hashmap_put(u
->meta
.manager
->watch_pids
, UINT32_TO_PTR(pid
), u
);
1032 void unit_unwatch_pid(Unit
*u
, pid_t pid
) {
1036 hashmap_remove_value(u
->meta
.manager
->watch_pids
, UINT32_TO_PTR(pid
), u
);
1039 int unit_watch_timer(Unit
*u
, usec_t delay
, Watch
*w
) {
1040 struct itimerspec its
;
1046 assert(w
->type
== WATCH_INVALID
|| (w
->type
== WATCH_TIMER
&& w
->data
.unit
== u
));
1048 /* This will try to reuse the old timer if there is one */
1050 if (w
->type
== WATCH_TIMER
) {
1055 if ((fd
= timerfd_create(CLOCK_MONOTONIC
, TFD_NONBLOCK
|TFD_CLOEXEC
)) < 0)
1062 /* Set absolute time in the past, but not 0, since we
1063 * don't want to disarm the timer */
1064 its
.it_value
.tv_sec
= 0;
1065 its
.it_value
.tv_nsec
= 1;
1067 flags
= TFD_TIMER_ABSTIME
;
1069 timespec_store(&its
.it_value
, delay
);
1073 /* This will also flush the elapse counter */
1074 if (timerfd_settime(fd
, flags
, &its
, NULL
) < 0)
1077 if (w
->type
== WATCH_INVALID
) {
1078 struct epoll_event ev
;
1082 ev
.events
= EPOLLIN
;
1084 if (epoll_ctl(u
->meta
.manager
->epoll_fd
, EPOLL_CTL_ADD
, fd
, &ev
) < 0)
1089 w
->type
= WATCH_TIMER
;
1096 close_nointr_nofail(fd
);
1101 void unit_unwatch_timer(Unit
*u
, Watch
*w
) {
1105 if (w
->type
== WATCH_INVALID
)
1108 assert(w
->type
== WATCH_TIMER
&& w
->data
.unit
== u
);
1110 assert_se(epoll_ctl(u
->meta
.manager
->epoll_fd
, EPOLL_CTL_DEL
, w
->fd
, NULL
) >= 0);
1111 assert_se(close_nointr(w
->fd
) == 0);
1114 w
->type
= WATCH_INVALID
;
1115 w
->data
.unit
= NULL
;
1118 bool unit_job_is_applicable(Unit
*u
, JobType j
) {
1120 assert(j
>= 0 && j
< _JOB_TYPE_MAX
);
1124 case JOB_VERIFY_ACTIVE
:
1130 case JOB_TRY_RESTART
:
1131 return unit_can_start(u
);
1134 return unit_can_reload(u
);
1136 case JOB_RELOAD_OR_START
:
1137 return unit_can_reload(u
) && unit_can_start(u
);
1140 assert_not_reached("Invalid job type");
1144 int unit_add_dependency(Unit
*u
, UnitDependency d
, Unit
*other
) {
1146 static const UnitDependency inverse_table
[_UNIT_DEPENDENCY_MAX
] = {
1147 [UNIT_REQUIRES
] = UNIT_REQUIRED_BY
,
1148 [UNIT_REQUIRES_OVERRIDABLE
] = UNIT_REQUIRED_BY_OVERRIDABLE
,
1149 [UNIT_WANTS
] = UNIT_WANTED_BY
,
1150 [UNIT_REQUISITE
] = UNIT_REQUIRED_BY
,
1151 [UNIT_REQUISITE_OVERRIDABLE
] = UNIT_REQUIRED_BY_OVERRIDABLE
,
1152 [UNIT_REQUIRED_BY
] = _UNIT_DEPENDENCY_INVALID
,
1153 [UNIT_REQUIRED_BY_OVERRIDABLE
] = _UNIT_DEPENDENCY_INVALID
,
1154 [UNIT_WANTED_BY
] = _UNIT_DEPENDENCY_INVALID
,
1155 [UNIT_CONFLICTS
] = UNIT_CONFLICTS
,
1156 [UNIT_BEFORE
] = UNIT_AFTER
,
1157 [UNIT_AFTER
] = UNIT_BEFORE
1162 assert(d
>= 0 && d
< _UNIT_DEPENDENCY_MAX
);
1163 assert(inverse_table
[d
] != _UNIT_DEPENDENCY_INVALID
);
1166 /* We won't allow dependencies on ourselves. We will not
1167 * consider them an error however. */
1171 if (UNIT_VTABLE(u
)->no_requires
&&
1172 (d
== UNIT_REQUIRES
||
1173 d
== UNIT_REQUIRES_OVERRIDABLE
||
1174 d
== UNIT_REQUISITE
||
1175 d
== UNIT_REQUISITE_OVERRIDABLE
)) {
1179 if ((r
= set_ensure_allocated(&u
->meta
.dependencies
[d
], trivial_hash_func
, trivial_compare_func
)) < 0)
1182 if ((r
= set_ensure_allocated(&other
->meta
.dependencies
[inverse_table
[d
]], trivial_hash_func
, trivial_compare_func
)) < 0)
1185 if ((r
= set_put(u
->meta
.dependencies
[d
], other
)) < 0)
1188 if ((r
= set_put(other
->meta
.dependencies
[inverse_table
[d
]], u
)) < 0) {
1189 set_remove(u
->meta
.dependencies
[d
], other
);
1193 unit_add_to_dbus_queue(u
);
1197 static const char *resolve_template(Unit
*u
, const char *name
, const char*path
, char **p
) {
1201 assert(name
|| path
);
1204 name
= file_name_from_path(path
);
1206 if (!unit_name_is_template(name
)) {
1211 if (u
->meta
.instance
)
1212 s
= unit_name_replace_instance(name
, u
->meta
.instance
);
1216 if (!(i
= unit_name_to_prefix(u
->meta
.id
)))
1219 s
= unit_name_replace_instance(name
, i
);
1230 int unit_add_dependency_by_name(Unit
*u
, UnitDependency d
, const char *name
, const char *path
) {
1236 assert(name
|| path
);
1238 if (!(name
= resolve_template(u
, name
, path
, &s
)))
1241 if ((r
= manager_load_unit(u
->meta
.manager
, name
, path
, &other
)) < 0)
1244 r
= unit_add_dependency(u
, d
, other
);
1251 int unit_add_dependency_by_name_inverse(Unit
*u
, UnitDependency d
, const char *name
, const char *path
) {
1257 assert(name
|| path
);
1259 if (!(name
= resolve_template(u
, name
, path
, &s
)))
1262 if ((r
= manager_load_unit(u
->meta
.manager
, name
, path
, &other
)) < 0)
1265 r
= unit_add_dependency(other
, d
, u
);
1272 int set_unit_path(const char *p
) {
1276 /* This is mostly for debug purposes */
1278 if (path_is_absolute(p
)) {
1279 if (!(c
= strdup(p
)))
1282 if (!(cwd
= get_current_dir_name()))
1285 r
= asprintf(&c
, "%s/%s", cwd
, p
);
1292 if (setenv("SYSTEMD_UNIT_PATH", c
, 0) < 0) {
1301 char *unit_dbus_path(Unit
*u
) {
1306 if (!(e
= bus_path_escape(u
->meta
.id
)))
1309 if (asprintf(&p
, "/org/freedesktop/systemd1/unit/%s", e
) < 0) {
1318 int unit_add_cgroup(Unit
*u
, CGroupBonding
*b
) {
1326 /* Ensure this hasn't been added yet */
1329 l
= hashmap_get(u
->meta
.manager
->cgroup_bondings
, b
->path
);
1330 LIST_PREPEND(CGroupBonding
, by_path
, l
, b
);
1332 if ((r
= hashmap_replace(u
->meta
.manager
->cgroup_bondings
, b
->path
, l
)) < 0) {
1333 LIST_REMOVE(CGroupBonding
, by_path
, l
, b
);
1337 LIST_PREPEND(CGroupBonding
, by_unit
, u
->meta
.cgroup_bondings
, b
);
1343 static char *default_cgroup_path(Unit
*u
) {
1349 if (u
->meta
.instance
) {
1352 if (!(t
= unit_name_template(u
->meta
.id
)))
1355 r
= asprintf(&p
, "%s/%s/%s", u
->meta
.manager
->cgroup_hierarchy
, t
, u
->meta
.instance
);
1358 r
= asprintf(&p
, "%s/%s", u
->meta
.manager
->cgroup_hierarchy
, u
->meta
.id
);
1360 return r
< 0 ? NULL
: p
;
1363 int unit_add_cgroup_from_text(Unit
*u
, const char *name
) {
1365 char *controller
= NULL
, *path
= NULL
;
1366 CGroupBonding
*b
= NULL
;
1372 /* Detect controller name */
1373 n
= strcspn(name
, ":");
1376 (name
[n
] == ':' && name
[n
+1] == 0)) {
1378 /* Only controller name, no path? */
1380 if (!(path
= default_cgroup_path(u
)))
1386 /* Controller name, and path. */
1389 if (!path_is_absolute(p
))
1392 if (!(path
= strdup(p
)))
1397 controller
= strndup(name
, n
);
1399 controller
= strdup(u
->meta
.manager
->cgroup_controller
);
1406 if (cgroup_bonding_find_list(u
->meta
.cgroup_bondings
, controller
)) {
1411 if (!(b
= new0(CGroupBonding
, 1))) {
1416 b
->controller
= controller
;
1419 b
->clean_up
= false;
1421 if ((r
= unit_add_cgroup(u
, b
)) < 0)
1434 int unit_add_default_cgroup(Unit
*u
) {
1440 /* Adds in the default cgroup data, if it wasn't specified yet */
1442 if (unit_get_default_cgroup(u
))
1445 if (!(b
= new0(CGroupBonding
, 1)))
1448 if (!(b
->controller
= strdup(u
->meta
.manager
->cgroup_controller
)))
1451 if (!(b
->path
= default_cgroup_path(u
)))
1457 if ((r
= unit_add_cgroup(u
, b
)) < 0)
1464 free(b
->controller
);
1470 CGroupBonding
* unit_get_default_cgroup(Unit
*u
) {
1473 return cgroup_bonding_find_list(u
->meta
.cgroup_bondings
, u
->meta
.manager
->cgroup_controller
);
1476 int unit_load_related_unit(Unit
*u
, const char *type
, Unit
**_found
) {
1484 if (!(t
= unit_name_change_suffix(u
->meta
.id
, type
)))
1487 assert(!unit_has_name(u
, t
));
1489 r
= manager_load_unit(u
->meta
.manager
, t
, NULL
, _found
);
1492 assert(r
< 0 || *_found
!= u
);
1497 static char *specifier_prefix_and_instance(char specifier
, void *data
, void *userdata
) {
1501 return unit_name_to_prefix_and_instance(u
->meta
.id
);
1504 static char *specifier_prefix(char specifier
, void *data
, void *userdata
) {
1508 return unit_name_to_prefix(u
->meta
.id
);
1511 static char *specifier_prefix_unescaped(char specifier
, void *data
, void *userdata
) {
1517 if (!(p
= unit_name_to_prefix(u
->meta
.id
)))
1520 r
= unit_name_unescape(p
);
1526 static char *specifier_instance_unescaped(char specifier
, void *data
, void *userdata
) {
1530 if (u
->meta
.instance
)
1531 return unit_name_unescape(u
->meta
.instance
);
1536 char *unit_name_printf(Unit
*u
, const char* format
) {
1539 * This will use the passed string as format string and
1540 * replace the following specifiers:
1542 * %n: the full id of the unit (foo@bar.waldo)
1543 * %N: the id of the unit without the suffix (foo@bar)
1544 * %p: the prefix (foo)
1545 * %i: the instance (bar)
1548 const Specifier table
[] = {
1549 { 'n', specifier_string
, u
->meta
.id
},
1550 { 'N', specifier_prefix_and_instance
, NULL
},
1551 { 'p', specifier_prefix
, NULL
},
1552 { 'i', specifier_string
, u
->meta
.instance
},
1559 return specifier_printf(format
, table
, u
);
1562 char *unit_full_printf(Unit
*u
, const char *format
) {
1564 /* This is similar to unit_name_printf() but also supports
1567 const Specifier table
[] = {
1568 { 'n', specifier_string
, u
->meta
.id
},
1569 { 'N', specifier_prefix_and_instance
, NULL
},
1570 { 'p', specifier_prefix
, NULL
},
1571 { 'P', specifier_prefix_unescaped
, NULL
},
1572 { 'i', specifier_string
, u
->meta
.instance
},
1573 { 'I', specifier_instance_unescaped
, NULL
},
1580 return specifier_printf(format
, table
, u
);
1583 char **unit_full_printf_strv(Unit
*u
, char **l
) {
1587 /* Applies unit_full_printf to every entry in l */
1592 if (!(r
= new(char*, n
+1)))
1595 for (i
= l
, j
= r
; *i
; i
++, j
++)
1596 if (!(*j
= unit_full_printf(u
, *i
)))
1612 int unit_watch_bus_name(Unit
*u
, const char *name
) {
1616 /* Watch a specific name on the bus. We only support one unit
1617 * watching each name for now. */
1619 return hashmap_put(u
->meta
.manager
->watch_bus
, name
, u
);
1622 void unit_unwatch_bus_name(Unit
*u
, const char *name
) {
1626 hashmap_remove_value(u
->meta
.manager
->watch_bus
, name
, u
);
1629 static const char* const unit_type_table
[_UNIT_TYPE_MAX
] = {
1630 [UNIT_SERVICE
] = "service",
1631 [UNIT_TIMER
] = "timer",
1632 [UNIT_SOCKET
] = "socket",
1633 [UNIT_TARGET
] = "target",
1634 [UNIT_DEVICE
] = "device",
1635 [UNIT_MOUNT
] = "mount",
1636 [UNIT_AUTOMOUNT
] = "automount",
1637 [UNIT_SNAPSHOT
] = "snapshot"
1640 DEFINE_STRING_TABLE_LOOKUP(unit_type
, UnitType
);
1642 static const char* const unit_load_state_table
[_UNIT_LOAD_STATE_MAX
] = {
1643 [UNIT_STUB
] = "stub",
1644 [UNIT_LOADED
] = "loaded",
1645 [UNIT_FAILED
] = "failed",
1646 [UNIT_MERGED
] = "merged"
1649 DEFINE_STRING_TABLE_LOOKUP(unit_load_state
, UnitLoadState
);
1651 static const char* const unit_active_state_table
[_UNIT_ACTIVE_STATE_MAX
] = {
1652 [UNIT_ACTIVE
] = "active",
1653 [UNIT_INACTIVE
] = "inactive",
1654 [UNIT_ACTIVATING
] = "activating",
1655 [UNIT_DEACTIVATING
] = "deactivating"
1658 DEFINE_STRING_TABLE_LOOKUP(unit_active_state
, UnitActiveState
);
1660 static const char* const unit_dependency_table
[_UNIT_DEPENDENCY_MAX
] = {
1661 [UNIT_REQUIRES
] = "Requires",
1662 [UNIT_REQUIRES_OVERRIDABLE
] = "RequiresOverridable",
1663 [UNIT_WANTS
] = "Wants",
1664 [UNIT_REQUISITE
] = "Requisite",
1665 [UNIT_REQUISITE_OVERRIDABLE
] = "RequisiteOverridable",
1666 [UNIT_REQUIRED_BY
] = "RequiredBy",
1667 [UNIT_REQUIRED_BY_OVERRIDABLE
] = "RequiredByOverridable",
1668 [UNIT_WANTED_BY
] = "WantedBy",
1669 [UNIT_CONFLICTS
] = "Conflicts",
1670 [UNIT_BEFORE
] = "Before",
1671 [UNIT_AFTER
] = "After",
1674 DEFINE_STRING_TABLE_LOOKUP(unit_dependency
, UnitDependency
);
1676 static const char* const kill_mode_table
[_KILL_MODE_MAX
] = {
1677 [KILL_CONTROL_GROUP
] = "control-group",
1678 [KILL_PROCESS_GROUP
] = "process-group",
1679 [KILL_PROCESS
] = "process",
1680 [KILL_NONE
] = "none"
1683 DEFINE_STRING_TABLE_LOOKUP(kill_mode
, KillMode
);