1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "bus-common-errors.h"
25 #include "dbus-snapshot.h"
26 #include "string-util.h"
27 #include "unit-name.h"
31 static const UnitActiveState state_translation_table
[_SNAPSHOT_STATE_MAX
] = {
32 [SNAPSHOT_DEAD
] = UNIT_INACTIVE
,
33 [SNAPSHOT_ACTIVE
] = UNIT_ACTIVE
36 static void snapshot_init(Unit
*u
) {
37 Snapshot
*s
= SNAPSHOT(u
);
40 assert(UNIT(s
)->load_state
== UNIT_STUB
);
42 UNIT(s
)->ignore_on_isolate
= true;
43 UNIT(s
)->ignore_on_snapshot
= true;
44 UNIT(s
)->allow_isolate
= true;
47 static void snapshot_set_state(Snapshot
*s
, SnapshotState state
) {
48 SnapshotState old_state
;
54 if (state
!= old_state
)
55 log_unit_debug(UNIT(s
), "Changed %s -> %s", snapshot_state_to_string(old_state
), snapshot_state_to_string(state
));
57 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], true);
60 static int snapshot_load(Unit
*u
) {
61 Snapshot
*s
= SNAPSHOT(u
);
64 assert(u
->load_state
== UNIT_STUB
);
66 /* Make sure that only snapshots created via snapshot_create()
68 if (!u
->transient
&& UNIT(s
)->manager
->n_reloading
<= 0)
71 u
->load_state
= UNIT_LOADED
;
75 static int snapshot_coldplug(Unit
*u
) {
76 Snapshot
*s
= SNAPSHOT(u
);
79 assert(s
->state
== SNAPSHOT_DEAD
);
81 if (s
->deserialized_state
!= s
->state
)
82 snapshot_set_state(s
, s
->deserialized_state
);
87 static void snapshot_dump(Unit
*u
, FILE *f
, const char *prefix
) {
88 Snapshot
*s
= SNAPSHOT(u
);
94 "%sSnapshot State: %s\n"
96 prefix
, snapshot_state_to_string(s
->state
),
97 prefix
, yes_no(s
->cleanup
));
100 static int snapshot_start(Unit
*u
) {
101 Snapshot
*s
= SNAPSHOT(u
);
104 assert(s
->state
== SNAPSHOT_DEAD
);
106 snapshot_set_state(s
, SNAPSHOT_ACTIVE
);
109 unit_add_to_cleanup_queue(u
);
114 static int snapshot_stop(Unit
*u
) {
115 Snapshot
*s
= SNAPSHOT(u
);
118 assert(s
->state
== SNAPSHOT_ACTIVE
);
120 snapshot_set_state(s
, SNAPSHOT_DEAD
);
124 static int snapshot_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
125 Snapshot
*s
= SNAPSHOT(u
);
133 unit_serialize_item(u
, f
, "state", snapshot_state_to_string(s
->state
));
134 unit_serialize_item(u
, f
, "cleanup", yes_no(s
->cleanup
));
135 SET_FOREACH(other
, u
->dependencies
[UNIT_WANTS
], i
)
136 unit_serialize_item(u
, f
, "wants", other
->id
);
141 static int snapshot_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
142 Snapshot
*s
= SNAPSHOT(u
);
150 if (streq(key
, "state")) {
153 state
= snapshot_state_from_string(value
);
155 log_unit_debug(u
, "Failed to parse state value: %s", value
);
157 s
->deserialized_state
= state
;
159 } else if (streq(key
, "cleanup")) {
161 r
= parse_boolean(value
);
163 log_unit_debug(u
, "Failed to parse cleanup value: %s", value
);
167 } else if (streq(key
, "wants")) {
169 r
= unit_add_two_dependencies_by_name(u
, UNIT_AFTER
, UNIT_WANTS
, value
, NULL
, true);
173 log_unit_debug(u
, "Unknown serialization key: %s", key
);
178 _pure_
static UnitActiveState
snapshot_active_state(Unit
*u
) {
181 return state_translation_table
[SNAPSHOT(u
)->state
];
184 _pure_
static const char *snapshot_sub_state_to_string(Unit
*u
) {
187 return snapshot_state_to_string(SNAPSHOT(u
)->state
);
190 int snapshot_create(Manager
*m
, const char *name
, bool cleanup
, sd_bus_error
*e
, Snapshot
**_s
) {
191 _cleanup_free_
char *n
= NULL
;
192 Unit
*other
, *u
= NULL
;
201 if (!unit_name_is_valid(name
, UNIT_NAME_PLAIN
))
202 return sd_bus_error_setf(e
, SD_BUS_ERROR_INVALID_ARGS
, "Unit name %s is not valid.", name
);
204 if (!endswith(name
, ".snapshot"))
205 return sd_bus_error_setf(e
, SD_BUS_ERROR_INVALID_ARGS
, "Unit name %s lacks snapshot suffix.", name
);
207 if (manager_get_unit(m
, name
))
208 return sd_bus_error_setf(e
, BUS_ERROR_UNIT_EXISTS
, "Snapshot %s exists already.", name
);
213 if (asprintf(&n
, "snapshot-%u.snapshot", ++ m
->n_snapshots
) < 0)
216 if (!manager_get_unit(m
, n
)) {
225 r
= manager_load_unit_prepare(m
, name
, NULL
, e
, &u
);
230 manager_dispatch_load_queue(m
);
231 assert(u
->load_state
== UNIT_LOADED
);
233 HASHMAP_FOREACH_KEY(other
, k
, m
->units
, i
) {
235 if (other
->ignore_on_snapshot
||
242 if (UNIT_VTABLE(other
)->check_snapshot
)
243 if (!UNIT_VTABLE(other
)->check_snapshot(other
))
246 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other
)))
249 r
= unit_add_two_dependencies(u
, UNIT_AFTER
, UNIT_WANTS
, other
, true);
254 SNAPSHOT(u
)->cleanup
= cleanup
;
257 log_unit_info(u
, "Created snapshot.");
263 unit_add_to_cleanup_queue(u
);
268 void snapshot_remove(Snapshot
*s
) {
271 log_unit_info(UNIT(s
), "Removing snapshot.");
273 unit_add_to_cleanup_queue(UNIT(s
));
276 const UnitVTable snapshot_vtable
= {
277 .object_size
= sizeof(Snapshot
),
280 .no_instances
= true,
283 .init
= snapshot_init
,
284 .load
= snapshot_load
,
286 .coldplug
= snapshot_coldplug
,
288 .dump
= snapshot_dump
,
290 .start
= snapshot_start
,
291 .stop
= snapshot_stop
,
293 .serialize
= snapshot_serialize
,
294 .deserialize_item
= snapshot_deserialize_item
,
296 .active_state
= snapshot_active_state
,
297 .sub_state_to_string
= snapshot_sub_state_to_string
,
299 .bus_vtable
= bus_snapshot_vtable