2006-12-06 17:25:32 -06:00
|
|
|
/*
|
|
|
|
* A swapon(8)/swapoff(8) for Linux 0.99.
|
|
|
|
*/
|
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>
|
2007-06-13 04:52:55 -05:00
|
|
|
#include <unistd.h>
|
2008-03-20 03:24:03 -05:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <fcntl.h>
|
2008-11-27 05:32:56 -06:00
|
|
|
#include <stdint.h>
|
2010-12-16 18:10:36 -06:00
|
|
|
#include <ctype.h>
|
2009-03-02 07:21:03 -06:00
|
|
|
|
2012-03-27 05:13:24 -05:00
|
|
|
#include <blkid.h>
|
|
|
|
|
2008-11-27 05:32:56 -06:00
|
|
|
#include "bitops.h"
|
|
|
|
#include "blkdev.h"
|
2006-12-06 17:25:39 -06:00
|
|
|
#include "nls.h"
|
2007-11-28 07:35:24 -06:00
|
|
|
#include "pathnames.h"
|
2008-11-27 05:32:56 -06:00
|
|
|
#include "swapheader.h"
|
2010-03-10 16:59:33 -06:00
|
|
|
#include "mangle.h"
|
2010-12-14 17:14:18 -06:00
|
|
|
#include "canonicalize.h"
|
2011-07-17 23:33:20 -05:00
|
|
|
#include "xalloc.h"
|
2011-01-25 15:44:52 -06:00
|
|
|
#include "c.h"
|
2012-04-04 12:49:40 -05:00
|
|
|
#include "closestream.h"
|
2007-02-09 10:15:39 -06:00
|
|
|
|
2008-03-20 03:24:03 -05:00
|
|
|
#define PATH_MKSWAP "/sbin/mkswap"
|
|
|
|
|
2007-06-13 04:52:55 -05:00
|
|
|
#ifdef HAVE_SYS_SWAP_H
|
|
|
|
# include <sys/swap.h>
|
|
|
|
#endif
|
|
|
|
|
2010-10-24 19:32:47 -05:00
|
|
|
#ifndef SWAP_FLAG_DISCARD
|
|
|
|
# define SWAP_FLAG_DISCARD 0x10000 /* discard swap cluster after use */
|
|
|
|
#endif
|
|
|
|
|
2012-01-11 04:43:42 -06:00
|
|
|
#ifndef SWAP_FLAG_PREFER
|
|
|
|
# define SWAP_FLAG_PREFER 0x8000 /* set if swap priority specified */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SWAP_FLAG_PRIO_MASK
|
|
|
|
# define SWAP_FLAG_PRIO_MASK 0x7fff
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SWAP_FLAG_PRIO_SHIFT
|
|
|
|
# define SWAP_FLAG_PRIO_SHIFT 0
|
|
|
|
#endif
|
|
|
|
|
2007-06-13 04:52:55 -05:00
|
|
|
#ifndef SWAPON_HAS_TWO_ARGS
|
|
|
|
/* libc is insane, let's call the kernel */
|
|
|
|
# include <sys/syscall.h>
|
|
|
|
# define swapon(path, flags) syscall(SYS_swapon, path, flags)
|
|
|
|
# define swapoff(path) syscall(SYS_swapoff, path)
|
|
|
|
#endif
|
|
|
|
|
2006-12-06 17:25:34 -06:00
|
|
|
#define streq(s, t) (strcmp ((s), (t)) == 0)
|
|
|
|
|
2006-12-06 17:25:58 -06:00
|
|
|
#define QUIET 1
|
2007-08-07 05:03:53 -05:00
|
|
|
#define CANONIC 1
|
2006-12-06 17:25:58 -06:00
|
|
|
|
2008-11-27 05:32:56 -06:00
|
|
|
#define MAX_PAGESIZE (64 * 1024)
|
|
|
|
|
2009-03-02 06:00:49 -06:00
|
|
|
enum {
|
|
|
|
SIG_SWAPSPACE = 1,
|
|
|
|
SIG_SWSUSPEND
|
|
|
|
};
|
|
|
|
|
2009-03-02 09:41:03 -06:00
|
|
|
#define SWAP_SIGNATURE "SWAPSPACE2"
|
|
|
|
#define SWAP_SIGNATURE_SZ (sizeof(SWAP_SIGNATURE) - 1)
|
|
|
|
|
2011-01-21 09:44:18 -06:00
|
|
|
static int all;
|
|
|
|
static int priority = -1; /* non-prioritized swap by default */
|
|
|
|
static int discard;
|
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 */
|
2011-01-21 09:44:18 -06:00
|
|
|
static int ifexists;
|
|
|
|
static int fixpgsz;
|
2009-03-02 07:21:03 -06:00
|
|
|
|
2011-01-21 09:44:18 -06:00
|
|
|
static int verbose;
|
|
|
|
static char *progname;
|
2006-12-06 17:26:05 -06:00
|
|
|
|
2011-01-21 09:44:18 -06:00
|
|
|
static const struct option longswaponopts[] = {
|
2006-12-06 17:25:58 -06:00
|
|
|
/* swapon only */
|
|
|
|
{ "priority", required_argument, 0, 'p' },
|
2010-10-24 19:32:47 -05:00
|
|
|
{ "discard", 0, 0, 'd' },
|
2006-12-06 17:26:05 -06:00
|
|
|
{ "ifexists", 0, 0, 'e' },
|
2006-12-06 17:25:58 -06:00
|
|
|
{ "summary", 0, 0, 's' },
|
2009-03-03 07:53:27 -06:00
|
|
|
{ "fixpgsz", 0, 0, 'f' },
|
2006-12-06 17:25:58 -06:00
|
|
|
/* 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
|
|
|
};
|
|
|
|
|
2011-01-21 09:44:18 -06:00
|
|
|
static const struct option *longswapoffopts = &longswaponopts[4];
|
2006-12-06 17:25:58 -06:00
|
|
|
|
2007-08-07 05:03:53 -05:00
|
|
|
static int cannot_find(const char *special);
|
|
|
|
|
2007-11-26 06:07:40 -06:00
|
|
|
#define PRINT_USAGE_SPECIAL(_fp) \
|
2011-08-16 07:25:05 -05:00
|
|
|
fputs(_("\nThe <spec> parameter:\n" \
|
|
|
|
" -L <label> LABEL of device to be used\n" \
|
|
|
|
" -U <uuid> UUID of device to be used\n" \
|
|
|
|
" LABEL=<label> LABEL of device to be used\n" \
|
|
|
|
" UUID=<uuid> UUID of device to be used\n" \
|
|
|
|
" <device> name of device to be used\n" \
|
|
|
|
" <file> name of file to be used\n\n"), _fp)
|
2007-11-26 06:07:40 -06:00
|
|
|
|
2006-12-06 17:25:32 -06:00
|
|
|
static void
|
2011-08-16 07:25:05 -05:00
|
|
|
swapon_usage(FILE *out, int n) {
|
|
|
|
fputs(_("\nUsage:\n"), out);
|
|
|
|
fprintf(out, _(" %s [options] [<spec>]\n"), progname);
|
|
|
|
|
|
|
|
fputs(_("\nOptions:\n"), out);
|
|
|
|
fputs(_(" -a, --all enable all swaps from /etc/fstab\n"
|
|
|
|
" -d, --discard discard freed pages before they are reused\n"
|
|
|
|
" -e, --ifexists silently skip devices that do not exis\n"
|
|
|
|
" -f, --fixpgsz reinitialize the swap space if necessary\n"
|
|
|
|
" -h, --help display help and exit\n"
|
2012-02-14 05:13:14 -06:00
|
|
|
" -p, --priority <prio> specify the priority of the swap device\n"
|
2011-08-16 07:25:05 -05:00
|
|
|
" -s, --summary display summary about used swap devices and exit\n"
|
|
|
|
" -v, --verbose verbose mode\n"
|
|
|
|
" -V, --version display version and exit\n"), out);
|
|
|
|
|
|
|
|
PRINT_USAGE_SPECIAL(out);
|
2007-11-26 06:07:40 -06:00
|
|
|
|
2006-12-06 17:25:58 -06:00
|
|
|
exit(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-08-16 07:25:05 -05:00
|
|
|
swapoff_usage(FILE *out, int n) {
|
|
|
|
fputs(_("\nUsage:\n"), out);
|
|
|
|
fprintf(out, _(" %s [options] [<spec>]\n"), progname);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
PRINT_USAGE_SPECIAL(out);
|
2007-11-26 06:07:40 -06:00
|
|
|
|
2006-12-06 17:25:58 -06:00
|
|
|
exit(n);
|
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;
|
2010-03-10 16:08:26 -06:00
|
|
|
size_t sz;
|
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
|
|
|
|
2007-11-28 07:35:24 -06:00
|
|
|
swaps = fopen(_PATH_PROC_SWAPS, "r");
|
2006-12-06 17:25:58 -06:00
|
|
|
if (swaps == NULL)
|
|
|
|
return; /* nothing wrong */
|
|
|
|
|
|
|
|
/* skip the first line */
|
2007-10-03 16:15:32 -05:00
|
|
|
if (!fgets(line, sizeof(line), swaps)) {
|
2009-10-13 05:05:19 -05:00
|
|
|
/* do not whine about an empty file */
|
|
|
|
if (ferror(swaps))
|
|
|
|
warn(_("%s: unexpected file format"), _PATH_PROC_SWAPS);
|
2007-10-03 16:15:32 -05:00
|
|
|
fclose(swaps);
|
|
|
|
return;
|
|
|
|
}
|
2009-10-13 05:05:19 -05:00
|
|
|
/* make sure the first line is the header */
|
|
|
|
if (line[0] != '\0' && strncmp(line, "Filename\t", 9))
|
|
|
|
goto valid_first_line;
|
|
|
|
|
2006-12-06 17:25:58 -06:00
|
|
|
while (fgets(line, sizeof(line), swaps)) {
|
2009-10-13 05:05:19 -05:00
|
|
|
valid_first_line:
|
2006-12-06 17:25:58 -06:00
|
|
|
/*
|
|
|
|
* Cut the line "swap_device ... more info" after device.
|
|
|
|
* This will fail with names with embedded spaces.
|
|
|
|
*/
|
|
|
|
for (p = line; *p && *p != ' '; p++);
|
2010-03-10 16:08:26 -06:00
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
/* the kernel can use " (deleted)" suffix for paths
|
|
|
|
* in /proc/swaps, we have to remove this junk.
|
|
|
|
*/
|
|
|
|
sz = strlen(line);
|
2010-04-02 04:43:36 -05:00
|
|
|
if (sz > PATH_DELETED_SUFFIX_SZ) {
|
|
|
|
p = line + (sz - PATH_DELETED_SUFFIX_SZ);
|
|
|
|
if (strcmp(p, PATH_DELETED_SUFFIX) == 0)
|
2010-03-10 16:08:26 -06:00
|
|
|
*p = '\0';
|
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2012-03-20 03:44:40 -05:00
|
|
|
q = xrealloc(swapFiles, (numSwaps+1) * sizeof(*swapFiles));
|
2006-12-06 17:26:30 -06:00
|
|
|
swapFiles = q;
|
|
|
|
|
2010-11-25 16:22:53 -06:00
|
|
|
if ((p = unmangle(line, NULL)) == NULL)
|
2010-12-14 17:14:18 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
swapFiles[numSwaps++] = canonicalize_path(p);
|
|
|
|
free(p);
|
2006-12-06 17:25:58 -06:00
|
|
|
}
|
|
|
|
fclose(swaps);
|
|
|
|
}
|
|
|
|
|
2010-12-14 17:14:18 -06:00
|
|
|
/* note that swapFiles are always canonicalized */
|
2006-12-06 17:25:58 -06:00
|
|
|
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;
|
|
|
|
|
|
|
|
for (i = 0; i < numSwaps; i++)
|
2008-03-20 08:45:33 -05:00
|
|
|
if (swapFiles[i] && !strcmp(fname, 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)
|
|
|
|
{
|
2010-12-16 18:10:36 -06:00
|
|
|
FILE *swaps;
|
|
|
|
char line[1024] ;
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2010-12-16 18:10:36 -06:00
|
|
|
if ((swaps = fopen(_PATH_PROC_SWAPS, "r")) == NULL) {
|
|
|
|
warn(_("%s: open failed"), _PATH_PROC_SWAPS);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-12-06 17:25:49 -06:00
|
|
|
|
2010-12-16 18:10:36 -06:00
|
|
|
while (fgets(line, sizeof(line), swaps)) {
|
|
|
|
char *p, *dev, *cn;
|
|
|
|
if (!strncmp(line, "Filename\t", 9)) {
|
|
|
|
printf("%s", line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (p = line; *p && *p != ' '; p++);
|
|
|
|
*p = '\0';
|
|
|
|
for (++p; *p && isblank((unsigned int) *p); p++);
|
|
|
|
|
2010-11-25 16:22:53 -06:00
|
|
|
dev = unmangle(line, NULL);
|
2010-12-16 18:10:36 -06:00
|
|
|
if (!dev)
|
|
|
|
continue;
|
|
|
|
cn = canonicalize_path(dev);
|
|
|
|
if (cn)
|
2011-08-09 06:11:56 -05:00
|
|
|
printf("%-39s %s", cn, p);
|
2010-12-16 18:10:36 -06:00
|
|
|
free(dev);
|
|
|
|
free(cn);
|
|
|
|
}
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2010-12-16 18:10:36 -06:00
|
|
|
fclose(swaps);
|
|
|
|
return 0 ;
|
2006-12-06 17:25:34 -06:00
|
|
|
}
|
|
|
|
|
2008-03-20 03:24:03 -05:00
|
|
|
/* calls mkswap */
|
|
|
|
static int
|
2012-03-27 05:13:24 -05:00
|
|
|
swap_reinitialize(const char *device, const char *label, const char *uuid)
|
|
|
|
{
|
2008-03-20 03:24:03 -05:00
|
|
|
pid_t pid;
|
|
|
|
int status, ret;
|
|
|
|
char *cmd[7];
|
|
|
|
int idx=0;
|
|
|
|
|
2009-03-03 07:53:27 -06:00
|
|
|
warnx(_("%s: reinitializing the swap."), device);
|
|
|
|
|
2008-03-20 03:24:03 -05:00
|
|
|
switch((pid=fork())) {
|
|
|
|
case -1: /* fork error */
|
2009-03-02 08:16:21 -06:00
|
|
|
warn(_("fork failed"));
|
2008-03-20 03:24:03 -05:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
case 0: /* child */
|
|
|
|
cmd[idx++] = PATH_MKSWAP;
|
|
|
|
if (label && *label) {
|
|
|
|
cmd[idx++] = "-L";
|
|
|
|
cmd[idx++] = (char *) label;
|
|
|
|
}
|
|
|
|
if (uuid && *uuid) {
|
|
|
|
cmd[idx++] = "-U";
|
|
|
|
cmd[idx++] = (char *) uuid;
|
|
|
|
}
|
|
|
|
cmd[idx++] = (char *) device;
|
|
|
|
cmd[idx++] = NULL;
|
|
|
|
execv(cmd[0], cmd);
|
2009-03-02 08:16:21 -06:00
|
|
|
err(EXIT_FAILURE, _("execv failed"));
|
2008-03-20 03:24:03 -05:00
|
|
|
|
|
|
|
default: /* parent */
|
|
|
|
do {
|
|
|
|
if ((ret = waitpid(pid, &status, 0)) < 0
|
|
|
|
&& errno == EINTR)
|
|
|
|
continue;
|
|
|
|
else if (ret < 0) {
|
2009-03-02 08:16:21 -06:00
|
|
|
warn(_("waitpid failed"));
|
2008-03-20 03:24:03 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
/* mkswap returns: 0=suss, 1=error */
|
|
|
|
if (WIFEXITED(status) && WEXITSTATUS(status)==0)
|
|
|
|
return 0; /* ok */
|
2012-01-31 13:59:54 -06:00
|
|
|
break;
|
2008-03-20 03:24:03 -05:00
|
|
|
}
|
|
|
|
return -1; /* error */
|
|
|
|
}
|
|
|
|
|
2009-03-02 09:41:03 -06:00
|
|
|
static int
|
|
|
|
swap_rewrite_signature(const char *devname, unsigned int pagesize)
|
|
|
|
{
|
|
|
|
int fd, rc = -1;
|
|
|
|
|
|
|
|
fd = open(devname, O_WRONLY);
|
|
|
|
if (fd == -1) {
|
|
|
|
warn(_("%s: open failed"), devname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-03-05 13:59:30 -06:00
|
|
|
if (lseek(fd, pagesize - SWAP_SIGNATURE_SZ, SEEK_SET) < 0) {
|
|
|
|
warn(_("%s: lseek failed"), devname);
|
2009-03-02 09:41:03 -06:00
|
|
|
goto err;
|
2009-03-05 13:59:30 -06:00
|
|
|
}
|
2009-03-02 09:41:03 -06:00
|
|
|
|
|
|
|
if (write(fd, (void *) SWAP_SIGNATURE,
|
2009-03-05 13:59:30 -06:00
|
|
|
SWAP_SIGNATURE_SZ) != SWAP_SIGNATURE_SZ) {
|
|
|
|
warn(_("%s: write signature failed"), devname);
|
2009-03-02 09:41:03 -06:00
|
|
|
goto err;
|
2009-03-05 13:59:30 -06:00
|
|
|
}
|
2009-03-02 09:41:03 -06:00
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
err:
|
|
|
|
close(fd);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-03-02 06:00:49 -06:00
|
|
|
static int
|
|
|
|
swap_detect_signature(const char *buf, int *sig)
|
2008-11-27 05:32:56 -06:00
|
|
|
{
|
2009-03-02 06:00:49 -06:00
|
|
|
if (memcmp(buf, "SWAP-SPACE", 10) == 0 ||
|
|
|
|
memcmp(buf, "SWAPSPACE2", 10) == 0)
|
|
|
|
*sig = SIG_SWAPSPACE;
|
|
|
|
|
|
|
|
else if (memcmp(buf, "S1SUSPEND", 9) == 0 ||
|
|
|
|
memcmp(buf, "S2SUSPEND", 9) == 0 ||
|
|
|
|
memcmp(buf, "ULSUSPEND", 9) == 0 ||
|
2010-11-29 03:26:18 -06:00
|
|
|
memcmp(buf, "\xed\xc3\x02\xe9\x98\x56\xe5\x0c", 8) == 0 ||
|
|
|
|
memcmp(buf, "LINHIB0001", 10) == 0)
|
2009-03-02 06:00:49 -06:00
|
|
|
*sig = SIG_SWSUSPEND;
|
|
|
|
else
|
|
|
|
return 0;
|
2008-11-27 05:32:56 -06:00
|
|
|
|
2009-03-02 06:00:49 -06:00
|
|
|
return 1;
|
2008-11-27 05:32:56 -06:00
|
|
|
}
|
|
|
|
|
2009-03-02 06:00:49 -06:00
|
|
|
static char *
|
|
|
|
swap_get_header(int fd, int *sig, unsigned int *pagesize)
|
2008-11-27 05:32:56 -06:00
|
|
|
{
|
|
|
|
char *buf;
|
2009-03-02 06:00:49 -06:00
|
|
|
ssize_t datasz;
|
|
|
|
unsigned int page;
|
2008-11-27 05:32:56 -06:00
|
|
|
|
2009-03-02 06:00:49 -06:00
|
|
|
*pagesize = 0;
|
|
|
|
*sig = 0;
|
2008-11-27 05:32:56 -06:00
|
|
|
|
2011-07-17 23:33:20 -05:00
|
|
|
buf = xmalloc(MAX_PAGESIZE);
|
2008-11-27 05:32:56 -06:00
|
|
|
|
|
|
|
datasz = read(fd, buf, MAX_PAGESIZE);
|
2009-03-02 06:00:49 -06:00
|
|
|
if (datasz == (ssize_t) -1)
|
|
|
|
goto err;
|
2008-11-27 05:32:56 -06:00
|
|
|
|
|
|
|
for (page = 0x1000; page <= MAX_PAGESIZE; page <<= 1) {
|
|
|
|
/* skip 32k pagesize since this does not seem to
|
|
|
|
* be supported */
|
|
|
|
if (page == 0x8000)
|
|
|
|
continue;
|
|
|
|
/* the smallest swap area is PAGE_SIZE*10, it means
|
|
|
|
* 40k, that's less than MAX_PAGESIZE */
|
2011-08-01 06:40:41 -05:00
|
|
|
if (datasz < 0 || (size_t) datasz < (page - SWAP_SIGNATURE_SZ))
|
2008-11-27 05:32:56 -06:00
|
|
|
break;
|
2009-03-02 09:41:03 -06:00
|
|
|
if (swap_detect_signature(buf + page - SWAP_SIGNATURE_SZ, sig)) {
|
2009-03-02 06:00:49 -06:00
|
|
|
*pagesize = page;
|
2008-11-27 05:32:56 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-02 06:00:49 -06:00
|
|
|
if (*pagesize)
|
|
|
|
return buf;
|
2008-11-27 05:32:56 -06:00
|
|
|
err:
|
2009-03-02 06:00:49 -06:00
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns real size of swap space */
|
|
|
|
unsigned long long
|
|
|
|
swap_get_size(const char *hdr, const char *devname, unsigned int pagesize)
|
|
|
|
{
|
|
|
|
unsigned int last_page = 0;
|
|
|
|
int swap_version = 0;
|
|
|
|
int flip = 0;
|
|
|
|
struct swap_header_v1_2 *s;
|
|
|
|
|
|
|
|
s = (struct swap_header_v1_2 *) hdr;
|
|
|
|
if (s->version == 1) {
|
|
|
|
swap_version = 1;
|
|
|
|
last_page = s->last_page;
|
|
|
|
} else if (swab32(s->version) == 1) {
|
|
|
|
flip = 1;
|
|
|
|
swap_version = 1;
|
|
|
|
last_page = swab32(s->last_page);
|
|
|
|
}
|
|
|
|
if (verbose)
|
2009-12-10 06:04:41 -06:00
|
|
|
warnx(_("%s: found swap signature: version %d, "
|
|
|
|
"page-size %d, %s byte order"),
|
2009-03-02 06:00:49 -06:00
|
|
|
devname,
|
|
|
|
swap_version,
|
2009-12-10 06:04:41 -06:00
|
|
|
pagesize / 1024,
|
|
|
|
flip ? _("different") : _("same"));
|
2009-03-02 06:00:49 -06:00
|
|
|
|
2010-02-16 06:48:09 -06:00
|
|
|
return ((unsigned long long) last_page + 1) * pagesize;
|
2008-11-27 05:32:56 -06:00
|
|
|
}
|
|
|
|
|
2012-03-27 05:13:24 -05:00
|
|
|
void
|
|
|
|
swap_get_info(const char *hdr, char **label, char **uuid)
|
|
|
|
{
|
|
|
|
struct swap_header_v1_2 *s = (struct swap_header_v1_2 *) hdr;
|
|
|
|
|
|
|
|
if (s && *s->volume_name && label)
|
|
|
|
*label = xstrdup(s->volume_name);
|
|
|
|
|
|
|
|
if (s && *s->uuid && uuid) {
|
|
|
|
const unsigned char *u = s->uuid;
|
|
|
|
char str[37];
|
|
|
|
|
|
|
|
snprintf(str, sizeof(str),
|
|
|
|
"%02x%02x%02x%02x-"
|
|
|
|
"%02x%02x-%02x%02x-"
|
|
|
|
"%02x%02x-%02x%02x%02x%02x%02x%02x",
|
|
|
|
u[0], u[1], u[2], u[3],
|
|
|
|
u[4], u[5], u[6], u[7],
|
|
|
|
u[8], u[9], u[10], u[11], u[12], u[13], u[14], u[15]);
|
|
|
|
*uuid = xstrdup(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-06 17:25:58 -06:00
|
|
|
static int
|
2009-03-02 09:12:26 -06:00
|
|
|
swapon_checks(const char *special)
|
|
|
|
{
|
2006-12-06 17:25:58 -06:00
|
|
|
struct stat st;
|
2009-03-02 09:12:26 -06:00
|
|
|
int fd = -1, sig;
|
|
|
|
char *hdr = NULL;
|
2009-03-02 06:00:49 -06:00
|
|
|
unsigned int pagesize;
|
|
|
|
unsigned long long devsize = 0;
|
2006-12-06 17:25:58 -06:00
|
|
|
|
|
|
|
if (stat(special, &st) < 0) {
|
2009-03-02 08:16:21 -06:00
|
|
|
warn(_("%s: stat failed"), special);
|
2009-03-02 09:12:26 -06:00
|
|
|
goto err;
|
2006-12-06 17:25:58 -06:00
|
|
|
}
|
|
|
|
|
2009-03-02 04:24:03 -06:00
|
|
|
/* 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);
|
|
|
|
|
2009-03-02 08:16:21 -06:00
|
|
|
if ((st.st_mode & permMask) != 0)
|
|
|
|
warnx(_("%s: insecure permissions %04o, %04o suggested."),
|
|
|
|
special, st.st_mode & 07777,
|
2009-03-02 04:24:03 -06:00
|
|
|
~permMask & 0666);
|
2010-10-07 02:03:31 -05:00
|
|
|
|
|
|
|
if (S_ISREG(st.st_mode) && st.st_uid != 0)
|
|
|
|
warnx(_("%s: insecure file owner %d, 0 (root) suggested."),
|
|
|
|
special, st.st_uid);
|
2009-03-02 04:24:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* test for holes by LBT */
|
|
|
|
if (S_ISREG(st.st_mode)) {
|
|
|
|
if (st.st_blocks * 512 < st.st_size) {
|
2009-03-02 08:16:21 -06:00
|
|
|
warnx(_("%s: skipping - it appears to have holes."),
|
|
|
|
special);
|
2009-03-02 09:12:26 -06:00
|
|
|
goto err;
|
2009-03-02 04:24:03 -06:00
|
|
|
}
|
2009-03-02 06:00:49 -06:00
|
|
|
devsize = st.st_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(special, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
2009-03-02 08:16:21 -06:00
|
|
|
warn(_("%s: open failed"), special);
|
2009-03-02 09:12:26 -06:00
|
|
|
goto err;
|
2009-03-02 06:00:49 -06:00
|
|
|
}
|
|
|
|
|
2009-03-02 09:12:26 -06:00
|
|
|
if (S_ISBLK(st.st_mode) && blkdev_get_size(fd, &devsize)) {
|
|
|
|
warn(_("%s: get size failed"), special);
|
|
|
|
goto err;
|
2009-03-02 06:00:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
hdr = swap_get_header(fd, &sig, &pagesize);
|
|
|
|
if (!hdr) {
|
2009-03-02 08:16:21 -06:00
|
|
|
warn(_("%s: read swap header failed"), special);
|
2009-03-02 09:12:26 -06:00
|
|
|
goto err;
|
2009-03-02 04:24:03 -06:00
|
|
|
}
|
|
|
|
|
2009-03-02 06:00:49 -06:00
|
|
|
if (sig == SIG_SWAPSPACE && pagesize) {
|
|
|
|
unsigned long long swapsize =
|
|
|
|
swap_get_size(hdr, special, pagesize);
|
2011-08-01 06:40:41 -05:00
|
|
|
int syspg = getpagesize();
|
|
|
|
|
2008-11-27 05:32:56 -06:00
|
|
|
if (verbose)
|
2010-06-10 07:29:27 -05:00
|
|
|
warnx(_("%s: pagesize=%d, swapsize=%llu, devsize=%llu"),
|
2009-03-02 06:00:49 -06:00
|
|
|
special, pagesize, swapsize, devsize);
|
|
|
|
|
|
|
|
if (swapsize > devsize) {
|
|
|
|
if (verbose)
|
2009-03-02 08:16:21 -06:00
|
|
|
warnx(_("%s: last_page 0x%08llx is larger"
|
|
|
|
" than actual size of swapspace"),
|
2009-03-02 06:00:49 -06:00
|
|
|
special, swapsize);
|
2011-08-01 06:40:41 -05:00
|
|
|
} else if (syspg < 0 || (unsigned) syspg != pagesize) {
|
2009-03-03 07:53:27 -06:00
|
|
|
if (fixpgsz) {
|
2012-03-27 05:13:24 -05:00
|
|
|
char *label = NULL, *uuid = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
swap_get_info(hdr, &label, &uuid);
|
|
|
|
|
2009-03-05 13:37:31 -06:00
|
|
|
warnx(_("%s: swap format pagesize does not match."),
|
2009-03-03 07:53:27 -06:00
|
|
|
special);
|
2012-03-27 05:13:24 -05:00
|
|
|
rc = swap_reinitialize(special, label, uuid);
|
|
|
|
free(label);
|
|
|
|
free(uuid);
|
|
|
|
if (rc < 0)
|
2009-03-03 07:53:27 -06:00
|
|
|
goto err;
|
|
|
|
} else
|
2009-03-05 13:37:31 -06:00
|
|
|
warnx(_("%s: swap format pagesize does not match. "
|
|
|
|
"(Use --fixpgsz to reinitialize it.)"),
|
2009-03-03 07:53:27 -06:00
|
|
|
special);
|
2009-03-02 06:00:49 -06:00
|
|
|
}
|
|
|
|
} else if (sig == SIG_SWSUSPEND) {
|
|
|
|
/* We have to reinitialize swap with old (=useless) software suspend
|
|
|
|
* data. The problem is that if we don't do it, then we get data
|
|
|
|
* corruption the next time an attempt at unsuspending is made.
|
|
|
|
*/
|
2009-03-02 08:16:21 -06:00
|
|
|
warnx(_("%s: software suspend data detected. "
|
2009-03-02 09:41:03 -06:00
|
|
|
"Rewriting the swap signature."),
|
2009-03-02 08:16:21 -06:00
|
|
|
special);
|
2009-03-02 09:41:03 -06:00
|
|
|
if (swap_rewrite_signature(special, pagesize) < 0)
|
2009-03-02 09:12:26 -06:00
|
|
|
goto err;
|
2008-03-20 03:24:03 -05:00
|
|
|
}
|
|
|
|
|
2009-03-02 09:12:26 -06:00
|
|
|
free(hdr);
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
free(hdr);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-12-06 17:25:58 -06:00
|
|
|
|
2009-03-02 09:12:26 -06:00
|
|
|
static int
|
2010-10-24 19:32:47 -05:00
|
|
|
do_swapon(const char *orig_special, int prio, int fl_discard, int canonic) {
|
2009-03-02 09:12:26 -06:00
|
|
|
int status;
|
|
|
|
const char *special = orig_special;
|
|
|
|
int flags = 0;
|
2006-12-06 17:25:58 -06:00
|
|
|
|
2009-03-02 09:12:26 -06:00
|
|
|
if (verbose)
|
|
|
|
printf(_("%s on %s\n"), progname, orig_special);
|
|
|
|
|
|
|
|
if (!canonic) {
|
2012-03-27 05:13:24 -05:00
|
|
|
special = blkid_evaluate_spec(orig_special, NULL);
|
2009-03-02 09:12:26 -06:00
|
|
|
if (!special)
|
|
|
|
return cannot_find(orig_special);
|
2006-12-06 17:25:58 -06:00
|
|
|
}
|
2007-06-13 04:52:55 -05:00
|
|
|
|
2009-03-02 09:12:26 -06:00
|
|
|
if (swapon_checks(special))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
#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
|
2010-10-24 19:32:47 -05:00
|
|
|
if (fl_discard)
|
|
|
|
flags |= SWAP_FLAG_DISCARD;
|
|
|
|
|
2009-03-02 09:12:26 -06:00
|
|
|
status = swapon(special, flags);
|
2009-03-02 08:16:21 -06:00
|
|
|
if (status < 0)
|
|
|
|
warn(_("%s: swapon failed"), orig_special);
|
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) {
|
2009-03-02 08:16:21 -06:00
|
|
|
warnx(_("cannot find the device for %s"), special);
|
2006-12-06 17:26:31 -06:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-10-24 19:32:47 -05:00
|
|
|
swapon_by_label(const char *label, int prio, int dsc) {
|
2012-03-27 05:13:24 -05:00
|
|
|
const char *special = blkid_evaluate_tag("LABEL", label, NULL);
|
2010-10-24 19:32:47 -05:00
|
|
|
return special ? do_swapon(special, prio, dsc, CANONIC) :
|
|
|
|
cannot_find(label);
|
2006-12-06 17:26:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-10-24 19:32:47 -05:00
|
|
|
swapon_by_uuid(const char *uuid, int prio, int dsc) {
|
2012-03-27 05:13:24 -05:00
|
|
|
const char *special = blkid_evaluate_tag("UUID", uuid, NULL);
|
2010-10-24 19:32:47 -05:00
|
|
|
return special ? do_swapon(special, prio, dsc, CANONIC) :
|
|
|
|
cannot_find(uuid);
|
2006-12-06 17:26:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-08-07 05:03:53 -05:00
|
|
|
do_swapoff(const char *orig_special, int quiet, int canonic) {
|
|
|
|
const char *special = orig_special;
|
2006-12-06 17:26:31 -06:00
|
|
|
|
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-08-07 05:03:53 -05:00
|
|
|
if (!canonic) {
|
2012-03-27 05:13:24 -05:00
|
|
|
special = blkid_evaluate_spec(orig_special, NULL);
|
2007-08-07 05:03:53 -05:00
|
|
|
if (!special)
|
|
|
|
return cannot_find(orig_special);
|
|
|
|
}
|
2006-12-06 17:25:58 -06:00
|
|
|
|
|
|
|
if (swapoff(special) == 0)
|
|
|
|
return 0; /* success */
|
|
|
|
|
2009-03-02 08:16:21 -06:00
|
|
|
if (errno == EPERM)
|
|
|
|
errx(EXIT_FAILURE, _("Not superuser."));
|
|
|
|
|
|
|
|
if (!quiet || errno == ENOMEM)
|
|
|
|
warn(_("%s: swapoff failed"), orig_special);
|
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) {
|
2012-03-27 05:13:24 -05:00
|
|
|
const char *special = blkid_evaluate_tag("LABEL", label, NULL);
|
2007-08-07 05:03:53 -05:00
|
|
|
return special ? do_swapoff(special, quiet, CANONIC) : cannot_find(label);
|
2006-12-06 17:26:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
swapoff_by_uuid(const char *uuid, int quiet) {
|
2012-03-27 05:13:24 -05:00
|
|
|
const char *special = blkid_evaluate_tag("UUID", uuid, NULL);
|
2007-08-07 05:03:53 -05:00
|
|
|
return special ? do_swapoff(special, quiet, CANONIC) : cannot_find(uuid);
|
2006-12-06 17:26:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2007-11-28 07:35:24 -06:00
|
|
|
fp = setmntent(_PATH_MNTTAB, "r");
|
2009-03-02 08:16:21 -06:00
|
|
|
if (fp == NULL)
|
|
|
|
err(2, _("%s: open failed"), _PATH_MNTTAB);
|
2006-12-06 17:26:31 -06:00
|
|
|
|
|
|
|
while ((fstab = getmntent(fp)) != NULL) {
|
|
|
|
const char *special;
|
2010-10-25 07:03:16 -05:00
|
|
|
int skip = 0, nofail = ifexists;
|
2010-10-24 19:32:47 -05:00
|
|
|
int pri = priority, dsc = discard;
|
2008-08-18 18:14:14 -05:00
|
|
|
char *opt, *opts;
|
2006-12-06 17:26:31 -06:00
|
|
|
|
|
|
|
if (!streq(fstab->mnt_type, MNTTYPE_SWAP))
|
|
|
|
continue;
|
|
|
|
|
2012-03-20 03:44:40 -05:00
|
|
|
opts = xstrdup(fstab->mnt_opts);
|
2008-08-18 18:14:14 -05:00
|
|
|
|
|
|
|
for (opt = strtok(opts, ","); opt != NULL;
|
|
|
|
opt = strtok(NULL, ",")) {
|
|
|
|
if (strncmp(opt, "pri=", 4) == 0)
|
|
|
|
pri = atoi(opt+4);
|
2010-10-24 19:32:47 -05:00
|
|
|
if (strcmp(opt, "discard") == 0)
|
|
|
|
dsc = 1;
|
2008-08-18 18:14:14 -05:00
|
|
|
if (strcmp(opt, "noauto") == 0)
|
|
|
|
skip = 1;
|
2010-10-25 07:03:16 -05:00
|
|
|
if (strcmp(opt, "nofail") == 0)
|
|
|
|
nofail = 1;
|
2008-08-18 18:14:14 -05:00
|
|
|
}
|
|
|
|
free(opts);
|
|
|
|
|
|
|
|
if (skip)
|
|
|
|
continue;
|
|
|
|
|
2012-03-27 05:13:24 -05:00
|
|
|
special = blkid_evaluate_spec(fstab->mnt_fsname, NULL);
|
2008-08-18 18:14:14 -05:00
|
|
|
if (!special) {
|
2010-10-25 07:03:16 -05:00
|
|
|
if (!nofail)
|
2008-08-18 18:14:14 -05:00
|
|
|
status |= cannot_find(fstab->mnt_fsname);
|
2006-12-06 17:26:31 -06:00
|
|
|
continue;
|
2008-08-18 18:14:14 -05:00
|
|
|
}
|
2006-12-06 17:26:31 -06:00
|
|
|
|
|
|
|
if (!is_in_proc_swaps(special) &&
|
2010-10-25 07:03:16 -05:00
|
|
|
(!nofail || !access(special, R_OK)))
|
2010-10-24 19:32:47 -05:00
|
|
|
status |= do_swapon(special, pri, dsc, CANONIC);
|
2008-08-18 18:14:14 -05:00
|
|
|
|
|
|
|
free((void *) special);
|
2006-12-06 17:26:31 -06:00
|
|
|
}
|
|
|
|
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) {
|
2011-07-17 23:33:20 -05:00
|
|
|
llist = (const char **) xrealloc(llist, (++llct) * sizeof(char *));
|
2006-12-06 17:26:31 -06:00
|
|
|
llist[llct-1] = label;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addu(const char *uuid) {
|
2011-07-17 23:33:20 -05:00
|
|
|
ulist = (const char **) xrealloc(ulist, (++ulct) * sizeof(char *));
|
2006-12-06 17:26:31 -06:00
|
|
|
ulist[ulct-1] = uuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
main_swapon(int argc, char *argv[]) {
|
|
|
|
int status = 0;
|
|
|
|
int c, i;
|
|
|
|
|
2010-10-24 19:32:47 -05:00
|
|
|
while ((c = getopt_long(argc, argv, "ahdefp:svVL:U:",
|
2006-12-06 17:26:31 -06:00
|
|
|
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;
|
2010-10-24 19:32:47 -05:00
|
|
|
case 'd':
|
|
|
|
discard = 1;
|
|
|
|
break;
|
2006-12-06 17:26:05 -06:00
|
|
|
case 'e': /* ifexists */
|
|
|
|
ifexists = 1;
|
|
|
|
break;
|
2009-03-03 07:53:27 -06:00
|
|
|
case 'f':
|
|
|
|
fixpgsz = 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 */
|
2010-06-10 07:29:27 -05:00
|
|
|
printf(_("%s (%s)\n"), progname, PACKAGE_STRING);
|
2011-02-03 14:43:29 -06:00
|
|
|
exit(EXIT_SUCCESS);
|
2006-12-06 17:25:58 -06:00
|
|
|
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++)
|
2010-10-24 19:32:47 -05:00
|
|
|
status |= swapon_by_label(llist[i], priority, discard);
|
2006-12-06 17:25:58 -06:00
|
|
|
|
2006-12-06 17:26:31 -06:00
|
|
|
for (i = 0; i < ulct; i++)
|
2010-10-24 19:32:47 -05:00
|
|
|
status |= swapon_by_uuid(ulist[i], priority, discard);
|
2006-12-06 17:25:58 -06:00
|
|
|
|
|
|
|
while (*argv != NULL)
|
2010-10-24 19:32:47 -05:00
|
|
|
status |= do_swapon(*argv++, priority, discard, !CANONIC);
|
2006-12-06 17:25:58 -06:00
|
|
|
|
|
|
|
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 */
|
2010-06-10 07:29:27 -05:00
|
|
|
printf(_("%s (%s)\n"), progname, PACKAGE_STRING);
|
2011-02-03 14:43:29 -06:00
|
|
|
exit(EXIT_SUCCESS);
|
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)
|
2007-08-07 05:03:53 -05:00
|
|
|
status |= do_swapoff(*argv++, !QUIET, !CANONIC);
|
2006-12-06 17:25:58 -06:00
|
|
|
|
|
|
|
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++)
|
2007-08-07 05:03:53 -05:00
|
|
|
status |= do_swapoff(swapFiles[i], QUIET, CANONIC);
|
2006-12-06 17:25:58 -06:00
|
|
|
|
|
|
|
/*
|
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.
|
|
|
|
*/
|
2007-11-28 07:35:24 -06:00
|
|
|
fp = setmntent(_PATH_MNTTAB, "r");
|
2009-03-02 08:16:21 -06:00
|
|
|
if (fp == NULL)
|
|
|
|
err(2, _("%s: open failed"), _PATH_MNTTAB);
|
|
|
|
|
2006-12-06 17:25:58 -06:00
|
|
|
while ((fstab = getmntent(fp)) != NULL) {
|
2007-08-04 19:04:11 -05:00
|
|
|
const char *special;
|
|
|
|
|
|
|
|
if (!streq(fstab->mnt_type, MNTTYPE_SWAP))
|
|
|
|
continue;
|
|
|
|
|
2012-03-27 05:13:24 -05:00
|
|
|
special = blkid_evaluate_spec(fstab->mnt_fsname, NULL);
|
2007-08-04 19:04:11 -05:00
|
|
|
if (!special)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!is_in_proc_swaps(special))
|
2007-08-07 05:03:53 -05:00
|
|
|
do_swapoff(special, QUIET, CANONIC);
|
2006-12-06 17:25:58 -06:00
|
|
|
}
|
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[]) {
|
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
2012-04-04 12:49:40 -05:00
|
|
|
atexit(close_stdout);
|
2006-12-06 17:25:58 -06:00
|
|
|
|
2009-03-02 08:16:21 -06:00
|
|
|
progname = program_invocation_short_name;
|
|
|
|
if (!progname) {
|
|
|
|
char *p = strrchr(argv[0], '/');
|
|
|
|
progname = p ? p+1 : argv[0];
|
|
|
|
}
|
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);
|
2009-10-13 07:22:33 -05:00
|
|
|
else if (streq(progname, "swapoff"))
|
2006-12-06 17:25:58 -06:00
|
|
|
return main_swapoff(argc, argv);
|
2009-10-13 07:22:33 -05:00
|
|
|
|
|
|
|
errx(EXIT_FAILURE, _("'%s' is unsupported program name "
|
|
|
|
"(must be 'swapon' or 'swapoff')."), progname);
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|