-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathdevices.c
More file actions
173 lines (152 loc) · 3.28 KB
/
devices.c
File metadata and controls
173 lines (152 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Routines for parsing /proc/devices for use by the ioctl fuzzer.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <linux/kdev_t.h>
#include "files.h"
#include "trinity.h"
static struct {
int major;
int minor;
const char *name;
} *block_devs, *char_devs, *misc_devs;
static size_t bldevs, chrdevs, miscdevs;
static bool parse_proc_devices(void)
{
FILE *fp;
char *p, *name, *line = NULL;
size_t n = 0;
int block, major;
void *new;
bool success = true;
fp = fopen("/proc/devices", "r");
if (!fp)
return false;
block = 0;
while (getline(&line, &n, fp) >= 0) {
if (strcmp("Block devices:\n", line) == 0)
block = 1;
else if (strcmp("Character devices:\n", line) == 0)
block = 0;
else if (sscanf(line, "%d %*s", &major) == 1) {
if ((p = strchr(line, '\n')) != NULL)
*p = 0;
if ((p = strrchr(line, ' ')) == NULL)
continue;
p++;
if (*p == '\0')
continue;
name = strdup(p);
if (!name)
continue;
if (block) {
new = realloc(block_devs, (bldevs+1)*sizeof(*block_devs));
if (!new) {
free(name);
success = false;
break;
}
block_devs = new;
block_devs[bldevs].major = major;
block_devs[bldevs].minor = 0;
block_devs[bldevs].name = name;
bldevs++;
} else {
new = realloc(char_devs, (chrdevs+1)*sizeof(*char_devs));
if (!new) {
free(name);
success = false;
break;
}
char_devs = new;
char_devs[chrdevs].major = major;
char_devs[chrdevs].minor = 0;
char_devs[chrdevs].name = name;
chrdevs++;
}
}
}
fclose(fp);
free(line);
return success;
}
static bool parse_proc_misc(void)
{
FILE *fp;
char *line = NULL;
size_t n = 0;
char *name;
int minor;
void *new;
bool success = true;
fp = fopen("/proc/misc", "r");
if (!fp)
return false;
while (getline(&line, &n, fp) >= 0) {
name = NULL;
if (sscanf(line, "%d %ms", &minor, &name) != 2) {
free(name);
continue;
}
new = realloc(misc_devs, (miscdevs+1)*sizeof(*misc_devs));
if (!new) {
free(name);
success = false;
break;
}
misc_devs = new;
misc_devs[miscdevs].major = 0;
misc_devs[miscdevs].minor = minor;
misc_devs[miscdevs].name = name;
miscdevs++;
}
fclose(fp);
free(line);
return success;
}
bool parse_devices(void)
{
bool success = true;
if (!parse_proc_devices()) {
output(0, "Warning: failed to fully parse /proc/devices\n");
success = false;
}
if (!parse_proc_misc()) {
output(0, "Warning: failed to fully parse /proc/misc\n");
success = false;
}
output(2, "Parsed %zu char devices, %zu block devices, %zu misc devices.\n",
chrdevs, bldevs, miscdevs);
return success;
}
const char *map_dev(dev_t st_rdev, mode_t st_mode)
{
int major, minor;
size_t i;
major = MAJOR(st_rdev);
minor = MINOR(st_rdev);
if (S_ISCHR(st_mode)) {
for (i = 0; i < chrdevs; ++i) {
if (char_devs[i].major == major) {
if (strcmp(char_devs[i].name, "misc") == 0) {
size_t j;
for (j = 0; j < miscdevs; ++j)
if (misc_devs[j].minor == minor)
return misc_devs[j].name;
} else
return char_devs[i].name;
}
}
} else if (S_ISBLK(st_mode)) {
for (i = 0; i < bldevs; ++i) {
if (block_devs[i].major == major)
return block_devs[i].name;
}
}
return NULL;
}