Signed-off-by: Alexey Gladkov <gladkov.alexey@xxxxxxxxx> --- Makefile.am | 15 ++++-- configure.ac | 5 ++ depmod.c | 2 +- elfops.c | 2 +- elfops.h | 2 +- grabfile.c | 63 ++++++++++++++++++++++ grabfile.h | 23 ++++++++ grabfile_plain.c | 52 ++++++++++++++++++ grabfile_plain.h | 9 +++ grabfile_zlib.c | 67 +++++++++++++++++++++++ grabfile_zlib.h | 9 +++ modinfo.c | 2 +- modprobe.c | 2 +- zlibsupport.c | 154 ------------------------------------------------------ zlibsupport.h | 23 -------- 15 files changed, 243 insertions(+), 187 deletions(-) create mode 100644 grabfile.c create mode 100644 grabfile.h create mode 100644 grabfile_plain.c create mode 100644 grabfile_plain.h create mode 100644 grabfile_zlib.c create mode 100644 grabfile_zlib.h delete mode 100644 zlibsupport.c delete mode 100644 zlibsupport.h diff --git a/Makefile.am b/Makefile.am index 3e699e5..114d670 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,13 +1,18 @@ ACLOCAL_AMFLAGS = -I m4 +grabfile_SOURCES = grabfile.c grabfile.h grabfile_plain.c grabfile_plain.h + +if WITH_ZLIB +grabfile_SOURCES += grabfile_zlib.c grabfile_zlib.h +endif + insmod_SOURCES = insmod.c testing.h lsmod_SOURCES = lsmod.c testing.h -modprobe_SOURCES = modprobe.c zlibsupport.c zlibsupport.h testing.h +modprobe_SOURCES = modprobe.c testing.h $(grabfile_SOURCES) rmmod_SOURCES = rmmod.c testing.h -depmod_SOURCES = depmod.c zlibsupport.c tables.c \ - zlibsupport.h tables.h testing.h -modinfo_SOURCES = modinfo.c zlibsupport.c zlibsupport.h testing.h -modindex_SOURCES = modindex.c zlibsupport.c zlibsupport.h testing.h +depmod_SOURCES = depmod.c tables.c tables.h testing.h $(grabfile_SOURCES) +modinfo_SOURCES = modinfo.c testing.h $(grabfile_SOURCES) +modindex_SOURCES = modindex.c testing.h $(grabfile_SOURCES) insmod_static_SOURCES = insmod.c insmod_static_LDFLAGS = -static diff --git a/configure.ac b/configure.ac index b085178..42629fa 100644 --- a/configure.ac +++ b/configure.ac @@ -8,11 +8,14 @@ AX_ENABLE_BUILDDIR([build]) # non-Linux systems and it's reasonable to expect GNU-compatibility here. AM_INIT_AUTOMAKE([-Wno-portability]) +WITH_ZLIB=no + # If zlib is required, libz must be linked static, modprobe is in # /sbin, libz is in /usr/lib and may not be available when it is run. AC_ARG_ENABLE(zlib, [ --enable-zlib Handle gzipped modules], [if test "$enableval" = "yes"; then + WITH_ZLIB=yes AC_DEFINE(CONFIG_USE_ZLIB) zlib_flags="-Wl,-Bstatic -lz -Wl,-Bdynamic" fi]) @@ -22,9 +25,11 @@ AC_ARG_ENABLE(zlib-dynamic, dynamically. Note that libz must be installed in /lib for this to work.], [if test "$enableval" = "yes"; then + WITH_ZLIB=yes AC_DEFINE(CONFIG_USE_ZLIB) zlib_flags="-lz" fi]) +AM_CONDITIONAL(WITH_ZLIB, test "$WITH_ZLIB" = "yes") AC_PROG_CC AC_PROG_RANLIB diff --git a/depmod.c b/depmod.c index a1d2f8c..3b5592e 100644 --- a/depmod.c +++ b/depmod.c @@ -38,7 +38,7 @@ #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #include "util.h" -#include "zlibsupport.h" +#include "grabfile.h" #include "depmod.h" #include "logging.h" #include "index.h" diff --git a/elfops.c b/elfops.c index 3468265..5177a9b 100644 --- a/elfops.c +++ b/elfops.c @@ -11,7 +11,7 @@ #include "logging.h" #include "elfops.h" #include "tables.h" -#include "zlibsupport.h" +#include "grabfile.h" #include "testing.h" diff --git a/elfops.h b/elfops.h index fd4b014..7594931 100644 --- a/elfops.h +++ b/elfops.h @@ -3,7 +3,7 @@ #include <stdio.h> #include <stdint.h> #include "logging.h" -#include "zlibsupport.h" +#include "grabfile.h" /* All the icky stuff to do with manipulating 64 and 32-bit modules belongs here. */ diff --git a/grabfile.c b/grabfile.c new file mode 100644 index 0000000..d5a09ad --- /dev/null +++ b/grabfile.c @@ -0,0 +1,63 @@ +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> + +#include "logging.h" + +#include "grabfile.h" +#include "grabfile_plain.h" + +#ifdef CONFIG_USE_ZLIB +#include "grabfile_zlib.h" +#endif + +static int compress_type(const char *filename) +{ + int fd; + unsigned char buf[6]; + + if ((fd = open(filename, O_RDONLY)) == -1) + return -1; + + if (read(fd, buf, sizeof(buf)) == -1) { + close(fd); + return -1; + } + + close(fd); + + if (buf[0] == 0x1F && buf[1] == 0x8B) + return GZIP_FILE; + + return PLAIN_FILE; +} + +int grab_file(const char *filename, struct grab_data *fdata) +{ + switch (compress_type(filename)) { +#ifdef CONFIG_USE_ZLIB + case GZIP_FILE: + return gzip_grab_file(filename, fdata); +#endif + case PLAIN_FILE: + return plain_grab_file(filename, fdata); + } + fatal("Unknown compression type\n"); + return -1; +} + +void release_file(struct grab_data *fdata) +{ + switch (fdata->type) { +#ifdef CONFIG_USE_ZLIB + case GZIP_FILE: + gzip_release_file(fdata); + return; +#endif + case PLAIN_FILE: + plain_release_file(fdata); + return; + } + fatal("Unknown compression type\n"); +} diff --git a/grabfile.h b/grabfile.h new file mode 100644 index 0000000..99565db --- /dev/null +++ b/grabfile.h @@ -0,0 +1,23 @@ +#ifndef _GRAB_FILE_H +#define _GRAB_FILE_H + +enum file_type +{ + PLAIN_FILE, + GZIP_FILE +}; + +struct grab_data +{ + enum file_type type; + unsigned long size; + void *data; +}; + +/* Grab file. Decompresses if that is supported. Returns NULL on error. */ +extern int grab_file(const char *filename, struct grab_data *fdata); + +/* Free it up. */ +extern void release_file(struct grab_data *fdata); + +#endif /* _GRAB_FILE_H */ diff --git a/grabfile_plain.c b/grabfile_plain.c new file mode 100644 index 0000000..8176c8a --- /dev/null +++ b/grabfile_plain.c @@ -0,0 +1,52 @@ +/* Support for compressed modules. Willy Tarreau <willy@xxxxxxxxxx> + * did the support for modutils, Andrey Borzenkov <arvidjaar@xxxxxxx> + * ported it to module-init-tools, and I said it was too ugly to live + * and rewrote it 8). + * + * (C) 2003 Rusty Russell, IBM Corporation. + */ +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <errno.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/mman.h> + +#include "grabfile_plain.h" +#include "logging.h" +#include "testing.h" + +static int grab_fd(int fd, struct grab_data *fdata) +{ + struct stat st; + int ret; + + ret = fstat(fd, &st); + if (ret < 0) + return -1; + fdata->size = st.st_size; + fdata->data = mmap(0, fdata->size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); + if (fdata->data == MAP_FAILED) + fdata->data = NULL; + return 0; +} + +int plain_grab_file(const char *filename, struct grab_data *fdata) +{ + int fd; + + fd = open(filename, O_RDONLY, 0); + if (fd < 0) + return -1; + if (grab_fd(fd, fdata) < 0) + return -1; + fdata->type = PLAIN_FILE; + close(fd); + return 0; +} + +void plain_release_file(struct grab_data *fdata) +{ + munmap(fdata->data, fdata->size); +} diff --git a/grabfile_plain.h b/grabfile_plain.h new file mode 100644 index 0000000..0b76205 --- /dev/null +++ b/grabfile_plain.h @@ -0,0 +1,9 @@ +#ifndef _GRABFILE_PLAIN_H +#define _GRABFILE_PLAIN_H + +#include "grabfile.h" + +extern int plain_grab_file(const char *filename, struct grab_data *fdata); +extern void plain_release_file(struct grab_data *fdata); + +#endif /* _GRABFILE_PLAIN_H */ diff --git a/grabfile_zlib.c b/grabfile_zlib.c new file mode 100644 index 0000000..4ad2a97 --- /dev/null +++ b/grabfile_zlib.c @@ -0,0 +1,67 @@ +/* Support for compressed modules. Willy Tarreau <willy@xxxxxxxxxx> + * did the support for modutils, Andrey Borzenkov <arvidjaar@xxxxxxx> + * ported it to module-init-tools, and I said it was too ugly to live + * and rewrote it 8). + * + * (C) 2003 Rusty Russell, IBM Corporation. + */ +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <errno.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/mman.h> + +#include "grabfile_zlib.h" +#include "logging.h" +#include "testing.h" + +#include <zlib.h> + +static int grab_contents(gzFile *gzfd, struct grab_data *fdata) +{ + unsigned int max = 16384; + int ret; + + fdata->data = NOFAIL(malloc(max)); + fdata->size = 0; + + while ((ret = gzread(gzfd, fdata->data + fdata->size, max - fdata->size)) > 0) { + fdata->size += ret; + if (fdata->size == max) + fdata->data = NOFAIL(realloc(fdata->data, max *= 2)); + } + if (ret < 0) { + free(fdata->data); + fdata->data = NULL; + return -1; + } + + return 0; +} + +int gzip_grab_file(const char *filename, struct grab_data *fdata) +{ + gzFile gzfd; + + errno = 0; + gzfd = gzopen(filename, "rb"); + if (!gzfd) { + if (errno == ENOMEM) + fatal("Memory allocation failure in gzopen\n"); + return -1; + } + if (grab_contents(gzfd, fdata) < 0) { + gzclose(gzfd); + return -1; + } + fdata->type = GZIP_FILE; + gzclose(gzfd); + return 0; +} + +void gzip_release_file(struct grab_data *fdata) +{ + free(fdata->data); +} diff --git a/grabfile_zlib.h b/grabfile_zlib.h new file mode 100644 index 0000000..1580c5a --- /dev/null +++ b/grabfile_zlib.h @@ -0,0 +1,9 @@ +#ifndef _GRABFILE_ZLIB_H +#define _GRABFILE_ZLIB_H + +#include "grabfile.h" + +extern int gzip_grab_file(const char *filename, struct grab_data *fdata); +extern void gzip_release_file(struct grab_data *fdata); + +#endif /* _GRABFILE_ZLIB_H */ diff --git a/modinfo.c b/modinfo.c index 9ae40f2..edc9f24 100644 --- a/modinfo.c +++ b/modinfo.c @@ -16,7 +16,7 @@ #include "util.h" #include "logging.h" #include "elfops.h" -#include "zlibsupport.h" +#include "grabfile.h" #include "testing.h" #ifndef MODULE_DIR diff --git a/modprobe.c b/modprobe.c index edf60d6..c42d3a0 100644 --- a/modprobe.c +++ b/modprobe.c @@ -44,7 +44,7 @@ #include "util.h" #include "elfops.h" -#include "zlibsupport.h" +#include "grabfile.h" #include "logging.h" #include "index.h" #include "list.h" diff --git a/zlibsupport.c b/zlibsupport.c deleted file mode 100644 index bff371a..0000000 --- a/zlibsupport.c +++ /dev/null @@ -1,154 +0,0 @@ -/* Support for compressed modules. Willy Tarreau <willy@xxxxxxxxxx> - * did the support for modutils, Andrey Borzenkov <arvidjaar@xxxxxxx> - * ported it to module-init-tools, and I said it was too ugly to live - * and rewrote it 8). - * - * (C) 2003 Rusty Russell, IBM Corporation. - */ -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <errno.h> -#include <stdlib.h> -#include <unistd.h> -#include <sys/mman.h> - -#include "zlibsupport.h" -#include "logging.h" -#include "testing.h" - -#ifdef CONFIG_USE_ZLIB -#include <zlib.h> - -static int grab_contents(gzFile *gzfd, struct grab_data *fdata) -{ - unsigned int max = 16384; - int ret; - - fdata->data = NOFAIL(malloc(max)); - fdata->size = 0; - - while ((ret = gzread(gzfd, fdata->data + fdata->size, max - fdata->size)) > 0) { - fdata->size += ret; - if (fdata->size == max) - fdata->data = NOFAIL(realloc(fdata->data, max *= 2)); - } - if (ret < 0) { - free(fdata->data); - fdata->data = NULL; - return -1; - } - - return 0; -} - -/* gzopen handles uncompressed files transparently. */ -static int gzip_grab_file(const char *filename, struct grab_data *fdata) -{ - gzFile gzfd; - - errno = 0; - gzfd = gzopen(filename, "rb"); - if (!gzfd) { - if (errno == ENOMEM) - fatal("Memory allocation failure in gzopen\n"); - return -1; - } - if (grab_contents(gzfd, fdata) < 0) { - gzclose(gzfd); - return -1; - } - fdata->type = GZIP_FILE; - gzclose(gzfd); - return 0; -} - -static void gzip_release_file(struct grab_data *fdata) -{ - free(fdata->data); -} -#endif - -static int grab_fd(int fd, struct grab_data *fdata) -{ - struct stat st; - int ret; - - ret = fstat(fd, &st); - if (ret < 0) - return -1; - fdata->size = st.st_size; - fdata->data = mmap(0, fdata->size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); - if (fdata->data == MAP_FAILED) - fdata->data = NULL; - return 0; -} - -static int plain_grab_file(const char *filename, struct grab_data *fdata) -{ - int fd; - - fd = open(filename, O_RDONLY, 0); - if (fd < 0) - return -1; - if (grab_fd(fd, fdata) < 0) - return -1; - fdata->type = PLAIN_FILE; - close(fd); - return 0; -} - -static void plain_release_file(struct grab_data *fdata) -{ - munmap(fdata->data, fdata->size); -} - -static int compress_type(const char *filename) -{ - int fd; - unsigned char buf[6]; - - if ((fd = open(filename, O_RDONLY)) == -1) - return -1; - - if (read(fd, buf, sizeof(buf)) == -1) { - close(fd); - return -1; - } - - close(fd); - - if (buf[0] == 0x1F && buf[1] == 0x8B) - return GZIP_FILE; - - return PLAIN_FILE; -} - -int grab_file(const char *filename, struct grab_data *fdata) -{ - switch (compress_type(filename)) { -#ifdef CONFIG_USE_ZLIB - case GZIP_FILE: - return gzip_grab_file(filename, fdata); -#endif - case PLAIN_FILE: - return plain_grab_file(filename, fdata); - } - fatal("Unknown compression type\n"); - return -1; -} - -void release_file(struct grab_data *fdata) -{ - switch (fdata->type) { -#ifdef CONFIG_USE_ZLIB - case GZIP_FILE: - gzip_release_file(fdata); - return; -#endif - case PLAIN_FILE: - plain_release_file(fdata); - return; - } - fatal("Unknown compression type\n"); -} diff --git a/zlibsupport.h b/zlibsupport.h deleted file mode 100644 index 3eb4c2d..0000000 --- a/zlibsupport.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef _ZLIB_SUPPORT_H -#define _ZLIB_SUPPORT_H - -enum file_type -{ - PLAIN_FILE, - GZIP_FILE -}; - -struct grab_data -{ - enum file_type type; - unsigned long size; - void *data; -}; - -/* Grab file. Decompresses if that is supported. Returns NULL on error. */ -extern int grab_file(const char *filename, struct grab_data *fdata); - -/* Free it up. */ -extern void release_file(struct grab_data *fdata); - -#endif /* _ZLIB_SUPPORT_H */ -- 1.7.3.5 -- To unsubscribe from this list: send the line "unsubscribe linux-modules" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html