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