]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/target.c
io.systemd.Unit.List fix context/runtime split (#38172)
[thirdparty/systemd.git] / src / core / target.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
a7334b09 2
836e4e7e
DDM
3#include <stdio.h>
4
6fcbec6f 5#include "dbus-unit.h"
d68c645b 6#include "serialize.h"
514f4ef5 7#include "special.h"
07630cea 8#include "string-util.h"
d68c645b 9#include "target.h"
07630cea 10#include "unit.h"
c22cbe26 11
fa068367 12static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = {
17f6b640
YW
13 [TARGET_DEAD] = UNIT_INACTIVE,
14 [TARGET_ACTIVE] = UNIT_ACTIVE,
fa068367
LP
15};
16
fa068367
LP
17static void target_set_state(Target *t, TargetState state) {
18 TargetState old_state;
b7e4e152 19
fa068367
LP
20 assert(t);
21
6fcbec6f
LP
22 if (t->state != state)
23 bus_unit_send_pending_change_signal(UNIT(t), false);
24
fa068367
LP
25 old_state = t->state;
26 t->state = state;
27
e537352b 28 if (state != old_state)
b7e4e152
MY
29 log_unit_debug(UNIT(t), "Changed %s -> %s",
30 target_state_to_string(old_state), target_state_to_string(state));
c22cbe26 31
96b09de5 32 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], /* reload_success = */ true);
fa068367
LP
33}
34
a40eb732 35static int target_add_default_dependencies(Target *t) {
15ed3c3a
LP
36 _cleanup_free_ Unit **others = NULL;
37 int r, n_others;
bba34eed 38
98bc2000 39 assert(t);
a40eb732 40
41c237af
IP
41 if (!UNIT(t)->default_dependencies)
42 return 0;
43
15ed3c3a
LP
44 /* Imply ordering for requirement dependencies on target units. Note that when the user created a
45 * contradicting ordering manually we won't add anything in here to make sure we don't create a
46 * loop.
47 *
48 * Note that quite likely iterating through these dependencies will add new dependencies, which
49 * conflicts with the hashmap-based iteration logic. Hence, instead of iterating through the
50 * dependencies and acting on them as we go, first take an "atomic snapshot" of sorts and iterate
51 * through that. */
52
53 n_others = unit_get_dependency_array(UNIT(t), UNIT_ATOM_ADD_DEFAULT_TARGET_DEPENDENCY_QUEUE, &others);
54 if (n_others < 0)
55 return n_others;
56
8b317c34
MY
57 FOREACH_ARRAY(i, others, n_others) {
58 r = unit_add_default_target_dependency(*i, UNIT(t));
15ed3c3a
LP
59 if (r < 0)
60 return r;
eef85c4a 61 }
bba34eed 62
33e28180
LP
63 if (unit_has_name(UNIT(t), SPECIAL_SHUTDOWN_TARGET))
64 return 0;
65
b401e1fb 66 /* Make sure targets are unloaded on shutdown */
3835b9aa 67 return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
a40eb732
LP
68}
69
70static int target_load(Unit *u) {
e9fa1bf7 71 Target *t = ASSERT_PTR(TARGET(u));
a40eb732
LP
72 int r;
73
c3620770 74 r = unit_load_fragment_and_dropin(u, true);
1850161f 75 if (r < 0)
a40eb732
LP
76 return r;
77
75193d41
ZJS
78 if (u->load_state != UNIT_LOADED)
79 return 0;
a40eb732 80
75193d41
ZJS
81 /* This is a new unit? Then let's add in some extras */
82 return target_add_default_dependencies(t);
a40eb732
LP
83}
84
be847e82 85static int target_coldplug(Unit *u) {
e9fa1bf7 86 Target *t = ASSERT_PTR(TARGET(u));
a16e1123 87
a16e1123
LP
88 assert(t->state == TARGET_DEAD);
89
90 if (t->deserialized_state != t->state)
91 target_set_state(t, t->deserialized_state);
92
93 return 0;
94}
95
96static void target_dump(Unit *u, FILE *f, const char *prefix) {
e9fa1bf7 97 Target *t = ASSERT_PTR(TARGET(u));
a16e1123 98
a16e1123 99 assert(f);
e9fa1bf7 100 assert(prefix);
a16e1123
LP
101
102 fprintf(f,
103 "%sTarget State: %s\n",
104 prefix, target_state_to_string(t->state));
105}
106
fa068367 107static int target_start(Unit *u) {
e9fa1bf7 108 Target *t = ASSERT_PTR(TARGET(u));
4b58153d 109 int r;
fa068367 110
fa068367
LP
111 assert(t->state == TARGET_DEAD);
112
4b58153d
LP
113 r = unit_acquire_invocation_id(u);
114 if (r < 0)
115 return r;
116
fa068367 117 target_set_state(t, TARGET_ACTIVE);
82a2b6bb 118 return 1;
fa068367 119}
c22cbe26 120
fa068367 121static int target_stop(Unit *u) {
e9fa1bf7 122 Target *t = ASSERT_PTR(TARGET(u));
fa068367 123
fa068367
LP
124 assert(t->state == TARGET_ACTIVE);
125
126 target_set_state(t, TARGET_DEAD);
82a2b6bb 127 return 1;
c22cbe26
LP
128}
129
a16e1123 130static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
e9fa1bf7 131 Target *t = ASSERT_PTR(TARGET(u));
a16e1123 132
a16e1123
LP
133 assert(f);
134 assert(fds);
135
e9fa1bf7 136 (void) serialize_item(f, "state", target_state_to_string(t->state));
a16e1123
LP
137 return 0;
138}
139
140static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
e9fa1bf7 141 Target *t = ASSERT_PTR(TARGET(u));
a16e1123 142
a16e1123
LP
143 assert(key);
144 assert(value);
145 assert(fds);
146
147 if (streq(key, "state")) {
148 TargetState state;
149
1850161f
LP
150 state = target_state_from_string(value);
151 if (state < 0)
e9fa1bf7 152 log_unit_debug(u, "Failed to parse state: %s", value);
a16e1123 153 else
e9fa1bf7 154 t->deserialized_state = state;
a16e1123
LP
155
156 } else
e9fa1bf7 157 log_unit_debug(u, "Unknown serialization key: %s", key);
a16e1123
LP
158
159 return 0;
160}
161
d1e8e8b5 162static UnitActiveState target_active_state(Unit *u) {
e9fa1bf7 163 Target *t = ASSERT_PTR(TARGET(u));
fa068367 164
e9fa1bf7 165 return state_translation_table[t->state];
c22cbe26
LP
166}
167
d1e8e8b5 168static const char *target_sub_state_to_string(Unit *u) {
e9fa1bf7 169 Target *t = ASSERT_PTR(TARGET(u));
10a94420 170
e9fa1bf7 171 return target_state_to_string(t->state);
10a94420
LP
172}
173
87f0e418 174const UnitVTable target_vtable = {
7d17cfbc 175 .object_size = sizeof(Target),
718db961 176
f975e971
LP
177 .sections =
178 "Unit\0"
179 "Target\0"
180 "Install\0",
c22cbe26 181
94d1ddbd
ZJS
182 .can_fail = true,
183
a40eb732 184 .load = target_load,
a16e1123 185 .coldplug = target_coldplug,
fa068367
LP
186
187 .dump = target_dump,
188
189 .start = target_start,
190 .stop = target_stop,
c22cbe26 191
a16e1123
LP
192 .serialize = target_serialize,
193 .deserialize_item = target_deserialize_item,
194
10a94420 195 .active_state = target_active_state,
4139c1b2
LP
196 .sub_state_to_string = target_sub_state_to_string,
197
c6918296
MS
198 .status_message_formats = {
199 .finished_start_job = {
200 [JOB_DONE] = "Reached target %s.",
c6918296
MS
201 },
202 .finished_stop_job = {
203 [JOB_DONE] = "Stopped target %s.",
204 },
205 },
b2d6bb5b
LP
206
207 .notify_supervisor = true,
c22cbe26 208};