summaryrefslogtreecommitdiffstats
path: root/mount/swapon.c
diff options
context:
space:
mode:
Diffstat (limited to 'mount/swapon.c')
-rw-r--r--mount/swapon.c80
1 files changed, 71 insertions, 9 deletions
diff --git a/mount/swapon.c b/mount/swapon.c
index 584033021..331238f0b 100644
--- a/mount/swapon.c
+++ b/mount/swapon.c
@@ -7,6 +7,9 @@
/* Nonzero for chatty (-v). This is a nonstandard flag (not in BSD). */
int verbose = 0;
+#ifdef SUPPORT_PRIORITIES
+int priority = -1; /* non-prioritized swap by default */
+#endif
extern char version[];
static char *program_name;
@@ -14,16 +17,27 @@ static struct option longopts[] =
{
{ "all", 0, 0, 'a' },
{ "help", 0, 0, 'h' },
+#ifdef SUPPORT_PRIORITIES
+ { "priority", required_argument, 0, 'p' },
+#endif
{ "verbose", 0, 0, 'v' },
{ "version", 0, 0, 'V' },
{ NULL, 0, 0, 0 }
};
+#ifdef SUPPORT_PRIORITIES
const char *usage_string = "\
usage: %s [-hV]\n\
%s -a [-v]\n\
- %s [-v] special ...\n\
+ %s [-v] [-p priority] special ...\n\
";
+#else
+const char *usage_string = "\
+usage: %s [-hV]\n\
+ %s -a [-v]\n\
+ %s [-v] [-p priority] special ...\n\
+";
+#endif
static void
usage (FILE *fp, int n)
@@ -33,17 +47,33 @@ usage (FILE *fp, int n)
}
static int
+#ifdef SUPPORT_PRIORITIES
+swap (const char *special, int prio)
+#else
swap (const char *special)
+#endif
{
int status;
+#ifdef SUPPORT_PRIORITIES
+ int flags;
+#endif
if (verbose)
printf("%s on device %s\n", program_name, special);
- if (streq (program_name, "swapon"))
- status = swapon (special);
- else
- status = swapoff (special);
+ if (streq (program_name, "swapon")) {
+#ifdef SUPPORT_PRIORITIES
+ flags = 0;
+ if (prio >= 0) {
+ flags = SWAP_FLAG_PREFER
+ | ((prio & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT);
+ }
+ status = swapon (special, flags);
+#else
+ status = swapon (special);
+#endif
+ } else
+ status = swapoff (special);
if (status < 0)
fprintf (stderr, "%s: %s: %s\n", program_name, special, strerror (errno));
@@ -64,7 +94,11 @@ main (int argc, char *argv[])
else
program_name = argv[0];
+#ifdef SUPPORT_PRIORITIES
+ while ((c = getopt_long (argc, argv, "ahp:vV", longopts, NULL)) != EOF)
+#else
while ((c = getopt_long (argc, argv, "ahvV", longopts, NULL)) != EOF)
+#endif
switch (c)
{
case 'a': /* all */
@@ -73,11 +107,16 @@ main (int argc, char *argv[])
case 'h': /* help */
usage (stdout, 0);
break;
+#ifdef SUPPORT_PRIORITIES
+ case 'p': /* priority */
+ priority = atoi(optarg);
+ break;
+#endif
case 'v': /* be chatty */
++verbose;
break;
case 'V': /* version */
- printf ("%s\n", version);
+ printf ("swapon: %s\n", version);
exit (0);
case 0:
break;
@@ -94,7 +133,25 @@ main (int argc, char *argv[])
{
while ((fstab = getfsent()) != NULL)
if (streq (fstab->fs_type, FSTAB_SW))
- status |= swap (fstab->fs_spec);
+ {
+#ifdef SUPPORT_PRIORITIES
+ /* parse mount options; */
+ char *opt, *opts = strdup(fstab->fs_mntopts);
+
+ for (opt = strtok (opts, ",");
+ opt != NULL;
+ opt = strtok (NULL, ","))
+ {
+ if (strncmp(opt, "pri=", 4) == 0)
+ {
+ priority = atoi(opt+4);
+ }
+ }
+ status |= swap (fstab->fs_spec, priority);
+#else
+ status |= swap (fstab->fs_spec);
+#endif
+ }
}
else if (*argv == NULL)
{
@@ -102,8 +159,13 @@ main (int argc, char *argv[])
}
else
{
- while (*argv != NULL)
- status |= swap (*argv++);
+ while (*argv != NULL) {
+#ifdef SUPPORT_PRIORITIES
+ status |= swap (*argv++,priority);
+#else
+ status |= swap (*argv++);
+#endif
+ }
}
return status;
}