blob: cf9be47a6a05d27bf869804e396d8320336f5f55 [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
6 * Copyright (c) 2008 Bernhard Fischer (initial depmod code)
7 *
8 * Licensed under GPLv2, see file LICENSE in this tarball for details.
9 */
10
11#include "libbb.h"
12#include "unarchive.h"
13
14#include <sys/utsname.h> /* uname() */
15#include <fnmatch.h>
16
Denis Vlasenko24a131e2008-07-09 15:30:57 +000017extern int init_module(void *module, unsigned long len, const char *options);
18extern int delete_module(const char *module, unsigned flags);
19extern int query_module(const char *name, int which, void *buf, size_t bufsize, size_t *ret);
20
21
Denis Vlasenkocee0dfc2008-07-06 11:11:35 +000022#define dbg1_error_msg(...) ((void)0)
23#define dbg2_error_msg(...) ((void)0)
24//#define dbg1_error_msg(...) bb_error_msg(__VA_ARGS__)
25//#define dbg2_error_msg(...) bb_error_msg(__VA_ARGS__)
Denis Vlasenko671691c2008-07-04 10:25:44 +000026
Denis Vlasenko24a131e2008-07-09 15:30:57 +000027#define DEPFILE_BB CONFIG_DEFAULT_DEPMOD_FILE".bb"
Denis Vlasenko671691c2008-07-04 10:25:44 +000028
29enum {
30 OPT_q = (1 << 0), /* be quiet */
31 OPT_r = (1 << 1), /* module removal instead of loading */
32};
33
34typedef struct module_info {
35 char *pathname;
Denis Vlasenko78436992008-07-10 14:14:20 +000036 char *aliases;
37 char *deps;
Denis Vlasenko671691c2008-07-04 10:25:44 +000038} module_info;
39
40/*
41 * GLOBALS
42 */
43struct globals {
44 module_info *modinfo;
45 char *module_load_options;
Denis Vlasenko7f950a92008-07-10 14:14:45 +000046 smallint dep_bb_seen;
Denis Vlasenko671691c2008-07-04 10:25:44 +000047 int module_count;
48 int module_found_idx;
49 int stringbuf_idx;
50 char stringbuf[32 * 1024]; /* some modules have lots of stuff */
51 /* for example, drivers/media/video/saa7134/saa7134.ko */
52};
53#define G (*ptr_to_globals)
54#define modinfo (G.modinfo )
Denis Vlasenko7f950a92008-07-10 14:14:45 +000055#define dep_bb_seen (G.dep_bb_seen )
Denis Vlasenko671691c2008-07-04 10:25:44 +000056#define module_count (G.module_count )
57#define module_found_idx (G.module_found_idx )
58#define module_load_options (G.module_load_options)
59#define stringbuf_idx (G.stringbuf_idx )
60#define stringbuf (G.stringbuf )
61#define INIT_G() do { \
62 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
63} while (0)
64
65
66static void appendc(char c)
67{
68 if (stringbuf_idx < sizeof(stringbuf))
69 stringbuf[stringbuf_idx++] = c;
70}
71
Denis Vlasenko24a131e2008-07-09 15:30:57 +000072static void bksp(void)
73{
74 if (stringbuf_idx)
75 stringbuf_idx--;
76}
77
Denis Vlasenko671691c2008-07-04 10:25:44 +000078static void append(const char *s)
79{
80 size_t len = strlen(s);
81 if (stringbuf_idx + len < sizeof(stringbuf)) {
82 memcpy(stringbuf + stringbuf_idx, s, len);
83 stringbuf_idx += len;
84 }
85}
86
87static void reset_stringbuf(void)
88{
89 stringbuf_idx = 0;
90}
91
92static char* copy_stringbuf(void)
93{
94 char *copy = xmalloc(stringbuf_idx);
95 return memcpy(copy, stringbuf, stringbuf_idx);
96}
97
98static char* find_keyword(char *ptr, size_t len, const char *word)
99{
100 int wlen;
101
102 if (!ptr) /* happens if read_module cannot read it */
103 return NULL;
104
105 wlen = strlen(word);
106 len -= wlen - 1;
107 while ((ssize_t)len > 0) {
108 char *old = ptr;
109 /* search for the first char in word */
110 ptr = memchr(ptr, *word, len);
111 if (ptr == NULL) /* no occurance left, done */
112 break;
113 if (strncmp(ptr, word, wlen) == 0)
114 return ptr + wlen; /* found, return ptr past it */
115 ++ptr;
116 len -= (ptr - old);
117 }
118 return NULL;
119}
120
121static void replace(char *s, char what, char with)
122{
123 while (*s) {
124 if (what == *s)
125 *s = with;
126 ++s;
127 }
128}
129
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000130/* Take "word word", return malloced "word",NUL,"word",NUL,NUL */
131static char* str_2_list(const char *str)
132{
133 int len = strlen(str) + 1;
134 char *dst = xmalloc(len + 1);
135
136 dst[len] = '\0';
137 memcpy(dst, str, len);
138//TODO: protect against 2+ spaces: "word word"
139 replace(dst, ' ', '\0');
140 return dst;
141}
142
Denis Vlasenko671691c2008-07-04 10:25:44 +0000143#if ENABLE_FEATURE_MODPROBE_SMALL_ZIPPED
144static char *xmalloc_open_zipped_read_close(const char *fname, size_t *sizep)
145{
146 size_t len;
147 char *image;
148 char *suffix;
149
150 int fd = open_or_warn(fname, O_RDONLY);
151 if (fd < 0)
152 return NULL;
153
154 suffix = strrchr(fname, '.');
155 if (suffix) {
156 if (strcmp(suffix, ".gz") == 0)
157 fd = open_transformer(fd, unpack_gz_stream, "gunzip");
158 else if (strcmp(suffix, ".bz2") == 0)
159 fd = open_transformer(fd, unpack_bz2_stream, "bunzip2");
160 }
161
162 len = (sizep) ? *sizep : 64 * 1024 * 1024;
163 image = xmalloc_read(fd, &len);
164 if (!image)
165 bb_perror_msg("read error from '%s'", fname);
166 close(fd);
167
168 if (sizep)
169 *sizep = len;
170 return image;
171}
172# define read_module xmalloc_open_zipped_read_close
173#else
174# define read_module xmalloc_open_read_close
175#endif
176
177/* We use error numbers in a loose translation... */
178static const char *moderror(int err)
179{
180 switch (err) {
181 case ENOEXEC:
182 return "invalid module format";
183 case ENOENT:
184 return "unknown symbol in module or invalid parameter";
185 case ESRCH:
186 return "module has wrong symbol version";
187 case EINVAL: /* "invalid parameter" */
188 return "unknown symbol in module or invalid parameter"
189 + sizeof("unknown symbol in module or");
190 default:
191 return strerror(err);
192 }
193}
194
195static int load_module(const char *fname, const char *options)
196{
197#if 1
198 int r;
199 size_t len = MAXINT(ssize_t);
200 char *module_image;
201 dbg1_error_msg("load_module('%s','%s')", fname, options);
202
203 module_image = read_module(fname, &len);
204 r = (!module_image || init_module(module_image, len, options ? options : "") != 0);
205 free(module_image);
206 dbg1_error_msg("load_module:%d", r);
207 return r; /* 0 = success */
208#else
209 /* For testing */
210 dbg1_error_msg("load_module('%s','%s')", fname, options);
211 return 1;
212#endif
213}
214
Denis Vlasenko78436992008-07-10 14:14:20 +0000215static void parse_module(module_info *info, const char *pathname)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000216{
217 char *module_image;
218 char *ptr;
219 size_t len;
220 size_t pos;
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000221 dbg1_error_msg("parse_module('%s')", pathname);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000222
223 /* Read (possibly compressed) module */
224 len = 64 * 1024 * 1024; /* 64 Mb at most */
225 module_image = read_module(pathname, &len);
Denis Vlasenko78436992008-07-10 14:14:20 +0000226//TODO: optimize redundant module body reads
Denis Vlasenko671691c2008-07-04 10:25:44 +0000227
Denis Vlasenko78436992008-07-10 14:14:20 +0000228 /* "alias1 symbol:sym1 alias2 symbol:sym2" */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000229 reset_stringbuf();
Denis Vlasenko671691c2008-07-04 10:25:44 +0000230 pos = 0;
231 while (1) {
232 ptr = find_keyword(module_image + pos, len - pos, "alias=");
233 if (!ptr) {
234 ptr = find_keyword(module_image + pos, len - pos, "__ksymtab_");
235 if (!ptr)
236 break;
237 /* DOCME: __ksymtab_gpl and __ksymtab_strings occur
238 * in many modules. What do they mean? */
239 if (strcmp(ptr, "gpl") != 0 && strcmp(ptr, "strings") != 0) {
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000240 dbg2_error_msg("alias:'symbol:%s'", ptr);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000241 append("symbol:");
242 }
243 } else {
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000244 dbg2_error_msg("alias:'%s'", ptr);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000245 }
246 append(ptr);
247 appendc(' ');
248 pos = (ptr - module_image);
249 }
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000250 bksp(); /* remove last ' ' */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000251 appendc('\0');
Denis Vlasenko78436992008-07-10 14:14:20 +0000252 info->aliases = copy_stringbuf();
Denis Vlasenko671691c2008-07-04 10:25:44 +0000253
Denis Vlasenko78436992008-07-10 14:14:20 +0000254 /* "dependency1 depandency2" */
255 reset_stringbuf();
Denis Vlasenko671691c2008-07-04 10:25:44 +0000256 ptr = find_keyword(module_image, len, "depends=");
257 if (ptr && *ptr) {
258 replace(ptr, ',', ' ');
259 replace(ptr, '-', '_');
260 dbg2_error_msg("dep:'%s'", ptr);
261 append(ptr);
262 }
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000263 appendc('\0');
Denis Vlasenko78436992008-07-10 14:14:20 +0000264 info->deps = copy_stringbuf();
Denis Vlasenko671691c2008-07-04 10:25:44 +0000265
266 free(module_image);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000267}
268
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000269static int pathname_matches_modname(const char *pathname, const char *modname)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000270{
271 const char *fname = bb_get_last_path_component_nostrip(pathname);
272 const char *suffix = strrstr(fname, ".ko");
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000273//TODO: can do without malloc?
Denis Vlasenko671691c2008-07-04 10:25:44 +0000274 char *name = xstrndup(fname, suffix - fname);
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000275 int r;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000276 replace(name, '-', '_');
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000277 r = (strcmp(name, modname) == 0);
278 free(name);
279 return r;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000280}
281
282static FAST_FUNC int fileAction(const char *pathname,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000283 struct stat *sb UNUSED_PARAM,
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000284 void *modname_to_match,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000285 int depth UNUSED_PARAM)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000286{
287 int cur;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000288 const char *fname;
289
290 pathname += 2; /* skip "./" */
291 fname = bb_get_last_path_component_nostrip(pathname);
292 if (!strrstr(fname, ".ko")) {
293 dbg1_error_msg("'%s' is not a module", pathname);
294 return TRUE; /* not a module, continue search */
295 }
296
297 cur = module_count++;
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000298 modinfo = xrealloc_vector(modinfo, 12, cur);
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000299//TODO: use zeroing version of xrealloc_vector?
Denis Vlasenko671691c2008-07-04 10:25:44 +0000300 modinfo[cur].pathname = xstrdup(pathname);
Denis Vlasenko78436992008-07-10 14:14:20 +0000301 modinfo[cur].aliases = NULL;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000302 modinfo[cur+1].pathname = NULL;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000303
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000304 if (!pathname_matches_modname(fname, modname_to_match)) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000305 dbg1_error_msg("'%s' module name doesn't match", pathname);
306 return TRUE; /* module name doesn't match, continue search */
307 }
308
309 dbg1_error_msg("'%s' module name matches", pathname);
310 module_found_idx = cur;
Denis Vlasenko78436992008-07-10 14:14:20 +0000311 parse_module(&modinfo[cur], pathname);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000312
313 if (!(option_mask32 & OPT_r)) {
314 if (load_module(pathname, module_load_options) == 0) {
315 /* Load was successful, there is nothing else to do.
316 * This can happen ONLY for "top-level" module load,
317 * not a dep, because deps dont do dirscan. */
318 exit(EXIT_SUCCESS);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000319 }
320 }
321
Denis Vlasenko671691c2008-07-04 10:25:44 +0000322 return TRUE;
323}
324
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000325static int load_dep_bb(void)
Denis Vlasenko78436992008-07-10 14:14:20 +0000326{
327 char *line;
328 FILE *fp = fopen(DEPFILE_BB, "r");
329
330 if (!fp)
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000331 return 0;
332
333 dep_bb_seen = 1;
334 dbg1_error_msg("loading "DEPFILE_BB);
335
336 /* Why? There is a rare scenario: we did not find modprobe.dep.bb,
337 * we scanned the dir and found no module by name, then we search
338 * for alias (full scan), and we decided to generate modprobe.dep.bb.
339 * But we see modprobe.dep.bb.new! Other modprobe is at work!
340 * We wait and other modprobe renames it to modprobe.dep.bb.
341 * Now we can use it.
342 * But we already have modinfo[] filled, and "module_count = 0"
343 * makes us start anew. Yes, we leak modinfo[].xxx pointers -
344 * there is not much of data there anyway. */
345 module_count = 0;
346 memset(&modinfo[0], 0, sizeof(modinfo[0]));
Denis Vlasenko78436992008-07-10 14:14:20 +0000347
348 while ((line = xmalloc_fgetline(fp)) != NULL) {
349 char* space;
350 int cur;
351
352 if (!line[0]) {
353 free(line);
354 continue;
355 }
356 space = strchrnul(line, ' ');
357 cur = module_count++;
358 modinfo = xrealloc_vector(modinfo, 12, cur);
359//TODO: use zeroing version of xrealloc_vector?
360 modinfo[cur+1].pathname = NULL;
361 modinfo[cur].pathname = line; /* we take ownership of malloced block here */
362 if (*space)
363 *space++ = '\0';
364 modinfo[cur].aliases = space;
365 modinfo[cur].deps = xmalloc_fgetline(fp) ? : xzalloc(1);
366 if (modinfo[cur].deps[0]) {
367 /* deps are not "", so next line must be empty */
368 line = xmalloc_fgetline(fp);
369 /* Refuse to work with damaged config file */
370 if (line && line[0])
371 bb_error_msg_and_die("error in %s at '%s'", DEPFILE_BB, line);
372 free(line);
373 }
374 }
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000375 return 1;
376}
377
378static int start_dep_bb_writeout(void)
379{
380 int fd;
381
382 fd = open(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0644);
383 if (fd < 0) {
384 if (errno == EEXIST) {
385 int count = 5 * 20;
386 dbg1_error_msg(DEPFILE_BB".new exists, waiting for "DEPFILE_BB);
387 while (1) {
388 usleep(1000*1000 / 20);
389 if (load_dep_bb()) {
390 dbg1_error_msg(DEPFILE_BB" appeared");
391 return -2; /* magic number */
392 }
393 if (!--count)
394 break;
395 }
396 bb_error_msg("deleting stale %s", DEPFILE_BB".new");
397 fd = open_or_warn(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC);
398 }
399 }
400 dbg1_error_msg("opened "DEPFILE_BB".new:%d", fd);
401 return fd;
402}
403
404static void write_out_dep_bb(int fd)
405{
406 int i;
407 FILE *fp;
408
409 /* We want good error reporting. fdprintf is not good enough. */
410 fp = fdopen(fd, "w");
411 if (!fp) {
412 close(fd);
413 goto err;
414 }
415 i = 0;
416 while (modinfo[i].pathname) {
417 fprintf(fp, "%s%s%s\n" "%s%s\n",
418 modinfo[i].pathname, modinfo[i].aliases[0] ? " " : "", modinfo[i].aliases,
419 modinfo[i].deps, modinfo[i].deps[0] ? "\n" : "");
420 i++;
421 }
422 /* Badly formatted depfile is a no-no. Be paranoid. */
423 errno = 0;
424 if (ferror(fp) | fclose(fp))
425 goto err;
426 if (rename(DEPFILE_BB".new", DEPFILE_BB) != 0) {
427 err:
428 bb_perror_msg("can't create %s", DEPFILE_BB);
429 unlink(DEPFILE_BB".new");
430 } else {
431 dbg1_error_msg("created "DEPFILE_BB);
432 }
Denis Vlasenko78436992008-07-10 14:14:20 +0000433}
434
Denis Vlasenko671691c2008-07-04 10:25:44 +0000435static module_info* find_alias(const char *alias)
436{
437 int i;
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000438 int dep_bb_fd;
439 module_info *result;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000440 dbg1_error_msg("find_alias('%s')", alias);
441
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000442 try_again:
Denis Vlasenko671691c2008-07-04 10:25:44 +0000443 /* First try to find by name (cheaper) */
444 i = 0;
445 while (modinfo[i].pathname) {
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000446 if (pathname_matches_modname(modinfo[i].pathname, alias)) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000447 dbg1_error_msg("found '%s' in module '%s'",
448 alias, modinfo[i].pathname);
Denis Vlasenko78436992008-07-10 14:14:20 +0000449 if (!modinfo[i].aliases) {
450 parse_module(&modinfo[i], modinfo[i].pathname);
451 }
Denis Vlasenko671691c2008-07-04 10:25:44 +0000452 return &modinfo[i];
453 }
Denis Vlasenko671691c2008-07-04 10:25:44 +0000454 i++;
455 }
456
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000457 /* Ok, we definitely have to scan module bodies. This is a good
458 * moment to generate modprobe.dep.bb, if it does not exist yet */
459 dep_bb_fd = dep_bb_seen ? -1 : start_dep_bb_writeout();
460 if (dep_bb_fd == -2) /* modprobe.dep.bb appeared? */
461 goto try_again;
462
Denis Vlasenko671691c2008-07-04 10:25:44 +0000463 /* Scan all module bodies, extract modinfo (it contains aliases) */
464 i = 0;
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000465 result = NULL;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000466 while (modinfo[i].pathname) {
467 char *desc, *s;
Denis Vlasenko78436992008-07-10 14:14:20 +0000468 if (!modinfo[i].aliases) {
469 parse_module(&modinfo[i], modinfo[i].pathname);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000470 }
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000471 if (result)
472 continue;
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000473 /* "alias1 symbol:sym1 alias2 symbol:sym2" */
Denis Vlasenko78436992008-07-10 14:14:20 +0000474 desc = str_2_list(modinfo[i].aliases);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000475 /* Does matching substring exist? */
Denis Vlasenko671691c2008-07-04 10:25:44 +0000476 for (s = desc; *s; s += strlen(s) + 1) {
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000477 /* Aliases in module bodies can be defined with
Denis Vlasenko58f59a22008-07-06 11:52:23 +0000478 * shell patterns. Example:
479 * "pci:v000010DEd000000D9sv*sd*bc*sc*i*".
480 * Plain strcmp() won't catch that */
481 if (fnmatch(s, alias, 0) == 0) {
Denis Vlasenko671691c2008-07-04 10:25:44 +0000482 dbg1_error_msg("found alias '%s' in module '%s'",
483 alias, modinfo[i].pathname);
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000484 result = &modinfo[i];
485 break;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000486 }
487 }
488 free(desc);
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000489 if (result && dep_bb_fd < 0)
490 return result;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000491 i++;
492 }
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000493
494 /* Create module.dep.bb if needed */
495 if (dep_bb_fd >= 0) {
496 write_out_dep_bb(dep_bb_fd);
497 }
498
499 dbg1_error_msg("find_alias '%s' returns %p", alias, result);
500 return result;
Denis Vlasenko671691c2008-07-04 10:25:44 +0000501}
502
503#if ENABLE_FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED
504static int already_loaded(const char *name)
505{
506 int ret = 0;
507 int len = strlen(name);
508 char *line;
509 FILE* modules;
510
511 modules = xfopen("/proc/modules", "r");
512 while ((line = xmalloc_fgets(modules)) != NULL) {
513 if (strncmp(line, name, len) == 0 && line[len] == ' ') {
514 free(line);
515 ret = 1;
516 break;
517 }
518 free(line);
519 }
520 fclose(modules);
521 return ret;
522}
523#else
524#define already_loaded(name) is_rmmod
525#endif
526
527/*
528 Given modules definition and module name (or alias, or symbol)
529 load/remove the module respecting dependencies
530*/
531#if !ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
532#define process_module(a,b) process_module(a)
533#define cmdline_options ""
534#endif
535static void process_module(char *name, const char *cmdline_options)
536{
537 char *s, *deps, *options;
538 module_info *info;
539 int is_rmmod = (option_mask32 & OPT_r) != 0;
540 dbg1_error_msg("process_module('%s','%s')", name, cmdline_options);
541
542 replace(name, '-', '_');
543
544 dbg1_error_msg("already_loaded:%d is_rmmod:%d", already_loaded(name), is_rmmod);
545 if (already_loaded(name) != is_rmmod) {
546 dbg1_error_msg("nothing to do for '%s'", name);
547 return;
548 }
549
550 options = NULL;
551 if (!is_rmmod) {
552 char *opt_filename = xasprintf("/etc/modules/%s", name);
553 options = xmalloc_open_read_close(opt_filename, NULL);
554 if (options)
555 replace(options, '\n', ' ');
556#if ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
557 if (cmdline_options) {
558 /* NB: cmdline_options always have one leading ' '
559 * (see main()), we remove it here */
560 char *op = xasprintf(options ? "%s %s" : "%s %s" + 3,
561 cmdline_options + 1, options);
562 free(options);
563 options = op;
564 }
565#endif
566 free(opt_filename);
567 module_load_options = options;
568 dbg1_error_msg("process_module('%s'): options:'%s'", name, options);
569 }
570
571 if (!module_count) {
572 /* Scan module directory. This is done only once.
573 * It will attempt module load, and will exit(EXIT_SUCCESS)
574 * on success. */
575 module_found_idx = -1;
576 recursive_action(".",
577 ACTION_RECURSE, /* flags */
578 fileAction, /* file action */
579 NULL, /* dir action */
580 name, /* user data */
581 0); /* depth */
582 dbg1_error_msg("dirscan complete");
583 /* Module was not found, or load failed, or is_rmmod */
584 if (module_found_idx >= 0) { /* module was found */
585 info = &modinfo[module_found_idx];
586 } else { /* search for alias, not a plain module name */
587 info = find_alias(name);
588 }
589 } else {
590 info = find_alias(name);
591 }
592
593 /* rmmod? unload it by name */
594 if (is_rmmod) {
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000595 if (delete_module(name, O_NONBLOCK | O_EXCL) != 0
Denis Vlasenko671691c2008-07-04 10:25:44 +0000596 && !(option_mask32 & OPT_q)
597 ) {
598 bb_perror_msg("remove '%s'", name);
599 goto ret;
600 }
601 /* N.B. we do not stop here -
602 * continue to unload modules on which the module depends:
603 * "-r --remove: option causes modprobe to remove a module.
604 * If the modules it depends on are also unused, modprobe
605 * will try to remove them, too." */
606 }
607
608 if (!info) { /* both dirscan and find_alias found nothing */
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000609 bb_error_msg("module '%s' not found", name);
610//TODO: _and_die()?
Denis Vlasenko671691c2008-07-04 10:25:44 +0000611 goto ret;
612 }
613
Denis Vlasenko671691c2008-07-04 10:25:44 +0000614 /* Iterate thru dependencies, trying to (un)load them */
Denis Vlasenko78436992008-07-10 14:14:20 +0000615 deps = str_2_list(info->deps);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000616 for (s = deps; *s; s += strlen(s) + 1) {
617 //if (strcmp(name, s) != 0) // N.B. do loops exist?
618 dbg1_error_msg("recurse on dep '%s'", s);
619 process_module(s, NULL);
620 dbg1_error_msg("recurse on dep '%s' done", s);
621 }
622 free(deps);
623
624 /* insmod -> load it */
625 if (!is_rmmod) {
626 errno = 0;
627 if (load_module(info->pathname, options) != 0) {
628 if (EEXIST != errno) {
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000629 bb_error_msg("'%s': %s",
630 info->pathname,
Denis Vlasenko671691c2008-07-04 10:25:44 +0000631 moderror(errno));
632 } else {
Denis Vlasenko24a131e2008-07-09 15:30:57 +0000633 dbg1_error_msg("'%s': %s",
634 info->pathname,
Denis Vlasenko671691c2008-07-04 10:25:44 +0000635 moderror(errno));
636 }
637 }
638 }
639 ret:
640 free(options);
641//TODO: return load attempt result from process_module.
642//If dep didn't load ok, continuing makes little sense.
643}
644#undef cmdline_options
645
646
647/* For reference, module-init-tools-0.9.15-pre2 options:
648
649# insmod
650Usage: insmod filename [args]
651
652# rmmod --help
653Usage: rmmod [-fhswvV] modulename ...
654 -f (or --force) forces a module unload, and may crash your machine.
655 -s (or --syslog) says use syslog, not stderr
656 -v (or --verbose) enables more messages
657 -w (or --wait) begins a module removal even if it is used
658 and will stop new users from accessing the module (so it
659 should eventually fall to zero).
660
661# modprobe
662Usage: modprobe [--verbose|--version|--config|--remove] filename [options]
663
664# depmod --help
665depmod 0.9.15-pre2 -- part of module-init-tools
666depmod -[aA] [-n -e -v -q -V -r -u] [-b basedirectory] [forced_version]
667depmod [-n -e -v -q -r -u] [-F kernelsyms] module1.o module2.o ...
668If no arguments (except options) are given, "depmod -a" is assumed
669
670depmod will output a dependancy list suitable for the modprobe utility.
671
672Options:
673 -a, --all Probe all modules
674 -n, --show Write the dependency file on stdout only
675 -b basedirectory
676 --basedir basedirectory Use an image of a module tree.
677 -F kernelsyms
678 --filesyms kernelsyms Use the file instead of the
679 current kernel symbols.
680*/
681
682int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000683int modprobe_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko671691c2008-07-04 10:25:44 +0000684{
685 struct utsname uts;
686 char applet0 = applet_name[0];
687 USE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(char *options;)
688
689 /* depmod is a stub */
690 if ('d' == applet0)
691 return EXIT_SUCCESS;
692
693 /* are we lsmod? -> just dump /proc/modules */
694 if ('l' == applet0) {
695 xprint_and_close_file(xfopen("/proc/modules", "r"));
696 return EXIT_SUCCESS;
697 }
698
699 INIT_G();
700
701 /* insmod, modprobe, rmmod require at least one argument */
702 opt_complementary = "-1";
703 /* only -q (quiet) and -r (rmmod),
704 * the rest are accepted and ignored (compat) */
705 getopt32(argv, "qrfsvw");
706 argv += optind;
707
708 /* are we rmmod? -> simulate modprobe -r */
709 if ('r' == applet0) {
710 option_mask32 |= OPT_r;
711 }
712
713 /* goto modules directory */
714 xchdir(CONFIG_DEFAULT_MODULES_DIR);
715 uname(&uts); /* never fails */
716 xchdir(uts.release);
717
718#if ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
719 /* If not rmmod, parse possible module options given on command line.
720 * insmod/modprobe takes one module name, the rest are parameters. */
721 options = NULL;
722 if ('r' != applet0) {
723 char **arg = argv;
724 while (*++arg) {
725 /* Enclose options in quotes */
726 char *s = options;
727 options = xasprintf("%s \"%s\"", s ? s : "", *arg);
728 free(s);
729 *arg = NULL;
730 }
731 }
732#else
733 if ('r' != applet0)
734 argv[1] = NULL;
735#endif
736
Denis Vlasenko7f950a92008-07-10 14:14:45 +0000737 /* Prevent ugly corner cases with no modules at all */
738 modinfo = xzalloc(sizeof(modinfo[0]));
739
740 /* Try to load modprobe.dep.bb */
Denis Vlasenko78436992008-07-10 14:14:20 +0000741 load_dep_bb();
742
Denis Vlasenko671691c2008-07-04 10:25:44 +0000743 /* Load/remove modules.
744 * Only rmmod loops here, insmod/modprobe has only argv[0] */
745 do {
746 process_module(*argv++, options);
747 } while (*argv);
748
749 if (ENABLE_FEATURE_CLEAN_UP) {
750 USE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(free(options);)
751 }
752 return EXIT_SUCCESS;
753}