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_coldplug(Unit
*u
) {
51 Snapshot
*s
= SNAPSHOT(u
);
54 assert(s
->state
== SNAPSHOT_DEAD
);
56 if (s
->deserialized_state
!= s
->state
)
57 snapshot_set_state(s
, s
->deserialized_state
);
62 static void snapshot_dump(Unit
*u
, FILE *f
, const char *prefix
) {
63 Snapshot
*s
= SNAPSHOT(u
);
69 "%sSnapshot State: %s\n"
71 prefix
, snapshot_state_to_string(s
->state
),
72 prefix
, yes_no(s
->cleanup
));
75 static int snapshot_start(Unit
*u
) {
76 Snapshot
*s
= SNAPSHOT(u
);
79 assert(s
->state
== SNAPSHOT_DEAD
);
81 snapshot_set_state(s
, SNAPSHOT_ACTIVE
);
84 unit_add_to_cleanup_queue(u
);
89 static int snapshot_stop(Unit
*u
) {
90 Snapshot
*s
= SNAPSHOT(u
);
93 assert(s
->state
== SNAPSHOT_ACTIVE
);
95 snapshot_set_state(s
, SNAPSHOT_DEAD
);
99 static int snapshot_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
100 Snapshot
*s
= SNAPSHOT(u
);
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
);
116 static int snapshot_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
117 Snapshot
*s
= SNAPSHOT(u
);
125 if (streq(key
, "state")) {
128 if ((state
= snapshot_state_from_string(value
)) < 0)
129 log_debug("Failed to parse state value %s", value
);
131 s
->deserialized_state
= state
;
133 } else if (streq(key
, "cleanup")) {
135 if ((r
= parse_boolean(value
)) < 0)
136 log_debug("Failed to parse cleanup value %s", value
);
140 } else if (streq(key
, "requires")) {
142 if ((r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, value
, NULL
, true)) < 0)
145 if ((r
= unit_add_dependency_by_name(u
, UNIT_REQUIRES
, value
, NULL
, true)) < 0)
148 log_debug("Unknown serialization key '%s'", key
);
153 static UnitActiveState
snapshot_active_state(Unit
*u
) {
156 return state_translation_table
[SNAPSHOT(u
)->state
];
159 static const char *snapshot_sub_state_to_string(Unit
*u
) {
162 return snapshot_state_to_string(SNAPSHOT(u
)->state
);
165 int snapshot_create(Manager
*m
, const char *name
, bool cleanup
, Snapshot
**_s
) {
167 Unit
*other
, *u
= NULL
;
176 if (!unit_name_is_valid(name
))
179 if (unit_name_to_type(name
) != UNIT_SNAPSHOT
)
182 if (manager_get_unit(m
, name
))
188 if (asprintf(&n
, "snapshot-%u.snapshot", ++ m
->n_snapshots
) < 0)
191 if (!manager_get_unit(m
, n
))
200 r
= manager_load_unit(m
, name
, NULL
, &u
);
206 HASHMAP_FOREACH_KEY(other
, k
, m
->units
, i
) {
208 if (UNIT_VTABLE(other
)->no_snapshots
)
211 if (k
!= other
->meta
.id
)
214 if (UNIT_VTABLE(other
)->check_snapshot
)
215 if (!UNIT_VTABLE(other
)->check_snapshot(other
))
218 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other
)))
221 if ((r
= unit_add_dependency(u
, UNIT_REQUIRES
, other
, true)) < 0)
224 if ((r
= unit_add_dependency(u
, UNIT_AFTER
, other
, true)) < 0)
228 SNAPSHOT(u
)->cleanup
= cleanup
;
235 unit_add_to_cleanup_queue(u
);
240 void snapshot_remove(Snapshot
*s
) {
243 unit_add_to_cleanup_queue(UNIT(s
));
246 static const char* const snapshot_state_table
[_SNAPSHOT_STATE_MAX
] = {
247 [SNAPSHOT_DEAD
] = "dead",
248 [SNAPSHOT_ACTIVE
] = "active"
251 DEFINE_STRING_TABLE_LOOKUP(snapshot_state
, SnapshotState
);
253 const UnitVTable snapshot_vtable
= {
254 .suffix
= ".snapshot",
257 .no_instances
= true,
258 .no_snapshots
= true,
261 .load
= unit_load_nop
,
262 .coldplug
= snapshot_coldplug
,
264 .dump
= snapshot_dump
,
266 .start
= snapshot_start
,
267 .stop
= snapshot_stop
,
269 .serialize
= snapshot_serialize
,
270 .deserialize_item
= snapshot_deserialize_item
,
272 .active_state
= snapshot_active_state
,
273 .sub_state_to_string
= snapshot_sub_state_to_string
,
275 .bus_message_handler
= bus_snapshot_message_handler