]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
swapon: move generic code to swapon-common.c
authorKarel Zak <kzak@redhat.com>
Wed, 18 Apr 2012 09:21:12 +0000 (11:21 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 18 Apr 2012 09:21:12 +0000 (11:21 +0200)
This will allow to use separate binary for swapon and swapoff.

Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/Makefile.am
sys-utils/swapon-common.c [new file with mode: 0644]
sys-utils/swapon-common.h [new file with mode: 0644]
sys-utils/swapon.c

index a1fb3dd82c2c0928e4c677b6c58d7641c36195d5..c85ab6f86c8c1a8dbbd10c349e6a07c5cf0f69b9 100644 (file)
@@ -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 (file)
index 0000000..d5b7cef
--- /dev/null
@@ -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 (file)
index 0000000..53ba15e
--- /dev/null
@@ -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 */
index 535317670182d408359d4b60a26b3d8bca7c34ff..2c235ed0708ab37fc3972e2934417d6e6d9d9c8f 100644 (file)
 #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)
 {
@@ -557,12 +511,6 @@ do_swapon(const char *orig_special, int prio, int fl_discard, int canonic) {
        return status;
 }
 
-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);
@@ -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;
 }