[CRIU] Re: [PATCH 5/6] util-net: Add send_fds and recv_fds
Pavel Emelyanov
xemul at parallels.com
Wed Mar 21 06:38:43 EDT 2012
On 03/21/2012 12:09 PM, Cyrill Gorcunov wrote:
> On Wed, Mar 21, 2012 at 11:47:56AM +0400, 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>
>> ---
>
> Updated version enveloped. Sorry for inconvenience.
>
> Cyrill
> diff --git a/util-net.c b/util-net.c
> index afd51bd..3ed3829 100644
> --- a/util-net.c
> +++ b/util-net.c
> @@ -1,8 +1,135 @@
> #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_update(struct scm_fdset *fdset, int nr_fds)
scm_fdset_init_chunk
> +{
> + int min_fd = min(nr_fds, CR_SCM_MAX_FD);
> + struct cmsghdr *cmsg;
> +
> + cmsg = CMSG_FIRSTHDR(&fdset->hdr);
> + fdset->hdr.msg_controllen = CMSG_LEN(sizeof(int) * min_fd);
> + cmsg->cmsg_len = fdset->hdr.msg_controllen;
> + fdset->nr_fds = min_fd;
> +}
> +
> +static int *scm_fdset_first(struct scm_fdset *fdset)
scm_fdset_array
> +{
> + struct cmsghdr *cmsg = CMSG_FIRSTHDR(&fdset->hdr);
> + return (int *)CMSG_DATA(cmsg);
> +}
> +
> +static void 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->nr_fds = CR_SCM_MAX_FD;
> + fdset->__pad = 0;
> +
> + fdset->iov.iov_base = &fdset->__pad;
> + fdset->iov.iov_len = sizeof(fdset->__pad);
Ugly. Rename this int __pad into char msg and leave a comment saying that
we need to send at least one byte.
> +
> + 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;
These tree are re-set by scm_fdset_update every loop cycle. No need in doing it here.
> +}
> +
> +static int scm_fdset_send(int sock, struct scm_fdset *fdset)
> +{
> + int ret = sys_sendmsg(sock, &fdset->hdr, 0);
> + if (ret <= 0)
> + return ret;
> +
> + return fdset->nr_fds;
> +}
> +
> +static int scm_fdset_recv(int sock, struct scm_fdset *fdset)
> +{
> + struct cmsghdr *cmsg;
> + int min_fd;
> + int ret = 0;
> +
> + scm_fdset_update(fdset, CR_SCM_MAX_FD);
> +
> + ret = sys_recvmsg(sock, &fdset->hdr, 0);
> + if (ret < 0)
> + return ret;
> +
> + cmsg = CMSG_FIRSTHDR(&fdset->hdr);
> + if (!cmsg || cmsg->cmsg_type != SCM_RIGHTS)
> + return -EINVAL;
> +
> + min_fd = (cmsg->cmsg_len - sizeof(struct cmsghdr)) / sizeof(int);
> + min_fd = min(min_fd, CR_SCM_MAX_FD);
> +
> + fdset->nr_fds = min_fd;
> +
> + return 0;
> +}
> +
> +int send_fds(int sock, struct sockaddr_un *saddr, int saddr_len, int *fds, int nr_fds)
> +{
> + struct scm_fdset fdset;
> + int *__fds_tx;
What are __-s for?
> + int i, j, ret;
> +
> + scm_fdset_init(&fdset, saddr, saddr_len);
> +
> + __fds_tx = scm_fdset_first(&fdset);
Make init return pointer to fd array. No separate call here.
> +
> + for (i = 0; i < nr_fds; i += fdset.nr_fds) {
> + scm_fdset_update(&fdset, nr_fds - i);
> + builtin_memcpy(__fds_tx, &fds[i], sizeof(int) * fdset.nr_fds);
> +
> + ret = scm_fdset_send(sock, &fdset);
> + if (ret < 0)
> + return ret;
> + }
> +
> + return 0;
> +}
> +
> +int recv_fds(int sock, int *fds, int nr_fds)
> +{
> + struct scm_fdset fdset;
> + int *__fds_rx;
> + int i, j, ret;
> +
> + scm_fdset_init(&fdset, NULL, 0);
> +
> + __fds_rx = scm_fdset_first(&fdset);
> +
> + for (i = 0; i < nr_fds; i += fdset.nr_fds) {
> + scm_fdset_update(&fdset, CR_SCM_MAX_FD);
> + ret = scm_fdset_recv(sock, &fdset);
> + if (ret < 0)
> + return ret;
> +
> + builtin_memcpy(&fds[i], __fds_rx, sizeof(int) * fdset.nr_fds);
> + }
> +
> + 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