blob: d36caaf68e300cdd5670f3445cb353cb40a66814
1 | /* |
2 | * Common modutils related functions for busybox |
3 | * |
4 | * Copyright (C) 2008 by Timo Teras <timo.teras@iki.fi> |
5 | * |
6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
7 | */ |
8 | #include "modutils.h" |
9 | |
10 | #ifdef __UCLIBC__ |
11 | extern int init_module(void *module, unsigned long len, const char *options); |
12 | extern 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) |
16 | # if defined(__NR_finit_module) |
17 | # define finit_module(fd, uargs, flags) syscall(__NR_finit_module, fd, uargs, flags) |
18 | # endif |
19 | # define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags) |
20 | #endif |
21 | |
22 | static 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; |
45 | IF_DEPMOD(e->dnext = e->dprev = e;) |
46 | |
47 | return e; |
48 | } |
49 | module_entry* FAST_FUNC moddb_get(module_db *db, const char *module) |
50 | { |
51 | return helper_get_module(db, module, 0); |
52 | } |
53 | module_entry* FAST_FUNC moddb_get_or_create(module_db *db, const char *module) |
54 | { |
55 | return helper_get_module(db, module, 1); |
56 | } |
57 | |
58 | void 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 | |
73 | void 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 | |
82 | char* FAST_FUNC replace_underscores(char *s) |
83 | { |
84 | replace(s, '-', '_'); |
85 | return s; |
86 | } |
87 | |
88 | int 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 | |
102 | char* FAST_FUNC filename2modname(const char *filename, char *modname) |
103 | { |
104 | char local_modname[MODULE_NAME_LEN]; |
105 | int i; |
106 | const char *from; |
107 | |
108 | if (filename == NULL) |
109 | return NULL; |
110 | if (modname == 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); |
116 | from = filename; |
117 | for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++) |
118 | modname[i] = (from[i] == '-') ? '_' : from[i]; |
119 | modname[i] = '\0'; |
120 | |
121 | if (modname == local_modname) |
122 | return xstrdup(modname); |
123 | |
124 | return modname; |
125 | } |
126 | |
127 | char* FAST_FUNC parse_cmdline_module_options(char **argv, int quote_spaces) |
128 | { |
129 | char *options; |
130 | int optlen; |
131 | |
132 | options = xzalloc(1); |
133 | optlen = 0; |
134 | while (*++argv) { |
135 | 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); |
157 | } |
158 | /* Remove trailing space. Disabled */ |
159 | /* if (optlen != 0) options[optlen-1] = '\0'; */ |
160 | return options; |
161 | } |
162 | |
163 | #if ENABLE_FEATURE_INSMOD_TRY_MMAP |
164 | void* 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 | |
197 | /* Return: |
198 | * 0 on success, |
199 | * -errno on open/read error, |
200 | * errno on init_module() error |
201 | */ |
202 | int FAST_FUNC bb_init_module(const char *filename, const char *options) |
203 | { |
204 | size_t image_size; |
205 | char *image; |
206 | int rc; |
207 | bool mmaped; |
208 | |
209 | if (!options) |
210 | options = ""; |
211 | |
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); |
216 | #endif |
217 | |
218 | /* |
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 | |
236 | 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 | } |
247 | |
248 | errno = 0; |
249 | init_module(image, image_size, options); |
250 | rc = errno; |
251 | if (mmaped) |
252 | munmap(image, image_size); |
253 | else |
254 | free(image); |
255 | return rc; |
256 | } |
257 | |
258 | int FAST_FUNC bb_delete_module(const char *module, unsigned int flags) |
259 | { |
260 | errno = 0; |
261 | delete_module(module, flags); |
262 | return errno; |
263 | } |
264 | |
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. |
269 | */ |
270 | const 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); |
287 | } |
288 |