fd closed twice

fd close()ed after call to unix_file_del of same fd

Change-Id: Ia8bf77b72b6cea7c70e004199ce11a33b6260720
Signed-off-by: Eyal Bari <ebari@cisco.com>
diff --git a/vnet/vnet/devices/af_packet/af_packet.c b/vnet/vnet/devices/af_packet/af_packet.c
index 17eddd6..cec25fe 100644
--- a/vnet/vnet/devices/af_packet/af_packet.c
+++ b/vnet/vnet/devices/af_packet/af_packet.c
@@ -308,6 +308,9 @@
       unix_file_del (&unix_main, unix_main.file_pool + apif->unix_file_index);
       apif->unix_file_index = ~0;
     }
+  else
+    close (apif->fd);
+
   ring_sz = apif->rx_req->tp_block_size * apif->rx_req->tp_block_nr +
     apif->tx_req->tp_block_size * apif->tx_req->tp_block_nr;
   if (munmap (apif->rx_ring, ring_sz))
@@ -315,7 +318,6 @@
 		  host_if_name);
   apif->rx_ring = NULL;
   apif->tx_ring = NULL;
-  close (apif->fd);
   apif->fd = -1;
 
   vec_free (apif->rx_req);
diff --git a/vnet/vnet/devices/netmap/netmap.c b/vnet/vnet/devices/netmap/netmap.c
index fe6f1ea..3bdb442 100644
--- a/vnet/vnet/devices/netmap/netmap.c
+++ b/vnet/vnet/devices/netmap/netmap.c
@@ -59,8 +59,7 @@
       unix_file_del (&unix_main, unix_main.file_pool + nif->unix_file_index);
       nif->unix_file_index = ~0;
     }
-
-  if (nif->fd > -1)
+  else if (nif->fd > -1)
     close (nif->fd);
 
   if (nif->mem_region)
diff --git a/vnet/vnet/devices/virtio/vhost-user.c b/vnet/vnet/devices/virtio/vhost-user.c
index 76c682f..2ca5aa8 100644
--- a/vnet/vnet/devices/virtio/vhost-user.c
+++ b/vnet/vnet/devices/virtio/vhost-user.c
@@ -252,10 +252,11 @@
       unix_file_del (&unix_main, unix_main.file_pool + vui->unix_file_index);
       vui->unix_file_index = ~0;
     }
+  else
+    close (vui->unix_fd);
 
   hash_unset (vum->vhost_user_interface_index_by_sock_fd, vui->unix_fd);
   hash_unset (vum->vhost_user_interface_index_by_listener_fd, vui->unix_fd);
-  close (vui->unix_fd);
   vui->unix_fd = -1;
   vui->is_up = 0;
   for (q = 0; q < vui->num_vrings; q++)
diff --git a/vnet/vnet/unix/tapcli.c b/vnet/vnet/unix/tapcli.c
index c2328f0..0be68a9 100644
--- a/vnet/vnet/unix/tapcli.c
+++ b/vnet/vnet/unix/tapcli.c
@@ -1025,10 +1025,11 @@
     unix_file_del (&unix_main, unix_main.file_pool + ti->unix_file_index);
     ti->unix_file_index = ~0;
   }
+  else
+    close(ti->unix_fd);
 
   hash_unset (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd);
   hash_unset (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index);
-  close(ti->unix_fd);
   close(ti->provision_fd);
   ti->unix_fd = -1;
   ti->provision_fd = -1;