[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.25

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