blob: 51ba42f7ac135f36c5cde4f965753797cc0dcbd6 [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 Vlasenkoe32b64c2016-11-23 07:54:52 +010010//config:config MODPROBE_SMALL
11//config: bool "Simplified modutils"
12//config: default y
13//config: select PLATFORM_LINUX
14//config: help
15//config: Simplified modutils.
16//config:
17//config: With this option modprobe does not require modules.dep file
18//config: and does not use /etc/modules.conf file.
19//config: It scans module files in /lib/modules/`uname -r` and
20//config: determines dependencies and module alias names on the fly.
21//config: This may make module loading slower, most notably
22//config: when one needs to load module by alias (this requires
23//config: scanning through module _bodies_).
24//config:
25//config: At the first attempt to load a module by alias modprobe
26//config: will try to generate modules.dep.bb file in order to speed up
27//config: future loads by alias. Failure to do so (read-only /lib/modules,
28//config: etc) is not reported, and future modprobes will be slow too.
29//config:
30//config: NB: modules.dep.bb file format is not compatible
31//config: with modules.dep file as created/used by standard module tools.
32//config:
33//config: Additional module parameters can be stored in
34//config: /etc/modules/$module_name files.
35//config:
36//config: Apart from modprobe, other utilities are also provided:
37//config: - insmod is an alias to modprobe
38//config: - rmmod is an alias to modprobe -r
39//config: - depmod generates modules.dep.bb
40//config:
41//config:config FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
42//config: bool "Accept module options on modprobe command line"
43//config: default y
44//config: depends on MODPROBE_SMALL
45//config: select PLATFORM_LINUX
46//config: help
47//config: Allow insmod and modprobe take module options from command line.
48//config:
49//config:config FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED
50//config: bool "Skip loading of already loaded modules"
51//config: default y
52//config: depends on MODPROBE_SMALL
53//config: help
54//config: Check if the module is already loaded.
Denis Vlasenko671691c2008-07-04 10:25:44 +000055
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +010056//applet:IF_MODPROBE_SMALL(APPLET(modprobe, BB_DIR_SBIN, BB_SUID_DROP))
Denys Vlasenko5fd3ddf2014-04-19 15:04:39 +020057//applet:IF_MODPROBE_SMALL(APPLET_ODDNAME(depmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, depmod))
58//applet:IF_MODPROBE_SMALL(APPLET_ODDNAME(insmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, insmod))
59//applet:IF_MODPROBE_SMALL(APPLET_ODDNAME(lsmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, lsmod))
60//applet:IF_MODPROBE_SMALL(APPLET_ODDNAME(rmmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, rmmod))
Denys Vlasenkoc15613c2010-10-15 11:29:02 +020061
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010062//kbuild:lib-$(CONFIG_MODPROBE_SMALL) += modprobe-small.o
63
Denis Vlasenko671691c2008-07-04 10:25:44 +000064#include "libbb.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +020065/* After libbb.h, since it needs sys/types.h on some systems */
Denis Vlasenko671691c2008-07-04 10:25:44 +000066#include <sys/utsname.h> /* uname() */
67#include <fnmatch.h>
Mike Frysinger3a45b872016-09-15 12:16:33 +020068#include <sys/syscall.h>
Denis Vlasenko671691c2008-07-04 10:25:44 +000069
Denis Vlasenko24a131e2008-07-09 15:30:57 +000070extern int init_module(void *module, unsigned long len, const char *options);
71extern int delete_module(const char *module, unsigned flags);
Mike Frysinger3a45b872016-09-15 12:16:33 +020072#ifdef __NR_finit_module
73# define finit_module(fd, uargs, flags) syscall(__NR_finit_module, fd, uargs, flags)
74#endif
Denys Vlasenko07e55552014-04-21 16:59:36 +020075/* linux/include/linux/module.h has limit of 64 chars on module names */
76#undef MODULE_NAME_LEN
77#define MODULE_NAME_LEN 64
Denis Vlasenko24a131e2008-07-09 15:30:57 +000078
79
Denys Vlasenko5b3151c2010-09-25 14:37:06 +020080#if 1
81# define dbg1_error_msg(...) ((void)0)
82# define dbg2_error_msg(...) ((void)0)
83#else
84# define dbg1_error_msg(...) bb_error_msg(__VA_ARGS__)
85# define dbg2_error_msg(...) bb_error_msg(__VA_ARGS__)
86#endif
Denis Vlasenko671691c2008-07-04 10:25:44 +000087
Denis Vlasenko24a131e2008-07-09 15:30:57 +000088#define DEPFILE_BB CONFIG_DEFAULT_DEPMOD_FILE".bb"
Denis Vlasenko671691c2008-07-04 10:25:44 +000089
90enum {
91 OPT_q = (1 << 0), /* be quiet */
92 OPT_r = (1 << 1), /* module removal instead of loading */
93};
94
95typedef struct module_info {
96 char *pathname;
Denis Vlasenko78436992008-07-10 14:14:20 +000097 char *aliases;
98 char *deps;
Denys Vlasenko5c3e0602016-06-20 10:59:06 +020099 smallint open_read_failed;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000100} module_info;
101
102/*
103 * GLOBALS
104 */
105struct globals {
106 module_info *modinfo;
107 char *module_load_options;
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000108 smallint dep_bb_seen;
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000109 smallint wrote_dep_bb_ok;
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200110 unsigned module_count;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000111 int module_found_idx;
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200112 unsigned stringbuf_idx;
113 unsigned stringbuf_size;
114 char *stringbuf; /* some modules have lots of stuff */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000115 /* for example, drivers/media/video/saa7134/saa7134.ko */
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200116 /* therefore having a fixed biggish buffer is not wise */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000117};
118#define G (*ptr_to_globals)
119#define modinfo (G.modinfo )
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000120#define dep_bb_seen (G.dep_bb_seen )
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000121#define wrote_dep_bb_ok (G.wrote_dep_bb_ok )
Denis Vlasenko671691c2008-07-04 10:25:44 +0000122#define module_count (G.module_count )
123#define module_found_idx (G.module_found_idx )
124#define module_load_options (G.module_load_options)
125#define stringbuf_idx (G.stringbuf_idx )
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200126#define stringbuf_size (G.stringbuf_size )
Denis Vlasenko671691c2008-07-04 10:25:44 +0000127#define stringbuf (G.stringbuf )
128#define INIT_G() do { \
129 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
130} while (0)
131
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200132static void append(const char *s)
133{
134 unsigned len = strlen(s);
135 if (stringbuf_idx + len + 15 > stringbuf_size) {
136 stringbuf_size = stringbuf_idx + len + 127;
137 dbg2_error_msg("grow stringbuf to %u", stringbuf_size);
138 stringbuf = xrealloc(stringbuf, stringbuf_size);
139 }
140 memcpy(stringbuf + stringbuf_idx, s, len);
141 stringbuf_idx += len;
142}
Denis Vlasenko671691c2008-07-04 10:25:44 +0000143
144static void appendc(char c)
145{
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200146 /* We appendc() only after append(), + 15 trick in append()
147 * makes it unnecessary to check for overflow here */
148 stringbuf[stringbuf_idx++] = c;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000149}
150
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000151static void bksp(void)
152{
153 if (stringbuf_idx)
154 stringbuf_idx--;
155}
156
Denis Vlasenko671691c2008-07-04 10:25:44 +0000157static void reset_stringbuf(void)
158{
159 stringbuf_idx = 0;
160}
161
162static char* copy_stringbuf(void)
163{
Denys Vlasenko0c6914e2009-09-07 02:38:26 +0200164 char *copy = xzalloc(stringbuf_idx + 1); /* terminating NUL */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000165 return memcpy(copy, stringbuf, stringbuf_idx);
166}
167
168static char* find_keyword(char *ptr, size_t len, const char *word)
169{
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000170 if (!ptr) /* happens if xmalloc_open_zipped_read_close cannot read it */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000171 return NULL;
172
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100173 len -= strlen(word) - 1;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000174 while ((ssize_t)len > 0) {
175 char *old = ptr;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100176 char *after_word;
177
Denis Vlasenko671691c2008-07-04 10:25:44 +0000178 /* search for the first char in word */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100179 ptr = memchr(ptr, word[0], len);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000180 if (ptr == NULL) /* no occurance left, done */
181 break;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100182 after_word = is_prefixed_with(ptr, word);
183 if (after_word)
184 return after_word; /* found, return ptr past it */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000185 ++ptr;
186 len -= (ptr - old);
187 }
188 return NULL;
189}
190
191static void replace(char *s, char what, char with)
192{
193 while (*s) {
194 if (what == *s)
195 *s = with;
196 ++s;
197 }
198}
199
Denys Vlasenko07e55552014-04-21 16:59:36 +0200200static char *filename2modname(const char *filename, char *modname)
201{
202 int i;
Denys Vlasenko78854522015-01-01 19:02:40 +0100203 const char *from;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200204
Denys Vlasenko78854522015-01-01 19:02:40 +0100205 // Disabled since otherwise "modprobe dir/name" would work
206 // as if it is "modprobe name". It is unclear why
207 // 'basenamization' was here in the first place.
208 //from = bb_get_last_path_component_nostrip(filename);
209 from = filename;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200210 for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++)
211 modname[i] = (from[i] == '-') ? '_' : from[i];
212 modname[i] = '\0';
213
214 return modname;
215}
216
Denys Vlasenko1b671532015-01-11 17:46:56 +0100217static int pathname_matches_modname(const char *pathname, const char *modname)
218{
219 int r;
220 char name[MODULE_NAME_LEN];
221 filename2modname(bb_get_last_path_component_nostrip(pathname), name);
222 r = (strcmp(name, modname) == 0);
223 return r;
224}
225
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000226/* Take "word word", return malloced "word",NUL,"word",NUL,NUL */
227static char* str_2_list(const char *str)
228{
229 int len = strlen(str) + 1;
230 char *dst = xmalloc(len + 1);
231
232 dst[len] = '\0';
233 memcpy(dst, str, len);
234//TODO: protect against 2+ spaces: "word word"
235 replace(dst, ' ', '\0');
236 return dst;
237}
238
Denis Vlasenko671691c2008-07-04 10:25:44 +0000239/* We use error numbers in a loose translation... */
240static const char *moderror(int err)
241{
242 switch (err) {
243 case ENOEXEC:
244 return "invalid module format";
245 case ENOENT:
246 return "unknown symbol in module or invalid parameter";
247 case ESRCH:
248 return "module has wrong symbol version";
249 case EINVAL: /* "invalid parameter" */
250 return "unknown symbol in module or invalid parameter"
251 + sizeof("unknown symbol in module or");
252 default:
253 return strerror(err);
254 }
255}
256
257static int load_module(const char *fname, const char *options)
258{
259#if 1
260 int r;
261 size_t len = MAXINT(ssize_t);
262 char *module_image;
Mike Frysinger3a45b872016-09-15 12:16:33 +0200263
264 if (!options)
265 options = "";
266
Denis Vlasenko671691c2008-07-04 10:25:44 +0000267 dbg1_error_msg("load_module('%s','%s')", fname, options);
268
Mike Frysinger3a45b872016-09-15 12:16:33 +0200269 /*
270 * First we try finit_module if available. Some kernels are configured
271 * to only allow loading of modules off of secure storage (like a read-
272 * only rootfs) which needs the finit_module call. If it fails, we fall
273 * back to normal module loading to support compressed modules.
274 */
275 r = 1;
276# ifdef __NR_finit_module
277 {
278 int fd = open(fname, O_RDONLY | O_CLOEXEC);
279 if (fd >= 0) {
280 r = finit_module(fd, options, 0) != 0;
281 close(fd);
282 }
283 }
284# endif
285 if (r != 0) {
286 module_image = xmalloc_open_zipped_read_close(fname, &len);
287 r = (!module_image || init_module(module_image, len, options) != 0);
288 free(module_image);
289 }
290
Denis Vlasenko671691c2008-07-04 10:25:44 +0000291 dbg1_error_msg("load_module:%d", r);
292 return r; /* 0 = success */
293#else
294 /* For testing */
295 dbg1_error_msg("load_module('%s','%s')", fname, options);
296 return 1;
297#endif
298}
299
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200300/* Returns !0 if open/read was unsuccessful */
301static int parse_module(module_info *info, const char *pathname)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000302{
303 char *module_image;
304 char *ptr;
305 size_t len;
306 size_t pos;
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000307 dbg1_error_msg("parse_module('%s')", pathname);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000308
309 /* Read (possibly compressed) module */
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200310 errno = 0;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000311 len = 64 * 1024 * 1024; /* 64 Mb at most */
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000312 module_image = xmalloc_open_zipped_read_close(pathname, &len);
Denys Vlasenko094cc512011-01-17 14:58:27 +0100313 /* module_image == NULL is ok here, find_keyword handles it */
Denis Vlasenko78436992008-07-10 14:14:20 +0000314//TODO: optimize redundant module body reads
Denis Vlasenko671691c2008-07-04 10:25:44 +0000315
Denis Vlasenko78436992008-07-10 14:14:20 +0000316 /* "alias1 symbol:sym1 alias2 symbol:sym2" */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000317 reset_stringbuf();
Denis Vlasenko671691c2008-07-04 10:25:44 +0000318 pos = 0;
319 while (1) {
Denys Vlasenko6116cb22014-04-19 16:17:27 +0200320 unsigned start = stringbuf_idx;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000321 ptr = find_keyword(module_image + pos, len - pos, "alias=");
322 if (!ptr) {
323 ptr = find_keyword(module_image + pos, len - pos, "__ksymtab_");
324 if (!ptr)
325 break;
326 /* DOCME: __ksymtab_gpl and __ksymtab_strings occur
327 * in many modules. What do they mean? */
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000328 if (strcmp(ptr, "gpl") == 0 || strcmp(ptr, "strings") == 0)
329 goto skip;
330 dbg2_error_msg("alias:'symbol:%s'", ptr);
331 append("symbol:");
Denis Vlasenko671691c2008-07-04 10:25:44 +0000332 } else {
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000333 dbg2_error_msg("alias:'%s'", ptr);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000334 }
335 append(ptr);
336 appendc(' ');
Denys Vlasenko6116cb22014-04-19 16:17:27 +0200337 /*
338 * Don't add redundant aliases, such as:
339 * libcrc32c.ko symbol:crc32c symbol:crc32c
340 */
341 if (start) { /* "if we aren't the first alias" */
342 char *found, *last;
343 stringbuf[stringbuf_idx] = '\0';
344 last = stringbuf + start;
345 /*
346 * String at last-1 is " symbol:crc32c "
347 * (with both leading and trailing spaces).
348 */
349 if (strncmp(stringbuf, last, stringbuf_idx - start) == 0)
350 /* First alias matches us */
351 found = stringbuf;
352 else
353 /* Does any other alias match? */
354 found = strstr(stringbuf, last-1);
355 if (found < last-1) {
356 /* There is absolutely the same string before us */
357 dbg2_error_msg("redundant:'%s'", last);
358 stringbuf_idx = start;
359 goto skip;
360 }
361 }
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000362 skip:
Denis Vlasenko671691c2008-07-04 10:25:44 +0000363 pos = (ptr - module_image);
364 }
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000365 bksp(); /* remove last ' ' */
Denis Vlasenko78436992008-07-10 14:14:20 +0000366 info->aliases = copy_stringbuf();
Denys Vlasenkof9c814b2009-09-07 02:37:19 +0200367 replace(info->aliases, '-', '_');
Denis Vlasenko671691c2008-07-04 10:25:44 +0000368
Denis Vlasenko78436992008-07-10 14:14:20 +0000369 /* "dependency1 depandency2" */
370 reset_stringbuf();
Denis Vlasenko671691c2008-07-04 10:25:44 +0000371 ptr = find_keyword(module_image, len, "depends=");
372 if (ptr && *ptr) {
373 replace(ptr, ',', ' ');
374 replace(ptr, '-', '_');
375 dbg2_error_msg("dep:'%s'", ptr);
376 append(ptr);
377 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200378 free(module_image);
Denis Vlasenko78436992008-07-10 14:14:20 +0000379 info->deps = copy_stringbuf();
Denis Vlasenko671691c2008-07-04 10:25:44 +0000380
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200381 info->open_read_failed = (module_image == NULL);
382 return info->open_read_failed;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000383}
384
Denis Vlasenko671691c2008-07-04 10:25:44 +0000385static FAST_FUNC int fileAction(const char *pathname,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000386 struct stat *sb UNUSED_PARAM,
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000387 void *modname_to_match,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000388 int depth UNUSED_PARAM)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000389{
390 int cur;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000391 const char *fname;
392
393 pathname += 2; /* skip "./" */
394 fname = bb_get_last_path_component_nostrip(pathname);
395 if (!strrstr(fname, ".ko")) {
396 dbg1_error_msg("'%s' is not a module", pathname);
397 return TRUE; /* not a module, continue search */
398 }
399
400 cur = module_count++;
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000401 modinfo = xrealloc_vector(modinfo, 12, cur);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000402 modinfo[cur].pathname = xstrdup(pathname);
Denis Vlasenko27842282008-08-04 13:20:36 +0000403 /*modinfo[cur].aliases = NULL; - xrealloc_vector did it */
404 /*modinfo[cur+1].pathname = NULL;*/
Denis Vlasenko671691c2008-07-04 10:25:44 +0000405
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000406 if (!pathname_matches_modname(fname, modname_to_match)) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000407 dbg1_error_msg("'%s' module name doesn't match", pathname);
408 return TRUE; /* module name doesn't match, continue search */
409 }
410
411 dbg1_error_msg("'%s' module name matches", pathname);
412 module_found_idx = cur;
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200413 if (parse_module(&modinfo[cur], pathname) != 0)
414 return TRUE; /* failed to open/read it, no point in trying loading */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000415
416 if (!(option_mask32 & OPT_r)) {
417 if (load_module(pathname, module_load_options) == 0) {
418 /* Load was successful, there is nothing else to do.
419 * This can happen ONLY for "top-level" module load,
420 * not a dep, because deps dont do dirscan. */
421 exit(EXIT_SUCCESS);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000422 }
423 }
424
Denis Vlasenko671691c2008-07-04 10:25:44 +0000425 return TRUE;
426}
427
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000428static int load_dep_bb(void)
Denis Vlasenko78436992008-07-10 14:14:20 +0000429{
430 char *line;
Denis Vlasenko5415c852008-07-21 23:05:26 +0000431 FILE *fp = fopen_for_read(DEPFILE_BB);
Denis Vlasenko78436992008-07-10 14:14:20 +0000432
433 if (!fp)
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000434 return 0;
435
436 dep_bb_seen = 1;
437 dbg1_error_msg("loading "DEPFILE_BB);
438
439 /* Why? There is a rare scenario: we did not find modprobe.dep.bb,
440 * we scanned the dir and found no module by name, then we search
441 * for alias (full scan), and we decided to generate modprobe.dep.bb.
442 * But we see modprobe.dep.bb.new! Other modprobe is at work!
443 * We wait and other modprobe renames it to modprobe.dep.bb.
444 * Now we can use it.
445 * But we already have modinfo[] filled, and "module_count = 0"
446 * makes us start anew. Yes, we leak modinfo[].xxx pointers -
447 * there is not much of data there anyway. */
448 module_count = 0;
449 memset(&modinfo[0], 0, sizeof(modinfo[0]));
Denis Vlasenko78436992008-07-10 14:14:20 +0000450
451 while ((line = xmalloc_fgetline(fp)) != NULL) {
452 char* space;
Denys Vlasenko90a99042009-09-06 02:36:23 +0200453 char* linebuf;
Denis Vlasenko78436992008-07-10 14:14:20 +0000454 int cur;
455
456 if (!line[0]) {
457 free(line);
458 continue;
459 }
460 space = strchrnul(line, ' ');
461 cur = module_count++;
462 modinfo = xrealloc_vector(modinfo, 12, cur);
Denis Vlasenko27842282008-08-04 13:20:36 +0000463 /*modinfo[cur+1].pathname = NULL; - xrealloc_vector did it */
Denis Vlasenko78436992008-07-10 14:14:20 +0000464 modinfo[cur].pathname = line; /* we take ownership of malloced block here */
465 if (*space)
466 *space++ = '\0';
467 modinfo[cur].aliases = space;
Denys Vlasenko90a99042009-09-06 02:36:23 +0200468 linebuf = xmalloc_fgetline(fp);
469 modinfo[cur].deps = linebuf ? linebuf : xzalloc(1);
Denis Vlasenko78436992008-07-10 14:14:20 +0000470 if (modinfo[cur].deps[0]) {
471 /* deps are not "", so next line must be empty */
472 line = xmalloc_fgetline(fp);
473 /* Refuse to work with damaged config file */
474 if (line && line[0])
475 bb_error_msg_and_die("error in %s at '%s'", DEPFILE_BB, line);
476 free(line);
477 }
478 }
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000479 return 1;
480}
481
482static int start_dep_bb_writeout(void)
483{
484 int fd;
485
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000486 /* depmod -n: write result to stdout */
487 if (applet_name[0] == 'd' && (option_mask32 & 1))
488 return STDOUT_FILENO;
489
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000490 fd = open(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0644);
491 if (fd < 0) {
492 if (errno == EEXIST) {
493 int count = 5 * 20;
494 dbg1_error_msg(DEPFILE_BB".new exists, waiting for "DEPFILE_BB);
495 while (1) {
496 usleep(1000*1000 / 20);
497 if (load_dep_bb()) {
498 dbg1_error_msg(DEPFILE_BB" appeared");
499 return -2; /* magic number */
500 }
501 if (!--count)
502 break;
503 }
504 bb_error_msg("deleting stale %s", DEPFILE_BB".new");
505 fd = open_or_warn(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC);
506 }
507 }
508 dbg1_error_msg("opened "DEPFILE_BB".new:%d", fd);
509 return fd;
510}
511
512static void write_out_dep_bb(int fd)
513{
514 int i;
515 FILE *fp;
516
517 /* We want good error reporting. fdprintf is not good enough. */
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +0100518 fp = xfdopen_for_write(fd);
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000519 i = 0;
520 while (modinfo[i].pathname) {
521 fprintf(fp, "%s%s%s\n" "%s%s\n",
522 modinfo[i].pathname, modinfo[i].aliases[0] ? " " : "", modinfo[i].aliases,
523 modinfo[i].deps, modinfo[i].deps[0] ? "\n" : "");
524 i++;
525 }
526 /* Badly formatted depfile is a no-no. Be paranoid. */
527 errno = 0;
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000528 if (ferror(fp) | fclose(fp)) /* | instead of || is intended */
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000529 goto err;
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000530
531 if (fd == STDOUT_FILENO) /* it was depmod -n */
532 goto ok;
533
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000534 if (rename(DEPFILE_BB".new", DEPFILE_BB) != 0) {
535 err:
Denys Vlasenko651a2692010-03-23 16:25:17 +0100536 bb_perror_msg("can't create '%s'", DEPFILE_BB);
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000537 unlink(DEPFILE_BB".new");
538 } else {
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000539 ok:
540 wrote_dep_bb_ok = 1;
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000541 dbg1_error_msg("created "DEPFILE_BB);
542 }
Denis Vlasenko78436992008-07-10 14:14:20 +0000543}
544
Denys Vlasenko07e55552014-04-21 16:59:36 +0200545static module_info** find_alias(const char *alias)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000546{
547 int i;
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000548 int dep_bb_fd;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200549 int infoidx;
550 module_info **infovec;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000551 dbg1_error_msg("find_alias('%s')", alias);
552
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000553 try_again:
Denis Vlasenko671691c2008-07-04 10:25:44 +0000554 /* First try to find by name (cheaper) */
555 i = 0;
556 while (modinfo[i].pathname) {
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000557 if (pathname_matches_modname(modinfo[i].pathname, alias)) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000558 dbg1_error_msg("found '%s' in module '%s'",
559 alias, modinfo[i].pathname);
Denis Vlasenko78436992008-07-10 14:14:20 +0000560 if (!modinfo[i].aliases) {
561 parse_module(&modinfo[i], modinfo[i].pathname);
562 }
Denys Vlasenko07e55552014-04-21 16:59:36 +0200563 infovec = xzalloc(2 * sizeof(infovec[0]));
564 infovec[0] = &modinfo[i];
565 return infovec;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000566 }
Denis Vlasenko671691c2008-07-04 10:25:44 +0000567 i++;
568 }
569
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000570 /* Ok, we definitely have to scan module bodies. This is a good
571 * moment to generate modprobe.dep.bb, if it does not exist yet */
572 dep_bb_fd = dep_bb_seen ? -1 : start_dep_bb_writeout();
573 if (dep_bb_fd == -2) /* modprobe.dep.bb appeared? */
574 goto try_again;
575
Denis Vlasenko671691c2008-07-04 10:25:44 +0000576 /* Scan all module bodies, extract modinfo (it contains aliases) */
577 i = 0;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200578 infoidx = 0;
579 infovec = NULL;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000580 while (modinfo[i].pathname) {
581 char *desc, *s;
Denis Vlasenko78436992008-07-10 14:14:20 +0000582 if (!modinfo[i].aliases) {
583 parse_module(&modinfo[i], modinfo[i].pathname);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000584 }
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000585 /* "alias1 symbol:sym1 alias2 symbol:sym2" */
Denis Vlasenko78436992008-07-10 14:14:20 +0000586 desc = str_2_list(modinfo[i].aliases);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000587 /* Does matching substring exist? */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000588 for (s = desc; *s; s += strlen(s) + 1) {
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000589 /* Aliases in module bodies can be defined with
Denis Vlasenko58f59a22008-07-06 11:52:23 +0000590 * shell patterns. Example:
591 * "pci:v000010DEd000000D9sv*sd*bc*sc*i*".
592 * Plain strcmp() won't catch that */
593 if (fnmatch(s, alias, 0) == 0) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000594 dbg1_error_msg("found alias '%s' in module '%s'",
595 alias, modinfo[i].pathname);
Denys Vlasenko07e55552014-04-21 16:59:36 +0200596 infovec = xrealloc_vector(infovec, 1, infoidx);
597 infovec[infoidx++] = &modinfo[i];
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000598 break;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000599 }
600 }
601 free(desc);
602 i++;
603 }
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000604
605 /* Create module.dep.bb if needed */
606 if (dep_bb_fd >= 0) {
607 write_out_dep_bb(dep_bb_fd);
608 }
609
Denys Vlasenko07e55552014-04-21 16:59:36 +0200610 dbg1_error_msg("find_alias '%s' returns %d results", alias, infoidx);
611 return infovec;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000612}
613
614#if ENABLE_FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000615// TODO: open only once, invent config_rewind()
Denis Vlasenko671691c2008-07-04 10:25:44 +0000616static int already_loaded(const char *name)
617{
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100618 int ret;
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100619 char *line;
620 FILE *fp;
621
622 ret = 5 * 2;
623 again:
624 fp = fopen_for_read("/proc/modules");
625 if (!fp)
626 return 0;
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100627 while ((line = xmalloc_fgetline(fp)) != NULL) {
628 char *live;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100629 char *after_name;
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100630
631 // Examples from kernel 3.14.6:
632 //pcspkr 12718 0 - Live 0xffffffffa017e000
633 //snd_timer 28690 2 snd_seq,snd_pcm, Live 0xffffffffa025e000
634 //i915 801405 2 - Live 0xffffffffa0096000
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100635 after_name = is_prefixed_with(line, name);
636 if (!after_name || *after_name != ' ') {
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100637 free(line);
638 continue;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000639 }
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100640 live = strstr(line, " Live");
641 free(line);
642 if (!live) {
643 /* State can be Unloading, Loading, or Live.
644 * modprobe must not return prematurely if we see "Loading":
645 * it can cause further programs to assume load completed,
646 * but it did not (yet)!
647 * Wait up to 5*20 ms for it to resolve.
648 */
649 ret -= 2;
650 if (ret == 0)
651 break; /* huh? report as "not loaded" */
652 fclose(fp);
653 usleep(20*1000);
654 goto again;
655 }
656 ret = 1;
657 break;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000658 }
Denys Vlasenko1fd84422015-02-12 16:18:39 +0100659 fclose(fp);
660
Denys Vlasenko402afe12015-02-13 11:53:33 +0100661 return ret & 1;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000662}
663#else
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100664#define already_loaded(name) 0
Denis Vlasenko671691c2008-07-04 10:25:44 +0000665#endif
666
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100667static int rmmod(const char *filename)
668{
669 int r;
670 char modname[MODULE_NAME_LEN];
671
672 filename2modname(filename, modname);
673 r = delete_module(modname, O_NONBLOCK | O_EXCL);
674 dbg1_error_msg("delete_module('%s', O_NONBLOCK | O_EXCL):%d", modname, r);
675 if (r != 0 && !(option_mask32 & OPT_q)) {
676 bb_perror_msg("remove '%s'", modname);
677 }
678 return r;
679}
680
Denis Vlasenko671691c2008-07-04 10:25:44 +0000681/*
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000682 * Given modules definition and module name (or alias, or symbol)
683 * load/remove the module respecting dependencies.
684 * NB: also called by depmod with bogus name "/",
685 * just in order to force modprobe.dep.bb creation.
Denis Vlasenko671691c2008-07-04 10:25:44 +0000686*/
687#if !ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
688#define process_module(a,b) process_module(a)
689#define cmdline_options ""
690#endif
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200691static int process_module(char *name, const char *cmdline_options)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000692{
693 char *s, *deps, *options;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200694 module_info **infovec;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000695 module_info *info;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200696 int infoidx;
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100697 int is_remove = (option_mask32 & OPT_r) != 0;
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200698 int exitcode = EXIT_SUCCESS;
Denys Vlasenko07e55552014-04-21 16:59:36 +0200699
Denis Vlasenko671691c2008-07-04 10:25:44 +0000700 dbg1_error_msg("process_module('%s','%s')", name, cmdline_options);
701
702 replace(name, '-', '_');
703
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100704 dbg1_error_msg("already_loaded:%d is_remove:%d", already_loaded(name), is_remove);
705
706 if (applet_name[0] == 'r') {
707 /* rmmod.
708 * Does not remove dependencies, no need to scan, just remove.
709 * (compat note: this allows and strips .ko suffix)
710 */
711 rmmod(name);
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200712 return EXIT_SUCCESS;
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100713 }
714
Denys Vlasenko07e55552014-04-21 16:59:36 +0200715 /*
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100716 * We used to have "is_remove != already_loaded(name)" check here, but
Denys Vlasenko07e55552014-04-21 16:59:36 +0200717 * modprobe -r pci:v00008086d00007010sv00000000sd00000000bc01sc01i80
718 * won't unload modules (there are more than one)
719 * which have this alias.
720 */
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100721 if (!is_remove && already_loaded(name)) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000722 dbg1_error_msg("nothing to do for '%s'", name);
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200723 return EXIT_SUCCESS;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000724 }
725
726 options = NULL;
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100727 if (!is_remove) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000728 char *opt_filename = xasprintf("/etc/modules/%s", name);
729 options = xmalloc_open_read_close(opt_filename, NULL);
730 if (options)
731 replace(options, '\n', ' ');
732#if ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
733 if (cmdline_options) {
734 /* NB: cmdline_options always have one leading ' '
735 * (see main()), we remove it here */
736 char *op = xasprintf(options ? "%s %s" : "%s %s" + 3,
737 cmdline_options + 1, options);
738 free(options);
739 options = op;
740 }
741#endif
742 free(opt_filename);
743 module_load_options = options;
744 dbg1_error_msg("process_module('%s'): options:'%s'", name, options);
745 }
746
747 if (!module_count) {
748 /* Scan module directory. This is done only once.
749 * It will attempt module load, and will exit(EXIT_SUCCESS)
Denys Vlasenko07e55552014-04-21 16:59:36 +0200750 * on success.
751 */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000752 module_found_idx = -1;
753 recursive_action(".",
754 ACTION_RECURSE, /* flags */
755 fileAction, /* file action */
756 NULL, /* dir action */
757 name, /* user data */
Denys Vlasenko07e55552014-04-21 16:59:36 +0200758 0 /* depth */
759 );
Denis Vlasenko671691c2008-07-04 10:25:44 +0000760 dbg1_error_msg("dirscan complete");
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100761 /* Module was not found, or load failed, or is_remove */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000762 if (module_found_idx >= 0) { /* module was found */
Denys Vlasenko07e55552014-04-21 16:59:36 +0200763 infovec = xzalloc(2 * sizeof(infovec[0]));
764 infovec[0] = &modinfo[module_found_idx];
Denis Vlasenko671691c2008-07-04 10:25:44 +0000765 } else { /* search for alias, not a plain module name */
Denys Vlasenko07e55552014-04-21 16:59:36 +0200766 infovec = find_alias(name);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000767 }
768 } else {
Denys Vlasenko07e55552014-04-21 16:59:36 +0200769 infovec = find_alias(name);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000770 }
771
Denys Vlasenko3c75b1c2015-01-11 17:40:30 +0100772 if (!infovec) {
773 /* both dirscan and find_alias found nothing */
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100774 if (!is_remove && applet_name[0] != 'd') /* it wasn't rmmod or depmod */
Denys Vlasenko3c75b1c2015-01-11 17:40:30 +0100775 bb_error_msg("module '%s' not found", name);
776//TODO: _and_die()? or should we continue (un)loading modules listed on cmdline?
777 goto ret;
778 }
779
Denys Vlasenko07e55552014-04-21 16:59:36 +0200780 /* There can be more than one module for the given alias. For example,
781 * "pci:v00008086d00007010sv00000000sd00000000bc01sc01i80" matches
782 * ata_piix because it has alias "pci:v00008086d00007010sv*sd*bc*sc*i*"
783 * and ata_generic, it has alias "pci:v*d*sv*sd*bc01sc01i*"
784 * Standard modprobe loads them both. We achieve it by returning
785 * a *list* of modinfo pointers from find_alias().
786 */
Denys Vlasenko63321512009-10-08 22:54:41 +0200787
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100788 /* modprobe -r? unload module(s) */
789 if (is_remove) {
Denys Vlasenko07e55552014-04-21 16:59:36 +0200790 infoidx = 0;
791 while ((info = infovec[infoidx++]) != NULL) {
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100792 int r = rmmod(bb_get_last_path_component_nostrip(info->pathname));
Denys Vlasenko07e55552014-04-21 16:59:36 +0200793 if (r != 0) {
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100794 goto ret; /* error */
Denys Vlasenko07e55552014-04-21 16:59:36 +0200795 }
Denis Vlasenko671691c2008-07-04 10:25:44 +0000796 }
Denys Vlasenko06a98e32012-09-25 20:37:38 +0200797 /* modprobe -r: we do not stop here -
Denis Vlasenko671691c2008-07-04 10:25:44 +0000798 * continue to unload modules on which the module depends:
799 * "-r --remove: option causes modprobe to remove a module.
800 * If the modules it depends on are also unused, modprobe
Denys Vlasenko06a98e32012-09-25 20:37:38 +0200801 * will try to remove them, too."
802 */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000803 }
804
Denys Vlasenko07e55552014-04-21 16:59:36 +0200805 infoidx = 0;
806 while ((info = infovec[infoidx++]) != NULL) {
807 /* Iterate thru dependencies, trying to (un)load them */
808 deps = str_2_list(info->deps);
809 for (s = deps; *s; s += strlen(s) + 1) {
810 //if (strcmp(name, s) != 0) // N.B. do loops exist?
811 dbg1_error_msg("recurse on dep '%s'", s);
812 process_module(s, NULL);
813 dbg1_error_msg("recurse on dep '%s' done", s);
814 }
815 free(deps);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000816
Denys Vlasenko68c048f2015-02-07 20:44:46 +0100817 if (is_remove)
Denys Vlasenko07e55552014-04-21 16:59:36 +0200818 continue;
819
820 /* We are modprobe: load it */
821 if (options && strstr(options, "blacklist")) {
Denis Vlasenko1ad4db12008-11-12 00:09:58 +0000822 dbg1_error_msg("'%s': blacklisted", info->pathname);
Denys Vlasenko07e55552014-04-21 16:59:36 +0200823 continue;
824 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200825 if (info->open_read_failed) {
826 /* We already tried it, didn't work. Don't try load again */
827 exitcode = EXIT_FAILURE;
828 continue;
829 }
Denys Vlasenko07e55552014-04-21 16:59:36 +0200830 errno = 0;
831 if (load_module(info->pathname, options) != 0) {
832 if (EEXIST != errno) {
833 bb_error_msg("'%s': %s",
834 info->pathname,
835 moderror(errno));
836 } else {
837 dbg1_error_msg("'%s': %s",
838 info->pathname,
839 moderror(errno));
840 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200841 exitcode = EXIT_FAILURE;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000842 }
843 }
844 ret:
Denys Vlasenko07e55552014-04-21 16:59:36 +0200845 free(infovec);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000846 free(options);
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200847
848 return exitcode;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000849}
850#undef cmdline_options
851
852
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000853/* For reference, module-init-tools v3.4 options:
Denis Vlasenko671691c2008-07-04 10:25:44 +0000854
855# insmod
856Usage: insmod filename [args]
857
858# rmmod --help
859Usage: rmmod [-fhswvV] modulename ...
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000860 -f (or --force) forces a module unload, and may crash your
861 machine. This requires the Forced Module Removal option
862 when the kernel was compiled.
863 -h (or --help) prints this help text
Denis Vlasenko671691c2008-07-04 10:25:44 +0000864 -s (or --syslog) says use syslog, not stderr
865 -v (or --verbose) enables more messages
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000866 -V (or --version) prints the version code
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000867 -w (or --wait) begins module removal even if it is used
Denis Vlasenko671691c2008-07-04 10:25:44 +0000868 and will stop new users from accessing the module (so it
869 should eventually fall to zero).
870
871# modprobe
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000872Usage: modprobe [-v] [-V] [-C config-file] [-n] [-i] [-q] [-b]
873 [-o <modname>] [ --dump-modversions ] <modname> [parameters...]
874modprobe -r [-n] [-i] [-v] <modulename> ...
875modprobe -l -t <dirname> [ -a <modulename> ...]
Denis Vlasenko671691c2008-07-04 10:25:44 +0000876
877# depmod --help
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000878depmod 3.4 -- part of module-init-tools
879depmod -[aA] [-n -e -v -q -V -r -u]
880 [-b basedirectory] [forced_version]
881depmod [-n -e -v -q -r -u] [-F kernelsyms] module1.ko module2.ko ...
882If no arguments (except options) are given, "depmod -a" is assumed.
Denys Vlasenkobf2af9a2009-05-25 04:15:37 +0200883depmod will output a dependency list suitable for the modprobe utility.
Denis Vlasenko671691c2008-07-04 10:25:44 +0000884Options:
Denis Vlasenko35d8c472008-08-05 07:59:25 +0000885 -a, --all Probe all modules
886 -A, --quick Only does the work if there's a new module
887 -n, --show Write the dependency file on stdout only
888 -e, --errsyms Report not supplied symbols
889 -V, --version Print the release version
890 -v, --verbose Enable verbose mode
891 -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
Denis Vlasenko671691c2008-07-04 10:25:44 +0000899*/
900
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200901//usage:#if ENABLE_MODPROBE_SMALL
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200902
903//usage:#define depmod_trivial_usage NOUSAGE_STR
904//usage:#define depmod_full_usage ""
905
906//usage:#define lsmod_trivial_usage
907//usage: ""
908//usage:#define lsmod_full_usage "\n\n"
909//usage: "List the currently loaded kernel modules"
910
911//usage:#define insmod_trivial_usage
912//usage: IF_FEATURE_2_4_MODULES("[OPTIONS] MODULE ")
913//usage: IF_NOT_FEATURE_2_4_MODULES("FILE ")
914//usage: "[SYMBOL=VALUE]..."
915//usage:#define insmod_full_usage "\n\n"
Denys Vlasenko5fd3ddf2014-04-19 15:04:39 +0200916//usage: "Load kernel module"
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200917//usage: IF_FEATURE_2_4_MODULES( "\n"
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200918//usage: "\n -f Force module to load into the wrong kernel version"
919//usage: "\n -k Make module autoclean-able"
920//usage: "\n -v Verbose"
921//usage: "\n -q Quiet"
922//usage: "\n -L Lock: prevent simultaneous loads"
923//usage: IF_FEATURE_INSMOD_LOAD_MAP(
924//usage: "\n -m Output load map to stdout"
925//usage: )
926//usage: "\n -x Don't export externs"
927//usage: )
928
929//usage:#define rmmod_trivial_usage
930//usage: "[-wfa] [MODULE]..."
931//usage:#define rmmod_full_usage "\n\n"
932//usage: "Unload kernel modules\n"
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200933//usage: "\n -w Wait until the module is no longer used"
934//usage: "\n -f Force unload"
935//usage: "\n -a Remove all unused modules (recursively)"
936//usage:
937//usage:#define rmmod_example_usage
938//usage: "$ rmmod tulip\n"
939
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200940//usage:#define modprobe_trivial_usage
Denys Vlasenko5fd3ddf2014-04-19 15:04:39 +0200941//usage: "[-qfwrsv] MODULE [SYMBOL=VALUE]..."
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200942//usage:#define modprobe_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +0200943//usage: " -r Remove MODULE (stacks) or do autoclean"
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200944//usage: "\n -q Quiet"
945//usage: "\n -v Verbose"
946//usage: "\n -f Force"
947//usage: "\n -w Wait for unload"
948//usage: "\n -s Report via syslog instead of stderr"
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +0200949
950//usage:#endif
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200951
Denis Vlasenko671691c2008-07-04 10:25:44 +0000952int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000953int modprobe_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000954{
Denys Vlasenko5c3e0602016-06-20 10:59:06 +0200955 int exitcode;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000956 struct utsname uts;
957 char applet0 = applet_name[0];
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000958 IF_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(char *options;)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000959
Denis Vlasenko671691c2008-07-04 10:25:44 +0000960 /* are we lsmod? -> just dump /proc/modules */
961 if ('l' == applet0) {
Denis Vlasenko5415c852008-07-21 23:05:26 +0000962 xprint_and_close_file(xfopen_for_read("/proc/modules"));
Denis Vlasenko671691c2008-07-04 10:25:44 +0000963 return EXIT_SUCCESS;
964 }
965
966 INIT_G();
967
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000968 /* Prevent ugly corner cases with no modules at all */
969 modinfo = xzalloc(sizeof(modinfo[0]));
970
Denis Vlasenko1c781cc2008-09-06 14:14:01 +0000971 if ('i' != applet0) { /* not insmod */
972 /* Goto modules directory */
973 xchdir(CONFIG_DEFAULT_MODULES_DIR);
974 }
Denis Vlasenko0e2c93f2008-07-10 14:16:11 +0000975 uname(&uts); /* never fails */
976
977 /* depmod? */
978 if ('d' == applet0) {
979 /* Supported:
980 * -n: print result to stdout
981 * -a: process all modules (default)
982 * optional VERSION parameter
983 * Ignored:
984 * -A: do work only if a module is newer than depfile
985 * -e: report any symbols which a module needs
986 * which are not supplied by other modules or the kernel
987 * -F FILE: System.map (symbols for -e)
988 * -q, -r, -u: noop?
989 * Not supported:
990 * -b BASEDIR: (TODO!) modules are in
991 * $BASEDIR/lib/modules/$VERSION
992 * -v: human readable deps to stdout
993 * -V: version (don't want to support it - people may depend
994 * on it as an indicator of "standard" depmod)
995 * -h: help (well duh)
996 * module1.o module2.o parameters (just ignored for now)
997 */
998 getopt32(argv, "na" "AeF:qru" /* "b:vV", NULL */, NULL);
999 argv += optind;
1000 /* if (argv[0] && argv[1]) bb_show_usage(); */
1001 /* Goto $VERSION directory */
1002 xchdir(argv[0] ? argv[0] : uts.release);
1003 /* Force full module scan by asking to find a bogus module.
1004 * This will generate modules.dep.bb as a side effect. */
1005 process_module((char*)"/", NULL);
1006 return !wrote_dep_bb_ok;
1007 }
1008
Denis Vlasenko671691c2008-07-04 10:25:44 +00001009 /* insmod, modprobe, rmmod require at least one argument */
1010 opt_complementary = "-1";
1011 /* only -q (quiet) and -r (rmmod),
1012 * the rest are accepted and ignored (compat) */
Felipe Contreras428bd2d2012-01-31 14:55:15 +01001013 getopt32(argv, "qrfsvwb");
Denis Vlasenko671691c2008-07-04 10:25:44 +00001014 argv += optind;
1015
1016 /* are we rmmod? -> simulate modprobe -r */
1017 if ('r' == applet0) {
1018 option_mask32 |= OPT_r;
1019 }
1020
Denis Vlasenko1c781cc2008-09-06 14:14:01 +00001021 if ('i' != applet0) { /* not insmod */
1022 /* Goto $VERSION directory */
1023 xchdir(uts.release);
1024 }
Denis Vlasenko671691c2008-07-04 10:25:44 +00001025
1026#if ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
Denys Vlasenko68c048f2015-02-07 20:44:46 +01001027 /* If not rmmod/-r, parse possible module options given on command line.
Denis Vlasenko671691c2008-07-04 10:25:44 +00001028 * insmod/modprobe takes one module name, the rest are parameters. */
1029 options = NULL;
Denys Vlasenko68c048f2015-02-07 20:44:46 +01001030 if (!(option_mask32 & OPT_r)) {
Denis Vlasenko671691c2008-07-04 10:25:44 +00001031 char **arg = argv;
1032 while (*++arg) {
1033 /* Enclose options in quotes */
1034 char *s = options;
1035 options = xasprintf("%s \"%s\"", s ? s : "", *arg);
1036 free(s);
1037 *arg = NULL;
1038 }
1039 }
1040#else
Denys Vlasenko68c048f2015-02-07 20:44:46 +01001041 if (!(option_mask32 & OPT_r))
Denis Vlasenko671691c2008-07-04 10:25:44 +00001042 argv[1] = NULL;
1043#endif
1044
Denis Vlasenko1c781cc2008-09-06 14:14:01 +00001045 if ('i' == applet0) { /* insmod */
1046 size_t len;
1047 void *map;
1048
1049 len = MAXINT(ssize_t);
Denys Vlasenkoe9d12b52011-01-09 20:57:52 +01001050 map = xmalloc_open_zipped_read_close(*argv, &len);
Denys Vlasenko094cc512011-01-17 14:58:27 +01001051 if (!map)
1052 bb_perror_msg_and_die("can't read '%s'", *argv);
Denis Vlasenko1c781cc2008-09-06 14:14:01 +00001053 if (init_module(map, len,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001054 IF_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(options ? options : "")
1055 IF_NOT_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE("")
Denys Vlasenkoe9d12b52011-01-09 20:57:52 +01001056 ) != 0
1057 ) {
Denis Vlasenko1f632292009-04-13 02:25:40 +00001058 bb_error_msg_and_die("can't insert '%s': %s",
Denis Vlasenko1c781cc2008-09-06 14:14:01 +00001059 *argv, moderror(errno));
Denys Vlasenkoe9d12b52011-01-09 20:57:52 +01001060 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001061 return EXIT_SUCCESS;
Denis Vlasenko1c781cc2008-09-06 14:14:01 +00001062 }
1063
Denis Vlasenko7f950a92008-07-10 14:14:45 +00001064 /* Try to load modprobe.dep.bb */
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001065 if ('r' != applet0) { /* not rmmod */
Denys Vlasenko68c048f2015-02-07 20:44:46 +01001066 load_dep_bb();
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001067 }
Denis Vlasenko78436992008-07-10 14:14:20 +00001068
Denis Vlasenko671691c2008-07-04 10:25:44 +00001069 /* Load/remove modules.
Denys Vlasenko68c048f2015-02-07 20:44:46 +01001070 * Only rmmod/modprobe -r loops here, insmod/modprobe has only argv[0] */
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001071 exitcode = EXIT_SUCCESS;
Denis Vlasenko671691c2008-07-04 10:25:44 +00001072 do {
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001073 exitcode |= process_module(*argv, options);
Denys Vlasenko5b3151c2010-09-25 14:37:06 +02001074 } while (*++argv);
Denis Vlasenko671691c2008-07-04 10:25:44 +00001075
1076 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001077 IF_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(free(options);)
Denis Vlasenko671691c2008-07-04 10:25:44 +00001078 }
Denys Vlasenko5c3e0602016-06-20 10:59:06 +02001079 return exitcode;
Denis Vlasenko671691c2008-07-04 10:25:44 +00001080}