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

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 [-d] device\n", argv0);
eprintf("usage: %s [-d] [-a] device\n", argv0);
}
int
@ -18,8 +19,12 @@ main(int argc, char *argv[])
int i;
int ret = EXIT_SUCCESS;
int flags = 0;
int all = 0;
ARGBEGIN {
case 'a':
all = 1;
break;
case 'd':
flags |= SWAP_FLAG_DISCARD;
break;
@ -27,15 +32,32 @@ main(int argc, char *argv[])
usage();
} ARGEND;
if (argc < 1)
if (!all && argc < 1)
usage();
for (i = 0; i < argc; i++) {
ret = swapon(argv[i], flags);
if (ret < 0) {
fprintf(stderr, "swapon %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
&& (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;