comparison toys/other/modinfo.c @ 877:37e668afd008

Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
author Rob Landley <rob@landley.net>
date Wed, 24 Apr 2013 03:04:31 -0500
parents 786841fdb1e0
children ac9991f66d0d
comparison
equal deleted inserted replaced
876:4dcd5decb4fd 877:37e668afd008
16 16
17 GLOBALS( 17 GLOBALS(
18 char *field; 18 char *field;
19 ) 19 )
20 20
21 static const char *modinfo_tags[] = { 21 static char *modinfo_tags[] = {
22 "alias", "license", "description", "author", "vermagic", 22 "alias", "license", "description", "author", "vermagic",
23 "srcversion", "intree", "parm", "depends", 23 "srcversion", "intree", "parm", "depends",
24 }; 24 };
25 25
26 static void output_field(const char *field, const char *value) 26 static void output_field(char *field, char *value)
27 { 27 {
28 int len; 28 int len;
29 29
30 if (TT.field && strcmp(TT.field, field) != 0) return; 30 if (TT.field && strcmp(TT.field, field)) return;
31 31
32 len = strlen(field); 32 len = strlen(field);
33 33
34 if (TT.field) xprintf("%s", value); 34 if (TT.field) xprintf("%s", value);
35 else xprintf("%s:%*s%s", field, 15 - len, "", value); 35 else xprintf("%s:%*s%s", field, 15 - len, "", value);
36 if (toys.optflags & FLAG_0) xwrite(fileno(stdout), "\0", 1); 36 xputc((toys.optflags & FLAG_0) ? 0 : '\n');
37 else xputs("");
38 } 37 }
39
40 38
41 static void modinfo_file(struct dirtree *dir) 39 static void modinfo_file(struct dirtree *dir)
42 { 40 {
43 int fd, len, i; 41 int fd, len, i;
44 char *buf, *pos; 42 char *buf, *pos, *full_name;
45 char *full_name;
46 43
47 full_name = dirtree_path(dir, NULL); 44 full_name = dirtree_path(dir, NULL);
48
49 output_field("filename", full_name); 45 output_field("filename", full_name);
50 fd = xopen(full_name, O_RDONLY); 46 fd = xopen(full_name, O_RDONLY);
47 free(full_name);
48
51 len = fdlength(fd); 49 len = fdlength(fd);
52 buf = xmalloc(len); 50 if (!(buf = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0)))
53 xreadall(fd, buf, len); 51 perror_exit("mmap %s", full_name);
54 52
55 for (pos = buf; pos < buf + len + 10; pos++) { 53 for (pos = buf; pos < buf+len; pos++) {
56 if (*pos) continue; 54 if (*pos) continue;
57 55
58 for (i = 0; i < sizeof(modinfo_tags) / sizeof(modinfo_tags[0]); i++) { 56 for (i = 0; i < sizeof(modinfo_tags) / sizeof(*modinfo_tags); i++) {
59 const char *str = modinfo_tags[i]; 57 char *str = modinfo_tags[i];
60 int len = strlen(str); 58 int len = strlen(str);
61 if (strncmp(pos + 1, str, len) == 0 && pos[len + 1] == '=') 59
62 output_field(str, &pos[len + 2]); 60 if (!strncmp(pos+1, str, len) && pos[len+1] == '=')
61 output_field(str, pos+len+2);
63 } 62 }
64 } 63 }
65 64
66 free(full_name); 65 munmap(buf, len);
67 free(buf);
68 close(fd); 66 close(fd);
69 } 67 }
70 68
71 static int check_module(struct dirtree *new) 69 static int check_module(struct dirtree *new)
72 { 70 {
73 if (S_ISREG(new->st.st_mode)) { 71 if (S_ISREG(new->st.st_mode)) {
74 char **s; 72 char **ss;
75 for (s = toys.optargs; *s; s++) { 73
76 int len = strlen(*s); 74 for (ss = toys.optargs; *ss; ss++) {
77 if (!strncmp(*s, new->name, len) && !strcmp(new->name+len, ".ko")) 75 char *s = *ss;
78 modinfo_file(new); 76 int len = 0;
77
78 // The kernel treats - and _ the same, so we should too.
79 for (len = 0; s[len]; len++) {
80 if (s[len] == '-' && new->name[len] == '_') continue;
81 if (s[len] == '_' && new->name[len] == '-') continue;
82 if (s[len] != new->name[len]) break;
83 }
84 if (s[len] || strcmp(new->name+len, ".ko")) break;
85
86 modinfo_file(new);
79 } 87 }
80 } 88 }
81 89
82 return dirtree_notdotdot(new); 90 return dirtree_notdotdot(new);
83 } 91 }
86 { 94 {
87 struct utsname uts; 95 struct utsname uts;
88 96
89 if (uname(&uts) < 0) perror_exit("bad uname"); 97 if (uname(&uts) < 0) perror_exit("bad uname");
90 sprintf(toybuf, "/lib/modules/%s", uts.release); 98 sprintf(toybuf, "/lib/modules/%s", uts.release);
99
91 dirtree_read(toybuf, check_module); 100 dirtree_read(toybuf, check_module);
92 } 101 }