comparison toys/pending/modprobe.c @ 1249:b13728c24a58 draft

modprobe: cleanup, incorporate Ashwini's fix for alias loading Move <fnmatch.h> to toys.h, since it's POSIX. Avoid duplicating code in an if/else block. Terser error messages, spelling. Don't always print the state.
author Isaac Dunham <ibid.ag@gmail.com>
date Wed, 09 Apr 2014 17:26:09 -0500
parents 7b3c1238380a
children 4a7438307429
comparison
equal deleted inserted replaced
1248:407357afa07f 1249:b13728c24a58
25 -b Apply blacklist to module names too 25 -b Apply blacklist to module names too
26 */ 26 */
27 #define FOR_modprobe 27 #define FOR_modprobe
28 #include "toys.h" 28 #include "toys.h"
29 #include <sys/syscall.h> 29 #include <sys/syscall.h>
30 #include <fnmatch.h>
31 30
32 GLOBALS( 31 GLOBALS(
33 struct arg_list *probes; 32 struct arg_list *probes;
34 struct arg_list *dbase[256]; 33 struct arg_list *dbase[256];
35 char *cmdopts; 34 char *cmdopts;
148 return rev; 147 return rev;
149 } 148 }
150 149
151 /* 150 /*
152 * Returns struct module_s from the data base if found, NULL otherwise. 151 * Returns struct module_s from the data base if found, NULL otherwise.
153 * if ps - create module entry, add it to data base and return the same mod. 152 * if add - create module entry, add it to data base and return the same mod.
154 */ 153 */
155 static struct module_s *get_mod(char *mod, uint8_t ps) 154 static struct module_s *get_mod(char *mod, uint8_t add)
156 { 155 {
157 char name[MODNAME_LEN]; 156 char name[MODNAME_LEN];
158 struct module_s *modentry; 157 struct module_s *modentry;
159 struct arg_list *temp; 158 struct arg_list *temp;
160 unsigned i, hash = 0; 159 unsigned i, hash = 0;
164 hash %= DBASE_SIZE; 163 hash %= DBASE_SIZE;
165 for (temp = TT.dbase[hash]; temp; temp = temp->next) { 164 for (temp = TT.dbase[hash]; temp; temp = temp->next) {
166 modentry = (struct module_s *) temp->arg; 165 modentry = (struct module_s *) temp->arg;
167 if (!strcmp(modentry->name, name)) return modentry; 166 if (!strcmp(modentry->name, name)) return modentry;
168 } 167 }
169 if (!ps) return NULL; 168 if (!add) return NULL;
170 modentry = xzalloc(sizeof(*modentry)); 169 modentry = xzalloc(sizeof(*modentry));
171 modentry->name = xstrdup(name); 170 modentry->name = xstrdup(name);
172 llist_add(&TT.dbase[hash], modentry); 171 llist_add(&TT.dbase[hash], modentry);
173 return modentry; 172 return modentry;
174 } 173 }
300 *tmp = '\0'; 299 *tmp = '\0';
301 char *name = basename(line); 300 char *name = basename(line);
302 301
303 tmp = strchr(name, '.'); 302 tmp = strchr(name, '.');
304 if (tmp) *tmp = '\0'; 303 if (tmp) *tmp = '\0';
305 if (!cmdname) { 304 if (!cmdname || !fnmatch(cmdname, name, 0)) {
306 if (tmp) *tmp = '.'; 305 if (tmp) *tmp = '.';
307 xprintf("%s\n", line); 306 dbg("%s\n", line);
308 ret = 0;
309 } else if (!fnmatch(cmdname, name, 0)) {
310 if (tmp) *tmp = '.';
311 xprintf("%s\n", line);
312 ret = 0; 307 ret = 0;
313 } 308 }
314 } 309 }
315 free(line); 310 free(line);
316 } 311 }
503 if (flags & FLAG_v) dbg = xprintf; 498 if (flags & FLAG_v) dbg = xprintf;
504 499
505 if ((toys.optc < 1) && (((flags & FLAG_r) && (flags & FLAG_l)) 500 if ((toys.optc < 1) && (((flags & FLAG_r) && (flags & FLAG_l))
506 ||(!((flags & FLAG_r)||(flags & FLAG_l))))) { 501 ||(!((flags & FLAG_r)||(flags & FLAG_l))))) {
507 toys.exithelp++; 502 toys.exithelp++;
508 error_exit(" Syntex Error."); 503 error_exit("bad syntax");
509 } 504 }
510 // Check for -r flag without arg if yes then do auto remove. 505 // Check for -r flag without arg if yes then do auto remove.
511 if ((flags & FLAG_r) && (!toys.optc)) { 506 if ((flags & FLAG_r) && (!toys.optc)) {
512 if (rm_mod(NULL, O_NONBLOCK | O_EXCL) != 0) perror_exit("rmmod"); 507 if (rm_mod(NULL, O_NONBLOCK | O_EXCL) != 0) perror_exit("rmmod");
513 return; 508 return;
521 // modules.dep processing for dependency check. 516 // modules.dep processing for dependency check.
522 if (flags & FLAG_l) { 517 if (flags & FLAG_l) {
523 if (depmode_read_entry(toys.optargs[0])) error_exit("no module found."); 518 if (depmode_read_entry(toys.optargs[0])) error_exit("no module found.");
524 return; 519 return;
525 } 520 }
526 // Read /proc/modules to get loadded modules. 521 // Read /proc/modules to get loaded modules.
527 fs = xfopen("/proc/modules", "r"); 522 fs = xfopen("/proc/modules", "r");
528 523
529 while (read_line(fs, &procline) > 0) { 524 while (read_line(fs, &procline) > 0) {
530 *(strchr(procline, ' ')) = '\0'; 525 *(strchr(procline, ' ')) = '\0';
531 get_mod(procline, 1)->flags = MOD_ALOADED; 526 get_mod(procline, 1)->flags = MOD_ALOADED;
540 } else { 535 } else {
541 add_mod(argv[0]); 536 add_mod(argv[0]);
542 TT.cmdopts = add_cmdopt(argv); 537 TT.cmdopts = add_cmdopt(argv);
543 } 538 }
544 if (!TT.probes) { 539 if (!TT.probes) {
545 fprintf(stderr, "All modules loaded successfully. \n"); 540 dbg("All modules loaded\n");
546 return; 541 return;
547 } 542 }
548 dirtree_read("/etc/modprobe.conf", config_action); 543 dirtree_read("/etc/modprobe.conf", config_action);
549 dirtree_read("/etc/modprobe.d", config_action); 544 dirtree_read("/etc/modprobe.d", config_action);
550 if (TT.symreq) dirtree_read("modules.symbols", config_action); 545 if (TT.symreq) dirtree_read("modules.symbols", config_action);
559 if (!(flags & FLAG_b) || !(module->flags & MOD_BLACKLIST)) 554 if (!(flags & FLAG_b) || !(module->flags & MOD_BLACKLIST))
560 go_probe(module); 555 go_probe(module);
561 continue; 556 continue;
562 } 557 }
563 do { // Probe all real names for the alias. 558 do { // Probe all real names for the alias.
564 char *real = llist_pop(&module->rnames); 559 char *real = ((struct arg_list*)llist_pop(&module->rnames))->arg;
565 struct module_s *m2 = get_mod(real, 0); 560 struct module_s *m2 = get_mod(real, 0);
566 561
567 dbg("probing alias %s by realname %s\n", module->name, real); 562 dbg("probing alias %s by realname %s\n", module->name, real);
568 if (!m2) continue; 563 if (!m2) continue;
569 if (!(m2->flags & MOD_BLACKLIST) 564 if (!(m2->flags & MOD_BLACKLIST)