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 int swap_verify(Swap
*s
) {
47 if (UNIT(s
)->meta
.load_state
!= UNIT_LOADED
)
50 if (!(e
= unit_name_from_path(s
->what
, ".swap")))
53 b
= unit_has_name(UNIT(s
), e
);
57 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", UNIT(s
)->meta
.id
);
63 static int swap_add_target_links(Swap
*s
) {
64 Manager
*m
= s
->meta
.manager
;
68 r
= manager_load_unit(m
, SPECIAL_SWAP_TARGET
, NULL
, &tu
);
72 if (!s
->no_auto
&& (r
= unit_add_dependency(tu
, UNIT_WANTS
, UNIT(s
), true)) < 0)
75 return unit_add_dependency(UNIT(s
), UNIT_BEFORE
, tu
, true);
78 static int swap_load(Unit
*u
) {
83 assert(u
->meta
.load_state
== UNIT_STUB
);
85 /* Load a .swap file */
86 if ((r
= unit_load_fragment_and_dropin_optional(u
)) < 0)
89 if (u
->meta
.load_state
== UNIT_LOADED
) {
91 if (!(s
->what
= unit_name_to_path(u
->meta
.id
)))
94 path_kill_slashes(s
->what
);
96 if ((r
= mount_add_node_links(u
, s
->what
)) < 0)
99 if (!path_startswith(s
->what
, "/dev/"))
100 if ((r
= mount_add_path_links(u
, s
->what
, true)) < 0)
103 if ((r
= swap_add_target_links(s
)) < 0)
107 return swap_verify(s
);
110 int swap_add_one(Manager
*m
, const char *what
, bool no_auto
, int prio
, bool from_proc_swaps
) {
116 if (!(e
= unit_name_from_path(what
, ".swap")))
119 if (!(u
= manager_get_unit(m
, e
))) {
122 if (!(u
= unit_new(m
))) {
127 r
= unit_add_name(u
, e
);
133 if (!(SWAP(u
)->what
= strdup(what
))) {
138 if ((r
= unit_set_description(u
, what
)) < 0)
141 unit_add_to_load_queue(u
);
143 SWAP(u
)->from_proc_swaps_only
= from_proc_swaps
;
145 if (SWAP(u
)->from_proc_swaps_only
&& !from_proc_swaps
)
146 SWAP(u
)->from_proc_swaps_only
= false;
152 if (!from_proc_swaps
)
153 SWAP(u
)->no_auto
= no_auto
;
155 SWAP(u
)->found_in_proc_swaps
= true;
157 SWAP(u
)->priority
= prio
;
168 static void swap_set_state(Swap
*s
, SwapState state
) {
172 old_state
= s
->state
;
175 if (state
!= old_state
)
176 log_debug("%s changed %s -> %s",
178 swap_state_to_string(old_state
),
179 swap_state_to_string(state
));
181 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
184 static int swap_coldplug(Unit
*u
) {
186 SwapState new_state
= SWAP_DEAD
;
189 assert(s
->state
== SWAP_DEAD
);
191 if (s
->deserialized_state
!= s
->state
)
192 new_state
= s
->deserialized_state
;
193 else if (s
->found_in_proc_swaps
)
194 new_state
= SWAP_ACTIVE
;
196 if (new_state
!= s
->state
)
197 swap_set_state(s
, s
->deserialized_state
);
202 static void swap_dump(Unit
*u
, FILE *f
, const char *prefix
) {
208 "%sAutomount State: %s\n"
212 prefix
, swap_state_to_string(s
->state
),
215 prefix
, yes_no(s
->no_auto
));
218 static void swap_enter_dead(Swap
*s
, bool success
) {
221 swap_set_state(s
, success
? SWAP_MAINTAINANCE
: SWAP_DEAD
);
224 static int swap_start(Unit
*u
) {
230 assert(s
->state
== SWAP_DEAD
|| s
->state
== SWAP_MAINTAINANCE
);
232 r
= swapon(s
->what
, (s
->priority
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
);
234 if (r
< 0 && errno
!= EBUSY
) {
236 swap_enter_dead(s
, false);
240 swap_set_state(s
, SWAP_ACTIVE
);
244 static int swap_stop(Unit
*u
) {
250 assert(s
->state
== SWAP_ACTIVE
);
252 r
= swapoff(s
->what
);
253 swap_enter_dead(s
, r
>= 0 || errno
== EINVAL
);
258 static int swap_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
265 unit_serialize_item(u
, f
, "state", swap_state_to_string(s
->state
));
270 static int swap_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
276 if (streq(key
, "state")) {
279 if ((state
= swap_state_from_string(value
)) < 0)
280 log_debug("Failed to parse state value %s", value
);
282 s
->deserialized_state
= state
;
284 log_debug("Unknown serialization key '%s'", key
);
289 static UnitActiveState
swap_active_state(Unit
*u
) {
292 return state_translation_table
[SWAP(u
)->state
];
295 static const char *swap_sub_state_to_string(Unit
*u
) {
298 return swap_state_to_string(SWAP(u
)->state
);
301 static bool swap_check_gc(Unit
*u
) {
306 return !s
->from_proc_swaps_only
|| s
->found_in_proc_swaps
;
309 static int swap_load_proc_swaps(Manager
*m
) {
312 rewind(m
->proc_swaps
);
313 fscanf(m
->proc_self_mountinfo
, "%*s %*s %*s %*s %*s\n");
316 char *dev
= NULL
, *d
;
319 k
= fscanf(m
->proc_self_mountinfo
,
320 "%ms " /* device/file */
321 "%*s " /* type of swap */
322 "%*s " /* swap size */
324 "%d\n", /* priority */
334 if (!(d
= cunescape(dev
))) {
340 k
= swap_add_one(m
, d
, false, prio
, true);
348 LIST_FOREACH(units_per_type
, meta
, m
->units_per_type
[UNIT_SWAP
]) {
349 Swap
*s
= (Swap
*) meta
;
351 if (s
->state
!= SWAP_DEAD
&& s
->state
!= SWAP_ACTIVE
)
354 if ((s
->state
== SWAP_DEAD
&& !s
->found_in_proc_swaps
) ||
355 (s
->state
== SWAP_ACTIVE
&& s
->found_in_proc_swaps
))
358 swap_set_state(s
, s
->found_in_proc_swaps
? SWAP_ACTIVE
: SWAP_DEAD
);
360 /* Reset the flags for later calls */
361 s
->found_in_proc_swaps
= false;
365 static void swap_shutdown(Manager
*m
) {
369 fclose(m
->proc_swaps
);
370 m
->proc_swaps
= NULL
;
374 static const char* const swap_state_table
[_SWAP_STATE_MAX
] = {
375 [SWAP_DEAD
] = "dead",
376 [SWAP_ACTIVE
] = "active",
377 [SWAP_MAINTAINANCE
] = "maintainance"
380 DEFINE_STRING_TABLE_LOOKUP(swap_state
, SwapState
);
382 static int swap_enumerate(Manager
*m
) {
386 if (!m
->proc_swaps
&&
387 !(m
->proc_swaps
= fopen("/proc/swaps", "er")))
390 if ((r
= swap_load_proc_swaps(m
)) < 0)
396 const UnitVTable swap_vtable
= {
400 .no_instances
= true,
404 .coldplug
= swap_coldplug
,
411 .serialize
= swap_serialize
,
412 .deserialize_item
= swap_deserialize_item
,
414 .active_state
= swap_active_state
,
415 .sub_state_to_string
= swap_sub_state_to_string
,
417 .check_gc
= swap_check_gc
,
419 .bus_message_handler
= bus_swap_message_handler
,
421 .shutdown
= swap_shutdown
,
423 .enumerate
= swap_enumerate