Annotation of src/usr.bin/usbhidaction/usbhidaction.c, Revision 1.17
1.17 ! deraadt 1: /* $OpenBSD: usbhidaction.c,v 1.16 2012/03/23 10:04:59 robert 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>
1.15 jasper 49: #include <paths.h>
1.1 nate 50:
51: int verbose = 0;
52: int isdemon = 0;
1.9 deraadt 53:
54: volatile sig_atomic_t reparse = 0;
1.1 nate 55:
56: struct command {
57: struct command *next;
58: int line;
59:
60: struct hid_item item;
61: int value;
62: char anyvalue;
63: char *name;
64: char *action;
65: };
66: struct command *commands;
67:
68: #define SIZE 4000
69:
70: void usage(void);
71: struct command *parse_conf(const char *, report_desc_t, int, int);
72: void docmd(struct command *, int, const char *, int, char **);
73: void freecommands(struct command *);
74:
1.5 deraadt 75: /* ARGSUSED */
1.1 nate 76: static void
1.5 deraadt 77: sighup(int signo)
1.1 nate 78: {
79: reparse = 1;
80: }
81:
82: int
83: main(int argc, char **argv)
84: {
85: const char *conf = NULL;
86: const char *dev = NULL;
87: int fd, ch, sz, n, val, i;
88: int demon, ignore;
89: report_desc_t repd;
90: char buf[100];
91: char devnamebuf[PATH_MAX];
92: struct command *cmd;
93: int reportid;
94:
95: demon = 1;
96: ignore = 0;
97: while ((ch = getopt(argc, argv, "c:df:iv")) != -1) {
98: switch(ch) {
99: case 'c':
100: conf = optarg;
101: break;
102: case 'd':
103: demon ^= 1;
104: break;
105: case 'i':
106: ignore++;
107: break;
108: case 'f':
109: dev = optarg;
110: break;
111: case 'v':
112: demon = 0;
113: verbose++;
114: break;
115: case '?':
116: default:
117: usage();
118: }
119: }
120: argc -= optind;
121: argv += optind;
122:
123: if (conf == NULL || dev == NULL)
124: usage();
125:
1.4 deraadt 126: if (hid_start(NULL) == -1)
127: errx(1, "hid_init");
1.1 nate 128:
129: if (dev[0] != '/') {
130: snprintf(devnamebuf, sizeof(devnamebuf), "/dev/%s%s",
1.17 ! deraadt 131: isdigit((unsigned char)dev[0]) ? "uhid" : "", dev);
1.1 nate 132: dev = devnamebuf;
133: }
1.8 mk 134:
135: if (demon && conf[0] != '/')
136: errx(1, "config file must have an absolute path, %s", conf);
1.1 nate 137:
138: fd = open(dev, O_RDWR);
139: if (fd < 0)
140: err(1, "%s", dev);
1.7 fgsch 141:
142: /* Avoid passing the device file descriptor to executed commands */
143: if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
144: err(1, "fcntl(F_SETFD, FD_CLOEXEC)");
145:
1.1 nate 146: if (ioctl(fd, USB_GET_REPORT_ID, &reportid) < 0)
147: reportid = -1;
148: repd = hid_get_report_desc(fd);
149: if (repd == NULL)
1.2 jsyn 150: err(1, "hid_get_report_desc() failed");
1.1 nate 151:
152: commands = parse_conf(conf, repd, reportid, ignore);
153:
154: sz = hid_report_size(repd, hid_input, reportid);
155:
156: if (verbose)
157: printf("report size %d\n", sz);
158: if (sz > sizeof buf)
159: errx(1, "report too large");
160:
161: (void)signal(SIGHUP, sighup);
1.16 robert 162:
163: /* we do not care about the children, so ignore them */
164: (void)signal(SIGCHLD, SIG_IGN);
1.1 nate 165:
166: if (demon) {
167: if (daemon(0, 0) < 0)
168: err(1, "daemon()");
169: pidfile(NULL);
170: isdemon = 1;
171: }
172:
173: for(;;) {
174: n = read(fd, buf, sz);
175: if (verbose > 2) {
176: printf("read %d bytes:", n);
177: for (i = 0; i < n; i++)
178: printf(" %02x", buf[i]);
179: printf("\n");
180: }
181: if (n < 0) {
182: if (verbose)
183: err(1, "read");
184: else
185: exit(1);
186: }
187: #if 0
188: if (n != sz) {
189: err(2, "read size");
190: }
191: #endif
192: for (cmd = commands; cmd; cmd = cmd->next) {
193: val = hid_get_data(buf, &cmd->item);
194: if (cmd->value == val || cmd->anyvalue)
195: docmd(cmd, val, dev, argc, argv);
196: }
197: if (reparse) {
198: struct command *cmds =
199: parse_conf(conf, repd, reportid, ignore);
200: if (cmds) {
201: freecommands(commands);
202: commands = cmds;
203: }
204: reparse = 0;
205: }
206: }
207:
208: exit(0);
209: }
210:
211: void
212: usage(void)
213: {
214: extern char *__progname;
215:
1.12 sobrado 216: fprintf(stderr, "usage: %s [-div] -c config-file -f device arg ...\n",
217: __progname);
1.1 nate 218: exit(1);
219: }
220:
221: static int
222: peek(FILE *f)
223: {
224: int c;
225:
226: c = getc(f);
227: if (c != EOF)
228: ungetc(c, f);
229: return c;
230: }
231:
232: struct command *
233: parse_conf(const char *conf, report_desc_t repd, int reportid, int ignore)
234: {
235: FILE *f;
236: char *p;
237: int line;
238: char buf[SIZE], name[SIZE], value[SIZE], action[SIZE];
239: char usage[SIZE], coll[SIZE];
240: struct command *cmd, *cmds;
241: struct hid_data *d;
242: struct hid_item h;
243: int u, lo, hi, range;
244:
245: f = fopen(conf, "r");
246: if (f == NULL)
247: err(1, "%s", conf);
248:
249: cmds = NULL;
250: for (line = 1; ; line++) {
251: if (fgets(buf, sizeof buf, f) == NULL)
252: break;
253: if (buf[0] == '#' || buf[0] == '\n')
254: continue;
255: p = strchr(buf, '\n');
256: while (p && isspace(peek(f))) {
257: if (fgets(p, sizeof buf - strlen(buf), f) == NULL)
258: break;
259: p = strchr(buf, '\n');
260: }
261: if (p)
262: *p = 0;
263: if (sscanf(buf, "%s %s %[^\n]", name, value, action) != 3) {
264: if (isdemon) {
265: syslog(LOG_WARNING, "config file `%s', line %d"
1.4 deraadt 266: ", syntax error: %s", conf, line, buf);
1.1 nate 267: freecommands(cmds);
1.11 guenther 268: fclose(f);
1.1 nate 269: return (NULL);
270: } else {
1.6 jaredy 271: errx(1, "config file `%s', line %d"
1.4 deraadt 272: ", syntax error: %s", conf, line, buf);
1.1 nate 273: }
274: }
275:
276: cmd = malloc(sizeof *cmd);
277: if (cmd == NULL)
278: err(1, "malloc failed");
279: cmd->next = cmds;
280: cmds = cmd;
281: cmd->line = line;
282:
283: if (strcmp(value, "*") == 0) {
284: cmd->anyvalue = 1;
285: } else {
286: cmd->anyvalue = 0;
287: if (sscanf(value, "%d", &cmd->value) != 1) {
288: if (isdemon) {
289: syslog(LOG_WARNING,
1.4 deraadt 290: "config file `%s', line %d, "
1.6 jaredy 291: "bad value: %s",
1.4 deraadt 292: conf, line, value);
1.1 nate 293: freecommands(cmds);
1.11 guenther 294: fclose(f);
1.1 nate 295: return (NULL);
296: } else {
297: errx(1, "config file `%s', line %d, "
1.6 jaredy 298: "bad value: %s",
1.4 deraadt 299: conf, line, value);
1.1 nate 300: }
301: }
302: }
303:
304: coll[0] = 0;
1.11 guenther 305: d = hid_start_parse(repd, 1 << hid_input, reportid);
306: if (d == NULL)
307: err(1, "hid_start_parse failed");
308: while (hid_get_item(d, &h)) {
1.1 nate 309: if (verbose > 2)
310: printf("kind=%d usage=%x\n", h.kind, h.usage);
311: if (h.flags & HIO_CONST)
312: continue;
313: switch (h.kind) {
314: case hid_input:
315: if (h.usage_minimum != 0 ||
316: h.usage_maximum != 0) {
317: lo = h.usage_minimum;
318: hi = h.usage_maximum;
319: range = 1;
320: } else {
321: lo = h.usage;
322: hi = h.usage;
323: range = 0;
324: }
325: for (u = lo; u <= hi; u++) {
326: snprintf(usage, sizeof usage, "%s:%s",
1.4 deraadt 327: hid_usage_page(HID_PAGE(u)),
1.1 nate 328: hid_usage_in_page(u));
329: if (verbose > 2)
330: printf("usage %s\n", usage);
331: if (!strcasecmp(usage, name))
332: goto foundhid;
333: if (coll[0]) {
334: snprintf(usage, sizeof usage,
335: "%s.%s:%s", coll+1,
1.4 deraadt 336: hid_usage_page(HID_PAGE(u)),
1.1 nate 337: hid_usage_in_page(u));
338: if (verbose > 2)
339: printf("usage %s\n",
1.4 deraadt 340: usage);
1.1 nate 341: if (!strcasecmp(usage, name))
342: goto foundhid;
343: }
344: }
345: break;
346: case hid_collection:
347: snprintf(coll + strlen(coll),
348: sizeof coll - strlen(coll), ".%s:%s",
1.4 deraadt 349: hid_usage_page(HID_PAGE(h.usage)),
1.1 nate 350: hid_usage_in_page(h.usage));
351: break;
352: case hid_endcollection:
353: if (coll[0])
354: *strrchr(coll, '.') = 0;
355: break;
356: default:
357: break;
358: }
359: }
1.11 guenther 360: hid_end_parse(d);
1.1 nate 361: if (ignore) {
362: if (verbose)
1.2 jsyn 363: warnx("ignore item '%s'", name);
1.11 guenther 364: /* pop and free this ignored item */
365: cmds = cmd->next;
366: free(cmd);
1.1 nate 367: continue;
368: }
369: if (isdemon) {
370: syslog(LOG_WARNING, "config file `%s', line %d, HID "
1.6 jaredy 371: "item not found: `%s'", conf, line, name);
1.1 nate 372: freecommands(cmds);
1.11 guenther 373: fclose(f);
1.1 nate 374: return (NULL);
375: } else {
376: errx(1, "config file `%s', line %d, HID item "
1.6 jaredy 377: "not found: `%s'", conf, line, name);
1.1 nate 378: }
379:
380: foundhid:
381: hid_end_parse(d);
382: cmd->item = h;
383: cmd->name = strdup(name);
384: cmd->action = strdup(action);
385: if (range) {
386: if (cmd->value == 1)
387: cmd->value = u - lo;
388: else
389: cmd->value = -1;
390: }
391:
392: if (verbose)
393: printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
1.4 deraadt 394: cmd->value, cmd->action);
1.1 nate 395: }
396: fclose(f);
397: return (cmds);
398: }
399:
400: void
401: docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
402: {
403: char cmdbuf[SIZE], *p, *q;
404: size_t len;
405: int n, r;
1.15 jasper 406: pid_t pid;
1.1 nate 407:
1.14 deraadt 408: if (cmd->action == NULL) {
1.13 ckuethe 409: if (verbose)
410: printf("no action for device %s value %d\n",
1.14 deraadt 411: hid, value);
1.13 ckuethe 412: return;
413: }
1.1 nate 414: for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
415: if (*p == '$') {
416: p++;
417: len = &cmdbuf[SIZE-1] - q;
1.17 ! deraadt 418: if (isdigit((unsigned char)*p)) {
1.1 nate 419: n = strtol(p, &p, 10) - 1;
420: if (n >= 0 && n < argc) {
421: strncpy(q, argv[n], len);
422: q += strlen(q);
423: }
424: } else if (*p == 'V') {
425: p++;
426: snprintf(q, len, "%d", value);
427: q += strlen(q);
428: } else if (*p == 'N') {
429: p++;
430: strncpy(q, cmd->name, len);
431: q += strlen(q);
432: } else if (*p == 'H') {
433: p++;
434: strncpy(q, hid, len);
435: q += strlen(q);
436: } else if (*p) {
437: *q++ = *p++;
438: }
439: } else {
440: *q++ = *p++;
441: }
442: }
443: *q = 0;
444:
1.15 jasper 445: pid = fork();
446: if (pid == -1)
447: warn("fork failed");
448: else if (pid == 0) {
449: setpgid(0, 0);
450: if (verbose)
451: printf("executing '%s'\n", cmdbuf);
452: r = execl(_PATH_BSHELL, "sh", "-c", cmdbuf, NULL);
453: err(1, "execl");
454: }
1.1 nate 455: }
456:
457: void
458: freecommands(struct command *cmd)
459: {
460: struct command *next;
461:
462: while (cmd) {
463: next = cmd->next;
464: free(cmd);
465: cmd = next;
466: }
467: }