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