blob: 053a7df896611ae43f4a3beec7e7cdcd3bc26723 [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
Kang-Che Sungfdfd7162017-01-31 17:06:43 +080014//applet:IF_LSMOD( IF_MODPROBE_SMALL(APPLET(lsmod, BB_DIR_SBIN, BB_SUID_DROP)))
Denys Vlasenkoa1cd0d92016-12-23 15:12:27 +010015//applet:IF_MODPROBE(IF_MODPROBE_SMALL(APPLET(modprobe, BB_DIR_SBIN, BB_SUID_DROP)))
Denys Vlasenko205d48e2017-01-29 14:57:33 +010016// APPLET_ODDNAME:name main location suid_type help
Denys Vlasenkoa1cd0d92016-12-23 15:12:27 +010017//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_ODDNAME(insmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, insmod)))
Kang-Che Sungfdfd7162017-01-31 17:06:43 +080019//applet:IF_RMMOD( IF_MODPROBE_SMALL(APPLET_ODDNAME(rmmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, rmmod)))
Denys Vlasenkoc15613c2010-10-15 11:29:02 +020020
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010021//kbuild:lib-$(CONFIG_MODPROBE_SMALL) += modprobe-small.o
22
Denis Vlasenko671691c2008-07-04 10:25:44 +000023#include "libbb.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +020024/* After libbb.h, since it needs sys/types.h on some systems */
Denis Vlasenko671691c2008-07-04 10:25:44 +000025#include <sys/utsname.h> /* uname() */
26#include <fnmatch.h>
Mike Frysinger3a45b872016-09-15 12:16:33 +020027#include <sys/syscall.h>
Denis Vlasenko671691c2008-07-04 10:25:44 +000028
Waldemar Brodkorb9c083f52016-12-26 20:07:59 +010029#define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
30#define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
Mike Frysinger3a45b872016-09-15 12:16:33 +020031#ifdef __NR_finit_module
32# define finit_module(fd, uargs, flags) syscall(__NR_finit_module, fd, uargs, flags)
33#endif
Denys Vlasenko07e55552014-04-21 16:59:36 +020034/* linux/include/linux/module.h has limit of 64 chars on module names */
35#undef MODULE_NAME_LEN
36#define MODULE_NAME_LEN 64
Denis Vlasenko24a131e2008-07-09 15:30:57 +000037
38
Denys Vlasenko5b3151c2010-09-25 14:37:06 +020039#if 1
40# define dbg1_error_msg(...) ((void)0)
41# define dbg2_error_msg(...) ((void)0)
42#else
43# define dbg1_error_msg(...) bb_error_msg(__VA_ARGS__)
44# define dbg2_error_msg(...) bb_error_msg(__VA_ARGS__)
45#endif
Denis Vlasenko671691c2008-07-04 10:25:44 +000046
Denis Vlasenko24a131e2008-07-09 15:30:57 +000047#define DEPFILE_BB CONFIG_DEFAULT_DEPMOD_FILE".bb"
Denis Vlasenko671691c2008-07-04 10:25:44 +000048
Kang-Che Sungfdfd7162017-01-31 17:06:43 +080049//usage:#if ENABLE_MODPROBE_SMALL
50
51//usage:#define lsmod_trivial_usage
52//usage: ""
53//usage:#define lsmod_full_usage "\n\n"
54//usage: "List loaded kernel modules"
55
56//usage:#endif
57
58#if ENABLE_LSMOD
59int lsmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
60int lsmod_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
61{
62 xprint_and_close_file(xfopen_for_read("/proc/modules"));
63 return EXIT_SUCCESS;
64}
65#endif
66
67/* Num of applets that use modprobe_main() entry point. */
68/* lsmod is not here. */
69#define MOD_APPLET_CNT (ENABLE_MODPROBE + ENABLE_DEPMOD + ENABLE_INSMOD + ENABLE_RMMOD)
Denys Vlasenkoe7b54d02017-01-09 16:31:21 +010070
71/* Do not bother if MODPROBE_SMALL=y but no applets selected. */
72/* The rest of the file is in this if block. */
73#if MOD_APPLET_CNT > 0
74
75#define ONLY_APPLET (MOD_APPLET_CNT == 1)
Explorer0980172e42017-01-09 15:04:43 +080076#define is_modprobe (ENABLE_MODPROBE && (ONLY_APPLET || applet_name[0] == 'm'))
77#define is_depmod (ENABLE_DEPMOD && (ONLY_APPLET || applet_name[0] == 'd'))
78#define is_insmod (ENABLE_INSMOD && (ONLY_APPLET || applet_name[0] == 'i'))
Explorer0980172e42017-01-09 15:04:43 +080079#define is_rmmod (ENABLE_RMMOD && (ONLY_APPLET || applet_name[0] == 'r'))
80
Denis Vlasenko671691c2008-07-04 10:25:44 +000081enum {
Kang-Che Sungaf316aa2017-02-09 22:48:52 +080082 DEPMOD_OPT_n = (1 << 0), /* dry-run, print to stdout */
Denis Vlasenko671691c2008-07-04 10:25:44 +000083 OPT_q = (1 << 0), /* be quiet */
84 OPT_r = (1 << 1), /* module removal instead of loading */
85};
86
87typedef struct module_info {
88 char *pathname;
Denis Vlasenko78436992008-07-10 14:14:20 +000089 char *aliases;
90 char *deps;
Denys Vlasenko5c3e0602016-06-20 10:59:06 +020091 smallint open_read_failed;
Denis Vlasenko671691c2008-07-04 10:25:44 +000092} module_info;
93
94/*
95 * GLOBALS
96 */
97struct globals {
98 module_info *modinfo;
99 char *module_load_options;
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000100 smallint dep_bb_seen;
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000101 smallint wrote_dep_bb_ok;
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200102 unsigned module_count;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000103 int module_found_idx;
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200104 unsigned stringbuf_idx;
105 unsigned stringbuf_size;
106 char *stringbuf; /* some modules have lots of stuff */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000107 /* for example, drivers/media/video/saa7134/saa7134.ko */
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200108 /* therefore having a fixed biggish buffer is not wise */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000109};
110#define G (*ptr_to_globals)
111#define modinfo (G.modinfo )
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000112#define dep_bb_seen (G.dep_bb_seen )
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000113#define wrote_dep_bb_ok (G.wrote_dep_bb_ok )
Denis Vlasenko671691c2008-07-04 10:25:44 +0000114#define module_count (G.module_count )
115#define module_found_idx (G.module_found_idx )
116#define module_load_options (G.module_load_options)
117#define stringbuf_idx (G.stringbuf_idx )
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200118#define stringbuf_size (G.stringbuf_size )
Denis Vlasenko671691c2008-07-04 10:25:44 +0000119#define stringbuf (G.stringbuf )
120#define INIT_G() do { \
121 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
122} while (0)
123
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200124static void append(const char *s)
125{
126 unsigned len = strlen(s);
127 if (stringbuf_idx + len + 15 > stringbuf_size) {
128 stringbuf_size = stringbuf_idx + len + 127;
129 dbg2_error_msg("grow stringbuf to %u", stringbuf_size);
130 stringbuf = xrealloc(stringbuf, stringbuf_size);
131 }
132 memcpy(stringbuf + stringbuf_idx, s, len);
133 stringbuf_idx += len;
134}
Denis Vlasenko671691c2008-07-04 10:25:44 +0000135
136static void appendc(char c)
137{
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200138 /* We appendc() only after append(), + 15 trick in append()
139 * makes it unnecessary to check for overflow here */
140 stringbuf[stringbuf_idx++] = c;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000141}
142
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000143static void bksp(void)
144{
145 if (stringbuf_idx)
146 stringbuf_idx--;
147}
148
Denis Vlasenko671691c2008-07-04 10:25:44 +0000149static void reset_stringbuf(void)
150{
151 stringbuf_idx = 0;
152}
153
154static char* copy_stringbuf(void)
155{
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200156 char *copy = xzalloc(stringbuf_idx + 1); /* terminating NUL */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000157 return memcpy(copy, stringbuf, stringbuf_idx);
158}
159
160static char* find_keyword(char *ptr, size_t len, const char *word)
161{
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000162 if (!ptr) /* happens if xmalloc_open_zipped_read_close cannot read it */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000163 return NULL;
164
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100165 len -= strlen(word) - 1;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000166 while ((ssize_t)len > 0) {
167 char *old = ptr;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100168 char *after_word;
169
Denis Vlasenko671691c2008-07-04 10:25:44 +0000170 /* search for the first char in word */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100171 ptr = memchr(ptr, word[0], len);
Denys Vlasenko10ad6222017-04-17 16:13:32 +0200172 if (ptr == NULL) /* no occurrence left, done */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000173 break;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100174 after_word = is_prefixed_with(ptr, word);
175 if (after_word)
176 return after_word; /* found, return ptr past it */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000177 ++ptr;
178 len -= (ptr - old);
179 }
180 return NULL;
181}
182
183static void replace(char *s, char what, char with)
184{
185 while (*s) {
186 if (what == *s)
187 *s = with;
188 ++s;
189 }
190}
191
Denys Vlasenko07e55552014-04-21 16:59:36 +0200192static char *filename2modname(const char *filename, char *modname)
193{
194 int i;
Denys Vlasenko78854522015-01-01 19:02:40 +0100195 const char *from;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200196
Denys Vlasenko78854522015-01-01 19:02:40 +0100197 // Disabled since otherwise "modprobe dir/name" would work
198 // as if it is "modprobe name". It is unclear why
199 // 'basenamization' was here in the first place.
200 //from = bb_get_last_path_component_nostrip(filename);
201 from = filename;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200202 for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++)
203 modname[i] = (from[i] == '-') ? '_' : from[i];
204 modname[i] = '\0';
205
206 return modname;
207}
208
Denys Vlasenko1b671532015-01-11 17:46:56 +0100209static int pathname_matches_modname(const char *pathname, const char *modname)
210{
211 int r;
212 char name[MODULE_NAME_LEN];
213 filename2modname(bb_get_last_path_component_nostrip(pathname), name);
214 r = (strcmp(name, modname) == 0);
215 return r;
216}
217
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000218/* Take "word word", return malloced "word",NUL,"word",NUL,NUL */
219static char* str_2_list(const char *str)
220{
221 int len = strlen(str) + 1;
222 char *dst = xmalloc(len + 1);
223
224 dst[len] = '\0';
225 memcpy(dst, str, len);
226//TODO: protect against 2+ spaces: "word word"
227 replace(dst, ' ', '\0');
228 return dst;
229}
230
Denis Vlasenko671691c2008-07-04 10:25:44 +0000231/* We use error numbers in a loose translation... */
232static const char *moderror(int err)
233{
234 switch (err) {
235 case ENOEXEC:
236 return "invalid module format";
237 case ENOENT:
238 return "unknown symbol in module or invalid parameter";
239 case ESRCH:
240 return "module has wrong symbol version";
241 case EINVAL: /* "invalid parameter" */
242 return "unknown symbol in module or invalid parameter"
243 + sizeof("unknown symbol in module or");
244 default:
245 return strerror(err);
246 }
247}
248
249static int load_module(const char *fname, const char *options)
250{
251#if 1
252 int r;
253 size_t len = MAXINT(ssize_t);
254 char *module_image;
Mike Frysinger3a45b872016-09-15 12:16:33 +0200255
256 if (!options)
257 options = "";
258
Denis Vlasenko671691c2008-07-04 10:25:44 +0000259 dbg1_error_msg("load_module('%s','%s')", fname, options);
260
Mike Frysinger3a45b872016-09-15 12:16:33 +0200261 /*
262 * First we try finit_module if available. Some kernels are configured
263 * to only allow loading of modules off of secure storage (like a read-
264 * only rootfs) which needs the finit_module call. If it fails, we fall
265 * back to normal module loading to support compressed modules.
266 */
267 r = 1;
268# ifdef __NR_finit_module
269 {
270 int fd = open(fname, O_RDONLY | O_CLOEXEC);
271 if (fd >= 0) {
272 r = finit_module(fd, options, 0) != 0;
273 close(fd);
274 }
275 }
276# endif
277 if (r != 0) {
278 module_image = xmalloc_open_zipped_read_close(fname, &len);
279 r = (!module_image || init_module(module_image, len, options) != 0);
280 free(module_image);
281 }
282
Denis Vlasenko671691c2008-07-04 10:25:44 +0000283 dbg1_error_msg("load_module:%d", r);
284 return r; /* 0 = success */
285#else
286 /* For testing */
287 dbg1_error_msg("load_module('%s','%s')", fname, options);
288 return 1;
289#endif
290}
291
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200292/* Returns !0 if open/read was unsuccessful */
293static int parse_module(module_info *info, const char *pathname)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000294{
295 char *module_image;
296 char *ptr;
297 size_t len;
298 size_t pos;
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000299 dbg1_error_msg("parse_module('%s')", pathname);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000300
301 /* Read (possibly compressed) module */
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200302 errno = 0;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000303 len = 64 * 1024 * 1024; /* 64 Mb at most */
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000304 module_image = xmalloc_open_zipped_read_close(pathname, &len);
Denys Vlasenko094cc512011-01-17 14:58:27 +0100305 /* module_image == NULL is ok here, find_keyword handles it */
Denis Vlasenko78436992008-07-10 14:14:20 +0000306//TODO: optimize redundant module body reads
Denis Vlasenko671691c2008-07-04 10:25:44 +0000307
Denis Vlasenko78436992008-07-10 14:14:20 +0000308 /* "alias1 symbol:sym1 alias2 symbol:sym2" */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000309 reset_stringbuf();
Denis Vlasenko671691c2008-07-04 10:25:44 +0000310 pos = 0;
311 while (1) {
Denys Vlasenko6116cb22014-04-19 16:17:27 +0200312 unsigned start = stringbuf_idx;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000313 ptr = find_keyword(module_image + pos, len - pos, "alias=");
314 if (!ptr) {
315 ptr = find_keyword(module_image + pos, len - pos, "__ksymtab_");
316 if (!ptr)
317 break;
318 /* DOCME: __ksymtab_gpl and __ksymtab_strings occur
319 * in many modules. What do they mean? */
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000320 if (strcmp(ptr, "gpl") == 0 || strcmp(ptr, "strings") == 0)
321 goto skip;
322 dbg2_error_msg("alias:'symbol:%s'", ptr);
323 append("symbol:");
Denis Vlasenko671691c2008-07-04 10:25:44 +0000324 } else {
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000325 dbg2_error_msg("alias:'%s'", ptr);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000326 }
327 append(ptr);
328 appendc(' ');
Denys Vlasenko6116cb22014-04-19 16:17:27 +0200329 /*
330 * Don't add redundant aliases, such as:
331 * libcrc32c.ko symbol:crc32c symbol:crc32c
332 */
333 if (start) { /* "if we aren't the first alias" */
334 char *found, *last;
335 stringbuf[stringbuf_idx] = '\0';
336 last = stringbuf + start;
337 /*
338 * String at last-1 is " symbol:crc32c "
339 * (with both leading and trailing spaces).
340 */
341 if (strncmp(stringbuf, last, stringbuf_idx - start) == 0)
342 /* First alias matches us */
343 found = stringbuf;
344 else
345 /* Does any other alias match? */
346 found = strstr(stringbuf, last-1);
347 if (found < last-1) {
348 /* There is absolutely the same string before us */
349 dbg2_error_msg("redundant:'%s'", last);
350 stringbuf_idx = start;
351 goto skip;
352 }
353 }
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000354 skip:
Denis Vlasenko671691c2008-07-04 10:25:44 +0000355 pos = (ptr - module_image);
356 }
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000357 bksp(); /* remove last ' ' */
Denis Vlasenko78436992008-07-10 14:14:20 +0000358 info->aliases = copy_stringbuf();
Denys Vlasenkof9c814b2009-09-07 02:37:19 +0200359 replace(info->aliases, '-', '_');
Denis Vlasenko671691c2008-07-04 10:25:44 +0000360
Denis Vlasenko78436992008-07-10 14:14:20 +0000361 /* "dependency1 depandency2" */
362 reset_stringbuf();
Denis Vlasenko671691c2008-07-04 10:25:44 +0000363 ptr = find_keyword(module_image, len, "depends=");
364 if (ptr && *ptr) {
365 replace(ptr, ',', ' ');
366 replace(ptr, '-', '_');
367 dbg2_error_msg("dep:'%s'", ptr);
368 append(ptr);
369 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200370 free(module_image);
Denis Vlasenko78436992008-07-10 14:14:20 +0000371 info->deps = copy_stringbuf();
Denis Vlasenko671691c2008-07-04 10:25:44 +0000372
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200373 info->open_read_failed = (module_image == NULL);
374 return info->open_read_failed;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000375}
376
Denis Vlasenko671691c2008-07-04 10:25:44 +0000377static FAST_FUNC int fileAction(const char *pathname,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000378 struct stat *sb UNUSED_PARAM,
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000379 void *modname_to_match,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000380 int depth UNUSED_PARAM)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000381{
382 int cur;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000383 const char *fname;
Explorer0980172e42017-01-09 15:04:43 +0800384 bool is_remove = (ENABLE_RMMOD && ONLY_APPLET)
385 || ((ENABLE_RMMOD || ENABLE_MODPROBE) && (option_mask32 & OPT_r));
Denis Vlasenko671691c2008-07-04 10:25:44 +0000386
387 pathname += 2; /* skip "./" */
388 fname = bb_get_last_path_component_nostrip(pathname);
389 if (!strrstr(fname, ".ko")) {
390 dbg1_error_msg("'%s' is not a module", pathname);
391 return TRUE; /* not a module, continue search */
392 }
393
394 cur = module_count++;
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000395 modinfo = xrealloc_vector(modinfo, 12, cur);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000396 modinfo[cur].pathname = xstrdup(pathname);
Denis Vlasenko27842282008-08-04 13:20:36 +0000397 /*modinfo[cur].aliases = NULL; - xrealloc_vector did it */
398 /*modinfo[cur+1].pathname = NULL;*/
Denis Vlasenko671691c2008-07-04 10:25:44 +0000399
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000400 if (!pathname_matches_modname(fname, modname_to_match)) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000401 dbg1_error_msg("'%s' module name doesn't match", pathname);
402 return TRUE; /* module name doesn't match, continue search */
403 }
404
405 dbg1_error_msg("'%s' module name matches", pathname);
406 module_found_idx = cur;
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200407 if (parse_module(&modinfo[cur], pathname) != 0)
408 return TRUE; /* failed to open/read it, no point in trying loading */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000409
Explorer0980172e42017-01-09 15:04:43 +0800410 if (!is_remove) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000411 if (load_module(pathname, module_load_options) == 0) {
412 /* Load was successful, there is nothing else to do.
413 * This can happen ONLY for "top-level" module load,
Denys Vlasenko10ad6222017-04-17 16:13:32 +0200414 * not a dep, because deps don't do dirscan. */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000415 exit(EXIT_SUCCESS);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000416 }
417 }
418
Denis Vlasenko671691c2008-07-04 10:25:44 +0000419 return TRUE;
420}
421
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000422static int load_dep_bb(void)
Denis Vlasenko78436992008-07-10 14:14:20 +0000423{
424 char *line;
Denis Vlasenko5415c852008-07-21 23:05:26 +0000425 FILE *fp = fopen_for_read(DEPFILE_BB);
Denis Vlasenko78436992008-07-10 14:14:20 +0000426
427 if (!fp)
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000428 return 0;
429
430 dep_bb_seen = 1;
431 dbg1_error_msg("loading "DEPFILE_BB);
432
433 /* Why? There is a rare scenario: we did not find modprobe.dep.bb,
434 * we scanned the dir and found no module by name, then we search
435 * for alias (full scan), and we decided to generate modprobe.dep.bb.
436 * But we see modprobe.dep.bb.new! Other modprobe is at work!
437 * We wait and other modprobe renames it to modprobe.dep.bb.
438 * Now we can use it.
439 * But we already have modinfo[] filled, and "module_count = 0"
440 * makes us start anew. Yes, we leak modinfo[].xxx pointers -
441 * there is not much of data there anyway. */
442 module_count = 0;
443 memset(&modinfo[0], 0, sizeof(modinfo[0]));
Denis Vlasenko78436992008-07-10 14:14:20 +0000444
445 while ((line = xmalloc_fgetline(fp)) != NULL) {
446 char* space;
Denys Vlasenko90a99042009-09-06 02:36:23 +0200447 char* linebuf;
Denis Vlasenko78436992008-07-10 14:14:20 +0000448 int cur;
449
450 if (!line[0]) {
451 free(line);
452 continue;
453 }
454 space = strchrnul(line, ' ');
455 cur = module_count++;
456 modinfo = xrealloc_vector(modinfo, 12, cur);
Denis Vlasenko27842282008-08-04 13:20:36 +0000457 /*modinfo[cur+1].pathname = NULL; - xrealloc_vector did it */
Denis Vlasenko78436992008-07-10 14:14:20 +0000458 modinfo[cur].pathname = line; /* we take ownership of malloced block here */
459 if (*space)
460 *space++ = '\0';
461 modinfo[cur].aliases = space;
Denys Vlasenko90a99042009-09-06 02:36:23 +0200462 linebuf = xmalloc_fgetline(fp);
463 modinfo[cur].deps = linebuf ? linebuf : xzalloc(1);
Denis Vlasenko78436992008-07-10 14:14:20 +0000464 if (modinfo[cur].deps[0]) {
465 /* deps are not "", so next line must be empty */
466 line = xmalloc_fgetline(fp);
467 /* Refuse to work with damaged config file */
468 if (line && line[0])
469 bb_error_msg_and_die("error in %s at '%s'", DEPFILE_BB, line);
470 free(line);
471 }
472 }
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000473 return 1;
474}
475
476static int start_dep_bb_writeout(void)
477{
478 int fd;
479
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000480 /* depmod -n: write result to stdout */
Kang-Che Sungaf316aa2017-02-09 22:48:52 +0800481 if (is_depmod && (option_mask32 & DEPMOD_OPT_n))
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000482 return STDOUT_FILENO;
483
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000484 fd = open(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0644);
485 if (fd < 0) {
486 if (errno == EEXIST) {
487 int count = 5 * 20;
488 dbg1_error_msg(DEPFILE_BB".new exists, waiting for "DEPFILE_BB);
489 while (1) {
490 usleep(1000*1000 / 20);
491 if (load_dep_bb()) {
492 dbg1_error_msg(DEPFILE_BB" appeared");
493 return -2; /* magic number */
494 }
495 if (!--count)
496 break;
497 }
498 bb_error_msg("deleting stale %s", DEPFILE_BB".new");
499 fd = open_or_warn(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC);
500 }
501 }
502 dbg1_error_msg("opened "DEPFILE_BB".new:%d", fd);
503 return fd;
504}
505
506static void write_out_dep_bb(int fd)
507{
508 int i;
509 FILE *fp;
510
511 /* We want good error reporting. fdprintf is not good enough. */
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +0100512 fp = xfdopen_for_write(fd);
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000513 i = 0;
514 while (modinfo[i].pathname) {
515 fprintf(fp, "%s%s%s\n" "%s%s\n",
516 modinfo[i].pathname, modinfo[i].aliases[0] ? " " : "", modinfo[i].aliases,
517 modinfo[i].deps, modinfo[i].deps[0] ? "\n" : "");
518 i++;
519 }
520 /* Badly formatted depfile is a no-no. Be paranoid. */
521 errno = 0;
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000522 if (ferror(fp) | fclose(fp)) /* | instead of || is intended */
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000523 goto err;
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000524
525 if (fd == STDOUT_FILENO) /* it was depmod -n */
526 goto ok;
527
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000528 if (rename(DEPFILE_BB".new", DEPFILE_BB) != 0) {
529 err:
Denys Vlasenko651a2692010-03-23 16:25:17 +0100530 bb_perror_msg("can't create '%s'", DEPFILE_BB);
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000531 unlink(DEPFILE_BB".new");
532 } else {
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000533 ok:
534 wrote_dep_bb_ok = 1;
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000535 dbg1_error_msg("created "DEPFILE_BB);
536 }
Denis Vlasenko78436992008-07-10 14:14:20 +0000537}
538
Denys Vlasenko07e55552014-04-21 16:59:36 +0200539static module_info** find_alias(const char *alias)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000540{
541 int i;
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000542 int dep_bb_fd;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200543 int infoidx;
544 module_info **infovec;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000545 dbg1_error_msg("find_alias('%s')", alias);
546
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000547 try_again:
Denis Vlasenko671691c2008-07-04 10:25:44 +0000548 /* First try to find by name (cheaper) */
549 i = 0;
550 while (modinfo[i].pathname) {
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000551 if (pathname_matches_modname(modinfo[i].pathname, alias)) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000552 dbg1_error_msg("found '%s' in module '%s'",
553 alias, modinfo[i].pathname);
Denis Vlasenko78436992008-07-10 14:14:20 +0000554 if (!modinfo[i].aliases) {
555 parse_module(&modinfo[i], modinfo[i].pathname);
556 }
Denys Vlasenko07e55552014-04-21 16:59:36 +0200557 infovec = xzalloc(2 * sizeof(infovec[0]));
558 infovec[0] = &modinfo[i];
559 return infovec;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000560 }
Denis Vlasenko671691c2008-07-04 10:25:44 +0000561 i++;
562 }
563
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000564 /* Ok, we definitely have to scan module bodies. This is a good
565 * moment to generate modprobe.dep.bb, if it does not exist yet */
566 dep_bb_fd = dep_bb_seen ? -1 : start_dep_bb_writeout();
567 if (dep_bb_fd == -2) /* modprobe.dep.bb appeared? */
568 goto try_again;
569
Denis Vlasenko671691c2008-07-04 10:25:44 +0000570 /* Scan all module bodies, extract modinfo (it contains aliases) */
571 i = 0;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200572 infoidx = 0;
573 infovec = NULL;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000574 while (modinfo[i].pathname) {
575 char *desc, *s;
Denis Vlasenko78436992008-07-10 14:14:20 +0000576 if (!modinfo[i].aliases) {
577 parse_module(&modinfo[i], modinfo[i].pathname);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000578 }
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000579 /* "alias1 symbol:sym1 alias2 symbol:sym2" */
Denis Vlasenko78436992008-07-10 14:14:20 +0000580 desc = str_2_list(modinfo[i].aliases);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000581 /* Does matching substring exist? */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000582 for (s = desc; *s; s += strlen(s) + 1) {
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000583 /* Aliases in module bodies can be defined with
Denis Vlasenko58f59a22008-07-06 11:52:23 +0000584 * shell patterns. Example:
585 * "pci:v000010DEd000000D9sv*sd*bc*sc*i*".
586 * Plain strcmp() won't catch that */
587 if (fnmatch(s, alias, 0) == 0) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000588 dbg1_error_msg("found alias '%s' in module '%s'",
589 alias, modinfo[i].pathname);
Denys Vlasenko07e55552014-04-21 16:59:36 +0200590 infovec = xrealloc_vector(infovec, 1, infoidx);
591 infovec[infoidx++] = &modinfo[i];
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000592 break;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000593 }
594 }
595 free(desc);
596 i++;
597 }
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000598
599 /* Create module.dep.bb if needed */
600 if (dep_bb_fd >= 0) {
601 write_out_dep_bb(dep_bb_fd);
602 }
603
Denys Vlasenko07e55552014-04-21 16:59:36 +0200604 dbg1_error_msg("find_alias '%s' returns %d results", alias, infoidx);
605 return infovec;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000606}
607
608#if ENABLE_FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000609// TODO: open only once, invent config_rewind()
Denis Vlasenko671691c2008-07-04 10:25:44 +0000610static int already_loaded(const char *name)
611{
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100612 int ret;
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100613 char *line;
614 FILE *fp;
615
616 ret = 5 * 2;
617 again:
618 fp = fopen_for_read("/proc/modules");
619 if (!fp)
620 return 0;
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100621 while ((line = xmalloc_fgetline(fp)) != NULL) {
622 char *live;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100623 char *after_name;
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100624
625 // Examples from kernel 3.14.6:
626 //pcspkr 12718 0 - Live 0xffffffffa017e000
627 //snd_timer 28690 2 snd_seq,snd_pcm, Live 0xffffffffa025e000
628 //i915 801405 2 - Live 0xffffffffa0096000
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100629 after_name = is_prefixed_with(line, name);
630 if (!after_name || *after_name != ' ') {
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100631 free(line);
632 continue;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000633 }
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100634 live = strstr(line, " Live");
635 free(line);
636 if (!live) {
637 /* State can be Unloading, Loading, or Live.
638 * modprobe must not return prematurely if we see "Loading":
639 * it can cause further programs to assume load completed,
640 * but it did not (yet)!
641 * Wait up to 5*20 ms for it to resolve.
642 */
643 ret -= 2;
644 if (ret == 0)
645 break; /* huh? report as "not loaded" */
646 fclose(fp);
647 usleep(20*1000);
648 goto again;
649 }
650 ret = 1;
651 break;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000652 }
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100653 fclose(fp);
654
Denys Vlasenko402afe12015-02-13 11:53:33 +0100655 return ret & 1;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000656}
657#else
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100658#define already_loaded(name) 0
Denis Vlasenko671691c2008-07-04 10:25:44 +0000659#endif
660
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100661static int rmmod(const char *filename)
662{
663 int r;
664 char modname[MODULE_NAME_LEN];
665
666 filename2modname(filename, modname);
667 r = delete_module(modname, O_NONBLOCK | O_EXCL);
668 dbg1_error_msg("delete_module('%s', O_NONBLOCK | O_EXCL):%d", modname, r);
669 if (r != 0 && !(option_mask32 & OPT_q)) {
670 bb_perror_msg("remove '%s'", modname);
671 }
672 return r;
673}
674
Denis Vlasenko671691c2008-07-04 10:25:44 +0000675/*
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000676 * Given modules definition and module name (or alias, or symbol)
677 * load/remove the module respecting dependencies.
678 * NB: also called by depmod with bogus name "/",
679 * just in order to force modprobe.dep.bb creation.
Denis Vlasenko671691c2008-07-04 10:25:44 +0000680*/
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +0800681#if !ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
Denis Vlasenko671691c2008-07-04 10:25:44 +0000682#define process_module(a,b) process_module(a)
683#define cmdline_options ""
684#endif
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200685static int process_module(char *name, const char *cmdline_options)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000686{
687 char *s, *deps, *options;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200688 module_info **infovec;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000689 module_info *info;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200690 int infoidx;
Explorer0980172e42017-01-09 15:04:43 +0800691 bool is_remove = (ENABLE_RMMOD && ONLY_APPLET)
692 || ((ENABLE_RMMOD || ENABLE_MODPROBE) && (option_mask32 & OPT_r));
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200693 int exitcode = EXIT_SUCCESS;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200694
Denis Vlasenko671691c2008-07-04 10:25:44 +0000695 dbg1_error_msg("process_module('%s','%s')", name, cmdline_options);
696
697 replace(name, '-', '_');
698
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100699 dbg1_error_msg("already_loaded:%d is_remove:%d", already_loaded(name), is_remove);
700
Explorer0980172e42017-01-09 15:04:43 +0800701 if (is_rmmod) {
702 /* Does not remove dependencies, no need to scan, just remove.
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100703 * (compat note: this allows and strips .ko suffix)
704 */
705 rmmod(name);
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200706 return EXIT_SUCCESS;
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100707 }
708
Denys Vlasenko07e55552014-04-21 16:59:36 +0200709 /*
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100710 * We used to have "is_remove != already_loaded(name)" check here, but
Denys Vlasenko07e55552014-04-21 16:59:36 +0200711 * modprobe -r pci:v00008086d00007010sv00000000sd00000000bc01sc01i80
712 * won't unload modules (there are more than one)
713 * which have this alias.
714 */
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100715 if (!is_remove && already_loaded(name)) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000716 dbg1_error_msg("nothing to do for '%s'", name);
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200717 return EXIT_SUCCESS;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000718 }
719
720 options = NULL;
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100721 if (!is_remove) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000722 char *opt_filename = xasprintf("/etc/modules/%s", name);
723 options = xmalloc_open_read_close(opt_filename, NULL);
724 if (options)
725 replace(options, '\n', ' ');
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +0800726#if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
Denis Vlasenko671691c2008-07-04 10:25:44 +0000727 if (cmdline_options) {
728 /* NB: cmdline_options always have one leading ' '
729 * (see main()), we remove it here */
730 char *op = xasprintf(options ? "%s %s" : "%s %s" + 3,
731 cmdline_options + 1, options);
732 free(options);
733 options = op;
734 }
735#endif
736 free(opt_filename);
737 module_load_options = options;
738 dbg1_error_msg("process_module('%s'): options:'%s'", name, options);
739 }
740
741 if (!module_count) {
742 /* Scan module directory. This is done only once.
743 * It will attempt module load, and will exit(EXIT_SUCCESS)
Denys Vlasenko07e55552014-04-21 16:59:36 +0200744 * on success.
745 */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000746 module_found_idx = -1;
747 recursive_action(".",
748 ACTION_RECURSE, /* flags */
749 fileAction, /* file action */
750 NULL, /* dir action */
751 name, /* user data */
Denys Vlasenko07e55552014-04-21 16:59:36 +0200752 0 /* depth */
753 );
Denis Vlasenko671691c2008-07-04 10:25:44 +0000754 dbg1_error_msg("dirscan complete");
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100755 /* Module was not found, or load failed, or is_remove */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000756 if (module_found_idx >= 0) { /* module was found */
Denys Vlasenko07e55552014-04-21 16:59:36 +0200757 infovec = xzalloc(2 * sizeof(infovec[0]));
758 infovec[0] = &modinfo[module_found_idx];
Denis Vlasenko671691c2008-07-04 10:25:44 +0000759 } else { /* search for alias, not a plain module name */
Denys Vlasenko07e55552014-04-21 16:59:36 +0200760 infovec = find_alias(name);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000761 }
762 } else {
Denys Vlasenko07e55552014-04-21 16:59:36 +0200763 infovec = find_alias(name);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000764 }
765
Denys Vlasenko3c75b1c2015-01-11 17:40:30 +0100766 if (!infovec) {
767 /* both dirscan and find_alias found nothing */
Denys Vlasenko0d6e3ad2017-04-06 13:45:00 +0200768 if (!is_remove && !is_depmod) { /* it wasn't rmmod or depmod */
Denys Vlasenko3c75b1c2015-01-11 17:40:30 +0100769 bb_error_msg("module '%s' not found", name);
770//TODO: _and_die()? or should we continue (un)loading modules listed on cmdline?
Denys Vlasenko0d6e3ad2017-04-06 13:45:00 +0200771 /* "modprobe non-existing-module; echo $?" must print 1 */
772 exitcode = EXIT_FAILURE;
773 }
Denys Vlasenko3c75b1c2015-01-11 17:40:30 +0100774 goto ret;
775 }
776
Denys Vlasenko07e55552014-04-21 16:59:36 +0200777 /* There can be more than one module for the given alias. For example,
778 * "pci:v00008086d00007010sv00000000sd00000000bc01sc01i80" matches
779 * ata_piix because it has alias "pci:v00008086d00007010sv*sd*bc*sc*i*"
780 * and ata_generic, it has alias "pci:v*d*sv*sd*bc01sc01i*"
781 * Standard modprobe loads them both. We achieve it by returning
782 * a *list* of modinfo pointers from find_alias().
783 */
Denys Vlasenko63321512009-10-08 22:54:41 +0200784
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100785 /* modprobe -r? unload module(s) */
786 if (is_remove) {
Denys Vlasenko07e55552014-04-21 16:59:36 +0200787 infoidx = 0;
788 while ((info = infovec[infoidx++]) != NULL) {
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100789 int r = rmmod(bb_get_last_path_component_nostrip(info->pathname));
Denys Vlasenko07e55552014-04-21 16:59:36 +0200790 if (r != 0) {
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100791 goto ret; /* error */
Denys Vlasenko07e55552014-04-21 16:59:36 +0200792 }
Denis Vlasenko671691c2008-07-04 10:25:44 +0000793 }
Denys Vlasenko06a98e32012-09-25 20:37:38 +0200794 /* modprobe -r: we do not stop here -
Denis Vlasenko671691c2008-07-04 10:25:44 +0000795 * continue to unload modules on which the module depends:
796 * "-r --remove: option causes modprobe to remove a module.
797 * If the modules it depends on are also unused, modprobe
Denys Vlasenko06a98e32012-09-25 20:37:38 +0200798 * will try to remove them, too."
799 */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000800 }
801
Denys Vlasenko07e55552014-04-21 16:59:36 +0200802 infoidx = 0;
803 while ((info = infovec[infoidx++]) != NULL) {
804 /* Iterate thru dependencies, trying to (un)load them */
805 deps = str_2_list(info->deps);
806 for (s = deps; *s; s += strlen(s) + 1) {
807 //if (strcmp(name, s) != 0) // N.B. do loops exist?
808 dbg1_error_msg("recurse on dep '%s'", s);
809 process_module(s, NULL);
810 dbg1_error_msg("recurse on dep '%s' done", s);
811 }
812 free(deps);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000813
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100814 if (is_remove)
Denys Vlasenko07e55552014-04-21 16:59:36 +0200815 continue;
816
817 /* We are modprobe: load it */
818 if (options && strstr(options, "blacklist")) {
Denis Vlasenko1ad4db12008-11-12 00:09:58 +0000819 dbg1_error_msg("'%s': blacklisted", info->pathname);
Denys Vlasenko07e55552014-04-21 16:59:36 +0200820 continue;
821 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200822 if (info->open_read_failed) {
823 /* We already tried it, didn't work. Don't try load again */
824 exitcode = EXIT_FAILURE;
825 continue;
826 }
Denys Vlasenko07e55552014-04-21 16:59:36 +0200827 errno = 0;
828 if (load_module(info->pathname, options) != 0) {
829 if (EEXIST != errno) {
830 bb_error_msg("'%s': %s",
831 info->pathname,
832 moderror(errno));
833 } else {
834 dbg1_error_msg("'%s': %s",
835 info->pathname,
836 moderror(errno));
837 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200838 exitcode = EXIT_FAILURE;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000839 }
840 }
841 ret:
Denys Vlasenko07e55552014-04-21 16:59:36 +0200842 free(infovec);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000843 free(options);
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200844
845 return exitcode;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000846}
847#undef cmdline_options
848
849
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000850/* For reference, module-init-tools v3.4 options:
Denis Vlasenko671691c2008-07-04 10:25:44 +0000851
852# insmod
853Usage: insmod filename [args]
854
855# rmmod --help
856Usage: rmmod [-fhswvV] modulename ...
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000857 -f (or --force) forces a module unload, and may crash your
858 machine. This requires the Forced Module Removal option
859 when the kernel was compiled.
860 -h (or --help) prints this help text
Denis Vlasenko671691c2008-07-04 10:25:44 +0000861 -s (or --syslog) says use syslog, not stderr
862 -v (or --verbose) enables more messages
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000863 -V (or --version) prints the version code
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000864 -w (or --wait) begins module removal even if it is used
Denis Vlasenko671691c2008-07-04 10:25:44 +0000865 and will stop new users from accessing the module (so it
866 should eventually fall to zero).
867
868# modprobe
Kang-Che Sung25773482017-02-09 22:48:51 +0800869Usage: modprobe [-v] [-V] [-C config-file] [-d <dirname> ] [-n] [-i] [-q]
870 [-b] [-o <modname>] [ --dump-modversions ] <modname> [parameters...]
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000871modprobe -r [-n] [-i] [-v] <modulename> ...
872modprobe -l -t <dirname> [ -a <modulename> ...]
Denis Vlasenko671691c2008-07-04 10:25:44 +0000873
874# depmod --help
Kang-Che Sung25773482017-02-09 22:48:51 +0800875depmod 3.13 -- part of module-init-tools
876depmod -[aA] [-n -e -v -q -V -r -u -w -m]
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000877 [-b basedirectory] [forced_version]
Kang-Che Sung25773482017-02-09 22:48:51 +0800878depmod [-n -e -v -q -r -u -w] [-F kernelsyms] module1.ko module2.ko ...
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000879If no arguments (except options) are given, "depmod -a" is assumed.
Denys Vlasenkobf2af9a2009-05-25 04:15:37 +0200880depmod will output a dependency list suitable for the modprobe utility.
Denis Vlasenko671691c2008-07-04 10:25:44 +0000881Options:
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000882 -a, --all Probe all modules
883 -A, --quick Only does the work if there's a new module
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000884 -e, --errsyms Report not supplied symbols
Kang-Che Sung25773482017-02-09 22:48:51 +0800885 -m, --map Create the legacy map files
886 -n, --show Write the dependency file on stdout only
887 -P, --symbol-prefix Architecture symbol prefix
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000888 -V, --version Print the release version
889 -v, --verbose Enable verbose mode
Kang-Che Sung25773482017-02-09 22:48:51 +0800890 -w, --warn Warn on duplicates
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000891 -h, --help Print this usage message
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000892The following options are useful for people managing distributions:
893 -b basedirectory
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000894 --basedir basedirectory
895 Use an image of a module tree
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000896 -F kernelsyms
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000897 --filesyms kernelsyms
898 Use the file instead of the current kernel symbols
Kang-Che Sung25773482017-02-09 22:48:51 +0800899 -E Module.symvers
900 --symvers Module.symvers
901 Use Module.symvers file to check symbol versions
Denis Vlasenko671691c2008-07-04 10:25:44 +0000902*/
903
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200904//usage:#if ENABLE_MODPROBE_SMALL
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200905
Kang-Che Sungc393b1e2017-02-09 22:48:50 +0800906//usage:#define depmod_trivial_usage "[-n]"
907//usage:#define depmod_full_usage "\n\n"
908//usage: "Generate modules.dep.bb"
909//usage: "\n"
910//usage: "\n -n Dry run: print file to stdout"
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200911
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200912//usage:#define insmod_trivial_usage
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +0800913//usage: "FILE" IF_FEATURE_CMDLINE_MODULE_OPTIONS(" [SYMBOL=VALUE]...")
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200914//usage:#define insmod_full_usage "\n\n"
Denys Vlasenko5fd3ddf2014-04-19 15:04:39 +0200915//usage: "Load kernel module"
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200916
917//usage:#define rmmod_trivial_usage
Denys Vlasenkoca659f82017-01-30 19:21:12 +0100918//usage: "MODULE..."
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200919//usage:#define rmmod_full_usage "\n\n"
Denys Vlasenkoca659f82017-01-30 19:21:12 +0100920//usage: "Unload kernel modules"
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200921
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200922//usage:#define modprobe_trivial_usage
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +0800923//usage: "[-rq] MODULE" IF_FEATURE_CMDLINE_MODULE_OPTIONS(" [SYMBOL=VALUE]...")
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200924//usage:#define modprobe_full_usage "\n\n"
Denys Vlasenkoca659f82017-01-30 19:21:12 +0100925//usage: " -r Remove MODULE"
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200926//usage: "\n -q Quiet"
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200927
928//usage:#endif
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200929
Denis Vlasenko671691c2008-07-04 10:25:44 +0000930int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000931int modprobe_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000932{
Denys Vlasenko6bf52b62017-01-09 09:48:58 +0100933#if ENABLE_MODPROBE || ENABLE_INSMOD || ENABLE_RMMOD
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200934 int exitcode;
Denys Vlasenko6bf52b62017-01-09 09:48:58 +0100935#endif
Denis Vlasenko671691c2008-07-04 10:25:44 +0000936 struct utsname uts;
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +0800937 IF_FEATURE_CMDLINE_MODULE_OPTIONS(char *options = NULL;)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000938
Denis Vlasenko671691c2008-07-04 10:25:44 +0000939 INIT_G();
940
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000941 /* Prevent ugly corner cases with no modules at all */
942 modinfo = xzalloc(sizeof(modinfo[0]));
943
Denys Vlasenkoc2b18582017-02-07 16:41:25 +0100944 if ((MOD_APPLET_CNT == 2 && ENABLE_DEPMOD && ENABLE_MODPROBE)
945 || is_depmod || is_modprobe
946 ) {
Denis Vlasenko1c781cc2008-09-06 14:14:01 +0000947 /* Goto modules directory */
948 xchdir(CONFIG_DEFAULT_MODULES_DIR);
Kang-Che Sung4ae658f2017-02-05 19:02:34 +0100949 uname(&uts); /* never fails */
Denis Vlasenko1c781cc2008-09-06 14:14:01 +0000950 }
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000951
Denys Vlasenko6bf52b62017-01-09 09:48:58 +0100952 /* depmod? */
Kang-Che Sungfdfd7162017-01-31 17:06:43 +0800953 if (is_depmod) {
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000954 /* Supported:
955 * -n: print result to stdout
956 * -a: process all modules (default)
957 * optional VERSION parameter
958 * Ignored:
959 * -A: do work only if a module is newer than depfile
960 * -e: report any symbols which a module needs
961 * which are not supplied by other modules or the kernel
962 * -F FILE: System.map (symbols for -e)
Kang-Che Sung25773482017-02-09 22:48:51 +0800963 * -q, -r, -u: noop
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000964 * Not supported:
965 * -b BASEDIR: (TODO!) modules are in
966 * $BASEDIR/lib/modules/$VERSION
Kang-Che Sung25773482017-02-09 22:48:51 +0800967 * -m: create legacy "modules.*map" files (deprecated; in
968 * kmod's depmod, prints a warning message and continues)
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000969 * -v: human readable deps to stdout
970 * -V: version (don't want to support it - people may depend
971 * on it as an indicator of "standard" depmod)
972 * -h: help (well duh)
973 * module1.o module2.o parameters (just ignored for now)
974 */
975 getopt32(argv, "na" "AeF:qru" /* "b:vV", NULL */, NULL);
976 argv += optind;
977 /* if (argv[0] && argv[1]) bb_show_usage(); */
978 /* Goto $VERSION directory */
979 xchdir(argv[0] ? argv[0] : uts.release);
980 /* Force full module scan by asking to find a bogus module.
981 * This will generate modules.dep.bb as a side effect. */
982 process_module((char*)"/", NULL);
983 return !wrote_dep_bb_ok;
984 }
985
Explorer0980172e42017-01-09 15:04:43 +0800986#if ENABLE_MODPROBE || ENABLE_INSMOD || ENABLE_RMMOD
987 /* modprobe, insmod, rmmod require at least one argument */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000988 opt_complementary = "-1";
989 /* only -q (quiet) and -r (rmmod),
990 * the rest are accepted and ignored (compat) */
Felipe Contreras428bd2d2012-01-31 14:55:15 +0100991 getopt32(argv, "qrfsvwb");
Denis Vlasenko671691c2008-07-04 10:25:44 +0000992 argv += optind;
993
Kang-Che Sung4ae658f2017-02-05 19:02:34 +0100994 if (is_modprobe) {
Denis Vlasenko1c781cc2008-09-06 14:14:01 +0000995 /* Goto $VERSION directory */
996 xchdir(uts.release);
997 }
Denis Vlasenko671691c2008-07-04 10:25:44 +0000998
Explorer0980172e42017-01-09 15:04:43 +0800999 /* are we rmmod? -> simulate modprobe -r, but don't bother the flag if
1000 * there're no other applets here */
1001 if (is_rmmod) {
1002 if (!ONLY_APPLET)
1003 option_mask32 |= OPT_r;
1004 } else if (!ENABLE_MODPROBE || !(option_mask32 & OPT_r)) {
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +08001005# if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
Denys Vlasenko68c048f2015-02-07 20:44:46 +01001006 /* If not rmmod/-r, parse possible module options given on command line.
Denis Vlasenko671691c2008-07-04 10:25:44 +00001007 * insmod/modprobe takes one module name, the rest are parameters. */
Denis Vlasenko671691c2008-07-04 10:25:44 +00001008 char **arg = argv;
1009 while (*++arg) {
1010 /* Enclose options in quotes */
1011 char *s = options;
1012 options = xasprintf("%s \"%s\"", s ? s : "", *arg);
1013 free(s);
1014 *arg = NULL;
1015 }
Explorer0980172e42017-01-09 15:04:43 +08001016# else
Denis Vlasenko671691c2008-07-04 10:25:44 +00001017 argv[1] = NULL;
Explorer0980172e42017-01-09 15:04:43 +08001018# endif
1019 }
Denis Vlasenko671691c2008-07-04 10:25:44 +00001020
Explorer0980172e42017-01-09 15:04:43 +08001021 if (is_insmod) {
Denis Vlasenko1c781cc2008-09-06 14:14:01 +00001022 size_t len;
1023 void *map;
1024
1025 len = MAXINT(ssize_t);
Denys Vlasenkoe9d12b52011-01-09 20:57:52 +01001026 map = xmalloc_open_zipped_read_close(*argv, &len);
Denys Vlasenko094cc512011-01-17 14:58:27 +01001027 if (!map)
1028 bb_perror_msg_and_die("can't read '%s'", *argv);
Denis Vlasenko1c781cc2008-09-06 14:14:01 +00001029 if (init_module(map, len,
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +08001030 (IF_FEATURE_CMDLINE_MODULE_OPTIONS(options ? options : ) "")
Denys Vlasenkoe9d12b52011-01-09 20:57:52 +01001031 ) != 0
1032 ) {
Denis Vlasenko1f632292009-04-13 02:25:40 +00001033 bb_error_msg_and_die("can't insert '%s': %s",
Denis Vlasenko1c781cc2008-09-06 14:14:01 +00001034 *argv, moderror(errno));
Denys Vlasenkoe9d12b52011-01-09 20:57:52 +01001035 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001036 return EXIT_SUCCESS;
Denis Vlasenko1c781cc2008-09-06 14:14:01 +00001037 }
1038
Denis Vlasenko7f950a92008-07-10 14:14:45 +00001039 /* Try to load modprobe.dep.bb */
Explorer0980172e42017-01-09 15:04:43 +08001040 if (!is_rmmod) {
Denys Vlasenko68c048f2015-02-07 20:44:46 +01001041 load_dep_bb();
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001042 }
Denis Vlasenko78436992008-07-10 14:14:20 +00001043
Denis Vlasenko671691c2008-07-04 10:25:44 +00001044 /* Load/remove modules.
Denys Vlasenko68c048f2015-02-07 20:44:46 +01001045 * Only rmmod/modprobe -r loops here, insmod/modprobe has only argv[0] */
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001046 exitcode = EXIT_SUCCESS;
Denis Vlasenko671691c2008-07-04 10:25:44 +00001047 do {
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001048 exitcode |= process_module(*argv, options);
Denys Vlasenko5b3151c2010-09-25 14:37:06 +02001049 } while (*++argv);
Denis Vlasenko671691c2008-07-04 10:25:44 +00001050
1051 if (ENABLE_FEATURE_CLEAN_UP) {
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +08001052 IF_FEATURE_CMDLINE_MODULE_OPTIONS(free(options);)
Denis Vlasenko671691c2008-07-04 10:25:44 +00001053 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001054 return exitcode;
Explorer0980172e42017-01-09 15:04:43 +08001055#endif /* MODPROBE || INSMOD || RMMOD */
Denis Vlasenko671691c2008-07-04 10:25:44 +00001056}
Denys Vlasenkoe7b54d02017-01-09 16:31:21 +01001057
1058#endif /* MOD_APPLET_CNT > 0 */