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