Home | History | Annotate | Line # | Download | only in netbsd32
      1  1.5  mrg /*	$NetBSD: netbsd32_epoll.c,v 1.5 2023/09/02 21:11:54 mrg Exp $	*/
      2  1.1  rin 
      3  1.1  rin /*-
      4  1.1  rin  * SPDX-License-Identifier: BSD-2-Clause
      5  1.1  rin  *
      6  1.1  rin  * Copyright (c) 2007 Roman Divacky
      7  1.1  rin  * Copyright (c) 2014 Dmitry Chagin <dchagin (at) FreeBSD.org>
      8  1.1  rin  *
      9  1.1  rin  * Redistribution and use in source and binary forms, with or without
     10  1.1  rin  * modification, are permitted provided that the following conditions
     11  1.1  rin  * are met:
     12  1.1  rin  * 1. Redistributions of source code must retain the above copyright
     13  1.1  rin  *    notice, this list of conditions and the following disclaimer.
     14  1.1  rin  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  rin  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  rin  *    documentation and/or other materials provided with the distribution.
     17  1.1  rin  *
     18  1.1  rin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     19  1.1  rin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  1.1  rin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  1.1  rin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  1.1  rin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  1.1  rin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  1.1  rin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.1  rin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  1.1  rin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.1  rin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.1  rin  * SUCH DAMAGE.
     29  1.1  rin  */
     30  1.1  rin #include <sys/cdefs.h>
     31  1.5  mrg __KERNEL_RCSID(0, "$NetBSD: netbsd32_epoll.c,v 1.5 2023/09/02 21:11:54 mrg Exp $");
     32  1.1  rin 
     33  1.1  rin #include <sys/types.h>
     34  1.1  rin #include <sys/epoll.h>
     35  1.1  rin #include <sys/kmem.h>
     36  1.1  rin #include <sys/systm.h>
     37  1.1  rin #include <sys/syscall.h>
     38  1.1  rin #include <sys/syscallargs.h>
     39  1.1  rin 
     40  1.1  rin #include <compat/netbsd32/netbsd32.h>
     41  1.1  rin #include <compat/netbsd32/netbsd32_conv.h>
     42  1.1  rin #include <compat/netbsd32/netbsd32_syscall.h>
     43  1.1  rin #include <compat/netbsd32/netbsd32_syscallargs.h>
     44  1.1  rin 
     45  1.1  rin int
     46  1.1  rin netbsd32_epoll_create1(struct lwp *l,
     47  1.1  rin     const struct netbsd32_epoll_create1_args *uap, register_t *retval)
     48  1.1  rin {
     49  1.1  rin 	/* {
     50  1.1  rin 		syscallarg(int)		flags;
     51  1.1  rin 	} */
     52  1.1  rin 	struct sys_epoll_create1_args ua;
     53  1.1  rin 
     54  1.1  rin 	NETBSD32TO64_UAP(flags);
     55  1.1  rin 	return sys_epoll_create1(l, &ua, retval);
     56  1.1  rin }
     57  1.1  rin 
     58  1.1  rin int
     59  1.1  rin netbsd32_epoll_ctl(struct lwp *l, const struct netbsd32_epoll_ctl_args *uap,
     60  1.1  rin     register_t *retval)
     61  1.1  rin {
     62  1.1  rin 	/* {
     63  1.1  rin 		syscallarg(int) epfd;
     64  1.1  rin 		syscallarg(int) op;
     65  1.1  rin 		syscallarg(int) fd;
     66  1.1  rin 		syscallarg(netbsd32_epoll_eventp_t) event;
     67  1.1  rin 	} */
     68  1.1  rin 	struct epoll_event ee, *eep;
     69  1.1  rin 	int error;
     70  1.1  rin 
     71  1.1  rin 	if (SCARG(uap, op) != EPOLL_CTL_DEL) {
     72  1.1  rin 		struct netbsd32_epoll_event ee32;
     73  1.1  rin 
     74  1.1  rin 		error = copyin(SCARG_P32(uap, event), &ee32, sizeof(ee32));
     75  1.1  rin 		if (error != 0)
     76  1.1  rin 			return error;
     77  1.1  rin 
     78  1.1  rin 		netbsd32_to_epoll_event(&ee32, &ee);
     79  1.1  rin 		eep = &ee;
     80  1.1  rin 	} else
     81  1.1  rin 		eep = NULL;
     82  1.1  rin 
     83  1.1  rin 	return epoll_ctl_common(l, retval, SCARG(uap, epfd), SCARG(uap, op),
     84  1.1  rin 	    SCARG(uap, fd), eep);
     85  1.1  rin }
     86  1.1  rin 
     87  1.1  rin int
     88  1.1  rin netbsd32_epoll_pwait2(struct lwp *l,
     89  1.1  rin     const struct netbsd32_epoll_pwait2_args *uap, register_t *retval)
     90  1.1  rin {
     91  1.1  rin 	/* {
     92  1.1  rin 		syscallarg(int) epfd;
     93  1.1  rin 		syscallarg(netbsd32_epoll_eventp_t) events;
     94  1.1  rin 		syscallarg(int) maxevents;
     95  1.1  rin 		syscallarg(netbsd32_timespecp_t) timeout;
     96  1.1  rin 		syscallarg(netbsd32_sigsetp_t) sigmask;
     97  1.1  rin 	} */
     98  1.1  rin 	struct epoll_event *events;
     99  1.1  rin 	struct timespec ts, *tsp;
    100  1.1  rin 	sigset_t ss, *ssp;
    101  1.1  rin 	int error;
    102  1.1  rin 	const int maxevents = SCARG(uap, maxevents);
    103  1.1  rin 
    104  1.1  rin 	if (maxevents <= 0 || maxevents >= EPOLL_MAX_EVENTS)
    105  1.1  rin 		return EINVAL;
    106  1.1  rin 
    107  1.1  rin 	if (SCARG_P32(uap, timeout) != NULL) {
    108  1.1  rin 		struct netbsd32_timespec ts32;
    109  1.1  rin 
    110  1.1  rin 		error = copyin(SCARG_P32(uap, timeout), &ts32, sizeof(ts32));
    111  1.1  rin 		if (error != 0)
    112  1.1  rin 			return error;
    113  1.1  rin 
    114  1.1  rin 		netbsd32_to_timespec(&ts32, &ts);
    115  1.1  rin 		tsp = &ts;
    116  1.1  rin 	} else
    117  1.1  rin 		tsp = NULL;
    118  1.1  rin 
    119  1.1  rin 	if (SCARG_P32(uap, sigmask) != NULL) {
    120  1.1  rin 		error = copyin(SCARG_P32(uap, sigmask), &ss, sizeof(ss));
    121  1.1  rin 		if (error != 0)
    122  1.1  rin 			return error;
    123  1.1  rin 
    124  1.1  rin 		ssp = &ss;
    125  1.1  rin 	} else
    126  1.1  rin 		ssp = NULL;
    127  1.1  rin 
    128  1.1  rin 	events = kmem_alloc(maxevents * sizeof(*events), KM_SLEEP);
    129  1.1  rin 
    130  1.1  rin 	error = epoll_wait_common(l, retval, SCARG(uap, epfd), events,
    131  1.1  rin 	    maxevents, tsp, ssp);
    132  1.3  rin 	if (error != 0 || *retval == 0)
    133  1.3  rin 		goto out;
    134  1.1  rin 
    135  1.1  rin 	struct netbsd32_epoll_event *events32 =
    136  1.1  rin 	    kmem_alloc(*retval * sizeof(*events32), KM_SLEEP);
    137  1.1  rin 
    138  1.5  mrg 	for (register_t i = 0; i < *retval; i++)
    139  1.1  rin 		netbsd32_from_epoll_event(&events[i], &events32[i]);
    140  1.1  rin 
    141  1.1  rin 	error = copyout(events, SCARG_P32(uap, events),
    142  1.1  rin 	    *retval * sizeof(*events32));
    143  1.1  rin 
    144  1.1  rin 	kmem_free(events32, *retval * sizeof(*events32));
    145  1.1  rin 
    146  1.3  rin  out:
    147  1.3  rin 	kmem_free(events, maxevents * sizeof(*events));
    148  1.1  rin 	return error;
    149  1.1  rin }
    150