From bd2342702558b0825b9a5d27a21c8a0d6f8ab377 Mon Sep 17 00:00:00 2001 From: "Carlos J. Torres" Date: Sat, 8 Feb 2014 12:44:50 -0500 Subject: [PATCH 1/2] add -a flag to enable all swap devices unless noauto opt is set --- swapon.c | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/swapon.c b/swapon.c index 0366aff..0610385 100644 --- a/swapon.c +++ b/swapon.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include +#include #include #include #include @@ -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; -- 1.8.4