aboutsummaryrefslogtreecommitdiff
path: root/sys/compat/cloudabi
diff options
context:
space:
mode:
authorEd Schouten <ed@FreeBSD.org>2017-03-22 06:43:10 +0000
committerEd Schouten <ed@FreeBSD.org>2017-03-22 06:43:10 +0000
commit36cc183884417d190d501a09e16ed9f0facc9473 (patch)
tree483fff26d1096d75c9f547391571beeb7607de80 /sys/compat/cloudabi
parent8b69c3e79f44d0bafd3a526ae04f05060ea308bf (diff)
downloadsrc-36cc183884417d190d501a09e16ed9f0facc9473.tar.gz
src-36cc183884417d190d501a09e16ed9f0facc9473.zip
Make file descriptor passing work for CloudABI's sendmsg().
Reduce the potential amount of code duplication between cloudabi32 and cloudabi64 by creating a cloudabi_sock_recv() utility function. The cloudabi32 and cloudabi64 modules will then only contain code to convert the iovecs to the native pointer size. In cloudabi_sock_recv(), we can now construct an SCM_RIGHTS cmsghdr in an mbuf and pass that on to kern_sendit().
Notes
Notes: svn path=/head/; revision=315700
Diffstat (limited to 'sys/compat/cloudabi')
-rw-r--r--sys/compat/cloudabi/cloudabi_sock.c50
-rw-r--r--sys/compat/cloudabi/cloudabi_util.h4
2 files changed, 54 insertions, 0 deletions
diff --git a/sys/compat/cloudabi/cloudabi_sock.c b/sys/compat/cloudabi/cloudabi_sock.c
index d4d853a162f4..64a72492b3b7 100644
--- a/sys/compat/cloudabi/cloudabi_sock.c
+++ b/sys/compat/cloudabi/cloudabi_sock.c
@@ -30,7 +30,9 @@ __FBSDID("$FreeBSD$");
#include <sys/capsicum.h>
#include <sys/lock.h>
#include <sys/malloc.h>
+#include <sys/mbuf.h>
#include <sys/mutex.h>
+#include <sys/proc.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
@@ -243,3 +245,51 @@ cloudabi_sys_sock_stat_get(struct thread *td,
fdrop(fp, td);
return (copyout(&ss, uap->buf, sizeof(ss)));
}
+
+int
+cloudabi_sock_send(struct thread *td, cloudabi_fd_t fd, struct iovec *data,
+ size_t datalen, const cloudabi_fd_t *fds, size_t fdslen,
+ cloudabi_msgflags_t flags, size_t *rdatalen)
+{
+ struct msghdr hdr = {
+ .msg_iov = data,
+ .msg_iovlen = datalen,
+ };
+ struct mbuf *control;
+ int error, mflags;
+
+ /* Convert flags. */
+ mflags = MSG_NOSIGNAL;
+ if (flags & CLOUDABI_MSG_EOR)
+ mflags |= MSG_EOR;
+
+ /* Convert file descriptor array to an SCM_RIGHTS message. */
+ if (fdslen > MCLBYTES || CMSG_SPACE(fdslen * sizeof(int)) > MCLBYTES) {
+ return (EINVAL);
+ } else if (fdslen > 0) {
+ struct cmsghdr *chdr;
+
+ control = m_get2(CMSG_SPACE(fdslen * sizeof(int)),
+ M_WAITOK, MT_CONTROL, 0);
+ control->m_len = CMSG_SPACE(fdslen * sizeof(int));
+
+ chdr = mtod(control, struct cmsghdr *);
+ chdr->cmsg_len = CMSG_LEN(fdslen * sizeof(int));
+ chdr->cmsg_level = SOL_SOCKET;
+ chdr->cmsg_type = SCM_RIGHTS;
+ error = copyin(fds, CMSG_DATA(chdr), fdslen * sizeof(int));
+ if (error != 0) {
+ m_free(control);
+ return (error);
+ }
+ } else {
+ control = NULL;
+ }
+
+ error = kern_sendit(td, fd, &hdr, mflags, control, UIO_USERSPACE);
+ if (error != 0)
+ return (error);
+ *rdatalen = td->td_retval[0];
+ td->td_retval[0] = 0;
+ return (0);
+}
diff --git a/sys/compat/cloudabi/cloudabi_util.h b/sys/compat/cloudabi/cloudabi_util.h
index 6eb65aa87b32..0dca3dc30592 100644
--- a/sys/compat/cloudabi/cloudabi_util.h
+++ b/sys/compat/cloudabi/cloudabi_util.h
@@ -77,6 +77,10 @@ int cloudabi_futex_lock_wrlock(struct thread *, cloudabi_lock_t *,
cloudabi_scope_t, cloudabi_clockid_t, cloudabi_timestamp_t,
cloudabi_timestamp_t);
+/* Socket operations. */
+int cloudabi_sock_send(struct thread *, cloudabi_fd_t, struct iovec *, size_t,
+ const cloudabi_fd_t *, size_t, cloudabi_msgflags_t, size_t *);
+
/* vDSO setup and teardown. */
void cloudabi_vdso_init(struct sysentvec *, char *, char *);
void cloudabi_vdso_destroy(struct sysentvec *);