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