[PATCH 1/6] Add API for duplicating a socket/client file descriptor

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

 



From: "Daniel P. Berrange" <berrange@xxxxxxxxxx>

* src/rpc/virnetsocket.c, src/rpc/virnetsocket.h: Add
  virNetSocketDupFD()
* src/rpc/virnetclient.c, src/rpc/virnetclient.h: Add
  virNetClientDupFD() and virNetClientGetFD()
---
 src/rpc/virnetclient.c |   20 ++++++++++++++++++++
 src/rpc/virnetclient.h |    3 +++
 src/rpc/virnetsocket.c |   18 ++++++++++++++++++
 src/rpc/virnetsocket.h |    2 ++
 4 files changed, 43 insertions(+), 0 deletions(-)

diff --git a/src/rpc/virnetclient.c b/src/rpc/virnetclient.c
index b845555..31d79ef 100644
--- a/src/rpc/virnetclient.c
+++ b/src/rpc/virnetclient.c
@@ -233,6 +233,26 @@ void virNetClientRef(virNetClientPtr client)
 }
 
 
+int virNetClientGetFD(virNetClientPtr client)
+{
+    int fd;
+    virNetClientLock(client);
+    fd = virNetSocketGetFD(client->sock);
+    virNetClientUnlock(client);
+    return fd;
+}
+
+
+int virNetClientDupFD(virNetClientPtr client, bool cloexec)
+{
+    int fd;
+    virNetClientLock(client);
+    fd = virNetSocketDupFD(client->sock, cloexec);
+    virNetClientUnlock(client);
+    return fd;
+}
+
+
 void virNetClientFree(virNetClientPtr client)
 {
     int i;
diff --git a/src/rpc/virnetclient.h b/src/rpc/virnetclient.h
index 90d19d3..1fabcfd 100644
--- a/src/rpc/virnetclient.h
+++ b/src/rpc/virnetclient.h
@@ -53,6 +53,9 @@ virNetClientPtr virNetClientNewExternal(const char **cmdargv);
 
 void virNetClientRef(virNetClientPtr client);
 
+int virNetClientGetFD(virNetClientPtr client);
+int virNetClientDupFD(virNetClientPtr client, bool cloexec);
+
 int virNetClientAddProgram(virNetClientPtr client,
                            virNetClientProgramPtr prog);
 
diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c
index 992e33a..c222743 100644
--- a/src/rpc/virnetsocket.c
+++ b/src/rpc/virnetsocket.c
@@ -28,6 +28,7 @@
 #include <unistd.h>
 #include <sys/wait.h>
 #include <signal.h>
+#include <fcntl.h>
 
 #ifdef HAVE_NETINET_TCP_H
 # include <netinet/tcp.h>
@@ -710,6 +711,23 @@ int virNetSocketGetFD(virNetSocketPtr sock)
 }
 
 
+int virNetSocketDupFD(virNetSocketPtr sock, bool cloexec)
+{
+    int fd;
+
+    if (cloexec)
+        fd = fcntl(sock->fd, F_DUPFD_CLOEXEC, (long) 0);
+    else
+        fd = dup(sock->fd);
+    if (fd < 0) {
+        virReportSystemError(errno, "%s",
+                             _("Unable to copy socket file handle"));
+        return -1;
+    }
+    return fd;
+}
+
+
 bool virNetSocketIsLocal(virNetSocketPtr sock)
 {
     bool isLocal = false;
diff --git a/src/rpc/virnetsocket.h b/src/rpc/virnetsocket.h
index 1e1c63c..d6c85d2 100644
--- a/src/rpc/virnetsocket.h
+++ b/src/rpc/virnetsocket.h
@@ -77,6 +77,8 @@ int virNetSocketNewConnectExternal(const char **cmdargv,
                                    virNetSocketPtr *addr);
 
 int virNetSocketGetFD(virNetSocketPtr sock);
+int virNetSocketDupFD(virNetSocketPtr sock, bool cloexec);
+
 bool virNetSocketIsLocal(virNetSocketPtr sock);
 
 int virNetSocketGetPort(virNetSocketPtr sock);
-- 
1.7.6

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]