2008-09-13 20:29:38 +05:30
|
|
|
/*
|
|
|
|
* Common modutils related functions for busybox
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 by Timo Teras <timo.teras@iki.fi>
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2008-09-13 20:29:38 +05:30
|
|
|
*/
|
|
|
|
#include "modutils.h"
|
|
|
|
|
2016-12-27 00:37:59 +05:30
|
|
|
#include <sys/syscall.h>
|
|
|
|
|
|
|
|
#define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
|
|
|
|
#if defined(__NR_finit_module)
|
|
|
|
# define finit_module(fd, uargs, flags) syscall(__NR_finit_module, fd, uargs, flags)
|
2008-09-13 20:29:38 +05:30
|
|
|
#endif
|
2016-12-27 00:37:59 +05:30
|
|
|
#define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
|
2008-09-13 20:29:38 +05:30
|
|
|
|
2015-11-05 23:24:55 +05:30
|
|
|
static module_entry *helper_get_module(module_db *db, const char *module, int create)
|
|
|
|
{
|
|
|
|
char modname[MODULE_NAME_LEN];
|
|
|
|
struct module_entry *e;
|
|
|
|
unsigned i, hash;
|
|
|
|
|
|
|
|
filename2modname(module, modname);
|
|
|
|
|
|
|
|
hash = 0;
|
|
|
|
for (i = 0; modname[i]; i++)
|
|
|
|
hash = ((hash << 5) + hash) + modname[i];
|
|
|
|
hash %= MODULE_HASH_SIZE;
|
|
|
|
|
|
|
|
for (e = db->buckets[hash]; e; e = e->next)
|
|
|
|
if (strcmp(e->modname, modname) == 0)
|
|
|
|
return e;
|
|
|
|
if (!create)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
e = xzalloc(sizeof(*e));
|
|
|
|
e->modname = xstrdup(modname);
|
|
|
|
e->next = db->buckets[hash];
|
|
|
|
db->buckets[hash] = e;
|
2015-11-06 20:20:28 +05:30
|
|
|
IF_DEPMOD(e->dnext = e->dprev = e;)
|
2015-11-05 23:24:55 +05:30
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
module_entry* FAST_FUNC moddb_get(module_db *db, const char *module)
|
|
|
|
{
|
|
|
|
return helper_get_module(db, module, 0);
|
|
|
|
}
|
|
|
|
module_entry* FAST_FUNC moddb_get_or_create(module_db *db, const char *module)
|
|
|
|
{
|
|
|
|
return helper_get_module(db, module, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FAST_FUNC moddb_free(module_db *db)
|
|
|
|
{
|
|
|
|
module_entry *e, *n;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < MODULE_HASH_SIZE; i++) {
|
|
|
|
for (e = db->buckets[i]; e; e = n) {
|
|
|
|
n = e->next;
|
|
|
|
free(e->name);
|
|
|
|
free(e->modname);
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-13 20:29:38 +05:30
|
|
|
void FAST_FUNC replace(char *s, char what, char with)
|
|
|
|
{
|
|
|
|
while (*s) {
|
|
|
|
if (what == *s)
|
|
|
|
*s = with;
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-17 22:16:06 +05:30
|
|
|
char* FAST_FUNC replace_underscores(char *s)
|
2008-09-13 20:29:38 +05:30
|
|
|
{
|
|
|
|
replace(s, '-', '_');
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FAST_FUNC string_to_llist(char *string, llist_t **llist, const char *delim)
|
|
|
|
{
|
|
|
|
char *tok;
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
while ((tok = strsep(&string, delim)) != NULL) {
|
|
|
|
if (tok[0] == '\0')
|
|
|
|
continue;
|
|
|
|
llist_add_to_end(llist, xstrdup(tok));
|
|
|
|
len += strlen(tok);
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2009-06-17 22:16:06 +05:30
|
|
|
char* FAST_FUNC filename2modname(const char *filename, char *modname)
|
2008-09-13 20:29:38 +05:30
|
|
|
{
|
2015-01-25 03:00:30 +05:30
|
|
|
char local_modname[MODULE_NAME_LEN];
|
2008-09-13 20:29:38 +05:30
|
|
|
int i;
|
2015-01-01 23:32:40 +05:30
|
|
|
const char *from;
|
2008-09-13 20:29:38 +05:30
|
|
|
|
|
|
|
if (filename == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (modname == NULL)
|
2015-01-25 03:00:30 +05:30
|
|
|
modname = local_modname;
|
2015-01-01 23:32:40 +05:30
|
|
|
// Disabled since otherwise "modprobe dir/name" would work
|
|
|
|
// as if it is "modprobe name". It is unclear why
|
|
|
|
// 'basenamization' was here in the first place.
|
|
|
|
//from = bb_get_last_path_component_nostrip(filename);
|
|
|
|
from = filename;
|
2009-02-26 17:30:52 +05:30
|
|
|
for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++)
|
2008-09-13 20:29:38 +05:30
|
|
|
modname[i] = (from[i] == '-') ? '_' : from[i];
|
2009-05-18 16:38:04 +05:30
|
|
|
modname[i] = '\0';
|
2008-09-13 20:29:38 +05:30
|
|
|
|
2015-01-25 03:00:30 +05:30
|
|
|
if (modname == local_modname)
|
|
|
|
return xstrdup(modname);
|
|
|
|
|
2008-09-13 20:29:38 +05:30
|
|
|
return modname;
|
|
|
|
}
|
|
|
|
|
2011-02-02 04:30:36 +05:30
|
|
|
char* FAST_FUNC parse_cmdline_module_options(char **argv, int quote_spaces)
|
2008-09-13 20:29:38 +05:30
|
|
|
{
|
|
|
|
char *options;
|
|
|
|
int optlen;
|
|
|
|
|
|
|
|
options = xzalloc(1);
|
|
|
|
optlen = 0;
|
|
|
|
while (*++argv) {
|
2011-02-02 04:30:36 +05:30
|
|
|
const char *fmt;
|
|
|
|
const char *var;
|
|
|
|
const char *val;
|
|
|
|
|
|
|
|
var = *argv;
|
|
|
|
options = xrealloc(options, optlen + 2 + strlen(var) + 2);
|
|
|
|
fmt = "%.*s%s ";
|
|
|
|
val = strchrnul(var, '=');
|
|
|
|
if (quote_spaces) {
|
|
|
|
/*
|
|
|
|
* modprobe (module-init-tools version 3.11.1) compat:
|
|
|
|
* quote only value:
|
|
|
|
* var="val with spaces", not "var=val with spaces"
|
|
|
|
* (note: var *name* is not checked for spaces!)
|
|
|
|
*/
|
|
|
|
if (*val) { /* has var=val format. skip '=' */
|
|
|
|
val++;
|
|
|
|
if (strchr(val, ' '))
|
|
|
|
fmt = "%.*s\"%s\" ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
optlen += sprintf(options + optlen, fmt, (int)(val - var), var, val);
|
2008-09-13 20:29:38 +05:30
|
|
|
}
|
2011-02-02 04:30:36 +05:30
|
|
|
/* Remove trailing space. Disabled */
|
|
|
|
/* if (optlen != 0) options[optlen-1] = '\0'; */
|
2008-09-13 20:29:38 +05:30
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2009-10-25 09:05:22 +05:30
|
|
|
#if ENABLE_FEATURE_INSMOD_TRY_MMAP
|
|
|
|
void* FAST_FUNC try_to_mmap_module(const char *filename, size_t *image_size_p)
|
|
|
|
{
|
|
|
|
/* We have user reports of failure to load 3MB module
|
|
|
|
* on a 16MB RAM machine. Apparently even a transient
|
|
|
|
* memory spike to 6MB during module load
|
|
|
|
* is too big for that system. */
|
|
|
|
void *image;
|
|
|
|
struct stat st;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = xopen(filename, O_RDONLY);
|
|
|
|
fstat(fd, &st);
|
|
|
|
image = NULL;
|
|
|
|
/* st.st_size is off_t, we can't just pass it to mmap */
|
|
|
|
if (st.st_size <= *image_size_p) {
|
|
|
|
size_t image_size = st.st_size;
|
|
|
|
image = mmap(NULL, image_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
if (image == MAP_FAILED) {
|
|
|
|
image = NULL;
|
|
|
|
} else if (*(uint32_t*)image != SWAP_BE32(0x7f454C46)) {
|
|
|
|
/* No ELF signature. Compressed module? */
|
|
|
|
munmap(image, image_size);
|
|
|
|
image = NULL;
|
|
|
|
} else {
|
|
|
|
/* Success. Report the size */
|
|
|
|
*image_size_p = image_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-06-17 22:16:06 +05:30
|
|
|
/* Return:
|
|
|
|
* 0 on success,
|
|
|
|
* -errno on open/read error,
|
|
|
|
* errno on init_module() error
|
|
|
|
*/
|
2008-09-13 20:29:38 +05:30
|
|
|
int FAST_FUNC bb_init_module(const char *filename, const char *options)
|
|
|
|
{
|
2009-10-25 09:05:22 +05:30
|
|
|
size_t image_size;
|
2008-09-13 20:29:38 +05:30
|
|
|
char *image;
|
2009-04-14 02:02:31 +05:30
|
|
|
int rc;
|
2009-10-25 09:05:22 +05:30
|
|
|
bool mmaped;
|
2008-09-13 20:29:38 +05:30
|
|
|
|
2009-03-14 05:13:26 +05:30
|
|
|
if (!options)
|
|
|
|
options = "";
|
|
|
|
|
2009-06-17 22:16:06 +05:30
|
|
|
//TODO: audit bb_init_module_24 to match error code convention
|
2008-09-13 20:29:38 +05:30
|
|
|
#if ENABLE_FEATURE_2_4_MODULES
|
|
|
|
if (get_linux_version_code() < KERNEL_VERSION(2,6,0))
|
|
|
|
return bb_init_module_24(filename, options);
|
|
|
|
#endif
|
|
|
|
|
2016-09-15 15:46:33 +05:30
|
|
|
/*
|
|
|
|
* First we try finit_module if available. Some kernels are configured
|
|
|
|
* to only allow loading of modules off of secure storage (like a read-
|
|
|
|
* only rootfs) which needs the finit_module call. If it fails, we fall
|
|
|
|
* back to normal module loading to support compressed modules.
|
|
|
|
*/
|
|
|
|
# ifdef __NR_finit_module
|
|
|
|
{
|
|
|
|
int fd = open(filename, O_RDONLY | O_CLOEXEC);
|
|
|
|
if (fd >= 0) {
|
|
|
|
rc = finit_module(fd, options, 0) != 0;
|
|
|
|
close(fd);
|
|
|
|
if (rc == 0)
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
2009-10-25 09:05:22 +05:30
|
|
|
image_size = INT_MAX - 4095;
|
|
|
|
mmaped = 0;
|
|
|
|
image = try_to_mmap_module(filename, &image_size);
|
|
|
|
if (image) {
|
|
|
|
mmaped = 1;
|
|
|
|
} else {
|
|
|
|
errno = ENOMEM; /* may be changed by e.g. open errors below */
|
|
|
|
image = xmalloc_open_zipped_read_close(filename, &image_size);
|
|
|
|
if (!image)
|
|
|
|
return -errno;
|
|
|
|
}
|
2008-09-13 20:29:38 +05:30
|
|
|
|
2009-06-17 22:16:06 +05:30
|
|
|
errno = 0;
|
2009-10-25 09:05:22 +05:30
|
|
|
init_module(image, image_size, options);
|
2009-06-17 22:16:06 +05:30
|
|
|
rc = errno;
|
2009-10-25 09:05:22 +05:30
|
|
|
if (mmaped)
|
|
|
|
munmap(image, image_size);
|
|
|
|
else
|
|
|
|
free(image);
|
2008-09-13 20:29:38 +05:30
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FAST_FUNC bb_delete_module(const char *module, unsigned int flags)
|
|
|
|
{
|
2009-06-17 22:16:06 +05:30
|
|
|
errno = 0;
|
|
|
|
delete_module(module, flags);
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
2015-10-24 07:47:04 +05:30
|
|
|
/* Note: not suitable for delete_module() errnos.
|
|
|
|
* For them, probably only EWOULDBLOCK needs explaining:
|
|
|
|
* "Other modules depend on us". So far we don't do such
|
|
|
|
* translation and don't use moderror() for removal errors.
|
|
|
|
*/
|
2009-06-17 22:16:06 +05:30
|
|
|
const char* FAST_FUNC moderror(int err)
|
|
|
|
{
|
|
|
|
switch (err) {
|
|
|
|
case -1: /* btw: it's -EPERM */
|
|
|
|
return "no such module";
|
|
|
|
case ENOEXEC:
|
|
|
|
return "invalid module format";
|
|
|
|
case ENOENT:
|
|
|
|
return "unknown symbol in module, or unknown parameter";
|
|
|
|
case ESRCH:
|
|
|
|
return "module has wrong symbol version";
|
|
|
|
case ENOSYS:
|
|
|
|
return "kernel does not support requested operation";
|
|
|
|
}
|
|
|
|
if (err < 0) /* should always be */
|
|
|
|
err = -err;
|
|
|
|
return strerror(err);
|
2008-09-13 20:29:38 +05:30
|
|
|
}
|