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