blob: 21fa9dbdde4a096a62b9f94190a8b76c10b598a8 [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
11/* config MODPROBE_SMALL is defined in Config.src to ensure better "make config" order */
12
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010013//config:config FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
14//config: bool "Accept module options on modprobe command line"
15//config: default y
Explorer0980172e42017-01-09 15:04:43 +080016//config: depends on MODPROBE_SMALL && (INSMOD || MODPROBE)
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010017//config: help
18//config: Allow insmod and modprobe take module options from command line.
19//config:
20//config:config FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED
21//config: bool "Skip loading of already loaded modules"
22//config: default y
Explorer0980172e42017-01-09 15:04:43 +080023//config: depends on MODPROBE_SMALL && (DEPMOD || INSMOD || MODPROBE)
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010024//config: help
25//config: Check if the module is already loaded.
Denis Vlasenko671691c2008-07-04 10:25:44 +000026
Denys Vlasenkoa1cd0d92016-12-23 15:12:27 +010027//applet:IF_MODPROBE(IF_MODPROBE_SMALL(APPLET(modprobe, BB_DIR_SBIN, BB_SUID_DROP)))
28//applet:IF_DEPMOD(IF_MODPROBE_SMALL(APPLET_ODDNAME(depmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, depmod)))
29//applet:IF_INSMOD(IF_MODPROBE_SMALL(APPLET_ODDNAME(insmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, insmod)))
30//applet:IF_LSMOD(IF_MODPROBE_SMALL(APPLET_ODDNAME(lsmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, lsmod)))
31//applet:IF_RMMOD(IF_MODPROBE_SMALL(APPLET_ODDNAME(rmmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, rmmod)))
Denys Vlasenkoc15613c2010-10-15 11:29:02 +020032
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010033//kbuild:lib-$(CONFIG_MODPROBE_SMALL) += modprobe-small.o
34
Denis Vlasenko671691c2008-07-04 10:25:44 +000035#include "libbb.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +020036/* After libbb.h, since it needs sys/types.h on some systems */
Denis Vlasenko671691c2008-07-04 10:25:44 +000037#include <sys/utsname.h> /* uname() */
38#include <fnmatch.h>
Mike Frysinger3a45b872016-09-15 12:16:33 +020039#include <sys/syscall.h>
Denis Vlasenko671691c2008-07-04 10:25:44 +000040
Waldemar Brodkorb9c083f52016-12-26 20:07:59 +010041#define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
42#define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
Mike Frysinger3a45b872016-09-15 12:16:33 +020043#ifdef __NR_finit_module
44# define finit_module(fd, uargs, flags) syscall(__NR_finit_module, fd, uargs, flags)
45#endif
Denys Vlasenko07e55552014-04-21 16:59:36 +020046/* linux/include/linux/module.h has limit of 64 chars on module names */
47#undef MODULE_NAME_LEN
48#define MODULE_NAME_LEN 64
Denis Vlasenko24a131e2008-07-09 15:30:57 +000049
50
Denys Vlasenko5b3151c2010-09-25 14:37:06 +020051#if 1
52# define dbg1_error_msg(...) ((void)0)
53# define dbg2_error_msg(...) ((void)0)
54#else
55# define dbg1_error_msg(...) bb_error_msg(__VA_ARGS__)
56# define dbg2_error_msg(...) bb_error_msg(__VA_ARGS__)
57#endif
Denis Vlasenko671691c2008-07-04 10:25:44 +000058
Denis Vlasenko24a131e2008-07-09 15:30:57 +000059#define DEPFILE_BB CONFIG_DEFAULT_DEPMOD_FILE".bb"
Denis Vlasenko671691c2008-07-04 10:25:44 +000060
Denys Vlasenko6bf52b62017-01-09 09:48:58 +010061#define MOD_APPLET_CNT (ENABLE_MODPROBE + ENABLE_DEPMOD + ENABLE_INSMOD + ENABLE_LSMOD + ENABLE_RMMOD)
Denys Vlasenkoe7b54d02017-01-09 16:31:21 +010062
63/* Do not bother if MODPROBE_SMALL=y but no applets selected. */
64/* The rest of the file is in this if block. */
65#if MOD_APPLET_CNT > 0
66
67#define ONLY_APPLET (MOD_APPLET_CNT == 1)
Explorer0980172e42017-01-09 15:04:43 +080068#define is_modprobe (ENABLE_MODPROBE && (ONLY_APPLET || applet_name[0] == 'm'))
69#define is_depmod (ENABLE_DEPMOD && (ONLY_APPLET || applet_name[0] == 'd'))
70#define is_insmod (ENABLE_INSMOD && (ONLY_APPLET || applet_name[0] == 'i'))
71#define is_lsmod (ENABLE_LSMOD && (ONLY_APPLET || applet_name[0] == 'l'))
72#define is_rmmod (ENABLE_RMMOD && (ONLY_APPLET || applet_name[0] == 'r'))
73
Denis Vlasenko671691c2008-07-04 10:25:44 +000074enum {
75 OPT_q = (1 << 0), /* be quiet */
76 OPT_r = (1 << 1), /* module removal instead of loading */
77};
78
79typedef struct module_info {
80 char *pathname;
Denis Vlasenko78436992008-07-10 14:14:20 +000081 char *aliases;
82 char *deps;
Denys Vlasenko5c3e0602016-06-20 10:59:06 +020083 smallint open_read_failed;
Denis Vlasenko671691c2008-07-04 10:25:44 +000084} module_info;
85
86/*
87 * GLOBALS
88 */
89struct globals {
90 module_info *modinfo;
91 char *module_load_options;
Denis Vlasenko7f950a92008-07-10 14:14:45 +000092 smallint dep_bb_seen;
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +000093 smallint wrote_dep_bb_ok;
Denys Vlasenko0c6914e2009-09-07 02:38:26 +020094 unsigned module_count;
Denis Vlasenko671691c2008-07-04 10:25:44 +000095 int module_found_idx;
Denys Vlasenko0c6914e2009-09-07 02:38:26 +020096 unsigned stringbuf_idx;
97 unsigned stringbuf_size;
98 char *stringbuf; /* some modules have lots of stuff */
Denis Vlasenko671691c2008-07-04 10:25:44 +000099 /* for example, drivers/media/video/saa7134/saa7134.ko */
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200100 /* therefore having a fixed biggish buffer is not wise */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000101};
102#define G (*ptr_to_globals)
103#define modinfo (G.modinfo )
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000104#define dep_bb_seen (G.dep_bb_seen )
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000105#define wrote_dep_bb_ok (G.wrote_dep_bb_ok )
Denis Vlasenko671691c2008-07-04 10:25:44 +0000106#define module_count (G.module_count )
107#define module_found_idx (G.module_found_idx )
108#define module_load_options (G.module_load_options)
109#define stringbuf_idx (G.stringbuf_idx )
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200110#define stringbuf_size (G.stringbuf_size )
Denis Vlasenko671691c2008-07-04 10:25:44 +0000111#define stringbuf (G.stringbuf )
112#define INIT_G() do { \
113 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
114} while (0)
115
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200116static void append(const char *s)
117{
118 unsigned len = strlen(s);
119 if (stringbuf_idx + len + 15 > stringbuf_size) {
120 stringbuf_size = stringbuf_idx + len + 127;
121 dbg2_error_msg("grow stringbuf to %u", stringbuf_size);
122 stringbuf = xrealloc(stringbuf, stringbuf_size);
123 }
124 memcpy(stringbuf + stringbuf_idx, s, len);
125 stringbuf_idx += len;
126}
Denis Vlasenko671691c2008-07-04 10:25:44 +0000127
128static void appendc(char c)
129{
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200130 /* We appendc() only after append(), + 15 trick in append()
131 * makes it unnecessary to check for overflow here */
132 stringbuf[stringbuf_idx++] = c;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000133}
134
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000135static void bksp(void)
136{
137 if (stringbuf_idx)
138 stringbuf_idx--;
139}
140
Denis Vlasenko671691c2008-07-04 10:25:44 +0000141static void reset_stringbuf(void)
142{
143 stringbuf_idx = 0;
144}
145
146static char* copy_stringbuf(void)
147{
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200148 char *copy = xzalloc(stringbuf_idx + 1); /* terminating NUL */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000149 return memcpy(copy, stringbuf, stringbuf_idx);
150}
151
152static char* find_keyword(char *ptr, size_t len, const char *word)
153{
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000154 if (!ptr) /* happens if xmalloc_open_zipped_read_close cannot read it */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000155 return NULL;
156
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100157 len -= strlen(word) - 1;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000158 while ((ssize_t)len > 0) {
159 char *old = ptr;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100160 char *after_word;
161
Denis Vlasenko671691c2008-07-04 10:25:44 +0000162 /* search for the first char in word */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100163 ptr = memchr(ptr, word[0], len);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000164 if (ptr == NULL) /* no occurance left, done */
165 break;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100166 after_word = is_prefixed_with(ptr, word);
167 if (after_word)
168 return after_word; /* found, return ptr past it */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000169 ++ptr;
170 len -= (ptr - old);
171 }
172 return NULL;
173}
174
175static void replace(char *s, char what, char with)
176{
177 while (*s) {
178 if (what == *s)
179 *s = with;
180 ++s;
181 }
182}
183
Denys Vlasenko07e55552014-04-21 16:59:36 +0200184static char *filename2modname(const char *filename, char *modname)
185{
186 int i;
Denys Vlasenko78854522015-01-01 19:02:40 +0100187 const char *from;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200188
Denys Vlasenko78854522015-01-01 19:02:40 +0100189 // Disabled since otherwise "modprobe dir/name" would work
190 // as if it is "modprobe name". It is unclear why
191 // 'basenamization' was here in the first place.
192 //from = bb_get_last_path_component_nostrip(filename);
193 from = filename;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200194 for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++)
195 modname[i] = (from[i] == '-') ? '_' : from[i];
196 modname[i] = '\0';
197
198 return modname;
199}
200
Denys Vlasenko1b671532015-01-11 17:46:56 +0100201static int pathname_matches_modname(const char *pathname, const char *modname)
202{
203 int r;
204 char name[MODULE_NAME_LEN];
205 filename2modname(bb_get_last_path_component_nostrip(pathname), name);
206 r = (strcmp(name, modname) == 0);
207 return r;
208}
209
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000210/* Take "word word", return malloced "word",NUL,"word",NUL,NUL */
211static char* str_2_list(const char *str)
212{
213 int len = strlen(str) + 1;
214 char *dst = xmalloc(len + 1);
215
216 dst[len] = '\0';
217 memcpy(dst, str, len);
218//TODO: protect against 2+ spaces: "word word"
219 replace(dst, ' ', '\0');
220 return dst;
221}
222
Denis Vlasenko671691c2008-07-04 10:25:44 +0000223/* We use error numbers in a loose translation... */
224static const char *moderror(int err)
225{
226 switch (err) {
227 case ENOEXEC:
228 return "invalid module format";
229 case ENOENT:
230 return "unknown symbol in module or invalid parameter";
231 case ESRCH:
232 return "module has wrong symbol version";
233 case EINVAL: /* "invalid parameter" */
234 return "unknown symbol in module or invalid parameter"
235 + sizeof("unknown symbol in module or");
236 default:
237 return strerror(err);
238 }
239}
240
241static int load_module(const char *fname, const char *options)
242{
243#if 1
244 int r;
245 size_t len = MAXINT(ssize_t);
246 char *module_image;
Mike Frysinger3a45b872016-09-15 12:16:33 +0200247
248 if (!options)
249 options = "";
250
Denis Vlasenko671691c2008-07-04 10:25:44 +0000251 dbg1_error_msg("load_module('%s','%s')", fname, options);
252
Mike Frysinger3a45b872016-09-15 12:16:33 +0200253 /*
254 * First we try finit_module if available. Some kernels are configured
255 * to only allow loading of modules off of secure storage (like a read-
256 * only rootfs) which needs the finit_module call. If it fails, we fall
257 * back to normal module loading to support compressed modules.
258 */
259 r = 1;
260# ifdef __NR_finit_module
261 {
262 int fd = open(fname, O_RDONLY | O_CLOEXEC);
263 if (fd >= 0) {
264 r = finit_module(fd, options, 0) != 0;
265 close(fd);
266 }
267 }
268# endif
269 if (r != 0) {
270 module_image = xmalloc_open_zipped_read_close(fname, &len);
271 r = (!module_image || init_module(module_image, len, options) != 0);
272 free(module_image);
273 }
274
Denis Vlasenko671691c2008-07-04 10:25:44 +0000275 dbg1_error_msg("load_module:%d", r);
276 return r; /* 0 = success */
277#else
278 /* For testing */
279 dbg1_error_msg("load_module('%s','%s')", fname, options);
280 return 1;
281#endif
282}
283
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200284/* Returns !0 if open/read was unsuccessful */
285static int parse_module(module_info *info, const char *pathname)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000286{
287 char *module_image;
288 char *ptr;
289 size_t len;
290 size_t pos;
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000291 dbg1_error_msg("parse_module('%s')", pathname);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000292
293 /* Read (possibly compressed) module */
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200294 errno = 0;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000295 len = 64 * 1024 * 1024; /* 64 Mb at most */
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000296 module_image = xmalloc_open_zipped_read_close(pathname, &len);
Denys Vlasenko094cc512011-01-17 14:58:27 +0100297 /* module_image == NULL is ok here, find_keyword handles it */
Denis Vlasenko78436992008-07-10 14:14:20 +0000298//TODO: optimize redundant module body reads
Denis Vlasenko671691c2008-07-04 10:25:44 +0000299
Denis Vlasenko78436992008-07-10 14:14:20 +0000300 /* "alias1 symbol:sym1 alias2 symbol:sym2" */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000301 reset_stringbuf();
Denis Vlasenko671691c2008-07-04 10:25:44 +0000302 pos = 0;
303 while (1) {
Denys Vlasenko6116cb22014-04-19 16:17:27 +0200304 unsigned start = stringbuf_idx;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000305 ptr = find_keyword(module_image + pos, len - pos, "alias=");
306 if (!ptr) {
307 ptr = find_keyword(module_image + pos, len - pos, "__ksymtab_");
308 if (!ptr)
309 break;
310 /* DOCME: __ksymtab_gpl and __ksymtab_strings occur
311 * in many modules. What do they mean? */
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000312 if (strcmp(ptr, "gpl") == 0 || strcmp(ptr, "strings") == 0)
313 goto skip;
314 dbg2_error_msg("alias:'symbol:%s'", ptr);
315 append("symbol:");
Denis Vlasenko671691c2008-07-04 10:25:44 +0000316 } else {
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000317 dbg2_error_msg("alias:'%s'", ptr);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000318 }
319 append(ptr);
320 appendc(' ');
Denys Vlasenko6116cb22014-04-19 16:17:27 +0200321 /*
322 * Don't add redundant aliases, such as:
323 * libcrc32c.ko symbol:crc32c symbol:crc32c
324 */
325 if (start) { /* "if we aren't the first alias" */
326 char *found, *last;
327 stringbuf[stringbuf_idx] = '\0';
328 last = stringbuf + start;
329 /*
330 * String at last-1 is " symbol:crc32c "
331 * (with both leading and trailing spaces).
332 */
333 if (strncmp(stringbuf, last, stringbuf_idx - start) == 0)
334 /* First alias matches us */
335 found = stringbuf;
336 else
337 /* Does any other alias match? */
338 found = strstr(stringbuf, last-1);
339 if (found < last-1) {
340 /* There is absolutely the same string before us */
341 dbg2_error_msg("redundant:'%s'", last);
342 stringbuf_idx = start;
343 goto skip;
344 }
345 }
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000346 skip:
Denis Vlasenko671691c2008-07-04 10:25:44 +0000347 pos = (ptr - module_image);
348 }
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000349 bksp(); /* remove last ' ' */
Denis Vlasenko78436992008-07-10 14:14:20 +0000350 info->aliases = copy_stringbuf();
Denys Vlasenkof9c814b2009-09-07 02:37:19 +0200351 replace(info->aliases, '-', '_');
Denis Vlasenko671691c2008-07-04 10:25:44 +0000352
Denis Vlasenko78436992008-07-10 14:14:20 +0000353 /* "dependency1 depandency2" */
354 reset_stringbuf();
Denis Vlasenko671691c2008-07-04 10:25:44 +0000355 ptr = find_keyword(module_image, len, "depends=");
356 if (ptr && *ptr) {
357 replace(ptr, ',', ' ');
358 replace(ptr, '-', '_');
359 dbg2_error_msg("dep:'%s'", ptr);
360 append(ptr);
361 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200362 free(module_image);
Denis Vlasenko78436992008-07-10 14:14:20 +0000363 info->deps = copy_stringbuf();
Denis Vlasenko671691c2008-07-04 10:25:44 +0000364
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200365 info->open_read_failed = (module_image == NULL);
366 return info->open_read_failed;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000367}
368
Denis Vlasenko671691c2008-07-04 10:25:44 +0000369static FAST_FUNC int fileAction(const char *pathname,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000370 struct stat *sb UNUSED_PARAM,
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000371 void *modname_to_match,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000372 int depth UNUSED_PARAM)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000373{
374 int cur;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000375 const char *fname;
Explorer0980172e42017-01-09 15:04:43 +0800376 bool is_remove = (ENABLE_RMMOD && ONLY_APPLET)
377 || ((ENABLE_RMMOD || ENABLE_MODPROBE) && (option_mask32 & OPT_r));
Denis Vlasenko671691c2008-07-04 10:25:44 +0000378
379 pathname += 2; /* skip "./" */
380 fname = bb_get_last_path_component_nostrip(pathname);
381 if (!strrstr(fname, ".ko")) {
382 dbg1_error_msg("'%s' is not a module", pathname);
383 return TRUE; /* not a module, continue search */
384 }
385
386 cur = module_count++;
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000387 modinfo = xrealloc_vector(modinfo, 12, cur);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000388 modinfo[cur].pathname = xstrdup(pathname);
Denis Vlasenko27842282008-08-04 13:20:36 +0000389 /*modinfo[cur].aliases = NULL; - xrealloc_vector did it */
390 /*modinfo[cur+1].pathname = NULL;*/
Denis Vlasenko671691c2008-07-04 10:25:44 +0000391
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000392 if (!pathname_matches_modname(fname, modname_to_match)) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000393 dbg1_error_msg("'%s' module name doesn't match", pathname);
394 return TRUE; /* module name doesn't match, continue search */
395 }
396
397 dbg1_error_msg("'%s' module name matches", pathname);
398 module_found_idx = cur;
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200399 if (parse_module(&modinfo[cur], pathname) != 0)
400 return TRUE; /* failed to open/read it, no point in trying loading */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000401
Explorer0980172e42017-01-09 15:04:43 +0800402 if (!is_remove) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000403 if (load_module(pathname, module_load_options) == 0) {
404 /* Load was successful, there is nothing else to do.
405 * This can happen ONLY for "top-level" module load,
406 * not a dep, because deps dont do dirscan. */
407 exit(EXIT_SUCCESS);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000408 }
409 }
410
Denis Vlasenko671691c2008-07-04 10:25:44 +0000411 return TRUE;
412}
413
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000414static int load_dep_bb(void)
Denis Vlasenko78436992008-07-10 14:14:20 +0000415{
416 char *line;
Denis Vlasenko5415c852008-07-21 23:05:26 +0000417 FILE *fp = fopen_for_read(DEPFILE_BB);
Denis Vlasenko78436992008-07-10 14:14:20 +0000418
419 if (!fp)
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000420 return 0;
421
422 dep_bb_seen = 1;
423 dbg1_error_msg("loading "DEPFILE_BB);
424
425 /* Why? There is a rare scenario: we did not find modprobe.dep.bb,
426 * we scanned the dir and found no module by name, then we search
427 * for alias (full scan), and we decided to generate modprobe.dep.bb.
428 * But we see modprobe.dep.bb.new! Other modprobe is at work!
429 * We wait and other modprobe renames it to modprobe.dep.bb.
430 * Now we can use it.
431 * But we already have modinfo[] filled, and "module_count = 0"
432 * makes us start anew. Yes, we leak modinfo[].xxx pointers -
433 * there is not much of data there anyway. */
434 module_count = 0;
435 memset(&modinfo[0], 0, sizeof(modinfo[0]));
Denis Vlasenko78436992008-07-10 14:14:20 +0000436
437 while ((line = xmalloc_fgetline(fp)) != NULL) {
438 char* space;
Denys Vlasenko90a99042009-09-06 02:36:23 +0200439 char* linebuf;
Denis Vlasenko78436992008-07-10 14:14:20 +0000440 int cur;
441
442 if (!line[0]) {
443 free(line);
444 continue;
445 }
446 space = strchrnul(line, ' ');
447 cur = module_count++;
448 modinfo = xrealloc_vector(modinfo, 12, cur);
Denis Vlasenko27842282008-08-04 13:20:36 +0000449 /*modinfo[cur+1].pathname = NULL; - xrealloc_vector did it */
Denis Vlasenko78436992008-07-10 14:14:20 +0000450 modinfo[cur].pathname = line; /* we take ownership of malloced block here */
451 if (*space)
452 *space++ = '\0';
453 modinfo[cur].aliases = space;
Denys Vlasenko90a99042009-09-06 02:36:23 +0200454 linebuf = xmalloc_fgetline(fp);
455 modinfo[cur].deps = linebuf ? linebuf : xzalloc(1);
Denis Vlasenko78436992008-07-10 14:14:20 +0000456 if (modinfo[cur].deps[0]) {
457 /* deps are not "", so next line must be empty */
458 line = xmalloc_fgetline(fp);
459 /* Refuse to work with damaged config file */
460 if (line && line[0])
461 bb_error_msg_and_die("error in %s at '%s'", DEPFILE_BB, line);
462 free(line);
463 }
464 }
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000465 return 1;
466}
467
468static int start_dep_bb_writeout(void)
469{
470 int fd;
471
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000472 /* depmod -n: write result to stdout */
473 if (applet_name[0] == 'd' && (option_mask32 & 1))
474 return STDOUT_FILENO;
475
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000476 fd = open(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0644);
477 if (fd < 0) {
478 if (errno == EEXIST) {
479 int count = 5 * 20;
480 dbg1_error_msg(DEPFILE_BB".new exists, waiting for "DEPFILE_BB);
481 while (1) {
482 usleep(1000*1000 / 20);
483 if (load_dep_bb()) {
484 dbg1_error_msg(DEPFILE_BB" appeared");
485 return -2; /* magic number */
486 }
487 if (!--count)
488 break;
489 }
490 bb_error_msg("deleting stale %s", DEPFILE_BB".new");
491 fd = open_or_warn(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC);
492 }
493 }
494 dbg1_error_msg("opened "DEPFILE_BB".new:%d", fd);
495 return fd;
496}
497
498static void write_out_dep_bb(int fd)
499{
500 int i;
501 FILE *fp;
502
503 /* We want good error reporting. fdprintf is not good enough. */
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +0100504 fp = xfdopen_for_write(fd);
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000505 i = 0;
506 while (modinfo[i].pathname) {
507 fprintf(fp, "%s%s%s\n" "%s%s\n",
508 modinfo[i].pathname, modinfo[i].aliases[0] ? " " : "", modinfo[i].aliases,
509 modinfo[i].deps, modinfo[i].deps[0] ? "\n" : "");
510 i++;
511 }
512 /* Badly formatted depfile is a no-no. Be paranoid. */
513 errno = 0;
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000514 if (ferror(fp) | fclose(fp)) /* | instead of || is intended */
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000515 goto err;
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000516
517 if (fd == STDOUT_FILENO) /* it was depmod -n */
518 goto ok;
519
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000520 if (rename(DEPFILE_BB".new", DEPFILE_BB) != 0) {
521 err:
Denys Vlasenko651a2692010-03-23 16:25:17 +0100522 bb_perror_msg("can't create '%s'", DEPFILE_BB);
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000523 unlink(DEPFILE_BB".new");
524 } else {
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000525 ok:
526 wrote_dep_bb_ok = 1;
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000527 dbg1_error_msg("created "DEPFILE_BB);
528 }
Denis Vlasenko78436992008-07-10 14:14:20 +0000529}
530
Denys Vlasenko07e55552014-04-21 16:59:36 +0200531static module_info** find_alias(const char *alias)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000532{
533 int i;
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000534 int dep_bb_fd;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200535 int infoidx;
536 module_info **infovec;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000537 dbg1_error_msg("find_alias('%s')", alias);
538
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000539 try_again:
Denis Vlasenko671691c2008-07-04 10:25:44 +0000540 /* First try to find by name (cheaper) */
541 i = 0;
542 while (modinfo[i].pathname) {
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000543 if (pathname_matches_modname(modinfo[i].pathname, alias)) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000544 dbg1_error_msg("found '%s' in module '%s'",
545 alias, modinfo[i].pathname);
Denis Vlasenko78436992008-07-10 14:14:20 +0000546 if (!modinfo[i].aliases) {
547 parse_module(&modinfo[i], modinfo[i].pathname);
548 }
Denys Vlasenko07e55552014-04-21 16:59:36 +0200549 infovec = xzalloc(2 * sizeof(infovec[0]));
550 infovec[0] = &modinfo[i];
551 return infovec;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000552 }
Denis Vlasenko671691c2008-07-04 10:25:44 +0000553 i++;
554 }
555
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000556 /* Ok, we definitely have to scan module bodies. This is a good
557 * moment to generate modprobe.dep.bb, if it does not exist yet */
558 dep_bb_fd = dep_bb_seen ? -1 : start_dep_bb_writeout();
559 if (dep_bb_fd == -2) /* modprobe.dep.bb appeared? */
560 goto try_again;
561
Denis Vlasenko671691c2008-07-04 10:25:44 +0000562 /* Scan all module bodies, extract modinfo (it contains aliases) */
563 i = 0;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200564 infoidx = 0;
565 infovec = NULL;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000566 while (modinfo[i].pathname) {
567 char *desc, *s;
Denis Vlasenko78436992008-07-10 14:14:20 +0000568 if (!modinfo[i].aliases) {
569 parse_module(&modinfo[i], modinfo[i].pathname);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000570 }
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000571 /* "alias1 symbol:sym1 alias2 symbol:sym2" */
Denis Vlasenko78436992008-07-10 14:14:20 +0000572 desc = str_2_list(modinfo[i].aliases);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000573 /* Does matching substring exist? */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000574 for (s = desc; *s; s += strlen(s) + 1) {
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000575 /* Aliases in module bodies can be defined with
Denis Vlasenko58f59a22008-07-06 11:52:23 +0000576 * shell patterns. Example:
577 * "pci:v000010DEd000000D9sv*sd*bc*sc*i*".
578 * Plain strcmp() won't catch that */
579 if (fnmatch(s, alias, 0) == 0) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000580 dbg1_error_msg("found alias '%s' in module '%s'",
581 alias, modinfo[i].pathname);
Denys Vlasenko07e55552014-04-21 16:59:36 +0200582 infovec = xrealloc_vector(infovec, 1, infoidx);
583 infovec[infoidx++] = &modinfo[i];
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000584 break;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000585 }
586 }
587 free(desc);
588 i++;
589 }
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000590
591 /* Create module.dep.bb if needed */
592 if (dep_bb_fd >= 0) {
593 write_out_dep_bb(dep_bb_fd);
594 }
595
Denys Vlasenko07e55552014-04-21 16:59:36 +0200596 dbg1_error_msg("find_alias '%s' returns %d results", alias, infoidx);
597 return infovec;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000598}
599
600#if ENABLE_FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000601// TODO: open only once, invent config_rewind()
Denis Vlasenko671691c2008-07-04 10:25:44 +0000602static int already_loaded(const char *name)
603{
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100604 int ret;
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100605 char *line;
606 FILE *fp;
607
608 ret = 5 * 2;
609 again:
610 fp = fopen_for_read("/proc/modules");
611 if (!fp)
612 return 0;
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100613 while ((line = xmalloc_fgetline(fp)) != NULL) {
614 char *live;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100615 char *after_name;
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100616
617 // Examples from kernel 3.14.6:
618 //pcspkr 12718 0 - Live 0xffffffffa017e000
619 //snd_timer 28690 2 snd_seq,snd_pcm, Live 0xffffffffa025e000
620 //i915 801405 2 - Live 0xffffffffa0096000
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100621 after_name = is_prefixed_with(line, name);
622 if (!after_name || *after_name != ' ') {
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100623 free(line);
624 continue;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000625 }
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100626 live = strstr(line, " Live");
627 free(line);
628 if (!live) {
629 /* State can be Unloading, Loading, or Live.
630 * modprobe must not return prematurely if we see "Loading":
631 * it can cause further programs to assume load completed,
632 * but it did not (yet)!
633 * Wait up to 5*20 ms for it to resolve.
634 */
635 ret -= 2;
636 if (ret == 0)
637 break; /* huh? report as "not loaded" */
638 fclose(fp);
639 usleep(20*1000);
640 goto again;
641 }
642 ret = 1;
643 break;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000644 }
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100645 fclose(fp);
646
Denys Vlasenko402afe12015-02-13 11:53:33 +0100647 return ret & 1;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000648}
649#else
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100650#define already_loaded(name) 0
Denis Vlasenko671691c2008-07-04 10:25:44 +0000651#endif
652
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100653static int rmmod(const char *filename)
654{
655 int r;
656 char modname[MODULE_NAME_LEN];
657
658 filename2modname(filename, modname);
659 r = delete_module(modname, O_NONBLOCK | O_EXCL);
660 dbg1_error_msg("delete_module('%s', O_NONBLOCK | O_EXCL):%d", modname, r);
661 if (r != 0 && !(option_mask32 & OPT_q)) {
662 bb_perror_msg("remove '%s'", modname);
663 }
664 return r;
665}
666
Denis Vlasenko671691c2008-07-04 10:25:44 +0000667/*
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000668 * Given modules definition and module name (or alias, or symbol)
669 * load/remove the module respecting dependencies.
670 * NB: also called by depmod with bogus name "/",
671 * just in order to force modprobe.dep.bb creation.
Denis Vlasenko671691c2008-07-04 10:25:44 +0000672*/
673#if !ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
674#define process_module(a,b) process_module(a)
675#define cmdline_options ""
676#endif
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200677static int process_module(char *name, const char *cmdline_options)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000678{
679 char *s, *deps, *options;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200680 module_info **infovec;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000681 module_info *info;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200682 int infoidx;
Explorer0980172e42017-01-09 15:04:43 +0800683 bool is_remove = (ENABLE_RMMOD && ONLY_APPLET)
684 || ((ENABLE_RMMOD || ENABLE_MODPROBE) && (option_mask32 & OPT_r));
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200685 int exitcode = EXIT_SUCCESS;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200686
Denis Vlasenko671691c2008-07-04 10:25:44 +0000687 dbg1_error_msg("process_module('%s','%s')", name, cmdline_options);
688
689 replace(name, '-', '_');
690
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100691 dbg1_error_msg("already_loaded:%d is_remove:%d", already_loaded(name), is_remove);
692
Explorer0980172e42017-01-09 15:04:43 +0800693 if (is_rmmod) {
694 /* Does not remove dependencies, no need to scan, just remove.
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100695 * (compat note: this allows and strips .ko suffix)
696 */
697 rmmod(name);
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200698 return EXIT_SUCCESS;
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100699 }
700
Denys Vlasenko07e55552014-04-21 16:59:36 +0200701 /*
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100702 * We used to have "is_remove != already_loaded(name)" check here, but
Denys Vlasenko07e55552014-04-21 16:59:36 +0200703 * modprobe -r pci:v00008086d00007010sv00000000sd00000000bc01sc01i80
704 * won't unload modules (there are more than one)
705 * which have this alias.
706 */
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100707 if (!is_remove && already_loaded(name)) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000708 dbg1_error_msg("nothing to do for '%s'", name);
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200709 return EXIT_SUCCESS;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000710 }
711
712 options = NULL;
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100713 if (!is_remove) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000714 char *opt_filename = xasprintf("/etc/modules/%s", name);
715 options = xmalloc_open_read_close(opt_filename, NULL);
716 if (options)
717 replace(options, '\n', ' ');
718#if ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
719 if (cmdline_options) {
720 /* NB: cmdline_options always have one leading ' '
721 * (see main()), we remove it here */
722 char *op = xasprintf(options ? "%s %s" : "%s %s" + 3,
723 cmdline_options + 1, options);
724 free(options);
725 options = op;
726 }
727#endif
728 free(opt_filename);
729 module_load_options = options;
730 dbg1_error_msg("process_module('%s'): options:'%s'", name, options);
731 }
732
733 if (!module_count) {
734 /* Scan module directory. This is done only once.
735 * It will attempt module load, and will exit(EXIT_SUCCESS)
Denys Vlasenko07e55552014-04-21 16:59:36 +0200736 * on success.
737 */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000738 module_found_idx = -1;
739 recursive_action(".",
740 ACTION_RECURSE, /* flags */
741 fileAction, /* file action */
742 NULL, /* dir action */
743 name, /* user data */
Denys Vlasenko07e55552014-04-21 16:59:36 +0200744 0 /* depth */
745 );
Denis Vlasenko671691c2008-07-04 10:25:44 +0000746 dbg1_error_msg("dirscan complete");
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100747 /* Module was not found, or load failed, or is_remove */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000748 if (module_found_idx >= 0) { /* module was found */
Denys Vlasenko07e55552014-04-21 16:59:36 +0200749 infovec = xzalloc(2 * sizeof(infovec[0]));
750 infovec[0] = &modinfo[module_found_idx];
Denis Vlasenko671691c2008-07-04 10:25:44 +0000751 } else { /* search for alias, not a plain module name */
Denys Vlasenko07e55552014-04-21 16:59:36 +0200752 infovec = find_alias(name);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000753 }
754 } else {
Denys Vlasenko07e55552014-04-21 16:59:36 +0200755 infovec = find_alias(name);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000756 }
757
Denys Vlasenko3c75b1c2015-01-11 17:40:30 +0100758 if (!infovec) {
759 /* both dirscan and find_alias found nothing */
Explorer0980172e42017-01-09 15:04:43 +0800760 if (!is_remove && !is_depmod) /* it wasn't rmmod or depmod */
Denys Vlasenko3c75b1c2015-01-11 17:40:30 +0100761 bb_error_msg("module '%s' not found", name);
762//TODO: _and_die()? or should we continue (un)loading modules listed on cmdline?
763 goto ret;
764 }
765
Denys Vlasenko07e55552014-04-21 16:59:36 +0200766 /* There can be more than one module for the given alias. For example,
767 * "pci:v00008086d00007010sv00000000sd00000000bc01sc01i80" matches
768 * ata_piix because it has alias "pci:v00008086d00007010sv*sd*bc*sc*i*"
769 * and ata_generic, it has alias "pci:v*d*sv*sd*bc01sc01i*"
770 * Standard modprobe loads them both. We achieve it by returning
771 * a *list* of modinfo pointers from find_alias().
772 */
Denys Vlasenko63321512009-10-08 22:54:41 +0200773
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100774 /* modprobe -r? unload module(s) */
775 if (is_remove) {
Denys Vlasenko07e55552014-04-21 16:59:36 +0200776 infoidx = 0;
777 while ((info = infovec[infoidx++]) != NULL) {
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100778 int r = rmmod(bb_get_last_path_component_nostrip(info->pathname));
Denys Vlasenko07e55552014-04-21 16:59:36 +0200779 if (r != 0) {
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100780 goto ret; /* error */
Denys Vlasenko07e55552014-04-21 16:59:36 +0200781 }
Denis Vlasenko671691c2008-07-04 10:25:44 +0000782 }
Denys Vlasenko06a98e32012-09-25 20:37:38 +0200783 /* modprobe -r: we do not stop here -
Denis Vlasenko671691c2008-07-04 10:25:44 +0000784 * continue to unload modules on which the module depends:
785 * "-r --remove: option causes modprobe to remove a module.
786 * If the modules it depends on are also unused, modprobe
Denys Vlasenko06a98e32012-09-25 20:37:38 +0200787 * will try to remove them, too."
788 */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000789 }
790
Denys Vlasenko07e55552014-04-21 16:59:36 +0200791 infoidx = 0;
792 while ((info = infovec[infoidx++]) != NULL) {
793 /* Iterate thru dependencies, trying to (un)load them */
794 deps = str_2_list(info->deps);
795 for (s = deps; *s; s += strlen(s) + 1) {
796 //if (strcmp(name, s) != 0) // N.B. do loops exist?
797 dbg1_error_msg("recurse on dep '%s'", s);
798 process_module(s, NULL);
799 dbg1_error_msg("recurse on dep '%s' done", s);
800 }
801 free(deps);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000802
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100803 if (is_remove)
Denys Vlasenko07e55552014-04-21 16:59:36 +0200804 continue;
805
806 /* We are modprobe: load it */
807 if (options && strstr(options, "blacklist")) {
Denis Vlasenko1ad4db12008-11-12 00:09:58 +0000808 dbg1_error_msg("'%s': blacklisted", info->pathname);
Denys Vlasenko07e55552014-04-21 16:59:36 +0200809 continue;
810 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200811 if (info->open_read_failed) {
812 /* We already tried it, didn't work. Don't try load again */
813 exitcode = EXIT_FAILURE;
814 continue;
815 }
Denys Vlasenko07e55552014-04-21 16:59:36 +0200816 errno = 0;
817 if (load_module(info->pathname, options) != 0) {
818 if (EEXIST != errno) {
819 bb_error_msg("'%s': %s",
820 info->pathname,
821 moderror(errno));
822 } else {
823 dbg1_error_msg("'%s': %s",
824 info->pathname,
825 moderror(errno));
826 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200827 exitcode = EXIT_FAILURE;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000828 }
829 }
830 ret:
Denys Vlasenko07e55552014-04-21 16:59:36 +0200831 free(infovec);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000832 free(options);
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200833
834 return exitcode;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000835}
836#undef cmdline_options
837
838
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000839/* For reference, module-init-tools v3.4 options:
Denis Vlasenko671691c2008-07-04 10:25:44 +0000840
841# insmod
842Usage: insmod filename [args]
843
844# rmmod --help
845Usage: rmmod [-fhswvV] modulename ...
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000846 -f (or --force) forces a module unload, and may crash your
847 machine. This requires the Forced Module Removal option
848 when the kernel was compiled.
849 -h (or --help) prints this help text
Denis Vlasenko671691c2008-07-04 10:25:44 +0000850 -s (or --syslog) says use syslog, not stderr
851 -v (or --verbose) enables more messages
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000852 -V (or --version) prints the version code
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000853 -w (or --wait) begins module removal even if it is used
Denis Vlasenko671691c2008-07-04 10:25:44 +0000854 and will stop new users from accessing the module (so it
855 should eventually fall to zero).
856
857# modprobe
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000858Usage: modprobe [-v] [-V] [-C config-file] [-n] [-i] [-q] [-b]
859 [-o <modname>] [ --dump-modversions ] <modname> [parameters...]
860modprobe -r [-n] [-i] [-v] <modulename> ...
861modprobe -l -t <dirname> [ -a <modulename> ...]
Denis Vlasenko671691c2008-07-04 10:25:44 +0000862
863# depmod --help
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000864depmod 3.4 -- part of module-init-tools
865depmod -[aA] [-n -e -v -q -V -r -u]
866 [-b basedirectory] [forced_version]
867depmod [-n -e -v -q -r -u] [-F kernelsyms] module1.ko module2.ko ...
868If no arguments (except options) are given, "depmod -a" is assumed.
Denys Vlasenkobf2af9a2009-05-25 04:15:37 +0200869depmod will output a dependency list suitable for the modprobe utility.
Denis Vlasenko671691c2008-07-04 10:25:44 +0000870Options:
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000871 -a, --all Probe all modules
872 -A, --quick Only does the work if there's a new module
873 -n, --show Write the dependency file on stdout only
874 -e, --errsyms Report not supplied symbols
875 -V, --version Print the release version
876 -v, --verbose Enable verbose mode
877 -h, --help Print this usage message
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000878The following options are useful for people managing distributions:
879 -b basedirectory
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000880 --basedir basedirectory
881 Use an image of a module tree
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000882 -F kernelsyms
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000883 --filesyms kernelsyms
884 Use the file instead of the current kernel symbols
Denis Vlasenko671691c2008-07-04 10:25:44 +0000885*/
886
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200887//usage:#if ENABLE_MODPROBE_SMALL
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200888
889//usage:#define depmod_trivial_usage NOUSAGE_STR
890//usage:#define depmod_full_usage ""
891
892//usage:#define lsmod_trivial_usage
893//usage: ""
894//usage:#define lsmod_full_usage "\n\n"
895//usage: "List the currently loaded kernel modules"
896
897//usage:#define insmod_trivial_usage
898//usage: IF_FEATURE_2_4_MODULES("[OPTIONS] MODULE ")
899//usage: IF_NOT_FEATURE_2_4_MODULES("FILE ")
900//usage: "[SYMBOL=VALUE]..."
901//usage:#define insmod_full_usage "\n\n"
Denys Vlasenko5fd3ddf2014-04-19 15:04:39 +0200902//usage: "Load kernel module"
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200903//usage: IF_FEATURE_2_4_MODULES( "\n"
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200904//usage: "\n -f Force module to load into the wrong kernel version"
905//usage: "\n -k Make module autoclean-able"
906//usage: "\n -v Verbose"
907//usage: "\n -q Quiet"
908//usage: "\n -L Lock: prevent simultaneous loads"
909//usage: IF_FEATURE_INSMOD_LOAD_MAP(
910//usage: "\n -m Output load map to stdout"
911//usage: )
912//usage: "\n -x Don't export externs"
913//usage: )
914
915//usage:#define rmmod_trivial_usage
916//usage: "[-wfa] [MODULE]..."
917//usage:#define rmmod_full_usage "\n\n"
918//usage: "Unload kernel modules\n"
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200919//usage: "\n -w Wait until the module is no longer used"
920//usage: "\n -f Force unload"
921//usage: "\n -a Remove all unused modules (recursively)"
922//usage:
923//usage:#define rmmod_example_usage
924//usage: "$ rmmod tulip\n"
925
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200926//usage:#define modprobe_trivial_usage
Denys Vlasenko5fd3ddf2014-04-19 15:04:39 +0200927//usage: "[-qfwrsv] MODULE [SYMBOL=VALUE]..."
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200928//usage:#define modprobe_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +0200929//usage: " -r Remove MODULE (stacks) or do autoclean"
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200930//usage: "\n -q Quiet"
931//usage: "\n -v Verbose"
932//usage: "\n -f Force"
933//usage: "\n -w Wait for unload"
934//usage: "\n -s Report via syslog instead of stderr"
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200935
936//usage:#endif
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200937
Denis Vlasenko671691c2008-07-04 10:25:44 +0000938int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000939int modprobe_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000940{
Denys Vlasenko6bf52b62017-01-09 09:48:58 +0100941#if ENABLE_MODPROBE || ENABLE_INSMOD || ENABLE_RMMOD
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200942 int exitcode;
Denys Vlasenko6bf52b62017-01-09 09:48:58 +0100943#endif
Denis Vlasenko671691c2008-07-04 10:25:44 +0000944 struct utsname uts;
Explorer0980172e42017-01-09 15:04:43 +0800945 IF_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(char *options = NULL;)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000946
Denis Vlasenko671691c2008-07-04 10:25:44 +0000947 /* are we lsmod? -> just dump /proc/modules */
Explorer0980172e42017-01-09 15:04:43 +0800948 if (is_lsmod) {
Denis Vlasenko5415c852008-07-21 23:05:26 +0000949 xprint_and_close_file(xfopen_for_read("/proc/modules"));
Denis Vlasenko671691c2008-07-04 10:25:44 +0000950 return EXIT_SUCCESS;
951 }
952
953 INIT_G();
954
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000955 /* Prevent ugly corner cases with no modules at all */
956 modinfo = xzalloc(sizeof(modinfo[0]));
957
Explorer0980172e42017-01-09 15:04:43 +0800958 if (!is_insmod) {
Denis Vlasenko1c781cc2008-09-06 14:14:01 +0000959 /* Goto modules directory */
960 xchdir(CONFIG_DEFAULT_MODULES_DIR);
961 }
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000962 uname(&uts); /* never fails */
963
Denys Vlasenko6bf52b62017-01-09 09:48:58 +0100964 /* depmod? */
965 if ((MOD_APPLET_CNT == 2 && ENABLE_DEPMOD && ENABLE_LSMOD)
966 /* ^^^^only depmod and lsmod is configured^^^^^^^^^^^^^^^ */
967 /* note: we already know here it is not lsmod (handled before) */
968 || is_depmod
969 ) {
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000970 /* Supported:
971 * -n: print result to stdout
972 * -a: process all modules (default)
973 * optional VERSION parameter
974 * Ignored:
975 * -A: do work only if a module is newer than depfile
976 * -e: report any symbols which a module needs
977 * which are not supplied by other modules or the kernel
978 * -F FILE: System.map (symbols for -e)
979 * -q, -r, -u: noop?
980 * Not supported:
981 * -b BASEDIR: (TODO!) modules are in
982 * $BASEDIR/lib/modules/$VERSION
983 * -v: human readable deps to stdout
984 * -V: version (don't want to support it - people may depend
985 * on it as an indicator of "standard" depmod)
986 * -h: help (well duh)
987 * module1.o module2.o parameters (just ignored for now)
988 */
989 getopt32(argv, "na" "AeF:qru" /* "b:vV", NULL */, NULL);
990 argv += optind;
991 /* if (argv[0] && argv[1]) bb_show_usage(); */
992 /* Goto $VERSION directory */
993 xchdir(argv[0] ? argv[0] : uts.release);
994 /* Force full module scan by asking to find a bogus module.
995 * This will generate modules.dep.bb as a side effect. */
996 process_module((char*)"/", NULL);
997 return !wrote_dep_bb_ok;
998 }
999
Explorer0980172e42017-01-09 15:04:43 +08001000#if ENABLE_MODPROBE || ENABLE_INSMOD || ENABLE_RMMOD
1001 /* modprobe, insmod, rmmod require at least one argument */
Denis Vlasenko671691c2008-07-04 10:25:44 +00001002 opt_complementary = "-1";
1003 /* only -q (quiet) and -r (rmmod),
1004 * the rest are accepted and ignored (compat) */
Felipe Contreras428bd2d2012-01-31 14:55:15 +01001005 getopt32(argv, "qrfsvwb");
Denis Vlasenko671691c2008-07-04 10:25:44 +00001006 argv += optind;
1007
Explorer0980172e42017-01-09 15:04:43 +08001008 if (!is_insmod) {
Denis Vlasenko1c781cc2008-09-06 14:14:01 +00001009 /* Goto $VERSION directory */
1010 xchdir(uts.release);
1011 }
Denis Vlasenko671691c2008-07-04 10:25:44 +00001012
Explorer0980172e42017-01-09 15:04:43 +08001013 /* are we rmmod? -> simulate modprobe -r, but don't bother the flag if
1014 * there're no other applets here */
1015 if (is_rmmod) {
1016 if (!ONLY_APPLET)
1017 option_mask32 |= OPT_r;
1018 } else if (!ENABLE_MODPROBE || !(option_mask32 & OPT_r)) {
1019# if ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
Denys Vlasenko68c048f2015-02-07 20:44:46 +01001020 /* If not rmmod/-r, parse possible module options given on command line.
Denis Vlasenko671691c2008-07-04 10:25:44 +00001021 * insmod/modprobe takes one module name, the rest are parameters. */
Denis Vlasenko671691c2008-07-04 10:25:44 +00001022 char **arg = argv;
1023 while (*++arg) {
1024 /* Enclose options in quotes */
1025 char *s = options;
1026 options = xasprintf("%s \"%s\"", s ? s : "", *arg);
1027 free(s);
1028 *arg = NULL;
1029 }
Explorer0980172e42017-01-09 15:04:43 +08001030# else
Denis Vlasenko671691c2008-07-04 10:25:44 +00001031 argv[1] = NULL;
Explorer0980172e42017-01-09 15:04:43 +08001032# endif
1033 }
Denis Vlasenko671691c2008-07-04 10:25:44 +00001034
Explorer0980172e42017-01-09 15:04:43 +08001035 if (is_insmod) {
Denis Vlasenko1c781cc2008-09-06 14:14:01 +00001036 size_t len;
1037 void *map;
1038
1039 len = MAXINT(ssize_t);
Denys Vlasenkoe9d12b52011-01-09 20:57:52 +01001040 map = xmalloc_open_zipped_read_close(*argv, &len);
Denys Vlasenko094cc512011-01-17 14:58:27 +01001041 if (!map)
1042 bb_perror_msg_and_die("can't read '%s'", *argv);
Denis Vlasenko1c781cc2008-09-06 14:14:01 +00001043 if (init_module(map, len,
Explorer0980172e42017-01-09 15:04:43 +08001044 (IF_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(options ? options : ) "")
Denys Vlasenkoe9d12b52011-01-09 20:57:52 +01001045 ) != 0
1046 ) {
Denis Vlasenko1f632292009-04-13 02:25:40 +00001047 bb_error_msg_and_die("can't insert '%s': %s",
Denis Vlasenko1c781cc2008-09-06 14:14:01 +00001048 *argv, moderror(errno));
Denys Vlasenkoe9d12b52011-01-09 20:57:52 +01001049 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001050 return EXIT_SUCCESS;
Denis Vlasenko1c781cc2008-09-06 14:14:01 +00001051 }
1052
Denis Vlasenko7f950a92008-07-10 14:14:45 +00001053 /* Try to load modprobe.dep.bb */
Explorer0980172e42017-01-09 15:04:43 +08001054 if (!is_rmmod) {
Denys Vlasenko68c048f2015-02-07 20:44:46 +01001055 load_dep_bb();
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001056 }
Denis Vlasenko78436992008-07-10 14:14:20 +00001057
Denis Vlasenko671691c2008-07-04 10:25:44 +00001058 /* Load/remove modules.
Denys Vlasenko68c048f2015-02-07 20:44:46 +01001059 * Only rmmod/modprobe -r loops here, insmod/modprobe has only argv[0] */
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001060 exitcode = EXIT_SUCCESS;
Denis Vlasenko671691c2008-07-04 10:25:44 +00001061 do {
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001062 exitcode |= process_module(*argv, options);
Denys Vlasenko5b3151c2010-09-25 14:37:06 +02001063 } while (*++argv);
Denis Vlasenko671691c2008-07-04 10:25:44 +00001064
1065 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001066 IF_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(free(options);)
Denis Vlasenko671691c2008-07-04 10:25:44 +00001067 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001068 return exitcode;
Explorer0980172e42017-01-09 15:04:43 +08001069#endif /* MODPROBE || INSMOD || RMMOD */
Denis Vlasenko671691c2008-07-04 10:25:44 +00001070}
Denys Vlasenkoe7b54d02017-01-09 16:31:21 +01001071
1072#endif /* MOD_APPLET_CNT > 0 */