Disallow writes to filesystems mounted read-only via NULLFS. In this case
[dragonfly.git] / sys / kern / kern_descrip.c
CommitLineData
984263bc 1/*
29d211fb
MD
2 * Copyright (c) 2005 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Jeffrey Hsu.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 *
984263bc
MD
35 * Copyright (c) 1982, 1986, 1989, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
37 * (c) UNIX System Laboratories, Inc.
38 * All or some portions of this file are derived from material licensed
39 * to the University of California by American Telephone and Telegraph
40 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
41 * the permission of UNIX System Laboratories, Inc.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94
c4cb6d8b 72 * $FreeBSD: src/sys/kern/kern_descrip.c,v 1.81.2.19 2004/02/28 00:43:31 tegge Exp $
77652cad 73 * $DragonFly: src/sys/kern/kern_descrip.c,v 1.73 2006/09/05 03:48:12 dillon Exp $
984263bc
MD
74 */
75
76#include "opt_compat.h"
77#include <sys/param.h>
78#include <sys/systm.h>
79#include <sys/malloc.h>
80#include <sys/sysproto.h>
81#include <sys/conf.h>
fef8985e 82#include <sys/device.h>
984263bc
MD
83#include <sys/filedesc.h>
84#include <sys/kernel.h>
85#include <sys/sysctl.h>
86#include <sys/vnode.h>
87#include <sys/proc.h>
fad57d0e 88#include <sys/nlookup.h>
984263bc
MD
89#include <sys/file.h>
90#include <sys/stat.h>
91#include <sys/filio.h>
92#include <sys/fcntl.h>
93#include <sys/unistd.h>
94#include <sys/resourcevar.h>
95#include <sys/event.h>
dda4b42b 96#include <sys/kern_syscall.h>
1c55bd1c 97#include <sys/kcore.h>
7b124c9f 98#include <sys/kinfo.h>
984263bc
MD
99
100#include <vm/vm.h>
101#include <vm/vm_extern.h>
102
e43a034f 103#include <sys/thread2.h>
dadab5e9 104#include <sys/file2.h>
85fceac1 105#include <sys/spinlock2.h>
dadab5e9 106
5b287bba
MD
107static void fsetfd_locked(struct filedesc *fdp, struct file *fp, int fd);
108static void fdreserve_locked (struct filedesc *fdp, int fd0, int incr);
109static struct file *funsetfd_locked (struct filedesc *fdp, int fd);
85fceac1 110static int checkfpclosed(struct filedesc *fdp, int fd, struct file *fp);
5b287bba 111static void ffree(struct file *fp);
fa541be6 112
984263bc
MD
113static MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
114static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "file desc to leader",
115 "file desc to leader structures");
116MALLOC_DEFINE(M_FILE, "file", "Open file structure");
117static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
118
119static d_open_t fdopen;
120#define NUMFDESC 64
121
122#define CDEV_MAJOR 22
fef8985e
MD
123static struct dev_ops fildesc_ops = {
124 { "FD", CDEV_MAJOR, 0 },
125 .d_open = fdopen,
984263bc
MD
126};
127
402ed7e1 128static int badfo_readwrite (struct file *fp, struct uio *uio,
87de5057 129 struct ucred *cred, int flags);
402ed7e1 130static int badfo_ioctl (struct file *fp, u_long com, caddr_t data,
87de5057
MD
131 struct ucred *cred);
132static int badfo_poll (struct file *fp, int events, struct ucred *cred);
402ed7e1 133static int badfo_kqfilter (struct file *fp, struct knote *kn);
87de5057
MD
134static int badfo_stat (struct file *fp, struct stat *sb, struct ucred *cred);
135static int badfo_close (struct file *fp);
136static int badfo_shutdown (struct file *fp, int how);
984263bc
MD
137
138/*
139 * Descriptor management.
140 */
2dd63755
MD
141static struct filelist filehead = LIST_HEAD_INITIALIZER(&filehead);
142static struct spinlock filehead_spin = SPINLOCK_INITIALIZER(&filehead_spin);
143static int nfiles; /* actual number of open files */
984263bc
MD
144extern int cmask;
145
8247b2f9
MD
146/*
147 * Fixup fd_freefile and fd_lastfile after a descriptor has been cleared.
5b287bba
MD
148 *
149 * MPSAFE - must be called with fdp->fd_spin exclusively held
8247b2f9
MD
150 */
151static __inline
152void
5b287bba 153fdfixup_locked(struct filedesc *fdp, int fd)
8247b2f9
MD
154{
155 if (fd < fdp->fd_freefile) {
156 fdp->fd_freefile = fd;
157 }
158 while (fdp->fd_lastfile >= 0 &&
159 fdp->fd_files[fdp->fd_lastfile].fp == NULL &&
160 fdp->fd_files[fdp->fd_lastfile].reserved == 0
161 ) {
162 --fdp->fd_lastfile;
163 }
164}
165
984263bc
MD
166/*
167 * System calls on descriptors.
5b287bba
MD
168 *
169 * MPSAFE
984263bc 170 */
984263bc 171int
753fd850 172sys_getdtablesize(struct getdtablesize_args *uap)
984263bc 173{
41c20dac 174 struct proc *p = curproc;
5b287bba 175 struct plimit *limit = p->p_limit;
984263bc 176
5b287bba 177 spin_lock_rd(&limit->p_spin);
c7114eea 178 uap->sysmsg_result =
5b287bba
MD
179 min((int)limit->pl_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
180 spin_unlock_rd(&limit->p_spin);
984263bc
MD
181 return (0);
182}
183
184/*
185 * Duplicate a file descriptor to a particular value.
186 *
187 * note: keep in mind that a potential race condition exists when closing
188 * descriptors from a shared descriptor table (via rfork).
5b287bba
MD
189 *
190 * MPSAFE
984263bc 191 */
984263bc 192int
753fd850 193sys_dup2(struct dup2_args *uap)
984263bc 194{
dda4b42b
DRJ
195 int error;
196
197 error = kern_dup(DUP_FIXED, uap->from, uap->to, uap->sysmsg_fds);
198
199 return (error);
984263bc
MD
200}
201
202/*
203 * Duplicate a file descriptor.
5b287bba
MD
204 *
205 * MPSAFE
984263bc 206 */
984263bc 207int
753fd850 208sys_dup(struct dup_args *uap)
984263bc 209{
dda4b42b 210 int error;
984263bc 211
dda4b42b
DRJ
212 error = kern_dup(DUP_VARIABLE, uap->fd, 0, uap->sysmsg_fds);
213
214 return (error);
984263bc
MD
215}
216
5b287bba
MD
217/*
218 * MPALMOSTSAFE - acquires mplock for fp operations
219 */
984263bc 220int
87de5057 221kern_fcntl(int fd, int cmd, union fcntl_dat *dat, struct ucred *cred)
984263bc 222{
dadab5e9
MD
223 struct thread *td = curthread;
224 struct proc *p = td->td_proc;
41c20dac 225 struct file *fp;
984263bc 226 struct vnode *vp;
984263bc 227 u_int newmin;
9ba76b73 228 u_int oflags;
dda4b42b 229 int tmp, error, flg = F_POSIX;
984263bc 230
dadab5e9
MD
231 KKASSERT(p);
232
5b287bba
MD
233 /*
234 * Operations on file descriptors that do not require a file pointer.
235 */
dda4b42b 236 switch (cmd) {
984263bc 237 case F_GETFD:
85fceac1
MD
238 error = fgetfdflags(p->p_fd, fd, &tmp);
239 if (error == 0)
240 dat->fc_cloexec = (tmp & UF_EXCLOSE) ? FD_CLOEXEC : 0;
5b287bba 241 return (error);
984263bc
MD
242
243 case F_SETFD:
85fceac1
MD
244 if (dat->fc_cloexec & FD_CLOEXEC)
245 error = fsetfdflags(p->p_fd, fd, UF_EXCLOSE);
246 else
247 error = fclrfdflags(p->p_fd, fd, UF_EXCLOSE);
5b287bba
MD
248 return (error);
249 case F_DUPFD:
250 newmin = dat->fc_fd;
251 error = kern_dup(DUP_VARIABLE, fd, newmin, &dat->fc_fd);
252 return (error);
253 default:
85fceac1 254 break;
5b287bba 255 }
984263bc 256
5b287bba
MD
257 /*
258 * Operations on file pointers
259 */
260 if ((fp = holdfp(p->p_fd, fd, -1)) == NULL)
261 return (EBADF);
262
263 get_mplock();
264 switch (cmd) {
984263bc 265 case F_GETFL:
dda4b42b 266 dat->fc_flags = OFLAGS(fp->f_flag);
85fceac1
MD
267 error = 0;
268 break;
984263bc
MD
269
270 case F_SETFL:
9ba76b73 271 oflags = fp->f_flag & FCNTLFLAGS;
984263bc 272 fp->f_flag &= ~FCNTLFLAGS;
dda4b42b 273 fp->f_flag |= FFLAGS(dat->fc_flags & ~O_ACCMODE) & FCNTLFLAGS;
f58b505b
MD
274 error = 0;
275 if ((fp->f_flag ^ oflags) & FASYNC) {
276 tmp = fp->f_flag & FASYNC;
277 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, cred);
278 }
279 if (error)
280 fp->f_flag = (fp->f_flag & ~FCNTLFLAGS) | oflags;
85fceac1 281 break;
984263bc
MD
282
283 case F_GETOWN:
87de5057 284 error = fo_ioctl(fp, FIOGETOWN, (caddr_t)&dat->fc_owner, cred);
85fceac1 285 break;
984263bc
MD
286
287 case F_SETOWN:
87de5057 288 error = fo_ioctl(fp, FIOSETOWN, (caddr_t)&dat->fc_owner, cred);
85fceac1 289 break;
984263bc
MD
290
291 case F_SETLKW:
292 flg |= F_WAIT;
293 /* Fall into F_SETLK */
294
295 case F_SETLK:
85fceac1
MD
296 if (fp->f_type != DTYPE_VNODE) {
297 error = EBADF;
298 break;
299 }
984263bc
MD
300 vp = (struct vnode *)fp->f_data;
301
302 /*
303 * copyin/lockop may block
304 */
dda4b42b
DRJ
305 if (dat->fc_flock.l_whence == SEEK_CUR)
306 dat->fc_flock.l_start += fp->f_offset;
984263bc 307
dda4b42b 308 switch (dat->fc_flock.l_type) {
984263bc
MD
309 case F_RDLCK:
310 if ((fp->f_flag & FREAD) == 0) {
311 error = EBADF;
312 break;
313 }
314 p->p_leader->p_flag |= P_ADVLOCK;
315 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
dda4b42b 316 &dat->fc_flock, flg);
984263bc
MD
317 break;
318 case F_WRLCK:
319 if ((fp->f_flag & FWRITE) == 0) {
320 error = EBADF;
321 break;
322 }
323 p->p_leader->p_flag |= P_ADVLOCK;
324 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
dda4b42b 325 &dat->fc_flock, flg);
984263bc
MD
326 break;
327 case F_UNLCK:
328 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
dda4b42b 329 &dat->fc_flock, F_POSIX);
984263bc
MD
330 break;
331 default:
332 error = EINVAL;
333 break;
334 }
85fceac1
MD
335
336 /*
337 * It is possible to race a close() on the descriptor while
338 * we were blocked getting the lock. If this occurs the
339 * close might not have caught the lock.
340 */
341 if (checkfpclosed(p->p_fd, fd, fp)) {
dda4b42b
DRJ
342 dat->fc_flock.l_whence = SEEK_SET;
343 dat->fc_flock.l_start = 0;
344 dat->fc_flock.l_len = 0;
345 dat->fc_flock.l_type = F_UNLCK;
984263bc 346 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
dda4b42b 347 F_UNLCK, &dat->fc_flock, F_POSIX);
984263bc 348 }
85fceac1 349 break;
984263bc
MD
350
351 case F_GETLK:
85fceac1
MD
352 if (fp->f_type != DTYPE_VNODE) {
353 error = EBADF;
354 break;
355 }
984263bc
MD
356 vp = (struct vnode *)fp->f_data;
357 /*
358 * copyin/lockop may block
359 */
dda4b42b
DRJ
360 if (dat->fc_flock.l_type != F_RDLCK &&
361 dat->fc_flock.l_type != F_WRLCK &&
362 dat->fc_flock.l_type != F_UNLCK) {
85fceac1
MD
363 error = EINVAL;
364 break;
984263bc 365 }
dda4b42b
DRJ
366 if (dat->fc_flock.l_whence == SEEK_CUR)
367 dat->fc_flock.l_start += fp->f_offset;
984263bc 368 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK,
dda4b42b 369 &dat->fc_flock, F_POSIX);
85fceac1 370 break;
984263bc 371 default:
85fceac1
MD
372 error = EINVAL;
373 break;
984263bc 374 }
5b287bba
MD
375 rel_mplock();
376
85fceac1
MD
377 fdrop(fp);
378 return (error);
984263bc
MD
379}
380
dda4b42b
DRJ
381/*
382 * The file control system call.
5b287bba
MD
383 *
384 * MPSAFE
dda4b42b
DRJ
385 */
386int
753fd850 387sys_fcntl(struct fcntl_args *uap)
dda4b42b
DRJ
388{
389 union fcntl_dat dat;
390 int error;
391
392 switch (uap->cmd) {
393 case F_DUPFD:
394 dat.fc_fd = uap->arg;
395 break;
396 case F_SETFD:
397 dat.fc_cloexec = uap->arg;
398 break;
399 case F_SETFL:
400 dat.fc_flags = uap->arg;
401 break;
402 case F_SETOWN:
403 dat.fc_owner = uap->arg;
404 break;
405 case F_SETLKW:
406 case F_SETLK:
407 case F_GETLK:
408 error = copyin((caddr_t)uap->arg, &dat.fc_flock,
5b287bba 409 sizeof(struct flock));
dda4b42b
DRJ
410 if (error)
411 return (error);
412 break;
413 }
414
87de5057 415 error = kern_fcntl(uap->fd, uap->cmd, &dat, curproc->p_ucred);
dda4b42b
DRJ
416
417 if (error == 0) {
418 switch (uap->cmd) {
419 case F_DUPFD:
420 uap->sysmsg_result = dat.fc_fd;
421 break;
422 case F_GETFD:
423 uap->sysmsg_result = dat.fc_cloexec;
424 break;
425 case F_GETFL:
426 uap->sysmsg_result = dat.fc_flags;
427 break;
428 case F_GETOWN:
429 uap->sysmsg_result = dat.fc_owner;
430 case F_GETLK:
431 error = copyout(&dat.fc_flock, (caddr_t)uap->arg,
432 sizeof(struct flock));
433 break;
434 }
435 }
436
437 return (error);
438}
439
984263bc
MD
440/*
441 * Common code for dup, dup2, and fcntl(F_DUPFD).
dda4b42b
DRJ
442 *
443 * The type flag can be either DUP_FIXED or DUP_VARIABLE. DUP_FIXED tells
444 * kern_dup() to destructively dup over an existing file descriptor if new
445 * is already open. DUP_VARIABLE tells kern_dup() to find the lowest
446 * unused file descriptor that is greater than or equal to new.
5b287bba
MD
447 *
448 * MPSAFE
984263bc 449 */
dda4b42b
DRJ
450int
451kern_dup(enum dup_type type, int old, int new, int *res)
984263bc 452{
dda4b42b
DRJ
453 struct thread *td = curthread;
454 struct proc *p = td->td_proc;
455 struct filedesc *fdp = p->p_fd;
984263bc
MD
456 struct file *fp;
457 struct file *delfp;
259b8ea0 458 int oldflags;
984263bc 459 int holdleaders;
dda4b42b
DRJ
460 int error, newfd;
461
462 /*
463 * Verify that we have a valid descriptor to dup from and
464 * possibly to dup to.
465 */
259b8ea0 466retry:
5b287bba
MD
467 spin_lock_wr(&fdp->fd_spin);
468 if (new < 0 || new > p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
469 new >= maxfilesperproc) {
470 spin_unlock_wr(&fdp->fd_spin);
471 return (EINVAL);
472 }
473 if ((unsigned)old >= fdp->fd_nfiles || fdp->fd_files[old].fp == NULL) {
474 spin_unlock_wr(&fdp->fd_spin);
dda4b42b 475 return (EBADF);
5b287bba 476 }
dda4b42b
DRJ
477 if (type == DUP_FIXED && old == new) {
478 *res = new;
5b287bba 479 spin_unlock_wr(&fdp->fd_spin);
dda4b42b
DRJ
480 return (0);
481 }
0679adc4 482 fp = fdp->fd_files[old].fp;
259b8ea0 483 oldflags = fdp->fd_files[old].fileflags;
5b287bba 484 fhold(fp); /* MPSAFE - can be called with a spinlock held */
dda4b42b
DRJ
485
486 /*
259b8ea0
MD
487 * Allocate a new descriptor if DUP_VARIABLE, or expand the table
488 * if the requested descriptor is beyond the current table size.
489 *
490 * This can block. Retry if the source descriptor no longer matches
491 * or if our expectation in the expansion case races.
492 *
493 * If we are not expanding or allocating a new decriptor, then reset
494 * the target descriptor to a reserved state so we have a uniform
495 * setup for the next code block.
dda4b42b
DRJ
496 */
497 if (type == DUP_VARIABLE || new >= fdp->fd_nfiles) {
5b287bba 498 spin_unlock_wr(&fdp->fd_spin);
dda4b42b 499 error = fdalloc(p, new, &newfd);
5b287bba 500 spin_lock_wr(&fdp->fd_spin);
dda4b42b 501 if (error) {
5b287bba 502 spin_unlock_wr(&fdp->fd_spin);
9f87144f 503 fdrop(fp);
dda4b42b
DRJ
504 return (error);
505 }
5b287bba
MD
506 /*
507 * Check for ripout
508 */
259b8ea0 509 if (old >= fdp->fd_nfiles || fdp->fd_files[old].fp != fp) {
5b287bba
MD
510 fsetfd_locked(fdp, NULL, newfd);
511 spin_unlock_wr(&fdp->fd_spin);
259b8ea0
MD
512 fdrop(fp);
513 goto retry;
514 }
5b287bba
MD
515 /*
516 * Check for expansion race
517 */
259b8ea0 518 if (type != DUP_VARIABLE && new != newfd) {
5b287bba
MD
519 fsetfd_locked(fdp, NULL, newfd);
520 spin_unlock_wr(&fdp->fd_spin);
259b8ea0
MD
521 fdrop(fp);
522 goto retry;
523 }
5b287bba
MD
524 /*
525 * Check for ripout, newfd reused old (this case probably
526 * can't occur).
527 */
259b8ea0 528 if (old == newfd) {
5b287bba
MD
529 fsetfd_locked(fdp, NULL, newfd);
530 spin_unlock_wr(&fdp->fd_spin);
259b8ea0
MD
531 fdrop(fp);
532 goto retry;
533 }
dda4b42b 534 new = newfd;
259b8ea0
MD
535 delfp = NULL;
536 } else {
537 if (fdp->fd_files[new].reserved) {
5b287bba 538 spin_unlock_wr(&fdp->fd_spin);
259b8ea0
MD
539 fdrop(fp);
540 printf("Warning: dup(): target descriptor %d is reserved, waiting for it to be resolved\n", new);
541 tsleep(fdp, 0, "fdres", hz);
542 goto retry;
543 }
dda4b42b 544
259b8ea0
MD
545 /*
546 * If the target descriptor was never allocated we have
547 * to allocate it. If it was we have to clean out the
5b287bba
MD
548 * old descriptor. delfp inherits the ref from the
549 * descriptor table.
259b8ea0
MD
550 */
551 delfp = fdp->fd_files[new].fp;
552 fdp->fd_files[new].fp = NULL;
553 fdp->fd_files[new].reserved = 1;
554 if (delfp == NULL) {
5b287bba 555 fdreserve_locked(fdp, new, 1);
259b8ea0
MD
556 if (new > fdp->fd_lastfile)
557 fdp->fd_lastfile = new;
dda4b42b 558 }
259b8ea0 559
dda4b42b 560 }
984263bc 561
5b287bba
MD
562 /*
563 * NOTE: still holding an exclusive spinlock
564 */
565
984263bc 566 /*
259b8ea0
MD
567 * If a descriptor is being overwritten we may hve to tell
568 * fdfree() to sleep to ensure that all relevant process
569 * leaders can be traversed in closef().
984263bc 570 */
984263bc 571 if (delfp != NULL && p->p_fdtol != NULL) {
984263bc
MD
572 fdp->fd_holdleaderscount++;
573 holdleaders = 1;
259b8ea0 574 } else {
984263bc 575 holdleaders = 0;
259b8ea0 576 }
dda4b42b 577 KASSERT(delfp == NULL || type == DUP_FIXED,
259b8ea0 578 ("dup() picked an open file"));
984263bc
MD
579
580 /*
259b8ea0
MD
581 * Duplicate the source descriptor, update lastfile. If the new
582 * descriptor was not allocated and we aren't replacing an existing
583 * descriptor we have to mark the descriptor as being in use.
584 *
585 * The fd_files[] array inherits fp's hold reference.
984263bc 586 */
5b287bba 587 fsetfd_locked(fdp, fp, new);
259b8ea0 588 fdp->fd_files[new].fileflags = oldflags & ~UF_EXCLOSE;
5b287bba
MD
589 spin_unlock_wr(&fdp->fd_spin);
590 fdrop(fp);
dda4b42b 591 *res = new;
984263bc
MD
592
593 /*
594 * If we dup'd over a valid file, we now own the reference to it
595 * and must dispose of it using closef() semantics (as if a
596 * close() were performed on it).
597 */
598 if (delfp) {
5b287bba 599 (void)closef(delfp, td);
984263bc 600 if (holdleaders) {
5b287bba 601 spin_lock_wr(&fdp->fd_spin);
984263bc
MD
602 fdp->fd_holdleaderscount--;
603 if (fdp->fd_holdleaderscount == 0 &&
604 fdp->fd_holdleaderswakeup != 0) {
605 fdp->fd_holdleaderswakeup = 0;
5b287bba 606 spin_unlock_wr(&fdp->fd_spin);
984263bc 607 wakeup(&fdp->fd_holdleaderscount);
5b287bba
MD
608 } else {
609 spin_unlock_wr(&fdp->fd_spin);
984263bc
MD
610 }
611 }
612 }
613 return (0);
614}
615
616/*
617 * If sigio is on the list associated with a process or process group,
618 * disable signalling from the device, remove sigio from the list and
619 * free sigio.
620 */
621void
7bf8660a 622funsetown(struct sigio *sigio)
984263bc 623{
984263bc
MD
624 if (sigio == NULL)
625 return;
e43a034f 626 crit_enter();
984263bc 627 *(sigio->sio_myref) = NULL;
e43a034f 628 crit_exit();
984263bc
MD
629 if (sigio->sio_pgid < 0) {
630 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
631 sigio, sio_pgsigio);
632 } else /* if ((*sigiop)->sio_pgid > 0) */ {
633 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
634 sigio, sio_pgsigio);
635 }
636 crfree(sigio->sio_ucred);
efda3bd0 637 kfree(sigio, M_SIGIO);
984263bc
MD
638}
639
640/* Free a list of sigio structures. */
641void
7bf8660a 642funsetownlst(struct sigiolst *sigiolst)
984263bc
MD
643{
644 struct sigio *sigio;
645
646 while ((sigio = SLIST_FIRST(sigiolst)) != NULL)
647 funsetown(sigio);
648}
649
650/*
651 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
652 *
653 * After permission checking, add a sigio structure to the sigio list for
654 * the process or process group.
655 */
656int
7bf8660a 657fsetown(pid_t pgid, struct sigio **sigiop)
984263bc
MD
658{
659 struct proc *proc;
660 struct pgrp *pgrp;
661 struct sigio *sigio;
984263bc
MD
662
663 if (pgid == 0) {
664 funsetown(*sigiop);
665 return (0);
666 }
667 if (pgid > 0) {
668 proc = pfind(pgid);
669 if (proc == NULL)
670 return (ESRCH);
671
672 /*
673 * Policy - Don't allow a process to FSETOWN a process
674 * in another session.
675 *
676 * Remove this test to allow maximum flexibility or
677 * restrict FSETOWN to the current process or process
678 * group for maximum safety.
679 */
680 if (proc->p_session != curproc->p_session)
681 return (EPERM);
682
683 pgrp = NULL;
684 } else /* if (pgid < 0) */ {
685 pgrp = pgfind(-pgid);
686 if (pgrp == NULL)
687 return (ESRCH);
688
689 /*
690 * Policy - Don't allow a process to FSETOWN a process
691 * in another session.
692 *
693 * Remove this test to allow maximum flexibility or
694 * restrict FSETOWN to the current process or process
695 * group for maximum safety.
696 */
697 if (pgrp->pg_session != curproc->p_session)
698 return (EPERM);
699
700 proc = NULL;
701 }
702 funsetown(*sigiop);
efda3bd0 703 sigio = kmalloc(sizeof(struct sigio), M_SIGIO, M_WAITOK);
984263bc
MD
704 if (pgid > 0) {
705 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
706 sigio->sio_proc = proc;
707 } else {
708 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
709 sigio->sio_pgrp = pgrp;
710 }
711 sigio->sio_pgid = pgid;
e9a372eb 712 sigio->sio_ucred = crhold(curproc->p_ucred);
984263bc 713 /* It would be convenient if p_ruid was in ucred. */
41c20dac 714 sigio->sio_ruid = curproc->p_ucred->cr_ruid;
984263bc 715 sigio->sio_myref = sigiop;
e43a034f 716 crit_enter();
984263bc 717 *sigiop = sigio;
e43a034f 718 crit_exit();
984263bc
MD
719 return (0);
720}
721
722/*
723 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
724 */
725pid_t
7bf8660a 726fgetown(struct sigio *sigio)
984263bc
MD
727{
728 return (sigio != NULL ? sigio->sio_pgid : 0);
729}
730
4336d5df
JS
731/*
732 * Close many file descriptors.
5b287bba
MD
733 *
734 * MPSAFE
4336d5df 735 */
4336d5df 736int
753fd850 737sys_closefrom(struct closefrom_args *uap)
4336d5df
JS
738{
739 return(kern_closefrom(uap->fd));
740}
741
5b287bba
MD
742/*
743 * Close all file descriptors greater then or equal to fd
744 *
745 * MPSAFE
746 */
4336d5df
JS
747int
748kern_closefrom(int fd)
749{
750 struct thread *td = curthread;
751 struct proc *p = td->td_proc;
752 struct filedesc *fdp;
4336d5df
JS
753
754 KKASSERT(p);
755 fdp = p->p_fd;
756
5b287bba
MD
757 if (fd < 0)
758 return (EINVAL);
5e8cfac8 759
259b8ea0
MD
760 /*
761 * NOTE: This function will skip unassociated descriptors and
762 * reserved descriptors that have not yet been assigned.
763 * fd_lastfile can change as a side effect of kern_close().
764 */
5b287bba 765 spin_lock_wr(&fdp->fd_spin);
259b8ea0
MD
766 while (fd <= fdp->fd_lastfile) {
767 if (fdp->fd_files[fd].fp != NULL) {
5b287bba
MD
768 spin_unlock_wr(&fdp->fd_spin);
769 /* ok if this races another close */
259b8ea0
MD
770 if (kern_close(fd) == EINTR)
771 return (EINTR);
5b287bba 772 spin_lock_wr(&fdp->fd_spin);
259b8ea0
MD
773 }
774 ++fd;
775 }
5b287bba 776 spin_unlock_wr(&fdp->fd_spin);
4336d5df
JS
777 return (0);
778}
779
984263bc
MD
780/*
781 * Close a file descriptor.
5b287bba
MD
782 *
783 * MPSAFE
984263bc 784 */
984263bc 785int
753fd850 786sys_close(struct close_args *uap)
12693083
MD
787{
788 return(kern_close(uap->fd));
789}
790
5b287bba
MD
791/*
792 * MPALMOSTSAFE - acquires mplock around knote_fdclose() calls
793 */
12693083
MD
794int
795kern_close(int fd)
984263bc 796{
dadab5e9
MD
797 struct thread *td = curthread;
798 struct proc *p = td->td_proc;
799 struct filedesc *fdp;
41c20dac 800 struct file *fp;
984263bc
MD
801 int error;
802 int holdleaders;
803
dadab5e9
MD
804 KKASSERT(p);
805 fdp = p->p_fd;
806
5b287bba
MD
807 spin_lock_wr(&fdp->fd_spin);
808 if ((fp = funsetfd_locked(fdp, fd)) == NULL) {
809 spin_unlock_wr(&fdp->fd_spin);
984263bc 810 return (EBADF);
5b287bba 811 }
984263bc
MD
812 holdleaders = 0;
813 if (p->p_fdtol != NULL) {
814 /*
815 * Ask fdfree() to sleep to ensure that all relevant
816 * process leaders can be traversed in closef().
817 */
818 fdp->fd_holdleaderscount++;
819 holdleaders = 1;
820 }
821
822 /*
823 * we now hold the fp reference that used to be owned by the descriptor
824 * array.
825 */
5b287bba
MD
826 spin_unlock_wr(&fdp->fd_spin);
827 if (fd < fdp->fd_knlistsize) {
828 get_mplock();
7749886d
MD
829 if (fd < fdp->fd_knlistsize)
830 knote_fdclose(p, fd);
5b287bba
MD
831 rel_mplock();
832 }
dadab5e9 833 error = closef(fp, td);
984263bc 834 if (holdleaders) {
5b287bba 835 spin_lock_wr(&fdp->fd_spin);
984263bc
MD
836 fdp->fd_holdleaderscount--;
837 if (fdp->fd_holdleaderscount == 0 &&
838 fdp->fd_holdleaderswakeup != 0) {
839 fdp->fd_holdleaderswakeup = 0;
5b287bba 840 spin_unlock_wr(&fdp->fd_spin);
984263bc 841 wakeup(&fdp->fd_holdleaderscount);
5b287bba
MD
842 } else {
843 spin_unlock_wr(&fdp->fd_spin);
984263bc
MD
844 }
845 }
846 return (error);
847}
848
004d2de5
MD
849/*
850 * shutdown_args(int fd, int how)
851 */
852int
853kern_shutdown(int fd, int how)
854{
855 struct thread *td = curthread;
856 struct proc *p = td->td_proc;
004d2de5
MD
857 struct file *fp;
858 int error;
859
860 KKASSERT(p);
861
5b287bba 862 if ((fp = holdfp(p->p_fd, fd, -1)) == NULL)
004d2de5 863 return (EBADF);
87de5057 864 error = fo_shutdown(fp, how);
9f87144f 865 fdrop(fp);
004d2de5
MD
866
867 return (error);
868}
869
870int
753fd850 871sys_shutdown(struct shutdown_args *uap)
004d2de5
MD
872{
873 int error;
874
875 error = kern_shutdown(uap->s, uap->how);
876
877 return (error);
878}
879
984263bc 880int
8f6f8622 881kern_fstat(int fd, struct stat *ub)
984263bc 882{
dadab5e9
MD
883 struct thread *td = curthread;
884 struct proc *p = td->td_proc;
41c20dac 885 struct file *fp;
984263bc
MD
886 int error;
887
dadab5e9 888 KKASSERT(p);
8f6f8622 889
5b287bba 890 if ((fp = holdfp(p->p_fd, fd, -1)) == NULL)
984263bc 891 return (EBADF);
87de5057 892 error = fo_stat(fp, ub, p->p_ucred);
9f87144f 893 fdrop(fp);
8f6f8622 894
984263bc
MD
895 return (error);
896}
984263bc
MD
897
898/*
899 * Return status information about a file descriptor.
900 */
984263bc 901int
753fd850 902sys_fstat(struct fstat_args *uap)
984263bc 903{
8f6f8622 904 struct stat st;
984263bc
MD
905 int error;
906
8f6f8622
DRJ
907 error = kern_fstat(uap->fd, &st);
908
984263bc 909 if (error == 0)
8f6f8622 910 error = copyout(&st, uap->sb, sizeof(st));
984263bc
MD
911 return (error);
912}
913
984263bc
MD
914/*
915 * Return pathconf information about a file descriptor.
916 */
984263bc
MD
917/* ARGSUSED */
918int
753fd850 919sys_fpathconf(struct fpathconf_args *uap)
984263bc 920{
dadab5e9
MD
921 struct thread *td = curthread;
922 struct proc *p = td->td_proc;
984263bc
MD
923 struct file *fp;
924 struct vnode *vp;
925 int error = 0;
926
dadab5e9 927 KKASSERT(p);
984263bc 928
5b287bba
MD
929 if ((fp = holdfp(p->p_fd, uap->fd, -1)) == NULL)
930 return (EBADF);
984263bc
MD
931
932 switch (fp->f_type) {
933 case DTYPE_PIPE:
934 case DTYPE_SOCKET:
935 if (uap->name != _PC_PIPE_BUF) {
936 error = EINVAL;
937 } else {
c7114eea 938 uap->sysmsg_result = PIPE_BUF;
984263bc
MD
939 error = 0;
940 }
941 break;
942 case DTYPE_FIFO:
943 case DTYPE_VNODE:
944 vp = (struct vnode *)fp->f_data;
c7114eea 945 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
984263bc
MD
946 break;
947 default:
948 error = EOPNOTSUPP;
949 break;
950 }
9f87144f 951 fdrop(fp);
984263bc
MD
952 return(error);
953}
954
984263bc
MD
955static int fdexpand;
956SYSCTL_INT(_debug, OID_AUTO, fdexpand, CTLFLAG_RD, &fdexpand, 0, "");
957
5b287bba
MD
958/*
959 * Grow the file table so it can hold through descriptor (want).
960 *
961 * The fdp's spinlock must be held exclusively on entry and may be held
962 * exclusively on return. The spinlock may be cycled by the routine.
963 *
964 * MPSAFE
965 */
69908319 966static void
5b287bba 967fdgrow_locked(struct filedesc *fdp, int want)
69908319 968{
0679adc4
MD
969 struct fdnode *newfiles;
970 struct fdnode *oldfiles;
69908319
JH
971 int nf, extra;
972
973 nf = fdp->fd_nfiles;
974 do {
975 /* nf has to be of the form 2^n - 1 */
976 nf = 2 * nf + 1;
977 } while (nf <= want);
978
5b287bba 979 spin_unlock_wr(&fdp->fd_spin);
efda3bd0 980 newfiles = kmalloc(nf * sizeof(struct fdnode), M_FILEDESC, M_WAITOK);
5b287bba 981 spin_lock_wr(&fdp->fd_spin);
69908319
JH
982
983 /*
5b287bba
MD
984 * We could have raced another extend while we were not holding
985 * the spinlock.
69908319
JH
986 */
987 if (fdp->fd_nfiles >= nf) {
5b287bba 988 spin_unlock_wr(&fdp->fd_spin);
efda3bd0 989 kfree(newfiles, M_FILEDESC);
5b287bba 990 spin_lock_wr(&fdp->fd_spin);
69908319
JH
991 return;
992 }
69908319
JH
993 /*
994 * Copy the existing ofile and ofileflags arrays
995 * and zero the new portion of each array.
996 */
997 extra = nf - fdp->fd_nfiles;
0679adc4
MD
998 bcopy(fdp->fd_files, newfiles, fdp->fd_nfiles * sizeof(struct fdnode));
999 bzero(&newfiles[fdp->fd_nfiles], extra * sizeof(struct fdnode));
1000
1001 oldfiles = fdp->fd_files;
1002 fdp->fd_files = newfiles;
69908319 1003 fdp->fd_nfiles = nf;
0679adc4 1004
5b287bba
MD
1005 if (oldfiles != fdp->fd_builtin_files) {
1006 spin_unlock_wr(&fdp->fd_spin);
efda3bd0 1007 kfree(oldfiles, M_FILEDESC);
5b287bba
MD
1008 spin_lock_wr(&fdp->fd_spin);
1009 }
69908319
JH
1010 fdexpand++;
1011}
1012
1013/*
1014 * Number of nodes in right subtree, including the root.
1015 */
1016static __inline int
1017right_subtree_size(int n)
1018{
1019 return (n ^ (n | (n + 1)));
1020}
1021
1022/*
1023 * Bigger ancestor.
1024 */
1025static __inline int
1026right_ancestor(int n)
1027{
1028 return (n | (n + 1));
1029}
1030
1031/*
1032 * Smaller ancestor.
1033 */
1034static __inline int
1035left_ancestor(int n)
1036{
1037 return ((n & (n + 1)) - 1);
1038}
1039
5b287bba
MD
1040/*
1041 * Traverse the in-place binary tree buttom-up adjusting the allocation
1042 * count so scans can determine where free descriptors are located.
1043 *
1044 * MPSAFE - caller must be holding an exclusive spinlock on fdp
1045 */
fa541be6 1046static
69908319 1047void
5b287bba 1048fdreserve_locked(struct filedesc *fdp, int fd, int incr)
69908319
JH
1049{
1050 while (fd >= 0) {
0679adc4
MD
1051 fdp->fd_files[fd].allocated += incr;
1052 KKASSERT(fdp->fd_files[fd].allocated >= 0);
69908319
JH
1053 fd = left_ancestor(fd);
1054 }
1055}
1056
1057/*
259b8ea0
MD
1058 * Reserve a file descriptor for the process. If no error occurs, the
1059 * caller MUST at some point call fsetfd() or assign a file pointer
1060 * or dispose of the reservation.
5b287bba
MD
1061 *
1062 * MPSAFE
69908319 1063 */
984263bc 1064int
dda4b42b 1065fdalloc(struct proc *p, int want, int *result)
984263bc 1066{
41c20dac 1067 struct filedesc *fdp = p->p_fd;
69908319
JH
1068 int fd, rsize, rsum, node, lim;
1069
5b287bba 1070 spin_lock_rd(&p->p_limit->p_spin);
69908319 1071 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
5b287bba 1072 spin_unlock_rd(&p->p_limit->p_spin);
69908319
JH
1073 if (want >= lim)
1074 return (EMFILE);
5b287bba 1075 spin_lock_wr(&fdp->fd_spin);
69908319 1076 if (want >= fdp->fd_nfiles)
5b287bba 1077 fdgrow_locked(fdp, want);
984263bc
MD
1078
1079 /*
1080 * Search for a free descriptor starting at the higher
1081 * of want or fd_freefile. If that fails, consider
1082 * expanding the ofile array.
259b8ea0
MD
1083 *
1084 * NOTE! the 'allocated' field is a cumulative recursive allocation
1085 * count. If we happen to see a value of 0 then we can shortcut
1086 * our search. Otherwise we run through through the tree going
1087 * down branches we know have free descriptor(s) until we hit a
1088 * leaf node. The leaf node will be free but will not necessarily
1089 * have an allocated field of 0.
984263bc 1090 */
69908319
JH
1091retry:
1092 /* move up the tree looking for a subtree with a free node */
1093 for (fd = max(want, fdp->fd_freefile); fd < min(fdp->fd_nfiles, lim);
1094 fd = right_ancestor(fd)) {
0679adc4 1095 if (fdp->fd_files[fd].allocated == 0)
69908319 1096 goto found;
984263bc 1097
69908319 1098 rsize = right_subtree_size(fd);
0679adc4 1099 if (fdp->fd_files[fd].allocated == rsize)
69908319 1100 continue; /* right subtree full */
984263bc
MD
1101
1102 /*
69908319
JH
1103 * Free fd is in the right subtree of the tree rooted at fd.
1104 * Call that subtree R. Look for the smallest (leftmost)
1105 * subtree of R with an unallocated fd: continue moving
1106 * down the left branch until encountering a full left
1107 * subtree, then move to the right.
984263bc 1108 */
69908319
JH
1109 for (rsum = 0, rsize /= 2; rsize > 0; rsize /= 2) {
1110 node = fd + rsize;
0679adc4
MD
1111 rsum += fdp->fd_files[node].allocated;
1112 if (fdp->fd_files[fd].allocated == rsum + rsize) {
69908319 1113 fd = node; /* move to the right */
0679adc4 1114 if (fdp->fd_files[node].allocated == 0)
69908319
JH
1115 goto found;
1116 rsum = 0;
1117 }
984263bc 1118 }
69908319 1119 goto found;
984263bc 1120 }
69908319
JH
1121
1122 /*
1123 * No space in current array. Expand?
1124 */
5b287bba
MD
1125 if (fdp->fd_nfiles >= lim) {
1126 spin_unlock_wr(&fdp->fd_spin);
69908319 1127 return (EMFILE);
5b287bba
MD
1128 }
1129 fdgrow_locked(fdp, want);
69908319
JH
1130 goto retry;
1131
1132found:
1133 KKASSERT(fd < fdp->fd_nfiles);
69908319
JH
1134 if (fd > fdp->fd_lastfile)
1135 fdp->fd_lastfile = fd;
1136 if (want <= fdp->fd_freefile)
1137 fdp->fd_freefile = fd;
1138 *result = fd;
0679adc4 1139 KKASSERT(fdp->fd_files[fd].fp == NULL);
259b8ea0
MD
1140 KKASSERT(fdp->fd_files[fd].reserved == 0);
1141 fdp->fd_files[fd].fileflags = 0;
1142 fdp->fd_files[fd].reserved = 1;
5b287bba
MD
1143 fdreserve_locked(fdp, fd, 1);
1144 spin_unlock_wr(&fdp->fd_spin);
984263bc
MD
1145 return (0);
1146}
1147
1148/*
1149 * Check to see whether n user file descriptors
1150 * are available to the process p.
5b287bba
MD
1151 *
1152 * MPSAFE
984263bc
MD
1153 */
1154int
7bf8660a 1155fdavail(struct proc *p, int n)
984263bc 1156{
41c20dac 1157 struct filedesc *fdp = p->p_fd;
0679adc4 1158 struct fdnode *fdnode;
41c20dac 1159 int i, lim, last;
984263bc 1160
5b287bba 1161 spin_lock_rd(&p->p_limit->p_spin);
984263bc 1162 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
5b287bba 1163 spin_unlock_rd(&p->p_limit->p_spin);
984263bc 1164
5b287bba
MD
1165 spin_lock_rd(&fdp->fd_spin);
1166 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0) {
1167 spin_unlock_rd(&fdp->fd_spin);
1168 return (1);
1169 }
984263bc 1170 last = min(fdp->fd_nfiles, lim);
0679adc4
MD
1171 fdnode = &fdp->fd_files[fdp->fd_freefile];
1172 for (i = last - fdp->fd_freefile; --i >= 0; ++fdnode) {
5b287bba
MD
1173 if (fdnode->fp == NULL && --n <= 0) {
1174 spin_unlock_rd(&fdp->fd_spin);
984263bc 1175 return (1);
5b287bba 1176 }
984263bc 1177 }
5b287bba 1178 spin_unlock_rd(&fdp->fd_spin);
984263bc
MD
1179 return (0);
1180}
1181
1182/*
39f91578 1183 * falloc:
259b8ea0
MD
1184 * Create a new open file structure and reserve a file decriptor
1185 * for the process that refers to it.
1186 *
1187 * Root creds are checked using p, or assumed if p is NULL. If
1188 * resultfd is non-NULL then p must also be non-NULL. No file
1189 * descriptor is reserved if resultfd is NULL.
fad57d0e 1190 *
259b8ea0
MD
1191 * A file pointer with a refcount of 1 is returned. Note that the
1192 * file pointer is NOT associated with the descriptor. If falloc
1193 * returns success, fsetfd() MUST be called to either associate the
1194 * file pointer or clear the reservation.
5b287bba 1195 *
2dd63755 1196 * MPSAFE
984263bc
MD
1197 */
1198int
39f91578 1199falloc(struct proc *p, struct file **resultfp, int *resultfd)
984263bc 1200{
c4cb6d8b
HP
1201 static struct timeval lastfail;
1202 static int curfail;
fad57d0e
MD
1203 struct file *fp;
1204 int error;
c4cb6d8b 1205
fad57d0e
MD
1206 fp = NULL;
1207
1208 /*
1209 * Handle filetable full issues and root overfill.
1210 */
60ee93b9 1211 if (nfiles >= maxfiles - maxfilesrootres &&
9b3f1fd5 1212 ((p && p->p_ucred->cr_ruid != 0) || nfiles >= maxfiles)) {
c4cb6d8b
HP
1213 if (ppsratecheck(&lastfail, &curfail, 1)) {
1214 printf("kern.maxfiles limit exceeded by uid %d, please see tuning(7).\n",
9b3f1fd5 1215 (p ? p->p_ucred->cr_ruid : -1));
c4cb6d8b 1216 }
fad57d0e
MD
1217 error = ENFILE;
1218 goto done;
984263bc 1219 }
fad57d0e 1220
984263bc
MD
1221 /*
1222 * Allocate a new file descriptor.
984263bc 1223 */
efda3bd0 1224 fp = kmalloc(sizeof(struct file), M_FILE, M_WAITOK | M_ZERO);
85fceac1 1225 spin_init(&fp->f_spin);
984263bc 1226 fp->f_count = 1;
984263bc
MD
1227 fp->f_ops = &badfileops;
1228 fp->f_seqcount = 1;
fad57d0e 1229 if (p)
a235f7bb 1230 fp->f_cred = crhold(p->p_ucred);
fad57d0e
MD
1231 else
1232 fp->f_cred = crhold(proc0.p_ucred);
2dd63755
MD
1233 spin_lock_wr(&filehead_spin);
1234 nfiles++;
fad57d0e 1235 LIST_INSERT_HEAD(&filehead, fp, f_list);
2dd63755 1236 spin_unlock_wr(&filehead_spin);
fad57d0e 1237 if (resultfd) {
259b8ea0 1238 if ((error = fdalloc(p, 0, resultfd)) != 0) {
9f87144f 1239 fdrop(fp);
fad57d0e 1240 fp = NULL;
39f91578 1241 }
a235f7bb 1242 } else {
fad57d0e 1243 error = 0;
984263bc 1244 }
fad57d0e
MD
1245done:
1246 *resultfp = fp;
64f33bc8 1247 return (error);
fad57d0e
MD
1248}
1249
85fceac1
MD
1250/*
1251 * MPSAFE
1252 */
1253static
1254int
1255checkfpclosed(struct filedesc *fdp, int fd, struct file *fp)
1256{
1257 int error;
1258
1259 spin_lock_rd(&fdp->fd_spin);
1260 if ((unsigned) fd >= fdp->fd_nfiles || fp != fdp->fd_files[fd].fp)
1261 error = EBADF;
1262 else
1263 error = 0;
1264 spin_unlock_rd(&fdp->fd_spin);
1265 return (error);
1266}
1267
fad57d0e 1268/*
259b8ea0
MD
1269 * Associate a file pointer with a previously reserved file descriptor.
1270 * This function always succeeds.
1271 *
1272 * If fp is NULL, the file descriptor is returned to the pool.
fad57d0e 1273 */
fad57d0e 1274
5b287bba
MD
1275/*
1276 * MPSAFE (exclusive spinlock must be held on call)
1277 */
1278static void
1279fsetfd_locked(struct filedesc *fdp, struct file *fp, int fd)
1280{
259b8ea0
MD
1281 KKASSERT((unsigned)fd < fdp->fd_nfiles);
1282 KKASSERT(fdp->fd_files[fd].reserved != 0);
1283 if (fp) {
fad57d0e 1284 fhold(fp);
259b8ea0
MD
1285 fdp->fd_files[fd].fp = fp;
1286 fdp->fd_files[fd].reserved = 0;
d9b2033e
MD
1287 if (fp->f_type == DTYPE_KQUEUE) {
1288 if (fdp->fd_knlistsize < 0)
1289 fdp->fd_knlistsize = 0;
1290 }
259b8ea0
MD
1291 } else {
1292 fdp->fd_files[fd].reserved = 0;
5b287bba
MD
1293 fdreserve_locked(fdp, fd, -1);
1294 fdfixup_locked(fdp, fd);
fad57d0e 1295 }
984263bc
MD
1296}
1297
5b287bba
MD
1298/*
1299 * MPSAFE
1300 */
1301void
1302fsetfd(struct proc *p, struct file *fp, int fd)
1303{
1304 struct filedesc *fdp = p->p_fd;
1305
1306 spin_lock_wr(&fdp->fd_spin);
1307 fsetfd_locked(fdp, fp, fd);
1308 spin_unlock_wr(&fdp->fd_spin);
1309}
1310
1311/*
1312 * MPSAFE (exclusive spinlock must be held on call)
1313 */
fa541be6 1314static
259b8ea0 1315struct file *
5b287bba 1316funsetfd_locked(struct filedesc *fdp, int fd)
69908319 1317{
259b8ea0
MD
1318 struct file *fp;
1319
1320 if ((unsigned)fd >= fdp->fd_nfiles)
1321 return (NULL);
1322 if ((fp = fdp->fd_files[fd].fp) == NULL)
1323 return (NULL);
0679adc4
MD
1324 fdp->fd_files[fd].fp = NULL;
1325 fdp->fd_files[fd].fileflags = 0;
259b8ea0 1326
5b287bba
MD
1327 fdreserve_locked(fdp, fd, -1);
1328 fdfixup_locked(fdp, fd);
259b8ea0 1329 return(fp);
69908319
JH
1330}
1331
85fceac1
MD
1332/*
1333 * MPSAFE
1334 */
1335int
1336fgetfdflags(struct filedesc *fdp, int fd, int *flagsp)
1337{
1338 int error;
1339
1340 spin_lock_rd(&fdp->fd_spin);
1341 if (((u_int)fd) >= fdp->fd_nfiles) {
1342 error = EBADF;
1343 } else if (fdp->fd_files[fd].fp == NULL) {
1344 error = EBADF;
1345 } else {
1346 *flagsp = fdp->fd_files[fd].fileflags;
1347 error = 0;
1348 }
1349 spin_unlock_rd(&fdp->fd_spin);
1350 return (error);
1351}
1352
1353/*
1354 * MPSAFE
1355 */
228b401d
MD
1356int
1357fsetfdflags(struct filedesc *fdp, int fd, int add_flags)
1358{
85fceac1
MD
1359 int error;
1360
1361 spin_lock_wr(&fdp->fd_spin);
1362 if (((u_int)fd) >= fdp->fd_nfiles) {
1363 error = EBADF;
1364 } else if (fdp->fd_files[fd].fp == NULL) {
1365 error = EBADF;
1366 } else {
1367 fdp->fd_files[fd].fileflags |= add_flags;
1368 error = 0;
1369 }
1370 spin_unlock_wr(&fdp->fd_spin);
1371 return (error);
228b401d
MD
1372}
1373
85fceac1
MD
1374/*
1375 * MPSAFE
1376 */
228b401d
MD
1377int
1378fclrfdflags(struct filedesc *fdp, int fd, int rem_flags)
1379{
85fceac1
MD
1380 int error;
1381
1382 spin_lock_wr(&fdp->fd_spin);
1383 if (((u_int)fd) >= fdp->fd_nfiles) {
1384 error = EBADF;
1385 } else if (fdp->fd_files[fd].fp == NULL) {
1386 error = EBADF;
1387 } else {
1388 fdp->fd_files[fd].fileflags &= ~rem_flags;
1389 error = 0;
1390 }
1391 spin_unlock_wr(&fdp->fd_spin);
1392 return (error);
228b401d
MD
1393}
1394
a235f7bb
MD
1395void
1396fsetcred(struct file *fp, struct ucred *cr)
1397{
1398 crhold(cr);
1399 crfree(fp->f_cred);
1400 fp->f_cred = cr;
1401}
1402
984263bc
MD
1403/*
1404 * Free a file descriptor.
1405 */
5b287bba 1406static
984263bc 1407void
7bf8660a 1408ffree(struct file *fp)
984263bc
MD
1409{
1410 KASSERT((fp->f_count == 0), ("ffree: fp_fcount not 0!"));
2dd63755 1411 spin_lock_wr(&filehead_spin);
984263bc 1412 LIST_REMOVE(fp, f_list);
2dd63755
MD
1413 nfiles--;
1414 spin_unlock_wr(&filehead_spin);
984263bc 1415 crfree(fp->f_cred);
21739618
MD
1416 if (fp->f_ncp) {
1417 cache_drop(fp->f_ncp);
1418 fp->f_ncp = NULL;
1419 }
efda3bd0 1420 kfree(fp, M_FILE);
984263bc
MD
1421}
1422
228b401d
MD
1423/*
1424 * called from init_main, initialize filedesc0 for proc0.
1425 */
1426void
1427fdinit_bootstrap(struct proc *p0, struct filedesc *fdp0, int cmask)
1428{
1429 p0->p_fd = fdp0;
1430 p0->p_fdtol = NULL;
1431 fdp0->fd_refcnt = 1;
1432 fdp0->fd_cmask = cmask;
1433 fdp0->fd_files = fdp0->fd_builtin_files;
1434 fdp0->fd_nfiles = NDFILE;
259b8ea0 1435 fdp0->fd_lastfile = -1;
85fceac1 1436 spin_init(&fdp0->fd_spin);
228b401d
MD
1437}
1438
984263bc
MD
1439/*
1440 * Build a new filedesc structure.
5b287bba
MD
1441 *
1442 * NOT MPSAFE (vref)
984263bc
MD
1443 */
1444struct filedesc *
7bf8660a 1445fdinit(struct proc *p)
984263bc 1446{
0679adc4 1447 struct filedesc *newfdp;
41c20dac 1448 struct filedesc *fdp = p->p_fd;
984263bc 1449
efda3bd0 1450 newfdp = kmalloc(sizeof(struct filedesc), M_FILEDESC, M_WAITOK|M_ZERO);
5b287bba 1451 spin_lock_rd(&fdp->fd_spin);
bccde7a3 1452 if (fdp->fd_cdir) {
0679adc4
MD
1453 newfdp->fd_cdir = fdp->fd_cdir;
1454 vref(newfdp->fd_cdir);
1455 newfdp->fd_ncdir = cache_hold(fdp->fd_ncdir);
690a3127 1456 }
bccde7a3
MD
1457
1458 /*
1459 * rdir may not be set in e.g. proc0 or anything vm_fork'd off of
1460 * proc0, but should unconditionally exist in other processes.
1461 */
1462 if (fdp->fd_rdir) {
0679adc4
MD
1463 newfdp->fd_rdir = fdp->fd_rdir;
1464 vref(newfdp->fd_rdir);
1465 newfdp->fd_nrdir = cache_hold(fdp->fd_nrdir);
bccde7a3
MD
1466 }
1467 if (fdp->fd_jdir) {
0679adc4
MD
1468 newfdp->fd_jdir = fdp->fd_jdir;
1469 vref(newfdp->fd_jdir);
1470 newfdp->fd_njdir = cache_hold(fdp->fd_njdir);
690a3127 1471 }
5b287bba 1472 spin_unlock_rd(&fdp->fd_spin);
690a3127 1473
984263bc 1474 /* Create the file descriptor table. */
0679adc4
MD
1475 newfdp->fd_refcnt = 1;
1476 newfdp->fd_cmask = cmask;
1477 newfdp->fd_files = newfdp->fd_builtin_files;
1478 newfdp->fd_nfiles = NDFILE;
1479 newfdp->fd_knlistsize = -1;
259b8ea0 1480 newfdp->fd_lastfile = -1;
85fceac1 1481 spin_init(&newfdp->fd_spin);
0679adc4
MD
1482
1483 return (newfdp);
984263bc
MD
1484}
1485
1486/*
1487 * Share a filedesc structure.
5b287bba
MD
1488 *
1489 * MPSAFE
984263bc
MD
1490 */
1491struct filedesc *
7bf8660a 1492fdshare(struct proc *p)
984263bc 1493{
5b287bba
MD
1494 struct filedesc *fdp;
1495
1496 fdp = p->p_fd;
1497 spin_lock_wr(&fdp->fd_spin);
1498 fdp->fd_refcnt++;
1499 spin_unlock_wr(&fdp->fd_spin);
1500 return (fdp);
984263bc
MD
1501}
1502
1503/*
1504 * Copy a filedesc structure.
5b287bba
MD
1505 *
1506 * MPSAFE
984263bc
MD
1507 */
1508struct filedesc *
7bf8660a 1509fdcopy(struct proc *p)
984263bc 1510{
5b287bba
MD
1511 struct filedesc *fdp = p->p_fd;
1512 struct filedesc *newfdp;
0679adc4 1513 struct fdnode *fdnode;
41c20dac 1514 int i;
5b287bba 1515 int ni;
984263bc 1516
5b287bba
MD
1517 /*
1518 * Certain daemons might not have file descriptors.
1519 */
984263bc
MD
1520 if (fdp == NULL)
1521 return (NULL);
1522
5b287bba
MD
1523 /*
1524 * Allocate the new filedesc and fd_files[] array. This can race
1525 * with operations by other threads on the fdp so we have to be
1526 * careful.
1527 */
efda3bd0 1528 newfdp = kmalloc(sizeof(struct filedesc), M_FILEDESC, M_WAITOK | M_ZERO);
5b287bba
MD
1529again:
1530 spin_lock_rd(&fdp->fd_spin);
1531 if (fdp->fd_lastfile < NDFILE) {
1532 newfdp->fd_files = newfdp->fd_builtin_files;
1533 i = NDFILE;
1534 } else {
1535 /*
1536 * We have to allocate (N^2-1) entries for our in-place
1537 * binary tree. Allow the table to shrink.
1538 */
1539 i = fdp->fd_nfiles;
1540 ni = (i - 1) / 2;
1541 while (ni > fdp->fd_lastfile && ni > NDFILE) {
1542 i = ni;
1543 ni = (i - 1) / 2;
1544 }
1545 spin_unlock_rd(&fdp->fd_spin);
77652cad 1546 newfdp->fd_files = kmalloc(i * sizeof(struct fdnode),
5b287bba
MD
1547 M_FILEDESC, M_WAITOK | M_ZERO);
1548
1549 /*
1550 * Check for race, retry
1551 */
1552 spin_lock_rd(&fdp->fd_spin);
1553 if (i <= fdp->fd_lastfile) {
1554 spin_unlock_rd(&fdp->fd_spin);
efda3bd0 1555 kfree(newfdp->fd_files, M_FILEDESC);
5b287bba
MD
1556 goto again;
1557 }
1558 }
1559
1560 /*
1561 * Dup the remaining fields. vref() and cache_hold() can be
1562 * safely called while holding the read spinlock on fdp.
1563 *
1564 * The read spinlock on fdp is still being held.
1565 *
1566 * NOTE: vref and cache_hold calls for the case where the vnode
1567 * or cache entry already has at least one ref may be called
1568 * while holding spin locks.
1569 */
1570 if ((newfdp->fd_cdir = fdp->fd_cdir) != NULL) {
597aea93 1571 vref(newfdp->fd_cdir);
5b287bba 1572 newfdp->fd_ncdir = cache_hold(fdp->fd_ncdir);
690a3127 1573 }
6bdbb368
DR
1574 /*
1575 * We must check for fd_rdir here, at least for now because
1576 * the init process is created before we have access to the
1577 * rootvode to take a reference to it.
1578 */
5b287bba 1579 if ((newfdp->fd_rdir = fdp->fd_rdir) != NULL) {
597aea93 1580 vref(newfdp->fd_rdir);
5b287bba 1581 newfdp->fd_nrdir = cache_hold(fdp->fd_nrdir);
690a3127 1582 }
5b287bba 1583 if ((newfdp->fd_jdir = fdp->fd_jdir) != NULL) {
597aea93 1584 vref(newfdp->fd_jdir);
5b287bba 1585 newfdp->fd_njdir = cache_hold(fdp->fd_njdir);
690a3127 1586 }
984263bc 1587 newfdp->fd_refcnt = 1;
984263bc 1588 newfdp->fd_nfiles = i;
5b287bba
MD
1589 newfdp->fd_lastfile = fdp->fd_lastfile;
1590 newfdp->fd_freefile = fdp->fd_freefile;
1591 newfdp->fd_cmask = fdp->fd_cmask;
1592 newfdp->fd_knlist = NULL;
1593 newfdp->fd_knlistsize = -1;
1594 newfdp->fd_knhash = NULL;
1595 newfdp->fd_knhashmask = 0;
1596 spin_init(&newfdp->fd_spin);
984263bc
MD
1597
1598 /*
5b287bba
MD
1599 * Copy the descriptor table through (i). This also copies the
1600 * allocation state. Then go through and ref the file pointers
1601 * and clean up any KQ descriptors.
1602 *
259b8ea0
MD
1603 * kq descriptors cannot be copied. Since we haven't ref'd the
1604 * copied files yet we can ignore the return value from funsetfd().
5b287bba
MD
1605 *
1606 * The read spinlock on fdp is still being held.
984263bc 1607 */
5b287bba
MD
1608 bcopy(fdp->fd_files, newfdp->fd_files, i * sizeof(struct fdnode));
1609 for (i = 0 ; i < newfdp->fd_nfiles; ++i) {
259b8ea0
MD
1610 fdnode = &newfdp->fd_files[i];
1611 if (fdnode->reserved) {
5b287bba 1612 fdreserve_locked(newfdp, i, -1);
259b8ea0 1613 fdnode->reserved = 0;
5b287bba
MD
1614 fdfixup_locked(newfdp, i);
1615 } else if (fdnode->fp) {
1616 if (fdnode->fp->f_type == DTYPE_KQUEUE) {
1617 (void)funsetfd_locked(newfdp, i);
1618 } else {
1619 fhold(fdnode->fp);
1620 }
259b8ea0 1621 }
984263bc 1622 }
5b287bba 1623 spin_unlock_rd(&fdp->fd_spin);
984263bc
MD
1624 return (newfdp);
1625}
1626
1627/*
1628 * Release a filedesc structure.
5b287bba
MD
1629 *
1630 * NOT MPSAFE (MPSAFE for refs > 1, but the final cleanup code is not MPSAFE)
984263bc
MD
1631 */
1632void
41c20dac 1633fdfree(struct proc *p)
984263bc 1634{
dadab5e9 1635 struct thread *td = p->p_thread;
41c20dac 1636 struct filedesc *fdp = p->p_fd;
0679adc4 1637 struct fdnode *fdnode;
41c20dac 1638 int i;
984263bc
MD
1639 struct filedesc_to_leader *fdtol;
1640 struct file *fp;
1641 struct vnode *vp;
1642 struct flock lf;
1643
1644 /* Certain daemons might not have file descriptors. */
1645 if (fdp == NULL)
1646 return;
1647
5b287bba
MD
1648 /*
1649 * Severe messing around to follow
1650 */
1651 spin_lock_wr(&fdp->fd_spin);
1652
984263bc
MD
1653 /* Check for special need to clear POSIX style locks */
1654 fdtol = p->p_fdtol;
1655 if (fdtol != NULL) {
1656 KASSERT(fdtol->fdl_refcount > 0,
1657 ("filedesc_to_refcount botch: fdl_refcount=%d",
1658 fdtol->fdl_refcount));
1659 if (fdtol->fdl_refcount == 1 &&
1660 (p->p_leader->p_flag & P_ADVLOCK) != 0) {
259b8ea0
MD
1661 for (i = 0; i <= fdp->fd_lastfile; ++i) {
1662 fdnode = &fdp->fd_files[i];
0679adc4 1663 if (fdnode->fp == NULL ||
259b8ea0 1664 fdnode->fp->f_type != DTYPE_VNODE) {
984263bc 1665 continue;
259b8ea0 1666 }
0679adc4 1667 fp = fdnode->fp;
984263bc 1668 fhold(fp);
5b287bba
MD
1669 spin_unlock_wr(&fdp->fd_spin);
1670
984263bc
MD
1671 lf.l_whence = SEEK_SET;
1672 lf.l_start = 0;
1673 lf.l_len = 0;
1674 lf.l_type = F_UNLCK;
1675 vp = (struct vnode *)fp->f_data;
1676 (void) VOP_ADVLOCK(vp,
1677 (caddr_t)p->p_leader,
1678 F_UNLCK,
1679 &lf,
1680 F_POSIX);
9f87144f 1681 fdrop(fp);
5b287bba 1682 spin_lock_wr(&fdp->fd_spin);
984263bc
MD
1683 }
1684 }
1685 retry:
1686 if (fdtol->fdl_refcount == 1) {
1687 if (fdp->fd_holdleaderscount > 0 &&
1688 (p->p_leader->p_flag & P_ADVLOCK) != 0) {
1689 /*
1690 * close() or do_dup() has cleared a reference
1691 * in a shared file descriptor table.
1692 */
1693 fdp->fd_holdleaderswakeup = 1;
7749886d
MD
1694 msleep(&fdp->fd_holdleaderscount,
1695 &fdp->fd_spin, 0, "fdlhold", 0);
984263bc
MD
1696 goto retry;
1697 }
1698 if (fdtol->fdl_holdcount > 0) {
1699 /*
1700 * Ensure that fdtol->fdl_leader
1701 * remains valid in closef().
1702 */
1703 fdtol->fdl_wakeup = 1;
7749886d 1704 msleep(fdtol, &fdp->fd_spin, 0, "fdlhold", 0);
984263bc
MD
1705 goto retry;
1706 }
1707 }
1708 fdtol->fdl_refcount--;
1709 if (fdtol->fdl_refcount == 0 &&
1710 fdtol->fdl_holdcount == 0) {
1711 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
1712 fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
5b287bba 1713 } else {
984263bc 1714 fdtol = NULL;
5b287bba 1715 }
984263bc 1716 p->p_fdtol = NULL;
5b287bba
MD
1717 if (fdtol != NULL) {
1718 spin_unlock_wr(&fdp->fd_spin);
efda3bd0 1719 kfree(fdtol, M_FILEDESC_TO_LEADER);
5b287bba
MD
1720 spin_lock_wr(&fdp->fd_spin);
1721 }
984263bc 1722 }
5b287bba
MD
1723 if (--fdp->fd_refcnt > 0) {
1724 spin_unlock_wr(&fdp->fd_spin);
984263bc 1725 return;
5b287bba
MD
1726 }
1727 spin_unlock_wr(&fdp->fd_spin);
1728
984263bc
MD
1729 /*
1730 * we are the last reference to the structure, we can
1731 * safely assume it will not change out from under us.
1732 */
0679adc4
MD
1733 for (i = 0; i <= fdp->fd_lastfile; ++i) {
1734 if (fdp->fd_files[i].fp)
1735 closef(fdp->fd_files[i].fp, td);
984263bc 1736 }
0679adc4 1737 if (fdp->fd_files != fdp->fd_builtin_files)
efda3bd0 1738 kfree(fdp->fd_files, M_FILEDESC);
690a3127
MD
1739 if (fdp->fd_cdir) {
1740 cache_drop(fdp->fd_ncdir);
984263bc 1741 vrele(fdp->fd_cdir);
690a3127 1742 }
bccde7a3
MD
1743 if (fdp->fd_rdir) {
1744 cache_drop(fdp->fd_nrdir);
1745 vrele(fdp->fd_rdir);
1746 }
690a3127
MD
1747 if (fdp->fd_jdir) {
1748 cache_drop(fdp->fd_njdir);
984263bc 1749 vrele(fdp->fd_jdir);
690a3127 1750 }
984263bc 1751 if (fdp->fd_knlist)
efda3bd0 1752 kfree(fdp->fd_knlist, M_KQUEUE);
984263bc 1753 if (fdp->fd_knhash)
efda3bd0
MD
1754 kfree(fdp->fd_knhash, M_KQUEUE);
1755 kfree(fdp, M_FILEDESC);
984263bc
MD
1756}
1757
fa541be6
MD
1758/*
1759 * Retrieve and reference the file pointer associated with a descriptor.
85fceac1
MD
1760 *
1761 * MPSAFE
fa541be6
MD
1762 */
1763struct file *
1764holdfp(struct filedesc *fdp, int fd, int flag)
1765{
1766 struct file* fp;
1767
85fceac1
MD
1768 spin_lock_rd(&fdp->fd_spin);
1769 if (((u_int)fd) >= fdp->fd_nfiles) {
1770 fp = NULL;
1771 goto done;
1772 }
fa541be6 1773 if ((fp = fdp->fd_files[fd].fp) == NULL)
85fceac1
MD
1774 goto done;
1775 if ((fp->f_flag & flag) == 0 && flag != -1) {
1776 fp = NULL;
1777 goto done;
1778 }
fa541be6 1779 fhold(fp);
85fceac1
MD
1780done:
1781 spin_unlock_rd(&fdp->fd_spin);
fa541be6
MD
1782 return (fp);
1783}
1784
1785/*
1786 * holdsock() - load the struct file pointer associated
1787 * with a socket into *fpp. If an error occurs, non-zero
1788 * will be returned and *fpp will be set to NULL.
5b287bba
MD
1789 *
1790 * MPSAFE
fa541be6
MD
1791 */
1792int
5b287bba 1793holdsock(struct filedesc *fdp, int fd, struct file **fpp)
fa541be6
MD
1794{
1795 struct file *fp;
5b287bba 1796 int error;
fa541be6 1797
5b287bba
MD
1798 spin_lock_rd(&fdp->fd_spin);
1799 if ((unsigned)fd >= fdp->fd_nfiles) {
1800 error = EBADF;
1801 fp = NULL;
1802 goto done;
1803 }
1804 if ((fp = fdp->fd_files[fd].fp) == NULL) {
1805 error = EBADF;
1806 goto done;
1807 }
1808 if (fp->f_type != DTYPE_SOCKET) {
1809 error = ENOTSOCK;
1810 goto done;
1811 }
fa541be6 1812 fhold(fp);
5b287bba
MD
1813 error = 0;
1814done:
1815 spin_unlock_rd(&fdp->fd_spin);
fa541be6
MD
1816 *fpp = fp;
1817 return (error);
1818}
1819
228b401d 1820/*
5b287bba
MD
1821 * Convert a user file descriptor to a held file pointer.
1822 *
1823 * MPSAFE
228b401d
MD
1824 */
1825int
5b287bba 1826holdvnode(struct filedesc *fdp, int fd, struct file **fpp)
228b401d
MD
1827{
1828 struct file *fp;
5b287bba
MD
1829 int error;
1830
1831 spin_lock_rd(&fdp->fd_spin);
1832 if ((unsigned)fd >= fdp->fd_nfiles) {
1833 error = EBADF;
1834 fp = NULL;
1835 goto done;
1836 }
1837 if ((fp = fdp->fd_files[fd].fp) == NULL) {
1838 error = EBADF;
1839 goto done;
1840 }
1841 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO) {
1842 error = EINVAL;
1843 goto done;
1844 }
1845 fhold(fp);
1846 error = 0;
1847done:
1848 spin_unlock_rd(&fdp->fd_spin);
228b401d 1849 *fpp = fp;
5b287bba 1850 return (error);
228b401d
MD
1851}
1852
984263bc
MD
1853/*
1854 * For setugid programs, we don't want to people to use that setugidness
1855 * to generate error messages which write to a file which otherwise would
1856 * otherwise be off-limits to the process.
1857 *
1858 * This is a gross hack to plug the hole. A better solution would involve
1859 * a special vop or other form of generalized access control mechanism. We
1860 * go ahead and just reject all procfs file systems accesses as dangerous.
1861 *
1862 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
1863 * sufficient. We also don't for check setugidness since we know we are.
1864 */
1865static int
1866is_unsafe(struct file *fp)
1867{
1868 if (fp->f_type == DTYPE_VNODE &&
1869 ((struct vnode *)(fp->f_data))->v_tag == VT_PROCFS)
1870 return (1);
1871 return (0);
1872}
1873
1874/*
1875 * Make this setguid thing safe, if at all possible.
5b287bba
MD
1876 *
1877 * NOT MPSAFE - scans fdp without spinlocks, calls knote_fdclose()
984263bc
MD
1878 */
1879void
dadab5e9 1880setugidsafety(struct proc *p)
984263bc 1881{
dadab5e9 1882 struct thread *td = p->p_thread;
984263bc 1883 struct filedesc *fdp = p->p_fd;
41c20dac 1884 int i;
984263bc
MD
1885
1886 /* Certain daemons might not have file descriptors. */
1887 if (fdp == NULL)
1888 return;
1889
1890 /*
0679adc4 1891 * note: fdp->fd_files may be reallocated out from under us while
984263bc
MD
1892 * we are blocked in a close. Be careful!
1893 */
1894 for (i = 0; i <= fdp->fd_lastfile; i++) {
1895 if (i > 2)
1896 break;
0679adc4 1897 if (fdp->fd_files[i].fp && is_unsafe(fdp->fd_files[i].fp)) {
984263bc
MD
1898 struct file *fp;
1899
984263bc
MD
1900 if (i < fdp->fd_knlistsize)
1901 knote_fdclose(p, i);
1902 /*
1903 * NULL-out descriptor prior to close to avoid
1904 * a race while close blocks.
1905 */
5b287bba 1906 if ((fp = funsetfd_locked(fdp, i)) != NULL)
259b8ea0 1907 closef(fp, td);
984263bc
MD
1908 }
1909 }
984263bc
MD
1910}
1911
1912/*
1913 * Close any files on exec?
5b287bba
MD
1914 *
1915 * NOT MPSAFE - scans fdp without spinlocks, calls knote_fdclose()
984263bc
MD
1916 */
1917void
dadab5e9 1918fdcloseexec(struct proc *p)
984263bc 1919{
dadab5e9 1920 struct thread *td = p->p_thread;
984263bc 1921 struct filedesc *fdp = p->p_fd;
41c20dac 1922 int i;
984263bc
MD
1923
1924 /* Certain daemons might not have file descriptors. */
1925 if (fdp == NULL)
1926 return;
1927
1928 /*
0679adc4
MD
1929 * We cannot cache fd_files since operations may block and rip
1930 * them out from under us.
984263bc
MD
1931 */
1932 for (i = 0; i <= fdp->fd_lastfile; i++) {
0679adc4
MD
1933 if (fdp->fd_files[i].fp != NULL &&
1934 (fdp->fd_files[i].fileflags & UF_EXCLOSE)) {
984263bc
MD
1935 struct file *fp;
1936
984263bc
MD
1937 if (i < fdp->fd_knlistsize)
1938 knote_fdclose(p, i);
1939 /*
1940 * NULL-out descriptor prior to close to avoid
1941 * a race while close blocks.
1942 */
5b287bba 1943 if ((fp = funsetfd_locked(fdp, i)) != NULL)
259b8ea0 1944 closef(fp, td);
984263bc
MD
1945 }
1946 }
984263bc
MD
1947}
1948
1949/*
1950 * It is unsafe for set[ug]id processes to be started with file
1951 * descriptors 0..2 closed, as these descriptors are given implicit
1952 * significance in the Standard C library. fdcheckstd() will create a
1953 * descriptor referencing /dev/null for each of stdin, stdout, and
1954 * stderr that is not already open.
5b287bba
MD
1955 *
1956 * NOT MPSAFE - calls falloc, vn_open, etc
984263bc
MD
1957 */
1958int
dadab5e9 1959fdcheckstd(struct proc *p)
984263bc 1960{
fad57d0e 1961 struct nlookupdata nd;
dadab5e9
MD
1962 struct filedesc *fdp;
1963 struct file *fp;
1964 register_t retval;
259b8ea0 1965 int i, error, flags, devnull;
984263bc
MD
1966
1967 fdp = p->p_fd;
1968 if (fdp == NULL)
1969 return (0);
1970 devnull = -1;
1971 error = 0;
1972 for (i = 0; i < 3; i++) {
0679adc4 1973 if (fdp->fd_files[i].fp != NULL)
fad57d0e
MD
1974 continue;
1975 if (devnull < 0) {
259b8ea0 1976 if ((error = falloc(p, &fp, &devnull)) != 0)
fad57d0e
MD
1977 break;
1978
1979 error = nlookup_init(&nd, "/dev/null", UIO_SYSSPACE,
1980 NLC_FOLLOW|NLC_LOCKVP);
1981 flags = FREAD | FWRITE;
1982 if (error == 0)
1983 error = vn_open(&nd, fp, flags, 0);
1984 if (error == 0)
259b8ea0
MD
1985 fsetfd(p, fp, devnull);
1986 else
1987 fsetfd(p, NULL, devnull);
9f87144f 1988 fdrop(fp);
fad57d0e
MD
1989 nlookup_done(&nd);
1990 if (error)
1991 break;
259b8ea0 1992 KKASSERT(i == devnull);
fad57d0e
MD
1993 } else {
1994 error = kern_dup(DUP_FIXED, devnull, i, &retval);
1995 if (error != 0)
1996 break;
1997 }
984263bc
MD
1998 }
1999 return (error);
2000}
2001
2002/*
2003 * Internal form of close.
2004 * Decrement reference count on file structure.
dadab5e9 2005 * Note: td and/or p may be NULL when closing a file
984263bc 2006 * that was being passed in a message.
5b287bba
MD
2007 *
2008 * MPALMOSTSAFE - acquires mplock for VOP operations
984263bc
MD
2009 */
2010int
dadab5e9 2011closef(struct file *fp, struct thread *td)
984263bc
MD
2012{
2013 struct vnode *vp;
2014 struct flock lf;
2015 struct filedesc_to_leader *fdtol;
dadab5e9 2016 struct proc *p;
984263bc
MD
2017
2018 if (fp == NULL)
2019 return (0);
dadab5e9
MD
2020 if (td == NULL) {
2021 td = curthread;
2022 p = NULL; /* allow no proc association */
2023 } else {
2024 p = td->td_proc; /* can also be NULL */
2025 }
984263bc
MD
2026 /*
2027 * POSIX record locking dictates that any close releases ALL
2028 * locks owned by this process. This is handled by setting
2029 * a flag in the unlock to free ONLY locks obeying POSIX
2030 * semantics, and not to free BSD-style file locks.
2031 * If the descriptor was in a message, POSIX-style locks
2032 * aren't passed with the descriptor.
2033 */
7749886d
MD
2034 if (p != NULL && fp->f_type == DTYPE_VNODE &&
2035 (((struct vnode *)fp->f_data)->v_flag & VMAYHAVELOCKS)
2036 ) {
2037 get_mplock();
984263bc
MD
2038 if ((p->p_leader->p_flag & P_ADVLOCK) != 0) {
2039 lf.l_whence = SEEK_SET;
2040 lf.l_start = 0;
2041 lf.l_len = 0;
2042 lf.l_type = F_UNLCK;
2043 vp = (struct vnode *)fp->f_data;
2044 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
2045 &lf, F_POSIX);
2046 }
2047 fdtol = p->p_fdtol;
2048 if (fdtol != NULL) {
2049 /*
2050 * Handle special case where file descriptor table
2051 * is shared between multiple process leaders.
2052 */
2053 for (fdtol = fdtol->fdl_next;
2054 fdtol != p->p_fdtol;
2055 fdtol = fdtol->fdl_next) {
2056 if ((fdtol->fdl_leader->p_flag &
2057 P_ADVLOCK) == 0)
2058 continue;
2059 fdtol->fdl_holdcount++;
2060 lf.l_whence = SEEK_SET;
2061 lf.l_start = 0;
2062 lf.l_len = 0;
2063 lf.l_type = F_UNLCK;
2064 vp = (struct vnode *)fp->f_data;
2065 (void) VOP_ADVLOCK(vp,
661fc1a0 2066 (caddr_t)fdtol->fdl_leader,
984263bc
MD
2067 F_UNLCK, &lf, F_POSIX);
2068 fdtol->fdl_holdcount--;
2069 if (fdtol->fdl_holdcount == 0 &&
2070 fdtol->fdl_wakeup != 0) {
2071 fdtol->fdl_wakeup = 0;
2072 wakeup(fdtol);
2073 }
2074 }
2075 }
7749886d 2076 rel_mplock();
984263bc 2077 }
9f87144f 2078 return (fdrop(fp));
984263bc
MD
2079}
2080
85fceac1
MD
2081/*
2082 * MPSAFE
2083 *
2084 * fhold() can only be called if f_count is already at least 1 (i.e. the
2085 * caller of fhold() already has a reference to the file pointer in some
5b287bba
MD
2086 * manner or other).
2087 *
2088 * This is a rare case where callers are allowed to hold spinlocks, so
2089 * we can't ourselves. Since we are not obtaining the fp spinlock,
2090 * we have to use an atomic lock to interlock against fdrop().
85fceac1
MD
2091 */
2092void
2093fhold(struct file *fp)
2094{
2095 atomic_add_int(&fp->f_count, 1);
2096}
2097
2098/*
5b287bba
MD
2099 * A spinlock is required to handle 1->0 transitions on f_count. We have
2100 * to use atomic_sub_int so as not to race the atomic_add_int in fhold().
85fceac1 2101 *
5b287bba 2102 * MPALMOSTSAFE - acquires mplock for final close sequence
85fceac1 2103 */
984263bc 2104int
9f87144f 2105fdrop(struct file *fp)
984263bc
MD
2106{
2107 struct flock lf;
2108 struct vnode *vp;
2109 int error;
2110
85fceac1 2111 spin_lock_wr(&fp->f_spin);
5b287bba
MD
2112 atomic_subtract_int(&fp->f_count, 1);
2113 if (fp->f_count > 0) {
85fceac1 2114 spin_unlock_wr(&fp->f_spin);
984263bc 2115 return (0);
85fceac1
MD
2116 }
2117 spin_unlock_wr(&fp->f_spin);
2118
5b287bba
MD
2119 get_mplock();
2120
85fceac1
MD
2121 /*
2122 * The last reference has gone away, we own the fp structure free
2123 * and clear.
2124 */
984263bc
MD
2125 if (fp->f_count < 0)
2126 panic("fdrop: count < 0");
7749886d
MD
2127 if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE &&
2128 (((struct vnode *)fp->f_data)->v_flag & VMAYHAVELOCKS)
2129 ) {
984263bc
MD
2130 lf.l_whence = SEEK_SET;
2131 lf.l_start = 0;
2132 lf.l_len = 0;
2133 lf.l_type = F_UNLCK;
2134 vp = (struct vnode *)fp->f_data;
71c18fe3 2135 (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, 0);
984263bc
MD
2136 }
2137 if (fp->f_ops != &badfileops)
87de5057 2138 error = fo_close(fp);
984263bc
MD
2139 else
2140 error = 0;
2141 ffree(fp);
5b287bba 2142 rel_mplock();
984263bc
MD
2143 return (error);
2144}
2145
2146/*
2147 * Apply an advisory lock on a file descriptor.
2148 *
2149 * Just attempt to get a record lock of the requested type on
2150 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
2151 */
984263bc 2152int
753fd850 2153sys_flock(struct flock_args *uap)
984263bc 2154{
41c20dac 2155 struct proc *p = curproc;
41c20dac 2156 struct file *fp;
984263bc
MD
2157 struct vnode *vp;
2158 struct flock lf;
5b287bba 2159 int error;
984263bc 2160
5b287bba 2161 if ((fp = holdfp(p->p_fd, uap->fd, -1)) == NULL)
984263bc 2162 return (EBADF);
5b287bba
MD
2163 if (fp->f_type != DTYPE_VNODE) {
2164 error = EOPNOTSUPP;
2165 goto done;
2166 }
984263bc
MD
2167 vp = (struct vnode *)fp->f_data;
2168 lf.l_whence = SEEK_SET;
2169 lf.l_start = 0;
2170 lf.l_len = 0;
2171 if (uap->how & LOCK_UN) {
2172 lf.l_type = F_UNLCK;
2173 fp->f_flag &= ~FHASLOCK;
5b287bba
MD
2174 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, 0);
2175 goto done;
984263bc
MD
2176 }
2177 if (uap->how & LOCK_EX)
2178 lf.l_type = F_WRLCK;
2179 else if (uap->how & LOCK_SH)
2180 lf.l_type = F_RDLCK;
5b287bba
MD
2181 else {
2182 error = EBADF;
2183 goto done;
2184 }
984263bc
MD
2185 fp->f_flag |= FHASLOCK;
2186 if (uap->how & LOCK_NB)
5b287bba
MD
2187 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, 0);
2188 else
2189 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_WAIT);
2190done:
2191 fdrop(fp);
2192 return (error);
984263bc
MD
2193}
2194
2195/*
2196 * File Descriptor pseudo-device driver (/dev/fd/).
2197 *
2198 * Opening minor device N dup()s the file (if any) connected to file
2199 * descriptor N belonging to the calling process. Note that this driver
2200 * consists of only the ``open()'' routine, because all subsequent
2201 * references to this file will be direct to the other driver.
2202 */
2203/* ARGSUSED */
2204static int
fef8985e 2205fdopen(struct dev_open_args *ap)
984263bc 2206{
fef8985e
MD
2207 thread_t td = curthread;
2208
fbfe4e7d 2209 KKASSERT(td->td_lwp != NULL);
984263bc
MD
2210
2211 /*
fbfe4e7d 2212 * XXX Kludge: set curlwp->lwp_dupfd to contain the value of the
984263bc
MD
2213 * the file descriptor being sought for duplication. The error
2214 * return ensures that the vnode for this device will be released
2215 * by vn_open. Open will detect this special error and take the
2216 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
2217 * will simply report the error.
2218 */
fef8985e 2219 td->td_lwp->lwp_dupfd = minor(ap->a_head.a_dev);
984263bc
MD
2220 return (ENODEV);
2221}
2222
2223/*
259b8ea0
MD
2224 * The caller has reserved the file descriptor dfd for us. On success we
2225 * must fsetfd() it. On failure the caller will clean it up.
5b287bba
MD
2226 *
2227 * NOT MPSAFE - isn't getting spinlocks, possibly other things
984263bc
MD
2228 */
2229int
259b8ea0 2230dupfdopen(struct proc *p, int dfd, int sfd, int mode, int error)
984263bc 2231{
259b8ea0 2232 struct filedesc *fdp = p->p_fd;
41c20dac 2233 struct file *wfp;
259b8ea0 2234 struct file *xfp;
984263bc 2235
259b8ea0 2236 if ((wfp = holdfp(fdp, sfd, -1)) == NULL)
984263bc 2237 return (EBADF);
984263bc
MD
2238
2239 /*
2240 * There are two cases of interest here.
2241 *
259b8ea0 2242 * For ENODEV simply dup sfd to file descriptor dfd and return.
984263bc 2243 *
259b8ea0
MD
2244 * For ENXIO steal away the file structure from sfd and store it
2245 * dfd. sfd is effectively closed by this operation.
984263bc
MD
2246 *
2247 * Any other error code is just returned.
2248 */
2249 switch (error) {
2250 case ENODEV:
2251 /*
2252 * Check that the mode the file is being opened for is a
2253 * subset of the mode of the existing descriptor.
2254 */
2255 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
2256 return (EACCES);
259b8ea0
MD
2257 fdp->fd_files[dfd].fileflags = fdp->fd_files[sfd].fileflags;
2258 fsetfd(p, wfp, dfd);
2259 error = 0;
2260 break;
984263bc
MD
2261 case ENXIO:
2262 /*
2263 * Steal away the file pointer from dfd, and stuff it into indx.
2264 */
259b8ea0
MD
2265 fdp->fd_files[dfd].fileflags = fdp->fd_files[sfd].fileflags;
2266 fsetfd(p, wfp, dfd);
5b287bba 2267 if ((xfp = funsetfd_locked(fdp, sfd)) != NULL)
259b8ea0
MD
2268 fdrop(xfp);
2269 KKASSERT(xfp == wfp); /* XXX MP RACE */
2270 error = 0;
2271 break;
984263bc 2272 default:
259b8ea0 2273 break;
984263bc 2274 }
259b8ea0
MD
2275 fdrop(wfp);
2276 return (error);
984263bc
MD
2277}
2278
5b287bba
MD
2279/*
2280 * NOT MPSAFE - I think these refer to a common file descriptor table
2281 * and we need to spinlock that to link fdtol in.
2282 */
984263bc
MD
2283struct filedesc_to_leader *
2284filedesc_to_leader_alloc(struct filedesc_to_leader *old,
2285 struct proc *leader)
2286{
2287 struct filedesc_to_leader *fdtol;
2288
77652cad 2289 fdtol = kmalloc(sizeof(struct filedesc_to_leader),
7bf8660a 2290 M_FILEDESC_TO_LEADER, M_WAITOK);
984263bc
MD
2291 fdtol->fdl_refcount = 1;
2292 fdtol->fdl_holdcount = 0;
2293 fdtol->fdl_wakeup = 0;
2294 fdtol->fdl_leader = leader;
2295 if (old != NULL) {
2296 fdtol->fdl_next = old->fdl_next;
2297 fdtol->fdl_prev = old;
2298 old->fdl_next = fdtol;
2299 fdtol->fdl_next->fdl_prev = fdtol;
2300 } else {
2301 fdtol->fdl_next = fdtol;
2302 fdtol->fdl_prev = fdtol;
2303 }
2304 return fdtol;
2305}
2306
2dd63755
MD
2307/*
2308 * Scan all file pointers in the system. The callback is made with
2309 * both the master list spinlock held and the fp spinlock held,
2310 * both exclusively.
2311 *
2312 * MPSAFE
2313 *
2314 * WARNING: both the filehead spinlock and the file pointer spinlock are
2315 * held exclusively when the callback is made. The file pointer is not
2316 * referenced.
2317 */
2318void
2319allfiles_scan_exclusive(int (*callback)(struct file *, void *), void *data)
2320{
2321 struct file *fp;
2322 int res;
2323
2324 spin_lock_wr(&filehead_spin);
2325 LIST_FOREACH(fp, &filehead, f_list) {
2326 spin_lock_wr(&fp->f_spin);
2327 res = callback(fp, data);
2328 spin_unlock_wr(&fp->f_spin);
2329 if (res < 0)
2330 break;
2331 }
2332 spin_unlock_wr(&filehead_spin);
2333}
2334
984263bc
MD
2335/*
2336 * Get file structures.
5b287bba
MD
2337 *
2338 * NOT MPSAFE - process list scan, SYSCTL_OUT (probably not mpsafe)
984263bc 2339 */
8fa76237
MD
2340
2341struct sysctl_kern_file_info {
2342 int count;
2343 int error;
2344 struct sysctl_req *req;
2345};
2346
2347static int sysctl_kern_file_callback(struct proc *p, void *data);
2348
984263bc
MD
2349static int
2350sysctl_kern_file(SYSCTL_HANDLER_ARGS)
2351{
8fa76237 2352 struct sysctl_kern_file_info info;
984263bc
MD
2353
2354 /*
7b124c9f
JS
2355 * Note: because the number of file descriptors is calculated
2356 * in different ways for sizing vs returning the data,
2357 * there is information leakage from the first loop. However,
2358 * it is of a similar order of magnitude to the leakage from
2359 * global system statistics such as kern.openfiles.
6d132b4d
MD
2360 *
2361 * When just doing a count, note that we cannot just count
2362 * the elements and add f_count via the filehead list because
2363 * threaded processes share their descriptor table and f_count might
2364 * still be '1' in that case.
e38910da
MD
2365 *
2366 * Since the SYSCTL op can block, we must hold the process to
2367 * prevent it being ripped out from under us either in the
2368 * file descriptor loop or in the greater LIST_FOREACH. The
2369 * process may be in varying states of disrepair. If the process
2370 * is in SZOMB we may have caught it just as it is being removed
2371 * from the allproc list, we must skip it in that case to maintain
2372 * an unbroken chain through the allproc list.
984263bc 2373 */
8fa76237
MD
2374 info.count = 0;
2375 info.error = 0;
2376 info.req = req;
2377 allproc_scan(sysctl_kern_file_callback, &info);
6d132b4d
MD
2378
2379 /*
2380 * When just calculating the size, overestimate a bit to try to
2381 * prevent system activity from causing the buffer-fill call
2382 * to fail later on.
2383 */
2384 if (req->oldptr == NULL) {
8fa76237
MD
2385 info.count = (info.count + 16) + (info.count / 10);
2386 info.error = SYSCTL_OUT(req, NULL,
2387 info.count * sizeof(struct kinfo_file));
6d132b4d 2388 }
8fa76237
MD
2389 return (info.error);
2390}
2391
2392static int
2393sysctl_kern_file_callback(struct proc *p, void *data)
2394{
2395 struct sysctl_kern_file_info *info = data;
2396 struct kinfo_file kf;
2397 struct filedesc *fdp;
2398 struct file *fp;
2399 uid_t uid;
2400 int n;
2401
2402 if (p->p_stat == SIDL || (p->p_flag & P_ZOMBIE))
2403 return(0);
2404 if (!PRISON_CHECK(info->req->td->td_proc->p_ucred, p->p_ucred) != 0)
2405 return(0);
2406 if ((fdp = p->p_fd) == NULL)
2407 return(0);
2408 spin_lock_rd(&fdp->fd_spin);
2409 for (n = 0; n < fdp->fd_nfiles; ++n) {
2410 if ((fp = fdp->fd_files[n].fp) == NULL)
2411 continue;
2412 if (info->req->oldptr == NULL) {
2413 ++info->count;
2414 } else {
2415 uid = p->p_ucred ? p->p_ucred->cr_uid : -1;
2416 kcore_make_file(&kf, fp, p->p_pid, uid, n);
2417 spin_unlock_rd(&fdp->fd_spin);
2418 info->error = SYSCTL_OUT(info->req, &kf, sizeof(kf));
2419 spin_lock_rd(&fdp->fd_spin);
2420 if (info->error)
2421 break;
2422 }
2423 }
2424 spin_unlock_rd(&fdp->fd_spin);
2425 if (info->error)
2426 return(-1);
2427 return(0);
984263bc
MD
2428}
2429
2430SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
2431 0, 0, sysctl_kern_file, "S,file", "Entire file table");
2432
2433SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
2434 &maxfilesperproc, 0, "Maximum files allowed open per process");
2435
2436SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
2437 &maxfiles, 0, "Maximum number of files");
2438
60ee93b9
MD
2439SYSCTL_INT(_kern, OID_AUTO, maxfilesrootres, CTLFLAG_RW,
2440 &maxfilesrootres, 0, "Descriptors reserved for root use");
2441
984263bc
MD
2442SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
2443 &nfiles, 0, "System-wide number of open files");
2444
2445static void
2446fildesc_drvinit(void *unused)
2447{
2448 int fd;
2449
fef8985e 2450 dev_ops_add(&fildesc_ops, 0, 0);
e4c9c0c8 2451 for (fd = 0; fd < NUMFDESC; fd++) {
fef8985e 2452 make_dev(&fildesc_ops, fd,
984263bc 2453 UID_BIN, GID_BIN, 0666, "fd/%d", fd);
e4c9c0c8 2454 }
fef8985e
MD
2455 make_dev(&fildesc_ops, 0, UID_ROOT, GID_WHEEL, 0666, "stdin");
2456 make_dev(&fildesc_ops, 1, UID_ROOT, GID_WHEEL, 0666, "stdout");
2457 make_dev(&fildesc_ops, 2, UID_ROOT, GID_WHEEL, 0666, "stderr");
984263bc
MD
2458}
2459
d9b2033e
MD
2460/*
2461 * MPSAFE
2462 */
984263bc 2463struct fileops badfileops = {
b2d248cb
MD
2464 .fo_read = badfo_readwrite,
2465 .fo_write = badfo_readwrite,
2466 .fo_ioctl = badfo_ioctl,
2467 .fo_poll = badfo_poll,
2468 .fo_kqfilter = badfo_kqfilter,
2469 .fo_stat = badfo_stat,
2470 .fo_close = badfo_close,
2471 .fo_shutdown = badfo_shutdown
984263bc
MD
2472};
2473
d9b2033e
MD
2474/*
2475 * MPSAFE
2476 */
984263bc 2477static int
dadab5e9
MD
2478badfo_readwrite(
2479 struct file *fp,
2480 struct uio *uio,
2481 struct ucred *cred,
87de5057 2482 int flags
dadab5e9 2483) {
984263bc
MD
2484 return (EBADF);
2485}
2486
d9b2033e
MD
2487/*
2488 * MPSAFE
2489 */
984263bc 2490static int
87de5057 2491badfo_ioctl(struct file *fp, u_long com, caddr_t data, struct ucred *cred)
984263bc 2492{
984263bc
MD
2493 return (EBADF);
2494}
2495
d9b2033e
MD
2496/*
2497 * MPSAFE
2498 */
984263bc 2499static int
87de5057 2500badfo_poll(struct file *fp, int events, struct ucred *cred)
984263bc 2501{
984263bc
MD
2502 return (0);
2503}
2504
d9b2033e
MD
2505/*
2506 * MPSAFE
2507 */
984263bc 2508static int
dadab5e9 2509badfo_kqfilter(struct file *fp, struct knote *kn)
984263bc 2510{
984263bc
MD
2511 return (0);
2512}
2513
2514static int
87de5057 2515badfo_stat(struct file *fp, struct stat *sb, struct ucred *cred)
984263bc 2516{
984263bc
MD
2517 return (EBADF);
2518}
2519
d9b2033e
MD
2520/*
2521 * MPSAFE
2522 */
984263bc 2523static int
87de5057 2524badfo_close(struct file *fp)
984263bc 2525{
984263bc
MD
2526 return (EBADF);
2527}
2528
d9b2033e
MD
2529/*
2530 * MPSAFE
2531 */
004d2de5 2532static int
87de5057 2533badfo_shutdown(struct file *fp, int how)
004d2de5
MD
2534{
2535 return (EBADF);
2536}
2537
d9b2033e
MD
2538/*
2539 * MPSAFE
2540 */
004d2de5 2541int
87de5057 2542nofo_shutdown(struct file *fp, int how)
004d2de5
MD
2543{
2544 return (EOPNOTSUPP);
2545}
2546
984263bc
MD
2547SYSINIT(fildescdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,
2548 fildesc_drvinit,NULL)