/*
* Mini insmod implementation for busybox
*
- * Copyright (C) 1999,2000 by Lineo, inc.
+ * Copyright (C) 1999,2000,2001 by Lineo, inc.
* Written by Erik Andersen <andersen@lineo.com>
* and Ron Alder <alder@lineo.com>
*
#include <dirent.h>
#include <ctype.h>
#include <assert.h>
+#include <string.h>
#include <getopt.h>
#include <sys/utsname.h>
+#include <sys/syscall.h>
+#include <linux/unistd.h>
//----------------------------------------------------------------------------
//--------modutils module.h, lines 45-242
#ifndef MODUTILS_MODULE_H
static const int MODUTILS_MODULE_H = 1;
-#ident "$Id: insmod.c,v 1.36 2001/01/23 22:30:04 markw Exp $"
+#ident "$Id: insmod.c,v 1.46 2001/02/14 21:23:06 andersen Exp $"
/* This file contains the structures used by the 2.0 and 2.1 kernels.
We do not use the kernel headers directly because we do not wish
#ifndef MODUTILS_OBJ_H
static const int MODUTILS_OBJ_H = 1;
-#ident "$Id: insmod.c,v 1.36 2001/01/23 22:30:04 markw Exp $"
+#ident "$Id: insmod.c,v 1.46 2001/02/14 21:23:06 andersen Exp $"
/* The relocatable object is manipulated using elfin types. */
#define Elf32_RelM Elf32_Rel
#else
-#error insmod.c no platform specified
+#error Sorry, but insmod.c does not yet support this architecture...
#endif
#ifndef ElfW
#define _PATH_MODULES "/lib/modules"
static const int STRVERSIONLEN = 32;
-#if !defined(BB_FEATURE_INSMOD_NEW_KERNEL) && !defined(BB_FEATURE_INSMOD_OLD_KERNEL)
-#error "Must have ether BB_FEATURE_INSMOD_NEW_KERNEL or BB_FEATURE_INSMOD_OLD_KERNEL defined"
-#endif
-
/*======================================================================*/
int flag_force_load = 0;
-- Bryan Rittmeyer <bryan@ixiacom.com> */
-#ifdef BB_FEATURE_INSMOD_OLD_KERNEL
+#ifdef BB_FEATURE_OLD_MODULE_INTERFACE
_syscall1(int, get_kernel_syms, struct old_kernel_sym *, ks)
#endif
/* Don't report an error if the symbol is coming from
the kernel or some external module. */
if (secidx <= SHN_HIRESERVE)
- error_msg("%s multiply defined\n", name);
+ error_msg("%s multiply defined", name);
return sym;
}
}
/* Also check that the parameter was not resolved from the kernel. */
if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
- error_msg("symbol for parameter %s not found\n", p);
+ error_msg("symbol for parameter %s not found", p);
return 0;
}
str = alloca(strlen(q));
for (r = str, q++; *q != '"'; ++q, ++r) {
if (*q == '\0') {
- error_msg("improperly terminated string argument for %s\n", p);
+ error_msg("improperly terminated string argument for %s", p);
return 0;
} else if (*q == '\\')
switch (*++q) {
#endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
-#ifdef BB_FEATURE_INSMOD_OLD_KERNEL
+#ifdef BB_FEATURE_OLD_MODULE_INTERFACE
/* Fetch all the symbols and divvy them up as appropriate for the modules. */
#define old_create_mod_use_count(x) TRUE
#define old_init_module(x, y, z) TRUE
-#endif /* BB_FEATURE_INSMOD_OLD_KERNEL */
+#endif /* BB_FEATURE_OLD_MODULE_INTERFACE */
p = get_modinfo_value(f, key);
key += 5;
if (p == NULL) {
- error_msg("invalid parameter %s\n", key);
+ error_msg("invalid parameter %s", key);
return 0;
}
/* Also check that the parameter was not resolved from the kernel. */
if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
- error_msg("symbol for parameter %s not found\n", key);
+ error_msg("symbol for parameter %s not found", key);
return 0;
}
str = alloca(strlen(q));
for (r = str, q++; *q != '"'; ++q, ++r) {
if (*q == '\0') {
- error_msg("improperly terminated string argument for %s\n",
+ error_msg("improperly terminated string argument for %s",
key);
return 0;
} else if (*q == '\\')
/* Probably we should do that outside the loop ? */
if (!isdigit(*(p + 1))) {
error_msg("parameter type 'c' for %s must be followed by"
- " the maximum size\n", key);
+ " the maximum size", key);
return 0;
}
charssize = strtoul(p + 1, (char **) NULL, 10);
/* Check length */
if (strlen(str) >= charssize) {
- error_msg("string too long for %s (max %ld)\n", key,
+ error_msg("string too long for %s (max %ld)", key,
charssize - 1);
return 0;
}
break;
default:
- error_msg("unknown parameter type '%c' for %s\n", *p, key);
+ error_msg("unknown parameter type '%c' for %s", *p, key);
return 0;
}
}
case ',':
if (++n > max) {
- error_msg("too many values for %s (max %d)\n", key, max);
+ error_msg("too many values for %s (max %d)", key, max);
return 0;
}
++q;
break;
default:
- error_msg("invalid argument syntax for %s\n", key);
+ error_msg("invalid argument syntax for %s", key);
return 0;
}
}
end_of_arg:
if (n < min) {
- error_msg("too few values for %s (min %d)\n", key, min);
+ error_msg("too few values for %s (min %d)", key, min);
return 0;
}
#endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
-#ifdef BB_FEATURE_INSMOD_NEW_KERNEL
+#ifdef BB_FEATURE_NEW_MODULE_INTERFACE
/* Fetch the loaded modules, and all currently exported symbols. */
#define new_create_module_ksymtab(x)
#define query_module(v, w, x, y, z) -1
-#endif /* BB_FEATURE_INSMOD_NEW_KERNEL */
+#endif /* BB_FEATURE_NEW_MODULE_INTERFACE */
/*======================================================================*/
sym->secidx = SHN_ABS;
sym->value = 0;
} else {
- error_msg("unresolved symbol %s\n", sym->name);
+ error_msg("unresolved symbol %s", sym->name);
ret = 0;
}
}
errmsg = "Unhandled relocation";
bad_reloc:
if (extsym) {
- error_msg("%s of type %ld for %s\n", errmsg,
+ error_msg("%s of type %ld for %s", errmsg,
(long) ELFW(R_TYPE) (rel->r_info),
strtab + extsym->st_name);
} else {
- error_msg("%s of type %ld\n", errmsg,
+ error_msg("%s of type %ld", errmsg,
(long) ELFW(R_TYPE) (rel->r_info));
}
ret = 0;
|| f->header.e_ident[EI_MAG1] != ELFMAG1
|| f->header.e_ident[EI_MAG2] != ELFMAG2
|| f->header.e_ident[EI_MAG3] != ELFMAG3) {
- error_msg("not an ELF file\n");
+ error_msg("not an ELF file");
return NULL;
}
if (f->header.e_ident[EI_CLASS] != ELFCLASSM
|| f->header.e_ident[EI_DATA] != ELFDATAM
|| f->header.e_ident[EI_VERSION] != EV_CURRENT
|| !MATCH_MACHINE(f->header.e_machine)) {
- error_msg("ELF file not for this architecture\n");
+ error_msg("ELF file not for this architecture");
return NULL;
}
if (f->header.e_type != ET_REL) {
- error_msg("ELF file not a relocatable object\n");
+ error_msg("ELF file not a relocatable object");
return NULL;
}
/* Read the section headers. */
if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
- error_msg("section header size mismatch: %lu != %lu\n",
+ error_msg("section header size mismatch: %lu != %lu",
(unsigned long) f->header.e_shentsize,
(unsigned long) sizeof(ElfW(Shdr)));
return NULL;
#if SHT_RELM == SHT_REL
case SHT_RELA:
- error_msg("RELA relocations not supported on this architecture\n");
+ error_msg("RELA relocations not supported on this architecture");
return NULL;
#else
case SHT_REL:
- error_msg("REL relocations not supported on this architecture\n");
+ error_msg("REL relocations not supported on this architecture");
return NULL;
#endif
break;
}
- error_msg("can't handle sections of type %ld\n",
+ error_msg("can't handle sections of type %ld",
(long) sec->header.sh_type);
return NULL;
}
ElfW(Sym) * sym;
if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
- error_msg("symbol size mismatch: %lu != %lu\n",
+ error_msg("symbol size mismatch: %lu != %lu",
(unsigned long) sec->header.sh_entsize,
(unsigned long) sizeof(ElfW(Sym)));
return NULL;
case SHT_RELM:
if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
- error_msg("relocation entry size mismatch: %lu != %lu\n",
+ error_msg("relocation entry size mismatch: %lu != %lu",
(unsigned long) sec->header.sh_entsize,
(unsigned long) sizeof(ElfW(RelM)));
return NULL;
extern int insmod_main( int argc, char **argv)
{
+ int opt;
int k_crcs;
int k_new_syscalls;
int len;
int m_crcs;
#endif
-
- if (argc <= 1) {
- usage(insmod_usage);
- }
-
/* Parse any options */
- while (--argc > 0 && **(++argv) == '-') {
- while (*(++(*argv))) {
- switch (**argv) {
+ while ((opt = getopt(argc, argv, "fkvxLo:")) > 0) {
+ switch (opt) {
case 'f': /* force loading */
flag_force_load = 1;
break;
case 'x': /* do not export externs */
flag_export = 0;
break;
+ case 'o': /* name the output module */
+ strncpy(m_name, optarg, BUFSIZ);
+ break;
+ case 'L': /* Stub warning */
+ /* This is needed for compatibility with modprobe.
+ * In theory, this does locking, but we don't do
+ * that. So be careful and plan your life around not
+ * loading the same module 50 times concurrently. */
+ break;
default:
- usage(insmod_usage);
- }
+ show_usage();
}
}
-
- if (argc <= 0) {
- usage(insmod_usage);
+
+ if (argv[optind] == NULL) {
+ show_usage();
}
+
/* Grab the module name */
- if ((tmp = strrchr(*argv, '/')) != NULL) {
+ if ((tmp = strrchr(argv[optind], '/')) != NULL) {
tmp++;
} else {
- tmp = *argv;
+ tmp = argv[optind];
}
len = strlen(tmp);
if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
len -= 2;
- memcpy(m_name, tmp, len);
- strcpy(m_fullName, m_name);
+ strncpy(m_fullName, tmp, len);
+ if (*m_name == '\0') {
+ strcpy(m_name, m_fullName);
+ }
strcat(m_fullName, ".o");
/* Get a filedesc for the module */
- if ((fp = fopen(*argv, "r")) == NULL) {
+ if ((fp = fopen(argv[optind], "r")) == NULL) {
/* Hmpf. Could not open it. Search through _PATH_MODULES to find a module named m_name */
if (recursive_action(_PATH_MODULES, TRUE, FALSE, FALSE,
findNamedModule, 0, m_fullName) == FALSE)
if (m_filename[0] == '\0'
|| ((fp = fopen(m_filename, "r")) == NULL))
{
- error_msg("No module named '%s' found in '%s'\n", m_fullName, _PATH_MODULES);
+ error_msg("No module named '%s' found in '%s'", m_fullName, _PATH_MODULES);
return EXIT_FAILURE;
}
} else
- error_msg_and_die("No module named '%s' found in '%s'\n", m_fullName, _PATH_MODULES);
+ error_msg_and_die("No module named '%s' found in '%s'", m_fullName, _PATH_MODULES);
} else
- memcpy(m_filename, *argv, strlen(*argv));
+ memcpy(m_filename, argv[optind], strlen(argv[optind]));
if ((f = obj_load(fp)) == NULL)
m_version = old_get_module_version(f, m_strversion);
if (m_version == -1) {
error_msg("couldn't find the kernel version the module was "
- "compiled for\n");
+ "compiled for");
goto out;
}
}
if (flag_force_load) {
error_msg("Warning: kernel-module version mismatch\n"
"\t%s was compiled for kernel version %s\n"
- "\twhile this kernel is version %s\n",
+ "\twhile this kernel is version %s",
m_filename, m_strversion, k_strversion);
} else {
error_msg("kernel-module version mismatch\n"
"\t%s was compiled for kernel version %s\n"
- "\twhile this kernel is version %s.\n",
+ "\twhile this kernel is version %s.",
m_filename, m_strversion, k_strversion);
goto out;
}
k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
if (k_new_syscalls) {
-#ifdef BB_FEATURE_INSMOD_NEW_KERNEL
+#ifdef BB_FEATURE_NEW_MODULE_INTERFACE
if (!new_get_kernel_symbols())
goto out;
k_crcs = new_is_kernel_checksummed();
#else
- error_msg("Not configured to support new kernels\n");
+ error_msg("Not configured to support new kernels");
goto out;
#endif
} else {
-#ifdef BB_FEATURE_INSMOD_OLD_KERNEL
+#ifdef BB_FEATURE_OLD_MODULE_INTERFACE
if (!old_get_kernel_symbols(m_name))
goto out;
k_crcs = old_is_kernel_checksummed();
#else
- error_msg("Not configured to support old kernels\n");
+ error_msg("Not configured to support old kernels");
goto out;
#endif
}
}
obj_allocate_commons(f);
+ /* done with the module name, on to the optional var=value arguments */
+ ++optind;
+
if (optind < argc) {
if (m_has_modinfo
? !new_process_module_arguments(f, argc - optind, argv + optind)
m_size = obj_load_size(f);
- errno = 0;
m_addr = create_module(m_name, m_size);
- switch (errno) {
- case 0:
- break;
+ if (m_addr==-1) switch (errno) {
case EEXIST:
- error_msg("A module named %s already exists\n", m_name);
+ error_msg("A module named %s already exists", m_name);
goto out;
case ENOMEM:
- error_msg("Can't allocate kernel memory for module; needed %lu bytes\n",
+ error_msg("Can't allocate kernel memory for module; needed %lu bytes",
m_size);
goto out;
default: