2 * Common modutils related functions for busybox
4 * Copyright (C) 2008 by Timo Teras <timo.teras@iki.fi>
6 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11 extern int init_module(void *module, unsigned long len, const char *options);
12 extern int delete_module(const char *module, unsigned int flags);
14 # include <sys/syscall.h>
15 # define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
16 # if defined(__NR_finit_module)
17 # define finit_module(fd, uargs, flags) syscall(__NR_finit_module, fd, uargs, flags)
19 # define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
22 static module_entry *helper_get_module(module_db *db, const char *module, int create)
24 char modname[MODULE_NAME_LEN];
25 struct module_entry *e;
28 filename2modname(module, modname);
31 for (i = 0; modname[i]; i++)
32 hash = ((hash << 5) + hash) + modname[i];
33 hash %= MODULE_HASH_SIZE;
35 for (e = db->buckets[hash]; e; e = e->next)
36 if (strcmp(e->modname, modname) == 0)
41 e = xzalloc(sizeof(*e));
42 e->modname = xstrdup(modname);
43 e->next = db->buckets[hash];
44 db->buckets[hash] = e;
45 IF_DEPMOD(e->dnext = e->dprev = e;)
49 module_entry* FAST_FUNC moddb_get(module_db *db, const char *module)
51 return helper_get_module(db, module, 0);
53 module_entry* FAST_FUNC moddb_get_or_create(module_db *db, const char *module)
55 return helper_get_module(db, module, 1);
58 void FAST_FUNC moddb_free(module_db *db)
63 for (i = 0; i < MODULE_HASH_SIZE; i++) {
64 for (e = db->buckets[i]; e; e = n) {
73 void FAST_FUNC replace(char *s, char what, char with)
82 char* FAST_FUNC replace_underscores(char *s)
88 int FAST_FUNC string_to_llist(char *string, llist_t **llist, const char *delim)
93 while ((tok = strsep(&string, delim)) != NULL) {
96 llist_add_to_end(llist, xstrdup(tok));
102 char* FAST_FUNC filename2modname(const char *filename, char *modname)
104 char local_modname[MODULE_NAME_LEN];
108 if (filename == NULL)
111 modname = local_modname;
112 // Disabled since otherwise "modprobe dir/name" would work
113 // as if it is "modprobe name". It is unclear why
114 // 'basenamization' was here in the first place.
115 //from = bb_get_last_path_component_nostrip(filename);
117 for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++)
118 modname[i] = (from[i] == '-') ? '_' : from[i];
121 if (modname == local_modname)
122 return xstrdup(modname);
127 char* FAST_FUNC parse_cmdline_module_options(char **argv, int quote_spaces)
132 options = xzalloc(1);
140 options = xrealloc(options, optlen + 2 + strlen(var) + 2);
142 val = strchrnul(var, '=');
145 * modprobe (module-init-tools version 3.11.1) compat:
147 * var="val with spaces", not "var=val with spaces"
148 * (note: var *name* is not checked for spaces!)
150 if (*val) { /* has var=val format. skip '=' */
152 if (strchr(val, ' '))
156 optlen += sprintf(options + optlen, fmt, (int)(val - var), var, val);
158 /* Remove trailing space. Disabled */
159 /* if (optlen != 0) options[optlen-1] = '\0'; */
163 #if ENABLE_FEATURE_INSMOD_TRY_MMAP
164 void* FAST_FUNC try_to_mmap_module(const char *filename, size_t *image_size_p)
166 /* We have user reports of failure to load 3MB module
167 * on a 16MB RAM machine. Apparently even a transient
168 * memory spike to 6MB during module load
169 * is too big for that system. */
174 fd = xopen(filename, O_RDONLY);
177 /* st.st_size is off_t, we can't just pass it to mmap */
178 if (st.st_size <= *image_size_p) {
179 size_t image_size = st.st_size;
180 image = mmap(NULL, image_size, PROT_READ, MAP_PRIVATE, fd, 0);
181 if (image == MAP_FAILED) {
183 } else if (*(uint32_t*)image != SWAP_BE32(0x7f454C46)) {
184 /* No ELF signature. Compressed module? */
185 munmap(image, image_size);
188 /* Success. Report the size */
189 *image_size_p = image_size;
199 * -errno on open/read error,
200 * errno on init_module() error
202 int FAST_FUNC bb_init_module(const char *filename, const char *options)
212 //TODO: audit bb_init_module_24 to match error code convention
213 #if ENABLE_FEATURE_2_4_MODULES
214 if (get_linux_version_code() < KERNEL_VERSION(2,6,0))
215 return bb_init_module_24(filename, options);
219 * First we try finit_module if available. Some kernels are configured
220 * to only allow loading of modules off of secure storage (like a read-
221 * only rootfs) which needs the finit_module call. If it fails, we fall
222 * back to normal module loading to support compressed modules.
224 # ifdef __NR_finit_module
226 int fd = open(filename, O_RDONLY | O_CLOEXEC);
228 rc = finit_module(fd, options, 0) != 0;
236 image_size = INT_MAX - 4095;
238 image = try_to_mmap_module(filename, &image_size);
242 errno = ENOMEM; /* may be changed by e.g. open errors below */
243 image = xmalloc_open_zipped_read_close(filename, &image_size);
249 init_module(image, image_size, options);
252 munmap(image, image_size);
258 int FAST_FUNC bb_delete_module(const char *module, unsigned int flags)
261 delete_module(module, flags);
265 /* Note: not suitable for delete_module() errnos.
266 * For them, probably only EWOULDBLOCK needs explaining:
267 * "Other modules depend on us". So far we don't do such
268 * translation and don't use moderror() for removal errors.
270 const char* FAST_FUNC moderror(int err)
273 case -1: /* btw: it's -EPERM */
274 return "no such module";
276 return "invalid module format";
278 return "unknown symbol in module, or unknown parameter";
280 return "module has wrong symbol version";
282 return "kernel does not support requested operation";
284 if (err < 0) /* should always be */
286 return strerror(err);