add -a flag to enable all swap devices unless noauto opt is set

This commit is contained in:
Carlos J. Torres 2014-02-08 12:44:50 -05:00 committed by sin
parent 30824954f8
commit 99a95f2734
1 changed files with 30 additions and 8 deletions

View File

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