cpio: testsuite for http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=466771
[oweals/busybox.git] / modutils / insmod.c
index 30d471b78bc146c957960c9c28110f861c3504fc..3b55451181a84e3c3a3602b0328843ffdc2e9964 100644 (file)
@@ -452,7 +452,7 @@ enum {
 /* The system calls unchanged between 2.0 and 2.1.  */
 
 unsigned long create_module(const char *, size_t);
-int delete_module(const char *);
+int delete_module(const char *module, unsigned int flags);
 
 
 #endif /* module.h */
@@ -673,8 +673,6 @@ static int obj_gpl_license(struct obj_file *f, const char **license);
 #define SPFX   ""
 #endif
 
-
-#define _PATH_MODULES  "/lib/modules"
 enum { STRVERSIONLEN = 64 };
 
 /*======================================================================*/
@@ -793,9 +791,9 @@ static char *m_fullName;
 /*======================================================================*/
 
 
-static int check_module_name_match(const char *filename,
-               struct stat *statbuf ATTRIBUTE_UNUSED,
-               void *userdata, int depth ATTRIBUTE_UNUSED)
+static int FAST_FUNC check_module_name_match(const char *filename,
+               struct stat *statbuf UNUSED_PARAM,
+               void *userdata, int depth UNUSED_PARAM)
 {
        char *fullname = (char *) userdata;
        char *tmp;
@@ -2203,7 +2201,7 @@ static struct obj_section *obj_create_alloced_section(struct obj_file *f,
        int newidx = f->header.e_shnum++;
        struct obj_section *sec;
 
-       f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
+       f->sections = xrealloc_vector(f->sections, 2, newidx);
        f->sections[newidx] = sec = arch_new_section();
 
        sec->header.sh_type = SHT_PROGBITS;
@@ -2252,7 +2250,8 @@ static void *obj_extend_section(struct obj_section *sec, unsigned long more)
 {
        unsigned long oldsize = sec->header.sh_size;
        if (more) {
-               sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
+               sec->header.sh_size += more;
+               sec->contents = xrealloc(sec->contents, sec->header.sh_size);
        }
        return sec->contents + oldsize;
 }
@@ -2738,7 +2737,8 @@ static void new_get_kernel_symbols(void)
  retry_kern_sym_load:
        if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
                if (errno == ENOSPC && bufsize < ret) {
-                       syms = xrealloc(syms, bufsize = ret);
+                       bufsize = ret;
+                       syms = xrealloc(syms, bufsize);
                        goto retry_kern_sym_load;
                }
                bb_perror_msg_and_die("kernel: QM_SYMBOLS");
@@ -2895,7 +2895,7 @@ new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
 
        sec = obj_find_section(f, ".this");
        if (!sec || !sec->contents) {
-               bb_perror_msg_and_die("corrupt module %s?",m_name);
+               bb_perror_msg_and_die("corrupt module %s?", m_name);
        }
        module = (struct new_module *) sec->contents;
        m_addr = sec->header.sh_addr;
@@ -3082,7 +3082,7 @@ static void obj_allocate_commons(struct obj_file *f)
                if (i == f->header.e_shnum) {
                        struct obj_section *sec;
 
-                       f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
+                       f->sections = xrealloc_vector(f->sections, 2, i);
                        f->sections[i] = sec = arch_new_section();
                        f->header.e_shnum = i + 1;
 
@@ -3308,7 +3308,7 @@ static int obj_create_image(struct obj_file *f, char *image)
 
 /*======================================================================*/
 
-static struct obj_file *obj_load(FILE * fp, int loadprogbits ATTRIBUTE_UNUSED)
+static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM)
 {
        struct obj_file *f;
        ElfW(Shdr) * section_headers;
@@ -3514,7 +3514,7 @@ static struct obj_file *obj_load(FILE * fp, int loadprogbits ATTRIBUTE_UNUSED)
  * kernel for the module
  */
 
-static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
+static int obj_load_progbits(FILE *fp, struct obj_file *f, char *imagebase)
 {
        ElfW(Addr) base = f->baseaddr;
        struct obj_section* sec;
@@ -3999,7 +3999,7 @@ int insmod_main(int argc, char **argv)
                        char *module_dir;
                        char *tmdn;
 
-                       tmdn = concat_path_file(_PATH_MODULES, myuname.release);
+                       tmdn = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, myuname.release);
                        /* Jump through hoops in case /lib/modules/`uname -r`
                         * is a symlink.  We do not want recursive_action to
                         * follow symlinks, but we do want to follow the
@@ -4021,9 +4021,9 @@ int insmod_main(int argc, char **argv)
 
                        free(m_filename);
                        m_filename = NULL;
-                       module_dir = xmalloc_readlink(_PATH_MODULES);
+                       module_dir = xmalloc_readlink(CONFIG_DEFAULT_MODULES_DIR);
                        if (!module_dir)
-                               module_dir = xstrdup(_PATH_MODULES);
+                               module_dir = xstrdup(CONFIG_DEFAULT_MODULES_DIR);
                        /* No module found under /lib/modules/`uname -r`, this
                         * time cast the net a bit wider.  Search /lib/modules/ */
                        r = recursive_action(module_dir, ACTION_RECURSE,
@@ -4143,18 +4143,18 @@ int insmod_main(int argc, char **argv)
         * now we can load them directly into the kernel memory
         */
        if (!obj_load_progbits(fp, f, (char*)m_addr)) {
-               delete_module(m_name);
+               delete_module(m_name, 0);
                goto out;
        }
 #endif
 
        if (!obj_relocate(f, m_addr)) {
-               delete_module(m_name);
+               delete_module(m_name, 0);
                goto out;
        }
 
        if (!new_init_module(m_name, f, m_size)) {
-               delete_module(m_name);
+               delete_module(m_name, 0);
                goto out;
        }
 
@@ -4185,7 +4185,8 @@ int insmod_main(int argc, char **argv)
 
 #include <sys/mman.h>
 
-#ifdef __UCLIBC__
+#if defined __UCLIBC__ && !ENABLE_FEATURE_2_4_MODULES
+/* big time suckage. The old prototype above renders our nice fwd-decl wrong */
 extern int init_module(void *module, unsigned long len, const char *options);
 #else
 #include <asm/unistd.h>
@@ -4212,9 +4213,9 @@ static const char *moderror(int err)
 
 #if !ENABLE_FEATURE_2_4_MODULES
 int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int insmod_main(int argc ATTRIBUTE_UNUSED, char **argv)
+int insmod_main(int argc UNUSED_PARAM, char **argv)
 #else
-static int insmod_ng_main(int argc ATTRIBUTE_UNUSED, char **argv)
+static int insmod_ng_main(int argc UNUSED_PARAM, char **argv)
 #endif
 {
        size_t len;
@@ -4236,12 +4237,15 @@ static int insmod_ng_main(int argc ATTRIBUTE_UNUSED, char **argv)
        }
 
 #if 0
-       /* Any special reason why mmap? It isn't performace critical... */
-
-       /* yes, xmalloc'ing can use *alot* of RAM. Don't forget that there are
+       /* Any special reason why mmap? It isn't performance critical. -vda */
+       /* Yes, xmalloc'ing can use *alot* of RAM. Don't forget that there are
         * modules out there that are half a megabyte! mmap()ing is way nicer
-        * for small mem boxes, i guess.
-        */
+        * for small mem boxes, i guess. */
+       /* But after load, these modules will take up that 0.5mb in kernel
+        * anyway. Using malloc here causes only a transient spike to 1mb,
+        * after module is loaded, we go back to normal 0.5mb usage
+        * (in kernel). Also, mmap isn't magic - when we touch mapped data,
+        * we use memory. -vda */
        int fd;
        struct stat st;
        unsigned long len;