]> git.ipfire.org Git - people/ms/systemd.git/blame - snapshot.c
Remove .h files from _SOURCES
[people/ms/systemd.git] / 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",
a16e1123
LP
43 UNIT(s)->meta.id,
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
a16e1123
LP
50static int snapshot_coldplug(Unit *u) {
51 Snapshot *s = SNAPSHOT(u);
41447faf 52
a16e1123
LP
53 assert(s);
54 assert(s->state == SNAPSHOT_DEAD);
41447faf 55
a16e1123
LP
56 if (s->deserialized_state != s->state)
57 snapshot_set_state(s, s->deserialized_state);
41447faf
LP
58
59 return 0;
60}
61
62static void snapshot_dump(Unit *u, FILE *f, const char *prefix) {
87f0e418 63 Snapshot *s = SNAPSHOT(u);
5cb5a6ff
LP
64
65 assert(s);
41447faf 66 assert(f);
5cb5a6ff 67
41447faf
LP
68 fprintf(f,
69 "%sSnapshot State: %s\n"
70 "%sClean Up: %s\n",
a16e1123 71 prefix, snapshot_state_to_string(s->state),
41447faf
LP
72 prefix, yes_no(s->cleanup));
73}
74
41447faf
LP
75static int snapshot_start(Unit *u) {
76 Snapshot *s = SNAPSHOT(u);
77
78 assert(s);
79 assert(s->state == SNAPSHOT_DEAD);
80
81 snapshot_set_state(s, SNAPSHOT_ACTIVE);
82
83 if (s->cleanup)
84 unit_add_to_cleanup_queue(u);
85
86 return 0;
87}
88
89static int snapshot_stop(Unit *u) {
90 Snapshot *s = SNAPSHOT(u);
91
92 assert(s);
93 assert(s->state == SNAPSHOT_ACTIVE);
94
95 snapshot_set_state(s, SNAPSHOT_DEAD);
96 return 0;
5cb5a6ff
LP
97}
98
a16e1123
LP
99static int snapshot_serialize(Unit *u, FILE *f, FDSet *fds) {
100 Snapshot *s = SNAPSHOT(u);
101 Unit *other;
102 Iterator i;
103
104 assert(s);
105 assert(f);
106 assert(fds);
107
108 unit_serialize_item(u, f, "state", snapshot_state_to_string(s->state));
109 unit_serialize_item(u, f, "cleanup", yes_no(s->cleanup));
110 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
111 unit_serialize_item(u, f, "requires", other->meta.id);
112
113 return 0;
114}
115
116static int snapshot_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
117 Snapshot *s = SNAPSHOT(u);
118 int r;
119
120 assert(u);
121 assert(key);
122 assert(value);
123 assert(fds);
124
125 if (streq(key, "state")) {
126 SnapshotState state;
127
128 if ((state = snapshot_state_from_string(value)) < 0)
129 log_debug("Failed to parse state value %s", value);
130 else
131 s->deserialized_state = state;
132
133 } else if (streq(key, "cleanup")) {
134
135 if ((r = parse_boolean(value)) < 0)
136 log_debug("Failed to parse cleanup value %s", value);
137 else
138 s->cleanup = r;
139
140 } else if (streq(key, "requires")) {
141
701cc384 142 if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, value, NULL, true)) < 0)
a16e1123
LP
143 return r;
144
701cc384 145 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, value, NULL, true)) < 0)
a16e1123
LP
146 return r;
147 } else
148 log_debug("Unknown serialization key '%s'", key);
149
150 return 0;
151}
152
87f0e418 153static UnitActiveState snapshot_active_state(Unit *u) {
41447faf
LP
154 assert(u);
155
156 return state_translation_table[SNAPSHOT(u)->state];
157}
158
159static const char *snapshot_sub_state_to_string(Unit *u) {
160 assert(u);
161
a16e1123 162 return snapshot_state_to_string(SNAPSHOT(u)->state);
41447faf
LP
163}
164
165int snapshot_create(Manager *m, const char *name, bool cleanup, Snapshot **_s) {
a16e1123
LP
166 Iterator i;
167 Unit *other, *u = NULL;
41447faf
LP
168 char *n = NULL;
169 int r;
a16e1123 170 const char *k;
41447faf
LP
171
172 assert(m);
173 assert(_s);
174
175 if (name) {
176 if (!unit_name_is_valid(name))
177 return -EINVAL;
178
179 if (unit_name_to_type(name) != UNIT_SNAPSHOT)
180 return -EINVAL;
181
182 if (manager_get_unit(m, name))
183 return -EEXIST;
184
185 } else {
186
187 for (;;) {
188 if (asprintf(&n, "snapshot-%u.snapshot", ++ m->n_snapshots) < 0)
189 return -ENOMEM;
190
191 if (!manager_get_unit(m, n))
192 break;
193
194 free(n);
195 }
196
197 name = n;
198 }
199
200 r = manager_load_unit(m, name, NULL, &u);
201 free(n);
202
203 if (r < 0)
a16e1123
LP
204 goto fail;
205
206 HASHMAP_FOREACH_KEY(other, k, m->units, i) {
207
208 if (UNIT_VTABLE(other)->no_snapshots)
209 continue;
210
211 if (k != other->meta.id)
212 continue;
213
701cc384
LP
214 if (UNIT_VTABLE(other)->check_snapshot)
215 if (!UNIT_VTABLE(other)->check_snapshot(other))
216 continue;
217
a16e1123
LP
218 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
219 continue;
220
701cc384 221 if ((r = unit_add_dependency(u, UNIT_REQUIRES, other, true)) < 0)
a16e1123
LP
222 goto fail;
223
701cc384 224 if ((r = unit_add_dependency(u, UNIT_AFTER, other, true)) < 0)
a16e1123
LP
225 goto fail;
226 }
41447faf
LP
227
228 SNAPSHOT(u)->cleanup = cleanup;
229 *_s = SNAPSHOT(u);
230
231 return 0;
a16e1123
LP
232
233fail:
234 if (u)
235 unit_add_to_cleanup_queue(u);
236
237 return r;
41447faf
LP
238}
239
240void snapshot_remove(Snapshot *s) {
241 assert(s);
242
243 unit_add_to_cleanup_queue(UNIT(s));
034c6ed7
LP
244}
245
a16e1123
LP
246static const char* const snapshot_state_table[_SNAPSHOT_STATE_MAX] = {
247 [SNAPSHOT_DEAD] = "dead",
248 [SNAPSHOT_ACTIVE] = "active"
249};
250
251DEFINE_STRING_TABLE_LOOKUP(snapshot_state, SnapshotState);
252
87f0e418 253const UnitVTable snapshot_vtable = {
5cb5a6ff
LP
254 .suffix = ".snapshot",
255
41447faf
LP
256 .no_alias = true,
257 .no_instances = true,
258 .no_snapshots = true,
701cc384 259 .no_gc = true,
41447faf 260
a16e1123
LP
261 .load = unit_load_nop,
262 .coldplug = snapshot_coldplug,
41447faf
LP
263
264 .dump = snapshot_dump,
265
266 .start = snapshot_start,
267 .stop = snapshot_stop,
268
a16e1123
LP
269 .serialize = snapshot_serialize,
270 .deserialize_item = snapshot_deserialize_item,
271
41447faf
LP
272 .active_state = snapshot_active_state,
273 .sub_state_to_string = snapshot_sub_state_to_string,
5cb5a6ff 274
41447faf 275 .bus_message_handler = bus_snapshot_message_handler
5cb5a6ff 276};