proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[dragonfly.git] / sys / kern / kern_descrip.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94
39 * $FreeBSD: src/sys/kern/kern_descrip.c,v 1.81.2.17 2003/06/06 20:21:32 tegge Exp $
dadab5e9 40 * $DragonFly: src/sys/kern/kern_descrip.c,v 1.4 2003/06/25 03:55:57 dillon Exp $
984263bc
MD
41 */
42
43#include "opt_compat.h"
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/malloc.h>
47#include <sys/sysproto.h>
48#include <sys/conf.h>
49#include <sys/filedesc.h>
50#include <sys/kernel.h>
51#include <sys/sysctl.h>
52#include <sys/vnode.h>
53#include <sys/proc.h>
54#include <sys/namei.h>
55#include <sys/file.h>
56#include <sys/stat.h>
57#include <sys/filio.h>
58#include <sys/fcntl.h>
59#include <sys/unistd.h>
60#include <sys/resourcevar.h>
61#include <sys/event.h>
62
63#include <vm/vm.h>
64#include <vm/vm_extern.h>
65
dadab5e9
MD
66#include <sys/file2.h>
67
984263bc
MD
68static MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
69static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "file desc to leader",
70 "file desc to leader structures");
71MALLOC_DEFINE(M_FILE, "file", "Open file structure");
72static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
73
74static d_open_t fdopen;
75#define NUMFDESC 64
76
77#define CDEV_MAJOR 22
78static struct cdevsw fildesc_cdevsw = {
79 /* open */ fdopen,
80 /* close */ noclose,
81 /* read */ noread,
82 /* write */ nowrite,
83 /* ioctl */ noioctl,
84 /* poll */ nopoll,
85 /* mmap */ nommap,
86 /* strategy */ nostrategy,
87 /* name */ "FD",
88 /* maj */ CDEV_MAJOR,
89 /* dump */ nodump,
90 /* psize */ nopsize,
91 /* flags */ 0,
92 /* bmaj */ -1
93};
94
95static int do_dup __P((struct filedesc *fdp, int old, int new, register_t *retval, struct proc *p));
96static int badfo_readwrite __P((struct file *fp, struct uio *uio,
dadab5e9 97 struct ucred *cred, int flags, struct thread *td));
984263bc 98static int badfo_ioctl __P((struct file *fp, u_long com, caddr_t data,
dadab5e9 99 struct thread *td));
984263bc 100static int badfo_poll __P((struct file *fp, int events,
dadab5e9 101 struct ucred *cred, struct thread *td));
984263bc 102static int badfo_kqfilter __P((struct file *fp, struct knote *kn));
dadab5e9
MD
103static int badfo_stat __P((struct file *fp, struct stat *sb, struct thread *td));
104static int badfo_close __P((struct file *fp, struct thread *td));
984263bc
MD
105
106/*
107 * Descriptor management.
108 */
109struct filelist filehead; /* head of list of open files */
110int nfiles; /* actual number of open files */
111extern int cmask;
112
113/*
114 * System calls on descriptors.
115 */
116#ifndef _SYS_SYSPROTO_H_
117struct getdtablesize_args {
118 int dummy;
119};
120#endif
121/* ARGSUSED */
122int
41c20dac 123getdtablesize(struct getdtablesize_args *uap)
984263bc 124{
41c20dac 125 struct proc *p = curproc;
984263bc
MD
126
127 p->p_retval[0] =
128 min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
129 return (0);
130}
131
132/*
133 * Duplicate a file descriptor to a particular value.
134 *
135 * note: keep in mind that a potential race condition exists when closing
136 * descriptors from a shared descriptor table (via rfork).
137 */
138#ifndef _SYS_SYSPROTO_H_
139struct dup2_args {
140 u_int from;
141 u_int to;
142};
143#endif
144/* ARGSUSED */
145int
41c20dac 146dup2(struct dup2_args *uap)
984263bc 147{
41c20dac
MD
148 struct proc *p = curproc;
149 struct filedesc *fdp = p->p_fd;
150 u_int old = uap->from, new = uap->to;
984263bc
MD
151 int i, error;
152
153retry:
154 if (old >= fdp->fd_nfiles ||
155 fdp->fd_ofiles[old] == NULL ||
156 new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
157 new >= maxfilesperproc) {
158 return (EBADF);
159 }
160 if (old == new) {
161 p->p_retval[0] = new;
162 return (0);
163 }
164 if (new >= fdp->fd_nfiles) {
165 if ((error = fdalloc(p, new, &i)))
166 return (error);
167 /*
168 * fdalloc() may block, retest everything.
169 */
170 goto retry;
171 }
172 return (do_dup(fdp, (int)old, (int)new, p->p_retval, p));
173}
174
175/*
176 * Duplicate a file descriptor.
177 */
178#ifndef _SYS_SYSPROTO_H_
179struct dup_args {
180 u_int fd;
181};
182#endif
183/* ARGSUSED */
184int
41c20dac 185dup(struct dup_args *uap)
984263bc 186{
41c20dac
MD
187 struct proc *p = curproc;
188 struct filedesc *fdp;
984263bc
MD
189 u_int old;
190 int new, error;
191
192 old = uap->fd;
193 fdp = p->p_fd;
194 if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL)
195 return (EBADF);
196 if ((error = fdalloc(p, 0, &new)))
197 return (error);
198 return (do_dup(fdp, (int)old, new, p->p_retval, p));
199}
200
201/*
202 * The file control system call.
203 */
204#ifndef _SYS_SYSPROTO_H_
205struct fcntl_args {
206 int fd;
207 int cmd;
208 long arg;
209};
210#endif
211/* ARGSUSED */
212int
41c20dac 213fcntl(struct fcntl_args *uap)
984263bc 214{
dadab5e9
MD
215 struct thread *td = curthread;
216 struct proc *p = td->td_proc;
41c20dac
MD
217 struct filedesc *fdp = p->p_fd;
218 struct file *fp;
219 char *pop;
984263bc
MD
220 struct vnode *vp;
221 int i, tmp, error, flg = F_POSIX;
222 struct flock fl;
223 u_int newmin;
224
dadab5e9
MD
225 KKASSERT(p);
226
984263bc
MD
227 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
228 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
229 return (EBADF);
230 pop = &fdp->fd_ofileflags[uap->fd];
231
232 switch (uap->cmd) {
233 case F_DUPFD:
234 newmin = uap->arg;
235 if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
236 newmin >= maxfilesperproc)
237 return (EINVAL);
238 if ((error = fdalloc(p, newmin, &i)))
239 return (error);
240 return (do_dup(fdp, uap->fd, i, p->p_retval, p));
241
242 case F_GETFD:
243 p->p_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
244 return (0);
245
246 case F_SETFD:
247 *pop = (*pop &~ UF_EXCLOSE) |
248 (uap->arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
249 return (0);
250
251 case F_GETFL:
252 p->p_retval[0] = OFLAGS(fp->f_flag);
253 return (0);
254
255 case F_SETFL:
256 fhold(fp);
257 fp->f_flag &= ~FCNTLFLAGS;
258 fp->f_flag |= FFLAGS(uap->arg & ~O_ACCMODE) & FCNTLFLAGS;
259 tmp = fp->f_flag & FNONBLOCK;
dadab5e9 260 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td);
984263bc 261 if (error) {
dadab5e9 262 fdrop(fp, td);
984263bc
MD
263 return (error);
264 }
265 tmp = fp->f_flag & FASYNC;
dadab5e9 266 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, td);
984263bc 267 if (!error) {
dadab5e9 268 fdrop(fp, td);
984263bc
MD
269 return (0);
270 }
271 fp->f_flag &= ~FNONBLOCK;
272 tmp = 0;
dadab5e9
MD
273 (void)fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td);
274 fdrop(fp, td);
984263bc
MD
275 return (error);
276
277 case F_GETOWN:
278 fhold(fp);
dadab5e9
MD
279 error = fo_ioctl(fp, FIOGETOWN, (caddr_t)p->p_retval, td);
280 fdrop(fp, td);
984263bc
MD
281 return(error);
282
283 case F_SETOWN:
284 fhold(fp);
dadab5e9
MD
285 error = fo_ioctl(fp, FIOSETOWN, (caddr_t)&uap->arg, td);
286 fdrop(fp, td);
984263bc
MD
287 return(error);
288
289 case F_SETLKW:
290 flg |= F_WAIT;
291 /* Fall into F_SETLK */
292
293 case F_SETLK:
294 if (fp->f_type != DTYPE_VNODE)
295 return (EBADF);
296 vp = (struct vnode *)fp->f_data;
297
298 /*
299 * copyin/lockop may block
300 */
301 fhold(fp);
302 /* Copy in the lock structure */
303 error = copyin((caddr_t)(intptr_t)uap->arg, (caddr_t)&fl,
304 sizeof(fl));
305 if (error) {
dadab5e9 306 fdrop(fp, td);
984263bc
MD
307 return (error);
308 }
309 if (fl.l_whence == SEEK_CUR)
310 fl.l_start += fp->f_offset;
311
312 switch (fl.l_type) {
313 case F_RDLCK:
314 if ((fp->f_flag & FREAD) == 0) {
315 error = EBADF;
316 break;
317 }
318 p->p_leader->p_flag |= P_ADVLOCK;
319 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
320 &fl, flg);
321 break;
322 case F_WRLCK:
323 if ((fp->f_flag & FWRITE) == 0) {
324 error = EBADF;
325 break;
326 }
327 p->p_leader->p_flag |= P_ADVLOCK;
328 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
329 &fl, flg);
330 break;
331 case F_UNLCK:
332 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
333 &fl, F_POSIX);
334 break;
335 default:
336 error = EINVAL;
337 break;
338 }
339 /* Check for race with close */
340 if ((unsigned) uap->fd >= fdp->fd_nfiles ||
341 fp != fdp->fd_ofiles[uap->fd]) {
342 fl.l_whence = SEEK_SET;
343 fl.l_start = 0;
344 fl.l_len = 0;
345 fl.l_type = F_UNLCK;
346 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
347 F_UNLCK, &fl, F_POSIX);
348 }
dadab5e9 349 fdrop(fp, td);
984263bc
MD
350 return(error);
351
352 case F_GETLK:
353 if (fp->f_type != DTYPE_VNODE)
354 return (EBADF);
355 vp = (struct vnode *)fp->f_data;
356 /*
357 * copyin/lockop may block
358 */
359 fhold(fp);
360 /* Copy in the lock structure */
361 error = copyin((caddr_t)(intptr_t)uap->arg, (caddr_t)&fl,
362 sizeof(fl));
363 if (error) {
dadab5e9 364 fdrop(fp, td);
984263bc
MD
365 return (error);
366 }
367 if (fl.l_type != F_RDLCK && fl.l_type != F_WRLCK &&
368 fl.l_type != F_UNLCK) {
dadab5e9 369 fdrop(fp, td);
984263bc
MD
370 return (EINVAL);
371 }
372 if (fl.l_whence == SEEK_CUR)
373 fl.l_start += fp->f_offset;
374 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK,
375 &fl, F_POSIX);
dadab5e9 376 fdrop(fp, td);
984263bc
MD
377 if (error == 0) {
378 error = copyout((caddr_t)&fl,
379 (caddr_t)(intptr_t)uap->arg, sizeof(fl));
380 }
381 return(error);
382 default:
383 return (EINVAL);
384 }
385 /* NOTREACHED */
386}
387
388/*
389 * Common code for dup, dup2, and fcntl(F_DUPFD).
390 */
391static int
392do_dup(fdp, old, new, retval, p)
41c20dac
MD
393 struct filedesc *fdp;
394 int old, new;
984263bc
MD
395 register_t *retval;
396 struct proc *p;
397{
dadab5e9 398 struct thread *td = p->p_thread;
984263bc
MD
399 struct file *fp;
400 struct file *delfp;
401 int holdleaders;
402
403 /*
404 * Save info on the descriptor being overwritten. We have
405 * to do the unmap now, but we cannot close it without
406 * introducing an ownership race for the slot.
407 */
408 delfp = fdp->fd_ofiles[new];
409 if (delfp != NULL && p->p_fdtol != NULL) {
410 /*
411 * Ask fdfree() to sleep to ensure that all relevant
412 * process leaders can be traversed in closef().
413 */
414 fdp->fd_holdleaderscount++;
415 holdleaders = 1;
416 } else
417 holdleaders = 0;
418#if 0
419 if (delfp && (fdp->fd_ofileflags[new] & UF_MAPPED))
420 (void) munmapfd(p, new);
421#endif
422
423 /*
424 * Duplicate the source descriptor, update lastfile
425 */
426 fp = fdp->fd_ofiles[old];
427 fdp->fd_ofiles[new] = fp;
428 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
429 fhold(fp);
430 if (new > fdp->fd_lastfile)
431 fdp->fd_lastfile = new;
432 *retval = new;
433
434 /*
435 * If we dup'd over a valid file, we now own the reference to it
436 * and must dispose of it using closef() semantics (as if a
437 * close() were performed on it).
438 */
439 if (delfp) {
dadab5e9 440 (void) closef(delfp, td);
984263bc
MD
441 if (holdleaders) {
442 fdp->fd_holdleaderscount--;
443 if (fdp->fd_holdleaderscount == 0 &&
444 fdp->fd_holdleaderswakeup != 0) {
445 fdp->fd_holdleaderswakeup = 0;
446 wakeup(&fdp->fd_holdleaderscount);
447 }
448 }
449 }
450 return (0);
451}
452
453/*
454 * If sigio is on the list associated with a process or process group,
455 * disable signalling from the device, remove sigio from the list and
456 * free sigio.
457 */
458void
459funsetown(sigio)
460 struct sigio *sigio;
461{
462 int s;
463
464 if (sigio == NULL)
465 return;
466 s = splhigh();
467 *(sigio->sio_myref) = NULL;
468 splx(s);
469 if (sigio->sio_pgid < 0) {
470 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
471 sigio, sio_pgsigio);
472 } else /* if ((*sigiop)->sio_pgid > 0) */ {
473 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
474 sigio, sio_pgsigio);
475 }
476 crfree(sigio->sio_ucred);
477 FREE(sigio, M_SIGIO);
478}
479
480/* Free a list of sigio structures. */
481void
482funsetownlst(sigiolst)
483 struct sigiolst *sigiolst;
484{
485 struct sigio *sigio;
486
487 while ((sigio = SLIST_FIRST(sigiolst)) != NULL)
488 funsetown(sigio);
489}
490
491/*
492 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
493 *
494 * After permission checking, add a sigio structure to the sigio list for
495 * the process or process group.
496 */
497int
498fsetown(pgid, sigiop)
499 pid_t pgid;
500 struct sigio **sigiop;
501{
502 struct proc *proc;
503 struct pgrp *pgrp;
504 struct sigio *sigio;
505 int s;
506
507 if (pgid == 0) {
508 funsetown(*sigiop);
509 return (0);
510 }
511 if (pgid > 0) {
512 proc = pfind(pgid);
513 if (proc == NULL)
514 return (ESRCH);
515
516 /*
517 * Policy - Don't allow a process to FSETOWN a process
518 * in another session.
519 *
520 * Remove this test to allow maximum flexibility or
521 * restrict FSETOWN to the current process or process
522 * group for maximum safety.
523 */
524 if (proc->p_session != curproc->p_session)
525 return (EPERM);
526
527 pgrp = NULL;
528 } else /* if (pgid < 0) */ {
529 pgrp = pgfind(-pgid);
530 if (pgrp == NULL)
531 return (ESRCH);
532
533 /*
534 * Policy - Don't allow a process to FSETOWN a process
535 * in another session.
536 *
537 * Remove this test to allow maximum flexibility or
538 * restrict FSETOWN to the current process or process
539 * group for maximum safety.
540 */
541 if (pgrp->pg_session != curproc->p_session)
542 return (EPERM);
543
544 proc = NULL;
545 }
546 funsetown(*sigiop);
547 MALLOC(sigio, struct sigio *, sizeof(struct sigio), M_SIGIO, M_WAITOK);
548 if (pgid > 0) {
549 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
550 sigio->sio_proc = proc;
551 } else {
552 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
553 sigio->sio_pgrp = pgrp;
554 }
555 sigio->sio_pgid = pgid;
dadab5e9 556 curproc->p_ucred = crhold(curproc->p_ucred);
984263bc
MD
557 sigio->sio_ucred = curproc->p_ucred;
558 /* It would be convenient if p_ruid was in ucred. */
41c20dac 559 sigio->sio_ruid = curproc->p_ucred->cr_ruid;
984263bc
MD
560 sigio->sio_myref = sigiop;
561 s = splhigh();
562 *sigiop = sigio;
563 splx(s);
564 return (0);
565}
566
567/*
568 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
569 */
570pid_t
571fgetown(sigio)
572 struct sigio *sigio;
573{
574 return (sigio != NULL ? sigio->sio_pgid : 0);
575}
576
577/*
578 * Close a file descriptor.
579 */
580#ifndef _SYS_SYSPROTO_H_
581struct close_args {
582 int fd;
583};
584#endif
585/* ARGSUSED */
586int
41c20dac 587close(struct close_args *uap)
984263bc 588{
dadab5e9
MD
589 struct thread *td = curthread;
590 struct proc *p = td->td_proc;
591 struct filedesc *fdp;
41c20dac
MD
592 struct file *fp;
593 int fd = uap->fd;
984263bc
MD
594 int error;
595 int holdleaders;
596
dadab5e9
MD
597 KKASSERT(p);
598 fdp = p->p_fd;
599
984263bc
MD
600 if ((unsigned)fd >= fdp->fd_nfiles ||
601 (fp = fdp->fd_ofiles[fd]) == NULL)
602 return (EBADF);
603#if 0
604 if (fdp->fd_ofileflags[fd] & UF_MAPPED)
605 (void) munmapfd(p, fd);
606#endif
607 fdp->fd_ofiles[fd] = NULL;
608 fdp->fd_ofileflags[fd] = 0;
609 holdleaders = 0;
610 if (p->p_fdtol != NULL) {
611 /*
612 * Ask fdfree() to sleep to ensure that all relevant
613 * process leaders can be traversed in closef().
614 */
615 fdp->fd_holdleaderscount++;
616 holdleaders = 1;
617 }
618
619 /*
620 * we now hold the fp reference that used to be owned by the descriptor
621 * array.
622 */
623 while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
624 fdp->fd_lastfile--;
625 if (fd < fdp->fd_freefile)
626 fdp->fd_freefile = fd;
627 if (fd < fdp->fd_knlistsize)
628 knote_fdclose(p, fd);
dadab5e9 629 error = closef(fp, td);
984263bc
MD
630 if (holdleaders) {
631 fdp->fd_holdleaderscount--;
632 if (fdp->fd_holdleaderscount == 0 &&
633 fdp->fd_holdleaderswakeup != 0) {
634 fdp->fd_holdleaderswakeup = 0;
635 wakeup(&fdp->fd_holdleaderscount);
636 }
637 }
638 return (error);
639}
640
641#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
642/*
643 * Return status information about a file descriptor.
644 */
645#ifndef _SYS_SYSPROTO_H_
646struct ofstat_args {
647 int fd;
648 struct ostat *sb;
649};
650#endif
651/* ARGSUSED */
652int
41c20dac 653ofstat(struct ofstat_args *uap)
984263bc 654{
dadab5e9
MD
655 struct thread *td = curthread;
656 struct proc *p = td->td_proc;
657 struct filedesc *fdp;
41c20dac 658 struct file *fp;
984263bc
MD
659 struct stat ub;
660 struct ostat oub;
661 int error;
662
dadab5e9
MD
663 KKASSERT(p);
664 fdp = p->p_fd;
984263bc
MD
665 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
666 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
667 return (EBADF);
668 fhold(fp);
dadab5e9 669 error = fo_stat(fp, &ub, td);
984263bc
MD
670 if (error == 0) {
671 cvtstat(&ub, &oub);
672 error = copyout((caddr_t)&oub, (caddr_t)uap->sb, sizeof (oub));
673 }
dadab5e9 674 fdrop(fp, td);
984263bc
MD
675 return (error);
676}
677#endif /* COMPAT_43 || COMPAT_SUNOS */
678
679/*
680 * Return status information about a file descriptor.
681 */
682#ifndef _SYS_SYSPROTO_H_
683struct fstat_args {
684 int fd;
685 struct stat *sb;
686};
687#endif
688/* ARGSUSED */
689int
41c20dac 690fstat(struct fstat_args *uap)
984263bc 691{
dadab5e9
MD
692 struct thread *td = curthread;
693 struct proc *p = td->td_proc;
41c20dac
MD
694 struct filedesc *fdp = p->p_fd;
695 struct file *fp;
984263bc
MD
696 struct stat ub;
697 int error;
698
dadab5e9
MD
699 KKASSERT(p);
700 fdp = p->p_fd;
984263bc
MD
701 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
702 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
703 return (EBADF);
704 fhold(fp);
dadab5e9 705 error = fo_stat(fp, &ub, td);
984263bc
MD
706 if (error == 0)
707 error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub));
dadab5e9 708 fdrop(fp, td);
984263bc
MD
709 return (error);
710}
711
712/*
713 * Return status information about a file descriptor.
714 */
715#ifndef _SYS_SYSPROTO_H_
716struct nfstat_args {
717 int fd;
718 struct nstat *sb;
719};
720#endif
721/* ARGSUSED */
722int
41c20dac 723nfstat(struct nfstat_args *uap)
984263bc 724{
dadab5e9
MD
725 struct thread *td = curthread;
726 struct proc *p = td->td_proc;
727 struct filedesc *fdp;
41c20dac 728 struct file *fp;
984263bc
MD
729 struct stat ub;
730 struct nstat nub;
731 int error;
732
dadab5e9
MD
733 KKASSERT(p);
734 fdp = p->p_fd;
984263bc
MD
735 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
736 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
737 return (EBADF);
738 fhold(fp);
dadab5e9 739 error = fo_stat(fp, &ub, td);
984263bc
MD
740 if (error == 0) {
741 cvtnstat(&ub, &nub);
742 error = copyout((caddr_t)&nub, (caddr_t)uap->sb, sizeof (nub));
743 }
dadab5e9 744 fdrop(fp, td);
984263bc
MD
745 return (error);
746}
747
748/*
749 * Return pathconf information about a file descriptor.
750 */
751#ifndef _SYS_SYSPROTO_H_
752struct fpathconf_args {
753 int fd;
754 int name;
755};
756#endif
757/* ARGSUSED */
758int
41c20dac 759fpathconf(struct fpathconf_args *uap)
984263bc 760{
dadab5e9
MD
761 struct thread *td = curthread;
762 struct proc *p = td->td_proc;
763 struct filedesc *fdp;
984263bc
MD
764 struct file *fp;
765 struct vnode *vp;
766 int error = 0;
767
dadab5e9
MD
768 KKASSERT(p);
769 fdp = p->p_fd;
984263bc
MD
770 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
771 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
772 return (EBADF);
773
774 fhold(fp);
775
776 switch (fp->f_type) {
777 case DTYPE_PIPE:
778 case DTYPE_SOCKET:
779 if (uap->name != _PC_PIPE_BUF) {
780 error = EINVAL;
781 } else {
782 p->p_retval[0] = PIPE_BUF;
783 error = 0;
784 }
785 break;
786 case DTYPE_FIFO:
787 case DTYPE_VNODE:
788 vp = (struct vnode *)fp->f_data;
789 error = VOP_PATHCONF(vp, uap->name, p->p_retval);
790 break;
791 default:
792 error = EOPNOTSUPP;
793 break;
794 }
dadab5e9 795 fdrop(fp, td);
984263bc
MD
796 return(error);
797}
798
799/*
800 * Allocate a file descriptor for the process.
801 */
802static int fdexpand;
803SYSCTL_INT(_debug, OID_AUTO, fdexpand, CTLFLAG_RD, &fdexpand, 0, "");
804
805int
806fdalloc(p, want, result)
807 struct proc *p;
808 int want;
809 int *result;
810{
41c20dac
MD
811 struct filedesc *fdp = p->p_fd;
812 int i;
984263bc
MD
813 int lim, last, nfiles;
814 struct file **newofile;
815 char *newofileflags;
816
817 /*
818 * Search for a free descriptor starting at the higher
819 * of want or fd_freefile. If that fails, consider
820 * expanding the ofile array.
821 */
822 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
823 for (;;) {
824 last = min(fdp->fd_nfiles, lim);
825 if ((i = want) < fdp->fd_freefile)
826 i = fdp->fd_freefile;
827 for (; i < last; i++) {
828 if (fdp->fd_ofiles[i] == NULL) {
829 fdp->fd_ofileflags[i] = 0;
830 if (i > fdp->fd_lastfile)
831 fdp->fd_lastfile = i;
832 if (want <= fdp->fd_freefile)
833 fdp->fd_freefile = i;
834 *result = i;
835 return (0);
836 }
837 }
838
839 /*
840 * No space in current array. Expand?
841 */
842 if (fdp->fd_nfiles >= lim)
843 return (EMFILE);
844 if (fdp->fd_nfiles < NDEXTENT)
845 nfiles = NDEXTENT;
846 else
847 nfiles = 2 * fdp->fd_nfiles;
848 MALLOC(newofile, struct file **, nfiles * OFILESIZE,
849 M_FILEDESC, M_WAITOK);
850
851 /*
852 * deal with file-table extend race that might have occured
853 * when malloc was blocked.
854 */
855 if (fdp->fd_nfiles >= nfiles) {
856 FREE(newofile, M_FILEDESC);
857 continue;
858 }
859 newofileflags = (char *) &newofile[nfiles];
860 /*
861 * Copy the existing ofile and ofileflags arrays
862 * and zero the new portion of each array.
863 */
864 bcopy(fdp->fd_ofiles, newofile,
865 (i = sizeof(struct file *) * fdp->fd_nfiles));
866 bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i);
867 bcopy(fdp->fd_ofileflags, newofileflags,
868 (i = sizeof(char) * fdp->fd_nfiles));
869 bzero(newofileflags + i, nfiles * sizeof(char) - i);
870 if (fdp->fd_nfiles > NDFILE)
871 FREE(fdp->fd_ofiles, M_FILEDESC);
872 fdp->fd_ofiles = newofile;
873 fdp->fd_ofileflags = newofileflags;
874 fdp->fd_nfiles = nfiles;
875 fdexpand++;
876 }
877 return (0);
878}
879
880/*
881 * Check to see whether n user file descriptors
882 * are available to the process p.
883 */
884int
885fdavail(p, n)
886 struct proc *p;
41c20dac 887 int n;
984263bc 888{
41c20dac
MD
889 struct filedesc *fdp = p->p_fd;
890 struct file **fpp;
891 int i, lim, last;
984263bc
MD
892
893 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
894 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
895 return (1);
896
897 last = min(fdp->fd_nfiles, lim);
898 fpp = &fdp->fd_ofiles[fdp->fd_freefile];
899 for (i = last - fdp->fd_freefile; --i >= 0; fpp++) {
900 if (*fpp == NULL && --n <= 0)
901 return (1);
902 }
903 return (0);
904}
905
906/*
907 * Create a new open file structure and allocate
908 * a file decriptor for the process that refers to it.
909 */
910int
911falloc(p, resultfp, resultfd)
41c20dac 912 struct proc *p;
984263bc
MD
913 struct file **resultfp;
914 int *resultfd;
915{
41c20dac 916 struct file *fp, *fq;
984263bc
MD
917 int error, i;
918
919 if (nfiles >= maxfiles) {
920 tablefull("file");
921 return (ENFILE);
922 }
923 /*
924 * Allocate a new file descriptor.
925 * If the process has file descriptor zero open, add to the list
926 * of open files at that point, otherwise put it at the front of
927 * the list of open files.
928 */
929 nfiles++;
930 MALLOC(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK);
931 bzero(fp, sizeof(struct file));
932
933 /*
934 * wait until after malloc (which may have blocked) returns before
935 * allocating the slot, else a race might have shrunk it if we had
936 * allocated it before the malloc.
937 */
938 if ((error = fdalloc(p, 0, &i))) {
939 nfiles--;
940 FREE(fp, M_FILE);
941 return (error);
942 }
943 fp->f_count = 1;
944 fp->f_cred = p->p_ucred;
945 fp->f_ops = &badfileops;
946 fp->f_seqcount = 1;
947 crhold(fp->f_cred);
948 if ((fq = p->p_fd->fd_ofiles[0])) {
949 LIST_INSERT_AFTER(fq, fp, f_list);
950 } else {
951 LIST_INSERT_HEAD(&filehead, fp, f_list);
952 }
953 p->p_fd->fd_ofiles[i] = fp;
954 if (resultfp)
955 *resultfp = fp;
956 if (resultfd)
957 *resultfd = i;
958 return (0);
959}
960
961/*
962 * Free a file descriptor.
963 */
964void
965ffree(fp)
41c20dac 966 struct file *fp;
984263bc
MD
967{
968 KASSERT((fp->f_count == 0), ("ffree: fp_fcount not 0!"));
969 LIST_REMOVE(fp, f_list);
970 crfree(fp->f_cred);
971 nfiles--;
972 FREE(fp, M_FILE);
973}
974
975/*
976 * Build a new filedesc structure.
977 */
978struct filedesc *
979fdinit(p)
980 struct proc *p;
981{
41c20dac
MD
982 struct filedesc0 *newfdp;
983 struct filedesc *fdp = p->p_fd;
984263bc
MD
984
985 MALLOC(newfdp, struct filedesc0 *, sizeof(struct filedesc0),
986 M_FILEDESC, M_WAITOK);
987 bzero(newfdp, sizeof(struct filedesc0));
988 newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
989 if (newfdp->fd_fd.fd_cdir)
990 VREF(newfdp->fd_fd.fd_cdir);
991 newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
992 if (newfdp->fd_fd.fd_rdir)
993 VREF(newfdp->fd_fd.fd_rdir);
994 newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
995 if (newfdp->fd_fd.fd_jdir)
996 VREF(newfdp->fd_fd.fd_jdir);
997
998 /* Create the file descriptor table. */
999 newfdp->fd_fd.fd_refcnt = 1;
1000 newfdp->fd_fd.fd_cmask = cmask;
1001 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1002 newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1003 newfdp->fd_fd.fd_nfiles = NDFILE;
1004 newfdp->fd_fd.fd_knlistsize = -1;
1005
1006 return (&newfdp->fd_fd);
1007}
1008
1009/*
1010 * Share a filedesc structure.
1011 */
1012struct filedesc *
1013fdshare(p)
1014 struct proc *p;
1015{
1016 p->p_fd->fd_refcnt++;
1017 return (p->p_fd);
1018}
1019
1020/*
1021 * Copy a filedesc structure.
1022 */
1023struct filedesc *
1024fdcopy(p)
1025 struct proc *p;
1026{
41c20dac
MD
1027 struct filedesc *newfdp, *fdp = p->p_fd;
1028 struct file **fpp;
1029 int i;
984263bc
MD
1030
1031 /* Certain daemons might not have file descriptors. */
1032 if (fdp == NULL)
1033 return (NULL);
1034
1035 MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0),
1036 M_FILEDESC, M_WAITOK);
1037 bcopy(fdp, newfdp, sizeof(struct filedesc));
1038 if (newfdp->fd_cdir)
1039 VREF(newfdp->fd_cdir);
1040 if (newfdp->fd_rdir)
1041 VREF(newfdp->fd_rdir);
1042 if (newfdp->fd_jdir)
1043 VREF(newfdp->fd_jdir);
1044 newfdp->fd_refcnt = 1;
1045
1046 /*
1047 * If the number of open files fits in the internal arrays
1048 * of the open file structure, use them, otherwise allocate
1049 * additional memory for the number of descriptors currently
1050 * in use.
1051 */
1052 if (newfdp->fd_lastfile < NDFILE) {
1053 newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
1054 newfdp->fd_ofileflags =
1055 ((struct filedesc0 *) newfdp)->fd_dfileflags;
1056 i = NDFILE;
1057 } else {
1058 /*
1059 * Compute the smallest multiple of NDEXTENT needed
1060 * for the file descriptors currently in use,
1061 * allowing the table to shrink.
1062 */
1063 i = newfdp->fd_nfiles;
1064 while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
1065 i /= 2;
1066 MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
1067 M_FILEDESC, M_WAITOK);
1068 newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
1069 }
1070 newfdp->fd_nfiles = i;
1071 bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **));
1072 bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char));
1073
1074 /*
1075 * kq descriptors cannot be copied.
1076 */
1077 if (newfdp->fd_knlistsize != -1) {
1078 fpp = &newfdp->fd_ofiles[newfdp->fd_lastfile];
1079 for (i = newfdp->fd_lastfile; i >= 0; i--, fpp--) {
1080 if (*fpp != NULL && (*fpp)->f_type == DTYPE_KQUEUE) {
1081 *fpp = NULL;
1082 if (i < newfdp->fd_freefile)
1083 newfdp->fd_freefile = i;
1084 }
1085 if (*fpp == NULL && i == newfdp->fd_lastfile && i > 0)
1086 newfdp->fd_lastfile--;
1087 }
1088 newfdp->fd_knlist = NULL;
1089 newfdp->fd_knlistsize = -1;
1090 newfdp->fd_knhash = NULL;
1091 newfdp->fd_knhashmask = 0;
1092 }
1093
1094 fpp = newfdp->fd_ofiles;
1095 for (i = newfdp->fd_lastfile; i-- >= 0; fpp++) {
1096 if (*fpp != NULL)
1097 fhold(*fpp);
1098 }
1099 return (newfdp);
1100}
1101
1102/*
1103 * Release a filedesc structure.
1104 */
1105void
41c20dac 1106fdfree(struct proc *p)
984263bc 1107{
dadab5e9 1108 struct thread *td = p->p_thread;
41c20dac 1109 struct filedesc *fdp = p->p_fd;
984263bc 1110 struct file **fpp;
41c20dac 1111 int i;
984263bc
MD
1112 struct filedesc_to_leader *fdtol;
1113 struct file *fp;
1114 struct vnode *vp;
1115 struct flock lf;
1116
1117 /* Certain daemons might not have file descriptors. */
1118 if (fdp == NULL)
1119 return;
1120
1121 /* Check for special need to clear POSIX style locks */
1122 fdtol = p->p_fdtol;
1123 if (fdtol != NULL) {
1124 KASSERT(fdtol->fdl_refcount > 0,
1125 ("filedesc_to_refcount botch: fdl_refcount=%d",
1126 fdtol->fdl_refcount));
1127 if (fdtol->fdl_refcount == 1 &&
1128 (p->p_leader->p_flag & P_ADVLOCK) != 0) {
1129 i = 0;
1130 fpp = fdp->fd_ofiles;
1131 for (i = 0, fpp = fdp->fd_ofiles;
1132 i < fdp->fd_lastfile;
1133 i++, fpp++) {
1134 if (*fpp == NULL ||
1135 (*fpp)->f_type != DTYPE_VNODE)
1136 continue;
1137 fp = *fpp;
1138 fhold(fp);
1139 lf.l_whence = SEEK_SET;
1140 lf.l_start = 0;
1141 lf.l_len = 0;
1142 lf.l_type = F_UNLCK;
1143 vp = (struct vnode *)fp->f_data;
1144 (void) VOP_ADVLOCK(vp,
1145 (caddr_t)p->p_leader,
1146 F_UNLCK,
1147 &lf,
1148 F_POSIX);
dadab5e9 1149 fdrop(fp, p->p_thread);
984263bc
MD
1150 fpp = fdp->fd_ofiles + i;
1151 }
1152 }
1153 retry:
1154 if (fdtol->fdl_refcount == 1) {
1155 if (fdp->fd_holdleaderscount > 0 &&
1156 (p->p_leader->p_flag & P_ADVLOCK) != 0) {
1157 /*
1158 * close() or do_dup() has cleared a reference
1159 * in a shared file descriptor table.
1160 */
1161 fdp->fd_holdleaderswakeup = 1;
1162 tsleep(&fdp->fd_holdleaderscount,
1163 PLOCK, "fdlhold", 0);
1164 goto retry;
1165 }
1166 if (fdtol->fdl_holdcount > 0) {
1167 /*
1168 * Ensure that fdtol->fdl_leader
1169 * remains valid in closef().
1170 */
1171 fdtol->fdl_wakeup = 1;
1172 tsleep(fdtol, PLOCK, "fdlhold", 0);
1173 goto retry;
1174 }
1175 }
1176 fdtol->fdl_refcount--;
1177 if (fdtol->fdl_refcount == 0 &&
1178 fdtol->fdl_holdcount == 0) {
1179 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
1180 fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
1181 } else
1182 fdtol = NULL;
1183 p->p_fdtol = NULL;
1184 if (fdtol != NULL)
1185 FREE(fdtol, M_FILEDESC_TO_LEADER);
1186 }
1187 if (--fdp->fd_refcnt > 0)
1188 return;
1189 /*
1190 * we are the last reference to the structure, we can
1191 * safely assume it will not change out from under us.
1192 */
1193 fpp = fdp->fd_ofiles;
1194 for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
1195 if (*fpp)
dadab5e9 1196 (void) closef(*fpp, td);
984263bc
MD
1197 }
1198 if (fdp->fd_nfiles > NDFILE)
1199 FREE(fdp->fd_ofiles, M_FILEDESC);
1200 if (fdp->fd_cdir)
1201 vrele(fdp->fd_cdir);
1202 if (fdp->fd_rdir)
1203 vrele(fdp->fd_rdir);
1204 if (fdp->fd_jdir)
1205 vrele(fdp->fd_jdir);
1206 if (fdp->fd_knlist)
1207 FREE(fdp->fd_knlist, M_KQUEUE);
1208 if (fdp->fd_knhash)
1209 FREE(fdp->fd_knhash, M_KQUEUE);
1210 FREE(fdp, M_FILEDESC);
1211}
1212
1213/*
1214 * For setugid programs, we don't want to people to use that setugidness
1215 * to generate error messages which write to a file which otherwise would
1216 * otherwise be off-limits to the process.
1217 *
1218 * This is a gross hack to plug the hole. A better solution would involve
1219 * a special vop or other form of generalized access control mechanism. We
1220 * go ahead and just reject all procfs file systems accesses as dangerous.
1221 *
1222 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
1223 * sufficient. We also don't for check setugidness since we know we are.
1224 */
1225static int
1226is_unsafe(struct file *fp)
1227{
1228 if (fp->f_type == DTYPE_VNODE &&
1229 ((struct vnode *)(fp->f_data))->v_tag == VT_PROCFS)
1230 return (1);
1231 return (0);
1232}
1233
1234/*
1235 * Make this setguid thing safe, if at all possible.
1236 */
1237void
dadab5e9 1238setugidsafety(struct proc *p)
984263bc 1239{
dadab5e9 1240 struct thread *td = p->p_thread;
984263bc 1241 struct filedesc *fdp = p->p_fd;
41c20dac 1242 int i;
984263bc
MD
1243
1244 /* Certain daemons might not have file descriptors. */
1245 if (fdp == NULL)
1246 return;
1247
1248 /*
1249 * note: fdp->fd_ofiles may be reallocated out from under us while
1250 * we are blocked in a close. Be careful!
1251 */
1252 for (i = 0; i <= fdp->fd_lastfile; i++) {
1253 if (i > 2)
1254 break;
1255 if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
1256 struct file *fp;
1257
1258#if 0
1259 if ((fdp->fd_ofileflags[i] & UF_MAPPED) != 0)
1260 (void) munmapfd(p, i);
1261#endif
1262 if (i < fdp->fd_knlistsize)
1263 knote_fdclose(p, i);
1264 /*
1265 * NULL-out descriptor prior to close to avoid
1266 * a race while close blocks.
1267 */
1268 fp = fdp->fd_ofiles[i];
1269 fdp->fd_ofiles[i] = NULL;
1270 fdp->fd_ofileflags[i] = 0;
1271 if (i < fdp->fd_freefile)
1272 fdp->fd_freefile = i;
dadab5e9 1273 (void) closef(fp, td);
984263bc
MD
1274 }
1275 }
1276 while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
1277 fdp->fd_lastfile--;
1278}
1279
1280/*
1281 * Close any files on exec?
1282 */
1283void
dadab5e9 1284fdcloseexec(struct proc *p)
984263bc 1285{
dadab5e9 1286 struct thread *td = p->p_thread;
984263bc 1287 struct filedesc *fdp = p->p_fd;
41c20dac 1288 int i;
984263bc
MD
1289
1290 /* Certain daemons might not have file descriptors. */
1291 if (fdp == NULL)
1292 return;
1293
1294 /*
1295 * We cannot cache fd_ofiles or fd_ofileflags since operations
1296 * may block and rip them out from under us.
1297 */
1298 for (i = 0; i <= fdp->fd_lastfile; i++) {
1299 if (fdp->fd_ofiles[i] != NULL &&
1300 (fdp->fd_ofileflags[i] & UF_EXCLOSE)) {
1301 struct file *fp;
1302
1303#if 0
1304 if (fdp->fd_ofileflags[i] & UF_MAPPED)
1305 (void) munmapfd(p, i);
1306#endif
1307 if (i < fdp->fd_knlistsize)
1308 knote_fdclose(p, i);
1309 /*
1310 * NULL-out descriptor prior to close to avoid
1311 * a race while close blocks.
1312 */
1313 fp = fdp->fd_ofiles[i];
1314 fdp->fd_ofiles[i] = NULL;
1315 fdp->fd_ofileflags[i] = 0;
1316 if (i < fdp->fd_freefile)
1317 fdp->fd_freefile = i;
dadab5e9 1318 (void) closef(fp, td);
984263bc
MD
1319 }
1320 }
1321 while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
1322 fdp->fd_lastfile--;
1323}
1324
1325/*
1326 * It is unsafe for set[ug]id processes to be started with file
1327 * descriptors 0..2 closed, as these descriptors are given implicit
1328 * significance in the Standard C library. fdcheckstd() will create a
1329 * descriptor referencing /dev/null for each of stdin, stdout, and
1330 * stderr that is not already open.
1331 */
1332int
dadab5e9 1333fdcheckstd(struct proc *p)
984263bc 1334{
dadab5e9
MD
1335 struct thread *td = p->p_thread;
1336 struct nameidata nd;
1337 struct filedesc *fdp;
1338 struct file *fp;
1339 register_t retval;
1340 int fd, i, error, flags, devnull;
984263bc
MD
1341
1342 fdp = p->p_fd;
1343 if (fdp == NULL)
1344 return (0);
1345 devnull = -1;
1346 error = 0;
1347 for (i = 0; i < 3; i++) {
1348 if (fdp->fd_ofiles[i] != NULL)
1349 continue;
1350 if (devnull < 0) {
1351 error = falloc(p, &fp, &fd);
1352 if (error != 0)
1353 break;
dadab5e9
MD
1354 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE,
1355 "/dev/null", td);
984263bc
MD
1356 flags = FREAD | FWRITE;
1357 error = vn_open(&nd, flags, 0);
1358 if (error != 0) {
1359 fdp->fd_ofiles[i] = NULL;
dadab5e9 1360 fdrop(fp, td);
984263bc
MD
1361 break;
1362 }
1363 NDFREE(&nd, NDF_ONLY_PNBUF);
1364 fp->f_data = (caddr_t)nd.ni_vp;
1365 fp->f_flag = flags;
1366 fp->f_ops = &vnops;
1367 fp->f_type = DTYPE_VNODE;
dadab5e9 1368 VOP_UNLOCK(nd.ni_vp, 0, td);
984263bc
MD
1369 devnull = fd;
1370 } else {
1371 error = fdalloc(p, 0, &fd);
1372 if (error != 0)
1373 break;
1374 error = do_dup(fdp, devnull, fd, &retval, p);
1375 if (error != 0)
1376 break;
1377 }
1378 }
1379 return (error);
1380}
1381
1382/*
1383 * Internal form of close.
1384 * Decrement reference count on file structure.
dadab5e9 1385 * Note: td and/or p may be NULL when closing a file
984263bc
MD
1386 * that was being passed in a message.
1387 */
1388int
dadab5e9 1389closef(struct file *fp, struct thread *td)
984263bc
MD
1390{
1391 struct vnode *vp;
1392 struct flock lf;
1393 struct filedesc_to_leader *fdtol;
dadab5e9 1394 struct proc *p;
984263bc
MD
1395
1396 if (fp == NULL)
1397 return (0);
dadab5e9
MD
1398 if (td == NULL) {
1399 td = curthread;
1400 p = NULL; /* allow no proc association */
1401 } else {
1402 p = td->td_proc; /* can also be NULL */
1403 }
984263bc
MD
1404 /*
1405 * POSIX record locking dictates that any close releases ALL
1406 * locks owned by this process. This is handled by setting
1407 * a flag in the unlock to free ONLY locks obeying POSIX
1408 * semantics, and not to free BSD-style file locks.
1409 * If the descriptor was in a message, POSIX-style locks
1410 * aren't passed with the descriptor.
1411 */
1412 if (p != NULL &&
1413 fp->f_type == DTYPE_VNODE) {
1414 if ((p->p_leader->p_flag & P_ADVLOCK) != 0) {
1415 lf.l_whence = SEEK_SET;
1416 lf.l_start = 0;
1417 lf.l_len = 0;
1418 lf.l_type = F_UNLCK;
1419 vp = (struct vnode *)fp->f_data;
1420 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
1421 &lf, F_POSIX);
1422 }
1423 fdtol = p->p_fdtol;
1424 if (fdtol != NULL) {
1425 /*
1426 * Handle special case where file descriptor table
1427 * is shared between multiple process leaders.
1428 */
1429 for (fdtol = fdtol->fdl_next;
1430 fdtol != p->p_fdtol;
1431 fdtol = fdtol->fdl_next) {
1432 if ((fdtol->fdl_leader->p_flag &
1433 P_ADVLOCK) == 0)
1434 continue;
1435 fdtol->fdl_holdcount++;
1436 lf.l_whence = SEEK_SET;
1437 lf.l_start = 0;
1438 lf.l_len = 0;
1439 lf.l_type = F_UNLCK;
1440 vp = (struct vnode *)fp->f_data;
1441 (void) VOP_ADVLOCK(vp,
1442 (caddr_t)p->p_leader,
1443 F_UNLCK, &lf, F_POSIX);
1444 fdtol->fdl_holdcount--;
1445 if (fdtol->fdl_holdcount == 0 &&
1446 fdtol->fdl_wakeup != 0) {
1447 fdtol->fdl_wakeup = 0;
1448 wakeup(fdtol);
1449 }
1450 }
1451 }
1452 }
dadab5e9 1453 return (fdrop(fp, td));
984263bc
MD
1454}
1455
1456int
dadab5e9 1457fdrop(struct file *fp, struct thread *td)
984263bc
MD
1458{
1459 struct flock lf;
1460 struct vnode *vp;
1461 int error;
1462
1463 if (--fp->f_count > 0)
1464 return (0);
1465 if (fp->f_count < 0)
1466 panic("fdrop: count < 0");
1467 if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
1468 lf.l_whence = SEEK_SET;
1469 lf.l_start = 0;
1470 lf.l_len = 0;
1471 lf.l_type = F_UNLCK;
1472 vp = (struct vnode *)fp->f_data;
1473 (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1474 }
1475 if (fp->f_ops != &badfileops)
dadab5e9 1476 error = fo_close(fp, td);
984263bc
MD
1477 else
1478 error = 0;
1479 ffree(fp);
1480 return (error);
1481}
1482
1483/*
1484 * Apply an advisory lock on a file descriptor.
1485 *
1486 * Just attempt to get a record lock of the requested type on
1487 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1488 */
1489#ifndef _SYS_SYSPROTO_H_
1490struct flock_args {
1491 int fd;
1492 int how;
1493};
1494#endif
1495/* ARGSUSED */
1496int
41c20dac 1497flock(struct flock_args *uap)
984263bc 1498{
41c20dac
MD
1499 struct proc *p = curproc;
1500 struct filedesc *fdp = p->p_fd;
1501 struct file *fp;
984263bc
MD
1502 struct vnode *vp;
1503 struct flock lf;
1504
1505 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
1506 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
1507 return (EBADF);
1508 if (fp->f_type != DTYPE_VNODE)
1509 return (EOPNOTSUPP);
1510 vp = (struct vnode *)fp->f_data;
1511 lf.l_whence = SEEK_SET;
1512 lf.l_start = 0;
1513 lf.l_len = 0;
1514 if (uap->how & LOCK_UN) {
1515 lf.l_type = F_UNLCK;
1516 fp->f_flag &= ~FHASLOCK;
1517 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
1518 }
1519 if (uap->how & LOCK_EX)
1520 lf.l_type = F_WRLCK;
1521 else if (uap->how & LOCK_SH)
1522 lf.l_type = F_RDLCK;
1523 else
1524 return (EBADF);
1525 fp->f_flag |= FHASLOCK;
1526 if (uap->how & LOCK_NB)
1527 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
1528 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
1529}
1530
1531/*
1532 * File Descriptor pseudo-device driver (/dev/fd/).
1533 *
1534 * Opening minor device N dup()s the file (if any) connected to file
1535 * descriptor N belonging to the calling process. Note that this driver
1536 * consists of only the ``open()'' routine, because all subsequent
1537 * references to this file will be direct to the other driver.
1538 */
1539/* ARGSUSED */
1540static int
41c20dac 1541fdopen(dev_t dev, int mode, int type, struct thread *td)
984263bc 1542{
41c20dac 1543 KKASSERT(td->td_proc != NULL);
984263bc
MD
1544
1545 /*
1546 * XXX Kludge: set curproc->p_dupfd to contain the value of the
1547 * the file descriptor being sought for duplication. The error
1548 * return ensures that the vnode for this device will be released
1549 * by vn_open. Open will detect this special error and take the
1550 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1551 * will simply report the error.
1552 */
41c20dac 1553 td->td_proc->p_dupfd = minor(dev);
984263bc
MD
1554 return (ENODEV);
1555}
1556
1557/*
1558 * Duplicate the specified descriptor to a free descriptor.
1559 */
1560int
41c20dac 1561dupfdopen(struct filedesc *fdp, int indx, int dfd, int mode, int error)
984263bc 1562{
41c20dac 1563 struct file *wfp;
984263bc
MD
1564 struct file *fp;
1565
1566 /*
1567 * If the to-be-dup'd fd number is greater than the allowed number
1568 * of file descriptors, or the fd to be dup'd has already been
1569 * closed, then reject.
1570 */
1571 if ((u_int)dfd >= fdp->fd_nfiles ||
1572 (wfp = fdp->fd_ofiles[dfd]) == NULL) {
1573 return (EBADF);
1574 }
1575
1576 /*
1577 * There are two cases of interest here.
1578 *
1579 * For ENODEV simply dup (dfd) to file descriptor
1580 * (indx) and return.
1581 *
1582 * For ENXIO steal away the file structure from (dfd) and
1583 * store it in (indx). (dfd) is effectively closed by
1584 * this operation.
1585 *
1586 * Any other error code is just returned.
1587 */
1588 switch (error) {
1589 case ENODEV:
1590 /*
1591 * Check that the mode the file is being opened for is a
1592 * subset of the mode of the existing descriptor.
1593 */
1594 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
1595 return (EACCES);
1596 fp = fdp->fd_ofiles[indx];
1597#if 0
1598 if (fp && fdp->fd_ofileflags[indx] & UF_MAPPED)
1599 (void) munmapfd(p, indx);
1600#endif
1601 fdp->fd_ofiles[indx] = wfp;
1602 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1603 fhold(wfp);
1604 if (indx > fdp->fd_lastfile)
1605 fdp->fd_lastfile = indx;
1606 /*
1607 * we now own the reference to fp that the ofiles[] array
1608 * used to own. Release it.
1609 */
1610 if (fp)
dadab5e9 1611 fdrop(fp, curthread);
984263bc
MD
1612 return (0);
1613
1614 case ENXIO:
1615 /*
1616 * Steal away the file pointer from dfd, and stuff it into indx.
1617 */
1618 fp = fdp->fd_ofiles[indx];
1619#if 0
1620 if (fp && fdp->fd_ofileflags[indx] & UF_MAPPED)
1621 (void) munmapfd(p, indx);
1622#endif
1623 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
1624 fdp->fd_ofiles[dfd] = NULL;
1625 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1626 fdp->fd_ofileflags[dfd] = 0;
1627
1628 /*
1629 * we now own the reference to fp that the ofiles[] array
1630 * used to own. Release it.
1631 */
1632 if (fp)
dadab5e9 1633 fdrop(fp, curthread);
984263bc
MD
1634 /*
1635 * Complete the clean up of the filedesc structure by
1636 * recomputing the various hints.
1637 */
1638 if (indx > fdp->fd_lastfile) {
1639 fdp->fd_lastfile = indx;
1640 } else {
1641 while (fdp->fd_lastfile > 0 &&
1642 fdp->fd_ofiles[fdp->fd_lastfile] == NULL) {
1643 fdp->fd_lastfile--;
1644 }
1645 if (dfd < fdp->fd_freefile)
1646 fdp->fd_freefile = dfd;
1647 }
1648 return (0);
1649
1650 default:
1651 return (error);
1652 }
1653 /* NOTREACHED */
1654}
1655
1656
1657struct filedesc_to_leader *
1658filedesc_to_leader_alloc(struct filedesc_to_leader *old,
1659 struct proc *leader)
1660{
1661 struct filedesc_to_leader *fdtol;
1662
1663 MALLOC(fdtol, struct filedesc_to_leader *,
1664 sizeof(struct filedesc_to_leader),
1665 M_FILEDESC_TO_LEADER,
1666 M_WAITOK);
1667 fdtol->fdl_refcount = 1;
1668 fdtol->fdl_holdcount = 0;
1669 fdtol->fdl_wakeup = 0;
1670 fdtol->fdl_leader = leader;
1671 if (old != NULL) {
1672 fdtol->fdl_next = old->fdl_next;
1673 fdtol->fdl_prev = old;
1674 old->fdl_next = fdtol;
1675 fdtol->fdl_next->fdl_prev = fdtol;
1676 } else {
1677 fdtol->fdl_next = fdtol;
1678 fdtol->fdl_prev = fdtol;
1679 }
1680 return fdtol;
1681}
1682
1683/*
1684 * Get file structures.
1685 */
1686static int
1687sysctl_kern_file(SYSCTL_HANDLER_ARGS)
1688{
1689 int error;
1690 struct file *fp;
1691
1692 if (!req->oldptr) {
1693 /*
1694 * overestimate by 10 files
1695 */
1696 return (SYSCTL_OUT(req, 0, sizeof(filehead) +
1697 (nfiles + 10) * sizeof(struct file)));
1698 }
1699
1700 error = SYSCTL_OUT(req, (caddr_t)&filehead, sizeof(filehead));
1701 if (error)
1702 return (error);
1703
1704 /*
1705 * followed by an array of file structures
1706 */
1707 LIST_FOREACH(fp, &filehead, f_list) {
1708 error = SYSCTL_OUT(req, (caddr_t)fp, sizeof (struct file));
1709 if (error)
1710 return (error);
1711 }
1712 return (0);
1713}
1714
1715SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
1716 0, 0, sysctl_kern_file, "S,file", "Entire file table");
1717
1718SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
1719 &maxfilesperproc, 0, "Maximum files allowed open per process");
1720
1721SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
1722 &maxfiles, 0, "Maximum number of files");
1723
1724SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
1725 &nfiles, 0, "System-wide number of open files");
1726
1727static void
1728fildesc_drvinit(void *unused)
1729{
1730 int fd;
1731
1732 for (fd = 0; fd < NUMFDESC; fd++)
1733 make_dev(&fildesc_cdevsw, fd,
1734 UID_BIN, GID_BIN, 0666, "fd/%d", fd);
1735 make_dev(&fildesc_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "stdin");
1736 make_dev(&fildesc_cdevsw, 1, UID_ROOT, GID_WHEEL, 0666, "stdout");
1737 make_dev(&fildesc_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "stderr");
1738}
1739
1740struct fileops badfileops = {
1741 badfo_readwrite,
1742 badfo_readwrite,
1743 badfo_ioctl,
1744 badfo_poll,
1745 badfo_kqfilter,
1746 badfo_stat,
1747 badfo_close
1748};
1749
1750static int
dadab5e9
MD
1751badfo_readwrite(
1752 struct file *fp,
1753 struct uio *uio,
1754 struct ucred *cred,
1755 int flags,
1756 struct thread *td
1757) {
984263bc
MD
1758 return (EBADF);
1759}
1760
1761static int
dadab5e9 1762badfo_ioctl(struct file *fp, u_long com, caddr_t data, struct thread *td)
984263bc 1763{
984263bc
MD
1764 return (EBADF);
1765}
1766
1767static int
dadab5e9 1768badfo_poll(struct file *fp, int events, struct ucred *cred, struct thread *td)
984263bc 1769{
984263bc
MD
1770 return (0);
1771}
1772
1773static int
dadab5e9 1774badfo_kqfilter(struct file *fp, struct knote *kn)
984263bc 1775{
984263bc
MD
1776 return (0);
1777}
1778
1779static int
dadab5e9 1780badfo_stat(struct file *fp, struct stat *sb, struct thread *td)
984263bc 1781{
984263bc
MD
1782 return (EBADF);
1783}
1784
1785static int
dadab5e9 1786badfo_close(struct file *fp, struct thread *td)
984263bc 1787{
984263bc
MD
1788 return (EBADF);
1789}
1790
1791SYSINIT(fildescdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,
1792 fildesc_drvinit,NULL)