Commit d360133f authored by Cyrill Gorcunov's avatar Cyrill Gorcunov Committed by Pavel Emelyanov

util-net: Itroduce send_fds/recv_fds routines

We will need these helpers to transfer file
descriptors from dumpee to our space.

Also make send_fd/recv_fd to be a wrappers over
send_fds/revc_fds to not duplicate the code.
Signed-off-by: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 5813cd0b
...@@ -28,6 +28,23 @@ struct scm_fdset { ...@@ -28,6 +28,23 @@ struct scm_fdset {
int msg; /* We are to send at least one byte */ int msg; /* We are to send at least one byte */
}; };
extern int send_fd(int sock, struct sockaddr_un *saddr, int len, int fd); extern int send_fds(int sock, struct sockaddr_un *saddr, int saddr_len, int *fds, int nr_fds);
extern int recv_fd(int sock); extern int recv_fds(int sock, int *fds, int nr_fds);
static inline int send_fd(int sock, struct sockaddr_un *saddr, int saddr_len, int fd)
{
return send_fds(sock, saddr, saddr_len, &fd, 1);
}
static inline int recv_fd(int sock)
{
int fd, ret;
ret = recv_fds(sock, &fd, 1);
if (ret)
return -1;
return fd;
}
#endif #endif
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <errno.h> #include <errno.h>
#include "compiler.h" #include "compiler.h"
#include "memcpy_64.h"
#include "types.h" #include "types.h"
#include "syscall.h" #include "syscall.h"
...@@ -47,36 +48,64 @@ static int *scm_fdset_init(struct scm_fdset *fdset, struct sockaddr_un *saddr, i ...@@ -47,36 +48,64 @@ static int *scm_fdset_init(struct scm_fdset *fdset, struct sockaddr_un *saddr, i
return (int *)CMSG_DATA(cmsg); return (int *)CMSG_DATA(cmsg);
} }
int send_fd(int sock, struct sockaddr_un *saddr, int len, int fd) int send_fds(int sock, struct sockaddr_un *saddr, int len, int *fds, int nr_fds)
{ {
struct scm_fdset fdset; struct scm_fdset fdset;
int *cmsg_data; int *cmsg_data;
int i, min_fd, ret;
cmsg_data = scm_fdset_init(&fdset, saddr, len); cmsg_data = scm_fdset_init(&fdset, saddr, len);
scm_fdset_init_chunk(&fdset, 1); for (i = 0; i < nr_fds; i += min_fd) {
*cmsg_data = fd; min_fd = min(CR_SCM_MAX_FD, nr_fds - i);
scm_fdset_init_chunk(&fdset, min_fd);
builtin_memcpy(cmsg_data, &fds[i], sizeof(int) * min_fd);
return sys_sendmsg(sock, &fdset.hdr, 0); ret = sys_sendmsg(sock, &fdset.hdr, 0);
if (ret <= 0)
return ret ? : -1;
}
return 0;
} }
int recv_fd(int sock) int recv_fds(int sock, int *fds, int nr_fds)
{ {
struct scm_fdset fdset; struct scm_fdset fdset;
struct cmsghdr *cmsg; struct cmsghdr *cmsg;
int *cmsg_data; int *cmsg_data;
int ret; int ret;
int i, min_fd;
cmsg_data = scm_fdset_init(&fdset, NULL, 0); cmsg_data = scm_fdset_init(&fdset, NULL, 0);
scm_fdset_init_chunk(&fdset, 1); for (i = 0; i < nr_fds; i += min_fd) {
min_fd = min(CR_SCM_MAX_FD, nr_fds - i);
ret = sys_recvmsg(sock, &fdset.hdr, 0); scm_fdset_init_chunk(&fdset, min_fd);
if (ret < 0)
return ret; ret = sys_recvmsg(sock, &fdset.hdr, 0);
if (ret <= 0)
cmsg = CMSG_FIRSTHDR(&fdset.hdr); return ret ? : -1;
if (!cmsg || (cmsg->cmsg_type != SCM_RIGHTS))
return -2; cmsg = CMSG_FIRSTHDR(&fdset.hdr);
if (!cmsg || cmsg->cmsg_type != SCM_RIGHTS)
return *cmsg_data; return -EINVAL;
min_fd = (cmsg->cmsg_len - sizeof(struct cmsghdr)) / sizeof(int);
/*
* In case if kernel screwed the recepient, most probably
* the caller stack frame will be overwriten, just scream
* and exit.
*
* FIXME Need to sanitize util.h to be able to include it
* into files which do not have glibc and a couple of
* sys_write_ helpers. Meawhile opencoded BUG_ON here.
*/
if (unlikely(min_fd > CR_SCM_MAX_FD))
*(volatile unsigned long *)NULL = 0xdead0000 + __LINE__;
if (unlikely(min_fd <= 0))
return -1;
builtin_memcpy(&fds[i], cmsg_data, sizeof(int) * min_fd);
}
return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment