1 /* $NetBSD: netbsd32_event.c,v 1.7 2009/01/11 02:45:49 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2005 The NetBSD Foundation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: netbsd32_event.c,v 1.7 2009/01/11 02:45:49 christos Exp $"); 31 32 #include <sys/types.h> 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/file.h> 36 #include <sys/filedesc.h> 37 #include <sys/select.h> 38 #include <sys/event.h> 39 #include <sys/eventvar.h> 40 #include <sys/malloc.h> 41 #include <sys/proc.h> 42 #include <sys/dirent.h> 43 44 #include <compat/netbsd32/netbsd32.h> 45 #include <compat/netbsd32/netbsd32_syscall.h> 46 #include <compat/netbsd32/netbsd32_syscallargs.h> 47 #include <compat/netbsd32/netbsd32_conv.h> 48 49 static int 50 netbsd32_kevent_fetch_timeout(const void *src, void *dest, size_t length) 51 { 52 struct netbsd32_timespec ts32; 53 int error; 54 55 KASSERT(length == sizeof(struct timespec)); 56 57 error = copyin(src, &ts32, sizeof(ts32)); 58 if (error) 59 return error; 60 netbsd32_to_timespec(&ts32, (struct timespec *)dest); 61 return 0; 62 } 63 64 static int 65 netbsd32_kevent_fetch_changes(void *private, const struct kevent *changelist, 66 struct kevent *changes, size_t index, int n) 67 { 68 const struct netbsd32_kevent *src = 69 (const struct netbsd32_kevent *)changelist; 70 struct netbsd32_kevent *kev32, *changes32 = private; 71 int error, i; 72 73 error = copyin(src + index, changes32, n * sizeof(*changes32)); 74 if (error) 75 return error; 76 for (i = 0, kev32 = changes32; i < n; i++, kev32++, changes++) 77 netbsd32_to_kevent(kev32, changes); 78 return 0; 79 } 80 81 static int 82 netbsd32_kevent_put_events(void *private, struct kevent *events, 83 struct kevent *eventlist, size_t index, int n) 84 { 85 struct netbsd32_kevent *kev32, *events32 = private; 86 int i; 87 88 for (i = 0, kev32 = events32; i < n; i++, kev32++, events++) 89 netbsd32_from_kevent(events, kev32); 90 kev32 = (struct netbsd32_kevent *)eventlist; 91 return copyout(events32, kev32, n * sizeof(*events32)); 92 } 93 94 int 95 netbsd32___kevent50(struct lwp *l, 96 const struct netbsd32___kevent50_args *uap, register_t *retval) 97 { 98 /* { 99 syscallarg(int) fd; 100 syscallarg(netbsd32_keventp_t) changelist; 101 syscallarg(netbsd32_size_t) nchanges; 102 syscallarg(netbsd32_keventp_t) eventlist; 103 syscallarg(netbsd32_size_t) nevents; 104 syscallarg(netbsd32_timespecp_t) timeout; 105 } */ 106 int error; 107 size_t maxalloc, nchanges, nevents; 108 struct kevent_ops netbsd32_kevent_ops = { 109 keo_fetch_timeout: netbsd32_kevent_fetch_timeout, 110 keo_fetch_changes: netbsd32_kevent_fetch_changes, 111 keo_put_events: netbsd32_kevent_put_events, 112 }; 113 114 nchanges = SCARG(uap, nchanges); 115 nevents = SCARG(uap, nevents); 116 maxalloc = MIN(KQ_NEVENTS, MAX(nchanges, nevents)); 117 netbsd32_kevent_ops.keo_private = 118 malloc(maxalloc * sizeof(struct netbsd32_kevent), M_TEMP, 119 M_WAITOK); 120 121 error = kevent1(retval, SCARG(uap, fd), 122 NETBSD32PTR64(SCARG(uap, changelist)), nchanges, 123 NETBSD32PTR64(SCARG(uap, eventlist)), nevents, 124 NETBSD32PTR64(SCARG(uap, timeout)), &netbsd32_kevent_ops); 125 126 free(netbsd32_kevent_ops.keo_private, M_TEMP); 127 return error; 128 } 129