xref: /netbsd-src/sys/kern/kern_descrip.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1 /*	$NetBSD: kern_descrip.c,v 1.29 1994/12/14 19:38:48 mycroft 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.6 (Berkeley) 4/19/94
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/filedesc.h>
46 #include <sys/kernel.h>
47 #include <sys/vnode.h>
48 #include <sys/proc.h>
49 #include <sys/file.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/stat.h>
53 #include <sys/ioctl.h>
54 #include <sys/fcntl.h>
55 #include <sys/malloc.h>
56 #include <sys/syslog.h>
57 #include <sys/unistd.h>
58 #include <sys/resourcevar.h>
59 
60 #include <sys/mount.h>
61 #include <sys/syscallargs.h>
62 
63 /*
64  * Descriptor management.
65  */
66 struct filelist filehead;	/* head of list of open files */
67 int nfiles;			/* actual number of open files */
68 
69 static __inline
70 fd_used(fdp, fd)
71 	register struct filedesc *fdp;
72 	register int fd;
73 {
74 
75 	if (fd > fdp->fd_lastfile)
76 		fdp->fd_lastfile = fd;
77 }
78 
79 static __inline
80 fd_unused(fdp, fd)
81 	register struct filedesc *fdp;
82 	register int fd;
83 {
84 
85 	if (fd < fdp->fd_freefile)
86 		fdp->fd_freefile = fd;
87 #ifdef DIAGNOSTIC
88 	if (fd > fdp->fd_lastfile)
89 		panic("fd_unused: fd_lastfile inconsistent");
90 #endif
91 	if (fd == fdp->fd_lastfile) {
92 		do {
93 			fd--;
94 		} while (fd >= 0 && fdp->fd_ofiles[fd] == NULL);
95 		fdp->fd_lastfile = fd;
96 	}
97 }
98 
99 /*
100  * System calls on descriptors.
101  */
102 
103 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_ULTRIX) || defined(COMPAT_HPUX)
104 /* ARGSUSED */
105 compat_43_getdtablesize(p, uap, retval)
106 	struct proc *p;
107 	void *uap;
108 	register_t *retval;
109 {
110 
111 	*retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
112 	return (0);
113 }
114 #endif
115 
116 /*
117  * Duplicate a file descriptor.
118  */
119 /* ARGSUSED */
120 dup(p, uap, retval)
121 	struct proc *p;
122 	struct dup_args /* {
123 		syscallarg(u_int) fd;
124 	} */ *uap;
125 	register_t *retval;
126 {
127 	register struct filedesc *fdp = p->p_fd;
128 	register int old = SCARG(uap, fd);
129 	int new;
130 	int error;
131 
132 	if ((u_int)old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL)
133 		return (EBADF);
134 	if (error = fdalloc(p, 0, &new))
135 		return (error);
136 	return (finishdup(fdp, old, new, retval));
137 }
138 
139 /*
140  * Duplicate a file descriptor to a particular value.
141  */
142 /* ARGSUSED */
143 dup2(p, uap, retval)
144 	struct proc *p;
145 	struct dup2_args /* {
146 		syscallarg(u_int) from;
147 		syscallarg(u_int) to;
148 	} */ *uap;
149 	register_t *retval;
150 {
151 	register struct filedesc *fdp = p->p_fd;
152 	register int old = SCARG(uap, from), new = SCARG(uap, to);
153 	int i, error;
154 
155 	if ((u_int)old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL ||
156 	    (u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
157 	    (u_int)new >= maxfiles)
158 		return (EBADF);
159 	if (old == new) {
160 		*retval = new;
161 		return (0);
162 	}
163 	if (new >= fdp->fd_nfiles) {
164 		if (error = fdalloc(p, new, &i))
165 			return (error);
166 		if (new != i)
167 			panic("dup2: fdalloc");
168 	} else {
169 		(void) fdclose(p, new);
170 	}
171 	return (finishdup(fdp, old, new, retval));
172 }
173 
174 /*
175  * The file control system call.
176  */
177 /* ARGSUSED */
178 fcntl(p, uap, retval)
179 	struct proc *p;
180 	register struct fcntl_args /* {
181 		syscallarg(int) fd;
182 		syscallarg(int) cmd;
183 		syscallarg(void *) arg;
184 	} */ *uap;
185 	register_t *retval;
186 {
187 	int fd = SCARG(uap, fd);
188 	register struct filedesc *fdp = p->p_fd;
189 	register struct file *fp;
190 	struct vnode *vp;
191 	int i, tmp, error, flg = F_POSIX;
192 	struct flock fl;
193 	int newmin;
194 
195 	if ((u_int)fd >= fdp->fd_nfiles ||
196 	    (fp = fdp->fd_ofiles[fd]) == NULL)
197 		return (EBADF);
198 	switch (SCARG(uap, cmd)) {
199 
200 	case F_DUPFD:
201 		newmin = (int)SCARG(uap, arg);
202 		if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
203 		    (u_int)newmin >= maxfiles)
204 			return (EINVAL);
205 		if (error = fdalloc(p, newmin, &i))
206 			return (error);
207 		return (finishdup(fdp, fd, i, retval));
208 
209 	case F_GETFD:
210 		*retval = fdp->fd_ofileflags[fd] & UF_EXCLOSE ? 1 : 0;
211 		return (0);
212 
213 	case F_SETFD:
214 		if ((long)SCARG(uap, arg) & 1)
215 			fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
216 		else
217 			fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
218 		return (0);
219 
220 	case F_GETFL:
221 		*retval = OFLAGS(fp->f_flag);
222 		return (0);
223 
224 	case F_SETFL:
225 		fp->f_flag &= ~FCNTLFLAGS;
226 		fp->f_flag |= FFLAGS((long)SCARG(uap, arg)) & FCNTLFLAGS;
227 		tmp = fp->f_flag & FNONBLOCK;
228 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
229 		if (error)
230 			return (error);
231 		tmp = fp->f_flag & FASYNC;
232 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
233 		if (!error)
234 			return (0);
235 		fp->f_flag &= ~FNONBLOCK;
236 		tmp = 0;
237 		(void) (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
238 		return (error);
239 
240 	case F_GETOWN:
241 		if (fp->f_type == DTYPE_SOCKET) {
242 			*retval = ((struct socket *)fp->f_data)->so_pgid;
243 			return (0);
244 		}
245 		error = (*fp->f_ops->fo_ioctl)
246 			(fp, (int)TIOCGPGRP, (caddr_t)retval, p);
247 		*retval = -*retval;
248 		return (error);
249 
250 	case F_SETOWN:
251 		if (fp->f_type == DTYPE_SOCKET) {
252 			((struct socket *)fp->f_data)->so_pgid =
253 			    (long)SCARG(uap, arg);
254 			return (0);
255 		}
256 		if ((long)SCARG(uap, arg) <= 0) {
257 			SCARG(uap, arg) = (void *)(-(long)SCARG(uap, arg));
258 		} else {
259 			struct proc *p1 = pfind((long)SCARG(uap, arg));
260 			if (p1 == 0)
261 				return (ESRCH);
262 			SCARG(uap, arg) = (void *)(long)p1->p_pgrp->pg_id;
263 		}
264 		return ((*fp->f_ops->fo_ioctl)
265 			(fp, (int)TIOCSPGRP, (caddr_t)&SCARG(uap, arg), p));
266 
267 	case F_SETLKW:
268 		flg |= F_WAIT;
269 		/* Fall into F_SETLK */
270 
271 	case F_SETLK:
272 		if (fp->f_type != DTYPE_VNODE)
273 			return (EBADF);
274 		vp = (struct vnode *)fp->f_data;
275 		/* Copy in the lock structure */
276 		error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&fl,
277 		    sizeof (fl));
278 		if (error)
279 			return (error);
280 		if (fl.l_whence == SEEK_CUR)
281 			fl.l_start += fp->f_offset;
282 		switch (fl.l_type) {
283 
284 		case F_RDLCK:
285 			if ((fp->f_flag & FREAD) == 0)
286 				return (EBADF);
287 			p->p_flag |= P_ADVLOCK;
288 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
289 
290 		case F_WRLCK:
291 			if ((fp->f_flag & FWRITE) == 0)
292 				return (EBADF);
293 			p->p_flag |= P_ADVLOCK;
294 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
295 
296 		case F_UNLCK:
297 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl,
298 				F_POSIX));
299 
300 		default:
301 			return (EINVAL);
302 		}
303 
304 	case F_GETLK:
305 		if (fp->f_type != DTYPE_VNODE)
306 			return (EBADF);
307 		vp = (struct vnode *)fp->f_data;
308 		/* Copy in the lock structure */
309 		error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&fl,
310 		    sizeof (fl));
311 		if (error)
312 			return (error);
313 		if (fl.l_whence == SEEK_CUR)
314 			fl.l_start += fp->f_offset;
315 		if (error = VOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX))
316 			return (error);
317 		return (copyout((caddr_t)&fl, (caddr_t)SCARG(uap, arg),
318 		    sizeof (fl)));
319 
320 	default:
321 		return (EINVAL);
322 	}
323 	/* NOTREACHED */
324 }
325 
326 /*
327  * Common code for dup, dup2, and fcntl(F_DUPFD).
328  */
329 int
330 finishdup(fdp, old, new, retval)
331 	register struct filedesc *fdp;
332 	register int old, new;
333 	register_t *retval;
334 {
335 	register struct file *fp;
336 
337 	fp = fdp->fd_ofiles[old];
338 	fdp->fd_ofiles[new] = fp;
339 	fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
340 	fp->f_count++;
341 	fd_used(fdp, new);
342 	*retval = new;
343 	return (0);
344 }
345 
346 int
347 fdclose(p, fd)
348 	struct proc *p;
349 	int fd;
350 {
351 	register struct filedesc *fdp = p->p_fd;
352 	register struct file **fpp, *fp;
353 	register char *pf;
354 
355 	fpp = &fdp->fd_ofiles[fd];
356 	fp = *fpp;
357 	if (fp == NULL)
358 		return (EBADF);
359 	pf = &fdp->fd_ofileflags[fd];
360 	if (*pf & UF_MAPPED)
361 		(void) munmapfd(p, fd);
362 	*fpp = NULL;
363 	*pf = 0;
364 	fd_unused(fdp, fd);
365 	return (closef(fp, p));
366 }
367 
368 /*
369  * Close a file descriptor.
370  */
371 /* ARGSUSED */
372 close(p, uap, retval)
373 	struct proc *p;
374 	struct close_args /* {
375 		syscallarg(int) fd;
376 	} */ *uap;
377 	register_t *retval;
378 {
379 	int fd = SCARG(uap, fd);
380 	register struct filedesc *fdp = p->p_fd;
381 
382 	if ((u_int)fd >= fdp->fd_nfiles)
383 		return (EBADF);
384 	return (fdclose(p, fd));
385 }
386 
387 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_IBCS2)
388 /*
389  * Return status information about a file descriptor.
390  */
391 /* ARGSUSED */
392 compat_43_fstat(p, uap, retval)
393 	struct proc *p;
394 	register struct compat_43_fstat_args /* {
395 		syscallarg(int) fd;
396 		syscallarg(struct ostat *) sb;
397 	} */ *uap;
398 	register_t *retval;
399 {
400 	int fd = SCARG(uap, fd);
401 	register struct filedesc *fdp = p->p_fd;
402 	register struct file *fp;
403 	struct stat ub;
404 	struct ostat oub;
405 	int error;
406 
407 	if ((u_int)fd >= fdp->fd_nfiles ||
408 	    (fp = fdp->fd_ofiles[fd]) == NULL)
409 		return (EBADF);
410 	switch (fp->f_type) {
411 
412 	case DTYPE_VNODE:
413 		error = vn_stat((struct vnode *)fp->f_data, &ub, p);
414 		break;
415 
416 	case DTYPE_SOCKET:
417 		error = soo_stat((struct socket *)fp->f_data, &ub);
418 		break;
419 
420 	default:
421 		panic("ofstat");
422 		/*NOTREACHED*/
423 	}
424 	cvtstat(&ub, &oub);
425 	if (error == 0)
426 		error = copyout((caddr_t)&oub, (caddr_t)SCARG(uap, sb),
427 		    sizeof (oub));
428 	return (error);
429 }
430 #endif /* COMPAT_43 || COMPAT_SUNOS || COMPAT_IBCS2 */
431 
432 /*
433  * Return status information about a file descriptor.
434  */
435 /* ARGSUSED */
436 fstat(p, uap, retval)
437 	struct proc *p;
438 	register struct fstat_args /* {
439 		syscallarg(int) fd;
440 		syscallarg(struct stat *) sb;
441 	} */ *uap;
442 	register_t *retval;
443 {
444 	int fd = SCARG(uap, fd);
445 	register struct filedesc *fdp = p->p_fd;
446 	register struct file *fp;
447 	struct stat ub;
448 	int error;
449 
450 	if ((u_int)fd >= fdp->fd_nfiles ||
451 	    (fp = fdp->fd_ofiles[fd]) == NULL)
452 		return (EBADF);
453 	switch (fp->f_type) {
454 
455 	case DTYPE_VNODE:
456 		error = vn_stat((struct vnode *)fp->f_data, &ub, p);
457 		break;
458 
459 	case DTYPE_SOCKET:
460 		error = soo_stat((struct socket *)fp->f_data, &ub);
461 		break;
462 
463 	default:
464 		panic("fstat");
465 		/*NOTREACHED*/
466 	}
467 	if (error == 0)
468 		error = copyout((caddr_t)&ub, (caddr_t)SCARG(uap, sb),
469 		    sizeof (ub));
470 	return (error);
471 }
472 
473 /*
474  * Return pathconf information about a file descriptor.
475  */
476 /* ARGSUSED */
477 fpathconf(p, uap, retval)
478 	struct proc *p;
479 	register struct fpathconf_args /* {
480 		syscallarg(int) fd;
481 		syscallarg(int) name;
482 	} */ *uap;
483 	register_t *retval;
484 {
485 	int fd = SCARG(uap, fd);
486 	struct filedesc *fdp = p->p_fd;
487 	struct file *fp;
488 	struct vnode *vp;
489 
490 	if ((u_int)fd >= fdp->fd_nfiles ||
491 	    (fp = fdp->fd_ofiles[fd]) == NULL)
492 		return (EBADF);
493 	switch (fp->f_type) {
494 
495 	case DTYPE_SOCKET:
496 		if (SCARG(uap, name) != _PC_PIPE_BUF)
497 			return (EINVAL);
498 		*retval = PIPE_BUF;
499 		return (0);
500 
501 	case DTYPE_VNODE:
502 		vp = (struct vnode *)fp->f_data;
503 #ifdef notyet
504 		return (VOP_PATHCONF(vp, SCARG(uap, name), retval));
505 #else
506 		return (ENOSYS);
507 #endif
508 
509 	default:
510 		panic("fpathconf");
511 	}
512 	/*NOTREACHED*/
513 }
514 
515 /*
516  * Allocate a file descriptor for the process.
517  */
518 int fdexpand;
519 
520 fdalloc(p, want, result)
521 	struct proc *p;
522 	int want;
523 	int *result;
524 {
525 	register struct filedesc *fdp = p->p_fd;
526 	register int i;
527 	int lim, last, nfiles;
528 	struct file **newofile;
529 	char *newofileflags;
530 
531 	/*
532 	 * Search for a free descriptor starting at the higher
533 	 * of want or fd_freefile.  If that fails, consider
534 	 * expanding the ofile array.
535 	 */
536 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
537 	for (;;) {
538 		last = min(fdp->fd_nfiles, lim);
539 		if ((i = want) < fdp->fd_freefile)
540 			i = fdp->fd_freefile;
541 		for (; i < last; i++) {
542 			if (fdp->fd_ofiles[i] == NULL) {
543 				fd_used(fdp, i);
544 				if (want <= fdp->fd_freefile)
545 					fdp->fd_freefile = i;
546 				*result = i;
547 				return (0);
548 			}
549 		}
550 
551 		/*
552 		 * No space in current array.  Expand?
553 		 */
554 		if (fdp->fd_nfiles >= lim)
555 			return (EMFILE);
556 		if (fdp->fd_nfiles < NDEXTENT)
557 			nfiles = NDEXTENT;
558 		else
559 			nfiles = 2 * fdp->fd_nfiles;
560 		MALLOC(newofile, struct file **, nfiles * OFILESIZE,
561 		    M_FILEDESC, M_WAITOK);
562 		newofileflags = (char *) &newofile[nfiles];
563 		/*
564 		 * Copy the existing ofile and ofileflags arrays
565 		 * and zero the new portion of each array.
566 		 */
567 		bcopy(fdp->fd_ofiles, newofile,
568 			(i = sizeof(struct file *) * fdp->fd_nfiles));
569 		bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i);
570 		bcopy(fdp->fd_ofileflags, newofileflags,
571 			(i = sizeof(char) * fdp->fd_nfiles));
572 		bzero(newofileflags + i, nfiles * sizeof(char) - i);
573 		if (fdp->fd_nfiles > NDFILE)
574 			FREE(fdp->fd_ofiles, M_FILEDESC);
575 		fdp->fd_ofiles = newofile;
576 		fdp->fd_ofileflags = newofileflags;
577 		fdp->fd_nfiles = nfiles;
578 		fdexpand++;
579 	}
580 }
581 
582 /*
583  * Check to see whether n user file descriptors
584  * are available to the process p.
585  */
586 fdavail(p, n)
587 	struct proc *p;
588 	register int n;
589 {
590 	register struct filedesc *fdp = p->p_fd;
591 	register struct file **fpp;
592 	register int i, lim;
593 
594 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
595 	if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
596 		return (1);
597 	fpp = &fdp->fd_ofiles[fdp->fd_freefile];
598 	for (i = fdp->fd_nfiles - fdp->fd_freefile; --i >= 0; fpp++)
599 		if (*fpp == NULL && --n <= 0)
600 			return (1);
601 	return (0);
602 }
603 
604 /*
605  * Create a new open file structure and allocate
606  * a file decriptor for the process that refers to it.
607  */
608 falloc(p, resultfp, resultfd)
609 	register struct proc *p;
610 	struct file **resultfp;
611 	int *resultfd;
612 {
613 	register struct file *fp, *fq;
614 	int error, i;
615 
616 	if (error = fdalloc(p, 0, &i))
617 		return (error);
618 	if (nfiles >= maxfiles) {
619 		tablefull("file");
620 		return (ENFILE);
621 	}
622 	/*
623 	 * Allocate a new file descriptor.
624 	 * If the process has file descriptor zero open, add to the list
625 	 * of open files at that point, otherwise put it at the front of
626 	 * the list of open files.
627 	 */
628 	nfiles++;
629 	MALLOC(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK);
630 	bzero(fp, sizeof(struct file));
631 	if (fq = p->p_fd->fd_ofiles[0]) {
632 		LIST_INSERT_AFTER(fq, fp, f_list);
633 	} else {
634 		LIST_INSERT_HEAD(&filehead, fp, f_list);
635 	}
636 	p->p_fd->fd_ofiles[i] = fp;
637 	fp->f_count = 1;
638 	fp->f_cred = p->p_ucred;
639 	crhold(fp->f_cred);
640 	if (resultfp)
641 		*resultfp = fp;
642 	if (resultfd)
643 		*resultfd = i;
644 	return (0);
645 }
646 
647 /*
648  * Free a file descriptor.
649  */
650 ffree(fp)
651 	register struct file *fp;
652 {
653 	register struct file *fq;
654 
655 	LIST_REMOVE(fp, f_list);
656 	crfree(fp->f_cred);
657 #ifdef DIAGNOSTIC
658 	fp->f_count = 0;
659 #endif
660 	nfiles--;
661 	FREE(fp, M_FILE);
662 }
663 
664 /*
665  * Copy a filedesc structure.
666  */
667 struct filedesc *
668 fdcopy(p)
669 	struct proc *p;
670 {
671 	register struct filedesc *newfdp, *fdp = p->p_fd;
672 	register struct file **fpp;
673 	register int i;
674 
675 	MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0),
676 	    M_FILEDESC, M_WAITOK);
677 	bcopy(fdp, newfdp, sizeof(struct filedesc));
678 	VREF(newfdp->fd_cdir);
679 	if (newfdp->fd_rdir)
680 		VREF(newfdp->fd_rdir);
681 	newfdp->fd_refcnt = 1;
682 
683 	/*
684 	 * If the number of open files fits in the internal arrays
685 	 * of the open file structure, use them, otherwise allocate
686 	 * additional memory for the number of descriptors currently
687 	 * in use.
688 	 */
689 	if (newfdp->fd_lastfile < NDFILE) {
690 		newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
691 		newfdp->fd_ofileflags =
692 		    ((struct filedesc0 *) newfdp)->fd_dfileflags;
693 		i = NDFILE;
694 	} else {
695 		/*
696 		 * Compute the smallest multiple of NDEXTENT needed
697 		 * for the file descriptors currently in use,
698 		 * allowing the table to shrink.
699 		 */
700 		i = newfdp->fd_nfiles;
701 		while (i >= 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
702 			i /= 2;
703 		MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
704 		    M_FILEDESC, M_WAITOK);
705 		newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
706 	}
707 	newfdp->fd_nfiles = i;
708 	bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **));
709 	bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char));
710 	fpp = newfdp->fd_ofiles;
711 	for (i = newfdp->fd_lastfile; i >= 0; i--, fpp++)
712 		if (*fpp != NULL)
713 			(*fpp)->f_count++;
714 	return (newfdp);
715 }
716 
717 /*
718  * Release a filedesc structure.
719  */
720 void
721 fdfree(p)
722 	struct proc *p;
723 {
724 	register struct filedesc *fdp = p->p_fd;
725 	struct file **fpp;
726 	register int i;
727 
728 	if (--fdp->fd_refcnt > 0)
729 		return;
730 	fpp = fdp->fd_ofiles;
731 	for (i = fdp->fd_lastfile; i >= 0; i--, fpp++)
732 		if (*fpp != NULL)
733 			(void) closef(*fpp, p);
734 	if (fdp->fd_nfiles > NDFILE)
735 		FREE(fdp->fd_ofiles, M_FILEDESC);
736 	vrele(fdp->fd_cdir);
737 	if (fdp->fd_rdir)
738 		vrele(fdp->fd_rdir);
739 	FREE(fdp, M_FILEDESC);
740 }
741 
742 /*
743  * Internal form of close.
744  * Decrement reference count on file structure.
745  * Note: p may be NULL when closing a file
746  * that was being passed in a message.
747  */
748 closef(fp, p)
749 	register struct file *fp;
750 	register struct proc *p;
751 {
752 	struct vnode *vp;
753 	struct flock lf;
754 	int error;
755 
756 	if (fp == NULL)
757 		return (0);
758 	/*
759 	 * POSIX record locking dictates that any close releases ALL
760 	 * locks owned by this process.  This is handled by setting
761 	 * a flag in the unlock to free ONLY locks obeying POSIX
762 	 * semantics, and not to free BSD-style file locks.
763 	 * If the descriptor was in a message, POSIX-style locks
764 	 * aren't passed with the descriptor.
765 	 */
766 	if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
767 		lf.l_whence = SEEK_SET;
768 		lf.l_start = 0;
769 		lf.l_len = 0;
770 		lf.l_type = F_UNLCK;
771 		vp = (struct vnode *)fp->f_data;
772 		(void) VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX);
773 	}
774 	if (--fp->f_count > 0)
775 		return (0);
776 	if (fp->f_count < 0)
777 		panic("closef: count < 0");
778 	if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
779 		lf.l_whence = SEEK_SET;
780 		lf.l_start = 0;
781 		lf.l_len = 0;
782 		lf.l_type = F_UNLCK;
783 		vp = (struct vnode *)fp->f_data;
784 		(void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
785 	}
786 	if (fp->f_ops)
787 		error = (*fp->f_ops->fo_close)(fp, p);
788 	else
789 		error = 0;
790 	ffree(fp);
791 	return (error);
792 }
793 
794 /*
795  * Apply an advisory lock on a file descriptor.
796  *
797  * Just attempt to get a record lock of the requested type on
798  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
799  */
800 /* ARGSUSED */
801 flock(p, uap, retval)
802 	struct proc *p;
803 	register struct flock_args /* {
804 		syscallarg(int) fd;
805 		syscallarg(int) how;
806 	} */ *uap;
807 	register_t *retval;
808 {
809 	int fd = SCARG(uap, fd);
810 	int how = SCARG(uap, how);
811 	register struct filedesc *fdp = p->p_fd;
812 	register struct file *fp;
813 	struct vnode *vp;
814 	struct flock lf;
815 
816 	if ((u_int)fd >= fdp->fd_nfiles ||
817 	    (fp = fdp->fd_ofiles[fd]) == NULL)
818 		return (EBADF);
819 	if (fp->f_type != DTYPE_VNODE)
820 		return (EOPNOTSUPP);
821 	vp = (struct vnode *)fp->f_data;
822 	lf.l_whence = SEEK_SET;
823 	lf.l_start = 0;
824 	lf.l_len = 0;
825 	if (how & LOCK_UN) {
826 		lf.l_type = F_UNLCK;
827 		fp->f_flag &= ~FHASLOCK;
828 		return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
829 	}
830 	if (how & LOCK_EX)
831 		lf.l_type = F_WRLCK;
832 	else if (how & LOCK_SH)
833 		lf.l_type = F_RDLCK;
834 	else
835 		return (EBADF);
836 	fp->f_flag |= FHASLOCK;
837 	if (how & LOCK_NB)
838 		return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
839 	return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
840 }
841 
842 /*
843  * File Descriptor pseudo-device driver (/dev/fd/).
844  *
845  * Opening minor device N dup()s the file (if any) connected to file
846  * descriptor N belonging to the calling process.  Note that this driver
847  * consists of only the ``open()'' routine, because all subsequent
848  * references to this file will be direct to the other driver.
849  */
850 /* ARGSUSED */
851 int
852 fdopen(dev, mode, type, p)
853 	dev_t dev;
854 	int mode, type;
855 	struct proc *p;
856 {
857 
858 	/*
859 	 * XXX Kludge: set curproc->p_dupfd to contain the value of the
860 	 * the file descriptor being sought for duplication. The error
861 	 * return ensures that the vnode for this device will be released
862 	 * by vn_open. Open will detect this special error and take the
863 	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
864 	 * will simply report the error.
865 	 */
866 	p->p_dupfd = minor(dev);
867 	return (ENODEV);
868 }
869 
870 /*
871  * Duplicate the specified descriptor to a free descriptor.
872  */
873 int
874 dupfdopen(fdp, indx, dfd, mode, error)
875 	register struct filedesc *fdp;
876 	register int indx, dfd;
877 	int mode;
878 	int error;
879 {
880 	register struct file *wfp;
881 	struct file *fp;
882 
883 	/*
884 	 * If the to-be-dup'd fd number is greater than the allowed number
885 	 * of file descriptors, or the fd to be dup'd has already been
886 	 * closed, reject.  Note, check for new == old is necessary as
887 	 * falloc could allocate an already closed to-be-dup'd descriptor
888 	 * as the new descriptor.
889 	 */
890 	fp = fdp->fd_ofiles[indx];
891 	if ((u_int)dfd >= fdp->fd_nfiles ||
892 	    (wfp = fdp->fd_ofiles[dfd]) == NULL || fp == wfp)
893 		return (EBADF);
894 
895 	/*
896 	 * There are two cases of interest here.
897 	 *
898 	 * For ENODEV simply dup (dfd) to file descriptor
899 	 * (indx) and return.
900 	 *
901 	 * For ENXIO steal away the file structure from (dfd) and
902 	 * store it in (indx).  (dfd) is effectively closed by
903 	 * this operation.
904 	 *
905 	 * Any other error code is just returned.
906 	 */
907 	switch (error) {
908 	case ENODEV:
909 		/*
910 		 * Check that the mode the file is being opened for is a
911 		 * subset of the mode of the existing descriptor.
912 		 */
913 		if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
914 			return (EACCES);
915 		fdp->fd_ofiles[indx] = wfp;
916 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
917 		wfp->f_count++;
918 		fd_used(fdp, indx);
919 		return (0);
920 
921 	case ENXIO:
922 		/*
923 		 * Steal away the file pointer from dfd, and stuff it into indx.
924 		 */
925 		fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
926 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
927 		fdp->fd_ofiles[dfd] = NULL;
928 		fdp->fd_ofileflags[dfd] = 0;
929 		/*
930 		 * Complete the clean up of the filedesc structure by
931 		 * recomputing the various hints.
932 		 */
933 		fd_used(fdp, indx);
934 		fd_unused(fdp, dfd);
935 		return (0);
936 
937 	default:
938 		return (error);
939 	}
940 	/* NOTREACHED */
941 }
942 
943 /*
944  * Close any files on exec?
945  */
946 void
947 fdcloseexec(p)
948 	struct proc *p;
949 {
950 	register struct filedesc *fdp = p->p_fd;
951 	register int fd;
952 
953 	for (fd = 0; fd <= fdp->fd_lastfile; fd++)
954 		if (fdp->fd_ofileflags[fd] & UF_EXCLOSE)
955 			(void) fdclose(p, fd);
956 }
957