-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devtag-allinone.c
446 lines (388 loc) · 8.61 KB
/
devtag-allinone.c
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#define DEVTAG_ALLINONE
/*
* File: usb.c
* Implements: scanning of usb devices
*
* Copyright: Jens Låås, 2011
* Copyright license: According to GPL, see file COPYING in this directory.
*
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <libgen.h>
#include <stdlib.h>
#include <ctype.h>
#include <fnmatch.h>
#ifdef DEVTAG_ALLINONE
#include "devtag-allinone.h"
#else
#include "libdevtag.h"
#endif
static int usb_scan_dir(struct dev_head *result, const struct devinfo_head *sel, const char *dir);
static char *getstring(const char *dir, const char *file)
{
char fn[512];
char buf[256];
int fd, n;
sprintf(fn, "%s/%s", dir, file);
fd = open(fn, O_RDONLY);
if(fd == -1) return NULL;
n = read(fd, buf, sizeof(buf)-1);
if(n >= 0) {
buf[n] = 0;
if(n > 0) {
if(buf[n-1] == '\n')
buf[n-1] = 0;
}
} else buf[0] = 0;
buf[sizeof(buf)-1] = 0;
close(fd);
return strdup(buf);
}
/*
* scan usb devices.
* add matching devices to result list
*/
int devtag_usb_scan(struct dev_head *result, const struct devinfo_head *sel)
{
return usb_scan_dir(result, sel, "/sys/bus/usb/devices");
}
static char *dev_probe(const char *name)
{
char fn[512];
struct stat statb;
sprintf(fn, "/dev/%s", name);
if(stat(fn, &statb)==0) {
if(S_ISBLK(statb.st_mode))
return "b";
if(S_ISCHR(statb.st_mode))
return "c";
}
return "f";
}
static struct devname *devname_new(const char *dir)
{
struct devname *dn;
char *name;
name = basename(strdup(dir));
if(isdigit(name[0]) )
return NULL;
dn = malloc(sizeof(struct devname));
dn->dev = getstring(dir, "dev");
dn->devname = name;
dn->type = dev_probe(name);
dn->pos = strlen(dir);
dn->next = NULL;
// printf("devname_new %s %s %s\n", dn->dev, dn->devname, dir);
return dn;
}
/*
* depth-first scan for all dev files. name of parentdir is assumed to be node name.
*/
static int usb_scan_devname(const char *dir, struct devname_head *devnames)
{
DIR *d;
struct dirent *ent;
char fn[512];
int len;
// printf("scan_devname opening dir %s\n", dir);
d = opendir(dir);
if(!d) return 0;
while((ent = readdir(d))) {
if(ent->d_type == DT_LNK) continue;
if(ent->d_type == DT_UNKNOWN) {
/* FIXME: use lstat to determine if file is a link */
printf("DT_UNKNOWN\n");
exit(1);
}
if(ent->d_name[0] != '.') {
sprintf(fn, "%s/%s", dir, ent->d_name);
usb_scan_devname(fn, devnames);
}
if(!strcmp(ent->d_name, "dev")) {
struct devname *devname;
devname = devname_new(dir);
if(devname) {
devname->next = devnames->head;
devnames->head = devname;
}
}
}
closedir(d);
{
struct devname *dn;
len=0;
for(dn=devnames->head;dn;dn=dn->next)
len++;
}
return len;
}
static int dev_info_add(struct dev *dev, const char *dir, const char *name)
{
struct devinfo *info;
char *value;
value = getstring(dir, name);
if(value) {
info = malloc(sizeof(struct devinfo));
info->name = name;
info->value = value;
info->next = dev->info.head;
dev->info.head = info;
return 0;
}
return -1;
}
static struct dev *dev_new()
{
struct dev *d;
d = malloc(sizeof(struct dev));
d->class = "usb";
d->next = NULL;
return d;
}
static int usb_dev_match(const struct devinfo_head *info, const struct devinfo_head *selectors)
{
int match=1;
struct devinfo *sel, *i;
for(sel=selectors->head;sel;sel=sel->next) {
match=0;
for(i=info->head;i;i=i->next) {
if(strcmp(i->name, sel->name)==0) {
if(fnmatch(sel->value, i->value, 0)==0) {
match=1;
break;
}
}
}
if(match==0)
return 0;
}
return match;
}
static int usb_scan_dev(struct dev_head *result, const struct devinfo_head *sel, const char *dir)
{
char *usbdev;
struct devname_head devnames;
struct dev *dev;
usbdev = getstring(dir, "dev");
if(usbdev) {
devnames.head = NULL;
if(usb_scan_devname(dir, &devnames)) {
dev = dev_new();
dev->devnames.head = devnames.head;
dev->info.head = NULL;
dev_info_add(dev, dir, "serial");
dev_info_add(dev, dir, "manufacturer");
dev_info_add(dev, dir, "product");
dev_info_add(dev, dir, "idProduct");
dev_info_add(dev, dir, "idVendor");
if(usb_dev_match(&dev->info, sel)) {
dev->next = result->head;
result->head = dev;
}
}
}
return 0;
}
static int usb_scan_dir(struct dev_head *result, const struct devinfo_head *sel, const char *dir)
{
DIR *d;
struct dirent *ent;
char fn[512];
/*
/sys/bus/usb/devices/usb1/1-4/serial
/sys/bus/usb/devices/1-4:1.0/serial
*/
// printf("opening dir %s\n", dir);
d = opendir(dir);
if(!d) return 0;
while((ent = readdir(d))) {
if(ent->d_name[0] != '.') {
sprintf(fn, "%s/%s", dir, ent->d_name);
usb_scan_dev(result, sel, fn);
}
}
closedir(d);
return 0;
}
/*
* File: dev.c
* Implements: main entry point for devicename scanning
*
* Copyright: Jens Låås, 2011
* Copyright license: According to GPL, see file COPYING in this directory.
*
*/
#ifdef DEVTAG_ALLINONE
#include "devtag-allinone.h"
#else
#include "libdevtag.h"
#endif
int devtag_dev_scan(struct dev_head *result, const struct devinfo_head *sel)
{
return devtag_usb_scan(result,sel);
}
/*
* File: lookup.c
* Implements: device name lookup functions
*
* Copyright: Jens Låås, 2011
* Copyright license: According to GPL, see file COPYING in this directory.
*
*/
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <fnmatch.h>
#include <stdlib.h>
#ifdef DEVTAG_ALLINONE
#include "devtag-allinone.h"
#else
#include "devtag.h"
#include "libdevtag.h"
#endif
static int parse(const char *devname, char **class, char **devpattern, char **constdev, struct devinfo_head *sel)
{
char *home, *pp, *p, *name, *value, fn[256], buf[512];
int fd;
ssize_t n;
struct devinfo *di;
*class = NULL;
*devpattern = NULL;
*constdev = NULL;
sprintf(fn, "/etc/devtag.d/%s.conf", devname);
fd = open(fn, O_RDONLY);
if(fd == -1) {
home = getenv("HOME");
if(home) {
sprintf(fn, "%s/.devtag.d/%s.conf", home, devname);
fd = open(fn, O_RDONLY);
}
}
if(fd == -1) return -1;
n = read(fd, buf, sizeof(buf)-1);
if(n <= 0)
return -2;
buf[n] = 0;
p = buf;
while(p && *p) {
name = p;
p = strchr(p, '\n');
if(p) {
*p = 0;
p++;
}
value = strchr(name, '=');
if(value) {
*value = 0;
value++;
if(*value == '"') {
value++;
pp = strchr(value, '"');
if(pp) *pp = 0;
}
if(!strcmp(name, "class")) {
*class = strdup(value);
continue;
}
if(!strcmp(name, "dev")) {
*devpattern = strdup(value);
continue;
}
if(!strcmp(name, "devname")) {
*constdev = strdup(value);
continue;
}
di = malloc(sizeof(struct devinfo));
if(di) {
di->name = strdup(name);
di->value = strdup(value);
di->next = sel->head;
sel->head = di;
}
}
}
close(fd);
return 0;
}
static char *dev_match(struct dev *dev, char *devpattern)
{
struct devname *devname;
for(devname=dev->devnames.head;devname;devname=devname->next) {
if(fnmatch(devpattern, devname->devname, 0)==0) {
return devname->devname;
}
}
return NULL;
}
int devtag_lookup2(char *buf, size_t bufsize, char *constbuf, size_t constsize, const char *devname)
{
struct devinfo_head sel;
struct dev_head result;
struct dev *dev;
char *class, *devpattern, *constdev, *pfx="";
struct devname *dn;
int len = 0;
sel.head = NULL;
result.head = NULL;
snprintf(buf, bufsize, "%s", devname);
if(strncmp(devname, "/dev/", 5)==0) {
pfx = "/dev/";
devname += 5;
}
/* parse config file in /etc/devtag.d/ */
if(parse(devname, &class, &devpattern, &constdev, &sel))
return -1;
if(constbuf) {
constbuf[0] = 0;
if(constdev)
strncpy(constbuf, constdev, constsize-1);
constbuf[constsize-1] = 0;
}
if(!class) class = "usb";
/* look for usb devices */
if(strcmp(class, "usb")==0)
devtag_usb_scan(&result, &sel);
for(dev=result.head;dev;dev=dev->next) {
len++;
}
if(!devpattern) {
dev = result.head;
if(dev) {
dn = dev->devnames.head;
if(dn) {
snprintf(buf, bufsize, "%s%s", pfx, dn->devname);
return len;
}
}
}
/* check for matching device node with devpattern among the devices that matched
selectors */
for(dev=result.head;dev;dev=dev->next) {
/* find any matching devicenode */
if((devname = dev_match(dev, devpattern))) {
snprintf(buf, bufsize, "%s%s", pfx, devname);
return len;
}
}
return 0;
}
int devtag_lookup(char *buf, size_t bufsize, const char *devname)
{
return devtag_lookup2(buf, bufsize, NULL, 0, devname);
}
char *devtag_get(const char *devname)
{
char buf[64];
buf[0] = 0;
devtag_lookup(buf, sizeof(buf), devname);
return strdup(buf);
}