]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/target.c
765c1f3fa48c9f17eae3a7eb74c62793a63777ad
[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 Iterator i;
60 Unit *other;
61 int r;
62 unsigned k;
63
64 assert(t);
65
66 /* Imply ordering for requirement dependencies on target
67 * units. Note that when the user created a contradicting
68 * ordering manually we won't add anything in here to make
69 * sure we don't create a loop. */
70
71 for (k = 0; k < ELEMENTSOF(deps); k++)
72 SET_FOREACH(other, UNIT(t)->dependencies[deps[k]], i) {
73 r = unit_add_default_target_dependency(other, UNIT(t));
74 if (r < 0)
75 return r;
76 }
77
78 /* Make sure targets are unloaded on shutdown */
79 return unit_add_dependency_by_name(UNIT(t), UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
80 }
81
82 static int target_load(Unit *u) {
83 Target *t = TARGET(u);
84 int r;
85
86 assert(t);
87
88 r = unit_load_fragment_and_dropin(u);
89 if (r < 0)
90 return r;
91
92 /* This is a new unit? Then let's add in some extras */
93 if (u->load_state == UNIT_LOADED && u->default_dependencies) {
94 r = target_add_default_dependencies(t);
95 if (r < 0)
96 return r;
97 }
98
99 return 0;
100 }
101
102 static int target_coldplug(Unit *u) {
103 Target *t = TARGET(u);
104
105 assert(t);
106 assert(t->state == TARGET_DEAD);
107
108 if (t->deserialized_state != t->state)
109 target_set_state(t, t->deserialized_state);
110
111 return 0;
112 }
113
114 static void target_dump(Unit *u, FILE *f, const char *prefix) {
115 Target *t = TARGET(u);
116
117 assert(t);
118 assert(f);
119
120 fprintf(f,
121 "%sTarget State: %s\n",
122 prefix, target_state_to_string(t->state));
123 }
124
125 static int target_start(Unit *u) {
126 Target *t = TARGET(u);
127 int r;
128
129 assert(t);
130 assert(t->state == TARGET_DEAD);
131
132 r = unit_acquire_invocation_id(u);
133 if (r < 0)
134 return r;
135
136 target_set_state(t, TARGET_ACTIVE);
137 return 1;
138 }
139
140 static int target_stop(Unit *u) {
141 Target *t = TARGET(u);
142
143 assert(t);
144 assert(t->state == TARGET_ACTIVE);
145
146 target_set_state(t, TARGET_DEAD);
147 return 1;
148 }
149
150 static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
151 Target *s = TARGET(u);
152
153 assert(s);
154 assert(f);
155 assert(fds);
156
157 unit_serialize_item(u, f, "state", target_state_to_string(s->state));
158 return 0;
159 }
160
161 static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
162 Target *s = TARGET(u);
163
164 assert(u);
165 assert(key);
166 assert(value);
167 assert(fds);
168
169 if (streq(key, "state")) {
170 TargetState state;
171
172 state = target_state_from_string(value);
173 if (state < 0)
174 log_debug("Failed to parse state value %s", value);
175 else
176 s->deserialized_state = state;
177
178 } else
179 log_debug("Unknown serialization key '%s'", key);
180
181 return 0;
182 }
183
184 _pure_ static UnitActiveState target_active_state(Unit *u) {
185 assert(u);
186
187 return state_translation_table[TARGET(u)->state];
188 }
189
190 _pure_ static const char *target_sub_state_to_string(Unit *u) {
191 assert(u);
192
193 return target_state_to_string(TARGET(u)->state);
194 }
195
196 const UnitVTable target_vtable = {
197 .object_size = sizeof(Target),
198
199 .sections =
200 "Unit\0"
201 "Target\0"
202 "Install\0",
203
204 .load = target_load,
205 .coldplug = target_coldplug,
206
207 .dump = target_dump,
208
209 .start = target_start,
210 .stop = target_stop,
211
212 .serialize = target_serialize,
213 .deserialize_item = target_deserialize_item,
214
215 .active_state = target_active_state,
216 .sub_state_to_string = target_sub_state_to_string,
217
218 .bus_vtable = bus_target_vtable,
219
220 .status_message_formats = {
221 .finished_start_job = {
222 [JOB_DONE] = "Reached target %s.",
223 },
224 .finished_stop_job = {
225 [JOB_DONE] = "Stopped target %s.",
226 },
227 },
228 };