kern_descrip.c revision 1.231.8.5 1 /* $NetBSD: kern_descrip.c,v 1.231.8.5 2018/11/26 01:52:50 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1989, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
35 * (c) UNIX System Laboratories, Inc.
36 * All or some portions of this file are derived from material licensed
37 * to the University of California by American Telephone and Telegraph
38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39 * the permission of UNIX System Laboratories, Inc.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
66 */
67
68 /*
69 * File descriptor management.
70 */
71
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.231.8.5 2018/11/26 01:52:50 pgoyette Exp $");
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/filedesc.h>
78 #include <sys/kernel.h>
79 #include <sys/proc.h>
80 #include <sys/file.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/stat.h>
84 #include <sys/ioctl.h>
85 #include <sys/fcntl.h>
86 #include <sys/pool.h>
87 #include <sys/unistd.h>
88 #include <sys/resourcevar.h>
89 #include <sys/conf.h>
90 #include <sys/event.h>
91 #include <sys/kauth.h>
92 #include <sys/atomic.h>
93 #include <sys/syscallargs.h>
94 #include <sys/cpu.h>
95 #include <sys/kmem.h>
96 #include <sys/vnode.h>
97 #include <sys/sysctl.h>
98 #include <sys/ktrace.h>
99
100 /*
101 * A list (head) of open files, counter, and lock protecting them.
102 */
103 struct filelist filehead __cacheline_aligned;
104 static u_int nfiles __cacheline_aligned;
105 kmutex_t filelist_lock __cacheline_aligned;
106
107 static pool_cache_t filedesc_cache __read_mostly;
108 static pool_cache_t file_cache __read_mostly;
109 static pool_cache_t fdfile_cache __read_mostly;
110
111 static int file_ctor(void *, void *, int);
112 static void file_dtor(void *, void *);
113 static int fdfile_ctor(void *, void *, int);
114 static void fdfile_dtor(void *, void *);
115 static int filedesc_ctor(void *, void *, int);
116 static void filedesc_dtor(void *, void *);
117 static int filedescopen(dev_t, int, int, lwp_t *);
118
119 static int sysctl_kern_file(SYSCTLFN_PROTO);
120 static int sysctl_kern_file2(SYSCTLFN_PROTO);
121 static void fill_file(struct file *, const struct file *);
122 static void fill_file2(struct kinfo_file *, const file_t *, const fdfile_t *,
123 int, pid_t);
124
125 const struct cdevsw filedesc_cdevsw = {
126 .d_open = filedescopen,
127 .d_close = noclose,
128 .d_read = noread,
129 .d_write = nowrite,
130 .d_ioctl = noioctl,
131 .d_stop = nostop,
132 .d_tty = notty,
133 .d_poll = nopoll,
134 .d_mmap = nommap,
135 .d_kqfilter = nokqfilter,
136 .d_discard = nodiscard,
137 .d_flag = D_OTHER | D_MPSAFE
138 };
139
140 /* For ease of reading. */
141 __strong_alias(fd_putvnode,fd_putfile)
142 __strong_alias(fd_putsock,fd_putfile)
143
144 /*
145 * Initialize the descriptor system.
146 */
147 void
148 fd_sys_init(void)
149 {
150 static struct sysctllog *clog;
151
152 mutex_init(&filelist_lock, MUTEX_DEFAULT, IPL_NONE);
153
154 LIST_INIT(&filehead);
155
156 file_cache = pool_cache_init(sizeof(file_t), coherency_unit, 0,
157 0, "file", NULL, IPL_NONE, file_ctor, file_dtor, NULL);
158 KASSERT(file_cache != NULL);
159
160 fdfile_cache = pool_cache_init(sizeof(fdfile_t), coherency_unit, 0,
161 PR_LARGECACHE, "fdfile", NULL, IPL_NONE, fdfile_ctor, fdfile_dtor,
162 NULL);
163 KASSERT(fdfile_cache != NULL);
164
165 filedesc_cache = pool_cache_init(sizeof(filedesc_t), coherency_unit,
166 0, 0, "filedesc", NULL, IPL_NONE, filedesc_ctor, filedesc_dtor,
167 NULL);
168 KASSERT(filedesc_cache != NULL);
169
170 sysctl_createv(&clog, 0, NULL, NULL,
171 CTLFLAG_PERMANENT,
172 CTLTYPE_STRUCT, "file",
173 SYSCTL_DESCR("System open file table"),
174 sysctl_kern_file, 0, NULL, 0,
175 CTL_KERN, KERN_FILE, CTL_EOL);
176 sysctl_createv(&clog, 0, NULL, NULL,
177 CTLFLAG_PERMANENT,
178 CTLTYPE_STRUCT, "file2",
179 SYSCTL_DESCR("System open file table"),
180 sysctl_kern_file2, 0, NULL, 0,
181 CTL_KERN, KERN_FILE2, CTL_EOL);
182 }
183
184 static bool
185 fd_isused(filedesc_t *fdp, unsigned fd)
186 {
187 u_int off = fd >> NDENTRYSHIFT;
188
189 KASSERT(fd < fdp->fd_dt->dt_nfiles);
190
191 return (fdp->fd_lomap[off] & (1U << (fd & NDENTRYMASK))) != 0;
192 }
193
194 /*
195 * Verify that the bitmaps match the descriptor table.
196 */
197 static inline void
198 fd_checkmaps(filedesc_t *fdp)
199 {
200 #ifdef DEBUG
201 fdtab_t *dt;
202 u_int fd;
203
204 dt = fdp->fd_dt;
205 if (fdp->fd_refcnt == -1) {
206 /*
207 * fd_free tears down the table without maintaining its bitmap.
208 */
209 return;
210 }
211 for (fd = 0; fd < dt->dt_nfiles; fd++) {
212 if (fd < NDFDFILE) {
213 KASSERT(dt->dt_ff[fd] ==
214 (fdfile_t *)fdp->fd_dfdfile[fd]);
215 }
216 if (dt->dt_ff[fd] == NULL) {
217 KASSERT(!fd_isused(fdp, fd));
218 } else if (dt->dt_ff[fd]->ff_file != NULL) {
219 KASSERT(fd_isused(fdp, fd));
220 }
221 }
222 #endif
223 }
224
225 static int
226 fd_next_zero(filedesc_t *fdp, uint32_t *bitmap, int want, u_int bits)
227 {
228 int i, off, maxoff;
229 uint32_t sub;
230
231 KASSERT(mutex_owned(&fdp->fd_lock));
232
233 fd_checkmaps(fdp);
234
235 if (want > bits)
236 return -1;
237
238 off = want >> NDENTRYSHIFT;
239 i = want & NDENTRYMASK;
240 if (i) {
241 sub = bitmap[off] | ((u_int)~0 >> (NDENTRIES - i));
242 if (sub != ~0)
243 goto found;
244 off++;
245 }
246
247 maxoff = NDLOSLOTS(bits);
248 while (off < maxoff) {
249 if ((sub = bitmap[off]) != ~0)
250 goto found;
251 off++;
252 }
253
254 return -1;
255
256 found:
257 return (off << NDENTRYSHIFT) + ffs(~sub) - 1;
258 }
259
260 static int
261 fd_last_set(filedesc_t *fd, int last)
262 {
263 int off, i;
264 fdfile_t **ff = fd->fd_dt->dt_ff;
265 uint32_t *bitmap = fd->fd_lomap;
266
267 KASSERT(mutex_owned(&fd->fd_lock));
268
269 fd_checkmaps(fd);
270
271 off = (last - 1) >> NDENTRYSHIFT;
272
273 while (off >= 0 && !bitmap[off])
274 off--;
275
276 if (off < 0)
277 return -1;
278
279 i = ((off + 1) << NDENTRYSHIFT) - 1;
280 if (i >= last)
281 i = last - 1;
282
283 /* XXX should use bitmap */
284 while (i > 0 && (ff[i] == NULL || !ff[i]->ff_allocated))
285 i--;
286
287 return i;
288 }
289
290 static inline void
291 fd_used(filedesc_t *fdp, unsigned fd)
292 {
293 u_int off = fd >> NDENTRYSHIFT;
294 fdfile_t *ff;
295
296 ff = fdp->fd_dt->dt_ff[fd];
297
298 KASSERT(mutex_owned(&fdp->fd_lock));
299 KASSERT((fdp->fd_lomap[off] & (1U << (fd & NDENTRYMASK))) == 0);
300 KASSERT(ff != NULL);
301 KASSERT(ff->ff_file == NULL);
302 KASSERT(!ff->ff_allocated);
303
304 ff->ff_allocated = true;
305 fdp->fd_lomap[off] |= 1U << (fd & NDENTRYMASK);
306 if (__predict_false(fdp->fd_lomap[off] == ~0)) {
307 KASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
308 (1U << (off & NDENTRYMASK))) == 0);
309 fdp->fd_himap[off >> NDENTRYSHIFT] |= 1U << (off & NDENTRYMASK);
310 }
311
312 if ((int)fd > fdp->fd_lastfile) {
313 fdp->fd_lastfile = fd;
314 }
315
316 fd_checkmaps(fdp);
317 }
318
319 static inline void
320 fd_unused(filedesc_t *fdp, unsigned fd)
321 {
322 u_int off = fd >> NDENTRYSHIFT;
323 fdfile_t *ff;
324
325 ff = fdp->fd_dt->dt_ff[fd];
326
327 /*
328 * Don't assert the lock is held here, as we may be copying
329 * the table during exec() and it is not needed there.
330 * procfs and sysctl are locked out by proc::p_reflock.
331 *
332 * KASSERT(mutex_owned(&fdp->fd_lock));
333 */
334 KASSERT(ff != NULL);
335 KASSERT(ff->ff_file == NULL);
336 KASSERT(ff->ff_allocated);
337
338 if (fd < fdp->fd_freefile) {
339 fdp->fd_freefile = fd;
340 }
341
342 if (fdp->fd_lomap[off] == ~0) {
343 KASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
344 (1U << (off & NDENTRYMASK))) != 0);
345 fdp->fd_himap[off >> NDENTRYSHIFT] &=
346 ~(1U << (off & NDENTRYMASK));
347 }
348 KASSERT((fdp->fd_lomap[off] & (1U << (fd & NDENTRYMASK))) != 0);
349 fdp->fd_lomap[off] &= ~(1U << (fd & NDENTRYMASK));
350 ff->ff_allocated = false;
351
352 KASSERT(fd <= fdp->fd_lastfile);
353 if (fd == fdp->fd_lastfile) {
354 fdp->fd_lastfile = fd_last_set(fdp, fd);
355 }
356 fd_checkmaps(fdp);
357 }
358
359 /*
360 * Look up the file structure corresponding to a file descriptor
361 * and return the file, holding a reference on the descriptor.
362 */
363 file_t *
364 fd_getfile(unsigned fd)
365 {
366 filedesc_t *fdp;
367 fdfile_t *ff;
368 file_t *fp;
369 fdtab_t *dt;
370
371 /*
372 * Look up the fdfile structure representing this descriptor.
373 * We are doing this unlocked. See fd_tryexpand().
374 */
375 fdp = curlwp->l_fd;
376 dt = fdp->fd_dt;
377 if (__predict_false(fd >= dt->dt_nfiles)) {
378 return NULL;
379 }
380 ff = dt->dt_ff[fd];
381 KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
382 if (__predict_false(ff == NULL)) {
383 return NULL;
384 }
385
386 /* Now get a reference to the descriptor. */
387 if (fdp->fd_refcnt == 1) {
388 /*
389 * Single threaded: don't need to worry about concurrent
390 * access (other than earlier calls to kqueue, which may
391 * hold a reference to the descriptor).
392 */
393 ff->ff_refcnt++;
394 } else {
395 /*
396 * Multi threaded: issue a memory barrier to ensure that we
397 * acquire the file pointer _after_ adding a reference. If
398 * no memory barrier, we could fetch a stale pointer.
399 */
400 atomic_inc_uint(&ff->ff_refcnt);
401 #ifndef __HAVE_ATOMIC_AS_MEMBAR
402 membar_enter();
403 #endif
404 }
405
406 /*
407 * If the file is not open or is being closed then put the
408 * reference back.
409 */
410 fp = ff->ff_file;
411 if (__predict_true(fp != NULL)) {
412 return fp;
413 }
414 fd_putfile(fd);
415 return NULL;
416 }
417
418 /*
419 * Release a reference to a file descriptor acquired with fd_getfile().
420 */
421 void
422 fd_putfile(unsigned fd)
423 {
424 filedesc_t *fdp;
425 fdfile_t *ff;
426 u_int u, v;
427
428 fdp = curlwp->l_fd;
429 ff = fdp->fd_dt->dt_ff[fd];
430
431 KASSERT(fd < fdp->fd_dt->dt_nfiles);
432 KASSERT(ff != NULL);
433 KASSERT((ff->ff_refcnt & FR_MASK) > 0);
434 KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
435
436 if (fdp->fd_refcnt == 1) {
437 /*
438 * Single threaded: don't need to worry about concurrent
439 * access (other than earlier calls to kqueue, which may
440 * hold a reference to the descriptor).
441 */
442 if (__predict_false((ff->ff_refcnt & FR_CLOSING) != 0)) {
443 fd_close(fd);
444 return;
445 }
446 ff->ff_refcnt--;
447 return;
448 }
449
450 /*
451 * Ensure that any use of the file is complete and globally
452 * visible before dropping the final reference. If no membar,
453 * the current CPU could still access memory associated with
454 * the file after it has been freed or recycled by another
455 * CPU.
456 */
457 #ifndef __HAVE_ATOMIC_AS_MEMBAR
458 membar_exit();
459 #endif
460
461 /*
462 * Be optimistic and start out with the assumption that no other
463 * threads are trying to close the descriptor. If the CAS fails,
464 * we lost a race and/or it's being closed.
465 */
466 for (u = ff->ff_refcnt & FR_MASK;; u = v) {
467 v = atomic_cas_uint(&ff->ff_refcnt, u, u - 1);
468 if (__predict_true(u == v)) {
469 return;
470 }
471 if (__predict_false((v & FR_CLOSING) != 0)) {
472 break;
473 }
474 }
475
476 /* Another thread is waiting to close the file: join it. */
477 (void)fd_close(fd);
478 }
479
480 /*
481 * Convenience wrapper around fd_getfile() that returns reference
482 * to a vnode.
483 */
484 int
485 fd_getvnode(unsigned fd, file_t **fpp)
486 {
487 vnode_t *vp;
488 file_t *fp;
489
490 fp = fd_getfile(fd);
491 if (__predict_false(fp == NULL)) {
492 return EBADF;
493 }
494 if (__predict_false(fp->f_type != DTYPE_VNODE)) {
495 fd_putfile(fd);
496 return EINVAL;
497 }
498 vp = fp->f_vnode;
499 if (__predict_false(vp->v_type == VBAD)) {
500 /* XXX Is this case really necessary? */
501 fd_putfile(fd);
502 return EBADF;
503 }
504 *fpp = fp;
505 return 0;
506 }
507
508 /*
509 * Convenience wrapper around fd_getfile() that returns reference
510 * to a socket.
511 */
512 int
513 fd_getsock1(unsigned fd, struct socket **sop, file_t **fp)
514 {
515 *fp = fd_getfile(fd);
516 if (__predict_false(*fp == NULL)) {
517 return EBADF;
518 }
519 if (__predict_false((*fp)->f_type != DTYPE_SOCKET)) {
520 fd_putfile(fd);
521 return ENOTSOCK;
522 }
523 *sop = (*fp)->f_socket;
524 return 0;
525 }
526
527 int
528 fd_getsock(unsigned fd, struct socket **sop)
529 {
530 file_t *fp;
531 return fd_getsock1(fd, sop, &fp);
532 }
533
534 /*
535 * Look up the file structure corresponding to a file descriptor
536 * and return it with a reference held on the file, not the
537 * descriptor.
538 *
539 * This is heavyweight and only used when accessing descriptors
540 * from a foreign process. The caller must ensure that `p' does
541 * not exit or fork across this call.
542 *
543 * To release the file (not descriptor) reference, use closef().
544 */
545 file_t *
546 fd_getfile2(proc_t *p, unsigned fd)
547 {
548 filedesc_t *fdp;
549 fdfile_t *ff;
550 file_t *fp;
551 fdtab_t *dt;
552
553 fdp = p->p_fd;
554 mutex_enter(&fdp->fd_lock);
555 dt = fdp->fd_dt;
556 if (fd >= dt->dt_nfiles) {
557 mutex_exit(&fdp->fd_lock);
558 return NULL;
559 }
560 if ((ff = dt->dt_ff[fd]) == NULL) {
561 mutex_exit(&fdp->fd_lock);
562 return NULL;
563 }
564 if ((fp = ff->ff_file) == NULL) {
565 mutex_exit(&fdp->fd_lock);
566 return NULL;
567 }
568 mutex_enter(&fp->f_lock);
569 fp->f_count++;
570 mutex_exit(&fp->f_lock);
571 mutex_exit(&fdp->fd_lock);
572
573 return fp;
574 }
575
576 /*
577 * Internal form of close. Must be called with a reference to the
578 * descriptor, and will drop the reference. When all descriptor
579 * references are dropped, releases the descriptor slot and a single
580 * reference to the file structure.
581 */
582 int
583 fd_close(unsigned fd)
584 {
585 struct flock lf;
586 filedesc_t *fdp;
587 fdfile_t *ff;
588 file_t *fp;
589 proc_t *p;
590 lwp_t *l;
591 u_int refcnt;
592
593 l = curlwp;
594 p = l->l_proc;
595 fdp = l->l_fd;
596 ff = fdp->fd_dt->dt_ff[fd];
597
598 KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
599
600 mutex_enter(&fdp->fd_lock);
601 KASSERT((ff->ff_refcnt & FR_MASK) > 0);
602 if (__predict_false(ff->ff_file == NULL)) {
603 /*
604 * Another user of the file is already closing, and is
605 * waiting for other users of the file to drain. Release
606 * our reference, and wake up the closer.
607 */
608 atomic_dec_uint(&ff->ff_refcnt);
609 cv_broadcast(&ff->ff_closing);
610 mutex_exit(&fdp->fd_lock);
611
612 /*
613 * An application error, so pretend that the descriptor
614 * was already closed. We can't safely wait for it to
615 * be closed without potentially deadlocking.
616 */
617 return (EBADF);
618 }
619 KASSERT((ff->ff_refcnt & FR_CLOSING) == 0);
620
621 /*
622 * There may be multiple users of this file within the process.
623 * Notify existing and new users that the file is closing. This
624 * will prevent them from adding additional uses to this file
625 * while we are closing it.
626 */
627 fp = ff->ff_file;
628 ff->ff_file = NULL;
629 ff->ff_exclose = false;
630
631 /*
632 * We expect the caller to hold a descriptor reference - drop it.
633 * The reference count may increase beyond zero at this point due
634 * to an erroneous descriptor reference by an application, but
635 * fd_getfile() will notice that the file is being closed and drop
636 * the reference again.
637 */
638 if (fdp->fd_refcnt == 1) {
639 /* Single threaded. */
640 refcnt = --(ff->ff_refcnt);
641 } else {
642 /* Multi threaded. */
643 #ifndef __HAVE_ATOMIC_AS_MEMBAR
644 membar_producer();
645 #endif
646 refcnt = atomic_dec_uint_nv(&ff->ff_refcnt);
647 }
648 if (__predict_false(refcnt != 0)) {
649 /*
650 * Wait for other references to drain. This is typically
651 * an application error - the descriptor is being closed
652 * while still in use.
653 * (Or just a threaded application trying to unblock its
654 * thread that sleeps in (say) accept()).
655 */
656 atomic_or_uint(&ff->ff_refcnt, FR_CLOSING);
657
658 /*
659 * Remove any knotes attached to the file. A knote
660 * attached to the descriptor can hold references on it.
661 */
662 mutex_exit(&fdp->fd_lock);
663 if (!SLIST_EMPTY(&ff->ff_knlist)) {
664 knote_fdclose(fd);
665 }
666
667 /*
668 * Since the file system code doesn't know which fd
669 * each request came from (think dup()), we have to
670 * ask it to return ERESTART for any long-term blocks.
671 * The re-entry through read/write/etc will detect the
672 * closed fd and return EBAFD.
673 * Blocked partial writes may return a short length.
674 */
675 (*fp->f_ops->fo_restart)(fp);
676 mutex_enter(&fdp->fd_lock);
677
678 /*
679 * We need to see the count drop to zero at least once,
680 * in order to ensure that all pre-existing references
681 * have been drained. New references past this point are
682 * of no interest.
683 * XXX (dsl) this may need to call fo_restart() after a
684 * timeout to guarantee that all the system calls exit.
685 */
686 while ((ff->ff_refcnt & FR_MASK) != 0) {
687 cv_wait(&ff->ff_closing, &fdp->fd_lock);
688 }
689 atomic_and_uint(&ff->ff_refcnt, ~FR_CLOSING);
690 } else {
691 /* If no references, there must be no knotes. */
692 KASSERT(SLIST_EMPTY(&ff->ff_knlist));
693 }
694
695 /*
696 * POSIX record locking dictates that any close releases ALL
697 * locks owned by this process. This is handled by setting
698 * a flag in the unlock to free ONLY locks obeying POSIX
699 * semantics, and not to free BSD-style file locks.
700 * If the descriptor was in a message, POSIX-style locks
701 * aren't passed with the descriptor.
702 */
703 if (__predict_false((p->p_flag & PK_ADVLOCK) != 0 &&
704 fp->f_type == DTYPE_VNODE)) {
705 lf.l_whence = SEEK_SET;
706 lf.l_start = 0;
707 lf.l_len = 0;
708 lf.l_type = F_UNLCK;
709 mutex_exit(&fdp->fd_lock);
710 (void)VOP_ADVLOCK(fp->f_vnode, p, F_UNLCK, &lf, F_POSIX);
711 mutex_enter(&fdp->fd_lock);
712 }
713
714 /* Free descriptor slot. */
715 fd_unused(fdp, fd);
716 mutex_exit(&fdp->fd_lock);
717
718 /* Now drop reference to the file itself. */
719 return closef(fp);
720 }
721
722 /*
723 * Duplicate a file descriptor.
724 */
725 int
726 fd_dup(file_t *fp, int minfd, int *newp, bool exclose)
727 {
728 proc_t *p = curproc;
729 int error;
730
731 while ((error = fd_alloc(p, minfd, newp)) != 0) {
732 if (error != ENOSPC) {
733 return error;
734 }
735 fd_tryexpand(p);
736 }
737
738 curlwp->l_fd->fd_dt->dt_ff[*newp]->ff_exclose = exclose;
739 fd_affix(p, fp, *newp);
740 return 0;
741 }
742
743 /*
744 * dup2 operation.
745 */
746 int
747 fd_dup2(file_t *fp, unsigned newfd, int flags)
748 {
749 filedesc_t *fdp = curlwp->l_fd;
750 fdfile_t *ff;
751 fdtab_t *dt;
752
753 if (flags & ~(O_CLOEXEC|O_NONBLOCK))
754 return EINVAL;
755 /*
756 * Ensure there are enough slots in the descriptor table,
757 * and allocate an fdfile_t up front in case we need it.
758 */
759 while (newfd >= fdp->fd_dt->dt_nfiles) {
760 fd_tryexpand(curproc);
761 }
762 ff = pool_cache_get(fdfile_cache, PR_WAITOK);
763
764 /*
765 * If there is already a file open, close it. If the file is
766 * half open, wait for it to be constructed before closing it.
767 * XXX Potential for deadlock here?
768 */
769 mutex_enter(&fdp->fd_lock);
770 while (fd_isused(fdp, newfd)) {
771 mutex_exit(&fdp->fd_lock);
772 if (fd_getfile(newfd) != NULL) {
773 (void)fd_close(newfd);
774 } else {
775 /*
776 * Crummy, but unlikely to happen.
777 * Can occur if we interrupt another
778 * thread while it is opening a file.
779 */
780 kpause("dup2", false, 1, NULL);
781 }
782 mutex_enter(&fdp->fd_lock);
783 }
784 dt = fdp->fd_dt;
785 if (dt->dt_ff[newfd] == NULL) {
786 KASSERT(newfd >= NDFDFILE);
787 dt->dt_ff[newfd] = ff;
788 ff = NULL;
789 }
790 fd_used(fdp, newfd);
791 mutex_exit(&fdp->fd_lock);
792
793 dt->dt_ff[newfd]->ff_exclose = (flags & O_CLOEXEC) != 0;
794 fp->f_flag |= flags & FNONBLOCK;
795 /* Slot is now allocated. Insert copy of the file. */
796 fd_affix(curproc, fp, newfd);
797 if (ff != NULL) {
798 pool_cache_put(fdfile_cache, ff);
799 }
800 return 0;
801 }
802
803 /*
804 * Drop reference to a file structure.
805 */
806 int
807 closef(file_t *fp)
808 {
809 struct flock lf;
810 int error;
811
812 /*
813 * Drop reference. If referenced elsewhere it's still open
814 * and we have nothing more to do.
815 */
816 mutex_enter(&fp->f_lock);
817 KASSERT(fp->f_count > 0);
818 if (--fp->f_count > 0) {
819 mutex_exit(&fp->f_lock);
820 return 0;
821 }
822 KASSERT(fp->f_count == 0);
823 mutex_exit(&fp->f_lock);
824
825 /* We held the last reference - release locks, close and free. */
826 if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
827 lf.l_whence = SEEK_SET;
828 lf.l_start = 0;
829 lf.l_len = 0;
830 lf.l_type = F_UNLCK;
831 (void)VOP_ADVLOCK(fp->f_vnode, fp, F_UNLCK, &lf, F_FLOCK);
832 }
833 if (fp->f_ops != NULL) {
834 error = (*fp->f_ops->fo_close)(fp);
835 } else {
836 error = 0;
837 }
838 KASSERT(fp->f_count == 0);
839 KASSERT(fp->f_cred != NULL);
840 pool_cache_put(file_cache, fp);
841
842 return error;
843 }
844
845 /*
846 * Allocate a file descriptor for the process.
847 */
848 int
849 fd_alloc(proc_t *p, int want, int *result)
850 {
851 filedesc_t *fdp = p->p_fd;
852 int i, lim, last, error, hi;
853 u_int off;
854 fdtab_t *dt;
855
856 KASSERT(p == curproc || p == &proc0);
857
858 /*
859 * Search for a free descriptor starting at the higher
860 * of want or fd_freefile.
861 */
862 mutex_enter(&fdp->fd_lock);
863 fd_checkmaps(fdp);
864 dt = fdp->fd_dt;
865 KASSERT(dt->dt_ff[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
866 lim = uimin((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
867 last = uimin(dt->dt_nfiles, lim);
868 for (;;) {
869 if ((i = want) < fdp->fd_freefile)
870 i = fdp->fd_freefile;
871 off = i >> NDENTRYSHIFT;
872 hi = fd_next_zero(fdp, fdp->fd_himap, off,
873 (last + NDENTRIES - 1) >> NDENTRYSHIFT);
874 if (hi == -1)
875 break;
876 i = fd_next_zero(fdp, &fdp->fd_lomap[hi],
877 hi > off ? 0 : i & NDENTRYMASK, NDENTRIES);
878 if (i == -1) {
879 /*
880 * Free file descriptor in this block was
881 * below want, try again with higher want.
882 */
883 want = (hi + 1) << NDENTRYSHIFT;
884 continue;
885 }
886 i += (hi << NDENTRYSHIFT);
887 if (i >= last) {
888 break;
889 }
890 if (dt->dt_ff[i] == NULL) {
891 KASSERT(i >= NDFDFILE);
892 dt->dt_ff[i] = pool_cache_get(fdfile_cache, PR_WAITOK);
893 }
894 KASSERT(dt->dt_ff[i]->ff_file == NULL);
895 fd_used(fdp, i);
896 if (want <= fdp->fd_freefile) {
897 fdp->fd_freefile = i;
898 }
899 *result = i;
900 KASSERT(i >= NDFDFILE ||
901 dt->dt_ff[i] == (fdfile_t *)fdp->fd_dfdfile[i]);
902 fd_checkmaps(fdp);
903 mutex_exit(&fdp->fd_lock);
904 return 0;
905 }
906
907 /* No space in current array. Let the caller expand and retry. */
908 error = (dt->dt_nfiles >= lim) ? EMFILE : ENOSPC;
909 mutex_exit(&fdp->fd_lock);
910 return error;
911 }
912
913 /*
914 * Allocate memory for a descriptor table.
915 */
916 static fdtab_t *
917 fd_dtab_alloc(int n)
918 {
919 fdtab_t *dt;
920 size_t sz;
921
922 KASSERT(n > NDFILE);
923
924 sz = sizeof(*dt) + (n - NDFILE) * sizeof(dt->dt_ff[0]);
925 dt = kmem_alloc(sz, KM_SLEEP);
926 #ifdef DIAGNOSTIC
927 memset(dt, 0xff, sz);
928 #endif
929 dt->dt_nfiles = n;
930 dt->dt_link = NULL;
931 return dt;
932 }
933
934 /*
935 * Free a descriptor table, and all tables linked for deferred free.
936 */
937 static void
938 fd_dtab_free(fdtab_t *dt)
939 {
940 fdtab_t *next;
941 size_t sz;
942
943 do {
944 next = dt->dt_link;
945 KASSERT(dt->dt_nfiles > NDFILE);
946 sz = sizeof(*dt) +
947 (dt->dt_nfiles - NDFILE) * sizeof(dt->dt_ff[0]);
948 #ifdef DIAGNOSTIC
949 memset(dt, 0xff, sz);
950 #endif
951 kmem_free(dt, sz);
952 dt = next;
953 } while (dt != NULL);
954 }
955
956 /*
957 * Allocate descriptor bitmap.
958 */
959 static void
960 fd_map_alloc(int n, uint32_t **lo, uint32_t **hi)
961 {
962 uint8_t *ptr;
963 size_t szlo, szhi;
964
965 KASSERT(n > NDENTRIES);
966
967 szlo = NDLOSLOTS(n) * sizeof(uint32_t);
968 szhi = NDHISLOTS(n) * sizeof(uint32_t);
969 ptr = kmem_alloc(szlo + szhi, KM_SLEEP);
970 *lo = (uint32_t *)ptr;
971 *hi = (uint32_t *)(ptr + szlo);
972 }
973
974 /*
975 * Free descriptor bitmap.
976 */
977 static void
978 fd_map_free(int n, uint32_t *lo, uint32_t *hi)
979 {
980 size_t szlo, szhi;
981
982 KASSERT(n > NDENTRIES);
983
984 szlo = NDLOSLOTS(n) * sizeof(uint32_t);
985 szhi = NDHISLOTS(n) * sizeof(uint32_t);
986 KASSERT(hi == (uint32_t *)((uint8_t *)lo + szlo));
987 kmem_free(lo, szlo + szhi);
988 }
989
990 /*
991 * Expand a process' descriptor table.
992 */
993 void
994 fd_tryexpand(proc_t *p)
995 {
996 filedesc_t *fdp;
997 int i, numfiles, oldnfiles;
998 fdtab_t *newdt, *dt;
999 uint32_t *newhimap, *newlomap;
1000
1001 KASSERT(p == curproc || p == &proc0);
1002
1003 fdp = p->p_fd;
1004 newhimap = NULL;
1005 newlomap = NULL;
1006 oldnfiles = fdp->fd_dt->dt_nfiles;
1007
1008 if (oldnfiles < NDEXTENT)
1009 numfiles = NDEXTENT;
1010 else
1011 numfiles = 2 * oldnfiles;
1012
1013 newdt = fd_dtab_alloc(numfiles);
1014 if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
1015 fd_map_alloc(numfiles, &newlomap, &newhimap);
1016 }
1017
1018 mutex_enter(&fdp->fd_lock);
1019 dt = fdp->fd_dt;
1020 KASSERT(dt->dt_ff[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
1021 if (dt->dt_nfiles != oldnfiles) {
1022 /* fdp changed; caller must retry */
1023 mutex_exit(&fdp->fd_lock);
1024 fd_dtab_free(newdt);
1025 if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
1026 fd_map_free(numfiles, newlomap, newhimap);
1027 }
1028 return;
1029 }
1030
1031 /* Copy the existing descriptor table and zero the new portion. */
1032 i = sizeof(fdfile_t *) * oldnfiles;
1033 memcpy(newdt->dt_ff, dt->dt_ff, i);
1034 memset((uint8_t *)newdt->dt_ff + i, 0,
1035 numfiles * sizeof(fdfile_t *) - i);
1036
1037 /*
1038 * Link old descriptor array into list to be discarded. We defer
1039 * freeing until the last reference to the descriptor table goes
1040 * away (usually process exit). This allows us to do lockless
1041 * lookups in fd_getfile().
1042 */
1043 if (oldnfiles > NDFILE) {
1044 if (fdp->fd_refcnt > 1) {
1045 newdt->dt_link = dt;
1046 } else {
1047 fd_dtab_free(dt);
1048 }
1049 }
1050
1051 if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
1052 i = NDHISLOTS(oldnfiles) * sizeof(uint32_t);
1053 memcpy(newhimap, fdp->fd_himap, i);
1054 memset((uint8_t *)newhimap + i, 0,
1055 NDHISLOTS(numfiles) * sizeof(uint32_t) - i);
1056
1057 i = NDLOSLOTS(oldnfiles) * sizeof(uint32_t);
1058 memcpy(newlomap, fdp->fd_lomap, i);
1059 memset((uint8_t *)newlomap + i, 0,
1060 NDLOSLOTS(numfiles) * sizeof(uint32_t) - i);
1061
1062 if (NDHISLOTS(oldnfiles) > NDHISLOTS(NDFILE)) {
1063 fd_map_free(oldnfiles, fdp->fd_lomap, fdp->fd_himap);
1064 }
1065 fdp->fd_himap = newhimap;
1066 fdp->fd_lomap = newlomap;
1067 }
1068
1069 /*
1070 * All other modifications must become globally visible before
1071 * the change to fd_dt. See fd_getfile().
1072 */
1073 membar_producer();
1074 fdp->fd_dt = newdt;
1075 KASSERT(newdt->dt_ff[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
1076 fd_checkmaps(fdp);
1077 mutex_exit(&fdp->fd_lock);
1078 }
1079
1080 /*
1081 * Create a new open file structure and allocate a file descriptor
1082 * for the current process.
1083 */
1084 int
1085 fd_allocfile(file_t **resultfp, int *resultfd)
1086 {
1087 proc_t *p = curproc;
1088 kauth_cred_t cred;
1089 file_t *fp;
1090 int error;
1091
1092 while ((error = fd_alloc(p, 0, resultfd)) != 0) {
1093 if (error != ENOSPC) {
1094 return error;
1095 }
1096 fd_tryexpand(p);
1097 }
1098
1099 fp = pool_cache_get(file_cache, PR_WAITOK);
1100 if (fp == NULL) {
1101 fd_abort(p, NULL, *resultfd);
1102 return ENFILE;
1103 }
1104 KASSERT(fp->f_count == 0);
1105 KASSERT(fp->f_msgcount == 0);
1106 KASSERT(fp->f_unpcount == 0);
1107
1108 /* Replace cached credentials if not what we need. */
1109 cred = curlwp->l_cred;
1110 if (__predict_false(cred != fp->f_cred)) {
1111 kauth_cred_free(fp->f_cred);
1112 kauth_cred_hold(cred);
1113 fp->f_cred = cred;
1114 }
1115
1116 /*
1117 * Don't allow recycled files to be scanned.
1118 * See uipc_usrreq.c.
1119 */
1120 if (__predict_false((fp->f_flag & FSCAN) != 0)) {
1121 mutex_enter(&fp->f_lock);
1122 atomic_and_uint(&fp->f_flag, ~FSCAN);
1123 mutex_exit(&fp->f_lock);
1124 }
1125
1126 fp->f_advice = 0;
1127 fp->f_offset = 0;
1128 *resultfp = fp;
1129
1130 return 0;
1131 }
1132
1133 /*
1134 * Successful creation of a new descriptor: make visible to the process.
1135 */
1136 void
1137 fd_affix(proc_t *p, file_t *fp, unsigned fd)
1138 {
1139 fdfile_t *ff;
1140 filedesc_t *fdp;
1141
1142 KASSERT(p == curproc || p == &proc0);
1143
1144 /* Add a reference to the file structure. */
1145 mutex_enter(&fp->f_lock);
1146 fp->f_count++;
1147 mutex_exit(&fp->f_lock);
1148
1149 /*
1150 * Insert the new file into the descriptor slot.
1151 *
1152 * The memory barriers provided by lock activity in this routine
1153 * ensure that any updates to the file structure become globally
1154 * visible before the file becomes visible to other LWPs in the
1155 * current process.
1156 */
1157 fdp = p->p_fd;
1158 ff = fdp->fd_dt->dt_ff[fd];
1159
1160 KASSERT(ff != NULL);
1161 KASSERT(ff->ff_file == NULL);
1162 KASSERT(ff->ff_allocated);
1163 KASSERT(fd_isused(fdp, fd));
1164 KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
1165
1166 /* No need to lock in order to make file initially visible. */
1167 ff->ff_file = fp;
1168 }
1169
1170 /*
1171 * Abort creation of a new descriptor: free descriptor slot and file.
1172 */
1173 void
1174 fd_abort(proc_t *p, file_t *fp, unsigned fd)
1175 {
1176 filedesc_t *fdp;
1177 fdfile_t *ff;
1178
1179 KASSERT(p == curproc || p == &proc0);
1180
1181 fdp = p->p_fd;
1182 ff = fdp->fd_dt->dt_ff[fd];
1183 ff->ff_exclose = false;
1184
1185 KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
1186
1187 mutex_enter(&fdp->fd_lock);
1188 KASSERT(fd_isused(fdp, fd));
1189 fd_unused(fdp, fd);
1190 mutex_exit(&fdp->fd_lock);
1191
1192 if (fp != NULL) {
1193 KASSERT(fp->f_count == 0);
1194 KASSERT(fp->f_cred != NULL);
1195 pool_cache_put(file_cache, fp);
1196 }
1197 }
1198
1199 static int
1200 file_ctor(void *arg, void *obj, int flags)
1201 {
1202 file_t *fp = obj;
1203
1204 memset(fp, 0, sizeof(*fp));
1205
1206 mutex_enter(&filelist_lock);
1207 if (__predict_false(nfiles >= maxfiles)) {
1208 mutex_exit(&filelist_lock);
1209 tablefull("file", "increase kern.maxfiles or MAXFILES");
1210 return ENFILE;
1211 }
1212 nfiles++;
1213 LIST_INSERT_HEAD(&filehead, fp, f_list);
1214 mutex_init(&fp->f_lock, MUTEX_DEFAULT, IPL_NONE);
1215 fp->f_cred = curlwp->l_cred;
1216 kauth_cred_hold(fp->f_cred);
1217 mutex_exit(&filelist_lock);
1218
1219 return 0;
1220 }
1221
1222 static void
1223 file_dtor(void *arg, void *obj)
1224 {
1225 file_t *fp = obj;
1226
1227 mutex_enter(&filelist_lock);
1228 nfiles--;
1229 LIST_REMOVE(fp, f_list);
1230 mutex_exit(&filelist_lock);
1231
1232 kauth_cred_free(fp->f_cred);
1233 mutex_destroy(&fp->f_lock);
1234 }
1235
1236 static int
1237 fdfile_ctor(void *arg, void *obj, int flags)
1238 {
1239 fdfile_t *ff = obj;
1240
1241 memset(ff, 0, sizeof(*ff));
1242 cv_init(&ff->ff_closing, "fdclose");
1243
1244 return 0;
1245 }
1246
1247 static void
1248 fdfile_dtor(void *arg, void *obj)
1249 {
1250 fdfile_t *ff = obj;
1251
1252 cv_destroy(&ff->ff_closing);
1253 }
1254
1255 file_t *
1256 fgetdummy(void)
1257 {
1258 file_t *fp;
1259
1260 fp = kmem_zalloc(sizeof(*fp), KM_SLEEP);
1261 mutex_init(&fp->f_lock, MUTEX_DEFAULT, IPL_NONE);
1262 return fp;
1263 }
1264
1265 void
1266 fputdummy(file_t *fp)
1267 {
1268
1269 mutex_destroy(&fp->f_lock);
1270 kmem_free(fp, sizeof(*fp));
1271 }
1272
1273 /*
1274 * Create an initial filedesc structure.
1275 */
1276 filedesc_t *
1277 fd_init(filedesc_t *fdp)
1278 {
1279 #ifdef DIAGNOSTIC
1280 unsigned fd;
1281 #endif
1282
1283 if (__predict_true(fdp == NULL)) {
1284 fdp = pool_cache_get(filedesc_cache, PR_WAITOK);
1285 } else {
1286 KASSERT(fdp == &filedesc0);
1287 filedesc_ctor(NULL, fdp, PR_WAITOK);
1288 }
1289
1290 #ifdef DIAGNOSTIC
1291 KASSERT(fdp->fd_lastfile == -1);
1292 KASSERT(fdp->fd_lastkqfile == -1);
1293 KASSERT(fdp->fd_knhash == NULL);
1294 KASSERT(fdp->fd_freefile == 0);
1295 KASSERT(fdp->fd_exclose == false);
1296 KASSERT(fdp->fd_dt == &fdp->fd_dtbuiltin);
1297 KASSERT(fdp->fd_dtbuiltin.dt_nfiles == NDFILE);
1298 for (fd = 0; fd < NDFDFILE; fd++) {
1299 KASSERT(fdp->fd_dtbuiltin.dt_ff[fd] ==
1300 (fdfile_t *)fdp->fd_dfdfile[fd]);
1301 }
1302 for (fd = NDFDFILE; fd < NDFILE; fd++) {
1303 KASSERT(fdp->fd_dtbuiltin.dt_ff[fd] == NULL);
1304 }
1305 KASSERT(fdp->fd_himap == fdp->fd_dhimap);
1306 KASSERT(fdp->fd_lomap == fdp->fd_dlomap);
1307 #endif /* DIAGNOSTIC */
1308
1309 fdp->fd_refcnt = 1;
1310 fd_checkmaps(fdp);
1311
1312 return fdp;
1313 }
1314
1315 /*
1316 * Initialize a file descriptor table.
1317 */
1318 static int
1319 filedesc_ctor(void *arg, void *obj, int flag)
1320 {
1321 filedesc_t *fdp = obj;
1322 fdfile_t **ffp;
1323 int i;
1324
1325 memset(fdp, 0, sizeof(*fdp));
1326 mutex_init(&fdp->fd_lock, MUTEX_DEFAULT, IPL_NONE);
1327 fdp->fd_lastfile = -1;
1328 fdp->fd_lastkqfile = -1;
1329 fdp->fd_dt = &fdp->fd_dtbuiltin;
1330 fdp->fd_dtbuiltin.dt_nfiles = NDFILE;
1331 fdp->fd_himap = fdp->fd_dhimap;
1332 fdp->fd_lomap = fdp->fd_dlomap;
1333
1334 CTASSERT(sizeof(fdp->fd_dfdfile[0]) >= sizeof(fdfile_t));
1335 for (i = 0, ffp = fdp->fd_dt->dt_ff; i < NDFDFILE; i++, ffp++) {
1336 *ffp = (fdfile_t *)fdp->fd_dfdfile[i];
1337 (void)fdfile_ctor(NULL, fdp->fd_dfdfile[i], PR_WAITOK);
1338 }
1339
1340 return 0;
1341 }
1342
1343 static void
1344 filedesc_dtor(void *arg, void *obj)
1345 {
1346 filedesc_t *fdp = obj;
1347 int i;
1348
1349 for (i = 0; i < NDFDFILE; i++) {
1350 fdfile_dtor(NULL, fdp->fd_dfdfile[i]);
1351 }
1352
1353 mutex_destroy(&fdp->fd_lock);
1354 }
1355
1356 /*
1357 * Make p share curproc's filedesc structure.
1358 */
1359 void
1360 fd_share(struct proc *p)
1361 {
1362 filedesc_t *fdp;
1363
1364 fdp = curlwp->l_fd;
1365 p->p_fd = fdp;
1366 atomic_inc_uint(&fdp->fd_refcnt);
1367 }
1368
1369 /*
1370 * Acquire a hold on a filedesc structure.
1371 */
1372 void
1373 fd_hold(lwp_t *l)
1374 {
1375 filedesc_t *fdp = l->l_fd;
1376
1377 atomic_inc_uint(&fdp->fd_refcnt);
1378 }
1379
1380 /*
1381 * Copy a filedesc structure.
1382 */
1383 filedesc_t *
1384 fd_copy(void)
1385 {
1386 filedesc_t *newfdp, *fdp;
1387 fdfile_t *ff, **ffp, **nffp, *ff2;
1388 int i, j, numfiles, lastfile, newlast;
1389 file_t *fp;
1390 fdtab_t *newdt;
1391
1392 fdp = curproc->p_fd;
1393 newfdp = pool_cache_get(filedesc_cache, PR_WAITOK);
1394 newfdp->fd_refcnt = 1;
1395
1396 #ifdef DIAGNOSTIC
1397 KASSERT(newfdp->fd_lastfile == -1);
1398 KASSERT(newfdp->fd_lastkqfile == -1);
1399 KASSERT(newfdp->fd_knhash == NULL);
1400 KASSERT(newfdp->fd_freefile == 0);
1401 KASSERT(newfdp->fd_exclose == false);
1402 KASSERT(newfdp->fd_dt == &newfdp->fd_dtbuiltin);
1403 KASSERT(newfdp->fd_dtbuiltin.dt_nfiles == NDFILE);
1404 for (i = 0; i < NDFDFILE; i++) {
1405 KASSERT(newfdp->fd_dtbuiltin.dt_ff[i] ==
1406 (fdfile_t *)&newfdp->fd_dfdfile[i]);
1407 }
1408 for (i = NDFDFILE; i < NDFILE; i++) {
1409 KASSERT(newfdp->fd_dtbuiltin.dt_ff[i] == NULL);
1410 }
1411 #endif /* DIAGNOSTIC */
1412
1413 mutex_enter(&fdp->fd_lock);
1414 fd_checkmaps(fdp);
1415 numfiles = fdp->fd_dt->dt_nfiles;
1416 lastfile = fdp->fd_lastfile;
1417
1418 /*
1419 * If the number of open files fits in the internal arrays
1420 * of the open file structure, use them, otherwise allocate
1421 * additional memory for the number of descriptors currently
1422 * in use.
1423 */
1424 if (lastfile < NDFILE) {
1425 i = NDFILE;
1426 newdt = newfdp->fd_dt;
1427 KASSERT(newfdp->fd_dt == &newfdp->fd_dtbuiltin);
1428 } else {
1429 /*
1430 * Compute the smallest multiple of NDEXTENT needed
1431 * for the file descriptors currently in use,
1432 * allowing the table to shrink.
1433 */
1434 i = numfiles;
1435 while (i >= 2 * NDEXTENT && i > lastfile * 2) {
1436 i /= 2;
1437 }
1438 KASSERT(i > NDFILE);
1439 newdt = fd_dtab_alloc(i);
1440 newfdp->fd_dt = newdt;
1441 memcpy(newdt->dt_ff, newfdp->fd_dtbuiltin.dt_ff,
1442 NDFDFILE * sizeof(fdfile_t **));
1443 memset(newdt->dt_ff + NDFDFILE, 0,
1444 (i - NDFDFILE) * sizeof(fdfile_t **));
1445 }
1446 if (NDHISLOTS(i) <= NDHISLOTS(NDFILE)) {
1447 newfdp->fd_himap = newfdp->fd_dhimap;
1448 newfdp->fd_lomap = newfdp->fd_dlomap;
1449 } else {
1450 fd_map_alloc(i, &newfdp->fd_lomap, &newfdp->fd_himap);
1451 KASSERT(i >= NDENTRIES * NDENTRIES);
1452 memset(newfdp->fd_himap, 0, NDHISLOTS(i)*sizeof(uint32_t));
1453 memset(newfdp->fd_lomap, 0, NDLOSLOTS(i)*sizeof(uint32_t));
1454 }
1455 newfdp->fd_freefile = fdp->fd_freefile;
1456 newfdp->fd_exclose = fdp->fd_exclose;
1457
1458 ffp = fdp->fd_dt->dt_ff;
1459 nffp = newdt->dt_ff;
1460 newlast = -1;
1461 for (i = 0; i <= lastfile; i++, ffp++, nffp++) {
1462 KASSERT(i >= NDFDFILE ||
1463 *nffp == (fdfile_t *)newfdp->fd_dfdfile[i]);
1464 ff = *ffp;
1465 if (ff == NULL || (fp = ff->ff_file) == NULL) {
1466 /* Descriptor unused, or descriptor half open. */
1467 KASSERT(!fd_isused(newfdp, i));
1468 continue;
1469 }
1470 if (__predict_false(fp->f_type == DTYPE_KQUEUE)) {
1471 /* kqueue descriptors cannot be copied. */
1472 if (i < newfdp->fd_freefile) {
1473 newfdp->fd_freefile = i;
1474 }
1475 continue;
1476 }
1477 /* It's active: add a reference to the file. */
1478 mutex_enter(&fp->f_lock);
1479 fp->f_count++;
1480 mutex_exit(&fp->f_lock);
1481
1482 /* Allocate an fdfile_t to represent it. */
1483 if (i >= NDFDFILE) {
1484 ff2 = pool_cache_get(fdfile_cache, PR_WAITOK);
1485 *nffp = ff2;
1486 } else {
1487 ff2 = newdt->dt_ff[i];
1488 }
1489 ff2->ff_file = fp;
1490 ff2->ff_exclose = ff->ff_exclose;
1491 ff2->ff_allocated = true;
1492
1493 /* Fix up bitmaps. */
1494 j = i >> NDENTRYSHIFT;
1495 KASSERT((newfdp->fd_lomap[j] & (1U << (i & NDENTRYMASK))) == 0);
1496 newfdp->fd_lomap[j] |= 1U << (i & NDENTRYMASK);
1497 if (__predict_false(newfdp->fd_lomap[j] == ~0)) {
1498 KASSERT((newfdp->fd_himap[j >> NDENTRYSHIFT] &
1499 (1U << (j & NDENTRYMASK))) == 0);
1500 newfdp->fd_himap[j >> NDENTRYSHIFT] |=
1501 1U << (j & NDENTRYMASK);
1502 }
1503 newlast = i;
1504 }
1505 KASSERT(newdt->dt_ff[0] == (fdfile_t *)newfdp->fd_dfdfile[0]);
1506 newfdp->fd_lastfile = newlast;
1507 fd_checkmaps(newfdp);
1508 mutex_exit(&fdp->fd_lock);
1509
1510 return newfdp;
1511 }
1512
1513 /*
1514 * Release a filedesc structure.
1515 */
1516 void
1517 fd_free(void)
1518 {
1519 fdfile_t *ff;
1520 file_t *fp;
1521 int fd, nf;
1522 fdtab_t *dt;
1523 lwp_t * const l = curlwp;
1524 filedesc_t * const fdp = l->l_fd;
1525 const bool noadvlock = (l->l_proc->p_flag & PK_ADVLOCK) == 0;
1526
1527 KASSERT(fdp->fd_dt->dt_ff[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
1528 KASSERT(fdp->fd_dtbuiltin.dt_nfiles == NDFILE);
1529 KASSERT(fdp->fd_dtbuiltin.dt_link == NULL);
1530
1531 #ifndef __HAVE_ATOMIC_AS_MEMBAR
1532 membar_exit();
1533 #endif
1534 if (atomic_dec_uint_nv(&fdp->fd_refcnt) > 0)
1535 return;
1536
1537 /*
1538 * Close any files that the process holds open.
1539 */
1540 dt = fdp->fd_dt;
1541 fd_checkmaps(fdp);
1542 #ifdef DEBUG
1543 fdp->fd_refcnt = -1; /* see fd_checkmaps */
1544 #endif
1545 for (fd = 0, nf = dt->dt_nfiles; fd < nf; fd++) {
1546 ff = dt->dt_ff[fd];
1547 KASSERT(fd >= NDFDFILE ||
1548 ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
1549 if (ff == NULL)
1550 continue;
1551 if ((fp = ff->ff_file) != NULL) {
1552 /*
1553 * Must use fd_close() here if there is
1554 * a reference from kqueue or we might have posix
1555 * advisory locks.
1556 */
1557 if (__predict_true(ff->ff_refcnt == 0) &&
1558 (noadvlock || fp->f_type != DTYPE_VNODE)) {
1559 ff->ff_file = NULL;
1560 ff->ff_exclose = false;
1561 ff->ff_allocated = false;
1562 closef(fp);
1563 } else {
1564 ff->ff_refcnt++;
1565 fd_close(fd);
1566 }
1567 }
1568 KASSERT(ff->ff_refcnt == 0);
1569 KASSERT(ff->ff_file == NULL);
1570 KASSERT(!ff->ff_exclose);
1571 KASSERT(!ff->ff_allocated);
1572 if (fd >= NDFDFILE) {
1573 pool_cache_put(fdfile_cache, ff);
1574 dt->dt_ff[fd] = NULL;
1575 }
1576 }
1577
1578 /*
1579 * Clean out the descriptor table for the next user and return
1580 * to the cache.
1581 */
1582 if (__predict_false(dt != &fdp->fd_dtbuiltin)) {
1583 fd_dtab_free(fdp->fd_dt);
1584 /* Otherwise, done above. */
1585 memset(&fdp->fd_dtbuiltin.dt_ff[NDFDFILE], 0,
1586 (NDFILE - NDFDFILE) * sizeof(fdp->fd_dtbuiltin.dt_ff[0]));
1587 fdp->fd_dt = &fdp->fd_dtbuiltin;
1588 }
1589 if (__predict_false(NDHISLOTS(nf) > NDHISLOTS(NDFILE))) {
1590 KASSERT(fdp->fd_himap != fdp->fd_dhimap);
1591 KASSERT(fdp->fd_lomap != fdp->fd_dlomap);
1592 fd_map_free(nf, fdp->fd_lomap, fdp->fd_himap);
1593 }
1594 if (__predict_false(fdp->fd_knhash != NULL)) {
1595 hashdone(fdp->fd_knhash, HASH_LIST, fdp->fd_knhashmask);
1596 fdp->fd_knhash = NULL;
1597 fdp->fd_knhashmask = 0;
1598 } else {
1599 KASSERT(fdp->fd_knhashmask == 0);
1600 }
1601 fdp->fd_dt = &fdp->fd_dtbuiltin;
1602 fdp->fd_lastkqfile = -1;
1603 fdp->fd_lastfile = -1;
1604 fdp->fd_freefile = 0;
1605 fdp->fd_exclose = false;
1606 memset(&fdp->fd_startzero, 0, sizeof(*fdp) -
1607 offsetof(filedesc_t, fd_startzero));
1608 fdp->fd_himap = fdp->fd_dhimap;
1609 fdp->fd_lomap = fdp->fd_dlomap;
1610 KASSERT(fdp->fd_dtbuiltin.dt_nfiles == NDFILE);
1611 KASSERT(fdp->fd_dtbuiltin.dt_link == NULL);
1612 KASSERT(fdp->fd_dt == &fdp->fd_dtbuiltin);
1613 #ifdef DEBUG
1614 fdp->fd_refcnt = 0; /* see fd_checkmaps */
1615 #endif
1616 fd_checkmaps(fdp);
1617 pool_cache_put(filedesc_cache, fdp);
1618 }
1619
1620 /*
1621 * File Descriptor pseudo-device driver (/dev/fd/).
1622 *
1623 * Opening minor device N dup()s the file (if any) connected to file
1624 * descriptor N belonging to the calling process. Note that this driver
1625 * consists of only the ``open()'' routine, because all subsequent
1626 * references to this file will be direct to the other driver.
1627 */
1628 static int
1629 filedescopen(dev_t dev, int mode, int type, lwp_t *l)
1630 {
1631
1632 /*
1633 * XXX Kludge: set dupfd to contain the value of the
1634 * the file descriptor being sought for duplication. The error
1635 * return ensures that the vnode for this device will be released
1636 * by vn_open. Open will detect this special error and take the
1637 * actions in fd_dupopen below. Other callers of vn_open or VOP_OPEN
1638 * will simply report the error.
1639 */
1640 l->l_dupfd = minor(dev); /* XXX */
1641 return EDUPFD;
1642 }
1643
1644 /*
1645 * Duplicate the specified descriptor to a free descriptor.
1646 */
1647 int
1648 fd_dupopen(int old, int *newp, int mode, int error)
1649 {
1650 filedesc_t *fdp;
1651 fdfile_t *ff;
1652 file_t *fp;
1653 fdtab_t *dt;
1654
1655 if ((fp = fd_getfile(old)) == NULL) {
1656 return EBADF;
1657 }
1658 fdp = curlwp->l_fd;
1659 dt = fdp->fd_dt;
1660 ff = dt->dt_ff[old];
1661
1662 /*
1663 * There are two cases of interest here.
1664 *
1665 * For EDUPFD simply dup (old) to file descriptor
1666 * (new) and return.
1667 *
1668 * For EMOVEFD steal away the file structure from (old) and
1669 * store it in (new). (old) is effectively closed by
1670 * this operation.
1671 *
1672 * Any other error code is just returned.
1673 */
1674 switch (error) {
1675 case EDUPFD:
1676 /*
1677 * Check that the mode the file is being opened for is a
1678 * subset of the mode of the existing descriptor.
1679 */
1680 if (((mode & (FREAD|FWRITE)) | fp->f_flag) != fp->f_flag) {
1681 error = EACCES;
1682 break;
1683 }
1684
1685 /* Copy it. */
1686 error = fd_dup(fp, 0, newp, ff->ff_exclose);
1687 break;
1688
1689 case EMOVEFD:
1690 /* Copy it. */
1691 error = fd_dup(fp, 0, newp, ff->ff_exclose);
1692 if (error != 0) {
1693 break;
1694 }
1695
1696 /* Steal away the file pointer from 'old'. */
1697 (void)fd_close(old);
1698 return 0;
1699 }
1700
1701 fd_putfile(old);
1702 return error;
1703 }
1704
1705 /*
1706 * Close open files on exec.
1707 */
1708 void
1709 fd_closeexec(void)
1710 {
1711 proc_t *p;
1712 filedesc_t *fdp;
1713 fdfile_t *ff;
1714 lwp_t *l;
1715 fdtab_t *dt;
1716 int fd;
1717
1718 l = curlwp;
1719 p = l->l_proc;
1720 fdp = p->p_fd;
1721
1722 if (fdp->fd_refcnt > 1) {
1723 fdp = fd_copy();
1724 fd_free();
1725 p->p_fd = fdp;
1726 l->l_fd = fdp;
1727 }
1728 if (!fdp->fd_exclose) {
1729 return;
1730 }
1731 fdp->fd_exclose = false;
1732 dt = fdp->fd_dt;
1733
1734 for (fd = 0; fd <= fdp->fd_lastfile; fd++) {
1735 if ((ff = dt->dt_ff[fd]) == NULL) {
1736 KASSERT(fd >= NDFDFILE);
1737 continue;
1738 }
1739 KASSERT(fd >= NDFDFILE ||
1740 ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
1741 if (ff->ff_file == NULL)
1742 continue;
1743 if (ff->ff_exclose) {
1744 /*
1745 * We need a reference to close the file.
1746 * No other threads can see the fdfile_t at
1747 * this point, so don't bother locking.
1748 */
1749 KASSERT((ff->ff_refcnt & FR_CLOSING) == 0);
1750 ff->ff_refcnt++;
1751 fd_close(fd);
1752 }
1753 }
1754 }
1755
1756 /*
1757 * Sets descriptor owner. If the owner is a process, 'pgid'
1758 * is set to positive value, process ID. If the owner is process group,
1759 * 'pgid' is set to -pg_id.
1760 */
1761 int
1762 fsetown(pid_t *pgid, u_long cmd, const void *data)
1763 {
1764 pid_t id = *(const pid_t *)data;
1765 int error;
1766
1767 switch (cmd) {
1768 case TIOCSPGRP:
1769 if (id < 0)
1770 return EINVAL;
1771 id = -id;
1772 break;
1773 default:
1774 break;
1775 }
1776 if (id > 0) {
1777 mutex_enter(proc_lock);
1778 error = proc_find(id) ? 0 : ESRCH;
1779 mutex_exit(proc_lock);
1780 } else if (id < 0) {
1781 error = pgid_in_session(curproc, -id);
1782 } else {
1783 error = 0;
1784 }
1785 if (!error) {
1786 *pgid = id;
1787 }
1788 return error;
1789 }
1790
1791 void
1792 fd_set_exclose(struct lwp *l, int fd, bool exclose)
1793 {
1794 filedesc_t *fdp = l->l_fd;
1795 fdfile_t *ff = fdp->fd_dt->dt_ff[fd];
1796
1797 ff->ff_exclose = exclose;
1798 if (exclose)
1799 fdp->fd_exclose = true;
1800 }
1801
1802 /*
1803 * Return descriptor owner information. If the value is positive,
1804 * it's process ID. If it's negative, it's process group ID and
1805 * needs the sign removed before use.
1806 */
1807 int
1808 fgetown(pid_t pgid, u_long cmd, void *data)
1809 {
1810
1811 switch (cmd) {
1812 case TIOCGPGRP:
1813 *(int *)data = -pgid;
1814 break;
1815 default:
1816 *(int *)data = pgid;
1817 break;
1818 }
1819 return 0;
1820 }
1821
1822 /*
1823 * Send signal to descriptor owner, either process or process group.
1824 */
1825 void
1826 fownsignal(pid_t pgid, int signo, int code, int band, void *fdescdata)
1827 {
1828 ksiginfo_t ksi;
1829
1830 KASSERT(!cpu_intr_p());
1831
1832 if (pgid == 0) {
1833 return;
1834 }
1835
1836 KSI_INIT(&ksi);
1837 ksi.ksi_signo = signo;
1838 ksi.ksi_code = code;
1839 ksi.ksi_band = band;
1840
1841 mutex_enter(proc_lock);
1842 if (pgid > 0) {
1843 struct proc *p1;
1844
1845 p1 = proc_find(pgid);
1846 if (p1 != NULL) {
1847 kpsignal(p1, &ksi, fdescdata);
1848 }
1849 } else {
1850 struct pgrp *pgrp;
1851
1852 KASSERT(pgid < 0);
1853 pgrp = pgrp_find(-pgid);
1854 if (pgrp != NULL) {
1855 kpgsignal(pgrp, &ksi, fdescdata, 0);
1856 }
1857 }
1858 mutex_exit(proc_lock);
1859 }
1860
1861 int
1862 fd_clone(file_t *fp, unsigned fd, int flag, const struct fileops *fops,
1863 void *data)
1864 {
1865 fdfile_t *ff;
1866 filedesc_t *fdp;
1867
1868 fp->f_flag = flag & FMASK;
1869 fdp = curproc->p_fd;
1870 ff = fdp->fd_dt->dt_ff[fd];
1871 KASSERT(ff != NULL);
1872 ff->ff_exclose = (flag & O_CLOEXEC) != 0;
1873 fp->f_type = DTYPE_MISC;
1874 fp->f_ops = fops;
1875 fp->f_data = data;
1876 curlwp->l_dupfd = fd;
1877 fd_affix(curproc, fp, fd);
1878
1879 return EMOVEFD;
1880 }
1881
1882 int
1883 fnullop_fcntl(file_t *fp, u_int cmd, void *data)
1884 {
1885
1886 if (cmd == F_SETFL)
1887 return 0;
1888
1889 return EOPNOTSUPP;
1890 }
1891
1892 int
1893 fnullop_poll(file_t *fp, int which)
1894 {
1895
1896 return 0;
1897 }
1898
1899 int
1900 fnullop_kqfilter(file_t *fp, struct knote *kn)
1901 {
1902
1903 return EOPNOTSUPP;
1904 }
1905
1906 void
1907 fnullop_restart(file_t *fp)
1908 {
1909
1910 }
1911
1912 int
1913 fbadop_read(file_t *fp, off_t *offset, struct uio *uio,
1914 kauth_cred_t cred, int flags)
1915 {
1916
1917 return EOPNOTSUPP;
1918 }
1919
1920 int
1921 fbadop_write(file_t *fp, off_t *offset, struct uio *uio,
1922 kauth_cred_t cred, int flags)
1923 {
1924
1925 return EOPNOTSUPP;
1926 }
1927
1928 int
1929 fbadop_ioctl(file_t *fp, u_long com, void *data)
1930 {
1931
1932 return EOPNOTSUPP;
1933 }
1934
1935 int
1936 fbadop_stat(file_t *fp, struct stat *sb)
1937 {
1938
1939 return EOPNOTSUPP;
1940 }
1941
1942 int
1943 fbadop_close(file_t *fp)
1944 {
1945
1946 return EOPNOTSUPP;
1947 }
1948
1949 /*
1950 * sysctl routines pertaining to file descriptors
1951 */
1952
1953 /* Initialized in sysctl_init() for now... */
1954 extern kmutex_t sysctl_file_marker_lock;
1955 static u_int sysctl_file_marker = 1;
1956
1957 /*
1958 * Expects to be called with proc_lock and sysctl_file_marker_lock locked.
1959 */
1960 static void
1961 sysctl_file_marker_reset(void)
1962 {
1963 struct proc *p;
1964
1965 PROCLIST_FOREACH(p, &allproc) {
1966 struct filedesc *fd = p->p_fd;
1967 fdtab_t *dt;
1968 u_int i;
1969
1970 mutex_enter(&fd->fd_lock);
1971 dt = fd->fd_dt;
1972 for (i = 0; i < dt->dt_nfiles; i++) {
1973 struct file *fp;
1974 fdfile_t *ff;
1975
1976 if ((ff = dt->dt_ff[i]) == NULL) {
1977 continue;
1978 }
1979 if ((fp = ff->ff_file) == NULL) {
1980 continue;
1981 }
1982 fp->f_marker = 0;
1983 }
1984 mutex_exit(&fd->fd_lock);
1985 }
1986 }
1987
1988 /*
1989 * sysctl helper routine for kern.file pseudo-subtree.
1990 */
1991 static int
1992 sysctl_kern_file(SYSCTLFN_ARGS)
1993 {
1994 const bool allowaddr = get_expose_address(curproc);
1995 struct filelist flist;
1996 int error;
1997 size_t buflen;
1998 struct file *fp, fbuf;
1999 char *start, *where;
2000 struct proc *p;
2001
2002 start = where = oldp;
2003 buflen = *oldlenp;
2004
2005 if (where == NULL) {
2006 /*
2007 * overestimate by 10 files
2008 */
2009 *oldlenp = sizeof(filehead) + (nfiles + 10) *
2010 sizeof(struct file);
2011 return 0;
2012 }
2013
2014 /*
2015 * first sysctl_copyout filehead
2016 */
2017 if (buflen < sizeof(filehead)) {
2018 *oldlenp = 0;
2019 return 0;
2020 }
2021 sysctl_unlock();
2022 if (allowaddr) {
2023 memcpy(&flist, &filehead, sizeof(flist));
2024 } else {
2025 memset(&flist, 0, sizeof(flist));
2026 }
2027 error = sysctl_copyout(l, &flist, where, sizeof(flist));
2028 if (error) {
2029 sysctl_relock();
2030 return error;
2031 }
2032 buflen -= sizeof(flist);
2033 where += sizeof(flist);
2034
2035 /*
2036 * followed by an array of file structures
2037 */
2038 mutex_enter(&sysctl_file_marker_lock);
2039 mutex_enter(proc_lock);
2040 PROCLIST_FOREACH(p, &allproc) {
2041 struct filedesc *fd;
2042 fdtab_t *dt;
2043 u_int i;
2044
2045 if (p->p_stat == SIDL) {
2046 /* skip embryonic processes */
2047 continue;
2048 }
2049 mutex_enter(p->p_lock);
2050 error = kauth_authorize_process(l->l_cred,
2051 KAUTH_PROCESS_CANSEE, p,
2052 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_OPENFILES),
2053 NULL, NULL);
2054 mutex_exit(p->p_lock);
2055 if (error != 0) {
2056 /*
2057 * Don't leak kauth retval if we're silently
2058 * skipping this entry.
2059 */
2060 error = 0;
2061 continue;
2062 }
2063
2064 /*
2065 * Grab a hold on the process.
2066 */
2067 if (!rw_tryenter(&p->p_reflock, RW_READER)) {
2068 continue;
2069 }
2070 mutex_exit(proc_lock);
2071
2072 fd = p->p_fd;
2073 mutex_enter(&fd->fd_lock);
2074 dt = fd->fd_dt;
2075 for (i = 0; i < dt->dt_nfiles; i++) {
2076 fdfile_t *ff;
2077
2078 if ((ff = dt->dt_ff[i]) == NULL) {
2079 continue;
2080 }
2081 if ((fp = ff->ff_file) == NULL) {
2082 continue;
2083 }
2084
2085 mutex_enter(&fp->f_lock);
2086
2087 if ((fp->f_count == 0) ||
2088 (fp->f_marker == sysctl_file_marker)) {
2089 mutex_exit(&fp->f_lock);
2090 continue;
2091 }
2092
2093 /* Check that we have enough space. */
2094 if (buflen < sizeof(struct file)) {
2095 *oldlenp = where - start;
2096 mutex_exit(&fp->f_lock);
2097 error = ENOMEM;
2098 break;
2099 }
2100
2101 fill_file(&fbuf, fp);
2102 mutex_exit(&fp->f_lock);
2103 error = sysctl_copyout(l, &fbuf, where, sizeof(fbuf));
2104 if (error) {
2105 break;
2106 }
2107 buflen -= sizeof(struct file);
2108 where += sizeof(struct file);
2109
2110 fp->f_marker = sysctl_file_marker;
2111 }
2112 mutex_exit(&fd->fd_lock);
2113
2114 /*
2115 * Release reference to process.
2116 */
2117 mutex_enter(proc_lock);
2118 rw_exit(&p->p_reflock);
2119
2120 if (error)
2121 break;
2122 }
2123
2124 sysctl_file_marker++;
2125 /* Reset all markers if wrapped. */
2126 if (sysctl_file_marker == 0) {
2127 sysctl_file_marker_reset();
2128 sysctl_file_marker++;
2129 }
2130
2131 mutex_exit(proc_lock);
2132 mutex_exit(&sysctl_file_marker_lock);
2133
2134 *oldlenp = where - start;
2135 sysctl_relock();
2136 return error;
2137 }
2138
2139 /*
2140 * sysctl helper function for kern.file2
2141 */
2142 static int
2143 sysctl_kern_file2(SYSCTLFN_ARGS)
2144 {
2145 struct proc *p;
2146 struct file *fp;
2147 struct filedesc *fd;
2148 struct kinfo_file kf;
2149 char *dp;
2150 u_int i, op;
2151 size_t len, needed, elem_size, out_size;
2152 int error, arg, elem_count;
2153 fdfile_t *ff;
2154 fdtab_t *dt;
2155
2156 if (namelen == 1 && name[0] == CTL_QUERY)
2157 return sysctl_query(SYSCTLFN_CALL(rnode));
2158
2159 if (namelen != 4)
2160 return EINVAL;
2161
2162 error = 0;
2163 dp = oldp;
2164 len = (oldp != NULL) ? *oldlenp : 0;
2165 op = name[0];
2166 arg = name[1];
2167 elem_size = name[2];
2168 elem_count = name[3];
2169 out_size = MIN(sizeof(kf), elem_size);
2170 needed = 0;
2171
2172 if (elem_size < 1 || elem_count < 0)
2173 return EINVAL;
2174
2175 switch (op) {
2176 case KERN_FILE_BYFILE:
2177 case KERN_FILE_BYPID:
2178 /*
2179 * We're traversing the process list in both cases; the BYFILE
2180 * case does additional work of keeping track of files already
2181 * looked at.
2182 */
2183
2184 /* doesn't use arg so it must be zero */
2185 if ((op == KERN_FILE_BYFILE) && (arg != 0))
2186 return EINVAL;
2187
2188 if ((op == KERN_FILE_BYPID) && (arg < -1))
2189 /* -1 means all processes */
2190 return EINVAL;
2191
2192 sysctl_unlock();
2193 if (op == KERN_FILE_BYFILE)
2194 mutex_enter(&sysctl_file_marker_lock);
2195 mutex_enter(proc_lock);
2196 PROCLIST_FOREACH(p, &allproc) {
2197 if (p->p_stat == SIDL) {
2198 /* skip embryonic processes */
2199 continue;
2200 }
2201 if (arg > 0 && p->p_pid != arg) {
2202 /* pick only the one we want */
2203 /* XXX want 0 to mean "kernel files" */
2204 continue;
2205 }
2206 mutex_enter(p->p_lock);
2207 error = kauth_authorize_process(l->l_cred,
2208 KAUTH_PROCESS_CANSEE, p,
2209 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_OPENFILES),
2210 NULL, NULL);
2211 mutex_exit(p->p_lock);
2212 if (error != 0) {
2213 /*
2214 * Don't leak kauth retval if we're silently
2215 * skipping this entry.
2216 */
2217 error = 0;
2218 continue;
2219 }
2220
2221 /*
2222 * Grab a hold on the process.
2223 */
2224 if (!rw_tryenter(&p->p_reflock, RW_READER)) {
2225 continue;
2226 }
2227 mutex_exit(proc_lock);
2228
2229 fd = p->p_fd;
2230 mutex_enter(&fd->fd_lock);
2231 dt = fd->fd_dt;
2232 for (i = 0; i < dt->dt_nfiles; i++) {
2233 if ((ff = dt->dt_ff[i]) == NULL) {
2234 continue;
2235 }
2236 if ((fp = ff->ff_file) == NULL) {
2237 continue;
2238 }
2239
2240 if ((op == KERN_FILE_BYFILE) &&
2241 (fp->f_marker == sysctl_file_marker)) {
2242 continue;
2243 }
2244 if (len >= elem_size && elem_count > 0) {
2245 mutex_enter(&fp->f_lock);
2246 fill_file2(&kf, fp, ff, i, p->p_pid);
2247 mutex_exit(&fp->f_lock);
2248 mutex_exit(&fd->fd_lock);
2249 error = sysctl_copyout(l,
2250 &kf, dp, out_size);
2251 mutex_enter(&fd->fd_lock);
2252 if (error)
2253 break;
2254 dp += elem_size;
2255 len -= elem_size;
2256 }
2257 if (op == KERN_FILE_BYFILE)
2258 fp->f_marker = sysctl_file_marker;
2259 needed += elem_size;
2260 if (elem_count > 0 && elem_count != INT_MAX)
2261 elem_count--;
2262 }
2263 mutex_exit(&fd->fd_lock);
2264
2265 /*
2266 * Release reference to process.
2267 */
2268 mutex_enter(proc_lock);
2269 rw_exit(&p->p_reflock);
2270 }
2271 if (op == KERN_FILE_BYFILE) {
2272 sysctl_file_marker++;
2273
2274 /* Reset all markers if wrapped. */
2275 if (sysctl_file_marker == 0) {
2276 sysctl_file_marker_reset();
2277 sysctl_file_marker++;
2278 }
2279 }
2280 mutex_exit(proc_lock);
2281 if (op == KERN_FILE_BYFILE)
2282 mutex_exit(&sysctl_file_marker_lock);
2283 sysctl_relock();
2284 break;
2285 default:
2286 return EINVAL;
2287 }
2288
2289 if (oldp == NULL)
2290 needed += KERN_FILESLOP * elem_size;
2291 *oldlenp = needed;
2292
2293 return error;
2294 }
2295
2296 static void
2297 fill_file(struct file *fp, const struct file *fpsrc)
2298 {
2299 const bool allowaddr = get_expose_address(curproc);
2300
2301 memset(fp, 0, sizeof(*fp));
2302
2303 fp->f_offset = fpsrc->f_offset;
2304 COND_SET_VALUE(fp->f_cred, fpsrc->f_cred, allowaddr);
2305 COND_SET_VALUE(fp->f_ops, fpsrc->f_ops, allowaddr);
2306 COND_SET_VALUE(fp->f_undata, fpsrc->f_undata, allowaddr);
2307 COND_SET_VALUE(fp->f_list, fpsrc->f_list, allowaddr);
2308 COND_SET_VALUE(fp->f_lock, fpsrc->f_lock, allowaddr);
2309 fp->f_flag = fpsrc->f_flag;
2310 fp->f_marker = fpsrc->f_marker;
2311 fp->f_type = fpsrc->f_type;
2312 fp->f_advice = fpsrc->f_advice;
2313 fp->f_count = fpsrc->f_count;
2314 fp->f_msgcount = fpsrc->f_msgcount;
2315 fp->f_unpcount = fpsrc->f_unpcount;
2316 COND_SET_VALUE(fp->f_unplist, fpsrc->f_unplist, allowaddr);
2317 }
2318
2319 static void
2320 fill_file2(struct kinfo_file *kp, const file_t *fp, const fdfile_t *ff,
2321 int i, pid_t pid)
2322 {
2323 const bool allowaddr = get_expose_address(curproc);
2324
2325 memset(kp, 0, sizeof(*kp));
2326
2327 COND_SET_VALUE(kp->ki_fileaddr, PTRTOUINT64(fp), allowaddr);
2328 kp->ki_flag = fp->f_flag;
2329 kp->ki_iflags = 0;
2330 kp->ki_ftype = fp->f_type;
2331 kp->ki_count = fp->f_count;
2332 kp->ki_msgcount = fp->f_msgcount;
2333 COND_SET_VALUE(kp->ki_fucred, PTRTOUINT64(fp->f_cred), allowaddr);
2334 kp->ki_fuid = kauth_cred_geteuid(fp->f_cred);
2335 kp->ki_fgid = kauth_cred_getegid(fp->f_cred);
2336 COND_SET_VALUE(kp->ki_fops, PTRTOUINT64(fp->f_ops), allowaddr);
2337 kp->ki_foffset = fp->f_offset;
2338 COND_SET_VALUE(kp->ki_fdata, PTRTOUINT64(fp->f_data), allowaddr);
2339
2340 /* vnode information to glue this file to something */
2341 if (fp->f_type == DTYPE_VNODE) {
2342 struct vnode *vp = fp->f_vnode;
2343
2344 COND_SET_VALUE(kp->ki_vun, PTRTOUINT64(vp->v_un.vu_socket),
2345 allowaddr);
2346 kp->ki_vsize = vp->v_size;
2347 kp->ki_vtype = vp->v_type;
2348 kp->ki_vtag = vp->v_tag;
2349 COND_SET_VALUE(kp->ki_vdata, PTRTOUINT64(vp->v_data),
2350 allowaddr);
2351 }
2352
2353 /* process information when retrieved via KERN_FILE_BYPID */
2354 if (ff != NULL) {
2355 kp->ki_pid = pid;
2356 kp->ki_fd = i;
2357 kp->ki_ofileflags = ff->ff_exclose;
2358 kp->ki_usecount = ff->ff_refcnt;
2359 }
2360 }
2361