2016-11-02 03:27:46 +03:00
|
|
|
#ifndef __sys
|
|
|
|
#error "The __sys macro is required"
|
|
|
|
#endif
|
|
|
|
|
2016-11-02 03:29:07 +03:00
|
|
|
#ifndef __memcpy
|
|
|
|
#error "The __memcpy macro is required"
|
|
|
|
#endif
|
|
|
|
|
2016-11-22 20:03:00 +03:00
|
|
|
static void scm_fdset_init_chunk(struct scm_fdset *fdset, int nr_fds,
|
|
|
|
void *data, unsigned ch_size)
|
2016-11-02 03:27:46 +03:00
|
|
|
{
|
|
|
|
struct cmsghdr *cmsg;
|
2016-11-22 20:03:00 +03:00
|
|
|
static char dummy;
|
2016-11-02 03:27:46 +03:00
|
|
|
|
|
|
|
fdset->hdr.msg_controllen = CMSG_LEN(sizeof(int) * nr_fds);
|
|
|
|
|
|
|
|
cmsg = CMSG_FIRSTHDR(&fdset->hdr);
|
|
|
|
cmsg->cmsg_len = fdset->hdr.msg_controllen;
|
|
|
|
|
2016-11-22 20:03:00 +03:00
|
|
|
if (data) {
|
|
|
|
fdset->iov.iov_base = data;
|
|
|
|
fdset->iov.iov_len = nr_fds * ch_size;
|
|
|
|
} else {
|
|
|
|
fdset->iov.iov_base = &dummy;
|
|
|
|
fdset->iov.iov_len = 1;
|
|
|
|
}
|
2016-11-02 03:27:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int *scm_fdset_init(struct scm_fdset *fdset, struct sockaddr_un *saddr,
|
2016-11-14 23:01:00 +03:00
|
|
|
int saddr_len)
|
2016-11-02 03:27:46 +03:00
|
|
|
{
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
|
|
|
|
BUILD_BUG_ON(sizeof(fdset->msg_buf) < (CMSG_SPACE(sizeof(int) * CR_SCM_MAX_FD)));
|
|
|
|
|
2016-11-22 20:03:00 +03:00
|
|
|
fdset->iov.iov_base = (void *)0xdeadbeef;
|
2016-11-02 03:27:46 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
int send_fds(int sock, struct sockaddr_un *saddr, int len,
|
2016-11-22 20:03:00 +03:00
|
|
|
int *fds, int nr_fds, void *data, unsigned ch_size)
|
2016-11-02 03:27:46 +03:00
|
|
|
{
|
|
|
|
struct scm_fdset fdset;
|
|
|
|
int *cmsg_data;
|
|
|
|
int i, min_fd, ret;
|
|
|
|
|
2016-11-14 23:01:00 +03:00
|
|
|
cmsg_data = scm_fdset_init(&fdset, saddr, len);
|
2016-11-02 03:27:46 +03:00
|
|
|
for (i = 0; i < nr_fds; i += min_fd) {
|
|
|
|
min_fd = min(CR_SCM_MAX_FD, nr_fds - i);
|
2016-11-22 20:03:00 +03:00
|
|
|
scm_fdset_init_chunk(&fdset, min_fd, data, ch_size);
|
2016-11-02 03:29:07 +03:00
|
|
|
__memcpy(cmsg_data, &fds[i], sizeof(int) * min_fd);
|
2016-11-02 03:27:46 +03:00
|
|
|
|
|
|
|
ret = __sys(sendmsg)(sock, &fdset.hdr, 0);
|
|
|
|
if (ret <= 0)
|
|
|
|
return ret ? : -1;
|
2016-11-22 20:03:00 +03:00
|
|
|
|
|
|
|
if (data)
|
|
|
|
data += min_fd * ch_size;
|
2016-11-02 03:27:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-22 20:03:00 +03:00
|
|
|
int recv_fds(int sock, int *fds, int nr_fds, void *data, unsigned ch_size)
|
2016-11-02 03:27:46 +03:00
|
|
|
{
|
|
|
|
struct scm_fdset fdset;
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
int *cmsg_data;
|
|
|
|
int ret;
|
|
|
|
int i, min_fd;
|
|
|
|
|
2016-11-14 23:01:00 +03:00
|
|
|
cmsg_data = scm_fdset_init(&fdset, NULL, 0);
|
2016-11-02 03:27:46 +03:00
|
|
|
for (i = 0; i < nr_fds; i += min_fd) {
|
|
|
|
min_fd = min(CR_SCM_MAX_FD, nr_fds - i);
|
2016-11-22 20:03:00 +03:00
|
|
|
scm_fdset_init_chunk(&fdset, min_fd, data, ch_size);
|
2016-11-02 03:27:46 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
if (fdset.hdr.msg_flags & MSG_CTRUNC)
|
|
|
|
return -ENFILE;
|
|
|
|
|
|
|
|
min_fd = (cmsg->cmsg_len - sizeof(struct cmsghdr)) / sizeof(int);
|
|
|
|
/*
|
|
|
|
* In case if kernel screwed the recipient, 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.
|
|
|
|
*/
|
|
|
|
BUG_ON(min_fd > CR_SCM_MAX_FD);
|
|
|
|
|
|
|
|
if (unlikely(min_fd <= 0))
|
|
|
|
return -1;
|
2016-11-22 20:03:00 +03:00
|
|
|
|
2016-11-02 03:29:07 +03:00
|
|
|
__memcpy(&fds[i], cmsg_data, sizeof(int) * min_fd);
|
2016-11-22 20:03:00 +03:00
|
|
|
if (data)
|
|
|
|
data += ch_size * min_fd;
|
2016-11-02 03:27:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|