提交 f4287c76 编写于 作者: D Daniel P. Berrange 提交者: Jiri Denemark

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()
上级 841a403f
......@@ -38,6 +38,7 @@ crypto/md5
dirname-lgpl
environ
fclose
fcntl
fcntl-h
ffs
fnmatch
......
......@@ -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;
......
......@@ -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);
......
......@@ -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;
......
......@@ -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);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册