kern_descrip.c revision 1.79.2.3 1 /* $NetBSD: kern_descrip.c,v 1.79.2.3 2002/01/10 19:59:45 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.79.2.3 2002/01/10 19:59:45 thorpej Exp $");
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/filedesc.h>
49 #include <sys/kernel.h>
50 #include <sys/vnode.h>
51 #include <sys/proc.h>
52 #include <sys/file.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/stat.h>
56 #include <sys/ioctl.h>
57 #include <sys/fcntl.h>
58 #include <sys/malloc.h>
59 #include <sys/pool.h>
60 #include <sys/syslog.h>
61 #include <sys/unistd.h>
62 #include <sys/resourcevar.h>
63 #include <sys/conf.h>
64 #include <sys/event.h>
65
66 #include <sys/mount.h>
67 #include <sys/syscallargs.h>
68
69 /*
70 * Descriptor management.
71 */
72 struct filelist filehead; /* head of list of open files */
73 int nfiles; /* actual number of open files */
74 struct pool file_pool; /* memory pool for file structures */
75 struct pool cwdi_pool; /* memory pool for cwdinfo structures */
76 struct pool filedesc0_pool; /* memory pool for filedesc0 structures */
77
78 static __inline void fd_used(struct filedesc *, int);
79 static __inline void fd_unused(struct filedesc *, int);
80 int finishdup(struct proc *, int, int, register_t *);
81 int fcntl_forfs(int, struct proc *, int, void *);
82
83 static __inline void
84 fd_used(struct filedesc *fdp, int fd)
85 {
86
87 if (fd > fdp->fd_lastfile)
88 fdp->fd_lastfile = fd;
89 }
90
91 static __inline void
92 fd_unused(struct filedesc *fdp, int fd)
93 {
94
95 if (fd < fdp->fd_freefile)
96 fdp->fd_freefile = fd;
97 #ifdef DIAGNOSTIC
98 if (fd > fdp->fd_lastfile)
99 panic("fd_unused: fd_lastfile inconsistent");
100 #endif
101 if (fd == fdp->fd_lastfile) {
102 do {
103 fd--;
104 } while (fd >= 0 && fdp->fd_ofiles[fd] == NULL);
105 fdp->fd_lastfile = fd;
106 }
107 }
108
109 struct file *
110 fd_getfile(struct filedesc *fdp, int fd)
111 {
112 struct file *fp;
113
114 if ((u_int) fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
115 return (NULL);
116
117 if (FILE_IS_USABLE(fp) == 0)
118 return (NULL);
119
120 return (fp);
121 }
122
123 /*
124 * System calls on descriptors.
125 */
126
127 /*
128 * Duplicate a file descriptor.
129 */
130 /* ARGSUSED */
131 int
132 sys_dup(struct proc *p, void *v, register_t *retval)
133 {
134 struct sys_dup_args /* {
135 syscallarg(int) fd;
136 } */ *uap = v;
137 struct file *fp;
138 struct filedesc *fdp;
139 int old, new, error;
140
141 fdp = p->p_fd;
142 old = SCARG(uap, fd);
143
144 restart:
145 if ((fp = fd_getfile(fdp, old)) == NULL)
146 return (EBADF);
147
148 FILE_USE(fp);
149
150 if ((error = fdalloc(p, 0, &new)) != 0) {
151 if (error == ENOSPC) {
152 fdexpand(p);
153 FILE_UNUSE(fp, p);
154 goto restart;
155 }
156 FILE_UNUSE(fp, p);
157 return (error);
158 }
159
160 /* finishdup() will unuse the descriptors for us */
161 return (finishdup(p, old, new, retval));
162 }
163
164 /*
165 * Duplicate a file descriptor to a particular value.
166 */
167 /* ARGSUSED */
168 int
169 sys_dup2(struct proc *p, void *v, register_t *retval)
170 {
171 struct sys_dup2_args /* {
172 syscallarg(int) from;
173 syscallarg(int) to;
174 } */ *uap = v;
175 struct file *fp;
176 struct filedesc *fdp;
177 int old, new, i, error;
178
179 fdp = p->p_fd;
180 old = SCARG(uap, from);
181 new = SCARG(uap, to);
182
183 restart:
184 if ((fp = fd_getfile(fdp, old)) == NULL)
185 return (EBADF);
186
187 if ((u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
188 (u_int)new >= maxfiles)
189 return (EBADF);
190
191 if (old == new) {
192 *retval = new;
193 return (0);
194 }
195
196 FILE_USE(fp);
197
198 if (new >= fdp->fd_nfiles) {
199 if ((error = fdalloc(p, new, &i)) != 0) {
200 if (error == ENOSPC) {
201 fdexpand(p);
202 FILE_UNUSE(fp, p);
203 goto restart;
204 }
205 FILE_UNUSE(fp, p);
206 return (error);
207 }
208 if (new != i)
209 panic("dup2: fdalloc");
210 }
211
212 /*
213 * finishdup() will close the file that's in the `new'
214 * slot, if there's one there.
215 */
216
217 /* finishdup() will unuse the descriptors for us */
218 return (finishdup(p, old, new, retval));
219 }
220
221 /*
222 * The file control system call.
223 */
224 /* ARGSUSED */
225 int
226 sys_fcntl(struct proc *p, void *v, register_t *retval)
227 {
228 struct sys_fcntl_args /* {
229 syscallarg(int) fd;
230 syscallarg(int) cmd;
231 syscallarg(void *) arg;
232 } */ *uap = v;
233 struct filedesc *fdp;
234 struct file *fp;
235 struct vnode *vp;
236 int fd, i, tmp, error, flg, cmd, newmin;
237 struct flock fl;
238
239 fd = SCARG(uap, fd);
240 fdp = p->p_fd;
241 error = 0;
242 flg = F_POSIX;
243
244 restart:
245 if ((fp = fd_getfile(fdp, fd)) == NULL)
246 return (EBADF);
247
248 FILE_USE(fp);
249
250 cmd = SCARG(uap, cmd);
251 if ((cmd & F_FSCTL)) {
252 error = fcntl_forfs(fd, p, cmd, SCARG(uap, arg));
253 goto out;
254 }
255
256 switch (cmd) {
257
258 case F_DUPFD:
259 newmin = (long)SCARG(uap, arg);
260 if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
261 (u_int)newmin >= maxfiles) {
262 error = EINVAL;
263 goto out;
264 }
265 if ((error = fdalloc(p, newmin, &i)) != 0) {
266 if (error == ENOSPC) {
267 fdexpand(p);
268 FILE_UNUSE(fp, p);
269 goto restart;
270 }
271 goto out;
272 }
273
274 /* finishdup() will unuse the descriptors for us */
275 return (finishdup(p, fd, i, retval));
276
277 case F_GETFD:
278 *retval = fdp->fd_ofileflags[fd] & UF_EXCLOSE ? 1 : 0;
279 break;
280
281 case F_SETFD:
282 if ((long)SCARG(uap, arg) & 1)
283 fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
284 else
285 fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
286 break;
287
288 case F_GETFL:
289 *retval = OFLAGS(fp->f_flag);
290 break;
291
292 case F_SETFL:
293 tmp = FFLAGS((long)SCARG(uap, arg)) & FCNTLFLAGS;
294 error = (*fp->f_ops->fo_fcntl)(fp, F_SETFL, (caddr_t)&tmp, p);
295 if (error)
296 goto out;
297 fp->f_flag &= ~FCNTLFLAGS;
298 fp->f_flag |= tmp;
299 tmp = fp->f_flag & FNONBLOCK;
300 error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
301 if (error)
302 goto out;
303 tmp = fp->f_flag & FASYNC;
304 error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
305 if (error == 0)
306 goto out;
307 fp->f_flag &= ~FNONBLOCK;
308 tmp = 0;
309 (void) (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
310 break;
311
312 case F_GETOWN:
313 if (fp->f_type == DTYPE_SOCKET) {
314 *retval = ((struct socket *)fp->f_data)->so_pgid;
315 goto out;
316 }
317 error = (*fp->f_ops->fo_ioctl)
318 (fp, TIOCGPGRP, (caddr_t)retval, p);
319 *retval = -*retval;
320 break;
321
322 case F_SETOWN:
323 if (fp->f_type == DTYPE_SOCKET) {
324 ((struct socket *)fp->f_data)->so_pgid =
325 (long)SCARG(uap, arg);
326 goto out;
327 }
328 if ((long)SCARG(uap, arg) <= 0) {
329 tmp = (-(long)SCARG(uap, arg));
330 } else {
331 struct proc *p1 = pfind((long)SCARG(uap, arg));
332 if (p1 == 0) {
333 error = ESRCH;
334 goto out;
335 }
336 tmp = (long)p1->p_pgrp->pg_id;
337 }
338 error = (*fp->f_ops->fo_ioctl)
339 (fp, TIOCSPGRP, (caddr_t)&tmp, p);
340 break;
341
342 case F_SETLKW:
343 flg |= F_WAIT;
344 /* Fall into F_SETLK */
345
346 case F_SETLK:
347 if (fp->f_type != DTYPE_VNODE) {
348 error = EINVAL;
349 goto out;
350 }
351 vp = (struct vnode *)fp->f_data;
352 /* Copy in the lock structure */
353 error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&fl,
354 sizeof(fl));
355 if (error)
356 goto out;
357 if (fl.l_whence == SEEK_CUR)
358 fl.l_start += fp->f_offset;
359 switch (fl.l_type) {
360 case F_RDLCK:
361 if ((fp->f_flag & FREAD) == 0) {
362 error = EBADF;
363 goto out;
364 }
365 p->p_flag |= P_ADVLOCK;
366 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg);
367 goto out;
368
369 case F_WRLCK:
370 if ((fp->f_flag & FWRITE) == 0) {
371 error = EBADF;
372 goto out;
373 }
374 p->p_flag |= P_ADVLOCK;
375 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg);
376 goto out;
377
378 case F_UNLCK:
379 error = VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl,
380 F_POSIX);
381 goto out;
382
383 default:
384 error = EINVAL;
385 goto out;
386 }
387
388 case F_GETLK:
389 if (fp->f_type != DTYPE_VNODE) {
390 error = EINVAL;
391 goto out;
392 }
393 vp = (struct vnode *)fp->f_data;
394 /* Copy in the lock structure */
395 error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&fl,
396 sizeof(fl));
397 if (error)
398 goto out;
399 if (fl.l_whence == SEEK_CUR)
400 fl.l_start += fp->f_offset;
401 if (fl.l_type != F_RDLCK &&
402 fl.l_type != F_WRLCK &&
403 fl.l_type != F_UNLCK) {
404 error = EINVAL;
405 goto out;
406 }
407 error = VOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX);
408 if (error)
409 goto out;
410 error = copyout((caddr_t)&fl, (caddr_t)SCARG(uap, arg),
411 sizeof(fl));
412 break;
413
414 default:
415 error = EINVAL;
416 }
417
418 out:
419 FILE_UNUSE(fp, p);
420 return (error);
421 }
422
423 /*
424 * Common code for dup, dup2, and fcntl(F_DUPFD).
425 */
426 int
427 finishdup(struct proc *p, int old, int new, register_t *retval)
428 {
429 struct filedesc *fdp;
430 struct file *fp, *delfp;
431
432 fdp = p->p_fd;
433
434 /*
435 * If there is a file in the new slot, remember it so we
436 * can close it after we've finished the dup. We need
437 * to do it after the dup is finished, since closing
438 * the file may block.
439 *
440 * Note: `old' is already used for us.
441 */
442 delfp = fdp->fd_ofiles[new];
443
444 fp = fdp->fd_ofiles[old];
445 fdp->fd_ofiles[new] = fp;
446 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
447 fp->f_count++;
448 /*
449 * Note, don't have to mark it "used" in the table if there
450 * was already a file in the `new' slot.
451 */
452 if (delfp == NULL)
453 fd_used(fdp, new);
454 *retval = new;
455 FILE_UNUSE(fp, p);
456
457 if (delfp != NULL) {
458 FILE_USE(delfp);
459 (void) closef(delfp, p);
460 }
461 return (0);
462 }
463
464 void
465 fdremove(struct filedesc *fdp, int fd)
466 {
467
468 fdp->fd_ofiles[fd] = NULL;
469 fd_unused(fdp, fd);
470 }
471
472 int
473 fdrelease(struct proc *p, int fd)
474 {
475 struct filedesc *fdp;
476 struct file **fpp, *fp;
477
478 fdp = p->p_fd;
479 fpp = &fdp->fd_ofiles[fd];
480 fp = *fpp;
481 if (fp == NULL)
482 return (EBADF);
483
484 FILE_USE(fp);
485
486 *fpp = NULL;
487 fdp->fd_ofileflags[fd] = 0;
488 if (fd < fdp->fd_knlistsize)
489 knote_fdclose(p, fd);
490 fd_unused(fdp, fd);
491 return (closef(fp, p));
492 }
493
494 /*
495 * Close a file descriptor.
496 */
497 /* ARGSUSED */
498 int
499 sys_close(struct proc *p, void *v, register_t *retval)
500 {
501 struct sys_close_args /* {
502 syscallarg(int) fd;
503 } */ *uap = v;
504 int fd;
505 struct filedesc *fdp;
506 struct file *fp;
507
508 fd = SCARG(uap, fd);
509 fdp = p->p_fd;
510
511 if ((fp = fd_getfile(fdp, fd)) == NULL)
512 return (EBADF);
513
514 return (fdrelease(p, fd));
515 }
516
517 /*
518 * Return status information about a file descriptor.
519 */
520 /* ARGSUSED */
521 int
522 sys___fstat13(struct proc *p, void *v, register_t *retval)
523 {
524 struct sys___fstat13_args /* {
525 syscallarg(int) fd;
526 syscallarg(struct stat *) sb;
527 } */ *uap = v;
528 int fd;
529 struct filedesc *fdp;
530 struct file *fp;
531 struct stat ub;
532 int error;
533
534 fd = SCARG(uap, fd);
535 fdp = p->p_fd;
536
537 if ((fp = fd_getfile(fdp, fd)) == NULL)
538 return (EBADF);
539
540 FILE_USE(fp);
541 error = (*fp->f_ops->fo_stat)(fp, &ub, p);
542 FILE_UNUSE(fp, p);
543
544 if (error == 0)
545 error = copyout(&ub, SCARG(uap, sb), sizeof(ub));
546
547 return (error);
548 }
549
550 /*
551 * Return pathconf information about a file descriptor.
552 */
553 /* ARGSUSED */
554 int
555 sys_fpathconf(struct proc *p, void *v, register_t *retval)
556 {
557 struct sys_fpathconf_args /* {
558 syscallarg(int) fd;
559 syscallarg(int) name;
560 } */ *uap = v;
561 int fd;
562 struct filedesc *fdp;
563 struct file *fp;
564 struct vnode *vp;
565 int error;
566
567 fd = SCARG(uap, fd);
568 fdp = p->p_fd;
569 error = 0;
570
571 if ((fp = fd_getfile(fdp, fd)) == NULL)
572 return (EBADF);
573
574 FILE_USE(fp);
575
576 switch (fp->f_type) {
577
578 case DTYPE_SOCKET:
579 case DTYPE_PIPE:
580 if (SCARG(uap, name) != _PC_PIPE_BUF)
581 error = EINVAL;
582 else
583 *retval = PIPE_BUF;
584 break;
585
586 case DTYPE_VNODE:
587 vp = (struct vnode *)fp->f_data;
588 error = VOP_PATHCONF(vp, SCARG(uap, name), retval);
589 break;
590
591 case DTYPE_KQUEUE:
592 error = EINVAL;
593 break;
594 default:
595 panic("fpathconf");
596 }
597
598 FILE_UNUSE(fp, p);
599 return (error);
600 }
601
602 /*
603 * Allocate a file descriptor for the process.
604 */
605 int fdexpanded; /* XXX: what else uses this? */
606
607 int
608 fdalloc(struct proc *p, int want, int *result)
609 {
610 struct filedesc *fdp;
611 int i, lim, last;
612
613 fdp = p->p_fd;
614
615 /*
616 * Search for a free descriptor starting at the higher
617 * of want or fd_freefile. If that fails, consider
618 * expanding the ofile array.
619 */
620 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
621 for (;;) {
622 last = min(fdp->fd_nfiles, lim);
623 if ((i = want) < fdp->fd_freefile)
624 i = fdp->fd_freefile;
625 for (; i < last; i++) {
626 if (fdp->fd_ofiles[i] == NULL) {
627 fd_used(fdp, i);
628 if (want <= fdp->fd_freefile)
629 fdp->fd_freefile = i;
630 *result = i;
631 return (0);
632 }
633 }
634
635 /* No space in current array. Expand? */
636 if (fdp->fd_nfiles >= lim)
637 return (EMFILE);
638
639 /* Let the caller do it. */
640 return (ENOSPC);
641 }
642 }
643
644 void
645 fdexpand(struct proc *p)
646 {
647 struct filedesc *fdp;
648 int i, nfiles;
649 struct file **newofile;
650 char *newofileflags;
651
652 fdp = p->p_fd;
653
654 if (fdp->fd_nfiles < NDEXTENT)
655 nfiles = NDEXTENT;
656 else
657 nfiles = 2 * fdp->fd_nfiles;
658 newofile = malloc(nfiles * OFILESIZE, M_FILEDESC, M_WAITOK);
659 newofileflags = (char *) &newofile[nfiles];
660 /*
661 * Copy the existing ofile and ofileflags arrays
662 * and zero the new portion of each array.
663 */
664 memcpy(newofile, fdp->fd_ofiles,
665 (i = sizeof(struct file *) * fdp->fd_nfiles));
666 memset((char *)newofile + i, 0,
667 nfiles * sizeof(struct file *) - i);
668 memcpy(newofileflags, fdp->fd_ofileflags,
669 (i = sizeof(char) * fdp->fd_nfiles));
670 memset(newofileflags + i, 0, nfiles * sizeof(char) - i);
671 if (fdp->fd_nfiles > NDFILE)
672 free(fdp->fd_ofiles, M_FILEDESC);
673 fdp->fd_ofiles = newofile;
674 fdp->fd_ofileflags = newofileflags;
675 fdp->fd_nfiles = nfiles;
676 fdexpanded++;
677 }
678
679 /*
680 * Check to see whether n user file descriptors
681 * are available to the process p.
682 */
683 int
684 fdavail(struct proc *p, int n)
685 {
686 struct filedesc *fdp;
687 struct file **fpp;
688 int i, lim;
689
690 fdp = p->p_fd;
691 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
692 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
693 return (1);
694 fpp = &fdp->fd_ofiles[fdp->fd_freefile];
695 for (i = min(lim,fdp->fd_nfiles) - fdp->fd_freefile; --i >= 0; fpp++)
696 if (*fpp == NULL && --n <= 0)
697 return (1);
698 return (0);
699 }
700
701 /*
702 * Initialize the data structures necessary for managing files.
703 */
704 void
705 finit(void)
706 {
707
708 pool_init(&file_pool, sizeof(struct file), 0, 0, 0, "filepl",
709 0, pool_page_alloc_nointr, pool_page_free_nointr, M_FILE);
710 pool_init(&cwdi_pool, sizeof(struct cwdinfo), 0, 0, 0, "cwdipl",
711 0, pool_page_alloc_nointr, pool_page_free_nointr, M_FILEDESC);
712 pool_init(&filedesc0_pool, sizeof(struct filedesc0), 0, 0, 0, "fdescpl",
713 0, pool_page_alloc_nointr, pool_page_free_nointr, M_FILEDESC);
714 }
715
716 /*
717 * Create a new open file structure and allocate
718 * a file decriptor for the process that refers to it.
719 */
720 int
721 falloc(struct proc *p, struct file **resultfp, int *resultfd)
722 {
723 struct file *fp, *fq;
724 int error, i;
725
726 restart:
727 if ((error = fdalloc(p, 0, &i)) != 0) {
728 if (error == ENOSPC) {
729 fdexpand(p);
730 goto restart;
731 }
732 return (error);
733 }
734 if (nfiles >= maxfiles) {
735 tablefull("file", "increase kern.maxfiles or MAXFILES");
736 return (ENFILE);
737 }
738 /*
739 * Allocate a new file descriptor.
740 * If the process has file descriptor zero open, add to the list
741 * of open files at that point, otherwise put it at the front of
742 * the list of open files.
743 */
744 nfiles++;
745 fp = pool_get(&file_pool, PR_WAITOK);
746 memset(fp, 0, sizeof(struct file));
747 fp->f_iflags = FIF_LARVAL;
748 if ((fq = p->p_fd->fd_ofiles[0]) != NULL) {
749 LIST_INSERT_AFTER(fq, fp, f_list);
750 } else {
751 LIST_INSERT_HEAD(&filehead, fp, f_list);
752 }
753 p->p_fd->fd_ofiles[i] = fp;
754 fp->f_count = 1;
755 fp->f_cred = p->p_ucred;
756 crhold(fp->f_cred);
757 if (resultfp) {
758 FILE_USE(fp);
759 *resultfp = fp;
760 }
761 if (resultfd)
762 *resultfd = i;
763 return (0);
764 }
765
766 /*
767 * Free a file descriptor.
768 */
769 void
770 ffree(struct file *fp)
771 {
772
773 #ifdef DIAGNOSTIC
774 if (fp->f_usecount)
775 panic("ffree");
776 #endif
777
778 LIST_REMOVE(fp, f_list);
779 crfree(fp->f_cred);
780 #ifdef DIAGNOSTIC
781 fp->f_count = 0;
782 #endif
783 nfiles--;
784 pool_put(&file_pool, fp);
785 }
786
787 /*
788 * Create an initial cwdinfo structure, using the same current and root
789 * directories as p.
790 */
791 struct cwdinfo *
792 cwdinit(struct proc *p)
793 {
794 struct cwdinfo *cwdi;
795
796 cwdi = pool_get(&cwdi_pool, PR_WAITOK);
797
798 cwdi->cwdi_cdir = p->p_cwdi->cwdi_cdir;
799 if (cwdi->cwdi_cdir)
800 VREF(cwdi->cwdi_cdir);
801 cwdi->cwdi_rdir = p->p_cwdi->cwdi_rdir;
802 if (cwdi->cwdi_rdir)
803 VREF(cwdi->cwdi_rdir);
804 cwdi->cwdi_cmask = p->p_cwdi->cwdi_cmask;
805 cwdi->cwdi_refcnt = 1;
806
807 return (cwdi);
808 }
809
810 /*
811 * Make p2 share p1's cwdinfo.
812 */
813 void
814 cwdshare(struct proc *p1, struct proc *p2)
815 {
816
817 p2->p_cwdi = p1->p_cwdi;
818 p1->p_cwdi->cwdi_refcnt++;
819 }
820
821 /*
822 * Make this process not share its cwdinfo structure, maintaining
823 * all cwdinfo state.
824 */
825 void
826 cwdunshare(struct proc *p)
827 {
828 struct cwdinfo *newcwdi;
829
830 if (p->p_cwdi->cwdi_refcnt == 1)
831 return;
832
833 newcwdi = cwdinit(p);
834 cwdfree(p);
835 p->p_cwdi = newcwdi;
836 }
837
838 /*
839 * Release a cwdinfo structure.
840 */
841 void
842 cwdfree(struct proc *p)
843 {
844 struct cwdinfo *cwdi;
845
846 cwdi = p->p_cwdi;
847 if (--cwdi->cwdi_refcnt > 0)
848 return;
849
850 p->p_cwdi = NULL;
851
852 vrele(cwdi->cwdi_cdir);
853 if (cwdi->cwdi_rdir)
854 vrele(cwdi->cwdi_rdir);
855 pool_put(&cwdi_pool, cwdi);
856 }
857
858 /*
859 * Create an initial filedesc structure, using the same current and root
860 * directories as p.
861 */
862 struct filedesc *
863 fdinit(struct proc *p)
864 {
865 struct filedesc0 *newfdp;
866
867 newfdp = pool_get(&filedesc0_pool, PR_WAITOK);
868 memset(newfdp, 0, sizeof(struct filedesc0));
869
870 fdinit1(newfdp);
871
872 return (&newfdp->fd_fd);
873 }
874
875 /*
876 * Initialize a file descriptor table.
877 */
878 void
879 fdinit1(struct filedesc0 *newfdp)
880 {
881
882 newfdp->fd_fd.fd_refcnt = 1;
883 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
884 newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
885 newfdp->fd_fd.fd_nfiles = NDFILE;
886 newfdp->fd_fd.fd_knlistsize = -1;
887 }
888
889 /*
890 * Make p2 share p1's filedesc structure.
891 */
892 void
893 fdshare(struct proc *p1, struct proc *p2)
894 {
895
896 p2->p_fd = p1->p_fd;
897 p1->p_fd->fd_refcnt++;
898 }
899
900 /*
901 * Make this process not share its filedesc structure, maintaining
902 * all file descriptor state.
903 */
904 void
905 fdunshare(struct proc *p)
906 {
907 struct filedesc *newfd;
908
909 if (p->p_fd->fd_refcnt == 1)
910 return;
911
912 newfd = fdcopy(p);
913 fdfree(p);
914 p->p_fd = newfd;
915 }
916
917 /*
918 * Clear a process's fd table.
919 */
920 void
921 fdclear(struct proc *p)
922 {
923 struct filedesc *newfd;
924
925 newfd = fdinit(p);
926 fdfree(p);
927 p->p_fd = newfd;
928 }
929
930 /*
931 * Copy a filedesc structure.
932 */
933 struct filedesc *
934 fdcopy(struct proc *p)
935 {
936 struct filedesc *newfdp, *fdp;
937 struct file **fpp;
938 int i;
939
940 fdp = p->p_fd;
941 newfdp = pool_get(&filedesc0_pool, PR_WAITOK);
942 memcpy(newfdp, fdp, sizeof(struct filedesc));
943 newfdp->fd_refcnt = 1;
944
945 /*
946 * If the number of open files fits in the internal arrays
947 * of the open file structure, use them, otherwise allocate
948 * additional memory for the number of descriptors currently
949 * in use.
950 */
951 if (newfdp->fd_lastfile < NDFILE) {
952 newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
953 newfdp->fd_ofileflags =
954 ((struct filedesc0 *) newfdp)->fd_dfileflags;
955 i = NDFILE;
956 } else {
957 /*
958 * Compute the smallest multiple of NDEXTENT needed
959 * for the file descriptors currently in use,
960 * allowing the table to shrink.
961 */
962 i = newfdp->fd_nfiles;
963 while (i >= 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
964 i /= 2;
965 newfdp->fd_ofiles = malloc(i * OFILESIZE, M_FILEDESC, M_WAITOK);
966 newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
967 }
968 newfdp->fd_nfiles = i;
969 memcpy(newfdp->fd_ofiles, fdp->fd_ofiles, i * sizeof(struct file **));
970 memcpy(newfdp->fd_ofileflags, fdp->fd_ofileflags, i * sizeof(char));
971 /*
972 * kq descriptors cannot be copied.
973 */
974 if (newfdp->fd_knlistsize != -1) {
975 fpp = newfdp->fd_ofiles;
976 for (i = newfdp->fd_lastfile; i-- >= 0; fpp++) {
977 if (*fpp != NULL && (*fpp)->f_type == DTYPE_KQUEUE)
978 *fpp = NULL;
979 }
980 newfdp->fd_knlist = NULL;
981 newfdp->fd_knlistsize = -1;
982 newfdp->fd_knhash = NULL;
983 newfdp->fd_knhashmask = 0;
984 }
985 fpp = newfdp->fd_ofiles;
986 for (i = newfdp->fd_lastfile; i >= 0; i--, fpp++)
987 if (*fpp != NULL)
988 (*fpp)->f_count++;
989 return (newfdp);
990 }
991
992 /*
993 * Release a filedesc structure.
994 */
995 void
996 fdfree(struct proc *p)
997 {
998 struct filedesc *fdp;
999 struct file **fpp, *fp;
1000 int i;
1001
1002 fdp = p->p_fd;
1003 if (--fdp->fd_refcnt > 0)
1004 return;
1005 fpp = fdp->fd_ofiles;
1006 for (i = fdp->fd_lastfile; i >= 0; i--, fpp++) {
1007 fp = *fpp;
1008 if (fp != NULL) {
1009 *fpp = NULL;
1010 FILE_USE(fp);
1011 if (i < fdp->fd_knlistsize)
1012 knote_fdclose(p, i);
1013 (void) closef(fp, p);
1014 }
1015 }
1016 p->p_fd = NULL;
1017 if (fdp->fd_nfiles > NDFILE)
1018 free(fdp->fd_ofiles, M_FILEDESC);
1019 if (fdp->fd_knlist)
1020 free(fdp->fd_knlist, M_KEVENT);
1021 if (fdp->fd_knhash)
1022 hashdone(fdp->fd_knhash, M_KEVENT);
1023 pool_put(&filedesc0_pool, fdp);
1024 }
1025
1026 /*
1027 * Internal form of close.
1028 * Decrement reference count on file structure.
1029 * Note: p may be NULL when closing a file
1030 * that was being passed in a message.
1031 *
1032 * Note: we expect the caller is holding a usecount, and expects us
1033 * to drop it (the caller thinks the file is going away forever).
1034 */
1035 int
1036 closef(struct file *fp, struct proc *p)
1037 {
1038 struct vnode *vp;
1039 struct flock lf;
1040 int error;
1041
1042 if (fp == NULL)
1043 return (0);
1044
1045 /*
1046 * POSIX record locking dictates that any close releases ALL
1047 * locks owned by this process. This is handled by setting
1048 * a flag in the unlock to free ONLY locks obeying POSIX
1049 * semantics, and not to free BSD-style file locks.
1050 * If the descriptor was in a message, POSIX-style locks
1051 * aren't passed with the descriptor.
1052 */
1053 if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
1054 lf.l_whence = SEEK_SET;
1055 lf.l_start = 0;
1056 lf.l_len = 0;
1057 lf.l_type = F_UNLCK;
1058 vp = (struct vnode *)fp->f_data;
1059 (void) VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX);
1060 }
1061
1062 /*
1063 * If WANTCLOSE is set, then the reference count on the file
1064 * is 0, but there were multiple users of the file. This can
1065 * happen if a filedesc structure is shared by multiple
1066 * processes.
1067 */
1068 if (fp->f_iflags & FIF_WANTCLOSE) {
1069 /*
1070 * Another user of the file is already closing, and is
1071 * simply waiting for other users of the file to drain.
1072 * Release our usecount, and wake up the closer if it
1073 * is the only remaining use.
1074 */
1075 #ifdef DIAGNOSTIC
1076 if (fp->f_count != 0)
1077 panic("closef: wantclose and count != 0");
1078 if (fp->f_usecount < 2)
1079 panic("closef: wantclose and usecount < 2");
1080 #endif
1081 if (--fp->f_usecount == 1)
1082 wakeup(&fp->f_usecount);
1083 return (0);
1084 } else {
1085 /*
1086 * Decrement the reference count. If we were not the
1087 * last reference, then release our use and just
1088 * return.
1089 */
1090 if (--fp->f_count > 0) {
1091 #ifdef DIAGNOSTIC
1092 if (fp->f_usecount < 1)
1093 panic("closef: no wantclose and usecount < 1");
1094 #endif
1095 fp->f_usecount--;
1096 return (0);
1097 }
1098 if (fp->f_count < 0)
1099 panic("closef: count < 0");
1100 }
1101
1102 /*
1103 * The reference count is now 0. However, there may be
1104 * multiple potential users of this file. This can happen
1105 * if multiple processes shared a single filedesc structure.
1106 *
1107 * Notify these potential users that the file is closing.
1108 * This will prevent them from adding additional uses to
1109 * the file.
1110 */
1111 fp->f_iflags |= FIF_WANTCLOSE;
1112
1113 /*
1114 * We expect the caller to add a use to the file. So, if we
1115 * are the last user, usecount will be 1. If it is not, we
1116 * must wait for the usecount to drain. When it drains back
1117 * to 1, we will be awakened so that we may proceed with the
1118 * close.
1119 */
1120 #ifdef DIAGNOSTIC
1121 if (fp->f_usecount < 1)
1122 panic("closef: usecount < 1");
1123 #endif
1124 while (fp->f_usecount > 1)
1125 (void) tsleep(&fp->f_usecount, PRIBIO, "closef", 0);
1126 #ifdef DIAGNOSTIC
1127 if (fp->f_usecount != 1)
1128 panic("closef: usecount != 1");
1129 #endif
1130
1131 if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
1132 lf.l_whence = SEEK_SET;
1133 lf.l_start = 0;
1134 lf.l_len = 0;
1135 lf.l_type = F_UNLCK;
1136 vp = (struct vnode *)fp->f_data;
1137 (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1138 }
1139 if (fp->f_ops)
1140 error = (*fp->f_ops->fo_close)(fp, p);
1141 else
1142 error = 0;
1143
1144 /* Nothing references the file now, drop the final use (us). */
1145 fp->f_usecount--;
1146
1147 ffree(fp);
1148 return (error);
1149 }
1150
1151 /*
1152 * Apply an advisory lock on a file descriptor.
1153 *
1154 * Just attempt to get a record lock of the requested type on
1155 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1156 */
1157 /* ARGSUSED */
1158 int
1159 sys_flock(struct proc *p, void *v, register_t *retval)
1160 {
1161 struct sys_flock_args /* {
1162 syscallarg(int) fd;
1163 syscallarg(int) how;
1164 } */ *uap = v;
1165 int fd, how, error;
1166 struct filedesc *fdp;
1167 struct file *fp;
1168 struct vnode *vp;
1169 struct flock lf;
1170
1171 fd = SCARG(uap, fd);
1172 how = SCARG(uap, how);
1173 fdp = p->p_fd;
1174 error = 0;
1175
1176 if ((fp = fd_getfile(fdp, fd)) == NULL)
1177 return (EBADF);
1178
1179 FILE_USE(fp);
1180
1181 if (fp->f_type != DTYPE_VNODE) {
1182 error = EOPNOTSUPP;
1183 goto out;
1184 }
1185
1186 vp = (struct vnode *)fp->f_data;
1187 lf.l_whence = SEEK_SET;
1188 lf.l_start = 0;
1189 lf.l_len = 0;
1190 if (how & LOCK_UN) {
1191 lf.l_type = F_UNLCK;
1192 fp->f_flag &= ~FHASLOCK;
1193 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1194 goto out;
1195 }
1196 if (how & LOCK_EX)
1197 lf.l_type = F_WRLCK;
1198 else if (how & LOCK_SH)
1199 lf.l_type = F_RDLCK;
1200 else {
1201 error = EINVAL;
1202 goto out;
1203 }
1204 fp->f_flag |= FHASLOCK;
1205 if (how & LOCK_NB)
1206 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK);
1207 else
1208 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1209 F_FLOCK|F_WAIT);
1210 out:
1211 FILE_UNUSE(fp, p);
1212 return (error);
1213 }
1214
1215 /*
1216 * File Descriptor pseudo-device driver (/dev/fd/).
1217 *
1218 * Opening minor device N dup()s the file (if any) connected to file
1219 * descriptor N belonging to the calling process. Note that this driver
1220 * consists of only the ``open()'' routine, because all subsequent
1221 * references to this file will be direct to the other driver.
1222 */
1223 /* ARGSUSED */
1224 int
1225 filedescopen(dev_t dev, int mode, int type, struct proc *p)
1226 {
1227
1228 /*
1229 * XXX Kludge: set p->p_dupfd to contain the value of the
1230 * the file descriptor being sought for duplication. The error
1231 * return ensures that the vnode for this device will be released
1232 * by vn_open. Open will detect this special error and take the
1233 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1234 * will simply report the error.
1235 */
1236 p->p_dupfd = minor(dev);
1237 return (ENODEV);
1238 }
1239
1240 /*
1241 * Duplicate the specified descriptor to a free descriptor.
1242 */
1243 int
1244 dupfdopen(struct proc *p, int indx, int dfd, int mode, int error)
1245 {
1246 struct filedesc *fdp;
1247 struct file *wfp, *fp;
1248
1249 fdp = p->p_fd;
1250 /*
1251 * If the to-be-dup'd fd number is greater than the allowed number
1252 * of file descriptors, or the fd to be dup'd has already been
1253 * closed, reject. Note, check for new == old is necessary as
1254 * falloc could allocate an already closed to-be-dup'd descriptor
1255 * as the new descriptor.
1256 */
1257 fp = fdp->fd_ofiles[indx];
1258
1259 if ((wfp = fd_getfile(fdp, dfd)) == NULL)
1260 return (EBADF);
1261
1262 if (fp == wfp)
1263 return (EBADF);
1264
1265 FILE_USE(wfp);
1266
1267 /*
1268 * There are two cases of interest here.
1269 *
1270 * For ENODEV simply dup (dfd) to file descriptor
1271 * (indx) and return.
1272 *
1273 * For ENXIO steal away the file structure from (dfd) and
1274 * store it in (indx). (dfd) is effectively closed by
1275 * this operation.
1276 *
1277 * Any other error code is just returned.
1278 */
1279 switch (error) {
1280 case ENODEV:
1281 /*
1282 * Check that the mode the file is being opened for is a
1283 * subset of the mode of the existing descriptor.
1284 */
1285 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
1286 FILE_UNUSE(wfp, p);
1287 return (EACCES);
1288 }
1289 fdp->fd_ofiles[indx] = wfp;
1290 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1291 wfp->f_count++;
1292 fd_used(fdp, indx);
1293 FILE_UNUSE(wfp, p);
1294 return (0);
1295
1296 case ENXIO:
1297 /*
1298 * Steal away the file pointer from dfd, and stuff it into indx.
1299 */
1300 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
1301 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1302 fdp->fd_ofiles[dfd] = NULL;
1303 fdp->fd_ofileflags[dfd] = 0;
1304 /*
1305 * Complete the clean up of the filedesc structure by
1306 * recomputing the various hints.
1307 */
1308 fd_used(fdp, indx);
1309 fd_unused(fdp, dfd);
1310 FILE_UNUSE(wfp, p);
1311 return (0);
1312
1313 default:
1314 FILE_UNUSE(wfp, p);
1315 return (error);
1316 }
1317 /* NOTREACHED */
1318 }
1319
1320 /*
1321 * fcntl call which is being passed to the file's fs.
1322 */
1323 int
1324 fcntl_forfs(int fd, struct proc *p, int cmd, void *arg)
1325 {
1326 struct file *fp;
1327 struct filedesc *fdp;
1328 int error;
1329 u_int size;
1330 caddr_t data, memp;
1331 #define STK_PARAMS 128
1332 char stkbuf[STK_PARAMS];
1333
1334 /* fd's value was validated in sys_fcntl before calling this routine */
1335 fdp = p->p_fd;
1336 fp = fdp->fd_ofiles[fd];
1337
1338 if ((fp->f_flag & (FREAD | FWRITE)) == 0)
1339 return (EBADF);
1340
1341 /*
1342 * Interpret high order word to find amount of data to be
1343 * copied to/from the user's address space.
1344 */
1345 size = (size_t)F_PARAM_LEN(cmd);
1346 if (size > F_PARAM_MAX)
1347 return (EINVAL);
1348 memp = NULL;
1349 if (size > sizeof(stkbuf)) {
1350 memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
1351 data = memp;
1352 } else
1353 data = stkbuf;
1354 if (cmd & F_FSIN) {
1355 if (size) {
1356 error = copyin(arg, data, size);
1357 if (error) {
1358 if (memp)
1359 free(memp, M_IOCTLOPS);
1360 return (error);
1361 }
1362 } else
1363 *(caddr_t *)data = arg;
1364 } else if ((cmd & F_FSOUT) && size)
1365 /*
1366 * Zero the buffer so the user always
1367 * gets back something deterministic.
1368 */
1369 memset(data, 0, size);
1370 else if (cmd & F_FSVOID)
1371 *(caddr_t *)data = arg;
1372
1373
1374 error = (*fp->f_ops->fo_fcntl)(fp, cmd, data, p);
1375
1376 /*
1377 * Copy any data to user, size was
1378 * already set and checked above.
1379 */
1380 if (error == 0 && (cmd & F_FSOUT) && size)
1381 error = copyout(data, arg, size);
1382 if (memp)
1383 free(memp, M_IOCTLOPS);
1384 return (error);
1385 }
1386
1387 /*
1388 * Close any files on exec?
1389 */
1390 void
1391 fdcloseexec(struct proc *p)
1392 {
1393 struct filedesc *fdp;
1394 int fd;
1395
1396 fdunshare(p);
1397 cwdunshare(p);
1398
1399 fdp = p->p_fd;
1400 for (fd = 0; fd <= fdp->fd_lastfile; fd++)
1401 if (fdp->fd_ofileflags[fd] & UF_EXCLOSE)
1402 (void) fdrelease(p, fd);
1403 }
1404