2013-08-09 14:48:29 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <sys/swap.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
|
|
|
|
#include <mntent.h>
|
2013-08-09 14:48:29 +00:00
|
|
|
#include <stdio.h>
|
2013-10-07 18:11:40 +00:00
|
|
|
#include <stdlib.h>
|
2013-08-09 14:48:29 +00:00
|
|
|
#include <string.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
|
2013-08-09 14:48:29 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-02-06 14:20:41 +00:00
|
|
|
eprintf("usage: %s [-dp] -a | device\n", argv0);
|
2013-08-09 14:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int i;
|
2014-10-02 22:45:25 +00:00
|
|
|
int ret = 0;
|
2013-08-09 14:48:29 +00:00
|
|
|
int flags = 0;
|
2014-02-08 17:44:50 +00:00
|
|
|
int all = 0;
|
2015-02-06 14:20:41 +00:00
|
|
|
struct mntent *me;
|
|
|
|
FILE *fp;
|
2013-08-09 14:48:29 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
2014-02-08 17:44:50 +00:00
|
|
|
case 'a':
|
|
|
|
all = 1;
|
|
|
|
break;
|
2013-08-09 14:48:29 +00:00
|
|
|
case 'd':
|
|
|
|
flags |= SWAP_FLAG_DISCARD;
|
|
|
|
break;
|
2014-02-09 03:24:17 +00:00
|
|
|
case 'p':
|
|
|
|
flags |= SWAP_FLAG_PREFER;
|
|
|
|
break;
|
2013-08-09 14:48:29 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2015-02-06 14:20:41 +00:00
|
|
|
if ((!all && argc < 1) || (all && argc > 0))
|
2013-08-09 14:48:29 +00:00
|
|
|
usage();
|
|
|
|
|
2014-02-08 17:44:50 +00:00
|
|
|
if (all) {
|
|
|
|
fp = setmntent("/etc/fstab", "r");
|
2014-02-15 18:26:01 +00:00
|
|
|
if (!fp)
|
|
|
|
eprintf("setmntent %s:", "/etc/fstab");
|
2014-02-08 17:44:50 +00:00
|
|
|
while ((me = getmntent(fp)) != NULL) {
|
|
|
|
if (strcmp(me->mnt_type, MNTTYPE_SWAP) == 0
|
2014-02-15 18:18:17 +00:00
|
|
|
&& (hasmntopt(me, MNTOPT_NOAUTO) == NULL)) {
|
2014-02-08 17:44:50 +00:00
|
|
|
if (swapon(me->mnt_fsname, flags) < 0) {
|
2014-06-30 21:42:56 +00:00
|
|
|
weprintf("swapon %s:", me->mnt_fsname);
|
2014-10-02 22:45:25 +00:00
|
|
|
ret = 1;
|
2014-02-08 17:44:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endmntent(fp);
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
if (swapon(argv[i], flags) < 0) {
|
2014-06-30 21:42:56 +00:00
|
|
|
weprintf("swapon %s:", argv[i]);
|
2014-10-02 22:45:25 +00:00
|
|
|
ret = 1;
|
2014-02-08 17:44:50 +00:00
|
|
|
}
|
2013-08-09 14:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|