Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libpcap Fix bpf_open so that we can run tcpdump without ...



details:   https://anonhg.NetBSD.org/src/rev/3ce70da42bb0
branches:  trunk
changeset: 581932:3ce70da42bb0
user:      dyoung <dyoung%NetBSD.org@localhost>
date:      Fri Jun 10 19:00:09 2005 +0000

description:
Fix bpf_open so that we can run tcpdump without write-access to
any of the /dev/bpf* device nodes.  In bpf_open, first try to open
each /dev/bpf* read-write; if that fails, try to open each read-only.

diffstat:

 lib/libpcap/pcap-bpf.c |  21 +++++++++++++--------
 1 files changed, 13 insertions(+), 8 deletions(-)

diffs (56 lines):

diff -r bbe539be364c -r 3ce70da42bb0 lib/libpcap/pcap-bpf.c
--- a/lib/libpcap/pcap-bpf.c    Fri Jun 10 18:24:15 2005 +0000
+++ b/lib/libpcap/pcap-bpf.c    Fri Jun 10 19:00:09 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pcap-bpf.c,v 1.13 2004/12/02 00:05:02 christos Exp $   */
+/*     $NetBSD: pcap-bpf.c,v 1.14 2005/06/10 19:00:09 dyoung Exp $     */
 
 /*
  * Copyright (c) 1993, 1994, 1995, 1996, 1998
@@ -26,7 +26,7 @@
 static const char rcsid[] =
     "@(#) Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.67.2.4 2003/11/22 00:06:28 guy Exp  (LBL)";
 #else
-__RCSID("$NetBSD: pcap-bpf.c,v 1.13 2004/12/02 00:05:02 christos Exp $");
+__RCSID("$NetBSD: pcap-bpf.c,v 1.14 2005/06/10 19:00:09 dyoung Exp $");
 #endif
 #endif
 
@@ -460,7 +460,7 @@
 {
        int fd;
 #ifndef _PATH_BPF
-       int n = 0;
+       int flags, n, tries;
        char device[sizeof "/dev/bpf0000000000"];
 #else
        const char *device = _PATH_BPF;
@@ -480,10 +480,14 @@
        /*
         * Go through all the minors and find one that isn't in use.
         */
-       do {
-               (void)snprintf(device, sizeof(device), "/dev/bpf%d", n++);
-               fd = open(device, O_RDWR);
-       } while (fd < 0 && errno == EBUSY);
+       for (tries = 0, flags = O_RDWR; tries < 2; tries++, flags = O_RDONLY) {
+               n = 0;
+               do {
+                       (void)snprintf(device, sizeof(device), "/dev/bpf%d",
+                           n++);
+                       fd = open(device, flags);
+               } while (fd < 0 && (errno == EBUSY || errno == EACCES));
+       }
 
        /*
         * XXX better message for all minors used
@@ -492,7 +496,8 @@
                (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
                    "(no devices found) %s: %s", device, pcap_strerror(errno));
 #else
-       if ((fd = open(device, O_RDWR)) == -1)
+       if ((fd = open(device, O_RDWR)) == -1 &&
+           (errno != EACCES || (fd = open(device, O_RDONLY)) == -1))
                (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
                    "(cannot open device) %s: %s", device, pcap_strerror(errno));
 #endif



Home | Main Index | Thread Index | Old Index