xref: /netbsd-src/sys/compat/common/kern_select_50.c (revision 53d1339bf7f9c7367b35a9e1ebe693f9b047a47b)
1 /*	$NetBSD: kern_select_50.c,v 1.3 2019/09/20 15:05:22 kamil Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
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 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: kern_select_50.c,v 1.3 2019/09/20 15:05:22 kamil Exp $");
33 
34 #if defined(_KERNEL_OPT)
35 #include "opt_compat_netbsd.h"
36 #endif
37 
38 #include <sys/param.h>
39 #include <sys/event.h>
40 #include <sys/poll.h>
41 #include <sys/select.h>
42 #include <sys/time.h>
43 #include <sys/syscall.h>
44 #include <sys/syscallvar.h>
45 #include <sys/syscallargs.h>
46 
47 #include <compat/sys/time.h>
48 #include <compat/common/compat_mod.h>
49 
50 static const struct syscall_package kern_select_50_syscalls[] = {
51 	{ SYS_compat_50_kevent, 0, (sy_call_t *)compat_50_sys_kevent },
52 	{ SYS_compat_50_select, 0, (sy_call_t *)compat_50_sys_select },
53 	{ SYS_compat_50_pselect, 0, (sy_call_t *)compat_50_sys_pselect },
54 	{ SYS_compat_50_pollts, 0, (sy_call_t *)compat_50_sys_pollts },
55 	{ 0, 0, NULL }
56 };
57 
58 static int
59 compat_50_kevent_fetch_timeout(const void *src, void *dest, size_t length)
60 {
61 	struct timespec50 ts50;
62 	int error;
63 
64 	KASSERT(length == sizeof(struct timespec));
65 
66 	error = copyin(src, &ts50, sizeof(ts50));
67 	if (error)
68 		return error;
69 	timespec50_to_timespec(&ts50, (struct timespec *)dest);
70 	return 0;
71 }
72 
73 int
74 compat_50_sys_kevent(struct lwp *l, const struct compat_50_sys_kevent_args *uap,
75     register_t *retval)
76 {
77 	/* {
78 		syscallarg(int) fd;
79 		syscallarg(keventp_t) changelist;
80 		syscallarg(size_t) nchanges;
81 		syscallarg(keventp_t) eventlist;
82 		syscallarg(size_t) nevents;
83 		syscallarg(struct timespec50) timeout;
84 	} */
85 	static const struct kevent_ops compat_50_kevent_ops = {
86 		.keo_private = NULL,
87 		.keo_fetch_timeout = compat_50_kevent_fetch_timeout,
88 		.keo_fetch_changes = kevent_fetch_changes,
89 		.keo_put_events = kevent_put_events,
90 	};
91 
92 	return kevent1(retval, SCARG(uap, fd), SCARG(uap, changelist),
93 	    SCARG(uap, nchanges), SCARG(uap, eventlist), SCARG(uap, nevents),
94 	    (const struct timespec *)(const void *)SCARG(uap, timeout),
95 	    &compat_50_kevent_ops);
96 }
97 
98 int
99 compat_50_sys_select(struct lwp *l,
100     const struct compat_50_sys_select_args *uap, register_t *retval)
101 {
102 	/* {
103 		syscallarg(int)			nd;
104 		syscallarg(fd_set *)		in;
105 		syscallarg(fd_set *)		ou;
106 		syscallarg(fd_set *)		ex;
107 		syscallarg(struct timeval50 *)	tv;
108 	} */
109 	struct timespec ats, *ts = NULL;
110 	struct timeval50 atv50;
111 	int error;
112 
113 	if (SCARG(uap, tv)) {
114 		error = copyin(SCARG(uap, tv), (void *)&atv50, sizeof(atv50));
115 		if (error)
116 			return error;
117 
118 		if (atv50.tv_usec < 0 || atv50.tv_usec >= 1000000)
119 			return EINVAL;
120 
121 		ats.tv_sec = atv50.tv_sec;
122 		ats.tv_nsec = atv50.tv_usec * 1000;
123 		ts = &ats;
124 	}
125 
126 	return selcommon(retval, SCARG(uap, nd), SCARG(uap, in),
127 	    SCARG(uap, ou), SCARG(uap, ex), ts, NULL);
128 }
129 
130 int
131 compat_50_sys_pselect(struct lwp *l,
132     const struct compat_50_sys_pselect_args *uap, register_t *retval)
133 {
134 	/* {
135 		syscallarg(int)				nd;
136 		syscallarg(fd_set *)			in;
137 		syscallarg(fd_set *)			ou;
138 		syscallarg(fd_set *)			ex;
139 		syscallarg(const struct timespec50 *)	ts;
140 		syscallarg(sigset_t *)			mask;
141 	} */
142 	struct timespec50	ats50;
143 	struct timespec	ats, *ts = NULL;
144 	sigset_t	amask, *mask = NULL;
145 	int		error;
146 
147 	if (SCARG(uap, ts)) {
148 		error = copyin(SCARG(uap, ts), &ats50, sizeof(ats50));
149 		if (error)
150 			return error;
151 		timespec50_to_timespec(&ats50, &ats);
152 		ts = &ats;
153 	}
154 	if (SCARG(uap, mask) != NULL) {
155 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
156 		if (error)
157 			return error;
158 		mask = &amask;
159 	}
160 
161 	return selcommon(retval, SCARG(uap, nd), SCARG(uap, in),
162 	    SCARG(uap, ou), SCARG(uap, ex), ts, mask);
163 }
164 
165 int
166 compat_50_sys_pollts(struct lwp *l, const struct compat_50_sys_pollts_args *uap,
167     register_t *retval)
168 {
169 	/* {
170 		syscallarg(struct pollfd *)		fds;
171 		syscallarg(u_int)			nfds;
172 		syscallarg(const struct timespec50 *)	ts;
173 		syscallarg(const sigset_t *)		mask;
174 	} */
175 	struct timespec	ats, *ts = NULL;
176 	struct timespec50 ats50;
177 	sigset_t	amask, *mask = NULL;
178 	int		error;
179 
180 	if (SCARG(uap, ts)) {
181 		error = copyin(SCARG(uap, ts), &ats50, sizeof(ats50));
182 		if (error)
183 			return error;
184 		timespec50_to_timespec(&ats50, &ats);
185 		ts = &ats;
186 	}
187 	if (SCARG(uap, mask)) {
188 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
189 		if (error)
190 			return error;
191 		mask = &amask;
192 	}
193 
194 	return pollcommon(retval, SCARG(uap, fds), SCARG(uap, nfds), ts, mask);
195 }
196 
197 int
198 kern_select_50_init(void)
199 {
200 
201 return syscall_establish(NULL, kern_select_50_syscalls);
202 }
203 
204 int
205 kern_select_50_fini(void)
206 {
207 
208 return syscall_disestablish(NULL, kern_select_50_syscalls);
209 }
210