sys_descrip.c revision 1.21 1 /* $NetBSD: sys_descrip.c,v 1.21 2011/06/12 03:35:56 rmind Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Copyright (c) 1982, 1986, 1989, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
63 */
64
65 /*
66 * System calls on descriptors.
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: sys_descrip.c,v 1.21 2011/06/12 03:35:56 rmind Exp $");
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/filedesc.h>
75 #include <sys/kernel.h>
76 #include <sys/vnode.h>
77 #include <sys/proc.h>
78 #include <sys/file.h>
79 #include <sys/namei.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
82 #include <sys/stat.h>
83 #include <sys/ioctl.h>
84 #include <sys/fcntl.h>
85 #include <sys/kmem.h>
86 #include <sys/pool.h>
87 #include <sys/syslog.h>
88 #include <sys/unistd.h>
89 #include <sys/resourcevar.h>
90 #include <sys/conf.h>
91 #include <sys/event.h>
92 #include <sys/kauth.h>
93 #include <sys/atomic.h>
94 #include <sys/mount.h>
95 #include <sys/syscallargs.h>
96
97 #include <uvm/uvm_readahead.h>
98
99 /*
100 * Duplicate a file descriptor.
101 */
102 int
103 sys_dup(struct lwp *l, const struct sys_dup_args *uap, register_t *retval)
104 {
105 /* {
106 syscallarg(int) fd;
107 } */
108 int new, error, old;
109 file_t *fp;
110
111 old = SCARG(uap, fd);
112
113 if ((fp = fd_getfile(old)) == NULL) {
114 return EBADF;
115 }
116 error = fd_dup(fp, 0, &new, false);
117 fd_putfile(old);
118 *retval = new;
119 return error;
120 }
121
122 /*
123 * Duplicate a file descriptor to a particular value.
124 */
125 int
126 sys_dup2(struct lwp *l, const struct sys_dup2_args *uap, register_t *retval)
127 {
128 /* {
129 syscallarg(int) from;
130 syscallarg(int) to;
131 } */
132 int old, new, error;
133 file_t *fp;
134
135 old = SCARG(uap, from);
136 new = SCARG(uap, to);
137
138 if ((fp = fd_getfile(old)) == NULL) {
139 return EBADF;
140 }
141 mutex_enter(&fp->f_lock);
142 fp->f_count++;
143 mutex_exit(&fp->f_lock);
144 fd_putfile(old);
145
146 if ((u_int)new >= curproc->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
147 (u_int)new >= maxfiles) {
148 error = EBADF;
149 } else if (old == new) {
150 error = 0;
151 } else {
152 error = fd_dup2(fp, new);
153 }
154 closef(fp);
155 *retval = new;
156
157 return error;
158 }
159
160 /*
161 * fcntl call which is being passed to the file's fs.
162 */
163 static int
164 fcntl_forfs(int fd, file_t *fp, int cmd, void *arg)
165 {
166 int error;
167 u_int size;
168 void *data, *memp;
169 #define STK_PARAMS 128
170 char stkbuf[STK_PARAMS];
171
172 if ((fp->f_flag & (FREAD | FWRITE)) == 0)
173 return (EBADF);
174
175 /*
176 * Interpret high order word to find amount of data to be
177 * copied to/from the user's address space.
178 */
179 size = (size_t)F_PARAM_LEN(cmd);
180 if (size > F_PARAM_MAX)
181 return (EINVAL);
182 memp = NULL;
183 if (size > sizeof(stkbuf)) {
184 memp = kmem_alloc(size, KM_SLEEP);
185 data = memp;
186 } else
187 data = stkbuf;
188 if (cmd & F_FSIN) {
189 if (size) {
190 error = copyin(arg, data, size);
191 if (error) {
192 if (memp)
193 kmem_free(memp, size);
194 return (error);
195 }
196 } else
197 *(void **)data = arg;
198 } else if ((cmd & F_FSOUT) != 0 && size != 0) {
199 /*
200 * Zero the buffer so the user always
201 * gets back something deterministic.
202 */
203 memset(data, 0, size);
204 } else if (cmd & F_FSVOID)
205 *(void **)data = arg;
206
207
208 error = (*fp->f_ops->fo_fcntl)(fp, cmd, data);
209
210 /*
211 * Copy any data to user, size was
212 * already set and checked above.
213 */
214 if (error == 0 && (cmd & F_FSOUT) && size)
215 error = copyout(data, arg, size);
216 if (memp)
217 kmem_free(memp, size);
218 return (error);
219 }
220
221 int
222 do_fcntl_lock(int fd, int cmd, struct flock *fl)
223 {
224 file_t *fp;
225 vnode_t *vp;
226 proc_t *p;
227 int error, flg;
228
229 if ((fp = fd_getfile(fd)) == NULL)
230 return EBADF;
231 if (fp->f_type != DTYPE_VNODE) {
232 fd_putfile(fd);
233 return EINVAL;
234 }
235 vp = fp->f_data;
236 if (fl->l_whence == SEEK_CUR)
237 fl->l_start += fp->f_offset;
238
239 flg = F_POSIX;
240 p = curproc;
241
242 switch (cmd) {
243 case F_SETLKW:
244 flg |= F_WAIT;
245 /* Fall into F_SETLK */
246
247 case F_SETLK:
248 switch (fl->l_type) {
249 case F_RDLCK:
250 if ((fp->f_flag & FREAD) == 0) {
251 error = EBADF;
252 break;
253 }
254 if ((p->p_flag & PK_ADVLOCK) == 0) {
255 mutex_enter(p->p_lock);
256 p->p_flag |= PK_ADVLOCK;
257 mutex_exit(p->p_lock);
258 }
259 error = VOP_ADVLOCK(vp, p, F_SETLK, fl, flg);
260 break;
261
262 case F_WRLCK:
263 if ((fp->f_flag & FWRITE) == 0) {
264 error = EBADF;
265 break;
266 }
267 if ((p->p_flag & PK_ADVLOCK) == 0) {
268 mutex_enter(p->p_lock);
269 p->p_flag |= PK_ADVLOCK;
270 mutex_exit(p->p_lock);
271 }
272 error = VOP_ADVLOCK(vp, p, F_SETLK, fl, flg);
273 break;
274
275 case F_UNLCK:
276 error = VOP_ADVLOCK(vp, p, F_UNLCK, fl, F_POSIX);
277 break;
278
279 default:
280 error = EINVAL;
281 break;
282 }
283 break;
284
285 case F_GETLK:
286 if (fl->l_type != F_RDLCK &&
287 fl->l_type != F_WRLCK &&
288 fl->l_type != F_UNLCK) {
289 error = EINVAL;
290 break;
291 }
292 error = VOP_ADVLOCK(vp, p, F_GETLK, fl, F_POSIX);
293 break;
294
295 default:
296 error = EINVAL;
297 break;
298 }
299
300 fd_putfile(fd);
301 return error;
302 }
303
304 /*
305 * The file control system call.
306 */
307 int
308 sys_fcntl(struct lwp *l, const struct sys_fcntl_args *uap, register_t *retval)
309 {
310 /* {
311 syscallarg(int) fd;
312 syscallarg(int) cmd;
313 syscallarg(void *) arg;
314 } */
315 int fd, i, tmp, error, cmd, newmin;
316 filedesc_t *fdp;
317 file_t *fp;
318 struct flock fl;
319
320 fd = SCARG(uap, fd);
321 cmd = SCARG(uap, cmd);
322 fdp = l->l_fd;
323 error = 0;
324
325 switch (cmd) {
326 case F_CLOSEM:
327 if (fd < 0)
328 return EBADF;
329 while ((i = fdp->fd_lastfile) >= fd) {
330 if (fd_getfile(i) == NULL) {
331 /* Another thread has updated. */
332 continue;
333 }
334 fd_close(i);
335 }
336 return 0;
337
338 case F_MAXFD:
339 *retval = fdp->fd_lastfile;
340 return 0;
341
342 case F_SETLKW:
343 case F_SETLK:
344 case F_GETLK:
345 error = copyin(SCARG(uap, arg), &fl, sizeof(fl));
346 if (error)
347 return error;
348 error = do_fcntl_lock(fd, cmd, &fl);
349 if (cmd == F_GETLK && error == 0)
350 error = copyout(&fl, SCARG(uap, arg), sizeof(fl));
351 return error;
352
353 default:
354 /* Handled below */
355 break;
356 }
357
358 if ((fp = fd_getfile(fd)) == NULL)
359 return (EBADF);
360
361 if ((cmd & F_FSCTL)) {
362 error = fcntl_forfs(fd, fp, cmd, SCARG(uap, arg));
363 fd_putfile(fd);
364 return error;
365 }
366
367 switch (cmd) {
368 case F_DUPFD:
369 newmin = (long)SCARG(uap, arg);
370 if ((u_int)newmin >=
371 l->l_proc->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
372 (u_int)newmin >= maxfiles) {
373 fd_putfile(fd);
374 return EINVAL;
375 }
376 error = fd_dup(fp, newmin, &i, false);
377 *retval = i;
378 break;
379
380 case F_GETFD:
381 *retval = fdp->fd_dt->dt_ff[fd]->ff_exclose;
382 break;
383
384 case F_SETFD:
385 fd_set_exclose(l, fd,
386 ((long)SCARG(uap, arg) & FD_CLOEXEC) != 0);
387 break;
388
389 case F_GETFL:
390 *retval = OFLAGS(fp->f_flag);
391 break;
392
393 case F_SETFL:
394 /* XXX not guaranteed to be atomic. */
395 tmp = FFLAGS((long)SCARG(uap, arg)) & FCNTLFLAGS;
396 error = (*fp->f_ops->fo_fcntl)(fp, F_SETFL, &tmp);
397 if (error)
398 break;
399 i = tmp ^ fp->f_flag;
400 if (i & FNONBLOCK) {
401 int flgs = tmp & FNONBLOCK;
402 error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, &flgs);
403 if (error) {
404 (*fp->f_ops->fo_fcntl)(fp, F_SETFL,
405 &fp->f_flag);
406 break;
407 }
408 }
409 if (i & FASYNC) {
410 int flgs = tmp & FASYNC;
411 error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, &flgs);
412 if (error) {
413 if (i & FNONBLOCK) {
414 tmp = fp->f_flag & FNONBLOCK;
415 (void)(*fp->f_ops->fo_ioctl)(fp,
416 FIONBIO, &tmp);
417 }
418 (*fp->f_ops->fo_fcntl)(fp, F_SETFL,
419 &fp->f_flag);
420 break;
421 }
422 }
423 fp->f_flag = (fp->f_flag & ~FCNTLFLAGS) | tmp;
424 break;
425
426 case F_GETOWN:
427 error = (*fp->f_ops->fo_ioctl)(fp, FIOGETOWN, &tmp);
428 *retval = tmp;
429 break;
430
431 case F_SETOWN:
432 tmp = (int)(uintptr_t) SCARG(uap, arg);
433 error = (*fp->f_ops->fo_ioctl)(fp, FIOSETOWN, &tmp);
434 break;
435
436 default:
437 error = EINVAL;
438 }
439
440 fd_putfile(fd);
441 return (error);
442 }
443
444 /*
445 * Close a file descriptor.
446 */
447 int
448 sys_close(struct lwp *l, const struct sys_close_args *uap, register_t *retval)
449 {
450 /* {
451 syscallarg(int) fd;
452 } */
453
454 if (fd_getfile(SCARG(uap, fd)) == NULL) {
455 return EBADF;
456 }
457 return fd_close(SCARG(uap, fd));
458 }
459
460 /*
461 * Return status information about a file descriptor.
462 * Common function for compat code.
463 */
464 int
465 do_sys_fstat(int fd, struct stat *sb)
466 {
467 file_t *fp;
468 int error;
469
470 if ((fp = fd_getfile(fd)) == NULL) {
471 return EBADF;
472 }
473 error = (*fp->f_ops->fo_stat)(fp, sb);
474 fd_putfile(fd);
475
476 return error;
477 }
478
479 /*
480 * Return status information about a file descriptor.
481 */
482 int
483 sys___fstat50(struct lwp *l, const struct sys___fstat50_args *uap,
484 register_t *retval)
485 {
486 /* {
487 syscallarg(int) fd;
488 syscallarg(struct stat *) sb;
489 } */
490 struct stat sb;
491 int error;
492
493 error = do_sys_fstat(SCARG(uap, fd), &sb);
494 if (error == 0) {
495 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
496 }
497 return error;
498 }
499
500 /*
501 * Return pathconf information about a file descriptor.
502 */
503 int
504 sys_fpathconf(struct lwp *l, const struct sys_fpathconf_args *uap,
505 register_t *retval)
506 {
507 /* {
508 syscallarg(int) fd;
509 syscallarg(int) name;
510 } */
511 int fd, error;
512 file_t *fp;
513
514 fd = SCARG(uap, fd);
515 error = 0;
516
517 if ((fp = fd_getfile(fd)) == NULL) {
518 return (EBADF);
519 }
520 switch (fp->f_type) {
521 case DTYPE_SOCKET:
522 case DTYPE_PIPE:
523 if (SCARG(uap, name) != _PC_PIPE_BUF)
524 error = EINVAL;
525 else
526 *retval = PIPE_BUF;
527 break;
528
529 case DTYPE_VNODE:
530 error = VOP_PATHCONF(fp->f_data, SCARG(uap, name), retval);
531 break;
532
533 case DTYPE_KQUEUE:
534 error = EINVAL;
535 break;
536
537 default:
538 error = EOPNOTSUPP;
539 break;
540 }
541
542 fd_putfile(fd);
543 return (error);
544 }
545
546 /*
547 * Apply an advisory lock on a file descriptor.
548 *
549 * Just attempt to get a record lock of the requested type on
550 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
551 */
552 /* ARGSUSED */
553 int
554 sys_flock(struct lwp *l, const struct sys_flock_args *uap, register_t *retval)
555 {
556 /* {
557 syscallarg(int) fd;
558 syscallarg(int) how;
559 } */
560 int fd, how, error;
561 file_t *fp;
562 vnode_t *vp;
563 struct flock lf;
564 proc_t *p;
565
566 fd = SCARG(uap, fd);
567 how = SCARG(uap, how);
568 error = 0;
569
570 if ((fp = fd_getfile(fd)) == NULL) {
571 return EBADF;
572 }
573 if (fp->f_type != DTYPE_VNODE) {
574 fd_putfile(fd);
575 return EOPNOTSUPP;
576 }
577
578 vp = fp->f_data;
579 lf.l_whence = SEEK_SET;
580 lf.l_start = 0;
581 lf.l_len = 0;
582
583 switch (how & ~LOCK_NB) {
584 case LOCK_UN:
585 lf.l_type = F_UNLCK;
586 atomic_and_uint(&fp->f_flag, ~FHASLOCK);
587 error = VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
588 fd_putfile(fd);
589 return error;
590 case LOCK_EX:
591 lf.l_type = F_WRLCK;
592 break;
593 case LOCK_SH:
594 lf.l_type = F_RDLCK;
595 break;
596 default:
597 fd_putfile(fd);
598 return EINVAL;
599 }
600
601 atomic_or_uint(&fp->f_flag, FHASLOCK);
602 p = curproc;
603 if (how & LOCK_NB) {
604 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, F_FLOCK);
605 } else {
606 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, F_FLOCK|F_WAIT);
607 }
608 fd_putfile(fd);
609 return error;
610 }
611
612 int
613 do_posix_fadvise(int fd, off_t offset, off_t len, int advice)
614 {
615 file_t *fp;
616 vnode_t *vp;
617 off_t endoffset;
618 int error;
619
620 CTASSERT(POSIX_FADV_NORMAL == UVM_ADV_NORMAL);
621 CTASSERT(POSIX_FADV_RANDOM == UVM_ADV_RANDOM);
622 CTASSERT(POSIX_FADV_SEQUENTIAL == UVM_ADV_SEQUENTIAL);
623
624 if (len == 0) {
625 endoffset = INT64_MAX;
626 } else if (len > 0 && (INT64_MAX - offset) >= len) {
627 endoffset = offset + len;
628 } else {
629 return EINVAL;
630 }
631 if ((fp = fd_getfile(fd)) == NULL) {
632 return EBADF;
633 }
634 if (fp->f_type != DTYPE_VNODE) {
635 if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
636 error = ESPIPE;
637 } else {
638 error = EOPNOTSUPP;
639 }
640 fd_putfile(fd);
641 return error;
642 }
643
644 switch (advice) {
645 case POSIX_FADV_WILLNEED:
646 case POSIX_FADV_DONTNEED:
647 vp = fp->f_data;
648 if (vp->v_type != VREG && vp->v_type != VBLK) {
649 fd_putfile(fd);
650 return 0;
651 }
652 break;
653 }
654
655 switch (advice) {
656 case POSIX_FADV_NORMAL:
657 case POSIX_FADV_RANDOM:
658 case POSIX_FADV_SEQUENTIAL:
659 /*
660 * We ignore offset and size. Must lock the file to
661 * do this, as f_advice is sub-word sized.
662 */
663 mutex_enter(&fp->f_lock);
664 fp->f_advice = (u_char)advice;
665 mutex_exit(&fp->f_lock);
666 error = 0;
667 break;
668
669 case POSIX_FADV_WILLNEED:
670 vp = fp->f_data;
671 error = uvm_readahead(&vp->v_uobj, offset, endoffset - offset);
672 break;
673
674 case POSIX_FADV_DONTNEED:
675 vp = fp->f_data;
676 /*
677 * Align the region to page boundaries as VOP_PUTPAGES expects
678 * by shrinking it. We shrink instead of expand because we
679 * do not want to deactivate cache outside of the requested
680 * region. It means that if the specified region is smaller
681 * than PAGE_SIZE, we do nothing.
682 */
683 if (round_page(offset) < trunc_page(endoffset) &&
684 offset <= round_page(offset)) {
685 mutex_enter(vp->v_interlock);
686 error = VOP_PUTPAGES(vp,
687 round_page(offset), trunc_page(endoffset),
688 PGO_DEACTIVATE | PGO_CLEANIT);
689 } else {
690 error = 0;
691 }
692 break;
693
694 case POSIX_FADV_NOREUSE:
695 /* Not implemented yet. */
696 error = 0;
697 break;
698 default:
699 error = EINVAL;
700 break;
701 }
702
703 fd_putfile(fd);
704 return error;
705 }
706
707 int
708 sys___posix_fadvise50(struct lwp *l,
709 const struct sys___posix_fadvise50_args *uap,
710 register_t *retval)
711 {
712 /* {
713 syscallarg(int) fd;
714 syscallarg(int) pad;
715 syscallarg(off_t) offset;
716 syscallarg(off_t) len;
717 syscallarg(int) advice;
718 } */
719
720 *retval = do_posix_fadvise(SCARG(uap, fd), SCARG(uap, offset),
721 SCARG(uap, len), SCARG(uap, advice));
722
723 return 0;
724 }
725