From fbb624e76fde53c42737332510f37ee5a15b3069 Mon Sep 17 00:00:00 2001 From: Ed Schouten Date: Wed, 12 Aug 2015 17:42:20 +0000 Subject: [PATCH] Add the last remaining system calls: send() and recv(). There is still one TODO item for these calls: add file descriptor passing. The data structures are already prepared for this. It's just the translation that's missing. Obtained from: http://github.com/NuxiNL/freebsd --- sys/compat/cloudabi64/cloudabi64_sock.c | 104 +++++++++++++++++++++++- 1 file changed, 100 insertions(+), 4 deletions(-) diff --git a/sys/compat/cloudabi64/cloudabi64_sock.c b/sys/compat/cloudabi64/cloudabi64_sock.c index be359899994..c612a32dfc7 100644 --- a/sys/compat/cloudabi64/cloudabi64_sock.c +++ b/sys/compat/cloudabi64/cloudabi64_sock.c @@ -26,23 +26,119 @@ #include __FBSDID("$FreeBSD$"); +#include +#include +#include +#include +#include +#include +#include +#include + +#include + #include #include +static MALLOC_DEFINE(M_SOCKET, "socket", "CloudABI socket"); + int cloudabi64_sys_sock_recv(struct thread *td, struct cloudabi64_sys_sock_recv_args *uap) { + struct sockaddr_storage ss; + cloudabi64_recv_in_t ri; + cloudabi64_recv_out_t ro = {}; + cloudabi64_iovec_t iovobj; + struct msghdr msghdr = {}; + size_t i; + int error; + + error = copyin(uap->in, &ri, sizeof(ri)); + if (error != 0) + return (error); + + /* Convert results in cloudabi_recv_in_t to struct msghdr. */ + if (ri.ri_datalen > UIO_MAXIOV) + return (EINVAL); + msghdr.msg_iovlen = ri.ri_datalen; + msghdr.msg_iov = malloc(msghdr.msg_iovlen * sizeof(struct iovec), + M_SOCKET, M_WAITOK); + for (i = 0; i < msghdr.msg_iovlen; i++) { + error = copyin(&((cloudabi64_iovec_t *)ri.ri_data)[i], &iovobj, + sizeof(iovobj)); + if (error != 0) { + free(msghdr.msg_iov, M_SOCKET); + return (error); + } + msghdr.msg_iov[i].iov_base = (void *)iovobj.iov_base; + msghdr.msg_iov[i].iov_len = iovobj.iov_len; + } + msghdr.msg_name = &ss; + msghdr.msg_namelen = sizeof(ss); + if (ri.ri_flags & CLOUDABI_MSG_PEEK) + msghdr.msg_flags |= MSG_PEEK; + if (ri.ri_flags & CLOUDABI_MSG_WAITALL) + msghdr.msg_flags |= MSG_WAITALL; - /* Not implemented. */ - return (ENOSYS); + /* TODO(ed): Add file descriptor passing. */ + error = kern_recvit(td, uap->s, &msghdr, UIO_SYSSPACE, NULL); + free(msghdr.msg_iov, M_SOCKET); + if (error != 0) + return (error); + + /* Convert results in msghdr to cloudabi_recv_out_t. */ + ro.ro_datalen = td->td_retval[0]; + cloudabi_convert_sockaddr((struct sockaddr *)&ss, + MIN(msghdr.msg_namelen, sizeof(ss)), &ro.ro_peername); + td->td_retval[0] = 0; + return (copyout(&ro, uap->out, sizeof(ro))); } int cloudabi64_sys_sock_send(struct thread *td, struct cloudabi64_sys_sock_send_args *uap) { + cloudabi64_send_in_t si; + cloudabi64_send_out_t so = {}; + cloudabi64_ciovec_t iovobj; + struct msghdr msghdr = {}; + size_t i; + int error, flags; + + error = copyin(uap->in, &si, sizeof(si)); + if (error != 0) + return (error); + + /* Convert results in cloudabi_send_in_t to struct msghdr. */ + if (si.si_datalen > UIO_MAXIOV) + return (EINVAL); + msghdr.msg_iovlen = si.si_datalen; + msghdr.msg_iov = malloc(msghdr.msg_iovlen * sizeof(struct iovec), + M_SOCKET, M_WAITOK); + for (i = 0; i < msghdr.msg_iovlen; i++) { + error = copyin(&((cloudabi64_ciovec_t *)si.si_data)[i], &iovobj, + sizeof(iovobj)); + if (error != 0) { + free(msghdr.msg_iov, M_SOCKET); + return (error); + } + msghdr.msg_iov[i].iov_base = (void *)iovobj.iov_base; + msghdr.msg_iov[i].iov_len = iovobj.iov_len; + } + + flags = MSG_NOSIGNAL; + if (si.si_flags & CLOUDABI_MSG_EOR) + flags |= MSG_EOR; + + /* TODO(ed): Add file descriptor passing. */ + error = kern_sendit(td, uap->s, &msghdr, flags, NULL, UIO_USERSPACE); + free(msghdr.msg_iov, M_SOCKET); + if (error != 0) + return (error); - /* Not implemented. */ - return (ENOSYS); + /* Convert results in msghdr to cloudabi_send_out_t. */ + so.so_datalen = td->td_retval[0]; + td->td_retval[0] = 0; + return (copyout(&so, uap->out, sizeof(so))); } -- 2.45.2