sys_generic.c revision 1.100.2.6 1 1.100.2.6 ad /* $NetBSD: sys_generic.c,v 1.100.2.6 2007/04/28 22:40:04 ad Exp $ */
2 1.100.2.6 ad
3 1.100.2.6 ad /*-
4 1.100.2.6 ad * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.100.2.6 ad * All rights reserved.
6 1.100.2.6 ad *
7 1.100.2.6 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.100.2.6 ad * by Andrew Doran.
9 1.100.2.6 ad *
10 1.100.2.6 ad * Redistribution and use in source and binary forms, with or without
11 1.100.2.6 ad * modification, are permitted provided that the following conditions
12 1.100.2.6 ad * are met:
13 1.100.2.6 ad * 1. Redistributions of source code must retain the above copyright
14 1.100.2.6 ad * notice, this list of conditions and the following disclaimer.
15 1.100.2.6 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.100.2.6 ad * notice, this list of conditions and the following disclaimer in the
17 1.100.2.6 ad * documentation and/or other materials provided with the distribution.
18 1.100.2.6 ad * 3. All advertising materials mentioning features or use of this software
19 1.100.2.6 ad * must display the following acknowledgement:
20 1.100.2.6 ad * This product includes software developed by the NetBSD
21 1.100.2.6 ad * Foundation, Inc. and its contributors.
22 1.100.2.6 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.100.2.6 ad * contributors may be used to endorse or promote products derived
24 1.100.2.6 ad * from this software without specific prior written permission.
25 1.100.2.6 ad *
26 1.100.2.6 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.100.2.6 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.100.2.6 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.100.2.6 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.100.2.6 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.100.2.6 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.100.2.6 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.100.2.6 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.100.2.6 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.100.2.6 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.100.2.6 ad * POSSIBILITY OF SUCH DAMAGE.
37 1.100.2.6 ad */
38 1.15 cgd
39 1.15 cgd /*
40 1.15 cgd * Copyright (c) 1982, 1986, 1989, 1993
41 1.15 cgd * The Regents of the University of California. All rights reserved.
42 1.15 cgd * (c) UNIX System Laboratories, Inc.
43 1.15 cgd * All or some portions of this file are derived from material licensed
44 1.15 cgd * to the University of California by American Telephone and Telegraph
45 1.15 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
46 1.15 cgd * the permission of UNIX System Laboratories, Inc.
47 1.15 cgd *
48 1.15 cgd * Redistribution and use in source and binary forms, with or without
49 1.15 cgd * modification, are permitted provided that the following conditions
50 1.15 cgd * are met:
51 1.15 cgd * 1. Redistributions of source code must retain the above copyright
52 1.15 cgd * notice, this list of conditions and the following disclaimer.
53 1.15 cgd * 2. Redistributions in binary form must reproduce the above copyright
54 1.15 cgd * notice, this list of conditions and the following disclaimer in the
55 1.15 cgd * documentation and/or other materials provided with the distribution.
56 1.77 agc * 3. Neither the name of the University nor the names of its contributors
57 1.15 cgd * may be used to endorse or promote products derived from this software
58 1.15 cgd * without specific prior written permission.
59 1.15 cgd *
60 1.15 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 1.15 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 1.15 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 1.15 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 1.15 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 1.15 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 1.15 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 1.15 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 1.15 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 1.15 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 1.15 cgd * SUCH DAMAGE.
71 1.15 cgd *
72 1.36 fvdl * @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
73 1.15 cgd */
74 1.59 lukem
75 1.100.2.3 ad /*
76 1.100.2.3 ad * System calls relating to files.
77 1.100.2.3 ad */
78 1.100.2.3 ad
79 1.59 lukem #include <sys/cdefs.h>
80 1.100.2.6 ad __KERNEL_RCSID(0, "$NetBSD: sys_generic.c,v 1.100.2.6 2007/04/28 22:40:04 ad Exp $");
81 1.37 thorpej
82 1.37 thorpej #include "opt_ktrace.h"
83 1.15 cgd
84 1.15 cgd #include <sys/param.h>
85 1.15 cgd #include <sys/systm.h>
86 1.15 cgd #include <sys/filedesc.h>
87 1.15 cgd #include <sys/ioctl.h>
88 1.15 cgd #include <sys/file.h>
89 1.15 cgd #include <sys/proc.h>
90 1.15 cgd #include <sys/socketvar.h>
91 1.22 christos #include <sys/signalvar.h>
92 1.15 cgd #include <sys/uio.h>
93 1.15 cgd #include <sys/kernel.h>
94 1.15 cgd #include <sys/stat.h>
95 1.100.2.1 ad #include <sys/kmem.h>
96 1.28 mycroft #include <sys/poll.h>
97 1.100.2.6 ad #include <sys/mount.h>
98 1.100.2.6 ad #include <sys/syscallargs.h>
99 1.15 cgd #ifdef KTRACE
100 1.15 cgd #include <sys/ktrace.h>
101 1.15 cgd #endif
102 1.15 cgd
103 1.85 yamt #include <uvm/uvm_extern.h>
104 1.85 yamt
105 1.100.2.6 ad /* Flags for lwp::l_selflag. */
106 1.100.2.6 ad #define SEL_RESET 0 /* awoken, interrupted, or not yet polling */
107 1.100.2.6 ad #define SEL_SCANNING 1 /* polling descriptors */
108 1.100.2.6 ad #define SEL_BLOCKING 2 /* about to block on select_cv */
109 1.100.2.6 ad
110 1.100.2.6 ad static int selscan(lwp_t *, fd_mask *, fd_mask *, int, register_t *);
111 1.100.2.6 ad static int pollscan(lwp_t *, struct pollfd *, int, register_t *);
112 1.100.2.6 ad static void selclear(void);
113 1.100.2.6 ad
114 1.100.2.6 ad /* Global state for select()/poll(). */
115 1.100.2.6 ad kmutex_t select_lock;
116 1.100.2.6 ad kcondvar_t select_cv;
117 1.100.2.6 ad int nselcoll;
118 1.82 matt
119 1.15 cgd /*
120 1.15 cgd * Read system call.
121 1.15 cgd */
122 1.15 cgd /* ARGSUSED */
123 1.22 christos int
124 1.100.2.6 ad sys_read(lwp_t *l, void *v, register_t *retval)
125 1.20 thorpej {
126 1.47 augustss struct sys_read_args /* {
127 1.53 lukem syscallarg(int) fd;
128 1.53 lukem syscallarg(void *) buf;
129 1.53 lukem syscallarg(size_t) nbyte;
130 1.20 thorpej } */ *uap = v;
131 1.53 lukem int fd;
132 1.53 lukem struct file *fp;
133 1.100.2.6 ad proc_t *p;
134 1.53 lukem struct filedesc *fdp;
135 1.39 thorpej
136 1.53 lukem fd = SCARG(uap, fd);
137 1.69 thorpej p = l->l_proc;
138 1.53 lukem fdp = p->p_fd;
139 1.56 thorpej
140 1.56 thorpej if ((fp = fd_getfile(fdp, fd)) == NULL)
141 1.56 thorpej return (EBADF);
142 1.56 thorpej
143 1.70 pk if ((fp->f_flag & FREAD) == 0) {
144 1.100.2.1 ad mutex_exit(&fp->f_lock);
145 1.39 thorpej return (EBADF);
146 1.70 pk }
147 1.39 thorpej
148 1.45 thorpej FILE_USE(fp);
149 1.45 thorpej
150 1.45 thorpej /* dofileread() will unuse the descriptor for us */
151 1.84 christos return (dofileread(l, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
152 1.39 thorpej &fp->f_offset, FOF_UPDATE_OFFSET, retval));
153 1.39 thorpej }
154 1.39 thorpej
155 1.39 thorpej int
156 1.100.2.6 ad dofileread(lwp_t *l, int fd, struct file *fp, void *buf, size_t nbyte,
157 1.53 lukem off_t *offset, int flags, register_t *retval)
158 1.53 lukem {
159 1.84 christos struct iovec aiov;
160 1.84 christos struct uio auio;
161 1.100.2.6 ad proc_t *p;
162 1.85 yamt struct vmspace *vm;
163 1.84 christos size_t cnt;
164 1.84 christos int error;
165 1.15 cgd #ifdef KTRACE
166 1.92 christos struct iovec ktriov = { .iov_base = NULL, };
167 1.15 cgd #endif
168 1.84 christos p = l->l_proc;
169 1.85 yamt
170 1.85 yamt error = proc_vmspace_getref(p, &vm);
171 1.85 yamt if (error) {
172 1.85 yamt goto out;
173 1.85 yamt }
174 1.15 cgd
175 1.100 christos aiov.iov_base = (void *)buf;
176 1.39 thorpej aiov.iov_len = nbyte;
177 1.15 cgd auio.uio_iov = &aiov;
178 1.15 cgd auio.uio_iovcnt = 1;
179 1.39 thorpej auio.uio_resid = nbyte;
180 1.15 cgd auio.uio_rw = UIO_READ;
181 1.85 yamt auio.uio_vmspace = vm;
182 1.40 thorpej
183 1.40 thorpej /*
184 1.40 thorpej * Reads return ssize_t because -1 is returned on error. Therefore
185 1.40 thorpej * we must restrict the length to SSIZE_MAX to avoid garbage return
186 1.40 thorpej * values.
187 1.40 thorpej */
188 1.45 thorpej if (auio.uio_resid > SSIZE_MAX) {
189 1.45 thorpej error = EINVAL;
190 1.45 thorpej goto out;
191 1.45 thorpej }
192 1.40 thorpej
193 1.15 cgd #ifdef KTRACE
194 1.15 cgd /*
195 1.15 cgd * if tracing, save a copy of iovec
196 1.15 cgd */
197 1.15 cgd if (KTRPOINT(p, KTR_GENIO))
198 1.15 cgd ktriov = aiov;
199 1.15 cgd #endif
200 1.38 thorpej cnt = auio.uio_resid;
201 1.39 thorpej error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
202 1.22 christos if (error)
203 1.15 cgd if (auio.uio_resid != cnt && (error == ERESTART ||
204 1.15 cgd error == EINTR || error == EWOULDBLOCK))
205 1.15 cgd error = 0;
206 1.15 cgd cnt -= auio.uio_resid;
207 1.15 cgd #ifdef KTRACE
208 1.15 cgd if (KTRPOINT(p, KTR_GENIO) && error == 0)
209 1.84 christos ktrgenio(l, fd, UIO_READ, &ktriov, cnt, error);
210 1.15 cgd #endif
211 1.15 cgd *retval = cnt;
212 1.45 thorpej out:
213 1.84 christos FILE_UNUSE(fp, l);
214 1.85 yamt uvmspace_free(vm);
215 1.15 cgd return (error);
216 1.15 cgd }
217 1.15 cgd
218 1.15 cgd /*
219 1.15 cgd * Scatter read system call.
220 1.15 cgd */
221 1.22 christos int
222 1.100.2.6 ad sys_readv(lwp_t *l, void *v, register_t *retval)
223 1.20 thorpej {
224 1.47 augustss struct sys_readv_args /* {
225 1.53 lukem syscallarg(int) fd;
226 1.53 lukem syscallarg(const struct iovec *) iovp;
227 1.53 lukem syscallarg(int) iovcnt;
228 1.20 thorpej } */ *uap = v;
229 1.53 lukem struct filedesc *fdp;
230 1.84 christos struct file *fp;
231 1.100.2.6 ad proc_t *p;
232 1.84 christos int fd;
233 1.39 thorpej
234 1.53 lukem fd = SCARG(uap, fd);
235 1.69 thorpej p = l->l_proc;
236 1.53 lukem fdp = p->p_fd;
237 1.56 thorpej
238 1.56 thorpej if ((fp = fd_getfile(fdp, fd)) == NULL)
239 1.56 thorpej return (EBADF);
240 1.56 thorpej
241 1.70 pk if ((fp->f_flag & FREAD) == 0) {
242 1.100.2.1 ad mutex_exit(&fp->f_lock);
243 1.39 thorpej return (EBADF);
244 1.70 pk }
245 1.39 thorpej
246 1.45 thorpej FILE_USE(fp);
247 1.45 thorpej
248 1.45 thorpej /* dofilereadv() will unuse the descriptor for us */
249 1.84 christos return (dofilereadv(l, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
250 1.39 thorpej &fp->f_offset, FOF_UPDATE_OFFSET, retval));
251 1.39 thorpej }
252 1.39 thorpej
253 1.39 thorpej int
254 1.100.2.6 ad dofilereadv(lwp_t *l, int fd, struct file *fp, const struct iovec *iovp,
255 1.53 lukem int iovcnt, off_t *offset, int flags, register_t *retval)
256 1.53 lukem {
257 1.100.2.6 ad proc_t *p;
258 1.53 lukem struct uio auio;
259 1.53 lukem struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
260 1.85 yamt struct vmspace *vm;
261 1.64 thorpej int i, error;
262 1.64 thorpej size_t cnt;
263 1.53 lukem u_int iovlen;
264 1.15 cgd #ifdef KTRACE
265 1.53 lukem struct iovec *ktriov;
266 1.15 cgd #endif
267 1.15 cgd
268 1.84 christos p = l->l_proc;
269 1.85 yamt error = proc_vmspace_getref(p, &vm);
270 1.85 yamt if (error) {
271 1.85 yamt goto out;
272 1.85 yamt }
273 1.85 yamt
274 1.53 lukem #ifdef KTRACE
275 1.53 lukem ktriov = NULL;
276 1.53 lukem #endif
277 1.15 cgd /* note: can't use iovlen until iovcnt is validated */
278 1.42 perry iovlen = iovcnt * sizeof(struct iovec);
279 1.34 mycroft if ((u_int)iovcnt > UIO_SMALLIOV) {
280 1.45 thorpej if ((u_int)iovcnt > IOV_MAX) {
281 1.45 thorpej error = EINVAL;
282 1.45 thorpej goto out;
283 1.45 thorpej }
284 1.100.2.1 ad iov = kmem_alloc(iovlen, KM_SLEEP);
285 1.15 cgd needfree = iov;
286 1.41 kleink } else if ((u_int)iovcnt > 0) {
287 1.15 cgd iov = aiov;
288 1.15 cgd needfree = NULL;
289 1.45 thorpej } else {
290 1.45 thorpej error = EINVAL;
291 1.45 thorpej goto out;
292 1.45 thorpej }
293 1.41 kleink
294 1.15 cgd auio.uio_iov = iov;
295 1.34 mycroft auio.uio_iovcnt = iovcnt;
296 1.15 cgd auio.uio_rw = UIO_READ;
297 1.85 yamt auio.uio_vmspace = vm;
298 1.39 thorpej error = copyin(iovp, iov, iovlen);
299 1.22 christos if (error)
300 1.15 cgd goto done;
301 1.15 cgd auio.uio_resid = 0;
302 1.34 mycroft for (i = 0; i < iovcnt; i++) {
303 1.15 cgd auio.uio_resid += iov->iov_len;
304 1.40 thorpej /*
305 1.40 thorpej * Reads return ssize_t because -1 is returned on error.
306 1.40 thorpej * Therefore we must restrict the length to SSIZE_MAX to
307 1.40 thorpej * avoid garbage return values.
308 1.40 thorpej */
309 1.40 thorpej if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
310 1.15 cgd error = EINVAL;
311 1.15 cgd goto done;
312 1.15 cgd }
313 1.15 cgd iov++;
314 1.15 cgd }
315 1.15 cgd #ifdef KTRACE
316 1.15 cgd /*
317 1.15 cgd * if tracing, save a copy of iovec
318 1.15 cgd */
319 1.15 cgd if (KTRPOINT(p, KTR_GENIO)) {
320 1.100.2.1 ad ktriov = kmem_alloc(iovlen, KM_SLEEP);
321 1.100 christos memcpy((void *)ktriov, (void *)auio.uio_iov, iovlen);
322 1.15 cgd }
323 1.15 cgd #endif
324 1.15 cgd cnt = auio.uio_resid;
325 1.39 thorpej error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
326 1.22 christos if (error)
327 1.15 cgd if (auio.uio_resid != cnt && (error == ERESTART ||
328 1.15 cgd error == EINTR || error == EWOULDBLOCK))
329 1.15 cgd error = 0;
330 1.15 cgd cnt -= auio.uio_resid;
331 1.15 cgd #ifdef KTRACE
332 1.58 itohy if (ktriov != NULL) {
333 1.78 drochner if (KTRPOINT(p, KTR_GENIO) && (error == 0))
334 1.84 christos ktrgenio(l, fd, UIO_READ, ktriov, cnt, error);
335 1.100.2.1 ad kmem_free(ktriov, iovlen);
336 1.15 cgd }
337 1.15 cgd #endif
338 1.15 cgd *retval = cnt;
339 1.45 thorpej done:
340 1.15 cgd if (needfree)
341 1.100.2.1 ad kmem_free(needfree, iovlen);
342 1.45 thorpej out:
343 1.84 christos FILE_UNUSE(fp, l);
344 1.85 yamt uvmspace_free(vm);
345 1.15 cgd return (error);
346 1.15 cgd }
347 1.15 cgd
348 1.15 cgd /*
349 1.15 cgd * Write system call
350 1.15 cgd */
351 1.22 christos int
352 1.100.2.6 ad sys_write(lwp_t *l, void *v, register_t *retval)
353 1.20 thorpej {
354 1.47 augustss struct sys_write_args /* {
355 1.53 lukem syscallarg(int) fd;
356 1.53 lukem syscallarg(const void *) buf;
357 1.53 lukem syscallarg(size_t) nbyte;
358 1.20 thorpej } */ *uap = v;
359 1.53 lukem int fd;
360 1.53 lukem struct file *fp;
361 1.100.2.6 ad proc_t *p;
362 1.53 lukem struct filedesc *fdp;
363 1.39 thorpej
364 1.53 lukem fd = SCARG(uap, fd);
365 1.69 thorpej p = l->l_proc;
366 1.53 lukem fdp = p->p_fd;
367 1.56 thorpej
368 1.56 thorpej if ((fp = fd_getfile(fdp, fd)) == NULL)
369 1.56 thorpej return (EBADF);
370 1.56 thorpej
371 1.70 pk if ((fp->f_flag & FWRITE) == 0) {
372 1.100.2.1 ad mutex_exit(&fp->f_lock);
373 1.39 thorpej return (EBADF);
374 1.70 pk }
375 1.39 thorpej
376 1.45 thorpej FILE_USE(fp);
377 1.45 thorpej
378 1.45 thorpej /* dofilewrite() will unuse the descriptor for us */
379 1.84 christos return (dofilewrite(l, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
380 1.39 thorpej &fp->f_offset, FOF_UPDATE_OFFSET, retval));
381 1.39 thorpej }
382 1.39 thorpej
383 1.39 thorpej int
384 1.100.2.6 ad dofilewrite(lwp_t *l, int fd, struct file *fp, const void *buf,
385 1.53 lukem size_t nbyte, off_t *offset, int flags, register_t *retval)
386 1.53 lukem {
387 1.84 christos struct iovec aiov;
388 1.84 christos struct uio auio;
389 1.100.2.6 ad proc_t *p;
390 1.85 yamt struct vmspace *vm;
391 1.84 christos size_t cnt;
392 1.84 christos int error;
393 1.15 cgd #ifdef KTRACE
394 1.92 christos struct iovec ktriov = { .iov_base = NULL, };
395 1.15 cgd #endif
396 1.15 cgd
397 1.84 christos p = l->l_proc;
398 1.85 yamt error = proc_vmspace_getref(p, &vm);
399 1.85 yamt if (error) {
400 1.85 yamt goto out;
401 1.85 yamt }
402 1.83 christos aiov.iov_base = __UNCONST(buf); /* XXXUNCONST kills const */
403 1.39 thorpej aiov.iov_len = nbyte;
404 1.15 cgd auio.uio_iov = &aiov;
405 1.15 cgd auio.uio_iovcnt = 1;
406 1.39 thorpej auio.uio_resid = nbyte;
407 1.15 cgd auio.uio_rw = UIO_WRITE;
408 1.85 yamt auio.uio_vmspace = vm;
409 1.40 thorpej
410 1.40 thorpej /*
411 1.40 thorpej * Writes return ssize_t because -1 is returned on error. Therefore
412 1.40 thorpej * we must restrict the length to SSIZE_MAX to avoid garbage return
413 1.40 thorpej * values.
414 1.40 thorpej */
415 1.45 thorpej if (auio.uio_resid > SSIZE_MAX) {
416 1.45 thorpej error = EINVAL;
417 1.45 thorpej goto out;
418 1.45 thorpej }
419 1.40 thorpej
420 1.15 cgd #ifdef KTRACE
421 1.15 cgd /*
422 1.15 cgd * if tracing, save a copy of iovec
423 1.15 cgd */
424 1.15 cgd if (KTRPOINT(p, KTR_GENIO))
425 1.15 cgd ktriov = aiov;
426 1.15 cgd #endif
427 1.38 thorpej cnt = auio.uio_resid;
428 1.39 thorpej error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
429 1.22 christos if (error) {
430 1.15 cgd if (auio.uio_resid != cnt && (error == ERESTART ||
431 1.15 cgd error == EINTR || error == EWOULDBLOCK))
432 1.15 cgd error = 0;
433 1.98 ad if (error == EPIPE) {
434 1.98 ad mutex_enter(&proclist_mutex);
435 1.15 cgd psignal(p, SIGPIPE);
436 1.98 ad mutex_exit(&proclist_mutex);
437 1.98 ad }
438 1.15 cgd }
439 1.15 cgd cnt -= auio.uio_resid;
440 1.15 cgd #ifdef KTRACE
441 1.15 cgd if (KTRPOINT(p, KTR_GENIO) && error == 0)
442 1.84 christos ktrgenio(l, fd, UIO_WRITE, &ktriov, cnt, error);
443 1.15 cgd #endif
444 1.15 cgd *retval = cnt;
445 1.45 thorpej out:
446 1.84 christos FILE_UNUSE(fp, l);
447 1.85 yamt uvmspace_free(vm);
448 1.15 cgd return (error);
449 1.15 cgd }
450 1.15 cgd
451 1.15 cgd /*
452 1.15 cgd * Gather write system call
453 1.15 cgd */
454 1.22 christos int
455 1.100.2.6 ad sys_writev(lwp_t *l, void *v, register_t *retval)
456 1.20 thorpej {
457 1.47 augustss struct sys_writev_args /* {
458 1.53 lukem syscallarg(int) fd;
459 1.53 lukem syscallarg(const struct iovec *) iovp;
460 1.53 lukem syscallarg(int) iovcnt;
461 1.20 thorpej } */ *uap = v;
462 1.53 lukem int fd;
463 1.53 lukem struct file *fp;
464 1.100.2.6 ad proc_t *p;
465 1.53 lukem struct filedesc *fdp;
466 1.39 thorpej
467 1.53 lukem fd = SCARG(uap, fd);
468 1.69 thorpej p = l->l_proc;
469 1.53 lukem fdp = p->p_fd;
470 1.56 thorpej
471 1.56 thorpej if ((fp = fd_getfile(fdp, fd)) == NULL)
472 1.56 thorpej return (EBADF);
473 1.56 thorpej
474 1.70 pk if ((fp->f_flag & FWRITE) == 0) {
475 1.100.2.1 ad mutex_exit(&fp->f_lock);
476 1.39 thorpej return (EBADF);
477 1.70 pk }
478 1.39 thorpej
479 1.45 thorpej FILE_USE(fp);
480 1.45 thorpej
481 1.45 thorpej /* dofilewritev() will unuse the descriptor for us */
482 1.84 christos return (dofilewritev(l, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
483 1.39 thorpej &fp->f_offset, FOF_UPDATE_OFFSET, retval));
484 1.39 thorpej }
485 1.39 thorpej
486 1.39 thorpej int
487 1.100.2.6 ad dofilewritev(lwp_t *l, int fd, struct file *fp, const struct iovec *iovp,
488 1.53 lukem int iovcnt, off_t *offset, int flags, register_t *retval)
489 1.53 lukem {
490 1.100.2.6 ad proc_t *p;
491 1.53 lukem struct uio auio;
492 1.53 lukem struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
493 1.85 yamt struct vmspace *vm;
494 1.64 thorpej int i, error;
495 1.64 thorpej size_t cnt;
496 1.53 lukem u_int iovlen;
497 1.15 cgd #ifdef KTRACE
498 1.53 lukem struct iovec *ktriov;
499 1.15 cgd #endif
500 1.15 cgd
501 1.84 christos p = l->l_proc;
502 1.85 yamt error = proc_vmspace_getref(p, &vm);
503 1.85 yamt if (error) {
504 1.85 yamt goto out;
505 1.85 yamt }
506 1.53 lukem #ifdef KTRACE
507 1.53 lukem ktriov = NULL;
508 1.53 lukem #endif
509 1.15 cgd /* note: can't use iovlen until iovcnt is validated */
510 1.42 perry iovlen = iovcnt * sizeof(struct iovec);
511 1.34 mycroft if ((u_int)iovcnt > UIO_SMALLIOV) {
512 1.62 jdolecek if ((u_int)iovcnt > IOV_MAX) {
513 1.62 jdolecek error = EINVAL;
514 1.62 jdolecek goto out;
515 1.62 jdolecek }
516 1.100.2.1 ad iov = kmem_alloc(iovlen, KM_SLEEP);
517 1.15 cgd needfree = iov;
518 1.41 kleink } else if ((u_int)iovcnt > 0) {
519 1.15 cgd iov = aiov;
520 1.15 cgd needfree = NULL;
521 1.45 thorpej } else {
522 1.45 thorpej error = EINVAL;
523 1.45 thorpej goto out;
524 1.45 thorpej }
525 1.41 kleink
526 1.15 cgd auio.uio_iov = iov;
527 1.34 mycroft auio.uio_iovcnt = iovcnt;
528 1.15 cgd auio.uio_rw = UIO_WRITE;
529 1.85 yamt auio.uio_vmspace = vm;
530 1.39 thorpej error = copyin(iovp, iov, iovlen);
531 1.22 christos if (error)
532 1.15 cgd goto done;
533 1.15 cgd auio.uio_resid = 0;
534 1.34 mycroft for (i = 0; i < iovcnt; i++) {
535 1.15 cgd auio.uio_resid += iov->iov_len;
536 1.40 thorpej /*
537 1.40 thorpej * Writes return ssize_t because -1 is returned on error.
538 1.40 thorpej * Therefore we must restrict the length to SSIZE_MAX to
539 1.40 thorpej * avoid garbage return values.
540 1.40 thorpej */
541 1.40 thorpej if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
542 1.15 cgd error = EINVAL;
543 1.15 cgd goto done;
544 1.15 cgd }
545 1.15 cgd iov++;
546 1.15 cgd }
547 1.15 cgd #ifdef KTRACE
548 1.15 cgd /*
549 1.15 cgd * if tracing, save a copy of iovec
550 1.15 cgd */
551 1.15 cgd if (KTRPOINT(p, KTR_GENIO)) {
552 1.100.2.1 ad ktriov = kmem_alloc(iovlen, KM_SLEEP);
553 1.100 christos memcpy((void *)ktriov, (void *)auio.uio_iov, iovlen);
554 1.15 cgd }
555 1.15 cgd #endif
556 1.15 cgd cnt = auio.uio_resid;
557 1.39 thorpej error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
558 1.22 christos if (error) {
559 1.15 cgd if (auio.uio_resid != cnt && (error == ERESTART ||
560 1.15 cgd error == EINTR || error == EWOULDBLOCK))
561 1.15 cgd error = 0;
562 1.98 ad if (error == EPIPE) {
563 1.98 ad mutex_enter(&proclist_mutex);
564 1.15 cgd psignal(p, SIGPIPE);
565 1.98 ad mutex_exit(&proclist_mutex);
566 1.98 ad }
567 1.15 cgd }
568 1.15 cgd cnt -= auio.uio_resid;
569 1.15 cgd #ifdef KTRACE
570 1.78 drochner if (ktriov != NULL) {
571 1.78 drochner if (KTRPOINT(p, KTR_GENIO) && (error == 0))
572 1.84 christos ktrgenio(l, fd, UIO_WRITE, ktriov, cnt, error);
573 1.100.2.1 ad kmem_free(ktriov, iovlen);
574 1.15 cgd }
575 1.15 cgd #endif
576 1.15 cgd *retval = cnt;
577 1.45 thorpej done:
578 1.15 cgd if (needfree)
579 1.100.2.1 ad kmem_free(needfree, iovlen);
580 1.45 thorpej out:
581 1.84 christos FILE_UNUSE(fp, l);
582 1.85 yamt uvmspace_free(vm);
583 1.15 cgd return (error);
584 1.15 cgd }
585 1.15 cgd
586 1.15 cgd /*
587 1.15 cgd * Ioctl system call
588 1.15 cgd */
589 1.15 cgd /* ARGSUSED */
590 1.22 christos int
591 1.100.2.6 ad sys_ioctl(lwp_t *l, void *v, register_t *retval)
592 1.20 thorpej {
593 1.47 augustss struct sys_ioctl_args /* {
594 1.53 lukem syscallarg(int) fd;
595 1.53 lukem syscallarg(u_long) com;
596 1.100 christos syscallarg(void *) data;
597 1.20 thorpej } */ *uap = v;
598 1.53 lukem struct file *fp;
599 1.100.2.6 ad proc_t *p;
600 1.53 lukem struct filedesc *fdp;
601 1.53 lukem u_long com;
602 1.53 lukem int error;
603 1.53 lukem u_int size;
604 1.100 christos void *data, *memp;
605 1.53 lukem #define STK_PARAMS 128
606 1.53 lukem u_long stkbuf[STK_PARAMS/sizeof(u_long)];
607 1.15 cgd
608 1.53 lukem error = 0;
609 1.69 thorpej p = l->l_proc;
610 1.15 cgd fdp = p->p_fd;
611 1.56 thorpej
612 1.56 thorpej if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
613 1.15 cgd return (EBADF);
614 1.15 cgd
615 1.45 thorpej FILE_USE(fp);
616 1.45 thorpej
617 1.45 thorpej if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
618 1.45 thorpej error = EBADF;
619 1.65 scw com = 0;
620 1.45 thorpej goto out;
621 1.45 thorpej }
622 1.15 cgd
623 1.16 cgd switch (com = SCARG(uap, com)) {
624 1.15 cgd case FIONCLEX:
625 1.100.2.5 ad rw_enter(&fdp->fd_lock, RW_WRITER);
626 1.16 cgd fdp->fd_ofileflags[SCARG(uap, fd)] &= ~UF_EXCLOSE;
627 1.100.2.5 ad rw_exit(&fdp->fd_lock);
628 1.45 thorpej goto out;
629 1.45 thorpej
630 1.15 cgd case FIOCLEX:
631 1.100.2.5 ad rw_enter(&fdp->fd_lock, RW_WRITER);
632 1.16 cgd fdp->fd_ofileflags[SCARG(uap, fd)] |= UF_EXCLOSE;
633 1.100.2.5 ad rw_exit(&fdp->fd_lock);
634 1.45 thorpej goto out;
635 1.15 cgd }
636 1.15 cgd
637 1.15 cgd /*
638 1.15 cgd * Interpret high order word to find amount of data to be
639 1.15 cgd * copied to/from the user's address space.
640 1.15 cgd */
641 1.15 cgd size = IOCPARM_LEN(com);
642 1.45 thorpej if (size > IOCPARM_MAX) {
643 1.45 thorpej error = ENOTTY;
644 1.45 thorpej goto out;
645 1.45 thorpej }
646 1.15 cgd memp = NULL;
647 1.42 perry if (size > sizeof(stkbuf)) {
648 1.100.2.1 ad memp = kmem_alloc(size, KM_SLEEP);
649 1.15 cgd data = memp;
650 1.15 cgd } else
651 1.100 christos data = (void *)stkbuf;
652 1.15 cgd if (com&IOC_IN) {
653 1.15 cgd if (size) {
654 1.31 cgd error = copyin(SCARG(uap, data), data, size);
655 1.15 cgd if (error) {
656 1.15 cgd if (memp)
657 1.100.2.1 ad kmem_free(memp, size);
658 1.45 thorpej goto out;
659 1.15 cgd }
660 1.73 dsl #ifdef KTRACE
661 1.73 dsl if (KTRPOINT(p, KTR_GENIO)) {
662 1.73 dsl struct iovec iov;
663 1.73 dsl iov.iov_base = SCARG(uap, data);
664 1.73 dsl iov.iov_len = size;
665 1.84 christos ktrgenio(l, SCARG(uap, fd), UIO_WRITE, &iov,
666 1.73 dsl size, 0);
667 1.73 dsl }
668 1.73 dsl #endif
669 1.15 cgd } else
670 1.100 christos *(void **)data = SCARG(uap, data);
671 1.15 cgd } else if ((com&IOC_OUT) && size)
672 1.15 cgd /*
673 1.15 cgd * Zero the buffer so the user always
674 1.15 cgd * gets back something deterministic.
675 1.15 cgd */
676 1.44 perry memset(data, 0, size);
677 1.15 cgd else if (com&IOC_VOID)
678 1.100 christos *(void **)data = SCARG(uap, data);
679 1.15 cgd
680 1.15 cgd switch (com) {
681 1.15 cgd
682 1.15 cgd case FIONBIO:
683 1.100.2.5 ad mutex_enter(&fp->f_lock);
684 1.79 jdolecek if (*(int *)data != 0)
685 1.15 cgd fp->f_flag |= FNONBLOCK;
686 1.15 cgd else
687 1.15 cgd fp->f_flag &= ~FNONBLOCK;
688 1.100.2.5 ad mutex_exit(&fp->f_lock);
689 1.84 christos error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, data, l);
690 1.15 cgd break;
691 1.15 cgd
692 1.15 cgd case FIOASYNC:
693 1.100.2.5 ad mutex_enter(&fp->f_lock);
694 1.79 jdolecek if (*(int *)data != 0)
695 1.15 cgd fp->f_flag |= FASYNC;
696 1.15 cgd else
697 1.15 cgd fp->f_flag &= ~FASYNC;
698 1.100.2.5 ad mutex_exit(&fp->f_lock);
699 1.84 christos error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, data, l);
700 1.15 cgd break;
701 1.15 cgd
702 1.15 cgd default:
703 1.84 christos error = (*fp->f_ops->fo_ioctl)(fp, com, data, l);
704 1.15 cgd /*
705 1.15 cgd * Copy any data to user, size was
706 1.15 cgd * already set and checked above.
707 1.15 cgd */
708 1.73 dsl if (error == 0 && (com&IOC_OUT) && size) {
709 1.31 cgd error = copyout(data, SCARG(uap, data), size);
710 1.73 dsl #ifdef KTRACE
711 1.73 dsl if (KTRPOINT(p, KTR_GENIO)) {
712 1.73 dsl struct iovec iov;
713 1.73 dsl iov.iov_base = SCARG(uap, data);
714 1.73 dsl iov.iov_len = size;
715 1.84 christos ktrgenio(l, SCARG(uap, fd), UIO_READ, &iov,
716 1.73 dsl size, error);
717 1.73 dsl }
718 1.73 dsl #endif
719 1.73 dsl }
720 1.15 cgd break;
721 1.15 cgd }
722 1.15 cgd if (memp)
723 1.100.2.1 ad kmem_free(memp, size);
724 1.45 thorpej out:
725 1.84 christos FILE_UNUSE(fp, l);
726 1.61 atatat switch (error) {
727 1.61 atatat case -1:
728 1.61 atatat printf("sys_ioctl: _IO%s%s('%c', %lu, %lu) returned -1: "
729 1.61 atatat "pid=%d comm=%s\n",
730 1.61 atatat (com & IOC_IN) ? "W" : "", (com & IOC_OUT) ? "R" : "",
731 1.61 atatat (char)IOCGROUP(com), (com & 0xff), IOCPARM_LEN(com),
732 1.61 atatat p->p_pid, p->p_comm);
733 1.61 atatat /* FALLTHROUGH */
734 1.61 atatat case EPASSTHROUGH:
735 1.61 atatat error = ENOTTY;
736 1.61 atatat /* FALLTHROUGH */
737 1.61 atatat default:
738 1.61 atatat return (error);
739 1.61 atatat }
740 1.15 cgd }
741 1.15 cgd
742 1.15 cgd /*
743 1.15 cgd * Select system call.
744 1.15 cgd */
745 1.22 christos int
746 1.100.2.6 ad sys_pselect(lwp_t *l, void *v, register_t *retval)
747 1.82 matt {
748 1.82 matt struct sys_pselect_args /* {
749 1.82 matt syscallarg(int) nd;
750 1.82 matt syscallarg(fd_set *) in;
751 1.82 matt syscallarg(fd_set *) ou;
752 1.82 matt syscallarg(fd_set *) ex;
753 1.82 matt syscallarg(const struct timespec *) ts;
754 1.82 matt syscallarg(sigset_t *) mask;
755 1.82 matt } */ * const uap = v;
756 1.82 matt struct timespec ats;
757 1.82 matt struct timeval atv, *tv = NULL;
758 1.82 matt sigset_t amask, *mask = NULL;
759 1.82 matt int error;
760 1.82 matt
761 1.82 matt if (SCARG(uap, ts)) {
762 1.82 matt error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
763 1.82 matt if (error)
764 1.82 matt return error;
765 1.82 matt atv.tv_sec = ats.tv_sec;
766 1.82 matt atv.tv_usec = ats.tv_nsec / 1000;
767 1.82 matt tv = &atv;
768 1.82 matt }
769 1.82 matt if (SCARG(uap, mask) != NULL) {
770 1.82 matt error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
771 1.82 matt if (error)
772 1.82 matt return error;
773 1.82 matt mask = &amask;
774 1.82 matt }
775 1.82 matt
776 1.82 matt return selcommon(l, retval, SCARG(uap, nd), SCARG(uap, in),
777 1.82 matt SCARG(uap, ou), SCARG(uap, ex), tv, mask);
778 1.82 matt }
779 1.82 matt
780 1.91 kardel int
781 1.90 christos inittimeleft(struct timeval *tv, struct timeval *sleeptv)
782 1.90 christos {
783 1.90 christos if (itimerfix(tv))
784 1.90 christos return -1;
785 1.90 christos getmicrouptime(sleeptv);
786 1.90 christos return 0;
787 1.90 christos }
788 1.90 christos
789 1.91 kardel int
790 1.90 christos gettimeleft(struct timeval *tv, struct timeval *sleeptv)
791 1.90 christos {
792 1.90 christos /*
793 1.90 christos * We have to recalculate the timeout on every retry.
794 1.90 christos */
795 1.90 christos struct timeval slepttv;
796 1.90 christos /*
797 1.90 christos * reduce tv by elapsed time
798 1.90 christos * based on monotonic time scale
799 1.90 christos */
800 1.90 christos getmicrouptime(&slepttv);
801 1.90 christos timeradd(tv, sleeptv, tv);
802 1.90 christos timersub(tv, &slepttv, tv);
803 1.90 christos *sleeptv = slepttv;
804 1.90 christos return tvtohz(tv);
805 1.90 christos }
806 1.90 christos
807 1.82 matt int
808 1.100.2.6 ad sys_select(lwp_t *l, void *v, register_t *retval)
809 1.20 thorpej {
810 1.47 augustss struct sys_select_args /* {
811 1.53 lukem syscallarg(int) nd;
812 1.53 lukem syscallarg(fd_set *) in;
813 1.53 lukem syscallarg(fd_set *) ou;
814 1.53 lukem syscallarg(fd_set *) ex;
815 1.53 lukem syscallarg(struct timeval *) tv;
816 1.82 matt } */ * const uap = v;
817 1.82 matt struct timeval atv, *tv = NULL;
818 1.82 matt int error;
819 1.82 matt
820 1.82 matt if (SCARG(uap, tv)) {
821 1.100 christos error = copyin(SCARG(uap, tv), (void *)&atv,
822 1.82 matt sizeof(atv));
823 1.82 matt if (error)
824 1.82 matt return error;
825 1.82 matt tv = &atv;
826 1.82 matt }
827 1.82 matt
828 1.82 matt return selcommon(l, retval, SCARG(uap, nd), SCARG(uap, in),
829 1.82 matt SCARG(uap, ou), SCARG(uap, ex), tv, NULL);
830 1.82 matt }
831 1.82 matt
832 1.82 matt int
833 1.100.2.6 ad selcommon(lwp_t *l, register_t *retval, int nd, fd_set *u_in,
834 1.100.2.4 ad fd_set *u_ou, fd_set *u_ex, struct timeval *tv, sigset_t *mask)
835 1.82 matt {
836 1.86 kardel char smallbits[howmany(FD_SETSIZE, NFDBITS) *
837 1.86 kardel sizeof(fd_mask) * 6];
838 1.100.2.6 ad proc_t * const p = l->l_proc;
839 1.100 christos char *bits;
840 1.100.2.3 ad int ncoll, error, timo;
841 1.53 lukem size_t ni;
842 1.82 matt sigset_t oldmask;
843 1.89 christos struct timeval sleeptv;
844 1.15 cgd
845 1.53 lukem error = 0;
846 1.82 matt if (nd < 0)
847 1.35 thorpej return (EINVAL);
848 1.82 matt if (nd > p->p_fd->fd_nfiles) {
849 1.16 cgd /* forgiving; slightly wrong */
850 1.82 matt nd = p->p_fd->fd_nfiles;
851 1.16 cgd }
852 1.82 matt ni = howmany(nd, NFDBITS) * sizeof(fd_mask);
853 1.100.2.2 ad if (ni * 6 > sizeof(smallbits))
854 1.100.2.2 ad bits = kmem_alloc(ni * 6, KM_SLEEP);
855 1.100.2.2 ad else
856 1.26 cgd bits = smallbits;
857 1.15 cgd
858 1.53 lukem #define getbits(name, x) \
859 1.82 matt if (u_ ## name) { \
860 1.82 matt error = copyin(u_ ## name, bits + ni * x, ni); \
861 1.53 lukem if (error) \
862 1.53 lukem goto done; \
863 1.53 lukem } else \
864 1.44 perry memset(bits + ni * x, 0, ni);
865 1.15 cgd getbits(in, 0);
866 1.15 cgd getbits(ou, 1);
867 1.15 cgd getbits(ex, 2);
868 1.15 cgd #undef getbits
869 1.15 cgd
870 1.65 scw timo = 0;
871 1.90 christos if (tv && inittimeleft(tv, &sleeptv) == -1) {
872 1.90 christos error = EINVAL;
873 1.90 christos goto done;
874 1.65 scw }
875 1.89 christos
876 1.98 ad if (mask) {
877 1.98 ad sigminusset(&sigcantmask, mask);
878 1.98 ad mutex_enter(&p->p_smutex);
879 1.98 ad oldmask = l->l_sigmask;
880 1.98 ad l->l_sigmask = *mask;
881 1.98 ad mutex_exit(&p->p_smutex);
882 1.98 ad } else
883 1.98 ad oldmask = l->l_sigmask; /* XXXgcc */
884 1.65 scw
885 1.100.2.3 ad mutex_enter(&select_lock);
886 1.100.2.6 ad SLIST_INIT(&l->l_selwait);
887 1.100.2.3 ad for (;;) {
888 1.100.2.6 ad l->l_selflag = SEL_SCANNING;
889 1.100.2.3 ad ncoll = nselcoll;
890 1.100.2.3 ad mutex_exit(&select_lock);
891 1.100.2.3 ad
892 1.100.2.3 ad error = selscan(l, (fd_mask *)(bits + ni * 0),
893 1.100.2.3 ad (fd_mask *)(bits + ni * 3), nd, retval);
894 1.100.2.3 ad
895 1.100.2.3 ad mutex_enter(&select_lock);
896 1.100.2.3 ad if (error || *retval)
897 1.100.2.3 ad break;
898 1.100.2.3 ad if (tv && (timo = gettimeleft(tv, &sleeptv)) <= 0)
899 1.100.2.3 ad break;
900 1.100.2.6 ad if (l->l_selflag != SEL_SCANNING || ncoll != nselcoll)
901 1.100.2.3 ad continue;
902 1.100.2.6 ad l->l_selflag = SEL_BLOCKING;
903 1.100.2.3 ad error = cv_timedwait_sig(&select_cv, &select_lock, timo);
904 1.100.2.3 ad if (error != 0)
905 1.100.2.3 ad break;
906 1.100.2.3 ad }
907 1.100.2.3 ad selclear();
908 1.100.2.3 ad mutex_exit(&select_lock);
909 1.100.2.3 ad
910 1.98 ad if (mask) {
911 1.98 ad mutex_enter(&p->p_smutex);
912 1.98 ad l->l_sigmask = oldmask;
913 1.98 ad mutex_exit(&p->p_smutex);
914 1.98 ad }
915 1.100.2.3 ad
916 1.97 ad done:
917 1.15 cgd /* select is not restarted after signals... */
918 1.15 cgd if (error == ERESTART)
919 1.15 cgd error = EINTR;
920 1.15 cgd if (error == EWOULDBLOCK)
921 1.15 cgd error = 0;
922 1.100.2.4 ad if (error == 0 && u_in != NULL)
923 1.100.2.4 ad error = copyout(bits + ni * 3, u_in, ni);
924 1.100.2.4 ad if (error == 0 && u_ou != NULL)
925 1.100.2.4 ad error = copyout(bits + ni * 4, u_ou, ni);
926 1.100.2.4 ad if (error == 0 && u_ex != NULL)
927 1.100.2.4 ad error = copyout(bits + ni * 5, u_ex, ni);
928 1.100.2.2 ad if (bits != smallbits)
929 1.100.2.2 ad kmem_free(bits, ni * 6);
930 1.15 cgd return (error);
931 1.15 cgd }
932 1.15 cgd
933 1.22 christos int
934 1.100.2.6 ad selscan(lwp_t *l, fd_mask *ibitp, fd_mask *obitp, int nfd,
935 1.53 lukem register_t *retval)
936 1.53 lukem {
937 1.63 jdolecek static const int flag[3] = { POLLRDNORM | POLLHUP | POLLERR,
938 1.28 mycroft POLLWRNORM | POLLHUP | POLLERR,
939 1.28 mycroft POLLRDBAND };
940 1.100.2.6 ad proc_t *p = l->l_proc;
941 1.84 christos struct filedesc *fdp;
942 1.84 christos int msk, i, j, fd, n;
943 1.84 christos fd_mask ibits, obits;
944 1.84 christos struct file *fp;
945 1.15 cgd
946 1.53 lukem fdp = p->p_fd;
947 1.53 lukem n = 0;
948 1.15 cgd for (msk = 0; msk < 3; msk++) {
949 1.15 cgd for (i = 0; i < nfd; i += NFDBITS) {
950 1.25 mycroft ibits = *ibitp++;
951 1.25 mycroft obits = 0;
952 1.25 mycroft while ((j = ffs(ibits)) && (fd = i + --j) < nfd) {
953 1.25 mycroft ibits &= ~(1 << j);
954 1.56 thorpej if ((fp = fd_getfile(fdp, fd)) == NULL)
955 1.15 cgd return (EBADF);
956 1.45 thorpej FILE_USE(fp);
957 1.84 christos if ((*fp->f_ops->fo_poll)(fp, flag[msk], l)) {
958 1.25 mycroft obits |= (1 << j);
959 1.15 cgd n++;
960 1.15 cgd }
961 1.84 christos FILE_UNUSE(fp, l);
962 1.15 cgd }
963 1.25 mycroft *obitp++ = obits;
964 1.15 cgd }
965 1.15 cgd }
966 1.15 cgd *retval = n;
967 1.15 cgd return (0);
968 1.15 cgd }
969 1.15 cgd
970 1.28 mycroft /*
971 1.28 mycroft * Poll system call.
972 1.28 mycroft */
973 1.28 mycroft int
974 1.100.2.6 ad sys_poll(lwp_t *l, void *v, register_t *retval)
975 1.28 mycroft {
976 1.47 augustss struct sys_poll_args /* {
977 1.53 lukem syscallarg(struct pollfd *) fds;
978 1.53 lukem syscallarg(u_int) nfds;
979 1.53 lukem syscallarg(int) timeout;
980 1.82 matt } */ * const uap = v;
981 1.82 matt struct timeval atv, *tv = NULL;
982 1.82 matt
983 1.82 matt if (SCARG(uap, timeout) != INFTIM) {
984 1.82 matt atv.tv_sec = SCARG(uap, timeout) / 1000;
985 1.82 matt atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
986 1.82 matt tv = &atv;
987 1.82 matt }
988 1.82 matt
989 1.82 matt return pollcommon(l, retval, SCARG(uap, fds), SCARG(uap, nfds),
990 1.82 matt tv, NULL);
991 1.82 matt }
992 1.82 matt
993 1.82 matt /*
994 1.82 matt * Poll system call.
995 1.82 matt */
996 1.82 matt int
997 1.100.2.6 ad sys_pollts(lwp_t *l, void *v, register_t *retval)
998 1.82 matt {
999 1.82 matt struct sys_pollts_args /* {
1000 1.82 matt syscallarg(struct pollfd *) fds;
1001 1.82 matt syscallarg(u_int) nfds;
1002 1.82 matt syscallarg(const struct timespec *) ts;
1003 1.82 matt syscallarg(const sigset_t *) mask;
1004 1.82 matt } */ * const uap = v;
1005 1.82 matt struct timespec ats;
1006 1.82 matt struct timeval atv, *tv = NULL;
1007 1.82 matt sigset_t amask, *mask = NULL;
1008 1.82 matt int error;
1009 1.82 matt
1010 1.82 matt if (SCARG(uap, ts)) {
1011 1.82 matt error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
1012 1.82 matt if (error)
1013 1.82 matt return error;
1014 1.82 matt atv.tv_sec = ats.tv_sec;
1015 1.82 matt atv.tv_usec = ats.tv_nsec / 1000;
1016 1.82 matt tv = &atv;
1017 1.82 matt }
1018 1.82 matt if (SCARG(uap, mask)) {
1019 1.82 matt error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
1020 1.82 matt if (error)
1021 1.82 matt return error;
1022 1.82 matt mask = &amask;
1023 1.82 matt }
1024 1.82 matt
1025 1.82 matt return pollcommon(l, retval, SCARG(uap, fds), SCARG(uap, nfds),
1026 1.82 matt tv, mask);
1027 1.82 matt }
1028 1.82 matt
1029 1.82 matt int
1030 1.100.2.6 ad pollcommon(lwp_t *l, register_t *retval,
1031 1.82 matt struct pollfd *u_fds, u_int nfds,
1032 1.82 matt struct timeval *tv, sigset_t *mask)
1033 1.82 matt {
1034 1.86 kardel char smallbits[32 * sizeof(struct pollfd)];
1035 1.100.2.6 ad proc_t * const p = l->l_proc;
1036 1.100 christos void * bits;
1037 1.82 matt sigset_t oldmask;
1038 1.100.2.3 ad int ncoll, error, timo;
1039 1.53 lukem size_t ni;
1040 1.89 christos struct timeval sleeptv;
1041 1.28 mycroft
1042 1.82 matt if (nfds > p->p_fd->fd_nfiles) {
1043 1.28 mycroft /* forgiving; slightly wrong */
1044 1.82 matt nfds = p->p_fd->fd_nfiles;
1045 1.28 mycroft }
1046 1.82 matt ni = nfds * sizeof(struct pollfd);
1047 1.28 mycroft if (ni > sizeof(smallbits))
1048 1.100.2.1 ad bits = kmem_alloc(ni, KM_SLEEP);
1049 1.28 mycroft else
1050 1.28 mycroft bits = smallbits;
1051 1.28 mycroft
1052 1.82 matt error = copyin(u_fds, bits, ni);
1053 1.28 mycroft if (error)
1054 1.28 mycroft goto done;
1055 1.28 mycroft
1056 1.65 scw timo = 0;
1057 1.90 christos if (tv && inittimeleft(tv, &sleeptv) == -1) {
1058 1.90 christos error = EINVAL;
1059 1.90 christos goto done;
1060 1.65 scw }
1061 1.89 christos
1062 1.98 ad if (mask) {
1063 1.98 ad sigminusset(&sigcantmask, mask);
1064 1.98 ad mutex_enter(&p->p_smutex);
1065 1.98 ad oldmask = l->l_sigmask;
1066 1.98 ad l->l_sigmask = *mask;
1067 1.98 ad mutex_exit(&p->p_smutex);
1068 1.98 ad } else
1069 1.98 ad oldmask = l->l_sigmask; /* XXXgcc */
1070 1.65 scw
1071 1.100.2.3 ad mutex_enter(&select_lock);
1072 1.100.2.6 ad SLIST_INIT(&l->l_selwait);
1073 1.100.2.3 ad for (;;) {
1074 1.100.2.3 ad ncoll = nselcoll;
1075 1.100.2.6 ad l->l_selflag = SEL_SCANNING;
1076 1.100.2.3 ad mutex_exit(&select_lock);
1077 1.100.2.3 ad
1078 1.100.2.3 ad error = pollscan(l, (struct pollfd *)bits, nfds, retval);
1079 1.100.2.3 ad
1080 1.100.2.3 ad mutex_enter(&select_lock);
1081 1.100.2.3 ad if (error || *retval)
1082 1.100.2.3 ad break;
1083 1.100.2.3 ad if (tv && (timo = gettimeleft(tv, &sleeptv)) <= 0)
1084 1.100.2.3 ad break;
1085 1.100.2.6 ad if (l->l_selflag != SEL_SCANNING || nselcoll != ncoll)
1086 1.100.2.3 ad continue;
1087 1.100.2.6 ad l->l_selflag = SEL_BLOCKING;
1088 1.100.2.3 ad error = cv_timedwait_sig(&select_cv, &select_lock, timo);
1089 1.100.2.3 ad if (error != 0)
1090 1.100.2.3 ad break;
1091 1.100.2.3 ad }
1092 1.100.2.3 ad selclear();
1093 1.100.2.3 ad mutex_exit(&select_lock);
1094 1.100.2.3 ad
1095 1.98 ad if (mask) {
1096 1.98 ad mutex_enter(&p->p_smutex);
1097 1.98 ad l->l_sigmask = oldmask;
1098 1.98 ad mutex_exit(&p->p_smutex);
1099 1.98 ad }
1100 1.97 ad done:
1101 1.28 mycroft /* poll is not restarted after signals... */
1102 1.28 mycroft if (error == ERESTART)
1103 1.28 mycroft error = EINTR;
1104 1.28 mycroft if (error == EWOULDBLOCK)
1105 1.28 mycroft error = 0;
1106 1.100.2.4 ad if (error == 0)
1107 1.82 matt error = copyout(bits, u_fds, ni);
1108 1.100.2.2 ad if (bits != smallbits)
1109 1.100.2.1 ad kmem_free(bits, ni);
1110 1.28 mycroft return (error);
1111 1.28 mycroft }
1112 1.28 mycroft
1113 1.28 mycroft int
1114 1.100.2.6 ad pollscan(lwp_t *l, struct pollfd *fds, int nfd, register_t *retval)
1115 1.53 lukem {
1116 1.100.2.6 ad proc_t *p = l->l_proc;
1117 1.53 lukem struct filedesc *fdp;
1118 1.53 lukem int i, n;
1119 1.53 lukem struct file *fp;
1120 1.28 mycroft
1121 1.53 lukem fdp = p->p_fd;
1122 1.54 lukem n = 0;
1123 1.28 mycroft for (i = 0; i < nfd; i++, fds++) {
1124 1.60 christos if (fds->fd >= fdp->fd_nfiles) {
1125 1.28 mycroft fds->revents = POLLNVAL;
1126 1.28 mycroft n++;
1127 1.60 christos } else if (fds->fd < 0) {
1128 1.60 christos fds->revents = 0;
1129 1.28 mycroft } else {
1130 1.56 thorpej if ((fp = fd_getfile(fdp, fds->fd)) == NULL) {
1131 1.32 mrg fds->revents = POLLNVAL;
1132 1.28 mycroft n++;
1133 1.32 mrg } else {
1134 1.45 thorpej FILE_USE(fp);
1135 1.32 mrg fds->revents = (*fp->f_ops->fo_poll)(fp,
1136 1.84 christos fds->events | POLLERR | POLLHUP, l);
1137 1.32 mrg if (fds->revents != 0)
1138 1.32 mrg n++;
1139 1.84 christos FILE_UNUSE(fp, l);
1140 1.32 mrg }
1141 1.28 mycroft }
1142 1.28 mycroft }
1143 1.28 mycroft *retval = n;
1144 1.28 mycroft return (0);
1145 1.28 mycroft }
1146 1.28 mycroft
1147 1.15 cgd /*ARGSUSED*/
1148 1.22 christos int
1149 1.100.2.6 ad seltrue(dev_t dev, int events, lwp_t *l)
1150 1.15 cgd {
1151 1.15 cgd
1152 1.28 mycroft return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1153 1.15 cgd }
1154 1.15 cgd
1155 1.15 cgd /*
1156 1.15 cgd * Record a select request.
1157 1.15 cgd */
1158 1.15 cgd void
1159 1.100.2.6 ad selrecord(lwp_t *selector, struct selinfo *sip)
1160 1.15 cgd {
1161 1.98 ad
1162 1.100.2.3 ad mutex_enter(&select_lock);
1163 1.100.2.3 ad if (sip->sel_lwp == NULL) {
1164 1.100.2.6 ad /* First named waiter, although there may be more. */
1165 1.100.2.3 ad sip->sel_lwp = selector;
1166 1.100.2.6 ad SLIST_INSERT_HEAD(&selector->l_selwait, sip, sel_chain);
1167 1.100.2.3 ad } else if (sip->sel_lwp != selector) {
1168 1.100.2.6 ad /* Multiple waiters. */
1169 1.100.2.3 ad sip->sel_collision = true;
1170 1.69 thorpej }
1171 1.100.2.3 ad mutex_exit(&select_lock);
1172 1.15 cgd }
1173 1.15 cgd
1174 1.15 cgd /*
1175 1.15 cgd * Do a wakeup when a selectable event occurs.
1176 1.15 cgd */
1177 1.15 cgd void
1178 1.100.2.3 ad selwakeup(struct selinfo *sip)
1179 1.15 cgd {
1180 1.100.2.6 ad lwp_t *l;
1181 1.15 cgd
1182 1.100.2.3 ad mutex_enter(&select_lock);
1183 1.73 dsl if (sip->sel_collision) {
1184 1.100.2.3 ad /* Multiple waiters - just notify everybody. */
1185 1.15 cgd nselcoll++;
1186 1.100.2.3 ad sip->sel_collision = false;
1187 1.100.2.3 ad cv_broadcast(&select_cv);
1188 1.100.2.3 ad } else if (sip->sel_lwp != NULL) {
1189 1.100.2.3 ad /* Only one LWP waiting. */
1190 1.100.2.3 ad l = sip->sel_lwp;
1191 1.100.2.6 ad if (l->l_selflag == SEL_BLOCKING) {
1192 1.100.2.3 ad /*
1193 1.100.2.6 ad * If it's sleeping, wake it up. If not, it's
1194 1.100.2.6 ad * already awake but hasn't yet removed itself
1195 1.100.2.6 ad * from the selector. We reset the state below
1196 1.100.2.6 ad * so that we only attempt to do this once.
1197 1.100.2.3 ad */
1198 1.100.2.3 ad lwp_lock(l);
1199 1.100.2.3 ad if (l->l_wchan == &select_cv) {
1200 1.100.2.3 ad /* lwp_unsleep() releases the LWP lock. */
1201 1.100.2.3 ad lwp_unsleep(l);
1202 1.100.2.3 ad } else
1203 1.100.2.3 ad lwp_unlock(l);
1204 1.100.2.6 ad } else {
1205 1.100.2.6 ad /*
1206 1.100.2.6 ad * Not yet asleep. Reset its state below so that
1207 1.100.2.6 ad * it will go around again.
1208 1.100.2.6 ad */
1209 1.100.2.3 ad }
1210 1.100.2.6 ad l->l_selflag = SEL_RESET;
1211 1.15 cgd }
1212 1.100.2.3 ad mutex_exit(&select_lock);
1213 1.100.2.3 ad }
1214 1.98 ad
1215 1.100.2.3 ad void
1216 1.100.2.3 ad selnotify(struct selinfo *sip, long knhint)
1217 1.100.2.3 ad {
1218 1.98 ad
1219 1.100.2.3 ad selwakeup(sip);
1220 1.100.2.3 ad KNOTE(&sip->sel_klist, knhint);
1221 1.100.2.3 ad }
1222 1.100.2.3 ad
1223 1.100.2.3 ad /*
1224 1.100.2.3 ad * Remove an LWP from all objects that it is waiting for.
1225 1.100.2.3 ad */
1226 1.100.2.3 ad static void
1227 1.100.2.3 ad selclear(void)
1228 1.100.2.3 ad {
1229 1.100.2.3 ad struct selinfo *sip;
1230 1.100.2.6 ad lwp_t *l = curlwp;
1231 1.100.2.3 ad
1232 1.100.2.3 ad KASSERT(mutex_owned(&select_lock));
1233 1.100.2.3 ad
1234 1.100.2.6 ad SLIST_FOREACH(sip, &l->l_selwait, sel_chain) {
1235 1.100.2.3 ad KASSERT(sip->sel_lwp == l);
1236 1.100.2.3 ad sip->sel_lwp = NULL;
1237 1.15 cgd }
1238 1.100.2.3 ad }
1239 1.100.2.3 ad
1240 1.100.2.3 ad /*
1241 1.100.2.3 ad * Initialize the select/poll system calls.
1242 1.100.2.3 ad */
1243 1.100.2.3 ad void
1244 1.100.2.3 ad selsysinit(void)
1245 1.100.2.3 ad {
1246 1.100.2.3 ad
1247 1.100.2.3 ad mutex_init(&select_lock, MUTEX_DRIVER, IPL_VM);
1248 1.100.2.3 ad cv_init(&select_cv, "select");
1249 1.15 cgd }
1250