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