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
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)
107 + if ((stat(g_filename, &st) == 0) && S_ISREG(st.st_mode))
119 #if ENABLE_FEATURE_2_4_MODULES
124 -#define _PATH_MODULES "/lib/modules"
125 enum { STRVERSIONLEN = 64 };
127 /*======================================================================*/
129 static int n_ext_modules_used;
130 extern int delete_module(const char *);
132 -static char *m_filename;
133 -static char *m_fullName;
136 -/*======================================================================*/
139 -static int check_module_name_match(const char *filename, struct stat *statbuf,
140 - void *userdata, int depth)
142 - char *fullname = (char *) userdata;
144 - if (fullname[0] == '\0')
147 - char *tmp, *tmp1 = xstrdup(filename);
148 - tmp = bb_get_last_path_component(tmp1);
149 - if (strcmp(tmp, fullname) == 0) {
151 - /* Stop searching if we find a match */
152 - m_filename = xstrdup(filename);
161 -/*======================================================================*/
163 static struct obj_file *arch_new_file(void)
166 @@ -3952,145 +4011,57 @@
167 void print_load_map(struct obj_file *f);
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)
178 unsigned long m_size;
183 - int exit_status = EXIT_FAILURE;
184 + char *tmp = NULL, *m_name = NULL;
187 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
188 struct utsname uts_info;
189 char m_strversion[STRVERSIONLEN];
190 int m_version, m_crcs;
192 -#if ENABLE_FEATURE_CLEAN_UP
200 struct utsname myuname;
202 + /* check the kernel version */
203 + if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
206 + k_version = myuname.release[2] - '0';
210 /* Parse any options */
211 getopt32(argv, OPTION_STR, &opt_o);
213 if (option_mask32 & OPT_o) { // -o /* name the output module */
215 m_name = xstrdup(opt_o);
218 - if (arg1 == NULL) {
223 - /* Grab the module name */
224 - tmp1 = xstrdup(arg1);
225 - tmp = basename(tmp1);
228 - if (uname(&myuname) == 0) {
229 - if (myuname.release[0] == '2') {
230 - k_version = myuname.release[2] - '0';
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'
242 - if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
248 -#if ENABLE_FEATURE_2_6_MODULES
250 - m_fullName = xasprintf("%s.ko", tmp);
253 - m_fullName = xasprintf("%s.o", tmp);
254 + ret = find_module(arg1);
262 - tmp1 = 0; /* flag for free(m_name) before exit() */
263 + tmp = xstrdup(arg1);
264 + m_name = basename(tmp);
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
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 */
276 - char real_module_dir[FILENAME_MAX];
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)
287 - module_dir = real_module_dir;
288 - recursive_action(module_dir, ACTION_RECURSE,
289 - check_module_name_match, 0, m_fullName, 0);
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];
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)
306 - if (m_filename == 0
307 - || ((fp = fopen(m_filename, "r")) == NULL)
309 - bb_error_msg("%s: no module by that name found", m_fullName);
313 - bb_error_msg_and_die("%s: no module by that name found", m_fullName);
316 - m_filename = xstrdup(arg1);
319 - printf("Using %s\n", m_filename);
321 -#if ENABLE_FEATURE_2_6_MODULES
322 - if (k_version > 4) {
323 - argv[optind] = m_filename;
325 - return insmod_ng_main(argc - optind, argv + optind);
326 + fp = fopen(g_filename, "r");
333 f = obj_load(fp, LOADBITS);
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)
344 @@ -4173,7 +4144,7 @@
345 hide_special_symbols(f);
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 */
352 new_create_module_ksymtab(f);
353 @@ -4220,30 +4191,22 @@
354 if (flag_print_load_map)
357 - exit_status = EXIT_SUCCESS;
361 #if ENABLE_FEATURE_CLEAN_UP
373 - return exit_status;
381 #if ENABLE_FEATURE_2_6_MODULES
383 -#include <sys/mman.h>
384 -#include <asm/unistd.h>
385 -#include <sys/syscall.h>
387 /* We use error numbers in a loose translation... */
388 static const char *moderror(int err)
390 @@ -4261,19 +4224,32 @@
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)
400 + char *filename, *options;
401 + struct utsname myuname;
406 - char *filename, *options;
409 + /* check the kernel version */
410 + if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
413 + k_version = myuname.release[2] - '0';
414 + if (k_version <= 4)
421 + ret = find_module(filename);
422 + if (ret || (g_filename == NULL))
425 /* Rest is options */
426 options = xzalloc(1);
428 @@ -4283,36 +4259,47 @@
429 optlen += sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
433 - /* Any special reason why mmap? It isn't performace critical... */
437 - fd = xopen(filename, O_RDONLY);
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);
445 - /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
447 - map = xmalloc(len);
448 - xread(fd, map, len);
451 len = MAXINT(ssize_t);
452 - map = xmalloc_open_read_close(filename, &len);
455 + map = xmalloc_open_read_close(g_filename, &len);
456 ret = syscall(__NR_init_module, map, len, options);
458 bb_perror_msg_and_die("cannot insert '%s': %s (%li)",
459 - filename, moderror(errno), ret);
460 + g_filename, moderror(errno), ret);
463 + if (g_filename && (g_filename != filename))
472 +int insmod_main(int argc, char **argv)
477 +#if ENABLE_FEATURE_2_6_MODULES
478 + ret = insmod_main_26(argc, argv);
479 + if (ret != ENOTSUP)
483 +#if ENABLE_FEATURE_2_4_MODULES
484 + ret = insmod_main_24(argc, argv);
485 + if (ret != ENOTSUP)
489 + fprintf(stderr, "Error: Kernel version not supported\n");
495 + bb_perror_msg("Loading module failed");