From 6cf8d46ceefe9a7f40c3a07f2f30128b7c1f54fe Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 18 Apr 2012 11:43:42 +0200 Subject: swapoff: move code from swapon.c to swapoff.c * multipurpose binaries (symlinks) suck when used with autotools * swapoff shares small subset of the code with swapon * usage() and 'struct option' is unnecessary complex when shared * shorter and simple code is easer to maintain... Signed-off-by: Karel Zak --- sys-utils/swapoff.c | 215 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100644 sys-utils/swapoff.c (limited to 'sys-utils/swapoff.c') 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