Thu Jan 24 17:46:00 2013 UTC ()
use O_CLOEXEC


(christos)
diff -r1.24 -r1.25 src/usr.bin/usbhidaction/usbhidaction.c

cvs diff -r1.24 -r1.25 src/usr.bin/usbhidaction/usbhidaction.c (expand / switch to unified diff)

--- src/usr.bin/usbhidaction/usbhidaction.c 2009/01/18 07:14:03 1.24
+++ src/usr.bin/usbhidaction/usbhidaction.c 2013/01/24 17:46:00 1.25
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: usbhidaction.c,v 1.24 2009/01/18 07:14:03 lukem Exp $ */ 1/* $NetBSD: usbhidaction.c,v 1.25 2013/01/24 17:46:00 christos Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2000, 2002 The NetBSD Foundation, Inc. 4 * Copyright (c) 2000, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson <lennart@augustsson.net>. 8 * by Lennart Augustsson <lennart@augustsson.net>.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -21,27 +21,27 @@ @@ -21,27 +21,27 @@
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31#include <sys/cdefs.h> 31#include <sys/cdefs.h>
32 32
33#ifndef lint 33#ifndef lint
34__RCSID("$NetBSD: usbhidaction.c,v 1.24 2009/01/18 07:14:03 lukem Exp $"); 34__RCSID("$NetBSD: usbhidaction.c,v 1.25 2013/01/24 17:46:00 christos Exp $");
35#endif 35#endif
36 36
37#include <stdio.h> 37#include <stdio.h>
38#include <stdlib.h> 38#include <stdlib.h>
39#include <string.h> 39#include <string.h>
40#include <ctype.h> 40#include <ctype.h>
41#include <err.h> 41#include <err.h>
42#include <fcntl.h> 42#include <fcntl.h>
43#include <limits.h> 43#include <limits.h>
44#include <unistd.h> 44#include <unistd.h>
45#include <sys/types.h> 45#include <sys/types.h>
46#include <sys/ioctl.h> 46#include <sys/ioctl.h>
47#include <dev/usb/usb.h> 47#include <dev/usb/usb.h>
@@ -133,34 +133,30 @@ main(int argc, char **argv) @@ -133,34 +133,30 @@ main(int argc, char **argv)
133 usage(); 133 usage();
134 134
135 hid_init(table); 135 hid_init(table);
136 136
137 if (dev[0] != '/') { 137 if (dev[0] != '/') {
138 (void)snprintf(devnamebuf, sizeof(devnamebuf), "/dev/%s%s", 138 (void)snprintf(devnamebuf, sizeof(devnamebuf), "/dev/%s%s",
139 isdigit((unsigned char)dev[0]) ? "uhid" : "", dev); 139 isdigit((unsigned char)dev[0]) ? "uhid" : "", dev);
140 dev = devnamebuf; 140 dev = devnamebuf;
141 } 141 }
142 142
143 if (demon && conf[0] != '/') 143 if (demon && conf[0] != '/')
144 errx(1, "config file must have an absolute path, %s", conf); 144 errx(1, "config file must have an absolute path, %s", conf);
145 145
146 fd = open(dev, O_RDWR); 146 fd = open(dev, O_RDWR | O_CLOEXEC);
147 if (fd < 0) 147 if (fd < 0)
148 err(1, "%s", dev); 148 err(1, "%s", dev);
149 149
150 /* Avoid passing the device file descriptor to executed commands */ 
151 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) 
152 err(1, "fcntl(F_SETFD, FD_CLOEXEC)"); 
153 
154 if (ioctl(fd, USB_GET_REPORT_ID, &reportid) < 0) 150 if (ioctl(fd, USB_GET_REPORT_ID, &reportid) < 0)
155 reportid = -1; 151 reportid = -1;
156 repd = hid_get_report_desc(fd); 152 repd = hid_get_report_desc(fd);
157 if (repd == NULL) 153 if (repd == NULL)
158 err(1, "hid_get_report_desc() failed"); 154 err(1, "hid_get_report_desc() failed");
159 155
160 commands = parse_conf(conf, repd, reportid, ignore); 156 commands = parse_conf(conf, repd, reportid, ignore);
161 157
162 sz = hid_report_size(repd, hid_input, reportid); 158 sz = hid_report_size(repd, hid_input, reportid);
163 159
164 if (verbose) 160 if (verbose)
165 (void)printf("report size %d\n", sz); 161 (void)printf("report size %d\n", sz);
166 if (sz > (int)sizeof(buf)) 162 if (sz > (int)sizeof(buf))