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

1.9     ! deraadt     1: /*     $OpenBSD: usbhidaction.c,v 1.8 2006/07/09 23:02:21 mk 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:  * 3. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *        This product includes software developed by the NetBSD
                     22:  *        Foundation, Inc. and its contributors.
                     23:  * 4. Neither the name of The NetBSD Foundation nor the names of its
                     24:  *    contributors may be used to endorse or promote products derived
                     25:  *    from this software without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     28:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     29:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     30:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     31:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     32:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     33:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     34:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     35:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     36:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     37:  * POSSIBILITY OF SUCH DAMAGE.
                     38:  */
                     39:
                     40: #include <stdio.h>
                     41: #include <stdlib.h>
                     42: #include <string.h>
                     43: #include <ctype.h>
                     44: #include <err.h>
                     45: #include <fcntl.h>
                     46: #include <limits.h>
                     47: #include <unistd.h>
                     48: #include <sys/types.h>
                     49: #include <sys/ioctl.h>
                     50: #include <dev/usb/usb.h>
                     51: #include <dev/usb/usbhid.h>
                     52: #include <usbhid.h>
                     53: #include <util.h>
                     54: #include <syslog.h>
                     55: #include <signal.h>
                     56:
                     57: int verbose = 0;
                     58: int isdemon = 0;
1.9     ! deraadt    59:
        !            60: volatile sig_atomic_t reparse = 0;
1.1       nate       61:
                     62: struct command {
                     63:        struct command *next;
                     64:        int line;
                     65:
                     66:        struct hid_item item;
                     67:        int value;
                     68:        char anyvalue;
                     69:        char *name;
                     70:        char *action;
                     71: };
                     72: struct command *commands;
                     73:
                     74: #define SIZE 4000
                     75:
                     76: void usage(void);
                     77: struct command *parse_conf(const char *, report_desc_t, int, int);
                     78: void docmd(struct command *, int, const char *, int, char **);
                     79: void freecommands(struct command *);
                     80:
1.5       deraadt    81: /* ARGSUSED */
1.1       nate       82: static void
1.5       deraadt    83: sighup(int signo)
1.1       nate       84: {
                     85:        reparse = 1;
                     86: }
                     87:
                     88: int
                     89: main(int argc, char **argv)
                     90: {
                     91:        const char *conf = NULL;
                     92:        const char *dev = NULL;
                     93:        int fd, ch, sz, n, val, i;
                     94:        int demon, ignore;
                     95:        report_desc_t repd;
                     96:        char buf[100];
                     97:        char devnamebuf[PATH_MAX];
                     98:        struct command *cmd;
                     99:        int reportid;
                    100:
                    101:        demon = 1;
                    102:        ignore = 0;
                    103:        while ((ch = getopt(argc, argv, "c:df:iv")) != -1) {
                    104:                switch(ch) {
                    105:                case 'c':
                    106:                        conf = optarg;
                    107:                        break;
                    108:                case 'd':
                    109:                        demon ^= 1;
                    110:                        break;
                    111:                case 'i':
                    112:                        ignore++;
                    113:                        break;
                    114:                case 'f':
                    115:                        dev = optarg;
                    116:                        break;
                    117:                case 'v':
                    118:                        demon = 0;
                    119:                        verbose++;
                    120:                        break;
                    121:                case '?':
                    122:                default:
                    123:                        usage();
                    124:                }
                    125:        }
                    126:        argc -= optind;
                    127:        argv += optind;
                    128:
                    129:        if (conf == NULL || dev == NULL)
                    130:                usage();
                    131:
1.4       deraadt   132:        if (hid_start(NULL) == -1)
                    133:                errx(1, "hid_init");
1.1       nate      134:
                    135:        if (dev[0] != '/') {
                    136:                snprintf(devnamebuf, sizeof(devnamebuf), "/dev/%s%s",
1.4       deraadt   137:                    isdigit(dev[0]) ? "uhid" : "", dev);
1.1       nate      138:                dev = devnamebuf;
                    139:        }
1.8       mk        140:
                    141:        if (demon && conf[0] != '/')
                    142:                errx(1, "config file must have an absolute path, %s", conf);
1.1       nate      143:
                    144:        fd = open(dev, O_RDWR);
                    145:        if (fd < 0)
                    146:                err(1, "%s", dev);
1.7       fgsch     147:
                    148:        /* Avoid passing the device file descriptor to executed commands */
                    149:        if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
                    150:                err(1, "fcntl(F_SETFD, FD_CLOEXEC)");
                    151:
1.1       nate      152:        if (ioctl(fd, USB_GET_REPORT_ID, &reportid) < 0)
                    153:                reportid = -1;
                    154:        repd = hid_get_report_desc(fd);
                    155:        if (repd == NULL)
1.2       jsyn      156:                err(1, "hid_get_report_desc() failed");
1.1       nate      157:
                    158:        commands = parse_conf(conf, repd, reportid, ignore);
                    159:
                    160:        sz = hid_report_size(repd, hid_input, reportid);
                    161:
                    162:        if (verbose)
                    163:                printf("report size %d\n", sz);
                    164:        if (sz > sizeof buf)
                    165:                errx(1, "report too large");
                    166:
                    167:        (void)signal(SIGHUP, sighup);
                    168:
                    169:        if (demon) {
                    170:                if (daemon(0, 0) < 0)
                    171:                        err(1, "daemon()");
                    172:                pidfile(NULL);
                    173:                isdemon = 1;
                    174:        }
                    175:
                    176:        for(;;) {
                    177:                n = read(fd, buf, sz);
                    178:                if (verbose > 2) {
                    179:                        printf("read %d bytes:", n);
                    180:                        for (i = 0; i < n; i++)
                    181:                                printf(" %02x", buf[i]);
                    182:                        printf("\n");
                    183:                }
                    184:                if (n < 0) {
                    185:                        if (verbose)
                    186:                                err(1, "read");
                    187:                        else
                    188:                                exit(1);
                    189:                }
                    190: #if 0
                    191:                if (n != sz) {
                    192:                        err(2, "read size");
                    193:                }
                    194: #endif
                    195:                for (cmd = commands; cmd; cmd = cmd->next) {
                    196:                        val = hid_get_data(buf, &cmd->item);
                    197:                        if (cmd->value == val || cmd->anyvalue)
                    198:                                docmd(cmd, val, dev, argc, argv);
                    199:                }
                    200:                if (reparse) {
                    201:                        struct command *cmds =
                    202:                            parse_conf(conf, repd, reportid, ignore);
                    203:                        if (cmds) {
                    204:                                freecommands(commands);
                    205:                                commands = cmds;
                    206:                        }
                    207:                        reparse = 0;
                    208:                }
                    209:        }
                    210:
                    211:        exit(0);
                    212: }
                    213:
                    214: void
                    215: usage(void)
                    216: {
                    217:        extern char *__progname;
                    218:
1.3       jmc       219:        fprintf(stderr, "Usage: %s [-div] -c config-file -f device "
                    220:                "arg ...\n", __progname);
1.1       nate      221:        exit(1);
                    222: }
                    223:
                    224: static int
                    225: peek(FILE *f)
                    226: {
                    227:        int c;
                    228:
                    229:        c = getc(f);
                    230:        if (c != EOF)
                    231:                ungetc(c, f);
                    232:        return c;
                    233: }
                    234:
                    235: struct command *
                    236: parse_conf(const char *conf, report_desc_t repd, int reportid, int ignore)
                    237: {
                    238:        FILE *f;
                    239:        char *p;
                    240:        int line;
                    241:        char buf[SIZE], name[SIZE], value[SIZE], action[SIZE];
                    242:        char usage[SIZE], coll[SIZE];
                    243:        struct command *cmd, *cmds;
                    244:        struct hid_data *d;
                    245:        struct hid_item h;
                    246:        int u, lo, hi, range;
                    247:
                    248:        f = fopen(conf, "r");
                    249:        if (f == NULL)
                    250:                err(1, "%s", conf);
                    251:
                    252:        cmds = NULL;
                    253:        for (line = 1; ; line++) {
                    254:                if (fgets(buf, sizeof buf, f) == NULL)
                    255:                        break;
                    256:                if (buf[0] == '#' || buf[0] == '\n')
                    257:                        continue;
                    258:                p = strchr(buf, '\n');
                    259:                while (p && isspace(peek(f))) {
                    260:                        if (fgets(p, sizeof buf - strlen(buf), f) == NULL)
                    261:                                break;
                    262:                        p = strchr(buf, '\n');
                    263:                }
                    264:                if (p)
                    265:                        *p = 0;
                    266:                if (sscanf(buf, "%s %s %[^\n]", name, value, action) != 3) {
                    267:                        if (isdemon) {
                    268:                                syslog(LOG_WARNING, "config file `%s', line %d"
1.4       deraadt   269:                                    ", syntax error: %s", conf, line, buf);
1.1       nate      270:                                freecommands(cmds);
                    271:                                return (NULL);
                    272:                        } else {
1.6       jaredy    273:                                errx(1, "config file `%s', line %d"
1.4       deraadt   274:                                    ", syntax error: %s", conf, line, buf);
1.1       nate      275:                        }
                    276:                }
                    277:
                    278:                cmd = malloc(sizeof *cmd);
                    279:                if (cmd == NULL)
                    280:                        err(1, "malloc failed");
                    281:                cmd->next = cmds;
                    282:                cmds = cmd;
                    283:                cmd->line = line;
                    284:
                    285:                if (strcmp(value, "*") == 0) {
                    286:                        cmd->anyvalue = 1;
                    287:                } else {
                    288:                        cmd->anyvalue = 0;
                    289:                        if (sscanf(value, "%d", &cmd->value) != 1) {
                    290:                                if (isdemon) {
                    291:                                        syslog(LOG_WARNING,
1.4       deraadt   292:                                            "config file `%s', line %d, "
1.6       jaredy    293:                                            "bad value: %s",
1.4       deraadt   294:                                            conf, line, value);
1.1       nate      295:                                        freecommands(cmds);
                    296:                                        return (NULL);
                    297:                                } else {
                    298:                                        errx(1, "config file `%s', line %d, "
1.6       jaredy    299:                                            "bad value: %s",
1.4       deraadt   300:                                            conf, line, value);
1.1       nate      301:                                }
                    302:                        }
                    303:                }
                    304:
                    305:                coll[0] = 0;
                    306:                for (d = hid_start_parse(repd, 1 << hid_input, reportid);
1.4       deraadt   307:                    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:                }
                    359:                if (ignore) {
                    360:                        if (verbose)
1.2       jsyn      361:                                warnx("ignore item '%s'", name);
1.1       nate      362:                        continue;
                    363:                }
                    364:                if (isdemon) {
                    365:                        syslog(LOG_WARNING, "config file `%s', line %d, HID "
1.6       jaredy    366:                            "item not found: `%s'", conf, line, name);
1.1       nate      367:                        freecommands(cmds);
                    368:                        return (NULL);
                    369:                } else {
                    370:                        errx(1, "config file `%s', line %d, HID item "
1.6       jaredy    371:                            "not found: `%s'", conf, line, name);
1.1       nate      372:                }
                    373:
                    374:        foundhid:
                    375:                hid_end_parse(d);
                    376:                cmd->item = h;
                    377:                cmd->name = strdup(name);
                    378:                cmd->action = strdup(action);
                    379:                if (range) {
                    380:                        if (cmd->value == 1)
                    381:                                cmd->value = u - lo;
                    382:                        else
                    383:                                cmd->value = -1;
                    384:                }
                    385:
                    386:                if (verbose)
                    387:                        printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
1.4       deraadt   388:                            cmd->value, cmd->action);
1.1       nate      389:        }
                    390:        fclose(f);
                    391:        return (cmds);
                    392: }
                    393:
                    394: void
                    395: docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
                    396: {
                    397:        char cmdbuf[SIZE], *p, *q;
                    398:        size_t len;
                    399:        int n, r;
                    400:
                    401:        for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
                    402:                if (*p == '$') {
                    403:                        p++;
                    404:                        len = &cmdbuf[SIZE-1] - q;
                    405:                        if (isdigit(*p)) {
                    406:                                n = strtol(p, &p, 10) - 1;
                    407:                                if (n >= 0 && n < argc) {
                    408:                                        strncpy(q, argv[n], len);
                    409:                                        q += strlen(q);
                    410:                                }
                    411:                        } else if (*p == 'V') {
                    412:                                p++;
                    413:                                snprintf(q, len, "%d", value);
                    414:                                q += strlen(q);
                    415:                        } else if (*p == 'N') {
                    416:                                p++;
                    417:                                strncpy(q, cmd->name, len);
                    418:                                q += strlen(q);
                    419:                        } else if (*p == 'H') {
                    420:                                p++;
                    421:                                strncpy(q, hid, len);
                    422:                                q += strlen(q);
                    423:                        } else if (*p) {
                    424:                                *q++ = *p++;
                    425:                        }
                    426:                } else {
                    427:                        *q++ = *p++;
                    428:                }
                    429:        }
                    430:        *q = 0;
                    431:
                    432:        if (verbose)
                    433:                printf("system '%s'\n", cmdbuf);
                    434:        r = system(cmdbuf);
                    435:        if (verbose > 1 && r)
                    436:                printf("return code = 0x%x\n", r);
                    437: }
                    438:
                    439: void
                    440: freecommands(struct command *cmd)
                    441: {
                    442:        struct command *next;
                    443:
                    444:        while (cmd) {
                    445:                next = cmd->next;
                    446:                free(cmd);
                    447:                cmd = next;
                    448:        }
                    449: }