blob: d36caaf68e300cdd5670f3445cb353cb40a66814 [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
10#ifdef __UCLIBC__
11extern int init_module(void *module, unsigned long len, const char *options);
12extern int delete_module(const char *module, unsigned int flags);
13#else
14# include <sys/syscall.h>
15# define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
Mike Frysinger3a45b872016-09-15 12:16:33 +020016# if defined(__NR_finit_module)
17# define finit_module(fd, uargs, flags) syscall(__NR_finit_module, fd, uargs, flags)
18# endif
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000019# define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
20#endif
21
Timo Teräs48dc80b2015-11-05 18:54:55 +010022static module_entry *helper_get_module(module_db *db, const char *module, int create)
23{
24 char modname[MODULE_NAME_LEN];
25 struct module_entry *e;
26 unsigned i, hash;
27
28 filename2modname(module, modname);
29
30 hash = 0;
31 for (i = 0; modname[i]; i++)
32 hash = ((hash << 5) + hash) + modname[i];
33 hash %= MODULE_HASH_SIZE;
34
35 for (e = db->buckets[hash]; e; e = e->next)
36 if (strcmp(e->modname, modname) == 0)
37 return e;
38 if (!create)
39 return NULL;
40
41 e = xzalloc(sizeof(*e));
42 e->modname = xstrdup(modname);
43 e->next = db->buckets[hash];
44 db->buckets[hash] = e;
Denys Vlasenko196e4002015-11-06 15:50:28 +010045 IF_DEPMOD(e->dnext = e->dprev = e;)
Timo Teräs48dc80b2015-11-05 18:54:55 +010046
47 return e;
48}
49module_entry* FAST_FUNC moddb_get(module_db *db, const char *module)
50{
51 return helper_get_module(db, module, 0);
52}
53module_entry* FAST_FUNC moddb_get_or_create(module_db *db, const char *module)
54{
55 return helper_get_module(db, module, 1);
56}
57
58void FAST_FUNC moddb_free(module_db *db)
59{
60 module_entry *e, *n;
61 unsigned i;
62
63 for (i = 0; i < MODULE_HASH_SIZE; i++) {
64 for (e = db->buckets[i]; e; e = n) {
65 n = e->next;
66 free(e->name);
67 free(e->modname);
68 free(e);
69 }
70 }
71}
72
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000073void FAST_FUNC replace(char *s, char what, char with)
74{
75 while (*s) {
76 if (what == *s)
77 *s = with;
78 ++s;
79 }
80}
81
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +020082char* FAST_FUNC replace_underscores(char *s)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000083{
84 replace(s, '-', '_');
85 return s;
86}
87
88int FAST_FUNC string_to_llist(char *string, llist_t **llist, const char *delim)
89{
90 char *tok;
91 int len = 0;
92
93 while ((tok = strsep(&string, delim)) != NULL) {
94 if (tok[0] == '\0')
95 continue;
96 llist_add_to_end(llist, xstrdup(tok));
97 len += strlen(tok);
98 }
99 return len;
100}
101
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200102char* FAST_FUNC filename2modname(const char *filename, char *modname)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000103{
Denys Vlasenkocc70b6f2015-01-24 22:30:30 +0100104 char local_modname[MODULE_NAME_LEN];
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000105 int i;
Denys Vlasenko78854522015-01-01 19:02:40 +0100106 const char *from;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000107
108 if (filename == NULL)
109 return NULL;
110 if (modname == NULL)
Denys Vlasenkocc70b6f2015-01-24 22:30:30 +0100111 modname = local_modname;
Denys Vlasenko78854522015-01-01 19:02:40 +0100112 // 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);
116 from = filename;
Denis Vlasenko48637e02009-02-26 12:00:52 +0000117 for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000118 modname[i] = (from[i] == '-') ? '_' : from[i];
Denys Vlasenko16bda3b2009-05-18 13:08:04 +0200119 modname[i] = '\0';
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000120
Denys Vlasenkocc70b6f2015-01-24 22:30:30 +0100121 if (modname == local_modname)
122 return xstrdup(modname);
123
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000124 return modname;
125}
126
Denys Vlasenkoc5830bd2011-02-02 00:00:36 +0100127char* FAST_FUNC parse_cmdline_module_options(char **argv, int quote_spaces)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000128{
129 char *options;
130 int optlen;
131
132 options = xzalloc(1);
133 optlen = 0;
134 while (*++argv) {
Denys Vlasenkoc5830bd2011-02-02 00:00:36 +0100135 const char *fmt;
136 const char *var;
137 const char *val;
138
139 var = *argv;
140 options = xrealloc(options, optlen + 2 + strlen(var) + 2);
141 fmt = "%.*s%s ";
142 val = strchrnul(var, '=');
143 if (quote_spaces) {
144 /*
145 * modprobe (module-init-tools version 3.11.1) compat:
146 * quote only value:
147 * var="val with spaces", not "var=val with spaces"
148 * (note: var *name* is not checked for spaces!)
149 */
150 if (*val) { /* has var=val format. skip '=' */
151 val++;
152 if (strchr(val, ' '))
153 fmt = "%.*s\"%s\" ";
154 }
155 }
156 optlen += sprintf(options + optlen, fmt, (int)(val - var), var, val);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000157 }
Denys Vlasenkoc5830bd2011-02-02 00:00:36 +0100158 /* Remove trailing space. Disabled */
159 /* if (optlen != 0) options[optlen-1] = '\0'; */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000160 return options;
161}
162
Denys Vlasenko77c066e2009-10-25 04:35:22 +0100163#if ENABLE_FEATURE_INSMOD_TRY_MMAP
164void* FAST_FUNC try_to_mmap_module(const char *filename, size_t *image_size_p)
165{
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. */
170 void *image;
171 struct stat st;
172 int fd;
173
174 fd = xopen(filename, O_RDONLY);
175 fstat(fd, &st);
176 image = NULL;
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) {
182 image = NULL;
183 } else if (*(uint32_t*)image != SWAP_BE32(0x7f454C46)) {
184 /* No ELF signature. Compressed module? */
185 munmap(image, image_size);
186 image = NULL;
187 } else {
188 /* Success. Report the size */
189 *image_size_p = image_size;
190 }
191 }
192 close(fd);
193 return image;
194}
195#endif
196
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200197/* Return:
198 * 0 on success,
199 * -errno on open/read error,
200 * errno on init_module() error
201 */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000202int FAST_FUNC bb_init_module(const char *filename, const char *options)
203{
Denys Vlasenko77c066e2009-10-25 04:35:22 +0100204 size_t image_size;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000205 char *image;
Denis Vlasenko41445042009-04-13 20:32:31 +0000206 int rc;
Denys Vlasenko77c066e2009-10-25 04:35:22 +0100207 bool mmaped;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000208
Denis Vlasenko3b5c9b82009-03-13 23:43:26 +0000209 if (!options)
210 options = "";
211
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200212//TODO: audit bb_init_module_24 to match error code convention
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000213#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);
216#endif
217
Mike Frysinger3a45b872016-09-15 12:16:33 +0200218 /*
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.
223 */
224# ifdef __NR_finit_module
225 {
226 int fd = open(filename, O_RDONLY | O_CLOEXEC);
227 if (fd >= 0) {
228 rc = finit_module(fd, options, 0) != 0;
229 close(fd);
230 if (rc == 0)
231 return rc;
232 }
233 }
234# endif
235
Denys Vlasenko77c066e2009-10-25 04:35:22 +0100236 image_size = INT_MAX - 4095;
237 mmaped = 0;
238 image = try_to_mmap_module(filename, &image_size);
239 if (image) {
240 mmaped = 1;
241 } else {
242 errno = ENOMEM; /* may be changed by e.g. open errors below */
243 image = xmalloc_open_zipped_read_close(filename, &image_size);
244 if (!image)
245 return -errno;
246 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000247
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200248 errno = 0;
Denys Vlasenko77c066e2009-10-25 04:35:22 +0100249 init_module(image, image_size, options);
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200250 rc = errno;
Denys Vlasenko77c066e2009-10-25 04:35:22 +0100251 if (mmaped)
252 munmap(image, image_size);
253 else
254 free(image);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000255 return rc;
256}
257
258int FAST_FUNC bb_delete_module(const char *module, unsigned int flags)
259{
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200260 errno = 0;
261 delete_module(module, flags);
262 return errno;
263}
264
Denys Vlasenkocd139742015-10-24 04:17:04 +0200265/* 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.
269 */
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200270const char* FAST_FUNC moderror(int err)
271{
272 switch (err) {
273 case -1: /* btw: it's -EPERM */
274 return "no such module";
275 case ENOEXEC:
276 return "invalid module format";
277 case ENOENT:
278 return "unknown symbol in module, or unknown parameter";
279 case ESRCH:
280 return "module has wrong symbol version";
281 case ENOSYS:
282 return "kernel does not support requested operation";
283 }
284 if (err < 0) /* should always be */
285 err = -err;
286 return strerror(err);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000287}