ignore the bootloader's commandline on orion
[oweals/openwrt.git] / package / busybox / patches / 470-insmod_search.patch
1 Index: busybox-1.8.1/modutils/insmod.c
2 ===================================================================
3 --- busybox-1.8.1.orig/modutils/insmod.c        2007-11-10 02:40:49.000000000 +0100
4 +++ busybox-1.8.1/modutils/insmod.c     2007-11-10 17:28:44.391223047 +0100
5 @@ -61,21 +61,117 @@
6  #include "libbb.h"
7  #include <libgen.h>
8  #include <sys/utsname.h>
9 +#if ENABLE_FEATURE_2_6_MODULES
10 +#include <sys/mman.h>
11 +#include <asm/unistd.h>
12 +#include <sys/syscall.h>
13 +#endif
14  
15  #if !ENABLE_FEATURE_2_4_MODULES && !ENABLE_FEATURE_2_6_MODULES
16  #undef ENABLE_FEATURE_2_4_MODULES
17  #define ENABLE_FEATURE_2_4_MODULES 1
18  #endif
19  
20 -/*
21 - * Big piece of 2.4-specific code
22 - */
23  #if ENABLE_FEATURE_2_4_MODULES
24 -
25 +int insmod_main_24(int argc, char **argv);
26 +#endif
27  #if ENABLE_FEATURE_2_6_MODULES
28 -static int insmod_ng_main(int argc, char **argv);
29 +int insmod_main_26(int argc, char **argv);
30 +#endif
31 +int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
32 +
33 +static char *g_filename = NULL;
34 +#define _PATH_MODULES  "/lib/modules"
35 +
36 +static int check_module_name_match(const char *filename, struct stat *statbuf,
37 +                                  void *userdata, int depth)
38 +{
39 +       char *fullname = (char *) userdata;
40 +       char *tmp;
41 +
42 +       if (fullname[0] == '\0')
43 +               return FALSE;
44 +
45 +       tmp = bb_get_last_path_component_nostrip(filename);
46 +       if (strcmp(tmp, fullname) == 0) {
47 +               /* Stop searching if we find a match */
48 +               g_filename = xstrdup(filename);
49 +               return FALSE;
50 +       }
51 +
52 +       return TRUE;
53 +}
54 +
55 +static int find_module(char *filename)
56 +{
57 +       char *module_dir, real_module_dir[FILENAME_MAX];
58 +       int len, slen, ret = ENOENT, k_version;
59 +       struct utsname myuname;
60 +       const char *suffix;
61 +       struct stat st;
62 +
63 +       /* check the kernel version */
64 +       if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
65 +               return EINVAL;
66 +
67 +       k_version = myuname.release[2] - '0';
68 +#if ENABLE_FEATURE_2_4_MODULES
69 +       if (k_version <= 4)
70 +               suffix = ".o";
71 +       else
72  #endif
73 +               suffix = ".ko";
74 +
75 +       len = strlen(filename);
76 +       slen = strlen(suffix);
77 +
78 +       /* check for suffix and absolute path first */
79 +       if ((len < slen + 2) || (strcmp(filename + len - slen, suffix) != 0)) {
80 +               filename = xasprintf("%s%s", filename, suffix);
81 +       } else {
82 +               filename = strdup(filename);
83 +               if ((stat(filename, &st) == 0) && S_ISREG(st.st_mode)) {
84 +                       g_filename = filename;
85 +                       return 0;
86 +               }
87 +               free(filename);
88 +               return ENOENT;
89 +       }
90 +
91 +       /* next: scan /lib/modules/<release> */
92 +       /* Jump through hoops in case /lib/modules/`uname -r`
93 +       * is a symlink.  We do not want recursive_action to
94 +       * follow symlinks, but we do want to follow the
95 +       * /lib/modules/`uname -r` dir, So resolve it ourselves
96 +       * if it is a link... */
97 +       module_dir = concat_path_file(_PATH_MODULES, myuname.release);
98 +       if (realpath(module_dir, real_module_dir) != NULL) {
99 +               free(module_dir);
100 +               module_dir = real_module_dir;
101 +       }
102 +
103 +       recursive_action(module_dir, ACTION_RECURSE,
104 +               check_module_name_match, 0, filename, 0);
105 +
106 +       /* Check if we have a complete path */
107 +       if (g_filename == NULL)
108 +               goto done;
109 +
110 +       if ((stat(g_filename, &st) == 0) && S_ISREG(st.st_mode))
111 +               ret = 0;
112 +       else
113 +               free(g_filename);
114  
115 +done:
116 +       free(filename);
117 +
118 +       return ret;
119 +}
120 +
121 +/*
122 + * Big piece of 2.4-specific code
123 + */
124 +#if ENABLE_FEATURE_2_4_MODULES
125  #if ENABLE_FEATURE_INSMOD_LOADINKMEM
126  #define LOADBITS 0
127  #else
128 @@ -673,7 +769,6 @@
129  #endif
130  
131  
132 -#define _PATH_MODULES  "/lib/modules"
133  enum { STRVERSIONLEN = 64 };
134  
135  /*======================================================================*/
136 @@ -789,27 +884,6 @@
137  static char *m_fullName;
138  
139  
140 -/*======================================================================*/
141 -
142 -
143 -static int check_module_name_match(const char *filename, struct stat *statbuf,
144 -                               void *userdata, int depth)
145 -{
146 -       char *fullname = (char *) userdata;
147 -       char *tmp;
148 -
149 -       if (fullname[0] == '\0')
150 -               return FALSE;
151 -
152 -       tmp = bb_get_last_path_component_nostrip(filename);
153 -       if (strcmp(tmp, fullname) == 0) {
154 -               /* Stop searching if we find a match */
155 -               m_filename = xstrdup(filename);
156 -               return FALSE;
157 -       }
158 -       return TRUE;
159 -}
160 -
161  
162  /*======================================================================*/
163  
164 @@ -3897,145 +3971,57 @@
165  void print_load_map(struct obj_file *f);
166  #endif
167  
168 -int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
169 -int insmod_main(int argc, char **argv)
170 +int insmod_main_24( int argc, char **argv)
171  {
172         char *opt_o, *arg1;
173 -       int len;
174         int k_crcs;
175 -       char *tmp, *tmp1;
176         unsigned long m_size;
177         ElfW(Addr) m_addr;
178         struct obj_file *f;
179 -       struct stat st;
180 -       char *m_name = NULL;
181 -       int exit_status = EXIT_FAILURE;
182 +       char *tmp = NULL, *m_name = NULL;
183 +       int ret = EINVAL;
184         int m_has_modinfo;
185  #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
186         struct utsname uts_info;
187         char m_strversion[STRVERSIONLEN];
188         int m_version, m_crcs;
189  #endif
190 -#if ENABLE_FEATURE_CLEAN_UP
191         FILE *fp = NULL;
192 -#else
193 -       FILE *fp;
194 -#endif
195 -       int k_version = 0;
196 +       int k_version;
197         struct utsname myuname;
198  
199 +       /* check the kernel version */
200 +       if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
201 +               return EINVAL;
202 +
203 +       k_version = myuname.release[2] - '0';
204 +       if (k_version > 4)
205 +               return ENOTSUP;
206 +
207         /* Parse any options */
208         getopt32(argv, OPTION_STR, &opt_o);
209         arg1 = argv[optind];
210         if (option_mask32 & OPT_o) { // -o /* name the output module */
211 -               free(m_name);
212                 m_name = xstrdup(opt_o);
213         }
214  
215 -       if (arg1 == NULL) {
216 +       if (arg1 == NULL)
217                 bb_show_usage();
218 -       }
219 -
220 -       /* Grab the module name */
221 -       tmp1 = xstrdup(arg1);
222 -       tmp = basename(tmp1);
223 -       len = strlen(tmp);
224 -
225 -       if (uname(&myuname) == 0) {
226 -               if (myuname.release[0] == '2') {
227 -                       k_version = myuname.release[2] - '0';
228 -               }
229 -       }
230 -
231 -#if ENABLE_FEATURE_2_6_MODULES
232 -       if (k_version > 4 && len > 3 && tmp[len - 3] == '.'
233 -        && tmp[len - 2] == 'k' && tmp[len - 1] == 'o'
234 -       ) {
235 -               len -= 3;
236 -               tmp[len] = '\0';
237 -       } else
238 -#endif
239 -               if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
240 -                       len -= 2;
241 -                       tmp[len] = '\0';
242 -               }
243  
244 -
245 -#if ENABLE_FEATURE_2_6_MODULES
246 -       if (k_version > 4)
247 -               m_fullName = xasprintf("%s.ko", tmp);
248 -       else
249 -#endif
250 -               m_fullName = xasprintf("%s.o", tmp);
251 +       ret = find_module(arg1);
252 +       if (ret)
253 +               goto out;
254  
255         if (!m_name) {
256 -               m_name = tmp;
257 -       } else {
258 -               free(tmp1);
259 -               tmp1 = NULL;       /* flag for free(m_name) before exit() */
260 +               tmp = xstrdup(arg1);
261 +               m_name = basename(tmp);
262         }
263  
264 -       /* Get a filedesc for the module.  Check that we have a complete path */
265 -       if (stat(arg1, &st) < 0 || !S_ISREG(st.st_mode)
266 -        || (fp = fopen(arg1, "r")) == NULL
267 -       ) {
268 -               /* Hmm.  Could not open it.  First search under /lib/modules/`uname -r`,
269 -                * but do not error out yet if we fail to find it... */
270 -               if (k_version) {        /* uname succeedd */
271 -                       char *module_dir;
272 -                       char *tmdn;
273 -
274 -                       tmdn = concat_path_file(_PATH_MODULES, myuname.release);
275 -                       /* Jump through hoops in case /lib/modules/`uname -r`
276 -                        * is a symlink.  We do not want recursive_action to
277 -                        * follow symlinks, but we do want to follow the
278 -                        * /lib/modules/`uname -r` dir, So resolve it ourselves
279 -                        * if it is a link... */
280 -                       module_dir = xmalloc_readlink(tmdn);
281 -                       if (!module_dir)
282 -                               module_dir = xstrdup(tmdn);
283 -                       recursive_action(module_dir, ACTION_RECURSE,
284 -                                       check_module_name_match, NULL, m_fullName, 0);
285 -                       free(module_dir);
286 -                       free(tmdn);
287 -               }
288 -
289 -               /* Check if we have found anything yet */
290 -               if (!m_filename || ((fp = fopen(m_filename, "r")) == NULL)) {
291 -                       int r;
292 -                       char *module_dir;
293 -
294 -                       free(m_filename);
295 -                       m_filename = NULL;
296 -                       module_dir = xmalloc_readlink(_PATH_MODULES);
297 -                       if (!module_dir)
298 -                               module_dir = xstrdup(_PATH_MODULES);
299 -                       /* No module found under /lib/modules/`uname -r`, this
300 -                        * time cast the net a bit wider.  Search /lib/modules/ */
301 -                       r = recursive_action(module_dir, ACTION_RECURSE,
302 -                                       check_module_name_match, NULL, m_fullName, 0);
303 -                       if (r)
304 -                               bb_error_msg_and_die("%s: module not found", m_fullName);
305 -                       free(module_dir);
306 -                       if (m_filename == NULL
307 -                        || ((fp = fopen(m_filename, "r")) == NULL)
308 -                       ) {
309 -                               bb_error_msg_and_die("%s: module not found", m_fullName);
310 -                       }
311 -               }
312 -       } else
313 -               m_filename = xstrdup(arg1);
314 -
315 -       if (flag_verbose)
316 -               printf("Using %s\n", m_filename);
317 -
318 -#if ENABLE_FEATURE_2_6_MODULES
319 -       if (k_version > 4) {
320 -               argv[optind] = m_filename;
321 -               optind--;
322 -               return insmod_ng_main(argc - optind, argv + optind);
323 +       fp = fopen(g_filename, "r");
324 +       if (!fp) {
325 +               ret = errno;
326 +               goto out;
327         }
328 -#endif
329  
330         f = obj_load(fp, LOADBITS);
331  
332 @@ -4062,7 +4048,7 @@
333                                 "\t%s was compiled for kernel version %s\n"
334                                 "\twhile this kernel is version %s",
335                                 flag_force_load ? "warning: " : "",
336 -                               m_filename, m_strversion, uts_info.release);
337 +                               g_filename, m_strversion, uts_info.release);
338                         if (!flag_force_load)
339                                 goto out;
340                 }
341 @@ -4104,7 +4090,7 @@
342         hide_special_symbols(f);
343  
344  #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
345 -       add_ksymoops_symbols(f, m_filename, m_name);
346 +       add_ksymoops_symbols(f, g_filename, m_name);
347  #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
348  
349         new_create_module_ksymtab(f);
350 @@ -4147,18 +4133,19 @@
351         if (flag_print_load_map)
352                 print_load_map(f);
353  
354 -       exit_status = EXIT_SUCCESS;
355 +       ret = EXIT_SUCCESS;
356  
357   out:
358  #if ENABLE_FEATURE_CLEAN_UP
359         if (fp)
360                 fclose(fp);
361 -       free(tmp1);
362 -       if (!tmp1)
363 +       if (tmp)
364 +               free(tmp);
365 +       else if (m_name)
366                 free(m_name);
367 -       free(m_filename);
368 +       free(g_filename);
369  #endif
370 -       return exit_status;
371 +       return ret;
372  }
373  
374  #endif /* ENABLE_FEATURE_2_4_MODULES */
375 @@ -4190,23 +4177,32 @@
376         }
377  }
378  
379 -#if !ENABLE_FEATURE_2_4_MODULES
380 -int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
381 -int insmod_main(int argc, char **argv)
382 -#else
383 -static int insmod_ng_main(int argc, char **argv)
384 -#endif
385 +int insmod_main_26(int argc, char **argv)
386  {
387 -       long ret;
388 -       size_t len;
389 +       char *filename, *options;
390 +       struct utsname myuname;
391 +       int k_version;
392         int optlen;
393 +       size_t len;
394         void *map;
395 -       char *filename, *options;
396 +       long ret = 0;
397 +
398 +       /* check the kernel version */
399 +       if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
400 +               return EINVAL;
401 +
402 +       k_version = myuname.release[2] - '0';
403 +       if (k_version <= 4)
404 +               return ENOTSUP;
405  
406         filename = *++argv;
407         if (!filename)
408                 bb_show_usage();
409  
410 +       ret = find_module(filename);
411 +       if (ret || (g_filename == NULL))
412 +               goto done;
413 +
414         /* Rest is options */
415         options = xzalloc(1);
416         optlen = 0;
417 @@ -4216,36 +4212,47 @@
418                 optlen += sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
419         }
420  
421 -#if 0
422 -       /* Any special reason why mmap? It isn't performace critical... */
423 -       int fd;
424 -       struct stat st;
425 -       unsigned long len;
426 -       fd = xopen(filename, O_RDONLY);
427 -       fstat(fd, &st);
428 -       len = st.st_size;
429 -       map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
430 -       if (map == MAP_FAILED) {
431 -               bb_perror_msg_and_die("cannot mmap '%s'", filename);
432 -       }
433 -
434 -       /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
435 -       if (map == NULL) {
436 -               map = xmalloc(len);
437 -               xread(fd, map, len);
438 -       }
439 -#else
440         len = MAXINT(ssize_t);
441 -       map = xmalloc_open_read_close(filename, &len);
442 -#endif
443 -
444 +       map = xmalloc_open_read_close(g_filename, &len);
445         ret = syscall(__NR_init_module, map, len, options);
446         if (ret != 0) {
447                 bb_perror_msg_and_die("cannot insert '%s': %s (%li)",
448 -                               filename, moderror(errno), ret);
449 +                               g_filename, moderror(errno), ret);
450         }
451 +done:
452 +       if (g_filename && (g_filename != filename))
453 +               free(g_filename);
454  
455 -       return 0;
456 +       return ret;
457  }
458  
459  #endif
460 +
461 +int insmod_main(int argc, char **argv)
462 +{
463 +       int ret;
464 +
465 +       g_filename = NULL;
466 +#if ENABLE_FEATURE_2_6_MODULES
467 +       ret = insmod_main_26(argc, argv);
468 +       if (ret != ENOTSUP)
469 +               goto done;
470 +#endif
471 +
472 +#if ENABLE_FEATURE_2_4_MODULES
473 +       ret = insmod_main_24(argc, argv);
474 +       if (ret != ENOTSUP)
475 +               goto done;
476 +#endif
477 +
478 +       fprintf(stderr, "Error: Kernel version not supported\n");
479 +       return 1;
480 +
481 +done:
482 +       if (ret) {
483 +               errno = ret;
484 +               bb_perror_msg("Loading module failed");
485 +               return ret;
486 +       } else
487 +               return 0;
488 +}