Annotation of src/usr.bin/usbhidaction/usbhidaction.c, Revision 1.18
1.18 ! guenther 1: /* $OpenBSD: usbhidaction.c,v 1.17 2013/11/27 00:13:23 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>
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:
1.18 ! guenther 138: fd = open(dev, O_RDWR | O_CLOEXEC);
1.1 nate 139: if (fd < 0)
140: err(1, "%s", dev);
1.7 fgsch 141:
1.1 nate 142: if (ioctl(fd, USB_GET_REPORT_ID, &reportid) < 0)
143: reportid = -1;
144: repd = hid_get_report_desc(fd);
145: if (repd == NULL)
1.2 jsyn 146: err(1, "hid_get_report_desc() failed");
1.1 nate 147:
148: commands = parse_conf(conf, repd, reportid, ignore);
149:
150: sz = hid_report_size(repd, hid_input, reportid);
151:
152: if (verbose)
153: printf("report size %d\n", sz);
154: if (sz > sizeof buf)
155: errx(1, "report too large");
156:
157: (void)signal(SIGHUP, sighup);
1.16 robert 158:
159: /* we do not care about the children, so ignore them */
160: (void)signal(SIGCHLD, SIG_IGN);
1.1 nate 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;
1.15 jasper 402: pid_t pid;
1.1 nate 403:
1.14 deraadt 404: if (cmd->action == NULL) {
1.13 ckuethe 405: if (verbose)
406: printf("no action for device %s value %d\n",
1.14 deraadt 407: hid, value);
1.13 ckuethe 408: return;
409: }
1.1 nate 410: for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
411: if (*p == '$') {
412: p++;
413: len = &cmdbuf[SIZE-1] - q;
1.17 deraadt 414: if (isdigit((unsigned char)*p)) {
1.1 nate 415: n = strtol(p, &p, 10) - 1;
416: if (n >= 0 && n < argc) {
417: strncpy(q, argv[n], len);
418: q += strlen(q);
419: }
420: } else if (*p == 'V') {
421: p++;
422: snprintf(q, len, "%d", value);
423: q += strlen(q);
424: } else if (*p == 'N') {
425: p++;
426: strncpy(q, cmd->name, len);
427: q += strlen(q);
428: } else if (*p == 'H') {
429: p++;
430: strncpy(q, hid, len);
431: q += strlen(q);
432: } else if (*p) {
433: *q++ = *p++;
434: }
435: } else {
436: *q++ = *p++;
437: }
438: }
439: *q = 0;
440:
1.15 jasper 441: pid = fork();
442: if (pid == -1)
443: warn("fork failed");
444: else if (pid == 0) {
445: setpgid(0, 0);
446: if (verbose)
447: printf("executing '%s'\n", cmdbuf);
448: r = execl(_PATH_BSHELL, "sh", "-c", cmdbuf, NULL);
449: err(1, "execl");
450: }
1.1 nate 451: }
452:
453: void
454: freecommands(struct command *cmd)
455: {
456: struct command *next;
457:
458: while (cmd) {
459: next = cmd->next;
460: free(cmd);
461: cmd = next;
462: }
463: }