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