]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_descrip.c
0b8a16e32f99abd07cc1e122441a46872f9b7379
[apple/xnu.git] / bsd / kern / kern_descrip.c
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
23 /*
24 * Copyright (c) 1982, 1986, 1989, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
61 */
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/filedesc.h>
66 #include <sys/kernel.h>
67 #include <sys/vnode.h>
68 #include <sys/proc.h>
69 #include <sys/file.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/stat.h>
73 #include <sys/ioctl.h>
74 #include <sys/fcntl.h>
75 #include <sys/malloc.h>
76 #include <sys/syslog.h>
77 #include <sys/unistd.h>
78 #include <sys/resourcevar.h>
79
80 #include <sys/mount.h>
81
82 /*
83 * Descriptor management.
84 */
85 struct filelist filehead; /* head of list of open files */
86 int nfiles; /* actual number of open files */
87
88 static int frele_internal(struct file *);
89
90 /*
91 * System calls on descriptors.
92 */
93 /* ARGSUSED */
94 int
95 getdtablesize(p, uap, retval)
96 struct proc *p;
97 void *uap;
98 register_t *retval;
99 {
100 *retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
101 return (0);
102 }
103
104 /* ARGSUSED */
105 int
106 ogetdtablesize(p, uap, retval)
107 struct proc *p;
108 void *uap;
109 register_t *retval;
110 {
111 *retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, NOFILE);
112 return (0);
113 }
114
115 static __inline__
116 void _fdrelse(fdp, fd)
117 register struct filedesc *fdp;
118 register int fd;
119 {
120 if (fd < fdp->fd_freefile)
121 fdp->fd_freefile = fd;
122 #if DIAGNOSTIC
123 if (fd > fdp->fd_lastfile)
124 panic("fdrelse: fd_lastfile inconsistent");
125 #endif
126 fdp->fd_ofiles[fd] = NULL;
127 fdp->fd_ofileflags[fd] = 0;
128 while ((fd = fdp->fd_lastfile) > 0 &&
129 fdp->fd_ofiles[fd] == NULL &&
130 !(fdp->fd_ofileflags[fd] & UF_RESERVED))
131 fdp->fd_lastfile--;
132 }
133
134 /*
135 * Duplicate a file descriptor.
136 */
137 struct dup_args {
138 u_int fd;
139 };
140 /* ARGSUSED */
141 int
142 dup(p, uap, retval)
143 struct proc *p;
144 struct dup_args *uap;
145 register_t *retval;
146 {
147 register struct filedesc *fdp = p->p_fd;
148 register int old = uap->fd;
149 int new, error;
150
151 if ((u_int)old >= fdp->fd_nfiles ||
152 fdp->fd_ofiles[old] == NULL ||
153 (fdp->fd_ofileflags[old] & UF_RESERVED))
154 return (EBADF);
155 if (error = fdalloc(p, 0, &new))
156 return (error);
157 return (finishdup(fdp, old, new, retval));
158 }
159
160 /*
161 * Duplicate a file descriptor to a particular value.
162 */
163 struct dup2_args {
164 u_int from;
165 u_int to;
166 };
167 /* ARGSUSED */
168 int
169 dup2(p, uap, retval)
170 struct proc *p;
171 struct dup2_args *uap;
172 register_t *retval;
173 {
174 register struct filedesc *fdp = p->p_fd;
175 register int old = uap->from, new = uap->to;
176 int i, error;
177
178 if ((u_int)old >= fdp->fd_nfiles ||
179 fdp->fd_ofiles[old] == NULL ||
180 (fdp->fd_ofileflags[old] & UF_RESERVED) ||
181 (u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
182 (u_int)new >= maxfiles)
183 return (EBADF);
184 if (old == new) {
185 *retval = new;
186 return (0);
187 }
188 if ((u_int)new >= fdp->fd_nfiles) {
189 if (error = fdalloc(p, new, &i))
190 return (error);
191 if (new != i) {
192 _fdrelse(fdp, i);
193 goto closeit;
194 }
195 } else {
196 struct file **fpp;
197 char flags;
198 closeit:
199 if ((flags = fdp->fd_ofileflags[new]) & UF_RESERVED)
200 return (EBADF);
201 fdp->fd_ofileflags[new] = (flags & ~UF_MAPPED) | UF_RESERVED;
202 /*
203 * dup2() must succeed even if the close has an error.
204 */
205 if (*(fpp = &fdp->fd_ofiles[new])) {
206 struct file *fp = *fpp;
207
208 *fpp = NULL;
209 (void) closef(fp, p);
210 }
211 }
212 return (finishdup(fdp, old, new, retval));
213 }
214
215 /*
216 * The file control system call.
217 */
218 struct fcntl_args {
219 int fd;
220 int cmd;
221 int arg;
222 };
223 /* ARGSUSED */
224 int
225 fcntl(p, uap, retval)
226 struct proc *p;
227 register struct fcntl_args *uap;
228 register_t *retval;
229 {
230 int fd = uap->fd;
231 register struct filedesc *fdp = p->p_fd;
232 register struct file *fp;
233 register char *pop;
234 struct vnode *vp, *devvp;
235 int i, tmp, error, error2, flg = F_POSIX;
236 struct flock fl;
237 fstore_t alloc_struct; /* structure for allocate command */
238 u_int32_t alloc_flags = 0;
239 off_t offset; /* used for F_SETSIZE */
240 int newmin;
241 struct radvisory ra_struct;
242 fbootstraptransfer_t fbt_struct; /* for F_READBOOTSTRAP and F_WRITEBOOTSTRAP */
243 struct log2phys l2p_struct; /* structure for allocate command */
244 daddr_t lbn, bn;
245 int devBlockSize = 0;
246
247 if ((u_int)fd >= fdp->fd_nfiles ||
248 (fp = fdp->fd_ofiles[fd]) == NULL ||
249 (fdp->fd_ofileflags[fd] & UF_RESERVED))
250 return (EBADF);
251 pop = &fdp->fd_ofileflags[fd];
252 switch (uap->cmd) {
253
254 case F_DUPFD:
255 newmin = (long)uap->arg;
256 if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
257 (u_int)newmin >= maxfiles)
258 return (EINVAL);
259 if (error = fdalloc(p, newmin, &i))
260 return (error);
261 return (finishdup(fdp, fd, i, retval));
262
263 case F_GETFD:
264 *retval = (*pop & UF_EXCLOSE)? 1 : 0;
265 return (0);
266
267 case F_SETFD:
268 *pop = (*pop &~ UF_EXCLOSE) |
269 ((long)(uap->arg) & 1)? UF_EXCLOSE : 0;
270 return (0);
271
272 case F_GETFL:
273 *retval = OFLAGS(fp->f_flag);
274 return (0);
275
276 case F_SETFL:
277 fp->f_flag &= ~FCNTLFLAGS;
278 fp->f_flag |= FFLAGS((long)uap->arg) & FCNTLFLAGS;
279 tmp = fp->f_flag & FNONBLOCK;
280 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
281 if (error)
282 return (error);
283 tmp = fp->f_flag & FASYNC;
284 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, p);
285 if (!error)
286 return (0);
287 fp->f_flag &= ~FNONBLOCK;
288 tmp = 0;
289 (void)fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
290 return (error);
291
292 case F_GETOWN:
293 if (fp->f_type == DTYPE_SOCKET) {
294 *retval = ((struct socket *)fp->f_data)->so_pgid;
295 return (0);
296 }
297 error = fo_ioctl(fp, (int)TIOCGPGRP, (caddr_t)retval, p);
298 *retval = -*retval;
299 return (error);
300
301 case F_SETOWN:
302 if (fp->f_type == DTYPE_SOCKET) {
303 ((struct socket *)fp->f_data)->so_pgid =
304 (long)uap->arg;
305 return (0);
306 }
307 if ((long)uap->arg <= 0) {
308 uap->arg = (void *)(-(long)(uap->arg));
309 } else {
310 struct proc *p1 = pfind((long)uap->arg);
311 if (p1 == 0)
312 return (ESRCH);
313 uap->arg = (void *)(long)p1->p_pgrp->pg_id;
314 }
315 return (fo_ioctl(fp, (int)TIOCSPGRP, (caddr_t)&uap->arg, p));
316
317 case F_SETLKW:
318 flg |= F_WAIT;
319 /* Fall into F_SETLK */
320
321 case F_SETLK:
322 if (fp->f_type != DTYPE_VNODE)
323 return (EBADF);
324 vp = (struct vnode *)fp->f_data;
325 /* Copy in the lock structure */
326 error = copyin((caddr_t)uap->arg, (caddr_t)&fl,
327 sizeof (fl));
328 if (error)
329 return (error);
330 if (fl.l_whence == SEEK_CUR)
331 fl.l_start += fp->f_offset;
332 switch (fl.l_type) {
333
334 case F_RDLCK:
335 if ((fp->f_flag & FREAD) == 0)
336 return (EBADF);
337 p->p_flag |= P_ADVLOCK;
338 return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
339
340 case F_WRLCK:
341 if ((fp->f_flag & FWRITE) == 0)
342 return (EBADF);
343 p->p_flag |= P_ADVLOCK;
344 return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
345
346 case F_UNLCK:
347 return (VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl,
348 F_POSIX));
349
350 default:
351 return (EINVAL);
352 }
353
354 case F_GETLK:
355 if (fp->f_type != DTYPE_VNODE)
356 return (EBADF);
357 vp = (struct vnode *)fp->f_data;
358 /* Copy in the lock structure */
359 error = copyin((caddr_t)uap->arg, (caddr_t)&fl,
360 sizeof (fl));
361 if (error)
362 return (error);
363 if (fl.l_whence == SEEK_CUR)
364 fl.l_start += fp->f_offset;
365 if (error = VOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX))
366 return (error);
367 return (copyout((caddr_t)&fl, (caddr_t)uap->arg,
368 sizeof (fl)));
369
370 case F_PREALLOCATE:
371 if (fp->f_type != DTYPE_VNODE)
372 return (EBADF);
373
374 /* make sure that we have write permission */
375 if ((fp->f_flag & FWRITE) == 0)
376 return (EBADF);
377
378 error = copyin((caddr_t)uap->arg, (caddr_t)&alloc_struct,
379 sizeof (alloc_struct));
380 if (error)
381 return (error);
382
383 /* now set the space allocated to 0 */
384 alloc_struct.fst_bytesalloc = 0;
385
386 /*
387 * Do some simple parameter checking
388 */
389
390 /* set up the flags */
391
392 alloc_flags |= PREALLOCATE;
393
394 if (alloc_struct.fst_flags & F_ALLOCATECONTIG)
395 alloc_flags |= ALLOCATECONTIG;
396
397 if (alloc_struct.fst_flags & F_ALLOCATEALL)
398 alloc_flags |= ALLOCATEALL;
399
400 /*
401 * Do any position mode specific stuff. The only
402 * position mode supported now is PEOFPOSMODE
403 */
404
405 switch (alloc_struct.fst_posmode) {
406
407 case F_PEOFPOSMODE:
408 if (alloc_struct.fst_offset != 0)
409 return (EINVAL);
410
411 alloc_flags |= ALLOCATEFROMPEOF;
412 break;
413
414 case F_VOLPOSMODE:
415 if (alloc_struct.fst_offset <= 0)
416 return (EINVAL);
417
418 alloc_flags |= ALLOCATEFROMVOL;
419 break;
420
421 default:
422 return(EINVAL);
423 }
424
425 vp = (struct vnode *)fp->f_data;
426
427 /* lock the vnode and call allocate to get the space */
428 error = vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, p);
429 if (error)
430 return (error);
431 error = VOP_ALLOCATE(vp,alloc_struct.fst_length,alloc_flags,
432 &alloc_struct.fst_bytesalloc, alloc_struct.fst_offset,
433 fp->f_cred, p);
434 VOP_UNLOCK(vp, 0, p);
435
436 if (error2 = copyout((caddr_t)&alloc_struct,
437 (caddr_t)uap->arg,
438 sizeof (alloc_struct))) {
439 if (error)
440 return(error);
441 else
442 return(error2);
443 }
444 return(error);
445
446 case F_SETSIZE:
447 if (fp->f_type != DTYPE_VNODE)
448 return (EBADF);
449
450 error = copyin((caddr_t)uap->arg, (caddr_t)&offset,
451 sizeof (off_t));
452 if (error)
453 return (error);
454
455 /*
456 * Make sure that we are root. Growing a file
457 * without zero filling the data is a security hole
458 * root would have access anyway so we'll allow it
459 */
460
461 if (!is_suser())
462 return (EACCES);
463
464 vp = (struct vnode *)fp->f_data;
465
466 /* lock the vnode and call allocate to get the space */
467 error = vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, p);
468 if (error)
469 return (error);
470 error = VOP_TRUNCATE(vp,offset,IO_NOZEROFILL,fp->f_cred,p);
471 VOP_UNLOCK(vp,0,p);
472 return(error);
473
474 case F_RDAHEAD:
475 if (fp->f_type != DTYPE_VNODE)
476 return (EBADF);
477 vp = (struct vnode *)fp->f_data;
478
479 simple_lock(&vp->v_interlock);
480 if (uap->arg)
481 vp->v_flag &= ~VRAOFF;
482 else
483 vp->v_flag |= VRAOFF;
484 simple_unlock(&vp->v_interlock);
485 return (0);
486
487 case F_NOCACHE:
488 if (fp->f_type != DTYPE_VNODE)
489 return (EBADF);
490 vp = (struct vnode *)fp->f_data;
491
492 simple_lock(&vp->v_interlock);
493 if (uap->arg)
494 vp->v_flag |= VNOCACHE_DATA;
495 else
496 vp->v_flag &= ~VNOCACHE_DATA;
497 simple_unlock(&vp->v_interlock);
498 return (0);
499
500 case F_RDADVISE:
501 if (fp->f_type != DTYPE_VNODE)
502 return (EBADF);
503 vp = (struct vnode *)fp->f_data;
504
505 if (error = copyin((caddr_t)uap->arg,
506 (caddr_t)&ra_struct, sizeof (ra_struct)))
507 return(error);
508 return (VOP_IOCTL(vp, 1, &ra_struct, 0, fp->f_cred, p));
509
510 case F_READBOOTSTRAP:
511 case F_WRITEBOOTSTRAP:
512 if (fp->f_type != DTYPE_VNODE)
513 return (EBADF);
514
515 error = copyin((caddr_t)uap->arg, (caddr_t)&fbt_struct,
516 sizeof (fbt_struct));
517 if (error)
518 return (error);
519
520 if (uap->cmd == F_WRITEBOOTSTRAP) {
521 /*
522 * Make sure that we are root. Updating the
523 * bootstrap on a disk could be a security hole
524 */
525 if (!is_suser())
526 return (EACCES);
527 }
528
529 vp = (struct vnode *)fp->f_data;
530 if (vp->v_tag != VT_HFS) /* XXX */
531 error = EINVAL;
532 else {
533 /* lock the vnode and call VOP_IOCTL to handle the I/O */
534 error = vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, p);
535 if (error)
536 return (error);
537 error = VOP_IOCTL(vp, (uap->cmd == F_WRITEBOOTSTRAP) ? 3 : 2,
538 &fbt_struct, 0, fp->f_cred, p);
539 VOP_UNLOCK(vp,0,p);
540 }
541 return(error);
542
543 case F_LOG2PHYS:
544 if (fp->f_type != DTYPE_VNODE)
545 return (EBADF);
546 vp = (struct vnode *)fp->f_data;
547 error = vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, p);
548 if (error)
549 return (error);
550 if (VOP_OFFTOBLK(vp, fp->f_offset, &lbn))
551 panic("fcntl LOG2PHYS OFFTOBLK");
552 if (VOP_BLKTOOFF(vp, lbn, &offset))
553 panic("fcntl LOG2PHYS BLKTOOFF1");
554 error = VOP_BMAP(vp, lbn, &devvp, &bn, 0);
555 VOP_DEVBLOCKSIZE(devvp, &devBlockSize);
556 VOP_UNLOCK(vp, 0, p);
557 if (!error) {
558 l2p_struct.l2p_flags = 0; /* for now */
559 l2p_struct.l2p_contigbytes = 0; /* for now */
560 l2p_struct.l2p_devoffset = bn * devBlockSize;
561 l2p_struct.l2p_devoffset += fp->f_offset - offset;
562 error = copyout((caddr_t)&l2p_struct,
563 (caddr_t)uap->arg,
564 sizeof (l2p_struct));
565 }
566 return (error);
567
568 default:
569 return (EINVAL);
570 }
571 /* NOTREACHED */
572 }
573
574 /*
575 * Common code for dup, dup2, and fcntl(F_DUPFD).
576 */
577 int
578 finishdup(fdp, old, new, retval)
579 register struct filedesc *fdp;
580 register int old, new;
581 register_t *retval;
582 {
583 register struct file *fp;
584
585 if ((fp = fdp->fd_ofiles[old]) == NULL ||
586 (fdp->fd_ofileflags[old] & UF_RESERVED)) {
587 _fdrelse(fdp, new);
588 return (EBADF);
589 }
590 fdp->fd_ofiles[new] = fp;
591 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
592 (void)fref(fp);
593 if (new > fdp->fd_lastfile)
594 fdp->fd_lastfile = new;
595 *retval = new;
596 return (0);
597 }
598
599 /*
600 * Close a file descriptor.
601 */
602 struct close_args {
603 int fd;
604 };
605 /* ARGSUSED */
606 int
607 close(p, uap, retval)
608 struct proc *p;
609 struct close_args *uap;
610 register_t *retval;
611 {
612 int fd = uap->fd;
613 register struct filedesc *fdp = p->p_fd;
614 register struct file *fp;
615
616 if ((u_int)fd >= fdp->fd_nfiles ||
617 (fp = fdp->fd_ofiles[fd]) == NULL ||
618 (fdp->fd_ofileflags[fd] & UF_RESERVED))
619 return (EBADF);
620 _fdrelse(fdp, fd);
621 return (closef(fp, p));
622 }
623
624 /*
625 * Return status information about a file descriptor.
626 */
627 struct fstat_args {
628 int fd;
629 struct stat *sb;
630 };
631 /* ARGSUSED */
632 int
633 fstat(p, uap, retval)
634 struct proc *p;
635 register struct fstat_args *uap;
636 register_t *retval;
637 {
638 int fd = uap->fd;
639 register struct filedesc *fdp = p->p_fd;
640 register struct file *fp;
641 struct stat ub;
642 int error;
643
644 if ((u_int)fd >= fdp->fd_nfiles ||
645 (fp = fdp->fd_ofiles[fd]) == NULL ||
646 (fdp->fd_ofileflags[fd] & UF_RESERVED))
647 return (EBADF);
648 switch (fp->f_type) {
649
650 case DTYPE_VNODE:
651 error = vn_stat((struct vnode *)fp->f_data, &ub, p);
652 break;
653
654 case DTYPE_SOCKET:
655 error = soo_stat((struct socket *)fp->f_data, &ub);
656 break;
657
658 case DTYPE_PSXSHM:
659 error = pshm_stat((void *)fp->f_data, &ub);
660 break;
661 default:
662 panic("fstat");
663 /*NOTREACHED*/
664 }
665 if (error == 0)
666 error = copyout((caddr_t)&ub, (caddr_t)uap->sb,
667 sizeof (ub));
668 return (error);
669 }
670
671 #if COMPAT_43
672 /*
673 * Return status information about a file descriptor.
674 */
675 struct ofstat_args {
676 int fd;
677 struct ostat *sb;
678 };
679 /* ARGSUSED */
680 ofstat(p, uap, retval)
681 struct proc *p;
682 register struct ofstat_args *uap;
683 register_t *retval;
684 {
685 int fd = uap->fd;
686 register struct filedesc *fdp = p->p_fd;
687 register struct file *fp;
688 struct stat ub;
689 struct ostat oub;
690 int error;
691
692 if ((u_int)fd >= fdp->fd_nfiles ||
693 (fp = fdp->fd_ofiles[fd]) == NULL ||
694 (fdp->fd_ofileflags[fd] & UF_RESERVED))
695 return (EBADF);
696 switch (fp->f_type) {
697
698 case DTYPE_VNODE:
699 error = vn_stat((struct vnode *)fp->f_data, &ub, p);
700 break;
701
702 case DTYPE_SOCKET:
703 error = soo_stat((struct socket *)fp->f_data, &ub);
704 break;
705
706 default:
707 panic("ofstat");
708 /*NOTREACHED*/
709 }
710 cvtstat(&ub, &oub);
711 if (error == 0)
712 error = copyout((caddr_t)&oub, (caddr_t)uap->sb,
713 sizeof (oub));
714 return (error);
715 }
716 #endif /* COMPAT_43 */
717
718 /*
719 * Return pathconf information about a file descriptor.
720 */
721 struct fpathconf_args {
722 int fd;
723 int name;
724 };
725 /* ARGSUSED */
726 fpathconf(p, uap, retval)
727 struct proc *p;
728 register struct fpathconf_args *uap;
729 register_t *retval;
730 {
731 int fd = uap->fd;
732 struct filedesc *fdp = p->p_fd;
733 struct file *fp;
734 struct vnode *vp;
735
736 if ((u_int)fd >= fdp->fd_nfiles ||
737 (fp = fdp->fd_ofiles[fd]) == NULL ||
738 (fdp->fd_ofileflags[fd] & UF_RESERVED))
739 return (EBADF);
740 switch (fp->f_type) {
741
742 case DTYPE_SOCKET:
743 if (uap->name != _PC_PIPE_BUF)
744 return (EINVAL);
745 *retval = PIPE_BUF;
746 return (0);
747
748 case DTYPE_VNODE:
749 vp = (struct vnode *)fp->f_data;
750 return (VOP_PATHCONF(vp, uap->name, retval));
751
752 default:
753 panic("fpathconf");
754 }
755 /*NOTREACHED*/
756 }
757
758 /*
759 * Allocate a file descriptor for the process.
760 */
761 int fdexpand;
762
763 int
764 fdalloc(p, want, result)
765 struct proc *p;
766 int want;
767 int *result;
768 {
769 register struct filedesc *fdp = p->p_fd;
770 register int i;
771 int lim, last, nfiles, oldnfiles;
772 struct file **newofiles, **ofiles;
773 char *newofileflags, *ofileflags;
774
775 /*
776 * Search for a free descriptor starting at the higher
777 * of want or fd_freefile. If that fails, consider
778 * expanding the ofile array.
779 */
780 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
781 for (;;) {
782 last = min(fdp->fd_nfiles, lim);
783 if ((i = want) < fdp->fd_freefile)
784 i = fdp->fd_freefile;
785 ofiles = &fdp->fd_ofiles[i];
786 ofileflags = &fdp->fd_ofileflags[i];
787 for (; i < last; i++) {
788 if (*ofiles == NULL && !(*ofileflags & UF_RESERVED)) {
789 *ofileflags = UF_RESERVED;
790 if (i > fdp->fd_lastfile)
791 fdp->fd_lastfile = i;
792 if (want <= fdp->fd_freefile)
793 fdp->fd_freefile = i;
794 *result = i;
795 return (0);
796 }
797 ofiles++; ofileflags++;
798 }
799
800 /*
801 * No space in current array. Expand?
802 */
803 if (fdp->fd_nfiles >= lim)
804 return (EMFILE);
805 if (fdp->fd_nfiles < NDEXTENT)
806 nfiles = NDEXTENT;
807 else
808 nfiles = 2 * fdp->fd_nfiles;
809 /* Enforce lim */
810 if (nfiles > lim)
811 nfiles = lim;
812 MALLOC_ZONE(newofiles, struct file **,
813 nfiles * OFILESIZE, M_OFILETABL, M_WAITOK);
814 if (fdp->fd_nfiles >= nfiles) {
815 FREE_ZONE(newofiles, nfiles * OFILESIZE, M_OFILETABL);
816 continue;
817 }
818 newofileflags = (char *) &newofiles[nfiles];
819 /*
820 * Copy the existing ofile and ofileflags arrays
821 * and zero the new portion of each array.
822 */
823 oldnfiles = fdp->fd_nfiles;
824 (void) memcpy(newofiles, fdp->fd_ofiles,
825 oldnfiles * sizeof *fdp->fd_ofiles);
826 (void) memset(&newofiles[oldnfiles], 0,
827 (nfiles - oldnfiles) * sizeof *fdp->fd_ofiles);
828
829 (void) memcpy(newofileflags, fdp->fd_ofileflags,
830 oldnfiles * sizeof *fdp->fd_ofileflags);
831 (void) memset(&newofileflags[oldnfiles], 0,
832 (nfiles - oldnfiles) *
833 sizeof *fdp->fd_ofileflags);
834 ofiles = fdp->fd_ofiles;
835 fdp->fd_ofiles = newofiles;
836 fdp->fd_ofileflags = newofileflags;
837 fdp->fd_nfiles = nfiles;
838 FREE_ZONE(ofiles, oldnfiles * OFILESIZE, M_OFILETABL);
839 fdexpand++;
840 }
841 }
842
843 /*
844 * Check to see whether n user file descriptors
845 * are available to the process p.
846 */
847 int
848 fdavail(p, n)
849 struct proc *p;
850 register int n;
851 {
852 register struct filedesc *fdp = p->p_fd;
853 register struct file **fpp;
854 register char *flags;
855 register int i, lim;
856
857 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
858 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
859 return (1);
860 fpp = &fdp->fd_ofiles[fdp->fd_freefile];
861 flags = &fdp->fd_ofileflags[fdp->fd_freefile];
862 for (i = fdp->fd_nfiles - fdp->fd_freefile; --i >= 0; fpp++, flags++)
863 if (*fpp == NULL && !(*flags & UF_RESERVED) && --n <= 0)
864 return (1);
865 return (0);
866 }
867
868 void
869 fdrelse(p, fd)
870 struct proc *p;
871 int fd;
872 {
873 _fdrelse(p->p_fd, fd);
874 }
875
876 int
877 fdgetf(p, fd, resultfp)
878 register struct proc *p;
879 register int fd;
880 struct file **resultfp;
881 {
882 register struct filedesc *fdp = p->p_fd;
883 struct file *fp;
884
885 if ((u_int)fd >= fdp->fd_nfiles ||
886 (fp = fdp->fd_ofiles[fd]) == NULL ||
887 (fdp->fd_ofileflags[fd] & UF_RESERVED))
888 return (EBADF);
889
890 if (resultfp)
891 *resultfp = fp;
892 return (0);
893 }
894
895 /*
896 * Create a new open file structure and allocate
897 * a file decriptor for the process that refers to it.
898 */
899 int
900 falloc(p, resultfp, resultfd)
901 register struct proc *p;
902 struct file **resultfp;
903 int *resultfd;
904 {
905 register struct file *fp, *fq;
906 int error, i;
907
908 if (error = fdalloc(p, 0, &i))
909 return (error);
910 if (nfiles >= maxfiles) {
911 tablefull("file");
912 return (ENFILE);
913 }
914 /*
915 * Allocate a new file descriptor.
916 * If the process has file descriptor zero open, add to the list
917 * of open files at that point, otherwise put it at the front of
918 * the list of open files.
919 */
920 nfiles++;
921 MALLOC_ZONE(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK);
922 bzero(fp, sizeof(struct file));
923 if (fq = p->p_fd->fd_ofiles[0]) {
924 LIST_INSERT_AFTER(fq, fp, f_list);
925 } else {
926 LIST_INSERT_HEAD(&filehead, fp, f_list);
927 }
928 p->p_fd->fd_ofiles[i] = fp;
929 fp->f_count = 1;
930 fp->f_cred = p->p_ucred;
931 crhold(fp->f_cred);
932 if (resultfp)
933 *resultfp = fp;
934 if (resultfd)
935 *resultfd = i;
936 return (0);
937 }
938
939 /*
940 * Free a file structure.
941 */
942 void
943 ffree(fp)
944 register struct file *fp;
945 {
946 register struct file *fq;
947 struct ucred *cred;
948
949 LIST_REMOVE(fp, f_list);
950 cred = fp->f_cred;
951 if (cred != NOCRED) {
952 fp->f_cred = NOCRED;
953 crfree(cred);
954 }
955
956 fp->f_count = 0;
957
958 nfiles--;
959 FREE_ZONE(fp, sizeof *fp, M_FILE);
960 }
961
962 void
963 fdexec(p)
964 struct proc *p;
965 {
966 register struct filedesc *fdp = p->p_fd;
967 register int i = fdp->fd_lastfile;
968 register struct file **fpp = &fdp->fd_ofiles[i];
969 register char *flags = &fdp->fd_ofileflags[i];
970
971 while (i >= 0) {
972 if ((*flags & (UF_RESERVED|UF_EXCLOSE)) == UF_EXCLOSE) {
973 register struct file *fp = *fpp;
974
975 *fpp = NULL; *flags = 0;
976 if (i == fdp->fd_lastfile && i > 0)
977 fdp->fd_lastfile--;
978 closef(fp, p);
979 }
980 else
981 *flags &= ~UF_MAPPED;
982
983 i--; fpp--; flags--;
984 }
985 }
986
987 /*
988 * Copy a filedesc structure.
989 */
990 struct filedesc *
991 fdcopy(p)
992 struct proc *p;
993 {
994 register struct filedesc *newfdp, *fdp = p->p_fd;
995 register int i;
996
997 MALLOC_ZONE(newfdp, struct filedesc *,
998 sizeof *newfdp, M_FILEDESC, M_WAITOK);
999 (void) memcpy(newfdp, fdp, sizeof *newfdp);
1000 VREF(newfdp->fd_cdir);
1001 if (newfdp->fd_rdir)
1002 VREF(newfdp->fd_rdir);
1003 newfdp->fd_refcnt = 1;
1004
1005 /*
1006 * If the number of open files fits in the internal arrays
1007 * of the open file structure, use them, otherwise allocate
1008 * additional memory for the number of descriptors currently
1009 * in use.
1010 */
1011 if (newfdp->fd_lastfile < NDFILE)
1012 i = NDFILE;
1013 else {
1014 /*
1015 * Compute the smallest multiple of NDEXTENT needed
1016 * for the file descriptors currently in use,
1017 * allowing the table to shrink.
1018 */
1019 i = newfdp->fd_nfiles;
1020 while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
1021 i /= 2;
1022 }
1023 MALLOC_ZONE(newfdp->fd_ofiles, struct file **,
1024 i * OFILESIZE, M_OFILETABL, M_WAITOK);
1025 newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
1026 newfdp->fd_nfiles = i;
1027 if (fdp->fd_nfiles > 0) {
1028 register struct file **fpp;
1029 register char *flags;
1030
1031 (void) memcpy(newfdp->fd_ofiles, fdp->fd_ofiles,
1032 i * sizeof *fdp->fd_ofiles);
1033 (void) memcpy(newfdp->fd_ofileflags, fdp->fd_ofileflags,
1034 i * sizeof *fdp->fd_ofileflags);
1035
1036 fpp = newfdp->fd_ofiles;
1037 flags = newfdp->fd_ofileflags;
1038 for (i = newfdp->fd_lastfile; i-- >= 0; fpp++, flags++)
1039 if (*fpp != NULL && !(*flags & UF_RESERVED)) {
1040 (void)fref(*fpp);
1041 } else {
1042 *fpp = NULL;
1043 *flags = 0;
1044 }
1045 } else
1046 (void) memset(newfdp->fd_ofiles, 0, i * OFILESIZE);
1047
1048 return (newfdp);
1049 }
1050
1051 /*
1052 * Release a filedesc structure.
1053 */
1054 void
1055 fdfree(p)
1056 struct proc *p;
1057 {
1058 struct filedesc *fdp;
1059 struct file **fpp;
1060 int i;
1061 struct vnode *tvp;
1062
1063 if ((fdp = p->p_fd) == NULL)
1064 return;
1065 if (--fdp->fd_refcnt > 0)
1066 return;
1067 p->p_fd = NULL;
1068 if (fdp->fd_nfiles > 0) {
1069 fpp = fdp->fd_ofiles;
1070 for (i = fdp->fd_lastfile; i-- >= 0; fpp++)
1071 if (*fpp)
1072 (void) closef(*fpp, p);
1073 FREE_ZONE(fdp->fd_ofiles,
1074 fdp->fd_nfiles * OFILESIZE, M_OFILETABL);
1075 }
1076 tvp = fdp->fd_cdir;
1077 fdp->fd_cdir = NULL;
1078 vrele(tvp);
1079 if (fdp->fd_rdir) {
1080 tvp = fdp->fd_rdir;
1081 fdp->fd_rdir = NULL;
1082 vrele(tvp);
1083 }
1084 FREE_ZONE(fdp, sizeof *fdp, M_FILEDESC);
1085 }
1086
1087 static int
1088 closef_finish(fp, p)
1089 register struct file *fp;
1090 register struct proc *p;
1091 {
1092 struct vnode *vp;
1093 struct flock lf;
1094 int error;
1095
1096 if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
1097 lf.l_whence = SEEK_SET;
1098 lf.l_start = 0;
1099 lf.l_len = 0;
1100 lf.l_type = F_UNLCK;
1101 vp = (struct vnode *)fp->f_data;
1102 (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1103 }
1104 if (fp->f_ops)
1105 error = fo_close(fp, p);
1106 else
1107 error = 0;
1108 ffree(fp);
1109 return (error);
1110 }
1111
1112 /*
1113 * Internal form of close.
1114 * Decrement reference count on file structure.
1115 * Note: p may be NULL when closing a file
1116 * that was being passed in a message.
1117 */
1118 int
1119 closef(fp, p)
1120 register struct file *fp;
1121 register struct proc *p;
1122 {
1123 struct vnode *vp;
1124 struct flock lf;
1125 int error;
1126
1127 if (fp == NULL)
1128 return (0);
1129 /*
1130 * POSIX record locking dictates that any close releases ALL
1131 * locks owned by this process. This is handled by setting
1132 * a flag in the unlock to free ONLY locks obeying POSIX
1133 * semantics, and not to free BSD-style file locks.
1134 * If the descriptor was in a message, POSIX-style locks
1135 * aren't passed with the descriptor.
1136 */
1137 if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
1138 lf.l_whence = SEEK_SET;
1139 lf.l_start = 0;
1140 lf.l_len = 0;
1141 lf.l_type = F_UNLCK;
1142 vp = (struct vnode *)fp->f_data;
1143 (void) VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX);
1144 }
1145 if (frele_internal(fp) > 0)
1146 return (0);
1147 return(closef_finish(fp, p));
1148 }
1149
1150 /*
1151 * Apply an advisory lock on a file descriptor.
1152 *
1153 * Just attempt to get a record lock of the requested type on
1154 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1155 */
1156 struct flock_args {
1157 int fd;
1158 int how;
1159 };
1160 /* ARGSUSED */
1161 int
1162 flock(p, uap, retval)
1163 struct proc *p;
1164 register struct flock_args *uap;
1165 register_t *retval;
1166 {
1167 int fd = uap->fd;
1168 int how = uap->how;
1169 register struct filedesc *fdp = p->p_fd;
1170 register struct file *fp;
1171 struct vnode *vp;
1172 struct flock lf;
1173
1174 if ((u_int)fd >= fdp->fd_nfiles ||
1175 (fp = fdp->fd_ofiles[fd]) == NULL ||
1176 (fdp->fd_ofileflags[fd] & UF_RESERVED))
1177 return (EBADF);
1178 if (fp->f_type != DTYPE_VNODE)
1179 return (EOPNOTSUPP);
1180 vp = (struct vnode *)fp->f_data;
1181 lf.l_whence = SEEK_SET;
1182 lf.l_start = 0;
1183 lf.l_len = 0;
1184 if (how & LOCK_UN) {
1185 lf.l_type = F_UNLCK;
1186 fp->f_flag &= ~FHASLOCK;
1187 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
1188 }
1189 if (how & LOCK_EX)
1190 lf.l_type = F_WRLCK;
1191 else if (how & LOCK_SH)
1192 lf.l_type = F_RDLCK;
1193 else
1194 return (EBADF);
1195 fp->f_flag |= FHASLOCK;
1196 if (how & LOCK_NB)
1197 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
1198 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
1199 }
1200
1201 /*
1202 * File Descriptor pseudo-device driver (/dev/fd/).
1203 *
1204 * Opening minor device N dup()s the file (if any) connected to file
1205 * descriptor N belonging to the calling process. Note that this driver
1206 * consists of only the ``open()'' routine, because all subsequent
1207 * references to this file will be direct to the other driver.
1208 */
1209 /* ARGSUSED */
1210 int
1211 fdopen(dev, mode, type, p)
1212 dev_t dev;
1213 int mode, type;
1214 struct proc *p;
1215 {
1216
1217 /*
1218 * XXX Kludge: set curproc->p_dupfd to contain the value of the
1219 * the file descriptor being sought for duplication. The error
1220 * return ensures that the vnode for this device will be released
1221 * by vn_open. Open will detect this special error and take the
1222 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1223 * will simply report the error.
1224 */
1225 p->p_dupfd = minor(dev);
1226 return (ENODEV);
1227 }
1228
1229 /*
1230 * Duplicate the specified descriptor to a free descriptor.
1231 */
1232 int
1233 dupfdopen(fdp, indx, dfd, mode, error)
1234 register struct filedesc *fdp;
1235 register int indx, dfd;
1236 int mode;
1237 int error;
1238 {
1239 register struct file *wfp;
1240 struct file *fp;
1241
1242 /*
1243 * If the to-be-dup'd fd number is greater than the allowed number
1244 * of file descriptors, or the fd to be dup'd has already been
1245 * closed, reject. Note, check for new == old is necessary as
1246 * falloc could allocate an already closed to-be-dup'd descriptor
1247 * as the new descriptor.
1248 */
1249 fp = fdp->fd_ofiles[indx];
1250 if ((u_int)dfd >= fdp->fd_nfiles ||
1251 (wfp = fdp->fd_ofiles[dfd]) == NULL || wfp == fp ||
1252 (fdp->fd_ofileflags[dfd] & UF_RESERVED))
1253 return (EBADF);
1254
1255 /*
1256 * There are two cases of interest here.
1257 *
1258 * For ENODEV simply dup (dfd) to file descriptor
1259 * (indx) and return.
1260 *
1261 * For ENXIO steal away the file structure from (dfd) and
1262 * store it in (indx). (dfd) is effectively closed by
1263 * this operation.
1264 *
1265 * Any other error code is just returned.
1266 */
1267 switch (error) {
1268 case ENODEV:
1269 /*
1270 * Check that the mode the file is being opened for is a
1271 * subset of the mode of the existing descriptor.
1272 */
1273 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
1274 return (EACCES);
1275 (void)fref(wfp);
1276 if (indx > fdp->fd_lastfile)
1277 fdp->fd_lastfile = indx;;
1278 fdp->fd_ofiles[indx] = wfp;
1279 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1280 return (0);
1281
1282 case ENXIO:
1283 /*
1284 * Steal away the file pointer from dfd, and stuff it into indx.
1285 */
1286 if (indx > fdp->fd_lastfile)
1287 fdp->fd_lastfile = indx;;
1288 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
1289 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1290 _fdrelse(fdp, dfd);
1291 return (0);
1292
1293 default:
1294 return (error);
1295 }
1296 /* NOTREACHED */
1297 }
1298
1299 /* Reference manipulation routines for the file structure */
1300
1301 int
1302 fref(struct file *fp)
1303 {
1304 if (++fp->f_count <= 0)
1305 panic("fref: f_count");
1306 return ((int)fp->f_count);
1307 }
1308
1309 static int
1310 frele_internal(struct file *fp)
1311 {
1312 if (--fp->f_count < 0)
1313 panic("frele: count < 0");
1314 return ((int)fp->f_count);
1315 }
1316
1317
1318 int
1319 frele(struct file *fp)
1320 {
1321 int count;
1322 funnel_t * fnl;
1323 extern int disable_funnel;
1324
1325 fnl = thread_funnel_get();
1326 /*
1327 * If the funnels are merged then atleast a funnel should be held
1328 * else frele should come in with kernel funnel only
1329 */
1330 if (!disable_funnel && (fnl != kernel_flock)) {
1331 panic("frele: kernel funnel not held");
1332
1333 } else if (fnl == THR_FUNNEL_NULL) {
1334 panic("frele: no funnel held");
1335 }
1336
1337 if ((count = frele_internal(fp)) == 0) {
1338 /* some one closed the fd while we were blocked */
1339 (void)closef_finish(fp, current_proc());
1340 }
1341 return(count);
1342 }
1343
1344 int
1345 fcount(struct file *fp)
1346 {
1347 return ((int)fp->f_count);
1348 }
1349