sys_generic.c revision 1.127.2.1 1 /* $NetBSD: sys_generic.c,v 1.127.2.1 2012/04/17 00:08:28 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
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.
40 *
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.
52 *
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.
64 *
65 * @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
66 */
67
68 /*
69 * System calls relating to files.
70 */
71
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: sys_generic.c,v 1.127.2.1 2012/04/17 00:08:28 yamt Exp $");
74
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>
93 #include <sys/disklabel.h>
94
95 /*
96 * Read system call.
97 */
98 /* ARGSUSED */
99 int
100 sys_read(struct lwp *l, const struct sys_read_args *uap, register_t *retval)
101 {
102 /* {
103 syscallarg(int) fd;
104 syscallarg(void *) buf;
105 syscallarg(size_t) nbyte;
106 } */
107 file_t *fp;
108 int fd;
109
110 fd = SCARG(uap, fd);
111
112 if ((fp = fd_getfile(fd)) == NULL)
113 return (EBADF);
114
115 if ((fp->f_flag & FREAD) == 0) {
116 fd_putfile(fd);
117 return (EBADF);
118 }
119
120 /* dofileread() will unuse the descriptor for us */
121 return (dofileread(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
122 &fp->f_offset, FOF_UPDATE_OFFSET, retval));
123 }
124
125 int
126 dofileread(int fd, struct file *fp, void *buf, size_t nbyte,
127 off_t *offset, int flags, register_t *retval)
128 {
129 struct iovec aiov;
130 struct uio auio;
131 size_t cnt;
132 int error;
133 lwp_t *l;
134
135 l = curlwp;
136
137 aiov.iov_base = (void *)buf;
138 aiov.iov_len = nbyte;
139 auio.uio_iov = &aiov;
140 auio.uio_iovcnt = 1;
141 auio.uio_resid = nbyte;
142 auio.uio_rw = UIO_READ;
143 auio.uio_vmspace = l->l_proc->p_vmspace;
144
145 /*
146 * Reads return ssize_t because -1 is returned on error. Therefore
147 * we must restrict the length to SSIZE_MAX to avoid garbage return
148 * values.
149 */
150 if (auio.uio_resid > SSIZE_MAX) {
151 error = EINVAL;
152 goto out;
153 }
154
155 cnt = auio.uio_resid;
156 error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
157 if (error)
158 if (auio.uio_resid != cnt && (error == ERESTART ||
159 error == EINTR || error == EWOULDBLOCK))
160 error = 0;
161 cnt -= auio.uio_resid;
162 ktrgenio(fd, UIO_READ, buf, cnt, error);
163 *retval = cnt;
164 out:
165 fd_putfile(fd);
166 return (error);
167 }
168
169 /*
170 * Scatter read system call.
171 */
172 int
173 sys_readv(struct lwp *l, const struct sys_readv_args *uap, register_t *retval)
174 {
175 /* {
176 syscallarg(int) fd;
177 syscallarg(const struct iovec *) iovp;
178 syscallarg(int) iovcnt;
179 } */
180
181 return do_filereadv(SCARG(uap, fd), SCARG(uap, iovp),
182 SCARG(uap, iovcnt), NULL, FOF_UPDATE_OFFSET, retval);
183 }
184
185 int
186 do_filereadv(int fd, const struct iovec *iovp, int iovcnt,
187 off_t *offset, int flags, register_t *retval)
188 {
189 struct uio auio;
190 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
191 int i, error;
192 size_t cnt;
193 u_int iovlen;
194 struct file *fp;
195 struct iovec *ktriov = NULL;
196
197 if (iovcnt == 0)
198 return EINVAL;
199
200 if ((fp = fd_getfile(fd)) == NULL)
201 return EBADF;
202
203 if ((fp->f_flag & FREAD) == 0) {
204 fd_putfile(fd);
205 return EBADF;
206 }
207
208 if (offset == NULL)
209 offset = &fp->f_offset;
210 else {
211 struct vnode *vp = fp->f_data;
212 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
213 error = ESPIPE;
214 goto out;
215 }
216 /*
217 * Test that the device is seekable ?
218 * XXX This works because no file systems actually
219 * XXX take any action on the seek operation.
220 */
221 error = VOP_SEEK(vp, fp->f_offset, *offset, fp->f_cred);
222 if (error != 0)
223 goto out;
224 }
225
226 iovlen = iovcnt * sizeof(struct iovec);
227 if (flags & FOF_IOV_SYSSPACE)
228 iov = __UNCONST(iovp);
229 else {
230 iov = aiov;
231 if ((u_int)iovcnt > UIO_SMALLIOV) {
232 if ((u_int)iovcnt > IOV_MAX) {
233 error = EINVAL;
234 goto out;
235 }
236 iov = kmem_alloc(iovlen, KM_SLEEP);
237 if (iov == NULL) {
238 error = ENOMEM;
239 goto out;
240 }
241 needfree = iov;
242 }
243 error = copyin(iovp, iov, iovlen);
244 if (error)
245 goto done;
246 }
247
248 auio.uio_iov = iov;
249 auio.uio_iovcnt = iovcnt;
250 auio.uio_rw = UIO_READ;
251 auio.uio_vmspace = curproc->p_vmspace;
252
253 auio.uio_resid = 0;
254 for (i = 0; i < iovcnt; i++, iov++) {
255 auio.uio_resid += iov->iov_len;
256 /*
257 * Reads return ssize_t because -1 is returned on error.
258 * Therefore we must restrict the length to SSIZE_MAX to
259 * avoid garbage return values.
260 */
261 if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
262 error = EINVAL;
263 goto done;
264 }
265 }
266
267 /*
268 * if tracing, save a copy of iovec
269 */
270 if (ktrpoint(KTR_GENIO)) {
271 ktriov = kmem_alloc(iovlen, KM_SLEEP);
272 if (ktriov != NULL)
273 memcpy(ktriov, auio.uio_iov, iovlen);
274 }
275
276 cnt = auio.uio_resid;
277 error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
278 if (error)
279 if (auio.uio_resid != cnt && (error == ERESTART ||
280 error == EINTR || error == EWOULDBLOCK))
281 error = 0;
282 cnt -= auio.uio_resid;
283 *retval = cnt;
284
285 if (ktriov != NULL) {
286 ktrgeniov(fd, UIO_READ, ktriov, cnt, error);
287 kmem_free(ktriov, iovlen);
288 }
289
290 done:
291 if (needfree)
292 kmem_free(needfree, iovlen);
293 out:
294 fd_putfile(fd);
295 return (error);
296 }
297
298 /*
299 * Write system call
300 */
301 int
302 sys_write(struct lwp *l, const struct sys_write_args *uap, register_t *retval)
303 {
304 /* {
305 syscallarg(int) fd;
306 syscallarg(const void *) buf;
307 syscallarg(size_t) nbyte;
308 } */
309 file_t *fp;
310 int fd;
311
312 fd = SCARG(uap, fd);
313
314 if ((fp = fd_getfile(fd)) == NULL)
315 return (EBADF);
316
317 if ((fp->f_flag & FWRITE) == 0) {
318 fd_putfile(fd);
319 return (EBADF);
320 }
321
322 /* dofilewrite() will unuse the descriptor for us */
323 return (dofilewrite(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
324 &fp->f_offset, FOF_UPDATE_OFFSET, retval));
325 }
326
327 int
328 dofilewrite(int fd, struct file *fp, const void *buf,
329 size_t nbyte, off_t *offset, int flags, register_t *retval)
330 {
331 struct iovec aiov;
332 struct uio auio;
333 size_t cnt;
334 int error;
335
336 aiov.iov_base = __UNCONST(buf); /* XXXUNCONST kills const */
337 aiov.iov_len = nbyte;
338 auio.uio_iov = &aiov;
339 auio.uio_iovcnt = 1;
340 auio.uio_resid = nbyte;
341 auio.uio_rw = UIO_WRITE;
342 auio.uio_vmspace = curproc->p_vmspace;
343
344 /*
345 * Writes return ssize_t because -1 is returned on error. Therefore
346 * we must restrict the length to SSIZE_MAX to avoid garbage return
347 * values.
348 */
349 if (auio.uio_resid > SSIZE_MAX) {
350 error = EINVAL;
351 goto out;
352 }
353
354 cnt = auio.uio_resid;
355 error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
356 if (error) {
357 if (auio.uio_resid != cnt && (error == ERESTART ||
358 error == EINTR || error == EWOULDBLOCK))
359 error = 0;
360 if (error == EPIPE && !(fp->f_flag & FNOSIGPIPE)) {
361 mutex_enter(proc_lock);
362 psignal(curproc, SIGPIPE);
363 mutex_exit(proc_lock);
364 }
365 }
366 cnt -= auio.uio_resid;
367 ktrgenio(fd, UIO_WRITE, buf, cnt, error);
368 *retval = cnt;
369 out:
370 fd_putfile(fd);
371 return (error);
372 }
373
374 /*
375 * Gather write system call
376 */
377 int
378 sys_writev(struct lwp *l, const struct sys_writev_args *uap, register_t *retval)
379 {
380 /* {
381 syscallarg(int) fd;
382 syscallarg(const struct iovec *) iovp;
383 syscallarg(int) iovcnt;
384 } */
385
386 return do_filewritev(SCARG(uap, fd), SCARG(uap, iovp),
387 SCARG(uap, iovcnt), NULL, FOF_UPDATE_OFFSET, retval);
388 }
389
390 int
391 do_filewritev(int fd, const struct iovec *iovp, int iovcnt,
392 off_t *offset, int flags, register_t *retval)
393 {
394 struct uio auio;
395 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
396 int i, error;
397 size_t cnt;
398 u_int iovlen;
399 struct file *fp;
400 struct iovec *ktriov = NULL;
401
402 if (iovcnt == 0)
403 return EINVAL;
404
405 if ((fp = fd_getfile(fd)) == NULL)
406 return EBADF;
407
408 if ((fp->f_flag & FWRITE) == 0) {
409 fd_putfile(fd);
410 return EBADF;
411 }
412
413 if (offset == NULL)
414 offset = &fp->f_offset;
415 else {
416 struct vnode *vp = fp->f_data;
417 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
418 error = ESPIPE;
419 goto out;
420 }
421 /*
422 * Test that the device is seekable ?
423 * XXX This works because no file systems actually
424 * XXX take any action on the seek operation.
425 */
426 error = VOP_SEEK(vp, fp->f_offset, *offset, fp->f_cred);
427 if (error != 0)
428 goto out;
429 }
430
431 iovlen = iovcnt * sizeof(struct iovec);
432 if (flags & FOF_IOV_SYSSPACE)
433 iov = __UNCONST(iovp);
434 else {
435 iov = aiov;
436 if ((u_int)iovcnt > UIO_SMALLIOV) {
437 if ((u_int)iovcnt > IOV_MAX) {
438 error = EINVAL;
439 goto out;
440 }
441 iov = kmem_alloc(iovlen, KM_SLEEP);
442 if (iov == NULL) {
443 error = ENOMEM;
444 goto out;
445 }
446 needfree = iov;
447 }
448 error = copyin(iovp, iov, iovlen);
449 if (error)
450 goto done;
451 }
452
453 auio.uio_iov = iov;
454 auio.uio_iovcnt = iovcnt;
455 auio.uio_rw = UIO_WRITE;
456 auio.uio_vmspace = curproc->p_vmspace;
457
458 auio.uio_resid = 0;
459 for (i = 0; i < iovcnt; i++, iov++) {
460 auio.uio_resid += iov->iov_len;
461 /*
462 * Writes return ssize_t because -1 is returned on error.
463 * Therefore we must restrict the length to SSIZE_MAX to
464 * avoid garbage return values.
465 */
466 if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
467 error = EINVAL;
468 goto done;
469 }
470 }
471
472 /*
473 * if tracing, save a copy of iovec
474 */
475 if (ktrpoint(KTR_GENIO)) {
476 ktriov = kmem_alloc(iovlen, KM_SLEEP);
477 if (ktriov != NULL)
478 memcpy(ktriov, auio.uio_iov, iovlen);
479 }
480
481 cnt = auio.uio_resid;
482 error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
483 if (error) {
484 if (auio.uio_resid != cnt && (error == ERESTART ||
485 error == EINTR || error == EWOULDBLOCK))
486 error = 0;
487 if (error == EPIPE && !(fp->f_flag & FNOSIGPIPE)) {
488 mutex_enter(proc_lock);
489 psignal(curproc, SIGPIPE);
490 mutex_exit(proc_lock);
491 }
492 }
493 cnt -= auio.uio_resid;
494 *retval = cnt;
495
496 if (ktriov != NULL) {
497 ktrgeniov(fd, UIO_WRITE, ktriov, cnt, error);
498 kmem_free(ktriov, iovlen);
499 }
500
501 done:
502 if (needfree)
503 kmem_free(needfree, iovlen);
504 out:
505 fd_putfile(fd);
506 return (error);
507 }
508
509 /*
510 * Ioctl system call
511 */
512 /* ARGSUSED */
513 int
514 sys_ioctl(struct lwp *l, const struct sys_ioctl_args *uap, register_t *retval)
515 {
516 /* {
517 syscallarg(int) fd;
518 syscallarg(u_long) com;
519 syscallarg(void *) data;
520 } */
521 struct file *fp;
522 proc_t *p;
523 u_long com;
524 int error;
525 size_t size, alloc_size;
526 void *data, *memp;
527 #define STK_PARAMS 128
528 u_long stkbuf[STK_PARAMS/sizeof(u_long)];
529
530 memp = NULL;
531 alloc_size = 0;
532 error = 0;
533 p = l->l_proc;
534
535 if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
536 return (EBADF);
537
538 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
539 error = EBADF;
540 com = 0;
541 goto out;
542 }
543
544 switch (com = SCARG(uap, com)) {
545 case FIONCLEX:
546 case FIOCLEX:
547 fd_set_exclose(l, SCARG(uap, fd), com == FIOCLEX);
548 goto out;
549 }
550
551 /*
552 * Interpret high order word to find amount of data to be
553 * copied to/from the user's address space.
554 */
555 size = IOCPARM_LEN(com);
556 alloc_size = size;
557
558 /*
559 * The disklabel is now padded to a multiple of 8 bytes however the old
560 * disklabel on 32bit platforms wasn't. This leaves a difference in
561 * size of 4 bytes between the two but are otherwise identical.
562 * To deal with this, we allocate enough space for the new disklabel
563 * but only copyin/out the smaller amount.
564 */
565 if (IOCGROUP(com) == 'd') {
566 u_long ncom = com ^ (DIOCGDINFO ^ DIOCGDINFO32);
567 switch (ncom) {
568 case DIOCGDINFO:
569 case DIOCWDINFO:
570 case DIOCSDINFO:
571 case DIOCGDEFLABEL:
572 com = ncom;
573 if (IOCPARM_LEN(DIOCGDINFO32) < IOCPARM_LEN(DIOCGDINFO))
574 alloc_size = IOCPARM_LEN(DIOCGDINFO);
575 break;
576 }
577 }
578 if (size > IOCPARM_MAX) {
579 error = ENOTTY;
580 goto out;
581 }
582 memp = NULL;
583 if ((com >> IOCPARM_SHIFT) == 0) {
584 /* UNIX-style ioctl. */
585 data = SCARG(uap, data);
586 } else {
587 if (alloc_size > sizeof(stkbuf)) {
588 memp = kmem_alloc(alloc_size, KM_SLEEP);
589 data = memp;
590 } else {
591 data = (void *)stkbuf;
592 }
593 if (com&IOC_IN) {
594 if (size) {
595 error = copyin(SCARG(uap, data), data, size);
596 if (error) {
597 goto out;
598 }
599 /*
600 * The data between size and alloc_size has
601 * not been overwritten. It shouldn't matter
602 * but let's clear that anyway.
603 */
604 if (__predict_false(size < alloc_size)) {
605 memset((char *)data+size, 0,
606 alloc_size - size);
607 }
608 ktrgenio(SCARG(uap, fd), UIO_WRITE,
609 SCARG(uap, data), size, 0);
610 } else {
611 *(void **)data = SCARG(uap, data);
612 }
613 } else if ((com&IOC_OUT) && size) {
614 /*
615 * Zero the buffer so the user always
616 * gets back something deterministic.
617 */
618 memset(data, 0, size);
619 } else if (com&IOC_VOID) {
620 *(void **)data = SCARG(uap, data);
621 }
622 }
623
624 switch (com) {
625
626 case FIONBIO:
627 /* XXX Code block is not atomic */
628 if (*(int *)data != 0)
629 atomic_or_uint(&fp->f_flag, FNONBLOCK);
630 else
631 atomic_and_uint(&fp->f_flag, ~FNONBLOCK);
632 error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, data);
633 break;
634
635 case FIOASYNC:
636 /* XXX Code block is not atomic */
637 if (*(int *)data != 0)
638 atomic_or_uint(&fp->f_flag, FASYNC);
639 else
640 atomic_and_uint(&fp->f_flag, ~FASYNC);
641 error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, data);
642 break;
643
644 default:
645 error = (*fp->f_ops->fo_ioctl)(fp, com, data);
646 /*
647 * Copy any data to user, size was
648 * already set and checked above.
649 */
650 if (error == 0 && (com&IOC_OUT) && size) {
651 error = copyout(data, SCARG(uap, data), size);
652 ktrgenio(SCARG(uap, fd), UIO_READ, SCARG(uap, data),
653 size, error);
654 }
655 break;
656 }
657 out:
658 if (memp)
659 kmem_free(memp, alloc_size);
660 fd_putfile(SCARG(uap, fd));
661 switch (error) {
662 case -1:
663 printf("sys_ioctl: _IO%s%s('%c', %lu, %lu) returned -1: "
664 "pid=%d comm=%s\n",
665 (com & IOC_IN) ? "W" : "", (com & IOC_OUT) ? "R" : "",
666 (char)IOCGROUP(com), (com & 0xff), IOCPARM_LEN(com),
667 p->p_pid, p->p_comm);
668 /* FALLTHROUGH */
669 case EPASSTHROUGH:
670 error = ENOTTY;
671 /* FALLTHROUGH */
672 default:
673 return (error);
674 }
675 }
676