From edwin@mavetju.org  Tue Oct 30 19:33:44 2001
Return-Path: <edwin@mavetju.org>
Received: from topaz.mdcc.cx (topaz.mdcc.cx [212.204.230.141])
	by hub.freebsd.org (Postfix) with ESMTP id 01B1637B401
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 30 Oct 2001 19:33:43 -0800 (PST)
Received: from k7.mavetju.org (topaz.mdcc.cx [212.204.230.141])
	by topaz.mdcc.cx (Postfix) with ESMTP id 297512B697
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 31 Oct 2001 04:33:39 +0100 (CET)
Received: by k7.mavetju.org (Postfix, from userid 1001)
	id C1959B4; Wed, 31 Oct 2001 14:33:35 +1100 (EST)
Message-Id: <20011031033335.C1959B4@k7.mavetju.org>
Date: Wed, 31 Oct 2001 14:33:35 +1100 (EST)
From: Edwin Groothuis <edwin@mavetju.org>
Reply-To: Edwin Groothuis <edwin@mavetju.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: libpcap doesn't work with -pthread
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         31649
>Category:       bin
>Synopsis:       libpcap doesn't work with -pthread
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Oct 30 19:40:01 PST 2001
>Closed-Date:    Mon Dec 17 11:34:11 PST 2001
>Last-Modified:  Mon Dec 17 11:34:23 PST 2001
>Originator:     Edwin Groothuis
>Release:        FreeBSD 4.4-RELEASE i386
>Organization:
-
>Environment:
System: FreeBSD k7.mavetju.org 4.4-RELEASE FreeBSD 4.4-RELEASE #0: Thu Oct 4 08:49:05 EST 2001 edwin@k7.mavetju.org:/usr/src/sys/compile/k7 i386

A normal 4.4 machine, build from the distribution cdrom

>Description:

When compiling a program which listens to a network device via the
pcap-library with -pthread, it stops working in the pcap_loop()
bits.

>How-To-Repeat:

#include <pcap.h>
#include <net/bpf.h>

#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/ioctl.h>

pcap_t *pd = NULL;
char *dev = "fxp0";
int link_offset;
int link_type;
int snaplen = 65535, promisc = 1, to = 1000;
char pc_err[PCAP_ERRBUF_SIZE];

void networkinit(void) {
    if ((pd = pcap_open_live(dev, snaplen, promisc, to, pc_err)) == NULL) {
        perror(pc_err);
        exit(-1);
    }
}

void process(u_char *data1, struct pcap_pkthdr* h, u_char *p) {
    fprintf(stderr,".");
}

void networkrun(void) {
    while (pcap_loop(pd, 0, (pcap_handler)process, 0));

    exit(-2);
}
    
int main(void) {
    networkinit();
    networkrun();
}


(You might have to tweak the fxp0 in the dev-variable)

Compile as "gcc -Wall -o nw nw.c -lpcap". It will print a dot for
every IP packet.
Compile as "gcc -Wall -o nw nw.c -lpcap -pthread". It will print
nothing.

>Fix:

none known.

>Release-Note:
>Audit-Trail:

From: Edwin Groothuis <edwin@mavetju.org>
To: freebsd-gnats-submit@FreeBSD.org
Cc:  
Subject: Re: bin/31649: libpcap doesn't work with -pthread
Date: Wed, 31 Oct 2001 17:17:11 +1100

 Some minor further investigations in libpcap showed that the read()
 function in /usr/src/contrib/libpcap/pcap-bpf.c:81 looks like it's
 blocking until the buffer is fully filled:
 
    if (p->cc == 0) {
 +    fprintf(stderr,"pcap_read before read (bufsize: %d bytes)\n",p->bufsize);
      cc = read(p->fd, (char *)p->buffer, p->bufsize);
 +    fprintf(stderr,"pcap_read after read (read: %d bytes)\n",cc);
      if (cc < 0) {
 
 
 This is the effect on a version without -pthread:
 [~/bpf] edwin@friet44>./nw_u 
 pcap_read entered
 pcap_read before read (bufsize: 32768 bytes)
 pcap_read after read (read: 244 bytes)
 ..pcap_read entered
 pcap_read before read (bufsize: 32768 bytes)
 pcap_read after read (read: 276 bytes)
 ..pcap_read entered
 
 This is the effect on a version with -pthread:
 [~/bpf] edwin@friet44>./nw
 pcap_read entered
 pcap_read before read bufsize: (32768 bytes)
 pcap_read after read (read: 32232 bytes)
 ...........................................................pcap_read entered
 pcap_read before read (bufsize: 32768 bytes)
 pcap_read after read (read: 32160 bytes)
 .........................................................pcap_read entered
 pcap_read before read (bufsize: 32768 bytes)
 pcap_read after read (read: 31648 bytes)
 
 I have no idea how to solve this...
 
 Edwin
 
 -- 
 Edwin Groothuis   |              Personal website: http://www.MavEtJu.org
 edwin@mavetju.org |           Interested in MUDs? Visit Fatal Dimensions:
 ------------------+                       http://www.FatalDimensions.org/

From: Edwin Groothuis <edwin@mavetju.org>
To: freebsd-gnats-submit@FreeBSD.org
Cc:  
Subject: Re: bin/31649: libpcap doesn't work with -pthread
Date: Wed, 31 Oct 2001 22:26:33 +1100

 I've tried the solution as suggested on:
     http://archives.neohapsis.com/archives/openbsd/2001-03/0857.html
 
 At least the threading works with it, but the data is still coming
 in a burst instead of on a per-packet base like in a normal unthreaded
 environment.
 
 Edwin
 
 -- 
 Edwin Groothuis   |              Personal website: http://www.MavEtJu.org
 edwin@mavetju.org |           Interested in MUDs? Visit Fatal Dimensions:
 ------------------+                       http://www.FatalDimensions.org/

From: "Guy Helmer" <ghelmer@palisadesys.com>
To: <freebsd-gnats-submit@FreeBSD.org>, <edwin@mavetju.org>
Cc:  
Subject: Re: bin/31649: libpcap doesn't work with -pthread
Date: Mon, 12 Nov 2001 08:46:31 -0600

 In essence, this is the same problem as identified in PR 22063.  If no one
 objects, I'll close this in a couple of days.
 
 I have submitted a patch to PR 22063 that solves this problem in the kernel
 code.
 
 Guy
 
State-Changed-From-To: open->closed 
State-Changed-By: jdp 
State-Changed-When: Mon Dec 17 11:34:11 PST 2001 
State-Changed-Why:  
Fixed in sys/net/bpf.c revision 1.59.2.8 and sys/net/bpfdesc.h  
revision 1.14.2.2. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=31649 
>Unformatted:
