294
static void replace_modname(struct elf_file *module,
295
void *mem, unsigned long len,
296
const char *oldname, const char *newname)
300
/* 64 - sizeof(unsigned long) - 1 */
301
if (strlen(newname) > 55)
302
fatal("New name %s is too long\n", newname);
304
/* Find where it is in the module structure. Don't assume layout! */
305
for (p = mem; p < (char *)mem + len - strlen(oldname); p++) {
306
if (memcmp(p, oldname, strlen(oldname)) == 0) {
312
warn("Could not find old name in %s to replace!\n", module->pathname);
315
static void rename_module(struct elf_file *module,
323
modstruct = module->ops->load_section(module,
324
".gnu.linkonce.this_module", &len);
327
modstruct = module->ops->load_section(module, "__module", &len);
329
warn("Could not find module name to change in %s\n",
332
replace_modname(module, modstruct, len, oldname, newname);
335
294
static void clear_magic(struct elf_file *module)
337
296
struct string_table *tbl;
942
901
struct string_table *post_modnames = NULL;
943
902
struct module_softdep *new;
945
modname = underscores(strsep_skipspace(&ptr, "\t "));
904
modname = strsep_skipspace(&ptr, "\t ");
946
905
if (!modname || !ptr)
947
906
goto syntax_error;
907
modname = underscores(modname);
948
909
while ((tk = strsep_skipspace(&ptr, "\t ")) != NULL) {
910
tk = underscores(tk);
949
912
if (streq(tk, "pre:")) {
950
913
pre = 1; post = 0;
951
914
} else if (streq(tk, "post:")) {
1311
1274
/* Forward declaration */
1312
1275
int do_modprobe(const char *modname,
1313
const char *newname,
1314
1276
const char *cmdline_opts,
1315
1277
const struct modprobe_conf *conf,
1316
1278
const char *dirname,
1325
1287
modprobe_flags_t flags)
1327
1289
struct string_table *pre_modnames, *post_modnames;
1290
modprobe_flags_t softdep_flags = flags;
1293
softdep_flags &= ~mit_first_time;
1294
softdep_flags &= ~mit_ignore_commands;
1295
if (flags & mit_remove)
1296
softdep_flags |= mit_quiet_inuse;
1330
1298
if (++recursion_depth >= MAX_RECURSION)
1331
1299
fatal("modprobe: softdep dependency loop encountered %s %s\n",
1332
1300
(flags & mit_remove) ? "removing" : "inserting",
1347
1315
/* Reverse module order if removing. */
1348
1316
j = (flags & mit_remove) ? pre_modnames->cnt-1 - i : i;
1350
do_modprobe(pre_modnames->str[j], NULL, "",
1351
conf, dirname, warn, flags);
1318
do_modprobe(pre_modnames->str[j], "",
1319
conf, dirname, warn, softdep_flags);
1354
1322
/* Modprobe main module, passing cmdline_opts, ignoring softdep */
1356
do_modprobe(softdep->modname, NULL, cmdline_opts,
1324
do_modprobe(softdep->modname, cmdline_opts,
1357
1325
conf, dirname, warn, flags | mit_ignore_commands);
1359
1327
/* Modprobe post_modnames */
1362
1330
/* Reverse module order if removing. */
1363
1331
j = (flags & mit_remove) ? post_modnames->cnt-1 - i : i;
1365
do_modprobe(post_modnames->str[j], NULL, "", conf,
1366
dirname, warn, flags);
1333
do_modprobe(post_modnames->str[j], "", conf,
1334
dirname, warn, softdep_flags);
1370
1338
/* Actually do the insert. */
1371
1339
static int insmod(struct list_head *list,
1372
1340
const char *optstring,
1373
const char *newname,
1374
1341
const char *cmdline_opts,
1375
1342
const struct modprobe_conf *conf,
1376
1343
const char *dirname,
1394
1361
modprobe_flags_t f = flags;
1395
1362
f &= ~mit_first_time;
1396
1363
f &= ~mit_ignore_commands;
1397
if ((rc = insmod(list, "", NULL,
1398
"", conf, dirname, warn, f)) != 0)
1364
if ((rc = insmod(list, "", "", conf, dirname, warn, f)) != 0)
1400
1366
error("Error inserting %s (%s): %s\n",
1401
1367
mod->modname, mod->filename,
1407
1373
/* Don't do ANYTHING if already in kernel. */
1408
already_loaded = module_in_kernel(newname ?: mod->modname, NULL);
1374
already_loaded = module_in_kernel(mod->modname, NULL);
1410
1376
if (!(flags & mit_ignore_loaded) && already_loaded == 1) {
1411
1377
if (flags & mit_first_time)
1412
error("Module %s already in kernel.\n",
1413
newname ?: mod->modname);
1378
error("Module %s already in kernel.\n", mod->modname);
1417
1382
softdep = find_softdep(mod->modname, conf->softdeps);
1418
1383
if (softdep && !(flags & mit_ignore_commands)) {
1419
do_softdep(softdep, cmdline_opts, conf, dirname,
1420
error, flags & (mit_remove | mit_dry_run));
1384
do_softdep(softdep, cmdline_opts, conf, dirname, error, flags);
1428
1392
" and /proc/modules does not exist.\n");
1429
1393
warn("Ignoring install commands for %s"
1430
1394
" in case it is already loaded.\n",
1431
newname ?: mod->modname);
1433
1397
do_command(mod->modname, command, flags & mit_dry_run,
1434
1398
error, "install", cmdline_opts);
1443
1407
strerror(errno));
1447
rename_module(module, mod->modname, newname);
1448
1410
if (flags & mit_strip_modversion)
1449
1411
module->ops->strip_section(module, "__versions");
1450
1412
if (flags & mit_strip_vermagic)
1463
1425
if (errno == EEXIST) {
1464
1426
if (flags & mit_first_time)
1465
1427
error("Module %s already in kernel.\n",
1466
newname ?: mod->modname);
1467
1429
goto out_elf_file;
1469
1431
/* don't warn noisely if we're loading multiple aliases. */
1485
1447
/* Do recursive removal. */
1486
1448
static void rmmod(struct list_head *list,
1488
1449
const char *cmdline_opts,
1489
1450
const struct modprobe_conf *conf,
1490
1451
const char *dirname,
1500
1461
/* Take first one off the list. */
1501
1462
list_del(&mod->list);
1504
name = mod->modname;
1506
1464
/* Don't do ANYTHING if not loaded. */
1507
exists = module_in_kernel(name, &usecount);
1465
exists = module_in_kernel(mod->modname, &usecount);
1508
1466
if (exists == 0)
1509
1467
goto nonexistent_module;
1513
1471
softdep = find_softdep(mod->modname, conf->softdeps);
1514
1472
if (softdep && !(flags & mit_ignore_commands)) {
1515
do_softdep(softdep, cmdline_opts, conf, dirname,
1516
error, flags & (mit_remove | mit_dry_run));
1473
do_softdep(softdep, cmdline_opts, conf, dirname, error, flags);
1517
1474
goto remove_rest;
1543
1500
if (flags & mit_dry_run)
1544
1501
goto remove_rest;
1546
if (delete_module(name, O_EXCL) != 0) {
1503
if (delete_module(mod->modname, O_EXCL) != 0) {
1547
1504
if (errno == ENOENT)
1548
1505
goto nonexistent_module;
1549
1506
error("Error removing %s (%s): %s\n",
1550
name, mod->filename,
1507
mod->modname, mod->filename,
1551
1508
remove_moderror(errno));
1558
1515
flags &= ~mit_ignore_commands;
1559
1516
flags |= mit_quiet_inuse;
1561
rmmod(list, NULL, "", conf, dirname, warn, flags);
1518
rmmod(list, "", conf, dirname, warn, flags);
1563
1520
free_module(mod);
1572
1529
static int handle_module(const char *modname,
1573
1530
struct list_head *todo_list,
1574
const char *newname,
1575
1531
const char *options,
1576
1532
const char *cmdline_opts,
1577
1533
const struct modprobe_conf *conf,
1580
1536
modprobe_flags_t flags)
1582
1538
if (list_empty(todo_list)) {
1583
const struct module_softdep *softdep;
1584
1539
const char *command;
1586
1541
/* The dependencies have to be real modules, but
1587
1542
handle case where the first is completely bogus. */
1589
softdep = find_softdep(modname, conf->softdeps);
1590
if (softdep && !(flags & mit_ignore_commands)) {
1591
do_softdep(softdep, cmdline_opts, conf, dirname,
1592
error, flags & (mit_remove | mit_dry_run));
1596
1544
command = find_command(modname, conf->commands);
1597
1545
if (command && !(flags & mit_ignore_commands)) {
1598
1546
do_command(modname, command, flags & mit_dry_run, error,
1608
1556
if (flags & mit_remove)
1609
rmmod(todo_list, newname, cmdline_opts,
1557
rmmod(todo_list, cmdline_opts,
1610
1558
conf, dirname, error, flags);
1612
insmod(todo_list, options, newname,
1560
insmod(todo_list, options,
1613
1561
cmdline_opts, conf, dirname, error, flags);
1635
int do_modprobe(const char *modulename,
1636
const char *newname,
1583
int do_modprobe(const char *modname,
1637
1584
const char *cmdline_opts,
1638
1585
const struct modprobe_conf *conf,
1639
1586
const char *dirname,
1641
1588
modprobe_flags_t flags)
1644
1590
struct module_alias *matching_aliases;
1645
1591
LIST_HEAD(list);
1646
1592
int failed = 0;
1648
/* Convert name we are looking for */
1649
modname = underscores(NOFAIL(strdup(modulename)));
1651
1594
matching_aliases = find_aliases(conf->aliases, modname);
1653
1596
/* No luck? Try symbol names, if starts with symbol:. */
1707
1650
read_depends(dirname, aliases->module, &list);
1708
1651
failed |= handle_module(aliases->module,
1709
&list, newname, opts, cmdline_opts,
1652
&list, opts, cmdline_opts,
1710
1653
conf, dirname, err, flags);
1712
1655
aliases = aliases->next;
1718
1661
&& find_blacklist(modname, conf->blacklist))
1721
failed |= handle_module(modname, &list, newname, cmdline_opts,
1664
failed |= handle_module(modname, &list, cmdline_opts,
1722
1665
cmdline_opts, conf, dirname, error, flags);
1727
1669
free_aliases(matching_aliases);
1739
1681
{ "dirname", 1, NULL, 'd' },
1740
1682
{ "set-version", 1, NULL, 'S' },
1741
1683
{ "config", 1, NULL, 'C' },
1742
{ "name", 1, NULL, 'o' },
1743
1684
{ "remove", 0, NULL, 'r' },
1744
1685
{ "showconfig", 0, NULL, 'c' },
1745
1686
{ "list", 0, NULL, 'l' },
1782
1722
argv = merge_args(getenv("MODPROBE_OPTIONS"), argv, &argc);
1785
while ((opt = getopt_long(argc, argv, "Vvqsnd:S:C:DRo:rclt:aibf", options, NULL)) != -1){
1725
while ((opt = getopt_long(argc, argv, "Vvqsnd:S:C:DRrclt:aibf", options, NULL)) != -1){
1788
1728
puts(PACKAGE " version " VERSION);
1891
1828
fatal("-t only supported with -l");
1831
dump_modversions(argv[optind], error);
1893
1835
/* Read aliases, options etc. */
1894
1836
parse_toplevel_config(configname, &conf, dump_config, flags & mit_remove);
1896
1838
/* Read module options from kernel command line */
1897
parse_kcmdline(1, &conf.options);
1839
parse_kcmdline(dump_config, &conf.options);
1899
1841
if (dump_config) {
1900
1842
char *aliasfilename, *symfilename;
1903
1845
nofail_asprintf(&aliasfilename, "%s/modules.alias", dirname);
1904
1846
nofail_asprintf(&symfilename, "%s/modules.symbols", dirname);
1906
parse_toplevel_config(configname, &conf, 1, 0);
1907
/* Read module options from kernel command line */
1908
parse_kcmdline(1, &conf.options);
1909
1848
read_aliases(aliasfilename, "", 1, &conf.aliases);
1910
1849
read_aliases(symfilename, "", 1, &conf.aliases);
1920
1859
cmdline_opts = gather_options(argv+optind+1);
1862
/* Convert names we are looking for */
1863
for (i = 0; i < num_modules; i++)
1864
underscores(argv[optind + i]);
1866
/* If we have a list of modules to remove, try the unused ones first.
1867
Aliases and modules which don't seem to exist are handled later. */
1868
if (flags & mit_remove) {
1873
for (i = 0; i < num_modules; i++) {
1874
const char *modname;
1878
modname = argv[optind + i];
1881
if (module_in_kernel(modname, &usecount) != 1)
1886
read_depends(dirname, modname, &list);
1888
failed |= handle_module(modname, &list,
1889
cmdline_opts, cmdline_opts,
1890
&conf, dirname, error, flags);
1892
argv[optind + i] = NULL;
1893
INIT_LIST_HEAD(&list);
1895
} while (progress > 0);
1923
1898
/* num_modules is always 1 except for -r or -a. */
1924
1899
for (i = 0; i < num_modules; i++) {
1925
char *modname = argv[optind + i];
1928
dump_modversions(modname, error);
1930
failed |= do_modprobe(modname, newname, cmdline_opts,
1931
&conf, dirname, error, flags);
1900
const char *modname = argv[optind + i];
1905
failed |= do_modprobe(modname, cmdline_opts,
1906
&conf, dirname, error, flags);