Mercurial > hg > toybox
annotate toys/other/lspci.c @ 1746:b11f536bac74 draft
install -D bugfix from David Halls.
(I tweaked some comment text while I was there.)
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 21 Mar 2015 15:49:38 -0500 |
parents | da1bf31ed322 |
children |
rev | line source |
---|---|
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
1 /* |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
2 * lspci - written by Isaac Dunham |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
3 |
1293
5f59ec6e38ff
lspci text output: add -i, allow -n when disabled
Isaac Dunham <ibid.ag@gmail.com>
parents:
1289
diff
changeset
|
4 USE_LSPCI(NEWTOY(lspci, "emkn"USE_LSPCI_TEXT("@i:"), TOYFLAG_USR|TOYFLAG_BIN)) |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
5 |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
6 config LSPCI |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
7 bool "lspci" |
1432 | 8 default y |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
9 help |
1289 | 10 usage: lspci [-ekm] |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
11 |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
12 List PCI devices. |
1289 | 13 |
1431 | 14 -e Print all 6 digits in class |
1289 | 15 -k Print kernel driver |
16 -m Machine parseable format | |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
17 |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
18 config LSPCI_TEXT |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
19 bool "lspci readable output" |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
20 depends on LSPCI |
1432 | 21 default y |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
22 help |
1314
bc618abf4fb2
Bugfix from Isaac Dunham (new pci database has # comments in it) and some minor cleanups I had in my tree already.
Rob Landley <rob@landley.net>
parents:
1293
diff
changeset
|
23 usage: lspci [-n] [-i FILE ] |
1289 | 24 |
25 -n Numeric output (repeat for readable and numeric) | |
1314
bc618abf4fb2
Bugfix from Isaac Dunham (new pci database has # comments in it) and some minor cleanups I had in my tree already.
Rob Landley <rob@landley.net>
parents:
1293
diff
changeset
|
26 -i PCI ID database (default /usr/share/misc/pci.ids) |
bc618abf4fb2
Bugfix from Isaac Dunham (new pci database has # comments in it) and some minor cleanups I had in my tree already.
Rob Landley <rob@landley.net>
parents:
1293
diff
changeset
|
27 |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
28 */ |
1289 | 29 |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
30 #define FOR_lspci |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
31 #include "toys.h" |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
32 |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
33 GLOBALS( |
1293
5f59ec6e38ff
lspci text output: add -i, allow -n when disabled
Isaac Dunham <ibid.ag@gmail.com>
parents:
1289
diff
changeset
|
34 char *ids; |
1289 | 35 long numeric; |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
36 |
1289 | 37 FILE *db; |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
38 ) |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
39 |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
40 int do_lspci(struct dirtree *new) |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
41 { |
1431 | 42 char *p = toybuf, *vendor = toybuf+9, *device = toybuf+18, |
43 driver[256], *vbig = 0, *dbig = 0, **fields; | |
44 int dirfd; | |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
45 |
1289 | 46 if (!new->parent) return DIRTREE_RECURSE; |
1431 | 47 |
48 // Parse data out of /proc | |
49 | |
50 if (-1 == (dirfd = openat(dirtree_parentfd(new), new->name, O_RDONLY))) | |
51 return 0; | |
52 | |
1571
e85e5f3b87c2
As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents:
1432
diff
changeset
|
53 // it's ok for the driver link not to be there, whatever fortify says |
1431 | 54 *driver = 0; |
55 if (toys.optflags & FLAG_k) | |
1572
da1bf31ed322
Tweak the "ignoring return value" fortify workaround for readlinkat.
Rob Landley <rob@landley.net>
parents:
1571
diff
changeset
|
56 if (readlinkat(dirfd, "driver", driver, sizeof(driver))) {}; |
1431 | 57 |
58 for (fields = (char*[]){"class", "vendor", "device", 0}; *fields; fields++) { | |
59 int fd, size = 6 + 2*((toys.optflags & FLAG_e) && p == toybuf); | |
60 *p = 0; | |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
61 |
1431 | 62 if (-1 == (fd = openat(dirfd, *fields, O_RDONLY))) { |
63 close(dirfd); | |
64 return 0; | |
65 } | |
66 xreadall(fd, p, size); | |
67 memmove(p, p+2, size -= 2); | |
68 p[size] = 0; | |
69 close(fd); | |
70 p += 9; | |
71 } | |
72 | |
73 close(dirfd); | |
74 | |
75 // Lookup/display data from pci.ids? | |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
76 |
1431 | 77 if (CFG_LSPCI_TEXT && TT.db) { |
78 if (TT.numeric != 1) { | |
79 char *s; | |
1289 | 80 |
1431 | 81 fseek(TT.db, 0, SEEK_SET); |
82 while (!vbig || !dbig) { | |
83 s = p; | |
84 if (!fgets(s, sizeof(toybuf)-(p-toybuf)-1, TT.db)) break; | |
85 while (isspace(*s)) s++; | |
86 if (*s == '#') continue; | |
87 if (vbig && s == p) break; | |
88 if (strstart(&s, vbig ? device : vendor)) { | |
89 if (vbig) dbig = s+2; | |
90 else vbig = s+2; | |
91 s += strlen(s); | |
92 s[-1] = 0; // trim ending newline | |
93 p = s + 1; | |
94 } | |
95 } | |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
96 } |
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
97 |
1431 | 98 if (TT.numeric > 1) { |
99 printf((toys.optflags & FLAG_m) | |
100 ? "%s, \"%s\" \"%s [%s]\" \"%s [%s]\"" | |
101 : "%s Class %s: %s [%s] %s [%s]", | |
102 new->name+5, toybuf, vbig ? vbig : "", vendor, | |
103 dbig ? dbig : "", device); | |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
104 |
1431 | 105 goto driver; |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
106 } |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
107 } |
1289 | 108 |
1431 | 109 printf((toys.optflags & FLAG_m) ? "%s \"%s\" \"%s\" \"%s\"" |
110 : "%s Class %s: %s:%s", new->name+5, toybuf, | |
111 vbig ? vbig : vendor, dbig ? dbig : device); | |
112 | |
113 driver: | |
114 if (*driver) | |
115 printf((toys.optflags & FLAG_m) ? " \"%s\"" : " %s", basename(driver)); | |
116 xputc('\n'); | |
117 | |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
118 return 0; |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
119 } |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
120 |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
121 void lspci_main(void) |
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
122 { |
1314
bc618abf4fb2
Bugfix from Isaac Dunham (new pci database has # comments in it) and some minor cleanups I had in my tree already.
Rob Landley <rob@landley.net>
parents:
1293
diff
changeset
|
123 if (CFG_LSPCI_TEXT && TT.numeric != 1) { |
1431 | 124 if (!TT.ids) TT.ids = "/usr/share/misc/pci.ids"; |
125 if (!(TT.db = fopen(TT.ids, "r"))) | |
126 perror_msg("could not open PCI ID db"); | |
990
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
127 } |
8caeba551a28
This is the preliminary version of lspci text output support (the location
Isaac Dunham <idunham@lavabit.com>
parents:
976
diff
changeset
|
128 |
969
a47c6658210c
lspci: use toybuf instead of dynamic memory allocation
Felix Janda <felix.janda@posteo.de>
parents:
968
diff
changeset
|
129 dirtree_read("/sys/bus/pci/devices", do_lspci); |
968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset
|
130 } |