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