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:25:34 -06:00
|
|
|
|
* Added '-s' (Summary option) <Vincent.Renardias@waw.com> 02/1997.
|
2006-12-06 17:25:39 -06:00
|
|
|
|
*
|
2006-12-06 17:25:46 -06:00
|
|
|
|
* 1999-02-22 Arkadiusz Mi<EFBFBD>kiewicz <misiek@pld.ORG.PL>
|
2006-12-06 17:25:39 -06:00
|
|
|
|
* - added Native Language Support
|
|
|
|
|
* Sun Mar 21 1999 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
|
|
|
|
|
* - fixed strerr(errno) in gettext calls
|
|
|
|
|
*
|
2006-12-06 17:25:32 -06:00
|
|
|
|
*/
|
|
|
|
|
|
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>
|
|
|
|
|
#include "swap_constants.h"
|
2006-12-06 17:25:34 -06:00
|
|
|
|
#include "swapargs.h"
|
2006-12-06 17:25:39 -06:00
|
|
|
|
#include "nls.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"
|
|
|
|
|
|
2006-12-06 17:25:37 -06:00
|
|
|
|
#define SWAPON_NEEDS_TWO_ARGS
|
2006-12-06 17:25:32 -06:00
|
|
|
|
|
|
|
|
|
/* Nonzero for chatty (-v). This is a nonstandard flag (not in BSD). */
|
|
|
|
|
int verbose = 0;
|
2006-12-06 17:25:33 -06:00
|
|
|
|
int priority = -1; /* non-prioritized swap by default */
|
2006-12-06 17:25:32 -06:00
|
|
|
|
|
|
|
|
|
extern char version[];
|
|
|
|
|
static char *program_name;
|
|
|
|
|
static struct option longopts[] =
|
|
|
|
|
{
|
|
|
|
|
{ "all", 0, 0, 'a' },
|
|
|
|
|
{ "help", 0, 0, 'h' },
|
2006-12-06 17:25:33 -06:00
|
|
|
|
{ "priority", required_argument, 0, 'p' },
|
2006-12-06 17:25:34 -06:00
|
|
|
|
{ "summary", 0, 0, 's' },
|
2006-12-06 17:25:32 -06:00
|
|
|
|
{ "verbose", 0, 0, 'v' },
|
|
|
|
|
{ "version", 0, 0, 'V' },
|
|
|
|
|
{ NULL, 0, 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
usage (FILE *fp, int n)
|
|
|
|
|
{
|
2006-12-06 17:25:39 -06:00
|
|
|
|
fprintf (fp, _("usage: %s [-hV]\n"
|
|
|
|
|
" %s -a [-v]\n"
|
|
|
|
|
" %s [-v] [-p priority] special ...\n"
|
|
|
|
|
" %s [-s]\n"),
|
|
|
|
|
program_name, program_name, program_name, program_name);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
exit (n);
|
|
|
|
|
}
|
|
|
|
|
|
2006-12-06 17:25:34 -06:00
|
|
|
|
#ifdef SWAPON_HAS_TWO_ARGS
|
|
|
|
|
#define SWAPON_NEEDS_TWO_ARGS
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef SWAPON_NEEDS_TWO_ARGS
|
|
|
|
|
#ifdef SWAPON_HAS_TWO_ARGS
|
|
|
|
|
/* libc is OK */
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#else
|
|
|
|
|
/* 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);
|
|
|
|
|
#endif
|
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
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
swap (const char *special, int prio)
|
2006-12-06 17:25:32 -06:00
|
|
|
|
{
|
|
|
|
|
int status;
|
2006-12-06 17:25:37 -06:00
|
|
|
|
struct stat st;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
|
|
|
|
|
if (verbose)
|
2006-12-06 17:25:39 -06:00
|
|
|
|
printf(_("%s on %s\n"), program_name, special);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
|
2006-12-06 17:25:33 -06:00
|
|
|
|
if (streq (program_name, "swapon")) {
|
2006-12-06 17:25:37 -06:00
|
|
|
|
if (stat(special, &st) < 0) {
|
2006-12-06 17:25:39 -06:00
|
|
|
|
int errsv = errno;
|
2006-12-06 17:25:48 -06:00
|
|
|
|
fprintf (stderr, _("swapon: cannot stat %s: %s\n"),
|
|
|
|
|
special, strerror (errsv));
|
2006-12-06 17:25:37 -06:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2006-12-06 17:25:39 -06:00
|
|
|
|
/* people generally dislike this warning - now it is printed
|
|
|
|
|
only when `verbose' is set */
|
2006-12-06 17:25:48 -06:00
|
|
|
|
if (verbose) {
|
|
|
|
|
int permMask = (S_ISBLK(st.st_mode) ? 07007 : 07077);
|
|
|
|
|
|
|
|
|
|
if ((st.st_mode & permMask) != 0) {
|
|
|
|
|
fprintf(stderr,
|
|
|
|
|
_("swapon: warning: %s has insecure permissions %04o, "
|
|
|
|
|
"%04o suggested\n"),
|
|
|
|
|
special, st.st_mode & 07777, ~permMask & 0666);
|
|
|
|
|
}
|
2006-12-06 17:25:37 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 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:25:39 -06:00
|
|
|
|
_("swapon: Skipping file %s - it appears to have holes.\n"),
|
2006-12-06 17:25:37 -06:00
|
|
|
|
special);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-12-06 17:25:34 -06:00
|
|
|
|
#ifdef SWAPON_NEEDS_TWO_ARGS
|
2006-12-06 17:25:37 -06:00
|
|
|
|
{
|
2006-12-06 17:25:34 -06:00
|
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
|
|
#ifdef SWAP_FLAG_PREFER
|
2006-12-06 17:25:33 -06:00
|
|
|
|
if (prio >= 0) {
|
2006-12-06 17:25:34 -06:00
|
|
|
|
if (prio > SWAP_FLAG_PRIO_MASK)
|
|
|
|
|
prio = SWAP_FLAG_PRIO_MASK;
|
2006-12-06 17:25:33 -06:00
|
|
|
|
flags = SWAP_FLAG_PREFER
|
|
|
|
|
| ((prio & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT);
|
|
|
|
|
}
|
2006-12-06 17:25:34 -06:00
|
|
|
|
#endif
|
2006-12-06 17:25:33 -06:00
|
|
|
|
status = swapon (special, flags);
|
2006-12-06 17:25:37 -06:00
|
|
|
|
}
|
2006-12-06 17:25:33 -06:00
|
|
|
|
#else
|
|
|
|
|
status = swapon (special);
|
|
|
|
|
#endif
|
|
|
|
|
} else
|
|
|
|
|
status = swapoff (special);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
|
2006-12-06 17:25:39 -06:00
|
|
|
|
if (status < 0) {
|
|
|
|
|
int errsv = errno;
|
|
|
|
|
fprintf (stderr, "%s: %s: %s\n", program_name, special, strerror (errsv));
|
|
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2006-12-06 17:25:34 -06:00
|
|
|
|
static int
|
|
|
|
|
display_summary(void)
|
|
|
|
|
{
|
|
|
|
|
FILE *swaps;
|
|
|
|
|
char line[200] ;
|
|
|
|
|
|
|
|
|
|
if ((swaps = fopen(PROC_SWAPS, "r")) == NULL) {
|
2006-12-06 17:25:39 -06:00
|
|
|
|
int errsv = errno;
|
2006-12-06 17:25:34 -06:00
|
|
|
|
fprintf (stderr, "%s: %s: %s\n", program_name, PROC_SWAPS,
|
2006-12-06 17:25:39 -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:34 -06:00
|
|
|
|
while ( fgets(line, sizeof(line), swaps))
|
|
|
|
|
printf ("%s", line);
|
|
|
|
|
|
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:32 -06:00
|
|
|
|
int
|
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
|
{
|
2006-12-06 17:25:34 -06:00
|
|
|
|
struct mntent *fstab;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
int status;
|
|
|
|
|
int all = 0;
|
|
|
|
|
int c;
|
|
|
|
|
|
2006-12-06 17:25:39 -06:00
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
|
textdomain(PACKAGE);
|
|
|
|
|
|
2006-12-06 17:25:32 -06:00
|
|
|
|
if (strrchr (argv[0], '/') != NULL)
|
|
|
|
|
program_name = strrchr (argv[0], '/') + 1;
|
|
|
|
|
else
|
|
|
|
|
program_name = argv[0];
|
|
|
|
|
|
2006-12-06 17:25:34 -06:00
|
|
|
|
while ((c = getopt_long (argc, argv, "ahp:svV", longopts, NULL)) != EOF)
|
2006-12-06 17:25:32 -06:00
|
|
|
|
switch (c)
|
|
|
|
|
{
|
|
|
|
|
case 'a': /* all */
|
|
|
|
|
++all;
|
|
|
|
|
break;
|
|
|
|
|
case 'h': /* help */
|
|
|
|
|
usage (stdout, 0);
|
|
|
|
|
break;
|
2006-12-06 17:25:33 -06:00
|
|
|
|
case 'p': /* priority */
|
|
|
|
|
priority = atoi(optarg);
|
|
|
|
|
break;
|
2006-12-06 17:25:34 -06:00
|
|
|
|
case 's': /* tell about current use of swap areas */
|
|
|
|
|
status = display_summary();
|
|
|
|
|
exit(status);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
case 'v': /* be chatty */
|
|
|
|
|
++verbose;
|
|
|
|
|
break;
|
|
|
|
|
case 'V': /* version */
|
2006-12-06 17:25:34 -06:00
|
|
|
|
printf ("%s: %s\n", program_name, version);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
exit (0);
|
|
|
|
|
case 0:
|
|
|
|
|
break;
|
|
|
|
|
case '?':
|
|
|
|
|
default:
|
|
|
|
|
usage (stderr, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
|
|
status = 0;
|
|
|
|
|
|
2006-12-06 17:25:34 -06:00
|
|
|
|
if (all) {
|
|
|
|
|
FILE *fp = setmntent(_PATH_FSTAB, "r");
|
|
|
|
|
if (fp == NULL) {
|
2006-12-06 17:25:39 -06:00
|
|
|
|
int errsv = errno;
|
|
|
|
|
fprintf(stderr, _("%s: cannot open %s: %s\n"), program_name,
|
|
|
|
|
_PATH_FSTAB, strerror(errsv));
|
2006-12-06 17:25:34 -06:00
|
|
|
|
exit(2);
|
|
|
|
|
}
|
|
|
|
|
while ((fstab = getmntent(fp)) != NULL) {
|
|
|
|
|
if (streq (fstab->mnt_type, MNTTYPE_SWAP)) {
|
|
|
|
|
/* parse mount options; */
|
|
|
|
|
char *opt, *opts = strdup(fstab->mnt_opts);
|
2006-12-06 17:25:33 -06:00
|
|
|
|
|
2006-12-06 17:25:34 -06:00
|
|
|
|
for (opt = strtok (opts, ","); opt != NULL;
|
|
|
|
|
opt = strtok (NULL, ","))
|
|
|
|
|
if (strncmp(opt, "pri=", 4) == 0)
|
|
|
|
|
priority = atoi(opt+4);
|
|
|
|
|
status |= swap (fstab->mnt_fsname, priority);
|
|
|
|
|
}
|
2006-12-06 17:25:33 -06:00
|
|
|
|
}
|
2006-12-06 17:25:34 -06:00
|
|
|
|
} else if (*argv == NULL) {
|
|
|
|
|
usage (stderr, 2);
|
|
|
|
|
} else {
|
|
|
|
|
while (*argv != NULL)
|
|
|
|
|
status |= swap (*argv++,priority);
|
|
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
|
return status;
|
|
|
|
|
}
|