2 * Routines for parsing /proc/devices for use by the ioctl fuzzer.
9 #include <linux/kdev_t.h>
18 } *block_devs
, *char_devs
, *misc_devs
;
20 static size_t bldevs
, chrdevs
, miscdevs
;
22 static void parse_proc_devices(void)
25 char *p
, *name
, *line
= NULL
;
30 fp
= fopen("/proc/devices", "r");
36 while (getline(&line
, &n
, fp
) >= 0) {
37 if (strcmp("Block devices:\n", line
) == 0)
39 else if (strcmp("Character devices:\n", line
) == 0)
41 else if (sscanf(line
, "%d %*s", &major
) == 1) {
42 if ((p
= strchr(line
, '\n')) != NULL
)
44 if ((p
= strrchr(line
, ' ')) == NULL
)
50 new = realloc(block_devs
, (bldevs
+1)*sizeof(*block_devs
));
51 if (!new) { // FIXME: We should propagate failure up here.
56 block_devs
[bldevs
].major
= major
;
57 block_devs
[bldevs
].minor
= 0;
58 block_devs
[bldevs
].name
= name
;
61 new = realloc(char_devs
, (chrdevs
+1)*sizeof(*char_devs
));
67 char_devs
[chrdevs
].major
= major
;
68 char_devs
[chrdevs
].minor
= 0;
69 char_devs
[chrdevs
].name
= name
;
79 static void parse_proc_misc(void)
86 fp
= fopen("/proc/misc", "r");
90 while (fscanf(fp
, "%d %ms", &minor
, &name
) == 2) {
91 new = realloc(misc_devs
, (miscdevs
+1)*sizeof(*misc_devs
));
97 misc_devs
[miscdevs
].major
= 0;
98 misc_devs
[miscdevs
].minor
= minor
;
99 misc_devs
[miscdevs
].name
= name
;
106 void parse_devices(void)
108 parse_proc_devices();
111 output(2, "Parsed %zu char devices, %zu block devices, %zu misc devices.\n",
112 chrdevs
, bldevs
, miscdevs
);
115 const char *map_dev(dev_t st_rdev
, mode_t st_mode
)
120 major
= MAJOR(st_rdev
);
121 minor
= MINOR(st_rdev
);
123 if (S_ISCHR(st_mode
)) {
124 for (i
= 0; i
< chrdevs
; ++i
) {
125 if (char_devs
[i
].major
== major
) {
126 if (strcmp(char_devs
[i
].name
, "misc") == 0) {
129 for (j
= 0; j
< miscdevs
; ++j
)
130 if (misc_devs
[j
].minor
== minor
)
131 return misc_devs
[j
].name
;
133 return char_devs
[i
].name
;
136 } else if (S_ISBLK(st_mode
)) {
137 for (i
= 0; i
< bldevs
; ++i
) {
138 if (block_devs
[i
].major
== major
)
139 return block_devs
[i
].name
;