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