No empty .Rs/.Re
[netbsd-mini2440.git] / sys / kern / sys_generic.c
bloba56056decae686464e708aff6e0f403451b1b1de
1 /* $NetBSD: sys_generic.c,v 1.123 2009/05/24 21:41:26 ad Exp $ */
3 /*-
4 * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
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.
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.
33 * Copyright (c) 1982, 1986, 1989, 1993
34 * The Regents of the University of California. All rights reserved.
35 * (c) UNIX System Laboratories, Inc.
36 * All or some portions of this file are derived from material licensed
37 * to the University of California by American Telephone and Telegraph
38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39 * the permission of UNIX System Laboratories, Inc.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
65 * @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
69 * System calls relating to files.
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: sys_generic.c,v 1.123 2009/05/24 21:41:26 ad Exp $");
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/filedesc.h>
78 #include <sys/ioctl.h>
79 #include <sys/file.h>
80 #include <sys/proc.h>
81 #include <sys/socketvar.h>
82 #include <sys/signalvar.h>
83 #include <sys/uio.h>
84 #include <sys/kernel.h>
85 #include <sys/stat.h>
86 #include <sys/kmem.h>
87 #include <sys/poll.h>
88 #include <sys/vnode.h>
89 #include <sys/mount.h>
90 #include <sys/syscallargs.h>
91 #include <sys/ktrace.h>
92 #include <sys/atomic.h>
94 #include <uvm/uvm_extern.h>
97 * Read system call.
99 /* ARGSUSED */
101 sys_read(struct lwp *l, const struct sys_read_args *uap, register_t *retval)
103 /* {
104 syscallarg(int) fd;
105 syscallarg(void *) buf;
106 syscallarg(size_t) nbyte;
107 } */
108 file_t *fp;
109 int fd;
111 fd = SCARG(uap, fd);
113 if ((fp = fd_getfile(fd)) == NULL)
114 return (EBADF);
116 if ((fp->f_flag & FREAD) == 0) {
117 fd_putfile(fd);
118 return (EBADF);
121 /* dofileread() will unuse the descriptor for us */
122 return (dofileread(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
123 &fp->f_offset, FOF_UPDATE_OFFSET, retval));
127 dofileread(int fd, struct file *fp, void *buf, size_t nbyte,
128 off_t *offset, int flags, register_t *retval)
130 struct iovec aiov;
131 struct uio auio;
132 size_t cnt;
133 int error;
134 lwp_t *l;
136 l = curlwp;
138 aiov.iov_base = (void *)buf;
139 aiov.iov_len = nbyte;
140 auio.uio_iov = &aiov;
141 auio.uio_iovcnt = 1;
142 auio.uio_resid = nbyte;
143 auio.uio_rw = UIO_READ;
144 auio.uio_vmspace = l->l_proc->p_vmspace;
147 * Reads return ssize_t because -1 is returned on error. Therefore
148 * we must restrict the length to SSIZE_MAX to avoid garbage return
149 * values.
151 if (auio.uio_resid > SSIZE_MAX) {
152 error = EINVAL;
153 goto out;
156 cnt = auio.uio_resid;
157 error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
158 if (error)
159 if (auio.uio_resid != cnt && (error == ERESTART ||
160 error == EINTR || error == EWOULDBLOCK))
161 error = 0;
162 cnt -= auio.uio_resid;
163 ktrgenio(fd, UIO_READ, buf, cnt, error);
164 *retval = cnt;
165 out:
166 fd_putfile(fd);
167 return (error);
171 * Scatter read system call.
174 sys_readv(struct lwp *l, const struct sys_readv_args *uap, register_t *retval)
176 /* {
177 syscallarg(int) fd;
178 syscallarg(const struct iovec *) iovp;
179 syscallarg(int) iovcnt;
180 } */
182 return do_filereadv(SCARG(uap, fd), SCARG(uap, iovp),
183 SCARG(uap, iovcnt), NULL, FOF_UPDATE_OFFSET, retval);
187 do_filereadv(int fd, const struct iovec *iovp, int iovcnt,
188 off_t *offset, int flags, register_t *retval)
190 struct uio auio;
191 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
192 int i, error;
193 size_t cnt;
194 u_int iovlen;
195 struct file *fp;
196 struct iovec *ktriov = NULL;
198 if (iovcnt == 0)
199 return EINVAL;
201 if ((fp = fd_getfile(fd)) == NULL)
202 return EBADF;
204 if ((fp->f_flag & FREAD) == 0) {
205 fd_putfile(fd);
206 return EBADF;
209 if (offset == NULL)
210 offset = &fp->f_offset;
211 else {
212 struct vnode *vp = fp->f_data;
213 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
214 error = ESPIPE;
215 goto out;
218 * Test that the device is seekable ?
219 * XXX This works because no file systems actually
220 * XXX take any action on the seek operation.
222 error = VOP_SEEK(vp, fp->f_offset, *offset, fp->f_cred);
223 if (error != 0)
224 goto out;
227 iovlen = iovcnt * sizeof(struct iovec);
228 if (flags & FOF_IOV_SYSSPACE)
229 iov = __UNCONST(iovp);
230 else {
231 iov = aiov;
232 if ((u_int)iovcnt > UIO_SMALLIOV) {
233 if ((u_int)iovcnt > IOV_MAX) {
234 error = EINVAL;
235 goto out;
237 iov = kmem_alloc(iovlen, KM_SLEEP);
238 if (iov == NULL) {
239 error = ENOMEM;
240 goto out;
242 needfree = iov;
244 error = copyin(iovp, iov, iovlen);
245 if (error)
246 goto done;
249 auio.uio_iov = iov;
250 auio.uio_iovcnt = iovcnt;
251 auio.uio_rw = UIO_READ;
252 auio.uio_vmspace = curproc->p_vmspace;
254 auio.uio_resid = 0;
255 for (i = 0; i < iovcnt; i++, iov++) {
256 auio.uio_resid += iov->iov_len;
258 * Reads return ssize_t because -1 is returned on error.
259 * Therefore we must restrict the length to SSIZE_MAX to
260 * avoid garbage return values.
262 if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
263 error = EINVAL;
264 goto done;
269 * if tracing, save a copy of iovec
271 if (ktrpoint(KTR_GENIO)) {
272 ktriov = kmem_alloc(iovlen, KM_SLEEP);
273 if (ktriov != NULL)
274 memcpy(ktriov, auio.uio_iov, iovlen);
277 cnt = auio.uio_resid;
278 error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
279 if (error)
280 if (auio.uio_resid != cnt && (error == ERESTART ||
281 error == EINTR || error == EWOULDBLOCK))
282 error = 0;
283 cnt -= auio.uio_resid;
284 *retval = cnt;
286 if (ktriov != NULL) {
287 ktrgeniov(fd, UIO_READ, ktriov, cnt, error);
288 kmem_free(ktriov, iovlen);
291 done:
292 if (needfree)
293 kmem_free(needfree, iovlen);
294 out:
295 fd_putfile(fd);
296 return (error);
300 * Write system call
303 sys_write(struct lwp *l, const struct sys_write_args *uap, register_t *retval)
305 /* {
306 syscallarg(int) fd;
307 syscallarg(const void *) buf;
308 syscallarg(size_t) nbyte;
309 } */
310 file_t *fp;
311 int fd;
313 fd = SCARG(uap, fd);
315 if ((fp = fd_getfile(fd)) == NULL)
316 return (EBADF);
318 if ((fp->f_flag & FWRITE) == 0) {
319 fd_putfile(fd);
320 return (EBADF);
323 /* dofilewrite() will unuse the descriptor for us */
324 return (dofilewrite(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
325 &fp->f_offset, FOF_UPDATE_OFFSET, retval));
329 dofilewrite(int fd, struct file *fp, const void *buf,
330 size_t nbyte, off_t *offset, int flags, register_t *retval)
332 struct iovec aiov;
333 struct uio auio;
334 size_t cnt;
335 int error;
337 aiov.iov_base = __UNCONST(buf); /* XXXUNCONST kills const */
338 aiov.iov_len = nbyte;
339 auio.uio_iov = &aiov;
340 auio.uio_iovcnt = 1;
341 auio.uio_resid = nbyte;
342 auio.uio_rw = UIO_WRITE;
343 auio.uio_vmspace = curproc->p_vmspace;
346 * Writes return ssize_t because -1 is returned on error. Therefore
347 * we must restrict the length to SSIZE_MAX to avoid garbage return
348 * values.
350 if (auio.uio_resid > SSIZE_MAX) {
351 error = EINVAL;
352 goto out;
355 cnt = auio.uio_resid;
356 error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
357 if (error) {
358 if (auio.uio_resid != cnt && (error == ERESTART ||
359 error == EINTR || error == EWOULDBLOCK))
360 error = 0;
361 if (error == EPIPE) {
362 mutex_enter(proc_lock);
363 psignal(curproc, SIGPIPE);
364 mutex_exit(proc_lock);
367 cnt -= auio.uio_resid;
368 ktrgenio(fd, UIO_WRITE, buf, cnt, error);
369 *retval = cnt;
370 out:
371 fd_putfile(fd);
372 return (error);
376 * Gather write system call
379 sys_writev(struct lwp *l, const struct sys_writev_args *uap, register_t *retval)
381 /* {
382 syscallarg(int) fd;
383 syscallarg(const struct iovec *) iovp;
384 syscallarg(int) iovcnt;
385 } */
387 return do_filewritev(SCARG(uap, fd), SCARG(uap, iovp),
388 SCARG(uap, iovcnt), NULL, FOF_UPDATE_OFFSET, retval);
392 do_filewritev(int fd, const struct iovec *iovp, int iovcnt,
393 off_t *offset, int flags, register_t *retval)
395 struct uio auio;
396 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
397 int i, error;
398 size_t cnt;
399 u_int iovlen;
400 struct file *fp;
401 struct iovec *ktriov = NULL;
403 if (iovcnt == 0)
404 return EINVAL;
406 if ((fp = fd_getfile(fd)) == NULL)
407 return EBADF;
409 if ((fp->f_flag & FWRITE) == 0) {
410 fd_putfile(fd);
411 return EBADF;
414 if (offset == NULL)
415 offset = &fp->f_offset;
416 else {
417 struct vnode *vp = fp->f_data;
418 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
419 error = ESPIPE;
420 goto out;
423 * Test that the device is seekable ?
424 * XXX This works because no file systems actually
425 * XXX take any action on the seek operation.
427 error = VOP_SEEK(vp, fp->f_offset, *offset, fp->f_cred);
428 if (error != 0)
429 goto out;
432 iovlen = iovcnt * sizeof(struct iovec);
433 if (flags & FOF_IOV_SYSSPACE)
434 iov = __UNCONST(iovp);
435 else {
436 iov = aiov;
437 if ((u_int)iovcnt > UIO_SMALLIOV) {
438 if ((u_int)iovcnt > IOV_MAX) {
439 error = EINVAL;
440 goto out;
442 iov = kmem_alloc(iovlen, KM_SLEEP);
443 if (iov == NULL) {
444 error = ENOMEM;
445 goto out;
447 needfree = iov;
449 error = copyin(iovp, iov, iovlen);
450 if (error)
451 goto done;
454 auio.uio_iov = iov;
455 auio.uio_iovcnt = iovcnt;
456 auio.uio_rw = UIO_WRITE;
457 auio.uio_vmspace = curproc->p_vmspace;
459 auio.uio_resid = 0;
460 for (i = 0; i < iovcnt; i++, iov++) {
461 auio.uio_resid += iov->iov_len;
463 * Writes return ssize_t because -1 is returned on error.
464 * Therefore we must restrict the length to SSIZE_MAX to
465 * avoid garbage return values.
467 if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
468 error = EINVAL;
469 goto done;
474 * if tracing, save a copy of iovec
476 if (ktrpoint(KTR_GENIO)) {
477 ktriov = kmem_alloc(iovlen, KM_SLEEP);
478 if (ktriov != NULL)
479 memcpy(ktriov, auio.uio_iov, iovlen);
482 cnt = auio.uio_resid;
483 error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
484 if (error) {
485 if (auio.uio_resid != cnt && (error == ERESTART ||
486 error == EINTR || error == EWOULDBLOCK))
487 error = 0;
488 if (error == EPIPE) {
489 mutex_enter(proc_lock);
490 psignal(curproc, SIGPIPE);
491 mutex_exit(proc_lock);
494 cnt -= auio.uio_resid;
495 *retval = cnt;
497 if (ktriov != NULL) {
498 ktrgeniov(fd, UIO_WRITE, ktriov, cnt, error);
499 kmem_free(ktriov, iovlen);
502 done:
503 if (needfree)
504 kmem_free(needfree, iovlen);
505 out:
506 fd_putfile(fd);
507 return (error);
511 * Ioctl system call
513 /* ARGSUSED */
515 sys_ioctl(struct lwp *l, const struct sys_ioctl_args *uap, register_t *retval)
517 /* {
518 syscallarg(int) fd;
519 syscallarg(u_long) com;
520 syscallarg(void *) data;
521 } */
522 struct file *fp;
523 proc_t *p;
524 struct filedesc *fdp;
525 u_long com;
526 int error;
527 u_int size;
528 void *data, *memp;
529 #define STK_PARAMS 128
530 u_long stkbuf[STK_PARAMS/sizeof(u_long)];
531 fdfile_t *ff;
533 error = 0;
534 p = l->l_proc;
535 fdp = p->p_fd;
537 if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
538 return (EBADF);
540 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
541 error = EBADF;
542 com = 0;
543 goto out;
546 ff = fdp->fd_dt->dt_ff[SCARG(uap, fd)];
547 switch (com = SCARG(uap, com)) {
548 case FIONCLEX:
549 ff->ff_exclose = false;
550 goto out;
552 case FIOCLEX:
553 ff->ff_exclose = true;
554 fdp->fd_exclose = true;
555 goto out;
559 * Interpret high order word to find amount of data to be
560 * copied to/from the user's address space.
562 size = IOCPARM_LEN(com);
563 if (size > IOCPARM_MAX) {
564 error = ENOTTY;
565 goto out;
567 memp = NULL;
568 if ((com >> IOCPARM_SHIFT) == 0) {
569 /* UNIX-style ioctl. */
570 data = SCARG(uap, data);
571 } else {
572 if (size > sizeof(stkbuf)) {
573 memp = kmem_alloc(size, KM_SLEEP);
574 data = memp;
575 } else {
576 data = (void *)stkbuf;
578 if (com&IOC_IN) {
579 if (size) {
580 error = copyin(SCARG(uap, data), data, size);
581 if (error) {
582 if (memp) {
583 kmem_free(memp, size);
585 goto out;
587 ktrgenio(SCARG(uap, fd), UIO_WRITE,
588 SCARG(uap, data), size, 0);
589 } else {
590 *(void **)data = SCARG(uap, data);
592 } else if ((com&IOC_OUT) && size) {
594 * Zero the buffer so the user always
595 * gets back something deterministic.
597 memset(data, 0, size);
598 } else if (com&IOC_VOID) {
599 *(void **)data = SCARG(uap, data);
603 switch (com) {
605 case FIONBIO:
606 /* XXX Code block is not atomic */
607 if (*(int *)data != 0)
608 atomic_or_uint(&fp->f_flag, FNONBLOCK);
609 else
610 atomic_and_uint(&fp->f_flag, ~FNONBLOCK);
611 error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, data);
612 break;
614 case FIOASYNC:
615 /* XXX Code block is not atomic */
616 if (*(int *)data != 0)
617 atomic_or_uint(&fp->f_flag, FASYNC);
618 else
619 atomic_and_uint(&fp->f_flag, ~FASYNC);
620 error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, data);
621 break;
623 default:
624 error = (*fp->f_ops->fo_ioctl)(fp, com, data);
626 * Copy any data to user, size was
627 * already set and checked above.
629 if (error == 0 && (com&IOC_OUT) && size) {
630 error = copyout(data, SCARG(uap, data), size);
631 ktrgenio(SCARG(uap, fd), UIO_READ, SCARG(uap, data),
632 size, error);
634 break;
636 if (memp)
637 kmem_free(memp, size);
638 out:
639 fd_putfile(SCARG(uap, fd));
640 switch (error) {
641 case -1:
642 printf("sys_ioctl: _IO%s%s('%c', %lu, %lu) returned -1: "
643 "pid=%d comm=%s\n",
644 (com & IOC_IN) ? "W" : "", (com & IOC_OUT) ? "R" : "",
645 (char)IOCGROUP(com), (com & 0xff), IOCPARM_LEN(com),
646 p->p_pid, p->p_comm);
647 /* FALLTHROUGH */
648 case EPASSTHROUGH:
649 error = ENOTTY;
650 /* FALLTHROUGH */
651 default:
652 return (error);