blob: 4204f06fe1f3e9d5de2bf24cbad340043d5686f1
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 | #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) |
15 | #endif |
16 | #define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags) |
17 | |
18 | static 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; |
41 | IF_DEPMOD(e->dnext = e->dprev = e;) |
42 | |
43 | return e; |
44 | } |
45 | module_entry* FAST_FUNC moddb_get(module_db *db, const char *module) |
46 | { |
47 | return helper_get_module(db, module, 0); |
48 | } |
49 | module_entry* FAST_FUNC moddb_get_or_create(module_db *db, const char *module) |
50 | { |
51 | return helper_get_module(db, module, 1); |
52 | } |
53 | |
54 | void 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 | |
69 | void 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 | |
78 | char* FAST_FUNC replace_underscores(char *s) |
79 | { |
80 | replace(s, '-', '_'); |
81 | return s; |
82 | } |
83 | |
84 | int 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 | |
98 | char* FAST_FUNC filename2modname(const char *filename, char *modname) |
99 | { |
100 | char local_modname[MODULE_NAME_LEN]; |
101 | int i; |
102 | const char *from; |
103 | |
104 | if (filename == NULL) |
105 | return NULL; |
106 | if (modname == NULL) |
107 | modname = local_modname; |
108 | // 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; |
113 | for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++) |
114 | modname[i] = (from[i] == '-') ? '_' : from[i]; |
115 | modname[i] = '\0'; |
116 | |
117 | if (modname == local_modname) |
118 | return xstrdup(modname); |
119 | |
120 | return modname; |
121 | } |
122 | |
123 | char* FAST_FUNC parse_cmdline_module_options(char **argv, int quote_spaces) |
124 | { |
125 | char *options; |
126 | int optlen; |
127 | |
128 | options = xzalloc(1); |
129 | optlen = 0; |
130 | while (*++argv) { |
131 | const char *fmt; |
132 | const char *var; |
133 | const char *val; |
134 | |
135 | var = *argv; |
136 | options = xrealloc(options, optlen + 2 + strlen(var) + 2); |
137 | fmt = "%.*s%s "; |
138 | val = strchrnul(var, '='); |
139 | if (quote_spaces) { |
140 | /* |
141 | * modprobe (module-init-tools version 3.11.1) compat: |
142 | * quote only value: |
143 | * var="val with spaces", not "var=val with spaces" |
144 | * (note: var *name* is not checked for spaces!) |
145 | */ |
146 | if (*val) { /* has var=val format. skip '=' */ |
147 | val++; |
148 | if (strchr(val, ' ')) |
149 | fmt = "%.*s\"%s\" "; |
150 | } |
151 | } |
152 | optlen += sprintf(options + optlen, fmt, (int)(val - var), var, val); |
153 | } |
154 | /* Remove trailing space. Disabled */ |
155 | /* if (optlen != 0) options[optlen-1] = '\0'; */ |
156 | return options; |
157 | } |
158 | |
159 | #if ENABLE_FEATURE_INSMOD_TRY_MMAP |
160 | void* FAST_FUNC try_to_mmap_module(const char *filename, size_t *image_size_p) |
161 | { |
162 | /* We have user reports of failure to load 3MB module |
163 | * on a 16MB RAM machine. Apparently even a transient |
164 | * memory spike to 6MB during module load |
165 | * is too big for that system. */ |
166 | void *image; |
167 | struct stat st; |
168 | int fd; |
169 | |
170 | fd = xopen(filename, O_RDONLY); |
171 | fstat(fd, &st); |
172 | image = NULL; |
173 | /* st.st_size is off_t, we can't just pass it to mmap */ |
174 | if (st.st_size <= *image_size_p) { |
175 | size_t image_size = st.st_size; |
176 | image = mmap(NULL, image_size, PROT_READ, MAP_PRIVATE, fd, 0); |
177 | if (image == MAP_FAILED) { |
178 | image = NULL; |
179 | } else if (*(uint32_t*)image != SWAP_BE32(0x7f454C46)) { |
180 | /* No ELF signature. Compressed module? */ |
181 | munmap(image, image_size); |
182 | image = NULL; |
183 | } else { |
184 | /* Success. Report the size */ |
185 | *image_size_p = image_size; |
186 | } |
187 | } |
188 | close(fd); |
189 | return image; |
190 | } |
191 | #endif |
192 | |
193 | /* Return: |
194 | * 0 on success, |
195 | * -errno on open/read error, |
196 | * errno on init_module() error |
197 | */ |
198 | int FAST_FUNC bb_init_module(const char *filename, const char *options) |
199 | { |
200 | size_t image_size; |
201 | char *image; |
202 | int rc; |
203 | bool mmaped; |
204 | |
205 | if (!options) |
206 | options = ""; |
207 | |
208 | //TODO: audit bb_init_module_24 to match error code convention |
209 | #if ENABLE_FEATURE_2_4_MODULES |
210 | if (get_linux_version_code() < KERNEL_VERSION(2,6,0)) |
211 | return bb_init_module_24(filename, options); |
212 | #endif |
213 | |
214 | /* |
215 | * First we try finit_module if available. Some kernels are configured |
216 | * to only allow loading of modules off of secure storage (like a read- |
217 | * only rootfs) which needs the finit_module call. If it fails, we fall |
218 | * back to normal module loading to support compressed modules. |
219 | */ |
220 | # ifdef __NR_finit_module |
221 | { |
222 | int fd = open(filename, O_RDONLY | O_CLOEXEC); |
223 | if (fd >= 0) { |
224 | rc = finit_module(fd, options, 0) != 0; |
225 | close(fd); |
226 | if (rc == 0) |
227 | return rc; |
228 | } |
229 | } |
230 | # endif |
231 | |
232 | image_size = INT_MAX - 4095; |
233 | mmaped = 0; |
234 | image = try_to_mmap_module(filename, &image_size); |
235 | if (image) { |
236 | mmaped = 1; |
237 | } else { |
238 | errno = ENOMEM; /* may be changed by e.g. open errors below */ |
239 | image = xmalloc_open_zipped_read_close(filename, &image_size); |
240 | if (!image) |
241 | return -errno; |
242 | } |
243 | |
244 | errno = 0; |
245 | init_module(image, image_size, options); |
246 | rc = errno; |
247 | if (mmaped) |
248 | munmap(image, image_size); |
249 | else |
250 | free(image); |
251 | return rc; |
252 | } |
253 | |
254 | int FAST_FUNC bb_delete_module(const char *module, unsigned int flags) |
255 | { |
256 | errno = 0; |
257 | delete_module(module, flags); |
258 | return errno; |
259 | } |
260 | |
261 | /* Note: not suitable for delete_module() errnos. |
262 | * For them, probably only EWOULDBLOCK needs explaining: |
263 | * "Other modules depend on us". So far we don't do such |
264 | * translation and don't use moderror() for removal errors. |
265 | */ |
266 | const char* FAST_FUNC moderror(int err) |
267 | { |
268 | switch (err) { |
269 | case -1: /* btw: it's -EPERM */ |
270 | return "no such module"; |
271 | case ENOEXEC: |
272 | return "invalid module format"; |
273 | case ENOENT: |
274 | return "unknown symbol in module, or unknown parameter"; |
275 | case ESRCH: |
276 | return "module has wrong symbol version"; |
277 | case ENOSYS: |
278 | return "kernel does not support requested operation"; |
279 | } |
280 | if (err < 0) /* should always be */ |
281 | err = -err; |
282 | return strerror(err); |
283 | } |
284 |