tcpdump mailing list archives

Patch: Fix atexit() handler for pcap-dag


From: Jesper Peterson <jesper () endace com>
Date: Thu, 02 Oct 2003 17:05:50 +1200


This patch fixes the pcap-dag atexit() handler for non-execing child processes. Previously a fork()/exit() would stop the packet capture (doh!).

Also contains a couple of minor optimisations.

--
Jesper Peterson, Senior Software Developer
http://www.endace.com, +64 7 839 0540

Index: pcap-dag.c
===================================================================
RCS file: /tcpdump/master/libpcap/pcap-dag.c,v
retrieving revision 1.8
diff -u -r1.8 pcap-dag.c
--- pcap-dag.c  18 Aug 2003 22:00:16 -0000      1.8
+++ pcap-dag.c  2 Oct 2003 05:00:07 -0000
@@ -69,6 +69,7 @@
 typedef struct pcap_dag_node {
   struct pcap_dag_node *next;
   pcap_t *p;
+  pid_t pid;
 } pcap_dag_node_t;
 
 static pcap_dag_node_t *pcap_dags = NULL;
@@ -149,13 +150,15 @@
   }
 #endif
   delete_pcap_dag(p);
-  /* XXX - does "dag_close()" do this?  If so, we don't need to. */
-  close(p->fd);
 }
 
 static void atexit_handler(void) {
   while (pcap_dags != NULL) {
-    dag_platform_close(pcap_dags->p);
+    if (pcap_dags->pid == getpid()) {
+      dag_platform_close(pcap_dags->p);
+    } else {
+      delete_pcap_dag(pcap_dags->p);
+    }
   }
 }
 
@@ -173,6 +176,7 @@
 
   node->next = pcap_dags;
   node->p = p;
+  node->pid = getpid();
 
   pcap_dags = node;
 
@@ -187,19 +191,19 @@
   register dag_record_t *record;
   int rlen;
 
-  if (p->md.dag_mem_top - p->md.dag_mem_bottom < dag_record_size) {
+  /*
+   * The buffer is guaranteed to only contain complete records so any
+   * time top and bottom differ there will be at least one record available.
+   * Here we test the difference is at least the size of a record header
+   * using the poorly named constant 'dag_record_size'.
+   */
+  while ((p->md.dag_mem_top - p->md.dag_mem_bottom) < dag_record_size) {
     p->md.dag_mem_top = dag_offset(p->fd, &(p->md.dag_mem_bottom), 0);
   }
 
   record = (dag_record_t *)(p->md.dag_mem_base + p->md.dag_mem_bottom);
-  rlen = ntohs(record->rlen);
-  while (p->md.dag_mem_top - p->md.dag_mem_bottom < rlen) {
-    p->md.dag_mem_top = dag_offset(p->fd, &(p->md.dag_mem_bottom), 0);
-    record = (dag_record_t *)(p->md.dag_mem_base + p->md.dag_mem_bottom);
-    rlen = ntohs(record->rlen);
-  }
 
-  p->md.dag_mem_bottom += rlen;
+  p->md.dag_mem_bottom += ntohs(record->rlen);
   
   return record;
 }

Current thread: