12d2d78315ae60cf874f33d97dceb7a78a543462
[platform/upstream/toybox.git] / toys / pending / modprobe.c
1 /* modprobe.c - modprobe utility.
2  *
3  * Copyright 2012 Madhur Verma <mad.flexi@gmail.com>
4  * Copyright 2013 Kyungwan Han <asura321@gmail.com>
5  *
6  * No Standard.
7
8 USE_MODPROBE(NEWTOY(modprobe, "alrqvsDb", TOYFLAG_SBIN))
9
10 config MODPROBE
11   bool "modprobe"
12   default n
13   help
14     usage: modprobe [-alrqvsDb] MODULE [symbol=value][...]
15
16     modprobe utility - inserts modules and dependencies.
17
18     -a  Load multiple MODULEs
19     -l  List (MODULE is a pattern)
20     -r  Remove MODULE (stacks) or do autoclean
21     -q  Quiet
22     -v  Verbose
23     -s  Log to syslog
24     -D  Show dependencies
25     -b  Apply blacklist to module names too
26 */
27 #define FOR_modprobe
28 #include "toys.h"
29 #include <sys/syscall.h>
30
31 GLOBALS(
32   struct arg_list *probes;
33   struct arg_list *dbase[256];
34   char *cmdopts;
35   int nudeps;
36   uint8_t symreq;
37   void (*dbg)(char *format, ...);
38 )
39
40 /* Note: if "#define DBASE_SIZE" modified, 
41  * Please update GLOBALS dbase[256] accordingly.
42  */
43 #define DBASE_SIZE  256
44 #define MODNAME_LEN 256
45
46 // Modules flag definations
47 #define MOD_ALOADED   0x0001
48 #define MOD_BLACKLIST 0x0002
49 #define MOD_FNDDEPMOD 0x0004
50 #define MOD_NDDEPS    0x0008
51
52 // dummy interface for debugging.
53 static void dummy(char *format, ...)
54 {
55 }
56
57 // Current probing modules info
58 struct module_s {
59   uint32_t flags;
60   char *cmdname, *name, *depent, *opts;
61   struct arg_list *rnames, *dep;
62 };
63
64 // Converts path name FILE to module name.
65 static char *path2mod(char *file, char *mod)
66 {
67   int i;
68   char *from, *lslash;
69
70   if (!file) return NULL;
71   if (!mod) mod = xmalloc(MODNAME_LEN);
72         
73   lslash = strrchr(file, '/');
74   if (!lslash || (lslash == file && !lslash[1])) from = file;
75   else from = lslash + 1;
76   
77   for (i = 0; i < (MODNAME_LEN-1) && from[i] && from[i] != '.'; i++)
78     mod[i] = (from[i] == '-') ? '_' : from[i];
79   mod[i] = '\0';
80   return mod;
81 }
82
83 // Add options in opts from toadd.
84 static char *add_opts(char *opts, char *toadd)
85 {
86   if (toadd) {
87     int optlen = 0;
88
89     if (opts) optlen = strlen(opts);
90     opts = xrealloc(opts, optlen + strlen(toadd) + 2);
91     sprintf(opts + optlen, " %s", toadd);
92   }
93   return opts;
94 }
95
96 // Remove first element from the list and return it.
97 static void *llist_popme(struct arg_list **head)
98 {
99   char *data = NULL;
100   struct arg_list *temp = *head;
101
102   if (temp) {
103     data = temp->arg;
104     *head = temp->next;
105     free(temp);
106   }
107   return data;
108 }
109
110 // Add new node at the beginning of the list.
111 static void llist_add(struct arg_list **old, void *data)
112 {
113   struct arg_list *new = xmalloc(sizeof(struct arg_list));
114
115   new->arg = (char*)data;
116   new->next = *old;
117   *old = new;
118 }
119
120 // Add new node at tail of list.
121 static void llist_add_tail(struct arg_list **head, void *data)
122 {
123   while (*head) head = &(*head)->next;
124   *head = xzalloc(sizeof(struct arg_list));
125   (*head)->arg = (char*)data;
126 }
127
128 // Reverse list order.
129 static struct arg_list *llist_rev(struct arg_list *list)
130 {
131   struct arg_list *rev = NULL;
132
133   while (list) {
134     struct arg_list *next = list->next;
135
136     list->next = rev;
137     rev = list;
138     list = next;
139   }
140   return rev;
141 }
142
143 /*
144  * Returns struct module_s from the data base if found, NULL otherwise.
145  * if add - create module entry, add it to data base and return the same mod.
146  */
147 static struct module_s *get_mod(char *mod, uint8_t add)
148 {
149   char name[MODNAME_LEN];
150   struct module_s *modentry;
151   struct arg_list *temp;
152   unsigned i, hash = 0;
153
154   path2mod(mod, name);
155   for (i = 0; name[i]; i++) hash = ((hash*31) + hash) + name[i];
156   hash %= DBASE_SIZE;
157   for (temp = TT.dbase[hash]; temp; temp = temp->next) {
158     modentry = (struct module_s *) temp->arg;
159     if (!strcmp(modentry->name, name)) return modentry;
160   }
161   if (!add) return NULL;
162   modentry = xzalloc(sizeof(*modentry));
163   modentry->name = xstrdup(name);
164   llist_add(&TT.dbase[hash], modentry);
165   return modentry;
166 }
167
168 /*
169  * Read a line from file with \ continuation and escape commented line.
170  * Return the line in allocated string (*li)
171  */
172 static int read_line(FILE *fl, char **li)
173 {
174   char *nxtline = NULL, *line;
175   int len, nxtlen, linelen, nxtlinelen;
176
177   while (1) {
178     line = NULL;
179     linelen = nxtlinelen = 0;
180     len = getline(&line, (size_t*)&linelen, fl);
181     if (len <= 0) {
182       free(line);
183       return len;
184     }
185     // checking for commented lines.
186     if (line[0] != '#') break;
187     free(line);
188   }
189   for (;;) {
190     if (line[len - 1] == '\n') len--;
191     if (!len) { 
192       free(line);
193       return len;
194     } else if (line[len - 1] != '\\') break;
195     
196     len--;
197     nxtlen = getline(&nxtline, (size_t*)&nxtlinelen, fl);
198     if (nxtlen <= 0) break;
199     if (linelen < len + nxtlen + 1) {
200       linelen = len + nxtlen + 1;
201       line = xrealloc(line, linelen);
202     }
203     memcpy(&line[len], nxtline, nxtlen);
204     len += nxtlen;
205   }
206   line[len] = '\0';
207   *li = xstrdup(line);
208   free(line);
209   if (nxtline) free(nxtline);
210   return len;
211 }
212
213 /*
214  * Action to be taken on all config files in default directories
215  * checks for aliases, options, install, remove and blacklist
216  */
217 static int config_action(struct dirtree *node)
218 {
219   FILE *fc;
220   char *filename, *tokens[3], *line, *linecp;
221   struct module_s *modent;
222   int tcount = 0;
223
224   if (!dirtree_notdotdot(node)) return 0;
225   if (S_ISDIR(node->st.st_mode)) return DIRTREE_RECURSE;
226
227   if (!S_ISREG(node->st.st_mode)) return 0; // process only regular file
228   filename = dirtree_path(node, NULL);
229   if (!(fc = fopen(filename, "r"))) {
230     free(filename);
231     return 0;
232   }
233   for (line = linecp = NULL; read_line(fc, &line) > 0; 
234       free(line), free(linecp), line = linecp = NULL) {
235     char *tk = NULL;
236
237     if (!strlen(line)) continue; 
238     linecp = xstrdup(line);
239     for (tk = strtok(linecp, "# \t"), tcount = 0; tk;
240         tk = strtok(NULL, "# \t"), tcount++) {
241       tokens[tcount] = tk;
242       if (tcount == 2) {
243         tokens[2] = line + strlen(tokens[0]) + strlen(tokens[1]) + 2;
244         break;
245       }
246     }
247     if (!tk) continue; 
248     // process the tokens[0] contains first word of config line.
249     if (!strcmp(tokens[0], "alias")) {
250       struct arg_list *temp;
251       char aliase[MODNAME_LEN], *realname;
252
253       if (!tokens[2]) continue;
254       path2mod(tokens[1], aliase);
255       for (temp = TT.probes; temp; temp = temp->next) {
256         modent = (struct module_s *) temp->arg;
257         if (fnmatch(aliase, modent->name, 0)) continue;
258         realname = path2mod(tokens[2], NULL);
259         llist_add(&modent->rnames, realname);
260         if (modent->flags & MOD_NDDEPS) {
261           modent->flags &= ~MOD_NDDEPS;
262           TT.nudeps--;
263         }
264         modent = get_mod(realname, 1);
265         if (!(modent->flags & MOD_NDDEPS)) {
266           modent->flags |= MOD_NDDEPS;
267           TT.nudeps++;
268         }
269       }
270     } else if (!strcmp(tokens[0], "options")) {
271       if (!tokens[2]) continue;
272       modent = get_mod(tokens[1], 1);
273       modent->opts = add_opts(modent->opts, tokens[2]);
274     } else if (!strcmp(tokens[0], "include"))
275       dirtree_read(tokens[1], config_action);
276     else if (!strcmp(tokens[0], "blacklist"))
277       get_mod(tokens[1], 1)->flags |= MOD_BLACKLIST;
278     else if (!strcmp(tokens[0], "install")) continue;
279     else if (!strcmp(tokens[0], "remove")) continue;
280     else error_msg("Invalid option %s found in file %s", tokens[0], filename);
281   }
282   fclose(fc);
283   free(filename);
284   return 0;
285 }
286
287 // Show matched modules else return -1 on failure.
288 static int depmode_read_entry(char *cmdname)
289 {
290   char *line;
291   int ret = -1;
292   FILE *fe = xfopen("modules.dep", "r");
293
294   while (read_line(fe, &line) > 0) {
295     char *tmp = strchr(line, ':');
296
297     if (tmp) {
298       *tmp = '\0';
299      char *name = basename(line);
300
301       tmp = strchr(name, '.');
302       if (tmp) *tmp = '\0';
303       if (!cmdname || !fnmatch(cmdname, name, 0)) {
304         if (tmp) *tmp = '.';
305         TT.dbg("%s\n", line);
306         ret = 0;
307       }
308     }
309     free(line);
310   }
311   fclose(fe);
312   return ret;
313 }
314
315 // Finds dependencies for modules from the modules.dep file.
316 static void find_dep(void)
317 {
318   char *line = NULL;
319   struct module_s *mod;
320   FILE *fe = xfopen("modules.dep", "r");
321
322   for (; read_line(fe, &line) > 0; free(line)) {
323     char *tmp = strchr(line, ':');
324
325     if (tmp) {
326       *tmp = '\0';
327       mod = get_mod(line, 0);
328       if (!mod) continue;
329       if ((mod->flags & MOD_ALOADED) &&
330           !(toys.optflags & (FLAG_r | FLAG_D))) continue;
331       
332       mod->flags |= MOD_FNDDEPMOD;
333       if ((mod->flags & MOD_NDDEPS) && (!mod->dep)) {
334         TT.nudeps--;
335         llist_add(&mod->dep, xstrdup(line));
336         tmp++;
337         if (*tmp) {
338           char *tok;
339
340           while ((tok = strsep(&tmp, " \t"))) {
341             if (!*tok) continue;
342             llist_add_tail(&mod->dep, xstrdup(tok));
343           }
344         }
345       }
346     }
347   }
348   fclose(fe);
349 }
350
351 // Remove a module from the Linux Kernel. if !modules does auto remove.
352 static int rm_mod(char *modules, uint32_t flags)
353 {
354   if (modules) {
355     int len = strlen(modules);
356
357     if (len > 3 && !strcmp(modules+len-3, ".ko" )) modules[len-3] = 0;
358   }
359
360   errno = 0;
361   syscall(__NR_delete_module, modules, flags ? flags : O_NONBLOCK|O_EXCL);
362
363   return errno;
364 }
365
366 // Insert module same as insmod implementation.
367 static int ins_mod(char *modules, char *flags)
368 {
369   char *buf = NULL;
370   int len, res;
371   int fd = xopen(modules, O_RDONLY);
372
373   len = fdlength(fd);
374   buf = xmalloc(len);
375   xreadall(fd, buf, len);
376   xclose(fd);
377
378   while (flags && strlen(toybuf) + strlen(flags) + 2 < sizeof(toybuf)) {
379     strcat(toybuf, flags);
380     strcat(toybuf, " ");
381   }
382   res = syscall(__NR_init_module, buf, len, toybuf);
383   if (CFG_TOYBOX_FREE && buf != toybuf) free(buf);
384   return res;
385 }
386
387 // Add module in probes list, if not loaded.
388 static void add_mod(char *name)
389 {
390   struct module_s *mod = get_mod(name, 1);
391
392   if (!(toys.optflags & (FLAG_r | FLAG_D)) && (mod->flags & MOD_ALOADED)) {
393     TT.dbg("skipping %s, it is already loaded\n", name);
394     return;
395   }
396   TT.dbg("queuing %s\n", name);
397   mod->cmdname = name;
398   mod->flags |= MOD_NDDEPS;
399   llist_add_tail(&TT.probes, mod);
400   TT.nudeps++;
401   if (!strncmp(mod->name, "symbol:", 7)) TT.symreq = 1;
402 }
403
404 // Parse cmdline options suplied for module.
405 static char *add_cmdopt(char **argv)
406 {
407   char *opt = xzalloc(1);
408   int lopt = 0;
409
410   while (*++argv) {
411     char *fmt, *var, *val;
412
413     var = *argv;
414     opt = xrealloc(opt, lopt + 2 + strlen(var) + 2);
415     // check for key=val or key = val.
416     fmt = "%.*s%s ";
417     for (val = var; *val && *val != '='; val++);
418     if (*val && strchr(++val, ' ')) fmt = "%.*s\"%s\" ";
419     lopt += sprintf(opt + lopt, fmt, (int) (val - var), var, val);
420   }
421   return opt;
422 }
423
424 // Probes a single module and loads all its dependencies.
425 static int go_probe(struct module_s *m)
426 {
427   int rc = 0, first = 1;
428
429   if (!(m->flags & MOD_FNDDEPMOD)) {
430     if (!(toys.optflags & FLAG_s))
431       error_msg("module %s not found in modules.dep", m->name);
432     return -ENOENT;
433   }
434   TT.dbg("go_prob'ing %s\n", m->name);
435   if (!(toys.optflags & FLAG_r)) m->dep = llist_rev(m->dep);
436   
437   while (m->dep) {
438     struct module_s *m2;
439     char *fn, *options;
440
441     rc = 0;
442     fn = llist_popme(&m->dep);
443     m2 = get_mod(fn, 1);
444     // are we removing ?
445     if (toys.optflags & FLAG_r) {
446       if (m2->flags & MOD_ALOADED) {
447         if ((rc = rm_mod(m2->name, O_EXCL))) {
448           if (first) {
449             perror_msg("can't unload module %s", m2->name);
450             break;
451           }
452         } else m2->flags &= ~MOD_ALOADED;
453       }
454       first = 0;
455       continue;
456     }
457     options = m2->opts;
458     m2->opts = NULL;
459     if (m == m2) options = add_opts(options, TT.cmdopts);
460
461     // are we only checking dependencies ?
462     if (toys.optflags & FLAG_D) {
463       TT.dbg(options ? "insmod %s %s\n" : "insmod %s\n", fn, options);
464       if (options) free(options);
465       continue;
466     }
467     if (m2->flags & MOD_ALOADED) {
468       TT.dbg("%s is already loaded, skipping\n", fn);
469       if (options) free(options);
470       continue;
471     }
472     // none of above is true insert the module.
473     rc = ins_mod(fn, options);
474     TT.dbg("loaded %s '%s', rc:%d\n", fn, options, rc);
475     if (rc == EEXIST) rc = 0;
476     if (options) free(options);
477     if (rc) {
478       perror_msg("can't load module %s (%s)", m2->name, fn);
479       break;
480     }
481     m2->flags |= MOD_ALOADED;
482   }
483   return rc;
484 }
485
486 void modprobe_main(void)
487 {
488   struct utsname uts;
489   char **argv = toys.optargs, *procline = NULL;
490   FILE *fs;
491   struct module_s *module;
492   unsigned flags = toys.optflags;
493
494   TT.dbg = (flags & FLAG_v) ? xprintf : dummy;
495
496   if ((toys.optc < 1) && (((flags & FLAG_r) && (flags & FLAG_l))
497         ||(!((flags & FLAG_r)||(flags & FLAG_l)))))
498   {
499           toys.exithelp++;
500           error_exit("bad syntax");
501   }
502   // Check for -r flag without arg if yes then do auto remove.
503   if ((flags & FLAG_r) && !toys.optc) {
504     if (rm_mod(NULL, O_NONBLOCK | O_EXCL)) perror_exit("rmmod");
505     return;
506   }
507
508   // change directory to /lib/modules/<release>/ 
509   xchdir("/lib/modules");
510   uname(&uts);
511   xchdir(uts.release);
512
513   // modules.dep processing for dependency check.
514   if (flags & FLAG_l) {
515     if (depmode_read_entry(toys.optargs[0])) error_exit("no module found.");
516     return;
517   }
518   // Read /proc/modules to get loaded modules.
519   fs = xfopen("/proc/modules", "r");
520   
521   while (read_line(fs, &procline) > 0) {
522     *(strchr(procline, ' ')) = '\0';
523     get_mod(procline, 1)->flags = MOD_ALOADED;
524     free(procline);
525     procline = NULL;
526   }
527   fclose(fs);
528   if ((flags & FLAG_a) || (flags & FLAG_r)) {
529     do {
530       add_mod(*argv++);
531     } while (*argv);
532   } else {
533     add_mod(argv[0]);
534     TT.cmdopts = add_cmdopt(argv);
535   }
536   if (!TT.probes) {
537     TT.dbg("All modules loaded\n");
538     return;
539   }
540   dirtree_read("/etc/modprobe.conf", config_action);
541   dirtree_read("/etc/modprobe.d", config_action);
542   if (TT.symreq) dirtree_read("modules.symbols", config_action);
543   if (TT.nudeps) dirtree_read("modules.alias", config_action);
544   find_dep();
545   while ((module = llist_popme(&TT.probes))) {
546     if (!module->rnames) {
547       TT.dbg("probing by module name\n");
548       /* This is not an alias. Literal names are blacklisted
549        * only if '-b' is given.
550        */
551       if (!(flags & FLAG_b) || !(module->flags & MOD_BLACKLIST))
552         go_probe(module);
553       continue;
554     }
555     do { // Probe all real names for the alias.
556       char *real = ((struct arg_list*)llist_pop(&module->rnames))->arg;
557       struct module_s *m2 = get_mod(real, 0);
558       
559       TT.dbg("probing alias %s by realname %s\n", module->name, real);
560       if (!m2) continue;
561       if (!(m2->flags & MOD_BLACKLIST) 
562           && (!(m2->flags & MOD_ALOADED) || (flags & (FLAG_r | FLAG_D))))
563         go_probe(m2);
564       free(real);
565     } while (module->rnames);
566   }
567 }