]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/target.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / core / target.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
a7334b09 2/***
a7334b09 3 Copyright 2010 Lennart Poettering
a7334b09
LP
4***/
5
4139c1b2 6#include "dbus-target.h"
07630cea 7#include "log.h"
514f4ef5 8#include "special.h"
07630cea 9#include "string-util.h"
b2bb3dbe 10#include "unit-name.h"
07630cea
LP
11#include "unit.h"
12#include "target.h"
c22cbe26 13
fa068367
LP
14static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = {
15 [TARGET_DEAD] = UNIT_INACTIVE,
16 [TARGET_ACTIVE] = UNIT_ACTIVE
17};
18
fa068367
LP
19static void target_set_state(Target *t, TargetState state) {
20 TargetState old_state;
21 assert(t);
22
23 old_state = t->state;
24 t->state = state;
25
e537352b 26 if (state != old_state)
40d50879 27 log_debug("%s changed %s -> %s",
1124fe6f 28 UNIT(t)->id,
a16e1123
LP
29 target_state_to_string(old_state),
30 target_state_to_string(state));
c22cbe26 31
2ad2e41a 32 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], 0);
fa068367
LP
33}
34
a40eb732 35static int target_add_default_dependencies(Target *t) {
1850161f 36
21256a2b
LP
37 static const UnitDependency deps[] = {
38 UNIT_REQUIRES,
21256a2b 39 UNIT_REQUISITE,
21256a2b 40 UNIT_WANTS,
1850161f
LP
41 UNIT_BINDS_TO,
42 UNIT_PART_OF
21256a2b
LP
43 };
44
bba34eed 45 int r;
21256a2b 46 unsigned k;
bba34eed 47
98bc2000 48 assert(t);
a40eb732 49
41c237af
IP
50 if (!UNIT(t)->default_dependencies)
51 return 0;
52
eef85c4a
LP
53 /* Imply ordering for requirement dependencies on target units. Note that when the user created a contradicting
54 * ordering manually we won't add anything in here to make sure we don't create a loop. */
bba34eed 55
eef85c4a
LP
56 for (k = 0; k < ELEMENTSOF(deps); k++) {
57 Unit *other;
58 Iterator i;
59 void *v;
60
61 HASHMAP_FOREACH_KEY(v, other, UNIT(t)->dependencies[deps[k]], i) {
1850161f
LP
62 r = unit_add_default_target_dependency(other, UNIT(t));
63 if (r < 0)
21256a2b 64 return r;
1850161f 65 }
eef85c4a 66 }
bba34eed 67
33e28180
LP
68 if (unit_has_name(UNIT(t), SPECIAL_SHUTDOWN_TARGET))
69 return 0;
70
b401e1fb 71 /* Make sure targets are unloaded on shutdown */
eef85c4a 72 return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true, UNIT_DEPENDENCY_DEFAULT);
a40eb732
LP
73}
74
75static int target_load(Unit *u) {
76 Target *t = TARGET(u);
77 int r;
78
79 assert(t);
80
1850161f
LP
81 r = unit_load_fragment_and_dropin(u);
82 if (r < 0)
a40eb732
LP
83 return r;
84
85 /* This is a new unit? Then let's add in some extras */
41c237af 86 if (u->load_state == UNIT_LOADED) {
1850161f
LP
87 r = target_add_default_dependencies(t);
88 if (r < 0)
89 return r;
a40eb732
LP
90 }
91
92 return 0;
93}
94
be847e82 95static int target_coldplug(Unit *u) {
a16e1123
LP
96 Target *t = TARGET(u);
97
98 assert(t);
99 assert(t->state == TARGET_DEAD);
100
101 if (t->deserialized_state != t->state)
102 target_set_state(t, t->deserialized_state);
103
104 return 0;
105}
106
107static void target_dump(Unit *u, FILE *f, const char *prefix) {
108 Target *t = TARGET(u);
109
110 assert(t);
111 assert(f);
112
113 fprintf(f,
114 "%sTarget State: %s\n",
115 prefix, target_state_to_string(t->state));
116}
117
fa068367
LP
118static int target_start(Unit *u) {
119 Target *t = TARGET(u);
4b58153d 120 int r;
fa068367
LP
121
122 assert(t);
123 assert(t->state == TARGET_DEAD);
124
4b58153d
LP
125 r = unit_acquire_invocation_id(u);
126 if (r < 0)
127 return r;
128
fa068367 129 target_set_state(t, TARGET_ACTIVE);
82a2b6bb 130 return 1;
fa068367 131}
c22cbe26 132
fa068367
LP
133static int target_stop(Unit *u) {
134 Target *t = TARGET(u);
135
136 assert(t);
137 assert(t->state == TARGET_ACTIVE);
138
139 target_set_state(t, TARGET_DEAD);
82a2b6bb 140 return 1;
c22cbe26
LP
141}
142
a16e1123
LP
143static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
144 Target *s = TARGET(u);
145
146 assert(s);
147 assert(f);
148 assert(fds);
149
150 unit_serialize_item(u, f, "state", target_state_to_string(s->state));
151 return 0;
152}
153
154static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
155 Target *s = TARGET(u);
156
157 assert(u);
158 assert(key);
159 assert(value);
160 assert(fds);
161
162 if (streq(key, "state")) {
163 TargetState state;
164
1850161f
LP
165 state = target_state_from_string(value);
166 if (state < 0)
a16e1123
LP
167 log_debug("Failed to parse state value %s", value);
168 else
169 s->deserialized_state = state;
170
171 } else
172 log_debug("Unknown serialization key '%s'", key);
173
174 return 0;
175}
176
44a6b1b6 177_pure_ static UnitActiveState target_active_state(Unit *u) {
fa068367
LP
178 assert(u);
179
180 return state_translation_table[TARGET(u)->state];
c22cbe26
LP
181}
182
44a6b1b6 183_pure_ static const char *target_sub_state_to_string(Unit *u) {
10a94420
LP
184 assert(u);
185
a16e1123 186 return target_state_to_string(TARGET(u)->state);
10a94420
LP
187}
188
87f0e418 189const UnitVTable target_vtable = {
7d17cfbc 190 .object_size = sizeof(Target),
718db961 191
f975e971
LP
192 .sections =
193 "Unit\0"
194 "Target\0"
195 "Install\0",
c22cbe26 196
a40eb732 197 .load = target_load,
a16e1123 198 .coldplug = target_coldplug,
fa068367
LP
199
200 .dump = target_dump,
201
202 .start = target_start,
203 .stop = target_stop,
c22cbe26 204
a16e1123
LP
205 .serialize = target_serialize,
206 .deserialize_item = target_deserialize_item,
207
10a94420 208 .active_state = target_active_state,
4139c1b2
LP
209 .sub_state_to_string = target_sub_state_to_string,
210
718db961 211 .bus_vtable = bus_target_vtable,
c6918296
MS
212
213 .status_message_formats = {
214 .finished_start_job = {
215 [JOB_DONE] = "Reached target %s.",
c6918296
MS
216 },
217 .finished_stop_job = {
218 [JOB_DONE] = "Stopped target %s.",
219 },
220 },
c22cbe26 221};