2006-12-06 17:25:32 -06:00
|
|
|
/*
|
|
|
|
* A swapon(8)/swapoff(8) for Linux 0.99.
|
|
|
|
* swapon.c,v 1.1.1.1 1993/11/18 08:40:51 jrs Exp
|
|
|
|
*/
|
|
|
|
|
2006-12-06 17:26:05 -06:00
|
|
|
#include <ctype.h>
|
2006-12-06 17:25:34 -06:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <mntent.h>
|
|
|
|
#include <errno.h>
|
2006-12-06 17:25:37 -06:00
|
|
|
#include <sys/stat.h>
|
2006-12-06 17:26:31 -06:00
|
|
|
#include "xmalloc.h"
|
2006-12-06 17:25:37 -06:00
|
|
|
#include "swap_constants.h"
|
2006-12-06 17:25:39 -06:00
|
|
|
#include "nls.h"
|
2007-05-09 08:38:16 -05:00
|
|
|
#include "fsprobe.h"
|
2007-02-09 10:15:39 -06:00
|
|
|
#include "realpath.h"
|
|
|
|
|
2006-12-06 17:25:34 -06:00
|
|
|
#define streq(s, t) (strcmp ((s), (t)) == 0)
|
|
|
|
|
|
|
|
#define _PATH_FSTAB "/etc/fstab"
|
|
|
|
#define PROC_SWAPS "/proc/swaps"
|
|
|
|
|
2007-05-15 13:46:20 -05:00
|
|
|
#ifdef SWAPON_HAS_TWO_ARGS
|
|
|
|
# include <asm/page.h>
|
|
|
|
# include <sys/swap.h>
|
|
|
|
#endif
|
|
|
|
|
2006-12-06 17:25:37 -06:00
|
|
|
#define SWAPON_NEEDS_TWO_ARGS
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2006-12-06 17:25:58 -06:00
|
|
|
#define QUIET 1
|
|
|
|
|
|
|
|
int all = 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
int verbose = 0;
|
2006-12-06 17:25:33 -06:00
|
|
|
int priority = -1; /* non-prioritized swap by default */
|
2007-05-14 07:31:28 -05:00
|
|
|
int mount_quiet = 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2006-12-06 17:26:05 -06:00
|
|
|
/* If true, don't complain if the device/file doesn't exist */
|
|
|
|
int ifexists = 0;
|
|
|
|
|
2006-12-06 17:26:31 -06:00
|
|
|
char *progname;
|
2006-12-06 17:25:58 -06:00
|
|
|
|
|
|
|
static struct option longswaponopts[] = {
|
|
|
|
/* swapon only */
|
|
|
|
{ "priority", required_argument, 0, 'p' },
|
2006-12-06 17:26:05 -06:00
|
|
|
{ "ifexists", 0, 0, 'e' },
|
2006-12-06 17:25:58 -06:00
|
|
|
{ "summary", 0, 0, 's' },
|
|
|
|
/* also for swapoff */
|
|
|
|
{ "all", 0, 0, 'a' },
|
|
|
|
{ "help", 0, 0, 'h' },
|
|
|
|
{ "verbose", 0, 0, 'v' },
|
|
|
|
{ "version", 0, 0, 'V' },
|
|
|
|
{ NULL, 0, 0, 0 }
|
2006-12-06 17:25:32 -06:00
|
|
|
};
|
|
|
|
|
2006-12-06 17:25:58 -06:00
|
|
|
static struct option *longswapoffopts = &longswaponopts[2];
|
|
|
|
|
2006-12-06 17:25:32 -06:00
|
|
|
static void
|
2006-12-06 17:25:58 -06:00
|
|
|
swapon_usage(FILE *fp, int n) {
|
|
|
|
fprintf(fp, _("usage: %s [-hV]\n"
|
2006-12-06 17:26:05 -06:00
|
|
|
" %s -a [-e] [-v]\n"
|
2006-12-06 17:26:31 -06:00
|
|
|
" %s [-v] [-p priority] special|LABEL=volume_name ...\n"
|
2006-12-06 17:25:58 -06:00
|
|
|
" %s [-s]\n"),
|
2006-12-06 17:26:31 -06:00
|
|
|
progname, progname, progname, progname);
|
2006-12-06 17:25:58 -06:00
|
|
|
exit(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
swapoff_usage(FILE *fp, int n) {
|
|
|
|
fprintf(fp, _("usage: %s [-hV]\n"
|
|
|
|
" %s -a [-v]\n"
|
|
|
|
" %s [-v] special ...\n"),
|
2006-12-06 17:26:31 -06:00
|
|
|
progname, progname, progname);
|
2006-12-06 17:25:58 -06:00
|
|
|
exit(n);
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2006-12-06 17:25:34 -06:00
|
|
|
#ifdef SWAPON_HAS_TWO_ARGS
|
|
|
|
#define SWAPON_NEEDS_TWO_ARGS
|
|
|
|
#endif
|
|
|
|
|
2007-05-15 13:46:13 -05:00
|
|
|
#if defined(SWAPON_NEEDS_TWO_ARGS) && !defined(SWAPON_HAS_TWO_ARGS)
|
2006-12-06 17:25:34 -06:00
|
|
|
/* We want a swapon with two args, but have an old libc.
|
|
|
|
Build the kernel call by hand. */
|
|
|
|
#include <linux/unistd.h>
|
|
|
|
static
|
|
|
|
_syscall2(int, swapon, const char *, path, int, flags);
|
|
|
|
static
|
|
|
|
_syscall1(int, swapoff, const char *, path);
|
2006-12-06 17:25:33 -06:00
|
|
|
#else
|
2006-12-06 17:25:34 -06:00
|
|
|
/* just do as libc says */
|
|
|
|
#include <unistd.h>
|
2006-12-06 17:25:33 -06:00
|
|
|
#endif
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2006-12-06 17:25:58 -06:00
|
|
|
/*
|
|
|
|
* contents of /proc/swaps
|
|
|
|
*/
|
|
|
|
static int numSwaps;
|
|
|
|
static char **swapFiles; /* array of swap file and partition names */
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2006-12-06 17:25:58 -06:00
|
|
|
static void
|
|
|
|
read_proc_swaps(void) {
|
|
|
|
FILE *swaps;
|
|
|
|
char line[1024];
|
2006-12-06 17:26:30 -06:00
|
|
|
char *p, **q;
|
2006-12-06 17:25:37 -06:00
|
|
|
|
2006-12-06 17:25:58 -06:00
|
|
|
numSwaps = 0;
|
|
|
|
swapFiles = NULL;
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2006-12-06 17:25:58 -06:00
|
|
|
swaps = fopen(PROC_SWAPS, "r");
|
|
|
|
if (swaps == NULL)
|
|
|
|
return; /* nothing wrong */
|
|
|
|
|
|
|
|
/* skip the first line */
|
|
|
|
fgets(line, sizeof(line), swaps);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2006-12-06 17:25:58 -06:00
|
|
|
while (fgets(line, sizeof(line), swaps)) {
|
|
|
|
/*
|
|
|
|
* Cut the line "swap_device ... more info" after device.
|
|
|
|
* This will fail with names with embedded spaces.
|
|
|
|
*/
|
|
|
|
for (p = line; *p && *p != ' '; p++);
|
|
|
|
*p = 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2006-12-06 17:26:30 -06:00
|
|
|
q = realloc(swapFiles, (numSwaps+1) * sizeof(*swapFiles));
|
|
|
|
if (q == NULL)
|
|
|
|
break;
|
|
|
|
swapFiles = q;
|
|
|
|
|
|
|
|
swapFiles[numSwaps++] = strdup(line);
|
2006-12-06 17:25:58 -06:00
|
|
|
}
|
|
|
|
fclose(swaps);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-12-06 17:26:31 -06:00
|
|
|
is_in_proc_swaps(const char *fname) {
|
2006-12-06 17:25:58 -06:00
|
|
|
int i;
|
2007-02-09 10:15:39 -06:00
|
|
|
char canonical[PATH_MAX + 2];
|
|
|
|
|
|
|
|
if (!myrealpath(fname, canonical, PATH_MAX + 1)) {
|
|
|
|
fprintf(stderr, _("%s: cannot canonicalize %s: %s\n"),
|
|
|
|
progname, fname, strerror(errno));
|
|
|
|
strncpy(canonical, fname, PATH_MAX + 1);
|
|
|
|
*(canonical + (PATH_MAX + 1)) = '\0';
|
|
|
|
}
|
2006-12-06 17:25:58 -06:00
|
|
|
|
|
|
|
for (i = 0; i < numSwaps; i++)
|
2007-02-09 10:15:39 -06:00
|
|
|
if (swapFiles[i] && !strcmp(canonical, swapFiles[i]))
|
2006-12-06 17:25:58 -06:00
|
|
|
return 1;
|
|
|
|
return 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2006-12-06 17:25:34 -06:00
|
|
|
static int
|
|
|
|
display_summary(void)
|
|
|
|
{
|
|
|
|
FILE *swaps;
|
2006-12-06 17:25:58 -06:00
|
|
|
char line[1024] ;
|
2006-12-06 17:25:34 -06:00
|
|
|
|
|
|
|
if ((swaps = fopen(PROC_SWAPS, "r")) == NULL) {
|
2006-12-06 17:25:39 -06:00
|
|
|
int errsv = errno;
|
2006-12-06 17:26:31 -06:00
|
|
|
fprintf(stderr, "%s: %s: %s\n", progname, PROC_SWAPS,
|
2006-12-06 17:25:58 -06:00
|
|
|
strerror(errsv));
|
2006-12-06 17:25:34 -06:00
|
|
|
return -1 ;
|
|
|
|
}
|
2006-12-06 17:25:49 -06:00
|
|
|
|
2006-12-06 17:25:58 -06:00
|
|
|
while (fgets(line, sizeof(line), swaps))
|
|
|
|
printf("%s", line);
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2006-12-06 17:25:49 -06:00
|
|
|
fclose(swaps);
|
2006-12-06 17:25:34 -06:00
|
|
|
return 0 ;
|
|
|
|
}
|
|
|
|
|
2006-12-06 17:25:58 -06:00
|
|
|
static int
|
2006-12-06 17:26:31 -06:00
|
|
|
do_swapon(const char *orig_special, int prio) {
|
2006-12-06 17:25:58 -06:00
|
|
|
int status;
|
|
|
|
struct stat st;
|
2006-12-06 17:26:31 -06:00
|
|
|
const char *special;
|
2006-12-06 17:25:58 -06:00
|
|
|
|
|
|
|
if (verbose)
|
2006-12-06 17:26:31 -06:00
|
|
|
printf(_("%s on %s\n"), progname, orig_special);
|
|
|
|
|
2007-05-10 04:21:35 -05:00
|
|
|
special = fsprobe_get_devname(orig_special);
|
2006-12-06 17:26:31 -06:00
|
|
|
if (!special) {
|
|
|
|
fprintf(stderr, _("%s: cannot find the device for %s\n"),
|
|
|
|
progname, orig_special);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-12-06 17:25:58 -06:00
|
|
|
|
|
|
|
if (stat(special, &st) < 0) {
|
|
|
|
int errsv = errno;
|
2006-12-06 17:26:31 -06:00
|
|
|
fprintf(stderr, _("%s: cannot stat %s: %s\n"),
|
|
|
|
progname, special, strerror(errsv));
|
2006-12-06 17:25:58 -06:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* people generally dislike this warning - now it is printed
|
|
|
|
only when `verbose' is set */
|
|
|
|
if (verbose) {
|
|
|
|
int permMask = (S_ISBLK(st.st_mode) ? 07007 : 07077);
|
|
|
|
|
|
|
|
if ((st.st_mode & permMask) != 0) {
|
2006-12-06 17:26:31 -06:00
|
|
|
fprintf(stderr, _("%s: warning: %s has "
|
2006-12-06 17:25:58 -06:00
|
|
|
"insecure permissions %04o, "
|
|
|
|
"%04o suggested\n"),
|
2006-12-06 17:26:31 -06:00
|
|
|
progname, special, st.st_mode & 07777,
|
2006-12-06 17:25:58 -06:00
|
|
|
~permMask & 0666);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test for holes by LBT */
|
|
|
|
if (S_ISREG(st.st_mode)) {
|
|
|
|
if (st.st_blocks * 512 < st.st_size) {
|
|
|
|
fprintf(stderr,
|
2006-12-06 17:26:31 -06:00
|
|
|
_("%s: Skipping file %s - it appears "
|
2006-12-06 17:25:58 -06:00
|
|
|
"to have holes.\n"),
|
2006-12-06 17:26:31 -06:00
|
|
|
progname, special);
|
2006-12-06 17:25:58 -06:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SWAPON_NEEDS_TWO_ARGS
|
|
|
|
{
|
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
#ifdef SWAP_FLAG_PREFER
|
|
|
|
if (prio >= 0) {
|
|
|
|
if (prio > SWAP_FLAG_PRIO_MASK)
|
|
|
|
prio = SWAP_FLAG_PRIO_MASK;
|
|
|
|
flags = SWAP_FLAG_PREFER
|
|
|
|
| ((prio & SWAP_FLAG_PRIO_MASK)
|
|
|
|
<< SWAP_FLAG_PRIO_SHIFT);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
status = swapon(special, flags);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
status = swapon(special);
|
|
|
|
#endif
|
|
|
|
if (status < 0) {
|
|
|
|
int errsv = errno;
|
2006-12-06 17:26:31 -06:00
|
|
|
fprintf(stderr, "%s: %s: %s\n",
|
|
|
|
progname, orig_special, strerror(errsv));
|
2006-12-06 17:25:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-12-06 17:26:31 -06:00
|
|
|
cannot_find(const char *special) {
|
|
|
|
fprintf(stderr, _("%s: cannot find the device for %s\n"),
|
|
|
|
progname, special);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
swapon_by_label(const char *label, int prio) {
|
2007-05-10 04:21:35 -05:00
|
|
|
const char *special = fsprobe_get_devname_by_label(label);
|
2006-12-06 17:26:31 -06:00
|
|
|
return special ? do_swapon(special, prio) : cannot_find(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
swapon_by_uuid(const char *uuid, int prio) {
|
2007-05-10 04:21:35 -05:00
|
|
|
const char *special = fsprobe_get_devname_by_uuid(uuid);
|
2006-12-06 17:26:31 -06:00
|
|
|
return special ? do_swapon(special, prio) : cannot_find(uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
do_swapoff(const char *orig_special, int quiet) {
|
|
|
|
const char *special;
|
|
|
|
|
2006-12-06 17:25:58 -06:00
|
|
|
if (verbose)
|
2006-12-06 17:26:31 -06:00
|
|
|
printf(_("%s on %s\n"), progname, orig_special);
|
|
|
|
|
2007-05-10 04:21:35 -05:00
|
|
|
special = fsprobe_get_devname(orig_special);
|
2006-12-06 17:26:31 -06:00
|
|
|
if (!special)
|
|
|
|
return cannot_find(orig_special);
|
2006-12-06 17:25:58 -06:00
|
|
|
|
|
|
|
if (swapoff(special) == 0)
|
|
|
|
return 0; /* success */
|
|
|
|
|
|
|
|
if (errno == EPERM) {
|
|
|
|
fprintf(stderr, _("Not superuser.\n"));
|
|
|
|
exit(1); /* any further swapoffs will also fail */
|
|
|
|
}
|
|
|
|
|
2006-12-06 17:26:19 -06:00
|
|
|
if (!quiet || errno == ENOMEM) {
|
2006-12-06 17:26:31 -06:00
|
|
|
fprintf(stderr, "%s: %s: %s\n",
|
|
|
|
progname, orig_special, strerror(errno));
|
2006-12-06 17:25:58 -06:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-12-06 17:26:31 -06:00
|
|
|
swapoff_by_label(const char *label, int quiet) {
|
2007-05-10 04:21:35 -05:00
|
|
|
const char *special = fsprobe_get_devname_by_label(label);
|
2006-12-06 17:26:31 -06:00
|
|
|
return special ? do_swapoff(special, quiet) : cannot_find(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
swapoff_by_uuid(const char *uuid, int quiet) {
|
2007-05-10 04:21:35 -05:00
|
|
|
const char *special = fsprobe_get_devname_by_uuid(uuid);
|
2006-12-06 17:26:31 -06:00
|
|
|
return special ? do_swapoff(special, quiet) : cannot_find(uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
swapon_all(void) {
|
2006-12-06 17:25:58 -06:00
|
|
|
FILE *fp;
|
|
|
|
struct mntent *fstab;
|
|
|
|
int status = 0;
|
|
|
|
|
2006-12-06 17:26:31 -06:00
|
|
|
read_proc_swaps();
|
|
|
|
|
|
|
|
fp = setmntent(_PATH_FSTAB, "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
int errsv = errno;
|
|
|
|
fprintf(stderr, _("%s: cannot open %s: %s\n"),
|
|
|
|
progname, _PATH_FSTAB, strerror(errsv));
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((fstab = getmntent(fp)) != NULL) {
|
|
|
|
const char *orig_special = fstab->mnt_fsname;
|
|
|
|
const char *special;
|
|
|
|
int skip = 0;
|
|
|
|
int pri = priority;
|
|
|
|
|
|
|
|
if (!streq(fstab->mnt_type, MNTTYPE_SWAP))
|
|
|
|
continue;
|
|
|
|
|
2007-05-10 04:21:35 -05:00
|
|
|
special = fsprobe_get_devname(orig_special);
|
2006-12-06 17:26:31 -06:00
|
|
|
if (!special)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!is_in_proc_swaps(special) &&
|
|
|
|
(!ifexists || !access(special, R_OK))) {
|
|
|
|
/* parse mount options; */
|
|
|
|
char *opt, *opts = strdup(fstab->mnt_opts);
|
|
|
|
|
|
|
|
for (opt = strtok(opts, ","); opt != NULL;
|
|
|
|
opt = strtok(NULL, ",")) {
|
|
|
|
if (strncmp(opt, "pri=", 4) == 0)
|
|
|
|
pri = atoi(opt+4);
|
|
|
|
if (strcmp(opt, "noauto") == 0)
|
|
|
|
skip = 1;
|
|
|
|
}
|
|
|
|
if (!skip)
|
|
|
|
status |= do_swapon(special, pri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char **llist = NULL;
|
|
|
|
static int llct = 0;
|
|
|
|
static const char **ulist = NULL;
|
|
|
|
static int ulct = 0;
|
|
|
|
|
|
|
|
static void addl(const char *label) {
|
|
|
|
llist = (const char **) xrealloc(llist, (++llct) * sizeof(char *));
|
|
|
|
llist[llct-1] = label;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addu(const char *uuid) {
|
|
|
|
ulist = (const char **) xrealloc(ulist, (++ulct) * sizeof(char *));
|
|
|
|
ulist[ulct-1] = uuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
main_swapon(int argc, char *argv[]) {
|
|
|
|
int status = 0;
|
|
|
|
int c, i;
|
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, "ahep:svVL:U:",
|
|
|
|
longswaponopts, NULL)) != -1) {
|
2006-12-06 17:25:58 -06:00
|
|
|
switch (c) {
|
|
|
|
case 'a': /* all */
|
|
|
|
++all;
|
|
|
|
break;
|
|
|
|
case 'h': /* help */
|
|
|
|
swapon_usage(stdout, 0);
|
|
|
|
break;
|
|
|
|
case 'p': /* priority */
|
|
|
|
priority = atoi(optarg);
|
|
|
|
break;
|
2006-12-06 17:26:31 -06:00
|
|
|
case 'L':
|
|
|
|
addl(optarg);
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
addu(optarg);
|
|
|
|
break;
|
2006-12-06 17:26:05 -06:00
|
|
|
case 'e': /* ifexists */
|
|
|
|
ifexists = 1;
|
|
|
|
break;
|
2006-12-06 17:25:58 -06:00
|
|
|
case 's': /* status report */
|
|
|
|
status = display_summary();
|
|
|
|
exit(status);
|
|
|
|
case 'v': /* be chatty */
|
|
|
|
++verbose;
|
|
|
|
break;
|
|
|
|
case 'V': /* version */
|
2006-12-06 17:26:58 -06:00
|
|
|
printf("%s: (%s)\n", progname, PACKAGE_STRING);
|
2006-12-06 17:25:58 -06:00
|
|
|
exit(0);
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
swapon_usage(stderr, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argv += optind;
|
|
|
|
|
2006-12-06 17:26:31 -06:00
|
|
|
if (!all && !llct && !ulct && *argv == NULL)
|
2006-12-06 17:25:58 -06:00
|
|
|
swapon_usage(stderr, 2);
|
|
|
|
|
2006-12-06 17:26:31 -06:00
|
|
|
if (ifexists && (!all || strcmp(progname, "swapon")))
|
|
|
|
swapon_usage(stderr, 1);
|
2006-12-06 17:26:05 -06:00
|
|
|
|
2006-12-06 17:26:31 -06:00
|
|
|
if (all)
|
|
|
|
status |= swapon_all();
|
2006-12-06 17:25:58 -06:00
|
|
|
|
2006-12-06 17:26:31 -06:00
|
|
|
for (i = 0; i < llct; i++)
|
|
|
|
status |= swapon_by_label(llist[i], priority);
|
2006-12-06 17:25:58 -06:00
|
|
|
|
2006-12-06 17:26:31 -06:00
|
|
|
for (i = 0; i < ulct; i++)
|
|
|
|
status |= swapon_by_uuid(ulist[i], priority);
|
2006-12-06 17:25:58 -06:00
|
|
|
|
|
|
|
while (*argv != NULL)
|
|
|
|
status |= do_swapon(*argv++, priority);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
main_swapoff(int argc, char *argv[]) {
|
|
|
|
FILE *fp;
|
|
|
|
struct mntent *fstab;
|
|
|
|
int status = 0;
|
|
|
|
int c, i;
|
|
|
|
|
2006-12-06 17:26:31 -06:00
|
|
|
while ((c = getopt_long(argc, argv, "ahvVL:U:",
|
2006-12-06 17:25:58 -06:00
|
|
|
longswapoffopts, NULL)) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'a': /* all */
|
|
|
|
++all;
|
|
|
|
break;
|
|
|
|
case 'h': /* help */
|
|
|
|
swapoff_usage(stdout, 0);
|
|
|
|
break;
|
|
|
|
case 'v': /* be chatty */
|
|
|
|
++verbose;
|
|
|
|
break;
|
|
|
|
case 'V': /* version */
|
2006-12-06 17:26:58 -06:00
|
|
|
printf("%s (%s)\n", progname, PACKAGE_STRING);
|
2006-12-06 17:25:58 -06:00
|
|
|
exit(0);
|
2006-12-06 17:26:31 -06:00
|
|
|
case 'L':
|
|
|
|
addl(optarg);
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
addu(optarg);
|
|
|
|
break;
|
2006-12-06 17:25:58 -06:00
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
swapoff_usage(stderr, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argv += optind;
|
|
|
|
|
2006-12-06 17:26:31 -06:00
|
|
|
if (!all && !llct && !ulct && *argv == NULL)
|
2006-12-06 17:25:58 -06:00
|
|
|
swapoff_usage(stderr, 2);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* swapoff any explicitly given arguments.
|
|
|
|
* Complain in case the swapoff call fails.
|
|
|
|
*/
|
2006-12-06 17:26:31 -06:00
|
|
|
for (i = 0; i < llct; i++)
|
|
|
|
status |= swapoff_by_label(llist[i], !QUIET);
|
|
|
|
|
|
|
|
for (i = 0; i < ulct; i++)
|
|
|
|
status |= swapoff_by_uuid(ulist[i], !QUIET);
|
|
|
|
|
2006-12-06 17:25:58 -06:00
|
|
|
while (*argv != NULL)
|
|
|
|
status |= do_swapoff(*argv++, !QUIET);
|
|
|
|
|
|
|
|
if (all) {
|
|
|
|
/*
|
2006-12-06 17:26:30 -06:00
|
|
|
* In case /proc/swaps exists, unswap stuff listed there.
|
2006-12-06 17:25:58 -06:00
|
|
|
* We are quiet but report errors in status.
|
|
|
|
* Errors might mean that /proc/swaps
|
|
|
|
* exists as ordinary file, not in procfs.
|
|
|
|
* do_swapoff() exits immediately on EPERM.
|
|
|
|
*/
|
|
|
|
read_proc_swaps();
|
|
|
|
for(i=0; i<numSwaps; i++)
|
|
|
|
status |= do_swapoff(swapFiles[i], QUIET);
|
|
|
|
|
|
|
|
/*
|
2006-12-06 17:26:30 -06:00
|
|
|
* Unswap stuff mentioned in /etc/fstab.
|
2006-12-06 17:25:58 -06:00
|
|
|
* Probably it was unmounted already, so errors are not bad.
|
|
|
|
* Doing swapoff -a twice should not give error messages.
|
|
|
|
*/
|
|
|
|
fp = setmntent(_PATH_FSTAB, "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
int errsv = errno;
|
|
|
|
fprintf(stderr, _("%s: cannot open %s: %s\n"),
|
2006-12-06 17:26:31 -06:00
|
|
|
progname, _PATH_FSTAB, strerror(errsv));
|
2006-12-06 17:25:58 -06:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
while ((fstab = getmntent(fp)) != NULL) {
|
|
|
|
if (streq(fstab->mnt_type, MNTTYPE_SWAP) &&
|
|
|
|
!is_in_proc_swaps(fstab->mnt_fsname))
|
|
|
|
do_swapoff(fstab->mnt_fsname, QUIET);
|
|
|
|
}
|
2006-12-06 17:26:16 -06:00
|
|
|
fclose(fp);
|
2006-12-06 17:25:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[]) {
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
|
|
|
|
2006-12-06 17:26:31 -06:00
|
|
|
progname = argv[0];
|
|
|
|
p = strrchr(progname, '/');
|
2006-12-06 17:25:58 -06:00
|
|
|
if (p)
|
2006-12-06 17:26:31 -06:00
|
|
|
progname = p+1;
|
2006-12-06 17:25:58 -06:00
|
|
|
|
2006-12-06 17:26:31 -06:00
|
|
|
if (streq(progname, "swapon"))
|
2006-12-06 17:25:58 -06:00
|
|
|
return main_swapon(argc, argv);
|
|
|
|
else
|
|
|
|
return main_swapoff(argc, argv);
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|