diff mbox

[ovs-dev] netdev-linux: change fd number if 0 was opened

Message ID 1443632777-20193-1-git-send-email-i.maximets@samsung.com
State Rejected
Headers show

Commit Message

Ilya Maximets Sept. 30, 2015, 5:06 p.m. UTC
Since 18167ffebe54e658579835a2e0acf67ec1d14692 fd = 0 instead of -1
considered as erroneous, because pollfd.fd on Windows does not take
negative values. So, we should avoid using it.

Reported-by: Nikita Kalyazin <n.kalyazin@samsung.com>
Signed-off-by: Ilya Maximets <i.maximets@samsung.com>
---
 lib/netdev-linux.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

Comments

Ben Pfaff Sept. 30, 2015, 5:29 p.m. UTC | #1
On Wed, Sep 30, 2015 at 08:06:17PM +0300, Ilya Maximets wrote:
> Since 18167ffebe54e658579835a2e0acf67ec1d14692 fd = 0 instead of -1
> considered as erroneous, because pollfd.fd on Windows does not take
> negative values. So, we should avoid using it.
> 
> Reported-by: Nikita Kalyazin <n.kalyazin@samsung.com>
> Signed-off-by: Ilya Maximets <i.maximets@samsung.com>

I don't think this is a good idea.  Lots of code opens fds, we're not
going to be able to find and fix every bit of code that does it this
way.
diff mbox

Patch

diff --git a/lib/netdev-linux.c b/lib/netdev-linux.c
index 584e804..e1c4e35 100644
--- a/lib/netdev-linux.c
+++ b/lib/netdev-linux.c
@@ -729,6 +729,20 @@  netdev_linux_alloc(void)
 }
 
 static void
+change_fd0(int *fd)
+{
+    int new_fd;
+    /* Forbiding to open fd 0 because of windows code restrictions
+     * inside poll_create_node(), that considers fd 0 erroneous. */
+    if (*fd == 0) {
+        new_fd = dup(*fd);
+        if (close(*fd) < 0)
+            VLOG_WARN("closing fd 0 failed: %s", ovs_strerror(error));
+        *fd = new_fd;
+    }
+}
+
+static void
 netdev_linux_common_construct(struct netdev_linux *netdev)
 {
     ovs_mutex_init(&netdev->mutex);
@@ -778,6 +792,7 @@  netdev_linux_construct_tap(struct netdev *netdev_)
 
     /* Open tap device. */
     netdev->tap_fd = open(tap_dev, O_RDWR);
+    change_fd0(&netdev->tap_fd);
     if (netdev->tap_fd < 0) {
         error = errno;
         VLOG_WARN("opening \"%s\" failed: %s", tap_dev, ovs_strerror(error));
@@ -871,6 +886,7 @@  netdev_linux_rxq_construct(struct netdev_rxq *rxq_)
 
         /* Create file descriptor. */
         rx->fd = socket(PF_PACKET, SOCK_RAW, 0);
+        change_fd0(&rx->fd);
         if (rx->fd < 0) {
             error = errno;
             VLOG_ERR("failed to create raw socket (%s)", ovs_strerror(error));