modprobe.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Modprobe written from scratch for BusyBox
  4. *
  5. * Copyright (c) 2002 by Robert Griebl, griebl@gmx.de
  6. * Copyright (c) 2003 by Andrew Dennison, andrew.dennison@motec.com.au
  7. * Copyright (c) 2005 by Jim Bauer, jfbauer@nfr.com
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include <sys/utsname.h>
  25. #include <sys/types.h>
  26. #include <sys/wait.h>
  27. #include <getopt.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <syslog.h>
  31. #include <string.h>
  32. #include <ctype.h>
  33. #include <fcntl.h>
  34. #include "busybox.h"
  35. struct dep_t {
  36. char * m_name;
  37. char * m_path;
  38. char * m_options;
  39. int m_isalias : 1;
  40. int m_reserved : 15;
  41. int m_depcnt : 16;
  42. char ** m_deparr;
  43. struct dep_t * m_next;
  44. };
  45. struct mod_list_t {
  46. char * m_name;
  47. char * m_path;
  48. char * m_options;
  49. struct mod_list_t * m_prev;
  50. struct mod_list_t * m_next;
  51. };
  52. static struct dep_t *depend;
  53. static int autoclean, show_only, quiet, do_syslog, verbose;
  54. static int k_version;
  55. int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
  56. {
  57. char *tag, *value;
  58. while ( isspace ( *buffer ))
  59. buffer++;
  60. tag = value = buffer;
  61. while ( !isspace ( *value ))
  62. if (!*value) return 0;
  63. else value++;
  64. *value++ = 0;
  65. while ( isspace ( *value ))
  66. value++;
  67. if (!*value) return 0;
  68. *ptag = tag;
  69. *pvalue = value;
  70. return 1;
  71. }
  72. /* Jump through hoops to simulate how fgets() grabs just one line at a
  73. * time... Don't use any stdio since modprobe gets called from a kernel
  74. * thread and stdio junk can overflow the limited stack...
  75. */
  76. static char *reads ( int fd, char *buffer, size_t len )
  77. {
  78. int n = read ( fd, buffer, len );
  79. if ( n > 0 ) {
  80. char *p;
  81. buffer [len-1] = 0;
  82. p = strchr ( buffer, '\n' );
  83. if ( p ) {
  84. off_t offset;
  85. offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
  86. lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
  87. p[1] = 0;
  88. }
  89. return buffer;
  90. }
  91. else
  92. return 0;
  93. }
  94. static struct dep_t *build_dep ( void )
  95. {
  96. int fd;
  97. struct utsname un;
  98. struct dep_t *first = 0;
  99. struct dep_t *current = 0;
  100. char buffer[2048];
  101. char *filename = buffer;
  102. int continuation_line = 0;
  103. k_version = 0;
  104. if ( uname ( &un ))
  105. return 0;
  106. // check for buffer overflow in following code
  107. if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
  108. return 0;
  109. }
  110. if (un.release[0] == '2') {
  111. k_version = un.release[2] - '0';
  112. }
  113. strcpy ( filename, "/lib/modules/" );
  114. strcat ( filename, un.release );
  115. strcat ( filename, "/modules.dep" );
  116. if (( fd = open ( filename, O_RDONLY )) < 0 ) {
  117. /* Ok, that didn't work. Fall back to looking in /lib/modules */
  118. if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
  119. return 0;
  120. }
  121. }
  122. while ( reads ( fd, buffer, sizeof( buffer ))) {
  123. int l = bb_strlen ( buffer );
  124. char *p = 0;
  125. while ( isspace ( buffer [l-1] )) {
  126. buffer [l-1] = 0;
  127. l--;
  128. }
  129. if ( l == 0 ) {
  130. continuation_line = 0;
  131. continue;
  132. }
  133. if ( !continuation_line ) {
  134. char *col = strchr ( buffer, ':' );
  135. char *dot = col;
  136. if ( col ) {
  137. char *mods;
  138. char *modpath;
  139. char *mod;
  140. *col = 0;
  141. mods = strrchr ( buffer, '/' );
  142. if ( !mods )
  143. mods = buffer;
  144. else
  145. mods++;
  146. modpath = strchr ( buffer, '/' );
  147. if ( !modpath )
  148. modpath = buffer;
  149. #if defined(CONFIG_FEATURE_2_6_MODULES)
  150. if ((k_version > 4) && ( *(col-3) == '.' ) &&
  151. ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ))
  152. dot = col - 3;
  153. else
  154. #endif
  155. if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
  156. dot = col - 2;
  157. mod = bb_xstrndup ( mods, dot - mods );
  158. if ( !current ) {
  159. first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
  160. }
  161. else {
  162. current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
  163. current = current-> m_next;
  164. }
  165. current-> m_name = mod;
  166. current-> m_path = bb_xstrdup(modpath);
  167. current-> m_options = 0;
  168. current-> m_isalias = 0;
  169. current-> m_depcnt = 0;
  170. current-> m_deparr = 0;
  171. current-> m_next = 0;
  172. //printf ( "%s:\n", mod );
  173. p = col + 1;
  174. }
  175. else
  176. p = 0;
  177. }
  178. else
  179. p = buffer;
  180. while ( p && *p && isblank(*p))
  181. p++;
  182. if ( p && *p ) {
  183. char *end = &buffer [l-1];
  184. char *deps;
  185. char *dep;
  186. char *next;
  187. int ext = 0;
  188. while ( isblank ( *end ) || ( *end == '\\' ))
  189. end--;
  190. do
  191. {
  192. next = strchr (p, ' ' );
  193. if (next)
  194. {
  195. *next = 0;
  196. next--;
  197. }
  198. else
  199. next = end;
  200. deps = strrchr ( p, '/' );
  201. if ( !deps || ( deps < p )) {
  202. deps = p;
  203. while ( isblank ( *deps ))
  204. deps++;
  205. }
  206. else
  207. deps++;
  208. #if defined(CONFIG_FEATURE_2_6_MODULES)
  209. if ((k_version > 4) && ( *(next-2) == '.' ) && *(next-1) == 'k' &&
  210. ( *next == 'o' ))
  211. ext = 3;
  212. else
  213. #endif
  214. if (( *(next-1) == '.' ) && ( *next == 'o' ))
  215. ext = 2;
  216. /* Cope with blank lines */
  217. if ((next-deps-ext+1) <= 0)
  218. continue;
  219. dep = bb_xstrndup ( deps, next - deps - ext + 1 );
  220. current-> m_depcnt++;
  221. current-> m_deparr = (char **) xrealloc ( current-> m_deparr,
  222. sizeof ( char *) * current-> m_depcnt );
  223. current-> m_deparr [current-> m_depcnt - 1] = dep;
  224. //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
  225. p = next + 2;
  226. } while (next < end);
  227. }
  228. if ( buffer [l-1] == '\\' )
  229. continuation_line = 1;
  230. else
  231. continuation_line = 0;
  232. }
  233. close ( fd );
  234. // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
  235. #if defined(CONFIG_FEATURE_2_6_MODULES)
  236. if (( fd = open ( "/etc/modprobe.conf", O_RDONLY )) < 0 )
  237. #endif
  238. if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
  239. if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
  240. return first;
  241. continuation_line = 0;
  242. while ( reads ( fd, buffer, sizeof( buffer ))) {
  243. int l;
  244. char *p;
  245. p = strchr ( buffer, '#' );
  246. if ( p )
  247. *p = 0;
  248. l = bb_strlen ( buffer );
  249. while ( l && isspace ( buffer [l-1] )) {
  250. buffer [l-1] = 0;
  251. l--;
  252. }
  253. if ( l == 0 ) {
  254. continuation_line = 0;
  255. continue;
  256. }
  257. if ( !continuation_line ) {
  258. if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
  259. char *alias, *mod;
  260. if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
  261. // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod );
  262. if ( !current ) {
  263. first = current = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
  264. }
  265. else {
  266. current-> m_next = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
  267. current = current-> m_next;
  268. }
  269. current-> m_name = bb_xstrdup ( alias );
  270. current-> m_isalias = 1;
  271. if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) {
  272. current-> m_depcnt = 0;
  273. current-> m_deparr = 0;
  274. }
  275. else {
  276. current-> m_depcnt = 1;
  277. current-> m_deparr = xmalloc ( 1 * sizeof( char * ));
  278. current-> m_deparr[0] = bb_xstrdup ( mod );
  279. }
  280. current-> m_next = 0;
  281. }
  282. }
  283. else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
  284. char *mod, *opt;
  285. if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
  286. struct dep_t *dt;
  287. for ( dt = first; dt; dt = dt-> m_next ) {
  288. if ( strcmp ( dt-> m_name, mod ) == 0 )
  289. break;
  290. }
  291. if ( dt ) {
  292. dt-> m_options = xrealloc ( dt-> m_options, bb_strlen( opt ) + 1 );
  293. strcpy ( dt-> m_options, opt );
  294. // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_name, dt-> m_options );
  295. }
  296. }
  297. }
  298. }
  299. }
  300. close ( fd );
  301. return first;
  302. }
  303. /* return 1 = loaded, 0 = not loaded, -1 = can't tell */
  304. static int already_loaded (const char *name)
  305. {
  306. int fd;
  307. char buffer[4096];
  308. fd = open ("/proc/modules", O_RDONLY);
  309. if (fd < 0)
  310. return -1;
  311. while ( reads ( fd, buffer, sizeof( buffer ))) {
  312. char *p;
  313. p = strchr (buffer, ' ');
  314. if (p) {
  315. *p = 0;
  316. if (strcmp (name, buffer) == 0) {
  317. close (fd);
  318. return 1;
  319. }
  320. }
  321. }
  322. close (fd);
  323. return 0;
  324. }
  325. static int mod_process ( struct mod_list_t *list, int do_insert )
  326. {
  327. int rc = 0;
  328. char *argv[10];
  329. int argc;
  330. while ( list ) {
  331. argc = 0;
  332. if ( do_insert ) {
  333. if (already_loaded (list->m_name) != 1) {
  334. argv[argc++] = "insmod";
  335. if (do_syslog)
  336. argv[argc++] = "-s";
  337. if (autoclean)
  338. argv[argc++] = "-k";
  339. if (quiet)
  340. argv[argc++] = "-q";
  341. argv[argc++] = list-> m_path;
  342. if (list-> m_options)
  343. argv[argc++] = list-> m_options;
  344. }
  345. } else {
  346. /* modutils uses short name for removal */
  347. if (already_loaded (list->m_name) != 0) {
  348. argv[argc++] = "rmmod";
  349. if (do_syslog)
  350. argv[argc++] = "-s";
  351. argv[argc++] = list->m_name;
  352. }
  353. }
  354. argv[argc] = NULL;
  355. if (argc) {
  356. if (verbose) {
  357. int i;
  358. for (i=0; i<argc; i++)
  359. printf("%s ", argv[i]);
  360. printf("\n");
  361. }
  362. if (!show_only) {
  363. int rc2 = 0;
  364. int status;
  365. switch (fork()) {
  366. case -1:
  367. rc2 = 1;
  368. break;
  369. case 0: //child
  370. execvp(argv[0], argv);
  371. bb_perror_msg_and_die("exec of %s", argv[0]);
  372. /* NOTREACHED */
  373. default:
  374. if (wait(&status) == -1) {
  375. rc2 = 1;
  376. break;
  377. }
  378. if (WIFEXITED(status))
  379. rc2 = WEXITSTATUS(status);
  380. if (WIFSIGNALED(status))
  381. rc2 = WTERMSIG(status);
  382. break;
  383. }
  384. if (do_insert) {
  385. rc = rc2; /* only last module matters */
  386. }
  387. else if (!rc2) {
  388. rc = 0; /* success if remove any mod */
  389. }
  390. }
  391. }
  392. list = do_insert ? list-> m_prev : list-> m_next;
  393. }
  394. return (show_only) ? 0 : rc;
  395. }
  396. static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
  397. {
  398. struct mod_list_t *find;
  399. struct dep_t *dt;
  400. char *opt = 0;
  401. char *path = 0;
  402. // check dependencies
  403. for ( dt = depend; dt; dt = dt-> m_next ) {
  404. if ( strcmp ( dt-> m_name, mod ) == 0) {
  405. mod = dt-> m_name;
  406. path = dt-> m_path;
  407. opt = dt-> m_options;
  408. break;
  409. }
  410. }
  411. // resolve alias names
  412. while ( dt && dt-> m_isalias ) {
  413. if ( dt-> m_depcnt == 1 ) {
  414. struct dep_t *adt;
  415. for ( adt = depend; adt; adt = adt-> m_next ) {
  416. if ( strcmp ( adt-> m_name, dt-> m_deparr [0] ) == 0 )
  417. break;
  418. }
  419. if ( adt ) {
  420. dt = adt;
  421. mod = dt-> m_name;
  422. path = dt-> m_path;
  423. if ( !opt )
  424. opt = dt-> m_options;
  425. }
  426. else
  427. return;
  428. }
  429. else
  430. return;
  431. }
  432. if ( !path ) {
  433. bb_error_msg ("module %s not found.", mod);
  434. return;
  435. }
  436. // search for duplicates
  437. for ( find = *head; find; find = find-> m_next ) {
  438. if ( !strcmp ( mod, find-> m_name )) {
  439. // found -> dequeue it
  440. if ( find-> m_prev )
  441. find-> m_prev-> m_next = find-> m_next;
  442. else
  443. *head = find-> m_next;
  444. if ( find-> m_next )
  445. find-> m_next-> m_prev = find-> m_prev;
  446. else
  447. *tail = find-> m_prev;
  448. break; // there can be only one duplicate
  449. }
  450. }
  451. if ( !find ) { // did not find a duplicate
  452. find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
  453. find-> m_name = mod;
  454. find-> m_path = path;
  455. find-> m_options = opt;
  456. }
  457. // enqueue at tail
  458. if ( *tail )
  459. (*tail)-> m_next = find;
  460. find-> m_prev = *tail;
  461. find-> m_next = 0;
  462. if ( !*head )
  463. *head = find;
  464. *tail = find;
  465. if ( dt ) {
  466. int i;
  467. for ( i = 0; i < dt-> m_depcnt; i++ )
  468. check_dep ( dt-> m_deparr [i], head, tail );
  469. }
  470. }
  471. static int mod_insert ( char *mod, int argc, char **argv )
  472. {
  473. struct mod_list_t *tail = 0;
  474. struct mod_list_t *head = 0;
  475. int rc;
  476. // get dep list for module mod
  477. check_dep ( mod, &head, &tail );
  478. if ( head && tail ) {
  479. #if defined(CONFIG_FEATURE_2_6_MODULES)
  480. if ( argc ) {
  481. int i;
  482. int l = 0;
  483. // append module args
  484. for ( i = 0; i < argc; i++ )
  485. l += ( bb_strlen ( argv [i] ) + 1 );
  486. head-> m_options = xrealloc ( head-> m_options, l + 1 );
  487. head-> m_options [0] = 0;
  488. for ( i = 0; i < argc; i++ ) {
  489. strcat ( head-> m_options, argv [i] );
  490. strcat ( head-> m_options, " " );
  491. }
  492. }
  493. #endif
  494. // process tail ---> head
  495. rc = mod_process ( tail, 1 );
  496. }
  497. else
  498. rc = 1;
  499. return rc;
  500. }
  501. static int mod_remove ( char *mod )
  502. {
  503. int rc;
  504. static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
  505. struct mod_list_t *head = 0;
  506. struct mod_list_t *tail = 0;
  507. if ( mod )
  508. check_dep ( mod, &head, &tail );
  509. else // autoclean
  510. head = tail = &rm_a_dummy;
  511. if ( head && tail )
  512. rc = mod_process ( head, 0 ); // process head ---> tail
  513. else
  514. rc = 1;
  515. return rc;
  516. }
  517. extern int modprobe_main(int argc, char** argv)
  518. {
  519. int opt;
  520. int remove_opt = 0;
  521. autoclean = show_only = quiet = do_syslog = verbose = 0;
  522. while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
  523. switch(opt) {
  524. case 'c': // no config used
  525. case 'l': // no pattern matching
  526. return EXIT_SUCCESS;
  527. break;
  528. case 'C': // no config used
  529. case 't': // no pattern matching
  530. bb_error_msg_and_die("-t and -C not supported");
  531. case 'a': // ignore
  532. case 'd': // ignore
  533. break;
  534. case 'k':
  535. autoclean++;
  536. break;
  537. case 'n':
  538. show_only++;
  539. break;
  540. case 'q':
  541. quiet++;
  542. break;
  543. case 'r':
  544. remove_opt++;
  545. break;
  546. case 's':
  547. do_syslog++;
  548. break;
  549. case 'v':
  550. verbose++;
  551. break;
  552. case 'V':
  553. default:
  554. bb_show_usage();
  555. break;
  556. }
  557. }
  558. depend = build_dep ( );
  559. if ( !depend )
  560. bb_error_msg_and_die ( "could not parse modules.dep\n" );
  561. if (remove_opt) {
  562. int rc = EXIT_SUCCESS;
  563. do {
  564. if (mod_remove ( optind < argc ?
  565. bb_xstrdup (argv [optind]) : NULL )) {
  566. bb_error_msg ("failed to remove module %s",
  567. argv [optind] );
  568. rc = EXIT_FAILURE;
  569. }
  570. } while ( ++optind < argc );
  571. return rc;
  572. }
  573. if (optind >= argc)
  574. bb_error_msg_and_die ( "No module or pattern provided\n" );
  575. if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ))
  576. bb_error_msg_and_die ( "failed to load module %s", argv [optind] );
  577. return EXIT_SUCCESS;
  578. }