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 <sys/epoll.h>
32 #include "load-fragment.h"
33 #include "load-dropin.h"
34 #include "unit-name.h"
35 #include "dbus-swap.h"
37 static const UnitActiveState state_translation_table
[_SWAP_STATE_MAX
] = {
38 [SWAP_DEAD
] = UNIT_INACTIVE
,
39 [SWAP_ACTIVE
] = UNIT_ACTIVE
,
40 [SWAP_MAINTAINANCE
] = UNIT_INACTIVE
43 static void swap_done(Unit
*u
) {
51 static int swap_verify(Swap
*s
) {
55 if (UNIT(s
)->meta
.load_state
!= UNIT_LOADED
)
58 if (!(e
= unit_name_from_path(s
->what
, ".swap")))
61 b
= unit_has_name(UNIT(s
), e
);
65 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", UNIT(s
)->meta
.id
);
71 static int swap_add_target_links(Swap
*s
) {
72 Manager
*m
= s
->meta
.manager
;
76 r
= manager_load_unit(m
, SPECIAL_SWAP_TARGET
, NULL
, &tu
);
80 if (!s
->no_auto
&& (r
= unit_add_dependency(tu
, UNIT_WANTS
, UNIT(s
), true)) < 0)
83 return unit_add_dependency(UNIT(s
), UNIT_BEFORE
, tu
, true);
86 static int swap_load(Unit
*u
) {
91 assert(u
->meta
.load_state
== UNIT_STUB
);
93 /* Load a .swap file */
94 if ((r
= unit_load_fragment_and_dropin_optional(u
)) < 0)
97 if (u
->meta
.load_state
== UNIT_LOADED
) {
99 if (!(s
->what
= unit_name_to_path(u
->meta
.id
)))
102 path_kill_slashes(s
->what
);
104 if ((r
= mount_add_node_links(u
, s
->what
)) < 0)
107 if (!path_startswith(s
->what
, "/dev/"))
108 if ((r
= mount_add_path_links(u
, s
->what
, true)) < 0)
111 if ((r
= swap_add_target_links(s
)) < 0)
115 return swap_verify(s
);
118 int swap_add_one(Manager
*m
, const char *what
, bool no_auto
, int prio
, bool from_proc_swaps
) {
124 if (!(e
= unit_name_from_path(what
, ".swap")))
127 if (!(u
= manager_get_unit(m
, e
))) {
130 if (!(u
= unit_new(m
))) {
135 r
= unit_add_name(u
, e
);
141 if (!(SWAP(u
)->what
= strdup(what
))) {
146 if ((r
= unit_set_description(u
, what
)) < 0)
149 unit_add_to_load_queue(u
);
151 SWAP(u
)->from_proc_swaps_only
= from_proc_swaps
;
153 if (SWAP(u
)->from_proc_swaps_only
&& !from_proc_swaps
)
154 SWAP(u
)->from_proc_swaps_only
= false;
160 if (!from_proc_swaps
)
161 SWAP(u
)->no_auto
= no_auto
;
163 SWAP(u
)->found_in_proc_swaps
= true;
165 SWAP(u
)->priority
= prio
;
176 static void swap_set_state(Swap
*s
, SwapState state
) {
180 old_state
= s
->state
;
183 if (state
!= old_state
)
184 log_debug("%s changed %s -> %s",
186 swap_state_to_string(old_state
),
187 swap_state_to_string(state
));
189 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
192 static int swap_coldplug(Unit
*u
) {
194 SwapState new_state
= SWAP_DEAD
;
197 assert(s
->state
== SWAP_DEAD
);
199 if (s
->deserialized_state
!= s
->state
)
200 new_state
= s
->deserialized_state
;
201 else if (s
->found_in_proc_swaps
)
202 new_state
= SWAP_ACTIVE
;
204 if (new_state
!= s
->state
)
205 swap_set_state(s
, s
->deserialized_state
);
210 static void swap_dump(Unit
*u
, FILE *f
, const char *prefix
) {
216 "%sAutomount State: %s\n"
220 prefix
, swap_state_to_string(s
->state
),
223 prefix
, yes_no(s
->no_auto
));
226 static void swap_enter_dead(Swap
*s
, bool success
) {
229 swap_set_state(s
, success
? SWAP_MAINTAINANCE
: SWAP_DEAD
);
232 static int swap_start(Unit
*u
) {
238 assert(s
->state
== SWAP_DEAD
|| s
->state
== SWAP_MAINTAINANCE
);
240 r
= swapon(s
->what
, (s
->priority
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
);
242 if (r
< 0 && errno
!= EBUSY
) {
244 swap_enter_dead(s
, false);
248 swap_set_state(s
, SWAP_ACTIVE
);
252 static int swap_stop(Unit
*u
) {
258 assert(s
->state
== SWAP_ACTIVE
);
260 r
= swapoff(s
->what
);
261 swap_enter_dead(s
, r
>= 0 || errno
== EINVAL
);
266 static int swap_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
273 unit_serialize_item(u
, f
, "state", swap_state_to_string(s
->state
));
278 static int swap_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
284 if (streq(key
, "state")) {
287 if ((state
= swap_state_from_string(value
)) < 0)
288 log_debug("Failed to parse state value %s", value
);
290 s
->deserialized_state
= state
;
292 log_debug("Unknown serialization key '%s'", key
);
297 static UnitActiveState
swap_active_state(Unit
*u
) {
300 return state_translation_table
[SWAP(u
)->state
];
303 static const char *swap_sub_state_to_string(Unit
*u
) {
306 return swap_state_to_string(SWAP(u
)->state
);
309 static bool swap_check_gc(Unit
*u
) {
314 return !s
->from_proc_swaps_only
|| s
->found_in_proc_swaps
;
317 static int swap_load_proc_swaps(Manager
*m
) {
320 rewind(m
->proc_swaps
);
322 (void) fscanf(m
->proc_self_mountinfo
, "%*s %*s %*s %*s %*s\n");
325 char *dev
= NULL
, *d
;
328 k
= fscanf(m
->proc_self_mountinfo
,
329 "%ms " /* device/file */
330 "%*s " /* type of swap */
331 "%*s " /* swap size */
333 "%d\n", /* priority */
343 if (!(d
= cunescape(dev
))) {
349 k
= swap_add_one(m
, d
, false, prio
, true);
357 LIST_FOREACH(units_per_type
, meta
, m
->units_per_type
[UNIT_SWAP
]) {
358 Swap
*s
= (Swap
*) meta
;
360 if (s
->state
!= SWAP_DEAD
&& s
->state
!= SWAP_ACTIVE
)
363 if ((s
->state
== SWAP_DEAD
&& !s
->found_in_proc_swaps
) ||
364 (s
->state
== SWAP_ACTIVE
&& s
->found_in_proc_swaps
))
367 swap_set_state(s
, s
->found_in_proc_swaps
? SWAP_ACTIVE
: SWAP_DEAD
);
369 /* Reset the flags for later calls */
370 s
->found_in_proc_swaps
= false;
374 static void swap_shutdown(Manager
*m
) {
378 fclose(m
->proc_swaps
);
379 m
->proc_swaps
= NULL
;
383 static const char* const swap_state_table
[_SWAP_STATE_MAX
] = {
384 [SWAP_DEAD
] = "dead",
385 [SWAP_ACTIVE
] = "active",
386 [SWAP_MAINTAINANCE
] = "maintainance"
389 DEFINE_STRING_TABLE_LOOKUP(swap_state
, SwapState
);
391 static int swap_enumerate(Manager
*m
) {
395 if (!m
->proc_swaps
&&
396 !(m
->proc_swaps
= fopen("/proc/swaps", "er")))
399 if ((r
= swap_load_proc_swaps(m
)) < 0)
405 const UnitVTable swap_vtable
= {
409 .no_instances
= true,
415 .coldplug
= swap_coldplug
,
422 .serialize
= swap_serialize
,
423 .deserialize_item
= swap_deserialize_item
,
425 .active_state
= swap_active_state
,
426 .sub_state_to_string
= swap_sub_state_to_string
,
428 .check_gc
= swap_check_gc
,
430 .bus_message_handler
= bus_swap_message_handler
,
432 .enumerate
= swap_enumerate
,
433 .shutdown
= swap_shutdown