1 /*	$NetBSD: netbsd32_event.c,v 1.12 2014/09/05 05:26:26 matt 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.12 2014/09/05 05:26:26 matt 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/proc.h>
41 #include <sys/dirent.h>
42 
43 #include <compat/netbsd32/netbsd32.h>
44 #include <compat/netbsd32/netbsd32_syscall.h>
45 #include <compat/netbsd32/netbsd32_syscallargs.h>
46 #include <compat/netbsd32/netbsd32_conv.h>
47 
48 static int
netbsd32_kevent_fetch_timeout(const void * src,void * dest,size_t length)49 netbsd32_kevent_fetch_timeout(const void *src, void *dest, size_t length)
50 {
51 	struct netbsd32_timespec ts32;
52 	int error;
53 
54 	KASSERT(length == sizeof(struct timespec));
55 
56 	error = copyin(src, &ts32, sizeof(ts32));
57 	if (error)
58 		return error;
59 	netbsd32_to_timespec(&ts32, (struct timespec *)dest);
60 	return 0;
61 }
62 
63 static int
netbsd32_kevent_fetch_changes(void * ctx,const struct kevent * changelist,struct kevent * changes,size_t index,int n)64 netbsd32_kevent_fetch_changes(void *ctx, const struct kevent *changelist,
65     struct kevent *changes, size_t index, int n)
66 {
67 	const struct netbsd32_kevent *src =
68 	    (const struct netbsd32_kevent *)changelist;
69 	struct netbsd32_kevent *kev32, *changes32 = ctx;
70 	int error, i;
71 
72 	error = copyin(src + index, changes32, n * sizeof(*changes32));
73 	if (error)
74 		return error;
75 	for (i = 0, kev32 = changes32; i < n; i++, kev32++, changes++)
76 		netbsd32_to_kevent(kev32, changes);
77 	return 0;
78 }
79 
80 static int
netbsd32_kevent_put_events(void * ctx,struct kevent * events,struct kevent * eventlist,size_t index,int n)81 netbsd32_kevent_put_events(void *ctx, struct kevent *events,
82     struct kevent *eventlist, size_t index, int n)
83 {
84 	struct netbsd32_kevent *kev32, *events32 = ctx;
85 	int i;
86 
87 	for (i = 0, kev32 = events32; i < n; i++, kev32++, events++)
88 		netbsd32_from_kevent(events, kev32);
89 	kev32 = ((struct netbsd32_kevent *)eventlist) + index;
90 	return  copyout(events32, kev32, n * sizeof(*events32));
91 }
92 
93 int
netbsd32___kevent50(struct lwp * l,const struct netbsd32___kevent50_args * uap,register_t * retval)94 netbsd32___kevent50(struct lwp *l,
95     const struct netbsd32___kevent50_args *uap, register_t *retval)
96 {
97 	/* {
98 		syscallarg(int) fd;
99 		syscallarg(netbsd32_keventp_t) changelist;
100 		syscallarg(netbsd32_size_t) nchanges;
101 		syscallarg(netbsd32_keventp_t) eventlist;
102 		syscallarg(netbsd32_size_t) nevents;
103 		syscallarg(netbsd32_timespecp_t) timeout;
104 	} */
105 	int error;
106 	size_t maxalloc, nchanges, nevents;
107 	struct kevent_ops netbsd32_kevent_ops = {
108 		.keo_fetch_timeout = netbsd32_kevent_fetch_timeout,
109 		.keo_fetch_changes = netbsd32_kevent_fetch_changes,
110 		.keo_put_events = netbsd32_kevent_put_events,
111 	};
112 
113 	nchanges = SCARG(uap, nchanges);
114 	nevents = SCARG(uap, nevents);
115 	maxalloc = KQ_NEVENTS;
116 
117 	netbsd32_kevent_ops.keo_private =
118 	    kmem_alloc(maxalloc * sizeof(struct netbsd32_kevent), KM_SLEEP);
119 
120 	error = kevent1(retval, SCARG(uap, fd),
121 	    NETBSD32PTR64(SCARG(uap, changelist)), nchanges,
122 	    NETBSD32PTR64(SCARG(uap, eventlist)), nevents,
123 	    NETBSD32PTR64(SCARG(uap, timeout)), &netbsd32_kevent_ops);
124 
125 	kmem_free(netbsd32_kevent_ops.keo_private,
126 	    maxalloc * sizeof(struct netbsd32_kevent));
127 	return error;
128 }
129