From 0b0c231f64d02e6822b62d38705573de6c038b2b Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 18 Apr 2012 11:21:12 +0200 Subject: [PATCH] swapon: move generic code to swapon-common.c This will allow to use separate binary for swapon and swapoff. Signed-off-by: Karel Zak --- sys-utils/Makefile.am | 1 + sys-utils/swapon-common.c | 106 +++++++++++++++++++++++++++++++++++++ sys-utils/swapon-common.h | 26 +++++++++ sys-utils/swapon.c | 107 ++++++++------------------------------ 4 files changed, 154 insertions(+), 86 deletions(-) create mode 100644 sys-utils/swapon-common.c create mode 100644 sys-utils/swapon-common.h diff --git a/sys-utils/Makefile.am b/sys-utils/Makefile.am index a1fb3dd82c..c85ab6f86c 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 0000000000..d5b7cef61d --- /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 0000000000..53ba15e0eb --- /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 + +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 5353176701..2c235ed070 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 parameter:\n" \ " -L