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

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