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 const char* const state_string_table
[_SNAPSHOT_STATE_MAX
] = {
35 [SNAPSHOT_DEAD
] = "dead",
36 [SNAPSHOT_ACTIVE
] = "active"
39 static int snapshot_load(Unit
*u
) {
46 HASHMAP_FOREACH(other
, u
->meta
.manager
->units
, i
) {
48 if (UNIT_VTABLE(other
)->no_snapshots
)
51 if ((r
= unit_add_dependency(u
, UNIT_REQUIRES
, other
)) < 0)
54 if ((r
= unit_add_dependency(u
, UNIT_AFTER
, other
)) < 0)
58 u
->meta
.load_state
= UNIT_LOADED
;
63 static void snapshot_dump(Unit
*u
, FILE *f
, const char *prefix
) {
64 Snapshot
*s
= SNAPSHOT(u
);
70 "%sSnapshot State: %s\n"
72 prefix
, state_string_table
[s
->state
],
73 prefix
, yes_no(s
->cleanup
));
76 static void snapshot_set_state(Snapshot
*s
, SnapshotState state
) {
77 SnapshotState old_state
;
83 if (state
!= old_state
)
84 log_debug("%s changed %s → %s", UNIT(s
)->meta
.id
, state_string_table
[old_state
], state_string_table
[state
]);
86 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
89 static int snapshot_start(Unit
*u
) {
90 Snapshot
*s
= SNAPSHOT(u
);
93 assert(s
->state
== SNAPSHOT_DEAD
);
95 snapshot_set_state(s
, SNAPSHOT_ACTIVE
);
98 unit_add_to_cleanup_queue(u
);
103 static int snapshot_stop(Unit
*u
) {
104 Snapshot
*s
= SNAPSHOT(u
);
107 assert(s
->state
== SNAPSHOT_ACTIVE
);
109 snapshot_set_state(s
, SNAPSHOT_DEAD
);
113 static UnitActiveState
snapshot_active_state(Unit
*u
) {
116 return state_translation_table
[SNAPSHOT(u
)->state
];
119 static const char *snapshot_sub_state_to_string(Unit
*u
) {
122 return state_string_table
[SNAPSHOT(u
)->state
];
125 int snapshot_create(Manager
*m
, const char *name
, bool cleanup
, Snapshot
**_s
) {
134 if (!unit_name_is_valid(name
))
137 if (unit_name_to_type(name
) != UNIT_SNAPSHOT
)
140 if (manager_get_unit(m
, name
))
146 if (asprintf(&n
, "snapshot-%u.snapshot", ++ m
->n_snapshots
) < 0)
149 if (!manager_get_unit(m
, n
))
158 r
= manager_load_unit(m
, name
, NULL
, &u
);
164 SNAPSHOT(u
)->cleanup
= cleanup
;
170 void snapshot_remove(Snapshot
*s
) {
173 unit_add_to_cleanup_queue(UNIT(s
));
176 const UnitVTable snapshot_vtable
= {
177 .suffix
= ".snapshot",
180 .no_instances
= true,
181 .no_snapshots
= true,
183 .load
= snapshot_load
,
185 .dump
= snapshot_dump
,
187 .start
= snapshot_start
,
188 .stop
= snapshot_stop
,
190 .active_state
= snapshot_active_state
,
191 .sub_state_to_string
= snapshot_sub_state_to_string
,
193 .bus_message_handler
= bus_snapshot_message_handler