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