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