disable all swap devices

This commit is contained in:
Carlos J. Torres 2014-02-08 12:54:04 -05:00 committed by sin
parent 99a95f2734
commit 9938320eea
1 changed files with 29 additions and 8 deletions

View File

@ -1,5 +1,6 @@
/* See LICENSE file for copyright and license details. */
#include <sys/swap.h>
#include <mntent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@ -9,7 +10,7 @@
static void
usage(void)
{
eprintf("usage: %s device\n", argv0);
eprintf("usage: %s [-a] device\n", argv0);
}
int
@ -17,21 +18,41 @@ main(int argc, char *argv[])
{
int i;
int ret = EXIT_SUCCESS;
int all = 0;
ARGBEGIN {
case 'a':
all = 1;
break;
default:
usage();
} ARGEND;
if (argc < 1)
if (!all && argc < 1)
usage();
for (i = 0; i < argc; i++) {
ret = swapoff(argv[i]);
if (ret < 0) {
fprintf(stderr, "swapoff %s: %s\n",
argv[i], strerror(errno));
ret = EXIT_FAILURE;
if (all) {
struct mntent *me = NULL;
FILE *fp;
fp = setmntent("/etc/fstab", "r");
while ((me = getmntent(fp)) != NULL) {
if (strcmp(me->mnt_type, MNTTYPE_SWAP) == 0) {
if (swapoff(me->mnt_fsname) < 0) {
fprintf(stderr, "swapoff %s: %s\n",
me->mnt_fsname, strerror(errno));
ret = EXIT_FAILURE;
}
}
}
endmntent(fp);
} else {
for (i = 0; i < argc; i++) {
if (swapoff(argv[i]) < 0) {
fprintf(stderr, "swapoff %s: %s\n",
argv[i], strerror(errno));
ret = EXIT_FAILURE;
}
}
}
return ret;