blob: dae623ee4f9ecbaa956f44d10f099f155025bf27 [file] [log] [blame]
Denis Vlasenkoba1315d2008-09-13 14:59:38 +00001/*
2 * Common modutils related functions for busybox
3 *
4 * Copyright (C) 2008 by Timo Teras <timo.teras@iki.fi>
5 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoba1315d2008-09-13 14:59:38 +00007 */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +00008#include "modutils.h"
9
Waldemar Brodkorb9c083f52016-12-26 20:07:59 +010010#include <sys/syscall.h>
11
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)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000015#endif
Waldemar Brodkorb9c083f52016-12-26 20:07:59 +010016#define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000017
Timo Teräs48dc80b2015-11-05 18:54:55 +010018static module_entry *helper_get_module(module_db *db, const char *module, int create)
19{
20 char modname[MODULE_NAME_LEN];
21 struct module_entry *e;
22 unsigned i, hash;
23
24 filename2modname(module, modname);
25
26 hash = 0;
27 for (i = 0; modname[i]; i++)
28 hash = ((hash << 5) + hash) + modname[i];
29 hash %= MODULE_HASH_SIZE;
30
31 for (e = db->buckets[hash]; e; e = e->next)
32 if (strcmp(e->modname, modname) == 0)
33 return e;
34 if (!create)
35 return NULL;
36
37 e = xzalloc(sizeof(*e));
38 e->modname = xstrdup(modname);
39 e->next = db->buckets[hash];
40 db->buckets[hash] = e;
Denys Vlasenko196e4002015-11-06 15:50:28 +010041 IF_DEPMOD(e->dnext = e->dprev = e;)
Timo Teräs48dc80b2015-11-05 18:54:55 +010042
43 return e;
44}
45module_entry* FAST_FUNC moddb_get(module_db *db, const char *module)
46{
47 return helper_get_module(db, module, 0);
48}
49module_entry* FAST_FUNC moddb_get_or_create(module_db *db, const char *module)
50{
51 return helper_get_module(db, module, 1);
52}
53
54void FAST_FUNC moddb_free(module_db *db)
55{
56 module_entry *e, *n;
57 unsigned i;
58
59 for (i = 0; i < MODULE_HASH_SIZE; i++) {
60 for (e = db->buckets[i]; e; e = n) {
61 n = e->next;
62 free(e->name);
63 free(e->modname);
64 free(e);
65 }
66 }
67}
68
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000069void FAST_FUNC replace(char *s, char what, char with)
70{
71 while (*s) {
72 if (what == *s)
73 *s = with;
74 ++s;
75 }
76}
77
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +020078char* FAST_FUNC replace_underscores(char *s)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000079{
80 replace(s, '-', '_');
81 return s;
82}
83
84int FAST_FUNC string_to_llist(char *string, llist_t **llist, const char *delim)
85{
86 char *tok;
87 int len = 0;
88
89 while ((tok = strsep(&string, delim)) != NULL) {
90 if (tok[0] == '\0')
91 continue;
92 llist_add_to_end(llist, xstrdup(tok));
93 len += strlen(tok);
94 }
95 return len;
96}
97
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +020098char* FAST_FUNC filename2modname(const char *filename, char *modname)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000099{
Denys Vlasenkocc70b6f2015-01-24 22:30:30 +0100100 char local_modname[MODULE_NAME_LEN];
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000101 int i;
Denys Vlasenko78854522015-01-01 19:02:40 +0100102 const char *from;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000103
104 if (filename == NULL)
105 return NULL;
106 if (modname == NULL)
Denys Vlasenkocc70b6f2015-01-24 22:30:30 +0100107 modname = local_modname;
Denys Vlasenko78854522015-01-01 19:02:40 +0100108 // Disabled since otherwise "modprobe dir/name" would work
109 // as if it is "modprobe name". It is unclear why
110 // 'basenamization' was here in the first place.
111 //from = bb_get_last_path_component_nostrip(filename);
112 from = filename;
Denis Vlasenko48637e02009-02-26 12:00:52 +0000113 for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000114 modname[i] = (from[i] == '-') ? '_' : from[i];
Denys Vlasenko16bda3b2009-05-18 13:08:04 +0200115 modname[i] = '\0';
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000116
Denys Vlasenkocc70b6f2015-01-24 22:30:30 +0100117 if (modname == local_modname)
118 return xstrdup(modname);
119
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000120 return modname;
121}
122
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +0800123#if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
Denys Vlasenkoc5830bd2011-02-02 00:00:36 +0100124char* FAST_FUNC parse_cmdline_module_options(char **argv, int quote_spaces)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000125{
126 char *options;
127 int optlen;
128
129 options = xzalloc(1);
130 optlen = 0;
131 while (*++argv) {
Denys Vlasenkoc5830bd2011-02-02 00:00:36 +0100132 const char *fmt;
133 const char *var;
134 const char *val;
135
136 var = *argv;
137 options = xrealloc(options, optlen + 2 + strlen(var) + 2);
138 fmt = "%.*s%s ";
139 val = strchrnul(var, '=');
140 if (quote_spaces) {
141 /*
142 * modprobe (module-init-tools version 3.11.1) compat:
143 * quote only value:
144 * var="val with spaces", not "var=val with spaces"
145 * (note: var *name* is not checked for spaces!)
146 */
147 if (*val) { /* has var=val format. skip '=' */
148 val++;
149 if (strchr(val, ' '))
150 fmt = "%.*s\"%s\" ";
151 }
152 }
153 optlen += sprintf(options + optlen, fmt, (int)(val - var), var, val);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000154 }
Denys Vlasenkoc5830bd2011-02-02 00:00:36 +0100155 /* Remove trailing space. Disabled */
156 /* if (optlen != 0) options[optlen-1] = '\0'; */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000157 return options;
158}
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +0800159#endif
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000160
Denys Vlasenko77c066e2009-10-25 04:35:22 +0100161#if ENABLE_FEATURE_INSMOD_TRY_MMAP
162void* FAST_FUNC try_to_mmap_module(const char *filename, size_t *image_size_p)
163{
164 /* We have user reports of failure to load 3MB module
165 * on a 16MB RAM machine. Apparently even a transient
166 * memory spike to 6MB during module load
167 * is too big for that system. */
168 void *image;
169 struct stat st;
170 int fd;
171
172 fd = xopen(filename, O_RDONLY);
173 fstat(fd, &st);
174 image = NULL;
175 /* st.st_size is off_t, we can't just pass it to mmap */
176 if (st.st_size <= *image_size_p) {
177 size_t image_size = st.st_size;
178 image = mmap(NULL, image_size, PROT_READ, MAP_PRIVATE, fd, 0);
179 if (image == MAP_FAILED) {
180 image = NULL;
181 } else if (*(uint32_t*)image != SWAP_BE32(0x7f454C46)) {
182 /* No ELF signature. Compressed module? */
183 munmap(image, image_size);
184 image = NULL;
185 } else {
186 /* Success. Report the size */
187 *image_size_p = image_size;
188 }
189 }
190 close(fd);
191 return image;
192}
193#endif
194
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200195/* Return:
196 * 0 on success,
197 * -errno on open/read error,
198 * errno on init_module() error
199 */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000200int FAST_FUNC bb_init_module(const char *filename, const char *options)
201{
Denys Vlasenko77c066e2009-10-25 04:35:22 +0100202 size_t image_size;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000203 char *image;
Denis Vlasenko41445042009-04-13 20:32:31 +0000204 int rc;
Denys Vlasenko77c066e2009-10-25 04:35:22 +0100205 bool mmaped;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000206
Denis Vlasenko3b5c9b82009-03-13 23:43:26 +0000207 if (!options)
208 options = "";
209
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200210//TODO: audit bb_init_module_24 to match error code convention
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000211#if ENABLE_FEATURE_2_4_MODULES
212 if (get_linux_version_code() < KERNEL_VERSION(2,6,0))
213 return bb_init_module_24(filename, options);
214#endif
215
Mike Frysinger3a45b872016-09-15 12:16:33 +0200216 /*
217 * First we try finit_module if available. Some kernels are configured
218 * to only allow loading of modules off of secure storage (like a read-
219 * only rootfs) which needs the finit_module call. If it fails, we fall
220 * back to normal module loading to support compressed modules.
221 */
222# ifdef __NR_finit_module
223 {
224 int fd = open(filename, O_RDONLY | O_CLOEXEC);
225 if (fd >= 0) {
226 rc = finit_module(fd, options, 0) != 0;
227 close(fd);
228 if (rc == 0)
229 return rc;
230 }
231 }
232# endif
233
Denys Vlasenko77c066e2009-10-25 04:35:22 +0100234 image_size = INT_MAX - 4095;
235 mmaped = 0;
236 image = try_to_mmap_module(filename, &image_size);
237 if (image) {
238 mmaped = 1;
239 } else {
240 errno = ENOMEM; /* may be changed by e.g. open errors below */
241 image = xmalloc_open_zipped_read_close(filename, &image_size);
242 if (!image)
243 return -errno;
244 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000245
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200246 errno = 0;
Denys Vlasenko77c066e2009-10-25 04:35:22 +0100247 init_module(image, image_size, options);
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200248 rc = errno;
Denys Vlasenko77c066e2009-10-25 04:35:22 +0100249 if (mmaped)
250 munmap(image, image_size);
251 else
252 free(image);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000253 return rc;
254}
255
256int FAST_FUNC bb_delete_module(const char *module, unsigned int flags)
257{
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200258 errno = 0;
259 delete_module(module, flags);
260 return errno;
261}
262
Denys Vlasenkocd139742015-10-24 04:17:04 +0200263/* Note: not suitable for delete_module() errnos.
264 * For them, probably only EWOULDBLOCK needs explaining:
265 * "Other modules depend on us". So far we don't do such
266 * translation and don't use moderror() for removal errors.
267 */
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200268const char* FAST_FUNC moderror(int err)
269{
270 switch (err) {
271 case -1: /* btw: it's -EPERM */
272 return "no such module";
273 case ENOEXEC:
274 return "invalid module format";
275 case ENOENT:
276 return "unknown symbol in module, or unknown parameter";
277 case ESRCH:
278 return "module has wrong symbol version";
279 case ENOSYS:
280 return "kernel does not support requested operation";
281 }
282 if (err < 0) /* should always be */
283 err = -err;
284 return strerror(err);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000285}