GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/usbhidaction/usbhidaction.c Lines: 0 218 0.0 %
Date: 2017-11-07 Branches: 0 158 0.0 %

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