From f4287c767683e39a6e1e4b03f3062d2f8a48cece Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Thu, 7 Jul 2011 15:17:21 +0100 Subject: [PATCH] Add API for duplicating a socket/client file descriptor * src/rpc/virnetsocket.c, src/rpc/virnetsocket.h: Add virNetSocketDupFD() * src/rpc/virnetclient.c, src/rpc/virnetclient.h: Add virNetClientDupFD() and virNetClientGetFD() --- bootstrap.conf | 1 + src/rpc/virnetclient.c | 20 ++++++++++++++++++++ src/rpc/virnetclient.h | 3 +++ src/rpc/virnetsocket.c | 18 ++++++++++++++++++ src/rpc/virnetsocket.h | 2 ++ 5 files changed, 44 insertions(+) diff --git a/bootstrap.conf b/bootstrap.conf index 7882886b79..94af05134c 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -38,6 +38,7 @@ crypto/md5 dirname-lgpl environ fclose +fcntl fcntl-h ffs fnmatch diff --git a/src/rpc/virnetclient.c b/src/rpc/virnetclient.c index b845555bec..31d79efb69 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 90d19d397c..1fabcfde86 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 992e33aa20..73ff5eef11 100644 --- a/src/rpc/virnetsocket.c +++ b/src/rpc/virnetsocket.c @@ -28,6 +28,7 @@ #include #include #include +#include #ifdef HAVE_NETINET_TCP_H # include @@ -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); + 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 1e1c63c6e5..d6c85d28d7 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);