sys_generic.c revision 1.41 1 1.41 kleink /* $NetBSD: sys_generic.c,v 1.41 1998/07/31 15:38:58 kleink Exp $ */
2 1.15 cgd
3 1.15 cgd /*
4 1.15 cgd * Copyright (c) 1982, 1986, 1989, 1993
5 1.15 cgd * The Regents of the University of California. All rights reserved.
6 1.15 cgd * (c) UNIX System Laboratories, Inc.
7 1.15 cgd * All or some portions of this file are derived from material licensed
8 1.15 cgd * to the University of California by American Telephone and Telegraph
9 1.15 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.15 cgd * the permission of UNIX System Laboratories, Inc.
11 1.15 cgd *
12 1.15 cgd * Redistribution and use in source and binary forms, with or without
13 1.15 cgd * modification, are permitted provided that the following conditions
14 1.15 cgd * are met:
15 1.15 cgd * 1. Redistributions of source code must retain the above copyright
16 1.15 cgd * notice, this list of conditions and the following disclaimer.
17 1.15 cgd * 2. Redistributions in binary form must reproduce the above copyright
18 1.15 cgd * notice, this list of conditions and the following disclaimer in the
19 1.15 cgd * documentation and/or other materials provided with the distribution.
20 1.15 cgd * 3. All advertising materials mentioning features or use of this software
21 1.15 cgd * must display the following acknowledgement:
22 1.15 cgd * This product includes software developed by the University of
23 1.15 cgd * California, Berkeley and its contributors.
24 1.15 cgd * 4. Neither the name of the University nor the names of its contributors
25 1.15 cgd * may be used to endorse or promote products derived from this software
26 1.15 cgd * without specific prior written permission.
27 1.15 cgd *
28 1.15 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.15 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.15 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.15 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.15 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.15 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.15 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.15 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.15 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.15 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.15 cgd * SUCH DAMAGE.
39 1.15 cgd *
40 1.36 fvdl * @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
41 1.15 cgd */
42 1.37 thorpej
43 1.37 thorpej #include "opt_ktrace.h"
44 1.15 cgd
45 1.15 cgd #include <sys/param.h>
46 1.15 cgd #include <sys/systm.h>
47 1.15 cgd #include <sys/filedesc.h>
48 1.15 cgd #include <sys/ioctl.h>
49 1.15 cgd #include <sys/file.h>
50 1.15 cgd #include <sys/proc.h>
51 1.15 cgd #include <sys/socketvar.h>
52 1.22 christos #include <sys/signalvar.h>
53 1.15 cgd #include <sys/uio.h>
54 1.15 cgd #include <sys/kernel.h>
55 1.15 cgd #include <sys/stat.h>
56 1.15 cgd #include <sys/malloc.h>
57 1.28 mycroft #include <sys/poll.h>
58 1.15 cgd #ifdef KTRACE
59 1.15 cgd #include <sys/ktrace.h>
60 1.15 cgd #endif
61 1.15 cgd
62 1.16 cgd #include <sys/mount.h>
63 1.16 cgd #include <sys/syscallargs.h>
64 1.22 christos
65 1.25 mycroft int selscan __P((struct proc *, fd_mask *, fd_mask *, int, register_t *));
66 1.28 mycroft int pollscan __P((struct proc *, struct pollfd *, int, register_t *));
67 1.22 christos
68 1.15 cgd /*
69 1.15 cgd * Read system call.
70 1.15 cgd */
71 1.15 cgd /* ARGSUSED */
72 1.22 christos int
73 1.21 mycroft sys_read(p, v, retval)
74 1.15 cgd struct proc *p;
75 1.20 thorpej void *v;
76 1.20 thorpej register_t *retval;
77 1.20 thorpej {
78 1.21 mycroft register struct sys_read_args /* {
79 1.16 cgd syscallarg(int) fd;
80 1.31 cgd syscallarg(void *) buf;
81 1.31 cgd syscallarg(size_t) nbyte;
82 1.20 thorpej } */ *uap = v;
83 1.34 mycroft int fd = SCARG(uap, fd);
84 1.15 cgd register struct file *fp;
85 1.15 cgd register struct filedesc *fdp = p->p_fd;
86 1.39 thorpej
87 1.39 thorpej if ((u_int)fd >= fdp->fd_nfiles ||
88 1.39 thorpej (fp = fdp->fd_ofiles[fd]) == NULL ||
89 1.39 thorpej (fp->f_flag & FREAD) == 0)
90 1.39 thorpej return (EBADF);
91 1.39 thorpej
92 1.39 thorpej return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
93 1.39 thorpej &fp->f_offset, FOF_UPDATE_OFFSET, retval));
94 1.39 thorpej }
95 1.39 thorpej
96 1.39 thorpej int
97 1.39 thorpej dofileread(p, fd, fp, buf, nbyte, offset, flags, retval)
98 1.39 thorpej struct proc *p;
99 1.39 thorpej int fd;
100 1.39 thorpej struct file *fp;
101 1.39 thorpej void *buf;
102 1.39 thorpej size_t nbyte;
103 1.39 thorpej off_t *offset;
104 1.39 thorpej int flags;
105 1.39 thorpej register_t *retval;
106 1.39 thorpej {
107 1.15 cgd struct uio auio;
108 1.15 cgd struct iovec aiov;
109 1.15 cgd long cnt, error = 0;
110 1.15 cgd #ifdef KTRACE
111 1.15 cgd struct iovec ktriov;
112 1.15 cgd #endif
113 1.15 cgd
114 1.39 thorpej aiov.iov_base = (caddr_t)buf;
115 1.39 thorpej aiov.iov_len = nbyte;
116 1.15 cgd auio.uio_iov = &aiov;
117 1.15 cgd auio.uio_iovcnt = 1;
118 1.39 thorpej auio.uio_resid = nbyte;
119 1.15 cgd auio.uio_rw = UIO_READ;
120 1.15 cgd auio.uio_segflg = UIO_USERSPACE;
121 1.15 cgd auio.uio_procp = p;
122 1.40 thorpej
123 1.40 thorpej /*
124 1.40 thorpej * Reads return ssize_t because -1 is returned on error. Therefore
125 1.40 thorpej * we must restrict the length to SSIZE_MAX to avoid garbage return
126 1.40 thorpej * values.
127 1.40 thorpej */
128 1.40 thorpej if (auio.uio_resid > SSIZE_MAX)
129 1.40 thorpej return (EINVAL);
130 1.40 thorpej
131 1.15 cgd #ifdef KTRACE
132 1.15 cgd /*
133 1.15 cgd * if tracing, save a copy of iovec
134 1.15 cgd */
135 1.15 cgd if (KTRPOINT(p, KTR_GENIO))
136 1.15 cgd ktriov = aiov;
137 1.15 cgd #endif
138 1.38 thorpej cnt = auio.uio_resid;
139 1.39 thorpej error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
140 1.22 christos if (error)
141 1.15 cgd if (auio.uio_resid != cnt && (error == ERESTART ||
142 1.15 cgd error == EINTR || error == EWOULDBLOCK))
143 1.15 cgd error = 0;
144 1.15 cgd cnt -= auio.uio_resid;
145 1.15 cgd #ifdef KTRACE
146 1.15 cgd if (KTRPOINT(p, KTR_GENIO) && error == 0)
147 1.34 mycroft ktrgenio(p->p_tracep, fd, UIO_READ, &ktriov, cnt, error);
148 1.15 cgd #endif
149 1.15 cgd *retval = cnt;
150 1.15 cgd return (error);
151 1.15 cgd }
152 1.15 cgd
153 1.15 cgd /*
154 1.15 cgd * Scatter read system call.
155 1.15 cgd */
156 1.22 christos int
157 1.21 mycroft sys_readv(p, v, retval)
158 1.15 cgd struct proc *p;
159 1.20 thorpej void *v;
160 1.20 thorpej register_t *retval;
161 1.20 thorpej {
162 1.21 mycroft register struct sys_readv_args /* {
163 1.16 cgd syscallarg(int) fd;
164 1.31 cgd syscallarg(const struct iovec *) iovp;
165 1.34 mycroft syscallarg(int) iovcnt;
166 1.20 thorpej } */ *uap = v;
167 1.34 mycroft int fd = SCARG(uap, fd);
168 1.15 cgd register struct file *fp;
169 1.15 cgd register struct filedesc *fdp = p->p_fd;
170 1.39 thorpej
171 1.39 thorpej if ((u_int)fd >= fdp->fd_nfiles ||
172 1.39 thorpej (fp = fdp->fd_ofiles[fd]) == NULL ||
173 1.39 thorpej (fp->f_flag & FREAD) == 0)
174 1.39 thorpej return (EBADF);
175 1.39 thorpej
176 1.39 thorpej return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
177 1.39 thorpej &fp->f_offset, FOF_UPDATE_OFFSET, retval));
178 1.39 thorpej }
179 1.39 thorpej
180 1.39 thorpej int
181 1.39 thorpej dofilereadv(p, fd, fp, iovp, iovcnt, offset, flags, retval)
182 1.39 thorpej struct proc *p;
183 1.39 thorpej int fd;
184 1.39 thorpej struct file *fp;
185 1.39 thorpej const struct iovec *iovp;
186 1.39 thorpej int iovcnt;
187 1.39 thorpej off_t *offset;
188 1.39 thorpej int flags;
189 1.39 thorpej register_t *retval;
190 1.39 thorpej {
191 1.15 cgd struct uio auio;
192 1.15 cgd register struct iovec *iov;
193 1.15 cgd struct iovec *needfree;
194 1.15 cgd struct iovec aiov[UIO_SMALLIOV];
195 1.15 cgd long i, cnt, error = 0;
196 1.15 cgd u_int iovlen;
197 1.15 cgd #ifdef KTRACE
198 1.15 cgd struct iovec *ktriov = NULL;
199 1.15 cgd #endif
200 1.15 cgd
201 1.15 cgd /* note: can't use iovlen until iovcnt is validated */
202 1.34 mycroft iovlen = iovcnt * sizeof (struct iovec);
203 1.34 mycroft if ((u_int)iovcnt > UIO_SMALLIOV) {
204 1.34 mycroft if ((u_int)iovcnt > UIO_MAXIOV)
205 1.15 cgd return (EINVAL);
206 1.15 cgd MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
207 1.15 cgd needfree = iov;
208 1.41 kleink } else if ((u_int)iovcnt > 0) {
209 1.15 cgd iov = aiov;
210 1.15 cgd needfree = NULL;
211 1.41 kleink } else
212 1.41 kleink return (EINVAL);
213 1.41 kleink
214 1.15 cgd auio.uio_iov = iov;
215 1.34 mycroft auio.uio_iovcnt = iovcnt;
216 1.15 cgd auio.uio_rw = UIO_READ;
217 1.15 cgd auio.uio_segflg = UIO_USERSPACE;
218 1.15 cgd auio.uio_procp = p;
219 1.39 thorpej error = copyin(iovp, iov, iovlen);
220 1.22 christos if (error)
221 1.15 cgd goto done;
222 1.15 cgd auio.uio_resid = 0;
223 1.34 mycroft for (i = 0; i < iovcnt; i++) {
224 1.15 cgd auio.uio_resid += iov->iov_len;
225 1.40 thorpej /*
226 1.40 thorpej * Reads return ssize_t because -1 is returned on error.
227 1.40 thorpej * Therefore we must restrict the length to SSIZE_MAX to
228 1.40 thorpej * avoid garbage return values.
229 1.40 thorpej */
230 1.40 thorpej if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
231 1.15 cgd error = EINVAL;
232 1.15 cgd goto done;
233 1.15 cgd }
234 1.15 cgd iov++;
235 1.15 cgd }
236 1.15 cgd #ifdef KTRACE
237 1.15 cgd /*
238 1.15 cgd * if tracing, save a copy of iovec
239 1.15 cgd */
240 1.15 cgd if (KTRPOINT(p, KTR_GENIO)) {
241 1.15 cgd MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
242 1.15 cgd bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
243 1.15 cgd }
244 1.15 cgd #endif
245 1.15 cgd cnt = auio.uio_resid;
246 1.39 thorpej error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
247 1.22 christos if (error)
248 1.15 cgd if (auio.uio_resid != cnt && (error == ERESTART ||
249 1.15 cgd error == EINTR || error == EWOULDBLOCK))
250 1.15 cgd error = 0;
251 1.15 cgd cnt -= auio.uio_resid;
252 1.15 cgd #ifdef KTRACE
253 1.34 mycroft if (KTRPOINT(p, KTR_GENIO))
254 1.34 mycroft if (error == 0) {
255 1.34 mycroft ktrgenio(p->p_tracep, fd, UIO_READ, ktriov, cnt,
256 1.34 mycroft error);
257 1.15 cgd FREE(ktriov, M_TEMP);
258 1.15 cgd }
259 1.15 cgd #endif
260 1.15 cgd *retval = cnt;
261 1.15 cgd done:
262 1.15 cgd if (needfree)
263 1.15 cgd FREE(needfree, M_IOV);
264 1.15 cgd return (error);
265 1.15 cgd }
266 1.15 cgd
267 1.15 cgd /*
268 1.15 cgd * Write system call
269 1.15 cgd */
270 1.22 christos int
271 1.21 mycroft sys_write(p, v, retval)
272 1.15 cgd struct proc *p;
273 1.20 thorpej void *v;
274 1.20 thorpej register_t *retval;
275 1.20 thorpej {
276 1.21 mycroft register struct sys_write_args /* {
277 1.16 cgd syscallarg(int) fd;
278 1.31 cgd syscallarg(const void *) buf;
279 1.31 cgd syscallarg(size_t) nbyte;
280 1.20 thorpej } */ *uap = v;
281 1.34 mycroft int fd = SCARG(uap, fd);
282 1.15 cgd register struct file *fp;
283 1.15 cgd register struct filedesc *fdp = p->p_fd;
284 1.39 thorpej
285 1.39 thorpej if ((u_int)fd >= fdp->fd_nfiles ||
286 1.39 thorpej (fp = fdp->fd_ofiles[fd]) == NULL ||
287 1.39 thorpej (fp->f_flag & FWRITE) == 0)
288 1.39 thorpej return (EBADF);
289 1.39 thorpej
290 1.39 thorpej return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
291 1.39 thorpej &fp->f_offset, FOF_UPDATE_OFFSET, retval));
292 1.39 thorpej }
293 1.39 thorpej
294 1.39 thorpej int
295 1.39 thorpej dofilewrite(p, fd, fp, buf, nbyte, offset, flags, retval)
296 1.39 thorpej struct proc *p;
297 1.39 thorpej int fd;
298 1.39 thorpej struct file *fp;
299 1.39 thorpej const void *buf;
300 1.39 thorpej size_t nbyte;
301 1.39 thorpej off_t *offset;
302 1.39 thorpej int flags;
303 1.39 thorpej register_t *retval;
304 1.39 thorpej {
305 1.15 cgd struct uio auio;
306 1.15 cgd struct iovec aiov;
307 1.15 cgd long cnt, error = 0;
308 1.15 cgd #ifdef KTRACE
309 1.15 cgd struct iovec ktriov;
310 1.15 cgd #endif
311 1.15 cgd
312 1.39 thorpej aiov.iov_base = (caddr_t)buf; /* XXX kills const */
313 1.39 thorpej aiov.iov_len = nbyte;
314 1.15 cgd auio.uio_iov = &aiov;
315 1.15 cgd auio.uio_iovcnt = 1;
316 1.39 thorpej auio.uio_resid = nbyte;
317 1.15 cgd auio.uio_rw = UIO_WRITE;
318 1.15 cgd auio.uio_segflg = UIO_USERSPACE;
319 1.15 cgd auio.uio_procp = p;
320 1.40 thorpej
321 1.40 thorpej /*
322 1.40 thorpej * Writes return ssize_t because -1 is returned on error. Therefore
323 1.40 thorpej * we must restrict the length to SSIZE_MAX to avoid garbage return
324 1.40 thorpej * values.
325 1.40 thorpej */
326 1.40 thorpej if (auio.uio_resid > SSIZE_MAX)
327 1.40 thorpej return (EINVAL);
328 1.40 thorpej
329 1.15 cgd #ifdef KTRACE
330 1.15 cgd /*
331 1.15 cgd * if tracing, save a copy of iovec
332 1.15 cgd */
333 1.15 cgd if (KTRPOINT(p, KTR_GENIO))
334 1.15 cgd ktriov = aiov;
335 1.15 cgd #endif
336 1.38 thorpej cnt = auio.uio_resid;
337 1.39 thorpej error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
338 1.22 christos if (error) {
339 1.15 cgd if (auio.uio_resid != cnt && (error == ERESTART ||
340 1.15 cgd error == EINTR || error == EWOULDBLOCK))
341 1.15 cgd error = 0;
342 1.15 cgd if (error == EPIPE)
343 1.15 cgd psignal(p, SIGPIPE);
344 1.15 cgd }
345 1.15 cgd cnt -= auio.uio_resid;
346 1.15 cgd #ifdef KTRACE
347 1.15 cgd if (KTRPOINT(p, KTR_GENIO) && error == 0)
348 1.34 mycroft ktrgenio(p->p_tracep, fd, UIO_WRITE, &ktriov, cnt, error);
349 1.15 cgd #endif
350 1.15 cgd *retval = cnt;
351 1.15 cgd return (error);
352 1.15 cgd }
353 1.15 cgd
354 1.15 cgd /*
355 1.15 cgd * Gather write system call
356 1.15 cgd */
357 1.22 christos int
358 1.21 mycroft sys_writev(p, v, retval)
359 1.15 cgd struct proc *p;
360 1.20 thorpej void *v;
361 1.20 thorpej register_t *retval;
362 1.20 thorpej {
363 1.21 mycroft register struct sys_writev_args /* {
364 1.16 cgd syscallarg(int) fd;
365 1.31 cgd syscallarg(const struct iovec *) iovp;
366 1.39 thorpej syscallarg(int) iovcnt;
367 1.20 thorpej } */ *uap = v;
368 1.34 mycroft int fd = SCARG(uap, fd);
369 1.15 cgd register struct file *fp;
370 1.15 cgd register struct filedesc *fdp = p->p_fd;
371 1.39 thorpej
372 1.39 thorpej if ((u_int)fd >= fdp->fd_nfiles ||
373 1.39 thorpej (fp = fdp->fd_ofiles[fd]) == NULL ||
374 1.39 thorpej (fp->f_flag & FWRITE) == 0)
375 1.39 thorpej return (EBADF);
376 1.39 thorpej
377 1.39 thorpej return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
378 1.39 thorpej &fp->f_offset, FOF_UPDATE_OFFSET, retval));
379 1.39 thorpej }
380 1.39 thorpej
381 1.39 thorpej int
382 1.39 thorpej dofilewritev(p, fd, fp, iovp, iovcnt, offset, flags, retval)
383 1.39 thorpej struct proc *p;
384 1.39 thorpej int fd;
385 1.39 thorpej struct file *fp;
386 1.39 thorpej const struct iovec *iovp;
387 1.39 thorpej int iovcnt;
388 1.39 thorpej off_t *offset;
389 1.39 thorpej int flags;
390 1.39 thorpej register_t *retval;
391 1.39 thorpej {
392 1.15 cgd struct uio auio;
393 1.15 cgd register struct iovec *iov;
394 1.15 cgd struct iovec *needfree;
395 1.15 cgd struct iovec aiov[UIO_SMALLIOV];
396 1.15 cgd long i, cnt, error = 0;
397 1.15 cgd u_int iovlen;
398 1.15 cgd #ifdef KTRACE
399 1.15 cgd struct iovec *ktriov = NULL;
400 1.15 cgd #endif
401 1.15 cgd
402 1.15 cgd /* note: can't use iovlen until iovcnt is validated */
403 1.34 mycroft iovlen = iovcnt * sizeof (struct iovec);
404 1.34 mycroft if ((u_int)iovcnt > UIO_SMALLIOV) {
405 1.34 mycroft if ((u_int)iovcnt > UIO_MAXIOV)
406 1.15 cgd return (EINVAL);
407 1.15 cgd MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
408 1.15 cgd needfree = iov;
409 1.41 kleink } else if ((u_int)iovcnt > 0) {
410 1.15 cgd iov = aiov;
411 1.15 cgd needfree = NULL;
412 1.41 kleink } else
413 1.41 kleink return (EINVAL);
414 1.41 kleink
415 1.15 cgd auio.uio_iov = iov;
416 1.34 mycroft auio.uio_iovcnt = iovcnt;
417 1.15 cgd auio.uio_rw = UIO_WRITE;
418 1.15 cgd auio.uio_segflg = UIO_USERSPACE;
419 1.15 cgd auio.uio_procp = p;
420 1.39 thorpej error = copyin(iovp, iov, iovlen);
421 1.22 christos if (error)
422 1.15 cgd goto done;
423 1.15 cgd auio.uio_resid = 0;
424 1.34 mycroft for (i = 0; i < iovcnt; i++) {
425 1.15 cgd auio.uio_resid += iov->iov_len;
426 1.40 thorpej /*
427 1.40 thorpej * Writes return ssize_t because -1 is returned on error.
428 1.40 thorpej * Therefore we must restrict the length to SSIZE_MAX to
429 1.40 thorpej * avoid garbage return values.
430 1.40 thorpej */
431 1.40 thorpej if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
432 1.15 cgd error = EINVAL;
433 1.15 cgd goto done;
434 1.15 cgd }
435 1.15 cgd iov++;
436 1.15 cgd }
437 1.15 cgd #ifdef KTRACE
438 1.15 cgd /*
439 1.15 cgd * if tracing, save a copy of iovec
440 1.15 cgd */
441 1.15 cgd if (KTRPOINT(p, KTR_GENIO)) {
442 1.15 cgd MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
443 1.15 cgd bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
444 1.15 cgd }
445 1.15 cgd #endif
446 1.15 cgd cnt = auio.uio_resid;
447 1.39 thorpej error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
448 1.22 christos if (error) {
449 1.15 cgd if (auio.uio_resid != cnt && (error == ERESTART ||
450 1.15 cgd error == EINTR || error == EWOULDBLOCK))
451 1.15 cgd error = 0;
452 1.15 cgd if (error == EPIPE)
453 1.15 cgd psignal(p, SIGPIPE);
454 1.15 cgd }
455 1.15 cgd cnt -= auio.uio_resid;
456 1.15 cgd #ifdef KTRACE
457 1.34 mycroft if (KTRPOINT(p, KTR_GENIO))
458 1.34 mycroft if (error == 0) {
459 1.34 mycroft ktrgenio(p->p_tracep, fd, UIO_WRITE, ktriov, cnt,
460 1.34 mycroft error);
461 1.15 cgd FREE(ktriov, M_TEMP);
462 1.15 cgd }
463 1.15 cgd #endif
464 1.15 cgd *retval = cnt;
465 1.15 cgd done:
466 1.15 cgd if (needfree)
467 1.15 cgd FREE(needfree, M_IOV);
468 1.15 cgd return (error);
469 1.15 cgd }
470 1.15 cgd
471 1.15 cgd /*
472 1.15 cgd * Ioctl system call
473 1.15 cgd */
474 1.15 cgd /* ARGSUSED */
475 1.22 christos int
476 1.21 mycroft sys_ioctl(p, v, retval)
477 1.15 cgd struct proc *p;
478 1.20 thorpej void *v;
479 1.20 thorpej register_t *retval;
480 1.20 thorpej {
481 1.21 mycroft register struct sys_ioctl_args /* {
482 1.16 cgd syscallarg(int) fd;
483 1.17 cgd syscallarg(u_long) com;
484 1.16 cgd syscallarg(caddr_t) data;
485 1.20 thorpej } */ *uap = v;
486 1.15 cgd register struct file *fp;
487 1.15 cgd register struct filedesc *fdp;
488 1.17 cgd register u_long com;
489 1.17 cgd register int error;
490 1.15 cgd register u_int size;
491 1.15 cgd caddr_t data, memp;
492 1.15 cgd int tmp;
493 1.15 cgd #define STK_PARAMS 128
494 1.15 cgd char stkbuf[STK_PARAMS];
495 1.15 cgd
496 1.15 cgd fdp = p->p_fd;
497 1.16 cgd if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
498 1.16 cgd (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
499 1.15 cgd return (EBADF);
500 1.15 cgd
501 1.15 cgd if ((fp->f_flag & (FREAD | FWRITE)) == 0)
502 1.15 cgd return (EBADF);
503 1.15 cgd
504 1.16 cgd switch (com = SCARG(uap, com)) {
505 1.15 cgd case FIONCLEX:
506 1.16 cgd fdp->fd_ofileflags[SCARG(uap, fd)] &= ~UF_EXCLOSE;
507 1.15 cgd return (0);
508 1.15 cgd case FIOCLEX:
509 1.16 cgd fdp->fd_ofileflags[SCARG(uap, fd)] |= UF_EXCLOSE;
510 1.15 cgd return (0);
511 1.15 cgd }
512 1.15 cgd
513 1.15 cgd /*
514 1.15 cgd * Interpret high order word to find amount of data to be
515 1.15 cgd * copied to/from the user's address space.
516 1.15 cgd */
517 1.15 cgd size = IOCPARM_LEN(com);
518 1.15 cgd if (size > IOCPARM_MAX)
519 1.15 cgd return (ENOTTY);
520 1.15 cgd memp = NULL;
521 1.15 cgd if (size > sizeof (stkbuf)) {
522 1.15 cgd memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
523 1.15 cgd data = memp;
524 1.15 cgd } else
525 1.15 cgd data = stkbuf;
526 1.15 cgd if (com&IOC_IN) {
527 1.15 cgd if (size) {
528 1.31 cgd error = copyin(SCARG(uap, data), data, size);
529 1.15 cgd if (error) {
530 1.15 cgd if (memp)
531 1.15 cgd free(memp, M_IOCTLOPS);
532 1.15 cgd return (error);
533 1.15 cgd }
534 1.15 cgd } else
535 1.16 cgd *(caddr_t *)data = SCARG(uap, data);
536 1.15 cgd } else if ((com&IOC_OUT) && size)
537 1.15 cgd /*
538 1.15 cgd * Zero the buffer so the user always
539 1.15 cgd * gets back something deterministic.
540 1.15 cgd */
541 1.15 cgd bzero(data, size);
542 1.15 cgd else if (com&IOC_VOID)
543 1.16 cgd *(caddr_t *)data = SCARG(uap, data);
544 1.15 cgd
545 1.15 cgd switch (com) {
546 1.15 cgd
547 1.15 cgd case FIONBIO:
548 1.22 christos if ((tmp = *(int *)data) != 0)
549 1.15 cgd fp->f_flag |= FNONBLOCK;
550 1.15 cgd else
551 1.15 cgd fp->f_flag &= ~FNONBLOCK;
552 1.15 cgd error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
553 1.15 cgd break;
554 1.15 cgd
555 1.15 cgd case FIOASYNC:
556 1.22 christos if ((tmp = *(int *)data) != 0)
557 1.15 cgd fp->f_flag |= FASYNC;
558 1.15 cgd else
559 1.15 cgd fp->f_flag &= ~FASYNC;
560 1.15 cgd error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
561 1.15 cgd break;
562 1.15 cgd
563 1.15 cgd case FIOSETOWN:
564 1.15 cgd tmp = *(int *)data;
565 1.15 cgd if (fp->f_type == DTYPE_SOCKET) {
566 1.15 cgd ((struct socket *)fp->f_data)->so_pgid = tmp;
567 1.15 cgd error = 0;
568 1.15 cgd break;
569 1.15 cgd }
570 1.15 cgd if (tmp <= 0) {
571 1.15 cgd tmp = -tmp;
572 1.15 cgd } else {
573 1.15 cgd struct proc *p1 = pfind(tmp);
574 1.15 cgd if (p1 == 0) {
575 1.15 cgd error = ESRCH;
576 1.15 cgd break;
577 1.15 cgd }
578 1.15 cgd tmp = p1->p_pgrp->pg_id;
579 1.15 cgd }
580 1.15 cgd error = (*fp->f_ops->fo_ioctl)
581 1.24 cgd (fp, TIOCSPGRP, (caddr_t)&tmp, p);
582 1.15 cgd break;
583 1.15 cgd
584 1.15 cgd case FIOGETOWN:
585 1.15 cgd if (fp->f_type == DTYPE_SOCKET) {
586 1.15 cgd error = 0;
587 1.15 cgd *(int *)data = ((struct socket *)fp->f_data)->so_pgid;
588 1.15 cgd break;
589 1.15 cgd }
590 1.17 cgd error = (*fp->f_ops->fo_ioctl)(fp, TIOCGPGRP, data, p);
591 1.15 cgd *(int *)data = -*(int *)data;
592 1.15 cgd break;
593 1.15 cgd
594 1.15 cgd default:
595 1.15 cgd error = (*fp->f_ops->fo_ioctl)(fp, com, data, p);
596 1.15 cgd /*
597 1.15 cgd * Copy any data to user, size was
598 1.15 cgd * already set and checked above.
599 1.15 cgd */
600 1.15 cgd if (error == 0 && (com&IOC_OUT) && size)
601 1.31 cgd error = copyout(data, SCARG(uap, data), size);
602 1.15 cgd break;
603 1.15 cgd }
604 1.15 cgd if (memp)
605 1.15 cgd free(memp, M_IOCTLOPS);
606 1.15 cgd return (error);
607 1.15 cgd }
608 1.15 cgd
609 1.15 cgd int selwait, nselcoll;
610 1.15 cgd
611 1.15 cgd /*
612 1.15 cgd * Select system call.
613 1.15 cgd */
614 1.22 christos int
615 1.21 mycroft sys_select(p, v, retval)
616 1.15 cgd register struct proc *p;
617 1.20 thorpej void *v;
618 1.20 thorpej register_t *retval;
619 1.20 thorpej {
620 1.21 mycroft register struct sys_select_args /* {
621 1.35 thorpej syscallarg(int) nd;
622 1.16 cgd syscallarg(fd_set *) in;
623 1.16 cgd syscallarg(fd_set *) ou;
624 1.16 cgd syscallarg(fd_set *) ex;
625 1.16 cgd syscallarg(struct timeval *) tv;
626 1.20 thorpej } */ *uap = v;
627 1.25 mycroft caddr_t bits;
628 1.26 cgd char smallbits[howmany(FD_SETSIZE, NFDBITS) * sizeof(fd_mask) * 6];
629 1.15 cgd struct timeval atv;
630 1.15 cgd int s, ncoll, error = 0, timo;
631 1.27 mycroft size_t ni;
632 1.15 cgd
633 1.35 thorpej if (SCARG(uap, nd) < 0)
634 1.35 thorpej return (EINVAL);
635 1.16 cgd if (SCARG(uap, nd) > p->p_fd->fd_nfiles) {
636 1.16 cgd /* forgiving; slightly wrong */
637 1.16 cgd SCARG(uap, nd) = p->p_fd->fd_nfiles;
638 1.16 cgd }
639 1.16 cgd ni = howmany(SCARG(uap, nd), NFDBITS) * sizeof(fd_mask);
640 1.27 mycroft if (ni * 6 > sizeof(smallbits))
641 1.25 mycroft bits = malloc(ni * 6, M_TEMP, M_WAITOK);
642 1.25 mycroft else
643 1.26 cgd bits = smallbits;
644 1.15 cgd
645 1.15 cgd #define getbits(name, x) \
646 1.25 mycroft if (SCARG(uap, name)) { \
647 1.31 cgd error = copyin(SCARG(uap, name), bits + ni * x, ni); \
648 1.25 mycroft if (error) \
649 1.25 mycroft goto done; \
650 1.25 mycroft } else \
651 1.25 mycroft bzero(bits + ni * x, ni);
652 1.15 cgd getbits(in, 0);
653 1.15 cgd getbits(ou, 1);
654 1.15 cgd getbits(ex, 2);
655 1.15 cgd #undef getbits
656 1.15 cgd
657 1.16 cgd if (SCARG(uap, tv)) {
658 1.31 cgd error = copyin(SCARG(uap, tv), (caddr_t)&atv,
659 1.15 cgd sizeof (atv));
660 1.15 cgd if (error)
661 1.15 cgd goto done;
662 1.15 cgd if (itimerfix(&atv)) {
663 1.15 cgd error = EINVAL;
664 1.15 cgd goto done;
665 1.15 cgd }
666 1.15 cgd s = splclock();
667 1.19 mycroft timeradd(&atv, &time, &atv);
668 1.15 cgd timo = hzto(&atv);
669 1.15 cgd /*
670 1.15 cgd * Avoid inadvertently sleeping forever.
671 1.15 cgd */
672 1.15 cgd if (timo == 0)
673 1.15 cgd timo = 1;
674 1.15 cgd splx(s);
675 1.15 cgd } else
676 1.15 cgd timo = 0;
677 1.15 cgd retry:
678 1.15 cgd ncoll = nselcoll;
679 1.15 cgd p->p_flag |= P_SELECT;
680 1.25 mycroft error = selscan(p, (fd_mask *)(bits + ni * 0),
681 1.25 mycroft (fd_mask *)(bits + ni * 3), SCARG(uap, nd), retval);
682 1.15 cgd if (error || *retval)
683 1.15 cgd goto done;
684 1.15 cgd s = splhigh();
685 1.27 mycroft if (timo && timercmp(&time, &atv, >=)) {
686 1.15 cgd splx(s);
687 1.15 cgd goto done;
688 1.15 cgd }
689 1.15 cgd if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
690 1.15 cgd splx(s);
691 1.15 cgd goto retry;
692 1.15 cgd }
693 1.15 cgd p->p_flag &= ~P_SELECT;
694 1.15 cgd error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
695 1.15 cgd splx(s);
696 1.15 cgd if (error == 0)
697 1.15 cgd goto retry;
698 1.15 cgd done:
699 1.15 cgd p->p_flag &= ~P_SELECT;
700 1.15 cgd /* select is not restarted after signals... */
701 1.15 cgd if (error == ERESTART)
702 1.15 cgd error = EINTR;
703 1.15 cgd if (error == EWOULDBLOCK)
704 1.15 cgd error = 0;
705 1.27 mycroft if (error == 0) {
706 1.15 cgd #define putbits(name, x) \
707 1.27 mycroft if (SCARG(uap, name)) { \
708 1.31 cgd error = copyout(bits + ni * x, SCARG(uap, name), ni); \
709 1.27 mycroft if (error) \
710 1.27 mycroft goto out; \
711 1.27 mycroft }
712 1.25 mycroft putbits(in, 3);
713 1.25 mycroft putbits(ou, 4);
714 1.25 mycroft putbits(ex, 5);
715 1.15 cgd #undef putbits
716 1.15 cgd }
717 1.27 mycroft out:
718 1.27 mycroft if (ni * 6 > sizeof(smallbits))
719 1.25 mycroft free(bits, M_TEMP);
720 1.15 cgd return (error);
721 1.15 cgd }
722 1.15 cgd
723 1.22 christos int
724 1.25 mycroft selscan(p, ibitp, obitp, nfd, retval)
725 1.15 cgd struct proc *p;
726 1.25 mycroft fd_mask *ibitp, *obitp;
727 1.16 cgd int nfd;
728 1.16 cgd register_t *retval;
729 1.15 cgd {
730 1.15 cgd register struct filedesc *fdp = p->p_fd;
731 1.15 cgd register int msk, i, j, fd;
732 1.25 mycroft register fd_mask ibits, obits;
733 1.15 cgd struct file *fp;
734 1.15 cgd int n = 0;
735 1.28 mycroft static int flag[3] = { POLLRDNORM | POLLHUP | POLLERR,
736 1.28 mycroft POLLWRNORM | POLLHUP | POLLERR,
737 1.28 mycroft POLLRDBAND };
738 1.15 cgd
739 1.15 cgd for (msk = 0; msk < 3; msk++) {
740 1.15 cgd for (i = 0; i < nfd; i += NFDBITS) {
741 1.25 mycroft ibits = *ibitp++;
742 1.25 mycroft obits = 0;
743 1.25 mycroft while ((j = ffs(ibits)) && (fd = i + --j) < nfd) {
744 1.25 mycroft ibits &= ~(1 << j);
745 1.15 cgd fp = fdp->fd_ofiles[fd];
746 1.15 cgd if (fp == NULL)
747 1.15 cgd return (EBADF);
748 1.28 mycroft if ((*fp->f_ops->fo_poll)(fp, flag[msk], p)) {
749 1.25 mycroft obits |= (1 << j);
750 1.15 cgd n++;
751 1.15 cgd }
752 1.15 cgd }
753 1.25 mycroft *obitp++ = obits;
754 1.15 cgd }
755 1.15 cgd }
756 1.15 cgd *retval = n;
757 1.15 cgd return (0);
758 1.15 cgd }
759 1.15 cgd
760 1.28 mycroft /*
761 1.28 mycroft * Poll system call.
762 1.28 mycroft */
763 1.28 mycroft int
764 1.28 mycroft sys_poll(p, v, retval)
765 1.28 mycroft register struct proc *p;
766 1.28 mycroft void *v;
767 1.28 mycroft register_t *retval;
768 1.28 mycroft {
769 1.28 mycroft register struct sys_poll_args /* {
770 1.28 mycroft syscallarg(struct pollfd *) fds;
771 1.29 mycroft syscallarg(u_int) nfds;
772 1.28 mycroft syscallarg(int) timeout;
773 1.28 mycroft } */ *uap = v;
774 1.28 mycroft caddr_t bits;
775 1.28 mycroft char smallbits[32 * sizeof(struct pollfd)];
776 1.28 mycroft struct timeval atv;
777 1.28 mycroft int s, ncoll, error = 0, timo;
778 1.28 mycroft size_t ni;
779 1.28 mycroft
780 1.28 mycroft if (SCARG(uap, nfds) > p->p_fd->fd_nfiles) {
781 1.28 mycroft /* forgiving; slightly wrong */
782 1.28 mycroft SCARG(uap, nfds) = p->p_fd->fd_nfiles;
783 1.28 mycroft }
784 1.28 mycroft ni = SCARG(uap, nfds) * sizeof(struct pollfd);
785 1.28 mycroft if (ni > sizeof(smallbits))
786 1.28 mycroft bits = malloc(ni, M_TEMP, M_WAITOK);
787 1.28 mycroft else
788 1.28 mycroft bits = smallbits;
789 1.28 mycroft
790 1.31 cgd error = copyin(SCARG(uap, fds), bits, ni);
791 1.28 mycroft if (error)
792 1.28 mycroft goto done;
793 1.28 mycroft
794 1.30 mycroft if (SCARG(uap, timeout) != INFTIM) {
795 1.28 mycroft atv.tv_sec = SCARG(uap, timeout) / 1000;
796 1.28 mycroft atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
797 1.28 mycroft if (itimerfix(&atv)) {
798 1.28 mycroft error = EINVAL;
799 1.28 mycroft goto done;
800 1.28 mycroft }
801 1.28 mycroft s = splclock();
802 1.28 mycroft timeradd(&atv, &time, &atv);
803 1.28 mycroft timo = hzto(&atv);
804 1.28 mycroft /*
805 1.28 mycroft * Avoid inadvertently sleeping forever.
806 1.28 mycroft */
807 1.28 mycroft if (timo == 0)
808 1.28 mycroft timo = 1;
809 1.28 mycroft splx(s);
810 1.28 mycroft } else
811 1.28 mycroft timo = 0;
812 1.28 mycroft retry:
813 1.28 mycroft ncoll = nselcoll;
814 1.28 mycroft p->p_flag |= P_SELECT;
815 1.28 mycroft error = pollscan(p, (struct pollfd *)bits, SCARG(uap, nfds), retval);
816 1.28 mycroft if (error || *retval)
817 1.28 mycroft goto done;
818 1.28 mycroft s = splhigh();
819 1.28 mycroft if (timo && timercmp(&time, &atv, >=)) {
820 1.28 mycroft splx(s);
821 1.28 mycroft goto done;
822 1.28 mycroft }
823 1.28 mycroft if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
824 1.28 mycroft splx(s);
825 1.28 mycroft goto retry;
826 1.28 mycroft }
827 1.28 mycroft p->p_flag &= ~P_SELECT;
828 1.28 mycroft error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
829 1.28 mycroft splx(s);
830 1.28 mycroft if (error == 0)
831 1.28 mycroft goto retry;
832 1.28 mycroft done:
833 1.28 mycroft p->p_flag &= ~P_SELECT;
834 1.28 mycroft /* poll is not restarted after signals... */
835 1.28 mycroft if (error == ERESTART)
836 1.28 mycroft error = EINTR;
837 1.28 mycroft if (error == EWOULDBLOCK)
838 1.28 mycroft error = 0;
839 1.28 mycroft if (error == 0) {
840 1.31 cgd error = copyout(bits, SCARG(uap, fds), ni);
841 1.28 mycroft if (error)
842 1.28 mycroft goto out;
843 1.28 mycroft }
844 1.28 mycroft out:
845 1.28 mycroft if (ni > sizeof(smallbits))
846 1.28 mycroft free(bits, M_TEMP);
847 1.28 mycroft return (error);
848 1.28 mycroft }
849 1.28 mycroft
850 1.28 mycroft int
851 1.28 mycroft pollscan(p, fds, nfd, retval)
852 1.28 mycroft struct proc *p;
853 1.28 mycroft struct pollfd *fds;
854 1.28 mycroft int nfd;
855 1.28 mycroft register_t *retval;
856 1.28 mycroft {
857 1.28 mycroft register struct filedesc *fdp = p->p_fd;
858 1.28 mycroft int i;
859 1.28 mycroft struct file *fp;
860 1.28 mycroft int n = 0;
861 1.28 mycroft
862 1.28 mycroft for (i = 0; i < nfd; i++, fds++) {
863 1.33 mrg if ((u_int)fds->fd >= fdp->fd_nfiles) {
864 1.28 mycroft fds->revents = POLLNVAL;
865 1.28 mycroft n++;
866 1.28 mycroft } else {
867 1.32 mrg fp = fdp->fd_ofiles[fds->fd];
868 1.32 mrg if (fp == 0) {
869 1.32 mrg fds->revents = POLLNVAL;
870 1.28 mycroft n++;
871 1.32 mrg } else {
872 1.32 mrg fds->revents = (*fp->f_ops->fo_poll)(fp,
873 1.32 mrg fds->events | POLLERR | POLLHUP, p);
874 1.32 mrg if (fds->revents != 0)
875 1.32 mrg n++;
876 1.32 mrg }
877 1.28 mycroft }
878 1.28 mycroft }
879 1.28 mycroft *retval = n;
880 1.28 mycroft return (0);
881 1.28 mycroft }
882 1.28 mycroft
883 1.15 cgd /*ARGSUSED*/
884 1.22 christos int
885 1.28 mycroft seltrue(dev, events, p)
886 1.15 cgd dev_t dev;
887 1.28 mycroft int events;
888 1.15 cgd struct proc *p;
889 1.15 cgd {
890 1.15 cgd
891 1.28 mycroft return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
892 1.15 cgd }
893 1.15 cgd
894 1.15 cgd /*
895 1.15 cgd * Record a select request.
896 1.15 cgd */
897 1.15 cgd void
898 1.15 cgd selrecord(selector, sip)
899 1.15 cgd struct proc *selector;
900 1.15 cgd struct selinfo *sip;
901 1.15 cgd {
902 1.15 cgd struct proc *p;
903 1.15 cgd pid_t mypid;
904 1.15 cgd
905 1.15 cgd mypid = selector->p_pid;
906 1.15 cgd if (sip->si_pid == mypid)
907 1.15 cgd return;
908 1.15 cgd if (sip->si_pid && (p = pfind(sip->si_pid)) &&
909 1.15 cgd p->p_wchan == (caddr_t)&selwait)
910 1.15 cgd sip->si_flags |= SI_COLL;
911 1.15 cgd else
912 1.15 cgd sip->si_pid = mypid;
913 1.15 cgd }
914 1.15 cgd
915 1.15 cgd /*
916 1.15 cgd * Do a wakeup when a selectable event occurs.
917 1.15 cgd */
918 1.15 cgd void
919 1.15 cgd selwakeup(sip)
920 1.15 cgd register struct selinfo *sip;
921 1.15 cgd {
922 1.15 cgd register struct proc *p;
923 1.15 cgd int s;
924 1.15 cgd
925 1.15 cgd if (sip->si_pid == 0)
926 1.15 cgd return;
927 1.15 cgd if (sip->si_flags & SI_COLL) {
928 1.15 cgd nselcoll++;
929 1.15 cgd sip->si_flags &= ~SI_COLL;
930 1.15 cgd wakeup((caddr_t)&selwait);
931 1.15 cgd }
932 1.15 cgd p = pfind(sip->si_pid);
933 1.15 cgd sip->si_pid = 0;
934 1.15 cgd if (p != NULL) {
935 1.15 cgd s = splhigh();
936 1.15 cgd if (p->p_wchan == (caddr_t)&selwait) {
937 1.15 cgd if (p->p_stat == SSLEEP)
938 1.15 cgd setrunnable(p);
939 1.15 cgd else
940 1.15 cgd unsleep(p);
941 1.15 cgd } else if (p->p_flag & P_SELECT)
942 1.15 cgd p->p_flag &= ~P_SELECT;
943 1.15 cgd splx(s);
944 1.15 cgd }
945 1.15 cgd }
946