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()
This commit is contained in:
Daniel P. Berrange 2011-07-07 15:17:21 +01:00 committed by Jiri Denemark
parent 841a403f94
commit f4287c7676
5 changed files with 44 additions and 0 deletions

View File

@ -38,6 +38,7 @@ crypto/md5
dirname-lgpl
environ
fclose
fcntl
fcntl-h
ffs
fnmatch

View File

@ -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;

View File

@ -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);

View File

@ -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);
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;

View File

@ -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);