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"
38 static const UnitActiveState state_translation_table
[_SWAP_STATE_MAX
] = {
39 [SWAP_DEAD
] = UNIT_INACTIVE
,
40 [SWAP_ACTIVE
] = UNIT_ACTIVE
,
41 [SWAP_MAINTENANCE
] = UNIT_INACTIVE
44 static void swap_init(Unit
*u
) {
48 assert(s
->meta
.load_state
== UNIT_STUB
);
50 s
->parameters_etc_fstab
.priority
= s
->parameters_proc_swaps
.priority
= s
->parameters_fragment
.priority
= -1;
53 static void swap_done(Unit
*u
) {
59 free(s
->parameters_etc_fstab
.what
);
60 free(s
->parameters_proc_swaps
.what
);
61 free(s
->parameters_fragment
.what
);
64 int swap_add_one_mount_link(Swap
*s
, Mount
*m
) {
70 if (s
->meta
.load_state
!= UNIT_LOADED
||
71 m
->meta
.load_state
!= UNIT_LOADED
)
74 if (is_device_path(s
->what
))
77 if (!path_startswith(s
->what
, m
->where
))
80 if ((r
= unit_add_dependency(UNIT(m
), UNIT_BEFORE
, UNIT(s
), true)) < 0)
83 if ((r
= unit_add_dependency(UNIT(s
), UNIT_REQUIRES
, UNIT(m
), true)) < 0)
89 static int swap_add_mount_links(Swap
*s
) {
95 LIST_FOREACH(units_per_type
, other
, s
->meta
.manager
->units_per_type
[UNIT_MOUNT
])
96 if ((r
= swap_add_one_mount_link(s
, (Mount
*) other
)) < 0)
102 static int swap_add_target_links(Swap
*s
) {
109 if (s
->from_fragment
)
110 p
= &s
->parameters_fragment
;
111 else if (s
->from_etc_fstab
)
112 p
= &s
->parameters_etc_fstab
;
116 if ((r
= manager_load_unit(s
->meta
.manager
, SPECIAL_SWAP_TARGET
, NULL
, &tu
)) < 0)
119 if (!p
->noauto
&& p
->handle
&& s
->meta
.manager
->running_as
!= MANAGER_SESSION
)
120 if ((r
= unit_add_dependency(tu
, UNIT_WANTS
, UNIT(s
), true)) < 0)
123 return unit_add_dependency(UNIT(s
), UNIT_BEFORE
, tu
, true);
126 static int swap_verify(Swap
*s
) {
130 if (UNIT(s
)->meta
.load_state
!= UNIT_LOADED
)
133 if (!(e
= unit_name_from_path(s
->what
, ".swap")))
136 b
= unit_has_name(UNIT(s
), e
);
140 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", UNIT(s
)->meta
.id
);
147 static int swap_load(Unit
*u
) {
152 assert(u
->meta
.load_state
== UNIT_STUB
);
154 /* Load a .swap file */
155 if ((r
= unit_load_fragment_and_dropin_optional(u
)) < 0)
158 if (u
->meta
.load_state
== UNIT_LOADED
) {
160 if (s
->meta
.fragment_path
)
161 s
->from_fragment
= true;
164 if (s
->parameters_fragment
.what
)
165 s
->what
= strdup(s
->parameters_fragment
.what
);
166 else if (s
->parameters_etc_fstab
.what
)
167 s
->what
= strdup(s
->parameters_etc_fstab
.what
);
168 else if (s
->parameters_proc_swaps
.what
)
169 s
->what
= strdup(s
->parameters_proc_swaps
.what
);
171 s
->what
= unit_name_to_path(u
->meta
.id
);
177 path_kill_slashes(s
->what
);
179 if (!s
->meta
.description
)
180 if ((r
= unit_set_description(u
, s
->what
)) < 0)
183 if ((r
= unit_add_node_link(u
, s
->what
,
184 (u
->meta
.manager
->running_as
== MANAGER_INIT
||
185 u
->meta
.manager
->running_as
== MANAGER_SYSTEM
))) < 0)
188 if ((r
= swap_add_mount_links(s
)) < 0)
191 if ((r
= swap_add_target_links(s
)) < 0)
195 return swap_verify(s
);
198 static int swap_find(Manager
*m
, const char *what
, Unit
**_u
) {
206 /* /proc/swaps and /etc/fstab might refer to this device by
207 * different names (e.g. one by uuid, the other by the kernel
208 * name), we hence need to look for all aliases we are aware
209 * of for this device */
211 if (!(e
= unit_name_from_path(what
, ".device")))
214 u
= manager_get_unit(m
, e
);
221 SET_FOREACH(d
, u
->meta
.names
, i
) {
224 if (!(e
= unit_name_change_suffix(d
, ".swap")))
227 k
= manager_get_unit(m
, e
);
247 bool from_proc_swaps
) {
249 char *e
= NULL
, *w
= NULL
;
257 if (!(e
= unit_name_from_path(what
, ".swap")))
260 if (!(u
= manager_get_unit(m
, e
)))
261 if ((r
= swap_find(m
, what
, &u
)) < 0)
267 if (!(u
= unit_new(m
))) {
274 if ((r
= unit_add_name(u
, e
)) < 0)
277 if (!(w
= strdup(what
))) {
282 if (from_proc_swaps
) {
283 p
= &SWAP(u
)->parameters_proc_swaps
;
284 SWAP(u
)->from_proc_swaps
= true;
286 p
= &SWAP(u
)->parameters_etc_fstab
;
287 SWAP(u
)->from_etc_fstab
= true;
293 p
->priority
= priority
;
298 unit_add_to_load_queue(u
);
300 unit_add_to_dbus_queue(u
);
316 static void swap_set_state(Swap
*s
, SwapState state
) {
320 old_state
= s
->state
;
323 if (state
!= old_state
)
324 log_debug("%s changed %s -> %s",
326 swap_state_to_string(old_state
),
327 swap_state_to_string(state
));
329 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
332 static int swap_coldplug(Unit
*u
) {
334 SwapState new_state
= SWAP_DEAD
;
337 assert(s
->state
== SWAP_DEAD
);
339 if (s
->deserialized_state
!= s
->state
)
340 new_state
= s
->deserialized_state
;
341 else if (s
->from_proc_swaps
)
342 new_state
= SWAP_ACTIVE
;
344 if (new_state
!= s
->state
)
345 swap_set_state(s
, new_state
);
350 static void swap_dump(Unit
*u
, FILE *f
, const char *prefix
) {
357 if (s
->from_proc_swaps
)
358 p
= &s
->parameters_proc_swaps
;
359 else if (s
->from_fragment
)
360 p
= &s
->parameters_fragment
;
362 p
= &s
->parameters_etc_fstab
;
370 "%sFrom /etc/fstab: %s\n"
371 "%sFrom /proc/swaps: %s\n"
372 "%sFrom fragment: %s\n",
373 prefix
, swap_state_to_string(s
->state
),
376 prefix
, yes_no(p
->noauto
),
377 prefix
, yes_no(p
->handle
),
378 prefix
, yes_no(s
->from_etc_fstab
),
379 prefix
, yes_no(s
->from_proc_swaps
),
380 prefix
, yes_no(s
->from_fragment
));
383 static void swap_enter_dead(Swap
*s
, bool success
) {
386 swap_set_state(s
, success
? SWAP_MAINTENANCE
: SWAP_DEAD
);
389 static int swap_start(Unit
*u
) {
395 assert(s
->state
== SWAP_DEAD
|| s
->state
== SWAP_MAINTENANCE
);
397 if (s
->from_fragment
)
398 priority
= s
->parameters_fragment
.priority
;
399 else if (s
->from_etc_fstab
)
400 priority
= s
->parameters_etc_fstab
.priority
;
402 r
= swapon(s
->what
, (priority
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
);
404 if (r
< 0 && errno
!= EBUSY
) {
406 swap_enter_dead(s
, false);
410 swap_set_state(s
, SWAP_ACTIVE
);
414 static int swap_stop(Unit
*u
) {
420 assert(s
->state
== SWAP_ACTIVE
);
422 r
= swapoff(s
->what
);
423 swap_enter_dead(s
, r
>= 0 || errno
== EINVAL
);
428 static int swap_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
435 unit_serialize_item(u
, f
, "state", swap_state_to_string(s
->state
));
440 static int swap_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
446 if (streq(key
, "state")) {
449 if ((state
= swap_state_from_string(value
)) < 0)
450 log_debug("Failed to parse state value %s", value
);
452 s
->deserialized_state
= state
;
454 log_debug("Unknown serialization key '%s'", key
);
459 static UnitActiveState
swap_active_state(Unit
*u
) {
462 return state_translation_table
[SWAP(u
)->state
];
465 static const char *swap_sub_state_to_string(Unit
*u
) {
468 return swap_state_to_string(SWAP(u
)->state
);
471 static bool swap_check_gc(Unit
*u
) {
476 return s
->from_etc_fstab
|| s
->from_proc_swaps
;
479 static int swap_load_proc_swaps(Manager
*m
) {
480 rewind(m
->proc_swaps
);
482 (void) fscanf(m
->proc_swaps
, "%*s %*s %*s %*s %*s\n");
485 char *dev
= NULL
, *d
;
488 if ((k
= fscanf(m
->proc_swaps
,
489 "%ms " /* device/file */
490 "%*s " /* type of swap */
491 "%*s " /* swap size */
493 "%i\n", /* priority */
494 &dev
, &prio
)) != 2) {
509 k
= swap_add_one(m
, d
, prio
, false, false, true);
519 static void swap_shutdown(Manager
*m
) {
523 fclose(m
->proc_swaps
);
524 m
->proc_swaps
= NULL
;
528 static const char* const swap_state_table
[_SWAP_STATE_MAX
] = {
529 [SWAP_DEAD
] = "dead",
530 [SWAP_ACTIVE
] = "active",
531 [SWAP_MAINTENANCE
] = "maintenance"
534 DEFINE_STRING_TABLE_LOOKUP(swap_state
, SwapState
);
536 static int swap_enumerate(Manager
*m
) {
541 if (!(m
->proc_swaps
= fopen("/proc/swaps", "re")))
544 if ((r
= swap_load_proc_swaps(m
)) < 0)
550 const UnitVTable swap_vtable
= {
553 .no_instances
= true,
560 .coldplug
= swap_coldplug
,
567 .serialize
= swap_serialize
,
568 .deserialize_item
= swap_deserialize_item
,
570 .active_state
= swap_active_state
,
571 .sub_state_to_string
= swap_sub_state_to_string
,
573 .check_gc
= swap_check_gc
,
575 .bus_message_handler
= bus_swap_message_handler
,
577 .enumerate
= swap_enumerate
,
578 .shutdown
= swap_shutdown