• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* modinfo.c - Display module info
2  *
3  * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
4  *
5  * TODO: cleanup
6 
7 USE_MODINFO(NEWTOY(modinfo, "<1b:k:F:0", TOYFLAG_SBIN))
8 
9 config MODINFO
10   bool "modinfo"
11   default y
12   help
13     usage: modinfo [-0] [-b basedir] [-k kernrelease] [-F field] [modulename...]
14 
15     Display module fields for all specified modules, looking in
16     <basedir>/lib/modules/<kernrelease>/ (kernrelease defaults to uname -r).
17 */
18 
19 #define FOR_modinfo
20 #include "toys.h"
21 
22 GLOBALS(
23   char *F, *k, *b;
24 
25   long mod;
26 )
27 
output_field(char * field,char * value)28 static void output_field(char *field, char *value)
29 {
30   if (!TT.F) xprintf("%s:%*c", field, 15-(int)strlen(field), ' ');
31   else if (strcmp(TT.F, field)) return;
32   xprintf("%s", value);
33   xputc((toys.optflags & FLAG_0) ? 0 : '\n');
34 }
35 
modinfo_file(char * full_name)36 static void modinfo_file(char *full_name)
37 {
38   int fd, len, i;
39   char *buf = 0, *pos, *modinfo_tags[] = {
40     "alias", "license", "description", "author", "firmware",
41     "vermagic", "srcversion", "intree", "depends", "parm",
42     "parmtype",
43   };
44 
45   if (-1 != (fd = open(full_name, O_RDONLY))) {
46     len = fdlength(fd);
47     buf = xmmap(0, len, PROT_READ, MAP_SHARED, fd, 0);
48     close(fd);
49   }
50 
51   if (!buf) {
52     perror_msg_raw(full_name);
53     return;
54   }
55 
56   output_field("filename", full_name);
57 
58   for (pos = buf; pos < buf+len; pos++) {
59     if (*pos) continue;
60 
61     for (i=0; i<ARRAY_LEN(modinfo_tags); i++) {
62       char *str = modinfo_tags[i];
63       int len = strlen(str);
64 
65       if (!strncmp(pos+1, str, len) && pos[len+1] == '=')
66         output_field(str, pos+len+2);
67     }
68   }
69 
70   munmap(buf, len);
71 }
72 
check_module(struct dirtree * new)73 static int check_module(struct dirtree *new)
74 {
75   if (!dirtree_notdotdot(new)) return 0;
76 
77   if (S_ISREG(new->st.st_mode)) {
78     char *s;
79 
80     for (s = toys.optargs[TT.mod]; *s; s++) {
81       int len = 0;
82 
83       // The kernel treats - and _ the same, so we should too.
84       for (len = 0; s[len]; len++) {
85         if (s[len] == '-' && new->name[len] == '_') continue;
86         if (s[len] == '_' && new->name[len] == '-') continue;
87         if (s[len] != new->name[len]) break;
88       }
89       if (s[len] || strcmp(new->name+len, ".ko")) break;
90 
91       modinfo_file(s = dirtree_path(new, 0));
92       free(s);
93 
94       return DIRTREE_ABORT;
95     }
96   }
97 
98   return DIRTREE_RECURSE;
99 }
100 
modinfo_main(void)101 void modinfo_main(void)
102 {
103   for(TT.mod = 0; TT.mod<toys.optc; TT.mod++) {
104     char *s = strstr(toys.optargs[TT.mod], ".ko");
105 
106     if (s && !s[3]) modinfo_file(toys.optargs[TT.mod]);
107     else {
108       struct utsname uts;
109 
110       if (uname(&uts) < 0) perror_exit("bad uname");
111       if (snprintf(toybuf, sizeof(toybuf), "%s/lib/modules/%s",
112           (toys.optflags & FLAG_b) ? TT.b : "",
113           (toys.optflags & FLAG_k) ? TT.k : uts.release) >= sizeof(toybuf))
114             perror_exit("basedir/kernrelease too long");
115       dirtree_read(toybuf, check_module);
116     }
117   }
118 }
119