]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/target.c
Merge pull request #7301 from poettering/loginctl-ellipsize
[thirdparty/systemd.git] / src / core / target.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "dbus-target.h"
21 #include "log.h"
22 #include "special.h"
23 #include "string-util.h"
24 #include "unit-name.h"
25 #include "unit.h"
26 #include "target.h"
27
28 static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = {
29 [TARGET_DEAD] = UNIT_INACTIVE,
30 [TARGET_ACTIVE] = UNIT_ACTIVE
31 };
32
33 static void target_set_state(Target *t, TargetState state) {
34 TargetState old_state;
35 assert(t);
36
37 old_state = t->state;
38 t->state = state;
39
40 if (state != old_state)
41 log_debug("%s changed %s -> %s",
42 UNIT(t)->id,
43 target_state_to_string(old_state),
44 target_state_to_string(state));
45
46 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], true);
47 }
48
49 static int target_add_default_dependencies(Target *t) {
50
51 static const UnitDependency deps[] = {
52 UNIT_REQUIRES,
53 UNIT_REQUISITE,
54 UNIT_WANTS,
55 UNIT_BINDS_TO,
56 UNIT_PART_OF
57 };
58
59 int r;
60 unsigned k;
61
62 assert(t);
63
64 if (!UNIT(t)->default_dependencies)
65 return 0;
66
67 /* Imply ordering for requirement dependencies on target units. Note that when the user created a contradicting
68 * ordering manually we won't add anything in here to make sure we don't create a loop. */
69
70 for (k = 0; k < ELEMENTSOF(deps); k++) {
71 Unit *other;
72 Iterator i;
73 void *v;
74
75 HASHMAP_FOREACH_KEY(v, other, UNIT(t)->dependencies[deps[k]], i) {
76 r = unit_add_default_target_dependency(other, UNIT(t));
77 if (r < 0)
78 return r;
79 }
80 }
81
82 if (unit_has_name(UNIT(t), SPECIAL_SHUTDOWN_TARGET))
83 return 0;
84
85 /* Make sure targets are unloaded on shutdown */
86 return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true, UNIT_DEPENDENCY_DEFAULT);
87 }
88
89 static int target_load(Unit *u) {
90 Target *t = TARGET(u);
91 int r;
92
93 assert(t);
94
95 r = unit_load_fragment_and_dropin(u);
96 if (r < 0)
97 return r;
98
99 /* This is a new unit? Then let's add in some extras */
100 if (u->load_state == UNIT_LOADED) {
101 r = target_add_default_dependencies(t);
102 if (r < 0)
103 return r;
104 }
105
106 return 0;
107 }
108
109 static int target_coldplug(Unit *u) {
110 Target *t = TARGET(u);
111
112 assert(t);
113 assert(t->state == TARGET_DEAD);
114
115 if (t->deserialized_state != t->state)
116 target_set_state(t, t->deserialized_state);
117
118 return 0;
119 }
120
121 static void target_dump(Unit *u, FILE *f, const char *prefix) {
122 Target *t = TARGET(u);
123
124 assert(t);
125 assert(f);
126
127 fprintf(f,
128 "%sTarget State: %s\n",
129 prefix, target_state_to_string(t->state));
130 }
131
132 static int target_start(Unit *u) {
133 Target *t = TARGET(u);
134 int r;
135
136 assert(t);
137 assert(t->state == TARGET_DEAD);
138
139 r = unit_acquire_invocation_id(u);
140 if (r < 0)
141 return r;
142
143 target_set_state(t, TARGET_ACTIVE);
144 return 1;
145 }
146
147 static int target_stop(Unit *u) {
148 Target *t = TARGET(u);
149
150 assert(t);
151 assert(t->state == TARGET_ACTIVE);
152
153 target_set_state(t, TARGET_DEAD);
154 return 1;
155 }
156
157 static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
158 Target *s = TARGET(u);
159
160 assert(s);
161 assert(f);
162 assert(fds);
163
164 unit_serialize_item(u, f, "state", target_state_to_string(s->state));
165 return 0;
166 }
167
168 static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
169 Target *s = TARGET(u);
170
171 assert(u);
172 assert(key);
173 assert(value);
174 assert(fds);
175
176 if (streq(key, "state")) {
177 TargetState state;
178
179 state = target_state_from_string(value);
180 if (state < 0)
181 log_debug("Failed to parse state value %s", value);
182 else
183 s->deserialized_state = state;
184
185 } else
186 log_debug("Unknown serialization key '%s'", key);
187
188 return 0;
189 }
190
191 _pure_ static UnitActiveState target_active_state(Unit *u) {
192 assert(u);
193
194 return state_translation_table[TARGET(u)->state];
195 }
196
197 _pure_ static const char *target_sub_state_to_string(Unit *u) {
198 assert(u);
199
200 return target_state_to_string(TARGET(u)->state);
201 }
202
203 const UnitVTable target_vtable = {
204 .object_size = sizeof(Target),
205
206 .sections =
207 "Unit\0"
208 "Target\0"
209 "Install\0",
210
211 .load = target_load,
212 .coldplug = target_coldplug,
213
214 .dump = target_dump,
215
216 .start = target_start,
217 .stop = target_stop,
218
219 .serialize = target_serialize,
220 .deserialize_item = target_deserialize_item,
221
222 .active_state = target_active_state,
223 .sub_state_to_string = target_sub_state_to_string,
224
225 .bus_vtable = bus_target_vtable,
226
227 .status_message_formats = {
228 .finished_start_job = {
229 [JOB_DONE] = "Reached target %s.",
230 },
231 .finished_stop_job = {
232 [JOB_DONE] = "Stopped target %s.",
233 },
234 },
235 };