~apw/module-init-tools/debian-merge-3.16

« back to all changes in this revision

Viewing changes to modprobe.c

* New upstream release.
  + modprobe -o is not supported anymore.
  + Added support for modules.devname for the benefit of udev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
291
291
        }
292
292
}
293
293
 
294
 
static void replace_modname(struct elf_file *module,
295
 
                            void *mem, unsigned long len,
296
 
                            const char *oldname, const char *newname)
297
 
{
298
 
        char *p;
299
 
 
300
 
        /* 64 - sizeof(unsigned long) - 1 */
301
 
        if (strlen(newname) > 55)
302
 
                fatal("New name %s is too long\n", newname);
303
 
 
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) {
307
 
                        strcpy(p, newname);
308
 
                        return;
309
 
                }
310
 
        }
311
 
 
312
 
        warn("Could not find old name in %s to replace!\n", module->pathname);
313
 
}
314
 
 
315
 
static void rename_module(struct elf_file *module,
316
 
                          const char *oldname,
317
 
                          const char *newname)
318
 
{
319
 
        void *modstruct;
320
 
        unsigned long len;
321
 
 
322
 
        /* Old-style */
323
 
        modstruct = module->ops->load_section(module,
324
 
                ".gnu.linkonce.this_module", &len);
325
 
        /* New-style */
326
 
        if (!modstruct)
327
 
                modstruct = module->ops->load_section(module, "__module", &len);
328
 
        if (!modstruct)
329
 
                warn("Could not find module name to change in %s\n",
330
 
                     module->pathname);
331
 
        else
332
 
                replace_modname(module, modstruct, len, oldname, newname);
333
 
}
334
 
 
335
294
static void clear_magic(struct elf_file *module)
336
295
{
337
296
        struct string_table *tbl;
942
901
                        struct string_table *post_modnames = NULL;
943
902
                        struct module_softdep *new;
944
903
 
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);
 
908
 
948
909
                        while ((tk = strsep_skipspace(&ptr, "\t ")) != NULL) {
 
910
                                tk = underscores(tk);
 
911
 
949
912
                                if (streq(tk, "pre:")) {
950
913
                                        pre = 1; post = 0;
951
914
                                } else if (streq(tk, "post:")) {
1310
1273
 
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)
1326
1288
{
1327
1289
        struct string_table *pre_modnames, *post_modnames;
 
1290
        modprobe_flags_t softdep_flags = flags;
1328
1291
        int i, j;
1329
1292
 
 
1293
        softdep_flags &= ~mit_first_time;
 
1294
        softdep_flags &= ~mit_ignore_commands;
 
1295
        if (flags & mit_remove)
 
1296
                softdep_flags |= mit_quiet_inuse;
 
1297
 
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;
1349
1317
 
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);
1352
1320
        }
1353
1321
 
1354
1322
        /* Modprobe main module, passing cmdline_opts, ignoring softdep */
1355
1323
 
1356
 
        do_modprobe(softdep->modname, NULL, cmdline_opts,
 
1324
        do_modprobe(softdep->modname, cmdline_opts,
1357
1325
                conf, dirname, warn, flags | mit_ignore_commands);
1358
1326
 
1359
1327
        /* Modprobe post_modnames */
1362
1330
                /* Reverse module order if removing. */
1363
1331
                j = (flags & mit_remove) ? post_modnames->cnt-1 - i : i;
1364
1332
 
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);
1367
1335
        }
1368
1336
}
1369
1337
 
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)
1399
1365
                {
1400
1366
                        error("Error inserting %s (%s): %s\n",
1401
1367
                                mod->modname, mod->filename,
1405
1371
        }
1406
1372
 
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);
1409
1375
 
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);
1414
1379
                goto out;
1415
1380
        }
1416
1381
 
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);
1421
1385
                goto out;
1422
1386
        }
1423
1387
 
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);
 
1395
                                mod->modname);
1432
1396
                } else {
1433
1397
                        do_command(mod->modname, command, flags & mit_dry_run,
1434
1398
                                   error, "install", cmdline_opts);
1443
1407
                                strerror(errno));
1444
1408
                goto out;
1445
1409
        }
1446
 
        if (newname)
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);
 
1428
                                      mod->modname);
1467
1429
                        goto out_elf_file;
1468
1430
                }
1469
1431
                /* don't warn noisely if we're loading multiple aliases. */
1484
1446
 
1485
1447
/* Do recursive removal. */
1486
1448
static void rmmod(struct list_head *list,
1487
 
                  const char *name,
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);
1502
1463
 
1503
 
        if (!name)
1504
 
                name = mod->modname;
1505
 
 
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;
1510
1468
 
1512
1470
 
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;
1518
1475
        }
1519
1476
 
1534
1491
 
1535
1492
        if (usecount != 0) {
1536
1493
                if (!(flags & mit_quiet_inuse))
1537
 
                        error("Module %s is in use.\n", name);
 
1494
                        error("Module %s is in use.\n", mod->modname);
1538
1495
                goto remove_rest;
1539
1496
        }
1540
1497
 
1543
1500
        if (flags & mit_dry_run)
1544
1501
                goto remove_rest;
1545
1502
 
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));
1552
1509
        }
1553
1510
 
1558
1515
                flags &= ~mit_ignore_commands;
1559
1516
                flags |= mit_quiet_inuse;
1560
1517
 
1561
 
                rmmod(list, NULL, "", conf, dirname, warn, flags);
 
1518
                rmmod(list, "", conf, dirname, warn, flags);
1562
1519
        }
1563
1520
        free_module(mod);
1564
1521
        return;
1571
1528
 
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)
1581
1537
{
1582
1538
        if (list_empty(todo_list)) {
1583
 
                const struct module_softdep *softdep;
1584
1539
                const char *command;
1585
1540
 
1586
1541
                /* The dependencies have to be real modules, but
1587
1542
                   handle case where the first is completely bogus. */
1588
1543
 
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));
1593
 
                        return 0;
1594
 
                }
1595
 
 
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,
1606
1554
        }
1607
1555
 
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);
1611
1559
        else
1612
 
                insmod(todo_list, options, newname,
 
1560
                insmod(todo_list, options,
1613
1561
                       cmdline_opts, conf, dirname, error, flags);
1614
1562
 
1615
1563
        return 0;
1632
1580
        return 0;
1633
1581
}
1634
1582
 
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,
1640
1587
                errfn_t error,
1641
1588
                modprobe_flags_t flags)
1642
1589
{
1643
 
        char *modname;
1644
1590
        struct module_alias *matching_aliases;
1645
1591
        LIST_HEAD(list);
1646
1592
        int failed = 0;
1647
1593
 
1648
 
        /* Convert name we are looking for */
1649
 
        modname = underscores(NOFAIL(strdup(modulename)));
1650
 
 
1651
1594
        matching_aliases = find_aliases(conf->aliases, modname);
1652
1595
 
1653
1596
        /* No luck?  Try symbol names, if starts with symbol:. */
1706
1649
 
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);
1711
1654
 
1712
1655
                        aliases = aliases->next;
1718
1661
                    && find_blacklist(modname, conf->blacklist))
1719
1662
                        goto out;
1720
1663
 
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);
1723
1666
        }
1724
1667
 
1725
1668
out:
1726
 
        free(modname);
1727
1669
        free_aliases(matching_aliases);
1728
1670
        return failed;
1729
1671
}
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' },
1769
1710
        const char *configname = NULL;
1770
1711
        char *basedir = "";
1771
1712
        char *cmdline_opts = NULL;
1772
 
        char *newname = NULL;
1773
1713
        char *dirname;
1774
1714
        errfn_t error = fatal;
1775
1715
        int failed = 0;
1782
1722
        argv = merge_args(getenv("MODPROBE_OPTIONS"), argv, &argc);
1783
1723
 
1784
1724
        uname(&buf);
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){
1786
1726
                switch (opt) {
1787
1727
                case 'V':
1788
1728
                        puts(PACKAGE " version " VERSION);
1822
1762
                case 'R':
1823
1763
                        flags |= mit_resolve_alias;
1824
1764
                        break;
1825
 
                case 'o':
1826
 
                        newname = optarg;
1827
 
                        break;
1828
1765
                case 'r':
1829
1766
                        flags |= mit_remove;
1830
1767
                        break;
1890
1827
        if (type)
1891
1828
                fatal("-t only supported with -l");
1892
1829
 
 
1830
        if (dump_modver) {
 
1831
                dump_modversions(argv[optind], error);
 
1832
                goto out;
 
1833
        }
 
1834
 
1893
1835
        /* Read aliases, options etc. */
1894
1836
        parse_toplevel_config(configname, &conf, dump_config, flags & mit_remove);
1895
1837
 
1896
1838
        /* Read module options from kernel command line */
1897
 
        parse_kcmdline(1, &conf.options);
 
1839
        parse_kcmdline(dump_config, &conf.options);
1898
1840
        
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);
1905
1847
 
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);
1911
1850
 
1920
1859
                cmdline_opts = gather_options(argv+optind+1);
1921
1860
        }
1922
1861
 
 
1862
        /* Convert names we are looking for */
 
1863
        for (i = 0; i < num_modules; i++)
 
1864
                underscores(argv[optind + i]);
 
1865
 
 
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) {
 
1869
                int progress;
 
1870
                do {
 
1871
                        progress = 0;
 
1872
 
 
1873
                        for (i = 0; i < num_modules; i++) {
 
1874
                                const char *modname;
 
1875
                                unsigned usecount;
 
1876
                                LIST_HEAD(list);
 
1877
 
 
1878
                                modname = argv[optind + i];
 
1879
                                if (!modname)
 
1880
                                        continue;
 
1881
                                if (module_in_kernel(modname, &usecount) != 1)
 
1882
                                        continue;
 
1883
                                if (usecount != 0)
 
1884
                                        continue;
 
1885
 
 
1886
                                read_depends(dirname, modname, &list);
 
1887
 
 
1888
                                failed |= handle_module(modname, &list,
 
1889
                                        cmdline_opts, cmdline_opts,
 
1890
                                        &conf, dirname, error, flags);
 
1891
                                progress++;
 
1892
                                argv[optind + i] = NULL;
 
1893
                                INIT_LIST_HEAD(&list);
 
1894
                        }
 
1895
                } while (progress > 0);
 
1896
        }
 
1897
 
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];
1926
 
 
1927
 
                if (dump_modver)
1928
 
                        dump_modversions(modname, error);
1929
 
                else
1930
 
                        failed |= do_modprobe(modname, newname, cmdline_opts,
1931
 
                                &conf, dirname, error, flags);
1932
 
 
 
1900
                const char *modname = argv[optind + i];
 
1901
 
 
1902
                if (!modname)
 
1903
                        continue;
 
1904
 
 
1905
                failed |= do_modprobe(modname, cmdline_opts,
 
1906
                        &conf, dirname, error, flags);
1933
1907
        }
1934
1908
 
1935
1909
out: