kern_descrip.c revision 1.64 1 /* $NetBSD: kern_descrip.c,v 1.64 2000/03/22 17:42:57 thorpej 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.8 (Berkeley) 2/14/95
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/pool.h>
57 #include <sys/syslog.h>
58 #include <sys/unistd.h>
59 #include <sys/resourcevar.h>
60 #include <sys/conf.h>
61
62 #include <sys/mount.h>
63 #include <sys/syscallargs.h>
64
65 #include <vm/vm.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 struct pool file_pool; /* memory pool for file structures */
73 struct pool cwdi_pool; /* memory pool for cwdinfo structures */
74 struct pool filedesc0_pool; /* memory pool for filedesc0 structures */
75
76 static __inline void fd_used __P((struct filedesc *, int));
77 static __inline void fd_unused __P((struct filedesc *, int));
78 int finishdup __P((struct proc *, int, int, register_t *));
79
80 static __inline void
81 fd_used(fdp, fd)
82 register struct filedesc *fdp;
83 register int fd;
84 {
85
86 if (fd > fdp->fd_lastfile)
87 fdp->fd_lastfile = fd;
88 }
89
90 static __inline void
91 fd_unused(fdp, fd)
92 register struct filedesc *fdp;
93 register int fd;
94 {
95
96 if (fd < fdp->fd_freefile)
97 fdp->fd_freefile = fd;
98 #ifdef DIAGNOSTIC
99 if (fd > fdp->fd_lastfile)
100 panic("fd_unused: fd_lastfile inconsistent");
101 #endif
102 if (fd == fdp->fd_lastfile) {
103 do {
104 fd--;
105 } while (fd >= 0 && fdp->fd_ofiles[fd] == NULL);
106 fdp->fd_lastfile = fd;
107 }
108 }
109
110 /*
111 * System calls on descriptors.
112 */
113
114 /*
115 * Duplicate a file descriptor.
116 */
117 /* ARGSUSED */
118 int
119 sys_dup(p, v, retval)
120 struct proc *p;
121 void *v;
122 register_t *retval;
123 {
124 struct sys_dup_args /* {
125 syscallarg(int) fd;
126 } */ *uap = v;
127 struct file *fp;
128 register struct filedesc *fdp = p->p_fd;
129 register int old = SCARG(uap, fd);
130 int new;
131 int error;
132
133 if ((u_int)old >= fdp->fd_nfiles ||
134 (fp = fdp->fd_ofiles[old]) == NULL ||
135 (fp->f_iflags & FIF_WANTCLOSE) != 0)
136 return (EBADF);
137
138 FILE_USE(fp);
139
140 if ((error = fdalloc(p, 0, &new)) != 0) {
141 FILE_UNUSE(fp, p);
142 return (error);
143 }
144
145 /* finishdup() will unuse the descriptors for us */
146 return (finishdup(p, old, new, retval));
147 }
148
149 /*
150 * Duplicate a file descriptor to a particular value.
151 */
152 /* ARGSUSED */
153 int
154 sys_dup2(p, v, retval)
155 struct proc *p;
156 void *v;
157 register_t *retval;
158 {
159 struct sys_dup2_args /* {
160 syscallarg(int) from;
161 syscallarg(int) to;
162 } */ *uap = v;
163 struct file *fp;
164 register struct filedesc *fdp = p->p_fd;
165 register int old = SCARG(uap, from), new = SCARG(uap, to);
166 int i, error;
167
168 if ((u_int)old >= fdp->fd_nfiles ||
169 (fp = fdp->fd_ofiles[old]) == NULL ||
170 (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
171 (u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
172 (u_int)new >= maxfiles)
173 return (EBADF);
174 if (old == new) {
175 *retval = new;
176 return (0);
177 }
178
179 FILE_USE(fp);
180
181 if (new >= fdp->fd_nfiles) {
182 if ((error = fdalloc(p, new, &i)) != 0) {
183 FILE_UNUSE(fp, p);
184 return (error);
185 }
186 if (new != i)
187 panic("dup2: fdalloc");
188 } else {
189 (void) fdrelease(p, new);
190 }
191
192 /* finishdup() will unuse the descriptors for us */
193 return (finishdup(p, old, new, retval));
194 }
195
196 int fcntl_forfs __P((int, struct proc *, int, void *));
197
198 /*
199 * The file control system call.
200 */
201 /* ARGSUSED */
202 int
203 sys_fcntl(p, v, retval)
204 struct proc *p;
205 void *v;
206 register_t *retval;
207 {
208 register struct sys_fcntl_args /* {
209 syscallarg(int) fd;
210 syscallarg(int) cmd;
211 syscallarg(void *) arg;
212 } */ *uap = v;
213 int fd = SCARG(uap, fd);
214 register struct filedesc *fdp = p->p_fd;
215 register struct file *fp;
216 struct vnode *vp;
217 int i, tmp, error = 0, flg = F_POSIX, cmd;
218 struct flock fl;
219 int newmin;
220
221 if ((u_int)fd >= fdp->fd_nfiles ||
222 (fp = fdp->fd_ofiles[fd]) == NULL ||
223 (fp->f_iflags & FIF_WANTCLOSE) != 0)
224 return (EBADF);
225
226 FILE_USE(fp);
227
228 cmd = SCARG(uap, cmd);
229 if ((cmd & F_FSCTL)) {
230 error = fcntl_forfs(fd, p, cmd, SCARG(uap, arg));
231 goto out;
232 }
233
234 switch (cmd) {
235
236 case F_DUPFD:
237 newmin = (long)SCARG(uap, arg);
238 if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
239 (u_int)newmin >= maxfiles) {
240 error = EINVAL;
241 goto out;
242 }
243 if ((error = fdalloc(p, newmin, &i)) != 0)
244 goto out;
245
246 /* finishdup() will unuse the descriptors for us */
247 return (finishdup(p, fd, i, retval));
248
249 case F_GETFD:
250 *retval = fdp->fd_ofileflags[fd] & UF_EXCLOSE ? 1 : 0;
251 break;
252
253 case F_SETFD:
254 if ((long)SCARG(uap, arg) & 1)
255 fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
256 else
257 fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
258 break;
259
260 case F_GETFL:
261 *retval = OFLAGS(fp->f_flag);
262 break;
263
264 case F_SETFL:
265 tmp = FFLAGS((long)SCARG(uap, arg)) & FCNTLFLAGS;
266 error = (*fp->f_ops->fo_fcntl)(fp, F_SETFL, (caddr_t)&tmp, p);
267 if (error)
268 goto out;
269 fp->f_flag &= ~FCNTLFLAGS;
270 fp->f_flag |= tmp;
271 tmp = fp->f_flag & FNONBLOCK;
272 error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
273 if (error)
274 goto out;
275 tmp = fp->f_flag & FASYNC;
276 error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
277 if (error == 0)
278 goto out;
279 fp->f_flag &= ~FNONBLOCK;
280 tmp = 0;
281 (void) (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
282 break;
283
284 case F_GETOWN:
285 if (fp->f_type == DTYPE_SOCKET) {
286 *retval = ((struct socket *)fp->f_data)->so_pgid;
287 goto out;
288 }
289 error = (*fp->f_ops->fo_ioctl)
290 (fp, TIOCGPGRP, (caddr_t)retval, p);
291 *retval = -*retval;
292 break;
293
294 case F_SETOWN:
295 if (fp->f_type == DTYPE_SOCKET) {
296 ((struct socket *)fp->f_data)->so_pgid =
297 (long)SCARG(uap, arg);
298 goto out;
299 }
300 if ((long)SCARG(uap, arg) <= 0) {
301 SCARG(uap, arg) = (void *)(-(long)SCARG(uap, arg));
302 } else {
303 struct proc *p1 = pfind((long)SCARG(uap, arg));
304 if (p1 == 0) {
305 error = ESRCH;
306 goto out;
307 }
308 SCARG(uap, arg) = (void *)(long)p1->p_pgrp->pg_id;
309 }
310 error = (*fp->f_ops->fo_ioctl)
311 (fp, TIOCSPGRP, (caddr_t)&SCARG(uap, arg), p);
312 break;
313
314 case F_SETLKW:
315 flg |= F_WAIT;
316 /* Fall into F_SETLK */
317
318 case F_SETLK:
319 if (fp->f_type != DTYPE_VNODE) {
320 error = EINVAL;
321 goto out;
322 }
323 vp = (struct vnode *)fp->f_data;
324 /* Copy in the lock structure */
325 error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&fl,
326 sizeof(fl));
327 if (error)
328 goto out;
329 if (fl.l_whence == SEEK_CUR)
330 fl.l_start += fp->f_offset;
331 switch (fl.l_type) {
332 case F_RDLCK:
333 if ((fp->f_flag & FREAD) == 0) {
334 error = EBADF;
335 goto out;
336 }
337 p->p_flag |= P_ADVLOCK;
338 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg);
339 goto out;
340
341 case F_WRLCK:
342 if ((fp->f_flag & FWRITE) == 0) {
343 error = EBADF;
344 goto out;
345 }
346 p->p_flag |= P_ADVLOCK;
347 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg);
348 goto out;
349
350 case F_UNLCK:
351 error = VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl,
352 F_POSIX);
353 goto out;
354
355 default:
356 error = EINVAL;
357 goto out;
358 }
359
360 case F_GETLK:
361 if (fp->f_type != DTYPE_VNODE) {
362 error = EINVAL;
363 goto out;
364 }
365 vp = (struct vnode *)fp->f_data;
366 /* Copy in the lock structure */
367 error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&fl,
368 sizeof(fl));
369 if (error)
370 goto out;
371 if (fl.l_whence == SEEK_CUR)
372 fl.l_start += fp->f_offset;
373 if (fl.l_type != F_RDLCK &&
374 fl.l_type != F_WRLCK &&
375 fl.l_type != F_UNLCK) {
376 error = EINVAL;
377 goto out;
378 }
379 error = VOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX);
380 if (error)
381 goto out;
382 error = copyout((caddr_t)&fl, (caddr_t)SCARG(uap, arg),
383 sizeof(fl));
384 break;
385
386 default:
387 error = EINVAL;
388 }
389
390 out:
391 FILE_UNUSE(fp, p);
392 return (error);
393 }
394
395 /*
396 * Common code for dup, dup2, and fcntl(F_DUPFD).
397 */
398 int
399 finishdup(p, old, new, retval)
400 struct proc *p;
401 int old, new;
402 register_t *retval;
403 {
404 struct filedesc *fdp = p->p_fd;
405 struct file *fp;
406
407 /*
408 * Note: `old' is already used for us.
409 */
410
411 fp = fdp->fd_ofiles[old];
412 fdp->fd_ofiles[new] = fp;
413 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
414 fp->f_count++;
415 fd_used(fdp, new);
416 *retval = new;
417 FILE_UNUSE(fp, p);
418 return (0);
419 }
420
421 int
422 fdrelease(p, fd)
423 struct proc *p;
424 int fd;
425 {
426 register struct filedesc *fdp = p->p_fd;
427 register struct file **fpp, *fp;
428 register char *pf;
429
430 fpp = &fdp->fd_ofiles[fd];
431 fp = *fpp;
432 if (fp == NULL)
433 return (EBADF);
434
435 FILE_USE(fp);
436
437 pf = &fdp->fd_ofileflags[fd];
438 if (*pf & UF_MAPPED) {
439 /* XXX: USELESS? XXXCDC check it */
440 p->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED;
441 }
442 *fpp = NULL;
443 *pf = 0;
444 fd_unused(fdp, fd);
445 return (closef(fp, p));
446 }
447
448 /*
449 * Close a file descriptor.
450 */
451 /* ARGSUSED */
452 int
453 sys_close(p, v, retval)
454 struct proc *p;
455 void *v;
456 register_t *retval;
457 {
458 struct sys_close_args /* {
459 syscallarg(int) fd;
460 } */ *uap = v;
461 int fd = SCARG(uap, fd);
462 register struct filedesc *fdp = p->p_fd;
463
464 if ((u_int)fd >= fdp->fd_nfiles)
465 return (EBADF);
466 return (fdrelease(p, fd));
467 }
468
469 /*
470 * Return status information about a file descriptor.
471 */
472 /* ARGSUSED */
473 int
474 sys___fstat13(p, v, retval)
475 struct proc *p;
476 void *v;
477 register_t *retval;
478 {
479 register struct sys___fstat13_args /* {
480 syscallarg(int) fd;
481 syscallarg(struct stat *) sb;
482 } */ *uap = v;
483 int fd = SCARG(uap, fd);
484 register struct filedesc *fdp = p->p_fd;
485 register struct file *fp;
486 struct stat ub;
487 int error;
488
489 if ((u_int)fd >= fdp->fd_nfiles ||
490 (fp = fdp->fd_ofiles[fd]) == NULL ||
491 (fp->f_iflags & FIF_WANTCLOSE) != 0)
492 return (EBADF);
493
494 FILE_USE(fp);
495
496 switch (fp->f_type) {
497
498 case DTYPE_VNODE:
499 error = vn_stat((struct vnode *)fp->f_data, &ub, p);
500 break;
501
502 case DTYPE_SOCKET:
503 error = soo_stat((struct socket *)fp->f_data, &ub);
504 break;
505
506 default:
507 panic("fstat");
508 /*NOTREACHED*/
509 }
510 if (error == 0)
511 error = copyout(&ub, SCARG(uap, sb), sizeof(ub));
512 FILE_UNUSE(fp, p);
513 return (error);
514 }
515
516 /*
517 * Return pathconf information about a file descriptor.
518 */
519 /* ARGSUSED */
520 int
521 sys_fpathconf(p, v, retval)
522 struct proc *p;
523 void *v;
524 register_t *retval;
525 {
526 register struct sys_fpathconf_args /* {
527 syscallarg(int) fd;
528 syscallarg(int) name;
529 } */ *uap = v;
530 int fd = SCARG(uap, fd);
531 struct filedesc *fdp = p->p_fd;
532 struct file *fp;
533 struct vnode *vp;
534 int error = 0;
535
536 if ((u_int)fd >= fdp->fd_nfiles ||
537 (fp = fdp->fd_ofiles[fd]) == NULL ||
538 (fp->f_iflags & FIF_WANTCLOSE) != 0)
539 return (EBADF);
540
541 FILE_USE(fp);
542
543 switch (fp->f_type) {
544
545 case DTYPE_SOCKET:
546 if (SCARG(uap, name) != _PC_PIPE_BUF)
547 error = EINVAL;
548 else
549 *retval = PIPE_BUF;
550 break;
551
552 case DTYPE_VNODE:
553 vp = (struct vnode *)fp->f_data;
554 error = VOP_PATHCONF(vp, SCARG(uap, name), retval);
555 break;
556
557 default:
558 panic("fpathconf");
559 }
560
561 FILE_UNUSE(fp, p);
562 return (error);
563 }
564
565 /*
566 * Allocate a file descriptor for the process.
567 */
568 int fdexpand;
569
570 int
571 fdalloc(p, want, result)
572 struct proc *p;
573 int want;
574 int *result;
575 {
576 register struct filedesc *fdp = p->p_fd;
577 register int i;
578 int lim, last, nfiles;
579 struct file **newofile;
580 char *newofileflags;
581
582 /*
583 * Search for a free descriptor starting at the higher
584 * of want or fd_freefile. If that fails, consider
585 * expanding the ofile array.
586 */
587 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
588 for (;;) {
589 last = min(fdp->fd_nfiles, lim);
590 if ((i = want) < fdp->fd_freefile)
591 i = fdp->fd_freefile;
592 for (; i < last; i++) {
593 if (fdp->fd_ofiles[i] == NULL) {
594 fd_used(fdp, i);
595 if (want <= fdp->fd_freefile)
596 fdp->fd_freefile = i;
597 *result = i;
598 return (0);
599 }
600 }
601
602 /*
603 * No space in current array. Expand?
604 */
605 if (fdp->fd_nfiles >= lim)
606 return (EMFILE);
607 if (fdp->fd_nfiles < NDEXTENT)
608 nfiles = NDEXTENT;
609 else
610 nfiles = 2 * fdp->fd_nfiles;
611 newofile = malloc(nfiles * OFILESIZE, M_FILEDESC, M_WAITOK);
612 newofileflags = (char *) &newofile[nfiles];
613 /*
614 * Copy the existing ofile and ofileflags arrays
615 * and zero the new portion of each array.
616 */
617 memcpy(newofile, fdp->fd_ofiles,
618 (i = sizeof(struct file *) * fdp->fd_nfiles));
619 memset((char *)newofile + i, 0, nfiles * sizeof(struct file *) - i);
620 memcpy(newofileflags, fdp->fd_ofileflags,
621 (i = sizeof(char) * fdp->fd_nfiles));
622 memset(newofileflags + i, 0, nfiles * sizeof(char) - i);
623 if (fdp->fd_nfiles > NDFILE)
624 free(fdp->fd_ofiles, M_FILEDESC);
625 fdp->fd_ofiles = newofile;
626 fdp->fd_ofileflags = newofileflags;
627 fdp->fd_nfiles = nfiles;
628 fdexpand++;
629 }
630 }
631
632 /*
633 * Check to see whether n user file descriptors
634 * are available to the process p.
635 */
636 int
637 fdavail(p, n)
638 struct proc *p;
639 register int n;
640 {
641 register struct filedesc *fdp = p->p_fd;
642 register struct file **fpp;
643 register int i, lim;
644
645 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
646 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
647 return (1);
648 fpp = &fdp->fd_ofiles[fdp->fd_freefile];
649 for (i = min(lim,fdp->fd_nfiles) - fdp->fd_freefile; --i >= 0; fpp++)
650 if (*fpp == NULL && --n <= 0)
651 return (1);
652 return (0);
653 }
654
655 /*
656 * Initialize the data structures necessary for managing files.
657 */
658 void
659 finit()
660 {
661
662 pool_init(&file_pool, sizeof(struct file), 0, 0, 0, "filepl",
663 0, pool_page_alloc_nointr, pool_page_free_nointr, M_FILE);
664 pool_init(&cwdi_pool, sizeof(struct cwdinfo), 0, 0, 0, "cwdipl",
665 0, pool_page_alloc_nointr, pool_page_free_nointr, M_FILEDESC);
666 pool_init(&filedesc0_pool, sizeof(struct filedesc0), 0, 0, 0, "fdescpl",
667 0, pool_page_alloc_nointr, pool_page_free_nointr, M_FILEDESC);
668 }
669
670 /*
671 * Create a new open file structure and allocate
672 * a file decriptor for the process that refers to it.
673 */
674 int
675 falloc(p, resultfp, resultfd)
676 register struct proc *p;
677 struct file **resultfp;
678 int *resultfd;
679 {
680 register struct file *fp, *fq;
681 int error, i;
682
683 if ((error = fdalloc(p, 0, &i)) != 0)
684 return (error);
685 if (nfiles >= maxfiles) {
686 tablefull("file");
687 return (ENFILE);
688 }
689 /*
690 * Allocate a new file descriptor.
691 * If the process has file descriptor zero open, add to the list
692 * of open files at that point, otherwise put it at the front of
693 * the list of open files.
694 */
695 nfiles++;
696 fp = pool_get(&file_pool, PR_WAITOK);
697 memset(fp, 0, sizeof(struct file));
698 if ((fq = p->p_fd->fd_ofiles[0]) != NULL) {
699 LIST_INSERT_AFTER(fq, fp, f_list);
700 } else {
701 LIST_INSERT_HEAD(&filehead, fp, f_list);
702 }
703 p->p_fd->fd_ofiles[i] = fp;
704 fp->f_count = 1;
705 fp->f_cred = p->p_ucred;
706 crhold(fp->f_cred);
707 if (resultfp) {
708 FILE_USE(fp);
709 *resultfp = fp;
710 }
711 if (resultfd)
712 *resultfd = i;
713 return (0);
714 }
715
716 /*
717 * Free a file descriptor.
718 */
719 void
720 ffree(fp)
721 register struct file *fp;
722 {
723
724 #ifdef DIAGNOSTIC
725 if (fp->f_usecount)
726 panic("ffree");
727 #endif
728
729 LIST_REMOVE(fp, f_list);
730 crfree(fp->f_cred);
731 #ifdef DIAGNOSTIC
732 fp->f_count = 0;
733 #endif
734 nfiles--;
735 pool_put(&file_pool, fp);
736 }
737
738 /*
739 * Create an initial cwdinfo structure, using the same current and root
740 * directories as p.
741 */
742 struct cwdinfo *
743 cwdinit(p)
744 struct proc *p;
745 {
746 struct cwdinfo *cwdi;
747
748 cwdi = pool_get(&cwdi_pool, PR_WAITOK);
749
750 cwdi->cwdi_cdir = p->p_cwdi->cwdi_cdir;
751 if (cwdi->cwdi_cdir)
752 VREF(cwdi->cwdi_cdir);
753 cwdi->cwdi_rdir = p->p_cwdi->cwdi_rdir;
754 if (cwdi->cwdi_rdir)
755 VREF(cwdi->cwdi_rdir);
756 cwdi->cwdi_cmask = p->p_cwdi->cwdi_cmask;
757 cwdi->cwdi_refcnt = 1;
758
759 return (cwdi);
760 }
761
762 /*
763 * Make p2 share p1's cwdinfo.
764 */
765 void
766 cwdshare(p1, p2)
767 struct proc *p1, *p2;
768 {
769
770 p2->p_cwdi = p1->p_cwdi;
771 p1->p_cwdi->cwdi_refcnt++;
772 }
773
774 /*
775 * Make this process not share its cwdinfo structure, maintaining
776 * all cwdinfo state.
777 */
778 void
779 cwdunshare(p)
780 struct proc *p;
781 {
782 struct cwdinfo *newcwdi;
783
784 if (p->p_cwdi->cwdi_refcnt == 1)
785 return;
786
787 newcwdi = cwdinit(p);
788 cwdfree(p);
789 p->p_cwdi = newcwdi;
790 }
791
792 /*
793 * Release a cwdinfo structure.
794 */
795 void
796 cwdfree(p)
797 struct proc *p;
798 {
799 struct cwdinfo *cwdi = p->p_cwdi;
800
801 if (--cwdi->cwdi_refcnt > 0)
802 return;
803
804 p->p_cwdi = NULL;
805
806 vrele(cwdi->cwdi_cdir);
807 if (cwdi->cwdi_rdir)
808 vrele(cwdi->cwdi_rdir);
809 pool_put(&cwdi_pool, cwdi);
810 }
811
812 /*
813 * Create an initial filedesc structure, using the same current and root
814 * directories as p.
815 */
816 struct filedesc *
817 fdinit(p)
818 struct proc *p;
819 {
820 struct filedesc0 *newfdp;
821
822 newfdp = pool_get(&filedesc0_pool, PR_WAITOK);
823 memset(newfdp, 0, sizeof(struct filedesc0));
824
825 fdinit1(newfdp);
826
827 return (&newfdp->fd_fd);
828 }
829
830 /*
831 * Initialize a file descriptor table.
832 */
833 void
834 fdinit1(newfdp)
835 struct filedesc0 *newfdp;
836 {
837
838 newfdp->fd_fd.fd_refcnt = 1;
839 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
840 newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
841 newfdp->fd_fd.fd_nfiles = NDFILE;
842 }
843
844 /*
845 * Make p2 share p1's filedesc structure.
846 */
847 void
848 fdshare(p1, p2)
849 struct proc *p1, *p2;
850 {
851
852 p2->p_fd = p1->p_fd;
853 p1->p_fd->fd_refcnt++;
854 }
855
856 /*
857 * Make this process not share its filedesc structure, maintaining
858 * all file descriptor state.
859 */
860 void
861 fdunshare(p)
862 struct proc *p;
863 {
864 struct filedesc *newfd;
865
866 if (p->p_fd->fd_refcnt == 1)
867 return;
868
869 newfd = fdcopy(p);
870 fdfree(p);
871 p->p_fd = newfd;
872 }
873
874 /*
875 * Clear a process's fd table.
876 */
877 void
878 fdclear(p)
879 struct proc *p;
880 {
881 struct filedesc *newfd;
882
883 newfd = fdinit(p);
884 fdfree(p);
885 p->p_fd = newfd;
886 }
887
888 /*
889 * Copy a filedesc structure.
890 */
891 struct filedesc *
892 fdcopy(p)
893 struct proc *p;
894 {
895 register struct filedesc *newfdp, *fdp = p->p_fd;
896 register struct file **fpp;
897 register int i;
898
899 newfdp = pool_get(&filedesc0_pool, PR_WAITOK);
900 memcpy(newfdp, fdp, sizeof(struct filedesc));
901 newfdp->fd_refcnt = 1;
902
903 /*
904 * If the number of open files fits in the internal arrays
905 * of the open file structure, use them, otherwise allocate
906 * additional memory for the number of descriptors currently
907 * in use.
908 */
909 if (newfdp->fd_lastfile < NDFILE) {
910 newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
911 newfdp->fd_ofileflags =
912 ((struct filedesc0 *) newfdp)->fd_dfileflags;
913 i = NDFILE;
914 } else {
915 /*
916 * Compute the smallest multiple of NDEXTENT needed
917 * for the file descriptors currently in use,
918 * allowing the table to shrink.
919 */
920 i = newfdp->fd_nfiles;
921 while (i >= 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
922 i /= 2;
923 newfdp->fd_ofiles = malloc(i * OFILESIZE, M_FILEDESC, M_WAITOK);
924 newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
925 }
926 newfdp->fd_nfiles = i;
927 memcpy(newfdp->fd_ofiles, fdp->fd_ofiles, i * sizeof(struct file **));
928 memcpy(newfdp->fd_ofileflags, fdp->fd_ofileflags, i * sizeof(char));
929 fpp = newfdp->fd_ofiles;
930 for (i = newfdp->fd_lastfile; i >= 0; i--, fpp++)
931 if (*fpp != NULL)
932 (*fpp)->f_count++;
933 return (newfdp);
934 }
935
936 /*
937 * Release a filedesc structure.
938 */
939 void
940 fdfree(p)
941 struct proc *p;
942 {
943 register struct filedesc *fdp = p->p_fd;
944 register struct file **fpp, *fp;
945 register int i;
946
947 if (--fdp->fd_refcnt > 0)
948 return;
949 fpp = fdp->fd_ofiles;
950 for (i = fdp->fd_lastfile; i >= 0; i--, fpp++) {
951 fp = *fpp;
952 if (fp != NULL) {
953 *fpp = NULL;
954 FILE_USE(fp);
955 (void) closef(fp, p);
956 }
957 }
958 p->p_fd = NULL;
959 if (fdp->fd_nfiles > NDFILE)
960 free(fdp->fd_ofiles, M_FILEDESC);
961 pool_put(&filedesc0_pool, fdp);
962 }
963
964 /*
965 * Internal form of close.
966 * Decrement reference count on file structure.
967 * Note: p may be NULL when closing a file
968 * that was being passed in a message.
969 *
970 * Note: we expect the caller is holding a usecount, and expects us
971 * to drop it (the caller thinks the file is going away forever).
972 */
973 int
974 closef(fp, p)
975 register struct file *fp;
976 register struct proc *p;
977 {
978 struct vnode *vp;
979 struct flock lf;
980 int error;
981
982 if (fp == NULL)
983 return (0);
984
985 /*
986 * POSIX record locking dictates that any close releases ALL
987 * locks owned by this process. This is handled by setting
988 * a flag in the unlock to free ONLY locks obeying POSIX
989 * semantics, and not to free BSD-style file locks.
990 * If the descriptor was in a message, POSIX-style locks
991 * aren't passed with the descriptor.
992 */
993 if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
994 lf.l_whence = SEEK_SET;
995 lf.l_start = 0;
996 lf.l_len = 0;
997 lf.l_type = F_UNLCK;
998 vp = (struct vnode *)fp->f_data;
999 (void) VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX);
1000 }
1001
1002 /*
1003 * If WANTCLOSE is set, then the reference count on the file
1004 * is 0, but there were multiple users of the file. This can
1005 * happen if a filedesc structure is shared by multiple
1006 * processes.
1007 */
1008 if (fp->f_iflags & FIF_WANTCLOSE) {
1009 /*
1010 * Another user of the file is already closing, and is
1011 * simply waiting for other users of the file to drain.
1012 * Release our usecount, and wake up the closer if it
1013 * is the only remaining use.
1014 */
1015 #ifdef DIAGNOSTIC
1016 if (fp->f_count != 0)
1017 panic("closef: wantclose and count != 0");
1018 if (fp->f_usecount < 2)
1019 panic("closef: wantclose and usecount < 2");
1020 #endif
1021 if (--fp->f_usecount == 1)
1022 wakeup(&fp->f_usecount);
1023 return (0);
1024 } else {
1025 /*
1026 * Decrement the reference count. If we were not the
1027 * last reference, then release our use and just
1028 * return.
1029 */
1030 if (--fp->f_count > 0) {
1031 #ifdef DIAGNOSTIC
1032 if (fp->f_usecount < 1)
1033 panic("closef: no wantclose and usecount < 1");
1034 #endif
1035 fp->f_usecount--;
1036 return (0);
1037 }
1038 if (fp->f_count < 0)
1039 panic("closef: count < 0");
1040 }
1041
1042 /*
1043 * The reference count is now 0. However, there may be
1044 * multiple potential users of this file. This can happen
1045 * if multiple processes shared a single filedesc structure.
1046 *
1047 * Notify these potential users that the file is closing.
1048 * This will prevent them from adding additional uses to
1049 * the file.
1050 */
1051 fp->f_iflags |= FIF_WANTCLOSE;
1052
1053 /*
1054 * We expect the caller to add a use to the file. So, if we
1055 * are the last user, usecount will be 1. If it is not, we
1056 * must wait for the usecount to drain. When it drains back
1057 * to 1, we will be awakened so that we may proceed with the
1058 * close.
1059 */
1060 #ifdef DIAGNOSTIC
1061 if (fp->f_usecount < 1)
1062 panic("closef: usecount < 1");
1063 #endif
1064 while (fp->f_usecount > 1)
1065 (void) tsleep(&fp->f_usecount, PRIBIO, "closef", 0);
1066 #ifdef DIAGNOSTIC
1067 if (fp->f_usecount != 1)
1068 panic("closef: usecount != 1");
1069 #endif
1070
1071 if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
1072 lf.l_whence = SEEK_SET;
1073 lf.l_start = 0;
1074 lf.l_len = 0;
1075 lf.l_type = F_UNLCK;
1076 vp = (struct vnode *)fp->f_data;
1077 (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1078 }
1079 if (fp->f_ops)
1080 error = (*fp->f_ops->fo_close)(fp, p);
1081 else
1082 error = 0;
1083
1084 /* Nothing references the file now, drop the final use (us). */
1085 fp->f_usecount--;
1086
1087 ffree(fp);
1088 return (error);
1089 }
1090
1091 /*
1092 * Apply an advisory lock on a file descriptor.
1093 *
1094 * Just attempt to get a record lock of the requested type on
1095 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1096 */
1097 /* ARGSUSED */
1098 int
1099 sys_flock(p, v, retval)
1100 struct proc *p;
1101 void *v;
1102 register_t *retval;
1103 {
1104 register struct sys_flock_args /* {
1105 syscallarg(int) fd;
1106 syscallarg(int) how;
1107 } */ *uap = v;
1108 int fd = SCARG(uap, fd);
1109 int how = SCARG(uap, how);
1110 register struct filedesc *fdp = p->p_fd;
1111 register struct file *fp;
1112 struct vnode *vp;
1113 struct flock lf;
1114 int error = 0;
1115
1116 if ((u_int)fd >= fdp->fd_nfiles ||
1117 (fp = fdp->fd_ofiles[fd]) == NULL ||
1118 (fp->f_iflags & FIF_WANTCLOSE) != 0)
1119 return (EBADF);
1120
1121 FILE_USE(fp);
1122
1123 if (fp->f_type != DTYPE_VNODE) {
1124 error = EOPNOTSUPP;
1125 goto out;
1126 }
1127
1128 vp = (struct vnode *)fp->f_data;
1129 lf.l_whence = SEEK_SET;
1130 lf.l_start = 0;
1131 lf.l_len = 0;
1132 if (how & LOCK_UN) {
1133 lf.l_type = F_UNLCK;
1134 fp->f_flag &= ~FHASLOCK;
1135 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1136 goto out;
1137 }
1138 if (how & LOCK_EX)
1139 lf.l_type = F_WRLCK;
1140 else if (how & LOCK_SH)
1141 lf.l_type = F_RDLCK;
1142 else {
1143 error = EINVAL;
1144 goto out;
1145 }
1146 fp->f_flag |= FHASLOCK;
1147 if (how & LOCK_NB)
1148 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK);
1149 else
1150 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1151 F_FLOCK|F_WAIT);
1152 out:
1153 FILE_UNUSE(fp, p);
1154 return (error);
1155 }
1156
1157 /*
1158 * File Descriptor pseudo-device driver (/dev/fd/).
1159 *
1160 * Opening minor device N dup()s the file (if any) connected to file
1161 * descriptor N belonging to the calling process. Note that this driver
1162 * consists of only the ``open()'' routine, because all subsequent
1163 * references to this file will be direct to the other driver.
1164 */
1165 /* ARGSUSED */
1166 int
1167 filedescopen(dev, mode, type, p)
1168 dev_t dev;
1169 int mode, type;
1170 struct proc *p;
1171 {
1172
1173 /*
1174 * XXX Kludge: set curproc->p_dupfd to contain the value of the
1175 * the file descriptor being sought for duplication. The error
1176 * return ensures that the vnode for this device will be released
1177 * by vn_open. Open will detect this special error and take the
1178 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1179 * will simply report the error.
1180 */
1181 p->p_dupfd = minor(dev);
1182 return (ENODEV);
1183 }
1184
1185 /*
1186 * Duplicate the specified descriptor to a free descriptor.
1187 */
1188 int
1189 dupfdopen(p, indx, dfd, mode, error)
1190 struct proc *p;
1191 int indx, dfd, mode, error;
1192 {
1193 struct filedesc *fdp = p->p_fd;
1194 struct file *wfp;
1195 struct file *fp;
1196
1197 /*
1198 * If the to-be-dup'd fd number is greater than the allowed number
1199 * of file descriptors, or the fd to be dup'd has already been
1200 * closed, reject. Note, check for new == old is necessary as
1201 * falloc could allocate an already closed to-be-dup'd descriptor
1202 * as the new descriptor.
1203 */
1204 fp = fdp->fd_ofiles[indx];
1205 if ((u_int)dfd >= fdp->fd_nfiles ||
1206 (wfp = fdp->fd_ofiles[dfd]) == NULL ||
1207 (wfp->f_iflags & FIF_WANTCLOSE) != 0 ||
1208 fp == wfp)
1209 return (EBADF);
1210
1211 FILE_USE(wfp);
1212
1213 /*
1214 * There are two cases of interest here.
1215 *
1216 * For ENODEV simply dup (dfd) to file descriptor
1217 * (indx) and return.
1218 *
1219 * For ENXIO steal away the file structure from (dfd) and
1220 * store it in (indx). (dfd) is effectively closed by
1221 * this operation.
1222 *
1223 * Any other error code is just returned.
1224 */
1225 switch (error) {
1226 case ENODEV:
1227 /*
1228 * Check that the mode the file is being opened for is a
1229 * subset of the mode of the existing descriptor.
1230 */
1231 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
1232 FILE_UNUSE(wfp, p);
1233 return (EACCES);
1234 }
1235 fdp->fd_ofiles[indx] = wfp;
1236 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1237 wfp->f_count++;
1238 fd_used(fdp, indx);
1239 FILE_UNUSE(wfp, p);
1240 return (0);
1241
1242 case ENXIO:
1243 /*
1244 * Steal away the file pointer from dfd, and stuff it into indx.
1245 */
1246 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
1247 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1248 fdp->fd_ofiles[dfd] = NULL;
1249 fdp->fd_ofileflags[dfd] = 0;
1250 /*
1251 * Complete the clean up of the filedesc structure by
1252 * recomputing the various hints.
1253 */
1254 fd_used(fdp, indx);
1255 fd_unused(fdp, dfd);
1256 FILE_UNUSE(wfp, p);
1257 return (0);
1258
1259 default:
1260 FILE_UNUSE(wfp, p);
1261 return (error);
1262 }
1263 /* NOTREACHED */
1264 }
1265
1266 /*
1267 * fcntl call which is being passed to the file's fs.
1268 */
1269 int
1270 fcntl_forfs(fd, p, cmd, arg)
1271 int fd, cmd;
1272 struct proc *p;
1273 void *arg;
1274 {
1275 register struct file *fp;
1276 register struct filedesc *fdp;
1277 register int error;
1278 register u_int size;
1279 caddr_t data, memp;
1280 #define STK_PARAMS 128
1281 char stkbuf[STK_PARAMS];
1282
1283 /* fd's value was validated in sys_fcntl before calling this routine */
1284 fdp = p->p_fd;
1285 fp = fdp->fd_ofiles[fd];
1286
1287 if ((fp->f_flag & (FREAD | FWRITE)) == 0)
1288 return (EBADF);
1289
1290 /*
1291 * Interpret high order word to find amount of data to be
1292 * copied to/from the user's address space.
1293 */
1294 size = (size_t)F_PARAM_LEN(cmd);
1295 if (size > F_PARAM_MAX)
1296 return (EINVAL);
1297 memp = NULL;
1298 if (size > sizeof(stkbuf)) {
1299 memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
1300 data = memp;
1301 } else
1302 data = stkbuf;
1303 if (cmd & F_FSIN) {
1304 if (size) {
1305 error = copyin(arg, data, size);
1306 if (error) {
1307 if (memp)
1308 free(memp, M_IOCTLOPS);
1309 return (error);
1310 }
1311 } else
1312 *(caddr_t *)data = arg;
1313 } else if ((cmd & F_FSOUT) && size)
1314 /*
1315 * Zero the buffer so the user always
1316 * gets back something deterministic.
1317 */
1318 memset(data, 0, size);
1319 else if (cmd & F_FSVOID)
1320 *(caddr_t *)data = arg;
1321
1322
1323 error = (*fp->f_ops->fo_fcntl)(fp, cmd, data, p);
1324
1325 /*
1326 * Copy any data to user, size was
1327 * already set and checked above.
1328 */
1329 if (error == 0 && (cmd & F_FSOUT) && size)
1330 error = copyout(data, arg, size);
1331 if (memp)
1332 free(memp, M_IOCTLOPS);
1333 return (error);
1334 }
1335
1336 /*
1337 * Close any files on exec?
1338 */
1339 void
1340 fdcloseexec(p)
1341 struct proc *p;
1342 {
1343 register struct filedesc *fdp = p->p_fd;
1344 register int fd;
1345
1346 for (fd = 0; fd <= fdp->fd_lastfile; fd++)
1347 if (fdp->fd_ofileflags[fd] & UF_EXCLOSE)
1348 (void) fdrelease(p, fd);
1349 }
1350