1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
26 #include "unit-name.h"
27 #include "dbus-snapshot.h"
29 static const UnitActiveState state_translation_table[_SNAPSHOT_STATE_MAX] = {
30 [SNAPSHOT_DEAD] = UNIT_INACTIVE,
31 [SNAPSHOT_ACTIVE] = UNIT_ACTIVE
34 static void snapshot_set_state(Snapshot *s, SnapshotState state) {
35 SnapshotState old_state;
41 if (state != old_state)
42 log_debug("%s changed %s -> %s",
44 snapshot_state_to_string(old_state),
45 snapshot_state_to_string(state));
47 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
50 static int snapshot_load(Unit *u) {
51 Snapshot *s = SNAPSHOT(u);
54 assert(u->meta.load_state == UNIT_STUB);
56 /* Make sure that only snapshots created via snapshot_create()
58 if (!s->by_snapshot_create)
61 u->meta.load_state = UNIT_LOADED;
65 static int snapshot_coldplug(Unit *u) {
66 Snapshot *s = SNAPSHOT(u);
69 assert(s->state == SNAPSHOT_DEAD);
71 if (s->deserialized_state != s->state)
72 snapshot_set_state(s, s->deserialized_state);
77 static void snapshot_dump(Unit *u, FILE *f, const char *prefix) {
78 Snapshot *s = SNAPSHOT(u);
84 "%sSnapshot State: %s\n"
86 prefix, snapshot_state_to_string(s->state),
87 prefix, yes_no(s->cleanup));
90 static int snapshot_start(Unit *u) {
91 Snapshot *s = SNAPSHOT(u);
94 assert(s->state == SNAPSHOT_DEAD);
96 snapshot_set_state(s, SNAPSHOT_ACTIVE);
99 unit_add_to_cleanup_queue(u);
104 static int snapshot_stop(Unit *u) {
105 Snapshot *s = SNAPSHOT(u);
108 assert(s->state == SNAPSHOT_ACTIVE);
110 snapshot_set_state(s, SNAPSHOT_DEAD);
114 static int snapshot_serialize(Unit *u, FILE *f, FDSet *fds) {
115 Snapshot *s = SNAPSHOT(u);
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);
131 static int snapshot_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
132 Snapshot *s = SNAPSHOT(u);
140 if (streq(key, "state")) {
143 if ((state = snapshot_state_from_string(value)) < 0)
144 log_debug("Failed to parse state value %s", value);
146 s->deserialized_state = state;
148 } else if (streq(key, "cleanup")) {
150 if ((r = parse_boolean(value)) < 0)
151 log_debug("Failed to parse cleanup value %s", value);
155 } else if (streq(key, "requires")) {
157 if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, value, NULL, true)) < 0)
160 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, value, NULL, true)) < 0)
163 log_debug("Unknown serialization key '%s'", key);
168 static UnitActiveState snapshot_active_state(Unit *u) {
171 return state_translation_table[SNAPSHOT(u)->state];
174 static const char *snapshot_sub_state_to_string(Unit *u) {
177 return snapshot_state_to_string(SNAPSHOT(u)->state);
180 int snapshot_create(Manager *m, const char *name, bool cleanup, Snapshot **_s) {
182 Unit *other, *u = NULL;
191 if (!unit_name_is_valid(name))
194 if (unit_name_to_type(name) != UNIT_SNAPSHOT)
197 if (manager_get_unit(m, name))
203 if (asprintf(&n, "snapshot-%u.snapshot", ++ m->n_snapshots) < 0)
206 if (!manager_get_unit(m, n))
215 r = manager_load_unit_prepare(m, name, NULL, &u);
221 SNAPSHOT(u)->by_snapshot_create = true;
222 manager_dispatch_load_queue(m);
223 assert(u->meta.load_state == UNIT_LOADED);
225 HASHMAP_FOREACH_KEY(other, k, m->units, i) {
227 if (UNIT_VTABLE(other)->no_snapshots)
230 if (k != other->meta.id)
233 if (UNIT_VTABLE(other)->check_snapshot)
234 if (!UNIT_VTABLE(other)->check_snapshot(other))
237 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
240 if ((r = unit_add_dependency(u, UNIT_REQUIRES, other, true)) < 0)
243 if ((r = unit_add_dependency(u, UNIT_AFTER, other, true)) < 0)
247 SNAPSHOT(u)->cleanup = cleanup;
254 unit_add_to_cleanup_queue(u);
259 void snapshot_remove(Snapshot *s) {
262 unit_add_to_cleanup_queue(UNIT(s));
265 static const char* const snapshot_state_table[_SNAPSHOT_STATE_MAX] = {
266 [SNAPSHOT_DEAD] = "dead",
267 [SNAPSHOT_ACTIVE] = "active"
270 DEFINE_STRING_TABLE_LOOKUP(snapshot_state, SnapshotState);
272 const UnitVTable snapshot_vtable = {
273 .suffix = ".snapshot",
276 .no_instances = true,
277 .no_snapshots = true,
280 .load = snapshot_load,
281 .coldplug = snapshot_coldplug,
283 .dump = snapshot_dump,
285 .start = snapshot_start,
286 .stop = snapshot_stop,
288 .serialize = snapshot_serialize,
289 .deserialize_item = snapshot_deserialize_item,
291 .active_state = snapshot_active_state,
292 .sub_state_to_string = snapshot_sub_state_to_string,
294 .bus_message_handler = bus_snapshot_message_handler