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
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>
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
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);
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);
30 +int insmod_main(int argc, char **argv);
32 +static char *g_filename = NULL;
33 +#define _PATH_MODULES "/lib/modules"
35 +static int check_module_name_match(const char *filename, struct stat *statbuf,
36 + void *userdata, int depth)
38 + char *fullname = (char *) userdata;
40 + if (fullname[0] == '\0')
43 + char *tmp, *tmp1 = xstrdup(filename);
44 + tmp = bb_get_last_path_component(tmp1);
45 + if (strcmp(tmp, fullname) == 0) {
47 + /* Stop searching if we find a match */
48 + g_filename = xstrdup(filename);
56 +static int find_module(char *filename)
58 + char *module_dir, real_module_dir[FILENAME_MAX];
59 + int len, slen, ret = ENOENT, k_version;
60 + struct utsname myuname;
64 + /* check the kernel version */
65 + if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
68 + k_version = myuname.release[2] - '0';
69 +#if ENABLE_FEATURE_2_4_MODULES
76 + len = strlen(filename);
77 + slen = strlen(suffix);
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);
83 + filename = strdup(filename);
84 + if ((stat(filename, &st) == 0) && S_ISREG(st.st_mode))
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) {
97 + module_dir = real_module_dir;
100 + recursive_action(module_dir, ACTION_RECURSE,
101 + check_module_name_match, 0, filename, 0);
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))
116 #if ENABLE_FEATURE_2_4_MODULES
121 -#define _PATH_MODULES "/lib/modules"
122 enum { STRVERSIONLEN = 64 };
124 /*======================================================================*/
126 static int n_ext_modules_used;
127 extern int delete_module(const char *);
129 -static char *m_filename;
130 -static char *m_fullName;
133 -/*======================================================================*/
136 -static int check_module_name_match(const char *filename, struct stat *statbuf,
137 - void *userdata, int depth)
139 - char *fullname = (char *) userdata;
141 - if (fullname[0] == '\0')
144 - char *tmp, *tmp1 = xstrdup(filename);
145 - tmp = bb_get_last_path_component(tmp1);
146 - if (strcmp(tmp, fullname) == 0) {
148 - /* Stop searching if we find a match */
149 - m_filename = xstrdup(filename);
158 -/*======================================================================*/
160 static struct obj_file *arch_new_file(void)
163 @@ -3952,33 +4008,35 @@
164 void print_load_map(struct obj_file *f);
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)
175 unsigned long m_size;
180 - int exit_status = EXIT_FAILURE;
183 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
184 struct utsname uts_info;
185 char m_strversion[STRVERSIONLEN];
186 int m_version, m_crcs;
188 -#if ENABLE_FEATURE_CLEAN_UP
196 struct utsname myuname;
198 + /* check the kernel version */
199 + if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
202 + k_version = myuname.release[2] - '0';
206 /* Parse any options */
207 getopt32(argv, OPTION_STR, &opt_o);
209 @@ -3987,110 +4045,18 @@
210 m_name = xstrdup(opt_o);
213 - if (arg1 == NULL) {
218 - /* Grab the module name */
219 - tmp1 = xstrdup(arg1);
220 - tmp = basename(tmp1);
223 - if (uname(&myuname) == 0) {
224 - if (myuname.release[0] == '2') {
225 - k_version = myuname.release[2] - '0';
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'
237 - if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
243 -#if ENABLE_FEATURE_2_6_MODULES
245 - m_fullName = xasprintf("%s.ko", tmp);
248 - m_fullName = xasprintf("%s.o", tmp);
254 - tmp1 = 0; /* flag for free(m_name) before exit() */
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
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 */
266 - char real_module_dir[FILENAME_MAX];
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)
277 - module_dir = real_module_dir;
278 - recursive_action(module_dir, ACTION_RECURSE,
279 - check_module_name_match, 0, m_fullName, 0);
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];
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)
296 - if (m_filename == 0
297 - || ((fp = fopen(m_filename, "r")) == NULL)
299 - bb_error_msg("%s: no module by that name found", m_fullName);
303 - bb_error_msg_and_die("%s: no module by that name found", m_fullName);
306 - m_filename = xstrdup(arg1);
309 - printf("Using %s\n", m_filename);
310 + ret = find_module(arg1);
314 -#if ENABLE_FEATURE_2_6_MODULES
315 - if (k_version > 4) {
316 - argv[optind] = m_filename;
318 - return insmod_ng_main(argc - optind, argv + optind);
319 + fp = fopen(g_filename, "r");
326 f = obj_load(fp, LOADBITS);
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)
337 @@ -4173,7 +4139,7 @@
338 hide_special_symbols(f);
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 */
345 new_create_module_ksymtab(f);
346 @@ -4220,8 +4186,7 @@
347 if (flag_print_load_map)
350 - exit_status = EXIT_SUCCESS;
354 #if ENABLE_FEATURE_CLEAN_UP
356 @@ -4229,21 +4194,13 @@
370 #if ENABLE_FEATURE_2_6_MODULES
372 -#include <sys/mman.h>
373 -#include <asm/unistd.h>
374 -#include <sys/syscall.h>
376 /* We use error numbers in a loose translation... */
377 static const char *moderror(int err)
379 @@ -4261,19 +4218,33 @@
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)
389 + char *filename, *options;
390 + struct utsname myuname;
395 - char *filename, *options;
398 + /* check the kernel version */
399 + if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
402 + k_version = myuname.release[2] - '0';
403 + if (k_version <= 4)
410 + g_filename = filename;
411 + ret = find_module(filename);
415 /* Rest is options */
416 options = xzalloc(1);
418 @@ -4283,36 +4254,46 @@
419 optlen += sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
423 - /* Any special reason why mmap? It isn't performace critical... */
427 - fd = xopen(filename, O_RDONLY);
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);
435 - /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
437 - map = xmalloc(len);
438 - xread(fd, map, len);
441 len = MAXINT(ssize_t);
442 - map = xmalloc_open_read_close(filename, &len);
445 + map = xmalloc_open_read_close(g_filename, &len);
446 ret = syscall(__NR_init_module, map, len, options);
448 bb_perror_msg_and_die("cannot insert '%s': %s (%li)",
449 - filename, moderror(errno), ret);
450 + g_filename, moderror(errno), ret);
453 + if (g_filename && (g_filename != filename))
462 +int insmod_main(int argc, char **argv)
466 +#if ENABLE_FEATURE_2_6_MODULES
467 + ret = insmod_main_26(argc, argv);
468 + if (ret != ENOTSUP)
472 +#if ENABLE_FEATURE_2_4_MODULES
473 + ret = insmod_main_24(argc, argv);
474 + if (ret != ENOTSUP)
478 + fprintf(stderr, "Error: Kernel version not supported\n");
484 + bb_perror_msg("Loading module failed");