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