xref: /csrg-svn/sys/kern/kern_descrip.c (revision 49940)
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.24 (Berkeley) 05/30/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 		tmp = fp->f_flag & FNONBLOCK;
177 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
178 		if (error)
179 			return (error);
180 		tmp = fp->f_flag & FASYNC;
181 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
182 		if (!error)
183 			return (0);
184 		fp->f_flag &= ~FNONBLOCK;
185 		tmp = 0;
186 		(void) (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
187 		return (error);
188 
189 	case F_GETOWN:
190 		if (fp->f_type == DTYPE_SOCKET) {
191 			*retval = ((struct socket *)fp->f_data)->so_pgid;
192 			return (0);
193 		}
194 		error = (*fp->f_ops->fo_ioctl)
195 			(fp, (int)TIOCGPGRP, (caddr_t)retval, p);
196 		*retval = -*retval;
197 		return (error);
198 
199 	case F_SETOWN:
200 		if (fp->f_type == DTYPE_SOCKET) {
201 			((struct socket *)fp->f_data)->so_pgid = uap->arg;
202 			return (0);
203 		}
204 		if (uap->arg <= 0) {
205 			uap->arg = -uap->arg;
206 		} else {
207 			struct proc *p1 = pfind(uap->arg);
208 			if (p1 == 0)
209 				return (ESRCH);
210 			uap->arg = p1->p_pgrp->pg_id;
211 		}
212 		return ((*fp->f_ops->fo_ioctl)
213 			(fp, (int)TIOCSPGRP, (caddr_t)&uap->arg, p));
214 
215 	case F_SETLKW:
216 		flg |= F_WAIT;
217 		/* Fall into F_SETLK */
218 
219 	case F_SETLK:
220 		if (fp->f_type != DTYPE_VNODE)
221 			return (EBADF);
222 		vp = (struct vnode *)fp->f_data;
223 		/* Copy in the lock structure */
224 		error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl));
225 		if (error)
226 			return (error);
227 		if (fl.l_whence == SEEK_CUR)
228 			fl.l_start += fp->f_offset;
229 		switch (fl.l_type) {
230 
231 		case F_RDLCK:
232 			if ((fp->f_flag & FREAD) == 0)
233 				return (EBADF);
234 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
235 
236 		case F_WRLCK:
237 			if ((fp->f_flag & FWRITE) == 0)
238 				return (EBADF);
239 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
240 
241 		case F_UNLCK:
242 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl,
243 				F_POSIX));
244 
245 		default:
246 			return (EINVAL);
247 		}
248 
249 	case F_GETLK:
250 		if (fp->f_type != DTYPE_VNODE)
251 			return (EBADF);
252 		vp = (struct vnode *)fp->f_data;
253 		/* Copy in the lock structure */
254 		error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl));
255 		if (error)
256 			return (error);
257 		if (fl.l_whence == SEEK_CUR)
258 			fl.l_start += fp->f_offset;
259 		if (error = VOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX))
260 			return (error);
261 		return (copyout((caddr_t)&fl, (caddr_t)uap->arg, sizeof (fl)));
262 
263 	default:
264 		return (EINVAL);
265 	}
266 	/* NOTREACHED */
267 }
268 
269 /*
270  * Close a file descriptor.
271  */
272 /* ARGSUSED */
273 close(p, uap, retval)
274 	struct proc *p;
275 	struct args {
276 		int	fd;
277 	} *uap;
278 	int *retval;
279 {
280 	register struct filedesc *fdp = p->p_fd;
281 	register struct file *fp;
282 	register int fd = uap->fd;
283 	register u_char *pf;
284 
285 	if ((unsigned)fd >= fdp->fd_nfiles ||
286 	    (fp = fdp->fd_ofiles[fd]) == NULL)
287 		return (EBADF);
288 	pf = (u_char *)&fdp->fd_ofileflags[fd];
289 	if (*pf & UF_MAPPED)
290 		(void) munmapfd(p, fd);
291 	fdp->fd_ofiles[fd] = NULL;
292 	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
293 		fdp->fd_lastfile--;
294 	if (fd < fdp->fd_freefile)
295 		fdp->fd_freefile = fd;
296 	*pf = 0;
297 	return (closef(fp, p));
298 }
299 
300 /*
301  * Return status information about a file descriptor.
302  */
303 /* ARGSUSED */
304 fstat(p, uap, retval)
305 	struct proc *p;
306 	register struct args {
307 		int	fd;
308 		struct	stat *sb;
309 	} *uap;
310 	int *retval;
311 {
312 	register struct filedesc *fdp = p->p_fd;
313 	register struct file *fp;
314 	struct stat ub;
315 	int error;
316 
317 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
318 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
319 		return (EBADF);
320 	switch (fp->f_type) {
321 
322 	case DTYPE_VNODE:
323 		error = vn_stat((struct vnode *)fp->f_data, &ub, p);
324 		break;
325 
326 	case DTYPE_SOCKET:
327 		error = soo_stat((struct socket *)fp->f_data, &ub);
328 		break;
329 
330 	default:
331 		panic("fstat");
332 		/*NOTREACHED*/
333 	}
334 	if (error == 0)
335 		error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub));
336 	return (error);
337 }
338 
339 /*
340  * Allocate a file descriptor for the process.
341  */
342 int fdexpand;
343 
344 fdalloc(p, want, result)
345 	struct proc *p;
346 	int want;
347 	int *result;
348 {
349 	register struct filedesc *fdp = p->p_fd;
350 	register int i;
351 	int lim, last, nfiles;
352 	struct file **newofile;
353 	char *newofileflags;
354 
355 	/*
356 	 * Search for a free descriptor starting at the higher
357 	 * of want or fd_freefile.  If that fails, consider
358 	 * expanding the ofile array.
359 	 */
360 	lim = p->p_rlimit[RLIMIT_OFILE].rlim_cur;
361 	for (;;) {
362 		last = min(fdp->fd_nfiles, lim);
363 		if ((i = want) < fdp->fd_freefile)
364 			i = fdp->fd_freefile;
365 		for (; i < last; i++) {
366 			if (fdp->fd_ofiles[i] == NULL) {
367 				fdp->fd_ofileflags[i] = 0;
368 				if (i > fdp->fd_lastfile)
369 					fdp->fd_lastfile = i;
370 				if (want <= fdp->fd_freefile)
371 					fdp->fd_freefile = i;
372 				*result = i;
373 				return (0);
374 			}
375 		}
376 
377 		/*
378 		 * No space in current array.  Expand?
379 		 */
380 		if (fdp->fd_nfiles >= lim)
381 			return (EMFILE);
382 		if (fdp->fd_nfiles < NDEXTENT)
383 			nfiles = NDEXTENT;
384 		else
385 			nfiles = 2 * fdp->fd_nfiles;
386 		MALLOC(newofile, struct file **, nfiles * OFILESIZE,
387 		    M_FILEDESC, M_WAITOK);
388 		newofileflags = (char *) &newofile[nfiles];
389 		/*
390 		 * Copy the existing ofile and ofileflags arrays
391 		 * and zero the new portion of each array.
392 		 */
393 		bcopy(fdp->fd_ofiles, newofile,
394 			(i = sizeof(struct file *) * fdp->fd_nfiles));
395 		bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i);
396 		bcopy(fdp->fd_ofileflags, newofileflags,
397 			(i = sizeof(char) * fdp->fd_nfiles));
398 		bzero(newofileflags + i, nfiles * sizeof(char) - i);
399 		if (fdp->fd_nfiles > NDFILE)
400 			FREE(fdp->fd_ofiles, M_FILEDESC);
401 		fdp->fd_ofiles = newofile;
402 		fdp->fd_ofileflags = newofileflags;
403 		fdp->fd_nfiles = nfiles;
404 		fdexpand++;
405 	}
406 }
407 
408 /*
409  * Check to see whether n user file descriptors
410  * are available to the process p.
411  */
412 fdavail(p, n)
413 	struct proc *p;
414 	register int n;
415 {
416 	register struct filedesc *fdp = p->p_fd;
417 	register struct file **fpp;
418 	register int i;
419 
420 	if ((i = p->p_rlimit[RLIMIT_OFILE].rlim_cur - fdp->fd_nfiles) > 0 &&
421 	    (n -= i) <= 0)
422 		return (1);
423 	fpp = &fdp->fd_ofiles[fdp->fd_freefile];
424 	for (i = fdp->fd_nfiles - fdp->fd_freefile; --i >= 0; fpp++)
425 		if (*fpp == NULL && --n <= 0)
426 			return (1);
427 	return (0);
428 }
429 
430 struct	file *lastf;
431 /*
432  * Create a new open file structure and allocate
433  * a file decriptor for the process that refers to it.
434  */
435 falloc(p, resultfp, resultfd)
436 	register struct proc *p;
437 	struct file **resultfp;
438 	int *resultfd;
439 {
440 	register struct file *fp;
441 	int error, i;
442 
443 	if (error = fdalloc(p, 0, &i))
444 		return (error);
445 	if (lastf == 0)
446 		lastf = file;
447 	for (fp = lastf; fp < fileNFILE; fp++)
448 		if (fp->f_count == 0)
449 			goto slot;
450 	for (fp = file; fp < lastf; fp++)
451 		if (fp->f_count == 0)
452 			goto slot;
453 	tablefull("file");
454 	return (ENFILE);
455 slot:
456 	p->p_fd->fd_ofiles[i] = fp;
457 	fp->f_count = 1;
458 	fp->f_data = 0;
459 	fp->f_offset = 0;
460 	fp->f_cred = p->p_ucred;
461 	crhold(fp->f_cred);
462 	lastf = fp + 1;
463 	if (resultfp)
464 		*resultfp = fp;
465 	if (resultfd)
466 		*resultfd = i;
467 	return (0);
468 }
469 
470 /*
471  * Copy a filedesc structure.
472  */
473 struct filedesc *
474 fdcopy(p)
475 	struct proc *p;
476 {
477 	register struct filedesc *newfdp, *fdp = p->p_fd;
478 	register struct file **fpp;
479 	register int i;
480 
481 	MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0),
482 	    M_FILEDESC, M_WAITOK);
483 	bcopy(fdp, newfdp, sizeof(struct filedesc));
484 	VREF(newfdp->fd_cdir);
485 	if (newfdp->fd_rdir)
486 		VREF(newfdp->fd_rdir);
487 	newfdp->fd_refcnt = 1;
488 
489 	/*
490 	 * If the number of open files fits in the internal arrays
491 	 * of the open file structure, use them, otherwise allocate
492 	 * additional memory for the number of descriptors currently
493 	 * in use.
494 	 */
495 	if (newfdp->fd_lastfile < NDFILE) {
496 		newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
497 		newfdp->fd_ofileflags =
498 		    ((struct filedesc0 *) newfdp)->fd_dfileflags;
499 		i = NDFILE;
500 	} else {
501 		/*
502 		 * Compute the smallest multiple of NDEXTENT needed
503 		 * for the file descriptors currently in use,
504 		 * allowing the table to shrink.
505 		 */
506 		i = newfdp->fd_nfiles;
507 		while (i > 2 * NDEXTENT && i >= newfdp->fd_lastfile * 2)
508 			i /= 2;
509 		MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
510 		    M_FILEDESC, M_WAITOK);
511 		newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
512 	}
513 	newfdp->fd_nfiles = i;
514 	bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **));
515 	bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char));
516 	fpp = newfdp->fd_ofiles;
517 	for (i = newfdp->fd_lastfile; i-- >= 0; fpp++)
518 		if (*fpp != NULL)
519 			(*fpp)->f_count++;
520 	return (newfdp);
521 }
522 
523 /*
524  * Release a filedesc structure.
525  */
526 void
527 fdfree(p)
528 	struct proc *p;
529 {
530 	register struct filedesc *fdp = p->p_fd;
531 	struct file **fpp;
532 	register int i;
533 
534 	if (--fdp->fd_refcnt > 0)
535 		return;
536 	fpp = fdp->fd_ofiles;
537 	for (i = fdp->fd_lastfile; i-- >= 0; fpp++)
538 		if (*fpp)
539 			(void) closef(*fpp, p);
540 	if (fdp->fd_nfiles > NDFILE)
541 		FREE(fdp->fd_ofiles, M_FILEDESC);
542 	vrele(fdp->fd_cdir);
543 	if (fdp->fd_rdir)
544 		vrele(fdp->fd_rdir);
545 	FREE(fdp, M_FILEDESC);
546 }
547 
548 /*
549  * Internal form of close.
550  * Decrement reference count on file structure.
551  */
552 closef(fp, p)
553 	register struct file *fp;
554 	struct proc *p;
555 {
556 	struct vnode *vp;
557 	struct flock lf;
558 	int error;
559 
560 	if (fp == NULL)
561 		return (0);
562 	/*
563 	 * POSIX record locking dictates that any close releases ALL
564 	 * locks owned by this process.  This is handled by setting
565 	 * a flag in the unlock to free ONLY locks obeying POSIX
566 	 * semantics, and not to free BSD-style file locks.
567 	 */
568 	if (fp->f_type == DTYPE_VNODE) {
569 		lf.l_whence = SEEK_SET;
570 		lf.l_start = 0;
571 		lf.l_len = 0;
572 		lf.l_type = F_UNLCK;
573 		vp = (struct vnode *)fp->f_data;
574 		(void) VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX);
575 	}
576 	if (--fp->f_count > 0)
577 		return (0);
578 	if (fp->f_count < 0)
579 		panic("closef: count < 0");
580 	if (fp->f_type == DTYPE_VNODE)
581 		(void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
582 	error = (*fp->f_ops->fo_close)(fp, p);
583 	crfree(fp->f_cred);
584 	fp->f_count = 0;
585 	return (error);
586 }
587 
588 /*
589  * Apply an advisory lock on a file descriptor.
590  *
591  * Just attempt to get a record lock of the requested type on
592  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
593  */
594 
595 /* ARGSUSED */
596 flock(p, uap, retval)
597 	struct proc *p;
598 	register struct args {
599 		int	fd;
600 		int	how;
601 	} *uap;
602 	int *retval;
603 {
604 	register struct filedesc *fdp = p->p_fd;
605 	register struct file *fp;
606 	struct vnode *vp;
607 	struct flock lf;
608 	int error;
609 
610 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
611 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
612 		return (EBADF);
613 	if (fp->f_type != DTYPE_VNODE)
614 		return (EOPNOTSUPP);
615 	vp = (struct vnode *)fp->f_data;
616 	lf.l_whence = SEEK_SET;
617 	lf.l_start = 0;
618 	lf.l_len = 0;
619 	if (uap->how & LOCK_UN) {
620 		lf.l_type = F_UNLCK;
621 		return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
622 	}
623 	if (uap->how & LOCK_EX)
624 		lf.l_type = F_WRLCK;
625 	else if (uap->how & LOCK_SH)
626 		lf.l_type = F_RDLCK;
627 	else
628 		return (EBADF);
629 	if (uap->how & LOCK_NB)
630 		return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
631 	return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
632 }
633 
634 /*
635  * File Descriptor pseudo-device driver (/dev/fd/).
636  *
637  * Opening minor device N dup()s the file (if any) connected to file
638  * descriptor N belonging to the calling process.  Note that this driver
639  * consists of only the ``open()'' routine, because all subsequent
640  * references to this file will be direct to the other driver.
641  */
642 /* ARGSUSED */
643 fdopen(dev, mode, type)
644 	dev_t dev;
645 	int mode, type;
646 {
647 
648 	/*
649 	 * XXX Kludge: set curproc->p_dupfd to contain the value of the
650 	 * the file descriptor being sought for duplication. The error
651 	 * return ensures that the vnode for this device will be released
652 	 * by vn_open. Open will detect this special error and take the
653 	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
654 	 * will simply report the error.
655 	 */
656 	curproc->p_dupfd = minor(dev);		/* XXX */
657 	return (ENODEV);
658 }
659 
660 /*
661  * Duplicate the specified descriptor to a free descriptor.
662  */
663 dupfdopen(fdp, indx, dfd, mode)
664 	register struct filedesc *fdp;
665 	register int indx, dfd;
666 	int mode;
667 {
668 	register struct file *wfp;
669 	struct file *fp;
670 
671 	/*
672 	 * If the to-be-dup'd fd number is greater than the allowed number
673 	 * of file descriptors, or the fd to be dup'd has already been
674 	 * closed, reject.  Note, check for new == old is necessary as
675 	 * falloc could allocate an already closed to-be-dup'd descriptor
676 	 * as the new descriptor.
677 	 */
678 	fp = fdp->fd_ofiles[indx];
679 	if ((u_int)dfd >= fdp->fd_nfiles ||
680 	    (wfp = fdp->fd_ofiles[dfd]) == NULL || fp == wfp)
681 		return (EBADF);
682 
683 	/*
684 	 * Check that the mode the file is being opened for is a subset
685 	 * of the mode of the existing descriptor.
686 	 */
687 	if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
688 		return (EACCES);
689 	fdp->fd_ofiles[indx] = wfp;
690 	fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
691 	wfp->f_count++;
692 	if (indx > fdp->fd_lastfile)
693 		fdp->fd_lastfile = indx;
694 	return (0);
695 }
696