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