2a08fb74f253a18d67398da3c280871a3b62a0b1
[librecmc/librecmc.git] / package / busybox / patches / 470-insmod_search.patch
1 --- a/modutils/insmod.c
2 +++ b/modutils/insmod.c
3 @@ -61,21 +61,117 @@
4  #include "libbb.h"
5  #include <libgen.h>
6  #include <sys/utsname.h>
7 +#if ENABLE_FEATURE_2_6_MODULES
8 +#include <sys/mman.h>
9 +#include <asm/unistd.h>
10 +#include <sys/syscall.h>
11 +#endif
12  
13  #if !ENABLE_FEATURE_2_4_MODULES && !ENABLE_FEATURE_2_6_MODULES
14  #undef ENABLE_FEATURE_2_4_MODULES
15  #define ENABLE_FEATURE_2_4_MODULES 1
16  #endif
17  
18 -/*
19 - * Big piece of 2.4-specific code
20 - */
21  #if ENABLE_FEATURE_2_4_MODULES
22 -
23 +int insmod_main_24(int argc, char **argv);
24 +#endif
25  #if ENABLE_FEATURE_2_6_MODULES
26 -static int insmod_ng_main(int argc, char **argv);
27 +int insmod_main_26(int argc, char **argv);
28  #endif
29 +int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
30  
31 +static char *g_filename = NULL;
32 +#define _PATH_MODULES  "/lib/modules"
33 +
34 +static int check_module_name_match(const char *filename, struct stat *statbuf,
35 +                                  void *userdata, int depth)
36 +{
37 +       char *fullname = (char *) userdata;
38 +       char *tmp;
39 +
40 +       if (fullname[0] == '\0')
41 +               return FALSE;
42 +
43 +       tmp = bb_get_last_path_component_nostrip(filename);
44 +       if (strcmp(tmp, fullname) == 0) {
45 +               /* Stop searching if we find a match */
46 +               g_filename = xstrdup(filename);
47 +               return FALSE;
48 +       }
49 +
50 +       return TRUE;
51 +}
52 +
53 +static int find_module(char *filename)
54 +{
55 +       char *module_dir, real_module_dir[FILENAME_MAX];
56 +       int len, slen, ret = ENOENT, k_version;
57 +       struct utsname myuname;
58 +       const char *suffix;
59 +       struct stat st;
60 +
61 +       /* check the kernel version */
62 +       if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
63 +               return EINVAL;
64 +
65 +       k_version = myuname.release[2] - '0';
66 +#if ENABLE_FEATURE_2_4_MODULES
67 +       if (k_version <= 4)
68 +               suffix = ".o";
69 +       else
70 +#endif
71 +               suffix = ".ko";
72 +
73 +       len = strlen(filename);
74 +       slen = strlen(suffix);
75 +
76 +       /* check for suffix and absolute path first */
77 +       if ((len < slen + 2) || (strcmp(filename + len - slen, suffix) != 0)) {
78 +               filename = xasprintf("%s%s", filename, suffix);
79 +       } else {
80 +               filename = strdup(filename);
81 +               if ((stat(filename, &st) == 0) && S_ISREG(st.st_mode)) {
82 +                       g_filename = filename;
83 +                       return 0;
84 +               }
85 +               free(filename);
86 +               return ENOENT;
87 +       }
88 +
89 +       /* next: scan /lib/modules/<release> */
90 +       /* Jump through hoops in case /lib/modules/`uname -r`
91 +       * is a symlink.  We do not want recursive_action to
92 +       * follow symlinks, but we do want to follow the
93 +       * /lib/modules/`uname -r` dir, So resolve it ourselves
94 +       * if it is a link... */
95 +       module_dir = concat_path_file(_PATH_MODULES, myuname.release);
96 +       if (realpath(module_dir, real_module_dir) != NULL) {
97 +               free(module_dir);
98 +               module_dir = real_module_dir;
99 +       }
100 +
101 +       recursive_action(module_dir, ACTION_RECURSE,
102 +               check_module_name_match, 0, filename, 0);
103 +
104 +       /* Check if we have a complete path */
105 +       if (g_filename == NULL)
106 +               goto done;
107 +
108 +       if ((stat(g_filename, &st) == 0) && S_ISREG(st.st_mode))
109 +               ret = 0;
110 +       else
111 +               free(g_filename);
112 +
113 +done:
114 +       free(filename);
115 +
116 +       return ret;
117 +}
118 +
119 +/*
120 + * Big piece of 2.4-specific code
121 + */
122 +#if ENABLE_FEATURE_2_4_MODULES
123  #if ENABLE_FEATURE_INSMOD_LOADINKMEM
124  #define LOADBITS 0
125  #else
126 @@ -184,7 +280,6 @@
127  /* Microblaze */
128  #if defined(__microblaze__)
129  #define USE_SINGLE
130 -#include <linux/elf-em.h>
131  #define MATCH_MACHINE(x) (x == EM_XILINX_MICROBLAZE)
132  #define SHT_RELM       SHT_RELA
133  #define Elf32_RelM     Elf32_Rela
134 @@ -452,7 +547,7 @@
135  /* The system calls unchanged between 2.0 and 2.1.  */
136  
137  unsigned long create_module(const char *, size_t);
138 -int delete_module(const char *module, unsigned int flags);
139 +int delete_module(const char *);
140  
141  
142  #endif /* module.h */
143 @@ -652,7 +747,7 @@
144  
145  static enum obj_reloc arch_apply_relocation(struct obj_file *f,
146                                       struct obj_section *targsec,
147 -                                     /*struct obj_section *symsec,*/
148 +                                     struct obj_section *symsec,
149                                       struct obj_symbol *sym,
150                                       ElfW(RelM) *rel, ElfW(Addr) value);
151  
152 @@ -673,6 +768,7 @@
153  #define SPFX   ""
154  #endif
155  
156 +
157  enum { STRVERSIONLEN = 64 };
158  
159  /*======================================================================*/
160 @@ -788,28 +884,6 @@
161  static char *m_fullName;
162  
163  
164 -/*======================================================================*/
165 -
166 -
167 -static int check_module_name_match(const char *filename,
168 -               struct stat *statbuf ATTRIBUTE_UNUSED,
169 -               void *userdata, int depth ATTRIBUTE_UNUSED)
170 -{
171 -       char *fullname = (char *) userdata;
172 -       char *tmp;
173 -
174 -       if (fullname[0] == '\0')
175 -               return FALSE;
176 -
177 -       tmp = bb_get_last_path_component_nostrip(filename);
178 -       if (strcmp(tmp, fullname) == 0) {
179 -               /* Stop searching if we find a match */
180 -               m_filename = xstrdup(filename);
181 -               return FALSE;
182 -       }
183 -       return TRUE;
184 -}
185 -
186  
187  /*======================================================================*/
188  
189 @@ -835,27 +909,18 @@
190  static enum obj_reloc
191  arch_apply_relocation(struct obj_file *f,
192                                 struct obj_section *targsec,
193 -                               /*struct obj_section *symsec,*/
194 +                               struct obj_section *symsec,
195                                 struct obj_symbol *sym,
196                                 ElfW(RelM) *rel, ElfW(Addr) v)
197  {
198 -#if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
199 - || defined(__sh__) || defined(__s390__) || defined(__x86_64__)
200         struct arch_file *ifile = (struct arch_file *) f;
201 -#endif
202         enum obj_reloc ret = obj_reloc_ok;
203         ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
204 -#if defined(__arm__) || defined(__H8300H__) || defined(__H8300S__) \
205 - || defined(__i386__) || defined(__mc68000__) || defined(__microblaze__) \
206 - || defined(__mips__) || defined(__nios2__) || defined(__powerpc__) \
207 - || defined(__s390__) || defined(__sh__) || defined(__x86_64__)
208         ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
209 -#endif
210  #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
211         struct arch_symbol *isym = (struct arch_symbol *) sym;
212  #endif
213 -#if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
214 - || defined(__sh__) || defined(__s390__)
215 +#if defined(__arm__) || defined(__i386__) || defined(__mc68000__) || defined(__sh__) || defined(__s390__)
216  #if defined(USE_GOT_ENTRIES)
217         ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
218  #endif
219 @@ -953,7 +1018,6 @@
220  
221                 case R_386_PLT32:
222                 case R_386_PC32:
223 -               case R_386_GOTOFF:
224                         *loc += v - dot;
225                         break;
226  
227 @@ -972,6 +1036,9 @@
228  
229                 case R_386_GOT32:
230                         goto bb_use_got;
231 +
232 +               case R_386_GOTOFF:
233 +                       *loc += v - got;
234                         break;
235  
236  #elif defined(__microblaze__)
237 @@ -1758,7 +1825,7 @@
238  
239  #if defined(USE_SINGLE)
240  
241 -static int arch_single_init(/*ElfW(RelM) *rel,*/ struct arch_single_entry *single,
242 +static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
243                              int offset, int size)
244  {
245         if (single->allocated == 0) {
246 @@ -1906,7 +1973,7 @@
247  #if defined(USE_GOT_ENTRIES)
248                         if (got_allocate) {
249                                 got_offset += arch_single_init(
250 -                                               /*rel,*/ &intsym->gotent,
251 +                                               rel, &intsym->gotent,
252                                                 got_offset, GOT_ENTRY_SIZE);
253  
254                                 got_needed = 1;
255 @@ -1920,7 +1987,7 @@
256                                                 plt_offset, PLT_ENTRY_SIZE);
257  #else
258                                 plt_offset += arch_single_init(
259 -                                               /*rel,*/ &intsym->pltent,
260 +                                               rel, &intsym->pltent,
261                                                 plt_offset, PLT_ENTRY_SIZE);
262  #endif
263                                 plt_needed = 1;
264 @@ -1958,8 +2025,7 @@
265         while (n > 0) {
266                 ch = *name++;
267                 h = (h << 4) + ch;
268 -               g = (h & 0xf0000000);
269 -               if (g != 0) {
270 +               if ((g = (h & 0xf0000000)) != 0) {
271                         h ^= g >> 24;
272                         h &= ~g;
273                 }
274 @@ -2038,7 +2104,7 @@
275         int n_type = ELF_ST_TYPE(info);
276         int n_binding = ELF_ST_BIND(info);
277  
278 -       for (sym = f->symtab[hash]; sym; sym = sym->next) {
279 +       for (sym = f->symtab[hash]; sym; sym = sym->next)
280                 if (f->symbol_cmp(sym->name, name) == 0) {
281                         int o_secidx = sym->secidx;
282                         int o_info = sym->info;
283 @@ -2097,14 +2163,14 @@
284                                 return sym;
285                         }
286                 }
287 -       }
288  
289         /* Completely new symbol.  */
290         sym = arch_new_symbol();
291         sym->next = f->symtab[hash];
292         f->symtab[hash] = sym;
293         sym->ksymidx = -1;
294 -       if (ELF_ST_BIND(info) == STB_LOCAL && symidx != (unsigned long)(-1)) {
295 +
296 +       if (ELF_ST_BIND(info) == STB_LOCAL && symidx != -1) {
297                 if (symidx >= f->local_symtab_size)
298                         bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
299                                         name, (long) symidx, (long) f->local_symtab_size);
300 @@ -3227,7 +3293,7 @@
301  
302                         /* Do it! */
303                         switch (arch_apply_relocation
304 -                                       (f, targsec, /*symsec,*/ intsym, rel, value)
305 +                                       (f, targsec, symsec, intsym, rel, value)
306                         ) {
307                         case obj_reloc_ok:
308                                 break;
309 @@ -3306,11 +3372,11 @@
310  
311  /*======================================================================*/
312  
313 -static struct obj_file *obj_load(FILE * fp, int loadprogbits ATTRIBUTE_UNUSED)
314 +static struct obj_file *obj_load(FILE * fp, int loadprogbits)
315  {
316         struct obj_file *f;
317         ElfW(Shdr) * section_headers;
318 -       size_t shnum, i;
319 +       int shnum, i;
320         char *shstrtab;
321  
322         /* Read the file header.  */
323 @@ -3582,7 +3648,7 @@
324                 while (ptr < endptr) {
325                         value = strchr(ptr, '=');
326                         if (value && strncmp(ptr, "license", value-ptr) == 0) {
327 -                               unsigned i;
328 +                               int i;
329                                 if (license)
330                                         *license = value+1;
331                                 for (i = 0; i < ARRAY_SIZE(gpl_licenses); ++i) {
332 @@ -3686,9 +3752,6 @@
333   * start of some sections.  this info is used by ksymoops to do better
334   * debugging.
335   */
336 -#if !ENABLE_FEATURE_INSMOD_VERSION_CHECKING
337 -#define get_module_version(f, str) get_module_version(str)
338 -#endif
339  static int
340  get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
341  {
342 @@ -3721,8 +3784,7 @@
343         struct obj_symbol *sym;
344         char *name, *absolute_filename;
345         char str[STRVERSIONLEN];
346 -       unsigned i;
347 -       int l, lm_name, lfilename, use_ksymtab, version;
348 +       int i, l, lm_name, lfilename, use_ksymtab, version;
349         struct stat statbuf;
350  
351         /* WARNING: was using realpath, but replaced by readlink to stop using
352 @@ -3909,145 +3971,57 @@
353  void print_load_map(struct obj_file *f);
354  #endif
355  
356 -int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
357 -int insmod_main(int argc, char **argv)
358 +int insmod_main_24( int argc, char **argv)
359  {
360         char *opt_o, *arg1;
361 -       int len;
362         int k_crcs;
363 -       char *tmp, *tmp1;
364         unsigned long m_size;
365         ElfW(Addr) m_addr;
366         struct obj_file *f;
367 -       struct stat st;
368 -       char *m_name = NULL;
369 -       int exit_status = EXIT_FAILURE;
370 +       char *tmp = NULL, *m_name = NULL;
371 +       int ret = EINVAL;
372         int m_has_modinfo;
373  #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
374         struct utsname uts_info;
375         char m_strversion[STRVERSIONLEN];
376         int m_version, m_crcs;
377  #endif
378 -#if ENABLE_FEATURE_CLEAN_UP
379         FILE *fp = NULL;
380 -#else
381 -       FILE *fp;
382 -#endif
383 -       int k_version = 0;
384 +       int k_version;
385         struct utsname myuname;
386  
387 +       /* check the kernel version */
388 +       if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
389 +               return EINVAL;
390 +
391 +       k_version = myuname.release[2] - '0';
392 +       if (k_version > 4)
393 +               return ENOTSUP;
394 +
395         /* Parse any options */
396         getopt32(argv, OPTION_STR, &opt_o);
397         arg1 = argv[optind];
398         if (option_mask32 & OPT_o) { // -o /* name the output module */
399 -               free(m_name);
400                 m_name = xstrdup(opt_o);
401         }
402  
403 -       if (arg1 == NULL) {
404 +       if (arg1 == NULL)
405                 bb_show_usage();
406 -       }
407 -
408 -       /* Grab the module name */
409 -       tmp1 = xstrdup(arg1);
410 -       tmp = basename(tmp1);
411 -       len = strlen(tmp);
412 -
413 -       if (uname(&myuname) == 0) {
414 -               if (myuname.release[0] == '2') {
415 -                       k_version = myuname.release[2] - '0';
416 -               }
417 -       }
418 -
419 -#if ENABLE_FEATURE_2_6_MODULES
420 -       if (k_version > 4 && len > 3 && tmp[len - 3] == '.'
421 -        && tmp[len - 2] == 'k' && tmp[len - 1] == 'o'
422 -       ) {
423 -               len -= 3;
424 -               tmp[len] = '\0';
425 -       } else
426 -#endif
427 -               if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
428 -                       len -= 2;
429 -                       tmp[len] = '\0';
430 -               }
431  
432 -
433 -#if ENABLE_FEATURE_2_6_MODULES
434 -       if (k_version > 4)
435 -               m_fullName = xasprintf("%s.ko", tmp);
436 -       else
437 -#endif
438 -               m_fullName = xasprintf("%s.o", tmp);
439 +       ret = find_module(arg1);
440 +       if (ret)
441 +               goto out;
442  
443         if (!m_name) {
444 -               m_name = tmp;
445 -       } else {
446 -               free(tmp1);
447 -               tmp1 = NULL;       /* flag for free(m_name) before exit() */
448 -       }
449 -
450 -       /* Get a filedesc for the module.  Check that we have a complete path */
451 -       if (stat(arg1, &st) < 0 || !S_ISREG(st.st_mode)
452 -        || (fp = fopen(arg1, "r")) == NULL
453 -       ) {
454 -               /* Hmm.  Could not open it.  First search under /lib/modules/`uname -r`,
455 -                * but do not error out yet if we fail to find it... */
456 -               if (k_version) {        /* uname succeedd */
457 -                       char *module_dir;
458 -                       char *tmdn;
459 -
460 -                       tmdn = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, myuname.release);
461 -                       /* Jump through hoops in case /lib/modules/`uname -r`
462 -                        * is a symlink.  We do not want recursive_action to
463 -                        * follow symlinks, but we do want to follow the
464 -                        * /lib/modules/`uname -r` dir, So resolve it ourselves
465 -                        * if it is a link... */
466 -                       module_dir = xmalloc_readlink(tmdn);
467 -                       if (!module_dir)
468 -                               module_dir = xstrdup(tmdn);
469 -                       recursive_action(module_dir, ACTION_RECURSE,
470 -                                       check_module_name_match, NULL, m_fullName, 0);
471 -                       free(module_dir);
472 -                       free(tmdn);
473 -               }
474 -
475 -               /* Check if we have found anything yet */
476 -               if (!m_filename || ((fp = fopen(m_filename, "r")) == NULL)) {
477 -                       int r;
478 -                       char *module_dir;
479 -
480 -                       free(m_filename);
481 -                       m_filename = NULL;
482 -                       module_dir = xmalloc_readlink(CONFIG_DEFAULT_MODULES_DIR);
483 -                       if (!module_dir)
484 -                               module_dir = xstrdup(CONFIG_DEFAULT_MODULES_DIR);
485 -                       /* No module found under /lib/modules/`uname -r`, this
486 -                        * time cast the net a bit wider.  Search /lib/modules/ */
487 -                       r = recursive_action(module_dir, ACTION_RECURSE,
488 -                                       check_module_name_match, NULL, m_fullName, 0);
489 -                       if (r)
490 -                               bb_error_msg_and_die("%s: module not found", m_fullName);
491 -                       free(module_dir);
492 -                       if (m_filename == NULL
493 -                        || ((fp = fopen(m_filename, "r")) == NULL)
494 -                       ) {
495 -                               bb_error_msg_and_die("%s: module not found", m_fullName);
496 -                       }
497 +               tmp = xstrdup(arg1);
498 +               m_name = basename(tmp);
499                 }
500 -       } else
501 -               m_filename = xstrdup(arg1);
502  
503 -       if (flag_verbose)
504 -               printf("Using %s\n", m_filename);
505 -
506 -#if ENABLE_FEATURE_2_6_MODULES
507 -       if (k_version > 4) {
508 -               argv[optind] = m_filename;
509 -               optind--;
510 -               return insmod_ng_main(argc - optind, argv + optind);
511 +       fp = fopen(g_filename, "r");
512 +       if (!fp) {
513 +               ret = errno;
514 +               goto out;
515         }
516 -#endif
517  
518         f = obj_load(fp, LOADBITS);
519  
520 @@ -4074,7 +4048,7 @@
521                                 "\t%s was compiled for kernel version %s\n"
522                                 "\twhile this kernel is version %s",
523                                 flag_force_load ? "warning: " : "",
524 -                               m_filename, m_strversion, uts_info.release);
525 +                               g_filename, m_strversion, uts_info.release);
526                         if (!flag_force_load)
527                                 goto out;
528                 }
529 @@ -4116,7 +4090,7 @@
530         hide_special_symbols(f);
531  
532  #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
533 -       add_ksymoops_symbols(f, m_filename, m_name);
534 +       add_ksymoops_symbols(f, g_filename, m_name);
535  #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
536  
537         new_create_module_ksymtab(f);
538 @@ -4125,7 +4099,7 @@
539         m_size = obj_load_size(f);
540  
541         m_addr = create_module(m_name, m_size);
542 -       if (m_addr == (ElfW(Addr))(-1)) switch (errno) {
543 +       if (m_addr == -1) switch (errno) {
544                 case EEXIST:
545                         bb_error_msg_and_die("a module named %s already exists", m_name);
546                 case ENOMEM:
547 @@ -4141,36 +4115,37 @@
548          * now we can load them directly into the kernel memory
549          */
550         if (!obj_load_progbits(fp, f, (char*)m_addr)) {
551 -               delete_module(m_name, 0);
552 +               delete_module(m_name);
553                 goto out;
554         }
555  #endif
556  
557         if (!obj_relocate(f, m_addr)) {
558 -               delete_module(m_name, 0);
559 +               delete_module(m_name);
560                 goto out;
561         }
562  
563         if (!new_init_module(m_name, f, m_size)) {
564 -               delete_module(m_name, 0);
565 +               delete_module(m_name);
566                 goto out;
567         }
568  
569         if (flag_print_load_map)
570                 print_load_map(f);
571  
572 -       exit_status = EXIT_SUCCESS;
573 +       ret = EXIT_SUCCESS;
574  
575   out:
576  #if ENABLE_FEATURE_CLEAN_UP
577         if (fp)
578                 fclose(fp);
579 -       free(tmp1);
580 -       if (!tmp1)
581 +       if (tmp)
582 +               free(tmp);
583 +       else if (m_name)
584                 free(m_name);
585 -       free(m_filename);
586 +       free(g_filename);
587  #endif
588 -       return exit_status;
589 +       return ret;
590  }
591  
592  #endif /* ENABLE_FEATURE_2_4_MODULES */
593 @@ -4182,15 +4157,8 @@
594  #if ENABLE_FEATURE_2_6_MODULES
595  
596  #include <sys/mman.h>
597 -
598 -#if defined __UCLIBC__ && !ENABLE_FEATURE_2_4_MODULES
599 -/* big time suckage. The old prototype above renders our nice fwd-decl wrong */
600 -extern int init_module(void *module, unsigned long len, const char *options);
601 -#else
602  #include <asm/unistd.h>
603  #include <sys/syscall.h>
604 -#define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
605 -#endif
606  
607  /* We use error numbers in a loose translation... */
608  static const char *moderror(int err)
609 @@ -4209,22 +4177,32 @@
610         }
611  }
612  
613 -#if !ENABLE_FEATURE_2_4_MODULES
614 -int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
615 -int insmod_main(int argc ATTRIBUTE_UNUSED, char **argv)
616 -#else
617 -static int insmod_ng_main(int argc ATTRIBUTE_UNUSED, char **argv)
618 -#endif
619 +int insmod_main_26(int argc, char **argv)
620  {
621 -       size_t len;
622 +       char *filename, *options;
623 +       struct utsname myuname;
624 +       int k_version;
625         int optlen;
626 +       size_t len;
627         void *map;
628 -       char *filename, *options;
629 +       long ret = 0;
630 +
631 +       /* check the kernel version */
632 +       if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
633 +               return EINVAL;
634 +
635 +       k_version = myuname.release[2] - '0';
636 +       if (k_version <= 4)
637 +               return ENOTSUP;
638  
639         filename = *++argv;
640         if (!filename)
641                 bb_show_usage();
642  
643 +       ret = find_module(filename);
644 +       if (ret || (g_filename == NULL))
645 +               goto done;
646 +
647         /* Rest is options */
648         options = xzalloc(1);
649         optlen = 0;
650 @@ -4234,41 +4212,47 @@
651                 optlen += sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
652         }
653  
654 -#if 0
655 -       /* Any special reason why mmap? It isn't performance critical. -vda */
656 -       /* Yes, xmalloc'ing can use *alot* of RAM. Don't forget that there are
657 -        * modules out there that are half a megabyte! mmap()ing is way nicer
658 -        * for small mem boxes, i guess. */
659 -       /* But after load, these modules will take up that 0.5mb in kernel
660 -        * anyway. Using malloc here causes only a transient spike to 1mb,
661 -        * after module is loaded, we go back to normal 0.5mb usage
662 -        * (in kernel). Also, mmap isn't magic - when we touch mapped data,
663 -        * we use memory. -vda */
664 -       int fd;
665 -       struct stat st;
666 -       unsigned long len;
667 -       fd = xopen(filename, O_RDONLY);
668 -       fstat(fd, &st);
669 -       len = st.st_size;
670 -       map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
671 -       if (map == MAP_FAILED) {
672 -               bb_perror_msg_and_die("cannot mmap '%s'", filename);
673 -       }
674 -
675 -       /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
676 -       if (map == NULL) {
677 -               map = xmalloc(len);
678 -               xread(fd, map, len);
679 -       }
680 -#else
681         len = MAXINT(ssize_t);
682 -       map = xmalloc_open_read_close(filename, &len);
683 -#endif
684 +       map = xmalloc_open_read_close(g_filename, &len);
685 +       ret = syscall(__NR_init_module, map, len, options);
686 +       if (ret != 0) {
687 +               bb_perror_msg_and_die("cannot insert '%s': %s (%li)",
688 +                               g_filename, moderror(errno), ret);
689 +       }
690 +done:
691 +       if (g_filename && (g_filename != filename))
692 +               free(g_filename);
693  
694 -       if (init_module(map, len, options) != 0)
695 -               bb_error_msg_and_die("cannot insert '%s': %s",
696 -                               filename, moderror(errno));
697 -       return 0;
698 +       return ret;
699  }
700  
701  #endif
702 +
703 +int insmod_main(int argc, char **argv)
704 +{
705 +       int ret;
706 +
707 +       g_filename = NULL;
708 +#if ENABLE_FEATURE_2_6_MODULES
709 +       ret = insmod_main_26(argc, argv);
710 +       if (ret != ENOTSUP)
711 +               goto done;
712 +#endif
713 +
714 +#if ENABLE_FEATURE_2_4_MODULES
715 +       ret = insmod_main_24(argc, argv);
716 +       if (ret != ENOTSUP)
717 +               goto done;
718 +#endif
719 +
720 +       fprintf(stderr, "Error: Kernel version not supported\n");
721 +       return 1;
722 +
723 +done:
724 +       if (ret) {
725 +               errno = ret;
726 +               bb_perror_msg("Loading module failed");
727 +               return ret;
728 +       } else
729 +               return 0;
730 +}