blob: a94b0b9a6b18bdf7e8b3d13a23e3083b7e47df2b [file] [log] [blame]
Denis Vlasenko671691c2008-07-04 10:25:44 +00001/* vi: set sw=4 ts=4: */
2/*
3 * simplified modprobe
4 *
5 * Copyright (c) 2008 Vladimir Dronnikov
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00006 * Copyright (c) 2008 Bernhard Reutner-Fischer (initial depmod code)
Denis Vlasenko671691c2008-07-04 10:25:44 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko671691c2008-07-04 10:25:44 +00009 */
Denys Vlasenko326edc32016-12-22 14:36:49 +010010
Kang-Che Sung415cc422017-02-01 19:43:54 +080011/* modprobe-small configs are defined in Config.src to ensure better
12 * "make config" order */
Denis Vlasenko671691c2008-07-04 10:25:44 +000013
Denys Vlasenko3346b4a2017-08-04 02:56:39 +020014//applet:IF_LSMOD( IF_MODPROBE_SMALL(APPLET_NOEXEC( lsmod, lsmod, BB_DIR_SBIN, BB_SUID_DROP, lsmod )))
15//applet:IF_MODPROBE(IF_MODPROBE_SMALL(APPLET_NOEXEC( modprobe, modprobe, BB_DIR_SBIN, BB_SUID_DROP, modprobe)))
16// APPLET_ODDNAME:name main location suid_type help
17//applet:IF_DEPMOD( IF_MODPROBE_SMALL(APPLET_ODDNAME(depmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, depmod )))
18//applet:IF_INSMOD( IF_MODPROBE_SMALL(APPLET_NOEXEC( insmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, insmod )))
19//applet:IF_RMMOD( IF_MODPROBE_SMALL(APPLET_NOEXEC( rmmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, rmmod )))
20/* noexec speeds up boot with many modules loaded (need SH_STANDALONE=y) */
21/* I measured about ~5 times faster insmod */
22/* depmod is not noexec, it runs longer and benefits from memory trimming via exec */
Denys Vlasenkoc15613c2010-10-15 11:29:02 +020023
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010024//kbuild:lib-$(CONFIG_MODPROBE_SMALL) += modprobe-small.o
25
Denis Vlasenko671691c2008-07-04 10:25:44 +000026#include "libbb.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +020027/* After libbb.h, since it needs sys/types.h on some systems */
Denis Vlasenko671691c2008-07-04 10:25:44 +000028#include <sys/utsname.h> /* uname() */
29#include <fnmatch.h>
Mike Frysinger3a45b872016-09-15 12:16:33 +020030#include <sys/syscall.h>
Denis Vlasenko671691c2008-07-04 10:25:44 +000031
Waldemar Brodkorb9c083f52016-12-26 20:07:59 +010032#define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
33#define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
Mike Frysinger3a45b872016-09-15 12:16:33 +020034#ifdef __NR_finit_module
35# define finit_module(fd, uargs, flags) syscall(__NR_finit_module, fd, uargs, flags)
36#endif
Denys Vlasenko07e55552014-04-21 16:59:36 +020037/* linux/include/linux/module.h has limit of 64 chars on module names */
38#undef MODULE_NAME_LEN
39#define MODULE_NAME_LEN 64
Denis Vlasenko24a131e2008-07-09 15:30:57 +000040
41
Denys Vlasenko5b3151c2010-09-25 14:37:06 +020042#if 1
43# define dbg1_error_msg(...) ((void)0)
44# define dbg2_error_msg(...) ((void)0)
45#else
46# define dbg1_error_msg(...) bb_error_msg(__VA_ARGS__)
47# define dbg2_error_msg(...) bb_error_msg(__VA_ARGS__)
48#endif
Denis Vlasenko671691c2008-07-04 10:25:44 +000049
Denis Vlasenko24a131e2008-07-09 15:30:57 +000050#define DEPFILE_BB CONFIG_DEFAULT_DEPMOD_FILE".bb"
Denis Vlasenko671691c2008-07-04 10:25:44 +000051
Kang-Che Sungfdfd7162017-01-31 17:06:43 +080052//usage:#if ENABLE_MODPROBE_SMALL
53
54//usage:#define lsmod_trivial_usage
55//usage: ""
56//usage:#define lsmod_full_usage "\n\n"
57//usage: "List loaded kernel modules"
58
59//usage:#endif
60
61#if ENABLE_LSMOD
62int lsmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
63int lsmod_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
64{
65 xprint_and_close_file(xfopen_for_read("/proc/modules"));
66 return EXIT_SUCCESS;
67}
68#endif
69
70/* Num of applets that use modprobe_main() entry point. */
71/* lsmod is not here. */
72#define MOD_APPLET_CNT (ENABLE_MODPROBE + ENABLE_DEPMOD + ENABLE_INSMOD + ENABLE_RMMOD)
Denys Vlasenkoe7b54d02017-01-09 16:31:21 +010073
74/* Do not bother if MODPROBE_SMALL=y but no applets selected. */
75/* The rest of the file is in this if block. */
76#if MOD_APPLET_CNT > 0
77
78#define ONLY_APPLET (MOD_APPLET_CNT == 1)
Explorer0980172e42017-01-09 15:04:43 +080079#define is_modprobe (ENABLE_MODPROBE && (ONLY_APPLET || applet_name[0] == 'm'))
80#define is_depmod (ENABLE_DEPMOD && (ONLY_APPLET || applet_name[0] == 'd'))
81#define is_insmod (ENABLE_INSMOD && (ONLY_APPLET || applet_name[0] == 'i'))
Explorer0980172e42017-01-09 15:04:43 +080082#define is_rmmod (ENABLE_RMMOD && (ONLY_APPLET || applet_name[0] == 'r'))
83
Denis Vlasenko671691c2008-07-04 10:25:44 +000084enum {
Kang-Che Sungaf316aa2017-02-09 22:48:52 +080085 DEPMOD_OPT_n = (1 << 0), /* dry-run, print to stdout */
Denis Vlasenko671691c2008-07-04 10:25:44 +000086 OPT_q = (1 << 0), /* be quiet */
87 OPT_r = (1 << 1), /* module removal instead of loading */
88};
89
90typedef struct module_info {
91 char *pathname;
Denis Vlasenko78436992008-07-10 14:14:20 +000092 char *aliases;
93 char *deps;
Denys Vlasenko5c3e0602016-06-20 10:59:06 +020094 smallint open_read_failed;
Denis Vlasenko671691c2008-07-04 10:25:44 +000095} module_info;
96
97/*
98 * GLOBALS
99 */
100struct globals {
101 module_info *modinfo;
102 char *module_load_options;
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000103 smallint dep_bb_seen;
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000104 smallint wrote_dep_bb_ok;
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200105 unsigned module_count;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000106 int module_found_idx;
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200107 unsigned stringbuf_idx;
108 unsigned stringbuf_size;
109 char *stringbuf; /* some modules have lots of stuff */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000110 /* for example, drivers/media/video/saa7134/saa7134.ko */
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200111 /* therefore having a fixed biggish buffer is not wise */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000112};
113#define G (*ptr_to_globals)
114#define modinfo (G.modinfo )
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000115#define dep_bb_seen (G.dep_bb_seen )
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000116#define wrote_dep_bb_ok (G.wrote_dep_bb_ok )
Denis Vlasenko671691c2008-07-04 10:25:44 +0000117#define module_count (G.module_count )
118#define module_found_idx (G.module_found_idx )
119#define module_load_options (G.module_load_options)
120#define stringbuf_idx (G.stringbuf_idx )
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200121#define stringbuf_size (G.stringbuf_size )
Denis Vlasenko671691c2008-07-04 10:25:44 +0000122#define stringbuf (G.stringbuf )
123#define INIT_G() do { \
124 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
125} while (0)
126
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200127static void append(const char *s)
128{
129 unsigned len = strlen(s);
130 if (stringbuf_idx + len + 15 > stringbuf_size) {
131 stringbuf_size = stringbuf_idx + len + 127;
132 dbg2_error_msg("grow stringbuf to %u", stringbuf_size);
133 stringbuf = xrealloc(stringbuf, stringbuf_size);
134 }
135 memcpy(stringbuf + stringbuf_idx, s, len);
136 stringbuf_idx += len;
137}
Denis Vlasenko671691c2008-07-04 10:25:44 +0000138
139static void appendc(char c)
140{
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200141 /* We appendc() only after append(), + 15 trick in append()
142 * makes it unnecessary to check for overflow here */
143 stringbuf[stringbuf_idx++] = c;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000144}
145
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000146static void bksp(void)
147{
148 if (stringbuf_idx)
149 stringbuf_idx--;
150}
151
Denis Vlasenko671691c2008-07-04 10:25:44 +0000152static void reset_stringbuf(void)
153{
154 stringbuf_idx = 0;
155}
156
157static char* copy_stringbuf(void)
158{
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200159 char *copy = xzalloc(stringbuf_idx + 1); /* terminating NUL */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000160 return memcpy(copy, stringbuf, stringbuf_idx);
161}
162
163static char* find_keyword(char *ptr, size_t len, const char *word)
164{
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000165 if (!ptr) /* happens if xmalloc_open_zipped_read_close cannot read it */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000166 return NULL;
167
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100168 len -= strlen(word) - 1;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000169 while ((ssize_t)len > 0) {
170 char *old = ptr;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100171 char *after_word;
172
Denis Vlasenko671691c2008-07-04 10:25:44 +0000173 /* search for the first char in word */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100174 ptr = memchr(ptr, word[0], len);
Denys Vlasenko10ad6222017-04-17 16:13:32 +0200175 if (ptr == NULL) /* no occurrence left, done */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000176 break;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100177 after_word = is_prefixed_with(ptr, word);
178 if (after_word)
179 return after_word; /* found, return ptr past it */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000180 ++ptr;
181 len -= (ptr - old);
182 }
183 return NULL;
184}
185
186static void replace(char *s, char what, char with)
187{
188 while (*s) {
189 if (what == *s)
190 *s = with;
191 ++s;
192 }
193}
194
Denys Vlasenko07e55552014-04-21 16:59:36 +0200195static char *filename2modname(const char *filename, char *modname)
196{
197 int i;
Denys Vlasenko78854522015-01-01 19:02:40 +0100198 const char *from;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200199
Denys Vlasenko78854522015-01-01 19:02:40 +0100200 // Disabled since otherwise "modprobe dir/name" would work
201 // as if it is "modprobe name". It is unclear why
202 // 'basenamization' was here in the first place.
203 //from = bb_get_last_path_component_nostrip(filename);
204 from = filename;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200205 for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++)
206 modname[i] = (from[i] == '-') ? '_' : from[i];
207 modname[i] = '\0';
208
209 return modname;
210}
211
Denys Vlasenko1b671532015-01-11 17:46:56 +0100212static int pathname_matches_modname(const char *pathname, const char *modname)
213{
214 int r;
215 char name[MODULE_NAME_LEN];
216 filename2modname(bb_get_last_path_component_nostrip(pathname), name);
217 r = (strcmp(name, modname) == 0);
218 return r;
219}
220
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000221/* Take "word word", return malloced "word",NUL,"word",NUL,NUL */
222static char* str_2_list(const char *str)
223{
224 int len = strlen(str) + 1;
225 char *dst = xmalloc(len + 1);
226
227 dst[len] = '\0';
228 memcpy(dst, str, len);
229//TODO: protect against 2+ spaces: "word word"
230 replace(dst, ' ', '\0');
231 return dst;
232}
233
Denis Vlasenko671691c2008-07-04 10:25:44 +0000234/* We use error numbers in a loose translation... */
235static const char *moderror(int err)
236{
237 switch (err) {
238 case ENOEXEC:
239 return "invalid module format";
240 case ENOENT:
241 return "unknown symbol in module or invalid parameter";
242 case ESRCH:
243 return "module has wrong symbol version";
244 case EINVAL: /* "invalid parameter" */
245 return "unknown symbol in module or invalid parameter"
246 + sizeof("unknown symbol in module or");
247 default:
248 return strerror(err);
249 }
250}
251
252static int load_module(const char *fname, const char *options)
253{
254#if 1
255 int r;
256 size_t len = MAXINT(ssize_t);
257 char *module_image;
Mike Frysinger3a45b872016-09-15 12:16:33 +0200258
259 if (!options)
260 options = "";
261
Denis Vlasenko671691c2008-07-04 10:25:44 +0000262 dbg1_error_msg("load_module('%s','%s')", fname, options);
263
Mike Frysinger3a45b872016-09-15 12:16:33 +0200264 /*
265 * First we try finit_module if available. Some kernels are configured
266 * to only allow loading of modules off of secure storage (like a read-
267 * only rootfs) which needs the finit_module call. If it fails, we fall
268 * back to normal module loading to support compressed modules.
269 */
270 r = 1;
271# ifdef __NR_finit_module
272 {
273 int fd = open(fname, O_RDONLY | O_CLOEXEC);
274 if (fd >= 0) {
275 r = finit_module(fd, options, 0) != 0;
276 close(fd);
277 }
278 }
279# endif
280 if (r != 0) {
281 module_image = xmalloc_open_zipped_read_close(fname, &len);
282 r = (!module_image || init_module(module_image, len, options) != 0);
283 free(module_image);
284 }
285
Denis Vlasenko671691c2008-07-04 10:25:44 +0000286 dbg1_error_msg("load_module:%d", r);
287 return r; /* 0 = success */
288#else
289 /* For testing */
290 dbg1_error_msg("load_module('%s','%s')", fname, options);
291 return 1;
292#endif
293}
294
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200295/* Returns !0 if open/read was unsuccessful */
296static int parse_module(module_info *info, const char *pathname)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000297{
298 char *module_image;
299 char *ptr;
300 size_t len;
301 size_t pos;
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000302 dbg1_error_msg("parse_module('%s')", pathname);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000303
304 /* Read (possibly compressed) module */
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200305 errno = 0;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000306 len = 64 * 1024 * 1024; /* 64 Mb at most */
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000307 module_image = xmalloc_open_zipped_read_close(pathname, &len);
Denys Vlasenko094cc512011-01-17 14:58:27 +0100308 /* module_image == NULL is ok here, find_keyword handles it */
Denis Vlasenko78436992008-07-10 14:14:20 +0000309//TODO: optimize redundant module body reads
Denis Vlasenko671691c2008-07-04 10:25:44 +0000310
Denis Vlasenko78436992008-07-10 14:14:20 +0000311 /* "alias1 symbol:sym1 alias2 symbol:sym2" */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000312 reset_stringbuf();
Denis Vlasenko671691c2008-07-04 10:25:44 +0000313 pos = 0;
314 while (1) {
Denys Vlasenko6116cb22014-04-19 16:17:27 +0200315 unsigned start = stringbuf_idx;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000316 ptr = find_keyword(module_image + pos, len - pos, "alias=");
317 if (!ptr) {
318 ptr = find_keyword(module_image + pos, len - pos, "__ksymtab_");
319 if (!ptr)
320 break;
321 /* DOCME: __ksymtab_gpl and __ksymtab_strings occur
322 * in many modules. What do they mean? */
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000323 if (strcmp(ptr, "gpl") == 0 || strcmp(ptr, "strings") == 0)
324 goto skip;
325 dbg2_error_msg("alias:'symbol:%s'", ptr);
326 append("symbol:");
Denis Vlasenko671691c2008-07-04 10:25:44 +0000327 } else {
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000328 dbg2_error_msg("alias:'%s'", ptr);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000329 }
330 append(ptr);
331 appendc(' ');
Denys Vlasenko6116cb22014-04-19 16:17:27 +0200332 /*
333 * Don't add redundant aliases, such as:
334 * libcrc32c.ko symbol:crc32c symbol:crc32c
335 */
336 if (start) { /* "if we aren't the first alias" */
337 char *found, *last;
338 stringbuf[stringbuf_idx] = '\0';
339 last = stringbuf + start;
340 /*
341 * String at last-1 is " symbol:crc32c "
342 * (with both leading and trailing spaces).
343 */
344 if (strncmp(stringbuf, last, stringbuf_idx - start) == 0)
345 /* First alias matches us */
346 found = stringbuf;
347 else
348 /* Does any other alias match? */
349 found = strstr(stringbuf, last-1);
350 if (found < last-1) {
351 /* There is absolutely the same string before us */
352 dbg2_error_msg("redundant:'%s'", last);
353 stringbuf_idx = start;
354 goto skip;
355 }
356 }
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000357 skip:
Denis Vlasenko671691c2008-07-04 10:25:44 +0000358 pos = (ptr - module_image);
359 }
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000360 bksp(); /* remove last ' ' */
Denis Vlasenko78436992008-07-10 14:14:20 +0000361 info->aliases = copy_stringbuf();
Denys Vlasenkof9c814b2009-09-07 02:37:19 +0200362 replace(info->aliases, '-', '_');
Denis Vlasenko671691c2008-07-04 10:25:44 +0000363
Denis Vlasenko78436992008-07-10 14:14:20 +0000364 /* "dependency1 depandency2" */
365 reset_stringbuf();
Denis Vlasenko671691c2008-07-04 10:25:44 +0000366 ptr = find_keyword(module_image, len, "depends=");
367 if (ptr && *ptr) {
368 replace(ptr, ',', ' ');
369 replace(ptr, '-', '_');
370 dbg2_error_msg("dep:'%s'", ptr);
371 append(ptr);
372 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200373 free(module_image);
Denis Vlasenko78436992008-07-10 14:14:20 +0000374 info->deps = copy_stringbuf();
Denis Vlasenko671691c2008-07-04 10:25:44 +0000375
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200376 info->open_read_failed = (module_image == NULL);
377 return info->open_read_failed;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000378}
379
Denis Vlasenko671691c2008-07-04 10:25:44 +0000380static FAST_FUNC int fileAction(const char *pathname,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000381 struct stat *sb UNUSED_PARAM,
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000382 void *modname_to_match,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000383 int depth UNUSED_PARAM)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000384{
385 int cur;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000386 const char *fname;
Explorer0980172e42017-01-09 15:04:43 +0800387 bool is_remove = (ENABLE_RMMOD && ONLY_APPLET)
388 || ((ENABLE_RMMOD || ENABLE_MODPROBE) && (option_mask32 & OPT_r));
Denis Vlasenko671691c2008-07-04 10:25:44 +0000389
390 pathname += 2; /* skip "./" */
391 fname = bb_get_last_path_component_nostrip(pathname);
392 if (!strrstr(fname, ".ko")) {
393 dbg1_error_msg("'%s' is not a module", pathname);
394 return TRUE; /* not a module, continue search */
395 }
396
397 cur = module_count++;
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000398 modinfo = xrealloc_vector(modinfo, 12, cur);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000399 modinfo[cur].pathname = xstrdup(pathname);
Denis Vlasenko27842282008-08-04 13:20:36 +0000400 /*modinfo[cur].aliases = NULL; - xrealloc_vector did it */
401 /*modinfo[cur+1].pathname = NULL;*/
Denis Vlasenko671691c2008-07-04 10:25:44 +0000402
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000403 if (!pathname_matches_modname(fname, modname_to_match)) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000404 dbg1_error_msg("'%s' module name doesn't match", pathname);
405 return TRUE; /* module name doesn't match, continue search */
406 }
407
408 dbg1_error_msg("'%s' module name matches", pathname);
409 module_found_idx = cur;
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200410 if (parse_module(&modinfo[cur], pathname) != 0)
411 return TRUE; /* failed to open/read it, no point in trying loading */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000412
Explorer0980172e42017-01-09 15:04:43 +0800413 if (!is_remove) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000414 if (load_module(pathname, module_load_options) == 0) {
415 /* Load was successful, there is nothing else to do.
416 * This can happen ONLY for "top-level" module load,
Denys Vlasenko10ad6222017-04-17 16:13:32 +0200417 * not a dep, because deps don't do dirscan. */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000418 exit(EXIT_SUCCESS);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000419 }
420 }
421
Denis Vlasenko671691c2008-07-04 10:25:44 +0000422 return TRUE;
423}
424
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000425static int load_dep_bb(void)
Denis Vlasenko78436992008-07-10 14:14:20 +0000426{
427 char *line;
Denis Vlasenko5415c852008-07-21 23:05:26 +0000428 FILE *fp = fopen_for_read(DEPFILE_BB);
Denis Vlasenko78436992008-07-10 14:14:20 +0000429
430 if (!fp)
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000431 return 0;
432
433 dep_bb_seen = 1;
434 dbg1_error_msg("loading "DEPFILE_BB);
435
436 /* Why? There is a rare scenario: we did not find modprobe.dep.bb,
437 * we scanned the dir and found no module by name, then we search
438 * for alias (full scan), and we decided to generate modprobe.dep.bb.
439 * But we see modprobe.dep.bb.new! Other modprobe is at work!
440 * We wait and other modprobe renames it to modprobe.dep.bb.
441 * Now we can use it.
442 * But we already have modinfo[] filled, and "module_count = 0"
443 * makes us start anew. Yes, we leak modinfo[].xxx pointers -
444 * there is not much of data there anyway. */
445 module_count = 0;
446 memset(&modinfo[0], 0, sizeof(modinfo[0]));
Denis Vlasenko78436992008-07-10 14:14:20 +0000447
448 while ((line = xmalloc_fgetline(fp)) != NULL) {
449 char* space;
Denys Vlasenko90a99042009-09-06 02:36:23 +0200450 char* linebuf;
Denis Vlasenko78436992008-07-10 14:14:20 +0000451 int cur;
452
453 if (!line[0]) {
454 free(line);
455 continue;
456 }
457 space = strchrnul(line, ' ');
458 cur = module_count++;
459 modinfo = xrealloc_vector(modinfo, 12, cur);
Denis Vlasenko27842282008-08-04 13:20:36 +0000460 /*modinfo[cur+1].pathname = NULL; - xrealloc_vector did it */
Denis Vlasenko78436992008-07-10 14:14:20 +0000461 modinfo[cur].pathname = line; /* we take ownership of malloced block here */
462 if (*space)
463 *space++ = '\0';
464 modinfo[cur].aliases = space;
Denys Vlasenko90a99042009-09-06 02:36:23 +0200465 linebuf = xmalloc_fgetline(fp);
466 modinfo[cur].deps = linebuf ? linebuf : xzalloc(1);
Denis Vlasenko78436992008-07-10 14:14:20 +0000467 if (modinfo[cur].deps[0]) {
468 /* deps are not "", so next line must be empty */
469 line = xmalloc_fgetline(fp);
470 /* Refuse to work with damaged config file */
471 if (line && line[0])
472 bb_error_msg_and_die("error in %s at '%s'", DEPFILE_BB, line);
473 free(line);
474 }
475 }
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000476 return 1;
477}
478
479static int start_dep_bb_writeout(void)
480{
481 int fd;
482
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000483 /* depmod -n: write result to stdout */
Kang-Che Sungaf316aa2017-02-09 22:48:52 +0800484 if (is_depmod && (option_mask32 & DEPMOD_OPT_n))
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000485 return STDOUT_FILENO;
486
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000487 fd = open(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0644);
488 if (fd < 0) {
489 if (errno == EEXIST) {
490 int count = 5 * 20;
491 dbg1_error_msg(DEPFILE_BB".new exists, waiting for "DEPFILE_BB);
492 while (1) {
493 usleep(1000*1000 / 20);
494 if (load_dep_bb()) {
495 dbg1_error_msg(DEPFILE_BB" appeared");
496 return -2; /* magic number */
497 }
498 if (!--count)
499 break;
500 }
501 bb_error_msg("deleting stale %s", DEPFILE_BB".new");
502 fd = open_or_warn(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC);
503 }
504 }
505 dbg1_error_msg("opened "DEPFILE_BB".new:%d", fd);
506 return fd;
507}
508
509static void write_out_dep_bb(int fd)
510{
511 int i;
512 FILE *fp;
513
514 /* We want good error reporting. fdprintf is not good enough. */
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +0100515 fp = xfdopen_for_write(fd);
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000516 i = 0;
517 while (modinfo[i].pathname) {
518 fprintf(fp, "%s%s%s\n" "%s%s\n",
519 modinfo[i].pathname, modinfo[i].aliases[0] ? " " : "", modinfo[i].aliases,
520 modinfo[i].deps, modinfo[i].deps[0] ? "\n" : "");
521 i++;
522 }
523 /* Badly formatted depfile is a no-no. Be paranoid. */
524 errno = 0;
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000525 if (ferror(fp) | fclose(fp)) /* | instead of || is intended */
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000526 goto err;
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000527
528 if (fd == STDOUT_FILENO) /* it was depmod -n */
529 goto ok;
530
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000531 if (rename(DEPFILE_BB".new", DEPFILE_BB) != 0) {
532 err:
Denys Vlasenko651a2692010-03-23 16:25:17 +0100533 bb_perror_msg("can't create '%s'", DEPFILE_BB);
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000534 unlink(DEPFILE_BB".new");
535 } else {
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000536 ok:
537 wrote_dep_bb_ok = 1;
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000538 dbg1_error_msg("created "DEPFILE_BB);
539 }
Denis Vlasenko78436992008-07-10 14:14:20 +0000540}
541
Denys Vlasenko07e55552014-04-21 16:59:36 +0200542static module_info** find_alias(const char *alias)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000543{
544 int i;
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000545 int dep_bb_fd;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200546 int infoidx;
547 module_info **infovec;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000548 dbg1_error_msg("find_alias('%s')", alias);
549
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000550 try_again:
Denis Vlasenko671691c2008-07-04 10:25:44 +0000551 /* First try to find by name (cheaper) */
552 i = 0;
553 while (modinfo[i].pathname) {
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000554 if (pathname_matches_modname(modinfo[i].pathname, alias)) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000555 dbg1_error_msg("found '%s' in module '%s'",
556 alias, modinfo[i].pathname);
Denis Vlasenko78436992008-07-10 14:14:20 +0000557 if (!modinfo[i].aliases) {
558 parse_module(&modinfo[i], modinfo[i].pathname);
559 }
Denys Vlasenko07e55552014-04-21 16:59:36 +0200560 infovec = xzalloc(2 * sizeof(infovec[0]));
561 infovec[0] = &modinfo[i];
562 return infovec;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000563 }
Denis Vlasenko671691c2008-07-04 10:25:44 +0000564 i++;
565 }
566
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000567 /* Ok, we definitely have to scan module bodies. This is a good
568 * moment to generate modprobe.dep.bb, if it does not exist yet */
569 dep_bb_fd = dep_bb_seen ? -1 : start_dep_bb_writeout();
570 if (dep_bb_fd == -2) /* modprobe.dep.bb appeared? */
571 goto try_again;
572
Denis Vlasenko671691c2008-07-04 10:25:44 +0000573 /* Scan all module bodies, extract modinfo (it contains aliases) */
574 i = 0;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200575 infoidx = 0;
576 infovec = NULL;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000577 while (modinfo[i].pathname) {
578 char *desc, *s;
Denis Vlasenko78436992008-07-10 14:14:20 +0000579 if (!modinfo[i].aliases) {
580 parse_module(&modinfo[i], modinfo[i].pathname);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000581 }
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000582 /* "alias1 symbol:sym1 alias2 symbol:sym2" */
Denis Vlasenko78436992008-07-10 14:14:20 +0000583 desc = str_2_list(modinfo[i].aliases);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000584 /* Does matching substring exist? */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000585 for (s = desc; *s; s += strlen(s) + 1) {
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000586 /* Aliases in module bodies can be defined with
Denis Vlasenko58f59a22008-07-06 11:52:23 +0000587 * shell patterns. Example:
588 * "pci:v000010DEd000000D9sv*sd*bc*sc*i*".
589 * Plain strcmp() won't catch that */
590 if (fnmatch(s, alias, 0) == 0) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000591 dbg1_error_msg("found alias '%s' in module '%s'",
592 alias, modinfo[i].pathname);
Denys Vlasenko07e55552014-04-21 16:59:36 +0200593 infovec = xrealloc_vector(infovec, 1, infoidx);
594 infovec[infoidx++] = &modinfo[i];
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000595 break;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000596 }
597 }
598 free(desc);
599 i++;
600 }
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000601
602 /* Create module.dep.bb if needed */
603 if (dep_bb_fd >= 0) {
604 write_out_dep_bb(dep_bb_fd);
605 }
606
Denys Vlasenko07e55552014-04-21 16:59:36 +0200607 dbg1_error_msg("find_alias '%s' returns %d results", alias, infoidx);
608 return infovec;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000609}
610
611#if ENABLE_FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000612// TODO: open only once, invent config_rewind()
Denis Vlasenko671691c2008-07-04 10:25:44 +0000613static int already_loaded(const char *name)
614{
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100615 int ret;
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100616 char *line;
617 FILE *fp;
618
619 ret = 5 * 2;
620 again:
621 fp = fopen_for_read("/proc/modules");
622 if (!fp)
623 return 0;
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100624 while ((line = xmalloc_fgetline(fp)) != NULL) {
625 char *live;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100626 char *after_name;
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100627
628 // Examples from kernel 3.14.6:
629 //pcspkr 12718 0 - Live 0xffffffffa017e000
630 //snd_timer 28690 2 snd_seq,snd_pcm, Live 0xffffffffa025e000
631 //i915 801405 2 - Live 0xffffffffa0096000
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100632 after_name = is_prefixed_with(line, name);
633 if (!after_name || *after_name != ' ') {
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100634 free(line);
635 continue;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000636 }
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100637 live = strstr(line, " Live");
638 free(line);
639 if (!live) {
640 /* State can be Unloading, Loading, or Live.
641 * modprobe must not return prematurely if we see "Loading":
642 * it can cause further programs to assume load completed,
643 * but it did not (yet)!
644 * Wait up to 5*20 ms for it to resolve.
645 */
646 ret -= 2;
647 if (ret == 0)
648 break; /* huh? report as "not loaded" */
649 fclose(fp);
650 usleep(20*1000);
651 goto again;
652 }
653 ret = 1;
654 break;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000655 }
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100656 fclose(fp);
657
Denys Vlasenko402afe12015-02-13 11:53:33 +0100658 return ret & 1;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000659}
660#else
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100661#define already_loaded(name) 0
Denis Vlasenko671691c2008-07-04 10:25:44 +0000662#endif
663
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100664static int rmmod(const char *filename)
665{
666 int r;
667 char modname[MODULE_NAME_LEN];
668
669 filename2modname(filename, modname);
670 r = delete_module(modname, O_NONBLOCK | O_EXCL);
671 dbg1_error_msg("delete_module('%s', O_NONBLOCK | O_EXCL):%d", modname, r);
672 if (r != 0 && !(option_mask32 & OPT_q)) {
673 bb_perror_msg("remove '%s'", modname);
674 }
675 return r;
676}
677
Denis Vlasenko671691c2008-07-04 10:25:44 +0000678/*
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000679 * Given modules definition and module name (or alias, or symbol)
680 * load/remove the module respecting dependencies.
681 * NB: also called by depmod with bogus name "/",
682 * just in order to force modprobe.dep.bb creation.
Denis Vlasenko671691c2008-07-04 10:25:44 +0000683*/
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +0800684#if !ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
Denis Vlasenko671691c2008-07-04 10:25:44 +0000685#define process_module(a,b) process_module(a)
686#define cmdline_options ""
687#endif
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200688static int process_module(char *name, const char *cmdline_options)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000689{
690 char *s, *deps, *options;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200691 module_info **infovec;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000692 module_info *info;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200693 int infoidx;
Explorer0980172e42017-01-09 15:04:43 +0800694 bool is_remove = (ENABLE_RMMOD && ONLY_APPLET)
695 || ((ENABLE_RMMOD || ENABLE_MODPROBE) && (option_mask32 & OPT_r));
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200696 int exitcode = EXIT_SUCCESS;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200697
Denis Vlasenko671691c2008-07-04 10:25:44 +0000698 dbg1_error_msg("process_module('%s','%s')", name, cmdline_options);
699
700 replace(name, '-', '_');
701
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100702 dbg1_error_msg("already_loaded:%d is_remove:%d", already_loaded(name), is_remove);
703
Explorer0980172e42017-01-09 15:04:43 +0800704 if (is_rmmod) {
705 /* Does not remove dependencies, no need to scan, just remove.
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100706 * (compat note: this allows and strips .ko suffix)
707 */
708 rmmod(name);
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200709 return EXIT_SUCCESS;
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100710 }
711
Denys Vlasenko07e55552014-04-21 16:59:36 +0200712 /*
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100713 * We used to have "is_remove != already_loaded(name)" check here, but
Denys Vlasenko07e55552014-04-21 16:59:36 +0200714 * modprobe -r pci:v00008086d00007010sv00000000sd00000000bc01sc01i80
715 * won't unload modules (there are more than one)
716 * which have this alias.
717 */
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100718 if (!is_remove && already_loaded(name)) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000719 dbg1_error_msg("nothing to do for '%s'", name);
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200720 return EXIT_SUCCESS;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000721 }
722
723 options = NULL;
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100724 if (!is_remove) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000725 char *opt_filename = xasprintf("/etc/modules/%s", name);
726 options = xmalloc_open_read_close(opt_filename, NULL);
727 if (options)
728 replace(options, '\n', ' ');
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +0800729#if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
Denis Vlasenko671691c2008-07-04 10:25:44 +0000730 if (cmdline_options) {
731 /* NB: cmdline_options always have one leading ' '
732 * (see main()), we remove it here */
733 char *op = xasprintf(options ? "%s %s" : "%s %s" + 3,
734 cmdline_options + 1, options);
735 free(options);
736 options = op;
737 }
738#endif
739 free(opt_filename);
740 module_load_options = options;
741 dbg1_error_msg("process_module('%s'): options:'%s'", name, options);
742 }
743
744 if (!module_count) {
745 /* Scan module directory. This is done only once.
746 * It will attempt module load, and will exit(EXIT_SUCCESS)
Denys Vlasenko07e55552014-04-21 16:59:36 +0200747 * on success.
748 */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000749 module_found_idx = -1;
750 recursive_action(".",
751 ACTION_RECURSE, /* flags */
752 fileAction, /* file action */
753 NULL, /* dir action */
754 name, /* user data */
Denys Vlasenko07e55552014-04-21 16:59:36 +0200755 0 /* depth */
756 );
Denis Vlasenko671691c2008-07-04 10:25:44 +0000757 dbg1_error_msg("dirscan complete");
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100758 /* Module was not found, or load failed, or is_remove */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000759 if (module_found_idx >= 0) { /* module was found */
Denys Vlasenko07e55552014-04-21 16:59:36 +0200760 infovec = xzalloc(2 * sizeof(infovec[0]));
761 infovec[0] = &modinfo[module_found_idx];
Denis Vlasenko671691c2008-07-04 10:25:44 +0000762 } else { /* search for alias, not a plain module name */
Denys Vlasenko07e55552014-04-21 16:59:36 +0200763 infovec = find_alias(name);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000764 }
765 } else {
Denys Vlasenko07e55552014-04-21 16:59:36 +0200766 infovec = find_alias(name);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000767 }
768
Denys Vlasenko3c75b1c2015-01-11 17:40:30 +0100769 if (!infovec) {
770 /* both dirscan and find_alias found nothing */
Denys Vlasenko0d6e3ad2017-04-06 13:45:00 +0200771 if (!is_remove && !is_depmod) { /* it wasn't rmmod or depmod */
Denys Vlasenko3c75b1c2015-01-11 17:40:30 +0100772 bb_error_msg("module '%s' not found", name);
773//TODO: _and_die()? or should we continue (un)loading modules listed on cmdline?
Denys Vlasenko0d6e3ad2017-04-06 13:45:00 +0200774 /* "modprobe non-existing-module; echo $?" must print 1 */
775 exitcode = EXIT_FAILURE;
776 }
Denys Vlasenko3c75b1c2015-01-11 17:40:30 +0100777 goto ret;
778 }
779
Denys Vlasenko07e55552014-04-21 16:59:36 +0200780 /* There can be more than one module for the given alias. For example,
781 * "pci:v00008086d00007010sv00000000sd00000000bc01sc01i80" matches
782 * ata_piix because it has alias "pci:v00008086d00007010sv*sd*bc*sc*i*"
783 * and ata_generic, it has alias "pci:v*d*sv*sd*bc01sc01i*"
784 * Standard modprobe loads them both. We achieve it by returning
785 * a *list* of modinfo pointers from find_alias().
786 */
Denys Vlasenko63321512009-10-08 22:54:41 +0200787
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100788 /* modprobe -r? unload module(s) */
789 if (is_remove) {
Denys Vlasenko07e55552014-04-21 16:59:36 +0200790 infoidx = 0;
791 while ((info = infovec[infoidx++]) != NULL) {
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100792 int r = rmmod(bb_get_last_path_component_nostrip(info->pathname));
Denys Vlasenko07e55552014-04-21 16:59:36 +0200793 if (r != 0) {
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100794 goto ret; /* error */
Denys Vlasenko07e55552014-04-21 16:59:36 +0200795 }
Denis Vlasenko671691c2008-07-04 10:25:44 +0000796 }
Denys Vlasenko06a98e32012-09-25 20:37:38 +0200797 /* modprobe -r: we do not stop here -
Denis Vlasenko671691c2008-07-04 10:25:44 +0000798 * continue to unload modules on which the module depends:
799 * "-r --remove: option causes modprobe to remove a module.
800 * If the modules it depends on are also unused, modprobe
Denys Vlasenko06a98e32012-09-25 20:37:38 +0200801 * will try to remove them, too."
802 */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000803 }
804
Denys Vlasenko07e55552014-04-21 16:59:36 +0200805 infoidx = 0;
806 while ((info = infovec[infoidx++]) != NULL) {
807 /* Iterate thru dependencies, trying to (un)load them */
808 deps = str_2_list(info->deps);
809 for (s = deps; *s; s += strlen(s) + 1) {
810 //if (strcmp(name, s) != 0) // N.B. do loops exist?
811 dbg1_error_msg("recurse on dep '%s'", s);
812 process_module(s, NULL);
813 dbg1_error_msg("recurse on dep '%s' done", s);
814 }
815 free(deps);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000816
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100817 if (is_remove)
Denys Vlasenko07e55552014-04-21 16:59:36 +0200818 continue;
819
820 /* We are modprobe: load it */
821 if (options && strstr(options, "blacklist")) {
Denis Vlasenko1ad4db12008-11-12 00:09:58 +0000822 dbg1_error_msg("'%s': blacklisted", info->pathname);
Denys Vlasenko07e55552014-04-21 16:59:36 +0200823 continue;
824 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200825 if (info->open_read_failed) {
826 /* We already tried it, didn't work. Don't try load again */
827 exitcode = EXIT_FAILURE;
828 continue;
829 }
Denys Vlasenko07e55552014-04-21 16:59:36 +0200830 errno = 0;
831 if (load_module(info->pathname, options) != 0) {
832 if (EEXIST != errno) {
833 bb_error_msg("'%s': %s",
834 info->pathname,
835 moderror(errno));
836 } else {
837 dbg1_error_msg("'%s': %s",
838 info->pathname,
839 moderror(errno));
840 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200841 exitcode = EXIT_FAILURE;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000842 }
843 }
844 ret:
Denys Vlasenko07e55552014-04-21 16:59:36 +0200845 free(infovec);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000846 free(options);
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200847
848 return exitcode;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000849}
850#undef cmdline_options
851
852
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000853/* For reference, module-init-tools v3.4 options:
Denis Vlasenko671691c2008-07-04 10:25:44 +0000854
855# insmod
856Usage: insmod filename [args]
857
858# rmmod --help
859Usage: rmmod [-fhswvV] modulename ...
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000860 -f (or --force) forces a module unload, and may crash your
861 machine. This requires the Forced Module Removal option
862 when the kernel was compiled.
863 -h (or --help) prints this help text
Denis Vlasenko671691c2008-07-04 10:25:44 +0000864 -s (or --syslog) says use syslog, not stderr
865 -v (or --verbose) enables more messages
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000866 -V (or --version) prints the version code
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000867 -w (or --wait) begins module removal even if it is used
Denis Vlasenko671691c2008-07-04 10:25:44 +0000868 and will stop new users from accessing the module (so it
869 should eventually fall to zero).
870
871# modprobe
Kang-Che Sung25773482017-02-09 22:48:51 +0800872Usage: modprobe [-v] [-V] [-C config-file] [-d <dirname> ] [-n] [-i] [-q]
873 [-b] [-o <modname>] [ --dump-modversions ] <modname> [parameters...]
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000874modprobe -r [-n] [-i] [-v] <modulename> ...
875modprobe -l -t <dirname> [ -a <modulename> ...]
Denis Vlasenko671691c2008-07-04 10:25:44 +0000876
877# depmod --help
Kang-Che Sung25773482017-02-09 22:48:51 +0800878depmod 3.13 -- part of module-init-tools
879depmod -[aA] [-n -e -v -q -V -r -u -w -m]
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000880 [-b basedirectory] [forced_version]
Kang-Che Sung25773482017-02-09 22:48:51 +0800881depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000882If no arguments (except options) are given, "depmod -a" is assumed.
Denys Vlasenkobf2af9a2009-05-25 04:15:37 +0200883depmod will output a dependency list suitable for the modprobe utility.
Denis Vlasenko671691c2008-07-04 10:25:44 +0000884Options:
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000885 -a, --all Probe all modules
886 -A, --quick Only does the work if there's a new module
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000887 -e, --errsyms Report not supplied symbols
Kang-Che Sung25773482017-02-09 22:48:51 +0800888 -m, --map Create the legacy map files
889 -n, --show Write the dependency file on stdout only
890 -P, --symbol-prefix Architecture symbol prefix
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000891 -V, --version Print the release version
892 -v, --verbose Enable verbose mode
Kang-Che Sung25773482017-02-09 22:48:51 +0800893 -w, --warn Warn on duplicates
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000894 -h, --help Print this usage message
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000895The following options are useful for people managing distributions:
896 -b basedirectory
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000897 --basedir basedirectory
898 Use an image of a module tree
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000899 -F kernelsyms
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000900 --filesyms kernelsyms
901 Use the file instead of the current kernel symbols
Kang-Che Sung25773482017-02-09 22:48:51 +0800902 -E Module.symvers
903 --symvers Module.symvers
904 Use Module.symvers file to check symbol versions
Denis Vlasenko671691c2008-07-04 10:25:44 +0000905*/
906
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200907//usage:#if ENABLE_MODPROBE_SMALL
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200908
Kang-Che Sungc393b1e2017-02-09 22:48:50 +0800909//usage:#define depmod_trivial_usage "[-n]"
910//usage:#define depmod_full_usage "\n\n"
911//usage: "Generate modules.dep.bb"
912//usage: "\n"
913//usage: "\n -n Dry run: print file to stdout"
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200914
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200915//usage:#define insmod_trivial_usage
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +0800916//usage: "FILE" IF_FEATURE_CMDLINE_MODULE_OPTIONS(" [SYMBOL=VALUE]...")
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200917//usage:#define insmod_full_usage "\n\n"
Denys Vlasenko5fd3ddf2014-04-19 15:04:39 +0200918//usage: "Load kernel module"
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200919
920//usage:#define rmmod_trivial_usage
Denys Vlasenkoca659f82017-01-30 19:21:12 +0100921//usage: "MODULE..."
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200922//usage:#define rmmod_full_usage "\n\n"
Denys Vlasenkoca659f82017-01-30 19:21:12 +0100923//usage: "Unload kernel modules"
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200924
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200925//usage:#define modprobe_trivial_usage
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +0800926//usage: "[-rq] MODULE" IF_FEATURE_CMDLINE_MODULE_OPTIONS(" [SYMBOL=VALUE]...")
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200927//usage:#define modprobe_full_usage "\n\n"
Denys Vlasenkoca659f82017-01-30 19:21:12 +0100928//usage: " -r Remove MODULE"
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200929//usage: "\n -q Quiet"
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200930
931//usage:#endif
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200932
Denis Vlasenko671691c2008-07-04 10:25:44 +0000933int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000934int modprobe_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000935{
Denys Vlasenko6bf52b62017-01-09 09:48:58 +0100936#if ENABLE_MODPROBE || ENABLE_INSMOD || ENABLE_RMMOD
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200937 int exitcode;
Denys Vlasenko6bf52b62017-01-09 09:48:58 +0100938#endif
Denis Vlasenko671691c2008-07-04 10:25:44 +0000939 struct utsname uts;
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +0800940 IF_FEATURE_CMDLINE_MODULE_OPTIONS(char *options = NULL;)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000941
Denis Vlasenko671691c2008-07-04 10:25:44 +0000942 INIT_G();
943
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000944 /* Prevent ugly corner cases with no modules at all */
945 modinfo = xzalloc(sizeof(modinfo[0]));
946
Denys Vlasenkoc2b18582017-02-07 16:41:25 +0100947 if ((MOD_APPLET_CNT == 2 && ENABLE_DEPMOD && ENABLE_MODPROBE)
948 || is_depmod || is_modprobe
949 ) {
Denis Vlasenko1c781cc2008-09-06 14:14:01 +0000950 /* Goto modules directory */
951 xchdir(CONFIG_DEFAULT_MODULES_DIR);
Kang-Che Sung4ae658f2017-02-05 19:02:34 +0100952 uname(&uts); /* never fails */
Denis Vlasenko1c781cc2008-09-06 14:14:01 +0000953 }
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000954
Denys Vlasenko6bf52b62017-01-09 09:48:58 +0100955 /* depmod? */
Kang-Che Sungfdfd7162017-01-31 17:06:43 +0800956 if (is_depmod) {
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000957 /* Supported:
958 * -n: print result to stdout
959 * -a: process all modules (default)
960 * optional VERSION parameter
961 * Ignored:
962 * -A: do work only if a module is newer than depfile
963 * -e: report any symbols which a module needs
964 * which are not supplied by other modules or the kernel
965 * -F FILE: System.map (symbols for -e)
Kang-Che Sung25773482017-02-09 22:48:51 +0800966 * -q, -r, -u: noop
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000967 * Not supported:
968 * -b BASEDIR: (TODO!) modules are in
969 * $BASEDIR/lib/modules/$VERSION
Kang-Che Sung25773482017-02-09 22:48:51 +0800970 * -m: create legacy "modules.*map" files (deprecated; in
971 * kmod's depmod, prints a warning message and continues)
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000972 * -v: human readable deps to stdout
973 * -V: version (don't want to support it - people may depend
974 * on it as an indicator of "standard" depmod)
975 * -h: help (well duh)
976 * module1.o module2.o parameters (just ignored for now)
977 */
978 getopt32(argv, "na" "AeF:qru" /* "b:vV", NULL */, NULL);
979 argv += optind;
980 /* if (argv[0] && argv[1]) bb_show_usage(); */
981 /* Goto $VERSION directory */
982 xchdir(argv[0] ? argv[0] : uts.release);
983 /* Force full module scan by asking to find a bogus module.
984 * This will generate modules.dep.bb as a side effect. */
985 process_module((char*)"/", NULL);
986 return !wrote_dep_bb_ok;
987 }
988
Explorer0980172e42017-01-09 15:04:43 +0800989#if ENABLE_MODPROBE || ENABLE_INSMOD || ENABLE_RMMOD
990 /* modprobe, insmod, rmmod require at least one argument */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000991 /* only -q (quiet) and -r (rmmod),
992 * the rest are accepted and ignored (compat) */
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200993 getopt32(argv, "^" "qrfsvwb" "\0" "-1");
Denis Vlasenko671691c2008-07-04 10:25:44 +0000994 argv += optind;
995
Kang-Che Sung4ae658f2017-02-05 19:02:34 +0100996 if (is_modprobe) {
Denis Vlasenko1c781cc2008-09-06 14:14:01 +0000997 /* Goto $VERSION directory */
998 xchdir(uts.release);
999 }
Denis Vlasenko671691c2008-07-04 10:25:44 +00001000
Explorer0980172e42017-01-09 15:04:43 +08001001 /* are we rmmod? -> simulate modprobe -r, but don't bother the flag if
1002 * there're no other applets here */
1003 if (is_rmmod) {
1004 if (!ONLY_APPLET)
1005 option_mask32 |= OPT_r;
1006 } else if (!ENABLE_MODPROBE || !(option_mask32 & OPT_r)) {
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +08001007# if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
Denys Vlasenko68c048f2015-02-07 20:44:46 +01001008 /* If not rmmod/-r, parse possible module options given on command line.
Denis Vlasenko671691c2008-07-04 10:25:44 +00001009 * insmod/modprobe takes one module name, the rest are parameters. */
Denis Vlasenko671691c2008-07-04 10:25:44 +00001010 char **arg = argv;
1011 while (*++arg) {
1012 /* Enclose options in quotes */
1013 char *s = options;
1014 options = xasprintf("%s \"%s\"", s ? s : "", *arg);
1015 free(s);
1016 *arg = NULL;
1017 }
Explorer0980172e42017-01-09 15:04:43 +08001018# else
Denis Vlasenko671691c2008-07-04 10:25:44 +00001019 argv[1] = NULL;
Explorer0980172e42017-01-09 15:04:43 +08001020# endif
1021 }
Denis Vlasenko671691c2008-07-04 10:25:44 +00001022
Explorer0980172e42017-01-09 15:04:43 +08001023 if (is_insmod) {
Denis Vlasenko1c781cc2008-09-06 14:14:01 +00001024 size_t len;
1025 void *map;
1026
1027 len = MAXINT(ssize_t);
Denys Vlasenkoe9d12b52011-01-09 20:57:52 +01001028 map = xmalloc_open_zipped_read_close(*argv, &len);
Denys Vlasenko094cc512011-01-17 14:58:27 +01001029 if (!map)
1030 bb_perror_msg_and_die("can't read '%s'", *argv);
Denis Vlasenko1c781cc2008-09-06 14:14:01 +00001031 if (init_module(map, len,
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +08001032 (IF_FEATURE_CMDLINE_MODULE_OPTIONS(options ? options : ) "")
Denys Vlasenkoe9d12b52011-01-09 20:57:52 +01001033 ) != 0
1034 ) {
Denis Vlasenko1f632292009-04-13 02:25:40 +00001035 bb_error_msg_and_die("can't insert '%s': %s",
Denis Vlasenko1c781cc2008-09-06 14:14:01 +00001036 *argv, moderror(errno));
Denys Vlasenkoe9d12b52011-01-09 20:57:52 +01001037 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001038 return EXIT_SUCCESS;
Denis Vlasenko1c781cc2008-09-06 14:14:01 +00001039 }
1040
Denis Vlasenko7f950a92008-07-10 14:14:45 +00001041 /* Try to load modprobe.dep.bb */
Explorer0980172e42017-01-09 15:04:43 +08001042 if (!is_rmmod) {
Denys Vlasenko68c048f2015-02-07 20:44:46 +01001043 load_dep_bb();
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001044 }
Denis Vlasenko78436992008-07-10 14:14:20 +00001045
Denis Vlasenko671691c2008-07-04 10:25:44 +00001046 /* Load/remove modules.
Denys Vlasenko68c048f2015-02-07 20:44:46 +01001047 * Only rmmod/modprobe -r loops here, insmod/modprobe has only argv[0] */
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001048 exitcode = EXIT_SUCCESS;
Denis Vlasenko671691c2008-07-04 10:25:44 +00001049 do {
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001050 exitcode |= process_module(*argv, options);
Denys Vlasenko5b3151c2010-09-25 14:37:06 +02001051 } while (*++argv);
Denis Vlasenko671691c2008-07-04 10:25:44 +00001052
1053 if (ENABLE_FEATURE_CLEAN_UP) {
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +08001054 IF_FEATURE_CMDLINE_MODULE_OPTIONS(free(options);)
Denis Vlasenko671691c2008-07-04 10:25:44 +00001055 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001056 return exitcode;
Explorer0980172e42017-01-09 15:04:43 +08001057#endif /* MODPROBE || INSMOD || RMMOD */
Denis Vlasenko671691c2008-07-04 10:25:44 +00001058}
Denys Vlasenkoe7b54d02017-01-09 16:31:21 +01001059
1060#endif /* MOD_APPLET_CNT > 0 */