Annotation of src/usr.bin/usbhidaction/usbhidaction.c, Revision 1.7
1.7 ! fgsch 1: /* $OpenBSD: usbhidaction.c,v 1.6 2004/10/27 18:16:57 jaredy Exp $ */
1.1 nate 2: /* $NetBSD: usbhidaction.c,v 1.7 2002/01/18 14:38:59 augustss Exp $ */
3:
4: /*
5: * Copyright (c) 2000, 2002 The NetBSD Foundation, Inc.
6: * All rights reserved.
7: *
8: * This code is derived from software contributed to The NetBSD Foundation
9: * by Lennart Augustsson <lennart@augustsson.net>.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
19: * 3. All advertising materials mentioning features or use of this software
20: * must display the following acknowledgement:
21: * This product includes software developed by the NetBSD
22: * Foundation, Inc. and its contributors.
23: * 4. Neither the name of The NetBSD Foundation nor the names of its
24: * contributors may be used to endorse or promote products derived
25: * from this software without specific prior written permission.
26: *
27: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37: * POSSIBILITY OF SUCH DAMAGE.
38: */
39:
40: #include <stdio.h>
41: #include <stdlib.h>
42: #include <string.h>
43: #include <ctype.h>
44: #include <err.h>
45: #include <fcntl.h>
46: #include <limits.h>
47: #include <unistd.h>
48: #include <sys/types.h>
49: #include <sys/ioctl.h>
50: #include <dev/usb/usb.h>
51: #include <dev/usb/usbhid.h>
52: #include <usbhid.h>
53: #include <util.h>
54: #include <syslog.h>
55: #include <signal.h>
56:
57: int verbose = 0;
58: int isdemon = 0;
59: int reparse = 1;
60:
61: struct command {
62: struct command *next;
63: int line;
64:
65: struct hid_item item;
66: int value;
67: char anyvalue;
68: char *name;
69: char *action;
70: };
71: struct command *commands;
72:
73: #define SIZE 4000
74:
75: void usage(void);
76: struct command *parse_conf(const char *, report_desc_t, int, int);
77: void docmd(struct command *, int, const char *, int, char **);
78: void freecommands(struct command *);
79:
1.5 deraadt 80: /* ARGSUSED */
1.1 nate 81: static void
1.5 deraadt 82: sighup(int signo)
1.1 nate 83: {
84: reparse = 1;
85: }
86:
87: int
88: main(int argc, char **argv)
89: {
90: const char *conf = NULL;
91: const char *dev = NULL;
92: int fd, ch, sz, n, val, i;
93: int demon, ignore;
94: report_desc_t repd;
95: char buf[100];
96: char devnamebuf[PATH_MAX];
97: struct command *cmd;
98: int reportid;
99:
100: demon = 1;
101: ignore = 0;
102: while ((ch = getopt(argc, argv, "c:df:iv")) != -1) {
103: switch(ch) {
104: case 'c':
105: conf = optarg;
106: break;
107: case 'd':
108: demon ^= 1;
109: break;
110: case 'i':
111: ignore++;
112: break;
113: case 'f':
114: dev = optarg;
115: break;
116: case 'v':
117: demon = 0;
118: verbose++;
119: break;
120: case '?':
121: default:
122: usage();
123: }
124: }
125: argc -= optind;
126: argv += optind;
127:
128: if (conf == NULL || dev == NULL)
129: usage();
130:
1.4 deraadt 131: if (hid_start(NULL) == -1)
132: errx(1, "hid_init");
1.1 nate 133:
134: if (dev[0] != '/') {
135: snprintf(devnamebuf, sizeof(devnamebuf), "/dev/%s%s",
1.4 deraadt 136: isdigit(dev[0]) ? "uhid" : "", dev);
1.1 nate 137: dev = devnamebuf;
138: }
139:
140: fd = open(dev, O_RDWR);
141: if (fd < 0)
142: err(1, "%s", dev);
1.7 ! fgsch 143:
! 144: /* Avoid passing the device file descriptor to executed commands */
! 145: if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
! 146: err(1, "fcntl(F_SETFD, FD_CLOEXEC)");
! 147:
1.1 nate 148: if (ioctl(fd, USB_GET_REPORT_ID, &reportid) < 0)
149: reportid = -1;
150: repd = hid_get_report_desc(fd);
151: if (repd == NULL)
1.2 jsyn 152: err(1, "hid_get_report_desc() failed");
1.1 nate 153:
154: commands = parse_conf(conf, repd, reportid, ignore);
155:
156: sz = hid_report_size(repd, hid_input, reportid);
157:
158: if (verbose)
159: printf("report size %d\n", sz);
160: if (sz > sizeof buf)
161: errx(1, "report too large");
162:
163: (void)signal(SIGHUP, sighup);
164:
165: if (demon) {
166: if (daemon(0, 0) < 0)
167: err(1, "daemon()");
168: pidfile(NULL);
169: isdemon = 1;
170: }
171:
172: for(;;) {
173: n = read(fd, buf, sz);
174: if (verbose > 2) {
175: printf("read %d bytes:", n);
176: for (i = 0; i < n; i++)
177: printf(" %02x", buf[i]);
178: printf("\n");
179: }
180: if (n < 0) {
181: if (verbose)
182: err(1, "read");
183: else
184: exit(1);
185: }
186: #if 0
187: if (n != sz) {
188: err(2, "read size");
189: }
190: #endif
191: for (cmd = commands; cmd; cmd = cmd->next) {
192: val = hid_get_data(buf, &cmd->item);
193: if (cmd->value == val || cmd->anyvalue)
194: docmd(cmd, val, dev, argc, argv);
195: }
196: if (reparse) {
197: struct command *cmds =
198: parse_conf(conf, repd, reportid, ignore);
199: if (cmds) {
200: freecommands(commands);
201: commands = cmds;
202: }
203: reparse = 0;
204: }
205: }
206:
207: exit(0);
208: }
209:
210: void
211: usage(void)
212: {
213: extern char *__progname;
214:
1.3 jmc 215: fprintf(stderr, "Usage: %s [-div] -c config-file -f device "
216: "arg ...\n", __progname);
1.1 nate 217: exit(1);
218: }
219:
220: static int
221: peek(FILE *f)
222: {
223: int c;
224:
225: c = getc(f);
226: if (c != EOF)
227: ungetc(c, f);
228: return c;
229: }
230:
231: struct command *
232: parse_conf(const char *conf, report_desc_t repd, int reportid, int ignore)
233: {
234: FILE *f;
235: char *p;
236: int line;
237: char buf[SIZE], name[SIZE], value[SIZE], action[SIZE];
238: char usage[SIZE], coll[SIZE];
239: struct command *cmd, *cmds;
240: struct hid_data *d;
241: struct hid_item h;
242: int u, lo, hi, range;
243:
244: f = fopen(conf, "r");
245: if (f == NULL)
246: err(1, "%s", conf);
247:
248: cmds = NULL;
249: for (line = 1; ; line++) {
250: if (fgets(buf, sizeof buf, f) == NULL)
251: break;
252: if (buf[0] == '#' || buf[0] == '\n')
253: continue;
254: p = strchr(buf, '\n');
255: while (p && isspace(peek(f))) {
256: if (fgets(p, sizeof buf - strlen(buf), f) == NULL)
257: break;
258: p = strchr(buf, '\n');
259: }
260: if (p)
261: *p = 0;
262: if (sscanf(buf, "%s %s %[^\n]", name, value, action) != 3) {
263: if (isdemon) {
264: syslog(LOG_WARNING, "config file `%s', line %d"
1.4 deraadt 265: ", syntax error: %s", conf, line, buf);
1.1 nate 266: freecommands(cmds);
267: return (NULL);
268: } else {
1.6 jaredy 269: errx(1, "config file `%s', line %d"
1.4 deraadt 270: ", syntax error: %s", conf, line, buf);
1.1 nate 271: }
272: }
273:
274: cmd = malloc(sizeof *cmd);
275: if (cmd == NULL)
276: err(1, "malloc failed");
277: cmd->next = cmds;
278: cmds = cmd;
279: cmd->line = line;
280:
281: if (strcmp(value, "*") == 0) {
282: cmd->anyvalue = 1;
283: } else {
284: cmd->anyvalue = 0;
285: if (sscanf(value, "%d", &cmd->value) != 1) {
286: if (isdemon) {
287: syslog(LOG_WARNING,
1.4 deraadt 288: "config file `%s', line %d, "
1.6 jaredy 289: "bad value: %s",
1.4 deraadt 290: conf, line, value);
1.1 nate 291: freecommands(cmds);
292: return (NULL);
293: } else {
294: errx(1, "config file `%s', line %d, "
1.6 jaredy 295: "bad value: %s",
1.4 deraadt 296: conf, line, value);
1.1 nate 297: }
298: }
299: }
300:
301: coll[0] = 0;
302: for (d = hid_start_parse(repd, 1 << hid_input, reportid);
1.4 deraadt 303: hid_get_item(d, &h); ) {
1.1 nate 304: if (verbose > 2)
305: printf("kind=%d usage=%x\n", h.kind, h.usage);
306: if (h.flags & HIO_CONST)
307: continue;
308: switch (h.kind) {
309: case hid_input:
310: if (h.usage_minimum != 0 ||
311: h.usage_maximum != 0) {
312: lo = h.usage_minimum;
313: hi = h.usage_maximum;
314: range = 1;
315: } else {
316: lo = h.usage;
317: hi = h.usage;
318: range = 0;
319: }
320: for (u = lo; u <= hi; u++) {
321: snprintf(usage, sizeof usage, "%s:%s",
1.4 deraadt 322: hid_usage_page(HID_PAGE(u)),
1.1 nate 323: hid_usage_in_page(u));
324: if (verbose > 2)
325: printf("usage %s\n", usage);
326: if (!strcasecmp(usage, name))
327: goto foundhid;
328: if (coll[0]) {
329: snprintf(usage, sizeof usage,
330: "%s.%s:%s", coll+1,
1.4 deraadt 331: hid_usage_page(HID_PAGE(u)),
1.1 nate 332: hid_usage_in_page(u));
333: if (verbose > 2)
334: printf("usage %s\n",
1.4 deraadt 335: usage);
1.1 nate 336: if (!strcasecmp(usage, name))
337: goto foundhid;
338: }
339: }
340: break;
341: case hid_collection:
342: snprintf(coll + strlen(coll),
343: sizeof coll - strlen(coll), ".%s:%s",
1.4 deraadt 344: hid_usage_page(HID_PAGE(h.usage)),
1.1 nate 345: hid_usage_in_page(h.usage));
346: break;
347: case hid_endcollection:
348: if (coll[0])
349: *strrchr(coll, '.') = 0;
350: break;
351: default:
352: break;
353: }
354: }
355: if (ignore) {
356: if (verbose)
1.2 jsyn 357: warnx("ignore item '%s'", name);
1.1 nate 358: continue;
359: }
360: if (isdemon) {
361: syslog(LOG_WARNING, "config file `%s', line %d, HID "
1.6 jaredy 362: "item not found: `%s'", conf, line, name);
1.1 nate 363: freecommands(cmds);
364: return (NULL);
365: } else {
366: errx(1, "config file `%s', line %d, HID item "
1.6 jaredy 367: "not found: `%s'", conf, line, name);
1.1 nate 368: }
369:
370: foundhid:
371: hid_end_parse(d);
372: cmd->item = h;
373: cmd->name = strdup(name);
374: cmd->action = strdup(action);
375: if (range) {
376: if (cmd->value == 1)
377: cmd->value = u - lo;
378: else
379: cmd->value = -1;
380: }
381:
382: if (verbose)
383: printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
1.4 deraadt 384: cmd->value, cmd->action);
1.1 nate 385: }
386: fclose(f);
387: return (cmds);
388: }
389:
390: void
391: docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
392: {
393: char cmdbuf[SIZE], *p, *q;
394: size_t len;
395: int n, r;
396:
397: for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
398: if (*p == '$') {
399: p++;
400: len = &cmdbuf[SIZE-1] - q;
401: if (isdigit(*p)) {
402: n = strtol(p, &p, 10) - 1;
403: if (n >= 0 && n < argc) {
404: strncpy(q, argv[n], len);
405: q += strlen(q);
406: }
407: } else if (*p == 'V') {
408: p++;
409: snprintf(q, len, "%d", value);
410: q += strlen(q);
411: } else if (*p == 'N') {
412: p++;
413: strncpy(q, cmd->name, len);
414: q += strlen(q);
415: } else if (*p == 'H') {
416: p++;
417: strncpy(q, hid, len);
418: q += strlen(q);
419: } else if (*p) {
420: *q++ = *p++;
421: }
422: } else {
423: *q++ = *p++;
424: }
425: }
426: *q = 0;
427:
428: if (verbose)
429: printf("system '%s'\n", cmdbuf);
430: r = system(cmdbuf);
431: if (verbose > 1 && r)
432: printf("return code = 0x%x\n", r);
433: }
434:
435: void
436: freecommands(struct command *cmd)
437: {
438: struct command *next;
439:
440: while (cmd) {
441: next = cmd->next;
442: free(cmd);
443: cmd = next;
444: }
445: }