add minimal uci support
[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 14:59:26.327304435 +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,33 @@
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 +4043,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 +4084,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 +4137,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 +4184,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 +4192,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 + return ret;
365 }
366 -
367 -
368 #endif
369
370 -
371 #if ENABLE_FEATURE_2_6_MODULES
372 -
373 -#include <sys/mman.h>
374 -#include <asm/unistd.h>
375 -#include <sys/syscall.h>
376 -
377 /* We use error numbers in a loose translation... */
378 static const char *moderror(int err)
379 {
380 @@ -4261,19 +4216,33 @@
381 }
382 }
383
384 -int insmod_ng_main(int argc, char **argv);
385 -int insmod_ng_main(int argc, char **argv)
386 +int insmod_main_26(int argc, char **argv)
387 {
388 - long ret;
389 - size_t len;
390 + char *filename, *options;
391 + struct utsname myuname;
392 + int k_version;
393 int optlen;
394 + size_t len;
395 void *map;
396 - char *filename, *options;
397 + long ret = 0;
398 +
399 + /* check the kernel version */
400 + if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
401 + return EINVAL;
402 +
403 + k_version = myuname.release[2] - '0';
404 + if (k_version <= 4)
405 + return ENOTSUP;
406
407 filename = *++argv;
408 if (!filename)
409 bb_show_usage();
410
411 + g_filename = filename;
412 + ret = find_module(filename);
413 + if (ret)
414 + goto done;
415 +
416 /* Rest is options */
417 options = xzalloc(1);
418 optlen = 0;
419 @@ -4283,36 +4252,46 @@
420 optlen += sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
421 }
422
423 -#if 0
424 - /* Any special reason why mmap? It isn't performace critical... */
425 - int fd;
426 - struct stat st;
427 - unsigned long len;
428 - fd = xopen(filename, O_RDONLY);
429 - fstat(fd, &st);
430 - len = st.st_size;
431 - map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
432 - if (map == MAP_FAILED) {
433 - bb_perror_msg_and_die("cannot mmap '%s'", filename);
434 - }
435 -
436 - /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
437 - if (map == NULL) {
438 - map = xmalloc(len);
439 - xread(fd, map, len);
440 - }
441 -#else
442 len = MAXINT(ssize_t);
443 - map = xmalloc_open_read_close(filename, &len);
444 -#endif
445 -
446 + map = xmalloc_open_read_close(g_filename, &len);
447 ret = syscall(__NR_init_module, map, len, options);
448 if (ret != 0) {
449 bb_perror_msg_and_die("cannot insert '%s': %s (%li)",
450 - filename, moderror(errno), ret);
451 + g_filename, moderror(errno), ret);
452 }
453 +done:
454 + if (g_filename && (g_filename != filename))
455 + free(g_filename);
456
457 - return 0;
458 + return ret;
459 }
460
461 #endif
462 +
463 +int insmod_main(int argc, char **argv)
464 +{
465 + int ret;
466 +
467 +#if ENABLE_FEATURE_2_6_MODULES
468 + ret = insmod_main_26(argc, argv);
469 + if (ret != ENOTSUP)
470 + goto done;
471 +#endif
472 +
473 +#if ENABLE_FEATURE_2_4_MODULES
474 + ret = insmod_main_24(argc, argv);
475 + if (ret != ENOTSUP)
476 + goto done;
477 +#endif
478 +
479 + fprintf(stderr, "Error: Kernel version not supported\n");
480 + return 1;
481 +
482 +done:
483 + if (ret) {
484 + errno = ret;
485 + bb_perror_msg("Loading module failed");
486 + return ret;
487 + } else
488 + return 0;
489 +}