qemu-devel
[Top][All Lists]
Advanced

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

[Qemu-devel] [PATCH 3/3] Add support for fd=msgfd for tap and socket net


From: Mark McLoughlin
Subject: [Qemu-devel] [PATCH 3/3] Add support for fd=msgfd for tap and socket networking
Date: Mon, 06 Jul 2009 18:32:13 +0100

This allows a program to initialize a host networking device using a
file descriptor passed over a unix monitor socket.

If the programs does e.g. "host_net_add tap fd=msgfd" it must pass
a file descriptor as part of that same message via SCM_RIGHTS ancillary
data.

Signed-off-by: Mark McLoughlin <address@hidden>
---
 net.c |   39 ++++++++++++++++++++++++++++++++++-----
 1 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/net.c b/net.c
index 001ebcb..0bb8b52 100644
--- a/net.c
+++ b/net.c
@@ -2388,6 +2388,30 @@ void qemu_check_nic_model_list(NICInfo *nd, const char * 
const *models,
     exit(exit_status);
 }
 
+static int net_handle_fd_param(Monitor *mon, const char *param)
+{
+    if (!strcmp(param, "msgfd")) {
+        int fd;
+
+        fd = monitor_get_msgfd(mon);
+        if (fd == -1) {
+            config_error(mon, "No file descriptor found in ancillary data\n");
+            return -1;
+        }
+
+        fd = dup(fd);
+        if (fd == -1) {
+            config_error(mon, "Failed to dup() file descriptor: %s\n",
+                         strerror(errno));
+            return -1;
+        }
+
+        return fd;
+    } else {
+        return strtol(param, NULL, 0);
+    }
+}
+
 int net_client_init(Monitor *mon, const char *device, const char *p)
 {
     char buf[1024];
@@ -2625,12 +2649,15 @@ int net_client_init(Monitor *mon, const char *device, 
const char *p)
             static const char * const fd_params[] = {
                 "vlan", "name", "fd", "sndbuf", NULL
             };
+            ret = -1;
             if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
                 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, 
p);
-                ret = -1;
                 goto out;
             }
-            fd = strtol(buf, NULL, 0);
+            fd = net_handle_fd_param(mon, buf);
+            if (fd == -1) {
+                goto out;
+            }
             fcntl(fd, F_SETFL, O_NONBLOCK);
             s = net_tap_fd_init(vlan, device, name, fd);
         } else {
@@ -2670,13 +2697,15 @@ int net_client_init(Monitor *mon, const char *device, 
const char *p)
                 "vlan", "name", "fd", NULL
             };
             int fd;
+            ret = -1;
             if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
                 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, 
p);
-                ret = -1;
                 goto out;
             }
-            fd = strtol(buf, NULL, 0);
-            ret = -1;
+            fd = net_handle_fd_param(mon, buf);
+            if (fd == -1) {
+                goto out;
+            }
             if (net_socket_fd_init(vlan, device, name, fd, 1))
                 ret = 0;
         } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
-- 
1.6.2.5





reply via email to

[Prev in Thread] Current Thread [Next in Thread]