fix empty download dir error
[openwrt/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-10-24 18:20:56.857757117 +0200
4 +++ busybox-1.7.2/modutils/insmod.c 2007-10-24 18:38:27.701641299 +0200
5 @@ -61,19 +61,114 @@
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 + g_filename = filename;
86 + return 0;
87 + }
88 + free(filename);
89 + return ENOENT;
90 + }
91 +
92 + /* next: scan /lib/modules/<release> */
93 + /* Jump through hoops in case /lib/modules/`uname -r`
94 + * is a symlink. We do not want recursive_action to
95 + * follow symlinks, but we do want to follow the
96 + * /lib/modules/`uname -r` dir, So resolve it ourselves
97 + * if it is a link... */
98 + module_dir = concat_path_file(_PATH_MODULES, myuname.release);
99 + if (realpath(module_dir, real_module_dir) != NULL) {
100 + free(module_dir);
101 + module_dir = real_module_dir;
102 + }
103 +
104 + recursive_action(module_dir, ACTION_RECURSE,
105 + check_module_name_match, 0, filename, 0);
106 +
107 + /* Check if we have a complete path */
108 + if (g_filename == NULL)
109 + goto done;
110 +
111 + if ((stat(g_filename, &st) == 0) && S_ISREG(st.st_mode))
112 + ret = 0;
113 + else
114 + free(g_filename);
115 +
116 +done:
117 + free(filename);
118 +
119 + return ret;
120 +}
121
122
123 #if ENABLE_FEATURE_2_4_MODULES
124 @@ -677,7 +772,6 @@
125 #endif
126
127
128 -#define _PATH_MODULES "/lib/modules"
129 enum { STRVERSIONLEN = 64 };
130
131 /*======================================================================*/
132 @@ -790,37 +884,6 @@
133 static int n_ext_modules_used;
134 extern int delete_module(const char *);
135
136 -static char *m_filename;
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 -
148 - if (fullname[0] == '\0')
149 - return FALSE;
150 - else {
151 - char *tmp, *tmp1 = xstrdup(filename);
152 - tmp = bb_get_last_path_component(tmp1);
153 - if (strcmp(tmp, fullname) == 0) {
154 - free(tmp1);
155 - /* Stop searching if we find a match */
156 - m_filename = xstrdup(filename);
157 - return FALSE;
158 - }
159 - free(tmp1);
160 - }
161 - return TRUE;
162 -}
163 -
164 -
165 -/*======================================================================*/
166 -
167 static struct obj_file *arch_new_file(void)
168 {
169 struct arch_file *f;
170 @@ -3952,145 +4015,57 @@
171 void print_load_map(struct obj_file *f);
172 #endif
173
174 -int insmod_main( int argc, char **argv);
175 -int insmod_main( int argc, char **argv)
176 +int insmod_main_24( int argc, char **argv)
177 {
178 char *opt_o, *arg1;
179 - int len;
180 int k_crcs;
181 - char *tmp, *tmp1;
182 unsigned long m_size;
183 ElfW(Addr) m_addr;
184 struct obj_file *f;
185 - struct stat st;
186 - char *m_name = 0;
187 - int exit_status = EXIT_FAILURE;
188 + char *tmp = NULL, *m_name = NULL;
189 + int ret = EINVAL;
190 int m_has_modinfo;
191 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
192 struct utsname uts_info;
193 char m_strversion[STRVERSIONLEN];
194 int m_version, m_crcs;
195 #endif
196 -#if ENABLE_FEATURE_CLEAN_UP
197 - FILE *fp = 0;
198 -#else
199 - FILE *fp;
200 -#endif
201 - int k_version = 0;
202 + FILE *fp = NULL;
203 + int k_version;
204 struct utsname myuname;
205
206 + /* check the kernel version */
207 + if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
208 + return EINVAL;
209 +
210 + k_version = myuname.release[2] - '0';
211 + if (k_version > 4)
212 + return ENOTSUP;
213 +
214 /* Parse any options */
215 getopt32(argv, OPTION_STR, &opt_o);
216 arg1 = argv[optind];
217 if (option_mask32 & OPT_o) { // -o /* name the output module */
218 - free(m_name);
219 m_name = xstrdup(opt_o);
220 }
221
222 - if (arg1 == NULL) {
223 + if (arg1 == NULL)
224 bb_show_usage();
225 - }
226 -
227 - /* Grab the module name */
228 - tmp1 = xstrdup(arg1);
229 - tmp = basename(tmp1);
230 - len = strlen(tmp);
231 -
232 - if (uname(&myuname) == 0) {
233 - if (myuname.release[0] == '2') {
234 - k_version = myuname.release[2] - '0';
235 - }
236 - }
237 -
238 -#if ENABLE_FEATURE_2_6_MODULES
239 - if (k_version > 4 && len > 3 && tmp[len - 3] == '.'
240 - && tmp[len - 2] == 'k' && tmp[len - 1] == 'o'
241 - ) {
242 - len -= 3;
243 - tmp[len] = '\0';
244 - } else
245 -#endif
246 - if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
247 - len -= 2;
248 - tmp[len] = '\0';
249 - }
250 -
251
252 -#if ENABLE_FEATURE_2_6_MODULES
253 - if (k_version > 4)
254 - m_fullName = xasprintf("%s.ko", tmp);
255 - else
256 -#endif
257 - m_fullName = xasprintf("%s.o", tmp);
258 + ret = find_module(arg1);
259 + if (ret)
260 + goto out;
261
262 if (!m_name) {
263 - m_name = tmp;
264 - } else {
265 - free(tmp1);
266 - tmp1 = 0; /* flag for free(m_name) before exit() */
267 + tmp = xstrdup(arg1);
268 + m_name = basename(tmp);
269 }
270
271 - /* Get a filedesc for the module. Check we we have a complete path */
272 - if (stat(arg1, &st) < 0 || !S_ISREG(st.st_mode)
273 - || (fp = fopen(arg1, "r")) == NULL
274 - ) {
275 - /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
276 - * but do not error out yet if we fail to find it... */
277 - if (k_version) { /* uname succeedd */
278 - char *module_dir;
279 - char *tmdn;
280 - char real_module_dir[FILENAME_MAX];
281 -
282 - tmdn = concat_path_file(_PATH_MODULES, myuname.release);
283 - /* Jump through hoops in case /lib/modules/`uname -r`
284 - * is a symlink. We do not want recursive_action to
285 - * follow symlinks, but we do want to follow the
286 - * /lib/modules/`uname -r` dir, So resolve it ourselves
287 - * if it is a link... */
288 - if (realpath(tmdn, real_module_dir) == NULL)
289 - module_dir = tmdn;
290 - else
291 - module_dir = real_module_dir;
292 - recursive_action(module_dir, ACTION_RECURSE,
293 - check_module_name_match, 0, m_fullName, 0);
294 - free(tmdn);
295 - }
296 -
297 - /* Check if we have found anything yet */
298 - if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL)) {
299 - char module_dir[FILENAME_MAX];
300 -
301 - free(m_filename);
302 - m_filename = 0;
303 - if (realpath (_PATH_MODULES, module_dir) == NULL)
304 - strcpy(module_dir, _PATH_MODULES);
305 - /* No module found under /lib/modules/`uname -r`, this
306 - * time cast the net a bit wider. Search /lib/modules/ */
307 - if (!recursive_action(module_dir, ACTION_RECURSE,
308 - check_module_name_match, 0, m_fullName, 0)
309 - ) {
310 - if (m_filename == 0
311 - || ((fp = fopen(m_filename, "r")) == NULL)
312 - ) {
313 - bb_error_msg("%s: no module by that name found", m_fullName);
314 - goto out;
315 - }
316 - } else
317 - bb_error_msg_and_die("%s: no module by that name found", m_fullName);
318 - }
319 - } else
320 - m_filename = xstrdup(arg1);
321 -
322 - if (flag_verbose)
323 - printf("Using %s\n", m_filename);
324 -
325 -#if ENABLE_FEATURE_2_6_MODULES
326 - if (k_version > 4) {
327 - argv[optind] = m_filename;
328 - optind--;
329 - return insmod_ng_main(argc - optind, argv + optind);
330 + fp = fopen(g_filename, "r");
331 + if (!fp) {
332 + ret = errno;
333 + goto out;
334 }
335 -#endif
336
337 f = obj_load(fp, LOADBITS);
338 if (f == NULL)
339 @@ -4120,7 +4095,7 @@
340 "\t%s was compiled for kernel version %s\n"
341 "\twhile this kernel is version %s",
342 flag_force_load ? "warning: " : "",
343 - m_filename, m_strversion, uts_info.release);
344 + g_filename, m_strversion, uts_info.release);
345 if (!flag_force_load)
346 goto out;
347 }
348 @@ -4173,7 +4148,7 @@
349 hide_special_symbols(f);
350
351 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
352 - add_ksymoops_symbols(f, m_filename, m_name);
353 + add_ksymoops_symbols(f, g_filename, m_name);
354 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
355
356 new_create_module_ksymtab(f);
357 @@ -4220,30 +4195,22 @@
358 if (flag_print_load_map)
359 print_load_map(f);
360
361 - exit_status = EXIT_SUCCESS;
362 -
363 + ret = 0;
364 out:
365 #if ENABLE_FEATURE_CLEAN_UP
366 if (fp)
367 fclose(fp);
368 - free(tmp1);
369 - if (!tmp1)
370 + if (tmp)
371 + free(tmp);
372 + else if (m_name)
373 free(m_name);
374 - free(m_filename);
375 + free(g_filename);
376 #endif
377 - return exit_status;
378 + return ret;
379 }
380 -
381 -
382 #endif
383
384 -
385 #if ENABLE_FEATURE_2_6_MODULES
386 -
387 -#include <sys/mman.h>
388 -#include <asm/unistd.h>
389 -#include <sys/syscall.h>
390 -
391 /* We use error numbers in a loose translation... */
392 static const char *moderror(int err)
393 {
394 @@ -4261,19 +4228,32 @@
395 }
396 }
397
398 -int insmod_ng_main(int argc, char **argv);
399 -int insmod_ng_main(int argc, char **argv)
400 +int insmod_main_26(int argc, char **argv)
401 {
402 - long ret;
403 - size_t len;
404 + char *filename, *options;
405 + struct utsname myuname;
406 + int k_version;
407 int optlen;
408 + size_t len;
409 void *map;
410 - char *filename, *options;
411 + long ret = 0;
412 +
413 + /* check the kernel version */
414 + if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
415 + return EINVAL;
416 +
417 + k_version = myuname.release[2] - '0';
418 + if (k_version <= 4)
419 + return ENOTSUP;
420
421 filename = *++argv;
422 if (!filename)
423 bb_show_usage();
424
425 + ret = find_module(filename);
426 + if (ret || (g_filename == NULL))
427 + goto done;
428 +
429 /* Rest is options */
430 options = xzalloc(1);
431 optlen = 0;
432 @@ -4283,36 +4263,47 @@
433 optlen += sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
434 }
435
436 -#if 0
437 - /* Any special reason why mmap? It isn't performace critical... */
438 - int fd;
439 - struct stat st;
440 - unsigned long len;
441 - fd = xopen(filename, O_RDONLY);
442 - fstat(fd, &st);
443 - len = st.st_size;
444 - map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
445 - if (map == MAP_FAILED) {
446 - bb_perror_msg_and_die("cannot mmap '%s'", filename);
447 - }
448 -
449 - /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
450 - if (map == NULL) {
451 - map = xmalloc(len);
452 - xread(fd, map, len);
453 - }
454 -#else
455 len = MAXINT(ssize_t);
456 - map = xmalloc_open_read_close(filename, &len);
457 -#endif
458 -
459 + map = xmalloc_open_read_close(g_filename, &len);
460 ret = syscall(__NR_init_module, map, len, options);
461 if (ret != 0) {
462 bb_perror_msg_and_die("cannot insert '%s': %s (%li)",
463 - filename, moderror(errno), ret);
464 + g_filename, moderror(errno), ret);
465 }
466 +done:
467 + if (g_filename && (g_filename != filename))
468 + free(g_filename);
469
470 - return 0;
471 + return ret;
472 }
473
474 #endif
475 +
476 +int insmod_main(int argc, char **argv)
477 +{
478 + int ret;
479 +
480 + g_filename = NULL;
481 +#if ENABLE_FEATURE_2_6_MODULES
482 + ret = insmod_main_26(argc, argv);
483 + if (ret != ENOTSUP)
484 + goto done;
485 +#endif
486 +
487 +#if ENABLE_FEATURE_2_4_MODULES
488 + ret = insmod_main_24(argc, argv);
489 + if (ret != ENOTSUP)
490 + goto done;
491 +#endif
492 +
493 + fprintf(stderr, "Error: Kernel version not supported\n");
494 + return 1;
495 +
496 +done:
497 + if (ret) {
498 + errno = ret;
499 + bb_perror_msg("Loading module failed");
500 + return ret;
501 + } else
502 + return 0;
503 +}