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_MAINTENANCE
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_two_dependencies(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, UNIT(m
), true)) < 0)
86 static int swap_add_mount_links(Swap
*s
) {
92 LIST_FOREACH(units_per_type
, other
, s
->meta
.manager
->units_per_type
[UNIT_MOUNT
])
93 if ((r
= swap_add_one_mount_link(s
, (Mount
*) other
)) < 0)
99 static int swap_add_target_links(Swap
*s
) {
106 if (s
->from_fragment
)
107 p
= &s
->parameters_fragment
;
108 else if (s
->from_etc_fstab
)
109 p
= &s
->parameters_etc_fstab
;
113 if ((r
= manager_load_unit(s
->meta
.manager
, SPECIAL_SWAP_TARGET
, NULL
, &tu
)) < 0)
116 if (!p
->noauto
&& p
->handle
&& s
->meta
.manager
->running_as
== MANAGER_SYSTEM
)
117 if ((r
= unit_add_dependency(tu
, UNIT_WANTS
, UNIT(s
), true)) < 0)
120 return unit_add_dependency(UNIT(s
), UNIT_BEFORE
, tu
, true);
123 static int swap_verify(Swap
*s
) {
127 if (s
->meta
.load_state
!= UNIT_LOADED
)
130 if (!(e
= unit_name_from_path(s
->what
, ".swap")))
133 b
= unit_has_name(UNIT(s
), e
);
137 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", s
->meta
.id
);
144 static int swap_load(Unit
*u
) {
149 assert(u
->meta
.load_state
== UNIT_STUB
);
151 /* Load a .swap file */
152 if ((r
= unit_load_fragment_and_dropin_optional(u
)) < 0)
155 if (u
->meta
.load_state
== UNIT_LOADED
) {
157 if (s
->meta
.fragment_path
)
158 s
->from_fragment
= true;
161 if (s
->parameters_fragment
.what
)
162 s
->what
= strdup(s
->parameters_fragment
.what
);
163 else if (s
->parameters_etc_fstab
.what
)
164 s
->what
= strdup(s
->parameters_etc_fstab
.what
);
165 else if (s
->parameters_proc_swaps
.what
)
166 s
->what
= strdup(s
->parameters_proc_swaps
.what
);
168 s
->what
= unit_name_to_path(u
->meta
.id
);
174 path_kill_slashes(s
->what
);
176 if (!s
->meta
.description
)
177 if ((r
= unit_set_description(u
, s
->what
)) < 0)
180 if ((r
= unit_add_node_link(u
, s
->what
, u
->meta
.manager
->running_as
== MANAGER_SYSTEM
)) < 0)
183 if ((r
= swap_add_mount_links(s
)) < 0)
186 if ((r
= swap_add_target_links(s
)) < 0)
190 return swap_verify(s
);
193 static int swap_find(Manager
*m
, const char *what
, Unit
**_u
) {
201 /* /proc/swaps and /etc/fstab might refer to this device by
202 * different names (e.g. one by uuid, the other by the kernel
203 * name), we hence need to look for all aliases we are aware
204 * of for this device */
206 if (!(e
= unit_name_from_path(what
, ".device")))
209 u
= manager_get_unit(m
, e
);
216 SET_FOREACH(d
, u
->meta
.names
, i
) {
219 if (!(e
= unit_name_change_suffix(d
, ".swap")))
222 k
= manager_get_unit(m
, e
);
242 bool from_proc_swaps
) {
244 char *e
= NULL
, *w
= NULL
;
252 if (!(e
= unit_name_from_path(what
, ".swap")))
255 if (!(u
= manager_get_unit(m
, e
)))
256 if ((r
= swap_find(m
, what
, &u
)) < 0)
262 if (!(u
= unit_new(m
))) {
269 if ((r
= unit_add_name(u
, e
)) < 0)
272 if (!(w
= strdup(what
))) {
277 if (from_proc_swaps
) {
278 p
= &SWAP(u
)->parameters_proc_swaps
;
279 SWAP(u
)->from_proc_swaps
= true;
281 p
= &SWAP(u
)->parameters_etc_fstab
;
282 SWAP(u
)->from_etc_fstab
= true;
288 p
->priority
= priority
;
293 unit_add_to_load_queue(u
);
295 unit_add_to_dbus_queue(u
);
311 static void swap_set_state(Swap
*s
, SwapState state
) {
315 old_state
= s
->state
;
318 if (state
!= old_state
)
319 log_debug("%s changed %s -> %s",
321 swap_state_to_string(old_state
),
322 swap_state_to_string(state
));
324 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
327 static int swap_coldplug(Unit
*u
) {
329 SwapState new_state
= SWAP_DEAD
;
332 assert(s
->state
== SWAP_DEAD
);
334 if (s
->deserialized_state
!= s
->state
)
335 new_state
= s
->deserialized_state
;
336 else if (s
->from_proc_swaps
)
337 new_state
= SWAP_ACTIVE
;
339 if (new_state
!= s
->state
)
340 swap_set_state(s
, new_state
);
345 static void swap_dump(Unit
*u
, FILE *f
, const char *prefix
) {
352 if (s
->from_proc_swaps
)
353 p
= &s
->parameters_proc_swaps
;
354 else if (s
->from_fragment
)
355 p
= &s
->parameters_fragment
;
357 p
= &s
->parameters_etc_fstab
;
365 "%sFrom /etc/fstab: %s\n"
366 "%sFrom /proc/swaps: %s\n"
367 "%sFrom fragment: %s\n",
368 prefix
, swap_state_to_string(s
->state
),
371 prefix
, yes_no(p
->noauto
),
372 prefix
, yes_no(p
->handle
),
373 prefix
, yes_no(s
->from_etc_fstab
),
374 prefix
, yes_no(s
->from_proc_swaps
),
375 prefix
, yes_no(s
->from_fragment
));
378 static void swap_enter_dead(Swap
*s
, bool success
) {
381 swap_set_state(s
, success
? SWAP_MAINTENANCE
: SWAP_DEAD
);
384 static int swap_start(Unit
*u
) {
390 assert(s
->state
== SWAP_DEAD
|| s
->state
== SWAP_MAINTENANCE
);
392 if (s
->from_fragment
)
393 priority
= s
->parameters_fragment
.priority
;
394 else if (s
->from_etc_fstab
)
395 priority
= s
->parameters_etc_fstab
.priority
;
397 r
= swapon(s
->what
, (priority
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
);
399 if (r
< 0 && errno
!= EBUSY
) {
401 swap_enter_dead(s
, false);
405 swap_set_state(s
, SWAP_ACTIVE
);
409 static int swap_stop(Unit
*u
) {
415 assert(s
->state
== SWAP_ACTIVE
);
417 r
= swapoff(s
->what
);
418 swap_enter_dead(s
, r
>= 0 || errno
== EINVAL
);
423 static int swap_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
430 unit_serialize_item(u
, f
, "state", swap_state_to_string(s
->state
));
435 static int swap_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
441 if (streq(key
, "state")) {
444 if ((state
= swap_state_from_string(value
)) < 0)
445 log_debug("Failed to parse state value %s", value
);
447 s
->deserialized_state
= state
;
449 log_debug("Unknown serialization key '%s'", key
);
454 static UnitActiveState
swap_active_state(Unit
*u
) {
457 return state_translation_table
[SWAP(u
)->state
];
460 static const char *swap_sub_state_to_string(Unit
*u
) {
463 return swap_state_to_string(SWAP(u
)->state
);
466 static bool swap_check_gc(Unit
*u
) {
471 return s
->from_etc_fstab
|| s
->from_proc_swaps
;
474 static int swap_load_proc_swaps(Manager
*m
) {
475 rewind(m
->proc_swaps
);
477 (void) fscanf(m
->proc_swaps
, "%*s %*s %*s %*s %*s\n");
480 char *dev
= NULL
, *d
;
483 if ((k
= fscanf(m
->proc_swaps
,
484 "%ms " /* device/file */
485 "%*s " /* type of swap */
486 "%*s " /* swap size */
488 "%i\n", /* priority */
489 &dev
, &prio
)) != 2) {
504 k
= swap_add_one(m
, d
, prio
, false, false, true);
514 static void swap_shutdown(Manager
*m
) {
518 fclose(m
->proc_swaps
);
519 m
->proc_swaps
= NULL
;
523 static const char* const swap_state_table
[_SWAP_STATE_MAX
] = {
524 [SWAP_DEAD
] = "dead",
525 [SWAP_ACTIVE
] = "active",
526 [SWAP_MAINTENANCE
] = "maintenance"
529 DEFINE_STRING_TABLE_LOOKUP(swap_state
, SwapState
);
531 static int swap_enumerate(Manager
*m
) {
536 if (!(m
->proc_swaps
= fopen("/proc/swaps", "re")))
539 if ((r
= swap_load_proc_swaps(m
)) < 0)
545 const UnitVTable swap_vtable
= {
548 .no_instances
= true,
556 .coldplug
= swap_coldplug
,
563 .serialize
= swap_serialize
,
564 .deserialize_item
= swap_deserialize_item
,
566 .active_state
= swap_active_state
,
567 .sub_state_to_string
= swap_sub_state_to_string
,
569 .check_gc
= swap_check_gc
,
571 .bus_message_handler
= bus_swap_message_handler
,
573 .enumerate
= swap_enumerate
,
574 .shutdown
= swap_shutdown