xref: /netbsd-src/sys/compat/netbsd32/netbsd32_event.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: netbsd32_event.c,v 1.3 2007/09/10 10:35:53 cube 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  *  3. Neither the name of The NetBSD Foundation nor the names of its
16  *     contributors may be used to endorse or promote products derived
17  *     from this software without specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  *  POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: netbsd32_event.c,v 1.3 2007/09/10 10:35:53 cube Exp $");
34 
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/file.h>
39 #include <sys/filedesc.h>
40 #include <sys/select.h>
41 #include <sys/event.h>
42 #include <sys/eventvar.h>
43 #include <sys/malloc.h>
44 #include <sys/proc.h>
45 #include <sys/dirent.h>
46 
47 #include <compat/netbsd32/netbsd32.h>
48 #include <compat/netbsd32/netbsd32_syscall.h>
49 #include <compat/netbsd32/netbsd32_syscallargs.h>
50 #include <compat/netbsd32/netbsd32_conv.h>
51 
52 static int
53 netbsd32_kevent_fetch_timeout(const void *src, void *dest, size_t length)
54 {
55 	struct netbsd32_timespec ts32;
56 	int error;
57 
58 	KASSERT(length == sizeof(struct timespec));
59 
60 	error = copyin(src, &ts32, sizeof(ts32));
61 	if (error)
62 		return error;
63 	netbsd32_to_timespec(&ts32, (struct timespec *)dest);
64 	return 0;
65 }
66 
67 static int
68 netbsd32_kevent_fetch_changes(void *private, const struct kevent *changelist,
69     struct kevent *changes, size_t index, int n)
70 {
71 	const struct netbsd32_kevent *src =
72 	    (const struct netbsd32_kevent *)changelist;
73 	struct netbsd32_kevent *kev32, *changes32 = private;
74 	int error, i;
75 
76 	error = copyin(src + index, changes32, n * sizeof(*changes32));
77 	if (error)
78 		return error;
79 	for (i = 0, kev32 = changes32; i < n; i++, kev32++, changes++)
80 		netbsd32_to_kevent(kev32, changes);
81 	return 0;
82 }
83 
84 static int
85 netbsd32_kevent_put_events(void *private, struct kevent *events,
86     struct kevent *eventlist, size_t index, int n)
87 {
88 	struct netbsd32_kevent *kev32, *events32 = private;
89 	int i;
90 
91 	for (i = 0, kev32 = events32; i < n; i++, kev32++, events++)
92 		netbsd32_from_kevent(events, kev32);
93 	kev32 = (struct netbsd32_kevent *)eventlist;
94 	return  copyout(events32, kev32, n * sizeof(*events32));
95 }
96 
97 int
98 netbsd32_kevent(struct lwp *l, void *v, register_t *retval)
99 {
100 	struct netbsd32_kevent_args /* {
101 		syscallarg(int) fd;
102 		syscallarg(netbsd32_keventp_t) changelist;
103 		syscallarg(netbsd32_size_t) nchanges;
104 		syscallarg(netbsd32_keventp_t) eventlist;
105 		syscallarg(netbsd32_size_t) nevents;
106 		syscallarg(netbsd32_timespecp_t) timeout;
107 	} */ *uap = v;
108 	int error;
109 	size_t maxalloc, nchanges, nevents;
110 	struct kevent_ops netbsd32_kevent_ops = {
111 		keo_fetch_timeout: netbsd32_kevent_fetch_timeout,
112 		keo_fetch_changes: netbsd32_kevent_fetch_changes,
113 		keo_put_events: netbsd32_kevent_put_events,
114 	};
115 
116 	nchanges = SCARG(uap, nchanges);
117 	nevents = SCARG(uap, nevents);
118 	maxalloc = MIN(KQ_NEVENTS, MAX(nchanges, nevents));
119 	netbsd32_kevent_ops.keo_private =
120 	    malloc(maxalloc * sizeof(struct netbsd32_kevent), M_TEMP,
121 	    M_WAITOK);
122 
123 	error = kevent1(l, retval, SCARG(uap, fd),
124 	    NETBSD32PTR64(SCARG(uap, changelist)), nchanges,
125 	    NETBSD32PTR64(SCARG(uap, eventlist)), nevents,
126 	    NETBSD32PTR64(SCARG(uap, timeout)), &netbsd32_kevent_ops);
127 
128 	free(netbsd32_kevent_ops.keo_private, M_TEMP);
129 	return error;
130 }
131