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