1 Index: busybox-1.7.2/modutils/insmod.c
2 ===================================================================
3 --- busybox-1.7.2.orig/modutils/insmod.c 2007-10-05 14:39:19.922555340 +0200
4 +++ busybox-1.7.2/modutils/insmod.c 2007-10-05 21:08:55.442650322 +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,145 +4008,57 @@
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;
181 + char *tmp = NULL, *m_name = NULL;
184 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
185 struct utsname uts_info;
186 char m_strversion[STRVERSIONLEN];
187 int m_version, m_crcs;
189 -#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 = 0; /* flag for free(m_name) before exit() */
260 + tmp = xstrdup(arg1);
261 + m_name = basename(tmp);
264 - /* Get a filedesc for the module. Check we 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 */
273 - char real_module_dir[FILENAME_MAX];
275 - tmdn = concat_path_file(_PATH_MODULES, myuname.release);
276 - /* Jump through hoops in case /lib/modules/`uname -r`
277 - * is a symlink. We do not want recursive_action to
278 - * follow symlinks, but we do want to follow the
279 - * /lib/modules/`uname -r` dir, So resolve it ourselves
280 - * if it is a link... */
281 - if (realpath(tmdn, real_module_dir) == NULL)
284 - module_dir = real_module_dir;
285 - recursive_action(module_dir, ACTION_RECURSE,
286 - check_module_name_match, 0, m_fullName, 0);
290 - /* Check if we have found anything yet */
291 - if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL)) {
292 - char module_dir[FILENAME_MAX];
296 - if (realpath (_PATH_MODULES, module_dir) == NULL)
297 - strcpy(module_dir, _PATH_MODULES);
298 - /* No module found under /lib/modules/`uname -r`, this
299 - * time cast the net a bit wider. Search /lib/modules/ */
300 - if (!recursive_action(module_dir, ACTION_RECURSE,
301 - check_module_name_match, 0, m_fullName, 0)
303 - if (m_filename == 0
304 - || ((fp = fopen(m_filename, "r")) == NULL)
306 - bb_error_msg("%s: no module by that name found", m_fullName);
310 - bb_error_msg_and_die("%s: no module by that name 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 @@ -4120,7 +4088,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 @@ -4173,7 +4141,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 @@ -4220,30 +4188,22 @@
351 if (flag_print_load_map)
354 - exit_status = EXIT_SUCCESS;
358 #if ENABLE_FEATURE_CLEAN_UP
370 - return exit_status;
378 #if ENABLE_FEATURE_2_6_MODULES
380 -#include <sys/mman.h>
381 -#include <asm/unistd.h>
382 -#include <sys/syscall.h>
384 /* We use error numbers in a loose translation... */
385 static const char *moderror(int err)
387 @@ -4261,19 +4221,33 @@
391 -int insmod_ng_main(int argc, char **argv);
392 -int insmod_ng_main(int argc, char **argv)
393 +int insmod_main_26(int argc, char **argv)
397 + char *filename, *options;
398 + struct utsname myuname;
403 - char *filename, *options;
406 + /* check the kernel version */
407 + if ((uname(&myuname) != 0) || (myuname.release[0] != '2'))
410 + k_version = myuname.release[2] - '0';
411 + if (k_version <= 4)
418 + g_filename = filename;
419 + ret = find_module(filename);
423 /* Rest is options */
424 options = xzalloc(1);
426 @@ -4283,36 +4257,46 @@
427 optlen += sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
431 - /* Any special reason why mmap? It isn't performace critical... */
435 - fd = xopen(filename, O_RDONLY);
438 - map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
439 - if (map == MAP_FAILED) {
440 - bb_perror_msg_and_die("cannot mmap '%s'", filename);
443 - /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
445 - map = xmalloc(len);
446 - xread(fd, map, len);
449 len = MAXINT(ssize_t);
450 - map = xmalloc_open_read_close(filename, &len);
453 + map = xmalloc_open_read_close(g_filename, &len);
454 ret = syscall(__NR_init_module, map, len, options);
456 bb_perror_msg_and_die("cannot insert '%s': %s (%li)",
457 - filename, moderror(errno), ret);
458 + g_filename, moderror(errno), ret);
461 + if (g_filename && (g_filename != filename))
470 +int insmod_main(int argc, char **argv)
474 +#if ENABLE_FEATURE_2_6_MODULES
475 + ret = insmod_main_26(argc, argv);
476 + if (ret != ENOTSUP)
480 +#if ENABLE_FEATURE_2_4_MODULES
481 + ret = insmod_main_24(argc, argv);
482 + if (ret != ENOTSUP)
486 + fprintf(stderr, "Error: Kernel version not supported\n");
492 + bb_perror_msg("Loading module failed");