]> git.ipfire.org Git - people/ms/systemd.git/blob - snapshot.c
device: allow easy identification of network interfaces without their full sysfs...
[people/ms/systemd.git] / snapshot.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
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
22 #include <errno.h>
23
24 #include "unit.h"
25 #include "snapshot.h"
26 #include "unit-name.h"
27 #include "dbus-snapshot.h"
28
29 static const UnitActiveState state_translation_table[_SNAPSHOT_STATE_MAX] = {
30 [SNAPSHOT_DEAD] = UNIT_INACTIVE,
31 [SNAPSHOT_ACTIVE] = UNIT_ACTIVE
32 };
33
34 static void snapshot_set_state(Snapshot *s, SnapshotState state) {
35 SnapshotState old_state;
36 assert(s);
37
38 old_state = s->state;
39 s->state = state;
40
41 if (state != old_state)
42 log_debug("%s changed %s -> %s",
43 UNIT(s)->meta.id,
44 snapshot_state_to_string(old_state),
45 snapshot_state_to_string(state));
46
47 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
48 }
49
50 static int snapshot_coldplug(Unit *u) {
51 Snapshot *s = SNAPSHOT(u);
52
53 assert(s);
54 assert(s->state == SNAPSHOT_DEAD);
55
56 if (s->deserialized_state != s->state)
57 snapshot_set_state(s, s->deserialized_state);
58
59 return 0;
60 }
61
62 static void snapshot_dump(Unit *u, FILE *f, const char *prefix) {
63 Snapshot *s = SNAPSHOT(u);
64
65 assert(s);
66 assert(f);
67
68 fprintf(f,
69 "%sSnapshot State: %s\n"
70 "%sClean Up: %s\n",
71 prefix, snapshot_state_to_string(s->state),
72 prefix, yes_no(s->cleanup));
73 }
74
75 static 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
89 static 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;
97 }
98
99 static 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
116 static 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
142 if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, value, NULL, true)) < 0)
143 return r;
144
145 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, value, NULL, true)) < 0)
146 return r;
147 } else
148 log_debug("Unknown serialization key '%s'", key);
149
150 return 0;
151 }
152
153 static UnitActiveState snapshot_active_state(Unit *u) {
154 assert(u);
155
156 return state_translation_table[SNAPSHOT(u)->state];
157 }
158
159 static const char *snapshot_sub_state_to_string(Unit *u) {
160 assert(u);
161
162 return snapshot_state_to_string(SNAPSHOT(u)->state);
163 }
164
165 int snapshot_create(Manager *m, const char *name, bool cleanup, Snapshot **_s) {
166 Iterator i;
167 Unit *other, *u = NULL;
168 char *n = NULL;
169 int r;
170 const char *k;
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)
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
214 if (UNIT_VTABLE(other)->check_snapshot)
215 if (!UNIT_VTABLE(other)->check_snapshot(other))
216 continue;
217
218 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
219 continue;
220
221 if ((r = unit_add_dependency(u, UNIT_REQUIRES, other, true)) < 0)
222 goto fail;
223
224 if ((r = unit_add_dependency(u, UNIT_AFTER, other, true)) < 0)
225 goto fail;
226 }
227
228 SNAPSHOT(u)->cleanup = cleanup;
229 *_s = SNAPSHOT(u);
230
231 return 0;
232
233 fail:
234 if (u)
235 unit_add_to_cleanup_queue(u);
236
237 return r;
238 }
239
240 void snapshot_remove(Snapshot *s) {
241 assert(s);
242
243 unit_add_to_cleanup_queue(UNIT(s));
244 }
245
246 static const char* const snapshot_state_table[_SNAPSHOT_STATE_MAX] = {
247 [SNAPSHOT_DEAD] = "dead",
248 [SNAPSHOT_ACTIVE] = "active"
249 };
250
251 DEFINE_STRING_TABLE_LOOKUP(snapshot_state, SnapshotState);
252
253 const UnitVTable snapshot_vtable = {
254 .suffix = ".snapshot",
255
256 .no_alias = true,
257 .no_instances = true,
258 .no_snapshots = true,
259 .no_gc = true,
260
261 .load = unit_load_nop,
262 .coldplug = snapshot_coldplug,
263
264 .dump = snapshot_dump,
265
266 .start = snapshot_start,
267 .stop = snapshot_stop,
268
269 .serialize = snapshot_serialize,
270 .deserialize_item = snapshot_deserialize_item,
271
272 .active_state = snapshot_active_state,
273 .sub_state_to_string = snapshot_sub_state_to_string,
274
275 .bus_message_handler = bus_snapshot_message_handler
276 };