2b4bff65bc550b50417c8ec8868c46db0c7923dc
[openwrt/svn-archive/archive.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-05 14:39:19.922555340 +0200
4 +++ busybox-1.7.2/modutils/insmod.c 2007-10-05 21:08:55.442650322 +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,145 +4008,57 @@
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 + char *tmp = NULL, *m_name = NULL;
182 + int ret = EINVAL;
183 int m_has_modinfo;
184 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
185 struct utsname uts_info;
186 char m_strversion[STRVERSIONLEN];
187 int m_version, m_crcs;
188 #endif
189 -#if ENABLE_FEATURE_CLEAN_UP
190 - FILE *fp = 0;
191 -#else
192 - FILE *fp;
193 -#endif
194 - int k_version = 0;
195 + FILE *fp = NULL;
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 = 0; /* 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 we 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 - char real_module_dir[FILENAME_MAX];
274 -
275 - tmdn = concat_path_file(_PATH_MODULES, myuname.release);
276 - /* Jump through hoops in case /lib/modules/`uname -r`
277 - * is a symlink. We do not want recursive_action to
278 - * follow symlinks, but we do want to follow the
279 - * /lib/modules/`uname -r` dir, So resolve it ourselves
280 - * if it is a link... */
281 - if (realpath(tmdn, real_module_dir) == NULL)
282 - module_dir = tmdn;
283 - else
284 - module_dir = real_module_dir;
285 - recursive_action(module_dir, ACTION_RECURSE,
286 - check_module_name_match, 0, m_fullName, 0);
287 - free(tmdn);
288 - }
289 -
290 - /* Check if we have found anything yet */
291 - if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL)) {
292 - char module_dir[FILENAME_MAX];
293 -
294 - free(m_filename);
295 - m_filename = 0;
296 - if (realpath (_PATH_MODULES, module_dir) == NULL)
297 - strcpy(module_dir, _PATH_MODULES);
298 - /* No module found under /lib/modules/`uname -r`, this
299 - * time cast the net a bit wider. Search /lib/modules/ */
300 - if (!recursive_action(module_dir, ACTION_RECURSE,
301 - check_module_name_match, 0, m_fullName, 0)
302 - ) {
303 - if (m_filename == 0
304 - || ((fp = fopen(m_filename, "r")) == NULL)
305 - ) {
306 - bb_error_msg("%s: no module by that name found", m_fullName);
307 - goto out;
308 - }
309 - } else
310 - bb_error_msg_and_die("%s: no module by that name found", m_fullName);
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 if (f == NULL)
332 @@ -4120,7 +4088,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 @@ -4173,7 +4141,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 @@ -4220,30 +4188,22 @@
351 if (flag_print_load_map)
352 print_load_map(f);
353
354 - exit_status = EXIT_SUCCESS;
355 -
356 + ret = 0;
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 -
375 #endif
376
377 -
378 #if ENABLE_FEATURE_2_6_MODULES
379 -
380 -#include <sys/mman.h>
381 -#include <asm/unistd.h>
382 -#include <sys/syscall.h>
383 -
384 /* We use error numbers in a loose translation... */
385 static const char *moderror(int err)
386 {
387 @@ -4261,19 +4221,33 @@
388 }
389 }
390
391 -int insmod_ng_main(int argc, char **argv);
392 -int insmod_ng_main(int argc, char **argv)
393 +int insmod_main_26(int argc, char **argv)
394 {
395 - long ret;
396 - size_t len;
397 + char *filename, *options;
398 + struct utsname myuname;
399 + int k_version;
400 int optlen;
401 + size_t len;
402 void *map;
403 - char *filename, *options;
404 + long ret = 0;
405 +
406 + /* check the kernel version */
407 + if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
408 + return EINVAL;
409 +
410 + k_version = myuname.release[2] - '0';
411 + if (k_version <= 4)
412 + return ENOTSUP;
413
414 filename = *++argv;
415 if (!filename)
416 bb_show_usage();
417
418 + g_filename = filename;
419 + ret = find_module(filename);
420 + if (ret)
421 + goto done;
422 +
423 /* Rest is options */
424 options = xzalloc(1);
425 optlen = 0;
426 @@ -4283,36 +4257,46 @@
427 optlen += sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
428 }
429
430 -#if 0
431 - /* Any special reason why mmap? It isn't performace critical... */
432 - int fd;
433 - struct stat st;
434 - unsigned long len;
435 - fd = xopen(filename, O_RDONLY);
436 - fstat(fd, &st);
437 - len = st.st_size;
438 - map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
439 - if (map == MAP_FAILED) {
440 - bb_perror_msg_and_die("cannot mmap '%s'", filename);
441 - }
442 -
443 - /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
444 - if (map == NULL) {
445 - map = xmalloc(len);
446 - xread(fd, map, len);
447 - }
448 -#else
449 len = MAXINT(ssize_t);
450 - map = xmalloc_open_read_close(filename, &len);
451 -#endif
452 -
453 + map = xmalloc_open_read_close(g_filename, &len);
454 ret = syscall(__NR_init_module, map, len, options);
455 if (ret != 0) {
456 bb_perror_msg_and_die("cannot insert '%s': %s (%li)",
457 - filename, moderror(errno), ret);
458 + g_filename, moderror(errno), ret);
459 }
460 +done:
461 + if (g_filename && (g_filename != filename))
462 + free(g_filename);
463
464 - return 0;
465 + return ret;
466 }
467
468 #endif
469 +
470 +int insmod_main(int argc, char **argv)
471 +{
472 + int ret;
473 +
474 +#if ENABLE_FEATURE_2_6_MODULES
475 + ret = insmod_main_26(argc, argv);
476 + if (ret != ENOTSUP)
477 + goto done;
478 +#endif
479 +
480 +#if ENABLE_FEATURE_2_4_MODULES
481 + ret = insmod_main_24(argc, argv);
482 + if (ret != ENOTSUP)
483 + goto done;
484 +#endif
485 +
486 + fprintf(stderr, "Error: Kernel version not supported\n");
487 + return 1;
488 +
489 +done:
490 + if (ret) {
491 + errno = ret;
492 + bb_perror_msg("Loading module failed");
493 + return ret;
494 + } else
495 + return 0;
496 +}