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