summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sys-utils/Makefile.am1
-rw-r--r--sys-utils/swapon-common.c106
-rw-r--r--sys-utils/swapon-common.h26
-rw-r--r--sys-utils/swapon.c107
4 files changed, 154 insertions, 86 deletions
diff --git a/sys-utils/Makefile.am b/sys-utils/Makefile.am
index a1fb3dd82..c85ab6f86 100644
--- a/sys-utils/Makefile.am
+++ b/sys-utils/Makefile.am
@@ -136,6 +136,7 @@ dist_man_MANS += swapoff.8 swapon.8
swapon_SOURCES = \
swapon.c \
+ swapon-common.c \
$(top_srcdir)/lib/blkdev.c \
$(top_srcdir)/lib/linux_version.c
diff --git a/sys-utils/swapon-common.c b/sys-utils/swapon-common.c
new file mode 100644
index 000000000..d5b7cef61
--- /dev/null
+++ b/sys-utils/swapon-common.c
@@ -0,0 +1,106 @@
+
+#include "c.h"
+#include "nls.h"
+#include "swapon-common.h"
+#include "xalloc.h"
+
+/*
+ * content of /proc/swaps and /etc/fstab
+ */
+static struct libmnt_table *swaps, *fstab;
+
+struct libmnt_cache *mntcache;
+
+struct libmnt_table *get_fstab(void)
+{
+ if (!fstab) {
+ fstab = mnt_new_table();
+ if (!fstab)
+ return NULL;
+ mnt_table_set_cache(fstab, mntcache);
+ if (mnt_table_parse_fstab(fstab, NULL) != 0)
+ return NULL;
+ }
+
+ return fstab;
+}
+
+struct libmnt_table *get_swaps(void)
+{
+ if (!swaps) {
+ swaps = mnt_new_table();
+ if (!swaps)
+ return NULL;
+ mnt_table_set_cache(swaps, mntcache);
+ if (mnt_table_parse_swaps(swaps, NULL) != 0)
+ return NULL;
+ }
+
+ return swaps;
+}
+
+void free_tables(void)
+{
+ mnt_free_table(swaps);
+ mnt_free_table(fstab);
+}
+
+int match_swap(struct libmnt_fs *fs, void *data __attribute__((unused)))
+{
+ return fs && mnt_fs_is_swaparea(fs);
+}
+
+int is_active_swap(const char *filename)
+{
+ struct libmnt_table *st = get_swaps();
+ return st && mnt_table_find_srcpath(st, filename, MNT_ITER_BACKWARD);
+}
+
+
+int cannot_find(const char *special)
+{
+ warnx(_("cannot find the device for %s"), special);
+ return -1;
+}
+
+/*
+ * Lists with -L and -U option
+ */
+static const char **llist;
+static size_t llct;
+static const char **ulist;
+static size_t ulct;
+
+
+void add_label(const char *label)
+{
+ llist = (const char **) xrealloc(llist, (++llct) * sizeof(char *));
+ llist[llct - 1] = label;
+}
+
+const char *get_label(size_t i)
+{
+ return i < llct ? llist[i] : NULL;
+}
+
+size_t numof_labels(void)
+{
+ return llct;
+}
+
+void add_uuid(const char *uuid)
+{
+ ulist = (const char **) xrealloc(ulist, (++ulct) * sizeof(char *));
+ ulist[ulct - 1] = uuid;
+}
+
+const char *get_uuid(size_t i)
+{
+ return i < ulct ? ulist[i] : NULL;
+}
+
+size_t numof_uuids(void)
+{
+ return ulct;
+}
+
diff --git a/sys-utils/swapon-common.h b/sys-utils/swapon-common.h
new file mode 100644
index 000000000..53ba15e0e
--- /dev/null
+++ b/sys-utils/swapon-common.h
@@ -0,0 +1,26 @@
+#ifndef UTIL_LINUX_SWAPON_COMMON_H
+#define UTIL_LINUX_SWAPON_COMMON_H
+
+#include <libmount.h>
+
+extern struct libmnt_cache *mntcache;
+
+extern struct libmnt_table *get_fstab(void);
+extern struct libmnt_table *get_swaps(void);
+extern void free_tables(void);
+
+extern int match_swap(struct libmnt_fs *fs, void *data);
+extern int is_active_swap(const char *filename);
+
+extern int cannot_find(const char *special);
+
+extern void add_label(const char *label);
+extern const char *get_label(size_t i);
+extern size_t numof_labels(void);
+
+extern void add_uuid(const char *uuid);
+extern const char *get_uuid(size_t i);
+extern size_t numof_uuids(void);
+
+
+#endif /* UTIL_LINUX_SWAPON_COMMON_H */
diff --git a/sys-utils/swapon.c b/sys-utils/swapon.c
index 535317670..2c235ed07 100644
--- a/sys-utils/swapon.c
+++ b/sys-utils/swapon.c
@@ -22,11 +22,12 @@
#include "nls.h"
#include "pathnames.h"
#include "swapheader.h"
-#include "mangle.h"
#include "xalloc.h"
#include "c.h"
#include "closestream.h"
+#include "swapon-common.h"
+
#define PATH_MKSWAP "/sbin/mkswap"
#ifdef HAVE_SYS_SWAP_H
@@ -99,8 +100,6 @@ static const struct option longswaponopts[] = {
static const struct option *longswapoffopts = &longswaponopts[4];
-static int cannot_find(const char *special);
-
#define PRINT_USAGE_SPECIAL(_fp) \
fputs(_("\nThe <spec> parameter:\n" \
" -L <label> LABEL of device to be used\n" \
@@ -147,51 +146,6 @@ swapoff_usage(FILE *out, int n) {
exit(n);
}
-/*
- * contents of /proc/swaps
- */
-static struct libmnt_table *swaps, *fstab;
-static struct libmnt_cache *mntcache;
-
-static struct libmnt_table *get_fstab(void)
-{
- if (!fstab) {
- fstab = mnt_new_table();
- if (!fstab)
- return NULL;
- mnt_table_set_cache(fstab, mntcache);
- if (mnt_table_parse_fstab(fstab, NULL) != 0)
- return NULL;
- }
-
- return fstab;
-}
-
-static struct libmnt_table *get_swaps(void)
-{
- if (!swaps) {
- swaps = mnt_new_table();
- if (!swaps)
- return NULL;
- mnt_table_set_cache(swaps, mntcache);
- if (mnt_table_parse_swaps(swaps, NULL) != 0)
- return NULL;
- }
-
- return swaps;
-}
-
-static int match_swap(struct libmnt_fs *fs, void *data __attribute__((unused)))
-{
- return fs && mnt_fs_is_swaparea(fs);
-}
-
-static int is_active_swap(const char *filename)
-{
- struct libmnt_table *st = get_swaps();
- return st && mnt_table_find_srcpath(st, filename, MNT_ITER_BACKWARD);
-}
-
static int
display_summary(void)
{
@@ -558,12 +512,6 @@ do_swapon(const char *orig_special, int prio, int fl_discard, int canonic) {
}
static int
-cannot_find(const char *special) {
- warnx(_("cannot find the device for %s"), special);
- return -1;
-}
-
-static int
swapon_by_label(const char *label, int prio, int dsc) {
const char *special = mnt_resolve_tag("LABEL", label, mntcache);
return special ? do_swapon(special, prio, dsc, CANONIC) :
@@ -658,25 +606,11 @@ swapon_all(void) {
return status;
}
-static const char **llist = NULL;
-static int llct = 0;
-static const char **ulist = NULL;
-static int ulct = 0;
-
-static void addl(const char *label) {
- llist = (const char **) xrealloc(llist, (++llct) * sizeof(char *));
- llist[llct-1] = label;
-}
-
-static void addu(const char *uuid) {
- ulist = (const char **) xrealloc(ulist, (++ulct) * sizeof(char *));
- ulist[ulct-1] = uuid;
-}
-
static int
main_swapon(int argc, char *argv[]) {
int status = 0;
- int c, i;
+ int c;
+ size_t i;
while ((c = getopt_long(argc, argv, "ahdefp:svVL:U:",
longswaponopts, NULL)) != -1) {
@@ -691,10 +625,10 @@ main_swapon(int argc, char *argv[]) {
priority = atoi(optarg);
break;
case 'L':
- addl(optarg);
+ add_label(optarg);
break;
case 'U':
- addu(optarg);
+ add_uuid(optarg);
break;
case 'd':
discard = 1;
@@ -723,7 +657,7 @@ main_swapon(int argc, char *argv[]) {
}
argv += optind;
- if (!all && !llct && !ulct && *argv == NULL)
+ if (!all && !numof_labels() && numof_uuids() && *argv == NULL)
swapon_usage(stderr, 2);
if (ifexists && (!all || strcmp(progname, "swapon")))
@@ -732,11 +666,11 @@ main_swapon(int argc, char *argv[]) {
if (all)
status |= swapon_all();
- for (i = 0; i < llct; i++)
- status |= swapon_by_label(llist[i], priority, discard);
+ for (i = 0; i < numof_labels(); i++)
+ status |= swapon_by_label(get_label(i), priority, discard);
- for (i = 0; i < ulct; i++)
- status |= swapon_by_uuid(ulist[i], priority, discard);
+ for (i = 0; i < numof_uuids(); i++)
+ status |= swapon_by_uuid(get_uuid(i), priority, discard);
while (*argv != NULL)
status |= do_swapon(*argv++, priority, discard, !CANONIC);
@@ -749,7 +683,8 @@ main_swapoff(int argc, char *argv[]) {
FILE *fp;
struct mntent *fstab;
int status = 0;
- int c, i;
+ int c;
+ size_t i;
while ((c = getopt_long(argc, argv, "ahvVL:U:",
longswapoffopts, NULL)) != -1) {
@@ -767,10 +702,10 @@ main_swapoff(int argc, char *argv[]) {
printf(_("%s (%s)\n"), progname, PACKAGE_STRING);
exit(EXIT_SUCCESS);
case 'L':
- addl(optarg);
+ add_label(optarg);
break;
case 'U':
- addu(optarg);
+ add_uuid(optarg);
break;
case 0:
break;
@@ -781,18 +716,18 @@ main_swapoff(int argc, char *argv[]) {
}
argv += optind;
- if (!all && !llct && !ulct && *argv == NULL)
+ if (!all && !numof_labels() && !numof_uuids() && *argv == NULL)
swapoff_usage(stderr, 2);
/*
* swapoff any explicitly given arguments.
* Complain in case the swapoff call fails.
*/
- for (i = 0; i < llct; i++)
- status |= swapoff_by_label(llist[i], !QUIET);
+ for (i = 0; i < numof_labels(); i++)
+ status |= swapoff_by_label(get_label(i), !QUIET);
- for (i = 0; i < ulct; i++)
- status |= swapoff_by_uuid(ulist[i], !QUIET);
+ for (i = 0; i < numof_uuids(); i++)
+ status |= swapoff_by_uuid(get_uuid(i), !QUIET);
while (*argv != NULL)
status |= do_swapoff(*argv++, !QUIET, !CANONIC);
@@ -873,7 +808,7 @@ main(int argc, char *argv[]) {
errx(EXIT_FAILURE, _("'%s' is unsupported program name "
"(must be 'swapon' or 'swapoff')."), progname);
- mnt_free_table(swaps);
+ free_tables();
mnt_free_cache(mntcache);
return status;
}