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"
28 #include "bus-errors.h"
30 static const UnitActiveState state_translation_table
[_SNAPSHOT_STATE_MAX
] = {
31 [SNAPSHOT_DEAD
] = UNIT_INACTIVE
,
32 [SNAPSHOT_ACTIVE
] = UNIT_ACTIVE
35 static void snapshot_set_state(Snapshot
*s
, SnapshotState state
) {
36 SnapshotState old_state
;
42 if (state
!= old_state
)
43 log_debug("%s changed %s -> %s",
45 snapshot_state_to_string(old_state
),
46 snapshot_state_to_string(state
));
48 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
51 static int snapshot_load(Unit
*u
) {
52 Snapshot
*s
= SNAPSHOT(u
);
55 assert(u
->meta
.load_state
== UNIT_STUB
);
57 /* Make sure that only snapshots created via snapshot_create()
59 if (!s
->by_snapshot_create
)
62 u
->meta
.load_state
= UNIT_LOADED
;
66 static int snapshot_coldplug(Unit
*u
) {
67 Snapshot
*s
= SNAPSHOT(u
);
70 assert(s
->state
== SNAPSHOT_DEAD
);
72 if (s
->deserialized_state
!= s
->state
)
73 snapshot_set_state(s
, s
->deserialized_state
);
78 static void snapshot_dump(Unit
*u
, FILE *f
, const char *prefix
) {
79 Snapshot
*s
= SNAPSHOT(u
);
85 "%sSnapshot State: %s\n"
87 prefix
, snapshot_state_to_string(s
->state
),
88 prefix
, yes_no(s
->cleanup
));
91 static int snapshot_start(Unit
*u
) {
92 Snapshot
*s
= SNAPSHOT(u
);
95 assert(s
->state
== SNAPSHOT_DEAD
);
97 snapshot_set_state(s
, SNAPSHOT_ACTIVE
);
100 unit_add_to_cleanup_queue(u
);
105 static int snapshot_stop(Unit
*u
) {
106 Snapshot
*s
= SNAPSHOT(u
);
109 assert(s
->state
== SNAPSHOT_ACTIVE
);
111 snapshot_set_state(s
, SNAPSHOT_DEAD
);
115 static int snapshot_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
116 Snapshot
*s
= SNAPSHOT(u
);
124 unit_serialize_item(u
, f
, "state", snapshot_state_to_string(s
->state
));
125 unit_serialize_item(u
, f
, "cleanup", yes_no(s
->cleanup
));
126 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_WANTS
], i
)
127 unit_serialize_item(u
, f
, "wants", other
->meta
.id
);
132 static int snapshot_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
133 Snapshot
*s
= SNAPSHOT(u
);
141 if (streq(key
, "state")) {
144 if ((state
= snapshot_state_from_string(value
)) < 0)
145 log_debug("Failed to parse state value %s", value
);
147 s
->deserialized_state
= state
;
149 } else if (streq(key
, "cleanup")) {
151 if ((r
= parse_boolean(value
)) < 0)
152 log_debug("Failed to parse cleanup value %s", value
);
156 } else if (streq(key
, "wants")) {
158 if ((r
= unit_add_two_dependencies_by_name(u
, UNIT_AFTER
, UNIT_WANTS
, value
, NULL
, true)) < 0)
161 log_debug("Unknown serialization key '%s'", key
);
166 static UnitActiveState
snapshot_active_state(Unit
*u
) {
169 return state_translation_table
[SNAPSHOT(u
)->state
];
172 static const char *snapshot_sub_state_to_string(Unit
*u
) {
175 return snapshot_state_to_string(SNAPSHOT(u
)->state
);
178 int snapshot_create(Manager
*m
, const char *name
, bool cleanup
, DBusError
*e
, Snapshot
**_s
) {
180 Unit
*other
, *u
= NULL
;
189 if (!unit_name_is_valid(name
)) {
190 dbus_set_error(e
, BUS_ERROR_INVALID_NAME
, "Unit name %s is not valid.", name
);
194 if (unit_name_to_type(name
) != UNIT_SNAPSHOT
) {
195 dbus_set_error(e
, BUS_ERROR_UNIT_TYPE_MISMATCH
, "Unit name %s lacks snapshot suffix.", name
);
199 if (manager_get_unit(m
, name
)) {
200 dbus_set_error(e
, BUS_ERROR_UNIT_EXISTS
, "Snapshot %s exists already.", name
);
207 if (asprintf(&n
, "snapshot-%u.snapshot", ++ m
->n_snapshots
) < 0)
210 if (!manager_get_unit(m
, n
))
219 r
= manager_load_unit_prepare(m
, name
, NULL
, e
, &u
);
225 SNAPSHOT(u
)->by_snapshot_create
= true;
226 manager_dispatch_load_queue(m
);
227 assert(u
->meta
.load_state
== UNIT_LOADED
);
229 HASHMAP_FOREACH_KEY(other
, k
, m
->units
, i
) {
231 if (UNIT_VTABLE(other
)->no_snapshots
)
234 if (k
!= other
->meta
.id
)
237 if (UNIT_VTABLE(other
)->check_snapshot
)
238 if (!UNIT_VTABLE(other
)->check_snapshot(other
))
241 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other
)))
244 if ((r
= unit_add_two_dependencies(u
, UNIT_AFTER
, UNIT_WANTS
, other
, true)) < 0)
248 SNAPSHOT(u
)->cleanup
= cleanup
;
255 unit_add_to_cleanup_queue(u
);
260 void snapshot_remove(Snapshot
*s
) {
263 unit_add_to_cleanup_queue(UNIT(s
));
266 static const char* const snapshot_state_table
[_SNAPSHOT_STATE_MAX
] = {
267 [SNAPSHOT_DEAD
] = "dead",
268 [SNAPSHOT_ACTIVE
] = "active"
271 DEFINE_STRING_TABLE_LOOKUP(snapshot_state
, SnapshotState
);
273 const UnitVTable snapshot_vtable
= {
274 .suffix
= ".snapshot",
277 .no_instances
= true,
278 .no_snapshots
= true,
281 .load
= snapshot_load
,
282 .coldplug
= snapshot_coldplug
,
284 .dump
= snapshot_dump
,
286 .start
= snapshot_start
,
287 .stop
= snapshot_stop
,
289 .serialize
= snapshot_serialize
,
290 .deserialize_item
= snapshot_deserialize_item
,
292 .active_state
= snapshot_active_state
,
293 .sub_state_to_string
= snapshot_sub_state_to_string
,
295 .bus_message_handler
= bus_snapshot_message_handler