kern_descrip.c revision 1.149 1 /* $NetBSD: kern_descrip.c,v 1.149 2007/01/31 16:00:43 ad 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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.149 2007/01/31 16:00:43 ad Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/filedesc.h>
45 #include <sys/kernel.h>
46 #include <sys/vnode.h>
47 #include <sys/proc.h>
48 #include <sys/file.h>
49 #include <sys/namei.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/stat.h>
53 #include <sys/ioctl.h>
54 #include <sys/fcntl.h>
55 #include <sys/malloc.h>
56 #include <sys/pool.h>
57 #include <sys/syslog.h>
58 #include <sys/unistd.h>
59 #include <sys/resourcevar.h>
60 #include <sys/conf.h>
61 #include <sys/event.h>
62 #include <sys/kauth.h>
63
64 #include <sys/mount.h>
65 #include <sys/sa.h>
66 #include <sys/syscallargs.h>
67
68 /*
69 * Descriptor management.
70 */
71 struct filelist filehead; /* head of list of open files */
72 int nfiles; /* actual number of open files */
73 POOL_INIT(file_pool, sizeof(struct file), 0, 0, 0, "filepl",
74 &pool_allocator_nointr);
75 POOL_INIT(cwdi_pool, sizeof(struct cwdinfo), 0, 0, 0, "cwdipl",
76 &pool_allocator_nointr);
77 POOL_INIT(filedesc0_pool, sizeof(struct filedesc0), 0, 0, 0, "fdescpl",
78 &pool_allocator_nointr);
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 int
88 find_next_zero(uint32_t *bitmap, int want, u_int bits)
89 {
90 int i, off, maxoff;
91 uint32_t sub;
92
93 if (want > bits)
94 return -1;
95
96 off = want >> NDENTRYSHIFT;
97 i = want & NDENTRYMASK;
98 if (i) {
99 sub = bitmap[off] | ((u_int)~0 >> (NDENTRIES - i));
100 if (sub != ~0)
101 goto found;
102 off++;
103 }
104
105 maxoff = NDLOSLOTS(bits);
106 while (off < maxoff) {
107 if ((sub = bitmap[off]) != ~0)
108 goto found;
109 off++;
110 }
111
112 return (-1);
113
114 found:
115 return (off << NDENTRYSHIFT) + ffs(~sub) - 1;
116 }
117
118 static int
119 find_last_set(struct filedesc *fd, int last)
120 {
121 int off, i;
122 struct file **ofiles = fd->fd_ofiles;
123 uint32_t *bitmap = fd->fd_lomap;
124
125 off = (last - 1) >> NDENTRYSHIFT;
126
127 while (off >= 0 && !bitmap[off])
128 off--;
129
130 if (off < 0)
131 return (-1);
132
133 i = ((off + 1) << NDENTRYSHIFT) - 1;
134 if (i >= last)
135 i = last - 1;
136
137 while (i > 0 && ofiles[i] == NULL)
138 i--;
139
140 return (i);
141 }
142
143 static inline void
144 fd_used(struct filedesc *fdp, int fd)
145 {
146 u_int off = fd >> NDENTRYSHIFT;
147
148 LOCK_ASSERT(simple_lock_held(&fdp->fd_slock));
149 KDASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) == 0);
150
151 fdp->fd_lomap[off] |= 1 << (fd & NDENTRYMASK);
152 if (fdp->fd_lomap[off] == ~0) {
153 KDASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
154 (1 << (off & NDENTRYMASK))) == 0);
155 fdp->fd_himap[off >> NDENTRYSHIFT] |= 1 << (off & NDENTRYMASK);
156 }
157
158 if (fd > fdp->fd_lastfile)
159 fdp->fd_lastfile = fd;
160 }
161
162 static inline void
163 fd_unused(struct filedesc *fdp, int fd)
164 {
165 u_int off = fd >> NDENTRYSHIFT;
166
167 LOCK_ASSERT(simple_lock_held(&fdp->fd_slock));
168 if (fd < fdp->fd_freefile)
169 fdp->fd_freefile = fd;
170
171 if (fdp->fd_lomap[off] == ~0) {
172 KDASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
173 (1 << (off & NDENTRYMASK))) != 0);
174 fdp->fd_himap[off >> NDENTRYSHIFT] &=
175 ~(1 << (off & NDENTRYMASK));
176 }
177 KDASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) != 0);
178 fdp->fd_lomap[off] &= ~(1 << (fd & NDENTRYMASK));
179
180 #ifdef DIAGNOSTIC
181 if (fd > fdp->fd_lastfile)
182 panic("fd_unused: fd_lastfile inconsistent");
183 #endif
184 if (fd == fdp->fd_lastfile)
185 fdp->fd_lastfile = find_last_set(fdp, fd);
186 }
187
188 /*
189 * Lookup the file structure corresponding to a file descriptor
190 * and return it locked.
191 * Note: typical usage is: `fp = fd_getfile(..); FILE_USE(fp);'
192 * The locking strategy has been optimised for this case, i.e.
193 * fd_getfile() returns the file locked while FILE_USE() will increment
194 * the file's use count and unlock.
195 */
196 struct file *
197 fd_getfile(struct filedesc *fdp, int fd)
198 {
199 struct file *fp;
200
201 if ((u_int) fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
202 return (NULL);
203
204 simple_lock(&fp->f_slock);
205 if (FILE_IS_USABLE(fp) == 0) {
206 simple_unlock(&fp->f_slock);
207 return (NULL);
208 }
209
210 return (fp);
211 }
212
213 /*
214 * Common code for dup, dup2, and fcntl(F_DUPFD).
215 */
216 static int
217 finishdup(struct lwp *l, int old, int new, register_t *retval)
218 {
219 struct filedesc *fdp;
220 struct file *fp, *delfp;
221
222 fdp = l->l_proc->p_fd;
223
224 /*
225 * If there is a file in the new slot, remember it so we
226 * can close it after we've finished the dup. We need
227 * to do it after the dup is finished, since closing
228 * the file may block.
229 *
230 * Note: `old' is already used for us.
231 * Note: Caller already marked `new' slot "used".
232 */
233 simple_lock(&fdp->fd_slock);
234 delfp = fdp->fd_ofiles[new];
235
236 fp = fdp->fd_ofiles[old];
237 KDASSERT(fp != NULL);
238 fdp->fd_ofiles[new] = fp;
239 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
240 simple_unlock(&fdp->fd_slock);
241
242 *retval = new;
243 simple_lock(&fp->f_slock);
244 fp->f_count++;
245 FILE_UNUSE_HAVELOCK(fp, l);
246
247 if (delfp != NULL) {
248 simple_lock(&delfp->f_slock);
249 FILE_USE(delfp);
250 if (new < fdp->fd_knlistsize)
251 knote_fdclose(l, new);
252 (void) closef(delfp, l);
253 }
254 return (0);
255 }
256
257 /*
258 * System calls on descriptors.
259 */
260
261 /*
262 * Duplicate a file descriptor.
263 */
264 /* ARGSUSED */
265 int
266 sys_dup(struct lwp *l, void *v, register_t *retval)
267 {
268 struct sys_dup_args /* {
269 syscallarg(int) fd;
270 } */ *uap = v;
271 struct file *fp;
272 struct filedesc *fdp;
273 struct proc *p;
274 int old, new, error;
275
276 p = l->l_proc;
277 fdp = p->p_fd;
278 old = SCARG(uap, fd);
279
280 restart:
281 if ((fp = fd_getfile(fdp, old)) == NULL)
282 return (EBADF);
283
284 FILE_USE(fp);
285
286 if ((error = fdalloc(p, 0, &new)) != 0) {
287 if (error == ENOSPC) {
288 fdexpand(p);
289 FILE_UNUSE(fp, l);
290 goto restart;
291 }
292 FILE_UNUSE(fp, l);
293 return (error);
294 }
295
296 /* finishdup() will unuse the descriptors for us */
297 return (finishdup(l, old, new, retval));
298 }
299
300 /*
301 * Duplicate a file descriptor to a particular value.
302 */
303 /* ARGSUSED */
304 int
305 sys_dup2(struct lwp *l, void *v, register_t *retval)
306 {
307 struct sys_dup2_args /* {
308 syscallarg(int) from;
309 syscallarg(int) to;
310 } */ *uap = v;
311 struct file *fp;
312 struct filedesc *fdp;
313 struct proc *p;
314 int old, new, i, error;
315
316 p = l->l_proc;
317 fdp = p->p_fd;
318 old = SCARG(uap, from);
319 new = SCARG(uap, to);
320
321 restart:
322 if ((fp = fd_getfile(fdp, old)) == NULL)
323 return (EBADF);
324
325 if ((u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
326 (u_int)new >= maxfiles) {
327 simple_unlock(&fp->f_slock);
328 return (EBADF);
329 }
330
331 if (old == new) {
332 simple_unlock(&fp->f_slock);
333 *retval = new;
334 return (0);
335 }
336
337 FILE_USE(fp);
338
339 if (new >= fdp->fd_nfiles) {
340 if ((error = fdalloc(p, new, &i)) != 0) {
341 if (error == ENOSPC) {
342 fdexpand(p);
343 FILE_UNUSE(fp, l);
344 goto restart;
345 }
346 FILE_UNUSE(fp, l);
347 return (error);
348 }
349 if (new != i)
350 panic("dup2: fdalloc");
351 } else {
352 simple_lock(&fdp->fd_slock);
353 /*
354 * Mark `new' slot "used" only if it was empty.
355 */
356 if (fdp->fd_ofiles[new] == NULL)
357 fd_used(fdp, new);
358 simple_unlock(&fdp->fd_slock);
359 }
360
361 /*
362 * finishdup() will close the file that's in the `new'
363 * slot, if there's one there.
364 */
365
366 /* finishdup() will unuse the descriptors for us */
367 return (finishdup(l, old, new, retval));
368 }
369
370 /*
371 * fcntl call which is being passed to the file's fs.
372 */
373 static int
374 fcntl_forfs(int fd, struct lwp *l, int cmd, void *arg)
375 {
376 struct file *fp;
377 struct filedesc *fdp;
378 int error;
379 u_int size;
380 void *data, *memp;
381 #define STK_PARAMS 128
382 char stkbuf[STK_PARAMS];
383
384 /* fd's value was validated in sys_fcntl before calling this routine */
385 fdp = l->l_proc->p_fd;
386 fp = fdp->fd_ofiles[fd];
387
388 if ((fp->f_flag & (FREAD | FWRITE)) == 0)
389 return (EBADF);
390
391 /*
392 * Interpret high order word to find amount of data to be
393 * copied to/from the user's address space.
394 */
395 size = (size_t)F_PARAM_LEN(cmd);
396 if (size > F_PARAM_MAX)
397 return (EINVAL);
398 memp = NULL;
399 if (size > sizeof(stkbuf)) {
400 memp = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
401 data = memp;
402 } else
403 data = stkbuf;
404 if (cmd & F_FSIN) {
405 if (size) {
406 error = copyin(arg, data, size);
407 if (error) {
408 if (memp)
409 free(memp, M_IOCTLOPS);
410 return (error);
411 }
412 } else
413 *(void **)data = arg;
414 } else if ((cmd & F_FSOUT) && size)
415 /*
416 * Zero the buffer so the user always
417 * gets back something deterministic.
418 */
419 memset(data, 0, size);
420 else if (cmd & F_FSVOID)
421 *(void **)data = arg;
422
423
424 error = (*fp->f_ops->fo_fcntl)(fp, cmd, data, l);
425
426 /*
427 * Copy any data to user, size was
428 * already set and checked above.
429 */
430 if (error == 0 && (cmd & F_FSOUT) && size)
431 error = copyout(data, arg, size);
432 if (memp)
433 free(memp, M_IOCTLOPS);
434 return (error);
435 }
436
437 /*
438 * The file control system call.
439 */
440 /* ARGSUSED */
441 int
442 sys_fcntl(struct lwp *l, void *v, register_t *retval)
443 {
444 struct sys_fcntl_args /* {
445 syscallarg(int) fd;
446 syscallarg(int) cmd;
447 syscallarg(void *) arg;
448 } */ *uap = v;
449 struct filedesc *fdp;
450 struct file *fp;
451 struct proc *p;
452 struct vnode *vp;
453 int fd, i, tmp, error, flg, cmd, newmin;
454 struct flock fl;
455
456 p = l->l_proc;
457 fd = SCARG(uap, fd);
458 cmd = SCARG(uap, cmd);
459 fdp = p->p_fd;
460 error = 0;
461 flg = F_POSIX;
462
463 switch (cmd) {
464 case F_CLOSEM:
465 if (fd < 0)
466 return EBADF;
467 while (fdp->fd_lastfile >= fd)
468 fdrelease(l, fdp->fd_lastfile);
469 return 0;
470
471 case F_MAXFD:
472 *retval = fdp->fd_lastfile;
473 return 0;
474
475 default:
476 /* Handled below */
477 break;
478 }
479
480 restart:
481 if ((fp = fd_getfile(fdp, fd)) == NULL)
482 return (EBADF);
483
484 FILE_USE(fp);
485
486 if ((cmd & F_FSCTL)) {
487 error = fcntl_forfs(fd, l, cmd, SCARG(uap, arg));
488 goto out;
489 }
490
491 switch (cmd) {
492
493 case F_DUPFD:
494 newmin = (long)SCARG(uap, arg);
495 if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
496 (u_int)newmin >= maxfiles) {
497 error = EINVAL;
498 goto out;
499 }
500 if ((error = fdalloc(p, newmin, &i)) != 0) {
501 if (error == ENOSPC) {
502 fdexpand(p);
503 FILE_UNUSE(fp, l);
504 goto restart;
505 }
506 goto out;
507 }
508
509 /* finishdup() will unuse the descriptors for us */
510 return (finishdup(l, fd, i, retval));
511
512 case F_GETFD:
513 *retval = fdp->fd_ofileflags[fd] & UF_EXCLOSE ? 1 : 0;
514 break;
515
516 case F_SETFD:
517 if ((long)SCARG(uap, arg) & 1)
518 fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
519 else
520 fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
521 break;
522
523 case F_GETFL:
524 *retval = OFLAGS(fp->f_flag);
525 break;
526
527 case F_SETFL:
528 tmp = FFLAGS((long)SCARG(uap, arg)) & FCNTLFLAGS;
529 error = (*fp->f_ops->fo_fcntl)(fp, F_SETFL, &tmp, l);
530 if (error)
531 break;
532 i = tmp ^ fp->f_flag;
533 if (i & FNONBLOCK) {
534 int flgs = tmp & FNONBLOCK;
535 error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, &flgs, l);
536 if (error)
537 goto reset_fcntl;
538 }
539 if (i & FASYNC) {
540 int flgs = tmp & FASYNC;
541 error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, &flgs, l);
542 if (error) {
543 if (i & FNONBLOCK) {
544 tmp = fp->f_flag & FNONBLOCK;
545 (void)(*fp->f_ops->fo_ioctl)(fp,
546 FIONBIO, &tmp, l);
547 }
548 goto reset_fcntl;
549 }
550 }
551 fp->f_flag = (fp->f_flag & ~FCNTLFLAGS) | tmp;
552 break;
553 reset_fcntl:
554 (void)(*fp->f_ops->fo_fcntl)(fp, F_SETFL, &fp->f_flag, l);
555 break;
556
557 case F_GETOWN:
558 error = (*fp->f_ops->fo_ioctl)(fp, FIOGETOWN, &tmp, l);
559 *retval = tmp;
560 break;
561
562 case F_SETOWN:
563 tmp = (int)(intptr_t) SCARG(uap, arg);
564 error = (*fp->f_ops->fo_ioctl)(fp, FIOSETOWN, &tmp, l);
565 break;
566
567 case F_SETLKW:
568 flg |= F_WAIT;
569 /* Fall into F_SETLK */
570
571 case F_SETLK:
572 if (fp->f_type != DTYPE_VNODE) {
573 error = EINVAL;
574 goto out;
575 }
576 vp = (struct vnode *)fp->f_data;
577 /* Copy in the lock structure */
578 error = copyin(SCARG(uap, arg), &fl, sizeof(fl));
579 if (error)
580 goto out;
581 if (fl.l_whence == SEEK_CUR)
582 fl.l_start += fp->f_offset;
583 switch (fl.l_type) {
584 case F_RDLCK:
585 if ((fp->f_flag & FREAD) == 0) {
586 error = EBADF;
587 goto out;
588 }
589 p->p_flag |= P_ADVLOCK;
590 error = VOP_ADVLOCK(vp, p, F_SETLK, &fl, flg);
591 goto out;
592
593 case F_WRLCK:
594 if ((fp->f_flag & FWRITE) == 0) {
595 error = EBADF;
596 goto out;
597 }
598 p->p_flag |= P_ADVLOCK;
599 error = VOP_ADVLOCK(vp, p, F_SETLK, &fl, flg);
600 goto out;
601
602 case F_UNLCK:
603 error = VOP_ADVLOCK(vp, p, F_UNLCK, &fl, F_POSIX);
604 goto out;
605
606 default:
607 error = EINVAL;
608 goto out;
609 }
610
611 case F_GETLK:
612 if (fp->f_type != DTYPE_VNODE) {
613 error = EINVAL;
614 goto out;
615 }
616 vp = (struct vnode *)fp->f_data;
617 /* Copy in the lock structure */
618 error = copyin(SCARG(uap, arg), &fl, sizeof(fl));
619 if (error)
620 goto out;
621 if (fl.l_whence == SEEK_CUR)
622 fl.l_start += fp->f_offset;
623 if (fl.l_type != F_RDLCK &&
624 fl.l_type != F_WRLCK &&
625 fl.l_type != F_UNLCK) {
626 error = EINVAL;
627 goto out;
628 }
629 error = VOP_ADVLOCK(vp, p, F_GETLK, &fl, F_POSIX);
630 if (error)
631 goto out;
632 error = copyout(&fl, SCARG(uap, arg), sizeof(fl));
633 break;
634
635 default:
636 error = EINVAL;
637 }
638
639 out:
640 FILE_UNUSE(fp, l);
641 return (error);
642 }
643
644 void
645 fdremove(struct filedesc *fdp, int fd)
646 {
647
648 simple_lock(&fdp->fd_slock);
649 fdp->fd_ofiles[fd] = NULL;
650 fd_unused(fdp, fd);
651 simple_unlock(&fdp->fd_slock);
652 }
653
654 int
655 fdrelease(struct lwp *l, int fd)
656 {
657 struct proc *p = l->l_proc;
658 struct filedesc *fdp;
659 struct file **fpp, *fp;
660
661 fdp = p->p_fd;
662 simple_lock(&fdp->fd_slock);
663 if (fd < 0 || fd > fdp->fd_lastfile)
664 goto badf;
665 fpp = &fdp->fd_ofiles[fd];
666 fp = *fpp;
667 if (fp == NULL)
668 goto badf;
669
670 simple_lock(&fp->f_slock);
671 if (!FILE_IS_USABLE(fp)) {
672 simple_unlock(&fp->f_slock);
673 goto badf;
674 }
675
676 FILE_USE(fp);
677
678 *fpp = NULL;
679 fdp->fd_ofileflags[fd] = 0;
680 fd_unused(fdp, fd);
681 simple_unlock(&fdp->fd_slock);
682 if (fd < fdp->fd_knlistsize)
683 knote_fdclose(l, fd);
684 return (closef(fp, l));
685
686 badf:
687 simple_unlock(&fdp->fd_slock);
688 return (EBADF);
689 }
690
691 /*
692 * Close a file descriptor.
693 */
694 /* ARGSUSED */
695 int
696 sys_close(struct lwp *l, void *v, register_t *retval)
697 {
698 struct sys_close_args /* {
699 syscallarg(int) fd;
700 } */ *uap = v;
701 int fd;
702 struct filedesc *fdp;
703 struct proc *p;
704
705 p = l->l_proc;
706 fd = SCARG(uap, fd);
707 fdp = p->p_fd;
708
709 #if 0
710 if (fd_getfile(fdp, fd) == NULL)
711 return (EBADF);
712 #endif
713
714 return (fdrelease(l, fd));
715 }
716
717 /*
718 * Return status information about a file descriptor.
719 */
720 /* ARGSUSED */
721 int
722 sys___fstat30(struct lwp *l, void *v, register_t *retval)
723 {
724 struct sys___fstat30_args /* {
725 syscallarg(int) fd;
726 syscallarg(struct stat *) sb;
727 } */ *uap = v;
728 int fd;
729 struct filedesc *fdp;
730 struct file *fp;
731 struct proc *p;
732 struct stat ub;
733 int error;
734
735 p = l->l_proc;
736 fd = SCARG(uap, fd);
737 fdp = p->p_fd;
738
739 if ((fp = fd_getfile(fdp, fd)) == NULL)
740 return (EBADF);
741
742 FILE_USE(fp);
743 error = (*fp->f_ops->fo_stat)(fp, &ub, l);
744 FILE_UNUSE(fp, l);
745
746 if (error == 0)
747 error = copyout(&ub, SCARG(uap, sb), sizeof(ub));
748
749 return (error);
750 }
751
752 /*
753 * Return pathconf information about a file descriptor.
754 */
755 /* ARGSUSED */
756 int
757 sys_fpathconf(struct lwp *l, void *v, register_t *retval)
758 {
759 struct sys_fpathconf_args /* {
760 syscallarg(int) fd;
761 syscallarg(int) name;
762 } */ *uap = v;
763 int fd;
764 struct filedesc *fdp;
765 struct file *fp;
766 struct proc *p;
767 struct vnode *vp;
768 int error;
769
770 p = l->l_proc;
771 fd = SCARG(uap, fd);
772 fdp = p->p_fd;
773 error = 0;
774
775 if ((fp = fd_getfile(fdp, fd)) == NULL)
776 return (EBADF);
777
778 FILE_USE(fp);
779
780 switch (fp->f_type) {
781
782 case DTYPE_SOCKET:
783 case DTYPE_PIPE:
784 if (SCARG(uap, name) != _PC_PIPE_BUF)
785 error = EINVAL;
786 else
787 *retval = PIPE_BUF;
788 break;
789
790 case DTYPE_VNODE:
791 vp = (struct vnode *)fp->f_data;
792 error = VOP_PATHCONF(vp, SCARG(uap, name), retval);
793 break;
794
795 case DTYPE_KQUEUE:
796 error = EINVAL;
797 break;
798
799 default:
800 error = EOPNOTSUPP;
801 break;
802 }
803
804 FILE_UNUSE(fp, l);
805 return (error);
806 }
807
808 /*
809 * Allocate a file descriptor for the process.
810 */
811 int fdexpanded; /* XXX: what else uses this? */
812
813 int
814 fdalloc(struct proc *p, int want, int *result)
815 {
816 struct filedesc *fdp;
817 int i, lim, last, error;
818 u_int off, new;
819
820 fdp = p->p_fd;
821 simple_lock(&fdp->fd_slock);
822
823 /*
824 * Search for a free descriptor starting at the higher
825 * of want or fd_freefile. If that fails, consider
826 * expanding the ofile array.
827 */
828 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
829 last = min(fdp->fd_nfiles, lim);
830 again:
831 if ((i = want) < fdp->fd_freefile)
832 i = fdp->fd_freefile;
833 off = i >> NDENTRYSHIFT;
834 new = find_next_zero(fdp->fd_himap, off,
835 (last + NDENTRIES - 1) >> NDENTRYSHIFT);
836 if (new != -1) {
837 i = find_next_zero(&fdp->fd_lomap[new],
838 new > off ? 0 : i & NDENTRYMASK, NDENTRIES);
839 if (i == -1) {
840 /*
841 * free file descriptor in this block was
842 * below want, try again with higher want.
843 */
844 want = (new + 1) << NDENTRYSHIFT;
845 goto again;
846 }
847 i += (new << NDENTRYSHIFT);
848 if (i < last) {
849 if (fdp->fd_ofiles[i] == NULL) {
850 fd_used(fdp, i);
851 if (want <= fdp->fd_freefile)
852 fdp->fd_freefile = i;
853 *result = i;
854 error = 0;
855 goto out;
856 }
857 }
858 }
859
860 /* No space in current array. Expand or let the caller do it. */
861 error = (fdp->fd_nfiles >= lim) ? EMFILE : ENOSPC;
862
863 out:
864 simple_unlock(&fdp->fd_slock);
865 return (error);
866 }
867
868 void
869 fdexpand(struct proc *p)
870 {
871 struct filedesc *fdp;
872 int i, numfiles, oldnfiles;
873 struct file **newofile;
874 char *newofileflags;
875 uint32_t *newhimap = NULL, *newlomap = NULL;
876
877 fdp = p->p_fd;
878
879 restart:
880 oldnfiles = fdp->fd_nfiles;
881
882 if (oldnfiles < NDEXTENT)
883 numfiles = NDEXTENT;
884 else
885 numfiles = 2 * oldnfiles;
886
887 newofile = malloc(numfiles * OFILESIZE, M_FILEDESC, M_WAITOK);
888 if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
889 newhimap = malloc(NDHISLOTS(numfiles) * sizeof(uint32_t),
890 M_FILEDESC, M_WAITOK);
891 newlomap = malloc(NDLOSLOTS(numfiles) * sizeof(uint32_t),
892 M_FILEDESC, M_WAITOK);
893 }
894
895 simple_lock(&fdp->fd_slock);
896 /* lock fdp */
897 if (fdp->fd_nfiles != oldnfiles) {
898 /* fdp changed; retry */
899 simple_unlock(&fdp->fd_slock);
900 free(newofile, M_FILEDESC);
901 if (newhimap != NULL) free(newhimap, M_FILEDESC);
902 if (newlomap != NULL) free(newlomap, M_FILEDESC);
903 goto restart;
904 }
905
906 newofileflags = (char *) &newofile[numfiles];
907 /*
908 * Copy the existing ofile and ofileflags arrays
909 * and zero the new portion of each array.
910 */
911 memcpy(newofile, fdp->fd_ofiles,
912 (i = sizeof(struct file *) * fdp->fd_nfiles));
913 memset((char *)newofile + i, 0,
914 numfiles * sizeof(struct file *) - i);
915 memcpy(newofileflags, fdp->fd_ofileflags,
916 (i = sizeof(char) * fdp->fd_nfiles));
917 memset(newofileflags + i, 0, numfiles * sizeof(char) - i);
918 if (oldnfiles > NDFILE)
919 free(fdp->fd_ofiles, M_FILEDESC);
920
921 if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
922 memcpy(newhimap, fdp->fd_himap,
923 (i = NDHISLOTS(oldnfiles) * sizeof(uint32_t)));
924 memset((char *)newhimap + i, 0,
925 NDHISLOTS(numfiles) * sizeof(uint32_t) - i);
926
927 memcpy(newlomap, fdp->fd_lomap,
928 (i = NDLOSLOTS(oldnfiles) * sizeof(uint32_t)));
929 memset((char *)newlomap + i, 0,
930 NDLOSLOTS(numfiles) * sizeof(uint32_t) - i);
931
932 if (NDHISLOTS(oldnfiles) > NDHISLOTS(NDFILE)) {
933 free(fdp->fd_himap, M_FILEDESC);
934 free(fdp->fd_lomap, M_FILEDESC);
935 }
936 fdp->fd_himap = newhimap;
937 fdp->fd_lomap = newlomap;
938 }
939
940 fdp->fd_ofiles = newofile;
941 fdp->fd_ofileflags = newofileflags;
942 fdp->fd_nfiles = numfiles;
943
944 simple_unlock(&fdp->fd_slock);
945
946 fdexpanded++;
947 }
948
949 /*
950 * Create a new open file structure and allocate
951 * a file descriptor for the process that refers to it.
952 */
953 int
954 falloc(struct lwp *l, struct file **resultfp, int *resultfd)
955 {
956 struct file *fp, *fq;
957 struct proc *p;
958 int error, i;
959
960 p = l->l_proc;
961
962 restart:
963 if ((error = fdalloc(p, 0, &i)) != 0) {
964 if (error == ENOSPC) {
965 fdexpand(p);
966 goto restart;
967 }
968 return (error);
969 }
970
971 fp = pool_get(&file_pool, PR_WAITOK);
972 simple_lock(&filelist_slock);
973 if (nfiles >= maxfiles) {
974 tablefull("file", "increase kern.maxfiles or MAXFILES");
975 simple_unlock(&filelist_slock);
976 simple_lock(&p->p_fd->fd_slock);
977 fd_unused(p->p_fd, i);
978 simple_unlock(&p->p_fd->fd_slock);
979 pool_put(&file_pool, fp);
980 return (ENFILE);
981 }
982 /*
983 * Allocate a new file descriptor.
984 * If the process has file descriptor zero open, add to the list
985 * of open files at that point, otherwise put it at the front of
986 * the list of open files.
987 */
988 nfiles++;
989 memset(fp, 0, sizeof(struct file));
990 fp->f_iflags = FIF_LARVAL;
991 if ((fq = p->p_fd->fd_ofiles[0]) != NULL) {
992 LIST_INSERT_AFTER(fq, fp, f_list);
993 } else {
994 LIST_INSERT_HEAD(&filehead, fp, f_list);
995 }
996 simple_unlock(&filelist_slock);
997 KDASSERT(p->p_fd->fd_ofiles[i] == NULL);
998 p->p_fd->fd_ofiles[i] = fp;
999 simple_lock_init(&fp->f_slock);
1000 fp->f_count = 1;
1001 fp->f_cred = l->l_cred;
1002 kauth_cred_hold(fp->f_cred);
1003 if (resultfp) {
1004 fp->f_usecount = 1;
1005 *resultfp = fp;
1006 }
1007 if (resultfd)
1008 *resultfd = i;
1009 return (0);
1010 }
1011
1012 /*
1013 * Free a file descriptor.
1014 */
1015 void
1016 ffree(struct file *fp)
1017 {
1018 kauth_cred_t cred;
1019
1020 #ifdef DIAGNOSTIC
1021 if (fp->f_usecount)
1022 panic("ffree");
1023 #endif
1024
1025 simple_lock(&filelist_slock);
1026 LIST_REMOVE(fp, f_list);
1027 cred = fp->f_cred;
1028 #ifdef DIAGNOSTIC
1029 fp->f_cred = NULL;
1030 fp->f_count = 0; /* What's the point? */
1031 #endif
1032 nfiles--;
1033 simple_unlock(&filelist_slock);
1034 pool_put(&file_pool, fp);
1035 kauth_cred_free(cred);
1036 }
1037
1038 /*
1039 * Create an initial cwdinfo structure, using the same current and root
1040 * directories as p.
1041 */
1042 struct cwdinfo *
1043 cwdinit(struct proc *p)
1044 {
1045 struct cwdinfo *cwdi;
1046
1047 cwdi = pool_get(&cwdi_pool, PR_WAITOK);
1048
1049 simple_lock_init(&cwdi->cwdi_slock);
1050 cwdi->cwdi_cdir = p->p_cwdi->cwdi_cdir;
1051 if (cwdi->cwdi_cdir)
1052 VREF(cwdi->cwdi_cdir);
1053 cwdi->cwdi_rdir = p->p_cwdi->cwdi_rdir;
1054 if (cwdi->cwdi_rdir)
1055 VREF(cwdi->cwdi_rdir);
1056 cwdi->cwdi_cmask = p->p_cwdi->cwdi_cmask;
1057 cwdi->cwdi_refcnt = 1;
1058
1059 return (cwdi);
1060 }
1061
1062 /*
1063 * Make p2 share p1's cwdinfo.
1064 */
1065 void
1066 cwdshare(struct proc *p1, struct proc *p2)
1067 {
1068 struct cwdinfo *cwdi = p1->p_cwdi;
1069
1070 simple_lock(&cwdi->cwdi_slock);
1071 cwdi->cwdi_refcnt++;
1072 simple_unlock(&cwdi->cwdi_slock);
1073 p2->p_cwdi = cwdi;
1074 }
1075
1076 /*
1077 * Make this process not share its cwdinfo structure, maintaining
1078 * all cwdinfo state.
1079 */
1080 void
1081 cwdunshare(struct proc *p)
1082 {
1083 struct cwdinfo *oldcwdi, *newcwdi;
1084
1085 if (p->p_cwdi->cwdi_refcnt == 1)
1086 return;
1087
1088 newcwdi = cwdinit(p);
1089 oldcwdi = p->p_cwdi;
1090 p->p_cwdi = newcwdi;
1091 cwdfree(oldcwdi);
1092 }
1093
1094 /*
1095 * Release a cwdinfo structure.
1096 */
1097 void
1098 cwdfree(struct cwdinfo *cwdi)
1099 {
1100 int n;
1101
1102 simple_lock(&cwdi->cwdi_slock);
1103 n = --cwdi->cwdi_refcnt;
1104 simple_unlock(&cwdi->cwdi_slock);
1105 if (n > 0)
1106 return;
1107
1108 vrele(cwdi->cwdi_cdir);
1109 if (cwdi->cwdi_rdir)
1110 vrele(cwdi->cwdi_rdir);
1111 pool_put(&cwdi_pool, cwdi);
1112 }
1113
1114 /*
1115 * Create an initial filedesc structure, using the same current and root
1116 * directories as p.
1117 */
1118 struct filedesc *
1119 fdinit(struct proc *p)
1120 {
1121 struct filedesc0 *newfdp;
1122
1123 newfdp = pool_get(&filedesc0_pool, PR_WAITOK);
1124 memset(newfdp, 0, sizeof(struct filedesc0));
1125
1126 fdinit1(newfdp);
1127
1128 return (&newfdp->fd_fd);
1129 }
1130
1131 /*
1132 * Initialize a file descriptor table.
1133 */
1134 void
1135 fdinit1(struct filedesc0 *newfdp)
1136 {
1137
1138 newfdp->fd_fd.fd_refcnt = 1;
1139 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1140 newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1141 newfdp->fd_fd.fd_nfiles = NDFILE;
1142 newfdp->fd_fd.fd_knlistsize = -1;
1143 newfdp->fd_fd.fd_himap = newfdp->fd_dhimap;
1144 newfdp->fd_fd.fd_lomap = newfdp->fd_dlomap;
1145 newfdp->fd_fd.fd_lastfile = -1;
1146 simple_lock_init(&newfdp->fd_fd.fd_slock);
1147 }
1148
1149 /*
1150 * Make p2 share p1's filedesc structure.
1151 */
1152 void
1153 fdshare(struct proc *p1, struct proc *p2)
1154 {
1155 struct filedesc *fdp = p1->p_fd;
1156
1157 simple_lock(&fdp->fd_slock);
1158 p2->p_fd = fdp;
1159 fdp->fd_refcnt++;
1160 simple_unlock(&fdp->fd_slock);
1161 }
1162
1163 /*
1164 * Make this process not share its filedesc structure, maintaining
1165 * all file descriptor state.
1166 */
1167 void
1168 fdunshare(struct lwp *l)
1169 {
1170 struct proc *p = l->l_proc;
1171 struct filedesc *newfd;
1172
1173 if (p->p_fd->fd_refcnt == 1)
1174 return;
1175
1176 newfd = fdcopy(p);
1177 fdfree(l);
1178 p->p_fd = newfd;
1179 }
1180
1181 /*
1182 * Clear a process's fd table.
1183 */
1184 void
1185 fdclear(struct lwp *l)
1186 {
1187 struct proc *p = l->l_proc;
1188 struct filedesc *newfd;
1189
1190 newfd = fdinit(p);
1191 fdfree(l);
1192 p->p_fd = newfd;
1193 }
1194
1195 /*
1196 * Copy a filedesc structure.
1197 */
1198 struct filedesc *
1199 fdcopy(struct proc *p)
1200 {
1201 struct filedesc *newfdp, *fdp;
1202 struct file **fpp, **nfpp;
1203 int i, numfiles, lastfile;
1204
1205 fdp = p->p_fd;
1206 newfdp = pool_get(&filedesc0_pool, PR_WAITOK);
1207 newfdp->fd_refcnt = 1;
1208 simple_lock_init(&newfdp->fd_slock);
1209
1210 restart:
1211 numfiles = fdp->fd_nfiles;
1212 lastfile = fdp->fd_lastfile;
1213
1214 /*
1215 * If the number of open files fits in the internal arrays
1216 * of the open file structure, use them, otherwise allocate
1217 * additional memory for the number of descriptors currently
1218 * in use.
1219 */
1220 if (lastfile < NDFILE) {
1221 i = NDFILE;
1222 } else {
1223 /*
1224 * Compute the smallest multiple of NDEXTENT needed
1225 * for the file descriptors currently in use,
1226 * allowing the table to shrink.
1227 */
1228 i = numfiles;
1229 while (i >= 2 * NDEXTENT && i > lastfile * 2)
1230 i /= 2;
1231 newfdp->fd_ofiles = malloc(i * OFILESIZE, M_FILEDESC, M_WAITOK);
1232 }
1233 if (NDHISLOTS(i) > NDHISLOTS(NDFILE)) {
1234 newfdp->fd_himap = malloc(NDHISLOTS(i) * sizeof(uint32_t),
1235 M_FILEDESC, M_WAITOK);
1236 newfdp->fd_lomap = malloc(NDLOSLOTS(i) * sizeof(uint32_t),
1237 M_FILEDESC, M_WAITOK);
1238 }
1239
1240 simple_lock(&fdp->fd_slock);
1241 if (numfiles != fdp->fd_nfiles || lastfile != fdp->fd_lastfile) {
1242 simple_unlock(&fdp->fd_slock);
1243 if (i > NDFILE)
1244 free(newfdp->fd_ofiles, M_FILEDESC);
1245 if (NDHISLOTS(i) > NDHISLOTS(NDFILE)) {
1246 free(newfdp->fd_himap, M_FILEDESC);
1247 free(newfdp->fd_lomap, M_FILEDESC);
1248 }
1249 goto restart;
1250 }
1251
1252 if (lastfile < NDFILE) {
1253 newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
1254 newfdp->fd_ofileflags =
1255 ((struct filedesc0 *) newfdp)->fd_dfileflags;
1256 } else {
1257 newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
1258 }
1259 if (NDHISLOTS(i) <= NDHISLOTS(NDFILE)) {
1260 newfdp->fd_himap =
1261 ((struct filedesc0 *) newfdp)->fd_dhimap;
1262 newfdp->fd_lomap =
1263 ((struct filedesc0 *) newfdp)->fd_dlomap;
1264 }
1265
1266 newfdp->fd_nfiles = i;
1267 newfdp->fd_lastfile = lastfile;
1268 newfdp->fd_freefile = fdp->fd_freefile;
1269
1270 /* Clear the entries that will not be copied over.
1271 * Avoid calling memset with 0 size (i.e. when
1272 * lastfile == i-1 */
1273 if (lastfile < (i-1))
1274 memset(newfdp->fd_ofiles + lastfile + 1, 0,
1275 (i - lastfile - 1) * sizeof(struct file **));
1276 memcpy(newfdp->fd_ofileflags, fdp->fd_ofileflags, i * sizeof(char));
1277 if (i < NDENTRIES * NDENTRIES)
1278 i = NDENTRIES * NDENTRIES; /* size of inlined bitmaps */
1279 memcpy(newfdp->fd_himap, fdp->fd_himap, NDHISLOTS(i)*sizeof(uint32_t));
1280 memcpy(newfdp->fd_lomap, fdp->fd_lomap, NDLOSLOTS(i)*sizeof(uint32_t));
1281
1282 fpp = fdp->fd_ofiles;
1283 nfpp = newfdp->fd_ofiles;
1284 for (i = 0; i <= lastfile; i++, fpp++, nfpp++) {
1285 if ((*nfpp = *fpp) == NULL)
1286 continue;
1287
1288 if ((*fpp)->f_type == DTYPE_KQUEUE)
1289 /* kq descriptors cannot be copied. */
1290 fdremove(newfdp, i);
1291 else {
1292 simple_lock(&(*fpp)->f_slock);
1293 (*fpp)->f_count++;
1294 simple_unlock(&(*fpp)->f_slock);
1295 }
1296 }
1297
1298 simple_unlock(&fdp->fd_slock);
1299
1300 newfdp->fd_knlist = NULL;
1301 newfdp->fd_knlistsize = -1;
1302 newfdp->fd_knhash = NULL;
1303 newfdp->fd_knhashmask = 0;
1304
1305 return (newfdp);
1306 }
1307
1308 /*
1309 * Release a filedesc structure.
1310 */
1311 void
1312 fdfree(struct lwp *l)
1313 {
1314 struct proc *p = l->l_proc;
1315 struct filedesc *fdp;
1316 struct file **fpp, *fp;
1317 int i;
1318
1319 fdp = p->p_fd;
1320 simple_lock(&fdp->fd_slock);
1321 i = --fdp->fd_refcnt;
1322 simple_unlock(&fdp->fd_slock);
1323 if (i > 0)
1324 return;
1325
1326 fpp = fdp->fd_ofiles;
1327 for (i = fdp->fd_lastfile; i >= 0; i--, fpp++) {
1328 fp = *fpp;
1329 if (fp != NULL) {
1330 *fpp = NULL;
1331 simple_lock(&fp->f_slock);
1332 FILE_USE(fp);
1333 if ((fdp->fd_lastfile - i) < fdp->fd_knlistsize)
1334 knote_fdclose(l, fdp->fd_lastfile - i);
1335 (void) closef(fp, l);
1336 }
1337 }
1338 p->p_fd = NULL;
1339 if (fdp->fd_nfiles > NDFILE)
1340 free(fdp->fd_ofiles, M_FILEDESC);
1341 if (NDHISLOTS(fdp->fd_nfiles) > NDHISLOTS(NDFILE)) {
1342 free(fdp->fd_himap, M_FILEDESC);
1343 free(fdp->fd_lomap, M_FILEDESC);
1344 }
1345 if (fdp->fd_knlist)
1346 free(fdp->fd_knlist, M_KEVENT);
1347 if (fdp->fd_knhash)
1348 hashdone(fdp->fd_knhash, M_KEVENT);
1349 pool_put(&filedesc0_pool, fdp);
1350 }
1351
1352 /*
1353 * Internal form of close.
1354 * Decrement reference count on file structure.
1355 * Note: p may be NULL when closing a file
1356 * that was being passed in a message.
1357 *
1358 * Note: we expect the caller is holding a usecount, and expects us
1359 * to drop it (the caller thinks the file is going away forever).
1360 */
1361 int
1362 closef(struct file *fp, struct lwp *l)
1363 {
1364 struct proc *p = l ? l->l_proc : NULL;
1365 struct vnode *vp;
1366 struct flock lf;
1367 int error;
1368
1369 if (fp == NULL)
1370 return (0);
1371
1372 /*
1373 * POSIX record locking dictates that any close releases ALL
1374 * locks owned by this process. This is handled by setting
1375 * a flag in the unlock to free ONLY locks obeying POSIX
1376 * semantics, and not to free BSD-style file locks.
1377 * If the descriptor was in a message, POSIX-style locks
1378 * aren't passed with the descriptor.
1379 */
1380 if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
1381 lf.l_whence = SEEK_SET;
1382 lf.l_start = 0;
1383 lf.l_len = 0;
1384 lf.l_type = F_UNLCK;
1385 vp = (struct vnode *)fp->f_data;
1386 (void) VOP_ADVLOCK(vp, p, F_UNLCK, &lf, F_POSIX);
1387 }
1388
1389 /*
1390 * If WANTCLOSE is set, then the reference count on the file
1391 * is 0, but there were multiple users of the file. This can
1392 * happen if a filedesc structure is shared by multiple
1393 * processes.
1394 */
1395 simple_lock(&fp->f_slock);
1396 if (fp->f_iflags & FIF_WANTCLOSE) {
1397 /*
1398 * Another user of the file is already closing, and is
1399 * simply waiting for other users of the file to drain.
1400 * Release our usecount, and wake up the closer if it
1401 * is the only remaining use.
1402 */
1403 #ifdef DIAGNOSTIC
1404 if (fp->f_count != 0)
1405 panic("closef: wantclose and count != 0");
1406 if (fp->f_usecount < 2)
1407 panic("closef: wantclose and usecount < 2");
1408 #endif
1409 if (--fp->f_usecount == 1)
1410 wakeup(&fp->f_usecount);
1411 simple_unlock(&fp->f_slock);
1412 return (0);
1413 } else {
1414 /*
1415 * Decrement the reference count. If we were not the
1416 * last reference, then release our use and just
1417 * return.
1418 */
1419 if (--fp->f_count > 0) {
1420 #ifdef DIAGNOSTIC
1421 if (fp->f_usecount < 1)
1422 panic("closef: no wantclose and usecount < 1");
1423 #endif
1424 fp->f_usecount--;
1425 simple_unlock(&fp->f_slock);
1426 return (0);
1427 }
1428 }
1429
1430 /*
1431 * The reference count is now 0. However, there may be
1432 * multiple potential users of this file. This can happen
1433 * if multiple processes shared a single filedesc structure.
1434 *
1435 * Notify these potential users that the file is closing.
1436 * This will prevent them from adding additional uses to
1437 * the file.
1438 */
1439 fp->f_iflags |= FIF_WANTCLOSE;
1440
1441 /*
1442 * We expect the caller to add a use to the file. So, if we
1443 * are the last user, usecount will be 1. If it is not, we
1444 * must wait for the usecount to drain. When it drains back
1445 * to 1, we will be awakened so that we may proceed with the
1446 * close.
1447 */
1448 #ifdef DIAGNOSTIC
1449 if (fp->f_usecount < 1)
1450 panic("closef: usecount < 1");
1451 #endif
1452 while (fp->f_usecount > 1)
1453 (void) ltsleep(&fp->f_usecount, PRIBIO, "closef", 0,
1454 &fp->f_slock);
1455 #ifdef DIAGNOSTIC
1456 if (fp->f_usecount != 1)
1457 panic("closef: usecount != 1");
1458 #endif
1459
1460 simple_unlock(&fp->f_slock);
1461 if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
1462 lf.l_whence = SEEK_SET;
1463 lf.l_start = 0;
1464 lf.l_len = 0;
1465 lf.l_type = F_UNLCK;
1466 vp = (struct vnode *)fp->f_data;
1467 (void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
1468 }
1469 if (fp->f_ops)
1470 error = (*fp->f_ops->fo_close)(fp, l);
1471 else
1472 error = 0;
1473
1474 /* Nothing references the file now, drop the final use (us). */
1475 fp->f_usecount--;
1476
1477 ffree(fp);
1478 return (error);
1479 }
1480
1481 /*
1482 * Apply an advisory lock on a file descriptor.
1483 *
1484 * Just attempt to get a record lock of the requested type on
1485 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1486 */
1487 /* ARGSUSED */
1488 int
1489 sys_flock(struct lwp *l, void *v, register_t *retval)
1490 {
1491 struct sys_flock_args /* {
1492 syscallarg(int) fd;
1493 syscallarg(int) how;
1494 } */ *uap = v;
1495 int fd, how, error;
1496 struct proc *p;
1497 struct filedesc *fdp;
1498 struct file *fp;
1499 struct vnode *vp;
1500 struct flock lf;
1501
1502 p = l->l_proc;
1503 fd = SCARG(uap, fd);
1504 how = SCARG(uap, how);
1505 fdp = p->p_fd;
1506 error = 0;
1507
1508 if ((fp = fd_getfile(fdp, fd)) == NULL)
1509 return (EBADF);
1510
1511 FILE_USE(fp);
1512
1513 if (fp->f_type != DTYPE_VNODE) {
1514 error = EOPNOTSUPP;
1515 goto out;
1516 }
1517
1518 vp = (struct vnode *)fp->f_data;
1519 lf.l_whence = SEEK_SET;
1520 lf.l_start = 0;
1521 lf.l_len = 0;
1522 if (how & LOCK_UN) {
1523 lf.l_type = F_UNLCK;
1524 fp->f_flag &= ~FHASLOCK;
1525 error = VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
1526 goto out;
1527 }
1528 if (how & LOCK_EX)
1529 lf.l_type = F_WRLCK;
1530 else if (how & LOCK_SH)
1531 lf.l_type = F_RDLCK;
1532 else {
1533 error = EINVAL;
1534 goto out;
1535 }
1536 fp->f_flag |= FHASLOCK;
1537 if (how & LOCK_NB)
1538 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, F_FLOCK);
1539 else
1540 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf,
1541 F_FLOCK|F_WAIT);
1542 out:
1543 FILE_UNUSE(fp, l);
1544 return (error);
1545 }
1546
1547 /* ARGSUSED */
1548 int
1549 sys_posix_fadvise(struct lwp *l, void *v, register_t *retval)
1550 {
1551 const struct sys_posix_fadvise_args /* {
1552 syscallarg(int) fd;
1553 syscallarg(off_t) offset;
1554 syscallarg(off_t) len;
1555 syscallarg(int) advice;
1556 } */ *uap = v;
1557 const int fd = SCARG(uap, fd);
1558 const int advice = SCARG(uap, advice);
1559 struct proc *p = l->l_proc;
1560 struct file *fp;
1561 int error = 0;
1562
1563 fp = fd_getfile(p->p_fd, fd);
1564 if (fp == NULL) {
1565 error = EBADF;
1566 goto out;
1567 }
1568 FILE_USE(fp);
1569
1570 if (fp->f_type != DTYPE_VNODE) {
1571 if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
1572 error = ESPIPE;
1573 } else {
1574 error = EOPNOTSUPP;
1575 }
1576 goto out;
1577 }
1578
1579 switch (advice) {
1580 case POSIX_FADV_NORMAL:
1581 case POSIX_FADV_RANDOM:
1582 case POSIX_FADV_SEQUENTIAL:
1583 KASSERT(POSIX_FADV_NORMAL == UVM_ADV_NORMAL);
1584 KASSERT(POSIX_FADV_RANDOM == UVM_ADV_RANDOM);
1585 KASSERT(POSIX_FADV_SEQUENTIAL == UVM_ADV_SEQUENTIAL);
1586
1587 /*
1588 * we ignore offset and size.
1589 */
1590
1591 fp->f_advice = advice;
1592 break;
1593
1594 case POSIX_FADV_WILLNEED:
1595 case POSIX_FADV_DONTNEED:
1596 case POSIX_FADV_NOREUSE:
1597
1598 /*
1599 * not implemented yet.
1600 */
1601
1602 break;
1603 default:
1604 error = EINVAL;
1605 break;
1606 }
1607 out:
1608 if (fp != NULL) {
1609 FILE_UNUSE(fp, l);
1610 }
1611 *retval = error;
1612 return 0;
1613 }
1614
1615 /*
1616 * File Descriptor pseudo-device driver (/dev/fd/).
1617 *
1618 * Opening minor device N dup()s the file (if any) connected to file
1619 * descriptor N belonging to the calling process. Note that this driver
1620 * consists of only the ``open()'' routine, because all subsequent
1621 * references to this file will be direct to the other driver.
1622 */
1623 /* ARGSUSED */
1624 static int
1625 filedescopen(dev_t dev, int mode, int type, struct lwp *l)
1626 {
1627
1628 /*
1629 * XXX Kludge: set dupfd to contain the value of the
1630 * the file descriptor being sought for duplication. The error
1631 * return ensures that the vnode for this device will be released
1632 * by vn_open. Open will detect this special error and take the
1633 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1634 * will simply report the error.
1635 */
1636 l->l_dupfd = minor(dev); /* XXX */
1637 return EDUPFD;
1638 }
1639
1640 const struct cdevsw filedesc_cdevsw = {
1641 filedescopen, noclose, noread, nowrite, noioctl,
1642 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
1643 };
1644
1645 /*
1646 * Duplicate the specified descriptor to a free descriptor.
1647 *
1648 * 'indx' has been fdalloc'ed (and will be fdremove'ed on error) by the caller.
1649 */
1650 int
1651 dupfdopen(struct lwp *l, int indx, int dfd, int mode, int error)
1652 {
1653 struct proc *p = l->l_proc;
1654 struct filedesc *fdp;
1655 struct file *wfp;
1656
1657 fdp = p->p_fd;
1658
1659 /* should be cleared by the caller */
1660 KASSERT(fdp->fd_ofiles[indx] == NULL);
1661
1662 /*
1663 * If the to-be-dup'd fd number is greater than the allowed number
1664 * of file descriptors, or the fd to be dup'd has already been
1665 * closed, reject.
1666 */
1667
1668 /*
1669 * Note, in the case of indx == dfd, fd_getfile below returns NULL.
1670 */
1671 if ((wfp = fd_getfile(fdp, dfd)) == NULL)
1672 return (EBADF);
1673
1674 FILE_USE(wfp);
1675
1676 /*
1677 * There are two cases of interest here.
1678 *
1679 * For EDUPFD simply dup (dfd) to file descriptor
1680 * (indx) and return.
1681 *
1682 * For EMOVEFD steal away the file structure from (dfd) and
1683 * store it in (indx). (dfd) is effectively closed by
1684 * this operation.
1685 *
1686 * Any other error code is just returned.
1687 */
1688 switch (error) {
1689 case EDUPFD:
1690 /*
1691 * Check that the mode the file is being opened for is a
1692 * subset of the mode of the existing descriptor.
1693 */
1694 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
1695 FILE_UNUSE(wfp, l);
1696 return (EACCES);
1697 }
1698 simple_lock(&fdp->fd_slock);
1699 fdp->fd_ofiles[indx] = wfp;
1700 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1701 simple_unlock(&fdp->fd_slock);
1702 simple_lock(&wfp->f_slock);
1703 wfp->f_count++;
1704 /* 'indx' has been fd_used'ed by caller */
1705 FILE_UNUSE_HAVELOCK(wfp, l);
1706 return (0);
1707
1708 case EMOVEFD:
1709 /*
1710 * Steal away the file pointer from dfd, and stuff it into indx.
1711 */
1712 simple_lock(&fdp->fd_slock);
1713 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
1714 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1715 fdp->fd_ofiles[dfd] = NULL;
1716 fdp->fd_ofileflags[dfd] = 0;
1717 /*
1718 * Complete the clean up of the filedesc structure by
1719 * recomputing the various hints.
1720 */
1721 /* 'indx' has been fd_used'ed by caller */
1722 fd_unused(fdp, dfd);
1723 simple_unlock(&fdp->fd_slock);
1724 FILE_UNUSE(wfp, l);
1725 return (0);
1726
1727 default:
1728 FILE_UNUSE(wfp, l);
1729 return (error);
1730 }
1731 /* NOTREACHED */
1732 }
1733
1734 /*
1735 * Close any files on exec?
1736 */
1737 void
1738 fdcloseexec(struct lwp *l)
1739 {
1740 struct proc *p = l->l_proc;
1741 struct filedesc *fdp;
1742 int fd;
1743
1744 fdunshare(l);
1745 cwdunshare(p);
1746
1747 fdp = p->p_fd;
1748 for (fd = 0; fd <= fdp->fd_lastfile; fd++)
1749 if (fdp->fd_ofileflags[fd] & UF_EXCLOSE)
1750 (void) fdrelease(l, fd);
1751 }
1752
1753 /*
1754 * It is unsafe for set[ug]id processes to be started with file
1755 * descriptors 0..2 closed, as these descriptors are given implicit
1756 * significance in the Standard C library. fdcheckstd() will create a
1757 * descriptor referencing /dev/null for each of stdin, stdout, and
1758 * stderr that is not already open.
1759 */
1760 #define CHECK_UPTO 3
1761 int
1762 fdcheckstd(l)
1763 struct lwp *l;
1764 {
1765 struct proc *p;
1766 struct nameidata nd;
1767 struct filedesc *fdp;
1768 struct file *fp;
1769 struct file *devnullfp = NULL; /* Quell compiler warning */
1770 struct proc *pp;
1771 register_t retval;
1772 int fd, i, error, flags = FREAD|FWRITE, devnull = -1;
1773 char closed[CHECK_UPTO * 3 + 1], which[3 + 1];
1774
1775 p = l->l_proc;
1776 closed[0] = '\0';
1777 if ((fdp = p->p_fd) == NULL)
1778 return (0);
1779 for (i = 0; i < CHECK_UPTO; i++) {
1780 if (fdp->fd_ofiles[i] != NULL)
1781 continue;
1782 snprintf(which, sizeof(which), ",%d", i);
1783 strlcat(closed, which, sizeof(closed));
1784 if (devnullfp == NULL) {
1785 if ((error = falloc(l, &fp, &fd)) != 0)
1786 return (error);
1787 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/null",
1788 l);
1789 if ((error = vn_open(&nd, flags, 0)) != 0) {
1790 FILE_UNUSE(fp, l);
1791 ffree(fp);
1792 fdremove(p->p_fd, fd);
1793 return (error);
1794 }
1795 fp->f_data = nd.ni_vp;
1796 fp->f_flag = flags;
1797 fp->f_ops = &vnops;
1798 fp->f_type = DTYPE_VNODE;
1799 VOP_UNLOCK(nd.ni_vp, 0);
1800 devnull = fd;
1801 devnullfp = fp;
1802 FILE_SET_MATURE(fp);
1803 } else {
1804 restart:
1805 if ((error = fdalloc(p, 0, &fd)) != 0) {
1806 if (error == ENOSPC) {
1807 fdexpand(p);
1808 goto restart;
1809 }
1810 return (error);
1811 }
1812
1813 simple_lock(&devnullfp->f_slock);
1814 FILE_USE(devnullfp);
1815 /* finishdup() will unuse the descriptors for us */
1816 if ((error = finishdup(l, devnull, fd, &retval)) != 0)
1817 return (error);
1818 }
1819 }
1820 if (devnullfp)
1821 FILE_UNUSE(devnullfp, l);
1822 if (closed[0] != '\0') {
1823 pp = p->p_pptr;
1824 log(LOG_WARNING, "set{u,g}id pid %d (%s) "
1825 "was invoked by uid %d ppid %d (%s) "
1826 "with fd %s closed\n",
1827 p->p_pid, p->p_comm, kauth_cred_geteuid(pp->p_cred),
1828 pp->p_pid, pp->p_comm, &closed[1]);
1829 }
1830 return (0);
1831 }
1832 #undef CHECK_UPTO
1833
1834 /*
1835 * Sets descriptor owner. If the owner is a process, 'pgid'
1836 * is set to positive value, process ID. If the owner is process group,
1837 * 'pgid' is set to -pg_id.
1838 */
1839 int
1840 fsetown(struct proc *p, pid_t *pgid, int cmd, const void *data)
1841 {
1842 int id = *(const int *)data;
1843 int error;
1844
1845 switch (cmd) {
1846 case TIOCSPGRP:
1847 if (id < 0)
1848 return (EINVAL);
1849 id = -id;
1850 break;
1851 default:
1852 break;
1853 }
1854
1855 if (id > 0 && !pfind(id))
1856 return (ESRCH);
1857 else if (id < 0 && (error = pgid_in_session(p, -id)))
1858 return (error);
1859
1860 *pgid = id;
1861 return (0);
1862 }
1863
1864 /*
1865 * Return descriptor owner information. If the value is positive,
1866 * it's process ID. If it's negative, it's process group ID and
1867 * needs the sign removed before use.
1868 */
1869 int
1870 fgetown(struct proc *p, pid_t pgid, int cmd, void *data)
1871 {
1872 switch (cmd) {
1873 case TIOCGPGRP:
1874 *(int *)data = -pgid;
1875 break;
1876 default:
1877 *(int *)data = pgid;
1878 break;
1879 }
1880 return (0);
1881 }
1882
1883 /*
1884 * Send signal to descriptor owner, either process or process group.
1885 */
1886 void
1887 fownsignal(pid_t pgid, int signo, int code, int band, void *fdescdata)
1888 {
1889 struct proc *p1;
1890 ksiginfo_t ksi;
1891
1892 KSI_INIT(&ksi);
1893 ksi.ksi_signo = signo;
1894 ksi.ksi_code = code;
1895 ksi.ksi_band = band;
1896
1897 if (pgid > 0 && (p1 = pfind(pgid)))
1898 kpsignal(p1, &ksi, fdescdata);
1899 else if (pgid < 0)
1900 kgsignal(-pgid, &ksi, fdescdata);
1901 }
1902
1903 int
1904 fdclone(struct lwp *l, struct file *fp, int fd, int flag,
1905 const struct fileops *fops, void *data)
1906 {
1907 fp->f_flag = flag;
1908 fp->f_type = DTYPE_MISC;
1909 fp->f_ops = fops;
1910 fp->f_data = data;
1911
1912 l->l_dupfd = fd;
1913
1914 FILE_SET_MATURE(fp);
1915 FILE_UNUSE(fp, l);
1916 return EMOVEFD;
1917 }
1918
1919 /* ARGSUSED */
1920 int
1921 fnullop_fcntl(struct file *fp, u_int cmd, void *data, struct lwp *l)
1922 {
1923
1924 if (cmd == F_SETFL)
1925 return 0;
1926
1927 return EOPNOTSUPP;
1928 }
1929
1930 /* ARGSUSED */
1931 int
1932 fnullop_poll(struct file *fp, int which, struct lwp *l)
1933 {
1934
1935 return 0;
1936 }
1937
1938
1939 /* ARGSUSED */
1940 int
1941 fnullop_kqfilter(struct file *fp, struct knote *kn)
1942 {
1943
1944 return 0;
1945 }
1946
1947 /* ARGSUSED */
1948 int
1949 fbadop_stat(struct file *fp, struct stat *sb, struct lwp *l)
1950 {
1951
1952 return EOPNOTSUPP;
1953 }
1954