1 Index: busybox-1.7.2/modutils/insmod.c
2 ===================================================================
3 --- busybox-1.7.2.orig/modutils/insmod.c 2007-10-24 18:20:56.857757117 +0200
4 +++ busybox-1.7.2/modutils/insmod.c 2007-10-24 18:38:27.701641299 +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)) {
85 + g_filename = filename;
92 + /* next: scan /lib/modules/<release> */
93 + /* Jump through hoops in case /lib/modules/`uname -r`
94 + * is a symlink. We do not want recursive_action to
95 + * follow symlinks, but we do want to follow the
96 + * /lib/modules/`uname -r` dir, So resolve it ourselves
97 + * if it is a link... */
98 + module_dir = concat_path_file(_PATH_MODULES, myuname.release);
99 + if (realpath(module_dir, real_module_dir) != NULL) {
101 + module_dir = real_module_dir;
104 + recursive_action(module_dir, ACTION_RECURSE,
105 + check_module_name_match, 0, filename, 0);
107 + /* Check if we have a complete path */
108 + if (g_filename == NULL)
111 + if ((stat(g_filename, &st) == 0) && S_ISREG(st.st_mode))
123 #if ENABLE_FEATURE_2_4_MODULES
128 -#define _PATH_MODULES "/lib/modules"
129 enum { STRVERSIONLEN = 64 };
131 /*======================================================================*/
133 static int n_ext_modules_used;
134 extern int delete_module(const char *);
136 -static char *m_filename;
137 -static char *m_fullName;
140 -/*======================================================================*/
143 -static int check_module_name_match(const char *filename, struct stat *statbuf,
144 - void *userdata, int depth)
146 - char *fullname = (char *) userdata;
148 - if (fullname[0] == '\0')
151 - char *tmp, *tmp1 = xstrdup(filename);
152 - tmp = bb_get_last_path_component(tmp1);
153 - if (strcmp(tmp, fullname) == 0) {
155 - /* Stop searching if we find a match */
156 - m_filename = xstrdup(filename);
165 -/*======================================================================*/
167 static struct obj_file *arch_new_file(void)
170 @@ -3952,145 +4015,57 @@
171 void print_load_map(struct obj_file *f);
174 -int insmod_main( int argc, char **argv);
175 -int insmod_main( int argc, char **argv)
176 +int insmod_main_24( int argc, char **argv)
182 unsigned long m_size;
187 - int exit_status = EXIT_FAILURE;
188 + char *tmp = NULL, *m_name = NULL;
191 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
192 struct utsname uts_info;
193 char m_strversion[STRVERSIONLEN];
194 int m_version, m_crcs;
196 -#if ENABLE_FEATURE_CLEAN_UP
204 struct utsname myuname;
206 + /* check the kernel version */
207 + if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
210 + k_version = myuname.release[2] - '0';
214 /* Parse any options */
215 getopt32(argv, OPTION_STR, &opt_o);
217 if (option_mask32 & OPT_o) { // -o /* name the output module */
219 m_name = xstrdup(opt_o);
222 - if (arg1 == NULL) {
227 - /* Grab the module name */
228 - tmp1 = xstrdup(arg1);
229 - tmp = basename(tmp1);
232 - if (uname(&myuname) == 0) {
233 - if (myuname.release[0] == '2') {
234 - k_version = myuname.release[2] - '0';
238 -#if ENABLE_FEATURE_2_6_MODULES
239 - if (k_version > 4 && len > 3 && tmp[len - 3] == '.'
240 - && tmp[len - 2] == 'k' && tmp[len - 1] == 'o'
246 - if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
252 -#if ENABLE_FEATURE_2_6_MODULES
254 - m_fullName = xasprintf("%s.ko", tmp);
257 - m_fullName = xasprintf("%s.o", tmp);
258 + ret = find_module(arg1);
266 - tmp1 = 0; /* flag for free(m_name) before exit() */
267 + tmp = xstrdup(arg1);
268 + m_name = basename(tmp);
271 - /* Get a filedesc for the module. Check we we have a complete path */
272 - if (stat(arg1, &st) < 0 || !S_ISREG(st.st_mode)
273 - || (fp = fopen(arg1, "r")) == NULL
275 - /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
276 - * but do not error out yet if we fail to find it... */
277 - if (k_version) { /* uname succeedd */
280 - char real_module_dir[FILENAME_MAX];
282 - tmdn = concat_path_file(_PATH_MODULES, myuname.release);
283 - /* Jump through hoops in case /lib/modules/`uname -r`
284 - * is a symlink. We do not want recursive_action to
285 - * follow symlinks, but we do want to follow the
286 - * /lib/modules/`uname -r` dir, So resolve it ourselves
287 - * if it is a link... */
288 - if (realpath(tmdn, real_module_dir) == NULL)
291 - module_dir = real_module_dir;
292 - recursive_action(module_dir, ACTION_RECURSE,
293 - check_module_name_match, 0, m_fullName, 0);
297 - /* Check if we have found anything yet */
298 - if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL)) {
299 - char module_dir[FILENAME_MAX];
303 - if (realpath (_PATH_MODULES, module_dir) == NULL)
304 - strcpy(module_dir, _PATH_MODULES);
305 - /* No module found under /lib/modules/`uname -r`, this
306 - * time cast the net a bit wider. Search /lib/modules/ */
307 - if (!recursive_action(module_dir, ACTION_RECURSE,
308 - check_module_name_match, 0, m_fullName, 0)
310 - if (m_filename == 0
311 - || ((fp = fopen(m_filename, "r")) == NULL)
313 - bb_error_msg("%s: no module by that name found", m_fullName);
317 - bb_error_msg_and_die("%s: no module by that name found", m_fullName);
320 - m_filename = xstrdup(arg1);
323 - printf("Using %s\n", m_filename);
325 -#if ENABLE_FEATURE_2_6_MODULES
326 - if (k_version > 4) {
327 - argv[optind] = m_filename;
329 - return insmod_ng_main(argc - optind, argv + optind);
330 + fp = fopen(g_filename, "r");
337 f = obj_load(fp, LOADBITS);
339 @@ -4120,7 +4095,7 @@
340 "\t%s was compiled for kernel version %s\n"
341 "\twhile this kernel is version %s",
342 flag_force_load ? "warning: " : "",
343 - m_filename, m_strversion, uts_info.release);
344 + g_filename, m_strversion, uts_info.release);
345 if (!flag_force_load)
348 @@ -4173,7 +4148,7 @@
349 hide_special_symbols(f);
351 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
352 - add_ksymoops_symbols(f, m_filename, m_name);
353 + add_ksymoops_symbols(f, g_filename, m_name);
354 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
356 new_create_module_ksymtab(f);
357 @@ -4220,30 +4195,22 @@
358 if (flag_print_load_map)
361 - exit_status = EXIT_SUCCESS;
365 #if ENABLE_FEATURE_CLEAN_UP
377 - return exit_status;
385 #if ENABLE_FEATURE_2_6_MODULES
387 -#include <sys/mman.h>
388 -#include <asm/unistd.h>
389 -#include <sys/syscall.h>
391 /* We use error numbers in a loose translation... */
392 static const char *moderror(int err)
394 @@ -4261,19 +4228,32 @@
398 -int insmod_ng_main(int argc, char **argv);
399 -int insmod_ng_main(int argc, char **argv)
400 +int insmod_main_26(int argc, char **argv)
404 + char *filename, *options;
405 + struct utsname myuname;
410 - char *filename, *options;
413 + /* check the kernel version */
414 + if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
417 + k_version = myuname.release[2] - '0';
418 + if (k_version <= 4)
425 + ret = find_module(filename);
426 + if (ret || (g_filename == NULL))
429 /* Rest is options */
430 options = xzalloc(1);
432 @@ -4283,36 +4263,47 @@
433 optlen += sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
437 - /* Any special reason why mmap? It isn't performace critical... */
441 - fd = xopen(filename, O_RDONLY);
444 - map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
445 - if (map == MAP_FAILED) {
446 - bb_perror_msg_and_die("cannot mmap '%s'", filename);
449 - /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
451 - map = xmalloc(len);
452 - xread(fd, map, len);
455 len = MAXINT(ssize_t);
456 - map = xmalloc_open_read_close(filename, &len);
459 + map = xmalloc_open_read_close(g_filename, &len);
460 ret = syscall(__NR_init_module, map, len, options);
462 bb_perror_msg_and_die("cannot insert '%s': %s (%li)",
463 - filename, moderror(errno), ret);
464 + g_filename, moderror(errno), ret);
467 + if (g_filename && (g_filename != filename))
476 +int insmod_main(int argc, char **argv)
481 +#if ENABLE_FEATURE_2_6_MODULES
482 + ret = insmod_main_26(argc, argv);
483 + if (ret != ENOTSUP)
487 +#if ENABLE_FEATURE_2_4_MODULES
488 + ret = insmod_main_24(argc, argv);
489 + if (ret != ENOTSUP)
493 + fprintf(stderr, "Error: Kernel version not supported\n");
499 + bb_perror_msg("Loading module failed");