[CRIU] Re: [PATCH 04/12] util-net: Add send_fds and recv_fds

Pavel Emelyanov xemul at parallels.com
Tue Mar 27 08:52:37 EDT 2012


On 03/26/2012 01:33 AM, Cyrill Gorcunov wrote:
> We will need these helpers to transfer file
> descriptors from dumpee to our space.
> 
> Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>

OK.

Now resplit the patches, so that old code is patched to support
multiple fds, instead of "new code is written, then old one is
removed".

> ---
>  include/util-net.h |    5 ++-
>  util-net.c         |  120 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 124 insertions(+), 1 deletions(-)
> 
> diff --git a/include/util-net.h b/include/util-net.h
> index 5993778..e96e603 100644
> --- a/include/util-net.h
> +++ b/include/util-net.h
> @@ -26,9 +26,12 @@ struct scm_fdset {
>  	struct iovec	iov;
>  	char		msg_buf[CR_SCM_MSG_SIZE];
>  	int		msg;	/* We are to send at least one byte */
> -	int		nr_fds;
>  };
>  
>  extern int send_fd(int sock, struct sockaddr_un *saddr, int len, int fd);
>  extern int recv_fd(int sock);
> +
> +extern int send_fds(int sock, struct sockaddr_un *saddr, int saddr_len, int *fds, int nr_fds);
> +extern int recv_fds(int sock, int *fds, int nr_fds);
> +
>  #endif
> diff --git a/util-net.c b/util-net.c
> index afd51bd..d4da6cc 100644
> --- a/util-net.c
> +++ b/util-net.c
> @@ -1,8 +1,128 @@
>  #include <sys/socket.h>
>  #include <sys/un.h>
> +#include <errno.h>
>  
> +#include "compiler.h"
> +#include "types.h"
> +#include "builtins.h"
>  #include "syscall.h"
>  
> +#include "util-net.h"
> +
> +static void scm_fdset_init_chunk(struct scm_fdset *fdset, int nr_fds)
> +{
> +	struct cmsghdr *cmsg;
> +	cmsg				= CMSG_FIRSTHDR(&fdset->hdr);
> +	fdset->hdr.msg_controllen	= CMSG_LEN(sizeof(int) * nr_fds);
> +	cmsg->cmsg_len			= fdset->hdr.msg_controllen;
> +}
> +
> +static int *scm_fdset_init(struct scm_fdset *fdset, struct sockaddr_un *saddr, int saddr_len)
> +{
> +	struct cmsghdr *cmsg;
> +
> +	BUILD_BUG_ON(CR_SCM_MAX_FD > SCM_MAX_FD);
> +	BUILD_BUG_ON(sizeof(fdset->msg_buf) < (CMSG_SPACE(sizeof(int) * CR_SCM_MAX_FD)));
> +
> +	fdset->msg			= 0;
> +
> +	fdset->iov.iov_base		= &fdset->msg;
> +	fdset->iov.iov_len		= sizeof(fdset->msg);
> +
> +	fdset->hdr.msg_iov		= &fdset->iov;
> +	fdset->hdr.msg_iovlen		= 1;
> +	fdset->hdr.msg_name		= (struct sockaddr *)saddr;
> +	fdset->hdr.msg_namelen		= saddr_len;
> +
> +	fdset->hdr.msg_control		= &fdset->msg_buf;
> +	fdset->hdr.msg_controllen	= CMSG_LEN(sizeof(int) * CR_SCM_MAX_FD);
> +
> +	cmsg				= CMSG_FIRSTHDR(&fdset->hdr);
> +	cmsg->cmsg_len			= fdset->hdr.msg_controllen;
> +	cmsg->cmsg_level		= SOL_SOCKET;
> +	cmsg->cmsg_type			= SCM_RIGHTS;
> +
> +	return (int *)CMSG_DATA(cmsg);
> +}
> +
> +static int scm_fdset_send(int sock, struct scm_fdset *fdset)
> +{
> +	int ret = sys_sendmsg(sock, &fdset->hdr, 0);
> +	if (ret <= 0)
> +		return ret ? : -1;
> +	return 0;
> +}
> +
> +static int scm_fdset_recv(int sock, struct scm_fdset *fdset)
> +{
> +	struct cmsghdr *cmsg;
> +	int ret, nr_fds;
> +
> +	ret = sys_recvmsg(sock, &fdset->hdr, 0);
> +	if (ret <= 0)
> +		return ret ? : -1;
> +
> +	cmsg = CMSG_FIRSTHDR(&fdset->hdr);
> +	if (!cmsg || cmsg->cmsg_type != SCM_RIGHTS)
> +		return -EINVAL;
> +
> +	nr_fds = (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(nr_fds > CR_SCM_MAX_FD))
> +		*(volatile unsigned long *)NULL = 0xdead0000 + __LINE__;
> +
> +	return nr_fds;
> +}
> +
> +int send_fds(int sock, struct sockaddr_un *saddr, int saddr_len, int *fds, int nr_fds)
> +{
> +	struct scm_fdset fdset;
> +	int *fds_tx, i, min_fd;
> +
> +	fds_tx = scm_fdset_init(&fdset, saddr, saddr_len);
> +
> +	for (i = min_fd = 0; i < nr_fds; i += min_fd) {
> +		min_fd = min(CR_SCM_MAX_FD, nr_fds - i);
> +		scm_fdset_init_chunk(&fdset, min_fd);
> +		builtin_memcpy(fds_tx, &fds[i], sizeof(int) * min_fd);
> +
> +		if (scm_fdset_send(sock, &fdset))
> +			return -1;
> +	}
> +
> +	return 0;
> +}
> +
> +int recv_fds(int sock, int *fds, int nr_fds)
> +{
> +	struct scm_fdset fdset;
> +	int *fds_rx, i, min_fd;
> +
> +	fds_rx = scm_fdset_init(&fdset, NULL, 0);
> +
> +	for (i = min_fd = 0; i < nr_fds; i += min_fd) {
> +		min_fd = min(CR_SCM_MAX_FD, nr_fds - i);
> +		scm_fdset_init_chunk(&fdset, min_fd);
> +
> +		min_fd = scm_fdset_recv(sock, &fdset);
> +		if (min_fd < 0)
> +			return min_fd;
> +
> +		builtin_memcpy(&fds[i], fds_rx, sizeof(int) * min_fd);
> +	}
> +
> +	return 0;
> +}
> +
>  int send_fd(int sock, struct sockaddr_un *saddr, int len, int fd)
>  {
>  	char cmsgbuf[CMSG_SPACE(sizeof(int))];



More information about the CRIU mailing list