commit 9938320eeabc1845110d7e7dfadeb74f274b51aa
parent 99a95f2734a3cd0e6e14c93b4a2ae245ec45f450
Author: Carlos J. Torres <vlaadbrain@gmail.com>
Date: Sat, 8 Feb 2014 12:54:04 -0500
disable all swap devices
Diffstat:
M | swapoff.c | | | 37 | +++++++++++++++++++++++++++++-------- |
1 file changed, 29 insertions(+), 8 deletions(-)
diff --git a/swapoff.c b/swapoff.c
@@ -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;