kern_descrip.c revision 1.163 1 /* $NetBSD: kern_descrip.c,v 1.163 2007/11/29 18:15:14 ad Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.163 2007/11/29 18:15:14 ad Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/filedesc.h>
45 #include <sys/kernel.h>
46 #include <sys/vnode.h>
47 #include <sys/proc.h>
48 #include <sys/file.h>
49 #include <sys/namei.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/stat.h>
53 #include <sys/ioctl.h>
54 #include <sys/fcntl.h>
55 #include <sys/malloc.h>
56 #include <sys/pool.h>
57 #include <sys/syslog.h>
58 #include <sys/unistd.h>
59 #include <sys/resourcevar.h>
60 #include <sys/conf.h>
61 #include <sys/event.h>
62 #include <sys/kauth.h>
63 #include <sys/atomic.h>
64
65 #include <sys/mount.h>
66 #include <sys/syscallargs.h>
67
68 static int cwdi_ctor(void *, void *, int);
69 static void cwdi_dtor(void *, void *);
70
71 /*
72 * Descriptor management.
73 */
74 struct filelist filehead; /* head of list of open files */
75 int nfiles; /* actual number of open files */
76
77 static pool_cache_t cwdi_cache;
78 static pool_cache_t filedesc0_cache;
79 static pool_cache_t file_cache;
80
81 /* Global file list lock */
82 kmutex_t filelist_lock;
83
84 MALLOC_DEFINE(M_FILE, "file", "Open file structure");
85 MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
86 MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
87
88 static inline int
89 find_next_zero(uint32_t *bitmap, int want, u_int bits)
90 {
91 int i, off, maxoff;
92 uint32_t sub;
93
94 if (want > bits)
95 return -1;
96
97 off = want >> NDENTRYSHIFT;
98 i = want & NDENTRYMASK;
99 if (i) {
100 sub = bitmap[off] | ((u_int)~0 >> (NDENTRIES - i));
101 if (sub != ~0)
102 goto found;
103 off++;
104 }
105
106 maxoff = NDLOSLOTS(bits);
107 while (off < maxoff) {
108 if ((sub = bitmap[off]) != ~0)
109 goto found;
110 off++;
111 }
112
113 return (-1);
114
115 found:
116 return (off << NDENTRYSHIFT) + ffs(~sub) - 1;
117 }
118
119 static int
120 find_last_set(struct filedesc *fd, int last)
121 {
122 int off, i;
123 struct file **ofiles = fd->fd_ofiles;
124 uint32_t *bitmap = fd->fd_lomap;
125
126 off = (last - 1) >> NDENTRYSHIFT;
127
128 while (off >= 0 && !bitmap[off])
129 off--;
130
131 if (off < 0)
132 return (-1);
133
134 i = ((off + 1) << NDENTRYSHIFT) - 1;
135 if (i >= last)
136 i = last - 1;
137
138 while (i > 0 && ofiles[i] == NULL)
139 i--;
140
141 return (i);
142 }
143
144 static inline void
145 fd_used(struct filedesc *fdp, int fd)
146 {
147 u_int off = fd >> NDENTRYSHIFT;
148
149 KASSERT(rw_write_held(&fdp->fd_lock));
150 KDASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) == 0);
151
152 fdp->fd_lomap[off] |= 1 << (fd & NDENTRYMASK);
153 if (fdp->fd_lomap[off] == ~0) {
154 KDASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
155 (1 << (off & NDENTRYMASK))) == 0);
156 fdp->fd_himap[off >> NDENTRYSHIFT] |= 1 << (off & NDENTRYMASK);
157 }
158
159 if (fd > fdp->fd_lastfile)
160 fdp->fd_lastfile = fd;
161 }
162
163 static inline void
164 fd_unused(struct filedesc *fdp, int fd)
165 {
166 u_int off = fd >> NDENTRYSHIFT;
167
168 KASSERT(rw_write_held(&fdp->fd_lock));
169 if (fd < fdp->fd_freefile)
170 fdp->fd_freefile = fd;
171
172 if (fdp->fd_lomap[off] == ~0) {
173 KDASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
174 (1 << (off & NDENTRYMASK))) != 0);
175 fdp->fd_himap[off >> NDENTRYSHIFT] &=
176 ~(1 << (off & NDENTRYMASK));
177 }
178 KDASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) != 0);
179 fdp->fd_lomap[off] &= ~(1 << (fd & NDENTRYMASK));
180
181 #ifdef DIAGNOSTIC
182 if (fd > fdp->fd_lastfile)
183 panic("fd_unused: fd_lastfile inconsistent");
184 #endif
185 if (fd == fdp->fd_lastfile)
186 fdp->fd_lastfile = find_last_set(fdp, fd);
187 }
188
189 /*
190 * Lookup the file structure corresponding to a file descriptor
191 * and return it locked.
192 * Note: typical usage is: `fp = fd_getfile(..); FILE_USE(fp);'
193 * The locking strategy has been optimised for this case, i.e.
194 * fd_getfile() returns the file locked while FILE_USE() will increment
195 * the file's use count and unlock.
196 */
197 struct file *
198 fd_getfile(struct filedesc *fdp, int fd)
199 {
200 struct file *fp;
201
202 rw_enter(&fdp->fd_lock, RW_READER);
203 if ((u_int) fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL) {
204 rw_exit(&fdp->fd_lock);
205 return (NULL);
206 }
207
208 mutex_enter(&fp->f_lock);
209 if (FILE_IS_USABLE(fp) == 0) {
210 mutex_exit(&fp->f_lock);
211 rw_exit(&fdp->fd_lock);
212 return (NULL);
213 }
214 rw_exit(&fdp->fd_lock);
215
216 return (fp);
217 }
218
219 /*
220 * Common code for dup, dup2, and fcntl(F_DUPFD).
221 */
222 static int
223 finishdup(struct lwp *l, int old, int new, register_t *retval)
224 {
225 struct filedesc *fdp;
226 struct file *fp, *delfp;
227
228 fdp = l->l_proc->p_fd;
229
230 /*
231 * If there is a file in the new slot, remember it so we
232 * can close it after we've finished the dup. We need
233 * to do it after the dup is finished, since closing
234 * the file may block.
235 *
236 * Note: `old' is already used for us.
237 * Note: Caller already marked `new' slot "used".
238 */
239 rw_enter(&fdp->fd_lock, RW_WRITER);
240 delfp = fdp->fd_ofiles[new];
241
242 fp = fdp->fd_ofiles[old];
243 KDASSERT(fp != NULL);
244 fdp->fd_ofiles[new] = fp;
245 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
246 rw_exit(&fdp->fd_lock);
247
248 *retval = new;
249 mutex_enter(&fp->f_lock);
250 fp->f_count++;
251 FILE_UNUSE_HAVELOCK(fp, l);
252
253 if (delfp != NULL) {
254 mutex_enter(&delfp->f_lock);
255 FILE_USE(delfp);
256 if (new < fdp->fd_knlistsize)
257 knote_fdclose(l, new);
258 (void) closef(delfp, l);
259 }
260 return (0);
261 }
262
263 /*
264 * Initialize the descriptor system.
265 */
266 void
267 filedesc_init(void)
268 {
269
270 mutex_init(&filelist_lock, MUTEX_DEFAULT, IPL_NONE);
271
272 file_cache = pool_cache_init(sizeof(struct file), 0, 0, 0,
273 "filepl", NULL, IPL_NONE, NULL, NULL, NULL);
274 KASSERT(file_cache != NULL);
275
276 cwdi_cache = pool_cache_init(sizeof(struct cwdinfo), 0, 0, 0,
277 "cwdipl", NULL, IPL_NONE, cwdi_ctor, cwdi_dtor, NULL);
278 KASSERT(cwdi_cache != NULL);
279
280 filedesc0_cache = pool_cache_init(sizeof(struct filedesc0), 0, 0, 0,
281 "fdescpl", NULL, IPL_NONE, NULL, NULL, NULL);
282 KASSERT(filedesc0_cache != NULL);
283 }
284
285 /*
286 * System calls on descriptors.
287 */
288
289 /*
290 * Duplicate a file descriptor.
291 */
292 /* ARGSUSED */
293 int
294 sys_dup(struct lwp *l, void *v, register_t *retval)
295 {
296 struct sys_dup_args /* {
297 syscallarg(int) fd;
298 } */ *uap = v;
299 struct file *fp;
300 struct filedesc *fdp;
301 struct proc *p;
302 int old, new, error;
303
304 p = l->l_proc;
305 fdp = p->p_fd;
306 old = SCARG(uap, fd);
307
308 restart:
309 if ((fp = fd_getfile(fdp, old)) == NULL)
310 return (EBADF);
311
312 FILE_USE(fp);
313
314 if ((error = fdalloc(p, 0, &new)) != 0) {
315 if (error == ENOSPC) {
316 fdexpand(p);
317 FILE_UNUSE(fp, l);
318 goto restart;
319 }
320 FILE_UNUSE(fp, l);
321 return (error);
322 }
323
324 /* finishdup() will unuse the descriptors for us */
325 return (finishdup(l, old, new, retval));
326 }
327
328 /*
329 * Duplicate a file descriptor to a particular value.
330 */
331 /* ARGSUSED */
332 int
333 sys_dup2(struct lwp *l, void *v, register_t *retval)
334 {
335 struct sys_dup2_args /* {
336 syscallarg(int) from;
337 syscallarg(int) to;
338 } */ *uap = v;
339 struct file *fp;
340 struct filedesc *fdp;
341 struct proc *p;
342 int old, new, i, error;
343
344 p = l->l_proc;
345 fdp = p->p_fd;
346 old = SCARG(uap, from);
347 new = SCARG(uap, to);
348
349 restart:
350 if ((fp = fd_getfile(fdp, old)) == NULL)
351 return (EBADF);
352
353 if ((u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
354 (u_int)new >= maxfiles) {
355 mutex_exit(&fp->f_lock);
356 return (EBADF);
357 }
358
359 if (old == new) {
360 mutex_exit(&fp->f_lock);
361 *retval = new;
362 return (0);
363 }
364
365 FILE_USE(fp);
366
367 if (new >= fdp->fd_nfiles) {
368 if ((error = fdalloc(p, new, &i)) != 0) {
369 if (error == ENOSPC) {
370 fdexpand(p);
371 FILE_UNUSE(fp, l);
372 goto restart;
373 }
374 FILE_UNUSE(fp, l);
375 return (error);
376 }
377 if (new != i)
378 panic("dup2: fdalloc");
379 } else {
380 rw_enter(&fdp->fd_lock, RW_WRITER);
381 /*
382 * Mark `new' slot "used" only if it was empty.
383 */
384 if (fdp->fd_ofiles[new] == NULL)
385 fd_used(fdp, new);
386 rw_exit(&fdp->fd_lock);
387 }
388
389 /*
390 * finishdup() will close the file that's in the `new'
391 * slot, if there's one there.
392 */
393
394 /* finishdup() will unuse the descriptors for us */
395 return (finishdup(l, old, new, retval));
396 }
397
398 /*
399 * fcntl call which is being passed to the file's fs.
400 */
401 static int
402 fcntl_forfs(int fd, struct lwp *l, int cmd, void *arg)
403 {
404 struct file *fp;
405 struct filedesc *fdp;
406 int error;
407 u_int size;
408 void *data, *memp;
409 #define STK_PARAMS 128
410 char stkbuf[STK_PARAMS];
411
412 /* fd's value was validated in sys_fcntl before calling this routine */
413 fdp = l->l_proc->p_fd;
414 fp = fdp->fd_ofiles[fd];
415
416 if ((fp->f_flag & (FREAD | FWRITE)) == 0)
417 return (EBADF);
418
419 /*
420 * Interpret high order word to find amount of data to be
421 * copied to/from the user's address space.
422 */
423 size = (size_t)F_PARAM_LEN(cmd);
424 if (size > F_PARAM_MAX)
425 return (EINVAL);
426 memp = NULL;
427 if (size > sizeof(stkbuf)) {
428 memp = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
429 data = memp;
430 } else
431 data = stkbuf;
432 if (cmd & F_FSIN) {
433 if (size) {
434 error = copyin(arg, data, size);
435 if (error) {
436 if (memp)
437 free(memp, M_IOCTLOPS);
438 return (error);
439 }
440 } else
441 *(void **)data = arg;
442 } else if ((cmd & F_FSOUT) && size)
443 /*
444 * Zero the buffer so the user always
445 * gets back something deterministic.
446 */
447 memset(data, 0, size);
448 else if (cmd & F_FSVOID)
449 *(void **)data = arg;
450
451
452 error = (*fp->f_ops->fo_fcntl)(fp, cmd, data, l);
453
454 /*
455 * Copy any data to user, size was
456 * already set and checked above.
457 */
458 if (error == 0 && (cmd & F_FSOUT) && size)
459 error = copyout(data, arg, size);
460 if (memp)
461 free(memp, M_IOCTLOPS);
462 return (error);
463 }
464
465 int
466 do_fcntl_lock(struct lwp *l, int fd, int cmd, struct flock *fl)
467 {
468 struct file *fp;
469 struct vnode *vp;
470 struct proc *p = l->l_proc;
471 int error, flg;
472
473 if ((fp = fd_getfile(p->p_fd, fd)) == NULL)
474 return (EBADF);
475
476 FILE_USE(fp);
477
478 if (fp->f_type != DTYPE_VNODE) {
479 error = EINVAL;
480 goto out;
481 }
482 vp = (struct vnode *)fp->f_data;
483 if (fl->l_whence == SEEK_CUR)
484 fl->l_start += fp->f_offset;
485
486 flg = F_POSIX;
487
488 switch (cmd) {
489
490 case F_SETLKW:
491 flg |= F_WAIT;
492 /* Fall into F_SETLK */
493
494 case F_SETLK:
495 switch (fl->l_type) {
496 case F_RDLCK:
497 if ((fp->f_flag & FREAD) == 0) {
498 error = EBADF;
499 goto out;
500 }
501 p->p_flag |= PK_ADVLOCK;
502 error = VOP_ADVLOCK(vp, p, F_SETLK, fl, flg);
503 goto out;
504
505 case F_WRLCK:
506 if ((fp->f_flag & FWRITE) == 0) {
507 error = EBADF;
508 goto out;
509 }
510 p->p_flag |= PK_ADVLOCK;
511 error = VOP_ADVLOCK(vp, p, F_SETLK, fl, flg);
512 goto out;
513
514 case F_UNLCK:
515 error = VOP_ADVLOCK(vp, p, F_UNLCK, fl, F_POSIX);
516 goto out;
517
518 default:
519 error = EINVAL;
520 goto out;
521 }
522
523 case F_GETLK:
524 if (fl->l_type != F_RDLCK &&
525 fl->l_type != F_WRLCK &&
526 fl->l_type != F_UNLCK) {
527 error = EINVAL;
528 goto out;
529 }
530 error = VOP_ADVLOCK(vp, p, F_GETLK, fl, F_POSIX);
531 break;
532
533 default:
534 error = EINVAL;
535 break;
536 }
537
538 out:
539 FILE_UNUSE(fp, l);
540 return error;
541 }
542
543 /*
544 * The file control system call.
545 */
546 /* ARGSUSED */
547 int
548 sys_fcntl(struct lwp *l, void *v, register_t *retval)
549 {
550 struct sys_fcntl_args /* {
551 syscallarg(int) fd;
552 syscallarg(int) cmd;
553 syscallarg(void *) arg;
554 } */ *uap = v;
555 struct filedesc *fdp;
556 struct file *fp;
557 struct proc *p;
558 int fd, i, tmp, error, cmd, newmin;
559 struct flock fl;
560
561 p = l->l_proc;
562 fd = SCARG(uap, fd);
563 cmd = SCARG(uap, cmd);
564 fdp = p->p_fd;
565 error = 0;
566
567 switch (cmd) {
568 case F_CLOSEM:
569 if (fd < 0)
570 return EBADF;
571 while (fdp->fd_lastfile >= fd)
572 fdrelease(l, fdp->fd_lastfile);
573 return 0;
574
575 case F_MAXFD:
576 *retval = fdp->fd_lastfile;
577 return 0;
578
579 case F_SETLKW:
580 case F_SETLK:
581 case F_GETLK:
582 error = copyin(SCARG(uap, arg), &fl, sizeof(fl));
583 if (error)
584 return error;
585 error = do_fcntl_lock(l, fd, cmd, &fl);
586 if (cmd == F_GETLK && error == 0)
587 error = copyout(&fl, SCARG(uap, arg), sizeof(fl));
588 return error;
589
590 default:
591 /* Handled below */
592 break;
593 }
594
595 restart:
596 if ((fp = fd_getfile(fdp, fd)) == NULL)
597 return (EBADF);
598
599 FILE_USE(fp);
600
601 if ((cmd & F_FSCTL)) {
602 error = fcntl_forfs(fd, l, cmd, SCARG(uap, arg));
603 goto out;
604 }
605
606 switch (cmd) {
607
608 case F_DUPFD:
609 newmin = (long)SCARG(uap, arg);
610 if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
611 (u_int)newmin >= maxfiles) {
612 error = EINVAL;
613 goto out;
614 }
615 if ((error = fdalloc(p, newmin, &i)) != 0) {
616 if (error == ENOSPC) {
617 fdexpand(p);
618 FILE_UNUSE(fp, l);
619 goto restart;
620 }
621 goto out;
622 }
623
624 /* finishdup() will unuse the descriptors for us */
625 return (finishdup(l, fd, i, retval));
626
627 case F_GETFD:
628 *retval = fdp->fd_ofileflags[fd] & UF_EXCLOSE ? 1 : 0;
629 break;
630
631 case F_SETFD:
632 if ((long)SCARG(uap, arg) & 1)
633 fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
634 else
635 fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
636 break;
637
638 case F_GETFL:
639 *retval = OFLAGS(fp->f_flag);
640 break;
641
642 case F_SETFL:
643 tmp = FFLAGS((long)SCARG(uap, arg)) & FCNTLFLAGS;
644 error = (*fp->f_ops->fo_fcntl)(fp, F_SETFL, &tmp, l);
645 if (error)
646 break;
647 i = tmp ^ fp->f_flag;
648 if (i & FNONBLOCK) {
649 int flgs = tmp & FNONBLOCK;
650 error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, &flgs, l);
651 if (error)
652 goto reset_fcntl;
653 }
654 if (i & FASYNC) {
655 int flgs = tmp & FASYNC;
656 error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, &flgs, l);
657 if (error) {
658 if (i & FNONBLOCK) {
659 tmp = fp->f_flag & FNONBLOCK;
660 (void)(*fp->f_ops->fo_ioctl)(fp,
661 FIONBIO, &tmp, l);
662 }
663 goto reset_fcntl;
664 }
665 }
666 fp->f_flag = (fp->f_flag & ~FCNTLFLAGS) | tmp;
667 break;
668 reset_fcntl:
669 (void)(*fp->f_ops->fo_fcntl)(fp, F_SETFL, &fp->f_flag, l);
670 break;
671
672 case F_GETOWN:
673 error = (*fp->f_ops->fo_ioctl)(fp, FIOGETOWN, &tmp, l);
674 *retval = tmp;
675 break;
676
677 case F_SETOWN:
678 tmp = (int)(intptr_t) SCARG(uap, arg);
679 error = (*fp->f_ops->fo_ioctl)(fp, FIOSETOWN, &tmp, l);
680 break;
681
682 default:
683 error = EINVAL;
684 }
685
686 out:
687 FILE_UNUSE(fp, l);
688 return (error);
689 }
690
691 void
692 fdremove(struct filedesc *fdp, int fd)
693 {
694
695 rw_enter(&fdp->fd_lock, RW_WRITER);
696 fdp->fd_ofiles[fd] = NULL;
697 fd_unused(fdp, fd);
698 rw_exit(&fdp->fd_lock);
699 }
700
701 int
702 fdrelease(struct lwp *l, int fd)
703 {
704 struct proc *p = l->l_proc;
705 struct filedesc *fdp;
706 struct file **fpp, *fp;
707
708 fdp = p->p_fd;
709 rw_enter(&fdp->fd_lock, RW_WRITER);
710 if (fd < 0 || fd > fdp->fd_lastfile)
711 goto badf;
712 fpp = &fdp->fd_ofiles[fd];
713 fp = *fpp;
714 if (fp == NULL)
715 goto badf;
716
717 mutex_enter(&fp->f_lock);
718 if (!FILE_IS_USABLE(fp)) {
719 mutex_exit(&fp->f_lock);
720 goto badf;
721 }
722
723 FILE_USE(fp);
724
725 *fpp = NULL;
726 fdp->fd_ofileflags[fd] = 0;
727 fd_unused(fdp, fd);
728 rw_exit(&fdp->fd_lock);
729 if (fd < fdp->fd_knlistsize)
730 knote_fdclose(l, fd);
731 return (closef(fp, l));
732
733 badf:
734 rw_exit(&fdp->fd_lock);
735 return (EBADF);
736 }
737
738 /*
739 * Close a file descriptor.
740 */
741 /* ARGSUSED */
742 int
743 sys_close(struct lwp *l, void *v, register_t *retval)
744 {
745 struct sys_close_args /* {
746 syscallarg(int) fd;
747 } */ *uap = v;
748 int fd;
749 struct filedesc *fdp;
750 struct proc *p;
751
752 p = l->l_proc;
753 fd = SCARG(uap, fd);
754 fdp = p->p_fd;
755
756 #if 0
757 if (fd_getfile(fdp, fd) == NULL)
758 return (EBADF);
759 #endif
760
761 return (fdrelease(l, fd));
762 }
763
764 /*
765 * Return status information about a file descriptor.
766 * Common function for compat code.
767 */
768 int
769 do_sys_fstat(struct lwp *l, int fd, struct stat *sb)
770 {
771 struct file *fp;
772 int error;
773
774 fp = fd_getfile(l->l_proc->p_fd, fd);
775 if (fp == NULL)
776 return EBADF;
777
778 FILE_USE(fp);
779 error = (*fp->f_ops->fo_stat)(fp, sb, l);
780 FILE_UNUSE(fp, l);
781
782 return error;
783 }
784
785 /*
786 * Return status information about a file descriptor.
787 */
788 /* ARGSUSED */
789 int
790 sys___fstat30(struct lwp *l, void *v, register_t *retval)
791 {
792 struct sys___fstat30_args /* {
793 syscallarg(int) fd;
794 syscallarg(struct stat *) sb;
795 } */ *uap = v;
796 struct stat sb;
797 int error;
798
799 error = do_sys_fstat(l, SCARG(uap, fd), &sb);
800
801 if (error == 0)
802 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
803
804 return (error);
805 }
806
807 /*
808 * Return pathconf information about a file descriptor.
809 */
810 /* ARGSUSED */
811 int
812 sys_fpathconf(struct lwp *l, void *v, register_t *retval)
813 {
814 struct sys_fpathconf_args /* {
815 syscallarg(int) fd;
816 syscallarg(int) name;
817 } */ *uap = v;
818 int fd;
819 struct filedesc *fdp;
820 struct file *fp;
821 struct proc *p;
822 struct vnode *vp;
823 int error;
824
825 p = l->l_proc;
826 fd = SCARG(uap, fd);
827 fdp = p->p_fd;
828 error = 0;
829
830 if ((fp = fd_getfile(fdp, fd)) == NULL)
831 return (EBADF);
832
833 FILE_USE(fp);
834
835 switch (fp->f_type) {
836
837 case DTYPE_SOCKET:
838 case DTYPE_PIPE:
839 if (SCARG(uap, name) != _PC_PIPE_BUF)
840 error = EINVAL;
841 else
842 *retval = PIPE_BUF;
843 break;
844
845 case DTYPE_VNODE:
846 vp = (struct vnode *)fp->f_data;
847 error = VOP_PATHCONF(vp, SCARG(uap, name), retval);
848 break;
849
850 case DTYPE_KQUEUE:
851 error = EINVAL;
852 break;
853
854 default:
855 error = EOPNOTSUPP;
856 break;
857 }
858
859 FILE_UNUSE(fp, l);
860 return (error);
861 }
862
863 /*
864 * Allocate a file descriptor for the process.
865 */
866 int fdexpanded; /* XXX: what else uses this? */
867
868 int
869 fdalloc(struct proc *p, int want, int *result)
870 {
871 struct filedesc *fdp;
872 int i, lim, last, error;
873 u_int off, new;
874
875 fdp = p->p_fd;
876 rw_enter(&fdp->fd_lock, RW_WRITER);
877
878 /*
879 * Search for a free descriptor starting at the higher
880 * of want or fd_freefile. If that fails, consider
881 * expanding the ofile array.
882 */
883 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
884 last = min(fdp->fd_nfiles, lim);
885 again:
886 if ((i = want) < fdp->fd_freefile)
887 i = fdp->fd_freefile;
888 off = i >> NDENTRYSHIFT;
889 new = find_next_zero(fdp->fd_himap, off,
890 (last + NDENTRIES - 1) >> NDENTRYSHIFT);
891 if (new != -1) {
892 i = find_next_zero(&fdp->fd_lomap[new],
893 new > off ? 0 : i & NDENTRYMASK, NDENTRIES);
894 if (i == -1) {
895 /*
896 * free file descriptor in this block was
897 * below want, try again with higher want.
898 */
899 want = (new + 1) << NDENTRYSHIFT;
900 goto again;
901 }
902 i += (new << NDENTRYSHIFT);
903 if (i < last) {
904 if (fdp->fd_ofiles[i] == NULL) {
905 fd_used(fdp, i);
906 if (want <= fdp->fd_freefile)
907 fdp->fd_freefile = i;
908 *result = i;
909 error = 0;
910 goto out;
911 }
912 }
913 }
914
915 /* No space in current array. Expand or let the caller do it. */
916 error = (fdp->fd_nfiles >= lim) ? EMFILE : ENOSPC;
917
918 out:
919 rw_exit(&fdp->fd_lock);
920 return (error);
921 }
922
923 void
924 fdexpand(struct proc *p)
925 {
926 struct filedesc *fdp;
927 int i, numfiles, oldnfiles;
928 struct file **newofile;
929 char *newofileflags;
930 uint32_t *newhimap = NULL, *newlomap = NULL;
931
932 fdp = p->p_fd;
933
934 restart:
935 oldnfiles = fdp->fd_nfiles;
936
937 if (oldnfiles < NDEXTENT)
938 numfiles = NDEXTENT;
939 else
940 numfiles = 2 * oldnfiles;
941
942 newofile = malloc(numfiles * OFILESIZE, M_FILEDESC, M_WAITOK);
943 if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
944 newhimap = malloc(NDHISLOTS(numfiles) * sizeof(uint32_t),
945 M_FILEDESC, M_WAITOK);
946 newlomap = malloc(NDLOSLOTS(numfiles) * sizeof(uint32_t),
947 M_FILEDESC, M_WAITOK);
948 }
949
950 rw_enter(&fdp->fd_lock, RW_WRITER);
951 /* lock fdp */
952 if (fdp->fd_nfiles != oldnfiles) {
953 /* fdp changed; retry */
954 rw_exit(&fdp->fd_lock);
955 free(newofile, M_FILEDESC);
956 if (newhimap != NULL) free(newhimap, M_FILEDESC);
957 if (newlomap != NULL) free(newlomap, M_FILEDESC);
958 goto restart;
959 }
960
961 newofileflags = (char *) &newofile[numfiles];
962 /*
963 * Copy the existing ofile and ofileflags arrays
964 * and zero the new portion of each array.
965 */
966 memcpy(newofile, fdp->fd_ofiles,
967 (i = sizeof(struct file *) * fdp->fd_nfiles));
968 memset((char *)newofile + i, 0,
969 numfiles * sizeof(struct file *) - i);
970 memcpy(newofileflags, fdp->fd_ofileflags,
971 (i = sizeof(char) * fdp->fd_nfiles));
972 memset(newofileflags + i, 0, numfiles * sizeof(char) - i);
973 if (oldnfiles > NDFILE)
974 free(fdp->fd_ofiles, M_FILEDESC);
975
976 if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
977 memcpy(newhimap, fdp->fd_himap,
978 (i = NDHISLOTS(oldnfiles) * sizeof(uint32_t)));
979 memset((char *)newhimap + i, 0,
980 NDHISLOTS(numfiles) * sizeof(uint32_t) - i);
981
982 memcpy(newlomap, fdp->fd_lomap,
983 (i = NDLOSLOTS(oldnfiles) * sizeof(uint32_t)));
984 memset((char *)newlomap + i, 0,
985 NDLOSLOTS(numfiles) * sizeof(uint32_t) - i);
986
987 if (NDHISLOTS(oldnfiles) > NDHISLOTS(NDFILE)) {
988 free(fdp->fd_himap, M_FILEDESC);
989 free(fdp->fd_lomap, M_FILEDESC);
990 }
991 fdp->fd_himap = newhimap;
992 fdp->fd_lomap = newlomap;
993 }
994
995 fdp->fd_ofiles = newofile;
996 fdp->fd_ofileflags = newofileflags;
997 fdp->fd_nfiles = numfiles;
998
999 rw_exit(&fdp->fd_lock);
1000
1001 fdexpanded++;
1002 }
1003
1004 /*
1005 * Create a new open file structure and allocate
1006 * a file descriptor for the process that refers to it.
1007 */
1008 int
1009 falloc(struct lwp *l, struct file **resultfp, int *resultfd)
1010 {
1011 struct filedesc *fdp;
1012 struct file *fp, *fq;
1013 struct proc *p;
1014 int error, i;
1015
1016 p = l->l_proc;
1017 fdp = p->p_fd;
1018
1019 restart:
1020 if ((error = fdalloc(p, 0, &i)) != 0) {
1021 if (error == ENOSPC) {
1022 fdexpand(p);
1023 goto restart;
1024 }
1025 return (error);
1026 }
1027
1028 fp = pool_cache_get(file_cache, PR_WAITOK);
1029 memset(fp, 0, sizeof(struct file));
1030 mutex_init(&fp->f_lock, MUTEX_DEFAULT, IPL_NONE);
1031 mutex_enter(&filelist_lock);
1032 if (nfiles >= maxfiles) {
1033 tablefull("file", "increase kern.maxfiles or MAXFILES");
1034 mutex_exit(&filelist_lock);
1035 rw_enter(&fdp->fd_lock, RW_WRITER);
1036 fd_unused(fdp, i);
1037 rw_exit(&fdp->fd_lock);
1038 mutex_destroy(&fp->f_lock);
1039 pool_cache_put(file_cache, fp);
1040 return (ENFILE);
1041 }
1042 /*
1043 * Allocate a new file descriptor.
1044 * If the process has file descriptor zero open, add to the list
1045 * of open files at that point, otherwise put it at the front of
1046 * the list of open files.
1047 */
1048 nfiles++;
1049 fp->f_iflags = FIF_LARVAL;
1050 cv_init(&fp->f_cv, "closef");
1051 rw_enter(&fdp->fd_lock, RW_WRITER); /* XXXAD check order */
1052 if ((fq = fdp->fd_ofiles[0]) != NULL) {
1053 LIST_INSERT_AFTER(fq, fp, f_list);
1054 } else {
1055 LIST_INSERT_HEAD(&filehead, fp, f_list);
1056 }
1057 KDASSERT(fdp->fd_ofiles[i] == NULL);
1058 fdp->fd_ofiles[i] = fp;
1059 fp->f_count = 1;
1060 fp->f_cred = l->l_cred;
1061 kauth_cred_hold(fp->f_cred);
1062 if (resultfp) {
1063 fp->f_usecount = 1;
1064 *resultfp = fp;
1065 }
1066 mutex_exit(&filelist_lock);
1067 rw_exit(&fdp->fd_lock);
1068 if (resultfd)
1069 *resultfd = i;
1070
1071 return (0);
1072 }
1073
1074 /*
1075 * Free a file descriptor.
1076 */
1077 void
1078 ffree(struct file *fp)
1079 {
1080 kauth_cred_t cred;
1081
1082 #ifdef DIAGNOSTIC
1083 if (fp->f_usecount)
1084 panic("ffree");
1085 #endif
1086
1087 mutex_enter(&filelist_lock);
1088 LIST_REMOVE(fp, f_list);
1089 cred = fp->f_cred;
1090 #ifdef DIAGNOSTIC
1091 fp->f_cred = NULL;
1092 fp->f_count = 0; /* What's the point? */
1093 #endif
1094 nfiles--;
1095 mutex_exit(&filelist_lock);
1096 mutex_destroy(&fp->f_lock);
1097 cv_destroy(&fp->f_cv);
1098 pool_cache_put(file_cache, fp);
1099 kauth_cred_free(cred);
1100 }
1101
1102 /*
1103 * Create an initial cwdinfo structure, using the same current and root
1104 * directories as p.
1105 */
1106 struct cwdinfo *
1107 cwdinit(struct proc *p)
1108 {
1109 struct cwdinfo *cwdi;
1110 struct cwdinfo *copy;
1111
1112 cwdi = pool_cache_get(cwdi_cache, PR_WAITOK);
1113 copy = p->p_cwdi;
1114
1115 rw_enter(©->cwdi_lock, RW_READER);
1116 cwdi->cwdi_cdir = p->p_cwdi->cwdi_cdir;
1117 if (cwdi->cwdi_cdir)
1118 VREF(cwdi->cwdi_cdir);
1119 cwdi->cwdi_rdir = p->p_cwdi->cwdi_rdir;
1120 if (cwdi->cwdi_rdir)
1121 VREF(cwdi->cwdi_rdir);
1122 cwdi->cwdi_edir = p->p_cwdi->cwdi_edir;
1123 if (cwdi->cwdi_edir)
1124 VREF(cwdi->cwdi_edir);
1125 cwdi->cwdi_cmask = p->p_cwdi->cwdi_cmask;
1126 cwdi->cwdi_refcnt = 1;
1127 rw_exit(©->cwdi_lock);
1128
1129 return (cwdi);
1130 }
1131
1132 static int
1133 cwdi_ctor(void *arg, void *obj, int flags)
1134 {
1135 struct cwdinfo *cwdi;
1136
1137 cwdi = obj;
1138 rw_init(&cwdi->cwdi_lock);
1139
1140 return 0;
1141 }
1142
1143 static void
1144 cwdi_dtor(void *arg, void *obj)
1145 {
1146 struct cwdinfo *cwdi;
1147
1148 cwdi = obj;
1149 rw_destroy(&cwdi->cwdi_lock);
1150 }
1151
1152 /*
1153 * Make p2 share p1's cwdinfo.
1154 */
1155 void
1156 cwdshare(struct proc *p1, struct proc *p2)
1157 {
1158 struct cwdinfo *cwdi = p1->p_cwdi;
1159
1160 atomic_inc_uint(&cwdi->cwdi_refcnt);
1161 p2->p_cwdi = cwdi;
1162 }
1163
1164 /*
1165 * Make this process not share its cwdinfo structure, maintaining
1166 * all cwdinfo state.
1167 */
1168 void
1169 cwdunshare(struct proc *p)
1170 {
1171 struct cwdinfo *oldcwdi, *newcwdi;
1172
1173 if (p->p_cwdi->cwdi_refcnt == 1)
1174 return;
1175
1176 newcwdi = cwdinit(p);
1177 oldcwdi = p->p_cwdi;
1178 p->p_cwdi = newcwdi;
1179 cwdfree(oldcwdi);
1180 }
1181
1182 /*
1183 * Release a cwdinfo structure.
1184 */
1185 void
1186 cwdfree(struct cwdinfo *cwdi)
1187 {
1188
1189 if (atomic_dec_uint_nv(&cwdi->cwdi_refcnt) > 0)
1190 return;
1191
1192 vrele(cwdi->cwdi_cdir);
1193 if (cwdi->cwdi_rdir)
1194 vrele(cwdi->cwdi_rdir);
1195 if (cwdi->cwdi_edir)
1196 vrele(cwdi->cwdi_edir);
1197 pool_cache_put(cwdi_cache, cwdi);
1198 }
1199
1200 /*
1201 * Create an initial filedesc structure, using the same current and root
1202 * directories as p.
1203 */
1204 struct filedesc *
1205 fdinit(struct proc *p)
1206 {
1207 struct filedesc0 *newfdp;
1208
1209 newfdp = pool_cache_get(filedesc0_cache, PR_WAITOK);
1210 memset(newfdp, 0, sizeof(struct filedesc0));
1211
1212 fdinit1(newfdp);
1213
1214 return (&newfdp->fd_fd);
1215 }
1216
1217 /*
1218 * Initialize a file descriptor table.
1219 */
1220 void
1221 fdinit1(struct filedesc0 *newfdp)
1222 {
1223
1224 newfdp->fd_fd.fd_refcnt = 1;
1225 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1226 newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1227 newfdp->fd_fd.fd_nfiles = NDFILE;
1228 newfdp->fd_fd.fd_knlistsize = -1;
1229 newfdp->fd_fd.fd_himap = newfdp->fd_dhimap;
1230 newfdp->fd_fd.fd_lomap = newfdp->fd_dlomap;
1231 newfdp->fd_fd.fd_lastfile = -1;
1232 rw_init(&newfdp->fd_fd.fd_lock);
1233 }
1234
1235 /*
1236 * Make p2 share p1's filedesc structure.
1237 */
1238 void
1239 fdshare(struct proc *p1, struct proc *p2)
1240 {
1241 struct filedesc *fdp = p1->p_fd;
1242
1243 rw_enter(&fdp->fd_lock, RW_WRITER);
1244 p2->p_fd = fdp;
1245 fdp->fd_refcnt++;
1246 rw_exit(&fdp->fd_lock);
1247 }
1248
1249 /*
1250 * Make this process not share its filedesc structure, maintaining
1251 * all file descriptor state.
1252 */
1253 void
1254 fdunshare(struct lwp *l)
1255 {
1256 struct proc *p = l->l_proc;
1257 struct filedesc *newfd;
1258
1259 if (p->p_fd->fd_refcnt == 1)
1260 return;
1261
1262 newfd = fdcopy(p);
1263 fdfree(l);
1264 p->p_fd = newfd;
1265 }
1266
1267 /*
1268 * Clear a process's fd table.
1269 */
1270 void
1271 fdclear(struct lwp *l)
1272 {
1273 struct proc *p = l->l_proc;
1274 struct filedesc *newfd;
1275
1276 newfd = fdinit(p);
1277 fdfree(l);
1278 p->p_fd = newfd;
1279 }
1280
1281 /*
1282 * Copy a filedesc structure.
1283 */
1284 struct filedesc *
1285 fdcopy(struct proc *p)
1286 {
1287 struct filedesc *newfdp, *fdp;
1288 struct file **fpp, **nfpp;
1289 int i, numfiles, lastfile;
1290
1291 fdp = p->p_fd;
1292 newfdp = pool_cache_get(filedesc0_cache, PR_WAITOK);
1293 newfdp->fd_refcnt = 1;
1294 rw_init(&newfdp->fd_lock);
1295
1296 restart:
1297 numfiles = fdp->fd_nfiles;
1298 lastfile = fdp->fd_lastfile;
1299
1300 /*
1301 * If the number of open files fits in the internal arrays
1302 * of the open file structure, use them, otherwise allocate
1303 * additional memory for the number of descriptors currently
1304 * in use.
1305 */
1306 if (lastfile < NDFILE) {
1307 i = NDFILE;
1308 } else {
1309 /*
1310 * Compute the smallest multiple of NDEXTENT needed
1311 * for the file descriptors currently in use,
1312 * allowing the table to shrink.
1313 */
1314 i = numfiles;
1315 while (i >= 2 * NDEXTENT && i > lastfile * 2)
1316 i /= 2;
1317 newfdp->fd_ofiles = malloc(i * OFILESIZE, M_FILEDESC, M_WAITOK);
1318 }
1319 if (NDHISLOTS(i) > NDHISLOTS(NDFILE)) {
1320 newfdp->fd_himap = malloc(NDHISLOTS(i) * sizeof(uint32_t),
1321 M_FILEDESC, M_WAITOK);
1322 newfdp->fd_lomap = malloc(NDLOSLOTS(i) * sizeof(uint32_t),
1323 M_FILEDESC, M_WAITOK);
1324 }
1325
1326 rw_enter(&fdp->fd_lock, RW_READER);
1327 if (numfiles != fdp->fd_nfiles || lastfile != fdp->fd_lastfile) {
1328 rw_exit(&fdp->fd_lock);
1329 if (i > NDFILE)
1330 free(newfdp->fd_ofiles, M_FILEDESC);
1331 if (NDHISLOTS(i) > NDHISLOTS(NDFILE)) {
1332 free(newfdp->fd_himap, M_FILEDESC);
1333 free(newfdp->fd_lomap, M_FILEDESC);
1334 }
1335 goto restart;
1336 }
1337
1338 if (lastfile < NDFILE) {
1339 newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
1340 newfdp->fd_ofileflags =
1341 ((struct filedesc0 *) newfdp)->fd_dfileflags;
1342 } else {
1343 newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
1344 }
1345 if (NDHISLOTS(i) <= NDHISLOTS(NDFILE)) {
1346 newfdp->fd_himap =
1347 ((struct filedesc0 *) newfdp)->fd_dhimap;
1348 newfdp->fd_lomap =
1349 ((struct filedesc0 *) newfdp)->fd_dlomap;
1350 }
1351
1352 newfdp->fd_nfiles = i;
1353 newfdp->fd_lastfile = lastfile;
1354 newfdp->fd_freefile = fdp->fd_freefile;
1355
1356 /* Clear the entries that will not be copied over.
1357 * Avoid calling memset with 0 size (i.e. when
1358 * lastfile == i-1 */
1359 if (lastfile < (i-1))
1360 memset(newfdp->fd_ofiles + lastfile + 1, 0,
1361 (i - lastfile - 1) * sizeof(struct file **));
1362 memcpy(newfdp->fd_ofileflags, fdp->fd_ofileflags, i * sizeof(char));
1363 if (i < NDENTRIES * NDENTRIES)
1364 i = NDENTRIES * NDENTRIES; /* size of inlined bitmaps */
1365 memcpy(newfdp->fd_himap, fdp->fd_himap, NDHISLOTS(i)*sizeof(uint32_t));
1366 memcpy(newfdp->fd_lomap, fdp->fd_lomap, NDLOSLOTS(i)*sizeof(uint32_t));
1367
1368 fpp = fdp->fd_ofiles;
1369 nfpp = newfdp->fd_ofiles;
1370 for (i = 0; i <= lastfile; i++, fpp++, nfpp++) {
1371 if ((*nfpp = *fpp) == NULL)
1372 continue;
1373
1374 if ((*fpp)->f_type == DTYPE_KQUEUE)
1375 /* kq descriptors cannot be copied. */
1376 fdremove(newfdp, i);
1377 else {
1378 mutex_enter(&(*fpp)->f_lock);
1379 (*fpp)->f_count++;
1380 mutex_exit(&(*fpp)->f_lock);
1381 }
1382 }
1383
1384 rw_exit(&fdp->fd_lock);
1385
1386 newfdp->fd_knlist = NULL;
1387 newfdp->fd_knlistsize = -1;
1388 newfdp->fd_knhash = NULL;
1389 newfdp->fd_knhashmask = 0;
1390
1391 return (newfdp);
1392 }
1393
1394 /*
1395 * Release a filedesc structure.
1396 */
1397 void
1398 fdfree(struct lwp *l)
1399 {
1400 struct proc *p = l->l_proc;
1401 struct filedesc *fdp;
1402 struct file **fpp, *fp;
1403 int i;
1404
1405 fdp = p->p_fd;
1406 rw_enter(&fdp->fd_lock, RW_WRITER);
1407 i = --fdp->fd_refcnt;
1408 rw_exit(&fdp->fd_lock);
1409 if (i > 0)
1410 return;
1411
1412 rw_destroy(&fdp->fd_lock);
1413 fpp = fdp->fd_ofiles;
1414 for (i = fdp->fd_lastfile; i >= 0; i--, fpp++) {
1415 fp = *fpp;
1416 if (fp != NULL) {
1417 *fpp = NULL;
1418 mutex_enter(&fp->f_lock);
1419 FILE_USE(fp);
1420 if ((fdp->fd_lastfile - i) < fdp->fd_knlistsize)
1421 knote_fdclose(l, fdp->fd_lastfile - i);
1422 (void) closef(fp, l);
1423 }
1424 }
1425 p->p_fd = NULL;
1426 if (fdp->fd_nfiles > NDFILE)
1427 free(fdp->fd_ofiles, M_FILEDESC);
1428 if (NDHISLOTS(fdp->fd_nfiles) > NDHISLOTS(NDFILE)) {
1429 free(fdp->fd_himap, M_FILEDESC);
1430 free(fdp->fd_lomap, M_FILEDESC);
1431 }
1432 if (fdp->fd_knlist)
1433 free(fdp->fd_knlist, M_KEVENT);
1434 if (fdp->fd_knhash)
1435 hashdone(fdp->fd_knhash, M_KEVENT);
1436 pool_cache_put(filedesc0_cache, fdp);
1437 }
1438
1439 /*
1440 * Internal form of close.
1441 * Decrement reference count on file structure.
1442 * Note: p may be NULL when closing a file
1443 * that was being passed in a message.
1444 *
1445 * Note: we expect the caller is holding a usecount, and expects us
1446 * to drop it (the caller thinks the file is going away forever).
1447 */
1448 int
1449 closef(struct file *fp, struct lwp *l)
1450 {
1451 struct proc *p = l ? l->l_proc : NULL;
1452 struct vnode *vp;
1453 struct flock lf;
1454 int error;
1455
1456 if (fp == NULL)
1457 return (0);
1458
1459 /*
1460 * POSIX record locking dictates that any close releases ALL
1461 * locks owned by this process. This is handled by setting
1462 * a flag in the unlock to free ONLY locks obeying POSIX
1463 * semantics, and not to free BSD-style file locks.
1464 * If the descriptor was in a message, POSIX-style locks
1465 * aren't passed with the descriptor.
1466 */
1467 if (p && (p->p_flag & PK_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
1468 lf.l_whence = SEEK_SET;
1469 lf.l_start = 0;
1470 lf.l_len = 0;
1471 lf.l_type = F_UNLCK;
1472 vp = (struct vnode *)fp->f_data;
1473 (void) VOP_ADVLOCK(vp, p, F_UNLCK, &lf, F_POSIX);
1474 }
1475
1476 /*
1477 * If WANTCLOSE is set, then the reference count on the file
1478 * is 0, but there were multiple users of the file. This can
1479 * happen if a filedesc structure is shared by multiple
1480 * processes.
1481 */
1482 mutex_enter(&fp->f_lock);
1483 if (fp->f_iflags & FIF_WANTCLOSE) {
1484 /*
1485 * Another user of the file is already closing, and is
1486 * simply waiting for other users of the file to drain.
1487 * Release our usecount, and wake up the closer if it
1488 * is the only remaining use.
1489 */
1490 #ifdef DIAGNOSTIC
1491 if (fp->f_count != 0)
1492 panic("closef: wantclose and count != 0");
1493 if (fp->f_usecount < 2)
1494 panic("closef: wantclose and usecount < 2");
1495 #endif
1496 if (--fp->f_usecount == 1)
1497 cv_broadcast(&fp->f_cv);
1498 mutex_exit(&fp->f_lock);
1499 return (0);
1500 } else {
1501 /*
1502 * Decrement the reference count. If we were not the
1503 * last reference, then release our use and just
1504 * return.
1505 */
1506 if (--fp->f_count > 0) {
1507 #ifdef DIAGNOSTIC
1508 if (fp->f_usecount < 1)
1509 panic("closef: no wantclose and usecount < 1");
1510 #endif
1511 fp->f_usecount--;
1512 mutex_exit(&fp->f_lock);
1513 return (0);
1514 }
1515 }
1516
1517 /*
1518 * The reference count is now 0. However, there may be
1519 * multiple potential users of this file. This can happen
1520 * if multiple processes shared a single filedesc structure.
1521 *
1522 * Notify these potential users that the file is closing.
1523 * This will prevent them from adding additional uses to
1524 * the file.
1525 */
1526 fp->f_iflags |= FIF_WANTCLOSE;
1527
1528 /*
1529 * We expect the caller to add a use to the file. So, if we
1530 * are the last user, usecount will be 1. If it is not, we
1531 * must wait for the usecount to drain. When it drains back
1532 * to 1, we will be awakened so that we may proceed with the
1533 * close.
1534 */
1535 #ifdef DIAGNOSTIC
1536 if (fp->f_usecount < 1)
1537 panic("closef: usecount < 1");
1538 #endif
1539 while (fp->f_usecount > 1)
1540 cv_wait(&fp->f_cv, &fp->f_lock);
1541 #ifdef DIAGNOSTIC
1542 if (fp->f_usecount != 1)
1543 panic("closef: usecount != 1");
1544 #endif
1545
1546 mutex_exit(&fp->f_lock);
1547 if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
1548 lf.l_whence = SEEK_SET;
1549 lf.l_start = 0;
1550 lf.l_len = 0;
1551 lf.l_type = F_UNLCK;
1552 vp = (struct vnode *)fp->f_data;
1553 (void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
1554 }
1555 if (fp->f_ops)
1556 error = (*fp->f_ops->fo_close)(fp, l);
1557 else
1558 error = 0;
1559
1560 /* Nothing references the file now, drop the final use (us). */
1561 fp->f_usecount--;
1562
1563 ffree(fp);
1564 return (error);
1565 }
1566
1567 /*
1568 * Apply an advisory lock on a file descriptor.
1569 *
1570 * Just attempt to get a record lock of the requested type on
1571 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1572 */
1573 /* ARGSUSED */
1574 int
1575 sys_flock(struct lwp *l, void *v, register_t *retval)
1576 {
1577 struct sys_flock_args /* {
1578 syscallarg(int) fd;
1579 syscallarg(int) how;
1580 } */ *uap = v;
1581 int fd, how, error;
1582 struct proc *p;
1583 struct filedesc *fdp;
1584 struct file *fp;
1585 struct vnode *vp;
1586 struct flock lf;
1587
1588 p = l->l_proc;
1589 fd = SCARG(uap, fd);
1590 how = SCARG(uap, how);
1591 fdp = p->p_fd;
1592 error = 0;
1593
1594 if ((fp = fd_getfile(fdp, fd)) == NULL)
1595 return (EBADF);
1596
1597 FILE_USE(fp);
1598
1599 if (fp->f_type != DTYPE_VNODE) {
1600 error = EOPNOTSUPP;
1601 goto out;
1602 }
1603
1604 vp = (struct vnode *)fp->f_data;
1605 lf.l_whence = SEEK_SET;
1606 lf.l_start = 0;
1607 lf.l_len = 0;
1608 if (how & LOCK_UN) {
1609 lf.l_type = F_UNLCK;
1610 fp->f_flag &= ~FHASLOCK;
1611 error = VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
1612 goto out;
1613 }
1614 if (how & LOCK_EX)
1615 lf.l_type = F_WRLCK;
1616 else if (how & LOCK_SH)
1617 lf.l_type = F_RDLCK;
1618 else {
1619 error = EINVAL;
1620 goto out;
1621 }
1622 fp->f_flag |= FHASLOCK;
1623 if (how & LOCK_NB)
1624 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, F_FLOCK);
1625 else
1626 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf,
1627 F_FLOCK|F_WAIT);
1628 out:
1629 FILE_UNUSE(fp, l);
1630 return (error);
1631 }
1632
1633 /* ARGSUSED */
1634 int
1635 sys_posix_fadvise(struct lwp *l, void *v, register_t *retval)
1636 {
1637 const struct sys_posix_fadvise_args /* {
1638 syscallarg(int) fd;
1639 syscallarg(off_t) offset;
1640 syscallarg(off_t) len;
1641 syscallarg(int) advice;
1642 } */ *uap = v;
1643 const int fd = SCARG(uap, fd);
1644 const int advice = SCARG(uap, advice);
1645 struct proc *p = l->l_proc;
1646 struct file *fp;
1647 int error = 0;
1648
1649 fp = fd_getfile(p->p_fd, fd);
1650 if (fp == NULL) {
1651 error = EBADF;
1652 goto out;
1653 }
1654 FILE_USE(fp);
1655
1656 if (fp->f_type != DTYPE_VNODE) {
1657 if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
1658 error = ESPIPE;
1659 } else {
1660 error = EOPNOTSUPP;
1661 }
1662 goto out;
1663 }
1664
1665 switch (advice) {
1666 case POSIX_FADV_NORMAL:
1667 case POSIX_FADV_RANDOM:
1668 case POSIX_FADV_SEQUENTIAL:
1669 KASSERT(POSIX_FADV_NORMAL == UVM_ADV_NORMAL);
1670 KASSERT(POSIX_FADV_RANDOM == UVM_ADV_RANDOM);
1671 KASSERT(POSIX_FADV_SEQUENTIAL == UVM_ADV_SEQUENTIAL);
1672
1673 /*
1674 * we ignore offset and size.
1675 */
1676
1677 fp->f_advice = advice;
1678 break;
1679
1680 case POSIX_FADV_WILLNEED:
1681 case POSIX_FADV_DONTNEED:
1682 case POSIX_FADV_NOREUSE:
1683
1684 /*
1685 * not implemented yet.
1686 */
1687
1688 break;
1689 default:
1690 error = EINVAL;
1691 break;
1692 }
1693 out:
1694 if (fp != NULL) {
1695 FILE_UNUSE(fp, l);
1696 }
1697 *retval = error;
1698 return 0;
1699 }
1700
1701 /*
1702 * File Descriptor pseudo-device driver (/dev/fd/).
1703 *
1704 * Opening minor device N dup()s the file (if any) connected to file
1705 * descriptor N belonging to the calling process. Note that this driver
1706 * consists of only the ``open()'' routine, because all subsequent
1707 * references to this file will be direct to the other driver.
1708 */
1709 /* ARGSUSED */
1710 static int
1711 filedescopen(dev_t dev, int mode, int type, struct lwp *l)
1712 {
1713
1714 /*
1715 * XXX Kludge: set dupfd to contain the value of the
1716 * the file descriptor being sought for duplication. The error
1717 * return ensures that the vnode for this device will be released
1718 * by vn_open. Open will detect this special error and take the
1719 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1720 * will simply report the error.
1721 */
1722 l->l_dupfd = minor(dev); /* XXX */
1723 return EDUPFD;
1724 }
1725
1726 const struct cdevsw filedesc_cdevsw = {
1727 filedescopen, noclose, noread, nowrite, noioctl,
1728 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
1729 };
1730
1731 /*
1732 * Duplicate the specified descriptor to a free descriptor.
1733 *
1734 * 'indx' has been fdalloc'ed (and will be fdremove'ed on error) by the caller.
1735 */
1736 int
1737 dupfdopen(struct lwp *l, int indx, int dfd, int mode, int error)
1738 {
1739 struct proc *p = l->l_proc;
1740 struct filedesc *fdp;
1741 struct file *wfp;
1742
1743 fdp = p->p_fd;
1744
1745 /* should be cleared by the caller */
1746 KASSERT(fdp->fd_ofiles[indx] == NULL);
1747
1748 /*
1749 * If the to-be-dup'd fd number is greater than the allowed number
1750 * of file descriptors, or the fd to be dup'd has already been
1751 * closed, reject.
1752 */
1753
1754 /*
1755 * Note, in the case of indx == dfd, fd_getfile below returns NULL.
1756 */
1757 if ((wfp = fd_getfile(fdp, dfd)) == NULL)
1758 return (EBADF);
1759
1760 FILE_USE(wfp);
1761
1762 /*
1763 * There are two cases of interest here.
1764 *
1765 * For EDUPFD simply dup (dfd) to file descriptor
1766 * (indx) and return.
1767 *
1768 * For EMOVEFD steal away the file structure from (dfd) and
1769 * store it in (indx). (dfd) is effectively closed by
1770 * this operation.
1771 *
1772 * Any other error code is just returned.
1773 */
1774 switch (error) {
1775 case EDUPFD:
1776 /*
1777 * Check that the mode the file is being opened for is a
1778 * subset of the mode of the existing descriptor.
1779 */
1780 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
1781 FILE_UNUSE(wfp, l);
1782 return (EACCES);
1783 }
1784 rw_enter(&fdp->fd_lock, RW_WRITER);
1785 fdp->fd_ofiles[indx] = wfp;
1786 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1787 rw_exit(&fdp->fd_lock);
1788 mutex_enter(&wfp->f_lock);
1789 wfp->f_count++;
1790 /* 'indx' has been fd_used'ed by caller */
1791 FILE_UNUSE_HAVELOCK(wfp, l);
1792 return (0);
1793
1794 case EMOVEFD:
1795 /*
1796 * Steal away the file pointer from dfd, and stuff it into indx.
1797 */
1798 rw_enter(&fdp->fd_lock, RW_WRITER);
1799 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
1800 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1801 fdp->fd_ofiles[dfd] = NULL;
1802 fdp->fd_ofileflags[dfd] = 0;
1803 /*
1804 * Complete the clean up of the filedesc structure by
1805 * recomputing the various hints.
1806 */
1807 /* 'indx' has been fd_used'ed by caller */
1808 fd_unused(fdp, dfd);
1809 rw_exit(&fdp->fd_lock);
1810 FILE_UNUSE(wfp, l);
1811 return (0);
1812
1813 default:
1814 FILE_UNUSE(wfp, l);
1815 return (error);
1816 }
1817 /* NOTREACHED */
1818 }
1819
1820 /*
1821 * Close any files on exec?
1822 */
1823 void
1824 fdcloseexec(struct lwp *l)
1825 {
1826 struct proc *p = l->l_proc;
1827 struct filedesc *fdp;
1828 int fd;
1829
1830 fdunshare(l);
1831 cwdunshare(p);
1832
1833 if (p->p_cwdi->cwdi_edir)
1834 vrele(p->p_cwdi->cwdi_edir);
1835
1836 fdp = p->p_fd;
1837 for (fd = 0; fd <= fdp->fd_lastfile; fd++)
1838 if (fdp->fd_ofileflags[fd] & UF_EXCLOSE)
1839 (void) fdrelease(l, fd);
1840 }
1841
1842 /*
1843 * It is unsafe for set[ug]id processes to be started with file
1844 * descriptors 0..2 closed, as these descriptors are given implicit
1845 * significance in the Standard C library. fdcheckstd() will create a
1846 * descriptor referencing /dev/null for each of stdin, stdout, and
1847 * stderr that is not already open.
1848 */
1849 #define CHECK_UPTO 3
1850 int
1851 fdcheckstd(struct lwp *l)
1852 {
1853 struct proc *p;
1854 struct nameidata nd;
1855 struct filedesc *fdp;
1856 struct file *fp;
1857 struct file *devnullfp = NULL; /* Quell compiler warning */
1858 struct proc *pp;
1859 register_t retval;
1860 int fd, i, error, flags = FREAD|FWRITE, devnull = -1;
1861 char closed[CHECK_UPTO * 3 + 1], which[3 + 1];
1862
1863 p = l->l_proc;
1864 closed[0] = '\0';
1865 if ((fdp = p->p_fd) == NULL)
1866 return (0);
1867 for (i = 0; i < CHECK_UPTO; i++) {
1868 if (fdp->fd_ofiles[i] != NULL)
1869 continue;
1870 snprintf(which, sizeof(which), ",%d", i);
1871 strlcat(closed, which, sizeof(closed));
1872 if (devnullfp == NULL) {
1873 if ((error = falloc(l, &fp, &fd)) != 0)
1874 return (error);
1875 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/null",
1876 l);
1877 if ((error = vn_open(&nd, flags, 0)) != 0) {
1878 FILE_UNUSE(fp, l);
1879 ffree(fp);
1880 fdremove(p->p_fd, fd);
1881 return (error);
1882 }
1883 fp->f_data = nd.ni_vp;
1884 fp->f_flag = flags;
1885 fp->f_ops = &vnops;
1886 fp->f_type = DTYPE_VNODE;
1887 VOP_UNLOCK(nd.ni_vp, 0);
1888 devnull = fd;
1889 devnullfp = fp;
1890 FILE_SET_MATURE(fp);
1891 } else {
1892 restart:
1893 if ((error = fdalloc(p, 0, &fd)) != 0) {
1894 if (error == ENOSPC) {
1895 fdexpand(p);
1896 goto restart;
1897 }
1898 return (error);
1899 }
1900
1901 mutex_enter(&devnullfp->f_lock);
1902 FILE_USE(devnullfp);
1903 /* finishdup() will unuse the descriptors for us */
1904 if ((error = finishdup(l, devnull, fd, &retval)) != 0)
1905 return (error);
1906 }
1907 }
1908 if (devnullfp)
1909 FILE_UNUSE(devnullfp, l);
1910 if (closed[0] != '\0') {
1911 mutex_enter(&proclist_lock);
1912 pp = p->p_pptr;
1913 mutex_enter(&pp->p_mutex);
1914 log(LOG_WARNING, "set{u,g}id pid %d (%s) "
1915 "was invoked by uid %d ppid %d (%s) "
1916 "with fd %s closed\n",
1917 p->p_pid, p->p_comm, kauth_cred_geteuid(pp->p_cred),
1918 pp->p_pid, pp->p_comm, &closed[1]);
1919 mutex_exit(&pp->p_mutex);
1920 mutex_exit(&proclist_lock);
1921 }
1922 return (0);
1923 }
1924 #undef CHECK_UPTO
1925
1926 /*
1927 * Sets descriptor owner. If the owner is a process, 'pgid'
1928 * is set to positive value, process ID. If the owner is process group,
1929 * 'pgid' is set to -pg_id.
1930 */
1931 int
1932 fsetown(struct proc *p, pid_t *pgid, int cmd, const void *data)
1933 {
1934 int id = *(const int *)data;
1935 int error;
1936
1937 switch (cmd) {
1938 case TIOCSPGRP:
1939 if (id < 0)
1940 return (EINVAL);
1941 id = -id;
1942 break;
1943 default:
1944 break;
1945 }
1946
1947 if (id > 0 && !pfind(id))
1948 return (ESRCH);
1949 else if (id < 0 && (error = pgid_in_session(p, -id)))
1950 return (error);
1951
1952 *pgid = id;
1953 return (0);
1954 }
1955
1956 /*
1957 * Return descriptor owner information. If the value is positive,
1958 * it's process ID. If it's negative, it's process group ID and
1959 * needs the sign removed before use.
1960 */
1961 int
1962 fgetown(struct proc *p, pid_t pgid, int cmd, void *data)
1963 {
1964 switch (cmd) {
1965 case TIOCGPGRP:
1966 *(int *)data = -pgid;
1967 break;
1968 default:
1969 *(int *)data = pgid;
1970 break;
1971 }
1972 return (0);
1973 }
1974
1975 /*
1976 * Send signal to descriptor owner, either process or process group.
1977 */
1978 void
1979 fownsignal(pid_t pgid, int signo, int code, int band, void *fdescdata)
1980 {
1981 struct proc *p1;
1982 struct pgrp *pgrp;
1983 ksiginfo_t ksi;
1984
1985 KSI_INIT(&ksi);
1986 ksi.ksi_signo = signo;
1987 ksi.ksi_code = code;
1988 ksi.ksi_band = band;
1989
1990 /*
1991 * Since we may be called from an interrupt context, we must use
1992 * the proclist_mutex.
1993 */
1994 mutex_enter(&proclist_mutex);
1995 if (pgid > 0 && (p1 = p_find(pgid, PFIND_LOCKED)))
1996 kpsignal(p1, &ksi, fdescdata);
1997 else if (pgid < 0 && (pgrp = pg_find(-pgid, PFIND_LOCKED)))
1998 kpgsignal(pgrp, &ksi, fdescdata, 0);
1999 mutex_exit(&proclist_mutex);
2000 }
2001
2002 int
2003 fdclone(struct lwp *l, struct file *fp, int fd, int flag,
2004 const struct fileops *fops, void *data)
2005 {
2006 fp->f_flag = flag;
2007 fp->f_type = DTYPE_MISC;
2008 fp->f_ops = fops;
2009 fp->f_data = data;
2010
2011 l->l_dupfd = fd;
2012
2013 FILE_SET_MATURE(fp);
2014 FILE_UNUSE(fp, l);
2015 return EMOVEFD;
2016 }
2017
2018 /* ARGSUSED */
2019 int
2020 fnullop_fcntl(struct file *fp, u_int cmd, void *data, struct lwp *l)
2021 {
2022
2023 if (cmd == F_SETFL)
2024 return 0;
2025
2026 return EOPNOTSUPP;
2027 }
2028
2029 /* ARGSUSED */
2030 int
2031 fnullop_poll(struct file *fp, int which, struct lwp *l)
2032 {
2033
2034 return 0;
2035 }
2036
2037
2038 /* ARGSUSED */
2039 int
2040 fnullop_kqfilter(struct file *fp, struct knote *kn)
2041 {
2042
2043 return 0;
2044 }
2045
2046 /* ARGSUSED */
2047 int
2048 fbadop_read(struct file *fp, off_t *offset, struct uio *uio,
2049 kauth_cred_t cred, int flags)
2050 {
2051
2052 return EOPNOTSUPP;
2053 }
2054
2055 /* ARGSUSED */
2056 int
2057 fbadop_write(struct file *fp, off_t *offset, struct uio *uio,
2058 kauth_cred_t cred, int flags)
2059 {
2060
2061 return EOPNOTSUPP;
2062 }
2063
2064 /* ARGSUSED */
2065 int
2066 fbadop_ioctl(struct file *fp, u_long com, void *data, struct lwp *l)
2067 {
2068
2069 return EOPNOTSUPP;
2070 }
2071
2072 /* ARGSUSED */
2073 int
2074 fbadop_stat(struct file *fp, struct stat *sb, struct lwp *l)
2075 {
2076
2077 return EOPNOTSUPP;
2078 }
2079
2080 /* ARGSUSED */
2081 int
2082 fbadop_close(struct file *fp, struct lwp *l)
2083 {
2084
2085 return EOPNOTSUPP;
2086 }
2087