kmodloader.c 19 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /*
  2. * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  3. * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License version 2.1
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #define _GNU_SOURCE
  15. #include <sys/syscall.h>
  16. #include <sys/mman.h>
  17. #include <sys/utsname.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <sys/syscall.h>
  21. #include <sys/types.h>
  22. #include <values.h>
  23. #include <errno.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <libgen.h>
  29. #include <glob.h>
  30. #include <elf.h>
  31. #include <libubox/avl.h>
  32. #include <libubox/avl-cmp.h>
  33. #include <libubox/utils.h>
  34. #include <libubox/ulog.h>
  35. #define DEF_MOD_PATH "/modules/%s/"
  36. enum {
  37. SCANNED,
  38. PROBE,
  39. LOADED,
  40. };
  41. struct module {
  42. char *name;
  43. char *depends;
  44. char *opts;
  45. int size;
  46. int usage;
  47. int state;
  48. int error;
  49. int refcnt; /* number of references from module_node.m */
  50. };
  51. struct module_node {
  52. struct avl_node avl;
  53. struct module *m;
  54. bool is_alias;
  55. };
  56. static struct avl_tree modules;
  57. static char **module_folders = NULL;
  58. static void free_module(struct module *m);
  59. static int init_module_folders(void)
  60. {
  61. int n = 0;
  62. struct stat st;
  63. struct utsname ver;
  64. char *s, *e, *p, path[256], ldpath[256];
  65. e = ldpath;
  66. s = getenv("LD_LIBRARY_PATH");
  67. if (s)
  68. e += snprintf(ldpath, sizeof(ldpath), "%s:", s);
  69. e += snprintf(e, sizeof(ldpath) - (e - ldpath), "/lib");
  70. uname(&ver);
  71. for (s = p = ldpath; p <= e; p++) {
  72. if (*p != ':' && *p != '\0')
  73. continue;
  74. *p = 0;
  75. snprintf(path, sizeof(path), "%s" DEF_MOD_PATH, s, ver.release);
  76. if (!stat(path, &st) && S_ISDIR(st.st_mode)) {
  77. module_folders = realloc(module_folders, sizeof(p) * (n + 2));
  78. if (!module_folders) {
  79. ULOG_ERR("out of memory\n");
  80. return -1;
  81. }
  82. module_folders[n++] = strdup(path);
  83. }
  84. s = p + 1;
  85. }
  86. if (!module_folders)
  87. return -1;
  88. module_folders[n] = NULL;
  89. return 0;
  90. }
  91. static struct module *find_module(const char *name)
  92. {
  93. struct module_node *mn;
  94. mn = avl_find_element(&modules, name, mn, avl);
  95. if (mn)
  96. return mn->m;
  97. else
  98. return NULL;
  99. }
  100. static void free_modules(void)
  101. {
  102. struct module_node *mn, *tmp;
  103. avl_remove_all_elements(&modules, mn, avl, tmp) {
  104. struct module *m = mn->m;
  105. m->refcnt -= 1;
  106. if (m->refcnt == 0)
  107. free_module(m);
  108. free(mn);
  109. }
  110. }
  111. static char* get_module_path(char *name)
  112. {
  113. char **p;
  114. static char path[256];
  115. struct stat s;
  116. if (!stat(name, &s) && S_ISREG(s.st_mode))
  117. return name;
  118. for (p = module_folders; *p; p++) {
  119. snprintf(path, sizeof(path), "%s%s.ko", *p, name);
  120. if (!stat(path, &s) && S_ISREG(s.st_mode))
  121. return path;
  122. }
  123. return NULL;
  124. }
  125. static char* get_module_name(char *path)
  126. {
  127. static char name[33];
  128. char *t;
  129. strncpy(name, basename(path), sizeof(name) - 1);
  130. t = strstr(name, ".ko");
  131. if (t)
  132. *t = '\0';
  133. return name;
  134. }
  135. static int elf64_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
  136. {
  137. const char *secnames;
  138. Elf64_Ehdr *e;
  139. Elf64_Shdr *sh;
  140. int i;
  141. e = (Elf64_Ehdr *) map;
  142. sh = (Elf64_Shdr *) (map + e->e_shoff);
  143. secnames = map + sh[e->e_shstrndx].sh_offset;
  144. for (i = 0; i < e->e_shnum; i++) {
  145. if (!strcmp(section, secnames + sh[i].sh_name)) {
  146. *size = sh[i].sh_size;
  147. *offset = sh[i].sh_offset;
  148. return 0;
  149. }
  150. }
  151. return -1;
  152. }
  153. static int elf32_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
  154. {
  155. const char *secnames;
  156. Elf32_Ehdr *e;
  157. Elf32_Shdr *sh;
  158. int i;
  159. e = (Elf32_Ehdr *) map;
  160. sh = (Elf32_Shdr *) (map + e->e_shoff);
  161. secnames = map + sh[e->e_shstrndx].sh_offset;
  162. for (i = 0; i < e->e_shnum; i++) {
  163. if (!strcmp(section, secnames + sh[i].sh_name)) {
  164. *size = sh[i].sh_size;
  165. *offset = sh[i].sh_offset;
  166. return 0;
  167. }
  168. }
  169. return -1;
  170. }
  171. static int elf_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
  172. {
  173. int clazz = map[EI_CLASS];
  174. int endian = map[EI_DATA];
  175. #if __BYTE_ORDER == __LITTLE_ENDIAN
  176. if (endian != ELFDATA2LSB)
  177. #elif __BYTE_ORDER == __BIG_ENDIAN
  178. if (endian != ELFDATA2MSB)
  179. #else
  180. #error "unsupported endian"
  181. #endif
  182. {
  183. ULOG_ERR("invalid endianess: %d\n", endian);
  184. return -1;
  185. }
  186. if (clazz == ELFCLASS32)
  187. return elf32_find_section(map, section, offset, size);
  188. else if (clazz == ELFCLASS64)
  189. return elf64_find_section(map, section, offset, size);
  190. ULOG_ERR("unknown elf format %d\n", clazz);
  191. return -1;
  192. }
  193. static struct module_node *
  194. alloc_module_node(const char *name, struct module *m, bool is_alias)
  195. {
  196. struct module_node *mn;
  197. char *_name;
  198. mn = calloc_a(sizeof(*mn),
  199. &_name, strlen(name) + 1);
  200. if (mn) {
  201. mn->avl.key = strcpy(_name, name);
  202. mn->m = m;
  203. mn->is_alias = is_alias;
  204. avl_insert(&modules, &mn->avl);
  205. m->refcnt += 1;
  206. }
  207. return mn;
  208. }
  209. static struct module *
  210. alloc_module(const char *name, const char * const *aliases, int naliases, const char *depends, int size)
  211. {
  212. struct module *m;
  213. char *_name, *_dep;
  214. int i;
  215. m = calloc_a(sizeof(*m),
  216. &_name, strlen(name) + 1,
  217. &_dep, depends ? strlen(depends) + 2 : 0);
  218. if (!m)
  219. return NULL;
  220. m->name = strcpy(_name, name);
  221. m->opts = 0;
  222. if (depends) {
  223. m->depends = strcpy(_dep, depends);
  224. while (*_dep) {
  225. if (*_dep == ',')
  226. *_dep = '\0';
  227. _dep++;
  228. }
  229. }
  230. m->size = size;
  231. m->refcnt = 0;
  232. alloc_module_node(m->name, m, false);
  233. for (i = 0; i < naliases; i++)
  234. alloc_module_node(aliases[i], m, true);
  235. return m;
  236. }
  237. static void free_module(struct module *m)
  238. {
  239. if (m->opts)
  240. free(m->opts);
  241. free(m);
  242. }
  243. static int scan_loaded_modules(void)
  244. {
  245. size_t buf_len = 0;
  246. char *buf = NULL;
  247. FILE *fp;
  248. fp = fopen("/proc/modules", "r");
  249. if (!fp) {
  250. ULOG_ERR("failed to open /proc/modules\n");
  251. return -1;
  252. }
  253. while (getline(&buf, &buf_len, fp) > 0) {
  254. struct module m;
  255. struct module *n;
  256. m.name = strtok(buf, " ");
  257. m.size = atoi(strtok(NULL, " "));
  258. m.usage = atoi(strtok(NULL, " "));
  259. m.depends = strtok(NULL, " ");
  260. if (!m.name || !m.depends)
  261. continue;
  262. n = find_module(m.name);
  263. if (!n) {
  264. /* possibly a module outside /lib/modules/<ver>/ */
  265. n = alloc_module(m.name, NULL, 0, m.depends, m.size);
  266. }
  267. n->usage = m.usage;
  268. n->state = LOADED;
  269. }
  270. free(buf);
  271. fclose(fp);
  272. return 0;
  273. }
  274. static struct module* get_module_info(const char *module, const char *name)
  275. {
  276. int fd = open(module, O_RDONLY);
  277. unsigned int offset, size;
  278. char *map = MAP_FAILED, *strings, *dep = NULL;
  279. const char **aliases = NULL;
  280. int naliases = 0;
  281. struct module *m = NULL;
  282. struct stat s;
  283. if (fd < 0) {
  284. ULOG_ERR("failed to open %s\n", module);
  285. goto out;
  286. }
  287. if (fstat(fd, &s) == -1) {
  288. ULOG_ERR("failed to stat %s\n", module);
  289. goto out;
  290. }
  291. map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  292. if (map == MAP_FAILED) {
  293. ULOG_ERR("failed to mmap %s\n", module);
  294. goto out;
  295. }
  296. if (elf_find_section(map, ".modinfo", &offset, &size)) {
  297. ULOG_ERR("failed to load the .modinfo section from %s\n", module);
  298. goto out;
  299. }
  300. strings = map + offset;
  301. while (true) {
  302. char *sep;
  303. int len;
  304. while (!strings[0])
  305. strings++;
  306. if (strings >= map + offset + size)
  307. break;
  308. sep = strstr(strings, "=");
  309. if (!sep)
  310. break;
  311. len = sep - strings;
  312. sep++;
  313. if (!strncmp(strings, "depends=", len + 1))
  314. dep = sep;
  315. else if (!strncmp(strings, "alias=", len + 1)) {
  316. aliases = realloc(aliases, sizeof(sep) * (naliases + 1));
  317. if (!aliases) {
  318. ULOG_ERR("out of memory\n");
  319. goto out;
  320. }
  321. aliases[naliases++] = sep;
  322. }
  323. strings = &sep[strlen(sep)];
  324. }
  325. m = alloc_module(name, aliases, naliases, dep, s.st_size);
  326. if (m)
  327. m->state = SCANNED;
  328. out:
  329. if (map != MAP_FAILED)
  330. munmap(map, s.st_size);
  331. if (fd >= 0)
  332. close(fd);
  333. free(aliases);
  334. return m;
  335. }
  336. static int scan_module_folder(const char *dir)
  337. {
  338. int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
  339. struct utsname ver;
  340. char *path;
  341. glob_t gl;
  342. int j, rv = 0;
  343. uname(&ver);
  344. path = alloca(strlen(dir) + sizeof("*.ko") + 1);
  345. sprintf(path, "%s*.ko", dir);
  346. if (glob(path, gl_flags, NULL, &gl) < 0)
  347. return -1;
  348. for (j = 0; j < gl.gl_pathc; j++) {
  349. char *name = get_module_name(gl.gl_pathv[j]);
  350. struct module *m;
  351. if (!name)
  352. continue;
  353. m = find_module(name);
  354. if (!m) {
  355. if (!get_module_info(gl.gl_pathv[j], name))
  356. rv |= -1;
  357. }
  358. }
  359. globfree(&gl);
  360. return rv;
  361. }
  362. static int scan_module_folders(void)
  363. {
  364. int rv = 0;
  365. char **p;
  366. if (init_module_folders())
  367. return -1;
  368. for (p = module_folders; *p; p++)
  369. rv |= scan_module_folder(*p);
  370. return rv;
  371. }
  372. static int print_modinfo(char *module)
  373. {
  374. int fd = open(module, O_RDONLY);
  375. unsigned int offset, size;
  376. struct stat s;
  377. char *map = MAP_FAILED, *strings;
  378. int rv = -1;
  379. if (fd < 0) {
  380. ULOG_ERR("failed to open %s\n", module);
  381. goto out;
  382. }
  383. if (fstat(fd, &s) == -1) {
  384. ULOG_ERR("failed to stat %s\n", module);
  385. goto out;
  386. }
  387. map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  388. if (map == MAP_FAILED) {
  389. ULOG_ERR("failed to mmap %s\n", module);
  390. goto out;
  391. }
  392. if (elf_find_section(map, ".modinfo", &offset, &size)) {
  393. ULOG_ERR("failed to load the .modinfo section from %s\n", module);
  394. goto out;
  395. }
  396. strings = map + offset;
  397. printf("module:\t\t%s\n", module);
  398. while (true) {
  399. char *dup = NULL;
  400. char *sep;
  401. while (!strings[0])
  402. strings++;
  403. if (strings >= map + offset + size)
  404. break;
  405. sep = strstr(strings, "=");
  406. if (!sep)
  407. break;
  408. dup = strndup(strings, sep - strings);
  409. sep++;
  410. if (strncmp(strings, "parm", 4)) {
  411. if (strlen(dup) < 7)
  412. printf("%s:\t\t%s\n", dup, sep);
  413. else
  414. printf("%s:\t%s\n", dup, sep);
  415. }
  416. strings = &sep[strlen(sep)];
  417. if (dup)
  418. free(dup);
  419. }
  420. rv = 0;
  421. out:
  422. if (map != MAP_FAILED)
  423. munmap(map, s.st_size);
  424. if (fd >= 0)
  425. close(fd);
  426. return rv;
  427. }
  428. static int deps_available(struct module *m, int verbose)
  429. {
  430. char *dep;
  431. int err = 0;
  432. if (!m->depends || !strcmp(m->depends, "-") || !strcmp(m->depends, ""))
  433. return 0;
  434. dep = m->depends;
  435. while (*dep) {
  436. m = find_module(dep);
  437. if (verbose && !m)
  438. ULOG_ERR("missing dependency %s\n", dep);
  439. if (verbose && m && (m->state != LOADED))
  440. ULOG_ERR("dependency not loaded %s\n", dep);
  441. if (!m || (m->state != LOADED))
  442. err++;
  443. dep += strlen(dep) + 1;
  444. }
  445. return err;
  446. }
  447. static int insert_module(char *path, const char *options)
  448. {
  449. void *data = 0;
  450. struct stat s;
  451. int fd, ret = -1;
  452. if (stat(path, &s)) {
  453. ULOG_ERR("missing module %s\n", path);
  454. return ret;
  455. }
  456. fd = open(path, O_RDONLY);
  457. if (fd < 0) {
  458. ULOG_ERR("cannot open %s\n", path);
  459. return ret;
  460. }
  461. data = malloc(s.st_size);
  462. if (!data) {
  463. ULOG_ERR("out of memory\n");
  464. goto out;
  465. }
  466. if (read(fd, data, s.st_size) == s.st_size) {
  467. ret = syscall(__NR_init_module, data, (unsigned long) s.st_size, options);
  468. if (errno == EEXIST)
  469. ret = 0;
  470. }
  471. else
  472. ULOG_ERR("failed to read full module %s\n", path);
  473. out:
  474. close(fd);
  475. free(data);
  476. return ret;
  477. }
  478. static void load_moddeps(struct module *_m)
  479. {
  480. char *dep;
  481. struct module *m;
  482. if (!strcmp(_m->depends, "-") || !strcmp(_m->depends, ""))
  483. return;
  484. dep = _m->depends;
  485. while (*dep) {
  486. m = find_module(dep);
  487. if (!m)
  488. ULOG_ERR("failed to find dependency %s\n", dep);
  489. if (m && (m->state != LOADED)) {
  490. m->state = PROBE;
  491. load_moddeps(m);
  492. }
  493. dep = dep + strlen(dep) + 1;
  494. }
  495. }
  496. static int iterations = 0;
  497. static int load_modprobe(void)
  498. {
  499. int loaded, todo;
  500. struct module_node *mn;
  501. struct module *m;
  502. avl_for_each_element(&modules, mn, avl) {
  503. if (mn->is_alias)
  504. continue;
  505. m = mn->m;
  506. if (m->state == PROBE)
  507. load_moddeps(m);
  508. }
  509. do {
  510. loaded = 0;
  511. todo = 0;
  512. avl_for_each_element(&modules, mn, avl) {
  513. if (mn->is_alias)
  514. continue;
  515. m = mn->m;
  516. if ((m->state == PROBE) && (!deps_available(m, 0))) {
  517. if (!insert_module(get_module_path(m->name), (m->opts) ? (m->opts) : (""))) {
  518. m->state = LOADED;
  519. m->error = 0;
  520. loaded++;
  521. continue;
  522. }
  523. m->error = 1;
  524. }
  525. if ((m->state == PROBE) || m->error)
  526. todo++;
  527. }
  528. iterations++;
  529. } while (loaded);
  530. return todo;
  531. }
  532. static int print_insmod_usage(void)
  533. {
  534. ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
  535. return -1;
  536. }
  537. static int print_modprobe_usage(void)
  538. {
  539. ULOG_INFO("Usage:\n\tmodprobe [-q] filename\n");
  540. return -1;
  541. }
  542. static int print_usage(char *arg)
  543. {
  544. ULOG_INFO("Usage:\n\t%s module\n", arg);
  545. return -1;
  546. }
  547. static int main_insmod(int argc, char **argv)
  548. {
  549. char *name, *cur, *options;
  550. int i, ret, len;
  551. if (argc < 2)
  552. return print_insmod_usage();
  553. name = get_module_name(argv[1]);
  554. if (!name) {
  555. ULOG_ERR("cannot find module - %s\n", argv[1]);
  556. return -1;
  557. }
  558. if (scan_loaded_modules())
  559. return -1;
  560. if (find_module(name)) {
  561. ULOG_ERR("module is already loaded - %s\n", name);
  562. return -1;
  563. }
  564. free_modules();
  565. for (len = 0, i = 2; i < argc; i++)
  566. len += strlen(argv[i]) + 1;
  567. options = malloc(len);
  568. if (!options) {
  569. ULOG_ERR("out of memory\n");
  570. ret = -1;
  571. goto err;
  572. }
  573. options[0] = 0;
  574. cur = options;
  575. for (i = 2; i < argc; i++) {
  576. if (options[0]) {
  577. *cur = ' ';
  578. cur++;
  579. }
  580. cur += sprintf(cur, "%s", argv[i]);
  581. }
  582. if (init_module_folders()) {
  583. fprintf(stderr, "Failed to find the folder holding the modules\n");
  584. ret = -1;
  585. goto err;
  586. }
  587. if (get_module_path(argv[1])) {
  588. name = argv[1];
  589. } else if (!get_module_path(name)) {
  590. fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
  591. ret = -1;
  592. goto err;
  593. }
  594. ret = insert_module(get_module_path(name), options);
  595. if (ret)
  596. ULOG_ERR("failed to insert %s\n", get_module_path(name));
  597. err:
  598. free(options);
  599. return ret;
  600. }
  601. static int main_rmmod(int argc, char **argv)
  602. {
  603. struct module *m;
  604. char *name;
  605. int ret;
  606. if (argc != 2)
  607. return print_usage("rmmod");
  608. if (scan_loaded_modules())
  609. return -1;
  610. name = get_module_name(argv[1]);
  611. m = find_module(name);
  612. if (!m) {
  613. ULOG_ERR("module is not loaded\n");
  614. return -1;
  615. }
  616. ret = syscall(__NR_delete_module, m->name, 0);
  617. if (ret)
  618. ULOG_ERR("unloading the module failed\n");
  619. free_modules();
  620. return ret;
  621. }
  622. static int main_lsmod(int argc, char **argv)
  623. {
  624. struct module_node *mn;
  625. struct module *m;
  626. char *dep;
  627. if (scan_loaded_modules())
  628. return -1;
  629. avl_for_each_element(&modules, mn, avl) {
  630. if (mn->is_alias)
  631. continue;
  632. m = mn->m;
  633. if (m->state == LOADED) {
  634. printf("%-20s%8d%3d ",
  635. m->name, m->size, m->usage);
  636. if (m->depends && strcmp(m->depends, "-") && strcmp(m->depends, "")) {
  637. dep = m->depends;
  638. while (*dep) {
  639. printf("%s", dep);
  640. dep = dep + strlen(dep) + 1;
  641. if (*dep)
  642. printf(",");
  643. }
  644. }
  645. printf("\n");
  646. }
  647. }
  648. free_modules();
  649. return 0;
  650. }
  651. static int main_modinfo(int argc, char **argv)
  652. {
  653. struct module *m;
  654. char *name;
  655. if (argc != 2)
  656. return print_usage("modinfo");
  657. if (scan_module_folders())
  658. return -1;
  659. name = get_module_name(argv[1]);
  660. m = find_module(name);
  661. if (!m) {
  662. ULOG_ERR("cannot find module - %s\n", argv[1]);
  663. return -1;
  664. }
  665. name = get_module_path(m->name);
  666. if (!name) {
  667. ULOG_ERR("cannot find path of module - %s\n", m->name);
  668. return -1;
  669. }
  670. print_modinfo(name);
  671. return 0;
  672. }
  673. static int main_modprobe(int argc, char **argv)
  674. {
  675. struct module_node *mn;
  676. struct module *m;
  677. char *name;
  678. char *mod = NULL;
  679. int opt;
  680. bool quiet = false;
  681. while ((opt = getopt(argc, argv, "q")) != -1 ) {
  682. switch (opt) {
  683. case 'q': /* shhhh! */
  684. quiet = true;
  685. break;
  686. default: /* '?' */
  687. return print_modprobe_usage();
  688. break;
  689. }
  690. }
  691. if (optind >= argc)
  692. return print_modprobe_usage(); /* expected module after options */
  693. mod = argv[optind];
  694. if (scan_module_folders())
  695. return -1;
  696. if (scan_loaded_modules())
  697. return -1;
  698. name = get_module_name(mod);
  699. m = find_module(name);
  700. if (m && m->state == LOADED) {
  701. if (!quiet)
  702. ULOG_ERR("%s is already loaded\n", name);
  703. return 0;
  704. } else if (!m) {
  705. if (!quiet)
  706. ULOG_ERR("failed to find a module named %s\n", name);
  707. return -1;
  708. } else {
  709. int fail;
  710. m->state = PROBE;
  711. fail = load_modprobe();
  712. if (fail) {
  713. ULOG_ERR("%d module%s could not be probed\n",
  714. fail, (fail == 1) ? ("") : ("s"));
  715. avl_for_each_element(&modules, mn, avl) {
  716. if (mn->is_alias)
  717. continue;
  718. m = mn->m;
  719. if ((m->state == PROBE) || m->error)
  720. ULOG_ERR("- %s\n", m->name);
  721. }
  722. }
  723. }
  724. free_modules();
  725. return 0;
  726. }
  727. static int main_loader(int argc, char **argv)
  728. {
  729. int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
  730. char *dir = "/etc/modules.d/";
  731. struct module_node *mn;
  732. struct module *m;
  733. glob_t gl;
  734. char *path;
  735. int fail, j;
  736. if (argc > 1)
  737. dir = argv[1];
  738. path = malloc(strlen(dir) + 2);
  739. if (!path) {
  740. ULOG_ERR("out of memory\n");
  741. return -1;
  742. }
  743. strcpy(path, dir);
  744. strcat(path, "*");
  745. if (scan_module_folders()) {
  746. free (path);
  747. return -1;
  748. }
  749. if (scan_loaded_modules()) {
  750. free (path);
  751. return -1;
  752. }
  753. ULOG_INFO("loading kernel modules from %s\n", path);
  754. if (glob(path, gl_flags, NULL, &gl) < 0)
  755. goto out;
  756. for (j = 0; j < gl.gl_pathc; j++) {
  757. FILE *fp = fopen(gl.gl_pathv[j], "r");
  758. size_t mod_len = 0;
  759. char *mod = NULL;
  760. if (!fp) {
  761. ULOG_ERR("failed to open %s\n", gl.gl_pathv[j]);
  762. continue;
  763. }
  764. while (getline(&mod, &mod_len, fp) > 0) {
  765. char *nl = strchr(mod, '\n');
  766. struct module *m;
  767. char *opts;
  768. if (nl)
  769. *nl = '\0';
  770. opts = strchr(mod, ' ');
  771. if (opts)
  772. *opts++ = '\0';
  773. m = find_module(get_module_name(mod));
  774. if (!m || (m->state == LOADED))
  775. continue;
  776. if (opts)
  777. m->opts = strdup(opts);
  778. m->state = PROBE;
  779. if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
  780. load_modprobe();
  781. }
  782. free(mod);
  783. fclose(fp);
  784. }
  785. fail = load_modprobe();
  786. if (fail) {
  787. ULOG_ERR("%d module%s could not be probed\n",
  788. fail, (fail == 1) ? ("") : ("s"));
  789. avl_for_each_element(&modules, mn, avl) {
  790. if (mn->is_alias)
  791. continue;
  792. m = mn->m;
  793. if ((m->state == PROBE) || (m->error))
  794. ULOG_ERR("- %s - %d\n", m->name, deps_available(m, 1));
  795. }
  796. } else {
  797. ULOG_INFO("done loading kernel modules from %s\n", path);
  798. }
  799. out:
  800. globfree(&gl);
  801. free(path);
  802. return 0;
  803. }
  804. static inline char weight(char c)
  805. {
  806. return c == '_' ? '-' : c;
  807. }
  808. static int avl_modcmp(const void *k1, const void *k2, void *ptr)
  809. {
  810. const char *s1 = k1;
  811. const char *s2 = k2;
  812. while (*s1 && (weight(*s1) == weight(*s2)))
  813. {
  814. s1++;
  815. s2++;
  816. }
  817. return (unsigned char)weight(*s1) - (unsigned char)weight(*s2);
  818. }
  819. int main(int argc, char **argv)
  820. {
  821. char *exec = basename(*argv);
  822. avl_init(&modules, avl_modcmp, false, NULL);
  823. if (!strcmp(exec, "insmod"))
  824. return main_insmod(argc, argv);
  825. if (!strcmp(exec, "rmmod"))
  826. return main_rmmod(argc, argv);
  827. if (!strcmp(exec, "lsmod"))
  828. return main_lsmod(argc, argv);
  829. if (!strcmp(exec, "modinfo"))
  830. return main_modinfo(argc, argv);
  831. if (!strcmp(exec, "modprobe"))
  832. return main_modprobe(argc, argv);
  833. ulog_open(ULOG_KMSG, LOG_USER, "kmodloader");
  834. return main_loader(argc, argv);
  835. }