Annotation of src/usr.bin/usbhidaction/usbhidaction.c, Revision 1.14
1.14 ! deraadt 1: /* $OpenBSD: usbhidaction.c,v 1.13 2010/06/29 17:15:23 ckuethe 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.12 sobrado 212: fprintf(stderr, "usage: %s [-div] -c config-file -f device arg ...\n",
213: __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);
1.11 guenther 264: fclose(f);
1.1 nate 265: return (NULL);
266: } else {
1.6 jaredy 267: errx(1, "config file `%s', line %d"
1.4 deraadt 268: ", syntax error: %s", conf, line, buf);
1.1 nate 269: }
270: }
271:
272: cmd = malloc(sizeof *cmd);
273: if (cmd == NULL)
274: err(1, "malloc failed");
275: cmd->next = cmds;
276: cmds = cmd;
277: cmd->line = line;
278:
279: if (strcmp(value, "*") == 0) {
280: cmd->anyvalue = 1;
281: } else {
282: cmd->anyvalue = 0;
283: if (sscanf(value, "%d", &cmd->value) != 1) {
284: if (isdemon) {
285: syslog(LOG_WARNING,
1.4 deraadt 286: "config file `%s', line %d, "
1.6 jaredy 287: "bad value: %s",
1.4 deraadt 288: conf, line, value);
1.1 nate 289: freecommands(cmds);
1.11 guenther 290: fclose(f);
1.1 nate 291: return (NULL);
292: } else {
293: errx(1, "config file `%s', line %d, "
1.6 jaredy 294: "bad value: %s",
1.4 deraadt 295: conf, line, value);
1.1 nate 296: }
297: }
298: }
299:
300: coll[0] = 0;
1.11 guenther 301: d = hid_start_parse(repd, 1 << hid_input, reportid);
302: if (d == NULL)
303: err(1, "hid_start_parse failed");
304: while (hid_get_item(d, &h)) {
1.1 nate 305: if (verbose > 2)
306: printf("kind=%d usage=%x\n", h.kind, h.usage);
307: if (h.flags & HIO_CONST)
308: continue;
309: switch (h.kind) {
310: case hid_input:
311: if (h.usage_minimum != 0 ||
312: h.usage_maximum != 0) {
313: lo = h.usage_minimum;
314: hi = h.usage_maximum;
315: range = 1;
316: } else {
317: lo = h.usage;
318: hi = h.usage;
319: range = 0;
320: }
321: for (u = lo; u <= hi; u++) {
322: snprintf(usage, sizeof usage, "%s:%s",
1.4 deraadt 323: hid_usage_page(HID_PAGE(u)),
1.1 nate 324: hid_usage_in_page(u));
325: if (verbose > 2)
326: printf("usage %s\n", usage);
327: if (!strcasecmp(usage, name))
328: goto foundhid;
329: if (coll[0]) {
330: snprintf(usage, sizeof usage,
331: "%s.%s:%s", coll+1,
1.4 deraadt 332: hid_usage_page(HID_PAGE(u)),
1.1 nate 333: hid_usage_in_page(u));
334: if (verbose > 2)
335: printf("usage %s\n",
1.4 deraadt 336: usage);
1.1 nate 337: if (!strcasecmp(usage, name))
338: goto foundhid;
339: }
340: }
341: break;
342: case hid_collection:
343: snprintf(coll + strlen(coll),
344: sizeof coll - strlen(coll), ".%s:%s",
1.4 deraadt 345: hid_usage_page(HID_PAGE(h.usage)),
1.1 nate 346: hid_usage_in_page(h.usage));
347: break;
348: case hid_endcollection:
349: if (coll[0])
350: *strrchr(coll, '.') = 0;
351: break;
352: default:
353: break;
354: }
355: }
1.11 guenther 356: hid_end_parse(d);
1.1 nate 357: if (ignore) {
358: if (verbose)
1.2 jsyn 359: warnx("ignore item '%s'", name);
1.11 guenther 360: /* pop and free this ignored item */
361: cmds = cmd->next;
362: free(cmd);
1.1 nate 363: continue;
364: }
365: if (isdemon) {
366: syslog(LOG_WARNING, "config file `%s', line %d, HID "
1.6 jaredy 367: "item not found: `%s'", conf, line, name);
1.1 nate 368: freecommands(cmds);
1.11 guenther 369: fclose(f);
1.1 nate 370: return (NULL);
371: } else {
372: errx(1, "config file `%s', line %d, HID item "
1.6 jaredy 373: "not found: `%s'", conf, line, name);
1.1 nate 374: }
375:
376: foundhid:
377: hid_end_parse(d);
378: cmd->item = h;
379: cmd->name = strdup(name);
380: cmd->action = strdup(action);
381: if (range) {
382: if (cmd->value == 1)
383: cmd->value = u - lo;
384: else
385: cmd->value = -1;
386: }
387:
388: if (verbose)
389: printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
1.4 deraadt 390: cmd->value, cmd->action);
1.1 nate 391: }
392: fclose(f);
393: return (cmds);
394: }
395:
396: void
397: docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
398: {
399: char cmdbuf[SIZE], *p, *q;
400: size_t len;
401: int n, r;
402:
1.14 ! deraadt 403: if (cmd->action == NULL) {
1.13 ckuethe 404: if (verbose)
405: printf("no action for device %s value %d\n",
1.14 ! deraadt 406: hid, value);
1.13 ckuethe 407: return;
408: }
1.1 nate 409: for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
410: if (*p == '$') {
411: p++;
412: len = &cmdbuf[SIZE-1] - q;
413: if (isdigit(*p)) {
414: n = strtol(p, &p, 10) - 1;
415: if (n >= 0 && n < argc) {
416: strncpy(q, argv[n], len);
417: q += strlen(q);
418: }
419: } else if (*p == 'V') {
420: p++;
421: snprintf(q, len, "%d", value);
422: q += strlen(q);
423: } else if (*p == 'N') {
424: p++;
425: strncpy(q, cmd->name, len);
426: q += strlen(q);
427: } else if (*p == 'H') {
428: p++;
429: strncpy(q, hid, len);
430: q += strlen(q);
431: } else if (*p) {
432: *q++ = *p++;
433: }
434: } else {
435: *q++ = *p++;
436: }
437: }
438: *q = 0;
439:
440: if (verbose)
441: printf("system '%s'\n", cmdbuf);
442: r = system(cmdbuf);
443: if (verbose > 1 && r)
444: printf("return code = 0x%x\n", r);
445: }
446:
447: void
448: freecommands(struct command *cmd)
449: {
450: struct command *next;
451:
452: while (cmd) {
453: next = cmd->next;
454: free(cmd);
455: cmd = next;
456: }
457: }