1 Index: busybox-1.8.1/modutils/insmod.c
2 ===================================================================
3 --- busybox-1.8.1.orig/modutils/insmod.c 2007-11-10 02:40:49.000000000 +0100
4 +++ busybox-1.8.1/modutils/insmod.c 2007-11-10 17:28:44.391223047 +0100
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
21 - * Big piece of 2.4-specific code
23 #if ENABLE_FEATURE_2_4_MODULES
25 +int insmod_main_24(int argc, char **argv);
27 #if ENABLE_FEATURE_2_6_MODULES
28 -static int insmod_ng_main(int argc, char **argv);
29 +int insmod_main_26(int argc, char **argv);
31 +int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
33 +static char *g_filename = NULL;
34 +#define _PATH_MODULES "/lib/modules"
36 +static int check_module_name_match(const char *filename, struct stat *statbuf,
37 + void *userdata, int depth)
39 + char *fullname = (char *) userdata;
42 + if (fullname[0] == '\0')
45 + tmp = bb_get_last_path_component_nostrip(filename);
46 + if (strcmp(tmp, fullname) == 0) {
47 + /* Stop searching if we find a match */
48 + g_filename = xstrdup(filename);
55 +static int find_module(char *filename)
57 + char *module_dir, real_module_dir[FILENAME_MAX];
58 + int len, slen, ret = ENOENT, k_version;
59 + struct utsname myuname;
63 + /* check the kernel version */
64 + if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
67 + k_version = myuname.release[2] - '0';
68 +#if ENABLE_FEATURE_2_4_MODULES
75 + len = strlen(filename);
76 + slen = strlen(suffix);
78 + /* check for suffix and absolute path first */
79 + if ((len < slen + 2) || (strcmp(filename + len - slen, suffix) != 0)) {
80 + filename = xasprintf("%s%s", filename, suffix);
82 + filename = strdup(filename);
83 + if ((stat(filename, &st) == 0) && S_ISREG(st.st_mode)) {
84 + g_filename = filename;
91 + /* next: scan /lib/modules/<release> */
92 + /* Jump through hoops in case /lib/modules/`uname -r`
93 + * is a symlink. We do not want recursive_action to
94 + * follow symlinks, but we do want to follow the
95 + * /lib/modules/`uname -r` dir, So resolve it ourselves
96 + * if it is a link... */
97 + module_dir = concat_path_file(_PATH_MODULES, myuname.release);
98 + if (realpath(module_dir, real_module_dir) != NULL) {
100 + module_dir = real_module_dir;
103 + recursive_action(module_dir, ACTION_RECURSE,
104 + check_module_name_match, 0, filename, 0);
106 + /* Check if we have a complete path */
107 + if (g_filename == NULL)
110 + if ((stat(g_filename, &st) == 0) && S_ISREG(st.st_mode))
122 + * Big piece of 2.4-specific code
124 +#if ENABLE_FEATURE_2_4_MODULES
125 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
132 -#define _PATH_MODULES "/lib/modules"
133 enum { STRVERSIONLEN = 64 };
135 /*======================================================================*/
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;
149 - if (fullname[0] == '\0')
152 - tmp = bb_get_last_path_component_nostrip(filename);
153 - if (strcmp(tmp, fullname) == 0) {
154 - /* Stop searching if we find a match */
155 - m_filename = xstrdup(filename);
162 /*======================================================================*/
164 @@ -3897,145 +3971,57 @@
165 void print_load_map(struct obj_file *f);
168 -int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
169 -int insmod_main(int argc, char **argv)
170 +int insmod_main_24( int argc, char **argv)
176 unsigned long m_size;
180 - char *m_name = NULL;
181 - int exit_status = EXIT_FAILURE;
182 + char *tmp = NULL, *m_name = NULL;
185 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
186 struct utsname uts_info;
187 char m_strversion[STRVERSIONLEN];
188 int m_version, m_crcs;
190 -#if ENABLE_FEATURE_CLEAN_UP
197 struct utsname myuname;
199 + /* check the kernel version */
200 + if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
203 + k_version = myuname.release[2] - '0';
207 /* Parse any options */
208 getopt32(argv, OPTION_STR, &opt_o);
210 if (option_mask32 & OPT_o) { // -o /* name the output module */
212 m_name = xstrdup(opt_o);
215 - if (arg1 == NULL) {
220 - /* Grab the module name */
221 - tmp1 = xstrdup(arg1);
222 - tmp = basename(tmp1);
225 - if (uname(&myuname) == 0) {
226 - if (myuname.release[0] == '2') {
227 - k_version = myuname.release[2] - '0';
231 -#if ENABLE_FEATURE_2_6_MODULES
232 - if (k_version > 4 && len > 3 && tmp[len - 3] == '.'
233 - && tmp[len - 2] == 'k' && tmp[len - 1] == 'o'
239 - if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
245 -#if ENABLE_FEATURE_2_6_MODULES
247 - m_fullName = xasprintf("%s.ko", tmp);
250 - m_fullName = xasprintf("%s.o", tmp);
251 + ret = find_module(arg1);
259 - tmp1 = NULL; /* flag for free(m_name) before exit() */
260 + tmp = xstrdup(arg1);
261 + m_name = basename(tmp);
264 - /* Get a filedesc for the module. Check that we have a complete path */
265 - if (stat(arg1, &st) < 0 || !S_ISREG(st.st_mode)
266 - || (fp = fopen(arg1, "r")) == NULL
268 - /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
269 - * but do not error out yet if we fail to find it... */
270 - if (k_version) { /* uname succeedd */
274 - tmdn = concat_path_file(_PATH_MODULES, myuname.release);
275 - /* Jump through hoops in case /lib/modules/`uname -r`
276 - * is a symlink. We do not want recursive_action to
277 - * follow symlinks, but we do want to follow the
278 - * /lib/modules/`uname -r` dir, So resolve it ourselves
279 - * if it is a link... */
280 - module_dir = xmalloc_readlink(tmdn);
282 - module_dir = xstrdup(tmdn);
283 - recursive_action(module_dir, ACTION_RECURSE,
284 - check_module_name_match, NULL, m_fullName, 0);
289 - /* Check if we have found anything yet */
290 - if (!m_filename || ((fp = fopen(m_filename, "r")) == NULL)) {
296 - module_dir = xmalloc_readlink(_PATH_MODULES);
298 - module_dir = xstrdup(_PATH_MODULES);
299 - /* No module found under /lib/modules/`uname -r`, this
300 - * time cast the net a bit wider. Search /lib/modules/ */
301 - r = recursive_action(module_dir, ACTION_RECURSE,
302 - check_module_name_match, NULL, m_fullName, 0);
304 - bb_error_msg_and_die("%s: module not found", m_fullName);
306 - if (m_filename == NULL
307 - || ((fp = fopen(m_filename, "r")) == NULL)
309 - bb_error_msg_and_die("%s: module not found", m_fullName);
313 - m_filename = xstrdup(arg1);
316 - printf("Using %s\n", m_filename);
318 -#if ENABLE_FEATURE_2_6_MODULES
319 - if (k_version > 4) {
320 - argv[optind] = m_filename;
322 - return insmod_ng_main(argc - optind, argv + optind);
323 + fp = fopen(g_filename, "r");
330 f = obj_load(fp, LOADBITS);
332 @@ -4062,7 +4048,7 @@
333 "\t%s was compiled for kernel version %s\n"
334 "\twhile this kernel is version %s",
335 flag_force_load ? "warning: " : "",
336 - m_filename, m_strversion, uts_info.release);
337 + g_filename, m_strversion, uts_info.release);
338 if (!flag_force_load)
341 @@ -4104,7 +4090,7 @@
342 hide_special_symbols(f);
344 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
345 - add_ksymoops_symbols(f, m_filename, m_name);
346 + add_ksymoops_symbols(f, g_filename, m_name);
347 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
349 new_create_module_ksymtab(f);
350 @@ -4147,18 +4133,19 @@
351 if (flag_print_load_map)
354 - exit_status = EXIT_SUCCESS;
355 + ret = EXIT_SUCCESS;
358 #if ENABLE_FEATURE_CLEAN_UP
370 - return exit_status;
374 #endif /* ENABLE_FEATURE_2_4_MODULES */
375 @@ -4190,23 +4177,32 @@
379 -#if !ENABLE_FEATURE_2_4_MODULES
380 -int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
381 -int insmod_main(int argc, char **argv)
383 -static 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 + ret = find_module(filename);
411 + if (ret || (g_filename == NULL))
414 /* Rest is options */
415 options = xzalloc(1);
417 @@ -4216,36 +4212,47 @@
418 optlen += sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
422 - /* Any special reason why mmap? It isn't performace critical... */
426 - fd = xopen(filename, O_RDONLY);
429 - map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
430 - if (map == MAP_FAILED) {
431 - bb_perror_msg_and_die("cannot mmap '%s'", filename);
434 - /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
436 - map = xmalloc(len);
437 - xread(fd, map, len);
440 len = MAXINT(ssize_t);
441 - map = xmalloc_open_read_close(filename, &len);
444 + map = xmalloc_open_read_close(g_filename, &len);
445 ret = syscall(__NR_init_module, map, len, options);
447 bb_perror_msg_and_die("cannot insert '%s': %s (%li)",
448 - filename, moderror(errno), ret);
449 + g_filename, moderror(errno), ret);
452 + if (g_filename && (g_filename != filename))
461 +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");