xref: /netbsd-src/sys/compat/common/kern_select_50.c (revision d11110f47395fad20b98cd0acd8c15e342942014)
1 /*	$NetBSD: kern_select_50.c,v 1.4 2023/07/28 18:19:00 christos 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.4 2023/07/28 18:19:00 christos 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/event.h>
48 #include <compat/sys/time.h>
49 #include <compat/common/compat_mod.h>
50 
51 static const struct syscall_package kern_select_50_syscalls[] = {
52 	{ SYS_compat_50_kevent, 0, (sy_call_t *)compat_50_sys_kevent },
53 	{ SYS_compat_50_select, 0, (sy_call_t *)compat_50_sys_select },
54 	{ SYS_compat_50_pselect, 0, (sy_call_t *)compat_50_sys_pselect },
55 	{ SYS_compat_50_pollts, 0, (sy_call_t *)compat_50_sys_pollts },
56 	{ 0, 0, NULL }
57 };
58 
59 static int
compat_50_kevent_fetch_timeout(const void * src,void * dest,size_t length)60 compat_50_kevent_fetch_timeout(const void *src, void *dest, size_t length)
61 {
62 	struct timespec50 ts50;
63 	int error;
64 
65 	KASSERT(length == sizeof(struct timespec));
66 
67 	error = copyin(src, &ts50, sizeof(ts50));
68 	if (error)
69 		return error;
70 	timespec50_to_timespec(&ts50, (struct timespec *)dest);
71 	return 0;
72 }
73 
74 int
compat_50_sys_kevent(struct lwp * l,const struct compat_50_sys_kevent_args * uap,register_t * retval)75 compat_50_sys_kevent(struct lwp *l, const struct compat_50_sys_kevent_args *uap,
76     register_t *retval)
77 {
78 	/* {
79 		syscallarg(int) fd;
80 		syscallarg(struct kevent100 *) changelist;
81 		syscallarg(size_t) nchanges;
82 		syscallarg(struct kevent100 *) eventlist;
83 		syscallarg(size_t) nevents;
84 		syscallarg(struct timespec50) timeout;
85 	} */
86 	static const struct kevent_ops compat_50_kevent_ops = {
87 		.keo_private = NULL,
88 		.keo_fetch_timeout = compat_50_kevent_fetch_timeout,
89 		.keo_fetch_changes = compat_100___kevent50_fetch_changes,
90 		.keo_put_events = compat_100___kevent50_put_events,
91 	};
92 
93 	return kevent1(retval, SCARG(uap, fd),
94 	    (const struct kevent *)(const void *)SCARG(uap, changelist), SCARG(uap, nchanges),
95 	    (struct kevent *)(void *)SCARG(uap, eventlist), SCARG(uap, nevents),
96 	    (const struct timespec *)(const void *)SCARG(uap, timeout),
97 	    &compat_50_kevent_ops);
98 }
99 
100 int
compat_50_sys_select(struct lwp * l,const struct compat_50_sys_select_args * uap,register_t * retval)101 compat_50_sys_select(struct lwp *l,
102     const struct compat_50_sys_select_args *uap, register_t *retval)
103 {
104 	/* {
105 		syscallarg(int)			nd;
106 		syscallarg(fd_set *)		in;
107 		syscallarg(fd_set *)		ou;
108 		syscallarg(fd_set *)		ex;
109 		syscallarg(struct timeval50 *)	tv;
110 	} */
111 	struct timespec ats, *ts = NULL;
112 	struct timeval50 atv50;
113 	int error;
114 
115 	if (SCARG(uap, tv)) {
116 		error = copyin(SCARG(uap, tv), (void *)&atv50, sizeof(atv50));
117 		if (error)
118 			return error;
119 
120 		if (atv50.tv_usec < 0 || atv50.tv_usec >= 1000000)
121 			return EINVAL;
122 
123 		ats.tv_sec = atv50.tv_sec;
124 		ats.tv_nsec = atv50.tv_usec * 1000;
125 		ts = &ats;
126 	}
127 
128 	return selcommon(retval, SCARG(uap, nd), SCARG(uap, in),
129 	    SCARG(uap, ou), SCARG(uap, ex), ts, NULL);
130 }
131 
132 int
compat_50_sys_pselect(struct lwp * l,const struct compat_50_sys_pselect_args * uap,register_t * retval)133 compat_50_sys_pselect(struct lwp *l,
134     const struct compat_50_sys_pselect_args *uap, register_t *retval)
135 {
136 	/* {
137 		syscallarg(int)				nd;
138 		syscallarg(fd_set *)			in;
139 		syscallarg(fd_set *)			ou;
140 		syscallarg(fd_set *)			ex;
141 		syscallarg(const struct timespec50 *)	ts;
142 		syscallarg(sigset_t *)			mask;
143 	} */
144 	struct timespec50	ats50;
145 	struct timespec	ats, *ts = NULL;
146 	sigset_t	amask, *mask = NULL;
147 	int		error;
148 
149 	if (SCARG(uap, ts)) {
150 		error = copyin(SCARG(uap, ts), &ats50, sizeof(ats50));
151 		if (error)
152 			return error;
153 		timespec50_to_timespec(&ats50, &ats);
154 		ts = &ats;
155 	}
156 	if (SCARG(uap, mask) != NULL) {
157 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
158 		if (error)
159 			return error;
160 		mask = &amask;
161 	}
162 
163 	return selcommon(retval, SCARG(uap, nd), SCARG(uap, in),
164 	    SCARG(uap, ou), SCARG(uap, ex), ts, mask);
165 }
166 
167 int
compat_50_sys_pollts(struct lwp * l,const struct compat_50_sys_pollts_args * uap,register_t * retval)168 compat_50_sys_pollts(struct lwp *l, const struct compat_50_sys_pollts_args *uap,
169     register_t *retval)
170 {
171 	/* {
172 		syscallarg(struct pollfd *)		fds;
173 		syscallarg(u_int)			nfds;
174 		syscallarg(const struct timespec50 *)	ts;
175 		syscallarg(const sigset_t *)		mask;
176 	} */
177 	struct timespec	ats, *ts = NULL;
178 	struct timespec50 ats50;
179 	sigset_t	amask, *mask = NULL;
180 	int		error;
181 
182 	if (SCARG(uap, ts)) {
183 		error = copyin(SCARG(uap, ts), &ats50, sizeof(ats50));
184 		if (error)
185 			return error;
186 		timespec50_to_timespec(&ats50, &ats);
187 		ts = &ats;
188 	}
189 	if (SCARG(uap, mask)) {
190 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
191 		if (error)
192 			return error;
193 		mask = &amask;
194 	}
195 
196 	return pollcommon(retval, SCARG(uap, fds), SCARG(uap, nfds), ts, mask);
197 }
198 
199 int
kern_select_50_init(void)200 kern_select_50_init(void)
201 {
202 
203 return syscall_establish(NULL, kern_select_50_syscalls);
204 }
205 
206 int
kern_select_50_fini(void)207 kern_select_50_fini(void)
208 {
209 
210 return syscall_disestablish(NULL, kern_select_50_syscalls);
211 }
212