]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/snapshot.c
unit: add DefaultDependencies= setting
[thirdparty/systemd.git] / src / snapshot.c
CommitLineData
5cb5a6ff
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
a7334b09
LP
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20***/
21
41447faf
LP
22#include <errno.h>
23
87f0e418 24#include "unit.h"
5cb5a6ff 25#include "snapshot.h"
41447faf
LP
26#include "unit-name.h"
27#include "dbus-snapshot.h"
28
29static const UnitActiveState state_translation_table[_SNAPSHOT_STATE_MAX] = {
30 [SNAPSHOT_DEAD] = UNIT_INACTIVE,
31 [SNAPSHOT_ACTIVE] = UNIT_ACTIVE
32};
33
a16e1123
LP
34static void snapshot_set_state(Snapshot *s, SnapshotState state) {
35 SnapshotState old_state;
36 assert(s);
41447faf 37
a16e1123
LP
38 old_state = s->state;
39 s->state = state;
41447faf 40
a16e1123 41 if (state != old_state)
40d50879 42 log_debug("%s changed %s -> %s",
4cd1fbcc 43 s->meta.id,
a16e1123
LP
44 snapshot_state_to_string(old_state),
45 snapshot_state_to_string(state));
5cb5a6ff 46
a16e1123
LP
47 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
48}
41447faf 49
6ec1117a
LP
50static int snapshot_load(Unit *u) {
51 Snapshot *s = SNAPSHOT(u);
52
53 assert(u);
54 assert(u->meta.load_state == UNIT_STUB);
55
56 /* Make sure that only snapshots created via snapshot_create()
57 * can be loaded */
58 if (!s->by_snapshot_create)
59 return -ENOENT;
60
61 u->meta.load_state = UNIT_LOADED;
62 return 0;
63}
64
a16e1123
LP
65static int snapshot_coldplug(Unit *u) {
66 Snapshot *s = SNAPSHOT(u);
41447faf 67
a16e1123
LP
68 assert(s);
69 assert(s->state == SNAPSHOT_DEAD);
41447faf 70
a16e1123
LP
71 if (s->deserialized_state != s->state)
72 snapshot_set_state(s, s->deserialized_state);
41447faf
LP
73
74 return 0;
75}
76
77static void snapshot_dump(Unit *u, FILE *f, const char *prefix) {
87f0e418 78 Snapshot *s = SNAPSHOT(u);
5cb5a6ff
LP
79
80 assert(s);
41447faf 81 assert(f);
5cb5a6ff 82
41447faf
LP
83 fprintf(f,
84 "%sSnapshot State: %s\n"
85 "%sClean Up: %s\n",
a16e1123 86 prefix, snapshot_state_to_string(s->state),
41447faf
LP
87 prefix, yes_no(s->cleanup));
88}
89
41447faf
LP
90static int snapshot_start(Unit *u) {
91 Snapshot *s = SNAPSHOT(u);
92
93 assert(s);
94 assert(s->state == SNAPSHOT_DEAD);
95
96 snapshot_set_state(s, SNAPSHOT_ACTIVE);
97
98 if (s->cleanup)
99 unit_add_to_cleanup_queue(u);
100
101 return 0;
102}
103
104static int snapshot_stop(Unit *u) {
105 Snapshot *s = SNAPSHOT(u);
106
107 assert(s);
108 assert(s->state == SNAPSHOT_ACTIVE);
109
110 snapshot_set_state(s, SNAPSHOT_DEAD);
111 return 0;
5cb5a6ff
LP
112}
113
a16e1123
LP
114static int snapshot_serialize(Unit *u, FILE *f, FDSet *fds) {
115 Snapshot *s = SNAPSHOT(u);
116 Unit *other;
117 Iterator i;
118
119 assert(s);
120 assert(f);
121 assert(fds);
122
123 unit_serialize_item(u, f, "state", snapshot_state_to_string(s->state));
124 unit_serialize_item(u, f, "cleanup", yes_no(s->cleanup));
125 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
126 unit_serialize_item(u, f, "requires", other->meta.id);
127
128 return 0;
129}
130
131static int snapshot_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
132 Snapshot *s = SNAPSHOT(u);
133 int r;
134
135 assert(u);
136 assert(key);
137 assert(value);
138 assert(fds);
139
140 if (streq(key, "state")) {
141 SnapshotState state;
142
143 if ((state = snapshot_state_from_string(value)) < 0)
144 log_debug("Failed to parse state value %s", value);
145 else
146 s->deserialized_state = state;
147
148 } else if (streq(key, "cleanup")) {
149
150 if ((r = parse_boolean(value)) < 0)
151 log_debug("Failed to parse cleanup value %s", value);
152 else
153 s->cleanup = r;
154
155 } else if (streq(key, "requires")) {
156
701cc384 157 if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, value, NULL, true)) < 0)
a16e1123
LP
158 return r;
159
701cc384 160 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, value, NULL, true)) < 0)
a16e1123
LP
161 return r;
162 } else
163 log_debug("Unknown serialization key '%s'", key);
164
165 return 0;
166}
167
87f0e418 168static UnitActiveState snapshot_active_state(Unit *u) {
41447faf
LP
169 assert(u);
170
171 return state_translation_table[SNAPSHOT(u)->state];
172}
173
174static const char *snapshot_sub_state_to_string(Unit *u) {
175 assert(u);
176
a16e1123 177 return snapshot_state_to_string(SNAPSHOT(u)->state);
41447faf
LP
178}
179
180int snapshot_create(Manager *m, const char *name, bool cleanup, Snapshot **_s) {
a16e1123
LP
181 Iterator i;
182 Unit *other, *u = NULL;
41447faf
LP
183 char *n = NULL;
184 int r;
a16e1123 185 const char *k;
41447faf
LP
186
187 assert(m);
188 assert(_s);
189
190 if (name) {
191 if (!unit_name_is_valid(name))
192 return -EINVAL;
193
194 if (unit_name_to_type(name) != UNIT_SNAPSHOT)
195 return -EINVAL;
196
197 if (manager_get_unit(m, name))
198 return -EEXIST;
199
200 } else {
201
202 for (;;) {
203 if (asprintf(&n, "snapshot-%u.snapshot", ++ m->n_snapshots) < 0)
204 return -ENOMEM;
205
206 if (!manager_get_unit(m, n))
207 break;
208
209 free(n);
210 }
211
212 name = n;
213 }
214
6ec1117a 215 r = manager_load_unit_prepare(m, name, NULL, &u);
41447faf
LP
216 free(n);
217
218 if (r < 0)
a16e1123
LP
219 goto fail;
220
6ec1117a
LP
221 SNAPSHOT(u)->by_snapshot_create = true;
222 manager_dispatch_load_queue(m);
223 assert(u->meta.load_state == UNIT_LOADED);
224
a16e1123
LP
225 HASHMAP_FOREACH_KEY(other, k, m->units, i) {
226
227 if (UNIT_VTABLE(other)->no_snapshots)
228 continue;
229
230 if (k != other->meta.id)
231 continue;
232
701cc384
LP
233 if (UNIT_VTABLE(other)->check_snapshot)
234 if (!UNIT_VTABLE(other)->check_snapshot(other))
235 continue;
236
a16e1123
LP
237 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
238 continue;
239
701cc384 240 if ((r = unit_add_dependency(u, UNIT_REQUIRES, other, true)) < 0)
a16e1123
LP
241 goto fail;
242
701cc384 243 if ((r = unit_add_dependency(u, UNIT_AFTER, other, true)) < 0)
a16e1123
LP
244 goto fail;
245 }
41447faf
LP
246
247 SNAPSHOT(u)->cleanup = cleanup;
248 *_s = SNAPSHOT(u);
249
250 return 0;
a16e1123
LP
251
252fail:
253 if (u)
254 unit_add_to_cleanup_queue(u);
255
256 return r;
41447faf
LP
257}
258
259void snapshot_remove(Snapshot *s) {
260 assert(s);
261
262 unit_add_to_cleanup_queue(UNIT(s));
034c6ed7
LP
263}
264
a16e1123
LP
265static const char* const snapshot_state_table[_SNAPSHOT_STATE_MAX] = {
266 [SNAPSHOT_DEAD] = "dead",
267 [SNAPSHOT_ACTIVE] = "active"
268};
269
270DEFINE_STRING_TABLE_LOOKUP(snapshot_state, SnapshotState);
271
87f0e418 272const UnitVTable snapshot_vtable = {
5cb5a6ff
LP
273 .suffix = ".snapshot",
274
41447faf
LP
275 .no_alias = true,
276 .no_instances = true,
277 .no_snapshots = true,
701cc384 278 .no_gc = true,
41447faf 279
6ec1117a 280 .load = snapshot_load,
a16e1123 281 .coldplug = snapshot_coldplug,
41447faf
LP
282
283 .dump = snapshot_dump,
284
285 .start = snapshot_start,
286 .stop = snapshot_stop,
287
a16e1123
LP
288 .serialize = snapshot_serialize,
289 .deserialize_item = snapshot_deserialize_item,
290
41447faf
LP
291 .active_state = snapshot_active_state,
292 .sub_state_to_string = snapshot_sub_state_to_string,
5cb5a6ff 293
41447faf 294 .bus_message_handler = bus_snapshot_message_handler
5cb5a6ff 295};