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