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