From owner-svn-src-all@freebsd.org Wed Aug 12 07:59:01 2015 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 83A6099F98F; Wed, 12 Aug 2015 07:59:01 +0000 (UTC) (envelope-from ed@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 676FDF18; Wed, 12 Aug 2015 07:59:01 +0000 (UTC) (envelope-from ed@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.70]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id t7C7x1YH006915; Wed, 12 Aug 2015 07:59:01 GMT (envelope-from ed@FreeBSD.org) Received: (from ed@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id t7C7x12L006914; Wed, 12 Aug 2015 07:59:01 GMT (envelope-from ed@FreeBSD.org) Message-Id: <201508120759.t7C7x12L006914@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: ed set sender to ed@FreeBSD.org using -f From: Ed Schouten Date: Wed, 12 Aug 2015 07:59:01 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r286654 - head/sys/compat/cloudabi64 X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 12 Aug 2015 07:59:01 -0000 Author: ed Date: Wed Aug 12 07:59:00 2015 New Revision: 286654 URL: https://svnweb.freebsd.org/changeset/base/286654 Log: Make poll() and kqueue() on CloudABI work. This change implements two functions, cloudabi64_kevent_copyin() and cloudabi64_kevent_copyout(), that convert CloudABI structures to FreeBSD's struct kevent. CloudABI uses two structures: subscription_t and event_t. The former is used for input, whereas the latter is used for output. Unlike struct kevent, fields aren't overloaded for multiple purposes or for separate event types. For poll() we call into the newly introduced kern_kevent_anonymous() function that allows us to poll without a file descriptor. This function is not only used by poll(), but also by functions such as sleep() and clock_nanosleep(). Reviewed by: jmg Obtained from: https://github.com/NuxiNL/freebsd Differential Revision: https://reviews.freebsd.org/D3308 Modified: head/sys/compat/cloudabi64/cloudabi64_poll.c Modified: head/sys/compat/cloudabi64/cloudabi64_poll.c ============================================================================== --- head/sys/compat/cloudabi64/cloudabi64_poll.c Wed Aug 12 04:03:04 2015 (r286653) +++ head/sys/compat/cloudabi64/cloudabi64_poll.c Wed Aug 12 07:59:00 2015 (r286654) @@ -26,22 +26,260 @@ #include __FBSDID("$FreeBSD$"); +#include +#include +#include + +#include + #include #include +/* Converts a FreeBSD signal number to a CloudABI signal number. */ +static cloudabi_signal_t +convert_signal(int sig) +{ + static const cloudabi_signal_t signals[] = { + [SIGABRT] = CLOUDABI_SIGABRT, + [SIGALRM] = CLOUDABI_SIGALRM, + [SIGBUS] = CLOUDABI_SIGBUS, + [SIGCHLD] = CLOUDABI_SIGCHLD, + [SIGCONT] = CLOUDABI_SIGCONT, + [SIGFPE] = CLOUDABI_SIGFPE, + [SIGHUP] = CLOUDABI_SIGHUP, + [SIGILL] = CLOUDABI_SIGILL, + [SIGINT] = CLOUDABI_SIGINT, + [SIGKILL] = CLOUDABI_SIGKILL, + [SIGPIPE] = CLOUDABI_SIGPIPE, + [SIGQUIT] = CLOUDABI_SIGQUIT, + [SIGSEGV] = CLOUDABI_SIGSEGV, + [SIGSTOP] = CLOUDABI_SIGSTOP, + [SIGSYS] = CLOUDABI_SIGSYS, + [SIGTERM] = CLOUDABI_SIGTERM, + [SIGTRAP] = CLOUDABI_SIGTRAP, + [SIGTSTP] = CLOUDABI_SIGTSTP, + [SIGTTIN] = CLOUDABI_SIGTTIN, + [SIGTTOU] = CLOUDABI_SIGTTOU, + [SIGURG] = CLOUDABI_SIGURG, + [SIGUSR1] = CLOUDABI_SIGUSR1, + [SIGUSR2] = CLOUDABI_SIGUSR2, + [SIGVTALRM] = CLOUDABI_SIGVTALRM, + [SIGXCPU] = CLOUDABI_SIGXCPU, + [SIGXFSZ] = CLOUDABI_SIGXFSZ, + }; + + /* Convert unknown signals to SIGABRT. */ + if (sig < 0 || sig >= nitems(signals) || signals[sig] == 0) + return (SIGABRT); + return (signals[sig]); +} + +struct cloudabi64_kevent_args { + const cloudabi64_subscription_t *in; + cloudabi64_event_t *out; + bool once; +}; + +/* Converts CloudABI's subscription objects to FreeBSD's struct kevent. */ +static int +cloudabi64_kevent_copyin(void *arg, struct kevent *kevp, int count) +{ + cloudabi64_subscription_t sub; + struct cloudabi64_kevent_args *args; + cloudabi_timestamp_t ts; + int error; + + args = arg; + while (count-- > 0) { + /* TODO(ed): Copy in multiple entries at once. */ + error = copyin(args->in++, &sub, sizeof(sub)); + if (error != 0) + return (error); + + memset(kevp, 0, sizeof(*kevp)); + kevp->udata = (void *)sub.userdata; + switch (sub.type) { + case CLOUDABI_EVENTTYPE_CLOCK: + kevp->filter = EVFILT_TIMER; + kevp->ident = sub.clock.identifier; + kevp->fflags = NOTE_NSECONDS; + if ((sub.clock.flags & + CLOUDABI_SUBSCRIPTION_CLOCK_ABSTIME) != 0 && + sub.clock.timeout > 0) { + /* Convert absolute timestamp to a relative. */ + error = cloudabi_clock_time_get(curthread, + sub.clock.clock_id, &ts); + if (error != 0) + return (error); + ts = ts > sub.clock.timeout ? 0 : + sub.clock.timeout - ts; + } else { + /* Relative timestamp. */ + ts = sub.clock.timeout; + } + kevp->data = ts > INTPTR_MAX ? INTPTR_MAX : ts; + break; + case CLOUDABI_EVENTTYPE_FD_READ: + kevp->filter = EVFILT_READ; + kevp->ident = sub.fd_readwrite.fd; + if ((sub.fd_readwrite.flags & + CLOUDABI_SUBSCRIPTION_FD_READWRITE_POLL) != 0) + kevp->fflags = NOTE_FILE_POLL; + break; + case CLOUDABI_EVENTTYPE_FD_WRITE: + kevp->filter = EVFILT_WRITE; + kevp->ident = sub.fd_readwrite.fd; + break; + case CLOUDABI_EVENTTYPE_PROC_TERMINATE: + kevp->filter = EVFILT_PROCDESC; + kevp->ident = sub.proc_terminate.fd; + kevp->fflags = NOTE_EXIT; + break; + } + if (args->once) { + /* Ignore flags. Simply use oneshot mode. */ + kevp->flags = EV_ADD | EV_ONESHOT; + } else { + /* Translate flags. */ + if ((sub.flags & CLOUDABI_SUBSCRIPTION_ADD) != 0) + kevp->flags |= EV_ADD; + if ((sub.flags & CLOUDABI_SUBSCRIPTION_CLEAR) != 0) + kevp->flags |= EV_CLEAR; + if ((sub.flags & CLOUDABI_SUBSCRIPTION_DELETE) != 0) + kevp->flags |= EV_DELETE; + if ((sub.flags & CLOUDABI_SUBSCRIPTION_DISABLE) != 0) + kevp->flags |= EV_DISABLE; + if ((sub.flags & CLOUDABI_SUBSCRIPTION_ENABLE) != 0) + kevp->flags |= EV_ENABLE; + if ((sub.flags & CLOUDABI_SUBSCRIPTION_ONESHOT) != 0) + kevp->flags |= EV_ONESHOT; + } + ++kevp; + } + return (0); +} + +/* Converts FreeBSD's struct kevent to CloudABI's event objects. */ +static int +cloudabi64_kevent_copyout(void *arg, struct kevent *kevp, int count) +{ + cloudabi64_event_t ev; + struct cloudabi64_kevent_args *args; + int error; + + args = arg; + while (count-- > 0) { + /* Convert fields that should always be present. */ + memset(&ev, 0, sizeof(ev)); + ev.userdata = (uintptr_t)kevp->udata; + switch (kevp->filter) { + case EVFILT_TIMER: + ev.type = CLOUDABI_EVENTTYPE_CLOCK; + ev.clock.identifier = kevp->ident; + break; + case EVFILT_READ: + ev.type = CLOUDABI_EVENTTYPE_FD_READ; + ev.fd_readwrite.fd = kevp->ident; + break; + case EVFILT_WRITE: + ev.type = CLOUDABI_EVENTTYPE_FD_WRITE; + ev.fd_readwrite.fd = kevp->ident; + break; + case EVFILT_PROCDESC: + ev.type = CLOUDABI_EVENTTYPE_PROC_TERMINATE; + ev.proc_terminate.fd = kevp->ident; + break; + } + + if ((kevp->flags & EV_ERROR) == 0) { + /* Success. */ + switch (kevp->filter) { + case EVFILT_READ: + case EVFILT_WRITE: + ev.fd_readwrite.nbytes = kevp->data; + if ((kevp->flags & EV_EOF) != 0) { + ev.fd_readwrite.flags |= + CLOUDABI_EVENT_FD_READWRITE_HANGUP; + } + break; + case EVFILT_PROCDESC: + if (WIFSIGNALED(kevp->data)) { + /* Process got signalled. */ + ev.proc_terminate.signal = + convert_signal(WTERMSIG(kevp->data)); + ev.proc_terminate.exitcode = 0; + } else { + /* Process exited. */ + ev.proc_terminate.signal = 0; + ev.proc_terminate.exitcode = + WEXITSTATUS(kevp->data); + } + break; + } + } else { + /* Error. */ + ev.error = cloudabi_convert_errno(kevp->data); + } + ++kevp; + + /* TODO(ed): Copy out multiple entries at once. */ + error = copyout(&ev, args->out++, sizeof(ev)); + if (error != 0) + return (error); + } + return (0); +} + int cloudabi64_sys_poll(struct thread *td, struct cloudabi64_sys_poll_args *uap) { + struct cloudabi64_kevent_args args = { + .in = uap->in, + .out = uap->out, + .once = true, + }; + struct kevent_copyops copyops = { + .k_copyin = cloudabi64_kevent_copyin, + .k_copyout = cloudabi64_kevent_copyout, + .arg = &args, + }; - /* Not implemented. */ - return (ENOSYS); + return (kern_kevent_anonymous(td, uap->nevents, ©ops)); } int cloudabi64_sys_poll_fd(struct thread *td, struct cloudabi64_sys_poll_fd_args *uap) { + struct cloudabi64_kevent_args args = { + .in = uap->in, + .out = uap->out, + .once = false, + }; + struct kevent_copyops copyops = { + .k_copyin = cloudabi64_kevent_copyin, + .k_copyout = cloudabi64_kevent_copyout, + .arg = &args, + }; + cloudabi64_subscription_t subtimo; + struct timespec timeout; + int error; - /* Not implemented. */ - return (ENOSYS); + if (uap->timeout != NULL) { + /* Poll with a timeout. */ + error = copyin(uap->timeout, &subtimo, sizeof(subtimo)); + if (error != 0) + return (error); + if (subtimo.type != CLOUDABI_EVENTTYPE_CLOCK || + subtimo.clock.flags != 0) + return (EINVAL); + timeout.tv_sec = subtimo.clock.timeout / 1000000000; + timeout.tv_nsec = subtimo.clock.timeout % 1000000000; + return (kern_kevent(td, uap->fd, uap->nin, uap->nout, ©ops, + &timeout)); + } else { + /* Poll without a timeout. */ + return (kern_kevent(td, uap->fd, uap->nin, uap->nout, ©ops, + NULL)); + } }