[BACK]Return to usbhidaction.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / usbhidaction

Annotation of src/usr.bin/usbhidaction/usbhidaction.c, Revision 1.20

1.20    ! deraadt     1: /*     $OpenBSD: usbhidaction.c,v 1.19 2015/01/18 17:18:08 mpi 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:                isdemon = 1;
                    166:        }
                    167:
                    168:        for(;;) {
                    169:                n = read(fd, buf, sz);
                    170:                if (verbose > 2) {
                    171:                        printf("read %d bytes:", n);
                    172:                        for (i = 0; i < n; i++)
                    173:                                printf(" %02x", buf[i]);
                    174:                        printf("\n");
                    175:                }
                    176:                if (n < 0) {
                    177:                        if (verbose)
                    178:                                err(1, "read");
                    179:                        else
                    180:                                exit(1);
                    181:                }
                    182:                if (n != sz) {
                    183:                        err(2, "read size");
                    184:                }
                    185:                for (cmd = commands; cmd; cmd = cmd->next) {
                    186:                        val = hid_get_data(buf, &cmd->item);
                    187:                        if (cmd->value == val || cmd->anyvalue)
                    188:                                docmd(cmd, val, dev, argc, argv);
                    189:                }
                    190:                if (reparse) {
                    191:                        struct command *cmds =
                    192:                            parse_conf(conf, repd, reportid, ignore);
                    193:                        if (cmds) {
                    194:                                freecommands(commands);
                    195:                                commands = cmds;
                    196:                        }
                    197:                        reparse = 0;
                    198:                }
                    199:        }
                    200:
                    201:        exit(0);
                    202: }
                    203:
                    204: void
                    205: usage(void)
                    206: {
                    207:        extern char *__progname;
                    208:
1.12      sobrado   209:        fprintf(stderr, "usage: %s [-div] -c config-file -f device arg ...\n",
                    210:            __progname);
1.1       nate      211:        exit(1);
                    212: }
                    213:
                    214: static int
                    215: peek(FILE *f)
                    216: {
                    217:        int c;
                    218:
                    219:        c = getc(f);
                    220:        if (c != EOF)
                    221:                ungetc(c, f);
                    222:        return c;
                    223: }
                    224:
                    225: struct command *
                    226: parse_conf(const char *conf, report_desc_t repd, int reportid, int ignore)
                    227: {
                    228:        FILE *f;
                    229:        char *p;
                    230:        int line;
                    231:        char buf[SIZE], name[SIZE], value[SIZE], action[SIZE];
                    232:        char usage[SIZE], coll[SIZE];
                    233:        struct command *cmd, *cmds;
                    234:        struct hid_data *d;
                    235:        struct hid_item h;
                    236:        int u, lo, hi, range;
                    237:
                    238:        f = fopen(conf, "r");
                    239:        if (f == NULL)
                    240:                err(1, "%s", conf);
                    241:
                    242:        cmds = NULL;
                    243:        for (line = 1; ; line++) {
                    244:                if (fgets(buf, sizeof buf, f) == NULL)
                    245:                        break;
                    246:                if (buf[0] == '#' || buf[0] == '\n')
                    247:                        continue;
                    248:                p = strchr(buf, '\n');
                    249:                while (p && isspace(peek(f))) {
                    250:                        if (fgets(p, sizeof buf - strlen(buf), f) == NULL)
                    251:                                break;
                    252:                        p = strchr(buf, '\n');
                    253:                }
                    254:                if (p)
                    255:                        *p = 0;
                    256:                if (sscanf(buf, "%s %s %[^\n]", name, value, action) != 3) {
                    257:                        if (isdemon) {
                    258:                                syslog(LOG_WARNING, "config file `%s', line %d"
1.4       deraadt   259:                                    ", syntax error: %s", conf, line, buf);
1.1       nate      260:                                freecommands(cmds);
1.11      guenther  261:                                fclose(f);
1.1       nate      262:                                return (NULL);
                    263:                        } else {
1.6       jaredy    264:                                errx(1, "config file `%s', line %d"
1.4       deraadt   265:                                    ", syntax error: %s", conf, line, buf);
1.1       nate      266:                        }
                    267:                }
                    268:
                    269:                cmd = malloc(sizeof *cmd);
                    270:                if (cmd == NULL)
                    271:                        err(1, "malloc failed");
                    272:                cmd->next = cmds;
                    273:                cmds = cmd;
                    274:                cmd->line = line;
                    275:
                    276:                if (strcmp(value, "*") == 0) {
                    277:                        cmd->anyvalue = 1;
                    278:                } else {
                    279:                        cmd->anyvalue = 0;
                    280:                        if (sscanf(value, "%d", &cmd->value) != 1) {
                    281:                                if (isdemon) {
                    282:                                        syslog(LOG_WARNING,
1.4       deraadt   283:                                            "config file `%s', line %d, "
1.6       jaredy    284:                                            "bad value: %s",
1.4       deraadt   285:                                            conf, line, value);
1.1       nate      286:                                        freecommands(cmds);
1.11      guenther  287:                                        fclose(f);
1.1       nate      288:                                        return (NULL);
                    289:                                } else {
                    290:                                        errx(1, "config file `%s', line %d, "
1.6       jaredy    291:                                            "bad value: %s",
1.4       deraadt   292:                                            conf, line, value);
1.1       nate      293:                                }
                    294:                        }
                    295:                }
                    296:
                    297:                coll[0] = 0;
1.11      guenther  298:                d = hid_start_parse(repd, 1 << hid_input, reportid);
                    299:                if (d == NULL)
                    300:                        err(1, "hid_start_parse failed");
                    301:                while (hid_get_item(d, &h)) {
1.1       nate      302:                        if (verbose > 2)
                    303:                                printf("kind=%d usage=%x\n", h.kind, h.usage);
                    304:                        if (h.flags & HIO_CONST)
                    305:                                continue;
                    306:                        switch (h.kind) {
                    307:                        case hid_input:
                    308:                                if (h.usage_minimum != 0 ||
                    309:                                    h.usage_maximum != 0) {
                    310:                                        lo = h.usage_minimum;
                    311:                                        hi = h.usage_maximum;
                    312:                                        range = 1;
                    313:                                } else {
                    314:                                        lo = h.usage;
                    315:                                        hi = h.usage;
                    316:                                        range = 0;
                    317:                                }
                    318:                                for (u = lo; u <= hi; u++) {
                    319:                                        snprintf(usage, sizeof usage,  "%s:%s",
1.4       deraadt   320:                                                 hid_usage_page(HID_PAGE(u)),
1.1       nate      321:                                                 hid_usage_in_page(u));
                    322:                                        if (verbose > 2)
                    323:                                                printf("usage %s\n", usage);
                    324:                                        if (!strcasecmp(usage, name))
                    325:                                                goto foundhid;
                    326:                                        if (coll[0]) {
                    327:                                                snprintf(usage, sizeof usage,
                    328:                                                  "%s.%s:%s", coll+1,
1.4       deraadt   329:                                                  hid_usage_page(HID_PAGE(u)),
1.1       nate      330:                                                  hid_usage_in_page(u));
                    331:                                                if (verbose > 2)
                    332:                                                        printf("usage %s\n",
1.4       deraadt   333:                                                            usage);
1.1       nate      334:                                                if (!strcasecmp(usage, name))
                    335:                                                        goto foundhid;
                    336:                                        }
                    337:                                }
                    338:                                break;
                    339:                        case hid_collection:
                    340:                                snprintf(coll + strlen(coll),
                    341:                                    sizeof coll - strlen(coll),  ".%s:%s",
1.4       deraadt   342:                                    hid_usage_page(HID_PAGE(h.usage)),
1.1       nate      343:                                    hid_usage_in_page(h.usage));
                    344:                                break;
                    345:                        case hid_endcollection:
                    346:                                if (coll[0])
                    347:                                        *strrchr(coll, '.') = 0;
                    348:                                break;
                    349:                        default:
                    350:                                break;
                    351:                        }
                    352:                }
1.11      guenther  353:                hid_end_parse(d);
1.1       nate      354:                if (ignore) {
                    355:                        if (verbose)
1.2       jsyn      356:                                warnx("ignore item '%s'", name);
1.11      guenther  357:                        /* pop and free this ignored item */
                    358:                        cmds = cmd->next;
                    359:                        free(cmd);
1.1       nate      360:                        continue;
                    361:                }
                    362:                if (isdemon) {
                    363:                        syslog(LOG_WARNING, "config file `%s', line %d, HID "
1.6       jaredy    364:                            "item not found: `%s'", conf, line, name);
1.1       nate      365:                        freecommands(cmds);
1.11      guenther  366:                        fclose(f);
1.1       nate      367:                        return (NULL);
                    368:                } else {
                    369:                        errx(1, "config file `%s', line %d, HID item "
1.6       jaredy    370:                            "not found: `%s'", conf, line, name);
1.1       nate      371:                }
                    372:
                    373:        foundhid:
                    374:                hid_end_parse(d);
                    375:                cmd->item = h;
                    376:                cmd->name = strdup(name);
                    377:                cmd->action = strdup(action);
                    378:                if (range) {
                    379:                        if (cmd->value == 1)
                    380:                                cmd->value = u - lo;
                    381:                        else
                    382:                                cmd->value = -1;
                    383:                }
                    384:
                    385:                if (verbose)
                    386:                        printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
1.4       deraadt   387:                            cmd->value, cmd->action);
1.1       nate      388:        }
                    389:        fclose(f);
                    390:        return (cmds);
                    391: }
                    392:
                    393: void
                    394: docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
                    395: {
                    396:        char cmdbuf[SIZE], *p, *q;
                    397:        size_t len;
                    398:        int n, r;
1.15      jasper    399:        pid_t pid;
1.1       nate      400:
1.14      deraadt   401:        if (cmd->action == NULL) {
1.13      ckuethe   402:                if (verbose)
                    403:                        printf("no action for device %s value %d\n",
1.14      deraadt   404:                            hid, value);
1.13      ckuethe   405:                return;
                    406:        }
1.1       nate      407:        for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
                    408:                if (*p == '$') {
                    409:                        p++;
                    410:                        len = &cmdbuf[SIZE-1] - q;
1.17      deraadt   411:                        if (isdigit((unsigned char)*p)) {
1.1       nate      412:                                n = strtol(p, &p, 10) - 1;
                    413:                                if (n >= 0 && n < argc) {
                    414:                                        strncpy(q, argv[n], len);
                    415:                                        q += strlen(q);
                    416:                                }
                    417:                        } else if (*p == 'V') {
                    418:                                p++;
                    419:                                snprintf(q, len, "%d", value);
                    420:                                q += strlen(q);
                    421:                        } else if (*p == 'N') {
                    422:                                p++;
                    423:                                strncpy(q, cmd->name, len);
                    424:                                q += strlen(q);
                    425:                        } else if (*p == 'H') {
                    426:                                p++;
                    427:                                strncpy(q, hid, len);
                    428:                                q += strlen(q);
                    429:                        } else if (*p) {
                    430:                                *q++ = *p++;
                    431:                        }
                    432:                } else {
                    433:                        *q++ = *p++;
                    434:                }
                    435:        }
                    436:        *q = 0;
                    437:
1.15      jasper    438:        pid = fork();
                    439:        if (pid == -1)
                    440:                warn("fork failed");
                    441:        else if (pid == 0) {
                    442:                setpgid(0, 0);
                    443:                if (verbose)
                    444:                        printf("executing '%s'\n", cmdbuf);
                    445:                r = execl(_PATH_BSHELL, "sh", "-c", cmdbuf, NULL);
                    446:                err(1, "execl");
                    447:        }
1.1       nate      448: }
                    449:
                    450: void
                    451: freecommands(struct command *cmd)
                    452: {
                    453:        struct command *next;
                    454:
                    455:        while (cmd) {
                    456:                next = cmd->next;
                    457:                free(cmd);
                    458:                cmd = next;
                    459:        }
                    460: }