xref: /original-bsd/sys/kern/kern_descrip.c (revision ba762ddc)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)kern_descrip.c	7.23 (Berkeley) 04/16/91
8  */
9 
10 #include "param.h"
11 #include "systm.h"
12 #include "filedesc.h"
13 #include "kernel.h"
14 #include "vnode.h"
15 #include "proc.h"
16 #include "file.h"
17 #include "socket.h"
18 #include "socketvar.h"
19 #include "stat.h"
20 #include "ioctl.h"
21 #include "fcntl.h"
22 #include "malloc.h"
23 #include "syslog.h"
24 #include "resourcevar.h"
25 
26 /*
27  * Descriptor management.
28  */
29 
30 /*
31  * System calls on descriptors.
32  */
33 /* ARGSUSED */
34 getdtablesize(p, uap, retval)
35 	struct proc *p;
36 	struct args *uap;
37 	int *retval;
38 {
39 
40 	*retval = p->p_rlimit[RLIMIT_OFILE].rlim_cur;
41 	return (0);
42 }
43 
44 /*
45  * Duplicate a file descriptor.
46  */
47 /* ARGSUSED */
48 dup(p, uap, retval)
49 	struct proc *p;
50 	struct args {
51 		int	i;
52 	} *uap;
53 	int *retval;
54 {
55 	register struct filedesc *fdp = p->p_fd;
56 	struct file *fp;
57 	int fd, error;
58 
59 	/*
60 	 * XXX Compatibility
61 	 */
62 	if (uap->i &~ 077) { uap->i &= 077; return (dup2(p, uap, retval)); }
63 
64 	if ((unsigned)uap->i >= fdp->fd_nfiles ||
65 	    (fp = fdp->fd_ofiles[uap->i]) == NULL)
66 		return (EBADF);
67 	if (error = fdalloc(p, 0, &fd))
68 		return (error);
69 	fdp->fd_ofiles[fd] = fp;
70 	fdp->fd_ofileflags[fd] = fdp->fd_ofileflags[uap->i] &~ UF_EXCLOSE;
71 	fp->f_count++;
72 	if (fd > fdp->fd_lastfile)
73 		fdp->fd_lastfile = fd;
74 	*retval = fd;
75 	return (0);
76 }
77 
78 /*
79  * Duplicate a file descriptor to a particular value.
80  */
81 /* ARGSUSED */
82 dup2(p, uap, retval)
83 	struct proc *p;
84 	struct args {
85 		u_int	from;
86 		u_int	to;
87 	} *uap;
88 	int *retval;
89 {
90 	register struct filedesc *fdp = p->p_fd;
91 	register struct file *fp;
92 	register u_int old = uap->from, new = uap->to;
93 	int i, error;
94 
95 	if (old >= fdp->fd_nfiles ||
96 	    (fp = fdp->fd_ofiles[old]) == NULL ||
97 	    new >= p->p_rlimit[RLIMIT_OFILE].rlim_cur)
98 		return (EBADF);
99 	*retval = new;
100 	if (old == new)
101 		return (0);
102 	if (new >= fdp->fd_nfiles) {
103 		if (error = fdalloc(p, new, &i))
104 			return (error);
105 		if (new != i)
106 			panic("dup2: fdalloc");
107 	} else if (fdp->fd_ofiles[new]) {
108 		if (fdp->fd_ofileflags[new] & UF_MAPPED)
109 			(void) munmapfd(p, new);
110 		/*
111 		 * dup2() must succeed even if the close has an error.
112 		 */
113 		(void) closef(fdp->fd_ofiles[new], p);
114 	}
115 	fdp->fd_ofiles[new] = fp;
116 	fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
117 	fp->f_count++;
118 	if (new > fdp->fd_lastfile)
119 		fdp->fd_lastfile = new;
120 	return (0);
121 }
122 
123 /*
124  * The file control system call.
125  */
126 /* ARGSUSED */
127 fcntl(p, uap, retval)
128 	struct proc *p;
129 	register struct args {
130 		int	fd;
131 		int	cmd;
132 		int	arg;
133 	} *uap;
134 	int *retval;
135 {
136 	register struct filedesc *fdp = p->p_fd;
137 	register struct file *fp;
138 	register char *pop;
139 	struct vnode *vp;
140 	int i, tmp, error, flg = F_POSIX;
141 	struct flock fl;
142 
143 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
144 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
145 		return (EBADF);
146 	pop = &fdp->fd_ofileflags[uap->fd];
147 	switch(uap->cmd) {
148 	case F_DUPFD:
149 		if ((unsigned)uap->arg >= p->p_rlimit[RLIMIT_OFILE].rlim_cur)
150 			return (EINVAL);
151 		if (error = fdalloc(p, uap->arg, &i))
152 			return (error);
153 		fdp->fd_ofiles[i] = fp;
154 		fdp->fd_ofileflags[i] = *pop &~ UF_EXCLOSE;
155 		fp->f_count++;
156 		if (i > fdp->fd_lastfile)
157 			fdp->fd_lastfile = i;
158 		*retval = i;
159 		return (0);
160 
161 	case F_GETFD:
162 		*retval = *pop & 1;
163 		return (0);
164 
165 	case F_SETFD:
166 		*pop = (*pop &~ 1) | (uap->arg & 1);
167 		return (0);
168 
169 	case F_GETFL:
170 		*retval = OFLAGS(fp->f_flag);
171 		return (0);
172 
173 	case F_SETFL:
174 		fp->f_flag &= ~FCNTLFLAGS;
175 		fp->f_flag |= FFLAGS(uap->arg) & FCNTLFLAGS;
176 		if (tmp = (fp->f_flag & FNDELAY))
177 			fp->f_flag |= FNDELAY;
178 		else
179 			fp->f_flag &= ~FNDELAY;
180 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
181 		if (error)
182 			return (error);
183 		if (tmp = (fp->f_flag & FASYNC))
184 			fp->f_flag |= FASYNC;
185 		else
186 			fp->f_flag &= ~FASYNC;
187 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
188 		if (!error)
189 			return (0);
190 		fp->f_flag &= ~FNDELAY;
191 		tmp = 0;
192 		(void) (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
193 		return (error);
194 
195 	case F_GETOWN:
196 		if (fp->f_type == DTYPE_SOCKET) {
197 			*retval = ((struct socket *)fp->f_data)->so_pgid;
198 			return (0);
199 		}
200 		error = (*fp->f_ops->fo_ioctl)
201 			(fp, (int)TIOCGPGRP, (caddr_t)retval, p);
202 		*retval = -*retval;
203 		return (error);
204 
205 	case F_SETOWN:
206 		if (fp->f_type == DTYPE_SOCKET) {
207 			((struct socket *)fp->f_data)->so_pgid = uap->arg;
208 			return (0);
209 		}
210 		if (uap->arg <= 0) {
211 			uap->arg = -uap->arg;
212 		} else {
213 			struct proc *p1 = pfind(uap->arg);
214 			if (p1 == 0)
215 				return (ESRCH);
216 			uap->arg = p1->p_pgrp->pg_id;
217 		}
218 		return ((*fp->f_ops->fo_ioctl)
219 			(fp, (int)TIOCSPGRP, (caddr_t)&uap->arg, p));
220 
221 	case F_SETLKW:
222 		flg |= F_WAIT;
223 		/* Fall into F_SETLK */
224 
225 	case F_SETLK:
226 		if (fp->f_type != DTYPE_VNODE)
227 			return (EBADF);
228 		vp = (struct vnode *)fp->f_data;
229 		/* Copy in the lock structure */
230 		error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl));
231 		if (error)
232 			return (error);
233 		if (fl.l_whence == SEEK_CUR)
234 			fl.l_start += fp->f_offset;
235 		switch (fl.l_type) {
236 
237 		case F_RDLCK:
238 			if ((fp->f_flag & FREAD) == 0)
239 				return (EBADF);
240 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
241 
242 		case F_WRLCK:
243 			if ((fp->f_flag & FWRITE) == 0)
244 				return (EBADF);
245 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
246 
247 		case F_UNLCK:
248 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl,
249 				F_POSIX));
250 
251 		default:
252 			return (EINVAL);
253 		}
254 
255 	case F_GETLK:
256 		if (fp->f_type != DTYPE_VNODE)
257 			return (EBADF);
258 		vp = (struct vnode *)fp->f_data;
259 		/* Copy in the lock structure */
260 		error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl));
261 		if (error)
262 			return (error);
263 		if (fl.l_whence == SEEK_CUR)
264 			fl.l_start += fp->f_offset;
265 		if (error = VOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX))
266 			return (error);
267 		return (copyout((caddr_t)&fl, (caddr_t)uap->arg, sizeof (fl)));
268 
269 	default:
270 		return (EINVAL);
271 	}
272 	/* NOTREACHED */
273 }
274 
275 /*
276  * Close a file descriptor.
277  */
278 /* ARGSUSED */
279 close(p, uap, retval)
280 	struct proc *p;
281 	struct args {
282 		int	fd;
283 	} *uap;
284 	int *retval;
285 {
286 	register struct filedesc *fdp = p->p_fd;
287 	register struct file *fp;
288 	register int fd = uap->fd;
289 	register u_char *pf;
290 
291 	if ((unsigned)fd >= fdp->fd_nfiles ||
292 	    (fp = fdp->fd_ofiles[fd]) == NULL)
293 		return (EBADF);
294 	pf = (u_char *)&fdp->fd_ofileflags[fd];
295 	if (*pf & UF_MAPPED)
296 		(void) munmapfd(p, fd);
297 	fdp->fd_ofiles[fd] = NULL;
298 	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
299 		fdp->fd_lastfile--;
300 	if (fd < fdp->fd_freefile)
301 		fdp->fd_freefile = fd;
302 	*pf = 0;
303 	return (closef(fp, p));
304 }
305 
306 /*
307  * Return status information about a file descriptor.
308  */
309 /* ARGSUSED */
310 fstat(p, uap, retval)
311 	struct proc *p;
312 	register struct args {
313 		int	fd;
314 		struct	stat *sb;
315 	} *uap;
316 	int *retval;
317 {
318 	register struct filedesc *fdp = p->p_fd;
319 	register struct file *fp;
320 	struct stat ub;
321 	int error;
322 
323 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
324 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
325 		return (EBADF);
326 	switch (fp->f_type) {
327 
328 	case DTYPE_VNODE:
329 		error = vn_stat((struct vnode *)fp->f_data, &ub, p);
330 		break;
331 
332 	case DTYPE_SOCKET:
333 		error = soo_stat((struct socket *)fp->f_data, &ub);
334 		break;
335 
336 	default:
337 		panic("fstat");
338 		/*NOTREACHED*/
339 	}
340 	if (error == 0)
341 		error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub));
342 	return (error);
343 }
344 
345 /*
346  * Allocate a file descriptor for the process.
347  */
348 int fdexpand;
349 
350 fdalloc(p, want, result)
351 	struct proc *p;
352 	int want;
353 	int *result;
354 {
355 	register struct filedesc *fdp = p->p_fd;
356 	register int i;
357 	int lim, last, nfiles;
358 	struct file **newofile;
359 	char *newofileflags;
360 
361 	/*
362 	 * Search for a free descriptor starting at the higher
363 	 * of want or fd_freefile.  If that fails, consider
364 	 * expanding the ofile array.
365 	 */
366 	lim = p->p_rlimit[RLIMIT_OFILE].rlim_cur;
367 	for (;;) {
368 		last = min(fdp->fd_nfiles, lim);
369 		if ((i = want) < fdp->fd_freefile)
370 			i = fdp->fd_freefile;
371 		for (; i < last; i++) {
372 			if (fdp->fd_ofiles[i] == NULL) {
373 				fdp->fd_ofileflags[i] = 0;
374 				if (i > fdp->fd_lastfile)
375 					fdp->fd_lastfile = i;
376 				if (want <= fdp->fd_freefile)
377 					fdp->fd_freefile = i;
378 				*result = i;
379 				return (0);
380 			}
381 		}
382 
383 		/*
384 		 * No space in current array.  Expand?
385 		 */
386 		if (fdp->fd_nfiles >= lim)
387 			return (EMFILE);
388 		if (fdp->fd_nfiles < NDEXTENT)
389 			nfiles = NDEXTENT;
390 		else
391 			nfiles = 2 * fdp->fd_nfiles;
392 		MALLOC(newofile, struct file **, nfiles * OFILESIZE,
393 		    M_FILEDESC, M_WAITOK);
394 		newofileflags = (char *) &newofile[nfiles];
395 		/*
396 		 * Copy the existing ofile and ofileflags arrays
397 		 * and zero the new portion of each array.
398 		 */
399 		bcopy(fdp->fd_ofiles, newofile,
400 			(i = sizeof(struct file *) * fdp->fd_nfiles));
401 		bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i);
402 		bcopy(fdp->fd_ofileflags, newofileflags,
403 			(i = sizeof(char) * fdp->fd_nfiles));
404 		bzero(newofileflags + i, nfiles * sizeof(char) - i);
405 		if (fdp->fd_nfiles > NDFILE)
406 			FREE(fdp->fd_ofiles, M_FILEDESC);
407 		fdp->fd_ofiles = newofile;
408 		fdp->fd_ofileflags = newofileflags;
409 		fdp->fd_nfiles = nfiles;
410 		fdexpand++;
411 	}
412 }
413 
414 /*
415  * Check to see whether n user file descriptors
416  * are available to the process p.
417  */
418 fdavail(p, n)
419 	struct proc *p;
420 	register int n;
421 {
422 	register struct filedesc *fdp = p->p_fd;
423 	register struct file **fpp;
424 	register int i;
425 
426 	if ((i = p->p_rlimit[RLIMIT_OFILE].rlim_cur - fdp->fd_nfiles) > 0 &&
427 	    (n -= i) <= 0)
428 		return (1);
429 	fpp = &fdp->fd_ofiles[fdp->fd_freefile];
430 	for (i = fdp->fd_nfiles - fdp->fd_freefile; --i >= 0; fpp++)
431 		if (*fpp == NULL && --n <= 0)
432 			return (1);
433 	return (0);
434 }
435 
436 struct	file *lastf;
437 /*
438  * Create a new open file structure and allocate
439  * a file decriptor for the process that refers to it.
440  */
441 falloc(p, resultfp, resultfd)
442 	register struct proc *p;
443 	struct file **resultfp;
444 	int *resultfd;
445 {
446 	register struct file *fp;
447 	int error, i;
448 
449 	if (error = fdalloc(p, 0, &i))
450 		return (error);
451 	if (lastf == 0)
452 		lastf = file;
453 	for (fp = lastf; fp < fileNFILE; fp++)
454 		if (fp->f_count == 0)
455 			goto slot;
456 	for (fp = file; fp < lastf; fp++)
457 		if (fp->f_count == 0)
458 			goto slot;
459 	tablefull("file");
460 	return (ENFILE);
461 slot:
462 	p->p_fd->fd_ofiles[i] = fp;
463 	fp->f_count = 1;
464 	fp->f_data = 0;
465 	fp->f_offset = 0;
466 	fp->f_cred = p->p_ucred;
467 	crhold(fp->f_cred);
468 	lastf = fp + 1;
469 	if (resultfp)
470 		*resultfp = fp;
471 	if (resultfd)
472 		*resultfd = i;
473 	return (0);
474 }
475 
476 /*
477  * Copy a filedesc structure.
478  */
479 struct filedesc *
480 fdcopy(p)
481 	struct proc *p;
482 {
483 	register struct filedesc *newfdp, *fdp = p->p_fd;
484 	register struct file **fpp;
485 	register int i;
486 
487 	MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0),
488 	    M_FILEDESC, M_WAITOK);
489 	bcopy(fdp, newfdp, sizeof(struct filedesc));
490 	VREF(newfdp->fd_cdir);
491 	if (newfdp->fd_rdir)
492 		VREF(newfdp->fd_rdir);
493 	newfdp->fd_refcnt = 1;
494 
495 	/*
496 	 * If the number of open files fits in the internal arrays
497 	 * of the open file structure, use them, otherwise allocate
498 	 * additional memory for the number of descriptors currently
499 	 * in use.
500 	 */
501 	if (newfdp->fd_lastfile < NDFILE) {
502 		newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
503 		newfdp->fd_ofileflags =
504 		    ((struct filedesc0 *) newfdp)->fd_dfileflags;
505 		i = NDFILE;
506 	} else {
507 		/*
508 		 * Compute the smallest multiple of NDEXTENT needed
509 		 * for the file descriptors currently in use,
510 		 * allowing the table to shrink.
511 		 */
512 		i = newfdp->fd_nfiles;
513 		while (i > 2 * NDEXTENT && i >= newfdp->fd_lastfile * 2)
514 			i /= 2;
515 		MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
516 		    M_FILEDESC, M_WAITOK);
517 		newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
518 	}
519 	newfdp->fd_nfiles = i;
520 	bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **));
521 	bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char));
522 	fpp = newfdp->fd_ofiles;
523 	for (i = newfdp->fd_lastfile; i-- >= 0; fpp++)
524 		if (*fpp != NULL)
525 			(*fpp)->f_count++;
526 	return (newfdp);
527 }
528 
529 /*
530  * Release a filedesc structure.
531  */
532 void
533 fdfree(p)
534 	struct proc *p;
535 {
536 	register struct filedesc *fdp = p->p_fd;
537 	struct file **fpp;
538 	register int i;
539 
540 	if (--fdp->fd_refcnt > 0)
541 		return;
542 	fpp = fdp->fd_ofiles;
543 	for (i = fdp->fd_lastfile; i-- >= 0; fpp++)
544 		if (*fpp)
545 			(void) closef(*fpp, p);
546 	if (fdp->fd_nfiles > NDFILE)
547 		FREE(fdp->fd_ofiles, M_FILEDESC);
548 	vrele(fdp->fd_cdir);
549 	if (fdp->fd_rdir)
550 		vrele(fdp->fd_rdir);
551 	FREE(fdp, M_FILEDESC);
552 }
553 
554 /*
555  * Internal form of close.
556  * Decrement reference count on file structure.
557  */
558 closef(fp, p)
559 	register struct file *fp;
560 	struct proc *p;
561 {
562 	struct vnode *vp;
563 	struct flock lf;
564 	int error;
565 
566 	if (fp == NULL)
567 		return (0);
568 	/*
569 	 * POSIX record locking dictates that any close releases ALL
570 	 * locks owned by this process.  This is handled by setting
571 	 * a flag in the unlock to free ONLY locks obeying POSIX
572 	 * semantics, and not to free BSD-style file locks.
573 	 */
574 	if (fp->f_type == DTYPE_VNODE) {
575 		lf.l_whence = SEEK_SET;
576 		lf.l_start = 0;
577 		lf.l_len = 0;
578 		lf.l_type = F_UNLCK;
579 		vp = (struct vnode *)fp->f_data;
580 		(void) VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX);
581 	}
582 	if (--fp->f_count > 0)
583 		return (0);
584 	if (fp->f_count < 0)
585 		panic("closef: count < 0");
586 	if (fp->f_type == DTYPE_VNODE)
587 		(void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
588 	error = (*fp->f_ops->fo_close)(fp, p);
589 	crfree(fp->f_cred);
590 	fp->f_count = 0;
591 	return (error);
592 }
593 
594 /*
595  * Apply an advisory lock on a file descriptor.
596  *
597  * Just attempt to get a record lock of the requested type on
598  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
599  */
600 
601 /* ARGSUSED */
602 flock(p, uap, retval)
603 	struct proc *p;
604 	register struct args {
605 		int	fd;
606 		int	how;
607 	} *uap;
608 	int *retval;
609 {
610 	register struct filedesc *fdp = p->p_fd;
611 	register struct file *fp;
612 	struct vnode *vp;
613 	struct flock lf;
614 	int error;
615 
616 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
617 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
618 		return (EBADF);
619 	if (fp->f_type != DTYPE_VNODE)
620 		return (EOPNOTSUPP);
621 	vp = (struct vnode *)fp->f_data;
622 	lf.l_whence = SEEK_SET;
623 	lf.l_start = 0;
624 	lf.l_len = 0;
625 	if (uap->how & LOCK_UN) {
626 		lf.l_type = F_UNLCK;
627 		return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
628 	}
629 	if (uap->how & LOCK_EX)
630 		lf.l_type = F_WRLCK;
631 	else if (uap->how & LOCK_SH)
632 		lf.l_type = F_RDLCK;
633 	else
634 		return (EBADF);
635 	if (uap->how & LOCK_NB)
636 		return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
637 	return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
638 }
639 
640 /*
641  * File Descriptor pseudo-device driver (/dev/fd/).
642  *
643  * Opening minor device N dup()s the file (if any) connected to file
644  * descriptor N belonging to the calling process.  Note that this driver
645  * consists of only the ``open()'' routine, because all subsequent
646  * references to this file will be direct to the other driver.
647  */
648 /* ARGSUSED */
649 fdopen(dev, mode, type)
650 	dev_t dev;
651 	int mode, type;
652 {
653 
654 	/*
655 	 * XXX Kludge: set curproc->p_dupfd to contain the value of the
656 	 * the file descriptor being sought for duplication. The error
657 	 * return ensures that the vnode for this device will be released
658 	 * by vn_open. Open will detect this special error and take the
659 	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
660 	 * will simply report the error.
661 	 */
662 	curproc->p_dupfd = minor(dev);		/* XXX */
663 	return (ENODEV);
664 }
665 
666 /*
667  * Duplicate the specified descriptor to a free descriptor.
668  */
669 dupfdopen(fdp, indx, dfd, mode)
670 	register struct filedesc *fdp;
671 	register int indx, dfd;
672 	int mode;
673 {
674 	register struct file *wfp;
675 	struct file *fp;
676 
677 	/*
678 	 * If the to-be-dup'd fd number is greater than the allowed number
679 	 * of file descriptors, or the fd to be dup'd has already been
680 	 * closed, reject.  Note, check for new == old is necessary as
681 	 * falloc could allocate an already closed to-be-dup'd descriptor
682 	 * as the new descriptor.
683 	 */
684 	fp = fdp->fd_ofiles[indx];
685 	if ((u_int)dfd >= fdp->fd_nfiles ||
686 	    (wfp = fdp->fd_ofiles[dfd]) == NULL || fp == wfp)
687 		return (EBADF);
688 
689 	/*
690 	 * Check that the mode the file is being opened for is a subset
691 	 * of the mode of the existing descriptor.
692 	 */
693 	if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
694 		return (EACCES);
695 	fdp->fd_ofiles[indx] = wfp;
696 	fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
697 	wfp->f_count++;
698 	if (indx > fdp->fd_lastfile)
699 		fdp->fd_lastfile = indx;
700 	return (0);
701 }
702