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