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