Deleted Added
full compact
kern_descrip.c (130344) kern_descrip.c (130387)
1/*
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94
35 */
36
37#include <sys/cdefs.h>
1/*
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/sys/kern/kern_descrip.c 130344 2004-06-11 11:16:26Z phk $");
38__FBSDID("$FreeBSD: head/sys/kern/kern_descrip.c 130387 2004-06-12 20:47:32Z rwatson $");
39
40#include "opt_compat.h"
41
42#include <sys/param.h>
43#include <sys/limits.h>
44#include <sys/systm.h>
45#include <sys/syscallsubr.h>
46#include <sys/sysproto.h>
47#include <sys/conf.h>
48#include <sys/filedesc.h>
49#include <sys/lock.h>
50#include <sys/kernel.h>
51#include <sys/limits.h>
52#include <sys/malloc.h>
53#include <sys/mutex.h>
54#include <sys/sysctl.h>
55#include <sys/vnode.h>
56#include <sys/mount.h>
57#include <sys/proc.h>
58#include <sys/namei.h>
59#include <sys/file.h>
60#include <sys/stat.h>
61#include <sys/filio.h>
62#include <sys/fcntl.h>
63#include <sys/unistd.h>
64#include <sys/resourcevar.h>
65#include <sys/event.h>
66#include <sys/sx.h>
67#include <sys/socketvar.h>
68#include <sys/signalvar.h>
69
70#include <vm/vm.h>
71#include <vm/vm_extern.h>
72#include <vm/uma.h>
73
74static MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
75static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "file desc to leader",
76 "file desc to leader structures");
77static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
78
79static uma_zone_t file_zone;
80
81static d_open_t fdopen;
82#define NUMFDESC 64
83
84#define CDEV_MAJOR 22
85static struct cdevsw fildesc_cdevsw = {
86 .d_version = D_VERSION,
87 .d_flags = D_NEEDGIANT,
88 .d_open = fdopen,
89 .d_name = "FD",
90 .d_maj = CDEV_MAJOR,
91};
92
93/* How to treat 'new' parameter when allocating a fd for do_dup(). */
94enum dup_type { DUP_VARIABLE, DUP_FIXED };
95
96static int do_dup(struct thread *td, enum dup_type type, int old, int new,
97 register_t *retval);
98static int fd_first_free(struct filedesc *, int, int);
99static int fd_last_used(struct filedesc *, int, int);
100static void fdgrowtable(struct filedesc *, int);
101
102/*
103 * Descriptor management.
104 */
105struct filelist filehead; /* head of list of open files */
106int nfiles; /* actual number of open files */
107struct sx filelist_lock; /* sx to protect filelist */
108struct mtx sigio_lock; /* mtx to protect pointers to sigio */
109
110/*
111 * Find the first zero bit in the given bitmap, starting at low and not
112 * exceeding size - 1.
113 */
114static int
115fd_first_free(struct filedesc *fdp, int low, int size)
116{
117 NDSLOTTYPE *map = fdp->fd_map;
118 NDSLOTTYPE mask;
119 int off, maxoff;
120
121 if (low >= size)
122 return (low);
123
124 off = NDSLOT(low);
125 if (low % NDENTRIES) {
126 mask = ~(~(NDSLOTTYPE)0 >> (NDENTRIES - (low % NDENTRIES)));
127 if ((mask &= ~map[off]) != 0UL)
128 return (off * NDENTRIES + ffsl(mask) - 1);
129 ++off;
130 }
131 for (maxoff = NDSLOTS(size); off < maxoff; ++off)
132 if (map[off] != ~0UL)
133 return (off * NDENTRIES + ffsl(~map[off]) - 1);
134 return (size);
135}
136
137/*
138 * Find the highest non-zero bit in the given bitmap, starting at low and
139 * not exceeding size - 1.
140 */
141static int
142fd_last_used(struct filedesc *fdp, int low, int size)
143{
144 NDSLOTTYPE *map = fdp->fd_map;
145 NDSLOTTYPE mask;
146 int off, minoff;
147
148 if (low >= size)
149 return (-1);
150
151 off = NDSLOT(size);
152 if (size % NDENTRIES) {
153 mask = ~(~(NDSLOTTYPE)0 << (size % NDENTRIES));
154 if ((mask &= map[off]) != 0)
155 return (off * NDENTRIES + flsl(mask) - 1);
156 --off;
157 }
158 for (minoff = NDSLOT(low); off >= minoff; --off)
159 if (map[off] != 0)
160 return (off * NDENTRIES + flsl(map[off]) - 1);
161 return (size - 1);
162}
163
164static int
165fdisused(struct filedesc *fdp, int fd)
166{
167 KASSERT(fd >= 0 && fd < fdp->fd_nfiles,
168 ("file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles));
169 return ((fdp->fd_map[NDSLOT(fd)] & NDBIT(fd)) != 0);
170}
171
172/*
173 * Mark a file descriptor as used.
174 */
175void
176fdused(struct filedesc *fdp, int fd)
177{
178 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
179 KASSERT(!fdisused(fdp, fd),
180 ("fd already used"));
181 fdp->fd_map[NDSLOT(fd)] |= NDBIT(fd);
182 if (fd > fdp->fd_lastfile)
183 fdp->fd_lastfile = fd;
184 if (fd == fdp->fd_freefile)
185 fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles);
186}
187
188/*
189 * Mark a file descriptor as unused.
190 */
191void
192fdunused(struct filedesc *fdp, int fd)
193{
194 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
195 KASSERT(fdisused(fdp, fd),
196 ("fd is already unused"));
197 KASSERT(fdp->fd_ofiles[fd] == NULL,
198 ("fd is still in use"));
199 fdp->fd_map[NDSLOT(fd)] &= ~NDBIT(fd);
200 if (fd < fdp->fd_freefile)
201 fdp->fd_freefile = fd;
202 if (fd == fdp->fd_lastfile)
203 fdp->fd_lastfile = fd_last_used(fdp, 0, fd);
204}
205
206/*
207 * System calls on descriptors.
208 */
209#ifndef _SYS_SYSPROTO_H_
210struct getdtablesize_args {
211 int dummy;
212};
213#endif
214/*
215 * MPSAFE
216 */
217/* ARGSUSED */
218int
219getdtablesize(td, uap)
220 struct thread *td;
221 struct getdtablesize_args *uap;
222{
223 struct proc *p = td->td_proc;
224
225 PROC_LOCK(p);
226 td->td_retval[0] =
227 min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
228 PROC_UNLOCK(p);
229 return (0);
230}
231
232/*
233 * Duplicate a file descriptor to a particular value.
234 *
235 * note: keep in mind that a potential race condition exists when closing
236 * descriptors from a shared descriptor table (via rfork).
237 */
238#ifndef _SYS_SYSPROTO_H_
239struct dup2_args {
240 u_int from;
241 u_int to;
242};
243#endif
244/*
245 * MPSAFE
246 */
247/* ARGSUSED */
248int
249dup2(td, uap)
250 struct thread *td;
251 struct dup2_args *uap;
252{
253
254 return (do_dup(td, DUP_FIXED, (int)uap->from, (int)uap->to,
255 td->td_retval));
256}
257
258/*
259 * Duplicate a file descriptor.
260 */
261#ifndef _SYS_SYSPROTO_H_
262struct dup_args {
263 u_int fd;
264};
265#endif
266/*
267 * MPSAFE
268 */
269/* ARGSUSED */
270int
271dup(td, uap)
272 struct thread *td;
273 struct dup_args *uap;
274{
275
276 return (do_dup(td, DUP_VARIABLE, (int)uap->fd, 0, td->td_retval));
277}
278
279/*
280 * The file control system call.
281 */
282#ifndef _SYS_SYSPROTO_H_
283struct fcntl_args {
284 int fd;
285 int cmd;
286 long arg;
287};
288#endif
289/*
290 * MPSAFE
291 */
292/* ARGSUSED */
293int
294fcntl(td, uap)
295 struct thread *td;
296 struct fcntl_args *uap;
297{
298 struct flock fl;
299 intptr_t arg;
300 int error;
301
302 error = 0;
303 switch (uap->cmd) {
304 case F_GETLK:
305 case F_SETLK:
306 case F_SETLKW:
307 error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl));
308 arg = (intptr_t)&fl;
309 break;
310 default:
311 arg = uap->arg;
312 break;
313 }
314 if (error)
315 return (error);
316 error = kern_fcntl(td, uap->fd, uap->cmd, arg);
317 if (error)
318 return (error);
319 if (uap->cmd == F_GETLK)
320 error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl));
321 return (error);
322}
323
324int
325kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg)
326{
327 struct filedesc *fdp;
328 struct flock *flp;
329 struct file *fp;
330 struct proc *p;
331 char *pop;
332 struct vnode *vp;
333 u_int newmin;
334 int error, flg, tmp;
335
336 error = 0;
337 flg = F_POSIX;
338 p = td->td_proc;
339 fdp = p->p_fd;
340 mtx_lock(&Giant);
341 FILEDESC_LOCK(fdp);
342 if ((unsigned)fd >= fdp->fd_nfiles ||
343 (fp = fdp->fd_ofiles[fd]) == NULL) {
344 FILEDESC_UNLOCK(fdp);
345 error = EBADF;
346 goto done2;
347 }
348 pop = &fdp->fd_ofileflags[fd];
349
350 switch (cmd) {
351 case F_DUPFD:
352 FILEDESC_UNLOCK(fdp);
353 newmin = arg;
354 PROC_LOCK(p);
355 if (newmin >= lim_cur(p, RLIMIT_NOFILE) ||
356 newmin >= maxfilesperproc) {
357 PROC_UNLOCK(p);
358 error = EINVAL;
359 break;
360 }
361 PROC_UNLOCK(p);
362 error = do_dup(td, DUP_VARIABLE, fd, newmin, td->td_retval);
363 break;
364
365 case F_GETFD:
366 td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
367 FILEDESC_UNLOCK(fdp);
368 break;
369
370 case F_SETFD:
371 *pop = (*pop &~ UF_EXCLOSE) |
372 (arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
373 FILEDESC_UNLOCK(fdp);
374 break;
375
376 case F_GETFL:
377 FILE_LOCK(fp);
378 FILEDESC_UNLOCK(fdp);
379 td->td_retval[0] = OFLAGS(fp->f_flag);
380 FILE_UNLOCK(fp);
381 break;
382
383 case F_SETFL:
384 FILE_LOCK(fp);
385 FILEDESC_UNLOCK(fdp);
386 fhold_locked(fp);
387 fp->f_flag &= ~FCNTLFLAGS;
388 fp->f_flag |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
389 FILE_UNLOCK(fp);
390 tmp = fp->f_flag & FNONBLOCK;
391 error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
392 if (error) {
393 fdrop(fp, td);
394 break;
395 }
396 tmp = fp->f_flag & FASYNC;
397 error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
398 if (error == 0) {
399 fdrop(fp, td);
400 break;
401 }
402 FILE_LOCK(fp);
403 fp->f_flag &= ~FNONBLOCK;
404 FILE_UNLOCK(fp);
405 tmp = 0;
406 (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
407 fdrop(fp, td);
408 break;
409
410 case F_GETOWN:
411 fhold(fp);
412 FILEDESC_UNLOCK(fdp);
413 error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td);
414 if (error == 0)
415 td->td_retval[0] = tmp;
416 fdrop(fp, td);
417 break;
418
419 case F_SETOWN:
420 fhold(fp);
421 FILEDESC_UNLOCK(fdp);
422 tmp = arg;
423 error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td);
424 fdrop(fp, td);
425 break;
426
427 case F_SETLKW:
428 flg |= F_WAIT;
429 /* FALLTHROUGH F_SETLK */
430
431 case F_SETLK:
432 if (fp->f_type != DTYPE_VNODE) {
433 FILEDESC_UNLOCK(fdp);
434 error = EBADF;
435 break;
436 }
437
438 flp = (struct flock *)arg;
439 if (flp->l_whence == SEEK_CUR) {
440 if (fp->f_offset < 0 ||
441 (flp->l_start > 0 &&
442 fp->f_offset > OFF_MAX - flp->l_start)) {
443 FILEDESC_UNLOCK(fdp);
444 error = EOVERFLOW;
445 break;
446 }
447 flp->l_start += fp->f_offset;
448 }
449
450 /*
451 * VOP_ADVLOCK() may block.
452 */
453 fhold(fp);
454 FILEDESC_UNLOCK(fdp);
455 vp = fp->f_vnode;
456
457 switch (flp->l_type) {
458 case F_RDLCK:
459 if ((fp->f_flag & FREAD) == 0) {
460 error = EBADF;
461 break;
462 }
463 PROC_LOCK(p->p_leader);
464 p->p_leader->p_flag |= P_ADVLOCK;
465 PROC_UNLOCK(p->p_leader);
466 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
467 flp, flg);
468 break;
469 case F_WRLCK:
470 if ((fp->f_flag & FWRITE) == 0) {
471 error = EBADF;
472 break;
473 }
474 PROC_LOCK(p->p_leader);
475 p->p_leader->p_flag |= P_ADVLOCK;
476 PROC_UNLOCK(p->p_leader);
477 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
478 flp, flg);
479 break;
480 case F_UNLCK:
481 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
482 flp, F_POSIX);
483 break;
484 default:
485 error = EINVAL;
486 break;
487 }
488 /* Check for race with close */
489 FILEDESC_LOCK(fdp);
490 if ((unsigned) fd >= fdp->fd_nfiles ||
491 fp != fdp->fd_ofiles[fd]) {
492 FILEDESC_UNLOCK(fdp);
493 flp->l_whence = SEEK_SET;
494 flp->l_start = 0;
495 flp->l_len = 0;
496 flp->l_type = F_UNLCK;
497 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
498 F_UNLCK, flp, F_POSIX);
499 } else
500 FILEDESC_UNLOCK(fdp);
501 fdrop(fp, td);
502 break;
503
504 case F_GETLK:
505 if (fp->f_type != DTYPE_VNODE) {
506 FILEDESC_UNLOCK(fdp);
507 error = EBADF;
508 break;
509 }
510 flp = (struct flock *)arg;
511 if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK &&
512 flp->l_type != F_UNLCK) {
513 FILEDESC_UNLOCK(fdp);
514 error = EINVAL;
515 break;
516 }
517 if (flp->l_whence == SEEK_CUR) {
518 if ((flp->l_start > 0 &&
519 fp->f_offset > OFF_MAX - flp->l_start) ||
520 (flp->l_start < 0 &&
521 fp->f_offset < OFF_MIN - flp->l_start)) {
522 FILEDESC_UNLOCK(fdp);
523 error = EOVERFLOW;
524 break;
525 }
526 flp->l_start += fp->f_offset;
527 }
528 /*
529 * VOP_ADVLOCK() may block.
530 */
531 fhold(fp);
532 FILEDESC_UNLOCK(fdp);
533 vp = fp->f_vnode;
534 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp,
535 F_POSIX);
536 fdrop(fp, td);
537 break;
538 default:
539 FILEDESC_UNLOCK(fdp);
540 error = EINVAL;
541 break;
542 }
543done2:
544 mtx_unlock(&Giant);
545 return (error);
546}
547
548/*
549 * Common code for dup, dup2, and fcntl(F_DUPFD).
550 */
551static int
552do_dup(td, type, old, new, retval)
553 enum dup_type type;
554 int old, new;
555 register_t *retval;
556 struct thread *td;
557{
558 struct filedesc *fdp;
559 struct proc *p;
560 struct file *fp;
561 struct file *delfp;
562 int error, holdleaders, maxfd;
563
564 KASSERT((type == DUP_VARIABLE || type == DUP_FIXED),
565 ("invalid dup type %d", type));
566
567 p = td->td_proc;
568 fdp = p->p_fd;
569
570 /*
571 * Verify we have a valid descriptor to dup from and possibly to
572 * dup to.
573 */
574 if (old < 0 || new < 0)
575 return (EBADF);
576 PROC_LOCK(p);
577 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
578 PROC_UNLOCK(p);
579 if (new >= maxfd)
580 return (EMFILE);
581
582 FILEDESC_LOCK(fdp);
583 if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) {
584 FILEDESC_UNLOCK(fdp);
585 return (EBADF);
586 }
587 if (type == DUP_FIXED && old == new) {
588 *retval = new;
589 FILEDESC_UNLOCK(fdp);
590 return (0);
591 }
592 fp = fdp->fd_ofiles[old];
593 fhold(fp);
594
595 /*
596 * If the caller specified a file descriptor, make sure the file
597 * table is large enough to hold it, and grab it. Otherwise, just
598 * allocate a new descriptor the usual way. Since the filedesc
599 * lock may be temporarily dropped in the process, we have to look
600 * out for a race.
601 */
602 if (type == DUP_FIXED) {
603 if (new >= fdp->fd_nfiles)
604 fdgrowtable(fdp, new + 1);
605 if (fdp->fd_ofiles[new] == NULL)
606 fdused(fdp, new);
607 } else {
608 if ((error = fdalloc(td, new, &new)) != 0) {
609 FILEDESC_UNLOCK(fdp);
610 fdrop(fp, td);
611 return (error);
612 }
613 }
614
615 /*
616 * If the old file changed out from under us then treat it as a
617 * bad file descriptor. Userland should do its own locking to
618 * avoid this case.
619 */
620 if (fdp->fd_ofiles[old] != fp) {
621 /* we've allocated a descriptor which we won't use */
622 if (fdp->fd_ofiles[new] == NULL)
623 fdunused(fdp, new);
624 FILEDESC_UNLOCK(fdp);
625 fdrop(fp, td);
626 return (EBADF);
627 }
628 KASSERT(old != new,
629 ("new fd is same as old"));
630
631 /*
632 * Save info on the descriptor being overwritten. We cannot close
633 * it without introducing an ownership race for the slot, since we
634 * need to drop the filedesc lock to call closef().
635 *
636 * XXX this duplicates parts of close().
637 */
638 delfp = fdp->fd_ofiles[new];
639 holdleaders = 0;
640 if (delfp != NULL) {
641 if (td->td_proc->p_fdtol != NULL) {
642 /*
643 * Ask fdfree() to sleep to ensure that all relevant
644 * process leaders can be traversed in closef().
645 */
646 fdp->fd_holdleaderscount++;
647 holdleaders = 1;
648 }
649 }
650
651 /*
652 * Duplicate the source descriptor
653 */
654 fdp->fd_ofiles[new] = fp;
655 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
656 if (new > fdp->fd_lastfile)
657 fdp->fd_lastfile = new;
658 FILEDESC_UNLOCK(fdp);
659 *retval = new;
660
661 /*
662 * If we dup'd over a valid file, we now own the reference to it
663 * and must dispose of it using closef() semantics (as if a
664 * close() were performed on it).
665 *
666 * XXX this duplicates parts of close().
667 */
668 if (delfp != NULL) {
669 /* XXX need to call knote_fdclose() */
670 mtx_lock(&Giant);
671 (void) closef(delfp, td);
672 mtx_unlock(&Giant);
673 if (holdleaders) {
674 FILEDESC_LOCK(fdp);
675 fdp->fd_holdleaderscount--;
676 if (fdp->fd_holdleaderscount == 0 &&
677 fdp->fd_holdleaderswakeup != 0) {
678 fdp->fd_holdleaderswakeup = 0;
679 wakeup(&fdp->fd_holdleaderscount);
680 }
681 FILEDESC_UNLOCK(fdp);
682 }
683 }
684 return (0);
685}
686
687/*
688 * If sigio is on the list associated with a process or process group,
689 * disable signalling from the device, remove sigio from the list and
690 * free sigio.
691 */
692void
693funsetown(sigiop)
694 struct sigio **sigiop;
695{
696 struct sigio *sigio;
697
698 SIGIO_LOCK();
699 sigio = *sigiop;
700 if (sigio == NULL) {
701 SIGIO_UNLOCK();
702 return;
703 }
704 *(sigio->sio_myref) = NULL;
705 if ((sigio)->sio_pgid < 0) {
706 struct pgrp *pg = (sigio)->sio_pgrp;
707 PGRP_LOCK(pg);
708 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
709 sigio, sio_pgsigio);
710 PGRP_UNLOCK(pg);
711 } else {
712 struct proc *p = (sigio)->sio_proc;
713 PROC_LOCK(p);
714 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
715 sigio, sio_pgsigio);
716 PROC_UNLOCK(p);
717 }
718 SIGIO_UNLOCK();
719 crfree(sigio->sio_ucred);
720 FREE(sigio, M_SIGIO);
721}
722
723/*
724 * Free a list of sigio structures.
725 * We only need to lock the SIGIO_LOCK because we have made ourselves
726 * inaccessable to callers of fsetown and therefore do not need to lock
727 * the proc or pgrp struct for the list manipulation.
728 */
729void
730funsetownlst(sigiolst)
731 struct sigiolst *sigiolst;
732{
733 struct proc *p;
734 struct pgrp *pg;
735 struct sigio *sigio;
736
737 sigio = SLIST_FIRST(sigiolst);
738 if (sigio == NULL)
739 return;
740 p = NULL;
741 pg = NULL;
742
743 /*
744 * Every entry of the list should belong
745 * to a single proc or pgrp.
746 */
747 if (sigio->sio_pgid < 0) {
748 pg = sigio->sio_pgrp;
749 PGRP_LOCK_ASSERT(pg, MA_NOTOWNED);
750 } else /* if (sigio->sio_pgid > 0) */ {
751 p = sigio->sio_proc;
752 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
753 }
754
755 SIGIO_LOCK();
756 while ((sigio = SLIST_FIRST(sigiolst)) != NULL) {
757 *(sigio->sio_myref) = NULL;
758 if (pg != NULL) {
759 KASSERT(sigio->sio_pgid < 0,
760 ("Proc sigio in pgrp sigio list"));
761 KASSERT(sigio->sio_pgrp == pg,
762 ("Bogus pgrp in sigio list"));
763 PGRP_LOCK(pg);
764 SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio,
765 sio_pgsigio);
766 PGRP_UNLOCK(pg);
767 } else /* if (p != NULL) */ {
768 KASSERT(sigio->sio_pgid > 0,
769 ("Pgrp sigio in proc sigio list"));
770 KASSERT(sigio->sio_proc == p,
771 ("Bogus proc in sigio list"));
772 PROC_LOCK(p);
773 SLIST_REMOVE(&p->p_sigiolst, sigio, sigio,
774 sio_pgsigio);
775 PROC_UNLOCK(p);
776 }
777 SIGIO_UNLOCK();
778 crfree(sigio->sio_ucred);
779 FREE(sigio, M_SIGIO);
780 SIGIO_LOCK();
781 }
782 SIGIO_UNLOCK();
783}
784
785/*
786 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
787 *
788 * After permission checking, add a sigio structure to the sigio list for
789 * the process or process group.
790 */
791int
792fsetown(pgid, sigiop)
793 pid_t pgid;
794 struct sigio **sigiop;
795{
796 struct proc *proc;
797 struct pgrp *pgrp;
798 struct sigio *sigio;
799 int ret;
800
801 if (pgid == 0) {
802 funsetown(sigiop);
803 return (0);
804 }
805
806 ret = 0;
807
808 /* Allocate and fill in the new sigio out of locks. */
809 MALLOC(sigio, struct sigio *, sizeof(struct sigio), M_SIGIO, M_WAITOK);
810 sigio->sio_pgid = pgid;
811 sigio->sio_ucred = crhold(curthread->td_ucred);
812 sigio->sio_myref = sigiop;
813
814 sx_slock(&proctree_lock);
815 if (pgid > 0) {
816 proc = pfind(pgid);
817 if (proc == NULL) {
818 ret = ESRCH;
819 goto fail;
820 }
821
822 /*
823 * Policy - Don't allow a process to FSETOWN a process
824 * in another session.
825 *
826 * Remove this test to allow maximum flexibility or
827 * restrict FSETOWN to the current process or process
828 * group for maximum safety.
829 */
830 PROC_UNLOCK(proc);
831 if (proc->p_session != curthread->td_proc->p_session) {
832 ret = EPERM;
833 goto fail;
834 }
835
836 pgrp = NULL;
837 } else /* if (pgid < 0) */ {
838 pgrp = pgfind(-pgid);
839 if (pgrp == NULL) {
840 ret = ESRCH;
841 goto fail;
842 }
843 PGRP_UNLOCK(pgrp);
844
845 /*
846 * Policy - Don't allow a process to FSETOWN a process
847 * in another session.
848 *
849 * Remove this test to allow maximum flexibility or
850 * restrict FSETOWN to the current process or process
851 * group for maximum safety.
852 */
853 if (pgrp->pg_session != curthread->td_proc->p_session) {
854 ret = EPERM;
855 goto fail;
856 }
857
858 proc = NULL;
859 }
860 funsetown(sigiop);
861 if (pgid > 0) {
862 PROC_LOCK(proc);
863 /*
864 * Since funsetownlst() is called without the proctree
865 * locked, we need to check for P_WEXIT.
866 * XXX: is ESRCH correct?
867 */
868 if ((proc->p_flag & P_WEXIT) != 0) {
869 PROC_UNLOCK(proc);
870 ret = ESRCH;
871 goto fail;
872 }
873 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
874 sigio->sio_proc = proc;
875 PROC_UNLOCK(proc);
876 } else {
877 PGRP_LOCK(pgrp);
878 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
879 sigio->sio_pgrp = pgrp;
880 PGRP_UNLOCK(pgrp);
881 }
882 sx_sunlock(&proctree_lock);
883 SIGIO_LOCK();
884 *sigiop = sigio;
885 SIGIO_UNLOCK();
886 return (0);
887
888fail:
889 sx_sunlock(&proctree_lock);
890 crfree(sigio->sio_ucred);
891 FREE(sigio, M_SIGIO);
892 return (ret);
893}
894
895/*
896 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
897 */
898pid_t
899fgetown(sigiop)
900 struct sigio **sigiop;
901{
902 pid_t pgid;
903
904 SIGIO_LOCK();
905 pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0;
906 SIGIO_UNLOCK();
907 return (pgid);
908}
909
910/*
911 * Close a file descriptor.
912 */
913#ifndef _SYS_SYSPROTO_H_
914struct close_args {
915 int fd;
916};
917#endif
918/*
919 * MPSAFE
920 */
921/* ARGSUSED */
922int
923close(td, uap)
924 struct thread *td;
925 struct close_args *uap;
926{
927 struct filedesc *fdp;
928 struct file *fp;
929 int fd, error;
930 int holdleaders;
931
932 fd = uap->fd;
933 error = 0;
934 holdleaders = 0;
935 fdp = td->td_proc->p_fd;
936 mtx_lock(&Giant);
937 FILEDESC_LOCK(fdp);
938 if ((unsigned)fd >= fdp->fd_nfiles ||
939 (fp = fdp->fd_ofiles[fd]) == NULL) {
940 FILEDESC_UNLOCK(fdp);
941 mtx_unlock(&Giant);
942 return (EBADF);
943 }
944 fdp->fd_ofiles[fd] = NULL;
945 fdp->fd_ofileflags[fd] = 0;
946 fdunused(fdp, fd);
947 if (td->td_proc->p_fdtol != NULL) {
948 /*
949 * Ask fdfree() to sleep to ensure that all relevant
950 * process leaders can be traversed in closef().
951 */
952 fdp->fd_holdleaderscount++;
953 holdleaders = 1;
954 }
955
956 /*
957 * we now hold the fp reference that used to be owned by the descriptor
958 * array.
959 */
960 if (fd < fdp->fd_knlistsize) {
961 FILEDESC_UNLOCK(fdp);
962 knote_fdclose(td, fd);
963 } else
964 FILEDESC_UNLOCK(fdp);
965
966 error = closef(fp, td);
967 mtx_unlock(&Giant);
968 if (holdleaders) {
969 FILEDESC_LOCK(fdp);
970 fdp->fd_holdleaderscount--;
971 if (fdp->fd_holdleaderscount == 0 &&
972 fdp->fd_holdleaderswakeup != 0) {
973 fdp->fd_holdleaderswakeup = 0;
974 wakeup(&fdp->fd_holdleaderscount);
975 }
976 FILEDESC_UNLOCK(fdp);
977 }
978 return (error);
979}
980
981#if defined(COMPAT_43)
982/*
983 * Return status information about a file descriptor.
984 */
985#ifndef _SYS_SYSPROTO_H_
986struct ofstat_args {
987 int fd;
988 struct ostat *sb;
989};
990#endif
991/*
992 * MPSAFE
993 */
994/* ARGSUSED */
995int
996ofstat(td, uap)
997 struct thread *td;
998 struct ofstat_args *uap;
999{
1000 struct file *fp;
1001 struct stat ub;
1002 struct ostat oub;
1003 int error;
1004
1005 if ((error = fget(td, uap->fd, &fp)) != 0)
1006 goto done2;
1007 mtx_lock(&Giant);
1008 error = fo_stat(fp, &ub, td->td_ucred, td);
1009 mtx_unlock(&Giant);
1010 if (error == 0) {
1011 cvtstat(&ub, &oub);
1012 error = copyout(&oub, uap->sb, sizeof(oub));
1013 }
1014 fdrop(fp, td);
1015done2:
1016 return (error);
1017}
1018#endif /* COMPAT_43 */
1019
1020/*
1021 * Return status information about a file descriptor.
1022 */
1023#ifndef _SYS_SYSPROTO_H_
1024struct fstat_args {
1025 int fd;
1026 struct stat *sb;
1027};
1028#endif
1029/*
1030 * MPSAFE
1031 */
1032/* ARGSUSED */
1033int
1034fstat(td, uap)
1035 struct thread *td;
1036 struct fstat_args *uap;
1037{
1038 struct file *fp;
1039 struct stat ub;
1040 int error;
1041
1042 if ((error = fget(td, uap->fd, &fp)) != 0)
1043 goto done2;
1044 mtx_lock(&Giant);
1045 error = fo_stat(fp, &ub, td->td_ucred, td);
1046 mtx_unlock(&Giant);
1047 if (error == 0)
1048 error = copyout(&ub, uap->sb, sizeof(ub));
1049 fdrop(fp, td);
1050done2:
1051 return (error);
1052}
1053
1054/*
1055 * Return status information about a file descriptor.
1056 */
1057#ifndef _SYS_SYSPROTO_H_
1058struct nfstat_args {
1059 int fd;
1060 struct nstat *sb;
1061};
1062#endif
1063/*
1064 * MPSAFE
1065 */
1066/* ARGSUSED */
1067int
1068nfstat(td, uap)
1069 struct thread *td;
1070 struct nfstat_args *uap;
1071{
1072 struct file *fp;
1073 struct stat ub;
1074 struct nstat nub;
1075 int error;
1076
1077 if ((error = fget(td, uap->fd, &fp)) != 0)
1078 goto done2;
1079 mtx_lock(&Giant);
1080 error = fo_stat(fp, &ub, td->td_ucred, td);
1081 mtx_unlock(&Giant);
1082 if (error == 0) {
1083 cvtnstat(&ub, &nub);
1084 error = copyout(&nub, uap->sb, sizeof(nub));
1085 }
1086 fdrop(fp, td);
1087done2:
1088 return (error);
1089}
1090
1091/*
1092 * Return pathconf information about a file descriptor.
1093 */
1094#ifndef _SYS_SYSPROTO_H_
1095struct fpathconf_args {
1096 int fd;
1097 int name;
1098};
1099#endif
1100/*
1101 * MPSAFE
1102 */
1103/* ARGSUSED */
1104int
1105fpathconf(td, uap)
1106 struct thread *td;
1107 struct fpathconf_args *uap;
1108{
1109 struct file *fp;
1110 struct vnode *vp;
1111 int error;
1112
1113 if ((error = fget(td, uap->fd, &fp)) != 0)
1114 return (error);
1115
1116 /* If asynchronous I/O is available, it works for all descriptors. */
1117 if (uap->name == _PC_ASYNC_IO) {
1118 td->td_retval[0] = async_io_version;
1119 goto out;
1120 }
1121 vp = fp->f_vnode;
1122 if (vp != NULL) {
1123 mtx_lock(&Giant);
1124 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1125 error = VOP_PATHCONF(vp, uap->name, td->td_retval);
1126 VOP_UNLOCK(vp, 0, td);
1127 mtx_unlock(&Giant);
1128 } else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
1129 if (uap->name != _PC_PIPE_BUF) {
1130 error = EINVAL;
1131 } else {
1132 td->td_retval[0] = PIPE_BUF;
1133 error = 0;
1134 }
1135 } else {
1136 error = EOPNOTSUPP;
1137 }
1138out:
1139 fdrop(fp, td);
1140 return (error);
1141}
1142
1143/*
1144 * Grow the file table to accomodate (at least) nfd descriptors. This may
1145 * block and drop the filedesc lock, but it will reacquire it before
1146 * returing.
1147 */
1148static void
1149fdgrowtable(struct filedesc *fdp, int nfd)
1150{
1151 struct file **ntable;
1152 char *nfileflags;
1153 int nnfiles, onfiles;
1154 NDSLOTTYPE *nmap;
1155
1156 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
1157
1158 KASSERT(fdp->fd_nfiles > 0,
1159 ("zero-length file table"));
1160
1161 /* compute the size of the new table */
1162 onfiles = fdp->fd_nfiles;
1163 nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */
1164 if (nnfiles <= onfiles)
1165 /* the table is already large enough */
1166 return;
1167
1168 /* allocate a new table and (if required) new bitmaps */
1169 FILEDESC_UNLOCK(fdp);
1170 MALLOC(ntable, struct file **, nnfiles * OFILESIZE,
1171 M_FILEDESC, M_ZERO | M_WAITOK);
1172 nfileflags = (char *)&ntable[nnfiles];
1173 if (NDSLOTS(nnfiles) > NDSLOTS(onfiles))
1174 MALLOC(nmap, NDSLOTTYPE *, NDSLOTS(nnfiles) * NDSLOTSIZE,
1175 M_FILEDESC, M_ZERO | M_WAITOK);
1176 else
1177 nmap = NULL;
1178 FILEDESC_LOCK(fdp);
1179
1180 /*
1181 * We now have new tables ready to go. Since we dropped the
1182 * filedesc lock to call malloc(), watch out for a race.
1183 */
1184 onfiles = fdp->fd_nfiles;
1185 if (onfiles >= nnfiles) {
1186 /* we lost the race, but that's OK */
1187 free(ntable, M_FILEDESC);
1188 if (nmap != NULL)
1189 free(nmap, M_FILEDESC);
1190 return;
1191 }
1192 bcopy(fdp->fd_ofiles, ntable, onfiles * sizeof(*ntable));
1193 bcopy(fdp->fd_ofileflags, nfileflags, onfiles);
1194 if (onfiles > NDFILE)
1195 free(fdp->fd_ofiles, M_FILEDESC);
1196 fdp->fd_ofiles = ntable;
1197 fdp->fd_ofileflags = nfileflags;
1198 if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) {
1199 bcopy(fdp->fd_map, nmap, NDSLOTS(onfiles) * sizeof(*nmap));
1200 if (NDSLOTS(onfiles) > NDSLOTS(NDFILE))
1201 free(fdp->fd_map, M_FILEDESC);
1202 fdp->fd_map = nmap;
1203 }
1204 fdp->fd_nfiles = nnfiles;
1205}
1206
1207/*
1208 * Allocate a file descriptor for the process.
1209 */
1210int
1211fdalloc(struct thread *td, int minfd, int *result)
1212{
1213 struct proc *p = td->td_proc;
1214 struct filedesc *fdp = p->p_fd;
1215 int fd = -1, maxfd;
1216
1217 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
1218
1219 PROC_LOCK(p);
1220 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1221 PROC_UNLOCK(p);
1222
1223 /*
1224 * Search the bitmap for a free descriptor. If none is found, try
1225 * to grow the file table. Keep at it until we either get a file
1226 * descriptor or run into process or system limits; fdgrowtable()
1227 * may drop the filedesc lock, so we're in a race.
1228 */
1229 for (;;) {
1230 fd = fd_first_free(fdp, minfd, fdp->fd_nfiles);
1231 if (fd >= maxfd)
1232 return (EMFILE);
1233 if (fd < fdp->fd_nfiles)
1234 break;
1235 fdgrowtable(fdp, min(fdp->fd_nfiles * 2, maxfd));
1236 }
1237
1238 /*
1239 * Perform some sanity checks, then mark the file descriptor as
1240 * used and return it to the caller.
1241 */
1242 KASSERT(!fdisused(fdp, fd),
1243 ("fd_first_free() returned non-free descriptor"));
1244 KASSERT(fdp->fd_ofiles[fd] == NULL,
1245 ("free descriptor isn't"));
1246 fdp->fd_ofileflags[fd] = 0; /* XXX needed? */
1247 fdused(fdp, fd);
1248 fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles);
1249 *result = fd;
1250 return (0);
1251}
1252
1253/*
1254 * Check to see whether n user file descriptors
1255 * are available to the process p.
1256 */
1257int
1258fdavail(td, n)
1259 struct thread *td;
1260 int n;
1261{
1262 struct proc *p = td->td_proc;
1263 struct filedesc *fdp = td->td_proc->p_fd;
1264 struct file **fpp;
1265 int i, lim, last;
1266
1267 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
1268
1269 PROC_LOCK(p);
1270 lim = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1271 PROC_UNLOCK(p);
1272 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
1273 return (1);
1274 last = min(fdp->fd_nfiles, lim);
1275 fpp = &fdp->fd_ofiles[fdp->fd_freefile];
1276 for (i = last - fdp->fd_freefile; --i >= 0; fpp++) {
1277 if (*fpp == NULL && --n <= 0)
1278 return (1);
1279 }
1280 return (0);
1281}
1282
1283/*
1284 * Create a new open file structure and allocate
1285 * a file decriptor for the process that refers to it.
1286 * We add one reference to the file for the descriptor table
1287 * and one reference for resultfp. This is to prevent us being
1288 * prempted and the entry in the descriptor table closed after
1289 * we release the FILEDESC lock.
1290 */
1291int
1292falloc(td, resultfp, resultfd)
1293 struct thread *td;
1294 struct file **resultfp;
1295 int *resultfd;
1296{
1297 struct proc *p = td->td_proc;
1298 struct file *fp, *fq;
1299 int error, i;
1300 int maxuserfiles = maxfiles - (maxfiles / 20);
1301 static struct timeval lastfail;
1302 static int curfail;
1303
1304 fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO);
1305 sx_xlock(&filelist_lock);
1306 if ((nfiles >= maxuserfiles && td->td_ucred->cr_ruid != 0)
1307 || nfiles >= maxfiles) {
1308 if (ppsratecheck(&lastfail, &curfail, 1)) {
1309 printf("kern.maxfiles limit exceeded by uid %i, please see tuning(7).\n",
1310 td->td_ucred->cr_ruid);
1311 }
1312 sx_xunlock(&filelist_lock);
1313 uma_zfree(file_zone, fp);
1314 return (ENFILE);
1315 }
1316 nfiles++;
1317
1318 /*
1319 * If the process has file descriptor zero open, add the new file
1320 * descriptor to the list of open files at that point, otherwise
1321 * put it at the front of the list of open files.
1322 */
1323 fp->f_mtxp = mtx_pool_alloc(mtxpool_sleep);
1324 fp->f_count = 1;
1325 if (resultfp)
1326 fp->f_count++;
1327 fp->f_cred = crhold(td->td_ucred);
1328 fp->f_ops = &badfileops;
1329 FILEDESC_LOCK(p->p_fd);
1330 if ((fq = p->p_fd->fd_ofiles[0])) {
1331 LIST_INSERT_AFTER(fq, fp, f_list);
1332 } else {
1333 LIST_INSERT_HEAD(&filehead, fp, f_list);
1334 }
1335 sx_xunlock(&filelist_lock);
1336 if ((error = fdalloc(td, 0, &i))) {
1337 FILEDESC_UNLOCK(p->p_fd);
1338 fdrop(fp, td);
1339 if (resultfp)
1340 fdrop(fp, td);
1341 return (error);
1342 }
1343 p->p_fd->fd_ofiles[i] = fp;
1344 FILEDESC_UNLOCK(p->p_fd);
1345 if (resultfp)
1346 *resultfp = fp;
1347 if (resultfd)
1348 *resultfd = i;
1349 return (0);
1350}
1351
1352/*
1353 * Free a file descriptor.
1354 */
1355void
1356ffree(fp)
1357 struct file *fp;
1358{
1359
1360 KASSERT(fp->f_count == 0, ("ffree: fp_fcount not 0!"));
1361 sx_xlock(&filelist_lock);
1362 LIST_REMOVE(fp, f_list);
1363 nfiles--;
1364 sx_xunlock(&filelist_lock);
1365 crfree(fp->f_cred);
1366 uma_zfree(file_zone, fp);
1367}
1368
1369/*
1370 * Build a new filedesc structure from another.
1371 * Copy the current, root, and jail root vnode references.
1372 */
1373struct filedesc *
1374fdinit(fdp)
1375 struct filedesc *fdp;
1376{
1377 struct filedesc0 *newfdp;
1378
1379 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
1380
1381 FILEDESC_UNLOCK(fdp);
1382 MALLOC(newfdp, struct filedesc0 *, sizeof(struct filedesc0),
1383 M_FILEDESC, M_WAITOK | M_ZERO);
1384 FILEDESC_LOCK(fdp);
1385 mtx_init(&newfdp->fd_fd.fd_mtx, FILEDESC_LOCK_DESC, NULL, MTX_DEF);
1386 newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
1387 if (newfdp->fd_fd.fd_cdir)
1388 VREF(newfdp->fd_fd.fd_cdir);
1389 newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
1390 if (newfdp->fd_fd.fd_rdir)
1391 VREF(newfdp->fd_fd.fd_rdir);
1392 newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
1393 if (newfdp->fd_fd.fd_jdir)
1394 VREF(newfdp->fd_fd.fd_jdir);
1395
1396 /* Create the file descriptor table. */
1397 newfdp->fd_fd.fd_refcnt = 1;
1398 newfdp->fd_fd.fd_cmask = CMASK;
1399 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1400 newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1401 newfdp->fd_fd.fd_nfiles = NDFILE;
1402 newfdp->fd_fd.fd_knlistsize = -1;
1403 newfdp->fd_fd.fd_map = newfdp->fd_dmap;
1404 return (&newfdp->fd_fd);
1405}
1406
1407/*
1408 * Share a filedesc structure.
1409 */
1410struct filedesc *
1411fdshare(fdp)
1412 struct filedesc *fdp;
1413{
1414 FILEDESC_LOCK(fdp);
1415 fdp->fd_refcnt++;
1416 FILEDESC_UNLOCK(fdp);
1417 return (fdp);
1418}
1419
1420/*
1421 * Copy a filedesc structure.
1422 * A NULL pointer in returns a NULL reference, this is to ease callers,
1423 * not catch errors.
1424 */
1425struct filedesc *
1426fdcopy(fdp)
1427 struct filedesc *fdp;
1428{
1429 struct filedesc *newfdp;
1430 int i;
1431
1432 /* Certain daemons might not have file descriptors. */
1433 if (fdp == NULL)
1434 return (NULL);
1435
1436 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
1437 newfdp = fdinit(fdp);
1438 while (fdp->fd_lastfile >= newfdp->fd_nfiles) {
1439 FILEDESC_UNLOCK(fdp);
1440 FILEDESC_LOCK(newfdp);
1441 fdgrowtable(newfdp, fdp->fd_lastfile + 1);
1442 FILEDESC_UNLOCK(newfdp);
1443 FILEDESC_LOCK(fdp);
1444 }
1445 /* copy everything except kqueue descriptors */
1446 newfdp->fd_freefile = -1;
1447 for (i = 0; i <= fdp->fd_lastfile; ++i) {
1448 if (fdisused(fdp, i) &&
1449 fdp->fd_ofiles[i]->f_type != DTYPE_KQUEUE) {
1450 newfdp->fd_ofiles[i] = fdp->fd_ofiles[i];
1451 newfdp->fd_ofileflags[i] = fdp->fd_ofileflags[i];
1452 fhold(newfdp->fd_ofiles[i]);
1453 newfdp->fd_lastfile = i;
1454 } else {
1455 if (newfdp->fd_freefile == -1)
1456 newfdp->fd_freefile = i;
1457 }
1458 }
1459 FILEDESC_UNLOCK(fdp);
1460 FILEDESC_LOCK(newfdp);
1461 for (i = 0; i <= newfdp->fd_lastfile; ++i)
1462 if (newfdp->fd_ofiles[i] != NULL)
1463 fdused(newfdp, i);
1464 FILEDESC_UNLOCK(newfdp);
1465 FILEDESC_LOCK(fdp);
1466 if (newfdp->fd_freefile == -1)
1467 newfdp->fd_freefile = i;
1468 newfdp->fd_cmask = fdp->fd_cmask;
1469 return (newfdp);
1470}
1471
1472/* A mutex to protect the association between a proc and filedesc. */
1473struct mtx fdesc_mtx;
1474MTX_SYSINIT(fdesc, &fdesc_mtx, "fdesc", MTX_DEF);
1475
1476/*
1477 * Release a filedesc structure.
1478 */
1479void
1480fdfree(td)
1481 struct thread *td;
1482{
1483 struct filedesc *fdp;
1484 struct file **fpp;
1485 int i;
1486 struct filedesc_to_leader *fdtol;
1487 struct file *fp;
1488 struct vnode *vp;
1489 struct flock lf;
1490
1491 /* Certain daemons might not have file descriptors. */
1492 fdp = td->td_proc->p_fd;
1493 if (fdp == NULL)
1494 return;
1495
1496 /* Check for special need to clear POSIX style locks */
1497 fdtol = td->td_proc->p_fdtol;
1498 if (fdtol != NULL) {
1499 FILEDESC_LOCK(fdp);
1500 KASSERT(fdtol->fdl_refcount > 0,
1501 ("filedesc_to_refcount botch: fdl_refcount=%d",
1502 fdtol->fdl_refcount));
1503 if (fdtol->fdl_refcount == 1 &&
1504 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1505 i = 0;
1506 fpp = fdp->fd_ofiles;
1507 for (i = 0, fpp = fdp->fd_ofiles;
1508 i <= fdp->fd_lastfile;
1509 i++, fpp++) {
1510 if (*fpp == NULL ||
1511 (*fpp)->f_type != DTYPE_VNODE)
1512 continue;
1513 fp = *fpp;
1514 fhold(fp);
1515 FILEDESC_UNLOCK(fdp);
1516 lf.l_whence = SEEK_SET;
1517 lf.l_start = 0;
1518 lf.l_len = 0;
1519 lf.l_type = F_UNLCK;
1520 vp = fp->f_vnode;
1521 (void) VOP_ADVLOCK(vp,
1522 (caddr_t)td->td_proc->
1523 p_leader,
1524 F_UNLCK,
1525 &lf,
1526 F_POSIX);
1527 FILEDESC_LOCK(fdp);
1528 fdrop(fp, td);
1529 fpp = fdp->fd_ofiles + i;
1530 }
1531 }
1532 retry:
1533 if (fdtol->fdl_refcount == 1) {
1534 if (fdp->fd_holdleaderscount > 0 &&
1535 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1536 /*
1537 * close() or do_dup() has cleared a reference
1538 * in a shared file descriptor table.
1539 */
1540 fdp->fd_holdleaderswakeup = 1;
1541 msleep(&fdp->fd_holdleaderscount, &fdp->fd_mtx,
1542 PLOCK, "fdlhold", 0);
1543 goto retry;
1544 }
1545 if (fdtol->fdl_holdcount > 0) {
1546 /*
1547 * Ensure that fdtol->fdl_leader
1548 * remains valid in closef().
1549 */
1550 fdtol->fdl_wakeup = 1;
1551 msleep(fdtol, &fdp->fd_mtx,
1552 PLOCK, "fdlhold", 0);
1553 goto retry;
1554 }
1555 }
1556 fdtol->fdl_refcount--;
1557 if (fdtol->fdl_refcount == 0 &&
1558 fdtol->fdl_holdcount == 0) {
1559 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
1560 fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
1561 } else
1562 fdtol = NULL;
1563 td->td_proc->p_fdtol = NULL;
1564 FILEDESC_UNLOCK(fdp);
1565 if (fdtol != NULL)
1566 FREE(fdtol, M_FILEDESC_TO_LEADER);
1567 }
1568 FILEDESC_LOCK(fdp);
1569 if (--fdp->fd_refcnt > 0) {
1570 FILEDESC_UNLOCK(fdp);
1571 return;
1572 }
1573
1574 /*
1575 * We are the last reference to the structure, so we can
1576 * safely assume it will not change out from under us.
1577 */
1578 FILEDESC_UNLOCK(fdp);
1579 fpp = fdp->fd_ofiles;
1580 for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
1581 if (*fpp)
1582 (void) closef(*fpp, td);
1583 }
1584
1585 /* XXX This should happen earlier. */
1586 mtx_lock(&fdesc_mtx);
1587 td->td_proc->p_fd = NULL;
1588 mtx_unlock(&fdesc_mtx);
1589
1590 if (fdp->fd_nfiles > NDFILE)
1591 FREE(fdp->fd_ofiles, M_FILEDESC);
1592 if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE))
1593 FREE(fdp->fd_map, M_FILEDESC);
1594 if (fdp->fd_cdir)
1595 vrele(fdp->fd_cdir);
1596 if (fdp->fd_rdir)
1597 vrele(fdp->fd_rdir);
1598 if (fdp->fd_jdir)
1599 vrele(fdp->fd_jdir);
1600 if (fdp->fd_knlist)
1601 FREE(fdp->fd_knlist, M_KQUEUE);
1602 if (fdp->fd_knhash)
1603 FREE(fdp->fd_knhash, M_KQUEUE);
1604 mtx_destroy(&fdp->fd_mtx);
1605 FREE(fdp, M_FILEDESC);
1606}
1607
1608/*
1609 * For setugid programs, we don't want to people to use that setugidness
1610 * to generate error messages which write to a file which otherwise would
1611 * otherwise be off-limits to the process. We check for filesystems where
1612 * the vnode can change out from under us after execve (like [lin]procfs).
1613 *
1614 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
1615 * sufficient. We also don't for check setugidness since we know we are.
1616 */
1617static int
1618is_unsafe(struct file *fp)
1619{
1620 if (fp->f_type == DTYPE_VNODE) {
1621 struct vnode *vp = fp->f_vnode;
1622
1623 if ((vp->v_vflag & VV_PROCDEP) != 0)
1624 return (1);
1625 }
1626 return (0);
1627}
1628
1629/*
1630 * Make this setguid thing safe, if at all possible.
1631 */
1632void
1633setugidsafety(td)
1634 struct thread *td;
1635{
1636 struct filedesc *fdp;
1637 int i;
1638
1639 /* Certain daemons might not have file descriptors. */
1640 fdp = td->td_proc->p_fd;
1641 if (fdp == NULL)
1642 return;
1643
1644 /*
1645 * Note: fdp->fd_ofiles may be reallocated out from under us while
1646 * we are blocked in a close. Be careful!
1647 */
1648 FILEDESC_LOCK(fdp);
1649 for (i = 0; i <= fdp->fd_lastfile; i++) {
1650 if (i > 2)
1651 break;
1652 if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
1653 struct file *fp;
1654
1655 if (i < fdp->fd_knlistsize) {
1656 FILEDESC_UNLOCK(fdp);
1657 knote_fdclose(td, i);
1658 FILEDESC_LOCK(fdp);
1659 }
1660 /*
1661 * NULL-out descriptor prior to close to avoid
1662 * a race while close blocks.
1663 */
1664 fp = fdp->fd_ofiles[i];
1665 fdp->fd_ofiles[i] = NULL;
1666 fdp->fd_ofileflags[i] = 0;
1667 fdunused(fdp, i);
1668 FILEDESC_UNLOCK(fdp);
1669 (void) closef(fp, td);
1670 FILEDESC_LOCK(fdp);
1671 }
1672 }
1673 FILEDESC_UNLOCK(fdp);
1674}
1675
1676/*
1677 * Close any files on exec?
1678 */
1679void
1680fdcloseexec(td)
1681 struct thread *td;
1682{
1683 struct filedesc *fdp;
1684 int i;
1685
1686 /* Certain daemons might not have file descriptors. */
1687 fdp = td->td_proc->p_fd;
1688 if (fdp == NULL)
1689 return;
1690
1691 FILEDESC_LOCK(fdp);
1692
1693 /*
1694 * We cannot cache fd_ofiles or fd_ofileflags since operations
1695 * may block and rip them out from under us.
1696 */
1697 for (i = 0; i <= fdp->fd_lastfile; i++) {
1698 if (fdp->fd_ofiles[i] != NULL &&
1699 (fdp->fd_ofileflags[i] & UF_EXCLOSE)) {
1700 struct file *fp;
1701
1702 if (i < fdp->fd_knlistsize) {
1703 FILEDESC_UNLOCK(fdp);
1704 knote_fdclose(td, i);
1705 FILEDESC_LOCK(fdp);
1706 }
1707 /*
1708 * NULL-out descriptor prior to close to avoid
1709 * a race while close blocks.
1710 */
1711 fp = fdp->fd_ofiles[i];
1712 fdp->fd_ofiles[i] = NULL;
1713 fdp->fd_ofileflags[i] = 0;
1714 fdunused(fdp, i);
1715 FILEDESC_UNLOCK(fdp);
1716 (void) closef(fp, td);
1717 FILEDESC_LOCK(fdp);
1718 }
1719 }
1720 FILEDESC_UNLOCK(fdp);
1721}
1722
1723/*
1724 * It is unsafe for set[ug]id processes to be started with file
1725 * descriptors 0..2 closed, as these descriptors are given implicit
1726 * significance in the Standard C library. fdcheckstd() will create a
1727 * descriptor referencing /dev/null for each of stdin, stdout, and
1728 * stderr that is not already open.
1729 */
1730int
1731fdcheckstd(td)
1732 struct thread *td;
1733{
1734 struct nameidata nd;
1735 struct filedesc *fdp;
1736 struct file *fp;
1737 register_t retval;
1738 int fd, i, error, flags, devnull;
1739
1740 fdp = td->td_proc->p_fd;
1741 if (fdp == NULL)
1742 return (0);
1743 KASSERT(fdp->fd_refcnt == 1, ("the fdtable should not be shared"));
1744 devnull = -1;
1745 error = 0;
1746 for (i = 0; i < 3; i++) {
1747 if (fdp->fd_ofiles[i] != NULL)
1748 continue;
1749 if (devnull < 0) {
1750 error = falloc(td, &fp, &fd);
1751 if (error != 0)
1752 break;
1753 /* Note extra ref on `fp' held for us by falloc(). */
1754 KASSERT(fd == i, ("oof, we didn't get our fd"));
1755 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/null",
1756 td);
1757 flags = FREAD | FWRITE;
1758 error = vn_open(&nd, &flags, 0, -1);
1759 if (error != 0) {
1760 /*
1761 * Someone may have closed the entry in the
1762 * file descriptor table, so check it hasn't
1763 * changed before dropping the reference count.
1764 */
1765 FILEDESC_LOCK(fdp);
1766 KASSERT(fdp->fd_ofiles[fd] == fp,
1767 ("table not shared, how did it change?"));
1768 fdp->fd_ofiles[fd] = NULL;
1769 fdunused(fdp, fd);
1770 FILEDESC_UNLOCK(fdp);
1771 fdrop(fp, td);
1772 fdrop(fp, td);
1773 break;
1774 }
1775 NDFREE(&nd, NDF_ONLY_PNBUF);
1776 fp->f_vnode = nd.ni_vp;
1777 fp->f_data = nd.ni_vp;
1778 fp->f_flag = flags;
1779 fp->f_ops = &vnops;
1780 fp->f_type = DTYPE_VNODE;
1781 VOP_UNLOCK(nd.ni_vp, 0, td);
1782 devnull = fd;
1783 fdrop(fp, td);
1784 } else {
1785 error = do_dup(td, DUP_FIXED, devnull, i, &retval);
1786 if (error != 0)
1787 break;
1788 }
1789 }
1790 return (error);
1791}
1792
1793/*
1794 * Internal form of close.
1795 * Decrement reference count on file structure.
1796 * Note: td may be NULL when closing a file
1797 * that was being passed in a message.
1798 */
1799int
1800closef(fp, td)
1801 struct file *fp;
1802 struct thread *td;
1803{
1804 struct vnode *vp;
1805 struct flock lf;
1806 struct filedesc_to_leader *fdtol;
1807 struct filedesc *fdp;
1808
1809 if (fp == NULL)
1810 return (0);
1811 /*
1812 * POSIX record locking dictates that any close releases ALL
1813 * locks owned by this process. This is handled by setting
1814 * a flag in the unlock to free ONLY locks obeying POSIX
1815 * semantics, and not to free BSD-style file locks.
1816 * If the descriptor was in a message, POSIX-style locks
1817 * aren't passed with the descriptor.
1818 */
1819 if (td != NULL &&
1820 fp->f_type == DTYPE_VNODE) {
1821 if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1822 lf.l_whence = SEEK_SET;
1823 lf.l_start = 0;
1824 lf.l_len = 0;
1825 lf.l_type = F_UNLCK;
1826 vp = fp->f_vnode;
1827 (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader,
1828 F_UNLCK, &lf, F_POSIX);
1829 }
1830 fdtol = td->td_proc->p_fdtol;
1831 if (fdtol != NULL) {
1832 /*
1833 * Handle special case where file descriptor table
1834 * is shared between multiple process leaders.
1835 */
1836 fdp = td->td_proc->p_fd;
1837 FILEDESC_LOCK(fdp);
1838 for (fdtol = fdtol->fdl_next;
1839 fdtol != td->td_proc->p_fdtol;
1840 fdtol = fdtol->fdl_next) {
1841 if ((fdtol->fdl_leader->p_flag &
1842 P_ADVLOCK) == 0)
1843 continue;
1844 fdtol->fdl_holdcount++;
1845 FILEDESC_UNLOCK(fdp);
1846 lf.l_whence = SEEK_SET;
1847 lf.l_start = 0;
1848 lf.l_len = 0;
1849 lf.l_type = F_UNLCK;
1850 vp = fp->f_vnode;
1851 (void) VOP_ADVLOCK(vp,
1852 (caddr_t)fdtol->fdl_leader,
1853 F_UNLCK, &lf, F_POSIX);
1854 FILEDESC_LOCK(fdp);
1855 fdtol->fdl_holdcount--;
1856 if (fdtol->fdl_holdcount == 0 &&
1857 fdtol->fdl_wakeup != 0) {
1858 fdtol->fdl_wakeup = 0;
1859 wakeup(fdtol);
1860 }
1861 }
1862 FILEDESC_UNLOCK(fdp);
1863 }
1864 }
1865 return (fdrop(fp, td));
1866}
1867
1868/*
1869 * Drop reference on struct file passed in, may call closef if the
1870 * reference hits zero.
1871 */
1872int
1873fdrop(fp, td)
1874 struct file *fp;
1875 struct thread *td;
1876{
1877
1878 FILE_LOCK(fp);
1879 return (fdrop_locked(fp, td));
1880}
1881
1882/*
1883 * Extract the file pointer associated with the specified descriptor for
1884 * the current user process.
1885 *
1886 * If the descriptor doesn't exist, EBADF is returned.
1887 *
1888 * If the descriptor exists but doesn't match 'flags' then
1889 * return EBADF for read attempts and EINVAL for write attempts.
1890 *
1891 * If 'hold' is set (non-zero) the file's refcount will be bumped on return.
1892 * It should be droped with fdrop().
1893 * If it is not set, then the refcount will not be bumped however the
1894 * thread's filedesc struct will be returned locked (for fgetsock).
1895 *
1896 * If an error occured the non-zero error is returned and *fpp is set to NULL.
1897 * Otherwise *fpp is set and zero is returned.
1898 */
1899static __inline int
1900_fget(struct thread *td, int fd, struct file **fpp, int flags, int hold)
1901{
1902 struct filedesc *fdp;
1903 struct file *fp;
1904
1905 *fpp = NULL;
1906 if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
1907 return (EBADF);
1908 FILEDESC_LOCK(fdp);
1909 if ((fp = fget_locked(fdp, fd)) == NULL || fp->f_ops == &badfileops) {
1910 FILEDESC_UNLOCK(fdp);
1911 return (EBADF);
1912 }
1913
1914 /*
1915 * Note: FREAD failures returns EBADF to maintain backwards
1916 * compatibility with what routines returned before.
1917 *
1918 * Only one flag, or 0, may be specified.
1919 */
1920 if (flags == FREAD && (fp->f_flag & FREAD) == 0) {
1921 FILEDESC_UNLOCK(fdp);
1922 return (EBADF);
1923 }
1924 if (flags == FWRITE && (fp->f_flag & FWRITE) == 0) {
1925 FILEDESC_UNLOCK(fdp);
1926 return (EINVAL);
1927 }
1928 if (hold) {
1929 fhold(fp);
1930 FILEDESC_UNLOCK(fdp);
1931 }
1932 *fpp = fp;
1933 return (0);
1934}
1935
1936int
1937fget(struct thread *td, int fd, struct file **fpp)
1938{
1939
1940 return(_fget(td, fd, fpp, 0, 1));
1941}
1942
1943int
1944fget_read(struct thread *td, int fd, struct file **fpp)
1945{
1946
1947 return(_fget(td, fd, fpp, FREAD, 1));
1948}
1949
1950int
1951fget_write(struct thread *td, int fd, struct file **fpp)
1952{
1953
1954 return(_fget(td, fd, fpp, FWRITE, 1));
1955}
1956
1957/*
1958 * Like fget() but loads the underlying vnode, or returns an error if
1959 * the descriptor does not represent a vnode. Note that pipes use vnodes
1960 * but never have VM objects (so VOP_GETVOBJECT() calls will return an
1961 * error). The returned vnode will be vref()d.
1962 */
1963static __inline int
1964_fgetvp(struct thread *td, int fd, struct vnode **vpp, int flags)
1965{
1966 struct file *fp;
1967 int error;
1968
1969 *vpp = NULL;
1970 if ((error = _fget(td, fd, &fp, 0, 0)) != 0)
1971 return (error);
1972 if (fp->f_vnode == NULL) {
1973 error = EINVAL;
1974 } else {
1975 *vpp = fp->f_vnode;
1976 vref(*vpp);
1977 }
1978 FILEDESC_UNLOCK(td->td_proc->p_fd);
1979 return (error);
1980}
1981
1982int
1983fgetvp(struct thread *td, int fd, struct vnode **vpp)
1984{
1985
1986 return (_fgetvp(td, fd, vpp, 0));
1987}
1988
1989int
1990fgetvp_read(struct thread *td, int fd, struct vnode **vpp)
1991{
1992
1993 return (_fgetvp(td, fd, vpp, FREAD));
1994}
1995
1996int
1997fgetvp_write(struct thread *td, int fd, struct vnode **vpp)
1998{
1999
2000 return (_fgetvp(td, fd, vpp, FWRITE));
2001}
2002
2003/*
2004 * Like fget() but loads the underlying socket, or returns an error if
2005 * the descriptor does not represent a socket.
2006 *
2007 * We bump the ref count on the returned socket. XXX Also obtain the SX
2008 * lock in the future.
2009 */
2010int
2011fgetsock(struct thread *td, int fd, struct socket **spp, u_int *fflagp)
2012{
2013 struct file *fp;
2014 int error;
2015
2016 *spp = NULL;
2017 if (fflagp != NULL)
2018 *fflagp = 0;
2019 if ((error = _fget(td, fd, &fp, 0, 0)) != 0)
2020 return (error);
2021 if (fp->f_type != DTYPE_SOCKET) {
2022 error = ENOTSOCK;
2023 } else {
2024 *spp = fp->f_data;
2025 if (fflagp)
2026 *fflagp = fp->f_flag;
39
40#include "opt_compat.h"
41
42#include <sys/param.h>
43#include <sys/limits.h>
44#include <sys/systm.h>
45#include <sys/syscallsubr.h>
46#include <sys/sysproto.h>
47#include <sys/conf.h>
48#include <sys/filedesc.h>
49#include <sys/lock.h>
50#include <sys/kernel.h>
51#include <sys/limits.h>
52#include <sys/malloc.h>
53#include <sys/mutex.h>
54#include <sys/sysctl.h>
55#include <sys/vnode.h>
56#include <sys/mount.h>
57#include <sys/proc.h>
58#include <sys/namei.h>
59#include <sys/file.h>
60#include <sys/stat.h>
61#include <sys/filio.h>
62#include <sys/fcntl.h>
63#include <sys/unistd.h>
64#include <sys/resourcevar.h>
65#include <sys/event.h>
66#include <sys/sx.h>
67#include <sys/socketvar.h>
68#include <sys/signalvar.h>
69
70#include <vm/vm.h>
71#include <vm/vm_extern.h>
72#include <vm/uma.h>
73
74static MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
75static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "file desc to leader",
76 "file desc to leader structures");
77static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
78
79static uma_zone_t file_zone;
80
81static d_open_t fdopen;
82#define NUMFDESC 64
83
84#define CDEV_MAJOR 22
85static struct cdevsw fildesc_cdevsw = {
86 .d_version = D_VERSION,
87 .d_flags = D_NEEDGIANT,
88 .d_open = fdopen,
89 .d_name = "FD",
90 .d_maj = CDEV_MAJOR,
91};
92
93/* How to treat 'new' parameter when allocating a fd for do_dup(). */
94enum dup_type { DUP_VARIABLE, DUP_FIXED };
95
96static int do_dup(struct thread *td, enum dup_type type, int old, int new,
97 register_t *retval);
98static int fd_first_free(struct filedesc *, int, int);
99static int fd_last_used(struct filedesc *, int, int);
100static void fdgrowtable(struct filedesc *, int);
101
102/*
103 * Descriptor management.
104 */
105struct filelist filehead; /* head of list of open files */
106int nfiles; /* actual number of open files */
107struct sx filelist_lock; /* sx to protect filelist */
108struct mtx sigio_lock; /* mtx to protect pointers to sigio */
109
110/*
111 * Find the first zero bit in the given bitmap, starting at low and not
112 * exceeding size - 1.
113 */
114static int
115fd_first_free(struct filedesc *fdp, int low, int size)
116{
117 NDSLOTTYPE *map = fdp->fd_map;
118 NDSLOTTYPE mask;
119 int off, maxoff;
120
121 if (low >= size)
122 return (low);
123
124 off = NDSLOT(low);
125 if (low % NDENTRIES) {
126 mask = ~(~(NDSLOTTYPE)0 >> (NDENTRIES - (low % NDENTRIES)));
127 if ((mask &= ~map[off]) != 0UL)
128 return (off * NDENTRIES + ffsl(mask) - 1);
129 ++off;
130 }
131 for (maxoff = NDSLOTS(size); off < maxoff; ++off)
132 if (map[off] != ~0UL)
133 return (off * NDENTRIES + ffsl(~map[off]) - 1);
134 return (size);
135}
136
137/*
138 * Find the highest non-zero bit in the given bitmap, starting at low and
139 * not exceeding size - 1.
140 */
141static int
142fd_last_used(struct filedesc *fdp, int low, int size)
143{
144 NDSLOTTYPE *map = fdp->fd_map;
145 NDSLOTTYPE mask;
146 int off, minoff;
147
148 if (low >= size)
149 return (-1);
150
151 off = NDSLOT(size);
152 if (size % NDENTRIES) {
153 mask = ~(~(NDSLOTTYPE)0 << (size % NDENTRIES));
154 if ((mask &= map[off]) != 0)
155 return (off * NDENTRIES + flsl(mask) - 1);
156 --off;
157 }
158 for (minoff = NDSLOT(low); off >= minoff; --off)
159 if (map[off] != 0)
160 return (off * NDENTRIES + flsl(map[off]) - 1);
161 return (size - 1);
162}
163
164static int
165fdisused(struct filedesc *fdp, int fd)
166{
167 KASSERT(fd >= 0 && fd < fdp->fd_nfiles,
168 ("file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles));
169 return ((fdp->fd_map[NDSLOT(fd)] & NDBIT(fd)) != 0);
170}
171
172/*
173 * Mark a file descriptor as used.
174 */
175void
176fdused(struct filedesc *fdp, int fd)
177{
178 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
179 KASSERT(!fdisused(fdp, fd),
180 ("fd already used"));
181 fdp->fd_map[NDSLOT(fd)] |= NDBIT(fd);
182 if (fd > fdp->fd_lastfile)
183 fdp->fd_lastfile = fd;
184 if (fd == fdp->fd_freefile)
185 fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles);
186}
187
188/*
189 * Mark a file descriptor as unused.
190 */
191void
192fdunused(struct filedesc *fdp, int fd)
193{
194 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
195 KASSERT(fdisused(fdp, fd),
196 ("fd is already unused"));
197 KASSERT(fdp->fd_ofiles[fd] == NULL,
198 ("fd is still in use"));
199 fdp->fd_map[NDSLOT(fd)] &= ~NDBIT(fd);
200 if (fd < fdp->fd_freefile)
201 fdp->fd_freefile = fd;
202 if (fd == fdp->fd_lastfile)
203 fdp->fd_lastfile = fd_last_used(fdp, 0, fd);
204}
205
206/*
207 * System calls on descriptors.
208 */
209#ifndef _SYS_SYSPROTO_H_
210struct getdtablesize_args {
211 int dummy;
212};
213#endif
214/*
215 * MPSAFE
216 */
217/* ARGSUSED */
218int
219getdtablesize(td, uap)
220 struct thread *td;
221 struct getdtablesize_args *uap;
222{
223 struct proc *p = td->td_proc;
224
225 PROC_LOCK(p);
226 td->td_retval[0] =
227 min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
228 PROC_UNLOCK(p);
229 return (0);
230}
231
232/*
233 * Duplicate a file descriptor to a particular value.
234 *
235 * note: keep in mind that a potential race condition exists when closing
236 * descriptors from a shared descriptor table (via rfork).
237 */
238#ifndef _SYS_SYSPROTO_H_
239struct dup2_args {
240 u_int from;
241 u_int to;
242};
243#endif
244/*
245 * MPSAFE
246 */
247/* ARGSUSED */
248int
249dup2(td, uap)
250 struct thread *td;
251 struct dup2_args *uap;
252{
253
254 return (do_dup(td, DUP_FIXED, (int)uap->from, (int)uap->to,
255 td->td_retval));
256}
257
258/*
259 * Duplicate a file descriptor.
260 */
261#ifndef _SYS_SYSPROTO_H_
262struct dup_args {
263 u_int fd;
264};
265#endif
266/*
267 * MPSAFE
268 */
269/* ARGSUSED */
270int
271dup(td, uap)
272 struct thread *td;
273 struct dup_args *uap;
274{
275
276 return (do_dup(td, DUP_VARIABLE, (int)uap->fd, 0, td->td_retval));
277}
278
279/*
280 * The file control system call.
281 */
282#ifndef _SYS_SYSPROTO_H_
283struct fcntl_args {
284 int fd;
285 int cmd;
286 long arg;
287};
288#endif
289/*
290 * MPSAFE
291 */
292/* ARGSUSED */
293int
294fcntl(td, uap)
295 struct thread *td;
296 struct fcntl_args *uap;
297{
298 struct flock fl;
299 intptr_t arg;
300 int error;
301
302 error = 0;
303 switch (uap->cmd) {
304 case F_GETLK:
305 case F_SETLK:
306 case F_SETLKW:
307 error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl));
308 arg = (intptr_t)&fl;
309 break;
310 default:
311 arg = uap->arg;
312 break;
313 }
314 if (error)
315 return (error);
316 error = kern_fcntl(td, uap->fd, uap->cmd, arg);
317 if (error)
318 return (error);
319 if (uap->cmd == F_GETLK)
320 error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl));
321 return (error);
322}
323
324int
325kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg)
326{
327 struct filedesc *fdp;
328 struct flock *flp;
329 struct file *fp;
330 struct proc *p;
331 char *pop;
332 struct vnode *vp;
333 u_int newmin;
334 int error, flg, tmp;
335
336 error = 0;
337 flg = F_POSIX;
338 p = td->td_proc;
339 fdp = p->p_fd;
340 mtx_lock(&Giant);
341 FILEDESC_LOCK(fdp);
342 if ((unsigned)fd >= fdp->fd_nfiles ||
343 (fp = fdp->fd_ofiles[fd]) == NULL) {
344 FILEDESC_UNLOCK(fdp);
345 error = EBADF;
346 goto done2;
347 }
348 pop = &fdp->fd_ofileflags[fd];
349
350 switch (cmd) {
351 case F_DUPFD:
352 FILEDESC_UNLOCK(fdp);
353 newmin = arg;
354 PROC_LOCK(p);
355 if (newmin >= lim_cur(p, RLIMIT_NOFILE) ||
356 newmin >= maxfilesperproc) {
357 PROC_UNLOCK(p);
358 error = EINVAL;
359 break;
360 }
361 PROC_UNLOCK(p);
362 error = do_dup(td, DUP_VARIABLE, fd, newmin, td->td_retval);
363 break;
364
365 case F_GETFD:
366 td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
367 FILEDESC_UNLOCK(fdp);
368 break;
369
370 case F_SETFD:
371 *pop = (*pop &~ UF_EXCLOSE) |
372 (arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
373 FILEDESC_UNLOCK(fdp);
374 break;
375
376 case F_GETFL:
377 FILE_LOCK(fp);
378 FILEDESC_UNLOCK(fdp);
379 td->td_retval[0] = OFLAGS(fp->f_flag);
380 FILE_UNLOCK(fp);
381 break;
382
383 case F_SETFL:
384 FILE_LOCK(fp);
385 FILEDESC_UNLOCK(fdp);
386 fhold_locked(fp);
387 fp->f_flag &= ~FCNTLFLAGS;
388 fp->f_flag |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
389 FILE_UNLOCK(fp);
390 tmp = fp->f_flag & FNONBLOCK;
391 error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
392 if (error) {
393 fdrop(fp, td);
394 break;
395 }
396 tmp = fp->f_flag & FASYNC;
397 error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
398 if (error == 0) {
399 fdrop(fp, td);
400 break;
401 }
402 FILE_LOCK(fp);
403 fp->f_flag &= ~FNONBLOCK;
404 FILE_UNLOCK(fp);
405 tmp = 0;
406 (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
407 fdrop(fp, td);
408 break;
409
410 case F_GETOWN:
411 fhold(fp);
412 FILEDESC_UNLOCK(fdp);
413 error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td);
414 if (error == 0)
415 td->td_retval[0] = tmp;
416 fdrop(fp, td);
417 break;
418
419 case F_SETOWN:
420 fhold(fp);
421 FILEDESC_UNLOCK(fdp);
422 tmp = arg;
423 error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td);
424 fdrop(fp, td);
425 break;
426
427 case F_SETLKW:
428 flg |= F_WAIT;
429 /* FALLTHROUGH F_SETLK */
430
431 case F_SETLK:
432 if (fp->f_type != DTYPE_VNODE) {
433 FILEDESC_UNLOCK(fdp);
434 error = EBADF;
435 break;
436 }
437
438 flp = (struct flock *)arg;
439 if (flp->l_whence == SEEK_CUR) {
440 if (fp->f_offset < 0 ||
441 (flp->l_start > 0 &&
442 fp->f_offset > OFF_MAX - flp->l_start)) {
443 FILEDESC_UNLOCK(fdp);
444 error = EOVERFLOW;
445 break;
446 }
447 flp->l_start += fp->f_offset;
448 }
449
450 /*
451 * VOP_ADVLOCK() may block.
452 */
453 fhold(fp);
454 FILEDESC_UNLOCK(fdp);
455 vp = fp->f_vnode;
456
457 switch (flp->l_type) {
458 case F_RDLCK:
459 if ((fp->f_flag & FREAD) == 0) {
460 error = EBADF;
461 break;
462 }
463 PROC_LOCK(p->p_leader);
464 p->p_leader->p_flag |= P_ADVLOCK;
465 PROC_UNLOCK(p->p_leader);
466 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
467 flp, flg);
468 break;
469 case F_WRLCK:
470 if ((fp->f_flag & FWRITE) == 0) {
471 error = EBADF;
472 break;
473 }
474 PROC_LOCK(p->p_leader);
475 p->p_leader->p_flag |= P_ADVLOCK;
476 PROC_UNLOCK(p->p_leader);
477 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
478 flp, flg);
479 break;
480 case F_UNLCK:
481 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
482 flp, F_POSIX);
483 break;
484 default:
485 error = EINVAL;
486 break;
487 }
488 /* Check for race with close */
489 FILEDESC_LOCK(fdp);
490 if ((unsigned) fd >= fdp->fd_nfiles ||
491 fp != fdp->fd_ofiles[fd]) {
492 FILEDESC_UNLOCK(fdp);
493 flp->l_whence = SEEK_SET;
494 flp->l_start = 0;
495 flp->l_len = 0;
496 flp->l_type = F_UNLCK;
497 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
498 F_UNLCK, flp, F_POSIX);
499 } else
500 FILEDESC_UNLOCK(fdp);
501 fdrop(fp, td);
502 break;
503
504 case F_GETLK:
505 if (fp->f_type != DTYPE_VNODE) {
506 FILEDESC_UNLOCK(fdp);
507 error = EBADF;
508 break;
509 }
510 flp = (struct flock *)arg;
511 if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK &&
512 flp->l_type != F_UNLCK) {
513 FILEDESC_UNLOCK(fdp);
514 error = EINVAL;
515 break;
516 }
517 if (flp->l_whence == SEEK_CUR) {
518 if ((flp->l_start > 0 &&
519 fp->f_offset > OFF_MAX - flp->l_start) ||
520 (flp->l_start < 0 &&
521 fp->f_offset < OFF_MIN - flp->l_start)) {
522 FILEDESC_UNLOCK(fdp);
523 error = EOVERFLOW;
524 break;
525 }
526 flp->l_start += fp->f_offset;
527 }
528 /*
529 * VOP_ADVLOCK() may block.
530 */
531 fhold(fp);
532 FILEDESC_UNLOCK(fdp);
533 vp = fp->f_vnode;
534 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp,
535 F_POSIX);
536 fdrop(fp, td);
537 break;
538 default:
539 FILEDESC_UNLOCK(fdp);
540 error = EINVAL;
541 break;
542 }
543done2:
544 mtx_unlock(&Giant);
545 return (error);
546}
547
548/*
549 * Common code for dup, dup2, and fcntl(F_DUPFD).
550 */
551static int
552do_dup(td, type, old, new, retval)
553 enum dup_type type;
554 int old, new;
555 register_t *retval;
556 struct thread *td;
557{
558 struct filedesc *fdp;
559 struct proc *p;
560 struct file *fp;
561 struct file *delfp;
562 int error, holdleaders, maxfd;
563
564 KASSERT((type == DUP_VARIABLE || type == DUP_FIXED),
565 ("invalid dup type %d", type));
566
567 p = td->td_proc;
568 fdp = p->p_fd;
569
570 /*
571 * Verify we have a valid descriptor to dup from and possibly to
572 * dup to.
573 */
574 if (old < 0 || new < 0)
575 return (EBADF);
576 PROC_LOCK(p);
577 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
578 PROC_UNLOCK(p);
579 if (new >= maxfd)
580 return (EMFILE);
581
582 FILEDESC_LOCK(fdp);
583 if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) {
584 FILEDESC_UNLOCK(fdp);
585 return (EBADF);
586 }
587 if (type == DUP_FIXED && old == new) {
588 *retval = new;
589 FILEDESC_UNLOCK(fdp);
590 return (0);
591 }
592 fp = fdp->fd_ofiles[old];
593 fhold(fp);
594
595 /*
596 * If the caller specified a file descriptor, make sure the file
597 * table is large enough to hold it, and grab it. Otherwise, just
598 * allocate a new descriptor the usual way. Since the filedesc
599 * lock may be temporarily dropped in the process, we have to look
600 * out for a race.
601 */
602 if (type == DUP_FIXED) {
603 if (new >= fdp->fd_nfiles)
604 fdgrowtable(fdp, new + 1);
605 if (fdp->fd_ofiles[new] == NULL)
606 fdused(fdp, new);
607 } else {
608 if ((error = fdalloc(td, new, &new)) != 0) {
609 FILEDESC_UNLOCK(fdp);
610 fdrop(fp, td);
611 return (error);
612 }
613 }
614
615 /*
616 * If the old file changed out from under us then treat it as a
617 * bad file descriptor. Userland should do its own locking to
618 * avoid this case.
619 */
620 if (fdp->fd_ofiles[old] != fp) {
621 /* we've allocated a descriptor which we won't use */
622 if (fdp->fd_ofiles[new] == NULL)
623 fdunused(fdp, new);
624 FILEDESC_UNLOCK(fdp);
625 fdrop(fp, td);
626 return (EBADF);
627 }
628 KASSERT(old != new,
629 ("new fd is same as old"));
630
631 /*
632 * Save info on the descriptor being overwritten. We cannot close
633 * it without introducing an ownership race for the slot, since we
634 * need to drop the filedesc lock to call closef().
635 *
636 * XXX this duplicates parts of close().
637 */
638 delfp = fdp->fd_ofiles[new];
639 holdleaders = 0;
640 if (delfp != NULL) {
641 if (td->td_proc->p_fdtol != NULL) {
642 /*
643 * Ask fdfree() to sleep to ensure that all relevant
644 * process leaders can be traversed in closef().
645 */
646 fdp->fd_holdleaderscount++;
647 holdleaders = 1;
648 }
649 }
650
651 /*
652 * Duplicate the source descriptor
653 */
654 fdp->fd_ofiles[new] = fp;
655 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
656 if (new > fdp->fd_lastfile)
657 fdp->fd_lastfile = new;
658 FILEDESC_UNLOCK(fdp);
659 *retval = new;
660
661 /*
662 * If we dup'd over a valid file, we now own the reference to it
663 * and must dispose of it using closef() semantics (as if a
664 * close() were performed on it).
665 *
666 * XXX this duplicates parts of close().
667 */
668 if (delfp != NULL) {
669 /* XXX need to call knote_fdclose() */
670 mtx_lock(&Giant);
671 (void) closef(delfp, td);
672 mtx_unlock(&Giant);
673 if (holdleaders) {
674 FILEDESC_LOCK(fdp);
675 fdp->fd_holdleaderscount--;
676 if (fdp->fd_holdleaderscount == 0 &&
677 fdp->fd_holdleaderswakeup != 0) {
678 fdp->fd_holdleaderswakeup = 0;
679 wakeup(&fdp->fd_holdleaderscount);
680 }
681 FILEDESC_UNLOCK(fdp);
682 }
683 }
684 return (0);
685}
686
687/*
688 * If sigio is on the list associated with a process or process group,
689 * disable signalling from the device, remove sigio from the list and
690 * free sigio.
691 */
692void
693funsetown(sigiop)
694 struct sigio **sigiop;
695{
696 struct sigio *sigio;
697
698 SIGIO_LOCK();
699 sigio = *sigiop;
700 if (sigio == NULL) {
701 SIGIO_UNLOCK();
702 return;
703 }
704 *(sigio->sio_myref) = NULL;
705 if ((sigio)->sio_pgid < 0) {
706 struct pgrp *pg = (sigio)->sio_pgrp;
707 PGRP_LOCK(pg);
708 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
709 sigio, sio_pgsigio);
710 PGRP_UNLOCK(pg);
711 } else {
712 struct proc *p = (sigio)->sio_proc;
713 PROC_LOCK(p);
714 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
715 sigio, sio_pgsigio);
716 PROC_UNLOCK(p);
717 }
718 SIGIO_UNLOCK();
719 crfree(sigio->sio_ucred);
720 FREE(sigio, M_SIGIO);
721}
722
723/*
724 * Free a list of sigio structures.
725 * We only need to lock the SIGIO_LOCK because we have made ourselves
726 * inaccessable to callers of fsetown and therefore do not need to lock
727 * the proc or pgrp struct for the list manipulation.
728 */
729void
730funsetownlst(sigiolst)
731 struct sigiolst *sigiolst;
732{
733 struct proc *p;
734 struct pgrp *pg;
735 struct sigio *sigio;
736
737 sigio = SLIST_FIRST(sigiolst);
738 if (sigio == NULL)
739 return;
740 p = NULL;
741 pg = NULL;
742
743 /*
744 * Every entry of the list should belong
745 * to a single proc or pgrp.
746 */
747 if (sigio->sio_pgid < 0) {
748 pg = sigio->sio_pgrp;
749 PGRP_LOCK_ASSERT(pg, MA_NOTOWNED);
750 } else /* if (sigio->sio_pgid > 0) */ {
751 p = sigio->sio_proc;
752 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
753 }
754
755 SIGIO_LOCK();
756 while ((sigio = SLIST_FIRST(sigiolst)) != NULL) {
757 *(sigio->sio_myref) = NULL;
758 if (pg != NULL) {
759 KASSERT(sigio->sio_pgid < 0,
760 ("Proc sigio in pgrp sigio list"));
761 KASSERT(sigio->sio_pgrp == pg,
762 ("Bogus pgrp in sigio list"));
763 PGRP_LOCK(pg);
764 SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio,
765 sio_pgsigio);
766 PGRP_UNLOCK(pg);
767 } else /* if (p != NULL) */ {
768 KASSERT(sigio->sio_pgid > 0,
769 ("Pgrp sigio in proc sigio list"));
770 KASSERT(sigio->sio_proc == p,
771 ("Bogus proc in sigio list"));
772 PROC_LOCK(p);
773 SLIST_REMOVE(&p->p_sigiolst, sigio, sigio,
774 sio_pgsigio);
775 PROC_UNLOCK(p);
776 }
777 SIGIO_UNLOCK();
778 crfree(sigio->sio_ucred);
779 FREE(sigio, M_SIGIO);
780 SIGIO_LOCK();
781 }
782 SIGIO_UNLOCK();
783}
784
785/*
786 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
787 *
788 * After permission checking, add a sigio structure to the sigio list for
789 * the process or process group.
790 */
791int
792fsetown(pgid, sigiop)
793 pid_t pgid;
794 struct sigio **sigiop;
795{
796 struct proc *proc;
797 struct pgrp *pgrp;
798 struct sigio *sigio;
799 int ret;
800
801 if (pgid == 0) {
802 funsetown(sigiop);
803 return (0);
804 }
805
806 ret = 0;
807
808 /* Allocate and fill in the new sigio out of locks. */
809 MALLOC(sigio, struct sigio *, sizeof(struct sigio), M_SIGIO, M_WAITOK);
810 sigio->sio_pgid = pgid;
811 sigio->sio_ucred = crhold(curthread->td_ucred);
812 sigio->sio_myref = sigiop;
813
814 sx_slock(&proctree_lock);
815 if (pgid > 0) {
816 proc = pfind(pgid);
817 if (proc == NULL) {
818 ret = ESRCH;
819 goto fail;
820 }
821
822 /*
823 * Policy - Don't allow a process to FSETOWN a process
824 * in another session.
825 *
826 * Remove this test to allow maximum flexibility or
827 * restrict FSETOWN to the current process or process
828 * group for maximum safety.
829 */
830 PROC_UNLOCK(proc);
831 if (proc->p_session != curthread->td_proc->p_session) {
832 ret = EPERM;
833 goto fail;
834 }
835
836 pgrp = NULL;
837 } else /* if (pgid < 0) */ {
838 pgrp = pgfind(-pgid);
839 if (pgrp == NULL) {
840 ret = ESRCH;
841 goto fail;
842 }
843 PGRP_UNLOCK(pgrp);
844
845 /*
846 * Policy - Don't allow a process to FSETOWN a process
847 * in another session.
848 *
849 * Remove this test to allow maximum flexibility or
850 * restrict FSETOWN to the current process or process
851 * group for maximum safety.
852 */
853 if (pgrp->pg_session != curthread->td_proc->p_session) {
854 ret = EPERM;
855 goto fail;
856 }
857
858 proc = NULL;
859 }
860 funsetown(sigiop);
861 if (pgid > 0) {
862 PROC_LOCK(proc);
863 /*
864 * Since funsetownlst() is called without the proctree
865 * locked, we need to check for P_WEXIT.
866 * XXX: is ESRCH correct?
867 */
868 if ((proc->p_flag & P_WEXIT) != 0) {
869 PROC_UNLOCK(proc);
870 ret = ESRCH;
871 goto fail;
872 }
873 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
874 sigio->sio_proc = proc;
875 PROC_UNLOCK(proc);
876 } else {
877 PGRP_LOCK(pgrp);
878 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
879 sigio->sio_pgrp = pgrp;
880 PGRP_UNLOCK(pgrp);
881 }
882 sx_sunlock(&proctree_lock);
883 SIGIO_LOCK();
884 *sigiop = sigio;
885 SIGIO_UNLOCK();
886 return (0);
887
888fail:
889 sx_sunlock(&proctree_lock);
890 crfree(sigio->sio_ucred);
891 FREE(sigio, M_SIGIO);
892 return (ret);
893}
894
895/*
896 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
897 */
898pid_t
899fgetown(sigiop)
900 struct sigio **sigiop;
901{
902 pid_t pgid;
903
904 SIGIO_LOCK();
905 pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0;
906 SIGIO_UNLOCK();
907 return (pgid);
908}
909
910/*
911 * Close a file descriptor.
912 */
913#ifndef _SYS_SYSPROTO_H_
914struct close_args {
915 int fd;
916};
917#endif
918/*
919 * MPSAFE
920 */
921/* ARGSUSED */
922int
923close(td, uap)
924 struct thread *td;
925 struct close_args *uap;
926{
927 struct filedesc *fdp;
928 struct file *fp;
929 int fd, error;
930 int holdleaders;
931
932 fd = uap->fd;
933 error = 0;
934 holdleaders = 0;
935 fdp = td->td_proc->p_fd;
936 mtx_lock(&Giant);
937 FILEDESC_LOCK(fdp);
938 if ((unsigned)fd >= fdp->fd_nfiles ||
939 (fp = fdp->fd_ofiles[fd]) == NULL) {
940 FILEDESC_UNLOCK(fdp);
941 mtx_unlock(&Giant);
942 return (EBADF);
943 }
944 fdp->fd_ofiles[fd] = NULL;
945 fdp->fd_ofileflags[fd] = 0;
946 fdunused(fdp, fd);
947 if (td->td_proc->p_fdtol != NULL) {
948 /*
949 * Ask fdfree() to sleep to ensure that all relevant
950 * process leaders can be traversed in closef().
951 */
952 fdp->fd_holdleaderscount++;
953 holdleaders = 1;
954 }
955
956 /*
957 * we now hold the fp reference that used to be owned by the descriptor
958 * array.
959 */
960 if (fd < fdp->fd_knlistsize) {
961 FILEDESC_UNLOCK(fdp);
962 knote_fdclose(td, fd);
963 } else
964 FILEDESC_UNLOCK(fdp);
965
966 error = closef(fp, td);
967 mtx_unlock(&Giant);
968 if (holdleaders) {
969 FILEDESC_LOCK(fdp);
970 fdp->fd_holdleaderscount--;
971 if (fdp->fd_holdleaderscount == 0 &&
972 fdp->fd_holdleaderswakeup != 0) {
973 fdp->fd_holdleaderswakeup = 0;
974 wakeup(&fdp->fd_holdleaderscount);
975 }
976 FILEDESC_UNLOCK(fdp);
977 }
978 return (error);
979}
980
981#if defined(COMPAT_43)
982/*
983 * Return status information about a file descriptor.
984 */
985#ifndef _SYS_SYSPROTO_H_
986struct ofstat_args {
987 int fd;
988 struct ostat *sb;
989};
990#endif
991/*
992 * MPSAFE
993 */
994/* ARGSUSED */
995int
996ofstat(td, uap)
997 struct thread *td;
998 struct ofstat_args *uap;
999{
1000 struct file *fp;
1001 struct stat ub;
1002 struct ostat oub;
1003 int error;
1004
1005 if ((error = fget(td, uap->fd, &fp)) != 0)
1006 goto done2;
1007 mtx_lock(&Giant);
1008 error = fo_stat(fp, &ub, td->td_ucred, td);
1009 mtx_unlock(&Giant);
1010 if (error == 0) {
1011 cvtstat(&ub, &oub);
1012 error = copyout(&oub, uap->sb, sizeof(oub));
1013 }
1014 fdrop(fp, td);
1015done2:
1016 return (error);
1017}
1018#endif /* COMPAT_43 */
1019
1020/*
1021 * Return status information about a file descriptor.
1022 */
1023#ifndef _SYS_SYSPROTO_H_
1024struct fstat_args {
1025 int fd;
1026 struct stat *sb;
1027};
1028#endif
1029/*
1030 * MPSAFE
1031 */
1032/* ARGSUSED */
1033int
1034fstat(td, uap)
1035 struct thread *td;
1036 struct fstat_args *uap;
1037{
1038 struct file *fp;
1039 struct stat ub;
1040 int error;
1041
1042 if ((error = fget(td, uap->fd, &fp)) != 0)
1043 goto done2;
1044 mtx_lock(&Giant);
1045 error = fo_stat(fp, &ub, td->td_ucred, td);
1046 mtx_unlock(&Giant);
1047 if (error == 0)
1048 error = copyout(&ub, uap->sb, sizeof(ub));
1049 fdrop(fp, td);
1050done2:
1051 return (error);
1052}
1053
1054/*
1055 * Return status information about a file descriptor.
1056 */
1057#ifndef _SYS_SYSPROTO_H_
1058struct nfstat_args {
1059 int fd;
1060 struct nstat *sb;
1061};
1062#endif
1063/*
1064 * MPSAFE
1065 */
1066/* ARGSUSED */
1067int
1068nfstat(td, uap)
1069 struct thread *td;
1070 struct nfstat_args *uap;
1071{
1072 struct file *fp;
1073 struct stat ub;
1074 struct nstat nub;
1075 int error;
1076
1077 if ((error = fget(td, uap->fd, &fp)) != 0)
1078 goto done2;
1079 mtx_lock(&Giant);
1080 error = fo_stat(fp, &ub, td->td_ucred, td);
1081 mtx_unlock(&Giant);
1082 if (error == 0) {
1083 cvtnstat(&ub, &nub);
1084 error = copyout(&nub, uap->sb, sizeof(nub));
1085 }
1086 fdrop(fp, td);
1087done2:
1088 return (error);
1089}
1090
1091/*
1092 * Return pathconf information about a file descriptor.
1093 */
1094#ifndef _SYS_SYSPROTO_H_
1095struct fpathconf_args {
1096 int fd;
1097 int name;
1098};
1099#endif
1100/*
1101 * MPSAFE
1102 */
1103/* ARGSUSED */
1104int
1105fpathconf(td, uap)
1106 struct thread *td;
1107 struct fpathconf_args *uap;
1108{
1109 struct file *fp;
1110 struct vnode *vp;
1111 int error;
1112
1113 if ((error = fget(td, uap->fd, &fp)) != 0)
1114 return (error);
1115
1116 /* If asynchronous I/O is available, it works for all descriptors. */
1117 if (uap->name == _PC_ASYNC_IO) {
1118 td->td_retval[0] = async_io_version;
1119 goto out;
1120 }
1121 vp = fp->f_vnode;
1122 if (vp != NULL) {
1123 mtx_lock(&Giant);
1124 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1125 error = VOP_PATHCONF(vp, uap->name, td->td_retval);
1126 VOP_UNLOCK(vp, 0, td);
1127 mtx_unlock(&Giant);
1128 } else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
1129 if (uap->name != _PC_PIPE_BUF) {
1130 error = EINVAL;
1131 } else {
1132 td->td_retval[0] = PIPE_BUF;
1133 error = 0;
1134 }
1135 } else {
1136 error = EOPNOTSUPP;
1137 }
1138out:
1139 fdrop(fp, td);
1140 return (error);
1141}
1142
1143/*
1144 * Grow the file table to accomodate (at least) nfd descriptors. This may
1145 * block and drop the filedesc lock, but it will reacquire it before
1146 * returing.
1147 */
1148static void
1149fdgrowtable(struct filedesc *fdp, int nfd)
1150{
1151 struct file **ntable;
1152 char *nfileflags;
1153 int nnfiles, onfiles;
1154 NDSLOTTYPE *nmap;
1155
1156 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
1157
1158 KASSERT(fdp->fd_nfiles > 0,
1159 ("zero-length file table"));
1160
1161 /* compute the size of the new table */
1162 onfiles = fdp->fd_nfiles;
1163 nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */
1164 if (nnfiles <= onfiles)
1165 /* the table is already large enough */
1166 return;
1167
1168 /* allocate a new table and (if required) new bitmaps */
1169 FILEDESC_UNLOCK(fdp);
1170 MALLOC(ntable, struct file **, nnfiles * OFILESIZE,
1171 M_FILEDESC, M_ZERO | M_WAITOK);
1172 nfileflags = (char *)&ntable[nnfiles];
1173 if (NDSLOTS(nnfiles) > NDSLOTS(onfiles))
1174 MALLOC(nmap, NDSLOTTYPE *, NDSLOTS(nnfiles) * NDSLOTSIZE,
1175 M_FILEDESC, M_ZERO | M_WAITOK);
1176 else
1177 nmap = NULL;
1178 FILEDESC_LOCK(fdp);
1179
1180 /*
1181 * We now have new tables ready to go. Since we dropped the
1182 * filedesc lock to call malloc(), watch out for a race.
1183 */
1184 onfiles = fdp->fd_nfiles;
1185 if (onfiles >= nnfiles) {
1186 /* we lost the race, but that's OK */
1187 free(ntable, M_FILEDESC);
1188 if (nmap != NULL)
1189 free(nmap, M_FILEDESC);
1190 return;
1191 }
1192 bcopy(fdp->fd_ofiles, ntable, onfiles * sizeof(*ntable));
1193 bcopy(fdp->fd_ofileflags, nfileflags, onfiles);
1194 if (onfiles > NDFILE)
1195 free(fdp->fd_ofiles, M_FILEDESC);
1196 fdp->fd_ofiles = ntable;
1197 fdp->fd_ofileflags = nfileflags;
1198 if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) {
1199 bcopy(fdp->fd_map, nmap, NDSLOTS(onfiles) * sizeof(*nmap));
1200 if (NDSLOTS(onfiles) > NDSLOTS(NDFILE))
1201 free(fdp->fd_map, M_FILEDESC);
1202 fdp->fd_map = nmap;
1203 }
1204 fdp->fd_nfiles = nnfiles;
1205}
1206
1207/*
1208 * Allocate a file descriptor for the process.
1209 */
1210int
1211fdalloc(struct thread *td, int minfd, int *result)
1212{
1213 struct proc *p = td->td_proc;
1214 struct filedesc *fdp = p->p_fd;
1215 int fd = -1, maxfd;
1216
1217 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
1218
1219 PROC_LOCK(p);
1220 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1221 PROC_UNLOCK(p);
1222
1223 /*
1224 * Search the bitmap for a free descriptor. If none is found, try
1225 * to grow the file table. Keep at it until we either get a file
1226 * descriptor or run into process or system limits; fdgrowtable()
1227 * may drop the filedesc lock, so we're in a race.
1228 */
1229 for (;;) {
1230 fd = fd_first_free(fdp, minfd, fdp->fd_nfiles);
1231 if (fd >= maxfd)
1232 return (EMFILE);
1233 if (fd < fdp->fd_nfiles)
1234 break;
1235 fdgrowtable(fdp, min(fdp->fd_nfiles * 2, maxfd));
1236 }
1237
1238 /*
1239 * Perform some sanity checks, then mark the file descriptor as
1240 * used and return it to the caller.
1241 */
1242 KASSERT(!fdisused(fdp, fd),
1243 ("fd_first_free() returned non-free descriptor"));
1244 KASSERT(fdp->fd_ofiles[fd] == NULL,
1245 ("free descriptor isn't"));
1246 fdp->fd_ofileflags[fd] = 0; /* XXX needed? */
1247 fdused(fdp, fd);
1248 fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles);
1249 *result = fd;
1250 return (0);
1251}
1252
1253/*
1254 * Check to see whether n user file descriptors
1255 * are available to the process p.
1256 */
1257int
1258fdavail(td, n)
1259 struct thread *td;
1260 int n;
1261{
1262 struct proc *p = td->td_proc;
1263 struct filedesc *fdp = td->td_proc->p_fd;
1264 struct file **fpp;
1265 int i, lim, last;
1266
1267 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
1268
1269 PROC_LOCK(p);
1270 lim = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1271 PROC_UNLOCK(p);
1272 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
1273 return (1);
1274 last = min(fdp->fd_nfiles, lim);
1275 fpp = &fdp->fd_ofiles[fdp->fd_freefile];
1276 for (i = last - fdp->fd_freefile; --i >= 0; fpp++) {
1277 if (*fpp == NULL && --n <= 0)
1278 return (1);
1279 }
1280 return (0);
1281}
1282
1283/*
1284 * Create a new open file structure and allocate
1285 * a file decriptor for the process that refers to it.
1286 * We add one reference to the file for the descriptor table
1287 * and one reference for resultfp. This is to prevent us being
1288 * prempted and the entry in the descriptor table closed after
1289 * we release the FILEDESC lock.
1290 */
1291int
1292falloc(td, resultfp, resultfd)
1293 struct thread *td;
1294 struct file **resultfp;
1295 int *resultfd;
1296{
1297 struct proc *p = td->td_proc;
1298 struct file *fp, *fq;
1299 int error, i;
1300 int maxuserfiles = maxfiles - (maxfiles / 20);
1301 static struct timeval lastfail;
1302 static int curfail;
1303
1304 fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO);
1305 sx_xlock(&filelist_lock);
1306 if ((nfiles >= maxuserfiles && td->td_ucred->cr_ruid != 0)
1307 || nfiles >= maxfiles) {
1308 if (ppsratecheck(&lastfail, &curfail, 1)) {
1309 printf("kern.maxfiles limit exceeded by uid %i, please see tuning(7).\n",
1310 td->td_ucred->cr_ruid);
1311 }
1312 sx_xunlock(&filelist_lock);
1313 uma_zfree(file_zone, fp);
1314 return (ENFILE);
1315 }
1316 nfiles++;
1317
1318 /*
1319 * If the process has file descriptor zero open, add the new file
1320 * descriptor to the list of open files at that point, otherwise
1321 * put it at the front of the list of open files.
1322 */
1323 fp->f_mtxp = mtx_pool_alloc(mtxpool_sleep);
1324 fp->f_count = 1;
1325 if (resultfp)
1326 fp->f_count++;
1327 fp->f_cred = crhold(td->td_ucred);
1328 fp->f_ops = &badfileops;
1329 FILEDESC_LOCK(p->p_fd);
1330 if ((fq = p->p_fd->fd_ofiles[0])) {
1331 LIST_INSERT_AFTER(fq, fp, f_list);
1332 } else {
1333 LIST_INSERT_HEAD(&filehead, fp, f_list);
1334 }
1335 sx_xunlock(&filelist_lock);
1336 if ((error = fdalloc(td, 0, &i))) {
1337 FILEDESC_UNLOCK(p->p_fd);
1338 fdrop(fp, td);
1339 if (resultfp)
1340 fdrop(fp, td);
1341 return (error);
1342 }
1343 p->p_fd->fd_ofiles[i] = fp;
1344 FILEDESC_UNLOCK(p->p_fd);
1345 if (resultfp)
1346 *resultfp = fp;
1347 if (resultfd)
1348 *resultfd = i;
1349 return (0);
1350}
1351
1352/*
1353 * Free a file descriptor.
1354 */
1355void
1356ffree(fp)
1357 struct file *fp;
1358{
1359
1360 KASSERT(fp->f_count == 0, ("ffree: fp_fcount not 0!"));
1361 sx_xlock(&filelist_lock);
1362 LIST_REMOVE(fp, f_list);
1363 nfiles--;
1364 sx_xunlock(&filelist_lock);
1365 crfree(fp->f_cred);
1366 uma_zfree(file_zone, fp);
1367}
1368
1369/*
1370 * Build a new filedesc structure from another.
1371 * Copy the current, root, and jail root vnode references.
1372 */
1373struct filedesc *
1374fdinit(fdp)
1375 struct filedesc *fdp;
1376{
1377 struct filedesc0 *newfdp;
1378
1379 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
1380
1381 FILEDESC_UNLOCK(fdp);
1382 MALLOC(newfdp, struct filedesc0 *, sizeof(struct filedesc0),
1383 M_FILEDESC, M_WAITOK | M_ZERO);
1384 FILEDESC_LOCK(fdp);
1385 mtx_init(&newfdp->fd_fd.fd_mtx, FILEDESC_LOCK_DESC, NULL, MTX_DEF);
1386 newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
1387 if (newfdp->fd_fd.fd_cdir)
1388 VREF(newfdp->fd_fd.fd_cdir);
1389 newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
1390 if (newfdp->fd_fd.fd_rdir)
1391 VREF(newfdp->fd_fd.fd_rdir);
1392 newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
1393 if (newfdp->fd_fd.fd_jdir)
1394 VREF(newfdp->fd_fd.fd_jdir);
1395
1396 /* Create the file descriptor table. */
1397 newfdp->fd_fd.fd_refcnt = 1;
1398 newfdp->fd_fd.fd_cmask = CMASK;
1399 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1400 newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1401 newfdp->fd_fd.fd_nfiles = NDFILE;
1402 newfdp->fd_fd.fd_knlistsize = -1;
1403 newfdp->fd_fd.fd_map = newfdp->fd_dmap;
1404 return (&newfdp->fd_fd);
1405}
1406
1407/*
1408 * Share a filedesc structure.
1409 */
1410struct filedesc *
1411fdshare(fdp)
1412 struct filedesc *fdp;
1413{
1414 FILEDESC_LOCK(fdp);
1415 fdp->fd_refcnt++;
1416 FILEDESC_UNLOCK(fdp);
1417 return (fdp);
1418}
1419
1420/*
1421 * Copy a filedesc structure.
1422 * A NULL pointer in returns a NULL reference, this is to ease callers,
1423 * not catch errors.
1424 */
1425struct filedesc *
1426fdcopy(fdp)
1427 struct filedesc *fdp;
1428{
1429 struct filedesc *newfdp;
1430 int i;
1431
1432 /* Certain daemons might not have file descriptors. */
1433 if (fdp == NULL)
1434 return (NULL);
1435
1436 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
1437 newfdp = fdinit(fdp);
1438 while (fdp->fd_lastfile >= newfdp->fd_nfiles) {
1439 FILEDESC_UNLOCK(fdp);
1440 FILEDESC_LOCK(newfdp);
1441 fdgrowtable(newfdp, fdp->fd_lastfile + 1);
1442 FILEDESC_UNLOCK(newfdp);
1443 FILEDESC_LOCK(fdp);
1444 }
1445 /* copy everything except kqueue descriptors */
1446 newfdp->fd_freefile = -1;
1447 for (i = 0; i <= fdp->fd_lastfile; ++i) {
1448 if (fdisused(fdp, i) &&
1449 fdp->fd_ofiles[i]->f_type != DTYPE_KQUEUE) {
1450 newfdp->fd_ofiles[i] = fdp->fd_ofiles[i];
1451 newfdp->fd_ofileflags[i] = fdp->fd_ofileflags[i];
1452 fhold(newfdp->fd_ofiles[i]);
1453 newfdp->fd_lastfile = i;
1454 } else {
1455 if (newfdp->fd_freefile == -1)
1456 newfdp->fd_freefile = i;
1457 }
1458 }
1459 FILEDESC_UNLOCK(fdp);
1460 FILEDESC_LOCK(newfdp);
1461 for (i = 0; i <= newfdp->fd_lastfile; ++i)
1462 if (newfdp->fd_ofiles[i] != NULL)
1463 fdused(newfdp, i);
1464 FILEDESC_UNLOCK(newfdp);
1465 FILEDESC_LOCK(fdp);
1466 if (newfdp->fd_freefile == -1)
1467 newfdp->fd_freefile = i;
1468 newfdp->fd_cmask = fdp->fd_cmask;
1469 return (newfdp);
1470}
1471
1472/* A mutex to protect the association between a proc and filedesc. */
1473struct mtx fdesc_mtx;
1474MTX_SYSINIT(fdesc, &fdesc_mtx, "fdesc", MTX_DEF);
1475
1476/*
1477 * Release a filedesc structure.
1478 */
1479void
1480fdfree(td)
1481 struct thread *td;
1482{
1483 struct filedesc *fdp;
1484 struct file **fpp;
1485 int i;
1486 struct filedesc_to_leader *fdtol;
1487 struct file *fp;
1488 struct vnode *vp;
1489 struct flock lf;
1490
1491 /* Certain daemons might not have file descriptors. */
1492 fdp = td->td_proc->p_fd;
1493 if (fdp == NULL)
1494 return;
1495
1496 /* Check for special need to clear POSIX style locks */
1497 fdtol = td->td_proc->p_fdtol;
1498 if (fdtol != NULL) {
1499 FILEDESC_LOCK(fdp);
1500 KASSERT(fdtol->fdl_refcount > 0,
1501 ("filedesc_to_refcount botch: fdl_refcount=%d",
1502 fdtol->fdl_refcount));
1503 if (fdtol->fdl_refcount == 1 &&
1504 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1505 i = 0;
1506 fpp = fdp->fd_ofiles;
1507 for (i = 0, fpp = fdp->fd_ofiles;
1508 i <= fdp->fd_lastfile;
1509 i++, fpp++) {
1510 if (*fpp == NULL ||
1511 (*fpp)->f_type != DTYPE_VNODE)
1512 continue;
1513 fp = *fpp;
1514 fhold(fp);
1515 FILEDESC_UNLOCK(fdp);
1516 lf.l_whence = SEEK_SET;
1517 lf.l_start = 0;
1518 lf.l_len = 0;
1519 lf.l_type = F_UNLCK;
1520 vp = fp->f_vnode;
1521 (void) VOP_ADVLOCK(vp,
1522 (caddr_t)td->td_proc->
1523 p_leader,
1524 F_UNLCK,
1525 &lf,
1526 F_POSIX);
1527 FILEDESC_LOCK(fdp);
1528 fdrop(fp, td);
1529 fpp = fdp->fd_ofiles + i;
1530 }
1531 }
1532 retry:
1533 if (fdtol->fdl_refcount == 1) {
1534 if (fdp->fd_holdleaderscount > 0 &&
1535 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1536 /*
1537 * close() or do_dup() has cleared a reference
1538 * in a shared file descriptor table.
1539 */
1540 fdp->fd_holdleaderswakeup = 1;
1541 msleep(&fdp->fd_holdleaderscount, &fdp->fd_mtx,
1542 PLOCK, "fdlhold", 0);
1543 goto retry;
1544 }
1545 if (fdtol->fdl_holdcount > 0) {
1546 /*
1547 * Ensure that fdtol->fdl_leader
1548 * remains valid in closef().
1549 */
1550 fdtol->fdl_wakeup = 1;
1551 msleep(fdtol, &fdp->fd_mtx,
1552 PLOCK, "fdlhold", 0);
1553 goto retry;
1554 }
1555 }
1556 fdtol->fdl_refcount--;
1557 if (fdtol->fdl_refcount == 0 &&
1558 fdtol->fdl_holdcount == 0) {
1559 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
1560 fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
1561 } else
1562 fdtol = NULL;
1563 td->td_proc->p_fdtol = NULL;
1564 FILEDESC_UNLOCK(fdp);
1565 if (fdtol != NULL)
1566 FREE(fdtol, M_FILEDESC_TO_LEADER);
1567 }
1568 FILEDESC_LOCK(fdp);
1569 if (--fdp->fd_refcnt > 0) {
1570 FILEDESC_UNLOCK(fdp);
1571 return;
1572 }
1573
1574 /*
1575 * We are the last reference to the structure, so we can
1576 * safely assume it will not change out from under us.
1577 */
1578 FILEDESC_UNLOCK(fdp);
1579 fpp = fdp->fd_ofiles;
1580 for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
1581 if (*fpp)
1582 (void) closef(*fpp, td);
1583 }
1584
1585 /* XXX This should happen earlier. */
1586 mtx_lock(&fdesc_mtx);
1587 td->td_proc->p_fd = NULL;
1588 mtx_unlock(&fdesc_mtx);
1589
1590 if (fdp->fd_nfiles > NDFILE)
1591 FREE(fdp->fd_ofiles, M_FILEDESC);
1592 if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE))
1593 FREE(fdp->fd_map, M_FILEDESC);
1594 if (fdp->fd_cdir)
1595 vrele(fdp->fd_cdir);
1596 if (fdp->fd_rdir)
1597 vrele(fdp->fd_rdir);
1598 if (fdp->fd_jdir)
1599 vrele(fdp->fd_jdir);
1600 if (fdp->fd_knlist)
1601 FREE(fdp->fd_knlist, M_KQUEUE);
1602 if (fdp->fd_knhash)
1603 FREE(fdp->fd_knhash, M_KQUEUE);
1604 mtx_destroy(&fdp->fd_mtx);
1605 FREE(fdp, M_FILEDESC);
1606}
1607
1608/*
1609 * For setugid programs, we don't want to people to use that setugidness
1610 * to generate error messages which write to a file which otherwise would
1611 * otherwise be off-limits to the process. We check for filesystems where
1612 * the vnode can change out from under us after execve (like [lin]procfs).
1613 *
1614 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
1615 * sufficient. We also don't for check setugidness since we know we are.
1616 */
1617static int
1618is_unsafe(struct file *fp)
1619{
1620 if (fp->f_type == DTYPE_VNODE) {
1621 struct vnode *vp = fp->f_vnode;
1622
1623 if ((vp->v_vflag & VV_PROCDEP) != 0)
1624 return (1);
1625 }
1626 return (0);
1627}
1628
1629/*
1630 * Make this setguid thing safe, if at all possible.
1631 */
1632void
1633setugidsafety(td)
1634 struct thread *td;
1635{
1636 struct filedesc *fdp;
1637 int i;
1638
1639 /* Certain daemons might not have file descriptors. */
1640 fdp = td->td_proc->p_fd;
1641 if (fdp == NULL)
1642 return;
1643
1644 /*
1645 * Note: fdp->fd_ofiles may be reallocated out from under us while
1646 * we are blocked in a close. Be careful!
1647 */
1648 FILEDESC_LOCK(fdp);
1649 for (i = 0; i <= fdp->fd_lastfile; i++) {
1650 if (i > 2)
1651 break;
1652 if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
1653 struct file *fp;
1654
1655 if (i < fdp->fd_knlistsize) {
1656 FILEDESC_UNLOCK(fdp);
1657 knote_fdclose(td, i);
1658 FILEDESC_LOCK(fdp);
1659 }
1660 /*
1661 * NULL-out descriptor prior to close to avoid
1662 * a race while close blocks.
1663 */
1664 fp = fdp->fd_ofiles[i];
1665 fdp->fd_ofiles[i] = NULL;
1666 fdp->fd_ofileflags[i] = 0;
1667 fdunused(fdp, i);
1668 FILEDESC_UNLOCK(fdp);
1669 (void) closef(fp, td);
1670 FILEDESC_LOCK(fdp);
1671 }
1672 }
1673 FILEDESC_UNLOCK(fdp);
1674}
1675
1676/*
1677 * Close any files on exec?
1678 */
1679void
1680fdcloseexec(td)
1681 struct thread *td;
1682{
1683 struct filedesc *fdp;
1684 int i;
1685
1686 /* Certain daemons might not have file descriptors. */
1687 fdp = td->td_proc->p_fd;
1688 if (fdp == NULL)
1689 return;
1690
1691 FILEDESC_LOCK(fdp);
1692
1693 /*
1694 * We cannot cache fd_ofiles or fd_ofileflags since operations
1695 * may block and rip them out from under us.
1696 */
1697 for (i = 0; i <= fdp->fd_lastfile; i++) {
1698 if (fdp->fd_ofiles[i] != NULL &&
1699 (fdp->fd_ofileflags[i] & UF_EXCLOSE)) {
1700 struct file *fp;
1701
1702 if (i < fdp->fd_knlistsize) {
1703 FILEDESC_UNLOCK(fdp);
1704 knote_fdclose(td, i);
1705 FILEDESC_LOCK(fdp);
1706 }
1707 /*
1708 * NULL-out descriptor prior to close to avoid
1709 * a race while close blocks.
1710 */
1711 fp = fdp->fd_ofiles[i];
1712 fdp->fd_ofiles[i] = NULL;
1713 fdp->fd_ofileflags[i] = 0;
1714 fdunused(fdp, i);
1715 FILEDESC_UNLOCK(fdp);
1716 (void) closef(fp, td);
1717 FILEDESC_LOCK(fdp);
1718 }
1719 }
1720 FILEDESC_UNLOCK(fdp);
1721}
1722
1723/*
1724 * It is unsafe for set[ug]id processes to be started with file
1725 * descriptors 0..2 closed, as these descriptors are given implicit
1726 * significance in the Standard C library. fdcheckstd() will create a
1727 * descriptor referencing /dev/null for each of stdin, stdout, and
1728 * stderr that is not already open.
1729 */
1730int
1731fdcheckstd(td)
1732 struct thread *td;
1733{
1734 struct nameidata nd;
1735 struct filedesc *fdp;
1736 struct file *fp;
1737 register_t retval;
1738 int fd, i, error, flags, devnull;
1739
1740 fdp = td->td_proc->p_fd;
1741 if (fdp == NULL)
1742 return (0);
1743 KASSERT(fdp->fd_refcnt == 1, ("the fdtable should not be shared"));
1744 devnull = -1;
1745 error = 0;
1746 for (i = 0; i < 3; i++) {
1747 if (fdp->fd_ofiles[i] != NULL)
1748 continue;
1749 if (devnull < 0) {
1750 error = falloc(td, &fp, &fd);
1751 if (error != 0)
1752 break;
1753 /* Note extra ref on `fp' held for us by falloc(). */
1754 KASSERT(fd == i, ("oof, we didn't get our fd"));
1755 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/null",
1756 td);
1757 flags = FREAD | FWRITE;
1758 error = vn_open(&nd, &flags, 0, -1);
1759 if (error != 0) {
1760 /*
1761 * Someone may have closed the entry in the
1762 * file descriptor table, so check it hasn't
1763 * changed before dropping the reference count.
1764 */
1765 FILEDESC_LOCK(fdp);
1766 KASSERT(fdp->fd_ofiles[fd] == fp,
1767 ("table not shared, how did it change?"));
1768 fdp->fd_ofiles[fd] = NULL;
1769 fdunused(fdp, fd);
1770 FILEDESC_UNLOCK(fdp);
1771 fdrop(fp, td);
1772 fdrop(fp, td);
1773 break;
1774 }
1775 NDFREE(&nd, NDF_ONLY_PNBUF);
1776 fp->f_vnode = nd.ni_vp;
1777 fp->f_data = nd.ni_vp;
1778 fp->f_flag = flags;
1779 fp->f_ops = &vnops;
1780 fp->f_type = DTYPE_VNODE;
1781 VOP_UNLOCK(nd.ni_vp, 0, td);
1782 devnull = fd;
1783 fdrop(fp, td);
1784 } else {
1785 error = do_dup(td, DUP_FIXED, devnull, i, &retval);
1786 if (error != 0)
1787 break;
1788 }
1789 }
1790 return (error);
1791}
1792
1793/*
1794 * Internal form of close.
1795 * Decrement reference count on file structure.
1796 * Note: td may be NULL when closing a file
1797 * that was being passed in a message.
1798 */
1799int
1800closef(fp, td)
1801 struct file *fp;
1802 struct thread *td;
1803{
1804 struct vnode *vp;
1805 struct flock lf;
1806 struct filedesc_to_leader *fdtol;
1807 struct filedesc *fdp;
1808
1809 if (fp == NULL)
1810 return (0);
1811 /*
1812 * POSIX record locking dictates that any close releases ALL
1813 * locks owned by this process. This is handled by setting
1814 * a flag in the unlock to free ONLY locks obeying POSIX
1815 * semantics, and not to free BSD-style file locks.
1816 * If the descriptor was in a message, POSIX-style locks
1817 * aren't passed with the descriptor.
1818 */
1819 if (td != NULL &&
1820 fp->f_type == DTYPE_VNODE) {
1821 if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1822 lf.l_whence = SEEK_SET;
1823 lf.l_start = 0;
1824 lf.l_len = 0;
1825 lf.l_type = F_UNLCK;
1826 vp = fp->f_vnode;
1827 (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader,
1828 F_UNLCK, &lf, F_POSIX);
1829 }
1830 fdtol = td->td_proc->p_fdtol;
1831 if (fdtol != NULL) {
1832 /*
1833 * Handle special case where file descriptor table
1834 * is shared between multiple process leaders.
1835 */
1836 fdp = td->td_proc->p_fd;
1837 FILEDESC_LOCK(fdp);
1838 for (fdtol = fdtol->fdl_next;
1839 fdtol != td->td_proc->p_fdtol;
1840 fdtol = fdtol->fdl_next) {
1841 if ((fdtol->fdl_leader->p_flag &
1842 P_ADVLOCK) == 0)
1843 continue;
1844 fdtol->fdl_holdcount++;
1845 FILEDESC_UNLOCK(fdp);
1846 lf.l_whence = SEEK_SET;
1847 lf.l_start = 0;
1848 lf.l_len = 0;
1849 lf.l_type = F_UNLCK;
1850 vp = fp->f_vnode;
1851 (void) VOP_ADVLOCK(vp,
1852 (caddr_t)fdtol->fdl_leader,
1853 F_UNLCK, &lf, F_POSIX);
1854 FILEDESC_LOCK(fdp);
1855 fdtol->fdl_holdcount--;
1856 if (fdtol->fdl_holdcount == 0 &&
1857 fdtol->fdl_wakeup != 0) {
1858 fdtol->fdl_wakeup = 0;
1859 wakeup(fdtol);
1860 }
1861 }
1862 FILEDESC_UNLOCK(fdp);
1863 }
1864 }
1865 return (fdrop(fp, td));
1866}
1867
1868/*
1869 * Drop reference on struct file passed in, may call closef if the
1870 * reference hits zero.
1871 */
1872int
1873fdrop(fp, td)
1874 struct file *fp;
1875 struct thread *td;
1876{
1877
1878 FILE_LOCK(fp);
1879 return (fdrop_locked(fp, td));
1880}
1881
1882/*
1883 * Extract the file pointer associated with the specified descriptor for
1884 * the current user process.
1885 *
1886 * If the descriptor doesn't exist, EBADF is returned.
1887 *
1888 * If the descriptor exists but doesn't match 'flags' then
1889 * return EBADF for read attempts and EINVAL for write attempts.
1890 *
1891 * If 'hold' is set (non-zero) the file's refcount will be bumped on return.
1892 * It should be droped with fdrop().
1893 * If it is not set, then the refcount will not be bumped however the
1894 * thread's filedesc struct will be returned locked (for fgetsock).
1895 *
1896 * If an error occured the non-zero error is returned and *fpp is set to NULL.
1897 * Otherwise *fpp is set and zero is returned.
1898 */
1899static __inline int
1900_fget(struct thread *td, int fd, struct file **fpp, int flags, int hold)
1901{
1902 struct filedesc *fdp;
1903 struct file *fp;
1904
1905 *fpp = NULL;
1906 if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
1907 return (EBADF);
1908 FILEDESC_LOCK(fdp);
1909 if ((fp = fget_locked(fdp, fd)) == NULL || fp->f_ops == &badfileops) {
1910 FILEDESC_UNLOCK(fdp);
1911 return (EBADF);
1912 }
1913
1914 /*
1915 * Note: FREAD failures returns EBADF to maintain backwards
1916 * compatibility with what routines returned before.
1917 *
1918 * Only one flag, or 0, may be specified.
1919 */
1920 if (flags == FREAD && (fp->f_flag & FREAD) == 0) {
1921 FILEDESC_UNLOCK(fdp);
1922 return (EBADF);
1923 }
1924 if (flags == FWRITE && (fp->f_flag & FWRITE) == 0) {
1925 FILEDESC_UNLOCK(fdp);
1926 return (EINVAL);
1927 }
1928 if (hold) {
1929 fhold(fp);
1930 FILEDESC_UNLOCK(fdp);
1931 }
1932 *fpp = fp;
1933 return (0);
1934}
1935
1936int
1937fget(struct thread *td, int fd, struct file **fpp)
1938{
1939
1940 return(_fget(td, fd, fpp, 0, 1));
1941}
1942
1943int
1944fget_read(struct thread *td, int fd, struct file **fpp)
1945{
1946
1947 return(_fget(td, fd, fpp, FREAD, 1));
1948}
1949
1950int
1951fget_write(struct thread *td, int fd, struct file **fpp)
1952{
1953
1954 return(_fget(td, fd, fpp, FWRITE, 1));
1955}
1956
1957/*
1958 * Like fget() but loads the underlying vnode, or returns an error if
1959 * the descriptor does not represent a vnode. Note that pipes use vnodes
1960 * but never have VM objects (so VOP_GETVOBJECT() calls will return an
1961 * error). The returned vnode will be vref()d.
1962 */
1963static __inline int
1964_fgetvp(struct thread *td, int fd, struct vnode **vpp, int flags)
1965{
1966 struct file *fp;
1967 int error;
1968
1969 *vpp = NULL;
1970 if ((error = _fget(td, fd, &fp, 0, 0)) != 0)
1971 return (error);
1972 if (fp->f_vnode == NULL) {
1973 error = EINVAL;
1974 } else {
1975 *vpp = fp->f_vnode;
1976 vref(*vpp);
1977 }
1978 FILEDESC_UNLOCK(td->td_proc->p_fd);
1979 return (error);
1980}
1981
1982int
1983fgetvp(struct thread *td, int fd, struct vnode **vpp)
1984{
1985
1986 return (_fgetvp(td, fd, vpp, 0));
1987}
1988
1989int
1990fgetvp_read(struct thread *td, int fd, struct vnode **vpp)
1991{
1992
1993 return (_fgetvp(td, fd, vpp, FREAD));
1994}
1995
1996int
1997fgetvp_write(struct thread *td, int fd, struct vnode **vpp)
1998{
1999
2000 return (_fgetvp(td, fd, vpp, FWRITE));
2001}
2002
2003/*
2004 * Like fget() but loads the underlying socket, or returns an error if
2005 * the descriptor does not represent a socket.
2006 *
2007 * We bump the ref count on the returned socket. XXX Also obtain the SX
2008 * lock in the future.
2009 */
2010int
2011fgetsock(struct thread *td, int fd, struct socket **spp, u_int *fflagp)
2012{
2013 struct file *fp;
2014 int error;
2015
2016 *spp = NULL;
2017 if (fflagp != NULL)
2018 *fflagp = 0;
2019 if ((error = _fget(td, fd, &fp, 0, 0)) != 0)
2020 return (error);
2021 if (fp->f_type != DTYPE_SOCKET) {
2022 error = ENOTSOCK;
2023 } else {
2024 *spp = fp->f_data;
2025 if (fflagp)
2026 *fflagp = fp->f_flag;
2027 SOCK_LOCK(*spp);
2027 soref(*spp);
2028 soref(*spp);
2029 SOCK_UNLOCK(*spp);
2028 }
2029 FILEDESC_UNLOCK(td->td_proc->p_fd);
2030 return (error);
2031}
2032
2033/*
2034 * Drop the reference count on the the socket and XXX release the SX lock in
2035 * the future. The last reference closes the socket.
2036 */
2037void
2038fputsock(struct socket *so)
2039{
2040
2041 NET_ASSERT_GIANT();
2030 }
2031 FILEDESC_UNLOCK(td->td_proc->p_fd);
2032 return (error);
2033}
2034
2035/*
2036 * Drop the reference count on the the socket and XXX release the SX lock in
2037 * the future. The last reference closes the socket.
2038 */
2039void
2040fputsock(struct socket *so)
2041{
2042
2043 NET_ASSERT_GIANT();
2044 SOCK_LOCK(so);
2042 sorele(so);
2043}
2044
2045/*
2046 * Drop reference on struct file passed in, may call closef if the
2047 * reference hits zero.
2048 * Expects struct file locked, and will unlock it.
2049 */
2050int
2051fdrop_locked(fp, td)
2052 struct file *fp;
2053 struct thread *td;
2054{
2055 int error;
2056
2057 FILE_LOCK_ASSERT(fp, MA_OWNED);
2058
2059 if (--fp->f_count > 0) {
2060 FILE_UNLOCK(fp);
2061 return (0);
2062 }
2063 /* We have the last ref so we can proceed without the file lock. */
2064 FILE_UNLOCK(fp);
2065 if (fp->f_count < 0)
2066 panic("fdrop: count < 0");
2067 mtx_lock(&Giant);
2068 if (fp->f_ops != &badfileops)
2069 error = fo_close(fp, td);
2070 else
2071 error = 0;
2072 ffree(fp);
2073 mtx_unlock(&Giant);
2074 return (error);
2075}
2076
2077/*
2078 * Apply an advisory lock on a file descriptor.
2079 *
2080 * Just attempt to get a record lock of the requested type on
2081 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
2082 */
2083#ifndef _SYS_SYSPROTO_H_
2084struct flock_args {
2085 int fd;
2086 int how;
2087};
2088#endif
2089/*
2090 * MPSAFE
2091 */
2092/* ARGSUSED */
2093int
2094flock(td, uap)
2095 struct thread *td;
2096 struct flock_args *uap;
2097{
2098 struct file *fp;
2099 struct vnode *vp;
2100 struct flock lf;
2101 int error;
2102
2103 if ((error = fget(td, uap->fd, &fp)) != 0)
2104 return (error);
2105 if (fp->f_type != DTYPE_VNODE) {
2106 fdrop(fp, td);
2107 return (EOPNOTSUPP);
2108 }
2109
2110 mtx_lock(&Giant);
2111 vp = fp->f_vnode;
2112 lf.l_whence = SEEK_SET;
2113 lf.l_start = 0;
2114 lf.l_len = 0;
2115 if (uap->how & LOCK_UN) {
2116 lf.l_type = F_UNLCK;
2117 FILE_LOCK(fp);
2118 fp->f_flag &= ~FHASLOCK;
2119 FILE_UNLOCK(fp);
2120 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
2121 goto done2;
2122 }
2123 if (uap->how & LOCK_EX)
2124 lf.l_type = F_WRLCK;
2125 else if (uap->how & LOCK_SH)
2126 lf.l_type = F_RDLCK;
2127 else {
2128 error = EBADF;
2129 goto done2;
2130 }
2131 FILE_LOCK(fp);
2132 fp->f_flag |= FHASLOCK;
2133 FILE_UNLOCK(fp);
2134 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
2135 (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
2136done2:
2137 fdrop(fp, td);
2138 mtx_unlock(&Giant);
2139 return (error);
2140}
2141
2142/*
2143 * File Descriptor pseudo-device driver (/dev/fd/).
2144 *
2145 * Opening minor device N dup()s the file (if any) connected to file
2146 * descriptor N belonging to the calling process. Note that this driver
2147 * consists of only the ``open()'' routine, because all subsequent
2148 * references to this file will be direct to the other driver.
2149 */
2150/* ARGSUSED */
2151static int
2152fdopen(dev, mode, type, td)
2153 dev_t dev;
2154 int mode, type;
2155 struct thread *td;
2156{
2157
2158 /*
2159 * XXX Kludge: set curthread->td_dupfd to contain the value of the
2160 * the file descriptor being sought for duplication. The error
2161 * return ensures that the vnode for this device will be released
2162 * by vn_open. Open will detect this special error and take the
2163 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
2164 * will simply report the error.
2165 */
2166 td->td_dupfd = dev2unit(dev);
2167 return (ENODEV);
2168}
2169
2170/*
2171 * Duplicate the specified descriptor to a free descriptor.
2172 */
2173int
2174dupfdopen(td, fdp, indx, dfd, mode, error)
2175 struct thread *td;
2176 struct filedesc *fdp;
2177 int indx, dfd;
2178 int mode;
2179 int error;
2180{
2181 struct file *wfp;
2182 struct file *fp;
2183
2184 /*
2185 * If the to-be-dup'd fd number is greater than the allowed number
2186 * of file descriptors, or the fd to be dup'd has already been
2187 * closed, then reject.
2188 */
2189 FILEDESC_LOCK(fdp);
2190 if (dfd < 0 || dfd >= fdp->fd_nfiles ||
2191 (wfp = fdp->fd_ofiles[dfd]) == NULL) {
2192 FILEDESC_UNLOCK(fdp);
2193 return (EBADF);
2194 }
2195
2196 /*
2197 * There are two cases of interest here.
2198 *
2199 * For ENODEV simply dup (dfd) to file descriptor
2200 * (indx) and return.
2201 *
2202 * For ENXIO steal away the file structure from (dfd) and
2203 * store it in (indx). (dfd) is effectively closed by
2204 * this operation.
2205 *
2206 * Any other error code is just returned.
2207 */
2208 switch (error) {
2209 case ENODEV:
2210 /*
2211 * Check that the mode the file is being opened for is a
2212 * subset of the mode of the existing descriptor.
2213 */
2214 FILE_LOCK(wfp);
2215 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
2216 FILE_UNLOCK(wfp);
2217 FILEDESC_UNLOCK(fdp);
2218 return (EACCES);
2219 }
2220 fp = fdp->fd_ofiles[indx];
2221 fdp->fd_ofiles[indx] = wfp;
2222 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2223 if (fp == NULL)
2224 fdused(fdp, indx);
2225 fhold_locked(wfp);
2226 FILE_UNLOCK(wfp);
2227 if (fp != NULL)
2228 FILE_LOCK(fp);
2229 FILEDESC_UNLOCK(fdp);
2230 /*
2231 * We now own the reference to fp that the ofiles[] array
2232 * used to own. Release it.
2233 */
2234 if (fp != NULL)
2235 fdrop_locked(fp, td);
2236 return (0);
2237
2238 case ENXIO:
2239 /*
2240 * Steal away the file pointer from dfd and stuff it into indx.
2241 */
2242 fp = fdp->fd_ofiles[indx];
2243 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
2244 fdp->fd_ofiles[dfd] = NULL;
2245 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2246 fdp->fd_ofileflags[dfd] = 0;
2247 fdunused(fdp, dfd);
2248 if (fp == NULL)
2249 fdused(fdp, indx);
2250 if (fp != NULL)
2251 FILE_LOCK(fp);
2252 FILEDESC_UNLOCK(fdp);
2253
2254 /*
2255 * we now own the reference to fp that the ofiles[] array
2256 * used to own. Release it.
2257 */
2258 if (fp != NULL)
2259 fdrop_locked(fp, td);
2260 return (0);
2261
2262 default:
2263 FILEDESC_UNLOCK(fdp);
2264 return (error);
2265 }
2266 /* NOTREACHED */
2267}
2268
2269struct filedesc_to_leader *
2270filedesc_to_leader_alloc(struct filedesc_to_leader *old,
2271 struct filedesc *fdp,
2272 struct proc *leader)
2273{
2274 struct filedesc_to_leader *fdtol;
2275
2276 MALLOC(fdtol, struct filedesc_to_leader *,
2277 sizeof(struct filedesc_to_leader),
2278 M_FILEDESC_TO_LEADER,
2279 M_WAITOK);
2280 fdtol->fdl_refcount = 1;
2281 fdtol->fdl_holdcount = 0;
2282 fdtol->fdl_wakeup = 0;
2283 fdtol->fdl_leader = leader;
2284 if (old != NULL) {
2285 FILEDESC_LOCK(fdp);
2286 fdtol->fdl_next = old->fdl_next;
2287 fdtol->fdl_prev = old;
2288 old->fdl_next = fdtol;
2289 fdtol->fdl_next->fdl_prev = fdtol;
2290 FILEDESC_UNLOCK(fdp);
2291 } else {
2292 fdtol->fdl_next = fdtol;
2293 fdtol->fdl_prev = fdtol;
2294 }
2295 return (fdtol);
2296}
2297
2298/*
2299 * Get file structures.
2300 */
2301static int
2302sysctl_kern_file(SYSCTL_HANDLER_ARGS)
2303{
2304 struct xfile xf;
2305 struct filedesc *fdp;
2306 struct file *fp;
2307 struct proc *p;
2308 int error, n;
2309
2310 /*
2311 * Note: because the number of file descriptors is calculated
2312 * in different ways for sizing vs returning the data,
2313 * there is information leakage from the first loop. However,
2314 * it is of a similar order of magnitude to the leakage from
2315 * global system statistics such as kern.openfiles.
2316 */
2317 error = sysctl_wire_old_buffer(req, 0);
2318 if (error != 0)
2319 return (error);
2320 if (req->oldptr == NULL) {
2321 n = 16; /* A slight overestimate. */
2322 sx_slock(&filelist_lock);
2323 LIST_FOREACH(fp, &filehead, f_list) {
2324 /*
2325 * We should grab the lock, but this is an
2326 * estimate, so does it really matter?
2327 */
2328 /* mtx_lock(fp->f_mtxp); */
2329 n += fp->f_count;
2330 /* mtx_unlock(f->f_mtxp); */
2331 }
2332 sx_sunlock(&filelist_lock);
2333 return (SYSCTL_OUT(req, 0, n * sizeof(xf)));
2334 }
2335 error = 0;
2336 bzero(&xf, sizeof(xf));
2337 xf.xf_size = sizeof(xf);
2338 sx_slock(&allproc_lock);
2339 LIST_FOREACH(p, &allproc, p_list) {
2340 PROC_LOCK(p);
2341 if (p_cansee(req->td, p) != 0) {
2342 PROC_UNLOCK(p);
2343 continue;
2344 }
2345 xf.xf_pid = p->p_pid;
2346 xf.xf_uid = p->p_ucred->cr_uid;
2347 PROC_UNLOCK(p);
2348 mtx_lock(&fdesc_mtx);
2349 if ((fdp = p->p_fd) == NULL) {
2350 mtx_unlock(&fdesc_mtx);
2351 continue;
2352 }
2353 FILEDESC_LOCK(fdp);
2354 for (n = 0; n < fdp->fd_nfiles; ++n) {
2355 if ((fp = fdp->fd_ofiles[n]) == NULL)
2356 continue;
2357 xf.xf_fd = n;
2358 xf.xf_file = fp;
2359 xf.xf_data = fp->f_data;
2360 xf.xf_type = fp->f_type;
2361 xf.xf_count = fp->f_count;
2362 xf.xf_msgcount = fp->f_msgcount;
2363 xf.xf_offset = fp->f_offset;
2364 xf.xf_flag = fp->f_flag;
2365 error = SYSCTL_OUT(req, &xf, sizeof(xf));
2366 if (error)
2367 break;
2368 }
2369 FILEDESC_UNLOCK(fdp);
2370 mtx_unlock(&fdesc_mtx);
2371 if (error)
2372 break;
2373 }
2374 sx_sunlock(&allproc_lock);
2375 return (error);
2376}
2377
2378SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
2379 0, 0, sysctl_kern_file, "S,xfile", "Entire file table");
2380
2381SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
2382 &maxfilesperproc, 0, "Maximum files allowed open per process");
2383
2384SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
2385 &maxfiles, 0, "Maximum number of files");
2386
2387SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
2388 &nfiles, 0, "System-wide number of open files");
2389
2390static void
2391fildesc_drvinit(void *unused)
2392{
2393 dev_t dev;
2394
2395 dev = make_dev(&fildesc_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "fd/0");
2396 make_dev_alias(dev, "stdin");
2397 dev = make_dev(&fildesc_cdevsw, 1, UID_ROOT, GID_WHEEL, 0666, "fd/1");
2398 make_dev_alias(dev, "stdout");
2399 dev = make_dev(&fildesc_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "fd/2");
2400 make_dev_alias(dev, "stderr");
2401}
2402
2403static fo_rdwr_t badfo_readwrite;
2404static fo_ioctl_t badfo_ioctl;
2405static fo_poll_t badfo_poll;
2406static fo_kqfilter_t badfo_kqfilter;
2407static fo_stat_t badfo_stat;
2408static fo_close_t badfo_close;
2409
2410struct fileops badfileops = {
2411 .fo_read = badfo_readwrite,
2412 .fo_write = badfo_readwrite,
2413 .fo_ioctl = badfo_ioctl,
2414 .fo_poll = badfo_poll,
2415 .fo_kqfilter = badfo_kqfilter,
2416 .fo_stat = badfo_stat,
2417 .fo_close = badfo_close,
2418};
2419
2420static int
2421badfo_readwrite(fp, uio, active_cred, flags, td)
2422 struct file *fp;
2423 struct uio *uio;
2424 struct ucred *active_cred;
2425 struct thread *td;
2426 int flags;
2427{
2428
2429 return (EBADF);
2430}
2431
2432static int
2433badfo_ioctl(fp, com, data, active_cred, td)
2434 struct file *fp;
2435 u_long com;
2436 void *data;
2437 struct ucred *active_cred;
2438 struct thread *td;
2439{
2440
2441 return (EBADF);
2442}
2443
2444static int
2445badfo_poll(fp, events, active_cred, td)
2446 struct file *fp;
2447 int events;
2448 struct ucred *active_cred;
2449 struct thread *td;
2450{
2451
2452 return (0);
2453}
2454
2455static int
2456badfo_kqfilter(fp, kn)
2457 struct file *fp;
2458 struct knote *kn;
2459{
2460
2461 return (0);
2462}
2463
2464static int
2465badfo_stat(fp, sb, active_cred, td)
2466 struct file *fp;
2467 struct stat *sb;
2468 struct ucred *active_cred;
2469 struct thread *td;
2470{
2471
2472 return (EBADF);
2473}
2474
2475static int
2476badfo_close(fp, td)
2477 struct file *fp;
2478 struct thread *td;
2479{
2480
2481 return (EBADF);
2482}
2483
2484SYSINIT(fildescdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,
2485 fildesc_drvinit,NULL)
2486
2487static void filelistinit(void *);
2488SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL)
2489
2490/* ARGSUSED*/
2491static void
2492filelistinit(dummy)
2493 void *dummy;
2494{
2495
2496 file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL,
2497 NULL, NULL, UMA_ALIGN_PTR, 0);
2498 sx_init(&filelist_lock, "filelist lock");
2499 mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF);
2500}
2045 sorele(so);
2046}
2047
2048/*
2049 * Drop reference on struct file passed in, may call closef if the
2050 * reference hits zero.
2051 * Expects struct file locked, and will unlock it.
2052 */
2053int
2054fdrop_locked(fp, td)
2055 struct file *fp;
2056 struct thread *td;
2057{
2058 int error;
2059
2060 FILE_LOCK_ASSERT(fp, MA_OWNED);
2061
2062 if (--fp->f_count > 0) {
2063 FILE_UNLOCK(fp);
2064 return (0);
2065 }
2066 /* We have the last ref so we can proceed without the file lock. */
2067 FILE_UNLOCK(fp);
2068 if (fp->f_count < 0)
2069 panic("fdrop: count < 0");
2070 mtx_lock(&Giant);
2071 if (fp->f_ops != &badfileops)
2072 error = fo_close(fp, td);
2073 else
2074 error = 0;
2075 ffree(fp);
2076 mtx_unlock(&Giant);
2077 return (error);
2078}
2079
2080/*
2081 * Apply an advisory lock on a file descriptor.
2082 *
2083 * Just attempt to get a record lock of the requested type on
2084 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
2085 */
2086#ifndef _SYS_SYSPROTO_H_
2087struct flock_args {
2088 int fd;
2089 int how;
2090};
2091#endif
2092/*
2093 * MPSAFE
2094 */
2095/* ARGSUSED */
2096int
2097flock(td, uap)
2098 struct thread *td;
2099 struct flock_args *uap;
2100{
2101 struct file *fp;
2102 struct vnode *vp;
2103 struct flock lf;
2104 int error;
2105
2106 if ((error = fget(td, uap->fd, &fp)) != 0)
2107 return (error);
2108 if (fp->f_type != DTYPE_VNODE) {
2109 fdrop(fp, td);
2110 return (EOPNOTSUPP);
2111 }
2112
2113 mtx_lock(&Giant);
2114 vp = fp->f_vnode;
2115 lf.l_whence = SEEK_SET;
2116 lf.l_start = 0;
2117 lf.l_len = 0;
2118 if (uap->how & LOCK_UN) {
2119 lf.l_type = F_UNLCK;
2120 FILE_LOCK(fp);
2121 fp->f_flag &= ~FHASLOCK;
2122 FILE_UNLOCK(fp);
2123 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
2124 goto done2;
2125 }
2126 if (uap->how & LOCK_EX)
2127 lf.l_type = F_WRLCK;
2128 else if (uap->how & LOCK_SH)
2129 lf.l_type = F_RDLCK;
2130 else {
2131 error = EBADF;
2132 goto done2;
2133 }
2134 FILE_LOCK(fp);
2135 fp->f_flag |= FHASLOCK;
2136 FILE_UNLOCK(fp);
2137 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
2138 (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
2139done2:
2140 fdrop(fp, td);
2141 mtx_unlock(&Giant);
2142 return (error);
2143}
2144
2145/*
2146 * File Descriptor pseudo-device driver (/dev/fd/).
2147 *
2148 * Opening minor device N dup()s the file (if any) connected to file
2149 * descriptor N belonging to the calling process. Note that this driver
2150 * consists of only the ``open()'' routine, because all subsequent
2151 * references to this file will be direct to the other driver.
2152 */
2153/* ARGSUSED */
2154static int
2155fdopen(dev, mode, type, td)
2156 dev_t dev;
2157 int mode, type;
2158 struct thread *td;
2159{
2160
2161 /*
2162 * XXX Kludge: set curthread->td_dupfd to contain the value of the
2163 * the file descriptor being sought for duplication. The error
2164 * return ensures that the vnode for this device will be released
2165 * by vn_open. Open will detect this special error and take the
2166 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
2167 * will simply report the error.
2168 */
2169 td->td_dupfd = dev2unit(dev);
2170 return (ENODEV);
2171}
2172
2173/*
2174 * Duplicate the specified descriptor to a free descriptor.
2175 */
2176int
2177dupfdopen(td, fdp, indx, dfd, mode, error)
2178 struct thread *td;
2179 struct filedesc *fdp;
2180 int indx, dfd;
2181 int mode;
2182 int error;
2183{
2184 struct file *wfp;
2185 struct file *fp;
2186
2187 /*
2188 * If the to-be-dup'd fd number is greater than the allowed number
2189 * of file descriptors, or the fd to be dup'd has already been
2190 * closed, then reject.
2191 */
2192 FILEDESC_LOCK(fdp);
2193 if (dfd < 0 || dfd >= fdp->fd_nfiles ||
2194 (wfp = fdp->fd_ofiles[dfd]) == NULL) {
2195 FILEDESC_UNLOCK(fdp);
2196 return (EBADF);
2197 }
2198
2199 /*
2200 * There are two cases of interest here.
2201 *
2202 * For ENODEV simply dup (dfd) to file descriptor
2203 * (indx) and return.
2204 *
2205 * For ENXIO steal away the file structure from (dfd) and
2206 * store it in (indx). (dfd) is effectively closed by
2207 * this operation.
2208 *
2209 * Any other error code is just returned.
2210 */
2211 switch (error) {
2212 case ENODEV:
2213 /*
2214 * Check that the mode the file is being opened for is a
2215 * subset of the mode of the existing descriptor.
2216 */
2217 FILE_LOCK(wfp);
2218 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
2219 FILE_UNLOCK(wfp);
2220 FILEDESC_UNLOCK(fdp);
2221 return (EACCES);
2222 }
2223 fp = fdp->fd_ofiles[indx];
2224 fdp->fd_ofiles[indx] = wfp;
2225 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2226 if (fp == NULL)
2227 fdused(fdp, indx);
2228 fhold_locked(wfp);
2229 FILE_UNLOCK(wfp);
2230 if (fp != NULL)
2231 FILE_LOCK(fp);
2232 FILEDESC_UNLOCK(fdp);
2233 /*
2234 * We now own the reference to fp that the ofiles[] array
2235 * used to own. Release it.
2236 */
2237 if (fp != NULL)
2238 fdrop_locked(fp, td);
2239 return (0);
2240
2241 case ENXIO:
2242 /*
2243 * Steal away the file pointer from dfd and stuff it into indx.
2244 */
2245 fp = fdp->fd_ofiles[indx];
2246 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
2247 fdp->fd_ofiles[dfd] = NULL;
2248 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2249 fdp->fd_ofileflags[dfd] = 0;
2250 fdunused(fdp, dfd);
2251 if (fp == NULL)
2252 fdused(fdp, indx);
2253 if (fp != NULL)
2254 FILE_LOCK(fp);
2255 FILEDESC_UNLOCK(fdp);
2256
2257 /*
2258 * we now own the reference to fp that the ofiles[] array
2259 * used to own. Release it.
2260 */
2261 if (fp != NULL)
2262 fdrop_locked(fp, td);
2263 return (0);
2264
2265 default:
2266 FILEDESC_UNLOCK(fdp);
2267 return (error);
2268 }
2269 /* NOTREACHED */
2270}
2271
2272struct filedesc_to_leader *
2273filedesc_to_leader_alloc(struct filedesc_to_leader *old,
2274 struct filedesc *fdp,
2275 struct proc *leader)
2276{
2277 struct filedesc_to_leader *fdtol;
2278
2279 MALLOC(fdtol, struct filedesc_to_leader *,
2280 sizeof(struct filedesc_to_leader),
2281 M_FILEDESC_TO_LEADER,
2282 M_WAITOK);
2283 fdtol->fdl_refcount = 1;
2284 fdtol->fdl_holdcount = 0;
2285 fdtol->fdl_wakeup = 0;
2286 fdtol->fdl_leader = leader;
2287 if (old != NULL) {
2288 FILEDESC_LOCK(fdp);
2289 fdtol->fdl_next = old->fdl_next;
2290 fdtol->fdl_prev = old;
2291 old->fdl_next = fdtol;
2292 fdtol->fdl_next->fdl_prev = fdtol;
2293 FILEDESC_UNLOCK(fdp);
2294 } else {
2295 fdtol->fdl_next = fdtol;
2296 fdtol->fdl_prev = fdtol;
2297 }
2298 return (fdtol);
2299}
2300
2301/*
2302 * Get file structures.
2303 */
2304static int
2305sysctl_kern_file(SYSCTL_HANDLER_ARGS)
2306{
2307 struct xfile xf;
2308 struct filedesc *fdp;
2309 struct file *fp;
2310 struct proc *p;
2311 int error, n;
2312
2313 /*
2314 * Note: because the number of file descriptors is calculated
2315 * in different ways for sizing vs returning the data,
2316 * there is information leakage from the first loop. However,
2317 * it is of a similar order of magnitude to the leakage from
2318 * global system statistics such as kern.openfiles.
2319 */
2320 error = sysctl_wire_old_buffer(req, 0);
2321 if (error != 0)
2322 return (error);
2323 if (req->oldptr == NULL) {
2324 n = 16; /* A slight overestimate. */
2325 sx_slock(&filelist_lock);
2326 LIST_FOREACH(fp, &filehead, f_list) {
2327 /*
2328 * We should grab the lock, but this is an
2329 * estimate, so does it really matter?
2330 */
2331 /* mtx_lock(fp->f_mtxp); */
2332 n += fp->f_count;
2333 /* mtx_unlock(f->f_mtxp); */
2334 }
2335 sx_sunlock(&filelist_lock);
2336 return (SYSCTL_OUT(req, 0, n * sizeof(xf)));
2337 }
2338 error = 0;
2339 bzero(&xf, sizeof(xf));
2340 xf.xf_size = sizeof(xf);
2341 sx_slock(&allproc_lock);
2342 LIST_FOREACH(p, &allproc, p_list) {
2343 PROC_LOCK(p);
2344 if (p_cansee(req->td, p) != 0) {
2345 PROC_UNLOCK(p);
2346 continue;
2347 }
2348 xf.xf_pid = p->p_pid;
2349 xf.xf_uid = p->p_ucred->cr_uid;
2350 PROC_UNLOCK(p);
2351 mtx_lock(&fdesc_mtx);
2352 if ((fdp = p->p_fd) == NULL) {
2353 mtx_unlock(&fdesc_mtx);
2354 continue;
2355 }
2356 FILEDESC_LOCK(fdp);
2357 for (n = 0; n < fdp->fd_nfiles; ++n) {
2358 if ((fp = fdp->fd_ofiles[n]) == NULL)
2359 continue;
2360 xf.xf_fd = n;
2361 xf.xf_file = fp;
2362 xf.xf_data = fp->f_data;
2363 xf.xf_type = fp->f_type;
2364 xf.xf_count = fp->f_count;
2365 xf.xf_msgcount = fp->f_msgcount;
2366 xf.xf_offset = fp->f_offset;
2367 xf.xf_flag = fp->f_flag;
2368 error = SYSCTL_OUT(req, &xf, sizeof(xf));
2369 if (error)
2370 break;
2371 }
2372 FILEDESC_UNLOCK(fdp);
2373 mtx_unlock(&fdesc_mtx);
2374 if (error)
2375 break;
2376 }
2377 sx_sunlock(&allproc_lock);
2378 return (error);
2379}
2380
2381SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
2382 0, 0, sysctl_kern_file, "S,xfile", "Entire file table");
2383
2384SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
2385 &maxfilesperproc, 0, "Maximum files allowed open per process");
2386
2387SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
2388 &maxfiles, 0, "Maximum number of files");
2389
2390SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
2391 &nfiles, 0, "System-wide number of open files");
2392
2393static void
2394fildesc_drvinit(void *unused)
2395{
2396 dev_t dev;
2397
2398 dev = make_dev(&fildesc_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "fd/0");
2399 make_dev_alias(dev, "stdin");
2400 dev = make_dev(&fildesc_cdevsw, 1, UID_ROOT, GID_WHEEL, 0666, "fd/1");
2401 make_dev_alias(dev, "stdout");
2402 dev = make_dev(&fildesc_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "fd/2");
2403 make_dev_alias(dev, "stderr");
2404}
2405
2406static fo_rdwr_t badfo_readwrite;
2407static fo_ioctl_t badfo_ioctl;
2408static fo_poll_t badfo_poll;
2409static fo_kqfilter_t badfo_kqfilter;
2410static fo_stat_t badfo_stat;
2411static fo_close_t badfo_close;
2412
2413struct fileops badfileops = {
2414 .fo_read = badfo_readwrite,
2415 .fo_write = badfo_readwrite,
2416 .fo_ioctl = badfo_ioctl,
2417 .fo_poll = badfo_poll,
2418 .fo_kqfilter = badfo_kqfilter,
2419 .fo_stat = badfo_stat,
2420 .fo_close = badfo_close,
2421};
2422
2423static int
2424badfo_readwrite(fp, uio, active_cred, flags, td)
2425 struct file *fp;
2426 struct uio *uio;
2427 struct ucred *active_cred;
2428 struct thread *td;
2429 int flags;
2430{
2431
2432 return (EBADF);
2433}
2434
2435static int
2436badfo_ioctl(fp, com, data, active_cred, td)
2437 struct file *fp;
2438 u_long com;
2439 void *data;
2440 struct ucred *active_cred;
2441 struct thread *td;
2442{
2443
2444 return (EBADF);
2445}
2446
2447static int
2448badfo_poll(fp, events, active_cred, td)
2449 struct file *fp;
2450 int events;
2451 struct ucred *active_cred;
2452 struct thread *td;
2453{
2454
2455 return (0);
2456}
2457
2458static int
2459badfo_kqfilter(fp, kn)
2460 struct file *fp;
2461 struct knote *kn;
2462{
2463
2464 return (0);
2465}
2466
2467static int
2468badfo_stat(fp, sb, active_cred, td)
2469 struct file *fp;
2470 struct stat *sb;
2471 struct ucred *active_cred;
2472 struct thread *td;
2473{
2474
2475 return (EBADF);
2476}
2477
2478static int
2479badfo_close(fp, td)
2480 struct file *fp;
2481 struct thread *td;
2482{
2483
2484 return (EBADF);
2485}
2486
2487SYSINIT(fildescdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,
2488 fildesc_drvinit,NULL)
2489
2490static void filelistinit(void *);
2491SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL)
2492
2493/* ARGSUSED*/
2494static void
2495filelistinit(dummy)
2496 void *dummy;
2497{
2498
2499 file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL,
2500 NULL, NULL, UMA_ALIGN_PTR, 0);
2501 sx_init(&filelist_lock, "filelist lock");
2502 mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF);
2503}