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