diff --git a/sys-utils/.gitignore b/sys-utils/.gitignore index d4e2e5d72..67cebb9eb 100644 --- a/sys-utils/.gitignore +++ b/sys-utils/.gitignore @@ -43,6 +43,7 @@ sparc32bash.8 sparc64.8 sparc.8 swapon +swapoff switch_root tunelp unshare diff --git a/sys-utils/Makefile.am b/sys-utils/Makefile.am index c85ab6f86..f2ba3a8e9 100644 --- a/sys-utils/Makefile.am +++ b/sys-utils/Makefile.am @@ -131,7 +131,7 @@ endif # BUILD_NEW_MOUNT if BUILD_SWAPON -sbin_PROGRAMS += swapon +sbin_PROGRAMS += swapon swapoff dist_man_MANS += swapoff.8 swapon.8 swapon_SOURCES = \ @@ -142,6 +142,10 @@ swapon_SOURCES = \ swapon_CFLAGS = $(AM_CFLAGS) -I$(ul_libmount_incdir) swapon_LDADD = $(ul_libmount_la) + +swapoff_SOURCES = swapoff.c swapon-common.c +swapoff_CFLAGS = $(AM_CFLAGS) -I$(ul_libmount_incdir) +swapoff_LDADD = $(ul_libmount_la) endif if BUILD_LSCPU diff --git a/sys-utils/swapoff.c b/sys-utils/swapoff.c new file mode 100644 index 000000000..2943e2bf6 --- /dev/null +++ b/sys-utils/swapoff.c @@ -0,0 +1,215 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_SYS_SWAP_H +# include +#endif + +#include "nls.h" +#include "c.h" +#include "closestream.h" + +#include "swapon-common.h" + +#ifndef SWAPON_HAS_TWO_ARGS +/* libc is insane, let's call the kernel */ +# include +# define swapoff(path) syscall(SYS_swapoff, path) +#endif + +static int verbose; +static int all; + +#define QUIET 1 +#define CANONIC 1 + +static int do_swapoff(const char *orig_special, int quiet, int canonic) +{ + const char *special = orig_special; + + if (verbose) + printf(_("swapoff %s\n"), orig_special); + + if (!canonic) { + special = mnt_resolve_spec(orig_special, mntcache); + if (!special) + return cannot_find(orig_special); + } + + if (swapoff(special) == 0) + return 0; /* success */ + + if (errno == EPERM) + errx(EXIT_FAILURE, _("Not superuser.")); + + if (!quiet || errno == ENOMEM) + warn(_("%s: swapoff failed"), orig_special); + + return -1; +} + +static int swapoff_by_label(const char *label, int quiet) +{ + const char *special = mnt_resolve_tag("LABEL", label, mntcache); + return special ? do_swapoff(special, quiet, CANONIC) : cannot_find(label); +} + +static int swapoff_by_uuid(const char *uuid, int quiet) +{ + const char *special = mnt_resolve_tag("UUID", uuid, mntcache); + return special ? do_swapoff(special, quiet, CANONIC) : cannot_find(uuid); +} + +static void usage(FILE *out, int n) +{ + fputs(_("\nUsage:\n"), out); + fprintf(out, _(" %s [options] []\n"), program_invocation_short_name); + + fputs(_("\nOptions:\n"), out); + fputs(_(" -a, --all disable all swaps from /proc/swaps\n" + " -h, --help display help and exit\n" + " -v, --verbose verbose mode\n" + " -V, --version display version and exit\n"), out); + + fputs(_("\nThe parameter:\n" \ + " -L