12 #include "closestream.h"
14 #include "swapprober.h"
15 #include "swapon-common.h"
17 #if !defined(HAVE_SWAPOFF) && defined(SYS_swapoff)
18 # include <sys/syscall.h>
19 # define swapoff(path) syscall(SYS_swapoff, path)
29 * This function works like mnt_resolve_tag(), but it's able to read UUID/LABEL
30 * from regular swap files too (according to entries in /proc/swaps). Note that
31 * mnt_resolve_tag() and mnt_resolve_spec() works with system visible block
34 static char *swapoff_resolve_tag(const char *name
, const char *value
,
35 struct libmnt_cache
*cache
)
38 struct libmnt_table
*tb
;
39 struct libmnt_iter
*itr
;
42 /* this is usual case for block devices (and it's really fast as it uses
43 * udev /dev/disk/by-* symlinks by default */
44 path
= mnt_resolve_tag(name
, value
, cache
);
48 /* try regular files from /proc/swaps */
53 itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
55 err(EXIT_FAILURE
, _("failed to initialize libmount iterator"));
57 while (tb
&& mnt_table_next_fs(tb
, itr
, &fs
) == 0) {
58 blkid_probe pr
= NULL
;
59 const char *src
= mnt_fs_get_source(fs
);
60 const char *type
= mnt_fs_get_swaptype(fs
);
61 const char *data
= NULL
;
63 if (!src
|| !type
|| strcmp(type
, "file") != 0)
65 pr
= get_swap_prober(src
);
68 blkid_probe_lookup_value(pr
, name
, &data
, NULL
);
69 if (data
&& strcmp(data
, value
) == 0)
80 static int do_swapoff(const char *orig_special
, int quiet
, int canonic
)
82 const char *special
= orig_special
;
85 printf(_("swapoff %s\n"), orig_special
);
90 special
= mnt_resolve_spec(orig_special
, mntcache
);
91 if (!special
&& blkid_parse_tag_string(orig_special
, &n
, &v
) == 0) {
92 special
= swapoff_resolve_tag(n
, v
, mntcache
);
97 return cannot_find(orig_special
);
100 if (swapoff(special
) == 0)
101 return 0; /* success */
104 errx(EXIT_FAILURE
, _("Not superuser."));
106 if (!quiet
|| errno
== ENOMEM
)
107 warn(_("%s: swapoff failed"), orig_special
);
112 static int swapoff_by(const char *name
, const char *value
, int quiet
)
114 const char *special
= swapoff_resolve_tag(name
, value
, mntcache
);
115 return special
? do_swapoff(special
, quiet
, CANONIC
) : cannot_find(value
);
118 static void __attribute__((__noreturn__
)) usage(void)
121 fputs(USAGE_HEADER
, out
);
122 fprintf(out
, _(" %s [options] [<spec>]\n"), program_invocation_short_name
);
124 fputs(USAGE_SEPARATOR
, out
);
125 fputs(_("Disable devices and files for paging and swapping.\n"), out
);
127 fputs(USAGE_OPTIONS
, out
);
128 fputs(_(" -a, --all disable all swaps from /proc/swaps\n"
129 " -v, --verbose verbose mode\n"), out
);
131 fputs(USAGE_SEPARATOR
, out
);
132 printf(USAGE_HELP_OPTIONS(24));
134 fputs(_("\nThe <spec> parameter:\n" \
135 " -L <label> LABEL of device to be used\n" \
136 " -U <uuid> UUID of device to be used\n" \
137 " LABEL=<label> LABEL of device to be used\n" \
138 " UUID=<uuid> UUID of device to be used\n" \
139 " <device> name of device to be used\n" \
140 " <file> name of file to be used\n"), out
);
142 printf(USAGE_MAN_TAIL("swapoff(8)"));
146 static int swapoff_all(void)
149 struct libmnt_table
*tb
;
150 struct libmnt_fs
*fs
;
151 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
154 err(EXIT_FAILURE
, _("failed to initialize libmount iterator"));
157 * In case /proc/swaps exists, unswap stuff listed there. We are quiet
158 * but report errors in status. Errors might mean that /proc/swaps
159 * exists as ordinary file, not in procfs. do_swapoff() exits
160 * immediately on EPERM.
164 while (tb
&& mnt_table_find_next_fs(tb
, itr
, match_swap
, NULL
, &fs
) == 0)
165 status
|= do_swapoff(mnt_fs_get_source(fs
), QUIET
, CANONIC
);
168 * Unswap stuff mentioned in /etc/fstab. Probably it was unmounted
169 * already, so errors are not bad. Doing swapoff -a twice should not
170 * give error messages.
173 mnt_reset_iter(itr
, MNT_ITER_FORWARD
);
175 while (tb
&& mnt_table_find_next_fs(tb
, itr
, match_swap
, NULL
, &fs
) == 0) {
176 if (!is_active_swap(mnt_fs_get_source(fs
)))
177 do_swapoff(mnt_fs_get_source(fs
), QUIET
, !CANONIC
);
184 int main(int argc
, char *argv
[])
189 static const struct option long_opts
[] = {
190 { "all", no_argument
, NULL
, 'a' },
191 { "help", no_argument
, NULL
, 'h' },
192 { "verbose", no_argument
, NULL
, 'v' },
193 { "version", no_argument
, NULL
, 'V' },
197 setlocale(LC_ALL
, "");
198 bindtextdomain(PACKAGE
, LOCALEDIR
);
200 close_stdout_atexit();
202 while ((c
= getopt_long(argc
, argv
, "ahvVL:U:",
203 long_opts
, NULL
)) != -1) {
208 case 'v': /* be chatty */
220 case 'V': /* version */
221 print_version(EXIT_SUCCESS
);
223 errtryhelp(EXIT_FAILURE
);
228 if (!all
&& !numof_labels() && !numof_uuids() && *argv
== NULL
) {
229 warnx(_("bad usage"));
230 errtryhelp(EXIT_FAILURE
);
234 mntcache
= mnt_new_cache();
236 for (i
= 0; i
< numof_labels(); i
++)
237 status
|= swapoff_by("LABEL", get_label(i
), !QUIET
);
239 for (i
= 0; i
< numof_uuids(); i
++)
240 status
|= swapoff_by("UUID", get_uuid(i
), !QUIET
);
242 while (*argv
!= NULL
)
243 status
|= do_swapoff(*argv
++, !QUIET
, !CANONIC
);
246 status
|= swapoff_all();
249 mnt_unref_cache(mntcache
);