kern_descrip.c revision 1.1.1.2 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 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/filedesc.h>
44 #include <sys/kernel.h>
45 #include <sys/vnode.h>
46 #include <sys/proc.h>
47 #include <sys/file.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/stat.h>
51 #include <sys/ioctl.h>
52 #include <sys/fcntl.h>
53 #include <sys/malloc.h>
54 #include <sys/syslog.h>
55 #include <sys/unistd.h>
56 #include <sys/resourcevar.h>
57
58 /*
59 * Descriptor management.
60 */
61 struct file *filehead; /* head of list of open files */
62 int nfiles; /* actual number of open files */
63
64 /*
65 * System calls on descriptors.
66 */
67 struct getdtablesize_args {
68 int dummy;
69 };
70 /* ARGSUSED */
71 getdtablesize(p, uap, retval)
72 struct proc *p;
73 struct getdtablesize_args *uap;
74 int *retval;
75 {
76
77 *retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
78 return (0);
79 }
80
81 /*
82 * Duplicate a file descriptor.
83 */
84 struct dup_args {
85 u_int fd;
86 };
87 /* ARGSUSED */
88 dup(p, uap, retval)
89 struct proc *p;
90 struct dup_args *uap;
91 int *retval;
92 {
93 register struct filedesc *fdp;
94 u_int old;
95 int new, error;
96
97 old = uap->fd;
98 /*
99 * XXX Compatibility
100 */
101 if (old &~ 077) { uap->fd &= 077; return (dup2(p, uap, retval)); }
102
103 fdp = p->p_fd;
104 if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL)
105 return (EBADF);
106 if (error = fdalloc(p, 0, &new))
107 return (error);
108 return (finishdup(fdp, (int)old, new, retval));
109 }
110
111 /*
112 * Duplicate a file descriptor to a particular value.
113 */
114 struct dup2_args {
115 u_int from;
116 u_int to;
117 };
118 /* ARGSUSED */
119 dup2(p, uap, retval)
120 struct proc *p;
121 struct dup2_args *uap;
122 int *retval;
123 {
124 register struct filedesc *fdp = p->p_fd;
125 register u_int old = uap->from, new = uap->to;
126 int i, error;
127
128 if (old >= fdp->fd_nfiles ||
129 fdp->fd_ofiles[old] == NULL ||
130 new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
131 new >= maxfiles)
132 return (EBADF);
133 if (old == new) {
134 *retval = new;
135 return (0);
136 }
137 if (new >= fdp->fd_nfiles) {
138 if (error = fdalloc(p, new, &i))
139 return (error);
140 if (new != i)
141 panic("dup2: fdalloc");
142 } else if (fdp->fd_ofiles[new]) {
143 if (fdp->fd_ofileflags[new] & UF_MAPPED)
144 (void) munmapfd(p, new);
145 /*
146 * dup2() must succeed even if the close has an error.
147 */
148 (void) closef(fdp->fd_ofiles[new], p);
149 }
150 return (finishdup(fdp, (int)old, (int)new, retval));
151 }
152
153 /*
154 * The file control system call.
155 */
156 struct fcntl_args {
157 int fd;
158 int cmd;
159 int arg;
160 };
161 /* ARGSUSED */
162 fcntl(p, uap, retval)
163 struct proc *p;
164 register struct fcntl_args *uap;
165 int *retval;
166 {
167 register struct filedesc *fdp = p->p_fd;
168 register struct file *fp;
169 register char *pop;
170 struct vnode *vp;
171 int i, tmp, error, flg = F_POSIX;
172 struct flock fl;
173 u_int newmin;
174
175 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
176 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
177 return (EBADF);
178 pop = &fdp->fd_ofileflags[uap->fd];
179 switch (uap->cmd) {
180
181 case F_DUPFD:
182 newmin = uap->arg;
183 if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
184 newmin >= maxfiles)
185 return (EINVAL);
186 if (error = fdalloc(p, newmin, &i))
187 return (error);
188 return (finishdup(fdp, uap->fd, i, retval));
189
190 case F_GETFD:
191 *retval = *pop & 1;
192 return (0);
193
194 case F_SETFD:
195 *pop = (*pop &~ 1) | (uap->arg & 1);
196 return (0);
197
198 case F_GETFL:
199 *retval = OFLAGS(fp->f_flag);
200 return (0);
201
202 case F_SETFL:
203 fp->f_flag &= ~FCNTLFLAGS;
204 fp->f_flag |= FFLAGS(uap->arg) & FCNTLFLAGS;
205 tmp = fp->f_flag & FNONBLOCK;
206 error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
207 if (error)
208 return (error);
209 tmp = fp->f_flag & FASYNC;
210 error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
211 if (!error)
212 return (0);
213 fp->f_flag &= ~FNONBLOCK;
214 tmp = 0;
215 (void) (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
216 return (error);
217
218 case F_GETOWN:
219 if (fp->f_type == DTYPE_SOCKET) {
220 *retval = ((struct socket *)fp->f_data)->so_pgid;
221 return (0);
222 }
223 error = (*fp->f_ops->fo_ioctl)
224 (fp, (int)TIOCGPGRP, (caddr_t)retval, p);
225 *retval = -*retval;
226 return (error);
227
228 case F_SETOWN:
229 if (fp->f_type == DTYPE_SOCKET) {
230 ((struct socket *)fp->f_data)->so_pgid = uap->arg;
231 return (0);
232 }
233 if (uap->arg <= 0) {
234 uap->arg = -uap->arg;
235 } else {
236 struct proc *p1 = pfind(uap->arg);
237 if (p1 == 0)
238 return (ESRCH);
239 uap->arg = p1->p_pgrp->pg_id;
240 }
241 return ((*fp->f_ops->fo_ioctl)
242 (fp, (int)TIOCSPGRP, (caddr_t)&uap->arg, p));
243
244 case F_SETLKW:
245 flg |= F_WAIT;
246 /* Fall into F_SETLK */
247
248 case F_SETLK:
249 if (fp->f_type != DTYPE_VNODE)
250 return (EBADF);
251 vp = (struct vnode *)fp->f_data;
252 /* Copy in the lock structure */
253 error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl));
254 if (error)
255 return (error);
256 if (fl.l_whence == SEEK_CUR)
257 fl.l_start += fp->f_offset;
258 switch (fl.l_type) {
259
260 case F_RDLCK:
261 if ((fp->f_flag & FREAD) == 0)
262 return (EBADF);
263 p->p_flag |= P_ADVLOCK;
264 return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
265
266 case F_WRLCK:
267 if ((fp->f_flag & FWRITE) == 0)
268 return (EBADF);
269 p->p_flag |= P_ADVLOCK;
270 return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
271
272 case F_UNLCK:
273 return (VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl,
274 F_POSIX));
275
276 default:
277 return (EINVAL);
278 }
279
280 case F_GETLK:
281 if (fp->f_type != DTYPE_VNODE)
282 return (EBADF);
283 vp = (struct vnode *)fp->f_data;
284 /* Copy in the lock structure */
285 error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl));
286 if (error)
287 return (error);
288 if (fl.l_whence == SEEK_CUR)
289 fl.l_start += fp->f_offset;
290 if (error = VOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX))
291 return (error);
292 return (copyout((caddr_t)&fl, (caddr_t)uap->arg, sizeof (fl)));
293
294 default:
295 return (EINVAL);
296 }
297 /* NOTREACHED */
298 }
299
300 /*
301 * Common code for dup, dup2, and fcntl(F_DUPFD).
302 */
303 int
304 finishdup(fdp, old, new, retval)
305 register struct filedesc *fdp;
306 register int old, new, *retval;
307 {
308 register struct file *fp;
309
310 fp = fdp->fd_ofiles[old];
311 fdp->fd_ofiles[new] = fp;
312 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
313 fp->f_count++;
314 if (new > fdp->fd_lastfile)
315 fdp->fd_lastfile = new;
316 *retval = new;
317 return (0);
318 }
319
320 /*
321 * Close a file descriptor.
322 */
323 struct close_args {
324 int fd;
325 };
326 /* ARGSUSED */
327 close(p, uap, retval)
328 struct proc *p;
329 struct close_args *uap;
330 int *retval;
331 {
332 register struct filedesc *fdp = p->p_fd;
333 register struct file *fp;
334 register int fd = uap->fd;
335 register u_char *pf;
336
337 if ((unsigned)fd >= fdp->fd_nfiles ||
338 (fp = fdp->fd_ofiles[fd]) == NULL)
339 return (EBADF);
340 pf = (u_char *)&fdp->fd_ofileflags[fd];
341 if (*pf & UF_MAPPED)
342 (void) munmapfd(p, fd);
343 fdp->fd_ofiles[fd] = NULL;
344 while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
345 fdp->fd_lastfile--;
346 if (fd < fdp->fd_freefile)
347 fdp->fd_freefile = fd;
348 *pf = 0;
349 return (closef(fp, p));
350 }
351
352 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
353 /*
354 * Return status information about a file descriptor.
355 */
356 struct ofstat_args {
357 int fd;
358 struct ostat *sb;
359 };
360 /* ARGSUSED */
361 ofstat(p, uap, retval)
362 struct proc *p;
363 register struct ofstat_args *uap;
364 int *retval;
365 {
366 register struct filedesc *fdp = p->p_fd;
367 register struct file *fp;
368 struct stat ub;
369 struct ostat oub;
370 int error;
371
372 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
373 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
374 return (EBADF);
375 switch (fp->f_type) {
376
377 case DTYPE_VNODE:
378 error = vn_stat((struct vnode *)fp->f_data, &ub, p);
379 break;
380
381 case DTYPE_SOCKET:
382 error = soo_stat((struct socket *)fp->f_data, &ub);
383 break;
384
385 default:
386 panic("ofstat");
387 /*NOTREACHED*/
388 }
389 cvtstat(&ub, &oub);
390 if (error == 0)
391 error = copyout((caddr_t)&oub, (caddr_t)uap->sb, sizeof (oub));
392 return (error);
393 }
394 #endif /* COMPAT_43 || COMPAT_SUNOS */
395
396 /*
397 * Return status information about a file descriptor.
398 */
399 struct fstat_args {
400 int fd;
401 struct stat *sb;
402 };
403 /* ARGSUSED */
404 fstat(p, uap, retval)
405 struct proc *p;
406 register struct fstat_args *uap;
407 int *retval;
408 {
409 register struct filedesc *fdp = p->p_fd;
410 register struct file *fp;
411 struct stat ub;
412 int error;
413
414 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
415 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
416 return (EBADF);
417 switch (fp->f_type) {
418
419 case DTYPE_VNODE:
420 error = vn_stat((struct vnode *)fp->f_data, &ub, p);
421 break;
422
423 case DTYPE_SOCKET:
424 error = soo_stat((struct socket *)fp->f_data, &ub);
425 break;
426
427 default:
428 panic("fstat");
429 /*NOTREACHED*/
430 }
431 if (error == 0)
432 error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub));
433 return (error);
434 }
435
436 /*
437 * Return pathconf information about a file descriptor.
438 */
439 struct fpathconf_args {
440 int fd;
441 int name;
442 };
443 /* ARGSUSED */
444 fpathconf(p, uap, retval)
445 struct proc *p;
446 register struct fpathconf_args *uap;
447 int *retval;
448 {
449 struct filedesc *fdp = p->p_fd;
450 struct file *fp;
451 struct vnode *vp;
452
453 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
454 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
455 return (EBADF);
456 switch (fp->f_type) {
457
458 case DTYPE_SOCKET:
459 if (uap->name != _PC_PIPE_BUF)
460 return (EINVAL);
461 *retval = PIPE_BUF;
462 return (0);
463
464 case DTYPE_VNODE:
465 vp = (struct vnode *)fp->f_data;
466 return (VOP_PATHCONF(vp, uap->name, retval));
467
468 default:
469 panic("fpathconf");
470 }
471 /*NOTREACHED*/
472 }
473
474 /*
475 * Allocate a file descriptor for the process.
476 */
477 int fdexpand;
478
479 fdalloc(p, want, result)
480 struct proc *p;
481 int want;
482 int *result;
483 {
484 register struct filedesc *fdp = p->p_fd;
485 register int i;
486 int lim, last, nfiles;
487 struct file **newofile;
488 char *newofileflags;
489
490 /*
491 * Search for a free descriptor starting at the higher
492 * of want or fd_freefile. If that fails, consider
493 * expanding the ofile array.
494 */
495 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
496 for (;;) {
497 last = min(fdp->fd_nfiles, lim);
498 if ((i = want) < fdp->fd_freefile)
499 i = fdp->fd_freefile;
500 for (; i < last; i++) {
501 if (fdp->fd_ofiles[i] == NULL) {
502 fdp->fd_ofileflags[i] = 0;
503 if (i > fdp->fd_lastfile)
504 fdp->fd_lastfile = i;
505 if (want <= fdp->fd_freefile)
506 fdp->fd_freefile = i;
507 *result = i;
508 return (0);
509 }
510 }
511
512 /*
513 * No space in current array. Expand?
514 */
515 if (fdp->fd_nfiles >= lim)
516 return (EMFILE);
517 if (fdp->fd_nfiles < NDEXTENT)
518 nfiles = NDEXTENT;
519 else
520 nfiles = 2 * fdp->fd_nfiles;
521 MALLOC(newofile, struct file **, nfiles * OFILESIZE,
522 M_FILEDESC, M_WAITOK);
523 newofileflags = (char *) &newofile[nfiles];
524 /*
525 * Copy the existing ofile and ofileflags arrays
526 * and zero the new portion of each array.
527 */
528 bcopy(fdp->fd_ofiles, newofile,
529 (i = sizeof(struct file *) * fdp->fd_nfiles));
530 bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i);
531 bcopy(fdp->fd_ofileflags, newofileflags,
532 (i = sizeof(char) * fdp->fd_nfiles));
533 bzero(newofileflags + i, nfiles * sizeof(char) - i);
534 if (fdp->fd_nfiles > NDFILE)
535 FREE(fdp->fd_ofiles, M_FILEDESC);
536 fdp->fd_ofiles = newofile;
537 fdp->fd_ofileflags = newofileflags;
538 fdp->fd_nfiles = nfiles;
539 fdexpand++;
540 }
541 }
542
543 /*
544 * Check to see whether n user file descriptors
545 * are available to the process p.
546 */
547 fdavail(p, n)
548 struct proc *p;
549 register int n;
550 {
551 register struct filedesc *fdp = p->p_fd;
552 register struct file **fpp;
553 register int i, lim;
554
555 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
556 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
557 return (1);
558 fpp = &fdp->fd_ofiles[fdp->fd_freefile];
559 for (i = fdp->fd_nfiles - fdp->fd_freefile; --i >= 0; fpp++)
560 if (*fpp == NULL && --n <= 0)
561 return (1);
562 return (0);
563 }
564
565 /*
566 * Create a new open file structure and allocate
567 * a file decriptor for the process that refers to it.
568 */
569 falloc(p, resultfp, resultfd)
570 register struct proc *p;
571 struct file **resultfp;
572 int *resultfd;
573 {
574 register struct file *fp, *fq, **fpp;
575 int error, i;
576
577 if (error = fdalloc(p, 0, &i))
578 return (error);
579 if (nfiles >= maxfiles) {
580 tablefull("file");
581 return (ENFILE);
582 }
583 /*
584 * Allocate a new file descriptor.
585 * If the process has file descriptor zero open, add to the list
586 * of open files at that point, otherwise put it at the front of
587 * the list of open files.
588 */
589 nfiles++;
590 MALLOC(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK);
591 bzero(fp, sizeof(struct file));
592 if (fq = p->p_fd->fd_ofiles[0])
593 fpp = &fq->f_filef;
594 else
595 fpp = &filehead;
596 p->p_fd->fd_ofiles[i] = fp;
597 if (fq = *fpp)
598 fq->f_fileb = &fp->f_filef;
599 fp->f_filef = fq;
600 fp->f_fileb = fpp;
601 *fpp = fp;
602 fp->f_count = 1;
603 fp->f_cred = p->p_ucred;
604 crhold(fp->f_cred);
605 if (resultfp)
606 *resultfp = fp;
607 if (resultfd)
608 *resultfd = i;
609 return (0);
610 }
611
612 /*
613 * Free a file descriptor.
614 */
615 ffree(fp)
616 register struct file *fp;
617 {
618 register struct file *fq;
619
620 if (fq = fp->f_filef)
621 fq->f_fileb = fp->f_fileb;
622 *fp->f_fileb = fq;
623 crfree(fp->f_cred);
624 #ifdef DIAGNOSTIC
625 fp->f_filef = NULL;
626 fp->f_fileb = NULL;
627 fp->f_count = 0;
628 #endif
629 nfiles--;
630 FREE(fp, M_FILE);
631 }
632
633 /*
634 * Copy a filedesc structure.
635 */
636 struct filedesc *
637 fdcopy(p)
638 struct proc *p;
639 {
640 register struct filedesc *newfdp, *fdp = p->p_fd;
641 register struct file **fpp;
642 register int i;
643
644 MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0),
645 M_FILEDESC, M_WAITOK);
646 bcopy(fdp, newfdp, sizeof(struct filedesc));
647 VREF(newfdp->fd_cdir);
648 if (newfdp->fd_rdir)
649 VREF(newfdp->fd_rdir);
650 newfdp->fd_refcnt = 1;
651
652 /*
653 * If the number of open files fits in the internal arrays
654 * of the open file structure, use them, otherwise allocate
655 * additional memory for the number of descriptors currently
656 * in use.
657 */
658 if (newfdp->fd_lastfile < NDFILE) {
659 newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
660 newfdp->fd_ofileflags =
661 ((struct filedesc0 *) newfdp)->fd_dfileflags;
662 i = NDFILE;
663 } else {
664 /*
665 * Compute the smallest multiple of NDEXTENT needed
666 * for the file descriptors currently in use,
667 * allowing the table to shrink.
668 */
669 i = newfdp->fd_nfiles;
670 while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
671 i /= 2;
672 MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
673 M_FILEDESC, M_WAITOK);
674 newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
675 }
676 newfdp->fd_nfiles = i;
677 bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **));
678 bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char));
679 fpp = newfdp->fd_ofiles;
680 for (i = newfdp->fd_lastfile; i-- >= 0; fpp++)
681 if (*fpp != NULL)
682 (*fpp)->f_count++;
683 return (newfdp);
684 }
685
686 /*
687 * Release a filedesc structure.
688 */
689 void
690 fdfree(p)
691 struct proc *p;
692 {
693 register struct filedesc *fdp = p->p_fd;
694 struct file **fpp;
695 register int i;
696
697 if (--fdp->fd_refcnt > 0)
698 return;
699 fpp = fdp->fd_ofiles;
700 for (i = fdp->fd_lastfile; i-- >= 0; fpp++)
701 if (*fpp)
702 (void) closef(*fpp, p);
703 if (fdp->fd_nfiles > NDFILE)
704 FREE(fdp->fd_ofiles, M_FILEDESC);
705 vrele(fdp->fd_cdir);
706 if (fdp->fd_rdir)
707 vrele(fdp->fd_rdir);
708 FREE(fdp, M_FILEDESC);
709 }
710
711 /*
712 * Internal form of close.
713 * Decrement reference count on file structure.
714 * Note: p may be NULL when closing a file
715 * that was being passed in a message.
716 */
717 closef(fp, p)
718 register struct file *fp;
719 register struct proc *p;
720 {
721 struct vnode *vp;
722 struct flock lf;
723 int error;
724
725 if (fp == NULL)
726 return (0);
727 /*
728 * POSIX record locking dictates that any close releases ALL
729 * locks owned by this process. This is handled by setting
730 * a flag in the unlock to free ONLY locks obeying POSIX
731 * semantics, and not to free BSD-style file locks.
732 * If the descriptor was in a message, POSIX-style locks
733 * aren't passed with the descriptor.
734 */
735 if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
736 lf.l_whence = SEEK_SET;
737 lf.l_start = 0;
738 lf.l_len = 0;
739 lf.l_type = F_UNLCK;
740 vp = (struct vnode *)fp->f_data;
741 (void) VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX);
742 }
743 if (--fp->f_count > 0)
744 return (0);
745 if (fp->f_count < 0)
746 panic("closef: count < 0");
747 if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
748 lf.l_whence = SEEK_SET;
749 lf.l_start = 0;
750 lf.l_len = 0;
751 lf.l_type = F_UNLCK;
752 vp = (struct vnode *)fp->f_data;
753 (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
754 }
755 if (fp->f_ops)
756 error = (*fp->f_ops->fo_close)(fp, p);
757 else
758 error = 0;
759 ffree(fp);
760 return (error);
761 }
762
763 /*
764 * Apply an advisory lock on a file descriptor.
765 *
766 * Just attempt to get a record lock of the requested type on
767 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
768 */
769 struct flock_args {
770 int fd;
771 int how;
772 };
773 /* ARGSUSED */
774 flock(p, uap, retval)
775 struct proc *p;
776 register struct flock_args *uap;
777 int *retval;
778 {
779 register struct filedesc *fdp = p->p_fd;
780 register struct file *fp;
781 struct vnode *vp;
782 struct flock lf;
783
784 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
785 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
786 return (EBADF);
787 if (fp->f_type != DTYPE_VNODE)
788 return (EOPNOTSUPP);
789 vp = (struct vnode *)fp->f_data;
790 lf.l_whence = SEEK_SET;
791 lf.l_start = 0;
792 lf.l_len = 0;
793 if (uap->how & LOCK_UN) {
794 lf.l_type = F_UNLCK;
795 fp->f_flag &= ~FHASLOCK;
796 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
797 }
798 if (uap->how & LOCK_EX)
799 lf.l_type = F_WRLCK;
800 else if (uap->how & LOCK_SH)
801 lf.l_type = F_RDLCK;
802 else
803 return (EBADF);
804 fp->f_flag |= FHASLOCK;
805 if (uap->how & LOCK_NB)
806 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
807 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
808 }
809
810 /*
811 * File Descriptor pseudo-device driver (/dev/fd/).
812 *
813 * Opening minor device N dup()s the file (if any) connected to file
814 * descriptor N belonging to the calling process. Note that this driver
815 * consists of only the ``open()'' routine, because all subsequent
816 * references to this file will be direct to the other driver.
817 */
818 /* ARGSUSED */
819 fdopen(dev, mode, type, p)
820 dev_t dev;
821 int mode, type;
822 struct proc *p;
823 {
824
825 /*
826 * XXX Kludge: set curproc->p_dupfd to contain the value of the
827 * the file descriptor being sought for duplication. The error
828 * return ensures that the vnode for this device will be released
829 * by vn_open. Open will detect this special error and take the
830 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
831 * will simply report the error.
832 */
833 p->p_dupfd = minor(dev);
834 return (ENODEV);
835 }
836
837 /*
838 * Duplicate the specified descriptor to a free descriptor.
839 */
840 dupfdopen(fdp, indx, dfd, mode, error)
841 register struct filedesc *fdp;
842 register int indx, dfd;
843 int mode;
844 int error;
845 {
846 register struct file *wfp;
847 struct file *fp;
848
849 /*
850 * If the to-be-dup'd fd number is greater than the allowed number
851 * of file descriptors, or the fd to be dup'd has already been
852 * closed, reject. Note, check for new == old is necessary as
853 * falloc could allocate an already closed to-be-dup'd descriptor
854 * as the new descriptor.
855 */
856 fp = fdp->fd_ofiles[indx];
857 if ((u_int)dfd >= fdp->fd_nfiles ||
858 (wfp = fdp->fd_ofiles[dfd]) == NULL || fp == wfp)
859 return (EBADF);
860
861 /*
862 * There are two cases of interest here.
863 *
864 * For ENODEV simply dup (dfd) to file descriptor
865 * (indx) and return.
866 *
867 * For ENXIO steal away the file structure from (dfd) and
868 * store it in (indx). (dfd) is effectively closed by
869 * this operation.
870 *
871 * Any other error code is just returned.
872 */
873 switch (error) {
874 case ENODEV:
875 /*
876 * Check that the mode the file is being opened for is a
877 * subset of the mode of the existing descriptor.
878 */
879 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
880 return (EACCES);
881 fdp->fd_ofiles[indx] = wfp;
882 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
883 wfp->f_count++;
884 if (indx > fdp->fd_lastfile)
885 fdp->fd_lastfile = indx;
886 return (0);
887
888 case ENXIO:
889 /*
890 * Steal away the file pointer from dfd, and stuff it into indx.
891 */
892 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
893 fdp->fd_ofiles[dfd] = NULL;
894 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
895 fdp->fd_ofileflags[dfd] = 0;
896 /*
897 * Complete the clean up of the filedesc structure by
898 * recomputing the various hints.
899 */
900 if (indx > fdp->fd_lastfile)
901 fdp->fd_lastfile = indx;
902 else
903 while (fdp->fd_lastfile > 0 &&
904 fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
905 fdp->fd_lastfile--;
906 if (dfd < fdp->fd_freefile)
907 fdp->fd_freefile = dfd;
908 return (0);
909
910 default:
911 return (error);
912 }
913 /* NOTREACHED */
914 }
915