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.
10 #include <sys/syscall.h>
12 #define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
13 #if defined(__NR_finit_module)
14 # define finit_module(fd, uargs, flags) syscall(__NR_finit_module, fd, uargs, flags)
16 #define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
18 static module_entry *helper_get_module(module_db *db, const char *module, int create)
20 char modname[MODULE_NAME_LEN];
21 struct module_entry *e;
24 filename2modname(module, modname);
27 for (i = 0; modname[i]; i++)
28 hash = ((hash << 5) + hash) + modname[i];
29 hash %= MODULE_HASH_SIZE;
31 for (e = db->buckets[hash]; e; e = e->next)
32 if (strcmp(e->modname, modname) == 0)
37 e = xzalloc(sizeof(*e));
38 e->modname = xstrdup(modname);
39 e->next = db->buckets[hash];
40 db->buckets[hash] = e;
41 IF_DEPMOD(e->dnext = e->dprev = e;)
45 module_entry* FAST_FUNC moddb_get(module_db *db, const char *module)
47 return helper_get_module(db, module, 0);
49 module_entry* FAST_FUNC moddb_get_or_create(module_db *db, const char *module)
51 return helper_get_module(db, module, 1);
54 void FAST_FUNC moddb_free(module_db *db)
59 for (i = 0; i < MODULE_HASH_SIZE; i++) {
60 for (e = db->buckets[i]; e; e = n) {
69 void FAST_FUNC replace(char *s, char what, char with)
78 int FAST_FUNC string_to_llist(char *string, llist_t **llist, const char *delim)
83 while ((tok = strsep(&string, delim)) != NULL) {
86 llist_add_to_end(llist, xstrdup(tok));
92 char* FAST_FUNC filename2modname(const char *filename, char *modname)
94 char local_modname[MODULE_NAME_LEN];
101 modname = local_modname;
102 // Disabled since otherwise "modprobe dir/name" would work
103 // as if it is "modprobe name". It is unclear why
104 // 'basenamization' was here in the first place.
105 //from = bb_get_last_path_component_nostrip(filename);
107 for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++)
108 modname[i] = (from[i] == '-') ? '_' : from[i];
111 if (modname == local_modname)
112 return xstrdup(modname);
117 #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
118 char* FAST_FUNC parse_cmdline_module_options(char **argv, int quote_spaces)
123 options = xzalloc(1);
131 options = xrealloc(options, optlen + 2 + strlen(var) + 2);
133 val = strchrnul(var, '=');
136 * modprobe (module-init-tools version 3.11.1) compat:
138 * var="val with spaces", not "var=val with spaces"
139 * (note: var *name* is not checked for spaces!)
141 if (*val) { /* has var=val format. skip '=' */
143 if (strchr(val, ' '))
147 optlen += sprintf(options + optlen, fmt, (int)(val - var), var, val);
149 /* Remove trailing space. Disabled */
150 /* if (optlen != 0) options[optlen-1] = '\0'; */
155 #if ENABLE_FEATURE_INSMOD_TRY_MMAP
156 void* FAST_FUNC try_to_mmap_module(const char *filename, size_t *image_size_p)
158 /* We have user reports of failure to load 3MB module
159 * on a 16MB RAM machine. Apparently even a transient
160 * memory spike to 6MB during module load
161 * is too big for that system. */
166 fd = xopen(filename, O_RDONLY);
169 /* st.st_size is off_t, we can't just pass it to mmap */
170 if (st.st_size <= *image_size_p) {
171 size_t image_size = st.st_size;
172 image = mmap(NULL, image_size, PROT_READ, MAP_PRIVATE, fd, 0);
173 if (image == MAP_FAILED) {
175 } else if (*(uint32_t*)image != SWAP_BE32(0x7f454C46)) {
176 /* No ELF signature. Compressed module? */
177 munmap(image, image_size);
180 /* Success. Report the size */
181 *image_size_p = image_size;
191 * -errno on open/read error,
192 * errno on init_module() error
194 int FAST_FUNC bb_init_module(const char *filename, const char *options)
204 //TODO: audit bb_init_module_24 to match error code convention
205 #if ENABLE_FEATURE_2_4_MODULES
206 if (get_linux_version_code() < KERNEL_VERSION(2,6,0))
207 return bb_init_module_24(filename, options);
211 * First we try finit_module if available. Some kernels are configured
212 * to only allow loading of modules off of secure storage (like a read-
213 * only rootfs) which needs the finit_module call. If it fails, we fall
214 * back to normal module loading to support compressed modules.
216 # ifdef __NR_finit_module
218 int fd = open(filename, O_RDONLY | O_CLOEXEC);
220 rc = finit_module(fd, options, 0) != 0;
228 image_size = INT_MAX - 4095;
230 image = try_to_mmap_module(filename, &image_size);
234 errno = ENOMEM; /* may be changed by e.g. open errors below */
235 image = xmalloc_open_zipped_read_close(filename, &image_size);
241 init_module(image, image_size, options);
244 munmap(image, image_size);
250 int FAST_FUNC bb_delete_module(const char *module, unsigned int flags)
253 delete_module(module, flags);
257 /* Note: not suitable for delete_module() errnos.
258 * For them, probably only EWOULDBLOCK needs explaining:
259 * "Other modules depend on us". So far we don't do such
260 * translation and don't use moderror() for removal errors.
262 const char* FAST_FUNC moderror(int err)
265 case -1: /* btw: it's -EPERM */
266 return "no such module";
268 return "invalid module format";
270 return "unknown symbol in module, or unknown parameter";
272 return "module has wrong symbol version";
274 return "kernel does not support requested operation";
276 if (err < 0) /* should always be */
278 return strerror(err);