kmodloader.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  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 <ctype.h>
  32. #include <libubox/avl.h>
  33. #include <libubox/avl-cmp.h>
  34. #include <libubox/utils.h>
  35. #include <libubox/ulog.h>
  36. #include <libubox/kvlist.h>
  37. #include <libubox/list.h>
  38. #define DEF_MOD_PATH "/modules/%s/"
  39. #define MOD_BUILTIN "modules.builtin"
  40. #define MOD_BUILTIN_MODINFO "modules.builtin.modinfo"
  41. /* duplicated from in-kernel include/linux/module.h */
  42. #define MODULE_NAME_LEN (64 - sizeof(unsigned long))
  43. struct param {
  44. char *name;
  45. char *desc;
  46. char *type;
  47. struct list_head list;
  48. };
  49. enum {
  50. BUILTIN,
  51. SCANNED,
  52. PROBE,
  53. LOADED,
  54. BLACKLISTED,
  55. };
  56. struct module {
  57. char *name;
  58. char *depends;
  59. char *opts;
  60. int size;
  61. int usage;
  62. int state;
  63. int error;
  64. int refcnt; /* number of references from module_node.m */
  65. };
  66. struct module_node {
  67. struct avl_node avl;
  68. struct module *m;
  69. bool is_alias;
  70. };
  71. static struct avl_tree modules;
  72. static KVLIST(options, kvlist_strlen);
  73. static char **module_folders = NULL;
  74. static void free_module(struct module *m);
  75. static int init_module_folders(void)
  76. {
  77. int n = 0;
  78. struct stat st;
  79. struct utsname ver;
  80. char *s, *e, *p, path[330], ldpath[256];
  81. e = ldpath;
  82. s = getenv("LD_LIBRARY_PATH");
  83. if (s)
  84. e += snprintf(ldpath, sizeof(ldpath), "%s:", s);
  85. e += snprintf(e, sizeof(ldpath) - (e - ldpath), "/lib");
  86. uname(&ver);
  87. for (s = p = ldpath; p <= e; p++) {
  88. if (*p != ':' && *p != '\0')
  89. continue;
  90. *p = 0;
  91. snprintf(path, sizeof(path), "%s" DEF_MOD_PATH, s, ver.release);
  92. if (!stat(path, &st) && S_ISDIR(st.st_mode)) {
  93. module_folders = realloc(module_folders, sizeof(p) * (n + 2));
  94. if (!module_folders) {
  95. ULOG_ERR("out of memory\n");
  96. return -1;
  97. }
  98. module_folders[n++] = strdup(path);
  99. }
  100. s = p + 1;
  101. }
  102. if (!module_folders) {
  103. ULOG_ERR("no module folders for kernel version %s found\n", ver.release);
  104. return -1;
  105. }
  106. module_folders[n] = NULL;
  107. return 0;
  108. }
  109. static struct module *find_module(const char *name)
  110. {
  111. struct module_node *mn;
  112. mn = avl_find_element(&modules, name, mn, avl);
  113. if (mn)
  114. return mn->m;
  115. else
  116. return NULL;
  117. }
  118. static void free_modules(void)
  119. {
  120. struct module_node *mn, *tmp;
  121. avl_remove_all_elements(&modules, mn, avl, tmp) {
  122. struct module *m = mn->m;
  123. m->refcnt -= 1;
  124. if (m->refcnt == 0)
  125. free_module(m);
  126. free(mn);
  127. }
  128. }
  129. static char* get_module_path(char *name)
  130. {
  131. char **p;
  132. static char path[256];
  133. struct stat s;
  134. if (!stat(name, &s) && S_ISREG(s.st_mode))
  135. return name;
  136. for (p = module_folders; *p; p++) {
  137. snprintf(path, sizeof(path), "%s%s.ko", *p, name);
  138. if (!stat(path, &s) && S_ISREG(s.st_mode))
  139. return path;
  140. }
  141. return NULL;
  142. }
  143. static char* get_module_name(char *path)
  144. {
  145. static char name[MODULE_NAME_LEN];
  146. char *t;
  147. strncpy(name, basename(path), sizeof(name) - 1);
  148. t = strstr(name, ".ko");
  149. if (t)
  150. *t = '\0';
  151. return name;
  152. }
  153. static int elf64_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
  154. {
  155. const char *secnames;
  156. Elf64_Ehdr *e;
  157. Elf64_Shdr *sh;
  158. int i;
  159. e = (Elf64_Ehdr *) map;
  160. sh = (Elf64_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 elf32_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
  172. {
  173. const char *secnames;
  174. Elf32_Ehdr *e;
  175. Elf32_Shdr *sh;
  176. int i;
  177. e = (Elf32_Ehdr *) map;
  178. sh = (Elf32_Shdr *) (map + e->e_shoff);
  179. secnames = map + sh[e->e_shstrndx].sh_offset;
  180. for (i = 0; i < e->e_shnum; i++) {
  181. if (!strcmp(section, secnames + sh[i].sh_name)) {
  182. *size = sh[i].sh_size;
  183. *offset = sh[i].sh_offset;
  184. return 0;
  185. }
  186. }
  187. return -1;
  188. }
  189. static int elf_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
  190. {
  191. int clazz = map[EI_CLASS];
  192. int endian = map[EI_DATA];
  193. #if __BYTE_ORDER == __LITTLE_ENDIAN
  194. if (endian != ELFDATA2LSB)
  195. #elif __BYTE_ORDER == __BIG_ENDIAN
  196. if (endian != ELFDATA2MSB)
  197. #else
  198. #error "unsupported endian"
  199. #endif
  200. {
  201. ULOG_ERR("invalid endianess: %d\n", endian);
  202. return -1;
  203. }
  204. if (clazz == ELFCLASS32)
  205. return elf32_find_section(map, section, offset, size);
  206. else if (clazz == ELFCLASS64)
  207. return elf64_find_section(map, section, offset, size);
  208. ULOG_ERR("unknown elf format %d\n", clazz);
  209. return -1;
  210. }
  211. static struct module_node *
  212. alloc_module_node(const char *name, struct module *m, bool is_alias)
  213. {
  214. struct module_node *mn;
  215. char *_name;
  216. mn = calloc_a(sizeof(*mn),
  217. &_name, strlen(name) + 1);
  218. if (mn) {
  219. mn->avl.key = strcpy(_name, name);
  220. mn->m = m;
  221. mn->is_alias = is_alias;
  222. avl_insert(&modules, &mn->avl);
  223. m->refcnt += 1;
  224. }
  225. return mn;
  226. }
  227. static struct module *
  228. alloc_module(const char *name, const char * const *aliases, int naliases, const char *depends, int size)
  229. {
  230. struct module *m;
  231. char *_name, *_dep;
  232. int i;
  233. m = calloc_a(sizeof(*m),
  234. &_name, strlen(name) + 1,
  235. &_dep, depends ? strlen(depends) + 2 : 0);
  236. if (!m)
  237. return NULL;
  238. m->name = strcpy(_name, name);
  239. m->opts = 0;
  240. if (depends) {
  241. m->depends = strcpy(_dep, depends);
  242. while (*_dep) {
  243. if (*_dep == ',')
  244. *_dep = '\0';
  245. _dep++;
  246. }
  247. }
  248. m->size = size;
  249. m->refcnt = 0;
  250. alloc_module_node(m->name, m, false);
  251. for (i = 0; i < naliases; i++)
  252. alloc_module_node(aliases[i], m, true);
  253. return m;
  254. }
  255. static void free_module(struct module *m)
  256. {
  257. if (m->opts)
  258. free(m->opts);
  259. free(m);
  260. }
  261. static int scan_loaded_modules(void)
  262. {
  263. size_t buf_len = 0;
  264. char *buf = NULL;
  265. int rv = -1;
  266. FILE *fp;
  267. fp = fopen("/proc/modules", "r");
  268. if (!fp) {
  269. ULOG_ERR("failed to open /proc/modules\n");
  270. return -1;
  271. }
  272. while (getline(&buf, &buf_len, fp) > 0) {
  273. struct module m;
  274. struct module *n;
  275. m.name = strtok(buf, " ");
  276. m.size = atoi(strtok(NULL, " "));
  277. m.usage = atoi(strtok(NULL, " "));
  278. m.depends = strtok(NULL, " ");
  279. if (!m.name || !m.depends)
  280. continue;
  281. n = find_module(m.name);
  282. if (!n) {
  283. /* possibly a module outside /lib/modules/<ver>/ */
  284. n = alloc_module(m.name, NULL, 0, m.depends, m.size);
  285. }
  286. if (!n) {
  287. ULOG_ERR("Failed to allocate memory for module\n");
  288. goto out;
  289. }
  290. n->usage = m.usage;
  291. n->state = LOADED;
  292. }
  293. rv = 0;
  294. out:
  295. free(buf);
  296. fclose(fp);
  297. return rv;
  298. }
  299. static char *mmap_modinfo(const char *module, const char *name, struct stat *s, unsigned int *offset, unsigned int *size)
  300. {
  301. const bool is_builtin = (module == NULL);
  302. const char *mpath = NULL;
  303. char *map = MAP_FAILED;
  304. char path[350], **f;
  305. int fd = -1;
  306. if (is_builtin)
  307. for (f = module_folders; *f; f++) {
  308. snprintf(path, sizeof(path), "%s%s", *f, MOD_BUILTIN_MODINFO);
  309. if (!stat(path, s) && S_ISREG(s->st_mode)) {
  310. mpath = path;
  311. break;
  312. }
  313. }
  314. else
  315. mpath = module;
  316. if (!mpath) {
  317. ULOG_ERR("cannot find modinfo path of module - %s\n", name);
  318. goto out;
  319. }
  320. fd = open(mpath, O_RDONLY);
  321. if (fd < 0) {
  322. ULOG_ERR("failed to open %s\n", mpath);
  323. goto out;
  324. }
  325. if (fstat(fd, s) == -1) {
  326. ULOG_ERR("failed to stat %s\n", mpath);
  327. goto out;
  328. }
  329. map = mmap(NULL, s->st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  330. if (map == MAP_FAILED) {
  331. ULOG_ERR("failed to mmap %s\n", mpath);
  332. goto out;
  333. }
  334. if (is_builtin) {
  335. *offset = 0;
  336. *size = s->st_size;
  337. } else if (elf_find_section(map, ".modinfo", offset, size)) {
  338. ULOG_ERR("failed to load the .modinfo section from %s\n", mpath);
  339. munmap(map, s->st_size);
  340. map = MAP_FAILED;
  341. }
  342. out:
  343. if (fd >= 0)
  344. close(fd);
  345. return map;
  346. }
  347. static struct module* get_module_info(const char *module, const char *name)
  348. {
  349. const bool is_builtin = (module == NULL);
  350. unsigned int offset, size;
  351. char *map, *strings, *dep = NULL;
  352. const char **aliases = NULL;
  353. const char **aliasesr;
  354. int naliases = 0;
  355. struct module *m = NULL;
  356. struct stat s;
  357. map = mmap_modinfo(module, name, &s, &offset, &size);
  358. if (map == MAP_FAILED)
  359. goto out;
  360. strings = map + offset;
  361. while (true) {
  362. char *end = map + offset + size;
  363. char *sep;
  364. int len;
  365. while ((strings < end) && !strings[0])
  366. strings++;
  367. if (strings >= end)
  368. break;
  369. if (is_builtin) {
  370. sep = strstr(strings, ".");
  371. if (!sep)
  372. break;
  373. if (strlen(name) == (sep - strings) &&
  374. !strncmp(strings, name, sep - strings))
  375. strings = sep + 1;
  376. else
  377. goto next_string;
  378. }
  379. sep = strstr(strings, "=");
  380. if (!sep)
  381. break;
  382. len = sep - strings;
  383. sep++;
  384. if (!strncmp(strings, "depends=", len + 1))
  385. dep = sep;
  386. else if (!strncmp(strings, "alias=", len + 1)) {
  387. aliasesr = realloc(aliases, sizeof(sep) * (naliases + 1));
  388. if (!aliasesr) {
  389. ULOG_ERR("out of memory\n");
  390. goto out;
  391. }
  392. aliases = aliasesr;
  393. aliases[naliases++] = sep;
  394. }
  395. next_string:
  396. strings = &sep[strlen(sep)];
  397. }
  398. m = alloc_module(name, aliases, naliases, dep, is_builtin ? 0 : s.st_size);
  399. if (m)
  400. m->state = is_builtin ? BUILTIN : SCANNED;
  401. out:
  402. if (map != MAP_FAILED)
  403. munmap(map, s.st_size);
  404. free(aliases);
  405. return m;
  406. }
  407. static int scan_builtin_modules(void)
  408. {
  409. char **p, path[350];
  410. size_t buf_len = 0;
  411. char *buf = NULL;
  412. struct stat st;
  413. FILE *fp = NULL;
  414. int rv = -1;
  415. if (!module_folders && init_module_folders())
  416. return -1;
  417. for (p = module_folders; *p; p++) {
  418. snprintf(path, sizeof(path), "%s%s", *p, MOD_BUILTIN);
  419. fp = fopen(path, "r");
  420. if (!fp)
  421. continue;
  422. if (!fstat(fileno(fp), &st) && S_ISREG(st.st_mode))
  423. break;
  424. /* Not regular file, close it and check next */
  425. fclose(fp);
  426. fp = NULL;
  427. }
  428. if (!fp)
  429. return 0; /* OK if modules.builtin unavailable */
  430. while (getline(&buf, &buf_len, fp) > 0) {
  431. struct module *m;
  432. char *name;
  433. name = get_module_name(buf);
  434. if (!name)
  435. continue;
  436. m = find_module(name);
  437. if (m && !strcmp(m->name, name)) {
  438. ULOG_WARN("found duplicate builtin module %s\n", name);
  439. continue;
  440. }
  441. m = get_module_info(NULL, name);
  442. if (!m) {
  443. ULOG_ERR("failed to find info for builtin module %s\n", name);
  444. goto err;
  445. }
  446. }
  447. rv = 0;
  448. err:
  449. free(buf);
  450. fclose(fp);
  451. return rv;
  452. }
  453. static int scan_module_folder(const char *dir)
  454. {
  455. int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
  456. char *path;
  457. glob_t gl;
  458. int j, rv = 0;
  459. path = alloca(strlen(dir) + sizeof("*.ko") + 1);
  460. sprintf(path, "%s*.ko", dir);
  461. if (glob(path, gl_flags, NULL, &gl) < 0)
  462. return -1;
  463. for (j = 0; j < gl.gl_pathc; j++) {
  464. char *name = get_module_name(gl.gl_pathv[j]);
  465. struct module *m;
  466. char *opts;
  467. if (!name)
  468. continue;
  469. m = find_module(name);
  470. if (m)
  471. continue;
  472. m = get_module_info(gl.gl_pathv[j], name);
  473. if (!m) {
  474. rv |= -1;
  475. continue;
  476. }
  477. opts = kvlist_get(&options, name);
  478. if (!opts)
  479. continue;
  480. if (*opts == '\x01')
  481. m->state = BLACKLISTED;
  482. else
  483. m->opts = strdup(opts);
  484. }
  485. globfree(&gl);
  486. return rv;
  487. }
  488. static int scan_module_folders(void)
  489. {
  490. int rv = 0;
  491. char **p;
  492. if (init_module_folders())
  493. return -1;
  494. for (p = module_folders; *p; p++)
  495. rv |= scan_module_folder(*p);
  496. return rv;
  497. }
  498. static int print_modinfo(const struct module *m)
  499. {
  500. const bool is_builtin = (m->state == BUILTIN);
  501. unsigned int offset, size;
  502. struct param *p;
  503. struct stat s;
  504. char *map, *strings, *mpath;
  505. int rv = -1;
  506. LIST_HEAD(params);
  507. mpath = get_module_path(m->name);
  508. map = mmap_modinfo(mpath, m->name, &s, &offset, &size);
  509. if (map == MAP_FAILED)
  510. goto out;
  511. strings = map + offset;
  512. if (is_builtin)
  513. printf("name:\t\t%s\n", m->name);
  514. printf("filename:\t%s\n", is_builtin ? "(builtin)" : mpath);
  515. while (true) {
  516. char *end = map + offset + size;
  517. char *pname, *pdata;
  518. char *dup = NULL;
  519. char *sep, *sep2;
  520. while ((strings < end) && !strings[0])
  521. strings++;
  522. if (strings >= end)
  523. break;
  524. if (is_builtin) {
  525. sep = strstr(strings, ".");
  526. if (!sep)
  527. break;
  528. if (strlen(m->name) == (sep - strings) &&
  529. !strncmp(strings, m->name, sep - strings))
  530. strings = sep + 1;
  531. else
  532. goto next_string;
  533. }
  534. sep = strstr(strings, "=");
  535. if (!sep)
  536. break;
  537. dup = strndup(strings, sep - strings);
  538. sep++;
  539. if (strncmp(strings, "parm", 4)) {
  540. if (strlen(dup) < 7)
  541. printf("%s:\t\t%s\n", dup, sep);
  542. else
  543. printf("%s:\t%s\n", dup, sep);
  544. } else {
  545. sep2 = strstr(sep, ":");
  546. if (!sep2) {
  547. free(dup);
  548. break;
  549. }
  550. pname = strndup(sep, sep2 - sep);
  551. sep2++;
  552. pdata = strdup(sep2);
  553. list_for_each_entry(p, &params, list)
  554. if (!strcmp(pname, p->name))
  555. break;
  556. if (list_entry_is_h(p, &params, list)) {
  557. p = alloca(sizeof(*p));
  558. p->name = pname;
  559. p->desc = p->type = NULL;
  560. list_add(&p->list, &params);
  561. } else {
  562. free(pname);
  563. }
  564. if (!strcmp(dup, "parmtype"))
  565. p->type = pdata;
  566. else
  567. p->desc = pdata;
  568. }
  569. free(dup);
  570. next_string:
  571. strings = &sep[strlen(sep)];
  572. }
  573. list_for_each_entry(p, &params, list) {
  574. printf("parm:\t\t%s", p->name);
  575. if (p->desc)
  576. printf(":%s", p->desc);
  577. if (p->type)
  578. printf(" (%s)", p->type);
  579. printf("\n");
  580. free(p->name);
  581. free(p->desc);
  582. free(p->type);
  583. }
  584. rv = 0;
  585. out:
  586. if (map != MAP_FAILED)
  587. munmap(map, s.st_size);
  588. return rv;
  589. }
  590. static int deps_available(struct module *m, int verbose)
  591. {
  592. char *dep;
  593. int err = 0;
  594. if (!m->depends || !strcmp(m->depends, "-") || !strcmp(m->depends, ""))
  595. return 0;
  596. dep = m->depends;
  597. while (*dep) {
  598. m = find_module(dep);
  599. if (verbose && !m)
  600. ULOG_ERR("missing dependency %s\n", dep);
  601. if (verbose && m && (m->state != LOADED))
  602. ULOG_ERR("dependency not loaded %s\n", dep);
  603. if (!m || (m->state != LOADED))
  604. err++;
  605. dep += strlen(dep) + 1;
  606. }
  607. return err;
  608. }
  609. static int insert_module(char *path, const char *options)
  610. {
  611. void *data = 0;
  612. struct stat s;
  613. int fd, ret = -1;
  614. if (!path) {
  615. ULOG_ERR("Path not specified\n");
  616. return ret;
  617. }
  618. if (stat(path, &s)) {
  619. ULOG_ERR("missing module %s\n", path);
  620. return ret;
  621. }
  622. fd = open(path, O_RDONLY);
  623. if (fd < 0) {
  624. ULOG_ERR("cannot open %s\n", path);
  625. return ret;
  626. }
  627. data = malloc(s.st_size);
  628. if (!data) {
  629. ULOG_ERR("out of memory\n");
  630. goto out;
  631. }
  632. if (read(fd, data, s.st_size) == s.st_size) {
  633. ret = syscall(__NR_init_module, data, (unsigned long) s.st_size, options);
  634. if (errno == EEXIST)
  635. ret = 0;
  636. }
  637. else
  638. ULOG_ERR("failed to read full module %s\n", path);
  639. out:
  640. close(fd);
  641. free(data);
  642. return ret;
  643. }
  644. static void load_moddeps(struct module *_m)
  645. {
  646. char *dep;
  647. struct module *m;
  648. if (!strcmp(_m->depends, "-") || !strcmp(_m->depends, ""))
  649. return;
  650. dep = _m->depends;
  651. while (*dep) {
  652. m = find_module(dep);
  653. if (!m)
  654. ULOG_ERR("failed to find dependency %s\n", dep);
  655. if (m && (m->state != LOADED)) {
  656. m->state = PROBE;
  657. load_moddeps(m);
  658. }
  659. dep = dep + strlen(dep) + 1;
  660. }
  661. }
  662. static int load_modprobe(bool allow_load_retry)
  663. {
  664. int loaded, skipped, failed;
  665. struct module_node *mn;
  666. struct module *m;
  667. bool load_retry = false;
  668. static bool first_iteration = true;
  669. avl_for_each_element(&modules, mn, avl) {
  670. if (mn->is_alias)
  671. continue;
  672. m = mn->m;
  673. if (m->state == PROBE)
  674. load_moddeps(m);
  675. }
  676. do {
  677. loaded = 0;
  678. skipped = 0;
  679. failed = 0;
  680. avl_for_each_element(&modules, mn, avl) {
  681. if (mn->is_alias)
  682. continue;
  683. m = mn->m;
  684. if ((m->state == PROBE) && (!deps_available(m, 0)) && (!m->error || load_retry)) {
  685. if (!insert_module(get_module_path(m->name), (m->opts) ? (m->opts) : (""))) {
  686. m->state = LOADED;
  687. m->error = 0;
  688. loaded++;
  689. continue;
  690. }
  691. m->error = 1;
  692. }
  693. if (m->error)
  694. failed++;
  695. else if (m->state == PROBE)
  696. skipped++;
  697. }
  698. if (allow_load_retry) {
  699. /* if we can't load anything else let's try to load failed modules */
  700. load_retry = loaded ? (failed && !skipped) : (failed && !load_retry && !first_iteration);
  701. }
  702. first_iteration = false;
  703. } while (loaded || load_retry);
  704. return skipped + failed;
  705. }
  706. static int print_insmod_usage(void)
  707. {
  708. ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
  709. return -1;
  710. }
  711. static int print_modprobe_usage(void)
  712. {
  713. ULOG_INFO(
  714. "Usage:\n"
  715. "\tmodprobe [-q] [-v] filename\n"
  716. "\tmodprobe -a [-q] [-v] filename [filename...]\n"
  717. );
  718. return -1;
  719. }
  720. static int print_usage(char *arg)
  721. {
  722. ULOG_INFO("Usage:\n\t%s module\n", arg);
  723. return -1;
  724. }
  725. static int main_insmod(int argc, char **argv)
  726. {
  727. char *name, *cur, *options;
  728. int i, ret, len;
  729. if (argc < 2)
  730. return print_insmod_usage();
  731. name = get_module_name(argv[1]);
  732. if (!name) {
  733. ULOG_ERR("cannot find module - %s\n", argv[1]);
  734. return -1;
  735. }
  736. if (scan_loaded_modules())
  737. return -1;
  738. if (find_module(name)) {
  739. ULOG_ERR("module is already loaded - %s\n", name);
  740. return -1;
  741. }
  742. free_modules();
  743. for (len = 0, i = 2; i < argc; i++)
  744. len += strlen(argv[i]) + 1;
  745. options = malloc(len);
  746. if (!options) {
  747. ULOG_ERR("out of memory\n");
  748. ret = -1;
  749. goto err;
  750. }
  751. options[0] = 0;
  752. cur = options;
  753. for (i = 2; i < argc; i++) {
  754. if (options[0]) {
  755. *cur = ' ';
  756. cur++;
  757. }
  758. cur += sprintf(cur, "%s", argv[i]);
  759. }
  760. if (init_module_folders()) {
  761. fprintf(stderr, "Failed to find the folder holding the modules\n");
  762. ret = -1;
  763. goto err;
  764. }
  765. if (get_module_path(argv[1])) {
  766. name = argv[1];
  767. } else if (!get_module_path(name)) {
  768. fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
  769. ret = -1;
  770. goto err;
  771. }
  772. ret = insert_module(get_module_path(name), options);
  773. if (ret)
  774. ULOG_ERR("failed to insert %s\n", get_module_path(name));
  775. err:
  776. free(options);
  777. return ret;
  778. }
  779. static int main_rmmod(int argc, char **argv)
  780. {
  781. struct module *m;
  782. char *name;
  783. int ret;
  784. if (argc != 2)
  785. return print_usage("rmmod");
  786. if (scan_loaded_modules())
  787. return -1;
  788. if (scan_builtin_modules())
  789. return -1;
  790. name = get_module_name(argv[1]);
  791. m = find_module(name);
  792. if (!m) {
  793. ULOG_ERR("module is not loaded\n");
  794. return -1;
  795. }
  796. if (m->state == BUILTIN) {
  797. ULOG_ERR("module is builtin\n");
  798. return -1;
  799. }
  800. ret = syscall(__NR_delete_module, m->name, 0);
  801. if (ret)
  802. ULOG_ERR("unloading the module failed\n");
  803. free_modules();
  804. return ret;
  805. }
  806. static int main_lsmod(int argc, char **argv)
  807. {
  808. struct module_node *mn;
  809. struct module *m;
  810. char *dep;
  811. if (scan_loaded_modules())
  812. return -1;
  813. avl_for_each_element(&modules, mn, avl) {
  814. if (mn->is_alias)
  815. continue;
  816. m = mn->m;
  817. if (m->state == LOADED) {
  818. printf("%-20s%8d%3d ",
  819. m->name, m->size, m->usage);
  820. if (m->depends && strcmp(m->depends, "-") && strcmp(m->depends, "")) {
  821. dep = m->depends;
  822. while (*dep) {
  823. printf("%s", dep);
  824. dep = dep + strlen(dep) + 1;
  825. if (*dep)
  826. printf(",");
  827. }
  828. }
  829. printf("\n");
  830. }
  831. }
  832. free_modules();
  833. return 0;
  834. }
  835. static int main_modinfo(int argc, char **argv)
  836. {
  837. struct module *m;
  838. char *name;
  839. if (argc != 2)
  840. return print_usage("modinfo");
  841. if (scan_module_folders())
  842. return -1;
  843. if (scan_builtin_modules())
  844. return -1;
  845. name = get_module_name(argv[1]);
  846. m = find_module(name);
  847. if (!m) {
  848. ULOG_ERR("cannot find module - %s\n", argv[1]);
  849. return -1;
  850. }
  851. print_modinfo(m);
  852. return 0;
  853. }
  854. static int main_modprobe(int argc, char **argv)
  855. {
  856. struct module_node *mn;
  857. struct module *m;
  858. int exit_code = 0;
  859. int load_fail;
  860. int log_level = LOG_WARNING;
  861. int opt;
  862. bool quiet = false;
  863. bool use_all = false;
  864. while ((opt = getopt(argc, argv, "aqv")) != -1 ) {
  865. switch (opt) {
  866. case 'a':
  867. use_all = true;
  868. break;
  869. case 'q': /* shhhh! */
  870. quiet = true;
  871. break;
  872. case 'v':
  873. log_level = LOG_DEBUG;
  874. break;
  875. default: /* '?' */
  876. return print_modprobe_usage();
  877. break;
  878. }
  879. }
  880. if (optind >= argc)
  881. return print_modprobe_usage(); /* expected module after options */
  882. /* after print_modprobe_usage() so it won't be filtered out */
  883. ulog_threshold(log_level);
  884. if (scan_module_folders())
  885. return -1;
  886. if (scan_loaded_modules())
  887. return -1;
  888. if (scan_builtin_modules())
  889. return -1;
  890. do {
  891. char *name;
  892. name = get_module_name(argv[optind]);
  893. m = find_module(name);
  894. if (m && m->state == BLACKLISTED) {
  895. if (!quiet)
  896. ULOG_INFO("%s is blacklisted\n", name);
  897. } else if (m && m->state == LOADED) {
  898. if (!quiet)
  899. ULOG_INFO("%s is already loaded\n", name);
  900. } else if (m && m->state == BUILTIN) {
  901. if (!quiet)
  902. ULOG_INFO("%s is builtin\n", name);
  903. } else if (!m) {
  904. if (!quiet)
  905. ULOG_ERR("failed to find a module named %s\n", name);
  906. exit_code = -1;
  907. } else {
  908. m->state = PROBE;
  909. }
  910. optind++;
  911. } while (use_all && optind < argc);
  912. load_fail = load_modprobe(true);
  913. if (load_fail) {
  914. ULOG_ERR("%d module%s could not be probed\n",
  915. load_fail, (load_fail == 1) ? ("") : ("s"));
  916. avl_for_each_element(&modules, mn, avl) {
  917. if (mn->is_alias)
  918. continue;
  919. m = mn->m;
  920. if ((m->state == PROBE) || m->error)
  921. ULOG_ERR("- %s\n", m->name);
  922. }
  923. exit_code = -1;
  924. }
  925. free_modules();
  926. return exit_code;
  927. }
  928. static int main_loader(int argc, char **argv)
  929. {
  930. int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
  931. char *dir = "/etc/modules.d/";
  932. struct module_node *mn;
  933. struct module *m;
  934. glob_t gl;
  935. char *path;
  936. int ret = 0, fail, j;
  937. if (argc > 1)
  938. dir = argv[1];
  939. path = malloc(strlen(dir) + 2);
  940. if (!path) {
  941. ULOG_ERR("out of memory\n");
  942. return -1;
  943. }
  944. strcpy(path, dir);
  945. strcat(path, "*");
  946. if (scan_module_folders()) {
  947. ret = -1;
  948. goto free_path;
  949. }
  950. if (scan_loaded_modules()) {
  951. ret = -1;
  952. goto free_path;
  953. }
  954. ULOG_INFO("loading kernel modules from %s\n", path);
  955. if (glob(path, gl_flags, NULL, &gl) < 0)
  956. goto out;
  957. for (j = 0; j < gl.gl_pathc; j++) {
  958. FILE *fp = fopen(gl.gl_pathv[j], "r");
  959. size_t mod_len = 0;
  960. char *mod = NULL;
  961. if (!fp) {
  962. ULOG_ERR("failed to open %s\n", gl.gl_pathv[j]);
  963. continue;
  964. }
  965. while (getline(&mod, &mod_len, fp) > 0) {
  966. char *nl = strchr(mod, '\n');
  967. struct module *m;
  968. char *opts;
  969. if (nl)
  970. *nl = '\0';
  971. opts = strchr(mod, ' ');
  972. if (opts)
  973. *opts++ = '\0';
  974. m = find_module(get_module_name(mod));
  975. if (!m || m->state == LOADED || m->state == BLACKLISTED)
  976. continue;
  977. if (opts) {
  978. if (m->opts) {
  979. char *prev = m->opts;
  980. fail = asprintf(&m->opts, "%s %s", prev, opts);
  981. free(prev);
  982. if (fail < 0) {
  983. ULOG_ERR("out of memory for opts %s\n", opts);
  984. free(mod);
  985. fclose(fp);
  986. ret = -1;
  987. goto out;
  988. }
  989. } else {
  990. m->opts = strdup(opts);
  991. }
  992. }
  993. m->state = PROBE;
  994. if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
  995. load_modprobe(false);
  996. }
  997. free(mod);
  998. fclose(fp);
  999. }
  1000. fail = load_modprobe(true);
  1001. if (fail) {
  1002. ULOG_ERR("%d module%s could not be probed\n",
  1003. fail, (fail == 1) ? ("") : ("s"));
  1004. avl_for_each_element(&modules, mn, avl) {
  1005. if (mn->is_alias)
  1006. continue;
  1007. m = mn->m;
  1008. if ((m->state == PROBE) || (m->error))
  1009. ULOG_ERR("- %s - %d\n", m->name, deps_available(m, 1));
  1010. }
  1011. } else {
  1012. ULOG_INFO("done loading kernel modules from %s\n", path);
  1013. }
  1014. out:
  1015. globfree(&gl);
  1016. free_path:
  1017. free(path);
  1018. return ret;
  1019. }
  1020. static inline char weight(char c)
  1021. {
  1022. return c == '_' ? '-' : c;
  1023. }
  1024. static int avl_modcmp(const void *k1, const void *k2, void *ptr)
  1025. {
  1026. const char *s1 = k1;
  1027. const char *s2 = k2;
  1028. while (*s1 && (weight(*s1) == weight(*s2)))
  1029. {
  1030. s1++;
  1031. s2++;
  1032. }
  1033. return (unsigned char)weight(*s1) - (unsigned char)weight(*s2);
  1034. }
  1035. static void
  1036. load_options(void)
  1037. {
  1038. static char buf[512];
  1039. char *s;
  1040. FILE *f;
  1041. f = fopen("/etc/modules.conf", "r");
  1042. if (!f)
  1043. return;
  1044. while ((s = fgets(buf, sizeof(buf), f)) != NULL) {
  1045. char *c, *cmd, *mod;
  1046. while (isspace(*s))
  1047. s++;
  1048. c = strchr(s, '#');
  1049. if (c)
  1050. *c = 0;
  1051. while (isspace(*s))
  1052. s++;
  1053. c = s + strlen(s);
  1054. while (c > s && isspace(c[-1])) {
  1055. c[-1] = 0;
  1056. c--;
  1057. }
  1058. cmd = strsep(&s, " \t");
  1059. if (!cmd || !*cmd)
  1060. continue;
  1061. while (isspace(*s))
  1062. s++;
  1063. mod = strsep(&s, " \t");
  1064. if (!mod || !*mod)
  1065. continue;
  1066. if (!strcmp(cmd, "blacklist")) {
  1067. kvlist_set(&options, mod, "\x01");
  1068. continue;
  1069. }
  1070. if (!strcmp(cmd, "options")) {
  1071. char *prev = kvlist_get(&options, mod);
  1072. char *val = NULL;
  1073. while (isspace(*s))
  1074. s++;
  1075. if (!*s)
  1076. continue;
  1077. if (prev && prev[0] == '\x01')
  1078. continue;
  1079. if (!prev) {
  1080. kvlist_set(&options, mod, s);
  1081. continue;
  1082. }
  1083. if (asprintf(&val, "%s %s", prev, s) < 0)
  1084. continue;
  1085. kvlist_set(&options, mod, val);
  1086. free(val);
  1087. continue;
  1088. }
  1089. }
  1090. fclose(f);
  1091. }
  1092. int main(int argc, char **argv)
  1093. {
  1094. char *exec = basename(*argv);
  1095. avl_init(&modules, avl_modcmp, false, NULL);
  1096. if (!strcmp(exec, "insmod"))
  1097. return main_insmod(argc, argv);
  1098. if (!strcmp(exec, "rmmod"))
  1099. return main_rmmod(argc, argv);
  1100. if (!strcmp(exec, "lsmod"))
  1101. return main_lsmod(argc, argv);
  1102. if (!strcmp(exec, "modinfo"))
  1103. return main_modinfo(argc, argv);
  1104. load_options();
  1105. if (!strcmp(exec, "modprobe"))
  1106. return main_modprobe(argc, argv);
  1107. ulog_open(ULOG_KMSG, LOG_USER, "kmodloader");
  1108. return main_loader(argc, argv);
  1109. }